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*58966Seric static char sccsid[] = "@(#)parseaddr.c	6.36 (Berkeley) 04/05/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 
124*58966Seric 	a = buildaddr(pvp, a, e);
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 
19858673Seric 	if (tTd(24, 4))
19958673Seric 		printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr);
20058673Seric 
20156678Seric 	if (copyf > 0 && paddr != NULL)
20256678Seric 	{
20358333Seric 		char savec = *delimptr;
2048078Seric 
20558333Seric 		*delimptr = '\0';
20656678Seric 		a->q_paddr = newstr(paddr);
20758333Seric 		*delimptr = savec;
2088078Seric 	}
209297Seric 	else
21056678Seric 		a->q_paddr = paddr;
21124944Seric 
21224944Seric 	if (a->q_user == NULL)
21324944Seric 		a->q_user = "";
21424944Seric 	if (a->q_host == NULL)
21524944Seric 		a->q_host = "";
21624944Seric 
2173149Seric 	if (copyf >= 0)
218297Seric 	{
21924944Seric 		a->q_host = newstr(a->q_host);
2203149Seric 		if (a->q_user != a->q_paddr)
2213149Seric 			a->q_user = newstr(a->q_user);
222297Seric 	}
223297Seric 
22456678Seric 	if (a->q_paddr == NULL)
22556678Seric 		a->q_paddr = a->q_user;
226297Seric }
227297Seric /*
228297Seric **  PRESCAN -- Prescan name and make it canonical
229297Seric **
2309374Seric **	Scans a name and turns it into a set of tokens.  This process
2319374Seric **	deletes blanks and comments (in parentheses).
232297Seric **
233297Seric **	This routine knows about quoted strings and angle brackets.
234297Seric **
235297Seric **	There are certain subtleties to this routine.  The one that
236297Seric **	comes to mind now is that backslashes on the ends of names
237297Seric **	are silently stripped off; this is intentional.  The problem
238297Seric **	is that some versions of sndmsg (like at LBL) set the kill
239297Seric **	character to something other than @ when reading addresses;
240297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
241297Seric **	berknet mailer.
242297Seric **
243297Seric **	Parameters:
244297Seric **		addr -- the name to chomp.
245297Seric **		delim -- the delimiter for the address, normally
246297Seric **			'\0' or ','; \0 is accepted in any case.
24715284Seric **			If '\t' then we are reading the .cf file.
24816914Seric **		pvpbuf -- place to put the saved text -- note that
24916914Seric **			the pointers are static.
25058333Seric **		delimptr -- if non-NULL, set to the location of the
25158333Seric **			terminating delimiter.
252297Seric **
253297Seric **	Returns:
2543149Seric **		A pointer to a vector of tokens.
255297Seric **		NULL on error.
256297Seric */
257297Seric 
2588078Seric /* states and character types */
2598078Seric # define OPR		0	/* operator */
2608078Seric # define ATM		1	/* atom */
2618078Seric # define QST		2	/* in quoted string */
2628078Seric # define SPC		3	/* chewing up spaces */
2638078Seric # define ONE		4	/* pick up one character */
2643149Seric 
2658078Seric # define NSTATES	5	/* number of states */
2668078Seric # define TYPE		017	/* mask to select state type */
2678078Seric 
2688078Seric /* meta bits for table */
2698078Seric # define M		020	/* meta character; don't pass through */
2708078Seric # define B		040	/* cause a break */
2718078Seric # define MB		M|B	/* meta-break */
2728078Seric 
2738078Seric static short StateTab[NSTATES][NSTATES] =
2748078Seric {
2758087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2769051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2779051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2789051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2798078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2808078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2818078Seric };
2828078Seric 
2838078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2848078Seric 
2853149Seric char **
28658333Seric prescan(addr, delim, pvpbuf, delimptr)
287297Seric 	char *addr;
288297Seric 	char delim;
28916914Seric 	char pvpbuf[];
29058333Seric 	char **delimptr;
291297Seric {
292297Seric 	register char *p;
2938078Seric 	register char *q;
2949346Seric 	register int c;
2953149Seric 	char **avp;
296297Seric 	bool bslashmode;
297297Seric 	int cmntcnt;
2988423Seric 	int anglecnt;
2993149Seric 	char *tok;
3008078Seric 	int state;
3018078Seric 	int newstate;
3028078Seric 	static char *av[MAXATOM+1];
30356678Seric 	extern int errno;
304297Seric 
30515253Seric 	/* make sure error messages don't have garbage on them */
30615253Seric 	errno = 0;
30715253Seric 
30816914Seric 	q = pvpbuf;
3093149Seric 	bslashmode = FALSE;
3107800Seric 	cmntcnt = 0;
3118423Seric 	anglecnt = 0;
3123149Seric 	avp = av;
31356678Seric 	state = ATM;
3148078Seric 	c = NOCHAR;
3158078Seric 	p = addr;
31656764Seric 	if (tTd(22, 11))
317297Seric 	{
3188078Seric 		printf("prescan: ");
3198078Seric 		xputs(p);
32023109Seric 		(void) putchar('\n');
3218078Seric 	}
3228078Seric 
3238078Seric 	do
3248078Seric 	{
3253149Seric 		/* read a token */
3263149Seric 		tok = q;
3278078Seric 		for (;;)
328297Seric 		{
3298078Seric 			/* store away any old lookahead character */
3308078Seric 			if (c != NOCHAR)
3318078Seric 			{
33215284Seric 				/* see if there is room */
33316914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3348078Seric 				{
33558151Seric 					usrerr("553 Address too long");
33658333Seric 					if (delimptr != NULL)
33758333Seric 						*delimptr = p;
3388078Seric 					return (NULL);
3398078Seric 				}
34015284Seric 
34115284Seric 				/* squirrel it away */
3428078Seric 				*q++ = c;
3438078Seric 			}
3448078Seric 
3458078Seric 			/* read a new input character */
3468078Seric 			c = *p++;
34757631Seric 			if (c == '\0')
34856764Seric 			{
34956764Seric 				/* diagnose and patch up bad syntax */
35056764Seric 				if (state == QST)
35156764Seric 				{
35258151Seric 					usrerr("553 Unbalanced '\"'");
35356764Seric 					c = '"';
35456764Seric 				}
35556764Seric 				else if (cmntcnt > 0)
35656764Seric 				{
35758151Seric 					usrerr("553 Unbalanced '('");
35856764Seric 					c = ')';
35956764Seric 				}
36056764Seric 				else if (anglecnt > 0)
36156764Seric 				{
36256764Seric 					c = '>';
36358151Seric 					usrerr("553 Unbalanced '<'");
36456764Seric 				}
36556764Seric 				else
36656764Seric 					break;
36715284Seric 
36856764Seric 				p--;
36956764Seric 			}
37057631Seric 			else if (c == delim && anglecnt <= 0 &&
37157631Seric 					cmntcnt <= 0 && state != QST)
37257631Seric 				break;
37356764Seric 
3748078Seric 			if (tTd(22, 101))
3758078Seric 				printf("c=%c, s=%d; ", c, state);
3768078Seric 
3773149Seric 			/* chew up special characters */
3783149Seric 			*q = '\0';
3793149Seric 			if (bslashmode)
3803149Seric 			{
38124944Seric 				/* kludge \! for naive users */
38258061Seric 				if (cmntcnt > 0)
38358061Seric 					c = NOCHAR;
38458061Seric 				else if (c != '!')
38556678Seric 					*q++ = '\\';
3863149Seric 				bslashmode = FALSE;
38756678Seric 				continue;
3883149Seric 			}
38956678Seric 
39056678Seric 			if (c == '\\')
3913149Seric 			{
3923149Seric 				bslashmode = TRUE;
3938078Seric 				c = NOCHAR;
39456678Seric 				continue;
3953149Seric 			}
39656678Seric 			else if (state == QST)
3978514Seric 			{
3988514Seric 				/* do nothing, just avoid next clauses */
3998514Seric 			}
4008078Seric 			else if (c == '(')
4014100Seric 			{
4028078Seric 				cmntcnt++;
4038078Seric 				c = NOCHAR;
4044100Seric 			}
4058078Seric 			else if (c == ')')
4063149Seric 			{
4078078Seric 				if (cmntcnt <= 0)
4083149Seric 				{
40958151Seric 					usrerr("553 Unbalanced ')'");
41058333Seric 					if (delimptr != NULL)
41158333Seric 						*delimptr = p;
4128078Seric 					return (NULL);
4133149Seric 				}
4148078Seric 				else
4158078Seric 					cmntcnt--;
4168078Seric 			}
4178078Seric 			else if (cmntcnt > 0)
4188078Seric 				c = NOCHAR;
4198423Seric 			else if (c == '<')
4208423Seric 				anglecnt++;
4218423Seric 			else if (c == '>')
4228423Seric 			{
4238423Seric 				if (anglecnt <= 0)
4248423Seric 				{
42558151Seric 					usrerr("553 Unbalanced '>'");
42658333Seric 					if (delimptr != NULL)
42758333Seric 						*delimptr = p;
4288423Seric 					return (NULL);
4298423Seric 				}
4308423Seric 				anglecnt--;
4318423Seric 			}
43258050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
43311423Seric 				c = ' ';
4343149Seric 
4358078Seric 			if (c == NOCHAR)
4368078Seric 				continue;
4373149Seric 
4388078Seric 			/* see if this is end of input */
43911405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4403149Seric 				break;
4413149Seric 
4428078Seric 			newstate = StateTab[state][toktype(c)];
4438078Seric 			if (tTd(22, 101))
4448078Seric 				printf("ns=%02o\n", newstate);
4458078Seric 			state = newstate & TYPE;
4468078Seric 			if (bitset(M, newstate))
4478078Seric 				c = NOCHAR;
4488078Seric 			if (bitset(B, newstate))
4494228Seric 				break;
450297Seric 		}
4513149Seric 
4523149Seric 		/* new token */
4538078Seric 		if (tok != q)
4541378Seric 		{
4558078Seric 			*q++ = '\0';
4568078Seric 			if (tTd(22, 36))
457297Seric 			{
4588078Seric 				printf("tok=");
4598078Seric 				xputs(tok);
46023109Seric 				(void) putchar('\n');
461297Seric 			}
4628078Seric 			if (avp >= &av[MAXATOM])
463297Seric 			{
46458151Seric 				syserr("553 prescan: too many tokens");
46558333Seric 				if (delimptr != NULL)
46658333Seric 					*delimptr = p;
4678078Seric 				return (NULL);
468297Seric 			}
4698078Seric 			*avp++ = tok;
470297Seric 		}
4718423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4723149Seric 	*avp = NULL;
47358333Seric 	p--;
47458333Seric 	if (delimptr != NULL)
47558333Seric 		*delimptr = p;
47656764Seric 	if (tTd(22, 12))
47756764Seric 	{
47856764Seric 		printf("prescan==>");
47956764Seric 		printav(av);
48056764Seric 	}
48158546Seric 	if (av[0] == NULL)
48258546Seric 		return (NULL);
48358403Seric 	return (av);
4843149Seric }
4853149Seric /*
4863149Seric **  TOKTYPE -- return token type
4873149Seric **
4883149Seric **	Parameters:
4893149Seric **		c -- the character in question.
4903149Seric **
4913149Seric **	Returns:
4923149Seric **		Its type.
4933149Seric **
4943149Seric **	Side Effects:
4953149Seric **		none.
4963149Seric */
497297Seric 
4983149Seric toktype(c)
49958050Seric 	register int c;
5003149Seric {
5013380Seric 	static char buf[50];
5023382Seric 	static bool firstime = TRUE;
5033380Seric 
5043382Seric 	if (firstime)
5053380Seric 	{
5063382Seric 		firstime = FALSE;
50758050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5087005Seric 		(void) strcat(buf, DELIMCHARS);
5093380Seric 	}
51058050Seric 	c &= 0377;
51156327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5128078Seric 		return (ONE);
5138078Seric 	if (c == '"')
5148078Seric 		return (QST);
51558050Seric 	if ((c & 0340) == 0200)
51658050Seric 		return (OPR);
5174100Seric 	if (!isascii(c))
5188078Seric 		return (ATM);
5198078Seric 	if (isspace(c) || c == ')')
5208078Seric 		return (SPC);
52158050Seric 	if (strchr(buf, c) != NULL)
5228078Seric 		return (OPR);
5238078Seric 	return (ATM);
5243149Seric }
5253149Seric /*
5263149Seric **  REWRITE -- apply rewrite rules to token vector.
5273149Seric **
5284476Seric **	This routine is an ordered production system.  Each rewrite
5294476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5304476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5314476Seric **
5324476Seric **	For each rewrite rule, 'avp' points the address vector we
5334476Seric **	are trying to match against, and 'pvp' points to the pattern.
5348058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5359585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5369585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5374476Seric **
5384476Seric **	When a match between avp & pvp does not match, we try to
5399585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5404476Seric **	we must also back out the match in mvp.  If we reach a
5418058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5428058Seric **	over again.
5434476Seric **
5444476Seric **	When we finally match, we rewrite the address vector
5454476Seric **	and try over again.
5464476Seric **
5473149Seric **	Parameters:
5483149Seric **		pvp -- pointer to token vector.
5493149Seric **
5503149Seric **	Returns:
5513149Seric **		none.
5523149Seric **
5533149Seric **	Side Effects:
5543149Seric **		pvp is modified.
5553149Seric */
5562091Seric 
5573149Seric struct match
5583149Seric {
5594468Seric 	char	**first;	/* first token matched */
5604468Seric 	char	**last;		/* last token matched */
56158825Seric 	char	**pattern;	/* pointer to pattern */
5623149Seric };
5633149Seric 
5644468Seric # define MAXMATCH	9	/* max params per rewrite */
5653149Seric 
5663149Seric 
5674070Seric rewrite(pvp, ruleset)
5683149Seric 	char **pvp;
5694070Seric 	int ruleset;
5703149Seric {
5713149Seric 	register char *ap;		/* address pointer */
5723149Seric 	register char *rp;		/* rewrite pointer */
5733149Seric 	register char **avp;		/* address vector pointer */
5743149Seric 	register char **rvp;		/* rewrite vector pointer */
5758058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5768058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
57758866Seric 	int ruleno;			/* current rule number */
57856678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5793149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5803149Seric 
5819279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5823149Seric 	{
5838959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
58456678Seric 		printav(pvp);
5853149Seric 	}
58656678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
58756326Seric 	{
58858151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
58956326Seric 		return;
59056326Seric 	}
59156678Seric 	if (pvp == NULL)
59256678Seric 		return;
59356326Seric 
5943149Seric 	/*
59556678Seric 	**  Run through the list of rewrite rules, applying
59656678Seric 	**	any that match.
5973149Seric 	*/
5983149Seric 
59958866Seric 	ruleno = 1;
6004070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6013149Seric 	{
60256678Seric 		int loopcount = 0;
60356678Seric 
6047675Seric 		if (tTd(21, 12))
605297Seric 		{
6068069Seric 			printf("-----trying rule:");
60756678Seric 			printav(rwr->r_lhs);
6083149Seric 		}
6093149Seric 
6103149Seric 		/* try to match on this rule */
6114468Seric 		mlp = mlist;
6128058Seric 		rvp = rwr->r_lhs;
6138058Seric 		avp = pvp;
61458866Seric 		if (++loopcount > 100)
6153149Seric 		{
61658866Seric 			syserr("554 Infinite loop in ruleset %d, rule %d",
61758866Seric 				ruleset, ruleno);
61858866Seric 			if (tTd(21, 1))
61952637Seric 			{
62056678Seric 				printf("workspace: ");
62156678Seric 				printav(pvp);
62252637Seric 			}
62358866Seric 			break;
62458866Seric 		}
62558866Seric 
62658866Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
62758866Seric 		{
6283149Seric 			rp = *rvp;
6298058Seric 			if (tTd(21, 35))
6308058Seric 			{
63158825Seric 				printf("ADVANCE rp=");
63257531Seric 				xputs(rp);
63357532Seric 				printf(", ap=");
6348058Seric 				xputs(ap);
6358069Seric 				printf("\n");
6368058Seric 			}
63756678Seric 			if (rp == NULL)
63856326Seric 			{
6393149Seric 				/* end-of-pattern before end-of-address */
6408058Seric 				goto backup;
64156678Seric 			}
64258173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
64358827Seric 			    (*rp & 0377) != MATCHZERO)
64456326Seric 			{
64558825Seric 				/* end-of-input with patterns left */
64658825Seric 				goto backup;
647297Seric 			}
64856326Seric 
64958050Seric 			switch (*rp & 0377)
6508058Seric 			{
65156678Seric 				register STAB *s;
65258814Seric 				char buf[MAXLINE];
65356326Seric 
65456678Seric 			  case MATCHCLASS:
65558825Seric 				/* match any phrase in a class */
65658825Seric 				mlp->pattern = rvp;
65758814Seric 				mlp->first = avp;
65858814Seric 	extendclass:
65958825Seric 				ap = *avp;
66058825Seric 				if (ap == NULL)
66158814Seric 					goto backup;
66258814Seric 				mlp->last = avp++;
66358814Seric 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
66458814Seric 				s = stab(buf, ST_CLASS, ST_FIND);
66556678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
66656326Seric 				{
66758825Seric 					if (tTd(21, 36))
66858825Seric 					{
66958825Seric 						printf("EXTEND  rp=");
67058825Seric 						xputs(rp);
67158825Seric 						printf(", ap=");
67258825Seric 						xputs(ap);
67358825Seric 						printf("\n");
67458825Seric 					}
67558825Seric 					goto extendclass;
67656326Seric 				}
67758825Seric 				if (tTd(21, 36))
67858825Seric 					printf("CLMATCH\n");
67958814Seric 				mlp++;
68058814Seric 				break;
6814060Seric 
68258825Seric 			  case MATCHNCLASS:
68358825Seric 				/* match any token not in a class */
68458825Seric 				s = stab(ap, ST_CLASS, ST_FIND);
68558825Seric 				if (s != NULL && bitnset(rp[1], s->s_class))
68658825Seric 					goto backup;
68758825Seric 
68858825Seric 				/* fall through */
68958825Seric 
69056678Seric 			  case MATCHONE:
69156678Seric 			  case MATCHANY:
69256678Seric 				/* match exactly one token */
69358825Seric 				mlp->pattern = rvp;
69456678Seric 				mlp->first = avp;
69556678Seric 				mlp->last = avp++;
6968058Seric 				mlp++;
69756678Seric 				break;
6988058Seric 
69956678Seric 			  case MATCHZANY:
70056678Seric 				/* match zero or more tokens */
70158825Seric 				mlp->pattern = rvp;
70256678Seric 				mlp->first = avp;
70356678Seric 				mlp->last = avp - 1;
70456678Seric 				mlp++;
70556678Seric 				break;
70656326Seric 
70758827Seric 			  case MATCHZERO:
70858173Seric 				/* match zero tokens */
70958173Seric 				break;
71058173Seric 
71156678Seric 			  default:
71256678Seric 				/* must have exact match */
71356678Seric 				if (strcasecmp(rp, ap))
7148058Seric 					goto backup;
7154468Seric 				avp++;
71656678Seric 				break;
7173149Seric 			}
7183149Seric 
71956678Seric 			/* successful match on this token */
7203149Seric 			rvp++;
7213149Seric 			continue;
7223149Seric 
72358825Seric 	  backup:
72456678Seric 			/* match failed -- back up */
72558825Seric 			while (--mlp >= mlist)
7263149Seric 			{
72758825Seric 				rvp = mlp->pattern;
72856678Seric 				rp = *rvp;
72958825Seric 				avp = mlp->last + 1;
73058825Seric 				ap = *avp;
73158825Seric 
73258825Seric 				if (tTd(21, 36))
73358825Seric 				{
73458825Seric 					printf("BACKUP  rp=");
73558825Seric 					xputs(rp);
73658825Seric 					printf(", ap=");
73758825Seric 					xputs(ap);
73858825Seric 					printf("\n");
73958825Seric 				}
74058825Seric 
74158825Seric 				if (ap == NULL)
74258825Seric 				{
74358825Seric 					/* run off the end -- back up again */
74458825Seric 					continue;
74558825Seric 				}
74658050Seric 				if ((*rp & 0377) == MATCHANY ||
74758050Seric 				    (*rp & 0377) == MATCHZANY)
7484468Seric 				{
74956678Seric 					/* extend binding and continue */
75058825Seric 					mlp->last = avp++;
75156678Seric 					rvp++;
75258825Seric 					mlp++;
75356678Seric 					break;
7544468Seric 				}
75558825Seric 				if ((*rp & 0377) == MATCHCLASS)
75656678Seric 				{
75758814Seric 					/* extend binding and try again */
75858825Seric 					mlp->last = avp++;
75958814Seric 					goto extendclass;
76058814Seric 				}
7613149Seric 			}
7623149Seric 
76358825Seric 			if (mlp < mlist)
76456678Seric 			{
76556678Seric 				/* total failure to match */
76656326Seric 				break;
7673149Seric 			}
768297Seric 		}
7693149Seric 
7703149Seric 		/*
77156678Seric 		**  See if we successfully matched
7723149Seric 		*/
7733149Seric 
77458827Seric 		if (mlp < mlist || *rvp != NULL)
7753149Seric 		{
7769374Seric 			if (tTd(21, 10))
7779374Seric 				printf("----- rule fails\n");
7789374Seric 			rwr = rwr->r_next;
77958866Seric 			ruleno++;
7809374Seric 			continue;
7819374Seric 		}
7823149Seric 
7839374Seric 		rvp = rwr->r_rhs;
7849374Seric 		if (tTd(21, 12))
7859374Seric 		{
7869374Seric 			printf("-----rule matches:");
78756678Seric 			printav(rvp);
7889374Seric 		}
7899374Seric 
7909374Seric 		rp = *rvp;
79158050Seric 		if ((*rp & 0377) == CANONUSER)
7929374Seric 		{
7939374Seric 			rvp++;
7949374Seric 			rwr = rwr->r_next;
79558866Seric 			ruleno++;
7969374Seric 		}
79758050Seric 		else if ((*rp & 0377) == CANONHOST)
7989374Seric 		{
7999374Seric 			rvp++;
8009374Seric 			rwr = NULL;
8019374Seric 		}
80258050Seric 		else if ((*rp & 0377) == CANONNET)
8039374Seric 			rwr = NULL;
8049374Seric 
8059374Seric 		/* substitute */
8069374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
8079374Seric 		{
8089374Seric 			register struct match *m;
8099374Seric 			register char **pp;
8109374Seric 
8118058Seric 			rp = *rvp;
81258050Seric 			if ((*rp & 0377) == MATCHREPL)
8138058Seric 			{
81416914Seric 				/* substitute from LHS */
81516914Seric 				m = &mlist[rp[1] - '1'];
81656678Seric 				if (m < mlist || m >= mlp)
8179374Seric 				{
81858151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
81956326Seric 						ruleset, rp[1]);
8209374Seric 					return;
8219374Seric 				}
82216914Seric 				if (tTd(21, 15))
82316914Seric 				{
82416914Seric 					printf("$%c:", rp[1]);
82516914Seric 					pp = m->first;
82656678Seric 					while (pp <= m->last)
82716914Seric 					{
82816914Seric 						printf(" %x=\"", *pp);
82916914Seric 						(void) fflush(stdout);
83016914Seric 						printf("%s\"", *pp++);
83116914Seric 					}
83216914Seric 					printf("\n");
83316914Seric 				}
8349374Seric 				pp = m->first;
83556678Seric 				while (pp <= m->last)
8363149Seric 				{
83716914Seric 					if (avp >= &npvp[MAXATOM])
83856678Seric 					{
83958151Seric 						syserr("554 rewrite: expansion too long");
84056678Seric 						return;
84156678Seric 					}
84216914Seric 					*avp++ = *pp++;
8433149Seric 				}
8443149Seric 			}
84516914Seric 			else
8468226Seric 			{
84716914Seric 				/* vanilla replacement */
8489374Seric 				if (avp >= &npvp[MAXATOM])
84916889Seric 				{
85056678Seric 	toolong:
85158151Seric 					syserr("554 rewrite: expansion too long");
85216889Seric 					return;
85316889Seric 				}
85456678Seric 				*avp++ = rp;
8558226Seric 			}
8569374Seric 		}
8579374Seric 		*avp++ = NULL;
85816914Seric 
85916914Seric 		/*
86056678Seric 		**  Check for any hostname/keyword lookups.
86116914Seric 		*/
86216914Seric 
86316914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
86416914Seric 		{
86556678Seric 			char **hbrvp;
86616914Seric 			char **xpvp;
86716914Seric 			int trsize;
86817473Seric 			char *olddelimchar;
86956678Seric 			char *replac;
87056678Seric 			int endtoken;
87156678Seric 			STAB *map;
87256678Seric 			char *mapname;
87356678Seric 			char **key_rvp;
87456678Seric 			char **arg_rvp;
87556678Seric 			char **default_rvp;
87656678Seric 			char buf[MAXNAME + 1];
87716914Seric 			char *pvpb1[MAXATOM + 1];
87856823Seric 			char *argvect[10];
87917174Seric 			char pvpbuf[PSBUFSIZE];
88016914Seric 
88158050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
88258050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
88316914Seric 				continue;
88416914Seric 
88516914Seric 			/*
88656678Seric 			**  Got a hostname/keyword lookup.
88716914Seric 			**
88816914Seric 			**	This could be optimized fairly easily.
88916914Seric 			*/
89016914Seric 
89116914Seric 			hbrvp = rvp;
89258050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
89356327Seric 			{
89456678Seric 				endtoken = HOSTEND;
89556678Seric 				mapname = "host";
89656327Seric 			}
89756326Seric 			else
89856327Seric 			{
89956678Seric 				endtoken = LOOKUPEND;
90056678Seric 				mapname = *++rvp;
90156327Seric 			}
90256678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
90356678Seric 			if (map == NULL)
90458151Seric 				syserr("554 rewrite: map %s not found", mapname);
90556678Seric 
90656678Seric 			/* extract the match part */
90756678Seric 			key_rvp = ++rvp;
90856823Seric 			default_rvp = NULL;
90956823Seric 			arg_rvp = argvect;
91056823Seric 			xpvp = NULL;
91156823Seric 			replac = pvpbuf;
91258050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
91353654Seric 			{
91458050Seric 				int nodetype = **rvp & 0377;
91556823Seric 
91656823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
91756678Seric 				{
91856823Seric 					rvp++;
91956823Seric 					continue;
92056823Seric 				}
92156823Seric 
92256823Seric 				*rvp++ = NULL;
92356823Seric 
92456823Seric 				if (xpvp != NULL)
92556823Seric 				{
92658814Seric 					cataddr(xpvp, NULL, replac,
92758082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
92858082Seric 						'\0');
92956823Seric 					*++arg_rvp = replac;
93056823Seric 					replac += strlen(replac) + 1;
93156823Seric 					xpvp = NULL;
93256823Seric 				}
93356823Seric 				switch (nodetype)
93456823Seric 				{
93556678Seric 				  case CANONHOST:
93656823Seric 					xpvp = rvp;
93756678Seric 					break;
93856678Seric 
93956678Seric 				  case CANONUSER:
94056678Seric 					default_rvp = rvp;
94156678Seric 					break;
94256678Seric 				}
94353654Seric 			}
94416914Seric 			if (*rvp != NULL)
94516914Seric 				*rvp++ = NULL;
94656823Seric 			if (xpvp != NULL)
94756823Seric 			{
94858814Seric 				cataddr(xpvp, NULL, replac,
94958082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
95058082Seric 					'\0');
95156823Seric 				*++arg_rvp = replac;
95256823Seric 			}
95356823Seric 			*++arg_rvp = NULL;
95416914Seric 
95516914Seric 			/* save the remainder of the input string */
95616914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
95716914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
95816914Seric 
95956678Seric 			/* look it up */
96058814Seric 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
96156823Seric 			argvect[0] = buf;
96256678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
96356836Seric 			{
96456836Seric 				int bsize = sizeof buf - 1;
96556836Seric 
96656836Seric 				if (map->s_map.map_app != NULL)
96756836Seric 					bsize -= strlen(map->s_map.map_app);
96858796Seric 				if (tTd(60, 1))
96958796Seric 					printf("map_lookup(%s, %s) => ",
97058796Seric 						mapname, buf);
97156836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
97256823Seric 						buf, sizeof buf - 1, argvect);
97356836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
97456836Seric 					strcat(replac, map->s_map.map_app);
97558796Seric 				if (tTd(60, 1))
97658796Seric 					printf("%s\n", replac ? replac : "NOT FOUND");
97756836Seric 			}
97853654Seric 			else
97956678Seric 				replac = NULL;
98056678Seric 
98156678Seric 			/* if no replacement, use default */
98256823Seric 			if (replac == NULL && default_rvp != NULL)
98356823Seric 			{
98456823Seric 				char buf2[sizeof buf];
98556823Seric 
98656823Seric 				/* rewrite the default with % translations */
98758814Seric 				cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0');
98856823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
98956823Seric 					argvect);
99056823Seric 				replac = buf;
99156823Seric 			}
99256823Seric 
99356678Seric 			if (replac == NULL)
99451317Seric 			{
99556823Seric 				xpvp = key_rvp;
99653654Seric 			}
99756678Seric 			else
99853654Seric 			{
99956678Seric 				/* scan the new replacement */
100058333Seric 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
100153654Seric 				if (xpvp == NULL)
100251317Seric 				{
100358403Seric 					/* prescan already printed error */
100456678Seric 					return;
100556678Seric 				}
100651317Seric 			}
100751317Seric 
100816914Seric 			/* append it to the token list */
100956678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
101056678Seric 			{
101117174Seric 				*avp++ = newstr(*xpvp);
101216920Seric 				if (avp >= &npvp[MAXATOM])
101316914Seric 					goto toolong;
101417174Seric 			}
101516914Seric 
101616914Seric 			/* restore the old trailing information */
101756678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
101816920Seric 				if (avp >= &npvp[MAXATOM])
101916914Seric 					goto toolong;
102017174Seric 
102156678Seric 			break;
102216914Seric 		}
102316914Seric 
102416914Seric 		/*
102516914Seric 		**  Check for subroutine calls.
102616914Seric 		*/
102716914Seric 
102858050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
102956678Seric 		{
103056678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
103156678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
103256678Seric 			if (tTd(21, 3))
103356678Seric 				printf("-----callsubr %s\n", npvp[1]);
103456678Seric 			rewrite(pvp, atoi(npvp[1]));
103556678Seric 		}
103656678Seric 		else
103756678Seric 		{
103817348Seric 			bcopy((char *) npvp, (char *) pvp,
103916900Seric 				(int) (avp - npvp) * sizeof *avp);
104056678Seric 		}
10419374Seric 		if (tTd(21, 4))
10429374Seric 		{
10439374Seric 			printf("rewritten as:");
104456678Seric 			printav(pvp);
10459374Seric 		}
1046297Seric 	}
10478069Seric 
10489279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
10498069Seric 	{
10508959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
105156678Seric 		printav(pvp);
10528069Seric 	}
10533149Seric }
10543149Seric /*
10553149Seric **  BUILDADDR -- build address from token vector.
10563149Seric **
10573149Seric **	Parameters:
10583149Seric **		tv -- token vector.
10593149Seric **		a -- pointer to address descriptor to fill.
10603149Seric **			If NULL, one will be allocated.
1061*58966Seric **		e -- the current envelope.
10623149Seric **
10633149Seric **	Returns:
10644279Seric **		NULL if there was an error.
10654279Seric **		'a' otherwise.
10663149Seric **
10673149Seric **	Side Effects:
10683149Seric **		fills in 'a'
10693149Seric */
10703149Seric 
107157249Seric struct errcodes
107257249Seric {
107357249Seric 	char	*ec_name;		/* name of error code */
107457249Seric 	int	ec_code;		/* numeric code */
107557249Seric } ErrorCodes[] =
107657249Seric {
107757249Seric 	"usage",	EX_USAGE,
107857249Seric 	"nouser",	EX_NOUSER,
107957249Seric 	"nohost",	EX_NOHOST,
108057249Seric 	"unavailable",	EX_UNAVAILABLE,
108157249Seric 	"software",	EX_SOFTWARE,
108257249Seric 	"tempfail",	EX_TEMPFAIL,
108357249Seric 	"protocol",	EX_PROTOCOL,
108457249Seric #ifdef EX_CONFIG
108557249Seric 	"config",	EX_CONFIG,
108657249Seric #endif
108757249Seric 	NULL,		EX_UNAVAILABLE,
108857249Seric };
108957249Seric 
109056678Seric ADDRESS *
1091*58966Seric buildaddr(tv, a, e)
10923149Seric 	register char **tv;
10933149Seric 	register ADDRESS *a;
1094*58966Seric 	register ENVELOPE *e;
10953149Seric {
10963149Seric 	struct mailer **mp;
10973149Seric 	register struct mailer *m;
109858008Seric 	char *bp;
109958008Seric 	int spaceleft;
110057402Seric 	static char buf[MAXNAME];
11013149Seric 
11023149Seric 	if (a == NULL)
11033149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
110416889Seric 	bzero((char *) a, sizeof *a);
11053149Seric 
11063149Seric 	/* figure out what net/mailer to use */
110758050Seric 	if ((**tv & 0377) != CANONNET)
11084279Seric 	{
110958151Seric 		syserr("554 buildaddr: no net");
11104279Seric 		return (NULL);
11114279Seric 	}
11123149Seric 	tv++;
111358680Seric 	if (strcasecmp(*tv, "error") == 0)
11144279Seric 	{
111558050Seric 		if ((**++tv & 0377) == CANONHOST)
111610183Seric 		{
111757249Seric 			register struct errcodes *ep;
111857249Seric 
111958050Seric 			if (isascii(**++tv) && isdigit(**tv))
112057249Seric 			{
112157249Seric 				setstat(atoi(*tv));
112257249Seric 			}
112357249Seric 			else
112457249Seric 			{
112557249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
112657249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
112757249Seric 						break;
112857249Seric 				setstat(ep->ec_code);
112957249Seric 			}
113010183Seric 			tv++;
113110183Seric 		}
113258050Seric 		if ((**tv & 0377) != CANONUSER)
113358151Seric 			syserr("554 buildaddr: error: no user");
113458814Seric 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
113558082Seric 		stripquotes(buf);
11364279Seric 		usrerr(buf);
1137*58966Seric 		if (e->e_message == NULL)
1138*58966Seric 			e->e_message = newstr(buf);
11394279Seric 		return (NULL);
11404279Seric 	}
114157402Seric 
11424598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
11433149Seric 	{
114458680Seric 		if (strcasecmp(m->m_name, *tv) == 0)
11453149Seric 			break;
11463149Seric 	}
11473149Seric 	if (m == NULL)
11484279Seric 	{
114958151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
11504279Seric 		return (NULL);
11514279Seric 	}
11524598Seric 	a->q_mailer = m;
11533149Seric 
11543149Seric 	/* figure out what host (if any) */
115556678Seric 	tv++;
115658509Seric 	if ((**tv & 0377) == CANONHOST)
11573149Seric 	{
115858008Seric 		bp = buf;
115958008Seric 		spaceleft = sizeof buf - 1;
116058050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
116158008Seric 		{
116258008Seric 			int i = strlen(*tv);
116358008Seric 
116458008Seric 			if (i > spaceleft)
116558008Seric 			{
116658008Seric 				/* out of space for this address */
116758008Seric 				if (spaceleft >= 0)
116858151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
116958008Seric 						buf);
117058008Seric 				i = spaceleft;
117158008Seric 				spaceleft = 0;
117258008Seric 			}
117358008Seric 			if (i <= 0)
117458008Seric 				continue;
117558008Seric 			bcopy(*tv, bp, i);
117658008Seric 			bp += i;
117758008Seric 			spaceleft -= i;
117858008Seric 		}
117958010Seric 		*bp = '\0';
11805704Seric 		a->q_host = newstr(buf);
11813149Seric 	}
118257249Seric 	else
118358509Seric 	{
118458509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
118558509Seric 		{
118658509Seric 			syserr("554 buildaddr: no host");
118758509Seric 			return (NULL);
118858509Seric 		}
118957249Seric 		a->q_host = NULL;
119058509Seric 	}
11913149Seric 
11923149Seric 	/* figure out the user */
119358050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
11944279Seric 	{
119558151Seric 		syserr("554 buildaddr: no user");
11964279Seric 		return (NULL);
11974279Seric 	}
119857402Seric 	tv++;
119951317Seric 
120057402Seric 	/* do special mapping for local mailer */
120157402Seric 	if (m == LocalMailer && *tv != NULL)
120257402Seric 	{
120357454Seric 		register char *p = *tv;
120457454Seric 
120557454Seric 		if (*p == '"')
120657454Seric 			p++;
120757454Seric 		if (*p == '|')
120857402Seric 			a->q_mailer = m = ProgMailer;
120957454Seric 		else if (*p == '/')
121057402Seric 			a->q_mailer = m = FileMailer;
121157454Seric 		else if (*p == ':')
121257402Seric 		{
121357402Seric 			/* may be :include: */
121458814Seric 			cataddr(tv, NULL, buf, sizeof buf, '\0');
121558008Seric 			stripquotes(buf);
121658008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
121758008Seric 			{
121858008Seric 				/* if :include:, don't need further rewriting */
121957402Seric 				a->q_mailer = m = InclMailer;
122058008Seric 				a->q_user = &buf[9];
122158008Seric 				return (a);
122258008Seric 			}
122357402Seric 		}
122457402Seric 	}
122557402Seric 
122658008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
122758008Seric 	{
122858008Seric 		tv++;
122958008Seric 		a->q_flags |= QNOTREMOTE;
123058008Seric 	}
123158008Seric 
123258673Seric 	/* do cleanup of final address */
123319040Seric 	rewrite(tv, 4);
123419040Seric 
123519040Seric 	/* save the result for the command line/RCPT argument */
123658814Seric 	cataddr(tv, NULL, buf, sizeof buf, '\0');
12373149Seric 	a->q_user = buf;
12383149Seric 
123958670Seric 	/*
124058670Seric 	**  Do mapping to lower case as requested by mailer
124158670Seric 	*/
124258670Seric 
124358670Seric 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
124458670Seric 		makelower(a->q_host);
124558670Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
124658670Seric 		makelower(a->q_user);
124758670Seric 
12483149Seric 	return (a);
12493149Seric }
12503188Seric /*
12514228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
12524228Seric **
12534228Seric **	Parameters:
12544228Seric **		pvp -- parameter vector to rebuild.
125558814Seric **		evp -- last parameter to include.  Can be NULL to
125658814Seric **			use entire pvp.
12574228Seric **		buf -- buffer to build the string into.
12584228Seric **		sz -- size of buf.
125958082Seric **		spacesub -- the space separator character; if null,
126058082Seric **			use SpaceSub.
12614228Seric **
12624228Seric **	Returns:
12634228Seric **		none.
12644228Seric **
12654228Seric **	Side Effects:
12664228Seric **		Destroys buf.
12674228Seric */
12684228Seric 
126958814Seric cataddr(pvp, evp, buf, sz, spacesub)
12704228Seric 	char **pvp;
127158814Seric 	char **evp;
12724228Seric 	char *buf;
12734228Seric 	register int sz;
127458082Seric 	char spacesub;
12754228Seric {
12764228Seric 	bool oatomtok = FALSE;
127756678Seric 	bool natomtok = FALSE;
12784228Seric 	register int i;
12794228Seric 	register char *p;
12804228Seric 
128158082Seric 	if (spacesub == '\0')
128258082Seric 		spacesub = SpaceSub;
128358082Seric 
12848423Seric 	if (pvp == NULL)
12858423Seric 	{
128623109Seric 		(void) strcpy(buf, "");
12878423Seric 		return;
12888423Seric 	}
12894228Seric 	p = buf;
129011156Seric 	sz -= 2;
12914228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
12924228Seric 	{
12938078Seric 		natomtok = (toktype(**pvp) == ATM);
12944228Seric 		if (oatomtok && natomtok)
129558082Seric 			*p++ = spacesub;
12964228Seric 		(void) strcpy(p, *pvp);
12974228Seric 		oatomtok = natomtok;
12984228Seric 		p += i;
129911156Seric 		sz -= i + 1;
130058814Seric 		if (pvp++ == evp)
130158814Seric 			break;
13024228Seric 	}
13034228Seric 	*p = '\0';
13044228Seric }
13054228Seric /*
13063188Seric **  SAMEADDR -- Determine if two addresses are the same
13073188Seric **
13083188Seric **	This is not just a straight comparison -- if the mailer doesn't
13093188Seric **	care about the host we just ignore it, etc.
13103188Seric **
13113188Seric **	Parameters:
13123188Seric **		a, b -- pointers to the internal forms to compare.
13133188Seric **
13143188Seric **	Returns:
13153188Seric **		TRUE -- they represent the same mailbox.
13163188Seric **		FALSE -- they don't.
13173188Seric **
13183188Seric **	Side Effects:
13193188Seric **		none.
13203188Seric */
13213188Seric 
13223188Seric bool
13239374Seric sameaddr(a, b)
13243188Seric 	register ADDRESS *a;
13253188Seric 	register ADDRESS *b;
13263188Seric {
13273188Seric 	/* if they don't have the same mailer, forget it */
13283188Seric 	if (a->q_mailer != b->q_mailer)
13293188Seric 		return (FALSE);
13303188Seric 
13313188Seric 	/* if the user isn't the same, we can drop out */
133256678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
13333188Seric 		return (FALSE);
13343188Seric 
133558438Seric 	/* if we have good uids for both but the differ, these are different */
133658438Seric 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
133758438Seric 		return (FALSE);
133858438Seric 
133958509Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
134058509Seric 	if (a->q_host == b->q_host)
134158509Seric 	{
134258509Seric 		/* probably both null pointers */
13433188Seric 		return (TRUE);
134458509Seric 	}
13453188Seric 	if (a->q_host == NULL || b->q_host == NULL)
134658509Seric 	{
134758509Seric 		/* only one is a null pointer */
13483188Seric 		return (FALSE);
134958509Seric 	}
135056678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
13513188Seric 		return (FALSE);
13523188Seric 
13533188Seric 	return (TRUE);
13543188Seric }
13553234Seric /*
13563234Seric **  PRINTADDR -- print address (for debugging)
13573234Seric **
13583234Seric **	Parameters:
13593234Seric **		a -- the address to print
13603234Seric **		follow -- follow the q_next chain.
13613234Seric **
13623234Seric **	Returns:
13633234Seric **		none.
13643234Seric **
13653234Seric **	Side Effects:
13663234Seric **		none.
13673234Seric */
13683234Seric 
13693234Seric printaddr(a, follow)
13703234Seric 	register ADDRESS *a;
13713234Seric 	bool follow;
13723234Seric {
13735001Seric 	bool first = TRUE;
137457731Seric 	register MAILER *m;
137557731Seric 	MAILER pseudomailer;
13765001Seric 
13773234Seric 	while (a != NULL)
13783234Seric 	{
13795001Seric 		first = FALSE;
13804443Seric 		printf("%x=", a);
13814085Seric 		(void) fflush(stdout);
138257731Seric 
138357731Seric 		/* find the mailer -- carefully */
138457731Seric 		m = a->q_mailer;
138557731Seric 		if (m == NULL)
138657731Seric 		{
138757731Seric 			m = &pseudomailer;
138857731Seric 			m->m_mno = -1;
138957731Seric 			m->m_name = "NULL";
139057731Seric 		}
139157731Seric 
139258680Seric 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
139357731Seric 		       a->q_paddr, m->m_mno, m->m_name,
139440973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
13958181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
13968181Seric 		       a->q_alias);
13978181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
13988181Seric 		       a->q_fullname);
13994996Seric 
14003234Seric 		if (!follow)
14013234Seric 			return;
14024996Seric 		a = a->q_next;
14033234Seric 	}
14045001Seric 	if (first)
14054443Seric 		printf("[NULL]\n");
14063234Seric }
14074317Seric 
14087682Seric /*
14097682Seric **  REMOTENAME -- return the name relative to the current mailer
14107682Seric **
14117682Seric **	Parameters:
14127682Seric **		name -- the name to translate.
14138069Seric **		m -- the mailer that we want to do rewriting relative
14148069Seric **			to.
14158069Seric **		senderaddress -- if set, uses the sender rewriting rules
14168069Seric **			rather than the recipient rewriting rules.
141758020Seric **		header -- set if this address is in the header, rather
141858020Seric **			than an envelope header.
141910310Seric **		canonical -- if set, strip out any comment information,
142010310Seric **			etc.
142158509Seric **		adddomain -- if set, OK to do domain extension.
142258020Seric **		e -- the current envelope.
14237682Seric **
14247682Seric **	Returns:
14257682Seric **		the text string representing this address relative to
14267682Seric **			the receiving mailer.
14277682Seric **
14287682Seric **	Side Effects:
14297682Seric **		none.
14307682Seric **
14317682Seric **	Warnings:
14327682Seric **		The text string returned is tucked away locally;
14337682Seric **			copy it if you intend to save it.
14347682Seric */
14357682Seric 
14367682Seric char *
143758509Seric remotename(name, m, senderaddress, header, canonical, adddomain, e)
14387682Seric 	char *name;
143956678Seric 	struct mailer *m;
14408069Seric 	bool senderaddress;
144158020Seric 	bool header;
144210310Seric 	bool canonical;
144358509Seric 	bool adddomain;
144456678Seric 	register ENVELOPE *e;
14457682Seric {
14468069Seric 	register char **pvp;
14478069Seric 	char *fancy;
144856678Seric 	extern char *macvalue();
144956678Seric 	char *oldg = macvalue('g', e);
145058020Seric 	int rwset;
14517682Seric 	static char buf[MAXNAME];
14527682Seric 	char lbuf[MAXNAME];
145316914Seric 	char pvpbuf[PSBUFSIZE];
145456678Seric 	extern char **prescan();
145556678Seric 	extern char *crackaddr();
14567682Seric 
14577755Seric 	if (tTd(12, 1))
14587755Seric 		printf("remotename(%s)\n", name);
14597755Seric 
146010177Seric 	/* don't do anything if we are tagging it as special */
146158020Seric 	if (senderaddress)
146258020Seric 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
146358020Seric 	else
146458020Seric 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
146558020Seric 	if (rwset < 0)
146610177Seric 		return (name);
146710177Seric 
14687682Seric 	/*
14698181Seric 	**  Do a heuristic crack of this name to extract any comment info.
14708181Seric 	**	This will leave the name as a comment and a $g macro.
14717889Seric 	*/
14727889Seric 
147358036Seric 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
147458050Seric 		fancy = "\201g";
147510310Seric 	else
147610310Seric 		fancy = crackaddr(name);
14777889Seric 
14788181Seric 	/*
14798181Seric 	**  Turn the name into canonical form.
14808181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
14818181Seric 	**	If this only resolves to "user", and the "C" flag is
14828181Seric 	**	specified in the sending mailer, then the sender's
14838181Seric 	**	domain will be appended.
14848181Seric 	*/
14858181Seric 
148658333Seric 	pvp = prescan(name, '\0', pvpbuf, NULL);
14877889Seric 	if (pvp == NULL)
14887889Seric 		return (name);
14898181Seric 	rewrite(pvp, 3);
149058509Seric 	if (adddomain && e->e_fromdomain != NULL)
14918181Seric 	{
14928181Seric 		/* append from domain to this address */
14938181Seric 		register char **pxp = pvp;
14948181Seric 
14959594Seric 		/* see if there is an "@domain" in the current name */
14968181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
14978181Seric 			pxp++;
14988181Seric 		if (*pxp == NULL)
14998181Seric 		{
15009594Seric 			/* no.... append the "@domain" from the sender */
150156678Seric 			register char **qxq = e->e_fromdomain;
15028181Seric 
15039594Seric 			while ((*pxp++ = *qxq++) != NULL)
15049594Seric 				continue;
150511726Seric 			rewrite(pvp, 3);
15068181Seric 		}
15078181Seric 	}
15088181Seric 
15098181Seric 	/*
15108959Seric 	**  Do more specific rewriting.
151156678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
151256678Seric 	**		a sender address or not.
15138181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
15148181Seric 	*/
15158181Seric 
15168069Seric 	if (senderaddress)
151756327Seric 		rewrite(pvp, 1);
15188069Seric 	else
151956327Seric 		rewrite(pvp, 2);
152058020Seric 	if (rwset > 0)
152158020Seric 		rewrite(pvp, rwset);
15227682Seric 
15238181Seric 	/*
15248959Seric 	**  Do any final sanitation the address may require.
15258959Seric 	**	This will normally be used to turn internal forms
15268959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
15278959Seric 	**	may be used as a default to the above rules.
15288959Seric 	*/
15298959Seric 
15308959Seric 	rewrite(pvp, 4);
15318959Seric 
15328959Seric 	/*
15338181Seric 	**  Now restore the comment information we had at the beginning.
15348181Seric 	*/
15358181Seric 
153658825Seric 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
153756678Seric 	define('g', lbuf, e);
153856678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
153956678Seric 	define('g', oldg, e);
15407682Seric 
15417682Seric 	if (tTd(12, 1))
15427755Seric 		printf("remotename => `%s'\n", buf);
15437682Seric 	return (buf);
15447682Seric }
154551317Seric /*
154656678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
154751317Seric **
154851317Seric **	Parameters:
154956678Seric **		a -- the address to map (but just the user name part).
155056678Seric **		sendq -- the sendq in which to install any replacement
155156678Seric **			addresses.
155251317Seric **
155351317Seric **	Returns:
155451317Seric **		none.
155551317Seric */
155651317Seric 
155756678Seric maplocaluser(a, sendq, e)
155856678Seric 	register ADDRESS *a;
155956678Seric 	ADDRESS **sendq;
156056678Seric 	ENVELOPE *e;
156151317Seric {
156256678Seric 	register char **pvp;
156356678Seric 	register ADDRESS *a1 = NULL;
156458333Seric 	auto char *delimptr;
156556678Seric 	char pvpbuf[PSBUFSIZE];
156651317Seric 
156756678Seric 	if (tTd(29, 1))
156856678Seric 	{
156956678Seric 		printf("maplocaluser: ");
157056678Seric 		printaddr(a, FALSE);
157156678Seric 	}
157258333Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
157356678Seric 	if (pvp == NULL)
157456678Seric 		return;
157551317Seric 
157656678Seric 	rewrite(pvp, 5);
157758050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
157856678Seric 		return;
157951317Seric 
158056678Seric 	/* if non-null, mailer destination specified -- has it changed? */
1581*58966Seric 	a1 = buildaddr(pvp, NULL, e);
158256678Seric 	if (a1 == NULL || sameaddr(a, a1))
158356678Seric 		return;
158451317Seric 
158556678Seric 	/* mark old address as dead; insert new address */
158656678Seric 	a->q_flags |= QDONTSEND;
158757731Seric 	if (tTd(29, 5))
158857731Seric 	{
158957731Seric 		printf("maplocaluser: QDONTSEND ");
159057731Seric 		printaddr(a, FALSE);
159157731Seric 	}
159256678Seric 	a1->q_alias = a;
159358333Seric 	allocaddr(a1, 1, NULL, delimptr);
159456678Seric 	(void) recipient(a1, sendq, e);
159551317Seric }
159658802Seric /*
159758802Seric **  DEQUOTE_INIT -- initialize dequote map
159858802Seric **
159958802Seric **	This is a no-op.
160058802Seric **
160158802Seric **	Parameters:
160258802Seric **		map -- the internal map structure.
160358802Seric **		mapname -- the name of the mapl.
160458802Seric **		args -- arguments.
160558802Seric **
160658802Seric **	Returns:
160758802Seric **		TRUE.
160858802Seric */
160958802Seric 
161058802Seric bool
161158802Seric dequote_init(map, mapname, args)
161258802Seric 	MAP *map;
161358802Seric 	char *mapname;
161458802Seric 	char *args;
161558802Seric {
161658805Seric 	register char *p = args;
161758805Seric 
161858805Seric 	for (;;)
161958805Seric 	{
162058805Seric 		while (isascii(*p) && isspace(*p))
162158805Seric 			p++;
162258805Seric 		if (*p != '-')
162358805Seric 			break;
162458805Seric 		switch (*++p)
162558805Seric 		{
162658805Seric 		  case 'a':
162758805Seric 			map->map_app = ++p;
162858805Seric 			break;
162958805Seric 		}
163058805Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
163158805Seric 			p++;
163258805Seric 		if (*p != '\0')
163358805Seric 			*p = '\0';
163458805Seric 	}
163558805Seric 	if (map->map_app != NULL)
163658805Seric 		map->map_app = newstr(map->map_app);
163758805Seric 
163858802Seric 	return TRUE;
163958802Seric }
164058802Seric /*
164158802Seric **  DEQUOTE_MAP -- unquote an address
164258802Seric **
164358802Seric **	Parameters:
164458802Seric **		map -- the internal map structure (ignored).
164558802Seric **		buf -- the buffer to dequote.
164658802Seric **		bufsiz -- the size of that buffer.
164758802Seric **		av -- arguments (ignored).
164858802Seric **
164958802Seric **	Returns:
165058802Seric **		NULL -- if there were no quotes, or if the resulting
165158802Seric **			unquoted buffer would not be acceptable to prescan.
165258802Seric **		else -- The dequoted buffer.
165358802Seric */
165458802Seric 
165558802Seric char *
165658802Seric dequote_map(map, buf, bufsiz, av)
165758802Seric 	MAP *map;
165858802Seric 	char buf[];
165958802Seric 	int bufsiz;
166058802Seric 	char **av;
166158802Seric {
166258802Seric 	register char *p;
166358802Seric 	register char *q;
166458802Seric 	register char c;
166558802Seric 	int anglecnt;
166658802Seric 	int cmntcnt;
166758802Seric 	int quotecnt;
166858802Seric 	bool quotemode;
166958802Seric 	bool bslashmode;
167058802Seric 
167158802Seric 	anglecnt = 0;
167258802Seric 	cmntcnt = 0;
167358802Seric 	quotecnt = 0;
167458802Seric 	quotemode = FALSE;
167558802Seric 	bslashmode = FALSE;
167658802Seric 
167758802Seric 	for (p = q = buf; (c = *p++) != '\0'; )
167858802Seric 	{
167958802Seric 		if (bslashmode)
168058802Seric 		{
168158802Seric 			bslashmode = FALSE;
168258802Seric 			*q++ = c;
168358802Seric 			continue;
168458802Seric 		}
168558802Seric 
168658802Seric 		switch (c)
168758802Seric 		{
168858802Seric 		  case '\\':
168958802Seric 			bslashmode = TRUE;
169058802Seric 			break;
169158802Seric 
169258802Seric 		  case '(':
169358802Seric 			cmntcnt++;
169458802Seric 			break;
169558802Seric 
169658802Seric 		  case ')':
169758802Seric 			if (cmntcnt-- <= 0)
169858802Seric 				return NULL;
169958802Seric 			break;
170058802Seric 		}
170158802Seric 
170258802Seric 		if (cmntcnt > 0)
170358802Seric 		{
170458802Seric 			*q++ = c;
170558802Seric 			continue;
170658802Seric 		}
170758802Seric 
170858802Seric 		switch (c)
170958802Seric 		{
171058802Seric 		  case '"':
171158802Seric 			quotemode = !quotemode;
171258802Seric 			quotecnt++;
171358802Seric 			continue;
171458802Seric 
171558802Seric 		  case '<':
171658802Seric 			anglecnt++;
171758802Seric 			break;
171858802Seric 
171958802Seric 		  case '>':
172058802Seric 			if (anglecnt-- <= 0)
172158802Seric 				return NULL;
172258802Seric 			break;
172358802Seric 		}
172458802Seric 		*q++ = c;
172558802Seric 	}
172658802Seric 
172758802Seric 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
172858802Seric 	    quotemode || quotecnt <= 0)
172958802Seric 		return NULL;
173058802Seric 	*q++ = '\0';
173158802Seric 	return buf;
173258802Seric }
1733