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*e2f2f828Seric static char sccsid[] = "@(#)deliver.c	6.53 (Berkeley) 03/19/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;
8651552439Seric 
876ef48975Seric 	if (tTd(10, 1))
885dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
8951552439Seric 			m->m_mno, host, to->q_user);
90f3dbc832Seric 
91f3dbc832Seric 	/*
92f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
93f3dbc832Seric 	**  connections now, just mark these addresses and return.
94f3dbc832Seric 	**	This is useful if we want to batch connections to
95f3dbc832Seric 	**	reduce load.  This will cause the messages to be
96f3dbc832Seric 	**	queued up, and a daemon will come along to send the
97f3dbc832Seric 	**	messages later.
98f3dbc832Seric 	**		This should be on a per-mailer basis.
99f3dbc832Seric 	*/
100f3dbc832Seric 
10119c47125Seric 	if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) &&
10219c47125Seric 	    bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
103f3dbc832Seric 	{
104f3dbc832Seric 		for (; to != NULL; to = to->q_next)
105f4560e80Seric 		{
106ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
107c6e18ac5Seric 			    to->q_mailer != m)
108f4560e80Seric 				continue;
109f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
110588cad61Seric 			e->e_to = to->q_paddr;
11108b25121Seric 			message("queued");
1122f624c86Seric 			if (LogLevel > 8)
11381161401Seric 				logdelivery(m, NULL, "queued", e);
114f4560e80Seric 		}
115588cad61Seric 		e->e_to = NULL;
116f3dbc832Seric 		return (0);
117f3dbc832Seric 	}
118f3dbc832Seric 
11925a99e2eSeric 	/*
1205dfc646bSeric 	**  Do initial argv setup.
1215dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
1225dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
1235dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
1245dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
1255dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
1263bea8136Seric 	**		The from address rewrite is expected to make
1273bea8136Seric 	**		the address relative to the other end.
1285dfc646bSeric 	*/
1295dfc646bSeric 
13078442df3Seric 	/* rewrite from address, using rewriting rules */
131ee4b0922Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, TRUE, FALSE,
1329c69ca01Seric 					   TRUE, FALSE, e));
133ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
134588cad61Seric 	define('h', host, e);			/* to host */
1355dfc646bSeric 	Errors = 0;
1365dfc646bSeric 	pvp = pv;
1375dfc646bSeric 	*pvp++ = m->m_argv[0];
1385dfc646bSeric 
1395dfc646bSeric 	/* insert -f or -r flag as appropriate */
14057fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
1415dfc646bSeric 	{
14257fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
1435dfc646bSeric 			*pvp++ = "-f";
1445dfc646bSeric 		else
1455dfc646bSeric 			*pvp++ = "-r";
146c23ed322Seric 		*pvp++ = newstr(rpathbuf);
1475dfc646bSeric 	}
1485dfc646bSeric 
1495dfc646bSeric 	/*
1505dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1515dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1525dfc646bSeric 	**  be one of these, and there are only a few more slots
1535dfc646bSeric 	**  in the pv after it.
1545dfc646bSeric 	*/
1555dfc646bSeric 
1565dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1575dfc646bSeric 	{
1582bc47524Seric 		/* can't use strchr here because of sign extension problems */
1592bc47524Seric 		while (*p != '\0')
1602bc47524Seric 		{
1612bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
1622bc47524Seric 			{
1632bc47524Seric 				if (*p == 'u')
1645dfc646bSeric 					break;
1652bc47524Seric 			}
1662bc47524Seric 		}
1672bc47524Seric 
1682bc47524Seric 		if (*p != '\0')
1695dfc646bSeric 			break;
1705dfc646bSeric 
1715dfc646bSeric 		/* this entry is safe -- go ahead and process it */
172588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1735dfc646bSeric 		*pvp++ = newstr(buf);
1745dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1755dfc646bSeric 		{
17608b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
1775dfc646bSeric 			return (-1);
1785dfc646bSeric 		}
1795dfc646bSeric 	}
180c579ef51Seric 
18133db8731Seric 	/*
18233db8731Seric 	**  If we have no substitution for the user name in the argument
18333db8731Seric 	**  list, we know that we must supply the names otherwise -- and
18433db8731Seric 	**  SMTP is the answer!!
18533db8731Seric 	*/
18633db8731Seric 
1875dfc646bSeric 	if (*mvp == NULL)
188c579ef51Seric 	{
189c579ef51Seric 		/* running SMTP */
1902c7e1b8dSeric # ifdef SMTP
191c579ef51Seric 		clever = TRUE;
192c579ef51Seric 		*pvp = NULL;
1936c2c3107Seric # else /* SMTP */
19433db8731Seric 		/* oops!  we don't implement SMTP */
19508b25121Seric 		syserr("554 SMTP style mailer");
1962c7e1b8dSeric 		return (EX_SOFTWARE);
1976c2c3107Seric # endif /* SMTP */
198c579ef51Seric 	}
1995dfc646bSeric 
2005dfc646bSeric 	/*
2015dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
2025dfc646bSeric 	**  run through our address list and append all the addresses
2035dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
2045dfc646bSeric 	**  always send another copy later.
2055dfc646bSeric 	*/
2065dfc646bSeric 
2075dfc646bSeric 	tobuf[0] = '\0';
208588cad61Seric 	e->e_to = tobuf;
2096259796dSeric 	ctladdr = NULL;
210e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
2115dfc646bSeric 	for (; to != NULL; to = to->q_next)
2125dfc646bSeric 	{
2135dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
21457fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
2155dfc646bSeric 			break;
2165dfc646bSeric 
2175dfc646bSeric 		/* if already sent or not for this host, don't send */
218ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
219e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
220e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
2215dfc646bSeric 			continue;
2226259796dSeric 
2234b22ea87Seric 		/* avoid overflowing tobuf */
224aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
2254b22ea87Seric 			break;
2264b22ea87Seric 
2276ef48975Seric 		if (tTd(10, 1))
228772e6e50Seric 		{
229772e6e50Seric 			printf("\nsend to ");
230772e6e50Seric 			printaddr(to, FALSE);
231772e6e50Seric 		}
232772e6e50Seric 
2336259796dSeric 		/* compute effective uid/gid when sending */
2347da1035fSeric 		if (to->q_mailer == ProgMailer)
2356259796dSeric 			ctladdr = getctladdr(to);
2366259796dSeric 
2375dfc646bSeric 		user = to->q_user;
238588cad61Seric 		e->e_to = to->q_paddr;
23975f1ade9Seric 		if (tTd(10, 5))
24075f1ade9Seric 		{
24175f1ade9Seric 			printf("deliver: QDONTSEND ");
24275f1ade9Seric 			printaddr(to, FALSE);
24375f1ade9Seric 		}
244ee4b0922Seric 		to->q_flags |= QDONTSEND;
2455dfc646bSeric 
2465dfc646bSeric 		/*
2475dfc646bSeric 		**  Check to see that these people are allowed to
2485dfc646bSeric 		**  talk to each other.
2492a6e0786Seric 		*/
2502a6e0786Seric 
25169582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
25269582d2fSeric 		{
25369582d2fSeric 			NoReturn = TRUE;
25408b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
25581161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
25669582d2fSeric 			continue;
25769582d2fSeric 		}
258fabb3bd4Seric 		rcode = checkcompat(to, e);
2591793c9c5Seric 		if (rcode != EX_OK)
2605dfc646bSeric 		{
26181161401Seric 			giveresponse(rcode, m, NULL, e);
2625dfc646bSeric 			continue;
2635dfc646bSeric 		}
2642a6e0786Seric 
2652a6e0786Seric 		/*
2669ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2679ec9501bSeric 		**	about them.
26825a99e2eSeric 		*/
26925a99e2eSeric 
27057fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
27125a99e2eSeric 		{
2721d8f1806Seric 			stripquotes(user);
2731d8f1806Seric 			stripquotes(host);
27425a99e2eSeric 		}
27525a99e2eSeric 
276cdb828c5Seric 		/* hack attack -- delivermail compatibility */
277cdb828c5Seric 		if (m == ProgMailer && *user == '|')
278cdb828c5Seric 			user++;
279cdb828c5Seric 
28025a99e2eSeric 		/*
2813efaed6eSeric 		**  If an error message has already been given, don't
2823efaed6eSeric 		**	bother to send to this address.
2833efaed6eSeric 		**
2843efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2853efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2863efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2873efaed6eSeric 		*/
2883efaed6eSeric 
2896cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2903efaed6eSeric 			continue;
2913efaed6eSeric 
292f2fec898Seric 		/* save statistics.... */
293588cad61Seric 		markstats(e, to);
294f2fec898Seric 
2953efaed6eSeric 		/*
29625a99e2eSeric 		**  See if this user name is "special".
29725a99e2eSeric 		**	If the user name has a slash in it, assume that this
29851552439Seric 		**	is a file -- send it off without further ado.  Note
29951552439Seric 		**	that this type of addresses is not processed along
30051552439Seric 		**	with the others, so we fudge on the To person.
30125a99e2eSeric 		*/
30225a99e2eSeric 
3032c017f8dSeric 		if (m == FileMailer)
30425a99e2eSeric 		{
305b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
30681161401Seric 			giveresponse(rcode, m, NULL, e);
307dde5acadSeric 			if (rcode == EX_OK)
308dde5acadSeric 				to->q_flags |= QSENT;
3095dfc646bSeric 			continue;
31025a99e2eSeric 		}
31125a99e2eSeric 
31213bbc08cSeric 		/*
31313bbc08cSeric 		**  Address is verified -- add this user to mailer
31413bbc08cSeric 		**  argv, and add it to the print list of recipients.
31513bbc08cSeric 		*/
31613bbc08cSeric 
317508daeccSeric 		/* link together the chain of recipients */
318508daeccSeric 		to->q_tchain = tochain;
319508daeccSeric 		tochain = to;
320508daeccSeric 
3215dfc646bSeric 		/* create list of users for error messages */
322db8841e9Seric 		(void) strcat(tobuf, ",");
323db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
324588cad61Seric 		define('u', user, e);		/* to user */
325588cad61Seric 		define('z', to->q_home, e);	/* user's home */
3265dfc646bSeric 
327c579ef51Seric 		/*
328508daeccSeric 		**  Expand out this user into argument list.
329c579ef51Seric 		*/
330c579ef51Seric 
331508daeccSeric 		if (!clever)
332c579ef51Seric 		{
333588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3345dfc646bSeric 			*pvp++ = newstr(buf);
3355dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3365dfc646bSeric 			{
3375dfc646bSeric 				/* allow some space for trailing parms */
3385dfc646bSeric 				break;
3395dfc646bSeric 			}
3405dfc646bSeric 		}
341c579ef51Seric 	}
3425dfc646bSeric 
343145b49b1Seric 	/* see if any addresses still exist */
344145b49b1Seric 	if (tobuf[0] == '\0')
345c579ef51Seric 	{
346588cad61Seric 		define('g', (char *) NULL, e);
347145b49b1Seric 		return (0);
348c579ef51Seric 	}
349145b49b1Seric 
3505dfc646bSeric 	/* print out messages as full list */
35163780dbdSeric 	e->e_to = tobuf + 1;
3525dfc646bSeric 
3535dfc646bSeric 	/*
3545dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3555dfc646bSeric 	*/
3565dfc646bSeric 
357c579ef51Seric 	while (!clever && *++mvp != NULL)
3585dfc646bSeric 	{
359588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
3605dfc646bSeric 		*pvp++ = newstr(buf);
3615dfc646bSeric 		if (pvp >= &pv[MAXPV])
36208b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
3635dfc646bSeric 	}
3645dfc646bSeric 	*pvp++ = NULL;
3655dfc646bSeric 
36625a99e2eSeric 	/*
36725a99e2eSeric 	**  Call the mailer.
3686328bdf7Seric 	**	The argument vector gets built, pipes
36925a99e2eSeric 	**	are created as necessary, and we fork & exec as
3706328bdf7Seric 	**	appropriate.
371c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
37225a99e2eSeric 	*/
37325a99e2eSeric 
3747ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
37586b26461Seric 		ctladdr = &e->e_from;
376134746fbSeric #ifdef NAMED_BIND
3772bcc6d2dSeric 	if (ConfigLevel < 2)
378912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
379134746fbSeric #endif
380b31e7f2bSeric 	mci = openmailer(m, pv, ctladdr, clever, e);
381b31e7f2bSeric 	if (mci == NULL)
382134746fbSeric 	{
383b31e7f2bSeric 		/* catastrophic error */
384845e533cSeric 		rcode = EX_OSERR;
385b31e7f2bSeric 		goto give_up;
386b31e7f2bSeric 	}
387b31e7f2bSeric 	else if (mci->mci_state != MCIS_OPEN)
388b31e7f2bSeric 	{
389b31e7f2bSeric 		/* couldn't open the mailer */
390b31e7f2bSeric 		rcode = mci->mci_exitstat;
3912a6bc25bSeric 		errno = mci->mci_errno;
392b31e7f2bSeric 		if (rcode == EX_OK)
393b31e7f2bSeric 		{
394b31e7f2bSeric 			/* shouldn't happen */
39508b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
3966b0f339dSeric 				rcode, mci->mci_state, firstsig);
397b31e7f2bSeric 			rcode = EX_SOFTWARE;
398b31e7f2bSeric 		}
399b31e7f2bSeric 	}
400b31e7f2bSeric 	else if (!clever)
401b31e7f2bSeric 	{
402b31e7f2bSeric 		/*
403b31e7f2bSeric 		**  Format and send message.
404b31e7f2bSeric 		*/
40515d084d5Seric 
406b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
407b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
408b31e7f2bSeric 		putline("\n", mci->mci_out, m);
409b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
410b31e7f2bSeric 
411b31e7f2bSeric 		/* get the exit status */
412b31e7f2bSeric 		rcode = endmailer(mci, pv[0]);
413134746fbSeric 	}
414134746fbSeric 	else
415b31e7f2bSeric #ifdef SMTP
416134746fbSeric 	{
417b31e7f2bSeric 		/*
418b31e7f2bSeric 		**  Send the MAIL FROM: protocol
419b31e7f2bSeric 		*/
42015d084d5Seric 
421b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
422b31e7f2bSeric 		if (rcode == EX_OK)
42375889e88Seric 		{
424ded0d3daSkarels 			register char *t = tobuf;
425ded0d3daSkarels 			register int i;
426ded0d3daSkarels 
427588cad61Seric 			/* send the recipient list */
42863780dbdSeric 			tobuf[0] = '\0';
42975889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
43075889e88Seric 			{
43163780dbdSeric 				e->e_to = to->q_paddr;
43215d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
43375889e88Seric 				{
43483b7ddc9Seric 					markfailure(e, to, i);
43581161401Seric 					giveresponse(i, m, mci, e);
43663780dbdSeric 				}
43775889e88Seric 				else
43875889e88Seric 				{
439911693bfSbostic 					*t++ = ',';
440b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
441b31e7f2bSeric 						continue;
442588cad61Seric 				}
443588cad61Seric 			}
444588cad61Seric 
44563780dbdSeric 			/* now send the data */
44663780dbdSeric 			if (tobuf[0] == '\0')
447b31e7f2bSeric 			{
44863780dbdSeric 				e->e_to = NULL;
449b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
450b31e7f2bSeric 					smtprset(m, mci, e);
451b31e7f2bSeric 			}
45275889e88Seric 			else
45375889e88Seric 			{
45463780dbdSeric 				e->e_to = tobuf + 1;
45575889e88Seric 				rcode = smtpdata(m, mci, e);
45663780dbdSeric 			}
45763780dbdSeric 
45863780dbdSeric 			/* now close the connection */
459b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
46015d084d5Seric 				smtpquit(m, mci, e);
46163780dbdSeric 		}
462c579ef51Seric 	}
463b31e7f2bSeric #else /* not SMTP */
464a05b3449Sbostic 	{
46508b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
466845e533cSeric 		rcode = EX_CONFIG;
467b31e7f2bSeric 		goto give_up;
468a05b3449Sbostic 	}
469b31e7f2bSeric #endif /* SMTP */
470134746fbSeric #ifdef NAMED_BIND
4712bcc6d2dSeric 	if (ConfigLevel < 2)
472912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
473134746fbSeric #endif
4745dfc646bSeric 
475b31e7f2bSeric 	/* arrange a return receipt if requested */
4768c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
477b31e7f2bSeric 	{
478b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
479b31e7f2bSeric 		/* do we want to send back more info? */
480b31e7f2bSeric 	}
481b31e7f2bSeric 
482c77d1c25Seric 	/*
48363780dbdSeric 	**  Do final status disposal.
48463780dbdSeric 	**	We check for something in tobuf for the SMTP case.
485c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
486c77d1c25Seric 	**		addressees.
487c77d1c25Seric 	*/
488c77d1c25Seric 
489b31e7f2bSeric   give_up:
49063780dbdSeric 	if (tobuf[0] != '\0')
49181161401Seric 		giveresponse(rcode, m, mci, e);
492772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
493b31e7f2bSeric 	{
494dde5acadSeric 		if (rcode != EX_OK)
49583b7ddc9Seric 			markfailure(e, to, rcode);
496dde5acadSeric 		else
497655518ecSeric 		{
498dde5acadSeric 			to->q_flags |= QSENT;
499655518ecSeric 			e->e_nsent++;
500655518ecSeric 		}
501b31e7f2bSeric 	}
502b31e7f2bSeric 
503b31e7f2bSeric 	/*
504b31e7f2bSeric 	**  Restore state and return.
505b31e7f2bSeric 	*/
506c77d1c25Seric 
50735490626Seric 	errno = 0;
508588cad61Seric 	define('g', (char *) NULL, e);
5095826d9d3Seric 	return (rcode);
51025a99e2eSeric }
5115dfc646bSeric /*
51283b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
51383b7ddc9Seric **
51483b7ddc9Seric **	Parameters:
51583b7ddc9Seric **		e -- the envelope we are sending.
51683b7ddc9Seric **		q -- the address to mark.
51783b7ddc9Seric **		rcode -- the code signifying the particular failure.
51883b7ddc9Seric **
51983b7ddc9Seric **	Returns:
52083b7ddc9Seric **		none.
52183b7ddc9Seric **
52283b7ddc9Seric **	Side Effects:
52383b7ddc9Seric **		marks the address (and possibly the envelope) with the
52483b7ddc9Seric **			failure so that an error will be returned or
52583b7ddc9Seric **			the message will be queued, as appropriate.
52683b7ddc9Seric */
52783b7ddc9Seric 
52883b7ddc9Seric markfailure(e, q, rcode)
52983b7ddc9Seric 	register ENVELOPE *e;
53083b7ddc9Seric 	register ADDRESS *q;
53183b7ddc9Seric 	int rcode;
53283b7ddc9Seric {
53319c47125Seric 	char buf[MAXLINE];
53419c47125Seric 	extern char *pintvl();
53519c47125Seric 
53683b7ddc9Seric 	if (rcode == EX_OK)
53783b7ddc9Seric 		return;
538ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
53983b7ddc9Seric 		q->q_flags |= QBADADDR;
54019c47125Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
54183b7ddc9Seric 	{
54283b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
543198d9be0Seric 		{
544198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
54519c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
546198d9be0Seric 			if (e->e_message != NULL)
547198d9be0Seric 				free(e->e_message);
548198d9be0Seric 			e->e_message = newstr(buf);
54908b25121Seric 			message(buf);
550198d9be0Seric 		}
55183b7ddc9Seric 		q->q_flags |= QBADADDR;
55283b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
553c13b5dfcSeric 		fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr);
55483b7ddc9Seric 	}
55583b7ddc9Seric 	else
55619c47125Seric 	{
55783b7ddc9Seric 		q->q_flags |= QQUEUEUP;
55819c47125Seric 		if (TimeOuts.to_q_warning > 0 &&
55919c47125Seric 		    curtime() > e->e_ctime + TimeOuts.to_q_warning)
56019c47125Seric 		{
56119c47125Seric 			if (!bitset(EF_WARNING, e->e_flags) &&
56219c47125Seric 			    e->e_class >= 0)
56319c47125Seric 			{
56419c47125Seric 				(void) sprintf(buf,
56519c47125Seric 					"warning: cannot send message for %s",
56619c47125Seric 					pintvl(TimeOuts.to_q_warning, FALSE));
56719c47125Seric 				if (e->e_message != NULL)
56819c47125Seric 					free(e->e_message);
56919c47125Seric 				e->e_message = newstr(buf);
57019c47125Seric 				message(buf);
57119c47125Seric 				e->e_flags |= EF_WARNING|EF_TIMEOUT;
57219c47125Seric 			}
57319c47125Seric 			fprintf(e->e_xfp,
57419c47125Seric 				"%s... Warning: message still undelivered after %s\n",
57519c47125Seric 				q->q_paddr, pintvl(TimeOuts.to_q_warning, FALSE));
57619c47125Seric 			fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
57719c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
57819c47125Seric 		}
57919c47125Seric 	}
58083b7ddc9Seric }
58183b7ddc9Seric /*
58232d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
58332d19d43Seric **
58432d19d43Seric **	This MUST be a macro, since after a vfork we are running
58532d19d43Seric **	two processes on the same stack!!!
58632d19d43Seric **
58732d19d43Seric **	Parameters:
58832d19d43Seric **		none.
58932d19d43Seric **
59032d19d43Seric **	Returns:
59132d19d43Seric **		From a macro???  You've got to be kidding!
59232d19d43Seric **
59332d19d43Seric **	Side Effects:
59432d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
59532d19d43Seric **			pid of child in parent, zero in child.
59632d19d43Seric **			-1 on unrecoverable error.
59732d19d43Seric **
59832d19d43Seric **	Notes:
59932d19d43Seric **		I'm awfully sorry this looks so awful.  That's
60032d19d43Seric **		vfork for you.....
60132d19d43Seric */
60232d19d43Seric 
60332d19d43Seric # define NFORKTRIES	5
60484f7cd1bSeric 
60584f7cd1bSeric # ifndef FORK
60684f7cd1bSeric # define FORK	fork
60784f7cd1bSeric # endif
60832d19d43Seric 
60932d19d43Seric # define DOFORK(fORKfN) \
61032d19d43Seric {\
61132d19d43Seric 	register int i;\
61232d19d43Seric \
61311799049Seric 	for (i = NFORKTRIES; --i >= 0; )\
61432d19d43Seric 	{\
61532d19d43Seric 		pid = fORKfN();\
61632d19d43Seric 		if (pid >= 0)\
61732d19d43Seric 			break;\
61811799049Seric 		if (i > 0)\
6196c4635f6Seric 			sleep((unsigned) NFORKTRIES - i);\
62032d19d43Seric 	}\
62132d19d43Seric }
62232d19d43Seric /*
6232ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
6242ed72599Seric **
6252ed72599Seric **	Parameters:
6262ed72599Seric **		none.
6272ed72599Seric **
6282ed72599Seric **	Returns:
6292ed72599Seric **		pid of child in parent.
6302ed72599Seric **		zero in child.
6312ed72599Seric **		-1 on error.
6322ed72599Seric **
6332ed72599Seric **	Side Effects:
6342ed72599Seric **		returns twice, once in parent and once in child.
6352ed72599Seric */
6362ed72599Seric 
6372ed72599Seric dofork()
6382ed72599Seric {
6392ed72599Seric 	register int pid;
6402ed72599Seric 
6412ed72599Seric 	DOFORK(fork);
6422ed72599Seric 	return (pid);
6432ed72599Seric }
6442ed72599Seric /*
645c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
646c579ef51Seric **
647c579ef51Seric **	We should never get fatal errors (e.g., segmentation
648c579ef51Seric **	violation), so we report those specially.  For other
649c579ef51Seric **	errors, we choose a status message (into statmsg),
650c579ef51Seric **	and if it represents an error, we print it.
651c579ef51Seric **
652c579ef51Seric **	Parameters:
653c579ef51Seric **		pid -- pid of mailer.
654c579ef51Seric **		name -- name of mailer (for error messages).
655c579ef51Seric **
656c579ef51Seric **	Returns:
657c579ef51Seric **		exit code of mailer.
658c579ef51Seric **
659c579ef51Seric **	Side Effects:
660c579ef51Seric **		none.
661c579ef51Seric */
662c579ef51Seric 
66375889e88Seric endmailer(mci, name)
664b31e7f2bSeric 	register MCI *mci;
665c579ef51Seric 	char *name;
666c579ef51Seric {
667588cad61Seric 	int st;
668c579ef51Seric 
66975889e88Seric 	/* close any connections */
67075889e88Seric 	if (mci->mci_in != NULL)
671ee4b0922Seric 		(void) xfclose(mci->mci_in, name, "mci_in");
67275889e88Seric 	if (mci->mci_out != NULL)
673ee4b0922Seric 		(void) xfclose(mci->mci_out, name, "mci_out");
67475889e88Seric 	mci->mci_in = mci->mci_out = NULL;
67575889e88Seric 	mci->mci_state = MCIS_CLOSED;
67675889e88Seric 
67733db8731Seric 	/* in the IPC case there is nothing to wait for */
67875889e88Seric 	if (mci->mci_pid == 0)
67933db8731Seric 		return (EX_OK);
68033db8731Seric 
68133db8731Seric 	/* wait for the mailer process to die and collect status */
68275889e88Seric 	st = waitfor(mci->mci_pid);
683588cad61Seric 	if (st == -1)
68478de67c1Seric 	{
685588cad61Seric 		syserr("endmailer %s: wait", name);
686588cad61Seric 		return (EX_SOFTWARE);
687c579ef51Seric 	}
68833db8731Seric 
68933db8731Seric 	/* see if it died a horrid death */
690c579ef51Seric 	if ((st & 0377) != 0)
691c579ef51Seric 	{
6925f73204aSeric 		syserr("mailer %s died with signal %o", name, st);
6935f73204aSeric 		ExitStat = EX_TEMPFAIL;
6945f73204aSeric 		return (EX_TEMPFAIL);
695c579ef51Seric 	}
69633db8731Seric 
69733db8731Seric 	/* normal death -- return status */
698588cad61Seric 	st = (st >> 8) & 0377;
699588cad61Seric 	return (st);
700c579ef51Seric }
701c579ef51Seric /*
702c579ef51Seric **  OPENMAILER -- open connection to mailer.
703c579ef51Seric **
704c579ef51Seric **	Parameters:
705c579ef51Seric **		m -- mailer descriptor.
706c579ef51Seric **		pvp -- parameter vector to pass to mailer.
707c579ef51Seric **		ctladdr -- controlling address for user.
708c579ef51Seric **		clever -- create a full duplex connection.
709c579ef51Seric **
710c579ef51Seric **	Returns:
71175889e88Seric **		The mail connection info struct for this connection.
71275889e88Seric **		NULL on failure.
713c579ef51Seric **
714c579ef51Seric **	Side Effects:
715c579ef51Seric **		creates a mailer in a subprocess.
716c579ef51Seric */
717c579ef51Seric 
718b31e7f2bSeric MCI *
719b31e7f2bSeric openmailer(m, pvp, ctladdr, clever, e)
720588cad61Seric 	MAILER *m;
721c579ef51Seric 	char **pvp;
722c579ef51Seric 	ADDRESS *ctladdr;
723c579ef51Seric 	bool clever;
724b31e7f2bSeric 	ENVELOPE *e;
725c579ef51Seric {
7265dfc646bSeric 	int pid;
727b31e7f2bSeric 	register MCI *mci;
728f8952a83Seric 	int mpvect[2];
729c579ef51Seric 	int rpvect[2];
7305dfc646bSeric 	extern FILE *fdopen();
7315dfc646bSeric 
7326ef48975Seric 	if (tTd(11, 1))
7335dfc646bSeric 	{
7348c57e552Seric 		printf("openmailer:");
7355dfc646bSeric 		printav(pvp);
7365dfc646bSeric 	}
73735490626Seric 	errno = 0;
7385dfc646bSeric 
739ef66a9d0Seric 	CurHostName = m->m_mailer;
740ef66a9d0Seric 
74133db8731Seric 	/*
74233db8731Seric 	**  Deal with the special case of mail handled through an IPC
74333db8731Seric 	**  connection.
74433db8731Seric 	**	In this case we don't actually fork.  We must be
74533db8731Seric 	**	running SMTP for this to work.  We will return a
74633db8731Seric 	**	zero pid to indicate that we are running IPC.
747e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
74833db8731Seric 	*/
74933db8731Seric 
750e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
751e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
752e7c1bd78Seric 	{
753b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
7542a087e90Seric 		bzero((char *) mci, sizeof *mci);
75575889e88Seric 		mci->mci_in = stdin;
75675889e88Seric 		mci->mci_out = stdout;
757b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
75815d084d5Seric 		mci->mci_mailer = m;
759e7c1bd78Seric 	}
760b31e7f2bSeric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
761914346b1Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
76233db8731Seric 	{
76384f7cd1bSeric #ifdef DAEMON
764e103b48fSeric 		register int i;
7651277f9a8Seric 		register u_short port;
766e103b48fSeric 		char *curhost;
767b31e7f2bSeric 		extern MCI *mci_get();
768e103b48fSeric 		extern char *hostsignature();
76933db8731Seric 
770ef66a9d0Seric 		CurHostName = pvp[1];
771e103b48fSeric 		curhost = hostsignature(m, pvp[1], e);
772b31e7f2bSeric 
77378bfb85bSeric 		if (curhost == NULL || curhost[0] == '\0')
77478bfb85bSeric 		{
77578bfb85bSeric 			syserr("null signature");
77678bfb85bSeric 			return NULL;
77778bfb85bSeric 		}
77878bfb85bSeric 
77933db8731Seric 		if (!clever)
78078bfb85bSeric 		{
78108b25121Seric 			syserr("554 non-clever IPC");
78278bfb85bSeric 			return NULL;
78378bfb85bSeric 		}
78493b6e3cfSeric 		if (pvp[2] != NULL)
7851277f9a8Seric 			port = atoi(pvp[2]);
78693b6e3cfSeric 		else
7871277f9a8Seric 			port = 0;
788e103b48fSeric 		while (*curhost != '\0')
789ebc61751Sbloom 		{
790e103b48fSeric 			register char *p;
7917d55540cSeric 			static char hostbuf[MAXNAME];
792e103b48fSeric 
793e103b48fSeric 			/* pull the next host from the signature */
794e103b48fSeric 			p = strchr(curhost, ':');
795e103b48fSeric 			if (p == NULL)
796e103b48fSeric 				p = &curhost[strlen(curhost)];
797e103b48fSeric 			strncpy(hostbuf, curhost, p - curhost);
798e103b48fSeric 			hostbuf[p - curhost] = '\0';
799e103b48fSeric 			if (*p != '\0')
800e103b48fSeric 				p++;
801e103b48fSeric 			curhost = p;
802e103b48fSeric 
80345a8316eSeric 			/* see if we already know that this host is fried */
804e103b48fSeric 			CurHostName = hostbuf;
805e103b48fSeric 			mci = mci_get(hostbuf, m);
806b31e7f2bSeric 			if (mci->mci_state != MCIS_CLOSED)
8071fa7c2ebSeric 			{
8081fa7c2ebSeric 				if (tTd(11, 1))
8091fa7c2ebSeric 				{
8101fa7c2ebSeric 					printf("openmailer: ");
8111fa7c2ebSeric 					mci_dump(mci);
8121fa7c2ebSeric 				}
81367088a9dSeric 				CurHostName = mci->mci_host;
81475889e88Seric 				return mci;
8151fa7c2ebSeric 			}
81615d084d5Seric 			mci->mci_mailer = m;
817b31e7f2bSeric 			if (mci->mci_exitstat != EX_OK)
818b31e7f2bSeric 				continue;
819b31e7f2bSeric 
82075889e88Seric 			/* try the connection */
821e103b48fSeric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
82208b25121Seric 			message("Connecting to %s (%s)...",
823e103b48fSeric 				hostbuf, m->m_name);
824e103b48fSeric 			i = makeconnection(hostbuf, port, mci,
825914346b1Seric 				bitnset(M_SECURE_PORT, m->m_flags));
82675889e88Seric 			mci->mci_exitstat = i;
82775889e88Seric 			mci->mci_errno = errno;
82875889e88Seric 			if (i == EX_OK)
829b31e7f2bSeric 			{
830b31e7f2bSeric 				mci->mci_state = MCIS_OPENING;
831b31e7f2bSeric 				mci_cache(mci);
832b31e7f2bSeric 				break;
833b31e7f2bSeric 			}
834b31e7f2bSeric 			else if (tTd(11, 1))
835b31e7f2bSeric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
836b31e7f2bSeric 					i, errno);
837b31e7f2bSeric 
83875889e88Seric 
8395f73204aSeric 			/* enter status of this host */
84075889e88Seric 			setstat(i);
841ed854c7bSeric 		}
8422a087e90Seric 		mci->mci_pid = 0;
843b31e7f2bSeric #else /* no DAEMON */
84408b25121Seric 		syserr("554 openmailer: no IPC");
8451fa7c2ebSeric 		if (tTd(11, 1))
8461fa7c2ebSeric 			printf("openmailer: NULL\n");
84775889e88Seric 		return NULL;
848b31e7f2bSeric #endif /* DAEMON */
849588cad61Seric 	}
850b31e7f2bSeric 	else
851b31e7f2bSeric 	{
8526328bdf7Seric 		/* create a pipe to shove the mail through */
853f8952a83Seric 		if (pipe(mpvect) < 0)
85425a99e2eSeric 		{
855588cad61Seric 			syserr("openmailer: pipe (to mailer)");
8561fa7c2ebSeric 			if (tTd(11, 1))
8571fa7c2ebSeric 				printf("openmailer: NULL\n");
85875889e88Seric 			return NULL;
85925a99e2eSeric 		}
860c579ef51Seric 
861c579ef51Seric 		/* if this mailer speaks smtp, create a return pipe */
862c579ef51Seric 		if (clever && pipe(rpvect) < 0)
863c579ef51Seric 		{
864588cad61Seric 			syserr("openmailer: pipe (from mailer)");
865c579ef51Seric 			(void) close(mpvect[0]);
866c579ef51Seric 			(void) close(mpvect[1]);
8671fa7c2ebSeric 			if (tTd(11, 1))
8681fa7c2ebSeric 				printf("openmailer: NULL\n");
86975889e88Seric 			return NULL;
870c579ef51Seric 		}
871c579ef51Seric 
87233db8731Seric 		/*
87333db8731Seric 		**  Actually fork the mailer process.
87433db8731Seric 		**	DOFORK is clever about retrying.
8756984bfddSeric 		**
8766984bfddSeric 		**	Dispose of SIGCHLD signal catchers that may be laying
8776984bfddSeric 		**	around so that endmail will get it.
87833db8731Seric 		*/
87933db8731Seric 
880b31e7f2bSeric 		if (e->e_xfp != NULL)
881b31e7f2bSeric 			(void) fflush(e->e_xfp);		/* for debugging */
882588cad61Seric 		(void) fflush(stdout);
8836984bfddSeric # ifdef SIGCHLD
8846984bfddSeric 		(void) signal(SIGCHLD, SIG_DFL);
8856c2c3107Seric # endif /* SIGCHLD */
88684f7cd1bSeric 		DOFORK(FORK);
887f129ec7dSeric 		/* pid is set by DOFORK */
88825a99e2eSeric 		if (pid < 0)
88925a99e2eSeric 		{
89033db8731Seric 			/* failure */
891588cad61Seric 			syserr("openmailer: cannot fork");
892f8952a83Seric 			(void) close(mpvect[0]);
893f8952a83Seric 			(void) close(mpvect[1]);
894c579ef51Seric 			if (clever)
895c579ef51Seric 			{
896c579ef51Seric 				(void) close(rpvect[0]);
897c579ef51Seric 				(void) close(rpvect[1]);
898c579ef51Seric 			}
8991fa7c2ebSeric 			if (tTd(11, 1))
9001fa7c2ebSeric 				printf("openmailer: NULL\n");
90175889e88Seric 			return NULL;
90225a99e2eSeric 		}
90325a99e2eSeric 		else if (pid == 0)
90425a99e2eSeric 		{
90513088b9fSeric 			int i;
906dafbc479Seric 			int saveerrno;
90780c5307eSeric 			char **ep;
90880c5307eSeric 			char *env[MAXUSERENVIRON];
90980c5307eSeric 			extern char **environ;
9105f73204aSeric 			extern int DtableSize;
91113088b9fSeric 
91225a99e2eSeric 			/* child -- set up input & exec mailer */
91303ab8e55Seric 			/* make diagnostic output be standard output */
9148f0e7860Seric 			(void) signal(SIGINT, SIG_IGN);
9158f0e7860Seric 			(void) signal(SIGHUP, SIG_IGN);
9160984da9fSeric 			(void) signal(SIGTERM, SIG_DFL);
917f8952a83Seric 
9181caa9a59Seric 			/* close any other cached connections */
9191caa9a59Seric 			mci_flush(FALSE, mci);
9201caa9a59Seric 
921b31e7f2bSeric 			/* arrange to filter std & diag output of command */
922c579ef51Seric 			if (clever)
923c579ef51Seric 			{
924c579ef51Seric 				(void) close(rpvect[0]);
92565886f3dSeric 				(void) dup2(rpvect[1], STDOUT_FILENO);
926c579ef51Seric 				(void) close(rpvect[1]);
927c579ef51Seric 			}
928276723a8Seric 			else if (OpMode == MD_SMTP || HoldErrs)
929f8952a83Seric 			{
930588cad61Seric 				/* put mailer output in transcript */
93165886f3dSeric 				(void) dup2(fileno(e->e_xfp), STDOUT_FILENO);
932f8952a83Seric 			}
93365886f3dSeric 			(void) dup2(STDOUT_FILENO, STDERR_FILENO);
934f8952a83Seric 
935f8952a83Seric 			/* arrange to get standard input */
936f8952a83Seric 			(void) close(mpvect[1]);
93765886f3dSeric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
93825a99e2eSeric 			{
93925a99e2eSeric 				syserr("Cannot dup to zero!");
940a590b978Seric 				_exit(EX_OSERR);
94125a99e2eSeric 			}
942f8952a83Seric 			(void) close(mpvect[0]);
94357fc6f17Seric 			if (!bitnset(M_RESTR, m->m_flags))
9440984da9fSeric 			{
94553e3fa05Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
946e36b99e2Seric 				{
947e36b99e2Seric 					(void) setgid(DefGid);
948898a126bSbostic 					(void) initgroups(DefUser, DefGid);
949e36b99e2Seric 					(void) setuid(DefUid);
950e36b99e2Seric 				}
951e36b99e2Seric 				else
95269f29479Seric 				{
953e36b99e2Seric 					(void) setgid(ctladdr->q_gid);
954898a126bSbostic 					(void) initgroups(ctladdr->q_ruser?
955898a126bSbostic 						ctladdr->q_ruser: ctladdr->q_user,
956898a126bSbostic 						ctladdr->q_gid);
957e36b99e2Seric 					(void) setuid(ctladdr->q_uid);
95869f29479Seric 				}
9590984da9fSeric 			}
960588cad61Seric 
96113088b9fSeric 			/* arrange for all the files to be closed */
962b31e7f2bSeric 			for (i = 3; i < DtableSize; i++)
963b31e7f2bSeric 			{
964fccb3f7aSbostic 				register int j;
965fccb3f7aSbostic 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
966fccb3f7aSbostic 					(void)fcntl(i, F_SETFD, j|1);
967fccb3f7aSbostic 			}
96833db8731Seric 
96980c5307eSeric 			/* set up the mailer environment */
97080c5307eSeric 			i = 0;
97180c5307eSeric 			env[i++] = "AGENT=sendmail";
97280c5307eSeric 			for (ep = environ; *ep != NULL; ep++)
97380c5307eSeric 			{
97480c5307eSeric 				if (strncmp(*ep, "TZ=", 3) == 0)
97580c5307eSeric 					env[i++] = *ep;
97680c5307eSeric 			}
97780c5307eSeric 			env[i++] = NULL;
97880c5307eSeric 
97933db8731Seric 			/* try to execute the mailer */
980d7a0480eSeric 			execve(m->m_mailer, pvp, env);
981dafbc479Seric 			saveerrno = errno;
98213088b9fSeric 			syserr("Cannot exec %s", m->m_mailer);
98395b76e4bSeric 			if (m == LocalMailer)
98455f33c03Seric 				_exit(EX_TEMPFAIL);
985e2de2524Seric 			if (transienterror(saveerrno))
98695b76e4bSeric 				_exit(EX_TEMPFAIL);
987a590b978Seric 			_exit(EX_UNAVAILABLE);
98825a99e2eSeric 		}
98925a99e2eSeric 
990f8952a83Seric 		/*
991c579ef51Seric 		**  Set up return value.
992f8952a83Seric 		*/
993f8952a83Seric 
994b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
9952a087e90Seric 		bzero((char *) mci, sizeof *mci);
99615d084d5Seric 		mci->mci_mailer = m;
997b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9982a087e90Seric 		mci->mci_pid = pid;
999f8952a83Seric 		(void) close(mpvect[0]);
100075889e88Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1001c579ef51Seric 		if (clever)
100225a99e2eSeric 		{
1003c579ef51Seric 			(void) close(rpvect[1]);
100475889e88Seric 			mci->mci_in = fdopen(rpvect[0], "r");
100575889e88Seric 		}
100675889e88Seric 		else
100775889e88Seric 		{
100875889e88Seric 			mci->mci_flags |= MCIF_TEMP;
100975889e88Seric 			mci->mci_in = NULL;
101075889e88Seric 		}
1011b31e7f2bSeric 	}
1012b31e7f2bSeric 
1013b31e7f2bSeric 	/*
1014b31e7f2bSeric 	**  If we are in SMTP opening state, send initial protocol.
1015b31e7f2bSeric 	*/
1016b31e7f2bSeric 
1017b31e7f2bSeric 	if (clever && mci->mci_state != MCIS_CLOSED)
1018b31e7f2bSeric 	{
1019b31e7f2bSeric 		smtpinit(m, mci, e);
1020b31e7f2bSeric 	}
10211fa7c2ebSeric 	if (tTd(11, 1))
10221fa7c2ebSeric 	{
10231fa7c2ebSeric 		printf("openmailer: ");
10241fa7c2ebSeric 		mci_dump(mci);
10251fa7c2ebSeric 	}
1026c579ef51Seric 
102775889e88Seric 	return mci;
102825a99e2eSeric }
102925a99e2eSeric /*
103025a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
103125a99e2eSeric **
103225a99e2eSeric **	Parameters:
103325a99e2eSeric **		stat -- the status code from the mailer (high byte
103425a99e2eSeric **			only; core dumps must have been taken care of
103525a99e2eSeric **			already).
103681161401Seric **		m -- the mailer info for this mailer.
103781161401Seric **		mci -- the mailer connection info -- can be NULL if the
103881161401Seric **			response is given before the connection is made.
103981161401Seric **		e -- the current envelope.
104025a99e2eSeric **
104125a99e2eSeric **	Returns:
1042db8841e9Seric **		none.
104325a99e2eSeric **
104425a99e2eSeric **	Side Effects:
1045c1f9df2cSeric **		Errors may be incremented.
104625a99e2eSeric **		ExitStat may be set.
104725a99e2eSeric */
104825a99e2eSeric 
104981161401Seric giveresponse(stat, m, mci, e)
105025a99e2eSeric 	int stat;
1051588cad61Seric 	register MAILER *m;
105281161401Seric 	register MCI *mci;
1053198d9be0Seric 	ENVELOPE *e;
105425a99e2eSeric {
105525a99e2eSeric 	register char *statmsg;
105625a99e2eSeric 	extern char *SysExMsg[];
105725a99e2eSeric 	register int i;
1058d4bd8f0eSbostic 	extern int N_SysEx;
1059d4bd8f0eSbostic #ifdef NAMED_BIND
1060d4bd8f0eSbostic 	extern int h_errno;
1061d4bd8f0eSbostic #endif
1062198d9be0Seric 	char buf[MAXLINE];
10637d55540cSeric 	extern char *errstring();
106425a99e2eSeric 
106513bbc08cSeric 	/*
106613bbc08cSeric 	**  Compute status message from code.
106713bbc08cSeric 	*/
106813bbc08cSeric 
106925a99e2eSeric 	i = stat - EX__BASE;
1070588cad61Seric 	if (stat == 0)
1071588cad61Seric 		statmsg = "250 Sent";
1072588cad61Seric 	else if (i < 0 || i > N_SysEx)
1073588cad61Seric 	{
1074588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1075588cad61Seric 		stat = EX_UNAVAILABLE;
1076588cad61Seric 		statmsg = buf;
1077588cad61Seric 	}
1078198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1079198d9be0Seric 	{
10807d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1081d4bd8f0eSbostic #ifdef NAMED_BIND
1082f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1083f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1084f28da541Smiriam 		else
1085d4bd8f0eSbostic #endif
1086f28da541Smiriam 		{
10878557d168Seric 			if (errno != 0)
1088d87e85f3Seric 				statmsg = errstring(errno);
1089d87e85f3Seric 			else
1090d87e85f3Seric 			{
1091d87e85f3Seric #ifdef SMTP
1092d87e85f3Seric 				extern char SmtpError[];
1093d87e85f3Seric 
1094d87e85f3Seric 				statmsg = SmtpError;
10956c2c3107Seric #else /* SMTP */
1096d87e85f3Seric 				statmsg = NULL;
10976c2c3107Seric #endif /* SMTP */
1098d87e85f3Seric 			}
1099f28da541Smiriam 		}
1100d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1101d87e85f3Seric 		{
110287c9b3e7Seric 			(void) strcat(buf, ": ");
1103d87e85f3Seric 			(void) strcat(buf, statmsg);
11048557d168Seric 		}
1105198d9be0Seric 		statmsg = buf;
1106198d9be0Seric 	}
110725a99e2eSeric 	else
1108d87e85f3Seric 	{
110925a99e2eSeric 		statmsg = SysExMsg[i];
11107d55540cSeric 		if (*statmsg++ == ':')
11117d55540cSeric 		{
11127d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
11137d55540cSeric 			statmsg = buf;
11147d55540cSeric 		}
1115d87e85f3Seric 	}
1116588cad61Seric 
1117588cad61Seric 	/*
1118588cad61Seric 	**  Print the message as appropriate
1119588cad61Seric 	*/
1120588cad61Seric 
1121198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1122e12d03eeSeric 		message(&statmsg[4], errstring(errno));
112325a99e2eSeric 	else
112425a99e2eSeric 	{
1125c1f9df2cSeric 		Errors++;
1126e12d03eeSeric 		usrerr(statmsg, errstring(errno));
112725a99e2eSeric 	}
112825a99e2eSeric 
112925a99e2eSeric 	/*
113025a99e2eSeric 	**  Final cleanup.
113125a99e2eSeric 	**	Log a record of the transaction.  Compute the new
113225a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
113325a99e2eSeric 	**	that.
113425a99e2eSeric 	*/
113525a99e2eSeric 
11362f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
113781161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1138eb238f8cSeric 
1139eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1140eb238f8cSeric 		setstat(stat);
1141198d9be0Seric 	if (stat != EX_OK)
1142198d9be0Seric 	{
1143198d9be0Seric 		if (e->e_message != NULL)
1144198d9be0Seric 			free(e->e_message);
1145198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1146198d9be0Seric 	}
11478557d168Seric 	errno = 0;
1148d4bd8f0eSbostic #ifdef NAMED_BIND
1149f28da541Smiriam 	h_errno = 0;
1150d4bd8f0eSbostic #endif
1151eb238f8cSeric }
1152eb238f8cSeric /*
1153eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1154eb238f8cSeric **
1155eb238f8cSeric **	Parameters:
115681161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
115781161401Seric **		mci -- the mailer connection info -- can be NULL if the
115881161401Seric **			log is occuring when no connection is active.
115981161401Seric **		stat -- the message to print for the status.
116081161401Seric **		e -- the current envelope.
1161eb238f8cSeric **
1162eb238f8cSeric **	Returns:
1163eb238f8cSeric **		none
1164eb238f8cSeric **
1165eb238f8cSeric **	Side Effects:
1166eb238f8cSeric **		none
1167eb238f8cSeric */
1168eb238f8cSeric 
116981161401Seric logdelivery(m, mci, stat, e)
117081161401Seric 	MAILER *m;
117181161401Seric 	register MCI *mci;
1172eb238f8cSeric 	char *stat;
1173b31e7f2bSeric 	register ENVELOPE *e;
11745cf56be3Seric {
1175eb238f8cSeric # ifdef LOG
11769507d1f9Seric 	char *curhost;
1177d6acf3eeSeric 	char buf[512];
11789507d1f9Seric 	extern char *pintvl();
11799507d1f9Seric 	extern char *macvalue();
11809507d1f9Seric 
118148ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
118281161401Seric 
118348ed5d33Seric 	if (m != NULL)
118471ff6caaSeric 	{
118548ed5d33Seric 		(void) strcat(buf, ", mailer=");
118648ed5d33Seric 		(void) strcat(buf, m->m_name);
118771ff6caaSeric 	}
118848ed5d33Seric 
118948ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
119071ff6caaSeric 	{
119171ff6caaSeric # ifdef DAEMON
1192*e2f2f828Seric 		extern SOCKADDR CurHostAddr;
1193*e2f2f828Seric 		extern char *anynet_ntoa();
119448ed5d33Seric # endif
119571ff6caaSeric 
119648ed5d33Seric 		(void) strcat(buf, ", relay=");
119748ed5d33Seric 		(void) strcat(buf, mci->mci_host);
119848ed5d33Seric 
119948ed5d33Seric # ifdef DAEMON
120048ed5d33Seric 		(void) strcat(buf, " (");
1201*e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
120248ed5d33Seric 		(void) strcat(buf, ")");
120371ff6caaSeric # endif
120471ff6caaSeric 	}
12059507d1f9Seric 	else
120648ed5d33Seric 	{
120748ed5d33Seric 		char *p = macvalue('h', e);
12089507d1f9Seric 
120948ed5d33Seric 		if (p != NULL && p[0] != '\0')
121048ed5d33Seric 		{
121148ed5d33Seric 			(void) strcat(buf, ", relay=");
121248ed5d33Seric 			(void) strcat(buf, p);
121348ed5d33Seric 		}
121448ed5d33Seric 	}
1215d6acf3eeSeric 
1216d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1217d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
12186c2c3107Seric # endif /* LOG */
121925a99e2eSeric }
122025a99e2eSeric /*
122151552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
122225a99e2eSeric **
122351552439Seric **	This can be made an arbitrary message separator by changing $l
122451552439Seric **
12259b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
12269b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
12279b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
12289b6c17a6Seric **	this kind of antique garbage????
122925a99e2eSeric **
123025a99e2eSeric **	Parameters:
123151552439Seric **		fp -- the file to output to.
123251552439Seric **		m -- the mailer describing this entry.
123325a99e2eSeric **
123425a99e2eSeric **	Returns:
123551552439Seric **		none
123625a99e2eSeric **
123725a99e2eSeric **	Side Effects:
123851552439Seric **		outputs some text to fp.
123925a99e2eSeric */
124025a99e2eSeric 
1241b31e7f2bSeric putfromline(fp, m, e)
124251552439Seric 	register FILE *fp;
124351552439Seric 	register MAILER *m;
1244b31e7f2bSeric 	ENVELOPE *e;
124525a99e2eSeric {
12462bc47524Seric 	char *template = "\201l\n";
124751552439Seric 	char buf[MAXLINE];
124825a99e2eSeric 
124957fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
125051552439Seric 		return;
125113bbc08cSeric 
12522c7e1b8dSeric # ifdef UGLYUUCP
125357fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
125474b6e67bSeric 	{
1255ea09d6edSeric 		char *bang;
1256ea09d6edSeric 		char xbuf[MAXLINE];
125774b6e67bSeric 
1258ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
12596c2c3107Seric 		bang = strchr(buf, '!');
126074b6e67bSeric 		if (bang == NULL)
126108b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
126274b6e67bSeric 		else
1263588cad61Seric 		{
1264ea09d6edSeric 			*bang++ = '\0';
12652bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1266ea09d6edSeric 			template = xbuf;
126774b6e67bSeric 		}
1268588cad61Seric 	}
12696c2c3107Seric # endif /* UGLYUUCP */
1270b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
127177b52738Seric 	putline(buf, fp, m);
1272bc6e2962Seric }
1273bc6e2962Seric /*
127451552439Seric **  PUTBODY -- put the body of a message.
127551552439Seric **
127651552439Seric **	Parameters:
127751552439Seric **		fp -- file to output onto.
127877b52738Seric **		m -- a mailer descriptor to control output format.
12799a6a5f55Seric **		e -- the envelope to put out.
128051552439Seric **
128151552439Seric **	Returns:
128251552439Seric **		none.
128351552439Seric **
128451552439Seric **	Side Effects:
128551552439Seric **		The message is written onto fp.
128651552439Seric */
128751552439Seric 
128877b52738Seric putbody(fp, m, e)
128951552439Seric 	FILE *fp;
1290588cad61Seric 	MAILER *m;
12919a6a5f55Seric 	register ENVELOPE *e;
129251552439Seric {
129377b52738Seric 	char buf[MAXLINE];
129451552439Seric 
129551552439Seric 	/*
129651552439Seric 	**  Output the body of the message
129751552439Seric 	*/
129851552439Seric 
12999a6a5f55Seric 	if (e->e_dfp == NULL)
130051552439Seric 	{
13019a6a5f55Seric 		if (e->e_df != NULL)
13029a6a5f55Seric 		{
13039a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
13049a6a5f55Seric 			if (e->e_dfp == NULL)
13058f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
13068f9146b0Srick 				e->e_df, e->e_to, e->e_from);
13079a6a5f55Seric 		}
13089a6a5f55Seric 		else
130977b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
13109a6a5f55Seric 	}
13119a6a5f55Seric 	if (e->e_dfp != NULL)
13129a6a5f55Seric 	{
13139a6a5f55Seric 		rewind(e->e_dfp);
131477b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
131524fc8aeeSeric 		{
131624fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1317d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
13183462ad9eSeric 				(void) putc('>', fp);
131977b52738Seric 			putline(buf, fp, m);
132024fc8aeeSeric 		}
132151552439Seric 
13229a6a5f55Seric 		if (ferror(e->e_dfp))
132351552439Seric 		{
132451552439Seric 			syserr("putbody: read error");
132551552439Seric 			ExitStat = EX_IOERR;
132651552439Seric 		}
132751552439Seric 	}
132851552439Seric 
132951552439Seric 	(void) fflush(fp);
133051552439Seric 	if (ferror(fp) && errno != EPIPE)
133151552439Seric 	{
133251552439Seric 		syserr("putbody: write error");
133351552439Seric 		ExitStat = EX_IOERR;
133451552439Seric 	}
133551552439Seric 	errno = 0;
133625a99e2eSeric }
133725a99e2eSeric /*
133825a99e2eSeric **  MAILFILE -- Send a message to a file.
133925a99e2eSeric **
1340f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1341f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1342f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1343f129ec7dSeric **	sendmail runs as root.
1344f129ec7dSeric **
1345588cad61Seric **	This could be done as a subordinate mailer, except that it
1346588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1347588cad61Seric **	view this as being sufficiently important as to include it
1348588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1349588cad61Seric **	to create another process plus some pipes to save the message.
1350588cad61Seric **
135125a99e2eSeric **	Parameters:
135225a99e2eSeric **		filename -- the name of the file to send to.
13536259796dSeric **		ctladdr -- the controlling address header -- includes
13546259796dSeric **			the userid/groupid to be when sending.
135525a99e2eSeric **
135625a99e2eSeric **	Returns:
135725a99e2eSeric **		The exit code associated with the operation.
135825a99e2eSeric **
135925a99e2eSeric **	Side Effects:
136025a99e2eSeric **		none.
136125a99e2eSeric */
136225a99e2eSeric 
1363b31e7f2bSeric mailfile(filename, ctladdr, e)
136425a99e2eSeric 	char *filename;
13656259796dSeric 	ADDRESS *ctladdr;
1366b31e7f2bSeric 	register ENVELOPE *e;
136725a99e2eSeric {
136825a99e2eSeric 	register FILE *f;
136932d19d43Seric 	register int pid;
137015d084d5Seric 	int mode;
137125a99e2eSeric 
137232d19d43Seric 	/*
137332d19d43Seric 	**  Fork so we can change permissions here.
137432d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
137532d19d43Seric 	**	the complications of calling subroutines, etc.
137632d19d43Seric 	*/
137732d19d43Seric 
137832d19d43Seric 	DOFORK(fork);
137932d19d43Seric 
138032d19d43Seric 	if (pid < 0)
138132d19d43Seric 		return (EX_OSERR);
138232d19d43Seric 	else if (pid == 0)
138332d19d43Seric 	{
138432d19d43Seric 		/* child -- actually write to file */
1385f129ec7dSeric 		struct stat stb;
1386f129ec7dSeric 
13870984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
13880984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
13890984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
13903462ad9eSeric 		(void) umask(OldUmask);
139195f16dc0Seric 
1392f129ec7dSeric 		if (stat(filename, &stb) < 0)
1393e6e1265fSeric 			stb.st_mode = 0666;
139415d084d5Seric 		mode = stb.st_mode;
139595f16dc0Seric 
139695f16dc0Seric 		/* limit the errors to those actually caused in the child */
139795f16dc0Seric 		errno = 0;
139895f16dc0Seric 		ExitStat = EX_OK;
139995f16dc0Seric 
1400f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1401f129ec7dSeric 			exit(EX_CANTCREAT);
140203827b5fSeric 		if (ctladdr == NULL)
14038f9146b0Srick 			ctladdr = &e->e_from;
140415d084d5Seric 		else
140515d084d5Seric 		{
140615d084d5Seric 			/* ignore setuid and setgid bits */
140715d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
140815d084d5Seric 		}
140915d084d5Seric 
14108f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
14118f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
14128f9146b0Srick 		{
14138f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
141495f16dc0Seric 			if (e->e_dfp == NULL)
141595f16dc0Seric 			{
14168f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
14178f9146b0Srick 					e->e_df, e->e_to, e->e_from);
14188f9146b0Srick 			}
14198f9146b0Srick 		}
14208f9146b0Srick 
142115d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1422e36b99e2Seric 		{
142395f16dc0Seric 			if (ctladdr->q_uid == 0)
142495f16dc0Seric 			{
1425e36b99e2Seric 				(void) setgid(DefGid);
1426898a126bSbostic 				(void) initgroups(DefUser, DefGid);
142795f16dc0Seric 			}
142895f16dc0Seric 			else
142995f16dc0Seric 			{
14306259796dSeric 				(void) setgid(ctladdr->q_gid);
1431898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1432898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1433898a126bSbostic 					ctladdr->q_gid);
1434898a126bSbostic 			}
1435e36b99e2Seric 		}
143615d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1437e36b99e2Seric 		{
1438e36b99e2Seric 			if (ctladdr->q_uid == 0)
1439e36b99e2Seric 				(void) setuid(DefUid);
1440e36b99e2Seric 			else
14416259796dSeric 				(void) setuid(ctladdr->q_uid);
1442e36b99e2Seric 		}
144395f16dc0Seric 		FileName = filename;
144495f16dc0Seric 		LineNumber = 0;
144527628d59Seric 		f = dfopen(filename, "a");
144625a99e2eSeric 		if (f == NULL)
144795f16dc0Seric 		{
144808b25121Seric 			message("554 cannot open");
144932d19d43Seric 			exit(EX_CANTCREAT);
145095f16dc0Seric 		}
145125a99e2eSeric 
1452b31e7f2bSeric 		putfromline(f, ProgMailer, e);
1453b843d759Seric 		(*e->e_puthdr)(f, ProgMailer, e);
145477b52738Seric 		putline("\n", f, ProgMailer);
1455b843d759Seric 		(*e->e_putbody)(f, ProgMailer, e);
145677b52738Seric 		putline("\n", f, ProgMailer);
145795f16dc0Seric 		if (ferror(f))
145895f16dc0Seric 		{
145908b25121Seric 			message("451 I/O error");
146095f16dc0Seric 			setstat(EX_IOERR);
146195f16dc0Seric 		}
1462ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
146332d19d43Seric 		(void) fflush(stdout);
1464e36b99e2Seric 
146527628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1466c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
146795f16dc0Seric 		exit(ExitStat);
146813bbc08cSeric 		/*NOTREACHED*/
146932d19d43Seric 	}
147032d19d43Seric 	else
147132d19d43Seric 	{
147232d19d43Seric 		/* parent -- wait for exit status */
1473588cad61Seric 		int st;
147432d19d43Seric 
1475588cad61Seric 		st = waitfor(pid);
1476588cad61Seric 		if ((st & 0377) != 0)
1477588cad61Seric 			return (EX_UNAVAILABLE);
1478588cad61Seric 		else
1479588cad61Seric 			return ((st >> 8) & 0377);
14808f9146b0Srick 		/*NOTREACHED*/
148132d19d43Seric 	}
148225a99e2eSeric }
1483ea4dc939Seric /*
1484ea4dc939Seric **  SENDALL -- actually send all the messages.
1485ea4dc939Seric **
1486ea4dc939Seric **	Parameters:
14870c52a0b3Seric **		e -- the envelope to send.
14887b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
1489511ad433Seric **			the current e->e_sendmode.
1490ea4dc939Seric **
1491ea4dc939Seric **	Returns:
1492ea4dc939Seric **		none.
1493ea4dc939Seric **
1494ea4dc939Seric **	Side Effects:
1495ea4dc939Seric **		Scans the send lists and sends everything it finds.
14960c52a0b3Seric **		Delivers any appropriate error messages.
1497276723a8Seric **		If we are running in a non-interactive mode, takes the
1498276723a8Seric **			appropriate action.
1499ea4dc939Seric */
1500ea4dc939Seric 
1501276723a8Seric sendall(e, mode)
15020c52a0b3Seric 	ENVELOPE *e;
1503276723a8Seric 	char mode;
1504ea4dc939Seric {
1505e77e673fSeric 	register ADDRESS *q;
15069836bea2Seric 	char *owner;
15079836bea2Seric 	int otherowners;
15081cc3a5d3Seric 	register ENVELOPE *ee;
15099836bea2Seric 	ENVELOPE *splitenv = NULL;
1510ea4dc939Seric 
15117b95031aSeric 	/* determine actual delivery mode */
15127b95031aSeric 	if (mode == SM_DEFAULT)
15137b95031aSeric 	{
15145f73204aSeric 		extern bool shouldqueue();
15157b95031aSeric 
1516511ad433Seric 		mode = e->e_sendmode;
15173f324333Seric 		if (mode != SM_VERIFY &&
15183f324333Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
15193f324333Seric 			mode = SM_QUEUE;
15207b95031aSeric 	}
15217b95031aSeric 
1522df864a8fSeric 	if (tTd(13, 1))
1523772e6e50Seric 	{
1524ee4b0922Seric 		printf("\nSENDALL: mode %c, e_from ", mode);
1525ee4b0922Seric 		printaddr(&e->e_from, FALSE);
1526ee4b0922Seric 		printf("sendqueue:\n");
15270c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1528772e6e50Seric 	}
1529ea4dc939Seric 
15300c52a0b3Seric 	/*
1531276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1532588cad61Seric 	**	Check to make sure the hop count is reasonable.
1533588cad61Seric 	**	Delete sends to the sender in mailing lists.
1534276723a8Seric 	*/
1535276723a8Seric 
1536588cad61Seric 	CurEnv = e;
1537276723a8Seric 
15381d57450bSeric 	if (e->e_hopcount > MaxHopCount)
1539276723a8Seric 	{
15408f9146b0Srick 		errno = 0;
154108b25121Seric 		syserr("554 too many hops %d (%d max): from %s, to %s",
1542655518ecSeric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
1543655518ecSeric 			e->e_sendqueue->q_paddr);
1544588cad61Seric 		return;
1545588cad61Seric 	}
1546588cad61Seric 
1547588cad61Seric 	if (!MeToo)
1548276723a8Seric 	{
1549f3d6c55cSeric 		extern ADDRESS *recipient();
1550f3d6c55cSeric 
155175f1ade9Seric 		if (tTd(13, 5))
155275f1ade9Seric 		{
155375f1ade9Seric 			printf("sendall: QDONTSEND ");
155475f1ade9Seric 			printaddr(&e->e_from, FALSE);
155575f1ade9Seric 		}
1556ee4b0922Seric 		e->e_from.q_flags |= QDONTSEND;
1557b843d759Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1558276723a8Seric 	}
1559588cad61Seric 
15609836bea2Seric 	/*
15619836bea2Seric 	**  Handle alias owners.
15629836bea2Seric 	**
15639836bea2Seric 	**	We scan up the q_alias chain looking for owners.
15649836bea2Seric 	**	We discard owners that are the same as the return path.
15659836bea2Seric 	*/
15669836bea2Seric 
15679836bea2Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15689836bea2Seric 	{
15699836bea2Seric 		register struct address *a;
15709836bea2Seric 
15719836bea2Seric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
15729836bea2Seric 			continue;
15739836bea2Seric 		if (a != NULL)
15749836bea2Seric 			q->q_owner = a->q_owner;
15759836bea2Seric 
1576ee4b0922Seric 		if (q->q_owner != NULL &&
1577ee4b0922Seric 		    !bitset(QDONTSEND, q->q_flags) &&
1578ee4b0922Seric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
15799836bea2Seric 			q->q_owner = NULL;
15809836bea2Seric 	}
15819836bea2Seric 
15829836bea2Seric 	owner = "";
15839836bea2Seric 	otherowners = 1;
15849836bea2Seric 	while (owner != NULL && otherowners > 0)
15859836bea2Seric 	{
15869836bea2Seric 		owner = NULL;
15879836bea2Seric 		otherowners = 0;
15889836bea2Seric 
15899836bea2Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15909836bea2Seric 		{
15919836bea2Seric 			if (bitset(QDONTSEND, q->q_flags))
15929836bea2Seric 				continue;
15939836bea2Seric 
15949836bea2Seric 			if (q->q_owner != NULL)
15959836bea2Seric 			{
15969836bea2Seric 				if (owner == NULL)
15979836bea2Seric 					owner = q->q_owner;
15989836bea2Seric 				else if (owner != q->q_owner)
15999836bea2Seric 				{
16009836bea2Seric 					if (strcmp(owner, q->q_owner) == 0)
16019836bea2Seric 					{
16029836bea2Seric 						/* make future comparisons cheap */
16039836bea2Seric 						q->q_owner = owner;
16049836bea2Seric 					}
16059836bea2Seric 					else
16069836bea2Seric 					{
16079836bea2Seric 						otherowners++;
16089836bea2Seric 					}
16099836bea2Seric 					owner = q->q_owner;
16109836bea2Seric 				}
16119836bea2Seric 			}
16129836bea2Seric 			else
16139836bea2Seric 			{
16149836bea2Seric 				otherowners++;
16159836bea2Seric 			}
16169836bea2Seric 		}
16179836bea2Seric 
16189836bea2Seric 		if (owner != NULL && otherowners > 0)
16199836bea2Seric 		{
16209836bea2Seric 			extern HDR *copyheader();
16219836bea2Seric 			extern ADDRESS *copyqueue();
16229836bea2Seric 
1623ee4b0922Seric 			/*
1624ee4b0922Seric 			**  Split this envelope into two.
1625ee4b0922Seric 			*/
1626ee4b0922Seric 
16279836bea2Seric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
1628ee4b0922Seric 			*ee = *e;
16299836bea2Seric 			ee->e_id = NULL;
1630ee4b0922Seric 			(void) queuename(ee, '\0');
1631ee4b0922Seric 
1632ee4b0922Seric 			if (tTd(13, 1))
1633ee4b0922Seric 				printf("sendall: split %s into %s\n",
1634ee4b0922Seric 					e->e_id, ee->e_id);
1635ee4b0922Seric 
16369836bea2Seric 			ee->e_header = copyheader(e->e_header);
16379836bea2Seric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
16389836bea2Seric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
16399836bea2Seric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE);
1640579ef0ddSeric 			setsender(owner, ee, NULL, TRUE);
1641ee4b0922Seric 			if (tTd(13, 5))
1642ee4b0922Seric 			{
1643ee4b0922Seric 				printf("sendall(split): QDONTSEND ");
1644ee4b0922Seric 				printaddr(&ee->e_from, FALSE);
1645ee4b0922Seric 			}
1646ee4b0922Seric 			ee->e_from.q_flags |= QDONTSEND;
1647ee4b0922Seric 			ee->e_dfp = NULL;
1648ee4b0922Seric 			ee->e_xfp = NULL;
1649ee4b0922Seric 			ee->e_lockfp = NULL;
1650ee4b0922Seric 			ee->e_df = NULL;
1651511ad433Seric 			ee->e_errormode = EM_MAIL;
16529836bea2Seric 			ee->e_sibling = splitenv;
16539836bea2Seric 			splitenv = ee;
16549836bea2Seric 
16559836bea2Seric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
16569836bea2Seric 				if (q->q_owner == owner)
16579836bea2Seric 					q->q_flags |= QDONTSEND;
16589836bea2Seric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
16599836bea2Seric 				if (q->q_owner != owner)
16609836bea2Seric 					q->q_flags |= QDONTSEND;
16619836bea2Seric 
16629836bea2Seric 			if (e->e_df != NULL && mode != SM_VERIFY)
16639836bea2Seric 			{
16649836bea2Seric 				ee->e_dfp = NULL;
1665239b3abaSeric 				ee->e_df = newstr(queuename(ee, 'd'));
16669836bea2Seric 				if (link(e->e_df, ee->e_df) < 0)
16679836bea2Seric 				{
16689836bea2Seric 					syserr("sendall: link(%s, %s)",
16699836bea2Seric 						e->e_df, ee->e_df);
16709836bea2Seric 				}
16719836bea2Seric 			}
1672ee4b0922Seric 
1673ee4b0922Seric 			if (mode != SM_VERIFY)
1674ee4b0922Seric 			{
1675ee4b0922Seric 				char xfbuf1[20], xfbuf2[20];
1676ee4b0922Seric 
1677ee4b0922Seric 				(void) strcpy(xfbuf1, queuename(e, 'x'));
1678ee4b0922Seric 				(void) strcpy(xfbuf2, queuename(ee, 'x'));
1679ee4b0922Seric 				if (link(xfbuf1, xfbuf2) < 0)
1680ee4b0922Seric 				{
1681ee4b0922Seric 					syserr("sendall: link(%s, %s)",
1682ee4b0922Seric 						xfbuf1, xfbuf2);
1683ee4b0922Seric 				}
1684ee4b0922Seric 			}
1685336a44ebSeric #ifdef LOG
1686336a44ebSeric 			if (LogLevel > 4)
1687336a44ebSeric 				syslog(LOG_INFO, "%s: clone %s",
1688336a44ebSeric 					ee->e_id, e->e_id);
1689336a44ebSeric #endif
16909836bea2Seric 		}
16919836bea2Seric 	}
16929836bea2Seric 
16939836bea2Seric 	if (owner != NULL)
1694ee4b0922Seric 	{
1695579ef0ddSeric 		setsender(owner, e, NULL, TRUE);
1696ee4b0922Seric 		if (tTd(13, 5))
1697ee4b0922Seric 		{
1698ee4b0922Seric 			printf("sendall(owner): QDONTSEND ");
1699ee4b0922Seric 			printaddr(&e->e_from, FALSE);
1700ee4b0922Seric 		}
1701ee4b0922Seric 		e->e_from.q_flags |= QDONTSEND;
1702511ad433Seric 		e->e_errormode = EM_MAIL;
1703ee4b0922Seric 	}
17049836bea2Seric 
1705588cad61Seric # ifdef QUEUE
1706b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1707b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1708b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
17091cc3a5d3Seric 	{
17101cc3a5d3Seric 		/* be sure everything is instantiated in the queue */
1711e020b127Seric 		queueup(e, TRUE, mode == SM_QUEUE);
17121cc3a5d3Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
17131cc3a5d3Seric 			queueup(ee, TRUE, mode == SM_QUEUE);
17141cc3a5d3Seric 	}
17156c2c3107Seric #endif /* QUEUE */
1716276723a8Seric 
17179836bea2Seric 	if (splitenv != NULL)
17189836bea2Seric 	{
17199836bea2Seric 		if (tTd(13, 1))
17209836bea2Seric 		{
17219836bea2Seric 			printf("\nsendall: Split queue; remaining queue:\n");
17229836bea2Seric 			printaddr(e->e_sendqueue, TRUE);
17239836bea2Seric 		}
17249836bea2Seric 
1725ee4b0922Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
17269836bea2Seric 		{
1727ee4b0922Seric 			CurEnv = ee;
1728ee4b0922Seric 			sendenvelope(ee, mode);
17299836bea2Seric 		}
17309836bea2Seric 
17319836bea2Seric 		CurEnv = e;
17329836bea2Seric 	}
1733ee4b0922Seric 	sendenvelope(e, mode);
1734ee4b0922Seric 
1735ee4b0922Seric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
1736ee4b0922Seric 		dropenvelope(splitenv);
1737ee4b0922Seric }
1738ee4b0922Seric 
1739ee4b0922Seric sendenvelope(e, mode)
1740ee4b0922Seric 	register ENVELOPE *e;
1741ee4b0922Seric 	char mode;
1742ee4b0922Seric {
1743ee4b0922Seric 	bool oldverbose;
1744ee4b0922Seric 	int pid;
1745ee4b0922Seric 	register ADDRESS *q;
17465ff3c5eaSeric #ifdef LOCKF
17475ff3c5eaSeric 	struct flock lfd;
17485ff3c5eaSeric #endif
17499836bea2Seric 
1750276723a8Seric 	oldverbose = Verbose;
1751276723a8Seric 	switch (mode)
1752276723a8Seric 	{
1753276723a8Seric 	  case SM_VERIFY:
1754276723a8Seric 		Verbose = TRUE;
1755276723a8Seric 		break;
1756276723a8Seric 
1757276723a8Seric 	  case SM_QUEUE:
1758c7f5410dSeric   queueonly:
1759b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1760276723a8Seric 		return;
1761276723a8Seric 
1762276723a8Seric 	  case SM_FORK:
17639a6a5f55Seric 		if (e->e_xfp != NULL)
17649a6a5f55Seric 			(void) fflush(e->e_xfp);
1765c7f5410dSeric 
1766c7f5410dSeric # ifdef LOCKF
1767c7f5410dSeric 		/*
1768c7f5410dSeric 		**  Since lockf has the interesting semantic that the
17691c265a00Seric 		**  lock is lost when we fork, we have to risk losing
17701c265a00Seric 		**  the lock here by closing before the fork, and then
17711c265a00Seric 		**  trying to get it back in the child.
1772c7f5410dSeric 		*/
1773c7f5410dSeric 
1774e020b127Seric 		if (e->e_lockfp != NULL)
1775c7f5410dSeric 		{
1776ee4b0922Seric 			(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
1777e020b127Seric 			e->e_lockfp = NULL;
1778c7f5410dSeric 		}
1779c7f5410dSeric # endif /* LOCKF */
1780c7f5410dSeric 
1781276723a8Seric 		pid = fork();
1782276723a8Seric 		if (pid < 0)
1783276723a8Seric 		{
1784c7f5410dSeric 			goto queueonly;
1785276723a8Seric 		}
1786276723a8Seric 		else if (pid > 0)
1787a6fce3d8Seric 		{
1788a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1789b254bcb6Seric 			e->e_id = e->e_df = NULL;
1790c7f5410dSeric # ifndef LOCKF
1791e020b127Seric 			if (e->e_lockfp != NULL)
179275f1ade9Seric 			{
1793ee4b0922Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
179475f1ade9Seric 				e->e_lockfp = NULL;
179575f1ade9Seric 			}
1796c7f5410dSeric # endif
179775f1ade9Seric 
179875f1ade9Seric 			/* close any random open files in the envelope */
179975f1ade9Seric 			if (e->e_dfp != NULL)
180075f1ade9Seric 			{
1801ee4b0922Seric 				(void) xfclose(e->e_dfp, "sendenvelope", "dfp");
180275f1ade9Seric 				e->e_dfp = NULL;
180375f1ade9Seric 			}
180475f1ade9Seric 			if (e->e_xfp != NULL)
180575f1ade9Seric 			{
1806ee4b0922Seric 				(void) xfclose(e->e_xfp, "sendenvelope", "xfp");
180775f1ade9Seric 				e->e_xfp = NULL;
180875f1ade9Seric 			}
1809276723a8Seric 			return;
1810a6fce3d8Seric 		}
1811276723a8Seric 
1812276723a8Seric 		/* double fork to avoid zombies */
1813276723a8Seric 		if (fork() > 0)
1814276723a8Seric 			exit(EX_OK);
1815276723a8Seric 
1816a6fce3d8Seric 		/* be sure we are immune from the terminal */
18178dadacffSeric 		disconnect(FALSE, e);
1818a6fce3d8Seric 
1819e6720d51Seric # ifdef LOCKF
1820e6720d51Seric 		/*
1821c7f5410dSeric 		**  Now try to get our lock back.
1822e6720d51Seric 		*/
1823e6720d51Seric 
18241c265a00Seric 		lfd.l_type = F_WRLCK;
18251c265a00Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1826e020b127Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1827e020b127Seric 		if (e->e_lockfp == NULL ||
18281c265a00Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1829e6720d51Seric 		{
1830e6720d51Seric 			/* oops....  lost it */
1831336a44ebSeric 			if (tTd(13, 1))
1832336a44ebSeric 				printf("sendenvelope: %s lost lock: lockfp=%x, %s\n",
1833336a44ebSeric 					e->e_id, e->e_lockfp, errstring(errno));
1834336a44ebSeric 
1835e6720d51Seric # ifdef LOG
18362f624c86Seric 			if (LogLevel > 29)
1837c7f5410dSeric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1838b31e7f2bSeric 					e->e_id);
1839e6720d51Seric # endif /* LOG */
1840e6720d51Seric 			exit(EX_OK);
1841e6720d51Seric 		}
1842e6720d51Seric # endif /* LOCKF */
1843e6720d51Seric 
18441caa9a59Seric 		/*
18451caa9a59Seric 		**  Close any cached connections.
18461caa9a59Seric 		**
18471caa9a59Seric 		**	We don't send the QUIT protocol because the parent
18481caa9a59Seric 		**	still knows about the connection.
18491caa9a59Seric 		**
18501caa9a59Seric 		**	This should only happen when delivering an error
18511caa9a59Seric 		**	message.
18521caa9a59Seric 		*/
18531caa9a59Seric 
18541caa9a59Seric 		mci_flush(FALSE, NULL);
18551caa9a59Seric 
1856276723a8Seric 		break;
1857276723a8Seric 	}
1858276723a8Seric 
1859276723a8Seric 	/*
18600c52a0b3Seric 	**  Run through the list and send everything.
18610c52a0b3Seric 	*/
18620c52a0b3Seric 
1863655518ecSeric 	e->e_nsent = 0;
18640c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1865ea4dc939Seric 	{
1866276723a8Seric 		if (mode == SM_VERIFY)
1867ea4dc939Seric 		{
1868a6fce3d8Seric 			e->e_to = q->q_paddr;
1869e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
187008b25121Seric 				message("deliverable");
1871ea4dc939Seric 		}
1872ee4b0922Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1873dde5acadSeric 		{
1874de1179f5Seric # ifdef QUEUE
1875dde5acadSeric 			/*
1876dde5acadSeric 			**  Checkpoint the send list every few addresses
1877dde5acadSeric 			*/
1878dde5acadSeric 
1879655518ecSeric 			if (e->e_nsent >= CheckpointInterval)
1880dde5acadSeric 			{
1881e020b127Seric 				queueup(e, TRUE, FALSE);
1882655518ecSeric 				e->e_nsent = 0;
1883dde5acadSeric 			}
1884de1179f5Seric # endif /* QUEUE */
1885655518ecSeric 			(void) deliver(e, q);
1886dde5acadSeric 		}
1887ea4dc939Seric 	}
188814a8ed7aSeric 	Verbose = oldverbose;
18890c52a0b3Seric 
18900c52a0b3Seric 	/*
18910c52a0b3Seric 	**  Now run through and check for errors.
18920c52a0b3Seric 	*/
18930c52a0b3Seric 
1894e020b127Seric 	if (mode == SM_VERIFY)
1895ee4b0922Seric 	{
18960c52a0b3Seric 		return;
1897ee4b0922Seric 	}
18980c52a0b3Seric 
18990c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
19000c52a0b3Seric 	{
1901df864a8fSeric 		if (tTd(13, 3))
1902df864a8fSeric 		{
1903df864a8fSeric 			printf("Checking ");
1904df864a8fSeric 			printaddr(q, FALSE);
1905df864a8fSeric 		}
1906df864a8fSeric 
1907b254bcb6Seric 		/* only send errors if the message failed */
1908b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1909b254bcb6Seric 			continue;
19100c52a0b3Seric 
1911ee4b0922Seric 		e->e_flags |= EF_FATALERRS;
1912ee4b0922Seric 
1913b8043ddbSeric 		if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0)
19149836bea2Seric 			(void) sendtolist(e->e_from.q_paddr, NULL,
19151caa9a59Seric 					  &e->e_errorqueue, e);
19160c52a0b3Seric 	}
1917276723a8Seric 
1918276723a8Seric 	if (mode == SM_FORK)
1919276723a8Seric 		finis();
19200c52a0b3Seric }
1921e103b48fSeric /*
1922e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1923e103b48fSeric **
1924e103b48fSeric **	The signature describes how we are going to send this -- it
1925e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1926e103b48fSeric **	an ordered list of MX hosts.
1927e103b48fSeric **
1928e103b48fSeric **	Parameters:
1929e103b48fSeric **		m -- the mailer describing this host.
1930e103b48fSeric **		host -- the host name.
1931e103b48fSeric **		e -- the current envelope.
1932e103b48fSeric **
1933e103b48fSeric **	Returns:
1934e103b48fSeric **		The signature for this host.
1935e103b48fSeric **
1936e103b48fSeric **	Side Effects:
1937e103b48fSeric **		Can tweak the symbol table.
1938e103b48fSeric */
1939e103b48fSeric 
1940e103b48fSeric char *
1941e103b48fSeric hostsignature(m, host, e)
1942e103b48fSeric 	register MAILER *m;
1943e103b48fSeric 	char *host;
1944e103b48fSeric 	ENVELOPE *e;
1945e103b48fSeric {
1946e103b48fSeric 	register char *p;
1947e103b48fSeric 	register STAB *s;
1948e103b48fSeric 	int i;
1949e103b48fSeric 	int len;
1950e103b48fSeric #ifdef NAMED_BIND
1951e103b48fSeric 	int nmx;
1952e103b48fSeric 	auto int rcode;
1953e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
1954e103b48fSeric 	static char myhostbuf[MAXNAME];
1955e103b48fSeric #endif
1956e103b48fSeric 
1957e103b48fSeric 	/*
1958e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
1959e103b48fSeric 	*/
1960e103b48fSeric 
1961e103b48fSeric 	p = m->m_mailer;
1962e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
1963e103b48fSeric 	{
1964e103b48fSeric 		/* just an ordinary mailer */
1965e103b48fSeric 		return host;
1966e103b48fSeric 	}
1967e103b48fSeric 
1968e103b48fSeric 	/*
1969e103b48fSeric 	**  If it is a numeric address, just return it.
1970e103b48fSeric 	*/
1971e103b48fSeric 
1972e103b48fSeric 	if (host[0] == '[')
1973e103b48fSeric 		return host;
1974e103b48fSeric 
1975e103b48fSeric 	/*
1976e103b48fSeric 	**  Look it up in the symbol table.
1977e103b48fSeric 	*/
1978e103b48fSeric 
1979e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
1980e103b48fSeric 	if (s->s_hostsig != NULL)
1981e103b48fSeric 		return s->s_hostsig;
1982e103b48fSeric 
1983e103b48fSeric 	/*
1984e103b48fSeric 	**  Not already there -- create a signature.
1985e103b48fSeric 	*/
1986e103b48fSeric 
1987e103b48fSeric #ifdef NAMED_BIND
1988e103b48fSeric 	if (myhostbuf[0] == '\0')
19892bc47524Seric 		expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e);
1990e103b48fSeric 
1991e103b48fSeric 	nmx = getmxrr(host, mxhosts, myhostbuf, &rcode);
19927d55540cSeric 
1993e103b48fSeric 	if (nmx <= 0)
1994e103b48fSeric 	{
1995e103b48fSeric 		register MCI *mci;
1996e103b48fSeric 		extern int errno;
1997e103b48fSeric 		extern MCI *mci_get();
1998e103b48fSeric 
1999e103b48fSeric 		/* update the connection info for this host */
2000e103b48fSeric 		mci = mci_get(host, m);
2001e103b48fSeric 		mci->mci_exitstat = rcode;
2002e103b48fSeric 		mci->mci_errno = errno;
2003e103b48fSeric 
2004e103b48fSeric 		/* and return the original host name as the signature */
2005e103b48fSeric 		s->s_hostsig = host;
2006e103b48fSeric 		return host;
2007e103b48fSeric 	}
2008e103b48fSeric 
2009e103b48fSeric 	len = 0;
2010e103b48fSeric 	for (i = 0; i < nmx; i++)
2011e103b48fSeric 	{
2012e103b48fSeric 		len += strlen(mxhosts[i]) + 1;
2013e103b48fSeric 	}
2014e103b48fSeric 	s->s_hostsig = p = xalloc(len);
2015e103b48fSeric 	for (i = 0; i < nmx; i++)
2016e103b48fSeric 	{
2017e103b48fSeric 		if (i != 0)
2018e103b48fSeric 			*p++ = ':';
2019e103b48fSeric 		strcpy(p, mxhosts[i]);
2020e103b48fSeric 		p += strlen(p);
2021e103b48fSeric 	}
2022e103b48fSeric 	makelower(s->s_hostsig);
2023e103b48fSeric #else
2024e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2025e103b48fSeric 	s->s_hostsig = host;
2026e103b48fSeric #endif
2027e103b48fSeric 	if (tTd(17, 1))
2028e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2029e103b48fSeric 	return s->s_hostsig;
2030e103b48fSeric }
2031