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