xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 57402)
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*57402Seric static char sccsid[] = "@(#)readcf.c	6.2 (Berkeley) 01/02/93";
1133731Sbostic #endif /* not lint */
1222709Sdist 
133313Seric # include "sendmail.h"
1452647Seric # include <sys/stat.h>
1554973Seric # include <unistd.h>
1657207Seric #ifdef NAMED_BIND
1757207Seric # include <arpa/nameser.h>
1857207Seric # include <resolv.h>
1957207Seric #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_eol = "\n";
51910327Seric 
52010327Seric 	/* collect the mailer name */
52110327Seric 	for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++)
52210327Seric 		continue;
52310327Seric 	if (*p != '\0')
52410327Seric 		*p++ = '\0';
52510327Seric 	m->m_name = newstr(line);
52610327Seric 
52710327Seric 	/* now scan through and assign info from the fields */
52810327Seric 	while (*p != '\0')
52910327Seric 	{
53010327Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
53110327Seric 			p++;
53210327Seric 
53310327Seric 		/* p now points to field code */
53410327Seric 		fcode = *p;
53510327Seric 		while (*p != '\0' && *p != '=' && *p != ',')
53610327Seric 			p++;
53710327Seric 		if (*p++ != '=')
53810327Seric 		{
53952637Seric 			syserr("mailer %s: `=' expected", m->m_name);
54010327Seric 			return;
54110327Seric 		}
54210327Seric 		while (isspace(*p))
54310327Seric 			p++;
54410327Seric 
54510327Seric 		/* p now points to the field body */
54610327Seric 		p = munchstring(p);
54710327Seric 
54810327Seric 		/* install the field into the mailer struct */
54910327Seric 		switch (fcode)
55010327Seric 		{
55110327Seric 		  case 'P':		/* pathname */
55210327Seric 			m->m_mailer = newstr(p);
55310327Seric 			break;
55410327Seric 
55510327Seric 		  case 'F':		/* flags */
55610687Seric 			for (; *p != '\0'; p++)
55752637Seric 				if (!isspace(*p))
55852637Seric 					setbitn(*p, m->m_flags);
55910327Seric 			break;
56010327Seric 
56110327Seric 		  case 'S':		/* sender rewriting ruleset */
56210327Seric 		  case 'R':		/* recipient rewriting ruleset */
56310327Seric 			i = atoi(p);
56410327Seric 			if (i < 0 || i >= MAXRWSETS)
56510327Seric 			{
56610327Seric 				syserr("invalid rewrite set, %d max", MAXRWSETS);
56710327Seric 				return;
56810327Seric 			}
56910327Seric 			if (fcode == 'S')
57010327Seric 				m->m_s_rwset = i;
57110327Seric 			else
57210327Seric 				m->m_r_rwset = i;
57310327Seric 			break;
57410327Seric 
57510327Seric 		  case 'E':		/* end of line string */
57610327Seric 			m->m_eol = newstr(p);
57710327Seric 			break;
57810327Seric 
57910327Seric 		  case 'A':		/* argument vector */
58010327Seric 			m->m_argv = makeargv(p);
58110327Seric 			break;
58210701Seric 
58310701Seric 		  case 'M':		/* maximum message size */
58410701Seric 			m->m_maxsize = atol(p);
58510701Seric 			break;
58652106Seric 
58752106Seric 		  case 'L':		/* maximum line length */
58852106Seric 			m->m_linelimit = atoi(p);
58952106Seric 			break;
59010327Seric 		}
59110327Seric 
59210327Seric 		p = DelimChar;
59310327Seric 	}
59410327Seric 
59552106Seric 	/* do some heuristic cleanup for back compatibility */
59652106Seric 	if (bitnset(M_LIMITS, m->m_flags))
59752106Seric 	{
59852106Seric 		if (m->m_linelimit == 0)
59952106Seric 			m->m_linelimit = SMTPLINELIM;
60055418Seric 		if (ConfigLevel < 2)
60152106Seric 			setbitn(M_7BITS, m->m_flags);
60252106Seric 	}
60352106Seric 
6044096Seric 	if (NextMailer >= MAXMAILERS)
6054096Seric 	{
6069381Seric 		syserr("too many mailers defined (%d max)", MAXMAILERS);
6074096Seric 		return;
6084096Seric 	}
609*57402Seric 
61010327Seric 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
611*57402Seric 	if (s->s_mailer != NULL)
612*57402Seric 	{
613*57402Seric 		i = s->s_mailer->m_mno;
614*57402Seric 		free(s->s_mailer);
615*57402Seric 	}
616*57402Seric 	else
617*57402Seric 	{
618*57402Seric 		i = NextMailer++;
619*57402Seric 	}
620*57402Seric 	Mailer[i] = s->s_mailer = m;
62110327Seric }
62210327Seric /*
62310327Seric **  MUNCHSTRING -- translate a string into internal form.
62410327Seric **
62510327Seric **	Parameters:
62610327Seric **		p -- the string to munch.
62710327Seric **
62810327Seric **	Returns:
62910327Seric **		the munched string.
63010327Seric **
63110327Seric **	Side Effects:
63210327Seric **		Sets "DelimChar" to point to the string that caused us
63310327Seric **		to stop.
63410327Seric */
6354096Seric 
63610327Seric char *
63710327Seric munchstring(p)
63810327Seric 	register char *p;
63910327Seric {
64010327Seric 	register char *q;
64110327Seric 	bool backslash = FALSE;
64210327Seric 	bool quotemode = FALSE;
64310327Seric 	static char buf[MAXLINE];
64410327Seric 	extern char *DelimChar;
6454096Seric 
64610327Seric 	for (q = buf; *p != '\0'; p++)
6474096Seric 	{
64810327Seric 		if (backslash)
64910327Seric 		{
65010327Seric 			/* everything is roughly literal */
65110357Seric 			backslash = FALSE;
65210327Seric 			switch (*p)
65310327Seric 			{
65410327Seric 			  case 'r':		/* carriage return */
65510327Seric 				*q++ = '\r';
65610327Seric 				continue;
65710327Seric 
65810327Seric 			  case 'n':		/* newline */
65910327Seric 				*q++ = '\n';
66010327Seric 				continue;
66110327Seric 
66210327Seric 			  case 'f':		/* form feed */
66310327Seric 				*q++ = '\f';
66410327Seric 				continue;
66510327Seric 
66610327Seric 			  case 'b':		/* backspace */
66710327Seric 				*q++ = '\b';
66810327Seric 				continue;
66910327Seric 			}
67010327Seric 			*q++ = *p;
67110327Seric 		}
67210327Seric 		else
67310327Seric 		{
67410327Seric 			if (*p == '\\')
67510327Seric 				backslash = TRUE;
67610327Seric 			else if (*p == '"')
67710327Seric 				quotemode = !quotemode;
67810327Seric 			else if (quotemode || *p != ',')
67910327Seric 				*q++ = *p;
68010327Seric 			else
68110327Seric 				break;
68210327Seric 		}
6834096Seric 	}
6844096Seric 
68510327Seric 	DelimChar = p;
68610327Seric 	*q++ = '\0';
68710327Seric 	return (buf);
68810327Seric }
68910327Seric /*
69010327Seric **  MAKEARGV -- break up a string into words
69110327Seric **
69210327Seric **	Parameters:
69310327Seric **		p -- the string to break up.
69410327Seric **
69510327Seric **	Returns:
69610327Seric **		a char **argv (dynamically allocated)
69710327Seric **
69810327Seric **	Side Effects:
69910327Seric **		munges p.
70010327Seric */
7014096Seric 
70210327Seric char **
70310327Seric makeargv(p)
70410327Seric 	register char *p;
70510327Seric {
70610327Seric 	char *q;
70710327Seric 	int i;
70810327Seric 	char **avp;
70910327Seric 	char *argv[MAXPV + 1];
71010327Seric 
71110327Seric 	/* take apart the words */
71210327Seric 	i = 0;
71310327Seric 	while (*p != '\0' && i < MAXPV)
7144096Seric 	{
71510327Seric 		q = p;
71610327Seric 		while (*p != '\0' && !isspace(*p))
71710327Seric 			p++;
71810327Seric 		while (isspace(*p))
71910327Seric 			*p++ = '\0';
72010327Seric 		argv[i++] = newstr(q);
7214096Seric 	}
72210327Seric 	argv[i++] = NULL;
7234096Seric 
72410327Seric 	/* now make a copy of the argv */
72510327Seric 	avp = (char **) xalloc(sizeof *avp * i);
72616893Seric 	bcopy((char *) argv, (char *) avp, sizeof *avp * i);
72710327Seric 
72810327Seric 	return (avp);
7293308Seric }
7303308Seric /*
7313308Seric **  PRINTRULES -- print rewrite rules (for debugging)
7323308Seric **
7333308Seric **	Parameters:
7343308Seric **		none.
7353308Seric **
7363308Seric **	Returns:
7373308Seric **		none.
7383308Seric **
7393308Seric **	Side Effects:
7403308Seric **		prints rewrite rules.
7413308Seric */
7423308Seric 
7433308Seric printrules()
7443308Seric {
7453308Seric 	register struct rewrite *rwp;
7464072Seric 	register int ruleset;
7473308Seric 
7484072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
7493308Seric 	{
7504072Seric 		if (RewriteRules[ruleset] == NULL)
7514072Seric 			continue;
7528067Seric 		printf("\n----Rule Set %d:", ruleset);
7533308Seric 
7544072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
7553308Seric 		{
7568067Seric 			printf("\nLHS:");
7578067Seric 			printav(rwp->r_lhs);
7588067Seric 			printf("RHS:");
7598067Seric 			printav(rwp->r_rhs);
7603308Seric 		}
7613308Seric 	}
7623308Seric }
7634319Seric 
7644096Seric /*
7658256Seric **  SETOPTION -- set global processing option
7668256Seric **
7678256Seric **	Parameters:
7688256Seric **		opt -- option name.
7698256Seric **		val -- option value (as a text string).
77021755Seric **		safe -- set if this came from a configuration file.
77121755Seric **			Some options (if set from the command line) will
77221755Seric **			reset the user id to avoid security problems.
7738269Seric **		sticky -- if set, don't let other setoptions override
7748269Seric **			this value.
7758256Seric **
7768256Seric **	Returns:
7778256Seric **		none.
7788256Seric **
7798256Seric **	Side Effects:
7808256Seric **		Sets options as implied by the arguments.
7818256Seric */
7828256Seric 
78310687Seric static BITMAP	StickyOpt;		/* set if option is stuck */
7848269Seric 
78557207Seric 
78657207Seric #ifdef NAMED_BIND
78757207Seric 
78857207Seric struct resolverflags
78957207Seric {
79057207Seric 	char	*rf_name;	/* name of the flag */
79157207Seric 	long	rf_bits;	/* bits to set/clear */
79257207Seric } ResolverFlags[] =
79357207Seric {
79457207Seric 	"debug",	RES_DEBUG,
79557207Seric 	"aaonly",	RES_AAONLY,
79657207Seric 	"usevc",	RES_USEVC,
79757207Seric 	"primary",	RES_PRIMARY,
79857207Seric 	"igntc",	RES_IGNTC,
79957207Seric 	"recurse",	RES_RECURSE,
80057207Seric 	"defnames",	RES_DEFNAMES,
80157207Seric 	"stayopen",	RES_STAYOPEN,
80257207Seric 	"dnsrch",	RES_DNSRCH,
80357207Seric 	NULL,		0
80457207Seric };
80557207Seric 
80657207Seric #endif
80757207Seric 
80821755Seric setoption(opt, val, safe, sticky)
8098256Seric 	char opt;
8108256Seric 	char *val;
81121755Seric 	bool safe;
8128269Seric 	bool sticky;
8138256Seric {
81457207Seric 	register char *p;
8158265Seric 	extern bool atobool();
81612633Seric 	extern time_t convtime();
81714879Seric 	extern int QueueLA;
81814879Seric 	extern int RefuseLA;
81917474Seric 	extern bool trusteduser();
82017474Seric 	extern char *username();
8218256Seric 
8228256Seric 	if (tTd(37, 1))
8239341Seric 		printf("setoption %c=%s", opt, val);
8248256Seric 
8258256Seric 	/*
8268269Seric 	**  See if this option is preset for us.
8278256Seric 	*/
8288256Seric 
82910687Seric 	if (bitnset(opt, StickyOpt))
8308269Seric 	{
8319341Seric 		if (tTd(37, 1))
8329341Seric 			printf(" (ignored)\n");
8338269Seric 		return;
8348269Seric 	}
8358269Seric 
83621755Seric 	/*
83721755Seric 	**  Check to see if this option can be specified by this user.
83821755Seric 	*/
83921755Seric 
84036238Skarels 	if (!safe && getuid() == 0)
84121755Seric 		safe = TRUE;
84257136Seric 	if (!safe && strchr("deEiLmorsvC8", opt) == NULL)
84321755Seric 	{
84439111Srick 		if (opt != 'M' || (val[0] != 'r' && val[0] != 's'))
84521755Seric 		{
84636582Sbostic 			if (tTd(37, 1))
84736582Sbostic 				printf(" (unsafe)");
84836582Sbostic 			if (getuid() != geteuid())
84936582Sbostic 			{
85051210Seric 				if (tTd(37, 1))
85151210Seric 					printf("(Resetting uid)");
85236582Sbostic 				(void) setgid(getgid());
85336582Sbostic 				(void) setuid(getuid());
85436582Sbostic 			}
85521755Seric 		}
85621755Seric 	}
85751210Seric 	if (tTd(37, 1))
85817985Seric 		printf("\n");
8598269Seric 
8608256Seric 	switch (opt)
8618256Seric 	{
86252106Seric 	  case '8':		/* allow eight-bit input */
86352106Seric 		EightBit = atobool(val);
86452106Seric 		break;
86552106Seric 
8668256Seric 	  case 'A':		/* set default alias file */
8679381Seric 		if (val[0] == '\0')
8688269Seric 			AliasFile = "aliases";
8699381Seric 		else
8709381Seric 			AliasFile = newstr(val);
8718256Seric 		break;
8728256Seric 
87317474Seric 	  case 'a':		/* look N minutes for "@:@" in alias file */
87417474Seric 		if (val[0] == '\0')
87517474Seric 			SafeAlias = 5;
87617474Seric 		else
87717474Seric 			SafeAlias = atoi(val);
87817474Seric 		break;
87917474Seric 
88016843Seric 	  case 'B':		/* substitution for blank character */
88116843Seric 		SpaceSub = val[0];
88216843Seric 		if (SpaceSub == '\0')
88316843Seric 			SpaceSub = ' ';
88416843Seric 		break;
88516843Seric 
8869284Seric 	  case 'c':		/* don't connect to "expensive" mailers */
8879381Seric 		NoConnect = atobool(val);
8889284Seric 		break;
8899284Seric 
89051305Seric 	  case 'C':		/* checkpoint every N addresses */
89151305Seric 		CheckpointInterval = atoi(val);
89224944Seric 		break;
89324944Seric 
8949284Seric 	  case 'd':		/* delivery mode */
8959284Seric 		switch (*val)
8968269Seric 		{
8979284Seric 		  case '\0':
8989284Seric 			SendMode = SM_DELIVER;
8998269Seric 			break;
9008269Seric 
90110755Seric 		  case SM_QUEUE:	/* queue only */
90210755Seric #ifndef QUEUE
90310755Seric 			syserr("need QUEUE to set -odqueue");
90456795Seric #endif /* QUEUE */
90510755Seric 			/* fall through..... */
90610755Seric 
9079284Seric 		  case SM_DELIVER:	/* do everything */
9089284Seric 		  case SM_FORK:		/* fork after verification */
9099284Seric 			SendMode = *val;
9108269Seric 			break;
9118269Seric 
9128269Seric 		  default:
9139284Seric 			syserr("Unknown delivery mode %c", *val);
9148269Seric 			exit(EX_USAGE);
9158269Seric 		}
9168269Seric 		break;
9178269Seric 
9189146Seric 	  case 'D':		/* rebuild alias database as needed */
9199381Seric 		AutoRebuild = atobool(val);
9209146Seric 		break;
9219146Seric 
92255372Seric 	  case 'E':		/* error message header/header file */
92355379Seric 		if (*val != '\0')
92455379Seric 			ErrMsgFile = newstr(val);
92555372Seric 		break;
92655372Seric 
9278269Seric 	  case 'e':		/* set error processing mode */
9288269Seric 		switch (*val)
9298269Seric 		{
9309381Seric 		  case EM_QUIET:	/* be silent about it */
9319381Seric 		  case EM_MAIL:		/* mail back */
9329381Seric 		  case EM_BERKNET:	/* do berknet error processing */
9339381Seric 		  case EM_WRITE:	/* write back (or mail) */
9348269Seric 			HoldErrs = TRUE;
9359381Seric 			/* fall through... */
9368269Seric 
9379381Seric 		  case EM_PRINT:	/* print errors normally (default) */
9389381Seric 			ErrorMode = *val;
9398269Seric 			break;
9408269Seric 		}
9418269Seric 		break;
9428269Seric 
9439049Seric 	  case 'F':		/* file mode */
94417975Seric 		FileMode = atooct(val) & 0777;
9459049Seric 		break;
9469049Seric 
9478269Seric 	  case 'f':		/* save Unix-style From lines on front */
9489381Seric 		SaveFrom = atobool(val);
9498269Seric 		break;
9508269Seric 
95153735Seric 	  case 'G':		/* match recipients against GECOS field */
95253735Seric 		MatchGecos = atobool(val);
95353735Seric 		break;
95453735Seric 
9558256Seric 	  case 'g':		/* default gid */
95617474Seric 		DefGid = atoi(val);
9578256Seric 		break;
9588256Seric 
9598256Seric 	  case 'H':		/* help file */
9609381Seric 		if (val[0] == '\0')
9618269Seric 			HelpFile = "sendmail.hf";
9629381Seric 		else
9639381Seric 			HelpFile = newstr(val);
9648256Seric 		break;
9658256Seric 
96651305Seric 	  case 'h':		/* maximum hop count */
96751305Seric 		MaxHopCount = atoi(val);
96851305Seric 		break;
96951305Seric 
97035651Seric 	  case 'I':		/* use internet domain name server */
97157207Seric #ifdef NAMED_BIND
97257207Seric 		UseNameServer = TRUE;
97357207Seric 		for (p = val; *p != 0; )
97457207Seric 		{
97557207Seric 			bool clearmode;
97657207Seric 			char *q;
97757207Seric 			struct resolverflags *rfp;
97857207Seric 
97957207Seric 			while (*p == ' ')
98057207Seric 				p++;
98157207Seric 			if (*p == '\0')
98257207Seric 				break;
98357207Seric 			clearmode = FALSE;
98457207Seric 			if (*p == '-')
98557207Seric 				clearmode = TRUE;
98657207Seric 			else if (*p != '+')
98757207Seric 				p--;
98857207Seric 			p++;
98957207Seric 			q = p;
99057207Seric 			while (*p != '\0' && !isspace(*p))
99157207Seric 				p++;
99257207Seric 			if (*p != '\0')
99357207Seric 				*p++ = '\0';
99457207Seric 			for (rfp = ResolverFlags; rfp->rf_name != NULL; rfp++)
99557207Seric 			{
99657207Seric 				if (strcasecmp(q, rfp->rf_name) == 0)
99757207Seric 					break;
99857207Seric 			}
99957207Seric 			if (clearmode)
100057207Seric 				_res.options &= ~rfp->rf_bits;
100157207Seric 			else
100257207Seric 				_res.options |= rfp->rf_bits;
100357207Seric 		}
100457207Seric 		if (tTd(8, 2))
100557207Seric 			printf("_res.options = %x\n", _res.options);
100657207Seric #else
100757207Seric 		usrerr("name server (I option) specified but BIND not compiled in");
100857207Seric #endif
100935651Seric 		break;
101035651Seric 
10118269Seric 	  case 'i':		/* ignore dot lines in message */
10129381Seric 		IgnrDot = atobool(val);
10138269Seric 		break;
10148269Seric 
101557136Seric 	  case 'J':		/* .forward search path */
101657136Seric 		ForwardPath = newstr(val);
101757136Seric 		break;
101857136Seric 
101954967Seric 	  case 'k':		/* connection cache size */
102054967Seric 		MaxMciCache = atoi(val);
102156215Seric 		if (MaxMciCache < 0)
102256215Seric 			MaxMciCache = 0;
102354967Seric 		break;
102454967Seric 
102554967Seric 	  case 'K':		/* connection cache timeout */
102654967Seric 		MciCacheTimeout = convtime(val);
102754967Seric 		break;
102854967Seric 
10298256Seric 	  case 'L':		/* log level */
10309381Seric 		LogLevel = atoi(val);
10318256Seric 		break;
10328256Seric 
10338269Seric 	  case 'M':		/* define macro */
10349381Seric 		define(val[0], newstr(&val[1]), CurEnv);
103516878Seric 		sticky = FALSE;
10368269Seric 		break;
10378269Seric 
10388269Seric 	  case 'm':		/* send to me too */
10399381Seric 		MeToo = atobool(val);
10408269Seric 		break;
10418269Seric 
104225820Seric 	  case 'n':		/* validate RHS in newaliases */
104325820Seric 		CheckAliases = atobool(val);
104425820Seric 		break;
104525820Seric 
10468269Seric 	  case 'o':		/* assume old style headers */
10479381Seric 		if (atobool(val))
10489341Seric 			CurEnv->e_flags |= EF_OLDSTYLE;
10499341Seric 		else
10509341Seric 			CurEnv->e_flags &= ~EF_OLDSTYLE;
10518269Seric 		break;
10528269Seric 
105324944Seric 	  case 'P':		/* postmaster copy address for returned mail */
105424944Seric 		PostMasterCopy = newstr(val);
105524944Seric 		break;
105624944Seric 
105724944Seric 	  case 'q':		/* slope of queue only function */
105824944Seric 		QueueFactor = atoi(val);
105924944Seric 		break;
106024944Seric 
10618256Seric 	  case 'Q':		/* queue directory */
10629381Seric 		if (val[0] == '\0')
10638269Seric 			QueueDir = "mqueue";
10649381Seric 		else
10659381Seric 			QueueDir = newstr(val);
10668256Seric 		break;
10678256Seric 
10688256Seric 	  case 'r':		/* read timeout */
10699381Seric 		ReadTimeout = convtime(val);
10708256Seric 		break;
10718256Seric 
10728256Seric 	  case 'S':		/* status file */
10739381Seric 		if (val[0] == '\0')
10748269Seric 			StatFile = "sendmail.st";
10759381Seric 		else
10769381Seric 			StatFile = newstr(val);
10778256Seric 		break;
10788256Seric 
10798265Seric 	  case 's':		/* be super safe, even if expensive */
10809381Seric 		SuperSafe = atobool(val);
10818256Seric 		break;
10828256Seric 
10838256Seric 	  case 'T':		/* queue timeout */
10849381Seric 		TimeOut = convtime(val);
108554967Seric 		break;
10868256Seric 
10878265Seric 	  case 't':		/* time zone name */
108852106Seric 		TimeZoneSpec = newstr(val);
10898265Seric 		break;
10908265Seric 
109150556Seric 	  case 'U':		/* location of user database */
109251360Seric 		UdbSpec = newstr(val);
109350556Seric 		break;
109450556Seric 
10958256Seric 	  case 'u':		/* set default uid */
109617474Seric 		DefUid = atoi(val);
109740973Sbostic 		setdefuser();
10988256Seric 		break;
10998256Seric 
11008269Seric 	  case 'v':		/* run in verbose mode */
11019381Seric 		Verbose = atobool(val);
11028256Seric 		break;
11038256Seric 
110451216Seric 	  case 'w':		/* we don't have wildcard MX records */
110551216Seric 		NoWildcardMX = atobool(val);
110650537Seric 		break;
110750537Seric 
110814879Seric 	  case 'x':		/* load avg at which to auto-queue msgs */
110914879Seric 		QueueLA = atoi(val);
111014879Seric 		break;
111114879Seric 
111214879Seric 	  case 'X':		/* load avg at which to auto-reject connections */
111314879Seric 		RefuseLA = atoi(val);
111414879Seric 		break;
111514879Seric 
111624981Seric 	  case 'y':		/* work recipient factor */
111724981Seric 		WkRecipFact = atoi(val);
111824981Seric 		break;
111924981Seric 
112024981Seric 	  case 'Y':		/* fork jobs during queue runs */
112124952Seric 		ForkQueueRuns = atobool(val);
112224952Seric 		break;
112324952Seric 
112424981Seric 	  case 'z':		/* work message class factor */
112524981Seric 		WkClassFact = atoi(val);
112624981Seric 		break;
112724981Seric 
112824981Seric 	  case 'Z':		/* work time factor */
112924981Seric 		WkTimeFact = atoi(val);
113024981Seric 		break;
113124981Seric 
11328256Seric 	  default:
11338256Seric 		break;
11348256Seric 	}
113516878Seric 	if (sticky)
113616878Seric 		setbitn(opt, StickyOpt);
11379188Seric 	return;
11388256Seric }
113910687Seric /*
114010687Seric **  SETCLASS -- set a word into a class
114110687Seric **
114210687Seric **	Parameters:
114310687Seric **		class -- the class to put the word in.
114410687Seric **		word -- the word to enter
114510687Seric **
114610687Seric **	Returns:
114710687Seric **		none.
114810687Seric **
114910687Seric **	Side Effects:
115010687Seric **		puts the word into the symbol table.
115110687Seric */
115210687Seric 
115310687Seric setclass(class, word)
115410687Seric 	int class;
115510687Seric 	char *word;
115610687Seric {
115710687Seric 	register STAB *s;
115810687Seric 
115910687Seric 	s = stab(word, ST_CLASS, ST_ENTER);
116010687Seric 	setbitn(class, s->s_class);
116110687Seric }
116253654Seric /*
116353654Seric **  MAKEMAPENTRY -- create a map entry
116453654Seric **
116553654Seric **	Parameters:
116653654Seric **		line -- the config file line
116753654Seric **
116853654Seric **	Returns:
116953654Seric **		TRUE if it successfully entered the map entry.
117053654Seric **		FALSE otherwise (usually syntax error).
117153654Seric **
117253654Seric **	Side Effects:
117353654Seric **		Enters the map into the dictionary.
117453654Seric */
117553654Seric 
117653654Seric void
117753654Seric makemapentry(line)
117853654Seric 	char *line;
117953654Seric {
118053654Seric 	register char *p;
118153654Seric 	char *mapname;
118253654Seric 	char *classname;
118353654Seric 	register STAB *map;
118453654Seric 	STAB *class;
118553654Seric 
118653654Seric 	for (p = line; isspace(*p); p++)
118753654Seric 		continue;
118853654Seric 	if (!isalnum(*p))
118953654Seric 	{
119053654Seric 		syserr("readcf: config K line: no map name");
119153654Seric 		return;
119253654Seric 	}
119353654Seric 
119453654Seric 	mapname = p;
119553654Seric 	while (isalnum(*++p))
119653654Seric 		continue;
119753654Seric 	if (*p != '\0')
119853654Seric 		*p++ = '\0';
119953654Seric 	while (isspace(*p))
120053654Seric 		p++;
120153654Seric 	if (!isalnum(*p))
120253654Seric 	{
120353654Seric 		syserr("readcf: config K line, map %s: no map class", mapname);
120453654Seric 		return;
120553654Seric 	}
120653654Seric 	classname = p;
120753654Seric 	while (isalnum(*++p))
120853654Seric 		continue;
120953654Seric 	if (*p != '\0')
121053654Seric 		*p++ = '\0';
121153654Seric 	while (isspace(*p))
121253654Seric 		p++;
121353654Seric 
121453654Seric 	/* look up the class */
121553654Seric 	class = stab(classname, ST_MAPCLASS, ST_FIND);
121653654Seric 	if (class == NULL)
121753654Seric 	{
121853654Seric 		syserr("readcf: map %s: class %s not available", mapname, classname);
121953654Seric 		return;
122053654Seric 	}
122153654Seric 
122253654Seric 	/* enter the map */
122353654Seric 	map = stab(mapname, ST_MAP, ST_ENTER);
122453654Seric 	map->s_map.map_class = &class->s_mapclass;
122553654Seric 
122656823Seric 	if ((*class->s_mapclass.map_init)(&map->s_map, mapname, p))
122753654Seric 		map->s_map.map_flags |= MF_VALID;
122853654Seric }
1229