xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 10343)
13313Seric # include "sendmail.h"
23308Seric 
3*10343Seric SCCSID(@(#)readcf.c	3.51		01/16/83);
43308Seric 
53308Seric /*
63308Seric **  READCF -- read control file.
73308Seric **
83308Seric **	This routine reads the control file and builds the internal
93308Seric **	form.
103308Seric **
114432Seric **	The file is formatted as a sequence of lines, each taken
124432Seric **	atomically.  The first character of each line describes how
134432Seric **	the line is to be interpreted.  The lines are:
144432Seric **		Dxval		Define macro x to have value val.
154432Seric **		Cxword		Put word into class x.
164432Seric **		Fxfile [fmt]	Read file for lines to put into
174432Seric **				class x.  Use scanf string 'fmt'
184432Seric **				or "%s" if not present.  Fmt should
194432Seric **				only produce one string-valued result.
204432Seric **		Hname: value	Define header with field-name 'name'
214432Seric **				and value as specified; this will be
224432Seric **				macro expanded immediately before
234432Seric **				use.
244432Seric **		Sn		Use rewriting set n.
254432Seric **		Rlhs rhs	Rewrite addresses that match lhs to
264432Seric **				be rhs.
278252Seric **		Mn p f s r a	Define mailer.  n - internal name,
288252Seric **				p - pathname, f - flags, s - rewriting
298252Seric **				ruleset for sender, s - rewriting ruleset
308252Seric **				for recipients, a - argument vector.
318252Seric **		Oxvalue		Set option x to value.
328252Seric **		Pname=value	Set precedence name to value.
334432Seric **
343308Seric **	Parameters:
353308Seric **		cfname -- control file name.
364217Seric **		safe -- set if this is a system configuration file.
374217Seric **			Non-system configuration files can not do
384217Seric **			certain things (e.g., leave the SUID bit on
394217Seric **			when executing mailers).
403308Seric **
413308Seric **	Returns:
423308Seric **		none.
433308Seric **
443308Seric **	Side Effects:
453308Seric **		Builds several internal tables.
463308Seric */
473308Seric 
484217Seric readcf(cfname, safe)
493308Seric 	char *cfname;
504217Seric 	bool safe;
513308Seric {
523308Seric 	FILE *cf;
538547Seric 	int class;
548547Seric 	int ruleset = 0;
558547Seric 	char *q;
568547Seric 	char **pv;
579350Seric 	struct rewrite *rwp = NULL;
583308Seric 	char buf[MAXLINE];
593308Seric 	register char *p;
603308Seric 	extern char **prescan();
613308Seric 	extern char **copyplist();
625909Seric 	char exbuf[MAXLINE];
639350Seric 	extern char *fgetfolded();
643308Seric 
653308Seric 	cf = fopen(cfname, "r");
663308Seric 	if (cf == NULL)
673308Seric 	{
683308Seric 		syserr("cannot open %s", cfname);
693308Seric 		exit(EX_OSFILE);
703308Seric 	}
713308Seric 
729381Seric 	FileName = cfname;
738056Seric 	LineNumber = 0;
747854Seric 	while (fgetfolded(buf, sizeof buf, cf) != NULL)
753308Seric 	{
763308Seric 		switch (buf[0])
773308Seric 		{
783308Seric 		  case '\0':
793308Seric 		  case '#':		/* comment */
803308Seric 			break;
813308Seric 
823308Seric 		  case 'R':		/* rewriting rule */
833308Seric 			for (p = &buf[1]; *p != '\0' && *p != '\t'; p++)
843308Seric 				continue;
853308Seric 
863308Seric 			if (*p == '\0')
875909Seric 			{
889381Seric 				syserr("invalid rewrite line \"%s\"", buf);
895909Seric 				break;
905909Seric 			}
915909Seric 
925909Seric 			/* allocate space for the rule header */
935909Seric 			if (rwp == NULL)
945909Seric 			{
955909Seric 				RewriteRules[ruleset] = rwp =
965909Seric 					(struct rewrite *) xalloc(sizeof *rwp);
975909Seric 			}
983308Seric 			else
993308Seric 			{
1005909Seric 				rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp);
1015909Seric 				rwp = rwp->r_next;
1025909Seric 			}
1035909Seric 			rwp->r_next = NULL;
1043308Seric 
1055909Seric 			/* expand and save the LHS */
1065909Seric 			*p = '\0';
1076991Seric 			expand(&buf[1], exbuf, &exbuf[sizeof exbuf], CurEnv);
1085909Seric 			rwp->r_lhs = prescan(exbuf, '\t');
1095909Seric 			if (rwp->r_lhs != NULL)
1105909Seric 				rwp->r_lhs = copyplist(rwp->r_lhs, TRUE);
1115909Seric 
1125909Seric 			/* expand and save the RHS */
1135909Seric 			while (*++p == '\t')
1145909Seric 				continue;
1157231Seric 			q = p;
1167231Seric 			while (*p != '\0' && *p != '\t')
1177231Seric 				p++;
1187231Seric 			*p = '\0';
1197231Seric 			expand(q, exbuf, &exbuf[sizeof exbuf], CurEnv);
1205909Seric 			rwp->r_rhs = prescan(exbuf, '\t');
1215909Seric 			if (rwp->r_rhs != NULL)
1225909Seric 				rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
1233308Seric 			break;
1243308Seric 
1254072Seric 		  case 'S':		/* select rewriting set */
1264072Seric 			ruleset = atoi(&buf[1]);
1278056Seric 			if (ruleset >= MAXRWSETS || ruleset < 0)
1288056Seric 			{
1299381Seric 				syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS);
1308056Seric 				ruleset = 0;
1318056Seric 			}
1324072Seric 			rwp = NULL;
1334072Seric 			break;
1344072Seric 
1353308Seric 		  case 'D':		/* macro definition */
1369381Seric 			define(buf[1], newstr(&buf[2]), CurEnv);
1373308Seric 			break;
1383308Seric 
1393387Seric 		  case 'H':		/* required header line */
1404088Seric 			(void) chompheader(&buf[1], TRUE);
1413387Seric 			break;
1423387Seric 
1434061Seric 		  case 'C':		/* word class */
1444432Seric 		  case 'F':		/* word class from file */
1454061Seric 			class = buf[1];
1464061Seric 			if (!isalpha(class))
1479381Seric 			{
1489381Seric 				syserr("illegal class name %c", class);
1499381Seric 				break;
1509381Seric 			}
1514061Seric 			if (isupper(class))
1524061Seric 				class -= 'A';
1534061Seric 			else
1544061Seric 				class -= 'a';
1554432Seric 
1564432Seric 			/* read list of words from argument or file */
1574432Seric 			if (buf[0] == 'F')
1584432Seric 			{
1594432Seric 				/* read from file */
1604432Seric 				for (p = &buf[2]; *p != '\0' && !isspace(*p); p++)
1614432Seric 					continue;
1624432Seric 				if (*p == '\0')
1634432Seric 					p = "%s";
1644432Seric 				else
1654432Seric 				{
1664432Seric 					*p = '\0';
1674432Seric 					while (isspace(*++p))
1684432Seric 						continue;
1694432Seric 				}
1704432Seric 				fileclass(class, &buf[2], p);
1714432Seric 				break;
1724432Seric 			}
1734061Seric 
1744432Seric 			/* scan the list of words and set class for all */
1754061Seric 			for (p = &buf[2]; *p != '\0'; )
1764061Seric 			{
1774061Seric 				register char *wd;
1784061Seric 				char delim;
1794061Seric 				register STAB *s;
1804061Seric 
1814061Seric 				while (*p != '\0' && isspace(*p))
1824061Seric 					p++;
1834061Seric 				wd = p;
1844061Seric 				while (*p != '\0' && !isspace(*p))
1854061Seric 					p++;
1864061Seric 				delim = *p;
1874061Seric 				*p = '\0';
1884061Seric 				if (wd[0] != '\0')
1894061Seric 				{
1904103Seric 					s = stab(wd, ST_CLASS, ST_ENTER);
1916275Seric 					s->s_class |= 1L << class;
1924061Seric 				}
1934061Seric 				*p = delim;
1944061Seric 			}
1954061Seric 			break;
1964061Seric 
1974096Seric 		  case 'M':		/* define mailer */
1984217Seric 			makemailer(&buf[1], safe);
1994096Seric 			break;
2004096Seric 
2018252Seric 		  case 'O':		/* set option */
2028269Seric 			setoption(buf[1], &buf[2], safe, FALSE);
2038252Seric 			break;
2048252Seric 
2058252Seric 		  case 'P':		/* set precedence */
2068252Seric 			if (NumPriorities >= MAXPRIORITIES)
2078252Seric 			{
2088547Seric 				toomany('P', MAXPRIORITIES);
2098252Seric 				break;
2108252Seric 			}
2119381Seric 			for (p = &buf[1]; *p != '\0' && *p != '=' && *p != '\t'; p++)
2128252Seric 				continue;
2138252Seric 			if (*p == '\0')
2148252Seric 				goto badline;
2158252Seric 			*p = '\0';
2168252Seric 			Priorities[NumPriorities].pri_name = newstr(&buf[1]);
2178252Seric 			Priorities[NumPriorities].pri_val = atoi(++p);
2188252Seric 			NumPriorities++;
2198252Seric 			break;
2208252Seric 
2218547Seric 		  case 'T':		/* trusted user(s) */
2228547Seric 			p = &buf[1];
2238547Seric 			while (*p != '\0')
2248547Seric 			{
2258547Seric 				while (isspace(*p))
2268547Seric 					p++;
2278547Seric 				q = p;
2288547Seric 				while (*p != '\0' && !isspace(*p))
2298547Seric 					p++;
2308547Seric 				if (*p != '\0')
2318547Seric 					*p++ = '\0';
2328547Seric 				if (*q == '\0')
2338547Seric 					continue;
2348547Seric 				for (pv = TrustedUsers; *pv != NULL; pv++)
2358547Seric 					continue;
2368547Seric 				if (pv >= &TrustedUsers[MAXTRUST])
2378547Seric 				{
2388547Seric 					toomany('T', MAXTRUST);
2398547Seric 					break;
2408547Seric 				}
2418547Seric 				*pv = newstr(q);
2428547Seric 			}
2438547Seric 			break;
2448547Seric 
2453308Seric 		  default:
2464061Seric 		  badline:
2479381Seric 			syserr("unknown control line \"%s\"", buf);
2483308Seric 		}
2493308Seric 	}
2509381Seric 	FileName = NULL;
2514096Seric }
2524096Seric /*
2538547Seric **  TOOMANY -- signal too many of some option
2548547Seric **
2558547Seric **	Parameters:
2568547Seric **		id -- the id of the error line
2578547Seric **		maxcnt -- the maximum possible values
2588547Seric **
2598547Seric **	Returns:
2608547Seric **		none.
2618547Seric **
2628547Seric **	Side Effects:
2638547Seric **		gives a syserr.
2648547Seric */
2658547Seric 
2668547Seric toomany(id, maxcnt)
2678547Seric 	char id;
2688547Seric 	int maxcnt;
2698547Seric {
2709381Seric 	syserr("too many %c lines, %d max", id, maxcnt);
2718547Seric }
2728547Seric /*
2734432Seric **  FILECLASS -- read members of a class from a file
2744432Seric **
2754432Seric **	Parameters:
2764432Seric **		class -- class to define.
2774432Seric **		filename -- name of file to read.
2784432Seric **		fmt -- scanf string to use for match.
2794432Seric **
2804432Seric **	Returns:
2814432Seric **		none
2824432Seric **
2834432Seric **	Side Effects:
2844432Seric **
2854432Seric **		puts all lines in filename that match a scanf into
2864432Seric **			the named class.
2874432Seric */
2884432Seric 
2894432Seric fileclass(class, filename, fmt)
2904432Seric 	int class;
2914432Seric 	char *filename;
2924432Seric 	char *fmt;
2934432Seric {
2944432Seric 	register FILE *f;
2954432Seric 	char buf[MAXLINE];
2964432Seric 
2974432Seric 	f = fopen(filename, "r");
2984432Seric 	if (f == NULL)
2994432Seric 	{
3004432Seric 		syserr("cannot open %s", filename);
3014432Seric 		return;
3024432Seric 	}
3034432Seric 
3044432Seric 	while (fgets(buf, sizeof buf, f) != NULL)
3054432Seric 	{
3064432Seric 		register STAB *s;
3074432Seric 		char wordbuf[MAXNAME+1];
3084432Seric 
3094432Seric 		if (sscanf(buf, fmt, wordbuf) != 1)
3104432Seric 			continue;
3114432Seric 		s = stab(wordbuf, ST_CLASS, ST_ENTER);
3126275Seric 		s->s_class |= 1L << class;
3134432Seric 	}
3144432Seric 
3154627Seric 	(void) fclose(f);
3164432Seric }
3174432Seric /*
3184096Seric **  MAKEMAILER -- define a new mailer.
3194096Seric **
3204096Seric **	Parameters:
32110327Seric **		line -- description of mailer.  This is in labeled
32210327Seric **			fields.  The fields are:
32310327Seric **			   P -- the path to the mailer
32410327Seric **			   F -- the flags associated with the mailer
32510327Seric **			   A -- the argv for this mailer
32610327Seric **			   S -- the sender rewriting set
32710327Seric **			   R -- the recipient rewriting set
32810327Seric **			   E -- the eol string
32910327Seric **			The first word is the canonical name of the mailer.
3304217Seric **		safe -- set if this is a safe configuration file.
3314096Seric **
3324096Seric **	Returns:
3334096Seric **		none.
3344096Seric **
3354096Seric **	Side Effects:
3364096Seric **		enters the mailer into the mailer table.
3374096Seric */
3383308Seric 
3394217Seric makemailer(line, safe)
3404096Seric 	char *line;
3414217Seric 	bool safe;
3424096Seric {
3434096Seric 	register char *p;
3448067Seric 	register struct mailer *m;
3458067Seric 	register STAB *s;
3468067Seric 	int i;
34710327Seric 	char fcode;
3484627Seric 	extern u_long mfencode();
3494096Seric 	extern int NextMailer;
35010327Seric 	extern char **makeargv();
35110327Seric 	extern char *munchstring();
35210327Seric 	extern char *DelimChar;
3534096Seric 
35410327Seric 	/* allocate a mailer and set up defaults */
35510327Seric 	m = (struct mailer *) xalloc(sizeof *m);
35610327Seric 	bzero((char *) m, sizeof *m);
35710327Seric 	m->m_mno = NextMailer;
35810327Seric 	m->m_eol = "\n";
35910327Seric 
36010327Seric 	/* collect the mailer name */
36110327Seric 	for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++)
36210327Seric 		continue;
36310327Seric 	if (*p != '\0')
36410327Seric 		*p++ = '\0';
36510327Seric 	m->m_name = newstr(line);
36610327Seric 
36710327Seric 	/* now scan through and assign info from the fields */
36810327Seric 	while (*p != '\0')
36910327Seric 	{
37010327Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
37110327Seric 			p++;
37210327Seric 
37310327Seric 		/* p now points to field code */
37410327Seric 		fcode = *p;
37510327Seric 		while (*p != '\0' && *p != '=' && *p != ',')
37610327Seric 			p++;
37710327Seric 		if (*p++ != '=')
37810327Seric 		{
37910327Seric 			syserr("`=' expected");
38010327Seric 			return;
38110327Seric 		}
38210327Seric 		while (isspace(*p))
38310327Seric 			p++;
38410327Seric 
38510327Seric 		/* p now points to the field body */
38610327Seric 		p = munchstring(p);
38710327Seric 
38810327Seric 		/* install the field into the mailer struct */
38910327Seric 		switch (fcode)
39010327Seric 		{
39110327Seric 		  case 'P':		/* pathname */
39210327Seric 			m->m_mailer = newstr(p);
39310327Seric 			break;
39410327Seric 
39510327Seric 		  case 'F':		/* flags */
39610327Seric 			m->m_flags = mfencode(p);
39710327Seric 			if (!safe)
39810327Seric 				m->m_flags &= ~M_RESTR;
39910327Seric 			break;
40010327Seric 
40110327Seric 		  case 'S':		/* sender rewriting ruleset */
40210327Seric 		  case 'R':		/* recipient rewriting ruleset */
40310327Seric 			i = atoi(p);
40410327Seric 			if (i < 0 || i >= MAXRWSETS)
40510327Seric 			{
40610327Seric 				syserr("invalid rewrite set, %d max", MAXRWSETS);
40710327Seric 				return;
40810327Seric 			}
40910327Seric 			if (fcode == 'S')
41010327Seric 				m->m_s_rwset = i;
41110327Seric 			else
41210327Seric 				m->m_r_rwset = i;
41310327Seric 			break;
41410327Seric 
41510327Seric 		  case 'E':		/* end of line string */
41610327Seric 			m->m_eol = newstr(p);
41710327Seric 			break;
41810327Seric 
41910327Seric 		  case 'A':		/* argument vector */
42010327Seric 			m->m_argv = makeargv(p);
42110327Seric 			break;
42210327Seric 		}
42310327Seric 
42410327Seric 		p = DelimChar;
42510327Seric 	}
42610327Seric 
42710327Seric 	/* now store the mailer away */
4284096Seric 	if (NextMailer >= MAXMAILERS)
4294096Seric 	{
4309381Seric 		syserr("too many mailers defined (%d max)", MAXMAILERS);
4314096Seric 		return;
4324096Seric 	}
43310327Seric 	Mailer[NextMailer++] = m;
43410327Seric 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
43510327Seric 	s->s_mailer = m;
43610327Seric }
43710327Seric /*
43810327Seric **  MUNCHSTRING -- translate a string into internal form.
43910327Seric **
44010327Seric **	Parameters:
44110327Seric **		p -- the string to munch.
44210327Seric **
44310327Seric **	Returns:
44410327Seric **		the munched string.
44510327Seric **
44610327Seric **	Side Effects:
44710327Seric **		Sets "DelimChar" to point to the string that caused us
44810327Seric **		to stop.
44910327Seric */
4504096Seric 
45110327Seric char *
45210327Seric munchstring(p)
45310327Seric 	register char *p;
45410327Seric {
45510327Seric 	register char *q;
45610327Seric 	bool backslash = FALSE;
45710327Seric 	bool quotemode = FALSE;
45810327Seric 	static char buf[MAXLINE];
45910327Seric 	extern char *DelimChar;
4604096Seric 
46110327Seric 	for (q = buf; *p != '\0'; p++)
4624096Seric 	{
46310327Seric 		if (backslash)
46410327Seric 		{
46510327Seric 			/* everything is roughly literal */
46610327Seric 			switch (*p)
46710327Seric 			{
46810327Seric 			  case 'r':		/* carriage return */
46910327Seric 				*q++ = '\r';
47010327Seric 				continue;
47110327Seric 
47210327Seric 			  case 'n':		/* newline */
47310327Seric 				*q++ = '\n';
47410327Seric 				continue;
47510327Seric 
47610327Seric 			  case 'f':		/* form feed */
47710327Seric 				*q++ = '\f';
47810327Seric 				continue;
47910327Seric 
48010327Seric 			  case 'b':		/* backspace */
48110327Seric 				*q++ = '\b';
48210327Seric 				continue;
48310327Seric 			}
48410327Seric 			*q++ = *p;
48510327Seric 			backslash = FALSE;
48610327Seric 		}
48710327Seric 		else
48810327Seric 		{
48910327Seric 			if (*p == '\\')
49010327Seric 				backslash = TRUE;
49110327Seric 			else if (*p == '"')
49210327Seric 				quotemode = !quotemode;
49310327Seric 			else if (quotemode || *p != ',')
49410327Seric 				*q++ = *p;
49510327Seric 			else
49610327Seric 				break;
49710327Seric 		}
4984096Seric 	}
4994096Seric 
50010327Seric 	DelimChar = p;
50110327Seric 	*q++ = '\0';
50210327Seric 	return (buf);
50310327Seric }
50410327Seric /*
50510327Seric **  MAKEARGV -- break up a string into words
50610327Seric **
50710327Seric **	Parameters:
50810327Seric **		p -- the string to break up.
50910327Seric **
51010327Seric **	Returns:
51110327Seric **		a char **argv (dynamically allocated)
51210327Seric **
51310327Seric **	Side Effects:
51410327Seric **		munges p.
51510327Seric */
5164096Seric 
51710327Seric char **
51810327Seric makeargv(p)
51910327Seric 	register char *p;
52010327Seric {
52110327Seric 	char *q;
52210327Seric 	int i;
52310327Seric 	char **avp;
52410327Seric 	char *argv[MAXPV + 1];
52510327Seric 
52610327Seric 	/* take apart the words */
52710327Seric 	i = 0;
52810327Seric 	while (*p != '\0' && i < MAXPV)
5294096Seric 	{
53010327Seric 		q = p;
53110327Seric 		while (*p != '\0' && !isspace(*p))
53210327Seric 			p++;
53310327Seric 		while (isspace(*p))
53410327Seric 			*p++ = '\0';
53510327Seric 		argv[i++] = newstr(q);
5364096Seric 	}
53710327Seric 	argv[i++] = NULL;
5384096Seric 
53910327Seric 	/* now make a copy of the argv */
54010327Seric 	avp = (char **) xalloc(sizeof *avp * i);
54110327Seric 	bmove((char *) argv, (char *) avp, sizeof *avp * i);
54210327Seric 
54310327Seric 	return (avp);
5443308Seric }
5453308Seric /*
5463308Seric **  PRINTRULES -- print rewrite rules (for debugging)
5473308Seric **
5483308Seric **	Parameters:
5493308Seric **		none.
5503308Seric **
5513308Seric **	Returns:
5523308Seric **		none.
5533308Seric **
5543308Seric **	Side Effects:
5553308Seric **		prints rewrite rules.
5563308Seric */
5573308Seric 
5584319Seric # ifdef DEBUG
5594319Seric 
5603308Seric printrules()
5613308Seric {
5623308Seric 	register struct rewrite *rwp;
5634072Seric 	register int ruleset;
5643308Seric 
5654072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
5663308Seric 	{
5674072Seric 		if (RewriteRules[ruleset] == NULL)
5684072Seric 			continue;
5698067Seric 		printf("\n----Rule Set %d:", ruleset);
5703308Seric 
5714072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
5723308Seric 		{
5738067Seric 			printf("\nLHS:");
5748067Seric 			printav(rwp->r_lhs);
5758067Seric 			printf("RHS:");
5768067Seric 			printav(rwp->r_rhs);
5773308Seric 		}
5783308Seric 	}
5793308Seric }
5804319Seric 
5814319Seric # endif DEBUG
5824096Seric /*
5834627Seric **  MFENCODE -- crack mailer options
5844096Seric **
5854096Seric **	These options modify the functioning of the mailer
5864096Seric **	from the configuration table.
5874096Seric **
5884096Seric **	Parameters:
5894096Seric **		p -- pointer to vector of options.
5904096Seric **
5914096Seric **	Returns:
5924096Seric **		option list in binary.
5934096Seric **
5944096Seric **	Side Effects:
5954096Seric **		none.
5964096Seric */
5974096Seric 
5984096Seric struct optlist
5994096Seric {
6004096Seric 	char	opt_name;	/* external name of option */
6016275Seric 	u_long	opt_value;	/* internal name of option */
6024096Seric };
6034096Seric struct optlist	OptList[] =
6044096Seric {
60510174Seric 	'A',	M_ARPAFMT,
60610174Seric 	'C',	M_CANONICAL,
60710174Seric 	'D',	M_NEEDDATE,
60810174Seric 	'e',	M_EXPENSIVE,
60910174Seric 	'F',	M_NEEDFROM,
6104096Seric 	'f',	M_FOPT,
61110174Seric 	'h',	M_HST_UPPER,
61210174Seric 	'I',	M_INTERNAL,
61310174Seric 	'L',	M_LIMITS,
61410174Seric 	'l',	M_LOCAL,
61510174Seric 	'M',	M_MSGID,
61610174Seric 	'm',	M_MUSER,
61710174Seric 	'n',	M_NHDR,
61810174Seric 	'P',	M_RPATH,
61910174Seric 	'p',	M_FROMPATH,
6204096Seric 	'r',	M_ROPT,
6214096Seric 	'S',	M_RESTR,
6224096Seric 	's',	M_STRIPQ,
62310174Seric 	'U',	M_UGLYUUCP,
6244096Seric 	'u',	M_USR_UPPER,
6254096Seric 	'x',	M_FULLNAME,
62610174Seric 	'X',	M_XDOT,
6274319Seric 	'\0',	0
6284096Seric };
6294096Seric 
6304627Seric u_long
6314627Seric mfencode(p)
6324096Seric 	register char *p;
6334096Seric {
6344096Seric 	register struct optlist *o;
6354627Seric 	register u_long opts = 0;
6364096Seric 
6374096Seric 	while (*p != '\0')
6384096Seric 	{
6394096Seric 		for (o = OptList; o->opt_name != '\0' && o->opt_name != *p; o++)
6404096Seric 			continue;
6414096Seric 		opts |= o->opt_value;
6424096Seric 		p++;
6434096Seric 	}
6444096Seric 	return (opts);
6454096Seric }
6464627Seric /*
6474627Seric **  MFDECODE -- decode mailer flags into external form.
6484627Seric **
6494627Seric **	Parameters:
6504627Seric **		flags -- value of flags to decode.
6514627Seric **		f -- file to write them onto.
6524627Seric **
6534627Seric **	Returns:
6544627Seric **		none.
6554627Seric **
6564627Seric **	Side Effects:
6574627Seric **		none.
6584627Seric */
6594627Seric 
6604627Seric mfdecode(flags, f)
6614627Seric 	u_long flags;
6624627Seric 	FILE *f;
6634627Seric {
6644627Seric 	register struct optlist *o;
6654627Seric 
6664627Seric 	putc('?', f);
6674627Seric 	for (o = OptList; o->opt_name != '\0'; o++)
6684627Seric 	{
6694627Seric 		if ((o->opt_value & flags) == o->opt_value)
6704627Seric 		{
6714627Seric 			flags &= ~o->opt_value;
6724627Seric 			putc(o->opt_name, f);
6734627Seric 		}
6744627Seric 	}
6754627Seric 	putc('?', f);
6764627Seric }
6778256Seric /*
6788256Seric **  SETOPTION -- set global processing option
6798256Seric **
6808256Seric **	Parameters:
6818256Seric **		opt -- option name.
6828256Seric **		val -- option value (as a text string).
6838269Seric **		safe -- if set, this came from a system configuration file.
6848269Seric **		sticky -- if set, don't let other setoptions override
6858269Seric **			this value.
6868256Seric **
6878256Seric **	Returns:
6888256Seric **		none.
6898256Seric **
6908256Seric **	Side Effects:
6918256Seric **		Sets options as implied by the arguments.
6928256Seric */
6938256Seric 
6948544Seric static int	StickyOpt[128 / sizeof (int)];	/* set if option is stuck */
6958544Seric extern char	*WizWord;			/* the stored wizard password */
696*10343Seric #ifdef DAEMON
697*10343Seric extern int	MaxConnections;			/* max simult. SMTP conns */
698*10343Seric #endif DAEMON
6998269Seric 
7008269Seric setoption(opt, val, safe, sticky)
7018256Seric 	char opt;
7028256Seric 	char *val;
7038269Seric 	bool safe;
7048269Seric 	bool sticky;
7058256Seric {
7068269Seric 	int smask;
7078269Seric 	int sindex;
7088265Seric 	extern bool atobool();
7098256Seric 
7108256Seric # ifdef DEBUG
7118256Seric 	if (tTd(37, 1))
7129341Seric 		printf("setoption %c=%s", opt, val);
7138256Seric # endif DEBUG
7148256Seric 
7158256Seric 	/*
7168269Seric 	**  See if this option is preset for us.
7178256Seric 	*/
7188256Seric 
7198269Seric 	sindex = opt;
7208269Seric 	smask = 1 << (sindex % sizeof (int));
7218269Seric 	sindex /= sizeof (int);
7228269Seric 	if (bitset(smask, StickyOpt[sindex]))
7238269Seric 	{
7248269Seric # ifdef DEBUG
7259341Seric 		if (tTd(37, 1))
7269341Seric 			printf(" (ignored)\n");
7278269Seric # endif DEBUG
7288269Seric 		return;
7298269Seric 	}
7309341Seric #ifdef DEBUG
7319341Seric 	else if (tTd(37, 1))
7329341Seric 		printf("\n");
7339341Seric #endif DEBUG
7348269Seric 	if (sticky)
7358269Seric 		StickyOpt[sindex] |= smask;
7368269Seric 
7378269Seric 	if (getruid() == 0)
7388269Seric 		safe = TRUE;
7398269Seric 
7408256Seric 	switch (opt)
7418256Seric 	{
7428256Seric 	  case 'A':		/* set default alias file */
7439381Seric 		if (val[0] == '\0')
7448269Seric 			AliasFile = "aliases";
7459381Seric 		else
7469381Seric 			AliasFile = newstr(val);
7478256Seric 		break;
7488256Seric 
7498931Seric 	  case 'a':		/* look for "@:@" in alias file */
7509381Seric 		SafeAlias = atobool(val);
7518931Seric 		break;
7528931Seric 
7539284Seric 	  case 'c':		/* don't connect to "expensive" mailers */
7549381Seric 		NoConnect = atobool(val);
7559284Seric 		break;
7569284Seric 
7579284Seric 	  case 'd':		/* delivery mode */
7589284Seric 		switch (*val)
7598269Seric 		{
7609284Seric 		  case '\0':
7619284Seric 			SendMode = SM_DELIVER;
7628269Seric 			break;
7638269Seric 
7649284Seric 		  case SM_DELIVER:	/* do everything */
7659284Seric 		  case SM_FORK:		/* fork after verification */
7669284Seric 		  case SM_QUEUE:	/* queue only */
7679284Seric 			SendMode = *val;
7688269Seric 			break;
7698269Seric 
7708269Seric 		  default:
7719284Seric 			syserr("Unknown delivery mode %c", *val);
7728269Seric 			exit(EX_USAGE);
7738269Seric 		}
7748269Seric 		break;
7758269Seric 
7769146Seric 	  case 'D':		/* rebuild alias database as needed */
7779381Seric 		AutoRebuild = atobool(val);
7789146Seric 		break;
7799146Seric 
7808269Seric 	  case 'e':		/* set error processing mode */
7818269Seric 		switch (*val)
7828269Seric 		{
7839381Seric 		  case EM_QUIET:	/* be silent about it */
7848269Seric 			(void) freopen("/dev/null", "w", stdout);
7859381Seric 			/* fall through... */
7868269Seric 
7879381Seric 		  case EM_MAIL:		/* mail back */
7889381Seric 		  case EM_BERKNET:	/* do berknet error processing */
7899381Seric 		  case EM_WRITE:	/* write back (or mail) */
7908269Seric 			HoldErrs = TRUE;
7919381Seric 			/* fall through... */
7928269Seric 
7939381Seric 		  case EM_PRINT:	/* print errors normally (default) */
7949381Seric 			ErrorMode = *val;
7958269Seric 			break;
7968269Seric 		}
7978269Seric 		break;
7988269Seric 
7999049Seric 	  case 'F':		/* file mode */
8009381Seric 		FileMode = atooct(val);
8019049Seric 		break;
8029049Seric 
8038269Seric 	  case 'f':		/* save Unix-style From lines on front */
8049381Seric 		SaveFrom = atobool(val);
8058269Seric 		break;
8068269Seric 
8078256Seric 	  case 'g':		/* default gid */
8089105Seric 		if (safe)
8099381Seric 			DefGid = atoi(val);
8108256Seric 		break;
8118256Seric 
8128256Seric 	  case 'H':		/* help file */
8139381Seric 		if (val[0] == '\0')
8148269Seric 			HelpFile = "sendmail.hf";
8159381Seric 		else
8169381Seric 			HelpFile = newstr(val);
8178256Seric 		break;
8188256Seric 
8198269Seric 	  case 'i':		/* ignore dot lines in message */
8209381Seric 		IgnrDot = atobool(val);
8218269Seric 		break;
8228269Seric 
8238256Seric 	  case 'L':		/* log level */
8249381Seric 		LogLevel = atoi(val);
8258256Seric 		break;
8268256Seric 
8278269Seric 	  case 'M':		/* define macro */
8289381Seric 		define(val[0], newstr(&val[1]), CurEnv);
8298269Seric 		break;
8308269Seric 
8318269Seric 	  case 'm':		/* send to me too */
8329381Seric 		MeToo = atobool(val);
8338269Seric 		break;
8348269Seric 
835*10343Seric #ifdef DAEMON
836*10343Seric 	  case 'N':		/* maximum simultaneous SMTP connections */
837*10343Seric 		MaxConnections = atoi(val);
838*10343Seric 		break;
839*10343Seric #endif DAEMON
840*10343Seric 
8418269Seric 	  case 'o':		/* assume old style headers */
8429381Seric 		if (atobool(val))
8439341Seric 			CurEnv->e_flags |= EF_OLDSTYLE;
8449341Seric 		else
8459341Seric 			CurEnv->e_flags &= ~EF_OLDSTYLE;
8468269Seric 		break;
8478269Seric 
8488256Seric 	  case 'Q':		/* queue directory */
8499381Seric 		if (val[0] == '\0')
8508269Seric 			QueueDir = "mqueue";
8519381Seric 		else
8529381Seric 			QueueDir = newstr(val);
8538256Seric 		break;
8548256Seric 
8558256Seric 	  case 'r':		/* read timeout */
8569381Seric 		ReadTimeout = convtime(val);
8578256Seric 		break;
8588256Seric 
8598256Seric 	  case 'S':		/* status file */
8609381Seric 		if (val[0] == '\0')
8618269Seric 			StatFile = "sendmail.st";
8629381Seric 		else
8639381Seric 			StatFile = newstr(val);
8648256Seric 		break;
8658256Seric 
8668265Seric 	  case 's':		/* be super safe, even if expensive */
8679381Seric 		SuperSafe = atobool(val);
8688256Seric 		break;
8698256Seric 
8708256Seric 	  case 'T':		/* queue timeout */
8719381Seric 		TimeOut = convtime(val);
8728256Seric 		break;
8738256Seric 
8748265Seric 	  case 't':		/* time zone name */
8758265Seric # ifdef V6
8769381Seric 		StdTimezone = newstr(val);
8779381Seric 		DstTimezone = index(StdTimeZone, ',');
8788265Seric 		if (DstTimezone == NULL)
8799381Seric 			syserr("bad time zone spec");
8809381Seric 		else
8819381Seric 			*DstTimezone++ = '\0';
8828265Seric # endif V6
8838265Seric 		break;
8848265Seric 
8858256Seric 	  case 'u':		/* set default uid */
8869105Seric 		if (safe)
8879381Seric 			DefUid = atoi(val);
8888256Seric 		break;
8898256Seric 
8908269Seric 	  case 'v':		/* run in verbose mode */
8919381Seric 		Verbose = atobool(val);
8928256Seric 		break;
8938256Seric 
8948544Seric # ifdef DEBUG
8958544Seric 	  case 'W':		/* set the wizards password */
8969105Seric 		if (safe)
8979381Seric 			WizWord = newstr(val);
8988544Seric 		break;
8998544Seric # endif DEBUG
9008544Seric 
9018256Seric 	  default:
9028256Seric 		break;
9038256Seric 	}
9049188Seric 	return;
9058256Seric }
906