xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 57207)
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*57207Seric static char sccsid[] = "@(#)readcf.c	5.55 (Berkeley) 12/18/92";
1133731Sbostic #endif /* not lint */
1222709Sdist 
133313Seric # include "sendmail.h"
1452647Seric # include <sys/stat.h>
1554973Seric # include <unistd.h>
16*57207Seric #ifdef NAMED_BIND
17*57207Seric # include <arpa/nameser.h>
18*57207Seric # include <resolv.h>
19*57207Seric #endif
203308Seric 
213308Seric /*
223308Seric **  READCF -- read control file.
233308Seric **
243308Seric **	This routine reads the control file and builds the internal
253308Seric **	form.
263308Seric **
274432Seric **	The file is formatted as a sequence of lines, each taken
284432Seric **	atomically.  The first character of each line describes how
294432Seric **	the line is to be interpreted.  The lines are:
304432Seric **		Dxval		Define macro x to have value val.
314432Seric **		Cxword		Put word into class x.
324432Seric **		Fxfile [fmt]	Read file for lines to put into
334432Seric **				class x.  Use scanf string 'fmt'
344432Seric **				or "%s" if not present.  Fmt should
354432Seric **				only produce one string-valued result.
364432Seric **		Hname: value	Define header with field-name 'name'
374432Seric **				and value as specified; this will be
384432Seric **				macro expanded immediately before
394432Seric **				use.
404432Seric **		Sn		Use rewriting set n.
414432Seric **		Rlhs rhs	Rewrite addresses that match lhs to
424432Seric **				be rhs.
4324944Seric **		Mn arg=val...	Define mailer.  n is the internal name.
4424944Seric **				Args specify mailer parameters.
458252Seric **		Oxvalue		Set option x to value.
468252Seric **		Pname=value	Set precedence name to value.
4752645Seric **		Vversioncode	Version level of configuration syntax.
4853654Seric **		Kmapname mapclass arguments....
4953654Seric **				Define keyed lookup of a given class.
5053654Seric **				Arguments are class dependent.
514432Seric **
523308Seric **	Parameters:
533308Seric **		cfname -- control file name.
5454973Seric **		safe -- TRUE if this is the system config file;
5554973Seric **			FALSE otherwise.
5655012Seric **		e -- the main envelope.
573308Seric **
583308Seric **	Returns:
593308Seric **		none.
603308Seric **
613308Seric **	Side Effects:
623308Seric **		Builds several internal tables.
633308Seric */
643308Seric 
6555012Seric readcf(cfname, safe, e)
663308Seric 	char *cfname;
6754973Seric 	bool safe;
6855012Seric 	register ENVELOPE *e;
693308Seric {
703308Seric 	FILE *cf;
718547Seric 	int ruleset = 0;
728547Seric 	char *q;
738547Seric 	char **pv;
749350Seric 	struct rewrite *rwp = NULL;
7557135Seric 	char *bp;
763308Seric 	char buf[MAXLINE];
773308Seric 	register char *p;
783308Seric 	extern char **prescan();
793308Seric 	extern char **copyplist();
8052647Seric 	struct stat statb;
815909Seric 	char exbuf[MAXLINE];
8216915Seric 	char pvpbuf[PSBUFSIZE];
839350Seric 	extern char *fgetfolded();
8410709Seric 	extern char *munchstring();
8553654Seric 	extern void makemapentry();
863308Seric 
8752647Seric 	FileName = cfname;
8852647Seric 	LineNumber = 0;
8952647Seric 
903308Seric 	cf = fopen(cfname, "r");
913308Seric 	if (cf == NULL)
923308Seric 	{
9352647Seric 		syserr("cannot open");
943308Seric 		exit(EX_OSFILE);
953308Seric 	}
963308Seric 
9752647Seric 	if (fstat(fileno(cf), &statb) < 0)
9852647Seric 	{
9952647Seric 		syserr("cannot fstat");
10052647Seric 		exit(EX_OSFILE);
10152647Seric 	}
10252647Seric 
10352647Seric 	if (!S_ISREG(statb.st_mode))
10452647Seric 	{
10552647Seric 		syserr("not a plain file");
10652647Seric 		exit(EX_OSFILE);
10752647Seric 	}
10852647Seric 
10952647Seric 	if (OpMode != MD_TEST && bitset(S_IWGRP|S_IWOTH, statb.st_mode))
11052647Seric 	{
11153037Seric 		if (OpMode == MD_DAEMON || OpMode == MD_FREEZE)
11253037Seric 			fprintf(stderr, "%s: WARNING: dangerous write permissions\n",
11353037Seric 				FileName);
11453037Seric #ifdef LOG
11553037Seric 		if (LogLevel > 0)
11653037Seric 			syslog(LOG_CRIT, "%s: WARNING: dangerous write permissions",
11753037Seric 				FileName);
11853037Seric #endif
11952647Seric 	}
12052647Seric 
12157135Seric 	while ((bp = fgetfolded(buf, sizeof buf, cf)) != NULL)
1223308Seric 	{
12357135Seric 		if (bp[0] == '#')
12457135Seric 		{
12557135Seric 			if (bp != buf)
12657135Seric 				free(bp);
12752637Seric 			continue;
12857135Seric 		}
12952637Seric 
13016157Seric 		/* map $ into \001 (ASCII SOH) for macro expansion */
13157135Seric 		for (p = bp; *p != '\0'; p++)
13216157Seric 		{
13357135Seric 			if (*p == '#' && p > bp && ConfigLevel >= 3)
13452647Seric 			{
13552647Seric 				/* this is an on-line comment */
13652647Seric 				register char *e;
13752647Seric 
13852647Seric 				switch (*--p)
13952647Seric 				{
14052647Seric 				  case '\001':
14152647Seric 					/* it's from $# -- let it go through */
14252647Seric 					p++;
14352647Seric 					break;
14452647Seric 
14552647Seric 				  case '\\':
14652647Seric 					/* it's backslash escaped */
14752647Seric 					(void) strcpy(p, p + 1);
14852647Seric 					break;
14952647Seric 
15052647Seric 				  default:
15152647Seric 					/* delete preceeding white space */
15257135Seric 					while (isspace(*p) && p > bp)
15352647Seric 						p--;
15456795Seric 					if ((e = strchr(++p, '\n')) != NULL)
15552647Seric 						(void) strcpy(p, e);
15652647Seric 					else
15752647Seric 						p[0] = p[1] = '\0';
15852647Seric 					break;
15952647Seric 				}
16052647Seric 				continue;
16152647Seric 			}
16252647Seric 
16316157Seric 			if (*p != '$')
16416157Seric 				continue;
16516157Seric 
16616157Seric 			if (p[1] == '$')
16716157Seric 			{
16816157Seric 				/* actual dollar sign.... */
16923111Seric 				(void) strcpy(p, p + 1);
17016157Seric 				continue;
17116157Seric 			}
17216157Seric 
17316157Seric 			/* convert to macro expansion character */
17416157Seric 			*p = '\001';
17516157Seric 		}
17616157Seric 
17716157Seric 		/* interpret this line */
17857135Seric 		switch (bp[0])
1793308Seric 		{
1803308Seric 		  case '\0':
1813308Seric 		  case '#':		/* comment */
1823308Seric 			break;
1833308Seric 
1843308Seric 		  case 'R':		/* rewriting rule */
18557135Seric 			for (p = &bp[1]; *p != '\0' && *p != '\t'; p++)
1863308Seric 				continue;
1873308Seric 
1883308Seric 			if (*p == '\0')
1895909Seric 			{
19057135Seric 				syserr("invalid rewrite line \"%s\"", bp);
1915909Seric 				break;
1925909Seric 			}
1935909Seric 
1945909Seric 			/* allocate space for the rule header */
1955909Seric 			if (rwp == NULL)
1965909Seric 			{
1975909Seric 				RewriteRules[ruleset] = rwp =
1985909Seric 					(struct rewrite *) xalloc(sizeof *rwp);
1995909Seric 			}
2003308Seric 			else
2013308Seric 			{
2025909Seric 				rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp);
2035909Seric 				rwp = rwp->r_next;
2045909Seric 			}
2055909Seric 			rwp->r_next = NULL;
2063308Seric 
2075909Seric 			/* expand and save the LHS */
2085909Seric 			*p = '\0';
20957135Seric 			expand(&bp[1], exbuf, &exbuf[sizeof exbuf], e);
21016915Seric 			rwp->r_lhs = prescan(exbuf, '\t', pvpbuf);
2115909Seric 			if (rwp->r_lhs != NULL)
2125909Seric 				rwp->r_lhs = copyplist(rwp->r_lhs, TRUE);
21356678Seric 			else
21456678Seric 				syserr("R line: null LHS");
2155909Seric 
2165909Seric 			/* expand and save the RHS */
2175909Seric 			while (*++p == '\t')
2185909Seric 				continue;
2197231Seric 			q = p;
2207231Seric 			while (*p != '\0' && *p != '\t')
2217231Seric 				p++;
2227231Seric 			*p = '\0';
22355012Seric 			expand(q, exbuf, &exbuf[sizeof exbuf], e);
22416915Seric 			rwp->r_rhs = prescan(exbuf, '\t', pvpbuf);
2255909Seric 			if (rwp->r_rhs != NULL)
2265909Seric 				rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
22756678Seric 			else
22856678Seric 				syserr("R line: null RHS");
2293308Seric 			break;
2303308Seric 
2314072Seric 		  case 'S':		/* select rewriting set */
23257135Seric 			ruleset = atoi(&bp[1]);
2338056Seric 			if (ruleset >= MAXRWSETS || ruleset < 0)
2348056Seric 			{
2359381Seric 				syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS);
2368056Seric 				ruleset = 0;
2378056Seric 			}
2384072Seric 			rwp = NULL;
2394072Seric 			break;
2404072Seric 
2413308Seric 		  case 'D':		/* macro definition */
24257135Seric 			define(bp[1], newstr(munchstring(&bp[2])), e);
2433308Seric 			break;
2443308Seric 
2453387Seric 		  case 'H':		/* required header line */
24657135Seric 			(void) chompheader(&bp[1], TRUE, e);
2473387Seric 			break;
2483387Seric 
2494061Seric 		  case 'C':		/* word class */
2504432Seric 		  case 'F':		/* word class from file */
2514432Seric 			/* read list of words from argument or file */
25257135Seric 			if (bp[0] == 'F')
2534432Seric 			{
2544432Seric 				/* read from file */
25557135Seric 				for (p = &bp[2]; *p != '\0' && !isspace(*p); p++)
2564432Seric 					continue;
2574432Seric 				if (*p == '\0')
2584432Seric 					p = "%s";
2594432Seric 				else
2604432Seric 				{
2614432Seric 					*p = '\0';
2624432Seric 					while (isspace(*++p))
2634432Seric 						continue;
2644432Seric 				}
26557135Seric 				fileclass(bp[1], &bp[2], p, safe);
2664432Seric 				break;
2674432Seric 			}
2684061Seric 
2694432Seric 			/* scan the list of words and set class for all */
27057135Seric 			for (p = &bp[2]; *p != '\0'; )
2714061Seric 			{
2724061Seric 				register char *wd;
2734061Seric 				char delim;
2744061Seric 
2754061Seric 				while (*p != '\0' && isspace(*p))
2764061Seric 					p++;
2774061Seric 				wd = p;
2784061Seric 				while (*p != '\0' && !isspace(*p))
2794061Seric 					p++;
2804061Seric 				delim = *p;
2814061Seric 				*p = '\0';
2824061Seric 				if (wd[0] != '\0')
28357135Seric 					setclass(bp[1], wd);
2844061Seric 				*p = delim;
2854061Seric 			}
2864061Seric 			break;
2874061Seric 
2884096Seric 		  case 'M':		/* define mailer */
28957135Seric 			makemailer(&bp[1]);
2904096Seric 			break;
2914096Seric 
2928252Seric 		  case 'O':		/* set option */
29357135Seric 			setoption(bp[1], &bp[2], safe, FALSE);
2948252Seric 			break;
2958252Seric 
2968252Seric 		  case 'P':		/* set precedence */
2978252Seric 			if (NumPriorities >= MAXPRIORITIES)
2988252Seric 			{
2998547Seric 				toomany('P', MAXPRIORITIES);
3008252Seric 				break;
3018252Seric 			}
30257135Seric 			for (p = &bp[1]; *p != '\0' && *p != '=' && *p != '\t'; p++)
3038252Seric 				continue;
3048252Seric 			if (*p == '\0')
3058252Seric 				goto badline;
3068252Seric 			*p = '\0';
30757135Seric 			Priorities[NumPriorities].pri_name = newstr(&bp[1]);
3088252Seric 			Priorities[NumPriorities].pri_val = atoi(++p);
3098252Seric 			NumPriorities++;
3108252Seric 			break;
3118252Seric 
3128547Seric 		  case 'T':		/* trusted user(s) */
31357135Seric 			p = &bp[1];
3148547Seric 			while (*p != '\0')
3158547Seric 			{
3168547Seric 				while (isspace(*p))
3178547Seric 					p++;
3188547Seric 				q = p;
3198547Seric 				while (*p != '\0' && !isspace(*p))
3208547Seric 					p++;
3218547Seric 				if (*p != '\0')
3228547Seric 					*p++ = '\0';
3238547Seric 				if (*q == '\0')
3248547Seric 					continue;
3258547Seric 				for (pv = TrustedUsers; *pv != NULL; pv++)
3268547Seric 					continue;
3278547Seric 				if (pv >= &TrustedUsers[MAXTRUST])
3288547Seric 				{
3298547Seric 					toomany('T', MAXTRUST);
3308547Seric 					break;
3318547Seric 				}
3328547Seric 				*pv = newstr(q);
3338547Seric 			}
3348547Seric 			break;
3358547Seric 
33652645Seric 		  case 'V':		/* configuration syntax version */
33757135Seric 			ConfigLevel = atoi(&bp[1]);
33852645Seric 			break;
33952645Seric 
34053654Seric 		  case 'K':
34157135Seric 			makemapentry(&bp[1]);
34253654Seric 			break;
34353654Seric 
3443308Seric 		  default:
3454061Seric 		  badline:
34657135Seric 			syserr("unknown control line \"%s\"", bp);
3473308Seric 		}
34857135Seric 		if (bp != buf)
34957135Seric 			free(bp);
3503308Seric 	}
35152637Seric 	if (ferror(cf))
35252637Seric 	{
35352647Seric 		syserr("I/O read error", cfname);
35452637Seric 		exit(EX_OSFILE);
35552637Seric 	}
35652637Seric 	fclose(cf);
3579381Seric 	FileName = NULL;
35856836Seric 
35957076Seric 	if (stab("host", ST_MAP, ST_FIND) == NULL)
36057076Seric 	{
36157076Seric 		/* user didn't initialize: set up host map */
36257076Seric 		strcpy(buf, "host host");
36357076Seric 		if (ConfigLevel >= 2)
36457076Seric 			strcat(buf, " -a.");
36557076Seric 		makemapentry(buf);
36657076Seric 	}
3674096Seric }
3684096Seric /*
3698547Seric **  TOOMANY -- signal too many of some option
3708547Seric **
3718547Seric **	Parameters:
3728547Seric **		id -- the id of the error line
3738547Seric **		maxcnt -- the maximum possible values
3748547Seric **
3758547Seric **	Returns:
3768547Seric **		none.
3778547Seric **
3788547Seric **	Side Effects:
3798547Seric **		gives a syserr.
3808547Seric */
3818547Seric 
3828547Seric toomany(id, maxcnt)
3838547Seric 	char id;
3848547Seric 	int maxcnt;
3858547Seric {
3869381Seric 	syserr("too many %c lines, %d max", id, maxcnt);
3878547Seric }
3888547Seric /*
3894432Seric **  FILECLASS -- read members of a class from a file
3904432Seric **
3914432Seric **	Parameters:
3924432Seric **		class -- class to define.
3934432Seric **		filename -- name of file to read.
3944432Seric **		fmt -- scanf string to use for match.
3954432Seric **
3964432Seric **	Returns:
3974432Seric **		none
3984432Seric **
3994432Seric **	Side Effects:
4004432Seric **
4014432Seric **		puts all lines in filename that match a scanf into
4024432Seric **			the named class.
4034432Seric */
4044432Seric 
40554973Seric fileclass(class, filename, fmt, safe)
4064432Seric 	int class;
4074432Seric 	char *filename;
4084432Seric 	char *fmt;
40954973Seric 	bool safe;
4104432Seric {
41125808Seric 	FILE *f;
41254973Seric 	struct stat stbuf;
4134432Seric 	char buf[MAXLINE];
4144432Seric 
41554973Seric 	if (stat(filename, &stbuf) < 0)
41654973Seric 	{
41754973Seric 		syserr("fileclass: cannot stat %s", filename);
41854973Seric 		return;
41954973Seric 	}
42054973Seric 	if (!S_ISREG(stbuf.st_mode))
42154973Seric 	{
42254973Seric 		syserr("fileclass: %s not a regular file", filename);
42354973Seric 		return;
42454973Seric 	}
42554973Seric 	if (!safe && access(filename, R_OK) < 0)
42654973Seric 	{
42754973Seric 		syserr("fileclass: access denied on %s", filename);
42854973Seric 		return;
42954973Seric 	}
43054973Seric 	f = fopen(filename, "r");
4314432Seric 	if (f == NULL)
4324432Seric 	{
43354973Seric 		syserr("fileclass: cannot open %s", filename);
4344432Seric 		return;
4354432Seric 	}
4364432Seric 
4374432Seric 	while (fgets(buf, sizeof buf, f) != NULL)
4384432Seric 	{
4394432Seric 		register STAB *s;
44025808Seric 		register char *p;
44125808Seric # ifdef SCANF
4424432Seric 		char wordbuf[MAXNAME+1];
4434432Seric 
4444432Seric 		if (sscanf(buf, fmt, wordbuf) != 1)
4454432Seric 			continue;
44625808Seric 		p = wordbuf;
44756795Seric # else /* SCANF */
44825808Seric 		p = buf;
44956795Seric # endif /* SCANF */
45025808Seric 
45125808Seric 		/*
45225808Seric 		**  Break up the match into words.
45325808Seric 		*/
45425808Seric 
45525808Seric 		while (*p != '\0')
45625808Seric 		{
45725808Seric 			register char *q;
45825808Seric 
45925808Seric 			/* strip leading spaces */
46025808Seric 			while (isspace(*p))
46125808Seric 				p++;
46225808Seric 			if (*p == '\0')
46325808Seric 				break;
46425808Seric 
46525808Seric 			/* find the end of the word */
46625808Seric 			q = p;
46725808Seric 			while (*p != '\0' && !isspace(*p))
46825808Seric 				p++;
46925808Seric 			if (*p != '\0')
47025808Seric 				*p++ = '\0';
47125808Seric 
47225808Seric 			/* enter the word in the symbol table */
47325808Seric 			s = stab(q, ST_CLASS, ST_ENTER);
47425808Seric 			setbitn(class, s->s_class);
47525808Seric 		}
4764432Seric 	}
4774432Seric 
47854973Seric 	(void) fclose(f);
4794432Seric }
4804432Seric /*
4814096Seric **  MAKEMAILER -- define a new mailer.
4824096Seric **
4834096Seric **	Parameters:
48410327Seric **		line -- description of mailer.  This is in labeled
48510327Seric **			fields.  The fields are:
48610327Seric **			   P -- the path to the mailer
48710327Seric **			   F -- the flags associated with the mailer
48810327Seric **			   A -- the argv for this mailer
48910327Seric **			   S -- the sender rewriting set
49010327Seric **			   R -- the recipient rewriting set
49110327Seric **			   E -- the eol string
49210327Seric **			The first word is the canonical name of the mailer.
4934096Seric **
4944096Seric **	Returns:
4954096Seric **		none.
4964096Seric **
4974096Seric **	Side Effects:
4984096Seric **		enters the mailer into the mailer table.
4994096Seric */
5003308Seric 
50121066Seric makemailer(line)
5024096Seric 	char *line;
5034096Seric {
5044096Seric 	register char *p;
5058067Seric 	register struct mailer *m;
5068067Seric 	register STAB *s;
5078067Seric 	int i;
50810327Seric 	char fcode;
5094096Seric 	extern int NextMailer;
51010327Seric 	extern char **makeargv();
51110327Seric 	extern char *munchstring();
51210327Seric 	extern char *DelimChar;
51310701Seric 	extern long atol();
5144096Seric 
51510327Seric 	/* allocate a mailer and set up defaults */
51610327Seric 	m = (struct mailer *) xalloc(sizeof *m);
51710327Seric 	bzero((char *) m, sizeof *m);
51810327Seric 	m->m_mno = NextMailer;
51910327Seric 	m->m_eol = "\n";
52010327Seric 
52110327Seric 	/* collect the mailer name */
52210327Seric 	for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++)
52310327Seric 		continue;
52410327Seric 	if (*p != '\0')
52510327Seric 		*p++ = '\0';
52610327Seric 	m->m_name = newstr(line);
52710327Seric 
52810327Seric 	/* now scan through and assign info from the fields */
52910327Seric 	while (*p != '\0')
53010327Seric 	{
53110327Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
53210327Seric 			p++;
53310327Seric 
53410327Seric 		/* p now points to field code */
53510327Seric 		fcode = *p;
53610327Seric 		while (*p != '\0' && *p != '=' && *p != ',')
53710327Seric 			p++;
53810327Seric 		if (*p++ != '=')
53910327Seric 		{
54052637Seric 			syserr("mailer %s: `=' expected", m->m_name);
54110327Seric 			return;
54210327Seric 		}
54310327Seric 		while (isspace(*p))
54410327Seric 			p++;
54510327Seric 
54610327Seric 		/* p now points to the field body */
54710327Seric 		p = munchstring(p);
54810327Seric 
54910327Seric 		/* install the field into the mailer struct */
55010327Seric 		switch (fcode)
55110327Seric 		{
55210327Seric 		  case 'P':		/* pathname */
55310327Seric 			m->m_mailer = newstr(p);
55410327Seric 			break;
55510327Seric 
55610327Seric 		  case 'F':		/* flags */
55710687Seric 			for (; *p != '\0'; p++)
55852637Seric 				if (!isspace(*p))
55952637Seric 					setbitn(*p, m->m_flags);
56010327Seric 			break;
56110327Seric 
56210327Seric 		  case 'S':		/* sender rewriting ruleset */
56310327Seric 		  case 'R':		/* recipient rewriting ruleset */
56410327Seric 			i = atoi(p);
56510327Seric 			if (i < 0 || i >= MAXRWSETS)
56610327Seric 			{
56710327Seric 				syserr("invalid rewrite set, %d max", MAXRWSETS);
56810327Seric 				return;
56910327Seric 			}
57010327Seric 			if (fcode == 'S')
57110327Seric 				m->m_s_rwset = i;
57210327Seric 			else
57310327Seric 				m->m_r_rwset = i;
57410327Seric 			break;
57510327Seric 
57610327Seric 		  case 'E':		/* end of line string */
57710327Seric 			m->m_eol = newstr(p);
57810327Seric 			break;
57910327Seric 
58010327Seric 		  case 'A':		/* argument vector */
58110327Seric 			m->m_argv = makeargv(p);
58210327Seric 			break;
58310701Seric 
58410701Seric 		  case 'M':		/* maximum message size */
58510701Seric 			m->m_maxsize = atol(p);
58610701Seric 			break;
58752106Seric 
58852106Seric 		  case 'L':		/* maximum line length */
58952106Seric 			m->m_linelimit = atoi(p);
59052106Seric 			break;
59110327Seric 		}
59210327Seric 
59310327Seric 		p = DelimChar;
59410327Seric 	}
59510327Seric 
59652106Seric 	/* do some heuristic cleanup for back compatibility */
59752106Seric 	if (bitnset(M_LIMITS, m->m_flags))
59852106Seric 	{
59952106Seric 		if (m->m_linelimit == 0)
60052106Seric 			m->m_linelimit = SMTPLINELIM;
60155418Seric 		if (ConfigLevel < 2)
60252106Seric 			setbitn(M_7BITS, m->m_flags);
60352106Seric 	}
60452106Seric 
60510327Seric 	/* now store the mailer away */
6064096Seric 	if (NextMailer >= MAXMAILERS)
6074096Seric 	{
6089381Seric 		syserr("too many mailers defined (%d max)", MAXMAILERS);
6094096Seric 		return;
6104096Seric 	}
61110327Seric 	Mailer[NextMailer++] = m;
61210327Seric 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
61310327Seric 	s->s_mailer = m;
61410327Seric }
61510327Seric /*
61610327Seric **  MUNCHSTRING -- translate a string into internal form.
61710327Seric **
61810327Seric **	Parameters:
61910327Seric **		p -- the string to munch.
62010327Seric **
62110327Seric **	Returns:
62210327Seric **		the munched string.
62310327Seric **
62410327Seric **	Side Effects:
62510327Seric **		Sets "DelimChar" to point to the string that caused us
62610327Seric **		to stop.
62710327Seric */
6284096Seric 
62910327Seric char *
63010327Seric munchstring(p)
63110327Seric 	register char *p;
63210327Seric {
63310327Seric 	register char *q;
63410327Seric 	bool backslash = FALSE;
63510327Seric 	bool quotemode = FALSE;
63610327Seric 	static char buf[MAXLINE];
63710327Seric 	extern char *DelimChar;
6384096Seric 
63910327Seric 	for (q = buf; *p != '\0'; p++)
6404096Seric 	{
64110327Seric 		if (backslash)
64210327Seric 		{
64310327Seric 			/* everything is roughly literal */
64410357Seric 			backslash = FALSE;
64510327Seric 			switch (*p)
64610327Seric 			{
64710327Seric 			  case 'r':		/* carriage return */
64810327Seric 				*q++ = '\r';
64910327Seric 				continue;
65010327Seric 
65110327Seric 			  case 'n':		/* newline */
65210327Seric 				*q++ = '\n';
65310327Seric 				continue;
65410327Seric 
65510327Seric 			  case 'f':		/* form feed */
65610327Seric 				*q++ = '\f';
65710327Seric 				continue;
65810327Seric 
65910327Seric 			  case 'b':		/* backspace */
66010327Seric 				*q++ = '\b';
66110327Seric 				continue;
66210327Seric 			}
66310327Seric 			*q++ = *p;
66410327Seric 		}
66510327Seric 		else
66610327Seric 		{
66710327Seric 			if (*p == '\\')
66810327Seric 				backslash = TRUE;
66910327Seric 			else if (*p == '"')
67010327Seric 				quotemode = !quotemode;
67110327Seric 			else if (quotemode || *p != ',')
67210327Seric 				*q++ = *p;
67310327Seric 			else
67410327Seric 				break;
67510327Seric 		}
6764096Seric 	}
6774096Seric 
67810327Seric 	DelimChar = p;
67910327Seric 	*q++ = '\0';
68010327Seric 	return (buf);
68110327Seric }
68210327Seric /*
68310327Seric **  MAKEARGV -- break up a string into words
68410327Seric **
68510327Seric **	Parameters:
68610327Seric **		p -- the string to break up.
68710327Seric **
68810327Seric **	Returns:
68910327Seric **		a char **argv (dynamically allocated)
69010327Seric **
69110327Seric **	Side Effects:
69210327Seric **		munges p.
69310327Seric */
6944096Seric 
69510327Seric char **
69610327Seric makeargv(p)
69710327Seric 	register char *p;
69810327Seric {
69910327Seric 	char *q;
70010327Seric 	int i;
70110327Seric 	char **avp;
70210327Seric 	char *argv[MAXPV + 1];
70310327Seric 
70410327Seric 	/* take apart the words */
70510327Seric 	i = 0;
70610327Seric 	while (*p != '\0' && i < MAXPV)
7074096Seric 	{
70810327Seric 		q = p;
70910327Seric 		while (*p != '\0' && !isspace(*p))
71010327Seric 			p++;
71110327Seric 		while (isspace(*p))
71210327Seric 			*p++ = '\0';
71310327Seric 		argv[i++] = newstr(q);
7144096Seric 	}
71510327Seric 	argv[i++] = NULL;
7164096Seric 
71710327Seric 	/* now make a copy of the argv */
71810327Seric 	avp = (char **) xalloc(sizeof *avp * i);
71916893Seric 	bcopy((char *) argv, (char *) avp, sizeof *avp * i);
72010327Seric 
72110327Seric 	return (avp);
7223308Seric }
7233308Seric /*
7243308Seric **  PRINTRULES -- print rewrite rules (for debugging)
7253308Seric **
7263308Seric **	Parameters:
7273308Seric **		none.
7283308Seric **
7293308Seric **	Returns:
7303308Seric **		none.
7313308Seric **
7323308Seric **	Side Effects:
7333308Seric **		prints rewrite rules.
7343308Seric */
7353308Seric 
7363308Seric printrules()
7373308Seric {
7383308Seric 	register struct rewrite *rwp;
7394072Seric 	register int ruleset;
7403308Seric 
7414072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
7423308Seric 	{
7434072Seric 		if (RewriteRules[ruleset] == NULL)
7444072Seric 			continue;
7458067Seric 		printf("\n----Rule Set %d:", ruleset);
7463308Seric 
7474072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
7483308Seric 		{
7498067Seric 			printf("\nLHS:");
7508067Seric 			printav(rwp->r_lhs);
7518067Seric 			printf("RHS:");
7528067Seric 			printav(rwp->r_rhs);
7533308Seric 		}
7543308Seric 	}
7553308Seric }
7564319Seric 
7574096Seric /*
7588256Seric **  SETOPTION -- set global processing option
7598256Seric **
7608256Seric **	Parameters:
7618256Seric **		opt -- option name.
7628256Seric **		val -- option value (as a text string).
76321755Seric **		safe -- set if this came from a configuration file.
76421755Seric **			Some options (if set from the command line) will
76521755Seric **			reset the user id to avoid security problems.
7668269Seric **		sticky -- if set, don't let other setoptions override
7678269Seric **			this value.
7688256Seric **
7698256Seric **	Returns:
7708256Seric **		none.
7718256Seric **
7728256Seric **	Side Effects:
7738256Seric **		Sets options as implied by the arguments.
7748256Seric */
7758256Seric 
77610687Seric static BITMAP	StickyOpt;		/* set if option is stuck */
7778269Seric 
778*57207Seric 
779*57207Seric #ifdef NAMED_BIND
780*57207Seric 
781*57207Seric struct resolverflags
782*57207Seric {
783*57207Seric 	char	*rf_name;	/* name of the flag */
784*57207Seric 	long	rf_bits;	/* bits to set/clear */
785*57207Seric } ResolverFlags[] =
786*57207Seric {
787*57207Seric 	"debug",	RES_DEBUG,
788*57207Seric 	"aaonly",	RES_AAONLY,
789*57207Seric 	"usevc",	RES_USEVC,
790*57207Seric 	"primary",	RES_PRIMARY,
791*57207Seric 	"igntc",	RES_IGNTC,
792*57207Seric 	"recurse",	RES_RECURSE,
793*57207Seric 	"defnames",	RES_DEFNAMES,
794*57207Seric 	"stayopen",	RES_STAYOPEN,
795*57207Seric 	"dnsrch",	RES_DNSRCH,
796*57207Seric 	NULL,		0
797*57207Seric };
798*57207Seric 
799*57207Seric #endif
800*57207Seric 
80121755Seric setoption(opt, val, safe, sticky)
8028256Seric 	char opt;
8038256Seric 	char *val;
80421755Seric 	bool safe;
8058269Seric 	bool sticky;
8068256Seric {
807*57207Seric 	register char *p;
8088265Seric 	extern bool atobool();
80912633Seric 	extern time_t convtime();
81014879Seric 	extern int QueueLA;
81114879Seric 	extern int RefuseLA;
81217474Seric 	extern bool trusteduser();
81317474Seric 	extern char *username();
8148256Seric 
8158256Seric 	if (tTd(37, 1))
8169341Seric 		printf("setoption %c=%s", opt, val);
8178256Seric 
8188256Seric 	/*
8198269Seric 	**  See if this option is preset for us.
8208256Seric 	*/
8218256Seric 
82210687Seric 	if (bitnset(opt, StickyOpt))
8238269Seric 	{
8249341Seric 		if (tTd(37, 1))
8259341Seric 			printf(" (ignored)\n");
8268269Seric 		return;
8278269Seric 	}
8288269Seric 
82921755Seric 	/*
83021755Seric 	**  Check to see if this option can be specified by this user.
83121755Seric 	*/
83221755Seric 
83336238Skarels 	if (!safe && getuid() == 0)
83421755Seric 		safe = TRUE;
83557136Seric 	if (!safe && strchr("deEiLmorsvC8", opt) == NULL)
83621755Seric 	{
83739111Srick 		if (opt != 'M' || (val[0] != 'r' && val[0] != 's'))
83821755Seric 		{
83936582Sbostic 			if (tTd(37, 1))
84036582Sbostic 				printf(" (unsafe)");
84136582Sbostic 			if (getuid() != geteuid())
84236582Sbostic 			{
84351210Seric 				if (tTd(37, 1))
84451210Seric 					printf("(Resetting uid)");
84536582Sbostic 				(void) setgid(getgid());
84636582Sbostic 				(void) setuid(getuid());
84736582Sbostic 			}
84821755Seric 		}
84921755Seric 	}
85051210Seric 	if (tTd(37, 1))
85117985Seric 		printf("\n");
8528269Seric 
8538256Seric 	switch (opt)
8548256Seric 	{
85552106Seric 	  case '8':		/* allow eight-bit input */
85652106Seric 		EightBit = atobool(val);
85752106Seric 		break;
85852106Seric 
8598256Seric 	  case 'A':		/* set default alias file */
8609381Seric 		if (val[0] == '\0')
8618269Seric 			AliasFile = "aliases";
8629381Seric 		else
8639381Seric 			AliasFile = newstr(val);
8648256Seric 		break;
8658256Seric 
86617474Seric 	  case 'a':		/* look N minutes for "@:@" in alias file */
86717474Seric 		if (val[0] == '\0')
86817474Seric 			SafeAlias = 5;
86917474Seric 		else
87017474Seric 			SafeAlias = atoi(val);
87117474Seric 		break;
87217474Seric 
87316843Seric 	  case 'B':		/* substitution for blank character */
87416843Seric 		SpaceSub = val[0];
87516843Seric 		if (SpaceSub == '\0')
87616843Seric 			SpaceSub = ' ';
87716843Seric 		break;
87816843Seric 
8799284Seric 	  case 'c':		/* don't connect to "expensive" mailers */
8809381Seric 		NoConnect = atobool(val);
8819284Seric 		break;
8829284Seric 
88351305Seric 	  case 'C':		/* checkpoint every N addresses */
88451305Seric 		CheckpointInterval = atoi(val);
88524944Seric 		break;
88624944Seric 
8879284Seric 	  case 'd':		/* delivery mode */
8889284Seric 		switch (*val)
8898269Seric 		{
8909284Seric 		  case '\0':
8919284Seric 			SendMode = SM_DELIVER;
8928269Seric 			break;
8938269Seric 
89410755Seric 		  case SM_QUEUE:	/* queue only */
89510755Seric #ifndef QUEUE
89610755Seric 			syserr("need QUEUE to set -odqueue");
89756795Seric #endif /* QUEUE */
89810755Seric 			/* fall through..... */
89910755Seric 
9009284Seric 		  case SM_DELIVER:	/* do everything */
9019284Seric 		  case SM_FORK:		/* fork after verification */
9029284Seric 			SendMode = *val;
9038269Seric 			break;
9048269Seric 
9058269Seric 		  default:
9069284Seric 			syserr("Unknown delivery mode %c", *val);
9078269Seric 			exit(EX_USAGE);
9088269Seric 		}
9098269Seric 		break;
9108269Seric 
9119146Seric 	  case 'D':		/* rebuild alias database as needed */
9129381Seric 		AutoRebuild = atobool(val);
9139146Seric 		break;
9149146Seric 
91555372Seric 	  case 'E':		/* error message header/header file */
91655379Seric 		if (*val != '\0')
91755379Seric 			ErrMsgFile = newstr(val);
91855372Seric 		break;
91955372Seric 
9208269Seric 	  case 'e':		/* set error processing mode */
9218269Seric 		switch (*val)
9228269Seric 		{
9239381Seric 		  case EM_QUIET:	/* be silent about it */
9249381Seric 		  case EM_MAIL:		/* mail back */
9259381Seric 		  case EM_BERKNET:	/* do berknet error processing */
9269381Seric 		  case EM_WRITE:	/* write back (or mail) */
9278269Seric 			HoldErrs = TRUE;
9289381Seric 			/* fall through... */
9298269Seric 
9309381Seric 		  case EM_PRINT:	/* print errors normally (default) */
9319381Seric 			ErrorMode = *val;
9328269Seric 			break;
9338269Seric 		}
9348269Seric 		break;
9358269Seric 
9369049Seric 	  case 'F':		/* file mode */
93717975Seric 		FileMode = atooct(val) & 0777;
9389049Seric 		break;
9399049Seric 
9408269Seric 	  case 'f':		/* save Unix-style From lines on front */
9419381Seric 		SaveFrom = atobool(val);
9428269Seric 		break;
9438269Seric 
94453735Seric 	  case 'G':		/* match recipients against GECOS field */
94553735Seric 		MatchGecos = atobool(val);
94653735Seric 		break;
94753735Seric 
9488256Seric 	  case 'g':		/* default gid */
94917474Seric 		DefGid = atoi(val);
9508256Seric 		break;
9518256Seric 
9528256Seric 	  case 'H':		/* help file */
9539381Seric 		if (val[0] == '\0')
9548269Seric 			HelpFile = "sendmail.hf";
9559381Seric 		else
9569381Seric 			HelpFile = newstr(val);
9578256Seric 		break;
9588256Seric 
95951305Seric 	  case 'h':		/* maximum hop count */
96051305Seric 		MaxHopCount = atoi(val);
96151305Seric 		break;
96251305Seric 
96335651Seric 	  case 'I':		/* use internet domain name server */
964*57207Seric #ifdef NAMED_BIND
965*57207Seric 		UseNameServer = TRUE;
966*57207Seric 		for (p = val; *p != 0; )
967*57207Seric 		{
968*57207Seric 			bool clearmode;
969*57207Seric 			char *q;
970*57207Seric 			struct resolverflags *rfp;
971*57207Seric 
972*57207Seric 			while (*p == ' ')
973*57207Seric 				p++;
974*57207Seric 			if (*p == '\0')
975*57207Seric 				break;
976*57207Seric 			clearmode = FALSE;
977*57207Seric 			if (*p == '-')
978*57207Seric 				clearmode = TRUE;
979*57207Seric 			else if (*p != '+')
980*57207Seric 				p--;
981*57207Seric 			p++;
982*57207Seric 			q = p;
983*57207Seric 			while (*p != '\0' && !isspace(*p))
984*57207Seric 				p++;
985*57207Seric 			if (*p != '\0')
986*57207Seric 				*p++ = '\0';
987*57207Seric 			for (rfp = ResolverFlags; rfp->rf_name != NULL; rfp++)
988*57207Seric 			{
989*57207Seric 				if (strcasecmp(q, rfp->rf_name) == 0)
990*57207Seric 					break;
991*57207Seric 			}
992*57207Seric 			if (clearmode)
993*57207Seric 				_res.options &= ~rfp->rf_bits;
994*57207Seric 			else
995*57207Seric 				_res.options |= rfp->rf_bits;
996*57207Seric 		}
997*57207Seric 		if (tTd(8, 2))
998*57207Seric 			printf("_res.options = %x\n", _res.options);
999*57207Seric #else
1000*57207Seric 		usrerr("name server (I option) specified but BIND not compiled in");
1001*57207Seric #endif
100235651Seric 		break;
100335651Seric 
10048269Seric 	  case 'i':		/* ignore dot lines in message */
10059381Seric 		IgnrDot = atobool(val);
10068269Seric 		break;
10078269Seric 
100857136Seric 	  case 'J':		/* .forward search path */
100957136Seric 		ForwardPath = newstr(val);
101057136Seric 		break;
101157136Seric 
101254967Seric 	  case 'k':		/* connection cache size */
101354967Seric 		MaxMciCache = atoi(val);
101456215Seric 		if (MaxMciCache < 0)
101556215Seric 			MaxMciCache = 0;
101654967Seric 		break;
101754967Seric 
101854967Seric 	  case 'K':		/* connection cache timeout */
101954967Seric 		MciCacheTimeout = convtime(val);
102054967Seric 		break;
102154967Seric 
10228256Seric 	  case 'L':		/* log level */
10239381Seric 		LogLevel = atoi(val);
10248256Seric 		break;
10258256Seric 
10268269Seric 	  case 'M':		/* define macro */
10279381Seric 		define(val[0], newstr(&val[1]), CurEnv);
102816878Seric 		sticky = FALSE;
10298269Seric 		break;
10308269Seric 
10318269Seric 	  case 'm':		/* send to me too */
10329381Seric 		MeToo = atobool(val);
10338269Seric 		break;
10348269Seric 
103525820Seric 	  case 'n':		/* validate RHS in newaliases */
103625820Seric 		CheckAliases = atobool(val);
103725820Seric 		break;
103825820Seric 
10398269Seric 	  case 'o':		/* assume old style headers */
10409381Seric 		if (atobool(val))
10419341Seric 			CurEnv->e_flags |= EF_OLDSTYLE;
10429341Seric 		else
10439341Seric 			CurEnv->e_flags &= ~EF_OLDSTYLE;
10448269Seric 		break;
10458269Seric 
104624944Seric 	  case 'P':		/* postmaster copy address for returned mail */
104724944Seric 		PostMasterCopy = newstr(val);
104824944Seric 		break;
104924944Seric 
105024944Seric 	  case 'q':		/* slope of queue only function */
105124944Seric 		QueueFactor = atoi(val);
105224944Seric 		break;
105324944Seric 
10548256Seric 	  case 'Q':		/* queue directory */
10559381Seric 		if (val[0] == '\0')
10568269Seric 			QueueDir = "mqueue";
10579381Seric 		else
10589381Seric 			QueueDir = newstr(val);
10598256Seric 		break;
10608256Seric 
10618256Seric 	  case 'r':		/* read timeout */
10629381Seric 		ReadTimeout = convtime(val);
10638256Seric 		break;
10648256Seric 
10658256Seric 	  case 'S':		/* status file */
10669381Seric 		if (val[0] == '\0')
10678269Seric 			StatFile = "sendmail.st";
10689381Seric 		else
10699381Seric 			StatFile = newstr(val);
10708256Seric 		break;
10718256Seric 
10728265Seric 	  case 's':		/* be super safe, even if expensive */
10739381Seric 		SuperSafe = atobool(val);
10748256Seric 		break;
10758256Seric 
10768256Seric 	  case 'T':		/* queue timeout */
10779381Seric 		TimeOut = convtime(val);
107854967Seric 		break;
10798256Seric 
10808265Seric 	  case 't':		/* time zone name */
108152106Seric 		TimeZoneSpec = newstr(val);
10828265Seric 		break;
10838265Seric 
108450556Seric 	  case 'U':		/* location of user database */
108551360Seric 		UdbSpec = newstr(val);
108650556Seric 		break;
108750556Seric 
10888256Seric 	  case 'u':		/* set default uid */
108917474Seric 		DefUid = atoi(val);
109040973Sbostic 		setdefuser();
10918256Seric 		break;
10928256Seric 
10938269Seric 	  case 'v':		/* run in verbose mode */
10949381Seric 		Verbose = atobool(val);
10958256Seric 		break;
10968256Seric 
109751216Seric 	  case 'w':		/* we don't have wildcard MX records */
109851216Seric 		NoWildcardMX = atobool(val);
109950537Seric 		break;
110050537Seric 
110114879Seric 	  case 'x':		/* load avg at which to auto-queue msgs */
110214879Seric 		QueueLA = atoi(val);
110314879Seric 		break;
110414879Seric 
110514879Seric 	  case 'X':		/* load avg at which to auto-reject connections */
110614879Seric 		RefuseLA = atoi(val);
110714879Seric 		break;
110814879Seric 
110924981Seric 	  case 'y':		/* work recipient factor */
111024981Seric 		WkRecipFact = atoi(val);
111124981Seric 		break;
111224981Seric 
111324981Seric 	  case 'Y':		/* fork jobs during queue runs */
111424952Seric 		ForkQueueRuns = atobool(val);
111524952Seric 		break;
111624952Seric 
111724981Seric 	  case 'z':		/* work message class factor */
111824981Seric 		WkClassFact = atoi(val);
111924981Seric 		break;
112024981Seric 
112124981Seric 	  case 'Z':		/* work time factor */
112224981Seric 		WkTimeFact = atoi(val);
112324981Seric 		break;
112424981Seric 
11258256Seric 	  default:
11268256Seric 		break;
11278256Seric 	}
112816878Seric 	if (sticky)
112916878Seric 		setbitn(opt, StickyOpt);
11309188Seric 	return;
11318256Seric }
113210687Seric /*
113310687Seric **  SETCLASS -- set a word into a class
113410687Seric **
113510687Seric **	Parameters:
113610687Seric **		class -- the class to put the word in.
113710687Seric **		word -- the word to enter
113810687Seric **
113910687Seric **	Returns:
114010687Seric **		none.
114110687Seric **
114210687Seric **	Side Effects:
114310687Seric **		puts the word into the symbol table.
114410687Seric */
114510687Seric 
114610687Seric setclass(class, word)
114710687Seric 	int class;
114810687Seric 	char *word;
114910687Seric {
115010687Seric 	register STAB *s;
115110687Seric 
115210687Seric 	s = stab(word, ST_CLASS, ST_ENTER);
115310687Seric 	setbitn(class, s->s_class);
115410687Seric }
115553654Seric /*
115653654Seric **  MAKEMAPENTRY -- create a map entry
115753654Seric **
115853654Seric **	Parameters:
115953654Seric **		line -- the config file line
116053654Seric **
116153654Seric **	Returns:
116253654Seric **		TRUE if it successfully entered the map entry.
116353654Seric **		FALSE otherwise (usually syntax error).
116453654Seric **
116553654Seric **	Side Effects:
116653654Seric **		Enters the map into the dictionary.
116753654Seric */
116853654Seric 
116953654Seric void
117053654Seric makemapentry(line)
117153654Seric 	char *line;
117253654Seric {
117353654Seric 	register char *p;
117453654Seric 	char *mapname;
117553654Seric 	char *classname;
117653654Seric 	register STAB *map;
117753654Seric 	STAB *class;
117853654Seric 
117953654Seric 	for (p = line; isspace(*p); p++)
118053654Seric 		continue;
118153654Seric 	if (!isalnum(*p))
118253654Seric 	{
118353654Seric 		syserr("readcf: config K line: no map name");
118453654Seric 		return;
118553654Seric 	}
118653654Seric 
118753654Seric 	mapname = p;
118853654Seric 	while (isalnum(*++p))
118953654Seric 		continue;
119053654Seric 	if (*p != '\0')
119153654Seric 		*p++ = '\0';
119253654Seric 	while (isspace(*p))
119353654Seric 		p++;
119453654Seric 	if (!isalnum(*p))
119553654Seric 	{
119653654Seric 		syserr("readcf: config K line, map %s: no map class", mapname);
119753654Seric 		return;
119853654Seric 	}
119953654Seric 	classname = p;
120053654Seric 	while (isalnum(*++p))
120153654Seric 		continue;
120253654Seric 	if (*p != '\0')
120353654Seric 		*p++ = '\0';
120453654Seric 	while (isspace(*p))
120553654Seric 		p++;
120653654Seric 
120753654Seric 	/* look up the class */
120853654Seric 	class = stab(classname, ST_MAPCLASS, ST_FIND);
120953654Seric 	if (class == NULL)
121053654Seric 	{
121153654Seric 		syserr("readcf: map %s: class %s not available", mapname, classname);
121253654Seric 		return;
121353654Seric 	}
121453654Seric 
121553654Seric 	/* enter the map */
121653654Seric 	map = stab(mapname, ST_MAP, ST_ENTER);
121753654Seric 	map->s_map.map_class = &class->s_mapclass;
121853654Seric 
121956823Seric 	if ((*class->s_mapclass.map_init)(&map->s_map, mapname, p))
122053654Seric 		map->s_map.map_flags |= MF_VALID;
122153654Seric }
1222