13312Seric # include "sendmail.h"
2297Seric 
3*16900Seric SCCSID(@(#)parseaddr.c	4.8		08/11/84);
4407Seric 
5297Seric /*
69888Seric **  PARSEADDR -- Parse an address
7297Seric **
8297Seric **	Parses an address and breaks it up into three parts: a
9297Seric **	net to transmit the message on, the host to transmit it
10297Seric **	to, and a user on that host.  These are loaded into an
112973Seric **	ADDRESS header with the values squirreled away if necessary.
12297Seric **	The "user" part may not be a real user; the process may
13297Seric **	just reoccur on that machine.  For example, on a machine
14297Seric **	with an arpanet connection, the address
15297Seric **		csvax.bill@berkeley
16297Seric **	will break up to a "user" of 'csvax.bill' and a host
17297Seric **	of 'berkeley' -- to be transmitted over the arpanet.
18297Seric **
19297Seric **	Parameters:
20297Seric **		addr -- the address to parse.
21297Seric **		a -- a pointer to the address descriptor buffer.
22297Seric **			If NULL, a header will be created.
23297Seric **		copyf -- determines what shall be copied:
24297Seric **			-1 -- don't copy anything.  The printname
25297Seric **				(q_paddr) is just addr, and the
26297Seric **				user & host are allocated internally
27297Seric **				to parse.
28297Seric **			0 -- copy out the parsed user & host, but
29297Seric **				don't copy the printname.
30297Seric **			+1 -- copy everything.
3111445Seric **		delim -- the character to terminate the address, passed
3211445Seric **			to prescan.
33297Seric **
34297Seric **	Returns:
35297Seric **		A pointer to the address descriptor header (`a' if
36297Seric **			`a' is non-NULL).
37297Seric **		NULL on error.
38297Seric **
39297Seric **	Side Effects:
40297Seric **		none
41297Seric */
42297Seric 
439374Seric /* following delimiters are inherent to the internal algorithms */
4416155Seric # define DELIMCHARS	"\001()<>,;\\\"\r\n"	/* word delimiters */
452091Seric 
462973Seric ADDRESS *
4711445Seric parseaddr(addr, a, copyf, delim)
48297Seric 	char *addr;
492973Seric 	register ADDRESS *a;
50297Seric 	int copyf;
5111445Seric 	char delim;
52297Seric {
533149Seric 	register char **pvp;
543149Seric 	register struct mailer *m;
553149Seric 	extern char **prescan();
563149Seric 	extern ADDRESS *buildaddr();
57297Seric 
58297Seric 	/*
59297Seric 	**  Initialize and prescan address.
60297Seric 	*/
61297Seric 
626903Seric 	CurEnv->e_to = addr;
633188Seric # ifdef DEBUG
647675Seric 	if (tTd(20, 1))
659888Seric 		printf("\n--parseaddr(%s)\n", addr);
663188Seric # endif DEBUG
673188Seric 
6811445Seric 	pvp = prescan(addr, delim);
693149Seric 	if (pvp == NULL)
70297Seric 		return (NULL);
71297Seric 
72297Seric 	/*
733149Seric 	**  Apply rewriting rules.
747889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
75297Seric 	*/
76297Seric 
778181Seric 	rewrite(pvp, 3);
784070Seric 	rewrite(pvp, 0);
79297Seric 
803149Seric 	/*
813149Seric 	**  See if we resolved to a real mailer.
823149Seric 	*/
83297Seric 
843149Seric 	if (pvp[0][0] != CANONNET)
853149Seric 	{
863149Seric 		setstat(EX_USAGE);
873149Seric 		usrerr("cannot resolve name");
883149Seric 		return (NULL);
89297Seric 	}
90297Seric 
91297Seric 	/*
923149Seric 	**  Build canonical address from pvp.
93297Seric 	*/
94297Seric 
953149Seric 	a = buildaddr(pvp, a);
964279Seric 	if (a == NULL)
974279Seric 		return (NULL);
984598Seric 	m = a->q_mailer;
99297Seric 
100297Seric 	/*
1013149Seric 	**  Make local copies of the host & user and then
1023149Seric 	**  transport them out.
103297Seric 	*/
104297Seric 
105297Seric 	if (copyf > 0)
1068078Seric 	{
1078078Seric 		extern char *DelimChar;
1088078Seric 		char savec = *DelimChar;
1098078Seric 
1108078Seric 		*DelimChar = '\0';
1112973Seric 		a->q_paddr = newstr(addr);
1128078Seric 		*DelimChar = savec;
1138078Seric 	}
114297Seric 	else
115297Seric 		a->q_paddr = addr;
1163149Seric 	if (copyf >= 0)
117297Seric 	{
1183149Seric 		if (a->q_host != NULL)
1193149Seric 			a->q_host = newstr(a->q_host);
120297Seric 		else
1213149Seric 			a->q_host = "";
1223149Seric 		if (a->q_user != a->q_paddr)
1233149Seric 			a->q_user = newstr(a->q_user);
124297Seric 	}
125297Seric 
126297Seric 	/*
12716202Seric 	**  Convert host name to lower case if requested.
12816202Seric 	**	User name will be done later.
12916202Seric 	*/
13016202Seric 
13116202Seric 	if (!bitnset(M_HST_UPPER, m->m_flags))
13216202Seric 		makelower(a->q_host);
13316202Seric 
13416202Seric 	/*
135297Seric 	**  Compute return value.
136297Seric 	*/
137297Seric 
138297Seric # ifdef DEBUG
1397675Seric 	if (tTd(20, 1))
1404443Seric 	{
1419888Seric 		printf("parseaddr-->");
1424443Seric 		printaddr(a, FALSE);
1434443Seric 	}
144297Seric # endif DEBUG
145297Seric 
146297Seric 	return (a);
147297Seric }
148297Seric /*
14916162Seric **  LOWERADDR -- map UPPER->lower case on addresses as requested.
15016162Seric **
15116162Seric **	Parameters:
15216162Seric **		a -- address to be mapped.
15316162Seric **
15416162Seric **	Returns:
15516162Seric **		none.
15616162Seric **
15716162Seric **	Side Effects:
15816162Seric **		none.
15916162Seric */
16016162Seric 
16116162Seric loweraddr(a)
16216162Seric 	register ADDRESS *a;
16316162Seric {
16416162Seric 	register MAILER *m = a->q_mailer;
16516162Seric 
16616162Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
16716162Seric 		makelower(a->q_user);
16816162Seric }
16916162Seric /*
170297Seric **  PRESCAN -- Prescan name and make it canonical
171297Seric **
1729374Seric **	Scans a name and turns it into a set of tokens.  This process
1739374Seric **	deletes blanks and comments (in parentheses).
174297Seric **
175297Seric **	This routine knows about quoted strings and angle brackets.
176297Seric **
177297Seric **	There are certain subtleties to this routine.  The one that
178297Seric **	comes to mind now is that backslashes on the ends of names
179297Seric **	are silently stripped off; this is intentional.  The problem
180297Seric **	is that some versions of sndmsg (like at LBL) set the kill
181297Seric **	character to something other than @ when reading addresses;
182297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
183297Seric **	berknet mailer.
184297Seric **
185297Seric **	Parameters:
186297Seric **		addr -- the name to chomp.
187297Seric **		delim -- the delimiter for the address, normally
188297Seric **			'\0' or ','; \0 is accepted in any case.
18915284Seric **			If '\t' then we are reading the .cf file.
190297Seric **
191297Seric **	Returns:
1923149Seric **		A pointer to a vector of tokens.
193297Seric **		NULL on error.
194297Seric **
195297Seric **	Side Effects:
1963149Seric **		none.
197297Seric */
198297Seric 
1998078Seric /* states and character types */
2008078Seric # define OPR		0	/* operator */
2018078Seric # define ATM		1	/* atom */
2028078Seric # define QST		2	/* in quoted string */
2038078Seric # define SPC		3	/* chewing up spaces */
2048078Seric # define ONE		4	/* pick up one character */
2053149Seric 
2068078Seric # define NSTATES	5	/* number of states */
2078078Seric # define TYPE		017	/* mask to select state type */
2088078Seric 
2098078Seric /* meta bits for table */
2108078Seric # define M		020	/* meta character; don't pass through */
2118078Seric # define B		040	/* cause a break */
2128078Seric # define MB		M|B	/* meta-break */
2138078Seric 
2148078Seric static short StateTab[NSTATES][NSTATES] =
2158078Seric {
2168087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2179051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2189051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2199051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2208078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2218078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2228078Seric };
2238078Seric 
2248078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2258078Seric 
2268078Seric char	*DelimChar;		/* set to point to the delimiter */
2278078Seric 
2283149Seric char **
2293149Seric prescan(addr, delim)
230297Seric 	char *addr;
231297Seric 	char delim;
232297Seric {
233297Seric 	register char *p;
2348078Seric 	register char *q;
2359346Seric 	register int c;
2363149Seric 	char **avp;
237297Seric 	bool bslashmode;
238297Seric 	int cmntcnt;
2398423Seric 	int anglecnt;
2403149Seric 	char *tok;
2418078Seric 	int state;
2428078Seric 	int newstate;
2438078Seric 	static char buf[MAXNAME+MAXATOM];
2448078Seric 	static char *av[MAXATOM+1];
24515253Seric 	extern int errno;
246297Seric 
24715253Seric 	/* make sure error messages don't have garbage on them */
24815253Seric 	errno = 0;
24915253Seric 
250297Seric 	q = buf;
2513149Seric 	bslashmode = FALSE;
2527800Seric 	cmntcnt = 0;
2538423Seric 	anglecnt = 0;
2543149Seric 	avp = av;
2558078Seric 	state = OPR;
2568078Seric 	c = NOCHAR;
2578078Seric 	p = addr;
2588078Seric # ifdef DEBUG
2598078Seric 	if (tTd(22, 45))
260297Seric 	{
2618078Seric 		printf("prescan: ");
2628078Seric 		xputs(p);
2638078Seric 		putchar('\n');
2648078Seric 	}
2658078Seric # endif DEBUG
2668078Seric 
2678078Seric 	do
2688078Seric 	{
2693149Seric 		/* read a token */
2703149Seric 		tok = q;
2718078Seric 		for (;;)
272297Seric 		{
2738078Seric 			/* store away any old lookahead character */
2748078Seric 			if (c != NOCHAR)
2758078Seric 			{
27615284Seric 				/* see if there is room */
2778078Seric 				if (q >= &buf[sizeof buf - 5])
2788078Seric 				{
2798078Seric 					usrerr("Address too long");
2808078Seric 					DelimChar = p;
2818078Seric 					return (NULL);
2828078Seric 				}
28315284Seric 
28415284Seric 				/* squirrel it away */
2858078Seric 				*q++ = c;
2868078Seric 			}
2878078Seric 
2888078Seric 			/* read a new input character */
2898078Seric 			c = *p++;
2908078Seric 			if (c == '\0')
2918078Seric 				break;
29215284Seric 			c &= ~0200;
29315284Seric 
2948078Seric # ifdef DEBUG
2958078Seric 			if (tTd(22, 101))
2968078Seric 				printf("c=%c, s=%d; ", c, state);
2978078Seric # endif DEBUG
2988078Seric 
2993149Seric 			/* chew up special characters */
3003149Seric 			*q = '\0';
3013149Seric 			if (bslashmode)
3023149Seric 			{
3033149Seric 				c |= 0200;
3043149Seric 				bslashmode = FALSE;
3053149Seric 			}
3063149Seric 			else if (c == '\\')
3073149Seric 			{
3083149Seric 				bslashmode = TRUE;
3098078Seric 				c = NOCHAR;
3103149Seric 			}
3118514Seric 			else if (state == QST)
3128514Seric 			{
3138514Seric 				/* do nothing, just avoid next clauses */
3148514Seric 			}
3158078Seric 			else if (c == '(')
3164100Seric 			{
3178078Seric 				cmntcnt++;
3188078Seric 				c = NOCHAR;
3194100Seric 			}
3208078Seric 			else if (c == ')')
3213149Seric 			{
3228078Seric 				if (cmntcnt <= 0)
3233149Seric 				{
3248078Seric 					usrerr("Unbalanced ')'");
3258078Seric 					DelimChar = p;
3268078Seric 					return (NULL);
3273149Seric 				}
3288078Seric 				else
3298078Seric 					cmntcnt--;
3308078Seric 			}
3318078Seric 			else if (cmntcnt > 0)
3328078Seric 				c = NOCHAR;
3338423Seric 			else if (c == '<')
3348423Seric 				anglecnt++;
3358423Seric 			else if (c == '>')
3368423Seric 			{
3378423Seric 				if (anglecnt <= 0)
3388423Seric 				{
3398423Seric 					usrerr("Unbalanced '>'");
3408423Seric 					DelimChar = p;
3418423Seric 					return (NULL);
3428423Seric 				}
3438423Seric 				anglecnt--;
3448423Seric 			}
34511423Seric 			else if (delim == ' ' && isspace(c))
34611423Seric 				c = ' ';
3473149Seric 
3488078Seric 			if (c == NOCHAR)
3498078Seric 				continue;
3503149Seric 
3518078Seric 			/* see if this is end of input */
35211405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
3533149Seric 				break;
3543149Seric 
3558078Seric 			newstate = StateTab[state][toktype(c)];
3568078Seric # ifdef DEBUG
3578078Seric 			if (tTd(22, 101))
3588078Seric 				printf("ns=%02o\n", newstate);
3598078Seric # endif DEBUG
3608078Seric 			state = newstate & TYPE;
3618078Seric 			if (bitset(M, newstate))
3628078Seric 				c = NOCHAR;
3638078Seric 			if (bitset(B, newstate))
3644228Seric 				break;
365297Seric 		}
3663149Seric 
3673149Seric 		/* new token */
3688078Seric 		if (tok != q)
3691378Seric 		{
3708078Seric 			*q++ = '\0';
3718078Seric # ifdef DEBUG
3728078Seric 			if (tTd(22, 36))
373297Seric 			{
3748078Seric 				printf("tok=");
3758078Seric 				xputs(tok);
3768078Seric 				putchar('\n');
377297Seric 			}
3788078Seric # endif DEBUG
3798078Seric 			if (avp >= &av[MAXATOM])
380297Seric 			{
3818078Seric 				syserr("prescan: too many tokens");
3828078Seric 				DelimChar = p;
3838078Seric 				return (NULL);
384297Seric 			}
3858078Seric 			*avp++ = tok;
386297Seric 		}
3878423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
3883149Seric 	*avp = NULL;
3898078Seric 	DelimChar = --p;
3903149Seric 	if (cmntcnt > 0)
3913149Seric 		usrerr("Unbalanced '('");
3928423Seric 	else if (anglecnt > 0)
3938423Seric 		usrerr("Unbalanced '<'");
3948078Seric 	else if (state == QST)
3953149Seric 		usrerr("Unbalanced '\"'");
3963149Seric 	else if (av[0] != NULL)
3973149Seric 		return (av);
3983149Seric 	return (NULL);
3993149Seric }
4003149Seric /*
4013149Seric **  TOKTYPE -- return token type
4023149Seric **
4033149Seric **	Parameters:
4043149Seric **		c -- the character in question.
4053149Seric **
4063149Seric **	Returns:
4073149Seric **		Its type.
4083149Seric **
4093149Seric **	Side Effects:
4103149Seric **		none.
4113149Seric */
412297Seric 
4133149Seric toktype(c)
4143149Seric 	register char c;
4153149Seric {
4163380Seric 	static char buf[50];
4173382Seric 	static bool firstime = TRUE;
4183380Seric 
4193382Seric 	if (firstime)
4203380Seric 	{
4213382Seric 		firstime = FALSE;
42216155Seric 		expand("\001o", buf, &buf[sizeof buf - 1], CurEnv);
4237005Seric 		(void) strcat(buf, DELIMCHARS);
4243380Seric 	}
4259585Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
4268078Seric 		return (ONE);
4278078Seric 	if (c == '"')
4288078Seric 		return (QST);
4294100Seric 	if (!isascii(c))
4308078Seric 		return (ATM);
4318078Seric 	if (isspace(c) || c == ')')
4328078Seric 		return (SPC);
4333380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
4348078Seric 		return (OPR);
4358078Seric 	return (ATM);
4363149Seric }
4373149Seric /*
4383149Seric **  REWRITE -- apply rewrite rules to token vector.
4393149Seric **
4404476Seric **	This routine is an ordered production system.  Each rewrite
4414476Seric **	rule has a LHS (called the pattern) and a RHS (called the
4424476Seric **	rewrite); 'rwr' points the the current rewrite rule.
4434476Seric **
4444476Seric **	For each rewrite rule, 'avp' points the address vector we
4454476Seric **	are trying to match against, and 'pvp' points to the pattern.
4468058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
4479585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
4489585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
4494476Seric **
4504476Seric **	When a match between avp & pvp does not match, we try to
4519585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
4524476Seric **	we must also back out the match in mvp.  If we reach a
4538058Seric **	MATCHANY or MATCHZANY we just extend the match and start
4548058Seric **	over again.
4554476Seric **
4564476Seric **	When we finally match, we rewrite the address vector
4574476Seric **	and try over again.
4584476Seric **
4593149Seric **	Parameters:
4603149Seric **		pvp -- pointer to token vector.
4613149Seric **
4623149Seric **	Returns:
4633149Seric **		none.
4643149Seric **
4653149Seric **	Side Effects:
4663149Seric **		pvp is modified.
4673149Seric */
4682091Seric 
4693149Seric struct match
4703149Seric {
4714468Seric 	char	**first;	/* first token matched */
4724468Seric 	char	**last;		/* last token matched */
4733149Seric };
4743149Seric 
4754468Seric # define MAXMATCH	9	/* max params per rewrite */
4763149Seric 
4773149Seric 
4784070Seric rewrite(pvp, ruleset)
4793149Seric 	char **pvp;
4804070Seric 	int ruleset;
4813149Seric {
4823149Seric 	register char *ap;		/* address pointer */
4833149Seric 	register char *rp;		/* rewrite pointer */
4843149Seric 	register char **avp;		/* address vector pointer */
4853149Seric 	register char **rvp;		/* rewrite vector pointer */
4868058Seric 	register struct match *mlp;	/* cur ptr into mlist */
4878058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
4884468Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
4893149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
4904060Seric 	extern bool sameword();
4913149Seric 
4929279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
4933149Seric 	{
4948959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
4953149Seric 		printav(pvp);
4963149Seric 	}
4978423Seric 	if (pvp == NULL)
4988423Seric 		return;
4993149Seric 
5003149Seric 	/*
5013149Seric 	**  Run through the list of rewrite rules, applying
5023149Seric 	**	any that match.
5033149Seric 	*/
5043149Seric 
5054070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
5063149Seric 	{
5074100Seric # ifdef DEBUG
5087675Seric 		if (tTd(21, 12))
509297Seric 		{
5108069Seric 			printf("-----trying rule:");
5113149Seric 			printav(rwr->r_lhs);
5123149Seric 		}
5134100Seric # endif DEBUG
5143149Seric 
5153149Seric 		/* try to match on this rule */
5164468Seric 		mlp = mlist;
5178058Seric 		rvp = rwr->r_lhs;
5188058Seric 		avp = pvp;
5198058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
5203149Seric 		{
5213149Seric 			rp = *rvp;
5228058Seric # ifdef DEBUG
5238058Seric 			if (tTd(21, 35))
5248058Seric 			{
5258069Seric 				printf("ap=");
5268058Seric 				xputs(ap);
5278069Seric 				printf(", rp=");
5288058Seric 				xputs(rp);
5298069Seric 				printf("\n");
5308058Seric 			}
5318058Seric # endif DEBUG
5323149Seric 			if (rp == NULL)
533297Seric 			{
5343149Seric 				/* end-of-pattern before end-of-address */
5358058Seric 				goto backup;
536297Seric 			}
5378058Seric 			if (ap == NULL && *rp != MATCHZANY)
5388058Seric 			{
5398058Seric 				/* end-of-input */
5408058Seric 				break;
5418058Seric 			}
5423149Seric 
5433149Seric 			switch (*rp)
5443149Seric 			{
5454060Seric 				register STAB *s;
5464060Seric 
5474060Seric 			  case MATCHCLASS:
5489585Seric 			  case MATCHNCLASS:
5499585Seric 				/* match any token in (not in) a class */
5504100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
55110690Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
5529585Seric 				{
5539585Seric 					if (*rp == MATCHCLASS)
5549585Seric 						goto backup;
5559585Seric 				}
5569585Seric 				else if (*rp == MATCHNCLASS)
5578058Seric 					goto backup;
5584468Seric 
5594476Seric 				/* explicit fall-through */
5604476Seric 
5614476Seric 			  case MATCHONE:
5624476Seric 			  case MATCHANY:
5634476Seric 				/* match exactly one token */
5648058Seric 				mlp->first = avp;
5658058Seric 				mlp->last = avp++;
5664468Seric 				mlp++;
5674060Seric 				break;
5684060Seric 
5698058Seric 			  case MATCHZANY:
5708058Seric 				/* match zero or more tokens */
5718058Seric 				mlp->first = avp;
5728058Seric 				mlp->last = avp - 1;
5738058Seric 				mlp++;
5748058Seric 				break;
5758058Seric 
5763149Seric 			  default:
5773149Seric 				/* must have exact match */
5784060Seric 				if (!sameword(rp, ap))
5798058Seric 					goto backup;
5804468Seric 				avp++;
5813149Seric 				break;
5823149Seric 			}
5833149Seric 
5843149Seric 			/* successful match on this token */
5853149Seric 			rvp++;
5863149Seric 			continue;
5873149Seric 
5888058Seric 		  backup:
5893149Seric 			/* match failed -- back up */
5903149Seric 			while (--rvp >= rwr->r_lhs)
5913149Seric 			{
5923149Seric 				rp = *rvp;
5938058Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
5944468Seric 				{
5954476Seric 					/* extend binding and continue */
5968058Seric 					avp = ++mlp[-1].last;
5978058Seric 					avp++;
5984476Seric 					rvp++;
5993149Seric 					break;
6004468Seric 				}
6014476Seric 				avp--;
6029585Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS ||
6039585Seric 				    *rp == MATCHNCLASS)
6043149Seric 				{
6054468Seric 					/* back out binding */
6064468Seric 					mlp--;
6073149Seric 				}
6083149Seric 			}
6093149Seric 
6103149Seric 			if (rvp < rwr->r_lhs)
6113149Seric 			{
6123149Seric 				/* total failure to match */
6133149Seric 				break;
6143149Seric 			}
615297Seric 		}
6163149Seric 
6173149Seric 		/*
6183149Seric 		**  See if we successfully matched
6193149Seric 		*/
6203149Seric 
6219374Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
6223149Seric 		{
6234100Seric # ifdef DEBUG
6249374Seric 			if (tTd(21, 10))
6259374Seric 				printf("----- rule fails\n");
6264100Seric # endif DEBUG
6279374Seric 			rwr = rwr->r_next;
6289374Seric 			continue;
6299374Seric 		}
6303149Seric 
6319374Seric 		rvp = rwr->r_rhs;
6329374Seric # ifdef DEBUG
6339374Seric 		if (tTd(21, 12))
6349374Seric 		{
6359374Seric 			printf("-----rule matches:");
6369374Seric 			printav(rvp);
6379374Seric 		}
6389374Seric # endif DEBUG
6399374Seric 
6409374Seric 		rp = *rvp;
6419374Seric 		if (*rp == CANONUSER)
6429374Seric 		{
6439374Seric 			rvp++;
6449374Seric 			rwr = rwr->r_next;
6459374Seric 		}
6469374Seric 		else if (*rp == CANONHOST)
6479374Seric 		{
6489374Seric 			rvp++;
6499374Seric 			rwr = NULL;
6509374Seric 		}
6519374Seric 		else if (*rp == CANONNET)
6529374Seric 			rwr = NULL;
6539374Seric 
6549374Seric 		/* substitute */
6559374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
6569374Seric 		{
6579374Seric 			register struct match *m;
6589374Seric 			register char **pp;
6599374Seric 
6608058Seric 			rp = *rvp;
6619374Seric 			if (*rp != MATCHREPL)
6628058Seric 			{
6639374Seric 				if (avp >= &npvp[MAXATOM])
6649374Seric 				{
6659374Seric 					syserr("rewrite: expansion too long");
6669374Seric 					return;
6679374Seric 				}
6689374Seric 				*avp++ = rp;
6699374Seric 				continue;
6708069Seric 			}
6718058Seric 
6729374Seric 			/* substitute from LHS */
6739374Seric 			m = &mlist[rp[1] - '1'];
67416889Seric 			if (m >= mlp)
67516889Seric 			{
67616889Seric 				syserr("rewrite: ruleset %d: replacement out of bounds", ruleset);
67716889Seric 				return;
67816889Seric 			}
6799374Seric # ifdef DEBUG
6809374Seric 			if (tTd(21, 15))
6813149Seric 			{
6829374Seric 				printf("$%c:", rp[1]);
6839374Seric 				pp = m->first;
6849374Seric 				while (pp <= m->last)
6853149Seric 				{
6869374Seric 					printf(" %x=\"", *pp);
6879374Seric 					(void) fflush(stdout);
6889374Seric 					printf("%s\"", *pp++);
6893149Seric 				}
6909374Seric 				printf("\n");
6913149Seric 			}
6928226Seric # endif DEBUG
6939374Seric 			pp = m->first;
6949374Seric 			while (pp <= m->last)
6958226Seric 			{
6969374Seric 				if (avp >= &npvp[MAXATOM])
69716889Seric 				{
69816889Seric 					syserr("rewrite: expansion too long");
69916889Seric 					return;
70016889Seric 				}
7019374Seric 				*avp++ = *pp++;
7028226Seric 			}
7039374Seric 		}
7049374Seric 		*avp++ = NULL;
70516889Seric 		if (**npvp == CALLSUBR)
7069374Seric 		{
70716889Seric 			bcopy((char *) &npvp[2], (char *) pvp,
708*16900Seric 				(int) (avp - npvp - 2) * sizeof *avp);
70916889Seric # ifdef DEBUG
71016889Seric 			if (tTd(21, 3))
71116889Seric 				printf("-----callsubr %s\n", npvp[1]);
71216889Seric # endif DEBUG
71316889Seric 			rewrite(pvp, atoi(npvp[1]));
7143149Seric 		}
7153149Seric 		else
7163149Seric 		{
71716889Seric 			bcopy((char *) npvp, (char *) pvp,
718*16900Seric 				(int) (avp - npvp) * sizeof *avp);
7199374Seric 		}
7204100Seric # ifdef DEBUG
7219374Seric 		if (tTd(21, 4))
7229374Seric 		{
7239374Seric 			printf("rewritten as:");
7249374Seric 			printav(pvp);
7259374Seric 		}
7264100Seric # endif DEBUG
727297Seric 	}
7288069Seric 
7299279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
7308069Seric 	{
7318959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
7328069Seric 		printav(pvp);
7338069Seric 	}
7343149Seric }
7353149Seric /*
7363149Seric **  BUILDADDR -- build address from token vector.
7373149Seric **
7383149Seric **	Parameters:
7393149Seric **		tv -- token vector.
7403149Seric **		a -- pointer to address descriptor to fill.
7413149Seric **			If NULL, one will be allocated.
7423149Seric **
7433149Seric **	Returns:
7444279Seric **		NULL if there was an error.
7454279Seric **		'a' otherwise.
7463149Seric **
7473149Seric **	Side Effects:
7483149Seric **		fills in 'a'
7493149Seric */
7503149Seric 
7513149Seric ADDRESS *
7523149Seric buildaddr(tv, a)
7533149Seric 	register char **tv;
7543149Seric 	register ADDRESS *a;
7553149Seric {
7563149Seric 	static char buf[MAXNAME];
7573149Seric 	struct mailer **mp;
7583149Seric 	register struct mailer *m;
7594635Seric 	extern bool sameword();
7603149Seric 
7613149Seric 	if (a == NULL)
7623149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
76316889Seric 	bzero((char *) a, sizeof *a);
7643149Seric 
7653149Seric 	/* figure out what net/mailer to use */
7663149Seric 	if (**tv != CANONNET)
7674279Seric 	{
7683149Seric 		syserr("buildaddr: no net");
7694279Seric 		return (NULL);
7704279Seric 	}
7713149Seric 	tv++;
7724635Seric 	if (sameword(*tv, "error"))
7734279Seric 	{
77410183Seric 		if (**++tv == CANONHOST)
77510183Seric 		{
77610183Seric 			setstat(atoi(*++tv));
77710183Seric 			tv++;
77810183Seric 		}
77910183Seric 		if (**tv != CANONUSER)
7804279Seric 			syserr("buildaddr: error: no user");
7814279Seric 		buf[0] = '\0';
7824279Seric 		while (*++tv != NULL)
7834279Seric 		{
7844279Seric 			if (buf[0] != '\0')
7857005Seric 				(void) strcat(buf, " ");
7867005Seric 			(void) strcat(buf, *tv);
7874279Seric 		}
7884279Seric 		usrerr(buf);
7894279Seric 		return (NULL);
7904279Seric 	}
7914598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
7923149Seric 	{
7934635Seric 		if (sameword(m->m_name, *tv))
7943149Seric 			break;
7953149Seric 	}
7963149Seric 	if (m == NULL)
7974279Seric 	{
7983149Seric 		syserr("buildaddr: unknown net %s", *tv);
7994279Seric 		return (NULL);
8004279Seric 	}
8014598Seric 	a->q_mailer = m;
8023149Seric 
8033149Seric 	/* figure out what host (if any) */
8043149Seric 	tv++;
80510690Seric 	if (!bitnset(M_LOCAL, m->m_flags))
8063149Seric 	{
8075704Seric 		if (**tv++ != CANONHOST)
8084279Seric 		{
8093149Seric 			syserr("buildaddr: no host");
8104279Seric 			return (NULL);
8114279Seric 		}
8125704Seric 		buf[0] = '\0';
8135704Seric 		while (*tv != NULL && **tv != CANONUSER)
8147005Seric 			(void) strcat(buf, *tv++);
8155704Seric 		a->q_host = newstr(buf);
8163149Seric 	}
8173149Seric 	else
8183149Seric 		a->q_host = NULL;
8193149Seric 
8203149Seric 	/* figure out the user */
8213149Seric 	if (**tv != CANONUSER)
8224279Seric 	{
8233149Seric 		syserr("buildaddr: no user");
8244279Seric 		return (NULL);
8254279Seric 	}
82611278Seric 	rewrite(++tv, 4);
82711278Seric 	cataddr(tv, buf, sizeof buf);
8283149Seric 	a->q_user = buf;
8293149Seric 
8303149Seric 	return (a);
8313149Seric }
8323188Seric /*
8334228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
8344228Seric **
8354228Seric **	Parameters:
8364228Seric **		pvp -- parameter vector to rebuild.
8374228Seric **		buf -- buffer to build the string into.
8384228Seric **		sz -- size of buf.
8394228Seric **
8404228Seric **	Returns:
8414228Seric **		none.
8424228Seric **
8434228Seric **	Side Effects:
8444228Seric **		Destroys buf.
8454228Seric */
8464228Seric 
8474228Seric cataddr(pvp, buf, sz)
8484228Seric 	char **pvp;
8494228Seric 	char *buf;
8504228Seric 	register int sz;
8514228Seric {
8524228Seric 	bool oatomtok = FALSE;
8534228Seric 	bool natomtok = FALSE;
8544228Seric 	register int i;
8554228Seric 	register char *p;
8564228Seric 
8578423Seric 	if (pvp == NULL)
8588423Seric 	{
8598423Seric 		strcpy(buf, "");
8608423Seric 		return;
8618423Seric 	}
8624228Seric 	p = buf;
86311156Seric 	sz -= 2;
8644228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
8654228Seric 	{
8668078Seric 		natomtok = (toktype(**pvp) == ATM);
8674228Seric 		if (oatomtok && natomtok)
8689042Seric 			*p++ = SpaceSub;
8694228Seric 		(void) strcpy(p, *pvp);
8704228Seric 		oatomtok = natomtok;
8714228Seric 		p += i;
87211156Seric 		sz -= i + 1;
8734228Seric 		pvp++;
8744228Seric 	}
8754228Seric 	*p = '\0';
8764228Seric }
8774228Seric /*
8783188Seric **  SAMEADDR -- Determine if two addresses are the same
8793188Seric **
8803188Seric **	This is not just a straight comparison -- if the mailer doesn't
8813188Seric **	care about the host we just ignore it, etc.
8823188Seric **
8833188Seric **	Parameters:
8843188Seric **		a, b -- pointers to the internal forms to compare.
8853188Seric **
8863188Seric **	Returns:
8873188Seric **		TRUE -- they represent the same mailbox.
8883188Seric **		FALSE -- they don't.
8893188Seric **
8903188Seric **	Side Effects:
8913188Seric **		none.
8923188Seric */
8933188Seric 
8943188Seric bool
8959374Seric sameaddr(a, b)
8963188Seric 	register ADDRESS *a;
8973188Seric 	register ADDRESS *b;
8983188Seric {
8993188Seric 	/* if they don't have the same mailer, forget it */
9003188Seric 	if (a->q_mailer != b->q_mailer)
9013188Seric 		return (FALSE);
9023188Seric 
9033188Seric 	/* if the user isn't the same, we can drop out */
9049374Seric 	if (strcmp(a->q_user, b->q_user) != 0)
9053188Seric 		return (FALSE);
9063188Seric 
9073188Seric 	/* if the mailer ignores hosts, we have succeeded! */
90810690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
9093188Seric 		return (TRUE);
9103188Seric 
9113188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
9123188Seric 	if (a->q_host == NULL || b->q_host == NULL)
9133188Seric 		return (FALSE);
9143188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
9153188Seric 		return (FALSE);
9163188Seric 
9173188Seric 	return (TRUE);
9183188Seric }
9193234Seric /*
9203234Seric **  PRINTADDR -- print address (for debugging)
9213234Seric **
9223234Seric **	Parameters:
9233234Seric **		a -- the address to print
9243234Seric **		follow -- follow the q_next chain.
9253234Seric **
9263234Seric **	Returns:
9273234Seric **		none.
9283234Seric **
9293234Seric **	Side Effects:
9303234Seric **		none.
9313234Seric */
9323234Seric 
9334317Seric # ifdef DEBUG
9344317Seric 
9353234Seric printaddr(a, follow)
9363234Seric 	register ADDRESS *a;
9373234Seric 	bool follow;
9383234Seric {
9395001Seric 	bool first = TRUE;
9405001Seric 
9413234Seric 	while (a != NULL)
9423234Seric 	{
9435001Seric 		first = FALSE;
9444443Seric 		printf("%x=", a);
9454085Seric 		(void) fflush(stdout);
9463234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
9478181Seric 		       a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host,
9488181Seric 		       a->q_user);
9498181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
9508181Seric 		       a->q_alias);
9518181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
9528181Seric 		       a->q_fullname);
9534996Seric 
9543234Seric 		if (!follow)
9553234Seric 			return;
9564996Seric 		a = a->q_next;
9573234Seric 	}
9585001Seric 	if (first)
9594443Seric 		printf("[NULL]\n");
9603234Seric }
9614317Seric 
9624317Seric # endif DEBUG
9637682Seric /*
9647682Seric **  REMOTENAME -- return the name relative to the current mailer
9657682Seric **
9667682Seric **	Parameters:
9677682Seric **		name -- the name to translate.
9688069Seric **		m -- the mailer that we want to do rewriting relative
9698069Seric **			to.
9708069Seric **		senderaddress -- if set, uses the sender rewriting rules
9718069Seric **			rather than the recipient rewriting rules.
97210310Seric **		canonical -- if set, strip out any comment information,
97310310Seric **			etc.
9747682Seric **
9757682Seric **	Returns:
9767682Seric **		the text string representing this address relative to
9777682Seric **			the receiving mailer.
9787682Seric **
9797682Seric **	Side Effects:
9807682Seric **		none.
9817682Seric **
9827682Seric **	Warnings:
9837682Seric **		The text string returned is tucked away locally;
9847682Seric **			copy it if you intend to save it.
9857682Seric */
9867682Seric 
9877682Seric char *
98810310Seric remotename(name, m, senderaddress, canonical)
9897682Seric 	char *name;
9907682Seric 	struct mailer *m;
9918069Seric 	bool senderaddress;
99210310Seric 	bool canonical;
9937682Seric {
9948069Seric 	register char **pvp;
9958069Seric 	char *fancy;
99615284Seric 	register char *p;
9978069Seric 	extern char *macvalue();
9988181Seric 	char *oldg = macvalue('g', CurEnv);
9997682Seric 	static char buf[MAXNAME];
10007682Seric 	char lbuf[MAXNAME];
10017682Seric 	extern char **prescan();
10027889Seric 	extern char *crackaddr();
10037682Seric 
10047755Seric # ifdef DEBUG
10057755Seric 	if (tTd(12, 1))
10067755Seric 		printf("remotename(%s)\n", name);
10077755Seric # endif DEBUG
10087755Seric 
100910177Seric 	/* don't do anything if we are tagging it as special */
101010177Seric 	if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0)
101110177Seric 		return (name);
101210177Seric 
10137682Seric 	/*
10148181Seric 	**  Do a heuristic crack of this name to extract any comment info.
10158181Seric 	**	This will leave the name as a comment and a $g macro.
10167889Seric 	*/
10177889Seric 
101810310Seric 	if (canonical)
101916155Seric 		fancy = "\001g";
102010310Seric 	else
102110310Seric 		fancy = crackaddr(name);
10227889Seric 
10238181Seric 	/*
10248181Seric 	**  Turn the name into canonical form.
10258181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
10268181Seric 	**	If this only resolves to "user", and the "C" flag is
10278181Seric 	**	specified in the sending mailer, then the sender's
10288181Seric 	**	domain will be appended.
10298181Seric 	*/
10308181Seric 
10317889Seric 	pvp = prescan(name, '\0');
10327889Seric 	if (pvp == NULL)
10337889Seric 		return (name);
10348181Seric 	rewrite(pvp, 3);
10358181Seric 	if (CurEnv->e_fromdomain != NULL)
10368181Seric 	{
10378181Seric 		/* append from domain to this address */
10388181Seric 		register char **pxp = pvp;
10398181Seric 
10409594Seric 		/* see if there is an "@domain" in the current name */
10418181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
10428181Seric 			pxp++;
10438181Seric 		if (*pxp == NULL)
10448181Seric 		{
10459594Seric 			/* no.... append the "@domain" from the sender */
10468181Seric 			register char **qxq = CurEnv->e_fromdomain;
10478181Seric 
10489594Seric 			while ((*pxp++ = *qxq++) != NULL)
10499594Seric 				continue;
105011726Seric 			rewrite(pvp, 3);
10518181Seric 		}
10528181Seric 	}
10538181Seric 
10548181Seric 	/*
10558959Seric 	**  Do more specific rewriting.
10568181Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
10578181Seric 	**		a sender address or not.
10588181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
10598181Seric 	*/
10608181Seric 
10618069Seric 	if (senderaddress)
10627755Seric 	{
10637889Seric 		rewrite(pvp, 1);
10648069Seric 		if (m->m_s_rwset > 0)
10658069Seric 			rewrite(pvp, m->m_s_rwset);
10668069Seric 	}
10678069Seric 	else
10688069Seric 	{
10697889Seric 		rewrite(pvp, 2);
10708069Seric 		if (m->m_r_rwset > 0)
10718069Seric 			rewrite(pvp, m->m_r_rwset);
10727682Seric 	}
10737682Seric 
10748181Seric 	/*
10758959Seric 	**  Do any final sanitation the address may require.
10768959Seric 	**	This will normally be used to turn internal forms
10778959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
10788959Seric 	**	may be used as a default to the above rules.
10798959Seric 	*/
10808959Seric 
10818959Seric 	rewrite(pvp, 4);
10828959Seric 
10838959Seric 	/*
10848181Seric 	**  Now restore the comment information we had at the beginning.
10858181Seric 	*/
10868181Seric 
10877682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
10889374Seric 	define('g', lbuf, CurEnv);
10897889Seric 	expand(fancy, buf, &buf[sizeof buf - 1], CurEnv);
10909374Seric 	define('g', oldg, CurEnv);
10917682Seric 
10927682Seric # ifdef DEBUG
10937682Seric 	if (tTd(12, 1))
10947755Seric 		printf("remotename => `%s'\n", buf);
10957682Seric # endif DEBUG
10967682Seric 	return (buf);
10977682Seric }
1098