122976Smiriam /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333730Sbostic  * Copyright (c) 1988 Regents of the University of California.
433730Sbostic  * All rights reserved.
533730Sbostic  *
633730Sbostic  * Redistribution and use in source and binary forms are permitted
734921Sbostic  * provided that the above copyright notice and this paragraph are
834921Sbostic  * duplicated in all such forms and that any documentation,
934921Sbostic  * advertising materials, and other materials related to such
1034921Sbostic  * distribution and use acknowledge that the software was developed
1134921Sbostic  * by the University of California, Berkeley.  The name of the
1234921Sbostic  * University may not be used to endorse or promote products derived
1334921Sbostic  * from this software without specific prior written permission.
1434921Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1534921Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1634921Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1733730Sbostic  */
1822976Smiriam 
1922976Smiriam #ifndef lint
20*36615Sbostic static char sccsid[] = "@(#)parseaddr.c	5.11 (Berkeley) 01/25/89";
2133730Sbostic #endif /* not lint */
2222976Smiriam 
233312Seric # include "sendmail.h"
24297Seric 
25297Seric /*
269888Seric **  PARSEADDR -- Parse an address
27297Seric **
28297Seric **	Parses an address and breaks it up into three parts: a
29297Seric **	net to transmit the message on, the host to transmit it
30297Seric **	to, and a user on that host.  These are loaded into an
312973Seric **	ADDRESS header with the values squirreled away if necessary.
32297Seric **	The "user" part may not be a real user; the process may
33297Seric **	just reoccur on that machine.  For example, on a machine
34297Seric **	with an arpanet connection, the address
35297Seric **		csvax.bill@berkeley
36297Seric **	will break up to a "user" of 'csvax.bill' and a host
37297Seric **	of 'berkeley' -- to be transmitted over the arpanet.
38297Seric **
39297Seric **	Parameters:
40297Seric **		addr -- the address to parse.
41297Seric **		a -- a pointer to the address descriptor buffer.
42297Seric **			If NULL, a header will be created.
43297Seric **		copyf -- determines what shall be copied:
44297Seric **			-1 -- don't copy anything.  The printname
45297Seric **				(q_paddr) is just addr, and the
46297Seric **				user & host are allocated internally
47297Seric **				to parse.
48297Seric **			0 -- copy out the parsed user & host, but
49297Seric **				don't copy the printname.
50297Seric **			+1 -- copy everything.
5111445Seric **		delim -- the character to terminate the address, passed
5211445Seric **			to prescan.
53297Seric **
54297Seric **	Returns:
55297Seric **		A pointer to the address descriptor header (`a' if
56297Seric **			`a' is non-NULL).
57297Seric **		NULL on error.
58297Seric **
59297Seric **	Side Effects:
60297Seric **		none
61297Seric */
62297Seric 
639374Seric /* following delimiters are inherent to the internal algorithms */
6416155Seric # define DELIMCHARS	"\001()<>,;\\\"\r\n"	/* word delimiters */
652091Seric 
662973Seric ADDRESS *
6711445Seric parseaddr(addr, a, copyf, delim)
68297Seric 	char *addr;
692973Seric 	register ADDRESS *a;
70297Seric 	int copyf;
7111445Seric 	char delim;
72297Seric {
733149Seric 	register char **pvp;
743149Seric 	register struct mailer *m;
7516914Seric 	char pvpbuf[PSBUFSIZE];
763149Seric 	extern char **prescan();
773149Seric 	extern ADDRESS *buildaddr();
78297Seric 
79297Seric 	/*
80297Seric 	**  Initialize and prescan address.
81297Seric 	*/
82297Seric 
836903Seric 	CurEnv->e_to = addr;
847675Seric 	if (tTd(20, 1))
859888Seric 		printf("\n--parseaddr(%s)\n", addr);
863188Seric 
8716914Seric 	pvp = prescan(addr, delim, pvpbuf);
883149Seric 	if (pvp == NULL)
89297Seric 		return (NULL);
90297Seric 
91297Seric 	/*
923149Seric 	**  Apply rewriting rules.
937889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
94297Seric 	*/
95297Seric 
968181Seric 	rewrite(pvp, 3);
974070Seric 	rewrite(pvp, 0);
98297Seric 
993149Seric 	/*
1003149Seric 	**  See if we resolved to a real mailer.
1013149Seric 	*/
102297Seric 
1033149Seric 	if (pvp[0][0] != CANONNET)
1043149Seric 	{
1053149Seric 		setstat(EX_USAGE);
1063149Seric 		usrerr("cannot resolve name");
1073149Seric 		return (NULL);
108297Seric 	}
109297Seric 
110297Seric 	/*
1113149Seric 	**  Build canonical address from pvp.
112297Seric 	*/
113297Seric 
1143149Seric 	a = buildaddr(pvp, a);
1154279Seric 	if (a == NULL)
1164279Seric 		return (NULL);
1174598Seric 	m = a->q_mailer;
118297Seric 
119297Seric 	/*
1203149Seric 	**  Make local copies of the host & user and then
1213149Seric 	**  transport them out.
122297Seric 	*/
123297Seric 
124297Seric 	if (copyf > 0)
1258078Seric 	{
1268078Seric 		extern char *DelimChar;
1278078Seric 		char savec = *DelimChar;
1288078Seric 
1298078Seric 		*DelimChar = '\0';
1302973Seric 		a->q_paddr = newstr(addr);
1318078Seric 		*DelimChar = savec;
1328078Seric 	}
133297Seric 	else
134297Seric 		a->q_paddr = addr;
13524944Seric 
13624944Seric 	if (a->q_user == NULL)
13724944Seric 		a->q_user = "";
13824944Seric 	if (a->q_host == NULL)
13924944Seric 		a->q_host = "";
14024944Seric 
1413149Seric 	if (copyf >= 0)
142297Seric 	{
14324944Seric 		a->q_host = newstr(a->q_host);
1443149Seric 		if (a->q_user != a->q_paddr)
1453149Seric 			a->q_user = newstr(a->q_user);
146297Seric 	}
147297Seric 
148297Seric 	/*
14916202Seric 	**  Convert host name to lower case if requested.
15016202Seric 	**	User name will be done later.
15116202Seric 	*/
15216202Seric 
15316202Seric 	if (!bitnset(M_HST_UPPER, m->m_flags))
15416202Seric 		makelower(a->q_host);
15516202Seric 
15616202Seric 	/*
157297Seric 	**  Compute return value.
158297Seric 	*/
159297Seric 
1607675Seric 	if (tTd(20, 1))
1614443Seric 	{
1629888Seric 		printf("parseaddr-->");
1634443Seric 		printaddr(a, FALSE);
1644443Seric 	}
165297Seric 
166297Seric 	return (a);
167297Seric }
168297Seric /*
16916162Seric **  LOWERADDR -- map UPPER->lower case on addresses as requested.
17016162Seric **
17116162Seric **	Parameters:
17216162Seric **		a -- address to be mapped.
17316162Seric **
17416162Seric **	Returns:
17516162Seric **		none.
17616162Seric **
17716162Seric **	Side Effects:
17816162Seric **		none.
17916162Seric */
18016162Seric 
18116162Seric loweraddr(a)
18216162Seric 	register ADDRESS *a;
18316162Seric {
18416162Seric 	register MAILER *m = a->q_mailer;
18516162Seric 
18616162Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
18716162Seric 		makelower(a->q_user);
18816162Seric }
18916162Seric /*
190297Seric **  PRESCAN -- Prescan name and make it canonical
191297Seric **
1929374Seric **	Scans a name and turns it into a set of tokens.  This process
1939374Seric **	deletes blanks and comments (in parentheses).
194297Seric **
195297Seric **	This routine knows about quoted strings and angle brackets.
196297Seric **
197297Seric **	There are certain subtleties to this routine.  The one that
198297Seric **	comes to mind now is that backslashes on the ends of names
199297Seric **	are silently stripped off; this is intentional.  The problem
200297Seric **	is that some versions of sndmsg (like at LBL) set the kill
201297Seric **	character to something other than @ when reading addresses;
202297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
203297Seric **	berknet mailer.
204297Seric **
205297Seric **	Parameters:
206297Seric **		addr -- the name to chomp.
207297Seric **		delim -- the delimiter for the address, normally
208297Seric **			'\0' or ','; \0 is accepted in any case.
20915284Seric **			If '\t' then we are reading the .cf file.
21016914Seric **		pvpbuf -- place to put the saved text -- note that
21116914Seric **			the pointers are static.
212297Seric **
213297Seric **	Returns:
2143149Seric **		A pointer to a vector of tokens.
215297Seric **		NULL on error.
216297Seric **
217297Seric **	Side Effects:
21825279Seric **		sets DelimChar to point to the character matching 'delim'.
219297Seric */
220297Seric 
2218078Seric /* states and character types */
2228078Seric # define OPR		0	/* operator */
2238078Seric # define ATM		1	/* atom */
2248078Seric # define QST		2	/* in quoted string */
2258078Seric # define SPC		3	/* chewing up spaces */
2268078Seric # define ONE		4	/* pick up one character */
2273149Seric 
2288078Seric # define NSTATES	5	/* number of states */
2298078Seric # define TYPE		017	/* mask to select state type */
2308078Seric 
2318078Seric /* meta bits for table */
2328078Seric # define M		020	/* meta character; don't pass through */
2338078Seric # define B		040	/* cause a break */
2348078Seric # define MB		M|B	/* meta-break */
2358078Seric 
2368078Seric static short StateTab[NSTATES][NSTATES] =
2378078Seric {
2388087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2399051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2409051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2419051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2428078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2438078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2448078Seric };
2458078Seric 
2468078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2478078Seric 
2488078Seric char	*DelimChar;		/* set to point to the delimiter */
2498078Seric 
2503149Seric char **
25116914Seric prescan(addr, delim, pvpbuf)
252297Seric 	char *addr;
253297Seric 	char delim;
25416914Seric 	char pvpbuf[];
255297Seric {
256297Seric 	register char *p;
2578078Seric 	register char *q;
2589346Seric 	register int c;
2593149Seric 	char **avp;
260297Seric 	bool bslashmode;
261297Seric 	int cmntcnt;
2628423Seric 	int anglecnt;
2633149Seric 	char *tok;
2648078Seric 	int state;
2658078Seric 	int newstate;
2668078Seric 	static char *av[MAXATOM+1];
26715253Seric 	extern int errno;
268297Seric 
26915253Seric 	/* make sure error messages don't have garbage on them */
27015253Seric 	errno = 0;
27115253Seric 
27216914Seric 	q = pvpbuf;
2733149Seric 	bslashmode = FALSE;
2747800Seric 	cmntcnt = 0;
2758423Seric 	anglecnt = 0;
2763149Seric 	avp = av;
2778078Seric 	state = OPR;
2788078Seric 	c = NOCHAR;
2798078Seric 	p = addr;
2808078Seric 	if (tTd(22, 45))
281297Seric 	{
2828078Seric 		printf("prescan: ");
2838078Seric 		xputs(p);
28423109Seric 		(void) putchar('\n');
2858078Seric 	}
2868078Seric 
2878078Seric 	do
2888078Seric 	{
2893149Seric 		/* read a token */
2903149Seric 		tok = q;
2918078Seric 		for (;;)
292297Seric 		{
2938078Seric 			/* store away any old lookahead character */
2948078Seric 			if (c != NOCHAR)
2958078Seric 			{
29615284Seric 				/* see if there is room */
29716914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
2988078Seric 				{
2998078Seric 					usrerr("Address too long");
3008078Seric 					DelimChar = p;
3018078Seric 					return (NULL);
3028078Seric 				}
30315284Seric 
30415284Seric 				/* squirrel it away */
3058078Seric 				*q++ = c;
3068078Seric 			}
3078078Seric 
3088078Seric 			/* read a new input character */
3098078Seric 			c = *p++;
3108078Seric 			if (c == '\0')
3118078Seric 				break;
31215284Seric 			c &= ~0200;
31315284Seric 
3148078Seric 			if (tTd(22, 101))
3158078Seric 				printf("c=%c, s=%d; ", c, state);
3168078Seric 
3173149Seric 			/* chew up special characters */
3183149Seric 			*q = '\0';
3193149Seric 			if (bslashmode)
3203149Seric 			{
32124944Seric 				/* kludge \! for naive users */
32224944Seric 				if (c != '!')
32324944Seric 					c |= 0200;
3243149Seric 				bslashmode = FALSE;
3253149Seric 			}
3263149Seric 			else if (c == '\\')
3273149Seric 			{
3283149Seric 				bslashmode = TRUE;
3298078Seric 				c = NOCHAR;
3303149Seric 			}
3318514Seric 			else if (state == QST)
3328514Seric 			{
3338514Seric 				/* do nothing, just avoid next clauses */
3348514Seric 			}
3358078Seric 			else if (c == '(')
3364100Seric 			{
3378078Seric 				cmntcnt++;
3388078Seric 				c = NOCHAR;
3394100Seric 			}
3408078Seric 			else if (c == ')')
3413149Seric 			{
3428078Seric 				if (cmntcnt <= 0)
3433149Seric 				{
3448078Seric 					usrerr("Unbalanced ')'");
3458078Seric 					DelimChar = p;
3468078Seric 					return (NULL);
3473149Seric 				}
3488078Seric 				else
3498078Seric 					cmntcnt--;
3508078Seric 			}
3518078Seric 			else if (cmntcnt > 0)
3528078Seric 				c = NOCHAR;
3538423Seric 			else if (c == '<')
3548423Seric 				anglecnt++;
3558423Seric 			else if (c == '>')
3568423Seric 			{
3578423Seric 				if (anglecnt <= 0)
3588423Seric 				{
3598423Seric 					usrerr("Unbalanced '>'");
3608423Seric 					DelimChar = p;
3618423Seric 					return (NULL);
3628423Seric 				}
3638423Seric 				anglecnt--;
3648423Seric 			}
36511423Seric 			else if (delim == ' ' && isspace(c))
36611423Seric 				c = ' ';
3673149Seric 
3688078Seric 			if (c == NOCHAR)
3698078Seric 				continue;
3703149Seric 
3718078Seric 			/* see if this is end of input */
37211405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
3733149Seric 				break;
3743149Seric 
3758078Seric 			newstate = StateTab[state][toktype(c)];
3768078Seric 			if (tTd(22, 101))
3778078Seric 				printf("ns=%02o\n", newstate);
3788078Seric 			state = newstate & TYPE;
3798078Seric 			if (bitset(M, newstate))
3808078Seric 				c = NOCHAR;
3818078Seric 			if (bitset(B, newstate))
3824228Seric 				break;
383297Seric 		}
3843149Seric 
3853149Seric 		/* new token */
3868078Seric 		if (tok != q)
3871378Seric 		{
3888078Seric 			*q++ = '\0';
3898078Seric 			if (tTd(22, 36))
390297Seric 			{
3918078Seric 				printf("tok=");
3928078Seric 				xputs(tok);
39323109Seric 				(void) putchar('\n');
394297Seric 			}
3958078Seric 			if (avp >= &av[MAXATOM])
396297Seric 			{
3978078Seric 				syserr("prescan: too many tokens");
3988078Seric 				DelimChar = p;
3998078Seric 				return (NULL);
400297Seric 			}
4018078Seric 			*avp++ = tok;
402297Seric 		}
4038423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4043149Seric 	*avp = NULL;
4058078Seric 	DelimChar = --p;
4063149Seric 	if (cmntcnt > 0)
4073149Seric 		usrerr("Unbalanced '('");
4088423Seric 	else if (anglecnt > 0)
4098423Seric 		usrerr("Unbalanced '<'");
4108078Seric 	else if (state == QST)
4113149Seric 		usrerr("Unbalanced '\"'");
4123149Seric 	else if (av[0] != NULL)
4133149Seric 		return (av);
4143149Seric 	return (NULL);
4153149Seric }
4163149Seric /*
4173149Seric **  TOKTYPE -- return token type
4183149Seric **
4193149Seric **	Parameters:
4203149Seric **		c -- the character in question.
4213149Seric **
4223149Seric **	Returns:
4233149Seric **		Its type.
4243149Seric **
4253149Seric **	Side Effects:
4263149Seric **		none.
4273149Seric */
428297Seric 
4293149Seric toktype(c)
4303149Seric 	register char c;
4313149Seric {
4323380Seric 	static char buf[50];
4333382Seric 	static bool firstime = TRUE;
4343380Seric 
4353382Seric 	if (firstime)
4363380Seric 	{
4373382Seric 		firstime = FALSE;
43816155Seric 		expand("\001o", buf, &buf[sizeof buf - 1], CurEnv);
4397005Seric 		(void) strcat(buf, DELIMCHARS);
4403380Seric 	}
4419585Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
4428078Seric 		return (ONE);
4438078Seric 	if (c == '"')
4448078Seric 		return (QST);
4454100Seric 	if (!isascii(c))
4468078Seric 		return (ATM);
4478078Seric 	if (isspace(c) || c == ')')
4488078Seric 		return (SPC);
4493380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
4508078Seric 		return (OPR);
4518078Seric 	return (ATM);
4523149Seric }
4533149Seric /*
4543149Seric **  REWRITE -- apply rewrite rules to token vector.
4553149Seric **
4564476Seric **	This routine is an ordered production system.  Each rewrite
4574476Seric **	rule has a LHS (called the pattern) and a RHS (called the
4584476Seric **	rewrite); 'rwr' points the the current rewrite rule.
4594476Seric **
4604476Seric **	For each rewrite rule, 'avp' points the address vector we
4614476Seric **	are trying to match against, and 'pvp' points to the pattern.
4628058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
4639585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
4649585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
4654476Seric **
4664476Seric **	When a match between avp & pvp does not match, we try to
4679585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
4684476Seric **	we must also back out the match in mvp.  If we reach a
4698058Seric **	MATCHANY or MATCHZANY we just extend the match and start
4708058Seric **	over again.
4714476Seric **
4724476Seric **	When we finally match, we rewrite the address vector
4734476Seric **	and try over again.
4744476Seric **
4753149Seric **	Parameters:
4763149Seric **		pvp -- pointer to token vector.
4773149Seric **
4783149Seric **	Returns:
4793149Seric **		none.
4803149Seric **
4813149Seric **	Side Effects:
4823149Seric **		pvp is modified.
4833149Seric */
4842091Seric 
4853149Seric struct match
4863149Seric {
4874468Seric 	char	**first;	/* first token matched */
4884468Seric 	char	**last;		/* last token matched */
4893149Seric };
4903149Seric 
4914468Seric # define MAXMATCH	9	/* max params per rewrite */
4923149Seric 
4933149Seric 
4944070Seric rewrite(pvp, ruleset)
4953149Seric 	char **pvp;
4964070Seric 	int ruleset;
4973149Seric {
4983149Seric 	register char *ap;		/* address pointer */
4993149Seric 	register char *rp;		/* rewrite pointer */
5003149Seric 	register char **avp;		/* address vector pointer */
5013149Seric 	register char **rvp;		/* rewrite vector pointer */
5028058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5038058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
5044468Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5053149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5063149Seric 
5079279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5083149Seric 	{
5098959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
5103149Seric 		printav(pvp);
5113149Seric 	}
5128423Seric 	if (pvp == NULL)
5138423Seric 		return;
5143149Seric 
5153149Seric 	/*
5163149Seric 	**  Run through the list of rewrite rules, applying
5173149Seric 	**	any that match.
5183149Seric 	*/
5193149Seric 
5204070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
5213149Seric 	{
5227675Seric 		if (tTd(21, 12))
523297Seric 		{
5248069Seric 			printf("-----trying rule:");
5253149Seric 			printav(rwr->r_lhs);
5263149Seric 		}
5273149Seric 
5283149Seric 		/* try to match on this rule */
5294468Seric 		mlp = mlist;
5308058Seric 		rvp = rwr->r_lhs;
5318058Seric 		avp = pvp;
5328058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
5333149Seric 		{
5343149Seric 			rp = *rvp;
5358058Seric 			if (tTd(21, 35))
5368058Seric 			{
5378069Seric 				printf("ap=");
5388058Seric 				xputs(ap);
5398069Seric 				printf(", rp=");
5408058Seric 				xputs(rp);
5418069Seric 				printf("\n");
5428058Seric 			}
5433149Seric 			if (rp == NULL)
544297Seric 			{
5453149Seric 				/* end-of-pattern before end-of-address */
5468058Seric 				goto backup;
547297Seric 			}
5488058Seric 			if (ap == NULL && *rp != MATCHZANY)
5498058Seric 			{
5508058Seric 				/* end-of-input */
5518058Seric 				break;
5528058Seric 			}
5533149Seric 
5543149Seric 			switch (*rp)
5553149Seric 			{
5564060Seric 				register STAB *s;
5574060Seric 
5584060Seric 			  case MATCHCLASS:
5599585Seric 			  case MATCHNCLASS:
5609585Seric 				/* match any token in (not in) a class */
5614100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
56210690Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
5639585Seric 				{
5649585Seric 					if (*rp == MATCHCLASS)
5659585Seric 						goto backup;
5669585Seric 				}
5679585Seric 				else if (*rp == MATCHNCLASS)
5688058Seric 					goto backup;
5694468Seric 
5704476Seric 				/* explicit fall-through */
5714476Seric 
5724476Seric 			  case MATCHONE:
5734476Seric 			  case MATCHANY:
5744476Seric 				/* match exactly one token */
5758058Seric 				mlp->first = avp;
5768058Seric 				mlp->last = avp++;
5774468Seric 				mlp++;
5784060Seric 				break;
5794060Seric 
5808058Seric 			  case MATCHZANY:
5818058Seric 				/* match zero or more tokens */
5828058Seric 				mlp->first = avp;
5838058Seric 				mlp->last = avp - 1;
5848058Seric 				mlp++;
5858058Seric 				break;
5868058Seric 
5873149Seric 			  default:
5883149Seric 				/* must have exact match */
58933725Sbostic 				if (strcasecmp(rp, ap))
5908058Seric 					goto backup;
5914468Seric 				avp++;
5923149Seric 				break;
5933149Seric 			}
5943149Seric 
5953149Seric 			/* successful match on this token */
5963149Seric 			rvp++;
5973149Seric 			continue;
5983149Seric 
5998058Seric 		  backup:
6003149Seric 			/* match failed -- back up */
6013149Seric 			while (--rvp >= rwr->r_lhs)
6023149Seric 			{
6033149Seric 				rp = *rvp;
6048058Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
6054468Seric 				{
6064476Seric 					/* extend binding and continue */
6078058Seric 					avp = ++mlp[-1].last;
6088058Seric 					avp++;
6094476Seric 					rvp++;
6103149Seric 					break;
6114468Seric 				}
6124476Seric 				avp--;
6139585Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS ||
6149585Seric 				    *rp == MATCHNCLASS)
6153149Seric 				{
6164468Seric 					/* back out binding */
6174468Seric 					mlp--;
6183149Seric 				}
6193149Seric 			}
6203149Seric 
6213149Seric 			if (rvp < rwr->r_lhs)
6223149Seric 			{
6233149Seric 				/* total failure to match */
6243149Seric 				break;
6253149Seric 			}
626297Seric 		}
6273149Seric 
6283149Seric 		/*
6293149Seric 		**  See if we successfully matched
6303149Seric 		*/
6313149Seric 
6329374Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
6333149Seric 		{
6349374Seric 			if (tTd(21, 10))
6359374Seric 				printf("----- rule fails\n");
6369374Seric 			rwr = rwr->r_next;
6379374Seric 			continue;
6389374Seric 		}
6393149Seric 
6409374Seric 		rvp = rwr->r_rhs;
6419374Seric 		if (tTd(21, 12))
6429374Seric 		{
6439374Seric 			printf("-----rule matches:");
6449374Seric 			printav(rvp);
6459374Seric 		}
6469374Seric 
6479374Seric 		rp = *rvp;
6489374Seric 		if (*rp == CANONUSER)
6499374Seric 		{
6509374Seric 			rvp++;
6519374Seric 			rwr = rwr->r_next;
6529374Seric 		}
6539374Seric 		else if (*rp == CANONHOST)
6549374Seric 		{
6559374Seric 			rvp++;
6569374Seric 			rwr = NULL;
6579374Seric 		}
6589374Seric 		else if (*rp == CANONNET)
6599374Seric 			rwr = NULL;
6609374Seric 
6619374Seric 		/* substitute */
6629374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
6639374Seric 		{
6649374Seric 			register struct match *m;
6659374Seric 			register char **pp;
6669374Seric 
6678058Seric 			rp = *rvp;
66816914Seric 			if (*rp == MATCHREPL)
6698058Seric 			{
67016914Seric 				/* substitute from LHS */
67116914Seric 				m = &mlist[rp[1] - '1'];
67216914Seric 				if (m >= mlp)
6739374Seric 				{
67416914Seric 					syserr("rewrite: ruleset %d: replacement out of bounds", ruleset);
6759374Seric 					return;
6769374Seric 				}
67716914Seric 				if (tTd(21, 15))
67816914Seric 				{
67916914Seric 					printf("$%c:", rp[1]);
68016914Seric 					pp = m->first;
68116914Seric 					while (pp <= m->last)
68216914Seric 					{
68316914Seric 						printf(" %x=\"", *pp);
68416914Seric 						(void) fflush(stdout);
68516914Seric 						printf("%s\"", *pp++);
68616914Seric 					}
68716914Seric 					printf("\n");
68816914Seric 				}
6899374Seric 				pp = m->first;
6909374Seric 				while (pp <= m->last)
6913149Seric 				{
69216914Seric 					if (avp >= &npvp[MAXATOM])
69316914Seric 					{
69416914Seric 						syserr("rewrite: expansion too long");
69516914Seric 						return;
69616914Seric 					}
69716914Seric 					*avp++ = *pp++;
6983149Seric 				}
6993149Seric 			}
70016914Seric 			else
7018226Seric 			{
70216914Seric 				/* vanilla replacement */
7039374Seric 				if (avp >= &npvp[MAXATOM])
70416889Seric 				{
70516914Seric 	toolong:
70616889Seric 					syserr("rewrite: expansion too long");
70716889Seric 					return;
70816889Seric 				}
70916914Seric 				*avp++ = rp;
7108226Seric 			}
7119374Seric 		}
7129374Seric 		*avp++ = NULL;
71316914Seric 
71416914Seric 		/*
71516914Seric 		**  Check for any hostname lookups.
71616914Seric 		*/
71716914Seric 
71816914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
71916914Seric 		{
72016914Seric 			char **hbrvp;
72116914Seric 			char **xpvp;
72216914Seric 			int trsize;
72317473Seric 			char *olddelimchar;
72416920Seric 			char buf[MAXNAME + 1];
72516914Seric 			char *pvpb1[MAXATOM + 1];
72617174Seric 			char pvpbuf[PSBUFSIZE];
72717473Seric 			extern char *DelimChar;
72816914Seric 
72916914Seric 			if (**rvp != HOSTBEGIN)
73016914Seric 				continue;
73116914Seric 
73216914Seric 			/*
73316914Seric 			**  Got a hostname lookup.
73416914Seric 			**
73516914Seric 			**	This could be optimized fairly easily.
73616914Seric 			*/
73716914Seric 
73816914Seric 			hbrvp = rvp;
73916914Seric 
74016914Seric 			/* extract the match part */
74116914Seric 			while (*++rvp != NULL && **rvp != HOSTEND)
74216914Seric 				continue;
74316914Seric 			if (*rvp != NULL)
74416914Seric 				*rvp++ = NULL;
74516914Seric 
74616914Seric 			/* save the remainder of the input string */
74716914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
74816914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
74916914Seric 
75016914Seric 			/* look it up */
75116914Seric 			cataddr(++hbrvp, buf, sizeof buf);
75216914Seric 			maphostname(buf, sizeof buf);
75316914Seric 
75416914Seric 			/* scan the new host name */
75517473Seric 			olddelimchar = DelimChar;
75616914Seric 			xpvp = prescan(buf, '\0', pvpbuf);
75717473Seric 			DelimChar = olddelimchar;
75816914Seric 			if (xpvp == NULL)
75916914Seric 			{
76016914Seric 				syserr("rewrite: cannot prescan canonical hostname: %s", buf);
76122976Smiriam 				return;
76216914Seric 			}
76316914Seric 
76416914Seric 			/* append it to the token list */
76517174Seric 			for (avp = --hbrvp; *xpvp != NULL; xpvp++)
76617174Seric 			{
76717174Seric 				*avp++ = newstr(*xpvp);
76816920Seric 				if (avp >= &npvp[MAXATOM])
76916914Seric 					goto toolong;
77017174Seric 			}
77116914Seric 
77216914Seric 			/* restore the old trailing information */
77317177Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
77416920Seric 				if (avp >= &npvp[MAXATOM])
77516914Seric 					goto toolong;
77617174Seric 
77717174Seric 			break;
77816914Seric 		}
77916914Seric 
78016914Seric 		/*
78116914Seric 		**  Check for subroutine calls.
78216914Seric 		*/
78316914Seric 
78424944Seric 		if (*npvp != NULL && **npvp == CALLSUBR)
7859374Seric 		{
78616889Seric 			bcopy((char *) &npvp[2], (char *) pvp,
78716900Seric 				(int) (avp - npvp - 2) * sizeof *avp);
78816889Seric 			if (tTd(21, 3))
78916889Seric 				printf("-----callsubr %s\n", npvp[1]);
79016889Seric 			rewrite(pvp, atoi(npvp[1]));
7913149Seric 		}
7923149Seric 		else
7933149Seric 		{
79417348Seric 			bcopy((char *) npvp, (char *) pvp,
79516900Seric 				(int) (avp - npvp) * sizeof *avp);
7969374Seric 		}
7979374Seric 		if (tTd(21, 4))
7989374Seric 		{
7999374Seric 			printf("rewritten as:");
8009374Seric 			printav(pvp);
8019374Seric 		}
802297Seric 	}
8038069Seric 
8049279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
8058069Seric 	{
8068959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
8078069Seric 		printav(pvp);
8088069Seric 	}
8093149Seric }
8103149Seric /*
8113149Seric **  BUILDADDR -- build address from token vector.
8123149Seric **
8133149Seric **	Parameters:
8143149Seric **		tv -- token vector.
8153149Seric **		a -- pointer to address descriptor to fill.
8163149Seric **			If NULL, one will be allocated.
8173149Seric **
8183149Seric **	Returns:
8194279Seric **		NULL if there was an error.
8204279Seric **		'a' otherwise.
8213149Seric **
8223149Seric **	Side Effects:
8233149Seric **		fills in 'a'
8243149Seric */
8253149Seric 
8263149Seric ADDRESS *
8273149Seric buildaddr(tv, a)
8283149Seric 	register char **tv;
8293149Seric 	register ADDRESS *a;
8303149Seric {
8313149Seric 	static char buf[MAXNAME];
8323149Seric 	struct mailer **mp;
8333149Seric 	register struct mailer *m;
8343149Seric 
8353149Seric 	if (a == NULL)
8363149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
83716889Seric 	bzero((char *) a, sizeof *a);
8383149Seric 
8393149Seric 	/* figure out what net/mailer to use */
8403149Seric 	if (**tv != CANONNET)
8414279Seric 	{
8423149Seric 		syserr("buildaddr: no net");
8434279Seric 		return (NULL);
8444279Seric 	}
8453149Seric 	tv++;
84633725Sbostic 	if (!strcasecmp(*tv, "error"))
8474279Seric 	{
84810183Seric 		if (**++tv == CANONHOST)
84910183Seric 		{
85010183Seric 			setstat(atoi(*++tv));
85110183Seric 			tv++;
85210183Seric 		}
85310183Seric 		if (**tv != CANONUSER)
8544279Seric 			syserr("buildaddr: error: no user");
8554279Seric 		buf[0] = '\0';
8564279Seric 		while (*++tv != NULL)
8574279Seric 		{
8584279Seric 			if (buf[0] != '\0')
8597005Seric 				(void) strcat(buf, " ");
8607005Seric 			(void) strcat(buf, *tv);
8614279Seric 		}
8624279Seric 		usrerr(buf);
8634279Seric 		return (NULL);
8644279Seric 	}
8654598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
8663149Seric 	{
86733725Sbostic 		if (!strcasecmp(m->m_name, *tv))
8683149Seric 			break;
8693149Seric 	}
8703149Seric 	if (m == NULL)
8714279Seric 	{
87224944Seric 		syserr("buildaddr: unknown mailer %s", *tv);
8734279Seric 		return (NULL);
8744279Seric 	}
8754598Seric 	a->q_mailer = m;
8763149Seric 
8773149Seric 	/* figure out what host (if any) */
8783149Seric 	tv++;
87910690Seric 	if (!bitnset(M_LOCAL, m->m_flags))
8803149Seric 	{
8815704Seric 		if (**tv++ != CANONHOST)
8824279Seric 		{
8833149Seric 			syserr("buildaddr: no host");
8844279Seric 			return (NULL);
8854279Seric 		}
8865704Seric 		buf[0] = '\0';
8875704Seric 		while (*tv != NULL && **tv != CANONUSER)
8887005Seric 			(void) strcat(buf, *tv++);
8895704Seric 		a->q_host = newstr(buf);
8903149Seric 	}
8913149Seric 	else
8923149Seric 		a->q_host = NULL;
8933149Seric 
8943149Seric 	/* figure out the user */
895*36615Sbostic 	if (*tv == NULL || **tv != CANONUSER)
8964279Seric 	{
8973149Seric 		syserr("buildaddr: no user");
8984279Seric 		return (NULL);
8994279Seric 	}
90019040Seric 
90119040Seric 	/* rewrite according recipient mailer rewriting rules */
90219040Seric 	rewrite(++tv, 2);
90319040Seric 	if (m->m_r_rwset > 0)
90419040Seric 		rewrite(tv, m->m_r_rwset);
90519040Seric 	rewrite(tv, 4);
90619040Seric 
90719040Seric 	/* save the result for the command line/RCPT argument */
90811278Seric 	cataddr(tv, buf, sizeof buf);
9093149Seric 	a->q_user = buf;
9103149Seric 
9113149Seric 	return (a);
9123149Seric }
9133188Seric /*
9144228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
9154228Seric **
9164228Seric **	Parameters:
9174228Seric **		pvp -- parameter vector to rebuild.
9184228Seric **		buf -- buffer to build the string into.
9194228Seric **		sz -- size of buf.
9204228Seric **
9214228Seric **	Returns:
9224228Seric **		none.
9234228Seric **
9244228Seric **	Side Effects:
9254228Seric **		Destroys buf.
9264228Seric */
9274228Seric 
9284228Seric cataddr(pvp, buf, sz)
9294228Seric 	char **pvp;
9304228Seric 	char *buf;
9314228Seric 	register int sz;
9324228Seric {
9334228Seric 	bool oatomtok = FALSE;
9344228Seric 	bool natomtok = FALSE;
9354228Seric 	register int i;
9364228Seric 	register char *p;
9374228Seric 
9388423Seric 	if (pvp == NULL)
9398423Seric 	{
94023109Seric 		(void) strcpy(buf, "");
9418423Seric 		return;
9428423Seric 	}
9434228Seric 	p = buf;
94411156Seric 	sz -= 2;
9454228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
9464228Seric 	{
9478078Seric 		natomtok = (toktype(**pvp) == ATM);
9484228Seric 		if (oatomtok && natomtok)
9499042Seric 			*p++ = SpaceSub;
9504228Seric 		(void) strcpy(p, *pvp);
9514228Seric 		oatomtok = natomtok;
9524228Seric 		p += i;
95311156Seric 		sz -= i + 1;
9544228Seric 		pvp++;
9554228Seric 	}
9564228Seric 	*p = '\0';
9574228Seric }
9584228Seric /*
9593188Seric **  SAMEADDR -- Determine if two addresses are the same
9603188Seric **
9613188Seric **	This is not just a straight comparison -- if the mailer doesn't
9623188Seric **	care about the host we just ignore it, etc.
9633188Seric **
9643188Seric **	Parameters:
9653188Seric **		a, b -- pointers to the internal forms to compare.
9663188Seric **
9673188Seric **	Returns:
9683188Seric **		TRUE -- they represent the same mailbox.
9693188Seric **		FALSE -- they don't.
9703188Seric **
9713188Seric **	Side Effects:
9723188Seric **		none.
9733188Seric */
9743188Seric 
9753188Seric bool
9769374Seric sameaddr(a, b)
9773188Seric 	register ADDRESS *a;
9783188Seric 	register ADDRESS *b;
9793188Seric {
9803188Seric 	/* if they don't have the same mailer, forget it */
9813188Seric 	if (a->q_mailer != b->q_mailer)
9823188Seric 		return (FALSE);
9833188Seric 
9843188Seric 	/* if the user isn't the same, we can drop out */
9859374Seric 	if (strcmp(a->q_user, b->q_user) != 0)
9863188Seric 		return (FALSE);
9873188Seric 
9883188Seric 	/* if the mailer ignores hosts, we have succeeded! */
98910690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
9903188Seric 		return (TRUE);
9913188Seric 
9923188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
9933188Seric 	if (a->q_host == NULL || b->q_host == NULL)
9943188Seric 		return (FALSE);
9953188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
9963188Seric 		return (FALSE);
9973188Seric 
9983188Seric 	return (TRUE);
9993188Seric }
10003234Seric /*
10013234Seric **  PRINTADDR -- print address (for debugging)
10023234Seric **
10033234Seric **	Parameters:
10043234Seric **		a -- the address to print
10053234Seric **		follow -- follow the q_next chain.
10063234Seric **
10073234Seric **	Returns:
10083234Seric **		none.
10093234Seric **
10103234Seric **	Side Effects:
10113234Seric **		none.
10123234Seric */
10133234Seric 
10143234Seric printaddr(a, follow)
10153234Seric 	register ADDRESS *a;
10163234Seric 	bool follow;
10173234Seric {
10185001Seric 	bool first = TRUE;
10195001Seric 
10203234Seric 	while (a != NULL)
10213234Seric 	{
10225001Seric 		first = FALSE;
10234443Seric 		printf("%x=", a);
10244085Seric 		(void) fflush(stdout);
10253234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
10268181Seric 		       a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host,
10278181Seric 		       a->q_user);
10288181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
10298181Seric 		       a->q_alias);
10308181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
10318181Seric 		       a->q_fullname);
10324996Seric 
10333234Seric 		if (!follow)
10343234Seric 			return;
10354996Seric 		a = a->q_next;
10363234Seric 	}
10375001Seric 	if (first)
10384443Seric 		printf("[NULL]\n");
10393234Seric }
10404317Seric 
10417682Seric /*
10427682Seric **  REMOTENAME -- return the name relative to the current mailer
10437682Seric **
10447682Seric **	Parameters:
10457682Seric **		name -- the name to translate.
10468069Seric **		m -- the mailer that we want to do rewriting relative
10478069Seric **			to.
10488069Seric **		senderaddress -- if set, uses the sender rewriting rules
10498069Seric **			rather than the recipient rewriting rules.
105010310Seric **		canonical -- if set, strip out any comment information,
105110310Seric **			etc.
10527682Seric **
10537682Seric **	Returns:
10547682Seric **		the text string representing this address relative to
10557682Seric **			the receiving mailer.
10567682Seric **
10577682Seric **	Side Effects:
10587682Seric **		none.
10597682Seric **
10607682Seric **	Warnings:
10617682Seric **		The text string returned is tucked away locally;
10627682Seric **			copy it if you intend to save it.
10637682Seric */
10647682Seric 
10657682Seric char *
106610310Seric remotename(name, m, senderaddress, canonical)
10677682Seric 	char *name;
10687682Seric 	struct mailer *m;
10698069Seric 	bool senderaddress;
107010310Seric 	bool canonical;
10717682Seric {
10728069Seric 	register char **pvp;
10738069Seric 	char *fancy;
10748069Seric 	extern char *macvalue();
10758181Seric 	char *oldg = macvalue('g', CurEnv);
10767682Seric 	static char buf[MAXNAME];
10777682Seric 	char lbuf[MAXNAME];
107816914Seric 	char pvpbuf[PSBUFSIZE];
10797682Seric 	extern char **prescan();
10807889Seric 	extern char *crackaddr();
10817682Seric 
10827755Seric 	if (tTd(12, 1))
10837755Seric 		printf("remotename(%s)\n", name);
10847755Seric 
108510177Seric 	/* don't do anything if we are tagging it as special */
108610177Seric 	if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0)
108710177Seric 		return (name);
108810177Seric 
10897682Seric 	/*
10908181Seric 	**  Do a heuristic crack of this name to extract any comment info.
10918181Seric 	**	This will leave the name as a comment and a $g macro.
10927889Seric 	*/
10937889Seric 
109410310Seric 	if (canonical)
109516155Seric 		fancy = "\001g";
109610310Seric 	else
109710310Seric 		fancy = crackaddr(name);
10987889Seric 
10998181Seric 	/*
11008181Seric 	**  Turn the name into canonical form.
11018181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
11028181Seric 	**	If this only resolves to "user", and the "C" flag is
11038181Seric 	**	specified in the sending mailer, then the sender's
11048181Seric 	**	domain will be appended.
11058181Seric 	*/
11068181Seric 
110716914Seric 	pvp = prescan(name, '\0', pvpbuf);
11087889Seric 	if (pvp == NULL)
11097889Seric 		return (name);
11108181Seric 	rewrite(pvp, 3);
11118181Seric 	if (CurEnv->e_fromdomain != NULL)
11128181Seric 	{
11138181Seric 		/* append from domain to this address */
11148181Seric 		register char **pxp = pvp;
11158181Seric 
11169594Seric 		/* see if there is an "@domain" in the current name */
11178181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
11188181Seric 			pxp++;
11198181Seric 		if (*pxp == NULL)
11208181Seric 		{
11219594Seric 			/* no.... append the "@domain" from the sender */
11228181Seric 			register char **qxq = CurEnv->e_fromdomain;
11238181Seric 
11249594Seric 			while ((*pxp++ = *qxq++) != NULL)
11259594Seric 				continue;
112611726Seric 			rewrite(pvp, 3);
11278181Seric 		}
11288181Seric 	}
11298181Seric 
11308181Seric 	/*
11318959Seric 	**  Do more specific rewriting.
11328181Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
11338181Seric 	**		a sender address or not.
11348181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
11358181Seric 	*/
11368181Seric 
11378069Seric 	if (senderaddress)
11387755Seric 	{
11397889Seric 		rewrite(pvp, 1);
11408069Seric 		if (m->m_s_rwset > 0)
11418069Seric 			rewrite(pvp, m->m_s_rwset);
11428069Seric 	}
11438069Seric 	else
11448069Seric 	{
11457889Seric 		rewrite(pvp, 2);
11468069Seric 		if (m->m_r_rwset > 0)
11478069Seric 			rewrite(pvp, m->m_r_rwset);
11487682Seric 	}
11497682Seric 
11508181Seric 	/*
11518959Seric 	**  Do any final sanitation the address may require.
11528959Seric 	**	This will normally be used to turn internal forms
11538959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
11548959Seric 	**	may be used as a default to the above rules.
11558959Seric 	*/
11568959Seric 
11578959Seric 	rewrite(pvp, 4);
11588959Seric 
11598959Seric 	/*
11608181Seric 	**  Now restore the comment information we had at the beginning.
11618181Seric 	*/
11628181Seric 
11637682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
11649374Seric 	define('g', lbuf, CurEnv);
11657889Seric 	expand(fancy, buf, &buf[sizeof buf - 1], CurEnv);
11669374Seric 	define('g', oldg, CurEnv);
11677682Seric 
11687682Seric 	if (tTd(12, 1))
11697755Seric 		printf("remotename => `%s'\n", buf);
11707682Seric 	return (buf);
11717682Seric }
1172