xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 52645)
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*52645Seric static char sccsid[] = "@(#)readcf.c	5.33 (Berkeley) 02/24/92";
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.
41*52645Seric **		Vversioncode	Version level of configuration syntax.
424432Seric **
433308Seric **	Parameters:
443308Seric **		cfname -- control file name.
453308Seric **
463308Seric **	Returns:
473308Seric **		none.
483308Seric **
493308Seric **	Side Effects:
503308Seric **		Builds several internal tables.
513308Seric */
523308Seric 
5321066Seric readcf(cfname)
543308Seric 	char *cfname;
553308Seric {
563308Seric 	FILE *cf;
578547Seric 	int ruleset = 0;
588547Seric 	char *q;
598547Seric 	char **pv;
609350Seric 	struct rewrite *rwp = NULL;
613308Seric 	char buf[MAXLINE];
623308Seric 	register char *p;
633308Seric 	extern char **prescan();
643308Seric 	extern char **copyplist();
655909Seric 	char exbuf[MAXLINE];
6616915Seric 	char pvpbuf[PSBUFSIZE];
679350Seric 	extern char *fgetfolded();
6810709Seric 	extern char *munchstring();
693308Seric 
703308Seric 	cf = fopen(cfname, "r");
713308Seric 	if (cf == NULL)
723308Seric 	{
733308Seric 		syserr("cannot open %s", cfname);
743308Seric 		exit(EX_OSFILE);
753308Seric 	}
763308Seric 
779381Seric 	FileName = cfname;
788056Seric 	LineNumber = 0;
797854Seric 	while (fgetfolded(buf, sizeof buf, cf) != NULL)
803308Seric 	{
8152637Seric 		if (buf[0] == '#')
8252637Seric 			continue;
8352637Seric 
8416157Seric 		/* map $ into \001 (ASCII SOH) for macro expansion */
8516157Seric 		for (p = buf; *p != '\0'; p++)
8616157Seric 		{
8716157Seric 			if (*p != '$')
8816157Seric 				continue;
8916157Seric 
9016157Seric 			if (p[1] == '$')
9116157Seric 			{
9216157Seric 				/* actual dollar sign.... */
9323111Seric 				(void) strcpy(p, p + 1);
9416157Seric 				continue;
9516157Seric 			}
9616157Seric 
9716157Seric 			/* convert to macro expansion character */
9816157Seric 			*p = '\001';
9916157Seric 		}
10016157Seric 
10116157Seric 		/* interpret this line */
1023308Seric 		switch (buf[0])
1033308Seric 		{
1043308Seric 		  case '\0':
1053308Seric 		  case '#':		/* comment */
1063308Seric 			break;
1073308Seric 
1083308Seric 		  case 'R':		/* rewriting rule */
1093308Seric 			for (p = &buf[1]; *p != '\0' && *p != '\t'; p++)
1103308Seric 				continue;
1113308Seric 
1123308Seric 			if (*p == '\0')
1135909Seric 			{
1149381Seric 				syserr("invalid rewrite line \"%s\"", buf);
1155909Seric 				break;
1165909Seric 			}
1175909Seric 
1185909Seric 			/* allocate space for the rule header */
1195909Seric 			if (rwp == NULL)
1205909Seric 			{
1215909Seric 				RewriteRules[ruleset] = rwp =
1225909Seric 					(struct rewrite *) xalloc(sizeof *rwp);
1235909Seric 			}
1243308Seric 			else
1253308Seric 			{
1265909Seric 				rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp);
1275909Seric 				rwp = rwp->r_next;
1285909Seric 			}
1295909Seric 			rwp->r_next = NULL;
1303308Seric 
1315909Seric 			/* expand and save the LHS */
1325909Seric 			*p = '\0';
1336991Seric 			expand(&buf[1], exbuf, &exbuf[sizeof exbuf], CurEnv);
13416915Seric 			rwp->r_lhs = prescan(exbuf, '\t', pvpbuf);
1355909Seric 			if (rwp->r_lhs != NULL)
1365909Seric 				rwp->r_lhs = copyplist(rwp->r_lhs, TRUE);
1375909Seric 
1385909Seric 			/* expand and save the RHS */
1395909Seric 			while (*++p == '\t')
1405909Seric 				continue;
1417231Seric 			q = p;
1427231Seric 			while (*p != '\0' && *p != '\t')
1437231Seric 				p++;
1447231Seric 			*p = '\0';
1457231Seric 			expand(q, exbuf, &exbuf[sizeof exbuf], CurEnv);
14616915Seric 			rwp->r_rhs = prescan(exbuf, '\t', pvpbuf);
1475909Seric 			if (rwp->r_rhs != NULL)
1485909Seric 				rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
1493308Seric 			break;
1503308Seric 
1514072Seric 		  case 'S':		/* select rewriting set */
1524072Seric 			ruleset = atoi(&buf[1]);
1538056Seric 			if (ruleset >= MAXRWSETS || ruleset < 0)
1548056Seric 			{
1559381Seric 				syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS);
1568056Seric 				ruleset = 0;
1578056Seric 			}
1584072Seric 			rwp = NULL;
1594072Seric 			break;
1604072Seric 
1613308Seric 		  case 'D':		/* macro definition */
16210709Seric 			define(buf[1], newstr(munchstring(&buf[2])), CurEnv);
1633308Seric 			break;
1643308Seric 
1653387Seric 		  case 'H':		/* required header line */
1664088Seric 			(void) chompheader(&buf[1], TRUE);
1673387Seric 			break;
1683387Seric 
1694061Seric 		  case 'C':		/* word class */
1704432Seric 		  case 'F':		/* word class from file */
1714432Seric 			/* read list of words from argument or file */
1724432Seric 			if (buf[0] == 'F')
1734432Seric 			{
1744432Seric 				/* read from file */
1754432Seric 				for (p = &buf[2]; *p != '\0' && !isspace(*p); p++)
1764432Seric 					continue;
1774432Seric 				if (*p == '\0')
1784432Seric 					p = "%s";
1794432Seric 				else
1804432Seric 				{
1814432Seric 					*p = '\0';
1824432Seric 					while (isspace(*++p))
1834432Seric 						continue;
1844432Seric 				}
18510687Seric 				fileclass(buf[1], &buf[2], p);
1864432Seric 				break;
1874432Seric 			}
1884061Seric 
1894432Seric 			/* scan the list of words and set class for all */
1904061Seric 			for (p = &buf[2]; *p != '\0'; )
1914061Seric 			{
1924061Seric 				register char *wd;
1934061Seric 				char delim;
1944061Seric 
1954061Seric 				while (*p != '\0' && isspace(*p))
1964061Seric 					p++;
1974061Seric 				wd = p;
1984061Seric 				while (*p != '\0' && !isspace(*p))
1994061Seric 					p++;
2004061Seric 				delim = *p;
2014061Seric 				*p = '\0';
2024061Seric 				if (wd[0] != '\0')
20310687Seric 					setclass(buf[1], wd);
2044061Seric 				*p = delim;
2054061Seric 			}
2064061Seric 			break;
2074061Seric 
2084096Seric 		  case 'M':		/* define mailer */
20921066Seric 			makemailer(&buf[1]);
2104096Seric 			break;
2114096Seric 
2128252Seric 		  case 'O':		/* set option */
21321755Seric 			setoption(buf[1], &buf[2], TRUE, FALSE);
2148252Seric 			break;
2158252Seric 
2168252Seric 		  case 'P':		/* set precedence */
2178252Seric 			if (NumPriorities >= MAXPRIORITIES)
2188252Seric 			{
2198547Seric 				toomany('P', MAXPRIORITIES);
2208252Seric 				break;
2218252Seric 			}
2229381Seric 			for (p = &buf[1]; *p != '\0' && *p != '=' && *p != '\t'; p++)
2238252Seric 				continue;
2248252Seric 			if (*p == '\0')
2258252Seric 				goto badline;
2268252Seric 			*p = '\0';
2278252Seric 			Priorities[NumPriorities].pri_name = newstr(&buf[1]);
2288252Seric 			Priorities[NumPriorities].pri_val = atoi(++p);
2298252Seric 			NumPriorities++;
2308252Seric 			break;
2318252Seric 
2328547Seric 		  case 'T':		/* trusted user(s) */
2338547Seric 			p = &buf[1];
2348547Seric 			while (*p != '\0')
2358547Seric 			{
2368547Seric 				while (isspace(*p))
2378547Seric 					p++;
2388547Seric 				q = p;
2398547Seric 				while (*p != '\0' && !isspace(*p))
2408547Seric 					p++;
2418547Seric 				if (*p != '\0')
2428547Seric 					*p++ = '\0';
2438547Seric 				if (*q == '\0')
2448547Seric 					continue;
2458547Seric 				for (pv = TrustedUsers; *pv != NULL; pv++)
2468547Seric 					continue;
2478547Seric 				if (pv >= &TrustedUsers[MAXTRUST])
2488547Seric 				{
2498547Seric 					toomany('T', MAXTRUST);
2508547Seric 					break;
2518547Seric 				}
2528547Seric 				*pv = newstr(q);
2538547Seric 			}
2548547Seric 			break;
2558547Seric 
256*52645Seric 		  case 'V':		/* configuration syntax version */
257*52645Seric 			ConfigLevel = atoi(&buf[1]);
258*52645Seric 			break;
259*52645Seric 
2603308Seric 		  default:
2614061Seric 		  badline:
2629381Seric 			syserr("unknown control line \"%s\"", buf);
2633308Seric 		}
2643308Seric 	}
26552637Seric 	if (ferror(cf))
26652637Seric 	{
26752637Seric 		syserr("Error reading %s", cfname);
26852637Seric 		exit(EX_OSFILE);
26952637Seric 	}
27052637Seric 	fclose(cf);
2719381Seric 	FileName = NULL;
2724096Seric }
2734096Seric /*
2748547Seric **  TOOMANY -- signal too many of some option
2758547Seric **
2768547Seric **	Parameters:
2778547Seric **		id -- the id of the error line
2788547Seric **		maxcnt -- the maximum possible values
2798547Seric **
2808547Seric **	Returns:
2818547Seric **		none.
2828547Seric **
2838547Seric **	Side Effects:
2848547Seric **		gives a syserr.
2858547Seric */
2868547Seric 
2878547Seric toomany(id, maxcnt)
2888547Seric 	char id;
2898547Seric 	int maxcnt;
2908547Seric {
2919381Seric 	syserr("too many %c lines, %d max", id, maxcnt);
2928547Seric }
2938547Seric /*
2944432Seric **  FILECLASS -- read members of a class from a file
2954432Seric **
2964432Seric **	Parameters:
2974432Seric **		class -- class to define.
2984432Seric **		filename -- name of file to read.
2994432Seric **		fmt -- scanf string to use for match.
3004432Seric **
3014432Seric **	Returns:
3024432Seric **		none
3034432Seric **
3044432Seric **	Side Effects:
3054432Seric **
3064432Seric **		puts all lines in filename that match a scanf into
3074432Seric **			the named class.
3084432Seric */
3094432Seric 
3104432Seric fileclass(class, filename, fmt)
3114432Seric 	int class;
3124432Seric 	char *filename;
3134432Seric 	char *fmt;
3144432Seric {
31525808Seric 	FILE *f;
3164432Seric 	char buf[MAXLINE];
3174432Seric 
31852062Seric 	if (filename[0] == '|')
31952062Seric 		f = popen(filename + 1, "r");
32052062Seric 	else
32152062Seric 		f = fopen(filename, "r");
3224432Seric 	if (f == NULL)
3234432Seric 	{
3244432Seric 		syserr("cannot open %s", filename);
3254432Seric 		return;
3264432Seric 	}
3274432Seric 
3284432Seric 	while (fgets(buf, sizeof buf, f) != NULL)
3294432Seric 	{
3304432Seric 		register STAB *s;
33125808Seric 		register char *p;
33225808Seric # ifdef SCANF
3334432Seric 		char wordbuf[MAXNAME+1];
3344432Seric 
3354432Seric 		if (sscanf(buf, fmt, wordbuf) != 1)
3364432Seric 			continue;
33725808Seric 		p = wordbuf;
33825808Seric # else SCANF
33925808Seric 		p = buf;
34025808Seric # endif SCANF
34125808Seric 
34225808Seric 		/*
34325808Seric 		**  Break up the match into words.
34425808Seric 		*/
34525808Seric 
34625808Seric 		while (*p != '\0')
34725808Seric 		{
34825808Seric 			register char *q;
34925808Seric 
35025808Seric 			/* strip leading spaces */
35125808Seric 			while (isspace(*p))
35225808Seric 				p++;
35325808Seric 			if (*p == '\0')
35425808Seric 				break;
35525808Seric 
35625808Seric 			/* find the end of the word */
35725808Seric 			q = p;
35825808Seric 			while (*p != '\0' && !isspace(*p))
35925808Seric 				p++;
36025808Seric 			if (*p != '\0')
36125808Seric 				*p++ = '\0';
36225808Seric 
36325808Seric 			/* enter the word in the symbol table */
36425808Seric 			s = stab(q, ST_CLASS, ST_ENTER);
36525808Seric 			setbitn(class, s->s_class);
36625808Seric 		}
3674432Seric 	}
3684432Seric 
36952062Seric 	if (filename[0] == '|')
37052062Seric 		(void) pclose(f);
37152062Seric 	else
37252062Seric 		(void) fclose(f);
3734432Seric }
3744432Seric /*
3754096Seric **  MAKEMAILER -- define a new mailer.
3764096Seric **
3774096Seric **	Parameters:
37810327Seric **		line -- description of mailer.  This is in labeled
37910327Seric **			fields.  The fields are:
38010327Seric **			   P -- the path to the mailer
38110327Seric **			   F -- the flags associated with the mailer
38210327Seric **			   A -- the argv for this mailer
38310327Seric **			   S -- the sender rewriting set
38410327Seric **			   R -- the recipient rewriting set
38510327Seric **			   E -- the eol string
38610327Seric **			The first word is the canonical name of the mailer.
3874096Seric **
3884096Seric **	Returns:
3894096Seric **		none.
3904096Seric **
3914096Seric **	Side Effects:
3924096Seric **		enters the mailer into the mailer table.
3934096Seric */
3943308Seric 
39521066Seric makemailer(line)
3964096Seric 	char *line;
3974096Seric {
3984096Seric 	register char *p;
3998067Seric 	register struct mailer *m;
4008067Seric 	register STAB *s;
4018067Seric 	int i;
40210327Seric 	char fcode;
4034096Seric 	extern int NextMailer;
40410327Seric 	extern char **makeargv();
40510327Seric 	extern char *munchstring();
40610327Seric 	extern char *DelimChar;
40710701Seric 	extern long atol();
4084096Seric 
40910327Seric 	/* allocate a mailer and set up defaults */
41010327Seric 	m = (struct mailer *) xalloc(sizeof *m);
41110327Seric 	bzero((char *) m, sizeof *m);
41210327Seric 	m->m_mno = NextMailer;
41310327Seric 	m->m_eol = "\n";
41410327Seric 
41510327Seric 	/* collect the mailer name */
41610327Seric 	for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++)
41710327Seric 		continue;
41810327Seric 	if (*p != '\0')
41910327Seric 		*p++ = '\0';
42010327Seric 	m->m_name = newstr(line);
42110327Seric 
42210327Seric 	/* now scan through and assign info from the fields */
42310327Seric 	while (*p != '\0')
42410327Seric 	{
42510327Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
42610327Seric 			p++;
42710327Seric 
42810327Seric 		/* p now points to field code */
42910327Seric 		fcode = *p;
43010327Seric 		while (*p != '\0' && *p != '=' && *p != ',')
43110327Seric 			p++;
43210327Seric 		if (*p++ != '=')
43310327Seric 		{
43452637Seric 			syserr("mailer %s: `=' expected", m->m_name);
43510327Seric 			return;
43610327Seric 		}
43710327Seric 		while (isspace(*p))
43810327Seric 			p++;
43910327Seric 
44010327Seric 		/* p now points to the field body */
44110327Seric 		p = munchstring(p);
44210327Seric 
44310327Seric 		/* install the field into the mailer struct */
44410327Seric 		switch (fcode)
44510327Seric 		{
44610327Seric 		  case 'P':		/* pathname */
44710327Seric 			m->m_mailer = newstr(p);
44810327Seric 			break;
44910327Seric 
45010327Seric 		  case 'F':		/* flags */
45110687Seric 			for (; *p != '\0'; p++)
45252637Seric 				if (!isspace(*p))
45352637Seric 					setbitn(*p, m->m_flags);
45410327Seric 			break;
45510327Seric 
45610327Seric 		  case 'S':		/* sender rewriting ruleset */
45710327Seric 		  case 'R':		/* recipient rewriting ruleset */
45810327Seric 			i = atoi(p);
45910327Seric 			if (i < 0 || i >= MAXRWSETS)
46010327Seric 			{
46110327Seric 				syserr("invalid rewrite set, %d max", MAXRWSETS);
46210327Seric 				return;
46310327Seric 			}
46410327Seric 			if (fcode == 'S')
46510327Seric 				m->m_s_rwset = i;
46610327Seric 			else
46710327Seric 				m->m_r_rwset = i;
46810327Seric 			break;
46910327Seric 
47010327Seric 		  case 'E':		/* end of line string */
47110327Seric 			m->m_eol = newstr(p);
47210327Seric 			break;
47310327Seric 
47410327Seric 		  case 'A':		/* argument vector */
47510327Seric 			m->m_argv = makeargv(p);
47610327Seric 			break;
47710701Seric 
47810701Seric 		  case 'M':		/* maximum message size */
47910701Seric 			m->m_maxsize = atol(p);
48010701Seric 			break;
48152106Seric 
48252106Seric 		  case 'L':		/* maximum line length */
48352106Seric 			m->m_linelimit = atoi(p);
48452106Seric 			break;
48510327Seric 		}
48610327Seric 
48710327Seric 		p = DelimChar;
48810327Seric 	}
48910327Seric 
49052106Seric 	/* do some heuristic cleanup for back compatibility */
49152106Seric 	if (bitnset(M_LIMITS, m->m_flags))
49252106Seric 	{
49352106Seric 		if (m->m_linelimit == 0)
49452106Seric 			m->m_linelimit = SMTPLINELIM;
49552106Seric 		if (!bitnset(M_8BITS, m->m_flags))
49652106Seric 			setbitn(M_7BITS, m->m_flags);
49752106Seric 	}
49852106Seric 
49910327Seric 	/* now store the mailer away */
5004096Seric 	if (NextMailer >= MAXMAILERS)
5014096Seric 	{
5029381Seric 		syserr("too many mailers defined (%d max)", MAXMAILERS);
5034096Seric 		return;
5044096Seric 	}
50510327Seric 	Mailer[NextMailer++] = m;
50610327Seric 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
50710327Seric 	s->s_mailer = m;
50810327Seric }
50910327Seric /*
51010327Seric **  MUNCHSTRING -- translate a string into internal form.
51110327Seric **
51210327Seric **	Parameters:
51310327Seric **		p -- the string to munch.
51410327Seric **
51510327Seric **	Returns:
51610327Seric **		the munched string.
51710327Seric **
51810327Seric **	Side Effects:
51910327Seric **		Sets "DelimChar" to point to the string that caused us
52010327Seric **		to stop.
52110327Seric */
5224096Seric 
52310327Seric char *
52410327Seric munchstring(p)
52510327Seric 	register char *p;
52610327Seric {
52710327Seric 	register char *q;
52810327Seric 	bool backslash = FALSE;
52910327Seric 	bool quotemode = FALSE;
53010327Seric 	static char buf[MAXLINE];
53110327Seric 	extern char *DelimChar;
5324096Seric 
53310327Seric 	for (q = buf; *p != '\0'; p++)
5344096Seric 	{
53510327Seric 		if (backslash)
53610327Seric 		{
53710327Seric 			/* everything is roughly literal */
53810357Seric 			backslash = FALSE;
53910327Seric 			switch (*p)
54010327Seric 			{
54110327Seric 			  case 'r':		/* carriage return */
54210327Seric 				*q++ = '\r';
54310327Seric 				continue;
54410327Seric 
54510327Seric 			  case 'n':		/* newline */
54610327Seric 				*q++ = '\n';
54710327Seric 				continue;
54810327Seric 
54910327Seric 			  case 'f':		/* form feed */
55010327Seric 				*q++ = '\f';
55110327Seric 				continue;
55210327Seric 
55310327Seric 			  case 'b':		/* backspace */
55410327Seric 				*q++ = '\b';
55510327Seric 				continue;
55610327Seric 			}
55710327Seric 			*q++ = *p;
55810327Seric 		}
55910327Seric 		else
56010327Seric 		{
56110327Seric 			if (*p == '\\')
56210327Seric 				backslash = TRUE;
56310327Seric 			else if (*p == '"')
56410327Seric 				quotemode = !quotemode;
56510327Seric 			else if (quotemode || *p != ',')
56610327Seric 				*q++ = *p;
56710327Seric 			else
56810327Seric 				break;
56910327Seric 		}
5704096Seric 	}
5714096Seric 
57210327Seric 	DelimChar = p;
57310327Seric 	*q++ = '\0';
57410327Seric 	return (buf);
57510327Seric }
57610327Seric /*
57710327Seric **  MAKEARGV -- break up a string into words
57810327Seric **
57910327Seric **	Parameters:
58010327Seric **		p -- the string to break up.
58110327Seric **
58210327Seric **	Returns:
58310327Seric **		a char **argv (dynamically allocated)
58410327Seric **
58510327Seric **	Side Effects:
58610327Seric **		munges p.
58710327Seric */
5884096Seric 
58910327Seric char **
59010327Seric makeargv(p)
59110327Seric 	register char *p;
59210327Seric {
59310327Seric 	char *q;
59410327Seric 	int i;
59510327Seric 	char **avp;
59610327Seric 	char *argv[MAXPV + 1];
59710327Seric 
59810327Seric 	/* take apart the words */
59910327Seric 	i = 0;
60010327Seric 	while (*p != '\0' && i < MAXPV)
6014096Seric 	{
60210327Seric 		q = p;
60310327Seric 		while (*p != '\0' && !isspace(*p))
60410327Seric 			p++;
60510327Seric 		while (isspace(*p))
60610327Seric 			*p++ = '\0';
60710327Seric 		argv[i++] = newstr(q);
6084096Seric 	}
60910327Seric 	argv[i++] = NULL;
6104096Seric 
61110327Seric 	/* now make a copy of the argv */
61210327Seric 	avp = (char **) xalloc(sizeof *avp * i);
61316893Seric 	bcopy((char *) argv, (char *) avp, sizeof *avp * i);
61410327Seric 
61510327Seric 	return (avp);
6163308Seric }
6173308Seric /*
6183308Seric **  PRINTRULES -- print rewrite rules (for debugging)
6193308Seric **
6203308Seric **	Parameters:
6213308Seric **		none.
6223308Seric **
6233308Seric **	Returns:
6243308Seric **		none.
6253308Seric **
6263308Seric **	Side Effects:
6273308Seric **		prints rewrite rules.
6283308Seric */
6293308Seric 
6303308Seric printrules()
6313308Seric {
6323308Seric 	register struct rewrite *rwp;
6334072Seric 	register int ruleset;
6343308Seric 
6354072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
6363308Seric 	{
6374072Seric 		if (RewriteRules[ruleset] == NULL)
6384072Seric 			continue;
6398067Seric 		printf("\n----Rule Set %d:", ruleset);
6403308Seric 
6414072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
6423308Seric 		{
6438067Seric 			printf("\nLHS:");
6448067Seric 			printav(rwp->r_lhs);
6458067Seric 			printf("RHS:");
6468067Seric 			printav(rwp->r_rhs);
6473308Seric 		}
6483308Seric 	}
6493308Seric }
6504319Seric 
6514096Seric /*
6528256Seric **  SETOPTION -- set global processing option
6538256Seric **
6548256Seric **	Parameters:
6558256Seric **		opt -- option name.
6568256Seric **		val -- option value (as a text string).
65721755Seric **		safe -- set if this came from a configuration file.
65821755Seric **			Some options (if set from the command line) will
65921755Seric **			reset the user id to avoid security problems.
6608269Seric **		sticky -- if set, don't let other setoptions override
6618269Seric **			this value.
6628256Seric **
6638256Seric **	Returns:
6648256Seric **		none.
6658256Seric **
6668256Seric **	Side Effects:
6678256Seric **		Sets options as implied by the arguments.
6688256Seric */
6698256Seric 
67010687Seric static BITMAP	StickyOpt;		/* set if option is stuck */
6718269Seric 
67221755Seric setoption(opt, val, safe, sticky)
6738256Seric 	char opt;
6748256Seric 	char *val;
67521755Seric 	bool safe;
6768269Seric 	bool sticky;
6778256Seric {
6788265Seric 	extern bool atobool();
67912633Seric 	extern time_t convtime();
68014879Seric 	extern int QueueLA;
68114879Seric 	extern int RefuseLA;
68217474Seric 	extern bool trusteduser();
68317474Seric 	extern char *username();
6848256Seric 
6858256Seric 	if (tTd(37, 1))
6869341Seric 		printf("setoption %c=%s", opt, val);
6878256Seric 
6888256Seric 	/*
6898269Seric 	**  See if this option is preset for us.
6908256Seric 	*/
6918256Seric 
69210687Seric 	if (bitnset(opt, StickyOpt))
6938269Seric 	{
6949341Seric 		if (tTd(37, 1))
6959341Seric 			printf(" (ignored)\n");
6968269Seric 		return;
6978269Seric 	}
6988269Seric 
69921755Seric 	/*
70021755Seric 	**  Check to see if this option can be specified by this user.
70121755Seric 	*/
70221755Seric 
70336238Skarels 	if (!safe && getuid() == 0)
70421755Seric 		safe = TRUE;
70521755Seric 	if (!safe && index("deiLmorsv", opt) == NULL)
70621755Seric 	{
70739111Srick 		if (opt != 'M' || (val[0] != 'r' && val[0] != 's'))
70821755Seric 		{
70936582Sbostic 			if (tTd(37, 1))
71036582Sbostic 				printf(" (unsafe)");
71136582Sbostic 			if (getuid() != geteuid())
71236582Sbostic 			{
71351210Seric 				if (tTd(37, 1))
71451210Seric 					printf("(Resetting uid)");
71536582Sbostic 				(void) setgid(getgid());
71636582Sbostic 				(void) setuid(getuid());
71736582Sbostic 			}
71821755Seric 		}
71921755Seric 	}
72051210Seric 	if (tTd(37, 1))
72117985Seric 		printf("\n");
7228269Seric 
7238256Seric 	switch (opt)
7248256Seric 	{
72551312Seric 	  case '=':		/* config file generation level */
72651312Seric 		ConfigLevel = atoi(val);
72751312Seric 		break;
72851312Seric 
72952106Seric 	  case '8':		/* allow eight-bit input */
73052106Seric 		EightBit = atobool(val);
73152106Seric 		break;
73252106Seric 
7338256Seric 	  case 'A':		/* set default alias file */
7349381Seric 		if (val[0] == '\0')
7358269Seric 			AliasFile = "aliases";
7369381Seric 		else
7379381Seric 			AliasFile = newstr(val);
7388256Seric 		break;
7398256Seric 
74017474Seric 	  case 'a':		/* look N minutes for "@:@" in alias file */
74117474Seric 		if (val[0] == '\0')
74217474Seric 			SafeAlias = 5;
74317474Seric 		else
74417474Seric 			SafeAlias = atoi(val);
74517474Seric 		break;
74617474Seric 
74716843Seric 	  case 'B':		/* substitution for blank character */
74816843Seric 		SpaceSub = val[0];
74916843Seric 		if (SpaceSub == '\0')
75016843Seric 			SpaceSub = ' ';
75116843Seric 		break;
75216843Seric 
7539284Seric 	  case 'c':		/* don't connect to "expensive" mailers */
7549381Seric 		NoConnect = atobool(val);
7559284Seric 		break;
7569284Seric 
75751305Seric 	  case 'C':		/* checkpoint every N addresses */
75851305Seric 		CheckpointInterval = atoi(val);
75924944Seric 		break;
76024944Seric 
7619284Seric 	  case 'd':		/* delivery mode */
7629284Seric 		switch (*val)
7638269Seric 		{
7649284Seric 		  case '\0':
7659284Seric 			SendMode = SM_DELIVER;
7668269Seric 			break;
7678269Seric 
76810755Seric 		  case SM_QUEUE:	/* queue only */
76910755Seric #ifndef QUEUE
77010755Seric 			syserr("need QUEUE to set -odqueue");
77110755Seric #endif QUEUE
77210755Seric 			/* fall through..... */
77310755Seric 
7749284Seric 		  case SM_DELIVER:	/* do everything */
7759284Seric 		  case SM_FORK:		/* fork after verification */
7769284Seric 			SendMode = *val;
7778269Seric 			break;
7788269Seric 
7798269Seric 		  default:
7809284Seric 			syserr("Unknown delivery mode %c", *val);
7818269Seric 			exit(EX_USAGE);
7828269Seric 		}
7838269Seric 		break;
7848269Seric 
7859146Seric 	  case 'D':		/* rebuild alias database as needed */
7869381Seric 		AutoRebuild = atobool(val);
7879146Seric 		break;
7889146Seric 
7898269Seric 	  case 'e':		/* set error processing mode */
7908269Seric 		switch (*val)
7918269Seric 		{
7929381Seric 		  case EM_QUIET:	/* be silent about it */
7939381Seric 		  case EM_MAIL:		/* mail back */
7949381Seric 		  case EM_BERKNET:	/* do berknet error processing */
7959381Seric 		  case EM_WRITE:	/* write back (or mail) */
7968269Seric 			HoldErrs = TRUE;
7979381Seric 			/* fall through... */
7988269Seric 
7999381Seric 		  case EM_PRINT:	/* print errors normally (default) */
8009381Seric 			ErrorMode = *val;
8018269Seric 			break;
8028269Seric 		}
8038269Seric 		break;
8048269Seric 
8059049Seric 	  case 'F':		/* file mode */
80617975Seric 		FileMode = atooct(val) & 0777;
8079049Seric 		break;
8089049Seric 
8098269Seric 	  case 'f':		/* save Unix-style From lines on front */
8109381Seric 		SaveFrom = atobool(val);
8118269Seric 		break;
8128269Seric 
8138256Seric 	  case 'g':		/* default gid */
81417474Seric 		DefGid = atoi(val);
8158256Seric 		break;
8168256Seric 
8178256Seric 	  case 'H':		/* help file */
8189381Seric 		if (val[0] == '\0')
8198269Seric 			HelpFile = "sendmail.hf";
8209381Seric 		else
8219381Seric 			HelpFile = newstr(val);
8228256Seric 		break;
8238256Seric 
82451305Seric 	  case 'h':		/* maximum hop count */
82551305Seric 		MaxHopCount = atoi(val);
82651305Seric 		break;
82751305Seric 
82835651Seric 	  case 'I':		/* use internet domain name server */
82935651Seric 		UseNameServer = atobool(val);
83035651Seric 		break;
83135651Seric 
8328269Seric 	  case 'i':		/* ignore dot lines in message */
8339381Seric 		IgnrDot = atobool(val);
8348269Seric 		break;
8358269Seric 
8368256Seric 	  case 'L':		/* log level */
8379381Seric 		LogLevel = atoi(val);
8388256Seric 		break;
8398256Seric 
8408269Seric 	  case 'M':		/* define macro */
8419381Seric 		define(val[0], newstr(&val[1]), CurEnv);
84216878Seric 		sticky = FALSE;
8438269Seric 		break;
8448269Seric 
8458269Seric 	  case 'm':		/* send to me too */
8469381Seric 		MeToo = atobool(val);
8478269Seric 		break;
8488269Seric 
84925820Seric 	  case 'n':		/* validate RHS in newaliases */
85025820Seric 		CheckAliases = atobool(val);
85125820Seric 		break;
85225820Seric 
8538269Seric 	  case 'o':		/* assume old style headers */
8549381Seric 		if (atobool(val))
8559341Seric 			CurEnv->e_flags |= EF_OLDSTYLE;
8569341Seric 		else
8579341Seric 			CurEnv->e_flags &= ~EF_OLDSTYLE;
8588269Seric 		break;
8598269Seric 
86024944Seric 	  case 'P':		/* postmaster copy address for returned mail */
86124944Seric 		PostMasterCopy = newstr(val);
86224944Seric 		break;
86324944Seric 
86424944Seric 	  case 'q':		/* slope of queue only function */
86524944Seric 		QueueFactor = atoi(val);
86624944Seric 		break;
86724944Seric 
8688256Seric 	  case 'Q':		/* queue directory */
8699381Seric 		if (val[0] == '\0')
8708269Seric 			QueueDir = "mqueue";
8719381Seric 		else
8729381Seric 			QueueDir = newstr(val);
8738256Seric 		break;
8748256Seric 
8758256Seric 	  case 'r':		/* read timeout */
8769381Seric 		ReadTimeout = convtime(val);
8778256Seric 		break;
8788256Seric 
8798256Seric 	  case 'S':		/* status file */
8809381Seric 		if (val[0] == '\0')
8818269Seric 			StatFile = "sendmail.st";
8829381Seric 		else
8839381Seric 			StatFile = newstr(val);
8848256Seric 		break;
8858256Seric 
8868265Seric 	  case 's':		/* be super safe, even if expensive */
8879381Seric 		SuperSafe = atobool(val);
8888256Seric 		break;
8898256Seric 
8908256Seric 	  case 'T':		/* queue timeout */
8919381Seric 		TimeOut = convtime(val);
89233936Sbostic 		/*FALLTHROUGH*/
8938256Seric 
8948265Seric 	  case 't':		/* time zone name */
89552106Seric 		TimeZoneSpec = newstr(val);
8968265Seric 		break;
8978265Seric 
89850556Seric 	  case 'U':		/* location of user database */
89951360Seric 		UdbSpec = newstr(val);
90050556Seric 		break;
90150556Seric 
9028256Seric 	  case 'u':		/* set default uid */
90317474Seric 		DefUid = atoi(val);
90440973Sbostic 		setdefuser();
9058256Seric 		break;
9068256Seric 
9078269Seric 	  case 'v':		/* run in verbose mode */
9089381Seric 		Verbose = atobool(val);
9098256Seric 		break;
9108256Seric 
91151216Seric 	  case 'w':		/* we don't have wildcard MX records */
91251216Seric 		NoWildcardMX = atobool(val);
91350537Seric 		break;
91450537Seric 
91514879Seric 	  case 'x':		/* load avg at which to auto-queue msgs */
91614879Seric 		QueueLA = atoi(val);
91714879Seric 		break;
91814879Seric 
91914879Seric 	  case 'X':		/* load avg at which to auto-reject connections */
92014879Seric 		RefuseLA = atoi(val);
92114879Seric 		break;
92214879Seric 
92324981Seric 	  case 'y':		/* work recipient factor */
92424981Seric 		WkRecipFact = atoi(val);
92524981Seric 		break;
92624981Seric 
92724981Seric 	  case 'Y':		/* fork jobs during queue runs */
92824952Seric 		ForkQueueRuns = atobool(val);
92924952Seric 		break;
93024952Seric 
93124981Seric 	  case 'z':		/* work message class factor */
93224981Seric 		WkClassFact = atoi(val);
93324981Seric 		break;
93424981Seric 
93524981Seric 	  case 'Z':		/* work time factor */
93624981Seric 		WkTimeFact = atoi(val);
93724981Seric 		break;
93824981Seric 
9398256Seric 	  default:
9408256Seric 		break;
9418256Seric 	}
94216878Seric 	if (sticky)
94316878Seric 		setbitn(opt, StickyOpt);
9449188Seric 	return;
9458256Seric }
94610687Seric /*
94710687Seric **  SETCLASS -- set a word into a class
94810687Seric **
94910687Seric **	Parameters:
95010687Seric **		class -- the class to put the word in.
95110687Seric **		word -- the word to enter
95210687Seric **
95310687Seric **	Returns:
95410687Seric **		none.
95510687Seric **
95610687Seric **	Side Effects:
95710687Seric **		puts the word into the symbol table.
95810687Seric */
95910687Seric 
96010687Seric setclass(class, word)
96110687Seric 	int class;
96210687Seric 	char *word;
96310687Seric {
96410687Seric 	register STAB *s;
96510687Seric 
96610687Seric 	s = stab(word, ST_CLASS, ST_ENTER);
96710687Seric 	setbitn(class, s->s_class);
96810687Seric }
969