122976Smiriam /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
363589Sbostic  * Copyright (c) 1988, 1993
463589Sbostic  *	The Regents of the University of California.  All rights reserved.
533730Sbostic  *
642828Sbostic  * %sccs.include.redist.c%
733730Sbostic  */
822976Smiriam 
922976Smiriam #ifndef lint
10*67764Seric static char sccsid[] = "@(#)parseaddr.c	8.36 (Berkeley) 09/08/94";
1133730Sbostic #endif /* not lint */
1222976Smiriam 
1356678Seric # include "sendmail.h"
14297Seric 
15297Seric /*
169888Seric **  PARSEADDR -- Parse an address
17297Seric **
18297Seric **	Parses an address and breaks it up into three parts: a
19297Seric **	net to transmit the message on, the host to transmit it
20297Seric **	to, and a user on that host.  These are loaded into an
212973Seric **	ADDRESS header with the values squirreled away if necessary.
22297Seric **	The "user" part may not be a real user; the process may
23297Seric **	just reoccur on that machine.  For example, on a machine
24297Seric **	with an arpanet connection, the address
25297Seric **		csvax.bill@berkeley
26297Seric **	will break up to a "user" of 'csvax.bill' and a host
27297Seric **	of 'berkeley' -- to be transmitted over the arpanet.
28297Seric **
29297Seric **	Parameters:
30297Seric **		addr -- the address to parse.
31297Seric **		a -- a pointer to the address descriptor buffer.
32297Seric **			If NULL, a header will be created.
3364284Seric **		flags -- describe detail for parsing.  See RF_ definitions
3464284Seric **			in sendmail.h.
3511445Seric **		delim -- the character to terminate the address, passed
3611445Seric **			to prescan.
3758333Seric **		delimptr -- if non-NULL, set to the location of the
3858333Seric **			delim character that was found.
3956678Seric **		e -- the envelope that will contain this address.
40297Seric **
41297Seric **	Returns:
42297Seric **		A pointer to the address descriptor header (`a' if
43297Seric **			`a' is non-NULL).
44297Seric **		NULL on error.
45297Seric **
46297Seric **	Side Effects:
47297Seric **		none
48297Seric */
49297Seric 
509374Seric /* following delimiters are inherent to the internal algorithms */
5159278Seric # define DELIMCHARS	"()<>,;\r\n"	/* default word delimiters */
522091Seric 
532973Seric ADDRESS *
5464284Seric parseaddr(addr, a, flags, delim, delimptr, e)
55297Seric 	char *addr;
562973Seric 	register ADDRESS *a;
5764284Seric 	int flags;
5859700Seric 	int delim;
5958333Seric 	char **delimptr;
6056678Seric 	register ENVELOPE *e;
61297Seric {
623149Seric 	register char **pvp;
6358333Seric 	auto char *delimptrbuf;
6459084Seric 	bool queueup;
6516914Seric 	char pvpbuf[PSBUFSIZE];
6656678Seric 	extern ADDRESS *buildaddr();
6757388Seric 	extern bool invalidaddr();
68297Seric 
69297Seric 	/*
70297Seric 	**  Initialize and prescan address.
71297Seric 	*/
72297Seric 
7356678Seric 	e->e_to = addr;
747675Seric 	if (tTd(20, 1))
759888Seric 		printf("\n--parseaddr(%s)\n", addr);
763188Seric 
7758333Seric 	if (delimptr == NULL)
7858333Seric 		delimptr = &delimptrbuf;
7958333Seric 
8065066Seric 	pvp = prescan(addr, delim, pvpbuf, sizeof pvpbuf, delimptr);
813149Seric 	if (pvp == NULL)
8256729Seric 	{
8356729Seric 		if (tTd(20, 1))
8456729Seric 			printf("parseaddr-->NULL\n");
85297Seric 		return (NULL);
8656729Seric 	}
87297Seric 
8864726Seric 	if (invalidaddr(addr, delim == '\0' ? NULL : *delimptr))
8964726Seric 	{
9064726Seric 		if (tTd(20, 1))
9164726Seric 			printf("parseaddr-->bad address\n");
9264726Seric 		return NULL;
9364726Seric 	}
9464726Seric 
95297Seric 	/*
9664348Seric 	**  Save addr if we are going to have to.
9764348Seric 	**
9864348Seric 	**	We have to do this early because there is a chance that
9964348Seric 	**	the map lookups in the rewriting rules could clobber
10064348Seric 	**	static memory somewhere.
10164348Seric 	*/
10264348Seric 
10364348Seric 	if (bitset(RF_COPYPADDR, flags) && addr != NULL)
10464348Seric 	{
10564348Seric 		char savec = **delimptr;
10664348Seric 
10764348Seric 		if (savec != '\0')
10864348Seric 			**delimptr = '\0';
10966794Seric 		e->e_to = addr = newstr(addr);
11064348Seric 		if (savec != '\0')
11164348Seric 			**delimptr = savec;
11264348Seric 	}
11364348Seric 
11464348Seric 	/*
1153149Seric 	**  Apply rewriting rules.
1167889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
117297Seric 	*/
118297Seric 
11959084Seric 	queueup = FALSE;
12065071Seric 	if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL)
12159084Seric 		queueup = TRUE;
12265071Seric 	if (rewrite(pvp, 0, 0, e) == EX_TEMPFAIL)
12359084Seric 		queueup = TRUE;
124297Seric 
125297Seric 
126297Seric 	/*
1273149Seric 	**  Build canonical address from pvp.
128297Seric 	*/
129297Seric 
13064284Seric 	a = buildaddr(pvp, a, flags, e);
131297Seric 
132297Seric 	/*
1333149Seric 	**  Make local copies of the host & user and then
1343149Seric 	**  transport them out.
135297Seric 	*/
136297Seric 
13764348Seric 	allocaddr(a, flags, addr);
13864306Seric 	if (bitset(QBADADDR, a->q_flags))
13964306Seric 		return a;
14056678Seric 
14156678Seric 	/*
14259084Seric 	**  If there was a parsing failure, mark it for queueing.
14359084Seric 	*/
14459084Seric 
14559084Seric 	if (queueup)
14659595Seric 	{
14759734Seric 		char *msg = "Transient parse error -- message queued for future delivery";
14859734Seric 
14959595Seric 		if (tTd(20, 1))
15059595Seric 			printf("parseaddr: queuing message\n");
15159734Seric 		message(msg);
15259734Seric 		if (e->e_message == NULL)
15360009Seric 			e->e_message = newstr(msg);
15459084Seric 		a->q_flags |= QQUEUEUP;
15559595Seric 	}
15659084Seric 
15759084Seric 	/*
15856678Seric 	**  Compute return value.
15956678Seric 	*/
16056678Seric 
16156678Seric 	if (tTd(20, 1))
1628078Seric 	{
16356678Seric 		printf("parseaddr-->");
16456678Seric 		printaddr(a, FALSE);
16556678Seric 	}
16656678Seric 
16756678Seric 	return (a);
16856678Seric }
16956678Seric /*
17057388Seric **  INVALIDADDR -- check for address containing meta-characters
17157388Seric **
17257388Seric **	Parameters:
17357388Seric **		addr -- the address to check.
17457388Seric **
17557388Seric **	Returns:
17657388Seric **		TRUE -- if the address has any "wierd" characters
17757388Seric **		FALSE -- otherwise.
17857388Seric */
17957388Seric 
18057388Seric bool
18164726Seric invalidaddr(addr, delimptr)
18257388Seric 	register char *addr;
18364726Seric 	char *delimptr;
18457388Seric {
18564726Seric 	char savedelim;
18664726Seric 
18764726Seric 	if (delimptr != NULL)
18864764Seric 	{
18964726Seric 		savedelim = *delimptr;
19064764Seric 		if (savedelim != '\0')
19164764Seric 			*delimptr = '\0';
19264764Seric 	}
19364726Seric #if 0
19464726Seric 	/* for testing.... */
19564726Seric 	if (strcmp(addr, "INvalidADDR") == 0)
19657388Seric 	{
19764726Seric 		usrerr("553 INvalid ADDRess");
19864764Seric 		goto addrfailure;
19957388Seric 	}
20064726Seric #endif
20164726Seric 	for (; *addr != '\0'; addr++)
20264726Seric 	{
20364726Seric 		if ((*addr & 0340) == 0200)
20464726Seric 			break;
20564726Seric 	}
20664726Seric 	if (*addr == '\0')
20764764Seric 	{
20864764Seric 		if (savedelim != '\0' && delimptr != NULL)
20964764Seric 			*delimptr = savedelim;
21064726Seric 		return FALSE;
21164764Seric 	}
21264726Seric 	setstat(EX_USAGE);
21364726Seric 	usrerr("553 Address contained invalid control characters");
21464764Seric   addrfailure:
21564764Seric 	if (savedelim != '\0' && delimptr != NULL)
21664764Seric 		*delimptr = savedelim;
21764726Seric 	return TRUE;
21857388Seric }
21957388Seric /*
22056678Seric **  ALLOCADDR -- do local allocations of address on demand.
22156678Seric **
22256678Seric **	Also lowercases the host name if requested.
22356678Seric **
22456678Seric **	Parameters:
22556678Seric **		a -- the address to reallocate.
22664284Seric **		flags -- the copy flag (see RF_ definitions in sendmail.h
22764284Seric **			for a description).
22856678Seric **		paddr -- the printname of the address.
22956678Seric **
23056678Seric **	Returns:
23156678Seric **		none.
23256678Seric **
23356678Seric **	Side Effects:
23456678Seric **		Copies portions of a into local buffers as requested.
23556678Seric */
23656678Seric 
23764348Seric allocaddr(a, flags, paddr)
23856678Seric 	register ADDRESS *a;
23964284Seric 	int flags;
24056678Seric 	char *paddr;
24156678Seric {
24258673Seric 	if (tTd(24, 4))
24367693Seric 		printf("allocaddr(flags=%x, paddr=%s)\n", flags, paddr);
24458673Seric 
24564348Seric 	a->q_paddr = paddr;
2468078Seric 
24724944Seric 	if (a->q_user == NULL)
24824944Seric 		a->q_user = "";
24924944Seric 	if (a->q_host == NULL)
25024944Seric 		a->q_host = "";
25124944Seric 
25264284Seric 	if (bitset(RF_COPYPARSE, flags))
253297Seric 	{
25424944Seric 		a->q_host = newstr(a->q_host);
2553149Seric 		if (a->q_user != a->q_paddr)
2563149Seric 			a->q_user = newstr(a->q_user);
257297Seric 	}
258297Seric 
25956678Seric 	if (a->q_paddr == NULL)
26056678Seric 		a->q_paddr = a->q_user;
261297Seric }
262297Seric /*
263297Seric **  PRESCAN -- Prescan name and make it canonical
264297Seric **
2659374Seric **	Scans a name and turns it into a set of tokens.  This process
2669374Seric **	deletes blanks and comments (in parentheses).
267297Seric **
268297Seric **	This routine knows about quoted strings and angle brackets.
269297Seric **
270297Seric **	There are certain subtleties to this routine.  The one that
271297Seric **	comes to mind now is that backslashes on the ends of names
272297Seric **	are silently stripped off; this is intentional.  The problem
273297Seric **	is that some versions of sndmsg (like at LBL) set the kill
274297Seric **	character to something other than @ when reading addresses;
275297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
276297Seric **	berknet mailer.
277297Seric **
278297Seric **	Parameters:
279297Seric **		addr -- the name to chomp.
280297Seric **		delim -- the delimiter for the address, normally
281297Seric **			'\0' or ','; \0 is accepted in any case.
28215284Seric **			If '\t' then we are reading the .cf file.
28316914Seric **		pvpbuf -- place to put the saved text -- note that
28416914Seric **			the pointers are static.
28565066Seric **		pvpbsize -- size of pvpbuf.
28658333Seric **		delimptr -- if non-NULL, set to the location of the
28758333Seric **			terminating delimiter.
288297Seric **
289297Seric **	Returns:
2903149Seric **		A pointer to a vector of tokens.
291297Seric **		NULL on error.
292297Seric */
293297Seric 
2948078Seric /* states and character types */
2958078Seric # define OPR		0	/* operator */
2968078Seric # define ATM		1	/* atom */
2978078Seric # define QST		2	/* in quoted string */
2988078Seric # define SPC		3	/* chewing up spaces */
2998078Seric # define ONE		4	/* pick up one character */
3003149Seric 
3018078Seric # define NSTATES	5	/* number of states */
3028078Seric # define TYPE		017	/* mask to select state type */
3038078Seric 
3048078Seric /* meta bits for table */
3058078Seric # define M		020	/* meta character; don't pass through */
3068078Seric # define B		040	/* cause a break */
3078078Seric # define MB		M|B	/* meta-break */
3088078Seric 
3098078Seric static short StateTab[NSTATES][NSTATES] =
3108078Seric {
3118087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
3129051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
3139051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
3149051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
3158078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
3168078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
3178078Seric };
3188078Seric 
31965092Seric /* token type table -- it gets modified with $o characters */
32065092Seric static TokTypeTab[256] =
32165092Seric {
32265092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
32365092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
32465092Seric 	SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM,ATM,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
32565092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
32665092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
32765092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
32865092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
32965092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
33065092Seric 	OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
33165092Seric 	OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
33265092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
33365092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
33465092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
33565092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
33665092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
33765092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
33865092Seric };
33965092Seric 
34065092Seric #define toktype(c)	((int) TokTypeTab[(c) & 0xff])
34165092Seric 
34265092Seric 
3438078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
3448078Seric 
3453149Seric char **
34665066Seric prescan(addr, delim, pvpbuf, pvpbsize, delimptr)
347297Seric 	char *addr;
348297Seric 	char delim;
34916914Seric 	char pvpbuf[];
35058333Seric 	char **delimptr;
351297Seric {
352297Seric 	register char *p;
3538078Seric 	register char *q;
3549346Seric 	register int c;
3553149Seric 	char **avp;
356297Seric 	bool bslashmode;
357297Seric 	int cmntcnt;
3588423Seric 	int anglecnt;
3593149Seric 	char *tok;
3608078Seric 	int state;
3618078Seric 	int newstate;
36259747Seric 	char *saveto = CurEnv->e_to;
3638078Seric 	static char *av[MAXATOM+1];
36465092Seric 	static char firsttime = TRUE;
36556678Seric 	extern int errno;
366297Seric 
36765092Seric 	if (firsttime)
36865092Seric 	{
36965092Seric 		/* initialize the token type table */
37065092Seric 		char obuf[50];
37165092Seric 
37265092Seric 		firsttime = FALSE;
37365092Seric 		expand("\201o", obuf, &obuf[sizeof obuf - sizeof DELIMCHARS], CurEnv);
37465092Seric 		strcat(obuf, DELIMCHARS);
37565092Seric 		for (p = obuf; *p != '\0'; p++)
37665092Seric 		{
37765092Seric 			if (TokTypeTab[*p & 0xff] == ATM)
37865092Seric 				TokTypeTab[*p & 0xff] = OPR;
37965092Seric 		}
38065092Seric 	}
38165092Seric 
38215253Seric 	/* make sure error messages don't have garbage on them */
38315253Seric 	errno = 0;
38415253Seric 
38516914Seric 	q = pvpbuf;
3863149Seric 	bslashmode = FALSE;
3877800Seric 	cmntcnt = 0;
3888423Seric 	anglecnt = 0;
3893149Seric 	avp = av;
39056678Seric 	state = ATM;
3918078Seric 	c = NOCHAR;
3928078Seric 	p = addr;
39359747Seric 	CurEnv->e_to = p;
39456764Seric 	if (tTd(22, 11))
395297Seric 	{
3968078Seric 		printf("prescan: ");
3978078Seric 		xputs(p);
39823109Seric 		(void) putchar('\n');
3998078Seric 	}
4008078Seric 
4018078Seric 	do
4028078Seric 	{
4033149Seric 		/* read a token */
4043149Seric 		tok = q;
4058078Seric 		for (;;)
406297Seric 		{
4078078Seric 			/* store away any old lookahead character */
40859277Seric 			if (c != NOCHAR && !bslashmode)
4098078Seric 			{
41015284Seric 				/* see if there is room */
41165066Seric 				if (q >= &pvpbuf[pvpbsize - 5])
4128078Seric 				{
41358151Seric 					usrerr("553 Address too long");
41465015Seric 	returnnull:
41558333Seric 					if (delimptr != NULL)
41658333Seric 						*delimptr = p;
41759747Seric 					CurEnv->e_to = saveto;
4188078Seric 					return (NULL);
4198078Seric 				}
42015284Seric 
42115284Seric 				/* squirrel it away */
4228078Seric 				*q++ = c;
4238078Seric 			}
4248078Seric 
4258078Seric 			/* read a new input character */
4268078Seric 			c = *p++;
42757631Seric 			if (c == '\0')
42856764Seric 			{
42956764Seric 				/* diagnose and patch up bad syntax */
43056764Seric 				if (state == QST)
43156764Seric 				{
43264767Seric 					usrerr("653 Unbalanced '\"'");
43356764Seric 					c = '"';
43456764Seric 				}
43556764Seric 				else if (cmntcnt > 0)
43656764Seric 				{
43764767Seric 					usrerr("653 Unbalanced '('");
43856764Seric 					c = ')';
43956764Seric 				}
44056764Seric 				else if (anglecnt > 0)
44156764Seric 				{
44256764Seric 					c = '>';
44364767Seric 					usrerr("653 Unbalanced '<'");
44456764Seric 				}
44556764Seric 				else
44656764Seric 					break;
44715284Seric 
44856764Seric 				p--;
44956764Seric 			}
45057631Seric 			else if (c == delim && anglecnt <= 0 &&
45157631Seric 					cmntcnt <= 0 && state != QST)
45257631Seric 				break;
45356764Seric 
4548078Seric 			if (tTd(22, 101))
4558078Seric 				printf("c=%c, s=%d; ", c, state);
4568078Seric 
4573149Seric 			/* chew up special characters */
4583149Seric 			*q = '\0';
4593149Seric 			if (bslashmode)
4603149Seric 			{
46159105Seric 				bslashmode = FALSE;
46259105Seric 
46324944Seric 				/* kludge \! for naive users */
46458061Seric 				if (cmntcnt > 0)
46559105Seric 				{
46658061Seric 					c = NOCHAR;
46759105Seric 					continue;
46859105Seric 				}
46959105Seric 				else if (c != '!' || state == QST)
47059105Seric 				{
47156678Seric 					*q++ = '\\';
47259105Seric 					continue;
47359105Seric 				}
4743149Seric 			}
47556678Seric 
47656678Seric 			if (c == '\\')
4773149Seric 			{
4783149Seric 				bslashmode = TRUE;
4793149Seric 			}
48056678Seric 			else if (state == QST)
4818514Seric 			{
4828514Seric 				/* do nothing, just avoid next clauses */
4838514Seric 			}
4848078Seric 			else if (c == '(')
4854100Seric 			{
4868078Seric 				cmntcnt++;
4878078Seric 				c = NOCHAR;
4884100Seric 			}
4898078Seric 			else if (c == ')')
4903149Seric 			{
4918078Seric 				if (cmntcnt <= 0)
4923149Seric 				{
49364767Seric 					usrerr("653 Unbalanced ')'");
49463844Seric 					c = NOCHAR;
4953149Seric 				}
4968078Seric 				else
4978078Seric 					cmntcnt--;
4988078Seric 			}
4998078Seric 			else if (cmntcnt > 0)
5008078Seric 				c = NOCHAR;
5018423Seric 			else if (c == '<')
5028423Seric 				anglecnt++;
5038423Seric 			else if (c == '>')
5048423Seric 			{
5058423Seric 				if (anglecnt <= 0)
5068423Seric 				{
50764767Seric 					usrerr("653 Unbalanced '>'");
50863844Seric 					c = NOCHAR;
5098423Seric 				}
51063844Seric 				else
51163844Seric 					anglecnt--;
5128423Seric 			}
51358050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
51411423Seric 				c = ' ';
5153149Seric 
5168078Seric 			if (c == NOCHAR)
5178078Seric 				continue;
5183149Seric 
5198078Seric 			/* see if this is end of input */
52011405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
5213149Seric 				break;
5223149Seric 
5238078Seric 			newstate = StateTab[state][toktype(c)];
5248078Seric 			if (tTd(22, 101))
5258078Seric 				printf("ns=%02o\n", newstate);
5268078Seric 			state = newstate & TYPE;
5278078Seric 			if (bitset(M, newstate))
5288078Seric 				c = NOCHAR;
5298078Seric 			if (bitset(B, newstate))
5304228Seric 				break;
531297Seric 		}
5323149Seric 
5333149Seric 		/* new token */
5348078Seric 		if (tok != q)
5351378Seric 		{
5368078Seric 			*q++ = '\0';
5378078Seric 			if (tTd(22, 36))
538297Seric 			{
5398078Seric 				printf("tok=");
5408078Seric 				xputs(tok);
54123109Seric 				(void) putchar('\n');
542297Seric 			}
5438078Seric 			if (avp >= &av[MAXATOM])
544297Seric 			{
54558151Seric 				syserr("553 prescan: too many tokens");
54665015Seric 				goto returnnull;
547297Seric 			}
54865015Seric 			if (q - tok > MAXNAME)
54965015Seric 			{
55065015Seric 				syserr("553 prescan: token too long");
55165015Seric 				goto returnnull;
55265015Seric 			}
5538078Seric 			*avp++ = tok;
554297Seric 		}
5558423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
5563149Seric 	*avp = NULL;
55758333Seric 	p--;
55858333Seric 	if (delimptr != NULL)
55958333Seric 		*delimptr = p;
56056764Seric 	if (tTd(22, 12))
56156764Seric 	{
56256764Seric 		printf("prescan==>");
56356764Seric 		printav(av);
56456764Seric 	}
56559747Seric 	CurEnv->e_to = saveto;
56658546Seric 	if (av[0] == NULL)
56764966Seric 	{
56864966Seric 		if (tTd(22, 1))
56964966Seric 			printf("prescan: null leading token\n");
57058546Seric 		return (NULL);
57164966Seric 	}
57258403Seric 	return (av);
5733149Seric }
5743149Seric /*
5753149Seric **  REWRITE -- apply rewrite rules to token vector.
5763149Seric **
5774476Seric **	This routine is an ordered production system.  Each rewrite
5784476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5794476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5804476Seric **
5814476Seric **	For each rewrite rule, 'avp' points the address vector we
5824476Seric **	are trying to match against, and 'pvp' points to the pattern.
5838058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5849585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5859585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5864476Seric **
5874476Seric **	When a match between avp & pvp does not match, we try to
5889585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5894476Seric **	we must also back out the match in mvp.  If we reach a
5908058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5918058Seric **	over again.
5924476Seric **
5934476Seric **	When we finally match, we rewrite the address vector
5944476Seric **	and try over again.
5954476Seric **
5963149Seric **	Parameters:
5973149Seric **		pvp -- pointer to token vector.
59859027Seric **		ruleset -- the ruleset to use for rewriting.
59965071Seric **		reclevel -- recursion level (to catch loops).
60059027Seric **		e -- the current envelope.
6013149Seric **
6023149Seric **	Returns:
60359084Seric **		A status code.  If EX_TEMPFAIL, higher level code should
60459084Seric **			attempt recovery.
6053149Seric **
6063149Seric **	Side Effects:
6073149Seric **		pvp is modified.
6083149Seric */
6092091Seric 
6103149Seric struct match
6113149Seric {
6124468Seric 	char	**first;	/* first token matched */
6134468Seric 	char	**last;		/* last token matched */
61458825Seric 	char	**pattern;	/* pointer to pattern */
6153149Seric };
6163149Seric 
6174468Seric # define MAXMATCH	9	/* max params per rewrite */
6183149Seric 
61965136Seric # ifndef MAXRULERECURSION
62065136Seric #  define MAXRULERECURSION	50	/* max recursion depth */
62165136Seric # endif
6223149Seric 
62365136Seric 
62459084Seric int
62565071Seric rewrite(pvp, ruleset, reclevel, e)
6263149Seric 	char **pvp;
6274070Seric 	int ruleset;
62865071Seric 	int reclevel;
62959027Seric 	register ENVELOPE *e;
6303149Seric {
6313149Seric 	register char *ap;		/* address pointer */
6323149Seric 	register char *rp;		/* rewrite pointer */
6333149Seric 	register char **avp;		/* address vector pointer */
6343149Seric 	register char **rvp;		/* rewrite vector pointer */
6358058Seric 	register struct match *mlp;	/* cur ptr into mlist */
6368058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
63758866Seric 	int ruleno;			/* current rule number */
63859084Seric 	int rstat = EX_OK;		/* return status */
63964740Seric 	int loopcount;
64056678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
6413149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
6423149Seric 
64367262Seric 	if (OpMode == MD_TEST || tTd(21, 1))
6443149Seric 	{
6458959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
64656678Seric 		printav(pvp);
6473149Seric 	}
64856678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
64956326Seric 	{
65058151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
65159084Seric 		return EX_CONFIG;
65256326Seric 	}
65365136Seric 	if (reclevel++ > MAXRULERECURSION)
65465071Seric 	{
65565071Seric 		syserr("rewrite: infinite recursion, ruleset %d", ruleset);
65665071Seric 		return EX_CONFIG;
65765071Seric 	}
65856678Seric 	if (pvp == NULL)
65959084Seric 		return EX_USAGE;
66056326Seric 
6613149Seric 	/*
66256678Seric 	**  Run through the list of rewrite rules, applying
66356678Seric 	**	any that match.
6643149Seric 	*/
6653149Seric 
66658866Seric 	ruleno = 1;
66764740Seric 	loopcount = 0;
6684070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6693149Seric 	{
6707675Seric 		if (tTd(21, 12))
671297Seric 		{
6728069Seric 			printf("-----trying rule:");
67356678Seric 			printav(rwr->r_lhs);
6743149Seric 		}
6753149Seric 
6763149Seric 		/* try to match on this rule */
6774468Seric 		mlp = mlist;
6788058Seric 		rvp = rwr->r_lhs;
6798058Seric 		avp = pvp;
68058866Seric 		if (++loopcount > 100)
6813149Seric 		{
68258866Seric 			syserr("554 Infinite loop in ruleset %d, rule %d",
68358866Seric 				ruleset, ruleno);
68458866Seric 			if (tTd(21, 1))
68552637Seric 			{
68656678Seric 				printf("workspace: ");
68756678Seric 				printav(pvp);
68852637Seric 			}
68958866Seric 			break;
69058866Seric 		}
69158866Seric 
69258866Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
69358866Seric 		{
6943149Seric 			rp = *rvp;
6958058Seric 			if (tTd(21, 35))
6968058Seric 			{
69758825Seric 				printf("ADVANCE rp=");
69857531Seric 				xputs(rp);
69957532Seric 				printf(", ap=");
7008058Seric 				xputs(ap);
7018069Seric 				printf("\n");
7028058Seric 			}
70356678Seric 			if (rp == NULL)
70456326Seric 			{
7053149Seric 				/* end-of-pattern before end-of-address */
7068058Seric 				goto backup;
70756678Seric 			}
70858173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
70958827Seric 			    (*rp & 0377) != MATCHZERO)
71056326Seric 			{
71158825Seric 				/* end-of-input with patterns left */
71258825Seric 				goto backup;
713297Seric 			}
71456326Seric 
71558050Seric 			switch (*rp & 0377)
7168058Seric 			{
71756678Seric 				register STAB *s;
71858814Seric 				char buf[MAXLINE];
71956326Seric 
72056678Seric 			  case MATCHCLASS:
72158825Seric 				/* match any phrase in a class */
72258825Seric 				mlp->pattern = rvp;
72358814Seric 				mlp->first = avp;
72458814Seric 	extendclass:
72558825Seric 				ap = *avp;
72658825Seric 				if (ap == NULL)
72758814Seric 					goto backup;
72858814Seric 				mlp->last = avp++;
72958814Seric 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
73058814Seric 				s = stab(buf, ST_CLASS, ST_FIND);
73156678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
73256326Seric 				{
73358825Seric 					if (tTd(21, 36))
73458825Seric 					{
73558825Seric 						printf("EXTEND  rp=");
73658825Seric 						xputs(rp);
73758825Seric 						printf(", ap=");
73858825Seric 						xputs(ap);
73958825Seric 						printf("\n");
74058825Seric 					}
74158825Seric 					goto extendclass;
74256326Seric 				}
74358825Seric 				if (tTd(21, 36))
74458825Seric 					printf("CLMATCH\n");
74558814Seric 				mlp++;
74658814Seric 				break;
7474060Seric 
74858825Seric 			  case MATCHNCLASS:
74958825Seric 				/* match any token not in a class */
75058825Seric 				s = stab(ap, ST_CLASS, ST_FIND);
75158825Seric 				if (s != NULL && bitnset(rp[1], s->s_class))
75258825Seric 					goto backup;
75358825Seric 
75458825Seric 				/* fall through */
75558825Seric 
75656678Seric 			  case MATCHONE:
75756678Seric 			  case MATCHANY:
75856678Seric 				/* match exactly one token */
75958825Seric 				mlp->pattern = rvp;
76056678Seric 				mlp->first = avp;
76156678Seric 				mlp->last = avp++;
7628058Seric 				mlp++;
76356678Seric 				break;
7648058Seric 
76556678Seric 			  case MATCHZANY:
76656678Seric 				/* match zero or more tokens */
76758825Seric 				mlp->pattern = rvp;
76856678Seric 				mlp->first = avp;
76956678Seric 				mlp->last = avp - 1;
77056678Seric 				mlp++;
77156678Seric 				break;
77256326Seric 
77358827Seric 			  case MATCHZERO:
77458173Seric 				/* match zero tokens */
77558173Seric 				break;
77658173Seric 
77759027Seric 			  case MACRODEXPAND:
77859027Seric 				/*
77959027Seric 				**  Match against run-time macro.
78059027Seric 				**  This algorithm is broken for the
78159027Seric 				**  general case (no recursive macros,
78259027Seric 				**  improper tokenization) but should
78359027Seric 				**  work for the usual cases.
78459027Seric 				*/
78559027Seric 
78659027Seric 				ap = macvalue(rp[1], e);
78759027Seric 				mlp->first = avp;
78859027Seric 				if (tTd(21, 2))
78959027Seric 					printf("rewrite: LHS $&%c => \"%s\"\n",
79059027Seric 						rp[1],
79159027Seric 						ap == NULL ? "(NULL)" : ap);
79259027Seric 
79359027Seric 				if (ap == NULL)
79459027Seric 					break;
79560502Seric 				while (*ap != '\0')
79659027Seric 				{
79759027Seric 					if (*avp == NULL ||
79859027Seric 					    strncasecmp(ap, *avp, strlen(*avp)) != 0)
79959027Seric 					{
80059027Seric 						/* no match */
80159027Seric 						avp = mlp->first;
80259027Seric 						goto backup;
80359027Seric 					}
80459027Seric 					ap += strlen(*avp++);
80559027Seric 				}
80659027Seric 
80759027Seric 				/* match */
80859027Seric 				break;
80959027Seric 
81056678Seric 			  default:
81156678Seric 				/* must have exact match */
81256678Seric 				if (strcasecmp(rp, ap))
8138058Seric 					goto backup;
8144468Seric 				avp++;
81556678Seric 				break;
8163149Seric 			}
8173149Seric 
81856678Seric 			/* successful match on this token */
8193149Seric 			rvp++;
8203149Seric 			continue;
8213149Seric 
82258825Seric 	  backup:
82356678Seric 			/* match failed -- back up */
82458825Seric 			while (--mlp >= mlist)
8253149Seric 			{
82658825Seric 				rvp = mlp->pattern;
82756678Seric 				rp = *rvp;
82858825Seric 				avp = mlp->last + 1;
82958825Seric 				ap = *avp;
83058825Seric 
83158825Seric 				if (tTd(21, 36))
83258825Seric 				{
83358825Seric 					printf("BACKUP  rp=");
83458825Seric 					xputs(rp);
83558825Seric 					printf(", ap=");
83658825Seric 					xputs(ap);
83758825Seric 					printf("\n");
83858825Seric 				}
83958825Seric 
84058825Seric 				if (ap == NULL)
84158825Seric 				{
84258825Seric 					/* run off the end -- back up again */
84358825Seric 					continue;
84458825Seric 				}
84558050Seric 				if ((*rp & 0377) == MATCHANY ||
84658050Seric 				    (*rp & 0377) == MATCHZANY)
8474468Seric 				{
84856678Seric 					/* extend binding and continue */
84958825Seric 					mlp->last = avp++;
85056678Seric 					rvp++;
85158825Seric 					mlp++;
85256678Seric 					break;
8534468Seric 				}
85458825Seric 				if ((*rp & 0377) == MATCHCLASS)
85556678Seric 				{
85658814Seric 					/* extend binding and try again */
85763397Seric 					mlp->last = avp;
85858814Seric 					goto extendclass;
85958814Seric 				}
8603149Seric 			}
8613149Seric 
86258825Seric 			if (mlp < mlist)
86356678Seric 			{
86456678Seric 				/* total failure to match */
86556326Seric 				break;
8663149Seric 			}
867297Seric 		}
8683149Seric 
8693149Seric 		/*
87056678Seric 		**  See if we successfully matched
8713149Seric 		*/
8723149Seric 
87358827Seric 		if (mlp < mlist || *rvp != NULL)
8743149Seric 		{
8759374Seric 			if (tTd(21, 10))
8769374Seric 				printf("----- rule fails\n");
8779374Seric 			rwr = rwr->r_next;
87858866Seric 			ruleno++;
87964740Seric 			loopcount = 0;
8809374Seric 			continue;
8819374Seric 		}
8823149Seric 
8839374Seric 		rvp = rwr->r_rhs;
8849374Seric 		if (tTd(21, 12))
8859374Seric 		{
8869374Seric 			printf("-----rule matches:");
88756678Seric 			printav(rvp);
8889374Seric 		}
8899374Seric 
8909374Seric 		rp = *rvp;
89158050Seric 		if ((*rp & 0377) == CANONUSER)
8929374Seric 		{
8939374Seric 			rvp++;
8949374Seric 			rwr = rwr->r_next;
89558866Seric 			ruleno++;
89664740Seric 			loopcount = 0;
8979374Seric 		}
89858050Seric 		else if ((*rp & 0377) == CANONHOST)
8999374Seric 		{
9009374Seric 			rvp++;
9019374Seric 			rwr = NULL;
9029374Seric 		}
90358050Seric 		else if ((*rp & 0377) == CANONNET)
9049374Seric 			rwr = NULL;
9059374Seric 
9069374Seric 		/* substitute */
9079374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
9089374Seric 		{
9099374Seric 			register struct match *m;
9109374Seric 			register char **pp;
9119374Seric 
9128058Seric 			rp = *rvp;
91358050Seric 			if ((*rp & 0377) == MATCHREPL)
9148058Seric 			{
91516914Seric 				/* substitute from LHS */
91616914Seric 				m = &mlist[rp[1] - '1'];
91756678Seric 				if (m < mlist || m >= mlp)
9189374Seric 				{
91958151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
92056326Seric 						ruleset, rp[1]);
92159084Seric 					return EX_CONFIG;
9229374Seric 				}
92316914Seric 				if (tTd(21, 15))
92416914Seric 				{
92516914Seric 					printf("$%c:", rp[1]);
92616914Seric 					pp = m->first;
92756678Seric 					while (pp <= m->last)
92816914Seric 					{
92916914Seric 						printf(" %x=\"", *pp);
93016914Seric 						(void) fflush(stdout);
93116914Seric 						printf("%s\"", *pp++);
93216914Seric 					}
93316914Seric 					printf("\n");
93416914Seric 				}
9359374Seric 				pp = m->first;
93656678Seric 				while (pp <= m->last)
9373149Seric 				{
93816914Seric 					if (avp >= &npvp[MAXATOM])
93956678Seric 					{
94058151Seric 						syserr("554 rewrite: expansion too long");
94159084Seric 						return EX_DATAERR;
94256678Seric 					}
94316914Seric 					*avp++ = *pp++;
9443149Seric 				}
9453149Seric 			}
94616914Seric 			else
9478226Seric 			{
94816914Seric 				/* vanilla replacement */
9499374Seric 				if (avp >= &npvp[MAXATOM])
95016889Seric 				{
95156678Seric 	toolong:
95258151Seric 					syserr("554 rewrite: expansion too long");
95359084Seric 					return EX_DATAERR;
95416889Seric 				}
95559027Seric 				if ((*rp & 0377) != MACRODEXPAND)
95659027Seric 					*avp++ = rp;
95759027Seric 				else
95859027Seric 				{
95959027Seric 					*avp = macvalue(rp[1], e);
96059027Seric 					if (tTd(21, 2))
96159027Seric 						printf("rewrite: RHS $&%c => \"%s\"\n",
96259027Seric 							rp[1],
96359027Seric 							*avp == NULL ? "(NULL)" : *avp);
96459027Seric 					if (*avp != NULL)
96559027Seric 						avp++;
96659027Seric 				}
9678226Seric 			}
9689374Seric 		}
9699374Seric 		*avp++ = NULL;
97016914Seric 
97116914Seric 		/*
97256678Seric 		**  Check for any hostname/keyword lookups.
97316914Seric 		*/
97416914Seric 
97516914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
97616914Seric 		{
97756678Seric 			char **hbrvp;
97816914Seric 			char **xpvp;
97916914Seric 			int trsize;
98056678Seric 			char *replac;
98156678Seric 			int endtoken;
98256678Seric 			STAB *map;
98356678Seric 			char *mapname;
98456678Seric 			char **key_rvp;
98556678Seric 			char **arg_rvp;
98656678Seric 			char **default_rvp;
98756678Seric 			char buf[MAXNAME + 1];
98816914Seric 			char *pvpb1[MAXATOM + 1];
98956823Seric 			char *argvect[10];
99017174Seric 			char pvpbuf[PSBUFSIZE];
99164404Seric 			char *nullpvp[1];
99216914Seric 
99358050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
99458050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
99516914Seric 				continue;
99616914Seric 
99716914Seric 			/*
99856678Seric 			**  Got a hostname/keyword lookup.
99916914Seric 			**
100016914Seric 			**	This could be optimized fairly easily.
100116914Seric 			*/
100216914Seric 
100316914Seric 			hbrvp = rvp;
100458050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
100556327Seric 			{
100656678Seric 				endtoken = HOSTEND;
100756678Seric 				mapname = "host";
100856327Seric 			}
100956326Seric 			else
101056327Seric 			{
101156678Seric 				endtoken = LOOKUPEND;
101256678Seric 				mapname = *++rvp;
101356327Seric 			}
101456678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
101556678Seric 			if (map == NULL)
101658151Seric 				syserr("554 rewrite: map %s not found", mapname);
101756678Seric 
101856678Seric 			/* extract the match part */
101956678Seric 			key_rvp = ++rvp;
102056823Seric 			default_rvp = NULL;
102156823Seric 			arg_rvp = argvect;
102256823Seric 			xpvp = NULL;
102356823Seric 			replac = pvpbuf;
102458050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
102553654Seric 			{
102658050Seric 				int nodetype = **rvp & 0377;
102756823Seric 
102856823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
102956678Seric 				{
103056823Seric 					rvp++;
103156823Seric 					continue;
103256823Seric 				}
103356823Seric 
103456823Seric 				*rvp++ = NULL;
103556823Seric 
103656823Seric 				if (xpvp != NULL)
103756823Seric 				{
103858814Seric 					cataddr(xpvp, NULL, replac,
103958082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
104058082Seric 						'\0');
104156823Seric 					*++arg_rvp = replac;
104256823Seric 					replac += strlen(replac) + 1;
104356823Seric 					xpvp = NULL;
104456823Seric 				}
104556823Seric 				switch (nodetype)
104656823Seric 				{
104756678Seric 				  case CANONHOST:
104856823Seric 					xpvp = rvp;
104956678Seric 					break;
105056678Seric 
105156678Seric 				  case CANONUSER:
105256678Seric 					default_rvp = rvp;
105356678Seric 					break;
105456678Seric 				}
105553654Seric 			}
105616914Seric 			if (*rvp != NULL)
105716914Seric 				*rvp++ = NULL;
105856823Seric 			if (xpvp != NULL)
105956823Seric 			{
106058814Seric 				cataddr(xpvp, NULL, replac,
106158082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
106258082Seric 					'\0');
106356823Seric 				*++arg_rvp = replac;
106456823Seric 			}
106556823Seric 			*++arg_rvp = NULL;
106616914Seric 
106716914Seric 			/* save the remainder of the input string */
106816914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
106916914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
107016914Seric 
107156678Seric 			/* look it up */
107258814Seric 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
107356823Seric 			argvect[0] = buf;
107460538Seric 			if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags))
107556836Seric 			{
107659084Seric 				auto int stat = EX_OK;
107756836Seric 
107860215Seric 				/* XXX should try to auto-open the map here */
107960215Seric 
108058796Seric 				if (tTd(60, 1))
108158796Seric 					printf("map_lookup(%s, %s) => ",
108258796Seric 						mapname, buf);
108356836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
108460089Seric 						buf, argvect, &stat);
108558796Seric 				if (tTd(60, 1))
108659084Seric 					printf("%s (%d)\n",
108759084Seric 						replac ? replac : "NOT FOUND",
108859084Seric 						stat);
108959084Seric 
109059084Seric 				/* should recover if stat == EX_TEMPFAIL */
109159084Seric 				if (stat == EX_TEMPFAIL)
109259084Seric 					rstat = stat;
109356836Seric 			}
109453654Seric 			else
109556678Seric 				replac = NULL;
109656678Seric 
109756678Seric 			/* if no replacement, use default */
109856823Seric 			if (replac == NULL && default_rvp != NULL)
109956823Seric 			{
110060089Seric 				/* create the default */
110160089Seric 				cataddr(default_rvp, NULL, buf, sizeof buf, '\0');
110256823Seric 				replac = buf;
110356823Seric 			}
110456823Seric 
110556678Seric 			if (replac == NULL)
110651317Seric 			{
110756823Seric 				xpvp = key_rvp;
110853654Seric 			}
110964404Seric 			else if (*replac == '\0')
111064404Seric 			{
111164404Seric 				/* null replacement */
111264404Seric 				nullpvp[0] = NULL;
111364404Seric 				xpvp = nullpvp;
111464404Seric 			}
111556678Seric 			else
111653654Seric 			{
111756678Seric 				/* scan the new replacement */
111865066Seric 				xpvp = prescan(replac, '\0', pvpbuf,
111965066Seric 					       sizeof pvpbuf, NULL);
112053654Seric 				if (xpvp == NULL)
112151317Seric 				{
112258403Seric 					/* prescan already printed error */
112359084Seric 					return EX_DATAERR;
112456678Seric 				}
112551317Seric 			}
112651317Seric 
112716914Seric 			/* append it to the token list */
112856678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
112956678Seric 			{
113017174Seric 				*avp++ = newstr(*xpvp);
113116920Seric 				if (avp >= &npvp[MAXATOM])
113216914Seric 					goto toolong;
113317174Seric 			}
113416914Seric 
113516914Seric 			/* restore the old trailing information */
113656678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
113716920Seric 				if (avp >= &npvp[MAXATOM])
113816914Seric 					goto toolong;
113917174Seric 
114056678Seric 			break;
114116914Seric 		}
114216914Seric 
114316914Seric 		/*
114416914Seric 		**  Check for subroutine calls.
114516914Seric 		*/
114616914Seric 
114758050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
114856678Seric 		{
114959084Seric 			int stat;
115059084Seric 
115165379Seric 			if (npvp[1] == NULL)
115265379Seric 			{
115365379Seric 				syserr("parseaddr: NULL subroutine call in ruleset %d, rule %d",
115465379Seric 					ruleset, ruleno);
115565379Seric 				*pvp = NULL;
115665379Seric 			}
115765379Seric 			else
115865379Seric 			{
115965379Seric 				bcopy((char *) &npvp[2], (char *) pvp,
116065379Seric 					(int) (avp - npvp - 2) * sizeof *avp);
116165379Seric 				if (tTd(21, 3))
116265379Seric 					printf("-----callsubr %s\n", npvp[1]);
116365379Seric 				stat = rewrite(pvp, atoi(npvp[1]), reclevel, e);
116465379Seric 				if (rstat == EX_OK || stat == EX_TEMPFAIL)
116565379Seric 					rstat = stat;
116665379Seric 				if (*pvp != NULL && (**pvp & 0377) == CANONNET)
116763581Seric 				rwr = NULL;
116865379Seric 			}
116956678Seric 		}
117056678Seric 		else
117156678Seric 		{
117217348Seric 			bcopy((char *) npvp, (char *) pvp,
117316900Seric 				(int) (avp - npvp) * sizeof *avp);
117456678Seric 		}
11759374Seric 		if (tTd(21, 4))
11769374Seric 		{
11779374Seric 			printf("rewritten as:");
117856678Seric 			printav(pvp);
11799374Seric 		}
1180297Seric 	}
11818069Seric 
118267262Seric 	if (OpMode == MD_TEST || tTd(21, 1))
11838069Seric 	{
11848959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
118556678Seric 		printav(pvp);
11868069Seric 	}
118759084Seric 
118859084Seric 	return rstat;
11893149Seric }
11903149Seric /*
11913149Seric **  BUILDADDR -- build address from token vector.
11923149Seric **
11933149Seric **	Parameters:
11943149Seric **		tv -- token vector.
11953149Seric **		a -- pointer to address descriptor to fill.
11963149Seric **			If NULL, one will be allocated.
119764284Seric **		flags -- info regarding whether this is a sender or
119864284Seric **			a recipient.
119958966Seric **		e -- the current envelope.
12003149Seric **
12013149Seric **	Returns:
12024279Seric **		NULL if there was an error.
12034279Seric **		'a' otherwise.
12043149Seric **
12053149Seric **	Side Effects:
12063149Seric **		fills in 'a'
12073149Seric */
12083149Seric 
120957249Seric struct errcodes
121057249Seric {
121157249Seric 	char	*ec_name;		/* name of error code */
121257249Seric 	int	ec_code;		/* numeric code */
121357249Seric } ErrorCodes[] =
121457249Seric {
121557249Seric 	"usage",	EX_USAGE,
121657249Seric 	"nouser",	EX_NOUSER,
121757249Seric 	"nohost",	EX_NOHOST,
121857249Seric 	"unavailable",	EX_UNAVAILABLE,
121957249Seric 	"software",	EX_SOFTWARE,
122057249Seric 	"tempfail",	EX_TEMPFAIL,
122157249Seric 	"protocol",	EX_PROTOCOL,
122257249Seric #ifdef EX_CONFIG
122357249Seric 	"config",	EX_CONFIG,
122457249Seric #endif
122557249Seric 	NULL,		EX_UNAVAILABLE,
122657249Seric };
122757249Seric 
122856678Seric ADDRESS *
122964284Seric buildaddr(tv, a, flags, e)
12303149Seric 	register char **tv;
12313149Seric 	register ADDRESS *a;
123264284Seric 	int flags;
123358966Seric 	register ENVELOPE *e;
12343149Seric {
12353149Seric 	struct mailer **mp;
12363149Seric 	register struct mailer *m;
123758008Seric 	char *bp;
123858008Seric 	int spaceleft;
123964306Seric 	static MAILER errormailer;
124064306Seric 	static char *errorargv[] = { "ERROR", NULL };
124157402Seric 	static char buf[MAXNAME];
12423149Seric 
124364791Seric 	if (tTd(24, 5))
124464791Seric 	{
124567693Seric 		printf("buildaddr, flags=%x, tv=", flags);
124664791Seric 		printav(tv);
124764791Seric 	}
124864791Seric 
12493149Seric 	if (a == NULL)
12503149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
125116889Seric 	bzero((char *) a, sizeof *a);
12523149Seric 
12533149Seric 	/* figure out what net/mailer to use */
125464306Seric 	if (*tv == NULL || (**tv & 0377) != CANONNET)
12554279Seric 	{
125658151Seric 		syserr("554 buildaddr: no net");
125764306Seric badaddr:
125864306Seric 		a->q_flags |= QBADADDR;
125964306Seric 		a->q_mailer = &errormailer;
126064306Seric 		if (errormailer.m_name == NULL)
126164306Seric 		{
126264306Seric 			/* initialize the bogus mailer */
126364306Seric 			errormailer.m_name = "*error*";
126464306Seric 			errormailer.m_mailer = "ERROR";
126564306Seric 			errormailer.m_argv = errorargv;
126664306Seric 		}
126764306Seric 		return a;
12684279Seric 	}
12693149Seric 	tv++;
127058680Seric 	if (strcasecmp(*tv, "error") == 0)
12714279Seric 	{
127258050Seric 		if ((**++tv & 0377) == CANONHOST)
127310183Seric 		{
127457249Seric 			register struct errcodes *ep;
127557249Seric 
127658050Seric 			if (isascii(**++tv) && isdigit(**tv))
127757249Seric 			{
127857249Seric 				setstat(atoi(*tv));
127957249Seric 			}
128057249Seric 			else
128157249Seric 			{
128257249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
128357249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
128457249Seric 						break;
128557249Seric 				setstat(ep->ec_code);
128657249Seric 			}
128710183Seric 			tv++;
128810183Seric 		}
128964928Seric 		else
129064928Seric 			setstat(EX_UNAVAILABLE);
129158050Seric 		if ((**tv & 0377) != CANONUSER)
129258151Seric 			syserr("554 buildaddr: error: no user");
129358814Seric 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
129458082Seric 		stripquotes(buf);
129564659Seric 		if (isascii(buf[0]) && isdigit(buf[0]) &&
129664659Seric 		    isascii(buf[1]) && isdigit(buf[1]) &&
129764659Seric 		    isascii(buf[2]) && isdigit(buf[2]) &&
129864659Seric 		    buf[3] == ' ')
129964659Seric 		{
130064659Seric 			char fmt[10];
130164659Seric 
130264659Seric 			strncpy(fmt, buf, 3);
130364659Seric 			strcpy(&fmt[3], " %s");
130464659Seric 			usrerr(fmt, buf + 4);
130564659Seric 		}
130664659Seric 		else
130764659Seric 		{
130866039Seric 			usrerr("553 %s", buf);
130964659Seric 		}
131064306Seric 		goto badaddr;
13114279Seric 	}
131257402Seric 
13134598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
13143149Seric 	{
131558680Seric 		if (strcasecmp(m->m_name, *tv) == 0)
13163149Seric 			break;
13173149Seric 	}
13183149Seric 	if (m == NULL)
13194279Seric 	{
132058151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
132164306Seric 		goto badaddr;
13224279Seric 	}
13234598Seric 	a->q_mailer = m;
13243149Seric 
13253149Seric 	/* figure out what host (if any) */
132656678Seric 	tv++;
132758509Seric 	if ((**tv & 0377) == CANONHOST)
13283149Seric 	{
132958008Seric 		bp = buf;
133058008Seric 		spaceleft = sizeof buf - 1;
133158050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
133258008Seric 		{
133358008Seric 			int i = strlen(*tv);
133458008Seric 
133558008Seric 			if (i > spaceleft)
133658008Seric 			{
133758008Seric 				/* out of space for this address */
133858008Seric 				if (spaceleft >= 0)
133958151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
134058008Seric 						buf);
134158008Seric 				i = spaceleft;
134258008Seric 				spaceleft = 0;
134358008Seric 			}
134458008Seric 			if (i <= 0)
134558008Seric 				continue;
134658008Seric 			bcopy(*tv, bp, i);
134758008Seric 			bp += i;
134858008Seric 			spaceleft -= i;
134958008Seric 		}
135058010Seric 		*bp = '\0';
13515704Seric 		a->q_host = newstr(buf);
13523149Seric 	}
135357249Seric 	else
135458509Seric 	{
135558509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
135658509Seric 		{
135758509Seric 			syserr("554 buildaddr: no host");
135864306Seric 			goto badaddr;
135958509Seric 		}
136057249Seric 		a->q_host = NULL;
136158509Seric 	}
13623149Seric 
13633149Seric 	/* figure out the user */
136458050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
13654279Seric 	{
136658151Seric 		syserr("554 buildaddr: no user");
136764306Seric 		goto badaddr;
13684279Seric 	}
136957402Seric 	tv++;
137051317Seric 
137157402Seric 	/* do special mapping for local mailer */
137267472Seric 	if (*tv != NULL)
137357402Seric 	{
137457454Seric 		register char *p = *tv;
137557454Seric 
137657454Seric 		if (*p == '"')
137757454Seric 			p++;
137867472Seric 		if (*p == '|' && bitnset(M_CHECKPROG, m->m_flags))
137957402Seric 			a->q_mailer = m = ProgMailer;
138067472Seric 		else if (*p == '/' && bitnset(M_CHECKFILE, m->m_flags))
138157402Seric 			a->q_mailer = m = FileMailer;
138267472Seric 		else if (*p == ':' && bitnset(M_CHECKINCLUDE, m->m_flags))
138357402Seric 		{
138457402Seric 			/* may be :include: */
138558814Seric 			cataddr(tv, NULL, buf, sizeof buf, '\0');
138658008Seric 			stripquotes(buf);
138758008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
138858008Seric 			{
138958008Seric 				/* if :include:, don't need further rewriting */
139057402Seric 				a->q_mailer = m = InclMailer;
139158008Seric 				a->q_user = &buf[9];
139258008Seric 				return (a);
139358008Seric 			}
139457402Seric 		}
139557402Seric 	}
139657402Seric 
139767472Seric 	if (bitnset(M_CHECKUDB, m->m_flags) && *tv != NULL &&
139867472Seric 	    strcmp(*tv, "@") == 0)
139958008Seric 	{
140058008Seric 		tv++;
140158008Seric 		a->q_flags |= QNOTREMOTE;
140258008Seric 	}
140358008Seric 
140464284Seric 	/* rewrite according recipient mailer rewriting rules */
140564284Seric 	define('h', a->q_host, e);
140664284Seric 	if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags))
140764284Seric 	{
140864284Seric 		/* sender addresses done later */
140965071Seric 		(void) rewrite(tv, 2, 0, e);
141064284Seric 		if (m->m_re_rwset > 0)
141165071Seric 		       (void) rewrite(tv, m->m_re_rwset, 0, e);
141264284Seric 	}
141365071Seric 	(void) rewrite(tv, 4, 0, e);
141419040Seric 
141519040Seric 	/* save the result for the command line/RCPT argument */
141658814Seric 	cataddr(tv, NULL, buf, sizeof buf, '\0');
14173149Seric 	a->q_user = buf;
14183149Seric 
141958670Seric 	/*
142058670Seric 	**  Do mapping to lower case as requested by mailer
142158670Seric 	*/
142258670Seric 
142358670Seric 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
142458670Seric 		makelower(a->q_host);
142558670Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
142658670Seric 		makelower(a->q_user);
142758670Seric 
14283149Seric 	return (a);
14293149Seric }
14303188Seric /*
14314228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
14324228Seric **
14334228Seric **	Parameters:
14344228Seric **		pvp -- parameter vector to rebuild.
143558814Seric **		evp -- last parameter to include.  Can be NULL to
143658814Seric **			use entire pvp.
14374228Seric **		buf -- buffer to build the string into.
14384228Seric **		sz -- size of buf.
143958082Seric **		spacesub -- the space separator character; if null,
144058082Seric **			use SpaceSub.
14414228Seric **
14424228Seric **	Returns:
14434228Seric **		none.
14444228Seric **
14454228Seric **	Side Effects:
14464228Seric **		Destroys buf.
14474228Seric */
14484228Seric 
144958814Seric cataddr(pvp, evp, buf, sz, spacesub)
14504228Seric 	char **pvp;
145158814Seric 	char **evp;
14524228Seric 	char *buf;
14534228Seric 	register int sz;
145458082Seric 	char spacesub;
14554228Seric {
14564228Seric 	bool oatomtok = FALSE;
145756678Seric 	bool natomtok = FALSE;
14584228Seric 	register int i;
14594228Seric 	register char *p;
14604228Seric 
146158082Seric 	if (spacesub == '\0')
146258082Seric 		spacesub = SpaceSub;
146358082Seric 
14648423Seric 	if (pvp == NULL)
14658423Seric 	{
146623109Seric 		(void) strcpy(buf, "");
14678423Seric 		return;
14688423Seric 	}
14694228Seric 	p = buf;
147011156Seric 	sz -= 2;
14714228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
14724228Seric 	{
14738078Seric 		natomtok = (toktype(**pvp) == ATM);
14744228Seric 		if (oatomtok && natomtok)
147558082Seric 			*p++ = spacesub;
14764228Seric 		(void) strcpy(p, *pvp);
14774228Seric 		oatomtok = natomtok;
14784228Seric 		p += i;
147911156Seric 		sz -= i + 1;
148058814Seric 		if (pvp++ == evp)
148158814Seric 			break;
14824228Seric 	}
14834228Seric 	*p = '\0';
14844228Seric }
14854228Seric /*
14863188Seric **  SAMEADDR -- Determine if two addresses are the same
14873188Seric **
14883188Seric **	This is not just a straight comparison -- if the mailer doesn't
14893188Seric **	care about the host we just ignore it, etc.
14903188Seric **
14913188Seric **	Parameters:
14923188Seric **		a, b -- pointers to the internal forms to compare.
14933188Seric **
14943188Seric **	Returns:
14953188Seric **		TRUE -- they represent the same mailbox.
14963188Seric **		FALSE -- they don't.
14973188Seric **
14983188Seric **	Side Effects:
14993188Seric **		none.
15003188Seric */
15013188Seric 
15023188Seric bool
15039374Seric sameaddr(a, b)
15043188Seric 	register ADDRESS *a;
15053188Seric 	register ADDRESS *b;
15063188Seric {
150765093Seric 	register ADDRESS *ca, *cb;
150865093Seric 
15093188Seric 	/* if they don't have the same mailer, forget it */
15103188Seric 	if (a->q_mailer != b->q_mailer)
15113188Seric 		return (FALSE);
15123188Seric 
15133188Seric 	/* if the user isn't the same, we can drop out */
151456678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
15153188Seric 		return (FALSE);
15163188Seric 
151765093Seric 	/* if we have good uids for both but they differ, these are different */
151865379Seric 	if (a->q_mailer == ProgMailer)
151965379Seric 	{
152065379Seric 		ca = getctladdr(a);
152165379Seric 		cb = getctladdr(b);
152265379Seric 		if (ca != NULL && cb != NULL &&
152365379Seric 		    bitset(QGOODUID, ca->q_flags & cb->q_flags) &&
152465379Seric 		    ca->q_uid != cb->q_uid)
152565379Seric 			return (FALSE);
152665379Seric 	}
152758438Seric 
152858509Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
152958509Seric 	if (a->q_host == b->q_host)
153058509Seric 	{
153158509Seric 		/* probably both null pointers */
15323188Seric 		return (TRUE);
153358509Seric 	}
15343188Seric 	if (a->q_host == NULL || b->q_host == NULL)
153558509Seric 	{
153658509Seric 		/* only one is a null pointer */
15373188Seric 		return (FALSE);
153858509Seric 	}
153956678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
15403188Seric 		return (FALSE);
15413188Seric 
15423188Seric 	return (TRUE);
15433188Seric }
15443234Seric /*
15453234Seric **  PRINTADDR -- print address (for debugging)
15463234Seric **
15473234Seric **	Parameters:
15483234Seric **		a -- the address to print
15493234Seric **		follow -- follow the q_next chain.
15503234Seric **
15513234Seric **	Returns:
15523234Seric **		none.
15533234Seric **
15543234Seric **	Side Effects:
15553234Seric **		none.
15563234Seric */
15573234Seric 
15583234Seric printaddr(a, follow)
15593234Seric 	register ADDRESS *a;
15603234Seric 	bool follow;
15613234Seric {
15625001Seric 	bool first = TRUE;
156357731Seric 	register MAILER *m;
156457731Seric 	MAILER pseudomailer;
15655001Seric 
15663234Seric 	while (a != NULL)
15673234Seric 	{
15685001Seric 		first = FALSE;
15694443Seric 		printf("%x=", a);
15704085Seric 		(void) fflush(stdout);
157157731Seric 
157257731Seric 		/* find the mailer -- carefully */
157357731Seric 		m = a->q_mailer;
157457731Seric 		if (m == NULL)
157557731Seric 		{
157657731Seric 			m = &pseudomailer;
157757731Seric 			m->m_mno = -1;
157857731Seric 			m->m_name = "NULL";
157957731Seric 		}
158057731Seric 
158158680Seric 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
158257731Seric 		       a->q_paddr, m->m_mno, m->m_name,
158367172Seric 		       a->q_host == NULL ? "<null>" : a->q_host, a->q_user,
158467172Seric 		       a->q_ruser == NULL ? "<null>" : a->q_ruser);
158567693Seric 		printf("\tnext=%x, flags=%x, alias %x, uid %d, gid %d\n",
158659269Seric 		       a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid);
158759269Seric 		printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n",
158859269Seric 		       a->q_owner == NULL ? "(none)" : a->q_owner,
158963756Seric 		       a->q_home == NULL ? "(none)" : a->q_home,
159063756Seric 		       a->q_fullname == NULL ? "(none)" : a->q_fullname);
15914996Seric 
15923234Seric 		if (!follow)
15933234Seric 			return;
15944996Seric 		a = a->q_next;
15953234Seric 	}
15965001Seric 	if (first)
15974443Seric 		printf("[NULL]\n");
15983234Seric }
15994317Seric 
16007682Seric /*
16017682Seric **  REMOTENAME -- return the name relative to the current mailer
16027682Seric **
16037682Seric **	Parameters:
16047682Seric **		name -- the name to translate.
16058069Seric **		m -- the mailer that we want to do rewriting relative
16068069Seric **			to.
160759163Seric **		flags -- fine tune operations.
160859163Seric **		pstat -- pointer to status word.
160958020Seric **		e -- the current envelope.
16107682Seric **
16117682Seric **	Returns:
16127682Seric **		the text string representing this address relative to
16137682Seric **			the receiving mailer.
16147682Seric **
16157682Seric **	Side Effects:
16167682Seric **		none.
16177682Seric **
16187682Seric **	Warnings:
16197682Seric **		The text string returned is tucked away locally;
16207682Seric **			copy it if you intend to save it.
16217682Seric */
16227682Seric 
16237682Seric char *
162459163Seric remotename(name, m, flags, pstat, e)
16257682Seric 	char *name;
162656678Seric 	struct mailer *m;
162759163Seric 	int flags;
162859163Seric 	int *pstat;
162956678Seric 	register ENVELOPE *e;
16307682Seric {
16318069Seric 	register char **pvp;
16328069Seric 	char *fancy;
163356678Seric 	char *oldg = macvalue('g', e);
163458020Seric 	int rwset;
16357682Seric 	static char buf[MAXNAME];
16367682Seric 	char lbuf[MAXNAME];
163716914Seric 	char pvpbuf[PSBUFSIZE];
163856678Seric 	extern char *crackaddr();
16397682Seric 
16407755Seric 	if (tTd(12, 1))
16417755Seric 		printf("remotename(%s)\n", name);
16427755Seric 
164310177Seric 	/* don't do anything if we are tagging it as special */
164459163Seric 	if (bitset(RF_SENDERADDR, flags))
164559163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
164659163Seric 						     : m->m_se_rwset;
164758020Seric 	else
164859163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
164959163Seric 						     : m->m_re_rwset;
165058020Seric 	if (rwset < 0)
165110177Seric 		return (name);
165210177Seric 
16537682Seric 	/*
16548181Seric 	**  Do a heuristic crack of this name to extract any comment info.
16558181Seric 	**	This will leave the name as a comment and a $g macro.
16567889Seric 	*/
16577889Seric 
165859163Seric 	if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags))
165958050Seric 		fancy = "\201g";
166010310Seric 	else
166110310Seric 		fancy = crackaddr(name);
16627889Seric 
16638181Seric 	/*
16648181Seric 	**  Turn the name into canonical form.
16658181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
16668181Seric 	**	If this only resolves to "user", and the "C" flag is
16678181Seric 	**	specified in the sending mailer, then the sender's
16688181Seric 	**	domain will be appended.
16698181Seric 	*/
16708181Seric 
167165066Seric 	pvp = prescan(name, '\0', pvpbuf, sizeof pvpbuf, NULL);
16727889Seric 	if (pvp == NULL)
16737889Seric 		return (name);
167465071Seric 	if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL)
167559163Seric 		*pstat = EX_TEMPFAIL;
167659163Seric 	if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
16778181Seric 	{
16788181Seric 		/* append from domain to this address */
16798181Seric 		register char **pxp = pvp;
16808181Seric 
16819594Seric 		/* see if there is an "@domain" in the current name */
16828181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
16838181Seric 			pxp++;
16848181Seric 		if (*pxp == NULL)
16858181Seric 		{
16869594Seric 			/* no.... append the "@domain" from the sender */
168756678Seric 			register char **qxq = e->e_fromdomain;
16888181Seric 
16899594Seric 			while ((*pxp++ = *qxq++) != NULL)
16909594Seric 				continue;
169165071Seric 			if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL)
169259163Seric 				*pstat = EX_TEMPFAIL;
16938181Seric 		}
16948181Seric 	}
16958181Seric 
16968181Seric 	/*
16978959Seric 	**  Do more specific rewriting.
169856678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
169956678Seric 	**		a sender address or not.
17008181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
17018181Seric 	*/
17028181Seric 
170359163Seric 	if (bitset(RF_SENDERADDR, flags))
170459541Seric 	{
170565071Seric 		if (rewrite(pvp, 1, 0, e) == EX_TEMPFAIL)
170659163Seric 			*pstat = EX_TEMPFAIL;
170759541Seric 	}
17088069Seric 	else
170959541Seric 	{
171065071Seric 		if (rewrite(pvp, 2, 0, e) == EX_TEMPFAIL)
171159163Seric 			*pstat = EX_TEMPFAIL;
171259541Seric 	}
171358020Seric 	if (rwset > 0)
171459541Seric 	{
171565071Seric 		if (rewrite(pvp, rwset, 0, e) == EX_TEMPFAIL)
171659163Seric 			*pstat = EX_TEMPFAIL;
171759541Seric 	}
17187682Seric 
17198181Seric 	/*
17208959Seric 	**  Do any final sanitation the address may require.
17218959Seric 	**	This will normally be used to turn internal forms
17228959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
17238959Seric 	**	may be used as a default to the above rules.
17248959Seric 	*/
17258959Seric 
172665071Seric 	if (rewrite(pvp, 4, 0, e) == EX_TEMPFAIL)
172759163Seric 		*pstat = EX_TEMPFAIL;
17288959Seric 
17298959Seric 	/*
17308181Seric 	**  Now restore the comment information we had at the beginning.
17318181Seric 	*/
17328181Seric 
173358825Seric 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
173456678Seric 	define('g', lbuf, e);
173565494Seric 
173665494Seric 	/* need to make sure route-addrs have <angle brackets> */
173765494Seric 	if (bitset(RF_CANONICAL, flags) && lbuf[0] == '@')
173865494Seric 		expand("<\201g>", buf, &buf[sizeof buf - 1], e);
173965494Seric 	else
174065494Seric 		expand(fancy, buf, &buf[sizeof buf - 1], e);
174165494Seric 
174256678Seric 	define('g', oldg, e);
17437682Seric 
17447682Seric 	if (tTd(12, 1))
17457755Seric 		printf("remotename => `%s'\n", buf);
17467682Seric 	return (buf);
17477682Seric }
174851317Seric /*
174956678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
175051317Seric **
175151317Seric **	Parameters:
175256678Seric **		a -- the address to map (but just the user name part).
175356678Seric **		sendq -- the sendq in which to install any replacement
175456678Seric **			addresses.
175551317Seric **
175651317Seric **	Returns:
175751317Seric **		none.
175851317Seric */
175951317Seric 
176056678Seric maplocaluser(a, sendq, e)
176156678Seric 	register ADDRESS *a;
176256678Seric 	ADDRESS **sendq;
176356678Seric 	ENVELOPE *e;
176451317Seric {
176556678Seric 	register char **pvp;
176656678Seric 	register ADDRESS *a1 = NULL;
176758333Seric 	auto char *delimptr;
176856678Seric 	char pvpbuf[PSBUFSIZE];
176951317Seric 
177056678Seric 	if (tTd(29, 1))
177156678Seric 	{
177256678Seric 		printf("maplocaluser: ");
177356678Seric 		printaddr(a, FALSE);
177456678Seric 	}
177565066Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, sizeof pvpbuf, &delimptr);
177656678Seric 	if (pvp == NULL)
177756678Seric 		return;
177851317Seric 
177965071Seric 	(void) rewrite(pvp, 5, 0, e);
178058050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
178156678Seric 		return;
178251317Seric 
178356678Seric 	/* if non-null, mailer destination specified -- has it changed? */
178464284Seric 	a1 = buildaddr(pvp, NULL, 0, e);
178556678Seric 	if (a1 == NULL || sameaddr(a, a1))
178656678Seric 		return;
178751317Seric 
178856678Seric 	/* mark old address as dead; insert new address */
178956678Seric 	a->q_flags |= QDONTSEND;
179057731Seric 	if (tTd(29, 5))
179157731Seric 	{
179257731Seric 		printf("maplocaluser: QDONTSEND ");
179357731Seric 		printaddr(a, FALSE);
179457731Seric 	}
179556678Seric 	a1->q_alias = a;
179664348Seric 	allocaddr(a1, RF_COPYALL, NULL);
179756678Seric 	(void) recipient(a1, sendq, e);
179851317Seric }
179958802Seric /*
180058802Seric **  DEQUOTE_INIT -- initialize dequote map
180158802Seric **
180258802Seric **	This is a no-op.
180358802Seric **
180458802Seric **	Parameters:
180558802Seric **		map -- the internal map structure.
180658802Seric **		args -- arguments.
180758802Seric **
180858802Seric **	Returns:
180958802Seric **		TRUE.
181058802Seric */
181158802Seric 
181258802Seric bool
181360219Seric dequote_init(map, args)
181458802Seric 	MAP *map;
181558802Seric 	char *args;
181658802Seric {
181758805Seric 	register char *p = args;
181858805Seric 
181958805Seric 	for (;;)
182058805Seric 	{
182158805Seric 		while (isascii(*p) && isspace(*p))
182258805Seric 			p++;
182358805Seric 		if (*p != '-')
182458805Seric 			break;
182558805Seric 		switch (*++p)
182658805Seric 		{
182758805Seric 		  case 'a':
182858805Seric 			map->map_app = ++p;
182958805Seric 			break;
183058805Seric 		}
183158805Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
183258805Seric 			p++;
183358805Seric 		if (*p != '\0')
183458805Seric 			*p = '\0';
183558805Seric 	}
183658805Seric 	if (map->map_app != NULL)
183758805Seric 		map->map_app = newstr(map->map_app);
183858805Seric 
183958802Seric 	return TRUE;
184058802Seric }
184158802Seric /*
184258802Seric **  DEQUOTE_MAP -- unquote an address
184358802Seric **
184458802Seric **	Parameters:
184558802Seric **		map -- the internal map structure (ignored).
184660089Seric **		name -- the name to dequote.
184758802Seric **		av -- arguments (ignored).
184859084Seric **		statp -- pointer to status out-parameter.
184958802Seric **
185058802Seric **	Returns:
185158802Seric **		NULL -- if there were no quotes, or if the resulting
185258802Seric **			unquoted buffer would not be acceptable to prescan.
185358802Seric **		else -- The dequoted buffer.
185458802Seric */
185558802Seric 
185658802Seric char *
185760089Seric dequote_map(map, name, av, statp)
185858802Seric 	MAP *map;
185960089Seric 	char *name;
186058802Seric 	char **av;
186159084Seric 	int *statp;
186258802Seric {
186358802Seric 	register char *p;
186458802Seric 	register char *q;
186558802Seric 	register char c;
186658802Seric 	int anglecnt;
186758802Seric 	int cmntcnt;
186858802Seric 	int quotecnt;
186959089Seric 	int spacecnt;
187058802Seric 	bool quotemode;
187158802Seric 	bool bslashmode;
187258802Seric 
187358802Seric 	anglecnt = 0;
187458802Seric 	cmntcnt = 0;
187558802Seric 	quotecnt = 0;
187659089Seric 	spacecnt = 0;
187758802Seric 	quotemode = FALSE;
187858802Seric 	bslashmode = FALSE;
187958802Seric 
188060089Seric 	for (p = q = name; (c = *p++) != '\0'; )
188158802Seric 	{
188258802Seric 		if (bslashmode)
188358802Seric 		{
188458802Seric 			bslashmode = FALSE;
188558802Seric 			*q++ = c;
188658802Seric 			continue;
188758802Seric 		}
188858802Seric 
1889*67764Seric 		if (c == ' ' && SpaceSub != '\0')
1890*67764Seric 			c = SpaceSub;
1891*67764Seric 
189258802Seric 		switch (c)
189358802Seric 		{
189458802Seric 		  case '\\':
189558802Seric 			bslashmode = TRUE;
189658802Seric 			break;
189758802Seric 
189858802Seric 		  case '(':
189958802Seric 			cmntcnt++;
190058802Seric 			break;
190158802Seric 
190258802Seric 		  case ')':
190358802Seric 			if (cmntcnt-- <= 0)
190458802Seric 				return NULL;
190558802Seric 			break;
190659089Seric 
190759089Seric 		  case ' ':
190859089Seric 			spacecnt++;
190959089Seric 			break;
191058802Seric 		}
191158802Seric 
191258802Seric 		if (cmntcnt > 0)
191358802Seric 		{
191458802Seric 			*q++ = c;
191558802Seric 			continue;
191658802Seric 		}
191758802Seric 
191858802Seric 		switch (c)
191958802Seric 		{
192058802Seric 		  case '"':
192158802Seric 			quotemode = !quotemode;
192258802Seric 			quotecnt++;
192358802Seric 			continue;
192458802Seric 
192558802Seric 		  case '<':
192658802Seric 			anglecnt++;
192758802Seric 			break;
192858802Seric 
192958802Seric 		  case '>':
193058802Seric 			if (anglecnt-- <= 0)
193158802Seric 				return NULL;
193258802Seric 			break;
193358802Seric 		}
193458802Seric 		*q++ = c;
193558802Seric 	}
193658802Seric 
193758802Seric 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
193859089Seric 	    quotemode || quotecnt <= 0 || spacecnt != 0)
193958802Seric 		return NULL;
194058802Seric 	*q++ = '\0';
194160089Seric 	return name;
194258802Seric }
1943