1297Seric # include <stdio.h>
2297Seric # include <ctype.h>
33312Seric # include "sendmail.h"
4297Seric 
5*4085Seric static char	SccsId[] = "@(#)parseaddr.c	3.16	08/09/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 
493380Seric # define DELIMCHARS	"$()<>,;\\\"\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();
613149Seric 	extern ADDRESS *buildaddr();
62297Seric 
63297Seric 	/*
64297Seric 	**  Initialize and prescan address.
65297Seric 	*/
66297Seric 
67297Seric 	To = addr;
683188Seric # ifdef DEBUG
693188Seric 	if (Debug)
703188Seric 		printf("\n--parse(%s)\n", addr);
713188Seric # endif DEBUG
723188Seric 
733149Seric 	pvp = prescan(addr, '\0');
743149Seric 	if (pvp == NULL)
75297Seric 		return (NULL);
76297Seric 
77297Seric 	/*
783149Seric 	**  Apply rewriting rules.
79297Seric 	*/
80297Seric 
814070Seric 	rewrite(pvp, 0);
82297Seric 
833149Seric 	/*
843149Seric 	**  See if we resolved to a real mailer.
853149Seric 	*/
86297Seric 
873149Seric 	if (pvp[0][0] != CANONNET)
883149Seric 	{
893149Seric 		setstat(EX_USAGE);
903149Seric 		usrerr("cannot resolve name");
913149Seric 		return (NULL);
92297Seric 	}
93297Seric 
94297Seric 	/*
953149Seric 	**  Build canonical address from pvp.
96297Seric 	*/
97297Seric 
983149Seric 	a = buildaddr(pvp, a);
993149Seric 	m = Mailer[a->q_mailer];
100297Seric 
101297Seric 	/*
1023149Seric 	**  Make local copies of the host & user and then
1033149Seric 	**  transport them out.
104297Seric 	*/
105297Seric 
106297Seric 	if (copyf > 0)
1072973Seric 		a->q_paddr = newstr(addr);
108297Seric 	else
109297Seric 		a->q_paddr = addr;
110297Seric 
1113149Seric 	if (copyf >= 0)
112297Seric 	{
1133149Seric 		if (a->q_host != NULL)
1143149Seric 			a->q_host = newstr(a->q_host);
115297Seric 		else
1163149Seric 			a->q_host = "";
1173149Seric 		if (a->q_user != a->q_paddr)
1183149Seric 			a->q_user = newstr(a->q_user);
119297Seric 	}
120297Seric 
121297Seric 	/*
122297Seric 	**  Do UPPER->lower case mapping unless inhibited.
123297Seric 	*/
124297Seric 
1253149Seric 	if (!bitset(M_HST_UPPER, m->m_flags))
126297Seric 		makelower(a->q_host);
1273149Seric 	if (!bitset(M_USR_UPPER, m->m_flags))
128297Seric 		makelower(a->q_user);
129297Seric 
130297Seric 	/*
131297Seric 	**  Compute return value.
132297Seric 	*/
133297Seric 
134297Seric # ifdef DEBUG
1351583Seric 	if (Debug)
136297Seric 		printf("parse(\"%s\"): host \"%s\" user \"%s\" mailer %d\n",
1373149Seric 		    addr, a->q_host, a->q_user, a->q_mailer);
138297Seric # endif DEBUG
139297Seric 
140297Seric 	return (a);
141297Seric }
142297Seric /*
143297Seric **  PRESCAN -- Prescan name and make it canonical
144297Seric **
145297Seric **	Scans a name and turns it into canonical form.  This involves
146297Seric **	deleting blanks, comments (in parentheses), and turning the
147297Seric **	word "at" into an at-sign ("@").  The name is copied as this
148297Seric **	is done; it is legal to copy a name onto itself, since this
149297Seric **	process can only make things smaller.
150297Seric **
151297Seric **	This routine knows about quoted strings and angle brackets.
152297Seric **
153297Seric **	There are certain subtleties to this routine.  The one that
154297Seric **	comes to mind now is that backslashes on the ends of names
155297Seric **	are silently stripped off; this is intentional.  The problem
156297Seric **	is that some versions of sndmsg (like at LBL) set the kill
157297Seric **	character to something other than @ when reading addresses;
158297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
159297Seric **	berknet mailer.
160297Seric **
161297Seric **	Parameters:
162297Seric **		addr -- the name to chomp.
163297Seric **		delim -- the delimiter for the address, normally
164297Seric **			'\0' or ','; \0 is accepted in any case.
165297Seric **			are moving in place; set buflim to high core.
166297Seric **
167297Seric **	Returns:
1683149Seric **		A pointer to a vector of tokens.
169297Seric **		NULL on error.
170297Seric **
171297Seric **	Side Effects:
1723149Seric **		none.
173297Seric */
174297Seric 
1753149Seric # define OPER		1
1763149Seric # define ATOM		2
1773149Seric # define EOTOK		3
1783149Seric # define QSTRING	4
1793149Seric # define SPACE		5
1803149Seric # define DOLLAR		6
1813149Seric # define GETONE		7
1823149Seric 
1833149Seric char **
1843149Seric prescan(addr, delim)
185297Seric 	char *addr;
186297Seric 	char delim;
187297Seric {
188297Seric 	register char *p;
1893149Seric 	static char buf[MAXNAME+MAXATOM];
1903149Seric 	static char *av[MAXATOM+1];
1913149Seric 	char **avp;
192297Seric 	bool space;
193297Seric 	bool bslashmode;
194297Seric 	int cmntcnt;
195297Seric 	int brccnt;
196297Seric 	register char c;
1973149Seric 	char *tok;
198297Seric 	register char *q;
1993149Seric 	register int state;
2003149Seric 	int nstate;
201*4085Seric 	extern char lower();
202297Seric 
2032091Seric 	space = FALSE;
204297Seric 	q = buf;
2053149Seric 	bslashmode = FALSE;
206297Seric 	cmntcnt = brccnt = 0;
2073149Seric 	avp = av;
2083149Seric 	state = OPER;
2093149Seric 	for (p = addr; *p != '\0' && *p != delim; )
210297Seric 	{
2113149Seric 		/* read a token */
2123149Seric 		tok = q;
2133149Seric 		while ((c = *p++) != '\0' && c != delim)
214297Seric 		{
2153149Seric 			/* chew up special characters */
2163149Seric 			*q = '\0';
2173149Seric 			if (bslashmode)
2183149Seric 			{
2193149Seric 				c |= 0200;
2203149Seric 				bslashmode = FALSE;
2213149Seric 			}
2223149Seric 			else if (c == '\\')
2233149Seric 			{
2243149Seric 				bslashmode = TRUE;
2253149Seric 				continue;
2263149Seric 			}
2273149Seric 
2283149Seric 			nstate = toktype(c);
2293149Seric 			switch (state)
2303149Seric 			{
2313149Seric 			  case QSTRING:		/* in quoted string */
2323149Seric 				if (c == '"')
2333149Seric 					state = OPER;
2343149Seric 				break;
2353149Seric 
2363149Seric 			  case ATOM:		/* regular atom */
2373149Seric 				state = nstate;
2383149Seric 				if (state != ATOM)
2393149Seric 				{
2403149Seric 					state = EOTOK;
2413149Seric 					p--;
2423149Seric 				}
2433149Seric 				break;
2443149Seric 
2453149Seric 			  case GETONE:		/* grab one character */
2463149Seric 				state = OPER;
2473149Seric 				break;
2483149Seric 
2493149Seric 			  case EOTOK:		/* after atom or q-string */
2503149Seric 				state = nstate;
2513149Seric 				if (state == SPACE)
2523149Seric 					continue;
2533149Seric 				break;
2543149Seric 
2553149Seric 			  case SPACE:		/* linear white space */
2563149Seric 				state = nstate;
2573149Seric 				space = TRUE;
2583149Seric 				continue;
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:
3013149Seric 					c = '$';
3023149Seric 					state = OPER;
3033149Seric 					p--;
3043149Seric 					break;
3053149Seric 				}
3063149Seric 				break;
3073149Seric 
3083149Seric 			  default:
3093149Seric 				syserr("prescan: unknown state %d", state);
3103149Seric 			}
3113149Seric 
3123149Seric 			if (state == OPER)
3133149Seric 				space = FALSE;
3143149Seric 			else if (state == EOTOK)
3153149Seric 				break;
3163149Seric 			if (c == '$' && delim == '\t')
3173149Seric 			{
3183149Seric 				state = DOLLAR;
3193149Seric 				continue;
3203149Seric 			}
3213149Seric 
3223149Seric 			/* squirrel it away */
3233149Seric 			if (q >= &buf[sizeof buf - 5])
3243149Seric 			{
3253149Seric 				usrerr("Address too long");
3263149Seric 				return (NULL);
3273149Seric 			}
3283149Seric 			if (space)
3293149Seric 				*q++ = SPACESUB;
3303149Seric 			*q++ = c;
3313149Seric 
3323149Seric 			/* decide whether this represents end of token */
3333149Seric 			if (state == OPER)
3343149Seric 				break;
335297Seric 		}
3363149Seric 		if (c == '\0' || c == delim)
3373149Seric 			p--;
3383149Seric 
3393149Seric 		/* new token */
3403149Seric 		if (tok == q)
341297Seric 			continue;
3423149Seric 		*q++ = '\0';
3433149Seric 
3443149Seric 		c = tok[0];
3453149Seric 		if (c == '(')
3461378Seric 		{
347297Seric 			cmntcnt++;
3481378Seric 			continue;
3491378Seric 		}
350297Seric 		else if (c == ')')
351297Seric 		{
352297Seric 			if (cmntcnt <= 0)
353297Seric 			{
354297Seric 				usrerr("Unbalanced ')'");
355297Seric 				return (NULL);
356297Seric 			}
357297Seric 			else
358297Seric 			{
359297Seric 				cmntcnt--;
360297Seric 				continue;
361297Seric 			}
362297Seric 		}
3633149Seric 		else if (cmntcnt > 0)
3642091Seric 			continue;
3653149Seric 
3663149Seric 		*avp++ = tok;
3673149Seric 
3683149Seric 		/* we prefer <> specs */
3693149Seric 		if (c == '<')
370297Seric 		{
3712092Seric 			if (brccnt < 0)
3722092Seric 			{
3732092Seric 				usrerr("multiple < spec");
3742092Seric 				return (NULL);
3752092Seric 			}
376297Seric 			brccnt++;
3772091Seric 			space = FALSE;
378297Seric 			if (brccnt == 1)
379297Seric 			{
380297Seric 				/* we prefer using machine readable name */
381297Seric 				q = buf;
382297Seric 				*q = '\0';
3833149Seric 				avp = av;
384297Seric 				continue;
385297Seric 			}
386297Seric 		}
387297Seric 		else if (c == '>')
388297Seric 		{
389297Seric 			if (brccnt <= 0)
390297Seric 			{
391297Seric 				usrerr("Unbalanced `>'");
392297Seric 				return (NULL);
393297Seric 			}
394297Seric 			else
395297Seric 				brccnt--;
396297Seric 			if (brccnt <= 0)
3972092Seric 			{
3982092Seric 				brccnt = -1;
399297Seric 				continue;
4002092Seric 			}
401297Seric 		}
402297Seric 
403297Seric 		/*
404297Seric 		**  Turn "at" into "@",
4051378Seric 		**	but only if "at" is a word.
406297Seric 		*/
407297Seric 
4083149Seric 		if (lower(tok[0]) == 'a' && lower(tok[1]) == 't' && tok[2] == '\0')
409297Seric 		{
4103149Seric 			tok[0] = '@';
4113149Seric 			tok[1] = '\0';
412297Seric 		}
4133149Seric 	}
4143149Seric 	*avp = NULL;
4153149Seric 	if (cmntcnt > 0)
4163149Seric 		usrerr("Unbalanced '('");
4173149Seric 	else if (brccnt > 0)
4183149Seric 		usrerr("Unbalanced '<'");
4193149Seric 	else if (state == QSTRING)
4203149Seric 		usrerr("Unbalanced '\"'");
4213149Seric 	else if (av[0] != NULL)
4223149Seric 		return (av);
4233149Seric 	return (NULL);
4243149Seric }
4253149Seric /*
4263149Seric **  TOKTYPE -- return token type
4273149Seric **
4283149Seric **	Parameters:
4293149Seric **		c -- the character in question.
4303149Seric **
4313149Seric **	Returns:
4323149Seric **		Its type.
4333149Seric **
4343149Seric **	Side Effects:
4353149Seric **		none.
4363149Seric */
437297Seric 
4383149Seric toktype(c)
4393149Seric 	register char c;
4403149Seric {
4413380Seric 	static char buf[50];
4423382Seric 	static bool firstime = TRUE;
4433380Seric 
4443382Seric 	if (firstime)
4453380Seric 	{
4463382Seric 		firstime = FALSE;
447*4085Seric 		(void) expand("$o", buf, &buf[sizeof buf - 1]);
4483380Seric 		strcat(buf, DELIMCHARS);
4493380Seric 	}
4503149Seric 	if (isspace(c))
4513149Seric 		return (SPACE);
4523380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
4533149Seric 		return (OPER);
4543149Seric 	return (ATOM);
4553149Seric }
4563149Seric /*
4573149Seric **  REWRITE -- apply rewrite rules to token vector.
4583149Seric **
4593149Seric **	Parameters:
4603149Seric **		pvp -- pointer to token vector.
4613149Seric **
4623149Seric **	Returns:
4633149Seric **		none.
4643149Seric **
4653149Seric **	Side Effects:
4663149Seric **		pvp is modified.
4673149Seric */
4682091Seric 
4693149Seric struct match
4703149Seric {
4713149Seric 	char	**firsttok;	/* first token matched */
4723149Seric 	char	**lasttok;	/* last token matched */
4733149Seric 	char	name;		/* name of parameter */
4743149Seric };
4753149Seric 
4763149Seric # define MAXMATCH	8	/* max params per rewrite */
4773149Seric 
4783149Seric 
4794070Seric rewrite(pvp, ruleset)
4803149Seric 	char **pvp;
4814070Seric 	int ruleset;
4823149Seric {
4833149Seric 	register char *ap;		/* address pointer */
4843149Seric 	register char *rp;		/* rewrite pointer */
4853149Seric 	register char **avp;		/* address vector pointer */
4863149Seric 	register char **rvp;		/* rewrite vector pointer */
4873149Seric 	struct rewrite *rwr;
4883149Seric 	struct match mlist[MAXMATCH];
4893149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
4904060Seric 	extern bool sameword();
4913149Seric 
4923188Seric # ifdef DEBUGX
4933149Seric 	if (Debug)
4943149Seric 	{
4953149Seric 		printf("rewrite: original pvp:\n");
4963149Seric 		printav(pvp);
4973149Seric 	}
4983188Seric # endif DEBUGX
4993149Seric 
5003149Seric 	/*
5013149Seric 	**  Run through the list of rewrite rules, applying
5023149Seric 	**	any that match.
5033149Seric 	*/
5043149Seric 
5054070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
5063149Seric 	{
5073188Seric # ifdef DEBUGX
5083149Seric 		if (Debug)
509297Seric 		{
5103149Seric 			printf("-----trying rule:\n");
5113149Seric 			printav(rwr->r_lhs);
5123149Seric 		}
5133188Seric # endif DEBUGX
5143149Seric 
5153149Seric 		/* try to match on this rule */
5163149Seric 		clrmatch(mlist);
5173149Seric 		for (rvp = rwr->r_lhs, avp = pvp; *avp != NULL; )
5183149Seric 		{
5193149Seric 			ap = *avp;
5203149Seric 			rp = *rvp;
5213149Seric 
5223149Seric 			if (rp == NULL)
523297Seric 			{
5243149Seric 				/* end-of-pattern before end-of-address */
5253149Seric 				goto fail;
526297Seric 			}
5273149Seric 
5283149Seric 			switch (*rp)
5293149Seric 			{
5304060Seric 				register STAB *s;
5314060Seric 				register int class;
5324060Seric 
5333149Seric 			  case MATCHONE:
5343149Seric 				/* match exactly one token */
5353149Seric 				setmatch(mlist, rp[1], avp, avp);
5363149Seric 				break;
5373149Seric 
5383149Seric 			  case MATCHANY:
5393149Seric 				/* match any number of tokens */
540*4085Seric 				setmatch(mlist, rp[1], (char **) NULL, avp);
5413149Seric 				break;
5423149Seric 
5434060Seric 			  case MATCHCLASS:
5444060Seric 				/* match any token in a class */
5454060Seric 				class = rp[1];
5464060Seric 				if (!isalpha(class))
5474060Seric 					goto fail;
5484060Seric 				if (isupper(class))
5494060Seric 					class -= 'A';
5504060Seric 				else
5514060Seric 					class -= 'a';
5524060Seric 				s = stab(ap, ST_FIND);
5534060Seric 				if (s == NULL || (s->s_class & (1 << class)) == 0)
5544060Seric 					goto fail;
5554060Seric 				break;
5564060Seric 
5573149Seric 			  default:
5583149Seric 				/* must have exact match */
5594060Seric 				if (!sameword(rp, ap))
5604060Seric 					goto fail;
5613149Seric 				break;
5623149Seric 			}
5633149Seric 
5643149Seric 			/* successful match on this token */
5653149Seric 			avp++;
5663149Seric 			rvp++;
5673149Seric 			continue;
5683149Seric 
5693149Seric 		  fail:
5703149Seric 			/* match failed -- back up */
5713149Seric 			while (--rvp >= rwr->r_lhs)
5723149Seric 			{
5733149Seric 				rp = *rvp;
5743149Seric 				if (*rp == MATCHANY)
5753149Seric 					break;
5763149Seric 
5773149Seric 				/* can't extend match: back up everything */
5783149Seric 				avp--;
5793149Seric 
5803149Seric 				if (*rp == MATCHONE)
5813149Seric 				{
5823149Seric 					/* undo binding */
583*4085Seric 					setmatch(mlist, rp[1], (char **) NULL, (char **) NULL);
5843149Seric 				}
5853149Seric 			}
5863149Seric 
5873149Seric 			if (rvp < rwr->r_lhs)
5883149Seric 			{
5893149Seric 				/* total failure to match */
5903149Seric 				break;
5913149Seric 			}
592297Seric 		}
5933149Seric 
5943149Seric 		/*
5953149Seric 		**  See if we successfully matched
5963149Seric 		*/
5973149Seric 
5983149Seric 		if (rvp >= rwr->r_lhs && *rvp == NULL)
5993149Seric 		{
6003188Seric # ifdef DEBUGX
6013149Seric 			if (Debug)
6023149Seric 			{
6033149Seric 				printf("-----rule matches:\n");
6043149Seric 				printav(rwr->r_rhs);
6053149Seric 			}
6063188Seric # endif DEBUGX
6073149Seric 
6083149Seric 			/* substitute */
6093149Seric 			for (rvp = rwr->r_rhs, avp = npvp; *rvp != NULL; rvp++)
6103149Seric 			{
6113149Seric 				rp = *rvp;
6123149Seric 				if (*rp == MATCHANY)
6133149Seric 				{
6143149Seric 					register struct match *m;
6153149Seric 					register char **pp;
6163149Seric 					extern struct match *findmatch();
6173149Seric 
6183149Seric 					m = findmatch(mlist, rp[1]);
6193149Seric 					if (m != NULL)
6203149Seric 					{
6213149Seric 						pp = m->firsttok;
6223149Seric 						do
6233149Seric 						{
6243149Seric 							*avp++ = *pp;
6253149Seric 						} while (pp++ != m->lasttok);
6263149Seric 					}
6273149Seric 				}
6283149Seric 				else
6293149Seric 					*avp++ = rp;
6303149Seric 			}
6313149Seric 			*avp++ = NULL;
632*4085Seric 			bmove((char *) npvp, (char *) pvp, (avp - npvp) * sizeof *avp);
6333149Seric # ifdef DEBUG
6343149Seric 			if (Debug)
6353149Seric 			{
6363188Seric 				char **vp;
6373188Seric 
6383188Seric 				printf("rewritten as `");
6393188Seric 				for (vp = pvp; *vp != NULL; vp++)
6403188Seric 					xputs(*vp);
6413188Seric 				printf("'\n");
6423149Seric 			}
6433149Seric # endif DEBUG
6443149Seric 			if (pvp[0][0] == CANONNET)
6453149Seric 				break;
6463149Seric 		}
6473149Seric 		else
6483149Seric 		{
6493188Seric # ifdef DEBUGX
6503149Seric 			if (Debug)
6513149Seric 				printf("----- rule fails\n");
6523188Seric # endif DEBUGX
6533149Seric 			rwr = rwr->r_next;
6543149Seric 		}
655297Seric 	}
6563149Seric }
6573149Seric /*
6583149Seric **  SETMATCH -- set parameter value in match vector
6593149Seric **
6603149Seric **	Parameters:
6613149Seric **		mlist -- list of match values.
6623149Seric **		name -- the character name of this parameter.
6633149Seric **		first -- the first location of the replacement.
6643149Seric **		last -- the last location of the replacement.
6653149Seric **
6663149Seric **		If last == NULL, delete this entry.
6673149Seric **		If first == NULL, extend this entry (or add it if
6683149Seric **			it does not exist).
6693149Seric **
6703149Seric **	Returns:
6713149Seric **		nothing.
6723149Seric **
6733149Seric **	Side Effects:
6743149Seric **		munges with mlist.
6753149Seric */
6763149Seric 
6773149Seric setmatch(mlist, name, first, last)
6783149Seric 	struct match *mlist;
6793149Seric 	char name;
6803149Seric 	char **first;
6813149Seric 	char **last;
6823149Seric {
6833149Seric 	register struct match *m;
6843149Seric 	struct match *nullm = NULL;
6853149Seric 
6863149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
6873149Seric 	{
6883149Seric 		if (m->name == name)
6893149Seric 			break;
6903149Seric 		if (m->name == '\0')
6913149Seric 			nullm = m;
6923149Seric 	}
6933149Seric 
6943149Seric 	if (m >= &mlist[MAXMATCH])
6953149Seric 		m = nullm;
6963149Seric 
6973149Seric 	if (last == NULL)
6983149Seric 	{
6993149Seric 		m->name = '\0';
7003149Seric 		return;
7013149Seric 	}
7023149Seric 
7033149Seric 	if (m->name == '\0')
7043149Seric 	{
7053149Seric 		if (first == NULL)
7063149Seric 			m->firsttok = last;
7073149Seric 		else
7083149Seric 			m->firsttok = first;
7093149Seric 	}
7103149Seric 	m->name = name;
7113149Seric 	m->lasttok = last;
7123149Seric }
7133149Seric /*
7143149Seric **  FINDMATCH -- find match in mlist
7153149Seric **
7163149Seric **	Parameters:
7173149Seric **		mlist -- list to search.
7183149Seric **		name -- name to find.
7193149Seric **
7203149Seric **	Returns:
7213149Seric **		pointer to match structure.
7223149Seric **		NULL if no match.
7233149Seric **
7243149Seric **	Side Effects:
7253149Seric **		none.
7263149Seric */
7273149Seric 
7283149Seric struct match *
7293149Seric findmatch(mlist, name)
7303149Seric 	struct match *mlist;
7313149Seric 	char name;
7323149Seric {
7333149Seric 	register struct match *m;
7343149Seric 
7353149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
7363149Seric 	{
7373149Seric 		if (m->name == name)
7383149Seric 			return (m);
7393149Seric 	}
7403149Seric 
741297Seric 	return (NULL);
742297Seric }
7433149Seric /*
7443149Seric **  CLRMATCH -- clear match list
7453149Seric **
7463149Seric **	Parameters:
7473149Seric **		mlist -- list to clear.
7483149Seric **
7493149Seric **	Returns:
7503149Seric **		none.
7513149Seric **
7523149Seric **	Side Effects:
7533149Seric **		mlist is cleared.
7543149Seric */
7553149Seric 
7563149Seric clrmatch(mlist)
7573149Seric 	struct match *mlist;
7583149Seric {
7593149Seric 	register struct match *m;
7603149Seric 
7613149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
7623149Seric 		m->name = '\0';
7633149Seric }
7643149Seric /*
7653149Seric **  BUILDADDR -- build address from token vector.
7663149Seric **
7673149Seric **	Parameters:
7683149Seric **		tv -- token vector.
7693149Seric **		a -- pointer to address descriptor to fill.
7703149Seric **			If NULL, one will be allocated.
7713149Seric **
7723149Seric **	Returns:
7733149Seric **		'a'
7743149Seric **
7753149Seric **	Side Effects:
7763149Seric **		fills in 'a'
7773149Seric */
7783149Seric 
7793149Seric ADDRESS *
7803149Seric buildaddr(tv, a)
7813149Seric 	register char **tv;
7823149Seric 	register ADDRESS *a;
7833149Seric {
7843149Seric 	register int i;
7853149Seric 	static char buf[MAXNAME];
7863149Seric 	struct mailer **mp;
7873149Seric 	register struct mailer *m;
7883149Seric 
7893149Seric 	if (a == NULL)
7903149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
7913188Seric 	a->q_flags = 0;
7924079Seric 	a->q_home = NULL;
7933149Seric 
7943149Seric 	/* figure out what net/mailer to use */
7953149Seric 	if (**tv != CANONNET)
7963149Seric 		syserr("buildaddr: no net");
7973149Seric 	tv++;
7983154Seric 	for (mp = Mailer, i = 0; (m = *mp++) != NULL; i++)
7993149Seric 	{
8003149Seric 		if (strcmp(m->m_name, *tv) == 0)
8013149Seric 			break;
8023149Seric 	}
8033149Seric 	if (m == NULL)
8043149Seric 		syserr("buildaddr: unknown net %s", *tv);
8053149Seric 	a->q_mailer = i;
8063149Seric 
8073149Seric 	/* figure out what host (if any) */
8083149Seric 	tv++;
8093149Seric 	if (!bitset(M_NOHOST, m->m_flags))
8103149Seric 	{
8113149Seric 		if (**tv != CANONHOST)
8123149Seric 			syserr("buildaddr: no host");
8133149Seric 		tv++;
8143149Seric 		a->q_host = *tv;
8153149Seric 		tv++;
8163149Seric 	}
8173149Seric 	else
8183149Seric 		a->q_host = NULL;
8193149Seric 
8203149Seric 	/* figure out the user */
8213149Seric 	if (**tv != CANONUSER)
8223149Seric 		syserr("buildaddr: no user");
8233149Seric 	buf[0] = '\0';
8243149Seric 	while (**++tv != NULL)
825*4085Seric 		(void) strcat(buf, *tv);
8263149Seric 	a->q_user = buf;
8273149Seric 
8283149Seric 	return (a);
8293149Seric }
8303188Seric /*
8313188Seric **  SAMEADDR -- Determine if two addresses are the same
8323188Seric **
8333188Seric **	This is not just a straight comparison -- if the mailer doesn't
8343188Seric **	care about the host we just ignore it, etc.
8353188Seric **
8363188Seric **	Parameters:
8373188Seric **		a, b -- pointers to the internal forms to compare.
8383188Seric **		wildflg -- if TRUE, 'a' may have no user specified,
8393188Seric **			in which case it is to match anything.
8403188Seric **
8413188Seric **	Returns:
8423188Seric **		TRUE -- they represent the same mailbox.
8433188Seric **		FALSE -- they don't.
8443188Seric **
8453188Seric **	Side Effects:
8463188Seric **		none.
8473188Seric */
8483188Seric 
8493188Seric bool
8503188Seric sameaddr(a, b, wildflg)
8513188Seric 	register ADDRESS *a;
8523188Seric 	register ADDRESS *b;
8533188Seric 	bool wildflg;
8543188Seric {
8553188Seric 	/* if they don't have the same mailer, forget it */
8563188Seric 	if (a->q_mailer != b->q_mailer)
8573188Seric 		return (FALSE);
8583188Seric 
8593188Seric 	/* if the user isn't the same, we can drop out */
8603188Seric 	if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0)
8613188Seric 		return (FALSE);
8623188Seric 
8633188Seric 	/* if the mailer ignores hosts, we have succeeded! */
8643188Seric 	if (bitset(M_NOHOST, Mailer[a->q_mailer]->m_flags))
8653188Seric 		return (TRUE);
8663188Seric 
8673188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
8683188Seric 	if (a->q_host == NULL || b->q_host == NULL)
8693188Seric 		return (FALSE);
8703188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
8713188Seric 		return (FALSE);
8723188Seric 
8733188Seric 	return (TRUE);
8743188Seric }
8753234Seric /*
8763234Seric **  PRINTADDR -- print address (for debugging)
8773234Seric **
8783234Seric **	Parameters:
8793234Seric **		a -- the address to print
8803234Seric **		follow -- follow the q_next chain.
8813234Seric **
8823234Seric **	Returns:
8833234Seric **		none.
8843234Seric **
8853234Seric **	Side Effects:
8863234Seric **		none.
8873234Seric */
8883234Seric 
8893234Seric printaddr(a, follow)
8903234Seric 	register ADDRESS *a;
8913234Seric 	bool follow;
8923234Seric {
8933234Seric 	while (a != NULL)
8943234Seric 	{
8953234Seric 		printf("addr@%x: ", a);
896*4085Seric 		(void) fflush(stdout);
8973234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
8983234Seric 		       a->q_mailer, Mailer[a->q_mailer]->m_name, a->q_host, a->q_user);
8993234Seric 		printf("\tnext=%x flags=%o, rmailer %d\n", a->q_next,
9003234Seric 		       a->q_flags, a->q_rmailer);
9013234Seric 
9023234Seric 		if (!follow)
9033234Seric 			return;
9043234Seric 		a = a->q_next;
9053234Seric 	}
9063234Seric }
907