xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 57076)
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*57076Seric static char sccsid[] = "@(#)readcf.c	5.51 (Berkeley) 12/11/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;
713308Seric 	char buf[MAXLINE];
723308Seric 	register char *p;
733308Seric 	extern char **prescan();
743308Seric 	extern char **copyplist();
7552647Seric 	struct stat statb;
765909Seric 	char exbuf[MAXLINE];
7716915Seric 	char pvpbuf[PSBUFSIZE];
789350Seric 	extern char *fgetfolded();
7910709Seric 	extern char *munchstring();
8053654Seric 	extern void makemapentry();
813308Seric 
8252647Seric 	FileName = cfname;
8352647Seric 	LineNumber = 0;
8452647Seric 
853308Seric 	cf = fopen(cfname, "r");
863308Seric 	if (cf == NULL)
873308Seric 	{
8852647Seric 		syserr("cannot open");
893308Seric 		exit(EX_OSFILE);
903308Seric 	}
913308Seric 
9252647Seric 	if (fstat(fileno(cf), &statb) < 0)
9352647Seric 	{
9452647Seric 		syserr("cannot fstat");
9552647Seric 		exit(EX_OSFILE);
9652647Seric 	}
9752647Seric 
9852647Seric 	if (!S_ISREG(statb.st_mode))
9952647Seric 	{
10052647Seric 		syserr("not a plain file");
10152647Seric 		exit(EX_OSFILE);
10252647Seric 	}
10352647Seric 
10452647Seric 	if (OpMode != MD_TEST && bitset(S_IWGRP|S_IWOTH, statb.st_mode))
10552647Seric 	{
10653037Seric 		if (OpMode == MD_DAEMON || OpMode == MD_FREEZE)
10753037Seric 			fprintf(stderr, "%s: WARNING: dangerous write permissions\n",
10853037Seric 				FileName);
10953037Seric #ifdef LOG
11053037Seric 		if (LogLevel > 0)
11153037Seric 			syslog(LOG_CRIT, "%s: WARNING: dangerous write permissions",
11253037Seric 				FileName);
11353037Seric #endif
11452647Seric 	}
11552647Seric 
1167854Seric 	while (fgetfolded(buf, sizeof buf, cf) != NULL)
1173308Seric 	{
11852637Seric 		if (buf[0] == '#')
11952637Seric 			continue;
12052637Seric 
12116157Seric 		/* map $ into \001 (ASCII SOH) for macro expansion */
12216157Seric 		for (p = buf; *p != '\0'; p++)
12316157Seric 		{
12452647Seric 			if (*p == '#' && p > buf && ConfigLevel >= 3)
12552647Seric 			{
12652647Seric 				/* this is an on-line comment */
12752647Seric 				register char *e;
12852647Seric 
12952647Seric 				switch (*--p)
13052647Seric 				{
13152647Seric 				  case '\001':
13252647Seric 					/* it's from $# -- let it go through */
13352647Seric 					p++;
13452647Seric 					break;
13552647Seric 
13652647Seric 				  case '\\':
13752647Seric 					/* it's backslash escaped */
13852647Seric 					(void) strcpy(p, p + 1);
13952647Seric 					break;
14052647Seric 
14152647Seric 				  default:
14252647Seric 					/* delete preceeding white space */
14352647Seric 					while (isspace(*p) && p > buf)
14452647Seric 						p--;
14556795Seric 					if ((e = strchr(++p, '\n')) != NULL)
14652647Seric 						(void) strcpy(p, e);
14752647Seric 					else
14852647Seric 						p[0] = p[1] = '\0';
14952647Seric 					break;
15052647Seric 				}
15152647Seric 				continue;
15252647Seric 			}
15352647Seric 
15416157Seric 			if (*p != '$')
15516157Seric 				continue;
15616157Seric 
15716157Seric 			if (p[1] == '$')
15816157Seric 			{
15916157Seric 				/* actual dollar sign.... */
16023111Seric 				(void) strcpy(p, p + 1);
16116157Seric 				continue;
16216157Seric 			}
16316157Seric 
16416157Seric 			/* convert to macro expansion character */
16516157Seric 			*p = '\001';
16616157Seric 		}
16716157Seric 
16816157Seric 		/* interpret this line */
1693308Seric 		switch (buf[0])
1703308Seric 		{
1713308Seric 		  case '\0':
1723308Seric 		  case '#':		/* comment */
1733308Seric 			break;
1743308Seric 
1753308Seric 		  case 'R':		/* rewriting rule */
1763308Seric 			for (p = &buf[1]; *p != '\0' && *p != '\t'; p++)
1773308Seric 				continue;
1783308Seric 
1793308Seric 			if (*p == '\0')
1805909Seric 			{
1819381Seric 				syserr("invalid rewrite line \"%s\"", buf);
1825909Seric 				break;
1835909Seric 			}
1845909Seric 
1855909Seric 			/* allocate space for the rule header */
1865909Seric 			if (rwp == NULL)
1875909Seric 			{
1885909Seric 				RewriteRules[ruleset] = rwp =
1895909Seric 					(struct rewrite *) xalloc(sizeof *rwp);
1905909Seric 			}
1913308Seric 			else
1923308Seric 			{
1935909Seric 				rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp);
1945909Seric 				rwp = rwp->r_next;
1955909Seric 			}
1965909Seric 			rwp->r_next = NULL;
1973308Seric 
1985909Seric 			/* expand and save the LHS */
1995909Seric 			*p = '\0';
20055012Seric 			expand(&buf[1], exbuf, &exbuf[sizeof exbuf], e);
20116915Seric 			rwp->r_lhs = prescan(exbuf, '\t', pvpbuf);
2025909Seric 			if (rwp->r_lhs != NULL)
2035909Seric 				rwp->r_lhs = copyplist(rwp->r_lhs, TRUE);
20456678Seric 			else
20556678Seric 				syserr("R line: null LHS");
2065909Seric 
2075909Seric 			/* expand and save the RHS */
2085909Seric 			while (*++p == '\t')
2095909Seric 				continue;
2107231Seric 			q = p;
2117231Seric 			while (*p != '\0' && *p != '\t')
2127231Seric 				p++;
2137231Seric 			*p = '\0';
21455012Seric 			expand(q, exbuf, &exbuf[sizeof exbuf], e);
21516915Seric 			rwp->r_rhs = prescan(exbuf, '\t', pvpbuf);
2165909Seric 			if (rwp->r_rhs != NULL)
2175909Seric 				rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
21856678Seric 			else
21956678Seric 				syserr("R line: null RHS");
2203308Seric 			break;
2213308Seric 
2224072Seric 		  case 'S':		/* select rewriting set */
2234072Seric 			ruleset = atoi(&buf[1]);
2248056Seric 			if (ruleset >= MAXRWSETS || ruleset < 0)
2258056Seric 			{
2269381Seric 				syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS);
2278056Seric 				ruleset = 0;
2288056Seric 			}
2294072Seric 			rwp = NULL;
2304072Seric 			break;
2314072Seric 
2323308Seric 		  case 'D':		/* macro definition */
23355012Seric 			define(buf[1], newstr(munchstring(&buf[2])), e);
2343308Seric 			break;
2353308Seric 
2363387Seric 		  case 'H':		/* required header line */
23755012Seric 			(void) chompheader(&buf[1], TRUE, e);
2383387Seric 			break;
2393387Seric 
2404061Seric 		  case 'C':		/* word class */
2414432Seric 		  case 'F':		/* word class from file */
2424432Seric 			/* read list of words from argument or file */
2434432Seric 			if (buf[0] == 'F')
2444432Seric 			{
2454432Seric 				/* read from file */
2464432Seric 				for (p = &buf[2]; *p != '\0' && !isspace(*p); p++)
2474432Seric 					continue;
2484432Seric 				if (*p == '\0')
2494432Seric 					p = "%s";
2504432Seric 				else
2514432Seric 				{
2524432Seric 					*p = '\0';
2534432Seric 					while (isspace(*++p))
2544432Seric 						continue;
2554432Seric 				}
25654973Seric 				fileclass(buf[1], &buf[2], p, safe);
2574432Seric 				break;
2584432Seric 			}
2594061Seric 
2604432Seric 			/* scan the list of words and set class for all */
2614061Seric 			for (p = &buf[2]; *p != '\0'; )
2624061Seric 			{
2634061Seric 				register char *wd;
2644061Seric 				char delim;
2654061Seric 
2664061Seric 				while (*p != '\0' && isspace(*p))
2674061Seric 					p++;
2684061Seric 				wd = p;
2694061Seric 				while (*p != '\0' && !isspace(*p))
2704061Seric 					p++;
2714061Seric 				delim = *p;
2724061Seric 				*p = '\0';
2734061Seric 				if (wd[0] != '\0')
27410687Seric 					setclass(buf[1], wd);
2754061Seric 				*p = delim;
2764061Seric 			}
2774061Seric 			break;
2784061Seric 
2794096Seric 		  case 'M':		/* define mailer */
28021066Seric 			makemailer(&buf[1]);
2814096Seric 			break;
2824096Seric 
2838252Seric 		  case 'O':		/* set option */
28454973Seric 			setoption(buf[1], &buf[2], safe, FALSE);
2858252Seric 			break;
2868252Seric 
2878252Seric 		  case 'P':		/* set precedence */
2888252Seric 			if (NumPriorities >= MAXPRIORITIES)
2898252Seric 			{
2908547Seric 				toomany('P', MAXPRIORITIES);
2918252Seric 				break;
2928252Seric 			}
2939381Seric 			for (p = &buf[1]; *p != '\0' && *p != '=' && *p != '\t'; p++)
2948252Seric 				continue;
2958252Seric 			if (*p == '\0')
2968252Seric 				goto badline;
2978252Seric 			*p = '\0';
2988252Seric 			Priorities[NumPriorities].pri_name = newstr(&buf[1]);
2998252Seric 			Priorities[NumPriorities].pri_val = atoi(++p);
3008252Seric 			NumPriorities++;
3018252Seric 			break;
3028252Seric 
3038547Seric 		  case 'T':		/* trusted user(s) */
3048547Seric 			p = &buf[1];
3058547Seric 			while (*p != '\0')
3068547Seric 			{
3078547Seric 				while (isspace(*p))
3088547Seric 					p++;
3098547Seric 				q = p;
3108547Seric 				while (*p != '\0' && !isspace(*p))
3118547Seric 					p++;
3128547Seric 				if (*p != '\0')
3138547Seric 					*p++ = '\0';
3148547Seric 				if (*q == '\0')
3158547Seric 					continue;
3168547Seric 				for (pv = TrustedUsers; *pv != NULL; pv++)
3178547Seric 					continue;
3188547Seric 				if (pv >= &TrustedUsers[MAXTRUST])
3198547Seric 				{
3208547Seric 					toomany('T', MAXTRUST);
3218547Seric 					break;
3228547Seric 				}
3238547Seric 				*pv = newstr(q);
3248547Seric 			}
3258547Seric 			break;
3268547Seric 
32752645Seric 		  case 'V':		/* configuration syntax version */
32852645Seric 			ConfigLevel = atoi(&buf[1]);
32952645Seric 			break;
33052645Seric 
33153654Seric 		  case 'K':
33253654Seric 			makemapentry(&buf[1]);
33353654Seric 			break;
33453654Seric 
3353308Seric 		  default:
3364061Seric 		  badline:
3379381Seric 			syserr("unknown control line \"%s\"", buf);
3383308Seric 		}
3393308Seric 	}
34052637Seric 	if (ferror(cf))
34152637Seric 	{
34252647Seric 		syserr("I/O read error", cfname);
34352637Seric 		exit(EX_OSFILE);
34452637Seric 	}
34552637Seric 	fclose(cf);
3469381Seric 	FileName = NULL;
34756836Seric 
348*57076Seric 	if (stab("host", ST_MAP, ST_FIND) == NULL)
349*57076Seric 	{
350*57076Seric 		/* user didn't initialize: set up host map */
351*57076Seric 		strcpy(buf, "host host");
352*57076Seric 		if (ConfigLevel >= 2)
353*57076Seric 			strcat(buf, " -a.");
354*57076Seric 		makemapentry(buf);
355*57076Seric 	}
3564096Seric }
3574096Seric /*
3588547Seric **  TOOMANY -- signal too many of some option
3598547Seric **
3608547Seric **	Parameters:
3618547Seric **		id -- the id of the error line
3628547Seric **		maxcnt -- the maximum possible values
3638547Seric **
3648547Seric **	Returns:
3658547Seric **		none.
3668547Seric **
3678547Seric **	Side Effects:
3688547Seric **		gives a syserr.
3698547Seric */
3708547Seric 
3718547Seric toomany(id, maxcnt)
3728547Seric 	char id;
3738547Seric 	int maxcnt;
3748547Seric {
3759381Seric 	syserr("too many %c lines, %d max", id, maxcnt);
3768547Seric }
3778547Seric /*
3784432Seric **  FILECLASS -- read members of a class from a file
3794432Seric **
3804432Seric **	Parameters:
3814432Seric **		class -- class to define.
3824432Seric **		filename -- name of file to read.
3834432Seric **		fmt -- scanf string to use for match.
3844432Seric **
3854432Seric **	Returns:
3864432Seric **		none
3874432Seric **
3884432Seric **	Side Effects:
3894432Seric **
3904432Seric **		puts all lines in filename that match a scanf into
3914432Seric **			the named class.
3924432Seric */
3934432Seric 
39454973Seric fileclass(class, filename, fmt, safe)
3954432Seric 	int class;
3964432Seric 	char *filename;
3974432Seric 	char *fmt;
39854973Seric 	bool safe;
3994432Seric {
40025808Seric 	FILE *f;
40154973Seric 	struct stat stbuf;
4024432Seric 	char buf[MAXLINE];
4034432Seric 
40454973Seric 	if (stat(filename, &stbuf) < 0)
40554973Seric 	{
40654973Seric 		syserr("fileclass: cannot stat %s", filename);
40754973Seric 		return;
40854973Seric 	}
40954973Seric 	if (!S_ISREG(stbuf.st_mode))
41054973Seric 	{
41154973Seric 		syserr("fileclass: %s not a regular file", filename);
41254973Seric 		return;
41354973Seric 	}
41454973Seric 	if (!safe && access(filename, R_OK) < 0)
41554973Seric 	{
41654973Seric 		syserr("fileclass: access denied on %s", filename);
41754973Seric 		return;
41854973Seric 	}
41954973Seric 	f = fopen(filename, "r");
4204432Seric 	if (f == NULL)
4214432Seric 	{
42254973Seric 		syserr("fileclass: cannot open %s", filename);
4234432Seric 		return;
4244432Seric 	}
4254432Seric 
4264432Seric 	while (fgets(buf, sizeof buf, f) != NULL)
4274432Seric 	{
4284432Seric 		register STAB *s;
42925808Seric 		register char *p;
43025808Seric # ifdef SCANF
4314432Seric 		char wordbuf[MAXNAME+1];
4324432Seric 
4334432Seric 		if (sscanf(buf, fmt, wordbuf) != 1)
4344432Seric 			continue;
43525808Seric 		p = wordbuf;
43656795Seric # else /* SCANF */
43725808Seric 		p = buf;
43856795Seric # endif /* SCANF */
43925808Seric 
44025808Seric 		/*
44125808Seric 		**  Break up the match into words.
44225808Seric 		*/
44325808Seric 
44425808Seric 		while (*p != '\0')
44525808Seric 		{
44625808Seric 			register char *q;
44725808Seric 
44825808Seric 			/* strip leading spaces */
44925808Seric 			while (isspace(*p))
45025808Seric 				p++;
45125808Seric 			if (*p == '\0')
45225808Seric 				break;
45325808Seric 
45425808Seric 			/* find the end of the word */
45525808Seric 			q = p;
45625808Seric 			while (*p != '\0' && !isspace(*p))
45725808Seric 				p++;
45825808Seric 			if (*p != '\0')
45925808Seric 				*p++ = '\0';
46025808Seric 
46125808Seric 			/* enter the word in the symbol table */
46225808Seric 			s = stab(q, ST_CLASS, ST_ENTER);
46325808Seric 			setbitn(class, s->s_class);
46425808Seric 		}
4654432Seric 	}
4664432Seric 
46754973Seric 	(void) fclose(f);
4684432Seric }
4694432Seric /*
4704096Seric **  MAKEMAILER -- define a new mailer.
4714096Seric **
4724096Seric **	Parameters:
47310327Seric **		line -- description of mailer.  This is in labeled
47410327Seric **			fields.  The fields are:
47510327Seric **			   P -- the path to the mailer
47610327Seric **			   F -- the flags associated with the mailer
47710327Seric **			   A -- the argv for this mailer
47810327Seric **			   S -- the sender rewriting set
47910327Seric **			   R -- the recipient rewriting set
48010327Seric **			   E -- the eol string
48110327Seric **			The first word is the canonical name of the mailer.
4824096Seric **
4834096Seric **	Returns:
4844096Seric **		none.
4854096Seric **
4864096Seric **	Side Effects:
4874096Seric **		enters the mailer into the mailer table.
4884096Seric */
4893308Seric 
49021066Seric makemailer(line)
4914096Seric 	char *line;
4924096Seric {
4934096Seric 	register char *p;
4948067Seric 	register struct mailer *m;
4958067Seric 	register STAB *s;
4968067Seric 	int i;
49710327Seric 	char fcode;
4984096Seric 	extern int NextMailer;
49910327Seric 	extern char **makeargv();
50010327Seric 	extern char *munchstring();
50110327Seric 	extern char *DelimChar;
50210701Seric 	extern long atol();
5034096Seric 
50410327Seric 	/* allocate a mailer and set up defaults */
50510327Seric 	m = (struct mailer *) xalloc(sizeof *m);
50610327Seric 	bzero((char *) m, sizeof *m);
50710327Seric 	m->m_mno = NextMailer;
50810327Seric 	m->m_eol = "\n";
50910327Seric 
51010327Seric 	/* collect the mailer name */
51110327Seric 	for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++)
51210327Seric 		continue;
51310327Seric 	if (*p != '\0')
51410327Seric 		*p++ = '\0';
51510327Seric 	m->m_name = newstr(line);
51610327Seric 
51710327Seric 	/* now scan through and assign info from the fields */
51810327Seric 	while (*p != '\0')
51910327Seric 	{
52010327Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
52110327Seric 			p++;
52210327Seric 
52310327Seric 		/* p now points to field code */
52410327Seric 		fcode = *p;
52510327Seric 		while (*p != '\0' && *p != '=' && *p != ',')
52610327Seric 			p++;
52710327Seric 		if (*p++ != '=')
52810327Seric 		{
52952637Seric 			syserr("mailer %s: `=' expected", m->m_name);
53010327Seric 			return;
53110327Seric 		}
53210327Seric 		while (isspace(*p))
53310327Seric 			p++;
53410327Seric 
53510327Seric 		/* p now points to the field body */
53610327Seric 		p = munchstring(p);
53710327Seric 
53810327Seric 		/* install the field into the mailer struct */
53910327Seric 		switch (fcode)
54010327Seric 		{
54110327Seric 		  case 'P':		/* pathname */
54210327Seric 			m->m_mailer = newstr(p);
54310327Seric 			break;
54410327Seric 
54510327Seric 		  case 'F':		/* flags */
54610687Seric 			for (; *p != '\0'; p++)
54752637Seric 				if (!isspace(*p))
54852637Seric 					setbitn(*p, m->m_flags);
54910327Seric 			break;
55010327Seric 
55110327Seric 		  case 'S':		/* sender rewriting ruleset */
55210327Seric 		  case 'R':		/* recipient rewriting ruleset */
55310327Seric 			i = atoi(p);
55410327Seric 			if (i < 0 || i >= MAXRWSETS)
55510327Seric 			{
55610327Seric 				syserr("invalid rewrite set, %d max", MAXRWSETS);
55710327Seric 				return;
55810327Seric 			}
55910327Seric 			if (fcode == 'S')
56010327Seric 				m->m_s_rwset = i;
56110327Seric 			else
56210327Seric 				m->m_r_rwset = i;
56310327Seric 			break;
56410327Seric 
56510327Seric 		  case 'E':		/* end of line string */
56610327Seric 			m->m_eol = newstr(p);
56710327Seric 			break;
56810327Seric 
56910327Seric 		  case 'A':		/* argument vector */
57010327Seric 			m->m_argv = makeargv(p);
57110327Seric 			break;
57210701Seric 
57310701Seric 		  case 'M':		/* maximum message size */
57410701Seric 			m->m_maxsize = atol(p);
57510701Seric 			break;
57652106Seric 
57752106Seric 		  case 'L':		/* maximum line length */
57852106Seric 			m->m_linelimit = atoi(p);
57952106Seric 			break;
58010327Seric 		}
58110327Seric 
58210327Seric 		p = DelimChar;
58310327Seric 	}
58410327Seric 
58552106Seric 	/* do some heuristic cleanup for back compatibility */
58652106Seric 	if (bitnset(M_LIMITS, m->m_flags))
58752106Seric 	{
58852106Seric 		if (m->m_linelimit == 0)
58952106Seric 			m->m_linelimit = SMTPLINELIM;
59055418Seric 		if (ConfigLevel < 2)
59152106Seric 			setbitn(M_7BITS, m->m_flags);
59252106Seric 	}
59352106Seric 
59410327Seric 	/* now store the mailer away */
5954096Seric 	if (NextMailer >= MAXMAILERS)
5964096Seric 	{
5979381Seric 		syserr("too many mailers defined (%d max)", MAXMAILERS);
5984096Seric 		return;
5994096Seric 	}
60010327Seric 	Mailer[NextMailer++] = m;
60110327Seric 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
60210327Seric 	s->s_mailer = m;
60310327Seric }
60410327Seric /*
60510327Seric **  MUNCHSTRING -- translate a string into internal form.
60610327Seric **
60710327Seric **	Parameters:
60810327Seric **		p -- the string to munch.
60910327Seric **
61010327Seric **	Returns:
61110327Seric **		the munched string.
61210327Seric **
61310327Seric **	Side Effects:
61410327Seric **		Sets "DelimChar" to point to the string that caused us
61510327Seric **		to stop.
61610327Seric */
6174096Seric 
61810327Seric char *
61910327Seric munchstring(p)
62010327Seric 	register char *p;
62110327Seric {
62210327Seric 	register char *q;
62310327Seric 	bool backslash = FALSE;
62410327Seric 	bool quotemode = FALSE;
62510327Seric 	static char buf[MAXLINE];
62610327Seric 	extern char *DelimChar;
6274096Seric 
62810327Seric 	for (q = buf; *p != '\0'; p++)
6294096Seric 	{
63010327Seric 		if (backslash)
63110327Seric 		{
63210327Seric 			/* everything is roughly literal */
63310357Seric 			backslash = FALSE;
63410327Seric 			switch (*p)
63510327Seric 			{
63610327Seric 			  case 'r':		/* carriage return */
63710327Seric 				*q++ = '\r';
63810327Seric 				continue;
63910327Seric 
64010327Seric 			  case 'n':		/* newline */
64110327Seric 				*q++ = '\n';
64210327Seric 				continue;
64310327Seric 
64410327Seric 			  case 'f':		/* form feed */
64510327Seric 				*q++ = '\f';
64610327Seric 				continue;
64710327Seric 
64810327Seric 			  case 'b':		/* backspace */
64910327Seric 				*q++ = '\b';
65010327Seric 				continue;
65110327Seric 			}
65210327Seric 			*q++ = *p;
65310327Seric 		}
65410327Seric 		else
65510327Seric 		{
65610327Seric 			if (*p == '\\')
65710327Seric 				backslash = TRUE;
65810327Seric 			else if (*p == '"')
65910327Seric 				quotemode = !quotemode;
66010327Seric 			else if (quotemode || *p != ',')
66110327Seric 				*q++ = *p;
66210327Seric 			else
66310327Seric 				break;
66410327Seric 		}
6654096Seric 	}
6664096Seric 
66710327Seric 	DelimChar = p;
66810327Seric 	*q++ = '\0';
66910327Seric 	return (buf);
67010327Seric }
67110327Seric /*
67210327Seric **  MAKEARGV -- break up a string into words
67310327Seric **
67410327Seric **	Parameters:
67510327Seric **		p -- the string to break up.
67610327Seric **
67710327Seric **	Returns:
67810327Seric **		a char **argv (dynamically allocated)
67910327Seric **
68010327Seric **	Side Effects:
68110327Seric **		munges p.
68210327Seric */
6834096Seric 
68410327Seric char **
68510327Seric makeargv(p)
68610327Seric 	register char *p;
68710327Seric {
68810327Seric 	char *q;
68910327Seric 	int i;
69010327Seric 	char **avp;
69110327Seric 	char *argv[MAXPV + 1];
69210327Seric 
69310327Seric 	/* take apart the words */
69410327Seric 	i = 0;
69510327Seric 	while (*p != '\0' && i < MAXPV)
6964096Seric 	{
69710327Seric 		q = p;
69810327Seric 		while (*p != '\0' && !isspace(*p))
69910327Seric 			p++;
70010327Seric 		while (isspace(*p))
70110327Seric 			*p++ = '\0';
70210327Seric 		argv[i++] = newstr(q);
7034096Seric 	}
70410327Seric 	argv[i++] = NULL;
7054096Seric 
70610327Seric 	/* now make a copy of the argv */
70710327Seric 	avp = (char **) xalloc(sizeof *avp * i);
70816893Seric 	bcopy((char *) argv, (char *) avp, sizeof *avp * i);
70910327Seric 
71010327Seric 	return (avp);
7113308Seric }
7123308Seric /*
7133308Seric **  PRINTRULES -- print rewrite rules (for debugging)
7143308Seric **
7153308Seric **	Parameters:
7163308Seric **		none.
7173308Seric **
7183308Seric **	Returns:
7193308Seric **		none.
7203308Seric **
7213308Seric **	Side Effects:
7223308Seric **		prints rewrite rules.
7233308Seric */
7243308Seric 
7253308Seric printrules()
7263308Seric {
7273308Seric 	register struct rewrite *rwp;
7284072Seric 	register int ruleset;
7293308Seric 
7304072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
7313308Seric 	{
7324072Seric 		if (RewriteRules[ruleset] == NULL)
7334072Seric 			continue;
7348067Seric 		printf("\n----Rule Set %d:", ruleset);
7353308Seric 
7364072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
7373308Seric 		{
7388067Seric 			printf("\nLHS:");
7398067Seric 			printav(rwp->r_lhs);
7408067Seric 			printf("RHS:");
7418067Seric 			printav(rwp->r_rhs);
7423308Seric 		}
7433308Seric 	}
7443308Seric }
7454319Seric 
7464096Seric /*
7478256Seric **  SETOPTION -- set global processing option
7488256Seric **
7498256Seric **	Parameters:
7508256Seric **		opt -- option name.
7518256Seric **		val -- option value (as a text string).
75221755Seric **		safe -- set if this came from a configuration file.
75321755Seric **			Some options (if set from the command line) will
75421755Seric **			reset the user id to avoid security problems.
7558269Seric **		sticky -- if set, don't let other setoptions override
7568269Seric **			this value.
7578256Seric **
7588256Seric **	Returns:
7598256Seric **		none.
7608256Seric **
7618256Seric **	Side Effects:
7628256Seric **		Sets options as implied by the arguments.
7638256Seric */
7648256Seric 
76510687Seric static BITMAP	StickyOpt;		/* set if option is stuck */
7668269Seric 
76721755Seric setoption(opt, val, safe, sticky)
7688256Seric 	char opt;
7698256Seric 	char *val;
77021755Seric 	bool safe;
7718269Seric 	bool sticky;
7728256Seric {
7738265Seric 	extern bool atobool();
77412633Seric 	extern time_t convtime();
77514879Seric 	extern int QueueLA;
77614879Seric 	extern int RefuseLA;
77717474Seric 	extern bool trusteduser();
77817474Seric 	extern char *username();
7798256Seric 
7808256Seric 	if (tTd(37, 1))
7819341Seric 		printf("setoption %c=%s", opt, val);
7828256Seric 
7838256Seric 	/*
7848269Seric 	**  See if this option is preset for us.
7858256Seric 	*/
7868256Seric 
78710687Seric 	if (bitnset(opt, StickyOpt))
7888269Seric 	{
7899341Seric 		if (tTd(37, 1))
7909341Seric 			printf(" (ignored)\n");
7918269Seric 		return;
7928269Seric 	}
7938269Seric 
79421755Seric 	/*
79521755Seric 	**  Check to see if this option can be specified by this user.
79621755Seric 	*/
79721755Seric 
79836238Skarels 	if (!safe && getuid() == 0)
79921755Seric 		safe = TRUE;
80056795Seric 	if (!safe && strchr("deEiLmorsvC", opt) == NULL)
80121755Seric 	{
80239111Srick 		if (opt != 'M' || (val[0] != 'r' && val[0] != 's'))
80321755Seric 		{
80436582Sbostic 			if (tTd(37, 1))
80536582Sbostic 				printf(" (unsafe)");
80636582Sbostic 			if (getuid() != geteuid())
80736582Sbostic 			{
80851210Seric 				if (tTd(37, 1))
80951210Seric 					printf("(Resetting uid)");
81036582Sbostic 				(void) setgid(getgid());
81136582Sbostic 				(void) setuid(getuid());
81236582Sbostic 			}
81321755Seric 		}
81421755Seric 	}
81551210Seric 	if (tTd(37, 1))
81617985Seric 		printf("\n");
8178269Seric 
8188256Seric 	switch (opt)
8198256Seric 	{
82051312Seric 	  case '=':		/* config file generation level */
82151312Seric 		ConfigLevel = atoi(val);
82251312Seric 		break;
82351312Seric 
82452106Seric 	  case '8':		/* allow eight-bit input */
82552106Seric 		EightBit = atobool(val);
82652106Seric 		break;
82752106Seric 
8288256Seric 	  case 'A':		/* set default alias file */
8299381Seric 		if (val[0] == '\0')
8308269Seric 			AliasFile = "aliases";
8319381Seric 		else
8329381Seric 			AliasFile = newstr(val);
8338256Seric 		break;
8348256Seric 
83517474Seric 	  case 'a':		/* look N minutes for "@:@" in alias file */
83617474Seric 		if (val[0] == '\0')
83717474Seric 			SafeAlias = 5;
83817474Seric 		else
83917474Seric 			SafeAlias = atoi(val);
84017474Seric 		break;
84117474Seric 
84216843Seric 	  case 'B':		/* substitution for blank character */
84316843Seric 		SpaceSub = val[0];
84416843Seric 		if (SpaceSub == '\0')
84516843Seric 			SpaceSub = ' ';
84616843Seric 		break;
84716843Seric 
8489284Seric 	  case 'c':		/* don't connect to "expensive" mailers */
8499381Seric 		NoConnect = atobool(val);
8509284Seric 		break;
8519284Seric 
85251305Seric 	  case 'C':		/* checkpoint every N addresses */
85351305Seric 		CheckpointInterval = atoi(val);
85424944Seric 		break;
85524944Seric 
8569284Seric 	  case 'd':		/* delivery mode */
8579284Seric 		switch (*val)
8588269Seric 		{
8599284Seric 		  case '\0':
8609284Seric 			SendMode = SM_DELIVER;
8618269Seric 			break;
8628269Seric 
86310755Seric 		  case SM_QUEUE:	/* queue only */
86410755Seric #ifndef QUEUE
86510755Seric 			syserr("need QUEUE to set -odqueue");
86656795Seric #endif /* QUEUE */
86710755Seric 			/* fall through..... */
86810755Seric 
8699284Seric 		  case SM_DELIVER:	/* do everything */
8709284Seric 		  case SM_FORK:		/* fork after verification */
8719284Seric 			SendMode = *val;
8728269Seric 			break;
8738269Seric 
8748269Seric 		  default:
8759284Seric 			syserr("Unknown delivery mode %c", *val);
8768269Seric 			exit(EX_USAGE);
8778269Seric 		}
8788269Seric 		break;
8798269Seric 
8809146Seric 	  case 'D':		/* rebuild alias database as needed */
8819381Seric 		AutoRebuild = atobool(val);
8829146Seric 		break;
8839146Seric 
88455372Seric 	  case 'E':		/* error message header/header file */
88555379Seric 		if (*val != '\0')
88655379Seric 			ErrMsgFile = newstr(val);
88755372Seric 		break;
88855372Seric 
8898269Seric 	  case 'e':		/* set error processing mode */
8908269Seric 		switch (*val)
8918269Seric 		{
8929381Seric 		  case EM_QUIET:	/* be silent about it */
8939381Seric 		  case EM_MAIL:		/* mail back */
8949381Seric 		  case EM_BERKNET:	/* do berknet error processing */
8959381Seric 		  case EM_WRITE:	/* write back (or mail) */
8968269Seric 			HoldErrs = TRUE;
8979381Seric 			/* fall through... */
8988269Seric 
8999381Seric 		  case EM_PRINT:	/* print errors normally (default) */
9009381Seric 			ErrorMode = *val;
9018269Seric 			break;
9028269Seric 		}
9038269Seric 		break;
9048269Seric 
9059049Seric 	  case 'F':		/* file mode */
90617975Seric 		FileMode = atooct(val) & 0777;
9079049Seric 		break;
9089049Seric 
9098269Seric 	  case 'f':		/* save Unix-style From lines on front */
9109381Seric 		SaveFrom = atobool(val);
9118269Seric 		break;
9128269Seric 
91353735Seric 	  case 'G':		/* match recipients against GECOS field */
91453735Seric 		MatchGecos = atobool(val);
91553735Seric 		break;
91653735Seric 
9178256Seric 	  case 'g':		/* default gid */
91817474Seric 		DefGid = atoi(val);
9198256Seric 		break;
9208256Seric 
9218256Seric 	  case 'H':		/* help file */
9229381Seric 		if (val[0] == '\0')
9238269Seric 			HelpFile = "sendmail.hf";
9249381Seric 		else
9259381Seric 			HelpFile = newstr(val);
9268256Seric 		break;
9278256Seric 
92851305Seric 	  case 'h':		/* maximum hop count */
92951305Seric 		MaxHopCount = atoi(val);
93051305Seric 		break;
93151305Seric 
93235651Seric 	  case 'I':		/* use internet domain name server */
93335651Seric 		UseNameServer = atobool(val);
93435651Seric 		break;
93535651Seric 
9368269Seric 	  case 'i':		/* ignore dot lines in message */
9379381Seric 		IgnrDot = atobool(val);
9388269Seric 		break;
9398269Seric 
94054967Seric 	  case 'k':		/* connection cache size */
94154967Seric 		MaxMciCache = atoi(val);
94256215Seric 		if (MaxMciCache < 0)
94356215Seric 			MaxMciCache = 0;
94454967Seric 		break;
94554967Seric 
94654967Seric 	  case 'K':		/* connection cache timeout */
94754967Seric 		MciCacheTimeout = convtime(val);
94854967Seric 		break;
94954967Seric 
9508256Seric 	  case 'L':		/* log level */
9519381Seric 		LogLevel = atoi(val);
9528256Seric 		break;
9538256Seric 
9548269Seric 	  case 'M':		/* define macro */
9559381Seric 		define(val[0], newstr(&val[1]), CurEnv);
95616878Seric 		sticky = FALSE;
9578269Seric 		break;
9588269Seric 
9598269Seric 	  case 'm':		/* send to me too */
9609381Seric 		MeToo = atobool(val);
9618269Seric 		break;
9628269Seric 
96325820Seric 	  case 'n':		/* validate RHS in newaliases */
96425820Seric 		CheckAliases = atobool(val);
96525820Seric 		break;
96625820Seric 
9678269Seric 	  case 'o':		/* assume old style headers */
9689381Seric 		if (atobool(val))
9699341Seric 			CurEnv->e_flags |= EF_OLDSTYLE;
9709341Seric 		else
9719341Seric 			CurEnv->e_flags &= ~EF_OLDSTYLE;
9728269Seric 		break;
9738269Seric 
97424944Seric 	  case 'P':		/* postmaster copy address for returned mail */
97524944Seric 		PostMasterCopy = newstr(val);
97624944Seric 		break;
97724944Seric 
97824944Seric 	  case 'q':		/* slope of queue only function */
97924944Seric 		QueueFactor = atoi(val);
98024944Seric 		break;
98124944Seric 
9828256Seric 	  case 'Q':		/* queue directory */
9839381Seric 		if (val[0] == '\0')
9848269Seric 			QueueDir = "mqueue";
9859381Seric 		else
9869381Seric 			QueueDir = newstr(val);
9878256Seric 		break;
9888256Seric 
9898256Seric 	  case 'r':		/* read timeout */
9909381Seric 		ReadTimeout = convtime(val);
9918256Seric 		break;
9928256Seric 
9938256Seric 	  case 'S':		/* status file */
9949381Seric 		if (val[0] == '\0')
9958269Seric 			StatFile = "sendmail.st";
9969381Seric 		else
9979381Seric 			StatFile = newstr(val);
9988256Seric 		break;
9998256Seric 
10008265Seric 	  case 's':		/* be super safe, even if expensive */
10019381Seric 		SuperSafe = atobool(val);
10028256Seric 		break;
10038256Seric 
10048256Seric 	  case 'T':		/* queue timeout */
10059381Seric 		TimeOut = convtime(val);
100654967Seric 		break;
10078256Seric 
10088265Seric 	  case 't':		/* time zone name */
100952106Seric 		TimeZoneSpec = newstr(val);
10108265Seric 		break;
10118265Seric 
101250556Seric 	  case 'U':		/* location of user database */
101351360Seric 		UdbSpec = newstr(val);
101450556Seric 		break;
101550556Seric 
10168256Seric 	  case 'u':		/* set default uid */
101717474Seric 		DefUid = atoi(val);
101840973Sbostic 		setdefuser();
10198256Seric 		break;
10208256Seric 
10218269Seric 	  case 'v':		/* run in verbose mode */
10229381Seric 		Verbose = atobool(val);
10238256Seric 		break;
10248256Seric 
102551216Seric 	  case 'w':		/* we don't have wildcard MX records */
102651216Seric 		NoWildcardMX = atobool(val);
102750537Seric 		break;
102850537Seric 
102914879Seric 	  case 'x':		/* load avg at which to auto-queue msgs */
103014879Seric 		QueueLA = atoi(val);
103114879Seric 		break;
103214879Seric 
103314879Seric 	  case 'X':		/* load avg at which to auto-reject connections */
103414879Seric 		RefuseLA = atoi(val);
103514879Seric 		break;
103614879Seric 
103724981Seric 	  case 'y':		/* work recipient factor */
103824981Seric 		WkRecipFact = atoi(val);
103924981Seric 		break;
104024981Seric 
104124981Seric 	  case 'Y':		/* fork jobs during queue runs */
104224952Seric 		ForkQueueRuns = atobool(val);
104324952Seric 		break;
104424952Seric 
104524981Seric 	  case 'z':		/* work message class factor */
104624981Seric 		WkClassFact = atoi(val);
104724981Seric 		break;
104824981Seric 
104924981Seric 	  case 'Z':		/* work time factor */
105024981Seric 		WkTimeFact = atoi(val);
105124981Seric 		break;
105224981Seric 
10538256Seric 	  default:
10548256Seric 		break;
10558256Seric 	}
105616878Seric 	if (sticky)
105716878Seric 		setbitn(opt, StickyOpt);
10589188Seric 	return;
10598256Seric }
106010687Seric /*
106110687Seric **  SETCLASS -- set a word into a class
106210687Seric **
106310687Seric **	Parameters:
106410687Seric **		class -- the class to put the word in.
106510687Seric **		word -- the word to enter
106610687Seric **
106710687Seric **	Returns:
106810687Seric **		none.
106910687Seric **
107010687Seric **	Side Effects:
107110687Seric **		puts the word into the symbol table.
107210687Seric */
107310687Seric 
107410687Seric setclass(class, word)
107510687Seric 	int class;
107610687Seric 	char *word;
107710687Seric {
107810687Seric 	register STAB *s;
107910687Seric 
108010687Seric 	s = stab(word, ST_CLASS, ST_ENTER);
108110687Seric 	setbitn(class, s->s_class);
108210687Seric }
108353654Seric /*
108453654Seric **  MAKEMAPENTRY -- create a map entry
108553654Seric **
108653654Seric **	Parameters:
108753654Seric **		line -- the config file line
108853654Seric **
108953654Seric **	Returns:
109053654Seric **		TRUE if it successfully entered the map entry.
109153654Seric **		FALSE otherwise (usually syntax error).
109253654Seric **
109353654Seric **	Side Effects:
109453654Seric **		Enters the map into the dictionary.
109553654Seric */
109653654Seric 
109753654Seric void
109853654Seric makemapentry(line)
109953654Seric 	char *line;
110053654Seric {
110153654Seric 	register char *p;
110253654Seric 	char *mapname;
110353654Seric 	char *classname;
110453654Seric 	register STAB *map;
110553654Seric 	STAB *class;
110653654Seric 
110753654Seric 	for (p = line; isspace(*p); p++)
110853654Seric 		continue;
110953654Seric 	if (!isalnum(*p))
111053654Seric 	{
111153654Seric 		syserr("readcf: config K line: no map name");
111253654Seric 		return;
111353654Seric 	}
111453654Seric 
111553654Seric 	mapname = p;
111653654Seric 	while (isalnum(*++p))
111753654Seric 		continue;
111853654Seric 	if (*p != '\0')
111953654Seric 		*p++ = '\0';
112053654Seric 	while (isspace(*p))
112153654Seric 		p++;
112253654Seric 	if (!isalnum(*p))
112353654Seric 	{
112453654Seric 		syserr("readcf: config K line, map %s: no map class", mapname);
112553654Seric 		return;
112653654Seric 	}
112753654Seric 	classname = p;
112853654Seric 	while (isalnum(*++p))
112953654Seric 		continue;
113053654Seric 	if (*p != '\0')
113153654Seric 		*p++ = '\0';
113253654Seric 	while (isspace(*p))
113353654Seric 		p++;
113453654Seric 
113553654Seric 	/* look up the class */
113653654Seric 	class = stab(classname, ST_MAPCLASS, ST_FIND);
113753654Seric 	if (class == NULL)
113853654Seric 	{
113953654Seric 		syserr("readcf: map %s: class %s not available", mapname, classname);
114053654Seric 		return;
114153654Seric 	}
114253654Seric 
114353654Seric 	/* enter the map */
114453654Seric 	map = stab(mapname, ST_MAP, ST_ENTER);
114553654Seric 	map->s_map.map_class = &class->s_mapclass;
114653654Seric 
114756823Seric 	if ((*class->s_mapclass.map_init)(&map->s_map, mapname, p))
114853654Seric 		map->s_map.map_flags |= MF_VALID;
114953654Seric }
1150