13312Seric # include "sendmail.h"
2297Seric 
3*15253Seric SCCSID(@(#)parseaddr.c	4.2		10/16/83);
4407Seric 
5297Seric /*
69888Seric **  PARSEADDR -- Parse an address
7297Seric **
8297Seric **	Parses an address and breaks it up into three parts: a
9297Seric **	net to transmit the message on, the host to transmit it
10297Seric **	to, and a user on that host.  These are loaded into an
112973Seric **	ADDRESS header with the values squirreled away if necessary.
12297Seric **	The "user" part may not be a real user; the process may
13297Seric **	just reoccur on that machine.  For example, on a machine
14297Seric **	with an arpanet connection, the address
15297Seric **		csvax.bill@berkeley
16297Seric **	will break up to a "user" of 'csvax.bill' and a host
17297Seric **	of 'berkeley' -- to be transmitted over the arpanet.
18297Seric **
19297Seric **	Parameters:
20297Seric **		addr -- the address to parse.
21297Seric **		a -- a pointer to the address descriptor buffer.
22297Seric **			If NULL, a header will be created.
23297Seric **		copyf -- determines what shall be copied:
24297Seric **			-1 -- don't copy anything.  The printname
25297Seric **				(q_paddr) is just addr, and the
26297Seric **				user & host are allocated internally
27297Seric **				to parse.
28297Seric **			0 -- copy out the parsed user & host, but
29297Seric **				don't copy the printname.
30297Seric **			+1 -- copy everything.
3111445Seric **		delim -- the character to terminate the address, passed
3211445Seric **			to prescan.
33297Seric **
34297Seric **	Returns:
35297Seric **		A pointer to the address descriptor header (`a' if
36297Seric **			`a' is non-NULL).
37297Seric **		NULL on error.
38297Seric **
39297Seric **	Side Effects:
40297Seric **		none
41297Seric */
42297Seric 
439374Seric /* following delimiters are inherent to the internal algorithms */
443380Seric # define DELIMCHARS	"$()<>,;\\\"\r\n"	/* word delimiters */
452091Seric 
462973Seric ADDRESS *
4711445Seric parseaddr(addr, a, copyf, delim)
48297Seric 	char *addr;
492973Seric 	register ADDRESS *a;
50297Seric 	int copyf;
5111445Seric 	char delim;
52297Seric {
533149Seric 	register char **pvp;
543149Seric 	register struct mailer *m;
553149Seric 	extern char **prescan();
563149Seric 	extern ADDRESS *buildaddr();
57297Seric 
58297Seric 	/*
59297Seric 	**  Initialize and prescan address.
60297Seric 	*/
61297Seric 
626903Seric 	CurEnv->e_to = addr;
633188Seric # ifdef DEBUG
647675Seric 	if (tTd(20, 1))
659888Seric 		printf("\n--parseaddr(%s)\n", addr);
663188Seric # endif DEBUG
673188Seric 
6811445Seric 	pvp = prescan(addr, delim);
693149Seric 	if (pvp == NULL)
70297Seric 		return (NULL);
71297Seric 
72297Seric 	/*
733149Seric 	**  Apply rewriting rules.
747889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
75297Seric 	*/
76297Seric 
778181Seric 	rewrite(pvp, 3);
784070Seric 	rewrite(pvp, 0);
79297Seric 
803149Seric 	/*
813149Seric 	**  See if we resolved to a real mailer.
823149Seric 	*/
83297Seric 
843149Seric 	if (pvp[0][0] != CANONNET)
853149Seric 	{
863149Seric 		setstat(EX_USAGE);
873149Seric 		usrerr("cannot resolve name");
883149Seric 		return (NULL);
89297Seric 	}
90297Seric 
91297Seric 	/*
923149Seric 	**  Build canonical address from pvp.
93297Seric 	*/
94297Seric 
953149Seric 	a = buildaddr(pvp, a);
964279Seric 	if (a == NULL)
974279Seric 		return (NULL);
984598Seric 	m = a->q_mailer;
99297Seric 
100297Seric 	/*
1013149Seric 	**  Make local copies of the host & user and then
1023149Seric 	**  transport them out.
103297Seric 	*/
104297Seric 
105297Seric 	if (copyf > 0)
1068078Seric 	{
1078078Seric 		extern char *DelimChar;
1088078Seric 		char savec = *DelimChar;
1098078Seric 
1108078Seric 		*DelimChar = '\0';
1112973Seric 		a->q_paddr = newstr(addr);
1128078Seric 		*DelimChar = savec;
1138078Seric 	}
114297Seric 	else
115297Seric 		a->q_paddr = addr;
1163149Seric 	if (copyf >= 0)
117297Seric 	{
1183149Seric 		if (a->q_host != NULL)
1193149Seric 			a->q_host = newstr(a->q_host);
120297Seric 		else
1213149Seric 			a->q_host = "";
1223149Seric 		if (a->q_user != a->q_paddr)
1233149Seric 			a->q_user = newstr(a->q_user);
124297Seric 	}
125297Seric 
126297Seric 	/*
127297Seric 	**  Do UPPER->lower case mapping unless inhibited.
128297Seric 	*/
129297Seric 
13010690Seric 	if (!bitnset(M_HST_UPPER, m->m_flags))
131297Seric 		makelower(a->q_host);
13210690Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
133297Seric 		makelower(a->q_user);
134297Seric 
135297Seric 	/*
136297Seric 	**  Compute return value.
137297Seric 	*/
138297Seric 
139297Seric # ifdef DEBUG
1407675Seric 	if (tTd(20, 1))
1414443Seric 	{
1429888Seric 		printf("parseaddr-->");
1434443Seric 		printaddr(a, FALSE);
1444443Seric 	}
145297Seric # endif DEBUG
146297Seric 
147297Seric 	return (a);
148297Seric }
149297Seric /*
150297Seric **  PRESCAN -- Prescan name and make it canonical
151297Seric **
1529374Seric **	Scans a name and turns it into a set of tokens.  This process
1539374Seric **	deletes blanks and comments (in parentheses).
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	*/
1969051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
1979051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
1989051Seric 	/*QST*/		QST,	QST,	OPR,	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;
2149346Seric 	register int c;
2153149Seric 	char **avp;
216297Seric 	bool bslashmode;
217297Seric 	int cmntcnt;
2188423Seric 	int anglecnt;
2193149Seric 	char *tok;
2208078Seric 	int state;
2218078Seric 	int newstate;
2228078Seric 	static char buf[MAXNAME+MAXATOM];
2238078Seric 	static char *av[MAXATOM+1];
224*15253Seric 	extern int errno;
225297Seric 
226*15253Seric 	/* make sure error messages don't have garbage on them */
227*15253Seric 	errno = 0;
228*15253Seric 
229297Seric 	q = buf;
2303149Seric 	bslashmode = FALSE;
2317800Seric 	cmntcnt = 0;
2328423Seric 	anglecnt = 0;
2333149Seric 	avp = av;
2348078Seric 	state = OPR;
2358078Seric 	c = NOCHAR;
2368078Seric 	p = addr;
2378078Seric # ifdef DEBUG
2388078Seric 	if (tTd(22, 45))
239297Seric 	{
2408078Seric 		printf("prescan: ");
2418078Seric 		xputs(p);
2428078Seric 		putchar('\n');
2438078Seric 	}
2448078Seric # endif DEBUG
2458078Seric 
2468078Seric 	do
2478078Seric 	{
2483149Seric 		/* read a token */
2493149Seric 		tok = q;
2508078Seric 		for (;;)
251297Seric 		{
2528078Seric 			/* store away any old lookahead character */
2538078Seric 			if (c != NOCHAR)
2548078Seric 			{
2558078Seric 				/* squirrel it away */
2568078Seric 				if (q >= &buf[sizeof buf - 5])
2578078Seric 				{
2588078Seric 					usrerr("Address too long");
2598078Seric 					DelimChar = p;
2608078Seric 					return (NULL);
2618078Seric 				}
2628078Seric 				*q++ = c;
2638078Seric 			}
2648078Seric 
2658078Seric 			/* read a new input character */
2668078Seric 			c = *p++;
2678078Seric 			if (c == '\0')
2688078Seric 				break;
2698078Seric # ifdef DEBUG
2708078Seric 			if (tTd(22, 101))
2718078Seric 				printf("c=%c, s=%d; ", c, state);
2728078Seric # endif DEBUG
2738078Seric 
2743149Seric 			/* chew up special characters */
2754100Seric 			c &= ~0200;
2763149Seric 			*q = '\0';
2773149Seric 			if (bslashmode)
2783149Seric 			{
2793149Seric 				c |= 0200;
2803149Seric 				bslashmode = FALSE;
2813149Seric 			}
2823149Seric 			else if (c == '\\')
2833149Seric 			{
2843149Seric 				bslashmode = TRUE;
2858078Seric 				c = NOCHAR;
2863149Seric 			}
2878514Seric 			else if (state == QST)
2888514Seric 			{
2898514Seric 				/* do nothing, just avoid next clauses */
2908514Seric 			}
2918078Seric 			else if (c == '(')
2924100Seric 			{
2938078Seric 				cmntcnt++;
2948078Seric 				c = NOCHAR;
2954100Seric 			}
2968078Seric 			else if (c == ')')
2973149Seric 			{
2988078Seric 				if (cmntcnt <= 0)
2993149Seric 				{
3008078Seric 					usrerr("Unbalanced ')'");
3018078Seric 					DelimChar = p;
3028078Seric 					return (NULL);
3033149Seric 				}
3048078Seric 				else
3058078Seric 					cmntcnt--;
3068078Seric 			}
3078078Seric 			else if (cmntcnt > 0)
3088078Seric 				c = NOCHAR;
3098423Seric 			else if (c == '<')
3108423Seric 				anglecnt++;
3118423Seric 			else if (c == '>')
3128423Seric 			{
3138423Seric 				if (anglecnt <= 0)
3148423Seric 				{
3158423Seric 					usrerr("Unbalanced '>'");
3168423Seric 					DelimChar = p;
3178423Seric 					return (NULL);
3188423Seric 				}
3198423Seric 				anglecnt--;
3208423Seric 			}
32111423Seric 			else if (delim == ' ' && isspace(c))
32211423Seric 				c = ' ';
3233149Seric 
3248078Seric 			if (c == NOCHAR)
3258078Seric 				continue;
3263149Seric 
3278078Seric 			/* see if this is end of input */
32811405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
3293149Seric 				break;
3303149Seric 
3318078Seric 			newstate = StateTab[state][toktype(c)];
3328078Seric # ifdef DEBUG
3338078Seric 			if (tTd(22, 101))
3348078Seric 				printf("ns=%02o\n", newstate);
3358078Seric # endif DEBUG
3368078Seric 			state = newstate & TYPE;
3378078Seric 			if (bitset(M, newstate))
3388078Seric 				c = NOCHAR;
3398078Seric 			if (bitset(B, newstate))
3404228Seric 				break;
341297Seric 		}
3423149Seric 
3433149Seric 		/* new token */
3448078Seric 		if (tok != q)
3451378Seric 		{
3468078Seric 			*q++ = '\0';
3478078Seric # ifdef DEBUG
3488078Seric 			if (tTd(22, 36))
349297Seric 			{
3508078Seric 				printf("tok=");
3518078Seric 				xputs(tok);
3528078Seric 				putchar('\n');
353297Seric 			}
3548078Seric # endif DEBUG
3558078Seric 			if (avp >= &av[MAXATOM])
356297Seric 			{
3578078Seric 				syserr("prescan: too many tokens");
3588078Seric 				DelimChar = p;
3598078Seric 				return (NULL);
360297Seric 			}
3618078Seric 			*avp++ = tok;
362297Seric 		}
3638423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
3643149Seric 	*avp = NULL;
3658078Seric 	DelimChar = --p;
3663149Seric 	if (cmntcnt > 0)
3673149Seric 		usrerr("Unbalanced '('");
3688423Seric 	else if (anglecnt > 0)
3698423Seric 		usrerr("Unbalanced '<'");
3708078Seric 	else if (state == QST)
3713149Seric 		usrerr("Unbalanced '\"'");
3723149Seric 	else if (av[0] != NULL)
3733149Seric 		return (av);
3743149Seric 	return (NULL);
3753149Seric }
3763149Seric /*
3773149Seric **  TOKTYPE -- return token type
3783149Seric **
3793149Seric **	Parameters:
3803149Seric **		c -- the character in question.
3813149Seric **
3823149Seric **	Returns:
3833149Seric **		Its type.
3843149Seric **
3853149Seric **	Side Effects:
3863149Seric **		none.
3873149Seric */
388297Seric 
3893149Seric toktype(c)
3903149Seric 	register char c;
3913149Seric {
3923380Seric 	static char buf[50];
3933382Seric 	static bool firstime = TRUE;
3943380Seric 
3953382Seric 	if (firstime)
3963380Seric 	{
3973382Seric 		firstime = FALSE;
3986977Seric 		expand("$o", buf, &buf[sizeof buf - 1], CurEnv);
3997005Seric 		(void) strcat(buf, DELIMCHARS);
4003380Seric 	}
4019585Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
4028078Seric 		return (ONE);
4038078Seric 	if (c == '"')
4048078Seric 		return (QST);
4054100Seric 	if (!isascii(c))
4068078Seric 		return (ATM);
4078078Seric 	if (isspace(c) || c == ')')
4088078Seric 		return (SPC);
4093380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
4108078Seric 		return (OPR);
4118078Seric 	return (ATM);
4123149Seric }
4133149Seric /*
4143149Seric **  REWRITE -- apply rewrite rules to token vector.
4153149Seric **
4164476Seric **	This routine is an ordered production system.  Each rewrite
4174476Seric **	rule has a LHS (called the pattern) and a RHS (called the
4184476Seric **	rewrite); 'rwr' points the the current rewrite rule.
4194476Seric **
4204476Seric **	For each rewrite rule, 'avp' points the address vector we
4214476Seric **	are trying to match against, and 'pvp' points to the pattern.
4228058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
4239585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
4249585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
4254476Seric **
4264476Seric **	When a match between avp & pvp does not match, we try to
4279585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
4284476Seric **	we must also back out the match in mvp.  If we reach a
4298058Seric **	MATCHANY or MATCHZANY we just extend the match and start
4308058Seric **	over again.
4314476Seric **
4324476Seric **	When we finally match, we rewrite the address vector
4334476Seric **	and try over again.
4344476Seric **
4353149Seric **	Parameters:
4363149Seric **		pvp -- pointer to token vector.
4373149Seric **
4383149Seric **	Returns:
4393149Seric **		none.
4403149Seric **
4413149Seric **	Side Effects:
4423149Seric **		pvp is modified.
4433149Seric */
4442091Seric 
4453149Seric struct match
4463149Seric {
4474468Seric 	char	**first;	/* first token matched */
4484468Seric 	char	**last;		/* last token matched */
4493149Seric };
4503149Seric 
4514468Seric # define MAXMATCH	9	/* max params per rewrite */
4523149Seric 
4533149Seric 
4544070Seric rewrite(pvp, ruleset)
4553149Seric 	char **pvp;
4564070Seric 	int ruleset;
4573149Seric {
4583149Seric 	register char *ap;		/* address pointer */
4593149Seric 	register char *rp;		/* rewrite pointer */
4603149Seric 	register char **avp;		/* address vector pointer */
4613149Seric 	register char **rvp;		/* rewrite vector pointer */
4628058Seric 	register struct match *mlp;	/* cur ptr into mlist */
4638058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
4644468Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
4653149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
4664060Seric 	extern bool sameword();
4673149Seric 
4689279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
4693149Seric 	{
4708959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
4713149Seric 		printav(pvp);
4723149Seric 	}
4738423Seric 	if (pvp == NULL)
4748423Seric 		return;
4753149Seric 
4763149Seric 	/*
4773149Seric 	**  Run through the list of rewrite rules, applying
4783149Seric 	**	any that match.
4793149Seric 	*/
4803149Seric 
4814070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
4823149Seric 	{
4834100Seric # ifdef DEBUG
4847675Seric 		if (tTd(21, 12))
485297Seric 		{
4868069Seric 			printf("-----trying rule:");
4873149Seric 			printav(rwr->r_lhs);
4883149Seric 		}
4894100Seric # endif DEBUG
4903149Seric 
4913149Seric 		/* try to match on this rule */
4924468Seric 		mlp = mlist;
4938058Seric 		rvp = rwr->r_lhs;
4948058Seric 		avp = pvp;
4958058Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
4963149Seric 		{
4973149Seric 			rp = *rvp;
4988058Seric # ifdef DEBUG
4998058Seric 			if (tTd(21, 35))
5008058Seric 			{
5018069Seric 				printf("ap=");
5028058Seric 				xputs(ap);
5038069Seric 				printf(", rp=");
5048058Seric 				xputs(rp);
5058069Seric 				printf("\n");
5068058Seric 			}
5078058Seric # endif DEBUG
5083149Seric 			if (rp == NULL)
509297Seric 			{
5103149Seric 				/* end-of-pattern before end-of-address */
5118058Seric 				goto backup;
512297Seric 			}
5138058Seric 			if (ap == NULL && *rp != MATCHZANY)
5148058Seric 			{
5158058Seric 				/* end-of-input */
5168058Seric 				break;
5178058Seric 			}
5183149Seric 
5193149Seric 			switch (*rp)
5203149Seric 			{
5214060Seric 				register STAB *s;
5224060Seric 
5234060Seric 			  case MATCHCLASS:
5249585Seric 			  case MATCHNCLASS:
5259585Seric 				/* match any token in (not in) a class */
5264100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
52710690Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
5289585Seric 				{
5299585Seric 					if (*rp == MATCHCLASS)
5309585Seric 						goto backup;
5319585Seric 				}
5329585Seric 				else if (*rp == MATCHNCLASS)
5338058Seric 					goto backup;
5344468Seric 
5354476Seric 				/* explicit fall-through */
5364476Seric 
5374476Seric 			  case MATCHONE:
5384476Seric 			  case MATCHANY:
5394476Seric 				/* match exactly one token */
5408058Seric 				mlp->first = avp;
5418058Seric 				mlp->last = avp++;
5424468Seric 				mlp++;
5434060Seric 				break;
5444060Seric 
5458058Seric 			  case MATCHZANY:
5468058Seric 				/* match zero or more tokens */
5478058Seric 				mlp->first = avp;
5488058Seric 				mlp->last = avp - 1;
5498058Seric 				mlp++;
5508058Seric 				break;
5518058Seric 
5523149Seric 			  default:
5533149Seric 				/* must have exact match */
5544060Seric 				if (!sameword(rp, ap))
5558058Seric 					goto backup;
5564468Seric 				avp++;
5573149Seric 				break;
5583149Seric 			}
5593149Seric 
5603149Seric 			/* successful match on this token */
5613149Seric 			rvp++;
5623149Seric 			continue;
5633149Seric 
5648058Seric 		  backup:
5653149Seric 			/* match failed -- back up */
5663149Seric 			while (--rvp >= rwr->r_lhs)
5673149Seric 			{
5683149Seric 				rp = *rvp;
5698058Seric 				if (*rp == MATCHANY || *rp == MATCHZANY)
5704468Seric 				{
5714476Seric 					/* extend binding and continue */
5728058Seric 					avp = ++mlp[-1].last;
5738058Seric 					avp++;
5744476Seric 					rvp++;
5753149Seric 					break;
5764468Seric 				}
5774476Seric 				avp--;
5789585Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS ||
5799585Seric 				    *rp == MATCHNCLASS)
5803149Seric 				{
5814468Seric 					/* back out binding */
5824468Seric 					mlp--;
5833149Seric 				}
5843149Seric 			}
5853149Seric 
5863149Seric 			if (rvp < rwr->r_lhs)
5873149Seric 			{
5883149Seric 				/* total failure to match */
5893149Seric 				break;
5903149Seric 			}
591297Seric 		}
5923149Seric 
5933149Seric 		/*
5943149Seric 		**  See if we successfully matched
5953149Seric 		*/
5963149Seric 
5979374Seric 		if (rvp < rwr->r_lhs || *rvp != NULL)
5983149Seric 		{
5994100Seric # ifdef DEBUG
6009374Seric 			if (tTd(21, 10))
6019374Seric 				printf("----- rule fails\n");
6024100Seric # endif DEBUG
6039374Seric 			rwr = rwr->r_next;
6049374Seric 			continue;
6059374Seric 		}
6063149Seric 
6079374Seric 		rvp = rwr->r_rhs;
6089374Seric # ifdef DEBUG
6099374Seric 		if (tTd(21, 12))
6109374Seric 		{
6119374Seric 			printf("-----rule matches:");
6129374Seric 			printav(rvp);
6139374Seric 		}
6149374Seric # endif DEBUG
6159374Seric 
6169374Seric 		rp = *rvp;
6179374Seric 		if (*rp == CANONUSER)
6189374Seric 		{
6199374Seric 			rvp++;
6209374Seric 			rwr = rwr->r_next;
6219374Seric 		}
6229374Seric 		else if (*rp == CANONHOST)
6239374Seric 		{
6249374Seric 			rvp++;
6259374Seric 			rwr = NULL;
6269374Seric 		}
6279374Seric 		else if (*rp == CANONNET)
6289374Seric 			rwr = NULL;
6299374Seric 
6309374Seric 		/* substitute */
6319374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
6329374Seric 		{
6339374Seric 			register struct match *m;
6349374Seric 			register char **pp;
6359374Seric 
6368058Seric 			rp = *rvp;
6379374Seric 			if (*rp != MATCHREPL)
6388058Seric 			{
6399374Seric 				if (avp >= &npvp[MAXATOM])
6409374Seric 				{
6419374Seric 					syserr("rewrite: expansion too long");
6429374Seric 					return;
6439374Seric 				}
6449374Seric 				*avp++ = rp;
6459374Seric 				continue;
6468069Seric 			}
6478058Seric 
6489374Seric 			/* substitute from LHS */
6499374Seric 			m = &mlist[rp[1] - '1'];
6509374Seric # ifdef DEBUG
6519374Seric 			if (tTd(21, 15))
6523149Seric 			{
6539374Seric 				printf("$%c:", rp[1]);
6549374Seric 				pp = m->first;
6559374Seric 				while (pp <= m->last)
6563149Seric 				{
6579374Seric 					printf(" %x=\"", *pp);
6589374Seric 					(void) fflush(stdout);
6599374Seric 					printf("%s\"", *pp++);
6603149Seric 				}
6619374Seric 				printf("\n");
6623149Seric 			}
6638226Seric # endif DEBUG
6649374Seric 			pp = m->first;
6659374Seric 			while (pp <= m->last)
6668226Seric 			{
6679374Seric 				if (avp >= &npvp[MAXATOM])
6689374Seric 				{
6699374Seric 					syserr("rewrite: expansion too long");
6709374Seric 					return;
6719374Seric 				}
6729374Seric 				*avp++ = *pp++;
6738226Seric 			}
6749374Seric 		}
6759374Seric 		*avp++ = NULL;
6769374Seric 		if (**npvp == CALLSUBR)
6779374Seric 		{
6789374Seric 			bmove((char *) &npvp[2], (char *) pvp,
6799374Seric 				(avp - npvp - 2) * sizeof *avp);
6808226Seric # ifdef DEBUG
6819374Seric 			if (tTd(21, 3))
6829374Seric 				printf("-----callsubr %s\n", npvp[1]);
6833149Seric # endif DEBUG
6849374Seric 			rewrite(pvp, atoi(npvp[1]));
6853149Seric 		}
6863149Seric 		else
6873149Seric 		{
6889374Seric 			bmove((char *) npvp, (char *) pvp,
6899374Seric 				(avp - npvp) * sizeof *avp);
6909374Seric 		}
6914100Seric # ifdef DEBUG
6929374Seric 		if (tTd(21, 4))
6939374Seric 		{
6949374Seric 			printf("rewritten as:");
6959374Seric 			printav(pvp);
6969374Seric 		}
6974100Seric # endif DEBUG
698297Seric 	}
6998069Seric 
7009279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
7018069Seric 	{
7028959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
7038069Seric 		printav(pvp);
7048069Seric 	}
7053149Seric }
7063149Seric /*
7073149Seric **  BUILDADDR -- build address from token vector.
7083149Seric **
7093149Seric **	Parameters:
7103149Seric **		tv -- token vector.
7113149Seric **		a -- pointer to address descriptor to fill.
7123149Seric **			If NULL, one will be allocated.
7133149Seric **
7143149Seric **	Returns:
7154279Seric **		NULL if there was an error.
7164279Seric **		'a' otherwise.
7173149Seric **
7183149Seric **	Side Effects:
7193149Seric **		fills in 'a'
7203149Seric */
7213149Seric 
7223149Seric ADDRESS *
7233149Seric buildaddr(tv, a)
7243149Seric 	register char **tv;
7253149Seric 	register ADDRESS *a;
7263149Seric {
7273149Seric 	static char buf[MAXNAME];
7283149Seric 	struct mailer **mp;
7293149Seric 	register struct mailer *m;
7304635Seric 	extern bool sameword();
7313149Seric 
7323149Seric 	if (a == NULL)
7333149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
7344988Seric 	clear((char *) a, sizeof *a);
7353149Seric 
7363149Seric 	/* figure out what net/mailer to use */
7373149Seric 	if (**tv != CANONNET)
7384279Seric 	{
7393149Seric 		syserr("buildaddr: no net");
7404279Seric 		return (NULL);
7414279Seric 	}
7423149Seric 	tv++;
7434635Seric 	if (sameword(*tv, "error"))
7444279Seric 	{
74510183Seric 		if (**++tv == CANONHOST)
74610183Seric 		{
74710183Seric 			setstat(atoi(*++tv));
74810183Seric 			tv++;
74910183Seric 		}
75010183Seric 		if (**tv != CANONUSER)
7514279Seric 			syserr("buildaddr: error: no user");
7524279Seric 		buf[0] = '\0';
7534279Seric 		while (*++tv != NULL)
7544279Seric 		{
7554279Seric 			if (buf[0] != '\0')
7567005Seric 				(void) strcat(buf, " ");
7577005Seric 			(void) strcat(buf, *tv);
7584279Seric 		}
7594279Seric 		usrerr(buf);
7604279Seric 		return (NULL);
7614279Seric 	}
7624598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
7633149Seric 	{
7644635Seric 		if (sameword(m->m_name, *tv))
7653149Seric 			break;
7663149Seric 	}
7673149Seric 	if (m == NULL)
7684279Seric 	{
7693149Seric 		syserr("buildaddr: unknown net %s", *tv);
7704279Seric 		return (NULL);
7714279Seric 	}
7724598Seric 	a->q_mailer = m;
7733149Seric 
7743149Seric 	/* figure out what host (if any) */
7753149Seric 	tv++;
77610690Seric 	if (!bitnset(M_LOCAL, m->m_flags))
7773149Seric 	{
7785704Seric 		if (**tv++ != CANONHOST)
7794279Seric 		{
7803149Seric 			syserr("buildaddr: no host");
7814279Seric 			return (NULL);
7824279Seric 		}
7835704Seric 		buf[0] = '\0';
7845704Seric 		while (*tv != NULL && **tv != CANONUSER)
7857005Seric 			(void) strcat(buf, *tv++);
7865704Seric 		a->q_host = newstr(buf);
7873149Seric 	}
7883149Seric 	else
7893149Seric 		a->q_host = NULL;
7903149Seric 
7913149Seric 	/* figure out the user */
7923149Seric 	if (**tv != CANONUSER)
7934279Seric 	{
7943149Seric 		syserr("buildaddr: no user");
7954279Seric 		return (NULL);
7964279Seric 	}
79711278Seric 	rewrite(++tv, 4);
79811278Seric 	cataddr(tv, buf, sizeof buf);
7993149Seric 	a->q_user = buf;
8003149Seric 
8013149Seric 	return (a);
8023149Seric }
8033188Seric /*
8044228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
8054228Seric **
8064228Seric **	Parameters:
8074228Seric **		pvp -- parameter vector to rebuild.
8084228Seric **		buf -- buffer to build the string into.
8094228Seric **		sz -- size of buf.
8104228Seric **
8114228Seric **	Returns:
8124228Seric **		none.
8134228Seric **
8144228Seric **	Side Effects:
8154228Seric **		Destroys buf.
8164228Seric */
8174228Seric 
8184228Seric cataddr(pvp, buf, sz)
8194228Seric 	char **pvp;
8204228Seric 	char *buf;
8214228Seric 	register int sz;
8224228Seric {
8234228Seric 	bool oatomtok = FALSE;
8244228Seric 	bool natomtok = FALSE;
8254228Seric 	register int i;
8264228Seric 	register char *p;
8274228Seric 
8288423Seric 	if (pvp == NULL)
8298423Seric 	{
8308423Seric 		strcpy(buf, "");
8318423Seric 		return;
8328423Seric 	}
8334228Seric 	p = buf;
83411156Seric 	sz -= 2;
8354228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
8364228Seric 	{
8378078Seric 		natomtok = (toktype(**pvp) == ATM);
8384228Seric 		if (oatomtok && natomtok)
8399042Seric 			*p++ = SpaceSub;
8404228Seric 		(void) strcpy(p, *pvp);
8414228Seric 		oatomtok = natomtok;
8424228Seric 		p += i;
84311156Seric 		sz -= i + 1;
8444228Seric 		pvp++;
8454228Seric 	}
8464228Seric 	*p = '\0';
8474228Seric }
8484228Seric /*
8493188Seric **  SAMEADDR -- Determine if two addresses are the same
8503188Seric **
8513188Seric **	This is not just a straight comparison -- if the mailer doesn't
8523188Seric **	care about the host we just ignore it, etc.
8533188Seric **
8543188Seric **	Parameters:
8553188Seric **		a, b -- pointers to the internal forms to compare.
8563188Seric **
8573188Seric **	Returns:
8583188Seric **		TRUE -- they represent the same mailbox.
8593188Seric **		FALSE -- they don't.
8603188Seric **
8613188Seric **	Side Effects:
8623188Seric **		none.
8633188Seric */
8643188Seric 
8653188Seric bool
8669374Seric sameaddr(a, b)
8673188Seric 	register ADDRESS *a;
8683188Seric 	register ADDRESS *b;
8693188Seric {
8703188Seric 	/* if they don't have the same mailer, forget it */
8713188Seric 	if (a->q_mailer != b->q_mailer)
8723188Seric 		return (FALSE);
8733188Seric 
8743188Seric 	/* if the user isn't the same, we can drop out */
8759374Seric 	if (strcmp(a->q_user, b->q_user) != 0)
8763188Seric 		return (FALSE);
8773188Seric 
8783188Seric 	/* if the mailer ignores hosts, we have succeeded! */
87910690Seric 	if (bitnset(M_LOCAL, a->q_mailer->m_flags))
8803188Seric 		return (TRUE);
8813188Seric 
8823188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
8833188Seric 	if (a->q_host == NULL || b->q_host == NULL)
8843188Seric 		return (FALSE);
8853188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
8863188Seric 		return (FALSE);
8873188Seric 
8883188Seric 	return (TRUE);
8893188Seric }
8903234Seric /*
8913234Seric **  PRINTADDR -- print address (for debugging)
8923234Seric **
8933234Seric **	Parameters:
8943234Seric **		a -- the address to print
8953234Seric **		follow -- follow the q_next chain.
8963234Seric **
8973234Seric **	Returns:
8983234Seric **		none.
8993234Seric **
9003234Seric **	Side Effects:
9013234Seric **		none.
9023234Seric */
9033234Seric 
9044317Seric # ifdef DEBUG
9054317Seric 
9063234Seric printaddr(a, follow)
9073234Seric 	register ADDRESS *a;
9083234Seric 	bool follow;
9093234Seric {
9105001Seric 	bool first = TRUE;
9115001Seric 
9123234Seric 	while (a != NULL)
9133234Seric 	{
9145001Seric 		first = FALSE;
9154443Seric 		printf("%x=", a);
9164085Seric 		(void) fflush(stdout);
9173234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
9188181Seric 		       a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host,
9198181Seric 		       a->q_user);
9208181Seric 		printf("\tnext=%x, flags=%o, alias %x\n", a->q_next, a->q_flags,
9218181Seric 		       a->q_alias);
9228181Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home,
9238181Seric 		       a->q_fullname);
9244996Seric 
9253234Seric 		if (!follow)
9263234Seric 			return;
9274996Seric 		a = a->q_next;
9283234Seric 	}
9295001Seric 	if (first)
9304443Seric 		printf("[NULL]\n");
9313234Seric }
9324317Seric 
9334317Seric # endif DEBUG
9347682Seric /*
9357682Seric **  REMOTENAME -- return the name relative to the current mailer
9367682Seric **
9377682Seric **	Parameters:
9387682Seric **		name -- the name to translate.
9398069Seric **		m -- the mailer that we want to do rewriting relative
9408069Seric **			to.
9418069Seric **		senderaddress -- if set, uses the sender rewriting rules
9428069Seric **			rather than the recipient rewriting rules.
94310310Seric **		canonical -- if set, strip out any comment information,
94410310Seric **			etc.
9457682Seric **
9467682Seric **	Returns:
9477682Seric **		the text string representing this address relative to
9487682Seric **			the receiving mailer.
9497682Seric **
9507682Seric **	Side Effects:
9517682Seric **		none.
9527682Seric **
9537682Seric **	Warnings:
9547682Seric **		The text string returned is tucked away locally;
9557682Seric **			copy it if you intend to save it.
9567682Seric */
9577682Seric 
9587682Seric char *
95910310Seric remotename(name, m, senderaddress, canonical)
9607682Seric 	char *name;
9617682Seric 	struct mailer *m;
9628069Seric 	bool senderaddress;
96310310Seric 	bool canonical;
9647682Seric {
9658069Seric 	register char **pvp;
9668069Seric 	char *fancy;
9678069Seric 	extern char *macvalue();
9688181Seric 	char *oldg = macvalue('g', CurEnv);
9697682Seric 	static char buf[MAXNAME];
9707682Seric 	char lbuf[MAXNAME];
9717682Seric 	extern char **prescan();
9727889Seric 	extern char *crackaddr();
9737682Seric 
9747755Seric # ifdef DEBUG
9757755Seric 	if (tTd(12, 1))
9767755Seric 		printf("remotename(%s)\n", name);
9777755Seric # endif DEBUG
9787755Seric 
97910177Seric 	/* don't do anything if we are tagging it as special */
98010177Seric 	if ((senderaddress ? m->m_s_rwset : m->m_r_rwset) < 0)
98110177Seric 		return (name);
98210177Seric 
9837682Seric 	/*
9848181Seric 	**  Do a heuristic crack of this name to extract any comment info.
9858181Seric 	**	This will leave the name as a comment and a $g macro.
9867889Seric 	*/
9877889Seric 
98810310Seric 	if (canonical)
98910310Seric 		fancy = "$g";
99010310Seric 	else
99110310Seric 		fancy = crackaddr(name);
9927889Seric 
9938181Seric 	/*
9948181Seric 	**  Turn the name into canonical form.
9958181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
9968181Seric 	**	If this only resolves to "user", and the "C" flag is
9978181Seric 	**	specified in the sending mailer, then the sender's
9988181Seric 	**	domain will be appended.
9998181Seric 	*/
10008181Seric 
10017889Seric 	pvp = prescan(name, '\0');
10027889Seric 	if (pvp == NULL)
10037889Seric 		return (name);
10048181Seric 	rewrite(pvp, 3);
10058181Seric 	if (CurEnv->e_fromdomain != NULL)
10068181Seric 	{
10078181Seric 		/* append from domain to this address */
10088181Seric 		register char **pxp = pvp;
10098181Seric 
10109594Seric 		/* see if there is an "@domain" in the current name */
10118181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
10128181Seric 			pxp++;
10138181Seric 		if (*pxp == NULL)
10148181Seric 		{
10159594Seric 			/* no.... append the "@domain" from the sender */
10168181Seric 			register char **qxq = CurEnv->e_fromdomain;
10178181Seric 
10189594Seric 			while ((*pxp++ = *qxq++) != NULL)
10199594Seric 				continue;
102011726Seric 			rewrite(pvp, 3);
10218181Seric 		}
10228181Seric 	}
10238181Seric 
10248181Seric 	/*
10258959Seric 	**  Do more specific rewriting.
10268181Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
10278181Seric 	**		a sender address or not.
10288181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
10298181Seric 	*/
10308181Seric 
10318069Seric 	if (senderaddress)
10327755Seric 	{
10337889Seric 		rewrite(pvp, 1);
10348069Seric 		if (m->m_s_rwset > 0)
10358069Seric 			rewrite(pvp, m->m_s_rwset);
10368069Seric 	}
10378069Seric 	else
10388069Seric 	{
10397889Seric 		rewrite(pvp, 2);
10408069Seric 		if (m->m_r_rwset > 0)
10418069Seric 			rewrite(pvp, m->m_r_rwset);
10427682Seric 	}
10437682Seric 
10448181Seric 	/*
10458959Seric 	**  Do any final sanitation the address may require.
10468959Seric 	**	This will normally be used to turn internal forms
10478959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
10488959Seric 	**	may be used as a default to the above rules.
10498959Seric 	*/
10508959Seric 
10518959Seric 	rewrite(pvp, 4);
10528959Seric 
10538959Seric 	/*
10548181Seric 	**  Now restore the comment information we had at the beginning.
10558181Seric 	*/
10568181Seric 
10577682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
10589374Seric 	define('g', lbuf, CurEnv);
10597889Seric 	expand(fancy, buf, &buf[sizeof buf - 1], CurEnv);
10609374Seric 	define('g', oldg, CurEnv);
10617682Seric 
10627682Seric # ifdef DEBUG
10637682Seric 	if (tTd(12, 1))
10647755Seric 		printf("remotename => `%s'\n", buf);
10657682Seric # endif DEBUG
10667682Seric 	return (buf);
10677682Seric }
1068