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*58333Seric static char sccsid[] = "@(#)parseaddr.c	6.21 (Berkeley) 03/01/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.
43*58333Seric **		delimptr -- if non-NULL, set to the location of the
44*58333Seric **			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 *
60*58333Seric parseaddr(addr, a, copyf, delim, delimptr, e)
61297Seric 	char *addr;
622973Seric 	register ADDRESS *a;
63297Seric 	int copyf;
6411445Seric 	char delim;
65*58333Seric 	char **delimptr;
6656678Seric 	register ENVELOPE *e;
67297Seric {
683149Seric 	register char **pvp;
69*58333Seric 	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 
90*58333Seric 	if (delimptr == NULL)
91*58333Seric 		delimptr = &delimptrbuf;
92*58333Seric 
93*58333Seric 	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 
133*58333Seric 	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.
181*58333Seric **		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 
190*58333Seric allocaddr(a, copyf, paddr, delimptr)
19156678Seric 	register ADDRESS *a;
19256678Seric 	int copyf;
19356678Seric 	char *paddr;
194*58333Seric 	char *delimptr;
19556678Seric {
19656678Seric 	register MAILER *m = a->q_mailer;
19756678Seric 
19856678Seric 	if (copyf > 0 && paddr != NULL)
19956678Seric 	{
200*58333Seric 		char savec = *delimptr;
2018078Seric 
202*58333Seric 		*delimptr = '\0';
20356678Seric 		a->q_paddr = newstr(paddr);
204*58333Seric 		*delimptr = savec;
2058078Seric 	}
206297Seric 	else
20756678Seric 		a->q_paddr = paddr;
20824944Seric 
20924944Seric 	if (a->q_user == NULL)
21024944Seric 		a->q_user = "";
21124944Seric 	if (a->q_host == NULL)
21224944Seric 		a->q_host = "";
21324944Seric 
2143149Seric 	if (copyf >= 0)
215297Seric 	{
21624944Seric 		a->q_host = newstr(a->q_host);
2173149Seric 		if (a->q_user != a->q_paddr)
2183149Seric 			a->q_user = newstr(a->q_user);
219297Seric 	}
220297Seric 
22156678Seric 	if (a->q_paddr == NULL)
22256678Seric 		a->q_paddr = a->q_user;
22356678Seric 
224297Seric 	/*
22516202Seric 	**  Convert host name to lower case if requested.
22616202Seric 	**	User name will be done later.
22716202Seric 	*/
22816202Seric 
22916202Seric 	if (!bitnset(M_HST_UPPER, m->m_flags))
23016202Seric 		makelower(a->q_host);
231297Seric }
232297Seric /*
23316162Seric **  LOWERADDR -- map UPPER->lower case on addresses as requested.
23416162Seric **
23516162Seric **	Parameters:
23616162Seric **		a -- address to be mapped.
23716162Seric **
23816162Seric **	Returns:
23916162Seric **		none.
24016162Seric **
24116162Seric **	Side Effects:
24216162Seric **		none.
24316162Seric */
24416162Seric 
24516162Seric loweraddr(a)
24616162Seric 	register ADDRESS *a;
24716162Seric {
24816162Seric 	register MAILER *m = a->q_mailer;
24916162Seric 
25016162Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
25116162Seric 		makelower(a->q_user);
25216162Seric }
25316162Seric /*
254297Seric **  PRESCAN -- Prescan name and make it canonical
255297Seric **
2569374Seric **	Scans a name and turns it into a set of tokens.  This process
2579374Seric **	deletes blanks and comments (in parentheses).
258297Seric **
259297Seric **	This routine knows about quoted strings and angle brackets.
260297Seric **
261297Seric **	There are certain subtleties to this routine.  The one that
262297Seric **	comes to mind now is that backslashes on the ends of names
263297Seric **	are silently stripped off; this is intentional.  The problem
264297Seric **	is that some versions of sndmsg (like at LBL) set the kill
265297Seric **	character to something other than @ when reading addresses;
266297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
267297Seric **	berknet mailer.
268297Seric **
269297Seric **	Parameters:
270297Seric **		addr -- the name to chomp.
271297Seric **		delim -- the delimiter for the address, normally
272297Seric **			'\0' or ','; \0 is accepted in any case.
27315284Seric **			If '\t' then we are reading the .cf file.
27416914Seric **		pvpbuf -- place to put the saved text -- note that
27516914Seric **			the pointers are static.
276*58333Seric **		delimptr -- if non-NULL, set to the location of the
277*58333Seric **			terminating delimiter.
278297Seric **
279297Seric **	Returns:
2803149Seric **		A pointer to a vector of tokens.
281297Seric **		NULL on error.
282297Seric */
283297Seric 
2848078Seric /* states and character types */
2858078Seric # define OPR		0	/* operator */
2868078Seric # define ATM		1	/* atom */
2878078Seric # define QST		2	/* in quoted string */
2888078Seric # define SPC		3	/* chewing up spaces */
2898078Seric # define ONE		4	/* pick up one character */
2903149Seric 
2918078Seric # define NSTATES	5	/* number of states */
2928078Seric # define TYPE		017	/* mask to select state type */
2938078Seric 
2948078Seric /* meta bits for table */
2958078Seric # define M		020	/* meta character; don't pass through */
2968078Seric # define B		040	/* cause a break */
2978078Seric # define MB		M|B	/* meta-break */
2988078Seric 
2998078Seric static short StateTab[NSTATES][NSTATES] =
3008078Seric {
3018087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
3029051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
3039051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
3049051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
3058078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
3068078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
3078078Seric };
3088078Seric 
3098078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
3108078Seric 
3113149Seric char **
312*58333Seric prescan(addr, delim, pvpbuf, delimptr)
313297Seric 	char *addr;
314297Seric 	char delim;
31516914Seric 	char pvpbuf[];
316*58333Seric 	char **delimptr;
317297Seric {
318297Seric 	register char *p;
3198078Seric 	register char *q;
3209346Seric 	register int c;
3213149Seric 	char **avp;
322297Seric 	bool bslashmode;
323297Seric 	int cmntcnt;
3248423Seric 	int anglecnt;
3253149Seric 	char *tok;
3268078Seric 	int state;
3278078Seric 	int newstate;
3288078Seric 	static char *av[MAXATOM+1];
32956678Seric 	extern int errno;
330297Seric 
33115253Seric 	/* make sure error messages don't have garbage on them */
33215253Seric 	errno = 0;
33315253Seric 
33416914Seric 	q = pvpbuf;
3353149Seric 	bslashmode = FALSE;
3367800Seric 	cmntcnt = 0;
3378423Seric 	anglecnt = 0;
3383149Seric 	avp = av;
33956678Seric 	state = ATM;
3408078Seric 	c = NOCHAR;
3418078Seric 	p = addr;
34256764Seric 	if (tTd(22, 11))
343297Seric 	{
3448078Seric 		printf("prescan: ");
3458078Seric 		xputs(p);
34623109Seric 		(void) putchar('\n');
3478078Seric 	}
3488078Seric 
3498078Seric 	do
3508078Seric 	{
3513149Seric 		/* read a token */
3523149Seric 		tok = q;
3538078Seric 		for (;;)
354297Seric 		{
3558078Seric 			/* store away any old lookahead character */
3568078Seric 			if (c != NOCHAR)
3578078Seric 			{
35815284Seric 				/* see if there is room */
35916914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3608078Seric 				{
36158151Seric 					usrerr("553 Address too long");
362*58333Seric 					if (delimptr != NULL)
363*58333Seric 						*delimptr = p;
3648078Seric 					return (NULL);
3658078Seric 				}
36615284Seric 
36715284Seric 				/* squirrel it away */
3688078Seric 				*q++ = c;
3698078Seric 			}
3708078Seric 
3718078Seric 			/* read a new input character */
3728078Seric 			c = *p++;
37357631Seric 			if (c == '\0')
37456764Seric 			{
37556764Seric 				/* diagnose and patch up bad syntax */
37656764Seric 				if (state == QST)
37756764Seric 				{
37858151Seric 					usrerr("553 Unbalanced '\"'");
37956764Seric 					c = '"';
38056764Seric 				}
38156764Seric 				else if (cmntcnt > 0)
38256764Seric 				{
38358151Seric 					usrerr("553 Unbalanced '('");
38456764Seric 					c = ')';
38556764Seric 				}
38656764Seric 				else if (anglecnt > 0)
38756764Seric 				{
38856764Seric 					c = '>';
38958151Seric 					usrerr("553 Unbalanced '<'");
39056764Seric 				}
39156764Seric 				else
39256764Seric 					break;
39315284Seric 
39456764Seric 				p--;
39556764Seric 			}
39657631Seric 			else if (c == delim && anglecnt <= 0 &&
39757631Seric 					cmntcnt <= 0 && state != QST)
39857631Seric 				break;
39956764Seric 
4008078Seric 			if (tTd(22, 101))
4018078Seric 				printf("c=%c, s=%d; ", c, state);
4028078Seric 
4033149Seric 			/* chew up special characters */
4043149Seric 			*q = '\0';
4053149Seric 			if (bslashmode)
4063149Seric 			{
40724944Seric 				/* kludge \! for naive users */
40858061Seric 				if (cmntcnt > 0)
40958061Seric 					c = NOCHAR;
41058061Seric 				else if (c != '!')
41156678Seric 					*q++ = '\\';
4123149Seric 				bslashmode = FALSE;
41356678Seric 				continue;
4143149Seric 			}
41556678Seric 
41656678Seric 			if (c == '\\')
4173149Seric 			{
4183149Seric 				bslashmode = TRUE;
4198078Seric 				c = NOCHAR;
42056678Seric 				continue;
4213149Seric 			}
42256678Seric 			else if (state == QST)
4238514Seric 			{
4248514Seric 				/* do nothing, just avoid next clauses */
4258514Seric 			}
4268078Seric 			else if (c == '(')
4274100Seric 			{
4288078Seric 				cmntcnt++;
4298078Seric 				c = NOCHAR;
4304100Seric 			}
4318078Seric 			else if (c == ')')
4323149Seric 			{
4338078Seric 				if (cmntcnt <= 0)
4343149Seric 				{
43558151Seric 					usrerr("553 Unbalanced ')'");
436*58333Seric 					if (delimptr != NULL)
437*58333Seric 						*delimptr = p;
4388078Seric 					return (NULL);
4393149Seric 				}
4408078Seric 				else
4418078Seric 					cmntcnt--;
4428078Seric 			}
4438078Seric 			else if (cmntcnt > 0)
4448078Seric 				c = NOCHAR;
4458423Seric 			else if (c == '<')
4468423Seric 				anglecnt++;
4478423Seric 			else if (c == '>')
4488423Seric 			{
4498423Seric 				if (anglecnt <= 0)
4508423Seric 				{
45158151Seric 					usrerr("553 Unbalanced '>'");
452*58333Seric 					if (delimptr != NULL)
453*58333Seric 						*delimptr = p;
4548423Seric 					return (NULL);
4558423Seric 				}
4568423Seric 				anglecnt--;
4578423Seric 			}
45858050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
45911423Seric 				c = ' ';
4603149Seric 
4618078Seric 			if (c == NOCHAR)
4628078Seric 				continue;
4633149Seric 
4648078Seric 			/* see if this is end of input */
46511405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4663149Seric 				break;
4673149Seric 
4688078Seric 			newstate = StateTab[state][toktype(c)];
4698078Seric 			if (tTd(22, 101))
4708078Seric 				printf("ns=%02o\n", newstate);
4718078Seric 			state = newstate & TYPE;
4728078Seric 			if (bitset(M, newstate))
4738078Seric 				c = NOCHAR;
4748078Seric 			if (bitset(B, newstate))
4754228Seric 				break;
476297Seric 		}
4773149Seric 
4783149Seric 		/* new token */
4798078Seric 		if (tok != q)
4801378Seric 		{
4818078Seric 			*q++ = '\0';
4828078Seric 			if (tTd(22, 36))
483297Seric 			{
4848078Seric 				printf("tok=");
4858078Seric 				xputs(tok);
48623109Seric 				(void) putchar('\n');
487297Seric 			}
4888078Seric 			if (avp >= &av[MAXATOM])
489297Seric 			{
49058151Seric 				syserr("553 prescan: too many tokens");
491*58333Seric 				if (delimptr != NULL)
492*58333Seric 					*delimptr = p;
4938078Seric 				return (NULL);
494297Seric 			}
4958078Seric 			*avp++ = tok;
496297Seric 		}
4978423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4983149Seric 	*avp = NULL;
499*58333Seric 	p--;
500*58333Seric 	if (delimptr != NULL)
501*58333Seric 		*delimptr = p;
50256764Seric 	if (tTd(22, 12))
50356764Seric 	{
50456764Seric 		printf("prescan==>");
50556764Seric 		printav(av);
50656764Seric 	}
50756764Seric 	if (av[0] != NULL)
5083149Seric 		return (av);
5093149Seric 	return (NULL);
5103149Seric }
5113149Seric /*
5123149Seric **  TOKTYPE -- return token type
5133149Seric **
5143149Seric **	Parameters:
5153149Seric **		c -- the character in question.
5163149Seric **
5173149Seric **	Returns:
5183149Seric **		Its type.
5193149Seric **
5203149Seric **	Side Effects:
5213149Seric **		none.
5223149Seric */
523297Seric 
5243149Seric toktype(c)
52558050Seric 	register int c;
5263149Seric {
5273380Seric 	static char buf[50];
5283382Seric 	static bool firstime = TRUE;
5293380Seric 
5303382Seric 	if (firstime)
5313380Seric 	{
5323382Seric 		firstime = FALSE;
53358050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5347005Seric 		(void) strcat(buf, DELIMCHARS);
5353380Seric 	}
53658050Seric 	c &= 0377;
53756327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5388078Seric 		return (ONE);
5398078Seric 	if (c == '"')
5408078Seric 		return (QST);
54158050Seric 	if ((c & 0340) == 0200)
54258050Seric 		return (OPR);
5434100Seric 	if (!isascii(c))
5448078Seric 		return (ATM);
5458078Seric 	if (isspace(c) || c == ')')
5468078Seric 		return (SPC);
54758050Seric 	if (strchr(buf, c) != NULL)
5488078Seric 		return (OPR);
5498078Seric 	return (ATM);
5503149Seric }
5513149Seric /*
5523149Seric **  REWRITE -- apply rewrite rules to token vector.
5533149Seric **
5544476Seric **	This routine is an ordered production system.  Each rewrite
5554476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5564476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5574476Seric **
5584476Seric **	For each rewrite rule, 'avp' points the address vector we
5594476Seric **	are trying to match against, and 'pvp' points to the pattern.
5608058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5619585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5629585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5634476Seric **
5644476Seric **	When a match between avp & pvp does not match, we try to
5659585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5664476Seric **	we must also back out the match in mvp.  If we reach a
5678058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5688058Seric **	over again.
5694476Seric **
5704476Seric **	When we finally match, we rewrite the address vector
5714476Seric **	and try over again.
5724476Seric **
5733149Seric **	Parameters:
5743149Seric **		pvp -- pointer to token vector.
5753149Seric **
5763149Seric **	Returns:
5773149Seric **		none.
5783149Seric **
5793149Seric **	Side Effects:
5803149Seric **		pvp is modified.
5813149Seric */
5822091Seric 
5833149Seric struct match
5843149Seric {
5854468Seric 	char	**first;	/* first token matched */
5864468Seric 	char	**last;		/* last token matched */
5873149Seric };
5883149Seric 
5894468Seric # define MAXMATCH	9	/* max params per rewrite */
5903149Seric 
5913149Seric 
5924070Seric rewrite(pvp, ruleset)
5933149Seric 	char **pvp;
5944070Seric 	int ruleset;
5953149Seric {
5963149Seric 	register char *ap;		/* address pointer */
5973149Seric 	register char *rp;		/* rewrite pointer */
5983149Seric 	register char **avp;		/* address vector pointer */
5993149Seric 	register char **rvp;		/* rewrite vector pointer */
6008058Seric 	register struct match *mlp;	/* cur ptr into mlist */
6018058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
60256678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
6033149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
6043149Seric 
6059279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
6063149Seric 	{
6078959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
60856678Seric 		printav(pvp);
6093149Seric 	}
61056678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
61156326Seric 	{
61258151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
61356326Seric 		return;
61456326Seric 	}
61556678Seric 	if (pvp == NULL)
61656678Seric 		return;
61756326Seric 
6183149Seric 	/*
61956678Seric 	**  Run through the list of rewrite rules, applying
62056678Seric 	**	any that match.
6213149Seric 	*/
6223149Seric 
6234070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6243149Seric 	{
62556678Seric 		int loopcount = 0;
62656678Seric 
6277675Seric 		if (tTd(21, 12))
628297Seric 		{
6298069Seric 			printf("-----trying rule:");
63056678Seric 			printav(rwr->r_lhs);
6313149Seric 		}
6323149Seric 
6333149Seric 		/* try to match on this rule */
6344468Seric 		mlp = mlist;
6358058Seric 		rvp = rwr->r_lhs;
6368058Seric 		avp = pvp;
6378058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
6383149Seric 		{
63956678Seric 			if (++loopcount > 100)
64052637Seric 			{
64158151Seric 				syserr("554 Infinite loop in ruleset %d", ruleset);
64256678Seric 				printf("workspace: ");
64356678Seric 				printav(pvp);
64452637Seric 				break;
64552637Seric 			}
6463149Seric 			rp = *rvp;
6478058Seric 			if (tTd(21, 35))
6488058Seric 			{
64957532Seric 				printf("rp=");
65057531Seric 				xputs(rp);
65157532Seric 				printf(", ap=");
6528058Seric 				xputs(ap);
6538069Seric 				printf("\n");
6548058Seric 			}
65556678Seric 			if (rp == NULL)
65656326Seric 			{
6573149Seric 				/* end-of-pattern before end-of-address */
6588058Seric 				goto backup;
65956678Seric 			}
66058173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
66158173Seric 			    (*rp & 0377) != CANONHOST)
66256326Seric 			{
66356678Seric 				/* end-of-input */
66456678Seric 				break;
665297Seric 			}
66656326Seric 
66758050Seric 			switch (*rp & 0377)
6688058Seric 			{
66956678Seric 				register STAB *s;
67056326Seric 
67156678Seric 			  case MATCHCLASS:
67256678Seric 			  case MATCHNCLASS:
67356678Seric 				/* match any token in (not in) a class */
67456678Seric 				s = stab(ap, ST_CLASS, ST_FIND);
67556678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
67656326Seric 				{
67758050Seric 					if ((*rp & 0377) == MATCHCLASS)
6789585Seric 						goto backup;
67956326Seric 				}
68058050Seric 				else if ((*rp & 0377) == MATCHNCLASS)
68156678Seric 					goto backup;
6824060Seric 
68356678Seric 				/* explicit fall-through */
68456678Seric 
68556678Seric 			  case MATCHONE:
68656678Seric 			  case MATCHANY:
68756678Seric 				/* match exactly one token */
68856678Seric 				mlp->first = avp;
68956678Seric 				mlp->last = avp++;
6908058Seric 				mlp++;
69156678Seric 				break;
6928058Seric 
69356678Seric 			  case MATCHZANY:
69456678Seric 				/* match zero or more tokens */
69556678Seric 				mlp->first = avp;
69656678Seric 				mlp->last = avp - 1;
69756678Seric 				mlp++;
69856678Seric 				break;
69956326Seric 
70058173Seric 			  case CANONHOST:
70158173Seric 				/* match zero tokens */
70258173Seric 				break;
70358173Seric 
70456678Seric 			  default:
70556678Seric 				/* must have exact match */
70656678Seric 				if (strcasecmp(rp, ap))
7078058Seric 					goto backup;
7084468Seric 				avp++;
70956678Seric 				break;
7103149Seric 			}
7113149Seric 
71256678Seric 			/* successful match on this token */
7133149Seric 			rvp++;
7143149Seric 			continue;
7153149Seric 
71656678Seric 		  backup:
71756678Seric 			/* match failed -- back up */
71856678Seric 			while (--rvp >= rwr->r_lhs)
7193149Seric 			{
72056678Seric 				rp = *rvp;
72158050Seric 				if ((*rp & 0377) == MATCHANY ||
72258050Seric 				    (*rp & 0377) == MATCHZANY)
7234468Seric 				{
72456678Seric 					/* extend binding and continue */
72556678Seric 					avp = ++mlp[-1].last;
72656678Seric 					avp++;
72756678Seric 					rvp++;
72856678Seric 					break;
7294468Seric 				}
73056678Seric 				avp--;
73158050Seric 				if ((*rp & 0377) == MATCHONE ||
73258050Seric 				    (*rp & 0377) == MATCHCLASS ||
73358050Seric 				    (*rp & 0377) == MATCHNCLASS)
73456678Seric 				{
73556678Seric 					/* back out binding */
73656678Seric 					mlp--;
73756678Seric 				}
7383149Seric 			}
7393149Seric 
74056678Seric 			if (rvp < rwr->r_lhs)
74156678Seric 			{
74256678Seric 				/* total failure to match */
74356326Seric 				break;
7443149Seric 			}
745297Seric 		}
7463149Seric 
7473149Seric 		/*
74856678Seric 		**  See if we successfully matched
7493149Seric 		*/
7503149Seric 
75156678Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
7523149Seric 		{
7539374Seric 			if (tTd(21, 10))
7549374Seric 				printf("----- rule fails\n");
7559374Seric 			rwr = rwr->r_next;
7569374Seric 			continue;
7579374Seric 		}
7583149Seric 
7599374Seric 		rvp = rwr->r_rhs;
7609374Seric 		if (tTd(21, 12))
7619374Seric 		{
7629374Seric 			printf("-----rule matches:");
76356678Seric 			printav(rvp);
7649374Seric 		}
7659374Seric 
7669374Seric 		rp = *rvp;
76758050Seric 		if ((*rp & 0377) == CANONUSER)
7689374Seric 		{
7699374Seric 			rvp++;
7709374Seric 			rwr = rwr->r_next;
7719374Seric 		}
77258050Seric 		else if ((*rp & 0377) == CANONHOST)
7739374Seric 		{
7749374Seric 			rvp++;
7759374Seric 			rwr = NULL;
7769374Seric 		}
77758050Seric 		else if ((*rp & 0377) == CANONNET)
7789374Seric 			rwr = NULL;
7799374Seric 
7809374Seric 		/* substitute */
7819374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
7829374Seric 		{
7839374Seric 			register struct match *m;
7849374Seric 			register char **pp;
7859374Seric 
7868058Seric 			rp = *rvp;
78758050Seric 			if ((*rp & 0377) == MATCHREPL)
7888058Seric 			{
78916914Seric 				/* substitute from LHS */
79016914Seric 				m = &mlist[rp[1] - '1'];
79156678Seric 				if (m < mlist || m >= mlp)
7929374Seric 				{
79358151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
79456326Seric 						ruleset, rp[1]);
7959374Seric 					return;
7969374Seric 				}
79716914Seric 				if (tTd(21, 15))
79816914Seric 				{
79916914Seric 					printf("$%c:", rp[1]);
80016914Seric 					pp = m->first;
80156678Seric 					while (pp <= m->last)
80216914Seric 					{
80316914Seric 						printf(" %x=\"", *pp);
80416914Seric 						(void) fflush(stdout);
80516914Seric 						printf("%s\"", *pp++);
80616914Seric 					}
80716914Seric 					printf("\n");
80816914Seric 				}
8099374Seric 				pp = m->first;
81056678Seric 				while (pp <= m->last)
8113149Seric 				{
81216914Seric 					if (avp >= &npvp[MAXATOM])
81356678Seric 					{
81458151Seric 						syserr("554 rewrite: expansion too long");
81556678Seric 						return;
81656678Seric 					}
81716914Seric 					*avp++ = *pp++;
8183149Seric 				}
8193149Seric 			}
82016914Seric 			else
8218226Seric 			{
82216914Seric 				/* vanilla replacement */
8239374Seric 				if (avp >= &npvp[MAXATOM])
82416889Seric 				{
82556678Seric 	toolong:
82658151Seric 					syserr("554 rewrite: expansion too long");
82716889Seric 					return;
82816889Seric 				}
82956678Seric 				*avp++ = rp;
8308226Seric 			}
8319374Seric 		}
8329374Seric 		*avp++ = NULL;
83316914Seric 
83416914Seric 		/*
83556678Seric 		**  Check for any hostname/keyword lookups.
83616914Seric 		*/
83716914Seric 
83816914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
83916914Seric 		{
84056678Seric 			char **hbrvp;
84116914Seric 			char **xpvp;
84216914Seric 			int trsize;
84317473Seric 			char *olddelimchar;
84456678Seric 			char *replac;
84556678Seric 			int endtoken;
84656678Seric 			STAB *map;
84756678Seric 			char *mapname;
84856678Seric 			char **key_rvp;
84956678Seric 			char **arg_rvp;
85056678Seric 			char **default_rvp;
85156678Seric 			char buf[MAXNAME + 1];
85216914Seric 			char *pvpb1[MAXATOM + 1];
85356823Seric 			char *argvect[10];
85417174Seric 			char pvpbuf[PSBUFSIZE];
85516914Seric 
85658050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
85758050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
85816914Seric 				continue;
85916914Seric 
86016914Seric 			/*
86156678Seric 			**  Got a hostname/keyword lookup.
86216914Seric 			**
86316914Seric 			**	This could be optimized fairly easily.
86416914Seric 			*/
86516914Seric 
86616914Seric 			hbrvp = rvp;
86758050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
86856327Seric 			{
86956678Seric 				endtoken = HOSTEND;
87056678Seric 				mapname = "host";
87156327Seric 			}
87256326Seric 			else
87356327Seric 			{
87456678Seric 				endtoken = LOOKUPEND;
87556678Seric 				mapname = *++rvp;
87656327Seric 			}
87756678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
87856678Seric 			if (map == NULL)
87958151Seric 				syserr("554 rewrite: map %s not found", mapname);
88056678Seric 
88156678Seric 			/* extract the match part */
88256678Seric 			key_rvp = ++rvp;
88356823Seric 			default_rvp = NULL;
88456823Seric 			arg_rvp = argvect;
88556823Seric 			xpvp = NULL;
88656823Seric 			replac = pvpbuf;
88758050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
88853654Seric 			{
88958050Seric 				int nodetype = **rvp & 0377;
89056823Seric 
89156823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
89256678Seric 				{
89356823Seric 					rvp++;
89456823Seric 					continue;
89556823Seric 				}
89656823Seric 
89756823Seric 				*rvp++ = NULL;
89856823Seric 
89956823Seric 				if (xpvp != NULL)
90056823Seric 				{
90156823Seric 					cataddr(xpvp, replac,
90258082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
90358082Seric 						'\0');
90456823Seric 					*++arg_rvp = replac;
90556823Seric 					replac += strlen(replac) + 1;
90656823Seric 					xpvp = NULL;
90756823Seric 				}
90856823Seric 				switch (nodetype)
90956823Seric 				{
91056678Seric 				  case CANONHOST:
91156823Seric 					xpvp = rvp;
91256678Seric 					break;
91356678Seric 
91456678Seric 				  case CANONUSER:
91556678Seric 					default_rvp = rvp;
91656678Seric 					break;
91756678Seric 				}
91853654Seric 			}
91916914Seric 			if (*rvp != NULL)
92016914Seric 				*rvp++ = NULL;
92156823Seric 			if (xpvp != NULL)
92256823Seric 			{
92356823Seric 				cataddr(xpvp, replac,
92458082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
92558082Seric 					'\0');
92656823Seric 				*++arg_rvp = replac;
92756823Seric 			}
92856823Seric 			*++arg_rvp = NULL;
92916914Seric 
93016914Seric 			/* save the remainder of the input string */
93116914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
93216914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
93316914Seric 
93456678Seric 			/* look it up */
93558082Seric 			cataddr(key_rvp, buf, sizeof buf, '\0');
93656823Seric 			argvect[0] = buf;
93756678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
93856836Seric 			{
93956836Seric 				int bsize = sizeof buf - 1;
94056836Seric 
94156836Seric 				if (map->s_map.map_app != NULL)
94256836Seric 					bsize -= strlen(map->s_map.map_app);
94356836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
94456823Seric 						buf, sizeof buf - 1, argvect);
94556836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
94656836Seric 					strcat(replac, map->s_map.map_app);
94756836Seric 			}
94853654Seric 			else
94956678Seric 				replac = NULL;
95056678Seric 
95156678Seric 			/* if no replacement, use default */
95256823Seric 			if (replac == NULL && default_rvp != NULL)
95356823Seric 			{
95456823Seric 				char buf2[sizeof buf];
95556823Seric 
95656823Seric 				/* rewrite the default with % translations */
95758082Seric 				cataddr(default_rvp, buf2, sizeof buf2, '\0');
95856823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
95956823Seric 					argvect);
96056823Seric 				replac = buf;
96156823Seric 			}
96256823Seric 
96356678Seric 			if (replac == NULL)
96451317Seric 			{
96556823Seric 				xpvp = key_rvp;
96653654Seric 			}
96756678Seric 			else
96853654Seric 			{
96956678Seric 				/* scan the new replacement */
970*58333Seric 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
97153654Seric 				if (xpvp == NULL)
97251317Seric 				{
97358151Seric 					syserr("553 rewrite: cannot prescan map value: %s", replac);
97456678Seric 					return;
97556678Seric 				}
97651317Seric 			}
97751317Seric 
97816914Seric 			/* append it to the token list */
97956678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
98056678Seric 			{
98117174Seric 				*avp++ = newstr(*xpvp);
98216920Seric 				if (avp >= &npvp[MAXATOM])
98316914Seric 					goto toolong;
98417174Seric 			}
98516914Seric 
98616914Seric 			/* restore the old trailing information */
98756678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
98816920Seric 				if (avp >= &npvp[MAXATOM])
98916914Seric 					goto toolong;
99017174Seric 
99156678Seric 			break;
99216914Seric 		}
99316914Seric 
99416914Seric 		/*
99516914Seric 		**  Check for subroutine calls.
99616914Seric 		*/
99716914Seric 
99858050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
99956678Seric 		{
100056678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
100156678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
100256678Seric 			if (tTd(21, 3))
100356678Seric 				printf("-----callsubr %s\n", npvp[1]);
100456678Seric 			rewrite(pvp, atoi(npvp[1]));
100556678Seric 		}
100656678Seric 		else
100756678Seric 		{
100817348Seric 			bcopy((char *) npvp, (char *) pvp,
100916900Seric 				(int) (avp - npvp) * sizeof *avp);
101056678Seric 		}
10119374Seric 		if (tTd(21, 4))
10129374Seric 		{
10139374Seric 			printf("rewritten as:");
101456678Seric 			printav(pvp);
10159374Seric 		}
1016297Seric 	}
10178069Seric 
10189279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
10198069Seric 	{
10208959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
102156678Seric 		printav(pvp);
10228069Seric 	}
10233149Seric }
10243149Seric /*
10253149Seric **  BUILDADDR -- build address from token vector.
10263149Seric **
10273149Seric **	Parameters:
10283149Seric **		tv -- token vector.
10293149Seric **		a -- pointer to address descriptor to fill.
10303149Seric **			If NULL, one will be allocated.
10313149Seric **
10323149Seric **	Returns:
10334279Seric **		NULL if there was an error.
10344279Seric **		'a' otherwise.
10353149Seric **
10363149Seric **	Side Effects:
10373149Seric **		fills in 'a'
10383149Seric */
10393149Seric 
104057249Seric struct errcodes
104157249Seric {
104257249Seric 	char	*ec_name;		/* name of error code */
104357249Seric 	int	ec_code;		/* numeric code */
104457249Seric } ErrorCodes[] =
104557249Seric {
104657249Seric 	"usage",	EX_USAGE,
104757249Seric 	"nouser",	EX_NOUSER,
104857249Seric 	"nohost",	EX_NOHOST,
104957249Seric 	"unavailable",	EX_UNAVAILABLE,
105057249Seric 	"software",	EX_SOFTWARE,
105157249Seric 	"tempfail",	EX_TEMPFAIL,
105257249Seric 	"protocol",	EX_PROTOCOL,
105357249Seric #ifdef EX_CONFIG
105457249Seric 	"config",	EX_CONFIG,
105557249Seric #endif
105657249Seric 	NULL,		EX_UNAVAILABLE,
105757249Seric };
105857249Seric 
105956678Seric ADDRESS *
10603149Seric buildaddr(tv, a)
10613149Seric 	register char **tv;
10623149Seric 	register ADDRESS *a;
10633149Seric {
10643149Seric 	struct mailer **mp;
10653149Seric 	register struct mailer *m;
106658008Seric 	char *bp;
106758008Seric 	int spaceleft;
106857402Seric 	static char buf[MAXNAME];
10693149Seric 
10703149Seric 	if (a == NULL)
10713149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
107216889Seric 	bzero((char *) a, sizeof *a);
10733149Seric 
10743149Seric 	/* figure out what net/mailer to use */
107558050Seric 	if ((**tv & 0377) != CANONNET)
10764279Seric 	{
107758151Seric 		syserr("554 buildaddr: no net");
10784279Seric 		return (NULL);
10794279Seric 	}
10803149Seric 	tv++;
108133725Sbostic 	if (!strcasecmp(*tv, "error"))
10824279Seric 	{
108358050Seric 		if ((**++tv & 0377) == CANONHOST)
108410183Seric 		{
108557249Seric 			register struct errcodes *ep;
108657249Seric 
108758050Seric 			if (isascii(**++tv) && isdigit(**tv))
108857249Seric 			{
108957249Seric 				setstat(atoi(*tv));
109057249Seric 			}
109157249Seric 			else
109257249Seric 			{
109357249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
109457249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
109557249Seric 						break;
109657249Seric 				setstat(ep->ec_code);
109757249Seric 			}
109810183Seric 			tv++;
109910183Seric 		}
110058050Seric 		if ((**tv & 0377) != CANONUSER)
110158151Seric 			syserr("554 buildaddr: error: no user");
110258082Seric 		cataddr(++tv, buf, sizeof buf, ' ');
110358082Seric 		stripquotes(buf);
11044279Seric 		usrerr(buf);
11054279Seric 		return (NULL);
11064279Seric 	}
110757402Seric 
11084598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
11093149Seric 	{
111033725Sbostic 		if (!strcasecmp(m->m_name, *tv))
11113149Seric 			break;
11123149Seric 	}
11133149Seric 	if (m == NULL)
11144279Seric 	{
111558151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
11164279Seric 		return (NULL);
11174279Seric 	}
11184598Seric 	a->q_mailer = m;
11193149Seric 
11203149Seric 	/* figure out what host (if any) */
112156678Seric 	tv++;
112258139Seric 	if (!bitnset(M_LOCALMAILER, m->m_flags))
11233149Seric 	{
112458050Seric 		if ((**tv & 0377) != CANONHOST)
11254279Seric 		{
112658151Seric 			syserr("554 buildaddr: no host");
11274279Seric 			return (NULL);
11284279Seric 		}
112958008Seric 		bp = buf;
113058008Seric 		spaceleft = sizeof buf - 1;
113158050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
113258008Seric 		{
113358008Seric 			int i = strlen(*tv);
113458008Seric 
113558008Seric 			if (i > spaceleft)
113658008Seric 			{
113758008Seric 				/* out of space for this address */
113858008Seric 				if (spaceleft >= 0)
113958151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
114058008Seric 						buf);
114158008Seric 				i = spaceleft;
114258008Seric 				spaceleft = 0;
114358008Seric 			}
114458008Seric 			if (i <= 0)
114558008Seric 				continue;
114658008Seric 			bcopy(*tv, bp, i);
114758008Seric 			bp += i;
114858008Seric 			spaceleft -= i;
114958008Seric 		}
115058010Seric 		*bp = '\0';
11515704Seric 		a->q_host = newstr(buf);
11523149Seric 	}
115357249Seric 	else
115457249Seric 		a->q_host = NULL;
11553149Seric 
11563149Seric 	/* figure out the user */
115758050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
11584279Seric 	{
115958151Seric 		syserr("554 buildaddr: no user");
11604279Seric 		return (NULL);
11614279Seric 	}
116257402Seric 	tv++;
116351317Seric 
116457402Seric 	/* do special mapping for local mailer */
116557402Seric 	if (m == LocalMailer && *tv != NULL)
116657402Seric 	{
116757454Seric 		register char *p = *tv;
116857454Seric 
116957454Seric 		if (*p == '"')
117057454Seric 			p++;
117157454Seric 		if (*p == '|')
117257402Seric 			a->q_mailer = m = ProgMailer;
117357454Seric 		else if (*p == '/')
117457402Seric 			a->q_mailer = m = FileMailer;
117557454Seric 		else if (*p == ':')
117657402Seric 		{
117757402Seric 			/* may be :include: */
117858082Seric 			cataddr(tv, buf, sizeof buf, '\0');
117958008Seric 			stripquotes(buf);
118058008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
118158008Seric 			{
118258008Seric 				/* if :include:, don't need further rewriting */
118357402Seric 				a->q_mailer = m = InclMailer;
118458008Seric 				a->q_user = &buf[9];
118558008Seric 				return (a);
118658008Seric 			}
118757402Seric 		}
118857402Seric 	}
118957402Seric 
119058008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
119158008Seric 	{
119258008Seric 		tv++;
119358008Seric 		a->q_flags |= QNOTREMOTE;
119458008Seric 	}
119558008Seric 
119619040Seric 	/* rewrite according recipient mailer rewriting rules */
119757402Seric 	rewrite(tv, 2);
119858020Seric 	if (m->m_re_rwset > 0)
119958020Seric 		rewrite(tv, m->m_re_rwset);
120019040Seric 	rewrite(tv, 4);
120119040Seric 
120219040Seric 	/* save the result for the command line/RCPT argument */
120358082Seric 	cataddr(tv, buf, sizeof buf, '\0');
12043149Seric 	a->q_user = buf;
12053149Seric 
12063149Seric 	return (a);
12073149Seric }
12083188Seric /*
12094228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
12104228Seric **
12114228Seric **	Parameters:
12124228Seric **		pvp -- parameter vector to rebuild.
12134228Seric **		buf -- buffer to build the string into.
12144228Seric **		sz -- size of buf.
121558082Seric **		spacesub -- the space separator character; if null,
121658082Seric **			use SpaceSub.
12174228Seric **
12184228Seric **	Returns:
12194228Seric **		none.
12204228Seric **
12214228Seric **	Side Effects:
12224228Seric **		Destroys buf.
12234228Seric */
12244228Seric 
122558082Seric cataddr(pvp, buf, sz, spacesub)
12264228Seric 	char **pvp;
12274228Seric 	char *buf;
12284228Seric 	register int sz;
122958082Seric 	char spacesub;
12304228Seric {
12314228Seric 	bool oatomtok = FALSE;
123256678Seric 	bool natomtok = FALSE;
12334228Seric 	register int i;
12344228Seric 	register char *p;
12354228Seric 
123658082Seric 	if (spacesub == '\0')
123758082Seric 		spacesub = SpaceSub;
123858082Seric 
12398423Seric 	if (pvp == NULL)
12408423Seric 	{
124123109Seric 		(void) strcpy(buf, "");
12428423Seric 		return;
12438423Seric 	}
12444228Seric 	p = buf;
124511156Seric 	sz -= 2;
12464228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
12474228Seric 	{
12488078Seric 		natomtok = (toktype(**pvp) == ATM);
12494228Seric 		if (oatomtok && natomtok)
125058082Seric 			*p++ = spacesub;
12514228Seric 		(void) strcpy(p, *pvp);
12524228Seric 		oatomtok = natomtok;
12534228Seric 		p += i;
125411156Seric 		sz -= i + 1;
12554228Seric 		pvp++;
12564228Seric 	}
12574228Seric 	*p = '\0';
12584228Seric }
12594228Seric /*
12603188Seric **  SAMEADDR -- Determine if two addresses are the same
12613188Seric **
12623188Seric **	This is not just a straight comparison -- if the mailer doesn't
12633188Seric **	care about the host we just ignore it, etc.
12643188Seric **
12653188Seric **	Parameters:
12663188Seric **		a, b -- pointers to the internal forms to compare.
12673188Seric **
12683188Seric **	Returns:
12693188Seric **		TRUE -- they represent the same mailbox.
12703188Seric **		FALSE -- they don't.
12713188Seric **
12723188Seric **	Side Effects:
12733188Seric **		none.
12743188Seric */
12753188Seric 
12763188Seric bool
12779374Seric sameaddr(a, b)
12783188Seric 	register ADDRESS *a;
12793188Seric 	register ADDRESS *b;
12803188Seric {
12813188Seric 	/* if they don't have the same mailer, forget it */
12823188Seric 	if (a->q_mailer != b->q_mailer)
12833188Seric 		return (FALSE);
12843188Seric 
12853188Seric 	/* if the user isn't the same, we can drop out */
128656678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
12873188Seric 		return (FALSE);
12883188Seric 
12893188Seric 	/* if the mailer ignores hosts, we have succeeded! */
129058139Seric 	if (bitnset(M_LOCALMAILER, a->q_mailer->m_flags))
12913188Seric 		return (TRUE);
12923188Seric 
12933188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
12943188Seric 	if (a->q_host == NULL || b->q_host == NULL)
12953188Seric 		return (FALSE);
129656678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
12973188Seric 		return (FALSE);
12983188Seric 
12993188Seric 	return (TRUE);
13003188Seric }
13013234Seric /*
13023234Seric **  PRINTADDR -- print address (for debugging)
13033234Seric **
13043234Seric **	Parameters:
13053234Seric **		a -- the address to print
13063234Seric **		follow -- follow the q_next chain.
13073234Seric **
13083234Seric **	Returns:
13093234Seric **		none.
13103234Seric **
13113234Seric **	Side Effects:
13123234Seric **		none.
13133234Seric */
13143234Seric 
13153234Seric printaddr(a, follow)
13163234Seric 	register ADDRESS *a;
13173234Seric 	bool follow;
13183234Seric {
13195001Seric 	bool first = TRUE;
132057731Seric 	register MAILER *m;
132157731Seric 	MAILER pseudomailer;
13225001Seric 
13233234Seric 	while (a != NULL)
13243234Seric 	{
13255001Seric 		first = FALSE;
13264443Seric 		printf("%x=", a);
13274085Seric 		(void) fflush(stdout);
132857731Seric 
132957731Seric 		/* find the mailer -- carefully */
133057731Seric 		m = a->q_mailer;
133157731Seric 		if (m == NULL)
133257731Seric 		{
133357731Seric 			m = &pseudomailer;
133457731Seric 			m->m_mno = -1;
133557731Seric 			m->m_name = "NULL";
133657731Seric 		}
133757731Seric 
133840973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
133957731Seric 		       a->q_paddr, m->m_mno, m->m_name,
134040973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
13418181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
13428181Seric 		       a->q_alias);
13438181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
13448181Seric 		       a->q_fullname);
13454996Seric 
13463234Seric 		if (!follow)
13473234Seric 			return;
13484996Seric 		a = a->q_next;
13493234Seric 	}
13505001Seric 	if (first)
13514443Seric 		printf("[NULL]\n");
13523234Seric }
13534317Seric 
13547682Seric /*
13557682Seric **  REMOTENAME -- return the name relative to the current mailer
13567682Seric **
13577682Seric **	Parameters:
13587682Seric **		name -- the name to translate.
13598069Seric **		m -- the mailer that we want to do rewriting relative
13608069Seric **			to.
13618069Seric **		senderaddress -- if set, uses the sender rewriting rules
13628069Seric **			rather than the recipient rewriting rules.
136358020Seric **		header -- set if this address is in the header, rather
136458020Seric **			than an envelope header.
136510310Seric **		canonical -- if set, strip out any comment information,
136610310Seric **			etc.
136758020Seric **		e -- the current envelope.
13687682Seric **
13697682Seric **	Returns:
13707682Seric **		the text string representing this address relative to
13717682Seric **			the receiving mailer.
13727682Seric **
13737682Seric **	Side Effects:
13747682Seric **		none.
13757682Seric **
13767682Seric **	Warnings:
13777682Seric **		The text string returned is tucked away locally;
13787682Seric **			copy it if you intend to save it.
13797682Seric */
13807682Seric 
13817682Seric char *
138258020Seric remotename(name, m, senderaddress, header, canonical, e)
13837682Seric 	char *name;
138456678Seric 	struct mailer *m;
13858069Seric 	bool senderaddress;
138658020Seric 	bool header;
138710310Seric 	bool canonical;
138856678Seric 	register ENVELOPE *e;
13897682Seric {
13908069Seric 	register char **pvp;
13918069Seric 	char *fancy;
139256678Seric 	extern char *macvalue();
139356678Seric 	char *oldg = macvalue('g', e);
139458020Seric 	int rwset;
13957682Seric 	static char buf[MAXNAME];
13967682Seric 	char lbuf[MAXNAME];
139716914Seric 	char pvpbuf[PSBUFSIZE];
139856678Seric 	extern char **prescan();
139956678Seric 	extern char *crackaddr();
14007682Seric 
14017755Seric 	if (tTd(12, 1))
14027755Seric 		printf("remotename(%s)\n", name);
14037755Seric 
140410177Seric 	/* don't do anything if we are tagging it as special */
140558020Seric 	if (senderaddress)
140658020Seric 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
140758020Seric 	else
140858020Seric 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
140958020Seric 	if (rwset < 0)
141010177Seric 		return (name);
141110177Seric 
14127682Seric 	/*
14138181Seric 	**  Do a heuristic crack of this name to extract any comment info.
14148181Seric 	**	This will leave the name as a comment and a $g macro.
14157889Seric 	*/
14167889Seric 
141758036Seric 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
141858050Seric 		fancy = "\201g";
141910310Seric 	else
142010310Seric 		fancy = crackaddr(name);
14217889Seric 
14228181Seric 	/*
14238181Seric 	**  Turn the name into canonical form.
14248181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
14258181Seric 	**	If this only resolves to "user", and the "C" flag is
14268181Seric 	**	specified in the sending mailer, then the sender's
14278181Seric 	**	domain will be appended.
14288181Seric 	*/
14298181Seric 
1430*58333Seric 	pvp = prescan(name, '\0', pvpbuf, NULL);
14317889Seric 	if (pvp == NULL)
14327889Seric 		return (name);
14338181Seric 	rewrite(pvp, 3);
143456678Seric 	if (e->e_fromdomain != NULL)
14358181Seric 	{
14368181Seric 		/* append from domain to this address */
14378181Seric 		register char **pxp = pvp;
14388181Seric 
14399594Seric 		/* see if there is an "@domain" in the current name */
14408181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
14418181Seric 			pxp++;
14428181Seric 		if (*pxp == NULL)
14438181Seric 		{
14449594Seric 			/* no.... append the "@domain" from the sender */
144556678Seric 			register char **qxq = e->e_fromdomain;
14468181Seric 
14479594Seric 			while ((*pxp++ = *qxq++) != NULL)
14489594Seric 				continue;
144911726Seric 			rewrite(pvp, 3);
14508181Seric 		}
14518181Seric 	}
14528181Seric 
14538181Seric 	/*
14548959Seric 	**  Do more specific rewriting.
145556678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
145656678Seric 	**		a sender address or not.
14578181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
14588181Seric 	*/
14598181Seric 
14608069Seric 	if (senderaddress)
146156327Seric 		rewrite(pvp, 1);
14628069Seric 	else
146356327Seric 		rewrite(pvp, 2);
146458020Seric 	if (rwset > 0)
146558020Seric 		rewrite(pvp, rwset);
14667682Seric 
14678181Seric 	/*
14688959Seric 	**  Do any final sanitation the address may require.
14698959Seric 	**	This will normally be used to turn internal forms
14708959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
14718959Seric 	**	may be used as a default to the above rules.
14728959Seric 	*/
14738959Seric 
14748959Seric 	rewrite(pvp, 4);
14758959Seric 
14768959Seric 	/*
14778181Seric 	**  Now restore the comment information we had at the beginning.
14788181Seric 	*/
14798181Seric 
148058082Seric 	cataddr(pvp, lbuf, sizeof lbuf, '\0');
148156678Seric 	define('g', lbuf, e);
148256678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
148356678Seric 	define('g', oldg, e);
14847682Seric 
14857682Seric 	if (tTd(12, 1))
14867755Seric 		printf("remotename => `%s'\n", buf);
14877682Seric 	return (buf);
14887682Seric }
148951317Seric /*
149056678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
149151317Seric **
149251317Seric **	Parameters:
149356678Seric **		a -- the address to map (but just the user name part).
149456678Seric **		sendq -- the sendq in which to install any replacement
149556678Seric **			addresses.
149651317Seric **
149751317Seric **	Returns:
149851317Seric **		none.
149951317Seric */
150051317Seric 
150156678Seric maplocaluser(a, sendq, e)
150256678Seric 	register ADDRESS *a;
150356678Seric 	ADDRESS **sendq;
150456678Seric 	ENVELOPE *e;
150551317Seric {
150656678Seric 	register char **pvp;
150756678Seric 	register ADDRESS *a1 = NULL;
1508*58333Seric 	auto char *delimptr;
150956678Seric 	char pvpbuf[PSBUFSIZE];
151051317Seric 
151156678Seric 	if (tTd(29, 1))
151256678Seric 	{
151356678Seric 		printf("maplocaluser: ");
151456678Seric 		printaddr(a, FALSE);
151556678Seric 	}
1516*58333Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
151756678Seric 	if (pvp == NULL)
151856678Seric 		return;
151951317Seric 
152056678Seric 	rewrite(pvp, 5);
152158050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
152256678Seric 		return;
152351317Seric 
152456678Seric 	/* if non-null, mailer destination specified -- has it changed? */
152556678Seric 	a1 = buildaddr(pvp, NULL);
152656678Seric 	if (a1 == NULL || sameaddr(a, a1))
152756678Seric 		return;
152851317Seric 
152956678Seric 	/* mark old address as dead; insert new address */
153056678Seric 	a->q_flags |= QDONTSEND;
153157731Seric 	if (tTd(29, 5))
153257731Seric 	{
153357731Seric 		printf("maplocaluser: QDONTSEND ");
153457731Seric 		printaddr(a, FALSE);
153557731Seric 	}
153656678Seric 	a1->q_alias = a;
1537*58333Seric 	allocaddr(a1, 1, NULL, delimptr);
153856678Seric 	(void) recipient(a1, sendq, e);
153951317Seric }
1540