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*58814Seric static char sccsid[] = "@(#)parseaddr.c	6.32 (Berkeley) 03/25/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 
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 */
5613149Seric };
5623149Seric 
5634468Seric # define MAXMATCH	9	/* max params per rewrite */
5643149Seric 
5653149Seric 
5664070Seric rewrite(pvp, ruleset)
5673149Seric 	char **pvp;
5684070Seric 	int ruleset;
5693149Seric {
5703149Seric 	register char *ap;		/* address pointer */
5713149Seric 	register char *rp;		/* rewrite pointer */
5723149Seric 	register char **avp;		/* address vector pointer */
5733149Seric 	register char **rvp;		/* rewrite vector pointer */
5748058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5758058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
57656678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5773149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5783149Seric 
5799279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5803149Seric 	{
5818959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
58256678Seric 		printav(pvp);
5833149Seric 	}
58456678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
58556326Seric 	{
58658151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
58756326Seric 		return;
58856326Seric 	}
58956678Seric 	if (pvp == NULL)
59056678Seric 		return;
59156326Seric 
5923149Seric 	/*
59356678Seric 	**  Run through the list of rewrite rules, applying
59456678Seric 	**	any that match.
5953149Seric 	*/
5963149Seric 
5974070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
5983149Seric 	{
59956678Seric 		int loopcount = 0;
60056678Seric 
6017675Seric 		if (tTd(21, 12))
602297Seric 		{
6038069Seric 			printf("-----trying rule:");
60456678Seric 			printav(rwr->r_lhs);
6053149Seric 		}
6063149Seric 
6073149Seric 		/* try to match on this rule */
6084468Seric 		mlp = mlist;
6098058Seric 		rvp = rwr->r_lhs;
6108058Seric 		avp = pvp;
6118058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
6123149Seric 		{
61356678Seric 			if (++loopcount > 100)
61452637Seric 			{
61558151Seric 				syserr("554 Infinite loop in ruleset %d", ruleset);
61656678Seric 				printf("workspace: ");
61756678Seric 				printav(pvp);
61852637Seric 				break;
61952637Seric 			}
6203149Seric 			rp = *rvp;
6218058Seric 			if (tTd(21, 35))
6228058Seric 			{
62357532Seric 				printf("rp=");
62457531Seric 				xputs(rp);
62557532Seric 				printf(", ap=");
6268058Seric 				xputs(ap);
6278069Seric 				printf("\n");
6288058Seric 			}
62956678Seric 			if (rp == NULL)
63056326Seric 			{
6313149Seric 				/* end-of-pattern before end-of-address */
6328058Seric 				goto backup;
63356678Seric 			}
63458173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
63558173Seric 			    (*rp & 0377) != CANONHOST)
63656326Seric 			{
63756678Seric 				/* end-of-input */
63856678Seric 				break;
639297Seric 			}
64056326Seric 
64158050Seric 			switch (*rp & 0377)
6428058Seric 			{
64356678Seric 				register STAB *s;
644*58814Seric 				char buf[MAXLINE];
64556326Seric 
64656678Seric 			  case MATCHCLASS:
64756678Seric 			  case MATCHNCLASS:
64856678Seric 				/* match any token in (not in) a class */
649*58814Seric 				mlp->first = avp;
650*58814Seric 	extendclass:
651*58814Seric 				if (*avp == NULL)
652*58814Seric 					goto backup;
653*58814Seric 				mlp->last = avp++;
654*58814Seric 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
655*58814Seric 				s = stab(buf, ST_CLASS, ST_FIND);
65656678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
65756326Seric 				{
65858050Seric 					if ((*rp & 0377) == MATCHCLASS)
659*58814Seric 						goto extendclass;
66056326Seric 				}
66158050Seric 				else if ((*rp & 0377) == MATCHNCLASS)
662*58814Seric 					goto extendclass;
663*58814Seric 				mlp++;
664*58814Seric 				break;
6654060Seric 
66656678Seric 			  case MATCHONE:
66756678Seric 			  case MATCHANY:
66856678Seric 				/* match exactly one token */
66956678Seric 				mlp->first = avp;
67056678Seric 				mlp->last = avp++;
6718058Seric 				mlp++;
67256678Seric 				break;
6738058Seric 
67456678Seric 			  case MATCHZANY:
67556678Seric 				/* match zero or more tokens */
67656678Seric 				mlp->first = avp;
67756678Seric 				mlp->last = avp - 1;
67856678Seric 				mlp++;
67956678Seric 				break;
68056326Seric 
68158173Seric 			  case CANONHOST:
68258173Seric 				/* match zero tokens */
68358173Seric 				break;
68458173Seric 
68556678Seric 			  default:
68656678Seric 				/* must have exact match */
68756678Seric 				if (strcasecmp(rp, ap))
6888058Seric 					goto backup;
6894468Seric 				avp++;
69056678Seric 				break;
6913149Seric 			}
6923149Seric 
69356678Seric 			/* successful match on this token */
6943149Seric 			rvp++;
6953149Seric 			continue;
6963149Seric 
69756678Seric 		  backup:
69856678Seric 			/* match failed -- back up */
69956678Seric 			while (--rvp >= rwr->r_lhs)
7003149Seric 			{
70156678Seric 				rp = *rvp;
70258050Seric 				if ((*rp & 0377) == MATCHANY ||
70358050Seric 				    (*rp & 0377) == MATCHZANY)
7044468Seric 				{
70556678Seric 					/* extend binding and continue */
70656678Seric 					avp = ++mlp[-1].last;
70756678Seric 					avp++;
70856678Seric 					rvp++;
70956678Seric 					break;
7104468Seric 				}
711*58814Seric 				if ((*rp & 0377) == MATCHCLASS ||
71258050Seric 				    (*rp & 0377) == MATCHNCLASS)
71356678Seric 				{
714*58814Seric 					/* extend binding and try again */
715*58814Seric 					mlp--;
716*58814Seric 					avp = ++mlp->last;
717*58814Seric 					goto extendclass;
718*58814Seric 				}
719*58814Seric 				avp--;
720*58814Seric 				if ((*rp & 0377) == MATCHONE)
721*58814Seric 				{
72256678Seric 					/* back out binding */
72356678Seric 					mlp--;
72456678Seric 				}
7253149Seric 			}
7263149Seric 
72756678Seric 			if (rvp < rwr->r_lhs)
72856678Seric 			{
72956678Seric 				/* total failure to match */
73056326Seric 				break;
7313149Seric 			}
732297Seric 		}
7333149Seric 
7343149Seric 		/*
73556678Seric 		**  See if we successfully matched
7363149Seric 		*/
7373149Seric 
73856678Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
7393149Seric 		{
7409374Seric 			if (tTd(21, 10))
7419374Seric 				printf("----- rule fails\n");
7429374Seric 			rwr = rwr->r_next;
7439374Seric 			continue;
7449374Seric 		}
7453149Seric 
7469374Seric 		rvp = rwr->r_rhs;
7479374Seric 		if (tTd(21, 12))
7489374Seric 		{
7499374Seric 			printf("-----rule matches:");
75056678Seric 			printav(rvp);
7519374Seric 		}
7529374Seric 
7539374Seric 		rp = *rvp;
75458050Seric 		if ((*rp & 0377) == CANONUSER)
7559374Seric 		{
7569374Seric 			rvp++;
7579374Seric 			rwr = rwr->r_next;
7589374Seric 		}
75958050Seric 		else if ((*rp & 0377) == CANONHOST)
7609374Seric 		{
7619374Seric 			rvp++;
7629374Seric 			rwr = NULL;
7639374Seric 		}
76458050Seric 		else if ((*rp & 0377) == CANONNET)
7659374Seric 			rwr = NULL;
7669374Seric 
7679374Seric 		/* substitute */
7689374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
7699374Seric 		{
7709374Seric 			register struct match *m;
7719374Seric 			register char **pp;
7729374Seric 
7738058Seric 			rp = *rvp;
77458050Seric 			if ((*rp & 0377) == MATCHREPL)
7758058Seric 			{
77616914Seric 				/* substitute from LHS */
77716914Seric 				m = &mlist[rp[1] - '1'];
77856678Seric 				if (m < mlist || m >= mlp)
7799374Seric 				{
78058151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
78156326Seric 						ruleset, rp[1]);
7829374Seric 					return;
7839374Seric 				}
78416914Seric 				if (tTd(21, 15))
78516914Seric 				{
78616914Seric 					printf("$%c:", rp[1]);
78716914Seric 					pp = m->first;
78856678Seric 					while (pp <= m->last)
78916914Seric 					{
79016914Seric 						printf(" %x=\"", *pp);
79116914Seric 						(void) fflush(stdout);
79216914Seric 						printf("%s\"", *pp++);
79316914Seric 					}
79416914Seric 					printf("\n");
79516914Seric 				}
7969374Seric 				pp = m->first;
79756678Seric 				while (pp <= m->last)
7983149Seric 				{
79916914Seric 					if (avp >= &npvp[MAXATOM])
80056678Seric 					{
80158151Seric 						syserr("554 rewrite: expansion too long");
80256678Seric 						return;
80356678Seric 					}
80416914Seric 					*avp++ = *pp++;
8053149Seric 				}
8063149Seric 			}
80716914Seric 			else
8088226Seric 			{
80916914Seric 				/* vanilla replacement */
8109374Seric 				if (avp >= &npvp[MAXATOM])
81116889Seric 				{
81256678Seric 	toolong:
81358151Seric 					syserr("554 rewrite: expansion too long");
81416889Seric 					return;
81516889Seric 				}
81656678Seric 				*avp++ = rp;
8178226Seric 			}
8189374Seric 		}
8199374Seric 		*avp++ = NULL;
82016914Seric 
82116914Seric 		/*
82256678Seric 		**  Check for any hostname/keyword lookups.
82316914Seric 		*/
82416914Seric 
82516914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
82616914Seric 		{
82756678Seric 			char **hbrvp;
82816914Seric 			char **xpvp;
82916914Seric 			int trsize;
83017473Seric 			char *olddelimchar;
83156678Seric 			char *replac;
83256678Seric 			int endtoken;
83356678Seric 			STAB *map;
83456678Seric 			char *mapname;
83556678Seric 			char **key_rvp;
83656678Seric 			char **arg_rvp;
83756678Seric 			char **default_rvp;
83856678Seric 			char buf[MAXNAME + 1];
83916914Seric 			char *pvpb1[MAXATOM + 1];
84056823Seric 			char *argvect[10];
84117174Seric 			char pvpbuf[PSBUFSIZE];
84216914Seric 
84358050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
84458050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
84516914Seric 				continue;
84616914Seric 
84716914Seric 			/*
84856678Seric 			**  Got a hostname/keyword lookup.
84916914Seric 			**
85016914Seric 			**	This could be optimized fairly easily.
85116914Seric 			*/
85216914Seric 
85316914Seric 			hbrvp = rvp;
85458050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
85556327Seric 			{
85656678Seric 				endtoken = HOSTEND;
85756678Seric 				mapname = "host";
85856327Seric 			}
85956326Seric 			else
86056327Seric 			{
86156678Seric 				endtoken = LOOKUPEND;
86256678Seric 				mapname = *++rvp;
86356327Seric 			}
86456678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
86556678Seric 			if (map == NULL)
86658151Seric 				syserr("554 rewrite: map %s not found", mapname);
86756678Seric 
86856678Seric 			/* extract the match part */
86956678Seric 			key_rvp = ++rvp;
87056823Seric 			default_rvp = NULL;
87156823Seric 			arg_rvp = argvect;
87256823Seric 			xpvp = NULL;
87356823Seric 			replac = pvpbuf;
87458050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
87553654Seric 			{
87658050Seric 				int nodetype = **rvp & 0377;
87756823Seric 
87856823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
87956678Seric 				{
88056823Seric 					rvp++;
88156823Seric 					continue;
88256823Seric 				}
88356823Seric 
88456823Seric 				*rvp++ = NULL;
88556823Seric 
88656823Seric 				if (xpvp != NULL)
88756823Seric 				{
888*58814Seric 					cataddr(xpvp, NULL, replac,
88958082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
89058082Seric 						'\0');
89156823Seric 					*++arg_rvp = replac;
89256823Seric 					replac += strlen(replac) + 1;
89356823Seric 					xpvp = NULL;
89456823Seric 				}
89556823Seric 				switch (nodetype)
89656823Seric 				{
89756678Seric 				  case CANONHOST:
89856823Seric 					xpvp = rvp;
89956678Seric 					break;
90056678Seric 
90156678Seric 				  case CANONUSER:
90256678Seric 					default_rvp = rvp;
90356678Seric 					break;
90456678Seric 				}
90553654Seric 			}
90616914Seric 			if (*rvp != NULL)
90716914Seric 				*rvp++ = NULL;
90856823Seric 			if (xpvp != NULL)
90956823Seric 			{
910*58814Seric 				cataddr(xpvp, NULL, replac,
91158082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
91258082Seric 					'\0');
91356823Seric 				*++arg_rvp = replac;
91456823Seric 			}
91556823Seric 			*++arg_rvp = NULL;
91616914Seric 
91716914Seric 			/* save the remainder of the input string */
91816914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
91916914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
92016914Seric 
92156678Seric 			/* look it up */
922*58814Seric 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
92356823Seric 			argvect[0] = buf;
92456678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
92556836Seric 			{
92656836Seric 				int bsize = sizeof buf - 1;
92756836Seric 
92856836Seric 				if (map->s_map.map_app != NULL)
92956836Seric 					bsize -= strlen(map->s_map.map_app);
93058796Seric 				if (tTd(60, 1))
93158796Seric 					printf("map_lookup(%s, %s) => ",
93258796Seric 						mapname, buf);
93356836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
93456823Seric 						buf, sizeof buf - 1, argvect);
93556836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
93656836Seric 					strcat(replac, map->s_map.map_app);
93758796Seric 				if (tTd(60, 1))
93858796Seric 					printf("%s\n", replac ? replac : "NOT FOUND");
93956836Seric 			}
94053654Seric 			else
94156678Seric 				replac = NULL;
94256678Seric 
94356678Seric 			/* if no replacement, use default */
94456823Seric 			if (replac == NULL && default_rvp != NULL)
94556823Seric 			{
94656823Seric 				char buf2[sizeof buf];
94756823Seric 
94856823Seric 				/* rewrite the default with % translations */
949*58814Seric 				cataddr(default_rvp, NULL, buf2, sizeof buf2, '\0');
95056823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
95156823Seric 					argvect);
95256823Seric 				replac = buf;
95356823Seric 			}
95456823Seric 
95556678Seric 			if (replac == NULL)
95651317Seric 			{
95756823Seric 				xpvp = key_rvp;
95853654Seric 			}
95956678Seric 			else
96053654Seric 			{
96156678Seric 				/* scan the new replacement */
96258333Seric 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
96353654Seric 				if (xpvp == NULL)
96451317Seric 				{
96558403Seric 					/* prescan already printed error */
96656678Seric 					return;
96756678Seric 				}
96851317Seric 			}
96951317Seric 
97016914Seric 			/* append it to the token list */
97156678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
97256678Seric 			{
97317174Seric 				*avp++ = newstr(*xpvp);
97416920Seric 				if (avp >= &npvp[MAXATOM])
97516914Seric 					goto toolong;
97617174Seric 			}
97716914Seric 
97816914Seric 			/* restore the old trailing information */
97956678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
98016920Seric 				if (avp >= &npvp[MAXATOM])
98116914Seric 					goto toolong;
98217174Seric 
98356678Seric 			break;
98416914Seric 		}
98516914Seric 
98616914Seric 		/*
98716914Seric 		**  Check for subroutine calls.
98816914Seric 		*/
98916914Seric 
99058050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
99156678Seric 		{
99256678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
99356678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
99456678Seric 			if (tTd(21, 3))
99556678Seric 				printf("-----callsubr %s\n", npvp[1]);
99656678Seric 			rewrite(pvp, atoi(npvp[1]));
99756678Seric 		}
99856678Seric 		else
99956678Seric 		{
100017348Seric 			bcopy((char *) npvp, (char *) pvp,
100116900Seric 				(int) (avp - npvp) * sizeof *avp);
100256678Seric 		}
10039374Seric 		if (tTd(21, 4))
10049374Seric 		{
10059374Seric 			printf("rewritten as:");
100656678Seric 			printav(pvp);
10079374Seric 		}
1008297Seric 	}
10098069Seric 
10109279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
10118069Seric 	{
10128959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
101356678Seric 		printav(pvp);
10148069Seric 	}
10153149Seric }
10163149Seric /*
10173149Seric **  BUILDADDR -- build address from token vector.
10183149Seric **
10193149Seric **	Parameters:
10203149Seric **		tv -- token vector.
10213149Seric **		a -- pointer to address descriptor to fill.
10223149Seric **			If NULL, one will be allocated.
10233149Seric **
10243149Seric **	Returns:
10254279Seric **		NULL if there was an error.
10264279Seric **		'a' otherwise.
10273149Seric **
10283149Seric **	Side Effects:
10293149Seric **		fills in 'a'
10303149Seric */
10313149Seric 
103257249Seric struct errcodes
103357249Seric {
103457249Seric 	char	*ec_name;		/* name of error code */
103557249Seric 	int	ec_code;		/* numeric code */
103657249Seric } ErrorCodes[] =
103757249Seric {
103857249Seric 	"usage",	EX_USAGE,
103957249Seric 	"nouser",	EX_NOUSER,
104057249Seric 	"nohost",	EX_NOHOST,
104157249Seric 	"unavailable",	EX_UNAVAILABLE,
104257249Seric 	"software",	EX_SOFTWARE,
104357249Seric 	"tempfail",	EX_TEMPFAIL,
104457249Seric 	"protocol",	EX_PROTOCOL,
104557249Seric #ifdef EX_CONFIG
104657249Seric 	"config",	EX_CONFIG,
104757249Seric #endif
104857249Seric 	NULL,		EX_UNAVAILABLE,
104957249Seric };
105057249Seric 
105156678Seric ADDRESS *
10523149Seric buildaddr(tv, a)
10533149Seric 	register char **tv;
10543149Seric 	register ADDRESS *a;
10553149Seric {
10563149Seric 	struct mailer **mp;
10573149Seric 	register struct mailer *m;
105858008Seric 	char *bp;
105958008Seric 	int spaceleft;
106057402Seric 	static char buf[MAXNAME];
10613149Seric 
10623149Seric 	if (a == NULL)
10633149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
106416889Seric 	bzero((char *) a, sizeof *a);
10653149Seric 
10663149Seric 	/* figure out what net/mailer to use */
106758050Seric 	if ((**tv & 0377) != CANONNET)
10684279Seric 	{
106958151Seric 		syserr("554 buildaddr: no net");
10704279Seric 		return (NULL);
10714279Seric 	}
10723149Seric 	tv++;
107358680Seric 	if (strcasecmp(*tv, "error") == 0)
10744279Seric 	{
107558050Seric 		if ((**++tv & 0377) == CANONHOST)
107610183Seric 		{
107757249Seric 			register struct errcodes *ep;
107857249Seric 
107958050Seric 			if (isascii(**++tv) && isdigit(**tv))
108057249Seric 			{
108157249Seric 				setstat(atoi(*tv));
108257249Seric 			}
108357249Seric 			else
108457249Seric 			{
108557249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
108657249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
108757249Seric 						break;
108857249Seric 				setstat(ep->ec_code);
108957249Seric 			}
109010183Seric 			tv++;
109110183Seric 		}
109258050Seric 		if ((**tv & 0377) != CANONUSER)
109358151Seric 			syserr("554 buildaddr: error: no user");
1094*58814Seric 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
109558082Seric 		stripquotes(buf);
10964279Seric 		usrerr(buf);
10974279Seric 		return (NULL);
10984279Seric 	}
109957402Seric 
11004598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
11013149Seric 	{
110258680Seric 		if (strcasecmp(m->m_name, *tv) == 0)
11033149Seric 			break;
11043149Seric 	}
11053149Seric 	if (m == NULL)
11064279Seric 	{
110758151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
11084279Seric 		return (NULL);
11094279Seric 	}
11104598Seric 	a->q_mailer = m;
11113149Seric 
11123149Seric 	/* figure out what host (if any) */
111356678Seric 	tv++;
111458509Seric 	if ((**tv & 0377) == CANONHOST)
11153149Seric 	{
111658008Seric 		bp = buf;
111758008Seric 		spaceleft = sizeof buf - 1;
111858050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
111958008Seric 		{
112058008Seric 			int i = strlen(*tv);
112158008Seric 
112258008Seric 			if (i > spaceleft)
112358008Seric 			{
112458008Seric 				/* out of space for this address */
112558008Seric 				if (spaceleft >= 0)
112658151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
112758008Seric 						buf);
112858008Seric 				i = spaceleft;
112958008Seric 				spaceleft = 0;
113058008Seric 			}
113158008Seric 			if (i <= 0)
113258008Seric 				continue;
113358008Seric 			bcopy(*tv, bp, i);
113458008Seric 			bp += i;
113558008Seric 			spaceleft -= i;
113658008Seric 		}
113758010Seric 		*bp = '\0';
11385704Seric 		a->q_host = newstr(buf);
11393149Seric 	}
114057249Seric 	else
114158509Seric 	{
114258509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
114358509Seric 		{
114458509Seric 			syserr("554 buildaddr: no host");
114558509Seric 			return (NULL);
114658509Seric 		}
114757249Seric 		a->q_host = NULL;
114858509Seric 	}
11493149Seric 
11503149Seric 	/* figure out the user */
115158050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
11524279Seric 	{
115358151Seric 		syserr("554 buildaddr: no user");
11544279Seric 		return (NULL);
11554279Seric 	}
115657402Seric 	tv++;
115751317Seric 
115857402Seric 	/* do special mapping for local mailer */
115957402Seric 	if (m == LocalMailer && *tv != NULL)
116057402Seric 	{
116157454Seric 		register char *p = *tv;
116257454Seric 
116357454Seric 		if (*p == '"')
116457454Seric 			p++;
116557454Seric 		if (*p == '|')
116657402Seric 			a->q_mailer = m = ProgMailer;
116757454Seric 		else if (*p == '/')
116857402Seric 			a->q_mailer = m = FileMailer;
116957454Seric 		else if (*p == ':')
117057402Seric 		{
117157402Seric 			/* may be :include: */
1172*58814Seric 			cataddr(tv, NULL, buf, sizeof buf, '\0');
117358008Seric 			stripquotes(buf);
117458008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
117558008Seric 			{
117658008Seric 				/* if :include:, don't need further rewriting */
117757402Seric 				a->q_mailer = m = InclMailer;
117858008Seric 				a->q_user = &buf[9];
117958008Seric 				return (a);
118058008Seric 			}
118157402Seric 		}
118257402Seric 	}
118357402Seric 
118458008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
118558008Seric 	{
118658008Seric 		tv++;
118758008Seric 		a->q_flags |= QNOTREMOTE;
118858008Seric 	}
118958008Seric 
119058673Seric 	/* do cleanup of final address */
119119040Seric 	rewrite(tv, 4);
119219040Seric 
119319040Seric 	/* save the result for the command line/RCPT argument */
1194*58814Seric 	cataddr(tv, NULL, buf, sizeof buf, '\0');
11953149Seric 	a->q_user = buf;
11963149Seric 
119758670Seric 	/*
119858670Seric 	**  Do mapping to lower case as requested by mailer
119958670Seric 	*/
120058670Seric 
120158670Seric 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
120258670Seric 		makelower(a->q_host);
120358670Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
120458670Seric 		makelower(a->q_user);
120558670Seric 
12063149Seric 	return (a);
12073149Seric }
12083188Seric /*
12094228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
12104228Seric **
12114228Seric **	Parameters:
12124228Seric **		pvp -- parameter vector to rebuild.
1213*58814Seric **		evp -- last parameter to include.  Can be NULL to
1214*58814Seric **			use entire pvp.
12154228Seric **		buf -- buffer to build the string into.
12164228Seric **		sz -- size of buf.
121758082Seric **		spacesub -- the space separator character; if null,
121858082Seric **			use SpaceSub.
12194228Seric **
12204228Seric **	Returns:
12214228Seric **		none.
12224228Seric **
12234228Seric **	Side Effects:
12244228Seric **		Destroys buf.
12254228Seric */
12264228Seric 
1227*58814Seric cataddr(pvp, evp, buf, sz, spacesub)
12284228Seric 	char **pvp;
1229*58814Seric 	char **evp;
12304228Seric 	char *buf;
12314228Seric 	register int sz;
123258082Seric 	char spacesub;
12334228Seric {
12344228Seric 	bool oatomtok = FALSE;
123556678Seric 	bool natomtok = FALSE;
12364228Seric 	register int i;
12374228Seric 	register char *p;
12384228Seric 
123958082Seric 	if (spacesub == '\0')
124058082Seric 		spacesub = SpaceSub;
124158082Seric 
12428423Seric 	if (pvp == NULL)
12438423Seric 	{
124423109Seric 		(void) strcpy(buf, "");
12458423Seric 		return;
12468423Seric 	}
12474228Seric 	p = buf;
124811156Seric 	sz -= 2;
12494228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
12504228Seric 	{
12518078Seric 		natomtok = (toktype(**pvp) == ATM);
12524228Seric 		if (oatomtok && natomtok)
125358082Seric 			*p++ = spacesub;
12544228Seric 		(void) strcpy(p, *pvp);
12554228Seric 		oatomtok = natomtok;
12564228Seric 		p += i;
125711156Seric 		sz -= i + 1;
1258*58814Seric 		if (pvp++ == evp)
1259*58814Seric 			break;
12604228Seric 	}
12614228Seric 	*p = '\0';
12624228Seric }
12634228Seric /*
12643188Seric **  SAMEADDR -- Determine if two addresses are the same
12653188Seric **
12663188Seric **	This is not just a straight comparison -- if the mailer doesn't
12673188Seric **	care about the host we just ignore it, etc.
12683188Seric **
12693188Seric **	Parameters:
12703188Seric **		a, b -- pointers to the internal forms to compare.
12713188Seric **
12723188Seric **	Returns:
12733188Seric **		TRUE -- they represent the same mailbox.
12743188Seric **		FALSE -- they don't.
12753188Seric **
12763188Seric **	Side Effects:
12773188Seric **		none.
12783188Seric */
12793188Seric 
12803188Seric bool
12819374Seric sameaddr(a, b)
12823188Seric 	register ADDRESS *a;
12833188Seric 	register ADDRESS *b;
12843188Seric {
12853188Seric 	/* if they don't have the same mailer, forget it */
12863188Seric 	if (a->q_mailer != b->q_mailer)
12873188Seric 		return (FALSE);
12883188Seric 
12893188Seric 	/* if the user isn't the same, we can drop out */
129056678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
12913188Seric 		return (FALSE);
12923188Seric 
129358438Seric 	/* if we have good uids for both but the differ, these are different */
129458438Seric 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
129558438Seric 		return (FALSE);
129658438Seric 
129758509Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
129858509Seric 	if (a->q_host == b->q_host)
129958509Seric 	{
130058509Seric 		/* probably both null pointers */
13013188Seric 		return (TRUE);
130258509Seric 	}
13033188Seric 	if (a->q_host == NULL || b->q_host == NULL)
130458509Seric 	{
130558509Seric 		/* only one is a null pointer */
13063188Seric 		return (FALSE);
130758509Seric 	}
130856678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
13093188Seric 		return (FALSE);
13103188Seric 
13113188Seric 	return (TRUE);
13123188Seric }
13133234Seric /*
13143234Seric **  PRINTADDR -- print address (for debugging)
13153234Seric **
13163234Seric **	Parameters:
13173234Seric **		a -- the address to print
13183234Seric **		follow -- follow the q_next chain.
13193234Seric **
13203234Seric **	Returns:
13213234Seric **		none.
13223234Seric **
13233234Seric **	Side Effects:
13243234Seric **		none.
13253234Seric */
13263234Seric 
13273234Seric printaddr(a, follow)
13283234Seric 	register ADDRESS *a;
13293234Seric 	bool follow;
13303234Seric {
13315001Seric 	bool first = TRUE;
133257731Seric 	register MAILER *m;
133357731Seric 	MAILER pseudomailer;
13345001Seric 
13353234Seric 	while (a != NULL)
13363234Seric 	{
13375001Seric 		first = FALSE;
13384443Seric 		printf("%x=", a);
13394085Seric 		(void) fflush(stdout);
134057731Seric 
134157731Seric 		/* find the mailer -- carefully */
134257731Seric 		m = a->q_mailer;
134357731Seric 		if (m == NULL)
134457731Seric 		{
134557731Seric 			m = &pseudomailer;
134657731Seric 			m->m_mno = -1;
134757731Seric 			m->m_name = "NULL";
134857731Seric 		}
134957731Seric 
135058680Seric 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
135157731Seric 		       a->q_paddr, m->m_mno, m->m_name,
135240973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
13538181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
13548181Seric 		       a->q_alias);
13558181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
13568181Seric 		       a->q_fullname);
13574996Seric 
13583234Seric 		if (!follow)
13593234Seric 			return;
13604996Seric 		a = a->q_next;
13613234Seric 	}
13625001Seric 	if (first)
13634443Seric 		printf("[NULL]\n");
13643234Seric }
13654317Seric 
13667682Seric /*
13677682Seric **  REMOTENAME -- return the name relative to the current mailer
13687682Seric **
13697682Seric **	Parameters:
13707682Seric **		name -- the name to translate.
13718069Seric **		m -- the mailer that we want to do rewriting relative
13728069Seric **			to.
13738069Seric **		senderaddress -- if set, uses the sender rewriting rules
13748069Seric **			rather than the recipient rewriting rules.
137558020Seric **		header -- set if this address is in the header, rather
137658020Seric **			than an envelope header.
137710310Seric **		canonical -- if set, strip out any comment information,
137810310Seric **			etc.
137958509Seric **		adddomain -- if set, OK to do domain extension.
138058020Seric **		e -- the current envelope.
13817682Seric **
13827682Seric **	Returns:
13837682Seric **		the text string representing this address relative to
13847682Seric **			the receiving mailer.
13857682Seric **
13867682Seric **	Side Effects:
13877682Seric **		none.
13887682Seric **
13897682Seric **	Warnings:
13907682Seric **		The text string returned is tucked away locally;
13917682Seric **			copy it if you intend to save it.
13927682Seric */
13937682Seric 
13947682Seric char *
139558509Seric remotename(name, m, senderaddress, header, canonical, adddomain, e)
13967682Seric 	char *name;
139756678Seric 	struct mailer *m;
13988069Seric 	bool senderaddress;
139958020Seric 	bool header;
140010310Seric 	bool canonical;
140158509Seric 	bool adddomain;
140256678Seric 	register ENVELOPE *e;
14037682Seric {
14048069Seric 	register char **pvp;
14058069Seric 	char *fancy;
140656678Seric 	extern char *macvalue();
140756678Seric 	char *oldg = macvalue('g', e);
140858020Seric 	int rwset;
14097682Seric 	static char buf[MAXNAME];
14107682Seric 	char lbuf[MAXNAME];
141116914Seric 	char pvpbuf[PSBUFSIZE];
141256678Seric 	extern char **prescan();
141356678Seric 	extern char *crackaddr();
14147682Seric 
14157755Seric 	if (tTd(12, 1))
14167755Seric 		printf("remotename(%s)\n", name);
14177755Seric 
141810177Seric 	/* don't do anything if we are tagging it as special */
141958020Seric 	if (senderaddress)
142058020Seric 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
142158020Seric 	else
142258020Seric 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
142358020Seric 	if (rwset < 0)
142410177Seric 		return (name);
142510177Seric 
14267682Seric 	/*
14278181Seric 	**  Do a heuristic crack of this name to extract any comment info.
14288181Seric 	**	This will leave the name as a comment and a $g macro.
14297889Seric 	*/
14307889Seric 
143158036Seric 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
143258050Seric 		fancy = "\201g";
143310310Seric 	else
143410310Seric 		fancy = crackaddr(name);
14357889Seric 
14368181Seric 	/*
14378181Seric 	**  Turn the name into canonical form.
14388181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
14398181Seric 	**	If this only resolves to "user", and the "C" flag is
14408181Seric 	**	specified in the sending mailer, then the sender's
14418181Seric 	**	domain will be appended.
14428181Seric 	*/
14438181Seric 
144458333Seric 	pvp = prescan(name, '\0', pvpbuf, NULL);
14457889Seric 	if (pvp == NULL)
14467889Seric 		return (name);
14478181Seric 	rewrite(pvp, 3);
144858509Seric 	if (adddomain && e->e_fromdomain != NULL)
14498181Seric 	{
14508181Seric 		/* append from domain to this address */
14518181Seric 		register char **pxp = pvp;
14528181Seric 
14539594Seric 		/* see if there is an "@domain" in the current name */
14548181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
14558181Seric 			pxp++;
14568181Seric 		if (*pxp == NULL)
14578181Seric 		{
14589594Seric 			/* no.... append the "@domain" from the sender */
145956678Seric 			register char **qxq = e->e_fromdomain;
14608181Seric 
14619594Seric 			while ((*pxp++ = *qxq++) != NULL)
14629594Seric 				continue;
146311726Seric 			rewrite(pvp, 3);
14648181Seric 		}
14658181Seric 	}
14668181Seric 
14678181Seric 	/*
14688959Seric 	**  Do more specific rewriting.
146956678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
147056678Seric 	**		a sender address or not.
14718181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
14728181Seric 	*/
14738181Seric 
14748069Seric 	if (senderaddress)
147556327Seric 		rewrite(pvp, 1);
14768069Seric 	else
147756327Seric 		rewrite(pvp, 2);
147858020Seric 	if (rwset > 0)
147958020Seric 		rewrite(pvp, rwset);
14807682Seric 
14818181Seric 	/*
14828959Seric 	**  Do any final sanitation the address may require.
14838959Seric 	**	This will normally be used to turn internal forms
14848959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
14858959Seric 	**	may be used as a default to the above rules.
14868959Seric 	*/
14878959Seric 
14888959Seric 	rewrite(pvp, 4);
14898959Seric 
14908959Seric 	/*
14918181Seric 	**  Now restore the comment information we had at the beginning.
14928181Seric 	*/
14938181Seric 
149458082Seric 	cataddr(pvp, lbuf, sizeof lbuf, '\0');
149556678Seric 	define('g', lbuf, e);
149656678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
149756678Seric 	define('g', oldg, e);
14987682Seric 
14997682Seric 	if (tTd(12, 1))
15007755Seric 		printf("remotename => `%s'\n", buf);
15017682Seric 	return (buf);
15027682Seric }
150351317Seric /*
150456678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
150551317Seric **
150651317Seric **	Parameters:
150756678Seric **		a -- the address to map (but just the user name part).
150856678Seric **		sendq -- the sendq in which to install any replacement
150956678Seric **			addresses.
151051317Seric **
151151317Seric **	Returns:
151251317Seric **		none.
151351317Seric */
151451317Seric 
151556678Seric maplocaluser(a, sendq, e)
151656678Seric 	register ADDRESS *a;
151756678Seric 	ADDRESS **sendq;
151856678Seric 	ENVELOPE *e;
151951317Seric {
152056678Seric 	register char **pvp;
152156678Seric 	register ADDRESS *a1 = NULL;
152258333Seric 	auto char *delimptr;
152356678Seric 	char pvpbuf[PSBUFSIZE];
152451317Seric 
152556678Seric 	if (tTd(29, 1))
152656678Seric 	{
152756678Seric 		printf("maplocaluser: ");
152856678Seric 		printaddr(a, FALSE);
152956678Seric 	}
153058333Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
153156678Seric 	if (pvp == NULL)
153256678Seric 		return;
153351317Seric 
153456678Seric 	rewrite(pvp, 5);
153558050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
153656678Seric 		return;
153751317Seric 
153856678Seric 	/* if non-null, mailer destination specified -- has it changed? */
153956678Seric 	a1 = buildaddr(pvp, NULL);
154056678Seric 	if (a1 == NULL || sameaddr(a, a1))
154156678Seric 		return;
154251317Seric 
154356678Seric 	/* mark old address as dead; insert new address */
154456678Seric 	a->q_flags |= QDONTSEND;
154557731Seric 	if (tTd(29, 5))
154657731Seric 	{
154757731Seric 		printf("maplocaluser: QDONTSEND ");
154857731Seric 		printaddr(a, FALSE);
154957731Seric 	}
155056678Seric 	a1->q_alias = a;
155158333Seric 	allocaddr(a1, 1, NULL, delimptr);
155256678Seric 	(void) recipient(a1, sendq, e);
155351317Seric }
155458802Seric /*
155558802Seric **  DEQUOTE_INIT -- initialize dequote map
155658802Seric **
155758802Seric **	This is a no-op.
155858802Seric **
155958802Seric **	Parameters:
156058802Seric **		map -- the internal map structure.
156158802Seric **		mapname -- the name of the mapl.
156258802Seric **		args -- arguments.
156358802Seric **
156458802Seric **	Returns:
156558802Seric **		TRUE.
156658802Seric */
156758802Seric 
156858802Seric bool
156958802Seric dequote_init(map, mapname, args)
157058802Seric 	MAP *map;
157158802Seric 	char *mapname;
157258802Seric 	char *args;
157358802Seric {
157458805Seric 	register char *p = args;
157558805Seric 
157658805Seric 	for (;;)
157758805Seric 	{
157858805Seric 		while (isascii(*p) && isspace(*p))
157958805Seric 			p++;
158058805Seric 		if (*p != '-')
158158805Seric 			break;
158258805Seric 		switch (*++p)
158358805Seric 		{
158458805Seric 		  case 'a':
158558805Seric 			map->map_app = ++p;
158658805Seric 			break;
158758805Seric 		}
158858805Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
158958805Seric 			p++;
159058805Seric 		if (*p != '\0')
159158805Seric 			*p = '\0';
159258805Seric 	}
159358805Seric 	if (map->map_app != NULL)
159458805Seric 		map->map_app = newstr(map->map_app);
159558805Seric 
159658802Seric 	return TRUE;
159758802Seric }
159858802Seric /*
159958802Seric **  DEQUOTE_MAP -- unquote an address
160058802Seric **
160158802Seric **	Parameters:
160258802Seric **		map -- the internal map structure (ignored).
160358802Seric **		buf -- the buffer to dequote.
160458802Seric **		bufsiz -- the size of that buffer.
160558802Seric **		av -- arguments (ignored).
160658802Seric **
160758802Seric **	Returns:
160858802Seric **		NULL -- if there were no quotes, or if the resulting
160958802Seric **			unquoted buffer would not be acceptable to prescan.
161058802Seric **		else -- The dequoted buffer.
161158802Seric */
161258802Seric 
161358802Seric char *
161458802Seric dequote_map(map, buf, bufsiz, av)
161558802Seric 	MAP *map;
161658802Seric 	char buf[];
161758802Seric 	int bufsiz;
161858802Seric 	char **av;
161958802Seric {
162058802Seric 	register char *p;
162158802Seric 	register char *q;
162258802Seric 	register char c;
162358802Seric 	int anglecnt;
162458802Seric 	int cmntcnt;
162558802Seric 	int quotecnt;
162658802Seric 	bool quotemode;
162758802Seric 	bool bslashmode;
162858802Seric 
162958802Seric 	anglecnt = 0;
163058802Seric 	cmntcnt = 0;
163158802Seric 	quotecnt = 0;
163258802Seric 	quotemode = FALSE;
163358802Seric 	bslashmode = FALSE;
163458802Seric 
163558802Seric 	for (p = q = buf; (c = *p++) != '\0'; )
163658802Seric 	{
163758802Seric 		if (bslashmode)
163858802Seric 		{
163958802Seric 			bslashmode = FALSE;
164058802Seric 			*q++ = c;
164158802Seric 			continue;
164258802Seric 		}
164358802Seric 
164458802Seric 		switch (c)
164558802Seric 		{
164658802Seric 		  case '\\':
164758802Seric 			bslashmode = TRUE;
164858802Seric 			break;
164958802Seric 
165058802Seric 		  case '(':
165158802Seric 			cmntcnt++;
165258802Seric 			break;
165358802Seric 
165458802Seric 		  case ')':
165558802Seric 			if (cmntcnt-- <= 0)
165658802Seric 				return NULL;
165758802Seric 			break;
165858802Seric 		}
165958802Seric 
166058802Seric 		if (cmntcnt > 0)
166158802Seric 		{
166258802Seric 			*q++ = c;
166358802Seric 			continue;
166458802Seric 		}
166558802Seric 
166658802Seric 		switch (c)
166758802Seric 		{
166858802Seric 		  case '"':
166958802Seric 			quotemode = !quotemode;
167058802Seric 			quotecnt++;
167158802Seric 			continue;
167258802Seric 
167358802Seric 		  case '<':
167458802Seric 			anglecnt++;
167558802Seric 			break;
167658802Seric 
167758802Seric 		  case '>':
167858802Seric 			if (anglecnt-- <= 0)
167958802Seric 				return NULL;
168058802Seric 			break;
168158802Seric 		}
168258802Seric 		*q++ = c;
168358802Seric 	}
168458802Seric 
168558802Seric 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
168658802Seric 	    quotemode || quotecnt <= 0)
168758802Seric 		return NULL;
168858802Seric 	*q++ = '\0';
168958802Seric 	return buf;
169058802Seric }
1691