14227346bSdist /*
20942ea6aSbostic  * Copyright (c) 1983 Eric P. Allman
3e70a7521Sbostic  * Copyright (c) 1988 Regents of the University of California.
4e70a7521Sbostic  * All rights reserved.
5e70a7521Sbostic  *
6*3bc94712Sbostic  * %sccs.include.redist.c%
74227346bSdist  */
84227346bSdist 
94227346bSdist #ifndef lint
10*3bc94712Sbostic static char sccsid[] = "@(#)deliver.c	5.38 (Berkeley) 06/01/90";
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;
57c77d1c25Seric 	register ADDRESS *to = firstto;
58c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
59772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
60911693bfSbostic 	int rcode;		/* response code */
61ee6bf8dfSeric 	char *pv[MAXPV+1];
62ee6bf8dfSeric 	char tobuf[MAXLINE-50];		/* text line of to people */
63ee6bf8dfSeric 	char buf[MAXNAME];
64ee6bf8dfSeric 	char tfrombuf[MAXNAME];		/* translated from person */
65ee6bf8dfSeric 	extern bool checkcompat();
66ee6bf8dfSeric 	extern ADDRESS *getctladdr();
67ee6bf8dfSeric 	extern char *remotename();
6825a99e2eSeric 
6935490626Seric 	errno = 0;
70da2935e1Seric 	if (bitset(QDONTSEND, to->q_flags))
715dfc646bSeric 		return (0);
7225a99e2eSeric 
73134746fbSeric #ifdef NAMED_BIND
74912a731aSbostic 	/* unless interactive, try twice, over a minute */
75912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
76912a731aSbostic 		_res.retrans = 30;
77912a731aSbostic 		_res.retry = 2;
78912a731aSbostic 	}
79d4bd8f0eSbostic #endif
80912a731aSbostic 
8151552439Seric 	m = to->q_mailer;
8251552439Seric 	host = to->q_host;
8351552439Seric 
846ef48975Seric 	if (tTd(10, 1))
855dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
8651552439Seric 			m->m_mno, host, to->q_user);
87f3dbc832Seric 
88f3dbc832Seric 	/*
89f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
90f3dbc832Seric 	**  connections now, just mark these addresses and return.
91f3dbc832Seric 	**	This is useful if we want to batch connections to
92f3dbc832Seric 	**	reduce load.  This will cause the messages to be
93f3dbc832Seric 	**	queued up, and a daemon will come along to send the
94f3dbc832Seric 	**	messages later.
95f3dbc832Seric 	**		This should be on a per-mailer basis.
96f3dbc832Seric 	*/
97f3dbc832Seric 
9857fc6f17Seric 	if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) &&
99317680d6Seric 	    !Verbose)
100f3dbc832Seric 	{
101f3dbc832Seric 		for (; to != NULL; to = to->q_next)
102f4560e80Seric 		{
103f4560e80Seric 			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
104f4560e80Seric 				continue;
105f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
106588cad61Seric 			e->e_to = to->q_paddr;
107eb238f8cSeric 			message(Arpa_Info, "queued");
108eb238f8cSeric 			if (LogLevel > 4)
109eb238f8cSeric 				logdelivery("queued");
110f4560e80Seric 		}
111588cad61Seric 		e->e_to = NULL;
112f3dbc832Seric 		return (0);
113f3dbc832Seric 	}
114f3dbc832Seric 
11525a99e2eSeric 	/*
1165dfc646bSeric 	**  Do initial argv setup.
1175dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
1185dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
1195dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
1205dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
1215dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
1223bea8136Seric 	**		The from address rewrite is expected to make
1233bea8136Seric 	**		the address relative to the other end.
1245dfc646bSeric 	*/
1255dfc646bSeric 
12678442df3Seric 	/* rewrite from address, using rewriting rules */
1279b6c17a6Seric 	expand("\001f", buf, &buf[sizeof buf - 1], e);
128ee6bf8dfSeric 	(void) strcpy(tfrombuf, remotename(buf, m, TRUE, TRUE));
12978442df3Seric 
130588cad61Seric 	define('g', tfrombuf, e);		/* translated sender address */
131588cad61Seric 	define('h', host, e);			/* to host */
1325dfc646bSeric 	Errors = 0;
1335dfc646bSeric 	pvp = pv;
1345dfc646bSeric 	*pvp++ = m->m_argv[0];
1355dfc646bSeric 
1365dfc646bSeric 	/* insert -f or -r flag as appropriate */
13757fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
1385dfc646bSeric 	{
13957fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
1405dfc646bSeric 			*pvp++ = "-f";
1415dfc646bSeric 		else
1425dfc646bSeric 			*pvp++ = "-r";
1439b6c17a6Seric 		expand("\001g", buf, &buf[sizeof buf - 1], e);
1445dfc646bSeric 		*pvp++ = newstr(buf);
1455dfc646bSeric 	}
1465dfc646bSeric 
1475dfc646bSeric 	/*
1485dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1495dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1505dfc646bSeric 	**  be one of these, and there are only a few more slots
1515dfc646bSeric 	**  in the pv after it.
1525dfc646bSeric 	*/
1535dfc646bSeric 
1545dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1555dfc646bSeric 	{
1569b6c17a6Seric 		while ((p = index(p, '\001')) != NULL)
1575dfc646bSeric 			if (*++p == 'u')
1585dfc646bSeric 				break;
1595dfc646bSeric 		if (p != NULL)
1605dfc646bSeric 			break;
1615dfc646bSeric 
1625dfc646bSeric 		/* this entry is safe -- go ahead and process it */
163588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1645dfc646bSeric 		*pvp++ = newstr(buf);
1655dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1665dfc646bSeric 		{
1675dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1685dfc646bSeric 			return (-1);
1695dfc646bSeric 		}
1705dfc646bSeric 	}
171c579ef51Seric 
17233db8731Seric 	/*
17333db8731Seric 	**  If we have no substitution for the user name in the argument
17433db8731Seric 	**  list, we know that we must supply the names otherwise -- and
17533db8731Seric 	**  SMTP is the answer!!
17633db8731Seric 	*/
17733db8731Seric 
1785dfc646bSeric 	if (*mvp == NULL)
179c579ef51Seric 	{
180c579ef51Seric 		/* running SMTP */
1812c7e1b8dSeric # ifdef SMTP
182c579ef51Seric 		clever = TRUE;
183c579ef51Seric 		*pvp = NULL;
1842c7e1b8dSeric # else SMTP
18533db8731Seric 		/* oops!  we don't implement SMTP */
1862c7e1b8dSeric 		syserr("SMTP style mailer");
1872c7e1b8dSeric 		return (EX_SOFTWARE);
1882c7e1b8dSeric # endif SMTP
189c579ef51Seric 	}
1905dfc646bSeric 
1915dfc646bSeric 	/*
1925dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
1935dfc646bSeric 	**  run through our address list and append all the addresses
1945dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
1955dfc646bSeric 	**  always send another copy later.
1965dfc646bSeric 	*/
1975dfc646bSeric 
1985dfc646bSeric 	tobuf[0] = '\0';
199588cad61Seric 	e->e_to = tobuf;
2006259796dSeric 	ctladdr = NULL;
2015dfc646bSeric 	for (; to != NULL; to = to->q_next)
2025dfc646bSeric 	{
2035dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
20457fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
2055dfc646bSeric 			break;
2065dfc646bSeric 
2075dfc646bSeric 		/* if already sent or not for this host, don't send */
208da2935e1Seric 		if (bitset(QDONTSEND, to->q_flags) ||
209da2935e1Seric 		    strcmp(to->q_host, host) != 0 ||
210da2935e1Seric 		    to->q_mailer != firstto->q_mailer)
2115dfc646bSeric 			continue;
2126259796dSeric 
2134b22ea87Seric 		/* avoid overflowing tobuf */
214aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
2154b22ea87Seric 			break;
2164b22ea87Seric 
2176ef48975Seric 		if (tTd(10, 1))
218772e6e50Seric 		{
219772e6e50Seric 			printf("\nsend to ");
220772e6e50Seric 			printaddr(to, FALSE);
221772e6e50Seric 		}
222772e6e50Seric 
2236259796dSeric 		/* compute effective uid/gid when sending */
2247da1035fSeric 		if (to->q_mailer == ProgMailer)
2256259796dSeric 			ctladdr = getctladdr(to);
2266259796dSeric 
2275dfc646bSeric 		user = to->q_user;
228588cad61Seric 		e->e_to = to->q_paddr;
2295dfc646bSeric 		to->q_flags |= QDONTSEND;
2305dfc646bSeric 
2315dfc646bSeric 		/*
2325dfc646bSeric 		**  Check to see that these people are allowed to
2335dfc646bSeric 		**  talk to each other.
2342a6e0786Seric 		*/
2352a6e0786Seric 
23669582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
23769582d2fSeric 		{
23869582d2fSeric 			NoReturn = TRUE;
239672bec4aSeric 			usrerr("Message is too large; %ld bytes max", m->m_maxsize);
24069582d2fSeric 			giveresponse(EX_UNAVAILABLE, m, e);
24169582d2fSeric 			continue;
24269582d2fSeric 		}
2432a6e0786Seric 		if (!checkcompat(to))
2445dfc646bSeric 		{
245198d9be0Seric 			giveresponse(EX_UNAVAILABLE, m, e);
2465dfc646bSeric 			continue;
2475dfc646bSeric 		}
2482a6e0786Seric 
2492a6e0786Seric 		/*
2509ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2519ec9501bSeric 		**	about them.
25225a99e2eSeric 		*/
25325a99e2eSeric 
25457fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
25525a99e2eSeric 		{
2569ec9501bSeric 			stripquotes(user, TRUE);
2579ec9501bSeric 			stripquotes(host, TRUE);
2589ec9501bSeric 		}
2599ec9501bSeric 		else
2609ec9501bSeric 		{
2619ec9501bSeric 			stripquotes(user, FALSE);
2629ec9501bSeric 			stripquotes(host, FALSE);
26325a99e2eSeric 		}
26425a99e2eSeric 
265cdb828c5Seric 		/* hack attack -- delivermail compatibility */
266cdb828c5Seric 		if (m == ProgMailer && *user == '|')
267cdb828c5Seric 			user++;
268cdb828c5Seric 
26925a99e2eSeric 		/*
2703efaed6eSeric 		**  If an error message has already been given, don't
2713efaed6eSeric 		**	bother to send to this address.
2723efaed6eSeric 		**
2733efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2743efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2753efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2763efaed6eSeric 		*/
2773efaed6eSeric 
2786cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2793efaed6eSeric 			continue;
2803efaed6eSeric 
281f2fec898Seric 		/* save statistics.... */
282588cad61Seric 		markstats(e, to);
283f2fec898Seric 
2843efaed6eSeric 		/*
28525a99e2eSeric 		**  See if this user name is "special".
28625a99e2eSeric 		**	If the user name has a slash in it, assume that this
28751552439Seric 		**	is a file -- send it off without further ado.  Note
28851552439Seric 		**	that this type of addresses is not processed along
28951552439Seric 		**	with the others, so we fudge on the To person.
29025a99e2eSeric 		*/
29125a99e2eSeric 
2927da1035fSeric 		if (m == LocalMailer)
29325a99e2eSeric 		{
294a49f24c0Seric 			if (user[0] == '/')
29525a99e2eSeric 			{
2965826d9d3Seric 				rcode = mailfile(user, getctladdr(to));
297198d9be0Seric 				giveresponse(rcode, m, e);
2985dfc646bSeric 				continue;
29925a99e2eSeric 			}
30025a99e2eSeric 		}
30125a99e2eSeric 
30213bbc08cSeric 		/*
30313bbc08cSeric 		**  Address is verified -- add this user to mailer
30413bbc08cSeric 		**  argv, and add it to the print list of recipients.
30513bbc08cSeric 		*/
30613bbc08cSeric 
307508daeccSeric 		/* link together the chain of recipients */
308508daeccSeric 		to->q_tchain = tochain;
309508daeccSeric 		tochain = to;
310508daeccSeric 
3115dfc646bSeric 		/* create list of users for error messages */
312db8841e9Seric 		(void) strcat(tobuf, ",");
313db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
314588cad61Seric 		define('u', user, e);		/* to user */
315588cad61Seric 		define('z', to->q_home, e);	/* user's home */
3165dfc646bSeric 
317c579ef51Seric 		/*
318508daeccSeric 		**  Expand out this user into argument list.
319c579ef51Seric 		*/
320c579ef51Seric 
321508daeccSeric 		if (!clever)
322c579ef51Seric 		{
323588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3245dfc646bSeric 			*pvp++ = newstr(buf);
3255dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3265dfc646bSeric 			{
3275dfc646bSeric 				/* allow some space for trailing parms */
3285dfc646bSeric 				break;
3295dfc646bSeric 			}
3305dfc646bSeric 		}
331c579ef51Seric 	}
3325dfc646bSeric 
333145b49b1Seric 	/* see if any addresses still exist */
334145b49b1Seric 	if (tobuf[0] == '\0')
335c579ef51Seric 	{
336588cad61Seric 		define('g', (char *) NULL, e);
337145b49b1Seric 		return (0);
338c579ef51Seric 	}
339145b49b1Seric 
3405dfc646bSeric 	/* print out messages as full list */
34163780dbdSeric 	e->e_to = tobuf + 1;
3425dfc646bSeric 
3435dfc646bSeric 	/*
3445dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3455dfc646bSeric 	*/
3465dfc646bSeric 
347c579ef51Seric 	while (!clever && *++mvp != NULL)
3485dfc646bSeric 	{
349588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
3505dfc646bSeric 		*pvp++ = newstr(buf);
3515dfc646bSeric 		if (pvp >= &pv[MAXPV])
3525dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3535dfc646bSeric 	}
3545dfc646bSeric 	*pvp++ = NULL;
3555dfc646bSeric 
35625a99e2eSeric 	/*
35725a99e2eSeric 	**  Call the mailer.
3586328bdf7Seric 	**	The argument vector gets built, pipes
35925a99e2eSeric 	**	are created as necessary, and we fork & exec as
3606328bdf7Seric 	**	appropriate.
361c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
36225a99e2eSeric 	*/
36325a99e2eSeric 
36486b26461Seric 	if (ctladdr == NULL)
36586b26461Seric 		ctladdr = &e->e_from;
366134746fbSeric #ifdef NAMED_BIND
367912a731aSbostic 	_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);		/* XXX */
368134746fbSeric #endif
3692c7e1b8dSeric #ifdef SMTP
370134746fbSeric 	if (clever)
371134746fbSeric 	{
372911693bfSbostic 		rcode = EX_OK;
373134746fbSeric #ifdef NAMED_BIND
3740877bd4eSbostic 		if (host[0] && host[0] != '[')
375134746fbSeric 		{
3760877bd4eSbostic 			expand("\001w", buf, &buf[sizeof(buf) - 1], e);
377134746fbSeric 			Nmx = getmxrr(host, MxHosts, buf, &rcode);
378134746fbSeric 		}
379134746fbSeric 		else
380134746fbSeric #endif
381134746fbSeric 		{
38260bcc2d9Sbostic 			Nmx = 1;
38360bcc2d9Sbostic 			MxHosts[0] = host;
384134746fbSeric 		}
385134746fbSeric 		if (Nmx >= 0)
386134746fbSeric 		{
387912a731aSbostic 			message(Arpa_Info, "Connecting to %s (%s)...",
388912a731aSbostic 			    MxHosts[0], m->m_name);
389912a731aSbostic 			if ((rcode = smtpinit(m, pv)) == EX_OK) {
390ded0d3daSkarels 				register char *t = tobuf;
391ded0d3daSkarels 				register int i;
392ded0d3daSkarels 
393588cad61Seric 				/* send the recipient list */
39463780dbdSeric 				tobuf[0] = '\0';
395911693bfSbostic 				for (to = tochain; to; to = to->q_tchain) {
39663780dbdSeric 					e->e_to = to->q_paddr;
397912a731aSbostic 					if ((i = smtprcpt(to, m)) != EX_OK) {
39883b7ddc9Seric 						markfailure(e, to, i);
399198d9be0Seric 						giveresponse(i, m, e);
40063780dbdSeric 					}
401911693bfSbostic 					else {
402911693bfSbostic 						*t++ = ',';
403911693bfSbostic 						for (p = to->q_paddr; *p; *t++ = *p++);
404588cad61Seric 					}
405588cad61Seric 				}
406588cad61Seric 
40763780dbdSeric 				/* now send the data */
40863780dbdSeric 				if (tobuf[0] == '\0')
40963780dbdSeric 					e->e_to = NULL;
410911693bfSbostic 				else {
41163780dbdSeric 					e->e_to = tobuf + 1;
41277b52738Seric 					rcode = smtpdata(m, e);
41363780dbdSeric 				}
41463780dbdSeric 
41563780dbdSeric 				/* now close the connection */
416a294c4b0Seric 				smtpquit(m);
41763780dbdSeric 			}
418c579ef51Seric 		}
419912a731aSbostic 	}
420c579ef51Seric 	else
421911693bfSbostic #endif /* SMTP */
422a05b3449Sbostic 	{
423912a731aSbostic 		message(Arpa_Info, "Connecting to %s (%s)...", host, m->m_name);
42477b52738Seric 		rcode = sendoff(e, m, pv, ctladdr);
425a05b3449Sbostic 	}
426134746fbSeric #ifdef NAMED_BIND
427912a731aSbostic 	_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
428134746fbSeric #endif
4295dfc646bSeric 
430c77d1c25Seric 	/*
43163780dbdSeric 	**  Do final status disposal.
43263780dbdSeric 	**	We check for something in tobuf for the SMTP case.
433c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
434c77d1c25Seric 	**		addressees.
435c77d1c25Seric 	*/
436c77d1c25Seric 
43763780dbdSeric 	if (tobuf[0] != '\0')
438198d9be0Seric 		giveresponse(rcode, m, e);
43963780dbdSeric 	if (rcode != EX_OK)
440772e6e50Seric 		for (to = tochain; to != NULL; to = to->q_tchain)
44183b7ddc9Seric 			markfailure(e, to, rcode);
442c77d1c25Seric 
44335490626Seric 	errno = 0;
444588cad61Seric 	define('g', (char *) NULL, e);
4455826d9d3Seric 	return (rcode);
44625a99e2eSeric }
4475dfc646bSeric /*
44883b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
44983b7ddc9Seric **
45083b7ddc9Seric **	Parameters:
45183b7ddc9Seric **		e -- the envelope we are sending.
45283b7ddc9Seric **		q -- the address to mark.
45383b7ddc9Seric **		rcode -- the code signifying the particular failure.
45483b7ddc9Seric **
45583b7ddc9Seric **	Returns:
45683b7ddc9Seric **		none.
45783b7ddc9Seric **
45883b7ddc9Seric **	Side Effects:
45983b7ddc9Seric **		marks the address (and possibly the envelope) with the
46083b7ddc9Seric **			failure so that an error will be returned or
46183b7ddc9Seric **			the message will be queued, as appropriate.
46283b7ddc9Seric */
46383b7ddc9Seric 
46483b7ddc9Seric markfailure(e, q, rcode)
46583b7ddc9Seric 	register ENVELOPE *e;
46683b7ddc9Seric 	register ADDRESS *q;
46783b7ddc9Seric 	int rcode;
46883b7ddc9Seric {
46983b7ddc9Seric 	if (rcode == EX_OK)
47083b7ddc9Seric 		return;
471ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
47283b7ddc9Seric 		q->q_flags |= QBADADDR;
47383b7ddc9Seric 	else if (curtime() > e->e_ctime + TimeOut)
47483b7ddc9Seric 	{
47583b7ddc9Seric 		extern char *pintvl();
476198d9be0Seric 		char buf[MAXLINE];
47783b7ddc9Seric 
47883b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
479198d9be0Seric 		{
480198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
48183b7ddc9Seric 				pintvl(TimeOut, FALSE));
482198d9be0Seric 			if (e->e_message != NULL)
483198d9be0Seric 				free(e->e_message);
484198d9be0Seric 			e->e_message = newstr(buf);
485198d9be0Seric 			message(Arpa_Info, buf);
486198d9be0Seric 		}
48783b7ddc9Seric 		q->q_flags |= QBADADDR;
48883b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
48983b7ddc9Seric 	}
49083b7ddc9Seric 	else
49183b7ddc9Seric 		q->q_flags |= QQUEUEUP;
49283b7ddc9Seric }
49383b7ddc9Seric /*
49432d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
49532d19d43Seric **
49632d19d43Seric **	This MUST be a macro, since after a vfork we are running
49732d19d43Seric **	two processes on the same stack!!!
49832d19d43Seric **
49932d19d43Seric **	Parameters:
50032d19d43Seric **		none.
50132d19d43Seric **
50232d19d43Seric **	Returns:
50332d19d43Seric **		From a macro???  You've got to be kidding!
50432d19d43Seric **
50532d19d43Seric **	Side Effects:
50632d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
50732d19d43Seric **			pid of child in parent, zero in child.
50832d19d43Seric **			-1 on unrecoverable error.
50932d19d43Seric **
51032d19d43Seric **	Notes:
51132d19d43Seric **		I'm awfully sorry this looks so awful.  That's
51232d19d43Seric **		vfork for you.....
51332d19d43Seric */
51432d19d43Seric 
51532d19d43Seric # define NFORKTRIES	5
5164300ddf0Seric # ifdef VMUNIX
51732d19d43Seric # define XFORK	vfork
5184300ddf0Seric # else VMUNIX
51932d19d43Seric # define XFORK	fork
5204300ddf0Seric # endif VMUNIX
52132d19d43Seric 
52232d19d43Seric # define DOFORK(fORKfN) \
52332d19d43Seric {\
52432d19d43Seric 	register int i;\
52532d19d43Seric \
52611799049Seric 	for (i = NFORKTRIES; --i >= 0; )\
52732d19d43Seric 	{\
52832d19d43Seric 		pid = fORKfN();\
52932d19d43Seric 		if (pid >= 0)\
53032d19d43Seric 			break;\
53111799049Seric 		if (i > 0)\
5326c4635f6Seric 			sleep((unsigned) NFORKTRIES - i);\
53332d19d43Seric 	}\
53432d19d43Seric }
53532d19d43Seric /*
5362ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
5372ed72599Seric **
5382ed72599Seric **	Parameters:
5392ed72599Seric **		none.
5402ed72599Seric **
5412ed72599Seric **	Returns:
5422ed72599Seric **		pid of child in parent.
5432ed72599Seric **		zero in child.
5442ed72599Seric **		-1 on error.
5452ed72599Seric **
5462ed72599Seric **	Side Effects:
5472ed72599Seric **		returns twice, once in parent and once in child.
5482ed72599Seric */
5492ed72599Seric 
5502ed72599Seric dofork()
5512ed72599Seric {
5522ed72599Seric 	register int pid;
5532ed72599Seric 
5542ed72599Seric 	DOFORK(fork);
5552ed72599Seric 	return (pid);
5562ed72599Seric }
5572ed72599Seric /*
5585dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
5595dfc646bSeric **
5605dfc646bSeric **	Parameters:
561588cad61Seric **		e -- the envelope to mail.
5625dfc646bSeric **		m -- mailer descriptor.
5635dfc646bSeric **		pvp -- parameter vector to send to it.
5646259796dSeric **		ctladdr -- an address pointer controlling the
5656259796dSeric **			user/groupid etc. of the mailer.
5665dfc646bSeric **
5675dfc646bSeric **	Returns:
5685dfc646bSeric **		exit status of mailer.
5695dfc646bSeric **
5705dfc646bSeric **	Side Effects:
5715dfc646bSeric **		none.
5725dfc646bSeric */
573912a731aSbostic static
57477b52738Seric sendoff(e, m, pvp, ctladdr)
575588cad61Seric 	register ENVELOPE *e;
576588cad61Seric 	MAILER *m;
5775dfc646bSeric 	char **pvp;
5786259796dSeric 	ADDRESS *ctladdr;
5795dfc646bSeric {
580c579ef51Seric 	auto FILE *mfile;
581c579ef51Seric 	auto FILE *rfile;
5825dfc646bSeric 	register int i;
583c579ef51Seric 	int pid;
584c579ef51Seric 
585c579ef51Seric 	/*
586c579ef51Seric 	**  Create connection to mailer.
587c579ef51Seric 	*/
588c579ef51Seric 
589c579ef51Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
590c579ef51Seric 	if (pid < 0)
591c579ef51Seric 		return (-1);
592c579ef51Seric 
593c579ef51Seric 	/*
594c579ef51Seric 	**  Format and send message.
595c579ef51Seric 	*/
596c579ef51Seric 
59777b52738Seric 	putfromline(mfile, m);
59877b52738Seric 	(*e->e_puthdr)(mfile, m, e);
59977b52738Seric 	putline("\n", mfile, m);
60077b52738Seric 	(*e->e_putbody)(mfile, m, e);
601c579ef51Seric 	(void) fclose(mfile);
6024d7d1e20Sbostic 	if (rfile != NULL)
6034d7d1e20Sbostic 		(void) fclose(rfile);
604c579ef51Seric 
605c579ef51Seric 	i = endmailer(pid, pvp[0]);
606bc6e2962Seric 
607bc6e2962Seric 	/* arrange a return receipt if requested */
60857fc6f17Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags))
609bc6e2962Seric 	{
610588cad61Seric 		e->e_flags |= EF_SENDRECEIPT;
611bc6e2962Seric 		/* do we want to send back more info? */
612bc6e2962Seric 	}
613bc6e2962Seric 
614c579ef51Seric 	return (i);
615c579ef51Seric }
616c579ef51Seric /*
617c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
618c579ef51Seric **
619c579ef51Seric **	We should never get fatal errors (e.g., segmentation
620c579ef51Seric **	violation), so we report those specially.  For other
621c579ef51Seric **	errors, we choose a status message (into statmsg),
622c579ef51Seric **	and if it represents an error, we print it.
623c579ef51Seric **
624c579ef51Seric **	Parameters:
625c579ef51Seric **		pid -- pid of mailer.
626c579ef51Seric **		name -- name of mailer (for error messages).
627c579ef51Seric **
628c579ef51Seric **	Returns:
629c579ef51Seric **		exit code of mailer.
630c579ef51Seric **
631c579ef51Seric **	Side Effects:
632c579ef51Seric **		none.
633c579ef51Seric */
634c579ef51Seric 
635c579ef51Seric endmailer(pid, name)
636c579ef51Seric 	int pid;
637c579ef51Seric 	char *name;
638c579ef51Seric {
639588cad61Seric 	int st;
640c579ef51Seric 
64133db8731Seric 	/* in the IPC case there is nothing to wait for */
64233db8731Seric 	if (pid == 0)
64333db8731Seric 		return (EX_OK);
64433db8731Seric 
64533db8731Seric 	/* wait for the mailer process to die and collect status */
646588cad61Seric 	st = waitfor(pid);
647588cad61Seric 	if (st == -1)
64878de67c1Seric 	{
649588cad61Seric 		syserr("endmailer %s: wait", name);
650588cad61Seric 		return (EX_SOFTWARE);
651c579ef51Seric 	}
65233db8731Seric 
65333db8731Seric 	/* see if it died a horrid death */
654c579ef51Seric 	if ((st & 0377) != 0)
655c579ef51Seric 	{
6565f73204aSeric 		syserr("mailer %s died with signal %o", name, st);
6575f73204aSeric 		ExitStat = EX_TEMPFAIL;
6585f73204aSeric 		return (EX_TEMPFAIL);
659c579ef51Seric 	}
66033db8731Seric 
66133db8731Seric 	/* normal death -- return status */
662588cad61Seric 	st = (st >> 8) & 0377;
663588cad61Seric 	return (st);
664c579ef51Seric }
665c579ef51Seric /*
666c579ef51Seric **  OPENMAILER -- open connection to mailer.
667c579ef51Seric **
668c579ef51Seric **	Parameters:
669c579ef51Seric **		m -- mailer descriptor.
670c579ef51Seric **		pvp -- parameter vector to pass to mailer.
671c579ef51Seric **		ctladdr -- controlling address for user.
672c579ef51Seric **		clever -- create a full duplex connection.
673c579ef51Seric **		pmfile -- pointer to mfile (to mailer) connection.
674c579ef51Seric **		prfile -- pointer to rfile (from mailer) connection.
675c579ef51Seric **
676c579ef51Seric **	Returns:
67733db8731Seric **		pid of mailer ( > 0 ).
678c579ef51Seric **		-1 on error.
67933db8731Seric **		zero on an IPC connection.
680c579ef51Seric **
681c579ef51Seric **	Side Effects:
682c579ef51Seric **		creates a mailer in a subprocess.
683c579ef51Seric */
684c579ef51Seric 
685c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
686588cad61Seric 	MAILER *m;
687c579ef51Seric 	char **pvp;
688c579ef51Seric 	ADDRESS *ctladdr;
689c579ef51Seric 	bool clever;
690c579ef51Seric 	FILE **pmfile;
691c579ef51Seric 	FILE **prfile;
692c579ef51Seric {
6935dfc646bSeric 	int pid;
694f8952a83Seric 	int mpvect[2];
695c579ef51Seric 	int rpvect[2];
696e90b7da2Sbostic 	FILE *mfile = NULL;
697e90b7da2Sbostic 	FILE *rfile = NULL;
6985dfc646bSeric 	extern FILE *fdopen();
6995dfc646bSeric 
7006ef48975Seric 	if (tTd(11, 1))
7015dfc646bSeric 	{
7028c57e552Seric 		printf("openmailer:");
7035dfc646bSeric 		printav(pvp);
7045dfc646bSeric 	}
70535490626Seric 	errno = 0;
7065dfc646bSeric 
707ef66a9d0Seric 	CurHostName = m->m_mailer;
708ef66a9d0Seric 
70933db8731Seric 	/*
71033db8731Seric 	**  Deal with the special case of mail handled through an IPC
71133db8731Seric 	**  connection.
71233db8731Seric 	**	In this case we don't actually fork.  We must be
71333db8731Seric 	**	running SMTP for this to work.  We will return a
71433db8731Seric 	**	zero pid to indicate that we are running IPC.
715e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
71633db8731Seric 	*/
71733db8731Seric 
718e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
719e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
720e7c1bd78Seric 	{
721e7c1bd78Seric 		*pmfile = stdout;
722e7c1bd78Seric 		*prfile = stdin;
723e7c1bd78Seric 		return (0);
724e7c1bd78Seric 	}
725e7c1bd78Seric 
72633db8731Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0)
72733db8731Seric 	{
7285f73204aSeric #ifdef HOSTINFO
7295f73204aSeric 		register STAB *st;
7305f73204aSeric 		extern STAB *stab();
7315f73204aSeric #endif HOSTINFO
732588cad61Seric #ifdef DAEMON
733ebc61751Sbloom 		register int i, j;
7341277f9a8Seric 		register u_short port;
73533db8731Seric 
736ef66a9d0Seric 		CurHostName = pvp[1];
73733db8731Seric 		if (!clever)
73833db8731Seric 			syserr("non-clever IPC");
73993b6e3cfSeric 		if (pvp[2] != NULL)
7401277f9a8Seric 			port = atoi(pvp[2]);
74193b6e3cfSeric 		else
7421277f9a8Seric 			port = 0;
743f1853fd7Seric 		for (j = 0; j < Nmx; j++)
744ebc61751Sbloom 		{
745f1853fd7Seric 			CurHostName = MxHosts[j];
7465f73204aSeric #ifdef HOSTINFO
7475f73204aSeric 		/* see if we have already determined that this host is fried */
748f1853fd7Seric 			st = stab(MxHosts[j], ST_HOST, ST_FIND);
749912a731aSbostic 			if (st == NULL || st->s_host.ho_exitstat == EX_OK) {
750912a731aSbostic 				if (j > 1)
751912a731aSbostic 					message(Arpa_Info,
752912a731aSbostic 					    "Connecting to %s (%s)...",
753912a731aSbostic 					    MxHosts[j], m->m_name);
754f1853fd7Seric 				i = makeconnection(MxHosts[j], port, pmfile, prfile);
755912a731aSbostic 			}
7565f73204aSeric 			else
757ef66a9d0Seric 			{
7585f73204aSeric 				i = st->s_host.ho_exitstat;
759ef66a9d0Seric 				errno = st->s_host.ho_errno;
760ef66a9d0Seric 			}
7615f73204aSeric #else HOSTINFO
762f1853fd7Seric 			i = makeconnection(MxHosts[j], port, pmfile, prfile);
7635f73204aSeric #endif HOSTINFO
76433db8731Seric 			if (i != EX_OK)
765ed854c7bSeric 			{
7665f73204aSeric #ifdef HOSTINFO
7675f73204aSeric 				/* enter status of this host */
7685f73204aSeric 				if (st == NULL)
769f1853fd7Seric 					st = stab(MxHosts[j], ST_HOST, ST_ENTER);
7705f73204aSeric 				st->s_host.ho_exitstat = i;
7715f73204aSeric 				st->s_host.ho_errno = errno;
7725f73204aSeric #endif HOSTINFO
773ed854c7bSeric 				ExitStat = i;
774ebc61751Sbloom 				continue;
775ed854c7bSeric 			}
77633db8731Seric 			else
77733db8731Seric 				return (0);
778ebc61751Sbloom 		}
779ebc61751Sbloom 		return (-1);
780588cad61Seric #else DAEMON
781588cad61Seric 		syserr("openmailer: no IPC");
782588cad61Seric 		return (-1);
78333db8731Seric #endif DAEMON
784588cad61Seric 	}
78533db8731Seric 
7866328bdf7Seric 	/* create a pipe to shove the mail through */
787f8952a83Seric 	if (pipe(mpvect) < 0)
78825a99e2eSeric 	{
789588cad61Seric 		syserr("openmailer: pipe (to mailer)");
79025a99e2eSeric 		return (-1);
79125a99e2eSeric 	}
792c579ef51Seric 
7932c7e1b8dSeric #ifdef SMTP
794c579ef51Seric 	/* if this mailer speaks smtp, create a return pipe */
795c579ef51Seric 	if (clever && pipe(rpvect) < 0)
796c579ef51Seric 	{
797588cad61Seric 		syserr("openmailer: pipe (from mailer)");
798c579ef51Seric 		(void) close(mpvect[0]);
799c579ef51Seric 		(void) close(mpvect[1]);
800c579ef51Seric 		return (-1);
801c579ef51Seric 	}
8022c7e1b8dSeric #endif SMTP
803c579ef51Seric 
80433db8731Seric 	/*
80533db8731Seric 	**  Actually fork the mailer process.
80633db8731Seric 	**	DOFORK is clever about retrying.
8076984bfddSeric 	**
8086984bfddSeric 	**	Dispose of SIGCHLD signal catchers that may be laying
8096984bfddSeric 	**	around so that endmail will get it.
81033db8731Seric 	*/
81133db8731Seric 
8129a6a5f55Seric 	if (CurEnv->e_xfp != NULL)
8139a6a5f55Seric 		(void) fflush(CurEnv->e_xfp);		/* for debugging */
814588cad61Seric 	(void) fflush(stdout);
8156984bfddSeric # ifdef SIGCHLD
8166984bfddSeric 	(void) signal(SIGCHLD, SIG_DFL);
8176984bfddSeric # endif SIGCHLD
81832d19d43Seric 	DOFORK(XFORK);
819f129ec7dSeric 	/* pid is set by DOFORK */
82025a99e2eSeric 	if (pid < 0)
82125a99e2eSeric 	{
82233db8731Seric 		/* failure */
823588cad61Seric 		syserr("openmailer: cannot fork");
824f8952a83Seric 		(void) close(mpvect[0]);
825f8952a83Seric 		(void) close(mpvect[1]);
826588cad61Seric #ifdef SMTP
827c579ef51Seric 		if (clever)
828c579ef51Seric 		{
829c579ef51Seric 			(void) close(rpvect[0]);
830c579ef51Seric 			(void) close(rpvect[1]);
831c579ef51Seric 		}
832588cad61Seric #endif SMTP
83325a99e2eSeric 		return (-1);
83425a99e2eSeric 	}
83525a99e2eSeric 	else if (pid == 0)
83625a99e2eSeric 	{
83713088b9fSeric 		int i;
8385f73204aSeric 		extern int DtableSize;
83913088b9fSeric 
84025a99e2eSeric 		/* child -- set up input & exec mailer */
84103ab8e55Seric 		/* make diagnostic output be standard output */
8428f0e7860Seric 		(void) signal(SIGINT, SIG_IGN);
8438f0e7860Seric 		(void) signal(SIGHUP, SIG_IGN);
8440984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
845f8952a83Seric 
846f8952a83Seric 		/* arrange to filter standard & diag output of command */
847c579ef51Seric 		if (clever)
848c579ef51Seric 		{
849c579ef51Seric 			(void) close(rpvect[0]);
850c579ef51Seric 			(void) close(1);
851c579ef51Seric 			(void) dup(rpvect[1]);
852c579ef51Seric 			(void) close(rpvect[1]);
853c579ef51Seric 		}
854276723a8Seric 		else if (OpMode == MD_SMTP || HoldErrs)
855f8952a83Seric 		{
856588cad61Seric 			/* put mailer output in transcript */
857f8952a83Seric 			(void) close(1);
8589a6a5f55Seric 			(void) dup(fileno(CurEnv->e_xfp));
859f8952a83Seric 		}
860db8841e9Seric 		(void) close(2);
861db8841e9Seric 		(void) dup(1);
862f8952a83Seric 
863f8952a83Seric 		/* arrange to get standard input */
864f8952a83Seric 		(void) close(mpvect[1]);
865db8841e9Seric 		(void) close(0);
866f8952a83Seric 		if (dup(mpvect[0]) < 0)
86725a99e2eSeric 		{
86825a99e2eSeric 			syserr("Cannot dup to zero!");
869a590b978Seric 			_exit(EX_OSERR);
87025a99e2eSeric 		}
871f8952a83Seric 		(void) close(mpvect[0]);
87257fc6f17Seric 		if (!bitnset(M_RESTR, m->m_flags))
8730984da9fSeric 		{
87453e3fa05Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
875e36b99e2Seric 			{
876e36b99e2Seric 				(void) setgid(DefGid);
877898a126bSbostic 				(void) initgroups(DefUser, DefGid);
878e36b99e2Seric 				(void) setuid(DefUid);
879e36b99e2Seric 			}
880e36b99e2Seric 			else
88169f29479Seric 			{
882e36b99e2Seric 				(void) setgid(ctladdr->q_gid);
883898a126bSbostic 				(void) initgroups(ctladdr->q_ruser?
884898a126bSbostic 					ctladdr->q_ruser: ctladdr->q_user,
885898a126bSbostic 					ctladdr->q_gid);
886e36b99e2Seric 				(void) setuid(ctladdr->q_uid);
88769f29479Seric 			}
8880984da9fSeric 		}
889588cad61Seric 
89013088b9fSeric 		/* arrange for all the files to be closed */
891fccb3f7aSbostic 		for (i = 3; i < DtableSize; i++) {
892fccb3f7aSbostic 			register int j;
893fccb3f7aSbostic 			if ((j = fcntl(i, F_GETFD, 0)) != -1)
894fccb3f7aSbostic 				(void)fcntl(i, F_SETFD, j|1);
895fccb3f7aSbostic 		}
89633db8731Seric 
89733db8731Seric 		/* try to execute the mailer */
8985df317aaSeric 		execve(m->m_mailer, pvp, UserEnviron);
89913088b9fSeric 		syserr("Cannot exec %s", m->m_mailer);
9005f73204aSeric 		if (m == LocalMailer || errno == EIO || errno == EAGAIN ||
9015f73204aSeric 		    errno == ENOMEM || errno == EPROCLIM)
90255f33c03Seric 			_exit(EX_TEMPFAIL);
90355f33c03Seric 		else
904a590b978Seric 			_exit(EX_UNAVAILABLE);
90525a99e2eSeric 	}
90625a99e2eSeric 
907f8952a83Seric 	/*
908c579ef51Seric 	**  Set up return value.
909f8952a83Seric 	*/
910f8952a83Seric 
911f8952a83Seric 	(void) close(mpvect[0]);
912f8952a83Seric 	mfile = fdopen(mpvect[1], "w");
913c579ef51Seric 	if (clever)
91425a99e2eSeric 	{
915c579ef51Seric 		(void) close(rpvect[1]);
916c579ef51Seric 		rfile = fdopen(rpvect[0], "r");
9174d7d1e20Sbostic 	} else
9184d7d1e20Sbostic 		rfile = NULL;
919c579ef51Seric 
920c579ef51Seric 	*pmfile = mfile;
921c579ef51Seric 	*prfile = rfile;
922c579ef51Seric 
923c579ef51Seric 	return (pid);
92425a99e2eSeric }
92525a99e2eSeric /*
92625a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
92725a99e2eSeric **
92825a99e2eSeric **	Parameters:
92925a99e2eSeric **		stat -- the status code from the mailer (high byte
93025a99e2eSeric **			only; core dumps must have been taken care of
93125a99e2eSeric **			already).
93225a99e2eSeric **		m -- the mailer descriptor for this mailer.
93325a99e2eSeric **
93425a99e2eSeric **	Returns:
935db8841e9Seric **		none.
93625a99e2eSeric **
93725a99e2eSeric **	Side Effects:
938c1f9df2cSeric **		Errors may be incremented.
93925a99e2eSeric **		ExitStat may be set.
94025a99e2eSeric */
94125a99e2eSeric 
942198d9be0Seric giveresponse(stat, m, e)
94325a99e2eSeric 	int stat;
944588cad61Seric 	register MAILER *m;
945198d9be0Seric 	ENVELOPE *e;
94625a99e2eSeric {
94725a99e2eSeric 	register char *statmsg;
94825a99e2eSeric 	extern char *SysExMsg[];
94925a99e2eSeric 	register int i;
950d4bd8f0eSbostic 	extern int N_SysEx;
951d4bd8f0eSbostic #ifdef NAMED_BIND
952d4bd8f0eSbostic 	extern int h_errno;
953d4bd8f0eSbostic #endif
954198d9be0Seric 	char buf[MAXLINE];
95525a99e2eSeric 
9567d1fc79dSeric #ifdef lint
9577d1fc79dSeric 	if (m == NULL)
9587d1fc79dSeric 		return;
9597d1fc79dSeric #endif lint
9607d1fc79dSeric 
96113bbc08cSeric 	/*
96213bbc08cSeric 	**  Compute status message from code.
96313bbc08cSeric 	*/
96413bbc08cSeric 
96525a99e2eSeric 	i = stat - EX__BASE;
966588cad61Seric 	if (stat == 0)
967588cad61Seric 		statmsg = "250 Sent";
968588cad61Seric 	else if (i < 0 || i > N_SysEx)
969588cad61Seric 	{
970588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
971588cad61Seric 		stat = EX_UNAVAILABLE;
972588cad61Seric 		statmsg = buf;
973588cad61Seric 	}
974198d9be0Seric 	else if (stat == EX_TEMPFAIL)
975198d9be0Seric 	{
9768557d168Seric 		(void) strcpy(buf, SysExMsg[i]);
977d4bd8f0eSbostic #ifdef NAMED_BIND
978f28da541Smiriam 		if (h_errno == TRY_AGAIN)
979f28da541Smiriam 		{
980f28da541Smiriam 			extern char *errstring();
981f28da541Smiriam 
982f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
983f28da541Smiriam 		}
984f28da541Smiriam 		else
985d4bd8f0eSbostic #endif
986f28da541Smiriam 		{
9878557d168Seric 			if (errno != 0)
988198d9be0Seric 			{
98987c9b3e7Seric 				extern char *errstring();
9908557d168Seric 
991d87e85f3Seric 				statmsg = errstring(errno);
992d87e85f3Seric 			}
993d87e85f3Seric 			else
994d87e85f3Seric 			{
995d87e85f3Seric #ifdef SMTP
996d87e85f3Seric 				extern char SmtpError[];
997d87e85f3Seric 
998d87e85f3Seric 				statmsg = SmtpError;
999d87e85f3Seric #else SMTP
1000d87e85f3Seric 				statmsg = NULL;
1001d87e85f3Seric #endif SMTP
1002d87e85f3Seric 			}
1003f28da541Smiriam 		}
1004d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1005d87e85f3Seric 		{
100687c9b3e7Seric 			(void) strcat(buf, ": ");
1007d87e85f3Seric 			(void) strcat(buf, statmsg);
10088557d168Seric 		}
1009198d9be0Seric 		statmsg = buf;
1010198d9be0Seric 	}
101125a99e2eSeric 	else
1012d87e85f3Seric 	{
101325a99e2eSeric 		statmsg = SysExMsg[i];
1014d87e85f3Seric 	}
1015588cad61Seric 
1016588cad61Seric 	/*
1017588cad61Seric 	**  Print the message as appropriate
1018588cad61Seric 	*/
1019588cad61Seric 
1020198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
10215826d9d3Seric 		message(Arpa_Info, &statmsg[4]);
102225a99e2eSeric 	else
102325a99e2eSeric 	{
1024c1f9df2cSeric 		Errors++;
10255826d9d3Seric 		usrerr(statmsg);
102625a99e2eSeric 	}
102725a99e2eSeric 
102825a99e2eSeric 	/*
102925a99e2eSeric 	**  Final cleanup.
103025a99e2eSeric 	**	Log a record of the transaction.  Compute the new
103125a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
103225a99e2eSeric 	**	that.
103325a99e2eSeric 	*/
103425a99e2eSeric 
103561f5a1d4Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
1036eb238f8cSeric 		logdelivery(&statmsg[4]);
1037eb238f8cSeric 
1038eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1039eb238f8cSeric 		setstat(stat);
1040198d9be0Seric 	if (stat != EX_OK)
1041198d9be0Seric 	{
1042198d9be0Seric 		if (e->e_message != NULL)
1043198d9be0Seric 			free(e->e_message);
1044198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1045198d9be0Seric 	}
10468557d168Seric 	errno = 0;
1047d4bd8f0eSbostic #ifdef NAMED_BIND
1048f28da541Smiriam 	h_errno = 0;
1049d4bd8f0eSbostic #endif
1050eb238f8cSeric }
1051eb238f8cSeric /*
1052eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1053eb238f8cSeric **
1054eb238f8cSeric **	Parameters:
1055eb238f8cSeric **		stat -- the message to print for the status
1056eb238f8cSeric **
1057eb238f8cSeric **	Returns:
1058eb238f8cSeric **		none
1059eb238f8cSeric **
1060eb238f8cSeric **	Side Effects:
1061eb238f8cSeric **		none
1062eb238f8cSeric */
1063eb238f8cSeric 
1064eb238f8cSeric logdelivery(stat)
1065eb238f8cSeric 	char *stat;
10665cf56be3Seric {
10675cf56be3Seric 	extern char *pintvl();
10685cf56be3Seric 
1069eb238f8cSeric # ifdef LOG
10705cf56be3Seric 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id,
1071eb238f8cSeric 	       CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat);
107225a99e2eSeric # endif LOG
107325a99e2eSeric }
107425a99e2eSeric /*
107551552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
107625a99e2eSeric **
107751552439Seric **	This can be made an arbitrary message separator by changing $l
107851552439Seric **
10799b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
10809b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
10819b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
10829b6c17a6Seric **	this kind of antique garbage????
108325a99e2eSeric **
108425a99e2eSeric **	Parameters:
108551552439Seric **		fp -- the file to output to.
108651552439Seric **		m -- the mailer describing this entry.
108725a99e2eSeric **
108825a99e2eSeric **	Returns:
108951552439Seric **		none
109025a99e2eSeric **
109125a99e2eSeric **	Side Effects:
109251552439Seric **		outputs some text to fp.
109325a99e2eSeric */
109425a99e2eSeric 
109577b52738Seric putfromline(fp, m)
109651552439Seric 	register FILE *fp;
109751552439Seric 	register MAILER *m;
109825a99e2eSeric {
10999b6c17a6Seric 	char *template = "\001l\n";
110051552439Seric 	char buf[MAXLINE];
110125a99e2eSeric 
110257fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
110351552439Seric 		return;
110413bbc08cSeric 
11052c7e1b8dSeric # ifdef UGLYUUCP
110657fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
110774b6e67bSeric 	{
1108ea09d6edSeric 		char *bang;
1109ea09d6edSeric 		char xbuf[MAXLINE];
111074b6e67bSeric 
11119b6c17a6Seric 		expand("\001g", buf, &buf[sizeof buf - 1], CurEnv);
1112ea09d6edSeric 		bang = index(buf, '!');
111374b6e67bSeric 		if (bang == NULL)
1114ea09d6edSeric 			syserr("No ! in UUCP! (%s)", buf);
111574b6e67bSeric 		else
1116588cad61Seric 		{
1117ea09d6edSeric 			*bang++ = '\0';
11189b6c17a6Seric 			(void) sprintf(xbuf, "From %s  \001d remote from %s\n", bang, buf);
1119ea09d6edSeric 			template = xbuf;
112074b6e67bSeric 		}
1121588cad61Seric 	}
11222c7e1b8dSeric # endif UGLYUUCP
1123ea09d6edSeric 	expand(template, buf, &buf[sizeof buf - 1], CurEnv);
112477b52738Seric 	putline(buf, fp, m);
1125bc6e2962Seric }
1126bc6e2962Seric /*
112751552439Seric **  PUTBODY -- put the body of a message.
112851552439Seric **
112951552439Seric **	Parameters:
113051552439Seric **		fp -- file to output onto.
113177b52738Seric **		m -- a mailer descriptor to control output format.
11329a6a5f55Seric **		e -- the envelope to put out.
113351552439Seric **
113451552439Seric **	Returns:
113551552439Seric **		none.
113651552439Seric **
113751552439Seric **	Side Effects:
113851552439Seric **		The message is written onto fp.
113951552439Seric */
114051552439Seric 
114177b52738Seric putbody(fp, m, e)
114251552439Seric 	FILE *fp;
1143588cad61Seric 	MAILER *m;
11449a6a5f55Seric 	register ENVELOPE *e;
114551552439Seric {
114677b52738Seric 	char buf[MAXLINE];
114751552439Seric 
114851552439Seric 	/*
114951552439Seric 	**  Output the body of the message
115051552439Seric 	*/
115151552439Seric 
11529a6a5f55Seric 	if (e->e_dfp == NULL)
115351552439Seric 	{
11549a6a5f55Seric 		if (e->e_df != NULL)
11559a6a5f55Seric 		{
11569a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
11579a6a5f55Seric 			if (e->e_dfp == NULL)
11588f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
11598f9146b0Srick 				e->e_df, e->e_to, e->e_from);
11609a6a5f55Seric 		}
11619a6a5f55Seric 		else
116277b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
11639a6a5f55Seric 	}
11649a6a5f55Seric 	if (e->e_dfp != NULL)
11659a6a5f55Seric 	{
11669a6a5f55Seric 		rewind(e->e_dfp);
116777b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
116824fc8aeeSeric 		{
116924fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1170d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
11713462ad9eSeric 				(void) putc('>', fp);
117277b52738Seric 			putline(buf, fp, m);
117324fc8aeeSeric 		}
117451552439Seric 
11759a6a5f55Seric 		if (ferror(e->e_dfp))
117651552439Seric 		{
117751552439Seric 			syserr("putbody: read error");
117851552439Seric 			ExitStat = EX_IOERR;
117951552439Seric 		}
118051552439Seric 	}
118151552439Seric 
118251552439Seric 	(void) fflush(fp);
118351552439Seric 	if (ferror(fp) && errno != EPIPE)
118451552439Seric 	{
118551552439Seric 		syserr("putbody: write error");
118651552439Seric 		ExitStat = EX_IOERR;
118751552439Seric 	}
118851552439Seric 	errno = 0;
118925a99e2eSeric }
119025a99e2eSeric /*
119125a99e2eSeric **  MAILFILE -- Send a message to a file.
119225a99e2eSeric **
1193f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1194f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1195f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1196f129ec7dSeric **	sendmail runs as root.
1197f129ec7dSeric **
1198588cad61Seric **	This could be done as a subordinate mailer, except that it
1199588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1200588cad61Seric **	view this as being sufficiently important as to include it
1201588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1202588cad61Seric **	to create another process plus some pipes to save the message.
1203588cad61Seric **
120425a99e2eSeric **	Parameters:
120525a99e2eSeric **		filename -- the name of the file to send to.
12066259796dSeric **		ctladdr -- the controlling address header -- includes
12076259796dSeric **			the userid/groupid to be when sending.
120825a99e2eSeric **
120925a99e2eSeric **	Returns:
121025a99e2eSeric **		The exit code associated with the operation.
121125a99e2eSeric **
121225a99e2eSeric **	Side Effects:
121325a99e2eSeric **		none.
121425a99e2eSeric */
121525a99e2eSeric 
12166259796dSeric mailfile(filename, ctladdr)
121725a99e2eSeric 	char *filename;
12186259796dSeric 	ADDRESS *ctladdr;
121925a99e2eSeric {
122025a99e2eSeric 	register FILE *f;
122132d19d43Seric 	register int pid;
12228f9146b0Srick 	ENVELOPE *e = CurEnv;
122325a99e2eSeric 
122432d19d43Seric 	/*
122532d19d43Seric 	**  Fork so we can change permissions here.
122632d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
122732d19d43Seric 	**	the complications of calling subroutines, etc.
122832d19d43Seric 	*/
122932d19d43Seric 
123032d19d43Seric 	DOFORK(fork);
123132d19d43Seric 
123232d19d43Seric 	if (pid < 0)
123332d19d43Seric 		return (EX_OSERR);
123432d19d43Seric 	else if (pid == 0)
123532d19d43Seric 	{
123632d19d43Seric 		/* child -- actually write to file */
1237f129ec7dSeric 		struct stat stb;
1238f129ec7dSeric 
12390984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
12400984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
12410984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
12423462ad9eSeric 		(void) umask(OldUmask);
1243f129ec7dSeric 		if (stat(filename, &stb) < 0)
124424447f54Seric 		{
124524447f54Seric 			errno = 0;
1246e6e1265fSeric 			stb.st_mode = 0666;
124724447f54Seric 		}
1248f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1249f129ec7dSeric 			exit(EX_CANTCREAT);
125003827b5fSeric 		if (ctladdr == NULL)
12518f9146b0Srick 			ctladdr = &e->e_from;
12528f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
12538f9146b0Srick 		if (e->e_dfp == NULL &&  e->e_df != NULL)
12548f9146b0Srick 		{
12558f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
12568f9146b0Srick 			if (e->e_dfp == NULL) {
12578f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
12588f9146b0Srick 				e->e_df, e->e_to, e->e_from);
12598f9146b0Srick 			}
12608f9146b0Srick 		}
12618f9146b0Srick 
1262f129ec7dSeric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
1263e36b99e2Seric 		{
1264898a126bSbostic 			if (ctladdr->q_uid == 0) {
1265e36b99e2Seric 				(void) setgid(DefGid);
1266898a126bSbostic 				(void) initgroups(DefUser, DefGid);
1267898a126bSbostic 			} else {
12686259796dSeric 				(void) setgid(ctladdr->q_gid);
1269898a126bSbostic 				(void) initgroups(ctladdr->q_ruser?
1270898a126bSbostic 					ctladdr->q_ruser: ctladdr->q_user,
1271898a126bSbostic 					ctladdr->q_gid);
1272898a126bSbostic 			}
1273e36b99e2Seric 		}
1274f129ec7dSeric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
1275e36b99e2Seric 		{
1276e36b99e2Seric 			if (ctladdr->q_uid == 0)
1277e36b99e2Seric 				(void) setuid(DefUid);
1278e36b99e2Seric 			else
12796259796dSeric 				(void) setuid(ctladdr->q_uid);
1280e36b99e2Seric 		}
128127628d59Seric 		f = dfopen(filename, "a");
128225a99e2eSeric 		if (f == NULL)
128332d19d43Seric 			exit(EX_CANTCREAT);
128425a99e2eSeric 
128577b52738Seric 		putfromline(f, ProgMailer);
128677b52738Seric 		(*CurEnv->e_puthdr)(f, ProgMailer, CurEnv);
128777b52738Seric 		putline("\n", f, ProgMailer);
128877b52738Seric 		(*CurEnv->e_putbody)(f, ProgMailer, CurEnv);
128977b52738Seric 		putline("\n", f, ProgMailer);
1290db8841e9Seric 		(void) fclose(f);
129132d19d43Seric 		(void) fflush(stdout);
1292e36b99e2Seric 
129327628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1294c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
129532d19d43Seric 		exit(EX_OK);
129613bbc08cSeric 		/*NOTREACHED*/
129732d19d43Seric 	}
129832d19d43Seric 	else
129932d19d43Seric 	{
130032d19d43Seric 		/* parent -- wait for exit status */
1301588cad61Seric 		int st;
130232d19d43Seric 
1303588cad61Seric 		st = waitfor(pid);
1304588cad61Seric 		if ((st & 0377) != 0)
1305588cad61Seric 			return (EX_UNAVAILABLE);
1306588cad61Seric 		else
1307588cad61Seric 			return ((st >> 8) & 0377);
13088f9146b0Srick 		/*NOTREACHED*/
130932d19d43Seric 	}
131025a99e2eSeric }
1311ea4dc939Seric /*
1312ea4dc939Seric **  SENDALL -- actually send all the messages.
1313ea4dc939Seric **
1314ea4dc939Seric **	Parameters:
13150c52a0b3Seric **		e -- the envelope to send.
13167b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
13177b95031aSeric **			the current SendMode.
1318ea4dc939Seric **
1319ea4dc939Seric **	Returns:
1320ea4dc939Seric **		none.
1321ea4dc939Seric **
1322ea4dc939Seric **	Side Effects:
1323ea4dc939Seric **		Scans the send lists and sends everything it finds.
13240c52a0b3Seric **		Delivers any appropriate error messages.
1325276723a8Seric **		If we are running in a non-interactive mode, takes the
1326276723a8Seric **			appropriate action.
1327ea4dc939Seric */
1328ea4dc939Seric 
1329276723a8Seric sendall(e, mode)
13300c52a0b3Seric 	ENVELOPE *e;
1331276723a8Seric 	char mode;
1332ea4dc939Seric {
1333e77e673fSeric 	register ADDRESS *q;
133414a8ed7aSeric 	bool oldverbose;
1335276723a8Seric 	int pid;
1336e90b7da2Sbostic 	FILE *lockfp = NULL, *queueup();
1337ea4dc939Seric 
13387b95031aSeric 	/* determine actual delivery mode */
13397b95031aSeric 	if (mode == SM_DEFAULT)
13407b95031aSeric 	{
13415f73204aSeric 		extern bool shouldqueue();
13427b95031aSeric 
13435f73204aSeric 		if (shouldqueue(e->e_msgpriority))
13447b95031aSeric 			mode = SM_QUEUE;
13457b95031aSeric 		else
13467b95031aSeric 			mode = SendMode;
13477b95031aSeric 	}
13487b95031aSeric 
1349df864a8fSeric 	if (tTd(13, 1))
1350772e6e50Seric 	{
1351276723a8Seric 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
13520c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1353772e6e50Seric 	}
1354ea4dc939Seric 
13550c52a0b3Seric 	/*
1356276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1357588cad61Seric 	**	Check to make sure the hop count is reasonable.
1358588cad61Seric 	**	Delete sends to the sender in mailing lists.
1359276723a8Seric 	*/
1360276723a8Seric 
1361588cad61Seric 	CurEnv = e;
1362276723a8Seric 
1363588cad61Seric 	if (e->e_hopcount > MAXHOP)
1364276723a8Seric 	{
13658f9146b0Srick 		errno = 0;
13668f9146b0Srick 		syserr("sendall: too many hops %d (%d max): from %s, to %s",
13678f9146b0Srick 			e->e_hopcount, MAXHOP, e->e_from, e->e_to);
1368588cad61Seric 		return;
1369588cad61Seric 	}
1370588cad61Seric 
1371588cad61Seric 	if (!MeToo)
1372276723a8Seric 	{
1373f3d6c55cSeric 		extern ADDRESS *recipient();
1374f3d6c55cSeric 
1375588cad61Seric 		e->e_from.q_flags |= QDONTSEND;
1376f3d6c55cSeric 		(void) recipient(&e->e_from, &e->e_sendqueue);
1377276723a8Seric 	}
1378588cad61Seric 
1379588cad61Seric # ifdef QUEUE
1380b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1381b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1382b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
13838f9146b0Srick 		lockfp = queueup(e, TRUE, mode == SM_QUEUE);
1384276723a8Seric #endif QUEUE
1385276723a8Seric 
1386276723a8Seric 	oldverbose = Verbose;
1387276723a8Seric 	switch (mode)
1388276723a8Seric 	{
1389276723a8Seric 	  case SM_VERIFY:
1390276723a8Seric 		Verbose = TRUE;
1391276723a8Seric 		break;
1392276723a8Seric 
1393276723a8Seric 	  case SM_QUEUE:
1394b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1395276723a8Seric 		return;
1396276723a8Seric 
1397276723a8Seric 	  case SM_FORK:
13989a6a5f55Seric 		if (e->e_xfp != NULL)
13999a6a5f55Seric 			(void) fflush(e->e_xfp);
1400276723a8Seric 		pid = fork();
1401276723a8Seric 		if (pid < 0)
1402276723a8Seric 		{
1403276723a8Seric 			mode = SM_DELIVER;
1404276723a8Seric 			break;
1405276723a8Seric 		}
1406276723a8Seric 		else if (pid > 0)
1407a6fce3d8Seric 		{
1408a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1409b254bcb6Seric 			e->e_id = e->e_df = NULL;
14108f9146b0Srick 			if (lockfp != NULL)
14118f9146b0Srick 				(void) fclose(lockfp);
1412276723a8Seric 			return;
1413a6fce3d8Seric 		}
1414276723a8Seric 
1415276723a8Seric 		/* double fork to avoid zombies */
1416276723a8Seric 		if (fork() > 0)
1417276723a8Seric 			exit(EX_OK);
1418276723a8Seric 
1419a6fce3d8Seric 		/* be sure we are immune from the terminal */
1420769e215aSeric 		disconnect(FALSE);
1421a6fce3d8Seric 
1422276723a8Seric 		break;
1423276723a8Seric 	}
1424276723a8Seric 
1425276723a8Seric 	/*
14260c52a0b3Seric 	**  Run through the list and send everything.
14270c52a0b3Seric 	*/
14280c52a0b3Seric 
14290c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1430ea4dc939Seric 	{
1431276723a8Seric 		if (mode == SM_VERIFY)
1432ea4dc939Seric 		{
1433a6fce3d8Seric 			e->e_to = q->q_paddr;
1434e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1435ea4dc939Seric 				message(Arpa_Info, "deliverable");
1436ea4dc939Seric 		}
1437ea4dc939Seric 		else
1438588cad61Seric 			(void) deliver(e, q);
1439ea4dc939Seric 	}
144014a8ed7aSeric 	Verbose = oldverbose;
14410c52a0b3Seric 
14420c52a0b3Seric 	/*
14430c52a0b3Seric 	**  Now run through and check for errors.
14440c52a0b3Seric 	*/
14450c52a0b3Seric 
14468f9146b0Srick 	if (mode == SM_VERIFY) {
14478f9146b0Srick 		if (lockfp != NULL)
14488f9146b0Srick 			(void) fclose(lockfp);
14490c52a0b3Seric 		return;
14508f9146b0Srick 	}
14510c52a0b3Seric 
14520c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14530c52a0b3Seric 	{
14540c52a0b3Seric 		register ADDRESS *qq;
14550c52a0b3Seric 
1456df864a8fSeric 		if (tTd(13, 3))
1457df864a8fSeric 		{
1458df864a8fSeric 			printf("Checking ");
1459df864a8fSeric 			printaddr(q, FALSE);
1460df864a8fSeric 		}
1461df864a8fSeric 
1462b254bcb6Seric 		/* only send errors if the message failed */
1463b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1464b254bcb6Seric 			continue;
14650c52a0b3Seric 
14660c52a0b3Seric 		/* we have an address that failed -- find the parent */
14670c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
14680c52a0b3Seric 		{
14690c52a0b3Seric 			char obuf[MAXNAME + 6];
14700c52a0b3Seric 			extern char *aliaslookup();
14710c52a0b3Seric 
14720c52a0b3Seric 			/* we can only have owners for local addresses */
147357fc6f17Seric 			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
14740c52a0b3Seric 				continue;
14750c52a0b3Seric 
14760c52a0b3Seric 			/* see if the owner list exists */
14770c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1478cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1479cec031e3Seric 				(void) strcat(obuf, "owner");
1480cec031e3Seric 			else
14810c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
1482ef137175Sbostic 			makelower(obuf);
14830c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
14840c52a0b3Seric 				continue;
14850c52a0b3Seric 
1486df864a8fSeric 			if (tTd(13, 4))
1487df864a8fSeric 				printf("Errors to %s\n", obuf);
1488df864a8fSeric 
14890c52a0b3Seric 			/* owner list exists -- add it to the error queue */
1490e3e4ed86Seric 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue);
149153e3fa05Seric 			ErrorMode = EM_MAIL;
14920c52a0b3Seric 			break;
14930c52a0b3Seric 		}
14940c52a0b3Seric 
14950c52a0b3Seric 		/* if we did not find an owner, send to the sender */
14967455aa0bSeric 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1497e3e4ed86Seric 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue);
14980c52a0b3Seric 	}
1499276723a8Seric 
15008f9146b0Srick 	/* this removes the lock on the file */
15018f9146b0Srick 	if (lockfp != NULL)
15028f9146b0Srick 		(void) fclose(lockfp);
15038f9146b0Srick 
1504276723a8Seric 	if (mode == SM_FORK)
1505276723a8Seric 		finis();
15060c52a0b3Seric }
1507