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*52637Seric static char sccsid[] = "@(#)parseaddr.c	5.17 (Berkeley) 02/21/92";
1133730Sbostic #endif /* not lint */
1222976Smiriam 
133312Seric # 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.
43297Seric **
44297Seric **	Returns:
45297Seric **		A pointer to the address descriptor header (`a' if
46297Seric **			`a' is non-NULL).
47297Seric **		NULL on error.
48297Seric **
49297Seric **	Side Effects:
50297Seric **		none
51297Seric */
52297Seric 
539374Seric /* following delimiters are inherent to the internal algorithms */
5416155Seric # define DELIMCHARS	"\001()<>,;\\\"\r\n"	/* word delimiters */
552091Seric 
562973Seric ADDRESS *
5711445Seric parseaddr(addr, a, copyf, delim)
58297Seric 	char *addr;
592973Seric 	register ADDRESS *a;
60297Seric 	int copyf;
6111445Seric 	char delim;
62297Seric {
633149Seric 	register char **pvp;
643149Seric 	register struct mailer *m;
6516914Seric 	char pvpbuf[PSBUFSIZE];
663149Seric 	extern char **prescan();
673149Seric 	extern ADDRESS *buildaddr();
68297Seric 
69297Seric 	/*
70297Seric 	**  Initialize and prescan address.
71297Seric 	*/
72297Seric 
736903Seric 	CurEnv->e_to = addr;
747675Seric 	if (tTd(20, 1))
759888Seric 		printf("\n--parseaddr(%s)\n", addr);
763188Seric 
7716914Seric 	pvp = prescan(addr, delim, pvpbuf);
783149Seric 	if (pvp == NULL)
79297Seric 		return (NULL);
80297Seric 
81297Seric 	/*
823149Seric 	**  Apply rewriting rules.
837889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
84297Seric 	*/
85297Seric 
868181Seric 	rewrite(pvp, 3);
874070Seric 	rewrite(pvp, 0);
88297Seric 
893149Seric 	/*
903149Seric 	**  See if we resolved to a real mailer.
913149Seric 	*/
92297Seric 
933149Seric 	if (pvp[0][0] != CANONNET)
943149Seric 	{
953149Seric 		setstat(EX_USAGE);
963149Seric 		usrerr("cannot resolve name");
973149Seric 		return (NULL);
98297Seric 	}
99297Seric 
100297Seric 	/*
1013149Seric 	**  Build canonical address from pvp.
102297Seric 	*/
103297Seric 
1043149Seric 	a = buildaddr(pvp, a);
1054279Seric 	if (a == NULL)
1064279Seric 		return (NULL);
107297Seric 
108297Seric 	/*
1093149Seric 	**  Make local copies of the host & user and then
1103149Seric 	**  transport them out.
111297Seric 	*/
112297Seric 
11351317Seric 	allocaddr(a, copyf, addr);
11451317Seric 
11551317Seric 	/*
11651317Seric 	**  Compute return value.
11751317Seric 	*/
11851317Seric 
11951317Seric 	if (tTd(20, 1))
1208078Seric 	{
12151317Seric 		printf("parseaddr-->");
12251317Seric 		printaddr(a, FALSE);
12351317Seric 	}
12451317Seric 
12551317Seric 	return (a);
12651317Seric }
12751317Seric /*
12851317Seric **  ALLOCADDR -- do local allocations of address on demand.
12951317Seric **
13051317Seric **	Also lowercases the host name if requested.
13151317Seric **
13251317Seric **	Parameters:
13351317Seric **		a -- the address to reallocate.
13451317Seric **		copyf -- the copy flag (see parseaddr for description).
13551317Seric **		paddr -- the printname of the address.
13651317Seric **
13751317Seric **	Returns:
13851317Seric **		none.
13951317Seric **
14051317Seric **	Side Effects:
14151317Seric **		Copies portions of a into local buffers as requested.
14251317Seric */
14351317Seric 
14451317Seric allocaddr(a, copyf, paddr)
14551317Seric 	register ADDRESS *a;
14651317Seric 	int copyf;
14751317Seric 	char *paddr;
14851317Seric {
14951317Seric 	register MAILER *m = a->q_mailer;
15051317Seric 
15151317Seric 	if (copyf > 0 && paddr != NULL)
15251317Seric 	{
1538078Seric 		extern char *DelimChar;
1548078Seric 		char savec = *DelimChar;
1558078Seric 
1568078Seric 		*DelimChar = '\0';
15751317Seric 		a->q_paddr = newstr(paddr);
1588078Seric 		*DelimChar = savec;
1598078Seric 	}
160297Seric 	else
16151317Seric 		a->q_paddr = paddr;
16224944Seric 
16324944Seric 	if (a->q_user == NULL)
16424944Seric 		a->q_user = "";
16524944Seric 	if (a->q_host == NULL)
16624944Seric 		a->q_host = "";
16724944Seric 
1683149Seric 	if (copyf >= 0)
169297Seric 	{
17024944Seric 		a->q_host = newstr(a->q_host);
1713149Seric 		if (a->q_user != a->q_paddr)
1723149Seric 			a->q_user = newstr(a->q_user);
173297Seric 	}
174297Seric 
17551317Seric 	if (a->q_paddr == NULL)
17651317Seric 		a->q_paddr = a->q_user;
17751317Seric 
178297Seric 	/*
17916202Seric 	**  Convert host name to lower case if requested.
18016202Seric 	**	User name will be done later.
18116202Seric 	*/
18216202Seric 
18316202Seric 	if (!bitnset(M_HST_UPPER, m->m_flags))
18416202Seric 		makelower(a->q_host);
185297Seric }
186297Seric /*
18716162Seric **  LOWERADDR -- map UPPER->lower case on addresses as requested.
18816162Seric **
18916162Seric **	Parameters:
19016162Seric **		a -- address to be mapped.
19116162Seric **
19216162Seric **	Returns:
19316162Seric **		none.
19416162Seric **
19516162Seric **	Side Effects:
19616162Seric **		none.
19716162Seric */
19816162Seric 
19916162Seric loweraddr(a)
20016162Seric 	register ADDRESS *a;
20116162Seric {
20216162Seric 	register MAILER *m = a->q_mailer;
20316162Seric 
20416162Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
20516162Seric 		makelower(a->q_user);
20616162Seric }
20716162Seric /*
208297Seric **  PRESCAN -- Prescan name and make it canonical
209297Seric **
2109374Seric **	Scans a name and turns it into a set of tokens.  This process
2119374Seric **	deletes blanks and comments (in parentheses).
212297Seric **
213297Seric **	This routine knows about quoted strings and angle brackets.
214297Seric **
215297Seric **	There are certain subtleties to this routine.  The one that
216297Seric **	comes to mind now is that backslashes on the ends of names
217297Seric **	are silently stripped off; this is intentional.  The problem
218297Seric **	is that some versions of sndmsg (like at LBL) set the kill
219297Seric **	character to something other than @ when reading addresses;
220297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
221297Seric **	berknet mailer.
222297Seric **
223297Seric **	Parameters:
224297Seric **		addr -- the name to chomp.
225297Seric **		delim -- the delimiter for the address, normally
226297Seric **			'\0' or ','; \0 is accepted in any case.
22715284Seric **			If '\t' then we are reading the .cf file.
22816914Seric **		pvpbuf -- place to put the saved text -- note that
22916914Seric **			the pointers are static.
230297Seric **
231297Seric **	Returns:
2323149Seric **		A pointer to a vector of tokens.
233297Seric **		NULL on error.
234297Seric **
235297Seric **	Side Effects:
23625279Seric **		sets DelimChar to point to the character matching 'delim'.
237297Seric */
238297Seric 
2398078Seric /* states and character types */
2408078Seric # define OPR		0	/* operator */
2418078Seric # define ATM		1	/* atom */
2428078Seric # define QST		2	/* in quoted string */
2438078Seric # define SPC		3	/* chewing up spaces */
2448078Seric # define ONE		4	/* pick up one character */
2453149Seric 
2468078Seric # define NSTATES	5	/* number of states */
2478078Seric # define TYPE		017	/* mask to select state type */
2488078Seric 
2498078Seric /* meta bits for table */
2508078Seric # define M		020	/* meta character; don't pass through */
2518078Seric # define B		040	/* cause a break */
2528078Seric # define MB		M|B	/* meta-break */
2538078Seric 
2548078Seric static short StateTab[NSTATES][NSTATES] =
2558078Seric {
2568087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2579051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2589051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2599051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2608078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2618078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2628078Seric };
2638078Seric 
2648078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2658078Seric 
2668078Seric char	*DelimChar;		/* set to point to the delimiter */
2678078Seric 
2683149Seric char **
26916914Seric prescan(addr, delim, pvpbuf)
270297Seric 	char *addr;
271297Seric 	char delim;
27216914Seric 	char pvpbuf[];
273297Seric {
274297Seric 	register char *p;
2758078Seric 	register char *q;
2769346Seric 	register int c;
2773149Seric 	char **avp;
278297Seric 	bool bslashmode;
279297Seric 	int cmntcnt;
2808423Seric 	int anglecnt;
2813149Seric 	char *tok;
2828078Seric 	int state;
2838078Seric 	int newstate;
2848078Seric 	static char *av[MAXATOM+1];
28515253Seric 	extern int errno;
286297Seric 
28715253Seric 	/* make sure error messages don't have garbage on them */
28815253Seric 	errno = 0;
28915253Seric 
29016914Seric 	q = pvpbuf;
2913149Seric 	bslashmode = FALSE;
2927800Seric 	cmntcnt = 0;
2938423Seric 	anglecnt = 0;
2943149Seric 	avp = av;
2958078Seric 	state = OPR;
2968078Seric 	c = NOCHAR;
2978078Seric 	p = addr;
2988078Seric 	if (tTd(22, 45))
299297Seric 	{
3008078Seric 		printf("prescan: ");
3018078Seric 		xputs(p);
30223109Seric 		(void) putchar('\n');
3038078Seric 	}
3048078Seric 
3058078Seric 	do
3068078Seric 	{
3073149Seric 		/* read a token */
3083149Seric 		tok = q;
3098078Seric 		for (;;)
310297Seric 		{
3118078Seric 			/* store away any old lookahead character */
3128078Seric 			if (c != NOCHAR)
3138078Seric 			{
31415284Seric 				/* see if there is room */
31516914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3168078Seric 				{
3178078Seric 					usrerr("Address too long");
3188078Seric 					DelimChar = p;
3198078Seric 					return (NULL);
3208078Seric 				}
32115284Seric 
32215284Seric 				/* squirrel it away */
3238078Seric 				*q++ = c;
3248078Seric 			}
3258078Seric 
3268078Seric 			/* read a new input character */
3278078Seric 			c = *p++;
3288078Seric 			if (c == '\0')
3298078Seric 				break;
33015284Seric 			c &= ~0200;
33115284Seric 
3328078Seric 			if (tTd(22, 101))
3338078Seric 				printf("c=%c, s=%d; ", c, state);
3348078Seric 
3353149Seric 			/* chew up special characters */
3363149Seric 			*q = '\0';
3373149Seric 			if (bslashmode)
3383149Seric 			{
33924944Seric 				/* kludge \! for naive users */
34024944Seric 				if (c != '!')
34124944Seric 					c |= 0200;
3423149Seric 				bslashmode = FALSE;
3433149Seric 			}
34451381Seric 
34551381Seric 			if (c == '\\')
3463149Seric 			{
3473149Seric 				bslashmode = TRUE;
3488078Seric 				c = NOCHAR;
3493149Seric 			}
3508514Seric 			else if (state == QST)
3518514Seric 			{
3528514Seric 				/* do nothing, just avoid next clauses */
3538514Seric 			}
3548078Seric 			else if (c == '(')
3554100Seric 			{
3568078Seric 				cmntcnt++;
3578078Seric 				c = NOCHAR;
3584100Seric 			}
3598078Seric 			else if (c == ')')
3603149Seric 			{
3618078Seric 				if (cmntcnt <= 0)
3623149Seric 				{
3638078Seric 					usrerr("Unbalanced ')'");
3648078Seric 					DelimChar = p;
3658078Seric 					return (NULL);
3663149Seric 				}
3678078Seric 				else
3688078Seric 					cmntcnt--;
3698078Seric 			}
3708078Seric 			else if (cmntcnt > 0)
3718078Seric 				c = NOCHAR;
3728423Seric 			else if (c == '<')
3738423Seric 				anglecnt++;
3748423Seric 			else if (c == '>')
3758423Seric 			{
3768423Seric 				if (anglecnt <= 0)
3778423Seric 				{
3788423Seric 					usrerr("Unbalanced '>'");
3798423Seric 					DelimChar = p;
3808423Seric 					return (NULL);
3818423Seric 				}
3828423Seric 				anglecnt--;
3838423Seric 			}
38411423Seric 			else if (delim == ' ' && isspace(c))
38511423Seric 				c = ' ';
3863149Seric 
3878078Seric 			if (c == NOCHAR)
3888078Seric 				continue;
3893149Seric 
3908078Seric 			/* see if this is end of input */
39111405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
3923149Seric 				break;
3933149Seric 
3948078Seric 			newstate = StateTab[state][toktype(c)];
3958078Seric 			if (tTd(22, 101))
3968078Seric 				printf("ns=%02o\n", newstate);
3978078Seric 			state = newstate & TYPE;
3988078Seric 			if (bitset(M, newstate))
3998078Seric 				c = NOCHAR;
4008078Seric 			if (bitset(B, newstate))
4014228Seric 				break;
402297Seric 		}
4033149Seric 
4043149Seric 		/* new token */
4058078Seric 		if (tok != q)
4061378Seric 		{
4078078Seric 			*q++ = '\0';
4088078Seric 			if (tTd(22, 36))
409297Seric 			{
4108078Seric 				printf("tok=");
4118078Seric 				xputs(tok);
41223109Seric 				(void) putchar('\n');
413297Seric 			}
4148078Seric 			if (avp >= &av[MAXATOM])
415297Seric 			{
4168078Seric 				syserr("prescan: too many tokens");
4178078Seric 				DelimChar = p;
4188078Seric 				return (NULL);
419297Seric 			}
4208078Seric 			*avp++ = tok;
421297Seric 		}
4228423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4233149Seric 	*avp = NULL;
4248078Seric 	DelimChar = --p;
4253149Seric 	if (cmntcnt > 0)
4263149Seric 		usrerr("Unbalanced '('");
4278423Seric 	else if (anglecnt > 0)
4288423Seric 		usrerr("Unbalanced '<'");
4298078Seric 	else if (state == QST)
4303149Seric 		usrerr("Unbalanced '\"'");
4313149Seric 	else if (av[0] != NULL)
4323149Seric 		return (av);
4333149Seric 	return (NULL);
4343149Seric }
4353149Seric /*
4363149Seric **  TOKTYPE -- return token type
4373149Seric **
4383149Seric **	Parameters:
4393149Seric **		c -- the character in question.
4403149Seric **
4413149Seric **	Returns:
4423149Seric **		Its type.
4433149Seric **
4443149Seric **	Side Effects:
4453149Seric **		none.
4463149Seric */
447297Seric 
4483149Seric toktype(c)
4493149Seric 	register char c;
4503149Seric {
4513380Seric 	static char buf[50];
4523382Seric 	static bool firstime = TRUE;
4533380Seric 
4543382Seric 	if (firstime)
4553380Seric 	{
4563382Seric 		firstime = FALSE;
45716155Seric 		expand("\001o", buf, &buf[sizeof buf - 1], CurEnv);
4587005Seric 		(void) strcat(buf, DELIMCHARS);
4593380Seric 	}
4609585Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
4618078Seric 		return (ONE);
4628078Seric 	if (c == '"')
4638078Seric 		return (QST);
4644100Seric 	if (!isascii(c))
4658078Seric 		return (ATM);
4668078Seric 	if (isspace(c) || c == ')')
4678078Seric 		return (SPC);
4683380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
4698078Seric 		return (OPR);
4708078Seric 	return (ATM);
4713149Seric }
4723149Seric /*
4733149Seric **  REWRITE -- apply rewrite rules to token vector.
4743149Seric **
4754476Seric **	This routine is an ordered production system.  Each rewrite
4764476Seric **	rule has a LHS (called the pattern) and a RHS (called the
4774476Seric **	rewrite); 'rwr' points the the current rewrite rule.
4784476Seric **
4794476Seric **	For each rewrite rule, 'avp' points the address vector we
4804476Seric **	are trying to match against, and 'pvp' points to the pattern.
4818058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
4829585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
4839585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
4844476Seric **
4854476Seric **	When a match between avp & pvp does not match, we try to
4869585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
4874476Seric **	we must also back out the match in mvp.  If we reach a
4888058Seric **	MATCHANY or MATCHZANY we just extend the match and start
4898058Seric **	over again.
4904476Seric **
4914476Seric **	When we finally match, we rewrite the address vector
4924476Seric **	and try over again.
4934476Seric **
4943149Seric **	Parameters:
4953149Seric **		pvp -- pointer to token vector.
4963149Seric **
4973149Seric **	Returns:
4983149Seric **		none.
4993149Seric **
5003149Seric **	Side Effects:
5013149Seric **		pvp is modified.
5023149Seric */
5032091Seric 
5043149Seric struct match
5053149Seric {
5064468Seric 	char	**first;	/* first token matched */
5074468Seric 	char	**last;		/* last token matched */
5083149Seric };
5093149Seric 
5104468Seric # define MAXMATCH	9	/* max params per rewrite */
5113149Seric 
5123149Seric 
5134070Seric rewrite(pvp, ruleset)
5143149Seric 	char **pvp;
5154070Seric 	int ruleset;
5163149Seric {
5173149Seric 	register char *ap;		/* address pointer */
5183149Seric 	register char *rp;		/* rewrite pointer */
5193149Seric 	register char **avp;		/* address vector pointer */
5203149Seric 	register char **rvp;		/* rewrite vector pointer */
5218058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5228058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
5234468Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5243149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5253149Seric 
5269279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5273149Seric 	{
5288959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
5293149Seric 		printav(pvp);
5303149Seric 	}
5318423Seric 	if (pvp == NULL)
5328423Seric 		return;
5333149Seric 
5343149Seric 	/*
5353149Seric 	**  Run through the list of rewrite rules, applying
5363149Seric 	**	any that match.
5373149Seric 	*/
5383149Seric 
5394070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
5403149Seric 	{
541*52637Seric 		int loopcount = 0;
542*52637Seric 
5437675Seric 		if (tTd(21, 12))
544297Seric 		{
5458069Seric 			printf("-----trying rule:");
5463149Seric 			printav(rwr->r_lhs);
5473149Seric 		}
5483149Seric 
5493149Seric 		/* try to match on this rule */
5504468Seric 		mlp = mlist;
5518058Seric 		rvp = rwr->r_lhs;
5528058Seric 		avp = pvp;
5538058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
5543149Seric 		{
555*52637Seric 			if (++loopcount > 100)
556*52637Seric 			{
557*52637Seric 				syserr("Infinite loop in ruleset %d", ruleset);
558*52637Seric 				break;
559*52637Seric 			}
5603149Seric 			rp = *rvp;
5618058Seric 			if (tTd(21, 35))
5628058Seric 			{
563*52637Seric 				printf("operator=");
5648058Seric 				xputs(ap);
565*52637Seric 				printf(", token=");
5668058Seric 				xputs(rp);
5678069Seric 				printf("\n");
5688058Seric 			}
5693149Seric 			if (rp == NULL)
570297Seric 			{
5713149Seric 				/* end-of-pattern before end-of-address */
5728058Seric 				goto backup;
573297Seric 			}
5748058Seric 			if (ap == NULL && *rp != MATCHZANY)
5758058Seric 			{
5768058Seric 				/* end-of-input */
5778058Seric 				break;
5788058Seric 			}
5793149Seric 
5803149Seric 			switch (*rp)
5813149Seric 			{
5824060Seric 				register STAB *s;
5834060Seric 
5844060Seric 			  case MATCHCLASS:
5859585Seric 			  case MATCHNCLASS:
5869585Seric 				/* match any token in (not in) a class */
5874100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
58810690Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
5899585Seric 				{
5909585Seric 					if (*rp == MATCHCLASS)
5919585Seric 						goto backup;
5929585Seric 				}
5939585Seric 				else if (*rp == MATCHNCLASS)
5948058Seric 					goto backup;
5954468Seric 
5964476Seric 				/* explicit fall-through */
5974476Seric 
5984476Seric 			  case MATCHONE:
5994476Seric 			  case MATCHANY:
6004476Seric 				/* match exactly one token */
6018058Seric 				mlp->first = avp;
6028058Seric 				mlp->last = avp++;
6034468Seric 				mlp++;
6044060Seric 				break;
6054060Seric 
6068058Seric 			  case MATCHZANY:
6078058Seric 				/* match zero or more tokens */
6088058Seric 				mlp->first = avp;
6098058Seric 				mlp->last = avp - 1;
6108058Seric 				mlp++;
6118058Seric 				break;
6128058Seric 
6133149Seric 			  default:
6143149Seric 				/* must have exact match */
61533725Sbostic 				if (strcasecmp(rp, ap))
6168058Seric 					goto backup;
6174468Seric 				avp++;
6183149Seric 				break;
6193149Seric 			}
6203149Seric 
6213149Seric 			/* successful match on this token */
6223149Seric 			rvp++;
6233149Seric 			continue;
6243149Seric 
6258058Seric 		  backup:
6263149Seric 			/* match failed -- back up */
6273149Seric 			while (--rvp >= rwr->r_lhs)
6283149Seric 			{
6293149Seric 				rp = *rvp;
6308058Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
6314468Seric 				{
6324476Seric 					/* extend binding and continue */
6338058Seric 					avp = ++mlp[-1].last;
6348058Seric 					avp++;
6354476Seric 					rvp++;
6363149Seric 					break;
6374468Seric 				}
6384476Seric 				avp--;
6399585Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS ||
6409585Seric 				    *rp == MATCHNCLASS)
6413149Seric 				{
6424468Seric 					/* back out binding */
6434468Seric 					mlp--;
6443149Seric 				}
6453149Seric 			}
6463149Seric 
6473149Seric 			if (rvp < rwr->r_lhs)
6483149Seric 			{
6493149Seric 				/* total failure to match */
6503149Seric 				break;
6513149Seric 			}
652297Seric 		}
6533149Seric 
6543149Seric 		/*
6553149Seric 		**  See if we successfully matched
6563149Seric 		*/
6573149Seric 
6589374Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
6593149Seric 		{
6609374Seric 			if (tTd(21, 10))
6619374Seric 				printf("----- rule fails\n");
6629374Seric 			rwr = rwr->r_next;
6639374Seric 			continue;
6649374Seric 		}
6653149Seric 
6669374Seric 		rvp = rwr->r_rhs;
6679374Seric 		if (tTd(21, 12))
6689374Seric 		{
6699374Seric 			printf("-----rule matches:");
6709374Seric 			printav(rvp);
6719374Seric 		}
6729374Seric 
6739374Seric 		rp = *rvp;
6749374Seric 		if (*rp == CANONUSER)
6759374Seric 		{
6769374Seric 			rvp++;
6779374Seric 			rwr = rwr->r_next;
6789374Seric 		}
6799374Seric 		else if (*rp == CANONHOST)
6809374Seric 		{
6819374Seric 			rvp++;
6829374Seric 			rwr = NULL;
6839374Seric 		}
6849374Seric 		else if (*rp == CANONNET)
6859374Seric 			rwr = NULL;
6869374Seric 
6879374Seric 		/* substitute */
6889374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
6899374Seric 		{
6909374Seric 			register struct match *m;
6919374Seric 			register char **pp;
6929374Seric 
6938058Seric 			rp = *rvp;
69416914Seric 			if (*rp == MATCHREPL)
6958058Seric 			{
69616914Seric 				/* substitute from LHS */
69716914Seric 				m = &mlist[rp[1] - '1'];
69816914Seric 				if (m >= mlp)
6999374Seric 				{
70016914Seric 					syserr("rewrite: ruleset %d: replacement out of bounds", ruleset);
7019374Seric 					return;
7029374Seric 				}
70316914Seric 				if (tTd(21, 15))
70416914Seric 				{
70516914Seric 					printf("$%c:", rp[1]);
70616914Seric 					pp = m->first;
70716914Seric 					while (pp <= m->last)
70816914Seric 					{
70916914Seric 						printf(" %x=\"", *pp);
71016914Seric 						(void) fflush(stdout);
71116914Seric 						printf("%s\"", *pp++);
71216914Seric 					}
71316914Seric 					printf("\n");
71416914Seric 				}
7159374Seric 				pp = m->first;
7169374Seric 				while (pp <= m->last)
7173149Seric 				{
71816914Seric 					if (avp >= &npvp[MAXATOM])
71916914Seric 					{
72016914Seric 						syserr("rewrite: expansion too long");
72116914Seric 						return;
72216914Seric 					}
72316914Seric 					*avp++ = *pp++;
7243149Seric 				}
7253149Seric 			}
72616914Seric 			else
7278226Seric 			{
72816914Seric 				/* vanilla replacement */
7299374Seric 				if (avp >= &npvp[MAXATOM])
73016889Seric 				{
73116914Seric 	toolong:
73216889Seric 					syserr("rewrite: expansion too long");
73316889Seric 					return;
73416889Seric 				}
73516914Seric 				*avp++ = rp;
7368226Seric 			}
7379374Seric 		}
7389374Seric 		*avp++ = NULL;
73916914Seric 
74016914Seric 		/*
74116914Seric 		**  Check for any hostname lookups.
74216914Seric 		*/
74316914Seric 
74416914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
74516914Seric 		{
74616914Seric 			char **hbrvp;
74716914Seric 			char **xpvp;
74816914Seric 			int trsize;
74917473Seric 			char *olddelimchar;
75016920Seric 			char buf[MAXNAME + 1];
75116914Seric 			char *pvpb1[MAXATOM + 1];
75217174Seric 			char pvpbuf[PSBUFSIZE];
75317473Seric 			extern char *DelimChar;
75416914Seric 
75516914Seric 			if (**rvp != HOSTBEGIN)
75616914Seric 				continue;
75716914Seric 
75816914Seric 			/*
75916914Seric 			**  Got a hostname lookup.
76016914Seric 			**
76116914Seric 			**	This could be optimized fairly easily.
76216914Seric 			*/
76316914Seric 
76416914Seric 			hbrvp = rvp;
76516914Seric 
76616914Seric 			/* extract the match part */
76716914Seric 			while (*++rvp != NULL && **rvp != HOSTEND)
76816914Seric 				continue;
76916914Seric 			if (*rvp != NULL)
77016914Seric 				*rvp++ = NULL;
77116914Seric 
77216914Seric 			/* save the remainder of the input string */
77316914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
77416914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
77516914Seric 
77616914Seric 			/* look it up */
77716914Seric 			cataddr(++hbrvp, buf, sizeof buf);
77851317Seric 			if (maphostname(buf, sizeof buf - 1) && ConfigLevel >= 2)
77951317Seric 			{
78051317Seric 				register int i;
78116914Seric 
78251317Seric 				/* it mapped -- mark it with a trailing dot */
78351317Seric 				i = strlen(buf);
78451317Seric 				if (i > 0 && buf[i - 1] != '.')
78551317Seric 				{
78651317Seric 					buf[i++] = '.';
78751317Seric 					buf[i] = '\0';
78851317Seric 				}
78951317Seric 			}
79051317Seric 
79116914Seric 			/* scan the new host name */
79217473Seric 			olddelimchar = DelimChar;
79316914Seric 			xpvp = prescan(buf, '\0', pvpbuf);
79417473Seric 			DelimChar = olddelimchar;
79516914Seric 			if (xpvp == NULL)
79616914Seric 			{
79716914Seric 				syserr("rewrite: cannot prescan canonical hostname: %s", buf);
79822976Smiriam 				return;
79916914Seric 			}
80016914Seric 
80116914Seric 			/* append it to the token list */
80217174Seric 			for (avp = --hbrvp; *xpvp != NULL; xpvp++)
80317174Seric 			{
80417174Seric 				*avp++ = newstr(*xpvp);
80516920Seric 				if (avp >= &npvp[MAXATOM])
80616914Seric 					goto toolong;
80717174Seric 			}
80816914Seric 
80916914Seric 			/* restore the old trailing information */
81017177Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
81116920Seric 				if (avp >= &npvp[MAXATOM])
81216914Seric 					goto toolong;
81317174Seric 
81417174Seric 			break;
81516914Seric 		}
81616914Seric 
81716914Seric 		/*
81816914Seric 		**  Check for subroutine calls.
81916914Seric 		*/
82016914Seric 
82124944Seric 		if (*npvp != NULL && **npvp == CALLSUBR)
8229374Seric 		{
82316889Seric 			bcopy((char *) &npvp[2], (char *) pvp,
82416900Seric 				(int) (avp - npvp - 2) * sizeof *avp);
82516889Seric 			if (tTd(21, 3))
82616889Seric 				printf("-----callsubr %s\n", npvp[1]);
82716889Seric 			rewrite(pvp, atoi(npvp[1]));
8283149Seric 		}
8293149Seric 		else
8303149Seric 		{
83117348Seric 			bcopy((char *) npvp, (char *) pvp,
83216900Seric 				(int) (avp - npvp) * sizeof *avp);
8339374Seric 		}
8349374Seric 		if (tTd(21, 4))
8359374Seric 		{
8369374Seric 			printf("rewritten as:");
8379374Seric 			printav(pvp);
8389374Seric 		}
839297Seric 	}
8408069Seric 
8419279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
8428069Seric 	{
8438959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
8448069Seric 		printav(pvp);
8458069Seric 	}
8463149Seric }
8473149Seric /*
8483149Seric **  BUILDADDR -- build address from token vector.
8493149Seric **
8503149Seric **	Parameters:
8513149Seric **		tv -- token vector.
8523149Seric **		a -- pointer to address descriptor to fill.
8533149Seric **			If NULL, one will be allocated.
8543149Seric **
8553149Seric **	Returns:
8564279Seric **		NULL if there was an error.
8574279Seric **		'a' otherwise.
8583149Seric **
8593149Seric **	Side Effects:
8603149Seric **		fills in 'a'
8613149Seric */
8623149Seric 
8633149Seric ADDRESS *
8643149Seric buildaddr(tv, a)
8653149Seric 	register char **tv;
8663149Seric 	register ADDRESS *a;
8673149Seric {
8683149Seric 	static char buf[MAXNAME];
8693149Seric 	struct mailer **mp;
8703149Seric 	register struct mailer *m;
8713149Seric 
8723149Seric 	if (a == NULL)
8733149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
87416889Seric 	bzero((char *) a, sizeof *a);
8753149Seric 
8763149Seric 	/* figure out what net/mailer to use */
8773149Seric 	if (**tv != CANONNET)
8784279Seric 	{
8793149Seric 		syserr("buildaddr: no net");
8804279Seric 		return (NULL);
8814279Seric 	}
8823149Seric 	tv++;
88333725Sbostic 	if (!strcasecmp(*tv, "error"))
8844279Seric 	{
88510183Seric 		if (**++tv == CANONHOST)
88610183Seric 		{
88710183Seric 			setstat(atoi(*++tv));
88810183Seric 			tv++;
88910183Seric 		}
89010183Seric 		if (**tv != CANONUSER)
8914279Seric 			syserr("buildaddr: error: no user");
8924279Seric 		buf[0] = '\0';
8934279Seric 		while (*++tv != NULL)
8944279Seric 		{
8954279Seric 			if (buf[0] != '\0')
8967005Seric 				(void) strcat(buf, " ");
8977005Seric 			(void) strcat(buf, *tv);
8984279Seric 		}
8994279Seric 		usrerr(buf);
9004279Seric 		return (NULL);
9014279Seric 	}
9024598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
9033149Seric 	{
90433725Sbostic 		if (!strcasecmp(m->m_name, *tv))
9053149Seric 			break;
9063149Seric 	}
9073149Seric 	if (m == NULL)
9084279Seric 	{
90924944Seric 		syserr("buildaddr: unknown mailer %s", *tv);
9104279Seric 		return (NULL);
9114279Seric 	}
9124598Seric 	a->q_mailer = m;
9133149Seric 
9143149Seric 	/* figure out what host (if any) */
9153149Seric 	tv++;
91610690Seric 	if (!bitnset(M_LOCAL, m->m_flags))
9173149Seric 	{
9185704Seric 		if (**tv++ != CANONHOST)
9194279Seric 		{
9203149Seric 			syserr("buildaddr: no host");
9214279Seric 			return (NULL);
9224279Seric 		}
9235704Seric 		buf[0] = '\0';
9245704Seric 		while (*tv != NULL && **tv != CANONUSER)
9257005Seric 			(void) strcat(buf, *tv++);
9265704Seric 		a->q_host = newstr(buf);
9273149Seric 	}
9283149Seric 	else
9293149Seric 		a->q_host = NULL;
9303149Seric 
9313149Seric 	/* figure out the user */
93236615Sbostic 	if (*tv == NULL || **tv != CANONUSER)
9334279Seric 	{
9343149Seric 		syserr("buildaddr: no user");
9354279Seric 		return (NULL);
9364279Seric 	}
93719040Seric 
93851719Seric 	if (m == LocalMailer && tv[1] != NULL && strcmp(tv[1], "@") == 0)
93951317Seric 	{
94051317Seric 		tv++;
94151317Seric 		a->q_flags |= QNOTREMOTE;
94251317Seric 	}
94351317Seric 
94419040Seric 	/* rewrite according recipient mailer rewriting rules */
94519040Seric 	rewrite(++tv, 2);
94619040Seric 	if (m->m_r_rwset > 0)
94719040Seric 		rewrite(tv, m->m_r_rwset);
94819040Seric 	rewrite(tv, 4);
94919040Seric 
95019040Seric 	/* save the result for the command line/RCPT argument */
95111278Seric 	cataddr(tv, buf, sizeof buf);
9523149Seric 	a->q_user = buf;
9533149Seric 
9543149Seric 	return (a);
9553149Seric }
9563188Seric /*
9574228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
9584228Seric **
9594228Seric **	Parameters:
9604228Seric **		pvp -- parameter vector to rebuild.
9614228Seric **		buf -- buffer to build the string into.
9624228Seric **		sz -- size of buf.
9634228Seric **
9644228Seric **	Returns:
9654228Seric **		none.
9664228Seric **
9674228Seric **	Side Effects:
9684228Seric **		Destroys buf.
9694228Seric */
9704228Seric 
9714228Seric cataddr(pvp, buf, sz)
9724228Seric 	char **pvp;
9734228Seric 	char *buf;
9744228Seric 	register int sz;
9754228Seric {
9764228Seric 	bool oatomtok = FALSE;
9774228Seric 	bool natomtok = FALSE;
9784228Seric 	register int i;
9794228Seric 	register char *p;
9804228Seric 
9818423Seric 	if (pvp == NULL)
9828423Seric 	{
98323109Seric 		(void) strcpy(buf, "");
9848423Seric 		return;
9858423Seric 	}
9864228Seric 	p = buf;
98711156Seric 	sz -= 2;
9884228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
9894228Seric 	{
9908078Seric 		natomtok = (toktype(**pvp) == ATM);
9914228Seric 		if (oatomtok && natomtok)
9929042Seric 			*p++ = SpaceSub;
9934228Seric 		(void) strcpy(p, *pvp);
9944228Seric 		oatomtok = natomtok;
9954228Seric 		p += i;
99611156Seric 		sz -= i + 1;
9974228Seric 		pvp++;
9984228Seric 	}
9994228Seric 	*p = '\0';
10004228Seric }
10014228Seric /*
10023188Seric **  SAMEADDR -- Determine if two addresses are the same
10033188Seric **
10043188Seric **	This is not just a straight comparison -- if the mailer doesn't
10053188Seric **	care about the host we just ignore it, etc.
10063188Seric **
10073188Seric **	Parameters:
10083188Seric **		a, b -- pointers to the internal forms to compare.
10093188Seric **
10103188Seric **	Returns:
10113188Seric **		TRUE -- they represent the same mailbox.
10123188Seric **		FALSE -- they don't.
10133188Seric **
10143188Seric **	Side Effects:
10153188Seric **		none.
10163188Seric */
10173188Seric 
10183188Seric bool
10199374Seric sameaddr(a, b)
10203188Seric 	register ADDRESS *a;
10213188Seric 	register ADDRESS *b;
10223188Seric {
10233188Seric 	/* if they don't have the same mailer, forget it */
10243188Seric 	if (a->q_mailer != b->q_mailer)
10253188Seric 		return (FALSE);
10263188Seric 
10273188Seric 	/* if the user isn't the same, we can drop out */
10289374Seric 	if (strcmp(a->q_user, b->q_user) != 0)
10293188Seric 		return (FALSE);
10303188Seric 
10313188Seric 	/* if the mailer ignores hosts, we have succeeded! */
103210690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
10333188Seric 		return (TRUE);
10343188Seric 
10353188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
10363188Seric 	if (a->q_host == NULL || b->q_host == NULL)
10373188Seric 		return (FALSE);
10383188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
10393188Seric 		return (FALSE);
10403188Seric 
10413188Seric 	return (TRUE);
10423188Seric }
10433234Seric /*
10443234Seric **  PRINTADDR -- print address (for debugging)
10453234Seric **
10463234Seric **	Parameters:
10473234Seric **		a -- the address to print
10483234Seric **		follow -- follow the q_next chain.
10493234Seric **
10503234Seric **	Returns:
10513234Seric **		none.
10523234Seric **
10533234Seric **	Side Effects:
10543234Seric **		none.
10553234Seric */
10563234Seric 
10573234Seric printaddr(a, follow)
10583234Seric 	register ADDRESS *a;
10593234Seric 	bool follow;
10603234Seric {
10615001Seric 	bool first = TRUE;
10625001Seric 
10633234Seric 	while (a != NULL)
10643234Seric 	{
10655001Seric 		first = FALSE;
10664443Seric 		printf("%x=", a);
10674085Seric 		(void) fflush(stdout);
106840973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
106940973Sbostic 		       a->q_paddr, a->q_mailer->m_mno, a->q_mailer->m_name,
107040973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
10718181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
10728181Seric 		       a->q_alias);
10738181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
10748181Seric 		       a->q_fullname);
10754996Seric 
10763234Seric 		if (!follow)
10773234Seric 			return;
10784996Seric 		a = a->q_next;
10793234Seric 	}
10805001Seric 	if (first)
10814443Seric 		printf("[NULL]\n");
10823234Seric }
10834317Seric 
10847682Seric /*
10857682Seric **  REMOTENAME -- return the name relative to the current mailer
10867682Seric **
10877682Seric **	Parameters:
10887682Seric **		name -- the name to translate.
10898069Seric **		m -- the mailer that we want to do rewriting relative
10908069Seric **			to.
10918069Seric **		senderaddress -- if set, uses the sender rewriting rules
10928069Seric **			rather than the recipient rewriting rules.
109310310Seric **		canonical -- if set, strip out any comment information,
109410310Seric **			etc.
10957682Seric **
10967682Seric **	Returns:
10977682Seric **		the text string representing this address relative to
10987682Seric **			the receiving mailer.
10997682Seric **
11007682Seric **	Side Effects:
11017682Seric **		none.
11027682Seric **
11037682Seric **	Warnings:
11047682Seric **		The text string returned is tucked away locally;
11057682Seric **			copy it if you intend to save it.
11067682Seric */
11077682Seric 
11087682Seric char *
110910310Seric remotename(name, m, senderaddress, canonical)
11107682Seric 	char *name;
11117682Seric 	struct mailer *m;
11128069Seric 	bool senderaddress;
111310310Seric 	bool canonical;
11147682Seric {
11158069Seric 	register char **pvp;
11168069Seric 	char *fancy;
11178069Seric 	extern char *macvalue();
11188181Seric 	char *oldg = macvalue('g', CurEnv);
11197682Seric 	static char buf[MAXNAME];
11207682Seric 	char lbuf[MAXNAME];
112116914Seric 	char pvpbuf[PSBUFSIZE];
11227682Seric 	extern char **prescan();
11237889Seric 	extern char *crackaddr();
11247682Seric 
11257755Seric 	if (tTd(12, 1))
11267755Seric 		printf("remotename(%s)\n", name);
11277755Seric 
112810177Seric 	/* don't do anything if we are tagging it as special */
112910177Seric 	if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0)
113010177Seric 		return (name);
113110177Seric 
11327682Seric 	/*
11338181Seric 	**  Do a heuristic crack of this name to extract any comment info.
11348181Seric 	**	This will leave the name as a comment and a $g macro.
11357889Seric 	*/
11367889Seric 
113710310Seric 	if (canonical)
113816155Seric 		fancy = "\001g";
113910310Seric 	else
114010310Seric 		fancy = crackaddr(name);
11417889Seric 
11428181Seric 	/*
11438181Seric 	**  Turn the name into canonical form.
11448181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
11458181Seric 	**	If this only resolves to "user", and the "C" flag is
11468181Seric 	**	specified in the sending mailer, then the sender's
11478181Seric 	**	domain will be appended.
11488181Seric 	*/
11498181Seric 
115016914Seric 	pvp = prescan(name, '\0', pvpbuf);
11517889Seric 	if (pvp == NULL)
11527889Seric 		return (name);
11538181Seric 	rewrite(pvp, 3);
11548181Seric 	if (CurEnv->e_fromdomain != NULL)
11558181Seric 	{
11568181Seric 		/* append from domain to this address */
11578181Seric 		register char **pxp = pvp;
11588181Seric 
11599594Seric 		/* see if there is an "@domain" in the current name */
11608181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
11618181Seric 			pxp++;
11628181Seric 		if (*pxp == NULL)
11638181Seric 		{
11649594Seric 			/* no.... append the "@domain" from the sender */
11658181Seric 			register char **qxq = CurEnv->e_fromdomain;
11668181Seric 
11679594Seric 			while ((*pxp++ = *qxq++) != NULL)
11689594Seric 				continue;
116911726Seric 			rewrite(pvp, 3);
11708181Seric 		}
11718181Seric 	}
11728181Seric 
11738181Seric 	/*
11748959Seric 	**  Do more specific rewriting.
11758181Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
11768181Seric 	**		a sender address or not.
11778181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
11788181Seric 	*/
11798181Seric 
11808069Seric 	if (senderaddress)
11817755Seric 	{
11827889Seric 		rewrite(pvp, 1);
11838069Seric 		if (m->m_s_rwset > 0)
11848069Seric 			rewrite(pvp, m->m_s_rwset);
11858069Seric 	}
11868069Seric 	else
11878069Seric 	{
11887889Seric 		rewrite(pvp, 2);
11898069Seric 		if (m->m_r_rwset > 0)
11908069Seric 			rewrite(pvp, m->m_r_rwset);
11917682Seric 	}
11927682Seric 
11938181Seric 	/*
11948959Seric 	**  Do any final sanitation the address may require.
11958959Seric 	**	This will normally be used to turn internal forms
11968959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
11978959Seric 	**	may be used as a default to the above rules.
11988959Seric 	*/
11998959Seric 
12008959Seric 	rewrite(pvp, 4);
12018959Seric 
12028959Seric 	/*
12038181Seric 	**  Now restore the comment information we had at the beginning.
12048181Seric 	*/
12058181Seric 
12067682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
12079374Seric 	define('g', lbuf, CurEnv);
12087889Seric 	expand(fancy, buf, &buf[sizeof buf - 1], CurEnv);
12099374Seric 	define('g', oldg, CurEnv);
12107682Seric 
12117682Seric 	if (tTd(12, 1))
12127755Seric 		printf("remotename => `%s'\n", buf);
12137682Seric 	return (buf);
12147682Seric }
121551317Seric /*
121651317Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
121751317Seric **
121851317Seric **	Parameters:
121951317Seric **		a -- the address to map (but just the user name part).
122051317Seric **		sendq -- the sendq in which to install any replacement
122151317Seric **			addresses.
122251317Seric **
122351317Seric **	Returns:
122451317Seric **		none.
122551317Seric */
122651317Seric 
122751317Seric maplocaluser(a, sendq)
122851317Seric 	register ADDRESS *a;
122951317Seric 	ADDRESS **sendq;
123051317Seric {
123151317Seric 	register char **pvp;
123251317Seric 	register ADDRESS *a1 = NULL;
123351317Seric 	char pvpbuf[PSBUFSIZE];
123451317Seric 
123551317Seric 	if (tTd(29, 1))
123651317Seric 	{
123751317Seric 		printf("maplocaluser: ");
123851317Seric 		printaddr(a, FALSE);
123951317Seric 	}
124051317Seric 	pvp = prescan(a->q_user, '\0', pvpbuf);
124151317Seric 	if (pvp == NULL)
124251317Seric 		return;
124351317Seric 
124451317Seric 	rewrite(pvp, 5);
124551317Seric 	if (pvp[0] == NULL || pvp[0][0] != CANONNET)
124651317Seric 		return;
124751317Seric 
124851317Seric 	/* if non-null, mailer destination specified -- has it changed? */
124951317Seric 	a1 = buildaddr(pvp, NULL);
125051317Seric 	if (a1 == NULL || sameaddr(a, a1))
125151317Seric 		return;
125251317Seric 
125351317Seric 	/* mark old address as dead; insert new address */
125451317Seric 	a->q_flags |= QDONTSEND;
125551317Seric 	a1->q_alias = a;
125651317Seric 	allocaddr(a1, 1, NULL);
125751317Seric 	(void) recipient(a1, sendq);
125851317Seric }
1259