13312Seric # include "sendmail.h"
2297Seric 
3*8181Seric SCCSID(@(#)parseaddr.c	3.55		09/12/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 
74*8181Seric 	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 
4424100Seric # ifdef DEBUG
4438069Seric 	if (tTd(21, 2))
4443149Seric 	{
4458069Seric 		printf("rewrite: ruleset %d, original pvp:", ruleset);
4463149Seric 		printav(pvp);
4473149Seric 	}
4484100Seric # endif DEBUG
4493149Seric 
4503149Seric 	/*
4513149Seric 	**  Run through the list of rewrite rules, applying
4523149Seric 	**	any that match.
4533149Seric 	*/
4543149Seric 
4554070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
4563149Seric 	{
4574100Seric # ifdef DEBUG
4587675Seric 		if (tTd(21, 12))
459297Seric 		{
4608069Seric 			printf("-----trying rule:");
4613149Seric 			printav(rwr->r_lhs);
4623149Seric 		}
4634100Seric # endif DEBUG
4643149Seric 
4653149Seric 		/* try to match on this rule */
4664468Seric 		mlp = mlist;
4678058Seric 		rvp = rwr->r_lhs;
4688058Seric 		avp = pvp;
4698058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
4703149Seric 		{
4713149Seric 			rp = *rvp;
4728058Seric # ifdef DEBUG
4738058Seric 			if (tTd(21, 35))
4748058Seric 			{
4758069Seric 				printf("ap=");
4768058Seric 				xputs(ap);
4778069Seric 				printf(", rp=");
4788058Seric 				xputs(rp);
4798069Seric 				printf("\n");
4808058Seric 			}
4818058Seric # endif DEBUG
4823149Seric 			if (rp == NULL)
483297Seric 			{
4843149Seric 				/* end-of-pattern before end-of-address */
4858058Seric 				goto backup;
486297Seric 			}
4878058Seric 			if (ap == NULL && *rp != MATCHZANY)
4888058Seric 			{
4898058Seric 				/* end-of-input */
4908058Seric 				break;
4918058Seric 			}
4923149Seric 
4933149Seric 			switch (*rp)
4943149Seric 			{
4954060Seric 				register STAB *s;
4964060Seric 				register int class;
4974060Seric 
4984060Seric 			  case MATCHCLASS:
4994060Seric 				/* match any token in a class */
5004060Seric 				class = rp[1];
5014060Seric 				if (!isalpha(class))
5028058Seric 					goto backup;
5034060Seric 				if (isupper(class))
5044060Seric 					class -= 'A';
5054060Seric 				else
5064060Seric 					class -= 'a';
5074100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
5086273Seric 				if (s == NULL || (s->s_class & (1L << class)) == 0)
5098058Seric 					goto backup;
5104468Seric 
5114476Seric 				/* explicit fall-through */
5124476Seric 
5134476Seric 			  case MATCHONE:
5144476Seric 			  case MATCHANY:
5154476Seric 				/* match exactly one token */
5168058Seric 				mlp->first = avp;
5178058Seric 				mlp->last = avp++;
5184468Seric 				mlp++;
5194060Seric 				break;
5204060Seric 
5218058Seric 			  case MATCHZANY:
5228058Seric 				/* match zero or more tokens */
5238058Seric 				mlp->first = avp;
5248058Seric 				mlp->last = avp - 1;
5258058Seric 				mlp++;
5268058Seric 				break;
5278058Seric 
5283149Seric 			  default:
5293149Seric 				/* must have exact match */
5304060Seric 				if (!sameword(rp, ap))
5318058Seric 					goto backup;
5324468Seric 				avp++;
5333149Seric 				break;
5343149Seric 			}
5353149Seric 
5363149Seric 			/* successful match on this token */
5373149Seric 			rvp++;
5383149Seric 			continue;
5393149Seric 
5408058Seric 		  backup:
5413149Seric 			/* match failed -- back up */
5423149Seric 			while (--rvp >= rwr->r_lhs)
5433149Seric 			{
5443149Seric 				rp = *rvp;
5458058Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
5464468Seric 				{
5474476Seric 					/* extend binding and continue */
5488058Seric 					avp = ++mlp[-1].last;
5498058Seric 					avp++;
5504476Seric 					rvp++;
5513149Seric 					break;
5524468Seric 				}
5534476Seric 				avp--;
5544476Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS)
5553149Seric 				{
5564468Seric 					/* back out binding */
5574468Seric 					mlp--;
5583149Seric 				}
5593149Seric 			}
5603149Seric 
5613149Seric 			if (rvp < rwr->r_lhs)
5623149Seric 			{
5633149Seric 				/* total failure to match */
5643149Seric 				break;
5653149Seric 			}
566297Seric 		}
5673149Seric 
5683149Seric 		/*
5693149Seric 		**  See if we successfully matched
5703149Seric 		*/
5713149Seric 
5723149Seric 		if (rvp >= rwr->r_lhs && *rvp == NULL)
5733149Seric 		{
5748058Seric 			rvp = rwr->r_rhs;
5754100Seric # ifdef DEBUG
5767675Seric 			if (tTd(21, 12))
5773149Seric 			{
5788069Seric 				printf("-----rule matches:");
5798058Seric 				printav(rvp);
5803149Seric 			}
5814100Seric # endif DEBUG
5823149Seric 
5838058Seric 			/* see if this is a "subroutine" call */
5848058Seric 			rp = *rvp;
5858058Seric 			if (*rp == CALLSUBR)
5868058Seric 			{
5878058Seric 				rp = *++rvp;
5888058Seric # ifdef DEBUG
5898069Seric 				if (tTd(21, 3))
5908058Seric 					printf("-----callsubr %s\n", rp);
5918058Seric # endif DEBUG
5928058Seric 				rewrite(pvp, atoi(rp));
5938058Seric 				rwr = rwr->r_next;
5948058Seric 				continue;
5958058Seric 			}
5968069Seric 			else if (*rp == CANONUSER)
5978069Seric 			{
5988069Seric 				rvp++;
5998069Seric 				rwr = rwr->r_next;
6008069Seric 			}
6018069Seric 			else if (*rp == CANONHOST)
6028069Seric 			{
6038069Seric 				rvp++;
6048069Seric 				rwr = NULL;
6058069Seric 			}
6068069Seric 			else if (*rp == CANONNET)
6078069Seric 				rwr = NULL;
6088058Seric 
6093149Seric 			/* substitute */
6108069Seric 			for (avp = npvp; *rvp != NULL; rvp++)
6113149Seric 			{
6123149Seric 				rp = *rvp;
6134468Seric 				if (*rp == MATCHREPL)
6143149Seric 				{
6153149Seric 					register struct match *m;
6163149Seric 					register char **pp;
6173149Seric 
6184468Seric 					m = &mlist[rp[1] - '1'];
6194476Seric # ifdef DEBUG
6207675Seric 					if (tTd(21, 15))
6214476Seric 					{
6224476Seric 						printf("$%c:", rp[1]);
6234476Seric 						pp = m->first;
6248058Seric 						while (pp <= m->last)
6254476Seric 						{
6264476Seric 							printf(" %x=\"", *pp);
6274625Seric 							(void) fflush(stdout);
6288058Seric 							printf("%s\"", *pp++);
6298058Seric 						}
6304476Seric 						printf("\n");
6314476Seric 					}
6324476Seric # endif DEBUG
6334468Seric 					pp = m->first;
6348058Seric 					while (pp <= m->last)
6353149Seric 					{
6364468Seric 						if (avp >= &npvp[MAXATOM])
6373149Seric 						{
6384468Seric 							syserr("rewrite: expansion too long");
6394468Seric 							return;
6404468Seric 						}
6418058Seric 						*avp++ = *pp++;
6428058Seric 					}
6433149Seric 				}
6443149Seric 				else
6454385Seric 				{
6464385Seric 					if (avp >= &npvp[MAXATOM])
6474385Seric 					{
6484385Seric 						syserr("rewrite: expansion too long");
6494385Seric 						return;
6504385Seric 					}
6513149Seric 					*avp++ = rp;
6524385Seric 				}
6533149Seric 			}
6543149Seric 			*avp++ = NULL;
6554085Seric 			bmove((char *) npvp, (char *) pvp, (avp - npvp) * sizeof *avp);
6563149Seric # 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 
6748069Seric # ifdef DEBUG
6758069Seric 	if (tTd(21, 2))
6768069Seric 	{
6778069Seric 		printf("rewrite: ruleset %d returns:", ruleset);
6788069Seric 		printav(pvp);
6798069Seric 	}
6808069Seric # endif DEBUG
6813149Seric }
6823149Seric /*
6833149Seric **  BUILDADDR -- build address from token vector.
6843149Seric **
6853149Seric **	Parameters:
6863149Seric **		tv -- token vector.
6873149Seric **		a -- pointer to address descriptor to fill.
6883149Seric **			If NULL, one will be allocated.
6893149Seric **
6903149Seric **	Returns:
6914279Seric **		NULL if there was an error.
6924279Seric **		'a' otherwise.
6933149Seric **
6943149Seric **	Side Effects:
6953149Seric **		fills in 'a'
6963149Seric */
6973149Seric 
6983149Seric ADDRESS *
6993149Seric buildaddr(tv, a)
7003149Seric 	register char **tv;
7013149Seric 	register ADDRESS *a;
7023149Seric {
7033149Seric 	static char buf[MAXNAME];
7043149Seric 	struct mailer **mp;
7053149Seric 	register struct mailer *m;
7064635Seric 	extern bool sameword();
7073149Seric 
7083149Seric 	if (a == NULL)
7093149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
7104988Seric 	clear((char *) a, sizeof *a);
7113149Seric 
7123149Seric 	/* figure out what net/mailer to use */
7133149Seric 	if (**tv != CANONNET)
7144279Seric 	{
7153149Seric 		syserr("buildaddr: no net");
7164279Seric 		return (NULL);
7174279Seric 	}
7183149Seric 	tv++;
7194635Seric 	if (sameword(*tv, "error"))
7204279Seric 	{
7214279Seric 		if (**++tv != CANONUSER)
7224279Seric 			syserr("buildaddr: error: no user");
7234279Seric 		buf[0] = '\0';
7244279Seric 		while (*++tv != NULL)
7254279Seric 		{
7264279Seric 			if (buf[0] != '\0')
7277005Seric 				(void) strcat(buf, " ");
7287005Seric 			(void) strcat(buf, *tv);
7294279Seric 		}
7304279Seric 		usrerr(buf);
7314279Seric 		return (NULL);
7324279Seric 	}
7334598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
7343149Seric 	{
7354635Seric 		if (sameword(m->m_name, *tv))
7363149Seric 			break;
7373149Seric 	}
7383149Seric 	if (m == NULL)
7394279Seric 	{
7403149Seric 		syserr("buildaddr: unknown net %s", *tv);
7414279Seric 		return (NULL);
7424279Seric 	}
7434598Seric 	a->q_mailer = m;
7443149Seric 
7453149Seric 	/* figure out what host (if any) */
7463149Seric 	tv++;
7474195Seric 	if (!bitset(M_LOCAL, m->m_flags))
7483149Seric 	{
7495704Seric 		if (**tv++ != CANONHOST)
7504279Seric 		{
7513149Seric 			syserr("buildaddr: no host");
7524279Seric 			return (NULL);
7534279Seric 		}
7545704Seric 		buf[0] = '\0';
7555704Seric 		while (*tv != NULL && **tv != CANONUSER)
7567005Seric 			(void) strcat(buf, *tv++);
7575704Seric 		a->q_host = newstr(buf);
7583149Seric 	}
7593149Seric 	else
7603149Seric 		a->q_host = NULL;
7613149Seric 
7623149Seric 	/* figure out the user */
7633149Seric 	if (**tv != CANONUSER)
7644279Seric 	{
7653149Seric 		syserr("buildaddr: no user");
7664279Seric 		return (NULL);
7674279Seric 	}
7684228Seric 	cataddr(++tv, buf, sizeof buf);
7693149Seric 	a->q_user = buf;
7703149Seric 
7713149Seric 	return (a);
7723149Seric }
7733188Seric /*
7744228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
7754228Seric **
7764228Seric **	Parameters:
7774228Seric **		pvp -- parameter vector to rebuild.
7784228Seric **		buf -- buffer to build the string into.
7794228Seric **		sz -- size of buf.
7804228Seric **
7814228Seric **	Returns:
7824228Seric **		none.
7834228Seric **
7844228Seric **	Side Effects:
7854228Seric **		Destroys buf.
7864228Seric */
7874228Seric 
7884228Seric cataddr(pvp, buf, sz)
7894228Seric 	char **pvp;
7904228Seric 	char *buf;
7914228Seric 	register int sz;
7924228Seric {
7934228Seric 	bool oatomtok = FALSE;
7944228Seric 	bool natomtok = FALSE;
7954228Seric 	register int i;
7964228Seric 	register char *p;
7974228Seric 
7984228Seric 	p = buf;
7994228Seric 	sz--;
8004228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
8014228Seric 	{
8028078Seric 		natomtok = (toktype(**pvp) == ATM);
8034228Seric 		if (oatomtok && natomtok)
8044228Seric 			*p++ = SPACESUB;
8054228Seric 		(void) strcpy(p, *pvp);
8064228Seric 		oatomtok = natomtok;
8074228Seric 		p += i;
8084228Seric 		sz -= i;
8094228Seric 		pvp++;
8104228Seric 	}
8114228Seric 	*p = '\0';
8124228Seric }
8134228Seric /*
8143188Seric **  SAMEADDR -- Determine if two addresses are the same
8153188Seric **
8163188Seric **	This is not just a straight comparison -- if the mailer doesn't
8173188Seric **	care about the host we just ignore it, etc.
8183188Seric **
8193188Seric **	Parameters:
8203188Seric **		a, b -- pointers to the internal forms to compare.
8213188Seric **		wildflg -- if TRUE, 'a' may have no user specified,
8223188Seric **			in which case it is to match anything.
8233188Seric **
8243188Seric **	Returns:
8253188Seric **		TRUE -- they represent the same mailbox.
8263188Seric **		FALSE -- they don't.
8273188Seric **
8283188Seric **	Side Effects:
8293188Seric **		none.
8303188Seric */
8313188Seric 
8323188Seric bool
8333188Seric sameaddr(a, b, wildflg)
8343188Seric 	register ADDRESS *a;
8353188Seric 	register ADDRESS *b;
8363188Seric 	bool wildflg;
8373188Seric {
8383188Seric 	/* if they don't have the same mailer, forget it */
8393188Seric 	if (a->q_mailer != b->q_mailer)
8403188Seric 		return (FALSE);
8413188Seric 
8423188Seric 	/* if the user isn't the same, we can drop out */
8433188Seric 	if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0)
8443188Seric 		return (FALSE);
8453188Seric 
8463188Seric 	/* if the mailer ignores hosts, we have succeeded! */
8474598Seric 	if (bitset(M_LOCAL, a->q_mailer->m_flags))
8483188Seric 		return (TRUE);
8493188Seric 
8503188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
8513188Seric 	if (a->q_host == NULL || b->q_host == NULL)
8523188Seric 		return (FALSE);
8533188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
8543188Seric 		return (FALSE);
8553188Seric 
8563188Seric 	return (TRUE);
8573188Seric }
8583234Seric /*
8593234Seric **  PRINTADDR -- print address (for debugging)
8603234Seric **
8613234Seric **	Parameters:
8623234Seric **		a -- the address to print
8633234Seric **		follow -- follow the q_next chain.
8643234Seric **
8653234Seric **	Returns:
8663234Seric **		none.
8673234Seric **
8683234Seric **	Side Effects:
8693234Seric **		none.
8703234Seric */
8713234Seric 
8724317Seric # ifdef DEBUG
8734317Seric 
8743234Seric printaddr(a, follow)
8753234Seric 	register ADDRESS *a;
8763234Seric 	bool follow;
8773234Seric {
8785001Seric 	bool first = TRUE;
8795001Seric 
8803234Seric 	while (a != NULL)
8813234Seric 	{
8825001Seric 		first = FALSE;
8834443Seric 		printf("%x=", a);
8844085Seric 		(void) fflush(stdout);
8853234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
886*8181Seric 		       a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host,
887*8181Seric 		       a->q_user);
888*8181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
889*8181Seric 		       a->q_alias);
890*8181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
891*8181Seric 		       a->q_fullname);
8924996Seric 
8933234Seric 		if (!follow)
8943234Seric 			return;
8954996Seric 		a = a->q_next;
8963234Seric 	}
8975001Seric 	if (first)
8984443Seric 		printf("[NULL]\n");
8993234Seric }
9004317Seric 
9014317Seric # endif DEBUG
9027682Seric /*
9037682Seric **  REMOTENAME -- return the name relative to the current mailer
9047682Seric **
9057682Seric **	Parameters:
9067682Seric **		name -- the name to translate.
9078069Seric **		m -- the mailer that we want to do rewriting relative
9088069Seric **			to.
9098069Seric **		senderaddress -- if set, uses the sender rewriting rules
9108069Seric **			rather than the recipient rewriting rules.
9117682Seric **
9127682Seric **	Returns:
9137682Seric **		the text string representing this address relative to
9147682Seric **			the receiving mailer.
9157682Seric **
9167682Seric **	Side Effects:
9177682Seric **		none.
9187682Seric **
9197682Seric **	Warnings:
9207682Seric **		The text string returned is tucked away locally;
9217682Seric **			copy it if you intend to save it.
9227682Seric */
9237682Seric 
9247682Seric char *
9258069Seric remotename(name, m, senderaddress)
9267682Seric 	char *name;
9277682Seric 	struct mailer *m;
9288069Seric 	bool senderaddress;
9297682Seric {
9308069Seric 	register char **pvp;
9318069Seric 	char *fancy;
9328069Seric 	extern char *macvalue();
933*8181Seric 	char *oldg = macvalue('g', CurEnv);
9347682Seric 	static char buf[MAXNAME];
9357682Seric 	char lbuf[MAXNAME];
9367682Seric 	extern char **prescan();
9377889Seric 	extern char *crackaddr();
9387682Seric 
9397755Seric # ifdef DEBUG
9407755Seric 	if (tTd(12, 1))
9417755Seric 		printf("remotename(%s)\n", name);
9427755Seric # endif DEBUG
9437755Seric 
9447682Seric 	/*
945*8181Seric 	**  Do a heuristic crack of this name to extract any comment info.
946*8181Seric 	**	This will leave the name as a comment and a $g macro.
9477889Seric 	*/
9487889Seric 
9497889Seric 	fancy = crackaddr(name);
9507889Seric 
951*8181Seric 	/*
952*8181Seric 	**  Turn the name into canonical form.
953*8181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
954*8181Seric 	**	If this only resolves to "user", and the "C" flag is
955*8181Seric 	**	specified in the sending mailer, then the sender's
956*8181Seric 	**	domain will be appended.
957*8181Seric 	*/
958*8181Seric 
9597889Seric 	pvp = prescan(name, '\0');
9607889Seric 	if (pvp == NULL)
9617889Seric 		return (name);
962*8181Seric 	rewrite(pvp, 3);
963*8181Seric 	if (CurEnv->e_fromdomain != NULL)
964*8181Seric 	{
965*8181Seric 		/* append from domain to this address */
966*8181Seric 		register char **pxp = pvp;
967*8181Seric 
968*8181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
969*8181Seric 			pxp++;
970*8181Seric 		if (*pxp == NULL)
971*8181Seric 		{
972*8181Seric 			register char **qxq = CurEnv->e_fromdomain;
973*8181Seric 
974*8181Seric 			while (*qxq != NULL)
975*8181Seric 				*pxp++ = *qxq++;
976*8181Seric 		}
977*8181Seric 	}
978*8181Seric 
979*8181Seric 	/*
980*8181Seric 	**  Now do more specific rewriting.
981*8181Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
982*8181Seric 	**		a sender address or not.
983*8181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
984*8181Seric 	*/
985*8181Seric 
9868069Seric 	if (senderaddress)
9877755Seric 	{
9887889Seric 		rewrite(pvp, 1);
9898069Seric 		if (m->m_s_rwset > 0)
9908069Seric 			rewrite(pvp, m->m_s_rwset);
9918069Seric 	}
9928069Seric 	else
9938069Seric 	{
9947889Seric 		rewrite(pvp, 2);
9958069Seric 		if (m->m_r_rwset > 0)
9968069Seric 			rewrite(pvp, m->m_r_rwset);
9977682Seric 	}
9987682Seric 
999*8181Seric 	/*
1000*8181Seric 	**  Now restore the comment information we had at the beginning.
1001*8181Seric 	*/
1002*8181Seric 
10037682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
10047682Seric 	define('g', lbuf);
10057889Seric 	expand(fancy, buf, &buf[sizeof buf - 1], CurEnv);
10067682Seric 	define('g', oldg);
10077682Seric 
10087682Seric # ifdef DEBUG
10097682Seric 	if (tTd(12, 1))
10107755Seric 		printf("remotename => `%s'\n", buf);
10117682Seric # endif DEBUG
10127682Seric 	return (buf);
10137682Seric }
10147682Seric /*
10157682Seric **  CANONNAME -- make name canonical
10167682Seric **
10177682Seric **	This is used for SMTP and misc. printing.  Given a print
1018*8181Seric **	address, it strips out comments, etc.
10197682Seric **
10207682Seric **	Parameters:
10217682Seric **		name -- the name to make canonical.
10227682Seric **
10237682Seric **	Returns:
10247682Seric **		pointer to canonical name.
10257682Seric **
10267682Seric **	Side Effects:
10277682Seric **		none.
10287682Seric **
10297682Seric **	Warning:
10307682Seric **		result is saved in static buf; future calls will trash it.
10317682Seric */
10327682Seric 
10337682Seric char *
10347682Seric canonname(name)
10357682Seric 	char *name;
10367682Seric {
10378069Seric 	static char nbuf[MAXNAME];
10387940Seric 	register char **pvp;
10397682Seric 
10407940Seric 	pvp = prescan(name, '\0');
10418069Seric 	rewrite(pvp, 3);
10428069Seric 	cataddr(pvp, nbuf, sizeof nbuf);
10437682Seric 	return (nbuf);
10447682Seric }
1045