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*58438Seric static char sccsid[] = "@(#)parseaddr.c	6.23 (Berkeley) 03/04/93";
1133730Sbostic #endif /* not lint */
1222976Smiriam 
1356678Seric # include "sendmail.h"
14297Seric 
15297Seric /*
169888Seric **  PARSEADDR -- Parse an address
17297Seric **
18297Seric **	Parses an address and breaks it up into three parts: a
19297Seric **	net to transmit the message on, the host to transmit it
20297Seric **	to, and a user on that host.  These are loaded into an
212973Seric **	ADDRESS header with the values squirreled away if necessary.
22297Seric **	The "user" part may not be a real user; the process may
23297Seric **	just reoccur on that machine.  For example, on a machine
24297Seric **	with an arpanet connection, the address
25297Seric **		csvax.bill@berkeley
26297Seric **	will break up to a "user" of 'csvax.bill' and a host
27297Seric **	of 'berkeley' -- to be transmitted over the arpanet.
28297Seric **
29297Seric **	Parameters:
30297Seric **		addr -- the address to parse.
31297Seric **		a -- a pointer to the address descriptor buffer.
32297Seric **			If NULL, a header will be created.
33297Seric **		copyf -- determines what shall be copied:
34297Seric **			-1 -- don't copy anything.  The printname
35297Seric **				(q_paddr) is just addr, and the
36297Seric **				user & host are allocated internally
37297Seric **				to parse.
38297Seric **			0 -- copy out the parsed user & host, but
39297Seric **				don't copy the printname.
40297Seric **			+1 -- copy everything.
4111445Seric **		delim -- the character to terminate the address, passed
4211445Seric **			to prescan.
4358333Seric **		delimptr -- if non-NULL, set to the location of the
4458333Seric **			delim character that was found.
4556678Seric **		e -- the envelope that will contain this address.
46297Seric **
47297Seric **	Returns:
48297Seric **		A pointer to the address descriptor header (`a' if
49297Seric **			`a' is non-NULL).
50297Seric **		NULL on error.
51297Seric **
52297Seric **	Side Effects:
53297Seric **		none
54297Seric */
55297Seric 
569374Seric /* following delimiters are inherent to the internal algorithms */
5758050Seric # define DELIMCHARS	"\201()<>,;\\\"\r\n"	/* word delimiters */
582091Seric 
592973Seric ADDRESS *
6058333Seric parseaddr(addr, a, copyf, delim, delimptr, e)
61297Seric 	char *addr;
622973Seric 	register ADDRESS *a;
63297Seric 	int copyf;
6411445Seric 	char delim;
6558333Seric 	char **delimptr;
6656678Seric 	register ENVELOPE *e;
67297Seric {
683149Seric 	register char **pvp;
6958333Seric 	auto char *delimptrbuf;
7016914Seric 	char pvpbuf[PSBUFSIZE];
7156678Seric 	extern char **prescan();
7256678Seric 	extern ADDRESS *buildaddr();
7357388Seric 	extern bool invalidaddr();
74297Seric 
75297Seric 	/*
76297Seric 	**  Initialize and prescan address.
77297Seric 	*/
78297Seric 
7956678Seric 	e->e_to = addr;
807675Seric 	if (tTd(20, 1))
819888Seric 		printf("\n--parseaddr(%s)\n", addr);
823188Seric 
8357388Seric 	if (invalidaddr(addr))
8457388Seric 	{
8557388Seric 		if (tTd(20, 1))
8657388Seric 			printf("parseaddr-->bad address\n");
8757388Seric 		return NULL;
8857388Seric 	}
8957388Seric 
9058333Seric 	if (delimptr == NULL)
9158333Seric 		delimptr = &delimptrbuf;
9258333Seric 
9358333Seric 	pvp = prescan(addr, delim, pvpbuf, delimptr);
943149Seric 	if (pvp == NULL)
9556729Seric 	{
9656729Seric 		if (tTd(20, 1))
9756729Seric 			printf("parseaddr-->NULL\n");
98297Seric 		return (NULL);
9956729Seric 	}
100297Seric 
101297Seric 	/*
1023149Seric 	**  Apply rewriting rules.
1037889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
104297Seric 	*/
105297Seric 
1068181Seric 	rewrite(pvp, 3);
1074070Seric 	rewrite(pvp, 0);
108297Seric 
1093149Seric 	/*
1103149Seric 	**  See if we resolved to a real mailer.
1113149Seric 	*/
112297Seric 
11358050Seric 	if ((pvp[0][0] & 0377) != CANONNET)
1143149Seric 	{
1153149Seric 		setstat(EX_USAGE);
11658151Seric 		syserr("554 cannot resolve name");
1173149Seric 		return (NULL);
118297Seric 	}
119297Seric 
120297Seric 	/*
1213149Seric 	**  Build canonical address from pvp.
122297Seric 	*/
123297Seric 
1243149Seric 	a = buildaddr(pvp, a);
1254279Seric 	if (a == NULL)
1264279Seric 		return (NULL);
127297Seric 
128297Seric 	/*
1293149Seric 	**  Make local copies of the host & user and then
1303149Seric 	**  transport them out.
131297Seric 	*/
132297Seric 
13358333Seric 	allocaddr(a, copyf, addr, *delimptr);
13456678Seric 
13556678Seric 	/*
13656678Seric 	**  Compute return value.
13756678Seric 	*/
13856678Seric 
13956678Seric 	if (tTd(20, 1))
1408078Seric 	{
14156678Seric 		printf("parseaddr-->");
14256678Seric 		printaddr(a, FALSE);
14356678Seric 	}
14456678Seric 
14556678Seric 	return (a);
14656678Seric }
14756678Seric /*
14857388Seric **  INVALIDADDR -- check for address containing meta-characters
14957388Seric **
15057388Seric **	Parameters:
15157388Seric **		addr -- the address to check.
15257388Seric **
15357388Seric **	Returns:
15457388Seric **		TRUE -- if the address has any "wierd" characters
15557388Seric **		FALSE -- otherwise.
15657388Seric */
15757388Seric 
15857388Seric bool
15957388Seric invalidaddr(addr)
16057388Seric 	register char *addr;
16157388Seric {
16257388Seric 	for (; *addr != '\0'; addr++)
16357388Seric 	{
16458050Seric 		if ((*addr & 0340) != 0200)
16557388Seric 			continue;
16657388Seric 		setstat(EX_USAGE);
16758151Seric 		usrerr("553 Address contained invalid control characters");
16857388Seric 		return TRUE;
16957388Seric 	}
17057388Seric 	return FALSE;
17157388Seric }
17257388Seric /*
17356678Seric **  ALLOCADDR -- do local allocations of address on demand.
17456678Seric **
17556678Seric **	Also lowercases the host name if requested.
17656678Seric **
17756678Seric **	Parameters:
17856678Seric **		a -- the address to reallocate.
17956678Seric **		copyf -- the copy flag (see parseaddr for description).
18056678Seric **		paddr -- the printname of the address.
18158333Seric **		delimptr -- a pointer to the address delimiter.  Must be set.
18256678Seric **
18356678Seric **	Returns:
18456678Seric **		none.
18556678Seric **
18656678Seric **	Side Effects:
18756678Seric **		Copies portions of a into local buffers as requested.
18856678Seric */
18956678Seric 
19058333Seric allocaddr(a, copyf, paddr, delimptr)
19156678Seric 	register ADDRESS *a;
19256678Seric 	int copyf;
19356678Seric 	char *paddr;
19458333Seric 	char *delimptr;
19556678Seric {
19656678Seric 	register MAILER *m = a->q_mailer;
19756678Seric 
19856678Seric 	if (copyf > 0 && paddr != NULL)
19956678Seric 	{
20058333Seric 		char savec = *delimptr;
2018078Seric 
20258333Seric 		*delimptr = '\0';
20356678Seric 		a->q_paddr = newstr(paddr);
20458333Seric 		*delimptr = savec;
2058078Seric 	}
206297Seric 	else
20756678Seric 		a->q_paddr = paddr;
20824944Seric 
20924944Seric 	if (a->q_user == NULL)
21024944Seric 		a->q_user = "";
21124944Seric 	if (a->q_host == NULL)
21224944Seric 		a->q_host = "";
21324944Seric 
2143149Seric 	if (copyf >= 0)
215297Seric 	{
21624944Seric 		a->q_host = newstr(a->q_host);
2173149Seric 		if (a->q_user != a->q_paddr)
2183149Seric 			a->q_user = newstr(a->q_user);
219297Seric 	}
220297Seric 
22156678Seric 	if (a->q_paddr == NULL)
22256678Seric 		a->q_paddr = a->q_user;
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.
27658333Seric **		delimptr -- if non-NULL, set to the location of the
27758333Seric **			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 **
31258333Seric prescan(addr, delim, pvpbuf, delimptr)
313297Seric 	char *addr;
314297Seric 	char delim;
31516914Seric 	char pvpbuf[];
31658333Seric 	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");
36258333Seric 					if (delimptr != NULL)
36358333Seric 						*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 ')'");
43658333Seric 					if (delimptr != NULL)
43758333Seric 						*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 '>'");
45258333Seric 					if (delimptr != NULL)
45358333Seric 						*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");
49158333Seric 				if (delimptr != NULL)
49258333Seric 					*delimptr = p;
4938078Seric 				return (NULL);
494297Seric 			}
4958078Seric 			*avp++ = tok;
496297Seric 		}
4978423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4983149Seric 	*avp = NULL;
49958333Seric 	p--;
50058333Seric 	if (delimptr != NULL)
50158333Seric 		*delimptr = p;
50256764Seric 	if (tTd(22, 12))
50356764Seric 	{
50456764Seric 		printf("prescan==>");
50556764Seric 		printav(av);
50656764Seric 	}
50758403Seric 	return (av);
5083149Seric }
5093149Seric /*
5103149Seric **  TOKTYPE -- return token type
5113149Seric **
5123149Seric **	Parameters:
5133149Seric **		c -- the character in question.
5143149Seric **
5153149Seric **	Returns:
5163149Seric **		Its type.
5173149Seric **
5183149Seric **	Side Effects:
5193149Seric **		none.
5203149Seric */
521297Seric 
5223149Seric toktype(c)
52358050Seric 	register int c;
5243149Seric {
5253380Seric 	static char buf[50];
5263382Seric 	static bool firstime = TRUE;
5273380Seric 
5283382Seric 	if (firstime)
5293380Seric 	{
5303382Seric 		firstime = FALSE;
53158050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5327005Seric 		(void) strcat(buf, DELIMCHARS);
5333380Seric 	}
53458050Seric 	c &= 0377;
53556327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5368078Seric 		return (ONE);
5378078Seric 	if (c == '"')
5388078Seric 		return (QST);
53958050Seric 	if ((c & 0340) == 0200)
54058050Seric 		return (OPR);
5414100Seric 	if (!isascii(c))
5428078Seric 		return (ATM);
5438078Seric 	if (isspace(c) || c == ')')
5448078Seric 		return (SPC);
54558050Seric 	if (strchr(buf, c) != NULL)
5468078Seric 		return (OPR);
5478078Seric 	return (ATM);
5483149Seric }
5493149Seric /*
5503149Seric **  REWRITE -- apply rewrite rules to token vector.
5513149Seric **
5524476Seric **	This routine is an ordered production system.  Each rewrite
5534476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5544476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5554476Seric **
5564476Seric **	For each rewrite rule, 'avp' points the address vector we
5574476Seric **	are trying to match against, and 'pvp' points to the pattern.
5588058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5599585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5609585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5614476Seric **
5624476Seric **	When a match between avp & pvp does not match, we try to
5639585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5644476Seric **	we must also back out the match in mvp.  If we reach a
5658058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5668058Seric **	over again.
5674476Seric **
5684476Seric **	When we finally match, we rewrite the address vector
5694476Seric **	and try over again.
5704476Seric **
5713149Seric **	Parameters:
5723149Seric **		pvp -- pointer to token vector.
5733149Seric **
5743149Seric **	Returns:
5753149Seric **		none.
5763149Seric **
5773149Seric **	Side Effects:
5783149Seric **		pvp is modified.
5793149Seric */
5802091Seric 
5813149Seric struct match
5823149Seric {
5834468Seric 	char	**first;	/* first token matched */
5844468Seric 	char	**last;		/* last token matched */
5853149Seric };
5863149Seric 
5874468Seric # define MAXMATCH	9	/* max params per rewrite */
5883149Seric 
5893149Seric 
5904070Seric rewrite(pvp, ruleset)
5913149Seric 	char **pvp;
5924070Seric 	int ruleset;
5933149Seric {
5943149Seric 	register char *ap;		/* address pointer */
5953149Seric 	register char *rp;		/* rewrite pointer */
5963149Seric 	register char **avp;		/* address vector pointer */
5973149Seric 	register char **rvp;		/* rewrite vector pointer */
5988058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5998058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
60056678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
6013149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
6023149Seric 
6039279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
6043149Seric 	{
6058959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
60656678Seric 		printav(pvp);
6073149Seric 	}
60856678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
60956326Seric 	{
61058151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
61156326Seric 		return;
61256326Seric 	}
61356678Seric 	if (pvp == NULL)
61456678Seric 		return;
61556326Seric 
6163149Seric 	/*
61756678Seric 	**  Run through the list of rewrite rules, applying
61856678Seric 	**	any that match.
6193149Seric 	*/
6203149Seric 
6214070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6223149Seric 	{
62356678Seric 		int loopcount = 0;
62456678Seric 
6257675Seric 		if (tTd(21, 12))
626297Seric 		{
6278069Seric 			printf("-----trying rule:");
62856678Seric 			printav(rwr->r_lhs);
6293149Seric 		}
6303149Seric 
6313149Seric 		/* try to match on this rule */
6324468Seric 		mlp = mlist;
6338058Seric 		rvp = rwr->r_lhs;
6348058Seric 		avp = pvp;
6358058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
6363149Seric 		{
63756678Seric 			if (++loopcount > 100)
63852637Seric 			{
63958151Seric 				syserr("554 Infinite loop in ruleset %d", ruleset);
64056678Seric 				printf("workspace: ");
64156678Seric 				printav(pvp);
64252637Seric 				break;
64352637Seric 			}
6443149Seric 			rp = *rvp;
6458058Seric 			if (tTd(21, 35))
6468058Seric 			{
64757532Seric 				printf("rp=");
64857531Seric 				xputs(rp);
64957532Seric 				printf(", ap=");
6508058Seric 				xputs(ap);
6518069Seric 				printf("\n");
6528058Seric 			}
65356678Seric 			if (rp == NULL)
65456326Seric 			{
6553149Seric 				/* end-of-pattern before end-of-address */
6568058Seric 				goto backup;
65756678Seric 			}
65858173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
65958173Seric 			    (*rp & 0377) != CANONHOST)
66056326Seric 			{
66156678Seric 				/* end-of-input */
66256678Seric 				break;
663297Seric 			}
66456326Seric 
66558050Seric 			switch (*rp & 0377)
6668058Seric 			{
66756678Seric 				register STAB *s;
66856326Seric 
66956678Seric 			  case MATCHCLASS:
67056678Seric 			  case MATCHNCLASS:
67156678Seric 				/* match any token in (not in) a class */
67256678Seric 				s = stab(ap, ST_CLASS, ST_FIND);
67356678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
67456326Seric 				{
67558050Seric 					if ((*rp & 0377) == MATCHCLASS)
6769585Seric 						goto backup;
67756326Seric 				}
67858050Seric 				else if ((*rp & 0377) == MATCHNCLASS)
67956678Seric 					goto backup;
6804060Seric 
68156678Seric 				/* explicit fall-through */
68256678Seric 
68356678Seric 			  case MATCHONE:
68456678Seric 			  case MATCHANY:
68556678Seric 				/* match exactly one token */
68656678Seric 				mlp->first = avp;
68756678Seric 				mlp->last = avp++;
6888058Seric 				mlp++;
68956678Seric 				break;
6908058Seric 
69156678Seric 			  case MATCHZANY:
69256678Seric 				/* match zero or more tokens */
69356678Seric 				mlp->first = avp;
69456678Seric 				mlp->last = avp - 1;
69556678Seric 				mlp++;
69656678Seric 				break;
69756326Seric 
69858173Seric 			  case CANONHOST:
69958173Seric 				/* match zero tokens */
70058173Seric 				break;
70158173Seric 
70256678Seric 			  default:
70356678Seric 				/* must have exact match */
70456678Seric 				if (strcasecmp(rp, ap))
7058058Seric 					goto backup;
7064468Seric 				avp++;
70756678Seric 				break;
7083149Seric 			}
7093149Seric 
71056678Seric 			/* successful match on this token */
7113149Seric 			rvp++;
7123149Seric 			continue;
7133149Seric 
71456678Seric 		  backup:
71556678Seric 			/* match failed -- back up */
71656678Seric 			while (--rvp >= rwr->r_lhs)
7173149Seric 			{
71856678Seric 				rp = *rvp;
71958050Seric 				if ((*rp & 0377) == MATCHANY ||
72058050Seric 				    (*rp & 0377) == MATCHZANY)
7214468Seric 				{
72256678Seric 					/* extend binding and continue */
72356678Seric 					avp = ++mlp[-1].last;
72456678Seric 					avp++;
72556678Seric 					rvp++;
72656678Seric 					break;
7274468Seric 				}
72856678Seric 				avp--;
72958050Seric 				if ((*rp & 0377) == MATCHONE ||
73058050Seric 				    (*rp & 0377) == MATCHCLASS ||
73158050Seric 				    (*rp & 0377) == MATCHNCLASS)
73256678Seric 				{
73356678Seric 					/* back out binding */
73456678Seric 					mlp--;
73556678Seric 				}
7363149Seric 			}
7373149Seric 
73856678Seric 			if (rvp < rwr->r_lhs)
73956678Seric 			{
74056678Seric 				/* total failure to match */
74156326Seric 				break;
7423149Seric 			}
743297Seric 		}
7443149Seric 
7453149Seric 		/*
74656678Seric 		**  See if we successfully matched
7473149Seric 		*/
7483149Seric 
74956678Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
7503149Seric 		{
7519374Seric 			if (tTd(21, 10))
7529374Seric 				printf("----- rule fails\n");
7539374Seric 			rwr = rwr->r_next;
7549374Seric 			continue;
7559374Seric 		}
7563149Seric 
7579374Seric 		rvp = rwr->r_rhs;
7589374Seric 		if (tTd(21, 12))
7599374Seric 		{
7609374Seric 			printf("-----rule matches:");
76156678Seric 			printav(rvp);
7629374Seric 		}
7639374Seric 
7649374Seric 		rp = *rvp;
76558050Seric 		if ((*rp & 0377) == CANONUSER)
7669374Seric 		{
7679374Seric 			rvp++;
7689374Seric 			rwr = rwr->r_next;
7699374Seric 		}
77058050Seric 		else if ((*rp & 0377) == CANONHOST)
7719374Seric 		{
7729374Seric 			rvp++;
7739374Seric 			rwr = NULL;
7749374Seric 		}
77558050Seric 		else if ((*rp & 0377) == CANONNET)
7769374Seric 			rwr = NULL;
7779374Seric 
7789374Seric 		/* substitute */
7799374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
7809374Seric 		{
7819374Seric 			register struct match *m;
7829374Seric 			register char **pp;
7839374Seric 
7848058Seric 			rp = *rvp;
78558050Seric 			if ((*rp & 0377) == MATCHREPL)
7868058Seric 			{
78716914Seric 				/* substitute from LHS */
78816914Seric 				m = &mlist[rp[1] - '1'];
78956678Seric 				if (m < mlist || m >= mlp)
7909374Seric 				{
79158151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
79256326Seric 						ruleset, rp[1]);
7939374Seric 					return;
7949374Seric 				}
79516914Seric 				if (tTd(21, 15))
79616914Seric 				{
79716914Seric 					printf("$%c:", rp[1]);
79816914Seric 					pp = m->first;
79956678Seric 					while (pp <= m->last)
80016914Seric 					{
80116914Seric 						printf(" %x=\"", *pp);
80216914Seric 						(void) fflush(stdout);
80316914Seric 						printf("%s\"", *pp++);
80416914Seric 					}
80516914Seric 					printf("\n");
80616914Seric 				}
8079374Seric 				pp = m->first;
80856678Seric 				while (pp <= m->last)
8093149Seric 				{
81016914Seric 					if (avp >= &npvp[MAXATOM])
81156678Seric 					{
81258151Seric 						syserr("554 rewrite: expansion too long");
81356678Seric 						return;
81456678Seric 					}
81516914Seric 					*avp++ = *pp++;
8163149Seric 				}
8173149Seric 			}
81816914Seric 			else
8198226Seric 			{
82016914Seric 				/* vanilla replacement */
8219374Seric 				if (avp >= &npvp[MAXATOM])
82216889Seric 				{
82356678Seric 	toolong:
82458151Seric 					syserr("554 rewrite: expansion too long");
82516889Seric 					return;
82616889Seric 				}
82756678Seric 				*avp++ = rp;
8288226Seric 			}
8299374Seric 		}
8309374Seric 		*avp++ = NULL;
83116914Seric 
83216914Seric 		/*
83356678Seric 		**  Check for any hostname/keyword lookups.
83416914Seric 		*/
83516914Seric 
83616914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
83716914Seric 		{
83856678Seric 			char **hbrvp;
83916914Seric 			char **xpvp;
84016914Seric 			int trsize;
84117473Seric 			char *olddelimchar;
84256678Seric 			char *replac;
84356678Seric 			int endtoken;
84456678Seric 			STAB *map;
84556678Seric 			char *mapname;
84656678Seric 			char **key_rvp;
84756678Seric 			char **arg_rvp;
84856678Seric 			char **default_rvp;
84956678Seric 			char buf[MAXNAME + 1];
85016914Seric 			char *pvpb1[MAXATOM + 1];
85156823Seric 			char *argvect[10];
85217174Seric 			char pvpbuf[PSBUFSIZE];
85316914Seric 
85458050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
85558050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
85616914Seric 				continue;
85716914Seric 
85816914Seric 			/*
85956678Seric 			**  Got a hostname/keyword lookup.
86016914Seric 			**
86116914Seric 			**	This could be optimized fairly easily.
86216914Seric 			*/
86316914Seric 
86416914Seric 			hbrvp = rvp;
86558050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
86656327Seric 			{
86756678Seric 				endtoken = HOSTEND;
86856678Seric 				mapname = "host";
86956327Seric 			}
87056326Seric 			else
87156327Seric 			{
87256678Seric 				endtoken = LOOKUPEND;
87356678Seric 				mapname = *++rvp;
87456327Seric 			}
87556678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
87656678Seric 			if (map == NULL)
87758151Seric 				syserr("554 rewrite: map %s not found", mapname);
87856678Seric 
87956678Seric 			/* extract the match part */
88056678Seric 			key_rvp = ++rvp;
88156823Seric 			default_rvp = NULL;
88256823Seric 			arg_rvp = argvect;
88356823Seric 			xpvp = NULL;
88456823Seric 			replac = pvpbuf;
88558050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
88653654Seric 			{
88758050Seric 				int nodetype = **rvp & 0377;
88856823Seric 
88956823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
89056678Seric 				{
89156823Seric 					rvp++;
89256823Seric 					continue;
89356823Seric 				}
89456823Seric 
89556823Seric 				*rvp++ = NULL;
89656823Seric 
89756823Seric 				if (xpvp != NULL)
89856823Seric 				{
89956823Seric 					cataddr(xpvp, replac,
90058082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
90158082Seric 						'\0');
90256823Seric 					*++arg_rvp = replac;
90356823Seric 					replac += strlen(replac) + 1;
90456823Seric 					xpvp = NULL;
90556823Seric 				}
90656823Seric 				switch (nodetype)
90756823Seric 				{
90856678Seric 				  case CANONHOST:
90956823Seric 					xpvp = rvp;
91056678Seric 					break;
91156678Seric 
91256678Seric 				  case CANONUSER:
91356678Seric 					default_rvp = rvp;
91456678Seric 					break;
91556678Seric 				}
91653654Seric 			}
91716914Seric 			if (*rvp != NULL)
91816914Seric 				*rvp++ = NULL;
91956823Seric 			if (xpvp != NULL)
92056823Seric 			{
92156823Seric 				cataddr(xpvp, replac,
92258082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
92358082Seric 					'\0');
92456823Seric 				*++arg_rvp = replac;
92556823Seric 			}
92656823Seric 			*++arg_rvp = NULL;
92716914Seric 
92816914Seric 			/* save the remainder of the input string */
92916914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
93016914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
93116914Seric 
93256678Seric 			/* look it up */
93358082Seric 			cataddr(key_rvp, buf, sizeof buf, '\0');
93456823Seric 			argvect[0] = buf;
93556678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
93656836Seric 			{
93756836Seric 				int bsize = sizeof buf - 1;
93856836Seric 
93956836Seric 				if (map->s_map.map_app != NULL)
94056836Seric 					bsize -= strlen(map->s_map.map_app);
94156836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
94256823Seric 						buf, sizeof buf - 1, argvect);
94356836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
94456836Seric 					strcat(replac, map->s_map.map_app);
94556836Seric 			}
94653654Seric 			else
94756678Seric 				replac = NULL;
94856678Seric 
94956678Seric 			/* if no replacement, use default */
95056823Seric 			if (replac == NULL && default_rvp != NULL)
95156823Seric 			{
95256823Seric 				char buf2[sizeof buf];
95356823Seric 
95456823Seric 				/* rewrite the default with % translations */
95558082Seric 				cataddr(default_rvp, buf2, sizeof buf2, '\0');
95656823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
95756823Seric 					argvect);
95856823Seric 				replac = buf;
95956823Seric 			}
96056823Seric 
96156678Seric 			if (replac == NULL)
96251317Seric 			{
96356823Seric 				xpvp = key_rvp;
96453654Seric 			}
96556678Seric 			else
96653654Seric 			{
96756678Seric 				/* scan the new replacement */
96858333Seric 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
96953654Seric 				if (xpvp == NULL)
97051317Seric 				{
97158403Seric 					/* prescan already printed error */
97256678Seric 					return;
97356678Seric 				}
97451317Seric 			}
97551317Seric 
97616914Seric 			/* append it to the token list */
97756678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
97856678Seric 			{
97917174Seric 				*avp++ = newstr(*xpvp);
98016920Seric 				if (avp >= &npvp[MAXATOM])
98116914Seric 					goto toolong;
98217174Seric 			}
98316914Seric 
98416914Seric 			/* restore the old trailing information */
98556678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
98616920Seric 				if (avp >= &npvp[MAXATOM])
98716914Seric 					goto toolong;
98817174Seric 
98956678Seric 			break;
99016914Seric 		}
99116914Seric 
99216914Seric 		/*
99316914Seric 		**  Check for subroutine calls.
99416914Seric 		*/
99516914Seric 
99658050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
99756678Seric 		{
99856678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
99956678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
100056678Seric 			if (tTd(21, 3))
100156678Seric 				printf("-----callsubr %s\n", npvp[1]);
100256678Seric 			rewrite(pvp, atoi(npvp[1]));
100356678Seric 		}
100456678Seric 		else
100556678Seric 		{
100617348Seric 			bcopy((char *) npvp, (char *) pvp,
100716900Seric 				(int) (avp - npvp) * sizeof *avp);
100856678Seric 		}
10099374Seric 		if (tTd(21, 4))
10109374Seric 		{
10119374Seric 			printf("rewritten as:");
101256678Seric 			printav(pvp);
10139374Seric 		}
1014297Seric 	}
10158069Seric 
10169279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
10178069Seric 	{
10188959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
101956678Seric 		printav(pvp);
10208069Seric 	}
10213149Seric }
10223149Seric /*
10233149Seric **  BUILDADDR -- build address from token vector.
10243149Seric **
10253149Seric **	Parameters:
10263149Seric **		tv -- token vector.
10273149Seric **		a -- pointer to address descriptor to fill.
10283149Seric **			If NULL, one will be allocated.
10293149Seric **
10303149Seric **	Returns:
10314279Seric **		NULL if there was an error.
10324279Seric **		'a' otherwise.
10333149Seric **
10343149Seric **	Side Effects:
10353149Seric **		fills in 'a'
10363149Seric */
10373149Seric 
103857249Seric struct errcodes
103957249Seric {
104057249Seric 	char	*ec_name;		/* name of error code */
104157249Seric 	int	ec_code;		/* numeric code */
104257249Seric } ErrorCodes[] =
104357249Seric {
104457249Seric 	"usage",	EX_USAGE,
104557249Seric 	"nouser",	EX_NOUSER,
104657249Seric 	"nohost",	EX_NOHOST,
104757249Seric 	"unavailable",	EX_UNAVAILABLE,
104857249Seric 	"software",	EX_SOFTWARE,
104957249Seric 	"tempfail",	EX_TEMPFAIL,
105057249Seric 	"protocol",	EX_PROTOCOL,
105157249Seric #ifdef EX_CONFIG
105257249Seric 	"config",	EX_CONFIG,
105357249Seric #endif
105457249Seric 	NULL,		EX_UNAVAILABLE,
105557249Seric };
105657249Seric 
105756678Seric ADDRESS *
10583149Seric buildaddr(tv, a)
10593149Seric 	register char **tv;
10603149Seric 	register ADDRESS *a;
10613149Seric {
10623149Seric 	struct mailer **mp;
10633149Seric 	register struct mailer *m;
106458008Seric 	char *bp;
106558008Seric 	int spaceleft;
106657402Seric 	static char buf[MAXNAME];
10673149Seric 
10683149Seric 	if (a == NULL)
10693149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
107016889Seric 	bzero((char *) a, sizeof *a);
10713149Seric 
10723149Seric 	/* figure out what net/mailer to use */
107358050Seric 	if ((**tv & 0377) != CANONNET)
10744279Seric 	{
107558151Seric 		syserr("554 buildaddr: no net");
10764279Seric 		return (NULL);
10774279Seric 	}
10783149Seric 	tv++;
107933725Sbostic 	if (!strcasecmp(*tv, "error"))
10804279Seric 	{
108158050Seric 		if ((**++tv & 0377) == CANONHOST)
108210183Seric 		{
108357249Seric 			register struct errcodes *ep;
108457249Seric 
108558050Seric 			if (isascii(**++tv) && isdigit(**tv))
108657249Seric 			{
108757249Seric 				setstat(atoi(*tv));
108857249Seric 			}
108957249Seric 			else
109057249Seric 			{
109157249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
109257249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
109357249Seric 						break;
109457249Seric 				setstat(ep->ec_code);
109557249Seric 			}
109610183Seric 			tv++;
109710183Seric 		}
109858050Seric 		if ((**tv & 0377) != CANONUSER)
109958151Seric 			syserr("554 buildaddr: error: no user");
110058082Seric 		cataddr(++tv, buf, sizeof buf, ' ');
110158082Seric 		stripquotes(buf);
11024279Seric 		usrerr(buf);
11034279Seric 		return (NULL);
11044279Seric 	}
110557402Seric 
11064598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
11073149Seric 	{
110833725Sbostic 		if (!strcasecmp(m->m_name, *tv))
11093149Seric 			break;
11103149Seric 	}
11113149Seric 	if (m == NULL)
11124279Seric 	{
111358151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
11144279Seric 		return (NULL);
11154279Seric 	}
11164598Seric 	a->q_mailer = m;
11173149Seric 
11183149Seric 	/* figure out what host (if any) */
111956678Seric 	tv++;
112058139Seric 	if (!bitnset(M_LOCALMAILER, m->m_flags))
11213149Seric 	{
112258050Seric 		if ((**tv & 0377) != CANONHOST)
11234279Seric 		{
112458151Seric 			syserr("554 buildaddr: no host");
11254279Seric 			return (NULL);
11264279Seric 		}
112758008Seric 		bp = buf;
112858008Seric 		spaceleft = sizeof buf - 1;
112958050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
113058008Seric 		{
113158008Seric 			int i = strlen(*tv);
113258008Seric 
113358008Seric 			if (i > spaceleft)
113458008Seric 			{
113558008Seric 				/* out of space for this address */
113658008Seric 				if (spaceleft >= 0)
113758151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
113858008Seric 						buf);
113958008Seric 				i = spaceleft;
114058008Seric 				spaceleft = 0;
114158008Seric 			}
114258008Seric 			if (i <= 0)
114358008Seric 				continue;
114458008Seric 			bcopy(*tv, bp, i);
114558008Seric 			bp += i;
114658008Seric 			spaceleft -= i;
114758008Seric 		}
114858010Seric 		*bp = '\0';
11495704Seric 		a->q_host = newstr(buf);
11503149Seric 	}
115157249Seric 	else
115257249Seric 		a->q_host = NULL;
11533149Seric 
11543149Seric 	/* figure out the user */
115558050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
11564279Seric 	{
115758151Seric 		syserr("554 buildaddr: no user");
11584279Seric 		return (NULL);
11594279Seric 	}
116057402Seric 	tv++;
116151317Seric 
116257402Seric 	/* do special mapping for local mailer */
116357402Seric 	if (m == LocalMailer && *tv != NULL)
116457402Seric 	{
116557454Seric 		register char *p = *tv;
116657454Seric 
116757454Seric 		if (*p == '"')
116857454Seric 			p++;
116957454Seric 		if (*p == '|')
117057402Seric 			a->q_mailer = m = ProgMailer;
117157454Seric 		else if (*p == '/')
117257402Seric 			a->q_mailer = m = FileMailer;
117357454Seric 		else if (*p == ':')
117457402Seric 		{
117557402Seric 			/* may be :include: */
117658082Seric 			cataddr(tv, buf, sizeof buf, '\0');
117758008Seric 			stripquotes(buf);
117858008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
117958008Seric 			{
118058008Seric 				/* if :include:, don't need further rewriting */
118157402Seric 				a->q_mailer = m = InclMailer;
118258008Seric 				a->q_user = &buf[9];
118358008Seric 				return (a);
118458008Seric 			}
118557402Seric 		}
118657402Seric 	}
118757402Seric 
118858008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
118958008Seric 	{
119058008Seric 		tv++;
119158008Seric 		a->q_flags |= QNOTREMOTE;
119258008Seric 	}
119358008Seric 
119419040Seric 	/* rewrite according recipient mailer rewriting rules */
119557402Seric 	rewrite(tv, 2);
119658020Seric 	if (m->m_re_rwset > 0)
119758020Seric 		rewrite(tv, m->m_re_rwset);
119819040Seric 	rewrite(tv, 4);
119919040Seric 
120019040Seric 	/* save the result for the command line/RCPT argument */
120158082Seric 	cataddr(tv, buf, sizeof buf, '\0');
12023149Seric 	a->q_user = buf;
12033149Seric 
12043149Seric 	return (a);
12053149Seric }
12063188Seric /*
12074228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
12084228Seric **
12094228Seric **	Parameters:
12104228Seric **		pvp -- parameter vector to rebuild.
12114228Seric **		buf -- buffer to build the string into.
12124228Seric **		sz -- size of buf.
121358082Seric **		spacesub -- the space separator character; if null,
121458082Seric **			use SpaceSub.
12154228Seric **
12164228Seric **	Returns:
12174228Seric **		none.
12184228Seric **
12194228Seric **	Side Effects:
12204228Seric **		Destroys buf.
12214228Seric */
12224228Seric 
122358082Seric cataddr(pvp, buf, sz, spacesub)
12244228Seric 	char **pvp;
12254228Seric 	char *buf;
12264228Seric 	register int sz;
122758082Seric 	char spacesub;
12284228Seric {
12294228Seric 	bool oatomtok = FALSE;
123056678Seric 	bool natomtok = FALSE;
12314228Seric 	register int i;
12324228Seric 	register char *p;
12334228Seric 
123458082Seric 	if (spacesub == '\0')
123558082Seric 		spacesub = SpaceSub;
123658082Seric 
12378423Seric 	if (pvp == NULL)
12388423Seric 	{
123923109Seric 		(void) strcpy(buf, "");
12408423Seric 		return;
12418423Seric 	}
12424228Seric 	p = buf;
124311156Seric 	sz -= 2;
12444228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
12454228Seric 	{
12468078Seric 		natomtok = (toktype(**pvp) == ATM);
12474228Seric 		if (oatomtok && natomtok)
124858082Seric 			*p++ = spacesub;
12494228Seric 		(void) strcpy(p, *pvp);
12504228Seric 		oatomtok = natomtok;
12514228Seric 		p += i;
125211156Seric 		sz -= i + 1;
12534228Seric 		pvp++;
12544228Seric 	}
12554228Seric 	*p = '\0';
12564228Seric }
12574228Seric /*
12583188Seric **  SAMEADDR -- Determine if two addresses are the same
12593188Seric **
12603188Seric **	This is not just a straight comparison -- if the mailer doesn't
12613188Seric **	care about the host we just ignore it, etc.
12623188Seric **
12633188Seric **	Parameters:
12643188Seric **		a, b -- pointers to the internal forms to compare.
12653188Seric **
12663188Seric **	Returns:
12673188Seric **		TRUE -- they represent the same mailbox.
12683188Seric **		FALSE -- they don't.
12693188Seric **
12703188Seric **	Side Effects:
12713188Seric **		none.
12723188Seric */
12733188Seric 
12743188Seric bool
12759374Seric sameaddr(a, b)
12763188Seric 	register ADDRESS *a;
12773188Seric 	register ADDRESS *b;
12783188Seric {
12793188Seric 	/* if they don't have the same mailer, forget it */
12803188Seric 	if (a->q_mailer != b->q_mailer)
12813188Seric 		return (FALSE);
12823188Seric 
12833188Seric 	/* if the user isn't the same, we can drop out */
128456678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
12853188Seric 		return (FALSE);
12863188Seric 
1287*58438Seric 	/* if we have good uids for both but the differ, these are different */
1288*58438Seric 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
1289*58438Seric 		return (FALSE);
1290*58438Seric 
12913188Seric 	/* if the mailer ignores hosts, we have succeeded! */
129258139Seric 	if (bitnset(M_LOCALMAILER, a->q_mailer->m_flags))
12933188Seric 		return (TRUE);
12943188Seric 
12953188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
12963188Seric 	if (a->q_host == NULL || b->q_host == NULL)
12973188Seric 		return (FALSE);
129856678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
12993188Seric 		return (FALSE);
13003188Seric 
13013188Seric 	return (TRUE);
13023188Seric }
13033234Seric /*
13043234Seric **  PRINTADDR -- print address (for debugging)
13053234Seric **
13063234Seric **	Parameters:
13073234Seric **		a -- the address to print
13083234Seric **		follow -- follow the q_next chain.
13093234Seric **
13103234Seric **	Returns:
13113234Seric **		none.
13123234Seric **
13133234Seric **	Side Effects:
13143234Seric **		none.
13153234Seric */
13163234Seric 
13173234Seric printaddr(a, follow)
13183234Seric 	register ADDRESS *a;
13193234Seric 	bool follow;
13203234Seric {
13215001Seric 	bool first = TRUE;
132257731Seric 	register MAILER *m;
132357731Seric 	MAILER pseudomailer;
13245001Seric 
13253234Seric 	while (a != NULL)
13263234Seric 	{
13275001Seric 		first = FALSE;
13284443Seric 		printf("%x=", a);
13294085Seric 		(void) fflush(stdout);
133057731Seric 
133157731Seric 		/* find the mailer -- carefully */
133257731Seric 		m = a->q_mailer;
133357731Seric 		if (m == NULL)
133457731Seric 		{
133557731Seric 			m = &pseudomailer;
133657731Seric 			m->m_mno = -1;
133757731Seric 			m->m_name = "NULL";
133857731Seric 		}
133957731Seric 
134040973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
134157731Seric 		       a->q_paddr, m->m_mno, m->m_name,
134240973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
13438181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
13448181Seric 		       a->q_alias);
13458181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
13468181Seric 		       a->q_fullname);
13474996Seric 
13483234Seric 		if (!follow)
13493234Seric 			return;
13504996Seric 		a = a->q_next;
13513234Seric 	}
13525001Seric 	if (first)
13534443Seric 		printf("[NULL]\n");
13543234Seric }
13554317Seric 
13567682Seric /*
13577682Seric **  REMOTENAME -- return the name relative to the current mailer
13587682Seric **
13597682Seric **	Parameters:
13607682Seric **		name -- the name to translate.
13618069Seric **		m -- the mailer that we want to do rewriting relative
13628069Seric **			to.
13638069Seric **		senderaddress -- if set, uses the sender rewriting rules
13648069Seric **			rather than the recipient rewriting rules.
136558020Seric **		header -- set if this address is in the header, rather
136658020Seric **			than an envelope header.
136710310Seric **		canonical -- if set, strip out any comment information,
136810310Seric **			etc.
136958020Seric **		e -- the current envelope.
13707682Seric **
13717682Seric **	Returns:
13727682Seric **		the text string representing this address relative to
13737682Seric **			the receiving mailer.
13747682Seric **
13757682Seric **	Side Effects:
13767682Seric **		none.
13777682Seric **
13787682Seric **	Warnings:
13797682Seric **		The text string returned is tucked away locally;
13807682Seric **			copy it if you intend to save it.
13817682Seric */
13827682Seric 
13837682Seric char *
138458020Seric remotename(name, m, senderaddress, header, canonical, e)
13857682Seric 	char *name;
138656678Seric 	struct mailer *m;
13878069Seric 	bool senderaddress;
138858020Seric 	bool header;
138910310Seric 	bool canonical;
139056678Seric 	register ENVELOPE *e;
13917682Seric {
13928069Seric 	register char **pvp;
13938069Seric 	char *fancy;
139456678Seric 	extern char *macvalue();
139556678Seric 	char *oldg = macvalue('g', e);
139658020Seric 	int rwset;
13977682Seric 	static char buf[MAXNAME];
13987682Seric 	char lbuf[MAXNAME];
139916914Seric 	char pvpbuf[PSBUFSIZE];
140056678Seric 	extern char **prescan();
140156678Seric 	extern char *crackaddr();
14027682Seric 
14037755Seric 	if (tTd(12, 1))
14047755Seric 		printf("remotename(%s)\n", name);
14057755Seric 
140610177Seric 	/* don't do anything if we are tagging it as special */
140758020Seric 	if (senderaddress)
140858020Seric 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
140958020Seric 	else
141058020Seric 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
141158020Seric 	if (rwset < 0)
141210177Seric 		return (name);
141310177Seric 
14147682Seric 	/*
14158181Seric 	**  Do a heuristic crack of this name to extract any comment info.
14168181Seric 	**	This will leave the name as a comment and a $g macro.
14177889Seric 	*/
14187889Seric 
141958036Seric 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
142058050Seric 		fancy = "\201g";
142110310Seric 	else
142210310Seric 		fancy = crackaddr(name);
14237889Seric 
14248181Seric 	/*
14258181Seric 	**  Turn the name into canonical form.
14268181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
14278181Seric 	**	If this only resolves to "user", and the "C" flag is
14288181Seric 	**	specified in the sending mailer, then the sender's
14298181Seric 	**	domain will be appended.
14308181Seric 	*/
14318181Seric 
143258333Seric 	pvp = prescan(name, '\0', pvpbuf, NULL);
14337889Seric 	if (pvp == NULL)
14347889Seric 		return (name);
14358181Seric 	rewrite(pvp, 3);
143656678Seric 	if (e->e_fromdomain != NULL)
14378181Seric 	{
14388181Seric 		/* append from domain to this address */
14398181Seric 		register char **pxp = pvp;
14408181Seric 
14419594Seric 		/* see if there is an "@domain" in the current name */
14428181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
14438181Seric 			pxp++;
14448181Seric 		if (*pxp == NULL)
14458181Seric 		{
14469594Seric 			/* no.... append the "@domain" from the sender */
144756678Seric 			register char **qxq = e->e_fromdomain;
14488181Seric 
14499594Seric 			while ((*pxp++ = *qxq++) != NULL)
14509594Seric 				continue;
145111726Seric 			rewrite(pvp, 3);
14528181Seric 		}
14538181Seric 	}
14548181Seric 
14558181Seric 	/*
14568959Seric 	**  Do more specific rewriting.
145756678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
145856678Seric 	**		a sender address or not.
14598181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
14608181Seric 	*/
14618181Seric 
14628069Seric 	if (senderaddress)
146356327Seric 		rewrite(pvp, 1);
14648069Seric 	else
146556327Seric 		rewrite(pvp, 2);
146658020Seric 	if (rwset > 0)
146758020Seric 		rewrite(pvp, rwset);
14687682Seric 
14698181Seric 	/*
14708959Seric 	**  Do any final sanitation the address may require.
14718959Seric 	**	This will normally be used to turn internal forms
14728959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
14738959Seric 	**	may be used as a default to the above rules.
14748959Seric 	*/
14758959Seric 
14768959Seric 	rewrite(pvp, 4);
14778959Seric 
14788959Seric 	/*
14798181Seric 	**  Now restore the comment information we had at the beginning.
14808181Seric 	*/
14818181Seric 
148258082Seric 	cataddr(pvp, lbuf, sizeof lbuf, '\0');
148356678Seric 	define('g', lbuf, e);
148456678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
148556678Seric 	define('g', oldg, e);
14867682Seric 
14877682Seric 	if (tTd(12, 1))
14887755Seric 		printf("remotename => `%s'\n", buf);
14897682Seric 	return (buf);
14907682Seric }
149151317Seric /*
149256678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
149351317Seric **
149451317Seric **	Parameters:
149556678Seric **		a -- the address to map (but just the user name part).
149656678Seric **		sendq -- the sendq in which to install any replacement
149756678Seric **			addresses.
149851317Seric **
149951317Seric **	Returns:
150051317Seric **		none.
150151317Seric */
150251317Seric 
150356678Seric maplocaluser(a, sendq, e)
150456678Seric 	register ADDRESS *a;
150556678Seric 	ADDRESS **sendq;
150656678Seric 	ENVELOPE *e;
150751317Seric {
150856678Seric 	register char **pvp;
150956678Seric 	register ADDRESS *a1 = NULL;
151058333Seric 	auto char *delimptr;
151156678Seric 	char pvpbuf[PSBUFSIZE];
151251317Seric 
151356678Seric 	if (tTd(29, 1))
151456678Seric 	{
151556678Seric 		printf("maplocaluser: ");
151656678Seric 		printaddr(a, FALSE);
151756678Seric 	}
151858333Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
151956678Seric 	if (pvp == NULL)
152056678Seric 		return;
152151317Seric 
152256678Seric 	rewrite(pvp, 5);
152358050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
152456678Seric 		return;
152551317Seric 
152656678Seric 	/* if non-null, mailer destination specified -- has it changed? */
152756678Seric 	a1 = buildaddr(pvp, NULL);
152856678Seric 	if (a1 == NULL || sameaddr(a, a1))
152956678Seric 		return;
153051317Seric 
153156678Seric 	/* mark old address as dead; insert new address */
153256678Seric 	a->q_flags |= QDONTSEND;
153357731Seric 	if (tTd(29, 5))
153457731Seric 	{
153557731Seric 		printf("maplocaluser: QDONTSEND ");
153657731Seric 		printaddr(a, FALSE);
153757731Seric 	}
153856678Seric 	a1->q_alias = a;
153958333Seric 	allocaddr(a1, 1, NULL, delimptr);
154056678Seric 	(void) recipient(a1, sendq, e);
154151317Seric }
1542