13313Seric # include "sendmail.h"
23308Seric 
3*4432Seric static char SccsId[] = "@(#)readcf.c	3.14	09/23/81";
43308Seric 
53308Seric /*
63308Seric **  READCF -- read control file.
73308Seric **
83308Seric **	This routine reads the control file and builds the internal
93308Seric **	form.
103308Seric **
11*4432Seric **	The file is formatted as a sequence of lines, each taken
12*4432Seric **	atomically.  The first character of each line describes how
13*4432Seric **	the line is to be interpreted.  The lines are:
14*4432Seric **		Dxval		Define macro x to have value val.
15*4432Seric **		Cxword		Put word into class x.
16*4432Seric **		Fxfile [fmt]	Read file for lines to put into
17*4432Seric **				class x.  Use scanf string 'fmt'
18*4432Seric **				or "%s" if not present.  Fmt should
19*4432Seric **				only produce one string-valued result.
20*4432Seric **		Hname: value	Define header with field-name 'name'
21*4432Seric **				and value as specified; this will be
22*4432Seric **				macro expanded immediately before
23*4432Seric **				use.
24*4432Seric **		Sn		Use rewriting set n.
25*4432Seric **		Rlhs rhs	Rewrite addresses that match lhs to
26*4432Seric **				be rhs.
27*4432Seric **		Mn p f r a	Define mailer.  n - internal name,
28*4432Seric **				p - pathname, f - flags, r - rewriting
29*4432Seric **				rule for sender, a - argument vector.
30*4432Seric **
313308Seric **	Parameters:
323308Seric **		cfname -- control file name.
334217Seric **		safe -- set if this is a system configuration file.
344217Seric **			Non-system configuration files can not do
354217Seric **			certain things (e.g., leave the SUID bit on
364217Seric **			when executing mailers).
373308Seric **
383308Seric **	Returns:
393308Seric **		none.
403308Seric **
413308Seric **	Side Effects:
423308Seric **		Builds several internal tables.
433308Seric */
443308Seric 
454072Seric struct rewrite	*RewriteRules[10];
463308Seric 
473308Seric 
484217Seric readcf(cfname, safe)
493308Seric 	char *cfname;
504217Seric 	bool safe;
513308Seric {
523308Seric 	FILE *cf;
533308Seric 	char buf[MAXLINE];
543308Seric 	register char *p;
553308Seric 	struct rewrite *rwp = NULL;
563308Seric 	extern char **prescan();
573308Seric 	extern char **copyplist();
584061Seric 	int class;
594072Seric 	int ruleset = 0;
603308Seric 
613308Seric 	cf = fopen(cfname, "r");
623308Seric 	if (cf == NULL)
633308Seric 	{
643308Seric 		syserr("cannot open %s", cfname);
653308Seric 		exit(EX_OSFILE);
663308Seric 	}
673308Seric 
683308Seric 	while (fgets(buf, sizeof buf, cf) != NULL)
693308Seric 	{
703308Seric 		p = rindex(buf, '\n');
713308Seric 		if (p != NULL)
723308Seric 			*p = '\0';
733308Seric 
743308Seric 		switch (buf[0])
753308Seric 		{
763308Seric 		  case '\n':
773308Seric 		  case '\0':
783308Seric 		  case ' ':
793308Seric 		  case '\t':
803308Seric 		  case '#':		/* comment */
813308Seric 			break;
823308Seric 
833308Seric 		  case 'R':		/* rewriting rule */
843308Seric 			for (p = &buf[1]; *p != '\0' && *p != '\t'; p++)
853308Seric 				continue;
863308Seric 
873308Seric 			if (*p == '\0')
883308Seric 				syserr("invalid rewrite line \"%s\"", buf);
893308Seric 			else
903308Seric 			{
913308Seric 				if (rwp == NULL)
924072Seric 				{
934072Seric 					RewriteRules[ruleset] = rwp =
944072Seric 						(struct rewrite *) xalloc(sizeof *rwp);
954072Seric 				}
963308Seric 				else
973308Seric 				{
983308Seric 					rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp);
993308Seric 					rwp = rwp->r_next;
1003308Seric 				}
1013308Seric 				rwp->r_next = NULL;
1023308Seric 
1033308Seric 				rwp->r_lhs = prescan(&buf[1], '\t');
1043308Seric 				if (rwp->r_lhs != NULL)
1053308Seric 					rwp->r_lhs = copyplist(rwp->r_lhs, TRUE);
1063308Seric 				while (*p == '\t')
1073308Seric 					p++;
1083308Seric 				rwp->r_rhs = prescan(p, '\t');
1093308Seric 				if (rwp->r_rhs != NULL)
1103308Seric 					rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
1113308Seric 			}
1123308Seric 			break;
1133308Seric 
1144072Seric 		  case 'S':		/* select rewriting set */
1154072Seric 			ruleset = atoi(&buf[1]);
1164072Seric 			rwp = NULL;
1174072Seric 			break;
1184072Seric 
1193308Seric 		  case 'D':		/* macro definition */
1203308Seric 			define(buf[1], newstr(&buf[2]));
1213308Seric 			break;
1223308Seric 
1233387Seric 		  case 'H':		/* required header line */
1244088Seric 			(void) chompheader(&buf[1], TRUE);
1253387Seric 			break;
1263387Seric 
1274061Seric 		  case 'C':		/* word class */
128*4432Seric 		  case 'F':		/* word class from file */
1294061Seric 			class = buf[1];
1304061Seric 			if (!isalpha(class))
1314061Seric 				goto badline;
1324061Seric 			if (isupper(class))
1334061Seric 				class -= 'A';
1344061Seric 			else
1354061Seric 				class -= 'a';
136*4432Seric 
137*4432Seric 			/* read list of words from argument or file */
138*4432Seric 			if (buf[0] == 'F')
139*4432Seric 			{
140*4432Seric 				/* read from file */
141*4432Seric 				for (p = &buf[2]; *p != '\0' && !isspace(*p); p++)
142*4432Seric 					continue;
143*4432Seric 				if (*p == '\0')
144*4432Seric 					p = "%s";
145*4432Seric 				else
146*4432Seric 				{
147*4432Seric 					*p = '\0';
148*4432Seric 					while (isspace(*++p))
149*4432Seric 						continue;
150*4432Seric 				}
151*4432Seric 				fileclass(class, &buf[2], p);
152*4432Seric 				break;
153*4432Seric 			}
1544061Seric 
155*4432Seric 			/* scan the list of words and set class for all */
1564061Seric 			for (p = &buf[2]; *p != '\0'; )
1574061Seric 			{
1584061Seric 				register char *wd;
1594061Seric 				char delim;
1604061Seric 				register STAB *s;
1614061Seric 
1624061Seric 				while (*p != '\0' && isspace(*p))
1634061Seric 					p++;
1644061Seric 				wd = p;
1654061Seric 				while (*p != '\0' && !isspace(*p))
1664061Seric 					p++;
1674061Seric 				delim = *p;
1684061Seric 				*p = '\0';
1694061Seric 				if (wd[0] != '\0')
1704061Seric 				{
1714103Seric 					s = stab(wd, ST_CLASS, ST_ENTER);
1724061Seric 					s->s_class |= 1 << class;
1734061Seric 				}
1744061Seric 				*p = delim;
1754061Seric 			}
1764061Seric 			break;
1774061Seric 
1784096Seric 		  case 'M':		/* define mailer */
1794217Seric 			makemailer(&buf[1], safe);
1804096Seric 			break;
1814096Seric 
1823308Seric 		  default:
1834061Seric 		  badline:
1843308Seric 			syserr("unknown control line \"%s\"", buf);
1853308Seric 		}
1863308Seric 	}
1874096Seric }
1884096Seric /*
189*4432Seric **  FILECLASS -- read members of a class from a file
190*4432Seric **
191*4432Seric **	Parameters:
192*4432Seric **		class -- class to define.
193*4432Seric **		filename -- name of file to read.
194*4432Seric **		fmt -- scanf string to use for match.
195*4432Seric **
196*4432Seric **	Returns:
197*4432Seric **		none
198*4432Seric **
199*4432Seric **	Side Effects:
200*4432Seric **
201*4432Seric **		puts all lines in filename that match a scanf into
202*4432Seric **			the named class.
203*4432Seric */
204*4432Seric 
205*4432Seric fileclass(class, filename, fmt)
206*4432Seric 	int class;
207*4432Seric 	char *filename;
208*4432Seric 	char *fmt;
209*4432Seric {
210*4432Seric 	register FILE *f;
211*4432Seric 	char buf[MAXLINE];
212*4432Seric 
213*4432Seric 	f = fopen(filename, "r");
214*4432Seric 	if (f == NULL)
215*4432Seric 	{
216*4432Seric 		syserr("cannot open %s", filename);
217*4432Seric 		return;
218*4432Seric 	}
219*4432Seric 
220*4432Seric 	while (fgets(buf, sizeof buf, f) != NULL)
221*4432Seric 	{
222*4432Seric 		register STAB *s;
223*4432Seric 		char wordbuf[MAXNAME+1];
224*4432Seric 
225*4432Seric 		if (sscanf(buf, fmt, wordbuf) != 1)
226*4432Seric 			continue;
227*4432Seric 		s = stab(wordbuf, ST_CLASS, ST_ENTER);
228*4432Seric 		s->s_class |= 1 << class;
229*4432Seric 	}
230*4432Seric 
231*4432Seric 	fclose(f);
232*4432Seric }
233*4432Seric /*
2344096Seric **  MAKEMAILER -- define a new mailer.
2354096Seric **
2364096Seric **	Parameters:
2374096Seric **		line -- description of mailer.  This is in tokens
2384096Seric **			separated by white space.  The fields are:
2394096Seric **			* the name of the mailer, as refered to
2404096Seric **			  in the rewriting rules.
2414096Seric **			* the pathname of the program to fork to
2424096Seric **			  execute it.
2434096Seric **			* the options needed by this program.
2444096Seric **			* the macro string needed to translate
2454096Seric **			  a local "from" name to one that can be
2464096Seric **			  returned to this machine.
2474096Seric **			* the argument vector (a series of parameters).
2484217Seric **		safe -- set if this is a safe configuration file.
2494096Seric **
2504096Seric **	Returns:
2514096Seric **		none.
2524096Seric **
2534096Seric **	Side Effects:
2544096Seric **		enters the mailer into the mailer table.
2554096Seric */
2563308Seric 
2574096Seric # define SETWORD \
2584096Seric 		{ \
2594096Seric 			while (*p != '\0' && isspace(*p)) \
2604096Seric 				p++; \
2614096Seric 			q = p; \
2624096Seric 			while (*p != '\0' && !isspace(*p)) \
2634096Seric 				p++; \
2644096Seric 			if (*p != '\0') \
2654096Seric 				*p++ = '\0'; \
2664096Seric 		}
2674096Seric 
2684217Seric makemailer(line, safe)
2694096Seric 	char *line;
2704217Seric 	bool safe;
2714096Seric {
2724096Seric 	register char *p;
2734096Seric 	register char *q;
2744096Seric 	char *mname;
2754096Seric 	char *mpath;
2764096Seric 	int mopts;
2774096Seric 	char *mfrom;
2784096Seric 	register struct mailer *m;
2794096Seric 	char *margv[MAXPV + 1];
2804096Seric 	register int i;
2814096Seric 	extern int NextMailer;
2824096Seric 
2834096Seric 	if (NextMailer >= MAXMAILERS)
2844096Seric 	{
2854096Seric 		syserr("Too many mailers defined");
2864096Seric 		return;
2874096Seric 	}
2884096Seric 
2894096Seric 	/* collect initial information */
2904096Seric 	p = line;
2914096Seric 	SETWORD;
2924096Seric 	mname = q;
2934096Seric 	SETWORD;
2944096Seric 	mpath = q;
2954096Seric 	SETWORD;
2964096Seric 	mopts = crackopts(q);
2974217Seric 	if (!safe)
2984217Seric 		mopts &= ~M_RESTR;
2994096Seric 	SETWORD;
3004096Seric 	mfrom = q;
3014096Seric 
3024096Seric 	if (*p == '\0')
3034096Seric 	{
3044096Seric 		syserr("invalid M line in configuration file");
3054096Seric 		return;
3064096Seric 	}
3074096Seric 
3084096Seric 	/* allocate a mailer */
3094096Seric 	m = (struct mailer *) xalloc(sizeof *m);
3104096Seric 	m->m_name = newstr(mname);
3114096Seric 	m->m_mailer = newstr(mpath);
3124096Seric 	m->m_flags = mopts;
3134096Seric 	m->m_from = newstr(mfrom);
3144096Seric 	m->m_badstat = EX_UNAVAILABLE;
3154096Seric 	m->m_sendq = NULL;
3164096Seric 	Mailer[NextMailer++] = m;
3174096Seric 
3184096Seric 	/* collect the argument vector */
3194096Seric 	for (i = 0; i < MAXPV - 1 && *p != '\0'; i++)
3204096Seric 	{
3214096Seric 		SETWORD;
3224096Seric 		margv[i] = newstr(q);
3234096Seric 	}
3244096Seric 	margv[i++] = NULL;
3254096Seric 
3264096Seric 	/* save the argv */
3274319Seric 	m->m_argv = (char **) xalloc((unsigned) (sizeof margv[0] * i));
3284096Seric 	bmove((char *) margv, (char *) m->m_argv, sizeof margv[0] * i);
3293308Seric }
3303308Seric /*
3313308Seric **  PRINTRULES -- print rewrite rules (for debugging)
3323308Seric **
3333308Seric **	Parameters:
3343308Seric **		none.
3353308Seric **
3363308Seric **	Returns:
3373308Seric **		none.
3383308Seric **
3393308Seric **	Side Effects:
3403308Seric **		prints rewrite rules.
3413308Seric */
3423308Seric 
3434319Seric # ifdef DEBUG
3444319Seric 
3453308Seric printrules()
3463308Seric {
3473308Seric 	register struct rewrite *rwp;
3484072Seric 	register int ruleset;
3493308Seric 
3504072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
3513308Seric 	{
3524072Seric 		if (RewriteRules[ruleset] == NULL)
3534072Seric 			continue;
3544072Seric 		printf("\n----Rule Set %d:\n", ruleset);
3553308Seric 
3564072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
3573308Seric 		{
3584072Seric 			register char **av;
3594072Seric 
3604072Seric 			printf("\n");
3614072Seric 			for (av = rwp->r_lhs; *av != NULL; av++)
3624072Seric 			{
3634072Seric 				xputs(*av);
3644072Seric 				putchar('_');
3654072Seric 			}
3664072Seric 			printf("\n\t");
3674072Seric 			for (av = rwp->r_rhs; *av != NULL; av++)
3684072Seric 			{
3694072Seric 				xputs(*av);
3704072Seric 				putchar('_');
3714072Seric 			}
3724072Seric 			printf("\n");
3733308Seric 		}
3743308Seric 	}
3753308Seric }
3764319Seric 
3774319Seric # endif DEBUG
3784096Seric /*
3794096Seric **  CRACKOPTS -- crack mailer options
3804096Seric **
3814096Seric **	These options modify the functioning of the mailer
3824096Seric **	from the configuration table.
3834096Seric **
3844096Seric **	Parameters:
3854096Seric **		p -- pointer to vector of options.
3864096Seric **
3874096Seric **	Returns:
3884096Seric **		option list in binary.
3894096Seric **
3904096Seric **	Side Effects:
3914096Seric **		none.
3924096Seric */
3934096Seric 
3944096Seric struct optlist
3954096Seric {
3964096Seric 	char	opt_name;	/* external name of option */
3974096Seric 	int	opt_value;	/* internal name of option */
3984096Seric };
3994096Seric struct optlist	OptList[] =
4004096Seric {
4014096Seric 	'f',	M_FOPT,
4024096Seric 	'r',	M_ROPT,
4034096Seric 	'q',	M_QUIET,
4044096Seric 	'S',	M_RESTR,
4054096Seric 	'n',	M_NHDR,
4064197Seric 	'l',	M_LOCAL,
4074096Seric 	's',	M_STRIPQ,
4084096Seric 	'm',	M_MUSER,
4094096Seric 	'F',	M_NEEDFROM,
4104096Seric 	'D',	M_NEEDDATE,
4114096Seric 	'M',	M_MSGID,
4124096Seric 	'u',	M_USR_UPPER,
4134096Seric 	'h',	M_HST_UPPER,
4144096Seric 	'x',	M_FULLNAME,
4154096Seric 	'A',	M_ARPAFMT,
4164319Seric 	'\0',	0
4174096Seric };
4184096Seric 
4194096Seric crackopts(p)
4204096Seric 	register char *p;
4214096Seric {
4224096Seric 	register struct optlist *o;
4234096Seric 	register int opts = 0;
4244096Seric 
4254096Seric 	while (*p != '\0')
4264096Seric 	{
4274096Seric 		for (o = OptList; o->opt_name != '\0' && o->opt_name != *p; o++)
4284096Seric 			continue;
4294096Seric 		if (o->opt_name == '\0')
4304096Seric 			syserr("bad mailer option %c", *p);
4314096Seric 		opts |= o->opt_value;
4324096Seric 		p++;
4334096Seric 	}
4344096Seric 	return (opts);
4354096Seric }
436