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*56823Seric static char sccsid[] = "@(#)parseaddr.c	5.26 (Berkeley) 11/15/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)
8056729Seric 	{
8156729Seric 		if (tTd(20, 1))
8256729Seric 			printf("parseaddr-->NULL\n");
83297Seric 		return (NULL);
8456729Seric 	}
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;
30356764Seric 	if (tTd(22, 11))
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++;
33356764Seric 			if (c == '\0' || (c == delim && anglecnt <= 0))
33456764Seric 			{
33556764Seric 				/* diagnose and patch up bad syntax */
33656764Seric 				if (state == QST)
33756764Seric 				{
33856764Seric 					usrerr("Unbalanced '\"'");
33956764Seric 					c = '"';
34056764Seric 				}
34156764Seric 				else if (cmntcnt > 0)
34256764Seric 				{
34356764Seric 					usrerr("Unbalanced '('");
34456764Seric 					c = ')';
34556764Seric 				}
34656764Seric 				else if (anglecnt > 0)
34756764Seric 				{
34856764Seric 					c = '>';
34956764Seric 					usrerr("Unbalanced '<'");
35056764Seric 				}
35156764Seric 				else
35256764Seric 					break;
35315284Seric 
35456764Seric 				p--;
35556764Seric 			}
35656764Seric 
3578078Seric 			if (tTd(22, 101))
3588078Seric 				printf("c=%c, s=%d; ", c, state);
3598078Seric 
3603149Seric 			/* chew up special characters */
3613149Seric 			*q = '\0';
3623149Seric 			if (bslashmode)
3633149Seric 			{
36424944Seric 				/* kludge \! for naive users */
36524944Seric 				if (c != '!')
36656678Seric 					*q++ = '\\';
3673149Seric 				bslashmode = FALSE;
36856678Seric 				continue;
3693149Seric 			}
37056678Seric 
37156678Seric 			if (c == '\\')
3723149Seric 			{
3733149Seric 				bslashmode = TRUE;
3748078Seric 				c = NOCHAR;
37556678Seric 				continue;
3763149Seric 			}
37756678Seric 			else if (state == QST)
3788514Seric 			{
3798514Seric 				/* do nothing, just avoid next clauses */
3808514Seric 			}
3818078Seric 			else if (c == '(')
3824100Seric 			{
3838078Seric 				cmntcnt++;
3848078Seric 				c = NOCHAR;
3854100Seric 			}
3868078Seric 			else if (c == ')')
3873149Seric 			{
3888078Seric 				if (cmntcnt <= 0)
3893149Seric 				{
3908078Seric 					usrerr("Unbalanced ')'");
3918078Seric 					DelimChar = p;
3928078Seric 					return (NULL);
3933149Seric 				}
3948078Seric 				else
3958078Seric 					cmntcnt--;
3968078Seric 			}
3978078Seric 			else if (cmntcnt > 0)
3988078Seric 				c = NOCHAR;
3998423Seric 			else if (c == '<')
4008423Seric 				anglecnt++;
4018423Seric 			else if (c == '>')
4028423Seric 			{
4038423Seric 				if (anglecnt <= 0)
4048423Seric 				{
4058423Seric 					usrerr("Unbalanced '>'");
4068423Seric 					DelimChar = p;
4078423Seric 					return (NULL);
4088423Seric 				}
4098423Seric 				anglecnt--;
4108423Seric 			}
41111423Seric 			else if (delim == ' ' && isspace(c))
41211423Seric 				c = ' ';
4133149Seric 
4148078Seric 			if (c == NOCHAR)
4158078Seric 				continue;
4163149Seric 
4178078Seric 			/* see if this is end of input */
41811405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4193149Seric 				break;
4203149Seric 
4218078Seric 			newstate = StateTab[state][toktype(c)];
4228078Seric 			if (tTd(22, 101))
4238078Seric 				printf("ns=%02o\n", newstate);
4248078Seric 			state = newstate & TYPE;
4258078Seric 			if (bitset(M, newstate))
4268078Seric 				c = NOCHAR;
4278078Seric 			if (bitset(B, newstate))
4284228Seric 				break;
429297Seric 		}
4303149Seric 
4313149Seric 		/* new token */
4328078Seric 		if (tok != q)
4331378Seric 		{
4348078Seric 			*q++ = '\0';
4358078Seric 			if (tTd(22, 36))
436297Seric 			{
4378078Seric 				printf("tok=");
4388078Seric 				xputs(tok);
43923109Seric 				(void) putchar('\n');
440297Seric 			}
4418078Seric 			if (avp >= &av[MAXATOM])
442297Seric 			{
4438078Seric 				syserr("prescan: too many tokens");
4448078Seric 				DelimChar = p;
4458078Seric 				return (NULL);
446297Seric 			}
4478078Seric 			*avp++ = tok;
448297Seric 		}
4498423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4503149Seric 	*avp = NULL;
4518078Seric 	DelimChar = --p;
45256764Seric 	if (tTd(22, 12))
45356764Seric 	{
45456764Seric 		printf("prescan==>");
45556764Seric 		printav(av);
45656764Seric 	}
45756764Seric 	if (av[0] != NULL)
4583149Seric 		return (av);
4593149Seric 	return (NULL);
4603149Seric }
4613149Seric /*
4623149Seric **  TOKTYPE -- return token type
4633149Seric **
4643149Seric **	Parameters:
4653149Seric **		c -- the character in question.
4663149Seric **
4673149Seric **	Returns:
4683149Seric **		Its type.
4693149Seric **
4703149Seric **	Side Effects:
4713149Seric **		none.
4723149Seric */
473297Seric 
4743149Seric toktype(c)
4753149Seric 	register char c;
4763149Seric {
4773380Seric 	static char buf[50];
4783382Seric 	static bool firstime = TRUE;
4793380Seric 
4803382Seric 	if (firstime)
4813380Seric 	{
4823382Seric 		firstime = FALSE;
48316155Seric 		expand("\001o", buf, &buf[sizeof buf - 1], CurEnv);
4847005Seric 		(void) strcat(buf, DELIMCHARS);
4853380Seric 	}
48656327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
4878078Seric 		return (ONE);
4888078Seric 	if (c == '"')
4898078Seric 		return (QST);
4904100Seric 	if (!isascii(c))
4918078Seric 		return (ATM);
4928078Seric 	if (isspace(c) || c == ')')
4938078Seric 		return (SPC);
49456795Seric 	if (iscntrl(c) || strchr(buf, c) != NULL)
4958078Seric 		return (OPR);
4968078Seric 	return (ATM);
4973149Seric }
4983149Seric /*
4993149Seric **  REWRITE -- apply rewrite rules to token vector.
5003149Seric **
5014476Seric **	This routine is an ordered production system.  Each rewrite
5024476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5034476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5044476Seric **
5054476Seric **	For each rewrite rule, 'avp' points the address vector we
5064476Seric **	are trying to match against, and 'pvp' points to the pattern.
5078058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5089585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5099585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5104476Seric **
5114476Seric **	When a match between avp & pvp does not match, we try to
5129585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5134476Seric **	we must also back out the match in mvp.  If we reach a
5148058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5158058Seric **	over again.
5164476Seric **
5174476Seric **	When we finally match, we rewrite the address vector
5184476Seric **	and try over again.
5194476Seric **
5203149Seric **	Parameters:
5213149Seric **		pvp -- pointer to token vector.
5223149Seric **
5233149Seric **	Returns:
5243149Seric **		none.
5253149Seric **
5263149Seric **	Side Effects:
5273149Seric **		pvp is modified.
5283149Seric */
5292091Seric 
5303149Seric struct match
5313149Seric {
5324468Seric 	char	**first;	/* first token matched */
5334468Seric 	char	**last;		/* last token matched */
5343149Seric };
5353149Seric 
5364468Seric # define MAXMATCH	9	/* max params per rewrite */
5373149Seric 
5383149Seric 
5394070Seric rewrite(pvp, ruleset)
5403149Seric 	char **pvp;
5414070Seric 	int ruleset;
5423149Seric {
5433149Seric 	register char *ap;		/* address pointer */
5443149Seric 	register char *rp;		/* rewrite pointer */
5453149Seric 	register char **avp;		/* address vector pointer */
5463149Seric 	register char **rvp;		/* rewrite vector pointer */
5478058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5488058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
54956678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5503149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5513149Seric 
5529279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
5533149Seric 	{
5548959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
55556678Seric 		printav(pvp);
5563149Seric 	}
55756678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
55856326Seric 	{
55956678Seric 		syserr("rewrite: illegal ruleset number %d", ruleset);
56056326Seric 		return;
56156326Seric 	}
56256678Seric 	if (pvp == NULL)
56356678Seric 		return;
56456326Seric 
5653149Seric 	/*
56656678Seric 	**  Run through the list of rewrite rules, applying
56756678Seric 	**	any that match.
5683149Seric 	*/
5693149Seric 
5704070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
5713149Seric 	{
57256678Seric 		int loopcount = 0;
57356678Seric 
5747675Seric 		if (tTd(21, 12))
575297Seric 		{
5768069Seric 			printf("-----trying rule:");
57756678Seric 			printav(rwr->r_lhs);
5783149Seric 		}
5793149Seric 
5803149Seric 		/* try to match on this rule */
5814468Seric 		mlp = mlist;
5828058Seric 		rvp = rwr->r_lhs;
5838058Seric 		avp = pvp;
5848058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
5853149Seric 		{
58656678Seric 			if (++loopcount > 100)
58752637Seric 			{
58856678Seric 				syserr("Infinite loop in ruleset %d", ruleset);
58956678Seric 				printf("workspace: ");
59056678Seric 				printav(pvp);
59152637Seric 				break;
59252637Seric 			}
5933149Seric 			rp = *rvp;
5948058Seric 			if (tTd(21, 35))
5958058Seric 			{
59656678Seric 				printf("operator=");
5978058Seric 				xputs(ap);
59856678Seric 				printf(", token=");
5998058Seric 				xputs(rp);
6008069Seric 				printf("\n");
6018058Seric 			}
60256678Seric 			if (rp == NULL)
60356326Seric 			{
6043149Seric 				/* end-of-pattern before end-of-address */
6058058Seric 				goto backup;
60656678Seric 			}
60756678Seric 			if (ap == NULL && *rp != MATCHZANY)
60856326Seric 			{
60956678Seric 				/* end-of-input */
61056678Seric 				break;
611297Seric 			}
61256326Seric 
61356678Seric 			switch (*rp)
6148058Seric 			{
61556678Seric 				register STAB *s;
61656326Seric 
61756678Seric 			  case MATCHCLASS:
61856678Seric 			  case MATCHNCLASS:
61956678Seric 				/* match any token in (not in) a class */
62056678Seric 				s = stab(ap, ST_CLASS, ST_FIND);
62156678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
62256326Seric 				{
62356678Seric 					if (*rp == MATCHCLASS)
6249585Seric 						goto backup;
62556326Seric 				}
62656678Seric 				else if (*rp == MATCHNCLASS)
62756678Seric 					goto backup;
6284060Seric 
62956678Seric 				/* explicit fall-through */
63056678Seric 
63156678Seric 			  case MATCHONE:
63256678Seric 			  case MATCHANY:
63356678Seric 				/* match exactly one token */
63456678Seric 				mlp->first = avp;
63556678Seric 				mlp->last = avp++;
6368058Seric 				mlp++;
63756678Seric 				break;
6388058Seric 
63956678Seric 			  case MATCHZANY:
64056678Seric 				/* match zero or more tokens */
64156678Seric 				mlp->first = avp;
64256678Seric 				mlp->last = avp - 1;
64356678Seric 				mlp++;
64456678Seric 				break;
64556326Seric 
64656678Seric 			  default:
64756678Seric 				/* must have exact match */
64856678Seric 				if (strcasecmp(rp, ap))
6498058Seric 					goto backup;
6504468Seric 				avp++;
65156678Seric 				break;
6523149Seric 			}
6533149Seric 
65456678Seric 			/* successful match on this token */
6553149Seric 			rvp++;
6563149Seric 			continue;
6573149Seric 
65856678Seric 		  backup:
65956678Seric 			/* match failed -- back up */
66056678Seric 			while (--rvp >= rwr->r_lhs)
6613149Seric 			{
66256678Seric 				rp = *rvp;
66356678Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
6644468Seric 				{
66556678Seric 					/* extend binding and continue */
66656678Seric 					avp = ++mlp[-1].last;
66756678Seric 					avp++;
66856678Seric 					rvp++;
66956678Seric 					break;
6704468Seric 				}
67156678Seric 				avp--;
67256678Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS ||
67356678Seric 				    *rp == MATCHNCLASS)
67456678Seric 				{
67556678Seric 					/* back out binding */
67656678Seric 					mlp--;
67756678Seric 				}
6783149Seric 			}
6793149Seric 
68056678Seric 			if (rvp < rwr->r_lhs)
68156678Seric 			{
68256678Seric 				/* total failure to match */
68356326Seric 				break;
6843149Seric 			}
685297Seric 		}
6863149Seric 
6873149Seric 		/*
68856678Seric 		**  See if we successfully matched
6893149Seric 		*/
6903149Seric 
69156678Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
6923149Seric 		{
6939374Seric 			if (tTd(21, 10))
6949374Seric 				printf("----- rule fails\n");
6959374Seric 			rwr = rwr->r_next;
6969374Seric 			continue;
6979374Seric 		}
6983149Seric 
6999374Seric 		rvp = rwr->r_rhs;
7009374Seric 		if (tTd(21, 12))
7019374Seric 		{
7029374Seric 			printf("-----rule matches:");
70356678Seric 			printav(rvp);
7049374Seric 		}
7059374Seric 
7069374Seric 		rp = *rvp;
7079374Seric 		if (*rp == CANONUSER)
7089374Seric 		{
7099374Seric 			rvp++;
7109374Seric 			rwr = rwr->r_next;
7119374Seric 		}
7129374Seric 		else if (*rp == CANONHOST)
7139374Seric 		{
7149374Seric 			rvp++;
7159374Seric 			rwr = NULL;
7169374Seric 		}
7179374Seric 		else if (*rp == CANONNET)
7189374Seric 			rwr = NULL;
7199374Seric 
7209374Seric 		/* substitute */
7219374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
7229374Seric 		{
7239374Seric 			register struct match *m;
7249374Seric 			register char **pp;
7259374Seric 
7268058Seric 			rp = *rvp;
72756678Seric 			if (*rp == MATCHREPL)
7288058Seric 			{
72916914Seric 				/* substitute from LHS */
73016914Seric 				m = &mlist[rp[1] - '1'];
73156678Seric 				if (m < mlist || m >= mlp)
7329374Seric 				{
73356678Seric 					syserr("rewrite: ruleset %d: replacement $%c out of bounds",
73456326Seric 						ruleset, rp[1]);
7359374Seric 					return;
7369374Seric 				}
73716914Seric 				if (tTd(21, 15))
73816914Seric 				{
73916914Seric 					printf("$%c:", rp[1]);
74016914Seric 					pp = m->first;
74156678Seric 					while (pp <= m->last)
74216914Seric 					{
74316914Seric 						printf(" %x=\"", *pp);
74416914Seric 						(void) fflush(stdout);
74516914Seric 						printf("%s\"", *pp++);
74616914Seric 					}
74716914Seric 					printf("\n");
74816914Seric 				}
7499374Seric 				pp = m->first;
75056678Seric 				while (pp <= m->last)
7513149Seric 				{
75216914Seric 					if (avp >= &npvp[MAXATOM])
75356678Seric 					{
75456678Seric 						syserr("rewrite: expansion too long");
75556678Seric 						return;
75656678Seric 					}
75716914Seric 					*avp++ = *pp++;
7583149Seric 				}
7593149Seric 			}
76016914Seric 			else
7618226Seric 			{
76216914Seric 				/* vanilla replacement */
7639374Seric 				if (avp >= &npvp[MAXATOM])
76416889Seric 				{
76556678Seric 	toolong:
76616889Seric 					syserr("rewrite: expansion too long");
76716889Seric 					return;
76816889Seric 				}
76956678Seric 				*avp++ = rp;
7708226Seric 			}
7719374Seric 		}
7729374Seric 		*avp++ = NULL;
77316914Seric 
77416914Seric 		/*
77556678Seric 		**  Check for any hostname/keyword lookups.
77616914Seric 		*/
77716914Seric 
77816914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
77916914Seric 		{
78056678Seric 			char **hbrvp;
78116914Seric 			char **xpvp;
78216914Seric 			int trsize;
78317473Seric 			char *olddelimchar;
78456678Seric 			char *replac;
78556678Seric 			int endtoken;
78656678Seric 			STAB *map;
78756678Seric 			char *mapname;
78856678Seric 			char **key_rvp;
78956678Seric 			char **arg_rvp;
79056678Seric 			char **default_rvp;
79156678Seric 			char buf[MAXNAME + 1];
79216914Seric 			char *pvpb1[MAXATOM + 1];
793*56823Seric 			char *argvect[10];
79417174Seric 			char pvpbuf[PSBUFSIZE];
79556678Seric 			extern char *DelimChar;
79616914Seric 
79756678Seric 			if (**rvp != HOSTBEGIN && **rvp != LOOKUPBEGIN)
79816914Seric 				continue;
79916914Seric 
80016914Seric 			/*
80156678Seric 			**  Got a hostname/keyword lookup.
80216914Seric 			**
80316914Seric 			**	This could be optimized fairly easily.
80416914Seric 			*/
80516914Seric 
80616914Seric 			hbrvp = rvp;
80756678Seric 			if (**rvp == HOSTBEGIN)
80856327Seric 			{
80956678Seric 				endtoken = HOSTEND;
81056678Seric 				mapname = "host";
81156327Seric 			}
81256326Seric 			else
81356327Seric 			{
81456678Seric 				endtoken = LOOKUPEND;
81556678Seric 				mapname = *++rvp;
81656327Seric 			}
81756678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
81856678Seric 			if (map == NULL)
81956678Seric 				syserr("rewrite: map %s not found", mapname);
82056678Seric 
82156678Seric 			/* extract the match part */
82256678Seric 			key_rvp = ++rvp;
823*56823Seric 			default_rvp = NULL;
824*56823Seric 			arg_rvp = argvect;
825*56823Seric 			xpvp = NULL;
826*56823Seric 			replac = pvpbuf;
82756678Seric 			while (*rvp != NULL && **rvp != endtoken)
82853654Seric 			{
829*56823Seric 				int nodetype = **rvp;
830*56823Seric 
831*56823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
83256678Seric 				{
833*56823Seric 					rvp++;
834*56823Seric 					continue;
835*56823Seric 				}
836*56823Seric 
837*56823Seric 				*rvp++ = NULL;
838*56823Seric 
839*56823Seric 				if (xpvp != NULL)
840*56823Seric 				{
841*56823Seric 					cataddr(xpvp, replac,
842*56823Seric 						&pvpbuf[sizeof pvpbuf] - replac);
843*56823Seric 					*++arg_rvp = replac;
844*56823Seric 					replac += strlen(replac) + 1;
845*56823Seric 					xpvp = NULL;
846*56823Seric 				}
847*56823Seric 				switch (nodetype)
848*56823Seric 				{
84956678Seric 				  case CANONHOST:
850*56823Seric 					xpvp = rvp;
85156678Seric 					break;
85256678Seric 
85356678Seric 				  case CANONUSER:
85456678Seric 					default_rvp = rvp;
85556678Seric 					break;
85656678Seric 				}
85753654Seric 			}
85816914Seric 			if (*rvp != NULL)
85916914Seric 				*rvp++ = NULL;
860*56823Seric 			if (xpvp != NULL)
861*56823Seric 			{
862*56823Seric 				cataddr(xpvp, replac,
863*56823Seric 					&pvpbuf[sizeof pvpbuf] - replac);
864*56823Seric 				*++arg_rvp = replac;
865*56823Seric 			}
866*56823Seric 			*++arg_rvp = NULL;
86716914Seric 
86816914Seric 			/* save the remainder of the input string */
86916914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
87016914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
87116914Seric 
87256678Seric 			/* look it up */
87356678Seric 			cataddr(key_rvp, buf, sizeof buf);
874*56823Seric 			argvect[0] = buf;
87556678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
876*56823Seric 				replac = (*map->s_map.map_class->map_lookup)(map,
877*56823Seric 						buf, sizeof buf - 1, argvect);
87853654Seric 			else
87956678Seric 				replac = NULL;
88056678Seric 
88156678Seric 			/* if no replacement, use default */
882*56823Seric 			if (replac == NULL && default_rvp != NULL)
883*56823Seric 			{
884*56823Seric 				char buf2[sizeof buf];
885*56823Seric 
886*56823Seric 				/* rewrite the default with % translations */
887*56823Seric 				cataddr(default_rvp, buf2, sizeof buf2);
888*56823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
889*56823Seric 					argvect);
890*56823Seric 				replac = buf;
891*56823Seric 			}
892*56823Seric 
89356678Seric 			if (replac == NULL)
89451317Seric 			{
895*56823Seric 				xpvp = key_rvp;
89653654Seric 			}
89756678Seric 			else
89853654Seric 			{
89956678Seric 				/* scan the new replacement */
90056678Seric 				olddelimchar = DelimChar;
90156678Seric 				xpvp = prescan(replac, '\0', pvpbuf);
90256678Seric 				DelimChar = olddelimchar;
90353654Seric 				if (xpvp == NULL)
90451317Seric 				{
90556678Seric 					syserr("rewrite: cannot prescan map value: %s", replac);
90656678Seric 					return;
90756678Seric 				}
90851317Seric 			}
90951317Seric 
91016914Seric 			/* append it to the token list */
91156678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
91256678Seric 			{
91317174Seric 				*avp++ = newstr(*xpvp);
91416920Seric 				if (avp >= &npvp[MAXATOM])
91516914Seric 					goto toolong;
91617174Seric 			}
91716914Seric 
91816914Seric 			/* restore the old trailing information */
91956678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
92016920Seric 				if (avp >= &npvp[MAXATOM])
92116914Seric 					goto toolong;
92217174Seric 
92356678Seric 			break;
92416914Seric 		}
92516914Seric 
92616914Seric 		/*
92716914Seric 		**  Check for subroutine calls.
92816914Seric 		*/
92916914Seric 
93056678Seric 		if (*npvp != NULL && **npvp == CALLSUBR)
93156678Seric 		{
93256678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
93356678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
93456678Seric 			if (tTd(21, 3))
93556678Seric 				printf("-----callsubr %s\n", npvp[1]);
93656678Seric 			rewrite(pvp, atoi(npvp[1]));
93756678Seric 		}
93856678Seric 		else
93956678Seric 		{
94017348Seric 			bcopy((char *) npvp, (char *) pvp,
94116900Seric 				(int) (avp - npvp) * sizeof *avp);
94256678Seric 		}
9439374Seric 		if (tTd(21, 4))
9449374Seric 		{
9459374Seric 			printf("rewritten as:");
94656678Seric 			printav(pvp);
9479374Seric 		}
948297Seric 	}
9498069Seric 
9509279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
9518069Seric 	{
9528959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
95356678Seric 		printav(pvp);
9548069Seric 	}
9553149Seric }
9563149Seric /*
9573149Seric **  BUILDADDR -- build address from token vector.
9583149Seric **
9593149Seric **	Parameters:
9603149Seric **		tv -- token vector.
9613149Seric **		a -- pointer to address descriptor to fill.
9623149Seric **			If NULL, one will be allocated.
9633149Seric **
9643149Seric **	Returns:
9654279Seric **		NULL if there was an error.
9664279Seric **		'a' otherwise.
9673149Seric **
9683149Seric **	Side Effects:
9693149Seric **		fills in 'a'
9703149Seric */
9713149Seric 
97256678Seric ADDRESS *
9733149Seric buildaddr(tv, a)
9743149Seric 	register char **tv;
9753149Seric 	register ADDRESS *a;
9763149Seric {
9773149Seric 	static char buf[MAXNAME];
9783149Seric 	struct mailer **mp;
9793149Seric 	register struct mailer *m;
9803149Seric 
9813149Seric 	if (a == NULL)
9823149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
98316889Seric 	bzero((char *) a, sizeof *a);
9843149Seric 
9853149Seric 	/* figure out what net/mailer to use */
98656678Seric 	if (**tv != CANONNET)
9874279Seric 	{
9883149Seric 		syserr("buildaddr: no net");
9894279Seric 		return (NULL);
9904279Seric 	}
9913149Seric 	tv++;
99233725Sbostic 	if (!strcasecmp(*tv, "error"))
9934279Seric 	{
99410183Seric 		if (**++tv == CANONHOST)
99510183Seric 		{
99610183Seric 			setstat(atoi(*++tv));
99710183Seric 			tv++;
99810183Seric 		}
99910183Seric 		if (**tv != CANONUSER)
10004279Seric 			syserr("buildaddr: error: no user");
100156678Seric 		buf[0] = '\0';
10024279Seric 		while (*++tv != NULL)
10034279Seric 		{
10044279Seric 			if (buf[0] != '\0')
10057005Seric 				(void) strcat(buf, " ");
10067005Seric 			(void) strcat(buf, *tv);
10074279Seric 		}
10084279Seric 		usrerr(buf);
10094279Seric 		return (NULL);
10104279Seric 	}
10114598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
10123149Seric 	{
101333725Sbostic 		if (!strcasecmp(m->m_name, *tv))
10143149Seric 			break;
10153149Seric 	}
10163149Seric 	if (m == NULL)
10174279Seric 	{
101824944Seric 		syserr("buildaddr: unknown mailer %s", *tv);
10194279Seric 		return (NULL);
10204279Seric 	}
10214598Seric 	a->q_mailer = m;
10223149Seric 
10233149Seric 	/* figure out what host (if any) */
102456678Seric 	tv++;
102556678Seric 	if (!bitnset(M_LOCAL, m->m_flags))
10263149Seric 	{
102756678Seric 		if (**tv++ != CANONHOST)
10284279Seric 		{
10293149Seric 			syserr("buildaddr: no host");
10304279Seric 			return (NULL);
10314279Seric 		}
10325704Seric 		buf[0] = '\0';
103356678Seric 		while (*tv != NULL && **tv != CANONUSER)
103456678Seric 			(void) strcat(buf, *tv++);
10355704Seric 		a->q_host = newstr(buf);
10363149Seric 	}
103756678Seric 	else
103856678Seric 		a->q_host = NULL;
10393149Seric 
10403149Seric 	/* figure out the user */
104136615Sbostic 	if (*tv == NULL || **tv != CANONUSER)
10424279Seric 	{
10433149Seric 		syserr("buildaddr: no user");
10444279Seric 		return (NULL);
10454279Seric 	}
104619040Seric 
104756678Seric 	if (m == LocalMailer && tv[1] != NULL && strcmp(tv[1], "@") == 0)
104856678Seric 	{
104956678Seric 		tv++;
105056678Seric 		a->q_flags |= QNOTREMOTE;
105156678Seric 	}
105251317Seric 
105319040Seric 	/* rewrite according recipient mailer rewriting rules */
105419040Seric 	rewrite(++tv, 2);
105556327Seric 	if (m->m_r_rwset > 0)
105656327Seric 		rewrite(tv, m->m_r_rwset);
105719040Seric 	rewrite(tv, 4);
105819040Seric 
105919040Seric 	/* save the result for the command line/RCPT argument */
106011278Seric 	cataddr(tv, buf, sizeof buf);
10613149Seric 	a->q_user = buf;
10623149Seric 
10633149Seric 	return (a);
10643149Seric }
10653188Seric /*
10664228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
10674228Seric **
10684228Seric **	Parameters:
10694228Seric **		pvp -- parameter vector to rebuild.
10704228Seric **		buf -- buffer to build the string into.
10714228Seric **		sz -- size of buf.
10724228Seric **
10734228Seric **	Returns:
10744228Seric **		none.
10754228Seric **
10764228Seric **	Side Effects:
10774228Seric **		Destroys buf.
10784228Seric */
10794228Seric 
10804228Seric cataddr(pvp, buf, sz)
10814228Seric 	char **pvp;
10824228Seric 	char *buf;
10834228Seric 	register int sz;
10844228Seric {
10854228Seric 	bool oatomtok = FALSE;
108656678Seric 	bool natomtok = FALSE;
10874228Seric 	register int i;
10884228Seric 	register char *p;
10894228Seric 
10908423Seric 	if (pvp == NULL)
10918423Seric 	{
109223109Seric 		(void) strcpy(buf, "");
10938423Seric 		return;
10948423Seric 	}
10954228Seric 	p = buf;
109611156Seric 	sz -= 2;
10974228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
10984228Seric 	{
10998078Seric 		natomtok = (toktype(**pvp) == ATM);
11004228Seric 		if (oatomtok && natomtok)
11019042Seric 			*p++ = SpaceSub;
11024228Seric 		(void) strcpy(p, *pvp);
11034228Seric 		oatomtok = natomtok;
11044228Seric 		p += i;
110511156Seric 		sz -= i + 1;
11064228Seric 		pvp++;
11074228Seric 	}
11084228Seric 	*p = '\0';
11094228Seric }
11104228Seric /*
11113188Seric **  SAMEADDR -- Determine if two addresses are the same
11123188Seric **
11133188Seric **	This is not just a straight comparison -- if the mailer doesn't
11143188Seric **	care about the host we just ignore it, etc.
11153188Seric **
11163188Seric **	Parameters:
11173188Seric **		a, b -- pointers to the internal forms to compare.
11183188Seric **
11193188Seric **	Returns:
11203188Seric **		TRUE -- they represent the same mailbox.
11213188Seric **		FALSE -- they don't.
11223188Seric **
11233188Seric **	Side Effects:
11243188Seric **		none.
11253188Seric */
11263188Seric 
11273188Seric bool
11289374Seric sameaddr(a, b)
11293188Seric 	register ADDRESS *a;
11303188Seric 	register ADDRESS *b;
11313188Seric {
11323188Seric 	/* if they don't have the same mailer, forget it */
11333188Seric 	if (a->q_mailer != b->q_mailer)
11343188Seric 		return (FALSE);
11353188Seric 
11363188Seric 	/* if the user isn't the same, we can drop out */
113756678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
11383188Seric 		return (FALSE);
11393188Seric 
11403188Seric 	/* if the mailer ignores hosts, we have succeeded! */
114110690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
11423188Seric 		return (TRUE);
11433188Seric 
11443188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
11453188Seric 	if (a->q_host == NULL || b->q_host == NULL)
11463188Seric 		return (FALSE);
114756678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
11483188Seric 		return (FALSE);
11493188Seric 
11503188Seric 	return (TRUE);
11513188Seric }
11523234Seric /*
11533234Seric **  PRINTADDR -- print address (for debugging)
11543234Seric **
11553234Seric **	Parameters:
11563234Seric **		a -- the address to print
11573234Seric **		follow -- follow the q_next chain.
11583234Seric **
11593234Seric **	Returns:
11603234Seric **		none.
11613234Seric **
11623234Seric **	Side Effects:
11633234Seric **		none.
11643234Seric */
11653234Seric 
11663234Seric printaddr(a, follow)
11673234Seric 	register ADDRESS *a;
11683234Seric 	bool follow;
11693234Seric {
11705001Seric 	bool first = TRUE;
11715001Seric 
11723234Seric 	while (a != NULL)
11733234Seric 	{
11745001Seric 		first = FALSE;
11754443Seric 		printf("%x=", a);
11764085Seric 		(void) fflush(stdout);
117740973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
117840973Sbostic 		       a->q_paddr, a->q_mailer->m_mno, a->q_mailer->m_name,
117940973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
11808181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
11818181Seric 		       a->q_alias);
11828181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
11838181Seric 		       a->q_fullname);
11844996Seric 
11853234Seric 		if (!follow)
11863234Seric 			return;
11874996Seric 		a = a->q_next;
11883234Seric 	}
11895001Seric 	if (first)
11904443Seric 		printf("[NULL]\n");
11913234Seric }
11924317Seric 
11937682Seric /*
11947682Seric **  REMOTENAME -- return the name relative to the current mailer
11957682Seric **
11967682Seric **	Parameters:
11977682Seric **		name -- the name to translate.
11988069Seric **		m -- the mailer that we want to do rewriting relative
11998069Seric **			to.
12008069Seric **		senderaddress -- if set, uses the sender rewriting rules
12018069Seric **			rather than the recipient rewriting rules.
120210310Seric **		canonical -- if set, strip out any comment information,
120310310Seric **			etc.
12047682Seric **
12057682Seric **	Returns:
12067682Seric **		the text string representing this address relative to
12077682Seric **			the receiving mailer.
12087682Seric **
12097682Seric **	Side Effects:
12107682Seric **		none.
12117682Seric **
12127682Seric **	Warnings:
12137682Seric **		The text string returned is tucked away locally;
12147682Seric **			copy it if you intend to save it.
12157682Seric */
12167682Seric 
12177682Seric char *
121856678Seric remotename(name, m, senderaddress, canonical, e)
12197682Seric 	char *name;
122056678Seric 	struct mailer *m;
12218069Seric 	bool senderaddress;
122210310Seric 	bool canonical;
122356678Seric 	register ENVELOPE *e;
12247682Seric {
12258069Seric 	register char **pvp;
12268069Seric 	char *fancy;
122756678Seric 	extern char *macvalue();
122856678Seric 	char *oldg = macvalue('g', e);
12297682Seric 	static char buf[MAXNAME];
12307682Seric 	char lbuf[MAXNAME];
123116914Seric 	char pvpbuf[PSBUFSIZE];
123256678Seric 	extern char **prescan();
123356678Seric 	extern char *crackaddr();
12347682Seric 
12357755Seric 	if (tTd(12, 1))
12367755Seric 		printf("remotename(%s)\n", name);
12377755Seric 
123810177Seric 	/* don't do anything if we are tagging it as special */
123956327Seric 	if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0)
124010177Seric 		return (name);
124110177Seric 
12427682Seric 	/*
12438181Seric 	**  Do a heuristic crack of this name to extract any comment info.
12448181Seric 	**	This will leave the name as a comment and a $g macro.
12457889Seric 	*/
12467889Seric 
124710310Seric 	if (canonical)
124816155Seric 		fancy = "\001g";
124910310Seric 	else
125010310Seric 		fancy = crackaddr(name);
12517889Seric 
12528181Seric 	/*
12538181Seric 	**  Turn the name into canonical form.
12548181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
12558181Seric 	**	If this only resolves to "user", and the "C" flag is
12568181Seric 	**	specified in the sending mailer, then the sender's
12578181Seric 	**	domain will be appended.
12588181Seric 	*/
12598181Seric 
126016914Seric 	pvp = prescan(name, '\0', pvpbuf);
12617889Seric 	if (pvp == NULL)
12627889Seric 		return (name);
12638181Seric 	rewrite(pvp, 3);
126456678Seric 	if (e->e_fromdomain != NULL)
12658181Seric 	{
12668181Seric 		/* append from domain to this address */
12678181Seric 		register char **pxp = pvp;
12688181Seric 
12699594Seric 		/* see if there is an "@domain" in the current name */
12708181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
12718181Seric 			pxp++;
12728181Seric 		if (*pxp == NULL)
12738181Seric 		{
12749594Seric 			/* no.... append the "@domain" from the sender */
127556678Seric 			register char **qxq = e->e_fromdomain;
12768181Seric 
12779594Seric 			while ((*pxp++ = *qxq++) != NULL)
12789594Seric 				continue;
127911726Seric 			rewrite(pvp, 3);
12808181Seric 		}
12818181Seric 	}
12828181Seric 
12838181Seric 	/*
12848959Seric 	**  Do more specific rewriting.
128556678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
128656678Seric 	**		a sender address or not.
12878181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
12888181Seric 	*/
12898181Seric 
12908069Seric 	if (senderaddress)
12917755Seric 	{
129256327Seric 		rewrite(pvp, 1);
129356327Seric 		if (m->m_s_rwset > 0)
129456327Seric 			rewrite(pvp, m->m_s_rwset);
12958069Seric 	}
12968069Seric 	else
12978069Seric 	{
129856327Seric 		rewrite(pvp, 2);
129956327Seric 		if (m->m_r_rwset > 0)
130056327Seric 			rewrite(pvp, m->m_r_rwset);
13017682Seric 	}
13027682Seric 
13038181Seric 	/*
13048959Seric 	**  Do any final sanitation the address may require.
13058959Seric 	**	This will normally be used to turn internal forms
13068959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
13078959Seric 	**	may be used as a default to the above rules.
13088959Seric 	*/
13098959Seric 
13108959Seric 	rewrite(pvp, 4);
13118959Seric 
13128959Seric 	/*
13138181Seric 	**  Now restore the comment information we had at the beginning.
13148181Seric 	*/
13158181Seric 
13167682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
131756678Seric 	define('g', lbuf, e);
131856678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
131956678Seric 	define('g', oldg, e);
13207682Seric 
13217682Seric 	if (tTd(12, 1))
13227755Seric 		printf("remotename => `%s'\n", buf);
13237682Seric 	return (buf);
13247682Seric }
132551317Seric /*
132656678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
132751317Seric **
132851317Seric **	Parameters:
132956678Seric **		a -- the address to map (but just the user name part).
133056678Seric **		sendq -- the sendq in which to install any replacement
133156678Seric **			addresses.
133251317Seric **
133351317Seric **	Returns:
133451317Seric **		none.
133551317Seric */
133651317Seric 
133756678Seric maplocaluser(a, sendq, e)
133856678Seric 	register ADDRESS *a;
133956678Seric 	ADDRESS **sendq;
134056678Seric 	ENVELOPE *e;
134151317Seric {
134256678Seric 	register char **pvp;
134356678Seric 	register ADDRESS *a1 = NULL;
134456678Seric 	char pvpbuf[PSBUFSIZE];
134551317Seric 
134656678Seric 	if (tTd(29, 1))
134756678Seric 	{
134856678Seric 		printf("maplocaluser: ");
134956678Seric 		printaddr(a, FALSE);
135056678Seric 	}
135156678Seric 	pvp = prescan(a->q_user, '\0', pvpbuf);
135256678Seric 	if (pvp == NULL)
135356678Seric 		return;
135451317Seric 
135556678Seric 	rewrite(pvp, 5);
135656678Seric 	if (pvp[0] == NULL || pvp[0][0] != CANONNET)
135756678Seric 		return;
135851317Seric 
135956678Seric 	/* if non-null, mailer destination specified -- has it changed? */
136056678Seric 	a1 = buildaddr(pvp, NULL);
136156678Seric 	if (a1 == NULL || sameaddr(a, a1))
136256678Seric 		return;
136351317Seric 
136456678Seric 	/* mark old address as dead; insert new address */
136556678Seric 	a->q_flags |= QDONTSEND;
136656678Seric 	a1->q_alias = a;
136756678Seric 	allocaddr(a1, 1, NULL);
136856678Seric 	(void) recipient(a1, sendq, e);
136951317Seric }
1370