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*75f1ade9Seric static char sccsid[] = "@(#)deliver.c	6.12 (Berkeley) 01/26/93";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14911693bfSbostic #include <sys/signal.h>
15c77d1c25Seric #include <sys/stat.h>
16f28da541Smiriam #include <netdb.h>
17fccb3f7aSbostic #include <fcntl.h>
18911693bfSbostic #include <errno.h>
19134746fbSeric #ifdef NAMED_BIND
20912a731aSbostic #include <arpa/nameser.h>
21912a731aSbostic #include <resolv.h>
22134746fbSeric #endif
2325a99e2eSeric 
2425a99e2eSeric /*
2513bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
2613bbc08cSeric **
2713bbc08cSeric **	This routine delivers to everyone on the same host as the
2813bbc08cSeric **	user on the head of the list.  It is clever about mailers
2913bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
3013bbc08cSeric **	that it will deliver to all these addresses however -- so
3113bbc08cSeric **	deliver should be called once for each address on the
3213bbc08cSeric **	list.
3325a99e2eSeric **
3425a99e2eSeric **	Parameters:
35588cad61Seric **		e -- the envelope to deliver.
36c77d1c25Seric **		firstto -- head of the address list to deliver to.
3725a99e2eSeric **
3825a99e2eSeric **	Returns:
3925a99e2eSeric **		zero -- successfully delivered.
4025a99e2eSeric **		else -- some failure, see ExitStat for more info.
4125a99e2eSeric **
4225a99e2eSeric **	Side Effects:
4325a99e2eSeric **		The standard input is passed off to someone.
4425a99e2eSeric */
4525a99e2eSeric 
46588cad61Seric deliver(e, firstto)
47588cad61Seric 	register ENVELOPE *e;
48c77d1c25Seric 	ADDRESS *firstto;
4925a99e2eSeric {
5078442df3Seric 	char *host;			/* host being sent to */
5178442df3Seric 	char *user;			/* user being sent to */
5225a99e2eSeric 	char **pvp;
535dfc646bSeric 	register char **mvp;
5425a99e2eSeric 	register char *p;
55588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
566259796dSeric 	ADDRESS *ctladdr;
57b31e7f2bSeric 	register MCI *mci;
58c77d1c25Seric 	register ADDRESS *to = firstto;
59c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
60772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
61911693bfSbostic 	int rcode;			/* response code */
62c23ed322Seric 	char *from;			/* pointer to from person */
63e103b48fSeric 	char *firstsig;			/* signature of firstto */
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 */
69fabb3bd4Seric 	extern int checkcompat();
70ee6bf8dfSeric 	extern ADDRESS *getctladdr();
71ee6bf8dfSeric 	extern char *remotename();
72b31e7f2bSeric 	extern MCI *openmailer();
73e103b48fSeric 	extern char *hostsignature();
7425a99e2eSeric 
7535490626Seric 	errno = 0;
76da2935e1Seric 	if (bitset(QDONTSEND, to->q_flags))
775dfc646bSeric 		return (0);
7825a99e2eSeric 
79134746fbSeric #ifdef NAMED_BIND
80912a731aSbostic 	/* unless interactive, try twice, over a minute */
81912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
82912a731aSbostic 		_res.retrans = 30;
83912a731aSbostic 		_res.retry = 2;
84912a731aSbostic 	}
85d4bd8f0eSbostic #endif
86912a731aSbostic 
8751552439Seric 	m = to->q_mailer;
8851552439Seric 	host = to->q_host;
8951552439Seric 
906ef48975Seric 	if (tTd(10, 1))
915dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
9251552439Seric 			m->m_mno, host, to->q_user);
93f3dbc832Seric 
94f3dbc832Seric 	/*
95f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
96f3dbc832Seric 	**  connections now, just mark these addresses and return.
97f3dbc832Seric 	**	This is useful if we want to batch connections to
98f3dbc832Seric 	**	reduce load.  This will cause the messages to be
99f3dbc832Seric 	**	queued up, and a daemon will come along to send the
100f3dbc832Seric 	**	messages later.
101f3dbc832Seric 	**		This should be on a per-mailer basis.
102f3dbc832Seric 	*/
103f3dbc832Seric 
10457fc6f17Seric 	if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) &&
105317680d6Seric 	    !Verbose)
106f3dbc832Seric 	{
107f3dbc832Seric 		for (; to != NULL; to = to->q_next)
108f4560e80Seric 		{
109f4560e80Seric 			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
110f4560e80Seric 				continue;
111f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
112588cad61Seric 			e->e_to = to->q_paddr;
113eb238f8cSeric 			message(Arpa_Info, "queued");
114eb238f8cSeric 			if (LogLevel > 4)
115b31e7f2bSeric 				logdelivery("queued", e);
116f4560e80Seric 		}
117588cad61Seric 		e->e_to = NULL;
118f3dbc832Seric 		return (0);
119f3dbc832Seric 	}
120f3dbc832Seric 
12125a99e2eSeric 	/*
1225dfc646bSeric 	**  Do initial argv setup.
1235dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
1245dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
1255dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
1265dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
1275dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
1283bea8136Seric 	**		The from address rewrite is expected to make
1293bea8136Seric 	**		the address relative to the other end.
1305dfc646bSeric 	*/
1315dfc646bSeric 
13278442df3Seric 	/* rewrite from address, using rewriting rules */
133b843d759Seric 	(void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, TRUE, e));
134c23ed322Seric 	if (e->e_returnpath == e->e_sender)
135c23ed322Seric 	{
136c23ed322Seric 		from = rpathbuf;
137c23ed322Seric 	}
138c23ed322Seric 	else
139c23ed322Seric 	{
140b843d759Seric 		(void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, TRUE, e));
141c23ed322Seric 		from = tfrombuf;
142c23ed322Seric 	}
14378442df3Seric 
144c23ed322Seric 	define('f', e->e_returnpath, e);	/* raw return path */
145c23ed322Seric 	define('<', rpathbuf, e);		/* translated return path */
146c23ed322Seric 	define('g', from, e);			/* translated sender */
147588cad61Seric 	define('h', host, e);			/* to host */
1485dfc646bSeric 	Errors = 0;
1495dfc646bSeric 	pvp = pv;
1505dfc646bSeric 	*pvp++ = m->m_argv[0];
1515dfc646bSeric 
1525dfc646bSeric 	/* insert -f or -r flag as appropriate */
15357fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
1545dfc646bSeric 	{
15557fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
1565dfc646bSeric 			*pvp++ = "-f";
1575dfc646bSeric 		else
1585dfc646bSeric 			*pvp++ = "-r";
159c23ed322Seric 		*pvp++ = newstr(rpathbuf);
1605dfc646bSeric 	}
1615dfc646bSeric 
1625dfc646bSeric 	/*
1635dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1645dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1655dfc646bSeric 	**  be one of these, and there are only a few more slots
1665dfc646bSeric 	**  in the pv after it.
1675dfc646bSeric 	*/
1685dfc646bSeric 
1695dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1705dfc646bSeric 	{
1716c2c3107Seric 		while ((p = strchr(p, '\001')) != NULL)
1725dfc646bSeric 			if (*++p == 'u')
1735dfc646bSeric 				break;
1745dfc646bSeric 		if (p != NULL)
1755dfc646bSeric 			break;
1765dfc646bSeric 
1775dfc646bSeric 		/* this entry is safe -- go ahead and process it */
178588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1795dfc646bSeric 		*pvp++ = newstr(buf);
1805dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1815dfc646bSeric 		{
1825dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1835dfc646bSeric 			return (-1);
1845dfc646bSeric 		}
1855dfc646bSeric 	}
186c579ef51Seric 
18733db8731Seric 	/*
18833db8731Seric 	**  If we have no substitution for the user name in the argument
18933db8731Seric 	**  list, we know that we must supply the names otherwise -- and
19033db8731Seric 	**  SMTP is the answer!!
19133db8731Seric 	*/
19233db8731Seric 
1935dfc646bSeric 	if (*mvp == NULL)
194c579ef51Seric 	{
195c579ef51Seric 		/* running SMTP */
1962c7e1b8dSeric # ifdef SMTP
197c579ef51Seric 		clever = TRUE;
198c579ef51Seric 		*pvp = NULL;
1996c2c3107Seric # else /* SMTP */
20033db8731Seric 		/* oops!  we don't implement SMTP */
2012c7e1b8dSeric 		syserr("SMTP style mailer");
2022c7e1b8dSeric 		return (EX_SOFTWARE);
2036c2c3107Seric # endif /* SMTP */
204c579ef51Seric 	}
2055dfc646bSeric 
2065dfc646bSeric 	/*
2075dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
2085dfc646bSeric 	**  run through our address list and append all the addresses
2095dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
2105dfc646bSeric 	**  always send another copy later.
2115dfc646bSeric 	*/
2125dfc646bSeric 
2135dfc646bSeric 	tobuf[0] = '\0';
214588cad61Seric 	e->e_to = tobuf;
2156259796dSeric 	ctladdr = NULL;
216e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
2175dfc646bSeric 	for (; to != NULL; to = to->q_next)
2185dfc646bSeric 	{
2195dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
22057fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
2215dfc646bSeric 			break;
2225dfc646bSeric 
2235dfc646bSeric 		/* if already sent or not for this host, don't send */
224da2935e1Seric 		if (bitset(QDONTSEND, to->q_flags) ||
225e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
226e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
2275dfc646bSeric 			continue;
2286259796dSeric 
2294b22ea87Seric 		/* avoid overflowing tobuf */
230aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
2314b22ea87Seric 			break;
2324b22ea87Seric 
2336ef48975Seric 		if (tTd(10, 1))
234772e6e50Seric 		{
235772e6e50Seric 			printf("\nsend to ");
236772e6e50Seric 			printaddr(to, FALSE);
237772e6e50Seric 		}
238772e6e50Seric 
2396259796dSeric 		/* compute effective uid/gid when sending */
2407da1035fSeric 		if (to->q_mailer == ProgMailer)
2416259796dSeric 			ctladdr = getctladdr(to);
2426259796dSeric 
2435dfc646bSeric 		user = to->q_user;
244588cad61Seric 		e->e_to = to->q_paddr;
2455dfc646bSeric 		to->q_flags |= QDONTSEND;
246*75f1ade9Seric 		if (tTd(10, 5))
247*75f1ade9Seric 		{
248*75f1ade9Seric 			printf("deliver: QDONTSEND ");
249*75f1ade9Seric 			printaddr(to, FALSE);
250*75f1ade9Seric 		}
2515dfc646bSeric 
2525dfc646bSeric 		/*
2535dfc646bSeric 		**  Check to see that these people are allowed to
2545dfc646bSeric 		**  talk to each other.
2552a6e0786Seric 		*/
2562a6e0786Seric 
25769582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
25869582d2fSeric 		{
25969582d2fSeric 			NoReturn = TRUE;
260672bec4aSeric 			usrerr("Message is too large; %ld bytes max", m->m_maxsize);
26169582d2fSeric 			giveresponse(EX_UNAVAILABLE, m, e);
26269582d2fSeric 			continue;
26369582d2fSeric 		}
264fabb3bd4Seric 		rcode = checkcompat(to, e);
2651793c9c5Seric 		if (rcode != EX_OK)
2665dfc646bSeric 		{
2671793c9c5Seric 			giveresponse(rcode, m, e);
2685dfc646bSeric 			continue;
2695dfc646bSeric 		}
2702a6e0786Seric 
2712a6e0786Seric 		/*
2729ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2739ec9501bSeric 		**	about them.
27425a99e2eSeric 		*/
27525a99e2eSeric 
27657fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
27725a99e2eSeric 		{
2781d8f1806Seric 			stripquotes(user);
2791d8f1806Seric 			stripquotes(host);
28025a99e2eSeric 		}
28125a99e2eSeric 
282cdb828c5Seric 		/* hack attack -- delivermail compatibility */
283cdb828c5Seric 		if (m == ProgMailer && *user == '|')
284cdb828c5Seric 			user++;
285cdb828c5Seric 
28625a99e2eSeric 		/*
2873efaed6eSeric 		**  If an error message has already been given, don't
2883efaed6eSeric 		**	bother to send to this address.
2893efaed6eSeric 		**
2903efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2913efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2923efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2933efaed6eSeric 		*/
2943efaed6eSeric 
2956cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2963efaed6eSeric 			continue;
2973efaed6eSeric 
298f2fec898Seric 		/* save statistics.... */
299588cad61Seric 		markstats(e, to);
300f2fec898Seric 
3013efaed6eSeric 		/*
30225a99e2eSeric 		**  See if this user name is "special".
30325a99e2eSeric 		**	If the user name has a slash in it, assume that this
30451552439Seric 		**	is a file -- send it off without further ado.  Note
30551552439Seric 		**	that this type of addresses is not processed along
30651552439Seric 		**	with the others, so we fudge on the To person.
30725a99e2eSeric 		*/
30825a99e2eSeric 
3092c017f8dSeric 		if (m == FileMailer)
31025a99e2eSeric 		{
311b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
312198d9be0Seric 			giveresponse(rcode, m, e);
313dde5acadSeric 			if (rcode == EX_OK)
314dde5acadSeric 				to->q_flags |= QSENT;
3155dfc646bSeric 			continue;
31625a99e2eSeric 		}
31725a99e2eSeric 
31813bbc08cSeric 		/*
31913bbc08cSeric 		**  Address is verified -- add this user to mailer
32013bbc08cSeric 		**  argv, and add it to the print list of recipients.
32113bbc08cSeric 		*/
32213bbc08cSeric 
323508daeccSeric 		/* link together the chain of recipients */
324508daeccSeric 		to->q_tchain = tochain;
325508daeccSeric 		tochain = to;
326508daeccSeric 
3275dfc646bSeric 		/* create list of users for error messages */
328db8841e9Seric 		(void) strcat(tobuf, ",");
329db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
330588cad61Seric 		define('u', user, e);		/* to user */
331588cad61Seric 		define('z', to->q_home, e);	/* user's home */
3325dfc646bSeric 
333c579ef51Seric 		/*
334508daeccSeric 		**  Expand out this user into argument list.
335c579ef51Seric 		*/
336c579ef51Seric 
337508daeccSeric 		if (!clever)
338c579ef51Seric 		{
339588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3405dfc646bSeric 			*pvp++ = newstr(buf);
3415dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3425dfc646bSeric 			{
3435dfc646bSeric 				/* allow some space for trailing parms */
3445dfc646bSeric 				break;
3455dfc646bSeric 			}
3465dfc646bSeric 		}
347c579ef51Seric 	}
3485dfc646bSeric 
349145b49b1Seric 	/* see if any addresses still exist */
350145b49b1Seric 	if (tobuf[0] == '\0')
351c579ef51Seric 	{
352588cad61Seric 		define('g', (char *) NULL, e);
353c23ed322Seric 		define('<', (char *) NULL, e);
354145b49b1Seric 		return (0);
355c579ef51Seric 	}
356145b49b1Seric 
3575dfc646bSeric 	/* print out messages as full list */
35863780dbdSeric 	e->e_to = tobuf + 1;
3595dfc646bSeric 
3605dfc646bSeric 	/*
3615dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3625dfc646bSeric 	*/
3635dfc646bSeric 
364c579ef51Seric 	while (!clever && *++mvp != NULL)
3655dfc646bSeric 	{
366588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
3675dfc646bSeric 		*pvp++ = newstr(buf);
3685dfc646bSeric 		if (pvp >= &pv[MAXPV])
3695dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3705dfc646bSeric 	}
3715dfc646bSeric 	*pvp++ = NULL;
3725dfc646bSeric 
37325a99e2eSeric 	/*
37425a99e2eSeric 	**  Call the mailer.
3756328bdf7Seric 	**	The argument vector gets built, pipes
37625a99e2eSeric 	**	are created as necessary, and we fork & exec as
3776328bdf7Seric 	**	appropriate.
378c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
37925a99e2eSeric 	*/
38025a99e2eSeric 
38186b26461Seric 	if (ctladdr == NULL)
38286b26461Seric 		ctladdr = &e->e_from;
383134746fbSeric #ifdef NAMED_BIND
3842bcc6d2dSeric 	if (ConfigLevel < 2)
385912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
386134746fbSeric #endif
387b31e7f2bSeric 	mci = openmailer(m, pv, ctladdr, clever, e);
388b31e7f2bSeric 	if (mci == NULL)
389134746fbSeric 	{
390b31e7f2bSeric 		/* catastrophic error */
391b31e7f2bSeric 		rcode = -1;
392b31e7f2bSeric 		goto give_up;
393b31e7f2bSeric 	}
394b31e7f2bSeric 	else if (mci->mci_state != MCIS_OPEN)
395b31e7f2bSeric 	{
396b31e7f2bSeric 		/* couldn't open the mailer */
397b31e7f2bSeric 		rcode = mci->mci_exitstat;
3982a6bc25bSeric 		errno = mci->mci_errno;
399b31e7f2bSeric 		if (rcode == EX_OK)
400b31e7f2bSeric 		{
401b31e7f2bSeric 			/* shouldn't happen */
4026b0f339dSeric 			syserr("deliver: rcode=%d, mci_state=%d, sig=%s",
4036b0f339dSeric 				rcode, mci->mci_state, firstsig);
404b31e7f2bSeric 			rcode = EX_SOFTWARE;
405b31e7f2bSeric 		}
406b31e7f2bSeric 	}
407b31e7f2bSeric 	else if (!clever)
408b31e7f2bSeric 	{
409b31e7f2bSeric 		/*
410b31e7f2bSeric 		**  Format and send message.
411b31e7f2bSeric 		*/
41215d084d5Seric 
413b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
414b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
415b31e7f2bSeric 		putline("\n", mci->mci_out, m);
416b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
417b31e7f2bSeric 
418b31e7f2bSeric 		/* get the exit status */
419b31e7f2bSeric 		rcode = endmailer(mci, pv[0]);
420134746fbSeric 	}
421134746fbSeric 	else
422b31e7f2bSeric #ifdef SMTP
423134746fbSeric 	{
424b31e7f2bSeric 		/*
425b31e7f2bSeric 		**  Send the MAIL FROM: protocol
426b31e7f2bSeric 		*/
42715d084d5Seric 
428b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
429b31e7f2bSeric 		if (rcode == EX_OK)
43075889e88Seric 		{
431ded0d3daSkarels 			register char *t = tobuf;
432ded0d3daSkarels 			register int i;
433ded0d3daSkarels 
434588cad61Seric 			/* send the recipient list */
43563780dbdSeric 			tobuf[0] = '\0';
43675889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
43775889e88Seric 			{
43863780dbdSeric 				e->e_to = to->q_paddr;
43915d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
44075889e88Seric 				{
44183b7ddc9Seric 					markfailure(e, to, i);
442198d9be0Seric 					giveresponse(i, m, e);
44363780dbdSeric 				}
44475889e88Seric 				else
44575889e88Seric 				{
446911693bfSbostic 					*t++ = ',';
447b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
448b31e7f2bSeric 						continue;
449588cad61Seric 				}
450588cad61Seric 			}
451588cad61Seric 
45263780dbdSeric 			/* now send the data */
45363780dbdSeric 			if (tobuf[0] == '\0')
454b31e7f2bSeric 			{
45563780dbdSeric 				e->e_to = NULL;
456b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
457b31e7f2bSeric 					smtprset(m, mci, e);
458b31e7f2bSeric 			}
45975889e88Seric 			else
46075889e88Seric 			{
46163780dbdSeric 				e->e_to = tobuf + 1;
46275889e88Seric 				rcode = smtpdata(m, mci, e);
46363780dbdSeric 			}
46463780dbdSeric 
46563780dbdSeric 			/* now close the connection */
466b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
46715d084d5Seric 				smtpquit(m, mci, e);
46863780dbdSeric 		}
469c579ef51Seric 	}
470b31e7f2bSeric #else /* not SMTP */
471a05b3449Sbostic 	{
472b31e7f2bSeric 		syserr("deliver: need SMTP compiled to use clever mailer");
473b31e7f2bSeric 		rcode = -1;
474b31e7f2bSeric 		goto give_up;
475a05b3449Sbostic 	}
476b31e7f2bSeric #endif /* SMTP */
477134746fbSeric #ifdef NAMED_BIND
4782bcc6d2dSeric 	if (ConfigLevel < 2)
479912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
480134746fbSeric #endif
4815dfc646bSeric 
482b31e7f2bSeric 	/* arrange a return receipt if requested */
483b31e7f2bSeric 	if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags))
484b31e7f2bSeric 	{
485b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
486b31e7f2bSeric 		/* do we want to send back more info? */
487b31e7f2bSeric 	}
488b31e7f2bSeric 
489c77d1c25Seric 	/*
49063780dbdSeric 	**  Do final status disposal.
49163780dbdSeric 	**	We check for something in tobuf for the SMTP case.
492c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
493c77d1c25Seric 	**		addressees.
494c77d1c25Seric 	*/
495c77d1c25Seric 
496b31e7f2bSeric   give_up:
49763780dbdSeric 	if (tobuf[0] != '\0')
498198d9be0Seric 		giveresponse(rcode, m, e);
499772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
500b31e7f2bSeric 	{
501dde5acadSeric 		if (rcode != EX_OK)
50283b7ddc9Seric 			markfailure(e, to, rcode);
503dde5acadSeric 		else
504655518ecSeric 		{
505dde5acadSeric 			to->q_flags |= QSENT;
506655518ecSeric 			e->e_nsent++;
507655518ecSeric 		}
508b31e7f2bSeric 	}
509b31e7f2bSeric 
510b31e7f2bSeric 	/*
511b31e7f2bSeric 	**  Restore state and return.
512b31e7f2bSeric 	*/
513c77d1c25Seric 
51435490626Seric 	errno = 0;
515588cad61Seric 	define('g', (char *) NULL, e);
516c23ed322Seric 	define('<', (char *) NULL, e);
5175826d9d3Seric 	return (rcode);
51825a99e2eSeric }
5195dfc646bSeric /*
52083b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
52183b7ddc9Seric **
52283b7ddc9Seric **	Parameters:
52383b7ddc9Seric **		e -- the envelope we are sending.
52483b7ddc9Seric **		q -- the address to mark.
52583b7ddc9Seric **		rcode -- the code signifying the particular failure.
52683b7ddc9Seric **
52783b7ddc9Seric **	Returns:
52883b7ddc9Seric **		none.
52983b7ddc9Seric **
53083b7ddc9Seric **	Side Effects:
53183b7ddc9Seric **		marks the address (and possibly the envelope) with the
53283b7ddc9Seric **			failure so that an error will be returned or
53383b7ddc9Seric **			the message will be queued, as appropriate.
53483b7ddc9Seric */
53583b7ddc9Seric 
53683b7ddc9Seric markfailure(e, q, rcode)
53783b7ddc9Seric 	register ENVELOPE *e;
53883b7ddc9Seric 	register ADDRESS *q;
53983b7ddc9Seric 	int rcode;
54083b7ddc9Seric {
54183b7ddc9Seric 	if (rcode == EX_OK)
54283b7ddc9Seric 		return;
543ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
54483b7ddc9Seric 		q->q_flags |= QBADADDR;
54583b7ddc9Seric 	else if (curtime() > e->e_ctime + TimeOut)
54683b7ddc9Seric 	{
54783b7ddc9Seric 		extern char *pintvl();
548198d9be0Seric 		char buf[MAXLINE];
54983b7ddc9Seric 
55083b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
551198d9be0Seric 		{
552198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
55383b7ddc9Seric 				pintvl(TimeOut, FALSE));
554198d9be0Seric 			if (e->e_message != NULL)
555198d9be0Seric 				free(e->e_message);
556198d9be0Seric 			e->e_message = newstr(buf);
557198d9be0Seric 			message(Arpa_Info, buf);
558198d9be0Seric 		}
55983b7ddc9Seric 		q->q_flags |= QBADADDR;
56083b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
561c13b5dfcSeric 		fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr);
56283b7ddc9Seric 	}
56383b7ddc9Seric 	else
56483b7ddc9Seric 		q->q_flags |= QQUEUEUP;
56583b7ddc9Seric }
56683b7ddc9Seric /*
56732d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
56832d19d43Seric **
56932d19d43Seric **	This MUST be a macro, since after a vfork we are running
57032d19d43Seric **	two processes on the same stack!!!
57132d19d43Seric **
57232d19d43Seric **	Parameters:
57332d19d43Seric **		none.
57432d19d43Seric **
57532d19d43Seric **	Returns:
57632d19d43Seric **		From a macro???  You've got to be kidding!
57732d19d43Seric **
57832d19d43Seric **	Side Effects:
57932d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
58032d19d43Seric **			pid of child in parent, zero in child.
58132d19d43Seric **			-1 on unrecoverable error.
58232d19d43Seric **
58332d19d43Seric **	Notes:
58432d19d43Seric **		I'm awfully sorry this looks so awful.  That's
58532d19d43Seric **		vfork for you.....
58632d19d43Seric */
58732d19d43Seric 
58832d19d43Seric # define NFORKTRIES	5
58984f7cd1bSeric 
59084f7cd1bSeric # ifndef FORK
59184f7cd1bSeric # define FORK	fork
59284f7cd1bSeric # endif
59332d19d43Seric 
59432d19d43Seric # define DOFORK(fORKfN) \
59532d19d43Seric {\
59632d19d43Seric 	register int i;\
59732d19d43Seric \
59811799049Seric 	for (i = NFORKTRIES; --i >= 0; )\
59932d19d43Seric 	{\
60032d19d43Seric 		pid = fORKfN();\
60132d19d43Seric 		if (pid >= 0)\
60232d19d43Seric 			break;\
60311799049Seric 		if (i > 0)\
6046c4635f6Seric 			sleep((unsigned) NFORKTRIES - i);\
60532d19d43Seric 	}\
60632d19d43Seric }
60732d19d43Seric /*
6082ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
6092ed72599Seric **
6102ed72599Seric **	Parameters:
6112ed72599Seric **		none.
6122ed72599Seric **
6132ed72599Seric **	Returns:
6142ed72599Seric **		pid of child in parent.
6152ed72599Seric **		zero in child.
6162ed72599Seric **		-1 on error.
6172ed72599Seric **
6182ed72599Seric **	Side Effects:
6192ed72599Seric **		returns twice, once in parent and once in child.
6202ed72599Seric */
6212ed72599Seric 
6222ed72599Seric dofork()
6232ed72599Seric {
6242ed72599Seric 	register int pid;
6252ed72599Seric 
6262ed72599Seric 	DOFORK(fork);
6272ed72599Seric 	return (pid);
6282ed72599Seric }
6292ed72599Seric /*
630c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
631c579ef51Seric **
632c579ef51Seric **	We should never get fatal errors (e.g., segmentation
633c579ef51Seric **	violation), so we report those specially.  For other
634c579ef51Seric **	errors, we choose a status message (into statmsg),
635c579ef51Seric **	and if it represents an error, we print it.
636c579ef51Seric **
637c579ef51Seric **	Parameters:
638c579ef51Seric **		pid -- pid of mailer.
639c579ef51Seric **		name -- name of mailer (for error messages).
640c579ef51Seric **
641c579ef51Seric **	Returns:
642c579ef51Seric **		exit code of mailer.
643c579ef51Seric **
644c579ef51Seric **	Side Effects:
645c579ef51Seric **		none.
646c579ef51Seric */
647c579ef51Seric 
64875889e88Seric endmailer(mci, name)
649b31e7f2bSeric 	register MCI *mci;
650c579ef51Seric 	char *name;
651c579ef51Seric {
652588cad61Seric 	int st;
653c579ef51Seric 
65475889e88Seric 	/* close any connections */
65575889e88Seric 	if (mci->mci_in != NULL)
65675889e88Seric 		(void) fclose(mci->mci_in);
65775889e88Seric 	if (mci->mci_out != NULL)
65875889e88Seric 		(void) fclose(mci->mci_out);
65975889e88Seric 	mci->mci_in = mci->mci_out = NULL;
66075889e88Seric 	mci->mci_state = MCIS_CLOSED;
66175889e88Seric 
66233db8731Seric 	/* in the IPC case there is nothing to wait for */
66375889e88Seric 	if (mci->mci_pid == 0)
66433db8731Seric 		return (EX_OK);
66533db8731Seric 
66633db8731Seric 	/* wait for the mailer process to die and collect status */
66775889e88Seric 	st = waitfor(mci->mci_pid);
668588cad61Seric 	if (st == -1)
66978de67c1Seric 	{
670588cad61Seric 		syserr("endmailer %s: wait", name);
671588cad61Seric 		return (EX_SOFTWARE);
672c579ef51Seric 	}
67333db8731Seric 
67433db8731Seric 	/* see if it died a horrid death */
675c579ef51Seric 	if ((st & 0377) != 0)
676c579ef51Seric 	{
6775f73204aSeric 		syserr("mailer %s died with signal %o", name, st);
6785f73204aSeric 		ExitStat = EX_TEMPFAIL;
6795f73204aSeric 		return (EX_TEMPFAIL);
680c579ef51Seric 	}
68133db8731Seric 
68233db8731Seric 	/* normal death -- return status */
683588cad61Seric 	st = (st >> 8) & 0377;
684588cad61Seric 	return (st);
685c579ef51Seric }
686c579ef51Seric /*
687c579ef51Seric **  OPENMAILER -- open connection to mailer.
688c579ef51Seric **
689c579ef51Seric **	Parameters:
690c579ef51Seric **		m -- mailer descriptor.
691c579ef51Seric **		pvp -- parameter vector to pass to mailer.
692c579ef51Seric **		ctladdr -- controlling address for user.
693c579ef51Seric **		clever -- create a full duplex connection.
694c579ef51Seric **
695c579ef51Seric **	Returns:
69675889e88Seric **		The mail connection info struct for this connection.
69775889e88Seric **		NULL on failure.
698c579ef51Seric **
699c579ef51Seric **	Side Effects:
700c579ef51Seric **		creates a mailer in a subprocess.
701c579ef51Seric */
702c579ef51Seric 
703b31e7f2bSeric MCI *
704b31e7f2bSeric openmailer(m, pvp, ctladdr, clever, e)
705588cad61Seric 	MAILER *m;
706c579ef51Seric 	char **pvp;
707c579ef51Seric 	ADDRESS *ctladdr;
708c579ef51Seric 	bool clever;
709b31e7f2bSeric 	ENVELOPE *e;
710c579ef51Seric {
7115dfc646bSeric 	int pid;
712b31e7f2bSeric 	register MCI *mci;
713f8952a83Seric 	int mpvect[2];
714c579ef51Seric 	int rpvect[2];
7155dfc646bSeric 	extern FILE *fdopen();
7165dfc646bSeric 
7176ef48975Seric 	if (tTd(11, 1))
7185dfc646bSeric 	{
7198c57e552Seric 		printf("openmailer:");
7205dfc646bSeric 		printav(pvp);
7215dfc646bSeric 	}
72235490626Seric 	errno = 0;
7235dfc646bSeric 
724ef66a9d0Seric 	CurHostName = m->m_mailer;
725ef66a9d0Seric 
72633db8731Seric 	/*
72733db8731Seric 	**  Deal with the special case of mail handled through an IPC
72833db8731Seric 	**  connection.
72933db8731Seric 	**	In this case we don't actually fork.  We must be
73033db8731Seric 	**	running SMTP for this to work.  We will return a
73133db8731Seric 	**	zero pid to indicate that we are running IPC.
732e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
73333db8731Seric 	*/
73433db8731Seric 
735e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
736e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
737e7c1bd78Seric 	{
738b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
7392a087e90Seric 		bzero((char *) mci, sizeof *mci);
74075889e88Seric 		mci->mci_in = stdin;
74175889e88Seric 		mci->mci_out = stdout;
742b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
74315d084d5Seric 		mci->mci_mailer = m;
744e7c1bd78Seric 	}
745b31e7f2bSeric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
746914346b1Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
74733db8731Seric 	{
74884f7cd1bSeric #ifdef DAEMON
749e103b48fSeric 		register int i;
7501277f9a8Seric 		register u_short port;
751e103b48fSeric 		char *curhost;
752b31e7f2bSeric 		extern MCI *mci_get();
753e103b48fSeric 		extern char *hostsignature();
75433db8731Seric 
755ef66a9d0Seric 		CurHostName = pvp[1];
756e103b48fSeric 		curhost = hostsignature(m, pvp[1], e);
757b31e7f2bSeric 
75833db8731Seric 		if (!clever)
75933db8731Seric 			syserr("non-clever IPC");
76093b6e3cfSeric 		if (pvp[2] != NULL)
7611277f9a8Seric 			port = atoi(pvp[2]);
76293b6e3cfSeric 		else
7631277f9a8Seric 			port = 0;
764e103b48fSeric 		while (*curhost != '\0')
765ebc61751Sbloom 		{
766e103b48fSeric 			register char *p;
767e103b48fSeric 			char hostbuf[MAXNAME];
768e103b48fSeric 
769e103b48fSeric 			/* pull the next host from the signature */
770e103b48fSeric 			p = strchr(curhost, ':');
771e103b48fSeric 			if (p == NULL)
772e103b48fSeric 				p = &curhost[strlen(curhost)];
773e103b48fSeric 			strncpy(hostbuf, curhost, p - curhost);
774e103b48fSeric 			hostbuf[p - curhost] = '\0';
775e103b48fSeric 			if (*p != '\0')
776e103b48fSeric 				p++;
777e103b48fSeric 			curhost = p;
778e103b48fSeric 
77945a8316eSeric 			/* see if we already know that this host is fried */
780e103b48fSeric 			CurHostName = hostbuf;
781e103b48fSeric 			mci = mci_get(hostbuf, m);
782b31e7f2bSeric 			if (mci->mci_state != MCIS_CLOSED)
7831fa7c2ebSeric 			{
7841fa7c2ebSeric 				if (tTd(11, 1))
7851fa7c2ebSeric 				{
7861fa7c2ebSeric 					printf("openmailer: ");
7871fa7c2ebSeric 					mci_dump(mci);
7881fa7c2ebSeric 				}
78975889e88Seric 				return mci;
7901fa7c2ebSeric 			}
79115d084d5Seric 			mci->mci_mailer = m;
792b31e7f2bSeric 			if (mci->mci_exitstat != EX_OK)
793b31e7f2bSeric 				continue;
794b31e7f2bSeric 
79575889e88Seric 			/* try the connection */
796e103b48fSeric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
797914346b1Seric 			message(Arpa_Info, "Connecting to %s (%s)...",
798e103b48fSeric 				hostbuf, m->m_name);
799e103b48fSeric 			i = makeconnection(hostbuf, port, mci,
800914346b1Seric 				bitnset(M_SECURE_PORT, m->m_flags));
80175889e88Seric 			mci->mci_exitstat = i;
80275889e88Seric 			mci->mci_errno = errno;
80375889e88Seric 			if (i == EX_OK)
804b31e7f2bSeric 			{
805b31e7f2bSeric 				mci->mci_state = MCIS_OPENING;
806b31e7f2bSeric 				mci_cache(mci);
807b31e7f2bSeric 				break;
808b31e7f2bSeric 			}
809b31e7f2bSeric 			else if (tTd(11, 1))
810b31e7f2bSeric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
811b31e7f2bSeric 					i, errno);
812b31e7f2bSeric 
81375889e88Seric 
8145f73204aSeric 			/* enter status of this host */
81575889e88Seric 			setstat(i);
816ed854c7bSeric 		}
8172a087e90Seric 		mci->mci_pid = 0;
818b31e7f2bSeric #else /* no DAEMON */
819588cad61Seric 		syserr("openmailer: no IPC");
8201fa7c2ebSeric 		if (tTd(11, 1))
8211fa7c2ebSeric 			printf("openmailer: NULL\n");
82275889e88Seric 		return NULL;
823b31e7f2bSeric #endif /* DAEMON */
824588cad61Seric 	}
825b31e7f2bSeric 	else
826b31e7f2bSeric 	{
8276328bdf7Seric 		/* create a pipe to shove the mail through */
828f8952a83Seric 		if (pipe(mpvect) < 0)
82925a99e2eSeric 		{
830588cad61Seric 			syserr("openmailer: pipe (to mailer)");
8311fa7c2ebSeric 			if (tTd(11, 1))
8321fa7c2ebSeric 				printf("openmailer: NULL\n");
83375889e88Seric 			return NULL;
83425a99e2eSeric 		}
835c579ef51Seric 
836c579ef51Seric 		/* if this mailer speaks smtp, create a return pipe */
837c579ef51Seric 		if (clever && pipe(rpvect) < 0)
838c579ef51Seric 		{
839588cad61Seric 			syserr("openmailer: pipe (from mailer)");
840c579ef51Seric 			(void) close(mpvect[0]);
841c579ef51Seric 			(void) close(mpvect[1]);
8421fa7c2ebSeric 			if (tTd(11, 1))
8431fa7c2ebSeric 				printf("openmailer: NULL\n");
84475889e88Seric 			return NULL;
845c579ef51Seric 		}
846c579ef51Seric 
84733db8731Seric 		/*
84833db8731Seric 		**  Actually fork the mailer process.
84933db8731Seric 		**	DOFORK is clever about retrying.
8506984bfddSeric 		**
8516984bfddSeric 		**	Dispose of SIGCHLD signal catchers that may be laying
8526984bfddSeric 		**	around so that endmail will get it.
85333db8731Seric 		*/
85433db8731Seric 
855b31e7f2bSeric 		if (e->e_xfp != NULL)
856b31e7f2bSeric 			(void) fflush(e->e_xfp);		/* for debugging */
857588cad61Seric 		(void) fflush(stdout);
8586984bfddSeric # ifdef SIGCHLD
8596984bfddSeric 		(void) signal(SIGCHLD, SIG_DFL);
8606c2c3107Seric # endif /* SIGCHLD */
86184f7cd1bSeric 		DOFORK(FORK);
862f129ec7dSeric 		/* pid is set by DOFORK */
86325a99e2eSeric 		if (pid < 0)
86425a99e2eSeric 		{
86533db8731Seric 			/* failure */
866588cad61Seric 			syserr("openmailer: cannot fork");
867f8952a83Seric 			(void) close(mpvect[0]);
868f8952a83Seric 			(void) close(mpvect[1]);
869c579ef51Seric 			if (clever)
870c579ef51Seric 			{
871c579ef51Seric 				(void) close(rpvect[0]);
872c579ef51Seric 				(void) close(rpvect[1]);
873c579ef51Seric 			}
8741fa7c2ebSeric 			if (tTd(11, 1))
8751fa7c2ebSeric 				printf("openmailer: NULL\n");
87675889e88Seric 			return NULL;
87725a99e2eSeric 		}
87825a99e2eSeric 		else if (pid == 0)
87925a99e2eSeric 		{
88013088b9fSeric 			int i;
881dafbc479Seric 			int saveerrno;
882d7a0480eSeric 			char *env[2];
8835f73204aSeric 			extern int DtableSize;
88413088b9fSeric 
88525a99e2eSeric 			/* child -- set up input & exec mailer */
88603ab8e55Seric 			/* make diagnostic output be standard output */
8878f0e7860Seric 			(void) signal(SIGINT, SIG_IGN);
8888f0e7860Seric 			(void) signal(SIGHUP, SIG_IGN);
8890984da9fSeric 			(void) signal(SIGTERM, SIG_DFL);
890f8952a83Seric 
891b31e7f2bSeric 			/* arrange to filter std & diag output of command */
892c579ef51Seric 			if (clever)
893c579ef51Seric 			{
894c579ef51Seric 				(void) close(rpvect[0]);
895c579ef51Seric 				(void) close(1);
896c579ef51Seric 				(void) dup(rpvect[1]);
897c579ef51Seric 				(void) close(rpvect[1]);
898c579ef51Seric 			}
899276723a8Seric 			else if (OpMode == MD_SMTP || HoldErrs)
900f8952a83Seric 			{
901588cad61Seric 				/* put mailer output in transcript */
902f8952a83Seric 				(void) close(1);
903b31e7f2bSeric 				(void) dup(fileno(e->e_xfp));
904f8952a83Seric 			}
905db8841e9Seric 			(void) close(2);
906db8841e9Seric 			(void) dup(1);
907f8952a83Seric 
908f8952a83Seric 			/* arrange to get standard input */
909f8952a83Seric 			(void) close(mpvect[1]);
910db8841e9Seric 			(void) close(0);
911f8952a83Seric 			if (dup(mpvect[0]) < 0)
91225a99e2eSeric 			{
91325a99e2eSeric 				syserr("Cannot dup to zero!");
914a590b978Seric 				_exit(EX_OSERR);
91525a99e2eSeric 			}
916f8952a83Seric 			(void) close(mpvect[0]);
91757fc6f17Seric 			if (!bitnset(M_RESTR, m->m_flags))
9180984da9fSeric 			{
91953e3fa05Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
920e36b99e2Seric 				{
921e36b99e2Seric 					(void) setgid(DefGid);
922898a126bSbostic 					(void) initgroups(DefUser, DefGid);
923e36b99e2Seric 					(void) setuid(DefUid);
924e36b99e2Seric 				}
925e36b99e2Seric 				else
92669f29479Seric 				{
927e36b99e2Seric 					(void) setgid(ctladdr->q_gid);
928898a126bSbostic 					(void) initgroups(ctladdr->q_ruser?
929898a126bSbostic 						ctladdr->q_ruser: ctladdr->q_user,
930898a126bSbostic 						ctladdr->q_gid);
931e36b99e2Seric 					(void) setuid(ctladdr->q_uid);
93269f29479Seric 				}
9330984da9fSeric 			}
934588cad61Seric 
93513088b9fSeric 			/* arrange for all the files to be closed */
936b31e7f2bSeric 			for (i = 3; i < DtableSize; i++)
937b31e7f2bSeric 			{
938fccb3f7aSbostic 				register int j;
939fccb3f7aSbostic 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
940fccb3f7aSbostic 					(void)fcntl(i, F_SETFD, j|1);
941fccb3f7aSbostic 			}
94233db8731Seric 
94333db8731Seric 			/* try to execute the mailer */
944d7a0480eSeric 			env[0] = "AGENT=sendmail";
945d7a0480eSeric 			env[1] = NULL;
946d7a0480eSeric 			execve(m->m_mailer, pvp, env);
947dafbc479Seric 			saveerrno = errno;
94813088b9fSeric 			syserr("Cannot exec %s", m->m_mailer);
94995b76e4bSeric 			if (m == LocalMailer)
95055f33c03Seric 				_exit(EX_TEMPFAIL);
951dafbc479Seric 			switch (saveerrno)
95295b76e4bSeric 			{
95395b76e4bSeric 			  case EIO:
95495b76e4bSeric 			  case EAGAIN:
95595b76e4bSeric 			  case ENOMEM:
95695b76e4bSeric # ifdef EPROCLIM
95795b76e4bSeric 			  case EPROCLIM:
95895b76e4bSeric # endif
95995b76e4bSeric 				_exit(EX_TEMPFAIL);
96095b76e4bSeric 			}
961a590b978Seric 			_exit(EX_UNAVAILABLE);
96225a99e2eSeric 		}
96325a99e2eSeric 
964f8952a83Seric 		/*
965c579ef51Seric 		**  Set up return value.
966f8952a83Seric 		*/
967f8952a83Seric 
968b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
9692a087e90Seric 		bzero((char *) mci, sizeof *mci);
97015d084d5Seric 		mci->mci_mailer = m;
971b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9722a087e90Seric 		mci->mci_pid = pid;
973f8952a83Seric 		(void) close(mpvect[0]);
97475889e88Seric 		mci->mci_out = fdopen(mpvect[1], "w");
975c579ef51Seric 		if (clever)
97625a99e2eSeric 		{
977c579ef51Seric 			(void) close(rpvect[1]);
97875889e88Seric 			mci->mci_in = fdopen(rpvect[0], "r");
97975889e88Seric 		}
98075889e88Seric 		else
98175889e88Seric 		{
98275889e88Seric 			mci->mci_flags |= MCIF_TEMP;
98375889e88Seric 			mci->mci_in = NULL;
98475889e88Seric 		}
985b31e7f2bSeric 	}
986b31e7f2bSeric 
987b31e7f2bSeric 	/*
988b31e7f2bSeric 	**  If we are in SMTP opening state, send initial protocol.
989b31e7f2bSeric 	*/
990b31e7f2bSeric 
991b31e7f2bSeric 	if (clever && mci->mci_state != MCIS_CLOSED)
992b31e7f2bSeric 	{
993b31e7f2bSeric 		smtpinit(m, mci, e);
994b31e7f2bSeric 	}
9951fa7c2ebSeric 	if (tTd(11, 1))
9961fa7c2ebSeric 	{
9971fa7c2ebSeric 		printf("openmailer: ");
9981fa7c2ebSeric 		mci_dump(mci);
9991fa7c2ebSeric 	}
1000c579ef51Seric 
100175889e88Seric 	return mci;
100225a99e2eSeric }
100325a99e2eSeric /*
100425a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
100525a99e2eSeric **
100625a99e2eSeric **	Parameters:
100725a99e2eSeric **		stat -- the status code from the mailer (high byte
100825a99e2eSeric **			only; core dumps must have been taken care of
100925a99e2eSeric **			already).
101025a99e2eSeric **		m -- the mailer descriptor for this mailer.
101125a99e2eSeric **
101225a99e2eSeric **	Returns:
1013db8841e9Seric **		none.
101425a99e2eSeric **
101525a99e2eSeric **	Side Effects:
1016c1f9df2cSeric **		Errors may be incremented.
101725a99e2eSeric **		ExitStat may be set.
101825a99e2eSeric */
101925a99e2eSeric 
1020198d9be0Seric giveresponse(stat, m, e)
102125a99e2eSeric 	int stat;
1022588cad61Seric 	register MAILER *m;
1023198d9be0Seric 	ENVELOPE *e;
102425a99e2eSeric {
102525a99e2eSeric 	register char *statmsg;
102625a99e2eSeric 	extern char *SysExMsg[];
102725a99e2eSeric 	register int i;
1028d4bd8f0eSbostic 	extern int N_SysEx;
1029d4bd8f0eSbostic #ifdef NAMED_BIND
1030d4bd8f0eSbostic 	extern int h_errno;
1031d4bd8f0eSbostic #endif
1032198d9be0Seric 	char buf[MAXLINE];
103325a99e2eSeric 
10347d1fc79dSeric #ifdef lint
10357d1fc79dSeric 	if (m == NULL)
10367d1fc79dSeric 		return;
10377d1fc79dSeric #endif lint
10387d1fc79dSeric 
103913bbc08cSeric 	/*
104013bbc08cSeric 	**  Compute status message from code.
104113bbc08cSeric 	*/
104213bbc08cSeric 
104325a99e2eSeric 	i = stat - EX__BASE;
1044588cad61Seric 	if (stat == 0)
1045588cad61Seric 		statmsg = "250 Sent";
1046588cad61Seric 	else if (i < 0 || i > N_SysEx)
1047588cad61Seric 	{
1048588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1049588cad61Seric 		stat = EX_UNAVAILABLE;
1050588cad61Seric 		statmsg = buf;
1051588cad61Seric 	}
1052198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1053198d9be0Seric 	{
10548557d168Seric 		(void) strcpy(buf, SysExMsg[i]);
1055d4bd8f0eSbostic #ifdef NAMED_BIND
1056f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1057f28da541Smiriam 		{
1058f28da541Smiriam 			extern char *errstring();
1059f28da541Smiriam 
1060f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1061f28da541Smiriam 		}
1062f28da541Smiriam 		else
1063d4bd8f0eSbostic #endif
1064f28da541Smiriam 		{
10658557d168Seric 			if (errno != 0)
1066198d9be0Seric 			{
106787c9b3e7Seric 				extern char *errstring();
10688557d168Seric 
1069d87e85f3Seric 				statmsg = errstring(errno);
1070d87e85f3Seric 			}
1071d87e85f3Seric 			else
1072d87e85f3Seric 			{
1073d87e85f3Seric #ifdef SMTP
1074d87e85f3Seric 				extern char SmtpError[];
1075d87e85f3Seric 
1076d87e85f3Seric 				statmsg = SmtpError;
10776c2c3107Seric #else /* SMTP */
1078d87e85f3Seric 				statmsg = NULL;
10796c2c3107Seric #endif /* SMTP */
1080d87e85f3Seric 			}
1081f28da541Smiriam 		}
1082d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1083d87e85f3Seric 		{
108487c9b3e7Seric 			(void) strcat(buf, ": ");
1085d87e85f3Seric 			(void) strcat(buf, statmsg);
10868557d168Seric 		}
1087198d9be0Seric 		statmsg = buf;
1088198d9be0Seric 	}
108925a99e2eSeric 	else
1090d87e85f3Seric 	{
109125a99e2eSeric 		statmsg = SysExMsg[i];
1092d87e85f3Seric 	}
1093588cad61Seric 
1094588cad61Seric 	/*
1095588cad61Seric 	**  Print the message as appropriate
1096588cad61Seric 	*/
1097588cad61Seric 
1098198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
10995826d9d3Seric 		message(Arpa_Info, &statmsg[4]);
110025a99e2eSeric 	else
110125a99e2eSeric 	{
1102c1f9df2cSeric 		Errors++;
11035826d9d3Seric 		usrerr(statmsg);
110425a99e2eSeric 	}
110525a99e2eSeric 
110625a99e2eSeric 	/*
110725a99e2eSeric 	**  Final cleanup.
110825a99e2eSeric 	**	Log a record of the transaction.  Compute the new
110925a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
111025a99e2eSeric 	**	that.
111125a99e2eSeric 	*/
111225a99e2eSeric 
111361f5a1d4Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
1114b31e7f2bSeric 		logdelivery(&statmsg[4], e);
1115eb238f8cSeric 
1116eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1117eb238f8cSeric 		setstat(stat);
1118198d9be0Seric 	if (stat != EX_OK)
1119198d9be0Seric 	{
1120198d9be0Seric 		if (e->e_message != NULL)
1121198d9be0Seric 			free(e->e_message);
1122198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1123198d9be0Seric 	}
11248557d168Seric 	errno = 0;
1125d4bd8f0eSbostic #ifdef NAMED_BIND
1126f28da541Smiriam 	h_errno = 0;
1127d4bd8f0eSbostic #endif
1128eb238f8cSeric }
1129eb238f8cSeric /*
1130eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1131eb238f8cSeric **
1132eb238f8cSeric **	Parameters:
1133eb238f8cSeric **		stat -- the message to print for the status
1134eb238f8cSeric **
1135eb238f8cSeric **	Returns:
1136eb238f8cSeric **		none
1137eb238f8cSeric **
1138eb238f8cSeric **	Side Effects:
1139eb238f8cSeric **		none
1140eb238f8cSeric */
1141eb238f8cSeric 
1142b31e7f2bSeric logdelivery(stat, e)
1143eb238f8cSeric 	char *stat;
1144b31e7f2bSeric 	register ENVELOPE *e;
11455cf56be3Seric {
11465cf56be3Seric 	extern char *pintvl();
11475cf56be3Seric 
1148eb238f8cSeric # ifdef LOG
1149b31e7f2bSeric 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", e->e_id,
1150b31e7f2bSeric 	       e->e_to, pintvl(curtime() - e->e_ctime, TRUE), stat);
11516c2c3107Seric # endif /* LOG */
115225a99e2eSeric }
115325a99e2eSeric /*
115451552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
115525a99e2eSeric **
115651552439Seric **	This can be made an arbitrary message separator by changing $l
115751552439Seric **
11589b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
11599b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
11609b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
11619b6c17a6Seric **	this kind of antique garbage????
116225a99e2eSeric **
116325a99e2eSeric **	Parameters:
116451552439Seric **		fp -- the file to output to.
116551552439Seric **		m -- the mailer describing this entry.
116625a99e2eSeric **
116725a99e2eSeric **	Returns:
116851552439Seric **		none
116925a99e2eSeric **
117025a99e2eSeric **	Side Effects:
117151552439Seric **		outputs some text to fp.
117225a99e2eSeric */
117325a99e2eSeric 
1174b31e7f2bSeric putfromline(fp, m, e)
117551552439Seric 	register FILE *fp;
117651552439Seric 	register MAILER *m;
1177b31e7f2bSeric 	ENVELOPE *e;
117825a99e2eSeric {
11799b6c17a6Seric 	char *template = "\001l\n";
118051552439Seric 	char buf[MAXLINE];
118125a99e2eSeric 
118257fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
118351552439Seric 		return;
118413bbc08cSeric 
11852c7e1b8dSeric # ifdef UGLYUUCP
118657fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
118774b6e67bSeric 	{
1188ea09d6edSeric 		char *bang;
1189ea09d6edSeric 		char xbuf[MAXLINE];
119074b6e67bSeric 
1191b31e7f2bSeric 		expand("\001<", buf, &buf[sizeof buf - 1], e);
11926c2c3107Seric 		bang = strchr(buf, '!');
119374b6e67bSeric 		if (bang == NULL)
1194ea09d6edSeric 			syserr("No ! in UUCP! (%s)", buf);
119574b6e67bSeric 		else
1196588cad61Seric 		{
1197ea09d6edSeric 			*bang++ = '\0';
11989b6c17a6Seric 			(void) sprintf(xbuf, "From %s  \001d remote from %s\n", bang, buf);
1199ea09d6edSeric 			template = xbuf;
120074b6e67bSeric 		}
1201588cad61Seric 	}
12026c2c3107Seric # endif /* UGLYUUCP */
1203b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
120477b52738Seric 	putline(buf, fp, m);
1205bc6e2962Seric }
1206bc6e2962Seric /*
120751552439Seric **  PUTBODY -- put the body of a message.
120851552439Seric **
120951552439Seric **	Parameters:
121051552439Seric **		fp -- file to output onto.
121177b52738Seric **		m -- a mailer descriptor to control output format.
12129a6a5f55Seric **		e -- the envelope to put out.
121351552439Seric **
121451552439Seric **	Returns:
121551552439Seric **		none.
121651552439Seric **
121751552439Seric **	Side Effects:
121851552439Seric **		The message is written onto fp.
121951552439Seric */
122051552439Seric 
122177b52738Seric putbody(fp, m, e)
122251552439Seric 	FILE *fp;
1223588cad61Seric 	MAILER *m;
12249a6a5f55Seric 	register ENVELOPE *e;
122551552439Seric {
122677b52738Seric 	char buf[MAXLINE];
122751552439Seric 
122851552439Seric 	/*
122951552439Seric 	**  Output the body of the message
123051552439Seric 	*/
123151552439Seric 
12329a6a5f55Seric 	if (e->e_dfp == NULL)
123351552439Seric 	{
12349a6a5f55Seric 		if (e->e_df != NULL)
12359a6a5f55Seric 		{
12369a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
12379a6a5f55Seric 			if (e->e_dfp == NULL)
12388f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
12398f9146b0Srick 				e->e_df, e->e_to, e->e_from);
12409a6a5f55Seric 		}
12419a6a5f55Seric 		else
124277b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
12439a6a5f55Seric 	}
12449a6a5f55Seric 	if (e->e_dfp != NULL)
12459a6a5f55Seric 	{
12469a6a5f55Seric 		rewind(e->e_dfp);
124777b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
124824fc8aeeSeric 		{
124924fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1250d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
12513462ad9eSeric 				(void) putc('>', fp);
125277b52738Seric 			putline(buf, fp, m);
125324fc8aeeSeric 		}
125451552439Seric 
12559a6a5f55Seric 		if (ferror(e->e_dfp))
125651552439Seric 		{
125751552439Seric 			syserr("putbody: read error");
125851552439Seric 			ExitStat = EX_IOERR;
125951552439Seric 		}
126051552439Seric 	}
126151552439Seric 
126251552439Seric 	(void) fflush(fp);
126351552439Seric 	if (ferror(fp) && errno != EPIPE)
126451552439Seric 	{
126551552439Seric 		syserr("putbody: write error");
126651552439Seric 		ExitStat = EX_IOERR;
126751552439Seric 	}
126851552439Seric 	errno = 0;
126925a99e2eSeric }
127025a99e2eSeric /*
127125a99e2eSeric **  MAILFILE -- Send a message to a file.
127225a99e2eSeric **
1273f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1274f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1275f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1276f129ec7dSeric **	sendmail runs as root.
1277f129ec7dSeric **
1278588cad61Seric **	This could be done as a subordinate mailer, except that it
1279588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1280588cad61Seric **	view this as being sufficiently important as to include it
1281588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1282588cad61Seric **	to create another process plus some pipes to save the message.
1283588cad61Seric **
128425a99e2eSeric **	Parameters:
128525a99e2eSeric **		filename -- the name of the file to send to.
12866259796dSeric **		ctladdr -- the controlling address header -- includes
12876259796dSeric **			the userid/groupid to be when sending.
128825a99e2eSeric **
128925a99e2eSeric **	Returns:
129025a99e2eSeric **		The exit code associated with the operation.
129125a99e2eSeric **
129225a99e2eSeric **	Side Effects:
129325a99e2eSeric **		none.
129425a99e2eSeric */
129525a99e2eSeric 
1296b31e7f2bSeric mailfile(filename, ctladdr, e)
129725a99e2eSeric 	char *filename;
12986259796dSeric 	ADDRESS *ctladdr;
1299b31e7f2bSeric 	register ENVELOPE *e;
130025a99e2eSeric {
130125a99e2eSeric 	register FILE *f;
130232d19d43Seric 	register int pid;
130315d084d5Seric 	int mode;
130425a99e2eSeric 
130532d19d43Seric 	/*
130632d19d43Seric 	**  Fork so we can change permissions here.
130732d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
130832d19d43Seric 	**	the complications of calling subroutines, etc.
130932d19d43Seric 	*/
131032d19d43Seric 
131132d19d43Seric 	DOFORK(fork);
131232d19d43Seric 
131332d19d43Seric 	if (pid < 0)
131432d19d43Seric 		return (EX_OSERR);
131532d19d43Seric 	else if (pid == 0)
131632d19d43Seric 	{
131732d19d43Seric 		/* child -- actually write to file */
1318f129ec7dSeric 		struct stat stb;
1319f129ec7dSeric 
13200984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
13210984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
13220984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
13233462ad9eSeric 		(void) umask(OldUmask);
132495f16dc0Seric 
1325f129ec7dSeric 		if (stat(filename, &stb) < 0)
1326e6e1265fSeric 			stb.st_mode = 0666;
132715d084d5Seric 		mode = stb.st_mode;
132895f16dc0Seric 
132995f16dc0Seric 		/* limit the errors to those actually caused in the child */
133095f16dc0Seric 		errno = 0;
133195f16dc0Seric 		ExitStat = EX_OK;
133295f16dc0Seric 
1333f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1334f129ec7dSeric 			exit(EX_CANTCREAT);
133503827b5fSeric 		if (ctladdr == NULL)
13368f9146b0Srick 			ctladdr = &e->e_from;
133715d084d5Seric 		else
133815d084d5Seric 		{
133915d084d5Seric 			/* ignore setuid and setgid bits */
134015d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
134115d084d5Seric 		}
134215d084d5Seric 
13438f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
13448f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
13458f9146b0Srick 		{
13468f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
134795f16dc0Seric 			if (e->e_dfp == NULL)
134895f16dc0Seric 			{
13498f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
13508f9146b0Srick 					e->e_df, e->e_to, e->e_from);
13518f9146b0Srick 			}
13528f9146b0Srick 		}
13538f9146b0Srick 
135415d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1355e36b99e2Seric 		{
135695f16dc0Seric 			if (ctladdr->q_uid == 0)
135795f16dc0Seric 			{
1358e36b99e2Seric 				(void) setgid(DefGid);
1359898a126bSbostic 				(void) initgroups(DefUser, DefGid);
136095f16dc0Seric 			}
136195f16dc0Seric 			else
136295f16dc0Seric 			{
13636259796dSeric 				(void) setgid(ctladdr->q_gid);
1364898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1365898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1366898a126bSbostic 					ctladdr->q_gid);
1367898a126bSbostic 			}
1368e36b99e2Seric 		}
136915d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1370e36b99e2Seric 		{
1371e36b99e2Seric 			if (ctladdr->q_uid == 0)
1372e36b99e2Seric 				(void) setuid(DefUid);
1373e36b99e2Seric 			else
13746259796dSeric 				(void) setuid(ctladdr->q_uid);
1375e36b99e2Seric 		}
137695f16dc0Seric 		FileName = filename;
137795f16dc0Seric 		LineNumber = 0;
137827628d59Seric 		f = dfopen(filename, "a");
137925a99e2eSeric 		if (f == NULL)
138095f16dc0Seric 		{
138195f16dc0Seric 			message("cannot open");
138232d19d43Seric 			exit(EX_CANTCREAT);
138395f16dc0Seric 		}
138425a99e2eSeric 
1385b31e7f2bSeric 		putfromline(f, ProgMailer, e);
1386b843d759Seric 		(*e->e_puthdr)(f, ProgMailer, e);
138777b52738Seric 		putline("\n", f, ProgMailer);
1388b843d759Seric 		(*e->e_putbody)(f, ProgMailer, e);
138977b52738Seric 		putline("\n", f, ProgMailer);
139095f16dc0Seric 		if (ferror(f))
139195f16dc0Seric 		{
139295f16dc0Seric 			message("I/O error");
139395f16dc0Seric 			setstat(EX_IOERR);
139495f16dc0Seric 		}
1395db8841e9Seric 		(void) fclose(f);
139632d19d43Seric 		(void) fflush(stdout);
1397e36b99e2Seric 
139827628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1399c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
140095f16dc0Seric 		exit(ExitStat);
140113bbc08cSeric 		/*NOTREACHED*/
140232d19d43Seric 	}
140332d19d43Seric 	else
140432d19d43Seric 	{
140532d19d43Seric 		/* parent -- wait for exit status */
1406588cad61Seric 		int st;
140732d19d43Seric 
1408588cad61Seric 		st = waitfor(pid);
1409588cad61Seric 		if ((st & 0377) != 0)
1410588cad61Seric 			return (EX_UNAVAILABLE);
1411588cad61Seric 		else
1412588cad61Seric 			return ((st >> 8) & 0377);
14138f9146b0Srick 		/*NOTREACHED*/
141432d19d43Seric 	}
141525a99e2eSeric }
1416ea4dc939Seric /*
1417ea4dc939Seric **  SENDALL -- actually send all the messages.
1418ea4dc939Seric **
1419ea4dc939Seric **	Parameters:
14200c52a0b3Seric **		e -- the envelope to send.
14217b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
14227b95031aSeric **			the current SendMode.
1423ea4dc939Seric **
1424ea4dc939Seric **	Returns:
1425ea4dc939Seric **		none.
1426ea4dc939Seric **
1427ea4dc939Seric **	Side Effects:
1428ea4dc939Seric **		Scans the send lists and sends everything it finds.
14290c52a0b3Seric **		Delivers any appropriate error messages.
1430276723a8Seric **		If we are running in a non-interactive mode, takes the
1431276723a8Seric **			appropriate action.
1432ea4dc939Seric */
1433ea4dc939Seric 
1434276723a8Seric sendall(e, mode)
14350c52a0b3Seric 	ENVELOPE *e;
1436276723a8Seric 	char mode;
1437ea4dc939Seric {
1438e77e673fSeric 	register ADDRESS *q;
143914a8ed7aSeric 	bool oldverbose;
1440276723a8Seric 	int pid;
14411c265a00Seric # ifdef LOCKF
14421c265a00Seric 	struct flock lfd;
14431c265a00Seric # endif
1444ea4dc939Seric 
14457b95031aSeric 	/* determine actual delivery mode */
14467b95031aSeric 	if (mode == SM_DEFAULT)
14477b95031aSeric 	{
14485f73204aSeric 		extern bool shouldqueue();
14497b95031aSeric 
145055e150dbSeric 		if (shouldqueue(e->e_msgpriority, e->e_ctime))
14517b95031aSeric 			mode = SM_QUEUE;
14527b95031aSeric 		else
14537b95031aSeric 			mode = SendMode;
14547b95031aSeric 	}
14557b95031aSeric 
1456df864a8fSeric 	if (tTd(13, 1))
1457772e6e50Seric 	{
1458276723a8Seric 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
14590c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1460772e6e50Seric 	}
1461ea4dc939Seric 
14620c52a0b3Seric 	/*
1463276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1464588cad61Seric 	**	Check to make sure the hop count is reasonable.
1465588cad61Seric 	**	Delete sends to the sender in mailing lists.
1466276723a8Seric 	*/
1467276723a8Seric 
1468588cad61Seric 	CurEnv = e;
1469276723a8Seric 
14701d57450bSeric 	if (e->e_hopcount > MaxHopCount)
1471276723a8Seric 	{
14728f9146b0Srick 		errno = 0;
14738f9146b0Srick 		syserr("sendall: too many hops %d (%d max): from %s, to %s",
1474655518ecSeric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
1475655518ecSeric 			e->e_sendqueue->q_paddr);
1476588cad61Seric 		return;
1477588cad61Seric 	}
1478588cad61Seric 
1479588cad61Seric 	if (!MeToo)
1480276723a8Seric 	{
1481f3d6c55cSeric 		extern ADDRESS *recipient();
1482f3d6c55cSeric 
1483588cad61Seric 		e->e_from.q_flags |= QDONTSEND;
1484*75f1ade9Seric 		if (tTd(13, 5))
1485*75f1ade9Seric 		{
1486*75f1ade9Seric 			printf("sendall: QDONTSEND ");
1487*75f1ade9Seric 			printaddr(&e->e_from, FALSE);
1488*75f1ade9Seric 		}
1489b843d759Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1490276723a8Seric 	}
1491588cad61Seric 
1492588cad61Seric # ifdef QUEUE
1493b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1494b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1495b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
1496e020b127Seric 		queueup(e, TRUE, mode == SM_QUEUE);
14976c2c3107Seric #endif /* QUEUE */
1498276723a8Seric 
1499276723a8Seric 	oldverbose = Verbose;
1500276723a8Seric 	switch (mode)
1501276723a8Seric 	{
1502276723a8Seric 	  case SM_VERIFY:
1503276723a8Seric 		Verbose = TRUE;
1504276723a8Seric 		break;
1505276723a8Seric 
1506276723a8Seric 	  case SM_QUEUE:
1507c7f5410dSeric   queueonly:
1508b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1509276723a8Seric 		return;
1510276723a8Seric 
1511276723a8Seric 	  case SM_FORK:
15129a6a5f55Seric 		if (e->e_xfp != NULL)
15139a6a5f55Seric 			(void) fflush(e->e_xfp);
1514c7f5410dSeric 
1515c7f5410dSeric # ifdef LOCKF
1516c7f5410dSeric 		/*
1517c7f5410dSeric 		**  Since lockf has the interesting semantic that the
15181c265a00Seric 		**  lock is lost when we fork, we have to risk losing
15191c265a00Seric 		**  the lock here by closing before the fork, and then
15201c265a00Seric 		**  trying to get it back in the child.
1521c7f5410dSeric 		*/
1522c7f5410dSeric 
1523e020b127Seric 		if (e->e_lockfp != NULL)
1524c7f5410dSeric 		{
1525e020b127Seric 			(void) fclose(e->e_lockfp);
1526e020b127Seric 			e->e_lockfp = NULL;
1527c7f5410dSeric 		}
1528c7f5410dSeric # endif /* LOCKF */
1529c7f5410dSeric 
1530276723a8Seric 		pid = fork();
1531276723a8Seric 		if (pid < 0)
1532276723a8Seric 		{
1533c7f5410dSeric 			goto queueonly;
1534276723a8Seric 		}
1535276723a8Seric 		else if (pid > 0)
1536a6fce3d8Seric 		{
1537a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1538b254bcb6Seric 			e->e_id = e->e_df = NULL;
1539c7f5410dSeric # ifndef LOCKF
1540e020b127Seric 			if (e->e_lockfp != NULL)
1541*75f1ade9Seric 			{
1542e020b127Seric 				(void) fclose(e->e_lockfp);
1543*75f1ade9Seric 				e->e_lockfp = NULL;
1544*75f1ade9Seric 			}
1545c7f5410dSeric # endif
1546*75f1ade9Seric 
1547*75f1ade9Seric 			/* close any random open files in the envelope */
1548*75f1ade9Seric 			if (e->e_dfp != NULL)
1549*75f1ade9Seric 			{
1550*75f1ade9Seric 				(void) fclose(e->e_dfp);
1551*75f1ade9Seric 				e->e_dfp = NULL;
1552*75f1ade9Seric 			}
1553*75f1ade9Seric 			if (e->e_xfp != NULL)
1554*75f1ade9Seric 			{
1555*75f1ade9Seric 				(void) fclose(e->e_xfp);
1556*75f1ade9Seric 				e->e_xfp = NULL;
1557*75f1ade9Seric 			}
1558276723a8Seric 			return;
1559a6fce3d8Seric 		}
1560276723a8Seric 
1561276723a8Seric 		/* double fork to avoid zombies */
1562276723a8Seric 		if (fork() > 0)
1563276723a8Seric 			exit(EX_OK);
1564276723a8Seric 
1565a6fce3d8Seric 		/* be sure we are immune from the terminal */
1566769e215aSeric 		disconnect(FALSE);
1567a6fce3d8Seric 
1568e6720d51Seric # ifdef LOCKF
1569e6720d51Seric 		/*
1570c7f5410dSeric 		**  Now try to get our lock back.
1571e6720d51Seric 		*/
1572e6720d51Seric 
15731c265a00Seric 		lfd.l_type = F_WRLCK;
15741c265a00Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1575e020b127Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1576e020b127Seric 		if (e->e_lockfp == NULL ||
15771c265a00Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1578e6720d51Seric 		{
1579e6720d51Seric 			/* oops....  lost it */
1580e6720d51Seric # ifdef LOG
1581e6720d51Seric 			if (LogLevel > 5)
1582c7f5410dSeric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1583b31e7f2bSeric 					e->e_id);
1584e6720d51Seric # endif /* LOG */
1585e6720d51Seric 			exit(EX_OK);
1586e6720d51Seric 		}
1587e6720d51Seric # endif /* LOCKF */
1588e6720d51Seric 
1589276723a8Seric 		break;
1590276723a8Seric 	}
1591276723a8Seric 
1592276723a8Seric 	/*
15930c52a0b3Seric 	**  Run through the list and send everything.
15940c52a0b3Seric 	*/
15950c52a0b3Seric 
1596655518ecSeric 	e->e_nsent = 0;
15970c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1598ea4dc939Seric 	{
1599276723a8Seric 		if (mode == SM_VERIFY)
1600ea4dc939Seric 		{
1601a6fce3d8Seric 			e->e_to = q->q_paddr;
1602e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1603ea4dc939Seric 				message(Arpa_Info, "deliverable");
1604ea4dc939Seric 		}
1605dde5acadSeric 		else if (!bitset(QDONTSEND, q->q_flags))
1606dde5acadSeric 		{
1607de1179f5Seric # ifdef QUEUE
1608dde5acadSeric 			/*
1609dde5acadSeric 			**  Checkpoint the send list every few addresses
1610dde5acadSeric 			*/
1611dde5acadSeric 
1612655518ecSeric 			if (e->e_nsent >= CheckpointInterval)
1613dde5acadSeric 			{
1614e020b127Seric 				queueup(e, TRUE, FALSE);
1615655518ecSeric 				e->e_nsent = 0;
1616dde5acadSeric 			}
1617de1179f5Seric # endif /* QUEUE */
1618655518ecSeric 			(void) deliver(e, q);
1619dde5acadSeric 		}
1620ea4dc939Seric 	}
162114a8ed7aSeric 	Verbose = oldverbose;
16220c52a0b3Seric 
16230c52a0b3Seric 	/*
16240c52a0b3Seric 	**  Now run through and check for errors.
16250c52a0b3Seric 	*/
16260c52a0b3Seric 
1627e020b127Seric 	if (mode == SM_VERIFY)
16280c52a0b3Seric 		return;
16290c52a0b3Seric 
16300c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
16310c52a0b3Seric 	{
16320c52a0b3Seric 		register ADDRESS *qq;
16330c52a0b3Seric 
1634df864a8fSeric 		if (tTd(13, 3))
1635df864a8fSeric 		{
1636df864a8fSeric 			printf("Checking ");
1637df864a8fSeric 			printaddr(q, FALSE);
1638df864a8fSeric 		}
1639df864a8fSeric 
1640b254bcb6Seric 		/* only send errors if the message failed */
1641b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1642b254bcb6Seric 			continue;
16430c52a0b3Seric 
16440c52a0b3Seric 		/* we have an address that failed -- find the parent */
16450c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
16460c52a0b3Seric 		{
16470c52a0b3Seric 			char obuf[MAXNAME + 6];
16480c52a0b3Seric 			extern char *aliaslookup();
16490c52a0b3Seric 
16500c52a0b3Seric 			/* we can only have owners for local addresses */
165157fc6f17Seric 			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
16520c52a0b3Seric 				continue;
16530c52a0b3Seric 
16540c52a0b3Seric 			/* see if the owner list exists */
16550c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1656cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1657cec031e3Seric 				(void) strcat(obuf, "owner");
1658cec031e3Seric 			else
16590c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
166055e150dbSeric 			if (!bitnset(M_USR_UPPER, qq->q_mailer->m_flags))
1661ef137175Sbostic 				makelower(obuf);
16620c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
16630c52a0b3Seric 				continue;
16640c52a0b3Seric 
1665df864a8fSeric 			if (tTd(13, 4))
1666df864a8fSeric 				printf("Errors to %s\n", obuf);
1667df864a8fSeric 
16680c52a0b3Seric 			/* owner list exists -- add it to the error queue */
1669b843d759Seric 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue, e);
167055e150dbSeric 
167155e150dbSeric 			/* and set the return path to point to it */
167255e150dbSeric 			e->e_returnpath = newstr(obuf);
167355e150dbSeric 
167453e3fa05Seric 			ErrorMode = EM_MAIL;
16750c52a0b3Seric 			break;
16760c52a0b3Seric 		}
16770c52a0b3Seric 
16780c52a0b3Seric 		/* if we did not find an owner, send to the sender */
16797455aa0bSeric 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1680b843d759Seric 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue, e);
16810c52a0b3Seric 	}
1682276723a8Seric 
1683276723a8Seric 	if (mode == SM_FORK)
1684276723a8Seric 		finis();
16850c52a0b3Seric }
1686e103b48fSeric /*
1687e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1688e103b48fSeric **
1689e103b48fSeric **	The signature describes how we are going to send this -- it
1690e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1691e103b48fSeric **	an ordered list of MX hosts.
1692e103b48fSeric **
1693e103b48fSeric **	Parameters:
1694e103b48fSeric **		m -- the mailer describing this host.
1695e103b48fSeric **		host -- the host name.
1696e103b48fSeric **		e -- the current envelope.
1697e103b48fSeric **
1698e103b48fSeric **	Returns:
1699e103b48fSeric **		The signature for this host.
1700e103b48fSeric **
1701e103b48fSeric **	Side Effects:
1702e103b48fSeric **		Can tweak the symbol table.
1703e103b48fSeric */
1704e103b48fSeric 
1705e103b48fSeric char *
1706e103b48fSeric hostsignature(m, host, e)
1707e103b48fSeric 	register MAILER *m;
1708e103b48fSeric 	char *host;
1709e103b48fSeric 	ENVELOPE *e;
1710e103b48fSeric {
1711e103b48fSeric 	register char *p;
1712e103b48fSeric 	register STAB *s;
1713e103b48fSeric 	int i;
1714e103b48fSeric 	int len;
1715e103b48fSeric #ifdef NAMED_BIND
1716e103b48fSeric 	int nmx;
1717e103b48fSeric 	auto int rcode;
1718e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
1719e103b48fSeric 	static char myhostbuf[MAXNAME];
1720e103b48fSeric #endif
1721e103b48fSeric 
1722e103b48fSeric 	/*
1723e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
1724e103b48fSeric 	*/
1725e103b48fSeric 
1726e103b48fSeric 	p = m->m_mailer;
1727e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
1728e103b48fSeric 	{
1729e103b48fSeric 		/* just an ordinary mailer */
1730e103b48fSeric 		return host;
1731e103b48fSeric 	}
1732e103b48fSeric 
1733e103b48fSeric 	/*
1734e103b48fSeric 	**  If it is a numeric address, just return it.
1735e103b48fSeric 	*/
1736e103b48fSeric 
1737e103b48fSeric 	if (host[0] == '[')
1738e103b48fSeric 		return host;
1739e103b48fSeric 
1740e103b48fSeric 	/*
1741e103b48fSeric 	**  Look it up in the symbol table.
1742e103b48fSeric 	*/
1743e103b48fSeric 
1744e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
1745e103b48fSeric 	if (s->s_hostsig != NULL)
1746e103b48fSeric 		return s->s_hostsig;
1747e103b48fSeric 
1748e103b48fSeric 	/*
1749e103b48fSeric 	**  Not already there -- create a signature.
1750e103b48fSeric 	*/
1751e103b48fSeric 
1752e103b48fSeric #ifdef NAMED_BIND
1753e103b48fSeric 	if (myhostbuf[0] == '\0')
1754e103b48fSeric 		expand("\001j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e);
1755e103b48fSeric 
1756e103b48fSeric 	nmx = getmxrr(host, mxhosts, myhostbuf, &rcode);
1757e103b48fSeric 	if (nmx <= 0)
1758e103b48fSeric 	{
1759e103b48fSeric 		register MCI *mci;
1760e103b48fSeric 		extern int errno;
1761e103b48fSeric 		extern MCI *mci_get();
1762e103b48fSeric 
1763e103b48fSeric 		/* update the connection info for this host */
1764e103b48fSeric 		mci = mci_get(host, m);
1765e103b48fSeric 		mci->mci_exitstat = rcode;
1766e103b48fSeric 		mci->mci_errno = errno;
1767e103b48fSeric 
1768e103b48fSeric 		/* and return the original host name as the signature */
1769e103b48fSeric 		s->s_hostsig = host;
1770e103b48fSeric 		return host;
1771e103b48fSeric 	}
1772e103b48fSeric 
1773e103b48fSeric 	len = 0;
1774e103b48fSeric 	for (i = 0; i < nmx; i++)
1775e103b48fSeric 	{
1776e103b48fSeric 		len += strlen(mxhosts[i]) + 1;
1777e103b48fSeric 	}
1778e103b48fSeric 	s->s_hostsig = p = xalloc(len);
1779e103b48fSeric 	for (i = 0; i < nmx; i++)
1780e103b48fSeric 	{
1781e103b48fSeric 		if (i != 0)
1782e103b48fSeric 			*p++ = ':';
1783e103b48fSeric 		strcpy(p, mxhosts[i]);
1784e103b48fSeric 		p += strlen(p);
1785e103b48fSeric 	}
1786e103b48fSeric 	makelower(s->s_hostsig);
1787e103b48fSeric #else
1788e103b48fSeric 	/* not using BIND -- the signature is just the host name */
1789e103b48fSeric 	s->s_hostsig = host;
1790e103b48fSeric #endif
1791e103b48fSeric 	if (tTd(17, 1))
1792e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
1793e103b48fSeric 	return s->s_hostsig;
1794e103b48fSeric }
1795