xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 59283)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)readcf.c	6.29 (Berkeley) 04/26/93";
11 #endif /* not lint */
12 
13 # include "sendmail.h"
14 # include <sys/stat.h>
15 #ifdef NAMED_BIND
16 # include <arpa/nameser.h>
17 # include <resolv.h>
18 #endif
19 
20 /* System 5 compatibility */
21 #ifndef S_ISREG
22 #define S_ISREG(foo)	((foo & S_IFREG) == S_IFREG)
23 #endif
24 #ifndef S_IWGRP
25 #define S_IWGRP		020
26 #endif
27 #ifndef S_IWOTH
28 #define S_IWOTH		002
29 #endif
30 
31 /*
32 **  READCF -- read control file.
33 **
34 **	This routine reads the control file and builds the internal
35 **	form.
36 **
37 **	The file is formatted as a sequence of lines, each taken
38 **	atomically.  The first character of each line describes how
39 **	the line is to be interpreted.  The lines are:
40 **		Dxval		Define macro x to have value val.
41 **		Cxword		Put word into class x.
42 **		Fxfile [fmt]	Read file for lines to put into
43 **				class x.  Use scanf string 'fmt'
44 **				or "%s" if not present.  Fmt should
45 **				only produce one string-valued result.
46 **		Hname: value	Define header with field-name 'name'
47 **				and value as specified; this will be
48 **				macro expanded immediately before
49 **				use.
50 **		Sn		Use rewriting set n.
51 **		Rlhs rhs	Rewrite addresses that match lhs to
52 **				be rhs.
53 **		Mn arg=val...	Define mailer.  n is the internal name.
54 **				Args specify mailer parameters.
55 **		Oxvalue		Set option x to value.
56 **		Pname=value	Set precedence name to value.
57 **		Vversioncode	Version level of configuration syntax.
58 **		Kmapname mapclass arguments....
59 **				Define keyed lookup of a given class.
60 **				Arguments are class dependent.
61 **
62 **	Parameters:
63 **		cfname -- control file name.
64 **		safe -- TRUE if this is the system config file;
65 **			FALSE otherwise.
66 **		e -- the main envelope.
67 **
68 **	Returns:
69 **		none.
70 **
71 **	Side Effects:
72 **		Builds several internal tables.
73 */
74 
75 readcf(cfname, safe, e)
76 	char *cfname;
77 	bool safe;
78 	register ENVELOPE *e;
79 {
80 	FILE *cf;
81 	int ruleset = 0;
82 	char *q;
83 	char **pv;
84 	struct rewrite *rwp = NULL;
85 	char *bp;
86 	int nfuzzy;
87 	char buf[MAXLINE];
88 	register char *p;
89 	extern char **prescan();
90 	extern char **copyplist();
91 	struct stat statb;
92 	char exbuf[MAXLINE];
93 	char pvpbuf[PSBUFSIZE];
94 	extern char *fgetfolded();
95 	extern char *munchstring();
96 	extern void makemapentry();
97 
98 	FileName = cfname;
99 	LineNumber = 0;
100 
101 	cf = fopen(cfname, "r");
102 	if (cf == NULL)
103 	{
104 		syserr("cannot open");
105 		exit(EX_OSFILE);
106 	}
107 
108 	if (fstat(fileno(cf), &statb) < 0)
109 	{
110 		syserr("cannot fstat");
111 		exit(EX_OSFILE);
112 	}
113 
114 	if (!S_ISREG(statb.st_mode))
115 	{
116 		syserr("not a plain file");
117 		exit(EX_OSFILE);
118 	}
119 
120 	if (OpMode != MD_TEST && bitset(S_IWGRP|S_IWOTH, statb.st_mode))
121 	{
122 		if (OpMode == MD_DAEMON || OpMode == MD_FREEZE)
123 			fprintf(stderr, "%s: WARNING: dangerous write permissions\n",
124 				FileName);
125 #ifdef LOG
126 		if (LogLevel > 0)
127 			syslog(LOG_CRIT, "%s: WARNING: dangerous write permissions",
128 				FileName);
129 #endif
130 	}
131 
132 #ifdef XLA
133 	xla_zero();
134 #endif
135 
136 	while ((bp = fgetfolded(buf, sizeof buf, cf)) != NULL)
137 	{
138 		if (bp[0] == '#')
139 		{
140 			if (bp != buf)
141 				free(bp);
142 			continue;
143 		}
144 
145 		/* map $ into \201 for macro expansion */
146 		for (p = bp; *p != '\0'; p++)
147 		{
148 			if (*p == '#' && p > bp && ConfigLevel >= 3)
149 			{
150 				/* this is an on-line comment */
151 				register char *e;
152 
153 				switch (*--p & 0377)
154 				{
155 				  case MACROEXPAND:
156 					/* it's from $# -- let it go through */
157 					p++;
158 					break;
159 
160 				  case '\\':
161 					/* it's backslash escaped */
162 					(void) strcpy(p, p + 1);
163 					break;
164 
165 				  default:
166 					/* delete preceeding white space */
167 					while (isascii(*p) && isspace(*p) && p > bp)
168 						p--;
169 					if ((e = strchr(++p, '\n')) != NULL)
170 						(void) strcpy(p, e);
171 					else
172 						p[0] = p[1] = '\0';
173 					break;
174 				}
175 				continue;
176 			}
177 
178 			if (*p != '$')
179 				continue;
180 
181 			if (p[1] == '$')
182 			{
183 				/* actual dollar sign.... */
184 				(void) strcpy(p, p + 1);
185 				continue;
186 			}
187 
188 			/* convert to macro expansion character */
189 			*p = MACROEXPAND;
190 		}
191 
192 		/* interpret this line */
193 		switch (bp[0])
194 		{
195 		  case '\0':
196 		  case '#':		/* comment */
197 			break;
198 
199 		  case 'R':		/* rewriting rule */
200 			for (p = &bp[1]; *p != '\0' && *p != '\t'; p++)
201 				continue;
202 
203 			if (*p == '\0')
204 			{
205 				syserr("invalid rewrite line \"%s\"", bp);
206 				break;
207 			}
208 
209 			/* allocate space for the rule header */
210 			if (rwp == NULL)
211 			{
212 				RewriteRules[ruleset] = rwp =
213 					(struct rewrite *) xalloc(sizeof *rwp);
214 			}
215 			else
216 			{
217 				rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp);
218 				rwp = rwp->r_next;
219 			}
220 			rwp->r_next = NULL;
221 
222 			/* expand and save the LHS */
223 			*p = '\0';
224 			expand(&bp[1], exbuf, &exbuf[sizeof exbuf], e);
225 			rwp->r_lhs = prescan(exbuf, '\t', pvpbuf, NULL);
226 			nfuzzy = 0;
227 			if (rwp->r_lhs != NULL)
228 			{
229 				register char **ap;
230 
231 				rwp->r_lhs = copyplist(rwp->r_lhs, TRUE);
232 
233 				/* count the number of fuzzy matches in LHS */
234 				for (ap = rwp->r_lhs; *ap != NULL; ap++)
235 				{
236 					char *botch;
237 
238 					botch = NULL;
239 					switch (**ap & 0377)
240 					{
241 					  case MATCHZANY:
242 					  case MATCHANY:
243 					  case MATCHONE:
244 					  case MATCHCLASS:
245 					  case MATCHNCLASS:
246 						nfuzzy++;
247 						break;
248 
249 					  case MATCHREPL:
250 						botch = "$0-$9";
251 						break;
252 
253 					  case CANONNET:
254 						botch = "$#";
255 						break;
256 
257 					  case CANONUSER:
258 						botch = "$:";
259 						break;
260 
261 					  case CALLSUBR:
262 						botch = "$>";
263 						break;
264 
265 					  case CONDIF:
266 						botch = "$?";
267 						break;
268 
269 					  case CONDELSE:
270 						botch = "$|";
271 						break;
272 
273 					  case CONDFI:
274 						botch = "$.";
275 						break;
276 
277 					  case HOSTBEGIN:
278 						botch = "$[";
279 						break;
280 
281 					  case HOSTEND:
282 						botch = "$]";
283 						break;
284 
285 					  case LOOKUPBEGIN:
286 						botch = "$(";
287 						break;
288 
289 					  case LOOKUPEND:
290 						botch = "$)";
291 						break;
292 					}
293 					if (botch != NULL)
294 						syserr("Inappropriate use of %s on LHS",
295 							botch);
296 				}
297 			}
298 			else
299 				syserr("R line: null LHS");
300 
301 			/* expand and save the RHS */
302 			while (*++p == '\t')
303 				continue;
304 			q = p;
305 			while (*p != '\0' && *p != '\t')
306 				p++;
307 			*p = '\0';
308 			expand(q, exbuf, &exbuf[sizeof exbuf], e);
309 			rwp->r_rhs = prescan(exbuf, '\t', pvpbuf, NULL);
310 			if (rwp->r_rhs != NULL)
311 			{
312 				register char **ap;
313 
314 				rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
315 
316 				/* check no out-of-bounds replacements */
317 				nfuzzy += '0';
318 				for (ap = rwp->r_rhs; *ap != NULL; ap++)
319 				{
320 					char *botch;
321 
322 					botch = NULL;
323 					switch (**ap & 0377)
324 					{
325 					  case MATCHREPL:
326 						if ((*ap)[1] <= '0' || (*ap)[1] > nfuzzy)
327 						{
328 							syserr("replacement $%c out of bounds",
329 								(*ap)[1]);
330 						}
331 						break;
332 
333 					  case MATCHZANY:
334 						botch = "$*";
335 						break;
336 
337 					  case MATCHANY:
338 						botch = "$+";
339 						break;
340 
341 					  case MATCHONE:
342 						botch = "$-";
343 						break;
344 
345 					  case MATCHCLASS:
346 						botch = "$=";
347 						break;
348 
349 					  case MATCHNCLASS:
350 						botch = "$~";
351 						break;
352 					}
353 					if (botch != NULL)
354 						syserr("Inappropriate use of %s on RHS",
355 							botch);
356 				}
357 			}
358 			else
359 				syserr("R line: null RHS");
360 			break;
361 
362 		  case 'S':		/* select rewriting set */
363 			ruleset = atoi(&bp[1]);
364 			if (ruleset >= MAXRWSETS || ruleset < 0)
365 			{
366 				syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS);
367 				ruleset = 0;
368 			}
369 			rwp = NULL;
370 			break;
371 
372 		  case 'D':		/* macro definition */
373 			define(bp[1], newstr(munchstring(&bp[2], NULL)), e);
374 			break;
375 
376 		  case 'H':		/* required header line */
377 			(void) chompheader(&bp[1], TRUE, e);
378 			break;
379 
380 		  case 'C':		/* word class */
381 			/* scan the list of words and set class for all */
382 			for (p = &bp[2]; *p != '\0'; )
383 			{
384 				register char *wd;
385 				char delim;
386 
387 				while (*p != '\0' && isascii(*p) && isspace(*p))
388 					p++;
389 				wd = p;
390 				while (*p != '\0' && !(isascii(*p) && isspace(*p)))
391 					p++;
392 				delim = *p;
393 				*p = '\0';
394 				if (wd[0] != '\0')
395 				{
396 					if (tTd(37, 2))
397 						printf("setclass(%c, %s)\n",
398 							bp[1], wd);
399 					setclass(bp[1], wd);
400 				}
401 				*p = delim;
402 			}
403 			break;
404 
405 		  case 'F':		/* word class from file */
406 			/* read list of words from argument or file */
407 			/* read from file */
408 			for (p = &bp[2];
409 			     *p != '\0' && !(isascii(*p) && isspace(*p));
410 			     p++)
411 				continue;
412 			if (*p == '\0')
413 				p = "%s";
414 			else
415 			{
416 				*p = '\0';
417 				while (isascii(*++p) && isspace(*p))
418 					continue;
419 			}
420 			fileclass(bp[1], &bp[2], p, safe);
421 			break;
422 
423 #ifdef XLA
424 		  case 'L':		/* extended load average description */
425 			xla_init(&bp[1]);
426 			break;
427 #endif
428 
429 		  case 'M':		/* define mailer */
430 			makemailer(&bp[1]);
431 			break;
432 
433 		  case 'O':		/* set option */
434 			setoption(bp[1], &bp[2], safe, FALSE, e);
435 			break;
436 
437 		  case 'P':		/* set precedence */
438 			if (NumPriorities >= MAXPRIORITIES)
439 			{
440 				toomany('P', MAXPRIORITIES);
441 				break;
442 			}
443 			for (p = &bp[1]; *p != '\0' && *p != '=' && *p != '\t'; p++)
444 				continue;
445 			if (*p == '\0')
446 				goto badline;
447 			*p = '\0';
448 			Priorities[NumPriorities].pri_name = newstr(&bp[1]);
449 			Priorities[NumPriorities].pri_val = atoi(++p);
450 			NumPriorities++;
451 			break;
452 
453 		  case 'T':		/* trusted user(s) */
454 			/* this option is obsolete, but will be ignored */
455 			break;
456 
457 		  case 'V':		/* configuration syntax version */
458 			ConfigLevel = atoi(&bp[1]);
459 			break;
460 
461 		  case 'K':
462 			makemapentry(&bp[1]);
463 			break;
464 
465 		  default:
466 		  badline:
467 			syserr("unknown control line \"%s\"", bp);
468 		}
469 		if (bp != buf)
470 			free(bp);
471 	}
472 	if (ferror(cf))
473 	{
474 		syserr("I/O read error", cfname);
475 		exit(EX_OSFILE);
476 	}
477 	fclose(cf);
478 	FileName = NULL;
479 
480 	if (stab("host", ST_MAP, ST_FIND) == NULL)
481 	{
482 		/* user didn't initialize: set up host map */
483 		strcpy(buf, "host host");
484 		if (ConfigLevel >= 2)
485 			strcat(buf, " -a.");
486 		makemapentry(buf);
487 	}
488 }
489 /*
490 **  TOOMANY -- signal too many of some option
491 **
492 **	Parameters:
493 **		id -- the id of the error line
494 **		maxcnt -- the maximum possible values
495 **
496 **	Returns:
497 **		none.
498 **
499 **	Side Effects:
500 **		gives a syserr.
501 */
502 
503 toomany(id, maxcnt)
504 	char id;
505 	int maxcnt;
506 {
507 	syserr("too many %c lines, %d max", id, maxcnt);
508 }
509 /*
510 **  FILECLASS -- read members of a class from a file
511 **
512 **	Parameters:
513 **		class -- class to define.
514 **		filename -- name of file to read.
515 **		fmt -- scanf string to use for match.
516 **
517 **	Returns:
518 **		none
519 **
520 **	Side Effects:
521 **
522 **		puts all lines in filename that match a scanf into
523 **			the named class.
524 */
525 
526 fileclass(class, filename, fmt, safe)
527 	int class;
528 	char *filename;
529 	char *fmt;
530 	bool safe;
531 {
532 	FILE *f;
533 	struct stat stbuf;
534 	char buf[MAXLINE];
535 
536 	if (stat(filename, &stbuf) < 0)
537 	{
538 		syserr("fileclass: cannot stat %s", filename);
539 		return;
540 	}
541 	if (!S_ISREG(stbuf.st_mode))
542 	{
543 		syserr("fileclass: %s not a regular file", filename);
544 		return;
545 	}
546 	if (!safe && access(filename, R_OK) < 0)
547 	{
548 		syserr("fileclass: access denied on %s", filename);
549 		return;
550 	}
551 	f = fopen(filename, "r");
552 	if (f == NULL)
553 	{
554 		syserr("fileclass: cannot open %s", filename);
555 		return;
556 	}
557 
558 	while (fgets(buf, sizeof buf, f) != NULL)
559 	{
560 		register STAB *s;
561 		register char *p;
562 # ifdef SCANF
563 		char wordbuf[MAXNAME+1];
564 
565 		if (sscanf(buf, fmt, wordbuf) != 1)
566 			continue;
567 		p = wordbuf;
568 # else /* SCANF */
569 		p = buf;
570 # endif /* SCANF */
571 
572 		/*
573 		**  Break up the match into words.
574 		*/
575 
576 		while (*p != '\0')
577 		{
578 			register char *q;
579 
580 			/* strip leading spaces */
581 			while (isascii(*p) && isspace(*p))
582 				p++;
583 			if (*p == '\0')
584 				break;
585 
586 			/* find the end of the word */
587 			q = p;
588 			while (*p != '\0' && !(isascii(*p) && isspace(*p)))
589 				p++;
590 			if (*p != '\0')
591 				*p++ = '\0';
592 
593 			/* enter the word in the symbol table */
594 			s = stab(q, ST_CLASS, ST_ENTER);
595 			setbitn(class, s->s_class);
596 		}
597 	}
598 
599 	(void) fclose(f);
600 }
601 /*
602 **  MAKEMAILER -- define a new mailer.
603 **
604 **	Parameters:
605 **		line -- description of mailer.  This is in labeled
606 **			fields.  The fields are:
607 **			   P -- the path to the mailer
608 **			   F -- the flags associated with the mailer
609 **			   A -- the argv for this mailer
610 **			   S -- the sender rewriting set
611 **			   R -- the recipient rewriting set
612 **			   E -- the eol string
613 **			The first word is the canonical name of the mailer.
614 **
615 **	Returns:
616 **		none.
617 **
618 **	Side Effects:
619 **		enters the mailer into the mailer table.
620 */
621 
622 makemailer(line)
623 	char *line;
624 {
625 	register char *p;
626 	register struct mailer *m;
627 	register STAB *s;
628 	int i;
629 	char fcode;
630 	auto char *endp;
631 	extern int NextMailer;
632 	extern char **makeargv();
633 	extern char *munchstring();
634 	extern long atol();
635 
636 	/* allocate a mailer and set up defaults */
637 	m = (struct mailer *) xalloc(sizeof *m);
638 	bzero((char *) m, sizeof *m);
639 	m->m_eol = "\n";
640 
641 	/* collect the mailer name */
642 	for (p = line; *p != '\0' && *p != ',' && !(isascii(*p) && isspace(*p)); p++)
643 		continue;
644 	if (*p != '\0')
645 		*p++ = '\0';
646 	m->m_name = newstr(line);
647 
648 	/* now scan through and assign info from the fields */
649 	while (*p != '\0')
650 	{
651 		auto char *delimptr;
652 
653 		while (*p != '\0' && (*p == ',' || (isascii(*p) && isspace(*p))))
654 			p++;
655 
656 		/* p now points to field code */
657 		fcode = *p;
658 		while (*p != '\0' && *p != '=' && *p != ',')
659 			p++;
660 		if (*p++ != '=')
661 		{
662 			syserr("mailer %s: `=' expected", m->m_name);
663 			return;
664 		}
665 		while (isascii(*p) && isspace(*p))
666 			p++;
667 
668 		/* p now points to the field body */
669 		p = munchstring(p, &delimptr);
670 
671 		/* install the field into the mailer struct */
672 		switch (fcode)
673 		{
674 		  case 'P':		/* pathname */
675 			m->m_mailer = newstr(p);
676 			break;
677 
678 		  case 'F':		/* flags */
679 			for (; *p != '\0'; p++)
680 				if (!(isascii(*p) && isspace(*p)))
681 					setbitn(*p, m->m_flags);
682 			break;
683 
684 		  case 'S':		/* sender rewriting ruleset */
685 		  case 'R':		/* recipient rewriting ruleset */
686 			i = strtol(p, &endp, 10);
687 			if (i < 0 || i >= MAXRWSETS)
688 			{
689 				syserr("invalid rewrite set, %d max", MAXRWSETS);
690 				return;
691 			}
692 			if (fcode == 'S')
693 				m->m_sh_rwset = m->m_se_rwset = i;
694 			else
695 				m->m_rh_rwset = m->m_re_rwset = i;
696 
697 			p = endp;
698 			if (*p == '/')
699 			{
700 				i = strtol(p, NULL, 10);
701 				if (i < 0 || i >= MAXRWSETS)
702 				{
703 					syserr("invalid rewrite set, %d max",
704 						MAXRWSETS);
705 					return;
706 				}
707 				if (fcode == 'S')
708 					m->m_sh_rwset = i;
709 				else
710 					m->m_rh_rwset = i;
711 			}
712 			break;
713 
714 		  case 'E':		/* end of line string */
715 			m->m_eol = newstr(p);
716 			break;
717 
718 		  case 'A':		/* argument vector */
719 			m->m_argv = makeargv(p);
720 			break;
721 
722 		  case 'M':		/* maximum message size */
723 			m->m_maxsize = atol(p);
724 			break;
725 
726 		  case 'L':		/* maximum line length */
727 			m->m_linelimit = atoi(p);
728 			break;
729 
730 		  case 'D':		/* working directory */
731 			m->m_execdir = newstr(p);
732 			break;
733 		}
734 
735 		p = delimptr;
736 	}
737 
738 	/* do some heuristic cleanup for back compatibility */
739 	if (bitnset(M_LIMITS, m->m_flags))
740 	{
741 		if (m->m_linelimit == 0)
742 			m->m_linelimit = SMTPLINELIM;
743 		if (ConfigLevel < 2)
744 			setbitn(M_7BITS, m->m_flags);
745 	}
746 
747 	/* do some rationality checking */
748 	if (m->m_argv == NULL)
749 	{
750 		syserr("M%s: A= argument required", m->m_name);
751 		return;
752 	}
753 	if (m->m_mailer == NULL)
754 	{
755 		syserr("M%s: P= argument required", m->m_name);
756 		return;
757 	}
758 
759 	if (NextMailer >= MAXMAILERS)
760 	{
761 		syserr("too many mailers defined (%d max)", MAXMAILERS);
762 		return;
763 	}
764 
765 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
766 	if (s->s_mailer != NULL)
767 	{
768 		i = s->s_mailer->m_mno;
769 		free(s->s_mailer);
770 	}
771 	else
772 	{
773 		i = NextMailer++;
774 	}
775 	Mailer[i] = s->s_mailer = m;
776 	m->m_mno = i;
777 }
778 /*
779 **  MUNCHSTRING -- translate a string into internal form.
780 **
781 **	Parameters:
782 **		p -- the string to munch.
783 **		delimptr -- if non-NULL, set to the pointer of the
784 **			field delimiter character.
785 **
786 **	Returns:
787 **		the munched string.
788 */
789 
790 char *
791 munchstring(p, delimptr)
792 	register char *p;
793 	char **delimptr;
794 {
795 	register char *q;
796 	bool backslash = FALSE;
797 	bool quotemode = FALSE;
798 	static char buf[MAXLINE];
799 
800 	for (q = buf; *p != '\0'; p++)
801 	{
802 		if (backslash)
803 		{
804 			/* everything is roughly literal */
805 			backslash = FALSE;
806 			switch (*p)
807 			{
808 			  case 'r':		/* carriage return */
809 				*q++ = '\r';
810 				continue;
811 
812 			  case 'n':		/* newline */
813 				*q++ = '\n';
814 				continue;
815 
816 			  case 'f':		/* form feed */
817 				*q++ = '\f';
818 				continue;
819 
820 			  case 'b':		/* backspace */
821 				*q++ = '\b';
822 				continue;
823 			}
824 			*q++ = *p;
825 		}
826 		else
827 		{
828 			if (*p == '\\')
829 				backslash = TRUE;
830 			else if (*p == '"')
831 				quotemode = !quotemode;
832 			else if (quotemode || *p != ',')
833 				*q++ = *p;
834 			else
835 				break;
836 		}
837 	}
838 
839 	if (delimptr != NULL)
840 		*delimptr = p;
841 	*q++ = '\0';
842 	return (buf);
843 }
844 /*
845 **  MAKEARGV -- break up a string into words
846 **
847 **	Parameters:
848 **		p -- the string to break up.
849 **
850 **	Returns:
851 **		a char **argv (dynamically allocated)
852 **
853 **	Side Effects:
854 **		munges p.
855 */
856 
857 char **
858 makeargv(p)
859 	register char *p;
860 {
861 	char *q;
862 	int i;
863 	char **avp;
864 	char *argv[MAXPV + 1];
865 
866 	/* take apart the words */
867 	i = 0;
868 	while (*p != '\0' && i < MAXPV)
869 	{
870 		q = p;
871 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
872 			p++;
873 		while (isascii(*p) && isspace(*p))
874 			*p++ = '\0';
875 		argv[i++] = newstr(q);
876 	}
877 	argv[i++] = NULL;
878 
879 	/* now make a copy of the argv */
880 	avp = (char **) xalloc(sizeof *avp * i);
881 	bcopy((char *) argv, (char *) avp, sizeof *avp * i);
882 
883 	return (avp);
884 }
885 /*
886 **  PRINTRULES -- print rewrite rules (for debugging)
887 **
888 **	Parameters:
889 **		none.
890 **
891 **	Returns:
892 **		none.
893 **
894 **	Side Effects:
895 **		prints rewrite rules.
896 */
897 
898 printrules()
899 {
900 	register struct rewrite *rwp;
901 	register int ruleset;
902 
903 	for (ruleset = 0; ruleset < 10; ruleset++)
904 	{
905 		if (RewriteRules[ruleset] == NULL)
906 			continue;
907 		printf("\n----Rule Set %d:", ruleset);
908 
909 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
910 		{
911 			printf("\nLHS:");
912 			printav(rwp->r_lhs);
913 			printf("RHS:");
914 			printav(rwp->r_rhs);
915 		}
916 	}
917 }
918 
919 /*
920 **  SETOPTION -- set global processing option
921 **
922 **	Parameters:
923 **		opt -- option name.
924 **		val -- option value (as a text string).
925 **		safe -- set if this came from a configuration file.
926 **			Some options (if set from the command line) will
927 **			reset the user id to avoid security problems.
928 **		sticky -- if set, don't let other setoptions override
929 **			this value.
930 **		e -- the main envelope.
931 **
932 **	Returns:
933 **		none.
934 **
935 **	Side Effects:
936 **		Sets options as implied by the arguments.
937 */
938 
939 static BITMAP	StickyOpt;		/* set if option is stuck */
940 
941 
942 #ifdef NAMED_BIND
943 
944 struct resolverflags
945 {
946 	char	*rf_name;	/* name of the flag */
947 	long	rf_bits;	/* bits to set/clear */
948 } ResolverFlags[] =
949 {
950 	"debug",	RES_DEBUG,
951 	"aaonly",	RES_AAONLY,
952 	"usevc",	RES_USEVC,
953 	"primary",	RES_PRIMARY,
954 	"igntc",	RES_IGNTC,
955 	"recurse",	RES_RECURSE,
956 	"defnames",	RES_DEFNAMES,
957 	"stayopen",	RES_STAYOPEN,
958 	"dnsrch",	RES_DNSRCH,
959 	NULL,		0
960 };
961 
962 #endif
963 
964 setoption(opt, val, safe, sticky, e)
965 	char opt;
966 	char *val;
967 	bool safe;
968 	bool sticky;
969 	register ENVELOPE *e;
970 {
971 	register char *p;
972 	extern bool atobool();
973 	extern time_t convtime();
974 	extern int QueueLA;
975 	extern int RefuseLA;
976 	extern bool trusteduser();
977 	extern char *username();
978 
979 	if (tTd(37, 1))
980 		printf("setoption %c=%s", opt, val);
981 
982 	/*
983 	**  See if this option is preset for us.
984 	*/
985 
986 	if (bitnset(opt, StickyOpt))
987 	{
988 		if (tTd(37, 1))
989 			printf(" (ignored)\n");
990 		return;
991 	}
992 
993 	/*
994 	**  Check to see if this option can be specified by this user.
995 	*/
996 
997 	if (!safe && getuid() == 0)
998 		safe = TRUE;
999 	if (!safe && strchr("bdeEiLmoprsvC8", opt) == NULL)
1000 	{
1001 		if (opt != 'M' || (val[0] != 'r' && val[0] != 's'))
1002 		{
1003 			if (tTd(37, 1))
1004 				printf(" (unsafe)");
1005 			if (getuid() != geteuid())
1006 			{
1007 				if (tTd(37, 1))
1008 					printf("(Resetting uid)");
1009 				(void) setgid(getgid());
1010 				(void) setuid(getuid());
1011 			}
1012 		}
1013 	}
1014 	if (tTd(37, 1))
1015 		printf("\n");
1016 
1017 	switch (opt)
1018 	{
1019 	  case '8':		/* allow eight-bit input */
1020 		EightBit = atobool(val);
1021 		break;
1022 
1023 	  case 'A':		/* set default alias file */
1024 		if (val[0] == '\0')
1025 			AliasFile = "aliases";
1026 		else
1027 			AliasFile = newstr(val);
1028 		break;
1029 
1030 	  case 'a':		/* look N minutes for "@:@" in alias file */
1031 		if (val[0] == '\0')
1032 			SafeAlias = 5;
1033 		else
1034 			SafeAlias = atoi(val);
1035 		break;
1036 
1037 	  case 'B':		/* substitution for blank character */
1038 		SpaceSub = val[0];
1039 		if (SpaceSub == '\0')
1040 			SpaceSub = ' ';
1041 		break;
1042 
1043 	  case 'b':		/* min blocks free on queue fs/max msg size */
1044 		p = strchr(val, '/');
1045 		if (p != NULL)
1046 		{
1047 			*p++ = '\0';
1048 			MaxMessageSize = atol(p);
1049 		}
1050 		MinBlocksFree = atol(val);
1051 		break;
1052 
1053 	  case 'c':		/* don't connect to "expensive" mailers */
1054 		NoConnect = atobool(val);
1055 		break;
1056 
1057 	  case 'C':		/* checkpoint every N addresses */
1058 		CheckpointInterval = atoi(val);
1059 		break;
1060 
1061 	  case 'd':		/* delivery mode */
1062 		switch (*val)
1063 		{
1064 		  case '\0':
1065 			e->e_sendmode = SM_DELIVER;
1066 			break;
1067 
1068 		  case SM_QUEUE:	/* queue only */
1069 #ifndef QUEUE
1070 			syserr("need QUEUE to set -odqueue");
1071 #endif /* QUEUE */
1072 			/* fall through..... */
1073 
1074 		  case SM_DELIVER:	/* do everything */
1075 		  case SM_FORK:		/* fork after verification */
1076 			e->e_sendmode = *val;
1077 			break;
1078 
1079 		  default:
1080 			syserr("Unknown delivery mode %c", *val);
1081 			exit(EX_USAGE);
1082 		}
1083 		break;
1084 
1085 	  case 'D':		/* rebuild alias database as needed */
1086 		AutoRebuild = atobool(val);
1087 		break;
1088 
1089 	  case 'E':		/* error message header/header file */
1090 		if (*val != '\0')
1091 			ErrMsgFile = newstr(val);
1092 		break;
1093 
1094 	  case 'e':		/* set error processing mode */
1095 		switch (*val)
1096 		{
1097 		  case EM_QUIET:	/* be silent about it */
1098 		  case EM_MAIL:		/* mail back */
1099 		  case EM_BERKNET:	/* do berknet error processing */
1100 		  case EM_WRITE:	/* write back (or mail) */
1101 			HoldErrs = TRUE;
1102 			/* fall through... */
1103 
1104 		  case EM_PRINT:	/* print errors normally (default) */
1105 			e->e_errormode = *val;
1106 			break;
1107 		}
1108 		break;
1109 
1110 	  case 'F':		/* file mode */
1111 		FileMode = atooct(val) & 0777;
1112 		break;
1113 
1114 	  case 'f':		/* save Unix-style From lines on front */
1115 		SaveFrom = atobool(val);
1116 		break;
1117 
1118 	  case 'G':		/* match recipients against GECOS field */
1119 		MatchGecos = atobool(val);
1120 		break;
1121 
1122 	  case 'g':		/* default gid */
1123 		DefGid = atoi(val);
1124 		break;
1125 
1126 	  case 'H':		/* help file */
1127 		if (val[0] == '\0')
1128 			HelpFile = "sendmail.hf";
1129 		else
1130 			HelpFile = newstr(val);
1131 		break;
1132 
1133 	  case 'h':		/* maximum hop count */
1134 		MaxHopCount = atoi(val);
1135 		break;
1136 
1137 	  case 'I':		/* use internet domain name server */
1138 #ifdef NAMED_BIND
1139 		UseNameServer = TRUE;
1140 		for (p = val; *p != 0; )
1141 		{
1142 			bool clearmode;
1143 			char *q;
1144 			struct resolverflags *rfp;
1145 
1146 			while (*p == ' ')
1147 				p++;
1148 			if (*p == '\0')
1149 				break;
1150 			clearmode = FALSE;
1151 			if (*p == '-')
1152 				clearmode = TRUE;
1153 			else if (*p != '+')
1154 				p--;
1155 			p++;
1156 			q = p;
1157 			while (*p != '\0' && !(isascii(*p) && isspace(*p)))
1158 				p++;
1159 			if (*p != '\0')
1160 				*p++ = '\0';
1161 			for (rfp = ResolverFlags; rfp->rf_name != NULL; rfp++)
1162 			{
1163 				if (strcasecmp(q, rfp->rf_name) == 0)
1164 					break;
1165 			}
1166 			if (clearmode)
1167 				_res.options &= ~rfp->rf_bits;
1168 			else
1169 				_res.options |= rfp->rf_bits;
1170 		}
1171 		if (tTd(8, 2))
1172 			printf("_res.options = %x\n", _res.options);
1173 #else
1174 		usrerr("name server (I option) specified but BIND not compiled in");
1175 #endif
1176 		break;
1177 
1178 	  case 'i':		/* ignore dot lines in message */
1179 		IgnrDot = atobool(val);
1180 		break;
1181 
1182 	  case 'J':		/* .forward search path */
1183 		ForwardPath = newstr(val);
1184 		break;
1185 
1186 	  case 'k':		/* connection cache size */
1187 		MaxMciCache = atoi(val);
1188 		if (MaxMciCache < 0)
1189 			MaxMciCache = 0;
1190 		break;
1191 
1192 	  case 'K':		/* connection cache timeout */
1193 		MciCacheTimeout = convtime(val, 'm');
1194 		break;
1195 
1196 	  case 'L':		/* log level */
1197 		LogLevel = atoi(val);
1198 		break;
1199 
1200 	  case 'M':		/* define macro */
1201 		define(val[0], newstr(&val[1]), CurEnv);
1202 		sticky = FALSE;
1203 		break;
1204 
1205 	  case 'm':		/* send to me too */
1206 		MeToo = atobool(val);
1207 		break;
1208 
1209 	  case 'n':		/* validate RHS in newaliases */
1210 		CheckAliases = atobool(val);
1211 		break;
1212 
1213 	  case 'O':		/* daemon options */
1214 		setdaemonoptions(val);
1215 		break;
1216 
1217 	  case 'o':		/* assume old style headers */
1218 		if (atobool(val))
1219 			CurEnv->e_flags |= EF_OLDSTYLE;
1220 		else
1221 			CurEnv->e_flags &= ~EF_OLDSTYLE;
1222 		break;
1223 
1224 	  case 'p':		/* select privacy level */
1225 		p = val;
1226 		for (;;)
1227 		{
1228 			register struct prival *pv;
1229 			extern struct prival PrivacyValues[];
1230 
1231 			while (isascii(*p) && (isspace(*p) || ispunct(*p)))
1232 				p++;
1233 			if (*p == '\0')
1234 				break;
1235 			val = p;
1236 			while (isascii(*p) && isalnum(*p))
1237 				p++;
1238 			if (*p != '\0')
1239 				*p++ = '\0';
1240 
1241 			for (pv = PrivacyValues; pv->pv_name != NULL; pv++)
1242 			{
1243 				if (strcasecmp(val, pv->pv_name) == 0)
1244 					break;
1245 			}
1246 			if (pv->pv_name == NULL)
1247 				syserr("readcf: Op line: %s unrecognized", val);
1248 			PrivacyFlags |= pv->pv_flag;
1249 		}
1250 		break;
1251 
1252 	  case 'P':		/* postmaster copy address for returned mail */
1253 		PostMasterCopy = newstr(val);
1254 		break;
1255 
1256 	  case 'q':		/* slope of queue only function */
1257 		QueueFactor = atoi(val);
1258 		break;
1259 
1260 	  case 'Q':		/* queue directory */
1261 		if (val[0] == '\0')
1262 			QueueDir = "mqueue";
1263 		else
1264 			QueueDir = newstr(val);
1265 		if (RealUid != 0 && !safe)
1266 			auth_warning(e, "Processed from queue %s", QueueDir);
1267 		break;
1268 
1269 	  case 'R':		/* don't prune routes */
1270 		DontPruneRoutes = atobool(val);
1271 		break;
1272 
1273 	  case 'r':		/* read timeout */
1274 		settimeouts(val);
1275 		break;
1276 
1277 	  case 'S':		/* status file */
1278 		if (val[0] == '\0')
1279 			StatFile = "sendmail.st";
1280 		else
1281 			StatFile = newstr(val);
1282 		break;
1283 
1284 	  case 's':		/* be super safe, even if expensive */
1285 		SuperSafe = atobool(val);
1286 		break;
1287 
1288 	  case 'T':		/* queue timeout */
1289 		p = strchr(val, '/');
1290 		if (p != NULL)
1291 		{
1292 			*p++ = '\0';
1293 			TimeOuts.to_q_warning = convtime(p, 'd');
1294 		}
1295 		TimeOuts.to_q_return = convtime(val, 'h');
1296 		break;
1297 
1298 	  case 't':		/* time zone name */
1299 		TimeZoneSpec = newstr(val);
1300 		break;
1301 
1302 	  case 'U':		/* location of user database */
1303 		UdbSpec = newstr(val);
1304 		break;
1305 
1306 	  case 'u':		/* set default uid */
1307 		DefUid = atoi(val);
1308 		setdefuser();
1309 		break;
1310 
1311 	  case 'V':		/* fallback MX host */
1312 		FallBackMX = newstr(val);
1313 		break;
1314 
1315 	  case 'v':		/* run in verbose mode */
1316 		Verbose = atobool(val);
1317 		break;
1318 
1319 	  case 'x':		/* load avg at which to auto-queue msgs */
1320 		QueueLA = atoi(val);
1321 		break;
1322 
1323 	  case 'X':		/* load avg at which to auto-reject connections */
1324 		RefuseLA = atoi(val);
1325 		break;
1326 
1327 	  case 'y':		/* work recipient factor */
1328 		WkRecipFact = atoi(val);
1329 		break;
1330 
1331 	  case 'Y':		/* fork jobs during queue runs */
1332 		ForkQueueRuns = atobool(val);
1333 		break;
1334 
1335 	  case 'z':		/* work message class factor */
1336 		WkClassFact = atoi(val);
1337 		break;
1338 
1339 	  case 'Z':		/* work time factor */
1340 		WkTimeFact = atoi(val);
1341 		break;
1342 
1343 	  default:
1344 		break;
1345 	}
1346 	if (sticky)
1347 		setbitn(opt, StickyOpt);
1348 	return;
1349 }
1350 /*
1351 **  SETCLASS -- set a word into a class
1352 **
1353 **	Parameters:
1354 **		class -- the class to put the word in.
1355 **		word -- the word to enter
1356 **
1357 **	Returns:
1358 **		none.
1359 **
1360 **	Side Effects:
1361 **		puts the word into the symbol table.
1362 */
1363 
1364 setclass(class, word)
1365 	int class;
1366 	char *word;
1367 {
1368 	register STAB *s;
1369 
1370 	if (tTd(37, 8))
1371 		printf("%s added to class %c\n", word, class);
1372 	s = stab(word, ST_CLASS, ST_ENTER);
1373 	setbitn(class, s->s_class);
1374 }
1375 /*
1376 **  MAKEMAPENTRY -- create a map entry
1377 **
1378 **	Parameters:
1379 **		line -- the config file line
1380 **
1381 **	Returns:
1382 **		TRUE if it successfully entered the map entry.
1383 **		FALSE otherwise (usually syntax error).
1384 **
1385 **	Side Effects:
1386 **		Enters the map into the dictionary.
1387 */
1388 
1389 void
1390 makemapentry(line)
1391 	char *line;
1392 {
1393 	register char *p;
1394 	char *mapname;
1395 	char *classname;
1396 	register STAB *map;
1397 	STAB *class;
1398 
1399 	for (p = line; isascii(*p) && isspace(*p); p++)
1400 		continue;
1401 	if (!(isascii(*p) && isalnum(*p)))
1402 	{
1403 		syserr("readcf: config K line: no map name");
1404 		return;
1405 	}
1406 
1407 	mapname = p;
1408 	while (isascii(*++p) && isalnum(*p))
1409 		continue;
1410 	if (*p != '\0')
1411 		*p++ = '\0';
1412 	while (isascii(*p) && isspace(*p))
1413 		p++;
1414 	if (!(isascii(*p) && isalnum(*p)))
1415 	{
1416 		syserr("readcf: config K line, map %s: no map class", mapname);
1417 		return;
1418 	}
1419 	classname = p;
1420 	while (isascii(*++p) && isalnum(*p))
1421 		continue;
1422 	if (*p != '\0')
1423 		*p++ = '\0';
1424 	while (isascii(*p) && isspace(*p))
1425 		p++;
1426 
1427 	/* look up the class */
1428 	class = stab(classname, ST_MAPCLASS, ST_FIND);
1429 	if (class == NULL)
1430 	{
1431 		syserr("readcf: map %s: class %s not available", mapname, classname);
1432 		return;
1433 	}
1434 
1435 	/* enter the map */
1436 	map = stab(mapname, ST_MAP, ST_ENTER);
1437 	map->s_map.map_class = &class->s_mapclass;
1438 
1439 	if ((*class->s_mapclass.map_init)(&map->s_map, mapname, p))
1440 		map->s_map.map_flags |= MF_VALID;
1441 }
1442 /*
1443 **  SETTIMEOUTS -- parse and set timeout values
1444 **
1445 **	Parameters:
1446 **		val -- a pointer to the values.  If NULL, do initial
1447 **			settings.
1448 **
1449 **	Returns:
1450 **		none.
1451 **
1452 **	Side Effects:
1453 **		Initializes the TimeOuts structure
1454 */
1455 
1456 #define MINUTES	* 60
1457 #define HOUR	* 3600
1458 
1459 settimeouts(val)
1460 	register char *val;
1461 {
1462 	register char *p;
1463 	extern time_t convtime();
1464 
1465 	if (val == NULL)
1466 	{
1467 		TimeOuts.to_initial = (time_t) 5 MINUTES;
1468 		TimeOuts.to_helo = (time_t) 5 MINUTES;
1469 		TimeOuts.to_mail = (time_t) 10 MINUTES;
1470 		TimeOuts.to_rcpt = (time_t) 1 HOUR;
1471 		TimeOuts.to_datainit = (time_t) 5 MINUTES;
1472 		TimeOuts.to_datablock = (time_t) 1 HOUR;
1473 		TimeOuts.to_datafinal = (time_t) 1 HOUR;
1474 		TimeOuts.to_rset = (time_t) 5 MINUTES;
1475 		TimeOuts.to_quit = (time_t) 2 MINUTES;
1476 		TimeOuts.to_nextcommand = (time_t) 1 HOUR;
1477 		TimeOuts.to_miscshort = (time_t) 2 MINUTES;
1478 		return;
1479 	}
1480 
1481 	for (;; val = p)
1482 	{
1483 		while (isascii(*val) && isspace(*val))
1484 			val++;
1485 		if (*val == '\0')
1486 			break;
1487 		for (p = val; *p != '\0' && *p != ','; p++)
1488 			continue;
1489 		if (*p != '\0')
1490 			*p++ = '\0';
1491 
1492 		if (isascii(*val) && isdigit(*val))
1493 		{
1494 			/* old syntax -- set everything */
1495 			TimeOuts.to_mail = convtime(val, 'm');
1496 			TimeOuts.to_rcpt = TimeOuts.to_mail;
1497 			TimeOuts.to_datainit = TimeOuts.to_mail;
1498 			TimeOuts.to_datablock = TimeOuts.to_mail;
1499 			TimeOuts.to_datafinal = TimeOuts.to_mail;
1500 			TimeOuts.to_nextcommand = TimeOuts.to_mail;
1501 			continue;
1502 		}
1503 		else
1504 		{
1505 			register char *q = strchr(val, '=');
1506 			time_t to;
1507 
1508 			if (q == NULL)
1509 			{
1510 				/* syntax error */
1511 				continue;
1512 			}
1513 			*q++ = '\0';
1514 			to = convtime(q, 'm');
1515 
1516 			if (strcasecmp(val, "initial") == 0)
1517 				TimeOuts.to_initial = to;
1518 			else if (strcasecmp(val, "mail") == 0)
1519 				TimeOuts.to_mail = to;
1520 			else if (strcasecmp(val, "rcpt") == 0)
1521 				TimeOuts.to_rcpt = to;
1522 			else if (strcasecmp(val, "datainit") == 0)
1523 				TimeOuts.to_datainit = to;
1524 			else if (strcasecmp(val, "datablock") == 0)
1525 				TimeOuts.to_datablock = to;
1526 			else if (strcasecmp(val, "datafinal") == 0)
1527 				TimeOuts.to_datafinal = to;
1528 			else if (strcasecmp(val, "command") == 0)
1529 				TimeOuts.to_nextcommand = to;
1530 			else if (strcasecmp(val, "rset") == 0)
1531 				TimeOuts.to_rset = to;
1532 			else if (strcasecmp(val, "helo") == 0)
1533 				TimeOuts.to_helo = to;
1534 			else if (strcasecmp(val, "quit") == 0)
1535 				TimeOuts.to_quit = to;
1536 			else if (strcasecmp(val, "misc") == 0)
1537 				TimeOuts.to_miscshort = to;
1538 			else
1539 				syserr("settimeouts: invalid timeout %s", val);
1540 		}
1541 	}
1542 }
1543