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*64764Seric static char sccsid[] = "@(#)parseaddr.c	8.15 (Berkeley) 10/28/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 
8058333Seric 	pvp = prescan(addr, delim, 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;
12059084Seric 	if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
12159084Seric 		queueup = TRUE;
12259084Seric 	if (rewrite(pvp, 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)
188*64764Seric 	{
18964726Seric 		savedelim = *delimptr;
190*64764Seric 		if (savedelim != '\0')
191*64764Seric 			*delimptr = '\0';
192*64764Seric 	}
19364726Seric #if 0
19464726Seric 	/* for testing.... */
19564726Seric 	if (strcmp(addr, "INvalidADDR") == 0)
19657388Seric 	{
19764726Seric 		usrerr("553 INvalid ADDRess");
198*64764Seric 		goto addrfailure;
19957388Seric 	}
20064726Seric #endif
20164726Seric 	for (; *addr != '\0'; addr++)
20264726Seric 	{
20364726Seric 		if ((*addr & 0340) == 0200)
20464726Seric 			break;
20564726Seric 	}
20664726Seric 	if (*addr == '\0')
207*64764Seric 	{
208*64764Seric 		if (savedelim != '\0' && delimptr != NULL)
209*64764Seric 			*delimptr = savedelim;
21064726Seric 		return FALSE;
211*64764Seric 	}
21264726Seric 	setstat(EX_USAGE);
21364726Seric 	usrerr("553 Address contained invalid control characters");
214*64764Seric   addrfailure:
215*64764Seric 	if (savedelim != '\0' && delimptr != NULL)
216*64764Seric 		*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.
28558333Seric **		delimptr -- if non-NULL, set to the location of the
28658333Seric **			terminating delimiter.
287297Seric **
288297Seric **	Returns:
2893149Seric **		A pointer to a vector of tokens.
290297Seric **		NULL on error.
291297Seric */
292297Seric 
2938078Seric /* states and character types */
2948078Seric # define OPR		0	/* operator */
2958078Seric # define ATM		1	/* atom */
2968078Seric # define QST		2	/* in quoted string */
2978078Seric # define SPC		3	/* chewing up spaces */
2988078Seric # define ONE		4	/* pick up one character */
2993149Seric 
3008078Seric # define NSTATES	5	/* number of states */
3018078Seric # define TYPE		017	/* mask to select state type */
3028078Seric 
3038078Seric /* meta bits for table */
3048078Seric # define M		020	/* meta character; don't pass through */
3058078Seric # define B		040	/* cause a break */
3068078Seric # define MB		M|B	/* meta-break */
3078078Seric 
3088078Seric static short StateTab[NSTATES][NSTATES] =
3098078Seric {
3108087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
3119051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
3129051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
3139051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
3148078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
3158078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
3168078Seric };
3178078Seric 
3188078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
3198078Seric 
3203149Seric char **
32158333Seric prescan(addr, delim, pvpbuf, delimptr)
322297Seric 	char *addr;
323297Seric 	char delim;
32416914Seric 	char pvpbuf[];
32558333Seric 	char **delimptr;
326297Seric {
327297Seric 	register char *p;
3288078Seric 	register char *q;
3299346Seric 	register int c;
3303149Seric 	char **avp;
331297Seric 	bool bslashmode;
332297Seric 	int cmntcnt;
3338423Seric 	int anglecnt;
3343149Seric 	char *tok;
3358078Seric 	int state;
3368078Seric 	int newstate;
33759747Seric 	char *saveto = CurEnv->e_to;
3388078Seric 	static char *av[MAXATOM+1];
33956678Seric 	extern int errno;
340297Seric 
34115253Seric 	/* make sure error messages don't have garbage on them */
34215253Seric 	errno = 0;
34315253Seric 
34416914Seric 	q = pvpbuf;
3453149Seric 	bslashmode = FALSE;
3467800Seric 	cmntcnt = 0;
3478423Seric 	anglecnt = 0;
3483149Seric 	avp = av;
34956678Seric 	state = ATM;
3508078Seric 	c = NOCHAR;
3518078Seric 	p = addr;
35259747Seric 	CurEnv->e_to = p;
35356764Seric 	if (tTd(22, 11))
354297Seric 	{
3558078Seric 		printf("prescan: ");
3568078Seric 		xputs(p);
35723109Seric 		(void) putchar('\n');
3588078Seric 	}
3598078Seric 
3608078Seric 	do
3618078Seric 	{
3623149Seric 		/* read a token */
3633149Seric 		tok = q;
3648078Seric 		for (;;)
365297Seric 		{
3668078Seric 			/* store away any old lookahead character */
36759277Seric 			if (c != NOCHAR && !bslashmode)
3688078Seric 			{
36915284Seric 				/* see if there is room */
37016914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3718078Seric 				{
37258151Seric 					usrerr("553 Address too long");
37358333Seric 					if (delimptr != NULL)
37458333Seric 						*delimptr = p;
37559747Seric 					CurEnv->e_to = saveto;
3768078Seric 					return (NULL);
3778078Seric 				}
37815284Seric 
37915284Seric 				/* squirrel it away */
3808078Seric 				*q++ = c;
3818078Seric 			}
3828078Seric 
3838078Seric 			/* read a new input character */
3848078Seric 			c = *p++;
38557631Seric 			if (c == '\0')
38656764Seric 			{
38756764Seric 				/* diagnose and patch up bad syntax */
38856764Seric 				if (state == QST)
38956764Seric 				{
39063851Seric 					usrerr("653 Unbalanced '\"' (fixed)");
39156764Seric 					c = '"';
39256764Seric 				}
39356764Seric 				else if (cmntcnt > 0)
39456764Seric 				{
39563851Seric 					usrerr("653 Unbalanced '(' (fixed)");
39656764Seric 					c = ')';
39756764Seric 				}
39856764Seric 				else if (anglecnt > 0)
39956764Seric 				{
40056764Seric 					c = '>';
40163851Seric 					usrerr("653 Unbalanced '<' (fixed)");
40256764Seric 				}
40356764Seric 				else
40456764Seric 					break;
40515284Seric 
40656764Seric 				p--;
40756764Seric 			}
40857631Seric 			else if (c == delim && anglecnt <= 0 &&
40957631Seric 					cmntcnt <= 0 && state != QST)
41057631Seric 				break;
41156764Seric 
4128078Seric 			if (tTd(22, 101))
4138078Seric 				printf("c=%c, s=%d; ", c, state);
4148078Seric 
4153149Seric 			/* chew up special characters */
4163149Seric 			*q = '\0';
4173149Seric 			if (bslashmode)
4183149Seric 			{
41959105Seric 				bslashmode = FALSE;
42059105Seric 
42124944Seric 				/* kludge \! for naive users */
42258061Seric 				if (cmntcnt > 0)
42359105Seric 				{
42458061Seric 					c = NOCHAR;
42559105Seric 					continue;
42659105Seric 				}
42759105Seric 				else if (c != '!' || state == QST)
42859105Seric 				{
42956678Seric 					*q++ = '\\';
43059105Seric 					continue;
43159105Seric 				}
4323149Seric 			}
43356678Seric 
43456678Seric 			if (c == '\\')
4353149Seric 			{
4363149Seric 				bslashmode = TRUE;
4373149Seric 			}
43856678Seric 			else if (state == QST)
4398514Seric 			{
4408514Seric 				/* do nothing, just avoid next clauses */
4418514Seric 			}
4428078Seric 			else if (c == '(')
4434100Seric 			{
4448078Seric 				cmntcnt++;
4458078Seric 				c = NOCHAR;
4464100Seric 			}
4478078Seric 			else if (c == ')')
4483149Seric 			{
4498078Seric 				if (cmntcnt <= 0)
4503149Seric 				{
45163851Seric 					usrerr("653 Unbalanced ')' (fixed)");
45263844Seric 					c = NOCHAR;
4533149Seric 				}
4548078Seric 				else
4558078Seric 					cmntcnt--;
4568078Seric 			}
4578078Seric 			else if (cmntcnt > 0)
4588078Seric 				c = NOCHAR;
4598423Seric 			else if (c == '<')
4608423Seric 				anglecnt++;
4618423Seric 			else if (c == '>')
4628423Seric 			{
4638423Seric 				if (anglecnt <= 0)
4648423Seric 				{
46563851Seric 					usrerr("653 Unbalanced '>' (fixed)");
46663844Seric 					c = NOCHAR;
4678423Seric 				}
46863844Seric 				else
46963844Seric 					anglecnt--;
4708423Seric 			}
47158050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
47211423Seric 				c = ' ';
4733149Seric 
4748078Seric 			if (c == NOCHAR)
4758078Seric 				continue;
4763149Seric 
4778078Seric 			/* see if this is end of input */
47811405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4793149Seric 				break;
4803149Seric 
4818078Seric 			newstate = StateTab[state][toktype(c)];
4828078Seric 			if (tTd(22, 101))
4838078Seric 				printf("ns=%02o\n", newstate);
4848078Seric 			state = newstate & TYPE;
4858078Seric 			if (bitset(M, newstate))
4868078Seric 				c = NOCHAR;
4878078Seric 			if (bitset(B, newstate))
4884228Seric 				break;
489297Seric 		}
4903149Seric 
4913149Seric 		/* new token */
4928078Seric 		if (tok != q)
4931378Seric 		{
4948078Seric 			*q++ = '\0';
4958078Seric 			if (tTd(22, 36))
496297Seric 			{
4978078Seric 				printf("tok=");
4988078Seric 				xputs(tok);
49923109Seric 				(void) putchar('\n');
500297Seric 			}
5018078Seric 			if (avp >= &av[MAXATOM])
502297Seric 			{
50358151Seric 				syserr("553 prescan: too many tokens");
50458333Seric 				if (delimptr != NULL)
50558333Seric 					*delimptr = p;
50659747Seric 				CurEnv->e_to = saveto;
5078078Seric 				return (NULL);
508297Seric 			}
5098078Seric 			*avp++ = tok;
510297Seric 		}
5118423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
5123149Seric 	*avp = NULL;
51358333Seric 	p--;
51458333Seric 	if (delimptr != NULL)
51558333Seric 		*delimptr = p;
51656764Seric 	if (tTd(22, 12))
51756764Seric 	{
51856764Seric 		printf("prescan==>");
51956764Seric 		printav(av);
52056764Seric 	}
52159747Seric 	CurEnv->e_to = saveto;
52258546Seric 	if (av[0] == NULL)
52358546Seric 		return (NULL);
52458403Seric 	return (av);
5253149Seric }
5263149Seric /*
5273149Seric **  TOKTYPE -- return token type
5283149Seric **
5293149Seric **	Parameters:
5303149Seric **		c -- the character in question.
5313149Seric **
5323149Seric **	Returns:
5333149Seric **		Its type.
5343149Seric **
5353149Seric **	Side Effects:
5363149Seric **		none.
5373149Seric */
538297Seric 
5393149Seric toktype(c)
54058050Seric 	register int c;
5413149Seric {
5423380Seric 	static char buf[50];
5433382Seric 	static bool firstime = TRUE;
5443380Seric 
5453382Seric 	if (firstime)
5463380Seric 	{
5473382Seric 		firstime = FALSE;
54858050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5497005Seric 		(void) strcat(buf, DELIMCHARS);
5503380Seric 	}
55158050Seric 	c &= 0377;
55256327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5538078Seric 		return (ONE);
55459027Seric 	if (c == MACRODEXPAND)
55559027Seric 		return (ONE);
5568078Seric 	if (c == '"')
5578078Seric 		return (QST);
55858050Seric 	if ((c & 0340) == 0200)
55958050Seric 		return (OPR);
5604100Seric 	if (!isascii(c))
5618078Seric 		return (ATM);
5628078Seric 	if (isspace(c) || c == ')')
5638078Seric 		return (SPC);
56458050Seric 	if (strchr(buf, c) != NULL)
5658078Seric 		return (OPR);
5668078Seric 	return (ATM);
5673149Seric }
5683149Seric /*
5693149Seric **  REWRITE -- apply rewrite rules to token vector.
5703149Seric **
5714476Seric **	This routine is an ordered production system.  Each rewrite
5724476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5734476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5744476Seric **
5754476Seric **	For each rewrite rule, 'avp' points the address vector we
5764476Seric **	are trying to match against, and 'pvp' points to the pattern.
5778058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5789585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5799585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5804476Seric **
5814476Seric **	When a match between avp & pvp does not match, we try to
5829585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5834476Seric **	we must also back out the match in mvp.  If we reach a
5848058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5858058Seric **	over again.
5864476Seric **
5874476Seric **	When we finally match, we rewrite the address vector
5884476Seric **	and try over again.
5894476Seric **
5903149Seric **	Parameters:
5913149Seric **		pvp -- pointer to token vector.
59259027Seric **		ruleset -- the ruleset to use for rewriting.
59359027Seric **		e -- the current envelope.
5943149Seric **
5953149Seric **	Returns:
59659084Seric **		A status code.  If EX_TEMPFAIL, higher level code should
59759084Seric **			attempt recovery.
5983149Seric **
5993149Seric **	Side Effects:
6003149Seric **		pvp is modified.
6013149Seric */
6022091Seric 
6033149Seric struct match
6043149Seric {
6054468Seric 	char	**first;	/* first token matched */
6064468Seric 	char	**last;		/* last token matched */
60758825Seric 	char	**pattern;	/* pointer to pattern */
6083149Seric };
6093149Seric 
6104468Seric # define MAXMATCH	9	/* max params per rewrite */
6113149Seric 
6123149Seric 
61359084Seric int
61459027Seric rewrite(pvp, ruleset, e)
6153149Seric 	char **pvp;
6164070Seric 	int ruleset;
61759027Seric 	register ENVELOPE *e;
6183149Seric {
6193149Seric 	register char *ap;		/* address pointer */
6203149Seric 	register char *rp;		/* rewrite pointer */
6213149Seric 	register char **avp;		/* address vector pointer */
6223149Seric 	register char **rvp;		/* rewrite vector pointer */
6238058Seric 	register struct match *mlp;	/* cur ptr into mlist */
6248058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
62558866Seric 	int ruleno;			/* current rule number */
62659084Seric 	int rstat = EX_OK;		/* return status */
62764740Seric 	int loopcount;
62856678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
6293149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
6303149Seric 
6319279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
6323149Seric 	{
6338959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
63456678Seric 		printav(pvp);
6353149Seric 	}
63656678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
63756326Seric 	{
63858151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
63959084Seric 		return EX_CONFIG;
64056326Seric 	}
64156678Seric 	if (pvp == NULL)
64259084Seric 		return EX_USAGE;
64356326Seric 
6443149Seric 	/*
64556678Seric 	**  Run through the list of rewrite rules, applying
64656678Seric 	**	any that match.
6473149Seric 	*/
6483149Seric 
64958866Seric 	ruleno = 1;
65064740Seric 	loopcount = 0;
6514070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6523149Seric 	{
6537675Seric 		if (tTd(21, 12))
654297Seric 		{
6558069Seric 			printf("-----trying rule:");
65656678Seric 			printav(rwr->r_lhs);
6573149Seric 		}
6583149Seric 
6593149Seric 		/* try to match on this rule */
6604468Seric 		mlp = mlist;
6618058Seric 		rvp = rwr->r_lhs;
6628058Seric 		avp = pvp;
66358866Seric 		if (++loopcount > 100)
6643149Seric 		{
66558866Seric 			syserr("554 Infinite loop in ruleset %d, rule %d",
66658866Seric 				ruleset, ruleno);
66758866Seric 			if (tTd(21, 1))
66852637Seric 			{
66956678Seric 				printf("workspace: ");
67056678Seric 				printav(pvp);
67152637Seric 			}
67258866Seric 			break;
67358866Seric 		}
67458866Seric 
67558866Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
67658866Seric 		{
6773149Seric 			rp = *rvp;
6788058Seric 			if (tTd(21, 35))
6798058Seric 			{
68058825Seric 				printf("ADVANCE rp=");
68157531Seric 				xputs(rp);
68257532Seric 				printf(", ap=");
6838058Seric 				xputs(ap);
6848069Seric 				printf("\n");
6858058Seric 			}
68656678Seric 			if (rp == NULL)
68756326Seric 			{
6883149Seric 				/* end-of-pattern before end-of-address */
6898058Seric 				goto backup;
69056678Seric 			}
69158173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
69258827Seric 			    (*rp & 0377) != MATCHZERO)
69356326Seric 			{
69458825Seric 				/* end-of-input with patterns left */
69558825Seric 				goto backup;
696297Seric 			}
69756326Seric 
69858050Seric 			switch (*rp & 0377)
6998058Seric 			{
70056678Seric 				register STAB *s;
70158814Seric 				char buf[MAXLINE];
70256326Seric 
70356678Seric 			  case MATCHCLASS:
70458825Seric 				/* match any phrase in a class */
70558825Seric 				mlp->pattern = rvp;
70658814Seric 				mlp->first = avp;
70758814Seric 	extendclass:
70858825Seric 				ap = *avp;
70958825Seric 				if (ap == NULL)
71058814Seric 					goto backup;
71158814Seric 				mlp->last = avp++;
71258814Seric 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
71358814Seric 				s = stab(buf, ST_CLASS, ST_FIND);
71456678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
71556326Seric 				{
71658825Seric 					if (tTd(21, 36))
71758825Seric 					{
71858825Seric 						printf("EXTEND  rp=");
71958825Seric 						xputs(rp);
72058825Seric 						printf(", ap=");
72158825Seric 						xputs(ap);
72258825Seric 						printf("\n");
72358825Seric 					}
72458825Seric 					goto extendclass;
72556326Seric 				}
72658825Seric 				if (tTd(21, 36))
72758825Seric 					printf("CLMATCH\n");
72858814Seric 				mlp++;
72958814Seric 				break;
7304060Seric 
73158825Seric 			  case MATCHNCLASS:
73258825Seric 				/* match any token not in a class */
73358825Seric 				s = stab(ap, ST_CLASS, ST_FIND);
73458825Seric 				if (s != NULL && bitnset(rp[1], s->s_class))
73558825Seric 					goto backup;
73658825Seric 
73758825Seric 				/* fall through */
73858825Seric 
73956678Seric 			  case MATCHONE:
74056678Seric 			  case MATCHANY:
74156678Seric 				/* match exactly one token */
74258825Seric 				mlp->pattern = rvp;
74356678Seric 				mlp->first = avp;
74456678Seric 				mlp->last = avp++;
7458058Seric 				mlp++;
74656678Seric 				break;
7478058Seric 
74856678Seric 			  case MATCHZANY:
74956678Seric 				/* match zero or more tokens */
75058825Seric 				mlp->pattern = rvp;
75156678Seric 				mlp->first = avp;
75256678Seric 				mlp->last = avp - 1;
75356678Seric 				mlp++;
75456678Seric 				break;
75556326Seric 
75658827Seric 			  case MATCHZERO:
75758173Seric 				/* match zero tokens */
75858173Seric 				break;
75958173Seric 
76059027Seric 			  case MACRODEXPAND:
76159027Seric 				/*
76259027Seric 				**  Match against run-time macro.
76359027Seric 				**  This algorithm is broken for the
76459027Seric 				**  general case (no recursive macros,
76559027Seric 				**  improper tokenization) but should
76659027Seric 				**  work for the usual cases.
76759027Seric 				*/
76859027Seric 
76959027Seric 				ap = macvalue(rp[1], e);
77059027Seric 				mlp->first = avp;
77159027Seric 				if (tTd(21, 2))
77259027Seric 					printf("rewrite: LHS $&%c => \"%s\"\n",
77359027Seric 						rp[1],
77459027Seric 						ap == NULL ? "(NULL)" : ap);
77559027Seric 
77659027Seric 				if (ap == NULL)
77759027Seric 					break;
77860502Seric 				while (*ap != '\0')
77959027Seric 				{
78059027Seric 					if (*avp == NULL ||
78159027Seric 					    strncasecmp(ap, *avp, strlen(*avp)) != 0)
78259027Seric 					{
78359027Seric 						/* no match */
78459027Seric 						avp = mlp->first;
78559027Seric 						goto backup;
78659027Seric 					}
78759027Seric 					ap += strlen(*avp++);
78859027Seric 				}
78959027Seric 
79059027Seric 				/* match */
79159027Seric 				break;
79259027Seric 
79356678Seric 			  default:
79456678Seric 				/* must have exact match */
79556678Seric 				if (strcasecmp(rp, ap))
7968058Seric 					goto backup;
7974468Seric 				avp++;
79856678Seric 				break;
7993149Seric 			}
8003149Seric 
80156678Seric 			/* successful match on this token */
8023149Seric 			rvp++;
8033149Seric 			continue;
8043149Seric 
80558825Seric 	  backup:
80656678Seric 			/* match failed -- back up */
80758825Seric 			while (--mlp >= mlist)
8083149Seric 			{
80958825Seric 				rvp = mlp->pattern;
81056678Seric 				rp = *rvp;
81158825Seric 				avp = mlp->last + 1;
81258825Seric 				ap = *avp;
81358825Seric 
81458825Seric 				if (tTd(21, 36))
81558825Seric 				{
81658825Seric 					printf("BACKUP  rp=");
81758825Seric 					xputs(rp);
81858825Seric 					printf(", ap=");
81958825Seric 					xputs(ap);
82058825Seric 					printf("\n");
82158825Seric 				}
82258825Seric 
82358825Seric 				if (ap == NULL)
82458825Seric 				{
82558825Seric 					/* run off the end -- back up again */
82658825Seric 					continue;
82758825Seric 				}
82858050Seric 				if ((*rp & 0377) == MATCHANY ||
82958050Seric 				    (*rp & 0377) == MATCHZANY)
8304468Seric 				{
83156678Seric 					/* extend binding and continue */
83258825Seric 					mlp->last = avp++;
83356678Seric 					rvp++;
83458825Seric 					mlp++;
83556678Seric 					break;
8364468Seric 				}
83758825Seric 				if ((*rp & 0377) == MATCHCLASS)
83856678Seric 				{
83958814Seric 					/* extend binding and try again */
84063397Seric 					mlp->last = avp;
84158814Seric 					goto extendclass;
84258814Seric 				}
8433149Seric 			}
8443149Seric 
84558825Seric 			if (mlp < mlist)
84656678Seric 			{
84756678Seric 				/* total failure to match */
84856326Seric 				break;
8493149Seric 			}
850297Seric 		}
8513149Seric 
8523149Seric 		/*
85356678Seric 		**  See if we successfully matched
8543149Seric 		*/
8553149Seric 
85658827Seric 		if (mlp < mlist || *rvp != NULL)
8573149Seric 		{
8589374Seric 			if (tTd(21, 10))
8599374Seric 				printf("----- rule fails\n");
8609374Seric 			rwr = rwr->r_next;
86158866Seric 			ruleno++;
86264740Seric 			loopcount = 0;
8639374Seric 			continue;
8649374Seric 		}
8653149Seric 
8669374Seric 		rvp = rwr->r_rhs;
8679374Seric 		if (tTd(21, 12))
8689374Seric 		{
8699374Seric 			printf("-----rule matches:");
87056678Seric 			printav(rvp);
8719374Seric 		}
8729374Seric 
8739374Seric 		rp = *rvp;
87458050Seric 		if ((*rp & 0377) == CANONUSER)
8759374Seric 		{
8769374Seric 			rvp++;
8779374Seric 			rwr = rwr->r_next;
87858866Seric 			ruleno++;
87964740Seric 			loopcount = 0;
8809374Seric 		}
88158050Seric 		else if ((*rp & 0377) == CANONHOST)
8829374Seric 		{
8839374Seric 			rvp++;
8849374Seric 			rwr = NULL;
8859374Seric 		}
88658050Seric 		else if ((*rp & 0377) == CANONNET)
8879374Seric 			rwr = NULL;
8889374Seric 
8899374Seric 		/* substitute */
8909374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
8919374Seric 		{
8929374Seric 			register struct match *m;
8939374Seric 			register char **pp;
8949374Seric 
8958058Seric 			rp = *rvp;
89658050Seric 			if ((*rp & 0377) == MATCHREPL)
8978058Seric 			{
89816914Seric 				/* substitute from LHS */
89916914Seric 				m = &mlist[rp[1] - '1'];
90056678Seric 				if (m < mlist || m >= mlp)
9019374Seric 				{
90258151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
90356326Seric 						ruleset, rp[1]);
90459084Seric 					return EX_CONFIG;
9059374Seric 				}
90616914Seric 				if (tTd(21, 15))
90716914Seric 				{
90816914Seric 					printf("$%c:", rp[1]);
90916914Seric 					pp = m->first;
91056678Seric 					while (pp <= m->last)
91116914Seric 					{
91216914Seric 						printf(" %x=\"", *pp);
91316914Seric 						(void) fflush(stdout);
91416914Seric 						printf("%s\"", *pp++);
91516914Seric 					}
91616914Seric 					printf("\n");
91716914Seric 				}
9189374Seric 				pp = m->first;
91956678Seric 				while (pp <= m->last)
9203149Seric 				{
92116914Seric 					if (avp >= &npvp[MAXATOM])
92256678Seric 					{
92358151Seric 						syserr("554 rewrite: expansion too long");
92459084Seric 						return EX_DATAERR;
92556678Seric 					}
92616914Seric 					*avp++ = *pp++;
9273149Seric 				}
9283149Seric 			}
92916914Seric 			else
9308226Seric 			{
93116914Seric 				/* vanilla replacement */
9329374Seric 				if (avp >= &npvp[MAXATOM])
93316889Seric 				{
93456678Seric 	toolong:
93558151Seric 					syserr("554 rewrite: expansion too long");
93659084Seric 					return EX_DATAERR;
93716889Seric 				}
93859027Seric 				if ((*rp & 0377) != MACRODEXPAND)
93959027Seric 					*avp++ = rp;
94059027Seric 				else
94159027Seric 				{
94259027Seric 					*avp = macvalue(rp[1], e);
94359027Seric 					if (tTd(21, 2))
94459027Seric 						printf("rewrite: RHS $&%c => \"%s\"\n",
94559027Seric 							rp[1],
94659027Seric 							*avp == NULL ? "(NULL)" : *avp);
94759027Seric 					if (*avp != NULL)
94859027Seric 						avp++;
94959027Seric 				}
9508226Seric 			}
9519374Seric 		}
9529374Seric 		*avp++ = NULL;
95316914Seric 
95416914Seric 		/*
95556678Seric 		**  Check for any hostname/keyword lookups.
95616914Seric 		*/
95716914Seric 
95816914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
95916914Seric 		{
96056678Seric 			char **hbrvp;
96116914Seric 			char **xpvp;
96216914Seric 			int trsize;
96356678Seric 			char *replac;
96456678Seric 			int endtoken;
96556678Seric 			STAB *map;
96656678Seric 			char *mapname;
96756678Seric 			char **key_rvp;
96856678Seric 			char **arg_rvp;
96956678Seric 			char **default_rvp;
97056678Seric 			char buf[MAXNAME + 1];
97116914Seric 			char *pvpb1[MAXATOM + 1];
97256823Seric 			char *argvect[10];
97317174Seric 			char pvpbuf[PSBUFSIZE];
97464404Seric 			char *nullpvp[1];
97516914Seric 
97658050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
97758050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
97816914Seric 				continue;
97916914Seric 
98016914Seric 			/*
98156678Seric 			**  Got a hostname/keyword lookup.
98216914Seric 			**
98316914Seric 			**	This could be optimized fairly easily.
98416914Seric 			*/
98516914Seric 
98616914Seric 			hbrvp = rvp;
98758050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
98856327Seric 			{
98956678Seric 				endtoken = HOSTEND;
99056678Seric 				mapname = "host";
99156327Seric 			}
99256326Seric 			else
99356327Seric 			{
99456678Seric 				endtoken = LOOKUPEND;
99556678Seric 				mapname = *++rvp;
99656327Seric 			}
99756678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
99856678Seric 			if (map == NULL)
99958151Seric 				syserr("554 rewrite: map %s not found", mapname);
100056678Seric 
100156678Seric 			/* extract the match part */
100256678Seric 			key_rvp = ++rvp;
100356823Seric 			default_rvp = NULL;
100456823Seric 			arg_rvp = argvect;
100556823Seric 			xpvp = NULL;
100656823Seric 			replac = pvpbuf;
100758050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
100853654Seric 			{
100958050Seric 				int nodetype = **rvp & 0377;
101056823Seric 
101156823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
101256678Seric 				{
101356823Seric 					rvp++;
101456823Seric 					continue;
101556823Seric 				}
101656823Seric 
101756823Seric 				*rvp++ = NULL;
101856823Seric 
101956823Seric 				if (xpvp != NULL)
102056823Seric 				{
102158814Seric 					cataddr(xpvp, NULL, replac,
102258082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
102358082Seric 						'\0');
102456823Seric 					*++arg_rvp = replac;
102556823Seric 					replac += strlen(replac) + 1;
102656823Seric 					xpvp = NULL;
102756823Seric 				}
102856823Seric 				switch (nodetype)
102956823Seric 				{
103056678Seric 				  case CANONHOST:
103156823Seric 					xpvp = rvp;
103256678Seric 					break;
103356678Seric 
103456678Seric 				  case CANONUSER:
103556678Seric 					default_rvp = rvp;
103656678Seric 					break;
103756678Seric 				}
103853654Seric 			}
103916914Seric 			if (*rvp != NULL)
104016914Seric 				*rvp++ = NULL;
104156823Seric 			if (xpvp != NULL)
104256823Seric 			{
104358814Seric 				cataddr(xpvp, NULL, replac,
104458082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
104558082Seric 					'\0');
104656823Seric 				*++arg_rvp = replac;
104756823Seric 			}
104856823Seric 			*++arg_rvp = NULL;
104916914Seric 
105016914Seric 			/* save the remainder of the input string */
105116914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
105216914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
105316914Seric 
105456678Seric 			/* look it up */
105558814Seric 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
105656823Seric 			argvect[0] = buf;
105760538Seric 			if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags))
105856836Seric 			{
105959084Seric 				auto int stat = EX_OK;
106056836Seric 
106160215Seric 				/* XXX should try to auto-open the map here */
106260215Seric 
106358796Seric 				if (tTd(60, 1))
106458796Seric 					printf("map_lookup(%s, %s) => ",
106558796Seric 						mapname, buf);
106656836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
106760089Seric 						buf, argvect, &stat);
106858796Seric 				if (tTd(60, 1))
106959084Seric 					printf("%s (%d)\n",
107059084Seric 						replac ? replac : "NOT FOUND",
107159084Seric 						stat);
107259084Seric 
107359084Seric 				/* should recover if stat == EX_TEMPFAIL */
107459084Seric 				if (stat == EX_TEMPFAIL)
107559084Seric 					rstat = stat;
107656836Seric 			}
107753654Seric 			else
107856678Seric 				replac = NULL;
107956678Seric 
108056678Seric 			/* if no replacement, use default */
108156823Seric 			if (replac == NULL && default_rvp != NULL)
108256823Seric 			{
108360089Seric 				/* create the default */
108460089Seric 				cataddr(default_rvp, NULL, buf, sizeof buf, '\0');
108556823Seric 				replac = buf;
108656823Seric 			}
108756823Seric 
108856678Seric 			if (replac == NULL)
108951317Seric 			{
109056823Seric 				xpvp = key_rvp;
109153654Seric 			}
109264404Seric 			else if (*replac == '\0')
109364404Seric 			{
109464404Seric 				/* null replacement */
109564404Seric 				nullpvp[0] = NULL;
109664404Seric 				xpvp = nullpvp;
109764404Seric 			}
109856678Seric 			else
109953654Seric 			{
110056678Seric 				/* scan the new replacement */
110158333Seric 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
110253654Seric 				if (xpvp == NULL)
110351317Seric 				{
110458403Seric 					/* prescan already printed error */
110559084Seric 					return EX_DATAERR;
110656678Seric 				}
110751317Seric 			}
110851317Seric 
110916914Seric 			/* append it to the token list */
111056678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
111156678Seric 			{
111217174Seric 				*avp++ = newstr(*xpvp);
111316920Seric 				if (avp >= &npvp[MAXATOM])
111416914Seric 					goto toolong;
111517174Seric 			}
111616914Seric 
111716914Seric 			/* restore the old trailing information */
111856678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
111916920Seric 				if (avp >= &npvp[MAXATOM])
112016914Seric 					goto toolong;
112117174Seric 
112256678Seric 			break;
112316914Seric 		}
112416914Seric 
112516914Seric 		/*
112616914Seric 		**  Check for subroutine calls.
112716914Seric 		*/
112816914Seric 
112958050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
113056678Seric 		{
113159084Seric 			int stat;
113259084Seric 
113356678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
113456678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
113556678Seric 			if (tTd(21, 3))
113656678Seric 				printf("-----callsubr %s\n", npvp[1]);
113759084Seric 			stat = rewrite(pvp, atoi(npvp[1]), e);
113859084Seric 			if (rstat == EX_OK || stat == EX_TEMPFAIL)
113959084Seric 				rstat = stat;
114063581Seric 			if ((**pvp & 0377) == CANONNET)
114163581Seric 				rwr = NULL;
114256678Seric 		}
114356678Seric 		else
114456678Seric 		{
114517348Seric 			bcopy((char *) npvp, (char *) pvp,
114616900Seric 				(int) (avp - npvp) * sizeof *avp);
114756678Seric 		}
11489374Seric 		if (tTd(21, 4))
11499374Seric 		{
11509374Seric 			printf("rewritten as:");
115156678Seric 			printav(pvp);
11529374Seric 		}
1153297Seric 	}
11548069Seric 
11559279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
11568069Seric 	{
11578959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
115856678Seric 		printav(pvp);
11598069Seric 	}
116059084Seric 
116159084Seric 	return rstat;
11623149Seric }
11633149Seric /*
11643149Seric **  BUILDADDR -- build address from token vector.
11653149Seric **
11663149Seric **	Parameters:
11673149Seric **		tv -- token vector.
11683149Seric **		a -- pointer to address descriptor to fill.
11693149Seric **			If NULL, one will be allocated.
117064284Seric **		flags -- info regarding whether this is a sender or
117164284Seric **			a recipient.
117258966Seric **		e -- the current envelope.
11733149Seric **
11743149Seric **	Returns:
11754279Seric **		NULL if there was an error.
11764279Seric **		'a' otherwise.
11773149Seric **
11783149Seric **	Side Effects:
11793149Seric **		fills in 'a'
11803149Seric */
11813149Seric 
118257249Seric struct errcodes
118357249Seric {
118457249Seric 	char	*ec_name;		/* name of error code */
118557249Seric 	int	ec_code;		/* numeric code */
118657249Seric } ErrorCodes[] =
118757249Seric {
118857249Seric 	"usage",	EX_USAGE,
118957249Seric 	"nouser",	EX_NOUSER,
119057249Seric 	"nohost",	EX_NOHOST,
119157249Seric 	"unavailable",	EX_UNAVAILABLE,
119257249Seric 	"software",	EX_SOFTWARE,
119357249Seric 	"tempfail",	EX_TEMPFAIL,
119457249Seric 	"protocol",	EX_PROTOCOL,
119557249Seric #ifdef EX_CONFIG
119657249Seric 	"config",	EX_CONFIG,
119757249Seric #endif
119857249Seric 	NULL,		EX_UNAVAILABLE,
119957249Seric };
120057249Seric 
120156678Seric ADDRESS *
120264284Seric buildaddr(tv, a, flags, e)
12033149Seric 	register char **tv;
12043149Seric 	register ADDRESS *a;
120564284Seric 	int flags;
120658966Seric 	register ENVELOPE *e;
12073149Seric {
12083149Seric 	struct mailer **mp;
12093149Seric 	register struct mailer *m;
121058008Seric 	char *bp;
121158008Seric 	int spaceleft;
121264306Seric 	static MAILER errormailer;
121364306Seric 	static char *errorargv[] = { "ERROR", NULL };
121457402Seric 	static char buf[MAXNAME];
12153149Seric 
12163149Seric 	if (a == NULL)
12173149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
121816889Seric 	bzero((char *) a, sizeof *a);
12193149Seric 
12203149Seric 	/* figure out what net/mailer to use */
122164306Seric 	if (*tv == NULL || (**tv & 0377) != CANONNET)
12224279Seric 	{
122358151Seric 		syserr("554 buildaddr: no net");
122464306Seric badaddr:
122564306Seric 		a->q_flags |= QBADADDR;
122664306Seric 		a->q_mailer = &errormailer;
122764306Seric 		if (errormailer.m_name == NULL)
122864306Seric 		{
122964306Seric 			/* initialize the bogus mailer */
123064306Seric 			errormailer.m_name = "*error*";
123164306Seric 			errormailer.m_mailer = "ERROR";
123264306Seric 			errormailer.m_argv = errorargv;
123364306Seric 		}
123464306Seric 		return a;
12354279Seric 	}
12363149Seric 	tv++;
123758680Seric 	if (strcasecmp(*tv, "error") == 0)
12384279Seric 	{
123958050Seric 		if ((**++tv & 0377) == CANONHOST)
124010183Seric 		{
124157249Seric 			register struct errcodes *ep;
124257249Seric 
124358050Seric 			if (isascii(**++tv) && isdigit(**tv))
124457249Seric 			{
124557249Seric 				setstat(atoi(*tv));
124657249Seric 			}
124757249Seric 			else
124857249Seric 			{
124957249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
125057249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
125157249Seric 						break;
125257249Seric 				setstat(ep->ec_code);
125357249Seric 			}
125410183Seric 			tv++;
125510183Seric 		}
125658050Seric 		if ((**tv & 0377) != CANONUSER)
125758151Seric 			syserr("554 buildaddr: error: no user");
125858814Seric 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
125958082Seric 		stripquotes(buf);
126064659Seric 		if (isascii(buf[0]) && isdigit(buf[0]) &&
126164659Seric 		    isascii(buf[1]) && isdigit(buf[1]) &&
126264659Seric 		    isascii(buf[2]) && isdigit(buf[2]) &&
126364659Seric 		    buf[3] == ' ')
126464659Seric 		{
126564659Seric 			char fmt[10];
126664659Seric 
126764659Seric 			strncpy(fmt, buf, 3);
126864659Seric 			strcpy(&fmt[3], " %s");
126964659Seric 			usrerr(fmt, buf + 4);
127064659Seric 		}
127164659Seric 		else
127264659Seric 		{
127364659Seric 			usrerr("%s", buf);
127464659Seric 		}
127564306Seric 		goto badaddr;
12764279Seric 	}
127757402Seric 
12784598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
12793149Seric 	{
128058680Seric 		if (strcasecmp(m->m_name, *tv) == 0)
12813149Seric 			break;
12823149Seric 	}
12833149Seric 	if (m == NULL)
12844279Seric 	{
128558151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
128664306Seric 		goto badaddr;
12874279Seric 	}
12884598Seric 	a->q_mailer = m;
12893149Seric 
12903149Seric 	/* figure out what host (if any) */
129156678Seric 	tv++;
129258509Seric 	if ((**tv & 0377) == CANONHOST)
12933149Seric 	{
129458008Seric 		bp = buf;
129558008Seric 		spaceleft = sizeof buf - 1;
129658050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
129758008Seric 		{
129858008Seric 			int i = strlen(*tv);
129958008Seric 
130058008Seric 			if (i > spaceleft)
130158008Seric 			{
130258008Seric 				/* out of space for this address */
130358008Seric 				if (spaceleft >= 0)
130458151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
130558008Seric 						buf);
130658008Seric 				i = spaceleft;
130758008Seric 				spaceleft = 0;
130858008Seric 			}
130958008Seric 			if (i <= 0)
131058008Seric 				continue;
131158008Seric 			bcopy(*tv, bp, i);
131258008Seric 			bp += i;
131358008Seric 			spaceleft -= i;
131458008Seric 		}
131558010Seric 		*bp = '\0';
13165704Seric 		a->q_host = newstr(buf);
13173149Seric 	}
131857249Seric 	else
131958509Seric 	{
132058509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
132158509Seric 		{
132258509Seric 			syserr("554 buildaddr: no host");
132364306Seric 			goto badaddr;
132458509Seric 		}
132557249Seric 		a->q_host = NULL;
132658509Seric 	}
13273149Seric 
13283149Seric 	/* figure out the user */
132958050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
13304279Seric 	{
133158151Seric 		syserr("554 buildaddr: no user");
133264306Seric 		goto badaddr;
13334279Seric 	}
133457402Seric 	tv++;
133551317Seric 
133657402Seric 	/* do special mapping for local mailer */
133757402Seric 	if (m == LocalMailer && *tv != NULL)
133857402Seric 	{
133957454Seric 		register char *p = *tv;
134057454Seric 
134157454Seric 		if (*p == '"')
134257454Seric 			p++;
134357454Seric 		if (*p == '|')
134457402Seric 			a->q_mailer = m = ProgMailer;
134557454Seric 		else if (*p == '/')
134657402Seric 			a->q_mailer = m = FileMailer;
134757454Seric 		else if (*p == ':')
134857402Seric 		{
134957402Seric 			/* may be :include: */
135058814Seric 			cataddr(tv, NULL, buf, sizeof buf, '\0');
135158008Seric 			stripquotes(buf);
135258008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
135358008Seric 			{
135458008Seric 				/* if :include:, don't need further rewriting */
135557402Seric 				a->q_mailer = m = InclMailer;
135658008Seric 				a->q_user = &buf[9];
135758008Seric 				return (a);
135858008Seric 			}
135957402Seric 		}
136057402Seric 	}
136157402Seric 
136258008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
136358008Seric 	{
136458008Seric 		tv++;
136558008Seric 		a->q_flags |= QNOTREMOTE;
136658008Seric 	}
136758008Seric 
136864284Seric 	/* rewrite according recipient mailer rewriting rules */
136964284Seric 	define('h', a->q_host, e);
137064284Seric 	if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags))
137164284Seric 	{
137264284Seric 		/* sender addresses done later */
137364284Seric 		(void) rewrite(tv, 2, e);
137464284Seric 		if (m->m_re_rwset > 0)
137564284Seric 		       (void) rewrite(tv, m->m_re_rwset, e);
137664284Seric 	}
137759084Seric 	(void) rewrite(tv, 4, e);
137819040Seric 
137919040Seric 	/* save the result for the command line/RCPT argument */
138058814Seric 	cataddr(tv, NULL, buf, sizeof buf, '\0');
13813149Seric 	a->q_user = buf;
13823149Seric 
138358670Seric 	/*
138458670Seric 	**  Do mapping to lower case as requested by mailer
138558670Seric 	*/
138658670Seric 
138758670Seric 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
138858670Seric 		makelower(a->q_host);
138958670Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
139058670Seric 		makelower(a->q_user);
139158670Seric 
13923149Seric 	return (a);
13933149Seric }
13943188Seric /*
13954228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
13964228Seric **
13974228Seric **	Parameters:
13984228Seric **		pvp -- parameter vector to rebuild.
139958814Seric **		evp -- last parameter to include.  Can be NULL to
140058814Seric **			use entire pvp.
14014228Seric **		buf -- buffer to build the string into.
14024228Seric **		sz -- size of buf.
140358082Seric **		spacesub -- the space separator character; if null,
140458082Seric **			use SpaceSub.
14054228Seric **
14064228Seric **	Returns:
14074228Seric **		none.
14084228Seric **
14094228Seric **	Side Effects:
14104228Seric **		Destroys buf.
14114228Seric */
14124228Seric 
141358814Seric cataddr(pvp, evp, buf, sz, spacesub)
14144228Seric 	char **pvp;
141558814Seric 	char **evp;
14164228Seric 	char *buf;
14174228Seric 	register int sz;
141858082Seric 	char spacesub;
14194228Seric {
14204228Seric 	bool oatomtok = FALSE;
142156678Seric 	bool natomtok = FALSE;
14224228Seric 	register int i;
14234228Seric 	register char *p;
14244228Seric 
142558082Seric 	if (spacesub == '\0')
142658082Seric 		spacesub = SpaceSub;
142758082Seric 
14288423Seric 	if (pvp == NULL)
14298423Seric 	{
143023109Seric 		(void) strcpy(buf, "");
14318423Seric 		return;
14328423Seric 	}
14334228Seric 	p = buf;
143411156Seric 	sz -= 2;
14354228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
14364228Seric 	{
14378078Seric 		natomtok = (toktype(**pvp) == ATM);
14384228Seric 		if (oatomtok && natomtok)
143958082Seric 			*p++ = spacesub;
14404228Seric 		(void) strcpy(p, *pvp);
14414228Seric 		oatomtok = natomtok;
14424228Seric 		p += i;
144311156Seric 		sz -= i + 1;
144458814Seric 		if (pvp++ == evp)
144558814Seric 			break;
14464228Seric 	}
14474228Seric 	*p = '\0';
14484228Seric }
14494228Seric /*
14503188Seric **  SAMEADDR -- Determine if two addresses are the same
14513188Seric **
14523188Seric **	This is not just a straight comparison -- if the mailer doesn't
14533188Seric **	care about the host we just ignore it, etc.
14543188Seric **
14553188Seric **	Parameters:
14563188Seric **		a, b -- pointers to the internal forms to compare.
14573188Seric **
14583188Seric **	Returns:
14593188Seric **		TRUE -- they represent the same mailbox.
14603188Seric **		FALSE -- they don't.
14613188Seric **
14623188Seric **	Side Effects:
14633188Seric **		none.
14643188Seric */
14653188Seric 
14663188Seric bool
14679374Seric sameaddr(a, b)
14683188Seric 	register ADDRESS *a;
14693188Seric 	register ADDRESS *b;
14703188Seric {
14713188Seric 	/* if they don't have the same mailer, forget it */
14723188Seric 	if (a->q_mailer != b->q_mailer)
14733188Seric 		return (FALSE);
14743188Seric 
14753188Seric 	/* if the user isn't the same, we can drop out */
147656678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
14773188Seric 		return (FALSE);
14783188Seric 
147958438Seric 	/* if we have good uids for both but the differ, these are different */
148058438Seric 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
148158438Seric 		return (FALSE);
148258438Seric 
148358509Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
148458509Seric 	if (a->q_host == b->q_host)
148558509Seric 	{
148658509Seric 		/* probably both null pointers */
14873188Seric 		return (TRUE);
148858509Seric 	}
14893188Seric 	if (a->q_host == NULL || b->q_host == NULL)
149058509Seric 	{
149158509Seric 		/* only one is a null pointer */
14923188Seric 		return (FALSE);
149358509Seric 	}
149456678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
14953188Seric 		return (FALSE);
14963188Seric 
14973188Seric 	return (TRUE);
14983188Seric }
14993234Seric /*
15003234Seric **  PRINTADDR -- print address (for debugging)
15013234Seric **
15023234Seric **	Parameters:
15033234Seric **		a -- the address to print
15043234Seric **		follow -- follow the q_next chain.
15053234Seric **
15063234Seric **	Returns:
15073234Seric **		none.
15083234Seric **
15093234Seric **	Side Effects:
15103234Seric **		none.
15113234Seric */
15123234Seric 
15133234Seric printaddr(a, follow)
15143234Seric 	register ADDRESS *a;
15153234Seric 	bool follow;
15163234Seric {
15175001Seric 	bool first = TRUE;
151857731Seric 	register MAILER *m;
151957731Seric 	MAILER pseudomailer;
15205001Seric 
15213234Seric 	while (a != NULL)
15223234Seric 	{
15235001Seric 		first = FALSE;
15244443Seric 		printf("%x=", a);
15254085Seric 		(void) fflush(stdout);
152657731Seric 
152757731Seric 		/* find the mailer -- carefully */
152857731Seric 		m = a->q_mailer;
152957731Seric 		if (m == NULL)
153057731Seric 		{
153157731Seric 			m = &pseudomailer;
153257731Seric 			m->m_mno = -1;
153357731Seric 			m->m_name = "NULL";
153457731Seric 		}
153557731Seric 
153658680Seric 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
153757731Seric 		       a->q_paddr, m->m_mno, m->m_name,
153863756Seric 		       a->q_host, a->q_user,
153963756Seric 		       a->q_ruser ? a->q_ruser : "<null>");
154059269Seric 		printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n",
154159269Seric 		       a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid);
154259269Seric 		printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n",
154359269Seric 		       a->q_owner == NULL ? "(none)" : a->q_owner,
154463756Seric 		       a->q_home == NULL ? "(none)" : a->q_home,
154563756Seric 		       a->q_fullname == NULL ? "(none)" : a->q_fullname);
15464996Seric 
15473234Seric 		if (!follow)
15483234Seric 			return;
15494996Seric 		a = a->q_next;
15503234Seric 	}
15515001Seric 	if (first)
15524443Seric 		printf("[NULL]\n");
15533234Seric }
15544317Seric 
15557682Seric /*
15567682Seric **  REMOTENAME -- return the name relative to the current mailer
15577682Seric **
15587682Seric **	Parameters:
15597682Seric **		name -- the name to translate.
15608069Seric **		m -- the mailer that we want to do rewriting relative
15618069Seric **			to.
156259163Seric **		flags -- fine tune operations.
156359163Seric **		pstat -- pointer to status word.
156458020Seric **		e -- the current envelope.
15657682Seric **
15667682Seric **	Returns:
15677682Seric **		the text string representing this address relative to
15687682Seric **			the receiving mailer.
15697682Seric **
15707682Seric **	Side Effects:
15717682Seric **		none.
15727682Seric **
15737682Seric **	Warnings:
15747682Seric **		The text string returned is tucked away locally;
15757682Seric **			copy it if you intend to save it.
15767682Seric */
15777682Seric 
15787682Seric char *
157959163Seric remotename(name, m, flags, pstat, e)
15807682Seric 	char *name;
158156678Seric 	struct mailer *m;
158259163Seric 	int flags;
158359163Seric 	int *pstat;
158456678Seric 	register ENVELOPE *e;
15857682Seric {
15868069Seric 	register char **pvp;
15878069Seric 	char *fancy;
158856678Seric 	char *oldg = macvalue('g', e);
158958020Seric 	int rwset;
15907682Seric 	static char buf[MAXNAME];
15917682Seric 	char lbuf[MAXNAME];
159216914Seric 	char pvpbuf[PSBUFSIZE];
159356678Seric 	extern char *crackaddr();
15947682Seric 
15957755Seric 	if (tTd(12, 1))
15967755Seric 		printf("remotename(%s)\n", name);
15977755Seric 
159810177Seric 	/* don't do anything if we are tagging it as special */
159959163Seric 	if (bitset(RF_SENDERADDR, flags))
160059163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
160159163Seric 						     : m->m_se_rwset;
160258020Seric 	else
160359163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
160459163Seric 						     : m->m_re_rwset;
160558020Seric 	if (rwset < 0)
160610177Seric 		return (name);
160710177Seric 
16087682Seric 	/*
16098181Seric 	**  Do a heuristic crack of this name to extract any comment info.
16108181Seric 	**	This will leave the name as a comment and a $g macro.
16117889Seric 	*/
16127889Seric 
161359163Seric 	if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags))
161458050Seric 		fancy = "\201g";
161510310Seric 	else
161610310Seric 		fancy = crackaddr(name);
16177889Seric 
16188181Seric 	/*
16198181Seric 	**  Turn the name into canonical form.
16208181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
16218181Seric 	**	If this only resolves to "user", and the "C" flag is
16228181Seric 	**	specified in the sending mailer, then the sender's
16238181Seric 	**	domain will be appended.
16248181Seric 	*/
16258181Seric 
162658333Seric 	pvp = prescan(name, '\0', pvpbuf, NULL);
16277889Seric 	if (pvp == NULL)
16287889Seric 		return (name);
162959163Seric 	if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
163059163Seric 		*pstat = EX_TEMPFAIL;
163159163Seric 	if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
16328181Seric 	{
16338181Seric 		/* append from domain to this address */
16348181Seric 		register char **pxp = pvp;
16358181Seric 
16369594Seric 		/* see if there is an "@domain" in the current name */
16378181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
16388181Seric 			pxp++;
16398181Seric 		if (*pxp == NULL)
16408181Seric 		{
16419594Seric 			/* no.... append the "@domain" from the sender */
164256678Seric 			register char **qxq = e->e_fromdomain;
16438181Seric 
16449594Seric 			while ((*pxp++ = *qxq++) != NULL)
16459594Seric 				continue;
164659163Seric 			if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
164759163Seric 				*pstat = EX_TEMPFAIL;
16488181Seric 		}
16498181Seric 	}
16508181Seric 
16518181Seric 	/*
16528959Seric 	**  Do more specific rewriting.
165356678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
165456678Seric 	**		a sender address or not.
16558181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
16568181Seric 	*/
16578181Seric 
165859163Seric 	if (bitset(RF_SENDERADDR, flags))
165959541Seric 	{
166059163Seric 		if (rewrite(pvp, 1, e) == EX_TEMPFAIL)
166159163Seric 			*pstat = EX_TEMPFAIL;
166259541Seric 	}
16638069Seric 	else
166459541Seric 	{
166559163Seric 		if (rewrite(pvp, 2, e) == EX_TEMPFAIL)
166659163Seric 			*pstat = EX_TEMPFAIL;
166759541Seric 	}
166858020Seric 	if (rwset > 0)
166959541Seric 	{
167059163Seric 		if (rewrite(pvp, rwset, e) == EX_TEMPFAIL)
167159163Seric 			*pstat = EX_TEMPFAIL;
167259541Seric 	}
16737682Seric 
16748181Seric 	/*
16758959Seric 	**  Do any final sanitation the address may require.
16768959Seric 	**	This will normally be used to turn internal forms
16778959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
16788959Seric 	**	may be used as a default to the above rules.
16798959Seric 	*/
16808959Seric 
168159163Seric 	if (rewrite(pvp, 4, e) == EX_TEMPFAIL)
168259163Seric 		*pstat = EX_TEMPFAIL;
16838959Seric 
16848959Seric 	/*
16858181Seric 	**  Now restore the comment information we had at the beginning.
16868181Seric 	*/
16878181Seric 
168858825Seric 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
168956678Seric 	define('g', lbuf, e);
169056678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
169156678Seric 	define('g', oldg, e);
16927682Seric 
16937682Seric 	if (tTd(12, 1))
16947755Seric 		printf("remotename => `%s'\n", buf);
16957682Seric 	return (buf);
16967682Seric }
169751317Seric /*
169856678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
169951317Seric **
170051317Seric **	Parameters:
170156678Seric **		a -- the address to map (but just the user name part).
170256678Seric **		sendq -- the sendq in which to install any replacement
170356678Seric **			addresses.
170451317Seric **
170551317Seric **	Returns:
170651317Seric **		none.
170751317Seric */
170851317Seric 
170956678Seric maplocaluser(a, sendq, e)
171056678Seric 	register ADDRESS *a;
171156678Seric 	ADDRESS **sendq;
171256678Seric 	ENVELOPE *e;
171351317Seric {
171456678Seric 	register char **pvp;
171556678Seric 	register ADDRESS *a1 = NULL;
171658333Seric 	auto char *delimptr;
171756678Seric 	char pvpbuf[PSBUFSIZE];
171851317Seric 
171956678Seric 	if (tTd(29, 1))
172056678Seric 	{
172156678Seric 		printf("maplocaluser: ");
172256678Seric 		printaddr(a, FALSE);
172356678Seric 	}
172458333Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
172556678Seric 	if (pvp == NULL)
172656678Seric 		return;
172751317Seric 
172859084Seric 	(void) rewrite(pvp, 5, e);
172958050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
173056678Seric 		return;
173151317Seric 
173256678Seric 	/* if non-null, mailer destination specified -- has it changed? */
173364284Seric 	a1 = buildaddr(pvp, NULL, 0, e);
173456678Seric 	if (a1 == NULL || sameaddr(a, a1))
173556678Seric 		return;
173651317Seric 
173756678Seric 	/* mark old address as dead; insert new address */
173856678Seric 	a->q_flags |= QDONTSEND;
173957731Seric 	if (tTd(29, 5))
174057731Seric 	{
174157731Seric 		printf("maplocaluser: QDONTSEND ");
174257731Seric 		printaddr(a, FALSE);
174357731Seric 	}
174456678Seric 	a1->q_alias = a;
174564348Seric 	allocaddr(a1, RF_COPYALL, NULL);
174656678Seric 	(void) recipient(a1, sendq, e);
174751317Seric }
174858802Seric /*
174958802Seric **  DEQUOTE_INIT -- initialize dequote map
175058802Seric **
175158802Seric **	This is a no-op.
175258802Seric **
175358802Seric **	Parameters:
175458802Seric **		map -- the internal map structure.
175558802Seric **		args -- arguments.
175658802Seric **
175758802Seric **	Returns:
175858802Seric **		TRUE.
175958802Seric */
176058802Seric 
176158802Seric bool
176260219Seric dequote_init(map, args)
176358802Seric 	MAP *map;
176458802Seric 	char *args;
176558802Seric {
176658805Seric 	register char *p = args;
176758805Seric 
176858805Seric 	for (;;)
176958805Seric 	{
177058805Seric 		while (isascii(*p) && isspace(*p))
177158805Seric 			p++;
177258805Seric 		if (*p != '-')
177358805Seric 			break;
177458805Seric 		switch (*++p)
177558805Seric 		{
177658805Seric 		  case 'a':
177758805Seric 			map->map_app = ++p;
177858805Seric 			break;
177958805Seric 		}
178058805Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
178158805Seric 			p++;
178258805Seric 		if (*p != '\0')
178358805Seric 			*p = '\0';
178458805Seric 	}
178558805Seric 	if (map->map_app != NULL)
178658805Seric 		map->map_app = newstr(map->map_app);
178758805Seric 
178858802Seric 	return TRUE;
178958802Seric }
179058802Seric /*
179158802Seric **  DEQUOTE_MAP -- unquote an address
179258802Seric **
179358802Seric **	Parameters:
179458802Seric **		map -- the internal map structure (ignored).
179560089Seric **		name -- the name to dequote.
179658802Seric **		av -- arguments (ignored).
179759084Seric **		statp -- pointer to status out-parameter.
179858802Seric **
179958802Seric **	Returns:
180058802Seric **		NULL -- if there were no quotes, or if the resulting
180158802Seric **			unquoted buffer would not be acceptable to prescan.
180258802Seric **		else -- The dequoted buffer.
180358802Seric */
180458802Seric 
180558802Seric char *
180660089Seric dequote_map(map, name, av, statp)
180758802Seric 	MAP *map;
180860089Seric 	char *name;
180958802Seric 	char **av;
181059084Seric 	int *statp;
181158802Seric {
181258802Seric 	register char *p;
181358802Seric 	register char *q;
181458802Seric 	register char c;
181558802Seric 	int anglecnt;
181658802Seric 	int cmntcnt;
181758802Seric 	int quotecnt;
181859089Seric 	int spacecnt;
181958802Seric 	bool quotemode;
182058802Seric 	bool bslashmode;
182158802Seric 
182258802Seric 	anglecnt = 0;
182358802Seric 	cmntcnt = 0;
182458802Seric 	quotecnt = 0;
182559089Seric 	spacecnt = 0;
182658802Seric 	quotemode = FALSE;
182758802Seric 	bslashmode = FALSE;
182858802Seric 
182960089Seric 	for (p = q = name; (c = *p++) != '\0'; )
183058802Seric 	{
183158802Seric 		if (bslashmode)
183258802Seric 		{
183358802Seric 			bslashmode = FALSE;
183458802Seric 			*q++ = c;
183558802Seric 			continue;
183658802Seric 		}
183758802Seric 
183858802Seric 		switch (c)
183958802Seric 		{
184058802Seric 		  case '\\':
184158802Seric 			bslashmode = TRUE;
184258802Seric 			break;
184358802Seric 
184458802Seric 		  case '(':
184558802Seric 			cmntcnt++;
184658802Seric 			break;
184758802Seric 
184858802Seric 		  case ')':
184958802Seric 			if (cmntcnt-- <= 0)
185058802Seric 				return NULL;
185158802Seric 			break;
185259089Seric 
185359089Seric 		  case ' ':
185459089Seric 			spacecnt++;
185559089Seric 			break;
185658802Seric 		}
185758802Seric 
185858802Seric 		if (cmntcnt > 0)
185958802Seric 		{
186058802Seric 			*q++ = c;
186158802Seric 			continue;
186258802Seric 		}
186358802Seric 
186458802Seric 		switch (c)
186558802Seric 		{
186658802Seric 		  case '"':
186758802Seric 			quotemode = !quotemode;
186858802Seric 			quotecnt++;
186958802Seric 			continue;
187058802Seric 
187158802Seric 		  case '<':
187258802Seric 			anglecnt++;
187358802Seric 			break;
187458802Seric 
187558802Seric 		  case '>':
187658802Seric 			if (anglecnt-- <= 0)
187758802Seric 				return NULL;
187858802Seric 			break;
187958802Seric 		}
188058802Seric 		*q++ = c;
188158802Seric 	}
188258802Seric 
188358802Seric 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
188459089Seric 	    quotemode || quotecnt <= 0 || spacecnt != 0)
188558802Seric 		return NULL;
188658802Seric 	*q++ = '\0';
188760089Seric 	return name;
188858802Seric }
1889