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*57532Seric static char sccsid[] = "@(#)parseaddr.c	6.8 (Berkeley) 01/14/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 */
5516155Seric # define DELIMCHARS	"\001()<>,;\\\"\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 
1063149Seric 	if (pvp[0][0] != 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 	{
15757454Seric 		if (!isascii((int) *addr & 0377) ||
15857454Seric 		    !iscntrl(*addr) || isspace(*addr))
15957388Seric 			continue;
16057388Seric 		setstat(EX_USAGE);
16157391Seric 		usrerr("Address contained invalid control characters");
16257388Seric 		return TRUE;
16357388Seric 	}
16457388Seric 	return FALSE;
16557388Seric }
16657388Seric /*
16756678Seric **  ALLOCADDR -- do local allocations of address on demand.
16856678Seric **
16956678Seric **	Also lowercases the host name if requested.
17056678Seric **
17156678Seric **	Parameters:
17256678Seric **		a -- the address to reallocate.
17356678Seric **		copyf -- the copy flag (see parseaddr for description).
17456678Seric **		paddr -- the printname of the address.
17556678Seric **
17656678Seric **	Returns:
17756678Seric **		none.
17856678Seric **
17956678Seric **	Side Effects:
18056678Seric **		Copies portions of a into local buffers as requested.
18156678Seric */
18256678Seric 
18356678Seric allocaddr(a, copyf, paddr)
18456678Seric 	register ADDRESS *a;
18556678Seric 	int copyf;
18656678Seric 	char *paddr;
18756678Seric {
18856678Seric 	register MAILER *m = a->q_mailer;
18956678Seric 
19056678Seric 	if (copyf > 0 && paddr != NULL)
19156678Seric 	{
19256678Seric 		extern char *DelimChar;
1938078Seric 		char savec = *DelimChar;
1948078Seric 
1958078Seric 		*DelimChar = '\0';
19656678Seric 		a->q_paddr = newstr(paddr);
1978078Seric 		*DelimChar = savec;
1988078Seric 	}
199297Seric 	else
20056678Seric 		a->q_paddr = paddr;
20124944Seric 
20224944Seric 	if (a->q_user == NULL)
20324944Seric 		a->q_user = "";
20424944Seric 	if (a->q_host == NULL)
20524944Seric 		a->q_host = "";
20624944Seric 
2073149Seric 	if (copyf >= 0)
208297Seric 	{
20924944Seric 		a->q_host = newstr(a->q_host);
2103149Seric 		if (a->q_user != a->q_paddr)
2113149Seric 			a->q_user = newstr(a->q_user);
212297Seric 	}
213297Seric 
21456678Seric 	if (a->q_paddr == NULL)
21556678Seric 		a->q_paddr = a->q_user;
21656678Seric 
217297Seric 	/*
21816202Seric 	**  Convert host name to lower case if requested.
21916202Seric 	**	User name will be done later.
22016202Seric 	*/
22116202Seric 
22216202Seric 	if (!bitnset(M_HST_UPPER, m->m_flags))
22316202Seric 		makelower(a->q_host);
224297Seric }
225297Seric /*
22616162Seric **  LOWERADDR -- map UPPER->lower case on addresses as requested.
22716162Seric **
22816162Seric **	Parameters:
22916162Seric **		a -- address to be mapped.
23016162Seric **
23116162Seric **	Returns:
23216162Seric **		none.
23316162Seric **
23416162Seric **	Side Effects:
23516162Seric **		none.
23616162Seric */
23716162Seric 
23816162Seric loweraddr(a)
23916162Seric 	register ADDRESS *a;
24016162Seric {
24116162Seric 	register MAILER *m = a->q_mailer;
24216162Seric 
24316162Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
24416162Seric 		makelower(a->q_user);
24516162Seric }
24616162Seric /*
247297Seric **  PRESCAN -- Prescan name and make it canonical
248297Seric **
2499374Seric **	Scans a name and turns it into a set of tokens.  This process
2509374Seric **	deletes blanks and comments (in parentheses).
251297Seric **
252297Seric **	This routine knows about quoted strings and angle brackets.
253297Seric **
254297Seric **	There are certain subtleties to this routine.  The one that
255297Seric **	comes to mind now is that backslashes on the ends of names
256297Seric **	are silently stripped off; this is intentional.  The problem
257297Seric **	is that some versions of sndmsg (like at LBL) set the kill
258297Seric **	character to something other than @ when reading addresses;
259297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
260297Seric **	berknet mailer.
261297Seric **
262297Seric **	Parameters:
263297Seric **		addr -- the name to chomp.
264297Seric **		delim -- the delimiter for the address, normally
265297Seric **			'\0' or ','; \0 is accepted in any case.
26615284Seric **			If '\t' then we are reading the .cf file.
26716914Seric **		pvpbuf -- place to put the saved text -- note that
26816914Seric **			the pointers are static.
269297Seric **
270297Seric **	Returns:
2713149Seric **		A pointer to a vector of tokens.
272297Seric **		NULL on error.
273297Seric **
274297Seric **	Side Effects:
27525279Seric **		sets DelimChar to point to the character matching 'delim'.
276297Seric */
277297Seric 
2788078Seric /* states and character types */
2798078Seric # define OPR		0	/* operator */
2808078Seric # define ATM		1	/* atom */
2818078Seric # define QST		2	/* in quoted string */
2828078Seric # define SPC		3	/* chewing up spaces */
2838078Seric # define ONE		4	/* pick up one character */
2843149Seric 
2858078Seric # define NSTATES	5	/* number of states */
2868078Seric # define TYPE		017	/* mask to select state type */
2878078Seric 
2888078Seric /* meta bits for table */
2898078Seric # define M		020	/* meta character; don't pass through */
2908078Seric # define B		040	/* cause a break */
2918078Seric # define MB		M|B	/* meta-break */
2928078Seric 
2938078Seric static short StateTab[NSTATES][NSTATES] =
2948078Seric {
2958087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2969051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2979051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2989051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2998078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
3008078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
3018078Seric };
3028078Seric 
3038078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
3048078Seric 
30556678Seric char	*DelimChar;		/* set to point to the delimiter */
30656678Seric 
3073149Seric char **
30816914Seric prescan(addr, delim, pvpbuf)
309297Seric 	char *addr;
310297Seric 	char delim;
31116914Seric 	char pvpbuf[];
312297Seric {
313297Seric 	register char *p;
3148078Seric 	register char *q;
3159346Seric 	register int c;
3163149Seric 	char **avp;
317297Seric 	bool bslashmode;
318297Seric 	int cmntcnt;
3198423Seric 	int anglecnt;
3203149Seric 	char *tok;
3218078Seric 	int state;
3228078Seric 	int newstate;
3238078Seric 	static char *av[MAXATOM+1];
32456678Seric 	extern int errno;
325297Seric 
32615253Seric 	/* make sure error messages don't have garbage on them */
32715253Seric 	errno = 0;
32815253Seric 
32916914Seric 	q = pvpbuf;
3303149Seric 	bslashmode = FALSE;
3317800Seric 	cmntcnt = 0;
3328423Seric 	anglecnt = 0;
3333149Seric 	avp = av;
33456678Seric 	state = ATM;
3358078Seric 	c = NOCHAR;
3368078Seric 	p = addr;
33756764Seric 	if (tTd(22, 11))
338297Seric 	{
3398078Seric 		printf("prescan: ");
3408078Seric 		xputs(p);
34123109Seric 		(void) putchar('\n');
3428078Seric 	}
3438078Seric 
3448078Seric 	do
3458078Seric 	{
3463149Seric 		/* read a token */
3473149Seric 		tok = q;
3488078Seric 		for (;;)
349297Seric 		{
3508078Seric 			/* store away any old lookahead character */
3518078Seric 			if (c != NOCHAR)
3528078Seric 			{
35315284Seric 				/* see if there is room */
35416914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3558078Seric 				{
3568078Seric 					usrerr("Address too long");
3578078Seric 					DelimChar = p;
3588078Seric 					return (NULL);
3598078Seric 				}
36015284Seric 
36115284Seric 				/* squirrel it away */
3628078Seric 				*q++ = c;
3638078Seric 			}
3648078Seric 
3658078Seric 			/* read a new input character */
3668078Seric 			c = *p++;
36757529Seric 			if (c == '\0' ||
36857529Seric 			    (c == delim && anglecnt <= 0 && cmntcnt <= 0))
36956764Seric 			{
37056764Seric 				/* diagnose and patch up bad syntax */
37156764Seric 				if (state == QST)
37256764Seric 				{
37356764Seric 					usrerr("Unbalanced '\"'");
37456764Seric 					c = '"';
37556764Seric 				}
37656764Seric 				else if (cmntcnt > 0)
37756764Seric 				{
37856764Seric 					usrerr("Unbalanced '('");
37956764Seric 					c = ')';
38056764Seric 				}
38156764Seric 				else if (anglecnt > 0)
38256764Seric 				{
38356764Seric 					c = '>';
38456764Seric 					usrerr("Unbalanced '<'");
38556764Seric 				}
38656764Seric 				else
38756764Seric 					break;
38815284Seric 
38956764Seric 				p--;
39056764Seric 			}
39156764Seric 
3928078Seric 			if (tTd(22, 101))
3938078Seric 				printf("c=%c, s=%d; ", c, state);
3948078Seric 
3953149Seric 			/* chew up special characters */
3963149Seric 			*q = '\0';
3973149Seric 			if (bslashmode)
3983149Seric 			{
39924944Seric 				/* kludge \! for naive users */
40024944Seric 				if (c != '!')
40156678Seric 					*q++ = '\\';
4023149Seric 				bslashmode = FALSE;
40356678Seric 				continue;
4043149Seric 			}
40556678Seric 
40656678Seric 			if (c == '\\')
4073149Seric 			{
4083149Seric 				bslashmode = TRUE;
4098078Seric 				c = NOCHAR;
41056678Seric 				continue;
4113149Seric 			}
41256678Seric 			else if (state == QST)
4138514Seric 			{
4148514Seric 				/* do nothing, just avoid next clauses */
4158514Seric 			}
4168078Seric 			else if (c == '(')
4174100Seric 			{
4188078Seric 				cmntcnt++;
4198078Seric 				c = NOCHAR;
4204100Seric 			}
4218078Seric 			else if (c == ')')
4223149Seric 			{
4238078Seric 				if (cmntcnt <= 0)
4243149Seric 				{
4258078Seric 					usrerr("Unbalanced ')'");
4268078Seric 					DelimChar = p;
4278078Seric 					return (NULL);
4283149Seric 				}
4298078Seric 				else
4308078Seric 					cmntcnt--;
4318078Seric 			}
4328078Seric 			else if (cmntcnt > 0)
4338078Seric 				c = NOCHAR;
4348423Seric 			else if (c == '<')
4358423Seric 				anglecnt++;
4368423Seric 			else if (c == '>')
4378423Seric 			{
4388423Seric 				if (anglecnt <= 0)
4398423Seric 				{
4408423Seric 					usrerr("Unbalanced '>'");
4418423Seric 					DelimChar = p;
4428423Seric 					return (NULL);
4438423Seric 				}
4448423Seric 				anglecnt--;
4458423Seric 			}
44611423Seric 			else if (delim == ' ' && isspace(c))
44711423Seric 				c = ' ';
4483149Seric 
4498078Seric 			if (c == NOCHAR)
4508078Seric 				continue;
4513149Seric 
4528078Seric 			/* see if this is end of input */
45311405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4543149Seric 				break;
4553149Seric 
4568078Seric 			newstate = StateTab[state][toktype(c)];
4578078Seric 			if (tTd(22, 101))
4588078Seric 				printf("ns=%02o\n", newstate);
4598078Seric 			state = newstate & TYPE;
4608078Seric 			if (bitset(M, newstate))
4618078Seric 				c = NOCHAR;
4628078Seric 			if (bitset(B, newstate))
4634228Seric 				break;
464297Seric 		}
4653149Seric 
4663149Seric 		/* new token */
4678078Seric 		if (tok != q)
4681378Seric 		{
4698078Seric 			*q++ = '\0';
4708078Seric 			if (tTd(22, 36))
471297Seric 			{
4728078Seric 				printf("tok=");
4738078Seric 				xputs(tok);
47423109Seric 				(void) putchar('\n');
475297Seric 			}
4768078Seric 			if (avp >= &av[MAXATOM])
477297Seric 			{
4788078Seric 				syserr("prescan: too many tokens");
4798078Seric 				DelimChar = p;
4808078Seric 				return (NULL);
481297Seric 			}
4828078Seric 			*avp++ = tok;
483297Seric 		}
4848423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4853149Seric 	*avp = NULL;
4868078Seric 	DelimChar = --p;
48756764Seric 	if (tTd(22, 12))
48856764Seric 	{
48956764Seric 		printf("prescan==>");
49056764Seric 		printav(av);
49156764Seric 	}
49256764Seric 	if (av[0] != NULL)
4933149Seric 		return (av);
4943149Seric 	return (NULL);
4953149Seric }
4963149Seric /*
4973149Seric **  TOKTYPE -- return token type
4983149Seric **
4993149Seric **	Parameters:
5003149Seric **		c -- the character in question.
5013149Seric **
5023149Seric **	Returns:
5033149Seric **		Its type.
5043149Seric **
5053149Seric **	Side Effects:
5063149Seric **		none.
5073149Seric */
508297Seric 
5093149Seric toktype(c)
5103149Seric 	register char c;
5113149Seric {
5123380Seric 	static char buf[50];
5133382Seric 	static bool firstime = TRUE;
5143380Seric 
5153382Seric 	if (firstime)
5163380Seric 	{
5173382Seric 		firstime = FALSE;
51816155Seric 		expand("\001o", buf, &buf[sizeof buf - 1], CurEnv);
5197005Seric 		(void) strcat(buf, DELIMCHARS);
5203380Seric 	}
52156327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5228078Seric 		return (ONE);
5238078Seric 	if (c == '"')
5248078Seric 		return (QST);
5254100Seric 	if (!isascii(c))
5268078Seric 		return (ATM);
5278078Seric 	if (isspace(c) || c == ')')
5288078Seric 		return (SPC);
52956795Seric 	if (iscntrl(c) || strchr(buf, c) != NULL)
5308078Seric 		return (OPR);
5318078Seric 	return (ATM);
5323149Seric }
5333149Seric /*
5343149Seric **  REWRITE -- apply rewrite rules to token vector.
5353149Seric **
5364476Seric **	This routine is an ordered production system.  Each rewrite
5374476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5384476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5394476Seric **
5404476Seric **	For each rewrite rule, 'avp' points the address vector we
5414476Seric **	are trying to match against, and 'pvp' points to the pattern.
5428058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5439585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5449585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5454476Seric **
5464476Seric **	When a match between avp & pvp does not match, we try to
5479585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5484476Seric **	we must also back out the match in mvp.  If we reach a
5498058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5508058Seric **	over again.
5514476Seric **
5524476Seric **	When we finally match, we rewrite the address vector
5534476Seric **	and try over again.
5544476Seric **
5553149Seric **	Parameters:
5563149Seric **		pvp -- pointer to token vector.
5573149Seric **
5583149Seric **	Returns:
5593149Seric **		none.
5603149Seric **
5613149Seric **	Side Effects:
5623149Seric **		pvp is modified.
5633149Seric */
5642091Seric 
5653149Seric struct match
5663149Seric {
5674468Seric 	char	**first;	/* first token matched */
5684468Seric 	char	**last;		/* last token matched */
5693149Seric };
5703149Seric 
5714468Seric # define MAXMATCH	9	/* max params per rewrite */
5723149Seric 
5733149Seric 
5744070Seric rewrite(pvp, ruleset)
5753149Seric 	char **pvp;
5764070Seric 	int ruleset;
5773149Seric {
5783149Seric 	register char *ap;		/* address pointer */
5793149Seric 	register char *rp;		/* rewrite pointer */
5803149Seric 	register char **avp;		/* address vector pointer */
5813149Seric 	register char **rvp;		/* rewrite vector pointer */
5828058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5838058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
58456678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5853149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5863149Seric 
5879279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5883149Seric 	{
5898959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
59056678Seric 		printav(pvp);
5913149Seric 	}
59256678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
59356326Seric 	{
59456678Seric 		syserr("rewrite: illegal ruleset number %d", ruleset);
59556326Seric 		return;
59656326Seric 	}
59756678Seric 	if (pvp == NULL)
59856678Seric 		return;
59956326Seric 
6003149Seric 	/*
60156678Seric 	**  Run through the list of rewrite rules, applying
60256678Seric 	**	any that match.
6033149Seric 	*/
6043149Seric 
6054070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6063149Seric 	{
60756678Seric 		int loopcount = 0;
60856678Seric 
6097675Seric 		if (tTd(21, 12))
610297Seric 		{
6118069Seric 			printf("-----trying rule:");
61256678Seric 			printav(rwr->r_lhs);
6133149Seric 		}
6143149Seric 
6153149Seric 		/* try to match on this rule */
6164468Seric 		mlp = mlist;
6178058Seric 		rvp = rwr->r_lhs;
6188058Seric 		avp = pvp;
6198058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
6203149Seric 		{
62156678Seric 			if (++loopcount > 100)
62252637Seric 			{
62356678Seric 				syserr("Infinite loop in ruleset %d", ruleset);
62456678Seric 				printf("workspace: ");
62556678Seric 				printav(pvp);
62652637Seric 				break;
62752637Seric 			}
6283149Seric 			rp = *rvp;
6298058Seric 			if (tTd(21, 35))
6308058Seric 			{
631*57532Seric 				printf("rp=");
63257531Seric 				xputs(rp);
633*57532Seric 				printf(", ap=");
6348058Seric 				xputs(ap);
6358069Seric 				printf("\n");
6368058Seric 			}
63756678Seric 			if (rp == NULL)
63856326Seric 			{
6393149Seric 				/* end-of-pattern before end-of-address */
6408058Seric 				goto backup;
64156678Seric 			}
64256678Seric 			if (ap == NULL && *rp != MATCHZANY)
64356326Seric 			{
64456678Seric 				/* end-of-input */
64556678Seric 				break;
646297Seric 			}
64756326Seric 
64856678Seric 			switch (*rp)
6498058Seric 			{
65056678Seric 				register STAB *s;
65156326Seric 
65256678Seric 			  case MATCHCLASS:
65356678Seric 			  case MATCHNCLASS:
65456678Seric 				/* match any token in (not in) a class */
65556678Seric 				s = stab(ap, ST_CLASS, ST_FIND);
65656678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
65756326Seric 				{
65856678Seric 					if (*rp == MATCHCLASS)
6599585Seric 						goto backup;
66056326Seric 				}
66156678Seric 				else if (*rp == MATCHNCLASS)
66256678Seric 					goto backup;
6634060Seric 
66456678Seric 				/* explicit fall-through */
66556678Seric 
66656678Seric 			  case MATCHONE:
66756678Seric 			  case MATCHANY:
66856678Seric 				/* match exactly one token */
66956678Seric 				mlp->first = avp;
67056678Seric 				mlp->last = avp++;
6718058Seric 				mlp++;
67256678Seric 				break;
6738058Seric 
67456678Seric 			  case MATCHZANY:
67556678Seric 				/* match zero or more tokens */
67656678Seric 				mlp->first = avp;
67756678Seric 				mlp->last = avp - 1;
67856678Seric 				mlp++;
67956678Seric 				break;
68056326Seric 
68156678Seric 			  default:
68256678Seric 				/* must have exact match */
68356678Seric 				if (strcasecmp(rp, ap))
6848058Seric 					goto backup;
6854468Seric 				avp++;
68656678Seric 				break;
6873149Seric 			}
6883149Seric 
68956678Seric 			/* successful match on this token */
6903149Seric 			rvp++;
6913149Seric 			continue;
6923149Seric 
69356678Seric 		  backup:
69456678Seric 			/* match failed -- back up */
69556678Seric 			while (--rvp >= rwr->r_lhs)
6963149Seric 			{
69756678Seric 				rp = *rvp;
69856678Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
6994468Seric 				{
70056678Seric 					/* extend binding and continue */
70156678Seric 					avp = ++mlp[-1].last;
70256678Seric 					avp++;
70356678Seric 					rvp++;
70456678Seric 					break;
7054468Seric 				}
70656678Seric 				avp--;
70756678Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS ||
70856678Seric 				    *rp == MATCHNCLASS)
70956678Seric 				{
71056678Seric 					/* back out binding */
71156678Seric 					mlp--;
71256678Seric 				}
7133149Seric 			}
7143149Seric 
71556678Seric 			if (rvp < rwr->r_lhs)
71656678Seric 			{
71756678Seric 				/* total failure to match */
71856326Seric 				break;
7193149Seric 			}
720297Seric 		}
7213149Seric 
7223149Seric 		/*
72356678Seric 		**  See if we successfully matched
7243149Seric 		*/
7253149Seric 
72656678Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
7273149Seric 		{
7289374Seric 			if (tTd(21, 10))
7299374Seric 				printf("----- rule fails\n");
7309374Seric 			rwr = rwr->r_next;
7319374Seric 			continue;
7329374Seric 		}
7333149Seric 
7349374Seric 		rvp = rwr->r_rhs;
7359374Seric 		if (tTd(21, 12))
7369374Seric 		{
7379374Seric 			printf("-----rule matches:");
73856678Seric 			printav(rvp);
7399374Seric 		}
7409374Seric 
7419374Seric 		rp = *rvp;
7429374Seric 		if (*rp == CANONUSER)
7439374Seric 		{
7449374Seric 			rvp++;
7459374Seric 			rwr = rwr->r_next;
7469374Seric 		}
7479374Seric 		else if (*rp == CANONHOST)
7489374Seric 		{
7499374Seric 			rvp++;
7509374Seric 			rwr = NULL;
7519374Seric 		}
7529374Seric 		else if (*rp == CANONNET)
7539374Seric 			rwr = NULL;
7549374Seric 
7559374Seric 		/* substitute */
7569374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
7579374Seric 		{
7589374Seric 			register struct match *m;
7599374Seric 			register char **pp;
7609374Seric 
7618058Seric 			rp = *rvp;
76256678Seric 			if (*rp == MATCHREPL)
7638058Seric 			{
76416914Seric 				/* substitute from LHS */
76516914Seric 				m = &mlist[rp[1] - '1'];
76656678Seric 				if (m < mlist || m >= mlp)
7679374Seric 				{
76856678Seric 					syserr("rewrite: ruleset %d: replacement $%c out of bounds",
76956326Seric 						ruleset, rp[1]);
7709374Seric 					return;
7719374Seric 				}
77216914Seric 				if (tTd(21, 15))
77316914Seric 				{
77416914Seric 					printf("$%c:", rp[1]);
77516914Seric 					pp = m->first;
77656678Seric 					while (pp <= m->last)
77716914Seric 					{
77816914Seric 						printf(" %x=\"", *pp);
77916914Seric 						(void) fflush(stdout);
78016914Seric 						printf("%s\"", *pp++);
78116914Seric 					}
78216914Seric 					printf("\n");
78316914Seric 				}
7849374Seric 				pp = m->first;
78556678Seric 				while (pp <= m->last)
7863149Seric 				{
78716914Seric 					if (avp >= &npvp[MAXATOM])
78856678Seric 					{
78956678Seric 						syserr("rewrite: expansion too long");
79056678Seric 						return;
79156678Seric 					}
79216914Seric 					*avp++ = *pp++;
7933149Seric 				}
7943149Seric 			}
79516914Seric 			else
7968226Seric 			{
79716914Seric 				/* vanilla replacement */
7989374Seric 				if (avp >= &npvp[MAXATOM])
79916889Seric 				{
80056678Seric 	toolong:
80116889Seric 					syserr("rewrite: expansion too long");
80216889Seric 					return;
80316889Seric 				}
80456678Seric 				*avp++ = rp;
8058226Seric 			}
8069374Seric 		}
8079374Seric 		*avp++ = NULL;
80816914Seric 
80916914Seric 		/*
81056678Seric 		**  Check for any hostname/keyword lookups.
81116914Seric 		*/
81216914Seric 
81316914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
81416914Seric 		{
81556678Seric 			char **hbrvp;
81616914Seric 			char **xpvp;
81716914Seric 			int trsize;
81817473Seric 			char *olddelimchar;
81956678Seric 			char *replac;
82056678Seric 			int endtoken;
82156678Seric 			STAB *map;
82256678Seric 			char *mapname;
82356678Seric 			char **key_rvp;
82456678Seric 			char **arg_rvp;
82556678Seric 			char **default_rvp;
82656678Seric 			char buf[MAXNAME + 1];
82716914Seric 			char *pvpb1[MAXATOM + 1];
82856823Seric 			char *argvect[10];
82917174Seric 			char pvpbuf[PSBUFSIZE];
83056678Seric 			extern char *DelimChar;
83116914Seric 
83256678Seric 			if (**rvp != HOSTBEGIN && **rvp != LOOKUPBEGIN)
83316914Seric 				continue;
83416914Seric 
83516914Seric 			/*
83656678Seric 			**  Got a hostname/keyword lookup.
83716914Seric 			**
83816914Seric 			**	This could be optimized fairly easily.
83916914Seric 			*/
84016914Seric 
84116914Seric 			hbrvp = rvp;
84256678Seric 			if (**rvp == HOSTBEGIN)
84356327Seric 			{
84456678Seric 				endtoken = HOSTEND;
84556678Seric 				mapname = "host";
84656327Seric 			}
84756326Seric 			else
84856327Seric 			{
84956678Seric 				endtoken = LOOKUPEND;
85056678Seric 				mapname = *++rvp;
85156327Seric 			}
85256678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
85356678Seric 			if (map == NULL)
85456678Seric 				syserr("rewrite: map %s not found", mapname);
85556678Seric 
85656678Seric 			/* extract the match part */
85756678Seric 			key_rvp = ++rvp;
85856823Seric 			default_rvp = NULL;
85956823Seric 			arg_rvp = argvect;
86056823Seric 			xpvp = NULL;
86156823Seric 			replac = pvpbuf;
86256678Seric 			while (*rvp != NULL && **rvp != endtoken)
86353654Seric 			{
86456823Seric 				int nodetype = **rvp;
86556823Seric 
86656823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
86756678Seric 				{
86856823Seric 					rvp++;
86956823Seric 					continue;
87056823Seric 				}
87156823Seric 
87256823Seric 				*rvp++ = NULL;
87356823Seric 
87456823Seric 				if (xpvp != NULL)
87556823Seric 				{
87656823Seric 					cataddr(xpvp, replac,
87756823Seric 						&pvpbuf[sizeof pvpbuf] - replac);
87856823Seric 					*++arg_rvp = replac;
87956823Seric 					replac += strlen(replac) + 1;
88056823Seric 					xpvp = NULL;
88156823Seric 				}
88256823Seric 				switch (nodetype)
88356823Seric 				{
88456678Seric 				  case CANONHOST:
88556823Seric 					xpvp = rvp;
88656678Seric 					break;
88756678Seric 
88856678Seric 				  case CANONUSER:
88956678Seric 					default_rvp = rvp;
89056678Seric 					break;
89156678Seric 				}
89253654Seric 			}
89316914Seric 			if (*rvp != NULL)
89416914Seric 				*rvp++ = NULL;
89556823Seric 			if (xpvp != NULL)
89656823Seric 			{
89756823Seric 				cataddr(xpvp, replac,
89856823Seric 					&pvpbuf[sizeof pvpbuf] - replac);
89956823Seric 				*++arg_rvp = replac;
90056823Seric 			}
90156823Seric 			*++arg_rvp = NULL;
90216914Seric 
90316914Seric 			/* save the remainder of the input string */
90416914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
90516914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
90616914Seric 
90756678Seric 			/* look it up */
90856678Seric 			cataddr(key_rvp, buf, sizeof buf);
90956823Seric 			argvect[0] = buf;
91056678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
91156836Seric 			{
91256836Seric 				int bsize = sizeof buf - 1;
91356836Seric 
91456836Seric 				if (map->s_map.map_app != NULL)
91556836Seric 					bsize -= strlen(map->s_map.map_app);
91656836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
91756823Seric 						buf, sizeof buf - 1, argvect);
91856836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
91956836Seric 					strcat(replac, map->s_map.map_app);
92056836Seric 			}
92153654Seric 			else
92256678Seric 				replac = NULL;
92356678Seric 
92456678Seric 			/* if no replacement, use default */
92556823Seric 			if (replac == NULL && default_rvp != NULL)
92656823Seric 			{
92756823Seric 				char buf2[sizeof buf];
92856823Seric 
92956823Seric 				/* rewrite the default with % translations */
93056823Seric 				cataddr(default_rvp, buf2, sizeof buf2);
93156823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
93256823Seric 					argvect);
93356823Seric 				replac = buf;
93456823Seric 			}
93556823Seric 
93656678Seric 			if (replac == NULL)
93751317Seric 			{
93856823Seric 				xpvp = key_rvp;
93953654Seric 			}
94056678Seric 			else
94153654Seric 			{
94256678Seric 				/* scan the new replacement */
94356678Seric 				olddelimchar = DelimChar;
94456678Seric 				xpvp = prescan(replac, '\0', pvpbuf);
94556678Seric 				DelimChar = olddelimchar;
94653654Seric 				if (xpvp == NULL)
94751317Seric 				{
94856678Seric 					syserr("rewrite: cannot prescan map value: %s", replac);
94956678Seric 					return;
95056678Seric 				}
95151317Seric 			}
95251317Seric 
95316914Seric 			/* append it to the token list */
95456678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
95556678Seric 			{
95617174Seric 				*avp++ = newstr(*xpvp);
95716920Seric 				if (avp >= &npvp[MAXATOM])
95816914Seric 					goto toolong;
95917174Seric 			}
96016914Seric 
96116914Seric 			/* restore the old trailing information */
96256678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
96316920Seric 				if (avp >= &npvp[MAXATOM])
96416914Seric 					goto toolong;
96517174Seric 
96656678Seric 			break;
96716914Seric 		}
96816914Seric 
96916914Seric 		/*
97016914Seric 		**  Check for subroutine calls.
97116914Seric 		*/
97216914Seric 
97356678Seric 		if (*npvp != NULL && **npvp == CALLSUBR)
97456678Seric 		{
97556678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
97656678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
97756678Seric 			if (tTd(21, 3))
97856678Seric 				printf("-----callsubr %s\n", npvp[1]);
97956678Seric 			rewrite(pvp, atoi(npvp[1]));
98056678Seric 		}
98156678Seric 		else
98256678Seric 		{
98317348Seric 			bcopy((char *) npvp, (char *) pvp,
98416900Seric 				(int) (avp - npvp) * sizeof *avp);
98556678Seric 		}
9869374Seric 		if (tTd(21, 4))
9879374Seric 		{
9889374Seric 			printf("rewritten as:");
98956678Seric 			printav(pvp);
9909374Seric 		}
991297Seric 	}
9928069Seric 
9939279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
9948069Seric 	{
9958959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
99656678Seric 		printav(pvp);
9978069Seric 	}
9983149Seric }
9993149Seric /*
10003149Seric **  BUILDADDR -- build address from token vector.
10013149Seric **
10023149Seric **	Parameters:
10033149Seric **		tv -- token vector.
10043149Seric **		a -- pointer to address descriptor to fill.
10053149Seric **			If NULL, one will be allocated.
10063149Seric **
10073149Seric **	Returns:
10084279Seric **		NULL if there was an error.
10094279Seric **		'a' otherwise.
10103149Seric **
10113149Seric **	Side Effects:
10123149Seric **		fills in 'a'
10133149Seric */
10143149Seric 
101557249Seric struct errcodes
101657249Seric {
101757249Seric 	char	*ec_name;		/* name of error code */
101857249Seric 	int	ec_code;		/* numeric code */
101957249Seric } ErrorCodes[] =
102057249Seric {
102157249Seric 	"usage",	EX_USAGE,
102257249Seric 	"nouser",	EX_NOUSER,
102357249Seric 	"nohost",	EX_NOHOST,
102457249Seric 	"unavailable",	EX_UNAVAILABLE,
102557249Seric 	"software",	EX_SOFTWARE,
102657249Seric 	"tempfail",	EX_TEMPFAIL,
102757249Seric 	"protocol",	EX_PROTOCOL,
102857249Seric #ifdef EX_CONFIG
102957249Seric 	"config",	EX_CONFIG,
103057249Seric #endif
103157249Seric 	NULL,		EX_UNAVAILABLE,
103257249Seric };
103357249Seric 
103456678Seric ADDRESS *
10353149Seric buildaddr(tv, a)
10363149Seric 	register char **tv;
10373149Seric 	register ADDRESS *a;
10383149Seric {
10393149Seric 	struct mailer **mp;
10403149Seric 	register struct mailer *m;
104157402Seric 	static char buf[MAXNAME];
10423149Seric 
10433149Seric 	if (a == NULL)
10443149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
104516889Seric 	bzero((char *) a, sizeof *a);
10463149Seric 
10473149Seric 	/* figure out what net/mailer to use */
104856678Seric 	if (**tv != CANONNET)
10494279Seric 	{
10503149Seric 		syserr("buildaddr: no net");
10514279Seric 		return (NULL);
10524279Seric 	}
10533149Seric 	tv++;
105433725Sbostic 	if (!strcasecmp(*tv, "error"))
10554279Seric 	{
105610183Seric 		if (**++tv == CANONHOST)
105710183Seric 		{
105857249Seric 			register struct errcodes *ep;
105957249Seric 
106057249Seric 			if (isdigit(**++tv))
106157249Seric 			{
106257249Seric 				setstat(atoi(*tv));
106357249Seric 			}
106457249Seric 			else
106557249Seric 			{
106657249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
106757249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
106857249Seric 						break;
106957249Seric 				setstat(ep->ec_code);
107057249Seric 			}
107110183Seric 			tv++;
107210183Seric 		}
107310183Seric 		if (**tv != CANONUSER)
10744279Seric 			syserr("buildaddr: error: no user");
107556678Seric 		buf[0] = '\0';
10764279Seric 		while (*++tv != NULL)
10774279Seric 		{
10784279Seric 			if (buf[0] != '\0')
10797005Seric 				(void) strcat(buf, " ");
10807005Seric 			(void) strcat(buf, *tv);
10814279Seric 		}
10824279Seric 		usrerr(buf);
10834279Seric 		return (NULL);
10844279Seric 	}
108557402Seric 
10864598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
10873149Seric 	{
108833725Sbostic 		if (!strcasecmp(m->m_name, *tv))
10893149Seric 			break;
10903149Seric 	}
10913149Seric 	if (m == NULL)
10924279Seric 	{
109324944Seric 		syserr("buildaddr: unknown mailer %s", *tv);
10944279Seric 		return (NULL);
10954279Seric 	}
10964598Seric 	a->q_mailer = m;
10973149Seric 
10983149Seric 	/* figure out what host (if any) */
109956678Seric 	tv++;
110057249Seric 	if (!bitnset(M_LOCAL, m->m_flags))
11013149Seric 	{
110257249Seric 		if (**tv++ != CANONHOST)
11034279Seric 		{
11043149Seric 			syserr("buildaddr: no host");
11054279Seric 			return (NULL);
11064279Seric 		}
11075704Seric 		buf[0] = '\0';
110857249Seric 		while (*tv != NULL && **tv != CANONUSER)
110957249Seric 			(void) strcat(buf, *tv++);
11105704Seric 		a->q_host = newstr(buf);
11113149Seric 	}
111257249Seric 	else
111357249Seric 		a->q_host = NULL;
11143149Seric 
11153149Seric 	/* figure out the user */
111636615Sbostic 	if (*tv == NULL || **tv != CANONUSER)
11174279Seric 	{
11183149Seric 		syserr("buildaddr: no user");
11194279Seric 		return (NULL);
11204279Seric 	}
112119040Seric 
112256678Seric 	if (m == LocalMailer && tv[1] != NULL && strcmp(tv[1], "@") == 0)
112356678Seric 	{
112456678Seric 		tv++;
112556678Seric 		a->q_flags |= QNOTREMOTE;
112656678Seric 	}
112757402Seric 	tv++;
112851317Seric 
112957402Seric 	/* do special mapping for local mailer */
113057402Seric 	if (m == LocalMailer && *tv != NULL)
113157402Seric 	{
113257454Seric 		register char *p = *tv;
113357454Seric 
113457454Seric 		if (*p == '"')
113557454Seric 			p++;
113657454Seric 		if (*p == '|')
113757402Seric 			a->q_mailer = m = ProgMailer;
113857454Seric 		else if (*p == '/')
113957402Seric 			a->q_mailer = m = FileMailer;
114057454Seric 		else if (*p == ':')
114157402Seric 		{
114257402Seric 			/* may be :include: */
114357402Seric 			cataddr(tv, buf, sizeof buf);
114457454Seric 			p = buf;
114557454Seric 			if (*p == '"')
114657454Seric 				p++;
114757454Seric 			p[9] = '\0';
114857454Seric 			if (strcasecmp(p, ":include:") == 0)
114957402Seric 				a->q_mailer = m = InclMailer;
115057402Seric 		}
115157402Seric 	}
115257402Seric 
115319040Seric 	/* rewrite according recipient mailer rewriting rules */
115457402Seric 	rewrite(tv, 2);
115556327Seric 	if (m->m_r_rwset > 0)
115656327Seric 		rewrite(tv, m->m_r_rwset);
115719040Seric 	rewrite(tv, 4);
115819040Seric 
115919040Seric 	/* save the result for the command line/RCPT argument */
116011278Seric 	cataddr(tv, buf, sizeof buf);
11613149Seric 	a->q_user = buf;
11623149Seric 
11633149Seric 	return (a);
11643149Seric }
11653188Seric /*
11664228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
11674228Seric **
11684228Seric **	Parameters:
11694228Seric **		pvp -- parameter vector to rebuild.
11704228Seric **		buf -- buffer to build the string into.
11714228Seric **		sz -- size of buf.
11724228Seric **
11734228Seric **	Returns:
11744228Seric **		none.
11754228Seric **
11764228Seric **	Side Effects:
11774228Seric **		Destroys buf.
11784228Seric */
11794228Seric 
11804228Seric cataddr(pvp, buf, sz)
11814228Seric 	char **pvp;
11824228Seric 	char *buf;
11834228Seric 	register int sz;
11844228Seric {
11854228Seric 	bool oatomtok = FALSE;
118656678Seric 	bool natomtok = FALSE;
11874228Seric 	register int i;
11884228Seric 	register char *p;
11894228Seric 
11908423Seric 	if (pvp == NULL)
11918423Seric 	{
119223109Seric 		(void) strcpy(buf, "");
11938423Seric 		return;
11948423Seric 	}
11954228Seric 	p = buf;
119611156Seric 	sz -= 2;
11974228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
11984228Seric 	{
11998078Seric 		natomtok = (toktype(**pvp) == ATM);
12004228Seric 		if (oatomtok && natomtok)
12019042Seric 			*p++ = SpaceSub;
12024228Seric 		(void) strcpy(p, *pvp);
12034228Seric 		oatomtok = natomtok;
12044228Seric 		p += i;
120511156Seric 		sz -= i + 1;
12064228Seric 		pvp++;
12074228Seric 	}
12084228Seric 	*p = '\0';
12094228Seric }
12104228Seric /*
12113188Seric **  SAMEADDR -- Determine if two addresses are the same
12123188Seric **
12133188Seric **	This is not just a straight comparison -- if the mailer doesn't
12143188Seric **	care about the host we just ignore it, etc.
12153188Seric **
12163188Seric **	Parameters:
12173188Seric **		a, b -- pointers to the internal forms to compare.
12183188Seric **
12193188Seric **	Returns:
12203188Seric **		TRUE -- they represent the same mailbox.
12213188Seric **		FALSE -- they don't.
12223188Seric **
12233188Seric **	Side Effects:
12243188Seric **		none.
12253188Seric */
12263188Seric 
12273188Seric bool
12289374Seric sameaddr(a, b)
12293188Seric 	register ADDRESS *a;
12303188Seric 	register ADDRESS *b;
12313188Seric {
12323188Seric 	/* if they don't have the same mailer, forget it */
12333188Seric 	if (a->q_mailer != b->q_mailer)
12343188Seric 		return (FALSE);
12353188Seric 
12363188Seric 	/* if the user isn't the same, we can drop out */
123756678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
12383188Seric 		return (FALSE);
12393188Seric 
12403188Seric 	/* if the mailer ignores hosts, we have succeeded! */
124110690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
12423188Seric 		return (TRUE);
12433188Seric 
12443188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
12453188Seric 	if (a->q_host == NULL || b->q_host == NULL)
12463188Seric 		return (FALSE);
124756678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
12483188Seric 		return (FALSE);
12493188Seric 
12503188Seric 	return (TRUE);
12513188Seric }
12523234Seric /*
12533234Seric **  PRINTADDR -- print address (for debugging)
12543234Seric **
12553234Seric **	Parameters:
12563234Seric **		a -- the address to print
12573234Seric **		follow -- follow the q_next chain.
12583234Seric **
12593234Seric **	Returns:
12603234Seric **		none.
12613234Seric **
12623234Seric **	Side Effects:
12633234Seric **		none.
12643234Seric */
12653234Seric 
12663234Seric printaddr(a, follow)
12673234Seric 	register ADDRESS *a;
12683234Seric 	bool follow;
12693234Seric {
12705001Seric 	bool first = TRUE;
12715001Seric 
12723234Seric 	while (a != NULL)
12733234Seric 	{
12745001Seric 		first = FALSE;
12754443Seric 		printf("%x=", a);
12764085Seric 		(void) fflush(stdout);
127740973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
127840973Sbostic 		       a->q_paddr, a->q_mailer->m_mno, a->q_mailer->m_name,
127940973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
12808181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
12818181Seric 		       a->q_alias);
12828181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
12838181Seric 		       a->q_fullname);
12844996Seric 
12853234Seric 		if (!follow)
12863234Seric 			return;
12874996Seric 		a = a->q_next;
12883234Seric 	}
12895001Seric 	if (first)
12904443Seric 		printf("[NULL]\n");
12913234Seric }
12924317Seric 
12937682Seric /*
12947682Seric **  REMOTENAME -- return the name relative to the current mailer
12957682Seric **
12967682Seric **	Parameters:
12977682Seric **		name -- the name to translate.
12988069Seric **		m -- the mailer that we want to do rewriting relative
12998069Seric **			to.
13008069Seric **		senderaddress -- if set, uses the sender rewriting rules
13018069Seric **			rather than the recipient rewriting rules.
130210310Seric **		canonical -- if set, strip out any comment information,
130310310Seric **			etc.
13047682Seric **
13057682Seric **	Returns:
13067682Seric **		the text string representing this address relative to
13077682Seric **			the receiving mailer.
13087682Seric **
13097682Seric **	Side Effects:
13107682Seric **		none.
13117682Seric **
13127682Seric **	Warnings:
13137682Seric **		The text string returned is tucked away locally;
13147682Seric **			copy it if you intend to save it.
13157682Seric */
13167682Seric 
13177682Seric char *
131856678Seric remotename(name, m, senderaddress, canonical, e)
13197682Seric 	char *name;
132056678Seric 	struct mailer *m;
13218069Seric 	bool senderaddress;
132210310Seric 	bool canonical;
132356678Seric 	register ENVELOPE *e;
13247682Seric {
13258069Seric 	register char **pvp;
13268069Seric 	char *fancy;
132756678Seric 	extern char *macvalue();
132856678Seric 	char *oldg = macvalue('g', e);
13297682Seric 	static char buf[MAXNAME];
13307682Seric 	char lbuf[MAXNAME];
133116914Seric 	char pvpbuf[PSBUFSIZE];
133256678Seric 	extern char **prescan();
133356678Seric 	extern char *crackaddr();
13347682Seric 
13357755Seric 	if (tTd(12, 1))
13367755Seric 		printf("remotename(%s)\n", name);
13377755Seric 
133810177Seric 	/* don't do anything if we are tagging it as special */
133956327Seric 	if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0)
134010177Seric 		return (name);
134110177Seric 
13427682Seric 	/*
13438181Seric 	**  Do a heuristic crack of this name to extract any comment info.
13448181Seric 	**	This will leave the name as a comment and a $g macro.
13457889Seric 	*/
13467889Seric 
134710310Seric 	if (canonical)
134816155Seric 		fancy = "\001g";
134910310Seric 	else
135010310Seric 		fancy = crackaddr(name);
13517889Seric 
13528181Seric 	/*
13538181Seric 	**  Turn the name into canonical form.
13548181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
13558181Seric 	**	If this only resolves to "user", and the "C" flag is
13568181Seric 	**	specified in the sending mailer, then the sender's
13578181Seric 	**	domain will be appended.
13588181Seric 	*/
13598181Seric 
136016914Seric 	pvp = prescan(name, '\0', pvpbuf);
13617889Seric 	if (pvp == NULL)
13627889Seric 		return (name);
13638181Seric 	rewrite(pvp, 3);
136456678Seric 	if (e->e_fromdomain != NULL)
13658181Seric 	{
13668181Seric 		/* append from domain to this address */
13678181Seric 		register char **pxp = pvp;
13688181Seric 
13699594Seric 		/* see if there is an "@domain" in the current name */
13708181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
13718181Seric 			pxp++;
13728181Seric 		if (*pxp == NULL)
13738181Seric 		{
13749594Seric 			/* no.... append the "@domain" from the sender */
137556678Seric 			register char **qxq = e->e_fromdomain;
13768181Seric 
13779594Seric 			while ((*pxp++ = *qxq++) != NULL)
13789594Seric 				continue;
137911726Seric 			rewrite(pvp, 3);
13808181Seric 		}
13818181Seric 	}
13828181Seric 
13838181Seric 	/*
13848959Seric 	**  Do more specific rewriting.
138556678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
138656678Seric 	**		a sender address or not.
13878181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
13888181Seric 	*/
13898181Seric 
13908069Seric 	if (senderaddress)
13917755Seric 	{
139256327Seric 		rewrite(pvp, 1);
139356327Seric 		if (m->m_s_rwset > 0)
139456327Seric 			rewrite(pvp, m->m_s_rwset);
13958069Seric 	}
13968069Seric 	else
13978069Seric 	{
139856327Seric 		rewrite(pvp, 2);
139956327Seric 		if (m->m_r_rwset > 0)
140056327Seric 			rewrite(pvp, m->m_r_rwset);
14017682Seric 	}
14027682Seric 
14038181Seric 	/*
14048959Seric 	**  Do any final sanitation the address may require.
14058959Seric 	**	This will normally be used to turn internal forms
14068959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
14078959Seric 	**	may be used as a default to the above rules.
14088959Seric 	*/
14098959Seric 
14108959Seric 	rewrite(pvp, 4);
14118959Seric 
14128959Seric 	/*
14138181Seric 	**  Now restore the comment information we had at the beginning.
14148181Seric 	*/
14158181Seric 
14167682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
141756678Seric 	define('g', lbuf, e);
141856678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
141956678Seric 	define('g', oldg, e);
14207682Seric 
14217682Seric 	if (tTd(12, 1))
14227755Seric 		printf("remotename => `%s'\n", buf);
14237682Seric 	return (buf);
14247682Seric }
142551317Seric /*
142656678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
142751317Seric **
142851317Seric **	Parameters:
142956678Seric **		a -- the address to map (but just the user name part).
143056678Seric **		sendq -- the sendq in which to install any replacement
143156678Seric **			addresses.
143251317Seric **
143351317Seric **	Returns:
143451317Seric **		none.
143551317Seric */
143651317Seric 
143756678Seric maplocaluser(a, sendq, e)
143856678Seric 	register ADDRESS *a;
143956678Seric 	ADDRESS **sendq;
144056678Seric 	ENVELOPE *e;
144151317Seric {
144256678Seric 	register char **pvp;
144356678Seric 	register ADDRESS *a1 = NULL;
144456678Seric 	char pvpbuf[PSBUFSIZE];
144551317Seric 
144656678Seric 	if (tTd(29, 1))
144756678Seric 	{
144856678Seric 		printf("maplocaluser: ");
144956678Seric 		printaddr(a, FALSE);
145056678Seric 	}
145156678Seric 	pvp = prescan(a->q_user, '\0', pvpbuf);
145256678Seric 	if (pvp == NULL)
145356678Seric 		return;
145451317Seric 
145556678Seric 	rewrite(pvp, 5);
145656678Seric 	if (pvp[0] == NULL || pvp[0][0] != CANONNET)
145756678Seric 		return;
145851317Seric 
145956678Seric 	/* if non-null, mailer destination specified -- has it changed? */
146056678Seric 	a1 = buildaddr(pvp, NULL);
146156678Seric 	if (a1 == NULL || sameaddr(a, a1))
146256678Seric 		return;
146351317Seric 
146456678Seric 	/* mark old address as dead; insert new address */
146556678Seric 	a->q_flags |= QDONTSEND;
146656678Seric 	a1->q_alias = a;
146756678Seric 	allocaddr(a1, 1, NULL);
146856678Seric 	(void) recipient(a1, sendq, e);
146951317Seric }
1470