13312Seric # include "sendmail.h"
2297Seric 
3*4424Seric static char	SccsId[] = "@(#)parseaddr.c	3.26	09/22/81";
4407Seric 
5297Seric /*
6297Seric **  PARSE -- Parse an address
7297Seric **
8297Seric **	Parses an address and breaks it up into three parts: a
9297Seric **	net to transmit the message on, the host to transmit it
10297Seric **	to, and a user on that host.  These are loaded into an
112973Seric **	ADDRESS header with the values squirreled away if necessary.
12297Seric **	The "user" part may not be a real user; the process may
13297Seric **	just reoccur on that machine.  For example, on a machine
14297Seric **	with an arpanet connection, the address
15297Seric **		csvax.bill@berkeley
16297Seric **	will break up to a "user" of 'csvax.bill' and a host
17297Seric **	of 'berkeley' -- to be transmitted over the arpanet.
18297Seric **
19297Seric **	Parameters:
20297Seric **		addr -- the address to parse.
21297Seric **		a -- a pointer to the address descriptor buffer.
22297Seric **			If NULL, a header will be created.
23297Seric **		copyf -- determines what shall be copied:
24297Seric **			-1 -- don't copy anything.  The printname
25297Seric **				(q_paddr) is just addr, and the
26297Seric **				user & host are allocated internally
27297Seric **				to parse.
28297Seric **			0 -- copy out the parsed user & host, but
29297Seric **				don't copy the printname.
30297Seric **			+1 -- copy everything.
31297Seric **
32297Seric **	Returns:
33297Seric **		A pointer to the address descriptor header (`a' if
34297Seric **			`a' is non-NULL).
35297Seric **		NULL on error.
36297Seric **
37297Seric **	Side Effects:
38297Seric **		none
39297Seric */
40297Seric 
413380Seric # define DELIMCHARS	"$()<>,;\\\"\r\n"	/* word delimiters */
422091Seric 
432973Seric ADDRESS *
44297Seric parse(addr, a, copyf)
45297Seric 	char *addr;
462973Seric 	register ADDRESS *a;
47297Seric 	int copyf;
48297Seric {
493149Seric 	register char **pvp;
503149Seric 	register struct mailer *m;
513149Seric 	extern char **prescan();
523149Seric 	extern ADDRESS *buildaddr();
53297Seric 
54297Seric 	/*
55297Seric 	**  Initialize and prescan address.
56297Seric 	*/
57297Seric 
58297Seric 	To = addr;
593188Seric # ifdef DEBUG
603188Seric 	if (Debug)
613188Seric 		printf("\n--parse(%s)\n", addr);
623188Seric # endif DEBUG
633188Seric 
643149Seric 	pvp = prescan(addr, '\0');
653149Seric 	if (pvp == NULL)
66297Seric 		return (NULL);
67297Seric 
68297Seric 	/*
693149Seric 	**  Apply rewriting rules.
70297Seric 	*/
71297Seric 
724070Seric 	rewrite(pvp, 0);
73297Seric 
743149Seric 	/*
753149Seric 	**  See if we resolved to a real mailer.
763149Seric 	*/
77297Seric 
783149Seric 	if (pvp[0][0] != CANONNET)
793149Seric 	{
803149Seric 		setstat(EX_USAGE);
813149Seric 		usrerr("cannot resolve name");
823149Seric 		return (NULL);
83297Seric 	}
84297Seric 
85297Seric 	/*
863149Seric 	**  Build canonical address from pvp.
87297Seric 	*/
88297Seric 
893149Seric 	a = buildaddr(pvp, a);
904279Seric 	if (a == NULL)
914279Seric 		return (NULL);
923149Seric 	m = Mailer[a->q_mailer];
93297Seric 
94297Seric 	/*
953149Seric 	**  Make local copies of the host & user and then
963149Seric 	**  transport them out.
97297Seric 	*/
98297Seric 
99297Seric 	if (copyf > 0)
1002973Seric 		a->q_paddr = newstr(addr);
101297Seric 	else
102297Seric 		a->q_paddr = addr;
103297Seric 
1043149Seric 	if (copyf >= 0)
105297Seric 	{
1063149Seric 		if (a->q_host != NULL)
1073149Seric 			a->q_host = newstr(a->q_host);
108297Seric 		else
1093149Seric 			a->q_host = "";
1103149Seric 		if (a->q_user != a->q_paddr)
1113149Seric 			a->q_user = newstr(a->q_user);
112297Seric 	}
113297Seric 
114297Seric 	/*
115297Seric 	**  Do UPPER->lower case mapping unless inhibited.
116297Seric 	*/
117297Seric 
1183149Seric 	if (!bitset(M_HST_UPPER, m->m_flags))
119297Seric 		makelower(a->q_host);
1203149Seric 	if (!bitset(M_USR_UPPER, m->m_flags))
121297Seric 		makelower(a->q_user);
122297Seric 
123297Seric 	/*
124297Seric 	**  Compute return value.
125297Seric 	*/
126297Seric 
127297Seric # ifdef DEBUG
1281583Seric 	if (Debug)
129297Seric 		printf("parse(\"%s\"): host \"%s\" user \"%s\" mailer %d\n",
1303149Seric 		    addr, a->q_host, a->q_user, a->q_mailer);
131297Seric # endif DEBUG
132297Seric 
133297Seric 	return (a);
134297Seric }
135297Seric /*
136297Seric **  PRESCAN -- Prescan name and make it canonical
137297Seric **
138297Seric **	Scans a name and turns it into canonical form.  This involves
139297Seric **	deleting blanks, comments (in parentheses), and turning the
140297Seric **	word "at" into an at-sign ("@").  The name is copied as this
141297Seric **	is done; it is legal to copy a name onto itself, since this
142297Seric **	process can only make things smaller.
143297Seric **
144297Seric **	This routine knows about quoted strings and angle brackets.
145297Seric **
146297Seric **	There are certain subtleties to this routine.  The one that
147297Seric **	comes to mind now is that backslashes on the ends of names
148297Seric **	are silently stripped off; this is intentional.  The problem
149297Seric **	is that some versions of sndmsg (like at LBL) set the kill
150297Seric **	character to something other than @ when reading addresses;
151297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
152297Seric **	berknet mailer.
153297Seric **
154297Seric **	Parameters:
155297Seric **		addr -- the name to chomp.
156297Seric **		delim -- the delimiter for the address, normally
157297Seric **			'\0' or ','; \0 is accepted in any case.
158297Seric **			are moving in place; set buflim to high core.
159297Seric **
160297Seric **	Returns:
1613149Seric **		A pointer to a vector of tokens.
162297Seric **		NULL on error.
163297Seric **
164297Seric **	Side Effects:
1653149Seric **		none.
166297Seric */
167297Seric 
1683149Seric # define OPER		1
1693149Seric # define ATOM		2
1703149Seric # define EOTOK		3
1713149Seric # define QSTRING	4
1723149Seric # define SPACE		5
1733149Seric # define DOLLAR		6
1743149Seric # define GETONE		7
175*4424Seric # define MACRO		8
1763149Seric 
1773149Seric char **
1783149Seric prescan(addr, delim)
179297Seric 	char *addr;
180297Seric 	char delim;
181297Seric {
182297Seric 	register char *p;
1833149Seric 	static char buf[MAXNAME+MAXATOM];
1843149Seric 	static char *av[MAXATOM+1];
1853149Seric 	char **avp;
186297Seric 	bool bslashmode;
187297Seric 	int cmntcnt;
188297Seric 	int brccnt;
189297Seric 	register char c;
1903149Seric 	char *tok;
191297Seric 	register char *q;
1923149Seric 	register int state;
1933149Seric 	int nstate;
1944085Seric 	extern char lower();
195297Seric 
196297Seric 	q = buf;
1973149Seric 	bslashmode = FALSE;
198297Seric 	cmntcnt = brccnt = 0;
1993149Seric 	avp = av;
2003149Seric 	state = OPER;
2013149Seric 	for (p = addr; *p != '\0' && *p != delim; )
202297Seric 	{
2033149Seric 		/* read a token */
2043149Seric 		tok = q;
2053149Seric 		while ((c = *p++) != '\0' && c != delim)
206297Seric 		{
2073149Seric 			/* chew up special characters */
2084100Seric 			c &= ~0200;
2093149Seric 			*q = '\0';
2103149Seric 			if (bslashmode)
2113149Seric 			{
2123149Seric 				c |= 0200;
2133149Seric 				bslashmode = FALSE;
2143149Seric 			}
2153149Seric 			else if (c == '\\')
2163149Seric 			{
2173149Seric 				bslashmode = TRUE;
2183149Seric 				continue;
2193149Seric 			}
2204100Seric 			else if (c == '"')
2214100Seric 			{
2224100Seric 				if (state == QSTRING)
2234100Seric 					state = OPER;
2244100Seric 				else
2254100Seric 					state = QSTRING;
2264100Seric 				break;
2274100Seric 			}
2283149Seric 
229*4424Seric 			if (c == '$' && delim == '\t')
230*4424Seric 				nstate = DOLLAR;
231*4424Seric 			else
232*4424Seric 				nstate = toktype(c);
2333149Seric 			switch (state)
2343149Seric 			{
2353149Seric 			  case QSTRING:		/* in quoted string */
2363149Seric 				break;
2373149Seric 
2383149Seric 			  case ATOM:		/* regular atom */
2394228Seric 				if (nstate != ATOM)
2403149Seric 				{
2413149Seric 					state = EOTOK;
2423149Seric 					p--;
2433149Seric 				}
2443149Seric 				break;
2453149Seric 
2463149Seric 			  case GETONE:		/* grab one character */
2473149Seric 				state = OPER;
2483149Seric 				break;
2493149Seric 
2503149Seric 			  case EOTOK:		/* after atom or q-string */
2513149Seric 				state = nstate;
2523149Seric 				if (state == SPACE)
2533149Seric 					continue;
2543149Seric 				break;
2553149Seric 
2563149Seric 			  case SPACE:		/* linear white space */
2573149Seric 				state = nstate;
2584228Seric 				break;
2593149Seric 
2603149Seric 			  case OPER:		/* operator */
2613149Seric 				if (nstate == SPACE)
2623149Seric 					continue;
2633149Seric 				state = nstate;
2643149Seric 				break;
2653149Seric 
2663149Seric 			  case DOLLAR:		/* $- etc. */
2673149Seric 				state = OPER;
2683149Seric 				switch (c)
2693149Seric 				{
2703149Seric 				  case '$':		/* literal $ */
2713149Seric 					break;
2723149Seric 
2733149Seric 				  case '+':		/* match anything */
2743149Seric 					c = MATCHANY;
2753149Seric 					state = GETONE;
2763149Seric 					break;
2773149Seric 
2783149Seric 				  case '-':		/* match one token */
2793149Seric 					c = MATCHONE;
2803149Seric 					state = GETONE;
2813149Seric 					break;
2823149Seric 
2834060Seric 				  case '=':		/* match one token of class */
2844060Seric 					c = MATCHCLASS;
2854060Seric 					state = GETONE;
2864060Seric 					break;
2874060Seric 
2883149Seric 				  case '#':		/* canonical net name */
2893149Seric 					c = CANONNET;
2903149Seric 					break;
2913149Seric 
2923149Seric 				  case '@':		/* canonical host name */
2933149Seric 					c = CANONHOST;
2943149Seric 					break;
2953149Seric 
2963149Seric 				  case ':':		/* canonical user name */
2973149Seric 					c = CANONUSER;
2983149Seric 					break;
2993149Seric 
3003149Seric 				  default:
301*4424Seric 					state = MACRO;
3023149Seric 					break;
3033149Seric 				}
3043149Seric 				break;
3053149Seric 
3063149Seric 			  default:
3073149Seric 				syserr("prescan: unknown state %d", state);
3083149Seric 			}
3093149Seric 
3104228Seric 			if (state == EOTOK || state == SPACE)
3113149Seric 				break;
312*4424Seric 			if (state == DOLLAR)
3133149Seric 				continue;
3143149Seric 
3153149Seric 			/* squirrel it away */
3163149Seric 			if (q >= &buf[sizeof buf - 5])
3173149Seric 			{
3183149Seric 				usrerr("Address too long");
3193149Seric 				return (NULL);
3203149Seric 			}
321*4424Seric 			if (state == MACRO)
322*4424Seric 			{
323*4424Seric 				char mbuf[3];
324*4424Seric 
325*4424Seric 				mbuf[0] = '$';
326*4424Seric 				mbuf[1] = c;
327*4424Seric 				mbuf[2] = '\0';
328*4424Seric 				(void) expand(mbuf, q, &buf[sizeof buf - 5]);
329*4424Seric 				q += strlen(q);
330*4424Seric 				state = EOTOK;
331*4424Seric 				break;
332*4424Seric 			}
3333149Seric 			*q++ = c;
3343149Seric 
3353149Seric 			/* decide whether this represents end of token */
3363149Seric 			if (state == OPER)
3373149Seric 				break;
338297Seric 		}
3393149Seric 		if (c == '\0' || c == delim)
3403149Seric 			p--;
3413149Seric 
3423149Seric 		/* new token */
3433149Seric 		if (tok == q)
344297Seric 			continue;
3453149Seric 		*q++ = '\0';
3463149Seric 
3473149Seric 		c = tok[0];
3483149Seric 		if (c == '(')
3491378Seric 		{
350297Seric 			cmntcnt++;
3511378Seric 			continue;
3521378Seric 		}
353297Seric 		else if (c == ')')
354297Seric 		{
355297Seric 			if (cmntcnt <= 0)
356297Seric 			{
357297Seric 				usrerr("Unbalanced ')'");
358297Seric 				return (NULL);
359297Seric 			}
360297Seric 			else
361297Seric 			{
362297Seric 				cmntcnt--;
363297Seric 				continue;
364297Seric 			}
365297Seric 		}
3663149Seric 		else if (cmntcnt > 0)
3672091Seric 			continue;
3683149Seric 
3694385Seric 		if (avp >= &av[MAXATOM])
3704385Seric 		{
3714385Seric 			syserr("prescan: too many tokens");
3724385Seric 			return (NULL);
3734385Seric 		}
3743149Seric 		*avp++ = tok;
3753149Seric 
3763149Seric 		/* we prefer <> specs */
3773149Seric 		if (c == '<')
378297Seric 		{
3792092Seric 			if (brccnt < 0)
3802092Seric 			{
3812092Seric 				usrerr("multiple < spec");
3822092Seric 				return (NULL);
3832092Seric 			}
384297Seric 			brccnt++;
385297Seric 			if (brccnt == 1)
386297Seric 			{
387297Seric 				/* we prefer using machine readable name */
388297Seric 				q = buf;
389297Seric 				*q = '\0';
3903149Seric 				avp = av;
391297Seric 				continue;
392297Seric 			}
393297Seric 		}
394297Seric 		else if (c == '>')
395297Seric 		{
396297Seric 			if (brccnt <= 0)
397297Seric 			{
398297Seric 				usrerr("Unbalanced `>'");
399297Seric 				return (NULL);
400297Seric 			}
401297Seric 			else
402297Seric 				brccnt--;
403297Seric 			if (brccnt <= 0)
4042092Seric 			{
4052092Seric 				brccnt = -1;
406297Seric 				continue;
4072092Seric 			}
408297Seric 		}
4093149Seric 	}
4103149Seric 	*avp = NULL;
4113149Seric 	if (cmntcnt > 0)
4123149Seric 		usrerr("Unbalanced '('");
4133149Seric 	else if (brccnt > 0)
4143149Seric 		usrerr("Unbalanced '<'");
4153149Seric 	else if (state == QSTRING)
4163149Seric 		usrerr("Unbalanced '\"'");
4173149Seric 	else if (av[0] != NULL)
4183149Seric 		return (av);
4193149Seric 	return (NULL);
4203149Seric }
4213149Seric /*
4223149Seric **  TOKTYPE -- return token type
4233149Seric **
4243149Seric **	Parameters:
4253149Seric **		c -- the character in question.
4263149Seric **
4273149Seric **	Returns:
4283149Seric **		Its type.
4293149Seric **
4303149Seric **	Side Effects:
4313149Seric **		none.
4323149Seric */
433297Seric 
4343149Seric toktype(c)
4353149Seric 	register char c;
4363149Seric {
4373380Seric 	static char buf[50];
4383382Seric 	static bool firstime = TRUE;
4393380Seric 
4403382Seric 	if (firstime)
4413380Seric 	{
4423382Seric 		firstime = FALSE;
4434085Seric 		(void) expand("$o", buf, &buf[sizeof buf - 1]);
4443380Seric 		strcat(buf, DELIMCHARS);
4453380Seric 	}
4464100Seric 	if (!isascii(c))
4474100Seric 		return (ATOM);
4483149Seric 	if (isspace(c))
4493149Seric 		return (SPACE);
4503380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
4513149Seric 		return (OPER);
4523149Seric 	return (ATOM);
4533149Seric }
4543149Seric /*
4553149Seric **  REWRITE -- apply rewrite rules to token vector.
4563149Seric **
4573149Seric **	Parameters:
4583149Seric **		pvp -- pointer to token vector.
4593149Seric **
4603149Seric **	Returns:
4613149Seric **		none.
4623149Seric **
4633149Seric **	Side Effects:
4643149Seric **		pvp is modified.
4653149Seric */
4662091Seric 
4673149Seric struct match
4683149Seric {
4693149Seric 	char	**firsttok;	/* first token matched */
4703149Seric 	char	**lasttok;	/* last token matched */
4713149Seric 	char	name;		/* name of parameter */
4723149Seric };
4733149Seric 
4743149Seric # define MAXMATCH	8	/* max params per rewrite */
4753149Seric 
4763149Seric 
4774070Seric rewrite(pvp, ruleset)
4783149Seric 	char **pvp;
4794070Seric 	int ruleset;
4803149Seric {
4813149Seric 	register char *ap;		/* address pointer */
4823149Seric 	register char *rp;		/* rewrite pointer */
4833149Seric 	register char **avp;		/* address vector pointer */
4843149Seric 	register char **rvp;		/* rewrite vector pointer */
4853149Seric 	struct rewrite *rwr;
4863149Seric 	struct match mlist[MAXMATCH];
4873149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
4884060Seric 	extern bool sameword();
4893149Seric 
4904100Seric # ifdef DEBUG
4914228Seric 	if (Debug > 9)
4923149Seric 	{
4933149Seric 		printf("rewrite: original pvp:\n");
4943149Seric 		printav(pvp);
4953149Seric 	}
4964100Seric # endif DEBUG
4973149Seric 
4983149Seric 	/*
4993149Seric 	**  Run through the list of rewrite rules, applying
5003149Seric 	**	any that match.
5013149Seric 	*/
5023149Seric 
5034070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
5043149Seric 	{
5054100Seric # ifdef DEBUG
5064100Seric 		if (Debug > 10)
507297Seric 		{
5083149Seric 			printf("-----trying rule:\n");
5093149Seric 			printav(rwr->r_lhs);
5103149Seric 		}
5114100Seric # endif DEBUG
5123149Seric 
5133149Seric 		/* try to match on this rule */
5143149Seric 		clrmatch(mlist);
5153149Seric 		for (rvp = rwr->r_lhs, avp = pvp; *avp != NULL; )
5163149Seric 		{
5173149Seric 			ap = *avp;
5183149Seric 			rp = *rvp;
5193149Seric 
5203149Seric 			if (rp == NULL)
521297Seric 			{
5223149Seric 				/* end-of-pattern before end-of-address */
5233149Seric 				goto fail;
524297Seric 			}
5253149Seric 
5263149Seric 			switch (*rp)
5273149Seric 			{
5284060Seric 				register STAB *s;
5294060Seric 				register int class;
5304060Seric 
5313149Seric 			  case MATCHONE:
5323149Seric 				/* match exactly one token */
5333149Seric 				setmatch(mlist, rp[1], avp, avp);
5343149Seric 				break;
5353149Seric 
5363149Seric 			  case MATCHANY:
5373149Seric 				/* match any number of tokens */
5384085Seric 				setmatch(mlist, rp[1], (char **) NULL, avp);
5393149Seric 				break;
5403149Seric 
5414060Seric 			  case MATCHCLASS:
5424060Seric 				/* match any token in a class */
5434060Seric 				class = rp[1];
5444060Seric 				if (!isalpha(class))
5454060Seric 					goto fail;
5464060Seric 				if (isupper(class))
5474060Seric 					class -= 'A';
5484060Seric 				else
5494060Seric 					class -= 'a';
5504100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
5514060Seric 				if (s == NULL || (s->s_class & (1 << class)) == 0)
5524060Seric 					goto fail;
5534060Seric 				break;
5544060Seric 
5553149Seric 			  default:
5563149Seric 				/* must have exact match */
5574060Seric 				if (!sameword(rp, ap))
5584060Seric 					goto fail;
5593149Seric 				break;
5603149Seric 			}
5613149Seric 
5623149Seric 			/* successful match on this token */
5633149Seric 			avp++;
5643149Seric 			rvp++;
5653149Seric 			continue;
5663149Seric 
5673149Seric 		  fail:
5683149Seric 			/* match failed -- back up */
5693149Seric 			while (--rvp >= rwr->r_lhs)
5703149Seric 			{
5713149Seric 				rp = *rvp;
5723149Seric 				if (*rp == MATCHANY)
5733149Seric 					break;
5743149Seric 
5753149Seric 				/* can't extend match: back up everything */
5763149Seric 				avp--;
5773149Seric 
5783149Seric 				if (*rp == MATCHONE)
5793149Seric 				{
5803149Seric 					/* undo binding */
5814085Seric 					setmatch(mlist, rp[1], (char **) NULL, (char **) NULL);
5823149Seric 				}
5833149Seric 			}
5843149Seric 
5853149Seric 			if (rvp < rwr->r_lhs)
5863149Seric 			{
5873149Seric 				/* total failure to match */
5883149Seric 				break;
5893149Seric 			}
590297Seric 		}
5913149Seric 
5923149Seric 		/*
5933149Seric 		**  See if we successfully matched
5943149Seric 		*/
5953149Seric 
5963149Seric 		if (rvp >= rwr->r_lhs && *rvp == NULL)
5973149Seric 		{
5984100Seric # ifdef DEBUG
5994100Seric 			if (Debug > 10)
6003149Seric 			{
6013149Seric 				printf("-----rule matches:\n");
6023149Seric 				printav(rwr->r_rhs);
6033149Seric 			}
6044100Seric # endif DEBUG
6053149Seric 
6063149Seric 			/* substitute */
6073149Seric 			for (rvp = rwr->r_rhs, avp = npvp; *rvp != NULL; rvp++)
6083149Seric 			{
6093149Seric 				rp = *rvp;
6103149Seric 				if (*rp == MATCHANY)
6113149Seric 				{
6123149Seric 					register struct match *m;
6133149Seric 					register char **pp;
6143149Seric 					extern struct match *findmatch();
6153149Seric 
6163149Seric 					m = findmatch(mlist, rp[1]);
6173149Seric 					if (m != NULL)
6183149Seric 					{
6193149Seric 						pp = m->firsttok;
6203149Seric 						do
6213149Seric 						{
6224385Seric 							if (avp >= &npvp[MAXATOM])
6234385Seric 							{
6244385Seric 								syserr("rewrite: expansion too long");
6254385Seric 								return;
6264385Seric 							}
6273149Seric 							*avp++ = *pp;
6283149Seric 						} while (pp++ != m->lasttok);
6293149Seric 					}
6303149Seric 				}
6313149Seric 				else
6324385Seric 				{
6334385Seric 					if (avp >= &npvp[MAXATOM])
6344385Seric 					{
6354385Seric 						syserr("rewrite: expansion too long");
6364385Seric 						return;
6374385Seric 					}
6383149Seric 					*avp++ = rp;
6394385Seric 				}
6403149Seric 			}
6413149Seric 			*avp++ = NULL;
6424085Seric 			bmove((char *) npvp, (char *) pvp, (avp - npvp) * sizeof *avp);
6433149Seric # ifdef DEBUG
6444228Seric 			if (Debug > 3)
6453149Seric 			{
6463188Seric 				char **vp;
6473188Seric 
6483188Seric 				printf("rewritten as `");
6493188Seric 				for (vp = pvp; *vp != NULL; vp++)
6504228Seric 				{
6514228Seric 					if (vp != pvp)
6524228Seric 						printf("_");
6533188Seric 					xputs(*vp);
6544228Seric 				}
6553188Seric 				printf("'\n");
6563149Seric 			}
6573149Seric # endif DEBUG
6583149Seric 			if (pvp[0][0] == CANONNET)
6593149Seric 				break;
6603149Seric 		}
6613149Seric 		else
6623149Seric 		{
6634100Seric # ifdef DEBUG
6644100Seric 			if (Debug > 10)
6653149Seric 				printf("----- rule fails\n");
6664100Seric # endif DEBUG
6673149Seric 			rwr = rwr->r_next;
6683149Seric 		}
669297Seric 	}
6703149Seric }
6713149Seric /*
6723149Seric **  SETMATCH -- set parameter value in match vector
6733149Seric **
6743149Seric **	Parameters:
6753149Seric **		mlist -- list of match values.
6763149Seric **		name -- the character name of this parameter.
6773149Seric **		first -- the first location of the replacement.
6783149Seric **		last -- the last location of the replacement.
6793149Seric **
6803149Seric **		If last == NULL, delete this entry.
6813149Seric **		If first == NULL, extend this entry (or add it if
6823149Seric **			it does not exist).
6833149Seric **
6843149Seric **	Returns:
6853149Seric **		nothing.
6863149Seric **
6873149Seric **	Side Effects:
6883149Seric **		munges with mlist.
6893149Seric */
6903149Seric 
6913149Seric setmatch(mlist, name, first, last)
6923149Seric 	struct match *mlist;
6933149Seric 	char name;
6943149Seric 	char **first;
6953149Seric 	char **last;
6963149Seric {
6973149Seric 	register struct match *m;
6983149Seric 	struct match *nullm = NULL;
6993149Seric 
7003149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
7013149Seric 	{
7023149Seric 		if (m->name == name)
7033149Seric 			break;
7043149Seric 		if (m->name == '\0')
7053149Seric 			nullm = m;
7063149Seric 	}
7073149Seric 
7083149Seric 	if (m >= &mlist[MAXMATCH])
7093149Seric 		m = nullm;
7103149Seric 
7113149Seric 	if (last == NULL)
7123149Seric 	{
7133149Seric 		m->name = '\0';
7143149Seric 		return;
7153149Seric 	}
7163149Seric 
7173149Seric 	if (m->name == '\0')
7183149Seric 	{
7193149Seric 		if (first == NULL)
7203149Seric 			m->firsttok = last;
7213149Seric 		else
7223149Seric 			m->firsttok = first;
7233149Seric 	}
7243149Seric 	m->name = name;
7253149Seric 	m->lasttok = last;
7263149Seric }
7273149Seric /*
7283149Seric **  FINDMATCH -- find match in mlist
7293149Seric **
7303149Seric **	Parameters:
7313149Seric **		mlist -- list to search.
7323149Seric **		name -- name to find.
7333149Seric **
7343149Seric **	Returns:
7353149Seric **		pointer to match structure.
7363149Seric **		NULL if no match.
7373149Seric **
7383149Seric **	Side Effects:
7393149Seric **		none.
7403149Seric */
7413149Seric 
7423149Seric struct match *
7433149Seric findmatch(mlist, name)
7443149Seric 	struct match *mlist;
7453149Seric 	char name;
7463149Seric {
7473149Seric 	register struct match *m;
7483149Seric 
7493149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
7503149Seric 	{
7513149Seric 		if (m->name == name)
7523149Seric 			return (m);
7533149Seric 	}
7543149Seric 
755297Seric 	return (NULL);
756297Seric }
7573149Seric /*
7583149Seric **  CLRMATCH -- clear match list
7593149Seric **
7603149Seric **	Parameters:
7613149Seric **		mlist -- list to clear.
7623149Seric **
7633149Seric **	Returns:
7643149Seric **		none.
7653149Seric **
7663149Seric **	Side Effects:
7673149Seric **		mlist is cleared.
7683149Seric */
7693149Seric 
7703149Seric clrmatch(mlist)
7713149Seric 	struct match *mlist;
7723149Seric {
7733149Seric 	register struct match *m;
7743149Seric 
7753149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
7763149Seric 		m->name = '\0';
7773149Seric }
7783149Seric /*
7793149Seric **  BUILDADDR -- build address from token vector.
7803149Seric **
7813149Seric **	Parameters:
7823149Seric **		tv -- token vector.
7833149Seric **		a -- pointer to address descriptor to fill.
7843149Seric **			If NULL, one will be allocated.
7853149Seric **
7863149Seric **	Returns:
7874279Seric **		NULL if there was an error.
7884279Seric **		'a' otherwise.
7893149Seric **
7903149Seric **	Side Effects:
7913149Seric **		fills in 'a'
7923149Seric */
7933149Seric 
7943149Seric ADDRESS *
7953149Seric buildaddr(tv, a)
7963149Seric 	register char **tv;
7973149Seric 	register ADDRESS *a;
7983149Seric {
7993149Seric 	register int i;
8003149Seric 	static char buf[MAXNAME];
8013149Seric 	struct mailer **mp;
8023149Seric 	register struct mailer *m;
8033149Seric 
8043149Seric 	if (a == NULL)
8053149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
8063188Seric 	a->q_flags = 0;
8074079Seric 	a->q_home = NULL;
8083149Seric 
8093149Seric 	/* figure out what net/mailer to use */
8103149Seric 	if (**tv != CANONNET)
8114279Seric 	{
8123149Seric 		syserr("buildaddr: no net");
8134279Seric 		return (NULL);
8144279Seric 	}
8153149Seric 	tv++;
8164279Seric 	if (strcmp(*tv, "error") == 0)
8174279Seric 	{
8184279Seric 		if (**++tv != CANONUSER)
8194279Seric 			syserr("buildaddr: error: no user");
8204279Seric 		buf[0] = '\0';
8214279Seric 		while (*++tv != NULL)
8224279Seric 		{
8234279Seric 			if (buf[0] != '\0')
8244279Seric 				strcat(buf, " ");
8254279Seric 			strcat(buf, *tv);
8264279Seric 		}
8274279Seric 		usrerr(buf);
8284279Seric 		return (NULL);
8294279Seric 	}
8303154Seric 	for (mp = Mailer, i = 0; (m = *mp++) != NULL; i++)
8313149Seric 	{
8323149Seric 		if (strcmp(m->m_name, *tv) == 0)
8333149Seric 			break;
8343149Seric 	}
8353149Seric 	if (m == NULL)
8364279Seric 	{
8373149Seric 		syserr("buildaddr: unknown net %s", *tv);
8384279Seric 		return (NULL);
8394279Seric 	}
8403149Seric 	a->q_mailer = i;
8413149Seric 
8423149Seric 	/* figure out what host (if any) */
8433149Seric 	tv++;
8444195Seric 	if (!bitset(M_LOCAL, m->m_flags))
8453149Seric 	{
8463149Seric 		if (**tv != CANONHOST)
8474279Seric 		{
8483149Seric 			syserr("buildaddr: no host");
8494279Seric 			return (NULL);
8504279Seric 		}
8513149Seric 		tv++;
8523149Seric 		a->q_host = *tv;
8533149Seric 		tv++;
8543149Seric 	}
8553149Seric 	else
8563149Seric 		a->q_host = NULL;
8573149Seric 
8583149Seric 	/* figure out the user */
8593149Seric 	if (**tv != CANONUSER)
8604279Seric 	{
8613149Seric 		syserr("buildaddr: no user");
8624279Seric 		return (NULL);
8634279Seric 	}
8644228Seric 	cataddr(++tv, buf, sizeof buf);
8653149Seric 	a->q_user = buf;
8663149Seric 
8673149Seric 	return (a);
8683149Seric }
8693188Seric /*
8704228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
8714228Seric **
8724228Seric **	Parameters:
8734228Seric **		pvp -- parameter vector to rebuild.
8744228Seric **		buf -- buffer to build the string into.
8754228Seric **		sz -- size of buf.
8764228Seric **
8774228Seric **	Returns:
8784228Seric **		none.
8794228Seric **
8804228Seric **	Side Effects:
8814228Seric **		Destroys buf.
8824228Seric */
8834228Seric 
8844228Seric cataddr(pvp, buf, sz)
8854228Seric 	char **pvp;
8864228Seric 	char *buf;
8874228Seric 	register int sz;
8884228Seric {
8894228Seric 	bool oatomtok = FALSE;
8904228Seric 	bool natomtok = FALSE;
8914228Seric 	register int i;
8924228Seric 	register char *p;
8934228Seric 
8944228Seric 	p = buf;
8954228Seric 	sz--;
8964228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
8974228Seric 	{
8984228Seric 		natomtok = (toktype(**pvp) == ATOM);
8994228Seric 		if (oatomtok && natomtok)
9004228Seric 			*p++ = SPACESUB;
9014228Seric 		(void) strcpy(p, *pvp);
9024228Seric 		oatomtok = natomtok;
9034228Seric 		p += i;
9044228Seric 		sz -= i;
9054228Seric 		pvp++;
9064228Seric 	}
9074228Seric 	*p = '\0';
9084228Seric }
9094228Seric /*
9103188Seric **  SAMEADDR -- Determine if two addresses are the same
9113188Seric **
9123188Seric **	This is not just a straight comparison -- if the mailer doesn't
9133188Seric **	care about the host we just ignore it, etc.
9143188Seric **
9153188Seric **	Parameters:
9163188Seric **		a, b -- pointers to the internal forms to compare.
9173188Seric **		wildflg -- if TRUE, 'a' may have no user specified,
9183188Seric **			in which case it is to match anything.
9193188Seric **
9203188Seric **	Returns:
9213188Seric **		TRUE -- they represent the same mailbox.
9223188Seric **		FALSE -- they don't.
9233188Seric **
9243188Seric **	Side Effects:
9253188Seric **		none.
9263188Seric */
9273188Seric 
9283188Seric bool
9293188Seric sameaddr(a, b, wildflg)
9303188Seric 	register ADDRESS *a;
9313188Seric 	register ADDRESS *b;
9323188Seric 	bool wildflg;
9333188Seric {
9343188Seric 	/* if they don't have the same mailer, forget it */
9353188Seric 	if (a->q_mailer != b->q_mailer)
9363188Seric 		return (FALSE);
9373188Seric 
9383188Seric 	/* if the user isn't the same, we can drop out */
9393188Seric 	if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0)
9403188Seric 		return (FALSE);
9413188Seric 
9423188Seric 	/* if the mailer ignores hosts, we have succeeded! */
9434195Seric 	if (bitset(M_LOCAL, Mailer[a->q_mailer]->m_flags))
9443188Seric 		return (TRUE);
9453188Seric 
9463188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
9473188Seric 	if (a->q_host == NULL || b->q_host == NULL)
9483188Seric 		return (FALSE);
9493188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
9503188Seric 		return (FALSE);
9513188Seric 
9523188Seric 	return (TRUE);
9533188Seric }
9543234Seric /*
9553234Seric **  PRINTADDR -- print address (for debugging)
9563234Seric **
9573234Seric **	Parameters:
9583234Seric **		a -- the address to print
9593234Seric **		follow -- follow the q_next chain.
9603234Seric **
9613234Seric **	Returns:
9623234Seric **		none.
9633234Seric **
9643234Seric **	Side Effects:
9653234Seric **		none.
9663234Seric */
9673234Seric 
9684317Seric # ifdef DEBUG
9694317Seric 
9703234Seric printaddr(a, follow)
9713234Seric 	register ADDRESS *a;
9723234Seric 	bool follow;
9733234Seric {
9743234Seric 	while (a != NULL)
9753234Seric 	{
9763234Seric 		printf("addr@%x: ", a);
9774085Seric 		(void) fflush(stdout);
9783234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
9793234Seric 		       a->q_mailer, Mailer[a->q_mailer]->m_name, a->q_host, a->q_user);
9803234Seric 		printf("\tnext=%x flags=%o, rmailer %d\n", a->q_next,
9813234Seric 		       a->q_flags, a->q_rmailer);
9823234Seric 
9833234Seric 		if (!follow)
9843234Seric 			return;
9853234Seric 		a = a->q_next;
9863234Seric 	}
9873234Seric }
9884317Seric 
9894317Seric # endif DEBUG
990