13312Seric # include "sendmail.h"
2297Seric 
3*7800Seric SCCSID(@(#)parseaddr.c	3.47		08/21/82);
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 
586903Seric 	CurEnv->e_to = addr;
593188Seric # ifdef DEBUG
607675Seric 	if (tTd(20, 1))
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);
924598Seric 	m = 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
1287675Seric 	if (tTd(20, 1))
1294443Seric 	{
1304443Seric 		printf("parse-->");
1314443Seric 		printaddr(a, FALSE);
1324443Seric 	}
133297Seric # endif DEBUG
134297Seric 
135297Seric 	return (a);
136297Seric }
137297Seric /*
138297Seric **  PRESCAN -- Prescan name and make it canonical
139297Seric **
140297Seric **	Scans a name and turns it into canonical form.  This involves
141297Seric **	deleting blanks, comments (in parentheses), and turning the
142297Seric **	word "at" into an at-sign ("@").  The name is copied as this
143297Seric **	is done; it is legal to copy a name onto itself, since this
144297Seric **	process can only make things smaller.
145297Seric **
146297Seric **	This routine knows about quoted strings and angle brackets.
147297Seric **
148297Seric **	There are certain subtleties to this routine.  The one that
149297Seric **	comes to mind now is that backslashes on the ends of names
150297Seric **	are silently stripped off; this is intentional.  The problem
151297Seric **	is that some versions of sndmsg (like at LBL) set the kill
152297Seric **	character to something other than @ when reading addresses;
153297Seric **	so people type "csvax.eric\@berkeley" -- which screws up the
154297Seric **	berknet mailer.
155297Seric **
156297Seric **	Parameters:
157297Seric **		addr -- the name to chomp.
158297Seric **		delim -- the delimiter for the address, normally
159297Seric **			'\0' or ','; \0 is accepted in any case.
160297Seric **			are moving in place; set buflim to high core.
161297Seric **
162297Seric **	Returns:
1633149Seric **		A pointer to a vector of tokens.
164297Seric **		NULL on error.
165297Seric **
166297Seric **	Side Effects:
1673149Seric **		none.
168297Seric */
169297Seric 
1703149Seric # define OPER		1
1713149Seric # define ATOM		2
1723149Seric # define EOTOK		3
1733149Seric # define QSTRING	4
1743149Seric # define SPACE		5
1756053Seric # define ONEMORE	6
1763149Seric # define GETONE		7
1774424Seric # define MACRO		8
1783149Seric 
1793149Seric char **
1803149Seric prescan(addr, delim)
181297Seric 	char *addr;
182297Seric 	char delim;
183297Seric {
184297Seric 	register char *p;
1853149Seric 	static char buf[MAXNAME+MAXATOM];
1863149Seric 	static char *av[MAXATOM+1];
1873149Seric 	char **avp;
188297Seric 	bool bslashmode;
189297Seric 	int cmntcnt;
190297Seric 	register char c;
1913149Seric 	char *tok;
192297Seric 	register char *q;
1933149Seric 	register int state;
1943149Seric 	int nstate;
1954085Seric 	extern char lower();
196297Seric 
197297Seric 	q = buf;
1983149Seric 	bslashmode = FALSE;
199*7800Seric 	cmntcnt = 0;
2003149Seric 	avp = av;
2013149Seric 	state = OPER;
2023149Seric 	for (p = addr; *p != '\0' && *p != delim; )
203297Seric 	{
2043149Seric 		/* read a token */
2053149Seric 		tok = q;
2063149Seric 		while ((c = *p++) != '\0' && c != delim)
207297Seric 		{
2083149Seric 			/* chew up special characters */
2094100Seric 			c &= ~0200;
2103149Seric 			*q = '\0';
2113149Seric 			if (bslashmode)
2123149Seric 			{
2133149Seric 				c |= 0200;
2143149Seric 				bslashmode = FALSE;
2153149Seric 			}
2163149Seric 			else if (c == '\\')
2173149Seric 			{
2183149Seric 				bslashmode = TRUE;
2193149Seric 				continue;
2203149Seric 			}
2214100Seric 			else if (c == '"')
2224100Seric 			{
2234100Seric 				if (state == QSTRING)
2244100Seric 					state = OPER;
2254100Seric 				else
2264100Seric 					state = QSTRING;
2274100Seric 				break;
2284100Seric 			}
2293149Seric 
2306053Seric 			nstate = toktype(c);
2313149Seric 			switch (state)
2323149Seric 			{
2333149Seric 			  case QSTRING:		/* in quoted string */
2343149Seric 				break;
2353149Seric 
2363149Seric 			  case ATOM:		/* regular atom */
2374228Seric 				if (nstate != ATOM)
2383149Seric 				{
2393149Seric 					state = EOTOK;
2403149Seric 					p--;
2413149Seric 				}
2423149Seric 				break;
2433149Seric 
2443149Seric 			  case GETONE:		/* grab one character */
2453149Seric 				state = OPER;
2463149Seric 				break;
2473149Seric 
2483149Seric 			  case EOTOK:		/* after atom or q-string */
2493149Seric 				state = nstate;
2503149Seric 				if (state == SPACE)
2513149Seric 					continue;
2523149Seric 				break;
2533149Seric 
2543149Seric 			  case SPACE:		/* linear white space */
2553149Seric 				state = nstate;
2564228Seric 				break;
2573149Seric 
2583149Seric 			  case OPER:		/* operator */
2593149Seric 				if (nstate == SPACE)
2603149Seric 					continue;
2613149Seric 				state = nstate;
2623149Seric 				break;
2633149Seric 
2646053Seric 			  case ONEMORE:		/* $- etc. */
2656053Seric 				state = GETONE;
2663149Seric 				break;
2673149Seric 
2683149Seric 			  default:
2693149Seric 				syserr("prescan: unknown state %d", state);
2703149Seric 			}
2713149Seric 
2724228Seric 			if (state == EOTOK || state == SPACE)
2733149Seric 				break;
2743149Seric 
2753149Seric 			/* squirrel it away */
2763149Seric 			if (q >= &buf[sizeof buf - 5])
2773149Seric 			{
2783149Seric 				usrerr("Address too long");
2793149Seric 				return (NULL);
2803149Seric 			}
2813149Seric 			*q++ = c;
2823149Seric 
2833149Seric 			/* decide whether this represents end of token */
2846053Seric 			if (state == OPER || state == GETONE)
2853149Seric 				break;
286297Seric 		}
2873149Seric 		if (c == '\0' || c == delim)
2883149Seric 			p--;
2893149Seric 
2903149Seric 		/* new token */
2913149Seric 		if (tok == q)
292297Seric 			continue;
2933149Seric 		*q++ = '\0';
2943149Seric 
2953149Seric 		c = tok[0];
2963149Seric 		if (c == '(')
2971378Seric 		{
298297Seric 			cmntcnt++;
2991378Seric 			continue;
3001378Seric 		}
301297Seric 		else if (c == ')')
302297Seric 		{
303297Seric 			if (cmntcnt <= 0)
304297Seric 			{
305297Seric 				usrerr("Unbalanced ')'");
306297Seric 				return (NULL);
307297Seric 			}
308297Seric 			else
309297Seric 			{
310297Seric 				cmntcnt--;
311297Seric 				continue;
312297Seric 			}
313297Seric 		}
3143149Seric 		else if (cmntcnt > 0)
3152091Seric 			continue;
3163149Seric 
3174448Seric 		if (avp >= &av[MAXATOM])
3184448Seric 		{
3194448Seric 			syserr("prescan: too many tokens");
3204448Seric 			return (NULL);
3214448Seric 		}
3224448Seric 		*avp++ = tok;
3233149Seric 	}
3243149Seric 	*avp = NULL;
3253149Seric 	if (cmntcnt > 0)
3263149Seric 		usrerr("Unbalanced '('");
3273149Seric 	else if (state == QSTRING)
3283149Seric 		usrerr("Unbalanced '\"'");
3293149Seric 	else if (av[0] != NULL)
3303149Seric 		return (av);
3313149Seric 	return (NULL);
3323149Seric }
3333149Seric /*
3343149Seric **  TOKTYPE -- return token type
3353149Seric **
3363149Seric **	Parameters:
3373149Seric **		c -- the character in question.
3383149Seric **
3393149Seric **	Returns:
3403149Seric **		Its type.
3413149Seric **
3423149Seric **	Side Effects:
3433149Seric **		none.
3443149Seric */
345297Seric 
3463149Seric toktype(c)
3473149Seric 	register char c;
3483149Seric {
3493380Seric 	static char buf[50];
3503382Seric 	static bool firstime = TRUE;
3513380Seric 
3523382Seric 	if (firstime)
3533380Seric 	{
3543382Seric 		firstime = FALSE;
3556977Seric 		expand("$o", buf, &buf[sizeof buf - 1], CurEnv);
3567005Seric 		(void) strcat(buf, DELIMCHARS);
3573380Seric 	}
3586053Seric 	if (c == MATCHCLASS || c == MATCHREPL)
3596053Seric 		return (ONEMORE);
3604100Seric 	if (!isascii(c))
3614100Seric 		return (ATOM);
3623149Seric 	if (isspace(c))
3633149Seric 		return (SPACE);
3643380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
3653149Seric 		return (OPER);
3663149Seric 	return (ATOM);
3673149Seric }
3683149Seric /*
3693149Seric **  REWRITE -- apply rewrite rules to token vector.
3703149Seric **
3714476Seric **	This routine is an ordered production system.  Each rewrite
3724476Seric **	rule has a LHS (called the pattern) and a RHS (called the
3734476Seric **	rewrite); 'rwr' points the the current rewrite rule.
3744476Seric **
3754476Seric **	For each rewrite rule, 'avp' points the address vector we
3764476Seric **	are trying to match against, and 'pvp' points to the pattern.
3774476Seric **	If pvp points to a special match value (MATCHANY, MATCHONE,
3784476Seric **	MATCHCLASS) then the address in avp matched is saved away
3794476Seric **	in the match vector (pointed to by 'mvp').
3804476Seric **
3814476Seric **	When a match between avp & pvp does not match, we try to
3824476Seric **	back out.  If we back up over a MATCHONE or a MATCHCLASS
3834476Seric **	we must also back out the match in mvp.  If we reach a
3844476Seric **	MATCHANY we just extend the match and start over again.
3854476Seric **
3864476Seric **	When we finally match, we rewrite the address vector
3874476Seric **	and try over again.
3884476Seric **
3893149Seric **	Parameters:
3903149Seric **		pvp -- pointer to token vector.
3913149Seric **
3923149Seric **	Returns:
3933149Seric **		none.
3943149Seric **
3953149Seric **	Side Effects:
3963149Seric **		pvp is modified.
3973149Seric */
3982091Seric 
3993149Seric struct match
4003149Seric {
4014468Seric 	char	**first;	/* first token matched */
4024468Seric 	char	**last;		/* last token matched */
4033149Seric };
4043149Seric 
4054468Seric # define MAXMATCH	9	/* max params per rewrite */
4063149Seric 
4073149Seric 
4084070Seric rewrite(pvp, ruleset)
4093149Seric 	char **pvp;
4104070Seric 	int ruleset;
4113149Seric {
4123149Seric 	register char *ap;		/* address pointer */
4133149Seric 	register char *rp;		/* rewrite pointer */
4143149Seric 	register char **avp;		/* address vector pointer */
4153149Seric 	register char **rvp;		/* rewrite vector pointer */
4164468Seric 	struct rewrite *rwr;		/* pointer to current rewrite rule */
4174468Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
4184468Seric 	struct match *mlp;		/* cur ptr into mlist */
4193149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
4204060Seric 	extern bool sameword();
4213149Seric 
4224100Seric # ifdef DEBUG
4237675Seric 	if (tTd(21, 9))
4243149Seric 	{
4253149Seric 		printf("rewrite: original pvp:\n");
4263149Seric 		printav(pvp);
4273149Seric 	}
4284100Seric # endif DEBUG
4293149Seric 
4303149Seric 	/*
4313149Seric 	**  Run through the list of rewrite rules, applying
4323149Seric 	**	any that match.
4333149Seric 	*/
4343149Seric 
4354070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
4363149Seric 	{
4374100Seric # ifdef DEBUG
4387675Seric 		if (tTd(21, 12))
439297Seric 		{
4403149Seric 			printf("-----trying rule:\n");
4413149Seric 			printav(rwr->r_lhs);
4423149Seric 		}
4434100Seric # endif DEBUG
4443149Seric 
4453149Seric 		/* try to match on this rule */
4464468Seric 		mlp = mlist;
4474476Seric 		for (rvp = rwr->r_lhs, avp = pvp; *avp != NULL; )
4483149Seric 		{
4493149Seric 			ap = *avp;
4503149Seric 			rp = *rvp;
4513149Seric 
4523149Seric 			if (rp == NULL)
453297Seric 			{
4543149Seric 				/* end-of-pattern before end-of-address */
4553149Seric 				goto fail;
456297Seric 			}
4573149Seric 
4583149Seric 			switch (*rp)
4593149Seric 			{
4604060Seric 				register STAB *s;
4614060Seric 				register int class;
4624060Seric 
4634060Seric 			  case MATCHCLASS:
4644060Seric 				/* match any token in a class */
4654060Seric 				class = rp[1];
4664060Seric 				if (!isalpha(class))
4674060Seric 					goto fail;
4684060Seric 				if (isupper(class))
4694060Seric 					class -= 'A';
4704060Seric 				else
4714060Seric 					class -= 'a';
4724100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
4736273Seric 				if (s == NULL || (s->s_class & (1L << class)) == 0)
4744060Seric 					goto fail;
4754468Seric 
4764476Seric 				/* explicit fall-through */
4774476Seric 
4784476Seric 			  case MATCHONE:
4794476Seric 			  case MATCHANY:
4804476Seric 				/* match exactly one token */
4814468Seric 				mlp->first = mlp->last = avp++;
4824468Seric 				mlp++;
4834060Seric 				break;
4844060Seric 
4853149Seric 			  default:
4863149Seric 				/* must have exact match */
4874060Seric 				if (!sameword(rp, ap))
4884060Seric 					goto fail;
4894468Seric 				avp++;
4903149Seric 				break;
4913149Seric 			}
4923149Seric 
4933149Seric 			/* successful match on this token */
4943149Seric 			rvp++;
4953149Seric 			continue;
4963149Seric 
4973149Seric 		  fail:
4983149Seric 			/* match failed -- back up */
4993149Seric 			while (--rvp >= rwr->r_lhs)
5003149Seric 			{
5013149Seric 				rp = *rvp;
5023149Seric 				if (*rp == MATCHANY)
5034468Seric 				{
5044476Seric 					/* extend binding and continue */
5054476Seric 					mlp[-1].last = avp++;
5064476Seric 					rvp++;
5073149Seric 					break;
5084468Seric 				}
5094476Seric 				avp--;
5104476Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS)
5113149Seric 				{
5124468Seric 					/* back out binding */
5134468Seric 					mlp--;
5143149Seric 				}
5153149Seric 			}
5163149Seric 
5173149Seric 			if (rvp < rwr->r_lhs)
5183149Seric 			{
5193149Seric 				/* total failure to match */
5203149Seric 				break;
5213149Seric 			}
522297Seric 		}
5233149Seric 
5243149Seric 		/*
5253149Seric 		**  See if we successfully matched
5263149Seric 		*/
5273149Seric 
5283149Seric 		if (rvp >= rwr->r_lhs && *rvp == NULL)
5293149Seric 		{
5304100Seric # ifdef DEBUG
5317675Seric 			if (tTd(21, 12))
5323149Seric 			{
5333149Seric 				printf("-----rule matches:\n");
5343149Seric 				printav(rwr->r_rhs);
5353149Seric 			}
5364100Seric # endif DEBUG
5373149Seric 
5383149Seric 			/* substitute */
5393149Seric 			for (rvp = rwr->r_rhs, avp = npvp; *rvp != NULL; rvp++)
5403149Seric 			{
5413149Seric 				rp = *rvp;
5424468Seric 				if (*rp == MATCHREPL)
5433149Seric 				{
5443149Seric 					register struct match *m;
5453149Seric 					register char **pp;
5463149Seric 
5474468Seric 					m = &mlist[rp[1] - '1'];
5484476Seric # ifdef DEBUG
5497675Seric 					if (tTd(21, 15))
5504476Seric 					{
5514476Seric 						printf("$%c:", rp[1]);
5524476Seric 						pp = m->first;
5534476Seric 						do
5544476Seric 						{
5554476Seric 							printf(" %x=\"", *pp);
5564625Seric 							(void) fflush(stdout);
5574476Seric 							printf("%s\"", *pp);
5584476Seric 						} while (pp++ != m->last);
5594476Seric 						printf("\n");
5604476Seric 					}
5614476Seric # endif DEBUG
5624468Seric 					pp = m->first;
5634468Seric 					do
5643149Seric 					{
5654468Seric 						if (avp >= &npvp[MAXATOM])
5663149Seric 						{
5674468Seric 							syserr("rewrite: expansion too long");
5684468Seric 							return;
5694468Seric 						}
5704468Seric 						*avp++ = *pp;
5714468Seric 					} while (pp++ != m->last);
5723149Seric 				}
5733149Seric 				else
5744385Seric 				{
5754385Seric 					if (avp >= &npvp[MAXATOM])
5764385Seric 					{
5774385Seric 						syserr("rewrite: expansion too long");
5784385Seric 						return;
5794385Seric 					}
5803149Seric 					*avp++ = rp;
5814385Seric 				}
5823149Seric 			}
5833149Seric 			*avp++ = NULL;
5844085Seric 			bmove((char *) npvp, (char *) pvp, (avp - npvp) * sizeof *avp);
5853149Seric # ifdef DEBUG
5867675Seric 			if (tTd(21, 4))
5873149Seric 			{
5883188Seric 				char **vp;
5893188Seric 
5903188Seric 				printf("rewritten as `");
5913188Seric 				for (vp = pvp; *vp != NULL; vp++)
5924228Seric 				{
5934228Seric 					if (vp != pvp)
5944228Seric 						printf("_");
5953188Seric 					xputs(*vp);
5964228Seric 				}
5973188Seric 				printf("'\n");
5983149Seric 			}
5993149Seric # endif DEBUG
6003149Seric 			if (pvp[0][0] == CANONNET)
6013149Seric 				break;
6023149Seric 		}
6033149Seric 		else
6043149Seric 		{
6054100Seric # ifdef DEBUG
6067675Seric 			if (tTd(21, 10))
6073149Seric 				printf("----- rule fails\n");
6084100Seric # endif DEBUG
6093149Seric 			rwr = rwr->r_next;
6103149Seric 		}
611297Seric 	}
6123149Seric }
6133149Seric /*
6143149Seric **  BUILDADDR -- build address from token vector.
6153149Seric **
6163149Seric **	Parameters:
6173149Seric **		tv -- token vector.
6183149Seric **		a -- pointer to address descriptor to fill.
6193149Seric **			If NULL, one will be allocated.
6203149Seric **
6213149Seric **	Returns:
6224279Seric **		NULL if there was an error.
6234279Seric **		'a' otherwise.
6243149Seric **
6253149Seric **	Side Effects:
6263149Seric **		fills in 'a'
6273149Seric */
6283149Seric 
6293149Seric ADDRESS *
6303149Seric buildaddr(tv, a)
6313149Seric 	register char **tv;
6323149Seric 	register ADDRESS *a;
6333149Seric {
6343149Seric 	static char buf[MAXNAME];
6353149Seric 	struct mailer **mp;
6363149Seric 	register struct mailer *m;
6374635Seric 	extern bool sameword();
6383149Seric 
6393149Seric 	if (a == NULL)
6403149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
6414988Seric 	clear((char *) a, sizeof *a);
6423149Seric 
6433149Seric 	/* figure out what net/mailer to use */
6443149Seric 	if (**tv != CANONNET)
6454279Seric 	{
6463149Seric 		syserr("buildaddr: no net");
6474279Seric 		return (NULL);
6484279Seric 	}
6493149Seric 	tv++;
6504635Seric 	if (sameword(*tv, "error"))
6514279Seric 	{
6524279Seric 		if (**++tv != CANONUSER)
6534279Seric 			syserr("buildaddr: error: no user");
6544279Seric 		buf[0] = '\0';
6554279Seric 		while (*++tv != NULL)
6564279Seric 		{
6574279Seric 			if (buf[0] != '\0')
6587005Seric 				(void) strcat(buf, " ");
6597005Seric 			(void) strcat(buf, *tv);
6604279Seric 		}
6614279Seric 		usrerr(buf);
6624279Seric 		return (NULL);
6634279Seric 	}
6644598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
6653149Seric 	{
6664635Seric 		if (sameword(m->m_name, *tv))
6673149Seric 			break;
6683149Seric 	}
6693149Seric 	if (m == NULL)
6704279Seric 	{
6713149Seric 		syserr("buildaddr: unknown net %s", *tv);
6724279Seric 		return (NULL);
6734279Seric 	}
6744598Seric 	a->q_mailer = m;
6753149Seric 
6763149Seric 	/* figure out what host (if any) */
6773149Seric 	tv++;
6784195Seric 	if (!bitset(M_LOCAL, m->m_flags))
6793149Seric 	{
6805704Seric 		if (**tv++ != CANONHOST)
6814279Seric 		{
6823149Seric 			syserr("buildaddr: no host");
6834279Seric 			return (NULL);
6844279Seric 		}
6855704Seric 		buf[0] = '\0';
6865704Seric 		while (*tv != NULL && **tv != CANONUSER)
6877005Seric 			(void) strcat(buf, *tv++);
6885704Seric 		a->q_host = newstr(buf);
6893149Seric 	}
6903149Seric 	else
6913149Seric 		a->q_host = NULL;
6923149Seric 
6933149Seric 	/* figure out the user */
6943149Seric 	if (**tv != CANONUSER)
6954279Seric 	{
6963149Seric 		syserr("buildaddr: no user");
6974279Seric 		return (NULL);
6984279Seric 	}
6994228Seric 	cataddr(++tv, buf, sizeof buf);
7003149Seric 	a->q_user = buf;
7013149Seric 
7023149Seric 	return (a);
7033149Seric }
7043188Seric /*
7054228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
7064228Seric **
7074228Seric **	Parameters:
7084228Seric **		pvp -- parameter vector to rebuild.
7094228Seric **		buf -- buffer to build the string into.
7104228Seric **		sz -- size of buf.
7114228Seric **
7124228Seric **	Returns:
7134228Seric **		none.
7144228Seric **
7154228Seric **	Side Effects:
7164228Seric **		Destroys buf.
7174228Seric */
7184228Seric 
7194228Seric cataddr(pvp, buf, sz)
7204228Seric 	char **pvp;
7214228Seric 	char *buf;
7224228Seric 	register int sz;
7234228Seric {
7244228Seric 	bool oatomtok = FALSE;
7254228Seric 	bool natomtok = FALSE;
7264228Seric 	register int i;
7274228Seric 	register char *p;
7284228Seric 
7294228Seric 	p = buf;
7304228Seric 	sz--;
7314228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
7324228Seric 	{
7334228Seric 		natomtok = (toktype(**pvp) == ATOM);
7344228Seric 		if (oatomtok && natomtok)
7354228Seric 			*p++ = SPACESUB;
7364228Seric 		(void) strcpy(p, *pvp);
7374228Seric 		oatomtok = natomtok;
7384228Seric 		p += i;
7394228Seric 		sz -= i;
7404228Seric 		pvp++;
7414228Seric 	}
7424228Seric 	*p = '\0';
7434228Seric }
7444228Seric /*
7453188Seric **  SAMEADDR -- Determine if two addresses are the same
7463188Seric **
7473188Seric **	This is not just a straight comparison -- if the mailer doesn't
7483188Seric **	care about the host we just ignore it, etc.
7493188Seric **
7503188Seric **	Parameters:
7513188Seric **		a, b -- pointers to the internal forms to compare.
7523188Seric **		wildflg -- if TRUE, 'a' may have no user specified,
7533188Seric **			in which case it is to match anything.
7543188Seric **
7553188Seric **	Returns:
7563188Seric **		TRUE -- they represent the same mailbox.
7573188Seric **		FALSE -- they don't.
7583188Seric **
7593188Seric **	Side Effects:
7603188Seric **		none.
7613188Seric */
7623188Seric 
7633188Seric bool
7643188Seric sameaddr(a, b, wildflg)
7653188Seric 	register ADDRESS *a;
7663188Seric 	register ADDRESS *b;
7673188Seric 	bool wildflg;
7683188Seric {
7693188Seric 	/* if they don't have the same mailer, forget it */
7703188Seric 	if (a->q_mailer != b->q_mailer)
7713188Seric 		return (FALSE);
7723188Seric 
7733188Seric 	/* if the user isn't the same, we can drop out */
7743188Seric 	if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0)
7753188Seric 		return (FALSE);
7763188Seric 
7773188Seric 	/* if the mailer ignores hosts, we have succeeded! */
7784598Seric 	if (bitset(M_LOCAL, a->q_mailer->m_flags))
7793188Seric 		return (TRUE);
7803188Seric 
7813188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
7823188Seric 	if (a->q_host == NULL || b->q_host == NULL)
7833188Seric 		return (FALSE);
7843188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
7853188Seric 		return (FALSE);
7863188Seric 
7873188Seric 	return (TRUE);
7883188Seric }
7893234Seric /*
7903234Seric **  PRINTADDR -- print address (for debugging)
7913234Seric **
7923234Seric **	Parameters:
7933234Seric **		a -- the address to print
7943234Seric **		follow -- follow the q_next chain.
7953234Seric **
7963234Seric **	Returns:
7973234Seric **		none.
7983234Seric **
7993234Seric **	Side Effects:
8003234Seric **		none.
8013234Seric */
8023234Seric 
8034317Seric # ifdef DEBUG
8044317Seric 
8053234Seric printaddr(a, follow)
8063234Seric 	register ADDRESS *a;
8073234Seric 	bool follow;
8083234Seric {
8095001Seric 	bool first = TRUE;
8105001Seric 
8113234Seric 	while (a != NULL)
8123234Seric 	{
8135001Seric 		first = FALSE;
8144443Seric 		printf("%x=", a);
8154085Seric 		(void) fflush(stdout);
8163234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
8174598Seric 		       a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, a->q_user);
8185035Seric 		printf("\tnext=%x, flags=%o, rmailer %d, alias %x\n", a->q_next,
8195035Seric 		       a->q_flags, a->q_rmailer, a->q_alias);
8205001Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, a->q_fullname);
8214996Seric 
8223234Seric 		if (!follow)
8233234Seric 			return;
8244996Seric 		a = a->q_next;
8253234Seric 	}
8265001Seric 	if (first)
8274443Seric 		printf("[NULL]\n");
8283234Seric }
8294317Seric 
8304317Seric # endif DEBUG
8317682Seric /*
8327682Seric **  REMOTENAME -- return the name relative to the current mailer
8337682Seric **
8347682Seric **	Parameters:
8357682Seric **		name -- the name to translate.
8367682Seric **		force -- if set, forces rewriting even if the mailer
8377682Seric **			does not request it.  Used for rewriting
8387682Seric **			sender addresses.
8397682Seric **
8407682Seric **	Returns:
8417682Seric **		the text string representing this address relative to
8427682Seric **			the receiving mailer.
8437682Seric **
8447682Seric **	Side Effects:
8457682Seric **		none.
8467682Seric **
8477682Seric **	Warnings:
8487682Seric **		The text string returned is tucked away locally;
8497682Seric **			copy it if you intend to save it.
8507682Seric */
8517682Seric 
8527682Seric char *
8537682Seric remotename(name, m, force)
8547682Seric 	char *name;
8557682Seric 	struct mailer *m;
8567682Seric 	bool force;
8577682Seric {
8587682Seric 	static char buf[MAXNAME];
8597682Seric 	char lbuf[MAXNAME];
8607682Seric 	extern char *macvalue();
8617682Seric 	char *oldf = macvalue('f');
8627682Seric 	char *oldx = macvalue('x');
8637682Seric 	char *oldg = macvalue('g');
8647682Seric 	extern char **prescan();
8657682Seric 	register char **pvp;
8667682Seric 	extern char *getxpart();
8677682Seric 	extern ADDRESS *buildaddr();
8687682Seric 
8697755Seric # ifdef DEBUG
8707755Seric 	if (tTd(12, 1))
8717755Seric 		printf("remotename(%s)\n", name);
8727755Seric # endif DEBUG
8737755Seric 
8747682Seric 	/*
8757682Seric 	**  See if this mailer wants the name to be rewritten.  There are
8767682Seric 	**  many problems here, owing to the standards for doing replies.
8777682Seric 	**  In general, these names should only be rewritten if we are
8787682Seric 	**  sending to another host that runs sendmail.
8797682Seric 	*/
8807682Seric 
8817682Seric 	if (!bitset(M_RELRCPT, m->m_flags) && !force)
8827755Seric 	{
8837755Seric # ifdef DEBUG
8847755Seric 		if (tTd(12, 1))
8857755Seric 			printf("remotename [ditto]\n");
8867755Seric # endif DEBUG
8877682Seric 		return (name);
8887755Seric 	}
8897682Seric 
8907682Seric 	/*
8917682Seric 	**  Do general rewriting of name.
8927682Seric 	**	This will also take care of doing global name translation.
8937682Seric 	*/
8947682Seric 
8957682Seric 	define('x', getxpart(name));
8967682Seric 	pvp = prescan(name, '\0');
8977682Seric 	if (pvp == NULL)
8987682Seric 		return (name);
8997682Seric 	rewrite(pvp, 1);
9007682Seric 	rewrite(pvp, 3);
9017682Seric 	if (**pvp == CANONNET)
9027682Seric 	{
9037682Seric 		/* oops... resolved to something */
9047682Seric 		return (name);
9057682Seric 	}
9067682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
9077682Seric 
9087682Seric 	/* make the name relative to the receiving mailer */
9097682Seric 	define('f', lbuf);
9107682Seric 	expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
9117682Seric 
9127682Seric 	/* rewrite to get rid of garbage we added in the expand above */
9137682Seric 	pvp = prescan(buf, '\0');
9147682Seric 	if (pvp == NULL)
9157682Seric 		return (name);
9167682Seric 	rewrite(pvp, 2);
9177682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
9187682Seric 
9197682Seric 	/* now add any comment info we had before back */
9207682Seric 	define('g', lbuf);
9217682Seric 	expand("$q", buf, &buf[sizeof buf - 1], CurEnv);
9227682Seric 
9237682Seric 	define('f', oldf);
9247682Seric 	define('g', oldg);
9257682Seric 	define('x', oldx);
9267682Seric 
9277682Seric # ifdef DEBUG
9287682Seric 	if (tTd(12, 1))
9297755Seric 		printf("remotename => `%s'\n", buf);
9307682Seric # endif DEBUG
9317682Seric 	return (buf);
9327682Seric }
9337682Seric /*
9347682Seric **  CANONNAME -- make name canonical
9357682Seric **
9367682Seric **	This is used for SMTP and misc. printing.  Given a print
9377682Seric **	address, it strips out comments, etc., and puts on exactly
9387682Seric **	one set of brackets.
9397682Seric **
9407682Seric **	Parameters:
9417682Seric **		name -- the name to make canonical.
9427682Seric **
9437682Seric **	Returns:
9447682Seric **		pointer to canonical name.
9457682Seric **
9467682Seric **	Side Effects:
9477682Seric **		none.
9487682Seric **
9497682Seric **	Warning:
9507682Seric **		result is saved in static buf; future calls will trash it.
9517682Seric */
9527682Seric 
9537682Seric char *
9547682Seric canonname(name)
9557682Seric 	char *name;
9567682Seric {
9577682Seric 	static char nbuf[MAXNAME + 2];
9587682Seric 
9597682Seric 	if (name[0] == '<')
9607682Seric 		return (name);
9617682Seric 	strcpy(nbuf, "<");
9627682Seric 	strcat(nbuf, name);
9637682Seric 	strcat(nbuf, ">");
9647682Seric 	return (nbuf);
9657682Seric }
966