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