xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 51210)
122709Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822709Sdist 
922709Sdist #ifndef lint
10*51210Seric static char sccsid[] = "@(#)readcf.c	5.25 (Berkeley) 09/30/91";
1133731Sbostic #endif /* not lint */
1222709Sdist 
133313Seric # include "sendmail.h"
143308Seric 
153308Seric /*
163308Seric **  READCF -- read control file.
173308Seric **
183308Seric **	This routine reads the control file and builds the internal
193308Seric **	form.
203308Seric **
214432Seric **	The file is formatted as a sequence of lines, each taken
224432Seric **	atomically.  The first character of each line describes how
234432Seric **	the line is to be interpreted.  The lines are:
244432Seric **		Dxval		Define macro x to have value val.
254432Seric **		Cxword		Put word into class x.
264432Seric **		Fxfile [fmt]	Read file for lines to put into
274432Seric **				class x.  Use scanf string 'fmt'
284432Seric **				or "%s" if not present.  Fmt should
294432Seric **				only produce one string-valued result.
304432Seric **		Hname: value	Define header with field-name 'name'
314432Seric **				and value as specified; this will be
324432Seric **				macro expanded immediately before
334432Seric **				use.
344432Seric **		Sn		Use rewriting set n.
354432Seric **		Rlhs rhs	Rewrite addresses that match lhs to
364432Seric **				be rhs.
3724944Seric **		Mn arg=val...	Define mailer.  n is the internal name.
3824944Seric **				Args specify mailer parameters.
398252Seric **		Oxvalue		Set option x to value.
408252Seric **		Pname=value	Set precedence name to value.
414432Seric **
423308Seric **	Parameters:
433308Seric **		cfname -- control file name.
443308Seric **
453308Seric **	Returns:
463308Seric **		none.
473308Seric **
483308Seric **	Side Effects:
493308Seric **		Builds several internal tables.
503308Seric */
513308Seric 
5221066Seric readcf(cfname)
533308Seric 	char *cfname;
543308Seric {
553308Seric 	FILE *cf;
568547Seric 	int ruleset = 0;
578547Seric 	char *q;
588547Seric 	char **pv;
599350Seric 	struct rewrite *rwp = NULL;
603308Seric 	char buf[MAXLINE];
613308Seric 	register char *p;
623308Seric 	extern char **prescan();
633308Seric 	extern char **copyplist();
645909Seric 	char exbuf[MAXLINE];
6516915Seric 	char pvpbuf[PSBUFSIZE];
669350Seric 	extern char *fgetfolded();
6710709Seric 	extern char *munchstring();
683308Seric 
693308Seric 	cf = fopen(cfname, "r");
703308Seric 	if (cf == NULL)
713308Seric 	{
723308Seric 		syserr("cannot open %s", cfname);
733308Seric 		exit(EX_OSFILE);
743308Seric 	}
753308Seric 
769381Seric 	FileName = cfname;
778056Seric 	LineNumber = 0;
787854Seric 	while (fgetfolded(buf, sizeof buf, cf) != NULL)
793308Seric 	{
8016157Seric 		/* map $ into \001 (ASCII SOH) for macro expansion */
8116157Seric 		for (p = buf; *p != '\0'; p++)
8216157Seric 		{
8316157Seric 			if (*p != '$')
8416157Seric 				continue;
8516157Seric 
8616157Seric 			if (p[1] == '$')
8716157Seric 			{
8816157Seric 				/* actual dollar sign.... */
8923111Seric 				(void) strcpy(p, p + 1);
9016157Seric 				continue;
9116157Seric 			}
9216157Seric 
9316157Seric 			/* convert to macro expansion character */
9416157Seric 			*p = '\001';
9516157Seric 		}
9616157Seric 
9716157Seric 		/* interpret this line */
983308Seric 		switch (buf[0])
993308Seric 		{
1003308Seric 		  case '\0':
1013308Seric 		  case '#':		/* comment */
1023308Seric 			break;
1033308Seric 
1043308Seric 		  case 'R':		/* rewriting rule */
1053308Seric 			for (p = &buf[1]; *p != '\0' && *p != '\t'; p++)
1063308Seric 				continue;
1073308Seric 
1083308Seric 			if (*p == '\0')
1095909Seric 			{
1109381Seric 				syserr("invalid rewrite line \"%s\"", buf);
1115909Seric 				break;
1125909Seric 			}
1135909Seric 
1145909Seric 			/* allocate space for the rule header */
1155909Seric 			if (rwp == NULL)
1165909Seric 			{
1175909Seric 				RewriteRules[ruleset] = rwp =
1185909Seric 					(struct rewrite *) xalloc(sizeof *rwp);
1195909Seric 			}
1203308Seric 			else
1213308Seric 			{
1225909Seric 				rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp);
1235909Seric 				rwp = rwp->r_next;
1245909Seric 			}
1255909Seric 			rwp->r_next = NULL;
1263308Seric 
1275909Seric 			/* expand and save the LHS */
1285909Seric 			*p = '\0';
1296991Seric 			expand(&buf[1], exbuf, &exbuf[sizeof exbuf], CurEnv);
13016915Seric 			rwp->r_lhs = prescan(exbuf, '\t', pvpbuf);
1315909Seric 			if (rwp->r_lhs != NULL)
1325909Seric 				rwp->r_lhs = copyplist(rwp->r_lhs, TRUE);
1335909Seric 
1345909Seric 			/* expand and save the RHS */
1355909Seric 			while (*++p == '\t')
1365909Seric 				continue;
1377231Seric 			q = p;
1387231Seric 			while (*p != '\0' && *p != '\t')
1397231Seric 				p++;
1407231Seric 			*p = '\0';
1417231Seric 			expand(q, exbuf, &exbuf[sizeof exbuf], CurEnv);
14216915Seric 			rwp->r_rhs = prescan(exbuf, '\t', pvpbuf);
1435909Seric 			if (rwp->r_rhs != NULL)
1445909Seric 				rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
1453308Seric 			break;
1463308Seric 
1474072Seric 		  case 'S':		/* select rewriting set */
1484072Seric 			ruleset = atoi(&buf[1]);
1498056Seric 			if (ruleset >= MAXRWSETS || ruleset < 0)
1508056Seric 			{
1519381Seric 				syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS);
1528056Seric 				ruleset = 0;
1538056Seric 			}
1544072Seric 			rwp = NULL;
1554072Seric 			break;
1564072Seric 
1573308Seric 		  case 'D':		/* macro definition */
15810709Seric 			define(buf[1], newstr(munchstring(&buf[2])), CurEnv);
1593308Seric 			break;
1603308Seric 
1613387Seric 		  case 'H':		/* required header line */
1624088Seric 			(void) chompheader(&buf[1], TRUE);
1633387Seric 			break;
1643387Seric 
1654061Seric 		  case 'C':		/* word class */
1664432Seric 		  case 'F':		/* word class from file */
1674432Seric 			/* read list of words from argument or file */
1684432Seric 			if (buf[0] == 'F')
1694432Seric 			{
1704432Seric 				/* read from file */
1714432Seric 				for (p = &buf[2]; *p != '\0' && !isspace(*p); p++)
1724432Seric 					continue;
1734432Seric 				if (*p == '\0')
1744432Seric 					p = "%s";
1754432Seric 				else
1764432Seric 				{
1774432Seric 					*p = '\0';
1784432Seric 					while (isspace(*++p))
1794432Seric 						continue;
1804432Seric 				}
18110687Seric 				fileclass(buf[1], &buf[2], p);
1824432Seric 				break;
1834432Seric 			}
1844061Seric 
1854432Seric 			/* scan the list of words and set class for all */
1864061Seric 			for (p = &buf[2]; *p != '\0'; )
1874061Seric 			{
1884061Seric 				register char *wd;
1894061Seric 				char delim;
1904061Seric 
1914061Seric 				while (*p != '\0' && isspace(*p))
1924061Seric 					p++;
1934061Seric 				wd = p;
1944061Seric 				while (*p != '\0' && !isspace(*p))
1954061Seric 					p++;
1964061Seric 				delim = *p;
1974061Seric 				*p = '\0';
1984061Seric 				if (wd[0] != '\0')
19910687Seric 					setclass(buf[1], wd);
2004061Seric 				*p = delim;
2014061Seric 			}
2024061Seric 			break;
2034061Seric 
2044096Seric 		  case 'M':		/* define mailer */
20521066Seric 			makemailer(&buf[1]);
2064096Seric 			break;
2074096Seric 
2088252Seric 		  case 'O':		/* set option */
20921755Seric 			setoption(buf[1], &buf[2], TRUE, FALSE);
2108252Seric 			break;
2118252Seric 
2128252Seric 		  case 'P':		/* set precedence */
2138252Seric 			if (NumPriorities >= MAXPRIORITIES)
2148252Seric 			{
2158547Seric 				toomany('P', MAXPRIORITIES);
2168252Seric 				break;
2178252Seric 			}
2189381Seric 			for (p = &buf[1]; *p != '\0' && *p != '=' && *p != '\t'; p++)
2198252Seric 				continue;
2208252Seric 			if (*p == '\0')
2218252Seric 				goto badline;
2228252Seric 			*p = '\0';
2238252Seric 			Priorities[NumPriorities].pri_name = newstr(&buf[1]);
2248252Seric 			Priorities[NumPriorities].pri_val = atoi(++p);
2258252Seric 			NumPriorities++;
2268252Seric 			break;
2278252Seric 
2288547Seric 		  case 'T':		/* trusted user(s) */
2298547Seric 			p = &buf[1];
2308547Seric 			while (*p != '\0')
2318547Seric 			{
2328547Seric 				while (isspace(*p))
2338547Seric 					p++;
2348547Seric 				q = p;
2358547Seric 				while (*p != '\0' && !isspace(*p))
2368547Seric 					p++;
2378547Seric 				if (*p != '\0')
2388547Seric 					*p++ = '\0';
2398547Seric 				if (*q == '\0')
2408547Seric 					continue;
2418547Seric 				for (pv = TrustedUsers; *pv != NULL; pv++)
2428547Seric 					continue;
2438547Seric 				if (pv >= &TrustedUsers[MAXTRUST])
2448547Seric 				{
2458547Seric 					toomany('T', MAXTRUST);
2468547Seric 					break;
2478547Seric 				}
2488547Seric 				*pv = newstr(q);
2498547Seric 			}
2508547Seric 			break;
2518547Seric 
2523308Seric 		  default:
2534061Seric 		  badline:
2549381Seric 			syserr("unknown control line \"%s\"", buf);
2553308Seric 		}
2563308Seric 	}
2579381Seric 	FileName = NULL;
2584096Seric }
2594096Seric /*
2608547Seric **  TOOMANY -- signal too many of some option
2618547Seric **
2628547Seric **	Parameters:
2638547Seric **		id -- the id of the error line
2648547Seric **		maxcnt -- the maximum possible values
2658547Seric **
2668547Seric **	Returns:
2678547Seric **		none.
2688547Seric **
2698547Seric **	Side Effects:
2708547Seric **		gives a syserr.
2718547Seric */
2728547Seric 
2738547Seric toomany(id, maxcnt)
2748547Seric 	char id;
2758547Seric 	int maxcnt;
2768547Seric {
2779381Seric 	syserr("too many %c lines, %d max", id, maxcnt);
2788547Seric }
2798547Seric /*
2804432Seric **  FILECLASS -- read members of a class from a file
2814432Seric **
2824432Seric **	Parameters:
2834432Seric **		class -- class to define.
2844432Seric **		filename -- name of file to read.
2854432Seric **		fmt -- scanf string to use for match.
2864432Seric **
2874432Seric **	Returns:
2884432Seric **		none
2894432Seric **
2904432Seric **	Side Effects:
2914432Seric **
2924432Seric **		puts all lines in filename that match a scanf into
2934432Seric **			the named class.
2944432Seric */
2954432Seric 
2964432Seric fileclass(class, filename, fmt)
2974432Seric 	int class;
2984432Seric 	char *filename;
2994432Seric 	char *fmt;
3004432Seric {
30125808Seric 	FILE *f;
3024432Seric 	char buf[MAXLINE];
3034432Seric 
3044432Seric 	f = fopen(filename, "r");
3054432Seric 	if (f == NULL)
3064432Seric 	{
3074432Seric 		syserr("cannot open %s", filename);
3084432Seric 		return;
3094432Seric 	}
3104432Seric 
3114432Seric 	while (fgets(buf, sizeof buf, f) != NULL)
3124432Seric 	{
3134432Seric 		register STAB *s;
31425808Seric 		register char *p;
31525808Seric # ifdef SCANF
3164432Seric 		char wordbuf[MAXNAME+1];
3174432Seric 
3184432Seric 		if (sscanf(buf, fmt, wordbuf) != 1)
3194432Seric 			continue;
32025808Seric 		p = wordbuf;
32125808Seric # else SCANF
32225808Seric 		p = buf;
32325808Seric # endif SCANF
32425808Seric 
32525808Seric 		/*
32625808Seric 		**  Break up the match into words.
32725808Seric 		*/
32825808Seric 
32925808Seric 		while (*p != '\0')
33025808Seric 		{
33125808Seric 			register char *q;
33225808Seric 
33325808Seric 			/* strip leading spaces */
33425808Seric 			while (isspace(*p))
33525808Seric 				p++;
33625808Seric 			if (*p == '\0')
33725808Seric 				break;
33825808Seric 
33925808Seric 			/* find the end of the word */
34025808Seric 			q = p;
34125808Seric 			while (*p != '\0' && !isspace(*p))
34225808Seric 				p++;
34325808Seric 			if (*p != '\0')
34425808Seric 				*p++ = '\0';
34525808Seric 
34625808Seric 			/* enter the word in the symbol table */
34725808Seric 			s = stab(q, ST_CLASS, ST_ENTER);
34825808Seric 			setbitn(class, s->s_class);
34925808Seric 		}
3504432Seric 	}
3514432Seric 
3524627Seric 	(void) fclose(f);
3534432Seric }
3544432Seric /*
3554096Seric **  MAKEMAILER -- define a new mailer.
3564096Seric **
3574096Seric **	Parameters:
35810327Seric **		line -- description of mailer.  This is in labeled
35910327Seric **			fields.  The fields are:
36010327Seric **			   P -- the path to the mailer
36110327Seric **			   F -- the flags associated with the mailer
36210327Seric **			   A -- the argv for this mailer
36310327Seric **			   S -- the sender rewriting set
36410327Seric **			   R -- the recipient rewriting set
36510327Seric **			   E -- the eol string
36610327Seric **			The first word is the canonical name of the mailer.
3674096Seric **
3684096Seric **	Returns:
3694096Seric **		none.
3704096Seric **
3714096Seric **	Side Effects:
3724096Seric **		enters the mailer into the mailer table.
3734096Seric */
3743308Seric 
37521066Seric makemailer(line)
3764096Seric 	char *line;
3774096Seric {
3784096Seric 	register char *p;
3798067Seric 	register struct mailer *m;
3808067Seric 	register STAB *s;
3818067Seric 	int i;
38210327Seric 	char fcode;
3834096Seric 	extern int NextMailer;
38410327Seric 	extern char **makeargv();
38510327Seric 	extern char *munchstring();
38610327Seric 	extern char *DelimChar;
38710701Seric 	extern long atol();
3884096Seric 
38910327Seric 	/* allocate a mailer and set up defaults */
39010327Seric 	m = (struct mailer *) xalloc(sizeof *m);
39110327Seric 	bzero((char *) m, sizeof *m);
39210327Seric 	m->m_mno = NextMailer;
39310327Seric 	m->m_eol = "\n";
39410327Seric 
39510327Seric 	/* collect the mailer name */
39610327Seric 	for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++)
39710327Seric 		continue;
39810327Seric 	if (*p != '\0')
39910327Seric 		*p++ = '\0';
40010327Seric 	m->m_name = newstr(line);
40110327Seric 
40210327Seric 	/* now scan through and assign info from the fields */
40310327Seric 	while (*p != '\0')
40410327Seric 	{
40510327Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
40610327Seric 			p++;
40710327Seric 
40810327Seric 		/* p now points to field code */
40910327Seric 		fcode = *p;
41010327Seric 		while (*p != '\0' && *p != '=' && *p != ',')
41110327Seric 			p++;
41210327Seric 		if (*p++ != '=')
41310327Seric 		{
41410327Seric 			syserr("`=' expected");
41510327Seric 			return;
41610327Seric 		}
41710327Seric 		while (isspace(*p))
41810327Seric 			p++;
41910327Seric 
42010327Seric 		/* p now points to the field body */
42110327Seric 		p = munchstring(p);
42210327Seric 
42310327Seric 		/* install the field into the mailer struct */
42410327Seric 		switch (fcode)
42510327Seric 		{
42610327Seric 		  case 'P':		/* pathname */
42710327Seric 			m->m_mailer = newstr(p);
42810327Seric 			break;
42910327Seric 
43010327Seric 		  case 'F':		/* flags */
43110687Seric 			for (; *p != '\0'; p++)
43210687Seric 				setbitn(*p, m->m_flags);
43310327Seric 			break;
43410327Seric 
43510327Seric 		  case 'S':		/* sender rewriting ruleset */
43610327Seric 		  case 'R':		/* recipient rewriting ruleset */
43710327Seric 			i = atoi(p);
43810327Seric 			if (i < 0 || i >= MAXRWSETS)
43910327Seric 			{
44010327Seric 				syserr("invalid rewrite set, %d max", MAXRWSETS);
44110327Seric 				return;
44210327Seric 			}
44310327Seric 			if (fcode == 'S')
44410327Seric 				m->m_s_rwset = i;
44510327Seric 			else
44610327Seric 				m->m_r_rwset = i;
44710327Seric 			break;
44810327Seric 
44910327Seric 		  case 'E':		/* end of line string */
45010327Seric 			m->m_eol = newstr(p);
45110327Seric 			break;
45210327Seric 
45310327Seric 		  case 'A':		/* argument vector */
45410327Seric 			m->m_argv = makeargv(p);
45510327Seric 			break;
45610701Seric 
45710701Seric 		  case 'M':		/* maximum message size */
45810701Seric 			m->m_maxsize = atol(p);
45910701Seric 			break;
46010327Seric 		}
46110327Seric 
46210327Seric 		p = DelimChar;
46310327Seric 	}
46410327Seric 
46510327Seric 	/* now store the mailer away */
4664096Seric 	if (NextMailer >= MAXMAILERS)
4674096Seric 	{
4689381Seric 		syserr("too many mailers defined (%d max)", MAXMAILERS);
4694096Seric 		return;
4704096Seric 	}
47110327Seric 	Mailer[NextMailer++] = m;
47210327Seric 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
47310327Seric 	s->s_mailer = m;
47410327Seric }
47510327Seric /*
47610327Seric **  MUNCHSTRING -- translate a string into internal form.
47710327Seric **
47810327Seric **	Parameters:
47910327Seric **		p -- the string to munch.
48010327Seric **
48110327Seric **	Returns:
48210327Seric **		the munched string.
48310327Seric **
48410327Seric **	Side Effects:
48510327Seric **		Sets "DelimChar" to point to the string that caused us
48610327Seric **		to stop.
48710327Seric */
4884096Seric 
48910327Seric char *
49010327Seric munchstring(p)
49110327Seric 	register char *p;
49210327Seric {
49310327Seric 	register char *q;
49410327Seric 	bool backslash = FALSE;
49510327Seric 	bool quotemode = FALSE;
49610327Seric 	static char buf[MAXLINE];
49710327Seric 	extern char *DelimChar;
4984096Seric 
49910327Seric 	for (q = buf; *p != '\0'; p++)
5004096Seric 	{
50110327Seric 		if (backslash)
50210327Seric 		{
50310327Seric 			/* everything is roughly literal */
50410357Seric 			backslash = FALSE;
50510327Seric 			switch (*p)
50610327Seric 			{
50710327Seric 			  case 'r':		/* carriage return */
50810327Seric 				*q++ = '\r';
50910327Seric 				continue;
51010327Seric 
51110327Seric 			  case 'n':		/* newline */
51210327Seric 				*q++ = '\n';
51310327Seric 				continue;
51410327Seric 
51510327Seric 			  case 'f':		/* form feed */
51610327Seric 				*q++ = '\f';
51710327Seric 				continue;
51810327Seric 
51910327Seric 			  case 'b':		/* backspace */
52010327Seric 				*q++ = '\b';
52110327Seric 				continue;
52210327Seric 			}
52310327Seric 			*q++ = *p;
52410327Seric 		}
52510327Seric 		else
52610327Seric 		{
52710327Seric 			if (*p == '\\')
52810327Seric 				backslash = TRUE;
52910327Seric 			else if (*p == '"')
53010327Seric 				quotemode = !quotemode;
53110327Seric 			else if (quotemode || *p != ',')
53210327Seric 				*q++ = *p;
53310327Seric 			else
53410327Seric 				break;
53510327Seric 		}
5364096Seric 	}
5374096Seric 
53810327Seric 	DelimChar = p;
53910327Seric 	*q++ = '\0';
54010327Seric 	return (buf);
54110327Seric }
54210327Seric /*
54310327Seric **  MAKEARGV -- break up a string into words
54410327Seric **
54510327Seric **	Parameters:
54610327Seric **		p -- the string to break up.
54710327Seric **
54810327Seric **	Returns:
54910327Seric **		a char **argv (dynamically allocated)
55010327Seric **
55110327Seric **	Side Effects:
55210327Seric **		munges p.
55310327Seric */
5544096Seric 
55510327Seric char **
55610327Seric makeargv(p)
55710327Seric 	register char *p;
55810327Seric {
55910327Seric 	char *q;
56010327Seric 	int i;
56110327Seric 	char **avp;
56210327Seric 	char *argv[MAXPV + 1];
56310327Seric 
56410327Seric 	/* take apart the words */
56510327Seric 	i = 0;
56610327Seric 	while (*p != '\0' && i < MAXPV)
5674096Seric 	{
56810327Seric 		q = p;
56910327Seric 		while (*p != '\0' && !isspace(*p))
57010327Seric 			p++;
57110327Seric 		while (isspace(*p))
57210327Seric 			*p++ = '\0';
57310327Seric 		argv[i++] = newstr(q);
5744096Seric 	}
57510327Seric 	argv[i++] = NULL;
5764096Seric 
57710327Seric 	/* now make a copy of the argv */
57810327Seric 	avp = (char **) xalloc(sizeof *avp * i);
57916893Seric 	bcopy((char *) argv, (char *) avp, sizeof *avp * i);
58010327Seric 
58110327Seric 	return (avp);
5823308Seric }
5833308Seric /*
5843308Seric **  PRINTRULES -- print rewrite rules (for debugging)
5853308Seric **
5863308Seric **	Parameters:
5873308Seric **		none.
5883308Seric **
5893308Seric **	Returns:
5903308Seric **		none.
5913308Seric **
5923308Seric **	Side Effects:
5933308Seric **		prints rewrite rules.
5943308Seric */
5953308Seric 
5963308Seric printrules()
5973308Seric {
5983308Seric 	register struct rewrite *rwp;
5994072Seric 	register int ruleset;
6003308Seric 
6014072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
6023308Seric 	{
6034072Seric 		if (RewriteRules[ruleset] == NULL)
6044072Seric 			continue;
6058067Seric 		printf("\n----Rule Set %d:", ruleset);
6063308Seric 
6074072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
6083308Seric 		{
6098067Seric 			printf("\nLHS:");
6108067Seric 			printav(rwp->r_lhs);
6118067Seric 			printf("RHS:");
6128067Seric 			printav(rwp->r_rhs);
6133308Seric 		}
6143308Seric 	}
6153308Seric }
6164319Seric 
6174096Seric /*
6188256Seric **  SETOPTION -- set global processing option
6198256Seric **
6208256Seric **	Parameters:
6218256Seric **		opt -- option name.
6228256Seric **		val -- option value (as a text string).
62321755Seric **		safe -- set if this came from a configuration file.
62421755Seric **			Some options (if set from the command line) will
62521755Seric **			reset the user id to avoid security problems.
6268269Seric **		sticky -- if set, don't let other setoptions override
6278269Seric **			this value.
6288256Seric **
6298256Seric **	Returns:
6308256Seric **		none.
6318256Seric **
6328256Seric **	Side Effects:
6338256Seric **		Sets options as implied by the arguments.
6348256Seric */
6358256Seric 
63610687Seric static BITMAP	StickyOpt;		/* set if option is stuck */
63725700Seric extern char	*NetName;		/* name of home (local) network */
6388269Seric 
63921755Seric setoption(opt, val, safe, sticky)
6408256Seric 	char opt;
6418256Seric 	char *val;
64221755Seric 	bool safe;
6438269Seric 	bool sticky;
6448256Seric {
6458265Seric 	extern bool atobool();
64612633Seric 	extern time_t convtime();
64714879Seric 	extern int QueueLA;
64814879Seric 	extern int RefuseLA;
64917474Seric 	extern bool trusteduser();
65017474Seric 	extern char *username();
6518256Seric 
6528256Seric 	if (tTd(37, 1))
6539341Seric 		printf("setoption %c=%s", opt, val);
6548256Seric 
6558256Seric 	/*
6568269Seric 	**  See if this option is preset for us.
6578256Seric 	*/
6588256Seric 
65910687Seric 	if (bitnset(opt, StickyOpt))
6608269Seric 	{
6619341Seric 		if (tTd(37, 1))
6629341Seric 			printf(" (ignored)\n");
6638269Seric 		return;
6648269Seric 	}
6658269Seric 
66621755Seric 	/*
66721755Seric 	**  Check to see if this option can be specified by this user.
66821755Seric 	*/
66921755Seric 
67036238Skarels 	if (!safe && getuid() == 0)
67121755Seric 		safe = TRUE;
67221755Seric 	if (!safe && index("deiLmorsv", opt) == NULL)
67321755Seric 	{
67439111Srick 		if (opt != 'M' || (val[0] != 'r' && val[0] != 's'))
67521755Seric 		{
67636582Sbostic 			if (tTd(37, 1))
67736582Sbostic 				printf(" (unsafe)");
67836582Sbostic 			if (getuid() != geteuid())
67936582Sbostic 			{
680*51210Seric 				if (tTd(37, 1))
681*51210Seric 					printf("(Resetting uid)");
68236582Sbostic 				(void) setgid(getgid());
68336582Sbostic 				(void) setuid(getuid());
68436582Sbostic 			}
68521755Seric 		}
68621755Seric 	}
687*51210Seric 	if (tTd(37, 1))
68817985Seric 		printf("\n");
6898269Seric 
6908256Seric 	switch (opt)
6918256Seric 	{
6928256Seric 	  case 'A':		/* set default alias file */
6939381Seric 		if (val[0] == '\0')
6948269Seric 			AliasFile = "aliases";
6959381Seric 		else
6969381Seric 			AliasFile = newstr(val);
6978256Seric 		break;
6988256Seric 
69917474Seric 	  case 'a':		/* look N minutes for "@:@" in alias file */
70017474Seric 		if (val[0] == '\0')
70117474Seric 			SafeAlias = 5;
70217474Seric 		else
70317474Seric 			SafeAlias = atoi(val);
70417474Seric 		break;
70517474Seric 
70616843Seric 	  case 'B':		/* substitution for blank character */
70716843Seric 		SpaceSub = val[0];
70816843Seric 		if (SpaceSub == '\0')
70916843Seric 			SpaceSub = ' ';
71016843Seric 		break;
71116843Seric 
7129284Seric 	  case 'c':		/* don't connect to "expensive" mailers */
7139381Seric 		NoConnect = atobool(val);
7149284Seric 		break;
7159284Seric 
71624944Seric 	  case 'C':		/* checkpoint after N connections */
71724944Seric 		CheckPointLimit = atoi(val);
71824944Seric 		break;
71924944Seric 
7209284Seric 	  case 'd':		/* delivery mode */
7219284Seric 		switch (*val)
7228269Seric 		{
7239284Seric 		  case '\0':
7249284Seric 			SendMode = SM_DELIVER;
7258269Seric 			break;
7268269Seric 
72710755Seric 		  case SM_QUEUE:	/* queue only */
72810755Seric #ifndef QUEUE
72910755Seric 			syserr("need QUEUE to set -odqueue");
73010755Seric #endif QUEUE
73110755Seric 			/* fall through..... */
73210755Seric 
7339284Seric 		  case SM_DELIVER:	/* do everything */
7349284Seric 		  case SM_FORK:		/* fork after verification */
7359284Seric 			SendMode = *val;
7368269Seric 			break;
7378269Seric 
7388269Seric 		  default:
7399284Seric 			syserr("Unknown delivery mode %c", *val);
7408269Seric 			exit(EX_USAGE);
7418269Seric 		}
7428269Seric 		break;
7438269Seric 
7449146Seric 	  case 'D':		/* rebuild alias database as needed */
7459381Seric 		AutoRebuild = atobool(val);
7469146Seric 		break;
7479146Seric 
7488269Seric 	  case 'e':		/* set error processing mode */
7498269Seric 		switch (*val)
7508269Seric 		{
7519381Seric 		  case EM_QUIET:	/* be silent about it */
7529381Seric 		  case EM_MAIL:		/* mail back */
7539381Seric 		  case EM_BERKNET:	/* do berknet error processing */
7549381Seric 		  case EM_WRITE:	/* write back (or mail) */
7558269Seric 			HoldErrs = TRUE;
7569381Seric 			/* fall through... */
7578269Seric 
7589381Seric 		  case EM_PRINT:	/* print errors normally (default) */
7599381Seric 			ErrorMode = *val;
7608269Seric 			break;
7618269Seric 		}
7628269Seric 		break;
7638269Seric 
7649049Seric 	  case 'F':		/* file mode */
76517975Seric 		FileMode = atooct(val) & 0777;
7669049Seric 		break;
7679049Seric 
7688269Seric 	  case 'f':		/* save Unix-style From lines on front */
7699381Seric 		SaveFrom = atobool(val);
7708269Seric 		break;
7718269Seric 
7728256Seric 	  case 'g':		/* default gid */
77317474Seric 		DefGid = atoi(val);
7748256Seric 		break;
7758256Seric 
7768256Seric 	  case 'H':		/* help file */
7779381Seric 		if (val[0] == '\0')
7788269Seric 			HelpFile = "sendmail.hf";
7799381Seric 		else
7809381Seric 			HelpFile = newstr(val);
7818256Seric 		break;
7828256Seric 
78335651Seric 	  case 'I':		/* use internet domain name server */
78435651Seric 		UseNameServer = atobool(val);
78535651Seric 		break;
78635651Seric 
7878269Seric 	  case 'i':		/* ignore dot lines in message */
7889381Seric 		IgnrDot = atobool(val);
7898269Seric 		break;
7908269Seric 
79147285Seric 	  case 'k':		/* checkpoint every N addresses */
79247285Seric 		CheckpointInterval = atoi(val);
79347285Seric 		break;
79447285Seric 
7958256Seric 	  case 'L':		/* log level */
7969381Seric 		LogLevel = atoi(val);
7978256Seric 		break;
7988256Seric 
7998269Seric 	  case 'M':		/* define macro */
8009381Seric 		define(val[0], newstr(&val[1]), CurEnv);
80116878Seric 		sticky = FALSE;
8028269Seric 		break;
8038269Seric 
8048269Seric 	  case 'm':		/* send to me too */
8059381Seric 		MeToo = atobool(val);
8068269Seric 		break;
8078269Seric 
80825820Seric 	  case 'n':		/* validate RHS in newaliases */
80925820Seric 		CheckAliases = atobool(val);
81025820Seric 		break;
81125820Seric 
81216143Seric # ifdef DAEMON
81316143Seric 	  case 'N':		/* home (local?) network name */
81416143Seric 		NetName = newstr(val);
81516143Seric 		break;
81616143Seric # endif DAEMON
81716143Seric 
8188269Seric 	  case 'o':		/* assume old style headers */
8199381Seric 		if (atobool(val))
8209341Seric 			CurEnv->e_flags |= EF_OLDSTYLE;
8219341Seric 		else
8229341Seric 			CurEnv->e_flags &= ~EF_OLDSTYLE;
8238269Seric 		break;
8248269Seric 
82524944Seric 	  case 'P':		/* postmaster copy address for returned mail */
82624944Seric 		PostMasterCopy = newstr(val);
82724944Seric 		break;
82824944Seric 
82924944Seric 	  case 'q':		/* slope of queue only function */
83024944Seric 		QueueFactor = atoi(val);
83124944Seric 		break;
83224944Seric 
8338256Seric 	  case 'Q':		/* queue directory */
8349381Seric 		if (val[0] == '\0')
8358269Seric 			QueueDir = "mqueue";
8369381Seric 		else
8379381Seric 			QueueDir = newstr(val);
8388256Seric 		break;
8398256Seric 
8408256Seric 	  case 'r':		/* read timeout */
8419381Seric 		ReadTimeout = convtime(val);
8428256Seric 		break;
8438256Seric 
8448256Seric 	  case 'S':		/* status file */
8459381Seric 		if (val[0] == '\0')
8468269Seric 			StatFile = "sendmail.st";
8479381Seric 		else
8489381Seric 			StatFile = newstr(val);
8498256Seric 		break;
8508256Seric 
8518265Seric 	  case 's':		/* be super safe, even if expensive */
8529381Seric 		SuperSafe = atobool(val);
8538256Seric 		break;
8548256Seric 
8558256Seric 	  case 'T':		/* queue timeout */
8569381Seric 		TimeOut = convtime(val);
85733936Sbostic 		/*FALLTHROUGH*/
8588256Seric 
8598265Seric 	  case 't':		/* time zone name */
8608265Seric 		break;
8618265Seric 
86250556Seric 	  case 'U':		/* location of user database */
86350556Seric 		UdbFileName = newstr(val);
86450556Seric 		break;
86550556Seric 
8668256Seric 	  case 'u':		/* set default uid */
86717474Seric 		DefUid = atoi(val);
86840973Sbostic 		setdefuser();
8698256Seric 		break;
8708256Seric 
8718269Seric 	  case 'v':		/* run in verbose mode */
8729381Seric 		Verbose = atobool(val);
8738256Seric 		break;
8748256Seric 
87550537Seric 	  case 'w':		/* we have wildcard MX records */
87650537Seric 		WildcardMX = atobool(val);
87750537Seric 		break;
87850537Seric 
87914879Seric 	  case 'x':		/* load avg at which to auto-queue msgs */
88014879Seric 		QueueLA = atoi(val);
88114879Seric 		break;
88214879Seric 
88314879Seric 	  case 'X':		/* load avg at which to auto-reject connections */
88414879Seric 		RefuseLA = atoi(val);
88514879Seric 		break;
88614879Seric 
88724981Seric 	  case 'y':		/* work recipient factor */
88824981Seric 		WkRecipFact = atoi(val);
88924981Seric 		break;
89024981Seric 
89124981Seric 	  case 'Y':		/* fork jobs during queue runs */
89224952Seric 		ForkQueueRuns = atobool(val);
89324952Seric 		break;
89424952Seric 
89524981Seric 	  case 'z':		/* work message class factor */
89624981Seric 		WkClassFact = atoi(val);
89724981Seric 		break;
89824981Seric 
89924981Seric 	  case 'Z':		/* work time factor */
90024981Seric 		WkTimeFact = atoi(val);
90124981Seric 		break;
90224981Seric 
9038256Seric 	  default:
9048256Seric 		break;
9058256Seric 	}
90616878Seric 	if (sticky)
90716878Seric 		setbitn(opt, StickyOpt);
9089188Seric 	return;
9098256Seric }
91010687Seric /*
91110687Seric **  SETCLASS -- set a word into a class
91210687Seric **
91310687Seric **	Parameters:
91410687Seric **		class -- the class to put the word in.
91510687Seric **		word -- the word to enter
91610687Seric **
91710687Seric **	Returns:
91810687Seric **		none.
91910687Seric **
92010687Seric **	Side Effects:
92110687Seric **		puts the word into the symbol table.
92210687Seric */
92310687Seric 
92410687Seric setclass(class, word)
92510687Seric 	int class;
92610687Seric 	char *word;
92710687Seric {
92810687Seric 	register STAB *s;
92910687Seric 
93010687Seric 	s = stab(word, ST_CLASS, ST_ENTER);
93110687Seric 	setbitn(class, s->s_class);
93210687Seric }
933