122976Smiriam 
222976Smiriam /*
322976Smiriam **  Sendmail
422976Smiriam **  Copyright (c) 1983  Eric P. Allman
522976Smiriam **  Berkeley, California
622976Smiriam **
722976Smiriam **  Copyright (c) 1983 Regents of the University of California.
822976Smiriam **  All rights reserved.  The Berkeley software License Agreement
922976Smiriam **  specifies the terms and conditions for redistribution.
1022976Smiriam */
1122976Smiriam 
1222976Smiriam #ifndef lint
13*25279Seric static char	SccsId[] = "@(#)parseaddr.c	5.3 (Berkeley) 10/24/85";
1422976Smiriam #endif not lint
1522976Smiriam 
163312Seric # include "sendmail.h"
17297Seric 
18297Seric /*
199888Seric **  PARSEADDR -- Parse an address
20297Seric **
21297Seric **	Parses an address and breaks it up into three parts: a
22297Seric **	net to transmit the message on, the host to transmit it
23297Seric **	to, and a user on that host.  These are loaded into an
242973Seric **	ADDRESS header with the values squirreled away if necessary.
25297Seric **	The "user" part may not be a real user; the process may
26297Seric **	just reoccur on that machine.  For example, on a machine
27297Seric **	with an arpanet connection, the address
28297Seric **		csvax.bill@berkeley
29297Seric **	will break up to a "user" of 'csvax.bill' and a host
30297Seric **	of 'berkeley' -- to be transmitted over the arpanet.
31297Seric **
32297Seric **	Parameters:
33297Seric **		addr -- the address to parse.
34297Seric **		a -- a pointer to the address descriptor buffer.
35297Seric **			If NULL, a header will be created.
36297Seric **		copyf -- determines what shall be copied:
37297Seric **			-1 -- don't copy anything.  The printname
38297Seric **				(q_paddr) is just addr, and the
39297Seric **				user & host are allocated internally
40297Seric **				to parse.
41297Seric **			0 -- copy out the parsed user & host, but
42297Seric **				don't copy the printname.
43297Seric **			+1 -- copy everything.
4411445Seric **		delim -- the character to terminate the address, passed
4511445Seric **			to prescan.
46297Seric **
47297Seric **	Returns:
48297Seric **		A pointer to the address descriptor header (`a' if
49297Seric **			`a' is non-NULL).
50297Seric **		NULL on error.
51297Seric **
52297Seric **	Side Effects:
53297Seric **		none
54297Seric */
55297Seric 
569374Seric /* following delimiters are inherent to the internal algorithms */
5716155Seric # define DELIMCHARS	"\001()<>,;\\\"\r\n"	/* word delimiters */
582091Seric 
592973Seric ADDRESS *
6011445Seric parseaddr(addr, a, copyf, delim)
61297Seric 	char *addr;
622973Seric 	register ADDRESS *a;
63297Seric 	int copyf;
6411445Seric 	char delim;
65297Seric {
663149Seric 	register char **pvp;
673149Seric 	register struct mailer *m;
6816914Seric 	char pvpbuf[PSBUFSIZE];
693149Seric 	extern char **prescan();
703149Seric 	extern ADDRESS *buildaddr();
71297Seric 
72297Seric 	/*
73297Seric 	**  Initialize and prescan address.
74297Seric 	*/
75297Seric 
766903Seric 	CurEnv->e_to = addr;
773188Seric # ifdef DEBUG
787675Seric 	if (tTd(20, 1))
799888Seric 		printf("\n--parseaddr(%s)\n", addr);
803188Seric # endif DEBUG
813188Seric 
8216914Seric 	pvp = prescan(addr, delim, pvpbuf);
833149Seric 	if (pvp == NULL)
84297Seric 		return (NULL);
85297Seric 
86297Seric 	/*
873149Seric 	**  Apply rewriting rules.
887889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
89297Seric 	*/
90297Seric 
918181Seric 	rewrite(pvp, 3);
924070Seric 	rewrite(pvp, 0);
93297Seric 
943149Seric 	/*
953149Seric 	**  See if we resolved to a real mailer.
963149Seric 	*/
97297Seric 
983149Seric 	if (pvp[0][0] != CANONNET)
993149Seric 	{
1003149Seric 		setstat(EX_USAGE);
1013149Seric 		usrerr("cannot resolve name");
1023149Seric 		return (NULL);
103297Seric 	}
104297Seric 
105297Seric 	/*
1063149Seric 	**  Build canonical address from pvp.
107297Seric 	*/
108297Seric 
1093149Seric 	a = buildaddr(pvp, a);
1104279Seric 	if (a == NULL)
1114279Seric 		return (NULL);
1124598Seric 	m = a->q_mailer;
113297Seric 
114297Seric 	/*
1153149Seric 	**  Make local copies of the host & user and then
1163149Seric 	**  transport them out.
117297Seric 	*/
118297Seric 
119297Seric 	if (copyf > 0)
1208078Seric 	{
1218078Seric 		extern char *DelimChar;
1228078Seric 		char savec = *DelimChar;
1238078Seric 
1248078Seric 		*DelimChar = '\0';
1252973Seric 		a->q_paddr = newstr(addr);
1268078Seric 		*DelimChar = savec;
1278078Seric 	}
128297Seric 	else
129297Seric 		a->q_paddr = addr;
13024944Seric 
13124944Seric 	if (a->q_user == NULL)
13224944Seric 		a->q_user = "";
13324944Seric 	if (a->q_host == NULL)
13424944Seric 		a->q_host = "";
13524944Seric 
1363149Seric 	if (copyf >= 0)
137297Seric 	{
13824944Seric 		a->q_host = newstr(a->q_host);
1393149Seric 		if (a->q_user != a->q_paddr)
1403149Seric 			a->q_user = newstr(a->q_user);
141297Seric 	}
142297Seric 
143297Seric 	/*
14416202Seric 	**  Convert host name to lower case if requested.
14516202Seric 	**	User name will be done later.
14616202Seric 	*/
14716202Seric 
14816202Seric 	if (!bitnset(M_HST_UPPER, m->m_flags))
14916202Seric 		makelower(a->q_host);
15016202Seric 
15116202Seric 	/*
152297Seric 	**  Compute return value.
153297Seric 	*/
154297Seric 
155297Seric # ifdef DEBUG
1567675Seric 	if (tTd(20, 1))
1574443Seric 	{
1589888Seric 		printf("parseaddr-->");
1594443Seric 		printaddr(a, FALSE);
1604443Seric 	}
161297Seric # endif DEBUG
162297Seric 
163297Seric 	return (a);
164297Seric }
165297Seric /*
16616162Seric **  LOWERADDR -- map UPPER->lower case on addresses as requested.
16716162Seric **
16816162Seric **	Parameters:
16916162Seric **		a -- address to be mapped.
17016162Seric **
17116162Seric **	Returns:
17216162Seric **		none.
17316162Seric **
17416162Seric **	Side Effects:
17516162Seric **		none.
17616162Seric */
17716162Seric 
17816162Seric loweraddr(a)
17916162Seric 	register ADDRESS *a;
18016162Seric {
18116162Seric 	register MAILER *m = a->q_mailer;
18216162Seric 
18316162Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
18416162Seric 		makelower(a->q_user);
18516162Seric }
18616162Seric /*
187297Seric **  PRESCAN -- Prescan name and make it canonical
188297Seric **
1899374Seric **	Scans a name and turns it into a set of tokens.  This process
1909374Seric **	deletes blanks and comments (in parentheses).
191297Seric **
192297Seric **	This routine knows about quoted strings and angle brackets.
193297Seric **
194297Seric **	There are certain subtleties to this routine.  The one that
195297Seric **	comes to mind now is that backslashes on the ends of names
196297Seric **	are silently stripped off; this is intentional.  The problem
197297Seric **	is that some versions of sndmsg (like at LBL) set the kill
198297Seric **	character to something other than @ when reading addresses;
199297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
200297Seric **	berknet mailer.
201297Seric **
202297Seric **	Parameters:
203297Seric **		addr -- the name to chomp.
204297Seric **		delim -- the delimiter for the address, normally
205297Seric **			'\0' or ','; \0 is accepted in any case.
20615284Seric **			If '\t' then we are reading the .cf file.
20716914Seric **		pvpbuf -- place to put the saved text -- note that
20816914Seric **			the pointers are static.
209297Seric **
210297Seric **	Returns:
2113149Seric **		A pointer to a vector of tokens.
212297Seric **		NULL on error.
213297Seric **
214297Seric **	Side Effects:
215*25279Seric **		sets DelimChar to point to the character matching 'delim'.
216297Seric */
217297Seric 
2188078Seric /* states and character types */
2198078Seric # define OPR		0	/* operator */
2208078Seric # define ATM		1	/* atom */
2218078Seric # define QST		2	/* in quoted string */
2228078Seric # define SPC		3	/* chewing up spaces */
2238078Seric # define ONE		4	/* pick up one character */
2243149Seric 
2258078Seric # define NSTATES	5	/* number of states */
2268078Seric # define TYPE		017	/* mask to select state type */
2278078Seric 
2288078Seric /* meta bits for table */
2298078Seric # define M		020	/* meta character; don't pass through */
2308078Seric # define B		040	/* cause a break */
2318078Seric # define MB		M|B	/* meta-break */
2328078Seric 
2338078Seric static short StateTab[NSTATES][NSTATES] =
2348078Seric {
2358087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2369051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2379051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2389051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2398078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2408078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2418078Seric };
2428078Seric 
2438078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2448078Seric 
2458078Seric char	*DelimChar;		/* set to point to the delimiter */
2468078Seric 
2473149Seric char **
24816914Seric prescan(addr, delim, pvpbuf)
249297Seric 	char *addr;
250297Seric 	char delim;
25116914Seric 	char pvpbuf[];
252297Seric {
253297Seric 	register char *p;
2548078Seric 	register char *q;
2559346Seric 	register int c;
2563149Seric 	char **avp;
257297Seric 	bool bslashmode;
258297Seric 	int cmntcnt;
2598423Seric 	int anglecnt;
2603149Seric 	char *tok;
2618078Seric 	int state;
2628078Seric 	int newstate;
2638078Seric 	static char *av[MAXATOM+1];
26415253Seric 	extern int errno;
265297Seric 
26615253Seric 	/* make sure error messages don't have garbage on them */
26715253Seric 	errno = 0;
26815253Seric 
26916914Seric 	q = pvpbuf;
2703149Seric 	bslashmode = FALSE;
2717800Seric 	cmntcnt = 0;
2728423Seric 	anglecnt = 0;
2733149Seric 	avp = av;
2748078Seric 	state = OPR;
2758078Seric 	c = NOCHAR;
2768078Seric 	p = addr;
2778078Seric # ifdef DEBUG
2788078Seric 	if (tTd(22, 45))
279297Seric 	{
2808078Seric 		printf("prescan: ");
2818078Seric 		xputs(p);
28223109Seric 		(void) putchar('\n');
2838078Seric 	}
2848078Seric # endif DEBUG
2858078Seric 
2868078Seric 	do
2878078Seric 	{
2883149Seric 		/* read a token */
2893149Seric 		tok = q;
2908078Seric 		for (;;)
291297Seric 		{
2928078Seric 			/* store away any old lookahead character */
2938078Seric 			if (c != NOCHAR)
2948078Seric 			{
29515284Seric 				/* see if there is room */
29616914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
2978078Seric 				{
2988078Seric 					usrerr("Address too long");
2998078Seric 					DelimChar = p;
3008078Seric 					return (NULL);
3018078Seric 				}
30215284Seric 
30315284Seric 				/* squirrel it away */
3048078Seric 				*q++ = c;
3058078Seric 			}
3068078Seric 
3078078Seric 			/* read a new input character */
3088078Seric 			c = *p++;
3098078Seric 			if (c == '\0')
3108078Seric 				break;
31115284Seric 			c &= ~0200;
31215284Seric 
3138078Seric # ifdef DEBUG
3148078Seric 			if (tTd(22, 101))
3158078Seric 				printf("c=%c, s=%d; ", c, state);
3168078Seric # endif DEBUG
3178078Seric 
3183149Seric 			/* chew up special characters */
3193149Seric 			*q = '\0';
3203149Seric 			if (bslashmode)
3213149Seric 			{
32224944Seric 				/* kludge \! for naive users */
32324944Seric 				if (c != '!')
32424944Seric 					c |= 0200;
3253149Seric 				bslashmode = FALSE;
3263149Seric 			}
3273149Seric 			else if (c == '\\')
3283149Seric 			{
3293149Seric 				bslashmode = TRUE;
3308078Seric 				c = NOCHAR;
3313149Seric 			}
3328514Seric 			else if (state == QST)
3338514Seric 			{
3348514Seric 				/* do nothing, just avoid next clauses */
3358514Seric 			}
3368078Seric 			else if (c == '(')
3374100Seric 			{
3388078Seric 				cmntcnt++;
3398078Seric 				c = NOCHAR;
3404100Seric 			}
3418078Seric 			else if (c == ')')
3423149Seric 			{
3438078Seric 				if (cmntcnt <= 0)
3443149Seric 				{
3458078Seric 					usrerr("Unbalanced ')'");
3468078Seric 					DelimChar = p;
3478078Seric 					return (NULL);
3483149Seric 				}
3498078Seric 				else
3508078Seric 					cmntcnt--;
3518078Seric 			}
3528078Seric 			else if (cmntcnt > 0)
3538078Seric 				c = NOCHAR;
3548423Seric 			else if (c == '<')
3558423Seric 				anglecnt++;
3568423Seric 			else if (c == '>')
3578423Seric 			{
3588423Seric 				if (anglecnt <= 0)
3598423Seric 				{
3608423Seric 					usrerr("Unbalanced '>'");
3618423Seric 					DelimChar = p;
3628423Seric 					return (NULL);
3638423Seric 				}
3648423Seric 				anglecnt--;
3658423Seric 			}
36611423Seric 			else if (delim == ' ' && isspace(c))
36711423Seric 				c = ' ';
3683149Seric 
3698078Seric 			if (c == NOCHAR)
3708078Seric 				continue;
3713149Seric 
3728078Seric 			/* see if this is end of input */
37311405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
3743149Seric 				break;
3753149Seric 
3768078Seric 			newstate = StateTab[state][toktype(c)];
3778078Seric # ifdef DEBUG
3788078Seric 			if (tTd(22, 101))
3798078Seric 				printf("ns=%02o\n", newstate);
3808078Seric # endif DEBUG
3818078Seric 			state = newstate & TYPE;
3828078Seric 			if (bitset(M, newstate))
3838078Seric 				c = NOCHAR;
3848078Seric 			if (bitset(B, newstate))
3854228Seric 				break;
386297Seric 		}
3873149Seric 
3883149Seric 		/* new token */
3898078Seric 		if (tok != q)
3901378Seric 		{
3918078Seric 			*q++ = '\0';
3928078Seric # ifdef DEBUG
3938078Seric 			if (tTd(22, 36))
394297Seric 			{
3958078Seric 				printf("tok=");
3968078Seric 				xputs(tok);
39723109Seric 				(void) putchar('\n');
398297Seric 			}
3998078Seric # endif DEBUG
4008078Seric 			if (avp >= &av[MAXATOM])
401297Seric 			{
4028078Seric 				syserr("prescan: too many tokens");
4038078Seric 				DelimChar = p;
4048078Seric 				return (NULL);
405297Seric 			}
4068078Seric 			*avp++ = tok;
407297Seric 		}
4088423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4093149Seric 	*avp = NULL;
4108078Seric 	DelimChar = --p;
4113149Seric 	if (cmntcnt > 0)
4123149Seric 		usrerr("Unbalanced '('");
4138423Seric 	else if (anglecnt > 0)
4148423Seric 		usrerr("Unbalanced '<'");
4158078Seric 	else if (state == QST)
4163149Seric 		usrerr("Unbalanced '\"'");
4173149Seric 	else if (av[0] != NULL)
4183149Seric 		return (av);
4193149Seric 	return (NULL);
4203149Seric }
4213149Seric /*
4223149Seric **  TOKTYPE -- return token type
4233149Seric **
4243149Seric **	Parameters:
4253149Seric **		c -- the character in question.
4263149Seric **
4273149Seric **	Returns:
4283149Seric **		Its type.
4293149Seric **
4303149Seric **	Side Effects:
4313149Seric **		none.
4323149Seric */
433297Seric 
4343149Seric toktype(c)
4353149Seric 	register char c;
4363149Seric {
4373380Seric 	static char buf[50];
4383382Seric 	static bool firstime = TRUE;
4393380Seric 
4403382Seric 	if (firstime)
4413380Seric 	{
4423382Seric 		firstime = FALSE;
44316155Seric 		expand("\001o", buf, &buf[sizeof buf - 1], CurEnv);
4447005Seric 		(void) strcat(buf, DELIMCHARS);
4453380Seric 	}
4469585Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
4478078Seric 		return (ONE);
4488078Seric 	if (c == '"')
4498078Seric 		return (QST);
4504100Seric 	if (!isascii(c))
4518078Seric 		return (ATM);
4528078Seric 	if (isspace(c) || c == ')')
4538078Seric 		return (SPC);
4543380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
4558078Seric 		return (OPR);
4568078Seric 	return (ATM);
4573149Seric }
4583149Seric /*
4593149Seric **  REWRITE -- apply rewrite rules to token vector.
4603149Seric **
4614476Seric **	This routine is an ordered production system.  Each rewrite
4624476Seric **	rule has a LHS (called the pattern) and a RHS (called the
4634476Seric **	rewrite); 'rwr' points the the current rewrite rule.
4644476Seric **
4654476Seric **	For each rewrite rule, 'avp' points the address vector we
4664476Seric **	are trying to match against, and 'pvp' points to the pattern.
4678058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
4689585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
4699585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
4704476Seric **
4714476Seric **	When a match between avp & pvp does not match, we try to
4729585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
4734476Seric **	we must also back out the match in mvp.  If we reach a
4748058Seric **	MATCHANY or MATCHZANY we just extend the match and start
4758058Seric **	over again.
4764476Seric **
4774476Seric **	When we finally match, we rewrite the address vector
4784476Seric **	and try over again.
4794476Seric **
4803149Seric **	Parameters:
4813149Seric **		pvp -- pointer to token vector.
4823149Seric **
4833149Seric **	Returns:
4843149Seric **		none.
4853149Seric **
4863149Seric **	Side Effects:
4873149Seric **		pvp is modified.
4883149Seric */
4892091Seric 
4903149Seric struct match
4913149Seric {
4924468Seric 	char	**first;	/* first token matched */
4934468Seric 	char	**last;		/* last token matched */
4943149Seric };
4953149Seric 
4964468Seric # define MAXMATCH	9	/* max params per rewrite */
4973149Seric 
4983149Seric 
4994070Seric rewrite(pvp, ruleset)
5003149Seric 	char **pvp;
5014070Seric 	int ruleset;
5023149Seric {
5033149Seric 	register char *ap;		/* address pointer */
5043149Seric 	register char *rp;		/* rewrite pointer */
5053149Seric 	register char **avp;		/* address vector pointer */
5063149Seric 	register char **rvp;		/* rewrite vector pointer */
5078058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5088058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
5094468Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5103149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5114060Seric 	extern bool sameword();
5123149Seric 
5139279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5143149Seric 	{
5158959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
5163149Seric 		printav(pvp);
5173149Seric 	}
5188423Seric 	if (pvp == NULL)
5198423Seric 		return;
5203149Seric 
5213149Seric 	/*
5223149Seric 	**  Run through the list of rewrite rules, applying
5233149Seric 	**	any that match.
5243149Seric 	*/
5253149Seric 
5264070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
5273149Seric 	{
5284100Seric # ifdef DEBUG
5297675Seric 		if (tTd(21, 12))
530297Seric 		{
5318069Seric 			printf("-----trying rule:");
5323149Seric 			printav(rwr->r_lhs);
5333149Seric 		}
5344100Seric # endif DEBUG
5353149Seric 
5363149Seric 		/* try to match on this rule */
5374468Seric 		mlp = mlist;
5388058Seric 		rvp = rwr->r_lhs;
5398058Seric 		avp = pvp;
5408058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
5413149Seric 		{
5423149Seric 			rp = *rvp;
5438058Seric # ifdef DEBUG
5448058Seric 			if (tTd(21, 35))
5458058Seric 			{
5468069Seric 				printf("ap=");
5478058Seric 				xputs(ap);
5488069Seric 				printf(", rp=");
5498058Seric 				xputs(rp);
5508069Seric 				printf("\n");
5518058Seric 			}
5528058Seric # endif DEBUG
5533149Seric 			if (rp == NULL)
554297Seric 			{
5553149Seric 				/* end-of-pattern before end-of-address */
5568058Seric 				goto backup;
557297Seric 			}
5588058Seric 			if (ap == NULL && *rp != MATCHZANY)
5598058Seric 			{
5608058Seric 				/* end-of-input */
5618058Seric 				break;
5628058Seric 			}
5633149Seric 
5643149Seric 			switch (*rp)
5653149Seric 			{
5664060Seric 				register STAB *s;
5674060Seric 
5684060Seric 			  case MATCHCLASS:
5699585Seric 			  case MATCHNCLASS:
5709585Seric 				/* match any token in (not in) a class */
5714100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
57210690Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
5739585Seric 				{
5749585Seric 					if (*rp == MATCHCLASS)
5759585Seric 						goto backup;
5769585Seric 				}
5779585Seric 				else if (*rp == MATCHNCLASS)
5788058Seric 					goto backup;
5794468Seric 
5804476Seric 				/* explicit fall-through */
5814476Seric 
5824476Seric 			  case MATCHONE:
5834476Seric 			  case MATCHANY:
5844476Seric 				/* match exactly one token */
5858058Seric 				mlp->first = avp;
5868058Seric 				mlp->last = avp++;
5874468Seric 				mlp++;
5884060Seric 				break;
5894060Seric 
5908058Seric 			  case MATCHZANY:
5918058Seric 				/* match zero or more tokens */
5928058Seric 				mlp->first = avp;
5938058Seric 				mlp->last = avp - 1;
5948058Seric 				mlp++;
5958058Seric 				break;
5968058Seric 
5973149Seric 			  default:
5983149Seric 				/* must have exact match */
5994060Seric 				if (!sameword(rp, ap))
6008058Seric 					goto backup;
6014468Seric 				avp++;
6023149Seric 				break;
6033149Seric 			}
6043149Seric 
6053149Seric 			/* successful match on this token */
6063149Seric 			rvp++;
6073149Seric 			continue;
6083149Seric 
6098058Seric 		  backup:
6103149Seric 			/* match failed -- back up */
6113149Seric 			while (--rvp >= rwr->r_lhs)
6123149Seric 			{
6133149Seric 				rp = *rvp;
6148058Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
6154468Seric 				{
6164476Seric 					/* extend binding and continue */
6178058Seric 					avp = ++mlp[-1].last;
6188058Seric 					avp++;
6194476Seric 					rvp++;
6203149Seric 					break;
6214468Seric 				}
6224476Seric 				avp--;
6239585Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS ||
6249585Seric 				    *rp == MATCHNCLASS)
6253149Seric 				{
6264468Seric 					/* back out binding */
6274468Seric 					mlp--;
6283149Seric 				}
6293149Seric 			}
6303149Seric 
6313149Seric 			if (rvp < rwr->r_lhs)
6323149Seric 			{
6333149Seric 				/* total failure to match */
6343149Seric 				break;
6353149Seric 			}
636297Seric 		}
6373149Seric 
6383149Seric 		/*
6393149Seric 		**  See if we successfully matched
6403149Seric 		*/
6413149Seric 
6429374Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
6433149Seric 		{
6444100Seric # ifdef DEBUG
6459374Seric 			if (tTd(21, 10))
6469374Seric 				printf("----- rule fails\n");
6474100Seric # endif DEBUG
6489374Seric 			rwr = rwr->r_next;
6499374Seric 			continue;
6509374Seric 		}
6513149Seric 
6529374Seric 		rvp = rwr->r_rhs;
6539374Seric # ifdef DEBUG
6549374Seric 		if (tTd(21, 12))
6559374Seric 		{
6569374Seric 			printf("-----rule matches:");
6579374Seric 			printav(rvp);
6589374Seric 		}
6599374Seric # endif DEBUG
6609374Seric 
6619374Seric 		rp = *rvp;
6629374Seric 		if (*rp == CANONUSER)
6639374Seric 		{
6649374Seric 			rvp++;
6659374Seric 			rwr = rwr->r_next;
6669374Seric 		}
6679374Seric 		else if (*rp == CANONHOST)
6689374Seric 		{
6699374Seric 			rvp++;
6709374Seric 			rwr = NULL;
6719374Seric 		}
6729374Seric 		else if (*rp == CANONNET)
6739374Seric 			rwr = NULL;
6749374Seric 
6759374Seric 		/* substitute */
6769374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
6779374Seric 		{
6789374Seric 			register struct match *m;
6799374Seric 			register char **pp;
6809374Seric 
6818058Seric 			rp = *rvp;
68216914Seric 			if (*rp == MATCHREPL)
6838058Seric 			{
68416914Seric 				/* substitute from LHS */
68516914Seric 				m = &mlist[rp[1] - '1'];
68616914Seric 				if (m >= mlp)
6879374Seric 				{
68816914Seric 					syserr("rewrite: ruleset %d: replacement out of bounds", ruleset);
6899374Seric 					return;
6909374Seric 				}
6919374Seric # ifdef DEBUG
69216914Seric 				if (tTd(21, 15))
69316914Seric 				{
69416914Seric 					printf("$%c:", rp[1]);
69516914Seric 					pp = m->first;
69616914Seric 					while (pp <= m->last)
69716914Seric 					{
69816914Seric 						printf(" %x=\"", *pp);
69916914Seric 						(void) fflush(stdout);
70016914Seric 						printf("%s\"", *pp++);
70116914Seric 					}
70216914Seric 					printf("\n");
70316914Seric 				}
70416914Seric # endif DEBUG
7059374Seric 				pp = m->first;
7069374Seric 				while (pp <= m->last)
7073149Seric 				{
70816914Seric 					if (avp >= &npvp[MAXATOM])
70916914Seric 					{
71016914Seric 						syserr("rewrite: expansion too long");
71116914Seric 						return;
71216914Seric 					}
71316914Seric 					*avp++ = *pp++;
7143149Seric 				}
7153149Seric 			}
71616914Seric 			else
7178226Seric 			{
71816914Seric 				/* vanilla replacement */
7199374Seric 				if (avp >= &npvp[MAXATOM])
72016889Seric 				{
72116914Seric 	toolong:
72216889Seric 					syserr("rewrite: expansion too long");
72316889Seric 					return;
72416889Seric 				}
72516914Seric 				*avp++ = rp;
7268226Seric 			}
7279374Seric 		}
7289374Seric 		*avp++ = NULL;
72916914Seric 
73016914Seric 		/*
73116914Seric 		**  Check for any hostname lookups.
73216914Seric 		*/
73316914Seric 
73416914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
73516914Seric 		{
73616914Seric 			char **hbrvp;
73716914Seric 			char **xpvp;
73816914Seric 			int trsize;
73917473Seric 			char *olddelimchar;
74016920Seric 			char buf[MAXNAME + 1];
74116914Seric 			char *pvpb1[MAXATOM + 1];
74217174Seric 			char pvpbuf[PSBUFSIZE];
74317473Seric 			extern char *DelimChar;
74416914Seric 
74516914Seric 			if (**rvp != HOSTBEGIN)
74616914Seric 				continue;
74716914Seric 
74816914Seric 			/*
74916914Seric 			**  Got a hostname lookup.
75016914Seric 			**
75116914Seric 			**	This could be optimized fairly easily.
75216914Seric 			*/
75316914Seric 
75416914Seric 			hbrvp = rvp;
75516914Seric 
75616914Seric 			/* extract the match part */
75716914Seric 			while (*++rvp != NULL && **rvp != HOSTEND)
75816914Seric 				continue;
75916914Seric 			if (*rvp != NULL)
76016914Seric 				*rvp++ = NULL;
76116914Seric 
76216914Seric 			/* save the remainder of the input string */
76316914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
76416914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
76516914Seric 
76616914Seric 			/* look it up */
76716914Seric 			cataddr(++hbrvp, buf, sizeof buf);
76816914Seric 			maphostname(buf, sizeof buf);
76916914Seric 
77016914Seric 			/* scan the new host name */
77117473Seric 			olddelimchar = DelimChar;
77216914Seric 			xpvp = prescan(buf, '\0', pvpbuf);
77317473Seric 			DelimChar = olddelimchar;
77416914Seric 			if (xpvp == NULL)
77516914Seric 			{
77616914Seric 				syserr("rewrite: cannot prescan canonical hostname: %s", buf);
77722976Smiriam 				return;
77816914Seric 			}
77916914Seric 
78016914Seric 			/* append it to the token list */
78117174Seric 			for (avp = --hbrvp; *xpvp != NULL; xpvp++)
78217174Seric 			{
78317174Seric 				*avp++ = newstr(*xpvp);
78416920Seric 				if (avp >= &npvp[MAXATOM])
78516914Seric 					goto toolong;
78617174Seric 			}
78716914Seric 
78816914Seric 			/* restore the old trailing information */
78917177Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
79016920Seric 				if (avp >= &npvp[MAXATOM])
79116914Seric 					goto toolong;
79217174Seric 
79317174Seric 			break;
79416914Seric 		}
79516914Seric 
79616914Seric 		/*
79716914Seric 		**  Check for subroutine calls.
79816914Seric 		*/
79916914Seric 
80024944Seric 		if (*npvp != NULL && **npvp == CALLSUBR)
8019374Seric 		{
80216889Seric 			bcopy((char *) &npvp[2], (char *) pvp,
80316900Seric 				(int) (avp - npvp - 2) * sizeof *avp);
80416889Seric # ifdef DEBUG
80516889Seric 			if (tTd(21, 3))
80616889Seric 				printf("-----callsubr %s\n", npvp[1]);
80716889Seric # endif DEBUG
80816889Seric 			rewrite(pvp, atoi(npvp[1]));
8093149Seric 		}
8103149Seric 		else
8113149Seric 		{
81217348Seric 			bcopy((char *) npvp, (char *) pvp,
81316900Seric 				(int) (avp - npvp) * sizeof *avp);
8149374Seric 		}
8154100Seric # ifdef DEBUG
8169374Seric 		if (tTd(21, 4))
8179374Seric 		{
8189374Seric 			printf("rewritten as:");
8199374Seric 			printav(pvp);
8209374Seric 		}
8214100Seric # endif DEBUG
822297Seric 	}
8238069Seric 
8249279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
8258069Seric 	{
8268959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
8278069Seric 		printav(pvp);
8288069Seric 	}
8293149Seric }
8303149Seric /*
8313149Seric **  BUILDADDR -- build address from token vector.
8323149Seric **
8333149Seric **	Parameters:
8343149Seric **		tv -- token vector.
8353149Seric **		a -- pointer to address descriptor to fill.
8363149Seric **			If NULL, one will be allocated.
8373149Seric **
8383149Seric **	Returns:
8394279Seric **		NULL if there was an error.
8404279Seric **		'a' otherwise.
8413149Seric **
8423149Seric **	Side Effects:
8433149Seric **		fills in 'a'
8443149Seric */
8453149Seric 
8463149Seric ADDRESS *
8473149Seric buildaddr(tv, a)
8483149Seric 	register char **tv;
8493149Seric 	register ADDRESS *a;
8503149Seric {
8513149Seric 	static char buf[MAXNAME];
8523149Seric 	struct mailer **mp;
8533149Seric 	register struct mailer *m;
8544635Seric 	extern bool sameword();
8553149Seric 
8563149Seric 	if (a == NULL)
8573149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
85816889Seric 	bzero((char *) a, sizeof *a);
8593149Seric 
8603149Seric 	/* figure out what net/mailer to use */
8613149Seric 	if (**tv != CANONNET)
8624279Seric 	{
8633149Seric 		syserr("buildaddr: no net");
8644279Seric 		return (NULL);
8654279Seric 	}
8663149Seric 	tv++;
8674635Seric 	if (sameword(*tv, "error"))
8684279Seric 	{
86910183Seric 		if (**++tv == CANONHOST)
87010183Seric 		{
87110183Seric 			setstat(atoi(*++tv));
87210183Seric 			tv++;
87310183Seric 		}
87410183Seric 		if (**tv != CANONUSER)
8754279Seric 			syserr("buildaddr: error: no user");
8764279Seric 		buf[0] = '\0';
8774279Seric 		while (*++tv != NULL)
8784279Seric 		{
8794279Seric 			if (buf[0] != '\0')
8807005Seric 				(void) strcat(buf, " ");
8817005Seric 			(void) strcat(buf, *tv);
8824279Seric 		}
8834279Seric 		usrerr(buf);
8844279Seric 		return (NULL);
8854279Seric 	}
8864598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
8873149Seric 	{
8884635Seric 		if (sameword(m->m_name, *tv))
8893149Seric 			break;
8903149Seric 	}
8913149Seric 	if (m == NULL)
8924279Seric 	{
89324944Seric 		syserr("buildaddr: unknown mailer %s", *tv);
8944279Seric 		return (NULL);
8954279Seric 	}
8964598Seric 	a->q_mailer = m;
8973149Seric 
8983149Seric 	/* figure out what host (if any) */
8993149Seric 	tv++;
90010690Seric 	if (!bitnset(M_LOCAL, m->m_flags))
9013149Seric 	{
9025704Seric 		if (**tv++ != CANONHOST)
9034279Seric 		{
9043149Seric 			syserr("buildaddr: no host");
9054279Seric 			return (NULL);
9064279Seric 		}
9075704Seric 		buf[0] = '\0';
9085704Seric 		while (*tv != NULL && **tv != CANONUSER)
9097005Seric 			(void) strcat(buf, *tv++);
9105704Seric 		a->q_host = newstr(buf);
9113149Seric 	}
9123149Seric 	else
9133149Seric 		a->q_host = NULL;
9143149Seric 
9153149Seric 	/* figure out the user */
9163149Seric 	if (**tv != CANONUSER)
9174279Seric 	{
9183149Seric 		syserr("buildaddr: no user");
9194279Seric 		return (NULL);
9204279Seric 	}
92119040Seric 
92219040Seric 	/* rewrite according recipient mailer rewriting rules */
92319040Seric 	rewrite(++tv, 2);
92419040Seric 	if (m->m_r_rwset > 0)
92519040Seric 		rewrite(tv, m->m_r_rwset);
92619040Seric 	rewrite(tv, 4);
92719040Seric 
92819040Seric 	/* save the result for the command line/RCPT argument */
92911278Seric 	cataddr(tv, buf, sizeof buf);
9303149Seric 	a->q_user = buf;
9313149Seric 
9323149Seric 	return (a);
9333149Seric }
9343188Seric /*
9354228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
9364228Seric **
9374228Seric **	Parameters:
9384228Seric **		pvp -- parameter vector to rebuild.
9394228Seric **		buf -- buffer to build the string into.
9404228Seric **		sz -- size of buf.
9414228Seric **
9424228Seric **	Returns:
9434228Seric **		none.
9444228Seric **
9454228Seric **	Side Effects:
9464228Seric **		Destroys buf.
9474228Seric */
9484228Seric 
9494228Seric cataddr(pvp, buf, sz)
9504228Seric 	char **pvp;
9514228Seric 	char *buf;
9524228Seric 	register int sz;
9534228Seric {
9544228Seric 	bool oatomtok = FALSE;
9554228Seric 	bool natomtok = FALSE;
9564228Seric 	register int i;
9574228Seric 	register char *p;
9584228Seric 
9598423Seric 	if (pvp == NULL)
9608423Seric 	{
96123109Seric 		(void) strcpy(buf, "");
9628423Seric 		return;
9638423Seric 	}
9644228Seric 	p = buf;
96511156Seric 	sz -= 2;
9664228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
9674228Seric 	{
9688078Seric 		natomtok = (toktype(**pvp) == ATM);
9694228Seric 		if (oatomtok && natomtok)
9709042Seric 			*p++ = SpaceSub;
9714228Seric 		(void) strcpy(p, *pvp);
9724228Seric 		oatomtok = natomtok;
9734228Seric 		p += i;
97411156Seric 		sz -= i + 1;
9754228Seric 		pvp++;
9764228Seric 	}
9774228Seric 	*p = '\0';
9784228Seric }
9794228Seric /*
9803188Seric **  SAMEADDR -- Determine if two addresses are the same
9813188Seric **
9823188Seric **	This is not just a straight comparison -- if the mailer doesn't
9833188Seric **	care about the host we just ignore it, etc.
9843188Seric **
9853188Seric **	Parameters:
9863188Seric **		a, b -- pointers to the internal forms to compare.
9873188Seric **
9883188Seric **	Returns:
9893188Seric **		TRUE -- they represent the same mailbox.
9903188Seric **		FALSE -- they don't.
9913188Seric **
9923188Seric **	Side Effects:
9933188Seric **		none.
9943188Seric */
9953188Seric 
9963188Seric bool
9979374Seric sameaddr(a, b)
9983188Seric 	register ADDRESS *a;
9993188Seric 	register ADDRESS *b;
10003188Seric {
10013188Seric 	/* if they don't have the same mailer, forget it */
10023188Seric 	if (a->q_mailer != b->q_mailer)
10033188Seric 		return (FALSE);
10043188Seric 
10053188Seric 	/* if the user isn't the same, we can drop out */
10069374Seric 	if (strcmp(a->q_user, b->q_user) != 0)
10073188Seric 		return (FALSE);
10083188Seric 
10093188Seric 	/* if the mailer ignores hosts, we have succeeded! */
101010690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
10113188Seric 		return (TRUE);
10123188Seric 
10133188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
10143188Seric 	if (a->q_host == NULL || b->q_host == NULL)
10153188Seric 		return (FALSE);
10163188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
10173188Seric 		return (FALSE);
10183188Seric 
10193188Seric 	return (TRUE);
10203188Seric }
10213234Seric /*
10223234Seric **  PRINTADDR -- print address (for debugging)
10233234Seric **
10243234Seric **	Parameters:
10253234Seric **		a -- the address to print
10263234Seric **		follow -- follow the q_next chain.
10273234Seric **
10283234Seric **	Returns:
10293234Seric **		none.
10303234Seric **
10313234Seric **	Side Effects:
10323234Seric **		none.
10333234Seric */
10343234Seric 
10354317Seric # ifdef DEBUG
10364317Seric 
10373234Seric printaddr(a, follow)
10383234Seric 	register ADDRESS *a;
10393234Seric 	bool follow;
10403234Seric {
10415001Seric 	bool first = TRUE;
10425001Seric 
10433234Seric 	while (a != NULL)
10443234Seric 	{
10455001Seric 		first = FALSE;
10464443Seric 		printf("%x=", a);
10474085Seric 		(void) fflush(stdout);
10483234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
10498181Seric 		       a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host,
10508181Seric 		       a->q_user);
10518181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
10528181Seric 		       a->q_alias);
10538181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
10548181Seric 		       a->q_fullname);
10554996Seric 
10563234Seric 		if (!follow)
10573234Seric 			return;
10584996Seric 		a = a->q_next;
10593234Seric 	}
10605001Seric 	if (first)
10614443Seric 		printf("[NULL]\n");
10623234Seric }
10634317Seric 
10644317Seric # endif DEBUG
10657682Seric /*
10667682Seric **  REMOTENAME -- return the name relative to the current mailer
10677682Seric **
10687682Seric **	Parameters:
10697682Seric **		name -- the name to translate.
10708069Seric **		m -- the mailer that we want to do rewriting relative
10718069Seric **			to.
10728069Seric **		senderaddress -- if set, uses the sender rewriting rules
10738069Seric **			rather than the recipient rewriting rules.
107410310Seric **		canonical -- if set, strip out any comment information,
107510310Seric **			etc.
10767682Seric **
10777682Seric **	Returns:
10787682Seric **		the text string representing this address relative to
10797682Seric **			the receiving mailer.
10807682Seric **
10817682Seric **	Side Effects:
10827682Seric **		none.
10837682Seric **
10847682Seric **	Warnings:
10857682Seric **		The text string returned is tucked away locally;
10867682Seric **			copy it if you intend to save it.
10877682Seric */
10887682Seric 
10897682Seric char *
109010310Seric remotename(name, m, senderaddress, canonical)
10917682Seric 	char *name;
10927682Seric 	struct mailer *m;
10938069Seric 	bool senderaddress;
109410310Seric 	bool canonical;
10957682Seric {
10968069Seric 	register char **pvp;
10978069Seric 	char *fancy;
10988069Seric 	extern char *macvalue();
10998181Seric 	char *oldg = macvalue('g', CurEnv);
11007682Seric 	static char buf[MAXNAME];
11017682Seric 	char lbuf[MAXNAME];
110216914Seric 	char pvpbuf[PSBUFSIZE];
11037682Seric 	extern char **prescan();
11047889Seric 	extern char *crackaddr();
11057682Seric 
11067755Seric # ifdef DEBUG
11077755Seric 	if (tTd(12, 1))
11087755Seric 		printf("remotename(%s)\n", name);
11097755Seric # endif DEBUG
11107755Seric 
111110177Seric 	/* don't do anything if we are tagging it as special */
111210177Seric 	if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0)
111310177Seric 		return (name);
111410177Seric 
11157682Seric 	/*
11168181Seric 	**  Do a heuristic crack of this name to extract any comment info.
11178181Seric 	**	This will leave the name as a comment and a $g macro.
11187889Seric 	*/
11197889Seric 
112010310Seric 	if (canonical)
112116155Seric 		fancy = "\001g";
112210310Seric 	else
112310310Seric 		fancy = crackaddr(name);
11247889Seric 
11258181Seric 	/*
11268181Seric 	**  Turn the name into canonical form.
11278181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
11288181Seric 	**	If this only resolves to "user", and the "C" flag is
11298181Seric 	**	specified in the sending mailer, then the sender's
11308181Seric 	**	domain will be appended.
11318181Seric 	*/
11328181Seric 
113316914Seric 	pvp = prescan(name, '\0', pvpbuf);
11347889Seric 	if (pvp == NULL)
11357889Seric 		return (name);
11368181Seric 	rewrite(pvp, 3);
11378181Seric 	if (CurEnv->e_fromdomain != NULL)
11388181Seric 	{
11398181Seric 		/* append from domain to this address */
11408181Seric 		register char **pxp = pvp;
11418181Seric 
11429594Seric 		/* see if there is an "@domain" in the current name */
11438181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
11448181Seric 			pxp++;
11458181Seric 		if (*pxp == NULL)
11468181Seric 		{
11479594Seric 			/* no.... append the "@domain" from the sender */
11488181Seric 			register char **qxq = CurEnv->e_fromdomain;
11498181Seric 
11509594Seric 			while ((*pxp++ = *qxq++) != NULL)
11519594Seric 				continue;
115211726Seric 			rewrite(pvp, 3);
11538181Seric 		}
11548181Seric 	}
11558181Seric 
11568181Seric 	/*
11578959Seric 	**  Do more specific rewriting.
11588181Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
11598181Seric 	**		a sender address or not.
11608181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
11618181Seric 	*/
11628181Seric 
11638069Seric 	if (senderaddress)
11647755Seric 	{
11657889Seric 		rewrite(pvp, 1);
11668069Seric 		if (m->m_s_rwset > 0)
11678069Seric 			rewrite(pvp, m->m_s_rwset);
11688069Seric 	}
11698069Seric 	else
11708069Seric 	{
11717889Seric 		rewrite(pvp, 2);
11728069Seric 		if (m->m_r_rwset > 0)
11738069Seric 			rewrite(pvp, m->m_r_rwset);
11747682Seric 	}
11757682Seric 
11768181Seric 	/*
11778959Seric 	**  Do any final sanitation the address may require.
11788959Seric 	**	This will normally be used to turn internal forms
11798959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
11808959Seric 	**	may be used as a default to the above rules.
11818959Seric 	*/
11828959Seric 
11838959Seric 	rewrite(pvp, 4);
11848959Seric 
11858959Seric 	/*
11868181Seric 	**  Now restore the comment information we had at the beginning.
11878181Seric 	*/
11888181Seric 
11897682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
11909374Seric 	define('g', lbuf, CurEnv);
11917889Seric 	expand(fancy, buf, &buf[sizeof buf - 1], CurEnv);
11929374Seric 	define('g', oldg, CurEnv);
11937682Seric 
11947682Seric # ifdef DEBUG
11957682Seric 	if (tTd(12, 1))
11967755Seric 		printf("remotename => `%s'\n", buf);
11977682Seric # endif DEBUG
11987682Seric 	return (buf);
11997682Seric }
1200