122976Smiriam /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333730Sbostic  * Copyright (c) 1988 Regents of the University of California.
433730Sbostic  * All rights reserved.
533730Sbostic  *
642828Sbostic  * %sccs.include.redist.c%
733730Sbostic  */
822976Smiriam 
922976Smiriam #ifndef lint
10*57391Seric static char sccsid[] = "@(#)parseaddr.c	6.3 (Berkeley) 01/01/93";
1133730Sbostic #endif /* not lint */
1222976Smiriam 
1356678Seric # include "sendmail.h"
14297Seric 
15297Seric /*
169888Seric **  PARSEADDR -- Parse an address
17297Seric **
18297Seric **	Parses an address and breaks it up into three parts: a
19297Seric **	net to transmit the message on, the host to transmit it
20297Seric **	to, and a user on that host.  These are loaded into an
212973Seric **	ADDRESS header with the values squirreled away if necessary.
22297Seric **	The "user" part may not be a real user; the process may
23297Seric **	just reoccur on that machine.  For example, on a machine
24297Seric **	with an arpanet connection, the address
25297Seric **		csvax.bill@berkeley
26297Seric **	will break up to a "user" of 'csvax.bill' and a host
27297Seric **	of 'berkeley' -- to be transmitted over the arpanet.
28297Seric **
29297Seric **	Parameters:
30297Seric **		addr -- the address to parse.
31297Seric **		a -- a pointer to the address descriptor buffer.
32297Seric **			If NULL, a header will be created.
33297Seric **		copyf -- determines what shall be copied:
34297Seric **			-1 -- don't copy anything.  The printname
35297Seric **				(q_paddr) is just addr, and the
36297Seric **				user & host are allocated internally
37297Seric **				to parse.
38297Seric **			0 -- copy out the parsed user & host, but
39297Seric **				don't copy the printname.
40297Seric **			+1 -- copy everything.
4111445Seric **		delim -- the character to terminate the address, passed
4211445Seric **			to prescan.
4356678Seric **		e -- the envelope that will contain this address.
44297Seric **
45297Seric **	Returns:
46297Seric **		A pointer to the address descriptor header (`a' if
47297Seric **			`a' is non-NULL).
48297Seric **		NULL on error.
49297Seric **
50297Seric **	Side Effects:
51297Seric **		none
52297Seric */
53297Seric 
549374Seric /* following delimiters are inherent to the internal algorithms */
5516155Seric # define DELIMCHARS	"\001()<>,;\\\"\r\n"	/* word delimiters */
562091Seric 
572973Seric ADDRESS *
5856678Seric parseaddr(addr, a, copyf, delim, e)
59297Seric 	char *addr;
602973Seric 	register ADDRESS *a;
61297Seric 	int copyf;
6211445Seric 	char delim;
6356678Seric 	register ENVELOPE *e;
64297Seric {
653149Seric 	register char **pvp;
6616914Seric 	char pvpbuf[PSBUFSIZE];
6756678Seric 	extern char **prescan();
6856678Seric 	extern ADDRESS *buildaddr();
6957388Seric 	extern bool invalidaddr();
70297Seric 
71297Seric 	/*
72297Seric 	**  Initialize and prescan address.
73297Seric 	*/
74297Seric 
7556678Seric 	e->e_to = addr;
767675Seric 	if (tTd(20, 1))
779888Seric 		printf("\n--parseaddr(%s)\n", addr);
783188Seric 
7957388Seric 	if (invalidaddr(addr))
8057388Seric 	{
8157388Seric 		if (tTd(20, 1))
8257388Seric 			printf("parseaddr-->bad address\n");
8357388Seric 		return NULL;
8457388Seric 	}
8557388Seric 
8616914Seric 	pvp = prescan(addr, delim, pvpbuf);
873149Seric 	if (pvp == NULL)
8856729Seric 	{
8956729Seric 		if (tTd(20, 1))
9056729Seric 			printf("parseaddr-->NULL\n");
91297Seric 		return (NULL);
9256729Seric 	}
93297Seric 
94297Seric 	/*
953149Seric 	**  Apply rewriting rules.
967889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
97297Seric 	*/
98297Seric 
998181Seric 	rewrite(pvp, 3);
1004070Seric 	rewrite(pvp, 0);
101297Seric 
1023149Seric 	/*
1033149Seric 	**  See if we resolved to a real mailer.
1043149Seric 	*/
105297Seric 
1063149Seric 	if (pvp[0][0] != CANONNET)
1073149Seric 	{
1083149Seric 		setstat(EX_USAGE);
1093149Seric 		usrerr("cannot resolve name");
1103149Seric 		return (NULL);
111297Seric 	}
112297Seric 
113297Seric 	/*
1143149Seric 	**  Build canonical address from pvp.
115297Seric 	*/
116297Seric 
1173149Seric 	a = buildaddr(pvp, a);
1184279Seric 	if (a == NULL)
1194279Seric 		return (NULL);
120297Seric 
121297Seric 	/*
1223149Seric 	**  Make local copies of the host & user and then
1233149Seric 	**  transport them out.
124297Seric 	*/
125297Seric 
12656678Seric 	allocaddr(a, copyf, addr);
12756678Seric 
12856678Seric 	/*
12956678Seric 	**  Compute return value.
13056678Seric 	*/
13156678Seric 
13256678Seric 	if (tTd(20, 1))
1338078Seric 	{
13456678Seric 		printf("parseaddr-->");
13556678Seric 		printaddr(a, FALSE);
13656678Seric 	}
13756678Seric 
13856678Seric 	return (a);
13956678Seric }
14056678Seric /*
14157388Seric **  INVALIDADDR -- check for address containing meta-characters
14257388Seric **
14357388Seric **	Parameters:
14457388Seric **		addr -- the address to check.
14557388Seric **
14657388Seric **	Returns:
14757388Seric **		TRUE -- if the address has any "wierd" characters
14857388Seric **		FALSE -- otherwise.
14957388Seric */
15057388Seric 
15157388Seric bool
15257388Seric invalidaddr(addr)
15357388Seric 	register char *addr;
15457388Seric {
15557388Seric 	for (; *addr != '\0'; addr++)
15657388Seric 	{
157*57391Seric 		if ((((int) *addr) & 0377) >= '\040' || isspace(*addr))
15857388Seric 			continue;
15957388Seric 		setstat(EX_USAGE);
160*57391Seric 		usrerr("Address contained invalid control characters");
16157388Seric 		return TRUE;
16257388Seric 	}
16357388Seric 	return FALSE;
16457388Seric }
16557388Seric /*
16656678Seric **  ALLOCADDR -- do local allocations of address on demand.
16756678Seric **
16856678Seric **	Also lowercases the host name if requested.
16956678Seric **
17056678Seric **	Parameters:
17156678Seric **		a -- the address to reallocate.
17256678Seric **		copyf -- the copy flag (see parseaddr for description).
17356678Seric **		paddr -- the printname of the address.
17456678Seric **
17556678Seric **	Returns:
17656678Seric **		none.
17756678Seric **
17856678Seric **	Side Effects:
17956678Seric **		Copies portions of a into local buffers as requested.
18056678Seric */
18156678Seric 
18256678Seric allocaddr(a, copyf, paddr)
18356678Seric 	register ADDRESS *a;
18456678Seric 	int copyf;
18556678Seric 	char *paddr;
18656678Seric {
18756678Seric 	register MAILER *m = a->q_mailer;
18856678Seric 
18956678Seric 	if (copyf > 0 && paddr != NULL)
19056678Seric 	{
19156678Seric 		extern char *DelimChar;
1928078Seric 		char savec = *DelimChar;
1938078Seric 
1948078Seric 		*DelimChar = '\0';
19556678Seric 		a->q_paddr = newstr(paddr);
1968078Seric 		*DelimChar = savec;
1978078Seric 	}
198297Seric 	else
19956678Seric 		a->q_paddr = paddr;
20024944Seric 
20124944Seric 	if (a->q_user == NULL)
20224944Seric 		a->q_user = "";
20324944Seric 	if (a->q_host == NULL)
20424944Seric 		a->q_host = "";
20524944Seric 
2063149Seric 	if (copyf >= 0)
207297Seric 	{
20824944Seric 		a->q_host = newstr(a->q_host);
2093149Seric 		if (a->q_user != a->q_paddr)
2103149Seric 			a->q_user = newstr(a->q_user);
211297Seric 	}
212297Seric 
21356678Seric 	if (a->q_paddr == NULL)
21456678Seric 		a->q_paddr = a->q_user;
21556678Seric 
216297Seric 	/*
21716202Seric 	**  Convert host name to lower case if requested.
21816202Seric 	**	User name will be done later.
21916202Seric 	*/
22016202Seric 
22116202Seric 	if (!bitnset(M_HST_UPPER, m->m_flags))
22216202Seric 		makelower(a->q_host);
223297Seric }
224297Seric /*
22516162Seric **  LOWERADDR -- map UPPER->lower case on addresses as requested.
22616162Seric **
22716162Seric **	Parameters:
22816162Seric **		a -- address to be mapped.
22916162Seric **
23016162Seric **	Returns:
23116162Seric **		none.
23216162Seric **
23316162Seric **	Side Effects:
23416162Seric **		none.
23516162Seric */
23616162Seric 
23716162Seric loweraddr(a)
23816162Seric 	register ADDRESS *a;
23916162Seric {
24016162Seric 	register MAILER *m = a->q_mailer;
24116162Seric 
24216162Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
24316162Seric 		makelower(a->q_user);
24416162Seric }
24516162Seric /*
246297Seric **  PRESCAN -- Prescan name and make it canonical
247297Seric **
2489374Seric **	Scans a name and turns it into a set of tokens.  This process
2499374Seric **	deletes blanks and comments (in parentheses).
250297Seric **
251297Seric **	This routine knows about quoted strings and angle brackets.
252297Seric **
253297Seric **	There are certain subtleties to this routine.  The one that
254297Seric **	comes to mind now is that backslashes on the ends of names
255297Seric **	are silently stripped off; this is intentional.  The problem
256297Seric **	is that some versions of sndmsg (like at LBL) set the kill
257297Seric **	character to something other than @ when reading addresses;
258297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
259297Seric **	berknet mailer.
260297Seric **
261297Seric **	Parameters:
262297Seric **		addr -- the name to chomp.
263297Seric **		delim -- the delimiter for the address, normally
264297Seric **			'\0' or ','; \0 is accepted in any case.
26515284Seric **			If '\t' then we are reading the .cf file.
26616914Seric **		pvpbuf -- place to put the saved text -- note that
26716914Seric **			the pointers are static.
268297Seric **
269297Seric **	Returns:
2703149Seric **		A pointer to a vector of tokens.
271297Seric **		NULL on error.
272297Seric **
273297Seric **	Side Effects:
27425279Seric **		sets DelimChar to point to the character matching 'delim'.
275297Seric */
276297Seric 
2778078Seric /* states and character types */
2788078Seric # define OPR		0	/* operator */
2798078Seric # define ATM		1	/* atom */
2808078Seric # define QST		2	/* in quoted string */
2818078Seric # define SPC		3	/* chewing up spaces */
2828078Seric # define ONE		4	/* pick up one character */
2833149Seric 
2848078Seric # define NSTATES	5	/* number of states */
2858078Seric # define TYPE		017	/* mask to select state type */
2868078Seric 
2878078Seric /* meta bits for table */
2888078Seric # define M		020	/* meta character; don't pass through */
2898078Seric # define B		040	/* cause a break */
2908078Seric # define MB		M|B	/* meta-break */
2918078Seric 
2928078Seric static short StateTab[NSTATES][NSTATES] =
2938078Seric {
2948087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2959051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2969051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2979051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2988078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2998078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
3008078Seric };
3018078Seric 
3028078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
3038078Seric 
30456678Seric char	*DelimChar;		/* set to point to the delimiter */
30556678Seric 
3063149Seric char **
30716914Seric prescan(addr, delim, pvpbuf)
308297Seric 	char *addr;
309297Seric 	char delim;
31016914Seric 	char pvpbuf[];
311297Seric {
312297Seric 	register char *p;
3138078Seric 	register char *q;
3149346Seric 	register int c;
3153149Seric 	char **avp;
316297Seric 	bool bslashmode;
317297Seric 	int cmntcnt;
3188423Seric 	int anglecnt;
3193149Seric 	char *tok;
3208078Seric 	int state;
3218078Seric 	int newstate;
3228078Seric 	static char *av[MAXATOM+1];
32356678Seric 	extern int errno;
324297Seric 
32515253Seric 	/* make sure error messages don't have garbage on them */
32615253Seric 	errno = 0;
32715253Seric 
32816914Seric 	q = pvpbuf;
3293149Seric 	bslashmode = FALSE;
3307800Seric 	cmntcnt = 0;
3318423Seric 	anglecnt = 0;
3323149Seric 	avp = av;
33356678Seric 	state = ATM;
3348078Seric 	c = NOCHAR;
3358078Seric 	p = addr;
33656764Seric 	if (tTd(22, 11))
337297Seric 	{
3388078Seric 		printf("prescan: ");
3398078Seric 		xputs(p);
34023109Seric 		(void) putchar('\n');
3418078Seric 	}
3428078Seric 
3438078Seric 	do
3448078Seric 	{
3453149Seric 		/* read a token */
3463149Seric 		tok = q;
3478078Seric 		for (;;)
348297Seric 		{
3498078Seric 			/* store away any old lookahead character */
3508078Seric 			if (c != NOCHAR)
3518078Seric 			{
35215284Seric 				/* see if there is room */
35316914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3548078Seric 				{
3558078Seric 					usrerr("Address too long");
3568078Seric 					DelimChar = p;
3578078Seric 					return (NULL);
3588078Seric 				}
35915284Seric 
36015284Seric 				/* squirrel it away */
3618078Seric 				*q++ = c;
3628078Seric 			}
3638078Seric 
3648078Seric 			/* read a new input character */
3658078Seric 			c = *p++;
36656764Seric 			if (c == '\0' || (c == delim && anglecnt <= 0))
36756764Seric 			{
36856764Seric 				/* diagnose and patch up bad syntax */
36956764Seric 				if (state == QST)
37056764Seric 				{
37156764Seric 					usrerr("Unbalanced '\"'");
37256764Seric 					c = '"';
37356764Seric 				}
37456764Seric 				else if (cmntcnt > 0)
37556764Seric 				{
37656764Seric 					usrerr("Unbalanced '('");
37756764Seric 					c = ')';
37856764Seric 				}
37956764Seric 				else if (anglecnt > 0)
38056764Seric 				{
38156764Seric 					c = '>';
38256764Seric 					usrerr("Unbalanced '<'");
38356764Seric 				}
38456764Seric 				else
38556764Seric 					break;
38615284Seric 
38756764Seric 				p--;
38856764Seric 			}
38956764Seric 
3908078Seric 			if (tTd(22, 101))
3918078Seric 				printf("c=%c, s=%d; ", c, state);
3928078Seric 
3933149Seric 			/* chew up special characters */
3943149Seric 			*q = '\0';
3953149Seric 			if (bslashmode)
3963149Seric 			{
39724944Seric 				/* kludge \! for naive users */
39824944Seric 				if (c != '!')
39956678Seric 					*q++ = '\\';
4003149Seric 				bslashmode = FALSE;
40156678Seric 				continue;
4023149Seric 			}
40356678Seric 
40456678Seric 			if (c == '\\')
4053149Seric 			{
4063149Seric 				bslashmode = TRUE;
4078078Seric 				c = NOCHAR;
40856678Seric 				continue;
4093149Seric 			}
41056678Seric 			else if (state == QST)
4118514Seric 			{
4128514Seric 				/* do nothing, just avoid next clauses */
4138514Seric 			}
4148078Seric 			else if (c == '(')
4154100Seric 			{
4168078Seric 				cmntcnt++;
4178078Seric 				c = NOCHAR;
4184100Seric 			}
4198078Seric 			else if (c == ')')
4203149Seric 			{
4218078Seric 				if (cmntcnt <= 0)
4223149Seric 				{
4238078Seric 					usrerr("Unbalanced ')'");
4248078Seric 					DelimChar = p;
4258078Seric 					return (NULL);
4263149Seric 				}
4278078Seric 				else
4288078Seric 					cmntcnt--;
4298078Seric 			}
4308078Seric 			else if (cmntcnt > 0)
4318078Seric 				c = NOCHAR;
4328423Seric 			else if (c == '<')
4338423Seric 				anglecnt++;
4348423Seric 			else if (c == '>')
4358423Seric 			{
4368423Seric 				if (anglecnt <= 0)
4378423Seric 				{
4388423Seric 					usrerr("Unbalanced '>'");
4398423Seric 					DelimChar = p;
4408423Seric 					return (NULL);
4418423Seric 				}
4428423Seric 				anglecnt--;
4438423Seric 			}
44411423Seric 			else if (delim == ' ' && isspace(c))
44511423Seric 				c = ' ';
4463149Seric 
4478078Seric 			if (c == NOCHAR)
4488078Seric 				continue;
4493149Seric 
4508078Seric 			/* see if this is end of input */
45111405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4523149Seric 				break;
4533149Seric 
4548078Seric 			newstate = StateTab[state][toktype(c)];
4558078Seric 			if (tTd(22, 101))
4568078Seric 				printf("ns=%02o\n", newstate);
4578078Seric 			state = newstate & TYPE;
4588078Seric 			if (bitset(M, newstate))
4598078Seric 				c = NOCHAR;
4608078Seric 			if (bitset(B, newstate))
4614228Seric 				break;
462297Seric 		}
4633149Seric 
4643149Seric 		/* new token */
4658078Seric 		if (tok != q)
4661378Seric 		{
4678078Seric 			*q++ = '\0';
4688078Seric 			if (tTd(22, 36))
469297Seric 			{
4708078Seric 				printf("tok=");
4718078Seric 				xputs(tok);
47223109Seric 				(void) putchar('\n');
473297Seric 			}
4748078Seric 			if (avp >= &av[MAXATOM])
475297Seric 			{
4768078Seric 				syserr("prescan: too many tokens");
4778078Seric 				DelimChar = p;
4788078Seric 				return (NULL);
479297Seric 			}
4808078Seric 			*avp++ = tok;
481297Seric 		}
4828423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4833149Seric 	*avp = NULL;
4848078Seric 	DelimChar = --p;
48556764Seric 	if (tTd(22, 12))
48656764Seric 	{
48756764Seric 		printf("prescan==>");
48856764Seric 		printav(av);
48956764Seric 	}
49056764Seric 	if (av[0] != NULL)
4913149Seric 		return (av);
4923149Seric 	return (NULL);
4933149Seric }
4943149Seric /*
4953149Seric **  TOKTYPE -- return token type
4963149Seric **
4973149Seric **	Parameters:
4983149Seric **		c -- the character in question.
4993149Seric **
5003149Seric **	Returns:
5013149Seric **		Its type.
5023149Seric **
5033149Seric **	Side Effects:
5043149Seric **		none.
5053149Seric */
506297Seric 
5073149Seric toktype(c)
5083149Seric 	register char c;
5093149Seric {
5103380Seric 	static char buf[50];
5113382Seric 	static bool firstime = TRUE;
5123380Seric 
5133382Seric 	if (firstime)
5143380Seric 	{
5153382Seric 		firstime = FALSE;
51616155Seric 		expand("\001o", buf, &buf[sizeof buf - 1], CurEnv);
5177005Seric 		(void) strcat(buf, DELIMCHARS);
5183380Seric 	}
51956327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5208078Seric 		return (ONE);
5218078Seric 	if (c == '"')
5228078Seric 		return (QST);
5234100Seric 	if (!isascii(c))
5248078Seric 		return (ATM);
5258078Seric 	if (isspace(c) || c == ')')
5268078Seric 		return (SPC);
52756795Seric 	if (iscntrl(c) || strchr(buf, c) != NULL)
5288078Seric 		return (OPR);
5298078Seric 	return (ATM);
5303149Seric }
5313149Seric /*
5323149Seric **  REWRITE -- apply rewrite rules to token vector.
5333149Seric **
5344476Seric **	This routine is an ordered production system.  Each rewrite
5354476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5364476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5374476Seric **
5384476Seric **	For each rewrite rule, 'avp' points the address vector we
5394476Seric **	are trying to match against, and 'pvp' points to the pattern.
5408058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5419585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5429585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5434476Seric **
5444476Seric **	When a match between avp & pvp does not match, we try to
5459585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5464476Seric **	we must also back out the match in mvp.  If we reach a
5478058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5488058Seric **	over again.
5494476Seric **
5504476Seric **	When we finally match, we rewrite the address vector
5514476Seric **	and try over again.
5524476Seric **
5533149Seric **	Parameters:
5543149Seric **		pvp -- pointer to token vector.
5553149Seric **
5563149Seric **	Returns:
5573149Seric **		none.
5583149Seric **
5593149Seric **	Side Effects:
5603149Seric **		pvp is modified.
5613149Seric */
5622091Seric 
5633149Seric struct match
5643149Seric {
5654468Seric 	char	**first;	/* first token matched */
5664468Seric 	char	**last;		/* last token matched */
5673149Seric };
5683149Seric 
5694468Seric # define MAXMATCH	9	/* max params per rewrite */
5703149Seric 
5713149Seric 
5724070Seric rewrite(pvp, ruleset)
5733149Seric 	char **pvp;
5744070Seric 	int ruleset;
5753149Seric {
5763149Seric 	register char *ap;		/* address pointer */
5773149Seric 	register char *rp;		/* rewrite pointer */
5783149Seric 	register char **avp;		/* address vector pointer */
5793149Seric 	register char **rvp;		/* rewrite vector pointer */
5808058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5818058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
58256678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5833149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5843149Seric 
5859279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5863149Seric 	{
5878959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
58856678Seric 		printav(pvp);
5893149Seric 	}
59056678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
59156326Seric 	{
59256678Seric 		syserr("rewrite: illegal ruleset number %d", ruleset);
59356326Seric 		return;
59456326Seric 	}
59556678Seric 	if (pvp == NULL)
59656678Seric 		return;
59756326Seric 
5983149Seric 	/*
59956678Seric 	**  Run through the list of rewrite rules, applying
60056678Seric 	**	any that match.
6013149Seric 	*/
6023149Seric 
6034070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6043149Seric 	{
60556678Seric 		int loopcount = 0;
60656678Seric 
6077675Seric 		if (tTd(21, 12))
608297Seric 		{
6098069Seric 			printf("-----trying rule:");
61056678Seric 			printav(rwr->r_lhs);
6113149Seric 		}
6123149Seric 
6133149Seric 		/* try to match on this rule */
6144468Seric 		mlp = mlist;
6158058Seric 		rvp = rwr->r_lhs;
6168058Seric 		avp = pvp;
6178058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
6183149Seric 		{
61956678Seric 			if (++loopcount > 100)
62052637Seric 			{
62156678Seric 				syserr("Infinite loop in ruleset %d", ruleset);
62256678Seric 				printf("workspace: ");
62356678Seric 				printav(pvp);
62452637Seric 				break;
62552637Seric 			}
6263149Seric 			rp = *rvp;
6278058Seric 			if (tTd(21, 35))
6288058Seric 			{
62956678Seric 				printf("operator=");
6308058Seric 				xputs(ap);
63156678Seric 				printf(", token=");
6328058Seric 				xputs(rp);
6338069Seric 				printf("\n");
6348058Seric 			}
63556678Seric 			if (rp == NULL)
63656326Seric 			{
6373149Seric 				/* end-of-pattern before end-of-address */
6388058Seric 				goto backup;
63956678Seric 			}
64056678Seric 			if (ap == NULL && *rp != MATCHZANY)
64156326Seric 			{
64256678Seric 				/* end-of-input */
64356678Seric 				break;
644297Seric 			}
64556326Seric 
64656678Seric 			switch (*rp)
6478058Seric 			{
64856678Seric 				register STAB *s;
64956326Seric 
65056678Seric 			  case MATCHCLASS:
65156678Seric 			  case MATCHNCLASS:
65256678Seric 				/* match any token in (not in) a class */
65356678Seric 				s = stab(ap, ST_CLASS, ST_FIND);
65456678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
65556326Seric 				{
65656678Seric 					if (*rp == MATCHCLASS)
6579585Seric 						goto backup;
65856326Seric 				}
65956678Seric 				else if (*rp == MATCHNCLASS)
66056678Seric 					goto backup;
6614060Seric 
66256678Seric 				/* explicit fall-through */
66356678Seric 
66456678Seric 			  case MATCHONE:
66556678Seric 			  case MATCHANY:
66656678Seric 				/* match exactly one token */
66756678Seric 				mlp->first = avp;
66856678Seric 				mlp->last = avp++;
6698058Seric 				mlp++;
67056678Seric 				break;
6718058Seric 
67256678Seric 			  case MATCHZANY:
67356678Seric 				/* match zero or more tokens */
67456678Seric 				mlp->first = avp;
67556678Seric 				mlp->last = avp - 1;
67656678Seric 				mlp++;
67756678Seric 				break;
67856326Seric 
67956678Seric 			  default:
68056678Seric 				/* must have exact match */
68156678Seric 				if (strcasecmp(rp, ap))
6828058Seric 					goto backup;
6834468Seric 				avp++;
68456678Seric 				break;
6853149Seric 			}
6863149Seric 
68756678Seric 			/* successful match on this token */
6883149Seric 			rvp++;
6893149Seric 			continue;
6903149Seric 
69156678Seric 		  backup:
69256678Seric 			/* match failed -- back up */
69356678Seric 			while (--rvp >= rwr->r_lhs)
6943149Seric 			{
69556678Seric 				rp = *rvp;
69656678Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
6974468Seric 				{
69856678Seric 					/* extend binding and continue */
69956678Seric 					avp = ++mlp[-1].last;
70056678Seric 					avp++;
70156678Seric 					rvp++;
70256678Seric 					break;
7034468Seric 				}
70456678Seric 				avp--;
70556678Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS ||
70656678Seric 				    *rp == MATCHNCLASS)
70756678Seric 				{
70856678Seric 					/* back out binding */
70956678Seric 					mlp--;
71056678Seric 				}
7113149Seric 			}
7123149Seric 
71356678Seric 			if (rvp < rwr->r_lhs)
71456678Seric 			{
71556678Seric 				/* total failure to match */
71656326Seric 				break;
7173149Seric 			}
718297Seric 		}
7193149Seric 
7203149Seric 		/*
72156678Seric 		**  See if we successfully matched
7223149Seric 		*/
7233149Seric 
72456678Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
7253149Seric 		{
7269374Seric 			if (tTd(21, 10))
7279374Seric 				printf("----- rule fails\n");
7289374Seric 			rwr = rwr->r_next;
7299374Seric 			continue;
7309374Seric 		}
7313149Seric 
7329374Seric 		rvp = rwr->r_rhs;
7339374Seric 		if (tTd(21, 12))
7349374Seric 		{
7359374Seric 			printf("-----rule matches:");
73656678Seric 			printav(rvp);
7379374Seric 		}
7389374Seric 
7399374Seric 		rp = *rvp;
7409374Seric 		if (*rp == CANONUSER)
7419374Seric 		{
7429374Seric 			rvp++;
7439374Seric 			rwr = rwr->r_next;
7449374Seric 		}
7459374Seric 		else if (*rp == CANONHOST)
7469374Seric 		{
7479374Seric 			rvp++;
7489374Seric 			rwr = NULL;
7499374Seric 		}
7509374Seric 		else if (*rp == CANONNET)
7519374Seric 			rwr = NULL;
7529374Seric 
7539374Seric 		/* substitute */
7549374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
7559374Seric 		{
7569374Seric 			register struct match *m;
7579374Seric 			register char **pp;
7589374Seric 
7598058Seric 			rp = *rvp;
76056678Seric 			if (*rp == MATCHREPL)
7618058Seric 			{
76216914Seric 				/* substitute from LHS */
76316914Seric 				m = &mlist[rp[1] - '1'];
76456678Seric 				if (m < mlist || m >= mlp)
7659374Seric 				{
76656678Seric 					syserr("rewrite: ruleset %d: replacement $%c out of bounds",
76756326Seric 						ruleset, rp[1]);
7689374Seric 					return;
7699374Seric 				}
77016914Seric 				if (tTd(21, 15))
77116914Seric 				{
77216914Seric 					printf("$%c:", rp[1]);
77316914Seric 					pp = m->first;
77456678Seric 					while (pp <= m->last)
77516914Seric 					{
77616914Seric 						printf(" %x=\"", *pp);
77716914Seric 						(void) fflush(stdout);
77816914Seric 						printf("%s\"", *pp++);
77916914Seric 					}
78016914Seric 					printf("\n");
78116914Seric 				}
7829374Seric 				pp = m->first;
78356678Seric 				while (pp <= m->last)
7843149Seric 				{
78516914Seric 					if (avp >= &npvp[MAXATOM])
78656678Seric 					{
78756678Seric 						syserr("rewrite: expansion too long");
78856678Seric 						return;
78956678Seric 					}
79016914Seric 					*avp++ = *pp++;
7913149Seric 				}
7923149Seric 			}
79316914Seric 			else
7948226Seric 			{
79516914Seric 				/* vanilla replacement */
7969374Seric 				if (avp >= &npvp[MAXATOM])
79716889Seric 				{
79856678Seric 	toolong:
79916889Seric 					syserr("rewrite: expansion too long");
80016889Seric 					return;
80116889Seric 				}
80256678Seric 				*avp++ = rp;
8038226Seric 			}
8049374Seric 		}
8059374Seric 		*avp++ = NULL;
80616914Seric 
80716914Seric 		/*
80856678Seric 		**  Check for any hostname/keyword lookups.
80916914Seric 		*/
81016914Seric 
81116914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
81216914Seric 		{
81356678Seric 			char **hbrvp;
81416914Seric 			char **xpvp;
81516914Seric 			int trsize;
81617473Seric 			char *olddelimchar;
81756678Seric 			char *replac;
81856678Seric 			int endtoken;
81956678Seric 			STAB *map;
82056678Seric 			char *mapname;
82156678Seric 			char **key_rvp;
82256678Seric 			char **arg_rvp;
82356678Seric 			char **default_rvp;
82456678Seric 			char buf[MAXNAME + 1];
82516914Seric 			char *pvpb1[MAXATOM + 1];
82656823Seric 			char *argvect[10];
82717174Seric 			char pvpbuf[PSBUFSIZE];
82856678Seric 			extern char *DelimChar;
82916914Seric 
83056678Seric 			if (**rvp != HOSTBEGIN && **rvp != LOOKUPBEGIN)
83116914Seric 				continue;
83216914Seric 
83316914Seric 			/*
83456678Seric 			**  Got a hostname/keyword lookup.
83516914Seric 			**
83616914Seric 			**	This could be optimized fairly easily.
83716914Seric 			*/
83816914Seric 
83916914Seric 			hbrvp = rvp;
84056678Seric 			if (**rvp == HOSTBEGIN)
84156327Seric 			{
84256678Seric 				endtoken = HOSTEND;
84356678Seric 				mapname = "host";
84456327Seric 			}
84556326Seric 			else
84656327Seric 			{
84756678Seric 				endtoken = LOOKUPEND;
84856678Seric 				mapname = *++rvp;
84956327Seric 			}
85056678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
85156678Seric 			if (map == NULL)
85256678Seric 				syserr("rewrite: map %s not found", mapname);
85356678Seric 
85456678Seric 			/* extract the match part */
85556678Seric 			key_rvp = ++rvp;
85656823Seric 			default_rvp = NULL;
85756823Seric 			arg_rvp = argvect;
85856823Seric 			xpvp = NULL;
85956823Seric 			replac = pvpbuf;
86056678Seric 			while (*rvp != NULL && **rvp != endtoken)
86153654Seric 			{
86256823Seric 				int nodetype = **rvp;
86356823Seric 
86456823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
86556678Seric 				{
86656823Seric 					rvp++;
86756823Seric 					continue;
86856823Seric 				}
86956823Seric 
87056823Seric 				*rvp++ = NULL;
87156823Seric 
87256823Seric 				if (xpvp != NULL)
87356823Seric 				{
87456823Seric 					cataddr(xpvp, replac,
87556823Seric 						&pvpbuf[sizeof pvpbuf] - replac);
87656823Seric 					*++arg_rvp = replac;
87756823Seric 					replac += strlen(replac) + 1;
87856823Seric 					xpvp = NULL;
87956823Seric 				}
88056823Seric 				switch (nodetype)
88156823Seric 				{
88256678Seric 				  case CANONHOST:
88356823Seric 					xpvp = rvp;
88456678Seric 					break;
88556678Seric 
88656678Seric 				  case CANONUSER:
88756678Seric 					default_rvp = rvp;
88856678Seric 					break;
88956678Seric 				}
89053654Seric 			}
89116914Seric 			if (*rvp != NULL)
89216914Seric 				*rvp++ = NULL;
89356823Seric 			if (xpvp != NULL)
89456823Seric 			{
89556823Seric 				cataddr(xpvp, replac,
89656823Seric 					&pvpbuf[sizeof pvpbuf] - replac);
89756823Seric 				*++arg_rvp = replac;
89856823Seric 			}
89956823Seric 			*++arg_rvp = NULL;
90016914Seric 
90116914Seric 			/* save the remainder of the input string */
90216914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
90316914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
90416914Seric 
90556678Seric 			/* look it up */
90656678Seric 			cataddr(key_rvp, buf, sizeof buf);
90756823Seric 			argvect[0] = buf;
90856678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
90956836Seric 			{
91056836Seric 				int bsize = sizeof buf - 1;
91156836Seric 
91256836Seric 				if (map->s_map.map_app != NULL)
91356836Seric 					bsize -= strlen(map->s_map.map_app);
91456836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
91556823Seric 						buf, sizeof buf - 1, argvect);
91656836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
91756836Seric 					strcat(replac, map->s_map.map_app);
91856836Seric 			}
91953654Seric 			else
92056678Seric 				replac = NULL;
92156678Seric 
92256678Seric 			/* if no replacement, use default */
92356823Seric 			if (replac == NULL && default_rvp != NULL)
92456823Seric 			{
92556823Seric 				char buf2[sizeof buf];
92656823Seric 
92756823Seric 				/* rewrite the default with % translations */
92856823Seric 				cataddr(default_rvp, buf2, sizeof buf2);
92956823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
93056823Seric 					argvect);
93156823Seric 				replac = buf;
93256823Seric 			}
93356823Seric 
93456678Seric 			if (replac == NULL)
93551317Seric 			{
93656823Seric 				xpvp = key_rvp;
93753654Seric 			}
93856678Seric 			else
93953654Seric 			{
94056678Seric 				/* scan the new replacement */
94156678Seric 				olddelimchar = DelimChar;
94256678Seric 				xpvp = prescan(replac, '\0', pvpbuf);
94356678Seric 				DelimChar = olddelimchar;
94453654Seric 				if (xpvp == NULL)
94551317Seric 				{
94656678Seric 					syserr("rewrite: cannot prescan map value: %s", replac);
94756678Seric 					return;
94856678Seric 				}
94951317Seric 			}
95051317Seric 
95116914Seric 			/* append it to the token list */
95256678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
95356678Seric 			{
95417174Seric 				*avp++ = newstr(*xpvp);
95516920Seric 				if (avp >= &npvp[MAXATOM])
95616914Seric 					goto toolong;
95717174Seric 			}
95816914Seric 
95916914Seric 			/* restore the old trailing information */
96056678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
96116920Seric 				if (avp >= &npvp[MAXATOM])
96216914Seric 					goto toolong;
96317174Seric 
96456678Seric 			break;
96516914Seric 		}
96616914Seric 
96716914Seric 		/*
96816914Seric 		**  Check for subroutine calls.
96916914Seric 		*/
97016914Seric 
97156678Seric 		if (*npvp != NULL && **npvp == CALLSUBR)
97256678Seric 		{
97356678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
97456678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
97556678Seric 			if (tTd(21, 3))
97656678Seric 				printf("-----callsubr %s\n", npvp[1]);
97756678Seric 			rewrite(pvp, atoi(npvp[1]));
97856678Seric 		}
97956678Seric 		else
98056678Seric 		{
98117348Seric 			bcopy((char *) npvp, (char *) pvp,
98216900Seric 				(int) (avp - npvp) * sizeof *avp);
98356678Seric 		}
9849374Seric 		if (tTd(21, 4))
9859374Seric 		{
9869374Seric 			printf("rewritten as:");
98756678Seric 			printav(pvp);
9889374Seric 		}
989297Seric 	}
9908069Seric 
9919279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
9928069Seric 	{
9938959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
99456678Seric 		printav(pvp);
9958069Seric 	}
9963149Seric }
9973149Seric /*
9983149Seric **  BUILDADDR -- build address from token vector.
9993149Seric **
10003149Seric **	Parameters:
10013149Seric **		tv -- token vector.
10023149Seric **		a -- pointer to address descriptor to fill.
10033149Seric **			If NULL, one will be allocated.
10043149Seric **
10053149Seric **	Returns:
10064279Seric **		NULL if there was an error.
10074279Seric **		'a' otherwise.
10083149Seric **
10093149Seric **	Side Effects:
10103149Seric **		fills in 'a'
10113149Seric */
10123149Seric 
101357249Seric struct errcodes
101457249Seric {
101557249Seric 	char	*ec_name;		/* name of error code */
101657249Seric 	int	ec_code;		/* numeric code */
101757249Seric } ErrorCodes[] =
101857249Seric {
101957249Seric 	"usage",	EX_USAGE,
102057249Seric 	"nouser",	EX_NOUSER,
102157249Seric 	"nohost",	EX_NOHOST,
102257249Seric 	"unavailable",	EX_UNAVAILABLE,
102357249Seric 	"software",	EX_SOFTWARE,
102457249Seric 	"tempfail",	EX_TEMPFAIL,
102557249Seric 	"protocol",	EX_PROTOCOL,
102657249Seric #ifdef EX_CONFIG
102757249Seric 	"config",	EX_CONFIG,
102857249Seric #endif
102957249Seric 	NULL,		EX_UNAVAILABLE,
103057249Seric };
103157249Seric 
103256678Seric ADDRESS *
10333149Seric buildaddr(tv, a)
10343149Seric 	register char **tv;
10353149Seric 	register ADDRESS *a;
10363149Seric {
10373149Seric 	static char buf[MAXNAME];
10383149Seric 	struct mailer **mp;
10393149Seric 	register struct mailer *m;
10403149Seric 
10413149Seric 	if (a == NULL)
10423149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
104316889Seric 	bzero((char *) a, sizeof *a);
10443149Seric 
10453149Seric 	/* figure out what net/mailer to use */
104656678Seric 	if (**tv != CANONNET)
10474279Seric 	{
10483149Seric 		syserr("buildaddr: no net");
10494279Seric 		return (NULL);
10504279Seric 	}
10513149Seric 	tv++;
105233725Sbostic 	if (!strcasecmp(*tv, "error"))
10534279Seric 	{
105410183Seric 		if (**++tv == CANONHOST)
105510183Seric 		{
105657249Seric 			register struct errcodes *ep;
105757249Seric 
105857249Seric 			if (isdigit(**++tv))
105957249Seric 			{
106057249Seric 				setstat(atoi(*tv));
106157249Seric 			}
106257249Seric 			else
106357249Seric 			{
106457249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
106557249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
106657249Seric 						break;
106757249Seric 				setstat(ep->ec_code);
106857249Seric 			}
106910183Seric 			tv++;
107010183Seric 		}
107110183Seric 		if (**tv != CANONUSER)
10724279Seric 			syserr("buildaddr: error: no user");
107356678Seric 		buf[0] = '\0';
10744279Seric 		while (*++tv != NULL)
10754279Seric 		{
10764279Seric 			if (buf[0] != '\0')
10777005Seric 				(void) strcat(buf, " ");
10787005Seric 			(void) strcat(buf, *tv);
10794279Seric 		}
10804279Seric 		usrerr(buf);
10814279Seric 		return (NULL);
10824279Seric 	}
10834598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
10843149Seric 	{
108533725Sbostic 		if (!strcasecmp(m->m_name, *tv))
10863149Seric 			break;
10873149Seric 	}
10883149Seric 	if (m == NULL)
10894279Seric 	{
109024944Seric 		syserr("buildaddr: unknown mailer %s", *tv);
10914279Seric 		return (NULL);
10924279Seric 	}
10934598Seric 	a->q_mailer = m;
10943149Seric 
10953149Seric 	/* figure out what host (if any) */
109656678Seric 	tv++;
109757249Seric 	if (!bitnset(M_LOCAL, m->m_flags))
10983149Seric 	{
109957249Seric 		if (**tv++ != CANONHOST)
11004279Seric 		{
11013149Seric 			syserr("buildaddr: no host");
11024279Seric 			return (NULL);
11034279Seric 		}
11045704Seric 		buf[0] = '\0';
110557249Seric 		while (*tv != NULL && **tv != CANONUSER)
110657249Seric 			(void) strcat(buf, *tv++);
11075704Seric 		a->q_host = newstr(buf);
11083149Seric 	}
110957249Seric 	else
111057249Seric 		a->q_host = NULL;
11113149Seric 
11123149Seric 	/* figure out the user */
111336615Sbostic 	if (*tv == NULL || **tv != CANONUSER)
11144279Seric 	{
11153149Seric 		syserr("buildaddr: no user");
11164279Seric 		return (NULL);
11174279Seric 	}
111819040Seric 
111956678Seric 	if (m == LocalMailer && tv[1] != NULL && strcmp(tv[1], "@") == 0)
112056678Seric 	{
112156678Seric 		tv++;
112256678Seric 		a->q_flags |= QNOTREMOTE;
112356678Seric 	}
112451317Seric 
112519040Seric 	/* rewrite according recipient mailer rewriting rules */
112619040Seric 	rewrite(++tv, 2);
112756327Seric 	if (m->m_r_rwset > 0)
112856327Seric 		rewrite(tv, m->m_r_rwset);
112919040Seric 	rewrite(tv, 4);
113019040Seric 
113119040Seric 	/* save the result for the command line/RCPT argument */
113211278Seric 	cataddr(tv, buf, sizeof buf);
11333149Seric 	a->q_user = buf;
11343149Seric 
11353149Seric 	return (a);
11363149Seric }
11373188Seric /*
11384228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
11394228Seric **
11404228Seric **	Parameters:
11414228Seric **		pvp -- parameter vector to rebuild.
11424228Seric **		buf -- buffer to build the string into.
11434228Seric **		sz -- size of buf.
11444228Seric **
11454228Seric **	Returns:
11464228Seric **		none.
11474228Seric **
11484228Seric **	Side Effects:
11494228Seric **		Destroys buf.
11504228Seric */
11514228Seric 
11524228Seric cataddr(pvp, buf, sz)
11534228Seric 	char **pvp;
11544228Seric 	char *buf;
11554228Seric 	register int sz;
11564228Seric {
11574228Seric 	bool oatomtok = FALSE;
115856678Seric 	bool natomtok = FALSE;
11594228Seric 	register int i;
11604228Seric 	register char *p;
11614228Seric 
11628423Seric 	if (pvp == NULL)
11638423Seric 	{
116423109Seric 		(void) strcpy(buf, "");
11658423Seric 		return;
11668423Seric 	}
11674228Seric 	p = buf;
116811156Seric 	sz -= 2;
11694228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
11704228Seric 	{
11718078Seric 		natomtok = (toktype(**pvp) == ATM);
11724228Seric 		if (oatomtok && natomtok)
11739042Seric 			*p++ = SpaceSub;
11744228Seric 		(void) strcpy(p, *pvp);
11754228Seric 		oatomtok = natomtok;
11764228Seric 		p += i;
117711156Seric 		sz -= i + 1;
11784228Seric 		pvp++;
11794228Seric 	}
11804228Seric 	*p = '\0';
11814228Seric }
11824228Seric /*
11833188Seric **  SAMEADDR -- Determine if two addresses are the same
11843188Seric **
11853188Seric **	This is not just a straight comparison -- if the mailer doesn't
11863188Seric **	care about the host we just ignore it, etc.
11873188Seric **
11883188Seric **	Parameters:
11893188Seric **		a, b -- pointers to the internal forms to compare.
11903188Seric **
11913188Seric **	Returns:
11923188Seric **		TRUE -- they represent the same mailbox.
11933188Seric **		FALSE -- they don't.
11943188Seric **
11953188Seric **	Side Effects:
11963188Seric **		none.
11973188Seric */
11983188Seric 
11993188Seric bool
12009374Seric sameaddr(a, b)
12013188Seric 	register ADDRESS *a;
12023188Seric 	register ADDRESS *b;
12033188Seric {
12043188Seric 	/* if they don't have the same mailer, forget it */
12053188Seric 	if (a->q_mailer != b->q_mailer)
12063188Seric 		return (FALSE);
12073188Seric 
12083188Seric 	/* if the user isn't the same, we can drop out */
120956678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
12103188Seric 		return (FALSE);
12113188Seric 
12123188Seric 	/* if the mailer ignores hosts, we have succeeded! */
121310690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
12143188Seric 		return (TRUE);
12153188Seric 
12163188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
12173188Seric 	if (a->q_host == NULL || b->q_host == NULL)
12183188Seric 		return (FALSE);
121956678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
12203188Seric 		return (FALSE);
12213188Seric 
12223188Seric 	return (TRUE);
12233188Seric }
12243234Seric /*
12253234Seric **  PRINTADDR -- print address (for debugging)
12263234Seric **
12273234Seric **	Parameters:
12283234Seric **		a -- the address to print
12293234Seric **		follow -- follow the q_next chain.
12303234Seric **
12313234Seric **	Returns:
12323234Seric **		none.
12333234Seric **
12343234Seric **	Side Effects:
12353234Seric **		none.
12363234Seric */
12373234Seric 
12383234Seric printaddr(a, follow)
12393234Seric 	register ADDRESS *a;
12403234Seric 	bool follow;
12413234Seric {
12425001Seric 	bool first = TRUE;
12435001Seric 
12443234Seric 	while (a != NULL)
12453234Seric 	{
12465001Seric 		first = FALSE;
12474443Seric 		printf("%x=", a);
12484085Seric 		(void) fflush(stdout);
124940973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
125040973Sbostic 		       a->q_paddr, a->q_mailer->m_mno, a->q_mailer->m_name,
125140973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
12528181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
12538181Seric 		       a->q_alias);
12548181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
12558181Seric 		       a->q_fullname);
12564996Seric 
12573234Seric 		if (!follow)
12583234Seric 			return;
12594996Seric 		a = a->q_next;
12603234Seric 	}
12615001Seric 	if (first)
12624443Seric 		printf("[NULL]\n");
12633234Seric }
12644317Seric 
12657682Seric /*
12667682Seric **  REMOTENAME -- return the name relative to the current mailer
12677682Seric **
12687682Seric **	Parameters:
12697682Seric **		name -- the name to translate.
12708069Seric **		m -- the mailer that we want to do rewriting relative
12718069Seric **			to.
12728069Seric **		senderaddress -- if set, uses the sender rewriting rules
12738069Seric **			rather than the recipient rewriting rules.
127410310Seric **		canonical -- if set, strip out any comment information,
127510310Seric **			etc.
12767682Seric **
12777682Seric **	Returns:
12787682Seric **		the text string representing this address relative to
12797682Seric **			the receiving mailer.
12807682Seric **
12817682Seric **	Side Effects:
12827682Seric **		none.
12837682Seric **
12847682Seric **	Warnings:
12857682Seric **		The text string returned is tucked away locally;
12867682Seric **			copy it if you intend to save it.
12877682Seric */
12887682Seric 
12897682Seric char *
129056678Seric remotename(name, m, senderaddress, canonical, e)
12917682Seric 	char *name;
129256678Seric 	struct mailer *m;
12938069Seric 	bool senderaddress;
129410310Seric 	bool canonical;
129556678Seric 	register ENVELOPE *e;
12967682Seric {
12978069Seric 	register char **pvp;
12988069Seric 	char *fancy;
129956678Seric 	extern char *macvalue();
130056678Seric 	char *oldg = macvalue('g', e);
13017682Seric 	static char buf[MAXNAME];
13027682Seric 	char lbuf[MAXNAME];
130316914Seric 	char pvpbuf[PSBUFSIZE];
130456678Seric 	extern char **prescan();
130556678Seric 	extern char *crackaddr();
13067682Seric 
13077755Seric 	if (tTd(12, 1))
13087755Seric 		printf("remotename(%s)\n", name);
13097755Seric 
131010177Seric 	/* don't do anything if we are tagging it as special */
131156327Seric 	if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0)
131210177Seric 		return (name);
131310177Seric 
13147682Seric 	/*
13158181Seric 	**  Do a heuristic crack of this name to extract any comment info.
13168181Seric 	**	This will leave the name as a comment and a $g macro.
13177889Seric 	*/
13187889Seric 
131910310Seric 	if (canonical)
132016155Seric 		fancy = "\001g";
132110310Seric 	else
132210310Seric 		fancy = crackaddr(name);
13237889Seric 
13248181Seric 	/*
13258181Seric 	**  Turn the name into canonical form.
13268181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
13278181Seric 	**	If this only resolves to "user", and the "C" flag is
13288181Seric 	**	specified in the sending mailer, then the sender's
13298181Seric 	**	domain will be appended.
13308181Seric 	*/
13318181Seric 
133216914Seric 	pvp = prescan(name, '\0', pvpbuf);
13337889Seric 	if (pvp == NULL)
13347889Seric 		return (name);
13358181Seric 	rewrite(pvp, 3);
133656678Seric 	if (e->e_fromdomain != NULL)
13378181Seric 	{
13388181Seric 		/* append from domain to this address */
13398181Seric 		register char **pxp = pvp;
13408181Seric 
13419594Seric 		/* see if there is an "@domain" in the current name */
13428181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
13438181Seric 			pxp++;
13448181Seric 		if (*pxp == NULL)
13458181Seric 		{
13469594Seric 			/* no.... append the "@domain" from the sender */
134756678Seric 			register char **qxq = e->e_fromdomain;
13488181Seric 
13499594Seric 			while ((*pxp++ = *qxq++) != NULL)
13509594Seric 				continue;
135111726Seric 			rewrite(pvp, 3);
13528181Seric 		}
13538181Seric 	}
13548181Seric 
13558181Seric 	/*
13568959Seric 	**  Do more specific rewriting.
135756678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
135856678Seric 	**		a sender address or not.
13598181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
13608181Seric 	*/
13618181Seric 
13628069Seric 	if (senderaddress)
13637755Seric 	{
136456327Seric 		rewrite(pvp, 1);
136556327Seric 		if (m->m_s_rwset > 0)
136656327Seric 			rewrite(pvp, m->m_s_rwset);
13678069Seric 	}
13688069Seric 	else
13698069Seric 	{
137056327Seric 		rewrite(pvp, 2);
137156327Seric 		if (m->m_r_rwset > 0)
137256327Seric 			rewrite(pvp, m->m_r_rwset);
13737682Seric 	}
13747682Seric 
13758181Seric 	/*
13768959Seric 	**  Do any final sanitation the address may require.
13778959Seric 	**	This will normally be used to turn internal forms
13788959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
13798959Seric 	**	may be used as a default to the above rules.
13808959Seric 	*/
13818959Seric 
13828959Seric 	rewrite(pvp, 4);
13838959Seric 
13848959Seric 	/*
13858181Seric 	**  Now restore the comment information we had at the beginning.
13868181Seric 	*/
13878181Seric 
13887682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
138956678Seric 	define('g', lbuf, e);
139056678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
139156678Seric 	define('g', oldg, e);
13927682Seric 
13937682Seric 	if (tTd(12, 1))
13947755Seric 		printf("remotename => `%s'\n", buf);
13957682Seric 	return (buf);
13967682Seric }
139751317Seric /*
139856678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
139951317Seric **
140051317Seric **	Parameters:
140156678Seric **		a -- the address to map (but just the user name part).
140256678Seric **		sendq -- the sendq in which to install any replacement
140356678Seric **			addresses.
140451317Seric **
140551317Seric **	Returns:
140651317Seric **		none.
140751317Seric */
140851317Seric 
140956678Seric maplocaluser(a, sendq, e)
141056678Seric 	register ADDRESS *a;
141156678Seric 	ADDRESS **sendq;
141256678Seric 	ENVELOPE *e;
141351317Seric {
141456678Seric 	register char **pvp;
141556678Seric 	register ADDRESS *a1 = NULL;
141656678Seric 	char pvpbuf[PSBUFSIZE];
141751317Seric 
141856678Seric 	if (tTd(29, 1))
141956678Seric 	{
142056678Seric 		printf("maplocaluser: ");
142156678Seric 		printaddr(a, FALSE);
142256678Seric 	}
142356678Seric 	pvp = prescan(a->q_user, '\0', pvpbuf);
142456678Seric 	if (pvp == NULL)
142556678Seric 		return;
142651317Seric 
142756678Seric 	rewrite(pvp, 5);
142856678Seric 	if (pvp[0] == NULL || pvp[0][0] != CANONNET)
142956678Seric 		return;
143051317Seric 
143156678Seric 	/* if non-null, mailer destination specified -- has it changed? */
143256678Seric 	a1 = buildaddr(pvp, NULL);
143356678Seric 	if (a1 == NULL || sameaddr(a, a1))
143456678Seric 		return;
143551317Seric 
143656678Seric 	/* mark old address as dead; insert new address */
143756678Seric 	a->q_flags |= QDONTSEND;
143856678Seric 	a1->q_alias = a;
143956678Seric 	allocaddr(a1, 1, NULL);
144056678Seric 	(void) recipient(a1, sendq, e);
144151317Seric }
1442