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*1d8f1806Seric static char sccsid[] = "@(#)deliver.c	5.58 (Berkeley) 07/12/92";
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
2012eec323Sbostic #include <sys/param.h>
21912a731aSbostic #include <arpa/nameser.h>
22912a731aSbostic #include <resolv.h>
23134746fbSeric #endif
2425a99e2eSeric 
2525a99e2eSeric /*
2613bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
2713bbc08cSeric **
2813bbc08cSeric **	This routine delivers to everyone on the same host as the
2913bbc08cSeric **	user on the head of the list.  It is clever about mailers
3013bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
3113bbc08cSeric **	that it will deliver to all these addresses however -- so
3213bbc08cSeric **	deliver should be called once for each address on the
3313bbc08cSeric **	list.
3425a99e2eSeric **
3525a99e2eSeric **	Parameters:
36588cad61Seric **		e -- the envelope to deliver.
37c77d1c25Seric **		firstto -- head of the address list to deliver to.
3825a99e2eSeric **
3925a99e2eSeric **	Returns:
4025a99e2eSeric **		zero -- successfully delivered.
4125a99e2eSeric **		else -- some failure, see ExitStat for more info.
4225a99e2eSeric **
4325a99e2eSeric **	Side Effects:
4425a99e2eSeric **		The standard input is passed off to someone.
4525a99e2eSeric */
4625a99e2eSeric 
47588cad61Seric deliver(e, firstto)
48588cad61Seric 	register ENVELOPE *e;
49c77d1c25Seric 	ADDRESS *firstto;
5025a99e2eSeric {
5178442df3Seric 	char *host;			/* host being sent to */
5278442df3Seric 	char *user;			/* user being sent to */
5325a99e2eSeric 	char **pvp;
545dfc646bSeric 	register char **mvp;
5525a99e2eSeric 	register char *p;
56588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
576259796dSeric 	ADDRESS *ctladdr;
58b31e7f2bSeric 	register MCI *mci;
59c77d1c25Seric 	register ADDRESS *to = firstto;
60c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
61772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
62911693bfSbostic 	int rcode;			/* response code */
63c23ed322Seric 	char *from;			/* pointer to from person */
64ee6bf8dfSeric 	char *pv[MAXPV+1];
65ee6bf8dfSeric 	char tobuf[MAXLINE-50];		/* text line of to people */
66ee6bf8dfSeric 	char buf[MAXNAME];
67ee6bf8dfSeric 	char tfrombuf[MAXNAME];		/* translated from person */
68c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
69ee6bf8dfSeric 	extern bool checkcompat();
70ee6bf8dfSeric 	extern ADDRESS *getctladdr();
71ee6bf8dfSeric 	extern char *remotename();
72b31e7f2bSeric 	extern MCI *openmailer();
7325a99e2eSeric 
7435490626Seric 	errno = 0;
75da2935e1Seric 	if (bitset(QDONTSEND, to->q_flags))
765dfc646bSeric 		return (0);
7725a99e2eSeric 
78134746fbSeric #ifdef NAMED_BIND
79912a731aSbostic 	/* unless interactive, try twice, over a minute */
80912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
81912a731aSbostic 		_res.retrans = 30;
82912a731aSbostic 		_res.retry = 2;
83912a731aSbostic 	}
84d4bd8f0eSbostic #endif
85912a731aSbostic 
8651552439Seric 	m = to->q_mailer;
8751552439Seric 	host = to->q_host;
8851552439Seric 
896ef48975Seric 	if (tTd(10, 1))
905dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
9151552439Seric 			m->m_mno, host, to->q_user);
92f3dbc832Seric 
93f3dbc832Seric 	/*
94f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
95f3dbc832Seric 	**  connections now, just mark these addresses and return.
96f3dbc832Seric 	**	This is useful if we want to batch connections to
97f3dbc832Seric 	**	reduce load.  This will cause the messages to be
98f3dbc832Seric 	**	queued up, and a daemon will come along to send the
99f3dbc832Seric 	**	messages later.
100f3dbc832Seric 	**		This should be on a per-mailer basis.
101f3dbc832Seric 	*/
102f3dbc832Seric 
10357fc6f17Seric 	if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) &&
104317680d6Seric 	    !Verbose)
105f3dbc832Seric 	{
106f3dbc832Seric 		for (; to != NULL; to = to->q_next)
107f4560e80Seric 		{
108f4560e80Seric 			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
109f4560e80Seric 				continue;
110f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
111588cad61Seric 			e->e_to = to->q_paddr;
112eb238f8cSeric 			message(Arpa_Info, "queued");
113eb238f8cSeric 			if (LogLevel > 4)
114b31e7f2bSeric 				logdelivery("queued", e);
115f4560e80Seric 		}
116588cad61Seric 		e->e_to = NULL;
117f3dbc832Seric 		return (0);
118f3dbc832Seric 	}
119f3dbc832Seric 
12025a99e2eSeric 	/*
1215dfc646bSeric 	**  Do initial argv setup.
1225dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
1235dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
1245dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
1255dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
1265dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
1273bea8136Seric 	**		The from address rewrite is expected to make
1283bea8136Seric 	**		the address relative to the other end.
1295dfc646bSeric 	*/
1305dfc646bSeric 
13178442df3Seric 	/* rewrite from address, using rewriting rules */
132c23ed322Seric 	(void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, TRUE));
133c23ed322Seric 	if (e->e_returnpath == e->e_sender)
134c23ed322Seric 	{
135c23ed322Seric 		from = rpathbuf;
136c23ed322Seric 	}
137c23ed322Seric 	else
138c23ed322Seric 	{
139c23ed322Seric 		(void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, TRUE));
140c23ed322Seric 		from = tfrombuf;
141c23ed322Seric 	}
14278442df3Seric 
143c23ed322Seric 	define('f', e->e_returnpath, e);	/* raw return path */
144c23ed322Seric 	define('<', rpathbuf, e);		/* translated return path */
145c23ed322Seric 	define('g', from, e);			/* translated sender */
146588cad61Seric 	define('h', host, e);			/* to host */
1475dfc646bSeric 	Errors = 0;
1485dfc646bSeric 	pvp = pv;
1495dfc646bSeric 	*pvp++ = m->m_argv[0];
1505dfc646bSeric 
1515dfc646bSeric 	/* insert -f or -r flag as appropriate */
15257fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
1535dfc646bSeric 	{
15457fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
1555dfc646bSeric 			*pvp++ = "-f";
1565dfc646bSeric 		else
1575dfc646bSeric 			*pvp++ = "-r";
158c23ed322Seric 		*pvp++ = newstr(rpathbuf);
1595dfc646bSeric 	}
1605dfc646bSeric 
1615dfc646bSeric 	/*
1625dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1635dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1645dfc646bSeric 	**  be one of these, and there are only a few more slots
1655dfc646bSeric 	**  in the pv after it.
1665dfc646bSeric 	*/
1675dfc646bSeric 
1685dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1695dfc646bSeric 	{
1709b6c17a6Seric 		while ((p = index(p, '\001')) != NULL)
1715dfc646bSeric 			if (*++p == 'u')
1725dfc646bSeric 				break;
1735dfc646bSeric 		if (p != NULL)
1745dfc646bSeric 			break;
1755dfc646bSeric 
1765dfc646bSeric 		/* this entry is safe -- go ahead and process it */
177588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1785dfc646bSeric 		*pvp++ = newstr(buf);
1795dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1805dfc646bSeric 		{
1815dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1825dfc646bSeric 			return (-1);
1835dfc646bSeric 		}
1845dfc646bSeric 	}
185c579ef51Seric 
18633db8731Seric 	/*
18733db8731Seric 	**  If we have no substitution for the user name in the argument
18833db8731Seric 	**  list, we know that we must supply the names otherwise -- and
18933db8731Seric 	**  SMTP is the answer!!
19033db8731Seric 	*/
19133db8731Seric 
1925dfc646bSeric 	if (*mvp == NULL)
193c579ef51Seric 	{
194c579ef51Seric 		/* running SMTP */
1952c7e1b8dSeric # ifdef SMTP
196c579ef51Seric 		clever = TRUE;
197c579ef51Seric 		*pvp = NULL;
1982c7e1b8dSeric # else SMTP
19933db8731Seric 		/* oops!  we don't implement SMTP */
2002c7e1b8dSeric 		syserr("SMTP style mailer");
2012c7e1b8dSeric 		return (EX_SOFTWARE);
2022c7e1b8dSeric # endif SMTP
203c579ef51Seric 	}
2045dfc646bSeric 
2055dfc646bSeric 	/*
2065dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
2075dfc646bSeric 	**  run through our address list and append all the addresses
2085dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
2095dfc646bSeric 	**  always send another copy later.
2105dfc646bSeric 	*/
2115dfc646bSeric 
2125dfc646bSeric 	tobuf[0] = '\0';
213588cad61Seric 	e->e_to = tobuf;
2146259796dSeric 	ctladdr = NULL;
2155dfc646bSeric 	for (; to != NULL; to = to->q_next)
2165dfc646bSeric 	{
2175dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
21857fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
2195dfc646bSeric 			break;
2205dfc646bSeric 
2215dfc646bSeric 		/* if already sent or not for this host, don't send */
222da2935e1Seric 		if (bitset(QDONTSEND, to->q_flags) ||
223da2935e1Seric 		    strcmp(to->q_host, host) != 0 ||
224da2935e1Seric 		    to->q_mailer != firstto->q_mailer)
2255dfc646bSeric 			continue;
2266259796dSeric 
2274b22ea87Seric 		/* avoid overflowing tobuf */
228aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
2294b22ea87Seric 			break;
2304b22ea87Seric 
2316ef48975Seric 		if (tTd(10, 1))
232772e6e50Seric 		{
233772e6e50Seric 			printf("\nsend to ");
234772e6e50Seric 			printaddr(to, FALSE);
235772e6e50Seric 		}
236772e6e50Seric 
2376259796dSeric 		/* compute effective uid/gid when sending */
2387da1035fSeric 		if (to->q_mailer == ProgMailer)
2396259796dSeric 			ctladdr = getctladdr(to);
2406259796dSeric 
2415dfc646bSeric 		user = to->q_user;
242588cad61Seric 		e->e_to = to->q_paddr;
2435dfc646bSeric 		to->q_flags |= QDONTSEND;
2445dfc646bSeric 
2455dfc646bSeric 		/*
2465dfc646bSeric 		**  Check to see that these people are allowed to
2475dfc646bSeric 		**  talk to each other.
2482a6e0786Seric 		*/
2492a6e0786Seric 
25069582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
25169582d2fSeric 		{
25269582d2fSeric 			NoReturn = TRUE;
253672bec4aSeric 			usrerr("Message is too large; %ld bytes max", m->m_maxsize);
25469582d2fSeric 			giveresponse(EX_UNAVAILABLE, m, e);
25569582d2fSeric 			continue;
25669582d2fSeric 		}
2572a6e0786Seric 		if (!checkcompat(to))
2585dfc646bSeric 		{
259198d9be0Seric 			giveresponse(EX_UNAVAILABLE, m, e);
2605dfc646bSeric 			continue;
2615dfc646bSeric 		}
2622a6e0786Seric 
2632a6e0786Seric 		/*
2649ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2659ec9501bSeric 		**	about them.
26625a99e2eSeric 		*/
26725a99e2eSeric 
26857fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
26925a99e2eSeric 		{
270*1d8f1806Seric 			stripquotes(user);
271*1d8f1806Seric 			stripquotes(host);
27225a99e2eSeric 		}
27325a99e2eSeric 
274cdb828c5Seric 		/* hack attack -- delivermail compatibility */
275cdb828c5Seric 		if (m == ProgMailer && *user == '|')
276cdb828c5Seric 			user++;
277cdb828c5Seric 
27825a99e2eSeric 		/*
2793efaed6eSeric 		**  If an error message has already been given, don't
2803efaed6eSeric 		**	bother to send to this address.
2813efaed6eSeric 		**
2823efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2833efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2843efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2853efaed6eSeric 		*/
2863efaed6eSeric 
2876cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2883efaed6eSeric 			continue;
2893efaed6eSeric 
290f2fec898Seric 		/* save statistics.... */
291588cad61Seric 		markstats(e, to);
292f2fec898Seric 
2933efaed6eSeric 		/*
29425a99e2eSeric 		**  See if this user name is "special".
29525a99e2eSeric 		**	If the user name has a slash in it, assume that this
29651552439Seric 		**	is a file -- send it off without further ado.  Note
29751552439Seric 		**	that this type of addresses is not processed along
29851552439Seric 		**	with the others, so we fudge on the To person.
29925a99e2eSeric 		*/
30025a99e2eSeric 
3017da1035fSeric 		if (m == LocalMailer)
30225a99e2eSeric 		{
303a49f24c0Seric 			if (user[0] == '/')
30425a99e2eSeric 			{
305b31e7f2bSeric 				rcode = mailfile(user, getctladdr(to), e);
306198d9be0Seric 				giveresponse(rcode, m, e);
307dde5acadSeric 				if (rcode == EX_OK)
308dde5acadSeric 					to->q_flags |= QSENT;
3095dfc646bSeric 				continue;
31025a99e2eSeric 			}
31125a99e2eSeric 		}
31225a99e2eSeric 
31313bbc08cSeric 		/*
31413bbc08cSeric 		**  Address is verified -- add this user to mailer
31513bbc08cSeric 		**  argv, and add it to the print list of recipients.
31613bbc08cSeric 		*/
31713bbc08cSeric 
318508daeccSeric 		/* link together the chain of recipients */
319508daeccSeric 		to->q_tchain = tochain;
320508daeccSeric 		tochain = to;
321508daeccSeric 
3225dfc646bSeric 		/* create list of users for error messages */
323db8841e9Seric 		(void) strcat(tobuf, ",");
324db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
325588cad61Seric 		define('u', user, e);		/* to user */
326588cad61Seric 		define('z', to->q_home, e);	/* user's home */
3275dfc646bSeric 
328c579ef51Seric 		/*
329508daeccSeric 		**  Expand out this user into argument list.
330c579ef51Seric 		*/
331c579ef51Seric 
332508daeccSeric 		if (!clever)
333c579ef51Seric 		{
334588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3355dfc646bSeric 			*pvp++ = newstr(buf);
3365dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3375dfc646bSeric 			{
3385dfc646bSeric 				/* allow some space for trailing parms */
3395dfc646bSeric 				break;
3405dfc646bSeric 			}
3415dfc646bSeric 		}
342c579ef51Seric 	}
3435dfc646bSeric 
344145b49b1Seric 	/* see if any addresses still exist */
345145b49b1Seric 	if (tobuf[0] == '\0')
346c579ef51Seric 	{
347588cad61Seric 		define('g', (char *) NULL, e);
348c23ed322Seric 		define('<', (char *) NULL, e);
349145b49b1Seric 		return (0);
350c579ef51Seric 	}
351145b49b1Seric 
3525dfc646bSeric 	/* print out messages as full list */
35363780dbdSeric 	e->e_to = tobuf + 1;
3545dfc646bSeric 
3555dfc646bSeric 	/*
3565dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3575dfc646bSeric 	*/
3585dfc646bSeric 
359c579ef51Seric 	while (!clever && *++mvp != NULL)
3605dfc646bSeric 	{
361588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
3625dfc646bSeric 		*pvp++ = newstr(buf);
3635dfc646bSeric 		if (pvp >= &pv[MAXPV])
3645dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3655dfc646bSeric 	}
3665dfc646bSeric 	*pvp++ = NULL;
3675dfc646bSeric 
36825a99e2eSeric 	/*
36925a99e2eSeric 	**  Call the mailer.
3706328bdf7Seric 	**	The argument vector gets built, pipes
37125a99e2eSeric 	**	are created as necessary, and we fork & exec as
3726328bdf7Seric 	**	appropriate.
373c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
37425a99e2eSeric 	*/
37525a99e2eSeric 
37686b26461Seric 	if (ctladdr == NULL)
37786b26461Seric 		ctladdr = &e->e_from;
378134746fbSeric #ifdef NAMED_BIND
3792bcc6d2dSeric 	if (ConfigLevel < 2)
380912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
381134746fbSeric #endif
382b31e7f2bSeric 	mci = openmailer(m, pv, ctladdr, clever, e);
383b31e7f2bSeric 	if (mci == NULL)
384134746fbSeric 	{
385b31e7f2bSeric 		/* catastrophic error */
386b31e7f2bSeric 		rcode = -1;
387b31e7f2bSeric 		goto give_up;
388b31e7f2bSeric 	}
389b31e7f2bSeric 	else if (mci->mci_state != MCIS_OPEN)
390b31e7f2bSeric 	{
391b31e7f2bSeric 		/* couldn't open the mailer */
392b31e7f2bSeric 		rcode = mci->mci_exitstat;
393b31e7f2bSeric 		if (rcode == EX_OK)
394b31e7f2bSeric 		{
395b31e7f2bSeric 			/* shouldn't happen */
396b31e7f2bSeric 			rcode = EX_SOFTWARE;
397b31e7f2bSeric 		}
398b31e7f2bSeric 	}
399b31e7f2bSeric 	else if (!clever)
400b31e7f2bSeric 	{
401b31e7f2bSeric 		/*
402b31e7f2bSeric 		**  Format and send message.
403b31e7f2bSeric 		*/
40415d084d5Seric 
405b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
406b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
407b31e7f2bSeric 		putline("\n", mci->mci_out, m);
408b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
409b31e7f2bSeric 
410b31e7f2bSeric 		/* get the exit status */
411b31e7f2bSeric 		rcode = endmailer(mci, pv[0]);
412134746fbSeric 	}
413134746fbSeric 	else
414b31e7f2bSeric #ifdef SMTP
415134746fbSeric 	{
416b31e7f2bSeric 		/*
417b31e7f2bSeric 		**  Send the MAIL FROM: protocol
418b31e7f2bSeric 		*/
41915d084d5Seric 
420b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
421b31e7f2bSeric 		if (rcode == EX_OK)
42275889e88Seric 		{
423ded0d3daSkarels 			register char *t = tobuf;
424ded0d3daSkarels 			register int i;
425ded0d3daSkarels 
426588cad61Seric 			/* send the recipient list */
42763780dbdSeric 			tobuf[0] = '\0';
42875889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
42975889e88Seric 			{
43063780dbdSeric 				e->e_to = to->q_paddr;
43115d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
43275889e88Seric 				{
43383b7ddc9Seric 					markfailure(e, to, i);
434198d9be0Seric 					giveresponse(i, m, e);
43563780dbdSeric 				}
43675889e88Seric 				else
43775889e88Seric 				{
438911693bfSbostic 					*t++ = ',';
439b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
440b31e7f2bSeric 						continue;
441588cad61Seric 				}
442588cad61Seric 			}
443588cad61Seric 
44463780dbdSeric 			/* now send the data */
44563780dbdSeric 			if (tobuf[0] == '\0')
446b31e7f2bSeric 			{
44763780dbdSeric 				e->e_to = NULL;
448b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
449b31e7f2bSeric 					smtprset(m, mci, e);
450b31e7f2bSeric 			}
45175889e88Seric 			else
45275889e88Seric 			{
45363780dbdSeric 				e->e_to = tobuf + 1;
45475889e88Seric 				rcode = smtpdata(m, mci, e);
45563780dbdSeric 			}
45663780dbdSeric 
45763780dbdSeric 			/* now close the connection */
458b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
45915d084d5Seric 				smtpquit(m, mci, e);
46063780dbdSeric 		}
461c579ef51Seric 	}
462b31e7f2bSeric #else /* not SMTP */
463a05b3449Sbostic 	{
464b31e7f2bSeric 		syserr("deliver: need SMTP compiled to use clever mailer");
465b31e7f2bSeric 		rcode = -1;
466b31e7f2bSeric 		goto give_up;
467a05b3449Sbostic 	}
468b31e7f2bSeric #endif /* SMTP */
469134746fbSeric #ifdef NAMED_BIND
4702bcc6d2dSeric 	if (ConfigLevel < 2)
471912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
472134746fbSeric #endif
4735dfc646bSeric 
474b31e7f2bSeric 	/* arrange a return receipt if requested */
475b31e7f2bSeric 	if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags))
476b31e7f2bSeric 	{
477b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
478b31e7f2bSeric 		/* do we want to send back more info? */
479b31e7f2bSeric 	}
480b31e7f2bSeric 
481c77d1c25Seric 	/*
48263780dbdSeric 	**  Do final status disposal.
48363780dbdSeric 	**	We check for something in tobuf for the SMTP case.
484c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
485c77d1c25Seric 	**		addressees.
486c77d1c25Seric 	*/
487c77d1c25Seric 
488b31e7f2bSeric   give_up:
48963780dbdSeric 	if (tobuf[0] != '\0')
490198d9be0Seric 		giveresponse(rcode, m, e);
491772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
492b31e7f2bSeric 	{
493dde5acadSeric 		if (rcode != EX_OK)
49483b7ddc9Seric 			markfailure(e, to, rcode);
495dde5acadSeric 		else
496dde5acadSeric 			to->q_flags |= QSENT;
497b31e7f2bSeric 	}
498b31e7f2bSeric 
499b31e7f2bSeric 	/*
500b31e7f2bSeric 	**  Restore state and return.
501b31e7f2bSeric 	*/
502c77d1c25Seric 
50335490626Seric 	errno = 0;
504588cad61Seric 	define('g', (char *) NULL, e);
505c23ed322Seric 	define('<', (char *) NULL, e);
5065826d9d3Seric 	return (rcode);
50725a99e2eSeric }
5085dfc646bSeric /*
50983b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
51083b7ddc9Seric **
51183b7ddc9Seric **	Parameters:
51283b7ddc9Seric **		e -- the envelope we are sending.
51383b7ddc9Seric **		q -- the address to mark.
51483b7ddc9Seric **		rcode -- the code signifying the particular failure.
51583b7ddc9Seric **
51683b7ddc9Seric **	Returns:
51783b7ddc9Seric **		none.
51883b7ddc9Seric **
51983b7ddc9Seric **	Side Effects:
52083b7ddc9Seric **		marks the address (and possibly the envelope) with the
52183b7ddc9Seric **			failure so that an error will be returned or
52283b7ddc9Seric **			the message will be queued, as appropriate.
52383b7ddc9Seric */
52483b7ddc9Seric 
52583b7ddc9Seric markfailure(e, q, rcode)
52683b7ddc9Seric 	register ENVELOPE *e;
52783b7ddc9Seric 	register ADDRESS *q;
52883b7ddc9Seric 	int rcode;
52983b7ddc9Seric {
53083b7ddc9Seric 	if (rcode == EX_OK)
53183b7ddc9Seric 		return;
532ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
53383b7ddc9Seric 		q->q_flags |= QBADADDR;
53483b7ddc9Seric 	else if (curtime() > e->e_ctime + TimeOut)
53583b7ddc9Seric 	{
53683b7ddc9Seric 		extern char *pintvl();
537198d9be0Seric 		char buf[MAXLINE];
53883b7ddc9Seric 
53983b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
540198d9be0Seric 		{
541198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
54283b7ddc9Seric 				pintvl(TimeOut, FALSE));
543198d9be0Seric 			if (e->e_message != NULL)
544198d9be0Seric 				free(e->e_message);
545198d9be0Seric 			e->e_message = newstr(buf);
546198d9be0Seric 			message(Arpa_Info, buf);
547198d9be0Seric 		}
54883b7ddc9Seric 		q->q_flags |= QBADADDR;
54983b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
55083b7ddc9Seric 	}
55183b7ddc9Seric 	else
55283b7ddc9Seric 		q->q_flags |= QQUEUEUP;
55383b7ddc9Seric }
55483b7ddc9Seric /*
55532d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
55632d19d43Seric **
55732d19d43Seric **	This MUST be a macro, since after a vfork we are running
55832d19d43Seric **	two processes on the same stack!!!
55932d19d43Seric **
56032d19d43Seric **	Parameters:
56132d19d43Seric **		none.
56232d19d43Seric **
56332d19d43Seric **	Returns:
56432d19d43Seric **		From a macro???  You've got to be kidding!
56532d19d43Seric **
56632d19d43Seric **	Side Effects:
56732d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
56832d19d43Seric **			pid of child in parent, zero in child.
56932d19d43Seric **			-1 on unrecoverable error.
57032d19d43Seric **
57132d19d43Seric **	Notes:
57232d19d43Seric **		I'm awfully sorry this looks so awful.  That's
57332d19d43Seric **		vfork for you.....
57432d19d43Seric */
57532d19d43Seric 
57632d19d43Seric # define NFORKTRIES	5
57784f7cd1bSeric 
57884f7cd1bSeric # ifndef FORK
57984f7cd1bSeric # define FORK	fork
58084f7cd1bSeric # endif
58132d19d43Seric 
58232d19d43Seric # define DOFORK(fORKfN) \
58332d19d43Seric {\
58432d19d43Seric 	register int i;\
58532d19d43Seric \
58611799049Seric 	for (i = NFORKTRIES; --i >= 0; )\
58732d19d43Seric 	{\
58832d19d43Seric 		pid = fORKfN();\
58932d19d43Seric 		if (pid >= 0)\
59032d19d43Seric 			break;\
59111799049Seric 		if (i > 0)\
5926c4635f6Seric 			sleep((unsigned) NFORKTRIES - i);\
59332d19d43Seric 	}\
59432d19d43Seric }
59532d19d43Seric /*
5962ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
5972ed72599Seric **
5982ed72599Seric **	Parameters:
5992ed72599Seric **		none.
6002ed72599Seric **
6012ed72599Seric **	Returns:
6022ed72599Seric **		pid of child in parent.
6032ed72599Seric **		zero in child.
6042ed72599Seric **		-1 on error.
6052ed72599Seric **
6062ed72599Seric **	Side Effects:
6072ed72599Seric **		returns twice, once in parent and once in child.
6082ed72599Seric */
6092ed72599Seric 
6102ed72599Seric dofork()
6112ed72599Seric {
6122ed72599Seric 	register int pid;
6132ed72599Seric 
6142ed72599Seric 	DOFORK(fork);
6152ed72599Seric 	return (pid);
6162ed72599Seric }
6172ed72599Seric /*
618c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
619c579ef51Seric **
620c579ef51Seric **	We should never get fatal errors (e.g., segmentation
621c579ef51Seric **	violation), so we report those specially.  For other
622c579ef51Seric **	errors, we choose a status message (into statmsg),
623c579ef51Seric **	and if it represents an error, we print it.
624c579ef51Seric **
625c579ef51Seric **	Parameters:
626c579ef51Seric **		pid -- pid of mailer.
627c579ef51Seric **		name -- name of mailer (for error messages).
628c579ef51Seric **
629c579ef51Seric **	Returns:
630c579ef51Seric **		exit code of mailer.
631c579ef51Seric **
632c579ef51Seric **	Side Effects:
633c579ef51Seric **		none.
634c579ef51Seric */
635c579ef51Seric 
63675889e88Seric endmailer(mci, name)
637b31e7f2bSeric 	register MCI *mci;
638c579ef51Seric 	char *name;
639c579ef51Seric {
640588cad61Seric 	int st;
641c579ef51Seric 
64275889e88Seric 	/* close any connections */
64375889e88Seric 	if (mci->mci_in != NULL)
64475889e88Seric 		(void) fclose(mci->mci_in);
64575889e88Seric 	if (mci->mci_out != NULL)
64675889e88Seric 		(void) fclose(mci->mci_out);
64775889e88Seric 	mci->mci_in = mci->mci_out = NULL;
64875889e88Seric 	mci->mci_state = MCIS_CLOSED;
64975889e88Seric 
65033db8731Seric 	/* in the IPC case there is nothing to wait for */
65175889e88Seric 	if (mci->mci_pid == 0)
65233db8731Seric 		return (EX_OK);
65333db8731Seric 
65433db8731Seric 	/* wait for the mailer process to die and collect status */
65575889e88Seric 	st = waitfor(mci->mci_pid);
656588cad61Seric 	if (st == -1)
65778de67c1Seric 	{
658588cad61Seric 		syserr("endmailer %s: wait", name);
659588cad61Seric 		return (EX_SOFTWARE);
660c579ef51Seric 	}
66133db8731Seric 
66233db8731Seric 	/* see if it died a horrid death */
663c579ef51Seric 	if ((st & 0377) != 0)
664c579ef51Seric 	{
6655f73204aSeric 		syserr("mailer %s died with signal %o", name, st);
6665f73204aSeric 		ExitStat = EX_TEMPFAIL;
6675f73204aSeric 		return (EX_TEMPFAIL);
668c579ef51Seric 	}
66933db8731Seric 
67033db8731Seric 	/* normal death -- return status */
671588cad61Seric 	st = (st >> 8) & 0377;
672588cad61Seric 	return (st);
673c579ef51Seric }
674c579ef51Seric /*
675c579ef51Seric **  OPENMAILER -- open connection to mailer.
676c579ef51Seric **
677c579ef51Seric **	Parameters:
678c579ef51Seric **		m -- mailer descriptor.
679c579ef51Seric **		pvp -- parameter vector to pass to mailer.
680c579ef51Seric **		ctladdr -- controlling address for user.
681c579ef51Seric **		clever -- create a full duplex connection.
682c579ef51Seric **
683c579ef51Seric **	Returns:
68475889e88Seric **		The mail connection info struct for this connection.
68575889e88Seric **		NULL on failure.
686c579ef51Seric **
687c579ef51Seric **	Side Effects:
688c579ef51Seric **		creates a mailer in a subprocess.
689c579ef51Seric */
690c579ef51Seric 
691b31e7f2bSeric MCI *
692b31e7f2bSeric openmailer(m, pvp, ctladdr, clever, e)
693588cad61Seric 	MAILER *m;
694c579ef51Seric 	char **pvp;
695c579ef51Seric 	ADDRESS *ctladdr;
696c579ef51Seric 	bool clever;
697b31e7f2bSeric 	ENVELOPE *e;
698c579ef51Seric {
6995dfc646bSeric 	int pid;
700b31e7f2bSeric 	register MCI *mci;
701f8952a83Seric 	int mpvect[2];
702c579ef51Seric 	int rpvect[2];
7035dfc646bSeric 	extern FILE *fdopen();
7045dfc646bSeric 
7056ef48975Seric 	if (tTd(11, 1))
7065dfc646bSeric 	{
7078c57e552Seric 		printf("openmailer:");
7085dfc646bSeric 		printav(pvp);
7095dfc646bSeric 	}
71035490626Seric 	errno = 0;
7115dfc646bSeric 
712ef66a9d0Seric 	CurHostName = m->m_mailer;
713ef66a9d0Seric 
71433db8731Seric 	/*
71533db8731Seric 	**  Deal with the special case of mail handled through an IPC
71633db8731Seric 	**  connection.
71733db8731Seric 	**	In this case we don't actually fork.  We must be
71833db8731Seric 	**	running SMTP for this to work.  We will return a
71933db8731Seric 	**	zero pid to indicate that we are running IPC.
720e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
72133db8731Seric 	*/
72233db8731Seric 
723e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
724e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
725e7c1bd78Seric 	{
726b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
72775889e88Seric 		mci->mci_in = stdin;
72875889e88Seric 		mci->mci_out = stdout;
72975889e88Seric 		mci->mci_pid = 0;
730b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
73115d084d5Seric 		mci->mci_mailer = m;
732b31e7f2bSeric 		mci->mci_flags = 0;
733e7c1bd78Seric 	}
734b31e7f2bSeric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
735914346b1Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
73633db8731Seric 	{
73784f7cd1bSeric #ifdef DAEMON
7385f73204aSeric 		register STAB *st;
7395f73204aSeric 		extern STAB *stab();
740ebc61751Sbloom 		register int i, j;
7411277f9a8Seric 		register u_short port;
742b31e7f2bSeric 		int nmx;
743b31e7f2bSeric 		char *mxhosts[MAXMXHOSTS + 1];
744b31e7f2bSeric 		extern MCI *mci_get();
74533db8731Seric 
746ef66a9d0Seric 		CurHostName = pvp[1];
747b31e7f2bSeric #ifdef NAMED_BIND
748b31e7f2bSeric 		if (CurHostName != NULL && CurHostName[0] != '\0' &&
749b31e7f2bSeric 		    CurHostName[0] != '[')
750b31e7f2bSeric 		{
751b31e7f2bSeric 			int rcode;
752b31e7f2bSeric 			char buf[MAXNAME];
753b31e7f2bSeric 
754b31e7f2bSeric 			expand("\001j", buf, &buf[sizeof(buf) - 1], e);
755b31e7f2bSeric 			nmx = getmxrr(CurHostName, mxhosts, buf, &rcode);
756b31e7f2bSeric 			if (nmx < 0)
757b31e7f2bSeric 			{
758b31e7f2bSeric 				mci = mci_get(CurHostName, m);
759b31e7f2bSeric 				mci->mci_exitstat = rcode;
760b31e7f2bSeric 				mci->mci_errno = errno;
761b31e7f2bSeric 			}
762b31e7f2bSeric 		}
763b31e7f2bSeric 		else
764b31e7f2bSeric #endif
765b31e7f2bSeric 		{
766b31e7f2bSeric 			nmx = 1;
767b31e7f2bSeric 			mxhosts[0] = CurHostName;
768b31e7f2bSeric 		}
769b31e7f2bSeric 
77033db8731Seric 		if (!clever)
77133db8731Seric 			syserr("non-clever IPC");
77293b6e3cfSeric 		if (pvp[2] != NULL)
7731277f9a8Seric 			port = atoi(pvp[2]);
77493b6e3cfSeric 		else
7751277f9a8Seric 			port = 0;
776b31e7f2bSeric 		for (j = 0; j < nmx; j++)
777ebc61751Sbloom 		{
77845a8316eSeric 			/* see if we already know that this host is fried */
779b31e7f2bSeric 			CurHostName = mxhosts[j];
780b31e7f2bSeric 			mci = mci_get(CurHostName, m);
781b31e7f2bSeric 			if (mci->mci_state != MCIS_CLOSED)
78275889e88Seric 				return mci;
78315d084d5Seric 			mci->mci_mailer = m;
784b31e7f2bSeric 			if (mci->mci_exitstat != EX_OK)
785b31e7f2bSeric 				continue;
786b31e7f2bSeric 
78775889e88Seric 			/* try the connection */
788b31e7f2bSeric 			setproctitle("%s %s: %s", e->e_id, mxhosts[1], "user open");
789914346b1Seric 			message(Arpa_Info, "Connecting to %s (%s)...",
790b31e7f2bSeric 				mxhosts[j], m->m_name);
791b31e7f2bSeric 			i = makeconnection(mxhosts[j], port, mci,
792914346b1Seric 				bitnset(M_SECURE_PORT, m->m_flags));
79375889e88Seric 			mci->mci_exitstat = i;
79475889e88Seric 			mci->mci_errno = errno;
79575889e88Seric 			if (i == EX_OK)
796b31e7f2bSeric 			{
797b31e7f2bSeric 				mci->mci_state = MCIS_OPENING;
798b31e7f2bSeric 				mci_cache(mci);
799b31e7f2bSeric 				break;
800b31e7f2bSeric 			}
801b31e7f2bSeric 			else if (tTd(11, 1))
802b31e7f2bSeric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
803b31e7f2bSeric 					i, errno);
804b31e7f2bSeric 
80575889e88Seric 
8065f73204aSeric 			/* enter status of this host */
80775889e88Seric 			setstat(i);
808ed854c7bSeric 		}
809b31e7f2bSeric #else /* no DAEMON */
810588cad61Seric 		syserr("openmailer: no IPC");
81175889e88Seric 		return NULL;
812b31e7f2bSeric #endif /* DAEMON */
813588cad61Seric 	}
814b31e7f2bSeric 	else
815b31e7f2bSeric 	{
8166328bdf7Seric 		/* create a pipe to shove the mail through */
817f8952a83Seric 		if (pipe(mpvect) < 0)
81825a99e2eSeric 		{
819588cad61Seric 			syserr("openmailer: pipe (to mailer)");
82075889e88Seric 			return NULL;
82125a99e2eSeric 		}
822c579ef51Seric 
823c579ef51Seric 		/* if this mailer speaks smtp, create a return pipe */
824c579ef51Seric 		if (clever && pipe(rpvect) < 0)
825c579ef51Seric 		{
826588cad61Seric 			syserr("openmailer: pipe (from mailer)");
827c579ef51Seric 			(void) close(mpvect[0]);
828c579ef51Seric 			(void) close(mpvect[1]);
82975889e88Seric 			return NULL;
830c579ef51Seric 		}
831c579ef51Seric 
83233db8731Seric 		/*
83333db8731Seric 		**  Actually fork the mailer process.
83433db8731Seric 		**	DOFORK is clever about retrying.
8356984bfddSeric 		**
8366984bfddSeric 		**	Dispose of SIGCHLD signal catchers that may be laying
8376984bfddSeric 		**	around so that endmail will get it.
83833db8731Seric 		*/
83933db8731Seric 
840b31e7f2bSeric 		if (e->e_xfp != NULL)
841b31e7f2bSeric 			(void) fflush(e->e_xfp);		/* for debugging */
842588cad61Seric 		(void) fflush(stdout);
8436984bfddSeric # ifdef SIGCHLD
8446984bfddSeric 		(void) signal(SIGCHLD, SIG_DFL);
8456984bfddSeric # endif SIGCHLD
84684f7cd1bSeric 		DOFORK(FORK);
847f129ec7dSeric 		/* pid is set by DOFORK */
84825a99e2eSeric 		if (pid < 0)
84925a99e2eSeric 		{
85033db8731Seric 			/* failure */
851588cad61Seric 			syserr("openmailer: cannot fork");
852f8952a83Seric 			(void) close(mpvect[0]);
853f8952a83Seric 			(void) close(mpvect[1]);
854c579ef51Seric 			if (clever)
855c579ef51Seric 			{
856c579ef51Seric 				(void) close(rpvect[0]);
857c579ef51Seric 				(void) close(rpvect[1]);
858c579ef51Seric 			}
85975889e88Seric 			return NULL;
86025a99e2eSeric 		}
86125a99e2eSeric 		else if (pid == 0)
86225a99e2eSeric 		{
86313088b9fSeric 			int i;
8645f73204aSeric 			extern int DtableSize;
86513088b9fSeric 
86625a99e2eSeric 			/* child -- set up input & exec mailer */
86703ab8e55Seric 			/* make diagnostic output be standard output */
8688f0e7860Seric 			(void) signal(SIGINT, SIG_IGN);
8698f0e7860Seric 			(void) signal(SIGHUP, SIG_IGN);
8700984da9fSeric 			(void) signal(SIGTERM, SIG_DFL);
871f8952a83Seric 
872b31e7f2bSeric 			/* arrange to filter std & diag output of command */
873c579ef51Seric 			if (clever)
874c579ef51Seric 			{
875c579ef51Seric 				(void) close(rpvect[0]);
876c579ef51Seric 				(void) close(1);
877c579ef51Seric 				(void) dup(rpvect[1]);
878c579ef51Seric 				(void) close(rpvect[1]);
879c579ef51Seric 			}
880276723a8Seric 			else if (OpMode == MD_SMTP || HoldErrs)
881f8952a83Seric 			{
882588cad61Seric 				/* put mailer output in transcript */
883f8952a83Seric 				(void) close(1);
884b31e7f2bSeric 				(void) dup(fileno(e->e_xfp));
885f8952a83Seric 			}
886db8841e9Seric 			(void) close(2);
887db8841e9Seric 			(void) dup(1);
888f8952a83Seric 
889f8952a83Seric 			/* arrange to get standard input */
890f8952a83Seric 			(void) close(mpvect[1]);
891db8841e9Seric 			(void) close(0);
892f8952a83Seric 			if (dup(mpvect[0]) < 0)
89325a99e2eSeric 			{
89425a99e2eSeric 				syserr("Cannot dup to zero!");
895a590b978Seric 				_exit(EX_OSERR);
89625a99e2eSeric 			}
897f8952a83Seric 			(void) close(mpvect[0]);
89857fc6f17Seric 			if (!bitnset(M_RESTR, m->m_flags))
8990984da9fSeric 			{
90053e3fa05Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
901e36b99e2Seric 				{
902e36b99e2Seric 					(void) setgid(DefGid);
903898a126bSbostic 					(void) initgroups(DefUser, DefGid);
904e36b99e2Seric 					(void) setuid(DefUid);
905e36b99e2Seric 				}
906e36b99e2Seric 				else
90769f29479Seric 				{
908e36b99e2Seric 					(void) setgid(ctladdr->q_gid);
909898a126bSbostic 					(void) initgroups(ctladdr->q_ruser?
910898a126bSbostic 						ctladdr->q_ruser: ctladdr->q_user,
911898a126bSbostic 						ctladdr->q_gid);
912e36b99e2Seric 					(void) setuid(ctladdr->q_uid);
91369f29479Seric 				}
9140984da9fSeric 			}
915588cad61Seric 
91613088b9fSeric 			/* arrange for all the files to be closed */
917b31e7f2bSeric 			for (i = 3; i < DtableSize; i++)
918b31e7f2bSeric 			{
919fccb3f7aSbostic 				register int j;
920fccb3f7aSbostic 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
921fccb3f7aSbostic 					(void)fcntl(i, F_SETFD, j|1);
922fccb3f7aSbostic 			}
92333db8731Seric 
92433db8731Seric 			/* try to execute the mailer */
9255df317aaSeric 			execve(m->m_mailer, pvp, UserEnviron);
92613088b9fSeric 			syserr("Cannot exec %s", m->m_mailer);
92795b76e4bSeric 			if (m == LocalMailer)
92855f33c03Seric 				_exit(EX_TEMPFAIL);
92995b76e4bSeric 			switch (errno)
93095b76e4bSeric 			{
93195b76e4bSeric 			  case EIO:
93295b76e4bSeric 			  case EAGAIN:
93395b76e4bSeric 			  case ENOMEM:
93495b76e4bSeric # ifdef EPROCLIM
93595b76e4bSeric 			  case EPROCLIM:
93695b76e4bSeric # endif
93795b76e4bSeric 				_exit(EX_TEMPFAIL);
93895b76e4bSeric 			}
939a590b978Seric 			_exit(EX_UNAVAILABLE);
94025a99e2eSeric 		}
94125a99e2eSeric 
942f8952a83Seric 		/*
943c579ef51Seric 		**  Set up return value.
944f8952a83Seric 		*/
945f8952a83Seric 
946b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
94715d084d5Seric 		mci->mci_mailer = m;
948b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
949f8952a83Seric 		(void) close(mpvect[0]);
95075889e88Seric 		mci->mci_out = fdopen(mpvect[1], "w");
951c579ef51Seric 		if (clever)
95225a99e2eSeric 		{
953c579ef51Seric 			(void) close(rpvect[1]);
95475889e88Seric 			mci->mci_in = fdopen(rpvect[0], "r");
95575889e88Seric 		}
95675889e88Seric 		else
95775889e88Seric 		{
95875889e88Seric 			mci->mci_flags |= MCIF_TEMP;
95975889e88Seric 			mci->mci_in = NULL;
96075889e88Seric 		}
961b31e7f2bSeric 	}
962b31e7f2bSeric 
963b31e7f2bSeric 	/*
964b31e7f2bSeric 	**  If we are in SMTP opening state, send initial protocol.
965b31e7f2bSeric 	*/
966b31e7f2bSeric 
967b31e7f2bSeric 	if (clever && mci->mci_state != MCIS_CLOSED)
968b31e7f2bSeric 	{
969b31e7f2bSeric 		smtpinit(m, mci, e);
970b31e7f2bSeric 	}
971c579ef51Seric 
97275889e88Seric 	return mci;
97325a99e2eSeric }
97425a99e2eSeric /*
97525a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
97625a99e2eSeric **
97725a99e2eSeric **	Parameters:
97825a99e2eSeric **		stat -- the status code from the mailer (high byte
97925a99e2eSeric **			only; core dumps must have been taken care of
98025a99e2eSeric **			already).
98125a99e2eSeric **		m -- the mailer descriptor for this mailer.
98225a99e2eSeric **
98325a99e2eSeric **	Returns:
984db8841e9Seric **		none.
98525a99e2eSeric **
98625a99e2eSeric **	Side Effects:
987c1f9df2cSeric **		Errors may be incremented.
98825a99e2eSeric **		ExitStat may be set.
98925a99e2eSeric */
99025a99e2eSeric 
991198d9be0Seric giveresponse(stat, m, e)
99225a99e2eSeric 	int stat;
993588cad61Seric 	register MAILER *m;
994198d9be0Seric 	ENVELOPE *e;
99525a99e2eSeric {
99625a99e2eSeric 	register char *statmsg;
99725a99e2eSeric 	extern char *SysExMsg[];
99825a99e2eSeric 	register int i;
999d4bd8f0eSbostic 	extern int N_SysEx;
1000d4bd8f0eSbostic #ifdef NAMED_BIND
1001d4bd8f0eSbostic 	extern int h_errno;
1002d4bd8f0eSbostic #endif
1003198d9be0Seric 	char buf[MAXLINE];
100425a99e2eSeric 
10057d1fc79dSeric #ifdef lint
10067d1fc79dSeric 	if (m == NULL)
10077d1fc79dSeric 		return;
10087d1fc79dSeric #endif lint
10097d1fc79dSeric 
101013bbc08cSeric 	/*
101113bbc08cSeric 	**  Compute status message from code.
101213bbc08cSeric 	*/
101313bbc08cSeric 
101425a99e2eSeric 	i = stat - EX__BASE;
1015588cad61Seric 	if (stat == 0)
1016588cad61Seric 		statmsg = "250 Sent";
1017588cad61Seric 	else if (i < 0 || i > N_SysEx)
1018588cad61Seric 	{
1019588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1020588cad61Seric 		stat = EX_UNAVAILABLE;
1021588cad61Seric 		statmsg = buf;
1022588cad61Seric 	}
1023198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1024198d9be0Seric 	{
10258557d168Seric 		(void) strcpy(buf, SysExMsg[i]);
1026d4bd8f0eSbostic #ifdef NAMED_BIND
1027f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1028f28da541Smiriam 		{
1029f28da541Smiriam 			extern char *errstring();
1030f28da541Smiriam 
1031f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1032f28da541Smiriam 		}
1033f28da541Smiriam 		else
1034d4bd8f0eSbostic #endif
1035f28da541Smiriam 		{
10368557d168Seric 			if (errno != 0)
1037198d9be0Seric 			{
103887c9b3e7Seric 				extern char *errstring();
10398557d168Seric 
1040d87e85f3Seric 				statmsg = errstring(errno);
1041d87e85f3Seric 			}
1042d87e85f3Seric 			else
1043d87e85f3Seric 			{
1044d87e85f3Seric #ifdef SMTP
1045d87e85f3Seric 				extern char SmtpError[];
1046d87e85f3Seric 
1047d87e85f3Seric 				statmsg = SmtpError;
1048d87e85f3Seric #else SMTP
1049d87e85f3Seric 				statmsg = NULL;
1050d87e85f3Seric #endif SMTP
1051d87e85f3Seric 			}
1052f28da541Smiriam 		}
1053d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1054d87e85f3Seric 		{
105587c9b3e7Seric 			(void) strcat(buf, ": ");
1056d87e85f3Seric 			(void) strcat(buf, statmsg);
10578557d168Seric 		}
1058198d9be0Seric 		statmsg = buf;
1059198d9be0Seric 	}
106025a99e2eSeric 	else
1061d87e85f3Seric 	{
106225a99e2eSeric 		statmsg = SysExMsg[i];
1063d87e85f3Seric 	}
1064588cad61Seric 
1065588cad61Seric 	/*
1066588cad61Seric 	**  Print the message as appropriate
1067588cad61Seric 	*/
1068588cad61Seric 
1069198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
10705826d9d3Seric 		message(Arpa_Info, &statmsg[4]);
107125a99e2eSeric 	else
107225a99e2eSeric 	{
1073c1f9df2cSeric 		Errors++;
10745826d9d3Seric 		usrerr(statmsg);
107525a99e2eSeric 	}
107625a99e2eSeric 
107725a99e2eSeric 	/*
107825a99e2eSeric 	**  Final cleanup.
107925a99e2eSeric 	**	Log a record of the transaction.  Compute the new
108025a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
108125a99e2eSeric 	**	that.
108225a99e2eSeric 	*/
108325a99e2eSeric 
108461f5a1d4Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
1085b31e7f2bSeric 		logdelivery(&statmsg[4], e);
1086eb238f8cSeric 
1087eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1088eb238f8cSeric 		setstat(stat);
1089198d9be0Seric 	if (stat != EX_OK)
1090198d9be0Seric 	{
1091198d9be0Seric 		if (e->e_message != NULL)
1092198d9be0Seric 			free(e->e_message);
1093198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1094198d9be0Seric 	}
10958557d168Seric 	errno = 0;
1096d4bd8f0eSbostic #ifdef NAMED_BIND
1097f28da541Smiriam 	h_errno = 0;
1098d4bd8f0eSbostic #endif
1099eb238f8cSeric }
1100eb238f8cSeric /*
1101eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1102eb238f8cSeric **
1103eb238f8cSeric **	Parameters:
1104eb238f8cSeric **		stat -- the message to print for the status
1105eb238f8cSeric **
1106eb238f8cSeric **	Returns:
1107eb238f8cSeric **		none
1108eb238f8cSeric **
1109eb238f8cSeric **	Side Effects:
1110eb238f8cSeric **		none
1111eb238f8cSeric */
1112eb238f8cSeric 
1113b31e7f2bSeric logdelivery(stat, e)
1114eb238f8cSeric 	char *stat;
1115b31e7f2bSeric 	register ENVELOPE *e;
11165cf56be3Seric {
11175cf56be3Seric 	extern char *pintvl();
11185cf56be3Seric 
1119eb238f8cSeric # ifdef LOG
1120b31e7f2bSeric 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", e->e_id,
1121b31e7f2bSeric 	       e->e_to, pintvl(curtime() - e->e_ctime, TRUE), stat);
112225a99e2eSeric # endif LOG
112325a99e2eSeric }
112425a99e2eSeric /*
112551552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
112625a99e2eSeric **
112751552439Seric **	This can be made an arbitrary message separator by changing $l
112851552439Seric **
11299b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
11309b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
11319b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
11329b6c17a6Seric **	this kind of antique garbage????
113325a99e2eSeric **
113425a99e2eSeric **	Parameters:
113551552439Seric **		fp -- the file to output to.
113651552439Seric **		m -- the mailer describing this entry.
113725a99e2eSeric **
113825a99e2eSeric **	Returns:
113951552439Seric **		none
114025a99e2eSeric **
114125a99e2eSeric **	Side Effects:
114251552439Seric **		outputs some text to fp.
114325a99e2eSeric */
114425a99e2eSeric 
1145b31e7f2bSeric putfromline(fp, m, e)
114651552439Seric 	register FILE *fp;
114751552439Seric 	register MAILER *m;
1148b31e7f2bSeric 	ENVELOPE *e;
114925a99e2eSeric {
11509b6c17a6Seric 	char *template = "\001l\n";
115151552439Seric 	char buf[MAXLINE];
115225a99e2eSeric 
115357fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
115451552439Seric 		return;
115513bbc08cSeric 
11562c7e1b8dSeric # ifdef UGLYUUCP
115757fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
115874b6e67bSeric 	{
1159ea09d6edSeric 		char *bang;
1160ea09d6edSeric 		char xbuf[MAXLINE];
116174b6e67bSeric 
1162b31e7f2bSeric 		expand("\001<", buf, &buf[sizeof buf - 1], e);
1163ea09d6edSeric 		bang = index(buf, '!');
116474b6e67bSeric 		if (bang == NULL)
1165ea09d6edSeric 			syserr("No ! in UUCP! (%s)", buf);
116674b6e67bSeric 		else
1167588cad61Seric 		{
1168ea09d6edSeric 			*bang++ = '\0';
11699b6c17a6Seric 			(void) sprintf(xbuf, "From %s  \001d remote from %s\n", bang, buf);
1170ea09d6edSeric 			template = xbuf;
117174b6e67bSeric 		}
1172588cad61Seric 	}
11732c7e1b8dSeric # endif UGLYUUCP
1174b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
117577b52738Seric 	putline(buf, fp, m);
1176bc6e2962Seric }
1177bc6e2962Seric /*
117851552439Seric **  PUTBODY -- put the body of a message.
117951552439Seric **
118051552439Seric **	Parameters:
118151552439Seric **		fp -- file to output onto.
118277b52738Seric **		m -- a mailer descriptor to control output format.
11839a6a5f55Seric **		e -- the envelope to put out.
118451552439Seric **
118551552439Seric **	Returns:
118651552439Seric **		none.
118751552439Seric **
118851552439Seric **	Side Effects:
118951552439Seric **		The message is written onto fp.
119051552439Seric */
119151552439Seric 
119277b52738Seric putbody(fp, m, e)
119351552439Seric 	FILE *fp;
1194588cad61Seric 	MAILER *m;
11959a6a5f55Seric 	register ENVELOPE *e;
119651552439Seric {
119777b52738Seric 	char buf[MAXLINE];
119851552439Seric 
119951552439Seric 	/*
120051552439Seric 	**  Output the body of the message
120151552439Seric 	*/
120251552439Seric 
12039a6a5f55Seric 	if (e->e_dfp == NULL)
120451552439Seric 	{
12059a6a5f55Seric 		if (e->e_df != NULL)
12069a6a5f55Seric 		{
12079a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
12089a6a5f55Seric 			if (e->e_dfp == NULL)
12098f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
12108f9146b0Srick 				e->e_df, e->e_to, e->e_from);
12119a6a5f55Seric 		}
12129a6a5f55Seric 		else
121377b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
12149a6a5f55Seric 	}
12159a6a5f55Seric 	if (e->e_dfp != NULL)
12169a6a5f55Seric 	{
12179a6a5f55Seric 		rewind(e->e_dfp);
121877b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
121924fc8aeeSeric 		{
122024fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1221d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
12223462ad9eSeric 				(void) putc('>', fp);
122377b52738Seric 			putline(buf, fp, m);
122424fc8aeeSeric 		}
122551552439Seric 
12269a6a5f55Seric 		if (ferror(e->e_dfp))
122751552439Seric 		{
122851552439Seric 			syserr("putbody: read error");
122951552439Seric 			ExitStat = EX_IOERR;
123051552439Seric 		}
123151552439Seric 	}
123251552439Seric 
123351552439Seric 	(void) fflush(fp);
123451552439Seric 	if (ferror(fp) && errno != EPIPE)
123551552439Seric 	{
123651552439Seric 		syserr("putbody: write error");
123751552439Seric 		ExitStat = EX_IOERR;
123851552439Seric 	}
123951552439Seric 	errno = 0;
124025a99e2eSeric }
124125a99e2eSeric /*
124225a99e2eSeric **  MAILFILE -- Send a message to a file.
124325a99e2eSeric **
1244f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1245f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1246f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1247f129ec7dSeric **	sendmail runs as root.
1248f129ec7dSeric **
1249588cad61Seric **	This could be done as a subordinate mailer, except that it
1250588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1251588cad61Seric **	view this as being sufficiently important as to include it
1252588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1253588cad61Seric **	to create another process plus some pipes to save the message.
1254588cad61Seric **
125525a99e2eSeric **	Parameters:
125625a99e2eSeric **		filename -- the name of the file to send to.
12576259796dSeric **		ctladdr -- the controlling address header -- includes
12586259796dSeric **			the userid/groupid to be when sending.
125925a99e2eSeric **
126025a99e2eSeric **	Returns:
126125a99e2eSeric **		The exit code associated with the operation.
126225a99e2eSeric **
126325a99e2eSeric **	Side Effects:
126425a99e2eSeric **		none.
126525a99e2eSeric */
126625a99e2eSeric 
1267b31e7f2bSeric mailfile(filename, ctladdr, e)
126825a99e2eSeric 	char *filename;
12696259796dSeric 	ADDRESS *ctladdr;
1270b31e7f2bSeric 	register ENVELOPE *e;
127125a99e2eSeric {
127225a99e2eSeric 	register FILE *f;
127332d19d43Seric 	register int pid;
127415d084d5Seric 	int mode;
127525a99e2eSeric 
127632d19d43Seric 	/*
127732d19d43Seric 	**  Fork so we can change permissions here.
127832d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
127932d19d43Seric 	**	the complications of calling subroutines, etc.
128032d19d43Seric 	*/
128132d19d43Seric 
128232d19d43Seric 	DOFORK(fork);
128332d19d43Seric 
128432d19d43Seric 	if (pid < 0)
128532d19d43Seric 		return (EX_OSERR);
128632d19d43Seric 	else if (pid == 0)
128732d19d43Seric 	{
128832d19d43Seric 		/* child -- actually write to file */
1289f129ec7dSeric 		struct stat stb;
1290f129ec7dSeric 
12910984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
12920984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
12930984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
12943462ad9eSeric 		(void) umask(OldUmask);
129595f16dc0Seric 
1296f129ec7dSeric 		if (stat(filename, &stb) < 0)
1297e6e1265fSeric 			stb.st_mode = 0666;
129815d084d5Seric 		mode = stb.st_mode;
129995f16dc0Seric 
130095f16dc0Seric 		/* limit the errors to those actually caused in the child */
130195f16dc0Seric 		errno = 0;
130295f16dc0Seric 		ExitStat = EX_OK;
130395f16dc0Seric 
1304f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1305f129ec7dSeric 			exit(EX_CANTCREAT);
130603827b5fSeric 		if (ctladdr == NULL)
13078f9146b0Srick 			ctladdr = &e->e_from;
130815d084d5Seric 		else
130915d084d5Seric 		{
131015d084d5Seric 			/* ignore setuid and setgid bits */
131115d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
131215d084d5Seric 		}
131315d084d5Seric 
13148f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
13158f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
13168f9146b0Srick 		{
13178f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
131895f16dc0Seric 			if (e->e_dfp == NULL)
131995f16dc0Seric 			{
13208f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
13218f9146b0Srick 					e->e_df, e->e_to, e->e_from);
13228f9146b0Srick 			}
13238f9146b0Srick 		}
13248f9146b0Srick 
132515d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1326e36b99e2Seric 		{
132795f16dc0Seric 			if (ctladdr->q_uid == 0)
132895f16dc0Seric 			{
1329e36b99e2Seric 				(void) setgid(DefGid);
1330898a126bSbostic 				(void) initgroups(DefUser, DefGid);
133195f16dc0Seric 			}
133295f16dc0Seric 			else
133395f16dc0Seric 			{
13346259796dSeric 				(void) setgid(ctladdr->q_gid);
1335898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1336898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1337898a126bSbostic 					ctladdr->q_gid);
1338898a126bSbostic 			}
1339e36b99e2Seric 		}
134015d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1341e36b99e2Seric 		{
1342e36b99e2Seric 			if (ctladdr->q_uid == 0)
1343e36b99e2Seric 				(void) setuid(DefUid);
1344e36b99e2Seric 			else
13456259796dSeric 				(void) setuid(ctladdr->q_uid);
1346e36b99e2Seric 		}
134795f16dc0Seric 		FileName = filename;
134895f16dc0Seric 		LineNumber = 0;
134927628d59Seric 		f = dfopen(filename, "a");
135025a99e2eSeric 		if (f == NULL)
135195f16dc0Seric 		{
135295f16dc0Seric 			message("cannot open");
135332d19d43Seric 			exit(EX_CANTCREAT);
135495f16dc0Seric 		}
135525a99e2eSeric 
1356b31e7f2bSeric 		putfromline(f, ProgMailer, e);
135777b52738Seric 		(*CurEnv->e_puthdr)(f, ProgMailer, CurEnv);
135877b52738Seric 		putline("\n", f, ProgMailer);
135977b52738Seric 		(*CurEnv->e_putbody)(f, ProgMailer, CurEnv);
136077b52738Seric 		putline("\n", f, ProgMailer);
136195f16dc0Seric 		if (ferror(f))
136295f16dc0Seric 		{
136395f16dc0Seric 			message("I/O error");
136495f16dc0Seric 			setstat(EX_IOERR);
136595f16dc0Seric 		}
1366db8841e9Seric 		(void) fclose(f);
136732d19d43Seric 		(void) fflush(stdout);
1368e36b99e2Seric 
136927628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1370c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
137195f16dc0Seric 		exit(ExitStat);
137213bbc08cSeric 		/*NOTREACHED*/
137332d19d43Seric 	}
137432d19d43Seric 	else
137532d19d43Seric 	{
137632d19d43Seric 		/* parent -- wait for exit status */
1377588cad61Seric 		int st;
137832d19d43Seric 
1379588cad61Seric 		st = waitfor(pid);
1380588cad61Seric 		if ((st & 0377) != 0)
1381588cad61Seric 			return (EX_UNAVAILABLE);
1382588cad61Seric 		else
1383588cad61Seric 			return ((st >> 8) & 0377);
13848f9146b0Srick 		/*NOTREACHED*/
138532d19d43Seric 	}
138625a99e2eSeric }
1387ea4dc939Seric /*
1388ea4dc939Seric **  SENDALL -- actually send all the messages.
1389ea4dc939Seric **
1390ea4dc939Seric **	Parameters:
13910c52a0b3Seric **		e -- the envelope to send.
13927b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
13937b95031aSeric **			the current SendMode.
1394ea4dc939Seric **
1395ea4dc939Seric **	Returns:
1396ea4dc939Seric **		none.
1397ea4dc939Seric **
1398ea4dc939Seric **	Side Effects:
1399ea4dc939Seric **		Scans the send lists and sends everything it finds.
14000c52a0b3Seric **		Delivers any appropriate error messages.
1401276723a8Seric **		If we are running in a non-interactive mode, takes the
1402276723a8Seric **			appropriate action.
1403ea4dc939Seric */
1404ea4dc939Seric 
1405276723a8Seric sendall(e, mode)
14060c52a0b3Seric 	ENVELOPE *e;
1407276723a8Seric 	char mode;
1408ea4dc939Seric {
1409e77e673fSeric 	register ADDRESS *q;
141014a8ed7aSeric 	bool oldverbose;
1411276723a8Seric 	int pid;
1412dde5acadSeric 	int nsent;
14131c265a00Seric # ifdef LOCKF
14141c265a00Seric 	struct flock lfd;
14151c265a00Seric # endif
1416ea4dc939Seric 
14177b95031aSeric 	/* determine actual delivery mode */
14187b95031aSeric 	if (mode == SM_DEFAULT)
14197b95031aSeric 	{
14205f73204aSeric 		extern bool shouldqueue();
14217b95031aSeric 
14225f73204aSeric 		if (shouldqueue(e->e_msgpriority))
14237b95031aSeric 			mode = SM_QUEUE;
14247b95031aSeric 		else
14257b95031aSeric 			mode = SendMode;
14267b95031aSeric 	}
14277b95031aSeric 
1428df864a8fSeric 	if (tTd(13, 1))
1429772e6e50Seric 	{
1430276723a8Seric 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
14310c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1432772e6e50Seric 	}
1433ea4dc939Seric 
14340c52a0b3Seric 	/*
1435276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1436588cad61Seric 	**	Check to make sure the hop count is reasonable.
1437588cad61Seric 	**	Delete sends to the sender in mailing lists.
1438276723a8Seric 	*/
1439276723a8Seric 
1440588cad61Seric 	CurEnv = e;
1441276723a8Seric 
14421d57450bSeric 	if (e->e_hopcount > MaxHopCount)
1443276723a8Seric 	{
14448f9146b0Srick 		errno = 0;
14458f9146b0Srick 		syserr("sendall: too many hops %d (%d max): from %s, to %s",
14461d57450bSeric 			e->e_hopcount, MaxHopCount, e->e_from, e->e_to);
1447588cad61Seric 		return;
1448588cad61Seric 	}
1449588cad61Seric 
1450588cad61Seric 	if (!MeToo)
1451276723a8Seric 	{
1452f3d6c55cSeric 		extern ADDRESS *recipient();
1453f3d6c55cSeric 
1454588cad61Seric 		e->e_from.q_flags |= QDONTSEND;
1455f3d6c55cSeric 		(void) recipient(&e->e_from, &e->e_sendqueue);
1456276723a8Seric 	}
1457588cad61Seric 
1458588cad61Seric # ifdef QUEUE
1459b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1460b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1461b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
1462e020b127Seric 		queueup(e, TRUE, mode == SM_QUEUE);
1463276723a8Seric #endif QUEUE
1464276723a8Seric 
1465276723a8Seric 	oldverbose = Verbose;
1466276723a8Seric 	switch (mode)
1467276723a8Seric 	{
1468276723a8Seric 	  case SM_VERIFY:
1469276723a8Seric 		Verbose = TRUE;
1470276723a8Seric 		break;
1471276723a8Seric 
1472276723a8Seric 	  case SM_QUEUE:
1473c7f5410dSeric   queueonly:
1474b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1475276723a8Seric 		return;
1476276723a8Seric 
1477276723a8Seric 	  case SM_FORK:
14789a6a5f55Seric 		if (e->e_xfp != NULL)
14799a6a5f55Seric 			(void) fflush(e->e_xfp);
1480c7f5410dSeric 
1481c7f5410dSeric # ifdef LOCKF
1482c7f5410dSeric 		/*
1483c7f5410dSeric 		**  Since lockf has the interesting semantic that the
14841c265a00Seric 		**  lock is lost when we fork, we have to risk losing
14851c265a00Seric 		**  the lock here by closing before the fork, and then
14861c265a00Seric 		**  trying to get it back in the child.
1487c7f5410dSeric 		*/
1488c7f5410dSeric 
1489e020b127Seric 		if (e->e_lockfp != NULL)
1490c7f5410dSeric 		{
1491e020b127Seric 			(void) fclose(e->e_lockfp);
1492e020b127Seric 			e->e_lockfp = NULL;
1493c7f5410dSeric 		}
1494c7f5410dSeric # endif /* LOCKF */
1495c7f5410dSeric 
1496276723a8Seric 		pid = fork();
1497276723a8Seric 		if (pid < 0)
1498276723a8Seric 		{
1499c7f5410dSeric 			goto queueonly;
1500276723a8Seric 		}
1501276723a8Seric 		else if (pid > 0)
1502a6fce3d8Seric 		{
1503a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1504b254bcb6Seric 			e->e_id = e->e_df = NULL;
1505c7f5410dSeric # ifndef LOCKF
1506e020b127Seric 			if (e->e_lockfp != NULL)
1507e020b127Seric 				(void) fclose(e->e_lockfp);
1508c7f5410dSeric # endif
1509276723a8Seric 			return;
1510a6fce3d8Seric 		}
1511276723a8Seric 
1512276723a8Seric 		/* double fork to avoid zombies */
1513276723a8Seric 		if (fork() > 0)
1514276723a8Seric 			exit(EX_OK);
1515276723a8Seric 
1516a6fce3d8Seric 		/* be sure we are immune from the terminal */
1517769e215aSeric 		disconnect(FALSE);
1518a6fce3d8Seric 
1519e6720d51Seric # ifdef LOCKF
1520e6720d51Seric 		/*
1521c7f5410dSeric 		**  Now try to get our lock back.
1522e6720d51Seric 		*/
1523e6720d51Seric 
15241c265a00Seric 		lfd.l_type = F_WRLCK;
15251c265a00Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1526e020b127Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1527e020b127Seric 		if (e->e_lockfp == NULL ||
15281c265a00Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1529e6720d51Seric 		{
1530e6720d51Seric 			/* oops....  lost it */
1531e6720d51Seric # ifdef LOG
1532e6720d51Seric 			if (LogLevel > 5)
1533c7f5410dSeric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1534b31e7f2bSeric 					e->e_id);
1535e6720d51Seric # endif /* LOG */
1536e6720d51Seric 			exit(EX_OK);
1537e6720d51Seric 		}
1538e6720d51Seric # endif /* LOCKF */
1539e6720d51Seric 
1540276723a8Seric 		break;
1541276723a8Seric 	}
1542276723a8Seric 
1543276723a8Seric 	/*
15440c52a0b3Seric 	**  Run through the list and send everything.
15450c52a0b3Seric 	*/
15460c52a0b3Seric 
1547dde5acadSeric 	nsent = 0;
15480c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1549ea4dc939Seric 	{
1550276723a8Seric 		if (mode == SM_VERIFY)
1551ea4dc939Seric 		{
1552a6fce3d8Seric 			e->e_to = q->q_paddr;
1553e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1554ea4dc939Seric 				message(Arpa_Info, "deliverable");
1555ea4dc939Seric 		}
1556dde5acadSeric 		else if (!bitset(QDONTSEND, q->q_flags))
1557dde5acadSeric 		{
1558de1179f5Seric # ifdef QUEUE
1559dde5acadSeric 			/*
1560dde5acadSeric 			**  Checkpoint the send list every few addresses
1561dde5acadSeric 			*/
1562dde5acadSeric 
1563dde5acadSeric 			if (nsent >= CheckpointInterval)
1564dde5acadSeric 			{
1565e020b127Seric 				queueup(e, TRUE, FALSE);
1566dde5acadSeric 				nsent = 0;
1567dde5acadSeric 			}
1568de1179f5Seric # endif /* QUEUE */
1569dde5acadSeric 			if (deliver(e, q) == EX_OK)
1570dde5acadSeric 				nsent++;
1571dde5acadSeric 		}
1572ea4dc939Seric 	}
157314a8ed7aSeric 	Verbose = oldverbose;
15740c52a0b3Seric 
15750c52a0b3Seric 	/*
15760c52a0b3Seric 	**  Now run through and check for errors.
15770c52a0b3Seric 	*/
15780c52a0b3Seric 
1579e020b127Seric 	if (mode == SM_VERIFY)
15800c52a0b3Seric 		return;
15810c52a0b3Seric 
15820c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15830c52a0b3Seric 	{
15840c52a0b3Seric 		register ADDRESS *qq;
15850c52a0b3Seric 
1586df864a8fSeric 		if (tTd(13, 3))
1587df864a8fSeric 		{
1588df864a8fSeric 			printf("Checking ");
1589df864a8fSeric 			printaddr(q, FALSE);
1590df864a8fSeric 		}
1591df864a8fSeric 
1592b254bcb6Seric 		/* only send errors if the message failed */
1593b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1594b254bcb6Seric 			continue;
15950c52a0b3Seric 
15960c52a0b3Seric 		/* we have an address that failed -- find the parent */
15970c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
15980c52a0b3Seric 		{
15990c52a0b3Seric 			char obuf[MAXNAME + 6];
16000c52a0b3Seric 			extern char *aliaslookup();
16010c52a0b3Seric 
16020c52a0b3Seric 			/* we can only have owners for local addresses */
160357fc6f17Seric 			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
16040c52a0b3Seric 				continue;
16050c52a0b3Seric 
16060c52a0b3Seric 			/* see if the owner list exists */
16070c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1608cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1609cec031e3Seric 				(void) strcat(obuf, "owner");
1610cec031e3Seric 			else
16110c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
1612ef137175Sbostic 			makelower(obuf);
16130c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
16140c52a0b3Seric 				continue;
16150c52a0b3Seric 
1616df864a8fSeric 			if (tTd(13, 4))
1617df864a8fSeric 				printf("Errors to %s\n", obuf);
1618df864a8fSeric 
16190c52a0b3Seric 			/* owner list exists -- add it to the error queue */
1620e3e4ed86Seric 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue);
162153e3fa05Seric 			ErrorMode = EM_MAIL;
16220c52a0b3Seric 			break;
16230c52a0b3Seric 		}
16240c52a0b3Seric 
16250c52a0b3Seric 		/* if we did not find an owner, send to the sender */
16267455aa0bSeric 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1627e3e4ed86Seric 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue);
16280c52a0b3Seric 	}
1629276723a8Seric 
1630276723a8Seric 	if (mode == SM_FORK)
1631276723a8Seric 		finis();
16320c52a0b3Seric }
1633