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*71ff6caaSeric static char sccsid[] = "@(#)deliver.c	6.30 (Berkeley) 02/28/93";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14a8c080f0Seric #include <signal.h>
15c77d1c25Seric #include <sys/stat.h>
16f28da541Smiriam #include <netdb.h>
17fccb3f7aSbostic #include <fcntl.h>
18911693bfSbostic #include <errno.h>
19134746fbSeric #ifdef NAMED_BIND
20912a731aSbostic #include <arpa/nameser.h>
21912a731aSbostic #include <resolv.h>
22134746fbSeric #endif
2325a99e2eSeric 
2425a99e2eSeric /*
2513bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
2613bbc08cSeric **
2713bbc08cSeric **	This routine delivers to everyone on the same host as the
2813bbc08cSeric **	user on the head of the list.  It is clever about mailers
2913bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
3013bbc08cSeric **	that it will deliver to all these addresses however -- so
3113bbc08cSeric **	deliver should be called once for each address on the
3213bbc08cSeric **	list.
3325a99e2eSeric **
3425a99e2eSeric **	Parameters:
35588cad61Seric **		e -- the envelope to deliver.
36c77d1c25Seric **		firstto -- head of the address list to deliver to.
3725a99e2eSeric **
3825a99e2eSeric **	Returns:
3925a99e2eSeric **		zero -- successfully delivered.
4025a99e2eSeric **		else -- some failure, see ExitStat for more info.
4125a99e2eSeric **
4225a99e2eSeric **	Side Effects:
4325a99e2eSeric **		The standard input is passed off to someone.
4425a99e2eSeric */
4525a99e2eSeric 
46588cad61Seric deliver(e, firstto)
47588cad61Seric 	register ENVELOPE *e;
48c77d1c25Seric 	ADDRESS *firstto;
4925a99e2eSeric {
5078442df3Seric 	char *host;			/* host being sent to */
5178442df3Seric 	char *user;			/* user being sent to */
5225a99e2eSeric 	char **pvp;
535dfc646bSeric 	register char **mvp;
5425a99e2eSeric 	register char *p;
55588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
566259796dSeric 	ADDRESS *ctladdr;
57b31e7f2bSeric 	register MCI *mci;
58c77d1c25Seric 	register ADDRESS *to = firstto;
59c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
60772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
61911693bfSbostic 	int rcode;			/* response code */
62c23ed322Seric 	char *from;			/* pointer to from person */
63e103b48fSeric 	char *firstsig;			/* signature of firstto */
64ee6bf8dfSeric 	char *pv[MAXPV+1];
65ee6bf8dfSeric 	char tobuf[MAXLINE-50];		/* text line of to people */
66ee6bf8dfSeric 	char buf[MAXNAME];
67ee6bf8dfSeric 	char tfrombuf[MAXNAME];		/* translated from person */
68c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
69fabb3bd4Seric 	extern int checkcompat();
70ee6bf8dfSeric 	extern ADDRESS *getctladdr();
71ee6bf8dfSeric 	extern char *remotename();
72b31e7f2bSeric 	extern MCI *openmailer();
73e103b48fSeric 	extern char *hostsignature();
7425a99e2eSeric 
7535490626Seric 	errno = 0;
76c6e18ac5Seric 	if (bitset(QDONTSEND|QQUEUEUP, to->q_flags))
775dfc646bSeric 		return (0);
7825a99e2eSeric 
79134746fbSeric #ifdef NAMED_BIND
80912a731aSbostic 	/* unless interactive, try twice, over a minute */
81912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
82912a731aSbostic 		_res.retrans = 30;
83912a731aSbostic 		_res.retry = 2;
84912a731aSbostic 	}
85d4bd8f0eSbostic #endif
86912a731aSbostic 
8751552439Seric 	m = to->q_mailer;
8851552439Seric 	host = to->q_host;
8951552439Seric 
906ef48975Seric 	if (tTd(10, 1))
915dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
9251552439Seric 			m->m_mno, host, to->q_user);
93f3dbc832Seric 
94f3dbc832Seric 	/*
95f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
96f3dbc832Seric 	**  connections now, just mark these addresses and return.
97f3dbc832Seric 	**	This is useful if we want to batch connections to
98f3dbc832Seric 	**	reduce load.  This will cause the messages to be
99f3dbc832Seric 	**	queued up, and a daemon will come along to send the
100f3dbc832Seric 	**	messages later.
101f3dbc832Seric 	**		This should be on a per-mailer basis.
102f3dbc832Seric 	*/
103f3dbc832Seric 
10457fc6f17Seric 	if (NoConnect && !QueueRun && bitnset(M_EXPENSIVE, m->m_flags) &&
105317680d6Seric 	    !Verbose)
106f3dbc832Seric 	{
107f3dbc832Seric 		for (; to != NULL; to = to->q_next)
108f4560e80Seric 		{
109c6e18ac5Seric 			if (bitset(QDONTSEND|QQUEUEUP, to->q_flags) ||
110c6e18ac5Seric 			    to->q_mailer != m)
111f4560e80Seric 				continue;
112f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
113588cad61Seric 			e->e_to = to->q_paddr;
11408b25121Seric 			message("queued");
1152f624c86Seric 			if (LogLevel > 8)
116b31e7f2bSeric 				logdelivery("queued", e);
117f4560e80Seric 		}
118588cad61Seric 		e->e_to = NULL;
119f3dbc832Seric 		return (0);
120f3dbc832Seric 	}
121f3dbc832Seric 
12225a99e2eSeric 	/*
1235dfc646bSeric 	**  Do initial argv setup.
1245dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
1255dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
1265dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
1275dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
1285dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
1293bea8136Seric 	**		The from address rewrite is expected to make
1303bea8136Seric 	**		the address relative to the other end.
1315dfc646bSeric 	*/
1325dfc646bSeric 
13378442df3Seric 	/* rewrite from address, using rewriting rules */
1342f624c86Seric 	(void) strcpy(rpathbuf, remotename(e->e_returnpath, m, TRUE, FALSE,
1352f624c86Seric 					   TRUE, e));
136c23ed322Seric 	if (e->e_returnpath == e->e_sender)
137c23ed322Seric 	{
138c23ed322Seric 		from = rpathbuf;
139c23ed322Seric 	}
140c23ed322Seric 	else
141c23ed322Seric 	{
1422f624c86Seric 		(void) strcpy(tfrombuf, remotename(e->e_sender, m, TRUE, FALSE,
1432f624c86Seric 						   TRUE, e));
144c23ed322Seric 		from = tfrombuf;
145c23ed322Seric 	}
14678442df3Seric 
147c23ed322Seric 	define('f', e->e_returnpath, e);	/* raw return path */
148c23ed322Seric 	define('<', rpathbuf, e);		/* translated return path */
149c23ed322Seric 	define('g', from, e);			/* translated sender */
150588cad61Seric 	define('h', host, e);			/* to host */
1515dfc646bSeric 	Errors = 0;
1525dfc646bSeric 	pvp = pv;
1535dfc646bSeric 	*pvp++ = m->m_argv[0];
1545dfc646bSeric 
1555dfc646bSeric 	/* insert -f or -r flag as appropriate */
15657fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
1575dfc646bSeric 	{
15857fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
1595dfc646bSeric 			*pvp++ = "-f";
1605dfc646bSeric 		else
1615dfc646bSeric 			*pvp++ = "-r";
162c23ed322Seric 		*pvp++ = newstr(rpathbuf);
1635dfc646bSeric 	}
1645dfc646bSeric 
1655dfc646bSeric 	/*
1665dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1675dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1685dfc646bSeric 	**  be one of these, and there are only a few more slots
1695dfc646bSeric 	**  in the pv after it.
1705dfc646bSeric 	*/
1715dfc646bSeric 
1725dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1735dfc646bSeric 	{
1742bc47524Seric 		/* can't use strchr here because of sign extension problems */
1752bc47524Seric 		while (*p != '\0')
1762bc47524Seric 		{
1772bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
1782bc47524Seric 			{
1792bc47524Seric 				if (*p == 'u')
1805dfc646bSeric 					break;
1812bc47524Seric 			}
1822bc47524Seric 		}
1832bc47524Seric 
1842bc47524Seric 		if (*p != '\0')
1855dfc646bSeric 			break;
1865dfc646bSeric 
1875dfc646bSeric 		/* this entry is safe -- go ahead and process it */
188588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1895dfc646bSeric 		*pvp++ = newstr(buf);
1905dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1915dfc646bSeric 		{
19208b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
1935dfc646bSeric 			return (-1);
1945dfc646bSeric 		}
1955dfc646bSeric 	}
196c579ef51Seric 
19733db8731Seric 	/*
19833db8731Seric 	**  If we have no substitution for the user name in the argument
19933db8731Seric 	**  list, we know that we must supply the names otherwise -- and
20033db8731Seric 	**  SMTP is the answer!!
20133db8731Seric 	*/
20233db8731Seric 
2035dfc646bSeric 	if (*mvp == NULL)
204c579ef51Seric 	{
205c579ef51Seric 		/* running SMTP */
2062c7e1b8dSeric # ifdef SMTP
207c579ef51Seric 		clever = TRUE;
208c579ef51Seric 		*pvp = NULL;
2096c2c3107Seric # else /* SMTP */
21033db8731Seric 		/* oops!  we don't implement SMTP */
21108b25121Seric 		syserr("554 SMTP style mailer");
2122c7e1b8dSeric 		return (EX_SOFTWARE);
2136c2c3107Seric # endif /* SMTP */
214c579ef51Seric 	}
2155dfc646bSeric 
2165dfc646bSeric 	/*
2175dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
2185dfc646bSeric 	**  run through our address list and append all the addresses
2195dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
2205dfc646bSeric 	**  always send another copy later.
2215dfc646bSeric 	*/
2225dfc646bSeric 
2235dfc646bSeric 	tobuf[0] = '\0';
224588cad61Seric 	e->e_to = tobuf;
2256259796dSeric 	ctladdr = NULL;
226e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
2275dfc646bSeric 	for (; to != NULL; to = to->q_next)
2285dfc646bSeric 	{
2295dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
23057fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
2315dfc646bSeric 			break;
2325dfc646bSeric 
2335dfc646bSeric 		/* if already sent or not for this host, don't send */
234c6e18ac5Seric 		if (bitset(QDONTSEND|QQUEUEUP, to->q_flags) ||
235e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
236e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
2375dfc646bSeric 			continue;
2386259796dSeric 
2394b22ea87Seric 		/* avoid overflowing tobuf */
240aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
2414b22ea87Seric 			break;
2424b22ea87Seric 
2436ef48975Seric 		if (tTd(10, 1))
244772e6e50Seric 		{
245772e6e50Seric 			printf("\nsend to ");
246772e6e50Seric 			printaddr(to, FALSE);
247772e6e50Seric 		}
248772e6e50Seric 
2496259796dSeric 		/* compute effective uid/gid when sending */
2507da1035fSeric 		if (to->q_mailer == ProgMailer)
2516259796dSeric 			ctladdr = getctladdr(to);
2526259796dSeric 
2535dfc646bSeric 		user = to->q_user;
254588cad61Seric 		e->e_to = to->q_paddr;
2555dfc646bSeric 		to->q_flags |= QDONTSEND;
25675f1ade9Seric 		if (tTd(10, 5))
25775f1ade9Seric 		{
25875f1ade9Seric 			printf("deliver: QDONTSEND ");
25975f1ade9Seric 			printaddr(to, FALSE);
26075f1ade9Seric 		}
2615dfc646bSeric 
2625dfc646bSeric 		/*
2635dfc646bSeric 		**  Check to see that these people are allowed to
2645dfc646bSeric 		**  talk to each other.
2652a6e0786Seric 		*/
2662a6e0786Seric 
26769582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
26869582d2fSeric 		{
26969582d2fSeric 			NoReturn = TRUE;
27008b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
27169582d2fSeric 			giveresponse(EX_UNAVAILABLE, m, e);
27269582d2fSeric 			continue;
27369582d2fSeric 		}
274fabb3bd4Seric 		rcode = checkcompat(to, e);
2751793c9c5Seric 		if (rcode != EX_OK)
2765dfc646bSeric 		{
2771793c9c5Seric 			giveresponse(rcode, m, e);
2785dfc646bSeric 			continue;
2795dfc646bSeric 		}
2802a6e0786Seric 
2812a6e0786Seric 		/*
2829ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2839ec9501bSeric 		**	about them.
28425a99e2eSeric 		*/
28525a99e2eSeric 
28657fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
28725a99e2eSeric 		{
2881d8f1806Seric 			stripquotes(user);
2891d8f1806Seric 			stripquotes(host);
29025a99e2eSeric 		}
29125a99e2eSeric 
292cdb828c5Seric 		/* hack attack -- delivermail compatibility */
293cdb828c5Seric 		if (m == ProgMailer && *user == '|')
294cdb828c5Seric 			user++;
295cdb828c5Seric 
29625a99e2eSeric 		/*
2973efaed6eSeric 		**  If an error message has already been given, don't
2983efaed6eSeric 		**	bother to send to this address.
2993efaed6eSeric 		**
3003efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
3013efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
3023efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
3033efaed6eSeric 		*/
3043efaed6eSeric 
3056cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
3063efaed6eSeric 			continue;
3073efaed6eSeric 
308f2fec898Seric 		/* save statistics.... */
309588cad61Seric 		markstats(e, to);
310f2fec898Seric 
3113efaed6eSeric 		/*
31225a99e2eSeric 		**  See if this user name is "special".
31325a99e2eSeric 		**	If the user name has a slash in it, assume that this
31451552439Seric 		**	is a file -- send it off without further ado.  Note
31551552439Seric 		**	that this type of addresses is not processed along
31651552439Seric 		**	with the others, so we fudge on the To person.
31725a99e2eSeric 		*/
31825a99e2eSeric 
3192c017f8dSeric 		if (m == FileMailer)
32025a99e2eSeric 		{
321b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
322198d9be0Seric 			giveresponse(rcode, m, e);
323dde5acadSeric 			if (rcode == EX_OK)
324dde5acadSeric 				to->q_flags |= QSENT;
3255dfc646bSeric 			continue;
32625a99e2eSeric 		}
32725a99e2eSeric 
32813bbc08cSeric 		/*
32913bbc08cSeric 		**  Address is verified -- add this user to mailer
33013bbc08cSeric 		**  argv, and add it to the print list of recipients.
33113bbc08cSeric 		*/
33213bbc08cSeric 
333508daeccSeric 		/* link together the chain of recipients */
334508daeccSeric 		to->q_tchain = tochain;
335508daeccSeric 		tochain = to;
336508daeccSeric 
3375dfc646bSeric 		/* create list of users for error messages */
338db8841e9Seric 		(void) strcat(tobuf, ",");
339db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
340588cad61Seric 		define('u', user, e);		/* to user */
341588cad61Seric 		define('z', to->q_home, e);	/* user's home */
3425dfc646bSeric 
343c579ef51Seric 		/*
344508daeccSeric 		**  Expand out this user into argument list.
345c579ef51Seric 		*/
346c579ef51Seric 
347508daeccSeric 		if (!clever)
348c579ef51Seric 		{
349588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3505dfc646bSeric 			*pvp++ = newstr(buf);
3515dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3525dfc646bSeric 			{
3535dfc646bSeric 				/* allow some space for trailing parms */
3545dfc646bSeric 				break;
3555dfc646bSeric 			}
3565dfc646bSeric 		}
357c579ef51Seric 	}
3585dfc646bSeric 
359145b49b1Seric 	/* see if any addresses still exist */
360145b49b1Seric 	if (tobuf[0] == '\0')
361c579ef51Seric 	{
362588cad61Seric 		define('g', (char *) NULL, e);
363c23ed322Seric 		define('<', (char *) NULL, e);
364145b49b1Seric 		return (0);
365c579ef51Seric 	}
366145b49b1Seric 
3675dfc646bSeric 	/* print out messages as full list */
36863780dbdSeric 	e->e_to = tobuf + 1;
3695dfc646bSeric 
3705dfc646bSeric 	/*
3715dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3725dfc646bSeric 	*/
3735dfc646bSeric 
374c579ef51Seric 	while (!clever && *++mvp != NULL)
3755dfc646bSeric 	{
376588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
3775dfc646bSeric 		*pvp++ = newstr(buf);
3785dfc646bSeric 		if (pvp >= &pv[MAXPV])
37908b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
3805dfc646bSeric 	}
3815dfc646bSeric 	*pvp++ = NULL;
3825dfc646bSeric 
38325a99e2eSeric 	/*
38425a99e2eSeric 	**  Call the mailer.
3856328bdf7Seric 	**	The argument vector gets built, pipes
38625a99e2eSeric 	**	are created as necessary, and we fork & exec as
3876328bdf7Seric 	**	appropriate.
388c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
38925a99e2eSeric 	*/
39025a99e2eSeric 
39186b26461Seric 	if (ctladdr == NULL)
39286b26461Seric 		ctladdr = &e->e_from;
393134746fbSeric #ifdef NAMED_BIND
3942bcc6d2dSeric 	if (ConfigLevel < 2)
395912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
396134746fbSeric #endif
397b31e7f2bSeric 	mci = openmailer(m, pv, ctladdr, clever, e);
398b31e7f2bSeric 	if (mci == NULL)
399134746fbSeric 	{
400b31e7f2bSeric 		/* catastrophic error */
401845e533cSeric 		rcode = EX_OSERR;
402b31e7f2bSeric 		goto give_up;
403b31e7f2bSeric 	}
404b31e7f2bSeric 	else if (mci->mci_state != MCIS_OPEN)
405b31e7f2bSeric 	{
406b31e7f2bSeric 		/* couldn't open the mailer */
407b31e7f2bSeric 		rcode = mci->mci_exitstat;
4082a6bc25bSeric 		errno = mci->mci_errno;
409b31e7f2bSeric 		if (rcode == EX_OK)
410b31e7f2bSeric 		{
411b31e7f2bSeric 			/* shouldn't happen */
41208b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
4136b0f339dSeric 				rcode, mci->mci_state, firstsig);
414b31e7f2bSeric 			rcode = EX_SOFTWARE;
415b31e7f2bSeric 		}
416b31e7f2bSeric 	}
417b31e7f2bSeric 	else if (!clever)
418b31e7f2bSeric 	{
419b31e7f2bSeric 		/*
420b31e7f2bSeric 		**  Format and send message.
421b31e7f2bSeric 		*/
42215d084d5Seric 
423b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
424b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
425b31e7f2bSeric 		putline("\n", mci->mci_out, m);
426b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
427b31e7f2bSeric 
428b31e7f2bSeric 		/* get the exit status */
429b31e7f2bSeric 		rcode = endmailer(mci, pv[0]);
430134746fbSeric 	}
431134746fbSeric 	else
432b31e7f2bSeric #ifdef SMTP
433134746fbSeric 	{
434b31e7f2bSeric 		/*
435b31e7f2bSeric 		**  Send the MAIL FROM: protocol
436b31e7f2bSeric 		*/
43715d084d5Seric 
438b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
439b31e7f2bSeric 		if (rcode == EX_OK)
44075889e88Seric 		{
441ded0d3daSkarels 			register char *t = tobuf;
442ded0d3daSkarels 			register int i;
443ded0d3daSkarels 
444588cad61Seric 			/* send the recipient list */
44563780dbdSeric 			tobuf[0] = '\0';
44675889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
44775889e88Seric 			{
44863780dbdSeric 				e->e_to = to->q_paddr;
44915d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
45075889e88Seric 				{
45183b7ddc9Seric 					markfailure(e, to, i);
452198d9be0Seric 					giveresponse(i, m, e);
45363780dbdSeric 				}
45475889e88Seric 				else
45575889e88Seric 				{
456911693bfSbostic 					*t++ = ',';
457b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
458b31e7f2bSeric 						continue;
459588cad61Seric 				}
460588cad61Seric 			}
461588cad61Seric 
46263780dbdSeric 			/* now send the data */
46363780dbdSeric 			if (tobuf[0] == '\0')
464b31e7f2bSeric 			{
46563780dbdSeric 				e->e_to = NULL;
466b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
467b31e7f2bSeric 					smtprset(m, mci, e);
468b31e7f2bSeric 			}
46975889e88Seric 			else
47075889e88Seric 			{
47163780dbdSeric 				e->e_to = tobuf + 1;
47275889e88Seric 				rcode = smtpdata(m, mci, e);
47363780dbdSeric 			}
47463780dbdSeric 
47563780dbdSeric 			/* now close the connection */
476b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
47715d084d5Seric 				smtpquit(m, mci, e);
47863780dbdSeric 		}
479c579ef51Seric 	}
480b31e7f2bSeric #else /* not SMTP */
481a05b3449Sbostic 	{
48208b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
483845e533cSeric 		rcode = EX_CONFIG;
484b31e7f2bSeric 		goto give_up;
485a05b3449Sbostic 	}
486b31e7f2bSeric #endif /* SMTP */
487134746fbSeric #ifdef NAMED_BIND
4882bcc6d2dSeric 	if (ConfigLevel < 2)
489912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
490134746fbSeric #endif
4915dfc646bSeric 
492b31e7f2bSeric 	/* arrange a return receipt if requested */
4938c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
494b31e7f2bSeric 	{
495b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
496b31e7f2bSeric 		/* do we want to send back more info? */
497b31e7f2bSeric 	}
498b31e7f2bSeric 
499c77d1c25Seric 	/*
50063780dbdSeric 	**  Do final status disposal.
50163780dbdSeric 	**	We check for something in tobuf for the SMTP case.
502c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
503c77d1c25Seric 	**		addressees.
504c77d1c25Seric 	*/
505c77d1c25Seric 
506b31e7f2bSeric   give_up:
50763780dbdSeric 	if (tobuf[0] != '\0')
508198d9be0Seric 		giveresponse(rcode, m, e);
509772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
510b31e7f2bSeric 	{
511dde5acadSeric 		if (rcode != EX_OK)
51283b7ddc9Seric 			markfailure(e, to, rcode);
513dde5acadSeric 		else
514655518ecSeric 		{
515dde5acadSeric 			to->q_flags |= QSENT;
516655518ecSeric 			e->e_nsent++;
517655518ecSeric 		}
518b31e7f2bSeric 	}
519b31e7f2bSeric 
520b31e7f2bSeric 	/*
521b31e7f2bSeric 	**  Restore state and return.
522b31e7f2bSeric 	*/
523c77d1c25Seric 
52435490626Seric 	errno = 0;
525588cad61Seric 	define('g', (char *) NULL, e);
526c23ed322Seric 	define('<', (char *) NULL, e);
5275826d9d3Seric 	return (rcode);
52825a99e2eSeric }
5295dfc646bSeric /*
53083b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
53183b7ddc9Seric **
53283b7ddc9Seric **	Parameters:
53383b7ddc9Seric **		e -- the envelope we are sending.
53483b7ddc9Seric **		q -- the address to mark.
53583b7ddc9Seric **		rcode -- the code signifying the particular failure.
53683b7ddc9Seric **
53783b7ddc9Seric **	Returns:
53883b7ddc9Seric **		none.
53983b7ddc9Seric **
54083b7ddc9Seric **	Side Effects:
54183b7ddc9Seric **		marks the address (and possibly the envelope) with the
54283b7ddc9Seric **			failure so that an error will be returned or
54383b7ddc9Seric **			the message will be queued, as appropriate.
54483b7ddc9Seric */
54583b7ddc9Seric 
54683b7ddc9Seric markfailure(e, q, rcode)
54783b7ddc9Seric 	register ENVELOPE *e;
54883b7ddc9Seric 	register ADDRESS *q;
54983b7ddc9Seric 	int rcode;
55083b7ddc9Seric {
55183b7ddc9Seric 	if (rcode == EX_OK)
55283b7ddc9Seric 		return;
553ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
55483b7ddc9Seric 		q->q_flags |= QBADADDR;
55583b7ddc9Seric 	else if (curtime() > e->e_ctime + TimeOut)
55683b7ddc9Seric 	{
55783b7ddc9Seric 		extern char *pintvl();
558198d9be0Seric 		char buf[MAXLINE];
55983b7ddc9Seric 
56083b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
561198d9be0Seric 		{
562198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
56383b7ddc9Seric 				pintvl(TimeOut, FALSE));
564198d9be0Seric 			if (e->e_message != NULL)
565198d9be0Seric 				free(e->e_message);
566198d9be0Seric 			e->e_message = newstr(buf);
56708b25121Seric 			message(buf);
568198d9be0Seric 		}
56983b7ddc9Seric 		q->q_flags |= QBADADDR;
57083b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
571c13b5dfcSeric 		fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr);
57283b7ddc9Seric 	}
57383b7ddc9Seric 	else
57483b7ddc9Seric 		q->q_flags |= QQUEUEUP;
57583b7ddc9Seric }
57683b7ddc9Seric /*
57732d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
57832d19d43Seric **
57932d19d43Seric **	This MUST be a macro, since after a vfork we are running
58032d19d43Seric **	two processes on the same stack!!!
58132d19d43Seric **
58232d19d43Seric **	Parameters:
58332d19d43Seric **		none.
58432d19d43Seric **
58532d19d43Seric **	Returns:
58632d19d43Seric **		From a macro???  You've got to be kidding!
58732d19d43Seric **
58832d19d43Seric **	Side Effects:
58932d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
59032d19d43Seric **			pid of child in parent, zero in child.
59132d19d43Seric **			-1 on unrecoverable error.
59232d19d43Seric **
59332d19d43Seric **	Notes:
59432d19d43Seric **		I'm awfully sorry this looks so awful.  That's
59532d19d43Seric **		vfork for you.....
59632d19d43Seric */
59732d19d43Seric 
59832d19d43Seric # define NFORKTRIES	5
59984f7cd1bSeric 
60084f7cd1bSeric # ifndef FORK
60184f7cd1bSeric # define FORK	fork
60284f7cd1bSeric # endif
60332d19d43Seric 
60432d19d43Seric # define DOFORK(fORKfN) \
60532d19d43Seric {\
60632d19d43Seric 	register int i;\
60732d19d43Seric \
60811799049Seric 	for (i = NFORKTRIES; --i >= 0; )\
60932d19d43Seric 	{\
61032d19d43Seric 		pid = fORKfN();\
61132d19d43Seric 		if (pid >= 0)\
61232d19d43Seric 			break;\
61311799049Seric 		if (i > 0)\
6146c4635f6Seric 			sleep((unsigned) NFORKTRIES - i);\
61532d19d43Seric 	}\
61632d19d43Seric }
61732d19d43Seric /*
6182ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
6192ed72599Seric **
6202ed72599Seric **	Parameters:
6212ed72599Seric **		none.
6222ed72599Seric **
6232ed72599Seric **	Returns:
6242ed72599Seric **		pid of child in parent.
6252ed72599Seric **		zero in child.
6262ed72599Seric **		-1 on error.
6272ed72599Seric **
6282ed72599Seric **	Side Effects:
6292ed72599Seric **		returns twice, once in parent and once in child.
6302ed72599Seric */
6312ed72599Seric 
6322ed72599Seric dofork()
6332ed72599Seric {
6342ed72599Seric 	register int pid;
6352ed72599Seric 
6362ed72599Seric 	DOFORK(fork);
6372ed72599Seric 	return (pid);
6382ed72599Seric }
6392ed72599Seric /*
640c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
641c579ef51Seric **
642c579ef51Seric **	We should never get fatal errors (e.g., segmentation
643c579ef51Seric **	violation), so we report those specially.  For other
644c579ef51Seric **	errors, we choose a status message (into statmsg),
645c579ef51Seric **	and if it represents an error, we print it.
646c579ef51Seric **
647c579ef51Seric **	Parameters:
648c579ef51Seric **		pid -- pid of mailer.
649c579ef51Seric **		name -- name of mailer (for error messages).
650c579ef51Seric **
651c579ef51Seric **	Returns:
652c579ef51Seric **		exit code of mailer.
653c579ef51Seric **
654c579ef51Seric **	Side Effects:
655c579ef51Seric **		none.
656c579ef51Seric */
657c579ef51Seric 
65875889e88Seric endmailer(mci, name)
659b31e7f2bSeric 	register MCI *mci;
660c579ef51Seric 	char *name;
661c579ef51Seric {
662588cad61Seric 	int st;
663c579ef51Seric 
66475889e88Seric 	/* close any connections */
66575889e88Seric 	if (mci->mci_in != NULL)
66675889e88Seric 		(void) fclose(mci->mci_in);
66775889e88Seric 	if (mci->mci_out != NULL)
66875889e88Seric 		(void) fclose(mci->mci_out);
66975889e88Seric 	mci->mci_in = mci->mci_out = NULL;
67075889e88Seric 	mci->mci_state = MCIS_CLOSED;
67175889e88Seric 
67233db8731Seric 	/* in the IPC case there is nothing to wait for */
67375889e88Seric 	if (mci->mci_pid == 0)
67433db8731Seric 		return (EX_OK);
67533db8731Seric 
67633db8731Seric 	/* wait for the mailer process to die and collect status */
67775889e88Seric 	st = waitfor(mci->mci_pid);
678588cad61Seric 	if (st == -1)
67978de67c1Seric 	{
680588cad61Seric 		syserr("endmailer %s: wait", name);
681588cad61Seric 		return (EX_SOFTWARE);
682c579ef51Seric 	}
68333db8731Seric 
68433db8731Seric 	/* see if it died a horrid death */
685c579ef51Seric 	if ((st & 0377) != 0)
686c579ef51Seric 	{
6875f73204aSeric 		syserr("mailer %s died with signal %o", name, st);
6885f73204aSeric 		ExitStat = EX_TEMPFAIL;
6895f73204aSeric 		return (EX_TEMPFAIL);
690c579ef51Seric 	}
69133db8731Seric 
69233db8731Seric 	/* normal death -- return status */
693588cad61Seric 	st = (st >> 8) & 0377;
694588cad61Seric 	return (st);
695c579ef51Seric }
696c579ef51Seric /*
697c579ef51Seric **  OPENMAILER -- open connection to mailer.
698c579ef51Seric **
699c579ef51Seric **	Parameters:
700c579ef51Seric **		m -- mailer descriptor.
701c579ef51Seric **		pvp -- parameter vector to pass to mailer.
702c579ef51Seric **		ctladdr -- controlling address for user.
703c579ef51Seric **		clever -- create a full duplex connection.
704c579ef51Seric **
705c579ef51Seric **	Returns:
70675889e88Seric **		The mail connection info struct for this connection.
70775889e88Seric **		NULL on failure.
708c579ef51Seric **
709c579ef51Seric **	Side Effects:
710c579ef51Seric **		creates a mailer in a subprocess.
711c579ef51Seric */
712c579ef51Seric 
713b31e7f2bSeric MCI *
714b31e7f2bSeric openmailer(m, pvp, ctladdr, clever, e)
715588cad61Seric 	MAILER *m;
716c579ef51Seric 	char **pvp;
717c579ef51Seric 	ADDRESS *ctladdr;
718c579ef51Seric 	bool clever;
719b31e7f2bSeric 	ENVELOPE *e;
720c579ef51Seric {
7215dfc646bSeric 	int pid;
722b31e7f2bSeric 	register MCI *mci;
723f8952a83Seric 	int mpvect[2];
724c579ef51Seric 	int rpvect[2];
7255dfc646bSeric 	extern FILE *fdopen();
7265dfc646bSeric 
7276ef48975Seric 	if (tTd(11, 1))
7285dfc646bSeric 	{
7298c57e552Seric 		printf("openmailer:");
7305dfc646bSeric 		printav(pvp);
7315dfc646bSeric 	}
73235490626Seric 	errno = 0;
7335dfc646bSeric 
734ef66a9d0Seric 	CurHostName = m->m_mailer;
735ef66a9d0Seric 
73633db8731Seric 	/*
73733db8731Seric 	**  Deal with the special case of mail handled through an IPC
73833db8731Seric 	**  connection.
73933db8731Seric 	**	In this case we don't actually fork.  We must be
74033db8731Seric 	**	running SMTP for this to work.  We will return a
74133db8731Seric 	**	zero pid to indicate that we are running IPC.
742e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
74333db8731Seric 	*/
74433db8731Seric 
745e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
746e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
747e7c1bd78Seric 	{
748b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
7492a087e90Seric 		bzero((char *) mci, sizeof *mci);
75075889e88Seric 		mci->mci_in = stdin;
75175889e88Seric 		mci->mci_out = stdout;
752b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
75315d084d5Seric 		mci->mci_mailer = m;
754e7c1bd78Seric 	}
755b31e7f2bSeric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
756914346b1Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
75733db8731Seric 	{
75884f7cd1bSeric #ifdef DAEMON
759e103b48fSeric 		register int i;
7601277f9a8Seric 		register u_short port;
761e103b48fSeric 		char *curhost;
762b31e7f2bSeric 		extern MCI *mci_get();
763e103b48fSeric 		extern char *hostsignature();
76433db8731Seric 
765ef66a9d0Seric 		CurHostName = pvp[1];
766e103b48fSeric 		curhost = hostsignature(m, pvp[1], e);
767b31e7f2bSeric 
76833db8731Seric 		if (!clever)
76908b25121Seric 			syserr("554 non-clever IPC");
77093b6e3cfSeric 		if (pvp[2] != NULL)
7711277f9a8Seric 			port = atoi(pvp[2]);
77293b6e3cfSeric 		else
7731277f9a8Seric 			port = 0;
774e103b48fSeric 		while (*curhost != '\0')
775ebc61751Sbloom 		{
776e103b48fSeric 			register char *p;
777e103b48fSeric 			char hostbuf[MAXNAME];
778e103b48fSeric 
779e103b48fSeric 			/* pull the next host from the signature */
780e103b48fSeric 			p = strchr(curhost, ':');
781e103b48fSeric 			if (p == NULL)
782e103b48fSeric 				p = &curhost[strlen(curhost)];
783e103b48fSeric 			strncpy(hostbuf, curhost, p - curhost);
784e103b48fSeric 			hostbuf[p - curhost] = '\0';
785e103b48fSeric 			if (*p != '\0')
786e103b48fSeric 				p++;
787e103b48fSeric 			curhost = p;
788e103b48fSeric 
78945a8316eSeric 			/* see if we already know that this host is fried */
790e103b48fSeric 			CurHostName = hostbuf;
791e103b48fSeric 			mci = mci_get(hostbuf, m);
792b31e7f2bSeric 			if (mci->mci_state != MCIS_CLOSED)
7931fa7c2ebSeric 			{
7941fa7c2ebSeric 				if (tTd(11, 1))
7951fa7c2ebSeric 				{
7961fa7c2ebSeric 					printf("openmailer: ");
7971fa7c2ebSeric 					mci_dump(mci);
7981fa7c2ebSeric 				}
79967088a9dSeric 				CurHostName = mci->mci_host;
80075889e88Seric 				return mci;
8011fa7c2ebSeric 			}
80215d084d5Seric 			mci->mci_mailer = m;
803b31e7f2bSeric 			if (mci->mci_exitstat != EX_OK)
804b31e7f2bSeric 				continue;
805b31e7f2bSeric 
80675889e88Seric 			/* try the connection */
807e103b48fSeric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
80808b25121Seric 			message("Connecting to %s (%s)...",
809e103b48fSeric 				hostbuf, m->m_name);
810e103b48fSeric 			i = makeconnection(hostbuf, port, mci,
811914346b1Seric 				bitnset(M_SECURE_PORT, m->m_flags));
81275889e88Seric 			mci->mci_exitstat = i;
81375889e88Seric 			mci->mci_errno = errno;
81475889e88Seric 			if (i == EX_OK)
815b31e7f2bSeric 			{
816b31e7f2bSeric 				mci->mci_state = MCIS_OPENING;
817b31e7f2bSeric 				mci_cache(mci);
818b31e7f2bSeric 				break;
819b31e7f2bSeric 			}
820b31e7f2bSeric 			else if (tTd(11, 1))
821b31e7f2bSeric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
822b31e7f2bSeric 					i, errno);
823b31e7f2bSeric 
82475889e88Seric 
8255f73204aSeric 			/* enter status of this host */
82675889e88Seric 			setstat(i);
827ed854c7bSeric 		}
8282a087e90Seric 		mci->mci_pid = 0;
829b31e7f2bSeric #else /* no DAEMON */
83008b25121Seric 		syserr("554 openmailer: no IPC");
8311fa7c2ebSeric 		if (tTd(11, 1))
8321fa7c2ebSeric 			printf("openmailer: NULL\n");
83375889e88Seric 		return NULL;
834b31e7f2bSeric #endif /* DAEMON */
835588cad61Seric 	}
836b31e7f2bSeric 	else
837b31e7f2bSeric 	{
8386328bdf7Seric 		/* create a pipe to shove the mail through */
839f8952a83Seric 		if (pipe(mpvect) < 0)
84025a99e2eSeric 		{
841588cad61Seric 			syserr("openmailer: pipe (to mailer)");
8421fa7c2ebSeric 			if (tTd(11, 1))
8431fa7c2ebSeric 				printf("openmailer: NULL\n");
84475889e88Seric 			return NULL;
84525a99e2eSeric 		}
846c579ef51Seric 
847c579ef51Seric 		/* if this mailer speaks smtp, create a return pipe */
848c579ef51Seric 		if (clever && pipe(rpvect) < 0)
849c579ef51Seric 		{
850588cad61Seric 			syserr("openmailer: pipe (from mailer)");
851c579ef51Seric 			(void) close(mpvect[0]);
852c579ef51Seric 			(void) close(mpvect[1]);
8531fa7c2ebSeric 			if (tTd(11, 1))
8541fa7c2ebSeric 				printf("openmailer: NULL\n");
85575889e88Seric 			return NULL;
856c579ef51Seric 		}
857c579ef51Seric 
85833db8731Seric 		/*
85933db8731Seric 		**  Actually fork the mailer process.
86033db8731Seric 		**	DOFORK is clever about retrying.
8616984bfddSeric 		**
8626984bfddSeric 		**	Dispose of SIGCHLD signal catchers that may be laying
8636984bfddSeric 		**	around so that endmail will get it.
86433db8731Seric 		*/
86533db8731Seric 
866b31e7f2bSeric 		if (e->e_xfp != NULL)
867b31e7f2bSeric 			(void) fflush(e->e_xfp);		/* for debugging */
868588cad61Seric 		(void) fflush(stdout);
8696984bfddSeric # ifdef SIGCHLD
8706984bfddSeric 		(void) signal(SIGCHLD, SIG_DFL);
8716c2c3107Seric # endif /* SIGCHLD */
87284f7cd1bSeric 		DOFORK(FORK);
873f129ec7dSeric 		/* pid is set by DOFORK */
87425a99e2eSeric 		if (pid < 0)
87525a99e2eSeric 		{
87633db8731Seric 			/* failure */
877588cad61Seric 			syserr("openmailer: cannot fork");
878f8952a83Seric 			(void) close(mpvect[0]);
879f8952a83Seric 			(void) close(mpvect[1]);
880c579ef51Seric 			if (clever)
881c579ef51Seric 			{
882c579ef51Seric 				(void) close(rpvect[0]);
883c579ef51Seric 				(void) close(rpvect[1]);
884c579ef51Seric 			}
8851fa7c2ebSeric 			if (tTd(11, 1))
8861fa7c2ebSeric 				printf("openmailer: NULL\n");
88775889e88Seric 			return NULL;
88825a99e2eSeric 		}
88925a99e2eSeric 		else if (pid == 0)
89025a99e2eSeric 		{
89113088b9fSeric 			int i;
892dafbc479Seric 			int saveerrno;
893d7a0480eSeric 			char *env[2];
8945f73204aSeric 			extern int DtableSize;
89513088b9fSeric 
89625a99e2eSeric 			/* child -- set up input & exec mailer */
89703ab8e55Seric 			/* make diagnostic output be standard output */
8988f0e7860Seric 			(void) signal(SIGINT, SIG_IGN);
8998f0e7860Seric 			(void) signal(SIGHUP, SIG_IGN);
9000984da9fSeric 			(void) signal(SIGTERM, SIG_DFL);
901f8952a83Seric 
9021caa9a59Seric 			/* close any other cached connections */
9031caa9a59Seric 			mci_flush(FALSE, mci);
9041caa9a59Seric 
905b31e7f2bSeric 			/* arrange to filter std & diag output of command */
906c579ef51Seric 			if (clever)
907c579ef51Seric 			{
908c579ef51Seric 				(void) close(rpvect[0]);
909c579ef51Seric 				(void) close(1);
910c579ef51Seric 				(void) dup(rpvect[1]);
911c579ef51Seric 				(void) close(rpvect[1]);
912c579ef51Seric 			}
913276723a8Seric 			else if (OpMode == MD_SMTP || HoldErrs)
914f8952a83Seric 			{
915588cad61Seric 				/* put mailer output in transcript */
916f8952a83Seric 				(void) close(1);
917b31e7f2bSeric 				(void) dup(fileno(e->e_xfp));
918f8952a83Seric 			}
919db8841e9Seric 			(void) close(2);
920db8841e9Seric 			(void) dup(1);
921f8952a83Seric 
922f8952a83Seric 			/* arrange to get standard input */
923f8952a83Seric 			(void) close(mpvect[1]);
924db8841e9Seric 			(void) close(0);
925f8952a83Seric 			if (dup(mpvect[0]) < 0)
92625a99e2eSeric 			{
92725a99e2eSeric 				syserr("Cannot dup to zero!");
928a590b978Seric 				_exit(EX_OSERR);
92925a99e2eSeric 			}
930f8952a83Seric 			(void) close(mpvect[0]);
93157fc6f17Seric 			if (!bitnset(M_RESTR, m->m_flags))
9320984da9fSeric 			{
93353e3fa05Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
934e36b99e2Seric 				{
935e36b99e2Seric 					(void) setgid(DefGid);
936898a126bSbostic 					(void) initgroups(DefUser, DefGid);
937e36b99e2Seric 					(void) setuid(DefUid);
938e36b99e2Seric 				}
939e36b99e2Seric 				else
94069f29479Seric 				{
941e36b99e2Seric 					(void) setgid(ctladdr->q_gid);
942898a126bSbostic 					(void) initgroups(ctladdr->q_ruser?
943898a126bSbostic 						ctladdr->q_ruser: ctladdr->q_user,
944898a126bSbostic 						ctladdr->q_gid);
945e36b99e2Seric 					(void) setuid(ctladdr->q_uid);
94669f29479Seric 				}
9470984da9fSeric 			}
948588cad61Seric 
94913088b9fSeric 			/* arrange for all the files to be closed */
950b31e7f2bSeric 			for (i = 3; i < DtableSize; i++)
951b31e7f2bSeric 			{
952fccb3f7aSbostic 				register int j;
953fccb3f7aSbostic 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
954fccb3f7aSbostic 					(void)fcntl(i, F_SETFD, j|1);
955fccb3f7aSbostic 			}
95633db8731Seric 
95733db8731Seric 			/* try to execute the mailer */
958d7a0480eSeric 			env[0] = "AGENT=sendmail";
959d7a0480eSeric 			env[1] = NULL;
960d7a0480eSeric 			execve(m->m_mailer, pvp, env);
961dafbc479Seric 			saveerrno = errno;
96213088b9fSeric 			syserr("Cannot exec %s", m->m_mailer);
96395b76e4bSeric 			if (m == LocalMailer)
96455f33c03Seric 				_exit(EX_TEMPFAIL);
965dafbc479Seric 			switch (saveerrno)
96695b76e4bSeric 			{
96795b76e4bSeric 			  case EIO:
96895b76e4bSeric 			  case EAGAIN:
96995b76e4bSeric 			  case ENOMEM:
97095b76e4bSeric # ifdef EPROCLIM
97195b76e4bSeric 			  case EPROCLIM:
97295b76e4bSeric # endif
9731ab55cd7Seric # ifdef ETIMEDOUT
9741ab55cd7Seric 			  case ETIMEDOUT:
9751ab55cd7Seric # endif
97695b76e4bSeric 				_exit(EX_TEMPFAIL);
97795b76e4bSeric 			}
978a590b978Seric 			_exit(EX_UNAVAILABLE);
97925a99e2eSeric 		}
98025a99e2eSeric 
981f8952a83Seric 		/*
982c579ef51Seric 		**  Set up return value.
983f8952a83Seric 		*/
984f8952a83Seric 
985b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
9862a087e90Seric 		bzero((char *) mci, sizeof *mci);
98715d084d5Seric 		mci->mci_mailer = m;
988b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9892a087e90Seric 		mci->mci_pid = pid;
990f8952a83Seric 		(void) close(mpvect[0]);
99175889e88Seric 		mci->mci_out = fdopen(mpvect[1], "w");
992c579ef51Seric 		if (clever)
99325a99e2eSeric 		{
994c579ef51Seric 			(void) close(rpvect[1]);
99575889e88Seric 			mci->mci_in = fdopen(rpvect[0], "r");
99675889e88Seric 		}
99775889e88Seric 		else
99875889e88Seric 		{
99975889e88Seric 			mci->mci_flags |= MCIF_TEMP;
100075889e88Seric 			mci->mci_in = NULL;
100175889e88Seric 		}
1002b31e7f2bSeric 	}
1003b31e7f2bSeric 
1004b31e7f2bSeric 	/*
1005b31e7f2bSeric 	**  If we are in SMTP opening state, send initial protocol.
1006b31e7f2bSeric 	*/
1007b31e7f2bSeric 
1008b31e7f2bSeric 	if (clever && mci->mci_state != MCIS_CLOSED)
1009b31e7f2bSeric 	{
1010b31e7f2bSeric 		smtpinit(m, mci, e);
1011b31e7f2bSeric 	}
10121fa7c2ebSeric 	if (tTd(11, 1))
10131fa7c2ebSeric 	{
10141fa7c2ebSeric 		printf("openmailer: ");
10151fa7c2ebSeric 		mci_dump(mci);
10161fa7c2ebSeric 	}
1017c579ef51Seric 
101875889e88Seric 	return mci;
101925a99e2eSeric }
102025a99e2eSeric /*
102125a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
102225a99e2eSeric **
102325a99e2eSeric **	Parameters:
102425a99e2eSeric **		stat -- the status code from the mailer (high byte
102525a99e2eSeric **			only; core dumps must have been taken care of
102625a99e2eSeric **			already).
102725a99e2eSeric **		m -- the mailer descriptor for this mailer.
102825a99e2eSeric **
102925a99e2eSeric **	Returns:
1030db8841e9Seric **		none.
103125a99e2eSeric **
103225a99e2eSeric **	Side Effects:
1033c1f9df2cSeric **		Errors may be incremented.
103425a99e2eSeric **		ExitStat may be set.
103525a99e2eSeric */
103625a99e2eSeric 
1037198d9be0Seric giveresponse(stat, m, e)
103825a99e2eSeric 	int stat;
1039588cad61Seric 	register MAILER *m;
1040198d9be0Seric 	ENVELOPE *e;
104125a99e2eSeric {
104225a99e2eSeric 	register char *statmsg;
104325a99e2eSeric 	extern char *SysExMsg[];
104425a99e2eSeric 	register int i;
1045d4bd8f0eSbostic 	extern int N_SysEx;
1046d4bd8f0eSbostic #ifdef NAMED_BIND
1047d4bd8f0eSbostic 	extern int h_errno;
1048d4bd8f0eSbostic #endif
1049198d9be0Seric 	char buf[MAXLINE];
105025a99e2eSeric 
10517d1fc79dSeric #ifdef lint
10527d1fc79dSeric 	if (m == NULL)
10537d1fc79dSeric 		return;
10547d1fc79dSeric #endif lint
10557d1fc79dSeric 
105613bbc08cSeric 	/*
105713bbc08cSeric 	**  Compute status message from code.
105813bbc08cSeric 	*/
105913bbc08cSeric 
106025a99e2eSeric 	i = stat - EX__BASE;
1061588cad61Seric 	if (stat == 0)
1062588cad61Seric 		statmsg = "250 Sent";
1063588cad61Seric 	else if (i < 0 || i > N_SysEx)
1064588cad61Seric 	{
1065588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1066588cad61Seric 		stat = EX_UNAVAILABLE;
1067588cad61Seric 		statmsg = buf;
1068588cad61Seric 	}
1069198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1070198d9be0Seric 	{
10718557d168Seric 		(void) strcpy(buf, SysExMsg[i]);
1072d4bd8f0eSbostic #ifdef NAMED_BIND
1073f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1074f28da541Smiriam 		{
1075f28da541Smiriam 			extern char *errstring();
1076f28da541Smiriam 
1077f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1078f28da541Smiriam 		}
1079f28da541Smiriam 		else
1080d4bd8f0eSbostic #endif
1081f28da541Smiriam 		{
10828557d168Seric 			if (errno != 0)
1083198d9be0Seric 			{
108487c9b3e7Seric 				extern char *errstring();
10858557d168Seric 
1086d87e85f3Seric 				statmsg = errstring(errno);
1087d87e85f3Seric 			}
1088d87e85f3Seric 			else
1089d87e85f3Seric 			{
1090d87e85f3Seric #ifdef SMTP
1091d87e85f3Seric 				extern char SmtpError[];
1092d87e85f3Seric 
1093d87e85f3Seric 				statmsg = SmtpError;
10946c2c3107Seric #else /* SMTP */
1095d87e85f3Seric 				statmsg = NULL;
10966c2c3107Seric #endif /* SMTP */
1097d87e85f3Seric 			}
1098f28da541Smiriam 		}
1099d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1100d87e85f3Seric 		{
110187c9b3e7Seric 			(void) strcat(buf, ": ");
1102d87e85f3Seric 			(void) strcat(buf, statmsg);
11038557d168Seric 		}
1104198d9be0Seric 		statmsg = buf;
1105198d9be0Seric 	}
110625a99e2eSeric 	else
1107d87e85f3Seric 	{
110825a99e2eSeric 		statmsg = SysExMsg[i];
1109d87e85f3Seric 	}
1110588cad61Seric 
1111588cad61Seric 	/*
1112588cad61Seric 	**  Print the message as appropriate
1113588cad61Seric 	*/
1114588cad61Seric 
1115198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
111608b25121Seric 		message(&statmsg[4]);
111725a99e2eSeric 	else
111825a99e2eSeric 	{
1119c1f9df2cSeric 		Errors++;
11205826d9d3Seric 		usrerr(statmsg);
112125a99e2eSeric 	}
112225a99e2eSeric 
112325a99e2eSeric 	/*
112425a99e2eSeric 	**  Final cleanup.
112525a99e2eSeric 	**	Log a record of the transaction.  Compute the new
112625a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
112725a99e2eSeric 	**	that.
112825a99e2eSeric 	*/
112925a99e2eSeric 
11302f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
1131b31e7f2bSeric 		logdelivery(&statmsg[4], e);
1132eb238f8cSeric 
1133eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1134eb238f8cSeric 		setstat(stat);
1135198d9be0Seric 	if (stat != EX_OK)
1136198d9be0Seric 	{
1137198d9be0Seric 		if (e->e_message != NULL)
1138198d9be0Seric 			free(e->e_message);
1139198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1140198d9be0Seric 	}
11418557d168Seric 	errno = 0;
1142d4bd8f0eSbostic #ifdef NAMED_BIND
1143f28da541Smiriam 	h_errno = 0;
1144d4bd8f0eSbostic #endif
1145eb238f8cSeric }
1146eb238f8cSeric /*
1147eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1148eb238f8cSeric **
1149eb238f8cSeric **	Parameters:
1150eb238f8cSeric **		stat -- the message to print for the status
1151eb238f8cSeric **
1152eb238f8cSeric **	Returns:
1153eb238f8cSeric **		none
1154eb238f8cSeric **
1155eb238f8cSeric **	Side Effects:
1156eb238f8cSeric **		none
1157eb238f8cSeric */
1158eb238f8cSeric 
1159b31e7f2bSeric logdelivery(stat, e)
1160eb238f8cSeric 	char *stat;
1161b31e7f2bSeric 	register ENVELOPE *e;
11625cf56be3Seric {
1163*71ff6caaSeric 	char *delay;
11645cf56be3Seric 	extern char *pintvl();
11655cf56be3Seric 
1166eb238f8cSeric # ifdef LOG
1167*71ff6caaSeric 	delay = pintvl(curtime() - e->e_ctime, TRUE);
1168*71ff6caaSeric 	if (strcmp(stat, "Sent") != 0 || CurHostName == NULL)
1169*71ff6caaSeric 	{
1170*71ff6caaSeric 		syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s",
1171*71ff6caaSeric 		       e->e_id, e->e_to, delay, stat);
1172*71ff6caaSeric 	}
1173*71ff6caaSeric 	else
1174*71ff6caaSeric 	{
1175*71ff6caaSeric 		char *p;
1176*71ff6caaSeric 		extern char *macvalue();
1177*71ff6caaSeric 
1178*71ff6caaSeric 		if (CurHostName[0] == '/')
1179*71ff6caaSeric 		{
1180*71ff6caaSeric 			p = macvalue('h', e);
1181*71ff6caaSeric 			if (p == NULL || p[0] == '\0')
1182*71ff6caaSeric 				p = "local";
1183*71ff6caaSeric 		}
1184*71ff6caaSeric # ifdef DAEMON
1185*71ff6caaSeric 		else
1186*71ff6caaSeric 		{
1187*71ff6caaSeric 			extern struct sockaddr_in CurHostAddr;
1188*71ff6caaSeric 			extern char *inet_ntoa();
1189*71ff6caaSeric 
1190*71ff6caaSeric 			p = inet_ntoa(CurHostAddr.sin_addr);
1191*71ff6caaSeric 		}
1192*71ff6caaSeric # endif
1193*71ff6caaSeric 		syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=Sent to %s (%s)",
1194*71ff6caaSeric 		       e->e_id, e->e_to, delay, p, CurHostName);
1195*71ff6caaSeric 	}
11966c2c3107Seric # endif /* LOG */
119725a99e2eSeric }
119825a99e2eSeric /*
119951552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
120025a99e2eSeric **
120151552439Seric **	This can be made an arbitrary message separator by changing $l
120251552439Seric **
12039b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
12049b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
12059b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
12069b6c17a6Seric **	this kind of antique garbage????
120725a99e2eSeric **
120825a99e2eSeric **	Parameters:
120951552439Seric **		fp -- the file to output to.
121051552439Seric **		m -- the mailer describing this entry.
121125a99e2eSeric **
121225a99e2eSeric **	Returns:
121351552439Seric **		none
121425a99e2eSeric **
121525a99e2eSeric **	Side Effects:
121651552439Seric **		outputs some text to fp.
121725a99e2eSeric */
121825a99e2eSeric 
1219b31e7f2bSeric putfromline(fp, m, e)
122051552439Seric 	register FILE *fp;
122151552439Seric 	register MAILER *m;
1222b31e7f2bSeric 	ENVELOPE *e;
122325a99e2eSeric {
12242bc47524Seric 	char *template = "\201l\n";
122551552439Seric 	char buf[MAXLINE];
122625a99e2eSeric 
122757fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
122851552439Seric 		return;
122913bbc08cSeric 
12302c7e1b8dSeric # ifdef UGLYUUCP
123157fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
123274b6e67bSeric 	{
1233ea09d6edSeric 		char *bang;
1234ea09d6edSeric 		char xbuf[MAXLINE];
123574b6e67bSeric 
12362bc47524Seric 		expand("\201<", buf, &buf[sizeof buf - 1], e);
12376c2c3107Seric 		bang = strchr(buf, '!');
123874b6e67bSeric 		if (bang == NULL)
123908b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
124074b6e67bSeric 		else
1241588cad61Seric 		{
1242ea09d6edSeric 			*bang++ = '\0';
12432bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1244ea09d6edSeric 			template = xbuf;
124574b6e67bSeric 		}
1246588cad61Seric 	}
12476c2c3107Seric # endif /* UGLYUUCP */
1248b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
124977b52738Seric 	putline(buf, fp, m);
1250bc6e2962Seric }
1251bc6e2962Seric /*
125251552439Seric **  PUTBODY -- put the body of a message.
125351552439Seric **
125451552439Seric **	Parameters:
125551552439Seric **		fp -- file to output onto.
125677b52738Seric **		m -- a mailer descriptor to control output format.
12579a6a5f55Seric **		e -- the envelope to put out.
125851552439Seric **
125951552439Seric **	Returns:
126051552439Seric **		none.
126151552439Seric **
126251552439Seric **	Side Effects:
126351552439Seric **		The message is written onto fp.
126451552439Seric */
126551552439Seric 
126677b52738Seric putbody(fp, m, e)
126751552439Seric 	FILE *fp;
1268588cad61Seric 	MAILER *m;
12699a6a5f55Seric 	register ENVELOPE *e;
127051552439Seric {
127177b52738Seric 	char buf[MAXLINE];
127251552439Seric 
127351552439Seric 	/*
127451552439Seric 	**  Output the body of the message
127551552439Seric 	*/
127651552439Seric 
12779a6a5f55Seric 	if (e->e_dfp == NULL)
127851552439Seric 	{
12799a6a5f55Seric 		if (e->e_df != NULL)
12809a6a5f55Seric 		{
12819a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
12829a6a5f55Seric 			if (e->e_dfp == NULL)
12838f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
12848f9146b0Srick 				e->e_df, e->e_to, e->e_from);
12859a6a5f55Seric 		}
12869a6a5f55Seric 		else
128777b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
12889a6a5f55Seric 	}
12899a6a5f55Seric 	if (e->e_dfp != NULL)
12909a6a5f55Seric 	{
12919a6a5f55Seric 		rewind(e->e_dfp);
129277b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
129324fc8aeeSeric 		{
129424fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1295d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
12963462ad9eSeric 				(void) putc('>', fp);
129777b52738Seric 			putline(buf, fp, m);
129824fc8aeeSeric 		}
129951552439Seric 
13009a6a5f55Seric 		if (ferror(e->e_dfp))
130151552439Seric 		{
130251552439Seric 			syserr("putbody: read error");
130351552439Seric 			ExitStat = EX_IOERR;
130451552439Seric 		}
130551552439Seric 	}
130651552439Seric 
130751552439Seric 	(void) fflush(fp);
130851552439Seric 	if (ferror(fp) && errno != EPIPE)
130951552439Seric 	{
131051552439Seric 		syserr("putbody: write error");
131151552439Seric 		ExitStat = EX_IOERR;
131251552439Seric 	}
131351552439Seric 	errno = 0;
131425a99e2eSeric }
131525a99e2eSeric /*
13169836bea2Seric **  PARENTBODY -- put the body of the parent of a message.
13179836bea2Seric **
13189836bea2Seric **	Parameters:
13199836bea2Seric **		fp -- file to output onto.
13209836bea2Seric **		m -- a mailer descriptor to control output format.
13219836bea2Seric **		e -- the envelope whose parent to put out.
13229836bea2Seric **
13239836bea2Seric **	Returns:
13249836bea2Seric **		none.
13259836bea2Seric **
13269836bea2Seric **	Side Effects:
13279836bea2Seric **		The message is written onto fp.
13289836bea2Seric */
13299836bea2Seric 
13309836bea2Seric parentbody(fp, m, e)
13319836bea2Seric 	FILE *fp;
13329836bea2Seric 	MAILER *m;
13339836bea2Seric 	register ENVELOPE *e;
13349836bea2Seric {
13359836bea2Seric 	putbody(fp, m, e->e_parent);
13369836bea2Seric }
13379836bea2Seric /*
133825a99e2eSeric **  MAILFILE -- Send a message to a file.
133925a99e2eSeric **
1340f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1341f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1342f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1343f129ec7dSeric **	sendmail runs as root.
1344f129ec7dSeric **
1345588cad61Seric **	This could be done as a subordinate mailer, except that it
1346588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1347588cad61Seric **	view this as being sufficiently important as to include it
1348588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1349588cad61Seric **	to create another process plus some pipes to save the message.
1350588cad61Seric **
135125a99e2eSeric **	Parameters:
135225a99e2eSeric **		filename -- the name of the file to send to.
13536259796dSeric **		ctladdr -- the controlling address header -- includes
13546259796dSeric **			the userid/groupid to be when sending.
135525a99e2eSeric **
135625a99e2eSeric **	Returns:
135725a99e2eSeric **		The exit code associated with the operation.
135825a99e2eSeric **
135925a99e2eSeric **	Side Effects:
136025a99e2eSeric **		none.
136125a99e2eSeric */
136225a99e2eSeric 
1363b31e7f2bSeric mailfile(filename, ctladdr, e)
136425a99e2eSeric 	char *filename;
13656259796dSeric 	ADDRESS *ctladdr;
1366b31e7f2bSeric 	register ENVELOPE *e;
136725a99e2eSeric {
136825a99e2eSeric 	register FILE *f;
136932d19d43Seric 	register int pid;
137015d084d5Seric 	int mode;
137125a99e2eSeric 
137232d19d43Seric 	/*
137332d19d43Seric 	**  Fork so we can change permissions here.
137432d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
137532d19d43Seric 	**	the complications of calling subroutines, etc.
137632d19d43Seric 	*/
137732d19d43Seric 
137832d19d43Seric 	DOFORK(fork);
137932d19d43Seric 
138032d19d43Seric 	if (pid < 0)
138132d19d43Seric 		return (EX_OSERR);
138232d19d43Seric 	else if (pid == 0)
138332d19d43Seric 	{
138432d19d43Seric 		/* child -- actually write to file */
1385f129ec7dSeric 		struct stat stb;
1386f129ec7dSeric 
13870984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
13880984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
13890984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
13903462ad9eSeric 		(void) umask(OldUmask);
139195f16dc0Seric 
1392f129ec7dSeric 		if (stat(filename, &stb) < 0)
1393e6e1265fSeric 			stb.st_mode = 0666;
139415d084d5Seric 		mode = stb.st_mode;
139595f16dc0Seric 
139695f16dc0Seric 		/* limit the errors to those actually caused in the child */
139795f16dc0Seric 		errno = 0;
139895f16dc0Seric 		ExitStat = EX_OK;
139995f16dc0Seric 
1400f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1401f129ec7dSeric 			exit(EX_CANTCREAT);
140203827b5fSeric 		if (ctladdr == NULL)
14038f9146b0Srick 			ctladdr = &e->e_from;
140415d084d5Seric 		else
140515d084d5Seric 		{
140615d084d5Seric 			/* ignore setuid and setgid bits */
140715d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
140815d084d5Seric 		}
140915d084d5Seric 
14108f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
14118f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
14128f9146b0Srick 		{
14138f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
141495f16dc0Seric 			if (e->e_dfp == NULL)
141595f16dc0Seric 			{
14168f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
14178f9146b0Srick 					e->e_df, e->e_to, e->e_from);
14188f9146b0Srick 			}
14198f9146b0Srick 		}
14208f9146b0Srick 
142115d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1422e36b99e2Seric 		{
142395f16dc0Seric 			if (ctladdr->q_uid == 0)
142495f16dc0Seric 			{
1425e36b99e2Seric 				(void) setgid(DefGid);
1426898a126bSbostic 				(void) initgroups(DefUser, DefGid);
142795f16dc0Seric 			}
142895f16dc0Seric 			else
142995f16dc0Seric 			{
14306259796dSeric 				(void) setgid(ctladdr->q_gid);
1431898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1432898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1433898a126bSbostic 					ctladdr->q_gid);
1434898a126bSbostic 			}
1435e36b99e2Seric 		}
143615d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1437e36b99e2Seric 		{
1438e36b99e2Seric 			if (ctladdr->q_uid == 0)
1439e36b99e2Seric 				(void) setuid(DefUid);
1440e36b99e2Seric 			else
14416259796dSeric 				(void) setuid(ctladdr->q_uid);
1442e36b99e2Seric 		}
144395f16dc0Seric 		FileName = filename;
144495f16dc0Seric 		LineNumber = 0;
144527628d59Seric 		f = dfopen(filename, "a");
144625a99e2eSeric 		if (f == NULL)
144795f16dc0Seric 		{
144808b25121Seric 			message("554 cannot open");
144932d19d43Seric 			exit(EX_CANTCREAT);
145095f16dc0Seric 		}
145125a99e2eSeric 
1452b31e7f2bSeric 		putfromline(f, ProgMailer, e);
1453b843d759Seric 		(*e->e_puthdr)(f, ProgMailer, e);
145477b52738Seric 		putline("\n", f, ProgMailer);
1455b843d759Seric 		(*e->e_putbody)(f, ProgMailer, e);
145677b52738Seric 		putline("\n", f, ProgMailer);
145795f16dc0Seric 		if (ferror(f))
145895f16dc0Seric 		{
145908b25121Seric 			message("451 I/O error");
146095f16dc0Seric 			setstat(EX_IOERR);
146195f16dc0Seric 		}
1462db8841e9Seric 		(void) fclose(f);
146332d19d43Seric 		(void) fflush(stdout);
1464e36b99e2Seric 
146527628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1466c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
146795f16dc0Seric 		exit(ExitStat);
146813bbc08cSeric 		/*NOTREACHED*/
146932d19d43Seric 	}
147032d19d43Seric 	else
147132d19d43Seric 	{
147232d19d43Seric 		/* parent -- wait for exit status */
1473588cad61Seric 		int st;
147432d19d43Seric 
1475588cad61Seric 		st = waitfor(pid);
1476588cad61Seric 		if ((st & 0377) != 0)
1477588cad61Seric 			return (EX_UNAVAILABLE);
1478588cad61Seric 		else
1479588cad61Seric 			return ((st >> 8) & 0377);
14808f9146b0Srick 		/*NOTREACHED*/
148132d19d43Seric 	}
148225a99e2eSeric }
1483ea4dc939Seric /*
1484ea4dc939Seric **  SENDALL -- actually send all the messages.
1485ea4dc939Seric **
1486ea4dc939Seric **	Parameters:
14870c52a0b3Seric **		e -- the envelope to send.
14887b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
14897b95031aSeric **			the current SendMode.
1490ea4dc939Seric **
1491ea4dc939Seric **	Returns:
1492ea4dc939Seric **		none.
1493ea4dc939Seric **
1494ea4dc939Seric **	Side Effects:
1495ea4dc939Seric **		Scans the send lists and sends everything it finds.
14960c52a0b3Seric **		Delivers any appropriate error messages.
1497276723a8Seric **		If we are running in a non-interactive mode, takes the
1498276723a8Seric **			appropriate action.
1499ea4dc939Seric */
1500ea4dc939Seric 
1501276723a8Seric sendall(e, mode)
15020c52a0b3Seric 	ENVELOPE *e;
1503276723a8Seric 	char mode;
1504ea4dc939Seric {
1505e77e673fSeric 	register ADDRESS *q;
150614a8ed7aSeric 	bool oldverbose;
1507276723a8Seric 	int pid;
15089836bea2Seric 	char *owner;
15099836bea2Seric 	int otherowners;
15109836bea2Seric 	ENVELOPE *splitenv = NULL;
15111c265a00Seric # ifdef LOCKF
15121c265a00Seric 	struct flock lfd;
15131c265a00Seric # endif
1514ea4dc939Seric 
15157b95031aSeric 	/* determine actual delivery mode */
15167b95031aSeric 	if (mode == SM_DEFAULT)
15177b95031aSeric 	{
15185f73204aSeric 		extern bool shouldqueue();
15197b95031aSeric 
15207b95031aSeric 		mode = SendMode;
15213f324333Seric 		if (mode != SM_VERIFY &&
15223f324333Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
15233f324333Seric 			mode = SM_QUEUE;
15247b95031aSeric 	}
15257b95031aSeric 
1526df864a8fSeric 	if (tTd(13, 1))
1527772e6e50Seric 	{
1528276723a8Seric 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
15290c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1530772e6e50Seric 	}
1531ea4dc939Seric 
15320c52a0b3Seric 	/*
1533276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1534588cad61Seric 	**	Check to make sure the hop count is reasonable.
1535588cad61Seric 	**	Delete sends to the sender in mailing lists.
1536276723a8Seric 	*/
1537276723a8Seric 
1538588cad61Seric 	CurEnv = e;
1539276723a8Seric 
15401d57450bSeric 	if (e->e_hopcount > MaxHopCount)
1541276723a8Seric 	{
15428f9146b0Srick 		errno = 0;
154308b25121Seric 		syserr("554 too many hops %d (%d max): from %s, to %s",
1544655518ecSeric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
1545655518ecSeric 			e->e_sendqueue->q_paddr);
1546588cad61Seric 		return;
1547588cad61Seric 	}
1548588cad61Seric 
1549588cad61Seric 	if (!MeToo)
1550276723a8Seric 	{
1551f3d6c55cSeric 		extern ADDRESS *recipient();
1552f3d6c55cSeric 
1553588cad61Seric 		e->e_from.q_flags |= QDONTSEND;
155475f1ade9Seric 		if (tTd(13, 5))
155575f1ade9Seric 		{
155675f1ade9Seric 			printf("sendall: QDONTSEND ");
155775f1ade9Seric 			printaddr(&e->e_from, FALSE);
155875f1ade9Seric 		}
1559b843d759Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1560276723a8Seric 	}
1561588cad61Seric 
15629836bea2Seric 	/*
15639836bea2Seric 	**  Handle alias owners.
15649836bea2Seric 	**
15659836bea2Seric 	**	We scan up the q_alias chain looking for owners.
15669836bea2Seric 	**	We discard owners that are the same as the return path.
15679836bea2Seric 	*/
15689836bea2Seric 
15699836bea2Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15709836bea2Seric 	{
15719836bea2Seric 		register struct address *a;
15729836bea2Seric 
15739836bea2Seric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
15749836bea2Seric 			continue;
15759836bea2Seric 		if (a != NULL)
15769836bea2Seric 			q->q_owner = a->q_owner;
15779836bea2Seric 
15789836bea2Seric 		if (q->q_owner != NULL && !bitset(QDONTSEND, q->q_flags) &&
15799836bea2Seric 		    strcmp(q->q_owner, e->e_returnpath) == 0)
15809836bea2Seric 			q->q_owner = NULL;
15819836bea2Seric 	}
15829836bea2Seric 
15839836bea2Seric 	owner = "";
15849836bea2Seric 	otherowners = 1;
15859836bea2Seric 	while (owner != NULL && otherowners > 0)
15869836bea2Seric 	{
15879836bea2Seric 		owner = NULL;
15889836bea2Seric 		otherowners = 0;
15899836bea2Seric 
15909836bea2Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15919836bea2Seric 		{
15929836bea2Seric 			if (bitset(QDONTSEND, q->q_flags))
15939836bea2Seric 				continue;
15949836bea2Seric 
15959836bea2Seric 			if (q->q_owner != NULL)
15969836bea2Seric 			{
15979836bea2Seric 				if (owner == NULL)
15989836bea2Seric 					owner = q->q_owner;
15999836bea2Seric 				else if (owner != q->q_owner)
16009836bea2Seric 				{
16019836bea2Seric 					if (strcmp(owner, q->q_owner) == 0)
16029836bea2Seric 					{
16039836bea2Seric 						/* make future comparisons cheap */
16049836bea2Seric 						q->q_owner = owner;
16059836bea2Seric 					}
16069836bea2Seric 					else
16079836bea2Seric 					{
16089836bea2Seric 						otherowners++;
16099836bea2Seric 					}
16109836bea2Seric 					owner = q->q_owner;
16119836bea2Seric 				}
16129836bea2Seric 			}
16139836bea2Seric 			else
16149836bea2Seric 			{
16159836bea2Seric 				otherowners++;
16169836bea2Seric 			}
16179836bea2Seric 		}
16189836bea2Seric 
16199836bea2Seric 		if (owner != NULL && otherowners > 0)
16209836bea2Seric 		{
16219836bea2Seric 			register ENVELOPE *ee;
16229836bea2Seric 			extern HDR *copyheader();
16239836bea2Seric 			extern ADDRESS *copyqueue();
16249836bea2Seric 
16259836bea2Seric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
16269836bea2Seric 			STRUCTCOPY(*e, *ee);
16279836bea2Seric 			ee->e_id = NULL;
16289836bea2Seric 			ee->e_parent = e;
16299836bea2Seric 			ee->e_header = copyheader(e->e_header);
16309836bea2Seric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
16319836bea2Seric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
16329836bea2Seric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE);
16339836bea2Seric 			ee->e_returnpath = owner;
16349836bea2Seric 			ee->e_putbody = parentbody;
16359836bea2Seric 			ee->e_sibling = splitenv;
16369836bea2Seric 			splitenv = ee;
16379836bea2Seric 
16389836bea2Seric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
16399836bea2Seric 				if (q->q_owner == owner)
16409836bea2Seric 					q->q_flags |= QDONTSEND;
16419836bea2Seric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
16429836bea2Seric 				if (q->q_owner != owner)
16439836bea2Seric 					q->q_flags |= QDONTSEND;
16449836bea2Seric 
16459836bea2Seric 			if (e->e_df != NULL && mode != SM_VERIFY)
16469836bea2Seric 			{
16479836bea2Seric 				ee->e_dfp = NULL;
1648239b3abaSeric 				ee->e_df = newstr(queuename(ee, 'd'));
16499836bea2Seric 				if (link(e->e_df, ee->e_df) < 0)
16509836bea2Seric 				{
16519836bea2Seric 					syserr("sendall: link(%s, %s)",
16529836bea2Seric 						e->e_df, ee->e_df);
16539836bea2Seric 				}
16549836bea2Seric 			}
16559836bea2Seric 		}
16569836bea2Seric 	}
16579836bea2Seric 
16589836bea2Seric 	if (owner != NULL)
16599836bea2Seric 		e->e_returnpath = owner;
16609836bea2Seric 
1661588cad61Seric # ifdef QUEUE
1662b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1663b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1664b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
1665e020b127Seric 		queueup(e, TRUE, mode == SM_QUEUE);
16666c2c3107Seric #endif /* QUEUE */
1667276723a8Seric 
16689836bea2Seric 	if (splitenv != NULL)
16699836bea2Seric 	{
16709836bea2Seric 		if (tTd(13, 1))
16719836bea2Seric 		{
16729836bea2Seric 			printf("\nsendall: Split queue; remaining queue:\n");
16739836bea2Seric 			printaddr(e->e_sendqueue, TRUE);
16749836bea2Seric 		}
16759836bea2Seric 
16769836bea2Seric 		while (splitenv != NULL)
16779836bea2Seric 		{
16789836bea2Seric 			sendall(splitenv, mode);
16799836bea2Seric 			splitenv = splitenv->e_sibling;
16809836bea2Seric 		}
16819836bea2Seric 
16829836bea2Seric 		CurEnv = e;
16839836bea2Seric 	}
16849836bea2Seric 
1685276723a8Seric 	oldverbose = Verbose;
1686276723a8Seric 	switch (mode)
1687276723a8Seric 	{
1688276723a8Seric 	  case SM_VERIFY:
1689276723a8Seric 		Verbose = TRUE;
1690276723a8Seric 		break;
1691276723a8Seric 
1692276723a8Seric 	  case SM_QUEUE:
1693c7f5410dSeric   queueonly:
1694b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1695276723a8Seric 		return;
1696276723a8Seric 
1697276723a8Seric 	  case SM_FORK:
16989a6a5f55Seric 		if (e->e_xfp != NULL)
16999a6a5f55Seric 			(void) fflush(e->e_xfp);
1700c7f5410dSeric 
1701c7f5410dSeric # ifdef LOCKF
1702c7f5410dSeric 		/*
1703c7f5410dSeric 		**  Since lockf has the interesting semantic that the
17041c265a00Seric 		**  lock is lost when we fork, we have to risk losing
17051c265a00Seric 		**  the lock here by closing before the fork, and then
17061c265a00Seric 		**  trying to get it back in the child.
1707c7f5410dSeric 		*/
1708c7f5410dSeric 
1709e020b127Seric 		if (e->e_lockfp != NULL)
1710c7f5410dSeric 		{
1711e020b127Seric 			(void) fclose(e->e_lockfp);
1712e020b127Seric 			e->e_lockfp = NULL;
1713c7f5410dSeric 		}
1714c7f5410dSeric # endif /* LOCKF */
1715c7f5410dSeric 
1716276723a8Seric 		pid = fork();
1717276723a8Seric 		if (pid < 0)
1718276723a8Seric 		{
1719c7f5410dSeric 			goto queueonly;
1720276723a8Seric 		}
1721276723a8Seric 		else if (pid > 0)
1722a6fce3d8Seric 		{
1723a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1724b254bcb6Seric 			e->e_id = e->e_df = NULL;
1725c7f5410dSeric # ifndef LOCKF
1726e020b127Seric 			if (e->e_lockfp != NULL)
172775f1ade9Seric 			{
1728e020b127Seric 				(void) fclose(e->e_lockfp);
172975f1ade9Seric 				e->e_lockfp = NULL;
173075f1ade9Seric 			}
1731c7f5410dSeric # endif
173275f1ade9Seric 
173375f1ade9Seric 			/* close any random open files in the envelope */
173475f1ade9Seric 			if (e->e_dfp != NULL)
173575f1ade9Seric 			{
173675f1ade9Seric 				(void) fclose(e->e_dfp);
173775f1ade9Seric 				e->e_dfp = NULL;
173875f1ade9Seric 			}
173975f1ade9Seric 			if (e->e_xfp != NULL)
174075f1ade9Seric 			{
174175f1ade9Seric 				(void) fclose(e->e_xfp);
174275f1ade9Seric 				e->e_xfp = NULL;
174375f1ade9Seric 			}
1744276723a8Seric 			return;
1745a6fce3d8Seric 		}
1746276723a8Seric 
1747276723a8Seric 		/* double fork to avoid zombies */
1748276723a8Seric 		if (fork() > 0)
1749276723a8Seric 			exit(EX_OK);
1750276723a8Seric 
1751a6fce3d8Seric 		/* be sure we are immune from the terminal */
1752769e215aSeric 		disconnect(FALSE);
1753a6fce3d8Seric 
1754e6720d51Seric # ifdef LOCKF
1755e6720d51Seric 		/*
1756c7f5410dSeric 		**  Now try to get our lock back.
1757e6720d51Seric 		*/
1758e6720d51Seric 
17591c265a00Seric 		lfd.l_type = F_WRLCK;
17601c265a00Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1761e020b127Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1762e020b127Seric 		if (e->e_lockfp == NULL ||
17631c265a00Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1764e6720d51Seric 		{
1765e6720d51Seric 			/* oops....  lost it */
1766e6720d51Seric # ifdef LOG
17672f624c86Seric 			if (LogLevel > 29)
1768c7f5410dSeric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1769b31e7f2bSeric 					e->e_id);
1770e6720d51Seric # endif /* LOG */
1771e6720d51Seric 			exit(EX_OK);
1772e6720d51Seric 		}
1773e6720d51Seric # endif /* LOCKF */
1774e6720d51Seric 
17751caa9a59Seric 		/*
17761caa9a59Seric 		**  Close any cached connections.
17771caa9a59Seric 		**
17781caa9a59Seric 		**	We don't send the QUIT protocol because the parent
17791caa9a59Seric 		**	still knows about the connection.
17801caa9a59Seric 		**
17811caa9a59Seric 		**	This should only happen when delivering an error
17821caa9a59Seric 		**	message.
17831caa9a59Seric 		*/
17841caa9a59Seric 
17851caa9a59Seric 		mci_flush(FALSE, NULL);
17861caa9a59Seric 
1787276723a8Seric 		break;
1788276723a8Seric 	}
1789276723a8Seric 
1790276723a8Seric 	/*
17910c52a0b3Seric 	**  Run through the list and send everything.
17920c52a0b3Seric 	*/
17930c52a0b3Seric 
1794655518ecSeric 	e->e_nsent = 0;
17950c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1796ea4dc939Seric 	{
1797276723a8Seric 		if (mode == SM_VERIFY)
1798ea4dc939Seric 		{
1799a6fce3d8Seric 			e->e_to = q->q_paddr;
1800e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
180108b25121Seric 				message("deliverable");
1802ea4dc939Seric 		}
1803dde5acadSeric 		else if (!bitset(QDONTSEND, q->q_flags))
1804dde5acadSeric 		{
1805de1179f5Seric # ifdef QUEUE
1806dde5acadSeric 			/*
1807dde5acadSeric 			**  Checkpoint the send list every few addresses
1808dde5acadSeric 			*/
1809dde5acadSeric 
1810655518ecSeric 			if (e->e_nsent >= CheckpointInterval)
1811dde5acadSeric 			{
1812e020b127Seric 				queueup(e, TRUE, FALSE);
1813655518ecSeric 				e->e_nsent = 0;
1814dde5acadSeric 			}
1815de1179f5Seric # endif /* QUEUE */
1816655518ecSeric 			(void) deliver(e, q);
1817dde5acadSeric 		}
1818ea4dc939Seric 	}
181914a8ed7aSeric 	Verbose = oldverbose;
18200c52a0b3Seric 
18210c52a0b3Seric 	/*
18220c52a0b3Seric 	**  Now run through and check for errors.
18230c52a0b3Seric 	*/
18240c52a0b3Seric 
1825e020b127Seric 	if (mode == SM_VERIFY)
18260c52a0b3Seric 		return;
18270c52a0b3Seric 
18280c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
18290c52a0b3Seric 	{
1830df864a8fSeric 		if (tTd(13, 3))
1831df864a8fSeric 		{
1832df864a8fSeric 			printf("Checking ");
1833df864a8fSeric 			printaddr(q, FALSE);
1834df864a8fSeric 		}
1835df864a8fSeric 
1836b254bcb6Seric 		/* only send errors if the message failed */
1837b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1838b254bcb6Seric 			continue;
18390c52a0b3Seric 
1840b8043ddbSeric 		if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0)
18419836bea2Seric 			(void) sendtolist(e->e_from.q_paddr, NULL,
18421caa9a59Seric 					  &e->e_errorqueue, e);
18430c52a0b3Seric 	}
1844276723a8Seric 
1845276723a8Seric 	if (mode == SM_FORK)
1846276723a8Seric 		finis();
18470c52a0b3Seric }
1848e103b48fSeric /*
1849e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1850e103b48fSeric **
1851e103b48fSeric **	The signature describes how we are going to send this -- it
1852e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1853e103b48fSeric **	an ordered list of MX hosts.
1854e103b48fSeric **
1855e103b48fSeric **	Parameters:
1856e103b48fSeric **		m -- the mailer describing this host.
1857e103b48fSeric **		host -- the host name.
1858e103b48fSeric **		e -- the current envelope.
1859e103b48fSeric **
1860e103b48fSeric **	Returns:
1861e103b48fSeric **		The signature for this host.
1862e103b48fSeric **
1863e103b48fSeric **	Side Effects:
1864e103b48fSeric **		Can tweak the symbol table.
1865e103b48fSeric */
1866e103b48fSeric 
1867e103b48fSeric char *
1868e103b48fSeric hostsignature(m, host, e)
1869e103b48fSeric 	register MAILER *m;
1870e103b48fSeric 	char *host;
1871e103b48fSeric 	ENVELOPE *e;
1872e103b48fSeric {
1873e103b48fSeric 	register char *p;
1874e103b48fSeric 	register STAB *s;
1875e103b48fSeric 	int i;
1876e103b48fSeric 	int len;
1877e103b48fSeric #ifdef NAMED_BIND
1878e103b48fSeric 	int nmx;
1879e103b48fSeric 	auto int rcode;
1880e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
1881e103b48fSeric 	static char myhostbuf[MAXNAME];
1882e103b48fSeric #endif
1883e103b48fSeric 
1884e103b48fSeric 	/*
1885e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
1886e103b48fSeric 	*/
1887e103b48fSeric 
1888e103b48fSeric 	p = m->m_mailer;
1889e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
1890e103b48fSeric 	{
1891e103b48fSeric 		/* just an ordinary mailer */
1892e103b48fSeric 		return host;
1893e103b48fSeric 	}
1894e103b48fSeric 
1895e103b48fSeric 	/*
1896e103b48fSeric 	**  If it is a numeric address, just return it.
1897e103b48fSeric 	*/
1898e103b48fSeric 
1899e103b48fSeric 	if (host[0] == '[')
1900e103b48fSeric 		return host;
1901e103b48fSeric 
1902e103b48fSeric 	/*
1903e103b48fSeric 	**  Look it up in the symbol table.
1904e103b48fSeric 	*/
1905e103b48fSeric 
1906e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
1907e103b48fSeric 	if (s->s_hostsig != NULL)
1908e103b48fSeric 		return s->s_hostsig;
1909e103b48fSeric 
1910e103b48fSeric 	/*
1911e103b48fSeric 	**  Not already there -- create a signature.
1912e103b48fSeric 	*/
1913e103b48fSeric 
1914e103b48fSeric #ifdef NAMED_BIND
1915e103b48fSeric 	if (myhostbuf[0] == '\0')
19162bc47524Seric 		expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e);
1917e103b48fSeric 
1918e103b48fSeric 	nmx = getmxrr(host, mxhosts, myhostbuf, &rcode);
1919e103b48fSeric 	if (nmx <= 0)
1920e103b48fSeric 	{
1921e103b48fSeric 		register MCI *mci;
1922e103b48fSeric 		extern int errno;
1923e103b48fSeric 		extern MCI *mci_get();
1924e103b48fSeric 
1925e103b48fSeric 		/* update the connection info for this host */
1926e103b48fSeric 		mci = mci_get(host, m);
1927e103b48fSeric 		mci->mci_exitstat = rcode;
1928e103b48fSeric 		mci->mci_errno = errno;
1929e103b48fSeric 
1930e103b48fSeric 		/* and return the original host name as the signature */
1931e103b48fSeric 		s->s_hostsig = host;
1932e103b48fSeric 		return host;
1933e103b48fSeric 	}
1934e103b48fSeric 
1935e103b48fSeric 	len = 0;
1936e103b48fSeric 	for (i = 0; i < nmx; i++)
1937e103b48fSeric 	{
1938e103b48fSeric 		len += strlen(mxhosts[i]) + 1;
1939e103b48fSeric 	}
1940e103b48fSeric 	s->s_hostsig = p = xalloc(len);
1941e103b48fSeric 	for (i = 0; i < nmx; i++)
1942e103b48fSeric 	{
1943e103b48fSeric 		if (i != 0)
1944e103b48fSeric 			*p++ = ':';
1945e103b48fSeric 		strcpy(p, mxhosts[i]);
1946e103b48fSeric 		p += strlen(p);
1947e103b48fSeric 	}
1948e103b48fSeric 	makelower(s->s_hostsig);
1949e103b48fSeric #else
1950e103b48fSeric 	/* not using BIND -- the signature is just the host name */
1951e103b48fSeric 	s->s_hostsig = host;
1952e103b48fSeric #endif
1953e103b48fSeric 	if (tTd(17, 1))
1954e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
1955e103b48fSeric 	return s->s_hostsig;
1956e103b48fSeric }
1957