13312Seric # include "sendmail.h"
2297Seric 
3*17348Seric SCCSID(@(#)parseaddr.c	4.13		11/13/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;
5516914Seric 	char pvpbuf[PSBUFSIZE];
563149Seric 	extern char **prescan();
573149Seric 	extern ADDRESS *buildaddr();
58297Seric 
59297Seric 	/*
60297Seric 	**  Initialize and prescan address.
61297Seric 	*/
62297Seric 
636903Seric 	CurEnv->e_to = addr;
643188Seric # ifdef DEBUG
657675Seric 	if (tTd(20, 1))
669888Seric 		printf("\n--parseaddr(%s)\n", addr);
673188Seric # endif DEBUG
683188Seric 
6916914Seric 	pvp = prescan(addr, delim, pvpbuf);
703149Seric 	if (pvp == NULL)
71297Seric 		return (NULL);
72297Seric 
73297Seric 	/*
743149Seric 	**  Apply rewriting rules.
757889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
76297Seric 	*/
77297Seric 
788181Seric 	rewrite(pvp, 3);
794070Seric 	rewrite(pvp, 0);
80297Seric 
813149Seric 	/*
823149Seric 	**  See if we resolved to a real mailer.
833149Seric 	*/
84297Seric 
853149Seric 	if (pvp[0][0] != CANONNET)
863149Seric 	{
873149Seric 		setstat(EX_USAGE);
883149Seric 		usrerr("cannot resolve name");
893149Seric 		return (NULL);
90297Seric 	}
91297Seric 
92297Seric 	/*
933149Seric 	**  Build canonical address from pvp.
94297Seric 	*/
95297Seric 
963149Seric 	a = buildaddr(pvp, a);
974279Seric 	if (a == NULL)
984279Seric 		return (NULL);
994598Seric 	m = a->q_mailer;
100297Seric 
101297Seric 	/*
1023149Seric 	**  Make local copies of the host & user and then
1033149Seric 	**  transport them out.
104297Seric 	*/
105297Seric 
106297Seric 	if (copyf > 0)
1078078Seric 	{
1088078Seric 		extern char *DelimChar;
1098078Seric 		char savec = *DelimChar;
1108078Seric 
1118078Seric 		*DelimChar = '\0';
1122973Seric 		a->q_paddr = newstr(addr);
1138078Seric 		*DelimChar = savec;
1148078Seric 	}
115297Seric 	else
116297Seric 		a->q_paddr = addr;
1173149Seric 	if (copyf >= 0)
118297Seric 	{
1193149Seric 		if (a->q_host != NULL)
1203149Seric 			a->q_host = newstr(a->q_host);
121297Seric 		else
1223149Seric 			a->q_host = "";
1233149Seric 		if (a->q_user != a->q_paddr)
1243149Seric 			a->q_user = newstr(a->q_user);
125297Seric 	}
126297Seric 
127297Seric 	/*
12816202Seric 	**  Convert host name to lower case if requested.
12916202Seric 	**	User name will be done later.
13016202Seric 	*/
13116202Seric 
13216202Seric 	if (!bitnset(M_HST_UPPER, m->m_flags))
13316202Seric 		makelower(a->q_host);
13416202Seric 
13516202Seric 	/*
136297Seric 	**  Compute return value.
137297Seric 	*/
138297Seric 
139297Seric # ifdef DEBUG
1407675Seric 	if (tTd(20, 1))
1414443Seric 	{
1429888Seric 		printf("parseaddr-->");
1434443Seric 		printaddr(a, FALSE);
1444443Seric 	}
145297Seric # endif DEBUG
146297Seric 
147297Seric 	return (a);
148297Seric }
149297Seric /*
15016162Seric **  LOWERADDR -- map UPPER->lower case on addresses as requested.
15116162Seric **
15216162Seric **	Parameters:
15316162Seric **		a -- address to be mapped.
15416162Seric **
15516162Seric **	Returns:
15616162Seric **		none.
15716162Seric **
15816162Seric **	Side Effects:
15916162Seric **		none.
16016162Seric */
16116162Seric 
16216162Seric loweraddr(a)
16316162Seric 	register ADDRESS *a;
16416162Seric {
16516162Seric 	register MAILER *m = a->q_mailer;
16616162Seric 
16716162Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
16816162Seric 		makelower(a->q_user);
16916162Seric }
17016162Seric /*
171297Seric **  PRESCAN -- Prescan name and make it canonical
172297Seric **
1739374Seric **	Scans a name and turns it into a set of tokens.  This process
1749374Seric **	deletes blanks and comments (in parentheses).
175297Seric **
176297Seric **	This routine knows about quoted strings and angle brackets.
177297Seric **
178297Seric **	There are certain subtleties to this routine.  The one that
179297Seric **	comes to mind now is that backslashes on the ends of names
180297Seric **	are silently stripped off; this is intentional.  The problem
181297Seric **	is that some versions of sndmsg (like at LBL) set the kill
182297Seric **	character to something other than @ when reading addresses;
183297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
184297Seric **	berknet mailer.
185297Seric **
186297Seric **	Parameters:
187297Seric **		addr -- the name to chomp.
188297Seric **		delim -- the delimiter for the address, normally
189297Seric **			'\0' or ','; \0 is accepted in any case.
19015284Seric **			If '\t' then we are reading the .cf file.
19116914Seric **		pvpbuf -- place to put the saved text -- note that
19216914Seric **			the pointers are static.
193297Seric **
194297Seric **	Returns:
1953149Seric **		A pointer to a vector of tokens.
196297Seric **		NULL on error.
197297Seric **
198297Seric **	Side Effects:
1993149Seric **		none.
200297Seric */
201297Seric 
2028078Seric /* states and character types */
2038078Seric # define OPR		0	/* operator */
2048078Seric # define ATM		1	/* atom */
2058078Seric # define QST		2	/* in quoted string */
2068078Seric # define SPC		3	/* chewing up spaces */
2078078Seric # define ONE		4	/* pick up one character */
2083149Seric 
2098078Seric # define NSTATES	5	/* number of states */
2108078Seric # define TYPE		017	/* mask to select state type */
2118078Seric 
2128078Seric /* meta bits for table */
2138078Seric # define M		020	/* meta character; don't pass through */
2148078Seric # define B		040	/* cause a break */
2158078Seric # define MB		M|B	/* meta-break */
2168078Seric 
2178078Seric static short StateTab[NSTATES][NSTATES] =
2188078Seric {
2198087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2209051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2219051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2229051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2238078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2248078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2258078Seric };
2268078Seric 
2278078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2288078Seric 
2298078Seric char	*DelimChar;		/* set to point to the delimiter */
2308078Seric 
2313149Seric char **
23216914Seric prescan(addr, delim, pvpbuf)
233297Seric 	char *addr;
234297Seric 	char delim;
23516914Seric 	char pvpbuf[];
236297Seric {
237297Seric 	register char *p;
2388078Seric 	register char *q;
2399346Seric 	register int c;
2403149Seric 	char **avp;
241297Seric 	bool bslashmode;
242297Seric 	int cmntcnt;
2438423Seric 	int anglecnt;
2443149Seric 	char *tok;
2458078Seric 	int state;
2468078Seric 	int newstate;
2478078Seric 	static char *av[MAXATOM+1];
24815253Seric 	extern int errno;
249297Seric 
25015253Seric 	/* make sure error messages don't have garbage on them */
25115253Seric 	errno = 0;
25215253Seric 
25316914Seric 	q = pvpbuf;
2543149Seric 	bslashmode = FALSE;
2557800Seric 	cmntcnt = 0;
2568423Seric 	anglecnt = 0;
2573149Seric 	avp = av;
2588078Seric 	state = OPR;
2598078Seric 	c = NOCHAR;
2608078Seric 	p = addr;
2618078Seric # ifdef DEBUG
2628078Seric 	if (tTd(22, 45))
263297Seric 	{
2648078Seric 		printf("prescan: ");
2658078Seric 		xputs(p);
2668078Seric 		putchar('\n');
2678078Seric 	}
2688078Seric # endif DEBUG
2698078Seric 
2708078Seric 	do
2718078Seric 	{
2723149Seric 		/* read a token */
2733149Seric 		tok = q;
2748078Seric 		for (;;)
275297Seric 		{
2768078Seric 			/* store away any old lookahead character */
2778078Seric 			if (c != NOCHAR)
2788078Seric 			{
27915284Seric 				/* see if there is room */
28016914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
2818078Seric 				{
2828078Seric 					usrerr("Address too long");
2838078Seric 					DelimChar = p;
2848078Seric 					return (NULL);
2858078Seric 				}
28615284Seric 
28715284Seric 				/* squirrel it away */
2888078Seric 				*q++ = c;
2898078Seric 			}
2908078Seric 
2918078Seric 			/* read a new input character */
2928078Seric 			c = *p++;
2938078Seric 			if (c == '\0')
2948078Seric 				break;
29515284Seric 			c &= ~0200;
29615284Seric 
2978078Seric # ifdef DEBUG
2988078Seric 			if (tTd(22, 101))
2998078Seric 				printf("c=%c, s=%d; ", c, state);
3008078Seric # endif DEBUG
3018078Seric 
3023149Seric 			/* chew up special characters */
3033149Seric 			*q = '\0';
3043149Seric 			if (bslashmode)
3053149Seric 			{
3063149Seric 				c |= 0200;
3073149Seric 				bslashmode = FALSE;
3083149Seric 			}
3093149Seric 			else if (c == '\\')
3103149Seric 			{
3113149Seric 				bslashmode = TRUE;
3128078Seric 				c = NOCHAR;
3133149Seric 			}
3148514Seric 			else if (state == QST)
3158514Seric 			{
3168514Seric 				/* do nothing, just avoid next clauses */
3178514Seric 			}
3188078Seric 			else if (c == '(')
3194100Seric 			{
3208078Seric 				cmntcnt++;
3218078Seric 				c = NOCHAR;
3224100Seric 			}
3238078Seric 			else if (c == ')')
3243149Seric 			{
3258078Seric 				if (cmntcnt <= 0)
3263149Seric 				{
3278078Seric 					usrerr("Unbalanced ')'");
3288078Seric 					DelimChar = p;
3298078Seric 					return (NULL);
3303149Seric 				}
3318078Seric 				else
3328078Seric 					cmntcnt--;
3338078Seric 			}
3348078Seric 			else if (cmntcnt > 0)
3358078Seric 				c = NOCHAR;
3368423Seric 			else if (c == '<')
3378423Seric 				anglecnt++;
3388423Seric 			else if (c == '>')
3398423Seric 			{
3408423Seric 				if (anglecnt <= 0)
3418423Seric 				{
3428423Seric 					usrerr("Unbalanced '>'");
3438423Seric 					DelimChar = p;
3448423Seric 					return (NULL);
3458423Seric 				}
3468423Seric 				anglecnt--;
3478423Seric 			}
34811423Seric 			else if (delim == ' ' && isspace(c))
34911423Seric 				c = ' ';
3503149Seric 
3518078Seric 			if (c == NOCHAR)
3528078Seric 				continue;
3533149Seric 
3548078Seric 			/* see if this is end of input */
35511405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
3563149Seric 				break;
3573149Seric 
3588078Seric 			newstate = StateTab[state][toktype(c)];
3598078Seric # ifdef DEBUG
3608078Seric 			if (tTd(22, 101))
3618078Seric 				printf("ns=%02o\n", newstate);
3628078Seric # endif DEBUG
3638078Seric 			state = newstate & TYPE;
3648078Seric 			if (bitset(M, newstate))
3658078Seric 				c = NOCHAR;
3668078Seric 			if (bitset(B, newstate))
3674228Seric 				break;
368297Seric 		}
3693149Seric 
3703149Seric 		/* new token */
3718078Seric 		if (tok != q)
3721378Seric 		{
3738078Seric 			*q++ = '\0';
3748078Seric # ifdef DEBUG
3758078Seric 			if (tTd(22, 36))
376297Seric 			{
3778078Seric 				printf("tok=");
3788078Seric 				xputs(tok);
3798078Seric 				putchar('\n');
380297Seric 			}
3818078Seric # endif DEBUG
3828078Seric 			if (avp >= &av[MAXATOM])
383297Seric 			{
3848078Seric 				syserr("prescan: too many tokens");
3858078Seric 				DelimChar = p;
3868078Seric 				return (NULL);
387297Seric 			}
3888078Seric 			*avp++ = tok;
389297Seric 		}
3908423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
3913149Seric 	*avp = NULL;
3928078Seric 	DelimChar = --p;
3933149Seric 	if (cmntcnt > 0)
3943149Seric 		usrerr("Unbalanced '('");
3958423Seric 	else if (anglecnt > 0)
3968423Seric 		usrerr("Unbalanced '<'");
3978078Seric 	else if (state == QST)
3983149Seric 		usrerr("Unbalanced '\"'");
3993149Seric 	else if (av[0] != NULL)
4003149Seric 		return (av);
4013149Seric 	return (NULL);
4023149Seric }
4033149Seric /*
4043149Seric **  TOKTYPE -- return token type
4053149Seric **
4063149Seric **	Parameters:
4073149Seric **		c -- the character in question.
4083149Seric **
4093149Seric **	Returns:
4103149Seric **		Its type.
4113149Seric **
4123149Seric **	Side Effects:
4133149Seric **		none.
4143149Seric */
415297Seric 
4163149Seric toktype(c)
4173149Seric 	register char c;
4183149Seric {
4193380Seric 	static char buf[50];
4203382Seric 	static bool firstime = TRUE;
4213380Seric 
4223382Seric 	if (firstime)
4233380Seric 	{
4243382Seric 		firstime = FALSE;
42516155Seric 		expand("\001o", buf, &buf[sizeof buf - 1], CurEnv);
4267005Seric 		(void) strcat(buf, DELIMCHARS);
4273380Seric 	}
4289585Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
4298078Seric 		return (ONE);
4308078Seric 	if (c == '"')
4318078Seric 		return (QST);
4324100Seric 	if (!isascii(c))
4338078Seric 		return (ATM);
4348078Seric 	if (isspace(c) || c == ')')
4358078Seric 		return (SPC);
4363380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
4378078Seric 		return (OPR);
4388078Seric 	return (ATM);
4393149Seric }
4403149Seric /*
4413149Seric **  REWRITE -- apply rewrite rules to token vector.
4423149Seric **
4434476Seric **	This routine is an ordered production system.  Each rewrite
4444476Seric **	rule has a LHS (called the pattern) and a RHS (called the
4454476Seric **	rewrite); 'rwr' points the the current rewrite rule.
4464476Seric **
4474476Seric **	For each rewrite rule, 'avp' points the address vector we
4484476Seric **	are trying to match against, and 'pvp' points to the pattern.
4498058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
4509585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
4519585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
4524476Seric **
4534476Seric **	When a match between avp & pvp does not match, we try to
4549585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
4554476Seric **	we must also back out the match in mvp.  If we reach a
4568058Seric **	MATCHANY or MATCHZANY we just extend the match and start
4578058Seric **	over again.
4584476Seric **
4594476Seric **	When we finally match, we rewrite the address vector
4604476Seric **	and try over again.
4614476Seric **
4623149Seric **	Parameters:
4633149Seric **		pvp -- pointer to token vector.
4643149Seric **
4653149Seric **	Returns:
4663149Seric **		none.
4673149Seric **
4683149Seric **	Side Effects:
4693149Seric **		pvp is modified.
4703149Seric */
4712091Seric 
4723149Seric struct match
4733149Seric {
4744468Seric 	char	**first;	/* first token matched */
4754468Seric 	char	**last;		/* last token matched */
4763149Seric };
4773149Seric 
4784468Seric # define MAXMATCH	9	/* max params per rewrite */
4793149Seric 
4803149Seric 
4814070Seric rewrite(pvp, ruleset)
4823149Seric 	char **pvp;
4834070Seric 	int ruleset;
4843149Seric {
4853149Seric 	register char *ap;		/* address pointer */
4863149Seric 	register char *rp;		/* rewrite pointer */
4873149Seric 	register char **avp;		/* address vector pointer */
4883149Seric 	register char **rvp;		/* rewrite vector pointer */
4898058Seric 	register struct match *mlp;	/* cur ptr into mlist */
4908058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
4914468Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
4923149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
4934060Seric 	extern bool sameword();
4943149Seric 
4959279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
4963149Seric 	{
4978959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
4983149Seric 		printav(pvp);
4993149Seric 	}
5008423Seric 	if (pvp == NULL)
5018423Seric 		return;
5023149Seric 
5033149Seric 	/*
5043149Seric 	**  Run through the list of rewrite rules, applying
5053149Seric 	**	any that match.
5063149Seric 	*/
5073149Seric 
5084070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
5093149Seric 	{
5104100Seric # ifdef DEBUG
5117675Seric 		if (tTd(21, 12))
512297Seric 		{
5138069Seric 			printf("-----trying rule:");
5143149Seric 			printav(rwr->r_lhs);
5153149Seric 		}
5164100Seric # endif DEBUG
5173149Seric 
5183149Seric 		/* try to match on this rule */
5194468Seric 		mlp = mlist;
5208058Seric 		rvp = rwr->r_lhs;
5218058Seric 		avp = pvp;
5228058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
5233149Seric 		{
5243149Seric 			rp = *rvp;
5258058Seric # ifdef DEBUG
5268058Seric 			if (tTd(21, 35))
5278058Seric 			{
5288069Seric 				printf("ap=");
5298058Seric 				xputs(ap);
5308069Seric 				printf(", rp=");
5318058Seric 				xputs(rp);
5328069Seric 				printf("\n");
5338058Seric 			}
5348058Seric # endif DEBUG
5353149Seric 			if (rp == NULL)
536297Seric 			{
5373149Seric 				/* end-of-pattern before end-of-address */
5388058Seric 				goto backup;
539297Seric 			}
5408058Seric 			if (ap == NULL && *rp != MATCHZANY)
5418058Seric 			{
5428058Seric 				/* end-of-input */
5438058Seric 				break;
5448058Seric 			}
5453149Seric 
5463149Seric 			switch (*rp)
5473149Seric 			{
5484060Seric 				register STAB *s;
5494060Seric 
5504060Seric 			  case MATCHCLASS:
5519585Seric 			  case MATCHNCLASS:
5529585Seric 				/* match any token in (not in) a class */
5534100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
55410690Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
5559585Seric 				{
5569585Seric 					if (*rp == MATCHCLASS)
5579585Seric 						goto backup;
5589585Seric 				}
5599585Seric 				else if (*rp == MATCHNCLASS)
5608058Seric 					goto backup;
5614468Seric 
5624476Seric 				/* explicit fall-through */
5634476Seric 
5644476Seric 			  case MATCHONE:
5654476Seric 			  case MATCHANY:
5664476Seric 				/* match exactly one token */
5678058Seric 				mlp->first = avp;
5688058Seric 				mlp->last = avp++;
5694468Seric 				mlp++;
5704060Seric 				break;
5714060Seric 
5728058Seric 			  case MATCHZANY:
5738058Seric 				/* match zero or more tokens */
5748058Seric 				mlp->first = avp;
5758058Seric 				mlp->last = avp - 1;
5768058Seric 				mlp++;
5778058Seric 				break;
5788058Seric 
5793149Seric 			  default:
5803149Seric 				/* must have exact match */
5814060Seric 				if (!sameword(rp, ap))
5828058Seric 					goto backup;
5834468Seric 				avp++;
5843149Seric 				break;
5853149Seric 			}
5863149Seric 
5873149Seric 			/* successful match on this token */
5883149Seric 			rvp++;
5893149Seric 			continue;
5903149Seric 
5918058Seric 		  backup:
5923149Seric 			/* match failed -- back up */
5933149Seric 			while (--rvp >= rwr->r_lhs)
5943149Seric 			{
5953149Seric 				rp = *rvp;
5968058Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
5974468Seric 				{
5984476Seric 					/* extend binding and continue */
5998058Seric 					avp = ++mlp[-1].last;
6008058Seric 					avp++;
6014476Seric 					rvp++;
6023149Seric 					break;
6034468Seric 				}
6044476Seric 				avp--;
6059585Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS ||
6069585Seric 				    *rp == MATCHNCLASS)
6073149Seric 				{
6084468Seric 					/* back out binding */
6094468Seric 					mlp--;
6103149Seric 				}
6113149Seric 			}
6123149Seric 
6133149Seric 			if (rvp < rwr->r_lhs)
6143149Seric 			{
6153149Seric 				/* total failure to match */
6163149Seric 				break;
6173149Seric 			}
618297Seric 		}
6193149Seric 
6203149Seric 		/*
6213149Seric 		**  See if we successfully matched
6223149Seric 		*/
6233149Seric 
6249374Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
6253149Seric 		{
6264100Seric # ifdef DEBUG
6279374Seric 			if (tTd(21, 10))
6289374Seric 				printf("----- rule fails\n");
6294100Seric # endif DEBUG
6309374Seric 			rwr = rwr->r_next;
6319374Seric 			continue;
6329374Seric 		}
6333149Seric 
6349374Seric 		rvp = rwr->r_rhs;
6359374Seric # ifdef DEBUG
6369374Seric 		if (tTd(21, 12))
6379374Seric 		{
6389374Seric 			printf("-----rule matches:");
6399374Seric 			printav(rvp);
6409374Seric 		}
6419374Seric # endif DEBUG
6429374Seric 
6439374Seric 		rp = *rvp;
6449374Seric 		if (*rp == CANONUSER)
6459374Seric 		{
6469374Seric 			rvp++;
6479374Seric 			rwr = rwr->r_next;
6489374Seric 		}
6499374Seric 		else if (*rp == CANONHOST)
6509374Seric 		{
6519374Seric 			rvp++;
6529374Seric 			rwr = NULL;
6539374Seric 		}
6549374Seric 		else if (*rp == CANONNET)
6559374Seric 			rwr = NULL;
6569374Seric 
6579374Seric 		/* substitute */
6589374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
6599374Seric 		{
6609374Seric 			register struct match *m;
6619374Seric 			register char **pp;
6629374Seric 
6638058Seric 			rp = *rvp;
66416914Seric 			if (*rp == MATCHREPL)
6658058Seric 			{
66616914Seric 				/* substitute from LHS */
66716914Seric 				m = &mlist[rp[1] - '1'];
66816914Seric 				if (m >= mlp)
6699374Seric 				{
67016914Seric 					syserr("rewrite: ruleset %d: replacement out of bounds", ruleset);
6719374Seric 					return;
6729374Seric 				}
6739374Seric # ifdef DEBUG
67416914Seric 				if (tTd(21, 15))
67516914Seric 				{
67616914Seric 					printf("$%c:", rp[1]);
67716914Seric 					pp = m->first;
67816914Seric 					while (pp <= m->last)
67916914Seric 					{
68016914Seric 						printf(" %x=\"", *pp);
68116914Seric 						(void) fflush(stdout);
68216914Seric 						printf("%s\"", *pp++);
68316914Seric 					}
68416914Seric 					printf("\n");
68516914Seric 				}
68616914Seric # endif DEBUG
6879374Seric 				pp = m->first;
6889374Seric 				while (pp <= m->last)
6893149Seric 				{
69016914Seric 					if (avp >= &npvp[MAXATOM])
69116914Seric 					{
69216914Seric 						syserr("rewrite: expansion too long");
69316914Seric 						return;
69416914Seric 					}
69516914Seric 					*avp++ = *pp++;
6963149Seric 				}
6973149Seric 			}
69816914Seric 			else
6998226Seric 			{
70016914Seric 				/* vanilla replacement */
7019374Seric 				if (avp >= &npvp[MAXATOM])
70216889Seric 				{
70316914Seric 	toolong:
70416889Seric 					syserr("rewrite: expansion too long");
70516889Seric 					return;
70616889Seric 				}
70716914Seric 				*avp++ = rp;
7088226Seric 			}
7099374Seric 		}
7109374Seric 		*avp++ = NULL;
71116914Seric 
71216914Seric 		/*
71316914Seric 		**  Check for any hostname lookups.
71416914Seric 		*/
71516914Seric 
71616914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
71716914Seric 		{
71816914Seric 			char **hbrvp;
71916914Seric 			char **xpvp;
72016914Seric 			int trsize;
72116914Seric 			int i;
72216920Seric 			char buf[MAXNAME + 1];
72316914Seric 			char *pvpb1[MAXATOM + 1];
72417174Seric 			char pvpbuf[PSBUFSIZE];
72516914Seric 
72616914Seric 			if (**rvp != HOSTBEGIN)
72716914Seric 				continue;
72816914Seric 
72916914Seric 			/*
73016914Seric 			**  Got a hostname lookup.
73116914Seric 			**
73216914Seric 			**	This could be optimized fairly easily.
73316914Seric 			*/
73416914Seric 
73516914Seric 			hbrvp = rvp;
73616914Seric 
73716914Seric 			/* extract the match part */
73816914Seric 			while (*++rvp != NULL && **rvp != HOSTEND)
73916914Seric 				continue;
74016914Seric 			if (*rvp != NULL)
74116914Seric 				*rvp++ = NULL;
74216914Seric 
74316914Seric 			/* save the remainder of the input string */
74416914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
74516914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
74616914Seric 
74716914Seric 			/* look it up */
74816914Seric 			cataddr(++hbrvp, buf, sizeof buf);
74916914Seric 			maphostname(buf, sizeof buf);
75016914Seric 
75116914Seric 			/* scan the new host name */
75216914Seric 			xpvp = prescan(buf, '\0', pvpbuf);
75316914Seric 			if (xpvp == NULL)
75416914Seric 			{
75516914Seric 				syserr("rewrite: cannot prescan canonical hostname: %s", buf);
75616914Seric 				return (NULL);
75716914Seric 			}
75816914Seric 
75916914Seric 			/* append it to the token list */
76017174Seric 			for (avp = --hbrvp; *xpvp != NULL; xpvp++)
76117174Seric 			{
76217174Seric 				*avp++ = newstr(*xpvp);
76316920Seric 				if (avp >= &npvp[MAXATOM])
76416914Seric 					goto toolong;
76517174Seric 			}
76616914Seric 
76716914Seric 			/* restore the old trailing information */
76817177Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
76916920Seric 				if (avp >= &npvp[MAXATOM])
77016914Seric 					goto toolong;
77117174Seric 
77217174Seric 			break;
77316914Seric 		}
77416914Seric 
77516914Seric 		/*
77616914Seric 		**  Check for subroutine calls.
77716914Seric 		*/
77816914Seric 
77916889Seric 		if (**npvp == CALLSUBR)
7809374Seric 		{
78116889Seric 			bcopy((char *) &npvp[2], (char *) pvp,
78216900Seric 				(int) (avp - npvp - 2) * sizeof *avp);
78316889Seric # ifdef DEBUG
78416889Seric 			if (tTd(21, 3))
78516889Seric 				printf("-----callsubr %s\n", npvp[1]);
78616889Seric # endif DEBUG
78716889Seric 			rewrite(pvp, atoi(npvp[1]));
7883149Seric 		}
7893149Seric 		else
7903149Seric 		{
791*17348Seric 			bcopy((char *) npvp, (char *) pvp,
79216900Seric 				(int) (avp - npvp) * sizeof *avp);
7939374Seric 		}
7944100Seric # ifdef DEBUG
7959374Seric 		if (tTd(21, 4))
7969374Seric 		{
7979374Seric 			printf("rewritten as:");
7989374Seric 			printav(pvp);
7999374Seric 		}
8004100Seric # endif DEBUG
801297Seric 	}
8028069Seric 
8039279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
8048069Seric 	{
8058959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
8068069Seric 		printav(pvp);
8078069Seric 	}
8083149Seric }
8093149Seric /*
8103149Seric **  BUILDADDR -- build address from token vector.
8113149Seric **
8123149Seric **	Parameters:
8133149Seric **		tv -- token vector.
8143149Seric **		a -- pointer to address descriptor to fill.
8153149Seric **			If NULL, one will be allocated.
8163149Seric **
8173149Seric **	Returns:
8184279Seric **		NULL if there was an error.
8194279Seric **		'a' otherwise.
8203149Seric **
8213149Seric **	Side Effects:
8223149Seric **		fills in 'a'
8233149Seric */
8243149Seric 
8253149Seric ADDRESS *
8263149Seric buildaddr(tv, a)
8273149Seric 	register char **tv;
8283149Seric 	register ADDRESS *a;
8293149Seric {
8303149Seric 	static char buf[MAXNAME];
8313149Seric 	struct mailer **mp;
8323149Seric 	register struct mailer *m;
8334635Seric 	extern bool sameword();
8343149Seric 
8353149Seric 	if (a == NULL)
8363149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
83716889Seric 	bzero((char *) a, sizeof *a);
8383149Seric 
8393149Seric 	/* figure out what net/mailer to use */
8403149Seric 	if (**tv != CANONNET)
8414279Seric 	{
8423149Seric 		syserr("buildaddr: no net");
8434279Seric 		return (NULL);
8444279Seric 	}
8453149Seric 	tv++;
8464635Seric 	if (sameword(*tv, "error"))
8474279Seric 	{
84810183Seric 		if (**++tv == CANONHOST)
84910183Seric 		{
85010183Seric 			setstat(atoi(*++tv));
85110183Seric 			tv++;
85210183Seric 		}
85310183Seric 		if (**tv != CANONUSER)
8544279Seric 			syserr("buildaddr: error: no user");
8554279Seric 		buf[0] = '\0';
8564279Seric 		while (*++tv != NULL)
8574279Seric 		{
8584279Seric 			if (buf[0] != '\0')
8597005Seric 				(void) strcat(buf, " ");
8607005Seric 			(void) strcat(buf, *tv);
8614279Seric 		}
8624279Seric 		usrerr(buf);
8634279Seric 		return (NULL);
8644279Seric 	}
8654598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
8663149Seric 	{
8674635Seric 		if (sameword(m->m_name, *tv))
8683149Seric 			break;
8693149Seric 	}
8703149Seric 	if (m == NULL)
8714279Seric 	{
8723149Seric 		syserr("buildaddr: unknown net %s", *tv);
8734279Seric 		return (NULL);
8744279Seric 	}
8754598Seric 	a->q_mailer = m;
8763149Seric 
8773149Seric 	/* figure out what host (if any) */
8783149Seric 	tv++;
87910690Seric 	if (!bitnset(M_LOCAL, m->m_flags))
8803149Seric 	{
8815704Seric 		if (**tv++ != CANONHOST)
8824279Seric 		{
8833149Seric 			syserr("buildaddr: no host");
8844279Seric 			return (NULL);
8854279Seric 		}
8865704Seric 		buf[0] = '\0';
8875704Seric 		while (*tv != NULL && **tv != CANONUSER)
8887005Seric 			(void) strcat(buf, *tv++);
8895704Seric 		a->q_host = newstr(buf);
8903149Seric 	}
8913149Seric 	else
8923149Seric 		a->q_host = NULL;
8933149Seric 
8943149Seric 	/* figure out the user */
8953149Seric 	if (**tv != CANONUSER)
8964279Seric 	{
8973149Seric 		syserr("buildaddr: no user");
8984279Seric 		return (NULL);
8994279Seric 	}
90011278Seric 	rewrite(++tv, 4);
90111278Seric 	cataddr(tv, buf, sizeof buf);
9023149Seric 	a->q_user = buf;
9033149Seric 
9043149Seric 	return (a);
9053149Seric }
9063188Seric /*
9074228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
9084228Seric **
9094228Seric **	Parameters:
9104228Seric **		pvp -- parameter vector to rebuild.
9114228Seric **		buf -- buffer to build the string into.
9124228Seric **		sz -- size of buf.
9134228Seric **
9144228Seric **	Returns:
9154228Seric **		none.
9164228Seric **
9174228Seric **	Side Effects:
9184228Seric **		Destroys buf.
9194228Seric */
9204228Seric 
9214228Seric cataddr(pvp, buf, sz)
9224228Seric 	char **pvp;
9234228Seric 	char *buf;
9244228Seric 	register int sz;
9254228Seric {
9264228Seric 	bool oatomtok = FALSE;
9274228Seric 	bool natomtok = FALSE;
9284228Seric 	register int i;
9294228Seric 	register char *p;
9304228Seric 
9318423Seric 	if (pvp == NULL)
9328423Seric 	{
9338423Seric 		strcpy(buf, "");
9348423Seric 		return;
9358423Seric 	}
9364228Seric 	p = buf;
93711156Seric 	sz -= 2;
9384228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
9394228Seric 	{
9408078Seric 		natomtok = (toktype(**pvp) == ATM);
9414228Seric 		if (oatomtok && natomtok)
9429042Seric 			*p++ = SpaceSub;
9434228Seric 		(void) strcpy(p, *pvp);
9444228Seric 		oatomtok = natomtok;
9454228Seric 		p += i;
94611156Seric 		sz -= i + 1;
9474228Seric 		pvp++;
9484228Seric 	}
9494228Seric 	*p = '\0';
9504228Seric }
9514228Seric /*
9523188Seric **  SAMEADDR -- Determine if two addresses are the same
9533188Seric **
9543188Seric **	This is not just a straight comparison -- if the mailer doesn't
9553188Seric **	care about the host we just ignore it, etc.
9563188Seric **
9573188Seric **	Parameters:
9583188Seric **		a, b -- pointers to the internal forms to compare.
9593188Seric **
9603188Seric **	Returns:
9613188Seric **		TRUE -- they represent the same mailbox.
9623188Seric **		FALSE -- they don't.
9633188Seric **
9643188Seric **	Side Effects:
9653188Seric **		none.
9663188Seric */
9673188Seric 
9683188Seric bool
9699374Seric sameaddr(a, b)
9703188Seric 	register ADDRESS *a;
9713188Seric 	register ADDRESS *b;
9723188Seric {
9733188Seric 	/* if they don't have the same mailer, forget it */
9743188Seric 	if (a->q_mailer != b->q_mailer)
9753188Seric 		return (FALSE);
9763188Seric 
9773188Seric 	/* if the user isn't the same, we can drop out */
9789374Seric 	if (strcmp(a->q_user, b->q_user) != 0)
9793188Seric 		return (FALSE);
9803188Seric 
9813188Seric 	/* if the mailer ignores hosts, we have succeeded! */
98210690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
9833188Seric 		return (TRUE);
9843188Seric 
9853188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
9863188Seric 	if (a->q_host == NULL || b->q_host == NULL)
9873188Seric 		return (FALSE);
9883188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
9893188Seric 		return (FALSE);
9903188Seric 
9913188Seric 	return (TRUE);
9923188Seric }
9933234Seric /*
9943234Seric **  PRINTADDR -- print address (for debugging)
9953234Seric **
9963234Seric **	Parameters:
9973234Seric **		a -- the address to print
9983234Seric **		follow -- follow the q_next chain.
9993234Seric **
10003234Seric **	Returns:
10013234Seric **		none.
10023234Seric **
10033234Seric **	Side Effects:
10043234Seric **		none.
10053234Seric */
10063234Seric 
10074317Seric # ifdef DEBUG
10084317Seric 
10093234Seric printaddr(a, follow)
10103234Seric 	register ADDRESS *a;
10113234Seric 	bool follow;
10123234Seric {
10135001Seric 	bool first = TRUE;
10145001Seric 
10153234Seric 	while (a != NULL)
10163234Seric 	{
10175001Seric 		first = FALSE;
10184443Seric 		printf("%x=", a);
10194085Seric 		(void) fflush(stdout);
10203234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
10218181Seric 		       a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host,
10228181Seric 		       a->q_user);
10238181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
10248181Seric 		       a->q_alias);
10258181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
10268181Seric 		       a->q_fullname);
10274996Seric 
10283234Seric 		if (!follow)
10293234Seric 			return;
10304996Seric 		a = a->q_next;
10313234Seric 	}
10325001Seric 	if (first)
10334443Seric 		printf("[NULL]\n");
10343234Seric }
10354317Seric 
10364317Seric # endif DEBUG
10377682Seric /*
10387682Seric **  REMOTENAME -- return the name relative to the current mailer
10397682Seric **
10407682Seric **	Parameters:
10417682Seric **		name -- the name to translate.
10428069Seric **		m -- the mailer that we want to do rewriting relative
10438069Seric **			to.
10448069Seric **		senderaddress -- if set, uses the sender rewriting rules
10458069Seric **			rather than the recipient rewriting rules.
104610310Seric **		canonical -- if set, strip out any comment information,
104710310Seric **			etc.
10487682Seric **
10497682Seric **	Returns:
10507682Seric **		the text string representing this address relative to
10517682Seric **			the receiving mailer.
10527682Seric **
10537682Seric **	Side Effects:
10547682Seric **		none.
10557682Seric **
10567682Seric **	Warnings:
10577682Seric **		The text string returned is tucked away locally;
10587682Seric **			copy it if you intend to save it.
10597682Seric */
10607682Seric 
10617682Seric char *
106210310Seric remotename(name, m, senderaddress, canonical)
10637682Seric 	char *name;
10647682Seric 	struct mailer *m;
10658069Seric 	bool senderaddress;
106610310Seric 	bool canonical;
10677682Seric {
10688069Seric 	register char **pvp;
10698069Seric 	char *fancy;
107015284Seric 	register char *p;
10718069Seric 	extern char *macvalue();
10728181Seric 	char *oldg = macvalue('g', CurEnv);
10737682Seric 	static char buf[MAXNAME];
10747682Seric 	char lbuf[MAXNAME];
107516914Seric 	char pvpbuf[PSBUFSIZE];
10767682Seric 	extern char **prescan();
10777889Seric 	extern char *crackaddr();
10787682Seric 
10797755Seric # ifdef DEBUG
10807755Seric 	if (tTd(12, 1))
10817755Seric 		printf("remotename(%s)\n", name);
10827755Seric # endif DEBUG
10837755Seric 
108410177Seric 	/* don't do anything if we are tagging it as special */
108510177Seric 	if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0)
108610177Seric 		return (name);
108710177Seric 
10887682Seric 	/*
10898181Seric 	**  Do a heuristic crack of this name to extract any comment info.
10908181Seric 	**	This will leave the name as a comment and a $g macro.
10917889Seric 	*/
10927889Seric 
109310310Seric 	if (canonical)
109416155Seric 		fancy = "\001g";
109510310Seric 	else
109610310Seric 		fancy = crackaddr(name);
10977889Seric 
10988181Seric 	/*
10998181Seric 	**  Turn the name into canonical form.
11008181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
11018181Seric 	**	If this only resolves to "user", and the "C" flag is
11028181Seric 	**	specified in the sending mailer, then the sender's
11038181Seric 	**	domain will be appended.
11048181Seric 	*/
11058181Seric 
110616914Seric 	pvp = prescan(name, '\0', pvpbuf);
11077889Seric 	if (pvp == NULL)
11087889Seric 		return (name);
11098181Seric 	rewrite(pvp, 3);
11108181Seric 	if (CurEnv->e_fromdomain != NULL)
11118181Seric 	{
11128181Seric 		/* append from domain to this address */
11138181Seric 		register char **pxp = pvp;
11148181Seric 
11159594Seric 		/* see if there is an "@domain" in the current name */
11168181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
11178181Seric 			pxp++;
11188181Seric 		if (*pxp == NULL)
11198181Seric 		{
11209594Seric 			/* no.... append the "@domain" from the sender */
11218181Seric 			register char **qxq = CurEnv->e_fromdomain;
11228181Seric 
11239594Seric 			while ((*pxp++ = *qxq++) != NULL)
11249594Seric 				continue;
112511726Seric 			rewrite(pvp, 3);
11268181Seric 		}
11278181Seric 	}
11288181Seric 
11298181Seric 	/*
11308959Seric 	**  Do more specific rewriting.
11318181Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
11328181Seric 	**		a sender address or not.
11338181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
11348181Seric 	*/
11358181Seric 
11368069Seric 	if (senderaddress)
11377755Seric 	{
11387889Seric 		rewrite(pvp, 1);
11398069Seric 		if (m->m_s_rwset > 0)
11408069Seric 			rewrite(pvp, m->m_s_rwset);
11418069Seric 	}
11428069Seric 	else
11438069Seric 	{
11447889Seric 		rewrite(pvp, 2);
11458069Seric 		if (m->m_r_rwset > 0)
11468069Seric 			rewrite(pvp, m->m_r_rwset);
11477682Seric 	}
11487682Seric 
11498181Seric 	/*
11508959Seric 	**  Do any final sanitation the address may require.
11518959Seric 	**	This will normally be used to turn internal forms
11528959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
11538959Seric 	**	may be used as a default to the above rules.
11548959Seric 	*/
11558959Seric 
11568959Seric 	rewrite(pvp, 4);
11578959Seric 
11588959Seric 	/*
11598181Seric 	**  Now restore the comment information we had at the beginning.
11608181Seric 	*/
11618181Seric 
11627682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
11639374Seric 	define('g', lbuf, CurEnv);
11647889Seric 	expand(fancy, buf, &buf[sizeof buf - 1], CurEnv);
11659374Seric 	define('g', oldg, CurEnv);
11667682Seric 
11677682Seric # ifdef DEBUG
11687682Seric 	if (tTd(12, 1))
11697755Seric 		printf("remotename => `%s'\n", buf);
11707682Seric # endif DEBUG
11717682Seric 	return (buf);
11727682Seric }
1173