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