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*fabb3bd4Seric static char sccsid[] = "@(#)deliver.c	6.5 (Berkeley) 01/10/93";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14911693bfSbostic #include <sys/signal.h>
15c77d1c25Seric #include <sys/stat.h>
16f28da541Smiriam #include <netdb.h>
17fccb3f7aSbostic #include <fcntl.h>
18911693bfSbostic #include <errno.h>
19134746fbSeric #ifdef NAMED_BIND
20912a731aSbostic #include <arpa/nameser.h>
21912a731aSbostic #include <resolv.h>
22134746fbSeric #endif
2325a99e2eSeric 
2425a99e2eSeric /*
2513bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
2613bbc08cSeric **
2713bbc08cSeric **	This routine delivers to everyone on the same host as the
2813bbc08cSeric **	user on the head of the list.  It is clever about mailers
2913bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
3013bbc08cSeric **	that it will deliver to all these addresses however -- so
3113bbc08cSeric **	deliver should be called once for each address on the
3213bbc08cSeric **	list.
3325a99e2eSeric **
3425a99e2eSeric **	Parameters:
35588cad61Seric **		e -- the envelope to deliver.
36c77d1c25Seric **		firstto -- head of the address list to deliver to.
3725a99e2eSeric **
3825a99e2eSeric **	Returns:
3925a99e2eSeric **		zero -- successfully delivered.
4025a99e2eSeric **		else -- some failure, see ExitStat for more info.
4125a99e2eSeric **
4225a99e2eSeric **	Side Effects:
4325a99e2eSeric **		The standard input is passed off to someone.
4425a99e2eSeric */
4525a99e2eSeric 
46588cad61Seric deliver(e, firstto)
47588cad61Seric 	register ENVELOPE *e;
48c77d1c25Seric 	ADDRESS *firstto;
4925a99e2eSeric {
5078442df3Seric 	char *host;			/* host being sent to */
5178442df3Seric 	char *user;			/* user being sent to */
5225a99e2eSeric 	char **pvp;
535dfc646bSeric 	register char **mvp;
5425a99e2eSeric 	register char *p;
55588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
566259796dSeric 	ADDRESS *ctladdr;
57b31e7f2bSeric 	register MCI *mci;
58c77d1c25Seric 	register ADDRESS *to = firstto;
59c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
60772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
61911693bfSbostic 	int rcode;			/* response code */
62c23ed322Seric 	char *from;			/* pointer to from person */
63ee6bf8dfSeric 	char *pv[MAXPV+1];
64ee6bf8dfSeric 	char tobuf[MAXLINE-50];		/* text line of to people */
65ee6bf8dfSeric 	char buf[MAXNAME];
66ee6bf8dfSeric 	char tfrombuf[MAXNAME];		/* translated from person */
67c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
68*fabb3bd4Seric 	extern int checkcompat();
69ee6bf8dfSeric 	extern ADDRESS *getctladdr();
70ee6bf8dfSeric 	extern char *remotename();
71b31e7f2bSeric 	extern MCI *openmailer();
7225a99e2eSeric 
7335490626Seric 	errno = 0;
74da2935e1Seric 	if (bitset(QDONTSEND, to->q_flags))
755dfc646bSeric 		return (0);
7625a99e2eSeric 
77134746fbSeric #ifdef NAMED_BIND
78912a731aSbostic 	/* unless interactive, try twice, over a minute */
79912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
80912a731aSbostic 		_res.retrans = 30;
81912a731aSbostic 		_res.retry = 2;
82912a731aSbostic 	}
83d4bd8f0eSbostic #endif
84912a731aSbostic 
8551552439Seric 	m = to->q_mailer;
8651552439Seric 	host = to->q_host;
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 
10257fc6f17Seric 	if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) &&
103317680d6Seric 	    !Verbose)
104f3dbc832Seric 	{
105f3dbc832Seric 		for (; to != NULL; to = to->q_next)
106f4560e80Seric 		{
107f4560e80Seric 			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
108f4560e80Seric 				continue;
109f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
110588cad61Seric 			e->e_to = to->q_paddr;
111eb238f8cSeric 			message(Arpa_Info, "queued");
112eb238f8cSeric 			if (LogLevel > 4)
113b31e7f2bSeric 				logdelivery("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 */
131b843d759Seric 	(void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, TRUE, e));
132c23ed322Seric 	if (e->e_returnpath == e->e_sender)
133c23ed322Seric 	{
134c23ed322Seric 		from = rpathbuf;
135c23ed322Seric 	}
136c23ed322Seric 	else
137c23ed322Seric 	{
138b843d759Seric 		(void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, TRUE, e));
139c23ed322Seric 		from = tfrombuf;
140c23ed322Seric 	}
14178442df3Seric 
142c23ed322Seric 	define('f', e->e_returnpath, e);	/* raw return path */
143c23ed322Seric 	define('<', rpathbuf, e);		/* translated return path */
144c23ed322Seric 	define('g', from, e);			/* translated sender */
145588cad61Seric 	define('h', host, e);			/* to host */
1465dfc646bSeric 	Errors = 0;
1475dfc646bSeric 	pvp = pv;
1485dfc646bSeric 	*pvp++ = m->m_argv[0];
1495dfc646bSeric 
1505dfc646bSeric 	/* insert -f or -r flag as appropriate */
15157fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
1525dfc646bSeric 	{
15357fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
1545dfc646bSeric 			*pvp++ = "-f";
1555dfc646bSeric 		else
1565dfc646bSeric 			*pvp++ = "-r";
157c23ed322Seric 		*pvp++ = newstr(rpathbuf);
1585dfc646bSeric 	}
1595dfc646bSeric 
1605dfc646bSeric 	/*
1615dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1625dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1635dfc646bSeric 	**  be one of these, and there are only a few more slots
1645dfc646bSeric 	**  in the pv after it.
1655dfc646bSeric 	*/
1665dfc646bSeric 
1675dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1685dfc646bSeric 	{
1696c2c3107Seric 		while ((p = strchr(p, '\001')) != NULL)
1705dfc646bSeric 			if (*++p == 'u')
1715dfc646bSeric 				break;
1725dfc646bSeric 		if (p != NULL)
1735dfc646bSeric 			break;
1745dfc646bSeric 
1755dfc646bSeric 		/* this entry is safe -- go ahead and process it */
176588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1775dfc646bSeric 		*pvp++ = newstr(buf);
1785dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1795dfc646bSeric 		{
1805dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1815dfc646bSeric 			return (-1);
1825dfc646bSeric 		}
1835dfc646bSeric 	}
184c579ef51Seric 
18533db8731Seric 	/*
18633db8731Seric 	**  If we have no substitution for the user name in the argument
18733db8731Seric 	**  list, we know that we must supply the names otherwise -- and
18833db8731Seric 	**  SMTP is the answer!!
18933db8731Seric 	*/
19033db8731Seric 
1915dfc646bSeric 	if (*mvp == NULL)
192c579ef51Seric 	{
193c579ef51Seric 		/* running SMTP */
1942c7e1b8dSeric # ifdef SMTP
195c579ef51Seric 		clever = TRUE;
196c579ef51Seric 		*pvp = NULL;
1976c2c3107Seric # else /* SMTP */
19833db8731Seric 		/* oops!  we don't implement SMTP */
1992c7e1b8dSeric 		syserr("SMTP style mailer");
2002c7e1b8dSeric 		return (EX_SOFTWARE);
2016c2c3107Seric # endif /* SMTP */
202c579ef51Seric 	}
2035dfc646bSeric 
2045dfc646bSeric 	/*
2055dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
2065dfc646bSeric 	**  run through our address list and append all the addresses
2075dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
2085dfc646bSeric 	**  always send another copy later.
2095dfc646bSeric 	*/
2105dfc646bSeric 
2115dfc646bSeric 	tobuf[0] = '\0';
212588cad61Seric 	e->e_to = tobuf;
2136259796dSeric 	ctladdr = NULL;
2145dfc646bSeric 	for (; to != NULL; to = to->q_next)
2155dfc646bSeric 	{
2165dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
21757fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
2185dfc646bSeric 			break;
2195dfc646bSeric 
2205dfc646bSeric 		/* if already sent or not for this host, don't send */
221da2935e1Seric 		if (bitset(QDONTSEND, to->q_flags) ||
222da2935e1Seric 		    strcmp(to->q_host, host) != 0 ||
223da2935e1Seric 		    to->q_mailer != firstto->q_mailer)
2245dfc646bSeric 			continue;
2256259796dSeric 
2264b22ea87Seric 		/* avoid overflowing tobuf */
227aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
2284b22ea87Seric 			break;
2294b22ea87Seric 
2306ef48975Seric 		if (tTd(10, 1))
231772e6e50Seric 		{
232772e6e50Seric 			printf("\nsend to ");
233772e6e50Seric 			printaddr(to, FALSE);
234772e6e50Seric 		}
235772e6e50Seric 
2366259796dSeric 		/* compute effective uid/gid when sending */
2377da1035fSeric 		if (to->q_mailer == ProgMailer)
2386259796dSeric 			ctladdr = getctladdr(to);
2396259796dSeric 
2405dfc646bSeric 		user = to->q_user;
241588cad61Seric 		e->e_to = to->q_paddr;
2425dfc646bSeric 		to->q_flags |= QDONTSEND;
2435dfc646bSeric 
2445dfc646bSeric 		/*
2455dfc646bSeric 		**  Check to see that these people are allowed to
2465dfc646bSeric 		**  talk to each other.
2472a6e0786Seric 		*/
2482a6e0786Seric 
24969582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
25069582d2fSeric 		{
25169582d2fSeric 			NoReturn = TRUE;
252672bec4aSeric 			usrerr("Message is too large; %ld bytes max", m->m_maxsize);
25369582d2fSeric 			giveresponse(EX_UNAVAILABLE, m, e);
25469582d2fSeric 			continue;
25569582d2fSeric 		}
256*fabb3bd4Seric 		rcode = checkcompat(to, e);
257*fabb3bd4Seric 		if (r <= 0)
2585dfc646bSeric 		{
259*fabb3bd4Seric 			giveresponse(rcode == 0 ? EX_UNAVAILABLE : EX_TEMPFAIL,
260*fabb3bd4Seric 				     m, e);
2615dfc646bSeric 			continue;
2625dfc646bSeric 		}
2632a6e0786Seric 
2642a6e0786Seric 		/*
2659ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2669ec9501bSeric 		**	about them.
26725a99e2eSeric 		*/
26825a99e2eSeric 
26957fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
27025a99e2eSeric 		{
2711d8f1806Seric 			stripquotes(user);
2721d8f1806Seric 			stripquotes(host);
27325a99e2eSeric 		}
27425a99e2eSeric 
275cdb828c5Seric 		/* hack attack -- delivermail compatibility */
276cdb828c5Seric 		if (m == ProgMailer && *user == '|')
277cdb828c5Seric 			user++;
278cdb828c5Seric 
27925a99e2eSeric 		/*
2803efaed6eSeric 		**  If an error message has already been given, don't
2813efaed6eSeric 		**	bother to send to this address.
2823efaed6eSeric 		**
2833efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2843efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2853efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2863efaed6eSeric 		*/
2873efaed6eSeric 
2886cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2893efaed6eSeric 			continue;
2903efaed6eSeric 
291f2fec898Seric 		/* save statistics.... */
292588cad61Seric 		markstats(e, to);
293f2fec898Seric 
2943efaed6eSeric 		/*
29525a99e2eSeric 		**  See if this user name is "special".
29625a99e2eSeric 		**	If the user name has a slash in it, assume that this
29751552439Seric 		**	is a file -- send it off without further ado.  Note
29851552439Seric 		**	that this type of addresses is not processed along
29951552439Seric 		**	with the others, so we fudge on the To person.
30025a99e2eSeric 		*/
30125a99e2eSeric 
3022c017f8dSeric 		if (m == FileMailer)
30325a99e2eSeric 		{
304b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
305198d9be0Seric 			giveresponse(rcode, m, e);
306dde5acadSeric 			if (rcode == EX_OK)
307dde5acadSeric 				to->q_flags |= QSENT;
3085dfc646bSeric 			continue;
30925a99e2eSeric 		}
31025a99e2eSeric 
31113bbc08cSeric 		/*
31213bbc08cSeric 		**  Address is verified -- add this user to mailer
31313bbc08cSeric 		**  argv, and add it to the print list of recipients.
31413bbc08cSeric 		*/
31513bbc08cSeric 
316508daeccSeric 		/* link together the chain of recipients */
317508daeccSeric 		to->q_tchain = tochain;
318508daeccSeric 		tochain = to;
319508daeccSeric 
3205dfc646bSeric 		/* create list of users for error messages */
321db8841e9Seric 		(void) strcat(tobuf, ",");
322db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
323588cad61Seric 		define('u', user, e);		/* to user */
324588cad61Seric 		define('z', to->q_home, e);	/* user's home */
3255dfc646bSeric 
326c579ef51Seric 		/*
327508daeccSeric 		**  Expand out this user into argument list.
328c579ef51Seric 		*/
329c579ef51Seric 
330508daeccSeric 		if (!clever)
331c579ef51Seric 		{
332588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3335dfc646bSeric 			*pvp++ = newstr(buf);
3345dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3355dfc646bSeric 			{
3365dfc646bSeric 				/* allow some space for trailing parms */
3375dfc646bSeric 				break;
3385dfc646bSeric 			}
3395dfc646bSeric 		}
340c579ef51Seric 	}
3415dfc646bSeric 
342145b49b1Seric 	/* see if any addresses still exist */
343145b49b1Seric 	if (tobuf[0] == '\0')
344c579ef51Seric 	{
345588cad61Seric 		define('g', (char *) NULL, e);
346c23ed322Seric 		define('<', (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])
3625dfc646bSeric 			syserr("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 
37486b26461Seric 	if (ctladdr == NULL)
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 */
384b31e7f2bSeric 		rcode = -1;
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 */
395b31e7f2bSeric 			rcode = EX_SOFTWARE;
396b31e7f2bSeric 		}
397b31e7f2bSeric 	}
398b31e7f2bSeric 	else if (!clever)
399b31e7f2bSeric 	{
400b31e7f2bSeric 		/*
401b31e7f2bSeric 		**  Format and send message.
402b31e7f2bSeric 		*/
40315d084d5Seric 
404b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
405b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
406b31e7f2bSeric 		putline("\n", mci->mci_out, m);
407b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
408b31e7f2bSeric 
409b31e7f2bSeric 		/* get the exit status */
410b31e7f2bSeric 		rcode = endmailer(mci, pv[0]);
411134746fbSeric 	}
412134746fbSeric 	else
413b31e7f2bSeric #ifdef SMTP
414134746fbSeric 	{
415b31e7f2bSeric 		/*
416b31e7f2bSeric 		**  Send the MAIL FROM: protocol
417b31e7f2bSeric 		*/
41815d084d5Seric 
419b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
420b31e7f2bSeric 		if (rcode == EX_OK)
42175889e88Seric 		{
422ded0d3daSkarels 			register char *t = tobuf;
423ded0d3daSkarels 			register int i;
424ded0d3daSkarels 
425588cad61Seric 			/* send the recipient list */
42663780dbdSeric 			tobuf[0] = '\0';
42775889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
42875889e88Seric 			{
42963780dbdSeric 				e->e_to = to->q_paddr;
43015d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
43175889e88Seric 				{
43283b7ddc9Seric 					markfailure(e, to, i);
433198d9be0Seric 					giveresponse(i, m, e);
43463780dbdSeric 				}
43575889e88Seric 				else
43675889e88Seric 				{
437911693bfSbostic 					*t++ = ',';
438b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
439b31e7f2bSeric 						continue;
440588cad61Seric 				}
441588cad61Seric 			}
442588cad61Seric 
44363780dbdSeric 			/* now send the data */
44463780dbdSeric 			if (tobuf[0] == '\0')
445b31e7f2bSeric 			{
44663780dbdSeric 				e->e_to = NULL;
447b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
448b31e7f2bSeric 					smtprset(m, mci, e);
449b31e7f2bSeric 			}
45075889e88Seric 			else
45175889e88Seric 			{
45263780dbdSeric 				e->e_to = tobuf + 1;
45375889e88Seric 				rcode = smtpdata(m, mci, e);
45463780dbdSeric 			}
45563780dbdSeric 
45663780dbdSeric 			/* now close the connection */
457b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
45815d084d5Seric 				smtpquit(m, mci, e);
45963780dbdSeric 		}
460c579ef51Seric 	}
461b31e7f2bSeric #else /* not SMTP */
462a05b3449Sbostic 	{
463b31e7f2bSeric 		syserr("deliver: need SMTP compiled to use clever mailer");
464b31e7f2bSeric 		rcode = -1;
465b31e7f2bSeric 		goto give_up;
466a05b3449Sbostic 	}
467b31e7f2bSeric #endif /* SMTP */
468134746fbSeric #ifdef NAMED_BIND
4692bcc6d2dSeric 	if (ConfigLevel < 2)
470912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
471134746fbSeric #endif
4725dfc646bSeric 
473b31e7f2bSeric 	/* arrange a return receipt if requested */
474b31e7f2bSeric 	if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags))
475b31e7f2bSeric 	{
476b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
477b31e7f2bSeric 		/* do we want to send back more info? */
478b31e7f2bSeric 	}
479b31e7f2bSeric 
480c77d1c25Seric 	/*
48163780dbdSeric 	**  Do final status disposal.
48263780dbdSeric 	**	We check for something in tobuf for the SMTP case.
483c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
484c77d1c25Seric 	**		addressees.
485c77d1c25Seric 	*/
486c77d1c25Seric 
487b31e7f2bSeric   give_up:
48863780dbdSeric 	if (tobuf[0] != '\0')
489198d9be0Seric 		giveresponse(rcode, m, e);
490772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
491b31e7f2bSeric 	{
492dde5acadSeric 		if (rcode != EX_OK)
49383b7ddc9Seric 			markfailure(e, to, rcode);
494dde5acadSeric 		else
495dde5acadSeric 			to->q_flags |= QSENT;
496b31e7f2bSeric 	}
497b31e7f2bSeric 
498b31e7f2bSeric 	/*
499b31e7f2bSeric 	**  Restore state and return.
500b31e7f2bSeric 	*/
501c77d1c25Seric 
50235490626Seric 	errno = 0;
503588cad61Seric 	define('g', (char *) NULL, e);
504c23ed322Seric 	define('<', (char *) NULL, e);
5055826d9d3Seric 	return (rcode);
50625a99e2eSeric }
5075dfc646bSeric /*
50883b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
50983b7ddc9Seric **
51083b7ddc9Seric **	Parameters:
51183b7ddc9Seric **		e -- the envelope we are sending.
51283b7ddc9Seric **		q -- the address to mark.
51383b7ddc9Seric **		rcode -- the code signifying the particular failure.
51483b7ddc9Seric **
51583b7ddc9Seric **	Returns:
51683b7ddc9Seric **		none.
51783b7ddc9Seric **
51883b7ddc9Seric **	Side Effects:
51983b7ddc9Seric **		marks the address (and possibly the envelope) with the
52083b7ddc9Seric **			failure so that an error will be returned or
52183b7ddc9Seric **			the message will be queued, as appropriate.
52283b7ddc9Seric */
52383b7ddc9Seric 
52483b7ddc9Seric markfailure(e, q, rcode)
52583b7ddc9Seric 	register ENVELOPE *e;
52683b7ddc9Seric 	register ADDRESS *q;
52783b7ddc9Seric 	int rcode;
52883b7ddc9Seric {
52983b7ddc9Seric 	if (rcode == EX_OK)
53083b7ddc9Seric 		return;
531ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
53283b7ddc9Seric 		q->q_flags |= QBADADDR;
53383b7ddc9Seric 	else if (curtime() > e->e_ctime + TimeOut)
53483b7ddc9Seric 	{
53583b7ddc9Seric 		extern char *pintvl();
536198d9be0Seric 		char buf[MAXLINE];
53783b7ddc9Seric 
53883b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
539198d9be0Seric 		{
540198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
54183b7ddc9Seric 				pintvl(TimeOut, FALSE));
542198d9be0Seric 			if (e->e_message != NULL)
543198d9be0Seric 				free(e->e_message);
544198d9be0Seric 			e->e_message = newstr(buf);
545198d9be0Seric 			message(Arpa_Info, buf);
546198d9be0Seric 		}
54783b7ddc9Seric 		q->q_flags |= QBADADDR;
54883b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
54983b7ddc9Seric 	}
55083b7ddc9Seric 	else
55183b7ddc9Seric 		q->q_flags |= QQUEUEUP;
55283b7ddc9Seric }
55383b7ddc9Seric /*
55432d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
55532d19d43Seric **
55632d19d43Seric **	This MUST be a macro, since after a vfork we are running
55732d19d43Seric **	two processes on the same stack!!!
55832d19d43Seric **
55932d19d43Seric **	Parameters:
56032d19d43Seric **		none.
56132d19d43Seric **
56232d19d43Seric **	Returns:
56332d19d43Seric **		From a macro???  You've got to be kidding!
56432d19d43Seric **
56532d19d43Seric **	Side Effects:
56632d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
56732d19d43Seric **			pid of child in parent, zero in child.
56832d19d43Seric **			-1 on unrecoverable error.
56932d19d43Seric **
57032d19d43Seric **	Notes:
57132d19d43Seric **		I'm awfully sorry this looks so awful.  That's
57232d19d43Seric **		vfork for you.....
57332d19d43Seric */
57432d19d43Seric 
57532d19d43Seric # define NFORKTRIES	5
57684f7cd1bSeric 
57784f7cd1bSeric # ifndef FORK
57884f7cd1bSeric # define FORK	fork
57984f7cd1bSeric # endif
58032d19d43Seric 
58132d19d43Seric # define DOFORK(fORKfN) \
58232d19d43Seric {\
58332d19d43Seric 	register int i;\
58432d19d43Seric \
58511799049Seric 	for (i = NFORKTRIES; --i >= 0; )\
58632d19d43Seric 	{\
58732d19d43Seric 		pid = fORKfN();\
58832d19d43Seric 		if (pid >= 0)\
58932d19d43Seric 			break;\
59011799049Seric 		if (i > 0)\
5916c4635f6Seric 			sleep((unsigned) NFORKTRIES - i);\
59232d19d43Seric 	}\
59332d19d43Seric }
59432d19d43Seric /*
5952ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
5962ed72599Seric **
5972ed72599Seric **	Parameters:
5982ed72599Seric **		none.
5992ed72599Seric **
6002ed72599Seric **	Returns:
6012ed72599Seric **		pid of child in parent.
6022ed72599Seric **		zero in child.
6032ed72599Seric **		-1 on error.
6042ed72599Seric **
6052ed72599Seric **	Side Effects:
6062ed72599Seric **		returns twice, once in parent and once in child.
6072ed72599Seric */
6082ed72599Seric 
6092ed72599Seric dofork()
6102ed72599Seric {
6112ed72599Seric 	register int pid;
6122ed72599Seric 
6132ed72599Seric 	DOFORK(fork);
6142ed72599Seric 	return (pid);
6152ed72599Seric }
6162ed72599Seric /*
617c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
618c579ef51Seric **
619c579ef51Seric **	We should never get fatal errors (e.g., segmentation
620c579ef51Seric **	violation), so we report those specially.  For other
621c579ef51Seric **	errors, we choose a status message (into statmsg),
622c579ef51Seric **	and if it represents an error, we print it.
623c579ef51Seric **
624c579ef51Seric **	Parameters:
625c579ef51Seric **		pid -- pid of mailer.
626c579ef51Seric **		name -- name of mailer (for error messages).
627c579ef51Seric **
628c579ef51Seric **	Returns:
629c579ef51Seric **		exit code of mailer.
630c579ef51Seric **
631c579ef51Seric **	Side Effects:
632c579ef51Seric **		none.
633c579ef51Seric */
634c579ef51Seric 
63575889e88Seric endmailer(mci, name)
636b31e7f2bSeric 	register MCI *mci;
637c579ef51Seric 	char *name;
638c579ef51Seric {
639588cad61Seric 	int st;
640c579ef51Seric 
64175889e88Seric 	/* close any connections */
64275889e88Seric 	if (mci->mci_in != NULL)
64375889e88Seric 		(void) fclose(mci->mci_in);
64475889e88Seric 	if (mci->mci_out != NULL)
64575889e88Seric 		(void) fclose(mci->mci_out);
64675889e88Seric 	mci->mci_in = mci->mci_out = NULL;
64775889e88Seric 	mci->mci_state = MCIS_CLOSED;
64875889e88Seric 
64933db8731Seric 	/* in the IPC case there is nothing to wait for */
65075889e88Seric 	if (mci->mci_pid == 0)
65133db8731Seric 		return (EX_OK);
65233db8731Seric 
65333db8731Seric 	/* wait for the mailer process to die and collect status */
65475889e88Seric 	st = waitfor(mci->mci_pid);
655588cad61Seric 	if (st == -1)
65678de67c1Seric 	{
657588cad61Seric 		syserr("endmailer %s: wait", name);
658588cad61Seric 		return (EX_SOFTWARE);
659c579ef51Seric 	}
66033db8731Seric 
66133db8731Seric 	/* see if it died a horrid death */
662c579ef51Seric 	if ((st & 0377) != 0)
663c579ef51Seric 	{
6645f73204aSeric 		syserr("mailer %s died with signal %o", name, st);
6655f73204aSeric 		ExitStat = EX_TEMPFAIL;
6665f73204aSeric 		return (EX_TEMPFAIL);
667c579ef51Seric 	}
66833db8731Seric 
66933db8731Seric 	/* normal death -- return status */
670588cad61Seric 	st = (st >> 8) & 0377;
671588cad61Seric 	return (st);
672c579ef51Seric }
673c579ef51Seric /*
674c579ef51Seric **  OPENMAILER -- open connection to mailer.
675c579ef51Seric **
676c579ef51Seric **	Parameters:
677c579ef51Seric **		m -- mailer descriptor.
678c579ef51Seric **		pvp -- parameter vector to pass to mailer.
679c579ef51Seric **		ctladdr -- controlling address for user.
680c579ef51Seric **		clever -- create a full duplex connection.
681c579ef51Seric **
682c579ef51Seric **	Returns:
68375889e88Seric **		The mail connection info struct for this connection.
68475889e88Seric **		NULL on failure.
685c579ef51Seric **
686c579ef51Seric **	Side Effects:
687c579ef51Seric **		creates a mailer in a subprocess.
688c579ef51Seric */
689c579ef51Seric 
690b31e7f2bSeric MCI *
691b31e7f2bSeric openmailer(m, pvp, ctladdr, clever, e)
692588cad61Seric 	MAILER *m;
693c579ef51Seric 	char **pvp;
694c579ef51Seric 	ADDRESS *ctladdr;
695c579ef51Seric 	bool clever;
696b31e7f2bSeric 	ENVELOPE *e;
697c579ef51Seric {
6985dfc646bSeric 	int pid;
699b31e7f2bSeric 	register MCI *mci;
700f8952a83Seric 	int mpvect[2];
701c579ef51Seric 	int rpvect[2];
7025dfc646bSeric 	extern FILE *fdopen();
7035dfc646bSeric 
7046ef48975Seric 	if (tTd(11, 1))
7055dfc646bSeric 	{
7068c57e552Seric 		printf("openmailer:");
7075dfc646bSeric 		printav(pvp);
7085dfc646bSeric 	}
70935490626Seric 	errno = 0;
7105dfc646bSeric 
711ef66a9d0Seric 	CurHostName = m->m_mailer;
712ef66a9d0Seric 
71333db8731Seric 	/*
71433db8731Seric 	**  Deal with the special case of mail handled through an IPC
71533db8731Seric 	**  connection.
71633db8731Seric 	**	In this case we don't actually fork.  We must be
71733db8731Seric 	**	running SMTP for this to work.  We will return a
71833db8731Seric 	**	zero pid to indicate that we are running IPC.
719e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
72033db8731Seric 	*/
72133db8731Seric 
722e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
723e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
724e7c1bd78Seric 	{
725b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
7262a087e90Seric 		bzero((char *) mci, sizeof *mci);
72775889e88Seric 		mci->mci_in = stdin;
72875889e88Seric 		mci->mci_out = stdout;
729b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
73015d084d5Seric 		mci->mci_mailer = m;
731e7c1bd78Seric 	}
732b31e7f2bSeric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
733914346b1Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
73433db8731Seric 	{
73584f7cd1bSeric #ifdef DAEMON
736ebc61751Sbloom 		register int i, j;
7371277f9a8Seric 		register u_short port;
738b31e7f2bSeric 		int nmx;
739b31e7f2bSeric 		char *mxhosts[MAXMXHOSTS + 1];
740b31e7f2bSeric 		extern MCI *mci_get();
74133db8731Seric 
742ef66a9d0Seric 		CurHostName = pvp[1];
743b31e7f2bSeric #ifdef NAMED_BIND
744b31e7f2bSeric 		if (CurHostName != NULL && CurHostName[0] != '\0' &&
745b31e7f2bSeric 		    CurHostName[0] != '[')
746b31e7f2bSeric 		{
747b31e7f2bSeric 			int rcode;
748b31e7f2bSeric 			char buf[MAXNAME];
749b31e7f2bSeric 
750b31e7f2bSeric 			expand("\001j", buf, &buf[sizeof(buf) - 1], e);
751b31e7f2bSeric 			nmx = getmxrr(CurHostName, mxhosts, buf, &rcode);
752b31e7f2bSeric 			if (nmx < 0)
753b31e7f2bSeric 			{
754b31e7f2bSeric 				mci = mci_get(CurHostName, m);
755b31e7f2bSeric 				mci->mci_exitstat = rcode;
756b31e7f2bSeric 				mci->mci_errno = errno;
757b31e7f2bSeric 			}
758b31e7f2bSeric 		}
759b31e7f2bSeric 		else
760b31e7f2bSeric #endif
761b31e7f2bSeric 		{
762b31e7f2bSeric 			nmx = 1;
763b31e7f2bSeric 			mxhosts[0] = CurHostName;
764b31e7f2bSeric 		}
765b31e7f2bSeric 
76633db8731Seric 		if (!clever)
76733db8731Seric 			syserr("non-clever IPC");
76893b6e3cfSeric 		if (pvp[2] != NULL)
7691277f9a8Seric 			port = atoi(pvp[2]);
77093b6e3cfSeric 		else
7711277f9a8Seric 			port = 0;
772b31e7f2bSeric 		for (j = 0; j < nmx; j++)
773ebc61751Sbloom 		{
77445a8316eSeric 			/* see if we already know that this host is fried */
775b31e7f2bSeric 			CurHostName = mxhosts[j];
776b31e7f2bSeric 			mci = mci_get(CurHostName, m);
777b31e7f2bSeric 			if (mci->mci_state != MCIS_CLOSED)
7781fa7c2ebSeric 			{
7791fa7c2ebSeric 				if (tTd(11, 1))
7801fa7c2ebSeric 				{
7811fa7c2ebSeric 					printf("openmailer: ");
7821fa7c2ebSeric 					mci_dump(mci);
7831fa7c2ebSeric 				}
78475889e88Seric 				return mci;
7851fa7c2ebSeric 			}
78615d084d5Seric 			mci->mci_mailer = m;
787b31e7f2bSeric 			if (mci->mci_exitstat != EX_OK)
788b31e7f2bSeric 				continue;
789b31e7f2bSeric 
79075889e88Seric 			/* try the connection */
791b6073cecSeric 			setproctitle("%s %s: %s", e->e_id, mxhosts[j], "user open");
792914346b1Seric 			message(Arpa_Info, "Connecting to %s (%s)...",
793b31e7f2bSeric 				mxhosts[j], m->m_name);
794b31e7f2bSeric 			i = makeconnection(mxhosts[j], port, mci,
795914346b1Seric 				bitnset(M_SECURE_PORT, m->m_flags));
79675889e88Seric 			mci->mci_exitstat = i;
79775889e88Seric 			mci->mci_errno = errno;
79875889e88Seric 			if (i == EX_OK)
799b31e7f2bSeric 			{
800b31e7f2bSeric 				mci->mci_state = MCIS_OPENING;
801b31e7f2bSeric 				mci_cache(mci);
802b31e7f2bSeric 				break;
803b31e7f2bSeric 			}
804b31e7f2bSeric 			else if (tTd(11, 1))
805b31e7f2bSeric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
806b31e7f2bSeric 					i, errno);
807b31e7f2bSeric 
80875889e88Seric 
8095f73204aSeric 			/* enter status of this host */
81075889e88Seric 			setstat(i);
811ed854c7bSeric 		}
8122a087e90Seric 		mci->mci_pid = 0;
813b31e7f2bSeric #else /* no DAEMON */
814588cad61Seric 		syserr("openmailer: no IPC");
8151fa7c2ebSeric 		if (tTd(11, 1))
8161fa7c2ebSeric 			printf("openmailer: NULL\n");
81775889e88Seric 		return NULL;
818b31e7f2bSeric #endif /* DAEMON */
819588cad61Seric 	}
820b31e7f2bSeric 	else
821b31e7f2bSeric 	{
8226328bdf7Seric 		/* create a pipe to shove the mail through */
823f8952a83Seric 		if (pipe(mpvect) < 0)
82425a99e2eSeric 		{
825588cad61Seric 			syserr("openmailer: pipe (to mailer)");
8261fa7c2ebSeric 			if (tTd(11, 1))
8271fa7c2ebSeric 				printf("openmailer: NULL\n");
82875889e88Seric 			return NULL;
82925a99e2eSeric 		}
830c579ef51Seric 
831c579ef51Seric 		/* if this mailer speaks smtp, create a return pipe */
832c579ef51Seric 		if (clever && pipe(rpvect) < 0)
833c579ef51Seric 		{
834588cad61Seric 			syserr("openmailer: pipe (from mailer)");
835c579ef51Seric 			(void) close(mpvect[0]);
836c579ef51Seric 			(void) close(mpvect[1]);
8371fa7c2ebSeric 			if (tTd(11, 1))
8381fa7c2ebSeric 				printf("openmailer: NULL\n");
83975889e88Seric 			return NULL;
840c579ef51Seric 		}
841c579ef51Seric 
84233db8731Seric 		/*
84333db8731Seric 		**  Actually fork the mailer process.
84433db8731Seric 		**	DOFORK is clever about retrying.
8456984bfddSeric 		**
8466984bfddSeric 		**	Dispose of SIGCHLD signal catchers that may be laying
8476984bfddSeric 		**	around so that endmail will get it.
84833db8731Seric 		*/
84933db8731Seric 
850b31e7f2bSeric 		if (e->e_xfp != NULL)
851b31e7f2bSeric 			(void) fflush(e->e_xfp);		/* for debugging */
852588cad61Seric 		(void) fflush(stdout);
8536984bfddSeric # ifdef SIGCHLD
8546984bfddSeric 		(void) signal(SIGCHLD, SIG_DFL);
8556c2c3107Seric # endif /* SIGCHLD */
85684f7cd1bSeric 		DOFORK(FORK);
857f129ec7dSeric 		/* pid is set by DOFORK */
85825a99e2eSeric 		if (pid < 0)
85925a99e2eSeric 		{
86033db8731Seric 			/* failure */
861588cad61Seric 			syserr("openmailer: cannot fork");
862f8952a83Seric 			(void) close(mpvect[0]);
863f8952a83Seric 			(void) close(mpvect[1]);
864c579ef51Seric 			if (clever)
865c579ef51Seric 			{
866c579ef51Seric 				(void) close(rpvect[0]);
867c579ef51Seric 				(void) close(rpvect[1]);
868c579ef51Seric 			}
8691fa7c2ebSeric 			if (tTd(11, 1))
8701fa7c2ebSeric 				printf("openmailer: NULL\n");
87175889e88Seric 			return NULL;
87225a99e2eSeric 		}
87325a99e2eSeric 		else if (pid == 0)
87425a99e2eSeric 		{
87513088b9fSeric 			int i;
876dafbc479Seric 			int saveerrno;
8775f73204aSeric 			extern int DtableSize;
87813088b9fSeric 
87925a99e2eSeric 			/* child -- set up input & exec mailer */
88003ab8e55Seric 			/* make diagnostic output be standard output */
8818f0e7860Seric 			(void) signal(SIGINT, SIG_IGN);
8828f0e7860Seric 			(void) signal(SIGHUP, SIG_IGN);
8830984da9fSeric 			(void) signal(SIGTERM, SIG_DFL);
884f8952a83Seric 
885b31e7f2bSeric 			/* arrange to filter std & diag output of command */
886c579ef51Seric 			if (clever)
887c579ef51Seric 			{
888c579ef51Seric 				(void) close(rpvect[0]);
889c579ef51Seric 				(void) close(1);
890c579ef51Seric 				(void) dup(rpvect[1]);
891c579ef51Seric 				(void) close(rpvect[1]);
892c579ef51Seric 			}
893276723a8Seric 			else if (OpMode == MD_SMTP || HoldErrs)
894f8952a83Seric 			{
895588cad61Seric 				/* put mailer output in transcript */
896f8952a83Seric 				(void) close(1);
897b31e7f2bSeric 				(void) dup(fileno(e->e_xfp));
898f8952a83Seric 			}
899db8841e9Seric 			(void) close(2);
900db8841e9Seric 			(void) dup(1);
901f8952a83Seric 
902f8952a83Seric 			/* arrange to get standard input */
903f8952a83Seric 			(void) close(mpvect[1]);
904db8841e9Seric 			(void) close(0);
905f8952a83Seric 			if (dup(mpvect[0]) < 0)
90625a99e2eSeric 			{
90725a99e2eSeric 				syserr("Cannot dup to zero!");
908a590b978Seric 				_exit(EX_OSERR);
90925a99e2eSeric 			}
910f8952a83Seric 			(void) close(mpvect[0]);
91157fc6f17Seric 			if (!bitnset(M_RESTR, m->m_flags))
9120984da9fSeric 			{
91353e3fa05Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
914e36b99e2Seric 				{
915e36b99e2Seric 					(void) setgid(DefGid);
916898a126bSbostic 					(void) initgroups(DefUser, DefGid);
917e36b99e2Seric 					(void) setuid(DefUid);
918e36b99e2Seric 				}
919e36b99e2Seric 				else
92069f29479Seric 				{
921e36b99e2Seric 					(void) setgid(ctladdr->q_gid);
922898a126bSbostic 					(void) initgroups(ctladdr->q_ruser?
923898a126bSbostic 						ctladdr->q_ruser: ctladdr->q_user,
924898a126bSbostic 						ctladdr->q_gid);
925e36b99e2Seric 					(void) setuid(ctladdr->q_uid);
92669f29479Seric 				}
9270984da9fSeric 			}
928588cad61Seric 
92913088b9fSeric 			/* arrange for all the files to be closed */
930b31e7f2bSeric 			for (i = 3; i < DtableSize; i++)
931b31e7f2bSeric 			{
932fccb3f7aSbostic 				register int j;
933fccb3f7aSbostic 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
934fccb3f7aSbostic 					(void)fcntl(i, F_SETFD, j|1);
935fccb3f7aSbostic 			}
93633db8731Seric 
93733db8731Seric 			/* try to execute the mailer */
9385df317aaSeric 			execve(m->m_mailer, pvp, UserEnviron);
939dafbc479Seric 			saveerrno = errno;
94013088b9fSeric 			syserr("Cannot exec %s", m->m_mailer);
94195b76e4bSeric 			if (m == LocalMailer)
94255f33c03Seric 				_exit(EX_TEMPFAIL);
943dafbc479Seric 			switch (saveerrno)
94495b76e4bSeric 			{
94595b76e4bSeric 			  case EIO:
94695b76e4bSeric 			  case EAGAIN:
94795b76e4bSeric 			  case ENOMEM:
94895b76e4bSeric # ifdef EPROCLIM
94995b76e4bSeric 			  case EPROCLIM:
95095b76e4bSeric # endif
95195b76e4bSeric 				_exit(EX_TEMPFAIL);
95295b76e4bSeric 			}
953a590b978Seric 			_exit(EX_UNAVAILABLE);
95425a99e2eSeric 		}
95525a99e2eSeric 
956f8952a83Seric 		/*
957c579ef51Seric 		**  Set up return value.
958f8952a83Seric 		*/
959f8952a83Seric 
960b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
9612a087e90Seric 		bzero((char *) mci, sizeof *mci);
96215d084d5Seric 		mci->mci_mailer = m;
963b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9642a087e90Seric 		mci->mci_pid = pid;
965f8952a83Seric 		(void) close(mpvect[0]);
96675889e88Seric 		mci->mci_out = fdopen(mpvect[1], "w");
967c579ef51Seric 		if (clever)
96825a99e2eSeric 		{
969c579ef51Seric 			(void) close(rpvect[1]);
97075889e88Seric 			mci->mci_in = fdopen(rpvect[0], "r");
97175889e88Seric 		}
97275889e88Seric 		else
97375889e88Seric 		{
97475889e88Seric 			mci->mci_flags |= MCIF_TEMP;
97575889e88Seric 			mci->mci_in = NULL;
97675889e88Seric 		}
977b31e7f2bSeric 	}
978b31e7f2bSeric 
979b31e7f2bSeric 	/*
980b31e7f2bSeric 	**  If we are in SMTP opening state, send initial protocol.
981b31e7f2bSeric 	*/
982b31e7f2bSeric 
983b31e7f2bSeric 	if (clever && mci->mci_state != MCIS_CLOSED)
984b31e7f2bSeric 	{
985b31e7f2bSeric 		smtpinit(m, mci, e);
986b31e7f2bSeric 	}
9871fa7c2ebSeric 	if (tTd(11, 1))
9881fa7c2ebSeric 	{
9891fa7c2ebSeric 		printf("openmailer: ");
9901fa7c2ebSeric 		mci_dump(mci);
9911fa7c2ebSeric 	}
992c579ef51Seric 
99375889e88Seric 	return mci;
99425a99e2eSeric }
99525a99e2eSeric /*
99625a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
99725a99e2eSeric **
99825a99e2eSeric **	Parameters:
99925a99e2eSeric **		stat -- the status code from the mailer (high byte
100025a99e2eSeric **			only; core dumps must have been taken care of
100125a99e2eSeric **			already).
100225a99e2eSeric **		m -- the mailer descriptor for this mailer.
100325a99e2eSeric **
100425a99e2eSeric **	Returns:
1005db8841e9Seric **		none.
100625a99e2eSeric **
100725a99e2eSeric **	Side Effects:
1008c1f9df2cSeric **		Errors may be incremented.
100925a99e2eSeric **		ExitStat may be set.
101025a99e2eSeric */
101125a99e2eSeric 
1012198d9be0Seric giveresponse(stat, m, e)
101325a99e2eSeric 	int stat;
1014588cad61Seric 	register MAILER *m;
1015198d9be0Seric 	ENVELOPE *e;
101625a99e2eSeric {
101725a99e2eSeric 	register char *statmsg;
101825a99e2eSeric 	extern char *SysExMsg[];
101925a99e2eSeric 	register int i;
1020d4bd8f0eSbostic 	extern int N_SysEx;
1021d4bd8f0eSbostic #ifdef NAMED_BIND
1022d4bd8f0eSbostic 	extern int h_errno;
1023d4bd8f0eSbostic #endif
1024198d9be0Seric 	char buf[MAXLINE];
102525a99e2eSeric 
10267d1fc79dSeric #ifdef lint
10277d1fc79dSeric 	if (m == NULL)
10287d1fc79dSeric 		return;
10297d1fc79dSeric #endif lint
10307d1fc79dSeric 
103113bbc08cSeric 	/*
103213bbc08cSeric 	**  Compute status message from code.
103313bbc08cSeric 	*/
103413bbc08cSeric 
103525a99e2eSeric 	i = stat - EX__BASE;
1036588cad61Seric 	if (stat == 0)
1037588cad61Seric 		statmsg = "250 Sent";
1038588cad61Seric 	else if (i < 0 || i > N_SysEx)
1039588cad61Seric 	{
1040588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1041588cad61Seric 		stat = EX_UNAVAILABLE;
1042588cad61Seric 		statmsg = buf;
1043588cad61Seric 	}
1044198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1045198d9be0Seric 	{
10468557d168Seric 		(void) strcpy(buf, SysExMsg[i]);
1047d4bd8f0eSbostic #ifdef NAMED_BIND
1048f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1049f28da541Smiriam 		{
1050f28da541Smiriam 			extern char *errstring();
1051f28da541Smiriam 
1052f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1053f28da541Smiriam 		}
1054f28da541Smiriam 		else
1055d4bd8f0eSbostic #endif
1056f28da541Smiriam 		{
10578557d168Seric 			if (errno != 0)
1058198d9be0Seric 			{
105987c9b3e7Seric 				extern char *errstring();
10608557d168Seric 
1061d87e85f3Seric 				statmsg = errstring(errno);
1062d87e85f3Seric 			}
1063d87e85f3Seric 			else
1064d87e85f3Seric 			{
1065d87e85f3Seric #ifdef SMTP
1066d87e85f3Seric 				extern char SmtpError[];
1067d87e85f3Seric 
1068d87e85f3Seric 				statmsg = SmtpError;
10696c2c3107Seric #else /* SMTP */
1070d87e85f3Seric 				statmsg = NULL;
10716c2c3107Seric #endif /* SMTP */
1072d87e85f3Seric 			}
1073f28da541Smiriam 		}
1074d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1075d87e85f3Seric 		{
107687c9b3e7Seric 			(void) strcat(buf, ": ");
1077d87e85f3Seric 			(void) strcat(buf, statmsg);
10788557d168Seric 		}
1079198d9be0Seric 		statmsg = buf;
1080198d9be0Seric 	}
108125a99e2eSeric 	else
1082d87e85f3Seric 	{
108325a99e2eSeric 		statmsg = SysExMsg[i];
1084d87e85f3Seric 	}
1085588cad61Seric 
1086588cad61Seric 	/*
1087588cad61Seric 	**  Print the message as appropriate
1088588cad61Seric 	*/
1089588cad61Seric 
1090198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
10915826d9d3Seric 		message(Arpa_Info, &statmsg[4]);
109225a99e2eSeric 	else
109325a99e2eSeric 	{
1094c1f9df2cSeric 		Errors++;
10955826d9d3Seric 		usrerr(statmsg);
109625a99e2eSeric 	}
109725a99e2eSeric 
109825a99e2eSeric 	/*
109925a99e2eSeric 	**  Final cleanup.
110025a99e2eSeric 	**	Log a record of the transaction.  Compute the new
110125a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
110225a99e2eSeric 	**	that.
110325a99e2eSeric 	*/
110425a99e2eSeric 
110561f5a1d4Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
1106b31e7f2bSeric 		logdelivery(&statmsg[4], e);
1107eb238f8cSeric 
1108eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1109eb238f8cSeric 		setstat(stat);
1110198d9be0Seric 	if (stat != EX_OK)
1111198d9be0Seric 	{
1112198d9be0Seric 		if (e->e_message != NULL)
1113198d9be0Seric 			free(e->e_message);
1114198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1115198d9be0Seric 	}
11168557d168Seric 	errno = 0;
1117d4bd8f0eSbostic #ifdef NAMED_BIND
1118f28da541Smiriam 	h_errno = 0;
1119d4bd8f0eSbostic #endif
1120eb238f8cSeric }
1121eb238f8cSeric /*
1122eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1123eb238f8cSeric **
1124eb238f8cSeric **	Parameters:
1125eb238f8cSeric **		stat -- the message to print for the status
1126eb238f8cSeric **
1127eb238f8cSeric **	Returns:
1128eb238f8cSeric **		none
1129eb238f8cSeric **
1130eb238f8cSeric **	Side Effects:
1131eb238f8cSeric **		none
1132eb238f8cSeric */
1133eb238f8cSeric 
1134b31e7f2bSeric logdelivery(stat, e)
1135eb238f8cSeric 	char *stat;
1136b31e7f2bSeric 	register ENVELOPE *e;
11375cf56be3Seric {
11385cf56be3Seric 	extern char *pintvl();
11395cf56be3Seric 
1140eb238f8cSeric # ifdef LOG
1141b31e7f2bSeric 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", e->e_id,
1142b31e7f2bSeric 	       e->e_to, pintvl(curtime() - e->e_ctime, TRUE), stat);
11436c2c3107Seric # endif /* LOG */
114425a99e2eSeric }
114525a99e2eSeric /*
114651552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
114725a99e2eSeric **
114851552439Seric **	This can be made an arbitrary message separator by changing $l
114951552439Seric **
11509b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
11519b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
11529b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
11539b6c17a6Seric **	this kind of antique garbage????
115425a99e2eSeric **
115525a99e2eSeric **	Parameters:
115651552439Seric **		fp -- the file to output to.
115751552439Seric **		m -- the mailer describing this entry.
115825a99e2eSeric **
115925a99e2eSeric **	Returns:
116051552439Seric **		none
116125a99e2eSeric **
116225a99e2eSeric **	Side Effects:
116351552439Seric **		outputs some text to fp.
116425a99e2eSeric */
116525a99e2eSeric 
1166b31e7f2bSeric putfromline(fp, m, e)
116751552439Seric 	register FILE *fp;
116851552439Seric 	register MAILER *m;
1169b31e7f2bSeric 	ENVELOPE *e;
117025a99e2eSeric {
11719b6c17a6Seric 	char *template = "\001l\n";
117251552439Seric 	char buf[MAXLINE];
117325a99e2eSeric 
117457fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
117551552439Seric 		return;
117613bbc08cSeric 
11772c7e1b8dSeric # ifdef UGLYUUCP
117857fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
117974b6e67bSeric 	{
1180ea09d6edSeric 		char *bang;
1181ea09d6edSeric 		char xbuf[MAXLINE];
118274b6e67bSeric 
1183b31e7f2bSeric 		expand("\001<", buf, &buf[sizeof buf - 1], e);
11846c2c3107Seric 		bang = strchr(buf, '!');
118574b6e67bSeric 		if (bang == NULL)
1186ea09d6edSeric 			syserr("No ! in UUCP! (%s)", buf);
118774b6e67bSeric 		else
1188588cad61Seric 		{
1189ea09d6edSeric 			*bang++ = '\0';
11909b6c17a6Seric 			(void) sprintf(xbuf, "From %s  \001d remote from %s\n", bang, buf);
1191ea09d6edSeric 			template = xbuf;
119274b6e67bSeric 		}
1193588cad61Seric 	}
11946c2c3107Seric # endif /* UGLYUUCP */
1195b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
119677b52738Seric 	putline(buf, fp, m);
1197bc6e2962Seric }
1198bc6e2962Seric /*
119951552439Seric **  PUTBODY -- put the body of a message.
120051552439Seric **
120151552439Seric **	Parameters:
120251552439Seric **		fp -- file to output onto.
120377b52738Seric **		m -- a mailer descriptor to control output format.
12049a6a5f55Seric **		e -- the envelope to put out.
120551552439Seric **
120651552439Seric **	Returns:
120751552439Seric **		none.
120851552439Seric **
120951552439Seric **	Side Effects:
121051552439Seric **		The message is written onto fp.
121151552439Seric */
121251552439Seric 
121377b52738Seric putbody(fp, m, e)
121451552439Seric 	FILE *fp;
1215588cad61Seric 	MAILER *m;
12169a6a5f55Seric 	register ENVELOPE *e;
121751552439Seric {
121877b52738Seric 	char buf[MAXLINE];
121951552439Seric 
122051552439Seric 	/*
122151552439Seric 	**  Output the body of the message
122251552439Seric 	*/
122351552439Seric 
12249a6a5f55Seric 	if (e->e_dfp == NULL)
122551552439Seric 	{
12269a6a5f55Seric 		if (e->e_df != NULL)
12279a6a5f55Seric 		{
12289a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
12299a6a5f55Seric 			if (e->e_dfp == NULL)
12308f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
12318f9146b0Srick 				e->e_df, e->e_to, e->e_from);
12329a6a5f55Seric 		}
12339a6a5f55Seric 		else
123477b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
12359a6a5f55Seric 	}
12369a6a5f55Seric 	if (e->e_dfp != NULL)
12379a6a5f55Seric 	{
12389a6a5f55Seric 		rewind(e->e_dfp);
123977b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
124024fc8aeeSeric 		{
124124fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1242d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
12433462ad9eSeric 				(void) putc('>', fp);
124477b52738Seric 			putline(buf, fp, m);
124524fc8aeeSeric 		}
124651552439Seric 
12479a6a5f55Seric 		if (ferror(e->e_dfp))
124851552439Seric 		{
124951552439Seric 			syserr("putbody: read error");
125051552439Seric 			ExitStat = EX_IOERR;
125151552439Seric 		}
125251552439Seric 	}
125351552439Seric 
125451552439Seric 	(void) fflush(fp);
125551552439Seric 	if (ferror(fp) && errno != EPIPE)
125651552439Seric 	{
125751552439Seric 		syserr("putbody: write error");
125851552439Seric 		ExitStat = EX_IOERR;
125951552439Seric 	}
126051552439Seric 	errno = 0;
126125a99e2eSeric }
126225a99e2eSeric /*
126325a99e2eSeric **  MAILFILE -- Send a message to a file.
126425a99e2eSeric **
1265f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1266f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1267f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1268f129ec7dSeric **	sendmail runs as root.
1269f129ec7dSeric **
1270588cad61Seric **	This could be done as a subordinate mailer, except that it
1271588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1272588cad61Seric **	view this as being sufficiently important as to include it
1273588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1274588cad61Seric **	to create another process plus some pipes to save the message.
1275588cad61Seric **
127625a99e2eSeric **	Parameters:
127725a99e2eSeric **		filename -- the name of the file to send to.
12786259796dSeric **		ctladdr -- the controlling address header -- includes
12796259796dSeric **			the userid/groupid to be when sending.
128025a99e2eSeric **
128125a99e2eSeric **	Returns:
128225a99e2eSeric **		The exit code associated with the operation.
128325a99e2eSeric **
128425a99e2eSeric **	Side Effects:
128525a99e2eSeric **		none.
128625a99e2eSeric */
128725a99e2eSeric 
1288b31e7f2bSeric mailfile(filename, ctladdr, e)
128925a99e2eSeric 	char *filename;
12906259796dSeric 	ADDRESS *ctladdr;
1291b31e7f2bSeric 	register ENVELOPE *e;
129225a99e2eSeric {
129325a99e2eSeric 	register FILE *f;
129432d19d43Seric 	register int pid;
129515d084d5Seric 	int mode;
129625a99e2eSeric 
129732d19d43Seric 	/*
129832d19d43Seric 	**  Fork so we can change permissions here.
129932d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
130032d19d43Seric 	**	the complications of calling subroutines, etc.
130132d19d43Seric 	*/
130232d19d43Seric 
130332d19d43Seric 	DOFORK(fork);
130432d19d43Seric 
130532d19d43Seric 	if (pid < 0)
130632d19d43Seric 		return (EX_OSERR);
130732d19d43Seric 	else if (pid == 0)
130832d19d43Seric 	{
130932d19d43Seric 		/* child -- actually write to file */
1310f129ec7dSeric 		struct stat stb;
1311f129ec7dSeric 
13120984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
13130984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
13140984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
13153462ad9eSeric 		(void) umask(OldUmask);
131695f16dc0Seric 
1317f129ec7dSeric 		if (stat(filename, &stb) < 0)
1318e6e1265fSeric 			stb.st_mode = 0666;
131915d084d5Seric 		mode = stb.st_mode;
132095f16dc0Seric 
132195f16dc0Seric 		/* limit the errors to those actually caused in the child */
132295f16dc0Seric 		errno = 0;
132395f16dc0Seric 		ExitStat = EX_OK;
132495f16dc0Seric 
1325f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1326f129ec7dSeric 			exit(EX_CANTCREAT);
132703827b5fSeric 		if (ctladdr == NULL)
13288f9146b0Srick 			ctladdr = &e->e_from;
132915d084d5Seric 		else
133015d084d5Seric 		{
133115d084d5Seric 			/* ignore setuid and setgid bits */
133215d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
133315d084d5Seric 		}
133415d084d5Seric 
13358f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
13368f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
13378f9146b0Srick 		{
13388f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
133995f16dc0Seric 			if (e->e_dfp == NULL)
134095f16dc0Seric 			{
13418f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
13428f9146b0Srick 					e->e_df, e->e_to, e->e_from);
13438f9146b0Srick 			}
13448f9146b0Srick 		}
13458f9146b0Srick 
134615d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1347e36b99e2Seric 		{
134895f16dc0Seric 			if (ctladdr->q_uid == 0)
134995f16dc0Seric 			{
1350e36b99e2Seric 				(void) setgid(DefGid);
1351898a126bSbostic 				(void) initgroups(DefUser, DefGid);
135295f16dc0Seric 			}
135395f16dc0Seric 			else
135495f16dc0Seric 			{
13556259796dSeric 				(void) setgid(ctladdr->q_gid);
1356898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1357898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1358898a126bSbostic 					ctladdr->q_gid);
1359898a126bSbostic 			}
1360e36b99e2Seric 		}
136115d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1362e36b99e2Seric 		{
1363e36b99e2Seric 			if (ctladdr->q_uid == 0)
1364e36b99e2Seric 				(void) setuid(DefUid);
1365e36b99e2Seric 			else
13666259796dSeric 				(void) setuid(ctladdr->q_uid);
1367e36b99e2Seric 		}
136895f16dc0Seric 		FileName = filename;
136995f16dc0Seric 		LineNumber = 0;
137027628d59Seric 		f = dfopen(filename, "a");
137125a99e2eSeric 		if (f == NULL)
137295f16dc0Seric 		{
137395f16dc0Seric 			message("cannot open");
137432d19d43Seric 			exit(EX_CANTCREAT);
137595f16dc0Seric 		}
137625a99e2eSeric 
1377b31e7f2bSeric 		putfromline(f, ProgMailer, e);
1378b843d759Seric 		(*e->e_puthdr)(f, ProgMailer, e);
137977b52738Seric 		putline("\n", f, ProgMailer);
1380b843d759Seric 		(*e->e_putbody)(f, ProgMailer, e);
138177b52738Seric 		putline("\n", f, ProgMailer);
138295f16dc0Seric 		if (ferror(f))
138395f16dc0Seric 		{
138495f16dc0Seric 			message("I/O error");
138595f16dc0Seric 			setstat(EX_IOERR);
138695f16dc0Seric 		}
1387db8841e9Seric 		(void) fclose(f);
138832d19d43Seric 		(void) fflush(stdout);
1389e36b99e2Seric 
139027628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1391c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
139295f16dc0Seric 		exit(ExitStat);
139313bbc08cSeric 		/*NOTREACHED*/
139432d19d43Seric 	}
139532d19d43Seric 	else
139632d19d43Seric 	{
139732d19d43Seric 		/* parent -- wait for exit status */
1398588cad61Seric 		int st;
139932d19d43Seric 
1400588cad61Seric 		st = waitfor(pid);
1401588cad61Seric 		if ((st & 0377) != 0)
1402588cad61Seric 			return (EX_UNAVAILABLE);
1403588cad61Seric 		else
1404588cad61Seric 			return ((st >> 8) & 0377);
14058f9146b0Srick 		/*NOTREACHED*/
140632d19d43Seric 	}
140725a99e2eSeric }
1408ea4dc939Seric /*
1409ea4dc939Seric **  SENDALL -- actually send all the messages.
1410ea4dc939Seric **
1411ea4dc939Seric **	Parameters:
14120c52a0b3Seric **		e -- the envelope to send.
14137b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
14147b95031aSeric **			the current SendMode.
1415ea4dc939Seric **
1416ea4dc939Seric **	Returns:
1417ea4dc939Seric **		none.
1418ea4dc939Seric **
1419ea4dc939Seric **	Side Effects:
1420ea4dc939Seric **		Scans the send lists and sends everything it finds.
14210c52a0b3Seric **		Delivers any appropriate error messages.
1422276723a8Seric **		If we are running in a non-interactive mode, takes the
1423276723a8Seric **			appropriate action.
1424ea4dc939Seric */
1425ea4dc939Seric 
1426276723a8Seric sendall(e, mode)
14270c52a0b3Seric 	ENVELOPE *e;
1428276723a8Seric 	char mode;
1429ea4dc939Seric {
1430e77e673fSeric 	register ADDRESS *q;
143114a8ed7aSeric 	bool oldverbose;
1432276723a8Seric 	int pid;
1433dde5acadSeric 	int nsent;
14341c265a00Seric # ifdef LOCKF
14351c265a00Seric 	struct flock lfd;
14361c265a00Seric # endif
1437ea4dc939Seric 
14387b95031aSeric 	/* determine actual delivery mode */
14397b95031aSeric 	if (mode == SM_DEFAULT)
14407b95031aSeric 	{
14415f73204aSeric 		extern bool shouldqueue();
14427b95031aSeric 
144355e150dbSeric 		if (shouldqueue(e->e_msgpriority, e->e_ctime))
14447b95031aSeric 			mode = SM_QUEUE;
14457b95031aSeric 		else
14467b95031aSeric 			mode = SendMode;
14477b95031aSeric 	}
14487b95031aSeric 
1449df864a8fSeric 	if (tTd(13, 1))
1450772e6e50Seric 	{
1451276723a8Seric 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
14520c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1453772e6e50Seric 	}
1454ea4dc939Seric 
14550c52a0b3Seric 	/*
1456276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1457588cad61Seric 	**	Check to make sure the hop count is reasonable.
1458588cad61Seric 	**	Delete sends to the sender in mailing lists.
1459276723a8Seric 	*/
1460276723a8Seric 
1461588cad61Seric 	CurEnv = e;
1462276723a8Seric 
14631d57450bSeric 	if (e->e_hopcount > MaxHopCount)
1464276723a8Seric 	{
14658f9146b0Srick 		errno = 0;
14668f9146b0Srick 		syserr("sendall: too many hops %d (%d max): from %s, to %s",
1467f37d7987Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr, e->e_to);
1468588cad61Seric 		return;
1469588cad61Seric 	}
1470588cad61Seric 
1471588cad61Seric 	if (!MeToo)
1472276723a8Seric 	{
1473f3d6c55cSeric 		extern ADDRESS *recipient();
1474f3d6c55cSeric 
1475588cad61Seric 		e->e_from.q_flags |= QDONTSEND;
1476b843d759Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1477276723a8Seric 	}
1478588cad61Seric 
1479588cad61Seric # ifdef QUEUE
1480b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1481b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1482b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
1483e020b127Seric 		queueup(e, TRUE, mode == SM_QUEUE);
14846c2c3107Seric #endif /* QUEUE */
1485276723a8Seric 
1486276723a8Seric 	oldverbose = Verbose;
1487276723a8Seric 	switch (mode)
1488276723a8Seric 	{
1489276723a8Seric 	  case SM_VERIFY:
1490276723a8Seric 		Verbose = TRUE;
1491276723a8Seric 		break;
1492276723a8Seric 
1493276723a8Seric 	  case SM_QUEUE:
1494c7f5410dSeric   queueonly:
1495b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1496276723a8Seric 		return;
1497276723a8Seric 
1498276723a8Seric 	  case SM_FORK:
14999a6a5f55Seric 		if (e->e_xfp != NULL)
15009a6a5f55Seric 			(void) fflush(e->e_xfp);
1501c7f5410dSeric 
1502c7f5410dSeric # ifdef LOCKF
1503c7f5410dSeric 		/*
1504c7f5410dSeric 		**  Since lockf has the interesting semantic that the
15051c265a00Seric 		**  lock is lost when we fork, we have to risk losing
15061c265a00Seric 		**  the lock here by closing before the fork, and then
15071c265a00Seric 		**  trying to get it back in the child.
1508c7f5410dSeric 		*/
1509c7f5410dSeric 
1510e020b127Seric 		if (e->e_lockfp != NULL)
1511c7f5410dSeric 		{
1512e020b127Seric 			(void) fclose(e->e_lockfp);
1513e020b127Seric 			e->e_lockfp = NULL;
1514c7f5410dSeric 		}
1515c7f5410dSeric # endif /* LOCKF */
1516c7f5410dSeric 
1517276723a8Seric 		pid = fork();
1518276723a8Seric 		if (pid < 0)
1519276723a8Seric 		{
1520c7f5410dSeric 			goto queueonly;
1521276723a8Seric 		}
1522276723a8Seric 		else if (pid > 0)
1523a6fce3d8Seric 		{
1524a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1525b254bcb6Seric 			e->e_id = e->e_df = NULL;
1526c7f5410dSeric # ifndef LOCKF
1527e020b127Seric 			if (e->e_lockfp != NULL)
1528e020b127Seric 				(void) fclose(e->e_lockfp);
1529c7f5410dSeric # endif
1530276723a8Seric 			return;
1531a6fce3d8Seric 		}
1532276723a8Seric 
1533276723a8Seric 		/* double fork to avoid zombies */
1534276723a8Seric 		if (fork() > 0)
1535276723a8Seric 			exit(EX_OK);
1536276723a8Seric 
1537a6fce3d8Seric 		/* be sure we are immune from the terminal */
1538769e215aSeric 		disconnect(FALSE);
1539a6fce3d8Seric 
1540e6720d51Seric # ifdef LOCKF
1541e6720d51Seric 		/*
1542c7f5410dSeric 		**  Now try to get our lock back.
1543e6720d51Seric 		*/
1544e6720d51Seric 
15451c265a00Seric 		lfd.l_type = F_WRLCK;
15461c265a00Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1547e020b127Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1548e020b127Seric 		if (e->e_lockfp == NULL ||
15491c265a00Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1550e6720d51Seric 		{
1551e6720d51Seric 			/* oops....  lost it */
1552e6720d51Seric # ifdef LOG
1553e6720d51Seric 			if (LogLevel > 5)
1554c7f5410dSeric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1555b31e7f2bSeric 					e->e_id);
1556e6720d51Seric # endif /* LOG */
1557e6720d51Seric 			exit(EX_OK);
1558e6720d51Seric 		}
1559e6720d51Seric # endif /* LOCKF */
1560e6720d51Seric 
1561276723a8Seric 		break;
1562276723a8Seric 	}
1563276723a8Seric 
1564276723a8Seric 	/*
15650c52a0b3Seric 	**  Run through the list and send everything.
15660c52a0b3Seric 	*/
15670c52a0b3Seric 
1568dde5acadSeric 	nsent = 0;
15690c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1570ea4dc939Seric 	{
1571276723a8Seric 		if (mode == SM_VERIFY)
1572ea4dc939Seric 		{
1573a6fce3d8Seric 			e->e_to = q->q_paddr;
1574e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1575ea4dc939Seric 				message(Arpa_Info, "deliverable");
1576ea4dc939Seric 		}
1577dde5acadSeric 		else if (!bitset(QDONTSEND, q->q_flags))
1578dde5acadSeric 		{
1579de1179f5Seric # ifdef QUEUE
1580dde5acadSeric 			/*
1581dde5acadSeric 			**  Checkpoint the send list every few addresses
1582dde5acadSeric 			*/
1583dde5acadSeric 
1584dde5acadSeric 			if (nsent >= CheckpointInterval)
1585dde5acadSeric 			{
1586e020b127Seric 				queueup(e, TRUE, FALSE);
1587dde5acadSeric 				nsent = 0;
1588dde5acadSeric 			}
1589de1179f5Seric # endif /* QUEUE */
1590dde5acadSeric 			if (deliver(e, q) == EX_OK)
1591dde5acadSeric 				nsent++;
1592dde5acadSeric 		}
1593ea4dc939Seric 	}
159414a8ed7aSeric 	Verbose = oldverbose;
15950c52a0b3Seric 
15960c52a0b3Seric 	/*
15970c52a0b3Seric 	**  Now run through and check for errors.
15980c52a0b3Seric 	*/
15990c52a0b3Seric 
1600e020b127Seric 	if (mode == SM_VERIFY)
16010c52a0b3Seric 		return;
16020c52a0b3Seric 
16030c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
16040c52a0b3Seric 	{
16050c52a0b3Seric 		register ADDRESS *qq;
16060c52a0b3Seric 
1607df864a8fSeric 		if (tTd(13, 3))
1608df864a8fSeric 		{
1609df864a8fSeric 			printf("Checking ");
1610df864a8fSeric 			printaddr(q, FALSE);
1611df864a8fSeric 		}
1612df864a8fSeric 
1613b254bcb6Seric 		/* only send errors if the message failed */
1614b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1615b254bcb6Seric 			continue;
16160c52a0b3Seric 
16170c52a0b3Seric 		/* we have an address that failed -- find the parent */
16180c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
16190c52a0b3Seric 		{
16200c52a0b3Seric 			char obuf[MAXNAME + 6];
16210c52a0b3Seric 			extern char *aliaslookup();
16220c52a0b3Seric 
16230c52a0b3Seric 			/* we can only have owners for local addresses */
162457fc6f17Seric 			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
16250c52a0b3Seric 				continue;
16260c52a0b3Seric 
16270c52a0b3Seric 			/* see if the owner list exists */
16280c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1629cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1630cec031e3Seric 				(void) strcat(obuf, "owner");
1631cec031e3Seric 			else
16320c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
163355e150dbSeric 			if (!bitnset(M_USR_UPPER, qq->q_mailer->m_flags))
1634ef137175Sbostic 				makelower(obuf);
16350c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
16360c52a0b3Seric 				continue;
16370c52a0b3Seric 
1638df864a8fSeric 			if (tTd(13, 4))
1639df864a8fSeric 				printf("Errors to %s\n", obuf);
1640df864a8fSeric 
16410c52a0b3Seric 			/* owner list exists -- add it to the error queue */
1642b843d759Seric 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue, e);
164355e150dbSeric 
164455e150dbSeric 			/* and set the return path to point to it */
164555e150dbSeric 			e->e_returnpath = newstr(obuf);
164655e150dbSeric 
164753e3fa05Seric 			ErrorMode = EM_MAIL;
16480c52a0b3Seric 			break;
16490c52a0b3Seric 		}
16500c52a0b3Seric 
16510c52a0b3Seric 		/* if we did not find an owner, send to the sender */
16527455aa0bSeric 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1653b843d759Seric 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue, e);
16540c52a0b3Seric 	}
1655276723a8Seric 
1656276723a8Seric 	if (mode == SM_FORK)
1657276723a8Seric 		finis();
16580c52a0b3Seric }
1659