13313Seric # include "sendmail.h"
23308Seric 
3*8056Seric SCCSID(@(#)readcf.c	3.29		09/05/82);
43308Seric 
53308Seric /*
63308Seric **  READCF -- read control file.
73308Seric **
83308Seric **	This routine reads the control file and builds the internal
93308Seric **	form.
103308Seric **
114432Seric **	The file is formatted as a sequence of lines, each taken
124432Seric **	atomically.  The first character of each line describes how
134432Seric **	the line is to be interpreted.  The lines are:
144432Seric **		Dxval		Define macro x to have value val.
154432Seric **		Cxword		Put word into class x.
164432Seric **		Fxfile [fmt]	Read file for lines to put into
174432Seric **				class x.  Use scanf string 'fmt'
184432Seric **				or "%s" if not present.  Fmt should
194432Seric **				only produce one string-valued result.
204432Seric **		Hname: value	Define header with field-name 'name'
214432Seric **				and value as specified; this will be
224432Seric **				macro expanded immediately before
234432Seric **				use.
244432Seric **		Sn		Use rewriting set n.
254432Seric **		Rlhs rhs	Rewrite addresses that match lhs to
264432Seric **				be rhs.
274432Seric **		Mn p f r a	Define mailer.  n - internal name,
284432Seric **				p - pathname, f - flags, r - rewriting
294432Seric **				rule for sender, a - argument vector.
304432Seric **
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 
454217Seric readcf(cfname, safe)
463308Seric 	char *cfname;
474217Seric 	bool safe;
483308Seric {
493308Seric 	FILE *cf;
503308Seric 	char buf[MAXLINE];
513308Seric 	register char *p;
523308Seric 	struct rewrite *rwp = NULL;
533308Seric 	extern char **prescan();
543308Seric 	extern char **copyplist();
554061Seric 	int class;
564072Seric 	int ruleset = 0;
575909Seric 	char exbuf[MAXLINE];
587231Seric 	char *q;
593308Seric 
603308Seric 	cf = fopen(cfname, "r");
613308Seric 	if (cf == NULL)
623308Seric 	{
633308Seric 		syserr("cannot open %s", cfname);
643308Seric 		exit(EX_OSFILE);
653308Seric 	}
663308Seric 
67*8056Seric 	LineNumber = 0;
687854Seric 	while (fgetfolded(buf, sizeof buf, cf) != NULL)
693308Seric 	{
703308Seric 		switch (buf[0])
713308Seric 		{
723308Seric 		  case '\0':
733308Seric 		  case '#':		/* comment */
743308Seric 			break;
753308Seric 
763308Seric 		  case 'R':		/* rewriting rule */
773308Seric 			for (p = &buf[1]; *p != '\0' && *p != '\t'; p++)
783308Seric 				continue;
793308Seric 
803308Seric 			if (*p == '\0')
815909Seric 			{
82*8056Seric 				syserr("line %d: invalid rewrite line \"%s\"",
83*8056Seric 					LineNumber, buf);
845909Seric 				break;
855909Seric 			}
865909Seric 
875909Seric 			/* allocate space for the rule header */
885909Seric 			if (rwp == NULL)
895909Seric 			{
905909Seric 				RewriteRules[ruleset] = rwp =
915909Seric 					(struct rewrite *) xalloc(sizeof *rwp);
925909Seric 			}
933308Seric 			else
943308Seric 			{
955909Seric 				rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp);
965909Seric 				rwp = rwp->r_next;
975909Seric 			}
985909Seric 			rwp->r_next = NULL;
993308Seric 
1005909Seric 			/* expand and save the LHS */
1015909Seric 			*p = '\0';
1026991Seric 			expand(&buf[1], exbuf, &exbuf[sizeof exbuf], CurEnv);
1035909Seric 			rwp->r_lhs = prescan(exbuf, '\t');
1045909Seric 			if (rwp->r_lhs != NULL)
1055909Seric 				rwp->r_lhs = copyplist(rwp->r_lhs, TRUE);
1065909Seric 
1075909Seric 			/* expand and save the RHS */
1085909Seric 			while (*++p == '\t')
1095909Seric 				continue;
1107231Seric 			q = p;
1117231Seric 			while (*p != '\0' && *p != '\t')
1127231Seric 				p++;
1137231Seric 			*p = '\0';
1147231Seric 			expand(q, exbuf, &exbuf[sizeof exbuf], CurEnv);
1155909Seric 			rwp->r_rhs = prescan(exbuf, '\t');
1165909Seric 			if (rwp->r_rhs != NULL)
1175909Seric 				rwp->r_rhs = copyplist(rwp->r_rhs, TRUE);
1183308Seric 			break;
1193308Seric 
1204072Seric 		  case 'S':		/* select rewriting set */
1214072Seric 			ruleset = atoi(&buf[1]);
122*8056Seric 			if (ruleset >= MAXRWSETS || ruleset < 0)
123*8056Seric 			{
124*8056Seric 				syserr("readcf: line %d: bad ruleset %d (%d max)",
125*8056Seric 					LineNumber, ruleset, MAXRWSETS);
126*8056Seric 				ruleset = 0;
127*8056Seric 			}
1284072Seric 			rwp = NULL;
1294072Seric 			break;
1304072Seric 
1313308Seric 		  case 'D':		/* macro definition */
1323308Seric 			define(buf[1], newstr(&buf[2]));
1333308Seric 			break;
1343308Seric 
1353387Seric 		  case 'H':		/* required header line */
1364088Seric 			(void) chompheader(&buf[1], TRUE);
1373387Seric 			break;
1383387Seric 
1394061Seric 		  case 'C':		/* word class */
1404432Seric 		  case 'F':		/* word class from file */
1414061Seric 			class = buf[1];
1424061Seric 			if (!isalpha(class))
1434061Seric 				goto badline;
1444061Seric 			if (isupper(class))
1454061Seric 				class -= 'A';
1464061Seric 			else
1474061Seric 				class -= 'a';
1484432Seric 
1494432Seric 			/* read list of words from argument or file */
1504432Seric 			if (buf[0] == 'F')
1514432Seric 			{
1524432Seric 				/* read from file */
1534432Seric 				for (p = &buf[2]; *p != '\0' && !isspace(*p); p++)
1544432Seric 					continue;
1554432Seric 				if (*p == '\0')
1564432Seric 					p = "%s";
1574432Seric 				else
1584432Seric 				{
1594432Seric 					*p = '\0';
1604432Seric 					while (isspace(*++p))
1614432Seric 						continue;
1624432Seric 				}
1634432Seric 				fileclass(class, &buf[2], p);
1644432Seric 				break;
1654432Seric 			}
1664061Seric 
1674432Seric 			/* scan the list of words and set class for all */
1684061Seric 			for (p = &buf[2]; *p != '\0'; )
1694061Seric 			{
1704061Seric 				register char *wd;
1714061Seric 				char delim;
1724061Seric 				register STAB *s;
1734061Seric 
1744061Seric 				while (*p != '\0' && isspace(*p))
1754061Seric 					p++;
1764061Seric 				wd = p;
1774061Seric 				while (*p != '\0' && !isspace(*p))
1784061Seric 					p++;
1794061Seric 				delim = *p;
1804061Seric 				*p = '\0';
1814061Seric 				if (wd[0] != '\0')
1824061Seric 				{
1834103Seric 					s = stab(wd, ST_CLASS, ST_ENTER);
1846275Seric 					s->s_class |= 1L << class;
1854061Seric 				}
1864061Seric 				*p = delim;
1874061Seric 			}
1884061Seric 			break;
1894061Seric 
1904096Seric 		  case 'M':		/* define mailer */
1914217Seric 			makemailer(&buf[1], safe);
1924096Seric 			break;
1934096Seric 
1943308Seric 		  default:
1954061Seric 		  badline:
196*8056Seric 			syserr("readcf: line %d: unknown control line \"%s\"",
197*8056Seric 				LineNumber, buf);
1983308Seric 		}
1993308Seric 	}
2004096Seric }
2014096Seric /*
2024432Seric **  FILECLASS -- read members of a class from a file
2034432Seric **
2044432Seric **	Parameters:
2054432Seric **		class -- class to define.
2064432Seric **		filename -- name of file to read.
2074432Seric **		fmt -- scanf string to use for match.
2084432Seric **
2094432Seric **	Returns:
2104432Seric **		none
2114432Seric **
2124432Seric **	Side Effects:
2134432Seric **
2144432Seric **		puts all lines in filename that match a scanf into
2154432Seric **			the named class.
2164432Seric */
2174432Seric 
2184432Seric fileclass(class, filename, fmt)
2194432Seric 	int class;
2204432Seric 	char *filename;
2214432Seric 	char *fmt;
2224432Seric {
2234432Seric 	register FILE *f;
2244432Seric 	char buf[MAXLINE];
2254432Seric 
2264432Seric 	f = fopen(filename, "r");
2274432Seric 	if (f == NULL)
2284432Seric 	{
2294432Seric 		syserr("cannot open %s", filename);
2304432Seric 		return;
2314432Seric 	}
2324432Seric 
2334432Seric 	while (fgets(buf, sizeof buf, f) != NULL)
2344432Seric 	{
2354432Seric 		register STAB *s;
2364432Seric 		char wordbuf[MAXNAME+1];
2374432Seric 
2384432Seric 		if (sscanf(buf, fmt, wordbuf) != 1)
2394432Seric 			continue;
2404432Seric 		s = stab(wordbuf, ST_CLASS, ST_ENTER);
2416275Seric 		s->s_class |= 1L << class;
2424432Seric 	}
2434432Seric 
2444627Seric 	(void) fclose(f);
2454432Seric }
2464432Seric /*
2474096Seric **  MAKEMAILER -- define a new mailer.
2484096Seric **
2494096Seric **	Parameters:
2504096Seric **		line -- description of mailer.  This is in tokens
2514096Seric **			separated by white space.  The fields are:
2524096Seric **			* the name of the mailer, as refered to
2534096Seric **			  in the rewriting rules.
2544096Seric **			* the pathname of the program to fork to
2554096Seric **			  execute it.
2564096Seric **			* the options needed by this program.
2574096Seric **			* the macro string needed to translate
2584096Seric **			  a local "from" name to one that can be
2594096Seric **			  returned to this machine.
2604096Seric **			* the argument vector (a series of parameters).
2614217Seric **		safe -- set if this is a safe configuration file.
2624096Seric **
2634096Seric **	Returns:
2644096Seric **		none.
2654096Seric **
2664096Seric **	Side Effects:
2674096Seric **		enters the mailer into the mailer table.
2684096Seric */
2693308Seric 
2704096Seric # define SETWORD \
2714096Seric 		{ \
2724096Seric 			while (*p != '\0' && isspace(*p)) \
2734096Seric 				p++; \
2744096Seric 			q = p; \
2754096Seric 			while (*p != '\0' && !isspace(*p)) \
2764096Seric 				p++; \
2774096Seric 			if (*p != '\0') \
2784096Seric 				*p++ = '\0'; \
2794096Seric 		}
2804096Seric 
2814217Seric makemailer(line, safe)
2824096Seric 	char *line;
2834217Seric 	bool safe;
2844096Seric {
2854096Seric 	register char *p;
2864096Seric 	register char *q;
2874096Seric 	char *mname;
2884096Seric 	char *mpath;
2894627Seric 	u_long mopts;
2904627Seric 	extern u_long mfencode();
2914096Seric 	char *mfrom;
2924096Seric 	register struct mailer *m;
2934096Seric 	char *margv[MAXPV + 1];
2944096Seric 	register int i;
2954096Seric 	extern int NextMailer;
2964439Seric 	STAB *s;
2974096Seric 
2984096Seric 	if (NextMailer >= MAXMAILERS)
2994096Seric 	{
300*8056Seric 		syserr("readcf: line %d: too many mailers defined (%d max)",
301*8056Seric 			LineNumber, MAXMAILERS);
3024096Seric 		return;
3034096Seric 	}
3044096Seric 
3054096Seric 	/* collect initial information */
3064096Seric 	p = line;
3074096Seric 	SETWORD;
3084096Seric 	mname = q;
3094096Seric 	SETWORD;
3104096Seric 	mpath = q;
3114096Seric 	SETWORD;
3124627Seric 	mopts = mfencode(q);
3134217Seric 	if (!safe)
3144217Seric 		mopts &= ~M_RESTR;
3154096Seric 	SETWORD;
3164096Seric 	mfrom = q;
3174096Seric 
3184096Seric 	if (*p == '\0')
3194096Seric 	{
320*8056Seric 		syserr("readcf: line %d: invalid M line in configuration file",
321*8056Seric 			LineNumber);
3224096Seric 		return;
3234096Seric 	}
3244096Seric 
3254096Seric 	/* allocate a mailer */
3264096Seric 	m = (struct mailer *) xalloc(sizeof *m);
3274096Seric 	m->m_name = newstr(mname);
3284096Seric 	m->m_mailer = newstr(mpath);
3294096Seric 	m->m_flags = mopts;
3304096Seric 	m->m_from = newstr(mfrom);
3314096Seric 	m->m_badstat = EX_UNAVAILABLE;
3324439Seric 	m->m_mno = NextMailer;
3334096Seric 	Mailer[NextMailer++] = m;
3344096Seric 
3354096Seric 	/* collect the argument vector */
3364096Seric 	for (i = 0; i < MAXPV - 1 && *p != '\0'; i++)
3374096Seric 	{
3384096Seric 		SETWORD;
3394096Seric 		margv[i] = newstr(q);
3404096Seric 	}
3414096Seric 	margv[i++] = NULL;
3424096Seric 
3434096Seric 	/* save the argv */
3447009Seric 	m->m_argv = (char **) xalloc(sizeof margv[0] * i);
3454096Seric 	bmove((char *) margv, (char *) m->m_argv, sizeof margv[0] * i);
3464439Seric 	s = stab(m->m_name, ST_MAILER, ST_ENTER);
3474439Seric 	s->s_mailer = m;
3483308Seric }
3493308Seric /*
3503308Seric **  PRINTRULES -- print rewrite rules (for debugging)
3513308Seric **
3523308Seric **	Parameters:
3533308Seric **		none.
3543308Seric **
3553308Seric **	Returns:
3563308Seric **		none.
3573308Seric **
3583308Seric **	Side Effects:
3593308Seric **		prints rewrite rules.
3603308Seric */
3613308Seric 
3624319Seric # ifdef DEBUG
3634319Seric 
3643308Seric printrules()
3653308Seric {
3663308Seric 	register struct rewrite *rwp;
3674072Seric 	register int ruleset;
3683308Seric 
3694072Seric 	for (ruleset = 0; ruleset < 10; ruleset++)
3703308Seric 	{
3714072Seric 		if (RewriteRules[ruleset] == NULL)
3724072Seric 			continue;
3734072Seric 		printf("\n----Rule Set %d:\n", ruleset);
3743308Seric 
3754072Seric 		for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next)
3763308Seric 		{
3774072Seric 			register char **av;
3784072Seric 
3794072Seric 			printf("\n");
3804072Seric 			for (av = rwp->r_lhs; *av != NULL; av++)
3814072Seric 			{
3824072Seric 				xputs(*av);
3834072Seric 				putchar('_');
3844072Seric 			}
3854072Seric 			printf("\n\t");
3864072Seric 			for (av = rwp->r_rhs; *av != NULL; av++)
3874072Seric 			{
3884072Seric 				xputs(*av);
3894072Seric 				putchar('_');
3904072Seric 			}
3914072Seric 			printf("\n");
3923308Seric 		}
3933308Seric 	}
3943308Seric }
3954319Seric 
3964319Seric # endif DEBUG
3974096Seric /*
3984627Seric **  MFENCODE -- crack mailer options
3994096Seric **
4004096Seric **	These options modify the functioning of the mailer
4014096Seric **	from the configuration table.
4024096Seric **
4034096Seric **	Parameters:
4044096Seric **		p -- pointer to vector of options.
4054096Seric **
4064096Seric **	Returns:
4074096Seric **		option list in binary.
4084096Seric **
4094096Seric **	Side Effects:
4104096Seric **		none.
4114096Seric */
4124096Seric 
4134096Seric struct optlist
4144096Seric {
4154096Seric 	char	opt_name;	/* external name of option */
4166275Seric 	u_long	opt_value;	/* internal name of option */
4174096Seric };
4184096Seric struct optlist	OptList[] =
4194096Seric {
4204096Seric 	'f',	M_FOPT,
4214096Seric 	'r',	M_ROPT,
4224096Seric 	'q',	M_QUIET,
4234096Seric 	'S',	M_RESTR,
4244096Seric 	'n',	M_NHDR,
4254197Seric 	'l',	M_LOCAL,
4264096Seric 	's',	M_STRIPQ,
4274096Seric 	'm',	M_MUSER,
4284096Seric 	'F',	M_NEEDFROM,
4294096Seric 	'D',	M_NEEDDATE,
4304096Seric 	'M',	M_MSGID,
4314096Seric 	'u',	M_USR_UPPER,
4324096Seric 	'h',	M_HST_UPPER,
4334096Seric 	'x',	M_FULLNAME,
4344096Seric 	'A',	M_ARPAFMT,
4355601Seric 	'U',	M_UGLYUUCP,
4365906Seric 	'e',	M_EXPENSIVE,
4376821Seric 	'R',	M_RELRCPT,
4386983Seric 	'X',	M_FULLSMTP,
4394319Seric 	'\0',	0
4404096Seric };
4414096Seric 
4424627Seric u_long
4434627Seric mfencode(p)
4444096Seric 	register char *p;
4454096Seric {
4464096Seric 	register struct optlist *o;
4474627Seric 	register u_long opts = 0;
4484096Seric 
4494096Seric 	while (*p != '\0')
4504096Seric 	{
4514096Seric 		for (o = OptList; o->opt_name != '\0' && o->opt_name != *p; o++)
4524096Seric 			continue;
4534096Seric 		if (o->opt_name == '\0')
4544096Seric 			syserr("bad mailer option %c", *p);
4554096Seric 		opts |= o->opt_value;
4564096Seric 		p++;
4574096Seric 	}
4584096Seric 	return (opts);
4594096Seric }
4604627Seric /*
4614627Seric **  MFDECODE -- decode mailer flags into external form.
4624627Seric **
4634627Seric **	Parameters:
4644627Seric **		flags -- value of flags to decode.
4654627Seric **		f -- file to write them onto.
4664627Seric **
4674627Seric **	Returns:
4684627Seric **		none.
4694627Seric **
4704627Seric **	Side Effects:
4714627Seric **		none.
4724627Seric */
4734627Seric 
4744627Seric mfdecode(flags, f)
4754627Seric 	u_long flags;
4764627Seric 	FILE *f;
4774627Seric {
4784627Seric 	register struct optlist *o;
4794627Seric 
4804627Seric 	putc('?', f);
4814627Seric 	for (o = OptList; o->opt_name != '\0'; o++)
4824627Seric 	{
4834627Seric 		if ((o->opt_value & flags) == o->opt_value)
4844627Seric 		{
4854627Seric 			flags &= ~o->opt_value;
4864627Seric 			putc(o->opt_name, f);
4874627Seric 		}
4884627Seric 	}
4894627Seric 	putc('?', f);
4904627Seric }
491