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*58050Seric static char sccsid[] = "@(#)parseaddr.c	6.15 (Berkeley) 02/18/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.
4356678Seric **		e -- the envelope that will contain this address.
44297Seric **
45297Seric **	Returns:
46297Seric **		A pointer to the address descriptor header (`a' if
47297Seric **			`a' is non-NULL).
48297Seric **		NULL on error.
49297Seric **
50297Seric **	Side Effects:
51297Seric **		none
52297Seric */
53297Seric 
549374Seric /* following delimiters are inherent to the internal algorithms */
55*58050Seric # define DELIMCHARS	"\201()<>,;\\\"\r\n"	/* word delimiters */
562091Seric 
572973Seric ADDRESS *
5856678Seric parseaddr(addr, a, copyf, delim, e)
59297Seric 	char *addr;
602973Seric 	register ADDRESS *a;
61297Seric 	int copyf;
6211445Seric 	char delim;
6356678Seric 	register ENVELOPE *e;
64297Seric {
653149Seric 	register char **pvp;
6616914Seric 	char pvpbuf[PSBUFSIZE];
6756678Seric 	extern char **prescan();
6856678Seric 	extern ADDRESS *buildaddr();
6957388Seric 	extern bool invalidaddr();
70297Seric 
71297Seric 	/*
72297Seric 	**  Initialize and prescan address.
73297Seric 	*/
74297Seric 
7556678Seric 	e->e_to = addr;
767675Seric 	if (tTd(20, 1))
779888Seric 		printf("\n--parseaddr(%s)\n", addr);
783188Seric 
7957388Seric 	if (invalidaddr(addr))
8057388Seric 	{
8157388Seric 		if (tTd(20, 1))
8257388Seric 			printf("parseaddr-->bad address\n");
8357388Seric 		return NULL;
8457388Seric 	}
8557388Seric 
8616914Seric 	pvp = prescan(addr, delim, pvpbuf);
873149Seric 	if (pvp == NULL)
8856729Seric 	{
8956729Seric 		if (tTd(20, 1))
9056729Seric 			printf("parseaddr-->NULL\n");
91297Seric 		return (NULL);
9256729Seric 	}
93297Seric 
94297Seric 	/*
953149Seric 	**  Apply rewriting rules.
967889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
97297Seric 	*/
98297Seric 
998181Seric 	rewrite(pvp, 3);
1004070Seric 	rewrite(pvp, 0);
101297Seric 
1023149Seric 	/*
1033149Seric 	**  See if we resolved to a real mailer.
1043149Seric 	*/
105297Seric 
106*58050Seric 	if ((pvp[0][0] & 0377) != CANONNET)
1073149Seric 	{
1083149Seric 		setstat(EX_USAGE);
1093149Seric 		usrerr("cannot resolve name");
1103149Seric 		return (NULL);
111297Seric 	}
112297Seric 
113297Seric 	/*
1143149Seric 	**  Build canonical address from pvp.
115297Seric 	*/
116297Seric 
1173149Seric 	a = buildaddr(pvp, a);
1184279Seric 	if (a == NULL)
1194279Seric 		return (NULL);
120297Seric 
121297Seric 	/*
1223149Seric 	**  Make local copies of the host & user and then
1233149Seric 	**  transport them out.
124297Seric 	*/
125297Seric 
12656678Seric 	allocaddr(a, copyf, addr);
12756678Seric 
12856678Seric 	/*
12956678Seric 	**  Compute return value.
13056678Seric 	*/
13156678Seric 
13256678Seric 	if (tTd(20, 1))
1338078Seric 	{
13456678Seric 		printf("parseaddr-->");
13556678Seric 		printaddr(a, FALSE);
13656678Seric 	}
13756678Seric 
13856678Seric 	return (a);
13956678Seric }
14056678Seric /*
14157388Seric **  INVALIDADDR -- check for address containing meta-characters
14257388Seric **
14357388Seric **	Parameters:
14457388Seric **		addr -- the address to check.
14557388Seric **
14657388Seric **	Returns:
14757388Seric **		TRUE -- if the address has any "wierd" characters
14857388Seric **		FALSE -- otherwise.
14957388Seric */
15057388Seric 
15157388Seric bool
15257388Seric invalidaddr(addr)
15357388Seric 	register char *addr;
15457388Seric {
15557388Seric 	for (; *addr != '\0'; addr++)
15657388Seric 	{
157*58050Seric 		if ((*addr & 0340) != 0200)
15857388Seric 			continue;
15957388Seric 		setstat(EX_USAGE);
16057391Seric 		usrerr("Address contained invalid control characters");
16157388Seric 		return TRUE;
16257388Seric 	}
16357388Seric 	return FALSE;
16457388Seric }
16557388Seric /*
16656678Seric **  ALLOCADDR -- do local allocations of address on demand.
16756678Seric **
16856678Seric **	Also lowercases the host name if requested.
16956678Seric **
17056678Seric **	Parameters:
17156678Seric **		a -- the address to reallocate.
17256678Seric **		copyf -- the copy flag (see parseaddr for description).
17356678Seric **		paddr -- the printname of the address.
17456678Seric **
17556678Seric **	Returns:
17656678Seric **		none.
17756678Seric **
17856678Seric **	Side Effects:
17956678Seric **		Copies portions of a into local buffers as requested.
18056678Seric */
18156678Seric 
18256678Seric allocaddr(a, copyf, paddr)
18356678Seric 	register ADDRESS *a;
18456678Seric 	int copyf;
18556678Seric 	char *paddr;
18656678Seric {
18756678Seric 	register MAILER *m = a->q_mailer;
18856678Seric 
18956678Seric 	if (copyf > 0 && paddr != NULL)
19056678Seric 	{
19156678Seric 		extern char *DelimChar;
1928078Seric 		char savec = *DelimChar;
1938078Seric 
1948078Seric 		*DelimChar = '\0';
19556678Seric 		a->q_paddr = newstr(paddr);
1968078Seric 		*DelimChar = savec;
1978078Seric 	}
198297Seric 	else
19956678Seric 		a->q_paddr = paddr;
20024944Seric 
20124944Seric 	if (a->q_user == NULL)
20224944Seric 		a->q_user = "";
20324944Seric 	if (a->q_host == NULL)
20424944Seric 		a->q_host = "";
20524944Seric 
2063149Seric 	if (copyf >= 0)
207297Seric 	{
20824944Seric 		a->q_host = newstr(a->q_host);
2093149Seric 		if (a->q_user != a->q_paddr)
2103149Seric 			a->q_user = newstr(a->q_user);
211297Seric 	}
212297Seric 
21356678Seric 	if (a->q_paddr == NULL)
21456678Seric 		a->q_paddr = a->q_user;
21556678Seric 
216297Seric 	/*
21716202Seric 	**  Convert host name to lower case if requested.
21816202Seric 	**	User name will be done later.
21916202Seric 	*/
22016202Seric 
22116202Seric 	if (!bitnset(M_HST_UPPER, m->m_flags))
22216202Seric 		makelower(a->q_host);
223297Seric }
224297Seric /*
22516162Seric **  LOWERADDR -- map UPPER->lower case on addresses as requested.
22616162Seric **
22716162Seric **	Parameters:
22816162Seric **		a -- address to be mapped.
22916162Seric **
23016162Seric **	Returns:
23116162Seric **		none.
23216162Seric **
23316162Seric **	Side Effects:
23416162Seric **		none.
23516162Seric */
23616162Seric 
23716162Seric loweraddr(a)
23816162Seric 	register ADDRESS *a;
23916162Seric {
24016162Seric 	register MAILER *m = a->q_mailer;
24116162Seric 
24216162Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
24316162Seric 		makelower(a->q_user);
24416162Seric }
24516162Seric /*
246297Seric **  PRESCAN -- Prescan name and make it canonical
247297Seric **
2489374Seric **	Scans a name and turns it into a set of tokens.  This process
2499374Seric **	deletes blanks and comments (in parentheses).
250297Seric **
251297Seric **	This routine knows about quoted strings and angle brackets.
252297Seric **
253297Seric **	There are certain subtleties to this routine.  The one that
254297Seric **	comes to mind now is that backslashes on the ends of names
255297Seric **	are silently stripped off; this is intentional.  The problem
256297Seric **	is that some versions of sndmsg (like at LBL) set the kill
257297Seric **	character to something other than @ when reading addresses;
258297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
259297Seric **	berknet mailer.
260297Seric **
261297Seric **	Parameters:
262297Seric **		addr -- the name to chomp.
263297Seric **		delim -- the delimiter for the address, normally
264297Seric **			'\0' or ','; \0 is accepted in any case.
26515284Seric **			If '\t' then we are reading the .cf file.
26616914Seric **		pvpbuf -- place to put the saved text -- note that
26716914Seric **			the pointers are static.
268297Seric **
269297Seric **	Returns:
2703149Seric **		A pointer to a vector of tokens.
271297Seric **		NULL on error.
272297Seric **
273297Seric **	Side Effects:
27425279Seric **		sets DelimChar to point to the character matching 'delim'.
275297Seric */
276297Seric 
2778078Seric /* states and character types */
2788078Seric # define OPR		0	/* operator */
2798078Seric # define ATM		1	/* atom */
2808078Seric # define QST		2	/* in quoted string */
2818078Seric # define SPC		3	/* chewing up spaces */
2828078Seric # define ONE		4	/* pick up one character */
2833149Seric 
2848078Seric # define NSTATES	5	/* number of states */
2858078Seric # define TYPE		017	/* mask to select state type */
2868078Seric 
2878078Seric /* meta bits for table */
2888078Seric # define M		020	/* meta character; don't pass through */
2898078Seric # define B		040	/* cause a break */
2908078Seric # define MB		M|B	/* meta-break */
2918078Seric 
2928078Seric static short StateTab[NSTATES][NSTATES] =
2938078Seric {
2948087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2959051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2969051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2979051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2988078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2998078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
3008078Seric };
3018078Seric 
3028078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
3038078Seric 
30456678Seric char	*DelimChar;		/* set to point to the delimiter */
30556678Seric 
3063149Seric char **
30716914Seric prescan(addr, delim, pvpbuf)
308297Seric 	char *addr;
309297Seric 	char delim;
31016914Seric 	char pvpbuf[];
311297Seric {
312297Seric 	register char *p;
3138078Seric 	register char *q;
3149346Seric 	register int c;
3153149Seric 	char **avp;
316297Seric 	bool bslashmode;
317297Seric 	int cmntcnt;
3188423Seric 	int anglecnt;
3193149Seric 	char *tok;
3208078Seric 	int state;
3218078Seric 	int newstate;
3228078Seric 	static char *av[MAXATOM+1];
32356678Seric 	extern int errno;
324297Seric 
32515253Seric 	/* make sure error messages don't have garbage on them */
32615253Seric 	errno = 0;
32715253Seric 
32816914Seric 	q = pvpbuf;
3293149Seric 	bslashmode = FALSE;
3307800Seric 	cmntcnt = 0;
3318423Seric 	anglecnt = 0;
3323149Seric 	avp = av;
33356678Seric 	state = ATM;
3348078Seric 	c = NOCHAR;
3358078Seric 	p = addr;
33656764Seric 	if (tTd(22, 11))
337297Seric 	{
3388078Seric 		printf("prescan: ");
3398078Seric 		xputs(p);
34023109Seric 		(void) putchar('\n');
3418078Seric 	}
3428078Seric 
3438078Seric 	do
3448078Seric 	{
3453149Seric 		/* read a token */
3463149Seric 		tok = q;
3478078Seric 		for (;;)
348297Seric 		{
3498078Seric 			/* store away any old lookahead character */
3508078Seric 			if (c != NOCHAR)
3518078Seric 			{
35215284Seric 				/* see if there is room */
35316914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3548078Seric 				{
3558078Seric 					usrerr("Address too long");
3568078Seric 					DelimChar = p;
3578078Seric 					return (NULL);
3588078Seric 				}
35915284Seric 
36015284Seric 				/* squirrel it away */
3618078Seric 				*q++ = c;
3628078Seric 			}
3638078Seric 
3648078Seric 			/* read a new input character */
3658078Seric 			c = *p++;
36657631Seric 			if (c == '\0')
36756764Seric 			{
36856764Seric 				/* diagnose and patch up bad syntax */
36956764Seric 				if (state == QST)
37056764Seric 				{
37156764Seric 					usrerr("Unbalanced '\"'");
37256764Seric 					c = '"';
37356764Seric 				}
37456764Seric 				else if (cmntcnt > 0)
37556764Seric 				{
37656764Seric 					usrerr("Unbalanced '('");
37756764Seric 					c = ')';
37856764Seric 				}
37956764Seric 				else if (anglecnt > 0)
38056764Seric 				{
38156764Seric 					c = '>';
38256764Seric 					usrerr("Unbalanced '<'");
38356764Seric 				}
38456764Seric 				else
38556764Seric 					break;
38615284Seric 
38756764Seric 				p--;
38856764Seric 			}
38957631Seric 			else if (c == delim && anglecnt <= 0 &&
39057631Seric 					cmntcnt <= 0 && state != QST)
39157631Seric 				break;
39256764Seric 
3938078Seric 			if (tTd(22, 101))
3948078Seric 				printf("c=%c, s=%d; ", c, state);
3958078Seric 
3963149Seric 			/* chew up special characters */
3973149Seric 			*q = '\0';
3983149Seric 			if (bslashmode)
3993149Seric 			{
40024944Seric 				/* kludge \! for naive users */
40124944Seric 				if (c != '!')
40256678Seric 					*q++ = '\\';
4033149Seric 				bslashmode = FALSE;
40456678Seric 				continue;
4053149Seric 			}
40656678Seric 
40756678Seric 			if (c == '\\')
4083149Seric 			{
4093149Seric 				bslashmode = TRUE;
4108078Seric 				c = NOCHAR;
41156678Seric 				continue;
4123149Seric 			}
41356678Seric 			else if (state == QST)
4148514Seric 			{
4158514Seric 				/* do nothing, just avoid next clauses */
4168514Seric 			}
4178078Seric 			else if (c == '(')
4184100Seric 			{
4198078Seric 				cmntcnt++;
4208078Seric 				c = NOCHAR;
4214100Seric 			}
4228078Seric 			else if (c == ')')
4233149Seric 			{
4248078Seric 				if (cmntcnt <= 0)
4253149Seric 				{
4268078Seric 					usrerr("Unbalanced ')'");
4278078Seric 					DelimChar = p;
4288078Seric 					return (NULL);
4293149Seric 				}
4308078Seric 				else
4318078Seric 					cmntcnt--;
4328078Seric 			}
4338078Seric 			else if (cmntcnt > 0)
4348078Seric 				c = NOCHAR;
4358423Seric 			else if (c == '<')
4368423Seric 				anglecnt++;
4378423Seric 			else if (c == '>')
4388423Seric 			{
4398423Seric 				if (anglecnt <= 0)
4408423Seric 				{
4418423Seric 					usrerr("Unbalanced '>'");
4428423Seric 					DelimChar = p;
4438423Seric 					return (NULL);
4448423Seric 				}
4458423Seric 				anglecnt--;
4468423Seric 			}
447*58050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
44811423Seric 				c = ' ';
4493149Seric 
4508078Seric 			if (c == NOCHAR)
4518078Seric 				continue;
4523149Seric 
4538078Seric 			/* see if this is end of input */
45411405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4553149Seric 				break;
4563149Seric 
4578078Seric 			newstate = StateTab[state][toktype(c)];
4588078Seric 			if (tTd(22, 101))
4598078Seric 				printf("ns=%02o\n", newstate);
4608078Seric 			state = newstate & TYPE;
4618078Seric 			if (bitset(M, newstate))
4628078Seric 				c = NOCHAR;
4638078Seric 			if (bitset(B, newstate))
4644228Seric 				break;
465297Seric 		}
4663149Seric 
4673149Seric 		/* new token */
4688078Seric 		if (tok != q)
4691378Seric 		{
4708078Seric 			*q++ = '\0';
4718078Seric 			if (tTd(22, 36))
472297Seric 			{
4738078Seric 				printf("tok=");
4748078Seric 				xputs(tok);
47523109Seric 				(void) putchar('\n');
476297Seric 			}
4778078Seric 			if (avp >= &av[MAXATOM])
478297Seric 			{
4798078Seric 				syserr("prescan: too many tokens");
4808078Seric 				DelimChar = p;
4818078Seric 				return (NULL);
482297Seric 			}
4838078Seric 			*avp++ = tok;
484297Seric 		}
4858423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4863149Seric 	*avp = NULL;
4878078Seric 	DelimChar = --p;
48856764Seric 	if (tTd(22, 12))
48956764Seric 	{
49056764Seric 		printf("prescan==>");
49156764Seric 		printav(av);
49256764Seric 	}
49356764Seric 	if (av[0] != NULL)
4943149Seric 		return (av);
4953149Seric 	return (NULL);
4963149Seric }
4973149Seric /*
4983149Seric **  TOKTYPE -- return token type
4993149Seric **
5003149Seric **	Parameters:
5013149Seric **		c -- the character in question.
5023149Seric **
5033149Seric **	Returns:
5043149Seric **		Its type.
5053149Seric **
5063149Seric **	Side Effects:
5073149Seric **		none.
5083149Seric */
509297Seric 
5103149Seric toktype(c)
511*58050Seric 	register int c;
5123149Seric {
5133380Seric 	static char buf[50];
5143382Seric 	static bool firstime = TRUE;
5153380Seric 
5163382Seric 	if (firstime)
5173380Seric 	{
5183382Seric 		firstime = FALSE;
519*58050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5207005Seric 		(void) strcat(buf, DELIMCHARS);
5213380Seric 	}
522*58050Seric 	c &= 0377;
52356327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5248078Seric 		return (ONE);
5258078Seric 	if (c == '"')
5268078Seric 		return (QST);
527*58050Seric 	if ((c & 0340) == 0200)
528*58050Seric 		return (OPR);
5294100Seric 	if (!isascii(c))
5308078Seric 		return (ATM);
5318078Seric 	if (isspace(c) || c == ')')
5328078Seric 		return (SPC);
533*58050Seric 	if (strchr(buf, c) != NULL)
5348078Seric 		return (OPR);
5358078Seric 	return (ATM);
5363149Seric }
5373149Seric /*
5383149Seric **  REWRITE -- apply rewrite rules to token vector.
5393149Seric **
5404476Seric **	This routine is an ordered production system.  Each rewrite
5414476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5424476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5434476Seric **
5444476Seric **	For each rewrite rule, 'avp' points the address vector we
5454476Seric **	are trying to match against, and 'pvp' points to the pattern.
5468058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5479585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5489585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5494476Seric **
5504476Seric **	When a match between avp & pvp does not match, we try to
5519585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5524476Seric **	we must also back out the match in mvp.  If we reach a
5538058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5548058Seric **	over again.
5554476Seric **
5564476Seric **	When we finally match, we rewrite the address vector
5574476Seric **	and try over again.
5584476Seric **
5593149Seric **	Parameters:
5603149Seric **		pvp -- pointer to token vector.
5613149Seric **
5623149Seric **	Returns:
5633149Seric **		none.
5643149Seric **
5653149Seric **	Side Effects:
5663149Seric **		pvp is modified.
5673149Seric */
5682091Seric 
5693149Seric struct match
5703149Seric {
5714468Seric 	char	**first;	/* first token matched */
5724468Seric 	char	**last;		/* last token matched */
5733149Seric };
5743149Seric 
5754468Seric # define MAXMATCH	9	/* max params per rewrite */
5763149Seric 
5773149Seric 
5784070Seric rewrite(pvp, ruleset)
5793149Seric 	char **pvp;
5804070Seric 	int ruleset;
5813149Seric {
5823149Seric 	register char *ap;		/* address pointer */
5833149Seric 	register char *rp;		/* rewrite pointer */
5843149Seric 	register char **avp;		/* address vector pointer */
5853149Seric 	register char **rvp;		/* rewrite vector pointer */
5868058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5878058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
58856678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5893149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5903149Seric 
5919279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5923149Seric 	{
5938959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
59456678Seric 		printav(pvp);
5953149Seric 	}
59656678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
59756326Seric 	{
59856678Seric 		syserr("rewrite: illegal ruleset number %d", ruleset);
59956326Seric 		return;
60056326Seric 	}
60156678Seric 	if (pvp == NULL)
60256678Seric 		return;
60356326Seric 
6043149Seric 	/*
60556678Seric 	**  Run through the list of rewrite rules, applying
60656678Seric 	**	any that match.
6073149Seric 	*/
6083149Seric 
6094070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6103149Seric 	{
61156678Seric 		int loopcount = 0;
61256678Seric 
6137675Seric 		if (tTd(21, 12))
614297Seric 		{
6158069Seric 			printf("-----trying rule:");
61656678Seric 			printav(rwr->r_lhs);
6173149Seric 		}
6183149Seric 
6193149Seric 		/* try to match on this rule */
6204468Seric 		mlp = mlist;
6218058Seric 		rvp = rwr->r_lhs;
6228058Seric 		avp = pvp;
6238058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
6243149Seric 		{
62556678Seric 			if (++loopcount > 100)
62652637Seric 			{
62756678Seric 				syserr("Infinite loop in ruleset %d", ruleset);
62856678Seric 				printf("workspace: ");
62956678Seric 				printav(pvp);
63052637Seric 				break;
63152637Seric 			}
6323149Seric 			rp = *rvp;
6338058Seric 			if (tTd(21, 35))
6348058Seric 			{
63557532Seric 				printf("rp=");
63657531Seric 				xputs(rp);
63757532Seric 				printf(", ap=");
6388058Seric 				xputs(ap);
6398069Seric 				printf("\n");
6408058Seric 			}
64156678Seric 			if (rp == NULL)
64256326Seric 			{
6433149Seric 				/* end-of-pattern before end-of-address */
6448058Seric 				goto backup;
64556678Seric 			}
646*58050Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY)
64756326Seric 			{
64856678Seric 				/* end-of-input */
64956678Seric 				break;
650297Seric 			}
65156326Seric 
652*58050Seric 			switch (*rp & 0377)
6538058Seric 			{
65456678Seric 				register STAB *s;
65556326Seric 
65656678Seric 			  case MATCHCLASS:
65756678Seric 			  case MATCHNCLASS:
65856678Seric 				/* match any token in (not in) a class */
65956678Seric 				s = stab(ap, ST_CLASS, ST_FIND);
66056678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
66156326Seric 				{
662*58050Seric 					if ((*rp & 0377) == MATCHCLASS)
6639585Seric 						goto backup;
66456326Seric 				}
665*58050Seric 				else if ((*rp & 0377) == MATCHNCLASS)
66656678Seric 					goto backup;
6674060Seric 
66856678Seric 				/* explicit fall-through */
66956678Seric 
67056678Seric 			  case MATCHONE:
67156678Seric 			  case MATCHANY:
67256678Seric 				/* match exactly one token */
67356678Seric 				mlp->first = avp;
67456678Seric 				mlp->last = avp++;
6758058Seric 				mlp++;
67656678Seric 				break;
6778058Seric 
67856678Seric 			  case MATCHZANY:
67956678Seric 				/* match zero or more tokens */
68056678Seric 				mlp->first = avp;
68156678Seric 				mlp->last = avp - 1;
68256678Seric 				mlp++;
68356678Seric 				break;
68456326Seric 
68556678Seric 			  default:
68656678Seric 				/* must have exact match */
68756678Seric 				if (strcasecmp(rp, ap))
6888058Seric 					goto backup;
6894468Seric 				avp++;
69056678Seric 				break;
6913149Seric 			}
6923149Seric 
69356678Seric 			/* successful match on this token */
6943149Seric 			rvp++;
6953149Seric 			continue;
6963149Seric 
69756678Seric 		  backup:
69856678Seric 			/* match failed -- back up */
69956678Seric 			while (--rvp >= rwr->r_lhs)
7003149Seric 			{
70156678Seric 				rp = *rvp;
702*58050Seric 				if ((*rp & 0377) == MATCHANY ||
703*58050Seric 				    (*rp & 0377) == MATCHZANY)
7044468Seric 				{
70556678Seric 					/* extend binding and continue */
70656678Seric 					avp = ++mlp[-1].last;
70756678Seric 					avp++;
70856678Seric 					rvp++;
70956678Seric 					break;
7104468Seric 				}
71156678Seric 				avp--;
712*58050Seric 				if ((*rp & 0377) == MATCHONE ||
713*58050Seric 				    (*rp & 0377) == MATCHCLASS ||
714*58050Seric 				    (*rp & 0377) == MATCHNCLASS)
71556678Seric 				{
71656678Seric 					/* back out binding */
71756678Seric 					mlp--;
71856678Seric 				}
7193149Seric 			}
7203149Seric 
72156678Seric 			if (rvp < rwr->r_lhs)
72256678Seric 			{
72356678Seric 				/* total failure to match */
72456326Seric 				break;
7253149Seric 			}
726297Seric 		}
7273149Seric 
7283149Seric 		/*
72956678Seric 		**  See if we successfully matched
7303149Seric 		*/
7313149Seric 
73256678Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
7333149Seric 		{
7349374Seric 			if (tTd(21, 10))
7359374Seric 				printf("----- rule fails\n");
7369374Seric 			rwr = rwr->r_next;
7379374Seric 			continue;
7389374Seric 		}
7393149Seric 
7409374Seric 		rvp = rwr->r_rhs;
7419374Seric 		if (tTd(21, 12))
7429374Seric 		{
7439374Seric 			printf("-----rule matches:");
74456678Seric 			printav(rvp);
7459374Seric 		}
7469374Seric 
7479374Seric 		rp = *rvp;
748*58050Seric 		if ((*rp & 0377) == CANONUSER)
7499374Seric 		{
7509374Seric 			rvp++;
7519374Seric 			rwr = rwr->r_next;
7529374Seric 		}
753*58050Seric 		else if ((*rp & 0377) == CANONHOST)
7549374Seric 		{
7559374Seric 			rvp++;
7569374Seric 			rwr = NULL;
7579374Seric 		}
758*58050Seric 		else if ((*rp & 0377) == CANONNET)
7599374Seric 			rwr = NULL;
7609374Seric 
7619374Seric 		/* substitute */
7629374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
7639374Seric 		{
7649374Seric 			register struct match *m;
7659374Seric 			register char **pp;
7669374Seric 
7678058Seric 			rp = *rvp;
768*58050Seric 			if ((*rp & 0377) == MATCHREPL)
7698058Seric 			{
77016914Seric 				/* substitute from LHS */
77116914Seric 				m = &mlist[rp[1] - '1'];
77256678Seric 				if (m < mlist || m >= mlp)
7739374Seric 				{
77456678Seric 					syserr("rewrite: ruleset %d: replacement $%c out of bounds",
77556326Seric 						ruleset, rp[1]);
7769374Seric 					return;
7779374Seric 				}
77816914Seric 				if (tTd(21, 15))
77916914Seric 				{
78016914Seric 					printf("$%c:", rp[1]);
78116914Seric 					pp = m->first;
78256678Seric 					while (pp <= m->last)
78316914Seric 					{
78416914Seric 						printf(" %x=\"", *pp);
78516914Seric 						(void) fflush(stdout);
78616914Seric 						printf("%s\"", *pp++);
78716914Seric 					}
78816914Seric 					printf("\n");
78916914Seric 				}
7909374Seric 				pp = m->first;
79156678Seric 				while (pp <= m->last)
7923149Seric 				{
79316914Seric 					if (avp >= &npvp[MAXATOM])
79456678Seric 					{
79556678Seric 						syserr("rewrite: expansion too long");
79656678Seric 						return;
79756678Seric 					}
79816914Seric 					*avp++ = *pp++;
7993149Seric 				}
8003149Seric 			}
80116914Seric 			else
8028226Seric 			{
80316914Seric 				/* vanilla replacement */
8049374Seric 				if (avp >= &npvp[MAXATOM])
80516889Seric 				{
80656678Seric 	toolong:
80716889Seric 					syserr("rewrite: expansion too long");
80816889Seric 					return;
80916889Seric 				}
81056678Seric 				*avp++ = rp;
8118226Seric 			}
8129374Seric 		}
8139374Seric 		*avp++ = NULL;
81416914Seric 
81516914Seric 		/*
81656678Seric 		**  Check for any hostname/keyword lookups.
81716914Seric 		*/
81816914Seric 
81916914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
82016914Seric 		{
82156678Seric 			char **hbrvp;
82216914Seric 			char **xpvp;
82316914Seric 			int trsize;
82417473Seric 			char *olddelimchar;
82556678Seric 			char *replac;
82656678Seric 			int endtoken;
82756678Seric 			STAB *map;
82856678Seric 			char *mapname;
82956678Seric 			char **key_rvp;
83056678Seric 			char **arg_rvp;
83156678Seric 			char **default_rvp;
83256678Seric 			char buf[MAXNAME + 1];
83316914Seric 			char *pvpb1[MAXATOM + 1];
83456823Seric 			char *argvect[10];
83517174Seric 			char pvpbuf[PSBUFSIZE];
83656678Seric 			extern char *DelimChar;
83716914Seric 
838*58050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
839*58050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
84016914Seric 				continue;
84116914Seric 
84216914Seric 			/*
84356678Seric 			**  Got a hostname/keyword lookup.
84416914Seric 			**
84516914Seric 			**	This could be optimized fairly easily.
84616914Seric 			*/
84716914Seric 
84816914Seric 			hbrvp = rvp;
849*58050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
85056327Seric 			{
85156678Seric 				endtoken = HOSTEND;
85256678Seric 				mapname = "host";
85356327Seric 			}
85456326Seric 			else
85556327Seric 			{
85656678Seric 				endtoken = LOOKUPEND;
85756678Seric 				mapname = *++rvp;
85856327Seric 			}
85956678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
86056678Seric 			if (map == NULL)
86156678Seric 				syserr("rewrite: map %s not found", mapname);
86256678Seric 
86356678Seric 			/* extract the match part */
86456678Seric 			key_rvp = ++rvp;
86556823Seric 			default_rvp = NULL;
86656823Seric 			arg_rvp = argvect;
86756823Seric 			xpvp = NULL;
86856823Seric 			replac = pvpbuf;
869*58050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
87053654Seric 			{
871*58050Seric 				int nodetype = **rvp & 0377;
87256823Seric 
87356823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
87456678Seric 				{
87556823Seric 					rvp++;
87656823Seric 					continue;
87756823Seric 				}
87856823Seric 
87956823Seric 				*rvp++ = NULL;
88056823Seric 
88156823Seric 				if (xpvp != NULL)
88256823Seric 				{
88356823Seric 					cataddr(xpvp, replac,
88456823Seric 						&pvpbuf[sizeof pvpbuf] - replac);
88556823Seric 					*++arg_rvp = replac;
88656823Seric 					replac += strlen(replac) + 1;
88756823Seric 					xpvp = NULL;
88856823Seric 				}
88956823Seric 				switch (nodetype)
89056823Seric 				{
89156678Seric 				  case CANONHOST:
89256823Seric 					xpvp = rvp;
89356678Seric 					break;
89456678Seric 
89556678Seric 				  case CANONUSER:
89656678Seric 					default_rvp = rvp;
89756678Seric 					break;
89856678Seric 				}
89953654Seric 			}
90016914Seric 			if (*rvp != NULL)
90116914Seric 				*rvp++ = NULL;
90256823Seric 			if (xpvp != NULL)
90356823Seric 			{
90456823Seric 				cataddr(xpvp, replac,
90556823Seric 					&pvpbuf[sizeof pvpbuf] - replac);
90656823Seric 				*++arg_rvp = replac;
90756823Seric 			}
90856823Seric 			*++arg_rvp = NULL;
90916914Seric 
91016914Seric 			/* save the remainder of the input string */
91116914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
91216914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
91316914Seric 
91456678Seric 			/* look it up */
91556678Seric 			cataddr(key_rvp, buf, sizeof buf);
91656823Seric 			argvect[0] = buf;
91756678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
91856836Seric 			{
91956836Seric 				int bsize = sizeof buf - 1;
92056836Seric 
92156836Seric 				if (map->s_map.map_app != NULL)
92256836Seric 					bsize -= strlen(map->s_map.map_app);
92356836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
92456823Seric 						buf, sizeof buf - 1, argvect);
92556836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
92656836Seric 					strcat(replac, map->s_map.map_app);
92756836Seric 			}
92853654Seric 			else
92956678Seric 				replac = NULL;
93056678Seric 
93156678Seric 			/* if no replacement, use default */
93256823Seric 			if (replac == NULL && default_rvp != NULL)
93356823Seric 			{
93456823Seric 				char buf2[sizeof buf];
93556823Seric 
93656823Seric 				/* rewrite the default with % translations */
93756823Seric 				cataddr(default_rvp, buf2, sizeof buf2);
93856823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
93956823Seric 					argvect);
94056823Seric 				replac = buf;
94156823Seric 			}
94256823Seric 
94356678Seric 			if (replac == NULL)
94451317Seric 			{
94556823Seric 				xpvp = key_rvp;
94653654Seric 			}
94756678Seric 			else
94853654Seric 			{
94956678Seric 				/* scan the new replacement */
95056678Seric 				olddelimchar = DelimChar;
95156678Seric 				xpvp = prescan(replac, '\0', pvpbuf);
95256678Seric 				DelimChar = olddelimchar;
95353654Seric 				if (xpvp == NULL)
95451317Seric 				{
95556678Seric 					syserr("rewrite: cannot prescan map value: %s", replac);
95656678Seric 					return;
95756678Seric 				}
95851317Seric 			}
95951317Seric 
96016914Seric 			/* append it to the token list */
96156678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
96256678Seric 			{
96317174Seric 				*avp++ = newstr(*xpvp);
96416920Seric 				if (avp >= &npvp[MAXATOM])
96516914Seric 					goto toolong;
96617174Seric 			}
96716914Seric 
96816914Seric 			/* restore the old trailing information */
96956678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
97016920Seric 				if (avp >= &npvp[MAXATOM])
97116914Seric 					goto toolong;
97217174Seric 
97356678Seric 			break;
97416914Seric 		}
97516914Seric 
97616914Seric 		/*
97716914Seric 		**  Check for subroutine calls.
97816914Seric 		*/
97916914Seric 
980*58050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
98156678Seric 		{
98256678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
98356678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
98456678Seric 			if (tTd(21, 3))
98556678Seric 				printf("-----callsubr %s\n", npvp[1]);
98656678Seric 			rewrite(pvp, atoi(npvp[1]));
98756678Seric 		}
98856678Seric 		else
98956678Seric 		{
99017348Seric 			bcopy((char *) npvp, (char *) pvp,
99116900Seric 				(int) (avp - npvp) * sizeof *avp);
99256678Seric 		}
9939374Seric 		if (tTd(21, 4))
9949374Seric 		{
9959374Seric 			printf("rewritten as:");
99656678Seric 			printav(pvp);
9979374Seric 		}
998297Seric 	}
9998069Seric 
10009279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
10018069Seric 	{
10028959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
100356678Seric 		printav(pvp);
10048069Seric 	}
10053149Seric }
10063149Seric /*
10073149Seric **  BUILDADDR -- build address from token vector.
10083149Seric **
10093149Seric **	Parameters:
10103149Seric **		tv -- token vector.
10113149Seric **		a -- pointer to address descriptor to fill.
10123149Seric **			If NULL, one will be allocated.
10133149Seric **
10143149Seric **	Returns:
10154279Seric **		NULL if there was an error.
10164279Seric **		'a' otherwise.
10173149Seric **
10183149Seric **	Side Effects:
10193149Seric **		fills in 'a'
10203149Seric */
10213149Seric 
102257249Seric struct errcodes
102357249Seric {
102457249Seric 	char	*ec_name;		/* name of error code */
102557249Seric 	int	ec_code;		/* numeric code */
102657249Seric } ErrorCodes[] =
102757249Seric {
102857249Seric 	"usage",	EX_USAGE,
102957249Seric 	"nouser",	EX_NOUSER,
103057249Seric 	"nohost",	EX_NOHOST,
103157249Seric 	"unavailable",	EX_UNAVAILABLE,
103257249Seric 	"software",	EX_SOFTWARE,
103357249Seric 	"tempfail",	EX_TEMPFAIL,
103457249Seric 	"protocol",	EX_PROTOCOL,
103557249Seric #ifdef EX_CONFIG
103657249Seric 	"config",	EX_CONFIG,
103757249Seric #endif
103857249Seric 	NULL,		EX_UNAVAILABLE,
103957249Seric };
104057249Seric 
104156678Seric ADDRESS *
10423149Seric buildaddr(tv, a)
10433149Seric 	register char **tv;
10443149Seric 	register ADDRESS *a;
10453149Seric {
10463149Seric 	struct mailer **mp;
10473149Seric 	register struct mailer *m;
104858008Seric 	char *bp;
104958008Seric 	int spaceleft;
105057402Seric 	static char buf[MAXNAME];
10513149Seric 
10523149Seric 	if (a == NULL)
10533149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
105416889Seric 	bzero((char *) a, sizeof *a);
10553149Seric 
10563149Seric 	/* figure out what net/mailer to use */
1057*58050Seric 	if ((**tv & 0377) != CANONNET)
10584279Seric 	{
10593149Seric 		syserr("buildaddr: no net");
10604279Seric 		return (NULL);
10614279Seric 	}
10623149Seric 	tv++;
106333725Sbostic 	if (!strcasecmp(*tv, "error"))
10644279Seric 	{
1065*58050Seric 		if ((**++tv & 0377) == CANONHOST)
106610183Seric 		{
106757249Seric 			register struct errcodes *ep;
106857249Seric 
1069*58050Seric 			if (isascii(**++tv) && isdigit(**tv))
107057249Seric 			{
107157249Seric 				setstat(atoi(*tv));
107257249Seric 			}
107357249Seric 			else
107457249Seric 			{
107557249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
107657249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
107757249Seric 						break;
107857249Seric 				setstat(ep->ec_code);
107957249Seric 			}
108010183Seric 			tv++;
108110183Seric 		}
1082*58050Seric 		if ((**tv & 0377) != CANONUSER)
10834279Seric 			syserr("buildaddr: error: no user");
108458008Seric 		bp = buf;
108558008Seric 		spaceleft = sizeof buf - 2;
10864279Seric 		while (*++tv != NULL)
10874279Seric 		{
108858008Seric 			int i = strlen(*tv);
108958008Seric 
109058008Seric 			if (i > spaceleft)
109158008Seric 			{
109258008Seric 				/* out of space for this address */
109358008Seric 				if (spaceleft >= 0)
109458008Seric 					syserr("buildaddr: error message too long (%.40s...)",
109558008Seric 						buf);
109658008Seric 				i = spaceleft;
109758008Seric 				spaceleft = 0;
109858008Seric 			}
109958008Seric 			if (i <= 0)
110058008Seric 				continue;
110158008Seric 			if (bp != buf)
110258008Seric 			{
110358008Seric 				*bp++ = ' ';
110458008Seric 				spaceleft--;
110558008Seric 			}
110658008Seric 			bcopy(*tv, bp, i);
110758008Seric 			bp += i;
110858008Seric 			spaceleft -= i;
11094279Seric 		}
111058010Seric 		*bp = '\0';
11114279Seric 		usrerr(buf);
11124279Seric 		return (NULL);
11134279Seric 	}
111457402Seric 
11154598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
11163149Seric 	{
111733725Sbostic 		if (!strcasecmp(m->m_name, *tv))
11183149Seric 			break;
11193149Seric 	}
11203149Seric 	if (m == NULL)
11214279Seric 	{
112224944Seric 		syserr("buildaddr: unknown mailer %s", *tv);
11234279Seric 		return (NULL);
11244279Seric 	}
11254598Seric 	a->q_mailer = m;
11263149Seric 
11273149Seric 	/* figure out what host (if any) */
112856678Seric 	tv++;
112957249Seric 	if (!bitnset(M_LOCAL, m->m_flags))
11303149Seric 	{
1131*58050Seric 		if ((**tv & 0377) != CANONHOST)
11324279Seric 		{
11333149Seric 			syserr("buildaddr: no host");
11344279Seric 			return (NULL);
11354279Seric 		}
113658008Seric 		bp = buf;
113758008Seric 		spaceleft = sizeof buf - 1;
1138*58050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
113958008Seric 		{
114058008Seric 			int i = strlen(*tv);
114158008Seric 
114258008Seric 			if (i > spaceleft)
114358008Seric 			{
114458008Seric 				/* out of space for this address */
114558008Seric 				if (spaceleft >= 0)
114658008Seric 					syserr("buildaddr: host too long (%.40s...)",
114758008Seric 						buf);
114858008Seric 				i = spaceleft;
114958008Seric 				spaceleft = 0;
115058008Seric 			}
115158008Seric 			if (i <= 0)
115258008Seric 				continue;
115358008Seric 			bcopy(*tv, bp, i);
115458008Seric 			bp += i;
115558008Seric 			spaceleft -= i;
115658008Seric 		}
115758010Seric 		*bp = '\0';
11585704Seric 		a->q_host = newstr(buf);
11593149Seric 	}
116057249Seric 	else
116157249Seric 		a->q_host = NULL;
11623149Seric 
11633149Seric 	/* figure out the user */
1164*58050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
11654279Seric 	{
11663149Seric 		syserr("buildaddr: no user");
11674279Seric 		return (NULL);
11684279Seric 	}
116957402Seric 	tv++;
117051317Seric 
117157402Seric 	/* do special mapping for local mailer */
117257402Seric 	if (m == LocalMailer && *tv != NULL)
117357402Seric 	{
117457454Seric 		register char *p = *tv;
117557454Seric 
117657454Seric 		if (*p == '"')
117757454Seric 			p++;
117857454Seric 		if (*p == '|')
117957402Seric 			a->q_mailer = m = ProgMailer;
118057454Seric 		else if (*p == '/')
118157402Seric 			a->q_mailer = m = FileMailer;
118257454Seric 		else if (*p == ':')
118357402Seric 		{
118457402Seric 			/* may be :include: */
118557402Seric 			cataddr(tv, buf, sizeof buf);
118658008Seric 			stripquotes(buf);
118758008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
118858008Seric 			{
118958008Seric 				/* if :include:, don't need further rewriting */
119057402Seric 				a->q_mailer = m = InclMailer;
119158008Seric 				a->q_user = &buf[9];
119258008Seric 				return (a);
119358008Seric 			}
119457402Seric 		}
119557402Seric 	}
119657402Seric 
119758008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
119858008Seric 	{
119958008Seric 		tv++;
120058008Seric 		a->q_flags |= QNOTREMOTE;
120158008Seric 	}
120258008Seric 
120319040Seric 	/* rewrite according recipient mailer rewriting rules */
120457402Seric 	rewrite(tv, 2);
120558020Seric 	if (m->m_re_rwset > 0)
120658020Seric 		rewrite(tv, m->m_re_rwset);
120719040Seric 	rewrite(tv, 4);
120819040Seric 
120919040Seric 	/* save the result for the command line/RCPT argument */
121011278Seric 	cataddr(tv, buf, sizeof buf);
12113149Seric 	a->q_user = buf;
12123149Seric 
12133149Seric 	return (a);
12143149Seric }
12153188Seric /*
12164228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
12174228Seric **
12184228Seric **	Parameters:
12194228Seric **		pvp -- parameter vector to rebuild.
12204228Seric **		buf -- buffer to build the string into.
12214228Seric **		sz -- size of buf.
12224228Seric **
12234228Seric **	Returns:
12244228Seric **		none.
12254228Seric **
12264228Seric **	Side Effects:
12274228Seric **		Destroys buf.
12284228Seric */
12294228Seric 
12304228Seric cataddr(pvp, buf, sz)
12314228Seric 	char **pvp;
12324228Seric 	char *buf;
12334228Seric 	register int sz;
12344228Seric {
12354228Seric 	bool oatomtok = FALSE;
123656678Seric 	bool natomtok = FALSE;
12374228Seric 	register int i;
12384228Seric 	register char *p;
12394228Seric 
12408423Seric 	if (pvp == NULL)
12418423Seric 	{
124223109Seric 		(void) strcpy(buf, "");
12438423Seric 		return;
12448423Seric 	}
12454228Seric 	p = buf;
124611156Seric 	sz -= 2;
12474228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
12484228Seric 	{
12498078Seric 		natomtok = (toktype(**pvp) == ATM);
12504228Seric 		if (oatomtok && natomtok)
12519042Seric 			*p++ = SpaceSub;
12524228Seric 		(void) strcpy(p, *pvp);
12534228Seric 		oatomtok = natomtok;
12544228Seric 		p += i;
125511156Seric 		sz -= i + 1;
12564228Seric 		pvp++;
12574228Seric 	}
12584228Seric 	*p = '\0';
12594228Seric }
12604228Seric /*
12613188Seric **  SAMEADDR -- Determine if two addresses are the same
12623188Seric **
12633188Seric **	This is not just a straight comparison -- if the mailer doesn't
12643188Seric **	care about the host we just ignore it, etc.
12653188Seric **
12663188Seric **	Parameters:
12673188Seric **		a, b -- pointers to the internal forms to compare.
12683188Seric **
12693188Seric **	Returns:
12703188Seric **		TRUE -- they represent the same mailbox.
12713188Seric **		FALSE -- they don't.
12723188Seric **
12733188Seric **	Side Effects:
12743188Seric **		none.
12753188Seric */
12763188Seric 
12773188Seric bool
12789374Seric sameaddr(a, b)
12793188Seric 	register ADDRESS *a;
12803188Seric 	register ADDRESS *b;
12813188Seric {
12823188Seric 	/* if they don't have the same mailer, forget it */
12833188Seric 	if (a->q_mailer != b->q_mailer)
12843188Seric 		return (FALSE);
12853188Seric 
12863188Seric 	/* if the user isn't the same, we can drop out */
128756678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
12883188Seric 		return (FALSE);
12893188Seric 
12903188Seric 	/* if the mailer ignores hosts, we have succeeded! */
129110690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
12923188Seric 		return (TRUE);
12933188Seric 
12943188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
12953188Seric 	if (a->q_host == NULL || b->q_host == NULL)
12963188Seric 		return (FALSE);
129756678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
12983188Seric 		return (FALSE);
12993188Seric 
13003188Seric 	return (TRUE);
13013188Seric }
13023234Seric /*
13033234Seric **  PRINTADDR -- print address (for debugging)
13043234Seric **
13053234Seric **	Parameters:
13063234Seric **		a -- the address to print
13073234Seric **		follow -- follow the q_next chain.
13083234Seric **
13093234Seric **	Returns:
13103234Seric **		none.
13113234Seric **
13123234Seric **	Side Effects:
13133234Seric **		none.
13143234Seric */
13153234Seric 
13163234Seric printaddr(a, follow)
13173234Seric 	register ADDRESS *a;
13183234Seric 	bool follow;
13193234Seric {
13205001Seric 	bool first = TRUE;
132157731Seric 	register MAILER *m;
132257731Seric 	MAILER pseudomailer;
13235001Seric 
13243234Seric 	while (a != NULL)
13253234Seric 	{
13265001Seric 		first = FALSE;
13274443Seric 		printf("%x=", a);
13284085Seric 		(void) fflush(stdout);
132957731Seric 
133057731Seric 		/* find the mailer -- carefully */
133157731Seric 		m = a->q_mailer;
133257731Seric 		if (m == NULL)
133357731Seric 		{
133457731Seric 			m = &pseudomailer;
133557731Seric 			m->m_mno = -1;
133657731Seric 			m->m_name = "NULL";
133757731Seric 		}
133857731Seric 
133940973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
134057731Seric 		       a->q_paddr, m->m_mno, m->m_name,
134140973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
13428181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
13438181Seric 		       a->q_alias);
13448181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
13458181Seric 		       a->q_fullname);
13464996Seric 
13473234Seric 		if (!follow)
13483234Seric 			return;
13494996Seric 		a = a->q_next;
13503234Seric 	}
13515001Seric 	if (first)
13524443Seric 		printf("[NULL]\n");
13533234Seric }
13544317Seric 
13557682Seric /*
13567682Seric **  REMOTENAME -- return the name relative to the current mailer
13577682Seric **
13587682Seric **	Parameters:
13597682Seric **		name -- the name to translate.
13608069Seric **		m -- the mailer that we want to do rewriting relative
13618069Seric **			to.
13628069Seric **		senderaddress -- if set, uses the sender rewriting rules
13638069Seric **			rather than the recipient rewriting rules.
136458020Seric **		header -- set if this address is in the header, rather
136558020Seric **			than an envelope header.
136610310Seric **		canonical -- if set, strip out any comment information,
136710310Seric **			etc.
136858020Seric **		e -- the current envelope.
13697682Seric **
13707682Seric **	Returns:
13717682Seric **		the text string representing this address relative to
13727682Seric **			the receiving mailer.
13737682Seric **
13747682Seric **	Side Effects:
13757682Seric **		none.
13767682Seric **
13777682Seric **	Warnings:
13787682Seric **		The text string returned is tucked away locally;
13797682Seric **			copy it if you intend to save it.
13807682Seric */
13817682Seric 
13827682Seric char *
138358020Seric remotename(name, m, senderaddress, header, canonical, e)
13847682Seric 	char *name;
138556678Seric 	struct mailer *m;
13868069Seric 	bool senderaddress;
138758020Seric 	bool header;
138810310Seric 	bool canonical;
138956678Seric 	register ENVELOPE *e;
13907682Seric {
13918069Seric 	register char **pvp;
13928069Seric 	char *fancy;
139356678Seric 	extern char *macvalue();
139456678Seric 	char *oldg = macvalue('g', e);
139558020Seric 	int rwset;
13967682Seric 	static char buf[MAXNAME];
13977682Seric 	char lbuf[MAXNAME];
139816914Seric 	char pvpbuf[PSBUFSIZE];
139956678Seric 	extern char **prescan();
140056678Seric 	extern char *crackaddr();
14017682Seric 
14027755Seric 	if (tTd(12, 1))
14037755Seric 		printf("remotename(%s)\n", name);
14047755Seric 
140510177Seric 	/* don't do anything if we are tagging it as special */
140658020Seric 	if (senderaddress)
140758020Seric 		rwset = header ? m->m_sh_rwset : m->m_se_rwset;
140858020Seric 	else
140958020Seric 		rwset = header ? m->m_rh_rwset : m->m_re_rwset;
141058020Seric 	if (rwset < 0)
141110177Seric 		return (name);
141210177Seric 
14137682Seric 	/*
14148181Seric 	**  Do a heuristic crack of this name to extract any comment info.
14158181Seric 	**	This will leave the name as a comment and a $g macro.
14167889Seric 	*/
14177889Seric 
141858036Seric 	if (canonical || bitnset(M_NOCOMMENT, m->m_flags))
1419*58050Seric 		fancy = "\201g";
142010310Seric 	else
142110310Seric 		fancy = crackaddr(name);
14227889Seric 
14238181Seric 	/*
14248181Seric 	**  Turn the name into canonical form.
14258181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
14268181Seric 	**	If this only resolves to "user", and the "C" flag is
14278181Seric 	**	specified in the sending mailer, then the sender's
14288181Seric 	**	domain will be appended.
14298181Seric 	*/
14308181Seric 
143116914Seric 	pvp = prescan(name, '\0', pvpbuf);
14327889Seric 	if (pvp == NULL)
14337889Seric 		return (name);
14348181Seric 	rewrite(pvp, 3);
143556678Seric 	if (e->e_fromdomain != NULL)
14368181Seric 	{
14378181Seric 		/* append from domain to this address */
14388181Seric 		register char **pxp = pvp;
14398181Seric 
14409594Seric 		/* see if there is an "@domain" in the current name */
14418181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
14428181Seric 			pxp++;
14438181Seric 		if (*pxp == NULL)
14448181Seric 		{
14459594Seric 			/* no.... append the "@domain" from the sender */
144656678Seric 			register char **qxq = e->e_fromdomain;
14478181Seric 
14489594Seric 			while ((*pxp++ = *qxq++) != NULL)
14499594Seric 				continue;
145011726Seric 			rewrite(pvp, 3);
14518181Seric 		}
14528181Seric 	}
14538181Seric 
14548181Seric 	/*
14558959Seric 	**  Do more specific rewriting.
145656678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
145756678Seric 	**		a sender address or not.
14588181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
14598181Seric 	*/
14608181Seric 
14618069Seric 	if (senderaddress)
146256327Seric 		rewrite(pvp, 1);
14638069Seric 	else
146456327Seric 		rewrite(pvp, 2);
146558020Seric 	if (rwset > 0)
146658020Seric 		rewrite(pvp, rwset);
14677682Seric 
14688181Seric 	/*
14698959Seric 	**  Do any final sanitation the address may require.
14708959Seric 	**	This will normally be used to turn internal forms
14718959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
14728959Seric 	**	may be used as a default to the above rules.
14738959Seric 	*/
14748959Seric 
14758959Seric 	rewrite(pvp, 4);
14768959Seric 
14778959Seric 	/*
14788181Seric 	**  Now restore the comment information we had at the beginning.
14798181Seric 	*/
14808181Seric 
14817682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
148256678Seric 	define('g', lbuf, e);
148356678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
148456678Seric 	define('g', oldg, e);
14857682Seric 
14867682Seric 	if (tTd(12, 1))
14877755Seric 		printf("remotename => `%s'\n", buf);
14887682Seric 	return (buf);
14897682Seric }
149051317Seric /*
149156678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
149251317Seric **
149351317Seric **	Parameters:
149456678Seric **		a -- the address to map (but just the user name part).
149556678Seric **		sendq -- the sendq in which to install any replacement
149656678Seric **			addresses.
149751317Seric **
149851317Seric **	Returns:
149951317Seric **		none.
150051317Seric */
150151317Seric 
150256678Seric maplocaluser(a, sendq, e)
150356678Seric 	register ADDRESS *a;
150456678Seric 	ADDRESS **sendq;
150556678Seric 	ENVELOPE *e;
150651317Seric {
150756678Seric 	register char **pvp;
150856678Seric 	register ADDRESS *a1 = NULL;
150956678Seric 	char pvpbuf[PSBUFSIZE];
151051317Seric 
151156678Seric 	if (tTd(29, 1))
151256678Seric 	{
151356678Seric 		printf("maplocaluser: ");
151456678Seric 		printaddr(a, FALSE);
151556678Seric 	}
151656678Seric 	pvp = prescan(a->q_user, '\0', pvpbuf);
151756678Seric 	if (pvp == NULL)
151856678Seric 		return;
151951317Seric 
152056678Seric 	rewrite(pvp, 5);
1521*58050Seric 	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;
153756678Seric 	allocaddr(a1, 1, NULL);
153856678Seric 	(void) recipient(a1, sendq, e);
153951317Seric }
1540