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*65092Seric static char sccsid[] = "@(#)parseaddr.c	8.24 (Berkeley) 12/11/93";
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';
10964348Seric 		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))
24364284Seric 		printf("allocaddr(flags=%o, 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 
319*65092Seric /* token type table -- it gets modified with $o characters */
320*65092Seric static TokTypeTab[256] =
321*65092Seric {
322*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
323*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
324*65092Seric 	SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM,ATM,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
325*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
326*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
327*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
328*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
329*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
330*65092Seric 	OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
331*65092Seric 	OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
332*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
333*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
334*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
335*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
336*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
337*65092Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
338*65092Seric };
339*65092Seric 
340*65092Seric #define toktype(c)	((int) TokTypeTab[(c) & 0xff])
341*65092Seric 
342*65092Seric 
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];
364*65092Seric 	static char firsttime = TRUE;
36556678Seric 	extern int errno;
366297Seric 
367*65092Seric 	if (firsttime)
368*65092Seric 	{
369*65092Seric 		/* initialize the token type table */
370*65092Seric 		char obuf[50];
371*65092Seric 
372*65092Seric 		firsttime = FALSE;
373*65092Seric 		expand("\201o", obuf, &obuf[sizeof obuf - sizeof DELIMCHARS], CurEnv);
374*65092Seric 		strcat(obuf, DELIMCHARS);
375*65092Seric 		for (p = obuf; *p != '\0'; p++)
376*65092Seric 		{
377*65092Seric 			if (TokTypeTab[*p & 0xff] == ATM)
378*65092Seric 				TokTypeTab[*p & 0xff] = OPR;
379*65092Seric 		}
380*65092Seric 	}
381*65092Seric 
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 
6193149Seric 
62059084Seric int
62165071Seric rewrite(pvp, ruleset, reclevel, e)
6223149Seric 	char **pvp;
6234070Seric 	int ruleset;
62465071Seric 	int reclevel;
62559027Seric 	register ENVELOPE *e;
6263149Seric {
6273149Seric 	register char *ap;		/* address pointer */
6283149Seric 	register char *rp;		/* rewrite pointer */
6293149Seric 	register char **avp;		/* address vector pointer */
6303149Seric 	register char **rvp;		/* rewrite vector pointer */
6318058Seric 	register struct match *mlp;	/* cur ptr into mlist */
6328058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
63358866Seric 	int ruleno;			/* current rule number */
63459084Seric 	int rstat = EX_OK;		/* return status */
63564740Seric 	int loopcount;
63656678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
6373149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
6383149Seric 
6399279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
6403149Seric 	{
6418959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
64256678Seric 		printav(pvp);
6433149Seric 	}
64456678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
64556326Seric 	{
64658151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
64759084Seric 		return EX_CONFIG;
64856326Seric 	}
64965071Seric 	if (reclevel++ > 50)
65065071Seric 	{
65165071Seric 		syserr("rewrite: infinite recursion, ruleset %d", ruleset);
65265071Seric 		return EX_CONFIG;
65365071Seric 	}
65456678Seric 	if (pvp == NULL)
65559084Seric 		return EX_USAGE;
65656326Seric 
6573149Seric 	/*
65856678Seric 	**  Run through the list of rewrite rules, applying
65956678Seric 	**	any that match.
6603149Seric 	*/
6613149Seric 
66258866Seric 	ruleno = 1;
66364740Seric 	loopcount = 0;
6644070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6653149Seric 	{
6667675Seric 		if (tTd(21, 12))
667297Seric 		{
6688069Seric 			printf("-----trying rule:");
66956678Seric 			printav(rwr->r_lhs);
6703149Seric 		}
6713149Seric 
6723149Seric 		/* try to match on this rule */
6734468Seric 		mlp = mlist;
6748058Seric 		rvp = rwr->r_lhs;
6758058Seric 		avp = pvp;
67658866Seric 		if (++loopcount > 100)
6773149Seric 		{
67858866Seric 			syserr("554 Infinite loop in ruleset %d, rule %d",
67958866Seric 				ruleset, ruleno);
68058866Seric 			if (tTd(21, 1))
68152637Seric 			{
68256678Seric 				printf("workspace: ");
68356678Seric 				printav(pvp);
68452637Seric 			}
68558866Seric 			break;
68658866Seric 		}
68758866Seric 
68858866Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
68958866Seric 		{
6903149Seric 			rp = *rvp;
6918058Seric 			if (tTd(21, 35))
6928058Seric 			{
69358825Seric 				printf("ADVANCE rp=");
69457531Seric 				xputs(rp);
69557532Seric 				printf(", ap=");
6968058Seric 				xputs(ap);
6978069Seric 				printf("\n");
6988058Seric 			}
69956678Seric 			if (rp == NULL)
70056326Seric 			{
7013149Seric 				/* end-of-pattern before end-of-address */
7028058Seric 				goto backup;
70356678Seric 			}
70458173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
70558827Seric 			    (*rp & 0377) != MATCHZERO)
70656326Seric 			{
70758825Seric 				/* end-of-input with patterns left */
70858825Seric 				goto backup;
709297Seric 			}
71056326Seric 
71158050Seric 			switch (*rp & 0377)
7128058Seric 			{
71356678Seric 				register STAB *s;
71458814Seric 				char buf[MAXLINE];
71556326Seric 
71656678Seric 			  case MATCHCLASS:
71758825Seric 				/* match any phrase in a class */
71858825Seric 				mlp->pattern = rvp;
71958814Seric 				mlp->first = avp;
72058814Seric 	extendclass:
72158825Seric 				ap = *avp;
72258825Seric 				if (ap == NULL)
72358814Seric 					goto backup;
72458814Seric 				mlp->last = avp++;
72558814Seric 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
72658814Seric 				s = stab(buf, ST_CLASS, ST_FIND);
72756678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
72856326Seric 				{
72958825Seric 					if (tTd(21, 36))
73058825Seric 					{
73158825Seric 						printf("EXTEND  rp=");
73258825Seric 						xputs(rp);
73358825Seric 						printf(", ap=");
73458825Seric 						xputs(ap);
73558825Seric 						printf("\n");
73658825Seric 					}
73758825Seric 					goto extendclass;
73856326Seric 				}
73958825Seric 				if (tTd(21, 36))
74058825Seric 					printf("CLMATCH\n");
74158814Seric 				mlp++;
74258814Seric 				break;
7434060Seric 
74458825Seric 			  case MATCHNCLASS:
74558825Seric 				/* match any token not in a class */
74658825Seric 				s = stab(ap, ST_CLASS, ST_FIND);
74758825Seric 				if (s != NULL && bitnset(rp[1], s->s_class))
74858825Seric 					goto backup;
74958825Seric 
75058825Seric 				/* fall through */
75158825Seric 
75256678Seric 			  case MATCHONE:
75356678Seric 			  case MATCHANY:
75456678Seric 				/* match exactly one token */
75558825Seric 				mlp->pattern = rvp;
75656678Seric 				mlp->first = avp;
75756678Seric 				mlp->last = avp++;
7588058Seric 				mlp++;
75956678Seric 				break;
7608058Seric 
76156678Seric 			  case MATCHZANY:
76256678Seric 				/* match zero or more tokens */
76358825Seric 				mlp->pattern = rvp;
76456678Seric 				mlp->first = avp;
76556678Seric 				mlp->last = avp - 1;
76656678Seric 				mlp++;
76756678Seric 				break;
76856326Seric 
76958827Seric 			  case MATCHZERO:
77058173Seric 				/* match zero tokens */
77158173Seric 				break;
77258173Seric 
77359027Seric 			  case MACRODEXPAND:
77459027Seric 				/*
77559027Seric 				**  Match against run-time macro.
77659027Seric 				**  This algorithm is broken for the
77759027Seric 				**  general case (no recursive macros,
77859027Seric 				**  improper tokenization) but should
77959027Seric 				**  work for the usual cases.
78059027Seric 				*/
78159027Seric 
78259027Seric 				ap = macvalue(rp[1], e);
78359027Seric 				mlp->first = avp;
78459027Seric 				if (tTd(21, 2))
78559027Seric 					printf("rewrite: LHS $&%c => \"%s\"\n",
78659027Seric 						rp[1],
78759027Seric 						ap == NULL ? "(NULL)" : ap);
78859027Seric 
78959027Seric 				if (ap == NULL)
79059027Seric 					break;
79160502Seric 				while (*ap != '\0')
79259027Seric 				{
79359027Seric 					if (*avp == NULL ||
79459027Seric 					    strncasecmp(ap, *avp, strlen(*avp)) != 0)
79559027Seric 					{
79659027Seric 						/* no match */
79759027Seric 						avp = mlp->first;
79859027Seric 						goto backup;
79959027Seric 					}
80059027Seric 					ap += strlen(*avp++);
80159027Seric 				}
80259027Seric 
80359027Seric 				/* match */
80459027Seric 				break;
80559027Seric 
80656678Seric 			  default:
80756678Seric 				/* must have exact match */
80856678Seric 				if (strcasecmp(rp, ap))
8098058Seric 					goto backup;
8104468Seric 				avp++;
81156678Seric 				break;
8123149Seric 			}
8133149Seric 
81456678Seric 			/* successful match on this token */
8153149Seric 			rvp++;
8163149Seric 			continue;
8173149Seric 
81858825Seric 	  backup:
81956678Seric 			/* match failed -- back up */
82058825Seric 			while (--mlp >= mlist)
8213149Seric 			{
82258825Seric 				rvp = mlp->pattern;
82356678Seric 				rp = *rvp;
82458825Seric 				avp = mlp->last + 1;
82558825Seric 				ap = *avp;
82658825Seric 
82758825Seric 				if (tTd(21, 36))
82858825Seric 				{
82958825Seric 					printf("BACKUP  rp=");
83058825Seric 					xputs(rp);
83158825Seric 					printf(", ap=");
83258825Seric 					xputs(ap);
83358825Seric 					printf("\n");
83458825Seric 				}
83558825Seric 
83658825Seric 				if (ap == NULL)
83758825Seric 				{
83858825Seric 					/* run off the end -- back up again */
83958825Seric 					continue;
84058825Seric 				}
84158050Seric 				if ((*rp & 0377) == MATCHANY ||
84258050Seric 				    (*rp & 0377) == MATCHZANY)
8434468Seric 				{
84456678Seric 					/* extend binding and continue */
84558825Seric 					mlp->last = avp++;
84656678Seric 					rvp++;
84758825Seric 					mlp++;
84856678Seric 					break;
8494468Seric 				}
85058825Seric 				if ((*rp & 0377) == MATCHCLASS)
85156678Seric 				{
85258814Seric 					/* extend binding and try again */
85363397Seric 					mlp->last = avp;
85458814Seric 					goto extendclass;
85558814Seric 				}
8563149Seric 			}
8573149Seric 
85858825Seric 			if (mlp < mlist)
85956678Seric 			{
86056678Seric 				/* total failure to match */
86156326Seric 				break;
8623149Seric 			}
863297Seric 		}
8643149Seric 
8653149Seric 		/*
86656678Seric 		**  See if we successfully matched
8673149Seric 		*/
8683149Seric 
86958827Seric 		if (mlp < mlist || *rvp != NULL)
8703149Seric 		{
8719374Seric 			if (tTd(21, 10))
8729374Seric 				printf("----- rule fails\n");
8739374Seric 			rwr = rwr->r_next;
87458866Seric 			ruleno++;
87564740Seric 			loopcount = 0;
8769374Seric 			continue;
8779374Seric 		}
8783149Seric 
8799374Seric 		rvp = rwr->r_rhs;
8809374Seric 		if (tTd(21, 12))
8819374Seric 		{
8829374Seric 			printf("-----rule matches:");
88356678Seric 			printav(rvp);
8849374Seric 		}
8859374Seric 
8869374Seric 		rp = *rvp;
88758050Seric 		if ((*rp & 0377) == CANONUSER)
8889374Seric 		{
8899374Seric 			rvp++;
8909374Seric 			rwr = rwr->r_next;
89158866Seric 			ruleno++;
89264740Seric 			loopcount = 0;
8939374Seric 		}
89458050Seric 		else if ((*rp & 0377) == CANONHOST)
8959374Seric 		{
8969374Seric 			rvp++;
8979374Seric 			rwr = NULL;
8989374Seric 		}
89958050Seric 		else if ((*rp & 0377) == CANONNET)
9009374Seric 			rwr = NULL;
9019374Seric 
9029374Seric 		/* substitute */
9039374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
9049374Seric 		{
9059374Seric 			register struct match *m;
9069374Seric 			register char **pp;
9079374Seric 
9088058Seric 			rp = *rvp;
90958050Seric 			if ((*rp & 0377) == MATCHREPL)
9108058Seric 			{
91116914Seric 				/* substitute from LHS */
91216914Seric 				m = &mlist[rp[1] - '1'];
91356678Seric 				if (m < mlist || m >= mlp)
9149374Seric 				{
91558151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
91656326Seric 						ruleset, rp[1]);
91759084Seric 					return EX_CONFIG;
9189374Seric 				}
91916914Seric 				if (tTd(21, 15))
92016914Seric 				{
92116914Seric 					printf("$%c:", rp[1]);
92216914Seric 					pp = m->first;
92356678Seric 					while (pp <= m->last)
92416914Seric 					{
92516914Seric 						printf(" %x=\"", *pp);
92616914Seric 						(void) fflush(stdout);
92716914Seric 						printf("%s\"", *pp++);
92816914Seric 					}
92916914Seric 					printf("\n");
93016914Seric 				}
9319374Seric 				pp = m->first;
93256678Seric 				while (pp <= m->last)
9333149Seric 				{
93416914Seric 					if (avp >= &npvp[MAXATOM])
93556678Seric 					{
93658151Seric 						syserr("554 rewrite: expansion too long");
93759084Seric 						return EX_DATAERR;
93856678Seric 					}
93916914Seric 					*avp++ = *pp++;
9403149Seric 				}
9413149Seric 			}
94216914Seric 			else
9438226Seric 			{
94416914Seric 				/* vanilla replacement */
9459374Seric 				if (avp >= &npvp[MAXATOM])
94616889Seric 				{
94756678Seric 	toolong:
94858151Seric 					syserr("554 rewrite: expansion too long");
94959084Seric 					return EX_DATAERR;
95016889Seric 				}
95159027Seric 				if ((*rp & 0377) != MACRODEXPAND)
95259027Seric 					*avp++ = rp;
95359027Seric 				else
95459027Seric 				{
95559027Seric 					*avp = macvalue(rp[1], e);
95659027Seric 					if (tTd(21, 2))
95759027Seric 						printf("rewrite: RHS $&%c => \"%s\"\n",
95859027Seric 							rp[1],
95959027Seric 							*avp == NULL ? "(NULL)" : *avp);
96059027Seric 					if (*avp != NULL)
96159027Seric 						avp++;
96259027Seric 				}
9638226Seric 			}
9649374Seric 		}
9659374Seric 		*avp++ = NULL;
96616914Seric 
96716914Seric 		/*
96856678Seric 		**  Check for any hostname/keyword lookups.
96916914Seric 		*/
97016914Seric 
97116914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
97216914Seric 		{
97356678Seric 			char **hbrvp;
97416914Seric 			char **xpvp;
97516914Seric 			int trsize;
97656678Seric 			char *replac;
97756678Seric 			int endtoken;
97856678Seric 			STAB *map;
97956678Seric 			char *mapname;
98056678Seric 			char **key_rvp;
98156678Seric 			char **arg_rvp;
98256678Seric 			char **default_rvp;
98356678Seric 			char buf[MAXNAME + 1];
98416914Seric 			char *pvpb1[MAXATOM + 1];
98556823Seric 			char *argvect[10];
98617174Seric 			char pvpbuf[PSBUFSIZE];
98764404Seric 			char *nullpvp[1];
98816914Seric 
98958050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
99058050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
99116914Seric 				continue;
99216914Seric 
99316914Seric 			/*
99456678Seric 			**  Got a hostname/keyword lookup.
99516914Seric 			**
99616914Seric 			**	This could be optimized fairly easily.
99716914Seric 			*/
99816914Seric 
99916914Seric 			hbrvp = rvp;
100058050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
100156327Seric 			{
100256678Seric 				endtoken = HOSTEND;
100356678Seric 				mapname = "host";
100456327Seric 			}
100556326Seric 			else
100656327Seric 			{
100756678Seric 				endtoken = LOOKUPEND;
100856678Seric 				mapname = *++rvp;
100956327Seric 			}
101056678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
101156678Seric 			if (map == NULL)
101258151Seric 				syserr("554 rewrite: map %s not found", mapname);
101356678Seric 
101456678Seric 			/* extract the match part */
101556678Seric 			key_rvp = ++rvp;
101656823Seric 			default_rvp = NULL;
101756823Seric 			arg_rvp = argvect;
101856823Seric 			xpvp = NULL;
101956823Seric 			replac = pvpbuf;
102058050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
102153654Seric 			{
102258050Seric 				int nodetype = **rvp & 0377;
102356823Seric 
102456823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
102556678Seric 				{
102656823Seric 					rvp++;
102756823Seric 					continue;
102856823Seric 				}
102956823Seric 
103056823Seric 				*rvp++ = NULL;
103156823Seric 
103256823Seric 				if (xpvp != NULL)
103356823Seric 				{
103458814Seric 					cataddr(xpvp, NULL, replac,
103558082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
103658082Seric 						'\0');
103756823Seric 					*++arg_rvp = replac;
103856823Seric 					replac += strlen(replac) + 1;
103956823Seric 					xpvp = NULL;
104056823Seric 				}
104156823Seric 				switch (nodetype)
104256823Seric 				{
104356678Seric 				  case CANONHOST:
104456823Seric 					xpvp = rvp;
104556678Seric 					break;
104656678Seric 
104756678Seric 				  case CANONUSER:
104856678Seric 					default_rvp = rvp;
104956678Seric 					break;
105056678Seric 				}
105153654Seric 			}
105216914Seric 			if (*rvp != NULL)
105316914Seric 				*rvp++ = NULL;
105456823Seric 			if (xpvp != NULL)
105556823Seric 			{
105658814Seric 				cataddr(xpvp, NULL, replac,
105758082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
105858082Seric 					'\0');
105956823Seric 				*++arg_rvp = replac;
106056823Seric 			}
106156823Seric 			*++arg_rvp = NULL;
106216914Seric 
106316914Seric 			/* save the remainder of the input string */
106416914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
106516914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
106616914Seric 
106756678Seric 			/* look it up */
106858814Seric 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
106956823Seric 			argvect[0] = buf;
107060538Seric 			if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags))
107156836Seric 			{
107259084Seric 				auto int stat = EX_OK;
107356836Seric 
107460215Seric 				/* XXX should try to auto-open the map here */
107560215Seric 
107658796Seric 				if (tTd(60, 1))
107758796Seric 					printf("map_lookup(%s, %s) => ",
107858796Seric 						mapname, buf);
107956836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
108060089Seric 						buf, argvect, &stat);
108158796Seric 				if (tTd(60, 1))
108259084Seric 					printf("%s (%d)\n",
108359084Seric 						replac ? replac : "NOT FOUND",
108459084Seric 						stat);
108559084Seric 
108659084Seric 				/* should recover if stat == EX_TEMPFAIL */
108759084Seric 				if (stat == EX_TEMPFAIL)
108859084Seric 					rstat = stat;
108956836Seric 			}
109053654Seric 			else
109156678Seric 				replac = NULL;
109256678Seric 
109356678Seric 			/* if no replacement, use default */
109456823Seric 			if (replac == NULL && default_rvp != NULL)
109556823Seric 			{
109660089Seric 				/* create the default */
109760089Seric 				cataddr(default_rvp, NULL, buf, sizeof buf, '\0');
109856823Seric 				replac = buf;
109956823Seric 			}
110056823Seric 
110156678Seric 			if (replac == NULL)
110251317Seric 			{
110356823Seric 				xpvp = key_rvp;
110453654Seric 			}
110564404Seric 			else if (*replac == '\0')
110664404Seric 			{
110764404Seric 				/* null replacement */
110864404Seric 				nullpvp[0] = NULL;
110964404Seric 				xpvp = nullpvp;
111064404Seric 			}
111156678Seric 			else
111253654Seric 			{
111356678Seric 				/* scan the new replacement */
111465066Seric 				xpvp = prescan(replac, '\0', pvpbuf,
111565066Seric 					       sizeof pvpbuf, NULL);
111653654Seric 				if (xpvp == NULL)
111751317Seric 				{
111858403Seric 					/* prescan already printed error */
111959084Seric 					return EX_DATAERR;
112056678Seric 				}
112151317Seric 			}
112251317Seric 
112316914Seric 			/* append it to the token list */
112456678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
112556678Seric 			{
112617174Seric 				*avp++ = newstr(*xpvp);
112716920Seric 				if (avp >= &npvp[MAXATOM])
112816914Seric 					goto toolong;
112917174Seric 			}
113016914Seric 
113116914Seric 			/* restore the old trailing information */
113256678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
113316920Seric 				if (avp >= &npvp[MAXATOM])
113416914Seric 					goto toolong;
113517174Seric 
113656678Seric 			break;
113716914Seric 		}
113816914Seric 
113916914Seric 		/*
114016914Seric 		**  Check for subroutine calls.
114116914Seric 		*/
114216914Seric 
114358050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
114456678Seric 		{
114559084Seric 			int stat;
114659084Seric 
114756678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
114856678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
114956678Seric 			if (tTd(21, 3))
115056678Seric 				printf("-----callsubr %s\n", npvp[1]);
115165071Seric 			stat = rewrite(pvp, atoi(npvp[1]), reclevel, e);
115259084Seric 			if (rstat == EX_OK || stat == EX_TEMPFAIL)
115359084Seric 				rstat = stat;
115465056Seric 			if (*pvp != NULL && (**pvp & 0377) == CANONNET)
115563581Seric 				rwr = NULL;
115656678Seric 		}
115756678Seric 		else
115856678Seric 		{
115917348Seric 			bcopy((char *) npvp, (char *) pvp,
116016900Seric 				(int) (avp - npvp) * sizeof *avp);
116156678Seric 		}
11629374Seric 		if (tTd(21, 4))
11639374Seric 		{
11649374Seric 			printf("rewritten as:");
116556678Seric 			printav(pvp);
11669374Seric 		}
1167297Seric 	}
11688069Seric 
11699279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
11708069Seric 	{
11718959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
117256678Seric 		printav(pvp);
11738069Seric 	}
117459084Seric 
117559084Seric 	return rstat;
11763149Seric }
11773149Seric /*
11783149Seric **  BUILDADDR -- build address from token vector.
11793149Seric **
11803149Seric **	Parameters:
11813149Seric **		tv -- token vector.
11823149Seric **		a -- pointer to address descriptor to fill.
11833149Seric **			If NULL, one will be allocated.
118464284Seric **		flags -- info regarding whether this is a sender or
118564284Seric **			a recipient.
118658966Seric **		e -- the current envelope.
11873149Seric **
11883149Seric **	Returns:
11894279Seric **		NULL if there was an error.
11904279Seric **		'a' otherwise.
11913149Seric **
11923149Seric **	Side Effects:
11933149Seric **		fills in 'a'
11943149Seric */
11953149Seric 
119657249Seric struct errcodes
119757249Seric {
119857249Seric 	char	*ec_name;		/* name of error code */
119957249Seric 	int	ec_code;		/* numeric code */
120057249Seric } ErrorCodes[] =
120157249Seric {
120257249Seric 	"usage",	EX_USAGE,
120357249Seric 	"nouser",	EX_NOUSER,
120457249Seric 	"nohost",	EX_NOHOST,
120557249Seric 	"unavailable",	EX_UNAVAILABLE,
120657249Seric 	"software",	EX_SOFTWARE,
120757249Seric 	"tempfail",	EX_TEMPFAIL,
120857249Seric 	"protocol",	EX_PROTOCOL,
120957249Seric #ifdef EX_CONFIG
121057249Seric 	"config",	EX_CONFIG,
121157249Seric #endif
121257249Seric 	NULL,		EX_UNAVAILABLE,
121357249Seric };
121457249Seric 
121556678Seric ADDRESS *
121664284Seric buildaddr(tv, a, flags, e)
12173149Seric 	register char **tv;
12183149Seric 	register ADDRESS *a;
121964284Seric 	int flags;
122058966Seric 	register ENVELOPE *e;
12213149Seric {
12223149Seric 	struct mailer **mp;
12233149Seric 	register struct mailer *m;
122458008Seric 	char *bp;
122558008Seric 	int spaceleft;
122664306Seric 	static MAILER errormailer;
122764306Seric 	static char *errorargv[] = { "ERROR", NULL };
122857402Seric 	static char buf[MAXNAME];
12293149Seric 
123064791Seric 	if (tTd(24, 5))
123164791Seric 	{
123264791Seric 		printf("buildaddr, flags=%o, tv=", flags);
123364791Seric 		printav(tv);
123464791Seric 	}
123564791Seric 
12363149Seric 	if (a == NULL)
12373149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
123816889Seric 	bzero((char *) a, sizeof *a);
12393149Seric 
12403149Seric 	/* figure out what net/mailer to use */
124164306Seric 	if (*tv == NULL || (**tv & 0377) != CANONNET)
12424279Seric 	{
124358151Seric 		syserr("554 buildaddr: no net");
124464306Seric badaddr:
124564306Seric 		a->q_flags |= QBADADDR;
124664306Seric 		a->q_mailer = &errormailer;
124764306Seric 		if (errormailer.m_name == NULL)
124864306Seric 		{
124964306Seric 			/* initialize the bogus mailer */
125064306Seric 			errormailer.m_name = "*error*";
125164306Seric 			errormailer.m_mailer = "ERROR";
125264306Seric 			errormailer.m_argv = errorargv;
125364306Seric 		}
125464306Seric 		return a;
12554279Seric 	}
12563149Seric 	tv++;
125758680Seric 	if (strcasecmp(*tv, "error") == 0)
12584279Seric 	{
125958050Seric 		if ((**++tv & 0377) == CANONHOST)
126010183Seric 		{
126157249Seric 			register struct errcodes *ep;
126257249Seric 
126358050Seric 			if (isascii(**++tv) && isdigit(**tv))
126457249Seric 			{
126557249Seric 				setstat(atoi(*tv));
126657249Seric 			}
126757249Seric 			else
126857249Seric 			{
126957249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
127057249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
127157249Seric 						break;
127257249Seric 				setstat(ep->ec_code);
127357249Seric 			}
127410183Seric 			tv++;
127510183Seric 		}
127664928Seric 		else
127764928Seric 			setstat(EX_UNAVAILABLE);
127858050Seric 		if ((**tv & 0377) != CANONUSER)
127958151Seric 			syserr("554 buildaddr: error: no user");
128058814Seric 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
128158082Seric 		stripquotes(buf);
128264659Seric 		if (isascii(buf[0]) && isdigit(buf[0]) &&
128364659Seric 		    isascii(buf[1]) && isdigit(buf[1]) &&
128464659Seric 		    isascii(buf[2]) && isdigit(buf[2]) &&
128564659Seric 		    buf[3] == ' ')
128664659Seric 		{
128764659Seric 			char fmt[10];
128864659Seric 
128964659Seric 			strncpy(fmt, buf, 3);
129064659Seric 			strcpy(&fmt[3], " %s");
129164659Seric 			usrerr(fmt, buf + 4);
129264659Seric 		}
129364659Seric 		else
129464659Seric 		{
129564659Seric 			usrerr("%s", buf);
129664659Seric 		}
129764306Seric 		goto badaddr;
12984279Seric 	}
129957402Seric 
13004598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
13013149Seric 	{
130258680Seric 		if (strcasecmp(m->m_name, *tv) == 0)
13033149Seric 			break;
13043149Seric 	}
13053149Seric 	if (m == NULL)
13064279Seric 	{
130758151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
130864306Seric 		goto badaddr;
13094279Seric 	}
13104598Seric 	a->q_mailer = m;
13113149Seric 
13123149Seric 	/* figure out what host (if any) */
131356678Seric 	tv++;
131458509Seric 	if ((**tv & 0377) == CANONHOST)
13153149Seric 	{
131658008Seric 		bp = buf;
131758008Seric 		spaceleft = sizeof buf - 1;
131858050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
131958008Seric 		{
132058008Seric 			int i = strlen(*tv);
132158008Seric 
132258008Seric 			if (i > spaceleft)
132358008Seric 			{
132458008Seric 				/* out of space for this address */
132558008Seric 				if (spaceleft >= 0)
132658151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
132758008Seric 						buf);
132858008Seric 				i = spaceleft;
132958008Seric 				spaceleft = 0;
133058008Seric 			}
133158008Seric 			if (i <= 0)
133258008Seric 				continue;
133358008Seric 			bcopy(*tv, bp, i);
133458008Seric 			bp += i;
133558008Seric 			spaceleft -= i;
133658008Seric 		}
133758010Seric 		*bp = '\0';
13385704Seric 		a->q_host = newstr(buf);
13393149Seric 	}
134057249Seric 	else
134158509Seric 	{
134258509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
134358509Seric 		{
134458509Seric 			syserr("554 buildaddr: no host");
134564306Seric 			goto badaddr;
134658509Seric 		}
134757249Seric 		a->q_host = NULL;
134858509Seric 	}
13493149Seric 
13503149Seric 	/* figure out the user */
135158050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
13524279Seric 	{
135358151Seric 		syserr("554 buildaddr: no user");
135464306Seric 		goto badaddr;
13554279Seric 	}
135657402Seric 	tv++;
135751317Seric 
135857402Seric 	/* do special mapping for local mailer */
135957402Seric 	if (m == LocalMailer && *tv != NULL)
136057402Seric 	{
136157454Seric 		register char *p = *tv;
136257454Seric 
136357454Seric 		if (*p == '"')
136457454Seric 			p++;
136557454Seric 		if (*p == '|')
136657402Seric 			a->q_mailer = m = ProgMailer;
136757454Seric 		else if (*p == '/')
136857402Seric 			a->q_mailer = m = FileMailer;
136957454Seric 		else if (*p == ':')
137057402Seric 		{
137157402Seric 			/* may be :include: */
137258814Seric 			cataddr(tv, NULL, buf, sizeof buf, '\0');
137358008Seric 			stripquotes(buf);
137458008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
137558008Seric 			{
137658008Seric 				/* if :include:, don't need further rewriting */
137757402Seric 				a->q_mailer = m = InclMailer;
137858008Seric 				a->q_user = &buf[9];
137958008Seric 				return (a);
138058008Seric 			}
138157402Seric 		}
138257402Seric 	}
138357402Seric 
138458008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
138558008Seric 	{
138658008Seric 		tv++;
138758008Seric 		a->q_flags |= QNOTREMOTE;
138858008Seric 	}
138958008Seric 
139064284Seric 	/* rewrite according recipient mailer rewriting rules */
139164284Seric 	define('h', a->q_host, e);
139264284Seric 	if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags))
139364284Seric 	{
139464284Seric 		/* sender addresses done later */
139565071Seric 		(void) rewrite(tv, 2, 0, e);
139664284Seric 		if (m->m_re_rwset > 0)
139765071Seric 		       (void) rewrite(tv, m->m_re_rwset, 0, e);
139864284Seric 	}
139965071Seric 	(void) rewrite(tv, 4, 0, e);
140019040Seric 
140119040Seric 	/* save the result for the command line/RCPT argument */
140258814Seric 	cataddr(tv, NULL, buf, sizeof buf, '\0');
14033149Seric 	a->q_user = buf;
14043149Seric 
140558670Seric 	/*
140658670Seric 	**  Do mapping to lower case as requested by mailer
140758670Seric 	*/
140858670Seric 
140958670Seric 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
141058670Seric 		makelower(a->q_host);
141158670Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
141258670Seric 		makelower(a->q_user);
141358670Seric 
14143149Seric 	return (a);
14153149Seric }
14163188Seric /*
14174228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
14184228Seric **
14194228Seric **	Parameters:
14204228Seric **		pvp -- parameter vector to rebuild.
142158814Seric **		evp -- last parameter to include.  Can be NULL to
142258814Seric **			use entire pvp.
14234228Seric **		buf -- buffer to build the string into.
14244228Seric **		sz -- size of buf.
142558082Seric **		spacesub -- the space separator character; if null,
142658082Seric **			use SpaceSub.
14274228Seric **
14284228Seric **	Returns:
14294228Seric **		none.
14304228Seric **
14314228Seric **	Side Effects:
14324228Seric **		Destroys buf.
14334228Seric */
14344228Seric 
143558814Seric cataddr(pvp, evp, buf, sz, spacesub)
14364228Seric 	char **pvp;
143758814Seric 	char **evp;
14384228Seric 	char *buf;
14394228Seric 	register int sz;
144058082Seric 	char spacesub;
14414228Seric {
14424228Seric 	bool oatomtok = FALSE;
144356678Seric 	bool natomtok = FALSE;
14444228Seric 	register int i;
14454228Seric 	register char *p;
14464228Seric 
144758082Seric 	if (spacesub == '\0')
144858082Seric 		spacesub = SpaceSub;
144958082Seric 
14508423Seric 	if (pvp == NULL)
14518423Seric 	{
145223109Seric 		(void) strcpy(buf, "");
14538423Seric 		return;
14548423Seric 	}
14554228Seric 	p = buf;
145611156Seric 	sz -= 2;
14574228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
14584228Seric 	{
14598078Seric 		natomtok = (toktype(**pvp) == ATM);
14604228Seric 		if (oatomtok && natomtok)
146158082Seric 			*p++ = spacesub;
14624228Seric 		(void) strcpy(p, *pvp);
14634228Seric 		oatomtok = natomtok;
14644228Seric 		p += i;
146511156Seric 		sz -= i + 1;
146658814Seric 		if (pvp++ == evp)
146758814Seric 			break;
14684228Seric 	}
14694228Seric 	*p = '\0';
14704228Seric }
14714228Seric /*
14723188Seric **  SAMEADDR -- Determine if two addresses are the same
14733188Seric **
14743188Seric **	This is not just a straight comparison -- if the mailer doesn't
14753188Seric **	care about the host we just ignore it, etc.
14763188Seric **
14773188Seric **	Parameters:
14783188Seric **		a, b -- pointers to the internal forms to compare.
14793188Seric **
14803188Seric **	Returns:
14813188Seric **		TRUE -- they represent the same mailbox.
14823188Seric **		FALSE -- they don't.
14833188Seric **
14843188Seric **	Side Effects:
14853188Seric **		none.
14863188Seric */
14873188Seric 
14883188Seric bool
14899374Seric sameaddr(a, b)
14903188Seric 	register ADDRESS *a;
14913188Seric 	register ADDRESS *b;
14923188Seric {
14933188Seric 	/* if they don't have the same mailer, forget it */
14943188Seric 	if (a->q_mailer != b->q_mailer)
14953188Seric 		return (FALSE);
14963188Seric 
14973188Seric 	/* if the user isn't the same, we can drop out */
149856678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
14993188Seric 		return (FALSE);
15003188Seric 
150158438Seric 	/* if we have good uids for both but the differ, these are different */
150258438Seric 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
150358438Seric 		return (FALSE);
150458438Seric 
150558509Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
150658509Seric 	if (a->q_host == b->q_host)
150758509Seric 	{
150858509Seric 		/* probably both null pointers */
15093188Seric 		return (TRUE);
151058509Seric 	}
15113188Seric 	if (a->q_host == NULL || b->q_host == NULL)
151258509Seric 	{
151358509Seric 		/* only one is a null pointer */
15143188Seric 		return (FALSE);
151558509Seric 	}
151656678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
15173188Seric 		return (FALSE);
15183188Seric 
15193188Seric 	return (TRUE);
15203188Seric }
15213234Seric /*
15223234Seric **  PRINTADDR -- print address (for debugging)
15233234Seric **
15243234Seric **	Parameters:
15253234Seric **		a -- the address to print
15263234Seric **		follow -- follow the q_next chain.
15273234Seric **
15283234Seric **	Returns:
15293234Seric **		none.
15303234Seric **
15313234Seric **	Side Effects:
15323234Seric **		none.
15333234Seric */
15343234Seric 
15353234Seric printaddr(a, follow)
15363234Seric 	register ADDRESS *a;
15373234Seric 	bool follow;
15383234Seric {
15395001Seric 	bool first = TRUE;
154057731Seric 	register MAILER *m;
154157731Seric 	MAILER pseudomailer;
15425001Seric 
15433234Seric 	while (a != NULL)
15443234Seric 	{
15455001Seric 		first = FALSE;
15464443Seric 		printf("%x=", a);
15474085Seric 		(void) fflush(stdout);
154857731Seric 
154957731Seric 		/* find the mailer -- carefully */
155057731Seric 		m = a->q_mailer;
155157731Seric 		if (m == NULL)
155257731Seric 		{
155357731Seric 			m = &pseudomailer;
155457731Seric 			m->m_mno = -1;
155557731Seric 			m->m_name = "NULL";
155657731Seric 		}
155757731Seric 
155858680Seric 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
155957731Seric 		       a->q_paddr, m->m_mno, m->m_name,
156063756Seric 		       a->q_host, a->q_user,
156163756Seric 		       a->q_ruser ? a->q_ruser : "<null>");
156259269Seric 		printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n",
156359269Seric 		       a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid);
156459269Seric 		printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n",
156559269Seric 		       a->q_owner == NULL ? "(none)" : a->q_owner,
156663756Seric 		       a->q_home == NULL ? "(none)" : a->q_home,
156763756Seric 		       a->q_fullname == NULL ? "(none)" : a->q_fullname);
15684996Seric 
15693234Seric 		if (!follow)
15703234Seric 			return;
15714996Seric 		a = a->q_next;
15723234Seric 	}
15735001Seric 	if (first)
15744443Seric 		printf("[NULL]\n");
15753234Seric }
15764317Seric 
15777682Seric /*
15787682Seric **  REMOTENAME -- return the name relative to the current mailer
15797682Seric **
15807682Seric **	Parameters:
15817682Seric **		name -- the name to translate.
15828069Seric **		m -- the mailer that we want to do rewriting relative
15838069Seric **			to.
158459163Seric **		flags -- fine tune operations.
158559163Seric **		pstat -- pointer to status word.
158658020Seric **		e -- the current envelope.
15877682Seric **
15887682Seric **	Returns:
15897682Seric **		the text string representing this address relative to
15907682Seric **			the receiving mailer.
15917682Seric **
15927682Seric **	Side Effects:
15937682Seric **		none.
15947682Seric **
15957682Seric **	Warnings:
15967682Seric **		The text string returned is tucked away locally;
15977682Seric **			copy it if you intend to save it.
15987682Seric */
15997682Seric 
16007682Seric char *
160159163Seric remotename(name, m, flags, pstat, e)
16027682Seric 	char *name;
160356678Seric 	struct mailer *m;
160459163Seric 	int flags;
160559163Seric 	int *pstat;
160656678Seric 	register ENVELOPE *e;
16077682Seric {
16088069Seric 	register char **pvp;
16098069Seric 	char *fancy;
161056678Seric 	char *oldg = macvalue('g', e);
161158020Seric 	int rwset;
16127682Seric 	static char buf[MAXNAME];
16137682Seric 	char lbuf[MAXNAME];
161416914Seric 	char pvpbuf[PSBUFSIZE];
161556678Seric 	extern char *crackaddr();
16167682Seric 
16177755Seric 	if (tTd(12, 1))
16187755Seric 		printf("remotename(%s)\n", name);
16197755Seric 
162010177Seric 	/* don't do anything if we are tagging it as special */
162159163Seric 	if (bitset(RF_SENDERADDR, flags))
162259163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
162359163Seric 						     : m->m_se_rwset;
162458020Seric 	else
162559163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
162659163Seric 						     : m->m_re_rwset;
162758020Seric 	if (rwset < 0)
162810177Seric 		return (name);
162910177Seric 
16307682Seric 	/*
16318181Seric 	**  Do a heuristic crack of this name to extract any comment info.
16328181Seric 	**	This will leave the name as a comment and a $g macro.
16337889Seric 	*/
16347889Seric 
163559163Seric 	if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags))
163658050Seric 		fancy = "\201g";
163710310Seric 	else
163810310Seric 		fancy = crackaddr(name);
16397889Seric 
16408181Seric 	/*
16418181Seric 	**  Turn the name into canonical form.
16428181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
16438181Seric 	**	If this only resolves to "user", and the "C" flag is
16448181Seric 	**	specified in the sending mailer, then the sender's
16458181Seric 	**	domain will be appended.
16468181Seric 	*/
16478181Seric 
164865066Seric 	pvp = prescan(name, '\0', pvpbuf, sizeof pvpbuf, NULL);
16497889Seric 	if (pvp == NULL)
16507889Seric 		return (name);
165165071Seric 	if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL)
165259163Seric 		*pstat = EX_TEMPFAIL;
165359163Seric 	if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
16548181Seric 	{
16558181Seric 		/* append from domain to this address */
16568181Seric 		register char **pxp = pvp;
16578181Seric 
16589594Seric 		/* see if there is an "@domain" in the current name */
16598181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
16608181Seric 			pxp++;
16618181Seric 		if (*pxp == NULL)
16628181Seric 		{
16639594Seric 			/* no.... append the "@domain" from the sender */
166456678Seric 			register char **qxq = e->e_fromdomain;
16658181Seric 
16669594Seric 			while ((*pxp++ = *qxq++) != NULL)
16679594Seric 				continue;
166865071Seric 			if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL)
166959163Seric 				*pstat = EX_TEMPFAIL;
16708181Seric 		}
16718181Seric 	}
16728181Seric 
16738181Seric 	/*
16748959Seric 	**  Do more specific rewriting.
167556678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
167656678Seric 	**		a sender address or not.
16778181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
16788181Seric 	*/
16798181Seric 
168059163Seric 	if (bitset(RF_SENDERADDR, flags))
168159541Seric 	{
168265071Seric 		if (rewrite(pvp, 1, 0, e) == EX_TEMPFAIL)
168359163Seric 			*pstat = EX_TEMPFAIL;
168459541Seric 	}
16858069Seric 	else
168659541Seric 	{
168765071Seric 		if (rewrite(pvp, 2, 0, e) == EX_TEMPFAIL)
168859163Seric 			*pstat = EX_TEMPFAIL;
168959541Seric 	}
169058020Seric 	if (rwset > 0)
169159541Seric 	{
169265071Seric 		if (rewrite(pvp, rwset, 0, e) == EX_TEMPFAIL)
169359163Seric 			*pstat = EX_TEMPFAIL;
169459541Seric 	}
16957682Seric 
16968181Seric 	/*
16978959Seric 	**  Do any final sanitation the address may require.
16988959Seric 	**	This will normally be used to turn internal forms
16998959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
17008959Seric 	**	may be used as a default to the above rules.
17018959Seric 	*/
17028959Seric 
170365071Seric 	if (rewrite(pvp, 4, 0, e) == EX_TEMPFAIL)
170459163Seric 		*pstat = EX_TEMPFAIL;
17058959Seric 
17068959Seric 	/*
17078181Seric 	**  Now restore the comment information we had at the beginning.
17088181Seric 	*/
17098181Seric 
171058825Seric 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
171156678Seric 	define('g', lbuf, e);
171256678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
171356678Seric 	define('g', oldg, e);
17147682Seric 
17157682Seric 	if (tTd(12, 1))
17167755Seric 		printf("remotename => `%s'\n", buf);
17177682Seric 	return (buf);
17187682Seric }
171951317Seric /*
172056678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
172151317Seric **
172251317Seric **	Parameters:
172356678Seric **		a -- the address to map (but just the user name part).
172456678Seric **		sendq -- the sendq in which to install any replacement
172556678Seric **			addresses.
172651317Seric **
172751317Seric **	Returns:
172851317Seric **		none.
172951317Seric */
173051317Seric 
173156678Seric maplocaluser(a, sendq, e)
173256678Seric 	register ADDRESS *a;
173356678Seric 	ADDRESS **sendq;
173456678Seric 	ENVELOPE *e;
173551317Seric {
173656678Seric 	register char **pvp;
173756678Seric 	register ADDRESS *a1 = NULL;
173858333Seric 	auto char *delimptr;
173956678Seric 	char pvpbuf[PSBUFSIZE];
174051317Seric 
174156678Seric 	if (tTd(29, 1))
174256678Seric 	{
174356678Seric 		printf("maplocaluser: ");
174456678Seric 		printaddr(a, FALSE);
174556678Seric 	}
174665066Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, sizeof pvpbuf, &delimptr);
174756678Seric 	if (pvp == NULL)
174856678Seric 		return;
174951317Seric 
175065071Seric 	(void) rewrite(pvp, 5, 0, e);
175158050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
175256678Seric 		return;
175351317Seric 
175456678Seric 	/* if non-null, mailer destination specified -- has it changed? */
175564284Seric 	a1 = buildaddr(pvp, NULL, 0, e);
175656678Seric 	if (a1 == NULL || sameaddr(a, a1))
175756678Seric 		return;
175851317Seric 
175956678Seric 	/* mark old address as dead; insert new address */
176056678Seric 	a->q_flags |= QDONTSEND;
176157731Seric 	if (tTd(29, 5))
176257731Seric 	{
176357731Seric 		printf("maplocaluser: QDONTSEND ");
176457731Seric 		printaddr(a, FALSE);
176557731Seric 	}
176656678Seric 	a1->q_alias = a;
176764348Seric 	allocaddr(a1, RF_COPYALL, NULL);
176856678Seric 	(void) recipient(a1, sendq, e);
176951317Seric }
177058802Seric /*
177158802Seric **  DEQUOTE_INIT -- initialize dequote map
177258802Seric **
177358802Seric **	This is a no-op.
177458802Seric **
177558802Seric **	Parameters:
177658802Seric **		map -- the internal map structure.
177758802Seric **		args -- arguments.
177858802Seric **
177958802Seric **	Returns:
178058802Seric **		TRUE.
178158802Seric */
178258802Seric 
178358802Seric bool
178460219Seric dequote_init(map, args)
178558802Seric 	MAP *map;
178658802Seric 	char *args;
178758802Seric {
178858805Seric 	register char *p = args;
178958805Seric 
179058805Seric 	for (;;)
179158805Seric 	{
179258805Seric 		while (isascii(*p) && isspace(*p))
179358805Seric 			p++;
179458805Seric 		if (*p != '-')
179558805Seric 			break;
179658805Seric 		switch (*++p)
179758805Seric 		{
179858805Seric 		  case 'a':
179958805Seric 			map->map_app = ++p;
180058805Seric 			break;
180158805Seric 		}
180258805Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
180358805Seric 			p++;
180458805Seric 		if (*p != '\0')
180558805Seric 			*p = '\0';
180658805Seric 	}
180758805Seric 	if (map->map_app != NULL)
180858805Seric 		map->map_app = newstr(map->map_app);
180958805Seric 
181058802Seric 	return TRUE;
181158802Seric }
181258802Seric /*
181358802Seric **  DEQUOTE_MAP -- unquote an address
181458802Seric **
181558802Seric **	Parameters:
181658802Seric **		map -- the internal map structure (ignored).
181760089Seric **		name -- the name to dequote.
181858802Seric **		av -- arguments (ignored).
181959084Seric **		statp -- pointer to status out-parameter.
182058802Seric **
182158802Seric **	Returns:
182258802Seric **		NULL -- if there were no quotes, or if the resulting
182358802Seric **			unquoted buffer would not be acceptable to prescan.
182458802Seric **		else -- The dequoted buffer.
182558802Seric */
182658802Seric 
182758802Seric char *
182860089Seric dequote_map(map, name, av, statp)
182958802Seric 	MAP *map;
183060089Seric 	char *name;
183158802Seric 	char **av;
183259084Seric 	int *statp;
183358802Seric {
183458802Seric 	register char *p;
183558802Seric 	register char *q;
183658802Seric 	register char c;
183758802Seric 	int anglecnt;
183858802Seric 	int cmntcnt;
183958802Seric 	int quotecnt;
184059089Seric 	int spacecnt;
184158802Seric 	bool quotemode;
184258802Seric 	bool bslashmode;
184358802Seric 
184458802Seric 	anglecnt = 0;
184558802Seric 	cmntcnt = 0;
184658802Seric 	quotecnt = 0;
184759089Seric 	spacecnt = 0;
184858802Seric 	quotemode = FALSE;
184958802Seric 	bslashmode = FALSE;
185058802Seric 
185160089Seric 	for (p = q = name; (c = *p++) != '\0'; )
185258802Seric 	{
185358802Seric 		if (bslashmode)
185458802Seric 		{
185558802Seric 			bslashmode = FALSE;
185658802Seric 			*q++ = c;
185758802Seric 			continue;
185858802Seric 		}
185958802Seric 
186058802Seric 		switch (c)
186158802Seric 		{
186258802Seric 		  case '\\':
186358802Seric 			bslashmode = TRUE;
186458802Seric 			break;
186558802Seric 
186658802Seric 		  case '(':
186758802Seric 			cmntcnt++;
186858802Seric 			break;
186958802Seric 
187058802Seric 		  case ')':
187158802Seric 			if (cmntcnt-- <= 0)
187258802Seric 				return NULL;
187358802Seric 			break;
187459089Seric 
187559089Seric 		  case ' ':
187659089Seric 			spacecnt++;
187759089Seric 			break;
187858802Seric 		}
187958802Seric 
188058802Seric 		if (cmntcnt > 0)
188158802Seric 		{
188258802Seric 			*q++ = c;
188358802Seric 			continue;
188458802Seric 		}
188558802Seric 
188658802Seric 		switch (c)
188758802Seric 		{
188858802Seric 		  case '"':
188958802Seric 			quotemode = !quotemode;
189058802Seric 			quotecnt++;
189158802Seric 			continue;
189258802Seric 
189358802Seric 		  case '<':
189458802Seric 			anglecnt++;
189558802Seric 			break;
189658802Seric 
189758802Seric 		  case '>':
189858802Seric 			if (anglecnt-- <= 0)
189958802Seric 				return NULL;
190058802Seric 			break;
190158802Seric 		}
190258802Seric 		*q++ = c;
190358802Seric 	}
190458802Seric 
190558802Seric 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
190659089Seric 	    quotemode || quotecnt <= 0 || spacecnt != 0)
190758802Seric 		return NULL;
190858802Seric 	*q++ = '\0';
190960089Seric 	return name;
191058802Seric }
1911