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*58061Seric static char sccsid[] = "@(#)parseaddr.c	6.16 (Berkeley) 02/19/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 */
401*58061Seric 				if (cmntcnt > 0)
402*58061Seric 					c = NOCHAR;
403*58061Seric 				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,
88656823Seric 						&pvpbuf[sizeof pvpbuf] - replac);
88756823Seric 					*++arg_rvp = replac;
88856823Seric 					replac += strlen(replac) + 1;
88956823Seric 					xpvp = NULL;
89056823Seric 				}
89156823Seric 				switch (nodetype)
89256823Seric 				{
89356678Seric 				  case CANONHOST:
89456823Seric 					xpvp = rvp;
89556678Seric 					break;
89656678Seric 
89756678Seric 				  case CANONUSER:
89856678Seric 					default_rvp = rvp;
89956678Seric 					break;
90056678Seric 				}
90153654Seric 			}
90216914Seric 			if (*rvp != NULL)
90316914Seric 				*rvp++ = NULL;
90456823Seric 			if (xpvp != NULL)
90556823Seric 			{
90656823Seric 				cataddr(xpvp, replac,
90756823Seric 					&pvpbuf[sizeof pvpbuf] - replac);
90856823Seric 				*++arg_rvp = replac;
90956823Seric 			}
91056823Seric 			*++arg_rvp = NULL;
91116914Seric 
91216914Seric 			/* save the remainder of the input string */
91316914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
91416914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
91516914Seric 
91656678Seric 			/* look it up */
91756678Seric 			cataddr(key_rvp, buf, sizeof buf);
91856823Seric 			argvect[0] = buf;
91956678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
92056836Seric 			{
92156836Seric 				int bsize = sizeof buf - 1;
92256836Seric 
92356836Seric 				if (map->s_map.map_app != NULL)
92456836Seric 					bsize -= strlen(map->s_map.map_app);
92556836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
92656823Seric 						buf, sizeof buf - 1, argvect);
92756836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
92856836Seric 					strcat(replac, map->s_map.map_app);
92956836Seric 			}
93053654Seric 			else
93156678Seric 				replac = NULL;
93256678Seric 
93356678Seric 			/* if no replacement, use default */
93456823Seric 			if (replac == NULL && default_rvp != NULL)
93556823Seric 			{
93656823Seric 				char buf2[sizeof buf];
93756823Seric 
93856823Seric 				/* rewrite the default with % translations */
93956823Seric 				cataddr(default_rvp, buf2, sizeof buf2);
94056823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
94156823Seric 					argvect);
94256823Seric 				replac = buf;
94356823Seric 			}
94456823Seric 
94556678Seric 			if (replac == NULL)
94651317Seric 			{
94756823Seric 				xpvp = key_rvp;
94853654Seric 			}
94956678Seric 			else
95053654Seric 			{
95156678Seric 				/* scan the new replacement */
95256678Seric 				olddelimchar = DelimChar;
95356678Seric 				xpvp = prescan(replac, '\0', pvpbuf);
95456678Seric 				DelimChar = olddelimchar;
95553654Seric 				if (xpvp == NULL)
95651317Seric 				{
95756678Seric 					syserr("rewrite: cannot prescan map value: %s", replac);
95856678Seric 					return;
95956678Seric 				}
96051317Seric 			}
96151317Seric 
96216914Seric 			/* append it to the token list */
96356678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
96456678Seric 			{
96517174Seric 				*avp++ = newstr(*xpvp);
96616920Seric 				if (avp >= &npvp[MAXATOM])
96716914Seric 					goto toolong;
96817174Seric 			}
96916914Seric 
97016914Seric 			/* restore the old trailing information */
97156678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
97216920Seric 				if (avp >= &npvp[MAXATOM])
97316914Seric 					goto toolong;
97417174Seric 
97556678Seric 			break;
97616914Seric 		}
97716914Seric 
97816914Seric 		/*
97916914Seric 		**  Check for subroutine calls.
98016914Seric 		*/
98116914Seric 
98258050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
98356678Seric 		{
98456678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
98556678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
98656678Seric 			if (tTd(21, 3))
98756678Seric 				printf("-----callsubr %s\n", npvp[1]);
98856678Seric 			rewrite(pvp, atoi(npvp[1]));
98956678Seric 		}
99056678Seric 		else
99156678Seric 		{
99217348Seric 			bcopy((char *) npvp, (char *) pvp,
99316900Seric 				(int) (avp - npvp) * sizeof *avp);
99456678Seric 		}
9959374Seric 		if (tTd(21, 4))
9969374Seric 		{
9979374Seric 			printf("rewritten as:");
99856678Seric 			printav(pvp);
9999374Seric 		}
1000297Seric 	}
10018069Seric 
10029279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
10038069Seric 	{
10048959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
100556678Seric 		printav(pvp);
10068069Seric 	}
10073149Seric }
10083149Seric /*
10093149Seric **  BUILDADDR -- build address from token vector.
10103149Seric **
10113149Seric **	Parameters:
10123149Seric **		tv -- token vector.
10133149Seric **		a -- pointer to address descriptor to fill.
10143149Seric **			If NULL, one will be allocated.
10153149Seric **
10163149Seric **	Returns:
10174279Seric **		NULL if there was an error.
10184279Seric **		'a' otherwise.
10193149Seric **
10203149Seric **	Side Effects:
10213149Seric **		fills in 'a'
10223149Seric */
10233149Seric 
102457249Seric struct errcodes
102557249Seric {
102657249Seric 	char	*ec_name;		/* name of error code */
102757249Seric 	int	ec_code;		/* numeric code */
102857249Seric } ErrorCodes[] =
102957249Seric {
103057249Seric 	"usage",	EX_USAGE,
103157249Seric 	"nouser",	EX_NOUSER,
103257249Seric 	"nohost",	EX_NOHOST,
103357249Seric 	"unavailable",	EX_UNAVAILABLE,
103457249Seric 	"software",	EX_SOFTWARE,
103557249Seric 	"tempfail",	EX_TEMPFAIL,
103657249Seric 	"protocol",	EX_PROTOCOL,
103757249Seric #ifdef EX_CONFIG
103857249Seric 	"config",	EX_CONFIG,
103957249Seric #endif
104057249Seric 	NULL,		EX_UNAVAILABLE,
104157249Seric };
104257249Seric 
104356678Seric ADDRESS *
10443149Seric buildaddr(tv, a)
10453149Seric 	register char **tv;
10463149Seric 	register ADDRESS *a;
10473149Seric {
10483149Seric 	struct mailer **mp;
10493149Seric 	register struct mailer *m;
105058008Seric 	char *bp;
105158008Seric 	int spaceleft;
105257402Seric 	static char buf[MAXNAME];
10533149Seric 
10543149Seric 	if (a == NULL)
10553149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
105616889Seric 	bzero((char *) a, sizeof *a);
10573149Seric 
10583149Seric 	/* figure out what net/mailer to use */
105958050Seric 	if ((**tv & 0377) != CANONNET)
10604279Seric 	{
10613149Seric 		syserr("buildaddr: no net");
10624279Seric 		return (NULL);
10634279Seric 	}
10643149Seric 	tv++;
106533725Sbostic 	if (!strcasecmp(*tv, "error"))
10664279Seric 	{
106758050Seric 		if ((**++tv & 0377) == CANONHOST)
106810183Seric 		{
106957249Seric 			register struct errcodes *ep;
107057249Seric 
107158050Seric 			if (isascii(**++tv) && isdigit(**tv))
107257249Seric 			{
107357249Seric 				setstat(atoi(*tv));
107457249Seric 			}
107557249Seric 			else
107657249Seric 			{
107757249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
107857249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
107957249Seric 						break;
108057249Seric 				setstat(ep->ec_code);
108157249Seric 			}
108210183Seric 			tv++;
108310183Seric 		}
108458050Seric 		if ((**tv & 0377) != CANONUSER)
10854279Seric 			syserr("buildaddr: error: no user");
108658008Seric 		bp = buf;
108758008Seric 		spaceleft = sizeof buf - 2;
10884279Seric 		while (*++tv != NULL)
10894279Seric 		{
109058008Seric 			int i = strlen(*tv);
109158008Seric 
109258008Seric 			if (i > spaceleft)
109358008Seric 			{
109458008Seric 				/* out of space for this address */
109558008Seric 				if (spaceleft >= 0)
109658008Seric 					syserr("buildaddr: error message too long (%.40s...)",
109758008Seric 						buf);
109858008Seric 				i = spaceleft;
109958008Seric 				spaceleft = 0;
110058008Seric 			}
110158008Seric 			if (i <= 0)
110258008Seric 				continue;
110358008Seric 			if (bp != buf)
110458008Seric 			{
110558008Seric 				*bp++ = ' ';
110658008Seric 				spaceleft--;
110758008Seric 			}
110858008Seric 			bcopy(*tv, bp, i);
110958008Seric 			bp += i;
111058008Seric 			spaceleft -= i;
11114279Seric 		}
111258010Seric 		*bp = '\0';
11134279Seric 		usrerr(buf);
11144279Seric 		return (NULL);
11154279Seric 	}
111657402Seric 
11174598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
11183149Seric 	{
111933725Sbostic 		if (!strcasecmp(m->m_name, *tv))
11203149Seric 			break;
11213149Seric 	}
11223149Seric 	if (m == NULL)
11234279Seric 	{
112424944Seric 		syserr("buildaddr: unknown mailer %s", *tv);
11254279Seric 		return (NULL);
11264279Seric 	}
11274598Seric 	a->q_mailer = m;
11283149Seric 
11293149Seric 	/* figure out what host (if any) */
113056678Seric 	tv++;
113157249Seric 	if (!bitnset(M_LOCAL, m->m_flags))
11323149Seric 	{
113358050Seric 		if ((**tv & 0377) != CANONHOST)
11344279Seric 		{
11353149Seric 			syserr("buildaddr: no host");
11364279Seric 			return (NULL);
11374279Seric 		}
113858008Seric 		bp = buf;
113958008Seric 		spaceleft = sizeof buf - 1;
114058050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
114158008Seric 		{
114258008Seric 			int i = strlen(*tv);
114358008Seric 
114458008Seric 			if (i > spaceleft)
114558008Seric 			{
114658008Seric 				/* out of space for this address */
114758008Seric 				if (spaceleft >= 0)
114858008Seric 					syserr("buildaddr: host too long (%.40s...)",
114958008Seric 						buf);
115058008Seric 				i = spaceleft;
115158008Seric 				spaceleft = 0;
115258008Seric 			}
115358008Seric 			if (i <= 0)
115458008Seric 				continue;
115558008Seric 			bcopy(*tv, bp, i);
115658008Seric 			bp += i;
115758008Seric 			spaceleft -= i;
115858008Seric 		}
115958010Seric 		*bp = '\0';
11605704Seric 		a->q_host = newstr(buf);
11613149Seric 	}
116257249Seric 	else
116357249Seric 		a->q_host = NULL;
11643149Seric 
11653149Seric 	/* figure out the user */
116658050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
11674279Seric 	{
11683149Seric 		syserr("buildaddr: no user");
11694279Seric 		return (NULL);
11704279Seric 	}
117157402Seric 	tv++;
117251317Seric 
117357402Seric 	/* do special mapping for local mailer */
117457402Seric 	if (m == LocalMailer && *tv != NULL)
117557402Seric 	{
117657454Seric 		register char *p = *tv;
117757454Seric 
117857454Seric 		if (*p == '"')
117957454Seric 			p++;
118057454Seric 		if (*p == '|')
118157402Seric 			a->q_mailer = m = ProgMailer;
118257454Seric 		else if (*p == '/')
118357402Seric 			a->q_mailer = m = FileMailer;
118457454Seric 		else if (*p == ':')
118557402Seric 		{
118657402Seric 			/* may be :include: */
118757402Seric 			cataddr(tv, buf, sizeof buf);
118858008Seric 			stripquotes(buf);
118958008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
119058008Seric 			{
119158008Seric 				/* if :include:, don't need further rewriting */
119257402Seric 				a->q_mailer = m = InclMailer;
119358008Seric 				a->q_user = &buf[9];
119458008Seric 				return (a);
119558008Seric 			}
119657402Seric 		}
119757402Seric 	}
119857402Seric 
119958008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
120058008Seric 	{
120158008Seric 		tv++;
120258008Seric 		a->q_flags |= QNOTREMOTE;
120358008Seric 	}
120458008Seric 
120519040Seric 	/* rewrite according recipient mailer rewriting rules */
120657402Seric 	rewrite(tv, 2);
120758020Seric 	if (m->m_re_rwset > 0)
120858020Seric 		rewrite(tv, m->m_re_rwset);
120919040Seric 	rewrite(tv, 4);
121019040Seric 
121119040Seric 	/* save the result for the command line/RCPT argument */
121211278Seric 	cataddr(tv, buf, sizeof buf);
12133149Seric 	a->q_user = buf;
12143149Seric 
12153149Seric 	return (a);
12163149Seric }
12173188Seric /*
12184228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
12194228Seric **
12204228Seric **	Parameters:
12214228Seric **		pvp -- parameter vector to rebuild.
12224228Seric **		buf -- buffer to build the string into.
12234228Seric **		sz -- size of buf.
12244228Seric **
12254228Seric **	Returns:
12264228Seric **		none.
12274228Seric **
12284228Seric **	Side Effects:
12294228Seric **		Destroys buf.
12304228Seric */
12314228Seric 
12324228Seric cataddr(pvp, buf, sz)
12334228Seric 	char **pvp;
12344228Seric 	char *buf;
12354228Seric 	register int sz;
12364228Seric {
12374228Seric 	bool oatomtok = FALSE;
123856678Seric 	bool natomtok = FALSE;
12394228Seric 	register int i;
12404228Seric 	register char *p;
12414228Seric 
12428423Seric 	if (pvp == NULL)
12438423Seric 	{
124423109Seric 		(void) strcpy(buf, "");
12458423Seric 		return;
12468423Seric 	}
12474228Seric 	p = buf;
124811156Seric 	sz -= 2;
12494228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
12504228Seric 	{
12518078Seric 		natomtok = (toktype(**pvp) == ATM);
12524228Seric 		if (oatomtok && natomtok)
12539042Seric 			*p++ = SpaceSub;
12544228Seric 		(void) strcpy(p, *pvp);
12554228Seric 		oatomtok = natomtok;
12564228Seric 		p += i;
125711156Seric 		sz -= i + 1;
12584228Seric 		pvp++;
12594228Seric 	}
12604228Seric 	*p = '\0';
12614228Seric }
12624228Seric /*
12633188Seric **  SAMEADDR -- Determine if two addresses are the same
12643188Seric **
12653188Seric **	This is not just a straight comparison -- if the mailer doesn't
12663188Seric **	care about the host we just ignore it, etc.
12673188Seric **
12683188Seric **	Parameters:
12693188Seric **		a, b -- pointers to the internal forms to compare.
12703188Seric **
12713188Seric **	Returns:
12723188Seric **		TRUE -- they represent the same mailbox.
12733188Seric **		FALSE -- they don't.
12743188Seric **
12753188Seric **	Side Effects:
12763188Seric **		none.
12773188Seric */
12783188Seric 
12793188Seric bool
12809374Seric sameaddr(a, b)
12813188Seric 	register ADDRESS *a;
12823188Seric 	register ADDRESS *b;
12833188Seric {
12843188Seric 	/* if they don't have the same mailer, forget it */
12853188Seric 	if (a->q_mailer != b->q_mailer)
12863188Seric 		return (FALSE);
12873188Seric 
12883188Seric 	/* if the user isn't the same, we can drop out */
128956678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
12903188Seric 		return (FALSE);
12913188Seric 
12923188Seric 	/* if the mailer ignores hosts, we have succeeded! */
129310690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
12943188Seric 		return (TRUE);
12953188Seric 
12963188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
12973188Seric 	if (a->q_host == NULL || b->q_host == NULL)
12983188Seric 		return (FALSE);
129956678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
13003188Seric 		return (FALSE);
13013188Seric 
13023188Seric 	return (TRUE);
13033188Seric }
13043234Seric /*
13053234Seric **  PRINTADDR -- print address (for debugging)
13063234Seric **
13073234Seric **	Parameters:
13083234Seric **		a -- the address to print
13093234Seric **		follow -- follow the q_next chain.
13103234Seric **
13113234Seric **	Returns:
13123234Seric **		none.
13133234Seric **
13143234Seric **	Side Effects:
13153234Seric **		none.
13163234Seric */
13173234Seric 
13183234Seric printaddr(a, follow)
13193234Seric 	register ADDRESS *a;
13203234Seric 	bool follow;
13213234Seric {
13225001Seric 	bool first = TRUE;
132357731Seric 	register MAILER *m;
132457731Seric 	MAILER pseudomailer;
13255001Seric 
13263234Seric 	while (a != NULL)
13273234Seric 	{
13285001Seric 		first = FALSE;
13294443Seric 		printf("%x=", a);
13304085Seric 		(void) fflush(stdout);
133157731Seric 
133257731Seric 		/* find the mailer -- carefully */
133357731Seric 		m = a->q_mailer;
133457731Seric 		if (m == NULL)
133557731Seric 		{
133657731Seric 			m = &pseudomailer;
133757731Seric 			m->m_mno = -1;
133857731Seric 			m->m_name = "NULL";
133957731Seric 		}
134057731Seric 
134140973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
134257731Seric 		       a->q_paddr, m->m_mno, m->m_name,
134340973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
13448181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
13458181Seric 		       a->q_alias);
13468181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
13478181Seric 		       a->q_fullname);
13484996Seric 
13493234Seric 		if (!follow)
13503234Seric 			return;
13514996Seric 		a = a->q_next;
13523234Seric 	}
13535001Seric 	if (first)
13544443Seric 		printf("[NULL]\n");
13553234Seric }
13564317Seric 
13577682Seric /*
13587682Seric **  REMOTENAME -- return the name relative to the current mailer
13597682Seric **
13607682Seric **	Parameters:
13617682Seric **		name -- the name to translate.
13628069Seric **		m -- the mailer that we want to do rewriting relative
13638069Seric **			to.
13648069Seric **		senderaddress -- if set, uses the sender rewriting rules
13658069Seric **			rather than the recipient rewriting rules.
136658020Seric **		header -- set if this address is in the header, rather
136758020Seric **			than an envelope header.
136810310Seric **		canonical -- if set, strip out any comment information,
136910310Seric **			etc.
137058020Seric **		e -- the current envelope.
13717682Seric **
13727682Seric **	Returns:
13737682Seric **		the text string representing this address relative to
13747682Seric **			the receiving mailer.
13757682Seric **
13767682Seric **	Side Effects:
13777682Seric **		none.
13787682Seric **
13797682Seric **	Warnings:
13807682Seric **		The text string returned is tucked away locally;
13817682Seric **			copy it if you intend to save it.
13827682Seric */
13837682Seric 
13847682Seric char *
138558020Seric remotename(name, m, senderaddress, header, canonical, e)
13867682Seric 	char *name;
138756678Seric 	struct mailer *m;
13888069Seric 	bool senderaddress;
138958020Seric 	bool header;
139010310Seric 	bool canonical;
139156678Seric 	register ENVELOPE *e;
13927682Seric {
13938069Seric 	register char **pvp;
13948069Seric 	char *fancy;
139556678Seric 	extern char *macvalue();
139656678Seric 	char *oldg = macvalue('g', e);
139758020Seric 	int rwset;
13987682Seric 	static char buf[MAXNAME];
13997682Seric 	char lbuf[MAXNAME];
140016914Seric 	char pvpbuf[PSBUFSIZE];
140156678Seric 	extern char **prescan();
140256678Seric 	extern char *crackaddr();
14037682Seric 
14047755Seric 	if (tTd(12, 1))
14057755Seric 		printf("remotename(%s)\n", name);
14067755Seric 
140710177Seric 	/* don't do anything if we are tagging it as special */
140858020Seric 	if (senderaddress)
140958020Seric 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
141058020Seric 	else
141158020Seric 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
141258020Seric 	if (rwset < 0)
141310177Seric 		return (name);
141410177Seric 
14157682Seric 	/*
14168181Seric 	**  Do a heuristic crack of this name to extract any comment info.
14178181Seric 	**	This will leave the name as a comment and a $g macro.
14187889Seric 	*/
14197889Seric 
142058036Seric 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
142158050Seric 		fancy = "\201g";
142210310Seric 	else
142310310Seric 		fancy = crackaddr(name);
14247889Seric 
14258181Seric 	/*
14268181Seric 	**  Turn the name into canonical form.
14278181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
14288181Seric 	**	If this only resolves to "user", and the "C" flag is
14298181Seric 	**	specified in the sending mailer, then the sender's
14308181Seric 	**	domain will be appended.
14318181Seric 	*/
14328181Seric 
143316914Seric 	pvp = prescan(name, '\0', pvpbuf);
14347889Seric 	if (pvp == NULL)
14357889Seric 		return (name);
14368181Seric 	rewrite(pvp, 3);
143756678Seric 	if (e->e_fromdomain != NULL)
14388181Seric 	{
14398181Seric 		/* append from domain to this address */
14408181Seric 		register char **pxp = pvp;
14418181Seric 
14429594Seric 		/* see if there is an "@domain" in the current name */
14438181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
14448181Seric 			pxp++;
14458181Seric 		if (*pxp == NULL)
14468181Seric 		{
14479594Seric 			/* no.... append the "@domain" from the sender */
144856678Seric 			register char **qxq = e->e_fromdomain;
14498181Seric 
14509594Seric 			while ((*pxp++ = *qxq++) != NULL)
14519594Seric 				continue;
145211726Seric 			rewrite(pvp, 3);
14538181Seric 		}
14548181Seric 	}
14558181Seric 
14568181Seric 	/*
14578959Seric 	**  Do more specific rewriting.
145856678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
145956678Seric 	**		a sender address or not.
14608181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
14618181Seric 	*/
14628181Seric 
14638069Seric 	if (senderaddress)
146456327Seric 		rewrite(pvp, 1);
14658069Seric 	else
146656327Seric 		rewrite(pvp, 2);
146758020Seric 	if (rwset > 0)
146858020Seric 		rewrite(pvp, rwset);
14697682Seric 
14708181Seric 	/*
14718959Seric 	**  Do any final sanitation the address may require.
14728959Seric 	**	This will normally be used to turn internal forms
14738959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
14748959Seric 	**	may be used as a default to the above rules.
14758959Seric 	*/
14768959Seric 
14778959Seric 	rewrite(pvp, 4);
14788959Seric 
14798959Seric 	/*
14808181Seric 	**  Now restore the comment information we had at the beginning.
14818181Seric 	*/
14828181Seric 
14837682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
148456678Seric 	define('g', lbuf, e);
148556678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
148656678Seric 	define('g', oldg, e);
14877682Seric 
14887682Seric 	if (tTd(12, 1))
14897755Seric 		printf("remotename => `%s'\n", buf);
14907682Seric 	return (buf);
14917682Seric }
149251317Seric /*
149356678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
149451317Seric **
149551317Seric **	Parameters:
149656678Seric **		a -- the address to map (but just the user name part).
149756678Seric **		sendq -- the sendq in which to install any replacement
149856678Seric **			addresses.
149951317Seric **
150051317Seric **	Returns:
150151317Seric **		none.
150251317Seric */
150351317Seric 
150456678Seric maplocaluser(a, sendq, e)
150556678Seric 	register ADDRESS *a;
150656678Seric 	ADDRESS **sendq;
150756678Seric 	ENVELOPE *e;
150851317Seric {
150956678Seric 	register char **pvp;
151056678Seric 	register ADDRESS *a1 = NULL;
151156678Seric 	char pvpbuf[PSBUFSIZE];
151251317Seric 
151356678Seric 	if (tTd(29, 1))
151456678Seric 	{
151556678Seric 		printf("maplocaluser: ");
151656678Seric 		printaddr(a, FALSE);
151756678Seric 	}
151856678Seric 	pvp = prescan(a->q_user, '\0', pvpbuf);
151956678Seric 	if (pvp == NULL)
152056678Seric 		return;
152151317Seric 
152256678Seric 	rewrite(pvp, 5);
152358050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
152456678Seric 		return;
152551317Seric 
152656678Seric 	/* if non-null, mailer destination specified -- has it changed? */
152756678Seric 	a1 = buildaddr(pvp, NULL);
152856678Seric 	if (a1 == NULL || sameaddr(a, a1))
152956678Seric 		return;
153051317Seric 
153156678Seric 	/* mark old address as dead; insert new address */
153256678Seric 	a->q_flags |= QDONTSEND;
153357731Seric 	if (tTd(29, 5))
153457731Seric 	{
153557731Seric 		printf("maplocaluser: QDONTSEND ");
153657731Seric 		printaddr(a, FALSE);
153757731Seric 	}
153856678Seric 	a1->q_alias = a;
153956678Seric 	allocaddr(a1, 1, NULL);
154056678Seric 	(void) recipient(a1, sendq, e);
154151317Seric }
1542