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*2a6bc25bSeric static char sccsid[] = "@(#)deliver.c	5.62 (Berkeley) 07/13/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 */
132b843d759Seric 	(void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, TRUE, e));
133c23ed322Seric 	if (e->e_returnpath == e->e_sender)
134c23ed322Seric 	{
135c23ed322Seric 		from = rpathbuf;
136c23ed322Seric 	}
137c23ed322Seric 	else
138c23ed322Seric 	{
139b843d759Seric 		(void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, TRUE, e));
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 		}
257b843d759Seric 		if (!checkcompat(to, e))
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 		{
2701d8f1806Seric 			stripquotes(user);
2711d8f1806Seric 			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;
393*2a6bc25bSeric 		errno = mci->mci_errno;
394b31e7f2bSeric 		if (rcode == EX_OK)
395b31e7f2bSeric 		{
396b31e7f2bSeric 			/* shouldn't happen */
397b31e7f2bSeric 			rcode = EX_SOFTWARE;
398b31e7f2bSeric 		}
399b31e7f2bSeric 	}
400b31e7f2bSeric 	else if (!clever)
401b31e7f2bSeric 	{
402b31e7f2bSeric 		/*
403b31e7f2bSeric 		**  Format and send message.
404b31e7f2bSeric 		*/
40515d084d5Seric 
406b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
407b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
408b31e7f2bSeric 		putline("\n", mci->mci_out, m);
409b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
410b31e7f2bSeric 
411b31e7f2bSeric 		/* get the exit status */
412b31e7f2bSeric 		rcode = endmailer(mci, pv[0]);
413134746fbSeric 	}
414134746fbSeric 	else
415b31e7f2bSeric #ifdef SMTP
416134746fbSeric 	{
417b31e7f2bSeric 		/*
418b31e7f2bSeric 		**  Send the MAIL FROM: protocol
419b31e7f2bSeric 		*/
42015d084d5Seric 
421b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
422b31e7f2bSeric 		if (rcode == EX_OK)
42375889e88Seric 		{
424ded0d3daSkarels 			register char *t = tobuf;
425ded0d3daSkarels 			register int i;
426ded0d3daSkarels 
427588cad61Seric 			/* send the recipient list */
42863780dbdSeric 			tobuf[0] = '\0';
42975889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
43075889e88Seric 			{
43163780dbdSeric 				e->e_to = to->q_paddr;
43215d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
43375889e88Seric 				{
43483b7ddc9Seric 					markfailure(e, to, i);
435198d9be0Seric 					giveresponse(i, m, e);
43663780dbdSeric 				}
43775889e88Seric 				else
43875889e88Seric 				{
439911693bfSbostic 					*t++ = ',';
440b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
441b31e7f2bSeric 						continue;
442588cad61Seric 				}
443588cad61Seric 			}
444588cad61Seric 
44563780dbdSeric 			/* now send the data */
44663780dbdSeric 			if (tobuf[0] == '\0')
447b31e7f2bSeric 			{
44863780dbdSeric 				e->e_to = NULL;
449b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
450b31e7f2bSeric 					smtprset(m, mci, e);
451b31e7f2bSeric 			}
45275889e88Seric 			else
45375889e88Seric 			{
45463780dbdSeric 				e->e_to = tobuf + 1;
45575889e88Seric 				rcode = smtpdata(m, mci, e);
45663780dbdSeric 			}
45763780dbdSeric 
45863780dbdSeric 			/* now close the connection */
459b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
46015d084d5Seric 				smtpquit(m, mci, e);
46163780dbdSeric 		}
462c579ef51Seric 	}
463b31e7f2bSeric #else /* not SMTP */
464a05b3449Sbostic 	{
465b31e7f2bSeric 		syserr("deliver: need SMTP compiled to use clever mailer");
466b31e7f2bSeric 		rcode = -1;
467b31e7f2bSeric 		goto give_up;
468a05b3449Sbostic 	}
469b31e7f2bSeric #endif /* SMTP */
470134746fbSeric #ifdef NAMED_BIND
4712bcc6d2dSeric 	if (ConfigLevel < 2)
472912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
473134746fbSeric #endif
4745dfc646bSeric 
475b31e7f2bSeric 	/* arrange a return receipt if requested */
476b31e7f2bSeric 	if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags))
477b31e7f2bSeric 	{
478b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
479b31e7f2bSeric 		/* do we want to send back more info? */
480b31e7f2bSeric 	}
481b31e7f2bSeric 
482c77d1c25Seric 	/*
48363780dbdSeric 	**  Do final status disposal.
48463780dbdSeric 	**	We check for something in tobuf for the SMTP case.
485c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
486c77d1c25Seric 	**		addressees.
487c77d1c25Seric 	*/
488c77d1c25Seric 
489b31e7f2bSeric   give_up:
49063780dbdSeric 	if (tobuf[0] != '\0')
491198d9be0Seric 		giveresponse(rcode, m, e);
492772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
493b31e7f2bSeric 	{
494dde5acadSeric 		if (rcode != EX_OK)
49583b7ddc9Seric 			markfailure(e, to, rcode);
496dde5acadSeric 		else
497dde5acadSeric 			to->q_flags |= QSENT;
498b31e7f2bSeric 	}
499b31e7f2bSeric 
500b31e7f2bSeric 	/*
501b31e7f2bSeric 	**  Restore state and return.
502b31e7f2bSeric 	*/
503c77d1c25Seric 
50435490626Seric 	errno = 0;
505588cad61Seric 	define('g', (char *) NULL, e);
506c23ed322Seric 	define('<', (char *) NULL, e);
5075826d9d3Seric 	return (rcode);
50825a99e2eSeric }
5095dfc646bSeric /*
51083b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
51183b7ddc9Seric **
51283b7ddc9Seric **	Parameters:
51383b7ddc9Seric **		e -- the envelope we are sending.
51483b7ddc9Seric **		q -- the address to mark.
51583b7ddc9Seric **		rcode -- the code signifying the particular failure.
51683b7ddc9Seric **
51783b7ddc9Seric **	Returns:
51883b7ddc9Seric **		none.
51983b7ddc9Seric **
52083b7ddc9Seric **	Side Effects:
52183b7ddc9Seric **		marks the address (and possibly the envelope) with the
52283b7ddc9Seric **			failure so that an error will be returned or
52383b7ddc9Seric **			the message will be queued, as appropriate.
52483b7ddc9Seric */
52583b7ddc9Seric 
52683b7ddc9Seric markfailure(e, q, rcode)
52783b7ddc9Seric 	register ENVELOPE *e;
52883b7ddc9Seric 	register ADDRESS *q;
52983b7ddc9Seric 	int rcode;
53083b7ddc9Seric {
53183b7ddc9Seric 	if (rcode == EX_OK)
53283b7ddc9Seric 		return;
533ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
53483b7ddc9Seric 		q->q_flags |= QBADADDR;
53583b7ddc9Seric 	else if (curtime() > e->e_ctime + TimeOut)
53683b7ddc9Seric 	{
53783b7ddc9Seric 		extern char *pintvl();
538198d9be0Seric 		char buf[MAXLINE];
53983b7ddc9Seric 
54083b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
541198d9be0Seric 		{
542198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
54383b7ddc9Seric 				pintvl(TimeOut, FALSE));
544198d9be0Seric 			if (e->e_message != NULL)
545198d9be0Seric 				free(e->e_message);
546198d9be0Seric 			e->e_message = newstr(buf);
547198d9be0Seric 			message(Arpa_Info, buf);
548198d9be0Seric 		}
54983b7ddc9Seric 		q->q_flags |= QBADADDR;
55083b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
55183b7ddc9Seric 	}
55283b7ddc9Seric 	else
55383b7ddc9Seric 		q->q_flags |= QQUEUEUP;
55483b7ddc9Seric }
55583b7ddc9Seric /*
55632d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
55732d19d43Seric **
55832d19d43Seric **	This MUST be a macro, since after a vfork we are running
55932d19d43Seric **	two processes on the same stack!!!
56032d19d43Seric **
56132d19d43Seric **	Parameters:
56232d19d43Seric **		none.
56332d19d43Seric **
56432d19d43Seric **	Returns:
56532d19d43Seric **		From a macro???  You've got to be kidding!
56632d19d43Seric **
56732d19d43Seric **	Side Effects:
56832d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
56932d19d43Seric **			pid of child in parent, zero in child.
57032d19d43Seric **			-1 on unrecoverable error.
57132d19d43Seric **
57232d19d43Seric **	Notes:
57332d19d43Seric **		I'm awfully sorry this looks so awful.  That's
57432d19d43Seric **		vfork for you.....
57532d19d43Seric */
57632d19d43Seric 
57732d19d43Seric # define NFORKTRIES	5
57884f7cd1bSeric 
57984f7cd1bSeric # ifndef FORK
58084f7cd1bSeric # define FORK	fork
58184f7cd1bSeric # endif
58232d19d43Seric 
58332d19d43Seric # define DOFORK(fORKfN) \
58432d19d43Seric {\
58532d19d43Seric 	register int i;\
58632d19d43Seric \
58711799049Seric 	for (i = NFORKTRIES; --i >= 0; )\
58832d19d43Seric 	{\
58932d19d43Seric 		pid = fORKfN();\
59032d19d43Seric 		if (pid >= 0)\
59132d19d43Seric 			break;\
59211799049Seric 		if (i > 0)\
5936c4635f6Seric 			sleep((unsigned) NFORKTRIES - i);\
59432d19d43Seric 	}\
59532d19d43Seric }
59632d19d43Seric /*
5972ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
5982ed72599Seric **
5992ed72599Seric **	Parameters:
6002ed72599Seric **		none.
6012ed72599Seric **
6022ed72599Seric **	Returns:
6032ed72599Seric **		pid of child in parent.
6042ed72599Seric **		zero in child.
6052ed72599Seric **		-1 on error.
6062ed72599Seric **
6072ed72599Seric **	Side Effects:
6082ed72599Seric **		returns twice, once in parent and once in child.
6092ed72599Seric */
6102ed72599Seric 
6112ed72599Seric dofork()
6122ed72599Seric {
6132ed72599Seric 	register int pid;
6142ed72599Seric 
6152ed72599Seric 	DOFORK(fork);
6162ed72599Seric 	return (pid);
6172ed72599Seric }
6182ed72599Seric /*
619c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
620c579ef51Seric **
621c579ef51Seric **	We should never get fatal errors (e.g., segmentation
622c579ef51Seric **	violation), so we report those specially.  For other
623c579ef51Seric **	errors, we choose a status message (into statmsg),
624c579ef51Seric **	and if it represents an error, we print it.
625c579ef51Seric **
626c579ef51Seric **	Parameters:
627c579ef51Seric **		pid -- pid of mailer.
628c579ef51Seric **		name -- name of mailer (for error messages).
629c579ef51Seric **
630c579ef51Seric **	Returns:
631c579ef51Seric **		exit code of mailer.
632c579ef51Seric **
633c579ef51Seric **	Side Effects:
634c579ef51Seric **		none.
635c579ef51Seric */
636c579ef51Seric 
63775889e88Seric endmailer(mci, name)
638b31e7f2bSeric 	register MCI *mci;
639c579ef51Seric 	char *name;
640c579ef51Seric {
641588cad61Seric 	int st;
642c579ef51Seric 
64375889e88Seric 	/* close any connections */
64475889e88Seric 	if (mci->mci_in != NULL)
64575889e88Seric 		(void) fclose(mci->mci_in);
64675889e88Seric 	if (mci->mci_out != NULL)
64775889e88Seric 		(void) fclose(mci->mci_out);
64875889e88Seric 	mci->mci_in = mci->mci_out = NULL;
64975889e88Seric 	mci->mci_state = MCIS_CLOSED;
65075889e88Seric 
65133db8731Seric 	/* in the IPC case there is nothing to wait for */
65275889e88Seric 	if (mci->mci_pid == 0)
65333db8731Seric 		return (EX_OK);
65433db8731Seric 
65533db8731Seric 	/* wait for the mailer process to die and collect status */
65675889e88Seric 	st = waitfor(mci->mci_pid);
657588cad61Seric 	if (st == -1)
65878de67c1Seric 	{
659588cad61Seric 		syserr("endmailer %s: wait", name);
660588cad61Seric 		return (EX_SOFTWARE);
661c579ef51Seric 	}
66233db8731Seric 
66333db8731Seric 	/* see if it died a horrid death */
664c579ef51Seric 	if ((st & 0377) != 0)
665c579ef51Seric 	{
6665f73204aSeric 		syserr("mailer %s died with signal %o", name, st);
6675f73204aSeric 		ExitStat = EX_TEMPFAIL;
6685f73204aSeric 		return (EX_TEMPFAIL);
669c579ef51Seric 	}
67033db8731Seric 
67133db8731Seric 	/* normal death -- return status */
672588cad61Seric 	st = (st >> 8) & 0377;
673588cad61Seric 	return (st);
674c579ef51Seric }
675c579ef51Seric /*
676c579ef51Seric **  OPENMAILER -- open connection to mailer.
677c579ef51Seric **
678c579ef51Seric **	Parameters:
679c579ef51Seric **		m -- mailer descriptor.
680c579ef51Seric **		pvp -- parameter vector to pass to mailer.
681c579ef51Seric **		ctladdr -- controlling address for user.
682c579ef51Seric **		clever -- create a full duplex connection.
683c579ef51Seric **
684c579ef51Seric **	Returns:
68575889e88Seric **		The mail connection info struct for this connection.
68675889e88Seric **		NULL on failure.
687c579ef51Seric **
688c579ef51Seric **	Side Effects:
689c579ef51Seric **		creates a mailer in a subprocess.
690c579ef51Seric */
691c579ef51Seric 
692b31e7f2bSeric MCI *
693b31e7f2bSeric openmailer(m, pvp, ctladdr, clever, e)
694588cad61Seric 	MAILER *m;
695c579ef51Seric 	char **pvp;
696c579ef51Seric 	ADDRESS *ctladdr;
697c579ef51Seric 	bool clever;
698b31e7f2bSeric 	ENVELOPE *e;
699c579ef51Seric {
7005dfc646bSeric 	int pid;
701b31e7f2bSeric 	register MCI *mci;
702f8952a83Seric 	int mpvect[2];
703c579ef51Seric 	int rpvect[2];
7045dfc646bSeric 	extern FILE *fdopen();
7055dfc646bSeric 
7066ef48975Seric 	if (tTd(11, 1))
7075dfc646bSeric 	{
7088c57e552Seric 		printf("openmailer:");
7095dfc646bSeric 		printav(pvp);
7105dfc646bSeric 	}
71135490626Seric 	errno = 0;
7125dfc646bSeric 
713ef66a9d0Seric 	CurHostName = m->m_mailer;
714ef66a9d0Seric 
71533db8731Seric 	/*
71633db8731Seric 	**  Deal with the special case of mail handled through an IPC
71733db8731Seric 	**  connection.
71833db8731Seric 	**	In this case we don't actually fork.  We must be
71933db8731Seric 	**	running SMTP for this to work.  We will return a
72033db8731Seric 	**	zero pid to indicate that we are running IPC.
721e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
72233db8731Seric 	*/
72333db8731Seric 
724e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
725e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
726e7c1bd78Seric 	{
727b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
7282a087e90Seric 		bzero((char *) mci, sizeof *mci);
72975889e88Seric 		mci->mci_in = stdin;
73075889e88Seric 		mci->mci_out = stdout;
731b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
73215d084d5Seric 		mci->mci_mailer = m;
733e7c1bd78Seric 	}
734b31e7f2bSeric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
735914346b1Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
73633db8731Seric 	{
73784f7cd1bSeric #ifdef DAEMON
738ebc61751Sbloom 		register int i, j;
7391277f9a8Seric 		register u_short port;
740b31e7f2bSeric 		int nmx;
741b31e7f2bSeric 		char *mxhosts[MAXMXHOSTS + 1];
742b31e7f2bSeric 		extern MCI *mci_get();
74333db8731Seric 
744ef66a9d0Seric 		CurHostName = pvp[1];
745b31e7f2bSeric #ifdef NAMED_BIND
746b31e7f2bSeric 		if (CurHostName != NULL && CurHostName[0] != '\0' &&
747b31e7f2bSeric 		    CurHostName[0] != '[')
748b31e7f2bSeric 		{
749b31e7f2bSeric 			int rcode;
750b31e7f2bSeric 			char buf[MAXNAME];
751b31e7f2bSeric 
752b31e7f2bSeric 			expand("\001j", buf, &buf[sizeof(buf) - 1], e);
753b31e7f2bSeric 			nmx = getmxrr(CurHostName, mxhosts, buf, &rcode);
754b31e7f2bSeric 			if (nmx < 0)
755b31e7f2bSeric 			{
756b31e7f2bSeric 				mci = mci_get(CurHostName, m);
757b31e7f2bSeric 				mci->mci_exitstat = rcode;
758b31e7f2bSeric 				mci->mci_errno = errno;
759b31e7f2bSeric 			}
760b31e7f2bSeric 		}
761b31e7f2bSeric 		else
762b31e7f2bSeric #endif
763b31e7f2bSeric 		{
764b31e7f2bSeric 			nmx = 1;
765b31e7f2bSeric 			mxhosts[0] = CurHostName;
766b31e7f2bSeric 		}
767b31e7f2bSeric 
76833db8731Seric 		if (!clever)
76933db8731Seric 			syserr("non-clever IPC");
77093b6e3cfSeric 		if (pvp[2] != NULL)
7711277f9a8Seric 			port = atoi(pvp[2]);
77293b6e3cfSeric 		else
7731277f9a8Seric 			port = 0;
774b31e7f2bSeric 		for (j = 0; j < nmx; j++)
775ebc61751Sbloom 		{
77645a8316eSeric 			/* see if we already know that this host is fried */
777b31e7f2bSeric 			CurHostName = mxhosts[j];
778b31e7f2bSeric 			mci = mci_get(CurHostName, m);
779b31e7f2bSeric 			if (mci->mci_state != MCIS_CLOSED)
78075889e88Seric 				return mci;
78115d084d5Seric 			mci->mci_mailer = m;
782b31e7f2bSeric 			if (mci->mci_exitstat != EX_OK)
783b31e7f2bSeric 				continue;
784b31e7f2bSeric 
78575889e88Seric 			/* try the connection */
786b31e7f2bSeric 			setproctitle("%s %s: %s", e->e_id, mxhosts[1], "user open");
787914346b1Seric 			message(Arpa_Info, "Connecting to %s (%s)...",
788b31e7f2bSeric 				mxhosts[j], m->m_name);
789b31e7f2bSeric 			i = makeconnection(mxhosts[j], port, mci,
790914346b1Seric 				bitnset(M_SECURE_PORT, m->m_flags));
79175889e88Seric 			mci->mci_exitstat = i;
79275889e88Seric 			mci->mci_errno = errno;
79375889e88Seric 			if (i == EX_OK)
794b31e7f2bSeric 			{
795b31e7f2bSeric 				mci->mci_state = MCIS_OPENING;
796b31e7f2bSeric 				mci_cache(mci);
797b31e7f2bSeric 				break;
798b31e7f2bSeric 			}
799b31e7f2bSeric 			else if (tTd(11, 1))
800b31e7f2bSeric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
801b31e7f2bSeric 					i, errno);
802b31e7f2bSeric 
80375889e88Seric 
8045f73204aSeric 			/* enter status of this host */
80575889e88Seric 			setstat(i);
806ed854c7bSeric 		}
8072a087e90Seric 		mci->mci_pid = 0;
808b31e7f2bSeric #else /* no DAEMON */
809588cad61Seric 		syserr("openmailer: no IPC");
81075889e88Seric 		return NULL;
811b31e7f2bSeric #endif /* DAEMON */
812588cad61Seric 	}
813b31e7f2bSeric 	else
814b31e7f2bSeric 	{
8156328bdf7Seric 		/* create a pipe to shove the mail through */
816f8952a83Seric 		if (pipe(mpvect) < 0)
81725a99e2eSeric 		{
818588cad61Seric 			syserr("openmailer: pipe (to mailer)");
81975889e88Seric 			return NULL;
82025a99e2eSeric 		}
821c579ef51Seric 
822c579ef51Seric 		/* if this mailer speaks smtp, create a return pipe */
823c579ef51Seric 		if (clever && pipe(rpvect) < 0)
824c579ef51Seric 		{
825588cad61Seric 			syserr("openmailer: pipe (from mailer)");
826c579ef51Seric 			(void) close(mpvect[0]);
827c579ef51Seric 			(void) close(mpvect[1]);
82875889e88Seric 			return NULL;
829c579ef51Seric 		}
830c579ef51Seric 
83133db8731Seric 		/*
83233db8731Seric 		**  Actually fork the mailer process.
83333db8731Seric 		**	DOFORK is clever about retrying.
8346984bfddSeric 		**
8356984bfddSeric 		**	Dispose of SIGCHLD signal catchers that may be laying
8366984bfddSeric 		**	around so that endmail will get it.
83733db8731Seric 		*/
83833db8731Seric 
839b31e7f2bSeric 		if (e->e_xfp != NULL)
840b31e7f2bSeric 			(void) fflush(e->e_xfp);		/* for debugging */
841588cad61Seric 		(void) fflush(stdout);
8426984bfddSeric # ifdef SIGCHLD
8436984bfddSeric 		(void) signal(SIGCHLD, SIG_DFL);
8446984bfddSeric # endif SIGCHLD
84584f7cd1bSeric 		DOFORK(FORK);
846f129ec7dSeric 		/* pid is set by DOFORK */
84725a99e2eSeric 		if (pid < 0)
84825a99e2eSeric 		{
84933db8731Seric 			/* failure */
850588cad61Seric 			syserr("openmailer: cannot fork");
851f8952a83Seric 			(void) close(mpvect[0]);
852f8952a83Seric 			(void) close(mpvect[1]);
853c579ef51Seric 			if (clever)
854c579ef51Seric 			{
855c579ef51Seric 				(void) close(rpvect[0]);
856c579ef51Seric 				(void) close(rpvect[1]);
857c579ef51Seric 			}
85875889e88Seric 			return NULL;
85925a99e2eSeric 		}
86025a99e2eSeric 		else if (pid == 0)
86125a99e2eSeric 		{
86213088b9fSeric 			int i;
8635f73204aSeric 			extern int DtableSize;
86413088b9fSeric 
86525a99e2eSeric 			/* child -- set up input & exec mailer */
86603ab8e55Seric 			/* make diagnostic output be standard output */
8678f0e7860Seric 			(void) signal(SIGINT, SIG_IGN);
8688f0e7860Seric 			(void) signal(SIGHUP, SIG_IGN);
8690984da9fSeric 			(void) signal(SIGTERM, SIG_DFL);
870f8952a83Seric 
871b31e7f2bSeric 			/* arrange to filter std & diag output of command */
872c579ef51Seric 			if (clever)
873c579ef51Seric 			{
874c579ef51Seric 				(void) close(rpvect[0]);
875c579ef51Seric 				(void) close(1);
876c579ef51Seric 				(void) dup(rpvect[1]);
877c579ef51Seric 				(void) close(rpvect[1]);
878c579ef51Seric 			}
879276723a8Seric 			else if (OpMode == MD_SMTP || HoldErrs)
880f8952a83Seric 			{
881588cad61Seric 				/* put mailer output in transcript */
882f8952a83Seric 				(void) close(1);
883b31e7f2bSeric 				(void) dup(fileno(e->e_xfp));
884f8952a83Seric 			}
885db8841e9Seric 			(void) close(2);
886db8841e9Seric 			(void) dup(1);
887f8952a83Seric 
888f8952a83Seric 			/* arrange to get standard input */
889f8952a83Seric 			(void) close(mpvect[1]);
890db8841e9Seric 			(void) close(0);
891f8952a83Seric 			if (dup(mpvect[0]) < 0)
89225a99e2eSeric 			{
89325a99e2eSeric 				syserr("Cannot dup to zero!");
894a590b978Seric 				_exit(EX_OSERR);
89525a99e2eSeric 			}
896f8952a83Seric 			(void) close(mpvect[0]);
89757fc6f17Seric 			if (!bitnset(M_RESTR, m->m_flags))
8980984da9fSeric 			{
89953e3fa05Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
900e36b99e2Seric 				{
901e36b99e2Seric 					(void) setgid(DefGid);
902898a126bSbostic 					(void) initgroups(DefUser, DefGid);
903e36b99e2Seric 					(void) setuid(DefUid);
904e36b99e2Seric 				}
905e36b99e2Seric 				else
90669f29479Seric 				{
907e36b99e2Seric 					(void) setgid(ctladdr->q_gid);
908898a126bSbostic 					(void) initgroups(ctladdr->q_ruser?
909898a126bSbostic 						ctladdr->q_ruser: ctladdr->q_user,
910898a126bSbostic 						ctladdr->q_gid);
911e36b99e2Seric 					(void) setuid(ctladdr->q_uid);
91269f29479Seric 				}
9130984da9fSeric 			}
914588cad61Seric 
91513088b9fSeric 			/* arrange for all the files to be closed */
916b31e7f2bSeric 			for (i = 3; i < DtableSize; i++)
917b31e7f2bSeric 			{
918fccb3f7aSbostic 				register int j;
919fccb3f7aSbostic 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
920fccb3f7aSbostic 					(void)fcntl(i, F_SETFD, j|1);
921fccb3f7aSbostic 			}
92233db8731Seric 
92333db8731Seric 			/* try to execute the mailer */
9245df317aaSeric 			execve(m->m_mailer, pvp, UserEnviron);
92513088b9fSeric 			syserr("Cannot exec %s", m->m_mailer);
92695b76e4bSeric 			if (m == LocalMailer)
92755f33c03Seric 				_exit(EX_TEMPFAIL);
92895b76e4bSeric 			switch (errno)
92995b76e4bSeric 			{
93095b76e4bSeric 			  case EIO:
93195b76e4bSeric 			  case EAGAIN:
93295b76e4bSeric 			  case ENOMEM:
93395b76e4bSeric # ifdef EPROCLIM
93495b76e4bSeric 			  case EPROCLIM:
93595b76e4bSeric # endif
93695b76e4bSeric 				_exit(EX_TEMPFAIL);
93795b76e4bSeric 			}
938a590b978Seric 			_exit(EX_UNAVAILABLE);
93925a99e2eSeric 		}
94025a99e2eSeric 
941f8952a83Seric 		/*
942c579ef51Seric 		**  Set up return value.
943f8952a83Seric 		*/
944f8952a83Seric 
945b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
9462a087e90Seric 		bzero((char *) mci, sizeof *mci);
94715d084d5Seric 		mci->mci_mailer = m;
948b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9492a087e90Seric 		mci->mci_pid = pid;
950f8952a83Seric 		(void) close(mpvect[0]);
95175889e88Seric 		mci->mci_out = fdopen(mpvect[1], "w");
952c579ef51Seric 		if (clever)
95325a99e2eSeric 		{
954c579ef51Seric 			(void) close(rpvect[1]);
95575889e88Seric 			mci->mci_in = fdopen(rpvect[0], "r");
95675889e88Seric 		}
95775889e88Seric 		else
95875889e88Seric 		{
95975889e88Seric 			mci->mci_flags |= MCIF_TEMP;
96075889e88Seric 			mci->mci_in = NULL;
96175889e88Seric 		}
962b31e7f2bSeric 	}
963b31e7f2bSeric 
964b31e7f2bSeric 	/*
965b31e7f2bSeric 	**  If we are in SMTP opening state, send initial protocol.
966b31e7f2bSeric 	*/
967b31e7f2bSeric 
968b31e7f2bSeric 	if (clever && mci->mci_state != MCIS_CLOSED)
969b31e7f2bSeric 	{
970b31e7f2bSeric 		smtpinit(m, mci, e);
971b31e7f2bSeric 	}
972c579ef51Seric 
97375889e88Seric 	return mci;
97425a99e2eSeric }
97525a99e2eSeric /*
97625a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
97725a99e2eSeric **
97825a99e2eSeric **	Parameters:
97925a99e2eSeric **		stat -- the status code from the mailer (high byte
98025a99e2eSeric **			only; core dumps must have been taken care of
98125a99e2eSeric **			already).
98225a99e2eSeric **		m -- the mailer descriptor for this mailer.
98325a99e2eSeric **
98425a99e2eSeric **	Returns:
985db8841e9Seric **		none.
98625a99e2eSeric **
98725a99e2eSeric **	Side Effects:
988c1f9df2cSeric **		Errors may be incremented.
98925a99e2eSeric **		ExitStat may be set.
99025a99e2eSeric */
99125a99e2eSeric 
992198d9be0Seric giveresponse(stat, m, e)
99325a99e2eSeric 	int stat;
994588cad61Seric 	register MAILER *m;
995198d9be0Seric 	ENVELOPE *e;
99625a99e2eSeric {
99725a99e2eSeric 	register char *statmsg;
99825a99e2eSeric 	extern char *SysExMsg[];
99925a99e2eSeric 	register int i;
1000d4bd8f0eSbostic 	extern int N_SysEx;
1001d4bd8f0eSbostic #ifdef NAMED_BIND
1002d4bd8f0eSbostic 	extern int h_errno;
1003d4bd8f0eSbostic #endif
1004198d9be0Seric 	char buf[MAXLINE];
100525a99e2eSeric 
10067d1fc79dSeric #ifdef lint
10077d1fc79dSeric 	if (m == NULL)
10087d1fc79dSeric 		return;
10097d1fc79dSeric #endif lint
10107d1fc79dSeric 
101113bbc08cSeric 	/*
101213bbc08cSeric 	**  Compute status message from code.
101313bbc08cSeric 	*/
101413bbc08cSeric 
101525a99e2eSeric 	i = stat - EX__BASE;
1016588cad61Seric 	if (stat == 0)
1017588cad61Seric 		statmsg = "250 Sent";
1018588cad61Seric 	else if (i < 0 || i > N_SysEx)
1019588cad61Seric 	{
1020588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1021588cad61Seric 		stat = EX_UNAVAILABLE;
1022588cad61Seric 		statmsg = buf;
1023588cad61Seric 	}
1024198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1025198d9be0Seric 	{
10268557d168Seric 		(void) strcpy(buf, SysExMsg[i]);
1027d4bd8f0eSbostic #ifdef NAMED_BIND
1028f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1029f28da541Smiriam 		{
1030f28da541Smiriam 			extern char *errstring();
1031f28da541Smiriam 
1032f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1033f28da541Smiriam 		}
1034f28da541Smiriam 		else
1035d4bd8f0eSbostic #endif
1036f28da541Smiriam 		{
10378557d168Seric 			if (errno != 0)
1038198d9be0Seric 			{
103987c9b3e7Seric 				extern char *errstring();
10408557d168Seric 
1041d87e85f3Seric 				statmsg = errstring(errno);
1042d87e85f3Seric 			}
1043d87e85f3Seric 			else
1044d87e85f3Seric 			{
1045d87e85f3Seric #ifdef SMTP
1046d87e85f3Seric 				extern char SmtpError[];
1047d87e85f3Seric 
1048d87e85f3Seric 				statmsg = SmtpError;
1049d87e85f3Seric #else SMTP
1050d87e85f3Seric 				statmsg = NULL;
1051d87e85f3Seric #endif SMTP
1052d87e85f3Seric 			}
1053f28da541Smiriam 		}
1054d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1055d87e85f3Seric 		{
105687c9b3e7Seric 			(void) strcat(buf, ": ");
1057d87e85f3Seric 			(void) strcat(buf, statmsg);
10588557d168Seric 		}
1059198d9be0Seric 		statmsg = buf;
1060198d9be0Seric 	}
106125a99e2eSeric 	else
1062d87e85f3Seric 	{
106325a99e2eSeric 		statmsg = SysExMsg[i];
1064d87e85f3Seric 	}
1065588cad61Seric 
1066588cad61Seric 	/*
1067588cad61Seric 	**  Print the message as appropriate
1068588cad61Seric 	*/
1069588cad61Seric 
1070198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
10715826d9d3Seric 		message(Arpa_Info, &statmsg[4]);
107225a99e2eSeric 	else
107325a99e2eSeric 	{
1074c1f9df2cSeric 		Errors++;
10755826d9d3Seric 		usrerr(statmsg);
107625a99e2eSeric 	}
107725a99e2eSeric 
107825a99e2eSeric 	/*
107925a99e2eSeric 	**  Final cleanup.
108025a99e2eSeric 	**	Log a record of the transaction.  Compute the new
108125a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
108225a99e2eSeric 	**	that.
108325a99e2eSeric 	*/
108425a99e2eSeric 
108561f5a1d4Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
1086b31e7f2bSeric 		logdelivery(&statmsg[4], e);
1087eb238f8cSeric 
1088eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1089eb238f8cSeric 		setstat(stat);
1090198d9be0Seric 	if (stat != EX_OK)
1091198d9be0Seric 	{
1092198d9be0Seric 		if (e->e_message != NULL)
1093198d9be0Seric 			free(e->e_message);
1094198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1095198d9be0Seric 	}
10968557d168Seric 	errno = 0;
1097d4bd8f0eSbostic #ifdef NAMED_BIND
1098f28da541Smiriam 	h_errno = 0;
1099d4bd8f0eSbostic #endif
1100eb238f8cSeric }
1101eb238f8cSeric /*
1102eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1103eb238f8cSeric **
1104eb238f8cSeric **	Parameters:
1105eb238f8cSeric **		stat -- the message to print for the status
1106eb238f8cSeric **
1107eb238f8cSeric **	Returns:
1108eb238f8cSeric **		none
1109eb238f8cSeric **
1110eb238f8cSeric **	Side Effects:
1111eb238f8cSeric **		none
1112eb238f8cSeric */
1113eb238f8cSeric 
1114b31e7f2bSeric logdelivery(stat, e)
1115eb238f8cSeric 	char *stat;
1116b31e7f2bSeric 	register ENVELOPE *e;
11175cf56be3Seric {
11185cf56be3Seric 	extern char *pintvl();
11195cf56be3Seric 
1120eb238f8cSeric # ifdef LOG
1121b31e7f2bSeric 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", e->e_id,
1122b31e7f2bSeric 	       e->e_to, pintvl(curtime() - e->e_ctime, TRUE), stat);
112325a99e2eSeric # endif LOG
112425a99e2eSeric }
112525a99e2eSeric /*
112651552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
112725a99e2eSeric **
112851552439Seric **	This can be made an arbitrary message separator by changing $l
112951552439Seric **
11309b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
11319b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
11329b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
11339b6c17a6Seric **	this kind of antique garbage????
113425a99e2eSeric **
113525a99e2eSeric **	Parameters:
113651552439Seric **		fp -- the file to output to.
113751552439Seric **		m -- the mailer describing this entry.
113825a99e2eSeric **
113925a99e2eSeric **	Returns:
114051552439Seric **		none
114125a99e2eSeric **
114225a99e2eSeric **	Side Effects:
114351552439Seric **		outputs some text to fp.
114425a99e2eSeric */
114525a99e2eSeric 
1146b31e7f2bSeric putfromline(fp, m, e)
114751552439Seric 	register FILE *fp;
114851552439Seric 	register MAILER *m;
1149b31e7f2bSeric 	ENVELOPE *e;
115025a99e2eSeric {
11519b6c17a6Seric 	char *template = "\001l\n";
115251552439Seric 	char buf[MAXLINE];
115325a99e2eSeric 
115457fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
115551552439Seric 		return;
115613bbc08cSeric 
11572c7e1b8dSeric # ifdef UGLYUUCP
115857fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
115974b6e67bSeric 	{
1160ea09d6edSeric 		char *bang;
1161ea09d6edSeric 		char xbuf[MAXLINE];
116274b6e67bSeric 
1163b31e7f2bSeric 		expand("\001<", buf, &buf[sizeof buf - 1], e);
1164ea09d6edSeric 		bang = index(buf, '!');
116574b6e67bSeric 		if (bang == NULL)
1166ea09d6edSeric 			syserr("No ! in UUCP! (%s)", buf);
116774b6e67bSeric 		else
1168588cad61Seric 		{
1169ea09d6edSeric 			*bang++ = '\0';
11709b6c17a6Seric 			(void) sprintf(xbuf, "From %s  \001d remote from %s\n", bang, buf);
1171ea09d6edSeric 			template = xbuf;
117274b6e67bSeric 		}
1173588cad61Seric 	}
11742c7e1b8dSeric # endif UGLYUUCP
1175b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
117677b52738Seric 	putline(buf, fp, m);
1177bc6e2962Seric }
1178bc6e2962Seric /*
117951552439Seric **  PUTBODY -- put the body of a message.
118051552439Seric **
118151552439Seric **	Parameters:
118251552439Seric **		fp -- file to output onto.
118377b52738Seric **		m -- a mailer descriptor to control output format.
11849a6a5f55Seric **		e -- the envelope to put out.
118551552439Seric **
118651552439Seric **	Returns:
118751552439Seric **		none.
118851552439Seric **
118951552439Seric **	Side Effects:
119051552439Seric **		The message is written onto fp.
119151552439Seric */
119251552439Seric 
119377b52738Seric putbody(fp, m, e)
119451552439Seric 	FILE *fp;
1195588cad61Seric 	MAILER *m;
11969a6a5f55Seric 	register ENVELOPE *e;
119751552439Seric {
119877b52738Seric 	char buf[MAXLINE];
119951552439Seric 
120051552439Seric 	/*
120151552439Seric 	**  Output the body of the message
120251552439Seric 	*/
120351552439Seric 
12049a6a5f55Seric 	if (e->e_dfp == NULL)
120551552439Seric 	{
12069a6a5f55Seric 		if (e->e_df != NULL)
12079a6a5f55Seric 		{
12089a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
12099a6a5f55Seric 			if (e->e_dfp == NULL)
12108f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
12118f9146b0Srick 				e->e_df, e->e_to, e->e_from);
12129a6a5f55Seric 		}
12139a6a5f55Seric 		else
121477b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
12159a6a5f55Seric 	}
12169a6a5f55Seric 	if (e->e_dfp != NULL)
12179a6a5f55Seric 	{
12189a6a5f55Seric 		rewind(e->e_dfp);
121977b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
122024fc8aeeSeric 		{
122124fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1222d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
12233462ad9eSeric 				(void) putc('>', fp);
122477b52738Seric 			putline(buf, fp, m);
122524fc8aeeSeric 		}
122651552439Seric 
12279a6a5f55Seric 		if (ferror(e->e_dfp))
122851552439Seric 		{
122951552439Seric 			syserr("putbody: read error");
123051552439Seric 			ExitStat = EX_IOERR;
123151552439Seric 		}
123251552439Seric 	}
123351552439Seric 
123451552439Seric 	(void) fflush(fp);
123551552439Seric 	if (ferror(fp) && errno != EPIPE)
123651552439Seric 	{
123751552439Seric 		syserr("putbody: write error");
123851552439Seric 		ExitStat = EX_IOERR;
123951552439Seric 	}
124051552439Seric 	errno = 0;
124125a99e2eSeric }
124225a99e2eSeric /*
124325a99e2eSeric **  MAILFILE -- Send a message to a file.
124425a99e2eSeric **
1245f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1246f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1247f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1248f129ec7dSeric **	sendmail runs as root.
1249f129ec7dSeric **
1250588cad61Seric **	This could be done as a subordinate mailer, except that it
1251588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1252588cad61Seric **	view this as being sufficiently important as to include it
1253588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1254588cad61Seric **	to create another process plus some pipes to save the message.
1255588cad61Seric **
125625a99e2eSeric **	Parameters:
125725a99e2eSeric **		filename -- the name of the file to send to.
12586259796dSeric **		ctladdr -- the controlling address header -- includes
12596259796dSeric **			the userid/groupid to be when sending.
126025a99e2eSeric **
126125a99e2eSeric **	Returns:
126225a99e2eSeric **		The exit code associated with the operation.
126325a99e2eSeric **
126425a99e2eSeric **	Side Effects:
126525a99e2eSeric **		none.
126625a99e2eSeric */
126725a99e2eSeric 
1268b31e7f2bSeric mailfile(filename, ctladdr, e)
126925a99e2eSeric 	char *filename;
12706259796dSeric 	ADDRESS *ctladdr;
1271b31e7f2bSeric 	register ENVELOPE *e;
127225a99e2eSeric {
127325a99e2eSeric 	register FILE *f;
127432d19d43Seric 	register int pid;
127515d084d5Seric 	int mode;
127625a99e2eSeric 
127732d19d43Seric 	/*
127832d19d43Seric 	**  Fork so we can change permissions here.
127932d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
128032d19d43Seric 	**	the complications of calling subroutines, etc.
128132d19d43Seric 	*/
128232d19d43Seric 
128332d19d43Seric 	DOFORK(fork);
128432d19d43Seric 
128532d19d43Seric 	if (pid < 0)
128632d19d43Seric 		return (EX_OSERR);
128732d19d43Seric 	else if (pid == 0)
128832d19d43Seric 	{
128932d19d43Seric 		/* child -- actually write to file */
1290f129ec7dSeric 		struct stat stb;
1291f129ec7dSeric 
12920984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
12930984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
12940984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
12953462ad9eSeric 		(void) umask(OldUmask);
129695f16dc0Seric 
1297f129ec7dSeric 		if (stat(filename, &stb) < 0)
1298e6e1265fSeric 			stb.st_mode = 0666;
129915d084d5Seric 		mode = stb.st_mode;
130095f16dc0Seric 
130195f16dc0Seric 		/* limit the errors to those actually caused in the child */
130295f16dc0Seric 		errno = 0;
130395f16dc0Seric 		ExitStat = EX_OK;
130495f16dc0Seric 
1305f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1306f129ec7dSeric 			exit(EX_CANTCREAT);
130703827b5fSeric 		if (ctladdr == NULL)
13088f9146b0Srick 			ctladdr = &e->e_from;
130915d084d5Seric 		else
131015d084d5Seric 		{
131115d084d5Seric 			/* ignore setuid and setgid bits */
131215d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
131315d084d5Seric 		}
131415d084d5Seric 
13158f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
13168f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
13178f9146b0Srick 		{
13188f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
131995f16dc0Seric 			if (e->e_dfp == NULL)
132095f16dc0Seric 			{
13218f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
13228f9146b0Srick 					e->e_df, e->e_to, e->e_from);
13238f9146b0Srick 			}
13248f9146b0Srick 		}
13258f9146b0Srick 
132615d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1327e36b99e2Seric 		{
132895f16dc0Seric 			if (ctladdr->q_uid == 0)
132995f16dc0Seric 			{
1330e36b99e2Seric 				(void) setgid(DefGid);
1331898a126bSbostic 				(void) initgroups(DefUser, DefGid);
133295f16dc0Seric 			}
133395f16dc0Seric 			else
133495f16dc0Seric 			{
13356259796dSeric 				(void) setgid(ctladdr->q_gid);
1336898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1337898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1338898a126bSbostic 					ctladdr->q_gid);
1339898a126bSbostic 			}
1340e36b99e2Seric 		}
134115d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1342e36b99e2Seric 		{
1343e36b99e2Seric 			if (ctladdr->q_uid == 0)
1344e36b99e2Seric 				(void) setuid(DefUid);
1345e36b99e2Seric 			else
13466259796dSeric 				(void) setuid(ctladdr->q_uid);
1347e36b99e2Seric 		}
134895f16dc0Seric 		FileName = filename;
134995f16dc0Seric 		LineNumber = 0;
135027628d59Seric 		f = dfopen(filename, "a");
135125a99e2eSeric 		if (f == NULL)
135295f16dc0Seric 		{
135395f16dc0Seric 			message("cannot open");
135432d19d43Seric 			exit(EX_CANTCREAT);
135595f16dc0Seric 		}
135625a99e2eSeric 
1357b31e7f2bSeric 		putfromline(f, ProgMailer, e);
1358b843d759Seric 		(*e->e_puthdr)(f, ProgMailer, e);
135977b52738Seric 		putline("\n", f, ProgMailer);
1360b843d759Seric 		(*e->e_putbody)(f, ProgMailer, e);
136177b52738Seric 		putline("\n", f, ProgMailer);
136295f16dc0Seric 		if (ferror(f))
136395f16dc0Seric 		{
136495f16dc0Seric 			message("I/O error");
136595f16dc0Seric 			setstat(EX_IOERR);
136695f16dc0Seric 		}
1367db8841e9Seric 		(void) fclose(f);
136832d19d43Seric 		(void) fflush(stdout);
1369e36b99e2Seric 
137027628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1371c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
137295f16dc0Seric 		exit(ExitStat);
137313bbc08cSeric 		/*NOTREACHED*/
137432d19d43Seric 	}
137532d19d43Seric 	else
137632d19d43Seric 	{
137732d19d43Seric 		/* parent -- wait for exit status */
1378588cad61Seric 		int st;
137932d19d43Seric 
1380588cad61Seric 		st = waitfor(pid);
1381588cad61Seric 		if ((st & 0377) != 0)
1382588cad61Seric 			return (EX_UNAVAILABLE);
1383588cad61Seric 		else
1384588cad61Seric 			return ((st >> 8) & 0377);
13858f9146b0Srick 		/*NOTREACHED*/
138632d19d43Seric 	}
138725a99e2eSeric }
1388ea4dc939Seric /*
1389ea4dc939Seric **  SENDALL -- actually send all the messages.
1390ea4dc939Seric **
1391ea4dc939Seric **	Parameters:
13920c52a0b3Seric **		e -- the envelope to send.
13937b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
13947b95031aSeric **			the current SendMode.
1395ea4dc939Seric **
1396ea4dc939Seric **	Returns:
1397ea4dc939Seric **		none.
1398ea4dc939Seric **
1399ea4dc939Seric **	Side Effects:
1400ea4dc939Seric **		Scans the send lists and sends everything it finds.
14010c52a0b3Seric **		Delivers any appropriate error messages.
1402276723a8Seric **		If we are running in a non-interactive mode, takes the
1403276723a8Seric **			appropriate action.
1404ea4dc939Seric */
1405ea4dc939Seric 
1406276723a8Seric sendall(e, mode)
14070c52a0b3Seric 	ENVELOPE *e;
1408276723a8Seric 	char mode;
1409ea4dc939Seric {
1410e77e673fSeric 	register ADDRESS *q;
141114a8ed7aSeric 	bool oldverbose;
1412276723a8Seric 	int pid;
1413dde5acadSeric 	int nsent;
14141c265a00Seric # ifdef LOCKF
14151c265a00Seric 	struct flock lfd;
14161c265a00Seric # endif
1417ea4dc939Seric 
14187b95031aSeric 	/* determine actual delivery mode */
14197b95031aSeric 	if (mode == SM_DEFAULT)
14207b95031aSeric 	{
14215f73204aSeric 		extern bool shouldqueue();
14227b95031aSeric 
14235f73204aSeric 		if (shouldqueue(e->e_msgpriority))
14247b95031aSeric 			mode = SM_QUEUE;
14257b95031aSeric 		else
14267b95031aSeric 			mode = SendMode;
14277b95031aSeric 	}
14287b95031aSeric 
1429df864a8fSeric 	if (tTd(13, 1))
1430772e6e50Seric 	{
1431276723a8Seric 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
14320c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1433772e6e50Seric 	}
1434ea4dc939Seric 
14350c52a0b3Seric 	/*
1436276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1437588cad61Seric 	**	Check to make sure the hop count is reasonable.
1438588cad61Seric 	**	Delete sends to the sender in mailing lists.
1439276723a8Seric 	*/
1440276723a8Seric 
1441588cad61Seric 	CurEnv = e;
1442276723a8Seric 
14431d57450bSeric 	if (e->e_hopcount > MaxHopCount)
1444276723a8Seric 	{
14458f9146b0Srick 		errno = 0;
14468f9146b0Srick 		syserr("sendall: too many hops %d (%d max): from %s, to %s",
14471d57450bSeric 			e->e_hopcount, MaxHopCount, e->e_from, e->e_to);
1448588cad61Seric 		return;
1449588cad61Seric 	}
1450588cad61Seric 
1451588cad61Seric 	if (!MeToo)
1452276723a8Seric 	{
1453f3d6c55cSeric 		extern ADDRESS *recipient();
1454f3d6c55cSeric 
1455588cad61Seric 		e->e_from.q_flags |= QDONTSEND;
1456b843d759Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1457276723a8Seric 	}
1458588cad61Seric 
1459588cad61Seric # ifdef QUEUE
1460b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1461b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1462b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
1463e020b127Seric 		queueup(e, TRUE, mode == SM_QUEUE);
1464276723a8Seric #endif QUEUE
1465276723a8Seric 
1466276723a8Seric 	oldverbose = Verbose;
1467276723a8Seric 	switch (mode)
1468276723a8Seric 	{
1469276723a8Seric 	  case SM_VERIFY:
1470276723a8Seric 		Verbose = TRUE;
1471276723a8Seric 		break;
1472276723a8Seric 
1473276723a8Seric 	  case SM_QUEUE:
1474c7f5410dSeric   queueonly:
1475b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1476276723a8Seric 		return;
1477276723a8Seric 
1478276723a8Seric 	  case SM_FORK:
14799a6a5f55Seric 		if (e->e_xfp != NULL)
14809a6a5f55Seric 			(void) fflush(e->e_xfp);
1481c7f5410dSeric 
1482c7f5410dSeric # ifdef LOCKF
1483c7f5410dSeric 		/*
1484c7f5410dSeric 		**  Since lockf has the interesting semantic that the
14851c265a00Seric 		**  lock is lost when we fork, we have to risk losing
14861c265a00Seric 		**  the lock here by closing before the fork, and then
14871c265a00Seric 		**  trying to get it back in the child.
1488c7f5410dSeric 		*/
1489c7f5410dSeric 
1490e020b127Seric 		if (e->e_lockfp != NULL)
1491c7f5410dSeric 		{
1492e020b127Seric 			(void) fclose(e->e_lockfp);
1493e020b127Seric 			e->e_lockfp = NULL;
1494c7f5410dSeric 		}
1495c7f5410dSeric # endif /* LOCKF */
1496c7f5410dSeric 
1497276723a8Seric 		pid = fork();
1498276723a8Seric 		if (pid < 0)
1499276723a8Seric 		{
1500c7f5410dSeric 			goto queueonly;
1501276723a8Seric 		}
1502276723a8Seric 		else if (pid > 0)
1503a6fce3d8Seric 		{
1504a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1505b254bcb6Seric 			e->e_id = e->e_df = NULL;
1506c7f5410dSeric # ifndef LOCKF
1507e020b127Seric 			if (e->e_lockfp != NULL)
1508e020b127Seric 				(void) fclose(e->e_lockfp);
1509c7f5410dSeric # endif
1510276723a8Seric 			return;
1511a6fce3d8Seric 		}
1512276723a8Seric 
1513276723a8Seric 		/* double fork to avoid zombies */
1514276723a8Seric 		if (fork() > 0)
1515276723a8Seric 			exit(EX_OK);
1516276723a8Seric 
1517a6fce3d8Seric 		/* be sure we are immune from the terminal */
1518769e215aSeric 		disconnect(FALSE);
1519a6fce3d8Seric 
1520e6720d51Seric # ifdef LOCKF
1521e6720d51Seric 		/*
1522c7f5410dSeric 		**  Now try to get our lock back.
1523e6720d51Seric 		*/
1524e6720d51Seric 
15251c265a00Seric 		lfd.l_type = F_WRLCK;
15261c265a00Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1527e020b127Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1528e020b127Seric 		if (e->e_lockfp == NULL ||
15291c265a00Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1530e6720d51Seric 		{
1531e6720d51Seric 			/* oops....  lost it */
1532e6720d51Seric # ifdef LOG
1533e6720d51Seric 			if (LogLevel > 5)
1534c7f5410dSeric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1535b31e7f2bSeric 					e->e_id);
1536e6720d51Seric # endif /* LOG */
1537e6720d51Seric 			exit(EX_OK);
1538e6720d51Seric 		}
1539e6720d51Seric # endif /* LOCKF */
1540e6720d51Seric 
1541276723a8Seric 		break;
1542276723a8Seric 	}
1543276723a8Seric 
1544276723a8Seric 	/*
15450c52a0b3Seric 	**  Run through the list and send everything.
15460c52a0b3Seric 	*/
15470c52a0b3Seric 
1548dde5acadSeric 	nsent = 0;
15490c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1550ea4dc939Seric 	{
1551276723a8Seric 		if (mode == SM_VERIFY)
1552ea4dc939Seric 		{
1553a6fce3d8Seric 			e->e_to = q->q_paddr;
1554e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1555ea4dc939Seric 				message(Arpa_Info, "deliverable");
1556ea4dc939Seric 		}
1557dde5acadSeric 		else if (!bitset(QDONTSEND, q->q_flags))
1558dde5acadSeric 		{
1559de1179f5Seric # ifdef QUEUE
1560dde5acadSeric 			/*
1561dde5acadSeric 			**  Checkpoint the send list every few addresses
1562dde5acadSeric 			*/
1563dde5acadSeric 
1564dde5acadSeric 			if (nsent >= CheckpointInterval)
1565dde5acadSeric 			{
1566e020b127Seric 				queueup(e, TRUE, FALSE);
1567dde5acadSeric 				nsent = 0;
1568dde5acadSeric 			}
1569de1179f5Seric # endif /* QUEUE */
1570dde5acadSeric 			if (deliver(e, q) == EX_OK)
1571dde5acadSeric 				nsent++;
1572dde5acadSeric 		}
1573ea4dc939Seric 	}
157414a8ed7aSeric 	Verbose = oldverbose;
15750c52a0b3Seric 
15760c52a0b3Seric 	/*
15770c52a0b3Seric 	**  Now run through and check for errors.
15780c52a0b3Seric 	*/
15790c52a0b3Seric 
1580e020b127Seric 	if (mode == SM_VERIFY)
15810c52a0b3Seric 		return;
15820c52a0b3Seric 
15830c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15840c52a0b3Seric 	{
15850c52a0b3Seric 		register ADDRESS *qq;
15860c52a0b3Seric 
1587df864a8fSeric 		if (tTd(13, 3))
1588df864a8fSeric 		{
1589df864a8fSeric 			printf("Checking ");
1590df864a8fSeric 			printaddr(q, FALSE);
1591df864a8fSeric 		}
1592df864a8fSeric 
1593b254bcb6Seric 		/* only send errors if the message failed */
1594b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1595b254bcb6Seric 			continue;
15960c52a0b3Seric 
15970c52a0b3Seric 		/* we have an address that failed -- find the parent */
15980c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
15990c52a0b3Seric 		{
16000c52a0b3Seric 			char obuf[MAXNAME + 6];
16010c52a0b3Seric 			extern char *aliaslookup();
16020c52a0b3Seric 
16030c52a0b3Seric 			/* we can only have owners for local addresses */
160457fc6f17Seric 			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
16050c52a0b3Seric 				continue;
16060c52a0b3Seric 
16070c52a0b3Seric 			/* see if the owner list exists */
16080c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1609cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1610cec031e3Seric 				(void) strcat(obuf, "owner");
1611cec031e3Seric 			else
16120c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
1613ef137175Sbostic 			makelower(obuf);
16140c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
16150c52a0b3Seric 				continue;
16160c52a0b3Seric 
1617df864a8fSeric 			if (tTd(13, 4))
1618df864a8fSeric 				printf("Errors to %s\n", obuf);
1619df864a8fSeric 
16200c52a0b3Seric 			/* owner list exists -- add it to the error queue */
1621b843d759Seric 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue, e);
162253e3fa05Seric 			ErrorMode = EM_MAIL;
16230c52a0b3Seric 			break;
16240c52a0b3Seric 		}
16250c52a0b3Seric 
16260c52a0b3Seric 		/* if we did not find an owner, send to the sender */
16277455aa0bSeric 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1628b843d759Seric 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue, e);
16290c52a0b3Seric 	}
1630276723a8Seric 
1631276723a8Seric 	if (mode == SM_FORK)
1632276723a8Seric 		finis();
16330c52a0b3Seric }
1634