13312Seric # include "sendmail.h"
2297Seric 
3*8353Seric SCCSID(@(#)parseaddr.c	3.59		10/07/82);
4407Seric 
5297Seric /*
6297Seric **  PARSE -- 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.
31297Seric **
32297Seric **	Returns:
33297Seric **		A pointer to the address descriptor header (`a' if
34297Seric **			`a' is non-NULL).
35297Seric **		NULL on error.
36297Seric **
37297Seric **	Side Effects:
38297Seric **		none
39297Seric */
40297Seric 
413380Seric # define DELIMCHARS	"$()<>,;\\\"\r\n"	/* word delimiters */
422091Seric 
432973Seric ADDRESS *
44297Seric parse(addr, a, copyf)
45297Seric 	char *addr;
462973Seric 	register ADDRESS *a;
47297Seric 	int copyf;
48297Seric {
493149Seric 	register char **pvp;
503149Seric 	register struct mailer *m;
513149Seric 	extern char **prescan();
523149Seric 	extern ADDRESS *buildaddr();
537889Seric 	static char nbuf[MAXNAME];
54297Seric 
55297Seric 	/*
56297Seric 	**  Initialize and prescan address.
57297Seric 	*/
58297Seric 
596903Seric 	CurEnv->e_to = addr;
603188Seric # ifdef DEBUG
617675Seric 	if (tTd(20, 1))
623188Seric 		printf("\n--parse(%s)\n", addr);
633188Seric # endif DEBUG
643188Seric 
658078Seric 	pvp = prescan(addr, ',');
663149Seric 	if (pvp == NULL)
67297Seric 		return (NULL);
68297Seric 
69297Seric 	/*
703149Seric 	**  Apply rewriting rules.
717889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
72297Seric 	*/
73297Seric 
748181Seric 	rewrite(pvp, 3);
754070Seric 	rewrite(pvp, 0);
76297Seric 
773149Seric 	/*
783149Seric 	**  See if we resolved to a real mailer.
793149Seric 	*/
80297Seric 
813149Seric 	if (pvp[0][0] != CANONNET)
823149Seric 	{
833149Seric 		setstat(EX_USAGE);
843149Seric 		usrerr("cannot resolve name");
853149Seric 		return (NULL);
86297Seric 	}
87297Seric 
88297Seric 	/*
893149Seric 	**  Build canonical address from pvp.
90297Seric 	*/
91297Seric 
923149Seric 	a = buildaddr(pvp, a);
934279Seric 	if (a == NULL)
944279Seric 		return (NULL);
954598Seric 	m = a->q_mailer;
96297Seric 
97297Seric 	/*
983149Seric 	**  Make local copies of the host & user and then
993149Seric 	**  transport them out.
100297Seric 	*/
101297Seric 
102297Seric 	if (copyf > 0)
1038078Seric 	{
1048078Seric 		extern char *DelimChar;
1058078Seric 		char savec = *DelimChar;
1068078Seric 
1078078Seric 		*DelimChar = '\0';
1082973Seric 		a->q_paddr = newstr(addr);
1098078Seric 		*DelimChar = savec;
1108078Seric 	}
111297Seric 	else
112297Seric 		a->q_paddr = addr;
1133149Seric 	if (copyf >= 0)
114297Seric 	{
1153149Seric 		if (a->q_host != NULL)
1163149Seric 			a->q_host = newstr(a->q_host);
117297Seric 		else
1183149Seric 			a->q_host = "";
1193149Seric 		if (a->q_user != a->q_paddr)
1203149Seric 			a->q_user = newstr(a->q_user);
121297Seric 	}
122297Seric 
123297Seric 	/*
124297Seric 	**  Do UPPER->lower case mapping unless inhibited.
125297Seric 	*/
126297Seric 
1273149Seric 	if (!bitset(M_HST_UPPER, m->m_flags))
128297Seric 		makelower(a->q_host);
1293149Seric 	if (!bitset(M_USR_UPPER, m->m_flags))
130297Seric 		makelower(a->q_user);
131297Seric 
132297Seric 	/*
133297Seric 	**  Compute return value.
134297Seric 	*/
135297Seric 
136297Seric # ifdef DEBUG
1377675Seric 	if (tTd(20, 1))
1384443Seric 	{
1394443Seric 		printf("parse-->");
1404443Seric 		printaddr(a, FALSE);
1414443Seric 	}
142297Seric # endif DEBUG
143297Seric 
144297Seric 	return (a);
145297Seric }
146297Seric /*
147297Seric **  PRESCAN -- Prescan name and make it canonical
148297Seric **
149297Seric **	Scans a name and turns it into canonical form.  This involves
150297Seric **	deleting blanks, comments (in parentheses), and turning the
151297Seric **	word "at" into an at-sign ("@").  The name is copied as this
152297Seric **	is done; it is legal to copy a name onto itself, since this
153297Seric **	process can only make things smaller.
154297Seric **
155297Seric **	This routine knows about quoted strings and angle brackets.
156297Seric **
157297Seric **	There are certain subtleties to this routine.  The one that
158297Seric **	comes to mind now is that backslashes on the ends of names
159297Seric **	are silently stripped off; this is intentional.  The problem
160297Seric **	is that some versions of sndmsg (like at LBL) set the kill
161297Seric **	character to something other than @ when reading addresses;
162297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
163297Seric **	berknet mailer.
164297Seric **
165297Seric **	Parameters:
166297Seric **		addr -- the name to chomp.
167297Seric **		delim -- the delimiter for the address, normally
168297Seric **			'\0' or ','; \0 is accepted in any case.
169297Seric **
170297Seric **	Returns:
1713149Seric **		A pointer to a vector of tokens.
172297Seric **		NULL on error.
173297Seric **
174297Seric **	Side Effects:
1753149Seric **		none.
176297Seric */
177297Seric 
1788078Seric /* states and character types */
1798078Seric # define OPR		0	/* operator */
1808078Seric # define ATM		1	/* atom */
1818078Seric # define QST		2	/* in quoted string */
1828078Seric # define SPC		3	/* chewing up spaces */
1838078Seric # define ONE		4	/* pick up one character */
1843149Seric 
1858078Seric # define NSTATES	5	/* number of states */
1868078Seric # define TYPE		017	/* mask to select state type */
1878078Seric 
1888078Seric /* meta bits for table */
1898078Seric # define M		020	/* meta character; don't pass through */
1908078Seric # define B		040	/* cause a break */
1918078Seric # define MB		M|B	/* meta-break */
1928078Seric 
1938078Seric static short StateTab[NSTATES][NSTATES] =
1948078Seric {
1958087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
1968078Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|MB,	SPC|MB,	ONE|B,
1978078Seric 	/*ATM*/		OPR|B,	ATM,	QST|MB,	SPC|MB,	ONE|B,
1988087Seric 	/*QST*/		QST,	QST,	OPR|MB,	QST,	QST,
1998078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2008078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2018078Seric };
2028078Seric 
2038078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2048078Seric 
2058078Seric char	*DelimChar;		/* set to point to the delimiter */
2068078Seric 
2073149Seric char **
2083149Seric prescan(addr, delim)
209297Seric 	char *addr;
210297Seric 	char delim;
211297Seric {
212297Seric 	register char *p;
2138078Seric 	register char *q;
2148078Seric 	register char c;
2153149Seric 	char **avp;
216297Seric 	bool bslashmode;
217297Seric 	int cmntcnt;
2183149Seric 	char *tok;
2198078Seric 	int state;
2208078Seric 	int newstate;
2218078Seric 	static char buf[MAXNAME+MAXATOM];
2228078Seric 	static char *av[MAXATOM+1];
223297Seric 
224297Seric 	q = buf;
2253149Seric 	bslashmode = FALSE;
2267800Seric 	cmntcnt = 0;
2273149Seric 	avp = av;
2288078Seric 	state = OPR;
2298078Seric 	c = NOCHAR;
2308078Seric 	p = addr;
2318078Seric # ifdef DEBUG
2328078Seric 	if (tTd(22, 45))
233297Seric 	{
2348078Seric 		printf("prescan: ");
2358078Seric 		xputs(p);
2368078Seric 		putchar('\n');
2378078Seric 	}
2388078Seric # endif DEBUG
2398078Seric 
2408078Seric 	do
2418078Seric 	{
2423149Seric 		/* read a token */
2433149Seric 		tok = q;
2448078Seric 		for (;;)
245297Seric 		{
2468078Seric 			/* store away any old lookahead character */
2478078Seric 			if (c != NOCHAR)
2488078Seric 			{
2498078Seric 				/* squirrel it away */
2508078Seric 				if (q >= &buf[sizeof buf - 5])
2518078Seric 				{
2528078Seric 					usrerr("Address too long");
2538078Seric 					DelimChar = p;
2548078Seric 					return (NULL);
2558078Seric 				}
2568078Seric 				*q++ = c;
2578078Seric 			}
2588078Seric 
2598078Seric 			/* read a new input character */
2608078Seric 			c = *p++;
2618078Seric 			if (c == '\0')
2628078Seric 				break;
2638078Seric # ifdef DEBUG
2648078Seric 			if (tTd(22, 101))
2658078Seric 				printf("c=%c, s=%d; ", c, state);
2668078Seric # endif DEBUG
2678078Seric 
2683149Seric 			/* chew up special characters */
2694100Seric 			c &= ~0200;
2703149Seric 			*q = '\0';
2713149Seric 			if (bslashmode)
2723149Seric 			{
2733149Seric 				c |= 0200;
2743149Seric 				bslashmode = FALSE;
2753149Seric 			}
2763149Seric 			else if (c == '\\')
2773149Seric 			{
2783149Seric 				bslashmode = TRUE;
2798078Seric 				c = NOCHAR;
2803149Seric 			}
2818078Seric 			else if (c == '(')
2824100Seric 			{
2838078Seric 				cmntcnt++;
2848078Seric 				c = NOCHAR;
2854100Seric 			}
2868078Seric 			else if (c == ')')
2873149Seric 			{
2888078Seric 				if (cmntcnt <= 0)
2893149Seric 				{
2908078Seric 					usrerr("Unbalanced ')'");
2918078Seric 					DelimChar = p;
2928078Seric 					return (NULL);
2933149Seric 				}
2948078Seric 				else
2958078Seric 					cmntcnt--;
2968078Seric 			}
2978078Seric 			else if (cmntcnt > 0)
2988078Seric 				c = NOCHAR;
2993149Seric 
3008078Seric 			if (c == NOCHAR)
3018078Seric 				continue;
3023149Seric 
3038078Seric 			/* see if this is end of input */
3048078Seric 			if (c == delim)
3053149Seric 				break;
3063149Seric 
3078078Seric 			newstate = StateTab[state][toktype(c)];
3088078Seric # ifdef DEBUG
3098078Seric 			if (tTd(22, 101))
3108078Seric 				printf("ns=%02o\n", newstate);
3118078Seric # endif DEBUG
3128078Seric 			state = newstate & TYPE;
3138078Seric 			if (bitset(M, newstate))
3148078Seric 				c = NOCHAR;
3158078Seric 			if (bitset(B, newstate))
3164228Seric 				break;
317297Seric 		}
3183149Seric 
3193149Seric 		/* new token */
3208078Seric 		if (tok != q)
3211378Seric 		{
3228078Seric 			*q++ = '\0';
3238078Seric # ifdef DEBUG
3248078Seric 			if (tTd(22, 36))
325297Seric 			{
3268078Seric 				printf("tok=");
3278078Seric 				xputs(tok);
3288078Seric 				putchar('\n');
329297Seric 			}
3308078Seric # endif DEBUG
3318078Seric 			if (avp >= &av[MAXATOM])
332297Seric 			{
3338078Seric 				syserr("prescan: too many tokens");
3348078Seric 				DelimChar = p;
3358078Seric 				return (NULL);
336297Seric 			}
3378078Seric 			*avp++ = tok;
338297Seric 		}
3398078Seric 	} while (c != '\0' && c != delim);
3403149Seric 	*avp = NULL;
3418078Seric 	DelimChar = --p;
3423149Seric 	if (cmntcnt > 0)
3433149Seric 		usrerr("Unbalanced '('");
3448078Seric 	else if (state == QST)
3453149Seric 		usrerr("Unbalanced '\"'");
3463149Seric 	else if (av[0] != NULL)
3473149Seric 		return (av);
3483149Seric 	return (NULL);
3493149Seric }
3503149Seric /*
3513149Seric **  TOKTYPE -- return token type
3523149Seric **
3533149Seric **	Parameters:
3543149Seric **		c -- the character in question.
3553149Seric **
3563149Seric **	Returns:
3573149Seric **		Its type.
3583149Seric **
3593149Seric **	Side Effects:
3603149Seric **		none.
3613149Seric */
362297Seric 
3633149Seric toktype(c)
3643149Seric 	register char c;
3653149Seric {
3663380Seric 	static char buf[50];
3673382Seric 	static bool firstime = TRUE;
3683380Seric 
3693382Seric 	if (firstime)
3703380Seric 	{
3713382Seric 		firstime = FALSE;
3726977Seric 		expand("$o", buf, &buf[sizeof buf - 1], CurEnv);
3737005Seric 		(void) strcat(buf, DELIMCHARS);
3743380Seric 	}
3756053Seric 	if (c == MATCHCLASS || c == MATCHREPL)
3768078Seric 		return (ONE);
3778078Seric 	if (c == '"')
3788078Seric 		return (QST);
3794100Seric 	if (!isascii(c))
3808078Seric 		return (ATM);
3818078Seric 	if (isspace(c) || c == ')')
3828078Seric 		return (SPC);
3833380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
3848078Seric 		return (OPR);
3858078Seric 	return (ATM);
3863149Seric }
3873149Seric /*
3883149Seric **  REWRITE -- apply rewrite rules to token vector.
3893149Seric **
3904476Seric **	This routine is an ordered production system.  Each rewrite
3914476Seric **	rule has a LHS (called the pattern) and a RHS (called the
3924476Seric **	rewrite); 'rwr' points the the current rewrite rule.
3934476Seric **
3944476Seric **	For each rewrite rule, 'avp' points the address vector we
3954476Seric **	are trying to match against, and 'pvp' points to the pattern.
3968058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
3978058Seric **	MATCHONE, MATCHCLASS) then the address in avp matched is
3988058Seric **	saved away in the match vector (pointed to by 'mvp').
3994476Seric **
4004476Seric **	When a match between avp & pvp does not match, we try to
4014476Seric **	back out.  If we back up over a MATCHONE or a MATCHCLASS
4024476Seric **	we must also back out the match in mvp.  If we reach a
4038058Seric **	MATCHANY or MATCHZANY we just extend the match and start
4048058Seric **	over again.
4054476Seric **
4064476Seric **	When we finally match, we rewrite the address vector
4074476Seric **	and try over again.
4084476Seric **
4093149Seric **	Parameters:
4103149Seric **		pvp -- pointer to token vector.
4113149Seric **
4123149Seric **	Returns:
4133149Seric **		none.
4143149Seric **
4153149Seric **	Side Effects:
4163149Seric **		pvp is modified.
4173149Seric */
4182091Seric 
4193149Seric struct match
4203149Seric {
4214468Seric 	char	**first;	/* first token matched */
4224468Seric 	char	**last;		/* last token matched */
4233149Seric };
4243149Seric 
4254468Seric # define MAXMATCH	9	/* max params per rewrite */
4263149Seric 
4273149Seric 
4284070Seric rewrite(pvp, ruleset)
4293149Seric 	char **pvp;
4304070Seric 	int ruleset;
4313149Seric {
4323149Seric 	register char *ap;		/* address pointer */
4333149Seric 	register char *rp;		/* rewrite pointer */
4343149Seric 	register char **avp;		/* address vector pointer */
4353149Seric 	register char **rvp;		/* rewrite vector pointer */
4368058Seric 	register struct match *mlp;	/* cur ptr into mlist */
4378058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
4384468Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
4393149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
4404060Seric 	extern bool sameword();
4413149Seric 
4428335Seric 	if (Mode == MD_TEST || tTd(21, 2))
4433149Seric 	{
4448069Seric 		printf("rewrite: ruleset %d, original pvp:", ruleset);
4453149Seric 		printav(pvp);
4463149Seric 	}
4473149Seric 
4483149Seric 	/*
4493149Seric 	**  Run through the list of rewrite rules, applying
4503149Seric 	**	any that match.
4513149Seric 	*/
4523149Seric 
4534070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
4543149Seric 	{
4554100Seric # ifdef DEBUG
4567675Seric 		if (tTd(21, 12))
457297Seric 		{
4588069Seric 			printf("-----trying rule:");
4593149Seric 			printav(rwr->r_lhs);
4603149Seric 		}
4614100Seric # endif DEBUG
4623149Seric 
4633149Seric 		/* try to match on this rule */
4644468Seric 		mlp = mlist;
4658058Seric 		rvp = rwr->r_lhs;
4668058Seric 		avp = pvp;
4678058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
4683149Seric 		{
4693149Seric 			rp = *rvp;
4708058Seric # ifdef DEBUG
4718058Seric 			if (tTd(21, 35))
4728058Seric 			{
4738069Seric 				printf("ap=");
4748058Seric 				xputs(ap);
4758069Seric 				printf(", rp=");
4768058Seric 				xputs(rp);
4778069Seric 				printf("\n");
4788058Seric 			}
4798058Seric # endif DEBUG
4803149Seric 			if (rp == NULL)
481297Seric 			{
4823149Seric 				/* end-of-pattern before end-of-address */
4838058Seric 				goto backup;
484297Seric 			}
4858058Seric 			if (ap == NULL && *rp != MATCHZANY)
4868058Seric 			{
4878058Seric 				/* end-of-input */
4888058Seric 				break;
4898058Seric 			}
4903149Seric 
4913149Seric 			switch (*rp)
4923149Seric 			{
4934060Seric 				register STAB *s;
4944060Seric 				register int class;
4954060Seric 
4964060Seric 			  case MATCHCLASS:
4974060Seric 				/* match any token in a class */
4984060Seric 				class = rp[1];
4994060Seric 				if (!isalpha(class))
5008058Seric 					goto backup;
5014060Seric 				if (isupper(class))
5024060Seric 					class -= 'A';
5034060Seric 				else
5044060Seric 					class -= 'a';
5054100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
5066273Seric 				if (s == NULL || (s->s_class & (1L << class)) == 0)
5078058Seric 					goto backup;
5084468Seric 
5094476Seric 				/* explicit fall-through */
5104476Seric 
5114476Seric 			  case MATCHONE:
5124476Seric 			  case MATCHANY:
5134476Seric 				/* match exactly one token */
5148058Seric 				mlp->first = avp;
5158058Seric 				mlp->last = avp++;
5164468Seric 				mlp++;
5174060Seric 				break;
5184060Seric 
5198058Seric 			  case MATCHZANY:
5208058Seric 				/* match zero or more tokens */
5218058Seric 				mlp->first = avp;
5228058Seric 				mlp->last = avp - 1;
5238058Seric 				mlp++;
5248058Seric 				break;
5258058Seric 
5263149Seric 			  default:
5273149Seric 				/* must have exact match */
5284060Seric 				if (!sameword(rp, ap))
5298058Seric 					goto backup;
5304468Seric 				avp++;
5313149Seric 				break;
5323149Seric 			}
5333149Seric 
5343149Seric 			/* successful match on this token */
5353149Seric 			rvp++;
5363149Seric 			continue;
5373149Seric 
5388058Seric 		  backup:
5393149Seric 			/* match failed -- back up */
5403149Seric 			while (--rvp >= rwr->r_lhs)
5413149Seric 			{
5423149Seric 				rp = *rvp;
5438058Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
5444468Seric 				{
5454476Seric 					/* extend binding and continue */
5468058Seric 					avp = ++mlp[-1].last;
5478058Seric 					avp++;
5484476Seric 					rvp++;
5493149Seric 					break;
5504468Seric 				}
5514476Seric 				avp--;
5524476Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS)
5533149Seric 				{
5544468Seric 					/* back out binding */
5554468Seric 					mlp--;
5563149Seric 				}
5573149Seric 			}
5583149Seric 
5593149Seric 			if (rvp < rwr->r_lhs)
5603149Seric 			{
5613149Seric 				/* total failure to match */
5623149Seric 				break;
5633149Seric 			}
564297Seric 		}
5653149Seric 
5663149Seric 		/*
5673149Seric 		**  See if we successfully matched
5683149Seric 		*/
5693149Seric 
5703149Seric 		if (rvp >= rwr->r_lhs && *rvp == NULL)
5713149Seric 		{
5728058Seric 			rvp = rwr->r_rhs;
5734100Seric # ifdef DEBUG
5747675Seric 			if (tTd(21, 12))
5753149Seric 			{
5768069Seric 				printf("-----rule matches:");
5778058Seric 				printav(rvp);
5783149Seric 			}
5794100Seric # endif DEBUG
5803149Seric 
5818058Seric 			rp = *rvp;
5828226Seric 			if (*rp == CANONUSER)
5838058Seric 			{
5848069Seric 				rvp++;
5858069Seric 				rwr = rwr->r_next;
5868069Seric 			}
5878069Seric 			else if (*rp == CANONHOST)
5888069Seric 			{
5898069Seric 				rvp++;
5908069Seric 				rwr = NULL;
5918069Seric 			}
5928069Seric 			else if (*rp == CANONNET)
5938069Seric 				rwr = NULL;
5948058Seric 
5953149Seric 			/* substitute */
5968069Seric 			for (avp = npvp; *rvp != NULL; rvp++)
5973149Seric 			{
5983149Seric 				rp = *rvp;
5994468Seric 				if (*rp == MATCHREPL)
6003149Seric 				{
6013149Seric 					register struct match *m;
6023149Seric 					register char **pp;
6033149Seric 
6044468Seric 					m = &mlist[rp[1] - '1'];
6054476Seric # ifdef DEBUG
6067675Seric 					if (tTd(21, 15))
6074476Seric 					{
6084476Seric 						printf("$%c:", rp[1]);
6094476Seric 						pp = m->first;
6108058Seric 						while (pp <= m->last)
6114476Seric 						{
6124476Seric 							printf(" %x=\"", *pp);
6134625Seric 							(void) fflush(stdout);
6148058Seric 							printf("%s\"", *pp++);
6158058Seric 						}
6164476Seric 						printf("\n");
6174476Seric 					}
6184476Seric # endif DEBUG
6194468Seric 					pp = m->first;
6208058Seric 					while (pp <= m->last)
6213149Seric 					{
6224468Seric 						if (avp >= &npvp[MAXATOM])
6233149Seric 						{
6244468Seric 							syserr("rewrite: expansion too long");
6254468Seric 							return;
6264468Seric 						}
6278058Seric 						*avp++ = *pp++;
6288058Seric 					}
6293149Seric 				}
6303149Seric 				else
6314385Seric 				{
6324385Seric 					if (avp >= &npvp[MAXATOM])
6334385Seric 					{
6344385Seric 						syserr("rewrite: expansion too long");
6354385Seric 						return;
6364385Seric 					}
6373149Seric 					*avp++ = rp;
6384385Seric 				}
6393149Seric 			}
6403149Seric 			*avp++ = NULL;
6418226Seric 			if (**npvp == CALLSUBR)
6428226Seric 			{
6438226Seric 				bmove((char *) &npvp[2], (char *) pvp,
6448226Seric 					(avp - npvp - 2) * sizeof *avp);
6453149Seric # ifdef DEBUG
6468226Seric 				if (tTd(21, 3))
6478226Seric 					printf("-----callsubr %s\n", npvp[1]);
6488226Seric # endif DEBUG
6498226Seric 				rewrite(pvp, atoi(npvp[1]));
6508226Seric 			}
6518226Seric 			else
6528226Seric 			{
6538226Seric 				bmove((char *) npvp, (char *) pvp,
6548226Seric 					(avp - npvp) * sizeof *avp);
6558226Seric 			}
6568226Seric # ifdef DEBUG
6577675Seric 			if (tTd(21, 4))
6583149Seric 			{
6598069Seric 				printf("rewritten as:");
6608069Seric 				printav(pvp);
6613149Seric 			}
6623149Seric # endif DEBUG
6633149Seric 		}
6643149Seric 		else
6653149Seric 		{
6664100Seric # ifdef DEBUG
6677675Seric 			if (tTd(21, 10))
6683149Seric 				printf("----- rule fails\n");
6694100Seric # endif DEBUG
6703149Seric 			rwr = rwr->r_next;
6713149Seric 		}
672297Seric 	}
6738069Seric 
6748335Seric 	if (Mode == MD_TEST || tTd(21, 2))
6758069Seric 	{
6768069Seric 		printf("rewrite: ruleset %d returns:", ruleset);
6778069Seric 		printav(pvp);
6788069Seric 	}
6793149Seric }
6803149Seric /*
6813149Seric **  BUILDADDR -- build address from token vector.
6823149Seric **
6833149Seric **	Parameters:
6843149Seric **		tv -- token vector.
6853149Seric **		a -- pointer to address descriptor to fill.
6863149Seric **			If NULL, one will be allocated.
6873149Seric **
6883149Seric **	Returns:
6894279Seric **		NULL if there was an error.
6904279Seric **		'a' otherwise.
6913149Seric **
6923149Seric **	Side Effects:
6933149Seric **		fills in 'a'
6943149Seric */
6953149Seric 
6963149Seric ADDRESS *
6973149Seric buildaddr(tv, a)
6983149Seric 	register char **tv;
6993149Seric 	register ADDRESS *a;
7003149Seric {
7013149Seric 	static char buf[MAXNAME];
7023149Seric 	struct mailer **mp;
7033149Seric 	register struct mailer *m;
7044635Seric 	extern bool sameword();
7053149Seric 
7063149Seric 	if (a == NULL)
7073149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
7084988Seric 	clear((char *) a, sizeof *a);
7093149Seric 
7103149Seric 	/* figure out what net/mailer to use */
7113149Seric 	if (**tv != CANONNET)
7124279Seric 	{
7133149Seric 		syserr("buildaddr: no net");
7144279Seric 		return (NULL);
7154279Seric 	}
7163149Seric 	tv++;
7174635Seric 	if (sameword(*tv, "error"))
7184279Seric 	{
7194279Seric 		if (**++tv != CANONUSER)
7204279Seric 			syserr("buildaddr: error: no user");
7214279Seric 		buf[0] = '\0';
7224279Seric 		while (*++tv != NULL)
7234279Seric 		{
7244279Seric 			if (buf[0] != '\0')
7257005Seric 				(void) strcat(buf, " ");
7267005Seric 			(void) strcat(buf, *tv);
7274279Seric 		}
7284279Seric 		usrerr(buf);
7294279Seric 		return (NULL);
7304279Seric 	}
7314598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
7323149Seric 	{
7334635Seric 		if (sameword(m->m_name, *tv))
7343149Seric 			break;
7353149Seric 	}
7363149Seric 	if (m == NULL)
7374279Seric 	{
7383149Seric 		syserr("buildaddr: unknown net %s", *tv);
7394279Seric 		return (NULL);
7404279Seric 	}
7414598Seric 	a->q_mailer = m;
7423149Seric 
7433149Seric 	/* figure out what host (if any) */
7443149Seric 	tv++;
7454195Seric 	if (!bitset(M_LOCAL, m->m_flags))
7463149Seric 	{
7475704Seric 		if (**tv++ != CANONHOST)
7484279Seric 		{
7493149Seric 			syserr("buildaddr: no host");
7504279Seric 			return (NULL);
7514279Seric 		}
7525704Seric 		buf[0] = '\0';
7535704Seric 		while (*tv != NULL && **tv != CANONUSER)
7547005Seric 			(void) strcat(buf, *tv++);
7555704Seric 		a->q_host = newstr(buf);
7563149Seric 	}
7573149Seric 	else
7583149Seric 		a->q_host = NULL;
7593149Seric 
7603149Seric 	/* figure out the user */
7613149Seric 	if (**tv != CANONUSER)
7624279Seric 	{
7633149Seric 		syserr("buildaddr: no user");
7644279Seric 		return (NULL);
7654279Seric 	}
7664228Seric 	cataddr(++tv, buf, sizeof buf);
7673149Seric 	a->q_user = buf;
7683149Seric 
7693149Seric 	return (a);
7703149Seric }
7713188Seric /*
7724228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
7734228Seric **
7744228Seric **	Parameters:
7754228Seric **		pvp -- parameter vector to rebuild.
7764228Seric **		buf -- buffer to build the string into.
7774228Seric **		sz -- size of buf.
7784228Seric **
7794228Seric **	Returns:
7804228Seric **		none.
7814228Seric **
7824228Seric **	Side Effects:
7834228Seric **		Destroys buf.
7844228Seric */
7854228Seric 
7864228Seric cataddr(pvp, buf, sz)
7874228Seric 	char **pvp;
7884228Seric 	char *buf;
7894228Seric 	register int sz;
7904228Seric {
7914228Seric 	bool oatomtok = FALSE;
7924228Seric 	bool natomtok = FALSE;
7934228Seric 	register int i;
7944228Seric 	register char *p;
7954228Seric 
7964228Seric 	p = buf;
7974228Seric 	sz--;
7984228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
7994228Seric 	{
8008078Seric 		natomtok = (toktype(**pvp) == ATM);
8014228Seric 		if (oatomtok && natomtok)
8024228Seric 			*p++ = SPACESUB;
8034228Seric 		(void) strcpy(p, *pvp);
8044228Seric 		oatomtok = natomtok;
8054228Seric 		p += i;
8064228Seric 		sz -= i;
8074228Seric 		pvp++;
8084228Seric 	}
8094228Seric 	*p = '\0';
8104228Seric }
8114228Seric /*
8123188Seric **  SAMEADDR -- Determine if two addresses are the same
8133188Seric **
8143188Seric **	This is not just a straight comparison -- if the mailer doesn't
8153188Seric **	care about the host we just ignore it, etc.
8163188Seric **
8173188Seric **	Parameters:
8183188Seric **		a, b -- pointers to the internal forms to compare.
8193188Seric **		wildflg -- if TRUE, 'a' may have no user specified,
8203188Seric **			in which case it is to match anything.
8213188Seric **
8223188Seric **	Returns:
8233188Seric **		TRUE -- they represent the same mailbox.
8243188Seric **		FALSE -- they don't.
8253188Seric **
8263188Seric **	Side Effects:
8273188Seric **		none.
8283188Seric */
8293188Seric 
8303188Seric bool
8313188Seric sameaddr(a, b, wildflg)
8323188Seric 	register ADDRESS *a;
8333188Seric 	register ADDRESS *b;
8343188Seric 	bool wildflg;
8353188Seric {
8363188Seric 	/* if they don't have the same mailer, forget it */
8373188Seric 	if (a->q_mailer != b->q_mailer)
8383188Seric 		return (FALSE);
8393188Seric 
8403188Seric 	/* if the user isn't the same, we can drop out */
8413188Seric 	if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0)
8423188Seric 		return (FALSE);
8433188Seric 
8443188Seric 	/* if the mailer ignores hosts, we have succeeded! */
8454598Seric 	if (bitset(M_LOCAL, a->q_mailer->m_flags))
8463188Seric 		return (TRUE);
8473188Seric 
8483188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
8493188Seric 	if (a->q_host == NULL || b->q_host == NULL)
8503188Seric 		return (FALSE);
8513188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
8523188Seric 		return (FALSE);
8533188Seric 
8543188Seric 	return (TRUE);
8553188Seric }
8563234Seric /*
8573234Seric **  PRINTADDR -- print address (for debugging)
8583234Seric **
8593234Seric **	Parameters:
8603234Seric **		a -- the address to print
8613234Seric **		follow -- follow the q_next chain.
8623234Seric **
8633234Seric **	Returns:
8643234Seric **		none.
8653234Seric **
8663234Seric **	Side Effects:
8673234Seric **		none.
8683234Seric */
8693234Seric 
8704317Seric # ifdef DEBUG
8714317Seric 
8723234Seric printaddr(a, follow)
8733234Seric 	register ADDRESS *a;
8743234Seric 	bool follow;
8753234Seric {
8765001Seric 	bool first = TRUE;
8775001Seric 
8783234Seric 	while (a != NULL)
8793234Seric 	{
8805001Seric 		first = FALSE;
8814443Seric 		printf("%x=", a);
8824085Seric 		(void) fflush(stdout);
8833234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
8848181Seric 		       a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host,
8858181Seric 		       a->q_user);
8868181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
8878181Seric 		       a->q_alias);
8888181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
8898181Seric 		       a->q_fullname);
8904996Seric 
8913234Seric 		if (!follow)
8923234Seric 			return;
8934996Seric 		a = a->q_next;
8943234Seric 	}
8955001Seric 	if (first)
8964443Seric 		printf("[NULL]\n");
8973234Seric }
8984317Seric 
8994317Seric # endif DEBUG
9007682Seric /*
9017682Seric **  REMOTENAME -- return the name relative to the current mailer
9027682Seric **
9037682Seric **	Parameters:
9047682Seric **		name -- the name to translate.
9058069Seric **		m -- the mailer that we want to do rewriting relative
9068069Seric **			to.
9078069Seric **		senderaddress -- if set, uses the sender rewriting rules
9088069Seric **			rather than the recipient rewriting rules.
9097682Seric **
9107682Seric **	Returns:
9117682Seric **		the text string representing this address relative to
9127682Seric **			the receiving mailer.
9137682Seric **
9147682Seric **	Side Effects:
9157682Seric **		none.
9167682Seric **
9177682Seric **	Warnings:
9187682Seric **		The text string returned is tucked away locally;
9197682Seric **			copy it if you intend to save it.
9207682Seric */
9217682Seric 
9227682Seric char *
9238069Seric remotename(name, m, senderaddress)
9247682Seric 	char *name;
9257682Seric 	struct mailer *m;
9268069Seric 	bool senderaddress;
9277682Seric {
9288069Seric 	register char **pvp;
9298069Seric 	char *fancy;
9308069Seric 	extern char *macvalue();
9318181Seric 	char *oldg = macvalue('g', CurEnv);
9327682Seric 	static char buf[MAXNAME];
9337682Seric 	char lbuf[MAXNAME];
9347682Seric 	extern char **prescan();
9357889Seric 	extern char *crackaddr();
9367682Seric 
9377755Seric # ifdef DEBUG
9387755Seric 	if (tTd(12, 1))
9397755Seric 		printf("remotename(%s)\n", name);
9407755Seric # endif DEBUG
9417755Seric 
9427682Seric 	/*
9438181Seric 	**  Do a heuristic crack of this name to extract any comment info.
9448181Seric 	**	This will leave the name as a comment and a $g macro.
9457889Seric 	*/
9467889Seric 
9477889Seric 	fancy = crackaddr(name);
9487889Seric 
9498181Seric 	/*
9508181Seric 	**  Turn the name into canonical form.
9518181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
9528181Seric 	**	If this only resolves to "user", and the "C" flag is
9538181Seric 	**	specified in the sending mailer, then the sender's
9548181Seric 	**	domain will be appended.
9558181Seric 	*/
9568181Seric 
9577889Seric 	pvp = prescan(name, '\0');
9587889Seric 	if (pvp == NULL)
9597889Seric 		return (name);
9608181Seric 	rewrite(pvp, 3);
9618181Seric 	if (CurEnv->e_fromdomain != NULL)
9628181Seric 	{
9638181Seric 		/* append from domain to this address */
9648181Seric 		register char **pxp = pvp;
9658181Seric 
9668181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
9678181Seric 			pxp++;
9688181Seric 		if (*pxp == NULL)
9698181Seric 		{
9708181Seric 			register char **qxq = CurEnv->e_fromdomain;
9718181Seric 
9728181Seric 			while (*qxq != NULL)
9738181Seric 				*pxp++ = *qxq++;
9748181Seric 		}
9758181Seric 	}
9768181Seric 
9778181Seric 	/*
9788181Seric 	**  Now do more specific rewriting.
9798181Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
9808181Seric 	**		a sender address or not.
9818181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
9828181Seric 	*/
9838181Seric 
9848069Seric 	if (senderaddress)
9857755Seric 	{
9867889Seric 		rewrite(pvp, 1);
9878069Seric 		if (m->m_s_rwset > 0)
9888069Seric 			rewrite(pvp, m->m_s_rwset);
9898069Seric 	}
9908069Seric 	else
9918069Seric 	{
9927889Seric 		rewrite(pvp, 2);
9938069Seric 		if (m->m_r_rwset > 0)
9948069Seric 			rewrite(pvp, m->m_r_rwset);
9957682Seric 	}
9967682Seric 
9978181Seric 	/*
9988181Seric 	**  Now restore the comment information we had at the beginning.
9998181Seric 	*/
10008181Seric 
10017682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
10027682Seric 	define('g', lbuf);
10037889Seric 	expand(fancy, buf, &buf[sizeof buf - 1], CurEnv);
10047682Seric 	define('g', oldg);
10057682Seric 
10067682Seric # ifdef DEBUG
10077682Seric 	if (tTd(12, 1))
10087755Seric 		printf("remotename => `%s'\n", buf);
10097682Seric # endif DEBUG
10107682Seric 	return (buf);
10117682Seric }
10127682Seric /*
10137682Seric **  CANONNAME -- make name canonical
10147682Seric **
10157682Seric **	This is used for SMTP and misc. printing.  Given a print
10168181Seric **	address, it strips out comments, etc.
10177682Seric **
10187682Seric **	Parameters:
10197682Seric **		name -- the name to make canonical.
1020*8353Seric **		ruleset -- the canonicalizing ruleset.
10217682Seric **
10227682Seric **	Returns:
10237682Seric **		pointer to canonical name.
10247682Seric **
10257682Seric **	Side Effects:
10267682Seric **		none.
10277682Seric **
10287682Seric **	Warning:
10297682Seric **		result is saved in static buf; future calls will trash it.
10307682Seric */
10317682Seric 
10327682Seric char *
1033*8353Seric canonname(name, ruleset)
10347682Seric 	char *name;
1035*8353Seric 	int ruleset;
10367682Seric {
10378069Seric 	static char nbuf[MAXNAME];
10387940Seric 	register char **pvp;
10397682Seric 
10407940Seric 	pvp = prescan(name, '\0');
10418069Seric 	rewrite(pvp, 3);
1042*8353Seric 	rewrite(pvp, ruleset);
10438069Seric 	cataddr(pvp, nbuf, sizeof nbuf);
10447682Seric 	return (nbuf);
10457682Seric }
1046