13312Seric # include "sendmail.h"
2297Seric 
3*9346Seric SCCSID(@(#)parseaddr.c	3.66		11/24/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();
53297Seric 
54297Seric 	/*
55297Seric 	**  Initialize and prescan address.
56297Seric 	*/
57297Seric 
586903Seric 	CurEnv->e_to = addr;
593188Seric # ifdef DEBUG
607675Seric 	if (tTd(20, 1))
613188Seric 		printf("\n--parse(%s)\n", addr);
623188Seric # endif DEBUG
633188Seric 
648078Seric 	pvp = prescan(addr, ',');
653149Seric 	if (pvp == NULL)
66297Seric 		return (NULL);
67297Seric 
68297Seric 	/*
693149Seric 	**  Apply rewriting rules.
707889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
71297Seric 	*/
72297Seric 
738181Seric 	rewrite(pvp, 3);
744070Seric 	rewrite(pvp, 0);
75297Seric 
763149Seric 	/*
773149Seric 	**  See if we resolved to a real mailer.
783149Seric 	*/
79297Seric 
803149Seric 	if (pvp[0][0] != CANONNET)
813149Seric 	{
823149Seric 		setstat(EX_USAGE);
833149Seric 		usrerr("cannot resolve name");
843149Seric 		return (NULL);
85297Seric 	}
86297Seric 
87297Seric 	/*
883149Seric 	**  Build canonical address from pvp.
89297Seric 	*/
90297Seric 
913149Seric 	a = buildaddr(pvp, a);
924279Seric 	if (a == NULL)
934279Seric 		return (NULL);
944598Seric 	m = a->q_mailer;
95297Seric 
96297Seric 	/*
973149Seric 	**  Make local copies of the host & user and then
983149Seric 	**  transport them out.
99297Seric 	*/
100297Seric 
101297Seric 	if (copyf > 0)
1028078Seric 	{
1038078Seric 		extern char *DelimChar;
1048078Seric 		char savec = *DelimChar;
1058078Seric 
1068078Seric 		*DelimChar = '\0';
1072973Seric 		a->q_paddr = newstr(addr);
1088078Seric 		*DelimChar = savec;
1098078Seric 	}
110297Seric 	else
111297Seric 		a->q_paddr = addr;
1123149Seric 	if (copyf >= 0)
113297Seric 	{
1143149Seric 		if (a->q_host != NULL)
1153149Seric 			a->q_host = newstr(a->q_host);
116297Seric 		else
1173149Seric 			a->q_host = "";
1183149Seric 		if (a->q_user != a->q_paddr)
1193149Seric 			a->q_user = newstr(a->q_user);
120297Seric 	}
121297Seric 
122297Seric 	/*
123297Seric 	**  Do UPPER->lower case mapping unless inhibited.
124297Seric 	*/
125297Seric 
1263149Seric 	if (!bitset(M_HST_UPPER, m->m_flags))
127297Seric 		makelower(a->q_host);
1283149Seric 	if (!bitset(M_USR_UPPER, m->m_flags))
129297Seric 		makelower(a->q_user);
130297Seric 
131297Seric 	/*
132297Seric 	**  Compute return value.
133297Seric 	*/
134297Seric 
135297Seric # ifdef DEBUG
1367675Seric 	if (tTd(20, 1))
1374443Seric 	{
1384443Seric 		printf("parse-->");
1394443Seric 		printaddr(a, FALSE);
1404443Seric 	}
141297Seric # endif DEBUG
142297Seric 
143297Seric 	return (a);
144297Seric }
145297Seric /*
146297Seric **  PRESCAN -- Prescan name and make it canonical
147297Seric **
148297Seric **	Scans a name and turns it into canonical form.  This involves
149297Seric **	deleting blanks, comments (in parentheses), and turning the
150297Seric **	word "at" into an at-sign ("@").  The name is copied as this
151297Seric **	is done; it is legal to copy a name onto itself, since this
152297Seric **	process can only make things smaller.
153297Seric **
154297Seric **	This routine knows about quoted strings and angle brackets.
155297Seric **
156297Seric **	There are certain subtleties to this routine.  The one that
157297Seric **	comes to mind now is that backslashes on the ends of names
158297Seric **	are silently stripped off; this is intentional.  The problem
159297Seric **	is that some versions of sndmsg (like at LBL) set the kill
160297Seric **	character to something other than @ when reading addresses;
161297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
162297Seric **	berknet mailer.
163297Seric **
164297Seric **	Parameters:
165297Seric **		addr -- the name to chomp.
166297Seric **		delim -- the delimiter for the address, normally
167297Seric **			'\0' or ','; \0 is accepted in any case.
168297Seric **
169297Seric **	Returns:
1703149Seric **		A pointer to a vector of tokens.
171297Seric **		NULL on error.
172297Seric **
173297Seric **	Side Effects:
1743149Seric **		none.
175297Seric */
176297Seric 
1778078Seric /* states and character types */
1788078Seric # define OPR		0	/* operator */
1798078Seric # define ATM		1	/* atom */
1808078Seric # define QST		2	/* in quoted string */
1818078Seric # define SPC		3	/* chewing up spaces */
1828078Seric # define ONE		4	/* pick up one character */
1833149Seric 
1848078Seric # define NSTATES	5	/* number of states */
1858078Seric # define TYPE		017	/* mask to select state type */
1868078Seric 
1878078Seric /* meta bits for table */
1888078Seric # define M		020	/* meta character; don't pass through */
1898078Seric # define B		040	/* cause a break */
1908078Seric # define MB		M|B	/* meta-break */
1918078Seric 
1928078Seric static short StateTab[NSTATES][NSTATES] =
1938078Seric {
1948087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
1959051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
1969051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
1979051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
1988078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
1998078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2008078Seric };
2018078Seric 
2028078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2038078Seric 
2048078Seric char	*DelimChar;		/* set to point to the delimiter */
2058078Seric 
2063149Seric char **
2073149Seric prescan(addr, delim)
208297Seric 	char *addr;
209297Seric 	char delim;
210297Seric {
211297Seric 	register char *p;
2128078Seric 	register char *q;
213*9346Seric 	register int c;
2143149Seric 	char **avp;
215297Seric 	bool bslashmode;
216297Seric 	int cmntcnt;
2178423Seric 	int anglecnt;
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;
2278423Seric 	anglecnt = 0;
2283149Seric 	avp = av;
2298078Seric 	state = OPR;
2308078Seric 	c = NOCHAR;
2318078Seric 	p = addr;
2328078Seric # ifdef DEBUG
2338078Seric 	if (tTd(22, 45))
234297Seric 	{
2358078Seric 		printf("prescan: ");
2368078Seric 		xputs(p);
2378078Seric 		putchar('\n');
2388078Seric 	}
2398078Seric # endif DEBUG
2408078Seric 
2418078Seric 	do
2428078Seric 	{
2433149Seric 		/* read a token */
2443149Seric 		tok = q;
2458078Seric 		for (;;)
246297Seric 		{
2478078Seric 			/* store away any old lookahead character */
2488078Seric 			if (c != NOCHAR)
2498078Seric 			{
2508078Seric 				/* squirrel it away */
2518078Seric 				if (q >= &buf[sizeof buf - 5])
2528078Seric 				{
2538078Seric 					usrerr("Address too long");
2548078Seric 					DelimChar = p;
2558078Seric 					return (NULL);
2568078Seric 				}
2578078Seric 				*q++ = c;
2588078Seric 			}
2598078Seric 
2608078Seric 			/* read a new input character */
2618078Seric 			c = *p++;
2628078Seric 			if (c == '\0')
2638078Seric 				break;
2648078Seric # ifdef DEBUG
2658078Seric 			if (tTd(22, 101))
2668078Seric 				printf("c=%c, s=%d; ", c, state);
2678078Seric # endif DEBUG
2688078Seric 
2693149Seric 			/* chew up special characters */
2704100Seric 			c &= ~0200;
2713149Seric 			*q = '\0';
2723149Seric 			if (bslashmode)
2733149Seric 			{
2743149Seric 				c |= 0200;
2753149Seric 				bslashmode = FALSE;
2763149Seric 			}
2773149Seric 			else if (c == '\\')
2783149Seric 			{
2793149Seric 				bslashmode = TRUE;
2808078Seric 				c = NOCHAR;
2813149Seric 			}
2828514Seric 			else if (state == QST)
2838514Seric 			{
2848514Seric 				/* do nothing, just avoid next clauses */
2858514Seric 			}
2868078Seric 			else if (c == '(')
2874100Seric 			{
2888078Seric 				cmntcnt++;
2898078Seric 				c = NOCHAR;
2904100Seric 			}
2918078Seric 			else if (c == ')')
2923149Seric 			{
2938078Seric 				if (cmntcnt <= 0)
2943149Seric 				{
2958078Seric 					usrerr("Unbalanced ')'");
2968078Seric 					DelimChar = p;
2978078Seric 					return (NULL);
2983149Seric 				}
2998078Seric 				else
3008078Seric 					cmntcnt--;
3018078Seric 			}
3028078Seric 			else if (cmntcnt > 0)
3038078Seric 				c = NOCHAR;
3048423Seric 			else if (c == '<')
3058423Seric 				anglecnt++;
3068423Seric 			else if (c == '>')
3078423Seric 			{
3088423Seric 				if (anglecnt <= 0)
3098423Seric 				{
3108423Seric 					usrerr("Unbalanced '>'");
3118423Seric 					DelimChar = p;
3128423Seric 					return (NULL);
3138423Seric 				}
3148423Seric 				anglecnt--;
3158423Seric 			}
3163149Seric 
3178078Seric 			if (c == NOCHAR)
3188078Seric 				continue;
3193149Seric 
3208078Seric 			/* see if this is end of input */
3218423Seric 			if (c == delim && anglecnt <= 0)
3223149Seric 				break;
3233149Seric 
3248078Seric 			newstate = StateTab[state][toktype(c)];
3258078Seric # ifdef DEBUG
3268078Seric 			if (tTd(22, 101))
3278078Seric 				printf("ns=%02o\n", newstate);
3288078Seric # endif DEBUG
3298078Seric 			state = newstate & TYPE;
3308078Seric 			if (bitset(M, newstate))
3318078Seric 				c = NOCHAR;
3328078Seric 			if (bitset(B, newstate))
3334228Seric 				break;
334297Seric 		}
3353149Seric 
3363149Seric 		/* new token */
3378078Seric 		if (tok != q)
3381378Seric 		{
3398078Seric 			*q++ = '\0';
3408078Seric # ifdef DEBUG
3418078Seric 			if (tTd(22, 36))
342297Seric 			{
3438078Seric 				printf("tok=");
3448078Seric 				xputs(tok);
3458078Seric 				putchar('\n');
346297Seric 			}
3478078Seric # endif DEBUG
3488078Seric 			if (avp >= &av[MAXATOM])
349297Seric 			{
3508078Seric 				syserr("prescan: too many tokens");
3518078Seric 				DelimChar = p;
3528078Seric 				return (NULL);
353297Seric 			}
3548078Seric 			*avp++ = tok;
355297Seric 		}
3568423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
3573149Seric 	*avp = NULL;
3588078Seric 	DelimChar = --p;
3593149Seric 	if (cmntcnt > 0)
3603149Seric 		usrerr("Unbalanced '('");
3618423Seric 	else if (anglecnt > 0)
3628423Seric 		usrerr("Unbalanced '<'");
3638078Seric 	else if (state == QST)
3643149Seric 		usrerr("Unbalanced '\"'");
3653149Seric 	else if (av[0] != NULL)
3663149Seric 		return (av);
3673149Seric 	return (NULL);
3683149Seric }
3693149Seric /*
3703149Seric **  TOKTYPE -- return token type
3713149Seric **
3723149Seric **	Parameters:
3733149Seric **		c -- the character in question.
3743149Seric **
3753149Seric **	Returns:
3763149Seric **		Its type.
3773149Seric **
3783149Seric **	Side Effects:
3793149Seric **		none.
3803149Seric */
381297Seric 
3823149Seric toktype(c)
3833149Seric 	register char c;
3843149Seric {
3853380Seric 	static char buf[50];
3863382Seric 	static bool firstime = TRUE;
3873380Seric 
3883382Seric 	if (firstime)
3893380Seric 	{
3903382Seric 		firstime = FALSE;
3916977Seric 		expand("$o", buf, &buf[sizeof buf - 1], CurEnv);
3927005Seric 		(void) strcat(buf, DELIMCHARS);
3933380Seric 	}
3946053Seric 	if (c == MATCHCLASS || c == MATCHREPL)
3958078Seric 		return (ONE);
3968078Seric 	if (c == '"')
3978078Seric 		return (QST);
3984100Seric 	if (!isascii(c))
3998078Seric 		return (ATM);
4008078Seric 	if (isspace(c) || c == ')')
4018078Seric 		return (SPC);
4023380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
4038078Seric 		return (OPR);
4048078Seric 	return (ATM);
4053149Seric }
4063149Seric /*
4073149Seric **  REWRITE -- apply rewrite rules to token vector.
4083149Seric **
4094476Seric **	This routine is an ordered production system.  Each rewrite
4104476Seric **	rule has a LHS (called the pattern) and a RHS (called the
4114476Seric **	rewrite); 'rwr' points the the current rewrite rule.
4124476Seric **
4134476Seric **	For each rewrite rule, 'avp' points the address vector we
4144476Seric **	are trying to match against, and 'pvp' points to the pattern.
4158058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
4168058Seric **	MATCHONE, MATCHCLASS) then the address in avp matched is
4178058Seric **	saved away in the match vector (pointed to by 'mvp').
4184476Seric **
4194476Seric **	When a match between avp & pvp does not match, we try to
4204476Seric **	back out.  If we back up over a MATCHONE or a MATCHCLASS
4214476Seric **	we must also back out the match in mvp.  If we reach a
4228058Seric **	MATCHANY or MATCHZANY we just extend the match and start
4238058Seric **	over again.
4244476Seric **
4254476Seric **	When we finally match, we rewrite the address vector
4264476Seric **	and try over again.
4274476Seric **
4283149Seric **	Parameters:
4293149Seric **		pvp -- pointer to token vector.
4303149Seric **
4313149Seric **	Returns:
4323149Seric **		none.
4333149Seric **
4343149Seric **	Side Effects:
4353149Seric **		pvp is modified.
4363149Seric */
4372091Seric 
4383149Seric struct match
4393149Seric {
4404468Seric 	char	**first;	/* first token matched */
4414468Seric 	char	**last;		/* last token matched */
4423149Seric };
4433149Seric 
4444468Seric # define MAXMATCH	9	/* max params per rewrite */
4453149Seric 
4463149Seric 
4474070Seric rewrite(pvp, ruleset)
4483149Seric 	char **pvp;
4494070Seric 	int ruleset;
4503149Seric {
4513149Seric 	register char *ap;		/* address pointer */
4523149Seric 	register char *rp;		/* rewrite pointer */
4533149Seric 	register char **avp;		/* address vector pointer */
4543149Seric 	register char **rvp;		/* rewrite vector pointer */
4558058Seric 	register struct match *mlp;	/* cur ptr into mlist */
4568058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
4574468Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
4583149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
4594060Seric 	extern bool sameword();
4603149Seric 
4619279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
4623149Seric 	{
4638959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
4643149Seric 		printav(pvp);
4653149Seric 	}
4668423Seric 	if (pvp == NULL)
4678423Seric 		return;
4683149Seric 
4693149Seric 	/*
4703149Seric 	**  Run through the list of rewrite rules, applying
4713149Seric 	**	any that match.
4723149Seric 	*/
4733149Seric 
4744070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
4753149Seric 	{
4764100Seric # ifdef DEBUG
4777675Seric 		if (tTd(21, 12))
478297Seric 		{
4798069Seric 			printf("-----trying rule:");
4803149Seric 			printav(rwr->r_lhs);
4813149Seric 		}
4824100Seric # endif DEBUG
4833149Seric 
4843149Seric 		/* try to match on this rule */
4854468Seric 		mlp = mlist;
4868058Seric 		rvp = rwr->r_lhs;
4878058Seric 		avp = pvp;
4888058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
4893149Seric 		{
4903149Seric 			rp = *rvp;
4918058Seric # ifdef DEBUG
4928058Seric 			if (tTd(21, 35))
4938058Seric 			{
4948069Seric 				printf("ap=");
4958058Seric 				xputs(ap);
4968069Seric 				printf(", rp=");
4978058Seric 				xputs(rp);
4988069Seric 				printf("\n");
4998058Seric 			}
5008058Seric # endif DEBUG
5013149Seric 			if (rp == NULL)
502297Seric 			{
5033149Seric 				/* end-of-pattern before end-of-address */
5048058Seric 				goto backup;
505297Seric 			}
5068058Seric 			if (ap == NULL && *rp != MATCHZANY)
5078058Seric 			{
5088058Seric 				/* end-of-input */
5098058Seric 				break;
5108058Seric 			}
5113149Seric 
5123149Seric 			switch (*rp)
5133149Seric 			{
5144060Seric 				register STAB *s;
5154060Seric 				register int class;
5164060Seric 
5174060Seric 			  case MATCHCLASS:
5184060Seric 				/* match any token in a class */
5194060Seric 				class = rp[1];
5204060Seric 				if (!isalpha(class))
5218058Seric 					goto backup;
5224060Seric 				if (isupper(class))
5234060Seric 					class -= 'A';
5244060Seric 				else
5254060Seric 					class -= 'a';
5264100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
5276273Seric 				if (s == NULL || (s->s_class & (1L << class)) == 0)
5288058Seric 					goto backup;
5294468Seric 
5304476Seric 				/* explicit fall-through */
5314476Seric 
5324476Seric 			  case MATCHONE:
5334476Seric 			  case MATCHANY:
5344476Seric 				/* match exactly one token */
5358058Seric 				mlp->first = avp;
5368058Seric 				mlp->last = avp++;
5374468Seric 				mlp++;
5384060Seric 				break;
5394060Seric 
5408058Seric 			  case MATCHZANY:
5418058Seric 				/* match zero or more tokens */
5428058Seric 				mlp->first = avp;
5438058Seric 				mlp->last = avp - 1;
5448058Seric 				mlp++;
5458058Seric 				break;
5468058Seric 
5473149Seric 			  default:
5483149Seric 				/* must have exact match */
5494060Seric 				if (!sameword(rp, ap))
5508058Seric 					goto backup;
5514468Seric 				avp++;
5523149Seric 				break;
5533149Seric 			}
5543149Seric 
5553149Seric 			/* successful match on this token */
5563149Seric 			rvp++;
5573149Seric 			continue;
5583149Seric 
5598058Seric 		  backup:
5603149Seric 			/* match failed -- back up */
5613149Seric 			while (--rvp >= rwr->r_lhs)
5623149Seric 			{
5633149Seric 				rp = *rvp;
5648058Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
5654468Seric 				{
5664476Seric 					/* extend binding and continue */
5678058Seric 					avp = ++mlp[-1].last;
5688058Seric 					avp++;
5694476Seric 					rvp++;
5703149Seric 					break;
5714468Seric 				}
5724476Seric 				avp--;
5734476Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS)
5743149Seric 				{
5754468Seric 					/* back out binding */
5764468Seric 					mlp--;
5773149Seric 				}
5783149Seric 			}
5793149Seric 
5803149Seric 			if (rvp < rwr->r_lhs)
5813149Seric 			{
5823149Seric 				/* total failure to match */
5833149Seric 				break;
5843149Seric 			}
585297Seric 		}
5863149Seric 
5873149Seric 		/*
5883149Seric 		**  See if we successfully matched
5893149Seric 		*/
5903149Seric 
5913149Seric 		if (rvp >= rwr->r_lhs && *rvp == NULL)
5923149Seric 		{
5938058Seric 			rvp = rwr->r_rhs;
5944100Seric # ifdef DEBUG
5957675Seric 			if (tTd(21, 12))
5963149Seric 			{
5978069Seric 				printf("-----rule matches:");
5988058Seric 				printav(rvp);
5993149Seric 			}
6004100Seric # endif DEBUG
6013149Seric 
6028058Seric 			rp = *rvp;
6038226Seric 			if (*rp == CANONUSER)
6048058Seric 			{
6058069Seric 				rvp++;
6068069Seric 				rwr = rwr->r_next;
6078069Seric 			}
6088069Seric 			else if (*rp == CANONHOST)
6098069Seric 			{
6108069Seric 				rvp++;
6118069Seric 				rwr = NULL;
6128069Seric 			}
6138069Seric 			else if (*rp == CANONNET)
6148069Seric 				rwr = NULL;
6158058Seric 
6163149Seric 			/* substitute */
6178069Seric 			for (avp = npvp; *rvp != NULL; rvp++)
6183149Seric 			{
6193149Seric 				rp = *rvp;
6204468Seric 				if (*rp == MATCHREPL)
6213149Seric 				{
6223149Seric 					register struct match *m;
6233149Seric 					register char **pp;
6243149Seric 
6254468Seric 					m = &mlist[rp[1] - '1'];
6264476Seric # ifdef DEBUG
6277675Seric 					if (tTd(21, 15))
6284476Seric 					{
6294476Seric 						printf("$%c:", rp[1]);
6304476Seric 						pp = m->first;
6318058Seric 						while (pp <= m->last)
6324476Seric 						{
6334476Seric 							printf(" %x=\"", *pp);
6344625Seric 							(void) fflush(stdout);
6358058Seric 							printf("%s\"", *pp++);
6368058Seric 						}
6374476Seric 						printf("\n");
6384476Seric 					}
6394476Seric # endif DEBUG
6404468Seric 					pp = m->first;
6418058Seric 					while (pp <= m->last)
6423149Seric 					{
6434468Seric 						if (avp >= &npvp[MAXATOM])
6443149Seric 						{
6454468Seric 							syserr("rewrite: expansion too long");
6464468Seric 							return;
6474468Seric 						}
6488058Seric 						*avp++ = *pp++;
6498058Seric 					}
6503149Seric 				}
6513149Seric 				else
6524385Seric 				{
6534385Seric 					if (avp >= &npvp[MAXATOM])
6544385Seric 					{
6554385Seric 						syserr("rewrite: expansion too long");
6564385Seric 						return;
6574385Seric 					}
6583149Seric 					*avp++ = rp;
6594385Seric 				}
6603149Seric 			}
6613149Seric 			*avp++ = NULL;
6628226Seric 			if (**npvp == CALLSUBR)
6638226Seric 			{
6648226Seric 				bmove((char *) &npvp[2], (char *) pvp,
6658226Seric 					(avp - npvp - 2) * sizeof *avp);
6663149Seric # ifdef DEBUG
6678226Seric 				if (tTd(21, 3))
6688226Seric 					printf("-----callsubr %s\n", npvp[1]);
6698226Seric # endif DEBUG
6708226Seric 				rewrite(pvp, atoi(npvp[1]));
6718226Seric 			}
6728226Seric 			else
6738226Seric 			{
6748226Seric 				bmove((char *) npvp, (char *) pvp,
6758226Seric 					(avp - npvp) * sizeof *avp);
6768226Seric 			}
6778226Seric # ifdef DEBUG
6787675Seric 			if (tTd(21, 4))
6793149Seric 			{
6808069Seric 				printf("rewritten as:");
6818069Seric 				printav(pvp);
6823149Seric 			}
6833149Seric # endif DEBUG
6843149Seric 		}
6853149Seric 		else
6863149Seric 		{
6874100Seric # ifdef DEBUG
6887675Seric 			if (tTd(21, 10))
6893149Seric 				printf("----- rule fails\n");
6904100Seric # endif DEBUG
6913149Seric 			rwr = rwr->r_next;
6923149Seric 		}
693297Seric 	}
6948069Seric 
6959279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
6968069Seric 	{
6978959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
6988069Seric 		printav(pvp);
6998069Seric 	}
7003149Seric }
7013149Seric /*
7023149Seric **  BUILDADDR -- build address from token vector.
7033149Seric **
7043149Seric **	Parameters:
7053149Seric **		tv -- token vector.
7063149Seric **		a -- pointer to address descriptor to fill.
7073149Seric **			If NULL, one will be allocated.
7083149Seric **
7093149Seric **	Returns:
7104279Seric **		NULL if there was an error.
7114279Seric **		'a' otherwise.
7123149Seric **
7133149Seric **	Side Effects:
7143149Seric **		fills in 'a'
7153149Seric */
7163149Seric 
7173149Seric ADDRESS *
7183149Seric buildaddr(tv, a)
7193149Seric 	register char **tv;
7203149Seric 	register ADDRESS *a;
7213149Seric {
7223149Seric 	static char buf[MAXNAME];
7233149Seric 	struct mailer **mp;
7243149Seric 	register struct mailer *m;
7254635Seric 	extern bool sameword();
7263149Seric 
7273149Seric 	if (a == NULL)
7283149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
7294988Seric 	clear((char *) a, sizeof *a);
7303149Seric 
7313149Seric 	/* figure out what net/mailer to use */
7323149Seric 	if (**tv != CANONNET)
7334279Seric 	{
7343149Seric 		syserr("buildaddr: no net");
7354279Seric 		return (NULL);
7364279Seric 	}
7373149Seric 	tv++;
7384635Seric 	if (sameword(*tv, "error"))
7394279Seric 	{
7404279Seric 		if (**++tv != CANONUSER)
7414279Seric 			syserr("buildaddr: error: no user");
7424279Seric 		buf[0] = '\0';
7434279Seric 		while (*++tv != NULL)
7444279Seric 		{
7454279Seric 			if (buf[0] != '\0')
7467005Seric 				(void) strcat(buf, " ");
7477005Seric 			(void) strcat(buf, *tv);
7484279Seric 		}
7494279Seric 		usrerr(buf);
7504279Seric 		return (NULL);
7514279Seric 	}
7524598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
7533149Seric 	{
7544635Seric 		if (sameword(m->m_name, *tv))
7553149Seric 			break;
7563149Seric 	}
7573149Seric 	if (m == NULL)
7584279Seric 	{
7593149Seric 		syserr("buildaddr: unknown net %s", *tv);
7604279Seric 		return (NULL);
7614279Seric 	}
7624598Seric 	a->q_mailer = m;
7633149Seric 
7643149Seric 	/* figure out what host (if any) */
7653149Seric 	tv++;
7664195Seric 	if (!bitset(M_LOCAL, m->m_flags))
7673149Seric 	{
7685704Seric 		if (**tv++ != CANONHOST)
7694279Seric 		{
7703149Seric 			syserr("buildaddr: no host");
7714279Seric 			return (NULL);
7724279Seric 		}
7735704Seric 		buf[0] = '\0';
7745704Seric 		while (*tv != NULL && **tv != CANONUSER)
7757005Seric 			(void) strcat(buf, *tv++);
7765704Seric 		a->q_host = newstr(buf);
7773149Seric 	}
7783149Seric 	else
7793149Seric 		a->q_host = NULL;
7803149Seric 
7813149Seric 	/* figure out the user */
7823149Seric 	if (**tv != CANONUSER)
7834279Seric 	{
7843149Seric 		syserr("buildaddr: no user");
7854279Seric 		return (NULL);
7864279Seric 	}
7874228Seric 	cataddr(++tv, buf, sizeof buf);
7883149Seric 	a->q_user = buf;
7893149Seric 
7903149Seric 	return (a);
7913149Seric }
7923188Seric /*
7934228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
7944228Seric **
7954228Seric **	Parameters:
7964228Seric **		pvp -- parameter vector to rebuild.
7974228Seric **		buf -- buffer to build the string into.
7984228Seric **		sz -- size of buf.
7994228Seric **
8004228Seric **	Returns:
8014228Seric **		none.
8024228Seric **
8034228Seric **	Side Effects:
8044228Seric **		Destroys buf.
8054228Seric */
8064228Seric 
8074228Seric cataddr(pvp, buf, sz)
8084228Seric 	char **pvp;
8094228Seric 	char *buf;
8104228Seric 	register int sz;
8114228Seric {
8124228Seric 	bool oatomtok = FALSE;
8134228Seric 	bool natomtok = FALSE;
8144228Seric 	register int i;
8154228Seric 	register char *p;
8164228Seric 
8178423Seric 	if (pvp == NULL)
8188423Seric 	{
8198423Seric 		strcpy(buf, "");
8208423Seric 		return;
8218423Seric 	}
8224228Seric 	p = buf;
8234228Seric 	sz--;
8244228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
8254228Seric 	{
8268078Seric 		natomtok = (toktype(**pvp) == ATM);
8274228Seric 		if (oatomtok && natomtok)
8289042Seric 			*p++ = SpaceSub;
8294228Seric 		(void) strcpy(p, *pvp);
8304228Seric 		oatomtok = natomtok;
8314228Seric 		p += i;
8324228Seric 		sz -= i;
8334228Seric 		pvp++;
8344228Seric 	}
8354228Seric 	*p = '\0';
8364228Seric }
8374228Seric /*
8383188Seric **  SAMEADDR -- Determine if two addresses are the same
8393188Seric **
8403188Seric **	This is not just a straight comparison -- if the mailer doesn't
8413188Seric **	care about the host we just ignore it, etc.
8423188Seric **
8433188Seric **	Parameters:
8443188Seric **		a, b -- pointers to the internal forms to compare.
8453188Seric **		wildflg -- if TRUE, 'a' may have no user specified,
8463188Seric **			in which case it is to match anything.
8473188Seric **
8483188Seric **	Returns:
8493188Seric **		TRUE -- they represent the same mailbox.
8503188Seric **		FALSE -- they don't.
8513188Seric **
8523188Seric **	Side Effects:
8533188Seric **		none.
8543188Seric */
8553188Seric 
8563188Seric bool
8573188Seric sameaddr(a, b, wildflg)
8583188Seric 	register ADDRESS *a;
8593188Seric 	register ADDRESS *b;
8603188Seric 	bool wildflg;
8613188Seric {
8623188Seric 	/* if they don't have the same mailer, forget it */
8633188Seric 	if (a->q_mailer != b->q_mailer)
8643188Seric 		return (FALSE);
8653188Seric 
8663188Seric 	/* if the user isn't the same, we can drop out */
8673188Seric 	if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0)
8683188Seric 		return (FALSE);
8693188Seric 
8703188Seric 	/* if the mailer ignores hosts, we have succeeded! */
8714598Seric 	if (bitset(M_LOCAL, a->q_mailer->m_flags))
8723188Seric 		return (TRUE);
8733188Seric 
8743188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
8753188Seric 	if (a->q_host == NULL || b->q_host == NULL)
8763188Seric 		return (FALSE);
8773188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
8783188Seric 		return (FALSE);
8793188Seric 
8803188Seric 	return (TRUE);
8813188Seric }
8823234Seric /*
8833234Seric **  PRINTADDR -- print address (for debugging)
8843234Seric **
8853234Seric **	Parameters:
8863234Seric **		a -- the address to print
8873234Seric **		follow -- follow the q_next chain.
8883234Seric **
8893234Seric **	Returns:
8903234Seric **		none.
8913234Seric **
8923234Seric **	Side Effects:
8933234Seric **		none.
8943234Seric */
8953234Seric 
8964317Seric # ifdef DEBUG
8974317Seric 
8983234Seric printaddr(a, follow)
8993234Seric 	register ADDRESS *a;
9003234Seric 	bool follow;
9013234Seric {
9025001Seric 	bool first = TRUE;
9035001Seric 
9043234Seric 	while (a != NULL)
9053234Seric 	{
9065001Seric 		first = FALSE;
9074443Seric 		printf("%x=", a);
9084085Seric 		(void) fflush(stdout);
9093234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
9108181Seric 		       a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host,
9118181Seric 		       a->q_user);
9128181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
9138181Seric 		       a->q_alias);
9148181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
9158181Seric 		       a->q_fullname);
9164996Seric 
9173234Seric 		if (!follow)
9183234Seric 			return;
9194996Seric 		a = a->q_next;
9203234Seric 	}
9215001Seric 	if (first)
9224443Seric 		printf("[NULL]\n");
9233234Seric }
9244317Seric 
9254317Seric # endif DEBUG
9267682Seric /*
9277682Seric **  REMOTENAME -- return the name relative to the current mailer
9287682Seric **
9297682Seric **	Parameters:
9307682Seric **		name -- the name to translate.
9318069Seric **		m -- the mailer that we want to do rewriting relative
9328069Seric **			to.
9338069Seric **		senderaddress -- if set, uses the sender rewriting rules
9348069Seric **			rather than the recipient rewriting rules.
9357682Seric **
9367682Seric **	Returns:
9377682Seric **		the text string representing this address relative to
9387682Seric **			the receiving mailer.
9397682Seric **
9407682Seric **	Side Effects:
9417682Seric **		none.
9427682Seric **
9437682Seric **	Warnings:
9447682Seric **		The text string returned is tucked away locally;
9457682Seric **			copy it if you intend to save it.
9467682Seric */
9477682Seric 
9487682Seric char *
9498069Seric remotename(name, m, senderaddress)
9507682Seric 	char *name;
9517682Seric 	struct mailer *m;
9528069Seric 	bool senderaddress;
9537682Seric {
9548069Seric 	register char **pvp;
9558069Seric 	char *fancy;
9568069Seric 	extern char *macvalue();
9578181Seric 	char *oldg = macvalue('g', CurEnv);
9587682Seric 	static char buf[MAXNAME];
9597682Seric 	char lbuf[MAXNAME];
9607682Seric 	extern char **prescan();
9617889Seric 	extern char *crackaddr();
9627682Seric 
9637755Seric # ifdef DEBUG
9647755Seric 	if (tTd(12, 1))
9657755Seric 		printf("remotename(%s)\n", name);
9667755Seric # endif DEBUG
9677755Seric 
9687682Seric 	/*
9698181Seric 	**  Do a heuristic crack of this name to extract any comment info.
9708181Seric 	**	This will leave the name as a comment and a $g macro.
9717889Seric 	*/
9727889Seric 
9737889Seric 	fancy = crackaddr(name);
9747889Seric 
9758181Seric 	/*
9768181Seric 	**  Turn the name into canonical form.
9778181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
9788181Seric 	**	If this only resolves to "user", and the "C" flag is
9798181Seric 	**	specified in the sending mailer, then the sender's
9808181Seric 	**	domain will be appended.
9818181Seric 	*/
9828181Seric 
9837889Seric 	pvp = prescan(name, '\0');
9847889Seric 	if (pvp == NULL)
9857889Seric 		return (name);
9868181Seric 	rewrite(pvp, 3);
9878181Seric 	if (CurEnv->e_fromdomain != NULL)
9888181Seric 	{
9898181Seric 		/* append from domain to this address */
9908181Seric 		register char **pxp = pvp;
9918181Seric 
9928181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
9938181Seric 			pxp++;
9948181Seric 		if (*pxp == NULL)
9958181Seric 		{
9968181Seric 			register char **qxq = CurEnv->e_fromdomain;
9978181Seric 
9988181Seric 			while (*qxq != NULL)
9998181Seric 				*pxp++ = *qxq++;
10008181Seric 		}
10018181Seric 	}
10028181Seric 
10038181Seric 	/*
10048959Seric 	**  Do more specific rewriting.
10058181Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
10068181Seric 	**		a sender address or not.
10078181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
10088181Seric 	*/
10098181Seric 
10108069Seric 	if (senderaddress)
10117755Seric 	{
10127889Seric 		rewrite(pvp, 1);
10138069Seric 		if (m->m_s_rwset > 0)
10148069Seric 			rewrite(pvp, m->m_s_rwset);
10158069Seric 	}
10168069Seric 	else
10178069Seric 	{
10187889Seric 		rewrite(pvp, 2);
10198069Seric 		if (m->m_r_rwset > 0)
10208069Seric 			rewrite(pvp, m->m_r_rwset);
10217682Seric 	}
10227682Seric 
10238181Seric 	/*
10248959Seric 	**  Do any final sanitation the address may require.
10258959Seric 	**	This will normally be used to turn internal forms
10268959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
10278959Seric 	**	may be used as a default to the above rules.
10288959Seric 	*/
10298959Seric 
10308959Seric 	rewrite(pvp, 4);
10318959Seric 
10328959Seric 	/*
10338181Seric 	**  Now restore the comment information we had at the beginning.
10348181Seric 	*/
10358181Seric 
10367682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
10377682Seric 	define('g', lbuf);
10387889Seric 	expand(fancy, buf, &buf[sizeof buf - 1], CurEnv);
10397682Seric 	define('g', oldg);
10407682Seric 
10417682Seric # ifdef DEBUG
10427682Seric 	if (tTd(12, 1))
10437755Seric 		printf("remotename => `%s'\n", buf);
10447682Seric # endif DEBUG
10457682Seric 	return (buf);
10467682Seric }
10477682Seric /*
10487682Seric **  CANONNAME -- make name canonical
10497682Seric **
10507682Seric **	This is used for SMTP and misc. printing.  Given a print
10518181Seric **	address, it strips out comments, etc.
10527682Seric **
10537682Seric **	Parameters:
10547682Seric **		name -- the name to make canonical.
10558353Seric **		ruleset -- the canonicalizing ruleset.
10567682Seric **
10577682Seric **	Returns:
10587682Seric **		pointer to canonical name.
10597682Seric **
10607682Seric **	Side Effects:
10617682Seric **		none.
10627682Seric **
10637682Seric **	Warning:
10647682Seric **		result is saved in static buf; future calls will trash it.
10657682Seric */
10667682Seric 
10677682Seric char *
10688353Seric canonname(name, ruleset)
10697682Seric 	char *name;
10708353Seric 	int ruleset;
10717682Seric {
10728069Seric 	static char nbuf[MAXNAME];
10737940Seric 	register char **pvp;
10747682Seric 
10757940Seric 	pvp = prescan(name, '\0');
10768069Seric 	rewrite(pvp, 3);
10778353Seric 	rewrite(pvp, ruleset);
10788069Seric 	cataddr(pvp, nbuf, sizeof nbuf);
10797682Seric 	return (nbuf);
10807682Seric }
1081