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*64726Seric static char sccsid[] = "@(#)parseaddr.c	8.13 (Berkeley) 10/17/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 
88*64726Seric 	if (invalidaddr(addr, delim == '\0' ? NULL : *delimptr))
89*64726Seric 	{
90*64726Seric 		if (tTd(20, 1))
91*64726Seric 			printf("parseaddr-->bad address\n");
92*64726Seric 		return NULL;
93*64726Seric 	}
94*64726Seric 
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
181*64726Seric invalidaddr(addr, delimptr)
18257388Seric 	register char *addr;
183*64726Seric 	char *delimptr;
18457388Seric {
185*64726Seric 	char savedelim;
186*64726Seric 
187*64726Seric 	if (delimptr != NULL)
188*64726Seric 		savedelim = *delimptr;
189*64726Seric #if 0
190*64726Seric 	/* for testing.... */
191*64726Seric 	if (strcmp(addr, "INvalidADDR") == 0)
19257388Seric 	{
193*64726Seric 		usrerr("553 INvalid ADDRess");
194*64726Seric 		if (delimptr != NULL)
195*64726Seric 			*delimptr = savedelim;
19657388Seric 		return TRUE;
19757388Seric 	}
198*64726Seric #endif
199*64726Seric 	for (; *addr != '\0'; addr++)
200*64726Seric 	{
201*64726Seric 		if ((*addr & 0340) == 0200)
202*64726Seric 			break;
203*64726Seric 	}
204*64726Seric 	if (delimptr != NULL)
205*64726Seric 		*delimptr = savedelim;
206*64726Seric 	if (*addr == '\0')
207*64726Seric 		return FALSE;
208*64726Seric 	setstat(EX_USAGE);
209*64726Seric 	usrerr("553 Address contained invalid control characters");
210*64726Seric 	return TRUE;
21157388Seric }
21257388Seric /*
21356678Seric **  ALLOCADDR -- do local allocations of address on demand.
21456678Seric **
21556678Seric **	Also lowercases the host name if requested.
21656678Seric **
21756678Seric **	Parameters:
21856678Seric **		a -- the address to reallocate.
21964284Seric **		flags -- the copy flag (see RF_ definitions in sendmail.h
22064284Seric **			for a description).
22156678Seric **		paddr -- the printname of the address.
22256678Seric **
22356678Seric **	Returns:
22456678Seric **		none.
22556678Seric **
22656678Seric **	Side Effects:
22756678Seric **		Copies portions of a into local buffers as requested.
22856678Seric */
22956678Seric 
23064348Seric allocaddr(a, flags, paddr)
23156678Seric 	register ADDRESS *a;
23264284Seric 	int flags;
23356678Seric 	char *paddr;
23456678Seric {
23558673Seric 	if (tTd(24, 4))
23664284Seric 		printf("allocaddr(flags=%o, paddr=%s)\n", flags, paddr);
23758673Seric 
23864348Seric 	a->q_paddr = paddr;
2398078Seric 
24024944Seric 	if (a->q_user == NULL)
24124944Seric 		a->q_user = "";
24224944Seric 	if (a->q_host == NULL)
24324944Seric 		a->q_host = "";
24424944Seric 
24564284Seric 	if (bitset(RF_COPYPARSE, flags))
246297Seric 	{
24724944Seric 		a->q_host = newstr(a->q_host);
2483149Seric 		if (a->q_user != a->q_paddr)
2493149Seric 			a->q_user = newstr(a->q_user);
250297Seric 	}
251297Seric 
25256678Seric 	if (a->q_paddr == NULL)
25356678Seric 		a->q_paddr = a->q_user;
254297Seric }
255297Seric /*
256297Seric **  PRESCAN -- Prescan name and make it canonical
257297Seric **
2589374Seric **	Scans a name and turns it into a set of tokens.  This process
2599374Seric **	deletes blanks and comments (in parentheses).
260297Seric **
261297Seric **	This routine knows about quoted strings and angle brackets.
262297Seric **
263297Seric **	There are certain subtleties to this routine.  The one that
264297Seric **	comes to mind now is that backslashes on the ends of names
265297Seric **	are silently stripped off; this is intentional.  The problem
266297Seric **	is that some versions of sndmsg (like at LBL) set the kill
267297Seric **	character to something other than @ when reading addresses;
268297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
269297Seric **	berknet mailer.
270297Seric **
271297Seric **	Parameters:
272297Seric **		addr -- the name to chomp.
273297Seric **		delim -- the delimiter for the address, normally
274297Seric **			'\0' or ','; \0 is accepted in any case.
27515284Seric **			If '\t' then we are reading the .cf file.
27616914Seric **		pvpbuf -- place to put the saved text -- note that
27716914Seric **			the pointers are static.
27858333Seric **		delimptr -- if non-NULL, set to the location of the
27958333Seric **			terminating delimiter.
280297Seric **
281297Seric **	Returns:
2823149Seric **		A pointer to a vector of tokens.
283297Seric **		NULL on error.
284297Seric */
285297Seric 
2868078Seric /* states and character types */
2878078Seric # define OPR		0	/* operator */
2888078Seric # define ATM		1	/* atom */
2898078Seric # define QST		2	/* in quoted string */
2908078Seric # define SPC		3	/* chewing up spaces */
2918078Seric # define ONE		4	/* pick up one character */
2923149Seric 
2938078Seric # define NSTATES	5	/* number of states */
2948078Seric # define TYPE		017	/* mask to select state type */
2958078Seric 
2968078Seric /* meta bits for table */
2978078Seric # define M		020	/* meta character; don't pass through */
2988078Seric # define B		040	/* cause a break */
2998078Seric # define MB		M|B	/* meta-break */
3008078Seric 
3018078Seric static short StateTab[NSTATES][NSTATES] =
3028078Seric {
3038087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
3049051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
3059051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
3069051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
3078078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
3088078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
3098078Seric };
3108078Seric 
3118078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
3128078Seric 
3133149Seric char **
31458333Seric prescan(addr, delim, pvpbuf, delimptr)
315297Seric 	char *addr;
316297Seric 	char delim;
31716914Seric 	char pvpbuf[];
31858333Seric 	char **delimptr;
319297Seric {
320297Seric 	register char *p;
3218078Seric 	register char *q;
3229346Seric 	register int c;
3233149Seric 	char **avp;
324297Seric 	bool bslashmode;
325297Seric 	int cmntcnt;
3268423Seric 	int anglecnt;
3273149Seric 	char *tok;
3288078Seric 	int state;
3298078Seric 	int newstate;
33059747Seric 	char *saveto = CurEnv->e_to;
3318078Seric 	static char *av[MAXATOM+1];
33256678Seric 	extern int errno;
333297Seric 
33415253Seric 	/* make sure error messages don't have garbage on them */
33515253Seric 	errno = 0;
33615253Seric 
33716914Seric 	q = pvpbuf;
3383149Seric 	bslashmode = FALSE;
3397800Seric 	cmntcnt = 0;
3408423Seric 	anglecnt = 0;
3413149Seric 	avp = av;
34256678Seric 	state = ATM;
3438078Seric 	c = NOCHAR;
3448078Seric 	p = addr;
34559747Seric 	CurEnv->e_to = p;
34656764Seric 	if (tTd(22, 11))
347297Seric 	{
3488078Seric 		printf("prescan: ");
3498078Seric 		xputs(p);
35023109Seric 		(void) putchar('\n');
3518078Seric 	}
3528078Seric 
3538078Seric 	do
3548078Seric 	{
3553149Seric 		/* read a token */
3563149Seric 		tok = q;
3578078Seric 		for (;;)
358297Seric 		{
3598078Seric 			/* store away any old lookahead character */
36059277Seric 			if (c != NOCHAR && !bslashmode)
3618078Seric 			{
36215284Seric 				/* see if there is room */
36316914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3648078Seric 				{
36558151Seric 					usrerr("553 Address too long");
36658333Seric 					if (delimptr != NULL)
36758333Seric 						*delimptr = p;
36859747Seric 					CurEnv->e_to = saveto;
3698078Seric 					return (NULL);
3708078Seric 				}
37115284Seric 
37215284Seric 				/* squirrel it away */
3738078Seric 				*q++ = c;
3748078Seric 			}
3758078Seric 
3768078Seric 			/* read a new input character */
3778078Seric 			c = *p++;
37857631Seric 			if (c == '\0')
37956764Seric 			{
38056764Seric 				/* diagnose and patch up bad syntax */
38156764Seric 				if (state == QST)
38256764Seric 				{
38363851Seric 					usrerr("653 Unbalanced '\"' (fixed)");
38456764Seric 					c = '"';
38556764Seric 				}
38656764Seric 				else if (cmntcnt > 0)
38756764Seric 				{
38863851Seric 					usrerr("653 Unbalanced '(' (fixed)");
38956764Seric 					c = ')';
39056764Seric 				}
39156764Seric 				else if (anglecnt > 0)
39256764Seric 				{
39356764Seric 					c = '>';
39463851Seric 					usrerr("653 Unbalanced '<' (fixed)");
39556764Seric 				}
39656764Seric 				else
39756764Seric 					break;
39815284Seric 
39956764Seric 				p--;
40056764Seric 			}
40157631Seric 			else if (c == delim && anglecnt <= 0 &&
40257631Seric 					cmntcnt <= 0 && state != QST)
40357631Seric 				break;
40456764Seric 
4058078Seric 			if (tTd(22, 101))
4068078Seric 				printf("c=%c, s=%d; ", c, state);
4078078Seric 
4083149Seric 			/* chew up special characters */
4093149Seric 			*q = '\0';
4103149Seric 			if (bslashmode)
4113149Seric 			{
41259105Seric 				bslashmode = FALSE;
41359105Seric 
41424944Seric 				/* kludge \! for naive users */
41558061Seric 				if (cmntcnt > 0)
41659105Seric 				{
41758061Seric 					c = NOCHAR;
41859105Seric 					continue;
41959105Seric 				}
42059105Seric 				else if (c != '!' || state == QST)
42159105Seric 				{
42256678Seric 					*q++ = '\\';
42359105Seric 					continue;
42459105Seric 				}
4253149Seric 			}
42656678Seric 
42756678Seric 			if (c == '\\')
4283149Seric 			{
4293149Seric 				bslashmode = TRUE;
4303149Seric 			}
43156678Seric 			else if (state == QST)
4328514Seric 			{
4338514Seric 				/* do nothing, just avoid next clauses */
4348514Seric 			}
4358078Seric 			else if (c == '(')
4364100Seric 			{
4378078Seric 				cmntcnt++;
4388078Seric 				c = NOCHAR;
4394100Seric 			}
4408078Seric 			else if (c == ')')
4413149Seric 			{
4428078Seric 				if (cmntcnt <= 0)
4433149Seric 				{
44463851Seric 					usrerr("653 Unbalanced ')' (fixed)");
44563844Seric 					c = NOCHAR;
4463149Seric 				}
4478078Seric 				else
4488078Seric 					cmntcnt--;
4498078Seric 			}
4508078Seric 			else if (cmntcnt > 0)
4518078Seric 				c = NOCHAR;
4528423Seric 			else if (c == '<')
4538423Seric 				anglecnt++;
4548423Seric 			else if (c == '>')
4558423Seric 			{
4568423Seric 				if (anglecnt <= 0)
4578423Seric 				{
45863851Seric 					usrerr("653 Unbalanced '>' (fixed)");
45963844Seric 					c = NOCHAR;
4608423Seric 				}
46163844Seric 				else
46263844Seric 					anglecnt--;
4638423Seric 			}
46458050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
46511423Seric 				c = ' ';
4663149Seric 
4678078Seric 			if (c == NOCHAR)
4688078Seric 				continue;
4693149Seric 
4708078Seric 			/* see if this is end of input */
47111405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4723149Seric 				break;
4733149Seric 
4748078Seric 			newstate = StateTab[state][toktype(c)];
4758078Seric 			if (tTd(22, 101))
4768078Seric 				printf("ns=%02o\n", newstate);
4778078Seric 			state = newstate & TYPE;
4788078Seric 			if (bitset(M, newstate))
4798078Seric 				c = NOCHAR;
4808078Seric 			if (bitset(B, newstate))
4814228Seric 				break;
482297Seric 		}
4833149Seric 
4843149Seric 		/* new token */
4858078Seric 		if (tok != q)
4861378Seric 		{
4878078Seric 			*q++ = '\0';
4888078Seric 			if (tTd(22, 36))
489297Seric 			{
4908078Seric 				printf("tok=");
4918078Seric 				xputs(tok);
49223109Seric 				(void) putchar('\n');
493297Seric 			}
4948078Seric 			if (avp >= &av[MAXATOM])
495297Seric 			{
49658151Seric 				syserr("553 prescan: too many tokens");
49758333Seric 				if (delimptr != NULL)
49858333Seric 					*delimptr = p;
49959747Seric 				CurEnv->e_to = saveto;
5008078Seric 				return (NULL);
501297Seric 			}
5028078Seric 			*avp++ = tok;
503297Seric 		}
5048423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
5053149Seric 	*avp = NULL;
50658333Seric 	p--;
50758333Seric 	if (delimptr != NULL)
50858333Seric 		*delimptr = p;
50956764Seric 	if (tTd(22, 12))
51056764Seric 	{
51156764Seric 		printf("prescan==>");
51256764Seric 		printav(av);
51356764Seric 	}
51459747Seric 	CurEnv->e_to = saveto;
51558546Seric 	if (av[0] == NULL)
51658546Seric 		return (NULL);
51758403Seric 	return (av);
5183149Seric }
5193149Seric /*
5203149Seric **  TOKTYPE -- return token type
5213149Seric **
5223149Seric **	Parameters:
5233149Seric **		c -- the character in question.
5243149Seric **
5253149Seric **	Returns:
5263149Seric **		Its type.
5273149Seric **
5283149Seric **	Side Effects:
5293149Seric **		none.
5303149Seric */
531297Seric 
5323149Seric toktype(c)
53358050Seric 	register int c;
5343149Seric {
5353380Seric 	static char buf[50];
5363382Seric 	static bool firstime = TRUE;
5373380Seric 
5383382Seric 	if (firstime)
5393380Seric 	{
5403382Seric 		firstime = FALSE;
54158050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5427005Seric 		(void) strcat(buf, DELIMCHARS);
5433380Seric 	}
54458050Seric 	c &= 0377;
54556327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5468078Seric 		return (ONE);
54759027Seric 	if (c == MACRODEXPAND)
54859027Seric 		return (ONE);
5498078Seric 	if (c == '"')
5508078Seric 		return (QST);
55158050Seric 	if ((c & 0340) == 0200)
55258050Seric 		return (OPR);
5534100Seric 	if (!isascii(c))
5548078Seric 		return (ATM);
5558078Seric 	if (isspace(c) || c == ')')
5568078Seric 		return (SPC);
55758050Seric 	if (strchr(buf, c) != NULL)
5588078Seric 		return (OPR);
5598078Seric 	return (ATM);
5603149Seric }
5613149Seric /*
5623149Seric **  REWRITE -- apply rewrite rules to token vector.
5633149Seric **
5644476Seric **	This routine is an ordered production system.  Each rewrite
5654476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5664476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5674476Seric **
5684476Seric **	For each rewrite rule, 'avp' points the address vector we
5694476Seric **	are trying to match against, and 'pvp' points to the pattern.
5708058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5719585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5729585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5734476Seric **
5744476Seric **	When a match between avp & pvp does not match, we try to
5759585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5764476Seric **	we must also back out the match in mvp.  If we reach a
5778058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5788058Seric **	over again.
5794476Seric **
5804476Seric **	When we finally match, we rewrite the address vector
5814476Seric **	and try over again.
5824476Seric **
5833149Seric **	Parameters:
5843149Seric **		pvp -- pointer to token vector.
58559027Seric **		ruleset -- the ruleset to use for rewriting.
58659027Seric **		e -- the current envelope.
5873149Seric **
5883149Seric **	Returns:
58959084Seric **		A status code.  If EX_TEMPFAIL, higher level code should
59059084Seric **			attempt recovery.
5913149Seric **
5923149Seric **	Side Effects:
5933149Seric **		pvp is modified.
5943149Seric */
5952091Seric 
5963149Seric struct match
5973149Seric {
5984468Seric 	char	**first;	/* first token matched */
5994468Seric 	char	**last;		/* last token matched */
60058825Seric 	char	**pattern;	/* pointer to pattern */
6013149Seric };
6023149Seric 
6034468Seric # define MAXMATCH	9	/* max params per rewrite */
6043149Seric 
6053149Seric 
60659084Seric int
60759027Seric rewrite(pvp, ruleset, e)
6083149Seric 	char **pvp;
6094070Seric 	int ruleset;
61059027Seric 	register ENVELOPE *e;
6113149Seric {
6123149Seric 	register char *ap;		/* address pointer */
6133149Seric 	register char *rp;		/* rewrite pointer */
6143149Seric 	register char **avp;		/* address vector pointer */
6153149Seric 	register char **rvp;		/* rewrite vector pointer */
6168058Seric 	register struct match *mlp;	/* cur ptr into mlist */
6178058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
61858866Seric 	int ruleno;			/* current rule number */
61959084Seric 	int rstat = EX_OK;		/* return status */
62056678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
6213149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
6223149Seric 
6239279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
6243149Seric 	{
6258959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
62656678Seric 		printav(pvp);
6273149Seric 	}
62856678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
62956326Seric 	{
63058151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
63159084Seric 		return EX_CONFIG;
63256326Seric 	}
63356678Seric 	if (pvp == NULL)
63459084Seric 		return EX_USAGE;
63556326Seric 
6363149Seric 	/*
63756678Seric 	**  Run through the list of rewrite rules, applying
63856678Seric 	**	any that match.
6393149Seric 	*/
6403149Seric 
64158866Seric 	ruleno = 1;
6424070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6433149Seric 	{
64456678Seric 		int loopcount = 0;
64556678Seric 
6467675Seric 		if (tTd(21, 12))
647297Seric 		{
6488069Seric 			printf("-----trying rule:");
64956678Seric 			printav(rwr->r_lhs);
6503149Seric 		}
6513149Seric 
6523149Seric 		/* try to match on this rule */
6534468Seric 		mlp = mlist;
6548058Seric 		rvp = rwr->r_lhs;
6558058Seric 		avp = pvp;
65658866Seric 		if (++loopcount > 100)
6573149Seric 		{
65858866Seric 			syserr("554 Infinite loop in ruleset %d, rule %d",
65958866Seric 				ruleset, ruleno);
66058866Seric 			if (tTd(21, 1))
66152637Seric 			{
66256678Seric 				printf("workspace: ");
66356678Seric 				printav(pvp);
66452637Seric 			}
66558866Seric 			break;
66658866Seric 		}
66758866Seric 
66858866Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
66958866Seric 		{
6703149Seric 			rp = *rvp;
6718058Seric 			if (tTd(21, 35))
6728058Seric 			{
67358825Seric 				printf("ADVANCE rp=");
67457531Seric 				xputs(rp);
67557532Seric 				printf(", ap=");
6768058Seric 				xputs(ap);
6778069Seric 				printf("\n");
6788058Seric 			}
67956678Seric 			if (rp == NULL)
68056326Seric 			{
6813149Seric 				/* end-of-pattern before end-of-address */
6828058Seric 				goto backup;
68356678Seric 			}
68458173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
68558827Seric 			    (*rp & 0377) != MATCHZERO)
68656326Seric 			{
68758825Seric 				/* end-of-input with patterns left */
68858825Seric 				goto backup;
689297Seric 			}
69056326Seric 
69158050Seric 			switch (*rp & 0377)
6928058Seric 			{
69356678Seric 				register STAB *s;
69458814Seric 				char buf[MAXLINE];
69556326Seric 
69656678Seric 			  case MATCHCLASS:
69758825Seric 				/* match any phrase in a class */
69858825Seric 				mlp->pattern = rvp;
69958814Seric 				mlp->first = avp;
70058814Seric 	extendclass:
70158825Seric 				ap = *avp;
70258825Seric 				if (ap == NULL)
70358814Seric 					goto backup;
70458814Seric 				mlp->last = avp++;
70558814Seric 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
70658814Seric 				s = stab(buf, ST_CLASS, ST_FIND);
70756678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
70856326Seric 				{
70958825Seric 					if (tTd(21, 36))
71058825Seric 					{
71158825Seric 						printf("EXTEND  rp=");
71258825Seric 						xputs(rp);
71358825Seric 						printf(", ap=");
71458825Seric 						xputs(ap);
71558825Seric 						printf("\n");
71658825Seric 					}
71758825Seric 					goto extendclass;
71856326Seric 				}
71958825Seric 				if (tTd(21, 36))
72058825Seric 					printf("CLMATCH\n");
72158814Seric 				mlp++;
72258814Seric 				break;
7234060Seric 
72458825Seric 			  case MATCHNCLASS:
72558825Seric 				/* match any token not in a class */
72658825Seric 				s = stab(ap, ST_CLASS, ST_FIND);
72758825Seric 				if (s != NULL && bitnset(rp[1], s->s_class))
72858825Seric 					goto backup;
72958825Seric 
73058825Seric 				/* fall through */
73158825Seric 
73256678Seric 			  case MATCHONE:
73356678Seric 			  case MATCHANY:
73456678Seric 				/* match exactly one token */
73558825Seric 				mlp->pattern = rvp;
73656678Seric 				mlp->first = avp;
73756678Seric 				mlp->last = avp++;
7388058Seric 				mlp++;
73956678Seric 				break;
7408058Seric 
74156678Seric 			  case MATCHZANY:
74256678Seric 				/* match zero or more tokens */
74358825Seric 				mlp->pattern = rvp;
74456678Seric 				mlp->first = avp;
74556678Seric 				mlp->last = avp - 1;
74656678Seric 				mlp++;
74756678Seric 				break;
74856326Seric 
74958827Seric 			  case MATCHZERO:
75058173Seric 				/* match zero tokens */
75158173Seric 				break;
75258173Seric 
75359027Seric 			  case MACRODEXPAND:
75459027Seric 				/*
75559027Seric 				**  Match against run-time macro.
75659027Seric 				**  This algorithm is broken for the
75759027Seric 				**  general case (no recursive macros,
75859027Seric 				**  improper tokenization) but should
75959027Seric 				**  work for the usual cases.
76059027Seric 				*/
76159027Seric 
76259027Seric 				ap = macvalue(rp[1], e);
76359027Seric 				mlp->first = avp;
76459027Seric 				if (tTd(21, 2))
76559027Seric 					printf("rewrite: LHS $&%c => \"%s\"\n",
76659027Seric 						rp[1],
76759027Seric 						ap == NULL ? "(NULL)" : ap);
76859027Seric 
76959027Seric 				if (ap == NULL)
77059027Seric 					break;
77160502Seric 				while (*ap != '\0')
77259027Seric 				{
77359027Seric 					if (*avp == NULL ||
77459027Seric 					    strncasecmp(ap, *avp, strlen(*avp)) != 0)
77559027Seric 					{
77659027Seric 						/* no match */
77759027Seric 						avp = mlp->first;
77859027Seric 						goto backup;
77959027Seric 					}
78059027Seric 					ap += strlen(*avp++);
78159027Seric 				}
78259027Seric 
78359027Seric 				/* match */
78459027Seric 				break;
78559027Seric 
78656678Seric 			  default:
78756678Seric 				/* must have exact match */
78856678Seric 				if (strcasecmp(rp, ap))
7898058Seric 					goto backup;
7904468Seric 				avp++;
79156678Seric 				break;
7923149Seric 			}
7933149Seric 
79456678Seric 			/* successful match on this token */
7953149Seric 			rvp++;
7963149Seric 			continue;
7973149Seric 
79858825Seric 	  backup:
79956678Seric 			/* match failed -- back up */
80058825Seric 			while (--mlp >= mlist)
8013149Seric 			{
80258825Seric 				rvp = mlp->pattern;
80356678Seric 				rp = *rvp;
80458825Seric 				avp = mlp->last + 1;
80558825Seric 				ap = *avp;
80658825Seric 
80758825Seric 				if (tTd(21, 36))
80858825Seric 				{
80958825Seric 					printf("BACKUP  rp=");
81058825Seric 					xputs(rp);
81158825Seric 					printf(", ap=");
81258825Seric 					xputs(ap);
81358825Seric 					printf("\n");
81458825Seric 				}
81558825Seric 
81658825Seric 				if (ap == NULL)
81758825Seric 				{
81858825Seric 					/* run off the end -- back up again */
81958825Seric 					continue;
82058825Seric 				}
82158050Seric 				if ((*rp & 0377) == MATCHANY ||
82258050Seric 				    (*rp & 0377) == MATCHZANY)
8234468Seric 				{
82456678Seric 					/* extend binding and continue */
82558825Seric 					mlp->last = avp++;
82656678Seric 					rvp++;
82758825Seric 					mlp++;
82856678Seric 					break;
8294468Seric 				}
83058825Seric 				if ((*rp & 0377) == MATCHCLASS)
83156678Seric 				{
83258814Seric 					/* extend binding and try again */
83363397Seric 					mlp->last = avp;
83458814Seric 					goto extendclass;
83558814Seric 				}
8363149Seric 			}
8373149Seric 
83858825Seric 			if (mlp < mlist)
83956678Seric 			{
84056678Seric 				/* total failure to match */
84156326Seric 				break;
8423149Seric 			}
843297Seric 		}
8443149Seric 
8453149Seric 		/*
84656678Seric 		**  See if we successfully matched
8473149Seric 		*/
8483149Seric 
84958827Seric 		if (mlp < mlist || *rvp != NULL)
8503149Seric 		{
8519374Seric 			if (tTd(21, 10))
8529374Seric 				printf("----- rule fails\n");
8539374Seric 			rwr = rwr->r_next;
85458866Seric 			ruleno++;
8559374Seric 			continue;
8569374Seric 		}
8573149Seric 
8589374Seric 		rvp = rwr->r_rhs;
8599374Seric 		if (tTd(21, 12))
8609374Seric 		{
8619374Seric 			printf("-----rule matches:");
86256678Seric 			printav(rvp);
8639374Seric 		}
8649374Seric 
8659374Seric 		rp = *rvp;
86658050Seric 		if ((*rp & 0377) == CANONUSER)
8679374Seric 		{
8689374Seric 			rvp++;
8699374Seric 			rwr = rwr->r_next;
87058866Seric 			ruleno++;
8719374Seric 		}
87258050Seric 		else if ((*rp & 0377) == CANONHOST)
8739374Seric 		{
8749374Seric 			rvp++;
8759374Seric 			rwr = NULL;
8769374Seric 		}
87758050Seric 		else if ((*rp & 0377) == CANONNET)
8789374Seric 			rwr = NULL;
8799374Seric 
8809374Seric 		/* substitute */
8819374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
8829374Seric 		{
8839374Seric 			register struct match *m;
8849374Seric 			register char **pp;
8859374Seric 
8868058Seric 			rp = *rvp;
88758050Seric 			if ((*rp & 0377) == MATCHREPL)
8888058Seric 			{
88916914Seric 				/* substitute from LHS */
89016914Seric 				m = &mlist[rp[1] - '1'];
89156678Seric 				if (m < mlist || m >= mlp)
8929374Seric 				{
89358151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
89456326Seric 						ruleset, rp[1]);
89559084Seric 					return EX_CONFIG;
8969374Seric 				}
89716914Seric 				if (tTd(21, 15))
89816914Seric 				{
89916914Seric 					printf("$%c:", rp[1]);
90016914Seric 					pp = m->first;
90156678Seric 					while (pp <= m->last)
90216914Seric 					{
90316914Seric 						printf(" %x=\"", *pp);
90416914Seric 						(void) fflush(stdout);
90516914Seric 						printf("%s\"", *pp++);
90616914Seric 					}
90716914Seric 					printf("\n");
90816914Seric 				}
9099374Seric 				pp = m->first;
91056678Seric 				while (pp <= m->last)
9113149Seric 				{
91216914Seric 					if (avp >= &npvp[MAXATOM])
91356678Seric 					{
91458151Seric 						syserr("554 rewrite: expansion too long");
91559084Seric 						return EX_DATAERR;
91656678Seric 					}
91716914Seric 					*avp++ = *pp++;
9183149Seric 				}
9193149Seric 			}
92016914Seric 			else
9218226Seric 			{
92216914Seric 				/* vanilla replacement */
9239374Seric 				if (avp >= &npvp[MAXATOM])
92416889Seric 				{
92556678Seric 	toolong:
92658151Seric 					syserr("554 rewrite: expansion too long");
92759084Seric 					return EX_DATAERR;
92816889Seric 				}
92959027Seric 				if ((*rp & 0377) != MACRODEXPAND)
93059027Seric 					*avp++ = rp;
93159027Seric 				else
93259027Seric 				{
93359027Seric 					*avp = macvalue(rp[1], e);
93459027Seric 					if (tTd(21, 2))
93559027Seric 						printf("rewrite: RHS $&%c => \"%s\"\n",
93659027Seric 							rp[1],
93759027Seric 							*avp == NULL ? "(NULL)" : *avp);
93859027Seric 					if (*avp != NULL)
93959027Seric 						avp++;
94059027Seric 				}
9418226Seric 			}
9429374Seric 		}
9439374Seric 		*avp++ = NULL;
94416914Seric 
94516914Seric 		/*
94656678Seric 		**  Check for any hostname/keyword lookups.
94716914Seric 		*/
94816914Seric 
94916914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
95016914Seric 		{
95156678Seric 			char **hbrvp;
95216914Seric 			char **xpvp;
95316914Seric 			int trsize;
95456678Seric 			char *replac;
95556678Seric 			int endtoken;
95656678Seric 			STAB *map;
95756678Seric 			char *mapname;
95856678Seric 			char **key_rvp;
95956678Seric 			char **arg_rvp;
96056678Seric 			char **default_rvp;
96156678Seric 			char buf[MAXNAME + 1];
96216914Seric 			char *pvpb1[MAXATOM + 1];
96356823Seric 			char *argvect[10];
96417174Seric 			char pvpbuf[PSBUFSIZE];
96564404Seric 			char *nullpvp[1];
96616914Seric 
96758050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
96858050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
96916914Seric 				continue;
97016914Seric 
97116914Seric 			/*
97256678Seric 			**  Got a hostname/keyword lookup.
97316914Seric 			**
97416914Seric 			**	This could be optimized fairly easily.
97516914Seric 			*/
97616914Seric 
97716914Seric 			hbrvp = rvp;
97858050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
97956327Seric 			{
98056678Seric 				endtoken = HOSTEND;
98156678Seric 				mapname = "host";
98256327Seric 			}
98356326Seric 			else
98456327Seric 			{
98556678Seric 				endtoken = LOOKUPEND;
98656678Seric 				mapname = *++rvp;
98756327Seric 			}
98856678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
98956678Seric 			if (map == NULL)
99058151Seric 				syserr("554 rewrite: map %s not found", mapname);
99156678Seric 
99256678Seric 			/* extract the match part */
99356678Seric 			key_rvp = ++rvp;
99456823Seric 			default_rvp = NULL;
99556823Seric 			arg_rvp = argvect;
99656823Seric 			xpvp = NULL;
99756823Seric 			replac = pvpbuf;
99858050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
99953654Seric 			{
100058050Seric 				int nodetype = **rvp & 0377;
100156823Seric 
100256823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
100356678Seric 				{
100456823Seric 					rvp++;
100556823Seric 					continue;
100656823Seric 				}
100756823Seric 
100856823Seric 				*rvp++ = NULL;
100956823Seric 
101056823Seric 				if (xpvp != NULL)
101156823Seric 				{
101258814Seric 					cataddr(xpvp, NULL, replac,
101358082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
101458082Seric 						'\0');
101556823Seric 					*++arg_rvp = replac;
101656823Seric 					replac += strlen(replac) + 1;
101756823Seric 					xpvp = NULL;
101856823Seric 				}
101956823Seric 				switch (nodetype)
102056823Seric 				{
102156678Seric 				  case CANONHOST:
102256823Seric 					xpvp = rvp;
102356678Seric 					break;
102456678Seric 
102556678Seric 				  case CANONUSER:
102656678Seric 					default_rvp = rvp;
102756678Seric 					break;
102856678Seric 				}
102953654Seric 			}
103016914Seric 			if (*rvp != NULL)
103116914Seric 				*rvp++ = NULL;
103256823Seric 			if (xpvp != NULL)
103356823Seric 			{
103458814Seric 				cataddr(xpvp, NULL, replac,
103558082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
103658082Seric 					'\0');
103756823Seric 				*++arg_rvp = replac;
103856823Seric 			}
103956823Seric 			*++arg_rvp = NULL;
104016914Seric 
104116914Seric 			/* save the remainder of the input string */
104216914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
104316914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
104416914Seric 
104556678Seric 			/* look it up */
104658814Seric 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
104756823Seric 			argvect[0] = buf;
104860538Seric 			if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags))
104956836Seric 			{
105059084Seric 				auto int stat = EX_OK;
105156836Seric 
105260215Seric 				/* XXX should try to auto-open the map here */
105360215Seric 
105458796Seric 				if (tTd(60, 1))
105558796Seric 					printf("map_lookup(%s, %s) => ",
105658796Seric 						mapname, buf);
105756836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
105860089Seric 						buf, argvect, &stat);
105958796Seric 				if (tTd(60, 1))
106059084Seric 					printf("%s (%d)\n",
106159084Seric 						replac ? replac : "NOT FOUND",
106259084Seric 						stat);
106359084Seric 
106459084Seric 				/* should recover if stat == EX_TEMPFAIL */
106559084Seric 				if (stat == EX_TEMPFAIL)
106659084Seric 					rstat = stat;
106756836Seric 			}
106853654Seric 			else
106956678Seric 				replac = NULL;
107056678Seric 
107156678Seric 			/* if no replacement, use default */
107256823Seric 			if (replac == NULL && default_rvp != NULL)
107356823Seric 			{
107460089Seric 				/* create the default */
107560089Seric 				cataddr(default_rvp, NULL, buf, sizeof buf, '\0');
107656823Seric 				replac = buf;
107756823Seric 			}
107856823Seric 
107956678Seric 			if (replac == NULL)
108051317Seric 			{
108156823Seric 				xpvp = key_rvp;
108253654Seric 			}
108364404Seric 			else if (*replac == '\0')
108464404Seric 			{
108564404Seric 				/* null replacement */
108664404Seric 				nullpvp[0] = NULL;
108764404Seric 				xpvp = nullpvp;
108864404Seric 			}
108956678Seric 			else
109053654Seric 			{
109156678Seric 				/* scan the new replacement */
109258333Seric 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
109353654Seric 				if (xpvp == NULL)
109451317Seric 				{
109558403Seric 					/* prescan already printed error */
109659084Seric 					return EX_DATAERR;
109756678Seric 				}
109851317Seric 			}
109951317Seric 
110016914Seric 			/* append it to the token list */
110156678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
110256678Seric 			{
110317174Seric 				*avp++ = newstr(*xpvp);
110416920Seric 				if (avp >= &npvp[MAXATOM])
110516914Seric 					goto toolong;
110617174Seric 			}
110716914Seric 
110816914Seric 			/* restore the old trailing information */
110956678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
111016920Seric 				if (avp >= &npvp[MAXATOM])
111116914Seric 					goto toolong;
111217174Seric 
111356678Seric 			break;
111416914Seric 		}
111516914Seric 
111616914Seric 		/*
111716914Seric 		**  Check for subroutine calls.
111816914Seric 		*/
111916914Seric 
112058050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
112156678Seric 		{
112259084Seric 			int stat;
112359084Seric 
112456678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
112556678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
112656678Seric 			if (tTd(21, 3))
112756678Seric 				printf("-----callsubr %s\n", npvp[1]);
112859084Seric 			stat = rewrite(pvp, atoi(npvp[1]), e);
112959084Seric 			if (rstat == EX_OK || stat == EX_TEMPFAIL)
113059084Seric 				rstat = stat;
113163581Seric 			if ((**pvp & 0377) == CANONNET)
113263581Seric 				rwr = NULL;
113356678Seric 		}
113456678Seric 		else
113556678Seric 		{
113617348Seric 			bcopy((char *) npvp, (char *) pvp,
113716900Seric 				(int) (avp - npvp) * sizeof *avp);
113856678Seric 		}
11399374Seric 		if (tTd(21, 4))
11409374Seric 		{
11419374Seric 			printf("rewritten as:");
114256678Seric 			printav(pvp);
11439374Seric 		}
1144297Seric 	}
11458069Seric 
11469279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
11478069Seric 	{
11488959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
114956678Seric 		printav(pvp);
11508069Seric 	}
115159084Seric 
115259084Seric 	return rstat;
11533149Seric }
11543149Seric /*
11553149Seric **  BUILDADDR -- build address from token vector.
11563149Seric **
11573149Seric **	Parameters:
11583149Seric **		tv -- token vector.
11593149Seric **		a -- pointer to address descriptor to fill.
11603149Seric **			If NULL, one will be allocated.
116164284Seric **		flags -- info regarding whether this is a sender or
116264284Seric **			a recipient.
116358966Seric **		e -- the current envelope.
11643149Seric **
11653149Seric **	Returns:
11664279Seric **		NULL if there was an error.
11674279Seric **		'a' otherwise.
11683149Seric **
11693149Seric **	Side Effects:
11703149Seric **		fills in 'a'
11713149Seric */
11723149Seric 
117357249Seric struct errcodes
117457249Seric {
117557249Seric 	char	*ec_name;		/* name of error code */
117657249Seric 	int	ec_code;		/* numeric code */
117757249Seric } ErrorCodes[] =
117857249Seric {
117957249Seric 	"usage",	EX_USAGE,
118057249Seric 	"nouser",	EX_NOUSER,
118157249Seric 	"nohost",	EX_NOHOST,
118257249Seric 	"unavailable",	EX_UNAVAILABLE,
118357249Seric 	"software",	EX_SOFTWARE,
118457249Seric 	"tempfail",	EX_TEMPFAIL,
118557249Seric 	"protocol",	EX_PROTOCOL,
118657249Seric #ifdef EX_CONFIG
118757249Seric 	"config",	EX_CONFIG,
118857249Seric #endif
118957249Seric 	NULL,		EX_UNAVAILABLE,
119057249Seric };
119157249Seric 
119256678Seric ADDRESS *
119364284Seric buildaddr(tv, a, flags, e)
11943149Seric 	register char **tv;
11953149Seric 	register ADDRESS *a;
119664284Seric 	int flags;
119758966Seric 	register ENVELOPE *e;
11983149Seric {
11993149Seric 	struct mailer **mp;
12003149Seric 	register struct mailer *m;
120158008Seric 	char *bp;
120258008Seric 	int spaceleft;
120364306Seric 	static MAILER errormailer;
120464306Seric 	static char *errorargv[] = { "ERROR", NULL };
120557402Seric 	static char buf[MAXNAME];
12063149Seric 
12073149Seric 	if (a == NULL)
12083149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
120916889Seric 	bzero((char *) a, sizeof *a);
12103149Seric 
12113149Seric 	/* figure out what net/mailer to use */
121264306Seric 	if (*tv == NULL || (**tv & 0377) != CANONNET)
12134279Seric 	{
121458151Seric 		syserr("554 buildaddr: no net");
121564306Seric badaddr:
121664306Seric 		a->q_flags |= QBADADDR;
121764306Seric 		a->q_mailer = &errormailer;
121864306Seric 		if (errormailer.m_name == NULL)
121964306Seric 		{
122064306Seric 			/* initialize the bogus mailer */
122164306Seric 			errormailer.m_name = "*error*";
122264306Seric 			errormailer.m_mailer = "ERROR";
122364306Seric 			errormailer.m_argv = errorargv;
122464306Seric 		}
122564306Seric 		return a;
12264279Seric 	}
12273149Seric 	tv++;
122858680Seric 	if (strcasecmp(*tv, "error") == 0)
12294279Seric 	{
123058050Seric 		if ((**++tv & 0377) == CANONHOST)
123110183Seric 		{
123257249Seric 			register struct errcodes *ep;
123357249Seric 
123458050Seric 			if (isascii(**++tv) && isdigit(**tv))
123557249Seric 			{
123657249Seric 				setstat(atoi(*tv));
123757249Seric 			}
123857249Seric 			else
123957249Seric 			{
124057249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
124157249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
124257249Seric 						break;
124357249Seric 				setstat(ep->ec_code);
124457249Seric 			}
124510183Seric 			tv++;
124610183Seric 		}
124758050Seric 		if ((**tv & 0377) != CANONUSER)
124858151Seric 			syserr("554 buildaddr: error: no user");
124958814Seric 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
125058082Seric 		stripquotes(buf);
125164659Seric 		if (isascii(buf[0]) && isdigit(buf[0]) &&
125264659Seric 		    isascii(buf[1]) && isdigit(buf[1]) &&
125364659Seric 		    isascii(buf[2]) && isdigit(buf[2]) &&
125464659Seric 		    buf[3] == ' ')
125564659Seric 		{
125664659Seric 			char fmt[10];
125764659Seric 
125864659Seric 			strncpy(fmt, buf, 3);
125964659Seric 			strcpy(&fmt[3], " %s");
126064659Seric 			usrerr(fmt, buf + 4);
126164659Seric 		}
126264659Seric 		else
126364659Seric 		{
126464659Seric 			usrerr("%s", buf);
126564659Seric 		}
126664306Seric 		goto badaddr;
12674279Seric 	}
126857402Seric 
12694598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
12703149Seric 	{
127158680Seric 		if (strcasecmp(m->m_name, *tv) == 0)
12723149Seric 			break;
12733149Seric 	}
12743149Seric 	if (m == NULL)
12754279Seric 	{
127658151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
127764306Seric 		goto badaddr;
12784279Seric 	}
12794598Seric 	a->q_mailer = m;
12803149Seric 
12813149Seric 	/* figure out what host (if any) */
128256678Seric 	tv++;
128358509Seric 	if ((**tv & 0377) == CANONHOST)
12843149Seric 	{
128558008Seric 		bp = buf;
128658008Seric 		spaceleft = sizeof buf - 1;
128758050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
128858008Seric 		{
128958008Seric 			int i = strlen(*tv);
129058008Seric 
129158008Seric 			if (i > spaceleft)
129258008Seric 			{
129358008Seric 				/* out of space for this address */
129458008Seric 				if (spaceleft >= 0)
129558151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
129658008Seric 						buf);
129758008Seric 				i = spaceleft;
129858008Seric 				spaceleft = 0;
129958008Seric 			}
130058008Seric 			if (i <= 0)
130158008Seric 				continue;
130258008Seric 			bcopy(*tv, bp, i);
130358008Seric 			bp += i;
130458008Seric 			spaceleft -= i;
130558008Seric 		}
130658010Seric 		*bp = '\0';
13075704Seric 		a->q_host = newstr(buf);
13083149Seric 	}
130957249Seric 	else
131058509Seric 	{
131158509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
131258509Seric 		{
131358509Seric 			syserr("554 buildaddr: no host");
131464306Seric 			goto badaddr;
131558509Seric 		}
131657249Seric 		a->q_host = NULL;
131758509Seric 	}
13183149Seric 
13193149Seric 	/* figure out the user */
132058050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
13214279Seric 	{
132258151Seric 		syserr("554 buildaddr: no user");
132364306Seric 		goto badaddr;
13244279Seric 	}
132557402Seric 	tv++;
132651317Seric 
132757402Seric 	/* do special mapping for local mailer */
132857402Seric 	if (m == LocalMailer && *tv != NULL)
132957402Seric 	{
133057454Seric 		register char *p = *tv;
133157454Seric 
133257454Seric 		if (*p == '"')
133357454Seric 			p++;
133457454Seric 		if (*p == '|')
133557402Seric 			a->q_mailer = m = ProgMailer;
133657454Seric 		else if (*p == '/')
133757402Seric 			a->q_mailer = m = FileMailer;
133857454Seric 		else if (*p == ':')
133957402Seric 		{
134057402Seric 			/* may be :include: */
134158814Seric 			cataddr(tv, NULL, buf, sizeof buf, '\0');
134258008Seric 			stripquotes(buf);
134358008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
134458008Seric 			{
134558008Seric 				/* if :include:, don't need further rewriting */
134657402Seric 				a->q_mailer = m = InclMailer;
134758008Seric 				a->q_user = &buf[9];
134858008Seric 				return (a);
134958008Seric 			}
135057402Seric 		}
135157402Seric 	}
135257402Seric 
135358008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
135458008Seric 	{
135558008Seric 		tv++;
135658008Seric 		a->q_flags |= QNOTREMOTE;
135758008Seric 	}
135858008Seric 
135964284Seric 	/* rewrite according recipient mailer rewriting rules */
136064284Seric 	define('h', a->q_host, e);
136164284Seric 	if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags))
136264284Seric 	{
136364284Seric 		/* sender addresses done later */
136464284Seric 		(void) rewrite(tv, 2, e);
136564284Seric 		if (m->m_re_rwset > 0)
136664284Seric 		       (void) rewrite(tv, m->m_re_rwset, e);
136764284Seric 	}
136859084Seric 	(void) rewrite(tv, 4, e);
136919040Seric 
137019040Seric 	/* save the result for the command line/RCPT argument */
137158814Seric 	cataddr(tv, NULL, buf, sizeof buf, '\0');
13723149Seric 	a->q_user = buf;
13733149Seric 
137458670Seric 	/*
137558670Seric 	**  Do mapping to lower case as requested by mailer
137658670Seric 	*/
137758670Seric 
137858670Seric 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
137958670Seric 		makelower(a->q_host);
138058670Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
138158670Seric 		makelower(a->q_user);
138258670Seric 
13833149Seric 	return (a);
13843149Seric }
13853188Seric /*
13864228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
13874228Seric **
13884228Seric **	Parameters:
13894228Seric **		pvp -- parameter vector to rebuild.
139058814Seric **		evp -- last parameter to include.  Can be NULL to
139158814Seric **			use entire pvp.
13924228Seric **		buf -- buffer to build the string into.
13934228Seric **		sz -- size of buf.
139458082Seric **		spacesub -- the space separator character; if null,
139558082Seric **			use SpaceSub.
13964228Seric **
13974228Seric **	Returns:
13984228Seric **		none.
13994228Seric **
14004228Seric **	Side Effects:
14014228Seric **		Destroys buf.
14024228Seric */
14034228Seric 
140458814Seric cataddr(pvp, evp, buf, sz, spacesub)
14054228Seric 	char **pvp;
140658814Seric 	char **evp;
14074228Seric 	char *buf;
14084228Seric 	register int sz;
140958082Seric 	char spacesub;
14104228Seric {
14114228Seric 	bool oatomtok = FALSE;
141256678Seric 	bool natomtok = FALSE;
14134228Seric 	register int i;
14144228Seric 	register char *p;
14154228Seric 
141658082Seric 	if (spacesub == '\0')
141758082Seric 		spacesub = SpaceSub;
141858082Seric 
14198423Seric 	if (pvp == NULL)
14208423Seric 	{
142123109Seric 		(void) strcpy(buf, "");
14228423Seric 		return;
14238423Seric 	}
14244228Seric 	p = buf;
142511156Seric 	sz -= 2;
14264228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
14274228Seric 	{
14288078Seric 		natomtok = (toktype(**pvp) == ATM);
14294228Seric 		if (oatomtok && natomtok)
143058082Seric 			*p++ = spacesub;
14314228Seric 		(void) strcpy(p, *pvp);
14324228Seric 		oatomtok = natomtok;
14334228Seric 		p += i;
143411156Seric 		sz -= i + 1;
143558814Seric 		if (pvp++ == evp)
143658814Seric 			break;
14374228Seric 	}
14384228Seric 	*p = '\0';
14394228Seric }
14404228Seric /*
14413188Seric **  SAMEADDR -- Determine if two addresses are the same
14423188Seric **
14433188Seric **	This is not just a straight comparison -- if the mailer doesn't
14443188Seric **	care about the host we just ignore it, etc.
14453188Seric **
14463188Seric **	Parameters:
14473188Seric **		a, b -- pointers to the internal forms to compare.
14483188Seric **
14493188Seric **	Returns:
14503188Seric **		TRUE -- they represent the same mailbox.
14513188Seric **		FALSE -- they don't.
14523188Seric **
14533188Seric **	Side Effects:
14543188Seric **		none.
14553188Seric */
14563188Seric 
14573188Seric bool
14589374Seric sameaddr(a, b)
14593188Seric 	register ADDRESS *a;
14603188Seric 	register ADDRESS *b;
14613188Seric {
14623188Seric 	/* if they don't have the same mailer, forget it */
14633188Seric 	if (a->q_mailer != b->q_mailer)
14643188Seric 		return (FALSE);
14653188Seric 
14663188Seric 	/* if the user isn't the same, we can drop out */
146756678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
14683188Seric 		return (FALSE);
14693188Seric 
147058438Seric 	/* if we have good uids for both but the differ, these are different */
147158438Seric 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
147258438Seric 		return (FALSE);
147358438Seric 
147458509Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
147558509Seric 	if (a->q_host == b->q_host)
147658509Seric 	{
147758509Seric 		/* probably both null pointers */
14783188Seric 		return (TRUE);
147958509Seric 	}
14803188Seric 	if (a->q_host == NULL || b->q_host == NULL)
148158509Seric 	{
148258509Seric 		/* only one is a null pointer */
14833188Seric 		return (FALSE);
148458509Seric 	}
148556678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
14863188Seric 		return (FALSE);
14873188Seric 
14883188Seric 	return (TRUE);
14893188Seric }
14903234Seric /*
14913234Seric **  PRINTADDR -- print address (for debugging)
14923234Seric **
14933234Seric **	Parameters:
14943234Seric **		a -- the address to print
14953234Seric **		follow -- follow the q_next chain.
14963234Seric **
14973234Seric **	Returns:
14983234Seric **		none.
14993234Seric **
15003234Seric **	Side Effects:
15013234Seric **		none.
15023234Seric */
15033234Seric 
15043234Seric printaddr(a, follow)
15053234Seric 	register ADDRESS *a;
15063234Seric 	bool follow;
15073234Seric {
15085001Seric 	bool first = TRUE;
150957731Seric 	register MAILER *m;
151057731Seric 	MAILER pseudomailer;
15115001Seric 
15123234Seric 	while (a != NULL)
15133234Seric 	{
15145001Seric 		first = FALSE;
15154443Seric 		printf("%x=", a);
15164085Seric 		(void) fflush(stdout);
151757731Seric 
151857731Seric 		/* find the mailer -- carefully */
151957731Seric 		m = a->q_mailer;
152057731Seric 		if (m == NULL)
152157731Seric 		{
152257731Seric 			m = &pseudomailer;
152357731Seric 			m->m_mno = -1;
152457731Seric 			m->m_name = "NULL";
152557731Seric 		}
152657731Seric 
152758680Seric 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
152857731Seric 		       a->q_paddr, m->m_mno, m->m_name,
152963756Seric 		       a->q_host, a->q_user,
153063756Seric 		       a->q_ruser ? a->q_ruser : "<null>");
153159269Seric 		printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n",
153259269Seric 		       a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid);
153359269Seric 		printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n",
153459269Seric 		       a->q_owner == NULL ? "(none)" : a->q_owner,
153563756Seric 		       a->q_home == NULL ? "(none)" : a->q_home,
153663756Seric 		       a->q_fullname == NULL ? "(none)" : a->q_fullname);
15374996Seric 
15383234Seric 		if (!follow)
15393234Seric 			return;
15404996Seric 		a = a->q_next;
15413234Seric 	}
15425001Seric 	if (first)
15434443Seric 		printf("[NULL]\n");
15443234Seric }
15454317Seric 
15467682Seric /*
15477682Seric **  REMOTENAME -- return the name relative to the current mailer
15487682Seric **
15497682Seric **	Parameters:
15507682Seric **		name -- the name to translate.
15518069Seric **		m -- the mailer that we want to do rewriting relative
15528069Seric **			to.
155359163Seric **		flags -- fine tune operations.
155459163Seric **		pstat -- pointer to status word.
155558020Seric **		e -- the current envelope.
15567682Seric **
15577682Seric **	Returns:
15587682Seric **		the text string representing this address relative to
15597682Seric **			the receiving mailer.
15607682Seric **
15617682Seric **	Side Effects:
15627682Seric **		none.
15637682Seric **
15647682Seric **	Warnings:
15657682Seric **		The text string returned is tucked away locally;
15667682Seric **			copy it if you intend to save it.
15677682Seric */
15687682Seric 
15697682Seric char *
157059163Seric remotename(name, m, flags, pstat, e)
15717682Seric 	char *name;
157256678Seric 	struct mailer *m;
157359163Seric 	int flags;
157459163Seric 	int *pstat;
157556678Seric 	register ENVELOPE *e;
15767682Seric {
15778069Seric 	register char **pvp;
15788069Seric 	char *fancy;
157956678Seric 	char *oldg = macvalue('g', e);
158058020Seric 	int rwset;
15817682Seric 	static char buf[MAXNAME];
15827682Seric 	char lbuf[MAXNAME];
158316914Seric 	char pvpbuf[PSBUFSIZE];
158456678Seric 	extern char *crackaddr();
15857682Seric 
15867755Seric 	if (tTd(12, 1))
15877755Seric 		printf("remotename(%s)\n", name);
15887755Seric 
158910177Seric 	/* don't do anything if we are tagging it as special */
159059163Seric 	if (bitset(RF_SENDERADDR, flags))
159159163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
159259163Seric 						     : m->m_se_rwset;
159358020Seric 	else
159459163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
159559163Seric 						     : m->m_re_rwset;
159658020Seric 	if (rwset < 0)
159710177Seric 		return (name);
159810177Seric 
15997682Seric 	/*
16008181Seric 	**  Do a heuristic crack of this name to extract any comment info.
16018181Seric 	**	This will leave the name as a comment and a $g macro.
16027889Seric 	*/
16037889Seric 
160459163Seric 	if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags))
160558050Seric 		fancy = "\201g";
160610310Seric 	else
160710310Seric 		fancy = crackaddr(name);
16087889Seric 
16098181Seric 	/*
16108181Seric 	**  Turn the name into canonical form.
16118181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
16128181Seric 	**	If this only resolves to "user", and the "C" flag is
16138181Seric 	**	specified in the sending mailer, then the sender's
16148181Seric 	**	domain will be appended.
16158181Seric 	*/
16168181Seric 
161758333Seric 	pvp = prescan(name, '\0', pvpbuf, NULL);
16187889Seric 	if (pvp == NULL)
16197889Seric 		return (name);
162059163Seric 	if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
162159163Seric 		*pstat = EX_TEMPFAIL;
162259163Seric 	if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
16238181Seric 	{
16248181Seric 		/* append from domain to this address */
16258181Seric 		register char **pxp = pvp;
16268181Seric 
16279594Seric 		/* see if there is an "@domain" in the current name */
16288181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
16298181Seric 			pxp++;
16308181Seric 		if (*pxp == NULL)
16318181Seric 		{
16329594Seric 			/* no.... append the "@domain" from the sender */
163356678Seric 			register char **qxq = e->e_fromdomain;
16348181Seric 
16359594Seric 			while ((*pxp++ = *qxq++) != NULL)
16369594Seric 				continue;
163759163Seric 			if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
163859163Seric 				*pstat = EX_TEMPFAIL;
16398181Seric 		}
16408181Seric 	}
16418181Seric 
16428181Seric 	/*
16438959Seric 	**  Do more specific rewriting.
164456678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
164556678Seric 	**		a sender address or not.
16468181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
16478181Seric 	*/
16488181Seric 
164959163Seric 	if (bitset(RF_SENDERADDR, flags))
165059541Seric 	{
165159163Seric 		if (rewrite(pvp, 1, e) == EX_TEMPFAIL)
165259163Seric 			*pstat = EX_TEMPFAIL;
165359541Seric 	}
16548069Seric 	else
165559541Seric 	{
165659163Seric 		if (rewrite(pvp, 2, e) == EX_TEMPFAIL)
165759163Seric 			*pstat = EX_TEMPFAIL;
165859541Seric 	}
165958020Seric 	if (rwset > 0)
166059541Seric 	{
166159163Seric 		if (rewrite(pvp, rwset, e) == EX_TEMPFAIL)
166259163Seric 			*pstat = EX_TEMPFAIL;
166359541Seric 	}
16647682Seric 
16658181Seric 	/*
16668959Seric 	**  Do any final sanitation the address may require.
16678959Seric 	**	This will normally be used to turn internal forms
16688959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
16698959Seric 	**	may be used as a default to the above rules.
16708959Seric 	*/
16718959Seric 
167259163Seric 	if (rewrite(pvp, 4, e) == EX_TEMPFAIL)
167359163Seric 		*pstat = EX_TEMPFAIL;
16748959Seric 
16758959Seric 	/*
16768181Seric 	**  Now restore the comment information we had at the beginning.
16778181Seric 	*/
16788181Seric 
167958825Seric 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
168056678Seric 	define('g', lbuf, e);
168156678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
168256678Seric 	define('g', oldg, e);
16837682Seric 
16847682Seric 	if (tTd(12, 1))
16857755Seric 		printf("remotename => `%s'\n", buf);
16867682Seric 	return (buf);
16877682Seric }
168851317Seric /*
168956678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
169051317Seric **
169151317Seric **	Parameters:
169256678Seric **		a -- the address to map (but just the user name part).
169356678Seric **		sendq -- the sendq in which to install any replacement
169456678Seric **			addresses.
169551317Seric **
169651317Seric **	Returns:
169751317Seric **		none.
169851317Seric */
169951317Seric 
170056678Seric maplocaluser(a, sendq, e)
170156678Seric 	register ADDRESS *a;
170256678Seric 	ADDRESS **sendq;
170356678Seric 	ENVELOPE *e;
170451317Seric {
170556678Seric 	register char **pvp;
170656678Seric 	register ADDRESS *a1 = NULL;
170758333Seric 	auto char *delimptr;
170856678Seric 	char pvpbuf[PSBUFSIZE];
170951317Seric 
171056678Seric 	if (tTd(29, 1))
171156678Seric 	{
171256678Seric 		printf("maplocaluser: ");
171356678Seric 		printaddr(a, FALSE);
171456678Seric 	}
171558333Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
171656678Seric 	if (pvp == NULL)
171756678Seric 		return;
171851317Seric 
171959084Seric 	(void) rewrite(pvp, 5, e);
172058050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
172156678Seric 		return;
172251317Seric 
172356678Seric 	/* if non-null, mailer destination specified -- has it changed? */
172464284Seric 	a1 = buildaddr(pvp, NULL, 0, e);
172556678Seric 	if (a1 == NULL || sameaddr(a, a1))
172656678Seric 		return;
172751317Seric 
172856678Seric 	/* mark old address as dead; insert new address */
172956678Seric 	a->q_flags |= QDONTSEND;
173057731Seric 	if (tTd(29, 5))
173157731Seric 	{
173257731Seric 		printf("maplocaluser: QDONTSEND ");
173357731Seric 		printaddr(a, FALSE);
173457731Seric 	}
173556678Seric 	a1->q_alias = a;
173664348Seric 	allocaddr(a1, RF_COPYALL, NULL);
173756678Seric 	(void) recipient(a1, sendq, e);
173851317Seric }
173958802Seric /*
174058802Seric **  DEQUOTE_INIT -- initialize dequote map
174158802Seric **
174258802Seric **	This is a no-op.
174358802Seric **
174458802Seric **	Parameters:
174558802Seric **		map -- the internal map structure.
174658802Seric **		args -- arguments.
174758802Seric **
174858802Seric **	Returns:
174958802Seric **		TRUE.
175058802Seric */
175158802Seric 
175258802Seric bool
175360219Seric dequote_init(map, args)
175458802Seric 	MAP *map;
175558802Seric 	char *args;
175658802Seric {
175758805Seric 	register char *p = args;
175858805Seric 
175958805Seric 	for (;;)
176058805Seric 	{
176158805Seric 		while (isascii(*p) && isspace(*p))
176258805Seric 			p++;
176358805Seric 		if (*p != '-')
176458805Seric 			break;
176558805Seric 		switch (*++p)
176658805Seric 		{
176758805Seric 		  case 'a':
176858805Seric 			map->map_app = ++p;
176958805Seric 			break;
177058805Seric 		}
177158805Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
177258805Seric 			p++;
177358805Seric 		if (*p != '\0')
177458805Seric 			*p = '\0';
177558805Seric 	}
177658805Seric 	if (map->map_app != NULL)
177758805Seric 		map->map_app = newstr(map->map_app);
177858805Seric 
177958802Seric 	return TRUE;
178058802Seric }
178158802Seric /*
178258802Seric **  DEQUOTE_MAP -- unquote an address
178358802Seric **
178458802Seric **	Parameters:
178558802Seric **		map -- the internal map structure (ignored).
178660089Seric **		name -- the name to dequote.
178758802Seric **		av -- arguments (ignored).
178859084Seric **		statp -- pointer to status out-parameter.
178958802Seric **
179058802Seric **	Returns:
179158802Seric **		NULL -- if there were no quotes, or if the resulting
179258802Seric **			unquoted buffer would not be acceptable to prescan.
179358802Seric **		else -- The dequoted buffer.
179458802Seric */
179558802Seric 
179658802Seric char *
179760089Seric dequote_map(map, name, av, statp)
179858802Seric 	MAP *map;
179960089Seric 	char *name;
180058802Seric 	char **av;
180159084Seric 	int *statp;
180258802Seric {
180358802Seric 	register char *p;
180458802Seric 	register char *q;
180558802Seric 	register char c;
180658802Seric 	int anglecnt;
180758802Seric 	int cmntcnt;
180858802Seric 	int quotecnt;
180959089Seric 	int spacecnt;
181058802Seric 	bool quotemode;
181158802Seric 	bool bslashmode;
181258802Seric 
181358802Seric 	anglecnt = 0;
181458802Seric 	cmntcnt = 0;
181558802Seric 	quotecnt = 0;
181659089Seric 	spacecnt = 0;
181758802Seric 	quotemode = FALSE;
181858802Seric 	bslashmode = FALSE;
181958802Seric 
182060089Seric 	for (p = q = name; (c = *p++) != '\0'; )
182158802Seric 	{
182258802Seric 		if (bslashmode)
182358802Seric 		{
182458802Seric 			bslashmode = FALSE;
182558802Seric 			*q++ = c;
182658802Seric 			continue;
182758802Seric 		}
182858802Seric 
182958802Seric 		switch (c)
183058802Seric 		{
183158802Seric 		  case '\\':
183258802Seric 			bslashmode = TRUE;
183358802Seric 			break;
183458802Seric 
183558802Seric 		  case '(':
183658802Seric 			cmntcnt++;
183758802Seric 			break;
183858802Seric 
183958802Seric 		  case ')':
184058802Seric 			if (cmntcnt-- <= 0)
184158802Seric 				return NULL;
184258802Seric 			break;
184359089Seric 
184459089Seric 		  case ' ':
184559089Seric 			spacecnt++;
184659089Seric 			break;
184758802Seric 		}
184858802Seric 
184958802Seric 		if (cmntcnt > 0)
185058802Seric 		{
185158802Seric 			*q++ = c;
185258802Seric 			continue;
185358802Seric 		}
185458802Seric 
185558802Seric 		switch (c)
185658802Seric 		{
185758802Seric 		  case '"':
185858802Seric 			quotemode = !quotemode;
185958802Seric 			quotecnt++;
186058802Seric 			continue;
186158802Seric 
186258802Seric 		  case '<':
186358802Seric 			anglecnt++;
186458802Seric 			break;
186558802Seric 
186658802Seric 		  case '>':
186758802Seric 			if (anglecnt-- <= 0)
186858802Seric 				return NULL;
186958802Seric 			break;
187058802Seric 		}
187158802Seric 		*q++ = c;
187258802Seric 	}
187358802Seric 
187458802Seric 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
187559089Seric 	    quotemode || quotecnt <= 0 || spacecnt != 0)
187658802Seric 		return NULL;
187758802Seric 	*q++ = '\0';
187860089Seric 	return name;
187958802Seric }
1880