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*95f16dc0Seric static char sccsid[] = "@(#)deliver.c	5.54 (Berkeley) 02/26/92";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14911693bfSbostic #include <sys/signal.h>
15c77d1c25Seric #include <sys/stat.h>
16f28da541Smiriam #include <netdb.h>
17fccb3f7aSbostic #include <fcntl.h>
18911693bfSbostic #include <errno.h>
19134746fbSeric #ifdef NAMED_BIND
2012eec323Sbostic #include <sys/param.h>
21912a731aSbostic #include <arpa/nameser.h>
22912a731aSbostic #include <resolv.h>
23134746fbSeric #endif
2425a99e2eSeric 
2525a99e2eSeric /*
2613bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
2713bbc08cSeric **
2813bbc08cSeric **	This routine delivers to everyone on the same host as the
2913bbc08cSeric **	user on the head of the list.  It is clever about mailers
3013bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
3113bbc08cSeric **	that it will deliver to all these addresses however -- so
3213bbc08cSeric **	deliver should be called once for each address on the
3313bbc08cSeric **	list.
3425a99e2eSeric **
3525a99e2eSeric **	Parameters:
36588cad61Seric **		e -- the envelope to deliver.
37c77d1c25Seric **		firstto -- head of the address list to deliver to.
3825a99e2eSeric **
3925a99e2eSeric **	Returns:
4025a99e2eSeric **		zero -- successfully delivered.
4125a99e2eSeric **		else -- some failure, see ExitStat for more info.
4225a99e2eSeric **
4325a99e2eSeric **	Side Effects:
4425a99e2eSeric **		The standard input is passed off to someone.
4525a99e2eSeric */
4625a99e2eSeric 
47588cad61Seric deliver(e, firstto)
48588cad61Seric 	register ENVELOPE *e;
49c77d1c25Seric 	ADDRESS *firstto;
5025a99e2eSeric {
5178442df3Seric 	char *host;			/* host being sent to */
5278442df3Seric 	char *user;			/* user being sent to */
5325a99e2eSeric 	char **pvp;
545dfc646bSeric 	register char **mvp;
5525a99e2eSeric 	register char *p;
56588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
576259796dSeric 	ADDRESS *ctladdr;
58c77d1c25Seric 	register ADDRESS *to = firstto;
59c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
60772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
61911693bfSbostic 	int rcode;			/* response code */
62c23ed322Seric 	char *from;			/* pointer to from person */
63ee6bf8dfSeric 	char *pv[MAXPV+1];
64ee6bf8dfSeric 	char tobuf[MAXLINE-50];		/* text line of to people */
65ee6bf8dfSeric 	char buf[MAXNAME];
66ee6bf8dfSeric 	char tfrombuf[MAXNAME];		/* translated from person */
67c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
68ee6bf8dfSeric 	extern bool checkcompat();
69ee6bf8dfSeric 	extern ADDRESS *getctladdr();
70ee6bf8dfSeric 	extern char *remotename();
7125a99e2eSeric 
7235490626Seric 	errno = 0;
73da2935e1Seric 	if (bitset(QDONTSEND, to->q_flags))
745dfc646bSeric 		return (0);
7525a99e2eSeric 
76134746fbSeric #ifdef NAMED_BIND
77912a731aSbostic 	/* unless interactive, try twice, over a minute */
78912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
79912a731aSbostic 		_res.retrans = 30;
80912a731aSbostic 		_res.retry = 2;
81912a731aSbostic 	}
82d4bd8f0eSbostic #endif
83912a731aSbostic 
8451552439Seric 	m = to->q_mailer;
8551552439Seric 	host = to->q_host;
8651552439Seric 
876ef48975Seric 	if (tTd(10, 1))
885dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
8951552439Seric 			m->m_mno, host, to->q_user);
90f3dbc832Seric 
91f3dbc832Seric 	/*
92f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
93f3dbc832Seric 	**  connections now, just mark these addresses and return.
94f3dbc832Seric 	**	This is useful if we want to batch connections to
95f3dbc832Seric 	**	reduce load.  This will cause the messages to be
96f3dbc832Seric 	**	queued up, and a daemon will come along to send the
97f3dbc832Seric 	**	messages later.
98f3dbc832Seric 	**		This should be on a per-mailer basis.
99f3dbc832Seric 	*/
100f3dbc832Seric 
10157fc6f17Seric 	if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) &&
102317680d6Seric 	    !Verbose)
103f3dbc832Seric 	{
104f3dbc832Seric 		for (; to != NULL; to = to->q_next)
105f4560e80Seric 		{
106f4560e80Seric 			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
107f4560e80Seric 				continue;
108f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
109588cad61Seric 			e->e_to = to->q_paddr;
110eb238f8cSeric 			message(Arpa_Info, "queued");
111eb238f8cSeric 			if (LogLevel > 4)
112eb238f8cSeric 				logdelivery("queued");
113f4560e80Seric 		}
114588cad61Seric 		e->e_to = NULL;
115f3dbc832Seric 		return (0);
116f3dbc832Seric 	}
117f3dbc832Seric 
11825a99e2eSeric 	/*
1195dfc646bSeric 	**  Do initial argv setup.
1205dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
1215dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
1225dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
1235dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
1245dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
1253bea8136Seric 	**		The from address rewrite is expected to make
1263bea8136Seric 	**		the address relative to the other end.
1275dfc646bSeric 	*/
1285dfc646bSeric 
12978442df3Seric 	/* rewrite from address, using rewriting rules */
130c23ed322Seric 	(void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, TRUE));
131c23ed322Seric 	if (e->e_returnpath == e->e_sender)
132c23ed322Seric 	{
133c23ed322Seric 		from = rpathbuf;
134c23ed322Seric 	}
135c23ed322Seric 	else
136c23ed322Seric 	{
137c23ed322Seric 		(void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, TRUE));
138c23ed322Seric 		from = tfrombuf;
139c23ed322Seric 	}
14078442df3Seric 
141c23ed322Seric 	define('f', e->e_returnpath, e);	/* raw return path */
142c23ed322Seric 	define('<', rpathbuf, e);		/* translated return path */
143c23ed322Seric 	define('g', from, e);			/* translated sender */
144588cad61Seric 	define('h', host, e);			/* to host */
1455dfc646bSeric 	Errors = 0;
1465dfc646bSeric 	pvp = pv;
1475dfc646bSeric 	*pvp++ = m->m_argv[0];
1485dfc646bSeric 
1495dfc646bSeric 	/* insert -f or -r flag as appropriate */
15057fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
1515dfc646bSeric 	{
15257fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
1535dfc646bSeric 			*pvp++ = "-f";
1545dfc646bSeric 		else
1555dfc646bSeric 			*pvp++ = "-r";
156c23ed322Seric 		*pvp++ = newstr(rpathbuf);
1575dfc646bSeric 	}
1585dfc646bSeric 
1595dfc646bSeric 	/*
1605dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1615dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1625dfc646bSeric 	**  be one of these, and there are only a few more slots
1635dfc646bSeric 	**  in the pv after it.
1645dfc646bSeric 	*/
1655dfc646bSeric 
1665dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1675dfc646bSeric 	{
1689b6c17a6Seric 		while ((p = index(p, '\001')) != NULL)
1695dfc646bSeric 			if (*++p == 'u')
1705dfc646bSeric 				break;
1715dfc646bSeric 		if (p != NULL)
1725dfc646bSeric 			break;
1735dfc646bSeric 
1745dfc646bSeric 		/* this entry is safe -- go ahead and process it */
175588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1765dfc646bSeric 		*pvp++ = newstr(buf);
1775dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1785dfc646bSeric 		{
1795dfc646bSeric 			syserr("Too many parameters to %s before $u", pv[0]);
1805dfc646bSeric 			return (-1);
1815dfc646bSeric 		}
1825dfc646bSeric 	}
183c579ef51Seric 
18433db8731Seric 	/*
18533db8731Seric 	**  If we have no substitution for the user name in the argument
18633db8731Seric 	**  list, we know that we must supply the names otherwise -- and
18733db8731Seric 	**  SMTP is the answer!!
18833db8731Seric 	*/
18933db8731Seric 
1905dfc646bSeric 	if (*mvp == NULL)
191c579ef51Seric 	{
192c579ef51Seric 		/* running SMTP */
1932c7e1b8dSeric # ifdef SMTP
194c579ef51Seric 		clever = TRUE;
195c579ef51Seric 		*pvp = NULL;
1962c7e1b8dSeric # else SMTP
19733db8731Seric 		/* oops!  we don't implement SMTP */
1982c7e1b8dSeric 		syserr("SMTP style mailer");
1992c7e1b8dSeric 		return (EX_SOFTWARE);
2002c7e1b8dSeric # endif SMTP
201c579ef51Seric 	}
2025dfc646bSeric 
2035dfc646bSeric 	/*
2045dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
2055dfc646bSeric 	**  run through our address list and append all the addresses
2065dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
2075dfc646bSeric 	**  always send another copy later.
2085dfc646bSeric 	*/
2095dfc646bSeric 
2105dfc646bSeric 	tobuf[0] = '\0';
211588cad61Seric 	e->e_to = tobuf;
2126259796dSeric 	ctladdr = NULL;
2135dfc646bSeric 	for (; to != NULL; to = to->q_next)
2145dfc646bSeric 	{
2155dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
21657fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
2175dfc646bSeric 			break;
2185dfc646bSeric 
2195dfc646bSeric 		/* if already sent or not for this host, don't send */
220da2935e1Seric 		if (bitset(QDONTSEND, to->q_flags) ||
221da2935e1Seric 		    strcmp(to->q_host, host) != 0 ||
222da2935e1Seric 		    to->q_mailer != firstto->q_mailer)
2235dfc646bSeric 			continue;
2246259796dSeric 
2254b22ea87Seric 		/* avoid overflowing tobuf */
226aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
2274b22ea87Seric 			break;
2284b22ea87Seric 
2296ef48975Seric 		if (tTd(10, 1))
230772e6e50Seric 		{
231772e6e50Seric 			printf("\nsend to ");
232772e6e50Seric 			printaddr(to, FALSE);
233772e6e50Seric 		}
234772e6e50Seric 
2356259796dSeric 		/* compute effective uid/gid when sending */
2367da1035fSeric 		if (to->q_mailer == ProgMailer)
2376259796dSeric 			ctladdr = getctladdr(to);
2386259796dSeric 
2395dfc646bSeric 		user = to->q_user;
240588cad61Seric 		e->e_to = to->q_paddr;
2415dfc646bSeric 		to->q_flags |= QDONTSEND;
2425dfc646bSeric 
2435dfc646bSeric 		/*
2445dfc646bSeric 		**  Check to see that these people are allowed to
2455dfc646bSeric 		**  talk to each other.
2462a6e0786Seric 		*/
2472a6e0786Seric 
24869582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
24969582d2fSeric 		{
25069582d2fSeric 			NoReturn = TRUE;
251672bec4aSeric 			usrerr("Message is too large; %ld bytes max", m->m_maxsize);
25269582d2fSeric 			giveresponse(EX_UNAVAILABLE, m, e);
25369582d2fSeric 			continue;
25469582d2fSeric 		}
2552a6e0786Seric 		if (!checkcompat(to))
2565dfc646bSeric 		{
257198d9be0Seric 			giveresponse(EX_UNAVAILABLE, m, e);
2585dfc646bSeric 			continue;
2595dfc646bSeric 		}
2602a6e0786Seric 
2612a6e0786Seric 		/*
2629ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2639ec9501bSeric 		**	about them.
26425a99e2eSeric 		*/
26525a99e2eSeric 
26657fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
26725a99e2eSeric 		{
2689ec9501bSeric 			stripquotes(user, TRUE);
2699ec9501bSeric 			stripquotes(host, TRUE);
2709ec9501bSeric 		}
2719ec9501bSeric 		else
2729ec9501bSeric 		{
2739ec9501bSeric 			stripquotes(user, FALSE);
2749ec9501bSeric 			stripquotes(host, FALSE);
27525a99e2eSeric 		}
27625a99e2eSeric 
277cdb828c5Seric 		/* hack attack -- delivermail compatibility */
278cdb828c5Seric 		if (m == ProgMailer && *user == '|')
279cdb828c5Seric 			user++;
280cdb828c5Seric 
28125a99e2eSeric 		/*
2823efaed6eSeric 		**  If an error message has already been given, don't
2833efaed6eSeric 		**	bother to send to this address.
2843efaed6eSeric 		**
2853efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2863efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2873efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2883efaed6eSeric 		*/
2893efaed6eSeric 
2906cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2913efaed6eSeric 			continue;
2923efaed6eSeric 
293f2fec898Seric 		/* save statistics.... */
294588cad61Seric 		markstats(e, to);
295f2fec898Seric 
2963efaed6eSeric 		/*
29725a99e2eSeric 		**  See if this user name is "special".
29825a99e2eSeric 		**	If the user name has a slash in it, assume that this
29951552439Seric 		**	is a file -- send it off without further ado.  Note
30051552439Seric 		**	that this type of addresses is not processed along
30151552439Seric 		**	with the others, so we fudge on the To person.
30225a99e2eSeric 		*/
30325a99e2eSeric 
3047da1035fSeric 		if (m == LocalMailer)
30525a99e2eSeric 		{
306a49f24c0Seric 			if (user[0] == '/')
30725a99e2eSeric 			{
3085826d9d3Seric 				rcode = mailfile(user, getctladdr(to));
309198d9be0Seric 				giveresponse(rcode, m, e);
310dde5acadSeric 				if (rcode == EX_OK)
311dde5acadSeric 					to->q_flags |= QSENT;
3125dfc646bSeric 				continue;
31325a99e2eSeric 			}
31425a99e2eSeric 		}
31525a99e2eSeric 
31613bbc08cSeric 		/*
31713bbc08cSeric 		**  Address is verified -- add this user to mailer
31813bbc08cSeric 		**  argv, and add it to the print list of recipients.
31913bbc08cSeric 		*/
32013bbc08cSeric 
321508daeccSeric 		/* link together the chain of recipients */
322508daeccSeric 		to->q_tchain = tochain;
323508daeccSeric 		tochain = to;
324508daeccSeric 
3255dfc646bSeric 		/* create list of users for error messages */
326db8841e9Seric 		(void) strcat(tobuf, ",");
327db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
328588cad61Seric 		define('u', user, e);		/* to user */
329588cad61Seric 		define('z', to->q_home, e);	/* user's home */
3305dfc646bSeric 
331c579ef51Seric 		/*
332508daeccSeric 		**  Expand out this user into argument list.
333c579ef51Seric 		*/
334c579ef51Seric 
335508daeccSeric 		if (!clever)
336c579ef51Seric 		{
337588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3385dfc646bSeric 			*pvp++ = newstr(buf);
3395dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3405dfc646bSeric 			{
3415dfc646bSeric 				/* allow some space for trailing parms */
3425dfc646bSeric 				break;
3435dfc646bSeric 			}
3445dfc646bSeric 		}
345c579ef51Seric 	}
3465dfc646bSeric 
347145b49b1Seric 	/* see if any addresses still exist */
348145b49b1Seric 	if (tobuf[0] == '\0')
349c579ef51Seric 	{
350588cad61Seric 		define('g', (char *) NULL, e);
351c23ed322Seric 		define('<', (char *) NULL, e);
352145b49b1Seric 		return (0);
353c579ef51Seric 	}
354145b49b1Seric 
3555dfc646bSeric 	/* print out messages as full list */
35663780dbdSeric 	e->e_to = tobuf + 1;
3575dfc646bSeric 
3585dfc646bSeric 	/*
3595dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3605dfc646bSeric 	*/
3615dfc646bSeric 
362c579ef51Seric 	while (!clever && *++mvp != NULL)
3635dfc646bSeric 	{
364588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
3655dfc646bSeric 		*pvp++ = newstr(buf);
3665dfc646bSeric 		if (pvp >= &pv[MAXPV])
3675dfc646bSeric 			syserr("deliver: pv overflow after $u for %s", pv[0]);
3685dfc646bSeric 	}
3695dfc646bSeric 	*pvp++ = NULL;
3705dfc646bSeric 
37125a99e2eSeric 	/*
37225a99e2eSeric 	**  Call the mailer.
3736328bdf7Seric 	**	The argument vector gets built, pipes
37425a99e2eSeric 	**	are created as necessary, and we fork & exec as
3756328bdf7Seric 	**	appropriate.
376c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
37725a99e2eSeric 	*/
37825a99e2eSeric 
37986b26461Seric 	if (ctladdr == NULL)
38086b26461Seric 		ctladdr = &e->e_from;
381134746fbSeric #ifdef NAMED_BIND
3822bcc6d2dSeric 	if (ConfigLevel < 2)
383912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
384134746fbSeric #endif
3852c7e1b8dSeric #ifdef SMTP
386134746fbSeric 	if (clever)
387134746fbSeric 	{
388911693bfSbostic 		rcode = EX_OK;
389134746fbSeric #ifdef NAMED_BIND
3900877bd4eSbostic 		if (host[0] && host[0] != '[')
391134746fbSeric 		{
3929a979fb2Seric 			expand("\001j", buf, &buf[sizeof(buf) - 1], e);
393134746fbSeric 			Nmx = getmxrr(host, MxHosts, buf, &rcode);
394134746fbSeric 		}
395134746fbSeric 		else
396134746fbSeric #endif
397134746fbSeric 		{
39860bcc2d9Sbostic 			Nmx = 1;
39960bcc2d9Sbostic 			MxHosts[0] = host;
400134746fbSeric 		}
401134746fbSeric 		if (Nmx >= 0)
402134746fbSeric 		{
403912a731aSbostic 			message(Arpa_Info, "Connecting to %s (%s)...",
404912a731aSbostic 			    MxHosts[0], m->m_name);
405912a731aSbostic 			if ((rcode = smtpinit(m, pv)) == EX_OK) {
406ded0d3daSkarels 				register char *t = tobuf;
407ded0d3daSkarels 				register int i;
408ded0d3daSkarels 
409588cad61Seric 				/* send the recipient list */
41063780dbdSeric 				tobuf[0] = '\0';
411911693bfSbostic 				for (to = tochain; to; to = to->q_tchain) {
41263780dbdSeric 					e->e_to = to->q_paddr;
413912a731aSbostic 					if ((i = smtprcpt(to, m)) != EX_OK) {
41483b7ddc9Seric 						markfailure(e, to, i);
415198d9be0Seric 						giveresponse(i, m, e);
41663780dbdSeric 					}
417911693bfSbostic 					else {
418911693bfSbostic 						*t++ = ',';
419911693bfSbostic 						for (p = to->q_paddr; *p; *t++ = *p++);
420588cad61Seric 					}
421588cad61Seric 				}
422588cad61Seric 
42363780dbdSeric 				/* now send the data */
42463780dbdSeric 				if (tobuf[0] == '\0')
42563780dbdSeric 					e->e_to = NULL;
426911693bfSbostic 				else {
42763780dbdSeric 					e->e_to = tobuf + 1;
42877b52738Seric 					rcode = smtpdata(m, e);
42963780dbdSeric 				}
43063780dbdSeric 
43163780dbdSeric 				/* now close the connection */
432a294c4b0Seric 				smtpquit(m);
43363780dbdSeric 			}
434c579ef51Seric 		}
435912a731aSbostic 	}
436c579ef51Seric 	else
437911693bfSbostic #endif /* SMTP */
438a05b3449Sbostic 	{
4399b100374Sbostic 		static int sendoff();
4409b100374Sbostic 
441912a731aSbostic 		message(Arpa_Info, "Connecting to %s (%s)...", host, m->m_name);
44277b52738Seric 		rcode = sendoff(e, m, pv, ctladdr);
443a05b3449Sbostic 	}
444134746fbSeric #ifdef NAMED_BIND
4452bcc6d2dSeric 	if (ConfigLevel < 2)
446912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
447134746fbSeric #endif
4485dfc646bSeric 
449c77d1c25Seric 	/*
45063780dbdSeric 	**  Do final status disposal.
45163780dbdSeric 	**	We check for something in tobuf for the SMTP case.
452c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
453c77d1c25Seric 	**		addressees.
454c77d1c25Seric 	*/
455c77d1c25Seric 
45663780dbdSeric 	if (tobuf[0] != '\0')
457198d9be0Seric 		giveresponse(rcode, m, e);
458772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
459dde5acadSeric 		if (rcode != EX_OK)
46083b7ddc9Seric 			markfailure(e, to, rcode);
461dde5acadSeric 		else
462dde5acadSeric 			to->q_flags |= QSENT;
463c77d1c25Seric 
46435490626Seric 	errno = 0;
465588cad61Seric 	define('g', (char *) NULL, e);
466c23ed322Seric 	define('<', (char *) NULL, e);
4675826d9d3Seric 	return (rcode);
46825a99e2eSeric }
4695dfc646bSeric /*
47083b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
47183b7ddc9Seric **
47283b7ddc9Seric **	Parameters:
47383b7ddc9Seric **		e -- the envelope we are sending.
47483b7ddc9Seric **		q -- the address to mark.
47583b7ddc9Seric **		rcode -- the code signifying the particular failure.
47683b7ddc9Seric **
47783b7ddc9Seric **	Returns:
47883b7ddc9Seric **		none.
47983b7ddc9Seric **
48083b7ddc9Seric **	Side Effects:
48183b7ddc9Seric **		marks the address (and possibly the envelope) with the
48283b7ddc9Seric **			failure so that an error will be returned or
48383b7ddc9Seric **			the message will be queued, as appropriate.
48483b7ddc9Seric */
48583b7ddc9Seric 
48683b7ddc9Seric markfailure(e, q, rcode)
48783b7ddc9Seric 	register ENVELOPE *e;
48883b7ddc9Seric 	register ADDRESS *q;
48983b7ddc9Seric 	int rcode;
49083b7ddc9Seric {
49183b7ddc9Seric 	if (rcode == EX_OK)
49283b7ddc9Seric 		return;
493ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
49483b7ddc9Seric 		q->q_flags |= QBADADDR;
49583b7ddc9Seric 	else if (curtime() > e->e_ctime + TimeOut)
49683b7ddc9Seric 	{
49783b7ddc9Seric 		extern char *pintvl();
498198d9be0Seric 		char buf[MAXLINE];
49983b7ddc9Seric 
50083b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
501198d9be0Seric 		{
502198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
50383b7ddc9Seric 				pintvl(TimeOut, FALSE));
504198d9be0Seric 			if (e->e_message != NULL)
505198d9be0Seric 				free(e->e_message);
506198d9be0Seric 			e->e_message = newstr(buf);
507198d9be0Seric 			message(Arpa_Info, buf);
508198d9be0Seric 		}
50983b7ddc9Seric 		q->q_flags |= QBADADDR;
51083b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
51183b7ddc9Seric 	}
51283b7ddc9Seric 	else
51383b7ddc9Seric 		q->q_flags |= QQUEUEUP;
51483b7ddc9Seric }
51583b7ddc9Seric /*
51632d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
51732d19d43Seric **
51832d19d43Seric **	This MUST be a macro, since after a vfork we are running
51932d19d43Seric **	two processes on the same stack!!!
52032d19d43Seric **
52132d19d43Seric **	Parameters:
52232d19d43Seric **		none.
52332d19d43Seric **
52432d19d43Seric **	Returns:
52532d19d43Seric **		From a macro???  You've got to be kidding!
52632d19d43Seric **
52732d19d43Seric **	Side Effects:
52832d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
52932d19d43Seric **			pid of child in parent, zero in child.
53032d19d43Seric **			-1 on unrecoverable error.
53132d19d43Seric **
53232d19d43Seric **	Notes:
53332d19d43Seric **		I'm awfully sorry this looks so awful.  That's
53432d19d43Seric **		vfork for you.....
53532d19d43Seric */
53632d19d43Seric 
53732d19d43Seric # define NFORKTRIES	5
53884f7cd1bSeric 
53984f7cd1bSeric # ifndef FORK
54084f7cd1bSeric # define FORK	fork
54184f7cd1bSeric # endif
54232d19d43Seric 
54332d19d43Seric # define DOFORK(fORKfN) \
54432d19d43Seric {\
54532d19d43Seric 	register int i;\
54632d19d43Seric \
54711799049Seric 	for (i = NFORKTRIES; --i >= 0; )\
54832d19d43Seric 	{\
54932d19d43Seric 		pid = fORKfN();\
55032d19d43Seric 		if (pid >= 0)\
55132d19d43Seric 			break;\
55211799049Seric 		if (i > 0)\
5536c4635f6Seric 			sleep((unsigned) NFORKTRIES - i);\
55432d19d43Seric 	}\
55532d19d43Seric }
55632d19d43Seric /*
5572ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
5582ed72599Seric **
5592ed72599Seric **	Parameters:
5602ed72599Seric **		none.
5612ed72599Seric **
5622ed72599Seric **	Returns:
5632ed72599Seric **		pid of child in parent.
5642ed72599Seric **		zero in child.
5652ed72599Seric **		-1 on error.
5662ed72599Seric **
5672ed72599Seric **	Side Effects:
5682ed72599Seric **		returns twice, once in parent and once in child.
5692ed72599Seric */
5702ed72599Seric 
5712ed72599Seric dofork()
5722ed72599Seric {
5732ed72599Seric 	register int pid;
5742ed72599Seric 
5752ed72599Seric 	DOFORK(fork);
5762ed72599Seric 	return (pid);
5772ed72599Seric }
5782ed72599Seric /*
5795dfc646bSeric **  SENDOFF -- send off call to mailer & collect response.
5805dfc646bSeric **
5815dfc646bSeric **	Parameters:
582588cad61Seric **		e -- the envelope to mail.
5835dfc646bSeric **		m -- mailer descriptor.
5845dfc646bSeric **		pvp -- parameter vector to send to it.
5856259796dSeric **		ctladdr -- an address pointer controlling the
5866259796dSeric **			user/groupid etc. of the mailer.
5875dfc646bSeric **
5885dfc646bSeric **	Returns:
5895dfc646bSeric **		exit status of mailer.
5905dfc646bSeric **
5915dfc646bSeric **	Side Effects:
5925dfc646bSeric **		none.
5935dfc646bSeric */
594912a731aSbostic static
59577b52738Seric sendoff(e, m, pvp, ctladdr)
596588cad61Seric 	register ENVELOPE *e;
597588cad61Seric 	MAILER *m;
5985dfc646bSeric 	char **pvp;
5996259796dSeric 	ADDRESS *ctladdr;
6005dfc646bSeric {
601c579ef51Seric 	auto FILE *mfile;
602c579ef51Seric 	auto FILE *rfile;
6035dfc646bSeric 	register int i;
604c579ef51Seric 	int pid;
605c579ef51Seric 
606c579ef51Seric 	/*
607c579ef51Seric 	**  Create connection to mailer.
608c579ef51Seric 	*/
609c579ef51Seric 
610c579ef51Seric 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
611c579ef51Seric 	if (pid < 0)
612c579ef51Seric 		return (-1);
613c579ef51Seric 
614c579ef51Seric 	/*
615c579ef51Seric 	**  Format and send message.
616c579ef51Seric 	*/
617c579ef51Seric 
61877b52738Seric 	putfromline(mfile, m);
61977b52738Seric 	(*e->e_puthdr)(mfile, m, e);
62077b52738Seric 	putline("\n", mfile, m);
62177b52738Seric 	(*e->e_putbody)(mfile, m, e);
622c579ef51Seric 	(void) fclose(mfile);
6234d7d1e20Sbostic 	if (rfile != NULL)
6244d7d1e20Sbostic 		(void) fclose(rfile);
625c579ef51Seric 
626c579ef51Seric 	i = endmailer(pid, pvp[0]);
627bc6e2962Seric 
628bc6e2962Seric 	/* arrange a return receipt if requested */
62957fc6f17Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCAL, m->m_flags))
630bc6e2962Seric 	{
631588cad61Seric 		e->e_flags |= EF_SENDRECEIPT;
632bc6e2962Seric 		/* do we want to send back more info? */
633bc6e2962Seric 	}
634bc6e2962Seric 
635c579ef51Seric 	return (i);
636c579ef51Seric }
637c579ef51Seric /*
638c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
639c579ef51Seric **
640c579ef51Seric **	We should never get fatal errors (e.g., segmentation
641c579ef51Seric **	violation), so we report those specially.  For other
642c579ef51Seric **	errors, we choose a status message (into statmsg),
643c579ef51Seric **	and if it represents an error, we print it.
644c579ef51Seric **
645c579ef51Seric **	Parameters:
646c579ef51Seric **		pid -- pid of mailer.
647c579ef51Seric **		name -- name of mailer (for error messages).
648c579ef51Seric **
649c579ef51Seric **	Returns:
650c579ef51Seric **		exit code of mailer.
651c579ef51Seric **
652c579ef51Seric **	Side Effects:
653c579ef51Seric **		none.
654c579ef51Seric */
655c579ef51Seric 
656c579ef51Seric endmailer(pid, name)
657c579ef51Seric 	int pid;
658c579ef51Seric 	char *name;
659c579ef51Seric {
660588cad61Seric 	int st;
661c579ef51Seric 
66233db8731Seric 	/* in the IPC case there is nothing to wait for */
66333db8731Seric 	if (pid == 0)
66433db8731Seric 		return (EX_OK);
66533db8731Seric 
66633db8731Seric 	/* wait for the mailer process to die and collect status */
667588cad61Seric 	st = waitfor(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 **		pmfile -- pointer to mfile (to mailer) connection.
695c579ef51Seric **		prfile -- pointer to rfile (from mailer) connection.
696c579ef51Seric **
697c579ef51Seric **	Returns:
69833db8731Seric **		pid of mailer ( > 0 ).
699c579ef51Seric **		-1 on error.
70033db8731Seric **		zero on an IPC connection.
701c579ef51Seric **
702c579ef51Seric **	Side Effects:
703c579ef51Seric **		creates a mailer in a subprocess.
704c579ef51Seric */
705c579ef51Seric 
706c579ef51Seric openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
707588cad61Seric 	MAILER *m;
708c579ef51Seric 	char **pvp;
709c579ef51Seric 	ADDRESS *ctladdr;
710c579ef51Seric 	bool clever;
711c579ef51Seric 	FILE **pmfile;
712c579ef51Seric 	FILE **prfile;
713c579ef51Seric {
7145dfc646bSeric 	int pid;
715f8952a83Seric 	int mpvect[2];
716c579ef51Seric 	int rpvect[2];
717e90b7da2Sbostic 	FILE *mfile = NULL;
718e90b7da2Sbostic 	FILE *rfile = NULL;
7195dfc646bSeric 	extern FILE *fdopen();
7205dfc646bSeric 
7216ef48975Seric 	if (tTd(11, 1))
7225dfc646bSeric 	{
7238c57e552Seric 		printf("openmailer:");
7245dfc646bSeric 		printav(pvp);
7255dfc646bSeric 	}
72635490626Seric 	errno = 0;
7275dfc646bSeric 
728ef66a9d0Seric 	CurHostName = m->m_mailer;
729ef66a9d0Seric 
73033db8731Seric 	/*
73133db8731Seric 	**  Deal with the special case of mail handled through an IPC
73233db8731Seric 	**  connection.
73333db8731Seric 	**	In this case we don't actually fork.  We must be
73433db8731Seric 	**	running SMTP for this to work.  We will return a
73533db8731Seric 	**	zero pid to indicate that we are running IPC.
736e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
73733db8731Seric 	*/
73833db8731Seric 
739e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
740e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
741e7c1bd78Seric 	{
742e7c1bd78Seric 		*pmfile = stdout;
743e7c1bd78Seric 		*prfile = stdin;
744e7c1bd78Seric 		return (0);
745e7c1bd78Seric 	}
746e7c1bd78Seric 
747914346b1Seric 	if (strcmp(m->m_mailer, "[IPC]") == 0 ||
748914346b1Seric 	    strcmp(m->m_mailer, "[TCP]") == 0)
74933db8731Seric 	{
75084f7cd1bSeric #ifdef DAEMON
7515f73204aSeric 		register STAB *st;
7525f73204aSeric 		extern STAB *stab();
753ebc61751Sbloom 		register int i, j;
7541277f9a8Seric 		register u_short port;
75533db8731Seric 
756ef66a9d0Seric 		CurHostName = pvp[1];
75733db8731Seric 		if (!clever)
75833db8731Seric 			syserr("non-clever IPC");
75993b6e3cfSeric 		if (pvp[2] != NULL)
7601277f9a8Seric 			port = atoi(pvp[2]);
76193b6e3cfSeric 		else
7621277f9a8Seric 			port = 0;
763f1853fd7Seric 		for (j = 0; j < Nmx; j++)
764ebc61751Sbloom 		{
765f1853fd7Seric 			CurHostName = MxHosts[j];
766914346b1Seric 			/* see if we already know that this host is fried */
767f1853fd7Seric 			st = stab(MxHosts[j], ST_HOST, ST_FIND);
768914346b1Seric 			if (st == NULL || st->s_host.ho_exitstat == EX_OK)
769914346b1Seric 			{
770914346b1Seric 				message(Arpa_Info, "Connecting to %s (%s)...",
771912a731aSbostic 					MxHosts[j], m->m_name);
772914346b1Seric 				i = makeconnection(MxHosts[j], port,
773914346b1Seric 					pmfile, prfile,
774914346b1Seric 					bitnset(M_SECURE_PORT, m->m_flags));
775912a731aSbostic 			}
7765f73204aSeric 			else
777ef66a9d0Seric 			{
7785f73204aSeric 				i = st->s_host.ho_exitstat;
779ef66a9d0Seric 				errno = st->s_host.ho_errno;
780ef66a9d0Seric 			}
78133db8731Seric 			if (i != EX_OK)
782ed854c7bSeric 			{
7835f73204aSeric 				/* enter status of this host */
7845f73204aSeric 				if (st == NULL)
785f1853fd7Seric 					st = stab(MxHosts[j], ST_HOST, ST_ENTER);
7865f73204aSeric 				st->s_host.ho_exitstat = i;
7875f73204aSeric 				st->s_host.ho_errno = errno;
788ed854c7bSeric 				ExitStat = i;
789ebc61751Sbloom 				continue;
790ed854c7bSeric 			}
79133db8731Seric 			else
79233db8731Seric 				return (0);
793ebc61751Sbloom 		}
794ebc61751Sbloom 		return (-1);
795588cad61Seric #else DAEMON
796588cad61Seric 		syserr("openmailer: no IPC");
797588cad61Seric 		return (-1);
79833db8731Seric #endif DAEMON
799588cad61Seric 	}
80033db8731Seric 
8016328bdf7Seric 	/* create a pipe to shove the mail through */
802f8952a83Seric 	if (pipe(mpvect) < 0)
80325a99e2eSeric 	{
804588cad61Seric 		syserr("openmailer: pipe (to mailer)");
80525a99e2eSeric 		return (-1);
80625a99e2eSeric 	}
807c579ef51Seric 
8082c7e1b8dSeric #ifdef SMTP
809c579ef51Seric 	/* if this mailer speaks smtp, create a return pipe */
810c579ef51Seric 	if (clever && pipe(rpvect) < 0)
811c579ef51Seric 	{
812588cad61Seric 		syserr("openmailer: pipe (from mailer)");
813c579ef51Seric 		(void) close(mpvect[0]);
814c579ef51Seric 		(void) close(mpvect[1]);
815c579ef51Seric 		return (-1);
816c579ef51Seric 	}
8172c7e1b8dSeric #endif SMTP
818c579ef51Seric 
81933db8731Seric 	/*
82033db8731Seric 	**  Actually fork the mailer process.
82133db8731Seric 	**	DOFORK is clever about retrying.
8226984bfddSeric 	**
8236984bfddSeric 	**	Dispose of SIGCHLD signal catchers that may be laying
8246984bfddSeric 	**	around so that endmail will get it.
82533db8731Seric 	*/
82633db8731Seric 
8279a6a5f55Seric 	if (CurEnv->e_xfp != NULL)
8289a6a5f55Seric 		(void) fflush(CurEnv->e_xfp);		/* for debugging */
829588cad61Seric 	(void) fflush(stdout);
8306984bfddSeric # ifdef SIGCHLD
8316984bfddSeric 	(void) signal(SIGCHLD, SIG_DFL);
8326984bfddSeric # endif SIGCHLD
83384f7cd1bSeric 	DOFORK(FORK);
834f129ec7dSeric 	/* pid is set by DOFORK */
83525a99e2eSeric 	if (pid < 0)
83625a99e2eSeric 	{
83733db8731Seric 		/* failure */
838588cad61Seric 		syserr("openmailer: cannot fork");
839f8952a83Seric 		(void) close(mpvect[0]);
840f8952a83Seric 		(void) close(mpvect[1]);
841588cad61Seric #ifdef SMTP
842c579ef51Seric 		if (clever)
843c579ef51Seric 		{
844c579ef51Seric 			(void) close(rpvect[0]);
845c579ef51Seric 			(void) close(rpvect[1]);
846c579ef51Seric 		}
847588cad61Seric #endif SMTP
84825a99e2eSeric 		return (-1);
84925a99e2eSeric 	}
85025a99e2eSeric 	else if (pid == 0)
85125a99e2eSeric 	{
85213088b9fSeric 		int i;
8535f73204aSeric 		extern int DtableSize;
85413088b9fSeric 
85525a99e2eSeric 		/* child -- set up input & exec mailer */
85603ab8e55Seric 		/* make diagnostic output be standard output */
8578f0e7860Seric 		(void) signal(SIGINT, SIG_IGN);
8588f0e7860Seric 		(void) signal(SIGHUP, SIG_IGN);
8590984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
860f8952a83Seric 
861f8952a83Seric 		/* arrange to filter standard & diag output of command */
862c579ef51Seric 		if (clever)
863c579ef51Seric 		{
864c579ef51Seric 			(void) close(rpvect[0]);
865c579ef51Seric 			(void) close(1);
866c579ef51Seric 			(void) dup(rpvect[1]);
867c579ef51Seric 			(void) close(rpvect[1]);
868c579ef51Seric 		}
869276723a8Seric 		else if (OpMode == MD_SMTP || HoldErrs)
870f8952a83Seric 		{
871588cad61Seric 			/* put mailer output in transcript */
872f8952a83Seric 			(void) close(1);
8739a6a5f55Seric 			(void) dup(fileno(CurEnv->e_xfp));
874f8952a83Seric 		}
875db8841e9Seric 		(void) close(2);
876db8841e9Seric 		(void) dup(1);
877f8952a83Seric 
878f8952a83Seric 		/* arrange to get standard input */
879f8952a83Seric 		(void) close(mpvect[1]);
880db8841e9Seric 		(void) close(0);
881f8952a83Seric 		if (dup(mpvect[0]) < 0)
88225a99e2eSeric 		{
88325a99e2eSeric 			syserr("Cannot dup to zero!");
884a590b978Seric 			_exit(EX_OSERR);
88525a99e2eSeric 		}
886f8952a83Seric 		(void) close(mpvect[0]);
88757fc6f17Seric 		if (!bitnset(M_RESTR, m->m_flags))
8880984da9fSeric 		{
88953e3fa05Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
890e36b99e2Seric 			{
891e36b99e2Seric 				(void) setgid(DefGid);
892898a126bSbostic 				(void) initgroups(DefUser, DefGid);
893e36b99e2Seric 				(void) setuid(DefUid);
894e36b99e2Seric 			}
895e36b99e2Seric 			else
89669f29479Seric 			{
897e36b99e2Seric 				(void) setgid(ctladdr->q_gid);
898898a126bSbostic 				(void) initgroups(ctladdr->q_ruser?
899898a126bSbostic 					ctladdr->q_ruser: ctladdr->q_user,
900898a126bSbostic 					ctladdr->q_gid);
901e36b99e2Seric 				(void) setuid(ctladdr->q_uid);
90269f29479Seric 			}
9030984da9fSeric 		}
904588cad61Seric 
90513088b9fSeric 		/* arrange for all the files to be closed */
906fccb3f7aSbostic 		for (i = 3; i < DtableSize; i++) {
907fccb3f7aSbostic 			register int j;
908fccb3f7aSbostic 			if ((j = fcntl(i, F_GETFD, 0)) != -1)
909fccb3f7aSbostic 				(void)fcntl(i, F_SETFD, j|1);
910fccb3f7aSbostic 		}
91133db8731Seric 
91233db8731Seric 		/* try to execute the mailer */
9135df317aaSeric 		execve(m->m_mailer, pvp, UserEnviron);
91413088b9fSeric 		syserr("Cannot exec %s", m->m_mailer);
91595b76e4bSeric 		if (m == LocalMailer)
91655f33c03Seric 			_exit(EX_TEMPFAIL);
91795b76e4bSeric 		switch (errno)
91895b76e4bSeric 		{
91995b76e4bSeric 		  case EIO:
92095b76e4bSeric 		  case EAGAIN:
92195b76e4bSeric 		  case ENOMEM:
92295b76e4bSeric # ifdef EPROCLIM
92395b76e4bSeric 		  case EPROCLIM:
92495b76e4bSeric # endif
92595b76e4bSeric 			_exit(EX_TEMPFAIL);
92695b76e4bSeric 		}
927a590b978Seric 		_exit(EX_UNAVAILABLE);
92825a99e2eSeric 	}
92925a99e2eSeric 
930f8952a83Seric 	/*
931c579ef51Seric 	**  Set up return value.
932f8952a83Seric 	*/
933f8952a83Seric 
934f8952a83Seric 	(void) close(mpvect[0]);
935f8952a83Seric 	mfile = fdopen(mpvect[1], "w");
936c579ef51Seric 	if (clever)
93725a99e2eSeric 	{
938c579ef51Seric 		(void) close(rpvect[1]);
939c579ef51Seric 		rfile = fdopen(rpvect[0], "r");
9404d7d1e20Sbostic 	} else
9414d7d1e20Sbostic 		rfile = NULL;
942c579ef51Seric 
943c579ef51Seric 	*pmfile = mfile;
944c579ef51Seric 	*prfile = rfile;
945c579ef51Seric 
946c579ef51Seric 	return (pid);
94725a99e2eSeric }
94825a99e2eSeric /*
94925a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
95025a99e2eSeric **
95125a99e2eSeric **	Parameters:
95225a99e2eSeric **		stat -- the status code from the mailer (high byte
95325a99e2eSeric **			only; core dumps must have been taken care of
95425a99e2eSeric **			already).
95525a99e2eSeric **		m -- the mailer descriptor for this mailer.
95625a99e2eSeric **
95725a99e2eSeric **	Returns:
958db8841e9Seric **		none.
95925a99e2eSeric **
96025a99e2eSeric **	Side Effects:
961c1f9df2cSeric **		Errors may be incremented.
96225a99e2eSeric **		ExitStat may be set.
96325a99e2eSeric */
96425a99e2eSeric 
965198d9be0Seric giveresponse(stat, m, e)
96625a99e2eSeric 	int stat;
967588cad61Seric 	register MAILER *m;
968198d9be0Seric 	ENVELOPE *e;
96925a99e2eSeric {
97025a99e2eSeric 	register char *statmsg;
97125a99e2eSeric 	extern char *SysExMsg[];
97225a99e2eSeric 	register int i;
973d4bd8f0eSbostic 	extern int N_SysEx;
974d4bd8f0eSbostic #ifdef NAMED_BIND
975d4bd8f0eSbostic 	extern int h_errno;
976d4bd8f0eSbostic #endif
977198d9be0Seric 	char buf[MAXLINE];
97825a99e2eSeric 
9797d1fc79dSeric #ifdef lint
9807d1fc79dSeric 	if (m == NULL)
9817d1fc79dSeric 		return;
9827d1fc79dSeric #endif lint
9837d1fc79dSeric 
98413bbc08cSeric 	/*
98513bbc08cSeric 	**  Compute status message from code.
98613bbc08cSeric 	*/
98713bbc08cSeric 
98825a99e2eSeric 	i = stat - EX__BASE;
989588cad61Seric 	if (stat == 0)
990588cad61Seric 		statmsg = "250 Sent";
991588cad61Seric 	else if (i < 0 || i > N_SysEx)
992588cad61Seric 	{
993588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
994588cad61Seric 		stat = EX_UNAVAILABLE;
995588cad61Seric 		statmsg = buf;
996588cad61Seric 	}
997198d9be0Seric 	else if (stat == EX_TEMPFAIL)
998198d9be0Seric 	{
9998557d168Seric 		(void) strcpy(buf, SysExMsg[i]);
1000d4bd8f0eSbostic #ifdef NAMED_BIND
1001f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1002f28da541Smiriam 		{
1003f28da541Smiriam 			extern char *errstring();
1004f28da541Smiriam 
1005f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1006f28da541Smiriam 		}
1007f28da541Smiriam 		else
1008d4bd8f0eSbostic #endif
1009f28da541Smiriam 		{
10108557d168Seric 			if (errno != 0)
1011198d9be0Seric 			{
101287c9b3e7Seric 				extern char *errstring();
10138557d168Seric 
1014d87e85f3Seric 				statmsg = errstring(errno);
1015d87e85f3Seric 			}
1016d87e85f3Seric 			else
1017d87e85f3Seric 			{
1018d87e85f3Seric #ifdef SMTP
1019d87e85f3Seric 				extern char SmtpError[];
1020d87e85f3Seric 
1021d87e85f3Seric 				statmsg = SmtpError;
1022d87e85f3Seric #else SMTP
1023d87e85f3Seric 				statmsg = NULL;
1024d87e85f3Seric #endif SMTP
1025d87e85f3Seric 			}
1026f28da541Smiriam 		}
1027d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1028d87e85f3Seric 		{
102987c9b3e7Seric 			(void) strcat(buf, ": ");
1030d87e85f3Seric 			(void) strcat(buf, statmsg);
10318557d168Seric 		}
1032198d9be0Seric 		statmsg = buf;
1033198d9be0Seric 	}
103425a99e2eSeric 	else
1035d87e85f3Seric 	{
103625a99e2eSeric 		statmsg = SysExMsg[i];
1037d87e85f3Seric 	}
1038588cad61Seric 
1039588cad61Seric 	/*
1040588cad61Seric 	**  Print the message as appropriate
1041588cad61Seric 	*/
1042588cad61Seric 
1043198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
10445826d9d3Seric 		message(Arpa_Info, &statmsg[4]);
104525a99e2eSeric 	else
104625a99e2eSeric 	{
1047c1f9df2cSeric 		Errors++;
10485826d9d3Seric 		usrerr(statmsg);
104925a99e2eSeric 	}
105025a99e2eSeric 
105125a99e2eSeric 	/*
105225a99e2eSeric 	**  Final cleanup.
105325a99e2eSeric 	**	Log a record of the transaction.  Compute the new
105425a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
105525a99e2eSeric 	**	that.
105625a99e2eSeric 	*/
105725a99e2eSeric 
105861f5a1d4Seric 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
1059eb238f8cSeric 		logdelivery(&statmsg[4]);
1060eb238f8cSeric 
1061eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1062eb238f8cSeric 		setstat(stat);
1063198d9be0Seric 	if (stat != EX_OK)
1064198d9be0Seric 	{
1065198d9be0Seric 		if (e->e_message != NULL)
1066198d9be0Seric 			free(e->e_message);
1067198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1068198d9be0Seric 	}
10698557d168Seric 	errno = 0;
1070d4bd8f0eSbostic #ifdef NAMED_BIND
1071f28da541Smiriam 	h_errno = 0;
1072d4bd8f0eSbostic #endif
1073eb238f8cSeric }
1074eb238f8cSeric /*
1075eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1076eb238f8cSeric **
1077eb238f8cSeric **	Parameters:
1078eb238f8cSeric **		stat -- the message to print for the status
1079eb238f8cSeric **
1080eb238f8cSeric **	Returns:
1081eb238f8cSeric **		none
1082eb238f8cSeric **
1083eb238f8cSeric **	Side Effects:
1084eb238f8cSeric **		none
1085eb238f8cSeric */
1086eb238f8cSeric 
1087eb238f8cSeric logdelivery(stat)
1088eb238f8cSeric 	char *stat;
10895cf56be3Seric {
10905cf56be3Seric 	extern char *pintvl();
10915cf56be3Seric 
1092eb238f8cSeric # ifdef LOG
10935cf56be3Seric 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id,
1094eb238f8cSeric 	       CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat);
109525a99e2eSeric # endif LOG
109625a99e2eSeric }
109725a99e2eSeric /*
109851552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
109925a99e2eSeric **
110051552439Seric **	This can be made an arbitrary message separator by changing $l
110151552439Seric **
11029b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
11039b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
11049b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
11059b6c17a6Seric **	this kind of antique garbage????
110625a99e2eSeric **
110725a99e2eSeric **	Parameters:
110851552439Seric **		fp -- the file to output to.
110951552439Seric **		m -- the mailer describing this entry.
111025a99e2eSeric **
111125a99e2eSeric **	Returns:
111251552439Seric **		none
111325a99e2eSeric **
111425a99e2eSeric **	Side Effects:
111551552439Seric **		outputs some text to fp.
111625a99e2eSeric */
111725a99e2eSeric 
111877b52738Seric putfromline(fp, m)
111951552439Seric 	register FILE *fp;
112051552439Seric 	register MAILER *m;
112125a99e2eSeric {
11229b6c17a6Seric 	char *template = "\001l\n";
112351552439Seric 	char buf[MAXLINE];
112425a99e2eSeric 
112557fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
112651552439Seric 		return;
112713bbc08cSeric 
11282c7e1b8dSeric # ifdef UGLYUUCP
112957fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
113074b6e67bSeric 	{
1131ea09d6edSeric 		char *bang;
1132ea09d6edSeric 		char xbuf[MAXLINE];
113374b6e67bSeric 
1134c23ed322Seric 		expand("\001<", buf, &buf[sizeof buf - 1], CurEnv);
1135ea09d6edSeric 		bang = index(buf, '!');
113674b6e67bSeric 		if (bang == NULL)
1137ea09d6edSeric 			syserr("No ! in UUCP! (%s)", buf);
113874b6e67bSeric 		else
1139588cad61Seric 		{
1140ea09d6edSeric 			*bang++ = '\0';
11419b6c17a6Seric 			(void) sprintf(xbuf, "From %s  \001d remote from %s\n", bang, buf);
1142ea09d6edSeric 			template = xbuf;
114374b6e67bSeric 		}
1144588cad61Seric 	}
11452c7e1b8dSeric # endif UGLYUUCP
1146ea09d6edSeric 	expand(template, buf, &buf[sizeof buf - 1], CurEnv);
114777b52738Seric 	putline(buf, fp, m);
1148bc6e2962Seric }
1149bc6e2962Seric /*
115051552439Seric **  PUTBODY -- put the body of a message.
115151552439Seric **
115251552439Seric **	Parameters:
115351552439Seric **		fp -- file to output onto.
115477b52738Seric **		m -- a mailer descriptor to control output format.
11559a6a5f55Seric **		e -- the envelope to put out.
115651552439Seric **
115751552439Seric **	Returns:
115851552439Seric **		none.
115951552439Seric **
116051552439Seric **	Side Effects:
116151552439Seric **		The message is written onto fp.
116251552439Seric */
116351552439Seric 
116477b52738Seric putbody(fp, m, e)
116551552439Seric 	FILE *fp;
1166588cad61Seric 	MAILER *m;
11679a6a5f55Seric 	register ENVELOPE *e;
116851552439Seric {
116977b52738Seric 	char buf[MAXLINE];
117051552439Seric 
117151552439Seric 	/*
117251552439Seric 	**  Output the body of the message
117351552439Seric 	*/
117451552439Seric 
11759a6a5f55Seric 	if (e->e_dfp == NULL)
117651552439Seric 	{
11779a6a5f55Seric 		if (e->e_df != NULL)
11789a6a5f55Seric 		{
11799a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
11809a6a5f55Seric 			if (e->e_dfp == NULL)
11818f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
11828f9146b0Srick 				e->e_df, e->e_to, e->e_from);
11839a6a5f55Seric 		}
11849a6a5f55Seric 		else
118577b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
11869a6a5f55Seric 	}
11879a6a5f55Seric 	if (e->e_dfp != NULL)
11889a6a5f55Seric 	{
11899a6a5f55Seric 		rewind(e->e_dfp);
119077b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
119124fc8aeeSeric 		{
119224fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1193d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
11943462ad9eSeric 				(void) putc('>', fp);
119577b52738Seric 			putline(buf, fp, m);
119624fc8aeeSeric 		}
119751552439Seric 
11989a6a5f55Seric 		if (ferror(e->e_dfp))
119951552439Seric 		{
120051552439Seric 			syserr("putbody: read error");
120151552439Seric 			ExitStat = EX_IOERR;
120251552439Seric 		}
120351552439Seric 	}
120451552439Seric 
120551552439Seric 	(void) fflush(fp);
120651552439Seric 	if (ferror(fp) && errno != EPIPE)
120751552439Seric 	{
120851552439Seric 		syserr("putbody: write error");
120951552439Seric 		ExitStat = EX_IOERR;
121051552439Seric 	}
121151552439Seric 	errno = 0;
121225a99e2eSeric }
121325a99e2eSeric /*
121425a99e2eSeric **  MAILFILE -- Send a message to a file.
121525a99e2eSeric **
1216f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1217f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1218f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1219f129ec7dSeric **	sendmail runs as root.
1220f129ec7dSeric **
1221588cad61Seric **	This could be done as a subordinate mailer, except that it
1222588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1223588cad61Seric **	view this as being sufficiently important as to include it
1224588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1225588cad61Seric **	to create another process plus some pipes to save the message.
1226588cad61Seric **
122725a99e2eSeric **	Parameters:
122825a99e2eSeric **		filename -- the name of the file to send to.
12296259796dSeric **		ctladdr -- the controlling address header -- includes
12306259796dSeric **			the userid/groupid to be when sending.
123125a99e2eSeric **
123225a99e2eSeric **	Returns:
123325a99e2eSeric **		The exit code associated with the operation.
123425a99e2eSeric **
123525a99e2eSeric **	Side Effects:
123625a99e2eSeric **		none.
123725a99e2eSeric */
123825a99e2eSeric 
12396259796dSeric mailfile(filename, ctladdr)
124025a99e2eSeric 	char *filename;
12416259796dSeric 	ADDRESS *ctladdr;
124225a99e2eSeric {
124325a99e2eSeric 	register FILE *f;
124432d19d43Seric 	register int pid;
12458f9146b0Srick 	ENVELOPE *e = CurEnv;
124625a99e2eSeric 
124732d19d43Seric 	/*
124832d19d43Seric 	**  Fork so we can change permissions here.
124932d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
125032d19d43Seric 	**	the complications of calling subroutines, etc.
125132d19d43Seric 	*/
125232d19d43Seric 
125332d19d43Seric 	DOFORK(fork);
125432d19d43Seric 
125532d19d43Seric 	if (pid < 0)
125632d19d43Seric 		return (EX_OSERR);
125732d19d43Seric 	else if (pid == 0)
125832d19d43Seric 	{
125932d19d43Seric 		/* child -- actually write to file */
1260f129ec7dSeric 		struct stat stb;
1261f129ec7dSeric 
12620984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
12630984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
12640984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
12653462ad9eSeric 		(void) umask(OldUmask);
1266*95f16dc0Seric 
1267f129ec7dSeric 		if (stat(filename, &stb) < 0)
1268e6e1265fSeric 			stb.st_mode = 0666;
1269*95f16dc0Seric 
1270*95f16dc0Seric 		/* limit the errors to those actually caused in the child */
1271*95f16dc0Seric 		errno = 0;
1272*95f16dc0Seric 		ExitStat = EX_OK;
1273*95f16dc0Seric 
1274f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1275f129ec7dSeric 			exit(EX_CANTCREAT);
127603827b5fSeric 		if (ctladdr == NULL)
12778f9146b0Srick 			ctladdr = &e->e_from;
12788f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
12798f9146b0Srick 		if (e->e_dfp == NULL &&  e->e_df != NULL)
12808f9146b0Srick 		{
12818f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
1282*95f16dc0Seric 			if (e->e_dfp == NULL)
1283*95f16dc0Seric 			{
12848f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
12858f9146b0Srick 				e->e_df, e->e_to, e->e_from);
12868f9146b0Srick 			}
12878f9146b0Srick 		}
12888f9146b0Srick 
1289f129ec7dSeric 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
1290e36b99e2Seric 		{
1291*95f16dc0Seric 			if (ctladdr->q_uid == 0)
1292*95f16dc0Seric 			{
1293e36b99e2Seric 				(void) setgid(DefGid);
1294898a126bSbostic 				(void) initgroups(DefUser, DefGid);
1295*95f16dc0Seric 			}
1296*95f16dc0Seric 			else
1297*95f16dc0Seric 			{
12986259796dSeric 				(void) setgid(ctladdr->q_gid);
1299898a126bSbostic 				(void) initgroups(ctladdr->q_ruser?
1300898a126bSbostic 					ctladdr->q_ruser: ctladdr->q_user,
1301898a126bSbostic 					ctladdr->q_gid);
1302898a126bSbostic 			}
1303e36b99e2Seric 		}
1304f129ec7dSeric 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
1305e36b99e2Seric 		{
1306e36b99e2Seric 			if (ctladdr->q_uid == 0)
1307e36b99e2Seric 				(void) setuid(DefUid);
1308e36b99e2Seric 			else
13096259796dSeric 				(void) setuid(ctladdr->q_uid);
1310e36b99e2Seric 		}
1311*95f16dc0Seric 		FileName = filename;
1312*95f16dc0Seric 		LineNumber = 0;
131327628d59Seric 		f = dfopen(filename, "a");
131425a99e2eSeric 		if (f == NULL)
1315*95f16dc0Seric 		{
1316*95f16dc0Seric 			message("cannot open");
131732d19d43Seric 			exit(EX_CANTCREAT);
1318*95f16dc0Seric 		}
131925a99e2eSeric 
132077b52738Seric 		putfromline(f, ProgMailer);
132177b52738Seric 		(*CurEnv->e_puthdr)(f, ProgMailer, CurEnv);
132277b52738Seric 		putline("\n", f, ProgMailer);
132377b52738Seric 		(*CurEnv->e_putbody)(f, ProgMailer, CurEnv);
132477b52738Seric 		putline("\n", f, ProgMailer);
1325*95f16dc0Seric 		if (ferror(f))
1326*95f16dc0Seric 		{
1327*95f16dc0Seric 			message("I/O error");
1328*95f16dc0Seric 			setstat(EX_IOERR);
1329*95f16dc0Seric 		}
1330db8841e9Seric 		(void) fclose(f);
133132d19d43Seric 		(void) fflush(stdout);
1332e36b99e2Seric 
133327628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1334c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
1335*95f16dc0Seric 		exit(ExitStat);
133613bbc08cSeric 		/*NOTREACHED*/
133732d19d43Seric 	}
133832d19d43Seric 	else
133932d19d43Seric 	{
134032d19d43Seric 		/* parent -- wait for exit status */
1341588cad61Seric 		int st;
134232d19d43Seric 
1343588cad61Seric 		st = waitfor(pid);
1344588cad61Seric 		if ((st & 0377) != 0)
1345588cad61Seric 			return (EX_UNAVAILABLE);
1346588cad61Seric 		else
1347588cad61Seric 			return ((st >> 8) & 0377);
13488f9146b0Srick 		/*NOTREACHED*/
134932d19d43Seric 	}
135025a99e2eSeric }
1351ea4dc939Seric /*
1352ea4dc939Seric **  SENDALL -- actually send all the messages.
1353ea4dc939Seric **
1354ea4dc939Seric **	Parameters:
13550c52a0b3Seric **		e -- the envelope to send.
13567b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
13577b95031aSeric **			the current SendMode.
1358ea4dc939Seric **
1359ea4dc939Seric **	Returns:
1360ea4dc939Seric **		none.
1361ea4dc939Seric **
1362ea4dc939Seric **	Side Effects:
1363ea4dc939Seric **		Scans the send lists and sends everything it finds.
13640c52a0b3Seric **		Delivers any appropriate error messages.
1365276723a8Seric **		If we are running in a non-interactive mode, takes the
1366276723a8Seric **			appropriate action.
1367ea4dc939Seric */
1368ea4dc939Seric 
1369276723a8Seric sendall(e, mode)
13700c52a0b3Seric 	ENVELOPE *e;
1371276723a8Seric 	char mode;
1372ea4dc939Seric {
1373e77e673fSeric 	register ADDRESS *q;
137414a8ed7aSeric 	bool oldverbose;
1375276723a8Seric 	int pid;
1376dde5acadSeric 	int nsent;
13771c265a00Seric # ifdef LOCKF
13781c265a00Seric 	struct flock lfd;
13791c265a00Seric # endif
1380ea4dc939Seric 
13817b95031aSeric 	/* determine actual delivery mode */
13827b95031aSeric 	if (mode == SM_DEFAULT)
13837b95031aSeric 	{
13845f73204aSeric 		extern bool shouldqueue();
13857b95031aSeric 
13865f73204aSeric 		if (shouldqueue(e->e_msgpriority))
13877b95031aSeric 			mode = SM_QUEUE;
13887b95031aSeric 		else
13897b95031aSeric 			mode = SendMode;
13907b95031aSeric 	}
13917b95031aSeric 
1392df864a8fSeric 	if (tTd(13, 1))
1393772e6e50Seric 	{
1394276723a8Seric 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
13950c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1396772e6e50Seric 	}
1397ea4dc939Seric 
13980c52a0b3Seric 	/*
1399276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1400588cad61Seric 	**	Check to make sure the hop count is reasonable.
1401588cad61Seric 	**	Delete sends to the sender in mailing lists.
1402276723a8Seric 	*/
1403276723a8Seric 
1404588cad61Seric 	CurEnv = e;
1405276723a8Seric 
14061d57450bSeric 	if (e->e_hopcount > MaxHopCount)
1407276723a8Seric 	{
14088f9146b0Srick 		errno = 0;
14098f9146b0Srick 		syserr("sendall: too many hops %d (%d max): from %s, to %s",
14101d57450bSeric 			e->e_hopcount, MaxHopCount, e->e_from, e->e_to);
1411588cad61Seric 		return;
1412588cad61Seric 	}
1413588cad61Seric 
1414588cad61Seric 	if (!MeToo)
1415276723a8Seric 	{
1416f3d6c55cSeric 		extern ADDRESS *recipient();
1417f3d6c55cSeric 
1418588cad61Seric 		e->e_from.q_flags |= QDONTSEND;
1419f3d6c55cSeric 		(void) recipient(&e->e_from, &e->e_sendqueue);
1420276723a8Seric 	}
1421588cad61Seric 
1422588cad61Seric # ifdef QUEUE
1423b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1424b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1425b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
1426e020b127Seric 		queueup(e, TRUE, mode == SM_QUEUE);
1427276723a8Seric #endif QUEUE
1428276723a8Seric 
1429276723a8Seric 	oldverbose = Verbose;
1430276723a8Seric 	switch (mode)
1431276723a8Seric 	{
1432276723a8Seric 	  case SM_VERIFY:
1433276723a8Seric 		Verbose = TRUE;
1434276723a8Seric 		break;
1435276723a8Seric 
1436276723a8Seric 	  case SM_QUEUE:
1437c7f5410dSeric   queueonly:
1438b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1439276723a8Seric 		return;
1440276723a8Seric 
1441276723a8Seric 	  case SM_FORK:
14429a6a5f55Seric 		if (e->e_xfp != NULL)
14439a6a5f55Seric 			(void) fflush(e->e_xfp);
1444c7f5410dSeric 
1445c7f5410dSeric # ifdef LOCKF
1446c7f5410dSeric 		/*
1447c7f5410dSeric 		**  Since lockf has the interesting semantic that the
14481c265a00Seric 		**  lock is lost when we fork, we have to risk losing
14491c265a00Seric 		**  the lock here by closing before the fork, and then
14501c265a00Seric 		**  trying to get it back in the child.
1451c7f5410dSeric 		*/
1452c7f5410dSeric 
1453e020b127Seric 		if (e->e_lockfp != NULL)
1454c7f5410dSeric 		{
1455e020b127Seric 			(void) fclose(e->e_lockfp);
1456e020b127Seric 			e->e_lockfp = NULL;
1457c7f5410dSeric 		}
1458c7f5410dSeric # endif /* LOCKF */
1459c7f5410dSeric 
1460276723a8Seric 		pid = fork();
1461276723a8Seric 		if (pid < 0)
1462276723a8Seric 		{
1463c7f5410dSeric 			goto queueonly;
1464276723a8Seric 		}
1465276723a8Seric 		else if (pid > 0)
1466a6fce3d8Seric 		{
1467a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1468b254bcb6Seric 			e->e_id = e->e_df = NULL;
1469c7f5410dSeric # ifndef LOCKF
1470e020b127Seric 			if (e->e_lockfp != NULL)
1471e020b127Seric 				(void) fclose(e->e_lockfp);
1472c7f5410dSeric # endif
1473276723a8Seric 			return;
1474a6fce3d8Seric 		}
1475276723a8Seric 
1476276723a8Seric 		/* double fork to avoid zombies */
1477276723a8Seric 		if (fork() > 0)
1478276723a8Seric 			exit(EX_OK);
1479276723a8Seric 
1480a6fce3d8Seric 		/* be sure we are immune from the terminal */
1481769e215aSeric 		disconnect(FALSE);
1482a6fce3d8Seric 
1483e6720d51Seric # ifdef LOCKF
1484e6720d51Seric 		/*
1485c7f5410dSeric 		**  Now try to get our lock back.
1486e6720d51Seric 		*/
1487e6720d51Seric 
14881c265a00Seric 		lfd.l_type = F_WRLCK;
14891c265a00Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1490e020b127Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1491e020b127Seric 		if (e->e_lockfp == NULL ||
14921c265a00Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1493e6720d51Seric 		{
1494e6720d51Seric 			/* oops....  lost it */
1495e6720d51Seric # ifdef LOG
1496e6720d51Seric 			if (LogLevel > 5)
1497c7f5410dSeric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1498e6720d51Seric 					CurEnv->e_id);
1499e6720d51Seric # endif /* LOG */
1500e6720d51Seric 			exit(EX_OK);
1501e6720d51Seric 		}
1502e6720d51Seric # endif /* LOCKF */
1503e6720d51Seric 
1504276723a8Seric 		break;
1505276723a8Seric 	}
1506276723a8Seric 
1507276723a8Seric 	/*
15080c52a0b3Seric 	**  Run through the list and send everything.
15090c52a0b3Seric 	*/
15100c52a0b3Seric 
1511dde5acadSeric 	nsent = 0;
15120c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1513ea4dc939Seric 	{
1514276723a8Seric 		if (mode == SM_VERIFY)
1515ea4dc939Seric 		{
1516a6fce3d8Seric 			e->e_to = q->q_paddr;
1517e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1518ea4dc939Seric 				message(Arpa_Info, "deliverable");
1519ea4dc939Seric 		}
1520dde5acadSeric 		else if (!bitset(QDONTSEND, q->q_flags))
1521dde5acadSeric 		{
1522de1179f5Seric # ifdef QUEUE
1523dde5acadSeric 			/*
1524dde5acadSeric 			**  Checkpoint the send list every few addresses
1525dde5acadSeric 			*/
1526dde5acadSeric 
1527dde5acadSeric 			if (nsent >= CheckpointInterval)
1528dde5acadSeric 			{
1529e020b127Seric 				queueup(e, TRUE, FALSE);
1530dde5acadSeric 				nsent = 0;
1531dde5acadSeric 			}
1532de1179f5Seric # endif /* QUEUE */
1533dde5acadSeric 			if (deliver(e, q) == EX_OK)
1534dde5acadSeric 				nsent++;
1535dde5acadSeric 		}
1536ea4dc939Seric 	}
153714a8ed7aSeric 	Verbose = oldverbose;
15380c52a0b3Seric 
15390c52a0b3Seric 	/*
15400c52a0b3Seric 	**  Now run through and check for errors.
15410c52a0b3Seric 	*/
15420c52a0b3Seric 
1543e020b127Seric 	if (mode == SM_VERIFY)
15440c52a0b3Seric 		return;
15450c52a0b3Seric 
15460c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15470c52a0b3Seric 	{
15480c52a0b3Seric 		register ADDRESS *qq;
15490c52a0b3Seric 
1550df864a8fSeric 		if (tTd(13, 3))
1551df864a8fSeric 		{
1552df864a8fSeric 			printf("Checking ");
1553df864a8fSeric 			printaddr(q, FALSE);
1554df864a8fSeric 		}
1555df864a8fSeric 
1556b254bcb6Seric 		/* only send errors if the message failed */
1557b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1558b254bcb6Seric 			continue;
15590c52a0b3Seric 
15600c52a0b3Seric 		/* we have an address that failed -- find the parent */
15610c52a0b3Seric 		for (qq = q; qq != NULL; qq = qq->q_alias)
15620c52a0b3Seric 		{
15630c52a0b3Seric 			char obuf[MAXNAME + 6];
15640c52a0b3Seric 			extern char *aliaslookup();
15650c52a0b3Seric 
15660c52a0b3Seric 			/* we can only have owners for local addresses */
156757fc6f17Seric 			if (!bitnset(M_LOCAL, qq->q_mailer->m_flags))
15680c52a0b3Seric 				continue;
15690c52a0b3Seric 
15700c52a0b3Seric 			/* see if the owner list exists */
15710c52a0b3Seric 			(void) strcpy(obuf, "owner-");
1572cec031e3Seric 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1573cec031e3Seric 				(void) strcat(obuf, "owner");
1574cec031e3Seric 			else
15750c52a0b3Seric 				(void) strcat(obuf, qq->q_user);
1576ef137175Sbostic 			makelower(obuf);
15770c52a0b3Seric 			if (aliaslookup(obuf) == NULL)
15780c52a0b3Seric 				continue;
15790c52a0b3Seric 
1580df864a8fSeric 			if (tTd(13, 4))
1581df864a8fSeric 				printf("Errors to %s\n", obuf);
1582df864a8fSeric 
15830c52a0b3Seric 			/* owner list exists -- add it to the error queue */
1584e3e4ed86Seric 			sendtolist(obuf, (ADDRESS *) NULL, &e->e_errorqueue);
158553e3fa05Seric 			ErrorMode = EM_MAIL;
15860c52a0b3Seric 			break;
15870c52a0b3Seric 		}
15880c52a0b3Seric 
15890c52a0b3Seric 		/* if we did not find an owner, send to the sender */
15907455aa0bSeric 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1591e3e4ed86Seric 			sendtolist(e->e_from.q_paddr, qq, &e->e_errorqueue);
15920c52a0b3Seric 	}
1593276723a8Seric 
1594276723a8Seric 	if (mode == SM_FORK)
1595276723a8Seric 		finis();
15960c52a0b3Seric }
1597