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*56729Seric static char sccsid[] = "@(#)parseaddr.c	5.23 (Berkeley) 11/13/92";
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();
69297Seric 
70297Seric 	/*
71297Seric 	**  Initialize and prescan address.
72297Seric 	*/
73297Seric 
7456678Seric 	e->e_to = addr;
757675Seric 	if (tTd(20, 1))
769888Seric 		printf("\n--parseaddr(%s)\n", addr);
773188Seric 
7816914Seric 	pvp = prescan(addr, delim, pvpbuf);
793149Seric 	if (pvp == NULL)
80*56729Seric 	{
81*56729Seric 		if (tTd(20, 1))
82*56729Seric 			printf("parseaddr-->NULL\n");
83297Seric 		return (NULL);
84*56729Seric 	}
85297Seric 
86297Seric 	/*
873149Seric 	**  Apply rewriting rules.
887889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
89297Seric 	*/
90297Seric 
918181Seric 	rewrite(pvp, 3);
924070Seric 	rewrite(pvp, 0);
93297Seric 
943149Seric 	/*
953149Seric 	**  See if we resolved to a real mailer.
963149Seric 	*/
97297Seric 
983149Seric 	if (pvp[0][0] != CANONNET)
993149Seric 	{
1003149Seric 		setstat(EX_USAGE);
1013149Seric 		usrerr("cannot resolve name");
1023149Seric 		return (NULL);
103297Seric 	}
104297Seric 
105297Seric 	/*
1063149Seric 	**  Build canonical address from pvp.
107297Seric 	*/
108297Seric 
1093149Seric 	a = buildaddr(pvp, a);
1104279Seric 	if (a == NULL)
1114279Seric 		return (NULL);
112297Seric 
113297Seric 	/*
1143149Seric 	**  Make local copies of the host & user and then
1153149Seric 	**  transport them out.
116297Seric 	*/
117297Seric 
11856678Seric 	allocaddr(a, copyf, addr);
11956678Seric 
12056678Seric 	/*
12156678Seric 	**  Compute return value.
12256678Seric 	*/
12356678Seric 
12456678Seric 	if (tTd(20, 1))
1258078Seric 	{
12656678Seric 		printf("parseaddr-->");
12756678Seric 		printaddr(a, FALSE);
12856678Seric 	}
12956678Seric 
13056678Seric 	return (a);
13156678Seric }
13256678Seric /*
13356678Seric **  ALLOCADDR -- do local allocations of address on demand.
13456678Seric **
13556678Seric **	Also lowercases the host name if requested.
13656678Seric **
13756678Seric **	Parameters:
13856678Seric **		a -- the address to reallocate.
13956678Seric **		copyf -- the copy flag (see parseaddr for description).
14056678Seric **		paddr -- the printname of the address.
14156678Seric **
14256678Seric **	Returns:
14356678Seric **		none.
14456678Seric **
14556678Seric **	Side Effects:
14656678Seric **		Copies portions of a into local buffers as requested.
14756678Seric */
14856678Seric 
14956678Seric allocaddr(a, copyf, paddr)
15056678Seric 	register ADDRESS *a;
15156678Seric 	int copyf;
15256678Seric 	char *paddr;
15356678Seric {
15456678Seric 	register MAILER *m = a->q_mailer;
15556678Seric 
15656678Seric 	if (copyf > 0 && paddr != NULL)
15756678Seric 	{
15856678Seric 		extern char *DelimChar;
1598078Seric 		char savec = *DelimChar;
1608078Seric 
1618078Seric 		*DelimChar = '\0';
16256678Seric 		a->q_paddr = newstr(paddr);
1638078Seric 		*DelimChar = savec;
1648078Seric 	}
165297Seric 	else
16656678Seric 		a->q_paddr = paddr;
16724944Seric 
16824944Seric 	if (a->q_user == NULL)
16924944Seric 		a->q_user = "";
17024944Seric 	if (a->q_host == NULL)
17124944Seric 		a->q_host = "";
17224944Seric 
1733149Seric 	if (copyf >= 0)
174297Seric 	{
17524944Seric 		a->q_host = newstr(a->q_host);
1763149Seric 		if (a->q_user != a->q_paddr)
1773149Seric 			a->q_user = newstr(a->q_user);
178297Seric 	}
179297Seric 
18056678Seric 	if (a->q_paddr == NULL)
18156678Seric 		a->q_paddr = a->q_user;
18256678Seric 
183297Seric 	/*
18416202Seric 	**  Convert host name to lower case if requested.
18516202Seric 	**	User name will be done later.
18616202Seric 	*/
18716202Seric 
18816202Seric 	if (!bitnset(M_HST_UPPER, m->m_flags))
18916202Seric 		makelower(a->q_host);
190297Seric }
191297Seric /*
19216162Seric **  LOWERADDR -- map UPPER->lower case on addresses as requested.
19316162Seric **
19416162Seric **	Parameters:
19516162Seric **		a -- address to be mapped.
19616162Seric **
19716162Seric **	Returns:
19816162Seric **		none.
19916162Seric **
20016162Seric **	Side Effects:
20116162Seric **		none.
20216162Seric */
20316162Seric 
20416162Seric loweraddr(a)
20516162Seric 	register ADDRESS *a;
20616162Seric {
20716162Seric 	register MAILER *m = a->q_mailer;
20816162Seric 
20916162Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
21016162Seric 		makelower(a->q_user);
21116162Seric }
21216162Seric /*
213297Seric **  PRESCAN -- Prescan name and make it canonical
214297Seric **
2159374Seric **	Scans a name and turns it into a set of tokens.  This process
2169374Seric **	deletes blanks and comments (in parentheses).
217297Seric **
218297Seric **	This routine knows about quoted strings and angle brackets.
219297Seric **
220297Seric **	There are certain subtleties to this routine.  The one that
221297Seric **	comes to mind now is that backslashes on the ends of names
222297Seric **	are silently stripped off; this is intentional.  The problem
223297Seric **	is that some versions of sndmsg (like at LBL) set the kill
224297Seric **	character to something other than @ when reading addresses;
225297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
226297Seric **	berknet mailer.
227297Seric **
228297Seric **	Parameters:
229297Seric **		addr -- the name to chomp.
230297Seric **		delim -- the delimiter for the address, normally
231297Seric **			'\0' or ','; \0 is accepted in any case.
23215284Seric **			If '\t' then we are reading the .cf file.
23316914Seric **		pvpbuf -- place to put the saved text -- note that
23416914Seric **			the pointers are static.
235297Seric **
236297Seric **	Returns:
2373149Seric **		A pointer to a vector of tokens.
238297Seric **		NULL on error.
239297Seric **
240297Seric **	Side Effects:
24125279Seric **		sets DelimChar to point to the character matching 'delim'.
242297Seric */
243297Seric 
2448078Seric /* states and character types */
2458078Seric # define OPR		0	/* operator */
2468078Seric # define ATM		1	/* atom */
2478078Seric # define QST		2	/* in quoted string */
2488078Seric # define SPC		3	/* chewing up spaces */
2498078Seric # define ONE		4	/* pick up one character */
2503149Seric 
2518078Seric # define NSTATES	5	/* number of states */
2528078Seric # define TYPE		017	/* mask to select state type */
2538078Seric 
2548078Seric /* meta bits for table */
2558078Seric # define M		020	/* meta character; don't pass through */
2568078Seric # define B		040	/* cause a break */
2578078Seric # define MB		M|B	/* meta-break */
2588078Seric 
2598078Seric static short StateTab[NSTATES][NSTATES] =
2608078Seric {
2618087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2629051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2639051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2649051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2658078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2668078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2678078Seric };
2688078Seric 
2698078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2708078Seric 
27156678Seric char	*DelimChar;		/* set to point to the delimiter */
27256678Seric 
2733149Seric char **
27416914Seric prescan(addr, delim, pvpbuf)
275297Seric 	char *addr;
276297Seric 	char delim;
27716914Seric 	char pvpbuf[];
278297Seric {
279297Seric 	register char *p;
2808078Seric 	register char *q;
2819346Seric 	register int c;
2823149Seric 	char **avp;
283297Seric 	bool bslashmode;
284297Seric 	int cmntcnt;
2858423Seric 	int anglecnt;
2863149Seric 	char *tok;
2878078Seric 	int state;
2888078Seric 	int newstate;
2898078Seric 	static char *av[MAXATOM+1];
29056678Seric 	extern int errno;
291297Seric 
29215253Seric 	/* make sure error messages don't have garbage on them */
29315253Seric 	errno = 0;
29415253Seric 
29516914Seric 	q = pvpbuf;
2963149Seric 	bslashmode = FALSE;
2977800Seric 	cmntcnt = 0;
2988423Seric 	anglecnt = 0;
2993149Seric 	avp = av;
30056678Seric 	state = ATM;
3018078Seric 	c = NOCHAR;
3028078Seric 	p = addr;
3038078Seric 	if (tTd(22, 45))
304297Seric 	{
3058078Seric 		printf("prescan: ");
3068078Seric 		xputs(p);
30723109Seric 		(void) putchar('\n');
3088078Seric 	}
3098078Seric 
3108078Seric 	do
3118078Seric 	{
3123149Seric 		/* read a token */
3133149Seric 		tok = q;
3148078Seric 		for (;;)
315297Seric 		{
3168078Seric 			/* store away any old lookahead character */
3178078Seric 			if (c != NOCHAR)
3188078Seric 			{
31915284Seric 				/* see if there is room */
32016914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3218078Seric 				{
3228078Seric 					usrerr("Address too long");
3238078Seric 					DelimChar = p;
3248078Seric 					return (NULL);
3258078Seric 				}
32615284Seric 
32715284Seric 				/* squirrel it away */
3288078Seric 				*q++ = c;
3298078Seric 			}
3308078Seric 
3318078Seric 			/* read a new input character */
3328078Seric 			c = *p++;
3338078Seric 			if (c == '\0')
3348078Seric 				break;
33515284Seric 
3368078Seric 			if (tTd(22, 101))
3378078Seric 				printf("c=%c, s=%d; ", c, state);
3388078Seric 
3393149Seric 			/* chew up special characters */
3403149Seric 			*q = '\0';
3413149Seric 			if (bslashmode)
3423149Seric 			{
34324944Seric 				/* kludge \! for naive users */
34424944Seric 				if (c != '!')
34556678Seric 					*q++ = '\\';
3463149Seric 				bslashmode = FALSE;
34756678Seric 				continue;
3483149Seric 			}
34956678Seric 
35056678Seric 			if (c == '\\')
3513149Seric 			{
3523149Seric 				bslashmode = TRUE;
3538078Seric 				c = NOCHAR;
35456678Seric 				continue;
3553149Seric 			}
35656678Seric 			else if (state == QST)
3578514Seric 			{
3588514Seric 				/* do nothing, just avoid next clauses */
3598514Seric 			}
3608078Seric 			else if (c == '(')
3614100Seric 			{
3628078Seric 				cmntcnt++;
3638078Seric 				c = NOCHAR;
3644100Seric 			}
3658078Seric 			else if (c == ')')
3663149Seric 			{
3678078Seric 				if (cmntcnt <= 0)
3683149Seric 				{
3698078Seric 					usrerr("Unbalanced ')'");
3708078Seric 					DelimChar = p;
3718078Seric 					return (NULL);
3723149Seric 				}
3738078Seric 				else
3748078Seric 					cmntcnt--;
3758078Seric 			}
3768078Seric 			else if (cmntcnt > 0)
3778078Seric 				c = NOCHAR;
3788423Seric 			else if (c == '<')
3798423Seric 				anglecnt++;
3808423Seric 			else if (c == '>')
3818423Seric 			{
3828423Seric 				if (anglecnt <= 0)
3838423Seric 				{
3848423Seric 					usrerr("Unbalanced '>'");
3858423Seric 					DelimChar = p;
3868423Seric 					return (NULL);
3878423Seric 				}
3888423Seric 				anglecnt--;
3898423Seric 			}
39011423Seric 			else if (delim == ' ' && isspace(c))
39111423Seric 				c = ' ';
3923149Seric 
3938078Seric 			if (c == NOCHAR)
3948078Seric 				continue;
3953149Seric 
3968078Seric 			/* see if this is end of input */
39711405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
3983149Seric 				break;
3993149Seric 
4008078Seric 			newstate = StateTab[state][toktype(c)];
4018078Seric 			if (tTd(22, 101))
4028078Seric 				printf("ns=%02o\n", newstate);
4038078Seric 			state = newstate & TYPE;
4048078Seric 			if (bitset(M, newstate))
4058078Seric 				c = NOCHAR;
4068078Seric 			if (bitset(B, newstate))
4074228Seric 				break;
408297Seric 		}
4093149Seric 
4103149Seric 		/* new token */
4118078Seric 		if (tok != q)
4121378Seric 		{
4138078Seric 			*q++ = '\0';
4148078Seric 			if (tTd(22, 36))
415297Seric 			{
4168078Seric 				printf("tok=");
4178078Seric 				xputs(tok);
41823109Seric 				(void) putchar('\n');
419297Seric 			}
4208078Seric 			if (avp >= &av[MAXATOM])
421297Seric 			{
4228078Seric 				syserr("prescan: too many tokens");
4238078Seric 				DelimChar = p;
4248078Seric 				return (NULL);
425297Seric 			}
4268078Seric 			*avp++ = tok;
427297Seric 		}
4288423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4293149Seric 	*avp = NULL;
4308078Seric 	DelimChar = --p;
4313149Seric 	if (cmntcnt > 0)
4323149Seric 		usrerr("Unbalanced '('");
4338423Seric 	else if (anglecnt > 0)
4348423Seric 		usrerr("Unbalanced '<'");
4358078Seric 	else if (state == QST)
4363149Seric 		usrerr("Unbalanced '\"'");
4373149Seric 	else if (av[0] != NULL)
4383149Seric 		return (av);
4393149Seric 	return (NULL);
4403149Seric }
4413149Seric /*
4423149Seric **  TOKTYPE -- return token type
4433149Seric **
4443149Seric **	Parameters:
4453149Seric **		c -- the character in question.
4463149Seric **
4473149Seric **	Returns:
4483149Seric **		Its type.
4493149Seric **
4503149Seric **	Side Effects:
4513149Seric **		none.
4523149Seric */
453297Seric 
4543149Seric toktype(c)
4553149Seric 	register char c;
4563149Seric {
4573380Seric 	static char buf[50];
4583382Seric 	static bool firstime = TRUE;
4593380Seric 
4603382Seric 	if (firstime)
4613380Seric 	{
4623382Seric 		firstime = FALSE;
46316155Seric 		expand("\001o", buf, &buf[sizeof buf - 1], CurEnv);
4647005Seric 		(void) strcat(buf, DELIMCHARS);
4653380Seric 	}
46656327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
4678078Seric 		return (ONE);
4688078Seric 	if (c == '"')
4698078Seric 		return (QST);
4704100Seric 	if (!isascii(c))
4718078Seric 		return (ATM);
4728078Seric 	if (isspace(c) || c == ')')
4738078Seric 		return (SPC);
4743380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
4758078Seric 		return (OPR);
4768078Seric 	return (ATM);
4773149Seric }
4783149Seric /*
4793149Seric **  REWRITE -- apply rewrite rules to token vector.
4803149Seric **
4814476Seric **	This routine is an ordered production system.  Each rewrite
4824476Seric **	rule has a LHS (called the pattern) and a RHS (called the
4834476Seric **	rewrite); 'rwr' points the the current rewrite rule.
4844476Seric **
4854476Seric **	For each rewrite rule, 'avp' points the address vector we
4864476Seric **	are trying to match against, and 'pvp' points to the pattern.
4878058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
4889585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
4899585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
4904476Seric **
4914476Seric **	When a match between avp & pvp does not match, we try to
4929585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
4934476Seric **	we must also back out the match in mvp.  If we reach a
4948058Seric **	MATCHANY or MATCHZANY we just extend the match and start
4958058Seric **	over again.
4964476Seric **
4974476Seric **	When we finally match, we rewrite the address vector
4984476Seric **	and try over again.
4994476Seric **
5003149Seric **	Parameters:
5013149Seric **		pvp -- pointer to token vector.
5023149Seric **
5033149Seric **	Returns:
5043149Seric **		none.
5053149Seric **
5063149Seric **	Side Effects:
5073149Seric **		pvp is modified.
5083149Seric */
5092091Seric 
5103149Seric struct match
5113149Seric {
5124468Seric 	char	**first;	/* first token matched */
5134468Seric 	char	**last;		/* last token matched */
5143149Seric };
5153149Seric 
5164468Seric # define MAXMATCH	9	/* max params per rewrite */
5173149Seric 
5183149Seric 
5194070Seric rewrite(pvp, ruleset)
5203149Seric 	char **pvp;
5214070Seric 	int ruleset;
5223149Seric {
5233149Seric 	register char *ap;		/* address pointer */
5243149Seric 	register char *rp;		/* rewrite pointer */
5253149Seric 	register char **avp;		/* address vector pointer */
5263149Seric 	register char **rvp;		/* rewrite vector pointer */
5278058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5288058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
52956678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5303149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5313149Seric 
5329279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5333149Seric 	{
5348959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
53556678Seric 		printav(pvp);
5363149Seric 	}
53756678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
53856326Seric 	{
53956678Seric 		syserr("rewrite: illegal ruleset number %d", ruleset);
54056326Seric 		return;
54156326Seric 	}
54256678Seric 	if (pvp == NULL)
54356678Seric 		return;
54456326Seric 
5453149Seric 	/*
54656678Seric 	**  Run through the list of rewrite rules, applying
54756678Seric 	**	any that match.
5483149Seric 	*/
5493149Seric 
5504070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
5513149Seric 	{
55256678Seric 		int loopcount = 0;
55356678Seric 
5547675Seric 		if (tTd(21, 12))
555297Seric 		{
5568069Seric 			printf("-----trying rule:");
55756678Seric 			printav(rwr->r_lhs);
5583149Seric 		}
5593149Seric 
5603149Seric 		/* try to match on this rule */
5614468Seric 		mlp = mlist;
5628058Seric 		rvp = rwr->r_lhs;
5638058Seric 		avp = pvp;
5648058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
5653149Seric 		{
56656678Seric 			if (++loopcount > 100)
56752637Seric 			{
56856678Seric 				syserr("Infinite loop in ruleset %d", ruleset);
56956678Seric 				printf("workspace: ");
57056678Seric 				printav(pvp);
57152637Seric 				break;
57252637Seric 			}
5733149Seric 			rp = *rvp;
5748058Seric 			if (tTd(21, 35))
5758058Seric 			{
57656678Seric 				printf("operator=");
5778058Seric 				xputs(ap);
57856678Seric 				printf(", token=");
5798058Seric 				xputs(rp);
5808069Seric 				printf("\n");
5818058Seric 			}
58256678Seric 			if (rp == NULL)
58356326Seric 			{
5843149Seric 				/* end-of-pattern before end-of-address */
5858058Seric 				goto backup;
58656678Seric 			}
58756678Seric 			if (ap == NULL && *rp != MATCHZANY)
58856326Seric 			{
58956678Seric 				/* end-of-input */
59056678Seric 				break;
591297Seric 			}
59256326Seric 
59356678Seric 			switch (*rp)
5948058Seric 			{
59556678Seric 				register STAB *s;
59656326Seric 
59756678Seric 			  case MATCHCLASS:
59856678Seric 			  case MATCHNCLASS:
59956678Seric 				/* match any token in (not in) a class */
60056678Seric 				s = stab(ap, ST_CLASS, ST_FIND);
60156678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
60256326Seric 				{
60356678Seric 					if (*rp == MATCHCLASS)
6049585Seric 						goto backup;
60556326Seric 				}
60656678Seric 				else if (*rp == MATCHNCLASS)
60756678Seric 					goto backup;
6084060Seric 
60956678Seric 				/* explicit fall-through */
61056678Seric 
61156678Seric 			  case MATCHONE:
61256678Seric 			  case MATCHANY:
61356678Seric 				/* match exactly one token */
61456678Seric 				mlp->first = avp;
61556678Seric 				mlp->last = avp++;
6168058Seric 				mlp++;
61756678Seric 				break;
6188058Seric 
61956678Seric 			  case MATCHZANY:
62056678Seric 				/* match zero or more tokens */
62156678Seric 				mlp->first = avp;
62256678Seric 				mlp->last = avp - 1;
62356678Seric 				mlp++;
62456678Seric 				break;
62556326Seric 
62656678Seric 			  default:
62756678Seric 				/* must have exact match */
62856678Seric 				if (strcasecmp(rp, ap))
6298058Seric 					goto backup;
6304468Seric 				avp++;
63156678Seric 				break;
6323149Seric 			}
6333149Seric 
63456678Seric 			/* successful match on this token */
6353149Seric 			rvp++;
6363149Seric 			continue;
6373149Seric 
63856678Seric 		  backup:
63956678Seric 			/* match failed -- back up */
64056678Seric 			while (--rvp >= rwr->r_lhs)
6413149Seric 			{
64256678Seric 				rp = *rvp;
64356678Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
6444468Seric 				{
64556678Seric 					/* extend binding and continue */
64656678Seric 					avp = ++mlp[-1].last;
64756678Seric 					avp++;
64856678Seric 					rvp++;
64956678Seric 					break;
6504468Seric 				}
65156678Seric 				avp--;
65256678Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS ||
65356678Seric 				    *rp == MATCHNCLASS)
65456678Seric 				{
65556678Seric 					/* back out binding */
65656678Seric 					mlp--;
65756678Seric 				}
6583149Seric 			}
6593149Seric 
66056678Seric 			if (rvp < rwr->r_lhs)
66156678Seric 			{
66256678Seric 				/* total failure to match */
66356326Seric 				break;
6643149Seric 			}
665297Seric 		}
6663149Seric 
6673149Seric 		/*
66856678Seric 		**  See if we successfully matched
6693149Seric 		*/
6703149Seric 
67156678Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
6723149Seric 		{
6739374Seric 			if (tTd(21, 10))
6749374Seric 				printf("----- rule fails\n");
6759374Seric 			rwr = rwr->r_next;
6769374Seric 			continue;
6779374Seric 		}
6783149Seric 
6799374Seric 		rvp = rwr->r_rhs;
6809374Seric 		if (tTd(21, 12))
6819374Seric 		{
6829374Seric 			printf("-----rule matches:");
68356678Seric 			printav(rvp);
6849374Seric 		}
6859374Seric 
6869374Seric 		rp = *rvp;
6879374Seric 		if (*rp == CANONUSER)
6889374Seric 		{
6899374Seric 			rvp++;
6909374Seric 			rwr = rwr->r_next;
6919374Seric 		}
6929374Seric 		else if (*rp == CANONHOST)
6939374Seric 		{
6949374Seric 			rvp++;
6959374Seric 			rwr = NULL;
6969374Seric 		}
6979374Seric 		else if (*rp == CANONNET)
6989374Seric 			rwr = NULL;
6999374Seric 
7009374Seric 		/* substitute */
7019374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
7029374Seric 		{
7039374Seric 			register struct match *m;
7049374Seric 			register char **pp;
7059374Seric 
7068058Seric 			rp = *rvp;
70756678Seric 			if (*rp == MATCHREPL)
7088058Seric 			{
70916914Seric 				/* substitute from LHS */
71016914Seric 				m = &mlist[rp[1] - '1'];
71156678Seric 				if (m < mlist || m >= mlp)
7129374Seric 				{
71356678Seric 					syserr("rewrite: ruleset %d: replacement $%c out of bounds",
71456326Seric 						ruleset, rp[1]);
7159374Seric 					return;
7169374Seric 				}
71716914Seric 				if (tTd(21, 15))
71816914Seric 				{
71916914Seric 					printf("$%c:", rp[1]);
72016914Seric 					pp = m->first;
72156678Seric 					while (pp <= m->last)
72216914Seric 					{
72316914Seric 						printf(" %x=\"", *pp);
72416914Seric 						(void) fflush(stdout);
72516914Seric 						printf("%s\"", *pp++);
72616914Seric 					}
72716914Seric 					printf("\n");
72816914Seric 				}
7299374Seric 				pp = m->first;
73056678Seric 				while (pp <= m->last)
7313149Seric 				{
73216914Seric 					if (avp >= &npvp[MAXATOM])
73356678Seric 					{
73456678Seric 						syserr("rewrite: expansion too long");
73556678Seric 						return;
73656678Seric 					}
73716914Seric 					*avp++ = *pp++;
7383149Seric 				}
7393149Seric 			}
74016914Seric 			else
7418226Seric 			{
74216914Seric 				/* vanilla replacement */
7439374Seric 				if (avp >= &npvp[MAXATOM])
74416889Seric 				{
74556678Seric 	toolong:
74616889Seric 					syserr("rewrite: expansion too long");
74716889Seric 					return;
74816889Seric 				}
74956678Seric 				*avp++ = rp;
7508226Seric 			}
7519374Seric 		}
7529374Seric 		*avp++ = NULL;
75316914Seric 
75416914Seric 		/*
75556678Seric 		**  Check for any hostname/keyword lookups.
75616914Seric 		*/
75716914Seric 
75816914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
75916914Seric 		{
76056678Seric 			char **hbrvp;
76116914Seric 			char **xpvp;
76216914Seric 			int trsize;
76317473Seric 			char *olddelimchar;
76456678Seric 			char *replac;
76556678Seric 			int endtoken;
76656678Seric 			STAB *map;
76756678Seric 			char *mapname;
76856678Seric 			char **key_rvp;
76956678Seric 			char **arg_rvp;
77056678Seric 			char **default_rvp;
77156678Seric 			char buf[MAXNAME + 1];
77216914Seric 			char *pvpb1[MAXATOM + 1];
77317174Seric 			char pvpbuf[PSBUFSIZE];
77456678Seric 			extern char *DelimChar;
77516914Seric 
77656678Seric 			if (**rvp != HOSTBEGIN && **rvp != LOOKUPBEGIN)
77716914Seric 				continue;
77816914Seric 
77916914Seric 			/*
78056678Seric 			**  Got a hostname/keyword lookup.
78116914Seric 			**
78216914Seric 			**	This could be optimized fairly easily.
78316914Seric 			*/
78416914Seric 
78516914Seric 			hbrvp = rvp;
78656678Seric 			arg_rvp = default_rvp = NULL;
78756678Seric 			if (**rvp == HOSTBEGIN)
78856327Seric 			{
78956678Seric 				endtoken = HOSTEND;
79056678Seric 				mapname = "host";
79156327Seric 			}
79256326Seric 			else
79356327Seric 			{
79456678Seric 				endtoken = LOOKUPEND;
79556678Seric 				mapname = *++rvp;
79656327Seric 			}
79756678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
79856678Seric 			if (map == NULL)
79956678Seric 				syserr("rewrite: map %s not found", mapname);
80056678Seric 
80156678Seric 			/* extract the match part */
80256678Seric 			key_rvp = ++rvp;
80356678Seric 			while (*rvp != NULL && **rvp != endtoken)
80453654Seric 			{
80556678Seric 				switch (**rvp)
80656678Seric 				{
80756678Seric 				  case CANONHOST:
80856678Seric 					*rvp++ = NULL;
80956678Seric 					arg_rvp = rvp;
81056678Seric 					break;
81156678Seric 
81256678Seric 				  case CANONUSER:
81356678Seric 					*rvp++ = NULL;
81456678Seric 					default_rvp = rvp;
81556678Seric 					break;
81656678Seric 
81756678Seric 				  default:
81856678Seric 					rvp++;
81956678Seric 					break;
82056678Seric 				}
82153654Seric 			}
82216914Seric 			if (*rvp != NULL)
82316914Seric 				*rvp++ = NULL;
82416914Seric 
82516914Seric 			/* save the remainder of the input string */
82616914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
82716914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
82816914Seric 
82956678Seric 			/* look it up */
83056678Seric 			cataddr(key_rvp, buf, sizeof buf);
83156678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
83256678Seric 				replac = (*map->s_map.map_class->map_lookup)(buf,
83356678Seric 						sizeof buf - 1, arg_rvp);
83453654Seric 			else
83556678Seric 				replac = NULL;
83656678Seric 
83756678Seric 			/* if no replacement, use default */
83856678Seric 			if (replac == NULL)
83951317Seric 			{
84056678Seric 				if (default_rvp != NULL)
84156678Seric 					xpvp = default_rvp;
84253654Seric 				else
84356678Seric 					xpvp = key_rvp;
84453654Seric 			}
84556678Seric 			else
84653654Seric 			{
84756678Seric 				/* scan the new replacement */
84856678Seric 				olddelimchar = DelimChar;
84956678Seric 				xpvp = prescan(replac, '\0', pvpbuf);
85056678Seric 				DelimChar = olddelimchar;
85153654Seric 				if (xpvp == NULL)
85251317Seric 				{
85356678Seric 					syserr("rewrite: cannot prescan map value: %s", replac);
85456678Seric 					return;
85556678Seric 				}
85651317Seric 			}
85751317Seric 
85816914Seric 			/* append it to the token list */
85956678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
86056678Seric 			{
86117174Seric 				*avp++ = newstr(*xpvp);
86216920Seric 				if (avp >= &npvp[MAXATOM])
86316914Seric 					goto toolong;
86417174Seric 			}
86516914Seric 
86616914Seric 			/* restore the old trailing information */
86756678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
86816920Seric 				if (avp >= &npvp[MAXATOM])
86916914Seric 					goto toolong;
87017174Seric 
87156678Seric 			break;
87216914Seric 		}
87316914Seric 
87416914Seric 		/*
87516914Seric 		**  Check for subroutine calls.
87616914Seric 		*/
87716914Seric 
87856678Seric 		if (*npvp != NULL && **npvp == CALLSUBR)
87956678Seric 		{
88056678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
88156678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
88256678Seric 			if (tTd(21, 3))
88356678Seric 				printf("-----callsubr %s\n", npvp[1]);
88456678Seric 			rewrite(pvp, atoi(npvp[1]));
88556678Seric 		}
88656678Seric 		else
88756678Seric 		{
88817348Seric 			bcopy((char *) npvp, (char *) pvp,
88916900Seric 				(int) (avp - npvp) * sizeof *avp);
89056678Seric 		}
8919374Seric 		if (tTd(21, 4))
8929374Seric 		{
8939374Seric 			printf("rewritten as:");
89456678Seric 			printav(pvp);
8959374Seric 		}
896297Seric 	}
8978069Seric 
8989279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
8998069Seric 	{
9008959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
90156678Seric 		printav(pvp);
9028069Seric 	}
9033149Seric }
9043149Seric /*
9053149Seric **  BUILDADDR -- build address from token vector.
9063149Seric **
9073149Seric **	Parameters:
9083149Seric **		tv -- token vector.
9093149Seric **		a -- pointer to address descriptor to fill.
9103149Seric **			If NULL, one will be allocated.
9113149Seric **
9123149Seric **	Returns:
9134279Seric **		NULL if there was an error.
9144279Seric **		'a' otherwise.
9153149Seric **
9163149Seric **	Side Effects:
9173149Seric **		fills in 'a'
9183149Seric */
9193149Seric 
92056678Seric ADDRESS *
9213149Seric buildaddr(tv, a)
9223149Seric 	register char **tv;
9233149Seric 	register ADDRESS *a;
9243149Seric {
9253149Seric 	static char buf[MAXNAME];
9263149Seric 	struct mailer **mp;
9273149Seric 	register struct mailer *m;
9283149Seric 
9293149Seric 	if (a == NULL)
9303149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
93116889Seric 	bzero((char *) a, sizeof *a);
9323149Seric 
9333149Seric 	/* figure out what net/mailer to use */
93456678Seric 	if (**tv != CANONNET)
9354279Seric 	{
9363149Seric 		syserr("buildaddr: no net");
9374279Seric 		return (NULL);
9384279Seric 	}
9393149Seric 	tv++;
94033725Sbostic 	if (!strcasecmp(*tv, "error"))
9414279Seric 	{
94210183Seric 		if (**++tv == CANONHOST)
94310183Seric 		{
94410183Seric 			setstat(atoi(*++tv));
94510183Seric 			tv++;
94610183Seric 		}
94710183Seric 		if (**tv != CANONUSER)
9484279Seric 			syserr("buildaddr: error: no user");
94956678Seric 		buf[0] = '\0';
9504279Seric 		while (*++tv != NULL)
9514279Seric 		{
9524279Seric 			if (buf[0] != '\0')
9537005Seric 				(void) strcat(buf, " ");
9547005Seric 			(void) strcat(buf, *tv);
9554279Seric 		}
9564279Seric 		usrerr(buf);
9574279Seric 		return (NULL);
9584279Seric 	}
9594598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
9603149Seric 	{
96133725Sbostic 		if (!strcasecmp(m->m_name, *tv))
9623149Seric 			break;
9633149Seric 	}
9643149Seric 	if (m == NULL)
9654279Seric 	{
96624944Seric 		syserr("buildaddr: unknown mailer %s", *tv);
9674279Seric 		return (NULL);
9684279Seric 	}
9694598Seric 	a->q_mailer = m;
9703149Seric 
9713149Seric 	/* figure out what host (if any) */
97256678Seric 	tv++;
97356678Seric 	if (!bitnset(M_LOCAL, m->m_flags))
9743149Seric 	{
97556678Seric 		if (**tv++ != CANONHOST)
9764279Seric 		{
9773149Seric 			syserr("buildaddr: no host");
9784279Seric 			return (NULL);
9794279Seric 		}
9805704Seric 		buf[0] = '\0';
98156678Seric 		while (*tv != NULL && **tv != CANONUSER)
98256678Seric 			(void) strcat(buf, *tv++);
9835704Seric 		a->q_host = newstr(buf);
9843149Seric 	}
98556678Seric 	else
98656678Seric 		a->q_host = NULL;
9873149Seric 
9883149Seric 	/* figure out the user */
98936615Sbostic 	if (*tv == NULL || **tv != CANONUSER)
9904279Seric 	{
9913149Seric 		syserr("buildaddr: no user");
9924279Seric 		return (NULL);
9934279Seric 	}
99419040Seric 
99556678Seric 	if (m == LocalMailer && tv[1] != NULL && strcmp(tv[1], "@") == 0)
99656678Seric 	{
99756678Seric 		tv++;
99856678Seric 		a->q_flags |= QNOTREMOTE;
99956678Seric 	}
100051317Seric 
100119040Seric 	/* rewrite according recipient mailer rewriting rules */
100219040Seric 	rewrite(++tv, 2);
100356327Seric 	if (m->m_r_rwset > 0)
100456327Seric 		rewrite(tv, m->m_r_rwset);
100519040Seric 	rewrite(tv, 4);
100619040Seric 
100719040Seric 	/* save the result for the command line/RCPT argument */
100811278Seric 	cataddr(tv, buf, sizeof buf);
10093149Seric 	a->q_user = buf;
10103149Seric 
10113149Seric 	return (a);
10123149Seric }
10133188Seric /*
10144228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
10154228Seric **
10164228Seric **	Parameters:
10174228Seric **		pvp -- parameter vector to rebuild.
10184228Seric **		buf -- buffer to build the string into.
10194228Seric **		sz -- size of buf.
10204228Seric **
10214228Seric **	Returns:
10224228Seric **		none.
10234228Seric **
10244228Seric **	Side Effects:
10254228Seric **		Destroys buf.
10264228Seric */
10274228Seric 
10284228Seric cataddr(pvp, buf, sz)
10294228Seric 	char **pvp;
10304228Seric 	char *buf;
10314228Seric 	register int sz;
10324228Seric {
10334228Seric 	bool oatomtok = FALSE;
103456678Seric 	bool natomtok = FALSE;
10354228Seric 	register int i;
10364228Seric 	register char *p;
10374228Seric 
10388423Seric 	if (pvp == NULL)
10398423Seric 	{
104023109Seric 		(void) strcpy(buf, "");
10418423Seric 		return;
10428423Seric 	}
10434228Seric 	p = buf;
104411156Seric 	sz -= 2;
10454228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
10464228Seric 	{
10478078Seric 		natomtok = (toktype(**pvp) == ATM);
10484228Seric 		if (oatomtok && natomtok)
10499042Seric 			*p++ = SpaceSub;
10504228Seric 		(void) strcpy(p, *pvp);
10514228Seric 		oatomtok = natomtok;
10524228Seric 		p += i;
105311156Seric 		sz -= i + 1;
10544228Seric 		pvp++;
10554228Seric 	}
10564228Seric 	*p = '\0';
10574228Seric }
10584228Seric /*
10593188Seric **  SAMEADDR -- Determine if two addresses are the same
10603188Seric **
10613188Seric **	This is not just a straight comparison -- if the mailer doesn't
10623188Seric **	care about the host we just ignore it, etc.
10633188Seric **
10643188Seric **	Parameters:
10653188Seric **		a, b -- pointers to the internal forms to compare.
10663188Seric **
10673188Seric **	Returns:
10683188Seric **		TRUE -- they represent the same mailbox.
10693188Seric **		FALSE -- they don't.
10703188Seric **
10713188Seric **	Side Effects:
10723188Seric **		none.
10733188Seric */
10743188Seric 
10753188Seric bool
10769374Seric sameaddr(a, b)
10773188Seric 	register ADDRESS *a;
10783188Seric 	register ADDRESS *b;
10793188Seric {
10803188Seric 	/* if they don't have the same mailer, forget it */
10813188Seric 	if (a->q_mailer != b->q_mailer)
10823188Seric 		return (FALSE);
10833188Seric 
10843188Seric 	/* if the user isn't the same, we can drop out */
108556678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
10863188Seric 		return (FALSE);
10873188Seric 
10883188Seric 	/* if the mailer ignores hosts, we have succeeded! */
108910690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
10903188Seric 		return (TRUE);
10913188Seric 
10923188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
10933188Seric 	if (a->q_host == NULL || b->q_host == NULL)
10943188Seric 		return (FALSE);
109556678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
10963188Seric 		return (FALSE);
10973188Seric 
10983188Seric 	return (TRUE);
10993188Seric }
11003234Seric /*
11013234Seric **  PRINTADDR -- print address (for debugging)
11023234Seric **
11033234Seric **	Parameters:
11043234Seric **		a -- the address to print
11053234Seric **		follow -- follow the q_next chain.
11063234Seric **
11073234Seric **	Returns:
11083234Seric **		none.
11093234Seric **
11103234Seric **	Side Effects:
11113234Seric **		none.
11123234Seric */
11133234Seric 
11143234Seric printaddr(a, follow)
11153234Seric 	register ADDRESS *a;
11163234Seric 	bool follow;
11173234Seric {
11185001Seric 	bool first = TRUE;
11195001Seric 
11203234Seric 	while (a != NULL)
11213234Seric 	{
11225001Seric 		first = FALSE;
11234443Seric 		printf("%x=", a);
11244085Seric 		(void) fflush(stdout);
112540973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
112640973Sbostic 		       a->q_paddr, a->q_mailer->m_mno, a->q_mailer->m_name,
112740973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
11288181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
11298181Seric 		       a->q_alias);
11308181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
11318181Seric 		       a->q_fullname);
11324996Seric 
11333234Seric 		if (!follow)
11343234Seric 			return;
11354996Seric 		a = a->q_next;
11363234Seric 	}
11375001Seric 	if (first)
11384443Seric 		printf("[NULL]\n");
11393234Seric }
11404317Seric 
11417682Seric /*
11427682Seric **  REMOTENAME -- return the name relative to the current mailer
11437682Seric **
11447682Seric **	Parameters:
11457682Seric **		name -- the name to translate.
11468069Seric **		m -- the mailer that we want to do rewriting relative
11478069Seric **			to.
11488069Seric **		senderaddress -- if set, uses the sender rewriting rules
11498069Seric **			rather than the recipient rewriting rules.
115010310Seric **		canonical -- if set, strip out any comment information,
115110310Seric **			etc.
11527682Seric **
11537682Seric **	Returns:
11547682Seric **		the text string representing this address relative to
11557682Seric **			the receiving mailer.
11567682Seric **
11577682Seric **	Side Effects:
11587682Seric **		none.
11597682Seric **
11607682Seric **	Warnings:
11617682Seric **		The text string returned is tucked away locally;
11627682Seric **			copy it if you intend to save it.
11637682Seric */
11647682Seric 
11657682Seric char *
116656678Seric remotename(name, m, senderaddress, canonical, e)
11677682Seric 	char *name;
116856678Seric 	struct mailer *m;
11698069Seric 	bool senderaddress;
117010310Seric 	bool canonical;
117156678Seric 	register ENVELOPE *e;
11727682Seric {
11738069Seric 	register char **pvp;
11748069Seric 	char *fancy;
117556678Seric 	extern char *macvalue();
117656678Seric 	char *oldg = macvalue('g', e);
11777682Seric 	static char buf[MAXNAME];
11787682Seric 	char lbuf[MAXNAME];
117916914Seric 	char pvpbuf[PSBUFSIZE];
118056678Seric 	extern char **prescan();
118156678Seric 	extern char *crackaddr();
11827682Seric 
11837755Seric 	if (tTd(12, 1))
11847755Seric 		printf("remotename(%s)\n", name);
11857755Seric 
118610177Seric 	/* don't do anything if we are tagging it as special */
118756327Seric 	if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0)
118810177Seric 		return (name);
118910177Seric 
11907682Seric 	/*
11918181Seric 	**  Do a heuristic crack of this name to extract any comment info.
11928181Seric 	**	This will leave the name as a comment and a $g macro.
11937889Seric 	*/
11947889Seric 
119510310Seric 	if (canonical)
119616155Seric 		fancy = "\001g";
119710310Seric 	else
119810310Seric 		fancy = crackaddr(name);
11997889Seric 
12008181Seric 	/*
12018181Seric 	**  Turn the name into canonical form.
12028181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
12038181Seric 	**	If this only resolves to "user", and the "C" flag is
12048181Seric 	**	specified in the sending mailer, then the sender's
12058181Seric 	**	domain will be appended.
12068181Seric 	*/
12078181Seric 
120816914Seric 	pvp = prescan(name, '\0', pvpbuf);
12097889Seric 	if (pvp == NULL)
12107889Seric 		return (name);
12118181Seric 	rewrite(pvp, 3);
121256678Seric 	if (e->e_fromdomain != NULL)
12138181Seric 	{
12148181Seric 		/* append from domain to this address */
12158181Seric 		register char **pxp = pvp;
12168181Seric 
12179594Seric 		/* see if there is an "@domain" in the current name */
12188181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
12198181Seric 			pxp++;
12208181Seric 		if (*pxp == NULL)
12218181Seric 		{
12229594Seric 			/* no.... append the "@domain" from the sender */
122356678Seric 			register char **qxq = e->e_fromdomain;
12248181Seric 
12259594Seric 			while ((*pxp++ = *qxq++) != NULL)
12269594Seric 				continue;
122711726Seric 			rewrite(pvp, 3);
12288181Seric 		}
12298181Seric 	}
12308181Seric 
12318181Seric 	/*
12328959Seric 	**  Do more specific rewriting.
123356678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
123456678Seric 	**		a sender address or not.
12358181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
12368181Seric 	*/
12378181Seric 
12388069Seric 	if (senderaddress)
12397755Seric 	{
124056327Seric 		rewrite(pvp, 1);
124156327Seric 		if (m->m_s_rwset > 0)
124256327Seric 			rewrite(pvp, m->m_s_rwset);
12438069Seric 	}
12448069Seric 	else
12458069Seric 	{
124656327Seric 		rewrite(pvp, 2);
124756327Seric 		if (m->m_r_rwset > 0)
124856327Seric 			rewrite(pvp, m->m_r_rwset);
12497682Seric 	}
12507682Seric 
12518181Seric 	/*
12528959Seric 	**  Do any final sanitation the address may require.
12538959Seric 	**	This will normally be used to turn internal forms
12548959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
12558959Seric 	**	may be used as a default to the above rules.
12568959Seric 	*/
12578959Seric 
12588959Seric 	rewrite(pvp, 4);
12598959Seric 
12608959Seric 	/*
12618181Seric 	**  Now restore the comment information we had at the beginning.
12628181Seric 	*/
12638181Seric 
12647682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
126556678Seric 	define('g', lbuf, e);
126656678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
126756678Seric 	define('g', oldg, e);
12687682Seric 
12697682Seric 	if (tTd(12, 1))
12707755Seric 		printf("remotename => `%s'\n", buf);
12717682Seric 	return (buf);
12727682Seric }
127351317Seric /*
127456678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
127551317Seric **
127651317Seric **	Parameters:
127756678Seric **		a -- the address to map (but just the user name part).
127856678Seric **		sendq -- the sendq in which to install any replacement
127956678Seric **			addresses.
128051317Seric **
128151317Seric **	Returns:
128251317Seric **		none.
128351317Seric */
128451317Seric 
128556678Seric maplocaluser(a, sendq, e)
128656678Seric 	register ADDRESS *a;
128756678Seric 	ADDRESS **sendq;
128856678Seric 	ENVELOPE *e;
128951317Seric {
129056678Seric 	register char **pvp;
129156678Seric 	register ADDRESS *a1 = NULL;
129256678Seric 	char pvpbuf[PSBUFSIZE];
129351317Seric 
129456678Seric 	if (tTd(29, 1))
129556678Seric 	{
129656678Seric 		printf("maplocaluser: ");
129756678Seric 		printaddr(a, FALSE);
129856678Seric 	}
129956678Seric 	pvp = prescan(a->q_user, '\0', pvpbuf);
130056678Seric 	if (pvp == NULL)
130156678Seric 		return;
130251317Seric 
130356678Seric 	rewrite(pvp, 5);
130456678Seric 	if (pvp[0] == NULL || pvp[0][0] != CANONNET)
130556678Seric 		return;
130651317Seric 
130756678Seric 	/* if non-null, mailer destination specified -- has it changed? */
130856678Seric 	a1 = buildaddr(pvp, NULL);
130956678Seric 	if (a1 == NULL || sameaddr(a, a1))
131056678Seric 		return;
131151317Seric 
131256678Seric 	/* mark old address as dead; insert new address */
131356678Seric 	a->q_flags |= QDONTSEND;
131456678Seric 	a1->q_alias = a;
131556678Seric 	allocaddr(a1, 1, NULL);
131656678Seric 	(void) recipient(a1, sendq, e);
131751317Seric }
1318