14227346bSdist /*
20942ea6aSbostic  * Copyright (c) 1983 Eric P. Allman
3e70a7521Sbostic  * Copyright (c) 1988 Regents of the University of California.
4e70a7521Sbostic  * All rights reserved.
5e70a7521Sbostic  *
63bc94712Sbostic  * %sccs.include.redist.c%
74227346bSdist  */
84227346bSdist 
94227346bSdist #ifndef lint
10*c9be6216Seric static char sccsid[] = "@(#)deliver.c	6.54 (Berkeley) 03/25/93";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14a8c080f0Seric #include <signal.h>
15c77d1c25Seric #include <sys/stat.h>
16f28da541Smiriam #include <netdb.h>
17911693bfSbostic #include <errno.h>
18134746fbSeric #ifdef NAMED_BIND
19912a731aSbostic #include <arpa/nameser.h>
20912a731aSbostic #include <resolv.h>
21134746fbSeric #endif
2225a99e2eSeric 
2325a99e2eSeric /*
2413bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
2513bbc08cSeric **
2613bbc08cSeric **	This routine delivers to everyone on the same host as the
2713bbc08cSeric **	user on the head of the list.  It is clever about mailers
2813bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
2913bbc08cSeric **	that it will deliver to all these addresses however -- so
3013bbc08cSeric **	deliver should be called once for each address on the
3113bbc08cSeric **	list.
3225a99e2eSeric **
3325a99e2eSeric **	Parameters:
34588cad61Seric **		e -- the envelope to deliver.
35c77d1c25Seric **		firstto -- head of the address list to deliver to.
3625a99e2eSeric **
3725a99e2eSeric **	Returns:
3825a99e2eSeric **		zero -- successfully delivered.
3925a99e2eSeric **		else -- some failure, see ExitStat for more info.
4025a99e2eSeric **
4125a99e2eSeric **	Side Effects:
4225a99e2eSeric **		The standard input is passed off to someone.
4325a99e2eSeric */
4425a99e2eSeric 
45588cad61Seric deliver(e, firstto)
46588cad61Seric 	register ENVELOPE *e;
47c77d1c25Seric 	ADDRESS *firstto;
4825a99e2eSeric {
4978442df3Seric 	char *host;			/* host being sent to */
5078442df3Seric 	char *user;			/* user being sent to */
5125a99e2eSeric 	char **pvp;
525dfc646bSeric 	register char **mvp;
5325a99e2eSeric 	register char *p;
54588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
556259796dSeric 	ADDRESS *ctladdr;
56b31e7f2bSeric 	register MCI *mci;
57c77d1c25Seric 	register ADDRESS *to = firstto;
58c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
59772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
60911693bfSbostic 	int rcode;			/* response code */
61e103b48fSeric 	char *firstsig;			/* signature of firstto */
62ee6bf8dfSeric 	char *pv[MAXPV+1];
63579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
64ee6bf8dfSeric 	char buf[MAXNAME];
65c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
66fabb3bd4Seric 	extern int checkcompat();
67ee6bf8dfSeric 	extern ADDRESS *getctladdr();
68ee6bf8dfSeric 	extern char *remotename();
69b31e7f2bSeric 	extern MCI *openmailer();
70e103b48fSeric 	extern char *hostsignature();
7125a99e2eSeric 
7235490626Seric 	errno = 0;
73ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
745dfc646bSeric 		return (0);
7525a99e2eSeric 
76134746fbSeric #ifdef NAMED_BIND
77912a731aSbostic 	/* unless interactive, try twice, over a minute */
78912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
79912a731aSbostic 		_res.retrans = 30;
80912a731aSbostic 		_res.retry = 2;
81912a731aSbostic 	}
82d4bd8f0eSbostic #endif
83912a731aSbostic 
8451552439Seric 	m = to->q_mailer;
8551552439Seric 	host = to->q_host;
86*c9be6216Seric 	CurEnv = e;			/* just in case */
8751552439Seric 
886ef48975Seric 	if (tTd(10, 1))
895dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
9051552439Seric 			m->m_mno, host, to->q_user);
91f3dbc832Seric 
92f3dbc832Seric 	/*
93f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
94f3dbc832Seric 	**  connections now, just mark these addresses and return.
95f3dbc832Seric 	**	This is useful if we want to batch connections to
96f3dbc832Seric 	**	reduce load.  This will cause the messages to be
97f3dbc832Seric 	**	queued up, and a daemon will come along to send the
98f3dbc832Seric 	**	messages later.
99f3dbc832Seric 	**		This should be on a per-mailer basis.
100f3dbc832Seric 	*/
101f3dbc832Seric 
10219c47125Seric 	if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) &&
10319c47125Seric 	    bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
104f3dbc832Seric 	{
105f3dbc832Seric 		for (; to != NULL; to = to->q_next)
106f4560e80Seric 		{
107ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
108c6e18ac5Seric 			    to->q_mailer != m)
109f4560e80Seric 				continue;
110f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
111588cad61Seric 			e->e_to = to->q_paddr;
11208b25121Seric 			message("queued");
1132f624c86Seric 			if (LogLevel > 8)
11481161401Seric 				logdelivery(m, NULL, "queued", e);
115f4560e80Seric 		}
116588cad61Seric 		e->e_to = NULL;
117f3dbc832Seric 		return (0);
118f3dbc832Seric 	}
119f3dbc832Seric 
12025a99e2eSeric 	/*
1215dfc646bSeric 	**  Do initial argv setup.
1225dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
1235dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
1245dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
1255dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
1265dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
1273bea8136Seric 	**		The from address rewrite is expected to make
1283bea8136Seric 	**		the address relative to the other end.
1295dfc646bSeric 	*/
1305dfc646bSeric 
13178442df3Seric 	/* rewrite from address, using rewriting rules */
132ee4b0922Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, TRUE, FALSE,
1339c69ca01Seric 					   TRUE, FALSE, e));
134ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
135588cad61Seric 	define('h', host, e);			/* to host */
1365dfc646bSeric 	Errors = 0;
1375dfc646bSeric 	pvp = pv;
1385dfc646bSeric 	*pvp++ = m->m_argv[0];
1395dfc646bSeric 
1405dfc646bSeric 	/* insert -f or -r flag as appropriate */
14157fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
1425dfc646bSeric 	{
14357fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
1445dfc646bSeric 			*pvp++ = "-f";
1455dfc646bSeric 		else
1465dfc646bSeric 			*pvp++ = "-r";
147c23ed322Seric 		*pvp++ = newstr(rpathbuf);
1485dfc646bSeric 	}
1495dfc646bSeric 
1505dfc646bSeric 	/*
1515dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1525dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1535dfc646bSeric 	**  be one of these, and there are only a few more slots
1545dfc646bSeric 	**  in the pv after it.
1555dfc646bSeric 	*/
1565dfc646bSeric 
1575dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1585dfc646bSeric 	{
1592bc47524Seric 		/* can't use strchr here because of sign extension problems */
1602bc47524Seric 		while (*p != '\0')
1612bc47524Seric 		{
1622bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
1632bc47524Seric 			{
1642bc47524Seric 				if (*p == 'u')
1655dfc646bSeric 					break;
1662bc47524Seric 			}
1672bc47524Seric 		}
1682bc47524Seric 
1692bc47524Seric 		if (*p != '\0')
1705dfc646bSeric 			break;
1715dfc646bSeric 
1725dfc646bSeric 		/* this entry is safe -- go ahead and process it */
173588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1745dfc646bSeric 		*pvp++ = newstr(buf);
1755dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1765dfc646bSeric 		{
17708b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
1785dfc646bSeric 			return (-1);
1795dfc646bSeric 		}
1805dfc646bSeric 	}
181c579ef51Seric 
18233db8731Seric 	/*
18333db8731Seric 	**  If we have no substitution for the user name in the argument
18433db8731Seric 	**  list, we know that we must supply the names otherwise -- and
18533db8731Seric 	**  SMTP is the answer!!
18633db8731Seric 	*/
18733db8731Seric 
1885dfc646bSeric 	if (*mvp == NULL)
189c579ef51Seric 	{
190c579ef51Seric 		/* running SMTP */
1912c7e1b8dSeric # ifdef SMTP
192c579ef51Seric 		clever = TRUE;
193c579ef51Seric 		*pvp = NULL;
1946c2c3107Seric # else /* SMTP */
19533db8731Seric 		/* oops!  we don't implement SMTP */
19608b25121Seric 		syserr("554 SMTP style mailer");
1972c7e1b8dSeric 		return (EX_SOFTWARE);
1986c2c3107Seric # endif /* SMTP */
199c579ef51Seric 	}
2005dfc646bSeric 
2015dfc646bSeric 	/*
2025dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
2035dfc646bSeric 	**  run through our address list and append all the addresses
2045dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
2055dfc646bSeric 	**  always send another copy later.
2065dfc646bSeric 	*/
2075dfc646bSeric 
2085dfc646bSeric 	tobuf[0] = '\0';
209588cad61Seric 	e->e_to = tobuf;
2106259796dSeric 	ctladdr = NULL;
211e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
2125dfc646bSeric 	for (; to != NULL; to = to->q_next)
2135dfc646bSeric 	{
2145dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
21557fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
2165dfc646bSeric 			break;
2175dfc646bSeric 
2185dfc646bSeric 		/* if already sent or not for this host, don't send */
219ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
220e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
221e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
2225dfc646bSeric 			continue;
2236259796dSeric 
2244b22ea87Seric 		/* avoid overflowing tobuf */
225aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
2264b22ea87Seric 			break;
2274b22ea87Seric 
2286ef48975Seric 		if (tTd(10, 1))
229772e6e50Seric 		{
230772e6e50Seric 			printf("\nsend to ");
231772e6e50Seric 			printaddr(to, FALSE);
232772e6e50Seric 		}
233772e6e50Seric 
2346259796dSeric 		/* compute effective uid/gid when sending */
2357da1035fSeric 		if (to->q_mailer == ProgMailer)
2366259796dSeric 			ctladdr = getctladdr(to);
2376259796dSeric 
2385dfc646bSeric 		user = to->q_user;
239588cad61Seric 		e->e_to = to->q_paddr;
24075f1ade9Seric 		if (tTd(10, 5))
24175f1ade9Seric 		{
24275f1ade9Seric 			printf("deliver: QDONTSEND ");
24375f1ade9Seric 			printaddr(to, FALSE);
24475f1ade9Seric 		}
245ee4b0922Seric 		to->q_flags |= QDONTSEND;
2465dfc646bSeric 
2475dfc646bSeric 		/*
2485dfc646bSeric 		**  Check to see that these people are allowed to
2495dfc646bSeric 		**  talk to each other.
2502a6e0786Seric 		*/
2512a6e0786Seric 
25269582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
25369582d2fSeric 		{
25469582d2fSeric 			NoReturn = TRUE;
25508b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
25681161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
25769582d2fSeric 			continue;
25869582d2fSeric 		}
259fabb3bd4Seric 		rcode = checkcompat(to, e);
2601793c9c5Seric 		if (rcode != EX_OK)
2615dfc646bSeric 		{
26281161401Seric 			giveresponse(rcode, m, NULL, e);
2635dfc646bSeric 			continue;
2645dfc646bSeric 		}
2652a6e0786Seric 
2662a6e0786Seric 		/*
2679ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2689ec9501bSeric 		**	about them.
26925a99e2eSeric 		*/
27025a99e2eSeric 
27157fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
27225a99e2eSeric 		{
2731d8f1806Seric 			stripquotes(user);
2741d8f1806Seric 			stripquotes(host);
27525a99e2eSeric 		}
27625a99e2eSeric 
277cdb828c5Seric 		/* hack attack -- delivermail compatibility */
278cdb828c5Seric 		if (m == ProgMailer && *user == '|')
279cdb828c5Seric 			user++;
280cdb828c5Seric 
28125a99e2eSeric 		/*
2823efaed6eSeric 		**  If an error message has already been given, don't
2833efaed6eSeric 		**	bother to send to this address.
2843efaed6eSeric 		**
2853efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2863efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2873efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2883efaed6eSeric 		*/
2893efaed6eSeric 
2906cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2913efaed6eSeric 			continue;
2923efaed6eSeric 
293f2fec898Seric 		/* save statistics.... */
294588cad61Seric 		markstats(e, to);
295f2fec898Seric 
2963efaed6eSeric 		/*
29725a99e2eSeric 		**  See if this user name is "special".
29825a99e2eSeric 		**	If the user name has a slash in it, assume that this
29951552439Seric 		**	is a file -- send it off without further ado.  Note
30051552439Seric 		**	that this type of addresses is not processed along
30151552439Seric 		**	with the others, so we fudge on the To person.
30225a99e2eSeric 		*/
30325a99e2eSeric 
3042c017f8dSeric 		if (m == FileMailer)
30525a99e2eSeric 		{
306b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
30781161401Seric 			giveresponse(rcode, m, NULL, e);
308dde5acadSeric 			if (rcode == EX_OK)
309dde5acadSeric 				to->q_flags |= QSENT;
3105dfc646bSeric 			continue;
31125a99e2eSeric 		}
31225a99e2eSeric 
31313bbc08cSeric 		/*
31413bbc08cSeric 		**  Address is verified -- add this user to mailer
31513bbc08cSeric 		**  argv, and add it to the print list of recipients.
31613bbc08cSeric 		*/
31713bbc08cSeric 
318508daeccSeric 		/* link together the chain of recipients */
319508daeccSeric 		to->q_tchain = tochain;
320508daeccSeric 		tochain = to;
321508daeccSeric 
3225dfc646bSeric 		/* create list of users for error messages */
323db8841e9Seric 		(void) strcat(tobuf, ",");
324db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
325588cad61Seric 		define('u', user, e);		/* to user */
326588cad61Seric 		define('z', to->q_home, e);	/* user's home */
3275dfc646bSeric 
328c579ef51Seric 		/*
329508daeccSeric 		**  Expand out this user into argument list.
330c579ef51Seric 		*/
331c579ef51Seric 
332508daeccSeric 		if (!clever)
333c579ef51Seric 		{
334588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3355dfc646bSeric 			*pvp++ = newstr(buf);
3365dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3375dfc646bSeric 			{
3385dfc646bSeric 				/* allow some space for trailing parms */
3395dfc646bSeric 				break;
3405dfc646bSeric 			}
3415dfc646bSeric 		}
342c579ef51Seric 	}
3435dfc646bSeric 
344145b49b1Seric 	/* see if any addresses still exist */
345145b49b1Seric 	if (tobuf[0] == '\0')
346c579ef51Seric 	{
347588cad61Seric 		define('g', (char *) NULL, e);
348145b49b1Seric 		return (0);
349c579ef51Seric 	}
350145b49b1Seric 
3515dfc646bSeric 	/* print out messages as full list */
35263780dbdSeric 	e->e_to = tobuf + 1;
3535dfc646bSeric 
3545dfc646bSeric 	/*
3555dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3565dfc646bSeric 	*/
3575dfc646bSeric 
358c579ef51Seric 	while (!clever && *++mvp != NULL)
3595dfc646bSeric 	{
360588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
3615dfc646bSeric 		*pvp++ = newstr(buf);
3625dfc646bSeric 		if (pvp >= &pv[MAXPV])
36308b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
3645dfc646bSeric 	}
3655dfc646bSeric 	*pvp++ = NULL;
3665dfc646bSeric 
36725a99e2eSeric 	/*
36825a99e2eSeric 	**  Call the mailer.
3696328bdf7Seric 	**	The argument vector gets built, pipes
37025a99e2eSeric 	**	are created as necessary, and we fork & exec as
3716328bdf7Seric 	**	appropriate.
372c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
37325a99e2eSeric 	*/
37425a99e2eSeric 
3757ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
37686b26461Seric 		ctladdr = &e->e_from;
377134746fbSeric #ifdef NAMED_BIND
3782bcc6d2dSeric 	if (ConfigLevel < 2)
379912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
380134746fbSeric #endif
381b31e7f2bSeric 	mci = openmailer(m, pv, ctladdr, clever, e);
382b31e7f2bSeric 	if (mci == NULL)
383134746fbSeric 	{
384b31e7f2bSeric 		/* catastrophic error */
385845e533cSeric 		rcode = EX_OSERR;
386b31e7f2bSeric 		goto give_up;
387b31e7f2bSeric 	}
388b31e7f2bSeric 	else if (mci->mci_state != MCIS_OPEN)
389b31e7f2bSeric 	{
390b31e7f2bSeric 		/* couldn't open the mailer */
391b31e7f2bSeric 		rcode = mci->mci_exitstat;
3922a6bc25bSeric 		errno = mci->mci_errno;
393b31e7f2bSeric 		if (rcode == EX_OK)
394b31e7f2bSeric 		{
395b31e7f2bSeric 			/* shouldn't happen */
39608b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
3976b0f339dSeric 				rcode, mci->mci_state, firstsig);
398b31e7f2bSeric 			rcode = EX_SOFTWARE;
399b31e7f2bSeric 		}
400b31e7f2bSeric 	}
401b31e7f2bSeric 	else if (!clever)
402b31e7f2bSeric 	{
403b31e7f2bSeric 		/*
404b31e7f2bSeric 		**  Format and send message.
405b31e7f2bSeric 		*/
40615d084d5Seric 
407b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
408b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
409b31e7f2bSeric 		putline("\n", mci->mci_out, m);
410b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
411b31e7f2bSeric 
412b31e7f2bSeric 		/* get the exit status */
413*c9be6216Seric 		rcode = endmailer(mci, e, pv);
414134746fbSeric 	}
415134746fbSeric 	else
416b31e7f2bSeric #ifdef SMTP
417134746fbSeric 	{
418b31e7f2bSeric 		/*
419b31e7f2bSeric 		**  Send the MAIL FROM: protocol
420b31e7f2bSeric 		*/
42115d084d5Seric 
422b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
423b31e7f2bSeric 		if (rcode == EX_OK)
42475889e88Seric 		{
425ded0d3daSkarels 			register char *t = tobuf;
426ded0d3daSkarels 			register int i;
427ded0d3daSkarels 
428588cad61Seric 			/* send the recipient list */
42963780dbdSeric 			tobuf[0] = '\0';
43075889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
43175889e88Seric 			{
43263780dbdSeric 				e->e_to = to->q_paddr;
43315d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
43475889e88Seric 				{
43583b7ddc9Seric 					markfailure(e, to, i);
43681161401Seric 					giveresponse(i, m, mci, e);
43763780dbdSeric 				}
43875889e88Seric 				else
43975889e88Seric 				{
440911693bfSbostic 					*t++ = ',';
441b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
442b31e7f2bSeric 						continue;
443588cad61Seric 				}
444588cad61Seric 			}
445588cad61Seric 
44663780dbdSeric 			/* now send the data */
44763780dbdSeric 			if (tobuf[0] == '\0')
448b31e7f2bSeric 			{
44963780dbdSeric 				e->e_to = NULL;
450b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
451b31e7f2bSeric 					smtprset(m, mci, e);
452b31e7f2bSeric 			}
45375889e88Seric 			else
45475889e88Seric 			{
45563780dbdSeric 				e->e_to = tobuf + 1;
45675889e88Seric 				rcode = smtpdata(m, mci, e);
45763780dbdSeric 			}
45863780dbdSeric 
45963780dbdSeric 			/* now close the connection */
460b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
46115d084d5Seric 				smtpquit(m, mci, e);
46263780dbdSeric 		}
463c579ef51Seric 	}
464b31e7f2bSeric #else /* not SMTP */
465a05b3449Sbostic 	{
46608b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
467845e533cSeric 		rcode = EX_CONFIG;
468b31e7f2bSeric 		goto give_up;
469a05b3449Sbostic 	}
470b31e7f2bSeric #endif /* SMTP */
471134746fbSeric #ifdef NAMED_BIND
4722bcc6d2dSeric 	if (ConfigLevel < 2)
473912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
474134746fbSeric #endif
4755dfc646bSeric 
476b31e7f2bSeric 	/* arrange a return receipt if requested */
4778c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
478b31e7f2bSeric 	{
479b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
480b31e7f2bSeric 		/* do we want to send back more info? */
481b31e7f2bSeric 	}
482b31e7f2bSeric 
483c77d1c25Seric 	/*
48463780dbdSeric 	**  Do final status disposal.
48563780dbdSeric 	**	We check for something in tobuf for the SMTP case.
486c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
487c77d1c25Seric 	**		addressees.
488c77d1c25Seric 	*/
489c77d1c25Seric 
490b31e7f2bSeric   give_up:
49163780dbdSeric 	if (tobuf[0] != '\0')
49281161401Seric 		giveresponse(rcode, m, mci, e);
493772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
494b31e7f2bSeric 	{
495dde5acadSeric 		if (rcode != EX_OK)
49683b7ddc9Seric 			markfailure(e, to, rcode);
497dde5acadSeric 		else
498655518ecSeric 		{
499dde5acadSeric 			to->q_flags |= QSENT;
500655518ecSeric 			e->e_nsent++;
501655518ecSeric 		}
502b31e7f2bSeric 	}
503b31e7f2bSeric 
504b31e7f2bSeric 	/*
505b31e7f2bSeric 	**  Restore state and return.
506b31e7f2bSeric 	*/
507c77d1c25Seric 
50835490626Seric 	errno = 0;
509588cad61Seric 	define('g', (char *) NULL, e);
5105826d9d3Seric 	return (rcode);
51125a99e2eSeric }
5125dfc646bSeric /*
51383b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
51483b7ddc9Seric **
51583b7ddc9Seric **	Parameters:
51683b7ddc9Seric **		e -- the envelope we are sending.
51783b7ddc9Seric **		q -- the address to mark.
51883b7ddc9Seric **		rcode -- the code signifying the particular failure.
51983b7ddc9Seric **
52083b7ddc9Seric **	Returns:
52183b7ddc9Seric **		none.
52283b7ddc9Seric **
52383b7ddc9Seric **	Side Effects:
52483b7ddc9Seric **		marks the address (and possibly the envelope) with the
52583b7ddc9Seric **			failure so that an error will be returned or
52683b7ddc9Seric **			the message will be queued, as appropriate.
52783b7ddc9Seric */
52883b7ddc9Seric 
52983b7ddc9Seric markfailure(e, q, rcode)
53083b7ddc9Seric 	register ENVELOPE *e;
53183b7ddc9Seric 	register ADDRESS *q;
53283b7ddc9Seric 	int rcode;
53383b7ddc9Seric {
53419c47125Seric 	char buf[MAXLINE];
53519c47125Seric 	extern char *pintvl();
53619c47125Seric 
53783b7ddc9Seric 	if (rcode == EX_OK)
53883b7ddc9Seric 		return;
539ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
54083b7ddc9Seric 		q->q_flags |= QBADADDR;
54119c47125Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
54283b7ddc9Seric 	{
54383b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
544198d9be0Seric 		{
545198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
54619c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
547198d9be0Seric 			if (e->e_message != NULL)
548198d9be0Seric 				free(e->e_message);
549198d9be0Seric 			e->e_message = newstr(buf);
55008b25121Seric 			message(buf);
551198d9be0Seric 		}
55283b7ddc9Seric 		q->q_flags |= QBADADDR;
55383b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
554c13b5dfcSeric 		fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr);
55583b7ddc9Seric 	}
55683b7ddc9Seric 	else
55719c47125Seric 	{
55883b7ddc9Seric 		q->q_flags |= QQUEUEUP;
55919c47125Seric 		if (TimeOuts.to_q_warning > 0 &&
56019c47125Seric 		    curtime() > e->e_ctime + TimeOuts.to_q_warning)
56119c47125Seric 		{
56219c47125Seric 			if (!bitset(EF_WARNING, e->e_flags) &&
56319c47125Seric 			    e->e_class >= 0)
56419c47125Seric 			{
56519c47125Seric 				(void) sprintf(buf,
56619c47125Seric 					"warning: cannot send message for %s",
56719c47125Seric 					pintvl(TimeOuts.to_q_warning, FALSE));
56819c47125Seric 				if (e->e_message != NULL)
56919c47125Seric 					free(e->e_message);
57019c47125Seric 				e->e_message = newstr(buf);
57119c47125Seric 				message(buf);
57219c47125Seric 				e->e_flags |= EF_WARNING|EF_TIMEOUT;
57319c47125Seric 			}
57419c47125Seric 			fprintf(e->e_xfp,
57519c47125Seric 				"%s... Warning: message still undelivered after %s\n",
57619c47125Seric 				q->q_paddr, pintvl(TimeOuts.to_q_warning, FALSE));
57719c47125Seric 			fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
57819c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
57919c47125Seric 		}
58019c47125Seric 	}
58183b7ddc9Seric }
58283b7ddc9Seric /*
58332d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
58432d19d43Seric **
58532d19d43Seric **	This MUST be a macro, since after a vfork we are running
58632d19d43Seric **	two processes on the same stack!!!
58732d19d43Seric **
58832d19d43Seric **	Parameters:
58932d19d43Seric **		none.
59032d19d43Seric **
59132d19d43Seric **	Returns:
59232d19d43Seric **		From a macro???  You've got to be kidding!
59332d19d43Seric **
59432d19d43Seric **	Side Effects:
59532d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
59632d19d43Seric **			pid of child in parent, zero in child.
59732d19d43Seric **			-1 on unrecoverable error.
59832d19d43Seric **
59932d19d43Seric **	Notes:
60032d19d43Seric **		I'm awfully sorry this looks so awful.  That's
60132d19d43Seric **		vfork for you.....
60232d19d43Seric */
60332d19d43Seric 
60432d19d43Seric # define NFORKTRIES	5
60584f7cd1bSeric 
60684f7cd1bSeric # ifndef FORK
60784f7cd1bSeric # define FORK	fork
60884f7cd1bSeric # endif
60932d19d43Seric 
61032d19d43Seric # define DOFORK(fORKfN) \
61132d19d43Seric {\
61232d19d43Seric 	register int i;\
61332d19d43Seric \
61411799049Seric 	for (i = NFORKTRIES; --i >= 0; )\
61532d19d43Seric 	{\
61632d19d43Seric 		pid = fORKfN();\
61732d19d43Seric 		if (pid >= 0)\
61832d19d43Seric 			break;\
61911799049Seric 		if (i > 0)\
6206c4635f6Seric 			sleep((unsigned) NFORKTRIES - i);\
62132d19d43Seric 	}\
62232d19d43Seric }
62332d19d43Seric /*
6242ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
6252ed72599Seric **
6262ed72599Seric **	Parameters:
6272ed72599Seric **		none.
6282ed72599Seric **
6292ed72599Seric **	Returns:
6302ed72599Seric **		pid of child in parent.
6312ed72599Seric **		zero in child.
6322ed72599Seric **		-1 on error.
6332ed72599Seric **
6342ed72599Seric **	Side Effects:
6352ed72599Seric **		returns twice, once in parent and once in child.
6362ed72599Seric */
6372ed72599Seric 
6382ed72599Seric dofork()
6392ed72599Seric {
6402ed72599Seric 	register int pid;
6412ed72599Seric 
6422ed72599Seric 	DOFORK(fork);
6432ed72599Seric 	return (pid);
6442ed72599Seric }
6452ed72599Seric /*
646c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
647c579ef51Seric **
648c579ef51Seric **	We should never get fatal errors (e.g., segmentation
649c579ef51Seric **	violation), so we report those specially.  For other
650c579ef51Seric **	errors, we choose a status message (into statmsg),
651c579ef51Seric **	and if it represents an error, we print it.
652c579ef51Seric **
653c579ef51Seric **	Parameters:
654c579ef51Seric **		pid -- pid of mailer.
655*c9be6216Seric **		e -- the current envelope.
656*c9be6216Seric **		pv -- the parameter vector that invoked the mailer
657*c9be6216Seric **			(for error messages).
658c579ef51Seric **
659c579ef51Seric **	Returns:
660c579ef51Seric **		exit code of mailer.
661c579ef51Seric **
662c579ef51Seric **	Side Effects:
663c579ef51Seric **		none.
664c579ef51Seric */
665c579ef51Seric 
666*c9be6216Seric endmailer(mci, e, pv)
667b31e7f2bSeric 	register MCI *mci;
668*c9be6216Seric 	register ENVELOPE *e;
669*c9be6216Seric 	char **pv;
670c579ef51Seric {
671588cad61Seric 	int st;
672c579ef51Seric 
67375889e88Seric 	/* close any connections */
67475889e88Seric 	if (mci->mci_in != NULL)
675*c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
67675889e88Seric 	if (mci->mci_out != NULL)
677*c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
67875889e88Seric 	mci->mci_in = mci->mci_out = NULL;
67975889e88Seric 	mci->mci_state = MCIS_CLOSED;
68075889e88Seric 
68133db8731Seric 	/* in the IPC case there is nothing to wait for */
68275889e88Seric 	if (mci->mci_pid == 0)
68333db8731Seric 		return (EX_OK);
68433db8731Seric 
68533db8731Seric 	/* wait for the mailer process to die and collect status */
68675889e88Seric 	st = waitfor(mci->mci_pid);
687588cad61Seric 	if (st == -1)
68878de67c1Seric 	{
689*c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
690588cad61Seric 		return (EX_SOFTWARE);
691c579ef51Seric 	}
69233db8731Seric 
69333db8731Seric 	/* see if it died a horrid death */
694c579ef51Seric 	if ((st & 0377) != 0)
695c579ef51Seric 	{
696*c9be6216Seric 		syserr("mailer %s died with signal %o", pv[0], st);
697*c9be6216Seric 
698*c9be6216Seric 		/* log the arguments */
699*c9be6216Seric 		if (e->e_xfp != NULL)
700*c9be6216Seric 		{
701*c9be6216Seric 			register char **av;
702*c9be6216Seric 
703*c9be6216Seric 			fprintf(e->e_xfp, "Arguments:");
704*c9be6216Seric 			for (av = pv; *av != NULL; av++)
705*c9be6216Seric 				fprintf(e->e_xfp, " %s", *av);
706*c9be6216Seric 			fprintf(e->e_xfp, "\n");
707*c9be6216Seric 		}
708*c9be6216Seric 
7095f73204aSeric 		ExitStat = EX_TEMPFAIL;
7105f73204aSeric 		return (EX_TEMPFAIL);
711c579ef51Seric 	}
71233db8731Seric 
71333db8731Seric 	/* normal death -- return status */
714588cad61Seric 	st = (st >> 8) & 0377;
715588cad61Seric 	return (st);
716c579ef51Seric }
717c579ef51Seric /*
718c579ef51Seric **  OPENMAILER -- open connection to mailer.
719c579ef51Seric **
720c579ef51Seric **	Parameters:
721c579ef51Seric **		m -- mailer descriptor.
722c579ef51Seric **		pvp -- parameter vector to pass to mailer.
723c579ef51Seric **		ctladdr -- controlling address for user.
724c579ef51Seric **		clever -- create a full duplex connection.
725c579ef51Seric **
726c579ef51Seric **	Returns:
72775889e88Seric **		The mail connection info struct for this connection.
72875889e88Seric **		NULL on failure.
729c579ef51Seric **
730c579ef51Seric **	Side Effects:
731c579ef51Seric **		creates a mailer in a subprocess.
732c579ef51Seric */
733c579ef51Seric 
734b31e7f2bSeric MCI *
735b31e7f2bSeric openmailer(m, pvp, ctladdr, clever, e)
736588cad61Seric 	MAILER *m;
737c579ef51Seric 	char **pvp;
738c579ef51Seric 	ADDRESS *ctladdr;
739c579ef51Seric 	bool clever;
740b31e7f2bSeric 	ENVELOPE *e;
741c579ef51Seric {
7425dfc646bSeric 	int pid;
743b31e7f2bSeric 	register MCI *mci;
744f8952a83Seric 	int mpvect[2];
745c579ef51Seric 	int rpvect[2];
7465dfc646bSeric 	extern FILE *fdopen();
7475dfc646bSeric 
7486ef48975Seric 	if (tTd(11, 1))
7495dfc646bSeric 	{
7508c57e552Seric 		printf("openmailer:");
7515dfc646bSeric 		printav(pvp);
7525dfc646bSeric 	}
75335490626Seric 	errno = 0;
7545dfc646bSeric 
755ef66a9d0Seric 	CurHostName = m->m_mailer;
756ef66a9d0Seric 
75733db8731Seric 	/*
75833db8731Seric 	**  Deal with the special case of mail handled through an IPC
75933db8731Seric 	**  connection.
76033db8731Seric 	**	In this case we don't actually fork.  We must be
76133db8731Seric 	**	running SMTP for this to work.  We will return a
76233db8731Seric 	**	zero pid to indicate that we are running IPC.
763e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
76433db8731Seric 	*/
76533db8731Seric 
766e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
767e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
768e7c1bd78Seric 	{
769b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
7702a087e90Seric 		bzero((char *) mci, sizeof *mci);
77175889e88Seric 		mci->mci_in = stdin;
77275889e88Seric 		mci->mci_out = stdout;
773b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
77415d084d5Seric 		mci->mci_mailer = m;
775e7c1bd78Seric 	}
776b31e7f2bSeric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
777914346b1Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
77833db8731Seric 	{
77984f7cd1bSeric #ifdef DAEMON
780e103b48fSeric 		register int i;
7811277f9a8Seric 		register u_short port;
782e103b48fSeric 		char *curhost;
783b31e7f2bSeric 		extern MCI *mci_get();
784e103b48fSeric 		extern char *hostsignature();
78533db8731Seric 
786ef66a9d0Seric 		CurHostName = pvp[1];
787e103b48fSeric 		curhost = hostsignature(m, pvp[1], e);
788b31e7f2bSeric 
78978bfb85bSeric 		if (curhost == NULL || curhost[0] == '\0')
79078bfb85bSeric 		{
79178bfb85bSeric 			syserr("null signature");
79278bfb85bSeric 			return NULL;
79378bfb85bSeric 		}
79478bfb85bSeric 
79533db8731Seric 		if (!clever)
79678bfb85bSeric 		{
79708b25121Seric 			syserr("554 non-clever IPC");
79878bfb85bSeric 			return NULL;
79978bfb85bSeric 		}
80093b6e3cfSeric 		if (pvp[2] != NULL)
8011277f9a8Seric 			port = atoi(pvp[2]);
80293b6e3cfSeric 		else
8031277f9a8Seric 			port = 0;
804e103b48fSeric 		while (*curhost != '\0')
805ebc61751Sbloom 		{
806e103b48fSeric 			register char *p;
8077d55540cSeric 			static char hostbuf[MAXNAME];
808e103b48fSeric 
809e103b48fSeric 			/* pull the next host from the signature */
810e103b48fSeric 			p = strchr(curhost, ':');
811e103b48fSeric 			if (p == NULL)
812e103b48fSeric 				p = &curhost[strlen(curhost)];
813e103b48fSeric 			strncpy(hostbuf, curhost, p - curhost);
814e103b48fSeric 			hostbuf[p - curhost] = '\0';
815e103b48fSeric 			if (*p != '\0')
816e103b48fSeric 				p++;
817e103b48fSeric 			curhost = p;
818e103b48fSeric 
81945a8316eSeric 			/* see if we already know that this host is fried */
820e103b48fSeric 			CurHostName = hostbuf;
821e103b48fSeric 			mci = mci_get(hostbuf, m);
822b31e7f2bSeric 			if (mci->mci_state != MCIS_CLOSED)
8231fa7c2ebSeric 			{
8241fa7c2ebSeric 				if (tTd(11, 1))
8251fa7c2ebSeric 				{
8261fa7c2ebSeric 					printf("openmailer: ");
8271fa7c2ebSeric 					mci_dump(mci);
8281fa7c2ebSeric 				}
82967088a9dSeric 				CurHostName = mci->mci_host;
83075889e88Seric 				return mci;
8311fa7c2ebSeric 			}
83215d084d5Seric 			mci->mci_mailer = m;
833b31e7f2bSeric 			if (mci->mci_exitstat != EX_OK)
834b31e7f2bSeric 				continue;
835b31e7f2bSeric 
83675889e88Seric 			/* try the connection */
837e103b48fSeric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
83808b25121Seric 			message("Connecting to %s (%s)...",
839e103b48fSeric 				hostbuf, m->m_name);
840e103b48fSeric 			i = makeconnection(hostbuf, port, mci,
841914346b1Seric 				bitnset(M_SECURE_PORT, m->m_flags));
84275889e88Seric 			mci->mci_exitstat = i;
84375889e88Seric 			mci->mci_errno = errno;
84475889e88Seric 			if (i == EX_OK)
845b31e7f2bSeric 			{
846b31e7f2bSeric 				mci->mci_state = MCIS_OPENING;
847b31e7f2bSeric 				mci_cache(mci);
848b31e7f2bSeric 				break;
849b31e7f2bSeric 			}
850b31e7f2bSeric 			else if (tTd(11, 1))
851b31e7f2bSeric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
852b31e7f2bSeric 					i, errno);
853b31e7f2bSeric 
85475889e88Seric 
8555f73204aSeric 			/* enter status of this host */
85675889e88Seric 			setstat(i);
857ed854c7bSeric 		}
8582a087e90Seric 		mci->mci_pid = 0;
859b31e7f2bSeric #else /* no DAEMON */
86008b25121Seric 		syserr("554 openmailer: no IPC");
8611fa7c2ebSeric 		if (tTd(11, 1))
8621fa7c2ebSeric 			printf("openmailer: NULL\n");
86375889e88Seric 		return NULL;
864b31e7f2bSeric #endif /* DAEMON */
865588cad61Seric 	}
866b31e7f2bSeric 	else
867b31e7f2bSeric 	{
8686328bdf7Seric 		/* create a pipe to shove the mail through */
869f8952a83Seric 		if (pipe(mpvect) < 0)
87025a99e2eSeric 		{
871588cad61Seric 			syserr("openmailer: pipe (to mailer)");
8721fa7c2ebSeric 			if (tTd(11, 1))
8731fa7c2ebSeric 				printf("openmailer: NULL\n");
87475889e88Seric 			return NULL;
87525a99e2eSeric 		}
876c579ef51Seric 
877c579ef51Seric 		/* if this mailer speaks smtp, create a return pipe */
878c579ef51Seric 		if (clever && pipe(rpvect) < 0)
879c579ef51Seric 		{
880588cad61Seric 			syserr("openmailer: pipe (from mailer)");
881c579ef51Seric 			(void) close(mpvect[0]);
882c579ef51Seric 			(void) close(mpvect[1]);
8831fa7c2ebSeric 			if (tTd(11, 1))
8841fa7c2ebSeric 				printf("openmailer: NULL\n");
88575889e88Seric 			return NULL;
886c579ef51Seric 		}
887c579ef51Seric 
88833db8731Seric 		/*
88933db8731Seric 		**  Actually fork the mailer process.
89033db8731Seric 		**	DOFORK is clever about retrying.
8916984bfddSeric 		**
8926984bfddSeric 		**	Dispose of SIGCHLD signal catchers that may be laying
8936984bfddSeric 		**	around so that endmail will get it.
89433db8731Seric 		*/
89533db8731Seric 
896b31e7f2bSeric 		if (e->e_xfp != NULL)
897b31e7f2bSeric 			(void) fflush(e->e_xfp);		/* for debugging */
898588cad61Seric 		(void) fflush(stdout);
8996984bfddSeric # ifdef SIGCHLD
9006984bfddSeric 		(void) signal(SIGCHLD, SIG_DFL);
9016c2c3107Seric # endif /* SIGCHLD */
90284f7cd1bSeric 		DOFORK(FORK);
903f129ec7dSeric 		/* pid is set by DOFORK */
90425a99e2eSeric 		if (pid < 0)
90525a99e2eSeric 		{
90633db8731Seric 			/* failure */
907588cad61Seric 			syserr("openmailer: cannot fork");
908f8952a83Seric 			(void) close(mpvect[0]);
909f8952a83Seric 			(void) close(mpvect[1]);
910c579ef51Seric 			if (clever)
911c579ef51Seric 			{
912c579ef51Seric 				(void) close(rpvect[0]);
913c579ef51Seric 				(void) close(rpvect[1]);
914c579ef51Seric 			}
9151fa7c2ebSeric 			if (tTd(11, 1))
9161fa7c2ebSeric 				printf("openmailer: NULL\n");
91775889e88Seric 			return NULL;
91825a99e2eSeric 		}
91925a99e2eSeric 		else if (pid == 0)
92025a99e2eSeric 		{
92113088b9fSeric 			int i;
922dafbc479Seric 			int saveerrno;
92380c5307eSeric 			char **ep;
92480c5307eSeric 			char *env[MAXUSERENVIRON];
92580c5307eSeric 			extern char **environ;
9265f73204aSeric 			extern int DtableSize;
92713088b9fSeric 
92825a99e2eSeric 			/* child -- set up input & exec mailer */
92903ab8e55Seric 			/* make diagnostic output be standard output */
9308f0e7860Seric 			(void) signal(SIGINT, SIG_IGN);
9318f0e7860Seric 			(void) signal(SIGHUP, SIG_IGN);
9320984da9fSeric 			(void) signal(SIGTERM, SIG_DFL);
933f8952a83Seric 
9341caa9a59Seric 			/* close any other cached connections */
9351caa9a59Seric 			mci_flush(FALSE, mci);
9361caa9a59Seric 
937b31e7f2bSeric 			/* arrange to filter std & diag output of command */
938c579ef51Seric 			if (clever)
939c579ef51Seric 			{
940c579ef51Seric 				(void) close(rpvect[0]);
94165886f3dSeric 				(void) dup2(rpvect[1], STDOUT_FILENO);
942c579ef51Seric 				(void) close(rpvect[1]);
943c579ef51Seric 			}
944276723a8Seric 			else if (OpMode == MD_SMTP || HoldErrs)
945f8952a83Seric 			{
946588cad61Seric 				/* put mailer output in transcript */
94765886f3dSeric 				(void) dup2(fileno(e->e_xfp), STDOUT_FILENO);
948f8952a83Seric 			}
94965886f3dSeric 			(void) dup2(STDOUT_FILENO, STDERR_FILENO);
950f8952a83Seric 
951f8952a83Seric 			/* arrange to get standard input */
952f8952a83Seric 			(void) close(mpvect[1]);
95365886f3dSeric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
95425a99e2eSeric 			{
95525a99e2eSeric 				syserr("Cannot dup to zero!");
956a590b978Seric 				_exit(EX_OSERR);
95725a99e2eSeric 			}
958f8952a83Seric 			(void) close(mpvect[0]);
95957fc6f17Seric 			if (!bitnset(M_RESTR, m->m_flags))
9600984da9fSeric 			{
96153e3fa05Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
962e36b99e2Seric 				{
963e36b99e2Seric 					(void) setgid(DefGid);
964898a126bSbostic 					(void) initgroups(DefUser, DefGid);
965e36b99e2Seric 					(void) setuid(DefUid);
966e36b99e2Seric 				}
967e36b99e2Seric 				else
96869f29479Seric 				{
969e36b99e2Seric 					(void) setgid(ctladdr->q_gid);
970898a126bSbostic 					(void) initgroups(ctladdr->q_ruser?
971898a126bSbostic 						ctladdr->q_ruser: ctladdr->q_user,
972898a126bSbostic 						ctladdr->q_gid);
973e36b99e2Seric 					(void) setuid(ctladdr->q_uid);
97469f29479Seric 				}
9750984da9fSeric 			}
976588cad61Seric 
97713088b9fSeric 			/* arrange for all the files to be closed */
978b31e7f2bSeric 			for (i = 3; i < DtableSize; i++)
979b31e7f2bSeric 			{
980fccb3f7aSbostic 				register int j;
981fccb3f7aSbostic 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
982fccb3f7aSbostic 					(void)fcntl(i, F_SETFD, j|1);
983fccb3f7aSbostic 			}
98433db8731Seric 
98580c5307eSeric 			/* set up the mailer environment */
98680c5307eSeric 			i = 0;
98780c5307eSeric 			env[i++] = "AGENT=sendmail";
98880c5307eSeric 			for (ep = environ; *ep != NULL; ep++)
98980c5307eSeric 			{
99080c5307eSeric 				if (strncmp(*ep, "TZ=", 3) == 0)
99180c5307eSeric 					env[i++] = *ep;
99280c5307eSeric 			}
99380c5307eSeric 			env[i++] = NULL;
99480c5307eSeric 
99533db8731Seric 			/* try to execute the mailer */
996d7a0480eSeric 			execve(m->m_mailer, pvp, env);
997dafbc479Seric 			saveerrno = errno;
99813088b9fSeric 			syserr("Cannot exec %s", m->m_mailer);
99995b76e4bSeric 			if (m == LocalMailer)
100055f33c03Seric 				_exit(EX_TEMPFAIL);
1001e2de2524Seric 			if (transienterror(saveerrno))
100295b76e4bSeric 				_exit(EX_TEMPFAIL);
1003a590b978Seric 			_exit(EX_UNAVAILABLE);
100425a99e2eSeric 		}
100525a99e2eSeric 
1006f8952a83Seric 		/*
1007c579ef51Seric 		**  Set up return value.
1008f8952a83Seric 		*/
1009f8952a83Seric 
1010b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
10112a087e90Seric 		bzero((char *) mci, sizeof *mci);
101215d084d5Seric 		mci->mci_mailer = m;
1013b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
10142a087e90Seric 		mci->mci_pid = pid;
1015f8952a83Seric 		(void) close(mpvect[0]);
101675889e88Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1017c579ef51Seric 		if (clever)
101825a99e2eSeric 		{
1019c579ef51Seric 			(void) close(rpvect[1]);
102075889e88Seric 			mci->mci_in = fdopen(rpvect[0], "r");
102175889e88Seric 		}
102275889e88Seric 		else
102375889e88Seric 		{
102475889e88Seric 			mci->mci_flags |= MCIF_TEMP;
102575889e88Seric 			mci->mci_in = NULL;
102675889e88Seric 		}
1027b31e7f2bSeric 	}
1028b31e7f2bSeric 
1029b31e7f2bSeric 	/*
1030b31e7f2bSeric 	**  If we are in SMTP opening state, send initial protocol.
1031b31e7f2bSeric 	*/
1032b31e7f2bSeric 
1033b31e7f2bSeric 	if (clever && mci->mci_state != MCIS_CLOSED)
1034b31e7f2bSeric 	{
1035b31e7f2bSeric 		smtpinit(m, mci, e);
1036b31e7f2bSeric 	}
10371fa7c2ebSeric 	if (tTd(11, 1))
10381fa7c2ebSeric 	{
10391fa7c2ebSeric 		printf("openmailer: ");
10401fa7c2ebSeric 		mci_dump(mci);
10411fa7c2ebSeric 	}
1042c579ef51Seric 
104375889e88Seric 	return mci;
104425a99e2eSeric }
104525a99e2eSeric /*
104625a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
104725a99e2eSeric **
104825a99e2eSeric **	Parameters:
104925a99e2eSeric **		stat -- the status code from the mailer (high byte
105025a99e2eSeric **			only; core dumps must have been taken care of
105125a99e2eSeric **			already).
105281161401Seric **		m -- the mailer info for this mailer.
105381161401Seric **		mci -- the mailer connection info -- can be NULL if the
105481161401Seric **			response is given before the connection is made.
105581161401Seric **		e -- the current envelope.
105625a99e2eSeric **
105725a99e2eSeric **	Returns:
1058db8841e9Seric **		none.
105925a99e2eSeric **
106025a99e2eSeric **	Side Effects:
1061c1f9df2cSeric **		Errors may be incremented.
106225a99e2eSeric **		ExitStat may be set.
106325a99e2eSeric */
106425a99e2eSeric 
106581161401Seric giveresponse(stat, m, mci, e)
106625a99e2eSeric 	int stat;
1067588cad61Seric 	register MAILER *m;
106881161401Seric 	register MCI *mci;
1069198d9be0Seric 	ENVELOPE *e;
107025a99e2eSeric {
107125a99e2eSeric 	register char *statmsg;
107225a99e2eSeric 	extern char *SysExMsg[];
107325a99e2eSeric 	register int i;
1074d4bd8f0eSbostic 	extern int N_SysEx;
1075d4bd8f0eSbostic #ifdef NAMED_BIND
1076d4bd8f0eSbostic 	extern int h_errno;
1077d4bd8f0eSbostic #endif
1078198d9be0Seric 	char buf[MAXLINE];
10797d55540cSeric 	extern char *errstring();
108025a99e2eSeric 
108113bbc08cSeric 	/*
108213bbc08cSeric 	**  Compute status message from code.
108313bbc08cSeric 	*/
108413bbc08cSeric 
108525a99e2eSeric 	i = stat - EX__BASE;
1086588cad61Seric 	if (stat == 0)
1087588cad61Seric 		statmsg = "250 Sent";
1088588cad61Seric 	else if (i < 0 || i > N_SysEx)
1089588cad61Seric 	{
1090588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1091588cad61Seric 		stat = EX_UNAVAILABLE;
1092588cad61Seric 		statmsg = buf;
1093588cad61Seric 	}
1094198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1095198d9be0Seric 	{
10967d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1097d4bd8f0eSbostic #ifdef NAMED_BIND
1098f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1099f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1100f28da541Smiriam 		else
1101d4bd8f0eSbostic #endif
1102f28da541Smiriam 		{
11038557d168Seric 			if (errno != 0)
1104d87e85f3Seric 				statmsg = errstring(errno);
1105d87e85f3Seric 			else
1106d87e85f3Seric 			{
1107d87e85f3Seric #ifdef SMTP
1108d87e85f3Seric 				extern char SmtpError[];
1109d87e85f3Seric 
1110d87e85f3Seric 				statmsg = SmtpError;
11116c2c3107Seric #else /* SMTP */
1112d87e85f3Seric 				statmsg = NULL;
11136c2c3107Seric #endif /* SMTP */
1114d87e85f3Seric 			}
1115f28da541Smiriam 		}
1116d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1117d87e85f3Seric 		{
111887c9b3e7Seric 			(void) strcat(buf, ": ");
1119d87e85f3Seric 			(void) strcat(buf, statmsg);
11208557d168Seric 		}
1121198d9be0Seric 		statmsg = buf;
1122198d9be0Seric 	}
112325a99e2eSeric 	else
1124d87e85f3Seric 	{
112525a99e2eSeric 		statmsg = SysExMsg[i];
11267d55540cSeric 		if (*statmsg++ == ':')
11277d55540cSeric 		{
11287d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
11297d55540cSeric 			statmsg = buf;
11307d55540cSeric 		}
1131d87e85f3Seric 	}
1132588cad61Seric 
1133588cad61Seric 	/*
1134588cad61Seric 	**  Print the message as appropriate
1135588cad61Seric 	*/
1136588cad61Seric 
1137198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1138e12d03eeSeric 		message(&statmsg[4], errstring(errno));
113925a99e2eSeric 	else
114025a99e2eSeric 	{
1141c1f9df2cSeric 		Errors++;
1142e12d03eeSeric 		usrerr(statmsg, errstring(errno));
114325a99e2eSeric 	}
114425a99e2eSeric 
114525a99e2eSeric 	/*
114625a99e2eSeric 	**  Final cleanup.
114725a99e2eSeric 	**	Log a record of the transaction.  Compute the new
114825a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
114925a99e2eSeric 	**	that.
115025a99e2eSeric 	*/
115125a99e2eSeric 
11522f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
115381161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1154eb238f8cSeric 
1155eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1156eb238f8cSeric 		setstat(stat);
1157198d9be0Seric 	if (stat != EX_OK)
1158198d9be0Seric 	{
1159198d9be0Seric 		if (e->e_message != NULL)
1160198d9be0Seric 			free(e->e_message);
1161198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1162198d9be0Seric 	}
11638557d168Seric 	errno = 0;
1164d4bd8f0eSbostic #ifdef NAMED_BIND
1165f28da541Smiriam 	h_errno = 0;
1166d4bd8f0eSbostic #endif
1167eb238f8cSeric }
1168eb238f8cSeric /*
1169eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1170eb238f8cSeric **
1171eb238f8cSeric **	Parameters:
117281161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
117381161401Seric **		mci -- the mailer connection info -- can be NULL if the
117481161401Seric **			log is occuring when no connection is active.
117581161401Seric **		stat -- the message to print for the status.
117681161401Seric **		e -- the current envelope.
1177eb238f8cSeric **
1178eb238f8cSeric **	Returns:
1179eb238f8cSeric **		none
1180eb238f8cSeric **
1181eb238f8cSeric **	Side Effects:
1182eb238f8cSeric **		none
1183eb238f8cSeric */
1184eb238f8cSeric 
118581161401Seric logdelivery(m, mci, stat, e)
118681161401Seric 	MAILER *m;
118781161401Seric 	register MCI *mci;
1188eb238f8cSeric 	char *stat;
1189b31e7f2bSeric 	register ENVELOPE *e;
11905cf56be3Seric {
1191eb238f8cSeric # ifdef LOG
11929507d1f9Seric 	char *curhost;
1193d6acf3eeSeric 	char buf[512];
11949507d1f9Seric 	extern char *pintvl();
11959507d1f9Seric 	extern char *macvalue();
11969507d1f9Seric 
119748ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
119881161401Seric 
119948ed5d33Seric 	if (m != NULL)
120071ff6caaSeric 	{
120148ed5d33Seric 		(void) strcat(buf, ", mailer=");
120248ed5d33Seric 		(void) strcat(buf, m->m_name);
120371ff6caaSeric 	}
120448ed5d33Seric 
120548ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
120671ff6caaSeric 	{
120771ff6caaSeric # ifdef DAEMON
1208e2f2f828Seric 		extern SOCKADDR CurHostAddr;
1209e2f2f828Seric 		extern char *anynet_ntoa();
121048ed5d33Seric # endif
121171ff6caaSeric 
121248ed5d33Seric 		(void) strcat(buf, ", relay=");
121348ed5d33Seric 		(void) strcat(buf, mci->mci_host);
121448ed5d33Seric 
121548ed5d33Seric # ifdef DAEMON
121648ed5d33Seric 		(void) strcat(buf, " (");
1217e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
121848ed5d33Seric 		(void) strcat(buf, ")");
121971ff6caaSeric # endif
122071ff6caaSeric 	}
12219507d1f9Seric 	else
122248ed5d33Seric 	{
122348ed5d33Seric 		char *p = macvalue('h', e);
12249507d1f9Seric 
122548ed5d33Seric 		if (p != NULL && p[0] != '\0')
122648ed5d33Seric 		{
122748ed5d33Seric 			(void) strcat(buf, ", relay=");
122848ed5d33Seric 			(void) strcat(buf, p);
122948ed5d33Seric 		}
123048ed5d33Seric 	}
1231d6acf3eeSeric 
1232d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1233d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
12346c2c3107Seric # endif /* LOG */
123525a99e2eSeric }
123625a99e2eSeric /*
123751552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
123825a99e2eSeric **
123951552439Seric **	This can be made an arbitrary message separator by changing $l
124051552439Seric **
12419b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
12429b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
12439b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
12449b6c17a6Seric **	this kind of antique garbage????
124525a99e2eSeric **
124625a99e2eSeric **	Parameters:
124751552439Seric **		fp -- the file to output to.
124851552439Seric **		m -- the mailer describing this entry.
124925a99e2eSeric **
125025a99e2eSeric **	Returns:
125151552439Seric **		none
125225a99e2eSeric **
125325a99e2eSeric **	Side Effects:
125451552439Seric **		outputs some text to fp.
125525a99e2eSeric */
125625a99e2eSeric 
1257b31e7f2bSeric putfromline(fp, m, e)
125851552439Seric 	register FILE *fp;
125951552439Seric 	register MAILER *m;
1260b31e7f2bSeric 	ENVELOPE *e;
126125a99e2eSeric {
12622bc47524Seric 	char *template = "\201l\n";
126351552439Seric 	char buf[MAXLINE];
126425a99e2eSeric 
126557fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
126651552439Seric 		return;
126713bbc08cSeric 
12682c7e1b8dSeric # ifdef UGLYUUCP
126957fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
127074b6e67bSeric 	{
1271ea09d6edSeric 		char *bang;
1272ea09d6edSeric 		char xbuf[MAXLINE];
127374b6e67bSeric 
1274ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
12756c2c3107Seric 		bang = strchr(buf, '!');
127674b6e67bSeric 		if (bang == NULL)
127708b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
127874b6e67bSeric 		else
1279588cad61Seric 		{
1280ea09d6edSeric 			*bang++ = '\0';
12812bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1282ea09d6edSeric 			template = xbuf;
128374b6e67bSeric 		}
1284588cad61Seric 	}
12856c2c3107Seric # endif /* UGLYUUCP */
1286b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
128777b52738Seric 	putline(buf, fp, m);
1288bc6e2962Seric }
1289bc6e2962Seric /*
129051552439Seric **  PUTBODY -- put the body of a message.
129151552439Seric **
129251552439Seric **	Parameters:
129351552439Seric **		fp -- file to output onto.
129477b52738Seric **		m -- a mailer descriptor to control output format.
12959a6a5f55Seric **		e -- the envelope to put out.
129651552439Seric **
129751552439Seric **	Returns:
129851552439Seric **		none.
129951552439Seric **
130051552439Seric **	Side Effects:
130151552439Seric **		The message is written onto fp.
130251552439Seric */
130351552439Seric 
130477b52738Seric putbody(fp, m, e)
130551552439Seric 	FILE *fp;
1306588cad61Seric 	MAILER *m;
13079a6a5f55Seric 	register ENVELOPE *e;
130851552439Seric {
130977b52738Seric 	char buf[MAXLINE];
131051552439Seric 
131151552439Seric 	/*
131251552439Seric 	**  Output the body of the message
131351552439Seric 	*/
131451552439Seric 
13159a6a5f55Seric 	if (e->e_dfp == NULL)
131651552439Seric 	{
13179a6a5f55Seric 		if (e->e_df != NULL)
13189a6a5f55Seric 		{
13199a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
13209a6a5f55Seric 			if (e->e_dfp == NULL)
13218f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
13228f9146b0Srick 				e->e_df, e->e_to, e->e_from);
13239a6a5f55Seric 		}
13249a6a5f55Seric 		else
132577b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
13269a6a5f55Seric 	}
13279a6a5f55Seric 	if (e->e_dfp != NULL)
13289a6a5f55Seric 	{
13299a6a5f55Seric 		rewind(e->e_dfp);
133077b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
133124fc8aeeSeric 		{
133224fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1333d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
13343462ad9eSeric 				(void) putc('>', fp);
133577b52738Seric 			putline(buf, fp, m);
133624fc8aeeSeric 		}
133751552439Seric 
13389a6a5f55Seric 		if (ferror(e->e_dfp))
133951552439Seric 		{
134051552439Seric 			syserr("putbody: read error");
134151552439Seric 			ExitStat = EX_IOERR;
134251552439Seric 		}
134351552439Seric 	}
134451552439Seric 
134551552439Seric 	(void) fflush(fp);
134651552439Seric 	if (ferror(fp) && errno != EPIPE)
134751552439Seric 	{
134851552439Seric 		syserr("putbody: write error");
134951552439Seric 		ExitStat = EX_IOERR;
135051552439Seric 	}
135151552439Seric 	errno = 0;
135225a99e2eSeric }
135325a99e2eSeric /*
135425a99e2eSeric **  MAILFILE -- Send a message to a file.
135525a99e2eSeric **
1356f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1357f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1358f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1359f129ec7dSeric **	sendmail runs as root.
1360f129ec7dSeric **
1361588cad61Seric **	This could be done as a subordinate mailer, except that it
1362588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1363588cad61Seric **	view this as being sufficiently important as to include it
1364588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1365588cad61Seric **	to create another process plus some pipes to save the message.
1366588cad61Seric **
136725a99e2eSeric **	Parameters:
136825a99e2eSeric **		filename -- the name of the file to send to.
13696259796dSeric **		ctladdr -- the controlling address header -- includes
13706259796dSeric **			the userid/groupid to be when sending.
137125a99e2eSeric **
137225a99e2eSeric **	Returns:
137325a99e2eSeric **		The exit code associated with the operation.
137425a99e2eSeric **
137525a99e2eSeric **	Side Effects:
137625a99e2eSeric **		none.
137725a99e2eSeric */
137825a99e2eSeric 
1379b31e7f2bSeric mailfile(filename, ctladdr, e)
138025a99e2eSeric 	char *filename;
13816259796dSeric 	ADDRESS *ctladdr;
1382b31e7f2bSeric 	register ENVELOPE *e;
138325a99e2eSeric {
138425a99e2eSeric 	register FILE *f;
138532d19d43Seric 	register int pid;
138615d084d5Seric 	int mode;
138725a99e2eSeric 
138832d19d43Seric 	/*
138932d19d43Seric 	**  Fork so we can change permissions here.
139032d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
139132d19d43Seric 	**	the complications of calling subroutines, etc.
139232d19d43Seric 	*/
139332d19d43Seric 
139432d19d43Seric 	DOFORK(fork);
139532d19d43Seric 
139632d19d43Seric 	if (pid < 0)
139732d19d43Seric 		return (EX_OSERR);
139832d19d43Seric 	else if (pid == 0)
139932d19d43Seric 	{
140032d19d43Seric 		/* child -- actually write to file */
1401f129ec7dSeric 		struct stat stb;
1402f129ec7dSeric 
14030984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
14040984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
14050984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
14063462ad9eSeric 		(void) umask(OldUmask);
140795f16dc0Seric 
1408f129ec7dSeric 		if (stat(filename, &stb) < 0)
1409e6e1265fSeric 			stb.st_mode = 0666;
141015d084d5Seric 		mode = stb.st_mode;
141195f16dc0Seric 
141295f16dc0Seric 		/* limit the errors to those actually caused in the child */
141395f16dc0Seric 		errno = 0;
141495f16dc0Seric 		ExitStat = EX_OK;
141595f16dc0Seric 
1416f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1417f129ec7dSeric 			exit(EX_CANTCREAT);
141803827b5fSeric 		if (ctladdr == NULL)
14198f9146b0Srick 			ctladdr = &e->e_from;
142015d084d5Seric 		else
142115d084d5Seric 		{
142215d084d5Seric 			/* ignore setuid and setgid bits */
142315d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
142415d084d5Seric 		}
142515d084d5Seric 
14268f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
14278f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
14288f9146b0Srick 		{
14298f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
143095f16dc0Seric 			if (e->e_dfp == NULL)
143195f16dc0Seric 			{
14328f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
14338f9146b0Srick 					e->e_df, e->e_to, e->e_from);
14348f9146b0Srick 			}
14358f9146b0Srick 		}
14368f9146b0Srick 
143715d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1438e36b99e2Seric 		{
143995f16dc0Seric 			if (ctladdr->q_uid == 0)
144095f16dc0Seric 			{
1441e36b99e2Seric 				(void) setgid(DefGid);
1442898a126bSbostic 				(void) initgroups(DefUser, DefGid);
144395f16dc0Seric 			}
144495f16dc0Seric 			else
144595f16dc0Seric 			{
14466259796dSeric 				(void) setgid(ctladdr->q_gid);
1447898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1448898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1449898a126bSbostic 					ctladdr->q_gid);
1450898a126bSbostic 			}
1451e36b99e2Seric 		}
145215d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1453e36b99e2Seric 		{
1454e36b99e2Seric 			if (ctladdr->q_uid == 0)
1455e36b99e2Seric 				(void) setuid(DefUid);
1456e36b99e2Seric 			else
14576259796dSeric 				(void) setuid(ctladdr->q_uid);
1458e36b99e2Seric 		}
145995f16dc0Seric 		FileName = filename;
146095f16dc0Seric 		LineNumber = 0;
146127628d59Seric 		f = dfopen(filename, "a");
146225a99e2eSeric 		if (f == NULL)
146395f16dc0Seric 		{
146408b25121Seric 			message("554 cannot open");
146532d19d43Seric 			exit(EX_CANTCREAT);
146695f16dc0Seric 		}
146725a99e2eSeric 
1468b31e7f2bSeric 		putfromline(f, ProgMailer, e);
1469b843d759Seric 		(*e->e_puthdr)(f, ProgMailer, e);
147077b52738Seric 		putline("\n", f, ProgMailer);
1471b843d759Seric 		(*e->e_putbody)(f, ProgMailer, e);
147277b52738Seric 		putline("\n", f, ProgMailer);
147395f16dc0Seric 		if (ferror(f))
147495f16dc0Seric 		{
147508b25121Seric 			message("451 I/O error");
147695f16dc0Seric 			setstat(EX_IOERR);
147795f16dc0Seric 		}
1478ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
147932d19d43Seric 		(void) fflush(stdout);
1480e36b99e2Seric 
148127628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1482c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
148395f16dc0Seric 		exit(ExitStat);
148413bbc08cSeric 		/*NOTREACHED*/
148532d19d43Seric 	}
148632d19d43Seric 	else
148732d19d43Seric 	{
148832d19d43Seric 		/* parent -- wait for exit status */
1489588cad61Seric 		int st;
149032d19d43Seric 
1491588cad61Seric 		st = waitfor(pid);
1492588cad61Seric 		if ((st & 0377) != 0)
1493588cad61Seric 			return (EX_UNAVAILABLE);
1494588cad61Seric 		else
1495588cad61Seric 			return ((st >> 8) & 0377);
14968f9146b0Srick 		/*NOTREACHED*/
149732d19d43Seric 	}
149825a99e2eSeric }
1499ea4dc939Seric /*
1500ea4dc939Seric **  SENDALL -- actually send all the messages.
1501ea4dc939Seric **
1502ea4dc939Seric **	Parameters:
15030c52a0b3Seric **		e -- the envelope to send.
15047b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
1505511ad433Seric **			the current e->e_sendmode.
1506ea4dc939Seric **
1507ea4dc939Seric **	Returns:
1508ea4dc939Seric **		none.
1509ea4dc939Seric **
1510ea4dc939Seric **	Side Effects:
1511ea4dc939Seric **		Scans the send lists and sends everything it finds.
15120c52a0b3Seric **		Delivers any appropriate error messages.
1513276723a8Seric **		If we are running in a non-interactive mode, takes the
1514276723a8Seric **			appropriate action.
1515ea4dc939Seric */
1516ea4dc939Seric 
1517276723a8Seric sendall(e, mode)
15180c52a0b3Seric 	ENVELOPE *e;
1519276723a8Seric 	char mode;
1520ea4dc939Seric {
1521e77e673fSeric 	register ADDRESS *q;
15229836bea2Seric 	char *owner;
15239836bea2Seric 	int otherowners;
15241cc3a5d3Seric 	register ENVELOPE *ee;
15259836bea2Seric 	ENVELOPE *splitenv = NULL;
1526ea4dc939Seric 
15277b95031aSeric 	/* determine actual delivery mode */
15287b95031aSeric 	if (mode == SM_DEFAULT)
15297b95031aSeric 	{
15305f73204aSeric 		extern bool shouldqueue();
15317b95031aSeric 
1532511ad433Seric 		mode = e->e_sendmode;
15333f324333Seric 		if (mode != SM_VERIFY &&
15343f324333Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
15353f324333Seric 			mode = SM_QUEUE;
15367b95031aSeric 	}
15377b95031aSeric 
1538df864a8fSeric 	if (tTd(13, 1))
1539772e6e50Seric 	{
1540ee4b0922Seric 		printf("\nSENDALL: mode %c, e_from ", mode);
1541ee4b0922Seric 		printaddr(&e->e_from, FALSE);
1542ee4b0922Seric 		printf("sendqueue:\n");
15430c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1544772e6e50Seric 	}
1545ea4dc939Seric 
15460c52a0b3Seric 	/*
1547276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1548588cad61Seric 	**	Check to make sure the hop count is reasonable.
1549588cad61Seric 	**	Delete sends to the sender in mailing lists.
1550276723a8Seric 	*/
1551276723a8Seric 
1552588cad61Seric 	CurEnv = e;
1553276723a8Seric 
15541d57450bSeric 	if (e->e_hopcount > MaxHopCount)
1555276723a8Seric 	{
15568f9146b0Srick 		errno = 0;
155708b25121Seric 		syserr("554 too many hops %d (%d max): from %s, to %s",
1558655518ecSeric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
1559655518ecSeric 			e->e_sendqueue->q_paddr);
1560588cad61Seric 		return;
1561588cad61Seric 	}
1562588cad61Seric 
1563588cad61Seric 	if (!MeToo)
1564276723a8Seric 	{
1565f3d6c55cSeric 		extern ADDRESS *recipient();
1566f3d6c55cSeric 
156775f1ade9Seric 		if (tTd(13, 5))
156875f1ade9Seric 		{
156975f1ade9Seric 			printf("sendall: QDONTSEND ");
157075f1ade9Seric 			printaddr(&e->e_from, FALSE);
157175f1ade9Seric 		}
1572ee4b0922Seric 		e->e_from.q_flags |= QDONTSEND;
1573b843d759Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1574276723a8Seric 	}
1575588cad61Seric 
15769836bea2Seric 	/*
15779836bea2Seric 	**  Handle alias owners.
15789836bea2Seric 	**
15799836bea2Seric 	**	We scan up the q_alias chain looking for owners.
15809836bea2Seric 	**	We discard owners that are the same as the return path.
15819836bea2Seric 	*/
15829836bea2Seric 
15839836bea2Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15849836bea2Seric 	{
15859836bea2Seric 		register struct address *a;
15869836bea2Seric 
15879836bea2Seric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
15889836bea2Seric 			continue;
15899836bea2Seric 		if (a != NULL)
15909836bea2Seric 			q->q_owner = a->q_owner;
15919836bea2Seric 
1592ee4b0922Seric 		if (q->q_owner != NULL &&
1593ee4b0922Seric 		    !bitset(QDONTSEND, q->q_flags) &&
1594ee4b0922Seric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
15959836bea2Seric 			q->q_owner = NULL;
15969836bea2Seric 	}
15979836bea2Seric 
15989836bea2Seric 	owner = "";
15999836bea2Seric 	otherowners = 1;
16009836bea2Seric 	while (owner != NULL && otherowners > 0)
16019836bea2Seric 	{
16029836bea2Seric 		owner = NULL;
16039836bea2Seric 		otherowners = 0;
16049836bea2Seric 
16059836bea2Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
16069836bea2Seric 		{
16079836bea2Seric 			if (bitset(QDONTSEND, q->q_flags))
16089836bea2Seric 				continue;
16099836bea2Seric 
16109836bea2Seric 			if (q->q_owner != NULL)
16119836bea2Seric 			{
16129836bea2Seric 				if (owner == NULL)
16139836bea2Seric 					owner = q->q_owner;
16149836bea2Seric 				else if (owner != q->q_owner)
16159836bea2Seric 				{
16169836bea2Seric 					if (strcmp(owner, q->q_owner) == 0)
16179836bea2Seric 					{
16189836bea2Seric 						/* make future comparisons cheap */
16199836bea2Seric 						q->q_owner = owner;
16209836bea2Seric 					}
16219836bea2Seric 					else
16229836bea2Seric 					{
16239836bea2Seric 						otherowners++;
16249836bea2Seric 					}
16259836bea2Seric 					owner = q->q_owner;
16269836bea2Seric 				}
16279836bea2Seric 			}
16289836bea2Seric 			else
16299836bea2Seric 			{
16309836bea2Seric 				otherowners++;
16319836bea2Seric 			}
16329836bea2Seric 		}
16339836bea2Seric 
16349836bea2Seric 		if (owner != NULL && otherowners > 0)
16359836bea2Seric 		{
16369836bea2Seric 			extern HDR *copyheader();
16379836bea2Seric 			extern ADDRESS *copyqueue();
16389836bea2Seric 
1639ee4b0922Seric 			/*
1640ee4b0922Seric 			**  Split this envelope into two.
1641ee4b0922Seric 			*/
1642ee4b0922Seric 
16439836bea2Seric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
1644ee4b0922Seric 			*ee = *e;
16459836bea2Seric 			ee->e_id = NULL;
1646ee4b0922Seric 			(void) queuename(ee, '\0');
1647ee4b0922Seric 
1648ee4b0922Seric 			if (tTd(13, 1))
1649ee4b0922Seric 				printf("sendall: split %s into %s\n",
1650ee4b0922Seric 					e->e_id, ee->e_id);
1651ee4b0922Seric 
16529836bea2Seric 			ee->e_header = copyheader(e->e_header);
16539836bea2Seric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
16549836bea2Seric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
16559836bea2Seric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE);
1656579ef0ddSeric 			setsender(owner, ee, NULL, TRUE);
1657ee4b0922Seric 			if (tTd(13, 5))
1658ee4b0922Seric 			{
1659ee4b0922Seric 				printf("sendall(split): QDONTSEND ");
1660ee4b0922Seric 				printaddr(&ee->e_from, FALSE);
1661ee4b0922Seric 			}
1662ee4b0922Seric 			ee->e_from.q_flags |= QDONTSEND;
1663ee4b0922Seric 			ee->e_dfp = NULL;
1664ee4b0922Seric 			ee->e_xfp = NULL;
1665ee4b0922Seric 			ee->e_lockfp = NULL;
1666ee4b0922Seric 			ee->e_df = NULL;
1667511ad433Seric 			ee->e_errormode = EM_MAIL;
16689836bea2Seric 			ee->e_sibling = splitenv;
16699836bea2Seric 			splitenv = ee;
16709836bea2Seric 
16719836bea2Seric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
16729836bea2Seric 				if (q->q_owner == owner)
16739836bea2Seric 					q->q_flags |= QDONTSEND;
16749836bea2Seric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
16759836bea2Seric 				if (q->q_owner != owner)
16769836bea2Seric 					q->q_flags |= QDONTSEND;
16779836bea2Seric 
16789836bea2Seric 			if (e->e_df != NULL && mode != SM_VERIFY)
16799836bea2Seric 			{
16809836bea2Seric 				ee->e_dfp = NULL;
1681239b3abaSeric 				ee->e_df = newstr(queuename(ee, 'd'));
16829836bea2Seric 				if (link(e->e_df, ee->e_df) < 0)
16839836bea2Seric 				{
16849836bea2Seric 					syserr("sendall: link(%s, %s)",
16859836bea2Seric 						e->e_df, ee->e_df);
16869836bea2Seric 				}
16879836bea2Seric 			}
1688ee4b0922Seric 
1689ee4b0922Seric 			if (mode != SM_VERIFY)
1690*c9be6216Seric 				openxscript(ee);
1691336a44ebSeric #ifdef LOG
1692336a44ebSeric 			if (LogLevel > 4)
1693336a44ebSeric 				syslog(LOG_INFO, "%s: clone %s",
1694336a44ebSeric 					ee->e_id, e->e_id);
1695336a44ebSeric #endif
16969836bea2Seric 		}
16979836bea2Seric 	}
16989836bea2Seric 
16999836bea2Seric 	if (owner != NULL)
1700ee4b0922Seric 	{
1701579ef0ddSeric 		setsender(owner, e, NULL, TRUE);
1702ee4b0922Seric 		if (tTd(13, 5))
1703ee4b0922Seric 		{
1704ee4b0922Seric 			printf("sendall(owner): QDONTSEND ");
1705ee4b0922Seric 			printaddr(&e->e_from, FALSE);
1706ee4b0922Seric 		}
1707ee4b0922Seric 		e->e_from.q_flags |= QDONTSEND;
1708511ad433Seric 		e->e_errormode = EM_MAIL;
1709ee4b0922Seric 	}
17109836bea2Seric 
1711588cad61Seric # ifdef QUEUE
1712b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1713b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1714b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
17151cc3a5d3Seric 	{
17161cc3a5d3Seric 		/* be sure everything is instantiated in the queue */
1717e020b127Seric 		queueup(e, TRUE, mode == SM_QUEUE);
17181cc3a5d3Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
17191cc3a5d3Seric 			queueup(ee, TRUE, mode == SM_QUEUE);
17201cc3a5d3Seric 	}
17216c2c3107Seric #endif /* QUEUE */
1722276723a8Seric 
17239836bea2Seric 	if (splitenv != NULL)
17249836bea2Seric 	{
17259836bea2Seric 		if (tTd(13, 1))
17269836bea2Seric 		{
17279836bea2Seric 			printf("\nsendall: Split queue; remaining queue:\n");
17289836bea2Seric 			printaddr(e->e_sendqueue, TRUE);
17299836bea2Seric 		}
17309836bea2Seric 
1731ee4b0922Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
17329836bea2Seric 		{
1733ee4b0922Seric 			CurEnv = ee;
1734ee4b0922Seric 			sendenvelope(ee, mode);
17359836bea2Seric 		}
17369836bea2Seric 
17379836bea2Seric 		CurEnv = e;
17389836bea2Seric 	}
1739ee4b0922Seric 	sendenvelope(e, mode);
1740ee4b0922Seric 
1741ee4b0922Seric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
1742ee4b0922Seric 		dropenvelope(splitenv);
1743ee4b0922Seric }
1744ee4b0922Seric 
1745ee4b0922Seric sendenvelope(e, mode)
1746ee4b0922Seric 	register ENVELOPE *e;
1747ee4b0922Seric 	char mode;
1748ee4b0922Seric {
1749ee4b0922Seric 	bool oldverbose;
1750ee4b0922Seric 	int pid;
1751ee4b0922Seric 	register ADDRESS *q;
17525ff3c5eaSeric #ifdef LOCKF
17535ff3c5eaSeric 	struct flock lfd;
17545ff3c5eaSeric #endif
17559836bea2Seric 
1756276723a8Seric 	oldverbose = Verbose;
1757276723a8Seric 	switch (mode)
1758276723a8Seric 	{
1759276723a8Seric 	  case SM_VERIFY:
1760276723a8Seric 		Verbose = TRUE;
1761276723a8Seric 		break;
1762276723a8Seric 
1763276723a8Seric 	  case SM_QUEUE:
1764c7f5410dSeric   queueonly:
1765b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1766276723a8Seric 		return;
1767276723a8Seric 
1768276723a8Seric 	  case SM_FORK:
17699a6a5f55Seric 		if (e->e_xfp != NULL)
17709a6a5f55Seric 			(void) fflush(e->e_xfp);
1771c7f5410dSeric 
1772c7f5410dSeric # ifdef LOCKF
1773c7f5410dSeric 		/*
1774c7f5410dSeric 		**  Since lockf has the interesting semantic that the
17751c265a00Seric 		**  lock is lost when we fork, we have to risk losing
17761c265a00Seric 		**  the lock here by closing before the fork, and then
17771c265a00Seric 		**  trying to get it back in the child.
1778c7f5410dSeric 		*/
1779c7f5410dSeric 
1780e020b127Seric 		if (e->e_lockfp != NULL)
1781c7f5410dSeric 		{
1782ee4b0922Seric 			(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
1783e020b127Seric 			e->e_lockfp = NULL;
1784c7f5410dSeric 		}
1785c7f5410dSeric # endif /* LOCKF */
1786c7f5410dSeric 
1787276723a8Seric 		pid = fork();
1788276723a8Seric 		if (pid < 0)
1789276723a8Seric 		{
1790c7f5410dSeric 			goto queueonly;
1791276723a8Seric 		}
1792276723a8Seric 		else if (pid > 0)
1793a6fce3d8Seric 		{
1794a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1795b254bcb6Seric 			e->e_id = e->e_df = NULL;
1796c7f5410dSeric # ifndef LOCKF
1797e020b127Seric 			if (e->e_lockfp != NULL)
179875f1ade9Seric 			{
1799ee4b0922Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
180075f1ade9Seric 				e->e_lockfp = NULL;
180175f1ade9Seric 			}
1802c7f5410dSeric # endif
180375f1ade9Seric 
180475f1ade9Seric 			/* close any random open files in the envelope */
180575f1ade9Seric 			if (e->e_dfp != NULL)
180675f1ade9Seric 			{
1807ee4b0922Seric 				(void) xfclose(e->e_dfp, "sendenvelope", "dfp");
180875f1ade9Seric 				e->e_dfp = NULL;
180975f1ade9Seric 			}
181075f1ade9Seric 			if (e->e_xfp != NULL)
181175f1ade9Seric 			{
1812ee4b0922Seric 				(void) xfclose(e->e_xfp, "sendenvelope", "xfp");
181375f1ade9Seric 				e->e_xfp = NULL;
181475f1ade9Seric 			}
1815276723a8Seric 			return;
1816a6fce3d8Seric 		}
1817276723a8Seric 
1818276723a8Seric 		/* double fork to avoid zombies */
1819276723a8Seric 		if (fork() > 0)
1820276723a8Seric 			exit(EX_OK);
1821276723a8Seric 
1822a6fce3d8Seric 		/* be sure we are immune from the terminal */
18238dadacffSeric 		disconnect(FALSE, e);
1824a6fce3d8Seric 
1825e6720d51Seric # ifdef LOCKF
1826e6720d51Seric 		/*
1827c7f5410dSeric 		**  Now try to get our lock back.
1828e6720d51Seric 		*/
1829e6720d51Seric 
18301c265a00Seric 		lfd.l_type = F_WRLCK;
18311c265a00Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1832e020b127Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1833e020b127Seric 		if (e->e_lockfp == NULL ||
18341c265a00Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1835e6720d51Seric 		{
1836e6720d51Seric 			/* oops....  lost it */
1837336a44ebSeric 			if (tTd(13, 1))
1838336a44ebSeric 				printf("sendenvelope: %s lost lock: lockfp=%x, %s\n",
1839336a44ebSeric 					e->e_id, e->e_lockfp, errstring(errno));
1840336a44ebSeric 
1841e6720d51Seric # ifdef LOG
18422f624c86Seric 			if (LogLevel > 29)
1843c7f5410dSeric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1844b31e7f2bSeric 					e->e_id);
1845e6720d51Seric # endif /* LOG */
1846e6720d51Seric 			exit(EX_OK);
1847e6720d51Seric 		}
1848e6720d51Seric # endif /* LOCKF */
1849e6720d51Seric 
18501caa9a59Seric 		/*
18511caa9a59Seric 		**  Close any cached connections.
18521caa9a59Seric 		**
18531caa9a59Seric 		**	We don't send the QUIT protocol because the parent
18541caa9a59Seric 		**	still knows about the connection.
18551caa9a59Seric 		**
18561caa9a59Seric 		**	This should only happen when delivering an error
18571caa9a59Seric 		**	message.
18581caa9a59Seric 		*/
18591caa9a59Seric 
18601caa9a59Seric 		mci_flush(FALSE, NULL);
18611caa9a59Seric 
1862276723a8Seric 		break;
1863276723a8Seric 	}
1864276723a8Seric 
1865276723a8Seric 	/*
18660c52a0b3Seric 	**  Run through the list and send everything.
18670c52a0b3Seric 	*/
18680c52a0b3Seric 
1869655518ecSeric 	e->e_nsent = 0;
18700c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1871ea4dc939Seric 	{
1872276723a8Seric 		if (mode == SM_VERIFY)
1873ea4dc939Seric 		{
1874a6fce3d8Seric 			e->e_to = q->q_paddr;
1875e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
187608b25121Seric 				message("deliverable");
1877ea4dc939Seric 		}
1878ee4b0922Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1879dde5acadSeric 		{
1880de1179f5Seric # ifdef QUEUE
1881dde5acadSeric 			/*
1882dde5acadSeric 			**  Checkpoint the send list every few addresses
1883dde5acadSeric 			*/
1884dde5acadSeric 
1885655518ecSeric 			if (e->e_nsent >= CheckpointInterval)
1886dde5acadSeric 			{
1887e020b127Seric 				queueup(e, TRUE, FALSE);
1888655518ecSeric 				e->e_nsent = 0;
1889dde5acadSeric 			}
1890de1179f5Seric # endif /* QUEUE */
1891655518ecSeric 			(void) deliver(e, q);
1892dde5acadSeric 		}
1893ea4dc939Seric 	}
189414a8ed7aSeric 	Verbose = oldverbose;
18950c52a0b3Seric 
18960c52a0b3Seric 	/*
18970c52a0b3Seric 	**  Now run through and check for errors.
18980c52a0b3Seric 	*/
18990c52a0b3Seric 
1900e020b127Seric 	if (mode == SM_VERIFY)
1901ee4b0922Seric 	{
19020c52a0b3Seric 		return;
1903ee4b0922Seric 	}
19040c52a0b3Seric 
19050c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
19060c52a0b3Seric 	{
1907df864a8fSeric 		if (tTd(13, 3))
1908df864a8fSeric 		{
1909df864a8fSeric 			printf("Checking ");
1910df864a8fSeric 			printaddr(q, FALSE);
1911df864a8fSeric 		}
1912df864a8fSeric 
1913b254bcb6Seric 		/* only send errors if the message failed */
1914b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1915b254bcb6Seric 			continue;
19160c52a0b3Seric 
1917ee4b0922Seric 		e->e_flags |= EF_FATALERRS;
1918ee4b0922Seric 
1919b8043ddbSeric 		if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0)
19209836bea2Seric 			(void) sendtolist(e->e_from.q_paddr, NULL,
19211caa9a59Seric 					  &e->e_errorqueue, e);
19220c52a0b3Seric 	}
1923276723a8Seric 
1924276723a8Seric 	if (mode == SM_FORK)
1925276723a8Seric 		finis();
19260c52a0b3Seric }
1927e103b48fSeric /*
1928e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1929e103b48fSeric **
1930e103b48fSeric **	The signature describes how we are going to send this -- it
1931e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1932e103b48fSeric **	an ordered list of MX hosts.
1933e103b48fSeric **
1934e103b48fSeric **	Parameters:
1935e103b48fSeric **		m -- the mailer describing this host.
1936e103b48fSeric **		host -- the host name.
1937e103b48fSeric **		e -- the current envelope.
1938e103b48fSeric **
1939e103b48fSeric **	Returns:
1940e103b48fSeric **		The signature for this host.
1941e103b48fSeric **
1942e103b48fSeric **	Side Effects:
1943e103b48fSeric **		Can tweak the symbol table.
1944e103b48fSeric */
1945e103b48fSeric 
1946e103b48fSeric char *
1947e103b48fSeric hostsignature(m, host, e)
1948e103b48fSeric 	register MAILER *m;
1949e103b48fSeric 	char *host;
1950e103b48fSeric 	ENVELOPE *e;
1951e103b48fSeric {
1952e103b48fSeric 	register char *p;
1953e103b48fSeric 	register STAB *s;
1954e103b48fSeric 	int i;
1955e103b48fSeric 	int len;
1956e103b48fSeric #ifdef NAMED_BIND
1957e103b48fSeric 	int nmx;
1958e103b48fSeric 	auto int rcode;
1959e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
1960e103b48fSeric 	static char myhostbuf[MAXNAME];
1961e103b48fSeric #endif
1962e103b48fSeric 
1963e103b48fSeric 	/*
1964e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
1965e103b48fSeric 	*/
1966e103b48fSeric 
1967e103b48fSeric 	p = m->m_mailer;
1968e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
1969e103b48fSeric 	{
1970e103b48fSeric 		/* just an ordinary mailer */
1971e103b48fSeric 		return host;
1972e103b48fSeric 	}
1973e103b48fSeric 
1974e103b48fSeric 	/*
1975e103b48fSeric 	**  If it is a numeric address, just return it.
1976e103b48fSeric 	*/
1977e103b48fSeric 
1978e103b48fSeric 	if (host[0] == '[')
1979e103b48fSeric 		return host;
1980e103b48fSeric 
1981e103b48fSeric 	/*
1982e103b48fSeric 	**  Look it up in the symbol table.
1983e103b48fSeric 	*/
1984e103b48fSeric 
1985e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
1986e103b48fSeric 	if (s->s_hostsig != NULL)
1987e103b48fSeric 		return s->s_hostsig;
1988e103b48fSeric 
1989e103b48fSeric 	/*
1990e103b48fSeric 	**  Not already there -- create a signature.
1991e103b48fSeric 	*/
1992e103b48fSeric 
1993e103b48fSeric #ifdef NAMED_BIND
1994e103b48fSeric 	if (myhostbuf[0] == '\0')
19952bc47524Seric 		expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e);
1996e103b48fSeric 
1997e103b48fSeric 	nmx = getmxrr(host, mxhosts, myhostbuf, &rcode);
19987d55540cSeric 
1999e103b48fSeric 	if (nmx <= 0)
2000e103b48fSeric 	{
2001e103b48fSeric 		register MCI *mci;
2002e103b48fSeric 		extern int errno;
2003e103b48fSeric 		extern MCI *mci_get();
2004e103b48fSeric 
2005e103b48fSeric 		/* update the connection info for this host */
2006e103b48fSeric 		mci = mci_get(host, m);
2007e103b48fSeric 		mci->mci_exitstat = rcode;
2008e103b48fSeric 		mci->mci_errno = errno;
2009e103b48fSeric 
2010e103b48fSeric 		/* and return the original host name as the signature */
2011e103b48fSeric 		s->s_hostsig = host;
2012e103b48fSeric 		return host;
2013e103b48fSeric 	}
2014e103b48fSeric 
2015e103b48fSeric 	len = 0;
2016e103b48fSeric 	for (i = 0; i < nmx; i++)
2017e103b48fSeric 	{
2018e103b48fSeric 		len += strlen(mxhosts[i]) + 1;
2019e103b48fSeric 	}
2020e103b48fSeric 	s->s_hostsig = p = xalloc(len);
2021e103b48fSeric 	for (i = 0; i < nmx; i++)
2022e103b48fSeric 	{
2023e103b48fSeric 		if (i != 0)
2024e103b48fSeric 			*p++ = ':';
2025e103b48fSeric 		strcpy(p, mxhosts[i]);
2026e103b48fSeric 		p += strlen(p);
2027e103b48fSeric 	}
2028e103b48fSeric 	makelower(s->s_hostsig);
2029e103b48fSeric #else
2030e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2031e103b48fSeric 	s->s_hostsig = host;
2032e103b48fSeric #endif
2033e103b48fSeric 	if (tTd(17, 1))
2034e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2035e103b48fSeric 	return s->s_hostsig;
2036e103b48fSeric }
2037