xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 56678)
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*56678Seric static char sccsid[] = "@(#)readcf.c	5.47 (Berkeley) 11/04/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--;
14552647Seric 					if ((e = index(++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);
204*56678Seric 			else
205*56678Seric 				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);
218*56678Seric 			else
219*56678Seric 				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;
3474096Seric }
3484096Seric /*
3498547Seric **  TOOMANY -- signal too many of some option
3508547Seric **
3518547Seric **	Parameters:
3528547Seric **		id -- the id of the error line
3538547Seric **		maxcnt -- the maximum possible values
3548547Seric **
3558547Seric **	Returns:
3568547Seric **		none.
3578547Seric **
3588547Seric **	Side Effects:
3598547Seric **		gives a syserr.
3608547Seric */
3618547Seric 
3628547Seric toomany(id, maxcnt)
3638547Seric 	char id;
3648547Seric 	int maxcnt;
3658547Seric {
3669381Seric 	syserr("too many %c lines, %d max", id, maxcnt);
3678547Seric }
3688547Seric /*
3694432Seric **  FILECLASS -- read members of a class from a file
3704432Seric **
3714432Seric **	Parameters:
3724432Seric **		class -- class to define.
3734432Seric **		filename -- name of file to read.
3744432Seric **		fmt -- scanf string to use for match.
3754432Seric **
3764432Seric **	Returns:
3774432Seric **		none
3784432Seric **
3794432Seric **	Side Effects:
3804432Seric **
3814432Seric **		puts all lines in filename that match a scanf into
3824432Seric **			the named class.
3834432Seric */
3844432Seric 
38554973Seric fileclass(class, filename, fmt, safe)
3864432Seric 	int class;
3874432Seric 	char *filename;
3884432Seric 	char *fmt;
38954973Seric 	bool safe;
3904432Seric {
39125808Seric 	FILE *f;
39254973Seric 	struct stat stbuf;
3934432Seric 	char buf[MAXLINE];
3944432Seric 
39554973Seric 	if (stat(filename, &stbuf) < 0)
39654973Seric 	{
39754973Seric 		syserr("fileclass: cannot stat %s", filename);
39854973Seric 		return;
39954973Seric 	}
40054973Seric 	if (!S_ISREG(stbuf.st_mode))
40154973Seric 	{
40254973Seric 		syserr("fileclass: %s not a regular file", filename);
40354973Seric 		return;
40454973Seric 	}
40554973Seric 	if (!safe && access(filename, R_OK) < 0)
40654973Seric 	{
40754973Seric 		syserr("fileclass: access denied on %s", filename);
40854973Seric 		return;
40954973Seric 	}
41054973Seric 	f = fopen(filename, "r");
4114432Seric 	if (f == NULL)
4124432Seric 	{
41354973Seric 		syserr("fileclass: cannot open %s", filename);
4144432Seric 		return;
4154432Seric 	}
4164432Seric 
4174432Seric 	while (fgets(buf, sizeof buf, f) != NULL)
4184432Seric 	{
4194432Seric 		register STAB *s;
42025808Seric 		register char *p;
42125808Seric # ifdef SCANF
4224432Seric 		char wordbuf[MAXNAME+1];
4234432Seric 
4244432Seric 		if (sscanf(buf, fmt, wordbuf) != 1)
4254432Seric 			continue;
42625808Seric 		p = wordbuf;
42725808Seric # else SCANF
42825808Seric 		p = buf;
42925808Seric # endif SCANF
43025808Seric 
43125808Seric 		/*
43225808Seric 		**  Break up the match into words.
43325808Seric 		*/
43425808Seric 
43525808Seric 		while (*p != '\0')
43625808Seric 		{
43725808Seric 			register char *q;
43825808Seric 
43925808Seric 			/* strip leading spaces */
44025808Seric 			while (isspace(*p))
44125808Seric 				p++;
44225808Seric 			if (*p == '\0')
44325808Seric 				break;
44425808Seric 
44525808Seric 			/* find the end of the word */
44625808Seric 			q = p;
44725808Seric 			while (*p != '\0' && !isspace(*p))
44825808Seric 				p++;
44925808Seric 			if (*p != '\0')
45025808Seric 				*p++ = '\0';
45125808Seric 
45225808Seric 			/* enter the word in the symbol table */
45325808Seric 			s = stab(q, ST_CLASS, ST_ENTER);
45425808Seric 			setbitn(class, s->s_class);
45525808Seric 		}
4564432Seric 	}
4574432Seric 
45854973Seric 	(void) fclose(f);
4594432Seric }
4604432Seric /*
4614096Seric **  MAKEMAILER -- define a new mailer.
4624096Seric **
4634096Seric **	Parameters:
46410327Seric **		line -- description of mailer.  This is in labeled
46510327Seric **			fields.  The fields are:
46610327Seric **			   P -- the path to the mailer
46710327Seric **			   F -- the flags associated with the mailer
46810327Seric **			   A -- the argv for this mailer
46910327Seric **			   S -- the sender rewriting set
47010327Seric **			   R -- the recipient rewriting set
47110327Seric **			   E -- the eol string
47210327Seric **			The first word is the canonical name of the mailer.
4734096Seric **
4744096Seric **	Returns:
4754096Seric **		none.
4764096Seric **
4774096Seric **	Side Effects:
4784096Seric **		enters the mailer into the mailer table.
4794096Seric */
4803308Seric 
48121066Seric makemailer(line)
4824096Seric 	char *line;
4834096Seric {
4844096Seric 	register char *p;
4858067Seric 	register struct mailer *m;
4868067Seric 	register STAB *s;
4878067Seric 	int i;
48810327Seric 	char fcode;
4894096Seric 	extern int NextMailer;
49010327Seric 	extern char **makeargv();
49110327Seric 	extern char *munchstring();
49210327Seric 	extern char *DelimChar;
49310701Seric 	extern long atol();
4944096Seric 
49510327Seric 	/* allocate a mailer and set up defaults */
49610327Seric 	m = (struct mailer *) xalloc(sizeof *m);
49710327Seric 	bzero((char *) m, sizeof *m);
49810327Seric 	m->m_mno = NextMailer;
49910327Seric 	m->m_eol = "\n";
50010327Seric 
50110327Seric 	/* collect the mailer name */
50210327Seric 	for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++)
50310327Seric 		continue;
50410327Seric 	if (*p != '\0')
50510327Seric 		*p++ = '\0';
50610327Seric 	m->m_name = newstr(line);
50710327Seric 
50810327Seric 	/* now scan through and assign info from the fields */
50910327Seric 	while (*p != '\0')
51010327Seric 	{
51110327Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
51210327Seric 			p++;
51310327Seric 
51410327Seric 		/* p now points to field code */
51510327Seric 		fcode = *p;
51610327Seric 		while (*p != '\0' && *p != '=' && *p != ',')
51710327Seric 			p++;
51810327Seric 		if (*p++ != '=')
51910327Seric 		{
52052637Seric 			syserr("mailer %s: `=' expected", m->m_name);
52110327Seric 			return;
52210327Seric 		}
52310327Seric 		while (isspace(*p))
52410327Seric 			p++;
52510327Seric 
52610327Seric 		/* p now points to the field body */
52710327Seric 		p = munchstring(p);
52810327Seric 
52910327Seric 		/* install the field into the mailer struct */
53010327Seric 		switch (fcode)
53110327Seric 		{
53210327Seric 		  case 'P':		/* pathname */
53310327Seric 			m->m_mailer = newstr(p);
53410327Seric 			break;
53510327Seric 
53610327Seric 		  case 'F':		/* flags */
53710687Seric 			for (; *p != '\0'; p++)
53852637Seric 				if (!isspace(*p))
53952637Seric 					setbitn(*p, m->m_flags);
54010327Seric 			break;
54110327Seric 
54210327Seric 		  case 'S':		/* sender rewriting ruleset */
54310327Seric 		  case 'R':		/* recipient rewriting ruleset */
54410327Seric 			i = atoi(p);
54510327Seric 			if (i < 0 || i >= MAXRWSETS)
54610327Seric 			{
54710327Seric 				syserr("invalid rewrite set, %d max", MAXRWSETS);
54810327Seric 				return;
54910327Seric 			}
55010327Seric 			if (fcode == 'S')
55110327Seric 				m->m_s_rwset = i;
55210327Seric 			else
55310327Seric 				m->m_r_rwset = i;
55410327Seric 			break;
55510327Seric 
55610327Seric 		  case 'E':		/* end of line string */
55710327Seric 			m->m_eol = newstr(p);
55810327Seric 			break;
55910327Seric 
56010327Seric 		  case 'A':		/* argument vector */
56110327Seric 			m->m_argv = makeargv(p);
56210327Seric 			break;
56310701Seric 
56410701Seric 		  case 'M':		/* maximum message size */
56510701Seric 			m->m_maxsize = atol(p);
56610701Seric 			break;
56752106Seric 
56852106Seric 		  case 'L':		/* maximum line length */
56952106Seric 			m->m_linelimit = atoi(p);
57052106Seric 			break;
57110327Seric 		}
57210327Seric 
57310327Seric 		p = DelimChar;
57410327Seric 	}
57510327Seric 
57652106Seric 	/* do some heuristic cleanup for back compatibility */
57752106Seric 	if (bitnset(M_LIMITS, m->m_flags))
57852106Seric 	{
57952106Seric 		if (m->m_linelimit == 0)
58052106Seric 			m->m_linelimit = SMTPLINELIM;
58155418Seric 		if (ConfigLevel < 2)
58252106Seric 			setbitn(M_7BITS, m->m_flags);
58352106Seric 	}
58452106Seric 
58510327Seric 	/* now store the mailer away */
5864096Seric 	if (NextMailer >= MAXMAILERS)
5874096Seric 	{
5889381Seric 		syserr("too many mailers defined (%d max)", MAXMAILERS);
5894096Seric 		return;
5904096Seric 	}
59110327Seric 	Mailer[NextMailer++] = m;
59210327Seric 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
59310327Seric 	s->s_mailer = m;
59410327Seric }
59510327Seric /*
59610327Seric **  MUNCHSTRING -- translate a string into internal form.
59710327Seric **
59810327Seric **	Parameters:
59910327Seric **		p -- the string to munch.
60010327Seric **
60110327Seric **	Returns:
60210327Seric **		the munched string.
60310327Seric **
60410327Seric **	Side Effects:
60510327Seric **		Sets "DelimChar" to point to the string that caused us
60610327Seric **		to stop.
60710327Seric */
6084096Seric 
60910327Seric char *
61010327Seric munchstring(p)
61110327Seric 	register char *p;
61210327Seric {
61310327Seric 	register char *q;
61410327Seric 	bool backslash = FALSE;
61510327Seric 	bool quotemode = FALSE;
61610327Seric 	static char buf[MAXLINE];
61710327Seric 	extern char *DelimChar;
6184096Seric 
61910327Seric 	for (q = buf; *p != '\0'; p++)
6204096Seric 	{
62110327Seric 		if (backslash)
62210327Seric 		{
62310327Seric 			/* everything is roughly literal */
62410357Seric 			backslash = FALSE;
62510327Seric 			switch (*p)
62610327Seric 			{
62710327Seric 			  case 'r':		/* carriage return */
62810327Seric 				*q++ = '\r';
62910327Seric 				continue;
63010327Seric 
63110327Seric 			  case 'n':		/* newline */
63210327Seric 				*q++ = '\n';
63310327Seric 				continue;
63410327Seric 
63510327Seric 			  case 'f':		/* form feed */
63610327Seric 				*q++ = '\f';
63710327Seric 				continue;
63810327Seric 
63910327Seric 			  case 'b':		/* backspace */
64010327Seric 				*q++ = '\b';
64110327Seric 				continue;
64210327Seric 			}
64310327Seric 			*q++ = *p;
64410327Seric 		}
64510327Seric 		else
64610327Seric 		{
64710327Seric 			if (*p == '\\')
64810327Seric 				backslash = TRUE;
64910327Seric 			else if (*p == '"')
65010327Seric 				quotemode = !quotemode;
65110327Seric 			else if (quotemode || *p != ',')
65210327Seric 				*q++ = *p;
65310327Seric 			else
65410327Seric 				break;
65510327Seric 		}
6564096Seric 	}
6574096Seric 
65810327Seric 	DelimChar = p;
65910327Seric 	*q++ = '\0';
66010327Seric 	return (buf);
66110327Seric }
66210327Seric /*
66310327Seric **  MAKEARGV -- break up a string into words
66410327Seric **
66510327Seric **	Parameters:
66610327Seric **		p -- the string to break up.
66710327Seric **
66810327Seric **	Returns:
66910327Seric **		a char **argv (dynamically allocated)
67010327Seric **
67110327Seric **	Side Effects:
67210327Seric **		munges p.
67310327Seric */
6744096Seric 
67510327Seric char **
67610327Seric makeargv(p)
67710327Seric 	register char *p;
67810327Seric {
67910327Seric 	char *q;
68010327Seric 	int i;
68110327Seric 	char **avp;
68210327Seric 	char *argv[MAXPV + 1];
68310327Seric 
68410327Seric 	/* take apart the words */
68510327Seric 	i = 0;
68610327Seric 	while (*p != '\0' && i < MAXPV)
6874096Seric 	{
68810327Seric 		q = p;
68910327Seric 		while (*p != '\0' && !isspace(*p))
69010327Seric 			p++;
69110327Seric 		while (isspace(*p))
69210327Seric 			*p++ = '\0';
69310327Seric 		argv[i++] = newstr(q);
6944096Seric 	}
69510327Seric 	argv[i++] = NULL;
6964096Seric 
69710327Seric 	/* now make a copy of the argv */
69810327Seric 	avp = (char **) xalloc(sizeof *avp * i);
69916893Seric 	bcopy((char *) argv, (char *) avp, sizeof *avp * i);
70010327Seric 
70110327Seric 	return (avp);
7023308Seric }
7033308Seric /*
7043308Seric **  PRINTRULES -- print rewrite rules (for debugging)
7053308Seric **
7063308Seric **	Parameters:
7073308Seric **		none.
7083308Seric **
7093308Seric **	Returns:
7103308Seric **		none.
7113308Seric **
7123308Seric **	Side Effects:
7133308Seric **		prints rewrite rules.
7143308Seric */
7153308Seric 
7163308Seric printrules()
7173308Seric {
7183308Seric 	register struct rewrite *rwp;
7194072Seric 	register int ruleset;
7203308Seric 
7214072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
7223308Seric 	{
7234072Seric 		if (RewriteRules[ruleset] == NULL)
7244072Seric 			continue;
7258067Seric 		printf("\n----Rule Set %d:", ruleset);
7263308Seric 
7274072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
7283308Seric 		{
7298067Seric 			printf("\nLHS:");
7308067Seric 			printav(rwp->r_lhs);
7318067Seric 			printf("RHS:");
7328067Seric 			printav(rwp->r_rhs);
7333308Seric 		}
7343308Seric 	}
7353308Seric }
7364319Seric 
7374096Seric /*
7388256Seric **  SETOPTION -- set global processing option
7398256Seric **
7408256Seric **	Parameters:
7418256Seric **		opt -- option name.
7428256Seric **		val -- option value (as a text string).
74321755Seric **		safe -- set if this came from a configuration file.
74421755Seric **			Some options (if set from the command line) will
74521755Seric **			reset the user id to avoid security problems.
7468269Seric **		sticky -- if set, don't let other setoptions override
7478269Seric **			this value.
7488256Seric **
7498256Seric **	Returns:
7508256Seric **		none.
7518256Seric **
7528256Seric **	Side Effects:
7538256Seric **		Sets options as implied by the arguments.
7548256Seric */
7558256Seric 
75610687Seric static BITMAP	StickyOpt;		/* set if option is stuck */
7578269Seric 
75821755Seric setoption(opt, val, safe, sticky)
7598256Seric 	char opt;
7608256Seric 	char *val;
76121755Seric 	bool safe;
7628269Seric 	bool sticky;
7638256Seric {
7648265Seric 	extern bool atobool();
76512633Seric 	extern time_t convtime();
76614879Seric 	extern int QueueLA;
76714879Seric 	extern int RefuseLA;
76817474Seric 	extern bool trusteduser();
76917474Seric 	extern char *username();
7708256Seric 
7718256Seric 	if (tTd(37, 1))
7729341Seric 		printf("setoption %c=%s", opt, val);
7738256Seric 
7748256Seric 	/*
7758269Seric 	**  See if this option is preset for us.
7768256Seric 	*/
7778256Seric 
77810687Seric 	if (bitnset(opt, StickyOpt))
7798269Seric 	{
7809341Seric 		if (tTd(37, 1))
7819341Seric 			printf(" (ignored)\n");
7828269Seric 		return;
7838269Seric 	}
7848269Seric 
78521755Seric 	/*
78621755Seric 	**  Check to see if this option can be specified by this user.
78721755Seric 	*/
78821755Seric 
78936238Skarels 	if (!safe && getuid() == 0)
79021755Seric 		safe = TRUE;
79155380Seric 	if (!safe && index("deEiLmorsvC", opt) == NULL)
79221755Seric 	{
79339111Srick 		if (opt != 'M' || (val[0] != 'r' && val[0] != 's'))
79421755Seric 		{
79536582Sbostic 			if (tTd(37, 1))
79636582Sbostic 				printf(" (unsafe)");
79736582Sbostic 			if (getuid() != geteuid())
79836582Sbostic 			{
79951210Seric 				if (tTd(37, 1))
80051210Seric 					printf("(Resetting uid)");
80136582Sbostic 				(void) setgid(getgid());
80236582Sbostic 				(void) setuid(getuid());
80336582Sbostic 			}
80421755Seric 		}
80521755Seric 	}
80651210Seric 	if (tTd(37, 1))
80717985Seric 		printf("\n");
8088269Seric 
8098256Seric 	switch (opt)
8108256Seric 	{
81151312Seric 	  case '=':		/* config file generation level */
81251312Seric 		ConfigLevel = atoi(val);
81351312Seric 		break;
81451312Seric 
81552106Seric 	  case '8':		/* allow eight-bit input */
81652106Seric 		EightBit = atobool(val);
81752106Seric 		break;
81852106Seric 
8198256Seric 	  case 'A':		/* set default alias file */
8209381Seric 		if (val[0] == '\0')
8218269Seric 			AliasFile = "aliases";
8229381Seric 		else
8239381Seric 			AliasFile = newstr(val);
8248256Seric 		break;
8258256Seric 
82617474Seric 	  case 'a':		/* look N minutes for "@:@" in alias file */
82717474Seric 		if (val[0] == '\0')
82817474Seric 			SafeAlias = 5;
82917474Seric 		else
83017474Seric 			SafeAlias = atoi(val);
83117474Seric 		break;
83217474Seric 
83316843Seric 	  case 'B':		/* substitution for blank character */
83416843Seric 		SpaceSub = val[0];
83516843Seric 		if (SpaceSub == '\0')
83616843Seric 			SpaceSub = ' ';
83716843Seric 		break;
83816843Seric 
8399284Seric 	  case 'c':		/* don't connect to "expensive" mailers */
8409381Seric 		NoConnect = atobool(val);
8419284Seric 		break;
8429284Seric 
84351305Seric 	  case 'C':		/* checkpoint every N addresses */
84451305Seric 		CheckpointInterval = atoi(val);
84524944Seric 		break;
84624944Seric 
8479284Seric 	  case 'd':		/* delivery mode */
8489284Seric 		switch (*val)
8498269Seric 		{
8509284Seric 		  case '\0':
8519284Seric 			SendMode = SM_DELIVER;
8528269Seric 			break;
8538269Seric 
85410755Seric 		  case SM_QUEUE:	/* queue only */
85510755Seric #ifndef QUEUE
85610755Seric 			syserr("need QUEUE to set -odqueue");
85710755Seric #endif QUEUE
85810755Seric 			/* fall through..... */
85910755Seric 
8609284Seric 		  case SM_DELIVER:	/* do everything */
8619284Seric 		  case SM_FORK:		/* fork after verification */
8629284Seric 			SendMode = *val;
8638269Seric 			break;
8648269Seric 
8658269Seric 		  default:
8669284Seric 			syserr("Unknown delivery mode %c", *val);
8678269Seric 			exit(EX_USAGE);
8688269Seric 		}
8698269Seric 		break;
8708269Seric 
8719146Seric 	  case 'D':		/* rebuild alias database as needed */
8729381Seric 		AutoRebuild = atobool(val);
8739146Seric 		break;
8749146Seric 
87555372Seric 	  case 'E':		/* error message header/header file */
87655379Seric 		if (*val != '\0')
87755379Seric 			ErrMsgFile = newstr(val);
87855372Seric 		break;
87955372Seric 
8808269Seric 	  case 'e':		/* set error processing mode */
8818269Seric 		switch (*val)
8828269Seric 		{
8839381Seric 		  case EM_QUIET:	/* be silent about it */
8849381Seric 		  case EM_MAIL:		/* mail back */
8859381Seric 		  case EM_BERKNET:	/* do berknet error processing */
8869381Seric 		  case EM_WRITE:	/* write back (or mail) */
8878269Seric 			HoldErrs = TRUE;
8889381Seric 			/* fall through... */
8898269Seric 
8909381Seric 		  case EM_PRINT:	/* print errors normally (default) */
8919381Seric 			ErrorMode = *val;
8928269Seric 			break;
8938269Seric 		}
8948269Seric 		break;
8958269Seric 
8969049Seric 	  case 'F':		/* file mode */
89717975Seric 		FileMode = atooct(val) & 0777;
8989049Seric 		break;
8999049Seric 
9008269Seric 	  case 'f':		/* save Unix-style From lines on front */
9019381Seric 		SaveFrom = atobool(val);
9028269Seric 		break;
9038269Seric 
90453735Seric 	  case 'G':		/* match recipients against GECOS field */
90553735Seric 		MatchGecos = atobool(val);
90653735Seric 		break;
90753735Seric 
9088256Seric 	  case 'g':		/* default gid */
90917474Seric 		DefGid = atoi(val);
9108256Seric 		break;
9118256Seric 
9128256Seric 	  case 'H':		/* help file */
9139381Seric 		if (val[0] == '\0')
9148269Seric 			HelpFile = "sendmail.hf";
9159381Seric 		else
9169381Seric 			HelpFile = newstr(val);
9178256Seric 		break;
9188256Seric 
91951305Seric 	  case 'h':		/* maximum hop count */
92051305Seric 		MaxHopCount = atoi(val);
92151305Seric 		break;
92251305Seric 
92335651Seric 	  case 'I':		/* use internet domain name server */
92435651Seric 		UseNameServer = atobool(val);
92535651Seric 		break;
92635651Seric 
9278269Seric 	  case 'i':		/* ignore dot lines in message */
9289381Seric 		IgnrDot = atobool(val);
9298269Seric 		break;
9308269Seric 
93154967Seric 	  case 'k':		/* connection cache size */
93254967Seric 		MaxMciCache = atoi(val);
93356215Seric 		if (MaxMciCache < 0)
93456215Seric 			MaxMciCache = 0;
93554967Seric 		break;
93654967Seric 
93754967Seric 	  case 'K':		/* connection cache timeout */
93854967Seric 		MciCacheTimeout = convtime(val);
93954967Seric 		break;
94054967Seric 
9418256Seric 	  case 'L':		/* log level */
9429381Seric 		LogLevel = atoi(val);
9438256Seric 		break;
9448256Seric 
9458269Seric 	  case 'M':		/* define macro */
9469381Seric 		define(val[0], newstr(&val[1]), CurEnv);
94716878Seric 		sticky = FALSE;
9488269Seric 		break;
9498269Seric 
9508269Seric 	  case 'm':		/* send to me too */
9519381Seric 		MeToo = atobool(val);
9528269Seric 		break;
9538269Seric 
95425820Seric 	  case 'n':		/* validate RHS in newaliases */
95525820Seric 		CheckAliases = atobool(val);
95625820Seric 		break;
95725820Seric 
9588269Seric 	  case 'o':		/* assume old style headers */
9599381Seric 		if (atobool(val))
9609341Seric 			CurEnv->e_flags |= EF_OLDSTYLE;
9619341Seric 		else
9629341Seric 			CurEnv->e_flags &= ~EF_OLDSTYLE;
9638269Seric 		break;
9648269Seric 
96524944Seric 	  case 'P':		/* postmaster copy address for returned mail */
96624944Seric 		PostMasterCopy = newstr(val);
96724944Seric 		break;
96824944Seric 
96924944Seric 	  case 'q':		/* slope of queue only function */
97024944Seric 		QueueFactor = atoi(val);
97124944Seric 		break;
97224944Seric 
9738256Seric 	  case 'Q':		/* queue directory */
9749381Seric 		if (val[0] == '\0')
9758269Seric 			QueueDir = "mqueue";
9769381Seric 		else
9779381Seric 			QueueDir = newstr(val);
9788256Seric 		break;
9798256Seric 
9808256Seric 	  case 'r':		/* read timeout */
9819381Seric 		ReadTimeout = convtime(val);
9828256Seric 		break;
9838256Seric 
9848256Seric 	  case 'S':		/* status file */
9859381Seric 		if (val[0] == '\0')
9868269Seric 			StatFile = "sendmail.st";
9879381Seric 		else
9889381Seric 			StatFile = newstr(val);
9898256Seric 		break;
9908256Seric 
9918265Seric 	  case 's':		/* be super safe, even if expensive */
9929381Seric 		SuperSafe = atobool(val);
9938256Seric 		break;
9948256Seric 
9958256Seric 	  case 'T':		/* queue timeout */
9969381Seric 		TimeOut = convtime(val);
99754967Seric 		break;
9988256Seric 
9998265Seric 	  case 't':		/* time zone name */
100052106Seric 		TimeZoneSpec = newstr(val);
10018265Seric 		break;
10028265Seric 
100350556Seric 	  case 'U':		/* location of user database */
100451360Seric 		UdbSpec = newstr(val);
100550556Seric 		break;
100650556Seric 
10078256Seric 	  case 'u':		/* set default uid */
100817474Seric 		DefUid = atoi(val);
100940973Sbostic 		setdefuser();
10108256Seric 		break;
10118256Seric 
10128269Seric 	  case 'v':		/* run in verbose mode */
10139381Seric 		Verbose = atobool(val);
10148256Seric 		break;
10158256Seric 
101651216Seric 	  case 'w':		/* we don't have wildcard MX records */
101751216Seric 		NoWildcardMX = atobool(val);
101850537Seric 		break;
101950537Seric 
102014879Seric 	  case 'x':		/* load avg at which to auto-queue msgs */
102114879Seric 		QueueLA = atoi(val);
102214879Seric 		break;
102314879Seric 
102414879Seric 	  case 'X':		/* load avg at which to auto-reject connections */
102514879Seric 		RefuseLA = atoi(val);
102614879Seric 		break;
102714879Seric 
102824981Seric 	  case 'y':		/* work recipient factor */
102924981Seric 		WkRecipFact = atoi(val);
103024981Seric 		break;
103124981Seric 
103224981Seric 	  case 'Y':		/* fork jobs during queue runs */
103324952Seric 		ForkQueueRuns = atobool(val);
103424952Seric 		break;
103524952Seric 
103624981Seric 	  case 'z':		/* work message class factor */
103724981Seric 		WkClassFact = atoi(val);
103824981Seric 		break;
103924981Seric 
104024981Seric 	  case 'Z':		/* work time factor */
104124981Seric 		WkTimeFact = atoi(val);
104224981Seric 		break;
104324981Seric 
10448256Seric 	  default:
10458256Seric 		break;
10468256Seric 	}
104716878Seric 	if (sticky)
104816878Seric 		setbitn(opt, StickyOpt);
10499188Seric 	return;
10508256Seric }
105110687Seric /*
105210687Seric **  SETCLASS -- set a word into a class
105310687Seric **
105410687Seric **	Parameters:
105510687Seric **		class -- the class to put the word in.
105610687Seric **		word -- the word to enter
105710687Seric **
105810687Seric **	Returns:
105910687Seric **		none.
106010687Seric **
106110687Seric **	Side Effects:
106210687Seric **		puts the word into the symbol table.
106310687Seric */
106410687Seric 
106510687Seric setclass(class, word)
106610687Seric 	int class;
106710687Seric 	char *word;
106810687Seric {
106910687Seric 	register STAB *s;
107010687Seric 
107110687Seric 	s = stab(word, ST_CLASS, ST_ENTER);
107210687Seric 	setbitn(class, s->s_class);
107310687Seric }
107453654Seric /*
107553654Seric **  MAKEMAPENTRY -- create a map entry
107653654Seric **
107753654Seric **	Parameters:
107853654Seric **		line -- the config file line
107953654Seric **
108053654Seric **	Returns:
108153654Seric **		TRUE if it successfully entered the map entry.
108253654Seric **		FALSE otherwise (usually syntax error).
108353654Seric **
108453654Seric **	Side Effects:
108553654Seric **		Enters the map into the dictionary.
108653654Seric */
108753654Seric 
108853654Seric void
108953654Seric makemapentry(line)
109053654Seric 	char *line;
109153654Seric {
109253654Seric 	register char *p;
109353654Seric 	char *mapname;
109453654Seric 	char *classname;
109553654Seric 	register STAB *map;
109653654Seric 	STAB *class;
109753654Seric 
109853654Seric 	for (p = line; isspace(*p); p++)
109953654Seric 		continue;
110053654Seric 	if (!isalnum(*p))
110153654Seric 	{
110253654Seric 		syserr("readcf: config K line: no map name");
110353654Seric 		return;
110453654Seric 	}
110553654Seric 
110653654Seric 	mapname = p;
110753654Seric 	while (isalnum(*++p))
110853654Seric 		continue;
110953654Seric 	if (*p != '\0')
111053654Seric 		*p++ = '\0';
111153654Seric 	while (isspace(*p))
111253654Seric 		p++;
111353654Seric 	if (!isalnum(*p))
111453654Seric 	{
111553654Seric 		syserr("readcf: config K line, map %s: no map class", mapname);
111653654Seric 		return;
111753654Seric 	}
111853654Seric 	classname = p;
111953654Seric 	while (isalnum(*++p))
112053654Seric 		continue;
112153654Seric 	if (*p != '\0')
112253654Seric 		*p++ = '\0';
112353654Seric 	while (isspace(*p))
112453654Seric 		p++;
112553654Seric 
112653654Seric 	/* look up the class */
112753654Seric 	class = stab(classname, ST_MAPCLASS, ST_FIND);
112853654Seric 	if (class == NULL)
112953654Seric 	{
113053654Seric 		syserr("readcf: map %s: class %s not available", mapname, classname);
113153654Seric 		return;
113253654Seric 	}
113353654Seric 
113453654Seric 	/* enter the map */
113553654Seric 	map = stab(mapname, ST_MAP, ST_ENTER);
113653654Seric 	map->s_map.map_class = &class->s_mapclass;
113753654Seric 
113853654Seric 	if ((*class->s_mapclass.map_init)(&map->s_map, p))
113953654Seric 		map->s_map.map_flags |= MF_VALID;
114053654Seric }
1141