xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 64279)
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.10 (Berkeley) 08/16/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 			if (ConfigLevel >= 5)
460 			{
461 				/* level 5 configs have short name in $w */
462 				p = macvalue('w', e);
463 				if (p != NULL && (p = strchr(p, '.')) != NULL)
464 					*p = '\0';
465 			}
466 			break;
467 
468 		  case 'K':
469 			makemapentry(&bp[1]);
470 			break;
471 
472 		  default:
473 		  badline:
474 			syserr("unknown control line \"%s\"", bp);
475 		}
476 		if (bp != buf)
477 			free(bp);
478 	}
479 	if (ferror(cf))
480 	{
481 		syserr("I/O read error", cfname);
482 		exit(EX_OSFILE);
483 	}
484 	fclose(cf);
485 	FileName = NULL;
486 
487 	if (stab("host", ST_MAP, ST_FIND) == NULL)
488 	{
489 		/* user didn't initialize: set up host map */
490 		strcpy(buf, "host host");
491 		if (ConfigLevel >= 2)
492 			strcat(buf, " -a.");
493 		makemapentry(buf);
494 	}
495 }
496 /*
497 **  TOOMANY -- signal too many of some option
498 **
499 **	Parameters:
500 **		id -- the id of the error line
501 **		maxcnt -- the maximum possible values
502 **
503 **	Returns:
504 **		none.
505 **
506 **	Side Effects:
507 **		gives a syserr.
508 */
509 
510 toomany(id, maxcnt)
511 	char id;
512 	int maxcnt;
513 {
514 	syserr("too many %c lines, %d max", id, maxcnt);
515 }
516 /*
517 **  FILECLASS -- read members of a class from a file
518 **
519 **	Parameters:
520 **		class -- class to define.
521 **		filename -- name of file to read.
522 **		fmt -- scanf string to use for match.
523 **		safe -- if set, this is a safe read.
524 **		optional -- if set, it is not an error for the file to
525 **			not exist.
526 **
527 **	Returns:
528 **		none
529 **
530 **	Side Effects:
531 **
532 **		puts all lines in filename that match a scanf into
533 **			the named class.
534 */
535 
536 fileclass(class, filename, fmt, safe, optional)
537 	int class;
538 	char *filename;
539 	char *fmt;
540 	bool safe;
541 	bool optional;
542 {
543 	FILE *f;
544 	struct stat stbuf;
545 	char buf[MAXLINE];
546 
547 	if (stat(filename, &stbuf) < 0)
548 	{
549 		if (!optional)
550 			syserr("fileclass: cannot stat %s", filename);
551 		return;
552 	}
553 	if (!S_ISREG(stbuf.st_mode))
554 	{
555 		syserr("fileclass: %s not a regular file", filename);
556 		return;
557 	}
558 	if (!safe && access(filename, R_OK) < 0)
559 	{
560 		syserr("fileclass: access denied on %s", filename);
561 		return;
562 	}
563 	f = fopen(filename, "r");
564 	if (f == NULL)
565 	{
566 		syserr("fileclass: cannot open %s", filename);
567 		return;
568 	}
569 
570 	while (fgets(buf, sizeof buf, f) != NULL)
571 	{
572 		register STAB *s;
573 		register char *p;
574 # ifdef SCANF
575 		char wordbuf[MAXNAME+1];
576 
577 		if (sscanf(buf, fmt, wordbuf) != 1)
578 			continue;
579 		p = wordbuf;
580 # else /* SCANF */
581 		p = buf;
582 # endif /* SCANF */
583 
584 		/*
585 		**  Break up the match into words.
586 		*/
587 
588 		while (*p != '\0')
589 		{
590 			register char *q;
591 
592 			/* strip leading spaces */
593 			while (isascii(*p) && isspace(*p))
594 				p++;
595 			if (*p == '\0')
596 				break;
597 
598 			/* find the end of the word */
599 			q = p;
600 			while (*p != '\0' && !(isascii(*p) && isspace(*p)))
601 				p++;
602 			if (*p != '\0')
603 				*p++ = '\0';
604 
605 			/* enter the word in the symbol table */
606 			s = stab(q, ST_CLASS, ST_ENTER);
607 			setbitn(class, s->s_class);
608 		}
609 	}
610 
611 	(void) fclose(f);
612 }
613 /*
614 **  MAKEMAILER -- define a new mailer.
615 **
616 **	Parameters:
617 **		line -- description of mailer.  This is in labeled
618 **			fields.  The fields are:
619 **			   P -- the path to the mailer
620 **			   F -- the flags associated with the mailer
621 **			   A -- the argv for this mailer
622 **			   S -- the sender rewriting set
623 **			   R -- the recipient rewriting set
624 **			   E -- the eol string
625 **			The first word is the canonical name of the mailer.
626 **
627 **	Returns:
628 **		none.
629 **
630 **	Side Effects:
631 **		enters the mailer into the mailer table.
632 */
633 
634 makemailer(line)
635 	char *line;
636 {
637 	register char *p;
638 	register struct mailer *m;
639 	register STAB *s;
640 	int i;
641 	char fcode;
642 	auto char *endp;
643 	extern int NextMailer;
644 	extern char **makeargv();
645 	extern char *munchstring();
646 	extern long atol();
647 
648 	/* allocate a mailer and set up defaults */
649 	m = (struct mailer *) xalloc(sizeof *m);
650 	bzero((char *) m, sizeof *m);
651 	m->m_eol = "\n";
652 
653 	/* collect the mailer name */
654 	for (p = line; *p != '\0' && *p != ',' && !(isascii(*p) && isspace(*p)); p++)
655 		continue;
656 	if (*p != '\0')
657 		*p++ = '\0';
658 	m->m_name = newstr(line);
659 
660 	/* now scan through and assign info from the fields */
661 	while (*p != '\0')
662 	{
663 		auto char *delimptr;
664 
665 		while (*p != '\0' && (*p == ',' || (isascii(*p) && isspace(*p))))
666 			p++;
667 
668 		/* p now points to field code */
669 		fcode = *p;
670 		while (*p != '\0' && *p != '=' && *p != ',')
671 			p++;
672 		if (*p++ != '=')
673 		{
674 			syserr("mailer %s: `=' expected", m->m_name);
675 			return;
676 		}
677 		while (isascii(*p) && isspace(*p))
678 			p++;
679 
680 		/* p now points to the field body */
681 		p = munchstring(p, &delimptr);
682 
683 		/* install the field into the mailer struct */
684 		switch (fcode)
685 		{
686 		  case 'P':		/* pathname */
687 			m->m_mailer = newstr(p);
688 			break;
689 
690 		  case 'F':		/* flags */
691 			for (; *p != '\0'; p++)
692 				if (!(isascii(*p) && isspace(*p)))
693 					setbitn(*p, m->m_flags);
694 			break;
695 
696 		  case 'S':		/* sender rewriting ruleset */
697 		  case 'R':		/* recipient rewriting ruleset */
698 			i = strtol(p, &endp, 10);
699 			if (i < 0 || i >= MAXRWSETS)
700 			{
701 				syserr("invalid rewrite set, %d max", MAXRWSETS);
702 				return;
703 			}
704 			if (fcode == 'S')
705 				m->m_sh_rwset = m->m_se_rwset = i;
706 			else
707 				m->m_rh_rwset = m->m_re_rwset = i;
708 
709 			p = endp;
710 			if (*p++ == '/')
711 			{
712 				i = strtol(p, NULL, 10);
713 				if (i < 0 || i >= MAXRWSETS)
714 				{
715 					syserr("invalid rewrite set, %d max",
716 						MAXRWSETS);
717 					return;
718 				}
719 				if (fcode == 'S')
720 					m->m_sh_rwset = i;
721 				else
722 					m->m_rh_rwset = i;
723 			}
724 			break;
725 
726 		  case 'E':		/* end of line string */
727 			m->m_eol = newstr(p);
728 			break;
729 
730 		  case 'A':		/* argument vector */
731 			m->m_argv = makeargv(p);
732 			break;
733 
734 		  case 'M':		/* maximum message size */
735 			m->m_maxsize = atol(p);
736 			break;
737 
738 		  case 'L':		/* maximum line length */
739 			m->m_linelimit = atoi(p);
740 			break;
741 
742 		  case 'D':		/* working directory */
743 			m->m_execdir = newstr(p);
744 			break;
745 		}
746 
747 		p = delimptr;
748 	}
749 
750 	/* do some heuristic cleanup for back compatibility */
751 	if (bitnset(M_LIMITS, m->m_flags))
752 	{
753 		if (m->m_linelimit == 0)
754 			m->m_linelimit = SMTPLINELIM;
755 		if (ConfigLevel < 2)
756 			setbitn(M_7BITS, m->m_flags);
757 	}
758 
759 	/* do some rationality checking */
760 	if (m->m_argv == NULL)
761 	{
762 		syserr("M%s: A= argument required", m->m_name);
763 		return;
764 	}
765 	if (m->m_mailer == NULL)
766 	{
767 		syserr("M%s: P= argument required", m->m_name);
768 		return;
769 	}
770 
771 	if (NextMailer >= MAXMAILERS)
772 	{
773 		syserr("too many mailers defined (%d max)", MAXMAILERS);
774 		return;
775 	}
776 
777 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
778 	if (s->s_mailer != NULL)
779 	{
780 		i = s->s_mailer->m_mno;
781 		free(s->s_mailer);
782 	}
783 	else
784 	{
785 		i = NextMailer++;
786 	}
787 	Mailer[i] = s->s_mailer = m;
788 	m->m_mno = i;
789 }
790 /*
791 **  MUNCHSTRING -- translate a string into internal form.
792 **
793 **	Parameters:
794 **		p -- the string to munch.
795 **		delimptr -- if non-NULL, set to the pointer of the
796 **			field delimiter character.
797 **
798 **	Returns:
799 **		the munched string.
800 */
801 
802 char *
803 munchstring(p, delimptr)
804 	register char *p;
805 	char **delimptr;
806 {
807 	register char *q;
808 	bool backslash = FALSE;
809 	bool quotemode = FALSE;
810 	static char buf[MAXLINE];
811 
812 	for (q = buf; *p != '\0'; p++)
813 	{
814 		if (backslash)
815 		{
816 			/* everything is roughly literal */
817 			backslash = FALSE;
818 			switch (*p)
819 			{
820 			  case 'r':		/* carriage return */
821 				*q++ = '\r';
822 				continue;
823 
824 			  case 'n':		/* newline */
825 				*q++ = '\n';
826 				continue;
827 
828 			  case 'f':		/* form feed */
829 				*q++ = '\f';
830 				continue;
831 
832 			  case 'b':		/* backspace */
833 				*q++ = '\b';
834 				continue;
835 			}
836 			*q++ = *p;
837 		}
838 		else
839 		{
840 			if (*p == '\\')
841 				backslash = TRUE;
842 			else if (*p == '"')
843 				quotemode = !quotemode;
844 			else if (quotemode || *p != ',')
845 				*q++ = *p;
846 			else
847 				break;
848 		}
849 	}
850 
851 	if (delimptr != NULL)
852 		*delimptr = p;
853 	*q++ = '\0';
854 	return (buf);
855 }
856 /*
857 **  MAKEARGV -- break up a string into words
858 **
859 **	Parameters:
860 **		p -- the string to break up.
861 **
862 **	Returns:
863 **		a char **argv (dynamically allocated)
864 **
865 **	Side Effects:
866 **		munges p.
867 */
868 
869 char **
870 makeargv(p)
871 	register char *p;
872 {
873 	char *q;
874 	int i;
875 	char **avp;
876 	char *argv[MAXPV + 1];
877 
878 	/* take apart the words */
879 	i = 0;
880 	while (*p != '\0' && i < MAXPV)
881 	{
882 		q = p;
883 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
884 			p++;
885 		while (isascii(*p) && isspace(*p))
886 			*p++ = '\0';
887 		argv[i++] = newstr(q);
888 	}
889 	argv[i++] = NULL;
890 
891 	/* now make a copy of the argv */
892 	avp = (char **) xalloc(sizeof *avp * i);
893 	bcopy((char *) argv, (char *) avp, sizeof *avp * i);
894 
895 	return (avp);
896 }
897 /*
898 **  PRINTRULES -- print rewrite rules (for debugging)
899 **
900 **	Parameters:
901 **		none.
902 **
903 **	Returns:
904 **		none.
905 **
906 **	Side Effects:
907 **		prints rewrite rules.
908 */
909 
910 printrules()
911 {
912 	register struct rewrite *rwp;
913 	register int ruleset;
914 
915 	for (ruleset = 0; ruleset < 10; ruleset++)
916 	{
917 		if (RewriteRules[ruleset] == NULL)
918 			continue;
919 		printf("\n----Rule Set %d:", ruleset);
920 
921 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
922 		{
923 			printf("\nLHS:");
924 			printav(rwp->r_lhs);
925 			printf("RHS:");
926 			printav(rwp->r_rhs);
927 		}
928 	}
929 }
930 
931 /*
932 **  SETOPTION -- set global processing option
933 **
934 **	Parameters:
935 **		opt -- option name.
936 **		val -- option value (as a text string).
937 **		safe -- set if this came from a configuration file.
938 **			Some options (if set from the command line) will
939 **			reset the user id to avoid security problems.
940 **		sticky -- if set, don't let other setoptions override
941 **			this value.
942 **		e -- the main envelope.
943 **
944 **	Returns:
945 **		none.
946 **
947 **	Side Effects:
948 **		Sets options as implied by the arguments.
949 */
950 
951 static BITMAP	StickyOpt;		/* set if option is stuck */
952 
953 
954 #ifdef NAMED_BIND
955 
956 struct resolverflags
957 {
958 	char	*rf_name;	/* name of the flag */
959 	long	rf_bits;	/* bits to set/clear */
960 } ResolverFlags[] =
961 {
962 	"debug",	RES_DEBUG,
963 	"aaonly",	RES_AAONLY,
964 	"usevc",	RES_USEVC,
965 	"primary",	RES_PRIMARY,
966 	"igntc",	RES_IGNTC,
967 	"recurse",	RES_RECURSE,
968 	"defnames",	RES_DEFNAMES,
969 	"stayopen",	RES_STAYOPEN,
970 	"dnsrch",	RES_DNSRCH,
971 	NULL,		0
972 };
973 
974 #endif
975 
976 setoption(opt, val, safe, sticky, e)
977 	char opt;
978 	char *val;
979 	bool safe;
980 	bool sticky;
981 	register ENVELOPE *e;
982 {
983 	register char *p;
984 	extern bool atobool();
985 	extern time_t convtime();
986 	extern int QueueLA;
987 	extern int RefuseLA;
988 	extern bool trusteduser();
989 
990 	if (tTd(37, 1))
991 		printf("setoption %c=%s", opt, val);
992 
993 	/*
994 	**  See if this option is preset for us.
995 	*/
996 
997 	if (!sticky && bitnset(opt, StickyOpt))
998 	{
999 		if (tTd(37, 1))
1000 			printf(" (ignored)\n");
1001 		return;
1002 	}
1003 
1004 	/*
1005 	**  Check to see if this option can be specified by this user.
1006 	*/
1007 
1008 	if (!safe && RealUid == 0)
1009 		safe = TRUE;
1010 	if (!safe && strchr("bCdeEijLmoprsvw7", opt) == NULL)
1011 	{
1012 		if (opt != 'M' || (val[0] != 'r' && val[0] != 's'))
1013 		{
1014 			if (tTd(37, 1))
1015 				printf(" (unsafe)");
1016 			if (RealUid != geteuid())
1017 			{
1018 				if (tTd(37, 1))
1019 					printf("(Resetting uid)");
1020 				(void) setgid(RealGid);
1021 				(void) setuid(RealUid);
1022 			}
1023 		}
1024 	}
1025 	if (tTd(37, 1))
1026 		printf("\n");
1027 
1028 	switch (opt)
1029 	{
1030 	  case '7':		/* force seven-bit input */
1031 		SevenBit = atobool(val);
1032 		break;
1033 
1034 	  case 'A':		/* set default alias file */
1035 		if (val[0] == '\0')
1036 			setalias("aliases");
1037 		else
1038 			setalias(val);
1039 		break;
1040 
1041 	  case 'a':		/* look N minutes for "@:@" in alias file */
1042 		if (val[0] == '\0')
1043 			SafeAlias = 5;
1044 		else
1045 			SafeAlias = atoi(val);
1046 		break;
1047 
1048 	  case 'B':		/* substitution for blank character */
1049 		SpaceSub = val[0];
1050 		if (SpaceSub == '\0')
1051 			SpaceSub = ' ';
1052 		break;
1053 
1054 	  case 'b':		/* min blocks free on queue fs/max msg size */
1055 		p = strchr(val, '/');
1056 		if (p != NULL)
1057 		{
1058 			*p++ = '\0';
1059 			MaxMessageSize = atol(p);
1060 		}
1061 		MinBlocksFree = atol(val);
1062 		break;
1063 
1064 	  case 'c':		/* don't connect to "expensive" mailers */
1065 		NoConnect = atobool(val);
1066 		break;
1067 
1068 	  case 'C':		/* checkpoint every N addresses */
1069 		CheckpointInterval = atoi(val);
1070 		break;
1071 
1072 	  case 'd':		/* delivery mode */
1073 		switch (*val)
1074 		{
1075 		  case '\0':
1076 			e->e_sendmode = SM_DELIVER;
1077 			break;
1078 
1079 		  case SM_QUEUE:	/* queue only */
1080 #ifndef QUEUE
1081 			syserr("need QUEUE to set -odqueue");
1082 #endif /* QUEUE */
1083 			/* fall through..... */
1084 
1085 		  case SM_DELIVER:	/* do everything */
1086 		  case SM_FORK:		/* fork after verification */
1087 			e->e_sendmode = *val;
1088 			break;
1089 
1090 		  default:
1091 			syserr("Unknown delivery mode %c", *val);
1092 			exit(EX_USAGE);
1093 		}
1094 		break;
1095 
1096 	  case 'D':		/* rebuild alias database as needed */
1097 		AutoRebuild = atobool(val);
1098 		break;
1099 
1100 	  case 'E':		/* error message header/header file */
1101 		if (*val != '\0')
1102 			ErrMsgFile = newstr(val);
1103 		break;
1104 
1105 	  case 'e':		/* set error processing mode */
1106 		switch (*val)
1107 		{
1108 		  case EM_QUIET:	/* be silent about it */
1109 		  case EM_MAIL:		/* mail back */
1110 		  case EM_BERKNET:	/* do berknet error processing */
1111 		  case EM_WRITE:	/* write back (or mail) */
1112 			HoldErrs = TRUE;
1113 			/* fall through... */
1114 
1115 		  case EM_PRINT:	/* print errors normally (default) */
1116 			e->e_errormode = *val;
1117 			break;
1118 		}
1119 		break;
1120 
1121 	  case 'F':		/* file mode */
1122 		FileMode = atooct(val) & 0777;
1123 		break;
1124 
1125 	  case 'f':		/* save Unix-style From lines on front */
1126 		SaveFrom = atobool(val);
1127 		break;
1128 
1129 	  case 'G':		/* match recipients against GECOS field */
1130 		MatchGecos = atobool(val);
1131 		break;
1132 
1133 	  case 'g':		/* default gid */
1134 		if (isascii(*val) && isdigit(*val))
1135 			DefGid = atoi(val);
1136 		else
1137 		{
1138 			register struct group *gr;
1139 
1140 			DefGid = -1;
1141 			gr = getgrnam(val);
1142 			if (gr == NULL)
1143 				syserr("readcf: option g: unknown group %s", val);
1144 			else
1145 				DefGid = gr->gr_gid;
1146 		}
1147 		break;
1148 
1149 	  case 'H':		/* help file */
1150 		if (val[0] == '\0')
1151 			HelpFile = "sendmail.hf";
1152 		else
1153 			HelpFile = newstr(val);
1154 		break;
1155 
1156 	  case 'h':		/* maximum hop count */
1157 		MaxHopCount = atoi(val);
1158 		break;
1159 
1160 	  case 'I':		/* use internet domain name server */
1161 #ifdef NAMED_BIND
1162 		UseNameServer = TRUE;
1163 		for (p = val; *p != 0; )
1164 		{
1165 			bool clearmode;
1166 			char *q;
1167 			struct resolverflags *rfp;
1168 
1169 			while (*p == ' ')
1170 				p++;
1171 			if (*p == '\0')
1172 				break;
1173 			clearmode = FALSE;
1174 			if (*p == '-')
1175 				clearmode = TRUE;
1176 			else if (*p != '+')
1177 				p--;
1178 			p++;
1179 			q = p;
1180 			while (*p != '\0' && !(isascii(*p) && isspace(*p)))
1181 				p++;
1182 			if (*p != '\0')
1183 				*p++ = '\0';
1184 			for (rfp = ResolverFlags; rfp->rf_name != NULL; rfp++)
1185 			{
1186 				if (strcasecmp(q, rfp->rf_name) == 0)
1187 					break;
1188 			}
1189 			if (clearmode)
1190 				_res.options &= ~rfp->rf_bits;
1191 			else
1192 				_res.options |= rfp->rf_bits;
1193 		}
1194 		if (tTd(8, 2))
1195 			printf("_res.options = %x\n", _res.options);
1196 #else
1197 		usrerr("name server (I option) specified but BIND not compiled in");
1198 #endif
1199 		break;
1200 
1201 	  case 'i':		/* ignore dot lines in message */
1202 		IgnrDot = atobool(val);
1203 		break;
1204 
1205 	  case 'j':		/* send errors in MIME (RFC 1341) format */
1206 		SendMIMEErrors = atobool(val);
1207 		break;
1208 
1209 	  case 'J':		/* .forward search path */
1210 		ForwardPath = newstr(val);
1211 		break;
1212 
1213 	  case 'k':		/* connection cache size */
1214 		MaxMciCache = atoi(val);
1215 		if (MaxMciCache < 0)
1216 			MaxMciCache = 0;
1217 		break;
1218 
1219 	  case 'K':		/* connection cache timeout */
1220 		MciCacheTimeout = convtime(val, 'm');
1221 		break;
1222 
1223 	  case 'l':		/* use Errors-To: header */
1224 		UseErrorsTo = atobool(val);
1225 		break;
1226 
1227 	  case 'L':		/* log level */
1228 		if (safe || LogLevel < atoi(val))
1229 			LogLevel = atoi(val);
1230 		break;
1231 
1232 	  case 'M':		/* define macro */
1233 		define(val[0], newstr(&val[1]), CurEnv);
1234 		sticky = FALSE;
1235 		break;
1236 
1237 	  case 'm':		/* send to me too */
1238 		MeToo = atobool(val);
1239 		break;
1240 
1241 	  case 'n':		/* validate RHS in newaliases */
1242 		CheckAliases = atobool(val);
1243 		break;
1244 
1245 	    /* 'N' available -- was "net name" */
1246 
1247 	  case 'O':		/* daemon options */
1248 		setdaemonoptions(val);
1249 		break;
1250 
1251 	  case 'o':		/* assume old style headers */
1252 		if (atobool(val))
1253 			CurEnv->e_flags |= EF_OLDSTYLE;
1254 		else
1255 			CurEnv->e_flags &= ~EF_OLDSTYLE;
1256 		break;
1257 
1258 	  case 'p':		/* select privacy level */
1259 		p = val;
1260 		for (;;)
1261 		{
1262 			register struct prival *pv;
1263 			extern struct prival PrivacyValues[];
1264 
1265 			while (isascii(*p) && (isspace(*p) || ispunct(*p)))
1266 				p++;
1267 			if (*p == '\0')
1268 				break;
1269 			val = p;
1270 			while (isascii(*p) && isalnum(*p))
1271 				p++;
1272 			if (*p != '\0')
1273 				*p++ = '\0';
1274 
1275 			for (pv = PrivacyValues; pv->pv_name != NULL; pv++)
1276 			{
1277 				if (strcasecmp(val, pv->pv_name) == 0)
1278 					break;
1279 			}
1280 			if (pv->pv_name == NULL)
1281 				syserr("readcf: Op line: %s unrecognized", val);
1282 			PrivacyFlags |= pv->pv_flag;
1283 		}
1284 		break;
1285 
1286 	  case 'P':		/* postmaster copy address for returned mail */
1287 		PostMasterCopy = newstr(val);
1288 		break;
1289 
1290 	  case 'q':		/* slope of queue only function */
1291 		QueueFactor = atoi(val);
1292 		break;
1293 
1294 	  case 'Q':		/* queue directory */
1295 		if (val[0] == '\0')
1296 			QueueDir = "mqueue";
1297 		else
1298 			QueueDir = newstr(val);
1299 		if (RealUid != 0 && !safe)
1300 			auth_warning(e, "Processed from queue %s", QueueDir);
1301 		break;
1302 
1303 	  case 'R':		/* don't prune routes */
1304 		DontPruneRoutes = atobool(val);
1305 		break;
1306 
1307 	  case 'r':		/* read timeout */
1308 		settimeouts(val);
1309 		break;
1310 
1311 	  case 'S':		/* status file */
1312 		if (val[0] == '\0')
1313 			StatFile = "sendmail.st";
1314 		else
1315 			StatFile = newstr(val);
1316 		break;
1317 
1318 	  case 's':		/* be super safe, even if expensive */
1319 		SuperSafe = atobool(val);
1320 		break;
1321 
1322 	  case 'T':		/* queue timeout */
1323 		p = strchr(val, '/');
1324 		if (p != NULL)
1325 		{
1326 			*p++ = '\0';
1327 			TimeOuts.to_q_warning = convtime(p, 'd');
1328 		}
1329 		TimeOuts.to_q_return = convtime(val, 'h');
1330 		break;
1331 
1332 	  case 't':		/* time zone name */
1333 		TimeZoneSpec = newstr(val);
1334 		break;
1335 
1336 	  case 'U':		/* location of user database */
1337 		UdbSpec = newstr(val);
1338 		break;
1339 
1340 	  case 'u':		/* set default uid */
1341 		if (isascii(*val) && isdigit(*val))
1342 			DefUid = atoi(val);
1343 		else
1344 		{
1345 			register struct passwd *pw;
1346 
1347 			DefUid = -1;
1348 			pw = getpwnam(val);
1349 			if (pw == NULL)
1350 				syserr("readcf: option u: unknown user %s", val);
1351 			else
1352 				DefUid = pw->pw_uid;
1353 		}
1354 		setdefuser();
1355 		break;
1356 
1357 	  case 'V':		/* fallback MX host */
1358 		FallBackMX = newstr(val);
1359 		break;
1360 
1361 	  case 'v':		/* run in verbose mode */
1362 		Verbose = atobool(val);
1363 		break;
1364 
1365 	  case 'w':		/* if we are best MX, try host directly */
1366 		TryNullMXList = atobool(val);
1367 		break;
1368 
1369 	    /* 'W' available -- was wizard password */
1370 
1371 	  case 'x':		/* load avg at which to auto-queue msgs */
1372 		QueueLA = atoi(val);
1373 		break;
1374 
1375 	  case 'X':		/* load avg at which to auto-reject connections */
1376 		RefuseLA = atoi(val);
1377 		break;
1378 
1379 	  case 'y':		/* work recipient factor */
1380 		WkRecipFact = atoi(val);
1381 		break;
1382 
1383 	  case 'Y':		/* fork jobs during queue runs */
1384 		ForkQueueRuns = atobool(val);
1385 		break;
1386 
1387 	  case 'z':		/* work message class factor */
1388 		WkClassFact = atoi(val);
1389 		break;
1390 
1391 	  case 'Z':		/* work time factor */
1392 		WkTimeFact = atoi(val);
1393 		break;
1394 
1395 	  default:
1396 		break;
1397 	}
1398 	if (sticky)
1399 		setbitn(opt, StickyOpt);
1400 	return;
1401 }
1402 /*
1403 **  SETCLASS -- set a word into a class
1404 **
1405 **	Parameters:
1406 **		class -- the class to put the word in.
1407 **		word -- the word to enter
1408 **
1409 **	Returns:
1410 **		none.
1411 **
1412 **	Side Effects:
1413 **		puts the word into the symbol table.
1414 */
1415 
1416 setclass(class, word)
1417 	int class;
1418 	char *word;
1419 {
1420 	register STAB *s;
1421 
1422 	if (tTd(37, 8))
1423 		printf("%s added to class %c\n", word, class);
1424 	s = stab(word, ST_CLASS, ST_ENTER);
1425 	setbitn(class, s->s_class);
1426 }
1427 /*
1428 **  MAKEMAPENTRY -- create a map entry
1429 **
1430 **	Parameters:
1431 **		line -- the config file line
1432 **
1433 **	Returns:
1434 **		TRUE if it successfully entered the map entry.
1435 **		FALSE otherwise (usually syntax error).
1436 **
1437 **	Side Effects:
1438 **		Enters the map into the dictionary.
1439 */
1440 
1441 void
1442 makemapentry(line)
1443 	char *line;
1444 {
1445 	register char *p;
1446 	char *mapname;
1447 	char *classname;
1448 	register STAB *s;
1449 	STAB *class;
1450 
1451 	for (p = line; isascii(*p) && isspace(*p); p++)
1452 		continue;
1453 	if (!(isascii(*p) && isalnum(*p)))
1454 	{
1455 		syserr("readcf: config K line: no map name");
1456 		return;
1457 	}
1458 
1459 	mapname = p;
1460 	while (isascii(*++p) && isalnum(*p))
1461 		continue;
1462 	if (*p != '\0')
1463 		*p++ = '\0';
1464 	while (isascii(*p) && isspace(*p))
1465 		p++;
1466 	if (!(isascii(*p) && isalnum(*p)))
1467 	{
1468 		syserr("readcf: config K line, map %s: no map class", mapname);
1469 		return;
1470 	}
1471 	classname = p;
1472 	while (isascii(*++p) && isalnum(*p))
1473 		continue;
1474 	if (*p != '\0')
1475 		*p++ = '\0';
1476 	while (isascii(*p) && isspace(*p))
1477 		p++;
1478 
1479 	/* look up the class */
1480 	class = stab(classname, ST_MAPCLASS, ST_FIND);
1481 	if (class == NULL)
1482 	{
1483 		syserr("readcf: map %s: class %s not available", mapname, classname);
1484 		return;
1485 	}
1486 
1487 	/* enter the map */
1488 	s = stab(mapname, ST_MAP, ST_ENTER);
1489 	s->s_map.map_class = &class->s_mapclass;
1490 	s->s_map.map_mname = newstr(mapname);
1491 
1492 	if (class->s_mapclass.map_parse(&s->s_map, p))
1493 		s->s_map.map_mflags |= MF_VALID;
1494 
1495 	if (tTd(37, 5))
1496 	{
1497 		printf("map %s, class %s, flags %x, file %s,\n",
1498 			s->s_map.map_mname, s->s_map.map_class->map_cname,
1499 			s->s_map.map_mflags,
1500 			s->s_map.map_file == NULL ? "(null)" : s->s_map.map_file);
1501 		printf("\tapp %s, domain %s, rebuild %s\n",
1502 			s->s_map.map_app == NULL ? "(null)" : s->s_map.map_app,
1503 			s->s_map.map_domain == NULL ? "(null)" : s->s_map.map_domain,
1504 			s->s_map.map_rebuild == NULL ? "(null)" : s->s_map.map_rebuild);
1505 	}
1506 }
1507 /*
1508 **  SETTIMEOUTS -- parse and set timeout values
1509 **
1510 **	Parameters:
1511 **		val -- a pointer to the values.  If NULL, do initial
1512 **			settings.
1513 **
1514 **	Returns:
1515 **		none.
1516 **
1517 **	Side Effects:
1518 **		Initializes the TimeOuts structure
1519 */
1520 
1521 #define SECONDS
1522 #define MINUTES	* 60
1523 #define HOUR	* 3600
1524 
1525 settimeouts(val)
1526 	register char *val;
1527 {
1528 	register char *p;
1529 	extern time_t convtime();
1530 
1531 	if (val == NULL)
1532 	{
1533 		TimeOuts.to_initial = (time_t) 5 MINUTES;
1534 		TimeOuts.to_helo = (time_t) 5 MINUTES;
1535 		TimeOuts.to_mail = (time_t) 10 MINUTES;
1536 		TimeOuts.to_rcpt = (time_t) 1 HOUR;
1537 		TimeOuts.to_datainit = (time_t) 5 MINUTES;
1538 		TimeOuts.to_datablock = (time_t) 1 HOUR;
1539 		TimeOuts.to_datafinal = (time_t) 1 HOUR;
1540 		TimeOuts.to_rset = (time_t) 5 MINUTES;
1541 		TimeOuts.to_quit = (time_t) 2 MINUTES;
1542 		TimeOuts.to_nextcommand = (time_t) 1 HOUR;
1543 		TimeOuts.to_miscshort = (time_t) 2 MINUTES;
1544 		TimeOuts.to_ident = (time_t) 30 SECONDS;
1545 		return;
1546 	}
1547 
1548 	for (;; val = p)
1549 	{
1550 		while (isascii(*val) && isspace(*val))
1551 			val++;
1552 		if (*val == '\0')
1553 			break;
1554 		for (p = val; *p != '\0' && *p != ','; p++)
1555 			continue;
1556 		if (*p != '\0')
1557 			*p++ = '\0';
1558 
1559 		if (isascii(*val) && isdigit(*val))
1560 		{
1561 			/* old syntax -- set everything */
1562 			TimeOuts.to_mail = convtime(val, 'm');
1563 			TimeOuts.to_rcpt = TimeOuts.to_mail;
1564 			TimeOuts.to_datainit = TimeOuts.to_mail;
1565 			TimeOuts.to_datablock = TimeOuts.to_mail;
1566 			TimeOuts.to_datafinal = TimeOuts.to_mail;
1567 			TimeOuts.to_nextcommand = TimeOuts.to_mail;
1568 			continue;
1569 		}
1570 		else
1571 		{
1572 			register char *q = strchr(val, '=');
1573 			time_t to;
1574 
1575 			if (q == NULL)
1576 			{
1577 				/* syntax error */
1578 				continue;
1579 			}
1580 			*q++ = '\0';
1581 			to = convtime(q, 'm');
1582 
1583 			if (strcasecmp(val, "initial") == 0)
1584 				TimeOuts.to_initial = to;
1585 			else if (strcasecmp(val, "mail") == 0)
1586 				TimeOuts.to_mail = to;
1587 			else if (strcasecmp(val, "rcpt") == 0)
1588 				TimeOuts.to_rcpt = to;
1589 			else if (strcasecmp(val, "datainit") == 0)
1590 				TimeOuts.to_datainit = to;
1591 			else if (strcasecmp(val, "datablock") == 0)
1592 				TimeOuts.to_datablock = to;
1593 			else if (strcasecmp(val, "datafinal") == 0)
1594 				TimeOuts.to_datafinal = to;
1595 			else if (strcasecmp(val, "command") == 0)
1596 				TimeOuts.to_nextcommand = to;
1597 			else if (strcasecmp(val, "rset") == 0)
1598 				TimeOuts.to_rset = to;
1599 			else if (strcasecmp(val, "helo") == 0)
1600 				TimeOuts.to_helo = to;
1601 			else if (strcasecmp(val, "quit") == 0)
1602 				TimeOuts.to_quit = to;
1603 			else if (strcasecmp(val, "misc") == 0)
1604 				TimeOuts.to_miscshort = to;
1605 			else if (strcasecmp(val, "ident") == 0)
1606 				TimeOuts.to_ident = to;
1607 			else
1608 				syserr("settimeouts: invalid timeout %s", val);
1609 		}
1610 	}
1611 }
1612