13312Seric # include "sendmail.h"
2297Seric 
3*7940Seric SCCSID(@(#)parseaddr.c	3.49		08/27/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();
537889Seric 	static char nbuf[MAXNAME];
54297Seric 
55297Seric 	/*
56297Seric 	**  Initialize and prescan address.
57297Seric 	*/
58297Seric 
596903Seric 	CurEnv->e_to = addr;
603188Seric # ifdef DEBUG
617675Seric 	if (tTd(20, 1))
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.
717889Seric 	**	Ruleset 4 rewrites the address into a form that will
727889Seric 	**		be reflected in the outgoing message.  It must
737889Seric 	**		not resolve.
747889Seric 	**	Ruleset 0 does basic parsing.  It must resolve.
75297Seric 	*/
76297Seric 
777889Seric 	rewrite(pvp, 4);
784070Seric 	rewrite(pvp, 0);
79297Seric 
803149Seric 	/*
813149Seric 	**  See if we resolved to a real mailer.
823149Seric 	*/
83297Seric 
843149Seric 	if (pvp[0][0] != CANONNET)
853149Seric 	{
863149Seric 		setstat(EX_USAGE);
873149Seric 		usrerr("cannot resolve name");
883149Seric 		return (NULL);
89297Seric 	}
90297Seric 
91297Seric 	/*
923149Seric 	**  Build canonical address from pvp.
93297Seric 	*/
94297Seric 
953149Seric 	a = buildaddr(pvp, a);
964279Seric 	if (a == NULL)
974279Seric 		return (NULL);
984598Seric 	m = a->q_mailer;
99297Seric 
100297Seric 	/*
1013149Seric 	**  Make local copies of the host & user and then
1023149Seric 	**  transport them out.
103297Seric 	*/
104297Seric 
105297Seric 	if (copyf > 0)
1062973Seric 		a->q_paddr = newstr(addr);
107297Seric 	else
108297Seric 		a->q_paddr = addr;
1093149Seric 	if (copyf >= 0)
110297Seric 	{
1113149Seric 		if (a->q_host != NULL)
1123149Seric 			a->q_host = newstr(a->q_host);
113297Seric 		else
1143149Seric 			a->q_host = "";
1153149Seric 		if (a->q_user != a->q_paddr)
1163149Seric 			a->q_user = newstr(a->q_user);
117297Seric 	}
118297Seric 
119297Seric 	/*
120297Seric 	**  Do UPPER->lower case mapping unless inhibited.
121297Seric 	*/
122297Seric 
1233149Seric 	if (!bitset(M_HST_UPPER, m->m_flags))
124297Seric 		makelower(a->q_host);
1253149Seric 	if (!bitset(M_USR_UPPER, m->m_flags))
126297Seric 		makelower(a->q_user);
127297Seric 
128297Seric 	/*
129297Seric 	**  Compute return value.
130297Seric 	*/
131297Seric 
132297Seric # ifdef DEBUG
1337675Seric 	if (tTd(20, 1))
1344443Seric 	{
1354443Seric 		printf("parse-->");
1364443Seric 		printaddr(a, FALSE);
1374443Seric 	}
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
1806053Seric # define ONEMORE	6
1813149Seric # define GETONE		7
1824424Seric # define MACRO		8
1833149Seric 
1843149Seric char **
1853149Seric prescan(addr, delim)
186297Seric 	char *addr;
187297Seric 	char delim;
188297Seric {
189297Seric 	register char *p;
1903149Seric 	static char buf[MAXNAME+MAXATOM];
1913149Seric 	static char *av[MAXATOM+1];
1923149Seric 	char **avp;
193297Seric 	bool bslashmode;
194297Seric 	int cmntcnt;
195297Seric 	register char c;
1963149Seric 	char *tok;
197297Seric 	register char *q;
1983149Seric 	register int state;
1993149Seric 	int nstate;
2004085Seric 	extern char lower();
201297Seric 
202297Seric 	q = buf;
2033149Seric 	bslashmode = FALSE;
2047800Seric 	cmntcnt = 0;
2053149Seric 	avp = av;
2063149Seric 	state = OPER;
2073149Seric 	for (p = addr; *p != '\0' && *p != delim; )
208297Seric 	{
2093149Seric 		/* read a token */
2103149Seric 		tok = q;
2113149Seric 		while ((c = *p++) != '\0' && c != delim)
212297Seric 		{
2133149Seric 			/* chew up special characters */
2144100Seric 			c &= ~0200;
2153149Seric 			*q = '\0';
2163149Seric 			if (bslashmode)
2173149Seric 			{
2183149Seric 				c |= 0200;
2193149Seric 				bslashmode = FALSE;
2203149Seric 			}
2213149Seric 			else if (c == '\\')
2223149Seric 			{
2233149Seric 				bslashmode = TRUE;
2243149Seric 				continue;
2253149Seric 			}
2264100Seric 			else if (c == '"')
2274100Seric 			{
2284100Seric 				if (state == QSTRING)
2294100Seric 					state = OPER;
2304100Seric 				else
2314100Seric 					state = QSTRING;
2324100Seric 				break;
2334100Seric 			}
2343149Seric 
2356053Seric 			nstate = toktype(c);
2363149Seric 			switch (state)
2373149Seric 			{
2383149Seric 			  case QSTRING:		/* in quoted string */
2393149Seric 				break;
2403149Seric 
2413149Seric 			  case ATOM:		/* regular atom */
2424228Seric 				if (nstate != ATOM)
2433149Seric 				{
2443149Seric 					state = EOTOK;
2453149Seric 					p--;
2463149Seric 				}
2473149Seric 				break;
2483149Seric 
2493149Seric 			  case GETONE:		/* grab one character */
2503149Seric 				state = OPER;
2513149Seric 				break;
2523149Seric 
2533149Seric 			  case EOTOK:		/* after atom or q-string */
2543149Seric 				state = nstate;
2553149Seric 				if (state == SPACE)
2563149Seric 					continue;
2573149Seric 				break;
2583149Seric 
2593149Seric 			  case SPACE:		/* linear white space */
2603149Seric 				state = nstate;
2614228Seric 				break;
2623149Seric 
2633149Seric 			  case OPER:		/* operator */
2643149Seric 				if (nstate == SPACE)
2653149Seric 					continue;
2663149Seric 				state = nstate;
2673149Seric 				break;
2683149Seric 
2696053Seric 			  case ONEMORE:		/* $- etc. */
2706053Seric 				state = GETONE;
2713149Seric 				break;
2723149Seric 
2733149Seric 			  default:
2743149Seric 				syserr("prescan: unknown state %d", state);
2753149Seric 			}
2763149Seric 
2774228Seric 			if (state == EOTOK || state == SPACE)
2783149Seric 				break;
2793149Seric 
2803149Seric 			/* squirrel it away */
2813149Seric 			if (q >= &buf[sizeof buf - 5])
2823149Seric 			{
2833149Seric 				usrerr("Address too long");
2843149Seric 				return (NULL);
2853149Seric 			}
2863149Seric 			*q++ = c;
2873149Seric 
2883149Seric 			/* decide whether this represents end of token */
2896053Seric 			if (state == OPER || state == GETONE)
2903149Seric 				break;
291297Seric 		}
2923149Seric 		if (c == '\0' || c == delim)
2933149Seric 			p--;
2943149Seric 
2953149Seric 		/* new token */
2963149Seric 		if (tok == q)
297297Seric 			continue;
2983149Seric 		*q++ = '\0';
2993149Seric 
3003149Seric 		c = tok[0];
3013149Seric 		if (c == '(')
3021378Seric 		{
303297Seric 			cmntcnt++;
3041378Seric 			continue;
3051378Seric 		}
306297Seric 		else if (c == ')')
307297Seric 		{
308297Seric 			if (cmntcnt <= 0)
309297Seric 			{
310297Seric 				usrerr("Unbalanced ')'");
311297Seric 				return (NULL);
312297Seric 			}
313297Seric 			else
314297Seric 			{
315297Seric 				cmntcnt--;
316297Seric 				continue;
317297Seric 			}
318297Seric 		}
3193149Seric 		else if (cmntcnt > 0)
3202091Seric 			continue;
3213149Seric 
3224448Seric 		if (avp >= &av[MAXATOM])
3234448Seric 		{
3244448Seric 			syserr("prescan: too many tokens");
3254448Seric 			return (NULL);
3264448Seric 		}
3274448Seric 		*avp++ = tok;
3283149Seric 	}
3293149Seric 	*avp = NULL;
3303149Seric 	if (cmntcnt > 0)
3313149Seric 		usrerr("Unbalanced '('");
3323149Seric 	else if (state == QSTRING)
3333149Seric 		usrerr("Unbalanced '\"'");
3343149Seric 	else if (av[0] != NULL)
3353149Seric 		return (av);
3363149Seric 	return (NULL);
3373149Seric }
3383149Seric /*
3393149Seric **  TOKTYPE -- return token type
3403149Seric **
3413149Seric **	Parameters:
3423149Seric **		c -- the character in question.
3433149Seric **
3443149Seric **	Returns:
3453149Seric **		Its type.
3463149Seric **
3473149Seric **	Side Effects:
3483149Seric **		none.
3493149Seric */
350297Seric 
3513149Seric toktype(c)
3523149Seric 	register char c;
3533149Seric {
3543380Seric 	static char buf[50];
3553382Seric 	static bool firstime = TRUE;
3563380Seric 
3573382Seric 	if (firstime)
3583380Seric 	{
3593382Seric 		firstime = FALSE;
3606977Seric 		expand("$o", buf, &buf[sizeof buf - 1], CurEnv);
3617005Seric 		(void) strcat(buf, DELIMCHARS);
3623380Seric 	}
3636053Seric 	if (c == MATCHCLASS || c == MATCHREPL)
3646053Seric 		return (ONEMORE);
3654100Seric 	if (!isascii(c))
3664100Seric 		return (ATOM);
3673149Seric 	if (isspace(c))
3683149Seric 		return (SPACE);
3693380Seric 	if (iscntrl(c) || index(buf, c) != NULL)
3703149Seric 		return (OPER);
3713149Seric 	return (ATOM);
3723149Seric }
3733149Seric /*
3743149Seric **  REWRITE -- apply rewrite rules to token vector.
3753149Seric **
3764476Seric **	This routine is an ordered production system.  Each rewrite
3774476Seric **	rule has a LHS (called the pattern) and a RHS (called the
3784476Seric **	rewrite); 'rwr' points the the current rewrite rule.
3794476Seric **
3804476Seric **	For each rewrite rule, 'avp' points the address vector we
3814476Seric **	are trying to match against, and 'pvp' points to the pattern.
3824476Seric **	If pvp points to a special match value (MATCHANY, MATCHONE,
3834476Seric **	MATCHCLASS) then the address in avp matched is saved away
3844476Seric **	in the match vector (pointed to by 'mvp').
3854476Seric **
3864476Seric **	When a match between avp & pvp does not match, we try to
3874476Seric **	back out.  If we back up over a MATCHONE or a MATCHCLASS
3884476Seric **	we must also back out the match in mvp.  If we reach a
3894476Seric **	MATCHANY we just extend the match and start over again.
3904476Seric **
3914476Seric **	When we finally match, we rewrite the address vector
3924476Seric **	and try over again.
3934476Seric **
3943149Seric **	Parameters:
3953149Seric **		pvp -- pointer to token vector.
3963149Seric **
3973149Seric **	Returns:
3983149Seric **		none.
3993149Seric **
4003149Seric **	Side Effects:
4013149Seric **		pvp is modified.
4023149Seric */
4032091Seric 
4043149Seric struct match
4053149Seric {
4064468Seric 	char	**first;	/* first token matched */
4074468Seric 	char	**last;		/* last token matched */
4083149Seric };
4093149Seric 
4104468Seric # define MAXMATCH	9	/* max params per rewrite */
4113149Seric 
4123149Seric 
4134070Seric rewrite(pvp, ruleset)
4143149Seric 	char **pvp;
4154070Seric 	int ruleset;
4163149Seric {
4173149Seric 	register char *ap;		/* address pointer */
4183149Seric 	register char *rp;		/* rewrite pointer */
4193149Seric 	register char **avp;		/* address vector pointer */
4203149Seric 	register char **rvp;		/* rewrite vector pointer */
4214468Seric 	struct rewrite *rwr;		/* pointer to current rewrite rule */
4224468Seric 	struct match mlist[MAXMATCH];	/* stores match on LHS */
4234468Seric 	struct match *mlp;		/* cur ptr into mlist */
4243149Seric 	char *npvp[MAXATOM+1];		/* temporary space for rebuild */
4254060Seric 	extern bool sameword();
4263149Seric 
4274100Seric # ifdef DEBUG
4287675Seric 	if (tTd(21, 9))
4293149Seric 	{
4303149Seric 		printf("rewrite: original pvp:\n");
4313149Seric 		printav(pvp);
4323149Seric 	}
4334100Seric # endif DEBUG
4343149Seric 
4353149Seric 	/*
4363149Seric 	**  Run through the list of rewrite rules, applying
4373149Seric 	**	any that match.
4383149Seric 	*/
4393149Seric 
4404070Seric 	for (rwr = RewriteRules[ruleset]; rwr != NULL; )
4413149Seric 	{
4424100Seric # ifdef DEBUG
4437675Seric 		if (tTd(21, 12))
444297Seric 		{
4453149Seric 			printf("-----trying rule:\n");
4463149Seric 			printav(rwr->r_lhs);
4473149Seric 		}
4484100Seric # endif DEBUG
4493149Seric 
4503149Seric 		/* try to match on this rule */
4514468Seric 		mlp = mlist;
4524476Seric 		for (rvp = rwr->r_lhs, avp = pvp; *avp != NULL; )
4533149Seric 		{
4543149Seric 			ap = *avp;
4553149Seric 			rp = *rvp;
4563149Seric 
4573149Seric 			if (rp == NULL)
458297Seric 			{
4593149Seric 				/* end-of-pattern before end-of-address */
4603149Seric 				goto fail;
461297Seric 			}
4623149Seric 
4633149Seric 			switch (*rp)
4643149Seric 			{
4654060Seric 				register STAB *s;
4664060Seric 				register int class;
4674060Seric 
4684060Seric 			  case MATCHCLASS:
4694060Seric 				/* match any token in a class */
4704060Seric 				class = rp[1];
4714060Seric 				if (!isalpha(class))
4724060Seric 					goto fail;
4734060Seric 				if (isupper(class))
4744060Seric 					class -= 'A';
4754060Seric 				else
4764060Seric 					class -= 'a';
4774100Seric 				s = stab(ap, ST_CLASS, ST_FIND);
4786273Seric 				if (s == NULL || (s->s_class & (1L << class)) == 0)
4794060Seric 					goto fail;
4804468Seric 
4814476Seric 				/* explicit fall-through */
4824476Seric 
4834476Seric 			  case MATCHONE:
4844476Seric 			  case MATCHANY:
4854476Seric 				/* match exactly one token */
4864468Seric 				mlp->first = mlp->last = avp++;
4874468Seric 				mlp++;
4884060Seric 				break;
4894060Seric 
4903149Seric 			  default:
4913149Seric 				/* must have exact match */
4924060Seric 				if (!sameword(rp, ap))
4934060Seric 					goto fail;
4944468Seric 				avp++;
4953149Seric 				break;
4963149Seric 			}
4973149Seric 
4983149Seric 			/* successful match on this token */
4993149Seric 			rvp++;
5003149Seric 			continue;
5013149Seric 
5023149Seric 		  fail:
5033149Seric 			/* match failed -- back up */
5043149Seric 			while (--rvp >= rwr->r_lhs)
5053149Seric 			{
5063149Seric 				rp = *rvp;
5073149Seric 				if (*rp == MATCHANY)
5084468Seric 				{
5094476Seric 					/* extend binding and continue */
5104476Seric 					mlp[-1].last = avp++;
5114476Seric 					rvp++;
5123149Seric 					break;
5134468Seric 				}
5144476Seric 				avp--;
5154476Seric 				if (*rp == MATCHONE || *rp == MATCHCLASS)
5163149Seric 				{
5174468Seric 					/* back out binding */
5184468Seric 					mlp--;
5193149Seric 				}
5203149Seric 			}
5213149Seric 
5223149Seric 			if (rvp < rwr->r_lhs)
5233149Seric 			{
5243149Seric 				/* total failure to match */
5253149Seric 				break;
5263149Seric 			}
527297Seric 		}
5283149Seric 
5293149Seric 		/*
5303149Seric 		**  See if we successfully matched
5313149Seric 		*/
5323149Seric 
5333149Seric 		if (rvp >= rwr->r_lhs && *rvp == NULL)
5343149Seric 		{
5354100Seric # ifdef DEBUG
5367675Seric 			if (tTd(21, 12))
5373149Seric 			{
5383149Seric 				printf("-----rule matches:\n");
5393149Seric 				printav(rwr->r_rhs);
5403149Seric 			}
5414100Seric # endif DEBUG
5423149Seric 
5433149Seric 			/* substitute */
5443149Seric 			for (rvp = rwr->r_rhs, avp = npvp; *rvp != NULL; rvp++)
5453149Seric 			{
5463149Seric 				rp = *rvp;
5474468Seric 				if (*rp == MATCHREPL)
5483149Seric 				{
5493149Seric 					register struct match *m;
5503149Seric 					register char **pp;
5513149Seric 
5524468Seric 					m = &mlist[rp[1] - '1'];
5534476Seric # ifdef DEBUG
5547675Seric 					if (tTd(21, 15))
5554476Seric 					{
5564476Seric 						printf("$%c:", rp[1]);
5574476Seric 						pp = m->first;
5584476Seric 						do
5594476Seric 						{
5604476Seric 							printf(" %x=\"", *pp);
5614625Seric 							(void) fflush(stdout);
5624476Seric 							printf("%s\"", *pp);
5634476Seric 						} while (pp++ != m->last);
5644476Seric 						printf("\n");
5654476Seric 					}
5664476Seric # endif DEBUG
5674468Seric 					pp = m->first;
5684468Seric 					do
5693149Seric 					{
5704468Seric 						if (avp >= &npvp[MAXATOM])
5713149Seric 						{
5724468Seric 							syserr("rewrite: expansion too long");
5734468Seric 							return;
5744468Seric 						}
5754468Seric 						*avp++ = *pp;
5764468Seric 					} while (pp++ != m->last);
5773149Seric 				}
5783149Seric 				else
5794385Seric 				{
5804385Seric 					if (avp >= &npvp[MAXATOM])
5814385Seric 					{
5824385Seric 						syserr("rewrite: expansion too long");
5834385Seric 						return;
5844385Seric 					}
5853149Seric 					*avp++ = rp;
5864385Seric 				}
5873149Seric 			}
5883149Seric 			*avp++ = NULL;
5894085Seric 			bmove((char *) npvp, (char *) pvp, (avp - npvp) * sizeof *avp);
5903149Seric # ifdef DEBUG
5917675Seric 			if (tTd(21, 4))
5923149Seric 			{
5933188Seric 				char **vp;
5943188Seric 
5953188Seric 				printf("rewritten as `");
5963188Seric 				for (vp = pvp; *vp != NULL; vp++)
5974228Seric 				{
5984228Seric 					if (vp != pvp)
5994228Seric 						printf("_");
6003188Seric 					xputs(*vp);
6014228Seric 				}
6023188Seric 				printf("'\n");
6033149Seric 			}
6043149Seric # endif DEBUG
6053149Seric 			if (pvp[0][0] == CANONNET)
6063149Seric 				break;
6073149Seric 		}
6083149Seric 		else
6093149Seric 		{
6104100Seric # ifdef DEBUG
6117675Seric 			if (tTd(21, 10))
6123149Seric 				printf("----- rule fails\n");
6134100Seric # endif DEBUG
6143149Seric 			rwr = rwr->r_next;
6153149Seric 		}
616297Seric 	}
6173149Seric }
6183149Seric /*
6193149Seric **  BUILDADDR -- build address from token vector.
6203149Seric **
6213149Seric **	Parameters:
6223149Seric **		tv -- token vector.
6233149Seric **		a -- pointer to address descriptor to fill.
6243149Seric **			If NULL, one will be allocated.
6253149Seric **
6263149Seric **	Returns:
6274279Seric **		NULL if there was an error.
6284279Seric **		'a' otherwise.
6293149Seric **
6303149Seric **	Side Effects:
6313149Seric **		fills in 'a'
6323149Seric */
6333149Seric 
6343149Seric ADDRESS *
6353149Seric buildaddr(tv, a)
6363149Seric 	register char **tv;
6373149Seric 	register ADDRESS *a;
6383149Seric {
6393149Seric 	static char buf[MAXNAME];
6403149Seric 	struct mailer **mp;
6413149Seric 	register struct mailer *m;
6424635Seric 	extern bool sameword();
6433149Seric 
6443149Seric 	if (a == NULL)
6453149Seric 		a = (ADDRESS *) xalloc(sizeof *a);
6464988Seric 	clear((char *) a, sizeof *a);
6473149Seric 
6483149Seric 	/* figure out what net/mailer to use */
6493149Seric 	if (**tv != CANONNET)
6504279Seric 	{
6513149Seric 		syserr("buildaddr: no net");
6524279Seric 		return (NULL);
6534279Seric 	}
6543149Seric 	tv++;
6554635Seric 	if (sameword(*tv, "error"))
6564279Seric 	{
6574279Seric 		if (**++tv != CANONUSER)
6584279Seric 			syserr("buildaddr: error: no user");
6594279Seric 		buf[0] = '\0';
6604279Seric 		while (*++tv != NULL)
6614279Seric 		{
6624279Seric 			if (buf[0] != '\0')
6637005Seric 				(void) strcat(buf, " ");
6647005Seric 			(void) strcat(buf, *tv);
6654279Seric 		}
6664279Seric 		usrerr(buf);
6674279Seric 		return (NULL);
6684279Seric 	}
6694598Seric 	for (mp = Mailer; (m = *mp++) != NULL; )
6703149Seric 	{
6714635Seric 		if (sameword(m->m_name, *tv))
6723149Seric 			break;
6733149Seric 	}
6743149Seric 	if (m == NULL)
6754279Seric 	{
6763149Seric 		syserr("buildaddr: unknown net %s", *tv);
6774279Seric 		return (NULL);
6784279Seric 	}
6794598Seric 	a->q_mailer = m;
6803149Seric 
6813149Seric 	/* figure out what host (if any) */
6823149Seric 	tv++;
6834195Seric 	if (!bitset(M_LOCAL, m->m_flags))
6843149Seric 	{
6855704Seric 		if (**tv++ != CANONHOST)
6864279Seric 		{
6873149Seric 			syserr("buildaddr: no host");
6884279Seric 			return (NULL);
6894279Seric 		}
6905704Seric 		buf[0] = '\0';
6915704Seric 		while (*tv != NULL && **tv != CANONUSER)
6927005Seric 			(void) strcat(buf, *tv++);
6935704Seric 		a->q_host = newstr(buf);
6943149Seric 	}
6953149Seric 	else
6963149Seric 		a->q_host = NULL;
6973149Seric 
6983149Seric 	/* figure out the user */
6993149Seric 	if (**tv != CANONUSER)
7004279Seric 	{
7013149Seric 		syserr("buildaddr: no user");
7024279Seric 		return (NULL);
7034279Seric 	}
7044228Seric 	cataddr(++tv, buf, sizeof buf);
7053149Seric 	a->q_user = buf;
7063149Seric 
7073149Seric 	return (a);
7083149Seric }
7093188Seric /*
7104228Seric **  CATADDR -- concatenate pieces of addresses (putting in <LWSP> subs)
7114228Seric **
7124228Seric **	Parameters:
7134228Seric **		pvp -- parameter vector to rebuild.
7144228Seric **		buf -- buffer to build the string into.
7154228Seric **		sz -- size of buf.
7164228Seric **
7174228Seric **	Returns:
7184228Seric **		none.
7194228Seric **
7204228Seric **	Side Effects:
7214228Seric **		Destroys buf.
7224228Seric */
7234228Seric 
7244228Seric cataddr(pvp, buf, sz)
7254228Seric 	char **pvp;
7264228Seric 	char *buf;
7274228Seric 	register int sz;
7284228Seric {
7294228Seric 	bool oatomtok = FALSE;
7304228Seric 	bool natomtok = FALSE;
7314228Seric 	register int i;
7324228Seric 	register char *p;
7334228Seric 
7344228Seric 	p = buf;
7354228Seric 	sz--;
7364228Seric 	while (*pvp != NULL && (i = strlen(*pvp)) < sz)
7374228Seric 	{
7384228Seric 		natomtok = (toktype(**pvp) == ATOM);
7394228Seric 		if (oatomtok && natomtok)
7404228Seric 			*p++ = SPACESUB;
7414228Seric 		(void) strcpy(p, *pvp);
7424228Seric 		oatomtok = natomtok;
7434228Seric 		p += i;
7444228Seric 		sz -= i;
7454228Seric 		pvp++;
7464228Seric 	}
7474228Seric 	*p = '\0';
7484228Seric }
7494228Seric /*
7503188Seric **  SAMEADDR -- Determine if two addresses are the same
7513188Seric **
7523188Seric **	This is not just a straight comparison -- if the mailer doesn't
7533188Seric **	care about the host we just ignore it, etc.
7543188Seric **
7553188Seric **	Parameters:
7563188Seric **		a, b -- pointers to the internal forms to compare.
7573188Seric **		wildflg -- if TRUE, 'a' may have no user specified,
7583188Seric **			in which case it is to match anything.
7593188Seric **
7603188Seric **	Returns:
7613188Seric **		TRUE -- they represent the same mailbox.
7623188Seric **		FALSE -- they don't.
7633188Seric **
7643188Seric **	Side Effects:
7653188Seric **		none.
7663188Seric */
7673188Seric 
7683188Seric bool
7693188Seric sameaddr(a, b, wildflg)
7703188Seric 	register ADDRESS *a;
7713188Seric 	register ADDRESS *b;
7723188Seric 	bool wildflg;
7733188Seric {
7743188Seric 	/* if they don't have the same mailer, forget it */
7753188Seric 	if (a->q_mailer != b->q_mailer)
7763188Seric 		return (FALSE);
7773188Seric 
7783188Seric 	/* if the user isn't the same, we can drop out */
7793188Seric 	if ((!wildflg || a->q_user[0] != '\0') && strcmp(a->q_user, b->q_user) != 0)
7803188Seric 		return (FALSE);
7813188Seric 
7823188Seric 	/* if the mailer ignores hosts, we have succeeded! */
7834598Seric 	if (bitset(M_LOCAL, a->q_mailer->m_flags))
7843188Seric 		return (TRUE);
7853188Seric 
7863188Seric 	/* otherwise compare hosts (but be careful for NULL ptrs) */
7873188Seric 	if (a->q_host == NULL || b->q_host == NULL)
7883188Seric 		return (FALSE);
7893188Seric 	if (strcmp(a->q_host, b->q_host) != 0)
7903188Seric 		return (FALSE);
7913188Seric 
7923188Seric 	return (TRUE);
7933188Seric }
7943234Seric /*
7953234Seric **  PRINTADDR -- print address (for debugging)
7963234Seric **
7973234Seric **	Parameters:
7983234Seric **		a -- the address to print
7993234Seric **		follow -- follow the q_next chain.
8003234Seric **
8013234Seric **	Returns:
8023234Seric **		none.
8033234Seric **
8043234Seric **	Side Effects:
8053234Seric **		none.
8063234Seric */
8073234Seric 
8084317Seric # ifdef DEBUG
8094317Seric 
8103234Seric printaddr(a, follow)
8113234Seric 	register ADDRESS *a;
8123234Seric 	bool follow;
8133234Seric {
8145001Seric 	bool first = TRUE;
8155001Seric 
8163234Seric 	while (a != NULL)
8173234Seric 	{
8185001Seric 		first = FALSE;
8194443Seric 		printf("%x=", a);
8204085Seric 		(void) fflush(stdout);
8213234Seric 		printf("%s: mailer %d (%s), host `%s', user `%s'\n", a->q_paddr,
8224598Seric 		       a->q_mailer->m_mno, a->q_mailer->m_name, a->q_host, a->q_user);
8235035Seric 		printf("\tnext=%x, flags=%o, rmailer %d, alias %x\n", a->q_next,
8245035Seric 		       a->q_flags, a->q_rmailer, a->q_alias);
8255001Seric 		printf("\thome=\"%s\", fullname=\"%s\"\n", a->q_home, a->q_fullname);
8264996Seric 
8273234Seric 		if (!follow)
8283234Seric 			return;
8294996Seric 		a = a->q_next;
8303234Seric 	}
8315001Seric 	if (first)
8324443Seric 		printf("[NULL]\n");
8333234Seric }
8344317Seric 
8354317Seric # endif DEBUG
8367682Seric /*
8377682Seric **  REMOTENAME -- return the name relative to the current mailer
8387682Seric **
8397682Seric **	Parameters:
8407682Seric **		name -- the name to translate.
8417682Seric **		force -- if set, forces rewriting even if the mailer
8427682Seric **			does not request it.  Used for rewriting
8437682Seric **			sender addresses.
8447682Seric **
8457682Seric **	Returns:
8467682Seric **		the text string representing this address relative to
8477682Seric **			the receiving mailer.
8487682Seric **
8497682Seric **	Side Effects:
8507682Seric **		none.
8517682Seric **
8527682Seric **	Warnings:
8537682Seric **		The text string returned is tucked away locally;
8547682Seric **			copy it if you intend to save it.
8557682Seric */
8567682Seric 
8577682Seric char *
8587682Seric remotename(name, m, force)
8597682Seric 	char *name;
8607682Seric 	struct mailer *m;
8617682Seric 	bool force;
8627682Seric {
8637682Seric 	static char buf[MAXNAME];
8647682Seric 	char lbuf[MAXNAME];
8657682Seric 	extern char *macvalue();
8667682Seric 	char *oldf = macvalue('f');
8677682Seric 	char *oldg = macvalue('g');
8687682Seric 	extern char **prescan();
8697682Seric 	register char **pvp;
8707682Seric 	extern ADDRESS *buildaddr();
8717889Seric 	extern char *crackaddr();
8727889Seric 	char *fancy;
8737682Seric 
8747755Seric # ifdef DEBUG
8757755Seric 	if (tTd(12, 1))
8767755Seric 		printf("remotename(%s)\n", name);
8777755Seric # endif DEBUG
8787755Seric 
8797682Seric 	/*
8807889Seric 	**  First put this address into canonical form.
8817889Seric 	**	First turn it into a macro.
8827889Seric 	**	Then run it through ruleset 4.
8837889Seric 	*/
8847889Seric 
8857889Seric 	/* save away the extraneous pretty stuff */
8867889Seric 	fancy = crackaddr(name);
8877889Seric 
8887889Seric 	/* now run through ruleset four */
8897889Seric 	pvp = prescan(name, '\0');
8907889Seric 	if (pvp == NULL)
8917889Seric 		return (name);
8927889Seric 	rewrite(pvp, 4);
8937889Seric 
8947889Seric 	/*
8957682Seric 	**  See if this mailer wants the name to be rewritten.  There are
8967682Seric 	**  many problems here, owing to the standards for doing replies.
8977682Seric 	**  In general, these names should only be rewritten if we are
8987682Seric 	**  sending to another host that runs sendmail.
8997682Seric 	*/
9007682Seric 
9017889Seric 	if (bitset(M_RELRCPT, m->m_flags) && !force)
9027755Seric 	{
9037889Seric 		/*
9047889Seric 		**  Do general rewriting of name.
9057889Seric 		**	This will also take care of doing global name
9067889Seric 		**	translation.
9077889Seric 		*/
9087682Seric 
9097889Seric 		rewrite(pvp, 1);
9107889Seric 		rewrite(pvp, 3);
9117682Seric 
9127889Seric 		/* make the name relative to the receiving mailer */
9137889Seric 		cataddr(pvp, lbuf, sizeof lbuf);
9147889Seric 		define('f', lbuf);
9157889Seric 		expand(m->m_from, buf, &buf[sizeof buf - 1], CurEnv);
9167889Seric 
9177889Seric 		/* rewrite to get rid of garbage we added in the expand above */
9187889Seric 		pvp = prescan(buf, '\0');
9197889Seric 		if (pvp == NULL)
9207889Seric 			return (name);
9217889Seric 		rewrite(pvp, 2);
9227682Seric 	}
9237682Seric 
9247889Seric 	/* now add any comment info we had before back */
9257682Seric 	cataddr(pvp, lbuf, sizeof lbuf);
9267682Seric 	define('g', lbuf);
9277889Seric 	expand(fancy, buf, &buf[sizeof buf - 1], CurEnv);
9287682Seric 
9297682Seric 	define('f', oldf);
9307682Seric 	define('g', oldg);
9317682Seric 
9327682Seric # ifdef DEBUG
9337682Seric 	if (tTd(12, 1))
9347755Seric 		printf("remotename => `%s'\n", buf);
9357682Seric # endif DEBUG
9367682Seric 	return (buf);
9377682Seric }
9387682Seric /*
9397682Seric **  CANONNAME -- make name canonical
9407682Seric **
9417682Seric **	This is used for SMTP and misc. printing.  Given a print
9427682Seric **	address, it strips out comments, etc., and puts on exactly
9437682Seric **	one set of brackets.
9447682Seric **
9457682Seric **	Parameters:
9467682Seric **		name -- the name to make canonical.
9477682Seric **
9487682Seric **	Returns:
9497682Seric **		pointer to canonical name.
9507682Seric **
9517682Seric **	Side Effects:
9527682Seric **		none.
9537682Seric **
9547682Seric **	Warning:
9557682Seric **		result is saved in static buf; future calls will trash it.
9567682Seric */
9577682Seric 
9587682Seric char *
9597682Seric canonname(name)
9607682Seric 	char *name;
9617682Seric {
9627682Seric 	static char nbuf[MAXNAME + 2];
963*7940Seric 	register char **pvp;
9647682Seric 
965*7940Seric 	pvp = prescan(name, '\0');
966*7940Seric 	rewrite(pvp, 4);
967*7940Seric 	cataddr(pvp, nbuf + 1, MAXNAME);
968*7940Seric 	nbuf[0] = '<';
9697682Seric 	strcat(nbuf, ">");
9707682Seric 	return (nbuf);
9717682Seric }
972