xref: /onnv-gate/usr/src/cmd/sendmail/src/main.c (revision 11440:802724e2906a)
10Sstevel@tonic-gate /*
2*11440SJohn.Beck@Sun.COM  * Copyright (c) 1998-2006, 2008, 2009 Sendmail, Inc. and its suppliers.
30Sstevel@tonic-gate  *	All rights reserved.
40Sstevel@tonic-gate  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
50Sstevel@tonic-gate  * Copyright (c) 1988, 1993
60Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
90Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
100Sstevel@tonic-gate  * the sendmail distribution.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  */
130Sstevel@tonic-gate 
140Sstevel@tonic-gate /*
151658Sjbeck  * Copyright 1996-2006 Sun Microsystems, Inc.  All rights reserved.
160Sstevel@tonic-gate  * Use is subject to license terms.
170Sstevel@tonic-gate  */
180Sstevel@tonic-gate 
190Sstevel@tonic-gate #define _DEFINE
200Sstevel@tonic-gate #include <sendmail.h>
213544Sjbeck #include <sm/sendmail.h>
220Sstevel@tonic-gate #include <sm/xtrap.h>
230Sstevel@tonic-gate #include <sm/signal.h>
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #ifndef lint
260Sstevel@tonic-gate SM_UNUSED(static char copyright[]) =
270Sstevel@tonic-gate "@(#) Copyright (c) 1998-2003 Sendmail, Inc. and its suppliers.\n\
280Sstevel@tonic-gate @(#)	All rights reserved.\n\
290Sstevel@tonic-gate @(#) Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.\n\
300Sstevel@tonic-gate @(#) Copyright (c) 1988, 1993\n\
310Sstevel@tonic-gate @(#)	The Regents of the University of California.  All rights reserved.\n\
321658Sjbeck @(#) Copyright 1996-2006 Sun Microsystems, Inc.  All rights reserved.\n\
330Sstevel@tonic-gate @(#) Use is subject to license terms.\n";
340Sstevel@tonic-gate #endif /* ! lint */
350Sstevel@tonic-gate 
36*11440SJohn.Beck@Sun.COM SM_RCSID("@(#)$Id: main.c,v 8.971 2009/12/18 17:08:01 ca Exp $")
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #if NETINET || NETINET6
390Sstevel@tonic-gate # include <arpa/inet.h>
400Sstevel@tonic-gate #endif /* NETINET || NETINET6 */
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /* for getcfname() */
430Sstevel@tonic-gate #include <sendmail/pathnames.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static SM_DEBUG_T
460Sstevel@tonic-gate DebugNoPRestart = SM_DEBUG_INITIALIZER("no_persistent_restart",
470Sstevel@tonic-gate 	"@(#)$Debug: no_persistent_restart - don't restart, log only $");
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static void	dump_class __P((STAB *, int));
500Sstevel@tonic-gate static void	obsolete __P((char **));
510Sstevel@tonic-gate static void	testmodeline __P((char *, ENVELOPE *));
520Sstevel@tonic-gate static char	*getextenv __P((const char *));
530Sstevel@tonic-gate static void	sm_printoptions __P((char **));
540Sstevel@tonic-gate static SIGFUNC_DECL	intindebug __P((int));
550Sstevel@tonic-gate static SIGFUNC_DECL	sighup __P((int));
560Sstevel@tonic-gate static SIGFUNC_DECL	sigpipe __P((int));
570Sstevel@tonic-gate static SIGFUNC_DECL	sigterm __P((int));
580Sstevel@tonic-gate #ifdef SIGUSR1
590Sstevel@tonic-gate static SIGFUNC_DECL	sigusr1 __P((int));
600Sstevel@tonic-gate #endif /* SIGUSR1 */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate **  SENDMAIL -- Post mail to a set of destinations.
640Sstevel@tonic-gate **
650Sstevel@tonic-gate **	This is the basic mail router.  All user mail programs should
660Sstevel@tonic-gate **	call this routine to actually deliver mail.  Sendmail in
670Sstevel@tonic-gate **	turn calls a bunch of mail servers that do the real work of
680Sstevel@tonic-gate **	delivering the mail.
690Sstevel@tonic-gate **
700Sstevel@tonic-gate **	Sendmail is driven by settings read in from /etc/mail/sendmail.cf
710Sstevel@tonic-gate **	(read by readcf.c).
720Sstevel@tonic-gate **
730Sstevel@tonic-gate **	Usage:
740Sstevel@tonic-gate **		/usr/lib/sendmail [flags] addr ...
750Sstevel@tonic-gate **
760Sstevel@tonic-gate **		See the associated documentation for details.
770Sstevel@tonic-gate **
780Sstevel@tonic-gate **	Authors:
790Sstevel@tonic-gate **		Eric Allman, UCB/INGRES (until 10/81).
800Sstevel@tonic-gate **			     Britton-Lee, Inc., purveyors of fine
810Sstevel@tonic-gate **				database computers (11/81 - 10/88).
820Sstevel@tonic-gate **			     International Computer Science Institute
830Sstevel@tonic-gate **				(11/88 - 9/89).
840Sstevel@tonic-gate **			     UCB/Mammoth Project (10/89 - 7/95).
850Sstevel@tonic-gate **			     InReference, Inc. (8/95 - 1/97).
860Sstevel@tonic-gate **			     Sendmail, Inc. (1/98 - present).
870Sstevel@tonic-gate **		The support of my employers is gratefully acknowledged.
880Sstevel@tonic-gate **			Few of them (Britton-Lee in particular) have had
890Sstevel@tonic-gate **			anything to gain from my involvement in this project.
900Sstevel@tonic-gate **
910Sstevel@tonic-gate **		Gregory Neil Shapiro,
920Sstevel@tonic-gate **			Worcester Polytechnic Institute	(until 3/98).
930Sstevel@tonic-gate **			Sendmail, Inc. (3/98 - present).
940Sstevel@tonic-gate **
950Sstevel@tonic-gate **		Claus Assmann,
960Sstevel@tonic-gate **			Sendmail, Inc. (12/98 - present).
970Sstevel@tonic-gate */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate char		*FullName;	/* sender's full name */
1000Sstevel@tonic-gate ENVELOPE	BlankEnvelope;	/* a "blank" envelope */
1010Sstevel@tonic-gate static ENVELOPE	MainEnvelope;	/* the envelope around the basic letter */
1020Sstevel@tonic-gate ADDRESS		NullAddress =	/* a null address */
1030Sstevel@tonic-gate 		{ "", "", NULL, "" };
1040Sstevel@tonic-gate char		*CommandLineArgs;	/* command line args for pid file */
1050Sstevel@tonic-gate bool		Warn_Q_option = false;	/* warn about Q option use */
1060Sstevel@tonic-gate static int	MissingFds = 0;	/* bit map of fds missing on startup */
1070Sstevel@tonic-gate char		*Mbdb = "pw";	/* mailbox database defaults to /etc/passwd */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #ifdef NGROUPS_MAX
1100Sstevel@tonic-gate GIDSET_T	InitialGidSet[NGROUPS_MAX];
1110Sstevel@tonic-gate #endif /* NGROUPS_MAX */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #define MAXCONFIGLEVEL	10	/* highest config version level known */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate #if SASL
1160Sstevel@tonic-gate static sasl_callback_t srvcallbacks[] =
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	{	SASL_CB_VERIFYFILE,	&safesaslfile,	NULL	},
1190Sstevel@tonic-gate 	{	SASL_CB_PROXY_POLICY,	&proxy_policy,	NULL	},
1200Sstevel@tonic-gate 	{	SASL_CB_LIST_END,	NULL,		NULL	}
1210Sstevel@tonic-gate };
1220Sstevel@tonic-gate #endif /* SASL */
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate unsigned int	SubmitMode;
1250Sstevel@tonic-gate int		SyslogPrefixLen; /* estimated length of syslog prefix */
1260Sstevel@tonic-gate #define PIDLEN		6	/* pid length for computing SyslogPrefixLen */
1270Sstevel@tonic-gate #ifndef SL_FUDGE
1280Sstevel@tonic-gate # define SL_FUDGE	10	/* fudge offset for SyslogPrefixLen */
1290Sstevel@tonic-gate #endif /* ! SL_FUDGE */
1300Sstevel@tonic-gate #define SLDLL		8	/* est. length of default syslog label */
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /* Some options are dangerous to allow users to use in non-submit mode */
1340Sstevel@tonic-gate #define CHECK_AGAINST_OPMODE(cmd)					\
1350Sstevel@tonic-gate {									\
1360Sstevel@tonic-gate 	if (extraprivs &&						\
1370Sstevel@tonic-gate 	    OpMode != MD_DELIVER && OpMode != MD_SMTP &&		\
138*11440SJohn.Beck@Sun.COM 	    OpMode != MD_ARPAFTP && OpMode != MD_CHECKCONFIG &&		\
1390Sstevel@tonic-gate 	    OpMode != MD_VERIFY && OpMode != MD_TEST)			\
1400Sstevel@tonic-gate 	{								\
1410Sstevel@tonic-gate 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,		\
1420Sstevel@tonic-gate 				     "WARNING: Ignoring submission mode -%c option (not in submission mode)\n", \
1430Sstevel@tonic-gate 		       (cmd));						\
1440Sstevel@tonic-gate 		break;							\
1450Sstevel@tonic-gate 	}								\
1460Sstevel@tonic-gate 	if (extraprivs && queuerun)					\
1470Sstevel@tonic-gate 	{								\
1480Sstevel@tonic-gate 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,		\
1490Sstevel@tonic-gate 				     "WARNING: Ignoring submission mode -%c option with -q\n", \
1500Sstevel@tonic-gate 		       (cmd));						\
1510Sstevel@tonic-gate 		break;							\
1520Sstevel@tonic-gate 	}								\
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate int
main(argc,argv,envp)1560Sstevel@tonic-gate main(argc, argv, envp)
1570Sstevel@tonic-gate 	int argc;
1580Sstevel@tonic-gate 	char **argv;
1590Sstevel@tonic-gate 	char **envp;
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	register char *p;
1620Sstevel@tonic-gate 	char **av;
1630Sstevel@tonic-gate 	extern char Version[];
1640Sstevel@tonic-gate 	char *ep, *from;
1650Sstevel@tonic-gate 	STAB *st;
1660Sstevel@tonic-gate 	register int i;
1670Sstevel@tonic-gate 	int j;
1680Sstevel@tonic-gate 	int dp;
1690Sstevel@tonic-gate 	int fill_errno;
1700Sstevel@tonic-gate 	int qgrp = NOQGRP;		/* queue group to process */
1710Sstevel@tonic-gate 	bool safecf = true;
1720Sstevel@tonic-gate 	BITMAP256 *p_flags = NULL;	/* daemon flags */
1730Sstevel@tonic-gate 	bool warn_C_flag = false;
1740Sstevel@tonic-gate 	bool auth = true;		/* whether to set e_auth_param */
1750Sstevel@tonic-gate 	char warn_f_flag = '\0';
1760Sstevel@tonic-gate 	bool run_in_foreground = false;	/* -bD mode */
1770Sstevel@tonic-gate 	bool queuerun = false, debug = false;
1780Sstevel@tonic-gate 	struct passwd *pw;
1790Sstevel@tonic-gate 	struct hostent *hp;
1800Sstevel@tonic-gate 	char *nullserver = NULL;
1810Sstevel@tonic-gate 	char *authinfo = NULL;
1820Sstevel@tonic-gate 	char *sysloglabel = NULL;	/* label for syslog */
1830Sstevel@tonic-gate 	char *conffile = NULL;		/* name of .cf file */
1840Sstevel@tonic-gate 	char *queuegroup = NULL;	/* queue group to process */
1850Sstevel@tonic-gate 	char *quarantining = NULL;	/* quarantine queue items? */
1860Sstevel@tonic-gate 	bool extraprivs;
1870Sstevel@tonic-gate 	bool forged, negate;
1880Sstevel@tonic-gate 	bool queuepersistent = false;	/* queue runner process runs forever */
1890Sstevel@tonic-gate 	bool foregroundqueue = false;	/* queue run in foreground */
1900Sstevel@tonic-gate 	bool save_val;			/* to save some bool var. */
1910Sstevel@tonic-gate 	int cftype;			/* which cf file to use? */
1920Sstevel@tonic-gate 	SM_FILE_T *smdebug;
1930Sstevel@tonic-gate 	static time_t starttime = 0;	/* when was process started */
1940Sstevel@tonic-gate 	struct stat traf_st;		/* for TrafficLog FIFO check */
1950Sstevel@tonic-gate 	char buf[MAXLINE];
1960Sstevel@tonic-gate 	char jbuf[MAXHOSTNAMELEN];	/* holds MyHostName */
1970Sstevel@tonic-gate 	static char rnamebuf[MAXNAME];	/* holds RealUserName */
1980Sstevel@tonic-gate 	char *emptyenviron[1];
1990Sstevel@tonic-gate #if STARTTLS
2000Sstevel@tonic-gate 	bool tls_ok;
2010Sstevel@tonic-gate #endif /* STARTTLS */
2020Sstevel@tonic-gate 	QUEUE_CHAR *new;
2030Sstevel@tonic-gate 	ENVELOPE *e;
2040Sstevel@tonic-gate 	extern int DtableSize;
2050Sstevel@tonic-gate 	extern int optind;
2060Sstevel@tonic-gate 	extern int opterr;
2070Sstevel@tonic-gate 	extern char *optarg;
2080Sstevel@tonic-gate 	extern char **environ;
2090Sstevel@tonic-gate #if SASL
2100Sstevel@tonic-gate 	extern void sm_sasl_init __P((void));
2110Sstevel@tonic-gate #endif /* SASL */
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate #if USE_ENVIRON
2140Sstevel@tonic-gate 	envp = environ;
2150Sstevel@tonic-gate #endif /* USE_ENVIRON */
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/* turn off profiling */
2180Sstevel@tonic-gate 	SM_PROF(0);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	/* install default exception handler */
2210Sstevel@tonic-gate 	sm_exc_newthread(fatal_error);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	/* set the default in/out channel so errors reported to screen */
2240Sstevel@tonic-gate 	InChannel = smioin;
2250Sstevel@tonic-gate 	OutChannel = smioout;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/*
2280Sstevel@tonic-gate 	**  Check to see if we reentered.
2290Sstevel@tonic-gate 	**	This would normally happen if e_putheader or e_putbody
2300Sstevel@tonic-gate 	**	were NULL when invoked.
2310Sstevel@tonic-gate 	*/
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if (starttime != 0)
2340Sstevel@tonic-gate 	{
2350Sstevel@tonic-gate 		syserr("main: reentered!");
2360Sstevel@tonic-gate 		abort();
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	starttime = curtime();
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/* avoid null pointer dereferences */
2413544Sjbeck 	TermEscape.te_rv_on = TermEscape.te_under_on = TermEscape.te_normal = "";
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	RealUid = getuid();
2440Sstevel@tonic-gate 	RealGid = getgid();
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/* Check if sendmail is running with extra privs */
2470Sstevel@tonic-gate 	extraprivs = (RealUid != 0 &&
2480Sstevel@tonic-gate 		      (geteuid() != getuid() || getegid() != getgid()));
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	CurrentPid = getpid();
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/* get whatever .cf file is right for the opmode */
2530Sstevel@tonic-gate 	cftype = SM_GET_RIGHT_CF;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/* in 4.4BSD, the table can be huge; impose a reasonable limit */
2560Sstevel@tonic-gate 	DtableSize = getdtsize();
2570Sstevel@tonic-gate 	if (DtableSize > 256)
2580Sstevel@tonic-gate 		DtableSize = 256;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	**  Be sure we have enough file descriptors.
2620Sstevel@tonic-gate 	**	But also be sure that 0, 1, & 2 are open.
2630Sstevel@tonic-gate 	*/
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	/* reset errno and fill_errno; the latter is used way down below */
2660Sstevel@tonic-gate 	errno = fill_errno = 0;
2670Sstevel@tonic-gate 	fill_fd(STDIN_FILENO, NULL);
2680Sstevel@tonic-gate 	if (errno != 0)
2690Sstevel@tonic-gate 		fill_errno = errno;
2700Sstevel@tonic-gate 	fill_fd(STDOUT_FILENO, NULL);
2710Sstevel@tonic-gate 	if (errno != 0)
2720Sstevel@tonic-gate 		fill_errno = errno;
2730Sstevel@tonic-gate 	fill_fd(STDERR_FILENO, NULL);
2740Sstevel@tonic-gate 	if (errno != 0)
2750Sstevel@tonic-gate 		fill_errno = errno;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	sm_closefrom(STDERR_FILENO + 1, DtableSize);
2780Sstevel@tonic-gate 	errno = 0;
2790Sstevel@tonic-gate 	smdebug = NULL;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate #if LOG
2820Sstevel@tonic-gate # ifndef SM_LOG_STR
2830Sstevel@tonic-gate #  define SM_LOG_STR	"sendmail"
2840Sstevel@tonic-gate # endif /* ! SM_LOG_STR */
2850Sstevel@tonic-gate #  ifdef LOG_MAIL
2860Sstevel@tonic-gate 	openlog(SM_LOG_STR, LOG_PID, LOG_MAIL);
2870Sstevel@tonic-gate #  else /* LOG_MAIL */
2880Sstevel@tonic-gate 	openlog(SM_LOG_STR, LOG_PID);
2890Sstevel@tonic-gate #  endif /* LOG_MAIL */
2900Sstevel@tonic-gate #endif /* LOG */
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	/*
2930Sstevel@tonic-gate 	**  Seed the random number generator.
2940Sstevel@tonic-gate 	**  Used for queue file names, picking a queue directory, and
2950Sstevel@tonic-gate 	**  MX randomization.
2960Sstevel@tonic-gate 	*/
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	seed_random();
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	/* do machine-dependent initializations */
3010Sstevel@tonic-gate 	init_md(argc, argv);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	SyslogPrefixLen = PIDLEN + (MAXQFNAME - 3) + SL_FUDGE + SLDLL;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/* reset status from syserr() calls for missing file descriptors */
3070Sstevel@tonic-gate 	Errors = 0;
3080Sstevel@tonic-gate 	ExitStat = EX_OK;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	SubmitMode = SUBMIT_UNKNOWN;
3116562Sjbeck #if _FFR_LOCAL_DAEMON
3126562Sjbeck 	LocalDaemon = false;
3136562Sjbeck #endif /* _FFR_LOCAL_DAEMON */
3140Sstevel@tonic-gate #if XDEBUG
3150Sstevel@tonic-gate 	checkfd012("after openlog");
3160Sstevel@tonic-gate #endif /* XDEBUG */
3170Sstevel@tonic-gate 
3183544Sjbeck 	tTsetup(tTdvect, sizeof(tTdvect), "0-99.1,*_trace_*.1");
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate #ifdef NGROUPS_MAX
3210Sstevel@tonic-gate 	/* save initial group set for future checks */
3220Sstevel@tonic-gate 	i = getgroups(NGROUPS_MAX, InitialGidSet);
3230Sstevel@tonic-gate 	if (i <= 0)
3240Sstevel@tonic-gate 	{
3250Sstevel@tonic-gate 		InitialGidSet[0] = (GID_T) -1;
3260Sstevel@tonic-gate 		i = 0;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 	while (i < NGROUPS_MAX)
3290Sstevel@tonic-gate 		InitialGidSet[i++] = InitialGidSet[0];
3300Sstevel@tonic-gate #endif /* NGROUPS_MAX */
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/* drop group id privileges (RunAsUser not yet set) */
3330Sstevel@tonic-gate 	dp = drop_privileges(false);
3340Sstevel@tonic-gate 	setstat(dp);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate #ifdef SIGUSR1
3370Sstevel@tonic-gate 	/* Only allow root (or non-set-*-ID binaries) to use SIGUSR1 */
3380Sstevel@tonic-gate 	if (!extraprivs)
3390Sstevel@tonic-gate 	{
3400Sstevel@tonic-gate 		/* arrange to dump state on user-1 signal */
3410Sstevel@tonic-gate 		(void) sm_signal(SIGUSR1, sigusr1);
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 	else
3440Sstevel@tonic-gate 	{
3450Sstevel@tonic-gate 		/* ignore user-1 signal */
3460Sstevel@tonic-gate 		(void) sm_signal(SIGUSR1, SIG_IGN);
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate #endif /* SIGUSR1 */
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/* initialize for setproctitle */
3510Sstevel@tonic-gate 	initsetproctitle(argc, argv, envp);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	/* Handle any non-getoptable constructions. */
3540Sstevel@tonic-gate 	obsolete(argv);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/*
3570Sstevel@tonic-gate 	**  Do a quick prescan of the argument list.
3580Sstevel@tonic-gate 	*/
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	/* find initial opMode */
3620Sstevel@tonic-gate 	OpMode = MD_DELIVER;
3630Sstevel@tonic-gate 	av = argv;
3640Sstevel@tonic-gate 	p = strrchr(*av, '/');
3650Sstevel@tonic-gate 	if (p++ == NULL)
3660Sstevel@tonic-gate 		p = *av;
3670Sstevel@tonic-gate 	if (strcmp(p, "newaliases") == 0)
3680Sstevel@tonic-gate 		OpMode = MD_INITALIAS;
3690Sstevel@tonic-gate 	else if (strcmp(p, "mailq") == 0)
3700Sstevel@tonic-gate 		OpMode = MD_PRINT;
3710Sstevel@tonic-gate 	else if (strcmp(p, "smtpd") == 0)
3720Sstevel@tonic-gate 		OpMode = MD_DAEMON;
3730Sstevel@tonic-gate 	else if (strcmp(p, "hoststat") == 0)
3740Sstevel@tonic-gate 		OpMode = MD_HOSTSTAT;
3750Sstevel@tonic-gate 	else if (strcmp(p, "purgestat") == 0)
3760Sstevel@tonic-gate 		OpMode = MD_PURGESTAT;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate #if defined(__osf__) || defined(_AIX3)
3790Sstevel@tonic-gate # define OPTIONS	"A:B:b:C:cD:d:e:F:f:Gh:IiL:M:mN:nO:o:p:Q:q:R:r:sTtV:vX:x"
3800Sstevel@tonic-gate #endif /* defined(__osf__) || defined(_AIX3) */
3810Sstevel@tonic-gate #if defined(sony_news)
3820Sstevel@tonic-gate # define OPTIONS	"A:B:b:C:cD:d:E:e:F:f:Gh:IiJ:L:M:mN:nO:o:p:Q:q:R:r:sTtV:vX:"
3830Sstevel@tonic-gate #endif /* defined(sony_news) */
3840Sstevel@tonic-gate #ifndef OPTIONS
3850Sstevel@tonic-gate # define OPTIONS	"A:B:b:C:cD:d:e:F:f:Gh:IiL:M:mN:nO:o:p:Q:q:R:r:sTtV:vX:"
3860Sstevel@tonic-gate #endif /* ! OPTIONS */
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	/* Set to 0 to allow -b; need to check optarg before using it! */
3890Sstevel@tonic-gate 	opterr = 0;
3900Sstevel@tonic-gate 	while ((j = getopt(argc, argv, OPTIONS)) != -1)
3910Sstevel@tonic-gate 	{
3920Sstevel@tonic-gate 		switch (j)
3930Sstevel@tonic-gate 		{
3940Sstevel@tonic-gate 		  case 'b':	/* operations mode */
3950Sstevel@tonic-gate 			j = (optarg == NULL) ? ' ' : *optarg;
3960Sstevel@tonic-gate 			switch (j)
3970Sstevel@tonic-gate 			{
3980Sstevel@tonic-gate 			  case MD_DAEMON:
3990Sstevel@tonic-gate 			  case MD_FGDAEMON:
4000Sstevel@tonic-gate 			  case MD_SMTP:
4010Sstevel@tonic-gate 			  case MD_INITALIAS:
4020Sstevel@tonic-gate 			  case MD_DELIVER:
4030Sstevel@tonic-gate 			  case MD_VERIFY:
4040Sstevel@tonic-gate 			  case MD_TEST:
4050Sstevel@tonic-gate 			  case MD_PRINT:
4060Sstevel@tonic-gate 			  case MD_PRINTNQE:
4070Sstevel@tonic-gate 			  case MD_HOSTSTAT:
4080Sstevel@tonic-gate 			  case MD_PURGESTAT:
4090Sstevel@tonic-gate 			  case MD_ARPAFTP:
410*11440SJohn.Beck@Sun.COM #if _FFR_CHECKCONFIG
411*11440SJohn.Beck@Sun.COM 			  case MD_CHECKCONFIG:
412*11440SJohn.Beck@Sun.COM #endif /* _FFR_CHECKCONFIG */
4130Sstevel@tonic-gate 				OpMode = j;
4140Sstevel@tonic-gate 				break;
4150Sstevel@tonic-gate 
4166562Sjbeck #if _FFR_LOCAL_DAEMON
4176562Sjbeck 			  case MD_LOCAL:
4186562Sjbeck 				OpMode = MD_DAEMON;
4196562Sjbeck 				LocalDaemon = true;
4206562Sjbeck 				break;
4216562Sjbeck #endif /* _FFR_LOCAL_DAEMON */
4226562Sjbeck 
4230Sstevel@tonic-gate 			  case MD_FREEZE:
4240Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4250Sstevel@tonic-gate 						     "Frozen configurations unsupported\n");
4260Sstevel@tonic-gate 				return EX_USAGE;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 			  default:
4290Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4300Sstevel@tonic-gate 						     "Invalid operation mode %c\n",
4310Sstevel@tonic-gate 						     j);
4320Sstevel@tonic-gate 				return EX_USAGE;
4330Sstevel@tonic-gate 			}
4340Sstevel@tonic-gate 			break;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 		  case 'D':
4370Sstevel@tonic-gate 			if (debug)
4380Sstevel@tonic-gate 			{
4390Sstevel@tonic-gate 				errno = 0;
4400Sstevel@tonic-gate 				syserr("-D file must be before -d");
4410Sstevel@tonic-gate 				ExitStat = EX_USAGE;
4420Sstevel@tonic-gate 				break;
4430Sstevel@tonic-gate 			}
4440Sstevel@tonic-gate 			dp = drop_privileges(true);
4450Sstevel@tonic-gate 			setstat(dp);
4460Sstevel@tonic-gate 			smdebug = sm_io_open(SmFtStdio, SM_TIME_DEFAULT,
4470Sstevel@tonic-gate 					    optarg, SM_IO_APPEND, NULL);
4480Sstevel@tonic-gate 			if (smdebug == NULL)
4490Sstevel@tonic-gate 			{
4500Sstevel@tonic-gate 				syserr("cannot open %s", optarg);
4510Sstevel@tonic-gate 				ExitStat = EX_CANTCREAT;
4520Sstevel@tonic-gate 				break;
4530Sstevel@tonic-gate 			}
4540Sstevel@tonic-gate 			sm_debug_setfile(smdebug);
4550Sstevel@tonic-gate 			break;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		  case 'd':
4580Sstevel@tonic-gate 			debug = true;
4590Sstevel@tonic-gate 			tTflag(optarg);
4600Sstevel@tonic-gate 			(void) sm_io_setvbuf(sm_debug_file(), SM_TIME_DEFAULT,
4610Sstevel@tonic-gate 					     (char *) NULL, SM_IO_NBF,
4620Sstevel@tonic-gate 					     SM_IO_BUFSIZ);
4630Sstevel@tonic-gate 			break;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		  case 'G':	/* relay (gateway) submission */
4660Sstevel@tonic-gate 			SubmitMode = SUBMIT_MTA;
4670Sstevel@tonic-gate 			break;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 		  case 'L':
4700Sstevel@tonic-gate 			if (optarg == NULL)
4710Sstevel@tonic-gate 			{
4720Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4730Sstevel@tonic-gate 						     "option requires an argument -- '%c'",
4740Sstevel@tonic-gate 						     (char) j);
4750Sstevel@tonic-gate 				return EX_USAGE;
4760Sstevel@tonic-gate 			}
4770Sstevel@tonic-gate 			j = SM_MIN(strlen(optarg), 32) + 1;
4780Sstevel@tonic-gate 			sysloglabel = xalloc(j);
4790Sstevel@tonic-gate 			(void) sm_strlcpy(sysloglabel, optarg, j);
4800Sstevel@tonic-gate 			SyslogPrefixLen = PIDLEN + (MAXQFNAME - 3) +
4810Sstevel@tonic-gate 					  SL_FUDGE + j;
4820Sstevel@tonic-gate 			break;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		  case 'Q':
4850Sstevel@tonic-gate 		  case 'q':
4860Sstevel@tonic-gate 			/* just check if it is there */
4870Sstevel@tonic-gate 			queuerun = true;
4880Sstevel@tonic-gate 			break;
4890Sstevel@tonic-gate 		}
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 	opterr = 1;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	/* Don't leak queue information via debug flags */
4940Sstevel@tonic-gate 	if (extraprivs && queuerun && debug)
4950Sstevel@tonic-gate 	{
4960Sstevel@tonic-gate 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
4970Sstevel@tonic-gate 				     "WARNING: Can not use -d with -q.  Disabling debugging.\n");
4980Sstevel@tonic-gate 		sm_debug_close();
4990Sstevel@tonic-gate 		sm_debug_setfile(NULL);
5003544Sjbeck 		(void) memset(tTdvect, '\0', sizeof(tTdvect));
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate #if LOG
5040Sstevel@tonic-gate 	if (sysloglabel != NULL)
5050Sstevel@tonic-gate 	{
5060Sstevel@tonic-gate 		/* Sanitize the string */
5070Sstevel@tonic-gate 		for (p = sysloglabel; *p != '\0'; p++)
5080Sstevel@tonic-gate 		{
5090Sstevel@tonic-gate 			if (!isascii(*p) || !isprint(*p) || *p == '%')
5100Sstevel@tonic-gate 				*p = '*';
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 		closelog();
5130Sstevel@tonic-gate #  ifdef LOG_MAIL
5140Sstevel@tonic-gate 		openlog(sysloglabel, LOG_PID, LOG_MAIL);
5150Sstevel@tonic-gate #  else /* LOG_MAIL */
5160Sstevel@tonic-gate 		openlog(sysloglabel, LOG_PID);
5170Sstevel@tonic-gate #  endif /* LOG_MAIL */
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate #endif /* LOG */
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/* set up the blank envelope */
5220Sstevel@tonic-gate 	BlankEnvelope.e_puthdr = putheader;
5230Sstevel@tonic-gate 	BlankEnvelope.e_putbody = putbody;
5240Sstevel@tonic-gate 	BlankEnvelope.e_xfp = NULL;
5250Sstevel@tonic-gate 	STRUCTCOPY(NullAddress, BlankEnvelope.e_from);
5260Sstevel@tonic-gate 	CurEnv = &BlankEnvelope;
5270Sstevel@tonic-gate 	STRUCTCOPY(NullAddress, MainEnvelope.e_from);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	/*
5300Sstevel@tonic-gate 	**  Set default values for variables.
5310Sstevel@tonic-gate 	**	These cannot be in initialized data space.
5320Sstevel@tonic-gate 	*/
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	setdefaults(&BlankEnvelope);
5350Sstevel@tonic-gate 	initmacros(&BlankEnvelope);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	/* reset macro */
5380Sstevel@tonic-gate 	set_op_mode(OpMode);
5392197Sjbeck 	if (OpMode == MD_DAEMON)
5402197Sjbeck 		DaemonPid = CurrentPid;	/* needed for finis() to work */
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	pw = sm_getpwuid(RealUid);
5430Sstevel@tonic-gate 	if (pw != NULL)
5443544Sjbeck 		(void) sm_strlcpy(rnamebuf, pw->pw_name, sizeof(rnamebuf));
5450Sstevel@tonic-gate 	else
5463544Sjbeck 		(void) sm_snprintf(rnamebuf, sizeof(rnamebuf), "Unknown UID %d",
5470Sstevel@tonic-gate 				   (int) RealUid);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	RealUserName = rnamebuf;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	if (tTd(0, 101))
5520Sstevel@tonic-gate 	{
5530Sstevel@tonic-gate 		sm_dprintf("Version %s\n", Version);
5540Sstevel@tonic-gate 		finis(false, true, EX_OK);
5550Sstevel@tonic-gate 		/* NOTREACHED */
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	/*
5590Sstevel@tonic-gate 	**  if running non-set-user-ID binary as non-root, pretend
5600Sstevel@tonic-gate 	**  we are the RunAsUid
5610Sstevel@tonic-gate 	*/
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	if (RealUid != 0 && geteuid() == RealUid)
5640Sstevel@tonic-gate 	{
5650Sstevel@tonic-gate 		if (tTd(47, 1))
5660Sstevel@tonic-gate 			sm_dprintf("Non-set-user-ID binary: RunAsUid = RealUid = %d\n",
5670Sstevel@tonic-gate 				   (int) RealUid);
5680Sstevel@tonic-gate 		RunAsUid = RealUid;
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 	else if (geteuid() != 0)
5710Sstevel@tonic-gate 		RunAsUid = geteuid();
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	EffGid = getegid();
5740Sstevel@tonic-gate 	if (RealUid != 0 && EffGid == RealGid)
5750Sstevel@tonic-gate 		RunAsGid = RealGid;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	if (tTd(47, 5))
5780Sstevel@tonic-gate 	{
5790Sstevel@tonic-gate 		sm_dprintf("main: e/ruid = %d/%d e/rgid = %d/%d\n",
5800Sstevel@tonic-gate 			   (int) geteuid(), (int) getuid(),
5810Sstevel@tonic-gate 			   (int) getegid(), (int) getgid());
5820Sstevel@tonic-gate 		sm_dprintf("main: RunAsUser = %d:%d\n",
5830Sstevel@tonic-gate 			   (int) RunAsUid, (int) RunAsGid);
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	/* save command line arguments */
5870Sstevel@tonic-gate 	j = 0;
5880Sstevel@tonic-gate 	for (av = argv; *av != NULL; )
5890Sstevel@tonic-gate 		j += strlen(*av++) + 1;
5903544Sjbeck 	SaveArgv = (char **) xalloc(sizeof(char *) * (argc + 1));
5910Sstevel@tonic-gate 	CommandLineArgs = xalloc(j);
5920Sstevel@tonic-gate 	p = CommandLineArgs;
5930Sstevel@tonic-gate 	for (av = argv, i = 0; *av != NULL; )
5940Sstevel@tonic-gate 	{
5950Sstevel@tonic-gate 		int h;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 		SaveArgv[i++] = newstr(*av);
5980Sstevel@tonic-gate 		if (av != argv)
5990Sstevel@tonic-gate 			*p++ = ' ';
6000Sstevel@tonic-gate 		(void) sm_strlcpy(p, *av++, j);
6010Sstevel@tonic-gate 		h = strlen(p);
6020Sstevel@tonic-gate 		p += h;
6030Sstevel@tonic-gate 		j -= h + 1;
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 	SaveArgv[i] = NULL;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if (tTd(0, 1))
6080Sstevel@tonic-gate 	{
6090Sstevel@tonic-gate 		extern char *CompileOptions[];
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		sm_dprintf("Version %s\n Compiled with:", Version);
6120Sstevel@tonic-gate 		sm_printoptions(CompileOptions);
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 	if (tTd(0, 10))
6150Sstevel@tonic-gate 	{
6160Sstevel@tonic-gate 		extern char *OsCompileOptions[];
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 		sm_dprintf("    OS Defines:");
6190Sstevel@tonic-gate 		sm_printoptions(OsCompileOptions);
6200Sstevel@tonic-gate #ifdef _PATH_UNIX
6210Sstevel@tonic-gate 		sm_dprintf("Kernel symbols:\t%s\n", _PATH_UNIX);
6220Sstevel@tonic-gate #endif /* _PATH_UNIX */
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		sm_dprintf("     Conf file:\t%s (default for MSP)\n",
6250Sstevel@tonic-gate 			   getcfname(OpMode, SubmitMode, SM_GET_SUBMIT_CF,
6260Sstevel@tonic-gate 				     conffile));
6270Sstevel@tonic-gate 		sm_dprintf("     Conf file:\t%s (default for MTA)\n",
6280Sstevel@tonic-gate 			   getcfname(OpMode, SubmitMode, SM_GET_SENDMAIL_CF,
6290Sstevel@tonic-gate 				     conffile));
6300Sstevel@tonic-gate 		sm_dprintf("      Pid file:\t%s (default)\n", PidFile);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	if (tTd(0, 12))
6340Sstevel@tonic-gate 	{
6350Sstevel@tonic-gate 		extern char *SmCompileOptions[];
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		sm_dprintf(" libsm Defines:");
6380Sstevel@tonic-gate 		sm_printoptions(SmCompileOptions);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	if (tTd(0, 13))
6420Sstevel@tonic-gate 	{
6430Sstevel@tonic-gate 		extern char *FFRCompileOptions[];
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		sm_dprintf("   FFR Defines:");
6460Sstevel@tonic-gate 		sm_printoptions(FFRCompileOptions);
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	/* clear sendmail's environment */
6500Sstevel@tonic-gate 	ExternalEnviron = environ;
6510Sstevel@tonic-gate 	emptyenviron[0] = NULL;
6520Sstevel@tonic-gate 	environ = emptyenviron;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	/*
6550Sstevel@tonic-gate 	**  restore any original TZ setting until TimeZoneSpec has been
6560Sstevel@tonic-gate 	**  determined - or early log messages may get bogus time stamps
6570Sstevel@tonic-gate 	*/
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	if ((p = getextenv("TZ")) != NULL)
6600Sstevel@tonic-gate 	{
6610Sstevel@tonic-gate 		char *tz;
6620Sstevel@tonic-gate 		int tzlen;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 		/* XXX check for reasonable length? */
6650Sstevel@tonic-gate 		tzlen = strlen(p) + 4;
6660Sstevel@tonic-gate 		tz = xalloc(tzlen);
6670Sstevel@tonic-gate 		(void) sm_strlcpyn(tz, tzlen, 2, "TZ=", p);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 		/* XXX check return code? */
6700Sstevel@tonic-gate 		(void) putenv(tz);
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/* prime the child environment */
6741658Sjbeck 	sm_setuserenv("AGENT", "sendmail");
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	(void) sm_signal(SIGPIPE, SIG_IGN);
6770Sstevel@tonic-gate 	OldUmask = umask(022);
6780Sstevel@tonic-gate 	FullName = getextenv("NAME");
6790Sstevel@tonic-gate 	if (FullName != NULL)
6800Sstevel@tonic-gate 		FullName = newstr(FullName);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	/*
6830Sstevel@tonic-gate 	**  Initialize name server if it is going to be used.
6840Sstevel@tonic-gate 	*/
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate #if NAMED_BIND
6870Sstevel@tonic-gate 	if (!bitset(RES_INIT, _res.options))
6880Sstevel@tonic-gate 		(void) res_init();
6890Sstevel@tonic-gate 	if (tTd(8, 8))
6900Sstevel@tonic-gate 		_res.options |= RES_DEBUG;
6910Sstevel@tonic-gate 	else
6920Sstevel@tonic-gate 		_res.options &= ~RES_DEBUG;
6930Sstevel@tonic-gate # ifdef RES_NOALIASES
6940Sstevel@tonic-gate 	_res.options |= RES_NOALIASES;
6950Sstevel@tonic-gate # endif /* RES_NOALIASES */
6960Sstevel@tonic-gate 	TimeOuts.res_retry[RES_TO_DEFAULT] = _res.retry;
6970Sstevel@tonic-gate 	TimeOuts.res_retry[RES_TO_FIRST] = _res.retry;
6980Sstevel@tonic-gate 	TimeOuts.res_retry[RES_TO_NORMAL] = _res.retry;
6990Sstevel@tonic-gate 	TimeOuts.res_retrans[RES_TO_DEFAULT] = _res.retrans;
7000Sstevel@tonic-gate 	TimeOuts.res_retrans[RES_TO_FIRST] = _res.retrans;
7010Sstevel@tonic-gate 	TimeOuts.res_retrans[RES_TO_NORMAL] = _res.retrans;
7020Sstevel@tonic-gate #endif /* NAMED_BIND */
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	errno = 0;
7050Sstevel@tonic-gate 	from = NULL;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	/* initialize some macros, etc. */
7080Sstevel@tonic-gate 	init_vendor_macros(&BlankEnvelope);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	/* version */
7110Sstevel@tonic-gate 	macdefine(&BlankEnvelope.e_macro, A_PERM, 'v', Version);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	/* hostname */
7143544Sjbeck 	hp = myhostname(jbuf, sizeof(jbuf));
7150Sstevel@tonic-gate 	if (jbuf[0] != '\0')
7160Sstevel@tonic-gate 	{
7170Sstevel@tonic-gate 		struct utsname utsname;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		if (tTd(0, 4))
7200Sstevel@tonic-gate 			sm_dprintf("Canonical name: %s\n", jbuf);
7210Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_TEMP, 'w', jbuf);
7220Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_TEMP, 'j', jbuf);
7230Sstevel@tonic-gate 		setclass('w', jbuf);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		p = strchr(jbuf, '.');
7260Sstevel@tonic-gate 		if (p != NULL && p[1] != '\0')
7270Sstevel@tonic-gate 			macdefine(&BlankEnvelope.e_macro, A_TEMP, 'm', &p[1]);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		if (uname(&utsname) >= 0)
7300Sstevel@tonic-gate 			p = utsname.nodename;
7310Sstevel@tonic-gate 		else
7320Sstevel@tonic-gate 		{
7330Sstevel@tonic-gate 			if (tTd(0, 22))
7340Sstevel@tonic-gate 				sm_dprintf("uname failed (%s)\n",
7350Sstevel@tonic-gate 					   sm_errstring(errno));
7360Sstevel@tonic-gate 			makelower(jbuf);
7370Sstevel@tonic-gate 			p = jbuf;
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 		if (tTd(0, 4))
7400Sstevel@tonic-gate 			sm_dprintf(" UUCP nodename: %s\n", p);
7410Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_TEMP, 'k', p);
7420Sstevel@tonic-gate 		setclass('k', p);
7430Sstevel@tonic-gate 		setclass('w', p);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 	if (hp != NULL)
7460Sstevel@tonic-gate 	{
7470Sstevel@tonic-gate 		for (av = hp->h_aliases; av != NULL && *av != NULL; av++)
7480Sstevel@tonic-gate 		{
7490Sstevel@tonic-gate 			if (tTd(0, 4))
7500Sstevel@tonic-gate 				sm_dprintf("\ta.k.a.: %s\n", *av);
7510Sstevel@tonic-gate 			setclass('w', *av);
7520Sstevel@tonic-gate 		}
7530Sstevel@tonic-gate #if NETINET || NETINET6
7540Sstevel@tonic-gate 		for (i = 0; i >= 0 && hp->h_addr_list[i] != NULL; i++)
7550Sstevel@tonic-gate 		{
7560Sstevel@tonic-gate # if NETINET6
7570Sstevel@tonic-gate 			char *addr;
7580Sstevel@tonic-gate 			char buf6[INET6_ADDRSTRLEN];
7590Sstevel@tonic-gate 			struct in6_addr ia6;
7600Sstevel@tonic-gate # endif /* NETINET6 */
7610Sstevel@tonic-gate # if NETINET
7620Sstevel@tonic-gate 			struct in_addr ia;
7630Sstevel@tonic-gate # endif /* NETINET */
7640Sstevel@tonic-gate 			char ipbuf[103];
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 			ipbuf[0] = '\0';
7670Sstevel@tonic-gate 			switch (hp->h_addrtype)
7680Sstevel@tonic-gate 			{
7690Sstevel@tonic-gate # if NETINET
7700Sstevel@tonic-gate 			  case AF_INET:
7710Sstevel@tonic-gate 				if (hp->h_length != INADDRSZ)
7720Sstevel@tonic-gate 					break;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 				memmove(&ia, hp->h_addr_list[i], INADDRSZ);
7753544Sjbeck 				(void) sm_snprintf(ipbuf, sizeof(ipbuf),
7760Sstevel@tonic-gate 						   "[%.100s]", inet_ntoa(ia));
7770Sstevel@tonic-gate 				break;
7780Sstevel@tonic-gate # endif /* NETINET */
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate # if NETINET6
7810Sstevel@tonic-gate 			  case AF_INET6:
7820Sstevel@tonic-gate 				if (hp->h_length != IN6ADDRSZ)
7830Sstevel@tonic-gate 					break;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 				memmove(&ia6, hp->h_addr_list[i], IN6ADDRSZ);
7863544Sjbeck 				addr = anynet_ntop(&ia6, buf6, sizeof(buf6));
7870Sstevel@tonic-gate 				if (addr != NULL)
7883544Sjbeck 					(void) sm_snprintf(ipbuf, sizeof(ipbuf),
7890Sstevel@tonic-gate 							   "[%.100s]", addr);
7900Sstevel@tonic-gate 				break;
7910Sstevel@tonic-gate # endif /* NETINET6 */
7920Sstevel@tonic-gate 			}
7930Sstevel@tonic-gate 			if (ipbuf[0] == '\0')
7940Sstevel@tonic-gate 				break;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 			if (tTd(0, 4))
7970Sstevel@tonic-gate 				sm_dprintf("\ta.k.a.: %s\n", ipbuf);
7980Sstevel@tonic-gate 			setclass('w', ipbuf);
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate #endif /* NETINET || NETINET6 */
8010Sstevel@tonic-gate #if NETINET6
8020Sstevel@tonic-gate 		freehostent(hp);
8030Sstevel@tonic-gate 		hp = NULL;
8040Sstevel@tonic-gate #endif /* NETINET6 */
8050Sstevel@tonic-gate 	}
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	/* current time */
8080Sstevel@tonic-gate 	macdefine(&BlankEnvelope.e_macro, A_TEMP, 'b', arpadate((char *) NULL));
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	/* current load average */
8110Sstevel@tonic-gate 	sm_getla();
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	QueueLimitRecipient = (QUEUE_CHAR *) NULL;
8140Sstevel@tonic-gate 	QueueLimitSender = (QUEUE_CHAR *) NULL;
8150Sstevel@tonic-gate 	QueueLimitId = (QUEUE_CHAR *) NULL;
8160Sstevel@tonic-gate 	QueueLimitQuarantine = (QUEUE_CHAR *) NULL;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	/*
8190Sstevel@tonic-gate 	**  Crack argv.
8200Sstevel@tonic-gate 	*/
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	optind = 1;
8230Sstevel@tonic-gate 	while ((j = getopt(argc, argv, OPTIONS)) != -1)
8240Sstevel@tonic-gate 	{
8250Sstevel@tonic-gate 		switch (j)
8260Sstevel@tonic-gate 		{
8270Sstevel@tonic-gate 		  case 'b':	/* operations mode */
8280Sstevel@tonic-gate 			/* already done */
8290Sstevel@tonic-gate 			break;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 		  case 'A':	/* use Alternate sendmail/submit.cf */
8320Sstevel@tonic-gate 			cftype = optarg[0] == 'c' ? SM_GET_SUBMIT_CF
8330Sstevel@tonic-gate 						  : SM_GET_SENDMAIL_CF;
8340Sstevel@tonic-gate 			break;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 		  case 'B':	/* body type */
8370Sstevel@tonic-gate 			CHECK_AGAINST_OPMODE(j);
8380Sstevel@tonic-gate 			BlankEnvelope.e_bodytype = newstr(optarg);
8390Sstevel@tonic-gate 			break;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 		  case 'C':	/* select configuration file (already done) */
8420Sstevel@tonic-gate 			if (RealUid != 0)
8430Sstevel@tonic-gate 				warn_C_flag = true;
8440Sstevel@tonic-gate 			conffile = newstr(optarg);
8450Sstevel@tonic-gate 			dp = drop_privileges(true);
8460Sstevel@tonic-gate 			setstat(dp);
8470Sstevel@tonic-gate 			safecf = false;
8480Sstevel@tonic-gate 			break;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 		  case 'D':
8510Sstevel@tonic-gate 		  case 'd':	/* debugging */
8520Sstevel@tonic-gate 			/* already done */
8530Sstevel@tonic-gate 			break;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 		  case 'f':	/* from address */
8560Sstevel@tonic-gate 		  case 'r':	/* obsolete -f flag */
8570Sstevel@tonic-gate 			CHECK_AGAINST_OPMODE(j);
8580Sstevel@tonic-gate 			if (from != NULL)
8590Sstevel@tonic-gate 			{
8600Sstevel@tonic-gate 				usrerr("More than one \"from\" person");
8610Sstevel@tonic-gate 				ExitStat = EX_USAGE;
8620Sstevel@tonic-gate 				break;
8630Sstevel@tonic-gate 			}
8640Sstevel@tonic-gate 			if (optarg[0] == '\0')
8650Sstevel@tonic-gate 				from = newstr("<>");
8660Sstevel@tonic-gate 			else
8670Sstevel@tonic-gate 				from = newstr(denlstring(optarg, true, true));
8680Sstevel@tonic-gate 			if (strcmp(RealUserName, from) != 0)
8690Sstevel@tonic-gate 				warn_f_flag = j;
8700Sstevel@tonic-gate 			break;
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		  case 'F':	/* set full name */
8730Sstevel@tonic-gate 			CHECK_AGAINST_OPMODE(j);
8740Sstevel@tonic-gate 			FullName = newstr(optarg);
8750Sstevel@tonic-gate 			break;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 		  case 'G':	/* relay (gateway) submission */
8780Sstevel@tonic-gate 			/* already set */
8790Sstevel@tonic-gate 			CHECK_AGAINST_OPMODE(j);
8800Sstevel@tonic-gate 			break;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 		  case 'h':	/* hop count */
8830Sstevel@tonic-gate 			CHECK_AGAINST_OPMODE(j);
8840Sstevel@tonic-gate 			BlankEnvelope.e_hopcount = (short) strtol(optarg, &ep,
8850Sstevel@tonic-gate 								  10);
8863544Sjbeck 			(void) sm_snprintf(buf, sizeof(buf), "%d",
8870Sstevel@tonic-gate 					   BlankEnvelope.e_hopcount);
8880Sstevel@tonic-gate 			macdefine(&BlankEnvelope.e_macro, A_TEMP, 'c', buf);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 			if (*ep)
8910Sstevel@tonic-gate 			{
8920Sstevel@tonic-gate 				usrerr("Bad hop count (%s)", optarg);
8930Sstevel@tonic-gate 				ExitStat = EX_USAGE;
8940Sstevel@tonic-gate 			}
8950Sstevel@tonic-gate 			break;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 		  case 'L':	/* program label */
8980Sstevel@tonic-gate 			/* already set */
8990Sstevel@tonic-gate 			break;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 		  case 'n':	/* don't alias */
9020Sstevel@tonic-gate 			CHECK_AGAINST_OPMODE(j);
9030Sstevel@tonic-gate 			NoAlias = true;
9040Sstevel@tonic-gate 			break;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 		  case 'N':	/* delivery status notifications */
9070Sstevel@tonic-gate 			CHECK_AGAINST_OPMODE(j);
9080Sstevel@tonic-gate 			DefaultNotify |= QHASNOTIFY;
9090Sstevel@tonic-gate 			macdefine(&BlankEnvelope.e_macro, A_TEMP,
9100Sstevel@tonic-gate 				macid("{dsn_notify}"), optarg);
9110Sstevel@tonic-gate 			if (sm_strcasecmp(optarg, "never") == 0)
9120Sstevel@tonic-gate 				break;
9130Sstevel@tonic-gate 			for (p = optarg; p != NULL; optarg = p)
9140Sstevel@tonic-gate 			{
9150Sstevel@tonic-gate 				p = strchr(p, ',');
9160Sstevel@tonic-gate 				if (p != NULL)
9170Sstevel@tonic-gate 					*p++ = '\0';
9180Sstevel@tonic-gate 				if (sm_strcasecmp(optarg, "success") == 0)
9190Sstevel@tonic-gate 					DefaultNotify |= QPINGONSUCCESS;
9200Sstevel@tonic-gate 				else if (sm_strcasecmp(optarg, "failure") == 0)
9210Sstevel@tonic-gate 					DefaultNotify |= QPINGONFAILURE;
9220Sstevel@tonic-gate 				else if (sm_strcasecmp(optarg, "delay") == 0)
9230Sstevel@tonic-gate 					DefaultNotify |= QPINGONDELAY;
9240Sstevel@tonic-gate 				else
9250Sstevel@tonic-gate 				{
9260Sstevel@tonic-gate 					usrerr("Invalid -N argument");
9270Sstevel@tonic-gate 					ExitStat = EX_USAGE;
9280Sstevel@tonic-gate 				}
9290Sstevel@tonic-gate 			}
9300Sstevel@tonic-gate 			break;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		  case 'o':	/* set option */
9330Sstevel@tonic-gate 			setoption(*optarg, optarg + 1, false, true,
9340Sstevel@tonic-gate 				  &BlankEnvelope);
9350Sstevel@tonic-gate 			break;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 		  case 'O':	/* set option (long form) */
9380Sstevel@tonic-gate 			setoption(' ', optarg, false, true, &BlankEnvelope);
9390Sstevel@tonic-gate 			break;
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 		  case 'p':	/* set protocol */
9420Sstevel@tonic-gate 			CHECK_AGAINST_OPMODE(j);
9430Sstevel@tonic-gate 			p = strchr(optarg, ':');
9440Sstevel@tonic-gate 			if (p != NULL)
9450Sstevel@tonic-gate 			{
9460Sstevel@tonic-gate 				*p++ = '\0';
9470Sstevel@tonic-gate 				if (*p != '\0')
9480Sstevel@tonic-gate 				{
9490Sstevel@tonic-gate 					i = strlen(p) + 1;
9500Sstevel@tonic-gate 					ep = sm_malloc_x(i);
9510Sstevel@tonic-gate 					cleanstrcpy(ep, p, i);
9520Sstevel@tonic-gate 					macdefine(&BlankEnvelope.e_macro,
9530Sstevel@tonic-gate 						  A_HEAP, 's', ep);
9540Sstevel@tonic-gate 				}
9550Sstevel@tonic-gate 			}
9560Sstevel@tonic-gate 			if (*optarg != '\0')
9570Sstevel@tonic-gate 			{
9580Sstevel@tonic-gate 				i = strlen(optarg) + 1;
9590Sstevel@tonic-gate 				ep = sm_malloc_x(i);
9600Sstevel@tonic-gate 				cleanstrcpy(ep, optarg, i);
9610Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_HEAP,
9620Sstevel@tonic-gate 					  'r', ep);
9630Sstevel@tonic-gate 			}
9640Sstevel@tonic-gate 			break;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 		  case 'Q':	/* change quarantining on queued items */
9670Sstevel@tonic-gate 			/* sanity check */
9680Sstevel@tonic-gate 			if (OpMode != MD_DELIVER &&
9690Sstevel@tonic-gate 			    OpMode != MD_QUEUERUN)
9700Sstevel@tonic-gate 			{
9710Sstevel@tonic-gate 				usrerr("Can not use -Q with -b%c", OpMode);
9720Sstevel@tonic-gate 				ExitStat = EX_USAGE;
9730Sstevel@tonic-gate 				break;
9740Sstevel@tonic-gate 			}
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 			if (OpMode == MD_DELIVER)
9770Sstevel@tonic-gate 				set_op_mode(MD_QUEUERUN);
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 			FullName = NULL;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 			quarantining = newstr(optarg);
9820Sstevel@tonic-gate 			break;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 		  case 'q':	/* run queue files at intervals */
9850Sstevel@tonic-gate 			/* sanity check */
9860Sstevel@tonic-gate 			if (OpMode != MD_DELIVER &&
9870Sstevel@tonic-gate 			    OpMode != MD_DAEMON &&
9880Sstevel@tonic-gate 			    OpMode != MD_FGDAEMON &&
9890Sstevel@tonic-gate 			    OpMode != MD_PRINT &&
9900Sstevel@tonic-gate 			    OpMode != MD_PRINTNQE &&
9910Sstevel@tonic-gate 			    OpMode != MD_QUEUERUN)
9920Sstevel@tonic-gate 			{
9930Sstevel@tonic-gate 				usrerr("Can not use -q with -b%c", OpMode);
9940Sstevel@tonic-gate 				ExitStat = EX_USAGE;
9950Sstevel@tonic-gate 				break;
9960Sstevel@tonic-gate 			}
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 			/* don't override -bd, -bD or -bp */
9990Sstevel@tonic-gate 			if (OpMode == MD_DELIVER)
10000Sstevel@tonic-gate 				set_op_mode(MD_QUEUERUN);
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 			FullName = NULL;
10030Sstevel@tonic-gate 			negate = optarg[0] == '!';
10040Sstevel@tonic-gate 			if (negate)
10050Sstevel@tonic-gate 			{
10060Sstevel@tonic-gate 				/* negate meaning of pattern match */
10070Sstevel@tonic-gate 				optarg++; /* skip '!' for next switch */
10080Sstevel@tonic-gate 			}
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 			switch (optarg[0])
10110Sstevel@tonic-gate 			{
10120Sstevel@tonic-gate 			  case 'G': /* Limit by queue group name */
10130Sstevel@tonic-gate 				if (negate)
10140Sstevel@tonic-gate 				{
10150Sstevel@tonic-gate 					usrerr("Can not use -q!G");
10160Sstevel@tonic-gate 					ExitStat = EX_USAGE;
10170Sstevel@tonic-gate 					break;
10180Sstevel@tonic-gate 				}
10190Sstevel@tonic-gate 				if (queuegroup != NULL)
10200Sstevel@tonic-gate 				{
10210Sstevel@tonic-gate 					usrerr("Can not use multiple -qG options");
10220Sstevel@tonic-gate 					ExitStat = EX_USAGE;
10230Sstevel@tonic-gate 					break;
10240Sstevel@tonic-gate 				}
10250Sstevel@tonic-gate 				queuegroup = newstr(&optarg[1]);
10260Sstevel@tonic-gate 				break;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 			  case 'I': /* Limit by ID */
10293544Sjbeck 				new = (QUEUE_CHAR *) xalloc(sizeof(*new));
10300Sstevel@tonic-gate 				new->queue_match = newstr(&optarg[1]);
10310Sstevel@tonic-gate 				new->queue_negate = negate;
10320Sstevel@tonic-gate 				new->queue_next = QueueLimitId;
10330Sstevel@tonic-gate 				QueueLimitId = new;
10340Sstevel@tonic-gate 				break;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 			  case 'R': /* Limit by recipient */
10373544Sjbeck 				new = (QUEUE_CHAR *) xalloc(sizeof(*new));
10380Sstevel@tonic-gate 				new->queue_match = newstr(&optarg[1]);
10390Sstevel@tonic-gate 				new->queue_negate = negate;
10400Sstevel@tonic-gate 				new->queue_next = QueueLimitRecipient;
10410Sstevel@tonic-gate 				QueueLimitRecipient = new;
10420Sstevel@tonic-gate 				break;
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 			  case 'S': /* Limit by sender */
10453544Sjbeck 				new = (QUEUE_CHAR *) xalloc(sizeof(*new));
10460Sstevel@tonic-gate 				new->queue_match = newstr(&optarg[1]);
10470Sstevel@tonic-gate 				new->queue_negate = negate;
10480Sstevel@tonic-gate 				new->queue_next = QueueLimitSender;
10490Sstevel@tonic-gate 				QueueLimitSender = new;
10500Sstevel@tonic-gate 				break;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 			  case 'f': /* foreground queue run */
10530Sstevel@tonic-gate 				foregroundqueue  = true;
10540Sstevel@tonic-gate 				break;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 			  case 'Q': /* Limit by quarantine message */
10570Sstevel@tonic-gate 				if (optarg[1] != '\0')
10580Sstevel@tonic-gate 				{
10593544Sjbeck 					new = (QUEUE_CHAR *) xalloc(sizeof(*new));
10600Sstevel@tonic-gate 					new->queue_match = newstr(&optarg[1]);
10610Sstevel@tonic-gate 					new->queue_negate = negate;
10620Sstevel@tonic-gate 					new->queue_next = QueueLimitQuarantine;
10630Sstevel@tonic-gate 					QueueLimitQuarantine = new;
10640Sstevel@tonic-gate 				}
10650Sstevel@tonic-gate 				QueueMode = QM_QUARANTINE;
10660Sstevel@tonic-gate 				break;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 			  case 'L': /* act on lost items */
10690Sstevel@tonic-gate 				QueueMode = QM_LOST;
10700Sstevel@tonic-gate 				break;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 			  case 'p': /* Persistent queue */
10730Sstevel@tonic-gate 				queuepersistent = true;
10740Sstevel@tonic-gate 				if (QueueIntvl == 0)
10750Sstevel@tonic-gate 					QueueIntvl = 1;
10760Sstevel@tonic-gate 				if (optarg[1] == '\0')
10770Sstevel@tonic-gate 					break;
10780Sstevel@tonic-gate 				++optarg;
10790Sstevel@tonic-gate 				/* FALLTHROUGH */
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 			  default:
10820Sstevel@tonic-gate 				i = Errors;
10830Sstevel@tonic-gate 				QueueIntvl = convtime(optarg, 'm');
10840Sstevel@tonic-gate 				if (QueueIntvl < 0)
10850Sstevel@tonic-gate 				{
10860Sstevel@tonic-gate 					usrerr("Invalid -q value");
10870Sstevel@tonic-gate 					ExitStat = EX_USAGE;
10880Sstevel@tonic-gate 				}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 				/* check for bad conversion */
10910Sstevel@tonic-gate 				if (i < Errors)
10920Sstevel@tonic-gate 					ExitStat = EX_USAGE;
10930Sstevel@tonic-gate 				break;
10940Sstevel@tonic-gate 			}
10950Sstevel@tonic-gate 			break;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 		  case 'R':	/* DSN RET: what to return */
10980Sstevel@tonic-gate 			CHECK_AGAINST_OPMODE(j);
10990Sstevel@tonic-gate 			if (bitset(EF_RET_PARAM, BlankEnvelope.e_flags))
11000Sstevel@tonic-gate 			{
11010Sstevel@tonic-gate 				usrerr("Duplicate -R flag");
11020Sstevel@tonic-gate 				ExitStat = EX_USAGE;
11030Sstevel@tonic-gate 				break;
11040Sstevel@tonic-gate 			}
11050Sstevel@tonic-gate 			BlankEnvelope.e_flags |= EF_RET_PARAM;
11060Sstevel@tonic-gate 			if (sm_strcasecmp(optarg, "hdrs") == 0)
11070Sstevel@tonic-gate 				BlankEnvelope.e_flags |= EF_NO_BODY_RETN;
11080Sstevel@tonic-gate 			else if (sm_strcasecmp(optarg, "full") != 0)
11090Sstevel@tonic-gate 			{
11100Sstevel@tonic-gate 				usrerr("Invalid -R value");
11110Sstevel@tonic-gate 				ExitStat = EX_USAGE;
11120Sstevel@tonic-gate 			}
11130Sstevel@tonic-gate 			macdefine(&BlankEnvelope.e_macro, A_TEMP,
11140Sstevel@tonic-gate 				  macid("{dsn_ret}"), optarg);
11150Sstevel@tonic-gate 			break;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 		  case 't':	/* read recipients from message */
11180Sstevel@tonic-gate 			CHECK_AGAINST_OPMODE(j);
11190Sstevel@tonic-gate 			GrabTo = true;
11200Sstevel@tonic-gate 			break;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 		  case 'V':	/* DSN ENVID: set "original" envelope id */
11230Sstevel@tonic-gate 			CHECK_AGAINST_OPMODE(j);
11240Sstevel@tonic-gate 			if (!xtextok(optarg))
11250Sstevel@tonic-gate 			{
11260Sstevel@tonic-gate 				usrerr("Invalid syntax in -V flag");
11270Sstevel@tonic-gate 				ExitStat = EX_USAGE;
11280Sstevel@tonic-gate 			}
11290Sstevel@tonic-gate 			else
11300Sstevel@tonic-gate 			{
11310Sstevel@tonic-gate 				BlankEnvelope.e_envid = newstr(optarg);
11320Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_TEMP,
11330Sstevel@tonic-gate 					  macid("{dsn_envid}"), optarg);
11340Sstevel@tonic-gate 			}
11350Sstevel@tonic-gate 			break;
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 		  case 'X':	/* traffic log file */
11380Sstevel@tonic-gate 			dp = drop_privileges(true);
11390Sstevel@tonic-gate 			setstat(dp);
11400Sstevel@tonic-gate 			if (stat(optarg, &traf_st) == 0 &&
11410Sstevel@tonic-gate 			    S_ISFIFO(traf_st.st_mode))
11420Sstevel@tonic-gate 				TrafficLogFile = sm_io_open(SmFtStdio,
11430Sstevel@tonic-gate 							    SM_TIME_DEFAULT,
11440Sstevel@tonic-gate 							    optarg,
11450Sstevel@tonic-gate 							    SM_IO_WRONLY, NULL);
11460Sstevel@tonic-gate 			else
11470Sstevel@tonic-gate 				TrafficLogFile = sm_io_open(SmFtStdio,
11480Sstevel@tonic-gate 							    SM_TIME_DEFAULT,
11490Sstevel@tonic-gate 							    optarg,
11500Sstevel@tonic-gate 							    SM_IO_APPEND, NULL);
11510Sstevel@tonic-gate 			if (TrafficLogFile == NULL)
11520Sstevel@tonic-gate 			{
11530Sstevel@tonic-gate 				syserr("cannot open %s", optarg);
11540Sstevel@tonic-gate 				ExitStat = EX_CANTCREAT;
11550Sstevel@tonic-gate 				break;
11560Sstevel@tonic-gate 			}
11570Sstevel@tonic-gate 			(void) sm_io_setvbuf(TrafficLogFile, SM_TIME_DEFAULT,
11580Sstevel@tonic-gate 					     NULL, SM_IO_LBF, 0);
11590Sstevel@tonic-gate 			break;
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 			/* compatibility flags */
11620Sstevel@tonic-gate 		  case 'c':	/* connect to non-local mailers */
11630Sstevel@tonic-gate 		  case 'i':	/* don't let dot stop me */
11640Sstevel@tonic-gate 		  case 'm':	/* send to me too */
11650Sstevel@tonic-gate 		  case 'T':	/* set timeout interval */
11660Sstevel@tonic-gate 		  case 'v':	/* give blow-by-blow description */
11670Sstevel@tonic-gate 			setoption(j, "T", false, true, &BlankEnvelope);
11680Sstevel@tonic-gate 			break;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 		  case 'e':	/* error message disposition */
11710Sstevel@tonic-gate 		  case 'M':	/* define macro */
11720Sstevel@tonic-gate 			setoption(j, optarg, false, true, &BlankEnvelope);
11730Sstevel@tonic-gate 			break;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 		  case 's':	/* save From lines in headers */
11760Sstevel@tonic-gate 			setoption('f', "T", false, true, &BlankEnvelope);
11770Sstevel@tonic-gate 			break;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate #ifdef DBM
11800Sstevel@tonic-gate 		  case 'I':	/* initialize alias DBM file */
11810Sstevel@tonic-gate 			set_op_mode(MD_INITALIAS);
11820Sstevel@tonic-gate 			break;
11830Sstevel@tonic-gate #endif /* DBM */
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate #if defined(__osf__) || defined(_AIX3)
11860Sstevel@tonic-gate 		  case 'x':	/* random flag that OSF/1 & AIX mailx passes */
11870Sstevel@tonic-gate 			break;
11880Sstevel@tonic-gate #endif /* defined(__osf__) || defined(_AIX3) */
11890Sstevel@tonic-gate #if defined(sony_news)
11900Sstevel@tonic-gate 		  case 'E':
11910Sstevel@tonic-gate 		  case 'J':	/* ignore flags for Japanese code conversion
11920Sstevel@tonic-gate 				   implemented on Sony NEWS */
11930Sstevel@tonic-gate 			break;
11940Sstevel@tonic-gate #endif /* defined(sony_news) */
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 		  default:
11970Sstevel@tonic-gate 			finis(true, true, EX_USAGE);
11980Sstevel@tonic-gate 			/* NOTREACHED */
11990Sstevel@tonic-gate 			break;
12000Sstevel@tonic-gate 		}
12010Sstevel@tonic-gate 	}
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	/* if we've had errors so far, exit now */
1204*11440SJohn.Beck@Sun.COM 	if ((ExitStat != EX_OK && OpMode != MD_TEST && OpMode != MD_CHECKCONFIG) ||
12050Sstevel@tonic-gate 	    ExitStat == EX_OSERR)
12060Sstevel@tonic-gate 	{
12070Sstevel@tonic-gate 		finis(false, true, ExitStat);
12080Sstevel@tonic-gate 		/* NOTREACHED */
12090Sstevel@tonic-gate 	}
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	if (bitset(SUBMIT_MTA, SubmitMode))
12120Sstevel@tonic-gate 	{
12130Sstevel@tonic-gate 		/* If set daemon_flags on command line, don't reset it */
12140Sstevel@tonic-gate 		if (macvalue(macid("{daemon_flags}"), &BlankEnvelope) == NULL)
12150Sstevel@tonic-gate 			macdefine(&BlankEnvelope.e_macro, A_PERM,
12160Sstevel@tonic-gate 				  macid("{daemon_flags}"), "CC f");
12170Sstevel@tonic-gate 	}
12180Sstevel@tonic-gate 	else if (OpMode == MD_DELIVER || OpMode == MD_SMTP)
12190Sstevel@tonic-gate 	{
12200Sstevel@tonic-gate 		SubmitMode = SUBMIT_MSA;
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 		/* If set daemon_flags on command line, don't reset it */
12230Sstevel@tonic-gate 		if (macvalue(macid("{daemon_flags}"), &BlankEnvelope) == NULL)
12240Sstevel@tonic-gate 			macdefine(&BlankEnvelope.e_macro, A_PERM,
12250Sstevel@tonic-gate 				  macid("{daemon_flags}"), "c u");
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	/*
12290Sstevel@tonic-gate 	**  Do basic initialization.
12300Sstevel@tonic-gate 	**	Read system control file.
12310Sstevel@tonic-gate 	**	Extract special fields for local use.
12320Sstevel@tonic-gate 	*/
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate #if XDEBUG
12350Sstevel@tonic-gate 	checkfd012("before readcf");
12360Sstevel@tonic-gate #endif /* XDEBUG */
12370Sstevel@tonic-gate 	vendor_pre_defaults(&BlankEnvelope);
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	readcf(getcfname(OpMode, SubmitMode, cftype, conffile),
12400Sstevel@tonic-gate 			 safecf, &BlankEnvelope);
12410Sstevel@tonic-gate #if !defined(_USE_SUN_NSSWITCH_) && !defined(_USE_DEC_SVC_CONF_)
12420Sstevel@tonic-gate 	ConfigFileRead = true;
12430Sstevel@tonic-gate #endif /* !defined(_USE_SUN_NSSWITCH_) && !defined(_USE_DEC_SVC_CONF_) */
12440Sstevel@tonic-gate 	vendor_post_defaults(&BlankEnvelope);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	/* now we can complain about missing fds */
12470Sstevel@tonic-gate 	if (MissingFds != 0 && LogLevel > 8)
12480Sstevel@tonic-gate 	{
12490Sstevel@tonic-gate 		char mbuf[MAXLINE];
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 		mbuf[0] = '\0';
12520Sstevel@tonic-gate 		if (bitset(1 << STDIN_FILENO, MissingFds))
12533544Sjbeck 			(void) sm_strlcat(mbuf, ", stdin", sizeof(mbuf));
12540Sstevel@tonic-gate 		if (bitset(1 << STDOUT_FILENO, MissingFds))
12553544Sjbeck 			(void) sm_strlcat(mbuf, ", stdout", sizeof(mbuf));
12560Sstevel@tonic-gate 		if (bitset(1 << STDERR_FILENO, MissingFds))
12573544Sjbeck 			(void) sm_strlcat(mbuf, ", stderr", sizeof(mbuf));
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 		/* Notice: fill_errno is from high above: fill_fd() */
12600Sstevel@tonic-gate 		sm_syslog(LOG_WARNING, NOQID,
12610Sstevel@tonic-gate 			  "File descriptors missing on startup: %s; %s",
12620Sstevel@tonic-gate 			  &mbuf[2], sm_errstring(fill_errno));
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	/* Remove the ability for a normal user to send signals */
12660Sstevel@tonic-gate 	if (RealUid != 0 && RealUid != geteuid())
12670Sstevel@tonic-gate 	{
12680Sstevel@tonic-gate 		uid_t new_uid = geteuid();
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate #if HASSETREUID
12710Sstevel@tonic-gate 		/*
12720Sstevel@tonic-gate 		**  Since we can differentiate between uid and euid,
12730Sstevel@tonic-gate 		**  make the uid a different user so the real user
12740Sstevel@tonic-gate 		**  can't send signals.  However, it doesn't need to be
12750Sstevel@tonic-gate 		**  root (euid has root).
12760Sstevel@tonic-gate 		*/
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 		if (new_uid == 0)
12790Sstevel@tonic-gate 			new_uid = DefUid;
12800Sstevel@tonic-gate 		if (tTd(47, 5))
12810Sstevel@tonic-gate 			sm_dprintf("Changing real uid to %d\n", (int) new_uid);
12820Sstevel@tonic-gate 		if (setreuid(new_uid, geteuid()) < 0)
12830Sstevel@tonic-gate 		{
12840Sstevel@tonic-gate 			syserr("main: setreuid(%d, %d) failed",
12850Sstevel@tonic-gate 			       (int) new_uid, (int) geteuid());
12860Sstevel@tonic-gate 			finis(false, true, EX_OSERR);
12870Sstevel@tonic-gate 			/* NOTREACHED */
12880Sstevel@tonic-gate 		}
12890Sstevel@tonic-gate 		if (tTd(47, 10))
12900Sstevel@tonic-gate 			sm_dprintf("Now running as e/ruid %d:%d\n",
12910Sstevel@tonic-gate 				   (int) geteuid(), (int) getuid());
12920Sstevel@tonic-gate #else /* HASSETREUID */
12930Sstevel@tonic-gate 		/*
12940Sstevel@tonic-gate 		**  Have to change both effective and real so need to
12950Sstevel@tonic-gate 		**  change them both to effective to keep privs.
12960Sstevel@tonic-gate 		*/
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 		if (tTd(47, 5))
12990Sstevel@tonic-gate 			sm_dprintf("Changing uid to %d\n", (int) new_uid);
13000Sstevel@tonic-gate 		if (setuid(new_uid) < 0)
13010Sstevel@tonic-gate 		{
13020Sstevel@tonic-gate 			syserr("main: setuid(%d) failed", (int) new_uid);
13030Sstevel@tonic-gate 			finis(false, true, EX_OSERR);
13040Sstevel@tonic-gate 			/* NOTREACHED */
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 		if (tTd(47, 10))
13070Sstevel@tonic-gate 			sm_dprintf("Now running as e/ruid %d:%d\n",
13080Sstevel@tonic-gate 				   (int) geteuid(), (int) getuid());
13090Sstevel@tonic-gate #endif /* HASSETREUID */
13100Sstevel@tonic-gate 	}
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate #if NAMED_BIND
13130Sstevel@tonic-gate 	if (FallbackMX != NULL)
13140Sstevel@tonic-gate 		(void) getfallbackmxrr(FallbackMX);
13150Sstevel@tonic-gate #endif /* NAMED_BIND */
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	if (SuperSafe == SAFE_INTERACTIVE && CurEnv->e_sendmode != SM_DELIVER)
13180Sstevel@tonic-gate 	{
13190Sstevel@tonic-gate 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
13200Sstevel@tonic-gate 				     "WARNING: SuperSafe=interactive should only be used with\n         DeliveryMode=interactive\n");
13210Sstevel@tonic-gate 	}
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	if (UseMSP && (OpMode == MD_DAEMON || OpMode == MD_FGDAEMON))
13240Sstevel@tonic-gate 	{
13250Sstevel@tonic-gate 		usrerr("Mail submission program cannot be used as daemon");
13260Sstevel@tonic-gate 		finis(false, true, EX_USAGE);
13270Sstevel@tonic-gate 	}
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	if (OpMode == MD_DELIVER || OpMode == MD_SMTP ||
13300Sstevel@tonic-gate 	    OpMode == MD_QUEUERUN || OpMode == MD_ARPAFTP ||
13310Sstevel@tonic-gate 	    OpMode == MD_DAEMON || OpMode == MD_FGDAEMON)
13320Sstevel@tonic-gate 		makeworkgroups();
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	/* set up the basic signal handlers */
13350Sstevel@tonic-gate 	if (sm_signal(SIGINT, SIG_IGN) != SIG_IGN)
13360Sstevel@tonic-gate 		(void) sm_signal(SIGINT, intsig);
13370Sstevel@tonic-gate 	(void) sm_signal(SIGTERM, intsig);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	/* Enforce use of local time (null string overrides this) */
13400Sstevel@tonic-gate 	if (TimeZoneSpec == NULL)
13410Sstevel@tonic-gate 		unsetenv("TZ");
13420Sstevel@tonic-gate 	else if (TimeZoneSpec[0] != '\0')
13431658Sjbeck 		sm_setuserenv("TZ", TimeZoneSpec);
13440Sstevel@tonic-gate 	else
13451658Sjbeck 		sm_setuserenv("TZ", NULL);
13460Sstevel@tonic-gate 	tzset();
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	/* initialize mailbox database */
13490Sstevel@tonic-gate 	i = sm_mbdb_initialize(Mbdb);
13500Sstevel@tonic-gate 	if (i != EX_OK)
13510Sstevel@tonic-gate 	{
13520Sstevel@tonic-gate 		usrerr("Can't initialize mailbox database \"%s\": %s",
13530Sstevel@tonic-gate 		       Mbdb, sm_strexit(i));
13540Sstevel@tonic-gate 		ExitStat = i;
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	/* avoid denial-of-service attacks */
13580Sstevel@tonic-gate 	resetlimits();
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	if (OpMode == MD_TEST)
13610Sstevel@tonic-gate 	{
13620Sstevel@tonic-gate 		/* can't be done after readcf if RunAs* is used */
13630Sstevel@tonic-gate 		dp = drop_privileges(true);
13640Sstevel@tonic-gate 		if (dp != EX_OK)
13650Sstevel@tonic-gate 		{
13660Sstevel@tonic-gate 			finis(false, true, dp);
13670Sstevel@tonic-gate 			/* NOTREACHED */
13680Sstevel@tonic-gate 		}
13690Sstevel@tonic-gate 	}
13700Sstevel@tonic-gate 	else if (OpMode != MD_DAEMON && OpMode != MD_FGDAEMON)
13710Sstevel@tonic-gate 	{
13720Sstevel@tonic-gate 		/* drop privileges -- daemon mode done after socket/bind */
13730Sstevel@tonic-gate 		dp = drop_privileges(false);
13740Sstevel@tonic-gate 		setstat(dp);
13750Sstevel@tonic-gate 		if (dp == EX_OK && UseMSP && (geteuid() == 0 || getuid() == 0))
13760Sstevel@tonic-gate 		{
13770Sstevel@tonic-gate 			usrerr("Mail submission program must have RunAsUser set to non root user");
13780Sstevel@tonic-gate 			finis(false, true, EX_CONFIG);
13790Sstevel@tonic-gate 			/* NOTREACHED */
13800Sstevel@tonic-gate 		}
13810Sstevel@tonic-gate 	}
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate #if NAMED_BIND
13840Sstevel@tonic-gate 	_res.retry = TimeOuts.res_retry[RES_TO_DEFAULT];
13850Sstevel@tonic-gate 	_res.retrans = TimeOuts.res_retrans[RES_TO_DEFAULT];
13860Sstevel@tonic-gate #endif /* NAMED_BIND */
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	/*
13890Sstevel@tonic-gate 	**  Find our real host name for future logging.
13900Sstevel@tonic-gate 	*/
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	authinfo = getauthinfo(STDIN_FILENO, &forged);
13930Sstevel@tonic-gate 	macdefine(&BlankEnvelope.e_macro, A_TEMP, '_', authinfo);
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	/* suppress error printing if errors mailed back or whatever */
13960Sstevel@tonic-gate 	if (BlankEnvelope.e_errormode != EM_PRINT)
13970Sstevel@tonic-gate 		HoldErrs = true;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	/* set up the $=m class now, after .cf has a chance to redefine $m */
14003544Sjbeck 	expand("\201m", jbuf, sizeof(jbuf), &BlankEnvelope);
14010Sstevel@tonic-gate 	if (jbuf[0] != '\0')
14020Sstevel@tonic-gate 		setclass('m', jbuf);
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	/* probe interfaces and locate any additional names */
14050Sstevel@tonic-gate 	if (DontProbeInterfaces != DPI_PROBENONE)
14060Sstevel@tonic-gate 		load_if_names();
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	if (tTd(0, 10))
14090Sstevel@tonic-gate 	{
14100Sstevel@tonic-gate 		char pidpath[MAXPATHLEN];
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 		/* Now we know which .cf file we use */
14130Sstevel@tonic-gate 		sm_dprintf("     Conf file:\t%s (selected)\n",
14140Sstevel@tonic-gate 			   getcfname(OpMode, SubmitMode, cftype, conffile));
14153544Sjbeck 		expand(PidFile, pidpath, sizeof(pidpath), &BlankEnvelope);
14160Sstevel@tonic-gate 		sm_dprintf("      Pid file:\t%s (selected)\n", pidpath);
14170Sstevel@tonic-gate 	}
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	if (tTd(0, 1))
14200Sstevel@tonic-gate 	{
14210Sstevel@tonic-gate 		sm_dprintf("\n============ SYSTEM IDENTITY (after readcf) ============");
14220Sstevel@tonic-gate 		sm_dprintf("\n      (short domain name) $w = ");
14230Sstevel@tonic-gate 		xputs(sm_debug_file(), macvalue('w', &BlankEnvelope));
14240Sstevel@tonic-gate 		sm_dprintf("\n  (canonical domain name) $j = ");
14250Sstevel@tonic-gate 		xputs(sm_debug_file(), macvalue('j', &BlankEnvelope));
14260Sstevel@tonic-gate 		sm_dprintf("\n         (subdomain name) $m = ");
14270Sstevel@tonic-gate 		xputs(sm_debug_file(), macvalue('m', &BlankEnvelope));
14280Sstevel@tonic-gate 		sm_dprintf("\n              (node name) $k = ");
14290Sstevel@tonic-gate 		xputs(sm_debug_file(), macvalue('k', &BlankEnvelope));
14300Sstevel@tonic-gate 		sm_dprintf("\n========================================================\n\n");
14310Sstevel@tonic-gate 	}
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	/*
14340Sstevel@tonic-gate 	**  Do more command line checking -- these are things that
14350Sstevel@tonic-gate 	**  have to modify the results of reading the config file.
14360Sstevel@tonic-gate 	*/
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	/* process authorization warnings from command line */
14390Sstevel@tonic-gate 	if (warn_C_flag)
14400Sstevel@tonic-gate 		auth_warning(&BlankEnvelope, "Processed by %s with -C %s",
14410Sstevel@tonic-gate 			     RealUserName, conffile);
14420Sstevel@tonic-gate 	if (Warn_Q_option && !wordinclass(RealUserName, 't'))
14430Sstevel@tonic-gate 		auth_warning(&BlankEnvelope, "Processed from queue %s",
14440Sstevel@tonic-gate 			     QueueDir);
14450Sstevel@tonic-gate 	if (sysloglabel != NULL && !wordinclass(RealUserName, 't') &&
14460Sstevel@tonic-gate 	    RealUid != 0 && RealUid != TrustedUid && LogLevel > 1)
14470Sstevel@tonic-gate 		sm_syslog(LOG_WARNING, NOQID, "user %d changed syslog label",
14480Sstevel@tonic-gate 			  (int) RealUid);
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 	/* check body type for legality */
14510Sstevel@tonic-gate 	i = check_bodytype(BlankEnvelope.e_bodytype);
14520Sstevel@tonic-gate 	if (i == BODYTYPE_ILLEGAL)
14530Sstevel@tonic-gate 	{
14540Sstevel@tonic-gate 		usrerr("Illegal body type %s", BlankEnvelope.e_bodytype);
14550Sstevel@tonic-gate 		BlankEnvelope.e_bodytype = NULL;
14560Sstevel@tonic-gate 	}
14570Sstevel@tonic-gate 	else if (i != BODYTYPE_NONE)
14580Sstevel@tonic-gate 		SevenBitInput = (i == BODYTYPE_7BIT);
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	/* tweak default DSN notifications */
14610Sstevel@tonic-gate 	if (DefaultNotify == 0)
14620Sstevel@tonic-gate 		DefaultNotify = QPINGONFAILURE|QPINGONDELAY;
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	/* check for sane configuration level */
14650Sstevel@tonic-gate 	if (ConfigLevel > MAXCONFIGLEVEL)
14660Sstevel@tonic-gate 	{
14670Sstevel@tonic-gate 		syserr("Warning: .cf version level (%d) exceeds sendmail version %s functionality (%d)",
14680Sstevel@tonic-gate 		       ConfigLevel, Version, MAXCONFIGLEVEL);
14690Sstevel@tonic-gate 	}
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	/* need MCI cache to have persistence */
14720Sstevel@tonic-gate 	if (HostStatDir != NULL && MaxMciCache == 0)
14730Sstevel@tonic-gate 	{
14740Sstevel@tonic-gate 		HostStatDir = NULL;
14750Sstevel@tonic-gate 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
14760Sstevel@tonic-gate 				     "Warning: HostStatusDirectory disabled with ConnectionCacheSize = 0\n");
14770Sstevel@tonic-gate 	}
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	/* need HostStatusDir in order to have SingleThreadDelivery */
14800Sstevel@tonic-gate 	if (SingleThreadDelivery && HostStatDir == NULL)
14810Sstevel@tonic-gate 	{
14820Sstevel@tonic-gate 		SingleThreadDelivery = false;
14830Sstevel@tonic-gate 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
14840Sstevel@tonic-gate 				     "Warning: HostStatusDirectory required for SingleThreadDelivery\n");
14850Sstevel@tonic-gate 	}
14860Sstevel@tonic-gate 
14871658Sjbeck #if _FFR_MEMSTAT
14881658Sjbeck 	j = sm_memstat_open();
14891658Sjbeck 	if (j < 0 && (RefuseLowMem > 0 || QueueLowMem > 0) && LogLevel > 4)
14901658Sjbeck 	{
14911658Sjbeck 		sm_syslog(LOG_WARNING, NOQID,
14921658Sjbeck 			  "cannot get memory statistics, settings ignored, error=%d"
14931658Sjbeck 			  , j);
14941658Sjbeck 	}
14951658Sjbeck #endif /* _FFR_MEMSTAT */
14961658Sjbeck 
14970Sstevel@tonic-gate 	/* check for permissions */
14980Sstevel@tonic-gate 	if (RealUid != 0 &&
14990Sstevel@tonic-gate 	    RealUid != TrustedUid)
15000Sstevel@tonic-gate 	{
15010Sstevel@tonic-gate 		char *action = NULL;
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 		switch (OpMode)
15040Sstevel@tonic-gate 		{
15050Sstevel@tonic-gate 		  case MD_QUEUERUN:
15060Sstevel@tonic-gate 			if (quarantining != NULL)
15070Sstevel@tonic-gate 				action = "quarantine jobs";
15080Sstevel@tonic-gate 			else
15090Sstevel@tonic-gate 			{
15100Sstevel@tonic-gate 				/* Normal users can do a single queue run */
15110Sstevel@tonic-gate 				if (QueueIntvl == 0)
15120Sstevel@tonic-gate 					break;
15130Sstevel@tonic-gate 			}
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 			/* but not persistent queue runners */
15160Sstevel@tonic-gate 			if (action == NULL)
15170Sstevel@tonic-gate 				action = "start a queue runner daemon";
15180Sstevel@tonic-gate 			/* FALLTHROUGH */
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 		  case MD_PURGESTAT:
15210Sstevel@tonic-gate 			if (action == NULL)
15220Sstevel@tonic-gate 				action = "purge host status";
15230Sstevel@tonic-gate 			/* FALLTHROUGH */
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 		  case MD_DAEMON:
15260Sstevel@tonic-gate 		  case MD_FGDAEMON:
15270Sstevel@tonic-gate 			if (action == NULL)
15280Sstevel@tonic-gate 				action = "run daemon";
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 			if (tTd(65, 1))
15310Sstevel@tonic-gate 				sm_dprintf("Deny user %d attempt to %s\n",
15320Sstevel@tonic-gate 					   (int) RealUid, action);
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 			if (LogLevel > 1)
15350Sstevel@tonic-gate 				sm_syslog(LOG_ALERT, NOQID,
15360Sstevel@tonic-gate 					  "user %d attempted to %s",
15370Sstevel@tonic-gate 					  (int) RealUid, action);
15380Sstevel@tonic-gate 			HoldErrs = false;
15390Sstevel@tonic-gate 			usrerr("Permission denied (real uid not trusted)");
15400Sstevel@tonic-gate 			finis(false, true, EX_USAGE);
15410Sstevel@tonic-gate 			/* NOTREACHED */
15420Sstevel@tonic-gate 			break;
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 		  case MD_VERIFY:
15450Sstevel@tonic-gate 			if (bitset(PRIV_RESTRICTEXPAND, PrivacyFlags))
15460Sstevel@tonic-gate 			{
15470Sstevel@tonic-gate 				/*
15480Sstevel@tonic-gate 				**  If -bv and RestrictExpand,
15490Sstevel@tonic-gate 				**  drop privs to prevent normal
15500Sstevel@tonic-gate 				**  users from reading private
15510Sstevel@tonic-gate 				**  aliases/forwards/:include:s
15520Sstevel@tonic-gate 				*/
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 				if (tTd(65, 1))
15550Sstevel@tonic-gate 					sm_dprintf("Drop privs for user %d attempt to expand (RestrictExpand)\n",
15560Sstevel@tonic-gate 						   (int) RealUid);
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 				dp = drop_privileges(true);
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 				/* Fake address safety */
15610Sstevel@tonic-gate 				if (tTd(65, 1))
15620Sstevel@tonic-gate 					sm_dprintf("Faking DontBlameSendmail=NonRootSafeAddr\n");
15630Sstevel@tonic-gate 				setbitn(DBS_NONROOTSAFEADDR, DontBlameSendmail);
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 				if (dp != EX_OK)
15660Sstevel@tonic-gate 				{
15670Sstevel@tonic-gate 					if (tTd(65, 1))
15680Sstevel@tonic-gate 						sm_dprintf("Failed to drop privs for user %d attempt to expand, exiting\n",
15690Sstevel@tonic-gate 							   (int) RealUid);
15700Sstevel@tonic-gate 					CurEnv->e_id = NULL;
15710Sstevel@tonic-gate 					finis(true, true, dp);
15720Sstevel@tonic-gate 					/* NOTREACHED */
15730Sstevel@tonic-gate 				}
15740Sstevel@tonic-gate 			}
15750Sstevel@tonic-gate 			break;
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 		  case MD_TEST:
1578*11440SJohn.Beck@Sun.COM 		  case MD_CHECKCONFIG:
15790Sstevel@tonic-gate 		  case MD_PRINT:
15800Sstevel@tonic-gate 		  case MD_PRINTNQE:
15810Sstevel@tonic-gate 		  case MD_FREEZE:
15820Sstevel@tonic-gate 		  case MD_HOSTSTAT:
15830Sstevel@tonic-gate 			/* Nothing special to check */
15840Sstevel@tonic-gate 			break;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 		  case MD_INITALIAS:
15870Sstevel@tonic-gate 			if (!wordinclass(RealUserName, 't'))
15880Sstevel@tonic-gate 			{
15890Sstevel@tonic-gate 				if (tTd(65, 1))
15900Sstevel@tonic-gate 					sm_dprintf("Deny user %d attempt to rebuild the alias map\n",
15910Sstevel@tonic-gate 						   (int) RealUid);
15920Sstevel@tonic-gate 				if (LogLevel > 1)
15930Sstevel@tonic-gate 					sm_syslog(LOG_ALERT, NOQID,
15940Sstevel@tonic-gate 						  "user %d attempted to rebuild the alias map",
15950Sstevel@tonic-gate 						  (int) RealUid);
15960Sstevel@tonic-gate 				HoldErrs = false;
15970Sstevel@tonic-gate 				usrerr("Permission denied (real uid not trusted)");
15980Sstevel@tonic-gate 				finis(false, true, EX_USAGE);
15990Sstevel@tonic-gate 				/* NOTREACHED */
16000Sstevel@tonic-gate 			}
16010Sstevel@tonic-gate 			if (UseMSP)
16020Sstevel@tonic-gate 			{
16030Sstevel@tonic-gate 				HoldErrs = false;
16040Sstevel@tonic-gate 				usrerr("User %d cannot rebuild aliases in mail submission program",
16050Sstevel@tonic-gate 				       (int) RealUid);
16060Sstevel@tonic-gate 				finis(false, true, EX_USAGE);
16070Sstevel@tonic-gate 				/* NOTREACHED */
16080Sstevel@tonic-gate 			}
16090Sstevel@tonic-gate 			/* FALLTHROUGH */
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 		  default:
16120Sstevel@tonic-gate 			if (bitset(PRIV_RESTRICTEXPAND, PrivacyFlags) &&
16130Sstevel@tonic-gate 			    Verbose != 0)
16140Sstevel@tonic-gate 			{
16150Sstevel@tonic-gate 				/*
16160Sstevel@tonic-gate 				**  If -v and RestrictExpand, reset
16170Sstevel@tonic-gate 				**  Verbose to prevent normal users
16180Sstevel@tonic-gate 				**  from seeing the expansion of
16190Sstevel@tonic-gate 				**  aliases/forwards/:include:s
16200Sstevel@tonic-gate 				*/
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 				if (tTd(65, 1))
16230Sstevel@tonic-gate 					sm_dprintf("Dropping verbosity for user %d (RestrictExpand)\n",
16240Sstevel@tonic-gate 						   (int) RealUid);
16250Sstevel@tonic-gate 				Verbose = 0;
16260Sstevel@tonic-gate 			}
16270Sstevel@tonic-gate 			break;
16280Sstevel@tonic-gate 		}
16290Sstevel@tonic-gate 	}
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	if (MeToo)
16320Sstevel@tonic-gate 		BlankEnvelope.e_flags |= EF_METOO;
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	switch (OpMode)
16350Sstevel@tonic-gate 	{
16360Sstevel@tonic-gate 	  case MD_TEST:
16370Sstevel@tonic-gate 		/* don't have persistent host status in test mode */
16380Sstevel@tonic-gate 		HostStatDir = NULL;
1639*11440SJohn.Beck@Sun.COM 		/* FALLTHROUGH */
1640*11440SJohn.Beck@Sun.COM 
1641*11440SJohn.Beck@Sun.COM 	  case MD_CHECKCONFIG:
16420Sstevel@tonic-gate 		if (Verbose == 0)
16430Sstevel@tonic-gate 			Verbose = 2;
16440Sstevel@tonic-gate 		BlankEnvelope.e_errormode = EM_PRINT;
16450Sstevel@tonic-gate 		HoldErrs = false;
16460Sstevel@tonic-gate 		break;
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	  case MD_VERIFY:
16490Sstevel@tonic-gate 		BlankEnvelope.e_errormode = EM_PRINT;
16500Sstevel@tonic-gate 		HoldErrs = false;
16510Sstevel@tonic-gate 		/* arrange to exit cleanly on hangup signal */
16520Sstevel@tonic-gate 		if (sm_signal(SIGHUP, SIG_IGN) == (sigfunc_t) SIG_DFL)
16530Sstevel@tonic-gate 			(void) sm_signal(SIGHUP, intsig);
16540Sstevel@tonic-gate 		if (geteuid() != 0)
16550Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
16560Sstevel@tonic-gate 					     "Notice: -bv may give misleading output for non-privileged user\n");
16570Sstevel@tonic-gate 		break;
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	  case MD_FGDAEMON:
16600Sstevel@tonic-gate 		run_in_foreground = true;
16610Sstevel@tonic-gate 		set_op_mode(MD_DAEMON);
16620Sstevel@tonic-gate 		/* FALLTHROUGH */
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	  case MD_DAEMON:
16650Sstevel@tonic-gate 		vendor_daemon_setup(&BlankEnvelope);
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 		/* remove things that don't make sense in daemon mode */
16680Sstevel@tonic-gate 		FullName = NULL;
16690Sstevel@tonic-gate 		GrabTo = false;
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 		/* arrange to restart on hangup signal */
16720Sstevel@tonic-gate 		if (SaveArgv[0] == NULL || SaveArgv[0][0] != '/')
16730Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
16740Sstevel@tonic-gate 				  "daemon invoked without full pathname; kill -1 won't work");
16750Sstevel@tonic-gate 		break;
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	  case MD_INITALIAS:
16780Sstevel@tonic-gate 		Verbose = 2;
16790Sstevel@tonic-gate 		BlankEnvelope.e_errormode = EM_PRINT;
16800Sstevel@tonic-gate 		HoldErrs = false;
16810Sstevel@tonic-gate 		/* FALLTHROUGH */
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	  default:
16840Sstevel@tonic-gate 		/* arrange to exit cleanly on hangup signal */
16850Sstevel@tonic-gate 		if (sm_signal(SIGHUP, SIG_IGN) == (sigfunc_t) SIG_DFL)
16860Sstevel@tonic-gate 			(void) sm_signal(SIGHUP, intsig);
16870Sstevel@tonic-gate 		break;
16880Sstevel@tonic-gate 	}
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	/* special considerations for FullName */
16910Sstevel@tonic-gate 	if (FullName != NULL)
16920Sstevel@tonic-gate 	{
16930Sstevel@tonic-gate 		char *full = NULL;
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 		/* full names can't have newlines */
16960Sstevel@tonic-gate 		if (strchr(FullName, '\n') != NULL)
16970Sstevel@tonic-gate 		{
16980Sstevel@tonic-gate 			full = newstr(denlstring(FullName, true, true));
16990Sstevel@tonic-gate 			FullName = full;
17000Sstevel@tonic-gate 		}
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 		/* check for characters that may have to be quoted */
17030Sstevel@tonic-gate 		if (!rfc822_string(FullName))
17040Sstevel@tonic-gate 		{
17050Sstevel@tonic-gate 			/*
17060Sstevel@tonic-gate 			**  Quote a full name with special characters
17070Sstevel@tonic-gate 			**  as a comment so crackaddr() doesn't destroy
17080Sstevel@tonic-gate 			**  the name portion of the address.
17090Sstevel@tonic-gate 			*/
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 			FullName = addquotes(FullName, NULL);
17120Sstevel@tonic-gate 			if (full != NULL)
17130Sstevel@tonic-gate 				sm_free(full);  /* XXX */
17140Sstevel@tonic-gate 		}
17150Sstevel@tonic-gate 	}
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 	/* do heuristic mode adjustment */
17180Sstevel@tonic-gate 	if (Verbose)
17190Sstevel@tonic-gate 	{
17200Sstevel@tonic-gate 		/* turn off noconnect option */
17210Sstevel@tonic-gate 		setoption('c', "F", true, false, &BlankEnvelope);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 		/* turn on interactive delivery */
17240Sstevel@tonic-gate 		setoption('d', "", true, false, &BlankEnvelope);
17250Sstevel@tonic-gate 	}
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate #ifdef VENDOR_CODE
17280Sstevel@tonic-gate 	/* check for vendor mismatch */
17290Sstevel@tonic-gate 	if (VendorCode != VENDOR_CODE)
17300Sstevel@tonic-gate 	{
17310Sstevel@tonic-gate 		message("Warning: .cf file vendor code mismatch: sendmail expects vendor %s, .cf file vendor is %s",
17320Sstevel@tonic-gate 			getvendor(VENDOR_CODE), getvendor(VendorCode));
17330Sstevel@tonic-gate 	}
17340Sstevel@tonic-gate #endif /* VENDOR_CODE */
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	/* check for out of date configuration level */
17370Sstevel@tonic-gate 	if (ConfigLevel < MAXCONFIGLEVEL)
17380Sstevel@tonic-gate 	{
17390Sstevel@tonic-gate 		message("Warning: .cf file is out of date: sendmail %s supports version %d, .cf file is version %d",
17400Sstevel@tonic-gate 			Version, MAXCONFIGLEVEL, ConfigLevel);
17410Sstevel@tonic-gate 	}
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 	if (ConfigLevel < 3)
17440Sstevel@tonic-gate 		UseErrorsTo = true;
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 	/* set options that were previous macros */
17470Sstevel@tonic-gate 	if (SmtpGreeting == NULL)
17480Sstevel@tonic-gate 	{
17490Sstevel@tonic-gate 		if (ConfigLevel < 7 &&
17500Sstevel@tonic-gate 		    (p = macvalue('e', &BlankEnvelope)) != NULL)
17510Sstevel@tonic-gate 			SmtpGreeting = newstr(p);
17520Sstevel@tonic-gate 		else
17530Sstevel@tonic-gate 			SmtpGreeting = "\201j Sendmail \201v ready at \201b";
17540Sstevel@tonic-gate 	}
17550Sstevel@tonic-gate 	if (UnixFromLine == NULL)
17560Sstevel@tonic-gate 	{
17570Sstevel@tonic-gate 		if (ConfigLevel < 7 &&
17580Sstevel@tonic-gate 		    (p = macvalue('l', &BlankEnvelope)) != NULL)
17590Sstevel@tonic-gate 			UnixFromLine = newstr(p);
17600Sstevel@tonic-gate 		else
17610Sstevel@tonic-gate 			UnixFromLine = "From \201g  \201d";
17620Sstevel@tonic-gate 	}
17630Sstevel@tonic-gate 	SmtpError[0] = '\0';
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	/* our name for SMTP codes */
17663544Sjbeck 	expand("\201j", jbuf, sizeof(jbuf), &BlankEnvelope);
17670Sstevel@tonic-gate 	if (jbuf[0] == '\0')
17680Sstevel@tonic-gate 		PSTRSET(MyHostName, "localhost");
17690Sstevel@tonic-gate 	else
17700Sstevel@tonic-gate 		PSTRSET(MyHostName, jbuf);
17710Sstevel@tonic-gate 	if (strchr(MyHostName, '.') == NULL)
17720Sstevel@tonic-gate 		message("WARNING: local host name (%s) is not qualified; see cf/README: WHO AM I?",
17730Sstevel@tonic-gate 			MyHostName);
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	/* make certain that this name is part of the $=w class */
17760Sstevel@tonic-gate 	setclass('w', MyHostName);
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 	/* fill in the structure of the *default* queue */
17790Sstevel@tonic-gate 	st = stab("mqueue", ST_QUEUE, ST_FIND);
17800Sstevel@tonic-gate 	if (st == NULL)
17810Sstevel@tonic-gate 		syserr("No default queue (mqueue) defined");
17820Sstevel@tonic-gate 	else
17830Sstevel@tonic-gate 		set_def_queueval(st->s_quegrp, true);
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	/* the indices of built-in mailers */
17860Sstevel@tonic-gate 	st = stab("local", ST_MAILER, ST_FIND);
17870Sstevel@tonic-gate 	if (st != NULL)
17880Sstevel@tonic-gate 		LocalMailer = st->s_mailer;
17890Sstevel@tonic-gate 	else if (OpMode != MD_TEST || !warn_C_flag)
17900Sstevel@tonic-gate 		syserr("No local mailer defined");
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	st = stab("prog", ST_MAILER, ST_FIND);
17930Sstevel@tonic-gate 	if (st == NULL)
17940Sstevel@tonic-gate 		syserr("No prog mailer defined");
17950Sstevel@tonic-gate 	else
17960Sstevel@tonic-gate 	{
17970Sstevel@tonic-gate 		ProgMailer = st->s_mailer;
17980Sstevel@tonic-gate 		clrbitn(M_MUSER, ProgMailer->m_flags);
17990Sstevel@tonic-gate 	}
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	st = stab("*file*", ST_MAILER, ST_FIND);
18020Sstevel@tonic-gate 	if (st == NULL)
18030Sstevel@tonic-gate 		syserr("No *file* mailer defined");
18040Sstevel@tonic-gate 	else
18050Sstevel@tonic-gate 	{
18060Sstevel@tonic-gate 		FileMailer = st->s_mailer;
18070Sstevel@tonic-gate 		clrbitn(M_MUSER, FileMailer->m_flags);
18080Sstevel@tonic-gate 	}
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 	st = stab("*include*", ST_MAILER, ST_FIND);
18110Sstevel@tonic-gate 	if (st == NULL)
18120Sstevel@tonic-gate 		syserr("No *include* mailer defined");
18130Sstevel@tonic-gate 	else
18140Sstevel@tonic-gate 		InclMailer = st->s_mailer;
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	if (ConfigLevel < 6)
18170Sstevel@tonic-gate 	{
18180Sstevel@tonic-gate 		/* heuristic tweaking of local mailer for back compat */
18190Sstevel@tonic-gate 		if (LocalMailer != NULL)
18200Sstevel@tonic-gate 		{
18210Sstevel@tonic-gate 			setbitn(M_ALIASABLE, LocalMailer->m_flags);
18220Sstevel@tonic-gate 			setbitn(M_HASPWENT, LocalMailer->m_flags);
18230Sstevel@tonic-gate 			setbitn(M_TRYRULESET5, LocalMailer->m_flags);
18240Sstevel@tonic-gate 			setbitn(M_CHECKINCLUDE, LocalMailer->m_flags);
18250Sstevel@tonic-gate 			setbitn(M_CHECKPROG, LocalMailer->m_flags);
18260Sstevel@tonic-gate 			setbitn(M_CHECKFILE, LocalMailer->m_flags);
18270Sstevel@tonic-gate 			setbitn(M_CHECKUDB, LocalMailer->m_flags);
18280Sstevel@tonic-gate 		}
18290Sstevel@tonic-gate 		if (ProgMailer != NULL)
18300Sstevel@tonic-gate 			setbitn(M_RUNASRCPT, ProgMailer->m_flags);
18310Sstevel@tonic-gate 		if (FileMailer != NULL)
18320Sstevel@tonic-gate 			setbitn(M_RUNASRCPT, FileMailer->m_flags);
18330Sstevel@tonic-gate 	}
18340Sstevel@tonic-gate 	if (ConfigLevel < 7)
18350Sstevel@tonic-gate 	{
18360Sstevel@tonic-gate 		if (LocalMailer != NULL)
18370Sstevel@tonic-gate 			setbitn(M_VRFY250, LocalMailer->m_flags);
18380Sstevel@tonic-gate 		if (ProgMailer != NULL)
18390Sstevel@tonic-gate 			setbitn(M_VRFY250, ProgMailer->m_flags);
18400Sstevel@tonic-gate 		if (FileMailer != NULL)
18410Sstevel@tonic-gate 			setbitn(M_VRFY250, FileMailer->m_flags);
18420Sstevel@tonic-gate 	}
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 	/* MIME Content-Types that cannot be transfer encoded */
18450Sstevel@tonic-gate 	setclass('n', "multipart/signed");
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 	/* MIME message/xxx subtypes that can be treated as messages */
18480Sstevel@tonic-gate 	setclass('s', "rfc822");
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	/* MIME Content-Transfer-Encodings that can be encoded */
18510Sstevel@tonic-gate 	setclass('e', "7bit");
18520Sstevel@tonic-gate 	setclass('e', "8bit");
18530Sstevel@tonic-gate 	setclass('e', "binary");
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate #ifdef USE_B_CLASS
18560Sstevel@tonic-gate 	/* MIME Content-Types that should be treated as binary */
18570Sstevel@tonic-gate 	setclass('b', "image");
18580Sstevel@tonic-gate 	setclass('b', "audio");
18590Sstevel@tonic-gate 	setclass('b', "video");
18600Sstevel@tonic-gate 	setclass('b', "application/octet-stream");
18610Sstevel@tonic-gate #endif /* USE_B_CLASS */
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	/* MIME headers which have fields to check for overflow */
18640Sstevel@tonic-gate 	setclass(macid("{checkMIMEFieldHeaders}"), "content-disposition");
18650Sstevel@tonic-gate 	setclass(macid("{checkMIMEFieldHeaders}"), "content-type");
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	/* MIME headers to check for length overflow */
18680Sstevel@tonic-gate 	setclass(macid("{checkMIMETextHeaders}"), "content-description");
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 	/* MIME headers to check for overflow and rebalance */
18710Sstevel@tonic-gate 	setclass(macid("{checkMIMEHeaders}"), "content-disposition");
18720Sstevel@tonic-gate 	setclass(macid("{checkMIMEHeaders}"), "content-id");
18730Sstevel@tonic-gate 	setclass(macid("{checkMIMEHeaders}"), "content-transfer-encoding");
18740Sstevel@tonic-gate 	setclass(macid("{checkMIMEHeaders}"), "content-type");
18750Sstevel@tonic-gate 	setclass(macid("{checkMIMEHeaders}"), "mime-version");
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 	/* Macros to save in the queue file -- don't remove any */
18780Sstevel@tonic-gate 	setclass(macid("{persistentMacros}"), "r");
18790Sstevel@tonic-gate 	setclass(macid("{persistentMacros}"), "s");
18800Sstevel@tonic-gate 	setclass(macid("{persistentMacros}"), "_");
18810Sstevel@tonic-gate 	setclass(macid("{persistentMacros}"), "{if_addr}");
18820Sstevel@tonic-gate 	setclass(macid("{persistentMacros}"), "{daemon_flags}");
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 	/* operate in queue directory */
18850Sstevel@tonic-gate 	if (QueueDir == NULL || *QueueDir == '\0')
18860Sstevel@tonic-gate 	{
18870Sstevel@tonic-gate 		if (OpMode != MD_TEST)
18880Sstevel@tonic-gate 		{
18890Sstevel@tonic-gate 			syserr("QueueDirectory (Q) option must be set");
18900Sstevel@tonic-gate 			ExitStat = EX_CONFIG;
18910Sstevel@tonic-gate 		}
18920Sstevel@tonic-gate 	}
18930Sstevel@tonic-gate 	else
18940Sstevel@tonic-gate 	{
18950Sstevel@tonic-gate 		if (OpMode != MD_TEST)
18960Sstevel@tonic-gate 			setup_queues(OpMode == MD_DAEMON);
18970Sstevel@tonic-gate 	}
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 	/* check host status directory for validity */
19000Sstevel@tonic-gate 	if (HostStatDir != NULL && !path_is_dir(HostStatDir, false))
19010Sstevel@tonic-gate 	{
19020Sstevel@tonic-gate 		/* cannot use this value */
19030Sstevel@tonic-gate 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
19040Sstevel@tonic-gate 				     "Warning: Cannot use HostStatusDirectory = %s: %s\n",
19050Sstevel@tonic-gate 				     HostStatDir, sm_errstring(errno));
19060Sstevel@tonic-gate 		HostStatDir = NULL;
19070Sstevel@tonic-gate 	}
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	if (OpMode == MD_QUEUERUN &&
19100Sstevel@tonic-gate 	    RealUid != 0 && bitset(PRIV_RESTRICTQRUN, PrivacyFlags))
19110Sstevel@tonic-gate 	{
19120Sstevel@tonic-gate 		struct stat stbuf;
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 		/* check to see if we own the queue directory */
19150Sstevel@tonic-gate 		if (stat(".", &stbuf) < 0)
19160Sstevel@tonic-gate 			syserr("main: cannot stat %s", QueueDir);
19170Sstevel@tonic-gate 		if (stbuf.st_uid != RealUid)
19180Sstevel@tonic-gate 		{
19190Sstevel@tonic-gate 			/* nope, really a botch */
19200Sstevel@tonic-gate 			HoldErrs = false;
19210Sstevel@tonic-gate 			usrerr("You do not have permission to process the queue");
19220Sstevel@tonic-gate 			finis(false, true, EX_NOPERM);
19230Sstevel@tonic-gate 			/* NOTREACHED */
19240Sstevel@tonic-gate 		}
19250Sstevel@tonic-gate 	}
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate #if MILTER
19280Sstevel@tonic-gate 	/* sanity checks on milter filters */
19290Sstevel@tonic-gate 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP)
19300Sstevel@tonic-gate 	{
19310Sstevel@tonic-gate 		milter_config(InputFilterList, InputFilters, MAXFILTERS);
19320Sstevel@tonic-gate 		setup_daemon_milters();
19330Sstevel@tonic-gate 	}
19340Sstevel@tonic-gate #endif /* MILTER */
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 	/* Convert queuegroup string to qgrp number */
19370Sstevel@tonic-gate 	if (queuegroup != NULL)
19380Sstevel@tonic-gate 	{
19390Sstevel@tonic-gate 		qgrp = name2qid(queuegroup);
19400Sstevel@tonic-gate 		if (qgrp == NOQGRP)
19410Sstevel@tonic-gate 		{
19420Sstevel@tonic-gate 			HoldErrs = false;
19430Sstevel@tonic-gate 			usrerr("Queue group %s unknown", queuegroup);
19440Sstevel@tonic-gate 			finis(false, true, ExitStat);
19450Sstevel@tonic-gate 			/* NOTREACHED */
19460Sstevel@tonic-gate 		}
19470Sstevel@tonic-gate 	}
19480Sstevel@tonic-gate 
1949*11440SJohn.Beck@Sun.COM 	/* if checking config or have had errors so far, exit now */
1950*11440SJohn.Beck@Sun.COM 	if (OpMode == MD_CHECKCONFIG || (ExitStat != EX_OK && OpMode != MD_TEST))
19510Sstevel@tonic-gate 	{
19520Sstevel@tonic-gate 		finis(false, true, ExitStat);
19530Sstevel@tonic-gate 		/* NOTREACHED */
19540Sstevel@tonic-gate 	}
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate #if SASL
19570Sstevel@tonic-gate 	/* sendmail specific SASL initialization */
19580Sstevel@tonic-gate 	sm_sasl_init();
19590Sstevel@tonic-gate #endif /* SASL */
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate #if XDEBUG
19620Sstevel@tonic-gate 	checkfd012("before main() initmaps");
19630Sstevel@tonic-gate #endif /* XDEBUG */
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	/*
19660Sstevel@tonic-gate 	**  Do operation-mode-dependent initialization.
19670Sstevel@tonic-gate 	*/
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	switch (OpMode)
19700Sstevel@tonic-gate 	{
19710Sstevel@tonic-gate 	  case MD_PRINT:
19720Sstevel@tonic-gate 		/* print the queue */
19730Sstevel@tonic-gate 		HoldErrs = false;
1974*11440SJohn.Beck@Sun.COM 		(void) dropenvelope(&BlankEnvelope, true, false);
19750Sstevel@tonic-gate 		(void) sm_signal(SIGPIPE, sigpipe);
19760Sstevel@tonic-gate 		if (qgrp != NOQGRP)
19770Sstevel@tonic-gate 		{
19780Sstevel@tonic-gate 			int j;
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 			/* Selecting a particular queue group to run */
19810Sstevel@tonic-gate 			for (j = 0; j < Queue[qgrp]->qg_numqueues; j++)
19820Sstevel@tonic-gate 			{
19830Sstevel@tonic-gate 				if (StopRequest)
19840Sstevel@tonic-gate 					stop_sendmail();
19850Sstevel@tonic-gate 				(void) print_single_queue(qgrp, j);
19860Sstevel@tonic-gate 			}
19870Sstevel@tonic-gate 			finis(false, true, EX_OK);
19880Sstevel@tonic-gate 			/* NOTREACHED */
19890Sstevel@tonic-gate 		}
19900Sstevel@tonic-gate 		printqueue();
19910Sstevel@tonic-gate 		finis(false, true, EX_OK);
19920Sstevel@tonic-gate 		/* NOTREACHED */
19930Sstevel@tonic-gate 		break;
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	  case MD_PRINTNQE:
19960Sstevel@tonic-gate 		/* print number of entries in queue */
1997*11440SJohn.Beck@Sun.COM 		(void) dropenvelope(&BlankEnvelope, true, false);
19980Sstevel@tonic-gate 		(void) sm_signal(SIGPIPE, sigpipe);
19990Sstevel@tonic-gate 		printnqe(smioout, NULL);
20000Sstevel@tonic-gate 		finis(false, true, EX_OK);
20010Sstevel@tonic-gate 		/* NOTREACHED */
20020Sstevel@tonic-gate 		break;
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 	  case MD_QUEUERUN:
20050Sstevel@tonic-gate 		/* only handle quarantining here */
20060Sstevel@tonic-gate 		if (quarantining == NULL)
20070Sstevel@tonic-gate 			break;
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 		if (QueueMode != QM_QUARANTINE &&
20100Sstevel@tonic-gate 		    QueueMode != QM_NORMAL)
20110Sstevel@tonic-gate 		{
20120Sstevel@tonic-gate 			HoldErrs = false;
20130Sstevel@tonic-gate 			usrerr("Can not use -Q with -q%c", QueueMode);
20140Sstevel@tonic-gate 			ExitStat = EX_USAGE;
20150Sstevel@tonic-gate 			finis(false, true, ExitStat);
20160Sstevel@tonic-gate 			/* NOTREACHED */
20170Sstevel@tonic-gate 		}
20180Sstevel@tonic-gate 		quarantine_queue(quarantining, qgrp);
20190Sstevel@tonic-gate 		finis(false, true, EX_OK);
20200Sstevel@tonic-gate 		break;
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	  case MD_HOSTSTAT:
20230Sstevel@tonic-gate 		(void) sm_signal(SIGPIPE, sigpipe);
20240Sstevel@tonic-gate 		(void) mci_traverse_persistent(mci_print_persistent, NULL);
20250Sstevel@tonic-gate 		finis(false, true, EX_OK);
20260Sstevel@tonic-gate 		/* NOTREACHED */
20270Sstevel@tonic-gate 		break;
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 	  case MD_PURGESTAT:
20300Sstevel@tonic-gate 		(void) mci_traverse_persistent(mci_purge_persistent, NULL);
20310Sstevel@tonic-gate 		finis(false, true, EX_OK);
20320Sstevel@tonic-gate 		/* NOTREACHED */
20330Sstevel@tonic-gate 		break;
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	  case MD_INITALIAS:
20360Sstevel@tonic-gate 		/* initialize maps */
20370Sstevel@tonic-gate 		initmaps();
20380Sstevel@tonic-gate 		finis(false, true, ExitStat);
20390Sstevel@tonic-gate 		/* NOTREACHED */
20400Sstevel@tonic-gate 		break;
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	  case MD_SMTP:
20430Sstevel@tonic-gate 	  case MD_DAEMON:
20440Sstevel@tonic-gate 		/* reset DSN parameters */
20450Sstevel@tonic-gate 		DefaultNotify = QPINGONFAILURE|QPINGONDELAY;
20460Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_PERM,
20470Sstevel@tonic-gate 			  macid("{dsn_notify}"), NULL);
20480Sstevel@tonic-gate 		BlankEnvelope.e_envid = NULL;
20490Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_PERM,
20500Sstevel@tonic-gate 			  macid("{dsn_envid}"), NULL);
20510Sstevel@tonic-gate 		BlankEnvelope.e_flags &= ~(EF_RET_PARAM|EF_NO_BODY_RETN);
20520Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_PERM,
20530Sstevel@tonic-gate 			  macid("{dsn_ret}"), NULL);
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 		/* don't open maps for daemon -- done below in child */
20560Sstevel@tonic-gate 		break;
20570Sstevel@tonic-gate 	}
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	if (tTd(0, 15))
20600Sstevel@tonic-gate 	{
20610Sstevel@tonic-gate 		/* print configuration table (or at least part of it) */
20620Sstevel@tonic-gate 		if (tTd(0, 90))
20630Sstevel@tonic-gate 			printrules();
20640Sstevel@tonic-gate 		for (i = 0; i < MAXMAILERS; i++)
20650Sstevel@tonic-gate 		{
20660Sstevel@tonic-gate 			if (Mailer[i] != NULL)
20670Sstevel@tonic-gate 				printmailer(sm_debug_file(), Mailer[i]);
20680Sstevel@tonic-gate 		}
20690Sstevel@tonic-gate 	}
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	/*
20720Sstevel@tonic-gate 	**  Switch to the main envelope.
20730Sstevel@tonic-gate 	*/
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	CurEnv = newenvelope(&MainEnvelope, &BlankEnvelope,
20760Sstevel@tonic-gate 			     sm_rpool_new_x(NULL));
20770Sstevel@tonic-gate 	MainEnvelope.e_flags = BlankEnvelope.e_flags;
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 	/*
20800Sstevel@tonic-gate 	**  If test mode, read addresses from stdin and process.
20810Sstevel@tonic-gate 	*/
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	if (OpMode == MD_TEST)
20840Sstevel@tonic-gate 	{
20850Sstevel@tonic-gate 		if (isatty(sm_io_getinfo(smioin, SM_IO_WHAT_FD, NULL)))
20860Sstevel@tonic-gate 			Verbose = 2;
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 		if (Verbose)
20890Sstevel@tonic-gate 		{
20900Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
20910Sstevel@tonic-gate 				     "ADDRESS TEST MODE (ruleset 3 NOT automatically invoked)\n");
20920Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
20930Sstevel@tonic-gate 				     "Enter <ruleset> <address>\n");
20940Sstevel@tonic-gate 		}
20950Sstevel@tonic-gate 		macdefine(&(MainEnvelope.e_macro), A_PERM,
20960Sstevel@tonic-gate 			  macid("{addr_type}"), "e r");
20970Sstevel@tonic-gate 		for (;;)
20980Sstevel@tonic-gate 		{
20990Sstevel@tonic-gate 			SM_TRY
21000Sstevel@tonic-gate 			{
21010Sstevel@tonic-gate 				(void) sm_signal(SIGINT, intindebug);
21020Sstevel@tonic-gate 				(void) sm_releasesignal(SIGINT);
21030Sstevel@tonic-gate 				if (Verbose == 2)
21040Sstevel@tonic-gate 					(void) sm_io_fprintf(smioout,
21050Sstevel@tonic-gate 							     SM_TIME_DEFAULT,
21060Sstevel@tonic-gate 							     "> ");
21070Sstevel@tonic-gate 				(void) sm_io_flush(smioout, SM_TIME_DEFAULT);
21080Sstevel@tonic-gate 				if (sm_io_fgets(smioin, SM_TIME_DEFAULT, buf,
21093544Sjbeck 						sizeof(buf)) == NULL)
21100Sstevel@tonic-gate 					testmodeline("/quit", &MainEnvelope);
21110Sstevel@tonic-gate 				p = strchr(buf, '\n');
21120Sstevel@tonic-gate 				if (p != NULL)
21130Sstevel@tonic-gate 					*p = '\0';
21140Sstevel@tonic-gate 				if (Verbose < 2)
21150Sstevel@tonic-gate 					(void) sm_io_fprintf(smioout,
21160Sstevel@tonic-gate 							     SM_TIME_DEFAULT,
21170Sstevel@tonic-gate 							     "> %s\n", buf);
21180Sstevel@tonic-gate 				testmodeline(buf, &MainEnvelope);
21190Sstevel@tonic-gate 			}
21200Sstevel@tonic-gate 			SM_EXCEPT(exc, "[!F]*")
21210Sstevel@tonic-gate 			{
21220Sstevel@tonic-gate 				/*
21230Sstevel@tonic-gate 				**  8.10 just prints \n on interrupt.
21240Sstevel@tonic-gate 				**  I'm printing the exception here in case
21250Sstevel@tonic-gate 				**  sendmail is extended to raise additional
21260Sstevel@tonic-gate 				**  exceptions in this context.
21270Sstevel@tonic-gate 				*/
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
21300Sstevel@tonic-gate 						     "\n");
21310Sstevel@tonic-gate 				sm_exc_print(exc, smioout);
21320Sstevel@tonic-gate 			}
21330Sstevel@tonic-gate 			SM_END_TRY
21340Sstevel@tonic-gate 		}
21350Sstevel@tonic-gate 	}
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate #if STARTTLS
21380Sstevel@tonic-gate 	tls_ok = true;
21393544Sjbeck 	if (OpMode == MD_QUEUERUN || OpMode == MD_DELIVER ||
21403544Sjbeck 	    OpMode == MD_ARPAFTP)
21410Sstevel@tonic-gate 	{
21420Sstevel@tonic-gate 		/* check whether STARTTLS is turned off for the client */
21430Sstevel@tonic-gate 		if (chkclientmodifiers(D_NOTLS))
21440Sstevel@tonic-gate 			tls_ok = false;
21450Sstevel@tonic-gate 	}
21460Sstevel@tonic-gate 	else if (OpMode == MD_DAEMON || OpMode == MD_FGDAEMON ||
21470Sstevel@tonic-gate 		 OpMode == MD_SMTP)
21480Sstevel@tonic-gate 	{
2149*11440SJohn.Beck@Sun.COM 		/* check whether STARTTLS is turned off */
2150*11440SJohn.Beck@Sun.COM 		if (chkdaemonmodifiers(D_NOTLS) && chkclientmodifiers(D_NOTLS))
21510Sstevel@tonic-gate 			tls_ok = false;
21520Sstevel@tonic-gate 	}
21530Sstevel@tonic-gate 	else	/* other modes don't need STARTTLS */
21540Sstevel@tonic-gate 		tls_ok = false;
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 	if (tls_ok)
21570Sstevel@tonic-gate 	{
21580Sstevel@tonic-gate 		/* basic TLS initialization */
21590Sstevel@tonic-gate 		tls_ok = init_tls_library();
21600Sstevel@tonic-gate 	}
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 	if (!tls_ok && (OpMode == MD_QUEUERUN || OpMode == MD_DELIVER))
21630Sstevel@tonic-gate 	{
21640Sstevel@tonic-gate 		/* disable TLS for client */
21650Sstevel@tonic-gate 		setclttls(false);
21660Sstevel@tonic-gate 	}
21670Sstevel@tonic-gate #endif /* STARTTLS */
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate 	/*
21700Sstevel@tonic-gate 	**  If collecting stuff from the queue, go start doing that.
21710Sstevel@tonic-gate 	*/
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 	if (OpMode == MD_QUEUERUN && QueueIntvl == 0)
21740Sstevel@tonic-gate 	{
21750Sstevel@tonic-gate 		pid_t pid = -1;
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate #if STARTTLS
21780Sstevel@tonic-gate 		/* init TLS for client, ignore result for now */
21790Sstevel@tonic-gate 		(void) initclttls(tls_ok);
21800Sstevel@tonic-gate #endif /* STARTTLS */
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 		/*
21830Sstevel@tonic-gate 		**  The parent process of the caller of runqueue() needs
21840Sstevel@tonic-gate 		**  to stay around for a possible SIGTERM. The SIGTERM will
21850Sstevel@tonic-gate 		**  tell this process that all of the queue runners children
21860Sstevel@tonic-gate 		**  need to be sent SIGTERM as well. At the same time, we
21870Sstevel@tonic-gate 		**  want to return control to the command line. So we do an
21880Sstevel@tonic-gate 		**  extra fork().
21890Sstevel@tonic-gate 		*/
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 		if (Verbose || foregroundqueue || (pid = fork()) <= 0)
21920Sstevel@tonic-gate 		{
21930Sstevel@tonic-gate 			/*
21940Sstevel@tonic-gate 			**  If the fork() failed we should still try to do
21950Sstevel@tonic-gate 			**  the queue run. If it succeeded then the child
21960Sstevel@tonic-gate 			**  is going to start the run and wait for all
21970Sstevel@tonic-gate 			**  of the children to finish.
21980Sstevel@tonic-gate 			*/
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 			if (pid == 0)
22010Sstevel@tonic-gate 			{
22020Sstevel@tonic-gate 				/* Reset global flags */
22030Sstevel@tonic-gate 				RestartRequest = NULL;
22040Sstevel@tonic-gate 				ShutdownRequest = NULL;
22050Sstevel@tonic-gate 				PendingSignal = 0;
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 				/* disconnect from terminal */
22080Sstevel@tonic-gate 				disconnect(2, CurEnv);
22090Sstevel@tonic-gate 			}
22100Sstevel@tonic-gate 
22110Sstevel@tonic-gate 			CurrentPid = getpid();
22120Sstevel@tonic-gate 			if (qgrp != NOQGRP)
22130Sstevel@tonic-gate 			{
22140Sstevel@tonic-gate 				int rwgflags = RWG_NONE;
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 				/*
22170Sstevel@tonic-gate 				**  To run a specific queue group mark it to
22180Sstevel@tonic-gate 				**  be run, select the work group it's in and
22190Sstevel@tonic-gate 				**  increment the work counter.
22200Sstevel@tonic-gate 				*/
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 				for (i = 0; i < NumQueue && Queue[i] != NULL;
22230Sstevel@tonic-gate 				     i++)
22240Sstevel@tonic-gate 					Queue[i]->qg_nextrun = (time_t) -1;
22250Sstevel@tonic-gate 				Queue[qgrp]->qg_nextrun = 0;
22260Sstevel@tonic-gate 				if (Verbose)
22270Sstevel@tonic-gate 					rwgflags |= RWG_VERBOSE;
22280Sstevel@tonic-gate 				if (queuepersistent)
22290Sstevel@tonic-gate 					rwgflags |= RWG_PERSISTENT;
22300Sstevel@tonic-gate 				rwgflags |= RWG_FORCE;
22310Sstevel@tonic-gate 				(void) run_work_group(Queue[qgrp]->qg_wgrp,
22320Sstevel@tonic-gate 						      rwgflags);
22330Sstevel@tonic-gate 			}
22340Sstevel@tonic-gate 			else
22350Sstevel@tonic-gate 				(void) runqueue(false, Verbose,
22360Sstevel@tonic-gate 						queuepersistent, true);
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 			/* set the title to make it easier to find */
22390Sstevel@tonic-gate 			sm_setproctitle(true, CurEnv, "Queue control");
22400Sstevel@tonic-gate 			(void) sm_signal(SIGCHLD, SIG_DFL);
22410Sstevel@tonic-gate 			while (CurChildren > 0)
22420Sstevel@tonic-gate 			{
22430Sstevel@tonic-gate 				int status;
22440Sstevel@tonic-gate 				pid_t ret;
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate 				errno = 0;
22470Sstevel@tonic-gate 				while ((ret = sm_wait(&status)) <= 0)
22480Sstevel@tonic-gate 				{
22490Sstevel@tonic-gate 					if (errno == ECHILD)
22500Sstevel@tonic-gate 					{
22510Sstevel@tonic-gate 						/*
22520Sstevel@tonic-gate 						**  Oops... something got messed
22530Sstevel@tonic-gate 						**  up really bad. Waiting for
22540Sstevel@tonic-gate 						**  non-existent children
22550Sstevel@tonic-gate 						**  shouldn't happen. Let's get
22560Sstevel@tonic-gate 						**  out of here.
22570Sstevel@tonic-gate 						*/
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 						CurChildren = 0;
22600Sstevel@tonic-gate 						break;
22610Sstevel@tonic-gate 					}
22620Sstevel@tonic-gate 					continue;
22630Sstevel@tonic-gate 				}
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 				/* something is really really wrong */
22660Sstevel@tonic-gate 				if (errno == ECHILD)
22670Sstevel@tonic-gate 				{
22680Sstevel@tonic-gate 					sm_syslog(LOG_ERR, NOQID,
22690Sstevel@tonic-gate 						  "queue control process: lost all children: wait returned ECHILD");
22700Sstevel@tonic-gate 					break;
22710Sstevel@tonic-gate 				}
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 				/* Only drop when a child gives status */
22740Sstevel@tonic-gate 				if (WIFSTOPPED(status))
22750Sstevel@tonic-gate 					continue;
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate 				proc_list_drop(ret, status, NULL);
22780Sstevel@tonic-gate 			}
22790Sstevel@tonic-gate 		}
22800Sstevel@tonic-gate 		finis(true, true, ExitStat);
22810Sstevel@tonic-gate 		/* NOTREACHED */
22820Sstevel@tonic-gate 	}
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate # if SASL
22850Sstevel@tonic-gate 	if (OpMode == MD_SMTP || OpMode == MD_DAEMON)
22860Sstevel@tonic-gate 	{
22870Sstevel@tonic-gate 		/* check whether AUTH is turned off for the server */
22880Sstevel@tonic-gate 		if (!chkdaemonmodifiers(D_NOAUTH) &&
22890Sstevel@tonic-gate 		    (i = sasl_server_init(srvcallbacks, "Sendmail")) != SASL_OK)
22900Sstevel@tonic-gate 			syserr("!sasl_server_init failed! [%s]",
22910Sstevel@tonic-gate 				sasl_errstring(i, NULL, NULL));
22920Sstevel@tonic-gate 	}
22930Sstevel@tonic-gate # endif /* SASL */
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 	if (OpMode == MD_SMTP)
22960Sstevel@tonic-gate 	{
22970Sstevel@tonic-gate 		proc_list_add(CurrentPid, "Sendmail SMTP Agent",
22980Sstevel@tonic-gate 			      PROC_DAEMON, 0, -1, NULL);
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate 		/* clean up background delivery children */
23010Sstevel@tonic-gate 		(void) sm_signal(SIGCHLD, reapchild);
23020Sstevel@tonic-gate 	}
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	/*
23050Sstevel@tonic-gate 	**  If a daemon, wait for a request.
23060Sstevel@tonic-gate 	**	getrequests will always return in a child.
23070Sstevel@tonic-gate 	**	If we should also be processing the queue, start
23080Sstevel@tonic-gate 	**		doing it in background.
23090Sstevel@tonic-gate 	**	We check for any errors that might have happened
23100Sstevel@tonic-gate 	**		during startup.
23110Sstevel@tonic-gate 	*/
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 	if (OpMode == MD_DAEMON || QueueIntvl > 0)
23140Sstevel@tonic-gate 	{
23150Sstevel@tonic-gate 		char dtype[200];
23160Sstevel@tonic-gate 
23172526Sjbeck 		/* avoid cleanup in finis(), DaemonPid will be set below */
23182526Sjbeck 		DaemonPid = 0;
23190Sstevel@tonic-gate 		if (!run_in_foreground && !tTd(99, 100))
23200Sstevel@tonic-gate 		{
23210Sstevel@tonic-gate 			/* put us in background */
23220Sstevel@tonic-gate 			i = fork();
23230Sstevel@tonic-gate 			if (i < 0)
23240Sstevel@tonic-gate 				syserr("daemon: cannot fork");
23250Sstevel@tonic-gate 			if (i != 0)
23260Sstevel@tonic-gate 			{
23270Sstevel@tonic-gate 				finis(false, true, EX_OK);
23280Sstevel@tonic-gate 				/* NOTREACHED */
23290Sstevel@tonic-gate 			}
23300Sstevel@tonic-gate 
23310Sstevel@tonic-gate 			/*
23320Sstevel@tonic-gate 			**  Initialize exception stack and default exception
23330Sstevel@tonic-gate 			**  handler for child process.
23340Sstevel@tonic-gate 			*/
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 			/* Reset global flags */
23370Sstevel@tonic-gate 			RestartRequest = NULL;
23380Sstevel@tonic-gate 			RestartWorkGroup = false;
23390Sstevel@tonic-gate 			ShutdownRequest = NULL;
23400Sstevel@tonic-gate 			PendingSignal = 0;
23410Sstevel@tonic-gate 			CurrentPid = getpid();
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 			sm_exc_newthread(fatal_error);
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 			/* disconnect from our controlling tty */
23460Sstevel@tonic-gate 			disconnect(2, &MainEnvelope);
23470Sstevel@tonic-gate 		}
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate 		dtype[0] = '\0';
23500Sstevel@tonic-gate 		if (OpMode == MD_DAEMON)
23512526Sjbeck 		{
23523544Sjbeck 			(void) sm_strlcat(dtype, "+SMTP", sizeof(dtype));
23532526Sjbeck 			DaemonPid = CurrentPid;
23542526Sjbeck 		}
23550Sstevel@tonic-gate 		if (QueueIntvl > 0)
23560Sstevel@tonic-gate 		{
23570Sstevel@tonic-gate 			(void) sm_strlcat2(dtype,
23580Sstevel@tonic-gate 					   queuepersistent
23590Sstevel@tonic-gate 					   ? "+persistent-queueing@"
23600Sstevel@tonic-gate 					   : "+queueing@",
23610Sstevel@tonic-gate 					   pintvl(QueueIntvl, true),
23623544Sjbeck 					   sizeof(dtype));
23630Sstevel@tonic-gate 		}
23640Sstevel@tonic-gate 		if (tTd(0, 1))
23653544Sjbeck 			(void) sm_strlcat(dtype, "+debugging", sizeof(dtype));
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 		sm_syslog(LOG_INFO, NOQID,
23680Sstevel@tonic-gate 			  "starting daemon (%s): %s", Version, dtype + 1);
23690Sstevel@tonic-gate #if XLA
23700Sstevel@tonic-gate 		xla_create_file();
23710Sstevel@tonic-gate #endif /* XLA */
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 		/* save daemon type in a macro for possible PidFile use */
23740Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_TEMP,
23750Sstevel@tonic-gate 			macid("{daemon_info}"), dtype + 1);
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 		/* save queue interval in a macro for possible PidFile use */
23780Sstevel@tonic-gate 		macdefine(&MainEnvelope.e_macro, A_TEMP,
23790Sstevel@tonic-gate 			macid("{queue_interval}"), pintvl(QueueIntvl, true));
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 		/* workaround: can't seem to release the signal in the parent */
23820Sstevel@tonic-gate 		(void) sm_signal(SIGHUP, sighup);
23830Sstevel@tonic-gate 		(void) sm_releasesignal(SIGHUP);
23840Sstevel@tonic-gate 		(void) sm_signal(SIGTERM, sigterm);
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 		if (QueueIntvl > 0)
23870Sstevel@tonic-gate 		{
23885402Sjbeck #if _FFR_RUNPQG
23895402Sjbeck 			if (qgrp != NOQGRP)
23905402Sjbeck 			{
23915402Sjbeck 				int rwgflags = RWG_NONE;
23925402Sjbeck 
23935402Sjbeck 				/*
23945402Sjbeck 				**  To run a specific queue group mark it to
23955402Sjbeck 				**  be run, select the work group it's in and
23965402Sjbeck 				**  increment the work counter.
23975402Sjbeck 				*/
23985402Sjbeck 
23995402Sjbeck 				for (i = 0; i < NumQueue && Queue[i] != NULL;
24005402Sjbeck 				     i++)
24015402Sjbeck 					Queue[i]->qg_nextrun = (time_t) -1;
24025402Sjbeck 				Queue[qgrp]->qg_nextrun = 0;
24035402Sjbeck 				if (Verbose)
24045402Sjbeck 					rwgflags |= RWG_VERBOSE;
24055402Sjbeck 				if (queuepersistent)
24065402Sjbeck 					rwgflags |= RWG_PERSISTENT;
24075402Sjbeck 				rwgflags |= RWG_FORCE;
24085402Sjbeck 				(void) run_work_group(Queue[qgrp]->qg_wgrp,
24095402Sjbeck 						      rwgflags);
24105402Sjbeck 			}
24115402Sjbeck 			else
24125402Sjbeck #endif /* _FFR_RUNPQG */
24135402Sjbeck 				(void) runqueue(true, false, queuepersistent,
24145402Sjbeck 						true);
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate 			/*
24170Sstevel@tonic-gate 			**  If queuepersistent but not in daemon mode then
24180Sstevel@tonic-gate 			**  we're going to do the queue runner monitoring here.
24190Sstevel@tonic-gate 			**  If in daemon mode then the monitoring will happen
24200Sstevel@tonic-gate 			**  elsewhere.
24210Sstevel@tonic-gate 			*/
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 			if (OpMode != MD_DAEMON && queuepersistent)
24240Sstevel@tonic-gate 			{
24250Sstevel@tonic-gate 				/*
24260Sstevel@tonic-gate 				**  Write the pid to file
24270Sstevel@tonic-gate 				**  XXX Overwrites sendmail.pid
24280Sstevel@tonic-gate 				*/
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 				log_sendmail_pid(&MainEnvelope);
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 				/* set the title to make it easier to find */
24330Sstevel@tonic-gate 				sm_setproctitle(true, CurEnv, "Queue control");
24340Sstevel@tonic-gate 				(void) sm_signal(SIGCHLD, SIG_DFL);
24350Sstevel@tonic-gate 				while (CurChildren > 0)
24360Sstevel@tonic-gate 				{
24370Sstevel@tonic-gate 					int status;
24380Sstevel@tonic-gate 					pid_t ret;
24390Sstevel@tonic-gate 					int group;
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 					CHECK_RESTART;
24420Sstevel@tonic-gate 					errno = 0;
24430Sstevel@tonic-gate 					while ((ret = sm_wait(&status)) <= 0)
24440Sstevel@tonic-gate 					{
24450Sstevel@tonic-gate 						/*
24460Sstevel@tonic-gate 						**  Waiting for non-existent
24470Sstevel@tonic-gate 						**  children shouldn't happen.
24480Sstevel@tonic-gate 						**  Let's get out of here if
24490Sstevel@tonic-gate 						**  it occurs.
24500Sstevel@tonic-gate 						*/
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate 						if (errno == ECHILD)
24530Sstevel@tonic-gate 						{
24540Sstevel@tonic-gate 							CurChildren = 0;
24550Sstevel@tonic-gate 							break;
24560Sstevel@tonic-gate 						}
24570Sstevel@tonic-gate 						continue;
24580Sstevel@tonic-gate 					}
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 					/* something is really really wrong */
24610Sstevel@tonic-gate 					if (errno == ECHILD)
24620Sstevel@tonic-gate 					{
24630Sstevel@tonic-gate 						sm_syslog(LOG_ERR, NOQID,
24640Sstevel@tonic-gate 							  "persistent queue runner control process: lost all children: wait returned ECHILD");
24650Sstevel@tonic-gate 						break;
24660Sstevel@tonic-gate 					}
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 					if (WIFSTOPPED(status))
24690Sstevel@tonic-gate 						continue;
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 					/* Probe only on a child status */
24720Sstevel@tonic-gate 					proc_list_drop(ret, status, &group);
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 					if (WIFSIGNALED(status))
24750Sstevel@tonic-gate 					{
24760Sstevel@tonic-gate 						if (WCOREDUMP(status))
24770Sstevel@tonic-gate 						{
24780Sstevel@tonic-gate 							sm_syslog(LOG_ERR, NOQID,
24790Sstevel@tonic-gate 								  "persistent queue runner=%d core dumped, signal=%d",
24800Sstevel@tonic-gate 								  group, WTERMSIG(status));
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 							/* don't restart this */
24830Sstevel@tonic-gate 							mark_work_group_restart(
24840Sstevel@tonic-gate 								group, -1);
24850Sstevel@tonic-gate 							continue;
24860Sstevel@tonic-gate 						}
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 						sm_syslog(LOG_ERR, NOQID,
24893544Sjbeck 							  "persistent queue runner=%d died, pid=%ld, signal=%d",
24903544Sjbeck 							  group, (long) ret,
24913544Sjbeck 							  WTERMSIG(status));
24920Sstevel@tonic-gate 					}
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate 					/*
24950Sstevel@tonic-gate 					**  When debugging active, don't
24960Sstevel@tonic-gate 					**  restart the persistent queues.
24970Sstevel@tonic-gate 					**  But do log this as info.
24980Sstevel@tonic-gate 					*/
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 					if (sm_debug_active(&DebugNoPRestart,
25010Sstevel@tonic-gate 							    1))
25020Sstevel@tonic-gate 					{
25030Sstevel@tonic-gate 						sm_syslog(LOG_DEBUG, NOQID,
25040Sstevel@tonic-gate 							  "persistent queue runner=%d, exited",
25050Sstevel@tonic-gate 							  group);
25060Sstevel@tonic-gate 						mark_work_group_restart(group,
25070Sstevel@tonic-gate 									-1);
25080Sstevel@tonic-gate 					}
25093544Sjbeck 					CHECK_RESTART;
25100Sstevel@tonic-gate 				}
25110Sstevel@tonic-gate 				finis(true, true, ExitStat);
25120Sstevel@tonic-gate 				/* NOTREACHED */
25130Sstevel@tonic-gate 			}
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 			if (OpMode != MD_DAEMON)
25160Sstevel@tonic-gate 			{
25170Sstevel@tonic-gate 				char qtype[200];
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 				/*
25200Sstevel@tonic-gate 				**  Write the pid to file
25210Sstevel@tonic-gate 				**  XXX Overwrites sendmail.pid
25220Sstevel@tonic-gate 				*/
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate 				log_sendmail_pid(&MainEnvelope);
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 				/* set the title to make it easier to find */
25270Sstevel@tonic-gate 				qtype[0] = '\0';
25283544Sjbeck 				(void) sm_strlcpyn(qtype, sizeof(qtype), 4,
25290Sstevel@tonic-gate 						   "Queue runner@",
25300Sstevel@tonic-gate 						   pintvl(QueueIntvl, true),
25310Sstevel@tonic-gate 						   " for ",
25320Sstevel@tonic-gate 						   QueueDir);
25330Sstevel@tonic-gate 				sm_setproctitle(true, CurEnv, qtype);
25340Sstevel@tonic-gate 				for (;;)
25350Sstevel@tonic-gate 				{
25360Sstevel@tonic-gate 					(void) pause();
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 					CHECK_RESTART;
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 					if (doqueuerun())
25410Sstevel@tonic-gate 						(void) runqueue(true, false,
25420Sstevel@tonic-gate 								false, false);
25430Sstevel@tonic-gate 				}
25440Sstevel@tonic-gate 			}
25450Sstevel@tonic-gate 		}
2546*11440SJohn.Beck@Sun.COM 		(void) dropenvelope(&MainEnvelope, true, false);
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate #if STARTTLS
25490Sstevel@tonic-gate 		/* init TLS for server, ignore result for now */
25500Sstevel@tonic-gate 		(void) initsrvtls(tls_ok);
25510Sstevel@tonic-gate #endif /* STARTTLS */
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	nextreq:
25540Sstevel@tonic-gate 		p_flags = getrequests(&MainEnvelope);
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate 		/* drop privileges */
25570Sstevel@tonic-gate 		(void) drop_privileges(false);
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 		/*
25600Sstevel@tonic-gate 		**  Get authentication data
25610Sstevel@tonic-gate 		**  Set _ macro in BlankEnvelope before calling newenvelope().
25620Sstevel@tonic-gate 		*/
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 		authinfo = getauthinfo(sm_io_getinfo(InChannel, SM_IO_WHAT_FD,
25650Sstevel@tonic-gate 						     NULL), &forged);
25660Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_TEMP, '_', authinfo);
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate 		/* at this point we are in a child: reset state */
25690Sstevel@tonic-gate 		sm_rpool_free(MainEnvelope.e_rpool);
25700Sstevel@tonic-gate 		(void) newenvelope(&MainEnvelope, &MainEnvelope,
25710Sstevel@tonic-gate 				   sm_rpool_new_x(NULL));
25720Sstevel@tonic-gate 	}
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 	if (LogLevel > 9)
25750Sstevel@tonic-gate 	{
25760Sstevel@tonic-gate 		/* log connection information */
25770Sstevel@tonic-gate 		sm_syslog(LOG_INFO, NULL, "connect from %s", authinfo);
25780Sstevel@tonic-gate 	}
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate 	/*
25810Sstevel@tonic-gate 	**  If running SMTP protocol, start collecting and executing
25820Sstevel@tonic-gate 	**  commands.  This will never return.
25830Sstevel@tonic-gate 	*/
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	if (OpMode == MD_SMTP || OpMode == MD_DAEMON)
25860Sstevel@tonic-gate 	{
25870Sstevel@tonic-gate 		char pbuf[20];
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 		/*
25900Sstevel@tonic-gate 		**  Save some macros for check_* rulesets.
25910Sstevel@tonic-gate 		*/
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 		if (forged)
25940Sstevel@tonic-gate 		{
25950Sstevel@tonic-gate 			char ipbuf[103];
25960Sstevel@tonic-gate 
25973544Sjbeck 			(void) sm_snprintf(ipbuf, sizeof(ipbuf), "[%.100s]",
25980Sstevel@tonic-gate 					   anynet_ntoa(&RealHostAddr));
25990Sstevel@tonic-gate 			macdefine(&BlankEnvelope.e_macro, A_TEMP,
26000Sstevel@tonic-gate 				  macid("{client_name}"), ipbuf);
26010Sstevel@tonic-gate 		}
26020Sstevel@tonic-gate 		else
26030Sstevel@tonic-gate 			macdefine(&BlankEnvelope.e_macro, A_PERM,
26040Sstevel@tonic-gate 				  macid("{client_name}"), RealHostName);
26050Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_PERM,
26060Sstevel@tonic-gate 			  macid("{client_ptr}"), RealHostName);
26070Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_TEMP,
26080Sstevel@tonic-gate 			  macid("{client_addr}"), anynet_ntoa(&RealHostAddr));
26090Sstevel@tonic-gate 		sm_getla();
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 		switch (RealHostAddr.sa.sa_family)
26120Sstevel@tonic-gate 		{
26130Sstevel@tonic-gate #if NETINET
26140Sstevel@tonic-gate 		  case AF_INET:
26153544Sjbeck 			(void) sm_snprintf(pbuf, sizeof(pbuf), "%d",
26160Sstevel@tonic-gate 					   RealHostAddr.sin.sin_port);
26170Sstevel@tonic-gate 			break;
26180Sstevel@tonic-gate #endif /* NETINET */
26190Sstevel@tonic-gate #if NETINET6
26200Sstevel@tonic-gate 		  case AF_INET6:
26213544Sjbeck 			(void) sm_snprintf(pbuf, sizeof(pbuf), "%d",
26220Sstevel@tonic-gate 					   RealHostAddr.sin6.sin6_port);
26230Sstevel@tonic-gate 			break;
26240Sstevel@tonic-gate #endif /* NETINET6 */
26250Sstevel@tonic-gate 		  default:
26263544Sjbeck 			(void) sm_snprintf(pbuf, sizeof(pbuf), "0");
26270Sstevel@tonic-gate 			break;
26280Sstevel@tonic-gate 		}
26290Sstevel@tonic-gate 		macdefine(&BlankEnvelope.e_macro, A_TEMP,
26300Sstevel@tonic-gate 			macid("{client_port}"), pbuf);
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate 		if (OpMode == MD_DAEMON)
26330Sstevel@tonic-gate 		{
26343544Sjbeck 			ENVELOPE *saved_env;
26353544Sjbeck 
26360Sstevel@tonic-gate 			/* validate the connection */
26370Sstevel@tonic-gate 			HoldErrs = true;
26383544Sjbeck 			saved_env = CurEnv;
26393544Sjbeck 			CurEnv = &BlankEnvelope;
26400Sstevel@tonic-gate 			nullserver = validate_connection(&RealHostAddr,
26410Sstevel@tonic-gate 						macvalue(macid("{client_name}"),
26423544Sjbeck 							&BlankEnvelope),
26433544Sjbeck 						&BlankEnvelope);
26443544Sjbeck 			if (bitset(EF_DISCARD, BlankEnvelope.e_flags))
26453544Sjbeck 				MainEnvelope.e_flags |= EF_DISCARD;
26463544Sjbeck 			CurEnv = saved_env;
26470Sstevel@tonic-gate 			HoldErrs = false;
26480Sstevel@tonic-gate 		}
26490Sstevel@tonic-gate 		else if (p_flags == NULL)
26500Sstevel@tonic-gate 		{
26513544Sjbeck 			p_flags = (BITMAP256 *) xalloc(sizeof(*p_flags));
26520Sstevel@tonic-gate 			clrbitmap(p_flags);
26530Sstevel@tonic-gate 		}
26540Sstevel@tonic-gate #if STARTTLS
26550Sstevel@tonic-gate 		if (OpMode == MD_SMTP)
26560Sstevel@tonic-gate 			(void) initsrvtls(tls_ok);
26570Sstevel@tonic-gate #endif /* STARTTLS */
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 		/* turn off profiling */
26600Sstevel@tonic-gate 		SM_PROF(1);
26610Sstevel@tonic-gate 		smtp(nullserver, *p_flags, &MainEnvelope);
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 		if (tTd(93, 100))
26640Sstevel@tonic-gate 		{
26650Sstevel@tonic-gate 			/* turn off profiling */
26660Sstevel@tonic-gate 			SM_PROF(0);
26670Sstevel@tonic-gate 			if (OpMode == MD_DAEMON)
26680Sstevel@tonic-gate 				goto nextreq;
26690Sstevel@tonic-gate 		}
26700Sstevel@tonic-gate 	}
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate 	sm_rpool_free(MainEnvelope.e_rpool);
26730Sstevel@tonic-gate 	clearenvelope(&MainEnvelope, false, sm_rpool_new_x(NULL));
26740Sstevel@tonic-gate 	if (OpMode == MD_VERIFY)
26750Sstevel@tonic-gate 	{
26760Sstevel@tonic-gate 		set_delivery_mode(SM_VERIFY, &MainEnvelope);
26770Sstevel@tonic-gate 		PostMasterCopy = NULL;
26780Sstevel@tonic-gate 	}
26790Sstevel@tonic-gate 	else
26800Sstevel@tonic-gate 	{
26810Sstevel@tonic-gate 		/* interactive -- all errors are global */
26820Sstevel@tonic-gate 		MainEnvelope.e_flags |= EF_GLOBALERRS|EF_LOGSENDER;
26830Sstevel@tonic-gate 	}
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 	/*
26860Sstevel@tonic-gate 	**  Do basic system initialization and set the sender
26870Sstevel@tonic-gate 	*/
26880Sstevel@tonic-gate 
26890Sstevel@tonic-gate 	initsys(&MainEnvelope);
26900Sstevel@tonic-gate 	macdefine(&MainEnvelope.e_macro, A_PERM, macid("{ntries}"), "0");
26910Sstevel@tonic-gate 	macdefine(&MainEnvelope.e_macro, A_PERM, macid("{nrcpts}"), "0");
26920Sstevel@tonic-gate 	setsender(from, &MainEnvelope, NULL, '\0', false);
26930Sstevel@tonic-gate 	if (warn_f_flag != '\0' && !wordinclass(RealUserName, 't') &&
26940Sstevel@tonic-gate 	    (!bitnset(M_LOCALMAILER, MainEnvelope.e_from.q_mailer->m_flags) ||
26950Sstevel@tonic-gate 	     strcmp(MainEnvelope.e_from.q_user, RealUserName) != 0))
26960Sstevel@tonic-gate 	{
26970Sstevel@tonic-gate 		auth_warning(&MainEnvelope, "%s set sender to %s using -%c",
26980Sstevel@tonic-gate 			     RealUserName, from, warn_f_flag);
26990Sstevel@tonic-gate #if SASL
27000Sstevel@tonic-gate 		auth = false;
27010Sstevel@tonic-gate #endif /* SASL */
27020Sstevel@tonic-gate 	}
27030Sstevel@tonic-gate 	if (auth)
27040Sstevel@tonic-gate 	{
27050Sstevel@tonic-gate 		char *fv;
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 		/* set the initial sender for AUTH= to $f@$j */
27080Sstevel@tonic-gate 		fv = macvalue('f', &MainEnvelope);
27090Sstevel@tonic-gate 		if (fv == NULL || *fv == '\0')
27100Sstevel@tonic-gate 			MainEnvelope.e_auth_param = NULL;
27110Sstevel@tonic-gate 		else
27120Sstevel@tonic-gate 		{
27130Sstevel@tonic-gate 			if (strchr(fv, '@') == NULL)
27140Sstevel@tonic-gate 			{
27150Sstevel@tonic-gate 				i = strlen(fv) + strlen(macvalue('j',
27160Sstevel@tonic-gate 							&MainEnvelope)) + 2;
27170Sstevel@tonic-gate 				p = sm_malloc_x(i);
27180Sstevel@tonic-gate 				(void) sm_strlcpyn(p, i, 3, fv, "@",
27190Sstevel@tonic-gate 						   macvalue('j',
27200Sstevel@tonic-gate 							    &MainEnvelope));
27210Sstevel@tonic-gate 			}
27220Sstevel@tonic-gate 			else
27230Sstevel@tonic-gate 				p = sm_strdup_x(fv);
27240Sstevel@tonic-gate 			MainEnvelope.e_auth_param = sm_rpool_strdup_x(MainEnvelope.e_rpool,
27250Sstevel@tonic-gate 								      xtextify(p, "="));
27260Sstevel@tonic-gate 			sm_free(p);  /* XXX */
27270Sstevel@tonic-gate 		}
27280Sstevel@tonic-gate 	}
27290Sstevel@tonic-gate 	if (macvalue('s', &MainEnvelope) == NULL)
27300Sstevel@tonic-gate 		macdefine(&MainEnvelope.e_macro, A_PERM, 's', RealHostName);
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	av = argv + optind;
27330Sstevel@tonic-gate 	if (*av == NULL && !GrabTo)
27340Sstevel@tonic-gate 	{
27350Sstevel@tonic-gate 		MainEnvelope.e_to = NULL;
27360Sstevel@tonic-gate 		MainEnvelope.e_flags |= EF_GLOBALERRS;
27370Sstevel@tonic-gate 		HoldErrs = false;
27380Sstevel@tonic-gate 		SuperSafe = SAFE_NO;
27390Sstevel@tonic-gate 		usrerr("Recipient names must be specified");
27400Sstevel@tonic-gate 
27410Sstevel@tonic-gate 		/* collect body for UUCP return */
27420Sstevel@tonic-gate 		if (OpMode != MD_VERIFY)
27430Sstevel@tonic-gate 			collect(InChannel, false, NULL, &MainEnvelope, true);
27440Sstevel@tonic-gate 		finis(true, true, EX_USAGE);
27450Sstevel@tonic-gate 		/* NOTREACHED */
27460Sstevel@tonic-gate 	}
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 	/*
27490Sstevel@tonic-gate 	**  Scan argv and deliver the message to everyone.
27500Sstevel@tonic-gate 	*/
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 	save_val = LogUsrErrs;
27530Sstevel@tonic-gate 	LogUsrErrs = true;
27540Sstevel@tonic-gate 	sendtoargv(av, &MainEnvelope);
27550Sstevel@tonic-gate 	LogUsrErrs = save_val;
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate 	/* if we have had errors sofar, arrange a meaningful exit stat */
27580Sstevel@tonic-gate 	if (Errors > 0 && ExitStat == EX_OK)
27590Sstevel@tonic-gate 		ExitStat = EX_USAGE;
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate #if _FFR_FIX_DASHT
27620Sstevel@tonic-gate 	/*
27630Sstevel@tonic-gate 	**  If using -t, force not sending to argv recipients, even
27640Sstevel@tonic-gate 	**  if they are mentioned in the headers.
27650Sstevel@tonic-gate 	*/
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 	if (GrabTo)
27680Sstevel@tonic-gate 	{
27690Sstevel@tonic-gate 		ADDRESS *q;
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 		for (q = MainEnvelope.e_sendqueue; q != NULL; q = q->q_next)
27720Sstevel@tonic-gate 			q->q_state = QS_REMOVED;
27730Sstevel@tonic-gate 	}
27740Sstevel@tonic-gate #endif /* _FFR_FIX_DASHT */
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 	/*
27770Sstevel@tonic-gate 	**  Read the input mail.
27780Sstevel@tonic-gate 	*/
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate 	MainEnvelope.e_to = NULL;
27810Sstevel@tonic-gate 	if (OpMode != MD_VERIFY || GrabTo)
27820Sstevel@tonic-gate 	{
27830Sstevel@tonic-gate 		int savederrors;
27840Sstevel@tonic-gate 		unsigned long savedflags;
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 		/*
27870Sstevel@tonic-gate 		**  workaround for compiler warning on Irix:
27880Sstevel@tonic-gate 		**  do not initialize variable in the definition, but
27890Sstevel@tonic-gate 		**  later on:
27900Sstevel@tonic-gate 		**  warning(1548): transfer of control bypasses
27910Sstevel@tonic-gate 		**  initialization of:
27920Sstevel@tonic-gate 		**  variable "savederrors" (declared at line 2570)
27930Sstevel@tonic-gate 		**  variable "savedflags" (declared at line 2571)
27940Sstevel@tonic-gate 		**  goto giveup;
27950Sstevel@tonic-gate 		*/
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate 		savederrors = Errors;
27980Sstevel@tonic-gate 		savedflags = MainEnvelope.e_flags & EF_FATALERRS;
27990Sstevel@tonic-gate 		MainEnvelope.e_flags |= EF_GLOBALERRS;
28000Sstevel@tonic-gate 		MainEnvelope.e_flags &= ~EF_FATALERRS;
28010Sstevel@tonic-gate 		Errors = 0;
28020Sstevel@tonic-gate 		buffer_errors();
28030Sstevel@tonic-gate 		collect(InChannel, false, NULL, &MainEnvelope, true);
28040Sstevel@tonic-gate 
28050Sstevel@tonic-gate 		/* header checks failed */
28060Sstevel@tonic-gate 		if (Errors > 0)
28070Sstevel@tonic-gate 		{
28080Sstevel@tonic-gate   giveup:
28090Sstevel@tonic-gate 			if (!GrabTo)
28100Sstevel@tonic-gate 			{
28110Sstevel@tonic-gate 				/* Log who the mail would have gone to */
28120Sstevel@tonic-gate 				logundelrcpts(&MainEnvelope,
28130Sstevel@tonic-gate 					      MainEnvelope.e_message,
28140Sstevel@tonic-gate 					      8, false);
28150Sstevel@tonic-gate 			}
28160Sstevel@tonic-gate 			flush_errors(true);
28170Sstevel@tonic-gate 			finis(true, true, ExitStat);
28180Sstevel@tonic-gate 			/* NOTREACHED */
28190Sstevel@tonic-gate 			return -1;
28200Sstevel@tonic-gate 		}
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate 		/* bail out if message too large */
28230Sstevel@tonic-gate 		if (bitset(EF_CLRQUEUE, MainEnvelope.e_flags))
28240Sstevel@tonic-gate 		{
28250Sstevel@tonic-gate 			finis(true, true, ExitStat != EX_OK ? ExitStat
28260Sstevel@tonic-gate 							    : EX_DATAERR);
28270Sstevel@tonic-gate 			/* NOTREACHED */
28280Sstevel@tonic-gate 			return -1;
28290Sstevel@tonic-gate 		}
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 		/* set message size */
28323544Sjbeck 		(void) sm_snprintf(buf, sizeof(buf), "%ld",
28330Sstevel@tonic-gate 				   MainEnvelope.e_msgsize);
28340Sstevel@tonic-gate 		macdefine(&MainEnvelope.e_macro, A_TEMP,
28350Sstevel@tonic-gate 			  macid("{msg_size}"), buf);
28360Sstevel@tonic-gate 
28370Sstevel@tonic-gate 		Errors = savederrors;
28380Sstevel@tonic-gate 		MainEnvelope.e_flags |= savedflags;
28390Sstevel@tonic-gate 	}
28400Sstevel@tonic-gate 	errno = 0;
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 	if (tTd(1, 1))
28430Sstevel@tonic-gate 		sm_dprintf("From person = \"%s\"\n",
28440Sstevel@tonic-gate 			   MainEnvelope.e_from.q_paddr);
28450Sstevel@tonic-gate 
28460Sstevel@tonic-gate 	/* Check if quarantining stats should be updated */
28470Sstevel@tonic-gate 	if (MainEnvelope.e_quarmsg != NULL)
28480Sstevel@tonic-gate 		markstats(&MainEnvelope, NULL, STATS_QUARANTINE);
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 	/*
28510Sstevel@tonic-gate 	**  Actually send everything.
28520Sstevel@tonic-gate 	**	If verifying, just ack.
28530Sstevel@tonic-gate 	*/
28540Sstevel@tonic-gate 
28550Sstevel@tonic-gate 	if (Errors == 0)
28560Sstevel@tonic-gate 	{
28570Sstevel@tonic-gate 		if (!split_by_recipient(&MainEnvelope) &&
28580Sstevel@tonic-gate 		    bitset(EF_FATALERRS, MainEnvelope.e_flags))
28590Sstevel@tonic-gate 			goto giveup;
28600Sstevel@tonic-gate 	}
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	/* make sure we deliver at least the first envelope */
28630Sstevel@tonic-gate 	i = FastSplit > 0 ? 0 : -1;
28640Sstevel@tonic-gate 	for (e = &MainEnvelope; e != NULL; e = e->e_sibling, i++)
28650Sstevel@tonic-gate 	{
28660Sstevel@tonic-gate 		ENVELOPE *next;
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 		e->e_from.q_state = QS_SENDER;
28690Sstevel@tonic-gate 		if (tTd(1, 5))
28700Sstevel@tonic-gate 		{
28710Sstevel@tonic-gate 			sm_dprintf("main[%d]: QS_SENDER ", i);
28720Sstevel@tonic-gate 			printaddr(sm_debug_file(), &e->e_from, false);
28730Sstevel@tonic-gate 		}
28740Sstevel@tonic-gate 		e->e_to = NULL;
28750Sstevel@tonic-gate 		sm_getla();
28760Sstevel@tonic-gate 		GrabTo = false;
28770Sstevel@tonic-gate #if NAMED_BIND
28780Sstevel@tonic-gate 		_res.retry = TimeOuts.res_retry[RES_TO_FIRST];
28790Sstevel@tonic-gate 		_res.retrans = TimeOuts.res_retrans[RES_TO_FIRST];
28800Sstevel@tonic-gate #endif /* NAMED_BIND */
28810Sstevel@tonic-gate 		next = e->e_sibling;
28820Sstevel@tonic-gate 		e->e_sibling = NULL;
28830Sstevel@tonic-gate 
28840Sstevel@tonic-gate 		/* after FastSplit envelopes: queue up */
28850Sstevel@tonic-gate 		sendall(e, i >= FastSplit ? SM_QUEUE : SM_DEFAULT);
28860Sstevel@tonic-gate 		e->e_sibling = next;
28870Sstevel@tonic-gate 	}
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 	/*
28900Sstevel@tonic-gate 	**  All done.
28910Sstevel@tonic-gate 	**	Don't send return error message if in VERIFY mode.
28920Sstevel@tonic-gate 	*/
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate 	finis(true, true, ExitStat);
28950Sstevel@tonic-gate 	/* NOTREACHED */
28960Sstevel@tonic-gate 	return ExitStat;
28970Sstevel@tonic-gate }
28980Sstevel@tonic-gate /*
28990Sstevel@tonic-gate **  STOP_SENDMAIL -- Stop the running program
29000Sstevel@tonic-gate **
29010Sstevel@tonic-gate **	Parameters:
29020Sstevel@tonic-gate **		none.
29030Sstevel@tonic-gate **
29040Sstevel@tonic-gate **	Returns:
29050Sstevel@tonic-gate **		none.
29060Sstevel@tonic-gate **
29070Sstevel@tonic-gate **	Side Effects:
29080Sstevel@tonic-gate **		exits.
29090Sstevel@tonic-gate */
29100Sstevel@tonic-gate 
29110Sstevel@tonic-gate void
stop_sendmail()29120Sstevel@tonic-gate stop_sendmail()
29130Sstevel@tonic-gate {
29140Sstevel@tonic-gate 	/* reset uid for process accounting */
29150Sstevel@tonic-gate 	endpwent();
29160Sstevel@tonic-gate 	(void) setuid(RealUid);
29170Sstevel@tonic-gate 	exit(EX_OK);
29180Sstevel@tonic-gate }
29190Sstevel@tonic-gate /*
29200Sstevel@tonic-gate **  FINIS -- Clean up and exit.
29210Sstevel@tonic-gate **
29220Sstevel@tonic-gate **	Parameters:
29230Sstevel@tonic-gate **		drop -- whether or not to drop CurEnv envelope
29240Sstevel@tonic-gate **		cleanup -- call exit() or _exit()?
29250Sstevel@tonic-gate **		exitstat -- exit status to use for exit() call
29260Sstevel@tonic-gate **
29270Sstevel@tonic-gate **	Returns:
29280Sstevel@tonic-gate **		never
29290Sstevel@tonic-gate **
29300Sstevel@tonic-gate **	Side Effects:
29310Sstevel@tonic-gate **		exits sendmail
29320Sstevel@tonic-gate */
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate void
finis(drop,cleanup,exitstat)29350Sstevel@tonic-gate finis(drop, cleanup, exitstat)
29360Sstevel@tonic-gate 	bool drop;
29370Sstevel@tonic-gate 	bool cleanup;
29380Sstevel@tonic-gate 	volatile int exitstat;
29390Sstevel@tonic-gate {
29400Sstevel@tonic-gate 	char pidpath[MAXPATHLEN];
29412197Sjbeck 	pid_t pid;
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 	/* Still want to process new timeouts added below */
29440Sstevel@tonic-gate 	sm_clear_events();
29450Sstevel@tonic-gate 	(void) sm_releasesignal(SIGALRM);
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	if (tTd(2, 1))
29480Sstevel@tonic-gate 	{
29490Sstevel@tonic-gate 		sm_dprintf("\n====finis: stat %d e_id=%s e_flags=",
29500Sstevel@tonic-gate 			   exitstat,
29510Sstevel@tonic-gate 			   CurEnv->e_id == NULL ? "NOQUEUE" : CurEnv->e_id);
29520Sstevel@tonic-gate 		printenvflags(CurEnv);
29530Sstevel@tonic-gate 	}
29540Sstevel@tonic-gate 	if (tTd(2, 9))
29550Sstevel@tonic-gate 		printopenfds(false);
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 	SM_TRY
29580Sstevel@tonic-gate 		/*
29590Sstevel@tonic-gate 		**  Clean up.  This might raise E:mta.quickabort
29600Sstevel@tonic-gate 		*/
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate 		/* clean up temp files */
29630Sstevel@tonic-gate 		CurEnv->e_to = NULL;
29640Sstevel@tonic-gate 		if (drop)
29650Sstevel@tonic-gate 		{
29660Sstevel@tonic-gate 			if (CurEnv->e_id != NULL)
29670Sstevel@tonic-gate 			{
2968*11440SJohn.Beck@Sun.COM 				int r;
2969*11440SJohn.Beck@Sun.COM 
2970*11440SJohn.Beck@Sun.COM 				r = dropenvelope(CurEnv, true, false);
2971*11440SJohn.Beck@Sun.COM 				if (exitstat == EX_OK)
2972*11440SJohn.Beck@Sun.COM 					exitstat = r;
29730Sstevel@tonic-gate 				sm_rpool_free(CurEnv->e_rpool);
29740Sstevel@tonic-gate 				CurEnv->e_rpool = NULL;
29752526Sjbeck 
29763544Sjbeck 				/* these may have pointed to the rpool */
29772526Sjbeck 				CurEnv->e_to = NULL;
29783544Sjbeck 				CurEnv->e_message = NULL;
29793544Sjbeck 				CurEnv->e_statmsg = NULL;
29803544Sjbeck 				CurEnv->e_quarmsg = NULL;
29813544Sjbeck 				CurEnv->e_bodytype = NULL;
29823544Sjbeck 				CurEnv->e_id = NULL;
29833544Sjbeck 				CurEnv->e_envid = NULL;
29843544Sjbeck 				CurEnv->e_auth_param = NULL;
29850Sstevel@tonic-gate 			}
29860Sstevel@tonic-gate 			else
29870Sstevel@tonic-gate 				poststats(StatFile);
29880Sstevel@tonic-gate 		}
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 		/* flush any cached connections */
29910Sstevel@tonic-gate 		mci_flush(true, NULL);
29920Sstevel@tonic-gate 
29930Sstevel@tonic-gate 		/* close maps belonging to this pid */
29940Sstevel@tonic-gate 		closemaps(false);
29950Sstevel@tonic-gate 
29960Sstevel@tonic-gate #if USERDB
29970Sstevel@tonic-gate 		/* close UserDatabase */
29980Sstevel@tonic-gate 		_udbx_close();
29990Sstevel@tonic-gate #endif /* USERDB */
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate #if SASL
30020Sstevel@tonic-gate 		stop_sasl_client();
30030Sstevel@tonic-gate #endif /* SASL */
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate #if XLA
30060Sstevel@tonic-gate 		/* clean up extended load average stuff */
30070Sstevel@tonic-gate 		xla_all_end();
30080Sstevel@tonic-gate #endif /* XLA */
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate 	SM_FINALLY
30110Sstevel@tonic-gate 		/*
30120Sstevel@tonic-gate 		**  And exit.
30130Sstevel@tonic-gate 		*/
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 		if (LogLevel > 78)
30160Sstevel@tonic-gate 			sm_syslog(LOG_DEBUG, CurEnv->e_id, "finis, pid=%d",
30170Sstevel@tonic-gate 				  (int) CurrentPid);
30180Sstevel@tonic-gate 		if (exitstat == EX_TEMPFAIL ||
30190Sstevel@tonic-gate 		    CurEnv->e_errormode == EM_BERKNET)
30200Sstevel@tonic-gate 			exitstat = EX_OK;
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate 		/* XXX clean up queues and related data structures */
30230Sstevel@tonic-gate 		cleanup_queues();
30242197Sjbeck 		pid = getpid();
30250Sstevel@tonic-gate #if SM_CONF_SHM
30262197Sjbeck 		cleanup_shm(DaemonPid == pid);
30270Sstevel@tonic-gate #endif /* SM_CONF_SHM */
30280Sstevel@tonic-gate 
30290Sstevel@tonic-gate 		/* close locked pid file */
30300Sstevel@tonic-gate 		close_sendmail_pid();
30310Sstevel@tonic-gate 
30322197Sjbeck 		if (DaemonPid == pid || PidFilePid == pid)
30330Sstevel@tonic-gate 		{
30340Sstevel@tonic-gate 			/* blow away the pid file */
30353544Sjbeck 			expand(PidFile, pidpath, sizeof(pidpath), CurEnv);
30360Sstevel@tonic-gate 			(void) unlink(pidpath);
30370Sstevel@tonic-gate 		}
30380Sstevel@tonic-gate 
30390Sstevel@tonic-gate 		/* reset uid for process accounting */
30400Sstevel@tonic-gate 		endpwent();
30410Sstevel@tonic-gate 		sm_mbdb_terminate();
30421658Sjbeck #if _FFR_MEMSTAT
30431658Sjbeck 		(void) sm_memstat_close();
30441658Sjbeck #endif /* _FFR_MEMSTAT */
30450Sstevel@tonic-gate 		(void) setuid(RealUid);
30460Sstevel@tonic-gate #if SM_HEAP_CHECK
30470Sstevel@tonic-gate 		/* dump the heap, if we are checking for memory leaks */
30480Sstevel@tonic-gate 		if (sm_debug_active(&SmHeapCheck, 2))
30490Sstevel@tonic-gate 			sm_heap_report(smioout,
30500Sstevel@tonic-gate 				       sm_debug_level(&SmHeapCheck) - 1);
30510Sstevel@tonic-gate #endif /* SM_HEAP_CHECK */
30520Sstevel@tonic-gate 		if (sm_debug_active(&SmXtrapReport, 1))
30530Sstevel@tonic-gate 			sm_dprintf("xtrap count = %d\n", SmXtrapCount);
30540Sstevel@tonic-gate 		if (cleanup)
30550Sstevel@tonic-gate 			exit(exitstat);
30560Sstevel@tonic-gate 		else
30570Sstevel@tonic-gate 			_exit(exitstat);
30580Sstevel@tonic-gate 	SM_END_TRY
30590Sstevel@tonic-gate }
30600Sstevel@tonic-gate /*
30610Sstevel@tonic-gate **  INTINDEBUG -- signal handler for SIGINT in -bt mode
30620Sstevel@tonic-gate **
30630Sstevel@tonic-gate **	Parameters:
30640Sstevel@tonic-gate **		sig -- incoming signal.
30650Sstevel@tonic-gate **
30660Sstevel@tonic-gate **	Returns:
30670Sstevel@tonic-gate **		none.
30680Sstevel@tonic-gate **
30690Sstevel@tonic-gate **	Side Effects:
30700Sstevel@tonic-gate **		longjmps back to test mode loop.
30710Sstevel@tonic-gate **
30720Sstevel@tonic-gate **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
30730Sstevel@tonic-gate **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
30740Sstevel@tonic-gate **		DOING.
30750Sstevel@tonic-gate */
30760Sstevel@tonic-gate 
30770Sstevel@tonic-gate /* Type of an exception generated on SIGINT during address test mode.  */
30780Sstevel@tonic-gate static const SM_EXC_TYPE_T EtypeInterrupt =
30790Sstevel@tonic-gate {
30800Sstevel@tonic-gate 	SmExcTypeMagic,
30810Sstevel@tonic-gate 	"S:mta.interrupt",
30820Sstevel@tonic-gate 	"",
30830Sstevel@tonic-gate 	sm_etype_printf,
30840Sstevel@tonic-gate 	"interrupt",
30850Sstevel@tonic-gate };
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate /* ARGSUSED */
30880Sstevel@tonic-gate static SIGFUNC_DECL
intindebug(sig)30890Sstevel@tonic-gate intindebug(sig)
30900Sstevel@tonic-gate 	int sig;
30910Sstevel@tonic-gate {
30920Sstevel@tonic-gate 	int save_errno = errno;
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	FIX_SYSV_SIGNAL(sig, intindebug);
30950Sstevel@tonic-gate 	errno = save_errno;
30960Sstevel@tonic-gate 	CHECK_CRITICAL(sig);
30970Sstevel@tonic-gate 	errno = save_errno;
30980Sstevel@tonic-gate 	sm_exc_raisenew_x(&EtypeInterrupt);
30990Sstevel@tonic-gate 	errno = save_errno;
31000Sstevel@tonic-gate 	return SIGFUNC_RETURN;
31010Sstevel@tonic-gate }
31020Sstevel@tonic-gate /*
31030Sstevel@tonic-gate **  SIGTERM -- SIGTERM handler for the daemon
31040Sstevel@tonic-gate **
31050Sstevel@tonic-gate **	Parameters:
31060Sstevel@tonic-gate **		sig -- signal number.
31070Sstevel@tonic-gate **
31080Sstevel@tonic-gate **	Returns:
31090Sstevel@tonic-gate **		none.
31100Sstevel@tonic-gate **
31110Sstevel@tonic-gate **	Side Effects:
31120Sstevel@tonic-gate **		Sets ShutdownRequest which will hopefully trigger
31130Sstevel@tonic-gate **		the daemon to exit.
31140Sstevel@tonic-gate **
31150Sstevel@tonic-gate **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
31160Sstevel@tonic-gate **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
31170Sstevel@tonic-gate **		DOING.
31180Sstevel@tonic-gate */
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate /* ARGSUSED */
31210Sstevel@tonic-gate static SIGFUNC_DECL
sigterm(sig)31220Sstevel@tonic-gate sigterm(sig)
31230Sstevel@tonic-gate 	int sig;
31240Sstevel@tonic-gate {
31250Sstevel@tonic-gate 	int save_errno = errno;
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 	FIX_SYSV_SIGNAL(sig, sigterm);
31280Sstevel@tonic-gate 	ShutdownRequest = "signal";
31290Sstevel@tonic-gate 	errno = save_errno;
31300Sstevel@tonic-gate 	return SIGFUNC_RETURN;
31310Sstevel@tonic-gate }
31320Sstevel@tonic-gate /*
31330Sstevel@tonic-gate **  SIGHUP -- handle a SIGHUP signal
31340Sstevel@tonic-gate **
31350Sstevel@tonic-gate **	Parameters:
31360Sstevel@tonic-gate **		sig -- incoming signal.
31370Sstevel@tonic-gate **
31380Sstevel@tonic-gate **	Returns:
31390Sstevel@tonic-gate **		none.
31400Sstevel@tonic-gate **
31410Sstevel@tonic-gate **	Side Effects:
31420Sstevel@tonic-gate **		Sets RestartRequest which should cause the daemon
31430Sstevel@tonic-gate **		to restart.
31440Sstevel@tonic-gate **
31450Sstevel@tonic-gate **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
31460Sstevel@tonic-gate **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
31470Sstevel@tonic-gate **		DOING.
31480Sstevel@tonic-gate */
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate /* ARGSUSED */
31510Sstevel@tonic-gate static SIGFUNC_DECL
sighup(sig)31520Sstevel@tonic-gate sighup(sig)
31530Sstevel@tonic-gate 	int sig;
31540Sstevel@tonic-gate {
31550Sstevel@tonic-gate 	int save_errno = errno;
31560Sstevel@tonic-gate 
31570Sstevel@tonic-gate 	FIX_SYSV_SIGNAL(sig, sighup);
31580Sstevel@tonic-gate 	RestartRequest = "signal";
31590Sstevel@tonic-gate 	errno = save_errno;
31600Sstevel@tonic-gate 	return SIGFUNC_RETURN;
31610Sstevel@tonic-gate }
31620Sstevel@tonic-gate /*
31630Sstevel@tonic-gate **  SIGPIPE -- signal handler for SIGPIPE
31640Sstevel@tonic-gate **
31650Sstevel@tonic-gate **	Parameters:
31660Sstevel@tonic-gate **		sig -- incoming signal.
31670Sstevel@tonic-gate **
31680Sstevel@tonic-gate **	Returns:
31690Sstevel@tonic-gate **		none.
31700Sstevel@tonic-gate **
31710Sstevel@tonic-gate **	Side Effects:
31720Sstevel@tonic-gate **		Sets StopRequest which should cause the mailq/hoststatus
31730Sstevel@tonic-gate **		display to stop.
31740Sstevel@tonic-gate **
31750Sstevel@tonic-gate **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
31760Sstevel@tonic-gate **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
31770Sstevel@tonic-gate **		DOING.
31780Sstevel@tonic-gate */
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate /* ARGSUSED */
31810Sstevel@tonic-gate static SIGFUNC_DECL
sigpipe(sig)31820Sstevel@tonic-gate sigpipe(sig)
31830Sstevel@tonic-gate 	int sig;
31840Sstevel@tonic-gate {
31850Sstevel@tonic-gate 	int save_errno = errno;
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate 	FIX_SYSV_SIGNAL(sig, sigpipe);
31880Sstevel@tonic-gate 	StopRequest = true;
31890Sstevel@tonic-gate 	errno = save_errno;
31900Sstevel@tonic-gate 	return SIGFUNC_RETURN;
31910Sstevel@tonic-gate }
31920Sstevel@tonic-gate /*
31930Sstevel@tonic-gate **  INTSIG -- clean up on interrupt
31940Sstevel@tonic-gate **
31950Sstevel@tonic-gate **	This just arranges to exit.  It pessimizes in that it
31960Sstevel@tonic-gate **	may resend a message.
31970Sstevel@tonic-gate **
31980Sstevel@tonic-gate **	Parameters:
31990Sstevel@tonic-gate **		none.
32000Sstevel@tonic-gate **
32010Sstevel@tonic-gate **	Returns:
32020Sstevel@tonic-gate **		none.
32030Sstevel@tonic-gate **
32040Sstevel@tonic-gate **	Side Effects:
32050Sstevel@tonic-gate **		Unlocks the current job.
32060Sstevel@tonic-gate **
32070Sstevel@tonic-gate **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
32080Sstevel@tonic-gate **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
32090Sstevel@tonic-gate **		DOING.
32100Sstevel@tonic-gate **
32110Sstevel@tonic-gate **		XXX: More work is needed for this signal handler.
32120Sstevel@tonic-gate */
32130Sstevel@tonic-gate 
32140Sstevel@tonic-gate /* ARGSUSED */
32150Sstevel@tonic-gate SIGFUNC_DECL
intsig(sig)32160Sstevel@tonic-gate intsig(sig)
32170Sstevel@tonic-gate 	int sig;
32180Sstevel@tonic-gate {
32190Sstevel@tonic-gate 	bool drop = false;
32200Sstevel@tonic-gate 	int save_errno = errno;
32210Sstevel@tonic-gate 
32220Sstevel@tonic-gate 	FIX_SYSV_SIGNAL(sig, intsig);
32230Sstevel@tonic-gate 	errno = save_errno;
32240Sstevel@tonic-gate 	CHECK_CRITICAL(sig);
32250Sstevel@tonic-gate 	sm_allsignals(true);
32260Sstevel@tonic-gate 
32270Sstevel@tonic-gate 	if (sig != 0 && LogLevel > 79)
32280Sstevel@tonic-gate 		sm_syslog(LOG_DEBUG, CurEnv->e_id, "interrupt");
32290Sstevel@tonic-gate 	FileName = NULL;
32300Sstevel@tonic-gate 
32310Sstevel@tonic-gate 	/* Clean-up on aborted stdin message submission */
32320Sstevel@tonic-gate 	if (CurEnv->e_id != NULL &&
32330Sstevel@tonic-gate 	    (OpMode == MD_SMTP ||
32340Sstevel@tonic-gate 	     OpMode == MD_DELIVER ||
32350Sstevel@tonic-gate 	     OpMode == MD_ARPAFTP))
32360Sstevel@tonic-gate 	{
32370Sstevel@tonic-gate 		register ADDRESS *q;
32380Sstevel@tonic-gate 
32390Sstevel@tonic-gate 		/* don't return an error indication */
32400Sstevel@tonic-gate 		CurEnv->e_to = NULL;
32410Sstevel@tonic-gate 		CurEnv->e_flags &= ~EF_FATALERRS;
32420Sstevel@tonic-gate 		CurEnv->e_flags |= EF_CLRQUEUE;
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate 		/*
32450Sstevel@tonic-gate 		**  Spin through the addresses and
32460Sstevel@tonic-gate 		**  mark them dead to prevent bounces
32470Sstevel@tonic-gate 		*/
32480Sstevel@tonic-gate 
32490Sstevel@tonic-gate 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
32500Sstevel@tonic-gate 			q->q_state = QS_DONTSEND;
32510Sstevel@tonic-gate 
32520Sstevel@tonic-gate 		drop = true;
32530Sstevel@tonic-gate 	}
32540Sstevel@tonic-gate 	else if (OpMode != MD_TEST)
32550Sstevel@tonic-gate 	{
32560Sstevel@tonic-gate 		unlockqueue(CurEnv);
32570Sstevel@tonic-gate 	}
32580Sstevel@tonic-gate 
32590Sstevel@tonic-gate 	finis(drop, false, EX_OK);
32600Sstevel@tonic-gate 	/* NOTREACHED */
32610Sstevel@tonic-gate }
32620Sstevel@tonic-gate /*
32630Sstevel@tonic-gate **  DISCONNECT -- remove our connection with any foreground process
32640Sstevel@tonic-gate **
32650Sstevel@tonic-gate **	Parameters:
32660Sstevel@tonic-gate **		droplev -- how "deeply" we should drop the line.
32670Sstevel@tonic-gate **			0 -- ignore signals, mail back errors, make sure
32680Sstevel@tonic-gate **			     output goes to stdout.
32690Sstevel@tonic-gate **			1 -- also, make stdout go to /dev/null.
32700Sstevel@tonic-gate **			2 -- also, disconnect from controlling terminal
32710Sstevel@tonic-gate **			     (only for daemon mode).
32720Sstevel@tonic-gate **		e -- the current envelope.
32730Sstevel@tonic-gate **
32740Sstevel@tonic-gate **	Returns:
32750Sstevel@tonic-gate **		none
32760Sstevel@tonic-gate **
32770Sstevel@tonic-gate **	Side Effects:
32780Sstevel@tonic-gate **		Trys to insure that we are immune to vagaries of
32790Sstevel@tonic-gate **		the controlling tty.
32800Sstevel@tonic-gate */
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate void
disconnect(droplev,e)32830Sstevel@tonic-gate disconnect(droplev, e)
32840Sstevel@tonic-gate 	int droplev;
32850Sstevel@tonic-gate 	register ENVELOPE *e;
32860Sstevel@tonic-gate {
32870Sstevel@tonic-gate 	int fd;
32880Sstevel@tonic-gate 
32890Sstevel@tonic-gate 	if (tTd(52, 1))
32900Sstevel@tonic-gate 		sm_dprintf("disconnect: In %d Out %d, e=%p\n",
32910Sstevel@tonic-gate 			   sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL),
32920Sstevel@tonic-gate 			   sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL), e);
32930Sstevel@tonic-gate 	if (tTd(52, 100))
32940Sstevel@tonic-gate 	{
32950Sstevel@tonic-gate 		sm_dprintf("don't\n");
32960Sstevel@tonic-gate 		return;
32970Sstevel@tonic-gate 	}
32980Sstevel@tonic-gate 	if (LogLevel > 93)
32990Sstevel@tonic-gate 		sm_syslog(LOG_DEBUG, e->e_id,
33000Sstevel@tonic-gate 			  "disconnect level %d",
33010Sstevel@tonic-gate 			  droplev);
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate 	/* be sure we don't get nasty signals */
33040Sstevel@tonic-gate 	(void) sm_signal(SIGINT, SIG_IGN);
33050Sstevel@tonic-gate 	(void) sm_signal(SIGQUIT, SIG_IGN);
33060Sstevel@tonic-gate 
33070Sstevel@tonic-gate 	/* we can't communicate with our caller, so.... */
33080Sstevel@tonic-gate 	HoldErrs = true;
33090Sstevel@tonic-gate 	CurEnv->e_errormode = EM_MAIL;
33100Sstevel@tonic-gate 	Verbose = 0;
33110Sstevel@tonic-gate 	DisConnected = true;
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 	/* all input from /dev/null */
33140Sstevel@tonic-gate 	if (InChannel != smioin)
33150Sstevel@tonic-gate 	{
33160Sstevel@tonic-gate 		(void) sm_io_close(InChannel, SM_TIME_DEFAULT);
33170Sstevel@tonic-gate 		InChannel = smioin;
33180Sstevel@tonic-gate 	}
33190Sstevel@tonic-gate 	if (sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL,
33200Sstevel@tonic-gate 			 SM_IO_RDONLY, NULL, smioin) == NULL)
33210Sstevel@tonic-gate 		sm_syslog(LOG_ERR, e->e_id,
33220Sstevel@tonic-gate 			  "disconnect: sm_io_reopen(\"%s\") failed: %s",
33230Sstevel@tonic-gate 			  SM_PATH_DEVNULL, sm_errstring(errno));
33240Sstevel@tonic-gate 
33250Sstevel@tonic-gate 	/*
33260Sstevel@tonic-gate 	**  output to the transcript
33270Sstevel@tonic-gate 	**	We also compare the fd numbers here since OutChannel
33280Sstevel@tonic-gate 	**	might be a layer on top of smioout due to encryption
33290Sstevel@tonic-gate 	**	(see sfsasl.c).
33300Sstevel@tonic-gate 	*/
33310Sstevel@tonic-gate 
33320Sstevel@tonic-gate 	if (OutChannel != smioout &&
33330Sstevel@tonic-gate 	    sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL) !=
33340Sstevel@tonic-gate 	    sm_io_getinfo(smioout, SM_IO_WHAT_FD, NULL))
33350Sstevel@tonic-gate 	{
33360Sstevel@tonic-gate 		(void) sm_io_close(OutChannel, SM_TIME_DEFAULT);
33370Sstevel@tonic-gate 		OutChannel = smioout;
33380Sstevel@tonic-gate 
33390Sstevel@tonic-gate #if 0
33400Sstevel@tonic-gate 		/*
33410Sstevel@tonic-gate 		**  Has smioout been closed? Reopen it.
33420Sstevel@tonic-gate 		**	This shouldn't happen anymore, the code is here
33430Sstevel@tonic-gate 		**	just as a reminder.
33440Sstevel@tonic-gate 		*/
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 		if (smioout->sm_magic == NULL &&
33470Sstevel@tonic-gate 		    sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL,
33480Sstevel@tonic-gate 				 SM_IO_WRONLY, NULL, smioout) == NULL)
33490Sstevel@tonic-gate 			sm_syslog(LOG_ERR, e->e_id,
33500Sstevel@tonic-gate 				  "disconnect: sm_io_reopen(\"%s\") failed: %s",
33510Sstevel@tonic-gate 				  SM_PATH_DEVNULL, sm_errstring(errno));
33520Sstevel@tonic-gate #endif /* 0 */
33530Sstevel@tonic-gate 	}
33540Sstevel@tonic-gate 	if (droplev > 0)
33550Sstevel@tonic-gate 	{
33560Sstevel@tonic-gate 		fd = open(SM_PATH_DEVNULL, O_WRONLY, 0666);
33570Sstevel@tonic-gate 		if (fd == -1)
33582197Sjbeck 		{
33590Sstevel@tonic-gate 			sm_syslog(LOG_ERR, e->e_id,
33600Sstevel@tonic-gate 				  "disconnect: open(\"%s\") failed: %s",
33610Sstevel@tonic-gate 				  SM_PATH_DEVNULL, sm_errstring(errno));
33622197Sjbeck 		}
33630Sstevel@tonic-gate 		(void) sm_io_flush(smioout, SM_TIME_DEFAULT);
33642197Sjbeck 		if (fd >= 0)
33652197Sjbeck 		{
33662197Sjbeck 			(void) dup2(fd, STDOUT_FILENO);
33672197Sjbeck 			(void) dup2(fd, STDERR_FILENO);
33682197Sjbeck 			(void) close(fd);
33692197Sjbeck 		}
33700Sstevel@tonic-gate 	}
33710Sstevel@tonic-gate 
33720Sstevel@tonic-gate 	/* drop our controlling TTY completely if possible */
33730Sstevel@tonic-gate 	if (droplev > 1)
33740Sstevel@tonic-gate 	{
33750Sstevel@tonic-gate 		(void) setsid();
33760Sstevel@tonic-gate 		errno = 0;
33770Sstevel@tonic-gate 	}
33780Sstevel@tonic-gate 
33790Sstevel@tonic-gate #if XDEBUG
33800Sstevel@tonic-gate 	checkfd012("disconnect");
33810Sstevel@tonic-gate #endif /* XDEBUG */
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate 	if (LogLevel > 71)
33840Sstevel@tonic-gate 		sm_syslog(LOG_DEBUG, e->e_id, "in background, pid=%d",
33850Sstevel@tonic-gate 			  (int) CurrentPid);
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate 	errno = 0;
33880Sstevel@tonic-gate }
33890Sstevel@tonic-gate 
33900Sstevel@tonic-gate static void
obsolete(argv)33910Sstevel@tonic-gate obsolete(argv)
33920Sstevel@tonic-gate 	char *argv[];
33930Sstevel@tonic-gate {
33940Sstevel@tonic-gate 	register char *ap;
33950Sstevel@tonic-gate 	register char *op;
33960Sstevel@tonic-gate 
33970Sstevel@tonic-gate 	while ((ap = *++argv) != NULL)
33980Sstevel@tonic-gate 	{
33990Sstevel@tonic-gate 		/* Return if "--" or not an option of any form. */
34000Sstevel@tonic-gate 		if (ap[0] != '-' || ap[1] == '-')
34010Sstevel@tonic-gate 			return;
34020Sstevel@tonic-gate 
34030Sstevel@tonic-gate 		/* Don't allow users to use "-Q." or "-Q ." */
34040Sstevel@tonic-gate 		if ((ap[1] == 'Q' && ap[2] == '.') ||
34050Sstevel@tonic-gate 		    (ap[1] == 'Q' && argv[1] != NULL &&
34060Sstevel@tonic-gate 		     argv[1][0] == '.' && argv[1][1] == '\0'))
34070Sstevel@tonic-gate 		{
34080Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
34090Sstevel@tonic-gate 					     "Can not use -Q.\n");
34100Sstevel@tonic-gate 			exit(EX_USAGE);
34110Sstevel@tonic-gate 		}
34120Sstevel@tonic-gate 
34130Sstevel@tonic-gate 		/* skip over options that do have a value */
34140Sstevel@tonic-gate 		op = strchr(OPTIONS, ap[1]);
34150Sstevel@tonic-gate 		if (op != NULL && *++op == ':' && ap[2] == '\0' &&
34160Sstevel@tonic-gate 		    ap[1] != 'd' &&
34170Sstevel@tonic-gate #if defined(sony_news)
34180Sstevel@tonic-gate 		    ap[1] != 'E' && ap[1] != 'J' &&
34190Sstevel@tonic-gate #endif /* defined(sony_news) */
34200Sstevel@tonic-gate 		    argv[1] != NULL && argv[1][0] != '-')
34210Sstevel@tonic-gate 		{
34220Sstevel@tonic-gate 			argv++;
34230Sstevel@tonic-gate 			continue;
34240Sstevel@tonic-gate 		}
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 		/* If -C doesn't have an argument, use sendmail.cf. */
34270Sstevel@tonic-gate #define __DEFPATH	"sendmail.cf"
34280Sstevel@tonic-gate 		if (ap[1] == 'C' && ap[2] == '\0')
34290Sstevel@tonic-gate 		{
34300Sstevel@tonic-gate 			*argv = xalloc(sizeof(__DEFPATH) + 2);
34310Sstevel@tonic-gate 			(void) sm_strlcpyn(argv[0], sizeof(__DEFPATH) + 2, 2,
34320Sstevel@tonic-gate 					   "-C", __DEFPATH);
34330Sstevel@tonic-gate 		}
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 		/* If -q doesn't have an argument, run it once. */
34360Sstevel@tonic-gate 		if (ap[1] == 'q' && ap[2] == '\0')
34370Sstevel@tonic-gate 			*argv = "-q0";
34380Sstevel@tonic-gate 
34390Sstevel@tonic-gate 		/* If -Q doesn't have an argument, disable quarantining */
34400Sstevel@tonic-gate 		if (ap[1] == 'Q' && ap[2] == '\0')
34410Sstevel@tonic-gate 			*argv = "-Q.";
34420Sstevel@tonic-gate 
34430Sstevel@tonic-gate 		/* if -d doesn't have an argument, use 0-99.1 */
34440Sstevel@tonic-gate 		if (ap[1] == 'd' && ap[2] == '\0')
34450Sstevel@tonic-gate 			*argv = "-d0-99.1";
34460Sstevel@tonic-gate 
34470Sstevel@tonic-gate #if defined(sony_news)
34480Sstevel@tonic-gate 		/* if -E doesn't have an argument, use -EC */
34490Sstevel@tonic-gate 		if (ap[1] == 'E' && ap[2] == '\0')
34500Sstevel@tonic-gate 			*argv = "-EC";
34510Sstevel@tonic-gate 
34520Sstevel@tonic-gate 		/* if -J doesn't have an argument, use -JJ */
34530Sstevel@tonic-gate 		if (ap[1] == 'J' && ap[2] == '\0')
34540Sstevel@tonic-gate 			*argv = "-JJ";
34550Sstevel@tonic-gate #endif /* defined(sony_news) */
34560Sstevel@tonic-gate 	}
34570Sstevel@tonic-gate }
34580Sstevel@tonic-gate /*
34590Sstevel@tonic-gate **  AUTH_WARNING -- specify authorization warning
34600Sstevel@tonic-gate **
34610Sstevel@tonic-gate **	Parameters:
34620Sstevel@tonic-gate **		e -- the current envelope.
34630Sstevel@tonic-gate **		msg -- the text of the message.
34640Sstevel@tonic-gate **		args -- arguments to the message.
34650Sstevel@tonic-gate **
34660Sstevel@tonic-gate **	Returns:
34670Sstevel@tonic-gate **		none.
34680Sstevel@tonic-gate */
34690Sstevel@tonic-gate 
34700Sstevel@tonic-gate void
34710Sstevel@tonic-gate #ifdef __STDC__
auth_warning(register ENVELOPE * e,const char * msg,...)34720Sstevel@tonic-gate auth_warning(register ENVELOPE *e, const char *msg, ...)
34730Sstevel@tonic-gate #else /* __STDC__ */
34740Sstevel@tonic-gate auth_warning(e, msg, va_alist)
34750Sstevel@tonic-gate 	register ENVELOPE *e;
34760Sstevel@tonic-gate 	const char *msg;
34770Sstevel@tonic-gate 	va_dcl
34780Sstevel@tonic-gate #endif /* __STDC__ */
34790Sstevel@tonic-gate {
34800Sstevel@tonic-gate 	char buf[MAXLINE];
34810Sstevel@tonic-gate 	SM_VA_LOCAL_DECL
34820Sstevel@tonic-gate 
34830Sstevel@tonic-gate 	if (bitset(PRIV_AUTHWARNINGS, PrivacyFlags))
34840Sstevel@tonic-gate 	{
34850Sstevel@tonic-gate 		register char *p;
34860Sstevel@tonic-gate 		static char hostbuf[48];
34870Sstevel@tonic-gate 
34880Sstevel@tonic-gate 		if (hostbuf[0] == '\0')
34890Sstevel@tonic-gate 		{
34900Sstevel@tonic-gate 			struct hostent *hp;
34910Sstevel@tonic-gate 
34923544Sjbeck 			hp = myhostname(hostbuf, sizeof(hostbuf));
34930Sstevel@tonic-gate #if NETINET6
34940Sstevel@tonic-gate 			if (hp != NULL)
34950Sstevel@tonic-gate 			{
34960Sstevel@tonic-gate 				freehostent(hp);
34970Sstevel@tonic-gate 				hp = NULL;
34980Sstevel@tonic-gate 			}
34990Sstevel@tonic-gate #endif /* NETINET6 */
35000Sstevel@tonic-gate 		}
35010Sstevel@tonic-gate 
35023544Sjbeck 		(void) sm_strlcpyn(buf, sizeof(buf), 2, hostbuf, ": ");
35030Sstevel@tonic-gate 		p = &buf[strlen(buf)];
35040Sstevel@tonic-gate 		SM_VA_START(ap, msg);
35050Sstevel@tonic-gate 		(void) sm_vsnprintf(p, SPACELEFT(buf, p), msg, ap);
35060Sstevel@tonic-gate 		SM_VA_END(ap);
35073544Sjbeck 		addheader("X-Authentication-Warning", buf, 0, e, true);
35080Sstevel@tonic-gate 		if (LogLevel > 3)
35090Sstevel@tonic-gate 			sm_syslog(LOG_INFO, e->e_id,
35100Sstevel@tonic-gate 				  "Authentication-Warning: %.400s",
35110Sstevel@tonic-gate 				  buf);
35120Sstevel@tonic-gate 	}
35130Sstevel@tonic-gate }
35140Sstevel@tonic-gate /*
35150Sstevel@tonic-gate **  GETEXTENV -- get from external environment
35160Sstevel@tonic-gate **
35170Sstevel@tonic-gate **	Parameters:
35180Sstevel@tonic-gate **		envar -- the name of the variable to retrieve
35190Sstevel@tonic-gate **
35200Sstevel@tonic-gate **	Returns:
35210Sstevel@tonic-gate **		The value, if any.
35220Sstevel@tonic-gate */
35230Sstevel@tonic-gate 
35240Sstevel@tonic-gate static char *
getextenv(envar)35250Sstevel@tonic-gate getextenv(envar)
35260Sstevel@tonic-gate 	const char *envar;
35270Sstevel@tonic-gate {
35280Sstevel@tonic-gate 	char **envp;
35290Sstevel@tonic-gate 	int l;
35300Sstevel@tonic-gate 
35310Sstevel@tonic-gate 	l = strlen(envar);
35320Sstevel@tonic-gate 	for (envp = ExternalEnviron; envp != NULL && *envp != NULL; envp++)
35330Sstevel@tonic-gate 	{
35340Sstevel@tonic-gate 		if (strncmp(*envp, envar, l) == 0 && (*envp)[l] == '=')
35350Sstevel@tonic-gate 			return &(*envp)[l + 1];
35360Sstevel@tonic-gate 	}
35370Sstevel@tonic-gate 	return NULL;
35380Sstevel@tonic-gate }
35390Sstevel@tonic-gate /*
35401658Sjbeck **  SM_SETUSERENV -- set an environment variable in the propagated environment
35410Sstevel@tonic-gate **
35420Sstevel@tonic-gate **	Parameters:
35430Sstevel@tonic-gate **		envar -- the name of the environment variable.
35440Sstevel@tonic-gate **		value -- the value to which it should be set.  If
35450Sstevel@tonic-gate **			null, this is extracted from the incoming
35460Sstevel@tonic-gate **			environment.  If that is not set, the call
35471658Sjbeck **			to sm_setuserenv is ignored.
35480Sstevel@tonic-gate **
35490Sstevel@tonic-gate **	Returns:
35500Sstevel@tonic-gate **		none.
35510Sstevel@tonic-gate */
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate void
sm_setuserenv(envar,value)35541658Sjbeck sm_setuserenv(envar, value)
35550Sstevel@tonic-gate 	const char *envar;
35560Sstevel@tonic-gate 	const char *value;
35570Sstevel@tonic-gate {
35580Sstevel@tonic-gate 	int i, l;
35590Sstevel@tonic-gate 	char **evp = UserEnviron;
35600Sstevel@tonic-gate 	char *p;
35610Sstevel@tonic-gate 
35620Sstevel@tonic-gate 	if (value == NULL)
35630Sstevel@tonic-gate 	{
35640Sstevel@tonic-gate 		value = getextenv(envar);
35650Sstevel@tonic-gate 		if (value == NULL)
35660Sstevel@tonic-gate 			return;
35670Sstevel@tonic-gate 	}
35680Sstevel@tonic-gate 
35690Sstevel@tonic-gate 	/* XXX enforce reasonable size? */
35700Sstevel@tonic-gate 	i = strlen(envar) + 1;
35710Sstevel@tonic-gate 	l = strlen(value) + i + 1;
35720Sstevel@tonic-gate 	p = (char *) xalloc(l);
35730Sstevel@tonic-gate 	(void) sm_strlcpyn(p, l, 3, envar, "=", value);
35740Sstevel@tonic-gate 
35750Sstevel@tonic-gate 	while (*evp != NULL && strncmp(*evp, p, i) != 0)
35760Sstevel@tonic-gate 		evp++;
35770Sstevel@tonic-gate 	if (*evp != NULL)
35780Sstevel@tonic-gate 	{
35790Sstevel@tonic-gate 		*evp++ = p;
35800Sstevel@tonic-gate 	}
35810Sstevel@tonic-gate 	else if (evp < &UserEnviron[MAXUSERENVIRON])
35820Sstevel@tonic-gate 	{
35830Sstevel@tonic-gate 		*evp++ = p;
35840Sstevel@tonic-gate 		*evp = NULL;
35850Sstevel@tonic-gate 	}
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 	/* make sure it is in our environment as well */
35880Sstevel@tonic-gate 	if (putenv(p) < 0)
35891658Sjbeck 		syserr("sm_setuserenv: putenv(%s) failed", p);
35900Sstevel@tonic-gate }
35910Sstevel@tonic-gate /*
35920Sstevel@tonic-gate **  DUMPSTATE -- dump state
35930Sstevel@tonic-gate **
35940Sstevel@tonic-gate **	For debugging.
35950Sstevel@tonic-gate */
35960Sstevel@tonic-gate 
35970Sstevel@tonic-gate void
dumpstate(when)35980Sstevel@tonic-gate dumpstate(when)
35990Sstevel@tonic-gate 	char *when;
36000Sstevel@tonic-gate {
36010Sstevel@tonic-gate 	register char *j = macvalue('j', CurEnv);
36020Sstevel@tonic-gate 	int rs;
36030Sstevel@tonic-gate 	extern int NextMacroId;
36040Sstevel@tonic-gate 
36050Sstevel@tonic-gate 	sm_syslog(LOG_DEBUG, CurEnv->e_id,
36060Sstevel@tonic-gate 		  "--- dumping state on %s: $j = %s ---",
36070Sstevel@tonic-gate 		  when,
36080Sstevel@tonic-gate 		  j == NULL ? "<NULL>" : j);
36090Sstevel@tonic-gate 	if (j != NULL)
36100Sstevel@tonic-gate 	{
36110Sstevel@tonic-gate 		if (!wordinclass(j, 'w'))
36120Sstevel@tonic-gate 			sm_syslog(LOG_DEBUG, CurEnv->e_id,
36130Sstevel@tonic-gate 				  "*** $j not in $=w ***");
36140Sstevel@tonic-gate 	}
36150Sstevel@tonic-gate 	sm_syslog(LOG_DEBUG, CurEnv->e_id, "CurChildren = %d", CurChildren);
36160Sstevel@tonic-gate 	sm_syslog(LOG_DEBUG, CurEnv->e_id, "NextMacroId = %d (Max %d)",
36170Sstevel@tonic-gate 		  NextMacroId, MAXMACROID);
36180Sstevel@tonic-gate 	sm_syslog(LOG_DEBUG, CurEnv->e_id, "--- open file descriptors: ---");
36190Sstevel@tonic-gate 	printopenfds(true);
36200Sstevel@tonic-gate 	sm_syslog(LOG_DEBUG, CurEnv->e_id, "--- connection cache: ---");
36210Sstevel@tonic-gate 	mci_dump_all(smioout, true);
36220Sstevel@tonic-gate 	rs = strtorwset("debug_dumpstate", NULL, ST_FIND);
36230Sstevel@tonic-gate 	if (rs > 0)
36240Sstevel@tonic-gate 	{
36250Sstevel@tonic-gate 		int status;
36260Sstevel@tonic-gate 		register char **pvp;
36270Sstevel@tonic-gate 		char *pv[MAXATOM + 1];
36280Sstevel@tonic-gate 
36290Sstevel@tonic-gate 		pv[0] = NULL;
36300Sstevel@tonic-gate 		status = REWRITE(pv, rs, CurEnv);
36310Sstevel@tonic-gate 		sm_syslog(LOG_DEBUG, CurEnv->e_id,
36320Sstevel@tonic-gate 			  "--- ruleset debug_dumpstate returns stat %d, pv: ---",
36330Sstevel@tonic-gate 			  status);
36340Sstevel@tonic-gate 		for (pvp = pv; *pvp != NULL; pvp++)
36350Sstevel@tonic-gate 			sm_syslog(LOG_DEBUG, CurEnv->e_id, "%s", *pvp);
36360Sstevel@tonic-gate 	}
36370Sstevel@tonic-gate 	sm_syslog(LOG_DEBUG, CurEnv->e_id, "--- end of state dump ---");
36380Sstevel@tonic-gate }
36390Sstevel@tonic-gate 
36400Sstevel@tonic-gate #ifdef SIGUSR1
36410Sstevel@tonic-gate /*
36420Sstevel@tonic-gate **  SIGUSR1 -- Signal a request to dump state.
36430Sstevel@tonic-gate **
36440Sstevel@tonic-gate **	Parameters:
36450Sstevel@tonic-gate **		sig -- calling signal.
36460Sstevel@tonic-gate **
36470Sstevel@tonic-gate **	Returns:
36480Sstevel@tonic-gate **		none.
36490Sstevel@tonic-gate **
36500Sstevel@tonic-gate **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
36510Sstevel@tonic-gate **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
36520Sstevel@tonic-gate **		DOING.
36530Sstevel@tonic-gate **
36540Sstevel@tonic-gate **		XXX: More work is needed for this signal handler.
36550Sstevel@tonic-gate */
36560Sstevel@tonic-gate 
36570Sstevel@tonic-gate /* ARGSUSED */
36580Sstevel@tonic-gate static SIGFUNC_DECL
sigusr1(sig)36590Sstevel@tonic-gate sigusr1(sig)
36600Sstevel@tonic-gate 	int sig;
36610Sstevel@tonic-gate {
36620Sstevel@tonic-gate 	int save_errno = errno;
36630Sstevel@tonic-gate 
36640Sstevel@tonic-gate 	FIX_SYSV_SIGNAL(sig, sigusr1);
36650Sstevel@tonic-gate 	errno = save_errno;
36660Sstevel@tonic-gate 	CHECK_CRITICAL(sig);
36670Sstevel@tonic-gate 	dumpstate("user signal");
36680Sstevel@tonic-gate # if SM_HEAP_CHECK
36690Sstevel@tonic-gate 	dumpstab();
36700Sstevel@tonic-gate # endif /* SM_HEAP_CHECK */
36710Sstevel@tonic-gate 	errno = save_errno;
36720Sstevel@tonic-gate 	return SIGFUNC_RETURN;
36730Sstevel@tonic-gate }
36740Sstevel@tonic-gate #endif /* SIGUSR1 */
36750Sstevel@tonic-gate 
36760Sstevel@tonic-gate /*
36770Sstevel@tonic-gate **  DROP_PRIVILEGES -- reduce privileges to those of the RunAsUser option
36780Sstevel@tonic-gate **
36790Sstevel@tonic-gate **	Parameters:
36800Sstevel@tonic-gate **		to_real_uid -- if set, drop to the real uid instead
36810Sstevel@tonic-gate **			of the RunAsUser.
36820Sstevel@tonic-gate **
36830Sstevel@tonic-gate **	Returns:
36840Sstevel@tonic-gate **		EX_OSERR if the setuid failed.
36850Sstevel@tonic-gate **		EX_OK otherwise.
36860Sstevel@tonic-gate */
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate int
drop_privileges(to_real_uid)36890Sstevel@tonic-gate drop_privileges(to_real_uid)
36900Sstevel@tonic-gate 	bool to_real_uid;
36910Sstevel@tonic-gate {
36920Sstevel@tonic-gate 	int rval = EX_OK;
36930Sstevel@tonic-gate 	GIDSET_T emptygidset[1];
36940Sstevel@tonic-gate 
36950Sstevel@tonic-gate 	if (tTd(47, 1))
36960Sstevel@tonic-gate 		sm_dprintf("drop_privileges(%d): Real[UG]id=%d:%d, get[ug]id=%d:%d, gete[ug]id=%d:%d, RunAs[UG]id=%d:%d\n",
36970Sstevel@tonic-gate 			   (int) to_real_uid,
36980Sstevel@tonic-gate 			   (int) RealUid, (int) RealGid,
36990Sstevel@tonic-gate 			   (int) getuid(), (int) getgid(),
37000Sstevel@tonic-gate 			   (int) geteuid(), (int) getegid(),
37010Sstevel@tonic-gate 			   (int) RunAsUid, (int) RunAsGid);
37020Sstevel@tonic-gate 
37030Sstevel@tonic-gate 	if (to_real_uid)
37040Sstevel@tonic-gate 	{
37050Sstevel@tonic-gate 		RunAsUserName = RealUserName;
37060Sstevel@tonic-gate 		RunAsUid = RealUid;
37070Sstevel@tonic-gate 		RunAsGid = RealGid;
37080Sstevel@tonic-gate 		EffGid = RunAsGid;
37090Sstevel@tonic-gate 	}
37100Sstevel@tonic-gate 
37110Sstevel@tonic-gate 	/* make sure no one can grab open descriptors for secret files */
37120Sstevel@tonic-gate 	endpwent();
37130Sstevel@tonic-gate 	sm_mbdb_terminate();
37140Sstevel@tonic-gate 
37150Sstevel@tonic-gate 	/* reset group permissions; these can be set later */
37160Sstevel@tonic-gate 	emptygidset[0] = (to_real_uid || RunAsGid != 0) ? RunAsGid : getegid();
37170Sstevel@tonic-gate 
37180Sstevel@tonic-gate 	/*
37190Sstevel@tonic-gate 	**  Notice:  on some OS (Linux...) the setgroups() call causes
37200Sstevel@tonic-gate 	**	a logfile entry if sendmail is not run by root.
37210Sstevel@tonic-gate 	**	However, it is unclear (no POSIX standard) whether
37220Sstevel@tonic-gate 	**	setgroups() can only succeed if executed by root.
37230Sstevel@tonic-gate 	**	So for now we keep it as it is; if you want to change it, use
37240Sstevel@tonic-gate 	**  if (geteuid() == 0 && setgroups(1, emptygidset) == -1)
37250Sstevel@tonic-gate 	*/
37260Sstevel@tonic-gate 
37270Sstevel@tonic-gate 	if (setgroups(1, emptygidset) == -1 && geteuid() == 0)
37280Sstevel@tonic-gate 	{
37290Sstevel@tonic-gate 		syserr("drop_privileges: setgroups(1, %d) failed",
37300Sstevel@tonic-gate 		       (int) emptygidset[0]);
37310Sstevel@tonic-gate 		rval = EX_OSERR;
37320Sstevel@tonic-gate 	}
37330Sstevel@tonic-gate 
37340Sstevel@tonic-gate 	/* reset primary group id */
37350Sstevel@tonic-gate 	if (to_real_uid)
37360Sstevel@tonic-gate 	{
37370Sstevel@tonic-gate 		/*
37380Sstevel@tonic-gate 		**  Drop gid to real gid.
37390Sstevel@tonic-gate 		**  On some OS we must reset the effective[/real[/saved]] gid,
37400Sstevel@tonic-gate 		**  and then use setgid() to finally drop all group privileges.
37410Sstevel@tonic-gate 		**  Later on we check whether we can get back the
37420Sstevel@tonic-gate 		**  effective gid.
37430Sstevel@tonic-gate 		*/
37440Sstevel@tonic-gate 
37450Sstevel@tonic-gate #if HASSETEGID
37460Sstevel@tonic-gate 		if (setegid(RunAsGid) < 0)
37470Sstevel@tonic-gate 		{
37480Sstevel@tonic-gate 			syserr("drop_privileges: setegid(%d) failed",
37490Sstevel@tonic-gate 			       (int) RunAsGid);
37500Sstevel@tonic-gate 			rval = EX_OSERR;
37510Sstevel@tonic-gate 		}
37520Sstevel@tonic-gate #else /* HASSETEGID */
37530Sstevel@tonic-gate # if HASSETREGID
37540Sstevel@tonic-gate 		if (setregid(RunAsGid, RunAsGid) < 0)
37550Sstevel@tonic-gate 		{
37560Sstevel@tonic-gate 			syserr("drop_privileges: setregid(%d, %d) failed",
37570Sstevel@tonic-gate 			       (int) RunAsGid, (int) RunAsGid);
37580Sstevel@tonic-gate 			rval = EX_OSERR;
37590Sstevel@tonic-gate 		}
37600Sstevel@tonic-gate # else /* HASSETREGID */
37610Sstevel@tonic-gate #  if HASSETRESGID
37620Sstevel@tonic-gate 		if (setresgid(RunAsGid, RunAsGid, RunAsGid) < 0)
37630Sstevel@tonic-gate 		{
37640Sstevel@tonic-gate 			syserr("drop_privileges: setresgid(%d, %d, %d) failed",
37650Sstevel@tonic-gate 			       (int) RunAsGid, (int) RunAsGid, (int) RunAsGid);
37660Sstevel@tonic-gate 			rval = EX_OSERR;
37670Sstevel@tonic-gate 		}
37680Sstevel@tonic-gate #  endif /* HASSETRESGID */
37690Sstevel@tonic-gate # endif /* HASSETREGID */
37700Sstevel@tonic-gate #endif /* HASSETEGID */
37710Sstevel@tonic-gate 	}
37720Sstevel@tonic-gate 	if (rval == EX_OK && (to_real_uid || RunAsGid != 0))
37730Sstevel@tonic-gate 	{
37740Sstevel@tonic-gate 		if (setgid(RunAsGid) < 0 && (!UseMSP || getegid() != RunAsGid))
37750Sstevel@tonic-gate 		{
37760Sstevel@tonic-gate 			syserr("drop_privileges: setgid(%d) failed",
37770Sstevel@tonic-gate 			       (int) RunAsGid);
37780Sstevel@tonic-gate 			rval = EX_OSERR;
37790Sstevel@tonic-gate 		}
37800Sstevel@tonic-gate 		errno = 0;
37810Sstevel@tonic-gate 		if (rval == EX_OK && getegid() != RunAsGid)
37820Sstevel@tonic-gate 		{
37830Sstevel@tonic-gate 			syserr("drop_privileges: Unable to set effective gid=%d to RunAsGid=%d",
37840Sstevel@tonic-gate 			       (int) getegid(), (int) RunAsGid);
37850Sstevel@tonic-gate 			rval = EX_OSERR;
37860Sstevel@tonic-gate 		}
37870Sstevel@tonic-gate 	}
37880Sstevel@tonic-gate 
37890Sstevel@tonic-gate 	/* fiddle with uid */
37900Sstevel@tonic-gate 	if (to_real_uid || RunAsUid != 0)
37910Sstevel@tonic-gate 	{
37920Sstevel@tonic-gate 		uid_t euid;
37930Sstevel@tonic-gate 
37940Sstevel@tonic-gate 		/*
37950Sstevel@tonic-gate 		**  Try to setuid(RunAsUid).
37960Sstevel@tonic-gate 		**  euid must be RunAsUid,
37970Sstevel@tonic-gate 		**  ruid must be RunAsUid unless (e|r)uid wasn't 0
37980Sstevel@tonic-gate 		**	and we didn't have to drop privileges to the real uid.
37990Sstevel@tonic-gate 		*/
38000Sstevel@tonic-gate 
38010Sstevel@tonic-gate 		if (setuid(RunAsUid) < 0 ||
38020Sstevel@tonic-gate 		    geteuid() != RunAsUid ||
38030Sstevel@tonic-gate 		    (getuid() != RunAsUid &&
38040Sstevel@tonic-gate 		     (to_real_uid || geteuid() == 0 || getuid() == 0)))
38050Sstevel@tonic-gate 		{
38060Sstevel@tonic-gate #if HASSETREUID
38070Sstevel@tonic-gate 			/*
38080Sstevel@tonic-gate 			**  if ruid != RunAsUid, euid == RunAsUid, then
38090Sstevel@tonic-gate 			**  try resetting just the real uid, then using
38100Sstevel@tonic-gate 			**  setuid() to drop the saved-uid as well.
38110Sstevel@tonic-gate 			*/
38120Sstevel@tonic-gate 
38130Sstevel@tonic-gate 			if (geteuid() == RunAsUid)
38140Sstevel@tonic-gate 			{
38150Sstevel@tonic-gate 				if (setreuid(RunAsUid, -1) < 0)
38160Sstevel@tonic-gate 				{
38170Sstevel@tonic-gate 					syserr("drop_privileges: setreuid(%d, -1) failed",
38180Sstevel@tonic-gate 					       (int) RunAsUid);
38190Sstevel@tonic-gate 					rval = EX_OSERR;
38200Sstevel@tonic-gate 				}
38210Sstevel@tonic-gate 				if (setuid(RunAsUid) < 0)
38220Sstevel@tonic-gate 				{
38230Sstevel@tonic-gate 					syserr("drop_privileges: second setuid(%d) attempt failed",
38240Sstevel@tonic-gate 					       (int) RunAsUid);
38250Sstevel@tonic-gate 					rval = EX_OSERR;
38260Sstevel@tonic-gate 				}
38270Sstevel@tonic-gate 			}
38280Sstevel@tonic-gate 			else
38290Sstevel@tonic-gate #endif /* HASSETREUID */
38300Sstevel@tonic-gate 			{
38310Sstevel@tonic-gate 				syserr("drop_privileges: setuid(%d) failed",
38320Sstevel@tonic-gate 				       (int) RunAsUid);
38330Sstevel@tonic-gate 				rval = EX_OSERR;
38340Sstevel@tonic-gate 			}
38350Sstevel@tonic-gate 		}
38360Sstevel@tonic-gate 		euid = geteuid();
38370Sstevel@tonic-gate 		if (RunAsUid != 0 && setuid(0) == 0)
38380Sstevel@tonic-gate 		{
38390Sstevel@tonic-gate 			/*
38400Sstevel@tonic-gate 			**  Believe it or not, the Linux capability model
38410Sstevel@tonic-gate 			**  allows a non-root process to override setuid()
38420Sstevel@tonic-gate 			**  on a process running as root and prevent that
38430Sstevel@tonic-gate 			**  process from dropping privileges.
38440Sstevel@tonic-gate 			*/
38450Sstevel@tonic-gate 
38460Sstevel@tonic-gate 			syserr("drop_privileges: setuid(0) succeeded (when it should not)");
38470Sstevel@tonic-gate 			rval = EX_OSERR;
38480Sstevel@tonic-gate 		}
38490Sstevel@tonic-gate 		else if (RunAsUid != euid && setuid(euid) == 0)
38500Sstevel@tonic-gate 		{
38510Sstevel@tonic-gate 			/*
38520Sstevel@tonic-gate 			**  Some operating systems will keep the saved-uid
38530Sstevel@tonic-gate 			**  if a non-root effective-uid calls setuid(real-uid)
38540Sstevel@tonic-gate 			**  making it possible to set it back again later.
38550Sstevel@tonic-gate 			*/
38560Sstevel@tonic-gate 
38570Sstevel@tonic-gate 			syserr("drop_privileges: Unable to drop non-root set-user-ID privileges");
38580Sstevel@tonic-gate 			rval = EX_OSERR;
38590Sstevel@tonic-gate 		}
38600Sstevel@tonic-gate 	}
38610Sstevel@tonic-gate 
38620Sstevel@tonic-gate 	if ((to_real_uid || RunAsGid != 0) &&
38630Sstevel@tonic-gate 	    rval == EX_OK && RunAsGid != EffGid &&
38640Sstevel@tonic-gate 	    getuid() != 0 && geteuid() != 0)
38650Sstevel@tonic-gate 	{
38660Sstevel@tonic-gate 		errno = 0;
38670Sstevel@tonic-gate 		if (setgid(EffGid) == 0)
38680Sstevel@tonic-gate 		{
38690Sstevel@tonic-gate 			syserr("drop_privileges: setgid(%d) succeeded (when it should not)",
38700Sstevel@tonic-gate 			       (int) EffGid);
38710Sstevel@tonic-gate 			rval = EX_OSERR;
38720Sstevel@tonic-gate 		}
38730Sstevel@tonic-gate 	}
38740Sstevel@tonic-gate 
38750Sstevel@tonic-gate 	if (tTd(47, 5))
38760Sstevel@tonic-gate 	{
38770Sstevel@tonic-gate 		sm_dprintf("drop_privileges: e/ruid = %d/%d e/rgid = %d/%d\n",
38780Sstevel@tonic-gate 			   (int) geteuid(), (int) getuid(),
38790Sstevel@tonic-gate 			   (int) getegid(), (int) getgid());
38800Sstevel@tonic-gate 		sm_dprintf("drop_privileges: RunAsUser = %d:%d\n",
38810Sstevel@tonic-gate 			   (int) RunAsUid, (int) RunAsGid);
38820Sstevel@tonic-gate 		if (tTd(47, 10))
38830Sstevel@tonic-gate 			sm_dprintf("drop_privileges: rval = %d\n", rval);
38840Sstevel@tonic-gate 	}
38850Sstevel@tonic-gate 	return rval;
38860Sstevel@tonic-gate }
38870Sstevel@tonic-gate /*
38880Sstevel@tonic-gate **  FILL_FD -- make sure a file descriptor has been properly allocated
38890Sstevel@tonic-gate **
38900Sstevel@tonic-gate **	Used to make sure that stdin/out/err are allocated on startup
38910Sstevel@tonic-gate **
38920Sstevel@tonic-gate **	Parameters:
38930Sstevel@tonic-gate **		fd -- the file descriptor to be filled.
38940Sstevel@tonic-gate **		where -- a string used for logging.  If NULL, this is
38950Sstevel@tonic-gate **			being called on startup, and logging should
38960Sstevel@tonic-gate **			not be done.
38970Sstevel@tonic-gate **
38980Sstevel@tonic-gate **	Returns:
38990Sstevel@tonic-gate **		none
39000Sstevel@tonic-gate **
39010Sstevel@tonic-gate **	Side Effects:
39020Sstevel@tonic-gate **		possibly changes MissingFds
39030Sstevel@tonic-gate */
39040Sstevel@tonic-gate 
39050Sstevel@tonic-gate void
fill_fd(fd,where)39060Sstevel@tonic-gate fill_fd(fd, where)
39070Sstevel@tonic-gate 	int fd;
39080Sstevel@tonic-gate 	char *where;
39090Sstevel@tonic-gate {
39100Sstevel@tonic-gate 	int i;
39110Sstevel@tonic-gate 	struct stat stbuf;
39120Sstevel@tonic-gate 
39130Sstevel@tonic-gate 	if (fstat(fd, &stbuf) >= 0 || errno != EBADF)
39140Sstevel@tonic-gate 		return;
39150Sstevel@tonic-gate 
39160Sstevel@tonic-gate 	if (where != NULL)
39170Sstevel@tonic-gate 		syserr("fill_fd: %s: fd %d not open", where, fd);
39180Sstevel@tonic-gate 	else
39190Sstevel@tonic-gate 		MissingFds |= 1 << fd;
39200Sstevel@tonic-gate 	i = open(SM_PATH_DEVNULL, fd == 0 ? O_RDONLY : O_WRONLY, 0666);
39210Sstevel@tonic-gate 	if (i < 0)
39220Sstevel@tonic-gate 	{
39230Sstevel@tonic-gate 		syserr("!fill_fd: %s: cannot open %s",
39240Sstevel@tonic-gate 		       where == NULL ? "startup" : where, SM_PATH_DEVNULL);
39250Sstevel@tonic-gate 	}
39260Sstevel@tonic-gate 	if (fd != i)
39270Sstevel@tonic-gate 	{
39280Sstevel@tonic-gate 		(void) dup2(i, fd);
39290Sstevel@tonic-gate 		(void) close(i);
39300Sstevel@tonic-gate 	}
39310Sstevel@tonic-gate }
39320Sstevel@tonic-gate /*
39330Sstevel@tonic-gate **  SM_PRINTOPTIONS -- print options
39340Sstevel@tonic-gate **
39350Sstevel@tonic-gate **	Parameters:
39360Sstevel@tonic-gate **		options -- array of options.
39370Sstevel@tonic-gate **
39380Sstevel@tonic-gate **	Returns:
39390Sstevel@tonic-gate **		none.
39400Sstevel@tonic-gate */
39410Sstevel@tonic-gate 
39420Sstevel@tonic-gate static void
sm_printoptions(options)39430Sstevel@tonic-gate sm_printoptions(options)
39440Sstevel@tonic-gate 	char **options;
39450Sstevel@tonic-gate {
39460Sstevel@tonic-gate 	int ll;
39470Sstevel@tonic-gate 	char **av;
39480Sstevel@tonic-gate 
39490Sstevel@tonic-gate 	av = options;
39500Sstevel@tonic-gate 	ll = 7;
39510Sstevel@tonic-gate 	while (*av != NULL)
39520Sstevel@tonic-gate 	{
39530Sstevel@tonic-gate 		if (ll + strlen(*av) > 63)
39540Sstevel@tonic-gate 		{
39550Sstevel@tonic-gate 			sm_dprintf("\n");
39560Sstevel@tonic-gate 			ll = 0;
39570Sstevel@tonic-gate 		}
39580Sstevel@tonic-gate 		if (ll == 0)
39590Sstevel@tonic-gate 			sm_dprintf("\t\t");
39600Sstevel@tonic-gate 		else
39610Sstevel@tonic-gate 			sm_dprintf(" ");
39620Sstevel@tonic-gate 		sm_dprintf("%s", *av);
39630Sstevel@tonic-gate 		ll += strlen(*av++) + 1;
39640Sstevel@tonic-gate 	}
39650Sstevel@tonic-gate 	sm_dprintf("\n");
39660Sstevel@tonic-gate }
39673544Sjbeck 
39683544Sjbeck /*
39693544Sjbeck **  TO8BIT -- convert \octal sequences in a test mode input line
39703544Sjbeck **
39713544Sjbeck **	Parameters:
39723544Sjbeck **		str -- the input line.
39733544Sjbeck **
39743544Sjbeck **	Returns:
39753544Sjbeck **		none.
39763544Sjbeck **
39773544Sjbeck **	Side Effects:
39783544Sjbeck **		replaces \0octal in str with octal value.
39793544Sjbeck */
39803544Sjbeck 
39813544Sjbeck static bool to8bit __P((char *));
39823544Sjbeck 
39833544Sjbeck static bool
to8bit(str)39843544Sjbeck to8bit(str)
39853544Sjbeck 	char *str;
39863544Sjbeck {
39873544Sjbeck 	int c, len;
39883544Sjbeck 	char *out, *in;
39893544Sjbeck 	bool changed;
39903544Sjbeck 
39913544Sjbeck 	if (str == NULL)
39923544Sjbeck 		return false;
39933544Sjbeck 	in = out = str;
39943544Sjbeck 	changed = false;
39953544Sjbeck 	len = 0;
39963544Sjbeck 	while ((c = (*str++ & 0377)) != '\0')
39973544Sjbeck 	{
39983544Sjbeck 		int oct, nxtc;
39993544Sjbeck 
40003544Sjbeck 		++len;
40013544Sjbeck 		if (c == '\\' &&
40023544Sjbeck 		    (nxtc = (*str & 0377)) == '0')
40033544Sjbeck 		{
40043544Sjbeck 			oct = 0;
40053544Sjbeck 			while ((nxtc = (*str & 0377)) != '\0' &&
40063544Sjbeck 				isascii(nxtc) && isdigit(nxtc))
40073544Sjbeck 			{
40083544Sjbeck 				oct <<= 3;
40093544Sjbeck 				oct += nxtc - '0';
40103544Sjbeck 				++str;
40113544Sjbeck 				++len;
40123544Sjbeck 			}
40133544Sjbeck 			changed = true;
40143544Sjbeck 			c = oct;
40153544Sjbeck 		}
40163544Sjbeck 		*out++ = c;
40173544Sjbeck 	}
40183544Sjbeck 	*out++ = c;
40193544Sjbeck 	if (changed)
40203544Sjbeck 	{
40213544Sjbeck 		char *q;
40223544Sjbeck 
40233544Sjbeck 		q = quote_internal_chars(in, in, &len);
40243544Sjbeck 		if (q != in)
40253544Sjbeck 			sm_strlcpy(in, q, len);
40263544Sjbeck 	}
40273544Sjbeck 	return changed;
40283544Sjbeck }
40293544Sjbeck 
40300Sstevel@tonic-gate /*
40310Sstevel@tonic-gate **  TESTMODELINE -- process a test mode input line
40320Sstevel@tonic-gate **
40330Sstevel@tonic-gate **	Parameters:
40340Sstevel@tonic-gate **		line -- the input line.
40350Sstevel@tonic-gate **		e -- the current environment.
40360Sstevel@tonic-gate **	Syntax:
40370Sstevel@tonic-gate **		#  a comment
40380Sstevel@tonic-gate **		.X process X as a configuration line
40390Sstevel@tonic-gate **		=X dump a configuration item (such as mailers)
40400Sstevel@tonic-gate **		$X dump a macro or class
40410Sstevel@tonic-gate **		/X try an activity
40420Sstevel@tonic-gate **		X  normal process through rule set X
40430Sstevel@tonic-gate */
40440Sstevel@tonic-gate 
40450Sstevel@tonic-gate static void
testmodeline(line,e)40460Sstevel@tonic-gate testmodeline(line, e)
40470Sstevel@tonic-gate 	char *line;
40480Sstevel@tonic-gate 	ENVELOPE *e;
40490Sstevel@tonic-gate {
40500Sstevel@tonic-gate 	register char *p;
40510Sstevel@tonic-gate 	char *q;
40520Sstevel@tonic-gate 	auto char *delimptr;
40530Sstevel@tonic-gate 	int mid;
40540Sstevel@tonic-gate 	int i, rs;
40550Sstevel@tonic-gate 	STAB *map;
40560Sstevel@tonic-gate 	char **s;
40570Sstevel@tonic-gate 	struct rewrite *rw;
40580Sstevel@tonic-gate 	ADDRESS a;
40593544Sjbeck 	char *lbp;
40603544Sjbeck 	auto int lbs;
40610Sstevel@tonic-gate 	static int tryflags = RF_COPYNONE;
40620Sstevel@tonic-gate 	char exbuf[MAXLINE];
40633544Sjbeck 	char lbuf[MAXLINE];
40640Sstevel@tonic-gate 	extern unsigned char TokTypeNoC[];
40653544Sjbeck 	bool eightbit;
40660Sstevel@tonic-gate 
40670Sstevel@tonic-gate 	/* skip leading spaces */
40680Sstevel@tonic-gate 	while (*line == ' ')
40690Sstevel@tonic-gate 		line++;
40700Sstevel@tonic-gate 
40713544Sjbeck 	lbp = NULL;
40723544Sjbeck 	eightbit = false;
40730Sstevel@tonic-gate 	switch (line[0])
40740Sstevel@tonic-gate 	{
40750Sstevel@tonic-gate 	  case '#':
40760Sstevel@tonic-gate 	  case '\0':
40770Sstevel@tonic-gate 		return;
40780Sstevel@tonic-gate 
40790Sstevel@tonic-gate 	  case '?':
40800Sstevel@tonic-gate 		help("-bt", e);
40810Sstevel@tonic-gate 		return;
40820Sstevel@tonic-gate 
40830Sstevel@tonic-gate 	  case '.':		/* config-style settings */
40840Sstevel@tonic-gate 		switch (line[1])
40850Sstevel@tonic-gate 		{
40860Sstevel@tonic-gate 		  case 'D':
40870Sstevel@tonic-gate 			mid = macid_parse(&line[2], &delimptr);
40880Sstevel@tonic-gate 			if (mid == 0)
40890Sstevel@tonic-gate 				return;
40903544Sjbeck 			lbs = sizeof(lbuf);
40913544Sjbeck 			lbp = translate_dollars(delimptr, lbuf, &lbs);
40923544Sjbeck 			macdefine(&e->e_macro, A_TEMP, mid, lbp);
40933544Sjbeck 			if (lbp != lbuf)
40943544Sjbeck 				SM_FREE(lbp);
40950Sstevel@tonic-gate 			break;
40960Sstevel@tonic-gate 
40970Sstevel@tonic-gate 		  case 'C':
40980Sstevel@tonic-gate 			if (line[2] == '\0')	/* not to call syserr() */
40990Sstevel@tonic-gate 				return;
41000Sstevel@tonic-gate 
41010Sstevel@tonic-gate 			mid = macid_parse(&line[2], &delimptr);
41020Sstevel@tonic-gate 			if (mid == 0)
41030Sstevel@tonic-gate 				return;
41043544Sjbeck 			lbs = sizeof(lbuf);
41053544Sjbeck 			lbp = translate_dollars(delimptr, lbuf, &lbs);
41063544Sjbeck 			expand(lbp, exbuf, sizeof(exbuf), e);
41073544Sjbeck 			if (lbp != lbuf)
41083544Sjbeck 				SM_FREE(lbp);
41090Sstevel@tonic-gate 			p = exbuf;
41100Sstevel@tonic-gate 			while (*p != '\0')
41110Sstevel@tonic-gate 			{
41120Sstevel@tonic-gate 				register char *wd;
41130Sstevel@tonic-gate 				char delim;
41140Sstevel@tonic-gate 
41150Sstevel@tonic-gate 				while (*p != '\0' && isascii(*p) && isspace(*p))
41160Sstevel@tonic-gate 					p++;
41170Sstevel@tonic-gate 				wd = p;
41180Sstevel@tonic-gate 				while (*p != '\0' && !(isascii(*p) && isspace(*p)))
41190Sstevel@tonic-gate 					p++;
41200Sstevel@tonic-gate 				delim = *p;
41210Sstevel@tonic-gate 				*p = '\0';
41220Sstevel@tonic-gate 				if (wd[0] != '\0')
41230Sstevel@tonic-gate 					setclass(mid, wd);
41240Sstevel@tonic-gate 				*p = delim;
41250Sstevel@tonic-gate 			}
41260Sstevel@tonic-gate 			break;
41270Sstevel@tonic-gate 
41280Sstevel@tonic-gate 		  case '\0':
41290Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
41300Sstevel@tonic-gate 					     "Usage: .[DC]macro value(s)\n");
41310Sstevel@tonic-gate 			break;
41320Sstevel@tonic-gate 
41330Sstevel@tonic-gate 		  default:
41340Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
41350Sstevel@tonic-gate 					     "Unknown \".\" command %s\n", line);
41360Sstevel@tonic-gate 			break;
41370Sstevel@tonic-gate 		}
41380Sstevel@tonic-gate 		return;
41390Sstevel@tonic-gate 
41400Sstevel@tonic-gate 	  case '=':		/* config-style settings */
41410Sstevel@tonic-gate 		switch (line[1])
41420Sstevel@tonic-gate 		{
41430Sstevel@tonic-gate 		  case 'S':		/* dump rule set */
41440Sstevel@tonic-gate 			rs = strtorwset(&line[2], NULL, ST_FIND);
41450Sstevel@tonic-gate 			if (rs < 0)
41460Sstevel@tonic-gate 			{
41470Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
41480Sstevel@tonic-gate 						     "Undefined ruleset %s\n", &line[2]);
41490Sstevel@tonic-gate 				return;
41500Sstevel@tonic-gate 			}
41510Sstevel@tonic-gate 			rw = RewriteRules[rs];
41520Sstevel@tonic-gate 			if (rw == NULL)
41530Sstevel@tonic-gate 				return;
41540Sstevel@tonic-gate 			do
41550Sstevel@tonic-gate 			{
41560Sstevel@tonic-gate 				(void) sm_io_putc(smioout, SM_TIME_DEFAULT,
41570Sstevel@tonic-gate 						  'R');
41580Sstevel@tonic-gate 				s = rw->r_lhs;
41590Sstevel@tonic-gate 				while (*s != NULL)
41600Sstevel@tonic-gate 				{
41610Sstevel@tonic-gate 					xputs(smioout, *s++);
41620Sstevel@tonic-gate 					(void) sm_io_putc(smioout,
41630Sstevel@tonic-gate 							  SM_TIME_DEFAULT, ' ');
41640Sstevel@tonic-gate 				}
41650Sstevel@tonic-gate 				(void) sm_io_putc(smioout, SM_TIME_DEFAULT,
41660Sstevel@tonic-gate 						  '\t');
41670Sstevel@tonic-gate 				(void) sm_io_putc(smioout, SM_TIME_DEFAULT,
41680Sstevel@tonic-gate 						  '\t');
41690Sstevel@tonic-gate 				s = rw->r_rhs;
41700Sstevel@tonic-gate 				while (*s != NULL)
41710Sstevel@tonic-gate 				{
41720Sstevel@tonic-gate 					xputs(smioout, *s++);
41730Sstevel@tonic-gate 					(void) sm_io_putc(smioout,
41740Sstevel@tonic-gate 							  SM_TIME_DEFAULT, ' ');
41750Sstevel@tonic-gate 				}
41760Sstevel@tonic-gate 				(void) sm_io_putc(smioout, SM_TIME_DEFAULT,
41770Sstevel@tonic-gate 						  '\n');
41780Sstevel@tonic-gate 			} while ((rw = rw->r_next) != NULL);
41790Sstevel@tonic-gate 			break;
41800Sstevel@tonic-gate 
41810Sstevel@tonic-gate 		  case 'M':
41820Sstevel@tonic-gate 			for (i = 0; i < MAXMAILERS; i++)
41830Sstevel@tonic-gate 			{
41840Sstevel@tonic-gate 				if (Mailer[i] != NULL)
41850Sstevel@tonic-gate 					printmailer(smioout, Mailer[i]);
41860Sstevel@tonic-gate 			}
41870Sstevel@tonic-gate 			break;
41880Sstevel@tonic-gate 
41890Sstevel@tonic-gate 		  case '\0':
41900Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
41910Sstevel@tonic-gate 					     "Usage: =Sruleset or =M\n");
41920Sstevel@tonic-gate 			break;
41930Sstevel@tonic-gate 
41940Sstevel@tonic-gate 		  default:
41950Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
41960Sstevel@tonic-gate 					     "Unknown \"=\" command %s\n", line);
41970Sstevel@tonic-gate 			break;
41980Sstevel@tonic-gate 		}
41990Sstevel@tonic-gate 		return;
42000Sstevel@tonic-gate 
42010Sstevel@tonic-gate 	  case '-':		/* set command-line-like opts */
42020Sstevel@tonic-gate 		switch (line[1])
42030Sstevel@tonic-gate 		{
42040Sstevel@tonic-gate 		  case 'd':
42050Sstevel@tonic-gate 			tTflag(&line[2]);
42060Sstevel@tonic-gate 			break;
42070Sstevel@tonic-gate 
42080Sstevel@tonic-gate 		  case '\0':
42090Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
42100Sstevel@tonic-gate 					     "Usage: -d{debug arguments}\n");
42110Sstevel@tonic-gate 			break;
42120Sstevel@tonic-gate 
42130Sstevel@tonic-gate 		  default:
42140Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
42150Sstevel@tonic-gate 					     "Unknown \"-\" command %s\n", line);
42160Sstevel@tonic-gate 			break;
42170Sstevel@tonic-gate 		}
42180Sstevel@tonic-gate 		return;
42190Sstevel@tonic-gate 
42200Sstevel@tonic-gate 	  case '$':
42210Sstevel@tonic-gate 		if (line[1] == '=')
42220Sstevel@tonic-gate 		{
42230Sstevel@tonic-gate 			mid = macid(&line[2]);
42240Sstevel@tonic-gate 			if (mid != 0)
42250Sstevel@tonic-gate 				stabapply(dump_class, mid);
42260Sstevel@tonic-gate 			return;
42270Sstevel@tonic-gate 		}
42280Sstevel@tonic-gate 		mid = macid(&line[1]);
42290Sstevel@tonic-gate 		if (mid == 0)
42300Sstevel@tonic-gate 			return;
42310Sstevel@tonic-gate 		p = macvalue(mid, e);
42320Sstevel@tonic-gate 		if (p == NULL)
42330Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
42340Sstevel@tonic-gate 					     "Undefined\n");
42350Sstevel@tonic-gate 		else
42360Sstevel@tonic-gate 		{
42370Sstevel@tonic-gate 			xputs(smioout, p);
42380Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
42390Sstevel@tonic-gate 					     "\n");
42400Sstevel@tonic-gate 		}
42410Sstevel@tonic-gate 		return;
42420Sstevel@tonic-gate 
42430Sstevel@tonic-gate 	  case '/':		/* miscellaneous commands */
42440Sstevel@tonic-gate 		p = &line[strlen(line)];
42450Sstevel@tonic-gate 		while (--p >= line && isascii(*p) && isspace(*p))
42460Sstevel@tonic-gate 			*p = '\0';
42470Sstevel@tonic-gate 		p = strpbrk(line, " \t");
42480Sstevel@tonic-gate 		if (p != NULL)
42490Sstevel@tonic-gate 		{
42500Sstevel@tonic-gate 			while (isascii(*p) && isspace(*p))
42510Sstevel@tonic-gate 				*p++ = '\0';
42520Sstevel@tonic-gate 		}
42530Sstevel@tonic-gate 		else
42540Sstevel@tonic-gate 			p = "";
42550Sstevel@tonic-gate 		if (line[1] == '\0')
42560Sstevel@tonic-gate 		{
42570Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
42580Sstevel@tonic-gate 					     "Usage: /[canon|map|mx|parse|try|tryflags]\n");
42590Sstevel@tonic-gate 			return;
42600Sstevel@tonic-gate 		}
42610Sstevel@tonic-gate 		if (sm_strcasecmp(&line[1], "quit") == 0)
42620Sstevel@tonic-gate 		{
42630Sstevel@tonic-gate 			CurEnv->e_id = NULL;
42640Sstevel@tonic-gate 			finis(true, true, ExitStat);
42650Sstevel@tonic-gate 			/* NOTREACHED */
42660Sstevel@tonic-gate 		}
42670Sstevel@tonic-gate 		if (sm_strcasecmp(&line[1], "mx") == 0)
42680Sstevel@tonic-gate 		{
42690Sstevel@tonic-gate #if NAMED_BIND
42700Sstevel@tonic-gate 			/* look up MX records */
42710Sstevel@tonic-gate 			int nmx;
42720Sstevel@tonic-gate 			auto int rcode;
42730Sstevel@tonic-gate 			char *mxhosts[MAXMXHOSTS + 1];
42740Sstevel@tonic-gate 
42750Sstevel@tonic-gate 			if (*p == '\0')
42760Sstevel@tonic-gate 			{
42770Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
42780Sstevel@tonic-gate 						     "Usage: /mx address\n");
42790Sstevel@tonic-gate 				return;
42800Sstevel@tonic-gate 			}
42810Sstevel@tonic-gate 			nmx = getmxrr(p, mxhosts, NULL, false, &rcode, true,
42820Sstevel@tonic-gate 				      NULL);
42830Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
42840Sstevel@tonic-gate 					     "getmxrr(%s) returns %d value(s):\n",
42850Sstevel@tonic-gate 				p, nmx);
42860Sstevel@tonic-gate 			for (i = 0; i < nmx; i++)
42870Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
42880Sstevel@tonic-gate 						     "\t%s\n", mxhosts[i]);
42890Sstevel@tonic-gate #else /* NAMED_BIND */
42900Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
42910Sstevel@tonic-gate 					     "No MX code compiled in\n");
42920Sstevel@tonic-gate #endif /* NAMED_BIND */
42930Sstevel@tonic-gate 		}
42940Sstevel@tonic-gate 		else if (sm_strcasecmp(&line[1], "canon") == 0)
42950Sstevel@tonic-gate 		{
42960Sstevel@tonic-gate 			char host[MAXHOSTNAMELEN];
42970Sstevel@tonic-gate 
42980Sstevel@tonic-gate 			if (*p == '\0')
42990Sstevel@tonic-gate 			{
43000Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43010Sstevel@tonic-gate 						     "Usage: /canon address\n");
43020Sstevel@tonic-gate 				return;
43030Sstevel@tonic-gate 			}
43043544Sjbeck 			else if (sm_strlcpy(host, p, sizeof(host)) >= sizeof(host))
43050Sstevel@tonic-gate 			{
43060Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43070Sstevel@tonic-gate 						     "Name too long\n");
43080Sstevel@tonic-gate 				return;
43090Sstevel@tonic-gate 			}
43103544Sjbeck 			(void) getcanonname(host, sizeof(host), !HasWildcardMX,
43110Sstevel@tonic-gate 					    NULL);
43120Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43130Sstevel@tonic-gate 					     "getcanonname(%s) returns %s\n",
43140Sstevel@tonic-gate 					     p, host);
43150Sstevel@tonic-gate 		}
43160Sstevel@tonic-gate 		else if (sm_strcasecmp(&line[1], "map") == 0)
43170Sstevel@tonic-gate 		{
43180Sstevel@tonic-gate 			auto int rcode = EX_OK;
43190Sstevel@tonic-gate 			char *av[2];
43200Sstevel@tonic-gate 
43210Sstevel@tonic-gate 			if (*p == '\0')
43220Sstevel@tonic-gate 			{
43230Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43240Sstevel@tonic-gate 						     "Usage: /map mapname key\n");
43250Sstevel@tonic-gate 				return;
43260Sstevel@tonic-gate 			}
43270Sstevel@tonic-gate 			for (q = p; *q != '\0' && !(isascii(*q) && isspace(*q));			     q++)
43280Sstevel@tonic-gate 				continue;
43290Sstevel@tonic-gate 			if (*q == '\0')
43300Sstevel@tonic-gate 			{
43310Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43320Sstevel@tonic-gate 						     "No key specified\n");
43330Sstevel@tonic-gate 				return;
43340Sstevel@tonic-gate 			}
43350Sstevel@tonic-gate 			*q++ = '\0';
43360Sstevel@tonic-gate 			map = stab(p, ST_MAP, ST_FIND);
43370Sstevel@tonic-gate 			if (map == NULL)
43380Sstevel@tonic-gate 			{
43390Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43400Sstevel@tonic-gate 						     "Map named \"%s\" not found\n", p);
43410Sstevel@tonic-gate 				return;
43420Sstevel@tonic-gate 			}
43430Sstevel@tonic-gate 			if (!bitset(MF_OPEN, map->s_map.map_mflags) &&
43440Sstevel@tonic-gate 			    !openmap(&(map->s_map)))
43450Sstevel@tonic-gate 			{
43460Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43470Sstevel@tonic-gate 						     "Map named \"%s\" not open\n", p);
43480Sstevel@tonic-gate 				return;
43490Sstevel@tonic-gate 			}
43500Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43510Sstevel@tonic-gate 					     "map_lookup: %s (%s) ", p, q);
43520Sstevel@tonic-gate 			av[0] = q;
43530Sstevel@tonic-gate 			av[1] = NULL;
43540Sstevel@tonic-gate 			p = (*map->s_map.map_class->map_lookup)
43550Sstevel@tonic-gate 					(&map->s_map, q, av, &rcode);
43560Sstevel@tonic-gate 			if (p == NULL)
43570Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43580Sstevel@tonic-gate 						     "no match (%d)\n",
43590Sstevel@tonic-gate 						     rcode);
43600Sstevel@tonic-gate 			else
43610Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43620Sstevel@tonic-gate 						     "returns %s (%d)\n", p,
43630Sstevel@tonic-gate 						     rcode);
43640Sstevel@tonic-gate 		}
43650Sstevel@tonic-gate 		else if (sm_strcasecmp(&line[1], "try") == 0)
43660Sstevel@tonic-gate 		{
43670Sstevel@tonic-gate 			MAILER *m;
43680Sstevel@tonic-gate 			STAB *st;
43690Sstevel@tonic-gate 			auto int rcode = EX_OK;
43700Sstevel@tonic-gate 
43710Sstevel@tonic-gate 			q = strpbrk(p, " \t");
43720Sstevel@tonic-gate 			if (q != NULL)
43730Sstevel@tonic-gate 			{
43740Sstevel@tonic-gate 				while (isascii(*q) && isspace(*q))
43750Sstevel@tonic-gate 					*q++ = '\0';
43760Sstevel@tonic-gate 			}
43770Sstevel@tonic-gate 			if (q == NULL || *q == '\0')
43780Sstevel@tonic-gate 			{
43790Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43800Sstevel@tonic-gate 						     "Usage: /try mailer address\n");
43810Sstevel@tonic-gate 				return;
43820Sstevel@tonic-gate 			}
43830Sstevel@tonic-gate 			st = stab(p, ST_MAILER, ST_FIND);
43840Sstevel@tonic-gate 			if (st == NULL)
43850Sstevel@tonic-gate 			{
43860Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43870Sstevel@tonic-gate 						     "Unknown mailer %s\n", p);
43880Sstevel@tonic-gate 				return;
43890Sstevel@tonic-gate 			}
43900Sstevel@tonic-gate 			m = st->s_mailer;
43910Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43920Sstevel@tonic-gate 					     "Trying %s %s address %s for mailer %s\n",
43930Sstevel@tonic-gate 				     bitset(RF_HEADERADDR, tryflags) ? "header"
43940Sstevel@tonic-gate 							: "envelope",
43950Sstevel@tonic-gate 				     bitset(RF_SENDERADDR, tryflags) ? "sender"
43960Sstevel@tonic-gate 							: "recipient", q, p);
43970Sstevel@tonic-gate 			p = remotename(q, m, tryflags, &rcode, CurEnv);
43980Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
43990Sstevel@tonic-gate 					     "Rcode = %d, addr = %s\n",
44000Sstevel@tonic-gate 					     rcode, p == NULL ? "<NULL>" : p);
44010Sstevel@tonic-gate 			e->e_to = NULL;
44020Sstevel@tonic-gate 		}
44030Sstevel@tonic-gate 		else if (sm_strcasecmp(&line[1], "tryflags") == 0)
44040Sstevel@tonic-gate 		{
44050Sstevel@tonic-gate 			if (*p == '\0')
44060Sstevel@tonic-gate 			{
44070Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
44080Sstevel@tonic-gate 						     "Usage: /tryflags [Hh|Ee][Ss|Rr]\n");
44090Sstevel@tonic-gate 				return;
44100Sstevel@tonic-gate 			}
44110Sstevel@tonic-gate 			for (; *p != '\0'; p++)
44120Sstevel@tonic-gate 			{
44130Sstevel@tonic-gate 				switch (*p)
44140Sstevel@tonic-gate 				{
44150Sstevel@tonic-gate 				  case 'H':
44160Sstevel@tonic-gate 				  case 'h':
44170Sstevel@tonic-gate 					tryflags |= RF_HEADERADDR;
44180Sstevel@tonic-gate 					break;
44190Sstevel@tonic-gate 
44200Sstevel@tonic-gate 				  case 'E':
44210Sstevel@tonic-gate 				  case 'e':
44220Sstevel@tonic-gate 					tryflags &= ~RF_HEADERADDR;
44230Sstevel@tonic-gate 					break;
44240Sstevel@tonic-gate 
44250Sstevel@tonic-gate 				  case 'S':
44260Sstevel@tonic-gate 				  case 's':
44270Sstevel@tonic-gate 					tryflags |= RF_SENDERADDR;
44280Sstevel@tonic-gate 					break;
44290Sstevel@tonic-gate 
44300Sstevel@tonic-gate 				  case 'R':
44310Sstevel@tonic-gate 				  case 'r':
44320Sstevel@tonic-gate 					tryflags &= ~RF_SENDERADDR;
44330Sstevel@tonic-gate 					break;
44340Sstevel@tonic-gate 				}
44350Sstevel@tonic-gate 			}
44360Sstevel@tonic-gate 			exbuf[0] = bitset(RF_HEADERADDR, tryflags) ? 'h' : 'e';
44370Sstevel@tonic-gate 			exbuf[1] = ' ';
44380Sstevel@tonic-gate 			exbuf[2] = bitset(RF_SENDERADDR, tryflags) ? 's' : 'r';
44390Sstevel@tonic-gate 			exbuf[3] = '\0';
44400Sstevel@tonic-gate 			macdefine(&e->e_macro, A_TEMP,
44410Sstevel@tonic-gate 				macid("{addr_type}"), exbuf);
44420Sstevel@tonic-gate 		}
44430Sstevel@tonic-gate 		else if (sm_strcasecmp(&line[1], "parse") == 0)
44440Sstevel@tonic-gate 		{
44450Sstevel@tonic-gate 			if (*p == '\0')
44460Sstevel@tonic-gate 			{
44470Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
44480Sstevel@tonic-gate 						     "Usage: /parse address\n");
44490Sstevel@tonic-gate 				return;
44500Sstevel@tonic-gate 			}
44510Sstevel@tonic-gate 			q = crackaddr(p, e);
44520Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
44530Sstevel@tonic-gate 					     "Cracked address = ");
44540Sstevel@tonic-gate 			xputs(smioout, q);
44550Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
44560Sstevel@tonic-gate 					     "\nParsing %s %s address\n",
44570Sstevel@tonic-gate 					     bitset(RF_HEADERADDR, tryflags) ?
44580Sstevel@tonic-gate 							"header" : "envelope",
44590Sstevel@tonic-gate 					     bitset(RF_SENDERADDR, tryflags) ?
44600Sstevel@tonic-gate 							"sender" : "recipient");
44610Sstevel@tonic-gate 			if (parseaddr(p, &a, tryflags, '\0', NULL, e, true)
44620Sstevel@tonic-gate 			    == NULL)
44630Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
44640Sstevel@tonic-gate 						     "Cannot parse\n");
44650Sstevel@tonic-gate 			else if (a.q_host != NULL && a.q_host[0] != '\0')
44660Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
44670Sstevel@tonic-gate 						     "mailer %s, host %s, user %s\n",
44680Sstevel@tonic-gate 						     a.q_mailer->m_name,
44690Sstevel@tonic-gate 						     a.q_host,
44700Sstevel@tonic-gate 						     a.q_user);
44710Sstevel@tonic-gate 			else
44720Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
44730Sstevel@tonic-gate 						     "mailer %s, user %s\n",
44740Sstevel@tonic-gate 						     a.q_mailer->m_name,
44750Sstevel@tonic-gate 						     a.q_user);
44760Sstevel@tonic-gate 			e->e_to = NULL;
44770Sstevel@tonic-gate 		}
44783544Sjbeck 		else if (sm_strcasecmp(&line[1], "header") == 0)
44793544Sjbeck 		{
44803544Sjbeck 			unsigned long ul;
44813544Sjbeck 
44823544Sjbeck 			ul = chompheader(p, CHHDR_CHECK|CHHDR_USER, NULL, e);
44833544Sjbeck 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
44843544Sjbeck 					     "ul = %lu\n", ul);
44853544Sjbeck 		}
44860Sstevel@tonic-gate 		else
44870Sstevel@tonic-gate 		{
44880Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
44890Sstevel@tonic-gate 					     "Unknown \"/\" command %s\n",
44900Sstevel@tonic-gate 					     line);
44910Sstevel@tonic-gate 		}
44923544Sjbeck 		(void) sm_io_flush(smioout, SM_TIME_DEFAULT);
44930Sstevel@tonic-gate 		return;
44940Sstevel@tonic-gate 	}
44950Sstevel@tonic-gate 
44960Sstevel@tonic-gate 	for (p = line; isascii(*p) && isspace(*p); p++)
44970Sstevel@tonic-gate 		continue;
44980Sstevel@tonic-gate 	q = p;
44990Sstevel@tonic-gate 	while (*p != '\0' && !(isascii(*p) && isspace(*p)))
45000Sstevel@tonic-gate 		p++;
45010Sstevel@tonic-gate 	if (*p == '\0')
45020Sstevel@tonic-gate 	{
45030Sstevel@tonic-gate 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
45040Sstevel@tonic-gate 				     "No address!\n");
45050Sstevel@tonic-gate 		return;
45060Sstevel@tonic-gate 	}
45070Sstevel@tonic-gate 	*p = '\0';
45083544Sjbeck 	if (tTd(23, 101))
45093544Sjbeck 		eightbit = to8bit(p + 1);
45100Sstevel@tonic-gate 	if (invalidaddr(p + 1, NULL, true))
45110Sstevel@tonic-gate 		return;
45120Sstevel@tonic-gate 	do
45130Sstevel@tonic-gate 	{
45140Sstevel@tonic-gate 		register char **pvp;
45150Sstevel@tonic-gate 		char pvpbuf[PSBUFSIZE];
45160Sstevel@tonic-gate 
45173544Sjbeck 		pvp = prescan(++p, ',', pvpbuf, sizeof(pvpbuf), &delimptr,
45183544Sjbeck 			      ConfigLevel >= 9 ? TokTypeNoC : ExtTokenTab, false);
45190Sstevel@tonic-gate 		if (pvp == NULL)
45200Sstevel@tonic-gate 			continue;
45210Sstevel@tonic-gate 		p = q;
45220Sstevel@tonic-gate 		while (*p != '\0')
45230Sstevel@tonic-gate 		{
45240Sstevel@tonic-gate 			int status;
45250Sstevel@tonic-gate 
45260Sstevel@tonic-gate 			rs = strtorwset(p, NULL, ST_FIND);
45270Sstevel@tonic-gate 			if (rs < 0)
45280Sstevel@tonic-gate 			{
45290Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
45300Sstevel@tonic-gate 						     "Undefined ruleset %s\n",
45310Sstevel@tonic-gate 						     p);
45320Sstevel@tonic-gate 				break;
45330Sstevel@tonic-gate 			}
45340Sstevel@tonic-gate 			status = REWRITE(pvp, rs, e);
45350Sstevel@tonic-gate 			if (status != EX_OK)
45360Sstevel@tonic-gate 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
45370Sstevel@tonic-gate 						     "== Ruleset %s (%d) status %d\n",
45380Sstevel@tonic-gate 						     p, rs, status);
45393544Sjbeck 			else if (eightbit)
45403544Sjbeck 			{
45413544Sjbeck 				cataddr(pvp, NULL, exbuf, sizeof(exbuf), '\0',
45423544Sjbeck 					true);
45433544Sjbeck 				(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
45443544Sjbeck 						     "cataddr: %s\n",
45453544Sjbeck 						     str2prt(exbuf));
45463544Sjbeck 			}
45470Sstevel@tonic-gate 			while (*p != '\0' && *p++ != ',')
45480Sstevel@tonic-gate 				continue;
45490Sstevel@tonic-gate 		}
45500Sstevel@tonic-gate 	} while (*(p = delimptr) != '\0');
45513544Sjbeck 	(void) sm_io_flush(smioout, SM_TIME_DEFAULT);
45520Sstevel@tonic-gate }
45530Sstevel@tonic-gate 
45540Sstevel@tonic-gate static void
dump_class(s,id)45550Sstevel@tonic-gate dump_class(s, id)
45560Sstevel@tonic-gate 	register STAB *s;
45570Sstevel@tonic-gate 	int id;
45580Sstevel@tonic-gate {
45590Sstevel@tonic-gate 	if (s->s_symtype != ST_CLASS)
45600Sstevel@tonic-gate 		return;
45610Sstevel@tonic-gate 	if (bitnset(bitidx(id), s->s_class))
45620Sstevel@tonic-gate 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
45630Sstevel@tonic-gate 				     "%s\n", s->s_name);
45640Sstevel@tonic-gate }
45650Sstevel@tonic-gate 
45660Sstevel@tonic-gate /*
45670Sstevel@tonic-gate **  An exception type used to create QuickAbort exceptions.
45680Sstevel@tonic-gate **  This is my first cut at converting QuickAbort from longjmp to exceptions.
45690Sstevel@tonic-gate **  These exceptions have a single integer argument, which is the argument
45700Sstevel@tonic-gate **  to longjmp in the original code (either 1 or 2).  I don't know the
45710Sstevel@tonic-gate **  significance of 1 vs 2: the calls to setjmp don't care.
45720Sstevel@tonic-gate */
45730Sstevel@tonic-gate 
45740Sstevel@tonic-gate const SM_EXC_TYPE_T EtypeQuickAbort =
45750Sstevel@tonic-gate {
45760Sstevel@tonic-gate 	SmExcTypeMagic,
45770Sstevel@tonic-gate 	"E:mta.quickabort",
45780Sstevel@tonic-gate 	"i",
45790Sstevel@tonic-gate 	sm_etype_printf,
45800Sstevel@tonic-gate 	"quick abort %0",
45810Sstevel@tonic-gate };
4582