xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 57136)
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*57136Seric static char sccsid[] = "@(#)readcf.c	5.53 (Berkeley) 12/15/92";
1133731Sbostic #endif /* not lint */
1222709Sdist 
133313Seric # include "sendmail.h"
1452647Seric # include <sys/stat.h>
1554973Seric # include <unistd.h>
163308Seric 
173308Seric /*
183308Seric **  READCF -- read control file.
193308Seric **
203308Seric **	This routine reads the control file and builds the internal
213308Seric **	form.
223308Seric **
234432Seric **	The file is formatted as a sequence of lines, each taken
244432Seric **	atomically.  The first character of each line describes how
254432Seric **	the line is to be interpreted.  The lines are:
264432Seric **		Dxval		Define macro x to have value val.
274432Seric **		Cxword		Put word into class x.
284432Seric **		Fxfile [fmt]	Read file for lines to put into
294432Seric **				class x.  Use scanf string 'fmt'
304432Seric **				or "%s" if not present.  Fmt should
314432Seric **				only produce one string-valued result.
324432Seric **		Hname: value	Define header with field-name 'name'
334432Seric **				and value as specified; this will be
344432Seric **				macro expanded immediately before
354432Seric **				use.
364432Seric **		Sn		Use rewriting set n.
374432Seric **		Rlhs rhs	Rewrite addresses that match lhs to
384432Seric **				be rhs.
3924944Seric **		Mn arg=val...	Define mailer.  n is the internal name.
4024944Seric **				Args specify mailer parameters.
418252Seric **		Oxvalue		Set option x to value.
428252Seric **		Pname=value	Set precedence name to value.
4352645Seric **		Vversioncode	Version level of configuration syntax.
4453654Seric **		Kmapname mapclass arguments....
4553654Seric **				Define keyed lookup of a given class.
4653654Seric **				Arguments are class dependent.
474432Seric **
483308Seric **	Parameters:
493308Seric **		cfname -- control file name.
5054973Seric **		safe -- TRUE if this is the system config file;
5154973Seric **			FALSE otherwise.
5255012Seric **		e -- the main envelope.
533308Seric **
543308Seric **	Returns:
553308Seric **		none.
563308Seric **
573308Seric **	Side Effects:
583308Seric **		Builds several internal tables.
593308Seric */
603308Seric 
6155012Seric readcf(cfname, safe, e)
623308Seric 	char *cfname;
6354973Seric 	bool safe;
6455012Seric 	register ENVELOPE *e;
653308Seric {
663308Seric 	FILE *cf;
678547Seric 	int ruleset = 0;
688547Seric 	char *q;
698547Seric 	char **pv;
709350Seric 	struct rewrite *rwp = NULL;
7157135Seric 	char *bp;
723308Seric 	char buf[MAXLINE];
733308Seric 	register char *p;
743308Seric 	extern char **prescan();
753308Seric 	extern char **copyplist();
7652647Seric 	struct stat statb;
775909Seric 	char exbuf[MAXLINE];
7816915Seric 	char pvpbuf[PSBUFSIZE];
799350Seric 	extern char *fgetfolded();
8010709Seric 	extern char *munchstring();
8153654Seric 	extern void makemapentry();
823308Seric 
8352647Seric 	FileName = cfname;
8452647Seric 	LineNumber = 0;
8552647Seric 
863308Seric 	cf = fopen(cfname, "r");
873308Seric 	if (cf == NULL)
883308Seric 	{
8952647Seric 		syserr("cannot open");
903308Seric 		exit(EX_OSFILE);
913308Seric 	}
923308Seric 
9352647Seric 	if (fstat(fileno(cf), &statb) < 0)
9452647Seric 	{
9552647Seric 		syserr("cannot fstat");
9652647Seric 		exit(EX_OSFILE);
9752647Seric 	}
9852647Seric 
9952647Seric 	if (!S_ISREG(statb.st_mode))
10052647Seric 	{
10152647Seric 		syserr("not a plain file");
10252647Seric 		exit(EX_OSFILE);
10352647Seric 	}
10452647Seric 
10552647Seric 	if (OpMode != MD_TEST && bitset(S_IWGRP|S_IWOTH, statb.st_mode))
10652647Seric 	{
10753037Seric 		if (OpMode == MD_DAEMON || OpMode == MD_FREEZE)
10853037Seric 			fprintf(stderr, "%s: WARNING: dangerous write permissions\n",
10953037Seric 				FileName);
11053037Seric #ifdef LOG
11153037Seric 		if (LogLevel > 0)
11253037Seric 			syslog(LOG_CRIT, "%s: WARNING: dangerous write permissions",
11353037Seric 				FileName);
11453037Seric #endif
11552647Seric 	}
11652647Seric 
11757135Seric 	while ((bp = fgetfolded(buf, sizeof buf, cf)) != NULL)
1183308Seric 	{
11957135Seric 		if (bp[0] == '#')
12057135Seric 		{
12157135Seric 			if (bp != buf)
12257135Seric 				free(bp);
12352637Seric 			continue;
12457135Seric 		}
12552637Seric 
12616157Seric 		/* map $ into \001 (ASCII SOH) for macro expansion */
12757135Seric 		for (p = bp; *p != '\0'; p++)
12816157Seric 		{
12957135Seric 			if (*p == '#' && p > bp && ConfigLevel >= 3)
13052647Seric 			{
13152647Seric 				/* this is an on-line comment */
13252647Seric 				register char *e;
13352647Seric 
13452647Seric 				switch (*--p)
13552647Seric 				{
13652647Seric 				  case '\001':
13752647Seric 					/* it's from $# -- let it go through */
13852647Seric 					p++;
13952647Seric 					break;
14052647Seric 
14152647Seric 				  case '\\':
14252647Seric 					/* it's backslash escaped */
14352647Seric 					(void) strcpy(p, p + 1);
14452647Seric 					break;
14552647Seric 
14652647Seric 				  default:
14752647Seric 					/* delete preceeding white space */
14857135Seric 					while (isspace(*p) && p > bp)
14952647Seric 						p--;
15056795Seric 					if ((e = strchr(++p, '\n')) != NULL)
15152647Seric 						(void) strcpy(p, e);
15252647Seric 					else
15352647Seric 						p[0] = p[1] = '\0';
15452647Seric 					break;
15552647Seric 				}
15652647Seric 				continue;
15752647Seric 			}
15852647Seric 
15916157Seric 			if (*p != '$')
16016157Seric 				continue;
16116157Seric 
16216157Seric 			if (p[1] == '$')
16316157Seric 			{
16416157Seric 				/* actual dollar sign.... */
16523111Seric 				(void) strcpy(p, p + 1);
16616157Seric 				continue;
16716157Seric 			}
16816157Seric 
16916157Seric 			/* convert to macro expansion character */
17016157Seric 			*p = '\001';
17116157Seric 		}
17216157Seric 
17316157Seric 		/* interpret this line */
17457135Seric 		switch (bp[0])
1753308Seric 		{
1763308Seric 		  case '\0':
1773308Seric 		  case '#':		/* comment */
1783308Seric 			break;
1793308Seric 
1803308Seric 		  case 'R':		/* rewriting rule */
18157135Seric 			for (p = &bp[1]; *p != '\0' && *p != '\t'; p++)
1823308Seric 				continue;
1833308Seric 
1843308Seric 			if (*p == '\0')
1855909Seric 			{
18657135Seric 				syserr("invalid rewrite line \"%s\"", bp);
1875909Seric 				break;
1885909Seric 			}
1895909Seric 
1905909Seric 			/* allocate space for the rule header */
1915909Seric 			if (rwp == NULL)
1925909Seric 			{
1935909Seric 				RewriteRules[ruleset] = rwp =
1945909Seric 					(struct rewrite *) xalloc(sizeof *rwp);
1955909Seric 			}
1963308Seric 			else
1973308Seric 			{
1985909Seric 				rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp);
1995909Seric 				rwp = rwp->r_next;
2005909Seric 			}
2015909Seric 			rwp->r_next = NULL;
2023308Seric 
2035909Seric 			/* expand and save the LHS */
2045909Seric 			*p = '\0';
20557135Seric 			expand(&bp[1], exbuf, &exbuf[sizeof exbuf], e);
20616915Seric 			rwp->r_lhs = prescan(exbuf, '\t', pvpbuf);
2075909Seric 			if (rwp->r_lhs != NULL)
2085909Seric 				rwp->r_lhs = copyplist(rwp->r_lhs, TRUE);
20956678Seric 			else
21056678Seric 				syserr("R line: null LHS");
2115909Seric 
2125909Seric 			/* expand and save the RHS */
2135909Seric 			while (*++p == '\t')
2145909Seric 				continue;
2157231Seric 			q = p;
2167231Seric 			while (*p != '\0' && *p != '\t')
2177231Seric 				p++;
2187231Seric 			*p = '\0';
21955012Seric 			expand(q, exbuf, &exbuf[sizeof exbuf], e);
22016915Seric 			rwp->r_rhs = prescan(exbuf, '\t', pvpbuf);
2215909Seric 			if (rwp->r_rhs != NULL)
2225909Seric 				rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
22356678Seric 			else
22456678Seric 				syserr("R line: null RHS");
2253308Seric 			break;
2263308Seric 
2274072Seric 		  case 'S':		/* select rewriting set */
22857135Seric 			ruleset = atoi(&bp[1]);
2298056Seric 			if (ruleset >= MAXRWSETS || ruleset < 0)
2308056Seric 			{
2319381Seric 				syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS);
2328056Seric 				ruleset = 0;
2338056Seric 			}
2344072Seric 			rwp = NULL;
2354072Seric 			break;
2364072Seric 
2373308Seric 		  case 'D':		/* macro definition */
23857135Seric 			define(bp[1], newstr(munchstring(&bp[2])), e);
2393308Seric 			break;
2403308Seric 
2413387Seric 		  case 'H':		/* required header line */
24257135Seric 			(void) chompheader(&bp[1], TRUE, e);
2433387Seric 			break;
2443387Seric 
2454061Seric 		  case 'C':		/* word class */
2464432Seric 		  case 'F':		/* word class from file */
2474432Seric 			/* read list of words from argument or file */
24857135Seric 			if (bp[0] == 'F')
2494432Seric 			{
2504432Seric 				/* read from file */
25157135Seric 				for (p = &bp[2]; *p != '\0' && !isspace(*p); p++)
2524432Seric 					continue;
2534432Seric 				if (*p == '\0')
2544432Seric 					p = "%s";
2554432Seric 				else
2564432Seric 				{
2574432Seric 					*p = '\0';
2584432Seric 					while (isspace(*++p))
2594432Seric 						continue;
2604432Seric 				}
26157135Seric 				fileclass(bp[1], &bp[2], p, safe);
2624432Seric 				break;
2634432Seric 			}
2644061Seric 
2654432Seric 			/* scan the list of words and set class for all */
26657135Seric 			for (p = &bp[2]; *p != '\0'; )
2674061Seric 			{
2684061Seric 				register char *wd;
2694061Seric 				char delim;
2704061Seric 
2714061Seric 				while (*p != '\0' && isspace(*p))
2724061Seric 					p++;
2734061Seric 				wd = p;
2744061Seric 				while (*p != '\0' && !isspace(*p))
2754061Seric 					p++;
2764061Seric 				delim = *p;
2774061Seric 				*p = '\0';
2784061Seric 				if (wd[0] != '\0')
27957135Seric 					setclass(bp[1], wd);
2804061Seric 				*p = delim;
2814061Seric 			}
2824061Seric 			break;
2834061Seric 
2844096Seric 		  case 'M':		/* define mailer */
28557135Seric 			makemailer(&bp[1]);
2864096Seric 			break;
2874096Seric 
2888252Seric 		  case 'O':		/* set option */
28957135Seric 			setoption(bp[1], &bp[2], safe, FALSE);
2908252Seric 			break;
2918252Seric 
2928252Seric 		  case 'P':		/* set precedence */
2938252Seric 			if (NumPriorities >= MAXPRIORITIES)
2948252Seric 			{
2958547Seric 				toomany('P', MAXPRIORITIES);
2968252Seric 				break;
2978252Seric 			}
29857135Seric 			for (p = &bp[1]; *p != '\0' && *p != '=' && *p != '\t'; p++)
2998252Seric 				continue;
3008252Seric 			if (*p == '\0')
3018252Seric 				goto badline;
3028252Seric 			*p = '\0';
30357135Seric 			Priorities[NumPriorities].pri_name = newstr(&bp[1]);
3048252Seric 			Priorities[NumPriorities].pri_val = atoi(++p);
3058252Seric 			NumPriorities++;
3068252Seric 			break;
3078252Seric 
3088547Seric 		  case 'T':		/* trusted user(s) */
30957135Seric 			p = &bp[1];
3108547Seric 			while (*p != '\0')
3118547Seric 			{
3128547Seric 				while (isspace(*p))
3138547Seric 					p++;
3148547Seric 				q = p;
3158547Seric 				while (*p != '\0' && !isspace(*p))
3168547Seric 					p++;
3178547Seric 				if (*p != '\0')
3188547Seric 					*p++ = '\0';
3198547Seric 				if (*q == '\0')
3208547Seric 					continue;
3218547Seric 				for (pv = TrustedUsers; *pv != NULL; pv++)
3228547Seric 					continue;
3238547Seric 				if (pv >= &TrustedUsers[MAXTRUST])
3248547Seric 				{
3258547Seric 					toomany('T', MAXTRUST);
3268547Seric 					break;
3278547Seric 				}
3288547Seric 				*pv = newstr(q);
3298547Seric 			}
3308547Seric 			break;
3318547Seric 
33252645Seric 		  case 'V':		/* configuration syntax version */
33357135Seric 			ConfigLevel = atoi(&bp[1]);
33452645Seric 			break;
33552645Seric 
33653654Seric 		  case 'K':
33757135Seric 			makemapentry(&bp[1]);
33853654Seric 			break;
33953654Seric 
3403308Seric 		  default:
3414061Seric 		  badline:
34257135Seric 			syserr("unknown control line \"%s\"", bp);
3433308Seric 		}
34457135Seric 		if (bp != buf)
34557135Seric 			free(bp);
3463308Seric 	}
34752637Seric 	if (ferror(cf))
34852637Seric 	{
34952647Seric 		syserr("I/O read error", cfname);
35052637Seric 		exit(EX_OSFILE);
35152637Seric 	}
35252637Seric 	fclose(cf);
3539381Seric 	FileName = NULL;
35456836Seric 
35557076Seric 	if (stab("host", ST_MAP, ST_FIND) == NULL)
35657076Seric 	{
35757076Seric 		/* user didn't initialize: set up host map */
35857076Seric 		strcpy(buf, "host host");
35957076Seric 		if (ConfigLevel >= 2)
36057076Seric 			strcat(buf, " -a.");
36157076Seric 		makemapentry(buf);
36257076Seric 	}
3634096Seric }
3644096Seric /*
3658547Seric **  TOOMANY -- signal too many of some option
3668547Seric **
3678547Seric **	Parameters:
3688547Seric **		id -- the id of the error line
3698547Seric **		maxcnt -- the maximum possible values
3708547Seric **
3718547Seric **	Returns:
3728547Seric **		none.
3738547Seric **
3748547Seric **	Side Effects:
3758547Seric **		gives a syserr.
3768547Seric */
3778547Seric 
3788547Seric toomany(id, maxcnt)
3798547Seric 	char id;
3808547Seric 	int maxcnt;
3818547Seric {
3829381Seric 	syserr("too many %c lines, %d max", id, maxcnt);
3838547Seric }
3848547Seric /*
3854432Seric **  FILECLASS -- read members of a class from a file
3864432Seric **
3874432Seric **	Parameters:
3884432Seric **		class -- class to define.
3894432Seric **		filename -- name of file to read.
3904432Seric **		fmt -- scanf string to use for match.
3914432Seric **
3924432Seric **	Returns:
3934432Seric **		none
3944432Seric **
3954432Seric **	Side Effects:
3964432Seric **
3974432Seric **		puts all lines in filename that match a scanf into
3984432Seric **			the named class.
3994432Seric */
4004432Seric 
40154973Seric fileclass(class, filename, fmt, safe)
4024432Seric 	int class;
4034432Seric 	char *filename;
4044432Seric 	char *fmt;
40554973Seric 	bool safe;
4064432Seric {
40725808Seric 	FILE *f;
40854973Seric 	struct stat stbuf;
4094432Seric 	char buf[MAXLINE];
4104432Seric 
41154973Seric 	if (stat(filename, &stbuf) < 0)
41254973Seric 	{
41354973Seric 		syserr("fileclass: cannot stat %s", filename);
41454973Seric 		return;
41554973Seric 	}
41654973Seric 	if (!S_ISREG(stbuf.st_mode))
41754973Seric 	{
41854973Seric 		syserr("fileclass: %s not a regular file", filename);
41954973Seric 		return;
42054973Seric 	}
42154973Seric 	if (!safe && access(filename, R_OK) < 0)
42254973Seric 	{
42354973Seric 		syserr("fileclass: access denied on %s", filename);
42454973Seric 		return;
42554973Seric 	}
42654973Seric 	f = fopen(filename, "r");
4274432Seric 	if (f == NULL)
4284432Seric 	{
42954973Seric 		syserr("fileclass: cannot open %s", filename);
4304432Seric 		return;
4314432Seric 	}
4324432Seric 
4334432Seric 	while (fgets(buf, sizeof buf, f) != NULL)
4344432Seric 	{
4354432Seric 		register STAB *s;
43625808Seric 		register char *p;
43725808Seric # ifdef SCANF
4384432Seric 		char wordbuf[MAXNAME+1];
4394432Seric 
4404432Seric 		if (sscanf(buf, fmt, wordbuf) != 1)
4414432Seric 			continue;
44225808Seric 		p = wordbuf;
44356795Seric # else /* SCANF */
44425808Seric 		p = buf;
44556795Seric # endif /* SCANF */
44625808Seric 
44725808Seric 		/*
44825808Seric 		**  Break up the match into words.
44925808Seric 		*/
45025808Seric 
45125808Seric 		while (*p != '\0')
45225808Seric 		{
45325808Seric 			register char *q;
45425808Seric 
45525808Seric 			/* strip leading spaces */
45625808Seric 			while (isspace(*p))
45725808Seric 				p++;
45825808Seric 			if (*p == '\0')
45925808Seric 				break;
46025808Seric 
46125808Seric 			/* find the end of the word */
46225808Seric 			q = p;
46325808Seric 			while (*p != '\0' && !isspace(*p))
46425808Seric 				p++;
46525808Seric 			if (*p != '\0')
46625808Seric 				*p++ = '\0';
46725808Seric 
46825808Seric 			/* enter the word in the symbol table */
46925808Seric 			s = stab(q, ST_CLASS, ST_ENTER);
47025808Seric 			setbitn(class, s->s_class);
47125808Seric 		}
4724432Seric 	}
4734432Seric 
47454973Seric 	(void) fclose(f);
4754432Seric }
4764432Seric /*
4774096Seric **  MAKEMAILER -- define a new mailer.
4784096Seric **
4794096Seric **	Parameters:
48010327Seric **		line -- description of mailer.  This is in labeled
48110327Seric **			fields.  The fields are:
48210327Seric **			   P -- the path to the mailer
48310327Seric **			   F -- the flags associated with the mailer
48410327Seric **			   A -- the argv for this mailer
48510327Seric **			   S -- the sender rewriting set
48610327Seric **			   R -- the recipient rewriting set
48710327Seric **			   E -- the eol string
48810327Seric **			The first word is the canonical name of the mailer.
4894096Seric **
4904096Seric **	Returns:
4914096Seric **		none.
4924096Seric **
4934096Seric **	Side Effects:
4944096Seric **		enters the mailer into the mailer table.
4954096Seric */
4963308Seric 
49721066Seric makemailer(line)
4984096Seric 	char *line;
4994096Seric {
5004096Seric 	register char *p;
5018067Seric 	register struct mailer *m;
5028067Seric 	register STAB *s;
5038067Seric 	int i;
50410327Seric 	char fcode;
5054096Seric 	extern int NextMailer;
50610327Seric 	extern char **makeargv();
50710327Seric 	extern char *munchstring();
50810327Seric 	extern char *DelimChar;
50910701Seric 	extern long atol();
5104096Seric 
51110327Seric 	/* allocate a mailer and set up defaults */
51210327Seric 	m = (struct mailer *) xalloc(sizeof *m);
51310327Seric 	bzero((char *) m, sizeof *m);
51410327Seric 	m->m_mno = NextMailer;
51510327Seric 	m->m_eol = "\n";
51610327Seric 
51710327Seric 	/* collect the mailer name */
51810327Seric 	for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++)
51910327Seric 		continue;
52010327Seric 	if (*p != '\0')
52110327Seric 		*p++ = '\0';
52210327Seric 	m->m_name = newstr(line);
52310327Seric 
52410327Seric 	/* now scan through and assign info from the fields */
52510327Seric 	while (*p != '\0')
52610327Seric 	{
52710327Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
52810327Seric 			p++;
52910327Seric 
53010327Seric 		/* p now points to field code */
53110327Seric 		fcode = *p;
53210327Seric 		while (*p != '\0' && *p != '=' && *p != ',')
53310327Seric 			p++;
53410327Seric 		if (*p++ != '=')
53510327Seric 		{
53652637Seric 			syserr("mailer %s: `=' expected", m->m_name);
53710327Seric 			return;
53810327Seric 		}
53910327Seric 		while (isspace(*p))
54010327Seric 			p++;
54110327Seric 
54210327Seric 		/* p now points to the field body */
54310327Seric 		p = munchstring(p);
54410327Seric 
54510327Seric 		/* install the field into the mailer struct */
54610327Seric 		switch (fcode)
54710327Seric 		{
54810327Seric 		  case 'P':		/* pathname */
54910327Seric 			m->m_mailer = newstr(p);
55010327Seric 			break;
55110327Seric 
55210327Seric 		  case 'F':		/* flags */
55310687Seric 			for (; *p != '\0'; p++)
55452637Seric 				if (!isspace(*p))
55552637Seric 					setbitn(*p, m->m_flags);
55610327Seric 			break;
55710327Seric 
55810327Seric 		  case 'S':		/* sender rewriting ruleset */
55910327Seric 		  case 'R':		/* recipient rewriting ruleset */
56010327Seric 			i = atoi(p);
56110327Seric 			if (i < 0 || i >= MAXRWSETS)
56210327Seric 			{
56310327Seric 				syserr("invalid rewrite set, %d max", MAXRWSETS);
56410327Seric 				return;
56510327Seric 			}
56610327Seric 			if (fcode == 'S')
56710327Seric 				m->m_s_rwset = i;
56810327Seric 			else
56910327Seric 				m->m_r_rwset = i;
57010327Seric 			break;
57110327Seric 
57210327Seric 		  case 'E':		/* end of line string */
57310327Seric 			m->m_eol = newstr(p);
57410327Seric 			break;
57510327Seric 
57610327Seric 		  case 'A':		/* argument vector */
57710327Seric 			m->m_argv = makeargv(p);
57810327Seric 			break;
57910701Seric 
58010701Seric 		  case 'M':		/* maximum message size */
58110701Seric 			m->m_maxsize = atol(p);
58210701Seric 			break;
58352106Seric 
58452106Seric 		  case 'L':		/* maximum line length */
58552106Seric 			m->m_linelimit = atoi(p);
58652106Seric 			break;
58710327Seric 		}
58810327Seric 
58910327Seric 		p = DelimChar;
59010327Seric 	}
59110327Seric 
59252106Seric 	/* do some heuristic cleanup for back compatibility */
59352106Seric 	if (bitnset(M_LIMITS, m->m_flags))
59452106Seric 	{
59552106Seric 		if (m->m_linelimit == 0)
59652106Seric 			m->m_linelimit = SMTPLINELIM;
59755418Seric 		if (ConfigLevel < 2)
59852106Seric 			setbitn(M_7BITS, m->m_flags);
59952106Seric 	}
60052106Seric 
60110327Seric 	/* now store the mailer away */
6024096Seric 	if (NextMailer >= MAXMAILERS)
6034096Seric 	{
6049381Seric 		syserr("too many mailers defined (%d max)", MAXMAILERS);
6054096Seric 		return;
6064096Seric 	}
60710327Seric 	Mailer[NextMailer++] = m;
60810327Seric 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
60910327Seric 	s->s_mailer = m;
61010327Seric }
61110327Seric /*
61210327Seric **  MUNCHSTRING -- translate a string into internal form.
61310327Seric **
61410327Seric **	Parameters:
61510327Seric **		p -- the string to munch.
61610327Seric **
61710327Seric **	Returns:
61810327Seric **		the munched string.
61910327Seric **
62010327Seric **	Side Effects:
62110327Seric **		Sets "DelimChar" to point to the string that caused us
62210327Seric **		to stop.
62310327Seric */
6244096Seric 
62510327Seric char *
62610327Seric munchstring(p)
62710327Seric 	register char *p;
62810327Seric {
62910327Seric 	register char *q;
63010327Seric 	bool backslash = FALSE;
63110327Seric 	bool quotemode = FALSE;
63210327Seric 	static char buf[MAXLINE];
63310327Seric 	extern char *DelimChar;
6344096Seric 
63510327Seric 	for (q = buf; *p != '\0'; p++)
6364096Seric 	{
63710327Seric 		if (backslash)
63810327Seric 		{
63910327Seric 			/* everything is roughly literal */
64010357Seric 			backslash = FALSE;
64110327Seric 			switch (*p)
64210327Seric 			{
64310327Seric 			  case 'r':		/* carriage return */
64410327Seric 				*q++ = '\r';
64510327Seric 				continue;
64610327Seric 
64710327Seric 			  case 'n':		/* newline */
64810327Seric 				*q++ = '\n';
64910327Seric 				continue;
65010327Seric 
65110327Seric 			  case 'f':		/* form feed */
65210327Seric 				*q++ = '\f';
65310327Seric 				continue;
65410327Seric 
65510327Seric 			  case 'b':		/* backspace */
65610327Seric 				*q++ = '\b';
65710327Seric 				continue;
65810327Seric 			}
65910327Seric 			*q++ = *p;
66010327Seric 		}
66110327Seric 		else
66210327Seric 		{
66310327Seric 			if (*p == '\\')
66410327Seric 				backslash = TRUE;
66510327Seric 			else if (*p == '"')
66610327Seric 				quotemode = !quotemode;
66710327Seric 			else if (quotemode || *p != ',')
66810327Seric 				*q++ = *p;
66910327Seric 			else
67010327Seric 				break;
67110327Seric 		}
6724096Seric 	}
6734096Seric 
67410327Seric 	DelimChar = p;
67510327Seric 	*q++ = '\0';
67610327Seric 	return (buf);
67710327Seric }
67810327Seric /*
67910327Seric **  MAKEARGV -- break up a string into words
68010327Seric **
68110327Seric **	Parameters:
68210327Seric **		p -- the string to break up.
68310327Seric **
68410327Seric **	Returns:
68510327Seric **		a char **argv (dynamically allocated)
68610327Seric **
68710327Seric **	Side Effects:
68810327Seric **		munges p.
68910327Seric */
6904096Seric 
69110327Seric char **
69210327Seric makeargv(p)
69310327Seric 	register char *p;
69410327Seric {
69510327Seric 	char *q;
69610327Seric 	int i;
69710327Seric 	char **avp;
69810327Seric 	char *argv[MAXPV + 1];
69910327Seric 
70010327Seric 	/* take apart the words */
70110327Seric 	i = 0;
70210327Seric 	while (*p != '\0' && i < MAXPV)
7034096Seric 	{
70410327Seric 		q = p;
70510327Seric 		while (*p != '\0' && !isspace(*p))
70610327Seric 			p++;
70710327Seric 		while (isspace(*p))
70810327Seric 			*p++ = '\0';
70910327Seric 		argv[i++] = newstr(q);
7104096Seric 	}
71110327Seric 	argv[i++] = NULL;
7124096Seric 
71310327Seric 	/* now make a copy of the argv */
71410327Seric 	avp = (char **) xalloc(sizeof *avp * i);
71516893Seric 	bcopy((char *) argv, (char *) avp, sizeof *avp * i);
71610327Seric 
71710327Seric 	return (avp);
7183308Seric }
7193308Seric /*
7203308Seric **  PRINTRULES -- print rewrite rules (for debugging)
7213308Seric **
7223308Seric **	Parameters:
7233308Seric **		none.
7243308Seric **
7253308Seric **	Returns:
7263308Seric **		none.
7273308Seric **
7283308Seric **	Side Effects:
7293308Seric **		prints rewrite rules.
7303308Seric */
7313308Seric 
7323308Seric printrules()
7333308Seric {
7343308Seric 	register struct rewrite *rwp;
7354072Seric 	register int ruleset;
7363308Seric 
7374072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
7383308Seric 	{
7394072Seric 		if (RewriteRules[ruleset] == NULL)
7404072Seric 			continue;
7418067Seric 		printf("\n----Rule Set %d:", ruleset);
7423308Seric 
7434072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
7443308Seric 		{
7458067Seric 			printf("\nLHS:");
7468067Seric 			printav(rwp->r_lhs);
7478067Seric 			printf("RHS:");
7488067Seric 			printav(rwp->r_rhs);
7493308Seric 		}
7503308Seric 	}
7513308Seric }
7524319Seric 
7534096Seric /*
7548256Seric **  SETOPTION -- set global processing option
7558256Seric **
7568256Seric **	Parameters:
7578256Seric **		opt -- option name.
7588256Seric **		val -- option value (as a text string).
75921755Seric **		safe -- set if this came from a configuration file.
76021755Seric **			Some options (if set from the command line) will
76121755Seric **			reset the user id to avoid security problems.
7628269Seric **		sticky -- if set, don't let other setoptions override
7638269Seric **			this value.
7648256Seric **
7658256Seric **	Returns:
7668256Seric **		none.
7678256Seric **
7688256Seric **	Side Effects:
7698256Seric **		Sets options as implied by the arguments.
7708256Seric */
7718256Seric 
77210687Seric static BITMAP	StickyOpt;		/* set if option is stuck */
7738269Seric 
77421755Seric setoption(opt, val, safe, sticky)
7758256Seric 	char opt;
7768256Seric 	char *val;
77721755Seric 	bool safe;
7788269Seric 	bool sticky;
7798256Seric {
7808265Seric 	extern bool atobool();
78112633Seric 	extern time_t convtime();
78214879Seric 	extern int QueueLA;
78314879Seric 	extern int RefuseLA;
78417474Seric 	extern bool trusteduser();
78517474Seric 	extern char *username();
7868256Seric 
7878256Seric 	if (tTd(37, 1))
7889341Seric 		printf("setoption %c=%s", opt, val);
7898256Seric 
7908256Seric 	/*
7918269Seric 	**  See if this option is preset for us.
7928256Seric 	*/
7938256Seric 
79410687Seric 	if (bitnset(opt, StickyOpt))
7958269Seric 	{
7969341Seric 		if (tTd(37, 1))
7979341Seric 			printf(" (ignored)\n");
7988269Seric 		return;
7998269Seric 	}
8008269Seric 
80121755Seric 	/*
80221755Seric 	**  Check to see if this option can be specified by this user.
80321755Seric 	*/
80421755Seric 
80536238Skarels 	if (!safe && getuid() == 0)
80621755Seric 		safe = TRUE;
807*57136Seric 	if (!safe && strchr("deEiLmorsvC8", opt) == NULL)
80821755Seric 	{
80939111Srick 		if (opt != 'M' || (val[0] != 'r' && val[0] != 's'))
81021755Seric 		{
81136582Sbostic 			if (tTd(37, 1))
81236582Sbostic 				printf(" (unsafe)");
81336582Sbostic 			if (getuid() != geteuid())
81436582Sbostic 			{
81551210Seric 				if (tTd(37, 1))
81651210Seric 					printf("(Resetting uid)");
81736582Sbostic 				(void) setgid(getgid());
81836582Sbostic 				(void) setuid(getuid());
81936582Sbostic 			}
82021755Seric 		}
82121755Seric 	}
82251210Seric 	if (tTd(37, 1))
82317985Seric 		printf("\n");
8248269Seric 
8258256Seric 	switch (opt)
8268256Seric 	{
82751312Seric 	  case '=':		/* config file generation level */
82851312Seric 		ConfigLevel = atoi(val);
82951312Seric 		break;
83051312Seric 
83152106Seric 	  case '8':		/* allow eight-bit input */
83252106Seric 		EightBit = atobool(val);
83352106Seric 		break;
83452106Seric 
8358256Seric 	  case 'A':		/* set default alias file */
8369381Seric 		if (val[0] == '\0')
8378269Seric 			AliasFile = "aliases";
8389381Seric 		else
8399381Seric 			AliasFile = newstr(val);
8408256Seric 		break;
8418256Seric 
84217474Seric 	  case 'a':		/* look N minutes for "@:@" in alias file */
84317474Seric 		if (val[0] == '\0')
84417474Seric 			SafeAlias = 5;
84517474Seric 		else
84617474Seric 			SafeAlias = atoi(val);
84717474Seric 		break;
84817474Seric 
84916843Seric 	  case 'B':		/* substitution for blank character */
85016843Seric 		SpaceSub = val[0];
85116843Seric 		if (SpaceSub == '\0')
85216843Seric 			SpaceSub = ' ';
85316843Seric 		break;
85416843Seric 
8559284Seric 	  case 'c':		/* don't connect to "expensive" mailers */
8569381Seric 		NoConnect = atobool(val);
8579284Seric 		break;
8589284Seric 
85951305Seric 	  case 'C':		/* checkpoint every N addresses */
86051305Seric 		CheckpointInterval = atoi(val);
86124944Seric 		break;
86224944Seric 
8639284Seric 	  case 'd':		/* delivery mode */
8649284Seric 		switch (*val)
8658269Seric 		{
8669284Seric 		  case '\0':
8679284Seric 			SendMode = SM_DELIVER;
8688269Seric 			break;
8698269Seric 
87010755Seric 		  case SM_QUEUE:	/* queue only */
87110755Seric #ifndef QUEUE
87210755Seric 			syserr("need QUEUE to set -odqueue");
87356795Seric #endif /* QUEUE */
87410755Seric 			/* fall through..... */
87510755Seric 
8769284Seric 		  case SM_DELIVER:	/* do everything */
8779284Seric 		  case SM_FORK:		/* fork after verification */
8789284Seric 			SendMode = *val;
8798269Seric 			break;
8808269Seric 
8818269Seric 		  default:
8829284Seric 			syserr("Unknown delivery mode %c", *val);
8838269Seric 			exit(EX_USAGE);
8848269Seric 		}
8858269Seric 		break;
8868269Seric 
8879146Seric 	  case 'D':		/* rebuild alias database as needed */
8889381Seric 		AutoRebuild = atobool(val);
8899146Seric 		break;
8909146Seric 
89155372Seric 	  case 'E':		/* error message header/header file */
89255379Seric 		if (*val != '\0')
89355379Seric 			ErrMsgFile = newstr(val);
89455372Seric 		break;
89555372Seric 
8968269Seric 	  case 'e':		/* set error processing mode */
8978269Seric 		switch (*val)
8988269Seric 		{
8999381Seric 		  case EM_QUIET:	/* be silent about it */
9009381Seric 		  case EM_MAIL:		/* mail back */
9019381Seric 		  case EM_BERKNET:	/* do berknet error processing */
9029381Seric 		  case EM_WRITE:	/* write back (or mail) */
9038269Seric 			HoldErrs = TRUE;
9049381Seric 			/* fall through... */
9058269Seric 
9069381Seric 		  case EM_PRINT:	/* print errors normally (default) */
9079381Seric 			ErrorMode = *val;
9088269Seric 			break;
9098269Seric 		}
9108269Seric 		break;
9118269Seric 
9129049Seric 	  case 'F':		/* file mode */
91317975Seric 		FileMode = atooct(val) & 0777;
9149049Seric 		break;
9159049Seric 
9168269Seric 	  case 'f':		/* save Unix-style From lines on front */
9179381Seric 		SaveFrom = atobool(val);
9188269Seric 		break;
9198269Seric 
92053735Seric 	  case 'G':		/* match recipients against GECOS field */
92153735Seric 		MatchGecos = atobool(val);
92253735Seric 		break;
92353735Seric 
9248256Seric 	  case 'g':		/* default gid */
92517474Seric 		DefGid = atoi(val);
9268256Seric 		break;
9278256Seric 
9288256Seric 	  case 'H':		/* help file */
9299381Seric 		if (val[0] == '\0')
9308269Seric 			HelpFile = "sendmail.hf";
9319381Seric 		else
9329381Seric 			HelpFile = newstr(val);
9338256Seric 		break;
9348256Seric 
93551305Seric 	  case 'h':		/* maximum hop count */
93651305Seric 		MaxHopCount = atoi(val);
93751305Seric 		break;
93851305Seric 
93935651Seric 	  case 'I':		/* use internet domain name server */
94035651Seric 		UseNameServer = atobool(val);
94135651Seric 		break;
94235651Seric 
9438269Seric 	  case 'i':		/* ignore dot lines in message */
9449381Seric 		IgnrDot = atobool(val);
9458269Seric 		break;
9468269Seric 
947*57136Seric 	  case 'J':		/* .forward search path */
948*57136Seric 		ForwardPath = newstr(val);
949*57136Seric 		break;
950*57136Seric 
95154967Seric 	  case 'k':		/* connection cache size */
95254967Seric 		MaxMciCache = atoi(val);
95356215Seric 		if (MaxMciCache < 0)
95456215Seric 			MaxMciCache = 0;
95554967Seric 		break;
95654967Seric 
95754967Seric 	  case 'K':		/* connection cache timeout */
95854967Seric 		MciCacheTimeout = convtime(val);
95954967Seric 		break;
96054967Seric 
9618256Seric 	  case 'L':		/* log level */
9629381Seric 		LogLevel = atoi(val);
9638256Seric 		break;
9648256Seric 
9658269Seric 	  case 'M':		/* define macro */
9669381Seric 		define(val[0], newstr(&val[1]), CurEnv);
96716878Seric 		sticky = FALSE;
9688269Seric 		break;
9698269Seric 
9708269Seric 	  case 'm':		/* send to me too */
9719381Seric 		MeToo = atobool(val);
9728269Seric 		break;
9738269Seric 
97425820Seric 	  case 'n':		/* validate RHS in newaliases */
97525820Seric 		CheckAliases = atobool(val);
97625820Seric 		break;
97725820Seric 
9788269Seric 	  case 'o':		/* assume old style headers */
9799381Seric 		if (atobool(val))
9809341Seric 			CurEnv->e_flags |= EF_OLDSTYLE;
9819341Seric 		else
9829341Seric 			CurEnv->e_flags &= ~EF_OLDSTYLE;
9838269Seric 		break;
9848269Seric 
98524944Seric 	  case 'P':		/* postmaster copy address for returned mail */
98624944Seric 		PostMasterCopy = newstr(val);
98724944Seric 		break;
98824944Seric 
98924944Seric 	  case 'q':		/* slope of queue only function */
99024944Seric 		QueueFactor = atoi(val);
99124944Seric 		break;
99224944Seric 
9938256Seric 	  case 'Q':		/* queue directory */
9949381Seric 		if (val[0] == '\0')
9958269Seric 			QueueDir = "mqueue";
9969381Seric 		else
9979381Seric 			QueueDir = newstr(val);
9988256Seric 		break;
9998256Seric 
10008256Seric 	  case 'r':		/* read timeout */
10019381Seric 		ReadTimeout = convtime(val);
10028256Seric 		break;
10038256Seric 
10048256Seric 	  case 'S':		/* status file */
10059381Seric 		if (val[0] == '\0')
10068269Seric 			StatFile = "sendmail.st";
10079381Seric 		else
10089381Seric 			StatFile = newstr(val);
10098256Seric 		break;
10108256Seric 
10118265Seric 	  case 's':		/* be super safe, even if expensive */
10129381Seric 		SuperSafe = atobool(val);
10138256Seric 		break;
10148256Seric 
10158256Seric 	  case 'T':		/* queue timeout */
10169381Seric 		TimeOut = convtime(val);
101754967Seric 		break;
10188256Seric 
10198265Seric 	  case 't':		/* time zone name */
102052106Seric 		TimeZoneSpec = newstr(val);
10218265Seric 		break;
10228265Seric 
102350556Seric 	  case 'U':		/* location of user database */
102451360Seric 		UdbSpec = newstr(val);
102550556Seric 		break;
102650556Seric 
10278256Seric 	  case 'u':		/* set default uid */
102817474Seric 		DefUid = atoi(val);
102940973Sbostic 		setdefuser();
10308256Seric 		break;
10318256Seric 
10328269Seric 	  case 'v':		/* run in verbose mode */
10339381Seric 		Verbose = atobool(val);
10348256Seric 		break;
10358256Seric 
103651216Seric 	  case 'w':		/* we don't have wildcard MX records */
103751216Seric 		NoWildcardMX = atobool(val);
103850537Seric 		break;
103950537Seric 
104014879Seric 	  case 'x':		/* load avg at which to auto-queue msgs */
104114879Seric 		QueueLA = atoi(val);
104214879Seric 		break;
104314879Seric 
104414879Seric 	  case 'X':		/* load avg at which to auto-reject connections */
104514879Seric 		RefuseLA = atoi(val);
104614879Seric 		break;
104714879Seric 
104824981Seric 	  case 'y':		/* work recipient factor */
104924981Seric 		WkRecipFact = atoi(val);
105024981Seric 		break;
105124981Seric 
105224981Seric 	  case 'Y':		/* fork jobs during queue runs */
105324952Seric 		ForkQueueRuns = atobool(val);
105424952Seric 		break;
105524952Seric 
105624981Seric 	  case 'z':		/* work message class factor */
105724981Seric 		WkClassFact = atoi(val);
105824981Seric 		break;
105924981Seric 
106024981Seric 	  case 'Z':		/* work time factor */
106124981Seric 		WkTimeFact = atoi(val);
106224981Seric 		break;
106324981Seric 
10648256Seric 	  default:
10658256Seric 		break;
10668256Seric 	}
106716878Seric 	if (sticky)
106816878Seric 		setbitn(opt, StickyOpt);
10699188Seric 	return;
10708256Seric }
107110687Seric /*
107210687Seric **  SETCLASS -- set a word into a class
107310687Seric **
107410687Seric **	Parameters:
107510687Seric **		class -- the class to put the word in.
107610687Seric **		word -- the word to enter
107710687Seric **
107810687Seric **	Returns:
107910687Seric **		none.
108010687Seric **
108110687Seric **	Side Effects:
108210687Seric **		puts the word into the symbol table.
108310687Seric */
108410687Seric 
108510687Seric setclass(class, word)
108610687Seric 	int class;
108710687Seric 	char *word;
108810687Seric {
108910687Seric 	register STAB *s;
109010687Seric 
109110687Seric 	s = stab(word, ST_CLASS, ST_ENTER);
109210687Seric 	setbitn(class, s->s_class);
109310687Seric }
109453654Seric /*
109553654Seric **  MAKEMAPENTRY -- create a map entry
109653654Seric **
109753654Seric **	Parameters:
109853654Seric **		line -- the config file line
109953654Seric **
110053654Seric **	Returns:
110153654Seric **		TRUE if it successfully entered the map entry.
110253654Seric **		FALSE otherwise (usually syntax error).
110353654Seric **
110453654Seric **	Side Effects:
110553654Seric **		Enters the map into the dictionary.
110653654Seric */
110753654Seric 
110853654Seric void
110953654Seric makemapentry(line)
111053654Seric 	char *line;
111153654Seric {
111253654Seric 	register char *p;
111353654Seric 	char *mapname;
111453654Seric 	char *classname;
111553654Seric 	register STAB *map;
111653654Seric 	STAB *class;
111753654Seric 
111853654Seric 	for (p = line; isspace(*p); p++)
111953654Seric 		continue;
112053654Seric 	if (!isalnum(*p))
112153654Seric 	{
112253654Seric 		syserr("readcf: config K line: no map name");
112353654Seric 		return;
112453654Seric 	}
112553654Seric 
112653654Seric 	mapname = p;
112753654Seric 	while (isalnum(*++p))
112853654Seric 		continue;
112953654Seric 	if (*p != '\0')
113053654Seric 		*p++ = '\0';
113153654Seric 	while (isspace(*p))
113253654Seric 		p++;
113353654Seric 	if (!isalnum(*p))
113453654Seric 	{
113553654Seric 		syserr("readcf: config K line, map %s: no map class", mapname);
113653654Seric 		return;
113753654Seric 	}
113853654Seric 	classname = p;
113953654Seric 	while (isalnum(*++p))
114053654Seric 		continue;
114153654Seric 	if (*p != '\0')
114253654Seric 		*p++ = '\0';
114353654Seric 	while (isspace(*p))
114453654Seric 		p++;
114553654Seric 
114653654Seric 	/* look up the class */
114753654Seric 	class = stab(classname, ST_MAPCLASS, ST_FIND);
114853654Seric 	if (class == NULL)
114953654Seric 	{
115053654Seric 		syserr("readcf: map %s: class %s not available", mapname, classname);
115153654Seric 		return;
115253654Seric 	}
115353654Seric 
115453654Seric 	/* enter the map */
115553654Seric 	map = stab(mapname, ST_MAP, ST_ENTER);
115653654Seric 	map->s_map.map_class = &class->s_mapclass;
115753654Seric 
115856823Seric 	if ((*class->s_mapclass.map_init)(&map->s_map, mapname, p))
115953654Seric 		map->s_map.map_flags |= MF_VALID;
116053654Seric }
1161