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*57249Seric static char sccsid[] = "@(#)parseaddr.c	5.28 (Berkeley) 12/21/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];
79356823Seric 			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;
82356823Seric 			default_rvp = NULL;
82456823Seric 			arg_rvp = argvect;
82556823Seric 			xpvp = NULL;
82656823Seric 			replac = pvpbuf;
82756678Seric 			while (*rvp != NULL && **rvp != endtoken)
82853654Seric 			{
82956823Seric 				int nodetype = **rvp;
83056823Seric 
83156823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
83256678Seric 				{
83356823Seric 					rvp++;
83456823Seric 					continue;
83556823Seric 				}
83656823Seric 
83756823Seric 				*rvp++ = NULL;
83856823Seric 
83956823Seric 				if (xpvp != NULL)
84056823Seric 				{
84156823Seric 					cataddr(xpvp, replac,
84256823Seric 						&pvpbuf[sizeof pvpbuf] - replac);
84356823Seric 					*++arg_rvp = replac;
84456823Seric 					replac += strlen(replac) + 1;
84556823Seric 					xpvp = NULL;
84656823Seric 				}
84756823Seric 				switch (nodetype)
84856823Seric 				{
84956678Seric 				  case CANONHOST:
85056823Seric 					xpvp = rvp;
85156678Seric 					break;
85256678Seric 
85356678Seric 				  case CANONUSER:
85456678Seric 					default_rvp = rvp;
85556678Seric 					break;
85656678Seric 				}
85753654Seric 			}
85816914Seric 			if (*rvp != NULL)
85916914Seric 				*rvp++ = NULL;
86056823Seric 			if (xpvp != NULL)
86156823Seric 			{
86256823Seric 				cataddr(xpvp, replac,
86356823Seric 					&pvpbuf[sizeof pvpbuf] - replac);
86456823Seric 				*++arg_rvp = replac;
86556823Seric 			}
86656823Seric 			*++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);
87456823Seric 			argvect[0] = buf;
87556678Seric 			if (map != NULL && bitset(MF_VALID, map->s_map.map_flags))
87656836Seric 			{
87756836Seric 				int bsize = sizeof buf - 1;
87856836Seric 
87956836Seric 				if (map->s_map.map_app != NULL)
88056836Seric 					bsize -= strlen(map->s_map.map_app);
88156836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
88256823Seric 						buf, sizeof buf - 1, argvect);
88356836Seric 				if (replac != NULL && map->s_map.map_app != NULL)
88456836Seric 					strcat(replac, map->s_map.map_app);
88556836Seric 			}
88653654Seric 			else
88756678Seric 				replac = NULL;
88856678Seric 
88956678Seric 			/* if no replacement, use default */
89056823Seric 			if (replac == NULL && default_rvp != NULL)
89156823Seric 			{
89256823Seric 				char buf2[sizeof buf];
89356823Seric 
89456823Seric 				/* rewrite the default with % translations */
89556823Seric 				cataddr(default_rvp, buf2, sizeof buf2);
89656823Seric 				map_rewrite(buf2, sizeof buf2, buf, sizeof buf,
89756823Seric 					argvect);
89856823Seric 				replac = buf;
89956823Seric 			}
90056823Seric 
90156678Seric 			if (replac == NULL)
90251317Seric 			{
90356823Seric 				xpvp = key_rvp;
90453654Seric 			}
90556678Seric 			else
90653654Seric 			{
90756678Seric 				/* scan the new replacement */
90856678Seric 				olddelimchar = DelimChar;
90956678Seric 				xpvp = prescan(replac, '\0', pvpbuf);
91056678Seric 				DelimChar = olddelimchar;
91153654Seric 				if (xpvp == NULL)
91251317Seric 				{
91356678Seric 					syserr("rewrite: cannot prescan map value: %s", replac);
91456678Seric 					return;
91556678Seric 				}
91651317Seric 			}
91751317Seric 
91816914Seric 			/* append it to the token list */
91956678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
92056678Seric 			{
92117174Seric 				*avp++ = newstr(*xpvp);
92216920Seric 				if (avp >= &npvp[MAXATOM])
92316914Seric 					goto toolong;
92417174Seric 			}
92516914Seric 
92616914Seric 			/* restore the old trailing information */
92756678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
92816920Seric 				if (avp >= &npvp[MAXATOM])
92916914Seric 					goto toolong;
93017174Seric 
93156678Seric 			break;
93216914Seric 		}
93316914Seric 
93416914Seric 		/*
93516914Seric 		**  Check for subroutine calls.
93616914Seric 		*/
93716914Seric 
93856678Seric 		if (*npvp != NULL && **npvp == CALLSUBR)
93956678Seric 		{
94056678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
94156678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
94256678Seric 			if (tTd(21, 3))
94356678Seric 				printf("-----callsubr %s\n", npvp[1]);
94456678Seric 			rewrite(pvp, atoi(npvp[1]));
94556678Seric 		}
94656678Seric 		else
94756678Seric 		{
94817348Seric 			bcopy((char *) npvp, (char *) pvp,
94916900Seric 				(int) (avp - npvp) * sizeof *avp);
95056678Seric 		}
9519374Seric 		if (tTd(21, 4))
9529374Seric 		{
9539374Seric 			printf("rewritten as:");
95456678Seric 			printav(pvp);
9559374Seric 		}
956297Seric 	}
9578069Seric 
9589279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
9598069Seric 	{
9608959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
96156678Seric 		printav(pvp);
9628069Seric 	}
9633149Seric }
9643149Seric /*
9653149Seric **  BUILDADDR -- build address from token vector.
9663149Seric **
9673149Seric **	Parameters:
9683149Seric **		tv -- token vector.
9693149Seric **		a -- pointer to address descriptor to fill.
9703149Seric **			If NULL, one will be allocated.
9713149Seric **
9723149Seric **	Returns:
9734279Seric **		NULL if there was an error.
9744279Seric **		'a' otherwise.
9753149Seric **
9763149Seric **	Side Effects:
9773149Seric **		fills in 'a'
9783149Seric */
9793149Seric 
980*57249Seric struct errcodes
981*57249Seric {
982*57249Seric 	char	*ec_name;		/* name of error code */
983*57249Seric 	int	ec_code;		/* numeric code */
984*57249Seric } ErrorCodes[] =
985*57249Seric {
986*57249Seric 	"usage",	EX_USAGE,
987*57249Seric 	"nouser",	EX_NOUSER,
988*57249Seric 	"nohost",	EX_NOHOST,
989*57249Seric 	"unavailable",	EX_UNAVAILABLE,
990*57249Seric 	"software",	EX_SOFTWARE,
991*57249Seric 	"tempfail",	EX_TEMPFAIL,
992*57249Seric 	"protocol",	EX_PROTOCOL,
993*57249Seric #ifdef EX_CONFIG
994*57249Seric 	"config",	EX_CONFIG,
995*57249Seric #endif
996*57249Seric 	NULL,		EX_UNAVAILABLE,
997*57249Seric };
998*57249Seric 
99956678Seric ADDRESS *
10003149Seric buildaddr(tv, a)
10013149Seric 	register char **tv;
10023149Seric 	register ADDRESS *a;
10033149Seric {
10043149Seric 	static char buf[MAXNAME];
10053149Seric 	struct mailer **mp;
10063149Seric 	register struct mailer *m;
10073149Seric 
10083149Seric 	if (a == NULL)
10093149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
101016889Seric 	bzero((char *) a, sizeof *a);
10113149Seric 
10123149Seric 	/* figure out what net/mailer to use */
101356678Seric 	if (**tv != CANONNET)
10144279Seric 	{
10153149Seric 		syserr("buildaddr: no net");
10164279Seric 		return (NULL);
10174279Seric 	}
10183149Seric 	tv++;
101933725Sbostic 	if (!strcasecmp(*tv, "error"))
10204279Seric 	{
102110183Seric 		if (**++tv == CANONHOST)
102210183Seric 		{
1023*57249Seric 			register struct errcodes *ep;
1024*57249Seric 
1025*57249Seric 			if (isdigit(**++tv))
1026*57249Seric 			{
1027*57249Seric 				setstat(atoi(*tv));
1028*57249Seric 			}
1029*57249Seric 			else
1030*57249Seric 			{
1031*57249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
1032*57249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
1033*57249Seric 						break;
1034*57249Seric 				setstat(ep->ec_code);
1035*57249Seric 			}
103610183Seric 			tv++;
103710183Seric 		}
103810183Seric 		if (**tv != CANONUSER)
10394279Seric 			syserr("buildaddr: error: no user");
104056678Seric 		buf[0] = '\0';
10414279Seric 		while (*++tv != NULL)
10424279Seric 		{
10434279Seric 			if (buf[0] != '\0')
10447005Seric 				(void) strcat(buf, " ");
10457005Seric 			(void) strcat(buf, *tv);
10464279Seric 		}
10474279Seric 		usrerr(buf);
10484279Seric 		return (NULL);
10494279Seric 	}
10504598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
10513149Seric 	{
105233725Sbostic 		if (!strcasecmp(m->m_name, *tv))
10533149Seric 			break;
10543149Seric 	}
10553149Seric 	if (m == NULL)
10564279Seric 	{
105724944Seric 		syserr("buildaddr: unknown mailer %s", *tv);
10584279Seric 		return (NULL);
10594279Seric 	}
10604598Seric 	a->q_mailer = m;
10613149Seric 
10623149Seric 	/* figure out what host (if any) */
106356678Seric 	tv++;
1064*57249Seric 	if (!bitnset(M_LOCAL, m->m_flags))
10653149Seric 	{
1066*57249Seric 		if (**tv++ != CANONHOST)
10674279Seric 		{
10683149Seric 			syserr("buildaddr: no host");
10694279Seric 			return (NULL);
10704279Seric 		}
10715704Seric 		buf[0] = '\0';
1072*57249Seric 		while (*tv != NULL && **tv != CANONUSER)
1073*57249Seric 			(void) strcat(buf, *tv++);
10745704Seric 		a->q_host = newstr(buf);
10753149Seric 	}
1076*57249Seric 	else
1077*57249Seric 		a->q_host = NULL;
10783149Seric 
10793149Seric 	/* figure out the user */
108036615Sbostic 	if (*tv == NULL || **tv != CANONUSER)
10814279Seric 	{
10823149Seric 		syserr("buildaddr: no user");
10834279Seric 		return (NULL);
10844279Seric 	}
108519040Seric 
108656678Seric 	if (m == LocalMailer && tv[1] != NULL && strcmp(tv[1], "@") == 0)
108756678Seric 	{
108856678Seric 		tv++;
108956678Seric 		a->q_flags |= QNOTREMOTE;
109056678Seric 	}
109151317Seric 
109219040Seric 	/* rewrite according recipient mailer rewriting rules */
109319040Seric 	rewrite(++tv, 2);
109456327Seric 	if (m->m_r_rwset > 0)
109556327Seric 		rewrite(tv, m->m_r_rwset);
109619040Seric 	rewrite(tv, 4);
109719040Seric 
109819040Seric 	/* save the result for the command line/RCPT argument */
109911278Seric 	cataddr(tv, buf, sizeof buf);
11003149Seric 	a->q_user = buf;
11013149Seric 
11023149Seric 	return (a);
11033149Seric }
11043188Seric /*
11054228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
11064228Seric **
11074228Seric **	Parameters:
11084228Seric **		pvp -- parameter vector to rebuild.
11094228Seric **		buf -- buffer to build the string into.
11104228Seric **		sz -- size of buf.
11114228Seric **
11124228Seric **	Returns:
11134228Seric **		none.
11144228Seric **
11154228Seric **	Side Effects:
11164228Seric **		Destroys buf.
11174228Seric */
11184228Seric 
11194228Seric cataddr(pvp, buf, sz)
11204228Seric 	char **pvp;
11214228Seric 	char *buf;
11224228Seric 	register int sz;
11234228Seric {
11244228Seric 	bool oatomtok = FALSE;
112556678Seric 	bool natomtok = FALSE;
11264228Seric 	register int i;
11274228Seric 	register char *p;
11284228Seric 
11298423Seric 	if (pvp == NULL)
11308423Seric 	{
113123109Seric 		(void) strcpy(buf, "");
11328423Seric 		return;
11338423Seric 	}
11344228Seric 	p = buf;
113511156Seric 	sz -= 2;
11364228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
11374228Seric 	{
11388078Seric 		natomtok = (toktype(**pvp) == ATM);
11394228Seric 		if (oatomtok && natomtok)
11409042Seric 			*p++ = SpaceSub;
11414228Seric 		(void) strcpy(p, *pvp);
11424228Seric 		oatomtok = natomtok;
11434228Seric 		p += i;
114411156Seric 		sz -= i + 1;
11454228Seric 		pvp++;
11464228Seric 	}
11474228Seric 	*p = '\0';
11484228Seric }
11494228Seric /*
11503188Seric **  SAMEADDR -- Determine if two addresses are the same
11513188Seric **
11523188Seric **	This is not just a straight comparison -- if the mailer doesn't
11533188Seric **	care about the host we just ignore it, etc.
11543188Seric **
11553188Seric **	Parameters:
11563188Seric **		a, b -- pointers to the internal forms to compare.
11573188Seric **
11583188Seric **	Returns:
11593188Seric **		TRUE -- they represent the same mailbox.
11603188Seric **		FALSE -- they don't.
11613188Seric **
11623188Seric **	Side Effects:
11633188Seric **		none.
11643188Seric */
11653188Seric 
11663188Seric bool
11679374Seric sameaddr(a, b)
11683188Seric 	register ADDRESS *a;
11693188Seric 	register ADDRESS *b;
11703188Seric {
11713188Seric 	/* if they don't have the same mailer, forget it */
11723188Seric 	if (a->q_mailer != b->q_mailer)
11733188Seric 		return (FALSE);
11743188Seric 
11753188Seric 	/* if the user isn't the same, we can drop out */
117656678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
11773188Seric 		return (FALSE);
11783188Seric 
11793188Seric 	/* if the mailer ignores hosts, we have succeeded! */
118010690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
11813188Seric 		return (TRUE);
11823188Seric 
11833188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
11843188Seric 	if (a->q_host == NULL || b->q_host == NULL)
11853188Seric 		return (FALSE);
118656678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
11873188Seric 		return (FALSE);
11883188Seric 
11893188Seric 	return (TRUE);
11903188Seric }
11913234Seric /*
11923234Seric **  PRINTADDR -- print address (for debugging)
11933234Seric **
11943234Seric **	Parameters:
11953234Seric **		a -- the address to print
11963234Seric **		follow -- follow the q_next chain.
11973234Seric **
11983234Seric **	Returns:
11993234Seric **		none.
12003234Seric **
12013234Seric **	Side Effects:
12023234Seric **		none.
12033234Seric */
12043234Seric 
12053234Seric printaddr(a, follow)
12063234Seric 	register ADDRESS *a;
12073234Seric 	bool follow;
12083234Seric {
12095001Seric 	bool first = TRUE;
12105001Seric 
12113234Seric 	while (a != NULL)
12123234Seric 	{
12135001Seric 		first = FALSE;
12144443Seric 		printf("%x=", a);
12154085Seric 		(void) fflush(stdout);
121640973Sbostic 		printf("%s: mailer %d (%s), host `%s', user `%s', ruser `%s'\n",
121740973Sbostic 		       a->q_paddr, a->q_mailer->m_mno, a->q_mailer->m_name,
121840973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
12198181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
12208181Seric 		       a->q_alias);
12218181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
12228181Seric 		       a->q_fullname);
12234996Seric 
12243234Seric 		if (!follow)
12253234Seric 			return;
12264996Seric 		a = a->q_next;
12273234Seric 	}
12285001Seric 	if (first)
12294443Seric 		printf("[NULL]\n");
12303234Seric }
12314317Seric 
12327682Seric /*
12337682Seric **  REMOTENAME -- return the name relative to the current mailer
12347682Seric **
12357682Seric **	Parameters:
12367682Seric **		name -- the name to translate.
12378069Seric **		m -- the mailer that we want to do rewriting relative
12388069Seric **			to.
12398069Seric **		senderaddress -- if set, uses the sender rewriting rules
12408069Seric **			rather than the recipient rewriting rules.
124110310Seric **		canonical -- if set, strip out any comment information,
124210310Seric **			etc.
12437682Seric **
12447682Seric **	Returns:
12457682Seric **		the text string representing this address relative to
12467682Seric **			the receiving mailer.
12477682Seric **
12487682Seric **	Side Effects:
12497682Seric **		none.
12507682Seric **
12517682Seric **	Warnings:
12527682Seric **		The text string returned is tucked away locally;
12537682Seric **			copy it if you intend to save it.
12547682Seric */
12557682Seric 
12567682Seric char *
125756678Seric remotename(name, m, senderaddress, canonical, e)
12587682Seric 	char *name;
125956678Seric 	struct mailer *m;
12608069Seric 	bool senderaddress;
126110310Seric 	bool canonical;
126256678Seric 	register ENVELOPE *e;
12637682Seric {
12648069Seric 	register char **pvp;
12658069Seric 	char *fancy;
126656678Seric 	extern char *macvalue();
126756678Seric 	char *oldg = macvalue('g', e);
12687682Seric 	static char buf[MAXNAME];
12697682Seric 	char lbuf[MAXNAME];
127016914Seric 	char pvpbuf[PSBUFSIZE];
127156678Seric 	extern char **prescan();
127256678Seric 	extern char *crackaddr();
12737682Seric 
12747755Seric 	if (tTd(12, 1))
12757755Seric 		printf("remotename(%s)\n", name);
12767755Seric 
127710177Seric 	/* don't do anything if we are tagging it as special */
127856327Seric 	if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0)
127910177Seric 		return (name);
128010177Seric 
12817682Seric 	/*
12828181Seric 	**  Do a heuristic crack of this name to extract any comment info.
12838181Seric 	**	This will leave the name as a comment and a $g macro.
12847889Seric 	*/
12857889Seric 
128610310Seric 	if (canonical)
128716155Seric 		fancy = "\001g";
128810310Seric 	else
128910310Seric 		fancy = crackaddr(name);
12907889Seric 
12918181Seric 	/*
12928181Seric 	**  Turn the name into canonical form.
12938181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
12948181Seric 	**	If this only resolves to "user", and the "C" flag is
12958181Seric 	**	specified in the sending mailer, then the sender's
12968181Seric 	**	domain will be appended.
12978181Seric 	*/
12988181Seric 
129916914Seric 	pvp = prescan(name, '\0', pvpbuf);
13007889Seric 	if (pvp == NULL)
13017889Seric 		return (name);
13028181Seric 	rewrite(pvp, 3);
130356678Seric 	if (e->e_fromdomain != NULL)
13048181Seric 	{
13058181Seric 		/* append from domain to this address */
13068181Seric 		register char **pxp = pvp;
13078181Seric 
13089594Seric 		/* see if there is an "@domain" in the current name */
13098181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
13108181Seric 			pxp++;
13118181Seric 		if (*pxp == NULL)
13128181Seric 		{
13139594Seric 			/* no.... append the "@domain" from the sender */
131456678Seric 			register char **qxq = e->e_fromdomain;
13158181Seric 
13169594Seric 			while ((*pxp++ = *qxq++) != NULL)
13179594Seric 				continue;
131811726Seric 			rewrite(pvp, 3);
13198181Seric 		}
13208181Seric 	}
13218181Seric 
13228181Seric 	/*
13238959Seric 	**  Do more specific rewriting.
132456678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
132556678Seric 	**		a sender address or not.
13268181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
13278181Seric 	*/
13288181Seric 
13298069Seric 	if (senderaddress)
13307755Seric 	{
133156327Seric 		rewrite(pvp, 1);
133256327Seric 		if (m->m_s_rwset > 0)
133356327Seric 			rewrite(pvp, m->m_s_rwset);
13348069Seric 	}
13358069Seric 	else
13368069Seric 	{
133756327Seric 		rewrite(pvp, 2);
133856327Seric 		if (m->m_r_rwset > 0)
133956327Seric 			rewrite(pvp, m->m_r_rwset);
13407682Seric 	}
13417682Seric 
13428181Seric 	/*
13438959Seric 	**  Do any final sanitation the address may require.
13448959Seric 	**	This will normally be used to turn internal forms
13458959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
13468959Seric 	**	may be used as a default to the above rules.
13478959Seric 	*/
13488959Seric 
13498959Seric 	rewrite(pvp, 4);
13508959Seric 
13518959Seric 	/*
13528181Seric 	**  Now restore the comment information we had at the beginning.
13538181Seric 	*/
13548181Seric 
13557682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
135656678Seric 	define('g', lbuf, e);
135756678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
135856678Seric 	define('g', oldg, e);
13597682Seric 
13607682Seric 	if (tTd(12, 1))
13617755Seric 		printf("remotename => `%s'\n", buf);
13627682Seric 	return (buf);
13637682Seric }
136451317Seric /*
136556678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
136651317Seric **
136751317Seric **	Parameters:
136856678Seric **		a -- the address to map (but just the user name part).
136956678Seric **		sendq -- the sendq in which to install any replacement
137056678Seric **			addresses.
137151317Seric **
137251317Seric **	Returns:
137351317Seric **		none.
137451317Seric */
137551317Seric 
137656678Seric maplocaluser(a, sendq, e)
137756678Seric 	register ADDRESS *a;
137856678Seric 	ADDRESS **sendq;
137956678Seric 	ENVELOPE *e;
138051317Seric {
138156678Seric 	register char **pvp;
138256678Seric 	register ADDRESS *a1 = NULL;
138356678Seric 	char pvpbuf[PSBUFSIZE];
138451317Seric 
138556678Seric 	if (tTd(29, 1))
138656678Seric 	{
138756678Seric 		printf("maplocaluser: ");
138856678Seric 		printaddr(a, FALSE);
138956678Seric 	}
139056678Seric 	pvp = prescan(a->q_user, '\0', pvpbuf);
139156678Seric 	if (pvp == NULL)
139256678Seric 		return;
139351317Seric 
139456678Seric 	rewrite(pvp, 5);
139556678Seric 	if (pvp[0] == NULL || pvp[0][0] != CANONNET)
139656678Seric 		return;
139751317Seric 
139856678Seric 	/* if non-null, mailer destination specified -- has it changed? */
139956678Seric 	a1 = buildaddr(pvp, NULL);
140056678Seric 	if (a1 == NULL || sameaddr(a, a1))
140156678Seric 		return;
140251317Seric 
140356678Seric 	/* mark old address as dead; insert new address */
140456678Seric 	a->q_flags |= QDONTSEND;
140556678Seric 	a1->q_alias = a;
140656678Seric 	allocaddr(a1, 1, NULL);
140756678Seric 	(void) recipient(a1, sendq, e);
140851317Seric }
1409