xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 55372)
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*55372Seric static char sccsid[] = "@(#)readcf.c	5.42 (Berkeley) 07/19/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);
2045909Seric 
2055909Seric 			/* expand and save the RHS */
2065909Seric 			while (*++p == '\t')
2075909Seric 				continue;
2087231Seric 			q = p;
2097231Seric 			while (*p != '\0' && *p != '\t')
2107231Seric 				p++;
2117231Seric 			*p = '\0';
21255012Seric 			expand(q, exbuf, &exbuf[sizeof exbuf], e);
21316915Seric 			rwp->r_rhs = prescan(exbuf, '\t', pvpbuf);
2145909Seric 			if (rwp->r_rhs != NULL)
2155909Seric 				rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
2163308Seric 			break;
2173308Seric 
2184072Seric 		  case 'S':		/* select rewriting set */
2194072Seric 			ruleset = atoi(&buf[1]);
2208056Seric 			if (ruleset >= MAXRWSETS || ruleset < 0)
2218056Seric 			{
2229381Seric 				syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS);
2238056Seric 				ruleset = 0;
2248056Seric 			}
2254072Seric 			rwp = NULL;
2264072Seric 			break;
2274072Seric 
2283308Seric 		  case 'D':		/* macro definition */
22955012Seric 			define(buf[1], newstr(munchstring(&buf[2])), e);
2303308Seric 			break;
2313308Seric 
2323387Seric 		  case 'H':		/* required header line */
23355012Seric 			(void) chompheader(&buf[1], TRUE, e);
2343387Seric 			break;
2353387Seric 
2364061Seric 		  case 'C':		/* word class */
2374432Seric 		  case 'F':		/* word class from file */
2384432Seric 			/* read list of words from argument or file */
2394432Seric 			if (buf[0] == 'F')
2404432Seric 			{
2414432Seric 				/* read from file */
2424432Seric 				for (p = &buf[2]; *p != '\0' && !isspace(*p); p++)
2434432Seric 					continue;
2444432Seric 				if (*p == '\0')
2454432Seric 					p = "%s";
2464432Seric 				else
2474432Seric 				{
2484432Seric 					*p = '\0';
2494432Seric 					while (isspace(*++p))
2504432Seric 						continue;
2514432Seric 				}
25254973Seric 				fileclass(buf[1], &buf[2], p, safe);
2534432Seric 				break;
2544432Seric 			}
2554061Seric 
2564432Seric 			/* scan the list of words and set class for all */
2574061Seric 			for (p = &buf[2]; *p != '\0'; )
2584061Seric 			{
2594061Seric 				register char *wd;
2604061Seric 				char delim;
2614061Seric 
2624061Seric 				while (*p != '\0' && isspace(*p))
2634061Seric 					p++;
2644061Seric 				wd = p;
2654061Seric 				while (*p != '\0' && !isspace(*p))
2664061Seric 					p++;
2674061Seric 				delim = *p;
2684061Seric 				*p = '\0';
2694061Seric 				if (wd[0] != '\0')
27010687Seric 					setclass(buf[1], wd);
2714061Seric 				*p = delim;
2724061Seric 			}
2734061Seric 			break;
2744061Seric 
2754096Seric 		  case 'M':		/* define mailer */
27621066Seric 			makemailer(&buf[1]);
2774096Seric 			break;
2784096Seric 
2798252Seric 		  case 'O':		/* set option */
28054973Seric 			setoption(buf[1], &buf[2], safe, FALSE);
2818252Seric 			break;
2828252Seric 
2838252Seric 		  case 'P':		/* set precedence */
2848252Seric 			if (NumPriorities >= MAXPRIORITIES)
2858252Seric 			{
2868547Seric 				toomany('P', MAXPRIORITIES);
2878252Seric 				break;
2888252Seric 			}
2899381Seric 			for (p = &buf[1]; *p != '\0' && *p != '=' && *p != '\t'; p++)
2908252Seric 				continue;
2918252Seric 			if (*p == '\0')
2928252Seric 				goto badline;
2938252Seric 			*p = '\0';
2948252Seric 			Priorities[NumPriorities].pri_name = newstr(&buf[1]);
2958252Seric 			Priorities[NumPriorities].pri_val = atoi(++p);
2968252Seric 			NumPriorities++;
2978252Seric 			break;
2988252Seric 
2998547Seric 		  case 'T':		/* trusted user(s) */
3008547Seric 			p = &buf[1];
3018547Seric 			while (*p != '\0')
3028547Seric 			{
3038547Seric 				while (isspace(*p))
3048547Seric 					p++;
3058547Seric 				q = p;
3068547Seric 				while (*p != '\0' && !isspace(*p))
3078547Seric 					p++;
3088547Seric 				if (*p != '\0')
3098547Seric 					*p++ = '\0';
3108547Seric 				if (*q == '\0')
3118547Seric 					continue;
3128547Seric 				for (pv = TrustedUsers; *pv != NULL; pv++)
3138547Seric 					continue;
3148547Seric 				if (pv >= &TrustedUsers[MAXTRUST])
3158547Seric 				{
3168547Seric 					toomany('T', MAXTRUST);
3178547Seric 					break;
3188547Seric 				}
3198547Seric 				*pv = newstr(q);
3208547Seric 			}
3218547Seric 			break;
3228547Seric 
32352645Seric 		  case 'V':		/* configuration syntax version */
32452645Seric 			ConfigLevel = atoi(&buf[1]);
32552645Seric 			break;
32652645Seric 
32753654Seric 		  case 'K':
32853654Seric 			makemapentry(&buf[1]);
32953654Seric 			break;
33053654Seric 
3313308Seric 		  default:
3324061Seric 		  badline:
3339381Seric 			syserr("unknown control line \"%s\"", buf);
3343308Seric 		}
3353308Seric 	}
33652637Seric 	if (ferror(cf))
33752637Seric 	{
33852647Seric 		syserr("I/O read error", cfname);
33952637Seric 		exit(EX_OSFILE);
34052637Seric 	}
34152637Seric 	fclose(cf);
3429381Seric 	FileName = NULL;
3434096Seric }
3444096Seric /*
3458547Seric **  TOOMANY -- signal too many of some option
3468547Seric **
3478547Seric **	Parameters:
3488547Seric **		id -- the id of the error line
3498547Seric **		maxcnt -- the maximum possible values
3508547Seric **
3518547Seric **	Returns:
3528547Seric **		none.
3538547Seric **
3548547Seric **	Side Effects:
3558547Seric **		gives a syserr.
3568547Seric */
3578547Seric 
3588547Seric toomany(id, maxcnt)
3598547Seric 	char id;
3608547Seric 	int maxcnt;
3618547Seric {
3629381Seric 	syserr("too many %c lines, %d max", id, maxcnt);
3638547Seric }
3648547Seric /*
3654432Seric **  FILECLASS -- read members of a class from a file
3664432Seric **
3674432Seric **	Parameters:
3684432Seric **		class -- class to define.
3694432Seric **		filename -- name of file to read.
3704432Seric **		fmt -- scanf string to use for match.
3714432Seric **
3724432Seric **	Returns:
3734432Seric **		none
3744432Seric **
3754432Seric **	Side Effects:
3764432Seric **
3774432Seric **		puts all lines in filename that match a scanf into
3784432Seric **			the named class.
3794432Seric */
3804432Seric 
38154973Seric fileclass(class, filename, fmt, safe)
3824432Seric 	int class;
3834432Seric 	char *filename;
3844432Seric 	char *fmt;
38554973Seric 	bool safe;
3864432Seric {
38725808Seric 	FILE *f;
38854973Seric 	struct stat stbuf;
3894432Seric 	char buf[MAXLINE];
3904432Seric 
39154973Seric 	if (stat(filename, &stbuf) < 0)
39254973Seric 	{
39354973Seric 		syserr("fileclass: cannot stat %s", filename);
39454973Seric 		return;
39554973Seric 	}
39654973Seric 	if (!S_ISREG(stbuf.st_mode))
39754973Seric 	{
39854973Seric 		syserr("fileclass: %s not a regular file", filename);
39954973Seric 		return;
40054973Seric 	}
40154973Seric 	if (!safe && access(filename, R_OK) < 0)
40254973Seric 	{
40354973Seric 		syserr("fileclass: access denied on %s", filename);
40454973Seric 		return;
40554973Seric 	}
40654973Seric 	f = fopen(filename, "r");
4074432Seric 	if (f == NULL)
4084432Seric 	{
40954973Seric 		syserr("fileclass: cannot open %s", filename);
4104432Seric 		return;
4114432Seric 	}
4124432Seric 
4134432Seric 	while (fgets(buf, sizeof buf, f) != NULL)
4144432Seric 	{
4154432Seric 		register STAB *s;
41625808Seric 		register char *p;
41725808Seric # ifdef SCANF
4184432Seric 		char wordbuf[MAXNAME+1];
4194432Seric 
4204432Seric 		if (sscanf(buf, fmt, wordbuf) != 1)
4214432Seric 			continue;
42225808Seric 		p = wordbuf;
42325808Seric # else SCANF
42425808Seric 		p = buf;
42525808Seric # endif SCANF
42625808Seric 
42725808Seric 		/*
42825808Seric 		**  Break up the match into words.
42925808Seric 		*/
43025808Seric 
43125808Seric 		while (*p != '\0')
43225808Seric 		{
43325808Seric 			register char *q;
43425808Seric 
43525808Seric 			/* strip leading spaces */
43625808Seric 			while (isspace(*p))
43725808Seric 				p++;
43825808Seric 			if (*p == '\0')
43925808Seric 				break;
44025808Seric 
44125808Seric 			/* find the end of the word */
44225808Seric 			q = p;
44325808Seric 			while (*p != '\0' && !isspace(*p))
44425808Seric 				p++;
44525808Seric 			if (*p != '\0')
44625808Seric 				*p++ = '\0';
44725808Seric 
44825808Seric 			/* enter the word in the symbol table */
44925808Seric 			s = stab(q, ST_CLASS, ST_ENTER);
45025808Seric 			setbitn(class, s->s_class);
45125808Seric 		}
4524432Seric 	}
4534432Seric 
45454973Seric 	(void) fclose(f);
4554432Seric }
4564432Seric /*
4574096Seric **  MAKEMAILER -- define a new mailer.
4584096Seric **
4594096Seric **	Parameters:
46010327Seric **		line -- description of mailer.  This is in labeled
46110327Seric **			fields.  The fields are:
46210327Seric **			   P -- the path to the mailer
46310327Seric **			   F -- the flags associated with the mailer
46410327Seric **			   A -- the argv for this mailer
46510327Seric **			   S -- the sender rewriting set
46610327Seric **			   R -- the recipient rewriting set
46710327Seric **			   E -- the eol string
46810327Seric **			The first word is the canonical name of the mailer.
4694096Seric **
4704096Seric **	Returns:
4714096Seric **		none.
4724096Seric **
4734096Seric **	Side Effects:
4744096Seric **		enters the mailer into the mailer table.
4754096Seric */
4763308Seric 
47721066Seric makemailer(line)
4784096Seric 	char *line;
4794096Seric {
4804096Seric 	register char *p;
4818067Seric 	register struct mailer *m;
4828067Seric 	register STAB *s;
4838067Seric 	int i;
48410327Seric 	char fcode;
4854096Seric 	extern int NextMailer;
48610327Seric 	extern char **makeargv();
48710327Seric 	extern char *munchstring();
48810327Seric 	extern char *DelimChar;
48910701Seric 	extern long atol();
4904096Seric 
49110327Seric 	/* allocate a mailer and set up defaults */
49210327Seric 	m = (struct mailer *) xalloc(sizeof *m);
49310327Seric 	bzero((char *) m, sizeof *m);
49410327Seric 	m->m_mno = NextMailer;
49510327Seric 	m->m_eol = "\n";
49610327Seric 
49710327Seric 	/* collect the mailer name */
49810327Seric 	for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++)
49910327Seric 		continue;
50010327Seric 	if (*p != '\0')
50110327Seric 		*p++ = '\0';
50210327Seric 	m->m_name = newstr(line);
50310327Seric 
50410327Seric 	/* now scan through and assign info from the fields */
50510327Seric 	while (*p != '\0')
50610327Seric 	{
50710327Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
50810327Seric 			p++;
50910327Seric 
51010327Seric 		/* p now points to field code */
51110327Seric 		fcode = *p;
51210327Seric 		while (*p != '\0' && *p != '=' && *p != ',')
51310327Seric 			p++;
51410327Seric 		if (*p++ != '=')
51510327Seric 		{
51652637Seric 			syserr("mailer %s: `=' expected", m->m_name);
51710327Seric 			return;
51810327Seric 		}
51910327Seric 		while (isspace(*p))
52010327Seric 			p++;
52110327Seric 
52210327Seric 		/* p now points to the field body */
52310327Seric 		p = munchstring(p);
52410327Seric 
52510327Seric 		/* install the field into the mailer struct */
52610327Seric 		switch (fcode)
52710327Seric 		{
52810327Seric 		  case 'P':		/* pathname */
52910327Seric 			m->m_mailer = newstr(p);
53010327Seric 			break;
53110327Seric 
53210327Seric 		  case 'F':		/* flags */
53310687Seric 			for (; *p != '\0'; p++)
53452637Seric 				if (!isspace(*p))
53552637Seric 					setbitn(*p, m->m_flags);
53610327Seric 			break;
53710327Seric 
53810327Seric 		  case 'S':		/* sender rewriting ruleset */
53910327Seric 		  case 'R':		/* recipient rewriting ruleset */
54010327Seric 			i = atoi(p);
54110327Seric 			if (i < 0 || i >= MAXRWSETS)
54210327Seric 			{
54310327Seric 				syserr("invalid rewrite set, %d max", MAXRWSETS);
54410327Seric 				return;
54510327Seric 			}
54610327Seric 			if (fcode == 'S')
54710327Seric 				m->m_s_rwset = i;
54810327Seric 			else
54910327Seric 				m->m_r_rwset = i;
55010327Seric 			break;
55110327Seric 
55210327Seric 		  case 'E':		/* end of line string */
55310327Seric 			m->m_eol = newstr(p);
55410327Seric 			break;
55510327Seric 
55610327Seric 		  case 'A':		/* argument vector */
55710327Seric 			m->m_argv = makeargv(p);
55810327Seric 			break;
55910701Seric 
56010701Seric 		  case 'M':		/* maximum message size */
56110701Seric 			m->m_maxsize = atol(p);
56210701Seric 			break;
56352106Seric 
56452106Seric 		  case 'L':		/* maximum line length */
56552106Seric 			m->m_linelimit = atoi(p);
56652106Seric 			break;
56710327Seric 		}
56810327Seric 
56910327Seric 		p = DelimChar;
57010327Seric 	}
57110327Seric 
57252106Seric 	/* do some heuristic cleanup for back compatibility */
57352106Seric 	if (bitnset(M_LIMITS, m->m_flags))
57452106Seric 	{
57552106Seric 		if (m->m_linelimit == 0)
57652106Seric 			m->m_linelimit = SMTPLINELIM;
57752106Seric 		if (!bitnset(M_8BITS, m->m_flags))
57852106Seric 			setbitn(M_7BITS, m->m_flags);
57952106Seric 	}
58052106Seric 
58110327Seric 	/* now store the mailer away */
5824096Seric 	if (NextMailer >= MAXMAILERS)
5834096Seric 	{
5849381Seric 		syserr("too many mailers defined (%d max)", MAXMAILERS);
5854096Seric 		return;
5864096Seric 	}
58710327Seric 	Mailer[NextMailer++] = m;
58810327Seric 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
58910327Seric 	s->s_mailer = m;
59010327Seric }
59110327Seric /*
59210327Seric **  MUNCHSTRING -- translate a string into internal form.
59310327Seric **
59410327Seric **	Parameters:
59510327Seric **		p -- the string to munch.
59610327Seric **
59710327Seric **	Returns:
59810327Seric **		the munched string.
59910327Seric **
60010327Seric **	Side Effects:
60110327Seric **		Sets "DelimChar" to point to the string that caused us
60210327Seric **		to stop.
60310327Seric */
6044096Seric 
60510327Seric char *
60610327Seric munchstring(p)
60710327Seric 	register char *p;
60810327Seric {
60910327Seric 	register char *q;
61010327Seric 	bool backslash = FALSE;
61110327Seric 	bool quotemode = FALSE;
61210327Seric 	static char buf[MAXLINE];
61310327Seric 	extern char *DelimChar;
6144096Seric 
61510327Seric 	for (q = buf; *p != '\0'; p++)
6164096Seric 	{
61710327Seric 		if (backslash)
61810327Seric 		{
61910327Seric 			/* everything is roughly literal */
62010357Seric 			backslash = FALSE;
62110327Seric 			switch (*p)
62210327Seric 			{
62310327Seric 			  case 'r':		/* carriage return */
62410327Seric 				*q++ = '\r';
62510327Seric 				continue;
62610327Seric 
62710327Seric 			  case 'n':		/* newline */
62810327Seric 				*q++ = '\n';
62910327Seric 				continue;
63010327Seric 
63110327Seric 			  case 'f':		/* form feed */
63210327Seric 				*q++ = '\f';
63310327Seric 				continue;
63410327Seric 
63510327Seric 			  case 'b':		/* backspace */
63610327Seric 				*q++ = '\b';
63710327Seric 				continue;
63810327Seric 			}
63910327Seric 			*q++ = *p;
64010327Seric 		}
64110327Seric 		else
64210327Seric 		{
64310327Seric 			if (*p == '\\')
64410327Seric 				backslash = TRUE;
64510327Seric 			else if (*p == '"')
64610327Seric 				quotemode = !quotemode;
64710327Seric 			else if (quotemode || *p != ',')
64810327Seric 				*q++ = *p;
64910327Seric 			else
65010327Seric 				break;
65110327Seric 		}
6524096Seric 	}
6534096Seric 
65410327Seric 	DelimChar = p;
65510327Seric 	*q++ = '\0';
65610327Seric 	return (buf);
65710327Seric }
65810327Seric /*
65910327Seric **  MAKEARGV -- break up a string into words
66010327Seric **
66110327Seric **	Parameters:
66210327Seric **		p -- the string to break up.
66310327Seric **
66410327Seric **	Returns:
66510327Seric **		a char **argv (dynamically allocated)
66610327Seric **
66710327Seric **	Side Effects:
66810327Seric **		munges p.
66910327Seric */
6704096Seric 
67110327Seric char **
67210327Seric makeargv(p)
67310327Seric 	register char *p;
67410327Seric {
67510327Seric 	char *q;
67610327Seric 	int i;
67710327Seric 	char **avp;
67810327Seric 	char *argv[MAXPV + 1];
67910327Seric 
68010327Seric 	/* take apart the words */
68110327Seric 	i = 0;
68210327Seric 	while (*p != '\0' && i < MAXPV)
6834096Seric 	{
68410327Seric 		q = p;
68510327Seric 		while (*p != '\0' && !isspace(*p))
68610327Seric 			p++;
68710327Seric 		while (isspace(*p))
68810327Seric 			*p++ = '\0';
68910327Seric 		argv[i++] = newstr(q);
6904096Seric 	}
69110327Seric 	argv[i++] = NULL;
6924096Seric 
69310327Seric 	/* now make a copy of the argv */
69410327Seric 	avp = (char **) xalloc(sizeof *avp * i);
69516893Seric 	bcopy((char *) argv, (char *) avp, sizeof *avp * i);
69610327Seric 
69710327Seric 	return (avp);
6983308Seric }
6993308Seric /*
7003308Seric **  PRINTRULES -- print rewrite rules (for debugging)
7013308Seric **
7023308Seric **	Parameters:
7033308Seric **		none.
7043308Seric **
7053308Seric **	Returns:
7063308Seric **		none.
7073308Seric **
7083308Seric **	Side Effects:
7093308Seric **		prints rewrite rules.
7103308Seric */
7113308Seric 
7123308Seric printrules()
7133308Seric {
7143308Seric 	register struct rewrite *rwp;
7154072Seric 	register int ruleset;
7163308Seric 
7174072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
7183308Seric 	{
7194072Seric 		if (RewriteRules[ruleset] == NULL)
7204072Seric 			continue;
7218067Seric 		printf("\n----Rule Set %d:", ruleset);
7223308Seric 
7234072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
7243308Seric 		{
7258067Seric 			printf("\nLHS:");
7268067Seric 			printav(rwp->r_lhs);
7278067Seric 			printf("RHS:");
7288067Seric 			printav(rwp->r_rhs);
7293308Seric 		}
7303308Seric 	}
7313308Seric }
7324319Seric 
7334096Seric /*
7348256Seric **  SETOPTION -- set global processing option
7358256Seric **
7368256Seric **	Parameters:
7378256Seric **		opt -- option name.
7388256Seric **		val -- option value (as a text string).
73921755Seric **		safe -- set if this came from a configuration file.
74021755Seric **			Some options (if set from the command line) will
74121755Seric **			reset the user id to avoid security problems.
7428269Seric **		sticky -- if set, don't let other setoptions override
7438269Seric **			this value.
7448256Seric **
7458256Seric **	Returns:
7468256Seric **		none.
7478256Seric **
7488256Seric **	Side Effects:
7498256Seric **		Sets options as implied by the arguments.
7508256Seric */
7518256Seric 
75210687Seric static BITMAP	StickyOpt;		/* set if option is stuck */
7538269Seric 
75421755Seric setoption(opt, val, safe, sticky)
7558256Seric 	char opt;
7568256Seric 	char *val;
75721755Seric 	bool safe;
7588269Seric 	bool sticky;
7598256Seric {
7608265Seric 	extern bool atobool();
76112633Seric 	extern time_t convtime();
76214879Seric 	extern int QueueLA;
76314879Seric 	extern int RefuseLA;
76417474Seric 	extern bool trusteduser();
76517474Seric 	extern char *username();
7668256Seric 
7678256Seric 	if (tTd(37, 1))
7689341Seric 		printf("setoption %c=%s", opt, val);
7698256Seric 
7708256Seric 	/*
7718269Seric 	**  See if this option is preset for us.
7728256Seric 	*/
7738256Seric 
77410687Seric 	if (bitnset(opt, StickyOpt))
7758269Seric 	{
7769341Seric 		if (tTd(37, 1))
7779341Seric 			printf(" (ignored)\n");
7788269Seric 		return;
7798269Seric 	}
7808269Seric 
78121755Seric 	/*
78221755Seric 	**  Check to see if this option can be specified by this user.
78321755Seric 	*/
78421755Seric 
78536238Skarels 	if (!safe && getuid() == 0)
78621755Seric 		safe = TRUE;
78753400Seric 	if (!safe && index("deiLmorsvC", opt) == NULL)
78821755Seric 	{
78939111Srick 		if (opt != 'M' || (val[0] != 'r' && val[0] != 's'))
79021755Seric 		{
79136582Sbostic 			if (tTd(37, 1))
79236582Sbostic 				printf(" (unsafe)");
79336582Sbostic 			if (getuid() != geteuid())
79436582Sbostic 			{
79551210Seric 				if (tTd(37, 1))
79651210Seric 					printf("(Resetting uid)");
79736582Sbostic 				(void) setgid(getgid());
79836582Sbostic 				(void) setuid(getuid());
79936582Sbostic 			}
80021755Seric 		}
80121755Seric 	}
80251210Seric 	if (tTd(37, 1))
80317985Seric 		printf("\n");
8048269Seric 
8058256Seric 	switch (opt)
8068256Seric 	{
80751312Seric 	  case '=':		/* config file generation level */
80851312Seric 		ConfigLevel = atoi(val);
80951312Seric 		break;
81051312Seric 
81152106Seric 	  case '8':		/* allow eight-bit input */
81252106Seric 		EightBit = atobool(val);
81352106Seric 		break;
81452106Seric 
8158256Seric 	  case 'A':		/* set default alias file */
8169381Seric 		if (val[0] == '\0')
8178269Seric 			AliasFile = "aliases";
8189381Seric 		else
8199381Seric 			AliasFile = newstr(val);
8208256Seric 		break;
8218256Seric 
82217474Seric 	  case 'a':		/* look N minutes for "@:@" in alias file */
82317474Seric 		if (val[0] == '\0')
82417474Seric 			SafeAlias = 5;
82517474Seric 		else
82617474Seric 			SafeAlias = atoi(val);
82717474Seric 		break;
82817474Seric 
82916843Seric 	  case 'B':		/* substitution for blank character */
83016843Seric 		SpaceSub = val[0];
83116843Seric 		if (SpaceSub == '\0')
83216843Seric 			SpaceSub = ' ';
83316843Seric 		break;
83416843Seric 
8359284Seric 	  case 'c':		/* don't connect to "expensive" mailers */
8369381Seric 		NoConnect = atobool(val);
8379284Seric 		break;
8389284Seric 
83951305Seric 	  case 'C':		/* checkpoint every N addresses */
84051305Seric 		CheckpointInterval = atoi(val);
84124944Seric 		break;
84224944Seric 
8439284Seric 	  case 'd':		/* delivery mode */
8449284Seric 		switch (*val)
8458269Seric 		{
8469284Seric 		  case '\0':
8479284Seric 			SendMode = SM_DELIVER;
8488269Seric 			break;
8498269Seric 
85010755Seric 		  case SM_QUEUE:	/* queue only */
85110755Seric #ifndef QUEUE
85210755Seric 			syserr("need QUEUE to set -odqueue");
85310755Seric #endif QUEUE
85410755Seric 			/* fall through..... */
85510755Seric 
8569284Seric 		  case SM_DELIVER:	/* do everything */
8579284Seric 		  case SM_FORK:		/* fork after verification */
8589284Seric 			SendMode = *val;
8598269Seric 			break;
8608269Seric 
8618269Seric 		  default:
8629284Seric 			syserr("Unknown delivery mode %c", *val);
8638269Seric 			exit(EX_USAGE);
8648269Seric 		}
8658269Seric 		break;
8668269Seric 
8679146Seric 	  case 'D':		/* rebuild alias database as needed */
8689381Seric 		AutoRebuild = atobool(val);
8699146Seric 		break;
8709146Seric 
871*55372Seric 	  case 'E':		/* error message header/header file */
872*55372Seric 		ErrMsgFile = newstr(val);
873*55372Seric 		break;
874*55372Seric 
8758269Seric 	  case 'e':		/* set error processing mode */
8768269Seric 		switch (*val)
8778269Seric 		{
8789381Seric 		  case EM_QUIET:	/* be silent about it */
8799381Seric 		  case EM_MAIL:		/* mail back */
8809381Seric 		  case EM_BERKNET:	/* do berknet error processing */
8819381Seric 		  case EM_WRITE:	/* write back (or mail) */
8828269Seric 			HoldErrs = TRUE;
8839381Seric 			/* fall through... */
8848269Seric 
8859381Seric 		  case EM_PRINT:	/* print errors normally (default) */
8869381Seric 			ErrorMode = *val;
8878269Seric 			break;
8888269Seric 		}
8898269Seric 		break;
8908269Seric 
8919049Seric 	  case 'F':		/* file mode */
89217975Seric 		FileMode = atooct(val) & 0777;
8939049Seric 		break;
8949049Seric 
8958269Seric 	  case 'f':		/* save Unix-style From lines on front */
8969381Seric 		SaveFrom = atobool(val);
8978269Seric 		break;
8988269Seric 
89953735Seric 	  case 'G':		/* match recipients against GECOS field */
90053735Seric 		MatchGecos = atobool(val);
90153735Seric 		break;
90253735Seric 
9038256Seric 	  case 'g':		/* default gid */
90417474Seric 		DefGid = atoi(val);
9058256Seric 		break;
9068256Seric 
9078256Seric 	  case 'H':		/* help file */
9089381Seric 		if (val[0] == '\0')
9098269Seric 			HelpFile = "sendmail.hf";
9109381Seric 		else
9119381Seric 			HelpFile = newstr(val);
9128256Seric 		break;
9138256Seric 
91451305Seric 	  case 'h':		/* maximum hop count */
91551305Seric 		MaxHopCount = atoi(val);
91651305Seric 		break;
91751305Seric 
91835651Seric 	  case 'I':		/* use internet domain name server */
91935651Seric 		UseNameServer = atobool(val);
92035651Seric 		break;
92135651Seric 
9228269Seric 	  case 'i':		/* ignore dot lines in message */
9239381Seric 		IgnrDot = atobool(val);
9248269Seric 		break;
9258269Seric 
92654967Seric 	  case 'k':		/* connection cache size */
92754967Seric 		MaxMciCache = atoi(val);
92854967Seric 		if (MaxMciCache <= 0)
92954967Seric 			MaxMciCache = 1;
93054967Seric 		break;
93154967Seric 
93254967Seric 	  case 'K':		/* connection cache timeout */
93354967Seric 		MciCacheTimeout = convtime(val);
93454967Seric 		break;
93554967Seric 
9368256Seric 	  case 'L':		/* log level */
9379381Seric 		LogLevel = atoi(val);
9388256Seric 		break;
9398256Seric 
9408269Seric 	  case 'M':		/* define macro */
9419381Seric 		define(val[0], newstr(&val[1]), CurEnv);
94216878Seric 		sticky = FALSE;
9438269Seric 		break;
9448269Seric 
9458269Seric 	  case 'm':		/* send to me too */
9469381Seric 		MeToo = atobool(val);
9478269Seric 		break;
9488269Seric 
94925820Seric 	  case 'n':		/* validate RHS in newaliases */
95025820Seric 		CheckAliases = atobool(val);
95125820Seric 		break;
95225820Seric 
9538269Seric 	  case 'o':		/* assume old style headers */
9549381Seric 		if (atobool(val))
9559341Seric 			CurEnv->e_flags |= EF_OLDSTYLE;
9569341Seric 		else
9579341Seric 			CurEnv->e_flags &= ~EF_OLDSTYLE;
9588269Seric 		break;
9598269Seric 
96024944Seric 	  case 'P':		/* postmaster copy address for returned mail */
96124944Seric 		PostMasterCopy = newstr(val);
96224944Seric 		break;
96324944Seric 
96424944Seric 	  case 'q':		/* slope of queue only function */
96524944Seric 		QueueFactor = atoi(val);
96624944Seric 		break;
96724944Seric 
9688256Seric 	  case 'Q':		/* queue directory */
9699381Seric 		if (val[0] == '\0')
9708269Seric 			QueueDir = "mqueue";
9719381Seric 		else
9729381Seric 			QueueDir = newstr(val);
9738256Seric 		break;
9748256Seric 
9758256Seric 	  case 'r':		/* read timeout */
9769381Seric 		ReadTimeout = convtime(val);
9778256Seric 		break;
9788256Seric 
9798256Seric 	  case 'S':		/* status file */
9809381Seric 		if (val[0] == '\0')
9818269Seric 			StatFile = "sendmail.st";
9829381Seric 		else
9839381Seric 			StatFile = newstr(val);
9848256Seric 		break;
9858256Seric 
9868265Seric 	  case 's':		/* be super safe, even if expensive */
9879381Seric 		SuperSafe = atobool(val);
9888256Seric 		break;
9898256Seric 
9908256Seric 	  case 'T':		/* queue timeout */
9919381Seric 		TimeOut = convtime(val);
99254967Seric 		break;
9938256Seric 
9948265Seric 	  case 't':		/* time zone name */
99552106Seric 		TimeZoneSpec = newstr(val);
9968265Seric 		break;
9978265Seric 
99850556Seric 	  case 'U':		/* location of user database */
99951360Seric 		UdbSpec = newstr(val);
100050556Seric 		break;
100150556Seric 
10028256Seric 	  case 'u':		/* set default uid */
100317474Seric 		DefUid = atoi(val);
100440973Sbostic 		setdefuser();
10058256Seric 		break;
10068256Seric 
10078269Seric 	  case 'v':		/* run in verbose mode */
10089381Seric 		Verbose = atobool(val);
10098256Seric 		break;
10108256Seric 
101151216Seric 	  case 'w':		/* we don't have wildcard MX records */
101251216Seric 		NoWildcardMX = atobool(val);
101350537Seric 		break;
101450537Seric 
101514879Seric 	  case 'x':		/* load avg at which to auto-queue msgs */
101614879Seric 		QueueLA = atoi(val);
101714879Seric 		break;
101814879Seric 
101914879Seric 	  case 'X':		/* load avg at which to auto-reject connections */
102014879Seric 		RefuseLA = atoi(val);
102114879Seric 		break;
102214879Seric 
102324981Seric 	  case 'y':		/* work recipient factor */
102424981Seric 		WkRecipFact = atoi(val);
102524981Seric 		break;
102624981Seric 
102724981Seric 	  case 'Y':		/* fork jobs during queue runs */
102824952Seric 		ForkQueueRuns = atobool(val);
102924952Seric 		break;
103024952Seric 
103124981Seric 	  case 'z':		/* work message class factor */
103224981Seric 		WkClassFact = atoi(val);
103324981Seric 		break;
103424981Seric 
103524981Seric 	  case 'Z':		/* work time factor */
103624981Seric 		WkTimeFact = atoi(val);
103724981Seric 		break;
103824981Seric 
10398256Seric 	  default:
10408256Seric 		break;
10418256Seric 	}
104216878Seric 	if (sticky)
104316878Seric 		setbitn(opt, StickyOpt);
10449188Seric 	return;
10458256Seric }
104610687Seric /*
104710687Seric **  SETCLASS -- set a word into a class
104810687Seric **
104910687Seric **	Parameters:
105010687Seric **		class -- the class to put the word in.
105110687Seric **		word -- the word to enter
105210687Seric **
105310687Seric **	Returns:
105410687Seric **		none.
105510687Seric **
105610687Seric **	Side Effects:
105710687Seric **		puts the word into the symbol table.
105810687Seric */
105910687Seric 
106010687Seric setclass(class, word)
106110687Seric 	int class;
106210687Seric 	char *word;
106310687Seric {
106410687Seric 	register STAB *s;
106510687Seric 
106610687Seric 	s = stab(word, ST_CLASS, ST_ENTER);
106710687Seric 	setbitn(class, s->s_class);
106810687Seric }
106953654Seric /*
107053654Seric **  MAKEMAPENTRY -- create a map entry
107153654Seric **
107253654Seric **	Parameters:
107353654Seric **		line -- the config file line
107453654Seric **
107553654Seric **	Returns:
107653654Seric **		TRUE if it successfully entered the map entry.
107753654Seric **		FALSE otherwise (usually syntax error).
107853654Seric **
107953654Seric **	Side Effects:
108053654Seric **		Enters the map into the dictionary.
108153654Seric */
108253654Seric 
108353654Seric void
108453654Seric makemapentry(line)
108553654Seric 	char *line;
108653654Seric {
108753654Seric 	register char *p;
108853654Seric 	char *mapname;
108953654Seric 	char *classname;
109053654Seric 	register STAB *map;
109153654Seric 	STAB *class;
109253654Seric 
109353654Seric 	for (p = line; isspace(*p); p++)
109453654Seric 		continue;
109553654Seric 	if (!isalnum(*p))
109653654Seric 	{
109753654Seric 		syserr("readcf: config K line: no map name");
109853654Seric 		return;
109953654Seric 	}
110053654Seric 
110153654Seric 	mapname = p;
110253654Seric 	while (isalnum(*++p))
110353654Seric 		continue;
110453654Seric 	if (*p != '\0')
110553654Seric 		*p++ = '\0';
110653654Seric 	while (isspace(*p))
110753654Seric 		p++;
110853654Seric 	if (!isalnum(*p))
110953654Seric 	{
111053654Seric 		syserr("readcf: config K line, map %s: no map class", mapname);
111153654Seric 		return;
111253654Seric 	}
111353654Seric 	classname = p;
111453654Seric 	while (isalnum(*++p))
111553654Seric 		continue;
111653654Seric 	if (*p != '\0')
111753654Seric 		*p++ = '\0';
111853654Seric 	while (isspace(*p))
111953654Seric 		p++;
112053654Seric 
112153654Seric 	/* look up the class */
112253654Seric 	class = stab(classname, ST_MAPCLASS, ST_FIND);
112353654Seric 	if (class == NULL)
112453654Seric 	{
112553654Seric 		syserr("readcf: map %s: class %s not available", mapname, classname);
112653654Seric 		return;
112753654Seric 	}
112853654Seric 
112953654Seric 	/* enter the map */
113053654Seric 	map = stab(mapname, ST_MAP, ST_ENTER);
113153654Seric 	map->s_map.map_class = &class->s_mapclass;
113253654Seric 
113353654Seric 	if ((*class->s_mapclass.map_init)(&map->s_map, p))
113453654Seric 		map->s_map.map_flags |= MF_VALID;
113553654Seric }
1136