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*64306Seric static char sccsid[] = "@(#)parseaddr.c	8.8 (Berkeley) 08/19/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 
7757388Seric 	if (invalidaddr(addr))
7857388Seric 	{
7957388Seric 		if (tTd(20, 1))
8057388Seric 			printf("parseaddr-->bad address\n");
8157388Seric 		return NULL;
8257388Seric 	}
8357388Seric 
8458333Seric 	if (delimptr == NULL)
8558333Seric 		delimptr = &delimptrbuf;
8658333Seric 
8758333Seric 	pvp = prescan(addr, delim, pvpbuf, delimptr);
883149Seric 	if (pvp == NULL)
8956729Seric 	{
9056729Seric 		if (tTd(20, 1))
9156729Seric 			printf("parseaddr-->NULL\n");
92297Seric 		return (NULL);
9356729Seric 	}
94297Seric 
95297Seric 	/*
963149Seric 	**  Apply rewriting rules.
977889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
98297Seric 	*/
99297Seric 
10059084Seric 	queueup = FALSE;
10159084Seric 	if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
10259084Seric 		queueup = TRUE;
10359084Seric 	if (rewrite(pvp, 0, e) == EX_TEMPFAIL)
10459084Seric 		queueup = TRUE;
105297Seric 
106297Seric 
107297Seric 	/*
1083149Seric 	**  Build canonical address from pvp.
109297Seric 	*/
110297Seric 
11164284Seric 	a = buildaddr(pvp, a, flags, e);
112297Seric 
113297Seric 	/*
1143149Seric 	**  Make local copies of the host & user and then
1153149Seric 	**  transport them out.
116297Seric 	*/
117297Seric 
11864284Seric 	allocaddr(a, flags, addr, *delimptr);
119*64306Seric 	if (bitset(QBADADDR, a->q_flags))
120*64306Seric 		return a;
12156678Seric 
12256678Seric 	/*
12359084Seric 	**  If there was a parsing failure, mark it for queueing.
12459084Seric 	*/
12559084Seric 
12659084Seric 	if (queueup)
12759595Seric 	{
12859734Seric 		char *msg = "Transient parse error -- message queued for future delivery";
12959734Seric 
13059595Seric 		if (tTd(20, 1))
13159595Seric 			printf("parseaddr: queuing message\n");
13259734Seric 		message(msg);
13359734Seric 		if (e->e_message == NULL)
13460009Seric 			e->e_message = newstr(msg);
13559084Seric 		a->q_flags |= QQUEUEUP;
13659595Seric 	}
13759084Seric 
13859084Seric 	/*
13956678Seric 	**  Compute return value.
14056678Seric 	*/
14156678Seric 
14256678Seric 	if (tTd(20, 1))
1438078Seric 	{
14456678Seric 		printf("parseaddr-->");
14556678Seric 		printaddr(a, FALSE);
14656678Seric 	}
14756678Seric 
14856678Seric 	return (a);
14956678Seric }
15056678Seric /*
15157388Seric **  INVALIDADDR -- check for address containing meta-characters
15257388Seric **
15357388Seric **	Parameters:
15457388Seric **		addr -- the address to check.
15557388Seric **
15657388Seric **	Returns:
15757388Seric **		TRUE -- if the address has any "wierd" characters
15857388Seric **		FALSE -- otherwise.
15957388Seric */
16057388Seric 
16157388Seric bool
16257388Seric invalidaddr(addr)
16357388Seric 	register char *addr;
16457388Seric {
16557388Seric 	for (; *addr != '\0'; addr++)
16657388Seric 	{
16758050Seric 		if ((*addr & 0340) != 0200)
16857388Seric 			continue;
16957388Seric 		setstat(EX_USAGE);
17058151Seric 		usrerr("553 Address contained invalid control characters");
17157388Seric 		return TRUE;
17257388Seric 	}
17357388Seric 	return FALSE;
17457388Seric }
17557388Seric /*
17656678Seric **  ALLOCADDR -- do local allocations of address on demand.
17756678Seric **
17856678Seric **	Also lowercases the host name if requested.
17956678Seric **
18056678Seric **	Parameters:
18156678Seric **		a -- the address to reallocate.
18264284Seric **		flags -- the copy flag (see RF_ definitions in sendmail.h
18364284Seric **			for a description).
18456678Seric **		paddr -- the printname of the address.
18558333Seric **		delimptr -- a pointer to the address delimiter.  Must be set.
18656678Seric **
18756678Seric **	Returns:
18856678Seric **		none.
18956678Seric **
19056678Seric **	Side Effects:
19156678Seric **		Copies portions of a into local buffers as requested.
19256678Seric */
19356678Seric 
19464284Seric allocaddr(a, flags, paddr, delimptr)
19556678Seric 	register ADDRESS *a;
19664284Seric 	int flags;
19756678Seric 	char *paddr;
19858333Seric 	char *delimptr;
19956678Seric {
20058673Seric 	if (tTd(24, 4))
20164284Seric 		printf("allocaddr(flags=%o, paddr=%s)\n", flags, paddr);
20258673Seric 
20364284Seric 	if (bitset(RF_COPYPADDR, flags) && paddr != NULL)
20456678Seric 	{
20558333Seric 		char savec = *delimptr;
2068078Seric 
20759747Seric 		if (savec != '\0')
20859747Seric 			*delimptr = '\0';
20956678Seric 		a->q_paddr = newstr(paddr);
21059747Seric 		if (savec != '\0')
21159747Seric 			*delimptr = savec;
2128078Seric 	}
213297Seric 	else
21456678Seric 		a->q_paddr = paddr;
21524944Seric 
21624944Seric 	if (a->q_user == NULL)
21724944Seric 		a->q_user = "";
21824944Seric 	if (a->q_host == NULL)
21924944Seric 		a->q_host = "";
22024944Seric 
22164284Seric 	if (bitset(RF_COPYPARSE, flags))
222297Seric 	{
22324944Seric 		a->q_host = newstr(a->q_host);
2243149Seric 		if (a->q_user != a->q_paddr)
2253149Seric 			a->q_user = newstr(a->q_user);
226297Seric 	}
227297Seric 
22856678Seric 	if (a->q_paddr == NULL)
22956678Seric 		a->q_paddr = a->q_user;
230297Seric }
231297Seric /*
232297Seric **  PRESCAN -- Prescan name and make it canonical
233297Seric **
2349374Seric **	Scans a name and turns it into a set of tokens.  This process
2359374Seric **	deletes blanks and comments (in parentheses).
236297Seric **
237297Seric **	This routine knows about quoted strings and angle brackets.
238297Seric **
239297Seric **	There are certain subtleties to this routine.  The one that
240297Seric **	comes to mind now is that backslashes on the ends of names
241297Seric **	are silently stripped off; this is intentional.  The problem
242297Seric **	is that some versions of sndmsg (like at LBL) set the kill
243297Seric **	character to something other than @ when reading addresses;
244297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
245297Seric **	berknet mailer.
246297Seric **
247297Seric **	Parameters:
248297Seric **		addr -- the name to chomp.
249297Seric **		delim -- the delimiter for the address, normally
250297Seric **			'\0' or ','; \0 is accepted in any case.
25115284Seric **			If '\t' then we are reading the .cf file.
25216914Seric **		pvpbuf -- place to put the saved text -- note that
25316914Seric **			the pointers are static.
25458333Seric **		delimptr -- if non-NULL, set to the location of the
25558333Seric **			terminating delimiter.
256297Seric **
257297Seric **	Returns:
2583149Seric **		A pointer to a vector of tokens.
259297Seric **		NULL on error.
260297Seric */
261297Seric 
2628078Seric /* states and character types */
2638078Seric # define OPR		0	/* operator */
2648078Seric # define ATM		1	/* atom */
2658078Seric # define QST		2	/* in quoted string */
2668078Seric # define SPC		3	/* chewing up spaces */
2678078Seric # define ONE		4	/* pick up one character */
2683149Seric 
2698078Seric # define NSTATES	5	/* number of states */
2708078Seric # define TYPE		017	/* mask to select state type */
2718078Seric 
2728078Seric /* meta bits for table */
2738078Seric # define M		020	/* meta character; don't pass through */
2748078Seric # define B		040	/* cause a break */
2758078Seric # define MB		M|B	/* meta-break */
2768078Seric 
2778078Seric static short StateTab[NSTATES][NSTATES] =
2788078Seric {
2798087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2809051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2819051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
2829051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
2838078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
2848078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
2858078Seric };
2868078Seric 
2878078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
2888078Seric 
2893149Seric char **
29058333Seric prescan(addr, delim, pvpbuf, delimptr)
291297Seric 	char *addr;
292297Seric 	char delim;
29316914Seric 	char pvpbuf[];
29458333Seric 	char **delimptr;
295297Seric {
296297Seric 	register char *p;
2978078Seric 	register char *q;
2989346Seric 	register int c;
2993149Seric 	char **avp;
300297Seric 	bool bslashmode;
301297Seric 	int cmntcnt;
3028423Seric 	int anglecnt;
3033149Seric 	char *tok;
3048078Seric 	int state;
3058078Seric 	int newstate;
30659747Seric 	char *saveto = CurEnv->e_to;
3078078Seric 	static char *av[MAXATOM+1];
30856678Seric 	extern int errno;
309297Seric 
31015253Seric 	/* make sure error messages don't have garbage on them */
31115253Seric 	errno = 0;
31215253Seric 
31316914Seric 	q = pvpbuf;
3143149Seric 	bslashmode = FALSE;
3157800Seric 	cmntcnt = 0;
3168423Seric 	anglecnt = 0;
3173149Seric 	avp = av;
31856678Seric 	state = ATM;
3198078Seric 	c = NOCHAR;
3208078Seric 	p = addr;
32159747Seric 	CurEnv->e_to = p;
32256764Seric 	if (tTd(22, 11))
323297Seric 	{
3248078Seric 		printf("prescan: ");
3258078Seric 		xputs(p);
32623109Seric 		(void) putchar('\n');
3278078Seric 	}
3288078Seric 
3298078Seric 	do
3308078Seric 	{
3313149Seric 		/* read a token */
3323149Seric 		tok = q;
3338078Seric 		for (;;)
334297Seric 		{
3358078Seric 			/* store away any old lookahead character */
33659277Seric 			if (c != NOCHAR && !bslashmode)
3378078Seric 			{
33815284Seric 				/* see if there is room */
33916914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3408078Seric 				{
34158151Seric 					usrerr("553 Address too long");
34258333Seric 					if (delimptr != NULL)
34358333Seric 						*delimptr = p;
34459747Seric 					CurEnv->e_to = saveto;
3458078Seric 					return (NULL);
3468078Seric 				}
34715284Seric 
34815284Seric 				/* squirrel it away */
3498078Seric 				*q++ = c;
3508078Seric 			}
3518078Seric 
3528078Seric 			/* read a new input character */
3538078Seric 			c = *p++;
35457631Seric 			if (c == '\0')
35556764Seric 			{
35656764Seric 				/* diagnose and patch up bad syntax */
35756764Seric 				if (state == QST)
35856764Seric 				{
35963851Seric 					usrerr("653 Unbalanced '\"' (fixed)");
36056764Seric 					c = '"';
36156764Seric 				}
36256764Seric 				else if (cmntcnt > 0)
36356764Seric 				{
36463851Seric 					usrerr("653 Unbalanced '(' (fixed)");
36556764Seric 					c = ')';
36656764Seric 				}
36756764Seric 				else if (anglecnt > 0)
36856764Seric 				{
36956764Seric 					c = '>';
37063851Seric 					usrerr("653 Unbalanced '<' (fixed)");
37156764Seric 				}
37256764Seric 				else
37356764Seric 					break;
37415284Seric 
37556764Seric 				p--;
37656764Seric 			}
37757631Seric 			else if (c == delim && anglecnt <= 0 &&
37857631Seric 					cmntcnt <= 0 && state != QST)
37957631Seric 				break;
38056764Seric 
3818078Seric 			if (tTd(22, 101))
3828078Seric 				printf("c=%c, s=%d; ", c, state);
3838078Seric 
3843149Seric 			/* chew up special characters */
3853149Seric 			*q = '\0';
3863149Seric 			if (bslashmode)
3873149Seric 			{
38859105Seric 				bslashmode = FALSE;
38959105Seric 
39024944Seric 				/* kludge \! for naive users */
39158061Seric 				if (cmntcnt > 0)
39259105Seric 				{
39358061Seric 					c = NOCHAR;
39459105Seric 					continue;
39559105Seric 				}
39659105Seric 				else if (c != '!' || state == QST)
39759105Seric 				{
39856678Seric 					*q++ = '\\';
39959105Seric 					continue;
40059105Seric 				}
4013149Seric 			}
40256678Seric 
40356678Seric 			if (c == '\\')
4043149Seric 			{
4053149Seric 				bslashmode = TRUE;
4063149Seric 			}
40756678Seric 			else if (state == QST)
4088514Seric 			{
4098514Seric 				/* do nothing, just avoid next clauses */
4108514Seric 			}
4118078Seric 			else if (c == '(')
4124100Seric 			{
4138078Seric 				cmntcnt++;
4148078Seric 				c = NOCHAR;
4154100Seric 			}
4168078Seric 			else if (c == ')')
4173149Seric 			{
4188078Seric 				if (cmntcnt <= 0)
4193149Seric 				{
42063851Seric 					usrerr("653 Unbalanced ')' (fixed)");
42163844Seric 					c = NOCHAR;
4223149Seric 				}
4238078Seric 				else
4248078Seric 					cmntcnt--;
4258078Seric 			}
4268078Seric 			else if (cmntcnt > 0)
4278078Seric 				c = NOCHAR;
4288423Seric 			else if (c == '<')
4298423Seric 				anglecnt++;
4308423Seric 			else if (c == '>')
4318423Seric 			{
4328423Seric 				if (anglecnt <= 0)
4338423Seric 				{
43463851Seric 					usrerr("653 Unbalanced '>' (fixed)");
43563844Seric 					c = NOCHAR;
4368423Seric 				}
43763844Seric 				else
43863844Seric 					anglecnt--;
4398423Seric 			}
44058050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
44111423Seric 				c = ' ';
4423149Seric 
4438078Seric 			if (c == NOCHAR)
4448078Seric 				continue;
4453149Seric 
4468078Seric 			/* see if this is end of input */
44711405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4483149Seric 				break;
4493149Seric 
4508078Seric 			newstate = StateTab[state][toktype(c)];
4518078Seric 			if (tTd(22, 101))
4528078Seric 				printf("ns=%02o\n", newstate);
4538078Seric 			state = newstate & TYPE;
4548078Seric 			if (bitset(M, newstate))
4558078Seric 				c = NOCHAR;
4568078Seric 			if (bitset(B, newstate))
4574228Seric 				break;
458297Seric 		}
4593149Seric 
4603149Seric 		/* new token */
4618078Seric 		if (tok != q)
4621378Seric 		{
4638078Seric 			*q++ = '\0';
4648078Seric 			if (tTd(22, 36))
465297Seric 			{
4668078Seric 				printf("tok=");
4678078Seric 				xputs(tok);
46823109Seric 				(void) putchar('\n');
469297Seric 			}
4708078Seric 			if (avp >= &av[MAXATOM])
471297Seric 			{
47258151Seric 				syserr("553 prescan: too many tokens");
47358333Seric 				if (delimptr != NULL)
47458333Seric 					*delimptr = p;
47559747Seric 				CurEnv->e_to = saveto;
4768078Seric 				return (NULL);
477297Seric 			}
4788078Seric 			*avp++ = tok;
479297Seric 		}
4808423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
4813149Seric 	*avp = NULL;
48258333Seric 	p--;
48358333Seric 	if (delimptr != NULL)
48458333Seric 		*delimptr = p;
48556764Seric 	if (tTd(22, 12))
48656764Seric 	{
48756764Seric 		printf("prescan==>");
48856764Seric 		printav(av);
48956764Seric 	}
49059747Seric 	CurEnv->e_to = saveto;
49158546Seric 	if (av[0] == NULL)
49258546Seric 		return (NULL);
49358403Seric 	return (av);
4943149Seric }
4953149Seric /*
4963149Seric **  TOKTYPE -- return token type
4973149Seric **
4983149Seric **	Parameters:
4993149Seric **		c -- the character in question.
5003149Seric **
5013149Seric **	Returns:
5023149Seric **		Its type.
5033149Seric **
5043149Seric **	Side Effects:
5053149Seric **		none.
5063149Seric */
507297Seric 
5083149Seric toktype(c)
50958050Seric 	register int c;
5103149Seric {
5113380Seric 	static char buf[50];
5123382Seric 	static bool firstime = TRUE;
5133380Seric 
5143382Seric 	if (firstime)
5153380Seric 	{
5163382Seric 		firstime = FALSE;
51758050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5187005Seric 		(void) strcat(buf, DELIMCHARS);
5193380Seric 	}
52058050Seric 	c &= 0377;
52156327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5228078Seric 		return (ONE);
52359027Seric 	if (c == MACRODEXPAND)
52459027Seric 		return (ONE);
5258078Seric 	if (c == '"')
5268078Seric 		return (QST);
52758050Seric 	if ((c & 0340) == 0200)
52858050Seric 		return (OPR);
5294100Seric 	if (!isascii(c))
5308078Seric 		return (ATM);
5318078Seric 	if (isspace(c) || c == ')')
5328078Seric 		return (SPC);
53358050Seric 	if (strchr(buf, c) != NULL)
5348078Seric 		return (OPR);
5358078Seric 	return (ATM);
5363149Seric }
5373149Seric /*
5383149Seric **  REWRITE -- apply rewrite rules to token vector.
5393149Seric **
5404476Seric **	This routine is an ordered production system.  Each rewrite
5414476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5424476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5434476Seric **
5444476Seric **	For each rewrite rule, 'avp' points the address vector we
5454476Seric **	are trying to match against, and 'pvp' points to the pattern.
5468058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5479585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5489585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5494476Seric **
5504476Seric **	When a match between avp & pvp does not match, we try to
5519585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5524476Seric **	we must also back out the match in mvp.  If we reach a
5538058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5548058Seric **	over again.
5554476Seric **
5564476Seric **	When we finally match, we rewrite the address vector
5574476Seric **	and try over again.
5584476Seric **
5593149Seric **	Parameters:
5603149Seric **		pvp -- pointer to token vector.
56159027Seric **		ruleset -- the ruleset to use for rewriting.
56259027Seric **		e -- the current envelope.
5633149Seric **
5643149Seric **	Returns:
56559084Seric **		A status code.  If EX_TEMPFAIL, higher level code should
56659084Seric **			attempt recovery.
5673149Seric **
5683149Seric **	Side Effects:
5693149Seric **		pvp is modified.
5703149Seric */
5712091Seric 
5723149Seric struct match
5733149Seric {
5744468Seric 	char	**first;	/* first token matched */
5754468Seric 	char	**last;		/* last token matched */
57658825Seric 	char	**pattern;	/* pointer to pattern */
5773149Seric };
5783149Seric 
5794468Seric # define MAXMATCH	9	/* max params per rewrite */
5803149Seric 
5813149Seric 
58259084Seric int
58359027Seric rewrite(pvp, ruleset, e)
5843149Seric 	char **pvp;
5854070Seric 	int ruleset;
58659027Seric 	register ENVELOPE *e;
5873149Seric {
5883149Seric 	register char *ap;		/* address pointer */
5893149Seric 	register char *rp;		/* rewrite pointer */
5903149Seric 	register char **avp;		/* address vector pointer */
5913149Seric 	register char **rvp;		/* rewrite vector pointer */
5928058Seric 	register struct match *mlp;	/* cur ptr into mlist */
5938058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
59458866Seric 	int ruleno;			/* current rule number */
59559084Seric 	int rstat = EX_OK;		/* return status */
59656678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
5973149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
5983149Seric 
5999279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
6003149Seric 	{
6018959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
60256678Seric 		printav(pvp);
6033149Seric 	}
60456678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
60556326Seric 	{
60658151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
60759084Seric 		return EX_CONFIG;
60856326Seric 	}
60956678Seric 	if (pvp == NULL)
61059084Seric 		return EX_USAGE;
61156326Seric 
6123149Seric 	/*
61356678Seric 	**  Run through the list of rewrite rules, applying
61456678Seric 	**	any that match.
6153149Seric 	*/
6163149Seric 
61758866Seric 	ruleno = 1;
6184070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
6193149Seric 	{
62056678Seric 		int loopcount = 0;
62156678Seric 
6227675Seric 		if (tTd(21, 12))
623297Seric 		{
6248069Seric 			printf("-----trying rule:");
62556678Seric 			printav(rwr->r_lhs);
6263149Seric 		}
6273149Seric 
6283149Seric 		/* try to match on this rule */
6294468Seric 		mlp = mlist;
6308058Seric 		rvp = rwr->r_lhs;
6318058Seric 		avp = pvp;
63258866Seric 		if (++loopcount > 100)
6333149Seric 		{
63458866Seric 			syserr("554 Infinite loop in ruleset %d, rule %d",
63558866Seric 				ruleset, ruleno);
63658866Seric 			if (tTd(21, 1))
63752637Seric 			{
63856678Seric 				printf("workspace: ");
63956678Seric 				printav(pvp);
64052637Seric 			}
64158866Seric 			break;
64258866Seric 		}
64358866Seric 
64458866Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
64558866Seric 		{
6463149Seric 			rp = *rvp;
6478058Seric 			if (tTd(21, 35))
6488058Seric 			{
64958825Seric 				printf("ADVANCE rp=");
65057531Seric 				xputs(rp);
65157532Seric 				printf(", ap=");
6528058Seric 				xputs(ap);
6538069Seric 				printf("\n");
6548058Seric 			}
65556678Seric 			if (rp == NULL)
65656326Seric 			{
6573149Seric 				/* end-of-pattern before end-of-address */
6588058Seric 				goto backup;
65956678Seric 			}
66058173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
66158827Seric 			    (*rp & 0377) != MATCHZERO)
66256326Seric 			{
66358825Seric 				/* end-of-input with patterns left */
66458825Seric 				goto backup;
665297Seric 			}
66656326Seric 
66758050Seric 			switch (*rp & 0377)
6688058Seric 			{
66956678Seric 				register STAB *s;
67058814Seric 				char buf[MAXLINE];
67156326Seric 
67256678Seric 			  case MATCHCLASS:
67358825Seric 				/* match any phrase in a class */
67458825Seric 				mlp->pattern = rvp;
67558814Seric 				mlp->first = avp;
67658814Seric 	extendclass:
67758825Seric 				ap = *avp;
67858825Seric 				if (ap == NULL)
67958814Seric 					goto backup;
68058814Seric 				mlp->last = avp++;
68158814Seric 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
68258814Seric 				s = stab(buf, ST_CLASS, ST_FIND);
68356678Seric 				if (s == NULL || !bitnset(rp[1], s->s_class))
68456326Seric 				{
68558825Seric 					if (tTd(21, 36))
68658825Seric 					{
68758825Seric 						printf("EXTEND  rp=");
68858825Seric 						xputs(rp);
68958825Seric 						printf(", ap=");
69058825Seric 						xputs(ap);
69158825Seric 						printf("\n");
69258825Seric 					}
69358825Seric 					goto extendclass;
69456326Seric 				}
69558825Seric 				if (tTd(21, 36))
69658825Seric 					printf("CLMATCH\n");
69758814Seric 				mlp++;
69858814Seric 				break;
6994060Seric 
70058825Seric 			  case MATCHNCLASS:
70158825Seric 				/* match any token not in a class */
70258825Seric 				s = stab(ap, ST_CLASS, ST_FIND);
70358825Seric 				if (s != NULL && bitnset(rp[1], s->s_class))
70458825Seric 					goto backup;
70558825Seric 
70658825Seric 				/* fall through */
70758825Seric 
70856678Seric 			  case MATCHONE:
70956678Seric 			  case MATCHANY:
71056678Seric 				/* match exactly one token */
71158825Seric 				mlp->pattern = rvp;
71256678Seric 				mlp->first = avp;
71356678Seric 				mlp->last = avp++;
7148058Seric 				mlp++;
71556678Seric 				break;
7168058Seric 
71756678Seric 			  case MATCHZANY:
71856678Seric 				/* match zero or more tokens */
71958825Seric 				mlp->pattern = rvp;
72056678Seric 				mlp->first = avp;
72156678Seric 				mlp->last = avp - 1;
72256678Seric 				mlp++;
72356678Seric 				break;
72456326Seric 
72558827Seric 			  case MATCHZERO:
72658173Seric 				/* match zero tokens */
72758173Seric 				break;
72858173Seric 
72959027Seric 			  case MACRODEXPAND:
73059027Seric 				/*
73159027Seric 				**  Match against run-time macro.
73259027Seric 				**  This algorithm is broken for the
73359027Seric 				**  general case (no recursive macros,
73459027Seric 				**  improper tokenization) but should
73559027Seric 				**  work for the usual cases.
73659027Seric 				*/
73759027Seric 
73859027Seric 				ap = macvalue(rp[1], e);
73959027Seric 				mlp->first = avp;
74059027Seric 				if (tTd(21, 2))
74159027Seric 					printf("rewrite: LHS $&%c => \"%s\"\n",
74259027Seric 						rp[1],
74359027Seric 						ap == NULL ? "(NULL)" : ap);
74459027Seric 
74559027Seric 				if (ap == NULL)
74659027Seric 					break;
74760502Seric 				while (*ap != '\0')
74859027Seric 				{
74959027Seric 					if (*avp == NULL ||
75059027Seric 					    strncasecmp(ap, *avp, strlen(*avp)) != 0)
75159027Seric 					{
75259027Seric 						/* no match */
75359027Seric 						avp = mlp->first;
75459027Seric 						goto backup;
75559027Seric 					}
75659027Seric 					ap += strlen(*avp++);
75759027Seric 				}
75859027Seric 
75959027Seric 				/* match */
76059027Seric 				break;
76159027Seric 
76256678Seric 			  default:
76356678Seric 				/* must have exact match */
76456678Seric 				if (strcasecmp(rp, ap))
7658058Seric 					goto backup;
7664468Seric 				avp++;
76756678Seric 				break;
7683149Seric 			}
7693149Seric 
77056678Seric 			/* successful match on this token */
7713149Seric 			rvp++;
7723149Seric 			continue;
7733149Seric 
77458825Seric 	  backup:
77556678Seric 			/* match failed -- back up */
77658825Seric 			while (--mlp >= mlist)
7773149Seric 			{
77858825Seric 				rvp = mlp->pattern;
77956678Seric 				rp = *rvp;
78058825Seric 				avp = mlp->last + 1;
78158825Seric 				ap = *avp;
78258825Seric 
78358825Seric 				if (tTd(21, 36))
78458825Seric 				{
78558825Seric 					printf("BACKUP  rp=");
78658825Seric 					xputs(rp);
78758825Seric 					printf(", ap=");
78858825Seric 					xputs(ap);
78958825Seric 					printf("\n");
79058825Seric 				}
79158825Seric 
79258825Seric 				if (ap == NULL)
79358825Seric 				{
79458825Seric 					/* run off the end -- back up again */
79558825Seric 					continue;
79658825Seric 				}
79758050Seric 				if ((*rp & 0377) == MATCHANY ||
79858050Seric 				    (*rp & 0377) == MATCHZANY)
7994468Seric 				{
80056678Seric 					/* extend binding and continue */
80158825Seric 					mlp->last = avp++;
80256678Seric 					rvp++;
80358825Seric 					mlp++;
80456678Seric 					break;
8054468Seric 				}
80658825Seric 				if ((*rp & 0377) == MATCHCLASS)
80756678Seric 				{
80858814Seric 					/* extend binding and try again */
80963397Seric 					mlp->last = avp;
81058814Seric 					goto extendclass;
81158814Seric 				}
8123149Seric 			}
8133149Seric 
81458825Seric 			if (mlp < mlist)
81556678Seric 			{
81656678Seric 				/* total failure to match */
81756326Seric 				break;
8183149Seric 			}
819297Seric 		}
8203149Seric 
8213149Seric 		/*
82256678Seric 		**  See if we successfully matched
8233149Seric 		*/
8243149Seric 
82558827Seric 		if (mlp < mlist || *rvp != NULL)
8263149Seric 		{
8279374Seric 			if (tTd(21, 10))
8289374Seric 				printf("----- rule fails\n");
8299374Seric 			rwr = rwr->r_next;
83058866Seric 			ruleno++;
8319374Seric 			continue;
8329374Seric 		}
8333149Seric 
8349374Seric 		rvp = rwr->r_rhs;
8359374Seric 		if (tTd(21, 12))
8369374Seric 		{
8379374Seric 			printf("-----rule matches:");
83856678Seric 			printav(rvp);
8399374Seric 		}
8409374Seric 
8419374Seric 		rp = *rvp;
84258050Seric 		if ((*rp & 0377) == CANONUSER)
8439374Seric 		{
8449374Seric 			rvp++;
8459374Seric 			rwr = rwr->r_next;
84658866Seric 			ruleno++;
8479374Seric 		}
84858050Seric 		else if ((*rp & 0377) == CANONHOST)
8499374Seric 		{
8509374Seric 			rvp++;
8519374Seric 			rwr = NULL;
8529374Seric 		}
85358050Seric 		else if ((*rp & 0377) == CANONNET)
8549374Seric 			rwr = NULL;
8559374Seric 
8569374Seric 		/* substitute */
8579374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
8589374Seric 		{
8599374Seric 			register struct match *m;
8609374Seric 			register char **pp;
8619374Seric 
8628058Seric 			rp = *rvp;
86358050Seric 			if ((*rp & 0377) == MATCHREPL)
8648058Seric 			{
86516914Seric 				/* substitute from LHS */
86616914Seric 				m = &mlist[rp[1] - '1'];
86756678Seric 				if (m < mlist || m >= mlp)
8689374Seric 				{
86958151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
87056326Seric 						ruleset, rp[1]);
87159084Seric 					return EX_CONFIG;
8729374Seric 				}
87316914Seric 				if (tTd(21, 15))
87416914Seric 				{
87516914Seric 					printf("$%c:", rp[1]);
87616914Seric 					pp = m->first;
87756678Seric 					while (pp <= m->last)
87816914Seric 					{
87916914Seric 						printf(" %x=\"", *pp);
88016914Seric 						(void) fflush(stdout);
88116914Seric 						printf("%s\"", *pp++);
88216914Seric 					}
88316914Seric 					printf("\n");
88416914Seric 				}
8859374Seric 				pp = m->first;
88656678Seric 				while (pp <= m->last)
8873149Seric 				{
88816914Seric 					if (avp >= &npvp[MAXATOM])
88956678Seric 					{
89058151Seric 						syserr("554 rewrite: expansion too long");
89159084Seric 						return EX_DATAERR;
89256678Seric 					}
89316914Seric 					*avp++ = *pp++;
8943149Seric 				}
8953149Seric 			}
89616914Seric 			else
8978226Seric 			{
89816914Seric 				/* vanilla replacement */
8999374Seric 				if (avp >= &npvp[MAXATOM])
90016889Seric 				{
90156678Seric 	toolong:
90258151Seric 					syserr("554 rewrite: expansion too long");
90359084Seric 					return EX_DATAERR;
90416889Seric 				}
90559027Seric 				if ((*rp & 0377) != MACRODEXPAND)
90659027Seric 					*avp++ = rp;
90759027Seric 				else
90859027Seric 				{
90959027Seric 					*avp = macvalue(rp[1], e);
91059027Seric 					if (tTd(21, 2))
91159027Seric 						printf("rewrite: RHS $&%c => \"%s\"\n",
91259027Seric 							rp[1],
91359027Seric 							*avp == NULL ? "(NULL)" : *avp);
91459027Seric 					if (*avp != NULL)
91559027Seric 						avp++;
91659027Seric 				}
9178226Seric 			}
9189374Seric 		}
9199374Seric 		*avp++ = NULL;
92016914Seric 
92116914Seric 		/*
92256678Seric 		**  Check for any hostname/keyword lookups.
92316914Seric 		*/
92416914Seric 
92516914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
92616914Seric 		{
92756678Seric 			char **hbrvp;
92816914Seric 			char **xpvp;
92916914Seric 			int trsize;
93056678Seric 			char *replac;
93156678Seric 			int endtoken;
93256678Seric 			STAB *map;
93356678Seric 			char *mapname;
93456678Seric 			char **key_rvp;
93556678Seric 			char **arg_rvp;
93656678Seric 			char **default_rvp;
93756678Seric 			char buf[MAXNAME + 1];
93816914Seric 			char *pvpb1[MAXATOM + 1];
93956823Seric 			char *argvect[10];
94017174Seric 			char pvpbuf[PSBUFSIZE];
94116914Seric 
94258050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
94358050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
94416914Seric 				continue;
94516914Seric 
94616914Seric 			/*
94756678Seric 			**  Got a hostname/keyword lookup.
94816914Seric 			**
94916914Seric 			**	This could be optimized fairly easily.
95016914Seric 			*/
95116914Seric 
95216914Seric 			hbrvp = rvp;
95358050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
95456327Seric 			{
95556678Seric 				endtoken = HOSTEND;
95656678Seric 				mapname = "host";
95756327Seric 			}
95856326Seric 			else
95956327Seric 			{
96056678Seric 				endtoken = LOOKUPEND;
96156678Seric 				mapname = *++rvp;
96256327Seric 			}
96356678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
96456678Seric 			if (map == NULL)
96558151Seric 				syserr("554 rewrite: map %s not found", mapname);
96656678Seric 
96756678Seric 			/* extract the match part */
96856678Seric 			key_rvp = ++rvp;
96956823Seric 			default_rvp = NULL;
97056823Seric 			arg_rvp = argvect;
97156823Seric 			xpvp = NULL;
97256823Seric 			replac = pvpbuf;
97358050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
97453654Seric 			{
97558050Seric 				int nodetype = **rvp & 0377;
97656823Seric 
97756823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
97856678Seric 				{
97956823Seric 					rvp++;
98056823Seric 					continue;
98156823Seric 				}
98256823Seric 
98356823Seric 				*rvp++ = NULL;
98456823Seric 
98556823Seric 				if (xpvp != NULL)
98656823Seric 				{
98758814Seric 					cataddr(xpvp, NULL, replac,
98858082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
98958082Seric 						'\0');
99056823Seric 					*++arg_rvp = replac;
99156823Seric 					replac += strlen(replac) + 1;
99256823Seric 					xpvp = NULL;
99356823Seric 				}
99456823Seric 				switch (nodetype)
99556823Seric 				{
99656678Seric 				  case CANONHOST:
99756823Seric 					xpvp = rvp;
99856678Seric 					break;
99956678Seric 
100056678Seric 				  case CANONUSER:
100156678Seric 					default_rvp = rvp;
100256678Seric 					break;
100356678Seric 				}
100453654Seric 			}
100516914Seric 			if (*rvp != NULL)
100616914Seric 				*rvp++ = NULL;
100756823Seric 			if (xpvp != NULL)
100856823Seric 			{
100958814Seric 				cataddr(xpvp, NULL, replac,
101058082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
101158082Seric 					'\0');
101256823Seric 				*++arg_rvp = replac;
101356823Seric 			}
101456823Seric 			*++arg_rvp = NULL;
101516914Seric 
101616914Seric 			/* save the remainder of the input string */
101716914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
101816914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
101916914Seric 
102056678Seric 			/* look it up */
102158814Seric 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
102256823Seric 			argvect[0] = buf;
102360538Seric 			if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags))
102456836Seric 			{
102559084Seric 				auto int stat = EX_OK;
102656836Seric 
102760215Seric 				/* XXX should try to auto-open the map here */
102860215Seric 
102958796Seric 				if (tTd(60, 1))
103058796Seric 					printf("map_lookup(%s, %s) => ",
103158796Seric 						mapname, buf);
103256836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
103360089Seric 						buf, argvect, &stat);
103458796Seric 				if (tTd(60, 1))
103559084Seric 					printf("%s (%d)\n",
103659084Seric 						replac ? replac : "NOT FOUND",
103759084Seric 						stat);
103859084Seric 
103959084Seric 				/* should recover if stat == EX_TEMPFAIL */
104059084Seric 				if (stat == EX_TEMPFAIL)
104159084Seric 					rstat = stat;
104256836Seric 			}
104353654Seric 			else
104456678Seric 				replac = NULL;
104556678Seric 
104656678Seric 			/* if no replacement, use default */
104756823Seric 			if (replac == NULL && default_rvp != NULL)
104856823Seric 			{
104960089Seric 				/* create the default */
105060089Seric 				cataddr(default_rvp, NULL, buf, sizeof buf, '\0');
105156823Seric 				replac = buf;
105256823Seric 			}
105356823Seric 
105456678Seric 			if (replac == NULL)
105551317Seric 			{
105656823Seric 				xpvp = key_rvp;
105753654Seric 			}
105856678Seric 			else
105953654Seric 			{
106056678Seric 				/* scan the new replacement */
106158333Seric 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
106253654Seric 				if (xpvp == NULL)
106351317Seric 				{
106458403Seric 					/* prescan already printed error */
106559084Seric 					return EX_DATAERR;
106656678Seric 				}
106751317Seric 			}
106851317Seric 
106916914Seric 			/* append it to the token list */
107056678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
107156678Seric 			{
107217174Seric 				*avp++ = newstr(*xpvp);
107316920Seric 				if (avp >= &npvp[MAXATOM])
107416914Seric 					goto toolong;
107517174Seric 			}
107616914Seric 
107716914Seric 			/* restore the old trailing information */
107856678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
107916920Seric 				if (avp >= &npvp[MAXATOM])
108016914Seric 					goto toolong;
108117174Seric 
108256678Seric 			break;
108316914Seric 		}
108416914Seric 
108516914Seric 		/*
108616914Seric 		**  Check for subroutine calls.
108716914Seric 		*/
108816914Seric 
108958050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
109056678Seric 		{
109159084Seric 			int stat;
109259084Seric 
109356678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
109456678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
109556678Seric 			if (tTd(21, 3))
109656678Seric 				printf("-----callsubr %s\n", npvp[1]);
109759084Seric 			stat = rewrite(pvp, atoi(npvp[1]), e);
109859084Seric 			if (rstat == EX_OK || stat == EX_TEMPFAIL)
109959084Seric 				rstat = stat;
110063581Seric 			if ((**pvp & 0377) == CANONNET)
110163581Seric 				rwr = NULL;
110256678Seric 		}
110356678Seric 		else
110456678Seric 		{
110517348Seric 			bcopy((char *) npvp, (char *) pvp,
110616900Seric 				(int) (avp - npvp) * sizeof *avp);
110756678Seric 		}
11089374Seric 		if (tTd(21, 4))
11099374Seric 		{
11109374Seric 			printf("rewritten as:");
111156678Seric 			printav(pvp);
11129374Seric 		}
1113297Seric 	}
11148069Seric 
11159279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
11168069Seric 	{
11178959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
111856678Seric 		printav(pvp);
11198069Seric 	}
112059084Seric 
112159084Seric 	return rstat;
11223149Seric }
11233149Seric /*
11243149Seric **  BUILDADDR -- build address from token vector.
11253149Seric **
11263149Seric **	Parameters:
11273149Seric **		tv -- token vector.
11283149Seric **		a -- pointer to address descriptor to fill.
11293149Seric **			If NULL, one will be allocated.
113064284Seric **		flags -- info regarding whether this is a sender or
113164284Seric **			a recipient.
113258966Seric **		e -- the current envelope.
11333149Seric **
11343149Seric **	Returns:
11354279Seric **		NULL if there was an error.
11364279Seric **		'a' otherwise.
11373149Seric **
11383149Seric **	Side Effects:
11393149Seric **		fills in 'a'
11403149Seric */
11413149Seric 
114257249Seric struct errcodes
114357249Seric {
114457249Seric 	char	*ec_name;		/* name of error code */
114557249Seric 	int	ec_code;		/* numeric code */
114657249Seric } ErrorCodes[] =
114757249Seric {
114857249Seric 	"usage",	EX_USAGE,
114957249Seric 	"nouser",	EX_NOUSER,
115057249Seric 	"nohost",	EX_NOHOST,
115157249Seric 	"unavailable",	EX_UNAVAILABLE,
115257249Seric 	"software",	EX_SOFTWARE,
115357249Seric 	"tempfail",	EX_TEMPFAIL,
115457249Seric 	"protocol",	EX_PROTOCOL,
115557249Seric #ifdef EX_CONFIG
115657249Seric 	"config",	EX_CONFIG,
115757249Seric #endif
115857249Seric 	NULL,		EX_UNAVAILABLE,
115957249Seric };
116057249Seric 
116156678Seric ADDRESS *
116264284Seric buildaddr(tv, a, flags, e)
11633149Seric 	register char **tv;
11643149Seric 	register ADDRESS *a;
116564284Seric 	int flags;
116658966Seric 	register ENVELOPE *e;
11673149Seric {
11683149Seric 	struct mailer **mp;
11693149Seric 	register struct mailer *m;
117058008Seric 	char *bp;
117158008Seric 	int spaceleft;
1172*64306Seric 	static MAILER errormailer;
1173*64306Seric 	static char *errorargv[] = { "ERROR", NULL };
117457402Seric 	static char buf[MAXNAME];
11753149Seric 
11763149Seric 	if (a == NULL)
11773149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
117816889Seric 	bzero((char *) a, sizeof *a);
11793149Seric 
11803149Seric 	/* figure out what net/mailer to use */
1181*64306Seric 	if (*tv == NULL || (**tv & 0377) != CANONNET)
11824279Seric 	{
118358151Seric 		syserr("554 buildaddr: no net");
1184*64306Seric badaddr:
1185*64306Seric 		a->q_flags |= QBADADDR;
1186*64306Seric 		a->q_mailer = &errormailer;
1187*64306Seric 		if (errormailer.m_name == NULL)
1188*64306Seric 		{
1189*64306Seric 			/* initialize the bogus mailer */
1190*64306Seric 			errormailer.m_name = "*error*";
1191*64306Seric 			errormailer.m_mailer = "ERROR";
1192*64306Seric 			errormailer.m_argv = errorargv;
1193*64306Seric 		}
1194*64306Seric 		return a;
11954279Seric 	}
11963149Seric 	tv++;
119758680Seric 	if (strcasecmp(*tv, "error") == 0)
11984279Seric 	{
119958050Seric 		if ((**++tv & 0377) == CANONHOST)
120010183Seric 		{
120157249Seric 			register struct errcodes *ep;
120257249Seric 
120358050Seric 			if (isascii(**++tv) && isdigit(**tv))
120457249Seric 			{
120557249Seric 				setstat(atoi(*tv));
120657249Seric 			}
120757249Seric 			else
120857249Seric 			{
120957249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
121057249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
121157249Seric 						break;
121257249Seric 				setstat(ep->ec_code);
121357249Seric 			}
121410183Seric 			tv++;
121510183Seric 		}
121658050Seric 		if ((**tv & 0377) != CANONUSER)
121758151Seric 			syserr("554 buildaddr: error: no user");
121858814Seric 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
121958082Seric 		stripquotes(buf);
12204279Seric 		usrerr(buf);
1221*64306Seric 		goto badaddr;
12224279Seric 	}
122357402Seric 
12244598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
12253149Seric 	{
122658680Seric 		if (strcasecmp(m->m_name, *tv) == 0)
12273149Seric 			break;
12283149Seric 	}
12293149Seric 	if (m == NULL)
12304279Seric 	{
123158151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
1232*64306Seric 		goto badaddr;
12334279Seric 	}
12344598Seric 	a->q_mailer = m;
12353149Seric 
12363149Seric 	/* figure out what host (if any) */
123756678Seric 	tv++;
123858509Seric 	if ((**tv & 0377) == CANONHOST)
12393149Seric 	{
124058008Seric 		bp = buf;
124158008Seric 		spaceleft = sizeof buf - 1;
124258050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
124358008Seric 		{
124458008Seric 			int i = strlen(*tv);
124558008Seric 
124658008Seric 			if (i > spaceleft)
124758008Seric 			{
124858008Seric 				/* out of space for this address */
124958008Seric 				if (spaceleft >= 0)
125058151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
125158008Seric 						buf);
125258008Seric 				i = spaceleft;
125358008Seric 				spaceleft = 0;
125458008Seric 			}
125558008Seric 			if (i <= 0)
125658008Seric 				continue;
125758008Seric 			bcopy(*tv, bp, i);
125858008Seric 			bp += i;
125958008Seric 			spaceleft -= i;
126058008Seric 		}
126158010Seric 		*bp = '\0';
12625704Seric 		a->q_host = newstr(buf);
12633149Seric 	}
126457249Seric 	else
126558509Seric 	{
126658509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
126758509Seric 		{
126858509Seric 			syserr("554 buildaddr: no host");
1269*64306Seric 			goto badaddr;
127058509Seric 		}
127157249Seric 		a->q_host = NULL;
127258509Seric 	}
12733149Seric 
12743149Seric 	/* figure out the user */
127558050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
12764279Seric 	{
127758151Seric 		syserr("554 buildaddr: no user");
1278*64306Seric 		goto badaddr;
12794279Seric 	}
128057402Seric 	tv++;
128151317Seric 
128257402Seric 	/* do special mapping for local mailer */
128357402Seric 	if (m == LocalMailer && *tv != NULL)
128457402Seric 	{
128557454Seric 		register char *p = *tv;
128657454Seric 
128757454Seric 		if (*p == '"')
128857454Seric 			p++;
128957454Seric 		if (*p == '|')
129057402Seric 			a->q_mailer = m = ProgMailer;
129157454Seric 		else if (*p == '/')
129257402Seric 			a->q_mailer = m = FileMailer;
129357454Seric 		else if (*p == ':')
129457402Seric 		{
129557402Seric 			/* may be :include: */
129658814Seric 			cataddr(tv, NULL, buf, sizeof buf, '\0');
129758008Seric 			stripquotes(buf);
129858008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
129958008Seric 			{
130058008Seric 				/* if :include:, don't need further rewriting */
130157402Seric 				a->q_mailer = m = InclMailer;
130258008Seric 				a->q_user = &buf[9];
130358008Seric 				return (a);
130458008Seric 			}
130557402Seric 		}
130657402Seric 	}
130757402Seric 
130858008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
130958008Seric 	{
131058008Seric 		tv++;
131158008Seric 		a->q_flags |= QNOTREMOTE;
131258008Seric 	}
131358008Seric 
131464284Seric 	/* rewrite according recipient mailer rewriting rules */
131564284Seric 	define('h', a->q_host, e);
131664284Seric 	if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags))
131764284Seric 	{
131864284Seric 		/* sender addresses done later */
131964284Seric 		(void) rewrite(tv, 2, e);
132064284Seric 		if (m->m_re_rwset > 0)
132164284Seric 		       (void) rewrite(tv, m->m_re_rwset, e);
132264284Seric 	}
132359084Seric 	(void) rewrite(tv, 4, e);
132419040Seric 
132519040Seric 	/* save the result for the command line/RCPT argument */
132658814Seric 	cataddr(tv, NULL, buf, sizeof buf, '\0');
13273149Seric 	a->q_user = buf;
13283149Seric 
132958670Seric 	/*
133058670Seric 	**  Do mapping to lower case as requested by mailer
133158670Seric 	*/
133258670Seric 
133358670Seric 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
133458670Seric 		makelower(a->q_host);
133558670Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
133658670Seric 		makelower(a->q_user);
133758670Seric 
13383149Seric 	return (a);
13393149Seric }
13403188Seric /*
13414228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
13424228Seric **
13434228Seric **	Parameters:
13444228Seric **		pvp -- parameter vector to rebuild.
134558814Seric **		evp -- last parameter to include.  Can be NULL to
134658814Seric **			use entire pvp.
13474228Seric **		buf -- buffer to build the string into.
13484228Seric **		sz -- size of buf.
134958082Seric **		spacesub -- the space separator character; if null,
135058082Seric **			use SpaceSub.
13514228Seric **
13524228Seric **	Returns:
13534228Seric **		none.
13544228Seric **
13554228Seric **	Side Effects:
13564228Seric **		Destroys buf.
13574228Seric */
13584228Seric 
135958814Seric cataddr(pvp, evp, buf, sz, spacesub)
13604228Seric 	char **pvp;
136158814Seric 	char **evp;
13624228Seric 	char *buf;
13634228Seric 	register int sz;
136458082Seric 	char spacesub;
13654228Seric {
13664228Seric 	bool oatomtok = FALSE;
136756678Seric 	bool natomtok = FALSE;
13684228Seric 	register int i;
13694228Seric 	register char *p;
13704228Seric 
137158082Seric 	if (spacesub == '\0')
137258082Seric 		spacesub = SpaceSub;
137358082Seric 
13748423Seric 	if (pvp == NULL)
13758423Seric 	{
137623109Seric 		(void) strcpy(buf, "");
13778423Seric 		return;
13788423Seric 	}
13794228Seric 	p = buf;
138011156Seric 	sz -= 2;
13814228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
13824228Seric 	{
13838078Seric 		natomtok = (toktype(**pvp) == ATM);
13844228Seric 		if (oatomtok && natomtok)
138558082Seric 			*p++ = spacesub;
13864228Seric 		(void) strcpy(p, *pvp);
13874228Seric 		oatomtok = natomtok;
13884228Seric 		p += i;
138911156Seric 		sz -= i + 1;
139058814Seric 		if (pvp++ == evp)
139158814Seric 			break;
13924228Seric 	}
13934228Seric 	*p = '\0';
13944228Seric }
13954228Seric /*
13963188Seric **  SAMEADDR -- Determine if two addresses are the same
13973188Seric **
13983188Seric **	This is not just a straight comparison -- if the mailer doesn't
13993188Seric **	care about the host we just ignore it, etc.
14003188Seric **
14013188Seric **	Parameters:
14023188Seric **		a, b -- pointers to the internal forms to compare.
14033188Seric **
14043188Seric **	Returns:
14053188Seric **		TRUE -- they represent the same mailbox.
14063188Seric **		FALSE -- they don't.
14073188Seric **
14083188Seric **	Side Effects:
14093188Seric **		none.
14103188Seric */
14113188Seric 
14123188Seric bool
14139374Seric sameaddr(a, b)
14143188Seric 	register ADDRESS *a;
14153188Seric 	register ADDRESS *b;
14163188Seric {
14173188Seric 	/* if they don't have the same mailer, forget it */
14183188Seric 	if (a->q_mailer != b->q_mailer)
14193188Seric 		return (FALSE);
14203188Seric 
14213188Seric 	/* if the user isn't the same, we can drop out */
142256678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
14233188Seric 		return (FALSE);
14243188Seric 
142558438Seric 	/* if we have good uids for both but the differ, these are different */
142658438Seric 	if (bitset(QGOODUID, a->q_flags & b->q_flags) && a->q_uid != b->q_uid)
142758438Seric 		return (FALSE);
142858438Seric 
142958509Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
143058509Seric 	if (a->q_host == b->q_host)
143158509Seric 	{
143258509Seric 		/* probably both null pointers */
14333188Seric 		return (TRUE);
143458509Seric 	}
14353188Seric 	if (a->q_host == NULL || b->q_host == NULL)
143658509Seric 	{
143758509Seric 		/* only one is a null pointer */
14383188Seric 		return (FALSE);
143958509Seric 	}
144056678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
14413188Seric 		return (FALSE);
14423188Seric 
14433188Seric 	return (TRUE);
14443188Seric }
14453234Seric /*
14463234Seric **  PRINTADDR -- print address (for debugging)
14473234Seric **
14483234Seric **	Parameters:
14493234Seric **		a -- the address to print
14503234Seric **		follow -- follow the q_next chain.
14513234Seric **
14523234Seric **	Returns:
14533234Seric **		none.
14543234Seric **
14553234Seric **	Side Effects:
14563234Seric **		none.
14573234Seric */
14583234Seric 
14593234Seric printaddr(a, follow)
14603234Seric 	register ADDRESS *a;
14613234Seric 	bool follow;
14623234Seric {
14635001Seric 	bool first = TRUE;
146457731Seric 	register MAILER *m;
146557731Seric 	MAILER pseudomailer;
14665001Seric 
14673234Seric 	while (a != NULL)
14683234Seric 	{
14695001Seric 		first = FALSE;
14704443Seric 		printf("%x=", a);
14714085Seric 		(void) fflush(stdout);
147257731Seric 
147357731Seric 		/* find the mailer -- carefully */
147457731Seric 		m = a->q_mailer;
147557731Seric 		if (m == NULL)
147657731Seric 		{
147757731Seric 			m = &pseudomailer;
147857731Seric 			m->m_mno = -1;
147957731Seric 			m->m_name = "NULL";
148057731Seric 		}
148157731Seric 
148258680Seric 		printf("%s:\n\tmailer %d (%s), host `%s', user `%s', ruser `%s'\n",
148357731Seric 		       a->q_paddr, m->m_mno, m->m_name,
148463756Seric 		       a->q_host, a->q_user,
148563756Seric 		       a->q_ruser ? a->q_ruser : "<null>");
148659269Seric 		printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n",
148759269Seric 		       a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid);
148859269Seric 		printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n",
148959269Seric 		       a->q_owner == NULL ? "(none)" : a->q_owner,
149063756Seric 		       a->q_home == NULL ? "(none)" : a->q_home,
149163756Seric 		       a->q_fullname == NULL ? "(none)" : a->q_fullname);
14924996Seric 
14933234Seric 		if (!follow)
14943234Seric 			return;
14954996Seric 		a = a->q_next;
14963234Seric 	}
14975001Seric 	if (first)
14984443Seric 		printf("[NULL]\n");
14993234Seric }
15004317Seric 
15017682Seric /*
15027682Seric **  REMOTENAME -- return the name relative to the current mailer
15037682Seric **
15047682Seric **	Parameters:
15057682Seric **		name -- the name to translate.
15068069Seric **		m -- the mailer that we want to do rewriting relative
15078069Seric **			to.
150859163Seric **		flags -- fine tune operations.
150959163Seric **		pstat -- pointer to status word.
151058020Seric **		e -- the current envelope.
15117682Seric **
15127682Seric **	Returns:
15137682Seric **		the text string representing this address relative to
15147682Seric **			the receiving mailer.
15157682Seric **
15167682Seric **	Side Effects:
15177682Seric **		none.
15187682Seric **
15197682Seric **	Warnings:
15207682Seric **		The text string returned is tucked away locally;
15217682Seric **			copy it if you intend to save it.
15227682Seric */
15237682Seric 
15247682Seric char *
152559163Seric remotename(name, m, flags, pstat, e)
15267682Seric 	char *name;
152756678Seric 	struct mailer *m;
152859163Seric 	int flags;
152959163Seric 	int *pstat;
153056678Seric 	register ENVELOPE *e;
15317682Seric {
15328069Seric 	register char **pvp;
15338069Seric 	char *fancy;
153456678Seric 	char *oldg = macvalue('g', e);
153558020Seric 	int rwset;
15367682Seric 	static char buf[MAXNAME];
15377682Seric 	char lbuf[MAXNAME];
153816914Seric 	char pvpbuf[PSBUFSIZE];
153956678Seric 	extern char *crackaddr();
15407682Seric 
15417755Seric 	if (tTd(12, 1))
15427755Seric 		printf("remotename(%s)\n", name);
15437755Seric 
154410177Seric 	/* don't do anything if we are tagging it as special */
154559163Seric 	if (bitset(RF_SENDERADDR, flags))
154659163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
154759163Seric 						     : m->m_se_rwset;
154858020Seric 	else
154959163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
155059163Seric 						     : m->m_re_rwset;
155158020Seric 	if (rwset < 0)
155210177Seric 		return (name);
155310177Seric 
15547682Seric 	/*
15558181Seric 	**  Do a heuristic crack of this name to extract any comment info.
15568181Seric 	**	This will leave the name as a comment and a $g macro.
15577889Seric 	*/
15587889Seric 
155959163Seric 	if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags))
156058050Seric 		fancy = "\201g";
156110310Seric 	else
156210310Seric 		fancy = crackaddr(name);
15637889Seric 
15648181Seric 	/*
15658181Seric 	**  Turn the name into canonical form.
15668181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
15678181Seric 	**	If this only resolves to "user", and the "C" flag is
15688181Seric 	**	specified in the sending mailer, then the sender's
15698181Seric 	**	domain will be appended.
15708181Seric 	*/
15718181Seric 
157258333Seric 	pvp = prescan(name, '\0', pvpbuf, NULL);
15737889Seric 	if (pvp == NULL)
15747889Seric 		return (name);
157559163Seric 	if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
157659163Seric 		*pstat = EX_TEMPFAIL;
157759163Seric 	if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
15788181Seric 	{
15798181Seric 		/* append from domain to this address */
15808181Seric 		register char **pxp = pvp;
15818181Seric 
15829594Seric 		/* see if there is an "@domain" in the current name */
15838181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
15848181Seric 			pxp++;
15858181Seric 		if (*pxp == NULL)
15868181Seric 		{
15879594Seric 			/* no.... append the "@domain" from the sender */
158856678Seric 			register char **qxq = e->e_fromdomain;
15898181Seric 
15909594Seric 			while ((*pxp++ = *qxq++) != NULL)
15919594Seric 				continue;
159259163Seric 			if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
159359163Seric 				*pstat = EX_TEMPFAIL;
15948181Seric 		}
15958181Seric 	}
15968181Seric 
15978181Seric 	/*
15988959Seric 	**  Do more specific rewriting.
159956678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
160056678Seric 	**		a sender address or not.
16018181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
16028181Seric 	*/
16038181Seric 
160459163Seric 	if (bitset(RF_SENDERADDR, flags))
160559541Seric 	{
160659163Seric 		if (rewrite(pvp, 1, e) == EX_TEMPFAIL)
160759163Seric 			*pstat = EX_TEMPFAIL;
160859541Seric 	}
16098069Seric 	else
161059541Seric 	{
161159163Seric 		if (rewrite(pvp, 2, e) == EX_TEMPFAIL)
161259163Seric 			*pstat = EX_TEMPFAIL;
161359541Seric 	}
161458020Seric 	if (rwset > 0)
161559541Seric 	{
161659163Seric 		if (rewrite(pvp, rwset, e) == EX_TEMPFAIL)
161759163Seric 			*pstat = EX_TEMPFAIL;
161859541Seric 	}
16197682Seric 
16208181Seric 	/*
16218959Seric 	**  Do any final sanitation the address may require.
16228959Seric 	**	This will normally be used to turn internal forms
16238959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
16248959Seric 	**	may be used as a default to the above rules.
16258959Seric 	*/
16268959Seric 
162759163Seric 	if (rewrite(pvp, 4, e) == EX_TEMPFAIL)
162859163Seric 		*pstat = EX_TEMPFAIL;
16298959Seric 
16308959Seric 	/*
16318181Seric 	**  Now restore the comment information we had at the beginning.
16328181Seric 	*/
16338181Seric 
163458825Seric 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
163556678Seric 	define('g', lbuf, e);
163656678Seric 	expand(fancy, buf, &buf[sizeof buf - 1], e);
163756678Seric 	define('g', oldg, e);
16387682Seric 
16397682Seric 	if (tTd(12, 1))
16407755Seric 		printf("remotename => `%s'\n", buf);
16417682Seric 	return (buf);
16427682Seric }
164351317Seric /*
164456678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
164551317Seric **
164651317Seric **	Parameters:
164756678Seric **		a -- the address to map (but just the user name part).
164856678Seric **		sendq -- the sendq in which to install any replacement
164956678Seric **			addresses.
165051317Seric **
165151317Seric **	Returns:
165251317Seric **		none.
165351317Seric */
165451317Seric 
165556678Seric maplocaluser(a, sendq, e)
165656678Seric 	register ADDRESS *a;
165756678Seric 	ADDRESS **sendq;
165856678Seric 	ENVELOPE *e;
165951317Seric {
166056678Seric 	register char **pvp;
166156678Seric 	register ADDRESS *a1 = NULL;
166258333Seric 	auto char *delimptr;
166356678Seric 	char pvpbuf[PSBUFSIZE];
166451317Seric 
166556678Seric 	if (tTd(29, 1))
166656678Seric 	{
166756678Seric 		printf("maplocaluser: ");
166856678Seric 		printaddr(a, FALSE);
166956678Seric 	}
167058333Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, &delimptr);
167156678Seric 	if (pvp == NULL)
167256678Seric 		return;
167351317Seric 
167459084Seric 	(void) rewrite(pvp, 5, e);
167558050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
167656678Seric 		return;
167751317Seric 
167856678Seric 	/* if non-null, mailer destination specified -- has it changed? */
167964284Seric 	a1 = buildaddr(pvp, NULL, 0, e);
168056678Seric 	if (a1 == NULL || sameaddr(a, a1))
168156678Seric 		return;
168251317Seric 
168356678Seric 	/* mark old address as dead; insert new address */
168456678Seric 	a->q_flags |= QDONTSEND;
168557731Seric 	if (tTd(29, 5))
168657731Seric 	{
168757731Seric 		printf("maplocaluser: QDONTSEND ");
168857731Seric 		printaddr(a, FALSE);
168957731Seric 	}
169056678Seric 	a1->q_alias = a;
169164284Seric 	allocaddr(a1, RF_COPYALL, NULL, delimptr);
169256678Seric 	(void) recipient(a1, sendq, e);
169351317Seric }
169458802Seric /*
169558802Seric **  DEQUOTE_INIT -- initialize dequote map
169658802Seric **
169758802Seric **	This is a no-op.
169858802Seric **
169958802Seric **	Parameters:
170058802Seric **		map -- the internal map structure.
170158802Seric **		args -- arguments.
170258802Seric **
170358802Seric **	Returns:
170458802Seric **		TRUE.
170558802Seric */
170658802Seric 
170758802Seric bool
170860219Seric dequote_init(map, args)
170958802Seric 	MAP *map;
171058802Seric 	char *args;
171158802Seric {
171258805Seric 	register char *p = args;
171358805Seric 
171458805Seric 	for (;;)
171558805Seric 	{
171658805Seric 		while (isascii(*p) && isspace(*p))
171758805Seric 			p++;
171858805Seric 		if (*p != '-')
171958805Seric 			break;
172058805Seric 		switch (*++p)
172158805Seric 		{
172258805Seric 		  case 'a':
172358805Seric 			map->map_app = ++p;
172458805Seric 			break;
172558805Seric 		}
172658805Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
172758805Seric 			p++;
172858805Seric 		if (*p != '\0')
172958805Seric 			*p = '\0';
173058805Seric 	}
173158805Seric 	if (map->map_app != NULL)
173258805Seric 		map->map_app = newstr(map->map_app);
173358805Seric 
173458802Seric 	return TRUE;
173558802Seric }
173658802Seric /*
173758802Seric **  DEQUOTE_MAP -- unquote an address
173858802Seric **
173958802Seric **	Parameters:
174058802Seric **		map -- the internal map structure (ignored).
174160089Seric **		name -- the name to dequote.
174258802Seric **		av -- arguments (ignored).
174359084Seric **		statp -- pointer to status out-parameter.
174458802Seric **
174558802Seric **	Returns:
174658802Seric **		NULL -- if there were no quotes, or if the resulting
174758802Seric **			unquoted buffer would not be acceptable to prescan.
174858802Seric **		else -- The dequoted buffer.
174958802Seric */
175058802Seric 
175158802Seric char *
175260089Seric dequote_map(map, name, av, statp)
175358802Seric 	MAP *map;
175460089Seric 	char *name;
175558802Seric 	char **av;
175659084Seric 	int *statp;
175758802Seric {
175858802Seric 	register char *p;
175958802Seric 	register char *q;
176058802Seric 	register char c;
176158802Seric 	int anglecnt;
176258802Seric 	int cmntcnt;
176358802Seric 	int quotecnt;
176459089Seric 	int spacecnt;
176558802Seric 	bool quotemode;
176658802Seric 	bool bslashmode;
176758802Seric 
176858802Seric 	anglecnt = 0;
176958802Seric 	cmntcnt = 0;
177058802Seric 	quotecnt = 0;
177159089Seric 	spacecnt = 0;
177258802Seric 	quotemode = FALSE;
177358802Seric 	bslashmode = FALSE;
177458802Seric 
177560089Seric 	for (p = q = name; (c = *p++) != '\0'; )
177658802Seric 	{
177758802Seric 		if (bslashmode)
177858802Seric 		{
177958802Seric 			bslashmode = FALSE;
178058802Seric 			*q++ = c;
178158802Seric 			continue;
178258802Seric 		}
178358802Seric 
178458802Seric 		switch (c)
178558802Seric 		{
178658802Seric 		  case '\\':
178758802Seric 			bslashmode = TRUE;
178858802Seric 			break;
178958802Seric 
179058802Seric 		  case '(':
179158802Seric 			cmntcnt++;
179258802Seric 			break;
179358802Seric 
179458802Seric 		  case ')':
179558802Seric 			if (cmntcnt-- <= 0)
179658802Seric 				return NULL;
179758802Seric 			break;
179859089Seric 
179959089Seric 		  case ' ':
180059089Seric 			spacecnt++;
180159089Seric 			break;
180258802Seric 		}
180358802Seric 
180458802Seric 		if (cmntcnt > 0)
180558802Seric 		{
180658802Seric 			*q++ = c;
180758802Seric 			continue;
180858802Seric 		}
180958802Seric 
181058802Seric 		switch (c)
181158802Seric 		{
181258802Seric 		  case '"':
181358802Seric 			quotemode = !quotemode;
181458802Seric 			quotecnt++;
181558802Seric 			continue;
181658802Seric 
181758802Seric 		  case '<':
181858802Seric 			anglecnt++;
181958802Seric 			break;
182058802Seric 
182158802Seric 		  case '>':
182258802Seric 			if (anglecnt-- <= 0)
182358802Seric 				return NULL;
182458802Seric 			break;
182558802Seric 		}
182658802Seric 		*q++ = c;
182758802Seric 	}
182858802Seric 
182958802Seric 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
183059089Seric 	    quotemode || quotecnt <= 0 || spacecnt != 0)
183158802Seric 		return NULL;
183258802Seric 	*q++ = '\0';
183360089Seric 	return name;
183458802Seric }
1835