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*58082Seric static char sccsid[] = "@(#)parseaddr.c	6.17 (Berkeley) 02/20/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 */
5558050Seric # define DELIMCHARS	"\201()<>,;\\\"\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 
10658050Seric 	if ((pvp[0][0] & 0377) != 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 	{
15758050Seric 		if ((*addr & 0340) != 0200)
15857388Seric 			continue;
15957388Seric 		setstat(EX_USAGE);
16057391Seric 		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++;
36657631Seric 			if (c == '\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 			}
38957631Seric 			else if (c == delim && anglecnt <= 0 &&
39057631Seric 					cmntcnt <= 0 && state != QST)
39157631Seric 				break;
39256764Seric 
3938078Seric 			if (tTd(22, 101))
3948078Seric 				printf("c=%c, s=%d; ", c, state);
3958078Seric 
3963149Seric 			/* chew up special characters */
3973149Seric 			*q = '\0';
3983149Seric 			if (bslashmode)
3993149Seric 			{
40024944Seric 				/* kludge \! for naive users */
40158061Seric 				if (cmntcnt > 0)
40258061Seric 					c = NOCHAR;
40358061Seric 				else if (c != '!')
40456678Seric 					*q++ = '\\';
4053149Seric 				bslashmode = FALSE;
40656678Seric 				continue;
4073149Seric 			}
40856678Seric 
40956678Seric 			if (c == '\\')
4103149Seric 			{
4113149Seric 				bslashmode = TRUE;
4128078Seric 				c = NOCHAR;
41356678Seric 				continue;
4143149Seric 			}
41556678Seric 			else if (state == QST)
4168514Seric 			{
4178514Seric 				/* do nothing, just avoid next clauses */
4188514Seric 			}
4198078Seric 			else if (c == '(')
4204100Seric 			{
4218078Seric 				cmntcnt++;
4228078Seric 				c = NOCHAR;
4234100Seric 			}
4248078Seric 			else if (c == ')')
4253149Seric 			{
4268078Seric 				if (cmntcnt <= 0)
4273149Seric 				{
4288078Seric 					usrerr("Unbalanced ')'");
4298078Seric 					DelimChar = p;
4308078Seric 					return (NULL);
4313149Seric 				}
4328078Seric 				else
4338078Seric 					cmntcnt--;
4348078Seric 			}
4358078Seric 			else if (cmntcnt > 0)
4368078Seric 				c = NOCHAR;
4378423Seric 			else if (c == '<')
4388423Seric 				anglecnt++;
4398423Seric 			else if (c == '>')
4408423Seric 			{
4418423Seric 				if (anglecnt <= 0)
4428423Seric 				{
4438423Seric 					usrerr("Unbalanced '>'");
4448423Seric 					DelimChar = p;
4458423Seric 					return (NULL);
4468423Seric 				}
4478423Seric 				anglecnt--;
4488423Seric 			}
44958050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
45011423Seric 				c = ' ';
4513149Seric 
4528078Seric 			if (c == NOCHAR)
4538078Seric 				continue;
4543149Seric 
4558078Seric 			/* see if this is end of input */
45611405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4573149Seric 				break;
4583149Seric 
4598078Seric 			newstate = StateTab[state][toktype(c)];
4608078Seric 			if (tTd(22, 101))
4618078Seric 				printf("ns=%02o\n", newstate);
4628078Seric 			state = newstate & TYPE;
4638078Seric 			if (bitset(M, newstate))
4648078Seric 				c = NOCHAR;
4658078Seric 			if (bitset(B, newstate))
4664228Seric 				break;
467297Seric 		}
4683149Seric 
4693149Seric 		/* new token */
4708078Seric 		if (tok != q)
4711378Seric 		{
4728078Seric 			*q++ = '\0';
4738078Seric 			if (tTd(22, 36))
474297Seric 			{
4758078Seric 				printf("tok=");
4768078Seric 				xputs(tok);
47723109Seric 				(void) putchar('\n');
478297Seric 			}
4798078Seric 			if (avp >= &av[MAXATOM])
480297Seric 			{
4818078Seric 				syserr("prescan: too many tokens");
4828078Seric 				DelimChar = p;
4838078Seric 				return (NULL);
484297Seric 			}
4858078Seric 			*avp++ = tok;
486297Seric 		}
4878423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4883149Seric 	*avp = NULL;
4898078Seric 	DelimChar = --p;
49056764Seric 	if (tTd(22, 12))
49156764Seric 	{
49256764Seric 		printf("prescan==>");
49356764Seric 		printav(av);
49456764Seric 	}
49556764Seric 	if (av[0] != NULL)
4963149Seric 		return (av);
4973149Seric 	return (NULL);
4983149Seric }
4993149Seric /*
5003149Seric **  TOKTYPE -- return token type
5013149Seric **
5023149Seric **	Parameters:
5033149Seric **		c -- the character in question.
5043149Seric **
5053149Seric **	Returns:
5063149Seric **		Its type.
5073149Seric **
5083149Seric **	Side Effects:
5093149Seric **		none.
5103149Seric */
511297Seric 
5123149Seric toktype(c)
51358050Seric 	register int c;
5143149Seric {
5153380Seric 	static char buf[50];
5163382Seric 	static bool firstime = TRUE;
5173380Seric 
5183382Seric 	if (firstime)
5193380Seric 	{
5203382Seric 		firstime = FALSE;
52158050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5227005Seric 		(void) strcat(buf, DELIMCHARS);
5233380Seric 	}
52458050Seric 	c &= 0377;
52556327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5268078Seric 		return (ONE);
5278078Seric 	if (c == '"')
5288078Seric 		return (QST);
52958050Seric 	if ((c & 0340) == 0200)
53058050Seric 		return (OPR);
5314100Seric 	if (!isascii(c))
5328078Seric 		return (ATM);
5338078Seric 	if (isspace(c) || c == ')')
5348078Seric 		return (SPC);
53558050Seric 	if (strchr(buf, c) != NULL)
5368078Seric 		return (OPR);
5378078Seric 	return (ATM);
5383149Seric }
5393149Seric /*
5403149Seric **  REWRITE -- apply rewrite rules to token vector.
5413149Seric **
5424476Seric **	This routine is an ordered production system.  Each rewrite
5434476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5444476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5454476Seric **
5464476Seric **	For each rewrite rule, 'avp' points the address vector we
5474476Seric **	are trying to match against, and 'pvp' points to the pattern.
5488058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5499585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5509585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5514476Seric **
5524476Seric **	When a match between avp & pvp does not match, we try to
5539585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5544476Seric **	we must also back out the match in mvp.  If we reach a
5558058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5568058Seric **	over again.
5574476Seric **
5584476Seric **	When we finally match, we rewrite the address vector
5594476Seric **	and try over again.
5604476Seric **
5613149Seric **	Parameters:
5623149Seric **		pvp -- pointer to token vector.
5633149Seric **
5643149Seric **	Returns:
5653149Seric **		none.
5663149Seric **
5673149Seric **	Side Effects:
5683149Seric **		pvp is modified.
5693149Seric */
5702091Seric 
5713149Seric struct match
5723149Seric {
5734468Seric 	char	**first;	/* first token matched */
5744468Seric 	char	**last;		/* last token matched */
5753149Seric };
5763149Seric 
5774468Seric # define MAXMATCH	9	/* max params per rewrite */
5783149Seric 
5793149Seric 
5804070Seric rewrite(pvp, ruleset)
5813149Seric 	char **pvp;
5824070Seric 	int ruleset;
5833149Seric {
5843149Seric 	register char *ap;		/* address pointer */
5853149Seric 	register char *rp;		/* rewrite pointer */
5863149Seric 	register char **avp;		/* address vector pointer */
5873149Seric 	register char **rvp;		/* rewrite vector pointer */
5888058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5898058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
59056678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5913149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5923149Seric 
5939279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5943149Seric 	{
5958959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
59656678Seric 		printav(pvp);
5973149Seric 	}
59856678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
59956326Seric 	{
60056678Seric 		syserr("rewrite: illegal ruleset number %d", ruleset);
60156326Seric 		return;
60256326Seric 	}
60356678Seric 	if (pvp == NULL)
60456678Seric 		return;
60556326Seric 
6063149Seric 	/*
60756678Seric 	**  Run through the list of rewrite rules, applying
60856678Seric 	**	any that match.
6093149Seric 	*/
6103149Seric 
6114070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6123149Seric 	{
61356678Seric 		int loopcount = 0;
61456678Seric 
6157675Seric 		if (tTd(21, 12))
616297Seric 		{
6178069Seric 			printf("-----trying rule:");
61856678Seric 			printav(rwr->r_lhs);
6193149Seric 		}
6203149Seric 
6213149Seric 		/* try to match on this rule */
6224468Seric 		mlp = mlist;
6238058Seric 		rvp = rwr->r_lhs;
6248058Seric 		avp = pvp;
6258058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
6263149Seric 		{
62756678Seric 			if (++loopcount > 100)
62852637Seric 			{
62956678Seric 				syserr("Infinite loop in ruleset %d", ruleset);
63056678Seric 				printf("workspace: ");
63156678Seric 				printav(pvp);
63252637Seric 				break;
63352637Seric 			}
6343149Seric 			rp = *rvp;
6358058Seric 			if (tTd(21, 35))
6368058Seric 			{
63757532Seric 				printf("rp=");
63857531Seric 				xputs(rp);
63957532Seric 				printf(", ap=");
6408058Seric 				xputs(ap);
6418069Seric 				printf("\n");
6428058Seric 			}
64356678Seric 			if (rp == NULL)
64456326Seric 			{
6453149Seric 				/* end-of-pattern before end-of-address */
6468058Seric 				goto backup;
64756678Seric 			}
64858050Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY)
64956326Seric 			{
65056678Seric 				/* end-of-input */
65156678Seric 				break;
652297Seric 			}
65356326Seric 
65458050Seric 			switch (*rp & 0377)
6558058Seric 			{
65656678Seric 				register STAB *s;
65756326Seric 
65856678Seric 			  case MATCHCLASS:
65956678Seric 			  case MATCHNCLASS:
66056678Seric 				/* match any token in (not in) a class */
66156678Seric 				s = stab(ap, ST_CLASS, ST_FIND);
66256678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
66356326Seric 				{
66458050Seric 					if ((*rp & 0377) == MATCHCLASS)
6659585Seric 						goto backup;
66656326Seric 				}
66758050Seric 				else if ((*rp & 0377) == MATCHNCLASS)
66856678Seric 					goto backup;
6694060Seric 
67056678Seric 				/* explicit fall-through */
67156678Seric 
67256678Seric 			  case MATCHONE:
67356678Seric 			  case MATCHANY:
67456678Seric 				/* match exactly one token */
67556678Seric 				mlp->first = avp;
67656678Seric 				mlp->last = avp++;
6778058Seric 				mlp++;
67856678Seric 				break;
6798058Seric 
68056678Seric 			  case MATCHZANY:
68156678Seric 				/* match zero or more tokens */
68256678Seric 				mlp->first = avp;
68356678Seric 				mlp->last = avp - 1;
68456678Seric 				mlp++;
68556678Seric 				break;
68656326Seric 
68756678Seric 			  default:
68856678Seric 				/* must have exact match */
68956678Seric 				if (strcasecmp(rp, ap))
6908058Seric 					goto backup;
6914468Seric 				avp++;
69256678Seric 				break;
6933149Seric 			}
6943149Seric 
69556678Seric 			/* successful match on this token */
6963149Seric 			rvp++;
6973149Seric 			continue;
6983149Seric 
69956678Seric 		  backup:
70056678Seric 			/* match failed -- back up */
70156678Seric 			while (--rvp >= rwr->r_lhs)
7023149Seric 			{
70356678Seric 				rp = *rvp;
70458050Seric 				if ((*rp & 0377) == MATCHANY ||
70558050Seric 				    (*rp & 0377) == MATCHZANY)
7064468Seric 				{
70756678Seric 					/* extend binding and continue */
70856678Seric 					avp = ++mlp[-1].last;
70956678Seric 					avp++;
71056678Seric 					rvp++;
71156678Seric 					break;
7124468Seric 				}
71356678Seric 				avp--;
71458050Seric 				if ((*rp & 0377) == MATCHONE ||
71558050Seric 				    (*rp & 0377) == MATCHCLASS ||
71658050Seric 				    (*rp & 0377) == MATCHNCLASS)
71756678Seric 				{
71856678Seric 					/* back out binding */
71956678Seric 					mlp--;
72056678Seric 				}
7213149Seric 			}
7223149Seric 
72356678Seric 			if (rvp < rwr->r_lhs)
72456678Seric 			{
72556678Seric 				/* total failure to match */
72656326Seric 				break;
7273149Seric 			}
728297Seric 		}
7293149Seric 
7303149Seric 		/*
73156678Seric 		**  See if we successfully matched
7323149Seric 		*/
7333149Seric 
73456678Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
7353149Seric 		{
7369374Seric 			if (tTd(21, 10))
7379374Seric 				printf("----- rule fails\n");
7389374Seric 			rwr = rwr->r_next;
7399374Seric 			continue;
7409374Seric 		}
7413149Seric 
7429374Seric 		rvp = rwr->r_rhs;
7439374Seric 		if (tTd(21, 12))
7449374Seric 		{
7459374Seric 			printf("-----rule matches:");
74656678Seric 			printav(rvp);
7479374Seric 		}
7489374Seric 
7499374Seric 		rp = *rvp;
75058050Seric 		if ((*rp & 0377) == CANONUSER)
7519374Seric 		{
7529374Seric 			rvp++;
7539374Seric 			rwr = rwr->r_next;
7549374Seric 		}
75558050Seric 		else if ((*rp & 0377) == CANONHOST)
7569374Seric 		{
7579374Seric 			rvp++;
7589374Seric 			rwr = NULL;
7599374Seric 		}
76058050Seric 		else if ((*rp & 0377) == CANONNET)
7619374Seric 			rwr = NULL;
7629374Seric 
7639374Seric 		/* substitute */
7649374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
7659374Seric 		{
7669374Seric 			register struct match *m;
7679374Seric 			register char **pp;
7689374Seric 
7698058Seric 			rp = *rvp;
77058050Seric 			if ((*rp & 0377) == MATCHREPL)
7718058Seric 			{
77216914Seric 				/* substitute from LHS */
77316914Seric 				m = &mlist[rp[1] - '1'];
77456678Seric 				if (m < mlist || m >= mlp)
7759374Seric 				{
77656678Seric 					syserr("rewrite: ruleset %d: replacement $%c out of bounds",
77756326Seric 						ruleset, rp[1]);
7789374Seric 					return;
7799374Seric 				}
78016914Seric 				if (tTd(21, 15))
78116914Seric 				{
78216914Seric 					printf("$%c:", rp[1]);
78316914Seric 					pp = m->first;
78456678Seric 					while (pp <= m->last)
78516914Seric 					{
78616914Seric 						printf(" %x=\"", *pp);
78716914Seric 						(void) fflush(stdout);
78816914Seric 						printf("%s\"", *pp++);
78916914Seric 					}
79016914Seric 					printf("\n");
79116914Seric 				}
7929374Seric 				pp = m->first;
79356678Seric 				while (pp <= m->last)
7943149Seric 				{
79516914Seric 					if (avp >= &npvp[MAXATOM])
79656678Seric 					{
79756678Seric 						syserr("rewrite: expansion too long");
79856678Seric 						return;
79956678Seric 					}
80016914Seric 					*avp++ = *pp++;
8013149Seric 				}
8023149Seric 			}
80316914Seric 			else
8048226Seric 			{
80516914Seric 				/* vanilla replacement */
8069374Seric 				if (avp >= &npvp[MAXATOM])
80716889Seric 				{
80856678Seric 	toolong:
80916889Seric 					syserr("rewrite: expansion too long");
81016889Seric 					return;
81116889Seric 				}
81256678Seric 				*avp++ = rp;
8138226Seric 			}
8149374Seric 		}
8159374Seric 		*avp++ = NULL;
81616914Seric 
81716914Seric 		/*
81856678Seric 		**  Check for any hostname/keyword lookups.
81916914Seric 		*/
82016914Seric 
82116914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
82216914Seric 		{
82356678Seric 			char **hbrvp;
82416914Seric 			char **xpvp;
82516914Seric 			int trsize;
82617473Seric 			char *olddelimchar;
82756678Seric 			char *replac;
82856678Seric 			int endtoken;
82956678Seric 			STAB *map;
83056678Seric 			char *mapname;
83156678Seric 			char **key_rvp;
83256678Seric 			char **arg_rvp;
83356678Seric 			char **default_rvp;
83456678Seric 			char buf[MAXNAME + 1];
83516914Seric 			char *pvpb1[MAXATOM + 1];
83656823Seric 			char *argvect[10];
83717174Seric 			char pvpbuf[PSBUFSIZE];
83856678Seric 			extern char *DelimChar;
83916914Seric 
84058050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
84158050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
84216914Seric 				continue;
84316914Seric 
84416914Seric 			/*
84556678Seric 			**  Got a hostname/keyword lookup.
84616914Seric 			**
84716914Seric 			**	This could be optimized fairly easily.
84816914Seric 			*/
84916914Seric 
85016914Seric 			hbrvp = rvp;
85158050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
85256327Seric 			{
85356678Seric 				endtoken = HOSTEND;
85456678Seric 				mapname = "host";
85556327Seric 			}
85656326Seric 			else
85756327Seric 			{
85856678Seric 				endtoken = LOOKUPEND;
85956678Seric 				mapname = *++rvp;
86056327Seric 			}
86156678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
86256678Seric 			if (map == NULL)
86356678Seric 				syserr("rewrite: map %s not found", mapname);
86456678Seric 
86556678Seric 			/* extract the match part */
86656678Seric 			key_rvp = ++rvp;
86756823Seric 			default_rvp = NULL;
86856823Seric 			arg_rvp = argvect;
86956823Seric 			xpvp = NULL;
87056823Seric 			replac = pvpbuf;
87158050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
87253654Seric 			{
87358050Seric 				int nodetype = **rvp & 0377;
87456823Seric 
87556823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
87656678Seric 				{
87756823Seric 					rvp++;
87856823Seric 					continue;
87956823Seric 				}
88056823Seric 
88156823Seric 				*rvp++ = NULL;
88256823Seric 
88356823Seric 				if (xpvp != NULL)
88456823Seric 				{
88556823Seric 					cataddr(xpvp, replac,
886*58082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
887*58082Seric 						'\0');
88856823Seric 					*++arg_rvp = replac;
88956823Seric 					replac += strlen(replac) + 1;
89056823Seric 					xpvp = NULL;
89156823Seric 				}
89256823Seric 				switch (nodetype)
89356823Seric 				{
89456678Seric 				  case CANONHOST:
89556823Seric 					xpvp = rvp;
89656678Seric 					break;
89756678Seric 
89856678Seric 				  case CANONUSER:
89956678Seric 					default_rvp = rvp;
90056678Seric 					break;
90156678Seric 				}
90253654Seric 			}
90316914Seric 			if (*rvp != NULL)
90416914Seric 				*rvp++ = NULL;
90556823Seric 			if (xpvp != NULL)
90656823Seric 			{
90756823Seric 				cataddr(xpvp, replac,
908*58082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
909*58082Seric 					'\0');
91056823Seric 				*++arg_rvp = replac;
91156823Seric 			}
91256823Seric 			*++arg_rvp = NULL;
91316914Seric 
91416914Seric 			/* save the remainder of the input string */
91516914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
91616914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
91716914Seric 
91856678Seric 			/* look it up */
919*58082Seric 			cataddr(key_rvp, buf, sizeof buf, '\0');
92056823Seric 			argvect[0] = buf;
92156678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
92256836Seric 			{
92356836Seric 				int bsize = sizeof buf - 1;
92456836Seric 
92556836Seric 				if (map->s_map.map_app != NULL)
92656836Seric 					bsize -= strlen(map->s_map.map_app);
92756836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
92856823Seric 						buf, sizeof buf - 1, argvect);
92956836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
93056836Seric 					strcat(replac, map->s_map.map_app);
93156836Seric 			}
93253654Seric 			else
93356678Seric 				replac = NULL;
93456678Seric 
93556678Seric 			/* if no replacement, use default */
93656823Seric 			if (replac == NULL && default_rvp != NULL)
93756823Seric 			{
93856823Seric 				char buf2[sizeof buf];
93956823Seric 
94056823Seric 				/* rewrite the default with % translations */
941*58082Seric 				cataddr(default_rvp, buf2, sizeof buf2, '\0');
94256823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
94356823Seric 					argvect);
94456823Seric 				replac = buf;
94556823Seric 			}
94656823Seric 
94756678Seric 			if (replac == NULL)
94851317Seric 			{
94956823Seric 				xpvp = key_rvp;
95053654Seric 			}
95156678Seric 			else
95253654Seric 			{
95356678Seric 				/* scan the new replacement */
95456678Seric 				olddelimchar = DelimChar;
95556678Seric 				xpvp = prescan(replac, '\0', pvpbuf);
95656678Seric 				DelimChar = olddelimchar;
95753654Seric 				if (xpvp == NULL)
95851317Seric 				{
95956678Seric 					syserr("rewrite: cannot prescan map value: %s", replac);
96056678Seric 					return;
96156678Seric 				}
96251317Seric 			}
96351317Seric 
96416914Seric 			/* append it to the token list */
96556678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
96656678Seric 			{
96717174Seric 				*avp++ = newstr(*xpvp);
96816920Seric 				if (avp >= &npvp[MAXATOM])
96916914Seric 					goto toolong;
97017174Seric 			}
97116914Seric 
97216914Seric 			/* restore the old trailing information */
97356678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
97416920Seric 				if (avp >= &npvp[MAXATOM])
97516914Seric 					goto toolong;
97617174Seric 
97756678Seric 			break;
97816914Seric 		}
97916914Seric 
98016914Seric 		/*
98116914Seric 		**  Check for subroutine calls.
98216914Seric 		*/
98316914Seric 
98458050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
98556678Seric 		{
98656678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
98756678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
98856678Seric 			if (tTd(21, 3))
98956678Seric 				printf("-----callsubr %s\n", npvp[1]);
99056678Seric 			rewrite(pvp, atoi(npvp[1]));
99156678Seric 		}
99256678Seric 		else
99356678Seric 		{
99417348Seric 			bcopy((char *) npvp, (char *) pvp,
99516900Seric 				(int) (avp - npvp) * sizeof *avp);
99656678Seric 		}
9979374Seric 		if (tTd(21, 4))
9989374Seric 		{
9999374Seric 			printf("rewritten as:");
100056678Seric 			printav(pvp);
10019374Seric 		}
1002297Seric 	}
10038069Seric 
10049279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
10058069Seric 	{
10068959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
100756678Seric 		printav(pvp);
10088069Seric 	}
10093149Seric }
10103149Seric /*
10113149Seric **  BUILDADDR -- build address from token vector.
10123149Seric **
10133149Seric **	Parameters:
10143149Seric **		tv -- token vector.
10153149Seric **		a -- pointer to address descriptor to fill.
10163149Seric **			If NULL, one will be allocated.
10173149Seric **
10183149Seric **	Returns:
10194279Seric **		NULL if there was an error.
10204279Seric **		'a' otherwise.
10213149Seric **
10223149Seric **	Side Effects:
10233149Seric **		fills in 'a'
10243149Seric */
10253149Seric 
102657249Seric struct errcodes
102757249Seric {
102857249Seric 	char	*ec_name;		/* name of error code */
102957249Seric 	int	ec_code;		/* numeric code */
103057249Seric } ErrorCodes[] =
103157249Seric {
103257249Seric 	"usage",	EX_USAGE,
103357249Seric 	"nouser",	EX_NOUSER,
103457249Seric 	"nohost",	EX_NOHOST,
103557249Seric 	"unavailable",	EX_UNAVAILABLE,
103657249Seric 	"software",	EX_SOFTWARE,
103757249Seric 	"tempfail",	EX_TEMPFAIL,
103857249Seric 	"protocol",	EX_PROTOCOL,
103957249Seric #ifdef EX_CONFIG
104057249Seric 	"config",	EX_CONFIG,
104157249Seric #endif
104257249Seric 	NULL,		EX_UNAVAILABLE,
104357249Seric };
104457249Seric 
104556678Seric ADDRESS *
10463149Seric buildaddr(tv, a)
10473149Seric 	register char **tv;
10483149Seric 	register ADDRESS *a;
10493149Seric {
10503149Seric 	struct mailer **mp;
10513149Seric 	register struct mailer *m;
105258008Seric 	char *bp;
105358008Seric 	int spaceleft;
105457402Seric 	static char buf[MAXNAME];
10553149Seric 
10563149Seric 	if (a == NULL)
10573149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
105816889Seric 	bzero((char *) a, sizeof *a);
10593149Seric 
10603149Seric 	/* figure out what net/mailer to use */
106158050Seric 	if ((**tv & 0377) != CANONNET)
10624279Seric 	{
10633149Seric 		syserr("buildaddr: no net");
10644279Seric 		return (NULL);
10654279Seric 	}
10663149Seric 	tv++;
106733725Sbostic 	if (!strcasecmp(*tv, "error"))
10684279Seric 	{
106958050Seric 		if ((**++tv & 0377) == CANONHOST)
107010183Seric 		{
107157249Seric 			register struct errcodes *ep;
107257249Seric 
107358050Seric 			if (isascii(**++tv) && isdigit(**tv))
107457249Seric 			{
107557249Seric 				setstat(atoi(*tv));
107657249Seric 			}
107757249Seric 			else
107857249Seric 			{
107957249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
108057249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
108157249Seric 						break;
108257249Seric 				setstat(ep->ec_code);
108357249Seric 			}
108410183Seric 			tv++;
108510183Seric 		}
108658050Seric 		if ((**tv & 0377) != CANONUSER)
10874279Seric 			syserr("buildaddr: error: no user");
1088*58082Seric 		cataddr(++tv, buf, sizeof buf, ' ');
1089*58082Seric 		stripquotes(buf);
10904279Seric 		usrerr(buf);
10914279Seric 		return (NULL);
10924279Seric 	}
109357402Seric 
10944598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
10953149Seric 	{
109633725Sbostic 		if (!strcasecmp(m->m_name, *tv))
10973149Seric 			break;
10983149Seric 	}
10993149Seric 	if (m == NULL)
11004279Seric 	{
110124944Seric 		syserr("buildaddr: unknown mailer %s", *tv);
11024279Seric 		return (NULL);
11034279Seric 	}
11044598Seric 	a->q_mailer = m;
11053149Seric 
11063149Seric 	/* figure out what host (if any) */
110756678Seric 	tv++;
110857249Seric 	if (!bitnset(M_LOCAL, m->m_flags))
11093149Seric 	{
111058050Seric 		if ((**tv & 0377) != CANONHOST)
11114279Seric 		{
11123149Seric 			syserr("buildaddr: no host");
11134279Seric 			return (NULL);
11144279Seric 		}
111558008Seric 		bp = buf;
111658008Seric 		spaceleft = sizeof buf - 1;
111758050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
111858008Seric 		{
111958008Seric 			int i = strlen(*tv);
112058008Seric 
112158008Seric 			if (i > spaceleft)
112258008Seric 			{
112358008Seric 				/* out of space for this address */
112458008Seric 				if (spaceleft >= 0)
112558008Seric 					syserr("buildaddr: host too long (%.40s...)",
112658008Seric 						buf);
112758008Seric 				i = spaceleft;
112858008Seric 				spaceleft = 0;
112958008Seric 			}
113058008Seric 			if (i <= 0)
113158008Seric 				continue;
113258008Seric 			bcopy(*tv, bp, i);
113358008Seric 			bp += i;
113458008Seric 			spaceleft -= i;
113558008Seric 		}
113658010Seric 		*bp = '\0';
11375704Seric 		a->q_host = newstr(buf);
11383149Seric 	}
113957249Seric 	else
114057249Seric 		a->q_host = NULL;
11413149Seric 
11423149Seric 	/* figure out the user */
114358050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
11444279Seric 	{
11453149Seric 		syserr("buildaddr: no user");
11464279Seric 		return (NULL);
11474279Seric 	}
114857402Seric 	tv++;
114951317Seric 
115057402Seric 	/* do special mapping for local mailer */
115157402Seric 	if (m == LocalMailer && *tv != NULL)
115257402Seric 	{
115357454Seric 		register char *p = *tv;
115457454Seric 
115557454Seric 		if (*p == '"')
115657454Seric 			p++;
115757454Seric 		if (*p == '|')
115857402Seric 			a->q_mailer = m = ProgMailer;
115957454Seric 		else if (*p == '/')
116057402Seric 			a->q_mailer = m = FileMailer;
116157454Seric 		else if (*p == ':')
116257402Seric 		{
116357402Seric 			/* may be :include: */
1164*58082Seric 			cataddr(tv, buf, sizeof buf, '\0');
116558008Seric 			stripquotes(buf);
116658008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
116758008Seric 			{
116858008Seric 				/* if :include:, don't need further rewriting */
116957402Seric 				a->q_mailer = m = InclMailer;
117058008Seric 				a->q_user = &buf[9];
117158008Seric 				return (a);
117258008Seric 			}
117357402Seric 		}
117457402Seric 	}
117557402Seric 
117658008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
117758008Seric 	{
117858008Seric 		tv++;
117958008Seric 		a->q_flags |= QNOTREMOTE;
118058008Seric 	}
118158008Seric 
118219040Seric 	/* rewrite according recipient mailer rewriting rules */
118357402Seric 	rewrite(tv, 2);
118458020Seric 	if (m->m_re_rwset > 0)
118558020Seric 		rewrite(tv, m->m_re_rwset);
118619040Seric 	rewrite(tv, 4);
118719040Seric 
118819040Seric 	/* save the result for the command line/RCPT argument */
1189*58082Seric 	cataddr(tv, buf, sizeof buf, '\0');
11903149Seric 	a->q_user = buf;
11913149Seric 
11923149Seric 	return (a);
11933149Seric }
11943188Seric /*
11954228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
11964228Seric **
11974228Seric **	Parameters:
11984228Seric **		pvp -- parameter vector to rebuild.
11994228Seric **		buf -- buffer to build the string into.
12004228Seric **		sz -- size of buf.
1201*58082Seric **		spacesub -- the space separator character; if null,
1202*58082Seric **			use SpaceSub.
12034228Seric **
12044228Seric **	Returns:
12054228Seric **		none.
12064228Seric **
12074228Seric **	Side Effects:
12084228Seric **		Destroys buf.
12094228Seric */
12104228Seric 
1211*58082Seric cataddr(pvp, buf, sz, spacesub)
12124228Seric 	char **pvp;
12134228Seric 	char *buf;
12144228Seric 	register int sz;
1215*58082Seric 	char spacesub;
12164228Seric {
12174228Seric 	bool oatomtok = FALSE;
121856678Seric 	bool natomtok = FALSE;
12194228Seric 	register int i;
12204228Seric 	register char *p;
12214228Seric 
1222*58082Seric 	if (spacesub == '\0')
1223*58082Seric 		spacesub = SpaceSub;
1224*58082Seric 
12258423Seric 	if (pvp == NULL)
12268423Seric 	{
122723109Seric 		(void) strcpy(buf, "");
12288423Seric 		return;
12298423Seric 	}
12304228Seric 	p = buf;
123111156Seric 	sz -= 2;
12324228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
12334228Seric 	{
12348078Seric 		natomtok = (toktype(**pvp) == ATM);
12354228Seric 		if (oatomtok && natomtok)
1236*58082Seric 			*p++ = spacesub;
12374228Seric 		(void) strcpy(p, *pvp);
12384228Seric 		oatomtok = natomtok;
12394228Seric 		p += i;
124011156Seric 		sz -= i + 1;
12414228Seric 		pvp++;
12424228Seric 	}
12434228Seric 	*p = '\0';
12444228Seric }
12454228Seric /*
12463188Seric **  SAMEADDR -- Determine if two addresses are the same
12473188Seric **
12483188Seric **	This is not just a straight comparison -- if the mailer doesn't
12493188Seric **	care about the host we just ignore it, etc.
12503188Seric **
12513188Seric **	Parameters:
12523188Seric **		a, b -- pointers to the internal forms to compare.
12533188Seric **
12543188Seric **	Returns:
12553188Seric **		TRUE -- they represent the same mailbox.
12563188Seric **		FALSE -- they don't.
12573188Seric **
12583188Seric **	Side Effects:
12593188Seric **		none.
12603188Seric */
12613188Seric 
12623188Seric bool
12639374Seric sameaddr(a, b)
12643188Seric 	register ADDRESS *a;
12653188Seric 	register ADDRESS *b;
12663188Seric {
12673188Seric 	/* if they don't have the same mailer, forget it */
12683188Seric 	if (a->q_mailer != b->q_mailer)
12693188Seric 		return (FALSE);
12703188Seric 
12713188Seric 	/* if the user isn't the same, we can drop out */
127256678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
12733188Seric 		return (FALSE);
12743188Seric 
12753188Seric 	/* if the mailer ignores hosts, we have succeeded! */
127610690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
12773188Seric 		return (TRUE);
12783188Seric 
12793188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
12803188Seric 	if (a->q_host == NULL || b->q_host == NULL)
12813188Seric 		return (FALSE);
128256678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
12833188Seric 		return (FALSE);
12843188Seric 
12853188Seric 	return (TRUE);
12863188Seric }
12873234Seric /*
12883234Seric **  PRINTADDR -- print address (for debugging)
12893234Seric **
12903234Seric **	Parameters:
12913234Seric **		a -- the address to print
12923234Seric **		follow -- follow the q_next chain.
12933234Seric **
12943234Seric **	Returns:
12953234Seric **		none.
12963234Seric **
12973234Seric **	Side Effects:
12983234Seric **		none.
12993234Seric */
13003234Seric 
13013234Seric printaddr(a, follow)
13023234Seric 	register ADDRESS *a;
13033234Seric 	bool follow;
13043234Seric {
13055001Seric 	bool first = TRUE;
130657731Seric 	register MAILER *m;
130757731Seric 	MAILER pseudomailer;
13085001Seric 
13093234Seric 	while (a != NULL)
13103234Seric 	{
13115001Seric 		first = FALSE;
13124443Seric 		printf("%x=", a);
13134085Seric 		(void) fflush(stdout);
131457731Seric 
131557731Seric 		/* find the mailer -- carefully */
131657731Seric 		m = a->q_mailer;
131757731Seric 		if (m == NULL)
131857731Seric 		{
131957731Seric 			m = &pseudomailer;
132057731Seric 			m->m_mno = -1;
132157731Seric 			m->m_name = "NULL";
132257731Seric 		}
132357731Seric 
132440973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
132557731Seric 		       a->q_paddr, m->m_mno, m->m_name,
132640973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
13278181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
13288181Seric 		       a->q_alias);
13298181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
13308181Seric 		       a->q_fullname);
13314996Seric 
13323234Seric 		if (!follow)
13333234Seric 			return;
13344996Seric 		a = a->q_next;
13353234Seric 	}
13365001Seric 	if (first)
13374443Seric 		printf("[NULL]\n");
13383234Seric }
13394317Seric 
13407682Seric /*
13417682Seric **  REMOTENAME -- return the name relative to the current mailer
13427682Seric **
13437682Seric **	Parameters:
13447682Seric **		name -- the name to translate.
13458069Seric **		m -- the mailer that we want to do rewriting relative
13468069Seric **			to.
13478069Seric **		senderaddress -- if set, uses the sender rewriting rules
13488069Seric **			rather than the recipient rewriting rules.
134958020Seric **		header -- set if this address is in the header, rather
135058020Seric **			than an envelope header.
135110310Seric **		canonical -- if set, strip out any comment information,
135210310Seric **			etc.
135358020Seric **		e -- the current envelope.
13547682Seric **
13557682Seric **	Returns:
13567682Seric **		the text string representing this address relative to
13577682Seric **			the receiving mailer.
13587682Seric **
13597682Seric **	Side Effects:
13607682Seric **		none.
13617682Seric **
13627682Seric **	Warnings:
13637682Seric **		The text string returned is tucked away locally;
13647682Seric **			copy it if you intend to save it.
13657682Seric */
13667682Seric 
13677682Seric char *
136858020Seric remotename(name, m, senderaddress, header, canonical, e)
13697682Seric 	char *name;
137056678Seric 	struct mailer *m;
13718069Seric 	bool senderaddress;
137258020Seric 	bool header;
137310310Seric 	bool canonical;
137456678Seric 	register ENVELOPE *e;
13757682Seric {
13768069Seric 	register char **pvp;
13778069Seric 	char *fancy;
137856678Seric 	extern char *macvalue();
137956678Seric 	char *oldg = macvalue('g', e);
138058020Seric 	int rwset;
13817682Seric 	static char buf[MAXNAME];
13827682Seric 	char lbuf[MAXNAME];
138316914Seric 	char pvpbuf[PSBUFSIZE];
138456678Seric 	extern char **prescan();
138556678Seric 	extern char *crackaddr();
13867682Seric 
13877755Seric 	if (tTd(12, 1))
13887755Seric 		printf("remotename(%s)\n", name);
13897755Seric 
139010177Seric 	/* don't do anything if we are tagging it as special */
139158020Seric 	if (senderaddress)
139258020Seric 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
139358020Seric 	else
139458020Seric 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
139558020Seric 	if (rwset < 0)
139610177Seric 		return (name);
139710177Seric 
13987682Seric 	/*
13998181Seric 	**  Do a heuristic crack of this name to extract any comment info.
14008181Seric 	**	This will leave the name as a comment and a $g macro.
14017889Seric 	*/
14027889Seric 
140358036Seric 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
140458050Seric 		fancy = "\201g";
140510310Seric 	else
140610310Seric 		fancy = crackaddr(name);
14077889Seric 
14088181Seric 	/*
14098181Seric 	**  Turn the name into canonical form.
14108181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
14118181Seric 	**	If this only resolves to "user", and the "C" flag is
14128181Seric 	**	specified in the sending mailer, then the sender's
14138181Seric 	**	domain will be appended.
14148181Seric 	*/
14158181Seric 
141616914Seric 	pvp = prescan(name, '\0', pvpbuf);
14177889Seric 	if (pvp == NULL)
14187889Seric 		return (name);
14198181Seric 	rewrite(pvp, 3);
142056678Seric 	if (e->e_fromdomain != NULL)
14218181Seric 	{
14228181Seric 		/* append from domain to this address */
14238181Seric 		register char **pxp = pvp;
14248181Seric 
14259594Seric 		/* see if there is an "@domain" in the current name */
14268181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
14278181Seric 			pxp++;
14288181Seric 		if (*pxp == NULL)
14298181Seric 		{
14309594Seric 			/* no.... append the "@domain" from the sender */
143156678Seric 			register char **qxq = e->e_fromdomain;
14328181Seric 
14339594Seric 			while ((*pxp++ = *qxq++) != NULL)
14349594Seric 				continue;
143511726Seric 			rewrite(pvp, 3);
14368181Seric 		}
14378181Seric 	}
14388181Seric 
14398181Seric 	/*
14408959Seric 	**  Do more specific rewriting.
144156678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
144256678Seric 	**		a sender address or not.
14438181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
14448181Seric 	*/
14458181Seric 
14468069Seric 	if (senderaddress)
144756327Seric 		rewrite(pvp, 1);
14488069Seric 	else
144956327Seric 		rewrite(pvp, 2);
145058020Seric 	if (rwset > 0)
145158020Seric 		rewrite(pvp, rwset);
14527682Seric 
14538181Seric 	/*
14548959Seric 	**  Do any final sanitation the address may require.
14558959Seric 	**	This will normally be used to turn internal forms
14568959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
14578959Seric 	**	may be used as a default to the above rules.
14588959Seric 	*/
14598959Seric 
14608959Seric 	rewrite(pvp, 4);
14618959Seric 
14628959Seric 	/*
14638181Seric 	**  Now restore the comment information we had at the beginning.
14648181Seric 	*/
14658181Seric 
1466*58082Seric 	cataddr(pvp, lbuf, sizeof lbuf, '\0');
146756678Seric 	define('g', lbuf, e);
146856678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
146956678Seric 	define('g', oldg, e);
14707682Seric 
14717682Seric 	if (tTd(12, 1))
14727755Seric 		printf("remotename => `%s'\n", buf);
14737682Seric 	return (buf);
14747682Seric }
147551317Seric /*
147656678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
147751317Seric **
147851317Seric **	Parameters:
147956678Seric **		a -- the address to map (but just the user name part).
148056678Seric **		sendq -- the sendq in which to install any replacement
148156678Seric **			addresses.
148251317Seric **
148351317Seric **	Returns:
148451317Seric **		none.
148551317Seric */
148651317Seric 
148756678Seric maplocaluser(a, sendq, e)
148856678Seric 	register ADDRESS *a;
148956678Seric 	ADDRESS **sendq;
149056678Seric 	ENVELOPE *e;
149151317Seric {
149256678Seric 	register char **pvp;
149356678Seric 	register ADDRESS *a1 = NULL;
149456678Seric 	char pvpbuf[PSBUFSIZE];
149551317Seric 
149656678Seric 	if (tTd(29, 1))
149756678Seric 	{
149856678Seric 		printf("maplocaluser: ");
149956678Seric 		printaddr(a, FALSE);
150056678Seric 	}
150156678Seric 	pvp = prescan(a->q_user, '\0', pvpbuf);
150256678Seric 	if (pvp == NULL)
150356678Seric 		return;
150451317Seric 
150556678Seric 	rewrite(pvp, 5);
150658050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
150756678Seric 		return;
150851317Seric 
150956678Seric 	/* if non-null, mailer destination specified -- has it changed? */
151056678Seric 	a1 = buildaddr(pvp, NULL);
151156678Seric 	if (a1 == NULL || sameaddr(a, a1))
151256678Seric 		return;
151351317Seric 
151456678Seric 	/* mark old address as dead; insert new address */
151556678Seric 	a->q_flags |= QDONTSEND;
151657731Seric 	if (tTd(29, 5))
151757731Seric 	{
151857731Seric 		printf("maplocaluser: QDONTSEND ");
151957731Seric 		printaddr(a, FALSE);
152057731Seric 	}
152156678Seric 	a1->q_alias = a;
152256678Seric 	allocaddr(a1, 1, NULL);
152356678Seric 	(void) recipient(a1, sendq, e);
152451317Seric }
1525