xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 54973)
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*54973Seric static char sccsid[] = "@(#)readcf.c	5.40 (Berkeley) 07/12/92";
1133731Sbostic #endif /* not lint */
1222709Sdist 
133313Seric # include "sendmail.h"
1452647Seric # include <sys/stat.h>
15*54973Seric # 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.
50*54973Seric **		safe -- TRUE if this is the system config file;
51*54973Seric **			FALSE otherwise.
523308Seric **
533308Seric **	Returns:
543308Seric **		none.
553308Seric **
563308Seric **	Side Effects:
573308Seric **		Builds several internal tables.
583308Seric */
593308Seric 
60*54973Seric readcf(cfname, safe)
613308Seric 	char *cfname;
62*54973Seric 	bool safe;
633308Seric {
643308Seric 	FILE *cf;
658547Seric 	int ruleset = 0;
668547Seric 	char *q;
678547Seric 	char **pv;
689350Seric 	struct rewrite *rwp = NULL;
693308Seric 	char buf[MAXLINE];
703308Seric 	register char *p;
713308Seric 	extern char **prescan();
723308Seric 	extern char **copyplist();
7352647Seric 	struct stat statb;
745909Seric 	char exbuf[MAXLINE];
7516915Seric 	char pvpbuf[PSBUFSIZE];
769350Seric 	extern char *fgetfolded();
7710709Seric 	extern char *munchstring();
7853654Seric 	extern void makemapentry();
793308Seric 
8052647Seric 	FileName = cfname;
8152647Seric 	LineNumber = 0;
8252647Seric 
833308Seric 	cf = fopen(cfname, "r");
843308Seric 	if (cf == NULL)
853308Seric 	{
8652647Seric 		syserr("cannot open");
873308Seric 		exit(EX_OSFILE);
883308Seric 	}
893308Seric 
9052647Seric 	if (fstat(fileno(cf), &statb) < 0)
9152647Seric 	{
9252647Seric 		syserr("cannot fstat");
9352647Seric 		exit(EX_OSFILE);
9452647Seric 	}
9552647Seric 
9652647Seric 	if (!S_ISREG(statb.st_mode))
9752647Seric 	{
9852647Seric 		syserr("not a plain file");
9952647Seric 		exit(EX_OSFILE);
10052647Seric 	}
10152647Seric 
10252647Seric 	if (OpMode != MD_TEST && bitset(S_IWGRP|S_IWOTH, statb.st_mode))
10352647Seric 	{
10453037Seric 		if (OpMode == MD_DAEMON || OpMode == MD_FREEZE)
10553037Seric 			fprintf(stderr, "%s: WARNING: dangerous write permissions\n",
10653037Seric 				FileName);
10753037Seric #ifdef LOG
10853037Seric 		if (LogLevel > 0)
10953037Seric 			syslog(LOG_CRIT, "%s: WARNING: dangerous write permissions",
11053037Seric 				FileName);
11153037Seric #endif
11252647Seric 	}
11352647Seric 
1147854Seric 	while (fgetfolded(buf, sizeof buf, cf) != NULL)
1153308Seric 	{
11652637Seric 		if (buf[0] == '#')
11752637Seric 			continue;
11852637Seric 
11916157Seric 		/* map $ into \001 (ASCII SOH) for macro expansion */
12016157Seric 		for (p = buf; *p != '\0'; p++)
12116157Seric 		{
12252647Seric 			if (*p == '#' && p > buf && ConfigLevel >= 3)
12352647Seric 			{
12452647Seric 				/* this is an on-line comment */
12552647Seric 				register char *e;
12652647Seric 
12752647Seric 				switch (*--p)
12852647Seric 				{
12952647Seric 				  case '\001':
13052647Seric 					/* it's from $# -- let it go through */
13152647Seric 					p++;
13252647Seric 					break;
13352647Seric 
13452647Seric 				  case '\\':
13552647Seric 					/* it's backslash escaped */
13652647Seric 					(void) strcpy(p, p + 1);
13752647Seric 					break;
13852647Seric 
13952647Seric 				  default:
14052647Seric 					/* delete preceeding white space */
14152647Seric 					while (isspace(*p) && p > buf)
14252647Seric 						p--;
14352647Seric 					if ((e = index(++p, '\n')) != NULL)
14452647Seric 						(void) strcpy(p, e);
14552647Seric 					else
14652647Seric 						p[0] = p[1] = '\0';
14752647Seric 					break;
14852647Seric 				}
14952647Seric 				continue;
15052647Seric 			}
15152647Seric 
15216157Seric 			if (*p != '$')
15316157Seric 				continue;
15416157Seric 
15516157Seric 			if (p[1] == '$')
15616157Seric 			{
15716157Seric 				/* actual dollar sign.... */
15823111Seric 				(void) strcpy(p, p + 1);
15916157Seric 				continue;
16016157Seric 			}
16116157Seric 
16216157Seric 			/* convert to macro expansion character */
16316157Seric 			*p = '\001';
16416157Seric 		}
16516157Seric 
16616157Seric 		/* interpret this line */
1673308Seric 		switch (buf[0])
1683308Seric 		{
1693308Seric 		  case '\0':
1703308Seric 		  case '#':		/* comment */
1713308Seric 			break;
1723308Seric 
1733308Seric 		  case 'R':		/* rewriting rule */
1743308Seric 			for (p = &buf[1]; *p != '\0' && *p != '\t'; p++)
1753308Seric 				continue;
1763308Seric 
1773308Seric 			if (*p == '\0')
1785909Seric 			{
1799381Seric 				syserr("invalid rewrite line \"%s\"", buf);
1805909Seric 				break;
1815909Seric 			}
1825909Seric 
1835909Seric 			/* allocate space for the rule header */
1845909Seric 			if (rwp == NULL)
1855909Seric 			{
1865909Seric 				RewriteRules[ruleset] = rwp =
1875909Seric 					(struct rewrite *) xalloc(sizeof *rwp);
1885909Seric 			}
1893308Seric 			else
1903308Seric 			{
1915909Seric 				rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp);
1925909Seric 				rwp = rwp->r_next;
1935909Seric 			}
1945909Seric 			rwp->r_next = NULL;
1953308Seric 
1965909Seric 			/* expand and save the LHS */
1975909Seric 			*p = '\0';
1986991Seric 			expand(&buf[1], exbuf, &exbuf[sizeof exbuf], CurEnv);
19916915Seric 			rwp->r_lhs = prescan(exbuf, '\t', pvpbuf);
2005909Seric 			if (rwp->r_lhs != NULL)
2015909Seric 				rwp->r_lhs = copyplist(rwp->r_lhs, TRUE);
2025909Seric 
2035909Seric 			/* expand and save the RHS */
2045909Seric 			while (*++p == '\t')
2055909Seric 				continue;
2067231Seric 			q = p;
2077231Seric 			while (*p != '\0' && *p != '\t')
2087231Seric 				p++;
2097231Seric 			*p = '\0';
2107231Seric 			expand(q, exbuf, &exbuf[sizeof exbuf], CurEnv);
21116915Seric 			rwp->r_rhs = prescan(exbuf, '\t', pvpbuf);
2125909Seric 			if (rwp->r_rhs != NULL)
2135909Seric 				rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
2143308Seric 			break;
2153308Seric 
2164072Seric 		  case 'S':		/* select rewriting set */
2174072Seric 			ruleset = atoi(&buf[1]);
2188056Seric 			if (ruleset >= MAXRWSETS || ruleset < 0)
2198056Seric 			{
2209381Seric 				syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS);
2218056Seric 				ruleset = 0;
2228056Seric 			}
2234072Seric 			rwp = NULL;
2244072Seric 			break;
2254072Seric 
2263308Seric 		  case 'D':		/* macro definition */
22710709Seric 			define(buf[1], newstr(munchstring(&buf[2])), CurEnv);
2283308Seric 			break;
2293308Seric 
2303387Seric 		  case 'H':		/* required header line */
2314088Seric 			(void) chompheader(&buf[1], TRUE);
2323387Seric 			break;
2333387Seric 
2344061Seric 		  case 'C':		/* word class */
2354432Seric 		  case 'F':		/* word class from file */
2364432Seric 			/* read list of words from argument or file */
2374432Seric 			if (buf[0] == 'F')
2384432Seric 			{
2394432Seric 				/* read from file */
2404432Seric 				for (p = &buf[2]; *p != '\0' && !isspace(*p); p++)
2414432Seric 					continue;
2424432Seric 				if (*p == '\0')
2434432Seric 					p = "%s";
2444432Seric 				else
2454432Seric 				{
2464432Seric 					*p = '\0';
2474432Seric 					while (isspace(*++p))
2484432Seric 						continue;
2494432Seric 				}
250*54973Seric 				fileclass(buf[1], &buf[2], p, safe);
2514432Seric 				break;
2524432Seric 			}
2534061Seric 
2544432Seric 			/* scan the list of words and set class for all */
2554061Seric 			for (p = &buf[2]; *p != '\0'; )
2564061Seric 			{
2574061Seric 				register char *wd;
2584061Seric 				char delim;
2594061Seric 
2604061Seric 				while (*p != '\0' && isspace(*p))
2614061Seric 					p++;
2624061Seric 				wd = p;
2634061Seric 				while (*p != '\0' && !isspace(*p))
2644061Seric 					p++;
2654061Seric 				delim = *p;
2664061Seric 				*p = '\0';
2674061Seric 				if (wd[0] != '\0')
26810687Seric 					setclass(buf[1], wd);
2694061Seric 				*p = delim;
2704061Seric 			}
2714061Seric 			break;
2724061Seric 
2734096Seric 		  case 'M':		/* define mailer */
27421066Seric 			makemailer(&buf[1]);
2754096Seric 			break;
2764096Seric 
2778252Seric 		  case 'O':		/* set option */
278*54973Seric 			setoption(buf[1], &buf[2], safe, FALSE);
2798252Seric 			break;
2808252Seric 
2818252Seric 		  case 'P':		/* set precedence */
2828252Seric 			if (NumPriorities >= MAXPRIORITIES)
2838252Seric 			{
2848547Seric 				toomany('P', MAXPRIORITIES);
2858252Seric 				break;
2868252Seric 			}
2879381Seric 			for (p = &buf[1]; *p != '\0' && *p != '=' && *p != '\t'; p++)
2888252Seric 				continue;
2898252Seric 			if (*p == '\0')
2908252Seric 				goto badline;
2918252Seric 			*p = '\0';
2928252Seric 			Priorities[NumPriorities].pri_name = newstr(&buf[1]);
2938252Seric 			Priorities[NumPriorities].pri_val = atoi(++p);
2948252Seric 			NumPriorities++;
2958252Seric 			break;
2968252Seric 
2978547Seric 		  case 'T':		/* trusted user(s) */
2988547Seric 			p = &buf[1];
2998547Seric 			while (*p != '\0')
3008547Seric 			{
3018547Seric 				while (isspace(*p))
3028547Seric 					p++;
3038547Seric 				q = p;
3048547Seric 				while (*p != '\0' && !isspace(*p))
3058547Seric 					p++;
3068547Seric 				if (*p != '\0')
3078547Seric 					*p++ = '\0';
3088547Seric 				if (*q == '\0')
3098547Seric 					continue;
3108547Seric 				for (pv = TrustedUsers; *pv != NULL; pv++)
3118547Seric 					continue;
3128547Seric 				if (pv >= &TrustedUsers[MAXTRUST])
3138547Seric 				{
3148547Seric 					toomany('T', MAXTRUST);
3158547Seric 					break;
3168547Seric 				}
3178547Seric 				*pv = newstr(q);
3188547Seric 			}
3198547Seric 			break;
3208547Seric 
32152645Seric 		  case 'V':		/* configuration syntax version */
32252645Seric 			ConfigLevel = atoi(&buf[1]);
32352645Seric 			break;
32452645Seric 
32553654Seric 		  case 'K':
32653654Seric 			makemapentry(&buf[1]);
32753654Seric 			break;
32853654Seric 
3293308Seric 		  default:
3304061Seric 		  badline:
3319381Seric 			syserr("unknown control line \"%s\"", buf);
3323308Seric 		}
3333308Seric 	}
33452637Seric 	if (ferror(cf))
33552637Seric 	{
33652647Seric 		syserr("I/O read error", cfname);
33752637Seric 		exit(EX_OSFILE);
33852637Seric 	}
33952637Seric 	fclose(cf);
3409381Seric 	FileName = NULL;
3414096Seric }
3424096Seric /*
3438547Seric **  TOOMANY -- signal too many of some option
3448547Seric **
3458547Seric **	Parameters:
3468547Seric **		id -- the id of the error line
3478547Seric **		maxcnt -- the maximum possible values
3488547Seric **
3498547Seric **	Returns:
3508547Seric **		none.
3518547Seric **
3528547Seric **	Side Effects:
3538547Seric **		gives a syserr.
3548547Seric */
3558547Seric 
3568547Seric toomany(id, maxcnt)
3578547Seric 	char id;
3588547Seric 	int maxcnt;
3598547Seric {
3609381Seric 	syserr("too many %c lines, %d max", id, maxcnt);
3618547Seric }
3628547Seric /*
3634432Seric **  FILECLASS -- read members of a class from a file
3644432Seric **
3654432Seric **	Parameters:
3664432Seric **		class -- class to define.
3674432Seric **		filename -- name of file to read.
3684432Seric **		fmt -- scanf string to use for match.
3694432Seric **
3704432Seric **	Returns:
3714432Seric **		none
3724432Seric **
3734432Seric **	Side Effects:
3744432Seric **
3754432Seric **		puts all lines in filename that match a scanf into
3764432Seric **			the named class.
3774432Seric */
3784432Seric 
379*54973Seric fileclass(class, filename, fmt, safe)
3804432Seric 	int class;
3814432Seric 	char *filename;
3824432Seric 	char *fmt;
383*54973Seric 	bool safe;
3844432Seric {
38525808Seric 	FILE *f;
386*54973Seric 	struct stat stbuf;
3874432Seric 	char buf[MAXLINE];
3884432Seric 
389*54973Seric 	if (stat(filename, &stbuf) < 0)
390*54973Seric 	{
391*54973Seric 		syserr("fileclass: cannot stat %s", filename);
392*54973Seric 		return;
393*54973Seric 	}
394*54973Seric 	if (!S_ISREG(stbuf.st_mode))
395*54973Seric 	{
396*54973Seric 		syserr("fileclass: %s not a regular file", filename);
397*54973Seric 		return;
398*54973Seric 	}
399*54973Seric 	if (!safe && access(filename, R_OK) < 0)
400*54973Seric 	{
401*54973Seric 		syserr("fileclass: access denied on %s", filename);
402*54973Seric 		return;
403*54973Seric 	}
404*54973Seric 	f = fopen(filename, "r");
4054432Seric 	if (f == NULL)
4064432Seric 	{
407*54973Seric 		syserr("fileclass: cannot open %s", filename);
4084432Seric 		return;
4094432Seric 	}
4104432Seric 
4114432Seric 	while (fgets(buf, sizeof buf, f) != NULL)
4124432Seric 	{
4134432Seric 		register STAB *s;
41425808Seric 		register char *p;
41525808Seric # ifdef SCANF
4164432Seric 		char wordbuf[MAXNAME+1];
4174432Seric 
4184432Seric 		if (sscanf(buf, fmt, wordbuf) != 1)
4194432Seric 			continue;
42025808Seric 		p = wordbuf;
42125808Seric # else SCANF
42225808Seric 		p = buf;
42325808Seric # endif SCANF
42425808Seric 
42525808Seric 		/*
42625808Seric 		**  Break up the match into words.
42725808Seric 		*/
42825808Seric 
42925808Seric 		while (*p != '\0')
43025808Seric 		{
43125808Seric 			register char *q;
43225808Seric 
43325808Seric 			/* strip leading spaces */
43425808Seric 			while (isspace(*p))
43525808Seric 				p++;
43625808Seric 			if (*p == '\0')
43725808Seric 				break;
43825808Seric 
43925808Seric 			/* find the end of the word */
44025808Seric 			q = p;
44125808Seric 			while (*p != '\0' && !isspace(*p))
44225808Seric 				p++;
44325808Seric 			if (*p != '\0')
44425808Seric 				*p++ = '\0';
44525808Seric 
44625808Seric 			/* enter the word in the symbol table */
44725808Seric 			s = stab(q, ST_CLASS, ST_ENTER);
44825808Seric 			setbitn(class, s->s_class);
44925808Seric 		}
4504432Seric 	}
4514432Seric 
452*54973Seric 	(void) fclose(f);
4534432Seric }
4544432Seric /*
4554096Seric **  MAKEMAILER -- define a new mailer.
4564096Seric **
4574096Seric **	Parameters:
45810327Seric **		line -- description of mailer.  This is in labeled
45910327Seric **			fields.  The fields are:
46010327Seric **			   P -- the path to the mailer
46110327Seric **			   F -- the flags associated with the mailer
46210327Seric **			   A -- the argv for this mailer
46310327Seric **			   S -- the sender rewriting set
46410327Seric **			   R -- the recipient rewriting set
46510327Seric **			   E -- the eol string
46610327Seric **			The first word is the canonical name of the mailer.
4674096Seric **
4684096Seric **	Returns:
4694096Seric **		none.
4704096Seric **
4714096Seric **	Side Effects:
4724096Seric **		enters the mailer into the mailer table.
4734096Seric */
4743308Seric 
47521066Seric makemailer(line)
4764096Seric 	char *line;
4774096Seric {
4784096Seric 	register char *p;
4798067Seric 	register struct mailer *m;
4808067Seric 	register STAB *s;
4818067Seric 	int i;
48210327Seric 	char fcode;
4834096Seric 	extern int NextMailer;
48410327Seric 	extern char **makeargv();
48510327Seric 	extern char *munchstring();
48610327Seric 	extern char *DelimChar;
48710701Seric 	extern long atol();
4884096Seric 
48910327Seric 	/* allocate a mailer and set up defaults */
49010327Seric 	m = (struct mailer *) xalloc(sizeof *m);
49110327Seric 	bzero((char *) m, sizeof *m);
49210327Seric 	m->m_mno = NextMailer;
49310327Seric 	m->m_eol = "\n";
49410327Seric 
49510327Seric 	/* collect the mailer name */
49610327Seric 	for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++)
49710327Seric 		continue;
49810327Seric 	if (*p != '\0')
49910327Seric 		*p++ = '\0';
50010327Seric 	m->m_name = newstr(line);
50110327Seric 
50210327Seric 	/* now scan through and assign info from the fields */
50310327Seric 	while (*p != '\0')
50410327Seric 	{
50510327Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
50610327Seric 			p++;
50710327Seric 
50810327Seric 		/* p now points to field code */
50910327Seric 		fcode = *p;
51010327Seric 		while (*p != '\0' && *p != '=' && *p != ',')
51110327Seric 			p++;
51210327Seric 		if (*p++ != '=')
51310327Seric 		{
51452637Seric 			syserr("mailer %s: `=' expected", m->m_name);
51510327Seric 			return;
51610327Seric 		}
51710327Seric 		while (isspace(*p))
51810327Seric 			p++;
51910327Seric 
52010327Seric 		/* p now points to the field body */
52110327Seric 		p = munchstring(p);
52210327Seric 
52310327Seric 		/* install the field into the mailer struct */
52410327Seric 		switch (fcode)
52510327Seric 		{
52610327Seric 		  case 'P':		/* pathname */
52710327Seric 			m->m_mailer = newstr(p);
52810327Seric 			break;
52910327Seric 
53010327Seric 		  case 'F':		/* flags */
53110687Seric 			for (; *p != '\0'; p++)
53252637Seric 				if (!isspace(*p))
53352637Seric 					setbitn(*p, m->m_flags);
53410327Seric 			break;
53510327Seric 
53610327Seric 		  case 'S':		/* sender rewriting ruleset */
53710327Seric 		  case 'R':		/* recipient rewriting ruleset */
53810327Seric 			i = atoi(p);
53910327Seric 			if (i < 0 || i >= MAXRWSETS)
54010327Seric 			{
54110327Seric 				syserr("invalid rewrite set, %d max", MAXRWSETS);
54210327Seric 				return;
54310327Seric 			}
54410327Seric 			if (fcode == 'S')
54510327Seric 				m->m_s_rwset = i;
54610327Seric 			else
54710327Seric 				m->m_r_rwset = i;
54810327Seric 			break;
54910327Seric 
55010327Seric 		  case 'E':		/* end of line string */
55110327Seric 			m->m_eol = newstr(p);
55210327Seric 			break;
55310327Seric 
55410327Seric 		  case 'A':		/* argument vector */
55510327Seric 			m->m_argv = makeargv(p);
55610327Seric 			break;
55710701Seric 
55810701Seric 		  case 'M':		/* maximum message size */
55910701Seric 			m->m_maxsize = atol(p);
56010701Seric 			break;
56152106Seric 
56252106Seric 		  case 'L':		/* maximum line length */
56352106Seric 			m->m_linelimit = atoi(p);
56452106Seric 			break;
56510327Seric 		}
56610327Seric 
56710327Seric 		p = DelimChar;
56810327Seric 	}
56910327Seric 
57052106Seric 	/* do some heuristic cleanup for back compatibility */
57152106Seric 	if (bitnset(M_LIMITS, m->m_flags))
57252106Seric 	{
57352106Seric 		if (m->m_linelimit == 0)
57452106Seric 			m->m_linelimit = SMTPLINELIM;
57552106Seric 		if (!bitnset(M_8BITS, m->m_flags))
57652106Seric 			setbitn(M_7BITS, m->m_flags);
57752106Seric 	}
57852106Seric 
57910327Seric 	/* now store the mailer away */
5804096Seric 	if (NextMailer >= MAXMAILERS)
5814096Seric 	{
5829381Seric 		syserr("too many mailers defined (%d max)", MAXMAILERS);
5834096Seric 		return;
5844096Seric 	}
58510327Seric 	Mailer[NextMailer++] = m;
58610327Seric 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
58710327Seric 	s->s_mailer = m;
58810327Seric }
58910327Seric /*
59010327Seric **  MUNCHSTRING -- translate a string into internal form.
59110327Seric **
59210327Seric **	Parameters:
59310327Seric **		p -- the string to munch.
59410327Seric **
59510327Seric **	Returns:
59610327Seric **		the munched string.
59710327Seric **
59810327Seric **	Side Effects:
59910327Seric **		Sets "DelimChar" to point to the string that caused us
60010327Seric **		to stop.
60110327Seric */
6024096Seric 
60310327Seric char *
60410327Seric munchstring(p)
60510327Seric 	register char *p;
60610327Seric {
60710327Seric 	register char *q;
60810327Seric 	bool backslash = FALSE;
60910327Seric 	bool quotemode = FALSE;
61010327Seric 	static char buf[MAXLINE];
61110327Seric 	extern char *DelimChar;
6124096Seric 
61310327Seric 	for (q = buf; *p != '\0'; p++)
6144096Seric 	{
61510327Seric 		if (backslash)
61610327Seric 		{
61710327Seric 			/* everything is roughly literal */
61810357Seric 			backslash = FALSE;
61910327Seric 			switch (*p)
62010327Seric 			{
62110327Seric 			  case 'r':		/* carriage return */
62210327Seric 				*q++ = '\r';
62310327Seric 				continue;
62410327Seric 
62510327Seric 			  case 'n':		/* newline */
62610327Seric 				*q++ = '\n';
62710327Seric 				continue;
62810327Seric 
62910327Seric 			  case 'f':		/* form feed */
63010327Seric 				*q++ = '\f';
63110327Seric 				continue;
63210327Seric 
63310327Seric 			  case 'b':		/* backspace */
63410327Seric 				*q++ = '\b';
63510327Seric 				continue;
63610327Seric 			}
63710327Seric 			*q++ = *p;
63810327Seric 		}
63910327Seric 		else
64010327Seric 		{
64110327Seric 			if (*p == '\\')
64210327Seric 				backslash = TRUE;
64310327Seric 			else if (*p == '"')
64410327Seric 				quotemode = !quotemode;
64510327Seric 			else if (quotemode || *p != ',')
64610327Seric 				*q++ = *p;
64710327Seric 			else
64810327Seric 				break;
64910327Seric 		}
6504096Seric 	}
6514096Seric 
65210327Seric 	DelimChar = p;
65310327Seric 	*q++ = '\0';
65410327Seric 	return (buf);
65510327Seric }
65610327Seric /*
65710327Seric **  MAKEARGV -- break up a string into words
65810327Seric **
65910327Seric **	Parameters:
66010327Seric **		p -- the string to break up.
66110327Seric **
66210327Seric **	Returns:
66310327Seric **		a char **argv (dynamically allocated)
66410327Seric **
66510327Seric **	Side Effects:
66610327Seric **		munges p.
66710327Seric */
6684096Seric 
66910327Seric char **
67010327Seric makeargv(p)
67110327Seric 	register char *p;
67210327Seric {
67310327Seric 	char *q;
67410327Seric 	int i;
67510327Seric 	char **avp;
67610327Seric 	char *argv[MAXPV + 1];
67710327Seric 
67810327Seric 	/* take apart the words */
67910327Seric 	i = 0;
68010327Seric 	while (*p != '\0' && i < MAXPV)
6814096Seric 	{
68210327Seric 		q = p;
68310327Seric 		while (*p != '\0' && !isspace(*p))
68410327Seric 			p++;
68510327Seric 		while (isspace(*p))
68610327Seric 			*p++ = '\0';
68710327Seric 		argv[i++] = newstr(q);
6884096Seric 	}
68910327Seric 	argv[i++] = NULL;
6904096Seric 
69110327Seric 	/* now make a copy of the argv */
69210327Seric 	avp = (char **) xalloc(sizeof *avp * i);
69316893Seric 	bcopy((char *) argv, (char *) avp, sizeof *avp * i);
69410327Seric 
69510327Seric 	return (avp);
6963308Seric }
6973308Seric /*
6983308Seric **  PRINTRULES -- print rewrite rules (for debugging)
6993308Seric **
7003308Seric **	Parameters:
7013308Seric **		none.
7023308Seric **
7033308Seric **	Returns:
7043308Seric **		none.
7053308Seric **
7063308Seric **	Side Effects:
7073308Seric **		prints rewrite rules.
7083308Seric */
7093308Seric 
7103308Seric printrules()
7113308Seric {
7123308Seric 	register struct rewrite *rwp;
7134072Seric 	register int ruleset;
7143308Seric 
7154072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
7163308Seric 	{
7174072Seric 		if (RewriteRules[ruleset] == NULL)
7184072Seric 			continue;
7198067Seric 		printf("\n----Rule Set %d:", ruleset);
7203308Seric 
7214072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
7223308Seric 		{
7238067Seric 			printf("\nLHS:");
7248067Seric 			printav(rwp->r_lhs);
7258067Seric 			printf("RHS:");
7268067Seric 			printav(rwp->r_rhs);
7273308Seric 		}
7283308Seric 	}
7293308Seric }
7304319Seric 
7314096Seric /*
7328256Seric **  SETOPTION -- set global processing option
7338256Seric **
7348256Seric **	Parameters:
7358256Seric **		opt -- option name.
7368256Seric **		val -- option value (as a text string).
73721755Seric **		safe -- set if this came from a configuration file.
73821755Seric **			Some options (if set from the command line) will
73921755Seric **			reset the user id to avoid security problems.
7408269Seric **		sticky -- if set, don't let other setoptions override
7418269Seric **			this value.
7428256Seric **
7438256Seric **	Returns:
7448256Seric **		none.
7458256Seric **
7468256Seric **	Side Effects:
7478256Seric **		Sets options as implied by the arguments.
7488256Seric */
7498256Seric 
75010687Seric static BITMAP	StickyOpt;		/* set if option is stuck */
7518269Seric 
75221755Seric setoption(opt, val, safe, sticky)
7538256Seric 	char opt;
7548256Seric 	char *val;
75521755Seric 	bool safe;
7568269Seric 	bool sticky;
7578256Seric {
7588265Seric 	extern bool atobool();
75912633Seric 	extern time_t convtime();
76014879Seric 	extern int QueueLA;
76114879Seric 	extern int RefuseLA;
76217474Seric 	extern bool trusteduser();
76317474Seric 	extern char *username();
7648256Seric 
7658256Seric 	if (tTd(37, 1))
7669341Seric 		printf("setoption %c=%s", opt, val);
7678256Seric 
7688256Seric 	/*
7698269Seric 	**  See if this option is preset for us.
7708256Seric 	*/
7718256Seric 
77210687Seric 	if (bitnset(opt, StickyOpt))
7738269Seric 	{
7749341Seric 		if (tTd(37, 1))
7759341Seric 			printf(" (ignored)\n");
7768269Seric 		return;
7778269Seric 	}
7788269Seric 
77921755Seric 	/*
78021755Seric 	**  Check to see if this option can be specified by this user.
78121755Seric 	*/
78221755Seric 
78336238Skarels 	if (!safe && getuid() == 0)
78421755Seric 		safe = TRUE;
78553400Seric 	if (!safe && index("deiLmorsvC", opt) == NULL)
78621755Seric 	{
78739111Srick 		if (opt != 'M' || (val[0] != 'r' && val[0] != 's'))
78821755Seric 		{
78936582Sbostic 			if (tTd(37, 1))
79036582Sbostic 				printf(" (unsafe)");
79136582Sbostic 			if (getuid() != geteuid())
79236582Sbostic 			{
79351210Seric 				if (tTd(37, 1))
79451210Seric 					printf("(Resetting uid)");
79536582Sbostic 				(void) setgid(getgid());
79636582Sbostic 				(void) setuid(getuid());
79736582Sbostic 			}
79821755Seric 		}
79921755Seric 	}
80051210Seric 	if (tTd(37, 1))
80117985Seric 		printf("\n");
8028269Seric 
8038256Seric 	switch (opt)
8048256Seric 	{
80551312Seric 	  case '=':		/* config file generation level */
80651312Seric 		ConfigLevel = atoi(val);
80751312Seric 		break;
80851312Seric 
80952106Seric 	  case '8':		/* allow eight-bit input */
81052106Seric 		EightBit = atobool(val);
81152106Seric 		break;
81252106Seric 
8138256Seric 	  case 'A':		/* set default alias file */
8149381Seric 		if (val[0] == '\0')
8158269Seric 			AliasFile = "aliases";
8169381Seric 		else
8179381Seric 			AliasFile = newstr(val);
8188256Seric 		break;
8198256Seric 
82017474Seric 	  case 'a':		/* look N minutes for "@:@" in alias file */
82117474Seric 		if (val[0] == '\0')
82217474Seric 			SafeAlias = 5;
82317474Seric 		else
82417474Seric 			SafeAlias = atoi(val);
82517474Seric 		break;
82617474Seric 
82716843Seric 	  case 'B':		/* substitution for blank character */
82816843Seric 		SpaceSub = val[0];
82916843Seric 		if (SpaceSub == '\0')
83016843Seric 			SpaceSub = ' ';
83116843Seric 		break;
83216843Seric 
8339284Seric 	  case 'c':		/* don't connect to "expensive" mailers */
8349381Seric 		NoConnect = atobool(val);
8359284Seric 		break;
8369284Seric 
83751305Seric 	  case 'C':		/* checkpoint every N addresses */
83851305Seric 		CheckpointInterval = atoi(val);
83924944Seric 		break;
84024944Seric 
8419284Seric 	  case 'd':		/* delivery mode */
8429284Seric 		switch (*val)
8438269Seric 		{
8449284Seric 		  case '\0':
8459284Seric 			SendMode = SM_DELIVER;
8468269Seric 			break;
8478269Seric 
84810755Seric 		  case SM_QUEUE:	/* queue only */
84910755Seric #ifndef QUEUE
85010755Seric 			syserr("need QUEUE to set -odqueue");
85110755Seric #endif QUEUE
85210755Seric 			/* fall through..... */
85310755Seric 
8549284Seric 		  case SM_DELIVER:	/* do everything */
8559284Seric 		  case SM_FORK:		/* fork after verification */
8569284Seric 			SendMode = *val;
8578269Seric 			break;
8588269Seric 
8598269Seric 		  default:
8609284Seric 			syserr("Unknown delivery mode %c", *val);
8618269Seric 			exit(EX_USAGE);
8628269Seric 		}
8638269Seric 		break;
8648269Seric 
8659146Seric 	  case 'D':		/* rebuild alias database as needed */
8669381Seric 		AutoRebuild = atobool(val);
8679146Seric 		break;
8689146Seric 
8698269Seric 	  case 'e':		/* set error processing mode */
8708269Seric 		switch (*val)
8718269Seric 		{
8729381Seric 		  case EM_QUIET:	/* be silent about it */
8739381Seric 		  case EM_MAIL:		/* mail back */
8749381Seric 		  case EM_BERKNET:	/* do berknet error processing */
8759381Seric 		  case EM_WRITE:	/* write back (or mail) */
8768269Seric 			HoldErrs = TRUE;
8779381Seric 			/* fall through... */
8788269Seric 
8799381Seric 		  case EM_PRINT:	/* print errors normally (default) */
8809381Seric 			ErrorMode = *val;
8818269Seric 			break;
8828269Seric 		}
8838269Seric 		break;
8848269Seric 
8859049Seric 	  case 'F':		/* file mode */
88617975Seric 		FileMode = atooct(val) & 0777;
8879049Seric 		break;
8889049Seric 
8898269Seric 	  case 'f':		/* save Unix-style From lines on front */
8909381Seric 		SaveFrom = atobool(val);
8918269Seric 		break;
8928269Seric 
89353735Seric 	  case 'G':		/* match recipients against GECOS field */
89453735Seric 		MatchGecos = atobool(val);
89553735Seric 		break;
89653735Seric 
8978256Seric 	  case 'g':		/* default gid */
89817474Seric 		DefGid = atoi(val);
8998256Seric 		break;
9008256Seric 
9018256Seric 	  case 'H':		/* help file */
9029381Seric 		if (val[0] == '\0')
9038269Seric 			HelpFile = "sendmail.hf";
9049381Seric 		else
9059381Seric 			HelpFile = newstr(val);
9068256Seric 		break;
9078256Seric 
90851305Seric 	  case 'h':		/* maximum hop count */
90951305Seric 		MaxHopCount = atoi(val);
91051305Seric 		break;
91151305Seric 
91235651Seric 	  case 'I':		/* use internet domain name server */
91335651Seric 		UseNameServer = atobool(val);
91435651Seric 		break;
91535651Seric 
9168269Seric 	  case 'i':		/* ignore dot lines in message */
9179381Seric 		IgnrDot = atobool(val);
9188269Seric 		break;
9198269Seric 
92054967Seric 	  case 'k':		/* connection cache size */
92154967Seric 		MaxMciCache = atoi(val);
92254967Seric 		if (MaxMciCache <= 0)
92354967Seric 			MaxMciCache = 1;
92454967Seric 		break;
92554967Seric 
92654967Seric 	  case 'K':		/* connection cache timeout */
92754967Seric 		MciCacheTimeout = convtime(val);
92854967Seric 		break;
92954967Seric 
9308256Seric 	  case 'L':		/* log level */
9319381Seric 		LogLevel = atoi(val);
9328256Seric 		break;
9338256Seric 
9348269Seric 	  case 'M':		/* define macro */
9359381Seric 		define(val[0], newstr(&val[1]), CurEnv);
93616878Seric 		sticky = FALSE;
9378269Seric 		break;
9388269Seric 
9398269Seric 	  case 'm':		/* send to me too */
9409381Seric 		MeToo = atobool(val);
9418269Seric 		break;
9428269Seric 
94325820Seric 	  case 'n':		/* validate RHS in newaliases */
94425820Seric 		CheckAliases = atobool(val);
94525820Seric 		break;
94625820Seric 
9478269Seric 	  case 'o':		/* assume old style headers */
9489381Seric 		if (atobool(val))
9499341Seric 			CurEnv->e_flags |= EF_OLDSTYLE;
9509341Seric 		else
9519341Seric 			CurEnv->e_flags &= ~EF_OLDSTYLE;
9528269Seric 		break;
9538269Seric 
95424944Seric 	  case 'P':		/* postmaster copy address for returned mail */
95524944Seric 		PostMasterCopy = newstr(val);
95624944Seric 		break;
95724944Seric 
95824944Seric 	  case 'q':		/* slope of queue only function */
95924944Seric 		QueueFactor = atoi(val);
96024944Seric 		break;
96124944Seric 
9628256Seric 	  case 'Q':		/* queue directory */
9639381Seric 		if (val[0] == '\0')
9648269Seric 			QueueDir = "mqueue";
9659381Seric 		else
9669381Seric 			QueueDir = newstr(val);
9678256Seric 		break;
9688256Seric 
9698256Seric 	  case 'r':		/* read timeout */
9709381Seric 		ReadTimeout = convtime(val);
9718256Seric 		break;
9728256Seric 
9738256Seric 	  case 'S':		/* status file */
9749381Seric 		if (val[0] == '\0')
9758269Seric 			StatFile = "sendmail.st";
9769381Seric 		else
9779381Seric 			StatFile = newstr(val);
9788256Seric 		break;
9798256Seric 
9808265Seric 	  case 's':		/* be super safe, even if expensive */
9819381Seric 		SuperSafe = atobool(val);
9828256Seric 		break;
9838256Seric 
9848256Seric 	  case 'T':		/* queue timeout */
9859381Seric 		TimeOut = convtime(val);
98654967Seric 		break;
9878256Seric 
9888265Seric 	  case 't':		/* time zone name */
98952106Seric 		TimeZoneSpec = newstr(val);
9908265Seric 		break;
9918265Seric 
99250556Seric 	  case 'U':		/* location of user database */
99351360Seric 		UdbSpec = newstr(val);
99450556Seric 		break;
99550556Seric 
9968256Seric 	  case 'u':		/* set default uid */
99717474Seric 		DefUid = atoi(val);
99840973Sbostic 		setdefuser();
9998256Seric 		break;
10008256Seric 
10018269Seric 	  case 'v':		/* run in verbose mode */
10029381Seric 		Verbose = atobool(val);
10038256Seric 		break;
10048256Seric 
100551216Seric 	  case 'w':		/* we don't have wildcard MX records */
100651216Seric 		NoWildcardMX = atobool(val);
100750537Seric 		break;
100850537Seric 
100914879Seric 	  case 'x':		/* load avg at which to auto-queue msgs */
101014879Seric 		QueueLA = atoi(val);
101114879Seric 		break;
101214879Seric 
101314879Seric 	  case 'X':		/* load avg at which to auto-reject connections */
101414879Seric 		RefuseLA = atoi(val);
101514879Seric 		break;
101614879Seric 
101724981Seric 	  case 'y':		/* work recipient factor */
101824981Seric 		WkRecipFact = atoi(val);
101924981Seric 		break;
102024981Seric 
102124981Seric 	  case 'Y':		/* fork jobs during queue runs */
102224952Seric 		ForkQueueRuns = atobool(val);
102324952Seric 		break;
102424952Seric 
102524981Seric 	  case 'z':		/* work message class factor */
102624981Seric 		WkClassFact = atoi(val);
102724981Seric 		break;
102824981Seric 
102924981Seric 	  case 'Z':		/* work time factor */
103024981Seric 		WkTimeFact = atoi(val);
103124981Seric 		break;
103224981Seric 
10338256Seric 	  default:
10348256Seric 		break;
10358256Seric 	}
103616878Seric 	if (sticky)
103716878Seric 		setbitn(opt, StickyOpt);
10389188Seric 	return;
10398256Seric }
104010687Seric /*
104110687Seric **  SETCLASS -- set a word into a class
104210687Seric **
104310687Seric **	Parameters:
104410687Seric **		class -- the class to put the word in.
104510687Seric **		word -- the word to enter
104610687Seric **
104710687Seric **	Returns:
104810687Seric **		none.
104910687Seric **
105010687Seric **	Side Effects:
105110687Seric **		puts the word into the symbol table.
105210687Seric */
105310687Seric 
105410687Seric setclass(class, word)
105510687Seric 	int class;
105610687Seric 	char *word;
105710687Seric {
105810687Seric 	register STAB *s;
105910687Seric 
106010687Seric 	s = stab(word, ST_CLASS, ST_ENTER);
106110687Seric 	setbitn(class, s->s_class);
106210687Seric }
106353654Seric /*
106453654Seric **  MAKEMAPENTRY -- create a map entry
106553654Seric **
106653654Seric **	Parameters:
106753654Seric **		line -- the config file line
106853654Seric **
106953654Seric **	Returns:
107053654Seric **		TRUE if it successfully entered the map entry.
107153654Seric **		FALSE otherwise (usually syntax error).
107253654Seric **
107353654Seric **	Side Effects:
107453654Seric **		Enters the map into the dictionary.
107553654Seric */
107653654Seric 
107753654Seric void
107853654Seric makemapentry(line)
107953654Seric 	char *line;
108053654Seric {
108153654Seric 	register char *p;
108253654Seric 	char *mapname;
108353654Seric 	char *classname;
108453654Seric 	register STAB *map;
108553654Seric 	STAB *class;
108653654Seric 
108753654Seric 	for (p = line; isspace(*p); p++)
108853654Seric 		continue;
108953654Seric 	if (!isalnum(*p))
109053654Seric 	{
109153654Seric 		syserr("readcf: config K line: no map name");
109253654Seric 		return;
109353654Seric 	}
109453654Seric 
109553654Seric 	mapname = p;
109653654Seric 	while (isalnum(*++p))
109753654Seric 		continue;
109853654Seric 	if (*p != '\0')
109953654Seric 		*p++ = '\0';
110053654Seric 	while (isspace(*p))
110153654Seric 		p++;
110253654Seric 	if (!isalnum(*p))
110353654Seric 	{
110453654Seric 		syserr("readcf: config K line, map %s: no map class", mapname);
110553654Seric 		return;
110653654Seric 	}
110753654Seric 	classname = p;
110853654Seric 	while (isalnum(*++p))
110953654Seric 		continue;
111053654Seric 	if (*p != '\0')
111153654Seric 		*p++ = '\0';
111253654Seric 	while (isspace(*p))
111353654Seric 		p++;
111453654Seric 
111553654Seric 	/* look up the class */
111653654Seric 	class = stab(classname, ST_MAPCLASS, ST_FIND);
111753654Seric 	if (class == NULL)
111853654Seric 	{
111953654Seric 		syserr("readcf: map %s: class %s not available", mapname, classname);
112053654Seric 		return;
112153654Seric 	}
112253654Seric 
112353654Seric 	/* enter the map */
112453654Seric 	map = stab(mapname, ST_MAP, ST_ENTER);
112553654Seric 	map->s_map.map_class = &class->s_mapclass;
112653654Seric 
112753654Seric 	if ((*class->s_mapclass.map_init)(&map->s_map, p))
112853654Seric 		map->s_map.map_flags |= MF_VALID;
112953654Seric }
1130