13312Seric # include "sendmail.h"
2297Seric 
3*4279Seric static char	SccsId[] = "@(#)parseaddr.c	3.22	08/31/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 # define SPACESUB	('.'|0200)		/* substitution for <lwsp> */
432091Seric 
442973Seric ADDRESS *
45297Seric parse(addr, a, copyf)
46297Seric 	char *addr;
472973Seric 	register ADDRESS *a;
48297Seric 	int copyf;
49297Seric {
503149Seric 	register char **pvp;
513149Seric 	register struct mailer *m;
523149Seric 	extern char **prescan();
533149Seric 	extern ADDRESS *buildaddr();
54297Seric 
55297Seric 	/*
56297Seric 	**  Initialize and prescan address.
57297Seric 	*/
58297Seric 
59297Seric 	To = addr;
603188Seric # ifdef DEBUG
613188Seric 	if (Debug)
623188Seric 		printf("\n--parse(%s)\n", addr);
633188Seric # endif DEBUG
643188Seric 
653149Seric 	pvp = prescan(addr, '\0');
663149Seric 	if (pvp == NULL)
67297Seric 		return (NULL);
68297Seric 
69297Seric 	/*
703149Seric 	**  Apply rewriting rules.
71297Seric 	*/
72297Seric 
734070Seric 	rewrite(pvp, 0);
74297Seric 
753149Seric 	/*
763149Seric 	**  See if we resolved to a real mailer.
773149Seric 	*/
78297Seric 
793149Seric 	if (pvp[0][0] != CANONNET)
803149Seric 	{
813149Seric 		setstat(EX_USAGE);
823149Seric 		usrerr("cannot resolve name");
833149Seric 		return (NULL);
84297Seric 	}
85297Seric 
86297Seric 	/*
873149Seric 	**  Build canonical address from pvp.
88297Seric 	*/
89297Seric 
903149Seric 	a = buildaddr(pvp, a);
91*4279Seric 	if (a == NULL)
92*4279Seric 		return (NULL);
933149Seric 	m = Mailer[a->q_mailer];
94297Seric 
95297Seric 	/*
963149Seric 	**  Make local copies of the host & user and then
973149Seric 	**  transport them out.
98297Seric 	*/
99297Seric 
100297Seric 	if (copyf > 0)
1012973Seric 		a->q_paddr = newstr(addr);
102297Seric 	else
103297Seric 		a->q_paddr = addr;
104297Seric 
1053149Seric 	if (copyf >= 0)
106297Seric 	{
1073149Seric 		if (a->q_host != NULL)
1083149Seric 			a->q_host = newstr(a->q_host);
109297Seric 		else
1103149Seric 			a->q_host = "";
1113149Seric 		if (a->q_user != a->q_paddr)
1123149Seric 			a->q_user = newstr(a->q_user);
113297Seric 	}
114297Seric 
115297Seric 	/*
116297Seric 	**  Do UPPER->lower case mapping unless inhibited.
117297Seric 	*/
118297Seric 
1193149Seric 	if (!bitset(M_HST_UPPER, m->m_flags))
120297Seric 		makelower(a->q_host);
1213149Seric 	if (!bitset(M_USR_UPPER, m->m_flags))
122297Seric 		makelower(a->q_user);
123297Seric 
124297Seric 	/*
125297Seric 	**  Compute return value.
126297Seric 	*/
127297Seric 
128297Seric # ifdef DEBUG
1291583Seric 	if (Debug)
130297Seric 		printf("parse(\"%s\"): host \"%s\" user \"%s\" mailer %d\n",
1313149Seric 		    addr, a->q_host, a->q_user, a->q_mailer);
132297Seric # endif DEBUG
133297Seric 
134297Seric 	return (a);
135297Seric }
136297Seric /*
137297Seric **  PRESCAN -- Prescan name and make it canonical
138297Seric **
139297Seric **	Scans a name and turns it into canonical form.  This involves
140297Seric **	deleting blanks, comments (in parentheses), and turning the
141297Seric **	word "at" into an at-sign ("@").  The name is copied as this
142297Seric **	is done; it is legal to copy a name onto itself, since this
143297Seric **	process can only make things smaller.
144297Seric **
145297Seric **	This routine knows about quoted strings and angle brackets.
146297Seric **
147297Seric **	There are certain subtleties to this routine.  The one that
148297Seric **	comes to mind now is that backslashes on the ends of names
149297Seric **	are silently stripped off; this is intentional.  The problem
150297Seric **	is that some versions of sndmsg (like at LBL) set the kill
151297Seric **	character to something other than @ when reading addresses;
152297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
153297Seric **	berknet mailer.
154297Seric **
155297Seric **	Parameters:
156297Seric **		addr -- the name to chomp.
157297Seric **		delim -- the delimiter for the address, normally
158297Seric **			'\0' or ','; \0 is accepted in any case.
159297Seric **			are moving in place; set buflim to high core.
160297Seric **
161297Seric **	Returns:
1623149Seric **		A pointer to a vector of tokens.
163297Seric **		NULL on error.
164297Seric **
165297Seric **	Side Effects:
1663149Seric **		none.
167297Seric */
168297Seric 
1693149Seric # define OPER		1
1703149Seric # define ATOM		2
1713149Seric # define EOTOK		3
1723149Seric # define QSTRING	4
1733149Seric # define SPACE		5
1743149Seric # define DOLLAR		6
1753149Seric # define GETONE		7
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 space;
187297Seric 	bool bslashmode;
188297Seric 	int cmntcnt;
189297Seric 	int brccnt;
190297Seric 	register char c;
1913149Seric 	char *tok;
192297Seric 	register char *q;
1933149Seric 	register int state;
1943149Seric 	int nstate;
1954085Seric 	extern char lower();
196297Seric 
1972091Seric 	space = FALSE;
198297Seric 	q = buf;
1993149Seric 	bslashmode = FALSE;
200297Seric 	cmntcnt = brccnt = 0;
2013149Seric 	avp = av;
2023149Seric 	state = OPER;
2033149Seric 	for (p = addr; *p != '\0' && *p != delim; )
204297Seric 	{
2053149Seric 		/* read a token */
2063149Seric 		tok = q;
2073149Seric 		while ((c = *p++) != '\0' && c != delim)
208297Seric 		{
2093149Seric 			/* chew up special characters */
2104100Seric 			c &= ~0200;
2113149Seric 			*q = '\0';
2123149Seric 			if (bslashmode)
2133149Seric 			{
2143149Seric 				c |= 0200;
2153149Seric 				bslashmode = FALSE;
2163149Seric 			}
2173149Seric 			else if (c == '\\')
2183149Seric 			{
2193149Seric 				bslashmode = TRUE;
2203149Seric 				continue;
2213149Seric 			}
2224100Seric 			else if (c == '"')
2234100Seric 			{
2244100Seric 				if (state == QSTRING)
2254100Seric 					state = OPER;
2264100Seric 				else
2274100Seric 					state = QSTRING;
2284100Seric 				break;
2294100Seric 			}
2303149Seric 
2313149Seric 			nstate = toktype(c);
2323149Seric 			switch (state)
2333149Seric 			{
2343149Seric 			  case QSTRING:		/* in quoted string */
2353149Seric 				break;
2363149Seric 
2373149Seric 			  case ATOM:		/* regular atom */
2384228Seric 				/* state = nstate; */
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;
2583149Seric 				space = TRUE;
2594228Seric 				break;
2603149Seric 
2613149Seric 			  case OPER:		/* operator */
2623149Seric 				if (nstate == SPACE)
2633149Seric 					continue;
2643149Seric 				state = nstate;
2653149Seric 				break;
2663149Seric 
2673149Seric 			  case DOLLAR:		/* $- etc. */
2683149Seric 				state = OPER;
2693149Seric 				switch (c)
2703149Seric 				{
2713149Seric 				  case '$':		/* literal $ */
2723149Seric 					break;
2733149Seric 
2743149Seric 				  case '+':		/* match anything */
2753149Seric 					c = MATCHANY;
2763149Seric 					state = GETONE;
2773149Seric 					break;
2783149Seric 
2793149Seric 				  case '-':		/* match one token */
2803149Seric 					c = MATCHONE;
2813149Seric 					state = GETONE;
2823149Seric 					break;
2833149Seric 
2844060Seric 				  case '=':		/* match one token of class */
2854060Seric 					c = MATCHCLASS;
2864060Seric 					state = GETONE;
2874060Seric 					break;
2884060Seric 
2893149Seric 				  case '#':		/* canonical net name */
2903149Seric 					c = CANONNET;
2913149Seric 					break;
2923149Seric 
2933149Seric 				  case '@':		/* canonical host name */
2943149Seric 					c = CANONHOST;
2953149Seric 					break;
2963149Seric 
2973149Seric 				  case ':':		/* canonical user name */
2983149Seric 					c = CANONUSER;
2993149Seric 					break;
3003149Seric 
3013149Seric 				  default:
3023149Seric 					c = '$';
3033149Seric 					state = OPER;
3043149Seric 					p--;
3053149Seric 					break;
3063149Seric 				}
3073149Seric 				break;
3083149Seric 
3093149Seric 			  default:
3103149Seric 				syserr("prescan: unknown state %d", state);
3113149Seric 			}
3123149Seric 
3134228Seric 			if (state == EOTOK || state == SPACE)
3143149Seric 				break;
3153149Seric 			if (c == '$' && delim == '\t')
3163149Seric 			{
3173149Seric 				state = DOLLAR;
3183149Seric 				continue;
3193149Seric 			}
3203149Seric 
3213149Seric 			/* squirrel it away */
3223149Seric 			if (q >= &buf[sizeof buf - 5])
3233149Seric 			{
3243149Seric 				usrerr("Address too long");
3253149Seric 				return (NULL);
3263149Seric 			}
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 		}
3993149Seric 	}
4003149Seric 	*avp = NULL;
4013149Seric 	if (cmntcnt > 0)
4023149Seric 		usrerr("Unbalanced '('");
4033149Seric 	else if (brccnt > 0)
4043149Seric 		usrerr("Unbalanced '<'");
4053149Seric 	else if (state == QSTRING)
4063149Seric 		usrerr("Unbalanced '\"'");
4073149Seric 	else if (av[0] != NULL)
4083149Seric 		return (av);
4093149Seric 	return (NULL);
4103149Seric }
4113149Seric /*
4123149Seric **  TOKTYPE -- return token type
4133149Seric **
4143149Seric **	Parameters:
4153149Seric **		c -- the character in question.
4163149Seric **
4173149Seric **	Returns:
4183149Seric **		Its type.
4193149Seric **
4203149Seric **	Side Effects:
4213149Seric **		none.
4223149Seric */
423297Seric 
4243149Seric toktype(c)
4253149Seric 	register char c;
4263149Seric {
4273380Seric 	static char buf[50];
4283382Seric 	static bool firstime = TRUE;
4293380Seric 
4303382Seric 	if (firstime)
4313380Seric 	{
4323382Seric 		firstime = FALSE;
4334085Seric 		(void) expand("$o", buf, &buf[sizeof buf - 1]);
4343380Seric 		strcat(buf, DELIMCHARS);
4353380Seric 	}
4364100Seric 	if (!isascii(c))
4374100Seric 		return (ATOM);
4383149Seric 	if (isspace(c))
4393149Seric 		return (SPACE);
4403380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
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 
4674070Seric rewrite(pvp, ruleset)
4683149Seric 	char **pvp;
4694070Seric 	int ruleset;
4703149Seric {
4713149Seric 	register char *ap;		/* address pointer */
4723149Seric 	register char *rp;		/* rewrite pointer */
4733149Seric 	register char **avp;		/* address vector pointer */
4743149Seric 	register char **rvp;		/* rewrite vector pointer */
4753149Seric 	struct rewrite *rwr;
4763149Seric 	struct match mlist[MAXMATCH];
4773149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
4784060Seric 	extern bool sameword();
4793149Seric 
4804100Seric # ifdef DEBUG
4814228Seric 	if (Debug > 9)
4823149Seric 	{
4833149Seric 		printf("rewrite: original pvp:\n");
4843149Seric 		printav(pvp);
4853149Seric 	}
4864100Seric # endif DEBUG
4873149Seric 
4883149Seric 	/*
4893149Seric 	**  Run through the list of rewrite rules, applying
4903149Seric 	**	any that match.
4913149Seric 	*/
4923149Seric 
4934070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
4943149Seric 	{
4954100Seric # ifdef DEBUG
4964100Seric 		if (Debug > 10)
497297Seric 		{
4983149Seric 			printf("-----trying rule:\n");
4993149Seric 			printav(rwr->r_lhs);
5003149Seric 		}
5014100Seric # endif DEBUG
5023149Seric 
5033149Seric 		/* try to match on this rule */
5043149Seric 		clrmatch(mlist);
5053149Seric 		for (rvp = rwr->r_lhs, avp = pvp; *avp != NULL; )
5063149Seric 		{
5073149Seric 			ap = *avp;
5083149Seric 			rp = *rvp;
5093149Seric 
5103149Seric 			if (rp == NULL)
511297Seric 			{
5123149Seric 				/* end-of-pattern before end-of-address */
5133149Seric 				goto fail;
514297Seric 			}
5153149Seric 
5163149Seric 			switch (*rp)
5173149Seric 			{
5184060Seric 				register STAB *s;
5194060Seric 				register int class;
5204060Seric 
5213149Seric 			  case MATCHONE:
5223149Seric 				/* match exactly one token */
5233149Seric 				setmatch(mlist, rp[1], avp, avp);
5243149Seric 				break;
5253149Seric 
5263149Seric 			  case MATCHANY:
5273149Seric 				/* match any number of tokens */
5284085Seric 				setmatch(mlist, rp[1], (char **) NULL, avp);
5293149Seric 				break;
5303149Seric 
5314060Seric 			  case MATCHCLASS:
5324060Seric 				/* match any token in a class */
5334060Seric 				class = rp[1];
5344060Seric 				if (!isalpha(class))
5354060Seric 					goto fail;
5364060Seric 				if (isupper(class))
5374060Seric 					class -= 'A';
5384060Seric 				else
5394060Seric 					class -= 'a';
5404100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
5414060Seric 				if (s == NULL || (s->s_class & (1 << class)) == 0)
5424060Seric 					goto fail;
5434060Seric 				break;
5444060Seric 
5453149Seric 			  default:
5463149Seric 				/* must have exact match */
5474060Seric 				if (!sameword(rp, ap))
5484060Seric 					goto fail;
5493149Seric 				break;
5503149Seric 			}
5513149Seric 
5523149Seric 			/* successful match on this token */
5533149Seric 			avp++;
5543149Seric 			rvp++;
5553149Seric 			continue;
5563149Seric 
5573149Seric 		  fail:
5583149Seric 			/* match failed -- back up */
5593149Seric 			while (--rvp >= rwr->r_lhs)
5603149Seric 			{
5613149Seric 				rp = *rvp;
5623149Seric 				if (*rp == MATCHANY)
5633149Seric 					break;
5643149Seric 
5653149Seric 				/* can't extend match: back up everything */
5663149Seric 				avp--;
5673149Seric 
5683149Seric 				if (*rp == MATCHONE)
5693149Seric 				{
5703149Seric 					/* undo binding */
5714085Seric 					setmatch(mlist, rp[1], (char **) NULL, (char **) NULL);
5723149Seric 				}
5733149Seric 			}
5743149Seric 
5753149Seric 			if (rvp < rwr->r_lhs)
5763149Seric 			{
5773149Seric 				/* total failure to match */
5783149Seric 				break;
5793149Seric 			}
580297Seric 		}
5813149Seric 
5823149Seric 		/*
5833149Seric 		**  See if we successfully matched
5843149Seric 		*/
5853149Seric 
5863149Seric 		if (rvp >= rwr->r_lhs && *rvp == NULL)
5873149Seric 		{
5884100Seric # ifdef DEBUG
5894100Seric 			if (Debug > 10)
5903149Seric 			{
5913149Seric 				printf("-----rule matches:\n");
5923149Seric 				printav(rwr->r_rhs);
5933149Seric 			}
5944100Seric # endif DEBUG
5953149Seric 
5963149Seric 			/* substitute */
5973149Seric 			for (rvp = rwr->r_rhs, avp = npvp; *rvp != NULL; rvp++)
5983149Seric 			{
5993149Seric 				rp = *rvp;
6003149Seric 				if (*rp == MATCHANY)
6013149Seric 				{
6023149Seric 					register struct match *m;
6033149Seric 					register char **pp;
6043149Seric 					extern struct match *findmatch();
6053149Seric 
6063149Seric 					m = findmatch(mlist, rp[1]);
6073149Seric 					if (m != NULL)
6083149Seric 					{
6093149Seric 						pp = m->firsttok;
6103149Seric 						do
6113149Seric 						{
6123149Seric 							*avp++ = *pp;
6133149Seric 						} while (pp++ != m->lasttok);
6143149Seric 					}
6153149Seric 				}
6163149Seric 				else
6173149Seric 					*avp++ = rp;
6183149Seric 			}
6193149Seric 			*avp++ = NULL;
6204085Seric 			bmove((char *) npvp, (char *) pvp, (avp - npvp) * sizeof *avp);
6213149Seric # ifdef DEBUG
6224228Seric 			if (Debug > 3)
6233149Seric 			{
6243188Seric 				char **vp;
6253188Seric 
6263188Seric 				printf("rewritten as `");
6273188Seric 				for (vp = pvp; *vp != NULL; vp++)
6284228Seric 				{
6294228Seric 					if (vp != pvp)
6304228Seric 						printf("_");
6313188Seric 					xputs(*vp);
6324228Seric 				}
6333188Seric 				printf("'\n");
6343149Seric 			}
6353149Seric # endif DEBUG
6363149Seric 			if (pvp[0][0] == CANONNET)
6373149Seric 				break;
6383149Seric 		}
6393149Seric 		else
6403149Seric 		{
6414100Seric # ifdef DEBUG
6424100Seric 			if (Debug > 10)
6433149Seric 				printf("----- rule fails\n");
6444100Seric # endif DEBUG
6453149Seric 			rwr = rwr->r_next;
6463149Seric 		}
647297Seric 	}
6483149Seric }
6493149Seric /*
6503149Seric **  SETMATCH -- set parameter value in match vector
6513149Seric **
6523149Seric **	Parameters:
6533149Seric **		mlist -- list of match values.
6543149Seric **		name -- the character name of this parameter.
6553149Seric **		first -- the first location of the replacement.
6563149Seric **		last -- the last location of the replacement.
6573149Seric **
6583149Seric **		If last == NULL, delete this entry.
6593149Seric **		If first == NULL, extend this entry (or add it if
6603149Seric **			it does not exist).
6613149Seric **
6623149Seric **	Returns:
6633149Seric **		nothing.
6643149Seric **
6653149Seric **	Side Effects:
6663149Seric **		munges with mlist.
6673149Seric */
6683149Seric 
6693149Seric setmatch(mlist, name, first, last)
6703149Seric 	struct match *mlist;
6713149Seric 	char name;
6723149Seric 	char **first;
6733149Seric 	char **last;
6743149Seric {
6753149Seric 	register struct match *m;
6763149Seric 	struct match *nullm = NULL;
6773149Seric 
6783149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
6793149Seric 	{
6803149Seric 		if (m->name == name)
6813149Seric 			break;
6823149Seric 		if (m->name == '\0')
6833149Seric 			nullm = m;
6843149Seric 	}
6853149Seric 
6863149Seric 	if (m >= &mlist[MAXMATCH])
6873149Seric 		m = nullm;
6883149Seric 
6893149Seric 	if (last == NULL)
6903149Seric 	{
6913149Seric 		m->name = '\0';
6923149Seric 		return;
6933149Seric 	}
6943149Seric 
6953149Seric 	if (m->name == '\0')
6963149Seric 	{
6973149Seric 		if (first == NULL)
6983149Seric 			m->firsttok = last;
6993149Seric 		else
7003149Seric 			m->firsttok = first;
7013149Seric 	}
7023149Seric 	m->name = name;
7033149Seric 	m->lasttok = last;
7043149Seric }
7053149Seric /*
7063149Seric **  FINDMATCH -- find match in mlist
7073149Seric **
7083149Seric **	Parameters:
7093149Seric **		mlist -- list to search.
7103149Seric **		name -- name to find.
7113149Seric **
7123149Seric **	Returns:
7133149Seric **		pointer to match structure.
7143149Seric **		NULL if no match.
7153149Seric **
7163149Seric **	Side Effects:
7173149Seric **		none.
7183149Seric */
7193149Seric 
7203149Seric struct match *
7213149Seric findmatch(mlist, name)
7223149Seric 	struct match *mlist;
7233149Seric 	char name;
7243149Seric {
7253149Seric 	register struct match *m;
7263149Seric 
7273149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
7283149Seric 	{
7293149Seric 		if (m->name == name)
7303149Seric 			return (m);
7313149Seric 	}
7323149Seric 
733297Seric 	return (NULL);
734297Seric }
7353149Seric /*
7363149Seric **  CLRMATCH -- clear match list
7373149Seric **
7383149Seric **	Parameters:
7393149Seric **		mlist -- list to clear.
7403149Seric **
7413149Seric **	Returns:
7423149Seric **		none.
7433149Seric **
7443149Seric **	Side Effects:
7453149Seric **		mlist is cleared.
7463149Seric */
7473149Seric 
7483149Seric clrmatch(mlist)
7493149Seric 	struct match *mlist;
7503149Seric {
7513149Seric 	register struct match *m;
7523149Seric 
7533149Seric 	for (m = mlist; m < &mlist[MAXMATCH]; m++)
7543149Seric 		m->name = '\0';
7553149Seric }
7563149Seric /*
7573149Seric **  BUILDADDR -- build address from token vector.
7583149Seric **
7593149Seric **	Parameters:
7603149Seric **		tv -- token vector.
7613149Seric **		a -- pointer to address descriptor to fill.
7623149Seric **			If NULL, one will be allocated.
7633149Seric **
7643149Seric **	Returns:
765*4279Seric **		NULL if there was an error.
766*4279Seric **		'a' otherwise.
7673149Seric **
7683149Seric **	Side Effects:
7693149Seric **		fills in 'a'
7703149Seric */
7713149Seric 
7723149Seric ADDRESS *
7733149Seric buildaddr(tv, a)
7743149Seric 	register char **tv;
7753149Seric 	register ADDRESS *a;
7763149Seric {
7773149Seric 	register int i;
7783149Seric 	static char buf[MAXNAME];
7793149Seric 	struct mailer **mp;
7803149Seric 	register struct mailer *m;
7813149Seric 
7823149Seric 	if (a == NULL)
7833149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
7843188Seric 	a->q_flags = 0;
7854079Seric 	a->q_home = NULL;
7863149Seric 
7873149Seric 	/* figure out what net/mailer to use */
7883149Seric 	if (**tv != CANONNET)
789*4279Seric 	{
7903149Seric 		syserr("buildaddr: no net");
791*4279Seric 		return (NULL);
792*4279Seric 	}
7933149Seric 	tv++;
794*4279Seric 	if (strcmp(*tv, "error") == 0)
795*4279Seric 	{
796*4279Seric 		if (**++tv != CANONUSER)
797*4279Seric 			syserr("buildaddr: error: no user");
798*4279Seric 		buf[0] = '\0';
799*4279Seric 		while (*++tv != NULL)
800*4279Seric 		{
801*4279Seric 			if (buf[0] != '\0')
802*4279Seric 				strcat(buf, " ");
803*4279Seric 			strcat(buf, *tv);
804*4279Seric 		}
805*4279Seric 		usrerr(buf);
806*4279Seric 		return (NULL);
807*4279Seric 	}
8083154Seric 	for (mp = Mailer, i = 0; (m = *mp++) != NULL; i++)
8093149Seric 	{
8103149Seric 		if (strcmp(m->m_name, *tv) == 0)
8113149Seric 			break;
8123149Seric 	}
8133149Seric 	if (m == NULL)
814*4279Seric 	{
8153149Seric 		syserr("buildaddr: unknown net %s", *tv);
816*4279Seric 		return (NULL);
817*4279Seric 	}
8183149Seric 	a->q_mailer = i;
8193149Seric 
8203149Seric 	/* figure out what host (if any) */
8213149Seric 	tv++;
8224195Seric 	if (!bitset(M_LOCAL, m->m_flags))
8233149Seric 	{
8243149Seric 		if (**tv != CANONHOST)
825*4279Seric 		{
8263149Seric 			syserr("buildaddr: no host");
827*4279Seric 			return (NULL);
828*4279Seric 		}
8293149Seric 		tv++;
8303149Seric 		a->q_host = *tv;
8313149Seric 		tv++;
8323149Seric 	}
8333149Seric 	else
8343149Seric 		a->q_host = NULL;
8353149Seric 
8363149Seric 	/* figure out the user */
8373149Seric 	if (**tv != CANONUSER)
838*4279Seric 	{
8393149Seric 		syserr("buildaddr: no user");
840*4279Seric 		return (NULL);
841*4279Seric 	}
8424228Seric 	cataddr(++tv, buf, sizeof buf);
8433149Seric 	a->q_user = buf;
8443149Seric 
8453149Seric 	return (a);
8463149Seric }
8473188Seric /*
8484228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
8494228Seric **
8504228Seric **	Parameters:
8514228Seric **		pvp -- parameter vector to rebuild.
8524228Seric **		buf -- buffer to build the string into.
8534228Seric **		sz -- size of buf.
8544228Seric **
8554228Seric **	Returns:
8564228Seric **		none.
8574228Seric **
8584228Seric **	Side Effects:
8594228Seric **		Destroys buf.
8604228Seric */
8614228Seric 
8624228Seric cataddr(pvp, buf, sz)
8634228Seric 	char **pvp;
8644228Seric 	char *buf;
8654228Seric 	register int sz;
8664228Seric {
8674228Seric 	bool oatomtok = FALSE;
8684228Seric 	bool natomtok = FALSE;
8694228Seric 	register int i;
8704228Seric 	register char *p;
8714228Seric 
8724228Seric 	p = buf;
8734228Seric 	sz--;
8744228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
8754228Seric 	{
8764228Seric 		natomtok = (toktype(**pvp) == ATOM);
8774228Seric 		if (oatomtok && natomtok)
8784228Seric 			*p++ = SPACESUB;
8794228Seric 		(void) strcpy(p, *pvp);
8804228Seric 		oatomtok = natomtok;
8814228Seric 		p += i;
8824228Seric 		sz -= i;
8834228Seric 		pvp++;
8844228Seric 	}
8854228Seric 	*p = '\0';
8864228Seric }
8874228Seric /*
8883188Seric **  SAMEADDR -- Determine if two addresses are the same
8893188Seric **
8903188Seric **	This is not just a straight comparison -- if the mailer doesn't
8913188Seric **	care about the host we just ignore it, etc.
8923188Seric **
8933188Seric **	Parameters:
8943188Seric **		a, b -- pointers to the internal forms to compare.
8953188Seric **		wildflg -- if TRUE, 'a' may have no user specified,
8963188Seric **			in which case it is to match anything.
8973188Seric **
8983188Seric **	Returns:
8993188Seric **		TRUE -- they represent the same mailbox.
9003188Seric **		FALSE -- they don't.
9013188Seric **
9023188Seric **	Side Effects:
9033188Seric **		none.
9043188Seric */
9053188Seric 
9063188Seric bool
9073188Seric sameaddr(a, b, wildflg)
9083188Seric 	register ADDRESS *a;
9093188Seric 	register ADDRESS *b;
9103188Seric 	bool wildflg;
9113188Seric {
9123188Seric 	/* if they don't have the same mailer, forget it */
9133188Seric 	if (a->q_mailer != b->q_mailer)
9143188Seric 		return (FALSE);
9153188Seric 
9163188Seric 	/* if the user isn't the same, we can drop out */
9173188Seric 	if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0)
9183188Seric 		return (FALSE);
9193188Seric 
9203188Seric 	/* if the mailer ignores hosts, we have succeeded! */
9214195Seric 	if (bitset(M_LOCAL, Mailer[a->q_mailer]->m_flags))
9223188Seric 		return (TRUE);
9233188Seric 
9243188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
9253188Seric 	if (a->q_host == NULL || b->q_host == NULL)
9263188Seric 		return (FALSE);
9273188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
9283188Seric 		return (FALSE);
9293188Seric 
9303188Seric 	return (TRUE);
9313188Seric }
9323234Seric /*
9333234Seric **  PRINTADDR -- print address (for debugging)
9343234Seric **
9353234Seric **	Parameters:
9363234Seric **		a -- the address to print
9373234Seric **		follow -- follow the q_next chain.
9383234Seric **
9393234Seric **	Returns:
9403234Seric **		none.
9413234Seric **
9423234Seric **	Side Effects:
9433234Seric **		none.
9443234Seric */
9453234Seric 
9463234Seric printaddr(a, follow)
9473234Seric 	register ADDRESS *a;
9483234Seric 	bool follow;
9493234Seric {
9503234Seric 	while (a != NULL)
9513234Seric 	{
9523234Seric 		printf("addr@%x: ", a);
9534085Seric 		(void) fflush(stdout);
9543234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
9553234Seric 		       a->q_mailer, Mailer[a->q_mailer]->m_name, a->q_host, a->q_user);
9563234Seric 		printf("\tnext=%x flags=%o, rmailer %d\n", a->q_next,
9573234Seric 		       a->q_flags, a->q_rmailer);
9583234Seric 
9593234Seric 		if (!follow)
9603234Seric 			return;
9613234Seric 		a = a->q_next;
9623234Seric 	}
9633234Seric }
964