xref: /csrg-svn/usr.sbin/sendmail/src/readcf.c (revision 10687)
1 # include "sendmail.h"
2 
3 SCCSID(@(#)readcf.c	3.53		02/02/83);
4 
5 /*
6 **  READCF -- read control file.
7 **
8 **	This routine reads the control file and builds the internal
9 **	form.
10 **
11 **	The file is formatted as a sequence of lines, each taken
12 **	atomically.  The first character of each line describes how
13 **	the line is to be interpreted.  The lines are:
14 **		Dxval		Define macro x to have value val.
15 **		Cxword		Put word into class x.
16 **		Fxfile [fmt]	Read file for lines to put into
17 **				class x.  Use scanf string 'fmt'
18 **				or "%s" if not present.  Fmt should
19 **				only produce one string-valued result.
20 **		Hname: value	Define header with field-name 'name'
21 **				and value as specified; this will be
22 **				macro expanded immediately before
23 **				use.
24 **		Sn		Use rewriting set n.
25 **		Rlhs rhs	Rewrite addresses that match lhs to
26 **				be rhs.
27 **		Mn p f s r a	Define mailer.  n - internal name,
28 **				p - pathname, f - flags, s - rewriting
29 **				ruleset for sender, s - rewriting ruleset
30 **				for recipients, a - argument vector.
31 **		Oxvalue		Set option x to value.
32 **		Pname=value	Set precedence name to value.
33 **
34 **	Parameters:
35 **		cfname -- control file name.
36 **		safe -- set if this is a system configuration file.
37 **			Non-system configuration files can not do
38 **			certain things (e.g., leave the SUID bit on
39 **			when executing mailers).
40 **
41 **	Returns:
42 **		none.
43 **
44 **	Side Effects:
45 **		Builds several internal tables.
46 */
47 
48 readcf(cfname, safe)
49 	char *cfname;
50 	bool safe;
51 {
52 	FILE *cf;
53 	int ruleset = 0;
54 	char *q;
55 	char **pv;
56 	struct rewrite *rwp = NULL;
57 	char buf[MAXLINE];
58 	register char *p;
59 	extern char **prescan();
60 	extern char **copyplist();
61 	char exbuf[MAXLINE];
62 	extern char *fgetfolded();
63 
64 	cf = fopen(cfname, "r");
65 	if (cf == NULL)
66 	{
67 		syserr("cannot open %s", cfname);
68 		exit(EX_OSFILE);
69 	}
70 
71 	FileName = cfname;
72 	LineNumber = 0;
73 	while (fgetfolded(buf, sizeof buf, cf) != NULL)
74 	{
75 		switch (buf[0])
76 		{
77 		  case '\0':
78 		  case '#':		/* comment */
79 			break;
80 
81 		  case 'R':		/* rewriting rule */
82 			for (p = &buf[1]; *p != '\0' && *p != '\t'; p++)
83 				continue;
84 
85 			if (*p == '\0')
86 			{
87 				syserr("invalid rewrite line \"%s\"", buf);
88 				break;
89 			}
90 
91 			/* allocate space for the rule header */
92 			if (rwp == NULL)
93 			{
94 				RewriteRules[ruleset] = rwp =
95 					(struct rewrite *) xalloc(sizeof *rwp);
96 			}
97 			else
98 			{
99 				rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp);
100 				rwp = rwp->r_next;
101 			}
102 			rwp->r_next = NULL;
103 
104 			/* expand and save the LHS */
105 			*p = '\0';
106 			expand(&buf[1], exbuf, &exbuf[sizeof exbuf], CurEnv);
107 			rwp->r_lhs = prescan(exbuf, '\t');
108 			if (rwp->r_lhs != NULL)
109 				rwp->r_lhs = copyplist(rwp->r_lhs, TRUE);
110 
111 			/* expand and save the RHS */
112 			while (*++p == '\t')
113 				continue;
114 			q = p;
115 			while (*p != '\0' && *p != '\t')
116 				p++;
117 			*p = '\0';
118 			expand(q, exbuf, &exbuf[sizeof exbuf], CurEnv);
119 			rwp->r_rhs = prescan(exbuf, '\t');
120 			if (rwp->r_rhs != NULL)
121 				rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
122 			break;
123 
124 		  case 'S':		/* select rewriting set */
125 			ruleset = atoi(&buf[1]);
126 			if (ruleset >= MAXRWSETS || ruleset < 0)
127 			{
128 				syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS);
129 				ruleset = 0;
130 			}
131 			rwp = NULL;
132 			break;
133 
134 		  case 'D':		/* macro definition */
135 			define(buf[1], newstr(&buf[2]), CurEnv);
136 			break;
137 
138 		  case 'H':		/* required header line */
139 			(void) chompheader(&buf[1], TRUE);
140 			break;
141 
142 		  case 'C':		/* word class */
143 		  case 'F':		/* word class from file */
144 			/* read list of words from argument or file */
145 			if (buf[0] == 'F')
146 			{
147 				/* read from file */
148 				for (p = &buf[2]; *p != '\0' && !isspace(*p); p++)
149 					continue;
150 				if (*p == '\0')
151 					p = "%s";
152 				else
153 				{
154 					*p = '\0';
155 					while (isspace(*++p))
156 						continue;
157 				}
158 				fileclass(buf[1], &buf[2], p);
159 				break;
160 			}
161 
162 			/* scan the list of words and set class for all */
163 			for (p = &buf[2]; *p != '\0'; )
164 			{
165 				register char *wd;
166 				char delim;
167 				register STAB *s;
168 
169 				while (*p != '\0' && isspace(*p))
170 					p++;
171 				wd = p;
172 				while (*p != '\0' && !isspace(*p))
173 					p++;
174 				delim = *p;
175 				*p = '\0';
176 				if (wd[0] != '\0')
177 					setclass(buf[1], wd);
178 				*p = delim;
179 			}
180 			break;
181 
182 		  case 'M':		/* define mailer */
183 			makemailer(&buf[1], safe);
184 			break;
185 
186 		  case 'O':		/* set option */
187 			setoption(buf[1], &buf[2], safe, FALSE);
188 			break;
189 
190 		  case 'P':		/* set precedence */
191 			if (NumPriorities >= MAXPRIORITIES)
192 			{
193 				toomany('P', MAXPRIORITIES);
194 				break;
195 			}
196 			for (p = &buf[1]; *p != '\0' && *p != '=' && *p != '\t'; p++)
197 				continue;
198 			if (*p == '\0')
199 				goto badline;
200 			*p = '\0';
201 			Priorities[NumPriorities].pri_name = newstr(&buf[1]);
202 			Priorities[NumPriorities].pri_val = atoi(++p);
203 			NumPriorities++;
204 			break;
205 
206 		  case 'T':		/* trusted user(s) */
207 			p = &buf[1];
208 			while (*p != '\0')
209 			{
210 				while (isspace(*p))
211 					p++;
212 				q = p;
213 				while (*p != '\0' && !isspace(*p))
214 					p++;
215 				if (*p != '\0')
216 					*p++ = '\0';
217 				if (*q == '\0')
218 					continue;
219 				for (pv = TrustedUsers; *pv != NULL; pv++)
220 					continue;
221 				if (pv >= &TrustedUsers[MAXTRUST])
222 				{
223 					toomany('T', MAXTRUST);
224 					break;
225 				}
226 				*pv = newstr(q);
227 			}
228 			break;
229 
230 		  default:
231 		  badline:
232 			syserr("unknown control line \"%s\"", buf);
233 		}
234 	}
235 	FileName = NULL;
236 }
237 /*
238 **  TOOMANY -- signal too many of some option
239 **
240 **	Parameters:
241 **		id -- the id of the error line
242 **		maxcnt -- the maximum possible values
243 **
244 **	Returns:
245 **		none.
246 **
247 **	Side Effects:
248 **		gives a syserr.
249 */
250 
251 toomany(id, maxcnt)
252 	char id;
253 	int maxcnt;
254 {
255 	syserr("too many %c lines, %d max", id, maxcnt);
256 }
257 /*
258 **  FILECLASS -- read members of a class from a file
259 **
260 **	Parameters:
261 **		class -- class to define.
262 **		filename -- name of file to read.
263 **		fmt -- scanf string to use for match.
264 **
265 **	Returns:
266 **		none
267 **
268 **	Side Effects:
269 **
270 **		puts all lines in filename that match a scanf into
271 **			the named class.
272 */
273 
274 fileclass(class, filename, fmt)
275 	int class;
276 	char *filename;
277 	char *fmt;
278 {
279 	register FILE *f;
280 	char buf[MAXLINE];
281 
282 	f = fopen(filename, "r");
283 	if (f == NULL)
284 	{
285 		syserr("cannot open %s", filename);
286 		return;
287 	}
288 
289 	while (fgets(buf, sizeof buf, f) != NULL)
290 	{
291 		register STAB *s;
292 		char wordbuf[MAXNAME+1];
293 
294 		if (sscanf(buf, fmt, wordbuf) != 1)
295 			continue;
296 		s = stab(wordbuf, ST_CLASS, ST_ENTER);
297 		setbitn(class, s->s_class);
298 	}
299 
300 	(void) fclose(f);
301 }
302 /*
303 **  MAKEMAILER -- define a new mailer.
304 **
305 **	Parameters:
306 **		line -- description of mailer.  This is in labeled
307 **			fields.  The fields are:
308 **			   P -- the path to the mailer
309 **			   F -- the flags associated with the mailer
310 **			   A -- the argv for this mailer
311 **			   S -- the sender rewriting set
312 **			   R -- the recipient rewriting set
313 **			   E -- the eol string
314 **			The first word is the canonical name of the mailer.
315 **		safe -- set if this is a safe configuration file.
316 **
317 **	Returns:
318 **		none.
319 **
320 **	Side Effects:
321 **		enters the mailer into the mailer table.
322 */
323 
324 makemailer(line, safe)
325 	char *line;
326 	bool safe;
327 {
328 	register char *p;
329 	register struct mailer *m;
330 	register STAB *s;
331 	int i;
332 	char fcode;
333 	extern int NextMailer;
334 	extern char **makeargv();
335 	extern char *munchstring();
336 	extern char *DelimChar;
337 
338 	/* allocate a mailer and set up defaults */
339 	m = (struct mailer *) xalloc(sizeof *m);
340 	bzero((char *) m, sizeof *m);
341 	m->m_mno = NextMailer;
342 	m->m_eol = "\n";
343 
344 	/* collect the mailer name */
345 	for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++)
346 		continue;
347 	if (*p != '\0')
348 		*p++ = '\0';
349 	m->m_name = newstr(line);
350 
351 	/* now scan through and assign info from the fields */
352 	while (*p != '\0')
353 	{
354 		while (*p != '\0' && (*p == ',' || isspace(*p)))
355 			p++;
356 
357 		/* p now points to field code */
358 		fcode = *p;
359 		while (*p != '\0' && *p != '=' && *p != ',')
360 			p++;
361 		if (*p++ != '=')
362 		{
363 			syserr("`=' expected");
364 			return;
365 		}
366 		while (isspace(*p))
367 			p++;
368 
369 		/* p now points to the field body */
370 		p = munchstring(p);
371 
372 		/* install the field into the mailer struct */
373 		switch (fcode)
374 		{
375 		  case 'P':		/* pathname */
376 			m->m_mailer = newstr(p);
377 			break;
378 
379 		  case 'F':		/* flags */
380 			for (; *p != '\0'; p++)
381 				setbitn(*p, m->m_flags);
382 			if (!safe)
383 				clrbitn(M_RESTR, m->m_flags);
384 			break;
385 
386 		  case 'S':		/* sender rewriting ruleset */
387 		  case 'R':		/* recipient rewriting ruleset */
388 			i = atoi(p);
389 			if (i < 0 || i >= MAXRWSETS)
390 			{
391 				syserr("invalid rewrite set, %d max", MAXRWSETS);
392 				return;
393 			}
394 			if (fcode == 'S')
395 				m->m_s_rwset = i;
396 			else
397 				m->m_r_rwset = i;
398 			break;
399 
400 		  case 'E':		/* end of line string */
401 			m->m_eol = newstr(p);
402 			break;
403 
404 		  case 'A':		/* argument vector */
405 			m->m_argv = makeargv(p);
406 			break;
407 		}
408 
409 		p = DelimChar;
410 	}
411 
412 	/* now store the mailer away */
413 	if (NextMailer >= MAXMAILERS)
414 	{
415 		syserr("too many mailers defined (%d max)", MAXMAILERS);
416 		return;
417 	}
418 	Mailer[NextMailer++] = m;
419 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
420 	s->s_mailer = m;
421 }
422 /*
423 **  MUNCHSTRING -- translate a string into internal form.
424 **
425 **	Parameters:
426 **		p -- the string to munch.
427 **
428 **	Returns:
429 **		the munched string.
430 **
431 **	Side Effects:
432 **		Sets "DelimChar" to point to the string that caused us
433 **		to stop.
434 */
435 
436 char *
437 munchstring(p)
438 	register char *p;
439 {
440 	register char *q;
441 	bool backslash = FALSE;
442 	bool quotemode = FALSE;
443 	static char buf[MAXLINE];
444 	extern char *DelimChar;
445 
446 	for (q = buf; *p != '\0'; p++)
447 	{
448 		if (backslash)
449 		{
450 			/* everything is roughly literal */
451 			backslash = FALSE;
452 			switch (*p)
453 			{
454 			  case 'r':		/* carriage return */
455 				*q++ = '\r';
456 				continue;
457 
458 			  case 'n':		/* newline */
459 				*q++ = '\n';
460 				continue;
461 
462 			  case 'f':		/* form feed */
463 				*q++ = '\f';
464 				continue;
465 
466 			  case 'b':		/* backspace */
467 				*q++ = '\b';
468 				continue;
469 			}
470 			*q++ = *p;
471 		}
472 		else
473 		{
474 			if (*p == '\\')
475 				backslash = TRUE;
476 			else if (*p == '"')
477 				quotemode = !quotemode;
478 			else if (quotemode || *p != ',')
479 				*q++ = *p;
480 			else
481 				break;
482 		}
483 	}
484 
485 	DelimChar = p;
486 	*q++ = '\0';
487 	return (buf);
488 }
489 /*
490 **  MAKEARGV -- break up a string into words
491 **
492 **	Parameters:
493 **		p -- the string to break up.
494 **
495 **	Returns:
496 **		a char **argv (dynamically allocated)
497 **
498 **	Side Effects:
499 **		munges p.
500 */
501 
502 char **
503 makeargv(p)
504 	register char *p;
505 {
506 	char *q;
507 	int i;
508 	char **avp;
509 	char *argv[MAXPV + 1];
510 
511 	/* take apart the words */
512 	i = 0;
513 	while (*p != '\0' && i < MAXPV)
514 	{
515 		q = p;
516 		while (*p != '\0' && !isspace(*p))
517 			p++;
518 		while (isspace(*p))
519 			*p++ = '\0';
520 		argv[i++] = newstr(q);
521 	}
522 	argv[i++] = NULL;
523 
524 	/* now make a copy of the argv */
525 	avp = (char **) xalloc(sizeof *avp * i);
526 	bmove((char *) argv, (char *) avp, sizeof *avp * i);
527 
528 	return (avp);
529 }
530 /*
531 **  PRINTRULES -- print rewrite rules (for debugging)
532 **
533 **	Parameters:
534 **		none.
535 **
536 **	Returns:
537 **		none.
538 **
539 **	Side Effects:
540 **		prints rewrite rules.
541 */
542 
543 # ifdef DEBUG
544 
545 printrules()
546 {
547 	register struct rewrite *rwp;
548 	register int ruleset;
549 
550 	for (ruleset = 0; ruleset < 10; ruleset++)
551 	{
552 		if (RewriteRules[ruleset] == NULL)
553 			continue;
554 		printf("\n----Rule Set %d:", ruleset);
555 
556 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
557 		{
558 			printf("\nLHS:");
559 			printav(rwp->r_lhs);
560 			printf("RHS:");
561 			printav(rwp->r_rhs);
562 		}
563 	}
564 }
565 
566 # endif DEBUG
567 /*
568 **  SETOPTION -- set global processing option
569 **
570 **	Parameters:
571 **		opt -- option name.
572 **		val -- option value (as a text string).
573 **		safe -- if set, this came from a system configuration file.
574 **		sticky -- if set, don't let other setoptions override
575 **			this value.
576 **
577 **	Returns:
578 **		none.
579 **
580 **	Side Effects:
581 **		Sets options as implied by the arguments.
582 */
583 
584 static BITMAP	StickyOpt;		/* set if option is stuck */
585 extern char	*WizWord;		/* the stored wizard password */
586 #ifdef DAEMON
587 extern int	MaxConnections;		/* max simult. SMTP conns */
588 #endif DAEMON
589 
590 setoption(opt, val, safe, sticky)
591 	char opt;
592 	char *val;
593 	bool safe;
594 	bool sticky;
595 {
596 	extern bool atobool();
597 
598 # ifdef DEBUG
599 	if (tTd(37, 1))
600 		printf("setoption %c=%s", opt, val);
601 # endif DEBUG
602 
603 	/*
604 	**  See if this option is preset for us.
605 	*/
606 
607 	if (bitnset(opt, StickyOpt))
608 	{
609 # ifdef DEBUG
610 		if (tTd(37, 1))
611 			printf(" (ignored)\n");
612 # endif DEBUG
613 		return;
614 	}
615 #ifdef DEBUG
616 	else if (tTd(37, 1))
617 		printf("\n");
618 #endif DEBUG
619 	if (sticky)
620 		setbitn(opt, StickyOpt);
621 
622 	if (getruid() == 0)
623 		safe = TRUE;
624 
625 	switch (opt)
626 	{
627 	  case 'A':		/* set default alias file */
628 		if (val[0] == '\0')
629 			AliasFile = "aliases";
630 		else
631 			AliasFile = newstr(val);
632 		break;
633 
634 	  case 'a':		/* look for "@:@" in alias file */
635 		SafeAlias = atobool(val);
636 		break;
637 
638 	  case 'c':		/* don't connect to "expensive" mailers */
639 		NoConnect = atobool(val);
640 		break;
641 
642 	  case 'd':		/* delivery mode */
643 		switch (*val)
644 		{
645 		  case '\0':
646 			SendMode = SM_DELIVER;
647 			break;
648 
649 		  case SM_DELIVER:	/* do everything */
650 		  case SM_FORK:		/* fork after verification */
651 		  case SM_QUEUE:	/* queue only */
652 			SendMode = *val;
653 			break;
654 
655 		  default:
656 			syserr("Unknown delivery mode %c", *val);
657 			exit(EX_USAGE);
658 		}
659 		break;
660 
661 	  case 'D':		/* rebuild alias database as needed */
662 		AutoRebuild = atobool(val);
663 		break;
664 
665 	  case 'e':		/* set error processing mode */
666 		switch (*val)
667 		{
668 		  case EM_QUIET:	/* be silent about it */
669 			(void) freopen("/dev/null", "w", stdout);
670 			/* fall through... */
671 
672 		  case EM_MAIL:		/* mail back */
673 		  case EM_BERKNET:	/* do berknet error processing */
674 		  case EM_WRITE:	/* write back (or mail) */
675 			HoldErrs = TRUE;
676 			/* fall through... */
677 
678 		  case EM_PRINT:	/* print errors normally (default) */
679 			ErrorMode = *val;
680 			break;
681 		}
682 		break;
683 
684 	  case 'F':		/* file mode */
685 		FileMode = atooct(val);
686 		break;
687 
688 	  case 'f':		/* save Unix-style From lines on front */
689 		SaveFrom = atobool(val);
690 		break;
691 
692 	  case 'g':		/* default gid */
693 		if (safe)
694 			DefGid = atoi(val);
695 		break;
696 
697 	  case 'H':		/* help file */
698 		if (val[0] == '\0')
699 			HelpFile = "sendmail.hf";
700 		else
701 			HelpFile = newstr(val);
702 		break;
703 
704 	  case 'i':		/* ignore dot lines in message */
705 		IgnrDot = atobool(val);
706 		break;
707 
708 	  case 'L':		/* log level */
709 		LogLevel = atoi(val);
710 		break;
711 
712 	  case 'M':		/* define macro */
713 		define(val[0], newstr(&val[1]), CurEnv);
714 		break;
715 
716 	  case 'm':		/* send to me too */
717 		MeToo = atobool(val);
718 		break;
719 
720 #ifdef DAEMON
721 	  case 'N':		/* maximum simultaneous SMTP connections */
722 		MaxConnections = atoi(val);
723 		break;
724 #endif DAEMON
725 
726 	  case 'o':		/* assume old style headers */
727 		if (atobool(val))
728 			CurEnv->e_flags |= EF_OLDSTYLE;
729 		else
730 			CurEnv->e_flags &= ~EF_OLDSTYLE;
731 		break;
732 
733 	  case 'Q':		/* queue directory */
734 		if (val[0] == '\0')
735 			QueueDir = "mqueue";
736 		else
737 			QueueDir = newstr(val);
738 		break;
739 
740 	  case 'r':		/* read timeout */
741 		ReadTimeout = convtime(val);
742 		break;
743 
744 	  case 'S':		/* status file */
745 		if (val[0] == '\0')
746 			StatFile = "sendmail.st";
747 		else
748 			StatFile = newstr(val);
749 		break;
750 
751 	  case 's':		/* be super safe, even if expensive */
752 		SuperSafe = atobool(val);
753 		break;
754 
755 	  case 'T':		/* queue timeout */
756 		TimeOut = convtime(val);
757 		break;
758 
759 	  case 't':		/* time zone name */
760 # ifdef V6
761 		StdTimezone = newstr(val);
762 		DstTimezone = index(StdTimeZone, ',');
763 		if (DstTimezone == NULL)
764 			syserr("bad time zone spec");
765 		else
766 			*DstTimezone++ = '\0';
767 # endif V6
768 		break;
769 
770 	  case 'u':		/* set default uid */
771 		if (safe)
772 			DefUid = atoi(val);
773 		break;
774 
775 	  case 'v':		/* run in verbose mode */
776 		Verbose = atobool(val);
777 		break;
778 
779 # ifdef DEBUG
780 	  case 'W':		/* set the wizards password */
781 		if (safe)
782 			WizWord = newstr(val);
783 		break;
784 # endif DEBUG
785 
786 	  default:
787 		break;
788 	}
789 	return;
790 }
791 /*
792 **  SETCLASS -- set a word into a class
793 **
794 **	Parameters:
795 **		class -- the class to put the word in.
796 **		word -- the word to enter
797 **
798 **	Returns:
799 **		none.
800 **
801 **	Side Effects:
802 **		puts the word into the symbol table.
803 */
804 
805 setclass(class, word)
806 	int class;
807 	char *word;
808 {
809 	register STAB *s;
810 
811 	s = stab(word, ST_CLASS, ST_ENTER);
812 	setbitn(class, s->s_class);
813 }
814