122976Smiriam /*
268839Seric  * Copyright (c) 1983, 1995 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*69703Seric static char sccsid[] = "@(#)parseaddr.c	8.69 (Berkeley) 05/26/95";
1133730Sbostic #endif /* not lint */
1222976Smiriam 
1356678Seric # include "sendmail.h"
14297Seric 
15297Seric /*
169888Seric **  PARSEADDR -- Parse an address
17297Seric **
18297Seric **	Parses an address and breaks it up into three parts: a
19297Seric **	net to transmit the message on, the host to transmit it
20297Seric **	to, and a user on that host.  These are loaded into an
212973Seric **	ADDRESS header with the values squirreled away if necessary.
22297Seric **	The "user" part may not be a real user; the process may
23297Seric **	just reoccur on that machine.  For example, on a machine
24297Seric **	with an arpanet connection, the address
25297Seric **		csvax.bill@berkeley
26297Seric **	will break up to a "user" of 'csvax.bill' and a host
27297Seric **	of 'berkeley' -- to be transmitted over the arpanet.
28297Seric **
29297Seric **	Parameters:
30297Seric **		addr -- the address to parse.
31297Seric **		a -- a pointer to the address descriptor buffer.
32297Seric **			If NULL, a header will be created.
3364284Seric **		flags -- describe detail for parsing.  See RF_ definitions
3464284Seric **			in sendmail.h.
3511445Seric **		delim -- the character to terminate the address, passed
3611445Seric **			to prescan.
3758333Seric **		delimptr -- if non-NULL, set to the location of the
3858333Seric **			delim character that was found.
3956678Seric **		e -- the envelope that will contain this address.
40297Seric **
41297Seric **	Returns:
42297Seric **		A pointer to the address descriptor header (`a' if
43297Seric **			`a' is non-NULL).
44297Seric **		NULL on error.
45297Seric **
46297Seric **	Side Effects:
47297Seric **		none
48297Seric */
49297Seric 
509374Seric /* following delimiters are inherent to the internal algorithms */
5159278Seric # define DELIMCHARS	"()<>,;\r\n"	/* default word delimiters */
522091Seric 
532973Seric ADDRESS *
5464284Seric parseaddr(addr, a, flags, delim, delimptr, e)
55297Seric 	char *addr;
562973Seric 	register ADDRESS *a;
5764284Seric 	int flags;
5859700Seric 	int delim;
5958333Seric 	char **delimptr;
6056678Seric 	register ENVELOPE *e;
61297Seric {
623149Seric 	register char **pvp;
6358333Seric 	auto char *delimptrbuf;
6459084Seric 	bool queueup;
6516914Seric 	char pvpbuf[PSBUFSIZE];
6656678Seric 	extern ADDRESS *buildaddr();
6757388Seric 	extern bool invalidaddr();
68297Seric 
69297Seric 	/*
70297Seric 	**  Initialize and prescan address.
71297Seric 	*/
72297Seric 
7356678Seric 	e->e_to = addr;
747675Seric 	if (tTd(20, 1))
759888Seric 		printf("\n--parseaddr(%s)\n", addr);
763188Seric 
7758333Seric 	if (delimptr == NULL)
7858333Seric 		delimptr = &delimptrbuf;
7958333Seric 
8068711Seric 	pvp = prescan(addr, delim, pvpbuf, sizeof pvpbuf, delimptr, NULL);
813149Seric 	if (pvp == NULL)
8256729Seric 	{
8356729Seric 		if (tTd(20, 1))
8456729Seric 			printf("parseaddr-->NULL\n");
85297Seric 		return (NULL);
8656729Seric 	}
87297Seric 
8864726Seric 	if (invalidaddr(addr, delim == '\0' ? NULL : *delimptr))
8964726Seric 	{
9064726Seric 		if (tTd(20, 1))
9164726Seric 			printf("parseaddr-->bad address\n");
9264726Seric 		return NULL;
9364726Seric 	}
9464726Seric 
95297Seric 	/*
9664348Seric 	**  Save addr if we are going to have to.
9764348Seric 	**
9864348Seric 	**	We have to do this early because there is a chance that
9964348Seric 	**	the map lookups in the rewriting rules could clobber
10064348Seric 	**	static memory somewhere.
10164348Seric 	*/
10264348Seric 
10364348Seric 	if (bitset(RF_COPYPADDR, flags) && addr != NULL)
10464348Seric 	{
10564348Seric 		char savec = **delimptr;
10664348Seric 
10764348Seric 		if (savec != '\0')
10864348Seric 			**delimptr = '\0';
10966794Seric 		e->e_to = addr = newstr(addr);
11064348Seric 		if (savec != '\0')
11164348Seric 			**delimptr = savec;
11264348Seric 	}
11364348Seric 
11464348Seric 	/*
1153149Seric 	**  Apply rewriting rules.
1167889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
117297Seric 	*/
118297Seric 
11959084Seric 	queueup = FALSE;
12065071Seric 	if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL)
12159084Seric 		queueup = TRUE;
12265071Seric 	if (rewrite(pvp, 0, 0, e) == EX_TEMPFAIL)
12359084Seric 		queueup = TRUE;
124297Seric 
125297Seric 
126297Seric 	/*
1273149Seric 	**  Build canonical address from pvp.
128297Seric 	*/
129297Seric 
13064284Seric 	a = buildaddr(pvp, a, flags, e);
131297Seric 
132297Seric 	/*
1333149Seric 	**  Make local copies of the host & user and then
1343149Seric 	**  transport them out.
135297Seric 	*/
136297Seric 
13764348Seric 	allocaddr(a, flags, addr);
13864306Seric 	if (bitset(QBADADDR, a->q_flags))
13964306Seric 		return a;
14056678Seric 
14156678Seric 	/*
14259084Seric 	**  If there was a parsing failure, mark it for queueing.
14359084Seric 	*/
14459084Seric 
14559084Seric 	if (queueup)
14659595Seric 	{
14759734Seric 		char *msg = "Transient parse error -- message queued for future delivery";
14859734Seric 
14959595Seric 		if (tTd(20, 1))
15059595Seric 			printf("parseaddr: queuing message\n");
15159734Seric 		message(msg);
15259734Seric 		if (e->e_message == NULL)
15360009Seric 			e->e_message = newstr(msg);
15459084Seric 		a->q_flags |= QQUEUEUP;
15568358Seric 		a->q_status = "4.4.3";
15659595Seric 	}
15759084Seric 
15859084Seric 	/*
15956678Seric 	**  Compute return value.
16056678Seric 	*/
16156678Seric 
16256678Seric 	if (tTd(20, 1))
1638078Seric 	{
16456678Seric 		printf("parseaddr-->");
16556678Seric 		printaddr(a, FALSE);
16656678Seric 	}
16756678Seric 
16856678Seric 	return (a);
16956678Seric }
17056678Seric /*
17157388Seric **  INVALIDADDR -- check for address containing meta-characters
17257388Seric **
17357388Seric **	Parameters:
17457388Seric **		addr -- the address to check.
17557388Seric **
17657388Seric **	Returns:
17757388Seric **		TRUE -- if the address has any "wierd" characters
17857388Seric **		FALSE -- otherwise.
17957388Seric */
18057388Seric 
18157388Seric bool
18264726Seric invalidaddr(addr, delimptr)
18357388Seric 	register char *addr;
18464726Seric 	char *delimptr;
18557388Seric {
18668433Seric 	char savedelim = '\0';
18764726Seric 
18864726Seric 	if (delimptr != NULL)
18964764Seric 	{
19064726Seric 		savedelim = *delimptr;
19164764Seric 		if (savedelim != '\0')
19264764Seric 			*delimptr = '\0';
19364764Seric 	}
19464726Seric #if 0
19564726Seric 	/* for testing.... */
19664726Seric 	if (strcmp(addr, "INvalidADDR") == 0)
19757388Seric 	{
19864726Seric 		usrerr("553 INvalid ADDRess");
19964764Seric 		goto addrfailure;
20057388Seric 	}
20164726Seric #endif
20264726Seric 	for (; *addr != '\0'; addr++)
20364726Seric 	{
20464726Seric 		if ((*addr & 0340) == 0200)
20564726Seric 			break;
20664726Seric 	}
20764726Seric 	if (*addr == '\0')
20864764Seric 	{
20968400Seric 		if (delimptr != NULL && savedelim != '\0')
21064764Seric 			*delimptr = savedelim;
21164726Seric 		return FALSE;
21264764Seric 	}
21364726Seric 	setstat(EX_USAGE);
21464726Seric 	usrerr("553 Address contained invalid control characters");
21564764Seric   addrfailure:
21668446Seric 	if (delimptr != NULL && savedelim != '\0')
21764764Seric 		*delimptr = savedelim;
21864726Seric 	return TRUE;
21957388Seric }
22057388Seric /*
22156678Seric **  ALLOCADDR -- do local allocations of address on demand.
22256678Seric **
22356678Seric **	Also lowercases the host name if requested.
22456678Seric **
22556678Seric **	Parameters:
22656678Seric **		a -- the address to reallocate.
22764284Seric **		flags -- the copy flag (see RF_ definitions in sendmail.h
22864284Seric **			for a description).
22956678Seric **		paddr -- the printname of the address.
23056678Seric **
23156678Seric **	Returns:
23256678Seric **		none.
23356678Seric **
23456678Seric **	Side Effects:
23556678Seric **		Copies portions of a into local buffers as requested.
23656678Seric */
23756678Seric 
23864348Seric allocaddr(a, flags, paddr)
23956678Seric 	register ADDRESS *a;
24064284Seric 	int flags;
24156678Seric 	char *paddr;
24256678Seric {
24358673Seric 	if (tTd(24, 4))
24467693Seric 		printf("allocaddr(flags=%x, paddr=%s)\n", flags, paddr);
24558673Seric 
24664348Seric 	a->q_paddr = paddr;
2478078Seric 
24824944Seric 	if (a->q_user == NULL)
24924944Seric 		a->q_user = "";
25024944Seric 	if (a->q_host == NULL)
25124944Seric 		a->q_host = "";
25224944Seric 
25364284Seric 	if (bitset(RF_COPYPARSE, flags))
254297Seric 	{
25524944Seric 		a->q_host = newstr(a->q_host);
2563149Seric 		if (a->q_user != a->q_paddr)
2573149Seric 			a->q_user = newstr(a->q_user);
258297Seric 	}
259297Seric 
26056678Seric 	if (a->q_paddr == NULL)
26156678Seric 		a->q_paddr = a->q_user;
262297Seric }
263297Seric /*
264297Seric **  PRESCAN -- Prescan name and make it canonical
265297Seric **
2669374Seric **	Scans a name and turns it into a set of tokens.  This process
2679374Seric **	deletes blanks and comments (in parentheses).
268297Seric **
269297Seric **	This routine knows about quoted strings and angle brackets.
270297Seric **
271297Seric **	There are certain subtleties to this routine.  The one that
272297Seric **	comes to mind now is that backslashes on the ends of names
273297Seric **	are silently stripped off; this is intentional.  The problem
274297Seric **	is that some versions of sndmsg (like at LBL) set the kill
275297Seric **	character to something other than @ when reading addresses;
276297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
277297Seric **	berknet mailer.
278297Seric **
279297Seric **	Parameters:
280297Seric **		addr -- the name to chomp.
281297Seric **		delim -- the delimiter for the address, normally
282297Seric **			'\0' or ','; \0 is accepted in any case.
28315284Seric **			If '\t' then we are reading the .cf file.
28416914Seric **		pvpbuf -- place to put the saved text -- note that
28516914Seric **			the pointers are static.
28665066Seric **		pvpbsize -- size of pvpbuf.
28758333Seric **		delimptr -- if non-NULL, set to the location of the
28858333Seric **			terminating delimiter.
28968711Seric **		toktab -- if set, a token table to use for parsing.
29068711Seric **			If NULL, use the default table.
291297Seric **
292297Seric **	Returns:
2933149Seric **		A pointer to a vector of tokens.
294297Seric **		NULL on error.
295297Seric */
296297Seric 
2978078Seric /* states and character types */
2988078Seric # define OPR		0	/* operator */
2998078Seric # define ATM		1	/* atom */
3008078Seric # define QST		2	/* in quoted string */
3018078Seric # define SPC		3	/* chewing up spaces */
3028078Seric # define ONE		4	/* pick up one character */
30368711Seric # define ILL		5	/* illegal character */
3043149Seric 
30568711Seric # define NSTATES	6	/* number of states */
3068078Seric # define TYPE		017	/* mask to select state type */
3078078Seric 
3088078Seric /* meta bits for table */
3098078Seric # define M		020	/* meta character; don't pass through */
3108078Seric # define B		040	/* cause a break */
3118078Seric # define MB		M|B	/* meta-break */
3128078Seric 
3138078Seric static short StateTab[NSTATES][NSTATES] =
3148078Seric {
31568711Seric    /*	oldst	chtype>	OPR	ATM	QST	SPC	ONE	ILL	*/
31668711Seric 	/*OPR*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,	ILL|MB,
31768711Seric 	/*ATM*/		OPR|B,	ATM,	QST|B,	SPC|MB,	ONE|B,	ILL|MB,
31868711Seric 	/*QST*/		QST,	QST,	OPR,	QST,	QST,	QST,
31968711Seric 	/*SPC*/		OPR,	ATM,	QST,	SPC|M,	ONE,	ILL|MB,
32068711Seric 	/*ONE*/		OPR,	OPR,	OPR,	OPR,	OPR,	ILL|MB,
32168711Seric 	/*ILL*/		OPR|B,	ATM|B,	QST|B,	SPC|MB,	ONE|B,	ILL|M,
3228078Seric };
3238078Seric 
32465092Seric /* token type table -- it gets modified with $o characters */
32568711Seric static char TokTypeTab[256] =
32665092Seric {
32768711Seric     /*	nul soh stx etx eot enq ack bel  bs  ht  nl  vt  np  cr  so  si   */
32868711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,SPC,SPC,SPC,SPC,SPC,ATM,ATM,
32968711Seric     /*	dle dc1 dc2 dc3 dc4 nak syn etb  can em  sub esc fs  gs  rs  us   */
33068711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
33168711Seric     /*  sp  !   "   #   $   %   &   '    (   )   *   +   ,   -   .   /    */
33268711Seric 	SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, ATM,SPC,ATM,ATM,ATM,ATM,ATM,ATM,
33368711Seric     /*	0   1   2   3   4   5   6   7    8   9   :   ;   <   =   >   ?    */
33468711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
33568711Seric     /*	@   A   B   C   D   E   F   G    H   I   J   K   L   M   N   O    */
33668711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
33768711Seric     /*  P   Q   R   S   T   U   V   W    X   Y   Z   [   \   ]   ^   _    */
33868711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
33968711Seric     /*	`   a   b   c   d   e   f   g    h   i   j   k   l   m   n   o    */
34068711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
34168711Seric     /*  p   q   r   s   t   u   v   w    x   y   z   {   |   }   ~   del  */
34268711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
34368711Seric 
34468711Seric     /*	nul soh stx etx eot enq ack bel  bs  ht  nl  vt  np  cr  so  si   */
34568711Seric 	OPR,OPR,ONE,OPR,OPR,OPR,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
34668711Seric     /*	dle dc1 dc2 dc3 dc4 nak syn etb  can em  sub esc fs  gs  rs  us   */
34768711Seric 	OPR,OPR,OPR,ONE,ONE,ONE,OPR,OPR, OPR,OPR,OPR,OPR,OPR,OPR,OPR,OPR,
34868711Seric     /*  sp  !   "   #   $   %   &   '    (   )   *   +   ,   -   .   /    */
34968711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
35068711Seric     /*	0   1   2   3   4   5   6   7    8   9   :   ;   <   =   >   ?    */
35168711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
35268711Seric     /*	@   A   B   C   D   E   F   G    H   I   J   K   L   M   N   O    */
35368711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
35468711Seric     /*  P   Q   R   S   T   U   V   W    X   Y   Z   [   \   ]   ^   _    */
35568711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
35668711Seric     /*	`   a   b   c   d   e   f   g    h   i   j   k   l   m   n   o    */
35768711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
35868711Seric     /*  p   q   r   s   t   u   v   w    x   y   z   {   |   }   ~   del  */
35968711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
36065092Seric };
36165092Seric 
36268711Seric /* token type table for MIME parsing */
36368711Seric char MimeTokenTab[256] =
36468711Seric {
36568711Seric     /*	nul soh stx etx eot enq ack bel  bs  ht  nl  vt  np  cr  so  si   */
36668711Seric 	ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,SPC,SPC,SPC,SPC,SPC,ILL,ILL,
36768711Seric     /*	dle dc1 dc2 dc3 dc4 nak syn etb  can em  sub esc fs  gs  rs  us   */
36868711Seric 	ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
36968711Seric     /*  sp  !   "   #   $   %   &   '    (   )   *   +   ,   -   .   /    */
37068711Seric 	SPC,ATM,QST,ATM,ATM,ATM,ATM,ATM, ATM,SPC,ATM,ATM,OPR,ATM,ATM,OPR,
37168711Seric     /*	0   1   2   3   4   5   6   7    8   9   :   ;   <   =   >   ?    */
37268711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,OPR,OPR,OPR,OPR,OPR,OPR,
37368711Seric     /*	@   A   B   C   D   E   F   G    H   I   J   K   L   M   N   O    */
37468711Seric 	OPR,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
37568711Seric     /*  P   Q   R   S   T   U   V   W    X   Y   Z   [   \   ]   ^   _    */
37668711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,OPR,OPR,OPR,ATM,ATM,
37768711Seric     /*	`   a   b   c   d   e   f   g    h   i   j   k   l   m   n   o    */
37868711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
37968711Seric     /*  p   q   r   s   t   u   v   w    x   y   z   {   |   }   ~   del  */
38068711Seric 	ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM, ATM,ATM,ATM,ATM,ATM,ATM,ATM,ATM,
38165092Seric 
38268711Seric     /*	nul soh stx etx eot enq ack bel  bs  ht  nl  vt  np  cr  so  si   */
38368711Seric 	ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
38468711Seric     /*	dle dc1 dc2 dc3 dc4 nak syn etb  can em  sub esc fs  gs  rs  us   */
38568711Seric 	ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
38668711Seric     /*  sp  !   "   #   $   %   &   '    (   )   *   +   ,   -   .   /    */
38768711Seric 	ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
38868711Seric     /*	0   1   2   3   4   5   6   7    8   9   :   ;   <   =   >   ?    */
38968711Seric 	ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
39068711Seric     /*	@   A   B   C   D   E   F   G    H   I   J   K   L   M   N   O    */
39168711Seric 	ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
39268711Seric     /*  P   Q   R   S   T   U   V   W    X   Y   Z   [   \   ]   ^   _    */
39368711Seric 	ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
39468711Seric     /*	`   a   b   c   d   e   f   g    h   i   j   k   l   m   n   o    */
39568711Seric 	ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
39668711Seric     /*  p   q   r   s   t   u   v   w    x   y   z   {   |   }   ~   del  */
39768711Seric 	ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL, ILL,ILL,ILL,ILL,ILL,ILL,ILL,ILL,
39868711Seric };
39965092Seric 
40068711Seric 
4018078Seric # define NOCHAR		-1	/* signal nothing in lookahead token */
4028078Seric 
4033149Seric char **
40468711Seric prescan(addr, delim, pvpbuf, pvpbsize, delimptr, toktab)
405297Seric 	char *addr;
40668353Seric 	int delim;
40716914Seric 	char pvpbuf[];
40858333Seric 	char **delimptr;
40968711Seric 	char *toktab;
410297Seric {
411297Seric 	register char *p;
4128078Seric 	register char *q;
4139346Seric 	register int c;
4143149Seric 	char **avp;
415297Seric 	bool bslashmode;
416297Seric 	int cmntcnt;
4178423Seric 	int anglecnt;
4183149Seric 	char *tok;
4198078Seric 	int state;
4208078Seric 	int newstate;
42159747Seric 	char *saveto = CurEnv->e_to;
4228078Seric 	static char *av[MAXATOM+1];
42365092Seric 	static char firsttime = TRUE;
42456678Seric 	extern int errno;
425297Seric 
42665092Seric 	if (firsttime)
42765092Seric 	{
42865092Seric 		/* initialize the token type table */
42965092Seric 		char obuf[50];
43065092Seric 
43165092Seric 		firsttime = FALSE;
43268529Seric 		expand("\201o", obuf, sizeof obuf - sizeof DELIMCHARS, CurEnv);
43365092Seric 		strcat(obuf, DELIMCHARS);
43465092Seric 		for (p = obuf; *p != '\0'; p++)
43565092Seric 		{
43665092Seric 			if (TokTypeTab[*p & 0xff] == ATM)
43765092Seric 				TokTypeTab[*p & 0xff] = OPR;
43865092Seric 		}
43965092Seric 	}
44068711Seric 	if (toktab == NULL)
44168711Seric 		toktab = TokTypeTab;
44265092Seric 
44315253Seric 	/* make sure error messages don't have garbage on them */
44415253Seric 	errno = 0;
44515253Seric 
44616914Seric 	q = pvpbuf;
4473149Seric 	bslashmode = FALSE;
4487800Seric 	cmntcnt = 0;
4498423Seric 	anglecnt = 0;
4503149Seric 	avp = av;
45156678Seric 	state = ATM;
4528078Seric 	c = NOCHAR;
4538078Seric 	p = addr;
45459747Seric 	CurEnv->e_to = p;
45556764Seric 	if (tTd(22, 11))
456297Seric 	{
4578078Seric 		printf("prescan: ");
4588078Seric 		xputs(p);
45923109Seric 		(void) putchar('\n');
4608078Seric 	}
4618078Seric 
4628078Seric 	do
4638078Seric 	{
4643149Seric 		/* read a token */
4653149Seric 		tok = q;
4668078Seric 		for (;;)
467297Seric 		{
4688078Seric 			/* store away any old lookahead character */
46959277Seric 			if (c != NOCHAR && !bslashmode)
4708078Seric 			{
47115284Seric 				/* see if there is room */
47265066Seric 				if (q >= &pvpbuf[pvpbsize - 5])
4738078Seric 				{
47458151Seric 					usrerr("553 Address too long");
47568526Seric 					if (strlen(addr) > MAXNAME)
47668526Seric 						addr[MAXNAME] = '\0';
47765015Seric 	returnnull:
47858333Seric 					if (delimptr != NULL)
47958333Seric 						*delimptr = p;
48059747Seric 					CurEnv->e_to = saveto;
4818078Seric 					return (NULL);
4828078Seric 				}
48315284Seric 
48415284Seric 				/* squirrel it away */
4858078Seric 				*q++ = c;
4868078Seric 			}
4878078Seric 
4888078Seric 			/* read a new input character */
4898078Seric 			c = *p++;
49057631Seric 			if (c == '\0')
49156764Seric 			{
49256764Seric 				/* diagnose and patch up bad syntax */
49356764Seric 				if (state == QST)
49456764Seric 				{
49564767Seric 					usrerr("653 Unbalanced '\"'");
49656764Seric 					c = '"';
49756764Seric 				}
49856764Seric 				else if (cmntcnt > 0)
49956764Seric 				{
50064767Seric 					usrerr("653 Unbalanced '('");
50156764Seric 					c = ')';
50256764Seric 				}
50356764Seric 				else if (anglecnt > 0)
50456764Seric 				{
50556764Seric 					c = '>';
50664767Seric 					usrerr("653 Unbalanced '<'");
50756764Seric 				}
50856764Seric 				else
50956764Seric 					break;
51015284Seric 
51156764Seric 				p--;
51256764Seric 			}
51357631Seric 			else if (c == delim && anglecnt <= 0 &&
51457631Seric 					cmntcnt <= 0 && state != QST)
51557631Seric 				break;
51656764Seric 
5178078Seric 			if (tTd(22, 101))
5188078Seric 				printf("c=%c, s=%d; ", c, state);
5198078Seric 
5203149Seric 			/* chew up special characters */
5213149Seric 			*q = '\0';
5223149Seric 			if (bslashmode)
5233149Seric 			{
52459105Seric 				bslashmode = FALSE;
52559105Seric 
52624944Seric 				/* kludge \! for naive users */
52758061Seric 				if (cmntcnt > 0)
52859105Seric 				{
52958061Seric 					c = NOCHAR;
53059105Seric 					continue;
53159105Seric 				}
53259105Seric 				else if (c != '!' || state == QST)
53359105Seric 				{
53456678Seric 					*q++ = '\\';
53559105Seric 					continue;
53659105Seric 				}
5373149Seric 			}
53856678Seric 
53956678Seric 			if (c == '\\')
5403149Seric 			{
5413149Seric 				bslashmode = TRUE;
5423149Seric 			}
54356678Seric 			else if (state == QST)
5448514Seric 			{
5458514Seric 				/* do nothing, just avoid next clauses */
5468514Seric 			}
5478078Seric 			else if (c == '(')
5484100Seric 			{
5498078Seric 				cmntcnt++;
5508078Seric 				c = NOCHAR;
5514100Seric 			}
5528078Seric 			else if (c == ')')
5533149Seric 			{
5548078Seric 				if (cmntcnt <= 0)
5553149Seric 				{
55664767Seric 					usrerr("653 Unbalanced ')'");
55763844Seric 					c = NOCHAR;
5583149Seric 				}
5598078Seric 				else
5608078Seric 					cmntcnt--;
5618078Seric 			}
5628078Seric 			else if (cmntcnt > 0)
5638078Seric 				c = NOCHAR;
5648423Seric 			else if (c == '<')
5658423Seric 				anglecnt++;
5668423Seric 			else if (c == '>')
5678423Seric 			{
5688423Seric 				if (anglecnt <= 0)
5698423Seric 				{
57064767Seric 					usrerr("653 Unbalanced '>'");
57163844Seric 					c = NOCHAR;
5728423Seric 				}
57363844Seric 				else
57463844Seric 					anglecnt--;
5758423Seric 			}
57658050Seric 			else if (delim == ' ' && isascii(c) && isspace(c))
57711423Seric 				c = ' ';
5783149Seric 
5798078Seric 			if (c == NOCHAR)
5808078Seric 				continue;
5813149Seric 
5828078Seric 			/* see if this is end of input */
58311405Seric 			if (c == delim && anglecnt <= 0 && state != QST)
5843149Seric 				break;
5853149Seric 
58668711Seric 			newstate = StateTab[state][toktab[c & 0xff]];
5878078Seric 			if (tTd(22, 101))
5888078Seric 				printf("ns=%02o\n", newstate);
5898078Seric 			state = newstate & TYPE;
59068711Seric 			if (state == ILL)
59168711Seric 			{
59268711Seric 				if (isascii(c) && isprint(c))
59368711Seric 					usrerr("653 Illegal character %c", c);
59468711Seric 				else
59568711Seric 					usrerr("653 Illegal character 0x%02x", c);
59668711Seric 			}
5978078Seric 			if (bitset(M, newstate))
5988078Seric 				c = NOCHAR;
5998078Seric 			if (bitset(B, newstate))
6004228Seric 				break;
601297Seric 		}
6023149Seric 
6033149Seric 		/* new token */
6048078Seric 		if (tok != q)
6051378Seric 		{
6068078Seric 			*q++ = '\0';
6078078Seric 			if (tTd(22, 36))
608297Seric 			{
6098078Seric 				printf("tok=");
6108078Seric 				xputs(tok);
61123109Seric 				(void) putchar('\n');
612297Seric 			}
6138078Seric 			if (avp >= &av[MAXATOM])
614297Seric 			{
61558151Seric 				syserr("553 prescan: too many tokens");
61665015Seric 				goto returnnull;
617297Seric 			}
61865015Seric 			if (q - tok > MAXNAME)
61965015Seric 			{
62065015Seric 				syserr("553 prescan: token too long");
62165015Seric 				goto returnnull;
62265015Seric 			}
6238078Seric 			*avp++ = tok;
624297Seric 		}
6258423Seric 	} while (c != '\0' && (c != delim || anglecnt > 0));
6263149Seric 	*avp = NULL;
62758333Seric 	p--;
62858333Seric 	if (delimptr != NULL)
62958333Seric 		*delimptr = p;
63056764Seric 	if (tTd(22, 12))
63156764Seric 	{
63256764Seric 		printf("prescan==>");
63356764Seric 		printav(av);
63456764Seric 	}
63559747Seric 	CurEnv->e_to = saveto;
63658546Seric 	if (av[0] == NULL)
63764966Seric 	{
63864966Seric 		if (tTd(22, 1))
63964966Seric 			printf("prescan: null leading token\n");
64058546Seric 		return (NULL);
64164966Seric 	}
64258403Seric 	return (av);
6433149Seric }
6443149Seric /*
6453149Seric **  REWRITE -- apply rewrite rules to token vector.
6463149Seric **
6474476Seric **	This routine is an ordered production system.  Each rewrite
6484476Seric **	rule has a LHS (called the pattern) and a RHS (called the
6494476Seric **	rewrite); 'rwr' points the the current rewrite rule.
6504476Seric **
6514476Seric **	For each rewrite rule, 'avp' points the address vector we
6524476Seric **	are trying to match against, and 'pvp' points to the pattern.
6538058Seric **	If pvp points to a special match value (MATCHZANY, MATCHANY,
6549585Seric **	MATCHONE, MATCHCLASS, MATCHNCLASS) then the address in avp
6559585Seric **	matched is saved away in the match vector (pointed to by 'mvp').
6564476Seric **
6574476Seric **	When a match between avp & pvp does not match, we try to
6589585Seric **	back out.  If we back up over MATCHONE, MATCHCLASS, or MATCHNCLASS
6594476Seric **	we must also back out the match in mvp.  If we reach a
6608058Seric **	MATCHANY or MATCHZANY we just extend the match and start
6618058Seric **	over again.
6624476Seric **
6634476Seric **	When we finally match, we rewrite the address vector
6644476Seric **	and try over again.
6654476Seric **
6663149Seric **	Parameters:
6673149Seric **		pvp -- pointer to token vector.
66859027Seric **		ruleset -- the ruleset to use for rewriting.
66965071Seric **		reclevel -- recursion level (to catch loops).
67059027Seric **		e -- the current envelope.
6713149Seric **
6723149Seric **	Returns:
67359084Seric **		A status code.  If EX_TEMPFAIL, higher level code should
67459084Seric **			attempt recovery.
6753149Seric **
6763149Seric **	Side Effects:
6773149Seric **		pvp is modified.
6783149Seric */
6792091Seric 
6803149Seric struct match
6813149Seric {
6824468Seric 	char	**first;	/* first token matched */
6834468Seric 	char	**last;		/* last token matched */
68458825Seric 	char	**pattern;	/* pointer to pattern */
6853149Seric };
6863149Seric 
6874468Seric # define MAXMATCH	9	/* max params per rewrite */
6883149Seric 
68965136Seric # ifndef MAXRULERECURSION
69065136Seric #  define MAXRULERECURSION	50	/* max recursion depth */
69165136Seric # endif
6923149Seric 
69365136Seric 
69459084Seric int
69565071Seric rewrite(pvp, ruleset, reclevel, e)
6963149Seric 	char **pvp;
6974070Seric 	int ruleset;
69865071Seric 	int reclevel;
69959027Seric 	register ENVELOPE *e;
7003149Seric {
7013149Seric 	register char *ap;		/* address pointer */
7023149Seric 	register char *rp;		/* rewrite pointer */
7033149Seric 	register char **avp;		/* address vector pointer */
7043149Seric 	register char **rvp;		/* rewrite vector pointer */
7058058Seric 	register struct match *mlp;	/* cur ptr into mlist */
7068058Seric 	register struct rewrite *rwr;	/* pointer to current rewrite rule */
70758866Seric 	int ruleno;			/* current rule number */
70859084Seric 	int rstat = EX_OK;		/* return status */
70964740Seric 	int loopcount;
71056678Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
7113149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
7123149Seric 
71367262Seric 	if (OpMode == MD_TEST || tTd(21, 1))
7143149Seric 	{
7158959Seric 		printf("rewrite: ruleset %2d   input:", ruleset);
71656678Seric 		printav(pvp);
7173149Seric 	}
71856678Seric 	if (ruleset < 0 || ruleset >= MAXRWSETS)
71956326Seric 	{
72058151Seric 		syserr("554 rewrite: illegal ruleset number %d", ruleset);
72159084Seric 		return EX_CONFIG;
72256326Seric 	}
72365136Seric 	if (reclevel++ > MAXRULERECURSION)
72465071Seric 	{
72565071Seric 		syserr("rewrite: infinite recursion, ruleset %d", ruleset);
72665071Seric 		return EX_CONFIG;
72765071Seric 	}
72856678Seric 	if (pvp == NULL)
72959084Seric 		return EX_USAGE;
73056326Seric 
7313149Seric 	/*
73256678Seric 	**  Run through the list of rewrite rules, applying
73356678Seric 	**	any that match.
7343149Seric 	*/
7353149Seric 
73658866Seric 	ruleno = 1;
73764740Seric 	loopcount = 0;
7384070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
7393149Seric 	{
7407675Seric 		if (tTd(21, 12))
741297Seric 		{
7428069Seric 			printf("-----trying rule:");
74356678Seric 			printav(rwr->r_lhs);
7443149Seric 		}
7453149Seric 
7463149Seric 		/* try to match on this rule */
7474468Seric 		mlp = mlist;
7488058Seric 		rvp = rwr->r_lhs;
7498058Seric 		avp = pvp;
75058866Seric 		if (++loopcount > 100)
7513149Seric 		{
75258866Seric 			syserr("554 Infinite loop in ruleset %d, rule %d",
75358866Seric 				ruleset, ruleno);
75458866Seric 			if (tTd(21, 1))
75552637Seric 			{
75656678Seric 				printf("workspace: ");
75756678Seric 				printav(pvp);
75852637Seric 			}
75958866Seric 			break;
76058866Seric 		}
76158866Seric 
76258866Seric 		while ((ap = *avp) != NULL || *rvp != NULL)
76358866Seric 		{
7643149Seric 			rp = *rvp;
7658058Seric 			if (tTd(21, 35))
7668058Seric 			{
76758825Seric 				printf("ADVANCE rp=");
76857531Seric 				xputs(rp);
76957532Seric 				printf(", ap=");
7708058Seric 				xputs(ap);
7718069Seric 				printf("\n");
7728058Seric 			}
77356678Seric 			if (rp == NULL)
77456326Seric 			{
7753149Seric 				/* end-of-pattern before end-of-address */
7768058Seric 				goto backup;
77756678Seric 			}
77858173Seric 			if (ap == NULL && (*rp & 0377) != MATCHZANY &&
77958827Seric 			    (*rp & 0377) != MATCHZERO)
78056326Seric 			{
78158825Seric 				/* end-of-input with patterns left */
78258825Seric 				goto backup;
783297Seric 			}
78456326Seric 
78558050Seric 			switch (*rp & 0377)
7868058Seric 			{
78758814Seric 				char buf[MAXLINE];
78856326Seric 
78956678Seric 			  case MATCHCLASS:
79058825Seric 				/* match any phrase in a class */
79158825Seric 				mlp->pattern = rvp;
79258814Seric 				mlp->first = avp;
79358814Seric 	extendclass:
79458825Seric 				ap = *avp;
79558825Seric 				if (ap == NULL)
79658814Seric 					goto backup;
79758814Seric 				mlp->last = avp++;
79858814Seric 				cataddr(mlp->first, mlp->last, buf, sizeof buf, '\0');
79968199Seric 				if (!wordinclass(buf, rp[1]))
80056326Seric 				{
80158825Seric 					if (tTd(21, 36))
80258825Seric 					{
80358825Seric 						printf("EXTEND  rp=");
80458825Seric 						xputs(rp);
80558825Seric 						printf(", ap=");
80658825Seric 						xputs(ap);
80758825Seric 						printf("\n");
80858825Seric 					}
80958825Seric 					goto extendclass;
81056326Seric 				}
81158825Seric 				if (tTd(21, 36))
81258825Seric 					printf("CLMATCH\n");
81358814Seric 				mlp++;
81458814Seric 				break;
8154060Seric 
81658825Seric 			  case MATCHNCLASS:
81758825Seric 				/* match any token not in a class */
81868199Seric 				if (wordinclass(ap, rp[1]))
81958825Seric 					goto backup;
82058825Seric 
82158825Seric 				/* fall through */
82258825Seric 
82356678Seric 			  case MATCHONE:
82456678Seric 			  case MATCHANY:
82556678Seric 				/* match exactly one token */
82658825Seric 				mlp->pattern = rvp;
82756678Seric 				mlp->first = avp;
82856678Seric 				mlp->last = avp++;
8298058Seric 				mlp++;
83056678Seric 				break;
8318058Seric 
83256678Seric 			  case MATCHZANY:
83356678Seric 				/* match zero or more tokens */
83458825Seric 				mlp->pattern = rvp;
83556678Seric 				mlp->first = avp;
83656678Seric 				mlp->last = avp - 1;
83756678Seric 				mlp++;
83856678Seric 				break;
83956326Seric 
84058827Seric 			  case MATCHZERO:
84158173Seric 				/* match zero tokens */
84258173Seric 				break;
84358173Seric 
84459027Seric 			  case MACRODEXPAND:
84559027Seric 				/*
84659027Seric 				**  Match against run-time macro.
84759027Seric 				**  This algorithm is broken for the
84859027Seric 				**  general case (no recursive macros,
84959027Seric 				**  improper tokenization) but should
85059027Seric 				**  work for the usual cases.
85159027Seric 				*/
85259027Seric 
85359027Seric 				ap = macvalue(rp[1], e);
85459027Seric 				mlp->first = avp;
85559027Seric 				if (tTd(21, 2))
85659027Seric 					printf("rewrite: LHS $&%c => \"%s\"\n",
85759027Seric 						rp[1],
85859027Seric 						ap == NULL ? "(NULL)" : ap);
85959027Seric 
86059027Seric 				if (ap == NULL)
86159027Seric 					break;
86260502Seric 				while (*ap != '\0')
86359027Seric 				{
86459027Seric 					if (*avp == NULL ||
86559027Seric 					    strncasecmp(ap, *avp, strlen(*avp)) != 0)
86659027Seric 					{
86759027Seric 						/* no match */
86859027Seric 						avp = mlp->first;
86959027Seric 						goto backup;
87059027Seric 					}
87159027Seric 					ap += strlen(*avp++);
87259027Seric 				}
87359027Seric 
87459027Seric 				/* match */
87559027Seric 				break;
87659027Seric 
87756678Seric 			  default:
87856678Seric 				/* must have exact match */
87956678Seric 				if (strcasecmp(rp, ap))
8808058Seric 					goto backup;
8814468Seric 				avp++;
88256678Seric 				break;
8833149Seric 			}
8843149Seric 
88556678Seric 			/* successful match on this token */
8863149Seric 			rvp++;
8873149Seric 			continue;
8883149Seric 
88958825Seric 	  backup:
89056678Seric 			/* match failed -- back up */
89158825Seric 			while (--mlp >= mlist)
8923149Seric 			{
89358825Seric 				rvp = mlp->pattern;
89456678Seric 				rp = *rvp;
89558825Seric 				avp = mlp->last + 1;
89658825Seric 				ap = *avp;
89758825Seric 
89858825Seric 				if (tTd(21, 36))
89958825Seric 				{
90058825Seric 					printf("BACKUP  rp=");
90158825Seric 					xputs(rp);
90258825Seric 					printf(", ap=");
90358825Seric 					xputs(ap);
90458825Seric 					printf("\n");
90558825Seric 				}
90658825Seric 
90758825Seric 				if (ap == NULL)
90858825Seric 				{
90958825Seric 					/* run off the end -- back up again */
91058825Seric 					continue;
91158825Seric 				}
91258050Seric 				if ((*rp & 0377) == MATCHANY ||
91358050Seric 				    (*rp & 0377) == MATCHZANY)
9144468Seric 				{
91556678Seric 					/* extend binding and continue */
91658825Seric 					mlp->last = avp++;
91756678Seric 					rvp++;
91858825Seric 					mlp++;
91956678Seric 					break;
9204468Seric 				}
92158825Seric 				if ((*rp & 0377) == MATCHCLASS)
92256678Seric 				{
92358814Seric 					/* extend binding and try again */
92463397Seric 					mlp->last = avp;
92558814Seric 					goto extendclass;
92658814Seric 				}
9273149Seric 			}
9283149Seric 
92958825Seric 			if (mlp < mlist)
93056678Seric 			{
93156678Seric 				/* total failure to match */
93256326Seric 				break;
9333149Seric 			}
934297Seric 		}
9353149Seric 
9363149Seric 		/*
93756678Seric 		**  See if we successfully matched
9383149Seric 		*/
9393149Seric 
94058827Seric 		if (mlp < mlist || *rvp != NULL)
9413149Seric 		{
9429374Seric 			if (tTd(21, 10))
9439374Seric 				printf("----- rule fails\n");
9449374Seric 			rwr = rwr->r_next;
94558866Seric 			ruleno++;
94664740Seric 			loopcount = 0;
9479374Seric 			continue;
9489374Seric 		}
9493149Seric 
9509374Seric 		rvp = rwr->r_rhs;
9519374Seric 		if (tTd(21, 12))
9529374Seric 		{
9539374Seric 			printf("-----rule matches:");
95456678Seric 			printav(rvp);
9559374Seric 		}
9569374Seric 
9579374Seric 		rp = *rvp;
95858050Seric 		if ((*rp & 0377) == CANONUSER)
9599374Seric 		{
9609374Seric 			rvp++;
9619374Seric 			rwr = rwr->r_next;
96258866Seric 			ruleno++;
96364740Seric 			loopcount = 0;
9649374Seric 		}
96558050Seric 		else if ((*rp & 0377) == CANONHOST)
9669374Seric 		{
9679374Seric 			rvp++;
9689374Seric 			rwr = NULL;
9699374Seric 		}
97058050Seric 		else if ((*rp & 0377) == CANONNET)
9719374Seric 			rwr = NULL;
9729374Seric 
9739374Seric 		/* substitute */
9749374Seric 		for (avp = npvp; *rvp != NULL; rvp++)
9759374Seric 		{
9769374Seric 			register struct match *m;
9779374Seric 			register char **pp;
9789374Seric 
9798058Seric 			rp = *rvp;
98058050Seric 			if ((*rp & 0377) == MATCHREPL)
9818058Seric 			{
98216914Seric 				/* substitute from LHS */
98316914Seric 				m = &mlist[rp[1] - '1'];
98456678Seric 				if (m < mlist || m >= mlp)
9859374Seric 				{
98658151Seric 					syserr("554 rewrite: ruleset %d: replacement $%c out of bounds",
98756326Seric 						ruleset, rp[1]);
98859084Seric 					return EX_CONFIG;
9899374Seric 				}
99016914Seric 				if (tTd(21, 15))
99116914Seric 				{
99216914Seric 					printf("$%c:", rp[1]);
99316914Seric 					pp = m->first;
99456678Seric 					while (pp <= m->last)
99516914Seric 					{
99616914Seric 						printf(" %x=\"", *pp);
99716914Seric 						(void) fflush(stdout);
99816914Seric 						printf("%s\"", *pp++);
99916914Seric 					}
100016914Seric 					printf("\n");
100116914Seric 				}
10029374Seric 				pp = m->first;
100356678Seric 				while (pp <= m->last)
10043149Seric 				{
100516914Seric 					if (avp >= &npvp[MAXATOM])
100656678Seric 					{
100758151Seric 						syserr("554 rewrite: expansion too long");
100859084Seric 						return EX_DATAERR;
100956678Seric 					}
101016914Seric 					*avp++ = *pp++;
10113149Seric 				}
10123149Seric 			}
101316914Seric 			else
10148226Seric 			{
101516914Seric 				/* vanilla replacement */
10169374Seric 				if (avp >= &npvp[MAXATOM])
101716889Seric 				{
101856678Seric 	toolong:
101958151Seric 					syserr("554 rewrite: expansion too long");
102059084Seric 					return EX_DATAERR;
102116889Seric 				}
102259027Seric 				if ((*rp & 0377) != MACRODEXPAND)
102359027Seric 					*avp++ = rp;
102459027Seric 				else
102559027Seric 				{
102659027Seric 					*avp = macvalue(rp[1], e);
102759027Seric 					if (tTd(21, 2))
102859027Seric 						printf("rewrite: RHS $&%c => \"%s\"\n",
102959027Seric 							rp[1],
103059027Seric 							*avp == NULL ? "(NULL)" : *avp);
103159027Seric 					if (*avp != NULL)
103259027Seric 						avp++;
103359027Seric 				}
10348226Seric 			}
10359374Seric 		}
10369374Seric 		*avp++ = NULL;
103716914Seric 
103816914Seric 		/*
103956678Seric 		**  Check for any hostname/keyword lookups.
104016914Seric 		*/
104116914Seric 
104216914Seric 		for (rvp = npvp; *rvp != NULL; rvp++)
104316914Seric 		{
104456678Seric 			char **hbrvp;
104516914Seric 			char **xpvp;
104616914Seric 			int trsize;
104756678Seric 			char *replac;
104856678Seric 			int endtoken;
104956678Seric 			STAB *map;
105056678Seric 			char *mapname;
105156678Seric 			char **key_rvp;
105256678Seric 			char **arg_rvp;
105356678Seric 			char **default_rvp;
105456678Seric 			char buf[MAXNAME + 1];
105516914Seric 			char *pvpb1[MAXATOM + 1];
105656823Seric 			char *argvect[10];
105717174Seric 			char pvpbuf[PSBUFSIZE];
105864404Seric 			char *nullpvp[1];
105916914Seric 
106058050Seric 			if ((**rvp & 0377) != HOSTBEGIN &&
106158050Seric 			    (**rvp & 0377) != LOOKUPBEGIN)
106216914Seric 				continue;
106316914Seric 
106416914Seric 			/*
106556678Seric 			**  Got a hostname/keyword lookup.
106616914Seric 			**
106716914Seric 			**	This could be optimized fairly easily.
106816914Seric 			*/
106916914Seric 
107016914Seric 			hbrvp = rvp;
107158050Seric 			if ((**rvp & 0377) == HOSTBEGIN)
107256327Seric 			{
107356678Seric 				endtoken = HOSTEND;
107456678Seric 				mapname = "host";
107556327Seric 			}
107656326Seric 			else
107756327Seric 			{
107856678Seric 				endtoken = LOOKUPEND;
107956678Seric 				mapname = *++rvp;
108056327Seric 			}
108156678Seric 			map = stab(mapname, ST_MAP, ST_FIND);
108256678Seric 			if (map == NULL)
108358151Seric 				syserr("554 rewrite: map %s not found", mapname);
108456678Seric 
108556678Seric 			/* extract the match part */
108656678Seric 			key_rvp = ++rvp;
108756823Seric 			default_rvp = NULL;
108856823Seric 			arg_rvp = argvect;
108956823Seric 			xpvp = NULL;
109056823Seric 			replac = pvpbuf;
109158050Seric 			while (*rvp != NULL && (**rvp & 0377) != endtoken)
109253654Seric 			{
109358050Seric 				int nodetype = **rvp & 0377;
109456823Seric 
109556823Seric 				if (nodetype != CANONHOST && nodetype != CANONUSER)
109656678Seric 				{
109756823Seric 					rvp++;
109856823Seric 					continue;
109956823Seric 				}
110056823Seric 
110156823Seric 				*rvp++ = NULL;
110256823Seric 
110356823Seric 				if (xpvp != NULL)
110456823Seric 				{
110558814Seric 					cataddr(xpvp, NULL, replac,
110658082Seric 						&pvpbuf[sizeof pvpbuf] - replac,
110758082Seric 						'\0');
110856823Seric 					*++arg_rvp = replac;
110956823Seric 					replac += strlen(replac) + 1;
111056823Seric 					xpvp = NULL;
111156823Seric 				}
111256823Seric 				switch (nodetype)
111356823Seric 				{
111456678Seric 				  case CANONHOST:
111556823Seric 					xpvp = rvp;
111656678Seric 					break;
111756678Seric 
111856678Seric 				  case CANONUSER:
111956678Seric 					default_rvp = rvp;
112056678Seric 					break;
112156678Seric 				}
112253654Seric 			}
112316914Seric 			if (*rvp != NULL)
112416914Seric 				*rvp++ = NULL;
112556823Seric 			if (xpvp != NULL)
112656823Seric 			{
112758814Seric 				cataddr(xpvp, NULL, replac,
112858082Seric 					&pvpbuf[sizeof pvpbuf] - replac,
112958082Seric 					'\0');
113056823Seric 				*++arg_rvp = replac;
113156823Seric 			}
113256823Seric 			*++arg_rvp = NULL;
113316914Seric 
113416914Seric 			/* save the remainder of the input string */
113516914Seric 			trsize = (int) (avp - rvp + 1) * sizeof *rvp;
113616914Seric 			bcopy((char *) rvp, (char *) pvpb1, trsize);
113716914Seric 
113856678Seric 			/* look it up */
113958814Seric 			cataddr(key_rvp, NULL, buf, sizeof buf, '\0');
114056823Seric 			argvect[0] = buf;
114160538Seric 			if (map != NULL && bitset(MF_OPEN, map->s_map.map_mflags))
114256836Seric 			{
114359084Seric 				auto int stat = EX_OK;
114456836Seric 
1145*69703Seric 				if (!bitset(MF_KEEPQUOTES, map->s_map.map_mflags))
1146*69703Seric 					stripquotes(buf);
1147*69703Seric 
114860215Seric 				/* XXX should try to auto-open the map here */
114960215Seric 
115058796Seric 				if (tTd(60, 1))
115158796Seric 					printf("map_lookup(%s, %s) => ",
115258796Seric 						mapname, buf);
115356836Seric 				replac = (*map->s_map.map_class->map_lookup)(&map->s_map,
115460089Seric 						buf, argvect, &stat);
115558796Seric 				if (tTd(60, 1))
115659084Seric 					printf("%s (%d)\n",
115759084Seric 						replac ? replac : "NOT FOUND",
115859084Seric 						stat);
115959084Seric 
116059084Seric 				/* should recover if stat == EX_TEMPFAIL */
116168881Seric 				if (stat == EX_TEMPFAIL)
116268706Seric 				{
116368706Seric 					rstat = EX_TEMPFAIL;
116468708Seric 					if (tTd(50, 1))
116568706Seric 						printf("map_lookup(%s, %s) failed (stat = %d)\n",
116668706Seric 							mapname, buf, stat);
116768706Seric 					if (e->e_message == NULL)
116868706Seric 					{
116968706Seric 						char mbuf[300];
117068706Seric 
117168706Seric 						sprintf(mbuf, "map %s: lookup (%s) failed",
117268706Seric 							mapname, buf);
117368706Seric 						e->e_message = newstr(mbuf);
117468706Seric 					}
117568706Seric 				}
117656836Seric 			}
117753654Seric 			else
117856678Seric 				replac = NULL;
117956678Seric 
118056678Seric 			/* if no replacement, use default */
118156823Seric 			if (replac == NULL && default_rvp != NULL)
118256823Seric 			{
118360089Seric 				/* create the default */
118460089Seric 				cataddr(default_rvp, NULL, buf, sizeof buf, '\0');
118556823Seric 				replac = buf;
118656823Seric 			}
118756823Seric 
118856678Seric 			if (replac == NULL)
118951317Seric 			{
119056823Seric 				xpvp = key_rvp;
119153654Seric 			}
119264404Seric 			else if (*replac == '\0')
119364404Seric 			{
119464404Seric 				/* null replacement */
119564404Seric 				nullpvp[0] = NULL;
119664404Seric 				xpvp = nullpvp;
119764404Seric 			}
119856678Seric 			else
119953654Seric 			{
120056678Seric 				/* scan the new replacement */
120165066Seric 				xpvp = prescan(replac, '\0', pvpbuf,
120268711Seric 					       sizeof pvpbuf, NULL, NULL);
120353654Seric 				if (xpvp == NULL)
120451317Seric 				{
120558403Seric 					/* prescan already printed error */
120659084Seric 					return EX_DATAERR;
120756678Seric 				}
120851317Seric 			}
120951317Seric 
121016914Seric 			/* append it to the token list */
121156678Seric 			for (avp = hbrvp; *xpvp != NULL; xpvp++)
121256678Seric 			{
121317174Seric 				*avp++ = newstr(*xpvp);
121416920Seric 				if (avp >= &npvp[MAXATOM])
121516914Seric 					goto toolong;
121617174Seric 			}
121716914Seric 
121816914Seric 			/* restore the old trailing information */
121956678Seric 			for (xpvp = pvpb1; (*avp++ = *xpvp++) != NULL; )
122016920Seric 				if (avp >= &npvp[MAXATOM])
122116914Seric 					goto toolong;
122217174Seric 
122356678Seric 			break;
122416914Seric 		}
122516914Seric 
122616914Seric 		/*
122716914Seric 		**  Check for subroutine calls.
122816914Seric 		*/
122916914Seric 
123068559Seric 		if (*npvp != NULL && (**npvp & 0377) == CALLSUBR)
123168559Seric 		{
123268559Seric 			int stat;
123359084Seric 
123468559Seric 			if (npvp[1] == NULL)
123568559Seric 			{
123668559Seric 				syserr("parseaddr: NULL subroutine call in ruleset %d, rule %d",
123768559Seric 					ruleset, ruleno);
123868559Seric 				*pvp = NULL;
123968559Seric 			}
124068559Seric 			else
124168559Seric 			{
124268559Seric 				int ruleset;
124368559Seric 				STAB *s;
124468385Seric 
124568559Seric 				bcopy((char *) &npvp[2], (char *) pvp,
124668559Seric 					(int) (avp - npvp - 2) * sizeof *avp);
124768559Seric 				if (tTd(21, 3))
124868559Seric 					printf("-----callsubr %s\n", npvp[1]);
124968559Seric 				s = stab(npvp[1], ST_RULESET, ST_FIND);
125068559Seric 				if (s == NULL)
125168559Seric 					ruleset = atoi(npvp[1]);
125268559Seric 				else
125368559Seric 					ruleset = s->s_ruleset;
125468559Seric 				stat = rewrite(pvp, ruleset, reclevel, e);
125568559Seric 				if (rstat == EX_OK || stat == EX_TEMPFAIL)
125668559Seric 					rstat = stat;
125768559Seric 				if (*pvp != NULL && (**pvp & 0377) == CANONNET)
125868559Seric 				rwr = NULL;
125968559Seric 			}
126068559Seric 		}
126168559Seric 		else
126268559Seric 		{
126368559Seric 			bcopy((char *) npvp, (char *) pvp,
126468559Seric 				(int) (avp - npvp) * sizeof *avp);
126568559Seric 		}
12669374Seric 		if (tTd(21, 4))
12679374Seric 		{
12689374Seric 			printf("rewritten as:");
126956678Seric 			printav(pvp);
12709374Seric 		}
1271297Seric 	}
12728069Seric 
127367262Seric 	if (OpMode == MD_TEST || tTd(21, 1))
12748069Seric 	{
12758959Seric 		printf("rewrite: ruleset %2d returns:", ruleset);
127656678Seric 		printav(pvp);
12778069Seric 	}
127859084Seric 
127959084Seric 	return rstat;
12803149Seric }
12813149Seric /*
12823149Seric **  BUILDADDR -- build address from token vector.
12833149Seric **
12843149Seric **	Parameters:
12853149Seric **		tv -- token vector.
12863149Seric **		a -- pointer to address descriptor to fill.
12873149Seric **			If NULL, one will be allocated.
128864284Seric **		flags -- info regarding whether this is a sender or
128964284Seric **			a recipient.
129058966Seric **		e -- the current envelope.
12913149Seric **
12923149Seric **	Returns:
12934279Seric **		NULL if there was an error.
12944279Seric **		'a' otherwise.
12953149Seric **
12963149Seric **	Side Effects:
12973149Seric **		fills in 'a'
12983149Seric */
12993149Seric 
130057249Seric struct errcodes
130157249Seric {
130257249Seric 	char	*ec_name;		/* name of error code */
130357249Seric 	int	ec_code;		/* numeric code */
130457249Seric } ErrorCodes[] =
130557249Seric {
130657249Seric 	"usage",	EX_USAGE,
130757249Seric 	"nouser",	EX_NOUSER,
130857249Seric 	"nohost",	EX_NOHOST,
130957249Seric 	"unavailable",	EX_UNAVAILABLE,
131057249Seric 	"software",	EX_SOFTWARE,
131157249Seric 	"tempfail",	EX_TEMPFAIL,
131257249Seric 	"protocol",	EX_PROTOCOL,
131357249Seric #ifdef EX_CONFIG
131457249Seric 	"config",	EX_CONFIG,
131557249Seric #endif
131657249Seric 	NULL,		EX_UNAVAILABLE,
131757249Seric };
131857249Seric 
131956678Seric ADDRESS *
132064284Seric buildaddr(tv, a, flags, e)
13213149Seric 	register char **tv;
13223149Seric 	register ADDRESS *a;
132364284Seric 	int flags;
132458966Seric 	register ENVELOPE *e;
13253149Seric {
13263149Seric 	struct mailer **mp;
13273149Seric 	register struct mailer *m;
132868849Seric 	register char *p;
132958008Seric 	char *bp;
133068849Seric 	char *mname;
133168849Seric 	char **hostp;
133268849Seric 	char hbuf[MAXNAME + 1];
133364306Seric 	static MAILER errormailer;
133464306Seric 	static char *errorargv[] = { "ERROR", NULL };
133568849Seric 	static char ubuf[MAXNAME + 1];
13363149Seric 
133764791Seric 	if (tTd(24, 5))
133864791Seric 	{
133967693Seric 		printf("buildaddr, flags=%x, tv=", flags);
134064791Seric 		printav(tv);
134164791Seric 	}
134264791Seric 
13433149Seric 	if (a == NULL)
13443149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
134516889Seric 	bzero((char *) a, sizeof *a);
13463149Seric 
134767880Seric 	/* set up default error return flags */
134867963Seric 	a->q_flags |= QPINGONFAILURE|QPINGONDELAY;
134967880Seric 
13503149Seric 	/* figure out what net/mailer to use */
135164306Seric 	if (*tv == NULL || (**tv & 0377) != CANONNET)
13524279Seric 	{
135358151Seric 		syserr("554 buildaddr: no net");
135464306Seric badaddr:
135564306Seric 		a->q_flags |= QBADADDR;
135664306Seric 		a->q_mailer = &errormailer;
135764306Seric 		if (errormailer.m_name == NULL)
135864306Seric 		{
135964306Seric 			/* initialize the bogus mailer */
136064306Seric 			errormailer.m_name = "*error*";
136164306Seric 			errormailer.m_mailer = "ERROR";
136264306Seric 			errormailer.m_argv = errorargv;
136364306Seric 		}
136464306Seric 		return a;
13654279Seric 	}
136668849Seric 	mname = *++tv;
136768849Seric 
136868849Seric 	/* extract host and user portions */
136968849Seric 	if ((**++tv & 0377) == CANONHOST)
137068849Seric 		hostp = ++tv;
137168849Seric 	else
137268849Seric 		hostp = NULL;
137368849Seric 	while (*tv != NULL && (**tv & 0377) != CANONUSER)
137468849Seric 		tv++;
137568849Seric 	if (*tv == NULL)
13764279Seric 	{
137768849Seric 		syserr("554 buildaddr: no user");
137868849Seric 		goto badaddr;
137968849Seric 	}
138068849Seric 	if (hostp != NULL)
138168849Seric 		cataddr(hostp, tv - 1, hbuf, sizeof hbuf, '\0');
138268849Seric 	cataddr(++tv, NULL, ubuf, sizeof ubuf, '\0');
138368849Seric 
138468849Seric 	/* save away the host name */
138568849Seric 	if (strcasecmp(mname, "error") == 0)
138668849Seric 	{
138768849Seric 		if (hostp != NULL)
138810183Seric 		{
138957249Seric 			register struct errcodes *ep;
139057249Seric 
139168849Seric 			if (strchr(hbuf, '.') != NULL)
139257249Seric 			{
139368849Seric 				a->q_status = newstr(hbuf);
139468849Seric 				setstat(dsntoexitstat(hbuf));
139557249Seric 			}
139668849Seric 			else if (isascii(hbuf[0]) && isdigit(hbuf[0]))
139768849Seric 			{
139868849Seric 				setstat(atoi(hbuf));
139968849Seric 			}
140057249Seric 			else
140157249Seric 			{
140257249Seric 				for (ep = ErrorCodes; ep->ec_name != NULL; ep++)
140368849Seric 					if (strcasecmp(ep->ec_name, hbuf) == 0)
140457249Seric 						break;
140557249Seric 				setstat(ep->ec_code);
140657249Seric 			}
140710183Seric 		}
140864928Seric 		else
140964928Seric 			setstat(EX_UNAVAILABLE);
141068849Seric 		stripquotes(ubuf);
141168849Seric 		if (isascii(ubuf[0]) && isdigit(ubuf[0]) &&
141268849Seric 		    isascii(ubuf[1]) && isdigit(ubuf[1]) &&
141368849Seric 		    isascii(ubuf[2]) && isdigit(ubuf[2]) &&
141468849Seric 		    ubuf[3] == ' ')
141564659Seric 		{
141664659Seric 			char fmt[10];
141764659Seric 
141868849Seric 			strncpy(fmt, ubuf, 3);
141964659Seric 			strcpy(&fmt[3], " %s");
142068849Seric 			usrerr(fmt, ubuf + 4);
142167786Seric 
142267786Seric 			/*
142367786Seric 			**  If this is a 4xx code and we aren't running
142467786Seric 			**  SMTP on our input, bounce this message;
142567786Seric 			**  otherwise it disappears without a trace.
142667786Seric 			*/
142767786Seric 
142867786Seric 			if (fmt[0] == '4' && OpMode != MD_SMTP &&
142967786Seric 			    OpMode != MD_DAEMON)
143067786Seric 			{
143167786Seric 				e->e_flags |= EF_FATALERRS;
143267786Seric 			}
143364659Seric 		}
143464659Seric 		else
143564659Seric 		{
143668849Seric 			usrerr("553 %s", ubuf);
143764659Seric 		}
143864306Seric 		goto badaddr;
14394279Seric 	}
144057402Seric 
14414598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
14423149Seric 	{
144368849Seric 		if (strcasecmp(m->m_name, mname) == 0)
14443149Seric 			break;
14453149Seric 	}
14463149Seric 	if (m == NULL)
14474279Seric 	{
144868849Seric 		syserr("554 buildaddr: unknown mailer %s", mname);
144964306Seric 		goto badaddr;
14504279Seric 	}
14514598Seric 	a->q_mailer = m;
14523149Seric 
14533149Seric 	/* figure out what host (if any) */
145468849Seric 	if (hostp == NULL)
14553149Seric 	{
145658509Seric 		if (!bitnset(M_LOCALMAILER, m->m_flags))
145758509Seric 		{
145858509Seric 			syserr("554 buildaddr: no host");
145964306Seric 			goto badaddr;
146058509Seric 		}
146157249Seric 		a->q_host = NULL;
146258509Seric 	}
146368849Seric 	else
146468849Seric 		a->q_host = newstr(hbuf);
14653149Seric 
14663149Seric 	/* figure out the user */
146768849Seric 	p = ubuf;
146868849Seric 	if (bitnset(M_CHECKUDB, m->m_flags) && *p == '@')
14694279Seric 	{
147068849Seric 		p++;
147168098Seric 		tv++;
147268098Seric 		a->q_flags |= QNOTREMOTE;
147368098Seric 	}
147468098Seric 
147557402Seric 	/* do special mapping for local mailer */
147668849Seric 	if (*p == '"')
147768849Seric 		p++;
147868849Seric 	if (*p == '|' && bitnset(M_CHECKPROG, m->m_flags))
147968849Seric 		a->q_mailer = m = ProgMailer;
148068849Seric 	else if (*p == '/' && bitnset(M_CHECKFILE, m->m_flags))
148168849Seric 		a->q_mailer = m = FileMailer;
148268849Seric 	else if (*p == ':' && bitnset(M_CHECKINCLUDE, m->m_flags))
148357402Seric 	{
148468849Seric 		/* may be :include: */
148568849Seric 		stripquotes(ubuf);
148668849Seric 		if (strncasecmp(ubuf, ":include:", 9) == 0)
148757402Seric 		{
148868849Seric 			/* if :include:, don't need further rewriting */
148968849Seric 			a->q_mailer = m = InclMailer;
149068849Seric 			a->q_user = newstr(&ubuf[9]);
149168849Seric 			return a;
149257402Seric 		}
149357402Seric 	}
149457402Seric 
149564284Seric 	/* rewrite according recipient mailer rewriting rules */
149664284Seric 	define('h', a->q_host, e);
149764284Seric 	if (!bitset(RF_SENDERADDR|RF_HEADERADDR, flags))
149864284Seric 	{
149964284Seric 		/* sender addresses done later */
150065071Seric 		(void) rewrite(tv, 2, 0, e);
150164284Seric 		if (m->m_re_rwset > 0)
150265071Seric 		       (void) rewrite(tv, m->m_re_rwset, 0, e);
150364284Seric 	}
150465071Seric 	(void) rewrite(tv, 4, 0, e);
150519040Seric 
150619040Seric 	/* save the result for the command line/RCPT argument */
150768849Seric 	cataddr(tv, NULL, ubuf, sizeof ubuf, '\0');
150868849Seric 	a->q_user = ubuf;
15093149Seric 
151058670Seric 	/*
151158670Seric 	**  Do mapping to lower case as requested by mailer
151258670Seric 	*/
151358670Seric 
151458670Seric 	if (a->q_host != NULL && !bitnset(M_HST_UPPER, m->m_flags))
151558670Seric 		makelower(a->q_host);
151658670Seric 	if (!bitnset(M_USR_UPPER, m->m_flags))
151758670Seric 		makelower(a->q_user);
151858670Seric 
151968849Seric 	return a;
15203149Seric }
15213188Seric /*
15224228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
15234228Seric **
15244228Seric **	Parameters:
15254228Seric **		pvp -- parameter vector to rebuild.
152658814Seric **		evp -- last parameter to include.  Can be NULL to
152758814Seric **			use entire pvp.
15284228Seric **		buf -- buffer to build the string into.
15294228Seric **		sz -- size of buf.
153058082Seric **		spacesub -- the space separator character; if null,
153158082Seric **			use SpaceSub.
15324228Seric **
15334228Seric **	Returns:
15344228Seric **		none.
15354228Seric **
15364228Seric **	Side Effects:
15374228Seric **		Destroys buf.
15384228Seric */
15394228Seric 
154058814Seric cataddr(pvp, evp, buf, sz, spacesub)
15414228Seric 	char **pvp;
154258814Seric 	char **evp;
15434228Seric 	char *buf;
15444228Seric 	register int sz;
154558082Seric 	char spacesub;
15464228Seric {
15474228Seric 	bool oatomtok = FALSE;
154856678Seric 	bool natomtok = FALSE;
15494228Seric 	register int i;
15504228Seric 	register char *p;
15514228Seric 
155258082Seric 	if (spacesub == '\0')
155358082Seric 		spacesub = SpaceSub;
155458082Seric 
15558423Seric 	if (pvp == NULL)
15568423Seric 	{
155723109Seric 		(void) strcpy(buf, "");
15588423Seric 		return;
15598423Seric 	}
15604228Seric 	p = buf;
156111156Seric 	sz -= 2;
15624228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
15634228Seric 	{
156468711Seric 		natomtok = (TokTypeTab[**pvp & 0xff] == ATM);
15654228Seric 		if (oatomtok && natomtok)
156658082Seric 			*p++ = spacesub;
15674228Seric 		(void) strcpy(p, *pvp);
15684228Seric 		oatomtok = natomtok;
15694228Seric 		p += i;
157011156Seric 		sz -= i + 1;
157158814Seric 		if (pvp++ == evp)
157258814Seric 			break;
15734228Seric 	}
15744228Seric 	*p = '\0';
15754228Seric }
15764228Seric /*
15773188Seric **  SAMEADDR -- Determine if two addresses are the same
15783188Seric **
15793188Seric **	This is not just a straight comparison -- if the mailer doesn't
15803188Seric **	care about the host we just ignore it, etc.
15813188Seric **
15823188Seric **	Parameters:
15833188Seric **		a, b -- pointers to the internal forms to compare.
15843188Seric **
15853188Seric **	Returns:
15863188Seric **		TRUE -- they represent the same mailbox.
15873188Seric **		FALSE -- they don't.
15883188Seric **
15893188Seric **	Side Effects:
15903188Seric **		none.
15913188Seric */
15923188Seric 
15933188Seric bool
15949374Seric sameaddr(a, b)
15953188Seric 	register ADDRESS *a;
15963188Seric 	register ADDRESS *b;
15973188Seric {
159865093Seric 	register ADDRESS *ca, *cb;
159965093Seric 
16003188Seric 	/* if they don't have the same mailer, forget it */
16013188Seric 	if (a->q_mailer != b->q_mailer)
16023188Seric 		return (FALSE);
16033188Seric 
16043188Seric 	/* if the user isn't the same, we can drop out */
160556678Seric 	if (strcmp(a->q_user, b->q_user) != 0)
16063188Seric 		return (FALSE);
16073188Seric 
160865093Seric 	/* if we have good uids for both but they differ, these are different */
160965379Seric 	if (a->q_mailer == ProgMailer)
161065379Seric 	{
161165379Seric 		ca = getctladdr(a);
161265379Seric 		cb = getctladdr(b);
161365379Seric 		if (ca != NULL && cb != NULL &&
161465379Seric 		    bitset(QGOODUID, ca->q_flags & cb->q_flags) &&
161565379Seric 		    ca->q_uid != cb->q_uid)
161665379Seric 			return (FALSE);
161765379Seric 	}
161858438Seric 
161958509Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
162058509Seric 	if (a->q_host == b->q_host)
162158509Seric 	{
162258509Seric 		/* probably both null pointers */
16233188Seric 		return (TRUE);
162458509Seric 	}
16253188Seric 	if (a->q_host == NULL || b->q_host == NULL)
162658509Seric 	{
162758509Seric 		/* only one is a null pointer */
16283188Seric 		return (FALSE);
162958509Seric 	}
163056678Seric 	if (strcmp(a->q_host, b->q_host) != 0)
16313188Seric 		return (FALSE);
16323188Seric 
16333188Seric 	return (TRUE);
16343188Seric }
16353234Seric /*
16363234Seric **  PRINTADDR -- print address (for debugging)
16373234Seric **
16383234Seric **	Parameters:
16393234Seric **		a -- the address to print
16403234Seric **		follow -- follow the q_next chain.
16413234Seric **
16423234Seric **	Returns:
16433234Seric **		none.
16443234Seric **
16453234Seric **	Side Effects:
16463234Seric **		none.
16473234Seric */
16483234Seric 
164967994Seric struct qflags
165067994Seric {
165167994Seric 	char	*qf_name;
165267994Seric 	u_long	qf_bit;
165367994Seric };
165467994Seric 
165567994Seric struct qflags	AddressFlags[] =
165667994Seric {
165767994Seric 	"QDONTSEND",		QDONTSEND,
165867994Seric 	"QBADADDR",		QBADADDR,
165967994Seric 	"QGOODUID",		QGOODUID,
166067994Seric 	"QPRIMARY",		QPRIMARY,
166167994Seric 	"QQUEUEUP",		QQUEUEUP,
166267994Seric 	"QSENT",		QSENT,
166367994Seric 	"QNOTREMOTE",		QNOTREMOTE,
166467994Seric 	"QSELFREF",		QSELFREF,
166567994Seric 	"QVERIFIED",		QVERIFIED,
166667994Seric 	"QBOGUSSHELL",		QBOGUSSHELL,
166767994Seric 	"QUNSAFEADDR",		QUNSAFEADDR,
166867994Seric 	"QPINGONSUCCESS",	QPINGONSUCCESS,
166967994Seric 	"QPINGONFAILURE",	QPINGONFAILURE,
167067994Seric 	"QPINGONDELAY",		QPINGONDELAY,
167168595Seric 	"QHASNOTIFY",		QHASNOTIFY,
167267994Seric 	"QRELAYED",		QRELAYED,
167368867Seric 	"QEXPANDED",		QEXPANDED,
167468867Seric 	"QDELIVERED",		QDELIVERED,
167568867Seric 	"QDELAYED",		QDELAYED,
167668603Seric 	"QTHISPASS",		QTHISPASS,
167767994Seric 	NULL
167867994Seric };
167967994Seric 
168068433Seric void
16813234Seric printaddr(a, follow)
16823234Seric 	register ADDRESS *a;
16833234Seric 	bool follow;
16843234Seric {
168557731Seric 	register MAILER *m;
168657731Seric 	MAILER pseudomailer;
168767994Seric 	register struct qflags *qfp;
168867994Seric 	bool firstone;
16895001Seric 
169067994Seric 	if (a == NULL)
169167994Seric 	{
169267994Seric 		printf("[NULL]\n");
169367994Seric 		return;
169467994Seric 	}
169567994Seric 
16963234Seric 	while (a != NULL)
16973234Seric 	{
16984443Seric 		printf("%x=", a);
16994085Seric 		(void) fflush(stdout);
170057731Seric 
170157731Seric 		/* find the mailer -- carefully */
170257731Seric 		m = a->q_mailer;
170357731Seric 		if (m == NULL)
170457731Seric 		{
170557731Seric 			m = &pseudomailer;
170657731Seric 			m->m_mno = -1;
170757731Seric 			m->m_name = "NULL";
170857731Seric 		}
170957731Seric 
171068603Seric 		printf("%s:\n\tmailer %d (%s), host `%s'\n",
171157731Seric 		       a->q_paddr, m->m_mno, m->m_name,
171268603Seric 		       a->q_host == NULL ? "<null>" : a->q_host);
171368603Seric 		printf("\tuser `%s', ruser `%s'\n",
171468603Seric 		       a->q_user,
171567172Seric 		       a->q_ruser == NULL ? "<null>" : a->q_ruser);
171667994Seric 		printf("\tnext=%x, alias %x, uid %d, gid %d\n",
171767994Seric 		       a->q_next, a->q_alias, a->q_uid, a->q_gid);
171867994Seric 		printf("\tflags=%lx<", a->q_flags);
171967994Seric 		firstone = TRUE;
172067994Seric 		for (qfp = AddressFlags; qfp->qf_name != NULL; qfp++)
172167994Seric 		{
172267994Seric 			if (!bitset(qfp->qf_bit, a->q_flags))
172367994Seric 				continue;
172467994Seric 			if (!firstone)
172567994Seric 				printf(",");
172667994Seric 			firstone = FALSE;
172767994Seric 			printf("%s", qfp->qf_name);
172867994Seric 		}
172967994Seric 		printf(">\n");
173059269Seric 		printf("\towner=%s, home=\"%s\", fullname=\"%s\"\n",
173159269Seric 		       a->q_owner == NULL ? "(none)" : a->q_owner,
173263756Seric 		       a->q_home == NULL ? "(none)" : a->q_home,
173363756Seric 		       a->q_fullname == NULL ? "(none)" : a->q_fullname);
173468228Seric 		printf("\torcpt=\"%s\", statmta=%s, rstatus=%s\n",
173567987Seric 		       a->q_orcpt == NULL ? "(none)" : a->q_orcpt,
173667987Seric 		       a->q_statmta == NULL ? "(none)" : a->q_statmta,
173767990Seric 		       a->q_rstatus == NULL ? "(none)" : a->q_rstatus);
17384996Seric 
17393234Seric 		if (!follow)
17403234Seric 			return;
17414996Seric 		a = a->q_next;
17423234Seric 	}
17433234Seric }
174467939Seric /*
174567939Seric **  EMPTYADDR -- return TRUE if this address is empty (``<>'')
174667939Seric **
174767939Seric **	Parameters:
174867939Seric **		a -- pointer to the address
174967939Seric **
175067939Seric **	Returns:
175167939Seric **		TRUE -- if this address is "empty" (i.e., no one should
175267939Seric **			ever generate replies to it.
175367939Seric **		FALSE -- if it is a "regular" (read: replyable) address.
175467939Seric */
17554317Seric 
175667939Seric bool
175767939Seric emptyaddr(a)
175867939Seric 	register ADDRESS *a;
175967939Seric {
176067939Seric 	return strcmp(a->q_paddr, "<>") == 0 || strcmp(a->q_user, "<>") == 0;
176167939Seric }
17627682Seric /*
17637682Seric **  REMOTENAME -- return the name relative to the current mailer
17647682Seric **
17657682Seric **	Parameters:
17667682Seric **		name -- the name to translate.
17678069Seric **		m -- the mailer that we want to do rewriting relative
17688069Seric **			to.
176959163Seric **		flags -- fine tune operations.
177059163Seric **		pstat -- pointer to status word.
177158020Seric **		e -- the current envelope.
17727682Seric **
17737682Seric **	Returns:
17747682Seric **		the text string representing this address relative to
17757682Seric **			the receiving mailer.
17767682Seric **
17777682Seric **	Side Effects:
17787682Seric **		none.
17797682Seric **
17807682Seric **	Warnings:
17817682Seric **		The text string returned is tucked away locally;
17827682Seric **			copy it if you intend to save it.
17837682Seric */
17847682Seric 
17857682Seric char *
178659163Seric remotename(name, m, flags, pstat, e)
17877682Seric 	char *name;
178856678Seric 	struct mailer *m;
178959163Seric 	int flags;
179059163Seric 	int *pstat;
179156678Seric 	register ENVELOPE *e;
17927682Seric {
17938069Seric 	register char **pvp;
17948069Seric 	char *fancy;
179556678Seric 	char *oldg = macvalue('g', e);
179658020Seric 	int rwset;
179768528Seric 	static char buf[MAXNAME + 1];
179868528Seric 	char lbuf[MAXNAME + 1];
179916914Seric 	char pvpbuf[PSBUFSIZE];
180056678Seric 	extern char *crackaddr();
18017682Seric 
18027755Seric 	if (tTd(12, 1))
18037755Seric 		printf("remotename(%s)\n", name);
18047755Seric 
180510177Seric 	/* don't do anything if we are tagging it as special */
180659163Seric 	if (bitset(RF_SENDERADDR, flags))
180759163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_sh_rwset
180859163Seric 						     : m->m_se_rwset;
180958020Seric 	else
181059163Seric 		rwset = bitset(RF_HEADERADDR, flags) ? m->m_rh_rwset
181159163Seric 						     : m->m_re_rwset;
181258020Seric 	if (rwset < 0)
181310177Seric 		return (name);
181410177Seric 
18157682Seric 	/*
18168181Seric 	**  Do a heuristic crack of this name to extract any comment info.
18178181Seric 	**	This will leave the name as a comment and a $g macro.
18187889Seric 	*/
18197889Seric 
182059163Seric 	if (bitset(RF_CANONICAL, flags) || bitnset(M_NOCOMMENT, m->m_flags))
182158050Seric 		fancy = "\201g";
182210310Seric 	else
182310310Seric 		fancy = crackaddr(name);
18247889Seric 
18258181Seric 	/*
18268181Seric 	**  Turn the name into canonical form.
18278181Seric 	**	Normally this will be RFC 822 style, i.e., "user@domain".
18288181Seric 	**	If this only resolves to "user", and the "C" flag is
18298181Seric 	**	specified in the sending mailer, then the sender's
18308181Seric 	**	domain will be appended.
18318181Seric 	*/
18328181Seric 
183368711Seric 	pvp = prescan(name, '\0', pvpbuf, sizeof pvpbuf, NULL, NULL);
18347889Seric 	if (pvp == NULL)
18357889Seric 		return (name);
183665071Seric 	if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL)
183759163Seric 		*pstat = EX_TEMPFAIL;
183859163Seric 	if (bitset(RF_ADDDOMAIN, flags) && e->e_fromdomain != NULL)
18398181Seric 	{
18408181Seric 		/* append from domain to this address */
18418181Seric 		register char **pxp = pvp;
18428181Seric 
18439594Seric 		/* see if there is an "@domain" in the current name */
18448181Seric 		while (*pxp != NULL && strcmp(*pxp, "@") != 0)
18458181Seric 			pxp++;
18468181Seric 		if (*pxp == NULL)
18478181Seric 		{
18489594Seric 			/* no.... append the "@domain" from the sender */
184956678Seric 			register char **qxq = e->e_fromdomain;
18508181Seric 
18519594Seric 			while ((*pxp++ = *qxq++) != NULL)
18529594Seric 				continue;
185365071Seric 			if (rewrite(pvp, 3, 0, e) == EX_TEMPFAIL)
185459163Seric 				*pstat = EX_TEMPFAIL;
18558181Seric 		}
18568181Seric 	}
18578181Seric 
18588181Seric 	/*
18598959Seric 	**  Do more specific rewriting.
186056678Seric 	**	Rewrite using ruleset 1 or 2 depending on whether this is
186156678Seric 	**		a sender address or not.
18628181Seric 	**	Then run it through any receiving-mailer-specific rulesets.
18638181Seric 	*/
18648181Seric 
186559163Seric 	if (bitset(RF_SENDERADDR, flags))
186659541Seric 	{
186765071Seric 		if (rewrite(pvp, 1, 0, e) == EX_TEMPFAIL)
186859163Seric 			*pstat = EX_TEMPFAIL;
186959541Seric 	}
18708069Seric 	else
187159541Seric 	{
187265071Seric 		if (rewrite(pvp, 2, 0, e) == EX_TEMPFAIL)
187359163Seric 			*pstat = EX_TEMPFAIL;
187459541Seric 	}
187558020Seric 	if (rwset > 0)
187659541Seric 	{
187765071Seric 		if (rewrite(pvp, rwset, 0, e) == EX_TEMPFAIL)
187859163Seric 			*pstat = EX_TEMPFAIL;
187959541Seric 	}
18807682Seric 
18818181Seric 	/*
18828959Seric 	**  Do any final sanitation the address may require.
18838959Seric 	**	This will normally be used to turn internal forms
18848959Seric 	**	(e.g., user@host.LOCAL) into external form.  This
18858959Seric 	**	may be used as a default to the above rules.
18868959Seric 	*/
18878959Seric 
188865071Seric 	if (rewrite(pvp, 4, 0, e) == EX_TEMPFAIL)
188959163Seric 		*pstat = EX_TEMPFAIL;
18908959Seric 
18918959Seric 	/*
18928181Seric 	**  Now restore the comment information we had at the beginning.
18938181Seric 	*/
18948181Seric 
189558825Seric 	cataddr(pvp, NULL, lbuf, sizeof lbuf, '\0');
189656678Seric 	define('g', lbuf, e);
189765494Seric 
189865494Seric 	/* need to make sure route-addrs have <angle brackets> */
189965494Seric 	if (bitset(RF_CANONICAL, flags) && lbuf[0] == '@')
190068529Seric 		expand("<\201g>", buf, sizeof buf, e);
190165494Seric 	else
190268529Seric 		expand(fancy, buf, sizeof buf, e);
190365494Seric 
190456678Seric 	define('g', oldg, e);
19057682Seric 
19067682Seric 	if (tTd(12, 1))
19077755Seric 		printf("remotename => `%s'\n", buf);
19087682Seric 	return (buf);
19097682Seric }
191051317Seric /*
191156678Seric **  MAPLOCALUSER -- run local username through ruleset 5 for final redirection
191251317Seric **
191351317Seric **	Parameters:
191456678Seric **		a -- the address to map (but just the user name part).
191556678Seric **		sendq -- the sendq in which to install any replacement
191656678Seric **			addresses.
191767982Seric **		aliaslevel -- the alias nesting depth.
191867982Seric **		e -- the envelope.
191951317Seric **
192051317Seric **	Returns:
192151317Seric **		none.
192251317Seric */
192351317Seric 
192467982Seric maplocaluser(a, sendq, aliaslevel, e)
192556678Seric 	register ADDRESS *a;
192656678Seric 	ADDRESS **sendq;
192767982Seric 	int aliaslevel;
192856678Seric 	ENVELOPE *e;
192951317Seric {
193056678Seric 	register char **pvp;
193156678Seric 	register ADDRESS *a1 = NULL;
193258333Seric 	auto char *delimptr;
193356678Seric 	char pvpbuf[PSBUFSIZE];
193451317Seric 
193556678Seric 	if (tTd(29, 1))
193656678Seric 	{
193756678Seric 		printf("maplocaluser: ");
193856678Seric 		printaddr(a, FALSE);
193956678Seric 	}
194068711Seric 	pvp = prescan(a->q_user, '\0', pvpbuf, sizeof pvpbuf, &delimptr, NULL);
194156678Seric 	if (pvp == NULL)
194256678Seric 		return;
194351317Seric 
194465071Seric 	(void) rewrite(pvp, 5, 0, e);
194558050Seric 	if (pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET)
194656678Seric 		return;
194751317Seric 
194856678Seric 	/* if non-null, mailer destination specified -- has it changed? */
194964284Seric 	a1 = buildaddr(pvp, NULL, 0, e);
195056678Seric 	if (a1 == NULL || sameaddr(a, a1))
195156678Seric 		return;
195251317Seric 
195356678Seric 	/* mark old address as dead; insert new address */
195456678Seric 	a->q_flags |= QDONTSEND;
195557731Seric 	if (tTd(29, 5))
195657731Seric 	{
195757731Seric 		printf("maplocaluser: QDONTSEND ");
195857731Seric 		printaddr(a, FALSE);
195957731Seric 	}
196056678Seric 	a1->q_alias = a;
196164348Seric 	allocaddr(a1, RF_COPYALL, NULL);
196267982Seric 	(void) recipient(a1, sendq, aliaslevel, e);
196351317Seric }
196458802Seric /*
196558802Seric **  DEQUOTE_INIT -- initialize dequote map
196658802Seric **
196758802Seric **	This is a no-op.
196858802Seric **
196958802Seric **	Parameters:
197058802Seric **		map -- the internal map structure.
197158802Seric **		args -- arguments.
197258802Seric **
197358802Seric **	Returns:
197458802Seric **		TRUE.
197558802Seric */
197658802Seric 
197758802Seric bool
197860219Seric dequote_init(map, args)
197958802Seric 	MAP *map;
198058802Seric 	char *args;
198158802Seric {
198258805Seric 	register char *p = args;
198358805Seric 
198458805Seric 	for (;;)
198558805Seric 	{
198658805Seric 		while (isascii(*p) && isspace(*p))
198758805Seric 			p++;
198858805Seric 		if (*p != '-')
198958805Seric 			break;
199058805Seric 		switch (*++p)
199158805Seric 		{
199258805Seric 		  case 'a':
199358805Seric 			map->map_app = ++p;
199458805Seric 			break;
199567824Seric 
199667824Seric 		  case 's':
199767824Seric 			map->map_coldelim = *++p;
199867824Seric 			break;
199958805Seric 		}
200058805Seric 		while (*p != '\0' && !(isascii(*p) && isspace(*p)))
200158805Seric 			p++;
200258805Seric 		if (*p != '\0')
200358805Seric 			*p = '\0';
200458805Seric 	}
200558805Seric 	if (map->map_app != NULL)
200658805Seric 		map->map_app = newstr(map->map_app);
200758805Seric 
200858802Seric 	return TRUE;
200958802Seric }
201058802Seric /*
201158802Seric **  DEQUOTE_MAP -- unquote an address
201258802Seric **
201358802Seric **	Parameters:
201458802Seric **		map -- the internal map structure (ignored).
201560089Seric **		name -- the name to dequote.
201658802Seric **		av -- arguments (ignored).
201759084Seric **		statp -- pointer to status out-parameter.
201858802Seric **
201958802Seric **	Returns:
202058802Seric **		NULL -- if there were no quotes, or if the resulting
202158802Seric **			unquoted buffer would not be acceptable to prescan.
202258802Seric **		else -- The dequoted buffer.
202358802Seric */
202458802Seric 
202558802Seric char *
202660089Seric dequote_map(map, name, av, statp)
202758802Seric 	MAP *map;
202860089Seric 	char *name;
202958802Seric 	char **av;
203059084Seric 	int *statp;
203158802Seric {
203258802Seric 	register char *p;
203358802Seric 	register char *q;
203458802Seric 	register char c;
203567824Seric 	int anglecnt = 0;
203667824Seric 	int cmntcnt = 0;
203767824Seric 	int quotecnt = 0;
203867824Seric 	int spacecnt = 0;
203967824Seric 	bool quotemode = FALSE;
204067824Seric 	bool bslashmode = FALSE;
204167824Seric 	char spacesub = map->map_coldelim;
204258802Seric 
204360089Seric 	for (p = q = name; (c = *p++) != '\0'; )
204458802Seric 	{
204558802Seric 		if (bslashmode)
204658802Seric 		{
204758802Seric 			bslashmode = FALSE;
204858802Seric 			*q++ = c;
204958802Seric 			continue;
205058802Seric 		}
205158802Seric 
205267824Seric 		if (c == ' ' && spacesub != '\0')
205367824Seric 			c = spacesub;
205467764Seric 
205558802Seric 		switch (c)
205658802Seric 		{
205758802Seric 		  case '\\':
205858802Seric 			bslashmode = TRUE;
205958802Seric 			break;
206058802Seric 
206158802Seric 		  case '(':
206258802Seric 			cmntcnt++;
206358802Seric 			break;
206458802Seric 
206558802Seric 		  case ')':
206658802Seric 			if (cmntcnt-- <= 0)
206758802Seric 				return NULL;
206858802Seric 			break;
206959089Seric 
207059089Seric 		  case ' ':
207159089Seric 			spacecnt++;
207259089Seric 			break;
207358802Seric 		}
207458802Seric 
207558802Seric 		if (cmntcnt > 0)
207658802Seric 		{
207758802Seric 			*q++ = c;
207858802Seric 			continue;
207958802Seric 		}
208058802Seric 
208158802Seric 		switch (c)
208258802Seric 		{
208358802Seric 		  case '"':
208458802Seric 			quotemode = !quotemode;
208558802Seric 			quotecnt++;
208658802Seric 			continue;
208758802Seric 
208858802Seric 		  case '<':
208958802Seric 			anglecnt++;
209058802Seric 			break;
209158802Seric 
209258802Seric 		  case '>':
209358802Seric 			if (anglecnt-- <= 0)
209458802Seric 				return NULL;
209558802Seric 			break;
209658802Seric 		}
209758802Seric 		*q++ = c;
209858802Seric 	}
209958802Seric 
210058802Seric 	if (anglecnt != 0 || cmntcnt != 0 || bslashmode ||
210159089Seric 	    quotemode || quotecnt <= 0 || spacecnt != 0)
210258802Seric 		return NULL;
210358802Seric 	*q++ = '\0';
210460089Seric 	return name;
210558802Seric }
2106