122976Smiriam /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333730Sbostic  * Copyright (c) 1988 Regents of the University of California.
433730Sbostic  * All rights reserved.
533730Sbostic  *
642828Sbostic  * %sccs.include.redist.c%
733730Sbostic  */
822976Smiriam 
922976Smiriam #ifndef lint
10*60219Seric static char sccsid[] = "@(#)parseaddr.c	6.56 (Berkeley) 05/22/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.
33297Seric **		copyf -- determines what shall be copied:
34297Seric **			-1 -- don't copy anything.  The printname
35297Seric **				(q_paddr) is just addr, and the
36297Seric **				user & host are allocated internally
37297Seric **				to parse.
38297Seric **			0 -- copy out the parsed user & host, but
39297Seric **				don't copy the printname.
40297Seric **			+1 -- copy everything.
4111445Seric **		delim -- the character to terminate the address, passed
4211445Seric **			to prescan.
4358333Seric **		delimptr -- if non-NULL, set to the location of the
4458333Seric **			delim character that was found.
4556678Seric **		e -- the envelope that will contain this address.
46297Seric **
47297Seric **	Returns:
48297Seric **		A pointer to the address descriptor header (`a' if
49297Seric **			`a' is non-NULL).
50297Seric **		NULL on error.
51297Seric **
52297Seric **	Side Effects:
53297Seric **		none
54297Seric */
55297Seric 
569374Seric /* following delimiters are inherent to the internal algorithms */
5759278Seric # define DELIMCHARS	"()<>,;\r\n"	/* default word delimiters */
582091Seric 
592973Seric ADDRESS *
6058333Seric parseaddr(addr, a, copyf, delim, delimptr, e)
61297Seric 	char *addr;
622973Seric 	register ADDRESS *a;
63297Seric 	int copyf;
6459700Seric 	int delim;
6558333Seric 	char **delimptr;
6656678Seric 	register ENVELOPE *e;
67297Seric {
683149Seric 	register char **pvp;
6958333Seric 	auto char *delimptrbuf;
7059084Seric 	bool queueup;
7116914Seric 	char pvpbuf[PSBUFSIZE];
7256678Seric 	extern char **prescan();
7356678Seric 	extern ADDRESS *buildaddr();
7457388Seric 	extern bool invalidaddr();
75297Seric 
76297Seric 	/*
77297Seric 	**  Initialize and prescan address.
78297Seric 	*/
79297Seric 
8056678Seric 	e->e_to = addr;
817675Seric 	if (tTd(20, 1))
829888Seric 		printf("\n--parseaddr(%s)\n", addr);
833188Seric 
8457388Seric 	if (invalidaddr(addr))
8557388Seric 	{
8657388Seric 		if (tTd(20, 1))
8757388Seric 			printf("parseaddr-->bad address\n");
8857388Seric 		return NULL;
8957388Seric 	}
9057388Seric 
9158333Seric 	if (delimptr == NULL)
9258333Seric 		delimptr = &delimptrbuf;
9358333Seric 
9458333Seric 	pvp = prescan(addr, delim, pvpbuf, delimptr);
953149Seric 	if (pvp == NULL)
9656729Seric 	{
9756729Seric 		if (tTd(20, 1))
9856729Seric 			printf("parseaddr-->NULL\n");
99297Seric 		return (NULL);
10056729Seric 	}
101297Seric 
102297Seric 	/*
1033149Seric 	**  Apply rewriting rules.
1047889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
105297Seric 	*/
106297Seric 
10759084Seric 	queueup = FALSE;
10859084Seric 	if (rewrite(pvp, 3, e) == EX_TEMPFAIL)
10959084Seric 		queueup = TRUE;
11059084Seric 	if (rewrite(pvp, 0, e) == EX_TEMPFAIL)
11159084Seric 		queueup = TRUE;
112297Seric 
1133149Seric 	/*
1143149Seric 	**  See if we resolved to a real mailer.
1153149Seric 	*/
116297Seric 
11759040Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
1183149Seric 	{
1193149Seric 		setstat(EX_USAGE);
12058151Seric 		syserr("554 cannot resolve name");
1213149Seric 		return (NULL);
122297Seric 	}
123297Seric 
124297Seric 	/*
1253149Seric 	**  Build canonical address from pvp.
126297Seric 	*/
127297Seric 
12858966Seric 	a = buildaddr(pvp, a, e);
1294279Seric 	if (a == NULL)
1304279Seric 		return (NULL);
131297Seric 
132297Seric 	/*
1333149Seric 	**  Make local copies of the host & user and then
1343149Seric 	**  transport them out.
135297Seric 	*/
136297Seric 
13758333Seric 	allocaddr(a, copyf, addr, *delimptr);
13856678Seric 
13956678Seric 	/*
14059084Seric 	**  If there was a parsing failure, mark it for queueing.
14159084Seric 	*/
14259084Seric 
14359084Seric 	if (queueup)
14459595Seric 	{
14559734Seric 		char *msg = "Transient parse error -- message queued for future delivery";
14659734Seric 
14759595Seric 		if (tTd(20, 1))
14859595Seric 			printf("parseaddr: queuing message\n");
14959734Seric 		message(msg);
15059734Seric 		if (e->e_message == NULL)
15160009Seric 			e->e_message = newstr(msg);
15259084Seric 		a->q_flags |= QQUEUEUP;
15359595Seric 	}
15459084Seric 
15559084Seric 	/*
15656678Seric 	**  Compute return value.
15756678Seric 	*/
15856678Seric 
15956678Seric 	if (tTd(20, 1))
1608078Seric 	{
16156678Seric 		printf("parseaddr-->");
16256678Seric 		printaddr(a, FALSE);
16356678Seric 	}
16456678Seric 
16556678Seric 	return (a);
16656678Seric }
16756678Seric /*
16857388Seric **  INVALIDADDR -- check for address containing meta-characters
16957388Seric **
17057388Seric **	Parameters:
17157388Seric **		addr -- the address to check.
17257388Seric **
17357388Seric **	Returns:
17457388Seric **		TRUE -- if the address has any "wierd" characters
17557388Seric **		FALSE -- otherwise.
17657388Seric */
17757388Seric 
17857388Seric bool
17957388Seric invalidaddr(addr)
18057388Seric 	register char *addr;
18157388Seric {
18257388Seric 	for (; *addr != '\0'; addr++)
18357388Seric 	{
18458050Seric 		if ((*addr & 0340) != 0200)
18557388Seric 			continue;
18657388Seric 		setstat(EX_USAGE);
18758151Seric 		usrerr("553 Address contained invalid control characters");
18857388Seric 		return TRUE;
18957388Seric 	}
19057388Seric 	return FALSE;
19157388Seric }
19257388Seric /*
19356678Seric **  ALLOCADDR -- do local allocations of address on demand.
19456678Seric **
19556678Seric **	Also lowercases the host name if requested.
19656678Seric **
19756678Seric **	Parameters:
19856678Seric **		a -- the address to reallocate.
19956678Seric **		copyf -- the copy flag (see parseaddr for description).
20056678Seric **		paddr -- the printname of the address.
20158333Seric **		delimptr -- a pointer to the address delimiter.  Must be set.
20256678Seric **
20356678Seric **	Returns:
20456678Seric **		none.
20556678Seric **
20656678Seric **	Side Effects:
20756678Seric **		Copies portions of a into local buffers as requested.
20856678Seric */
20956678Seric 
21058333Seric allocaddr(a, copyf, paddr, delimptr)
21156678Seric 	register ADDRESS *a;
21256678Seric 	int copyf;
21356678Seric 	char *paddr;
21458333Seric 	char *delimptr;
21556678Seric {
21656678Seric 	register MAILER *m = a->q_mailer;
21756678Seric 
21858673Seric 	if (tTd(24, 4))
21958673Seric 		printf("allocaddr(copyf=%d, paddr=%s)\n", copyf, paddr);
22058673Seric 
22156678Seric 	if (copyf > 0 && paddr != NULL)
22256678Seric 	{
22358333Seric 		char savec = *delimptr;
2248078Seric 
22559747Seric 		if (savec != '\0')
22659747Seric 			*delimptr = '\0';
22756678Seric 		a->q_paddr = newstr(paddr);
22859747Seric 		if (savec != '\0')
22959747Seric 			*delimptr = savec;
2308078Seric 	}
231297Seric 	else
23256678Seric 		a->q_paddr = paddr;
23324944Seric 
23424944Seric 	if (a->q_user == NULL)
23524944Seric 		a->q_user = "";
23624944Seric 	if (a->q_host == NULL)
23724944Seric 		a->q_host = "";
23824944Seric 
2393149Seric 	if (copyf >= 0)
240297Seric 	{
24124944Seric 		a->q_host = newstr(a->q_host);
2423149Seric 		if (a->q_user != a->q_paddr)
2433149Seric 			a->q_user = newstr(a->q_user);
244297Seric 	}
245297Seric 
24656678Seric 	if (a->q_paddr == NULL)
24756678Seric 		a->q_paddr = a->q_user;
248297Seric }
249297Seric /*
250297Seric **  PRESCAN -- Prescan name and make it canonical
251297Seric **
2529374Seric **	Scans a name and turns it into a set of tokens.  This process
2539374Seric **	deletes blanks and comments (in parentheses).
254297Seric **
255297Seric **	This routine knows about quoted strings and angle brackets.
256297Seric **
257297Seric **	There are certain subtleties to this routine.  The one that
258297Seric **	comes to mind now is that backslashes on the ends of names
259297Seric **	are silently stripped off; this is intentional.  The problem
260297Seric **	is that some versions of sndmsg (like at LBL) set the kill
261297Seric **	character to something other than @ when reading addresses;
262297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
263297Seric **	berknet mailer.
264297Seric **
265297Seric **	Parameters:
266297Seric **		addr -- the name to chomp.
267297Seric **		delim -- the delimiter for the address, normally
268297Seric **			'\0' or ','; \0 is accepted in any case.
26915284Seric **			If '\t' then we are reading the .cf file.
27016914Seric **		pvpbuf -- place to put the saved text -- note that
27116914Seric **			the pointers are static.
27258333Seric **		delimptr -- if non-NULL, set to the location of the
27358333Seric **			terminating delimiter.
274297Seric **
275297Seric **	Returns:
2763149Seric **		A pointer to a vector of tokens.
277297Seric **		NULL on error.
278297Seric */
279297Seric 
2808078Seric /* states and character types */
2818078Seric # define OPR		0	/* operator */
2828078Seric # define ATM		1	/* atom */
2838078Seric # define QST		2	/* in quoted string */
2848078Seric # define SPC		3	/* chewing up spaces */
2858078Seric # define ONE		4	/* pick up one character */
2863149Seric 
2878078Seric # define NSTATES	5	/* number of states */
2888078Seric # define TYPE		017	/* mask to select state type */
2898078Seric 
2908078Seric /* meta bits for table */
2918078Seric # define M		020	/* meta character; don't pass through */
2928078Seric # define B		040	/* cause a break */
2938078Seric # define MB		M|B	/* meta-break */
2948078Seric 
2958078Seric static short StateTab[NSTATES][NSTATES] =
2968078Seric {
2978087Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	*/
2989051Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,
2999051Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,
3009051Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,
3018078Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,
3028078Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,
3038078Seric };
3048078Seric 
3058078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
3068078Seric 
3073149Seric char **
30858333Seric prescan(addr, delim, pvpbuf, delimptr)
309297Seric 	char *addr;
310297Seric 	char delim;
31116914Seric 	char pvpbuf[];
31258333Seric 	char **delimptr;
313297Seric {
314297Seric 	register char *p;
3158078Seric 	register char *q;
3169346Seric 	register int c;
3173149Seric 	char **avp;
318297Seric 	bool bslashmode;
319297Seric 	int cmntcnt;
3208423Seric 	int anglecnt;
3213149Seric 	char *tok;
3228078Seric 	int state;
3238078Seric 	int newstate;
32459747Seric 	char *saveto = CurEnv->e_to;
3258078Seric 	static char *av[MAXATOM+1];
32656678Seric 	extern int errno;
327297Seric 
32815253Seric 	/* make sure error messages don't have garbage on them */
32915253Seric 	errno = 0;
33015253Seric 
33116914Seric 	q = pvpbuf;
3323149Seric 	bslashmode = FALSE;
3337800Seric 	cmntcnt = 0;
3348423Seric 	anglecnt = 0;
3353149Seric 	avp = av;
33656678Seric 	state = ATM;
3378078Seric 	c = NOCHAR;
3388078Seric 	p = addr;
33959747Seric 	CurEnv->e_to = p;
34056764Seric 	if (tTd(22, 11))
341297Seric 	{
3428078Seric 		printf("prescan: ");
3438078Seric 		xputs(p);
34423109Seric 		(void) putchar('\n');
3458078Seric 	}
3468078Seric 
3478078Seric 	do
3488078Seric 	{
3493149Seric 		/* read a token */
3503149Seric 		tok = q;
3518078Seric 		for (;;)
352297Seric 		{
3538078Seric 			/* store away any old lookahead character */
35459277Seric 			if (c != NOCHAR && !bslashmode)
3558078Seric 			{
35615284Seric 				/* see if there is room */
35716914Seric 				if (q >= &pvpbuf[PSBUFSIZE - 5])
3588078Seric 				{
35958151Seric 					usrerr("553 Address too long");
36058333Seric 					if (delimptr != NULL)
36158333Seric 						*delimptr = p;
36259747Seric 					CurEnv->e_to = saveto;
3638078Seric 					return (NULL);
3648078Seric 				}
36515284Seric 
36615284Seric 				/* squirrel it away */
3678078Seric 				*q++ = c;
3688078Seric 			}
3698078Seric 
3708078Seric 			/* read a new input character */
3718078Seric 			c = *p++;
37257631Seric 			if (c == '\0')
37356764Seric 			{
37456764Seric 				/* diagnose and patch up bad syntax */
37556764Seric 				if (state == QST)
37656764Seric 				{
37758151Seric 					usrerr("553 Unbalanced '\"'");
37856764Seric 					c = '"';
37956764Seric 				}
38056764Seric 				else if (cmntcnt > 0)
38156764Seric 				{
38258151Seric 					usrerr("553 Unbalanced '('");
38356764Seric 					c = ')';
38456764Seric 				}
38556764Seric 				else if (anglecnt > 0)
38656764Seric 				{
38756764Seric 					c = '>';
38858151Seric 					usrerr("553 Unbalanced '<'");
38956764Seric 				}
39056764Seric 				else
39156764Seric 					break;
39215284Seric 
39356764Seric 				p--;
39456764Seric 			}
39557631Seric 			else if (c == delim && anglecnt <= 0 &&
39657631Seric 					cmntcnt <= 0 && state != QST)
39757631Seric 				break;
39856764Seric 
3998078Seric 			if (tTd(22, 101))
4008078Seric 				printf("c=%c, s=%d; ", c, state);
4018078Seric 
4023149Seric 			/* chew up special characters */
4033149Seric 			*q = '\0';
4043149Seric 			if (bslashmode)
4053149Seric 			{
40659105Seric 				bslashmode = FALSE;
40759105Seric 
40824944Seric 				/* kludge \! for naive users */
40958061Seric 				if (cmntcnt > 0)
41059105Seric 				{
41158061Seric 					c = NOCHAR;
41259105Seric 					continue;
41359105Seric 				}
41459105Seric 				else if (c != '!' || state == QST)
41559105Seric 				{
41656678Seric 					*q++ = '\\';
41759105Seric 					continue;
41859105Seric 				}
4193149Seric 			}
42056678Seric 
42156678Seric 			if (c == '\\')
4223149Seric 			{
4233149Seric 				bslashmode = TRUE;
4243149Seric 			}
42556678Seric 			else if (state == QST)
4268514Seric 			{
4278514Seric 				/* do nothing, just avoid next clauses */
4288514Seric 			}
4298078Seric 			else if (c == '(')
4304100Seric 			{
4318078Seric 				cmntcnt++;
4328078Seric 				c = NOCHAR;
4334100Seric 			}
4348078Seric 			else if (c == ')')
4353149Seric 			{
4368078Seric 				if (cmntcnt <= 0)
4373149Seric 				{
43858151Seric 					usrerr("553 Unbalanced ')'");
43958333Seric 					if (delimptr != NULL)
44058333Seric 						*delimptr = p;
44159747Seric 					CurEnv->e_to = saveto;
4428078Seric 					return (NULL);
4433149Seric 				}
4448078Seric 				else
4458078Seric 					cmntcnt--;
4468078Seric 			}
4478078Seric 			else if (cmntcnt > 0)
4488078Seric 				c = NOCHAR;
4498423Seric 			else if (c == '<')
4508423Seric 				anglecnt++;
4518423Seric 			else if (c == '>')
4528423Seric 			{
4538423Seric 				if (anglecnt <= 0)
4548423Seric 				{
45558151Seric 					usrerr("553 Unbalanced '>'");
45658333Seric 					if (delimptr != NULL)
45758333Seric 						*delimptr = p;
45859747Seric 					CurEnv->e_to = saveto;
4598423Seric 					return (NULL);
4608423Seric 				}
4618423Seric 				anglecnt--;
4628423Seric 			}
46358050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
46411423Seric 				c = ' ';
4653149Seric 
4668078Seric 			if (c == NOCHAR)
4678078Seric 				continue;
4683149Seric 
4698078Seric 			/* see if this is end of input */
47011405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
4713149Seric 				break;
4723149Seric 
4738078Seric 			newstate = StateTab[state][toktype(c)];
4748078Seric 			if (tTd(22, 101))
4758078Seric 				printf("ns=%02o\n", newstate);
4768078Seric 			state = newstate & TYPE;
4778078Seric 			if (bitset(M, newstate))
4788078Seric 				c = NOCHAR;
4798078Seric 			if (bitset(B, newstate))
4804228Seric 				break;
481297Seric 		}
4823149Seric 
4833149Seric 		/* new token */
4848078Seric 		if (tok != q)
4851378Seric 		{
4868078Seric 			*q++ = '\0';
4878078Seric 			if (tTd(22, 36))
488297Seric 			{
4898078Seric 				printf("tok=");
4908078Seric 				xputs(tok);
49123109Seric 				(void) putchar('\n');
492297Seric 			}
4938078Seric 			if (avp >= &av[MAXATOM])
494297Seric 			{
49558151Seric 				syserr("553 prescan: too many tokens");
49658333Seric 				if (delimptr != NULL)
49758333Seric 					*delimptr = p;
49859747Seric 				CurEnv->e_to = saveto;
4998078Seric 				return (NULL);
500297Seric 			}
5018078Seric 			*avp++ = tok;
502297Seric 		}
5038423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
5043149Seric 	*avp = NULL;
50558333Seric 	p--;
50658333Seric 	if (delimptr != NULL)
50758333Seric 		*delimptr = p;
50856764Seric 	if (tTd(22, 12))
50956764Seric 	{
51056764Seric 		printf("prescan==>");
51156764Seric 		printav(av);
51256764Seric 	}
51359747Seric 	CurEnv->e_to = saveto;
51458546Seric 	if (av[0] == NULL)
51558546Seric 		return (NULL);
51658403Seric 	return (av);
5173149Seric }
5183149Seric /*
5193149Seric **  TOKTYPE -- return token type
5203149Seric **
5213149Seric **	Parameters:
5223149Seric **		c -- the character in question.
5233149Seric **
5243149Seric **	Returns:
5253149Seric **		Its type.
5263149Seric **
5273149Seric **	Side Effects:
5283149Seric **		none.
5293149Seric */
530297Seric 
5313149Seric toktype(c)
53258050Seric 	register int c;
5333149Seric {
5343380Seric 	static char buf[50];
5353382Seric 	static bool firstime = TRUE;
5363380Seric 
5373382Seric 	if (firstime)
5383380Seric 	{
5393382Seric 		firstime = FALSE;
54058050Seric 		expand("\201o", buf, &buf[sizeof buf - 1], CurEnv);
5417005Seric 		(void) strcat(buf, DELIMCHARS);
5423380Seric 	}
54358050Seric 	c &= 0377;
54456327Seric 	if (c == MATCHCLASS || c == MATCHREPL || c == MATCHNCLASS)
5458078Seric 		return (ONE);
54659027Seric 	if (c == MACRODEXPAND)
54759027Seric 		return (ONE);
5488078Seric 	if (c == '"')
5498078Seric 		return (QST);
55058050Seric 	if ((c & 0340) == 0200)
55158050Seric 		return (OPR);
5524100Seric 	if (!isascii(c))
5538078Seric 		return (ATM);
5548078Seric 	if (isspace(c) || c == ')')
5558078Seric 		return (SPC);
55658050Seric 	if (strchr(buf, c) != NULL)
5578078Seric 		return (OPR);
5588078Seric 	return (ATM);
5593149Seric }
5603149Seric /*
5613149Seric **  REWRITE -- apply rewrite rules to token vector.
5623149Seric **
5634476Seric **	This routine is an ordered production system.  Each rewrite
5644476Seric **	rule has a LHS (called the pattern) and a RHS (called the
5654476Seric **	rewrite); 'rwr' points the the current rewrite rule.
5664476Seric **
5674476Seric **	For each rewrite rule, 'avp' points the address vector we
5684476Seric **	are trying to match against, and 'pvp' points to the pattern.
5698058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
5709585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
5719585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
5724476Seric **
5734476Seric **	When a match between avp & pvp does not match, we try to
5749585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
5754476Seric **	we must also back out the match in mvp.  If we reach a
5768058Seric **	MATCHANY or MATCHZANY we just extend the match and start
5778058Seric **	over again.
5784476Seric **
5794476Seric **	When we finally match, we rewrite the address vector
5804476Seric **	and try over again.
5814476Seric **
5823149Seric **	Parameters:
5833149Seric **		pvp -- pointer to token vector.
58459027Seric **		ruleset -- the ruleset to use for rewriting.
58559027Seric **		e -- the current envelope.
5863149Seric **
5873149Seric **	Returns:
58859084Seric **		A status code.  If EX_TEMPFAIL, higher level code should
58959084Seric **			attempt recovery.
5903149Seric **
5913149Seric **	Side Effects:
5923149Seric **		pvp is modified.
5933149Seric */
5942091Seric 
5953149Seric struct match
5963149Seric {
5974468Seric 	char	**first;	/* first token matched */
5984468Seric 	char	**last;		/* last token matched */
59958825Seric 	char	**pattern;	/* pointer to pattern */
6003149Seric };
6013149Seric 
6024468Seric # define MAXMATCH	9	/* max params per rewrite */
6033149Seric 
6043149Seric 
60559084Seric int
60659027Seric rewrite(pvp, ruleset, e)
6073149Seric 	char **pvp;
6084070Seric 	int ruleset;
60959027Seric 	register ENVELOPE *e;
6103149Seric {
6113149Seric 	register char *ap;		/* address pointer */
6123149Seric 	register char *rp;		/* rewrite pointer */
6133149Seric 	register char **avp;		/* address vector pointer */
6143149Seric 	register char **rvp;		/* rewrite vector pointer */
6158058Seric 	register struct match *mlp;	/* cur ptr into mlist */
6168058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
61758866Seric 	int ruleno;			/* current rule number */
61859084Seric 	int rstat = EX_OK;		/* return status */
61956678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
6203149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
62159027Seric 	extern char *macvalue();
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;
77159027Seric 				while (*ap != NULL)
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 */
83358825Seric 					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;
95417473Seric 			char *olddelimchar;
95556678Seric 			char *replac;
95656678Seric 			int endtoken;
95756678Seric 			STAB *map;
95856678Seric 			char *mapname;
95956678Seric 			char **key_rvp;
96056678Seric 			char **arg_rvp;
96156678Seric 			char **default_rvp;
96256678Seric 			char buf[MAXNAME + 1];
96316914Seric 			char *pvpb1[MAXATOM + 1];
96456823Seric 			char *argvect[10];
96517174Seric 			char pvpbuf[PSBUFSIZE];
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;
104860209Seric 			if (map != NULL && bitset(MF_VALID, 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 			}
108356678Seric 			else
108453654Seric 			{
108556678Seric 				/* scan the new replacement */
108658333Seric 				xpvp = prescan(replac, '\0', pvpbuf, NULL);
108753654Seric 				if (xpvp == NULL)
108851317Seric 				{
108958403Seric 					/* prescan already printed error */
109059084Seric 					return EX_DATAERR;
109156678Seric 				}
109251317Seric 			}
109351317Seric 
109416914Seric 			/* append it to the token list */
109556678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
109656678Seric 			{
109717174Seric 				*avp++ = newstr(*xpvp);
109816920Seric 				if (avp >= &npvp[MAXATOM])
109916914Seric 					goto toolong;
110017174Seric 			}
110116914Seric 
110216914Seric 			/* restore the old trailing information */
110356678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
110416920Seric 				if (avp >= &npvp[MAXATOM])
110516914Seric 					goto toolong;
110617174Seric 
110756678Seric 			break;
110816914Seric 		}
110916914Seric 
111016914Seric 		/*
111116914Seric 		**  Check for subroutine calls.
111216914Seric 		*/
111316914Seric 
111458050Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
111556678Seric 		{
111659084Seric 			int stat;
111759084Seric 
111856678Seric 			bcopy((char *) &npvp[2], (char *) pvp,
111956678Seric 				(int) (avp - npvp - 2) * sizeof *avp);
112056678Seric 			if (tTd(21, 3))
112156678Seric 				printf("-----callsubr %s\n", npvp[1]);
112259084Seric 			stat = rewrite(pvp, atoi(npvp[1]), e);
112359084Seric 			if (rstat == EX_OK || stat == EX_TEMPFAIL)
112459084Seric 				rstat = stat;
112556678Seric 		}
112656678Seric 		else
112756678Seric 		{
112817348Seric 			bcopy((char *) npvp, (char *) pvp,
112916900Seric 				(int) (avp - npvp) * sizeof *avp);
113056678Seric 		}
11319374Seric 		if (tTd(21, 4))
11329374Seric 		{
11339374Seric 			printf("rewritten as:");
113456678Seric 			printav(pvp);
11359374Seric 		}
1136297Seric 	}
11378069Seric 
11389279Seric 	if (OpMode == MD_TEST || tTd(21, 2))
11398069Seric 	{
11408959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
114156678Seric 		printav(pvp);
11428069Seric 	}
114359084Seric 
114459084Seric 	return rstat;
11453149Seric }
11463149Seric /*
11473149Seric **  BUILDADDR -- build address from token vector.
11483149Seric **
11493149Seric **	Parameters:
11503149Seric **		tv -- token vector.
11513149Seric **		a -- pointer to address descriptor to fill.
11523149Seric **			If NULL, one will be allocated.
115358966Seric **		e -- the current envelope.
11543149Seric **
11553149Seric **	Returns:
11564279Seric **		NULL if there was an error.
11574279Seric **		'a' otherwise.
11583149Seric **
11593149Seric **	Side Effects:
11603149Seric **		fills in 'a'
11613149Seric */
11623149Seric 
116357249Seric struct errcodes
116457249Seric {
116557249Seric 	char	*ec_name;		/* name of error code */
116657249Seric 	int	ec_code;		/* numeric code */
116757249Seric } ErrorCodes[] =
116857249Seric {
116957249Seric 	"usage",	EX_USAGE,
117057249Seric 	"nouser",	EX_NOUSER,
117157249Seric 	"nohost",	EX_NOHOST,
117257249Seric 	"unavailable",	EX_UNAVAILABLE,
117357249Seric 	"software",	EX_SOFTWARE,
117457249Seric 	"tempfail",	EX_TEMPFAIL,
117557249Seric 	"protocol",	EX_PROTOCOL,
117657249Seric #ifdef EX_CONFIG
117757249Seric 	"config",	EX_CONFIG,
117857249Seric #endif
117957249Seric 	NULL,		EX_UNAVAILABLE,
118057249Seric };
118157249Seric 
118256678Seric ADDRESS *
118358966Seric buildaddr(tv, a, e)
11843149Seric 	register char **tv;
11853149Seric 	register ADDRESS *a;
118658966Seric 	register ENVELOPE *e;
11873149Seric {
11883149Seric 	struct mailer **mp;
11893149Seric 	register struct mailer *m;
119058008Seric 	char *bp;
119158008Seric 	int spaceleft;
119257402Seric 	static char buf[MAXNAME];
11933149Seric 
11943149Seric 	if (a == NULL)
11953149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
119616889Seric 	bzero((char *) a, sizeof *a);
11973149Seric 
11983149Seric 	/* figure out what net/mailer to use */
119958050Seric 	if ((**tv & 0377) != CANONNET)
12004279Seric 	{
120158151Seric 		syserr("554 buildaddr: no net");
12024279Seric 		return (NULL);
12034279Seric 	}
12043149Seric 	tv++;
120558680Seric 	if (strcasecmp(*tv, "error") == 0)
12064279Seric 	{
120758050Seric 		if ((**++tv & 0377) == CANONHOST)
120810183Seric 		{
120957249Seric 			register struct errcodes *ep;
121057249Seric 
121158050Seric 			if (isascii(**++tv) && isdigit(**tv))
121257249Seric 			{
121357249Seric 				setstat(atoi(*tv));
121457249Seric 			}
121557249Seric 			else
121657249Seric 			{
121757249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
121857249Seric 					if (strcasecmp(ep->ec_name, *tv) == 0)
121957249Seric 						break;
122057249Seric 				setstat(ep->ec_code);
122157249Seric 			}
122210183Seric 			tv++;
122310183Seric 		}
122458050Seric 		if ((**tv & 0377) != CANONUSER)
122558151Seric 			syserr("554 buildaddr: error: no user");
122658814Seric 		cataddr(++tv, NULL, buf, sizeof buf, ' ');
122758082Seric 		stripquotes(buf);
12284279Seric 		usrerr(buf);
12294279Seric 		return (NULL);
12304279Seric 	}
123157402Seric 
12324598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
12333149Seric 	{
123458680Seric 		if (strcasecmp(m->m_name, *tv) == 0)
12353149Seric 			break;
12363149Seric 	}
12373149Seric 	if (m == NULL)
12384279Seric 	{
123958151Seric 		syserr("554 buildaddr: unknown mailer %s", *tv);
12404279Seric 		return (NULL);
12414279Seric 	}
12424598Seric 	a->q_mailer = m;
12433149Seric 
12443149Seric 	/* figure out what host (if any) */
124556678Seric 	tv++;
124658509Seric 	if ((**tv & 0377) == CANONHOST)
12473149Seric 	{
124858008Seric 		bp = buf;
124958008Seric 		spaceleft = sizeof buf - 1;
125058050Seric 		while (*++tv != NULL && (**tv & 0377) != CANONUSER)
125158008Seric 		{
125258008Seric 			int i = strlen(*tv);
125358008Seric 
125458008Seric 			if (i > spaceleft)
125558008Seric 			{
125658008Seric 				/* out of space for this address */
125758008Seric 				if (spaceleft >= 0)
125858151Seric 					syserr("554 buildaddr: host too long (%.40s...)",
125958008Seric 						buf);
126058008Seric 				i = spaceleft;
126158008Seric 				spaceleft = 0;
126258008Seric 			}
126358008Seric 			if (i <= 0)
126458008Seric 				continue;
126558008Seric 			bcopy(*tv, bp, i);
126658008Seric 			bp += i;
126758008Seric 			spaceleft -= i;
126858008Seric 		}
126958010Seric 		*bp = '\0';
12705704Seric 		a->q_host = newstr(buf);
12713149Seric 	}
127257249Seric 	else
127358509Seric 	{
127458509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
127558509Seric 		{
127658509Seric 			syserr("554 buildaddr: no host");
127758509Seric 			return (NULL);
127858509Seric 		}
127957249Seric 		a->q_host = NULL;
128058509Seric 	}
12813149Seric 
12823149Seric 	/* figure out the user */
128358050Seric 	if (*tv == NULL || (**tv & 0377) != CANONUSER)
12844279Seric 	{
128558151Seric 		syserr("554 buildaddr: no user");
12864279Seric 		return (NULL);
12874279Seric 	}
128857402Seric 	tv++;
128951317Seric 
129057402Seric 	/* do special mapping for local mailer */
129157402Seric 	if (m == LocalMailer && *tv != NULL)
129257402Seric 	{
129357454Seric 		register char *p = *tv;
129457454Seric 
129557454Seric 		if (*p == '"')
129657454Seric 			p++;
129757454Seric 		if (*p == '|')
129857402Seric 			a->q_mailer = m = ProgMailer;
129957454Seric 		else if (*p == '/')
130057402Seric 			a->q_mailer = m = FileMailer;
130157454Seric 		else if (*p == ':')
130257402Seric 		{
130357402Seric 			/* may be :include: */
130458814Seric 			cataddr(tv, NULL, buf, sizeof buf, '\0');
130558008Seric 			stripquotes(buf);
130658008Seric 			if (strncasecmp(buf, ":include:", 9) == 0)
130758008Seric 			{
130858008Seric 				/* if :include:, don't need further rewriting */
130957402Seric 				a->q_mailer = m = InclMailer;
131058008Seric 				a->q_user = &buf[9];
131158008Seric 				return (a);
131258008Seric 			}
131357402Seric 		}
131457402Seric 	}
131557402Seric 
131658008Seric 	if (m == LocalMailer && *tv != NULL && strcmp(*tv, "@") == 0)
131758008Seric 	{
131858008Seric 		tv++;
131958008Seric 		a->q_flags |= QNOTREMOTE;
132058008Seric 	}
132158008Seric 
132258673Seric 	/* do cleanup of final address */
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,
148440973Sbostic 		       a->q_host, a->q_user, a->q_ruser? a->q_ruser: "<null>");
148559269Seric 		printf("\tnext=%x, flags=%o, alias %x, uid %d, gid %d\n",
148659269Seric 		       a->q_next, a->q_flags, a->q_alias, a->q_uid, a->q_gid);
148759269Seric 		printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n",
148859269Seric 		       a->q_owner == NULL ? "(none)" : a->q_owner,
148959269Seric 		       a->q_home, a->q_fullname);
14904996Seric 
14913234Seric 		if (!follow)
14923234Seric 			return;
14934996Seric 		a = a->q_next;
14943234Seric 	}
14955001Seric 	if (first)
14964443Seric 		printf("[NULL]\n");
14973234Seric }
14984317Seric 
14997682Seric /*
15007682Seric **  REMOTENAME -- return the name relative to the current mailer
15017682Seric **
15027682Seric **	Parameters:
15037682Seric **		name -- the name to translate.
15048069Seric **		m -- the mailer that we want to do rewriting relative
15058069Seric **			to.
150659163Seric **		flags -- fine tune operations.
150759163Seric **		pstat -- pointer to status word.
150858020Seric **		e -- the current envelope.
15097682Seric **
15107682Seric **	Returns:
15117682Seric **		the text string representing this address relative to
15127682Seric **			the receiving mailer.
15137682Seric **
15147682Seric **	Side Effects:
15157682Seric **		none.
15167682Seric **
15177682Seric **	Warnings:
15187682Seric **		The text string returned is tucked away locally;
15197682Seric **			copy it if you intend to save it.
15207682Seric */
15217682Seric 
15227682Seric char *
152359163Seric remotename(name, m, flags, pstat, e)
15247682Seric 	char *name;
152556678Seric 	struct mailer *m;
152659163Seric 	int flags;
152759163Seric 	int *pstat;
152856678Seric 	register ENVELOPE *e;
15297682Seric {
15308069Seric 	register char **pvp;
15318069Seric 	char *fancy;
153256678Seric 	extern char *macvalue();
153356678Seric 	char *oldg = macvalue('g', e);
153458020Seric 	int rwset;
15357682Seric 	static char buf[MAXNAME];
15367682Seric 	char lbuf[MAXNAME];
153716914Seric 	char pvpbuf[PSBUFSIZE];
153856678Seric 	extern char **prescan();
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? */
167958966Seric 	a1 = buildaddr(pvp, NULL, 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;
169158333Seric 	allocaddr(a1, 1, 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
1708*60219Seric 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