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*58670Seric static char sccsid[] = "@(#)parseaddr.c	6.26 (Berkeley) 03/14/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.
4358333Seric **		delimptr -- if non-NULL, set to the location of the
4458333Seric **			delim character that was found.
4556678Seric **		e -- the envelope that will contain this address.
46297Seric **
47297Seric **	Returns:
48297Seric **		A pointer to the address descriptor header (`a' if
49297Seric **			`a' is non-NULL).
50297Seric **		NULL on error.
51297Seric **
52297Seric **	Side Effects:
53297Seric **		none
54297Seric */
55297Seric 
569374Seric /* following delimiters are inherent to the internal algorithms */
5758050Seric # define DELIMCHARS	"\201()<>,;\\\"\r\n"	/* word delimiters */
582091Seric 
592973Seric ADDRESS *
6058333Seric parseaddr(addr, a, copyf, delim, delimptr, e)
61297Seric 	char *addr;
622973Seric 	register ADDRESS *a;
63297Seric 	int copyf;
6411445Seric 	char delim;
6558333Seric 	char **delimptr;
6656678Seric 	register ENVELOPE *e;
67297Seric {
683149Seric 	register char **pvp;
6958333Seric 	auto char *delimptrbuf;
7016914Seric 	char pvpbuf[PSBUFSIZE];
7156678Seric 	extern char **prescan();
7256678Seric 	extern ADDRESS *buildaddr();
7357388Seric 	extern bool invalidaddr();
74297Seric 
75297Seric 	/*
76297Seric 	**  Initialize and prescan address.
77297Seric 	*/
78297Seric 
7956678Seric 	e->e_to = addr;
807675Seric 	if (tTd(20, 1))
819888Seric 		printf("\n--parseaddr(%s)\n", addr);
823188Seric 
8357388Seric 	if (invalidaddr(addr))
8457388Seric 	{
8557388Seric 		if (tTd(20, 1))
8657388Seric 			printf("parseaddr-->bad address\n");
8757388Seric 		return NULL;
8857388Seric 	}
8957388Seric 
9058333Seric 	if (delimptr == NULL)
9158333Seric 		delimptr = &delimptrbuf;
9258333Seric 
9358333Seric 	pvp = prescan(addr, delim, pvpbuf, delimptr);
943149Seric 	if (pvp == NULL)
9556729Seric 	{
9656729Seric 		if (tTd(20, 1))
9756729Seric 			printf("parseaddr-->NULL\n");
98297Seric 		return (NULL);
9956729Seric 	}
100297Seric 
101297Seric 	/*
1023149Seric 	**  Apply rewriting rules.
1037889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
104297Seric 	*/
105297Seric 
1068181Seric 	rewrite(pvp, 3);
1074070Seric 	rewrite(pvp, 0);
108297Seric 
1093149Seric 	/*
1103149Seric 	**  See if we resolved to a real mailer.
1113149Seric 	*/
112297Seric 
11358050Seric 	if ((pvp[0][0] & 0377) != CANONNET)
1143149Seric 	{
1153149Seric 		setstat(EX_USAGE);
11658151Seric 		syserr("554 cannot resolve name");
1173149Seric 		return (NULL);
118297Seric 	}
119297Seric 
120297Seric 	/*
1213149Seric 	**  Build canonical address from pvp.
122297Seric 	*/
123297Seric 
1243149Seric 	a = buildaddr(pvp, a);
1254279Seric 	if (a == NULL)
1264279Seric 		return (NULL);
127297Seric 
128297Seric 	/*
1293149Seric 	**  Make local copies of the host & user and then
1303149Seric 	**  transport them out.
131297Seric 	*/
132297Seric 
13358333Seric 	allocaddr(a, copyf, addr, *delimptr);
13456678Seric 
13556678Seric 	/*
13656678Seric 	**  Compute return value.
13756678Seric 	*/
13856678Seric 
13956678Seric 	if (tTd(20, 1))
1408078Seric 	{
14156678Seric 		printf("parseaddr-->");
14256678Seric 		printaddr(a, FALSE);
14356678Seric 	}
14456678Seric 
14556678Seric 	return (a);
14656678Seric }
14756678Seric /*
14857388Seric **  INVALIDADDR -- check for address containing meta-characters
14957388Seric **
15057388Seric **	Parameters:
15157388Seric **		addr -- the address to check.
15257388Seric **
15357388Seric **	Returns:
15457388Seric **		TRUE -- if the address has any "wierd" characters
15557388Seric **		FALSE -- otherwise.
15657388Seric */
15757388Seric 
15857388Seric bool
15957388Seric invalidaddr(addr)
16057388Seric 	register char *addr;
16157388Seric {
16257388Seric 	for (; *addr != '\0'; addr++)
16357388Seric 	{
16458050Seric 		if ((*addr & 0340) != 0200)
16557388Seric 			continue;
16657388Seric 		setstat(EX_USAGE);
16758151Seric 		usrerr("553 Address contained invalid control characters");
16857388Seric 		return TRUE;
16957388Seric 	}
17057388Seric 	return FALSE;
17157388Seric }
17257388Seric /*
17356678Seric **  ALLOCADDR -- do local allocations of address on demand.
17456678Seric **
17556678Seric **	Also lowercases the host name if requested.
17656678Seric **
17756678Seric **	Parameters:
17856678Seric **		a -- the address to reallocate.
17956678Seric **		copyf -- the copy flag (see parseaddr for description).
18056678Seric **		paddr -- the printname of the address.
18158333Seric **		delimptr -- a pointer to the address delimiter.  Must be set.
18256678Seric **
18356678Seric **	Returns:
18456678Seric **		none.
18556678Seric **
18656678Seric **	Side Effects:
18756678Seric **		Copies portions of a into local buffers as requested.
18856678Seric */
18956678Seric 
19058333Seric allocaddr(a, copyf, paddr, delimptr)
19156678Seric 	register ADDRESS *a;
19256678Seric 	int copyf;
19356678Seric 	char *paddr;
19458333Seric 	char *delimptr;
19556678Seric {
19656678Seric 	register MAILER *m = a->q_mailer;
19756678Seric 
19856678Seric 	if (copyf > 0 && paddr != NULL)
19956678Seric 	{
20058333Seric 		char savec = *delimptr;
2018078Seric 
20258333Seric 		*delimptr = '\0';
20356678Seric 		a->q_paddr = newstr(paddr);
20458333Seric 		*delimptr = savec;
2058078Seric 	}
206297Seric 	else
20756678Seric 		a->q_paddr = paddr;
20824944Seric 
20924944Seric 	if (a->q_user == NULL)
21024944Seric 		a->q_user = "";
21124944Seric 	if (a->q_host == NULL)
21224944Seric 		a->q_host = "";
21324944Seric 
2143149Seric 	if (copyf >= 0)
215297Seric 	{
21624944Seric 		a->q_host = newstr(a->q_host);
2173149Seric 		if (a->q_user != a->q_paddr)
2183149Seric 			a->q_user = newstr(a->q_user);
219297Seric 	}
220297Seric 
22156678Seric 	if (a->q_paddr == NULL)
22256678Seric 		a->q_paddr = a->q_user;
223297Seric }
224297Seric /*
225297Seric **  PRESCAN -- Prescan name and make it canonical
226297Seric **
2279374Seric **	Scans a name and turns it into a set of tokens.  This process
2289374Seric **	deletes blanks and comments (in parentheses).
229297Seric **
230297Seric **	This routine knows about quoted strings and angle brackets.
231297Seric **
232297Seric **	There are certain subtleties to this routine.  The one that
233297Seric **	comes to mind now is that backslashes on the ends of names
234297Seric **	are silently stripped off; this is intentional.  The problem
235297Seric **	is that some versions of sndmsg (like at LBL) set the kill
236297Seric **	character to something other than @ when reading addresses;
237297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
238297Seric **	berknet mailer.
239297Seric **
240297Seric **	Parameters:
241297Seric **		addr -- the name to chomp.
242297Seric **		delim -- the delimiter for the address, normally
243297Seric **			'\0' or ','; \0 is accepted in any case.
24415284Seric **			If '\t' then we are reading the .cf file.
24516914Seric **		pvpbuf -- place to put the saved text -- note that
24616914Seric **			the pointers are static.
24758333Seric **		delimptr -- if non-NULL, set to the location of the
24858333Seric **			terminating delimiter.
249297Seric **
250297Seric **	Returns:
2513149Seric **		A pointer to a vector of tokens.
252297Seric **		NULL on error.
253297Seric */
254297Seric 
2558078Seric /* states and character types */
2568078Seric # define OPR		0	/* operator */
2578078Seric # define ATM		1	/* atom */
2588078Seric # define QST		2	/* in quoted string */
2598078Seric # define SPC		3	/* chewing up spaces */
2608078Seric # define ONE		4	/* pick up one character */
2613149Seric 
2628078Seric # define NSTATES	5	/* number of states */
2638078Seric # define TYPE		017	/* mask to select state type */
2648078Seric 
2658078Seric /* meta bits for table */
2668078Seric # define M		020	/* meta character; don't pass through */
2678078Seric # define B		040	/* cause a break */
2688078Seric # define MB		M|B	/* meta-break */
2698078Seric 
2708078Seric static short StateTab[NSTATES][NSTATES] =
2718078Seric {
2728087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2739051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2749051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2759051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2768078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2778078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2788078Seric };
2798078Seric 
2808078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2818078Seric 
2823149Seric char **
28358333Seric prescan(addr, delim, pvpbuf, delimptr)
284297Seric 	char *addr;
285297Seric 	char delim;
28616914Seric 	char pvpbuf[];
28758333Seric 	char **delimptr;
288297Seric {
289297Seric 	register char *p;
2908078Seric 	register char *q;
2919346Seric 	register int c;
2923149Seric 	char **avp;
293297Seric 	bool bslashmode;
294297Seric 	int cmntcnt;
2958423Seric 	int anglecnt;
2963149Seric 	char *tok;
2978078Seric 	int state;
2988078Seric 	int newstate;
2998078Seric 	static char *av[MAXATOM+1];
30056678Seric 	extern int errno;
301297Seric 
30215253Seric 	/* make sure error messages don't have garbage on them */
30315253Seric 	errno = 0;
30415253Seric 
30516914Seric 	q = pvpbuf;
3063149Seric 	bslashmode = FALSE;
3077800Seric 	cmntcnt = 0;
3088423Seric 	anglecnt = 0;
3093149Seric 	avp = av;
31056678Seric 	state = ATM;
3118078Seric 	c = NOCHAR;
3128078Seric 	p = addr;
31356764Seric 	if (tTd(22, 11))
314297Seric 	{
3158078Seric 		printf("prescan: ");
3168078Seric 		xputs(p);
31723109Seric 		(void) putchar('\n');
3188078Seric 	}
3198078Seric 
3208078Seric 	do
3218078Seric 	{
3223149Seric 		/* read a token */
3233149Seric 		tok = q;
3248078Seric 		for (;;)
325297Seric 		{
3268078Seric 			/* store away any old lookahead character */
3278078Seric 			if (c != NOCHAR)
3288078Seric 			{
32915284Seric 				/* see if there is room */
33016914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3318078Seric 				{
33258151Seric 					usrerr("553 Address too long");
33358333Seric 					if (delimptr != NULL)
33458333Seric 						*delimptr = p;
3358078Seric 					return (NULL);
3368078Seric 				}
33715284Seric 
33815284Seric 				/* squirrel it away */
3398078Seric 				*q++ = c;
3408078Seric 			}
3418078Seric 
3428078Seric 			/* read a new input character */
3438078Seric 			c = *p++;
34457631Seric 			if (c == '\0')
34556764Seric 			{
34656764Seric 				/* diagnose and patch up bad syntax */
34756764Seric 				if (state == QST)
34856764Seric 				{
34958151Seric 					usrerr("553 Unbalanced '\"'");
35056764Seric 					c = '"';
35156764Seric 				}
35256764Seric 				else if (cmntcnt > 0)
35356764Seric 				{
35458151Seric 					usrerr("553 Unbalanced '('");
35556764Seric 					c = ')';
35656764Seric 				}
35756764Seric 				else if (anglecnt > 0)
35856764Seric 				{
35956764Seric 					c = '>';
36058151Seric 					usrerr("553 Unbalanced '<'");
36156764Seric 				}
36256764Seric 				else
36356764Seric 					break;
36415284Seric 
36556764Seric 				p--;
36656764Seric 			}
36757631Seric 			else if (c == delim && anglecnt <= 0 &&
36857631Seric 					cmntcnt <= 0 && state != QST)
36957631Seric 				break;
37056764Seric 
3718078Seric 			if (tTd(22, 101))
3728078Seric 				printf("c=%c, s=%d; ", c, state);
3738078Seric 
3743149Seric 			/* chew up special characters */
3753149Seric 			*q = '\0';
3763149Seric 			if (bslashmode)
3773149Seric 			{
37824944Seric 				/* kludge \! for naive users */
37958061Seric 				if (cmntcnt > 0)
38058061Seric 					c = NOCHAR;
38158061Seric 				else if (c != '!')
38256678Seric 					*q++ = '\\';
3833149Seric 				bslashmode = FALSE;
38456678Seric 				continue;
3853149Seric 			}
38656678Seric 
38756678Seric 			if (c == '\\')
3883149Seric 			{
3893149Seric 				bslashmode = TRUE;
3908078Seric 				c = NOCHAR;
39156678Seric 				continue;
3923149Seric 			}
39356678Seric 			else if (state == QST)
3948514Seric 			{
3958514Seric 				/* do nothing, just avoid next clauses */
3968514Seric 			}
3978078Seric 			else if (c == '(')
3984100Seric 			{
3998078Seric 				cmntcnt++;
4008078Seric 				c = NOCHAR;
4014100Seric 			}
4028078Seric 			else if (c == ')')
4033149Seric 			{
4048078Seric 				if (cmntcnt <= 0)
4053149Seric 				{
40658151Seric 					usrerr("553 Unbalanced ')'");
40758333Seric 					if (delimptr != NULL)
40858333Seric 						*delimptr = p;
4098078Seric 					return (NULL);
4103149Seric 				}
4118078Seric 				else
4128078Seric 					cmntcnt--;
4138078Seric 			}
4148078Seric 			else if (cmntcnt > 0)
4158078Seric 				c = NOCHAR;
4168423Seric 			else if (c == '<')
4178423Seric 				anglecnt++;
4188423Seric 			else if (c == '>')
4198423Seric 			{
4208423Seric 				if (anglecnt <= 0)
4218423Seric 				{
42258151Seric 					usrerr("553 Unbalanced '>'");
42358333Seric 					if (delimptr != NULL)
42458333Seric 						*delimptr = p;
4258423Seric 					return (NULL);
4268423Seric 				}
4278423Seric 				anglecnt--;
4288423Seric 			}
42958050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
43011423Seric 				c = ' ';
4313149Seric 
4328078Seric 			if (c == NOCHAR)
4338078Seric 				continue;
4343149Seric 
4358078Seric 			/* see if this is end of input */
43611405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4373149Seric 				break;
4383149Seric 
4398078Seric 			newstate = StateTab[state][toktype(c)];
4408078Seric 			if (tTd(22, 101))
4418078Seric 				printf("ns=%02o\n", newstate);
4428078Seric 			state = newstate & TYPE;
4438078Seric 			if (bitset(M, newstate))
4448078Seric 				c = NOCHAR;
4458078Seric 			if (bitset(B, newstate))
4464228Seric 				break;
447297Seric 		}
4483149Seric 
4493149Seric 		/* new token */
4508078Seric 		if (tok != q)
4511378Seric 		{
4528078Seric 			*q++ = '\0';
4538078Seric 			if (tTd(22, 36))
454297Seric 			{
4558078Seric 				printf("tok=");
4568078Seric 				xputs(tok);
45723109Seric 				(void) putchar('\n');
458297Seric 			}
4598078Seric 			if (avp >= &av[MAXATOM])
460297Seric 			{
46158151Seric 				syserr("553 prescan: too many tokens");
46258333Seric 				if (delimptr != NULL)
46358333Seric 					*delimptr = p;
4648078Seric 				return (NULL);
465297Seric 			}
4668078Seric 			*avp++ = tok;
467297Seric 		}
4688423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4693149Seric 	*avp = NULL;
47058333Seric 	p--;
47158333Seric 	if (delimptr != NULL)
47258333Seric 		*delimptr = p;
47356764Seric 	if (tTd(22, 12))
47456764Seric 	{
47556764Seric 		printf("prescan==>");
47656764Seric 		printav(av);
47756764Seric 	}
47858546Seric 	if (av[0] == NULL)
47958546Seric 		return (NULL);
48058403Seric 	return (av);
4813149Seric }
4823149Seric /*
4833149Seric **  TOKTYPE -- return token type
4843149Seric **
4853149Seric **	Parameters:
4863149Seric **		c -- the character in question.
4873149Seric **
4883149Seric **	Returns:
4893149Seric **		Its type.
4903149Seric **
4913149Seric **	Side Effects:
4923149Seric **		none.
4933149Seric */
494297Seric 
4953149Seric toktype(c)
49658050Seric 	register int c;
4973149Seric {
4983380Seric 	static char buf[50];
4993382Seric 	static bool firstime = TRUE;
5003380Seric 
5013382Seric 	if (firstime)
5023380Seric 	{
5033382Seric 		firstime = FALSE;
50458050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5057005Seric 		(void) strcat(buf, DELIMCHARS);
5063380Seric 	}
50758050Seric 	c &= 0377;
50856327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5098078Seric 		return (ONE);
5108078Seric 	if (c == '"')
5118078Seric 		return (QST);
51258050Seric 	if ((c & 0340) == 0200)
51358050Seric 		return (OPR);
5144100Seric 	if (!isascii(c))
5158078Seric 		return (ATM);
5168078Seric 	if (isspace(c) || c == ')')
5178078Seric 		return (SPC);
51858050Seric 	if (strchr(buf, c) != NULL)
5198078Seric 		return (OPR);
5208078Seric 	return (ATM);
5213149Seric }
5223149Seric /*
5233149Seric **  REWRITE -- apply rewrite rules to token vector.
5243149Seric **
5254476Seric **	This routine is an ordered production system.  Each rewrite
5264476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5274476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5284476Seric **
5294476Seric **	For each rewrite rule, 'avp' points the address vector we
5304476Seric **	are trying to match against, and 'pvp' points to the pattern.
5318058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5329585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5339585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5344476Seric **
5354476Seric **	When a match between avp & pvp does not match, we try to
5369585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5374476Seric **	we must also back out the match in mvp.  If we reach a
5388058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5398058Seric **	over again.
5404476Seric **
5414476Seric **	When we finally match, we rewrite the address vector
5424476Seric **	and try over again.
5434476Seric **
5443149Seric **	Parameters:
5453149Seric **		pvp -- pointer to token vector.
5463149Seric **
5473149Seric **	Returns:
5483149Seric **		none.
5493149Seric **
5503149Seric **	Side Effects:
5513149Seric **		pvp is modified.
5523149Seric */
5532091Seric 
5543149Seric struct match
5553149Seric {
5564468Seric 	char	**first;	/* first token matched */
5574468Seric 	char	**last;		/* last token matched */
5583149Seric };
5593149Seric 
5604468Seric # define MAXMATCH	9	/* max params per rewrite */
5613149Seric 
5623149Seric 
5634070Seric rewrite(pvp, ruleset)
5643149Seric 	char **pvp;
5654070Seric 	int ruleset;
5663149Seric {
5673149Seric 	register char *ap;		/* address pointer */
5683149Seric 	register char *rp;		/* rewrite pointer */
5693149Seric 	register char **avp;		/* address vector pointer */
5703149Seric 	register char **rvp;		/* rewrite vector pointer */
5718058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5728058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
57356678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5743149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5753149Seric 
5769279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5773149Seric 	{
5788959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
57956678Seric 		printav(pvp);
5803149Seric 	}
58156678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
58256326Seric 	{
58358151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
58456326Seric 		return;
58556326Seric 	}
58656678Seric 	if (pvp == NULL)
58756678Seric 		return;
58856326Seric 
5893149Seric 	/*
59056678Seric 	**  Run through the list of rewrite rules, applying
59156678Seric 	**	any that match.
5923149Seric 	*/
5933149Seric 
5944070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
5953149Seric 	{
59656678Seric 		int loopcount = 0;
59756678Seric 
5987675Seric 		if (tTd(21, 12))
599297Seric 		{
6008069Seric 			printf("-----trying rule:");
60156678Seric 			printav(rwr->r_lhs);
6023149Seric 		}
6033149Seric 
6043149Seric 		/* try to match on this rule */
6054468Seric 		mlp = mlist;
6068058Seric 		rvp = rwr->r_lhs;
6078058Seric 		avp = pvp;
6088058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
6093149Seric 		{
61056678Seric 			if (++loopcount > 100)
61152637Seric 			{
61258151Seric 				syserr("554 Infinite loop in ruleset %d", ruleset);
61356678Seric 				printf("workspace: ");
61456678Seric 				printav(pvp);
61552637Seric 				break;
61652637Seric 			}
6173149Seric 			rp = *rvp;
6188058Seric 			if (tTd(21, 35))
6198058Seric 			{
62057532Seric 				printf("rp=");
62157531Seric 				xputs(rp);
62257532Seric 				printf(", ap=");
6238058Seric 				xputs(ap);
6248069Seric 				printf("\n");
6258058Seric 			}
62656678Seric 			if (rp == NULL)
62756326Seric 			{
6283149Seric 				/* end-of-pattern before end-of-address */
6298058Seric 				goto backup;
63056678Seric 			}
63158173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
63258173Seric 			    (*rp & 0377) != CANONHOST)
63356326Seric 			{
63456678Seric 				/* end-of-input */
63556678Seric 				break;
636297Seric 			}
63756326Seric 
63858050Seric 			switch (*rp & 0377)
6398058Seric 			{
64056678Seric 				register STAB *s;
64156326Seric 
64256678Seric 			  case MATCHCLASS:
64356678Seric 			  case MATCHNCLASS:
64456678Seric 				/* match any token in (not in) a class */
64556678Seric 				s = stab(ap, ST_CLASS, ST_FIND);
64656678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
64756326Seric 				{
64858050Seric 					if ((*rp & 0377) == MATCHCLASS)
6499585Seric 						goto backup;
65056326Seric 				}
65158050Seric 				else if ((*rp & 0377) == MATCHNCLASS)
65256678Seric 					goto backup;
6534060Seric 
65456678Seric 				/* explicit fall-through */
65556678Seric 
65656678Seric 			  case MATCHONE:
65756678Seric 			  case MATCHANY:
65856678Seric 				/* match exactly one token */
65956678Seric 				mlp->first = avp;
66056678Seric 				mlp->last = avp++;
6618058Seric 				mlp++;
66256678Seric 				break;
6638058Seric 
66456678Seric 			  case MATCHZANY:
66556678Seric 				/* match zero or more tokens */
66656678Seric 				mlp->first = avp;
66756678Seric 				mlp->last = avp - 1;
66856678Seric 				mlp++;
66956678Seric 				break;
67056326Seric 
67158173Seric 			  case CANONHOST:
67258173Seric 				/* match zero tokens */
67358173Seric 				break;
67458173Seric 
67556678Seric 			  default:
67656678Seric 				/* must have exact match */
67756678Seric 				if (strcasecmp(rp, ap))
6788058Seric 					goto backup;
6794468Seric 				avp++;
68056678Seric 				break;
6813149Seric 			}
6823149Seric 
68356678Seric 			/* successful match on this token */
6843149Seric 			rvp++;
6853149Seric 			continue;
6863149Seric 
68756678Seric 		  backup:
68856678Seric 			/* match failed -- back up */
68956678Seric 			while (--rvp >= rwr->r_lhs)
6903149Seric 			{
69156678Seric 				rp = *rvp;
69258050Seric 				if ((*rp & 0377) == MATCHANY ||
69358050Seric 				    (*rp & 0377) == MATCHZANY)
6944468Seric 				{
69556678Seric 					/* extend binding and continue */
69656678Seric 					avp = ++mlp[-1].last;
69756678Seric 					avp++;
69856678Seric 					rvp++;
69956678Seric 					break;
7004468Seric 				}
70156678Seric 				avp--;
70258050Seric 				if ((*rp & 0377) == MATCHONE ||
70358050Seric 				    (*rp & 0377) == MATCHCLASS ||
70458050Seric 				    (*rp & 0377) == MATCHNCLASS)
70556678Seric 				{
70656678Seric 					/* back out binding */
70756678Seric 					mlp--;
70856678Seric 				}
7093149Seric 			}
7103149Seric 
71156678Seric 			if (rvp < rwr->r_lhs)
71256678Seric 			{
71356678Seric 				/* total failure to match */
71456326Seric 				break;
7153149Seric 			}
716297Seric 		}
7173149Seric 
7183149Seric 		/*
71956678Seric 		**  See if we successfully matched
7203149Seric 		*/
7213149Seric 
72256678Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
7233149Seric 		{
7249374Seric 			if (tTd(21, 10))
7259374Seric 				printf("----- rule fails\n");
7269374Seric 			rwr = rwr->r_next;
7279374Seric 			continue;
7289374Seric 		}
7293149Seric 
7309374Seric 		rvp = rwr->r_rhs;
7319374Seric 		if (tTd(21, 12))
7329374Seric 		{
7339374Seric 			printf("-----rule matches:");
73456678Seric 			printav(rvp);
7359374Seric 		}
7369374Seric 
7379374Seric 		rp = *rvp;
73858050Seric 		if ((*rp & 0377) == CANONUSER)
7399374Seric 		{
7409374Seric 			rvp++;
7419374Seric 			rwr = rwr->r_next;
7429374Seric 		}
74358050Seric 		else if ((*rp & 0377) == CANONHOST)
7449374Seric 		{
7459374Seric 			rvp++;
7469374Seric 			rwr = NULL;
7479374Seric 		}
74858050Seric 		else if ((*rp & 0377) == CANONNET)
7499374Seric 			rwr = NULL;
7509374Seric 
7519374Seric 		/* substitute */
7529374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
7539374Seric 		{
7549374Seric 			register struct match *m;
7559374Seric 			register char **pp;
7569374Seric 
7578058Seric 			rp = *rvp;
75858050Seric 			if ((*rp & 0377) == MATCHREPL)
7598058Seric 			{
76016914Seric 				/* substitute from LHS */
76116914Seric 				m = &mlist[rp[1] - '1'];
76256678Seric 				if (m < mlist || m >= mlp)
7639374Seric 				{
76458151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
76556326Seric 						ruleset, rp[1]);
7669374Seric 					return;
7679374Seric 				}
76816914Seric 				if (tTd(21, 15))
76916914Seric 				{
77016914Seric 					printf("$%c:", rp[1]);
77116914Seric 					pp = m->first;
77256678Seric 					while (pp <= m->last)
77316914Seric 					{
77416914Seric 						printf(" %x=\"", *pp);
77516914Seric 						(void) fflush(stdout);
77616914Seric 						printf("%s\"", *pp++);
77716914Seric 					}
77816914Seric 					printf("\n");
77916914Seric 				}
7809374Seric 				pp = m->first;
78156678Seric 				while (pp <= m->last)
7823149Seric 				{
78316914Seric 					if (avp >= &npvp[MAXATOM])
78456678Seric 					{
78558151Seric 						syserr("554 rewrite: expansion too long");
78656678Seric 						return;
78756678Seric 					}
78816914Seric 					*avp++ = *pp++;
7893149Seric 				}
7903149Seric 			}
79116914Seric 			else
7928226Seric 			{
79316914Seric 				/* vanilla replacement */
7949374Seric 				if (avp >= &npvp[MAXATOM])
79516889Seric 				{
79656678Seric 	toolong:
79758151Seric 					syserr("554 rewrite: expansion too long");
79816889Seric 					return;
79916889Seric 				}
80056678Seric 				*avp++ = rp;
8018226Seric 			}
8029374Seric 		}
8039374Seric 		*avp++ = NULL;
80416914Seric 
80516914Seric 		/*
80656678Seric 		**  Check for any hostname/keyword lookups.
80716914Seric 		*/
80816914Seric 
80916914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
81016914Seric 		{
81156678Seric 			char **hbrvp;
81216914Seric 			char **xpvp;
81316914Seric 			int trsize;
81417473Seric 			char *olddelimchar;
81556678Seric 			char *replac;
81656678Seric 			int endtoken;
81756678Seric 			STAB *map;
81856678Seric 			char *mapname;
81956678Seric 			char **key_rvp;
82056678Seric 			char **arg_rvp;
82156678Seric 			char **default_rvp;
82256678Seric 			char buf[MAXNAME + 1];
82316914Seric 			char *pvpb1[MAXATOM + 1];
82456823Seric 			char *argvect[10];
82517174Seric 			char pvpbuf[PSBUFSIZE];
82616914Seric 
82758050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
82858050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
82916914Seric 				continue;
83016914Seric 
83116914Seric 			/*
83256678Seric 			**  Got a hostname/keyword lookup.
83316914Seric 			**
83416914Seric 			**	This could be optimized fairly easily.
83516914Seric 			*/
83616914Seric 
83716914Seric 			hbrvp = rvp;
83858050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
83956327Seric 			{
84056678Seric 				endtoken = HOSTEND;
84156678Seric 				mapname = "host";
84256327Seric 			}
84356326Seric 			else
84456327Seric 			{
84556678Seric 				endtoken = LOOKUPEND;
84656678Seric 				mapname = *++rvp;
84756327Seric 			}
84856678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
84956678Seric 			if (map == NULL)
85058151Seric 				syserr("554 rewrite: map %s not found", mapname);
85156678Seric 
85256678Seric 			/* extract the match part */
85356678Seric 			key_rvp = ++rvp;
85456823Seric 			default_rvp = NULL;
85556823Seric 			arg_rvp = argvect;
85656823Seric 			xpvp = NULL;
85756823Seric 			replac = pvpbuf;
85858050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
85953654Seric 			{
86058050Seric 				int nodetype = **rvp & 0377;
86156823Seric 
86256823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
86356678Seric 				{
86456823Seric 					rvp++;
86556823Seric 					continue;
86656823Seric 				}
86756823Seric 
86856823Seric 				*rvp++ = NULL;
86956823Seric 
87056823Seric 				if (xpvp != NULL)
87156823Seric 				{
87256823Seric 					cataddr(xpvp, replac,
87358082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
87458082Seric 						'\0');
87556823Seric 					*++arg_rvp = replac;
87656823Seric 					replac += strlen(replac) + 1;
87756823Seric 					xpvp = NULL;
87856823Seric 				}
87956823Seric 				switch (nodetype)
88056823Seric 				{
88156678Seric 				  case CANONHOST:
88256823Seric 					xpvp = rvp;
88356678Seric 					break;
88456678Seric 
88556678Seric 				  case CANONUSER:
88656678Seric 					default_rvp = rvp;
88756678Seric 					break;
88856678Seric 				}
88953654Seric 			}
89016914Seric 			if (*rvp != NULL)
89116914Seric 				*rvp++ = NULL;
89256823Seric 			if (xpvp != NULL)
89356823Seric 			{
89456823Seric 				cataddr(xpvp, replac,
89558082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
89658082Seric 					'\0');
89756823Seric 				*++arg_rvp = replac;
89856823Seric 			}
89956823Seric 			*++arg_rvp = NULL;
90016914Seric 
90116914Seric 			/* save the remainder of the input string */
90216914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
90316914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
90416914Seric 
90556678Seric 			/* look it up */
90658082Seric 			cataddr(key_rvp, buf, sizeof buf, '\0');
90756823Seric 			argvect[0] = buf;
90856678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
90956836Seric 			{
91056836Seric 				int bsize = sizeof buf - 1;
91156836Seric 
91256836Seric 				if (map->s_map.map_app != NULL)
91356836Seric 					bsize -= strlen(map->s_map.map_app);
91456836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
91556823Seric 						buf, sizeof buf - 1, argvect);
91656836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
91756836Seric 					strcat(replac, map->s_map.map_app);
91856836Seric 			}
91953654Seric 			else
92056678Seric 				replac = NULL;
92156678Seric 
92256678Seric 			/* if no replacement, use default */
92356823Seric 			if (replac == NULL && default_rvp != NULL)
92456823Seric 			{
92556823Seric 				char buf2[sizeof buf];
92656823Seric 
92756823Seric 				/* rewrite the default with % translations */
92858082Seric 				cataddr(default_rvp, buf2, sizeof buf2, '\0');
92956823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
93056823Seric 					argvect);
93156823Seric 				replac = buf;
93256823Seric 			}
93356823Seric 
93456678Seric 			if (replac == NULL)
93551317Seric 			{
93656823Seric 				xpvp = key_rvp;
93753654Seric 			}
93856678Seric 			else
93953654Seric 			{
94056678Seric 				/* scan the new replacement */
94158333Seric 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
94253654Seric 				if (xpvp == NULL)
94351317Seric 				{
94458403Seric 					/* prescan already printed error */
94556678Seric 					return;
94656678Seric 				}
94751317Seric 			}
94851317Seric 
94916914Seric 			/* append it to the token list */
95056678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
95156678Seric 			{
95217174Seric 				*avp++ = newstr(*xpvp);
95316920Seric 				if (avp >= &npvp[MAXATOM])
95416914Seric 					goto toolong;
95517174Seric 			}
95616914Seric 
95716914Seric 			/* restore the old trailing information */
95856678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
95916920Seric 				if (avp >= &npvp[MAXATOM])
96016914Seric 					goto toolong;
96117174Seric 
96256678Seric 			break;
96316914Seric 		}
96416914Seric 
96516914Seric 		/*
96616914Seric 		**  Check for subroutine calls.
96716914Seric 		*/
96816914Seric 
96958050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
97056678Seric 		{
97156678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
97256678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
97356678Seric 			if (tTd(21, 3))
97456678Seric 				printf("-----callsubr %s\n", npvp[1]);
97556678Seric 			rewrite(pvp, atoi(npvp[1]));
97656678Seric 		}
97756678Seric 		else
97856678Seric 		{
97917348Seric 			bcopy((char *) npvp, (char *) pvp,
98016900Seric 				(int) (avp - npvp) * sizeof *avp);
98156678Seric 		}
9829374Seric 		if (tTd(21, 4))
9839374Seric 		{
9849374Seric 			printf("rewritten as:");
98556678Seric 			printav(pvp);
9869374Seric 		}
987297Seric 	}
9888069Seric 
9899279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
9908069Seric 	{
9918959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
99256678Seric 		printav(pvp);
9938069Seric 	}
9943149Seric }
9953149Seric /*
9963149Seric **  BUILDADDR -- build address from token vector.
9973149Seric **
9983149Seric **	Parameters:
9993149Seric **		tv -- token vector.
10003149Seric **		a -- pointer to address descriptor to fill.
10013149Seric **			If NULL, one will be allocated.
10023149Seric **
10033149Seric **	Returns:
10044279Seric **		NULL if there was an error.
10054279Seric **		'a' otherwise.
10063149Seric **
10073149Seric **	Side Effects:
10083149Seric **		fills in 'a'
10093149Seric */
10103149Seric 
101157249Seric struct errcodes
101257249Seric {
101357249Seric 	char	*ec_name;		/* name of error code */
101457249Seric 	int	ec_code;		/* numeric code */
101557249Seric } ErrorCodes[] =
101657249Seric {
101757249Seric 	"usage",	EX_USAGE,
101857249Seric 	"nouser",	EX_NOUSER,
101957249Seric 	"nohost",	EX_NOHOST,
102057249Seric 	"unavailable",	EX_UNAVAILABLE,
102157249Seric 	"software",	EX_SOFTWARE,
102257249Seric 	"tempfail",	EX_TEMPFAIL,
102357249Seric 	"protocol",	EX_PROTOCOL,
102457249Seric #ifdef EX_CONFIG
102557249Seric 	"config",	EX_CONFIG,
102657249Seric #endif
102757249Seric 	NULL,		EX_UNAVAILABLE,
102857249Seric };
102957249Seric 
103056678Seric ADDRESS *
10313149Seric buildaddr(tv, a)
10323149Seric 	register char **tv;
10333149Seric 	register ADDRESS *a;
10343149Seric {
10353149Seric 	struct mailer **mp;
10363149Seric 	register struct mailer *m;
103758008Seric 	char *bp;
103858008Seric 	int spaceleft;
103957402Seric 	static char buf[MAXNAME];
10403149Seric 
10413149Seric 	if (a == NULL)
10423149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
104316889Seric 	bzero((char *) a, sizeof *a);
10443149Seric 
10453149Seric 	/* figure out what net/mailer to use */
104658050Seric 	if ((**tv & 0377) != CANONNET)
10474279Seric 	{
104858151Seric 		syserr("554 buildaddr: no net");
10494279Seric 		return (NULL);
10504279Seric 	}
10513149Seric 	tv++;
105233725Sbostic 	if (!strcasecmp(*tv, "error"))
10534279Seric 	{
105458050Seric 		if ((**++tv & 0377) == CANONHOST)
105510183Seric 		{
105657249Seric 			register struct errcodes *ep;
105757249Seric 
105858050Seric 			if (isascii(**++tv) && isdigit(**tv))
105957249Seric 			{
106057249Seric 				setstat(atoi(*tv));
106157249Seric 			}
106257249Seric 			else
106357249Seric 			{
106457249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
106557249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
106657249Seric 						break;
106757249Seric 				setstat(ep->ec_code);
106857249Seric 			}
106910183Seric 			tv++;
107010183Seric 		}
107158050Seric 		if ((**tv & 0377) != CANONUSER)
107258151Seric 			syserr("554 buildaddr: error: no user");
107358082Seric 		cataddr(++tv, buf, sizeof buf, ' ');
107458082Seric 		stripquotes(buf);
10754279Seric 		usrerr(buf);
10764279Seric 		return (NULL);
10774279Seric 	}
107857402Seric 
10794598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
10803149Seric 	{
108133725Sbostic 		if (!strcasecmp(m->m_name, *tv))
10823149Seric 			break;
10833149Seric 	}
10843149Seric 	if (m == NULL)
10854279Seric 	{
108658151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
10874279Seric 		return (NULL);
10884279Seric 	}
10894598Seric 	a->q_mailer = m;
10903149Seric 
10913149Seric 	/* figure out what host (if any) */
109256678Seric 	tv++;
109358509Seric 	if ((**tv & 0377) == CANONHOST)
10943149Seric 	{
109558008Seric 		bp = buf;
109658008Seric 		spaceleft = sizeof buf - 1;
109758050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
109858008Seric 		{
109958008Seric 			int i = strlen(*tv);
110058008Seric 
110158008Seric 			if (i > spaceleft)
110258008Seric 			{
110358008Seric 				/* out of space for this address */
110458008Seric 				if (spaceleft >= 0)
110558151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
110658008Seric 						buf);
110758008Seric 				i = spaceleft;
110858008Seric 				spaceleft = 0;
110958008Seric 			}
111058008Seric 			if (i <= 0)
111158008Seric 				continue;
111258008Seric 			bcopy(*tv, bp, i);
111358008Seric 			bp += i;
111458008Seric 			spaceleft -= i;
111558008Seric 		}
111658010Seric 		*bp = '\0';
11175704Seric 		a->q_host = newstr(buf);
11183149Seric 	}
111957249Seric 	else
112058509Seric 	{
112158509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
112258509Seric 		{
112358509Seric 			syserr("554 buildaddr: no host");
112458509Seric 			return (NULL);
112558509Seric 		}
112657249Seric 		a->q_host = NULL;
112758509Seric 	}
11283149Seric 
11293149Seric 	/* figure out the user */
113058050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
11314279Seric 	{
113258151Seric 		syserr("554 buildaddr: no user");
11334279Seric 		return (NULL);
11344279Seric 	}
113557402Seric 	tv++;
113651317Seric 
113757402Seric 	/* do special mapping for local mailer */
113857402Seric 	if (m == LocalMailer && *tv != NULL)
113957402Seric 	{
114057454Seric 		register char *p = *tv;
114157454Seric 
114257454Seric 		if (*p == '"')
114357454Seric 			p++;
114457454Seric 		if (*p == '|')
114557402Seric 			a->q_mailer = m = ProgMailer;
114657454Seric 		else if (*p == '/')
114757402Seric 			a->q_mailer = m = FileMailer;
114857454Seric 		else if (*p == ':')
114957402Seric 		{
115057402Seric 			/* may be :include: */
115158082Seric 			cataddr(tv, buf, sizeof buf, '\0');
115258008Seric 			stripquotes(buf);
115358008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
115458008Seric 			{
115558008Seric 				/* if :include:, don't need further rewriting */
115657402Seric 				a->q_mailer = m = InclMailer;
115758008Seric 				a->q_user = &buf[9];
115858008Seric 				return (a);
115958008Seric 			}
116057402Seric 		}
116157402Seric 	}
116257402Seric 
116358008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
116458008Seric 	{
116558008Seric 		tv++;
116658008Seric 		a->q_flags |= QNOTREMOTE;
116758008Seric 	}
116858008Seric 
116919040Seric 	/* rewrite according recipient mailer rewriting rules */
117057402Seric 	rewrite(tv, 2);
117158020Seric 	if (m->m_re_rwset > 0)
117258020Seric 		rewrite(tv, m->m_re_rwset);
117319040Seric 	rewrite(tv, 4);
117419040Seric 
117519040Seric 	/* save the result for the command line/RCPT argument */
117658082Seric 	cataddr(tv, buf, sizeof buf, '\0');
11773149Seric 	a->q_user = buf;
11783149Seric 
1179*58670Seric 	/*
1180*58670Seric 	**  Do mapping to lower case as requested by mailer
1181*58670Seric 	*/
1182*58670Seric 
1183*58670Seric 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
1184*58670Seric 		makelower(a->q_host);
1185*58670Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
1186*58670Seric 		makelower(a->q_user);
1187*58670Seric 
11883149Seric 	return (a);
11893149Seric }
11903188Seric /*
11914228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
11924228Seric **
11934228Seric **	Parameters:
11944228Seric **		pvp -- parameter vector to rebuild.
11954228Seric **		buf -- buffer to build the string into.
11964228Seric **		sz -- size of buf.
119758082Seric **		spacesub -- the space separator character; if null,
119858082Seric **			use SpaceSub.
11994228Seric **
12004228Seric **	Returns:
12014228Seric **		none.
12024228Seric **
12034228Seric **	Side Effects:
12044228Seric **		Destroys buf.
12054228Seric */
12064228Seric 
120758082Seric cataddr(pvp, buf, sz, spacesub)
12084228Seric 	char **pvp;
12094228Seric 	char *buf;
12104228Seric 	register int sz;
121158082Seric 	char spacesub;
12124228Seric {
12134228Seric 	bool oatomtok = FALSE;
121456678Seric 	bool natomtok = FALSE;
12154228Seric 	register int i;
12164228Seric 	register char *p;
12174228Seric 
121858082Seric 	if (spacesub == '\0')
121958082Seric 		spacesub = SpaceSub;
122058082Seric 
12218423Seric 	if (pvp == NULL)
12228423Seric 	{
122323109Seric 		(void) strcpy(buf, "");
12248423Seric 		return;
12258423Seric 	}
12264228Seric 	p = buf;
122711156Seric 	sz -= 2;
12284228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
12294228Seric 	{
12308078Seric 		natomtok = (toktype(**pvp) == ATM);
12314228Seric 		if (oatomtok && natomtok)
123258082Seric 			*p++ = spacesub;
12334228Seric 		(void) strcpy(p, *pvp);
12344228Seric 		oatomtok = natomtok;
12354228Seric 		p += i;
123611156Seric 		sz -= i + 1;
12374228Seric 		pvp++;
12384228Seric 	}
12394228Seric 	*p = '\0';
12404228Seric }
12414228Seric /*
12423188Seric **  SAMEADDR -- Determine if two addresses are the same
12433188Seric **
12443188Seric **	This is not just a straight comparison -- if the mailer doesn't
12453188Seric **	care about the host we just ignore it, etc.
12463188Seric **
12473188Seric **	Parameters:
12483188Seric **		a, b -- pointers to the internal forms to compare.
12493188Seric **
12503188Seric **	Returns:
12513188Seric **		TRUE -- they represent the same mailbox.
12523188Seric **		FALSE -- they don't.
12533188Seric **
12543188Seric **	Side Effects:
12553188Seric **		none.
12563188Seric */
12573188Seric 
12583188Seric bool
12599374Seric sameaddr(a, b)
12603188Seric 	register ADDRESS *a;
12613188Seric 	register ADDRESS *b;
12623188Seric {
12633188Seric 	/* if they don't have the same mailer, forget it */
12643188Seric 	if (a->q_mailer != b->q_mailer)
12653188Seric 		return (FALSE);
12663188Seric 
12673188Seric 	/* if the user isn't the same, we can drop out */
126856678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
12693188Seric 		return (FALSE);
12703188Seric 
127158438Seric 	/* if we have good uids for both but the differ, these are different */
127258438Seric 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
127358438Seric 		return (FALSE);
127458438Seric 
127558509Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
127658509Seric 	if (a->q_host == b->q_host)
127758509Seric 	{
127858509Seric 		/* probably both null pointers */
12793188Seric 		return (TRUE);
128058509Seric 	}
12813188Seric 	if (a->q_host == NULL || b->q_host == NULL)
128258509Seric 	{
128358509Seric 		/* only one is a null pointer */
12843188Seric 		return (FALSE);
128558509Seric 	}
128656678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
12873188Seric 		return (FALSE);
12883188Seric 
12893188Seric 	return (TRUE);
12903188Seric }
12913234Seric /*
12923234Seric **  PRINTADDR -- print address (for debugging)
12933234Seric **
12943234Seric **	Parameters:
12953234Seric **		a -- the address to print
12963234Seric **		follow -- follow the q_next chain.
12973234Seric **
12983234Seric **	Returns:
12993234Seric **		none.
13003234Seric **
13013234Seric **	Side Effects:
13023234Seric **		none.
13033234Seric */
13043234Seric 
13053234Seric printaddr(a, follow)
13063234Seric 	register ADDRESS *a;
13073234Seric 	bool follow;
13083234Seric {
13095001Seric 	bool first = TRUE;
131057731Seric 	register MAILER *m;
131157731Seric 	MAILER pseudomailer;
13125001Seric 
13133234Seric 	while (a != NULL)
13143234Seric 	{
13155001Seric 		first = FALSE;
13164443Seric 		printf("%x=", a);
13174085Seric 		(void) fflush(stdout);
131857731Seric 
131957731Seric 		/* find the mailer -- carefully */
132057731Seric 		m = a->q_mailer;
132157731Seric 		if (m == NULL)
132257731Seric 		{
132357731Seric 			m = &pseudomailer;
132457731Seric 			m->m_mno = -1;
132557731Seric 			m->m_name = "NULL";
132657731Seric 		}
132757731Seric 
132840973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
132957731Seric 		       a->q_paddr, m->m_mno, m->m_name,
133040973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
13318181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
13328181Seric 		       a->q_alias);
13338181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
13348181Seric 		       a->q_fullname);
13354996Seric 
13363234Seric 		if (!follow)
13373234Seric 			return;
13384996Seric 		a = a->q_next;
13393234Seric 	}
13405001Seric 	if (first)
13414443Seric 		printf("[NULL]\n");
13423234Seric }
13434317Seric 
13447682Seric /*
13457682Seric **  REMOTENAME -- return the name relative to the current mailer
13467682Seric **
13477682Seric **	Parameters:
13487682Seric **		name -- the name to translate.
13498069Seric **		m -- the mailer that we want to do rewriting relative
13508069Seric **			to.
13518069Seric **		senderaddress -- if set, uses the sender rewriting rules
13528069Seric **			rather than the recipient rewriting rules.
135358020Seric **		header -- set if this address is in the header, rather
135458020Seric **			than an envelope header.
135510310Seric **		canonical -- if set, strip out any comment information,
135610310Seric **			etc.
135758509Seric **		adddomain -- if set, OK to do domain extension.
135858020Seric **		e -- the current envelope.
13597682Seric **
13607682Seric **	Returns:
13617682Seric **		the text string representing this address relative to
13627682Seric **			the receiving mailer.
13637682Seric **
13647682Seric **	Side Effects:
13657682Seric **		none.
13667682Seric **
13677682Seric **	Warnings:
13687682Seric **		The text string returned is tucked away locally;
13697682Seric **			copy it if you intend to save it.
13707682Seric */
13717682Seric 
13727682Seric char *
137358509Seric remotename(name, m, senderaddress, header, canonical, adddomain, e)
13747682Seric 	char *name;
137556678Seric 	struct mailer *m;
13768069Seric 	bool senderaddress;
137758020Seric 	bool header;
137810310Seric 	bool canonical;
137958509Seric 	bool adddomain;
138056678Seric 	register ENVELOPE *e;
13817682Seric {
13828069Seric 	register char **pvp;
13838069Seric 	char *fancy;
138456678Seric 	extern char *macvalue();
138556678Seric 	char *oldg = macvalue('g', e);
138658020Seric 	int rwset;
13877682Seric 	static char buf[MAXNAME];
13887682Seric 	char lbuf[MAXNAME];
138916914Seric 	char pvpbuf[PSBUFSIZE];
139056678Seric 	extern char **prescan();
139156678Seric 	extern char *crackaddr();
13927682Seric 
13937755Seric 	if (tTd(12, 1))
13947755Seric 		printf("remotename(%s)\n", name);
13957755Seric 
139610177Seric 	/* don't do anything if we are tagging it as special */
139758020Seric 	if (senderaddress)
139858020Seric 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
139958020Seric 	else
140058020Seric 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
140158020Seric 	if (rwset < 0)
140210177Seric 		return (name);
140310177Seric 
14047682Seric 	/*
14058181Seric 	**  Do a heuristic crack of this name to extract any comment info.
14068181Seric 	**	This will leave the name as a comment and a $g macro.
14077889Seric 	*/
14087889Seric 
140958036Seric 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
141058050Seric 		fancy = "\201g";
141110310Seric 	else
141210310Seric 		fancy = crackaddr(name);
14137889Seric 
14148181Seric 	/*
14158181Seric 	**  Turn the name into canonical form.
14168181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
14178181Seric 	**	If this only resolves to "user", and the "C" flag is
14188181Seric 	**	specified in the sending mailer, then the sender's
14198181Seric 	**	domain will be appended.
14208181Seric 	*/
14218181Seric 
142258333Seric 	pvp = prescan(name, '\0', pvpbuf, NULL);
14237889Seric 	if (pvp == NULL)
14247889Seric 		return (name);
14258181Seric 	rewrite(pvp, 3);
142658509Seric 	if (adddomain && e->e_fromdomain != NULL)
14278181Seric 	{
14288181Seric 		/* append from domain to this address */
14298181Seric 		register char **pxp = pvp;
14308181Seric 
14319594Seric 		/* see if there is an "@domain" in the current name */
14328181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
14338181Seric 			pxp++;
14348181Seric 		if (*pxp == NULL)
14358181Seric 		{
14369594Seric 			/* no.... append the "@domain" from the sender */
143756678Seric 			register char **qxq = e->e_fromdomain;
14388181Seric 
14399594Seric 			while ((*pxp++ = *qxq++) != NULL)
14409594Seric 				continue;
144111726Seric 			rewrite(pvp, 3);
14428181Seric 		}
14438181Seric 	}
14448181Seric 
14458181Seric 	/*
14468959Seric 	**  Do more specific rewriting.
144756678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
144856678Seric 	**		a sender address or not.
14498181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
14508181Seric 	*/
14518181Seric 
14528069Seric 	if (senderaddress)
145356327Seric 		rewrite(pvp, 1);
14548069Seric 	else
145556327Seric 		rewrite(pvp, 2);
145658020Seric 	if (rwset > 0)
145758020Seric 		rewrite(pvp, rwset);
14587682Seric 
14598181Seric 	/*
14608959Seric 	**  Do any final sanitation the address may require.
14618959Seric 	**	This will normally be used to turn internal forms
14628959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
14638959Seric 	**	may be used as a default to the above rules.
14648959Seric 	*/
14658959Seric 
14668959Seric 	rewrite(pvp, 4);
14678959Seric 
14688959Seric 	/*
14698181Seric 	**  Now restore the comment information we had at the beginning.
14708181Seric 	*/
14718181Seric 
147258082Seric 	cataddr(pvp, lbuf, sizeof lbuf, '\0');
147356678Seric 	define('g', lbuf, e);
147456678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
147556678Seric 	define('g', oldg, e);
14767682Seric 
14777682Seric 	if (tTd(12, 1))
14787755Seric 		printf("remotename => `%s'\n", buf);
14797682Seric 	return (buf);
14807682Seric }
148151317Seric /*
148256678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
148351317Seric **
148451317Seric **	Parameters:
148556678Seric **		a -- the address to map (but just the user name part).
148656678Seric **		sendq -- the sendq in which to install any replacement
148756678Seric **			addresses.
148851317Seric **
148951317Seric **	Returns:
149051317Seric **		none.
149151317Seric */
149251317Seric 
149356678Seric maplocaluser(a, sendq, e)
149456678Seric 	register ADDRESS *a;
149556678Seric 	ADDRESS **sendq;
149656678Seric 	ENVELOPE *e;
149751317Seric {
149856678Seric 	register char **pvp;
149956678Seric 	register ADDRESS *a1 = NULL;
150058333Seric 	auto char *delimptr;
150156678Seric 	char pvpbuf[PSBUFSIZE];
150251317Seric 
150356678Seric 	if (tTd(29, 1))
150456678Seric 	{
150556678Seric 		printf("maplocaluser: ");
150656678Seric 		printaddr(a, FALSE);
150756678Seric 	}
150858333Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
150956678Seric 	if (pvp == NULL)
151056678Seric 		return;
151151317Seric 
151256678Seric 	rewrite(pvp, 5);
151358050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
151456678Seric 		return;
151551317Seric 
151656678Seric 	/* if non-null, mailer destination specified -- has it changed? */
151756678Seric 	a1 = buildaddr(pvp, NULL);
151856678Seric 	if (a1 == NULL || sameaddr(a, a1))
151956678Seric 		return;
152051317Seric 
152156678Seric 	/* mark old address as dead; insert new address */
152256678Seric 	a->q_flags |= QDONTSEND;
152357731Seric 	if (tTd(29, 5))
152457731Seric 	{
152557731Seric 		printf("maplocaluser: QDONTSEND ");
152657731Seric 		printaddr(a, FALSE);
152757731Seric 	}
152856678Seric 	a1->q_alias = a;
152958333Seric 	allocaddr(a1, 1, NULL, delimptr);
153056678Seric 	(void) recipient(a1, sendq, e);
153151317Seric }
1532