xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 59272)
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.28 (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':		/* minimum number of blocks free on queue fs */
1044 		MinBlocksFree = atol(val);
1045 		break;
1046 
1047 	  case 'c':		/* don't connect to "expensive" mailers */
1048 		NoConnect = atobool(val);
1049 		break;
1050 
1051 	  case 'C':		/* checkpoint every N addresses */
1052 		CheckpointInterval = atoi(val);
1053 		break;
1054 
1055 	  case 'd':		/* delivery mode */
1056 		switch (*val)
1057 		{
1058 		  case '\0':
1059 			e->e_sendmode = SM_DELIVER;
1060 			break;
1061 
1062 		  case SM_QUEUE:	/* queue only */
1063 #ifndef QUEUE
1064 			syserr("need QUEUE to set -odqueue");
1065 #endif /* QUEUE */
1066 			/* fall through..... */
1067 
1068 		  case SM_DELIVER:	/* do everything */
1069 		  case SM_FORK:		/* fork after verification */
1070 			e->e_sendmode = *val;
1071 			break;
1072 
1073 		  default:
1074 			syserr("Unknown delivery mode %c", *val);
1075 			exit(EX_USAGE);
1076 		}
1077 		break;
1078 
1079 	  case 'D':		/* rebuild alias database as needed */
1080 		AutoRebuild = atobool(val);
1081 		break;
1082 
1083 	  case 'E':		/* error message header/header file */
1084 		if (*val != '\0')
1085 			ErrMsgFile = newstr(val);
1086 		break;
1087 
1088 	  case 'e':		/* set error processing mode */
1089 		switch (*val)
1090 		{
1091 		  case EM_QUIET:	/* be silent about it */
1092 		  case EM_MAIL:		/* mail back */
1093 		  case EM_BERKNET:	/* do berknet error processing */
1094 		  case EM_WRITE:	/* write back (or mail) */
1095 			HoldErrs = TRUE;
1096 			/* fall through... */
1097 
1098 		  case EM_PRINT:	/* print errors normally (default) */
1099 			e->e_errormode = *val;
1100 			break;
1101 		}
1102 		break;
1103 
1104 	  case 'F':		/* file mode */
1105 		FileMode = atooct(val) & 0777;
1106 		break;
1107 
1108 	  case 'f':		/* save Unix-style From lines on front */
1109 		SaveFrom = atobool(val);
1110 		break;
1111 
1112 	  case 'G':		/* match recipients against GECOS field */
1113 		MatchGecos = atobool(val);
1114 		break;
1115 
1116 	  case 'g':		/* default gid */
1117 		DefGid = atoi(val);
1118 		break;
1119 
1120 	  case 'H':		/* help file */
1121 		if (val[0] == '\0')
1122 			HelpFile = "sendmail.hf";
1123 		else
1124 			HelpFile = newstr(val);
1125 		break;
1126 
1127 	  case 'h':		/* maximum hop count */
1128 		MaxHopCount = atoi(val);
1129 		break;
1130 
1131 	  case 'I':		/* use internet domain name server */
1132 #ifdef NAMED_BIND
1133 		UseNameServer = TRUE;
1134 		for (p = val; *p != 0; )
1135 		{
1136 			bool clearmode;
1137 			char *q;
1138 			struct resolverflags *rfp;
1139 
1140 			while (*p == ' ')
1141 				p++;
1142 			if (*p == '\0')
1143 				break;
1144 			clearmode = FALSE;
1145 			if (*p == '-')
1146 				clearmode = TRUE;
1147 			else if (*p != '+')
1148 				p--;
1149 			p++;
1150 			q = p;
1151 			while (*p != '\0' && !(isascii(*p) && isspace(*p)))
1152 				p++;
1153 			if (*p != '\0')
1154 				*p++ = '\0';
1155 			for (rfp = ResolverFlags; rfp->rf_name != NULL; rfp++)
1156 			{
1157 				if (strcasecmp(q, rfp->rf_name) == 0)
1158 					break;
1159 			}
1160 			if (clearmode)
1161 				_res.options &= ~rfp->rf_bits;
1162 			else
1163 				_res.options |= rfp->rf_bits;
1164 		}
1165 		if (tTd(8, 2))
1166 			printf("_res.options = %x\n", _res.options);
1167 #else
1168 		usrerr("name server (I option) specified but BIND not compiled in");
1169 #endif
1170 		break;
1171 
1172 	  case 'i':		/* ignore dot lines in message */
1173 		IgnrDot = atobool(val);
1174 		break;
1175 
1176 	  case 'J':		/* .forward search path */
1177 		ForwardPath = newstr(val);
1178 		break;
1179 
1180 	  case 'k':		/* connection cache size */
1181 		MaxMciCache = atoi(val);
1182 		if (MaxMciCache < 0)
1183 			MaxMciCache = 0;
1184 		break;
1185 
1186 	  case 'K':		/* connection cache timeout */
1187 		MciCacheTimeout = convtime(val, 'm');
1188 		break;
1189 
1190 	  case 'L':		/* log level */
1191 		LogLevel = atoi(val);
1192 		break;
1193 
1194 	  case 'M':		/* define macro */
1195 		define(val[0], newstr(&val[1]), CurEnv);
1196 		sticky = FALSE;
1197 		break;
1198 
1199 	  case 'm':		/* send to me too */
1200 		MeToo = atobool(val);
1201 		break;
1202 
1203 	  case 'n':		/* validate RHS in newaliases */
1204 		CheckAliases = atobool(val);
1205 		break;
1206 
1207 	  case 'O':		/* daemon options */
1208 		setdaemonoptions(val);
1209 		break;
1210 
1211 	  case 'o':		/* assume old style headers */
1212 		if (atobool(val))
1213 			CurEnv->e_flags |= EF_OLDSTYLE;
1214 		else
1215 			CurEnv->e_flags &= ~EF_OLDSTYLE;
1216 		break;
1217 
1218 	  case 'p':		/* select privacy level */
1219 		p = val;
1220 		for (;;)
1221 		{
1222 			register struct prival *pv;
1223 			extern struct prival PrivacyValues[];
1224 
1225 			while (isascii(*p) && (isspace(*p) || ispunct(*p)))
1226 				p++;
1227 			if (*p == '\0')
1228 				break;
1229 			val = p;
1230 			while (isascii(*p) && isalnum(*p))
1231 				p++;
1232 			if (*p != '\0')
1233 				*p++ = '\0';
1234 
1235 			for (pv = PrivacyValues; pv->pv_name != NULL; pv++)
1236 			{
1237 				if (strcasecmp(val, pv->pv_name) == 0)
1238 					break;
1239 			}
1240 			if (pv->pv_name == NULL)
1241 				syserr("readcf: Op line: %s unrecognized", val);
1242 			PrivacyFlags |= pv->pv_flag;
1243 		}
1244 		break;
1245 
1246 	  case 'P':		/* postmaster copy address for returned mail */
1247 		PostMasterCopy = newstr(val);
1248 		break;
1249 
1250 	  case 'q':		/* slope of queue only function */
1251 		QueueFactor = atoi(val);
1252 		break;
1253 
1254 	  case 'Q':		/* queue directory */
1255 		if (val[0] == '\0')
1256 			QueueDir = "mqueue";
1257 		else
1258 			QueueDir = newstr(val);
1259 		if (RealUid != 0 && !safe)
1260 			auth_warning(e, "Processed from queue %s", QueueDir);
1261 		break;
1262 
1263 	  case 'R':		/* don't prune routes */
1264 		DontPruneRoutes = atobool(val);
1265 		break;
1266 
1267 	  case 'r':		/* read timeout */
1268 		settimeouts(val);
1269 		break;
1270 
1271 	  case 'S':		/* status file */
1272 		if (val[0] == '\0')
1273 			StatFile = "sendmail.st";
1274 		else
1275 			StatFile = newstr(val);
1276 		break;
1277 
1278 	  case 's':		/* be super safe, even if expensive */
1279 		SuperSafe = atobool(val);
1280 		break;
1281 
1282 	  case 'T':		/* queue timeout */
1283 		p = strchr(val, '/');
1284 		if (p != NULL)
1285 		{
1286 			*p++ = '\0';
1287 			TimeOuts.to_q_warning = convtime(p, 'd');
1288 		}
1289 		TimeOuts.to_q_return = convtime(val, 'h');
1290 		break;
1291 
1292 	  case 't':		/* time zone name */
1293 		TimeZoneSpec = newstr(val);
1294 		break;
1295 
1296 	  case 'U':		/* location of user database */
1297 		UdbSpec = newstr(val);
1298 		break;
1299 
1300 	  case 'u':		/* set default uid */
1301 		DefUid = atoi(val);
1302 		setdefuser();
1303 		break;
1304 
1305 	  case 'V':		/* fallback MX host */
1306 		FallBackMX = newstr(val);
1307 		break;
1308 
1309 	  case 'v':		/* run in verbose mode */
1310 		Verbose = atobool(val);
1311 		break;
1312 
1313 	  case 'x':		/* load avg at which to auto-queue msgs */
1314 		QueueLA = atoi(val);
1315 		break;
1316 
1317 	  case 'X':		/* load avg at which to auto-reject connections */
1318 		RefuseLA = atoi(val);
1319 		break;
1320 
1321 	  case 'y':		/* work recipient factor */
1322 		WkRecipFact = atoi(val);
1323 		break;
1324 
1325 	  case 'Y':		/* fork jobs during queue runs */
1326 		ForkQueueRuns = atobool(val);
1327 		break;
1328 
1329 	  case 'z':		/* work message class factor */
1330 		WkClassFact = atoi(val);
1331 		break;
1332 
1333 	  case 'Z':		/* work time factor */
1334 		WkTimeFact = atoi(val);
1335 		break;
1336 
1337 	  default:
1338 		break;
1339 	}
1340 	if (sticky)
1341 		setbitn(opt, StickyOpt);
1342 	return;
1343 }
1344 /*
1345 **  SETCLASS -- set a word into a class
1346 **
1347 **	Parameters:
1348 **		class -- the class to put the word in.
1349 **		word -- the word to enter
1350 **
1351 **	Returns:
1352 **		none.
1353 **
1354 **	Side Effects:
1355 **		puts the word into the symbol table.
1356 */
1357 
1358 setclass(class, word)
1359 	int class;
1360 	char *word;
1361 {
1362 	register STAB *s;
1363 
1364 	if (tTd(37, 8))
1365 		printf("%s added to class %c\n", word, class);
1366 	s = stab(word, ST_CLASS, ST_ENTER);
1367 	setbitn(class, s->s_class);
1368 }
1369 /*
1370 **  MAKEMAPENTRY -- create a map entry
1371 **
1372 **	Parameters:
1373 **		line -- the config file line
1374 **
1375 **	Returns:
1376 **		TRUE if it successfully entered the map entry.
1377 **		FALSE otherwise (usually syntax error).
1378 **
1379 **	Side Effects:
1380 **		Enters the map into the dictionary.
1381 */
1382 
1383 void
1384 makemapentry(line)
1385 	char *line;
1386 {
1387 	register char *p;
1388 	char *mapname;
1389 	char *classname;
1390 	register STAB *map;
1391 	STAB *class;
1392 
1393 	for (p = line; isascii(*p) && isspace(*p); p++)
1394 		continue;
1395 	if (!(isascii(*p) && isalnum(*p)))
1396 	{
1397 		syserr("readcf: config K line: no map name");
1398 		return;
1399 	}
1400 
1401 	mapname = p;
1402 	while (isascii(*++p) && isalnum(*p))
1403 		continue;
1404 	if (*p != '\0')
1405 		*p++ = '\0';
1406 	while (isascii(*p) && isspace(*p))
1407 		p++;
1408 	if (!(isascii(*p) && isalnum(*p)))
1409 	{
1410 		syserr("readcf: config K line, map %s: no map class", mapname);
1411 		return;
1412 	}
1413 	classname = p;
1414 	while (isascii(*++p) && isalnum(*p))
1415 		continue;
1416 	if (*p != '\0')
1417 		*p++ = '\0';
1418 	while (isascii(*p) && isspace(*p))
1419 		p++;
1420 
1421 	/* look up the class */
1422 	class = stab(classname, ST_MAPCLASS, ST_FIND);
1423 	if (class == NULL)
1424 	{
1425 		syserr("readcf: map %s: class %s not available", mapname, classname);
1426 		return;
1427 	}
1428 
1429 	/* enter the map */
1430 	map = stab(mapname, ST_MAP, ST_ENTER);
1431 	map->s_map.map_class = &class->s_mapclass;
1432 
1433 	if ((*class->s_mapclass.map_init)(&map->s_map, mapname, p))
1434 		map->s_map.map_flags |= MF_VALID;
1435 }
1436 /*
1437 **  SETTIMEOUTS -- parse and set timeout values
1438 **
1439 **	Parameters:
1440 **		val -- a pointer to the values.  If NULL, do initial
1441 **			settings.
1442 **
1443 **	Returns:
1444 **		none.
1445 **
1446 **	Side Effects:
1447 **		Initializes the TimeOuts structure
1448 */
1449 
1450 #define MINUTES	* 60
1451 #define HOUR	* 3600
1452 
1453 settimeouts(val)
1454 	register char *val;
1455 {
1456 	register char *p;
1457 	extern time_t convtime();
1458 
1459 	if (val == NULL)
1460 	{
1461 		TimeOuts.to_initial = (time_t) 5 MINUTES;
1462 		TimeOuts.to_helo = (time_t) 5 MINUTES;
1463 		TimeOuts.to_mail = (time_t) 10 MINUTES;
1464 		TimeOuts.to_rcpt = (time_t) 1 HOUR;
1465 		TimeOuts.to_datainit = (time_t) 5 MINUTES;
1466 		TimeOuts.to_datablock = (time_t) 1 HOUR;
1467 		TimeOuts.to_datafinal = (time_t) 1 HOUR;
1468 		TimeOuts.to_rset = (time_t) 5 MINUTES;
1469 		TimeOuts.to_quit = (time_t) 2 MINUTES;
1470 		TimeOuts.to_nextcommand = (time_t) 1 HOUR;
1471 		TimeOuts.to_miscshort = (time_t) 2 MINUTES;
1472 		return;
1473 	}
1474 
1475 	for (;; val = p)
1476 	{
1477 		while (isascii(*val) && isspace(*val))
1478 			val++;
1479 		if (*val == '\0')
1480 			break;
1481 		for (p = val; *p != '\0' && *p != ','; p++)
1482 			continue;
1483 		if (*p != '\0')
1484 			*p++ = '\0';
1485 
1486 		if (isascii(*val) && isdigit(*val))
1487 		{
1488 			/* old syntax -- set everything */
1489 			TimeOuts.to_mail = convtime(val, 'm');
1490 			TimeOuts.to_rcpt = TimeOuts.to_mail;
1491 			TimeOuts.to_datainit = TimeOuts.to_mail;
1492 			TimeOuts.to_datablock = TimeOuts.to_mail;
1493 			TimeOuts.to_datafinal = TimeOuts.to_mail;
1494 			TimeOuts.to_nextcommand = TimeOuts.to_mail;
1495 			continue;
1496 		}
1497 		else
1498 		{
1499 			register char *q = strchr(val, '=');
1500 			time_t to;
1501 
1502 			if (q == NULL)
1503 			{
1504 				/* syntax error */
1505 				continue;
1506 			}
1507 			*q++ = '\0';
1508 			to = convtime(q, 'm');
1509 
1510 			if (strcasecmp(val, "initial") == 0)
1511 				TimeOuts.to_initial = to;
1512 			else if (strcasecmp(val, "mail") == 0)
1513 				TimeOuts.to_mail = to;
1514 			else if (strcasecmp(val, "rcpt") == 0)
1515 				TimeOuts.to_rcpt = to;
1516 			else if (strcasecmp(val, "datainit") == 0)
1517 				TimeOuts.to_datainit = to;
1518 			else if (strcasecmp(val, "datablock") == 0)
1519 				TimeOuts.to_datablock = to;
1520 			else if (strcasecmp(val, "datafinal") == 0)
1521 				TimeOuts.to_datafinal = to;
1522 			else if (strcasecmp(val, "command") == 0)
1523 				TimeOuts.to_nextcommand = to;
1524 			else if (strcasecmp(val, "rset") == 0)
1525 				TimeOuts.to_rset = to;
1526 			else if (strcasecmp(val, "helo") == 0)
1527 				TimeOuts.to_helo = to;
1528 			else if (strcasecmp(val, "quit") == 0)
1529 				TimeOuts.to_quit = to;
1530 			else if (strcasecmp(val, "misc") == 0)
1531 				TimeOuts.to_miscshort = to;
1532 			else
1533 				syserr("settimeouts: invalid timeout %s", val);
1534 		}
1535 	}
1536 }
1537