1297Seric # include <stdio.h>
2297Seric # include <ctype.h>
3*3312Seric # include "sendmail.h"
4297Seric 
5*3312Seric static char	SccsId[] = "@(#)parseaddr.c	3.10	03/20/81";
6407Seric 
7297Seric /*
8297Seric **  PARSE -- Parse an address
9297Seric **
10297Seric **	Parses an address and breaks it up into three parts: a
11297Seric **	net to transmit the message on, the host to transmit it
12297Seric **	to, and a user on that host.  These are loaded into an
132973Seric **	ADDRESS header with the values squirreled away if necessary.
14297Seric **	The "user" part may not be a real user; the process may
15297Seric **	just reoccur on that machine.  For example, on a machine
16297Seric **	with an arpanet connection, the address
17297Seric **		csvax.bill@berkeley
18297Seric **	will break up to a "user" of 'csvax.bill' and a host
19297Seric **	of 'berkeley' -- to be transmitted over the arpanet.
20297Seric **
21297Seric **	Parameters:
22297Seric **		addr -- the address to parse.
23297Seric **		a -- a pointer to the address descriptor buffer.
24297Seric **			If NULL, a header will be created.
25297Seric **		copyf -- determines what shall be copied:
26297Seric **			-1 -- don't copy anything.  The printname
27297Seric **				(q_paddr) is just addr, and the
28297Seric **				user & host are allocated internally
29297Seric **				to parse.
30297Seric **			0 -- copy out the parsed user & host, but
31297Seric **				don't copy the printname.
32297Seric **			+1 -- copy everything.
33297Seric **
34297Seric **	Returns:
35297Seric **		A pointer to the address descriptor header (`a' if
36297Seric **			`a' is non-NULL).
37297Seric **		NULL on error.
38297Seric **
39297Seric **	Side Effects:
40297Seric **		none
41297Seric **
42297Seric **	Called By:
43297Seric **		main
44297Seric **		sendto
45297Seric **		alias
46297Seric **		savemail
47297Seric */
48297Seric 
493149Seric # define DELIMCHARS	"$()<>@!.,;:\\\" \t\r\n"	/* word delimiters */
502091Seric # define SPACESUB	('.'|0200)		/* substitution for <lwsp> */
512091Seric 
522973Seric ADDRESS *
53297Seric parse(addr, a, copyf)
54297Seric 	char *addr;
552973Seric 	register ADDRESS *a;
56297Seric 	int copyf;
57297Seric {
583149Seric 	register char **pvp;
593149Seric 	register struct mailer *m;
603149Seric 	extern char **prescan();
612973Seric 	extern char *newstr();
622990Seric 	extern char *strcpy();
633149Seric 	extern ADDRESS *buildaddr();
64297Seric 
65297Seric 	/*
66297Seric 	**  Initialize and prescan address.
67297Seric 	*/
68297Seric 
69297Seric 	To = addr;
703188Seric # ifdef DEBUG
713188Seric 	if (Debug)
723188Seric 		printf("\n--parse(%s)\n", addr);
733188Seric # endif DEBUG
743188Seric 
753149Seric 	pvp = prescan(addr, '\0');
763149Seric 	if (pvp == NULL)
77297Seric 		return (NULL);
78297Seric 
79297Seric 	/*
803149Seric 	**  Apply rewriting rules.
81297Seric 	*/
82297Seric 
833149Seric 	rewrite(pvp);
84297Seric 
853149Seric 	/*
863149Seric 	**  See if we resolved to a real mailer.
873149Seric 	*/
88297Seric 
893149Seric 	if (pvp[0][0] != CANONNET)
903149Seric 	{
913149Seric 		setstat(EX_USAGE);
923149Seric 		usrerr("cannot resolve name");
933149Seric 		return (NULL);
94297Seric 	}
95297Seric 
96297Seric 	/*
973149Seric 	**  Build canonical address from pvp.
98297Seric 	*/
99297Seric 
1003149Seric 	a = buildaddr(pvp, a);
1013149Seric 	m = Mailer[a->q_mailer];
102297Seric 
103297Seric 	/*
1043149Seric 	**  Make local copies of the host & user and then
1053149Seric 	**  transport them out.
106297Seric 	*/
107297Seric 
108297Seric 	if (copyf > 0)
1092973Seric 		a->q_paddr = newstr(addr);
110297Seric 	else
111297Seric 		a->q_paddr = addr;
112297Seric 
1133149Seric 	if (copyf >= 0)
114297Seric 	{
1153149Seric 		if (a->q_host != NULL)
1163149Seric 			a->q_host = newstr(a->q_host);
117297Seric 		else
1183149Seric 			a->q_host = "";
1193149Seric 		if (a->q_user != a->q_paddr)
1203149Seric 			a->q_user = newstr(a->q_user);
121297Seric 	}
122297Seric 
123297Seric 	/*
124297Seric 	**  Do UPPER->lower case mapping unless inhibited.
125297Seric 	*/
126297Seric 
1273149Seric 	if (!bitset(M_HST_UPPER, m->m_flags))
128297Seric 		makelower(a->q_host);
1293149Seric 	if (!bitset(M_USR_UPPER, m->m_flags))
130297Seric 		makelower(a->q_user);
131297Seric 
132297Seric 	/*
133297Seric 	**  Compute return value.
134297Seric 	*/
135297Seric 
136297Seric # ifdef DEBUG
1371583Seric 	if (Debug)
138297Seric 		printf("parse(\"%s\"): host \"%s\" user \"%s\" mailer %d\n",
1393149Seric 		    addr, a->q_host, a->q_user, a->q_mailer);
140297Seric # endif DEBUG
141297Seric 
142297Seric 	return (a);
143297Seric }
144297Seric /*
145297Seric **  PRESCAN -- Prescan name and make it canonical
146297Seric **
147297Seric **	Scans a name and turns it into canonical form.  This involves
148297Seric **	deleting blanks, comments (in parentheses), and turning the
149297Seric **	word "at" into an at-sign ("@").  The name is copied as this
150297Seric **	is done; it is legal to copy a name onto itself, since this
151297Seric **	process can only make things smaller.
152297Seric **
153297Seric **	This routine knows about quoted strings and angle brackets.
154297Seric **
155297Seric **	There are certain subtleties to this routine.  The one that
156297Seric **	comes to mind now is that backslashes on the ends of names
157297Seric **	are silently stripped off; this is intentional.  The problem
158297Seric **	is that some versions of sndmsg (like at LBL) set the kill
159297Seric **	character to something other than @ when reading addresses;
160297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
161297Seric **	berknet mailer.
162297Seric **
163297Seric **	Parameters:
164297Seric **		addr -- the name to chomp.
165297Seric **		delim -- the delimiter for the address, normally
166297Seric **			'\0' or ','; \0 is accepted in any case.
167297Seric **			are moving in place; set buflim to high core.
168297Seric **
169297Seric **	Returns:
1703149Seric **		A pointer to a vector of tokens.
171297Seric **		NULL on error.
172297Seric **
173297Seric **	Side Effects:
1743149Seric **		none.
175297Seric */
176297Seric 
1773149Seric # define OPER		1
1783149Seric # define ATOM		2
1793149Seric # define EOTOK		3
1803149Seric # define QSTRING	4
1813149Seric # define SPACE		5
1823149Seric # define DOLLAR		6
1833149Seric # define GETONE		7
1843149Seric 
1853149Seric char **
1863149Seric prescan(addr, delim)
187297Seric 	char *addr;
188297Seric 	char delim;
189297Seric {
190297Seric 	register char *p;
1913149Seric 	static char buf[MAXNAME+MAXATOM];
1923149Seric 	static char *av[MAXATOM+1];
1933149Seric 	char **avp;
194297Seric 	bool space;
195297Seric 	bool bslashmode;
196297Seric 	int cmntcnt;
197297Seric 	int brccnt;
198297Seric 	register char c;
1993149Seric 	char *tok;
200297Seric 	register char *q;
2012973Seric 	extern char *index();
2023149Seric 	register int state;
2033149Seric 	int nstate;
204297Seric 
2052091Seric 	space = FALSE;
206297Seric 	q = buf;
2073149Seric 	bslashmode = FALSE;
208297Seric 	cmntcnt = brccnt = 0;
2093149Seric 	avp = av;
2103149Seric 	state = OPER;
2113149Seric 	for (p = addr; *p != '\0' && *p != delim; )
212297Seric 	{
2133149Seric 		/* read a token */
2143149Seric 		tok = q;
2153149Seric 		while ((c = *p++) != '\0' && c != delim)
216297Seric 		{
2173149Seric 			/* chew up special characters */
2183149Seric 			*q = '\0';
2193149Seric 			if (bslashmode)
2203149Seric 			{
2213149Seric 				c |= 0200;
2223149Seric 				bslashmode = FALSE;
2233149Seric 			}
2243149Seric 			else if (c == '\\')
2253149Seric 			{
2263149Seric 				bslashmode = TRUE;
2273149Seric 				continue;
2283149Seric 			}
2293149Seric 
2303149Seric 			nstate = toktype(c);
2313149Seric 			switch (state)
2323149Seric 			{
2333149Seric 			  case QSTRING:		/* in quoted string */
2343149Seric 				if (c == '"')
2353149Seric 					state = OPER;
2363149Seric 				break;
2373149Seric 
2383149Seric 			  case ATOM:		/* regular atom */
2393149Seric 				state = nstate;
2403149Seric 				if (state != ATOM)
2413149Seric 				{
2423149Seric 					state = EOTOK;
2433149Seric 					p--;
2443149Seric 				}
2453149Seric 				break;
2463149Seric 
2473149Seric 			  case GETONE:		/* grab one character */
2483149Seric 				state = OPER;
2493149Seric 				break;
2503149Seric 
2513149Seric 			  case EOTOK:		/* after atom or q-string */
2523149Seric 				state = nstate;
2533149Seric 				if (state == SPACE)
2543149Seric 					continue;
2553149Seric 				break;
2563149Seric 
2573149Seric 			  case SPACE:		/* linear white space */
2583149Seric 				state = nstate;
2593149Seric 				space = TRUE;
2603149Seric 				continue;
2613149Seric 
2623149Seric 			  case OPER:		/* operator */
2633149Seric 				if (nstate == SPACE)
2643149Seric 					continue;
2653149Seric 				state = nstate;
2663149Seric 				break;
2673149Seric 
2683149Seric 			  case DOLLAR:		/* $- etc. */
2693149Seric 				state = OPER;
2703149Seric 				switch (c)
2713149Seric 				{
2723149Seric 				  case '$':		/* literal $ */
2733149Seric 					break;
2743149Seric 
2753149Seric 				  case '+':		/* match anything */
2763149Seric 					c = MATCHANY;
2773149Seric 					state = GETONE;
2783149Seric 					break;
2793149Seric 
2803149Seric 				  case '-':		/* match one token */
2813149Seric 					c = MATCHONE;
2823149Seric 					state = GETONE;
2833149Seric 					break;
2843149Seric 
2853149Seric 				  case '#':		/* canonical net name */
2863149Seric 					c = CANONNET;
2873149Seric 					break;
2883149Seric 
2893149Seric 				  case '@':		/* canonical host name */
2903149Seric 					c = CANONHOST;
2913149Seric 					break;
2923149Seric 
2933149Seric 				  case ':':		/* canonical user name */
2943149Seric 					c = CANONUSER;
2953149Seric 					break;
2963149Seric 
2973149Seric 				  default:
2983149Seric 					c = '$';
2993149Seric 					state = OPER;
3003149Seric 					p--;
3013149Seric 					break;
3023149Seric 				}
3033149Seric 				break;
3043149Seric 
3053149Seric 			  default:
3063149Seric 				syserr("prescan: unknown state %d", state);
3073149Seric 			}
3083149Seric 
3093149Seric 			if (state == OPER)
3103149Seric 				space = FALSE;
3113149Seric 			else if (state == EOTOK)
3123149Seric 				break;
3133149Seric 			if (c == '$' && delim == '\t')
3143149Seric 			{
3153149Seric 				state = DOLLAR;
3163149Seric 				continue;
3173149Seric 			}
3183149Seric 
3193149Seric 			/* squirrel it away */
3203149Seric 			if (q >= &buf[sizeof buf - 5])
3213149Seric 			{
3223149Seric 				usrerr("Address too long");
3233149Seric 				return (NULL);
3243149Seric 			}
3253149Seric 			if (space)
3263149Seric 				*q++ = SPACESUB;
3273149Seric 			*q++ = c;
3283149Seric 
3293149Seric 			/* decide whether this represents end of token */
3303149Seric 			if (state == OPER)
3313149Seric 				break;
332297Seric 		}
3333149Seric 		if (c == '\0' || c == delim)
3343149Seric 			p--;
3353149Seric 
3363149Seric 		/* new token */
3373149Seric 		if (tok == q)
338297Seric 			continue;
3393149Seric 		*q++ = '\0';
3403149Seric 
3413149Seric 		c = tok[0];
3423149Seric 		if (c == '(')
3431378Seric 		{
344297Seric 			cmntcnt++;
3451378Seric 			continue;
3461378Seric 		}
347297Seric 		else if (c == ')')
348297Seric 		{
349297Seric 			if (cmntcnt <= 0)
350297Seric 			{
351297Seric 				usrerr("Unbalanced ')'");
352297Seric 				return (NULL);
353297Seric 			}
354297Seric 			else
355297Seric 			{
356297Seric 				cmntcnt--;
357297Seric 				continue;
358297Seric 			}
359297Seric 		}
3603149Seric 		else if (cmntcnt > 0)
3612091Seric 			continue;
3623149Seric 
3633149Seric 		*avp++ = tok;
3643149Seric 
3653149Seric 		/* we prefer <> specs */
3663149Seric 		if (c == '<')
367297Seric 		{
3682092Seric 			if (brccnt < 0)
3692092Seric 			{
3702092Seric 				usrerr("multiple < spec");
3712092Seric 				return (NULL);
3722092Seric 			}
373297Seric 			brccnt++;
3742091Seric 			space = FALSE;
375297Seric 			if (brccnt == 1)
376297Seric 			{
377297Seric 				/* we prefer using machine readable name */
378297Seric 				q = buf;
379297Seric 				*q = '\0';
3803149Seric 				avp = av;
381297Seric 				continue;
382297Seric 			}
383297Seric 		}
384297Seric 		else if (c == '>')
385297Seric 		{
386297Seric 			if (brccnt <= 0)
387297Seric 			{
388297Seric 				usrerr("Unbalanced `>'");
389297Seric 				return (NULL);
390297Seric 			}
391297Seric 			else
392297Seric 				brccnt--;
393297Seric 			if (brccnt <= 0)
3942092Seric 			{
3952092Seric 				brccnt = -1;
396297Seric 				continue;
3972092Seric 			}
398297Seric 		}
399297Seric 
400297Seric 		/*
401297Seric 		**  Turn "at" into "@",
4021378Seric 		**	but only if "at" is a word.
403297Seric 		*/
404297Seric 
4053149Seric 		if (lower(tok[0]) == 'a' && lower(tok[1]) == 't' && tok[2] == '\0')
406297Seric 		{
4073149Seric 			tok[0] = '@';
4083149Seric 			tok[1] = '\0';
409297Seric 		}
4103149Seric 	}
4113149Seric 	*avp = NULL;
4123149Seric 	if (cmntcnt > 0)
4133149Seric 		usrerr("Unbalanced '('");
4143149Seric 	else if (brccnt > 0)
4153149Seric 		usrerr("Unbalanced '<'");
4163149Seric 	else if (state == QSTRING)
4173149Seric 		usrerr("Unbalanced '\"'");
4183149Seric 	else if (av[0] != NULL)
4193149Seric 		return (av);
4203149Seric 	return (NULL);
4213149Seric }
4223149Seric /*
4233149Seric **  TOKTYPE -- return token type
4243149Seric **
4253149Seric **	Parameters:
4263149Seric **		c -- the character in question.
4273149Seric **
4283149Seric **	Returns:
4293149Seric **		Its type.
4303149Seric **
4313149Seric **	Side Effects:
4323149Seric **		none.
4333149Seric */
434297Seric 
4353149Seric toktype(c)
4363149Seric 	register char c;
4373149Seric {
4383149Seric 	if (isspace(c))
4393149Seric 		return (SPACE);
4403149Seric 	if (index(DELIMCHARS, c) != NULL || iscntrl(c))
4413149Seric 		return (OPER);
4423149Seric 	return (ATOM);
4433149Seric }
4443149Seric /*
4453149Seric **  REWRITE -- apply rewrite rules to token vector.
4463149Seric **
4473149Seric **	Parameters:
4483149Seric **		pvp -- pointer to token vector.
4493149Seric **
4503149Seric **	Returns:
4513149Seric **		none.
4523149Seric **
4533149Seric **	Side Effects:
4543149Seric **		pvp is modified.
4553149Seric */
4562091Seric 
4573149Seric struct match
4583149Seric {
4593149Seric 	char	**firsttok;	/* first token matched */
4603149Seric 	char	**lasttok;	/* last token matched */
4613149Seric 	char	name;		/* name of parameter */
4623149Seric };
4633149Seric 
4643149Seric # define MAXMATCH	8	/* max params per rewrite */
4653149Seric 
4663149Seric 
4673149Seric rewrite(pvp)
4683149Seric 	char **pvp;
4693149Seric {
4703149Seric 	register char *ap;		/* address pointer */
4713149Seric 	register char *rp;		/* rewrite pointer */
4723149Seric 	register char **avp;		/* address vector pointer */
4733149Seric 	register char **rvp;		/* rewrite vector pointer */
4743149Seric 	struct rewrite *rwr;
4753149Seric 	struct match mlist[MAXMATCH];
4763149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
4773149Seric 
4783188Seric # ifdef DEBUGX
4793149Seric 	if (Debug)
4803149Seric 	{
4813149Seric 		printf("rewrite: original pvp:\n");
4823149Seric 		printav(pvp);
4833149Seric 	}
4843188Seric # endif DEBUGX
4853149Seric 
4863149Seric 	/*
4873149Seric 	**  Run through the list of rewrite rules, applying
4883149Seric 	**	any that match.
4893149Seric 	*/
4903149Seric 
4913149Seric 	for (rwr = RewriteRules; rwr != NULL; )
4923149Seric 	{
4933188Seric # ifdef DEBUGX
4943149Seric 		if (Debug)
495297Seric 		{
4963149Seric 			printf("-----trying rule:\n");
4973149Seric 			printav(rwr->r_lhs);
4983149Seric 		}
4993188Seric # endif DEBUGX
5003149Seric 
5013149Seric 		/* try to match on this rule */
5023149Seric 		clrmatch(mlist);
5033149Seric 		for (rvp = rwr->r_lhs, avp = pvp; *avp != NULL; )
5043149Seric 		{
5053149Seric 			ap = *avp;
5063149Seric 			rp = *rvp;
5073149Seric 
5083149Seric 			if (rp == NULL)
509297Seric 			{
5103149Seric 				/* end-of-pattern before end-of-address */
5113149Seric 				goto fail;
512297Seric 			}
5133149Seric 
5143149Seric 			switch (*rp)
5153149Seric 			{
5163149Seric 			  case MATCHONE:
5173149Seric 				/* match exactly one token */
5183149Seric 				setmatch(mlist, rp[1], avp, avp);
5193149Seric 				break;
5203149Seric 
5213149Seric 			  case MATCHANY:
5223149Seric 				/* match any number of tokens */
5233149Seric 				setmatch(mlist, rp[1], NULL, avp);
5243149Seric 				break;
5253149Seric 
5263149Seric 			  default:
5273149Seric 				/* must have exact match */
5283149Seric 				/* can scribble rp & ap here safely */
5293154Seric 				while (*rp != '\0' || *ap != '\0')
5303149Seric 				{
5313149Seric 					if (*rp++ != lower(*ap++))
5323149Seric 						goto fail;
5333149Seric 				}
5343149Seric 				break;
5353149Seric 			}
5363149Seric 
5373149Seric 			/* successful match on this token */
5383149Seric 			avp++;
5393149Seric 			rvp++;
5403149Seric 			continue;
5413149Seric 
5423149Seric 		  fail:
5433149Seric 			/* match failed -- back up */
5443149Seric 			while (--rvp >= rwr->r_lhs)
5453149Seric 			{
5463149Seric 				rp = *rvp;
5473149Seric 				if (*rp == MATCHANY)
5483149Seric 					break;
5493149Seric 
5503149Seric 				/* can't extend match: back up everything */
5513149Seric 				avp--;
5523149Seric 
5533149Seric 				if (*rp == MATCHONE)
5543149Seric 				{
5553149Seric 					/* undo binding */
5563149Seric 					setmatch(mlist, rp[1], NULL, NULL);
5573149Seric 				}
5583149Seric 			}
5593149Seric 
5603149Seric 			if (rvp < rwr->r_lhs)
5613149Seric 			{
5623149Seric 				/* total failure to match */
5633149Seric 				break;
5643149Seric 			}
565297Seric 		}
5663149Seric 
5673149Seric 		/*
5683149Seric 		**  See if we successfully matched
5693149Seric 		*/
5703149Seric 
5713149Seric 		if (rvp >= rwr->r_lhs && *rvp == NULL)
5723149Seric 		{
5733188Seric # ifdef DEBUGX
5743149Seric 			if (Debug)
5753149Seric 			{
5763149Seric 				printf("-----rule matches:\n");
5773149Seric 				printav(rwr->r_rhs);
5783149Seric 			}
5793188Seric # endif DEBUGX
5803149Seric 
5813149Seric 			/* substitute */
5823149Seric 			for (rvp = rwr->r_rhs, avp = npvp; *rvp != NULL; rvp++)
5833149Seric 			{
5843149Seric 				rp = *rvp;
5853149Seric 				if (*rp == MATCHANY)
5863149Seric 				{
5873149Seric 					register struct match *m;
5883149Seric 					register char **pp;
5893149Seric 					extern struct match *findmatch();
5903149Seric 
5913149Seric 					m = findmatch(mlist, rp[1]);
5923149Seric 					if (m != NULL)
5933149Seric 					{
5943149Seric 						pp = m->firsttok;
5953149Seric 						do
5963149Seric 						{
5973149Seric 							*avp++ = *pp;
5983149Seric 						} while (pp++ != m->lasttok);
5993149Seric 					}
6003149Seric 				}
6013149Seric 				else
6023149Seric 					*avp++ = rp;
6033149Seric 			}
6043149Seric 			*avp++ = NULL;
6053149Seric 			bmove(npvp, pvp, (avp - npvp) * sizeof *avp);
6063149Seric # ifdef DEBUG
6073149Seric 			if (Debug)
6083149Seric 			{
6093188Seric 				char **vp;
6103188Seric 
6113188Seric 				printf("rewritten as `");
6123188Seric 				for (vp = pvp; *vp != NULL; vp++)
6133188Seric 					xputs(*vp);
6143188Seric 				printf("'\n");
6153149Seric 			}
6163149Seric # endif DEBUG
6173149Seric 			if (pvp[0][0] == CANONNET)
6183149Seric 				break;
6193149Seric 		}
6203149Seric 		else
6213149Seric 		{
6223188Seric # ifdef DEBUGX
6233149Seric 			if (Debug)
6243149Seric 				printf("----- rule fails\n");
6253188Seric # endif DEBUGX
6263149Seric 			rwr = rwr->r_next;
6273149Seric 		}
628297Seric 	}
6293149Seric }
6303149Seric /*
6313149Seric **  SETMATCH -- set parameter value in match vector
6323149Seric **
6333149Seric **	Parameters:
6343149Seric **		mlist -- list of match values.
6353149Seric **		name -- the character name of this parameter.
6363149Seric **		first -- the first location of the replacement.
6373149Seric **		last -- the last location of the replacement.
6383149Seric **
6393149Seric **		If last == NULL, delete this entry.
6403149Seric **		If first == NULL, extend this entry (or add it if
6413149Seric **			it does not exist).
6423149Seric **
6433149Seric **	Returns:
6443149Seric **		nothing.
6453149Seric **
6463149Seric **	Side Effects:
6473149Seric **		munges with mlist.
6483149Seric */
6493149Seric 
6503149Seric setmatch(mlist, name, first, last)
6513149Seric 	struct match *mlist;
6523149Seric 	char name;
6533149Seric 	char **first;
6543149Seric 	char **last;
6553149Seric {
6563149Seric 	register struct match *m;
6573149Seric 	struct match *nullm = NULL;
6583149Seric 
6593149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
6603149Seric 	{
6613149Seric 		if (m->name == name)
6623149Seric 			break;
6633149Seric 		if (m->name == '\0')
6643149Seric 			nullm = m;
6653149Seric 	}
6663149Seric 
6673149Seric 	if (m >= &mlist[MAXMATCH])
6683149Seric 		m = nullm;
6693149Seric 
6703149Seric 	if (last == NULL)
6713149Seric 	{
6723149Seric 		m->name = '\0';
6733149Seric 		return;
6743149Seric 	}
6753149Seric 
6763149Seric 	if (m->name == '\0')
6773149Seric 	{
6783149Seric 		if (first == NULL)
6793149Seric 			m->firsttok = last;
6803149Seric 		else
6813149Seric 			m->firsttok = first;
6823149Seric 	}
6833149Seric 	m->name = name;
6843149Seric 	m->lasttok = last;
6853149Seric }
6863149Seric /*
6873149Seric **  FINDMATCH -- find match in mlist
6883149Seric **
6893149Seric **	Parameters:
6903149Seric **		mlist -- list to search.
6913149Seric **		name -- name to find.
6923149Seric **
6933149Seric **	Returns:
6943149Seric **		pointer to match structure.
6953149Seric **		NULL if no match.
6963149Seric **
6973149Seric **	Side Effects:
6983149Seric **		none.
6993149Seric */
7003149Seric 
7013149Seric struct match *
7023149Seric findmatch(mlist, name)
7033149Seric 	struct match *mlist;
7043149Seric 	char name;
7053149Seric {
7063149Seric 	register struct match *m;
7073149Seric 
7083149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
7093149Seric 	{
7103149Seric 		if (m->name == name)
7113149Seric 			return (m);
7123149Seric 	}
7133149Seric 
714297Seric 	return (NULL);
715297Seric }
7163149Seric /*
7173149Seric **  CLRMATCH -- clear match list
7183149Seric **
7193149Seric **	Parameters:
7203149Seric **		mlist -- list to clear.
7213149Seric **
7223149Seric **	Returns:
7233149Seric **		none.
7243149Seric **
7253149Seric **	Side Effects:
7263149Seric **		mlist is cleared.
7273149Seric */
7283149Seric 
7293149Seric clrmatch(mlist)
7303149Seric 	struct match *mlist;
7313149Seric {
7323149Seric 	register struct match *m;
7333149Seric 
7343149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
7353149Seric 		m->name = '\0';
7363149Seric }
7373149Seric /*
7383149Seric **  BUILDADDR -- build address from token vector.
7393149Seric **
7403149Seric **	Parameters:
7413149Seric **		tv -- token vector.
7423149Seric **		a -- pointer to address descriptor to fill.
7433149Seric **			If NULL, one will be allocated.
7443149Seric **
7453149Seric **	Returns:
7463149Seric **		'a'
7473149Seric **
7483149Seric **	Side Effects:
7493149Seric **		fills in 'a'
7503149Seric */
7513149Seric 
7523149Seric ADDRESS *
7533149Seric buildaddr(tv, a)
7543149Seric 	register char **tv;
7553149Seric 	register ADDRESS *a;
7563149Seric {
7573149Seric 	register int i;
7583149Seric 	static char buf[MAXNAME];
7593149Seric 	struct mailer **mp;
7603149Seric 	register struct mailer *m;
7613149Seric 	extern char *xalloc();
7623149Seric 
7633149Seric 	if (a == NULL)
7643149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
7653188Seric 	a->q_flags = 0;
7663149Seric 
7673149Seric 	/* figure out what net/mailer to use */
7683149Seric 	if (**tv != CANONNET)
7693149Seric 		syserr("buildaddr: no net");
7703149Seric 	tv++;
7713154Seric 	for (mp = Mailer, i = 0; (m = *mp++) != NULL; i++)
7723149Seric 	{
7733149Seric 		if (strcmp(m->m_name, *tv) == 0)
7743149Seric 			break;
7753149Seric 	}
7763149Seric 	if (m == NULL)
7773149Seric 		syserr("buildaddr: unknown net %s", *tv);
7783149Seric 	a->q_mailer = i;
7793149Seric 
7803149Seric 	/* figure out what host (if any) */
7813149Seric 	tv++;
7823149Seric 	if (!bitset(M_NOHOST, m->m_flags))
7833149Seric 	{
7843149Seric 		if (**tv != CANONHOST)
7853149Seric 			syserr("buildaddr: no host");
7863149Seric 		tv++;
7873149Seric 		a->q_host = *tv;
7883149Seric 		tv++;
7893149Seric 	}
7903149Seric 	else
7913149Seric 		a->q_host = NULL;
7923149Seric 
7933149Seric 	/* figure out the user */
7943149Seric 	if (**tv != CANONUSER)
7953149Seric 		syserr("buildaddr: no user");
7963149Seric 	buf[0] = '\0';
7973149Seric 	while (**++tv != NULL)
7983149Seric 		strcat(buf, *tv);
7993149Seric 	a->q_user = buf;
8003149Seric 
8013149Seric 	return (a);
8023149Seric }
8033188Seric /*
8043188Seric **  SAMEADDR -- Determine if two addresses are the same
8053188Seric **
8063188Seric **	This is not just a straight comparison -- if the mailer doesn't
8073188Seric **	care about the host we just ignore it, etc.
8083188Seric **
8093188Seric **	Parameters:
8103188Seric **		a, b -- pointers to the internal forms to compare.
8113188Seric **		wildflg -- if TRUE, 'a' may have no user specified,
8123188Seric **			in which case it is to match anything.
8133188Seric **
8143188Seric **	Returns:
8153188Seric **		TRUE -- they represent the same mailbox.
8163188Seric **		FALSE -- they don't.
8173188Seric **
8183188Seric **	Side Effects:
8193188Seric **		none.
8203188Seric */
8213188Seric 
8223188Seric bool
8233188Seric sameaddr(a, b, wildflg)
8243188Seric 	register ADDRESS *a;
8253188Seric 	register ADDRESS *b;
8263188Seric 	bool wildflg;
8273188Seric {
8283188Seric 	/* if they don't have the same mailer, forget it */
8293188Seric 	if (a->q_mailer != b->q_mailer)
8303188Seric 		return (FALSE);
8313188Seric 
8323188Seric 	/* if the user isn't the same, we can drop out */
8333188Seric 	if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0)
8343188Seric 		return (FALSE);
8353188Seric 
8363188Seric 	/* if the mailer ignores hosts, we have succeeded! */
8373188Seric 	if (bitset(M_NOHOST, Mailer[a->q_mailer]->m_flags))
8383188Seric 		return (TRUE);
8393188Seric 
8403188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
8413188Seric 	if (a->q_host == NULL || b->q_host == NULL)
8423188Seric 		return (FALSE);
8433188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
8443188Seric 		return (FALSE);
8453188Seric 
8463188Seric 	return (TRUE);
8473188Seric }
8483234Seric /*
8493234Seric **  PRINTADDR -- print address (for debugging)
8503234Seric **
8513234Seric **	Parameters:
8523234Seric **		a -- the address to print
8533234Seric **		follow -- follow the q_next chain.
8543234Seric **
8553234Seric **	Returns:
8563234Seric **		none.
8573234Seric **
8583234Seric **	Side Effects:
8593234Seric **		none.
8603234Seric */
8613234Seric 
8623234Seric printaddr(a, follow)
8633234Seric 	register ADDRESS *a;
8643234Seric 	bool follow;
8653234Seric {
8663234Seric 	while (a != NULL)
8673234Seric 	{
8683234Seric 		printf("addr@%x: ", a);
8693234Seric 		fflush(stdout);
8703234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
8713234Seric 		       a->q_mailer, Mailer[a->q_mailer]->m_name, a->q_host, a->q_user);
8723234Seric 		printf("\tnext=%x flags=%o, rmailer %d\n", a->q_next,
8733234Seric 		       a->q_flags, a->q_rmailer);
8743234Seric 
8753234Seric 		if (!follow)
8763234Seric 			return;
8773234Seric 		a = a->q_next;
8783234Seric 	}
8793234Seric }
880