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*579ef0ddSeric static char sccsid[] = "@(#)deliver.c	6.47 (Berkeley) 03/18/93";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14a8c080f0Seric #include <signal.h>
15c77d1c25Seric #include <sys/stat.h>
16f28da541Smiriam #include <netdb.h>
17911693bfSbostic #include <errno.h>
18134746fbSeric #ifdef NAMED_BIND
19912a731aSbostic #include <arpa/nameser.h>
20912a731aSbostic #include <resolv.h>
21134746fbSeric #endif
2225a99e2eSeric 
2325a99e2eSeric /*
2413bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
2513bbc08cSeric **
2613bbc08cSeric **	This routine delivers to everyone on the same host as the
2713bbc08cSeric **	user on the head of the list.  It is clever about mailers
2813bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
2913bbc08cSeric **	that it will deliver to all these addresses however -- so
3013bbc08cSeric **	deliver should be called once for each address on the
3113bbc08cSeric **	list.
3225a99e2eSeric **
3325a99e2eSeric **	Parameters:
34588cad61Seric **		e -- the envelope to deliver.
35c77d1c25Seric **		firstto -- head of the address list to deliver to.
3625a99e2eSeric **
3725a99e2eSeric **	Returns:
3825a99e2eSeric **		zero -- successfully delivered.
3925a99e2eSeric **		else -- some failure, see ExitStat for more info.
4025a99e2eSeric **
4125a99e2eSeric **	Side Effects:
4225a99e2eSeric **		The standard input is passed off to someone.
4325a99e2eSeric */
4425a99e2eSeric 
45588cad61Seric deliver(e, firstto)
46588cad61Seric 	register ENVELOPE *e;
47c77d1c25Seric 	ADDRESS *firstto;
4825a99e2eSeric {
4978442df3Seric 	char *host;			/* host being sent to */
5078442df3Seric 	char *user;			/* user being sent to */
5125a99e2eSeric 	char **pvp;
525dfc646bSeric 	register char **mvp;
5325a99e2eSeric 	register char *p;
54588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
556259796dSeric 	ADDRESS *ctladdr;
56b31e7f2bSeric 	register MCI *mci;
57c77d1c25Seric 	register ADDRESS *to = firstto;
58c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
59772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
60911693bfSbostic 	int rcode;			/* response code */
61e103b48fSeric 	char *firstsig;			/* signature of firstto */
62ee6bf8dfSeric 	char *pv[MAXPV+1];
63*579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
64ee6bf8dfSeric 	char buf[MAXNAME];
65c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
66fabb3bd4Seric 	extern int checkcompat();
67ee6bf8dfSeric 	extern ADDRESS *getctladdr();
68ee6bf8dfSeric 	extern char *remotename();
69b31e7f2bSeric 	extern MCI *openmailer();
70e103b48fSeric 	extern char *hostsignature();
7125a99e2eSeric 
7235490626Seric 	errno = 0;
73ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, 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 		{
106ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
107c6e18ac5Seric 			    to->q_mailer != m)
108f4560e80Seric 				continue;
109f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
110588cad61Seric 			e->e_to = to->q_paddr;
11108b25121Seric 			message("queued");
1122f624c86Seric 			if (LogLevel > 8)
11381161401Seric 				logdelivery(m, NULL, "queued", e);
114f4560e80Seric 		}
115588cad61Seric 		e->e_to = NULL;
116f3dbc832Seric 		return (0);
117f3dbc832Seric 	}
118f3dbc832Seric 
11925a99e2eSeric 	/*
1205dfc646bSeric 	**  Do initial argv setup.
1215dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
1225dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
1235dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
1245dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
1255dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
1263bea8136Seric 	**		The from address rewrite is expected to make
1273bea8136Seric 	**		the address relative to the other end.
1285dfc646bSeric 	*/
1295dfc646bSeric 
13078442df3Seric 	/* rewrite from address, using rewriting rules */
131ee4b0922Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, TRUE, FALSE,
1329c69ca01Seric 					   TRUE, FALSE, e));
133ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
134588cad61Seric 	define('h', host, e);			/* to host */
1355dfc646bSeric 	Errors = 0;
1365dfc646bSeric 	pvp = pv;
1375dfc646bSeric 	*pvp++ = m->m_argv[0];
1385dfc646bSeric 
1395dfc646bSeric 	/* insert -f or -r flag as appropriate */
14057fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
1415dfc646bSeric 	{
14257fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
1435dfc646bSeric 			*pvp++ = "-f";
1445dfc646bSeric 		else
1455dfc646bSeric 			*pvp++ = "-r";
146c23ed322Seric 		*pvp++ = newstr(rpathbuf);
1475dfc646bSeric 	}
1485dfc646bSeric 
1495dfc646bSeric 	/*
1505dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
1515dfc646bSeric 	**  up to the first entry containing "$u".  There can only
1525dfc646bSeric 	**  be one of these, and there are only a few more slots
1535dfc646bSeric 	**  in the pv after it.
1545dfc646bSeric 	*/
1555dfc646bSeric 
1565dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
1575dfc646bSeric 	{
1582bc47524Seric 		/* can't use strchr here because of sign extension problems */
1592bc47524Seric 		while (*p != '\0')
1602bc47524Seric 		{
1612bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
1622bc47524Seric 			{
1632bc47524Seric 				if (*p == 'u')
1645dfc646bSeric 					break;
1652bc47524Seric 			}
1662bc47524Seric 		}
1672bc47524Seric 
1682bc47524Seric 		if (*p != '\0')
1695dfc646bSeric 			break;
1705dfc646bSeric 
1715dfc646bSeric 		/* this entry is safe -- go ahead and process it */
172588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
1735dfc646bSeric 		*pvp++ = newstr(buf);
1745dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
1755dfc646bSeric 		{
17608b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
1775dfc646bSeric 			return (-1);
1785dfc646bSeric 		}
1795dfc646bSeric 	}
180c579ef51Seric 
18133db8731Seric 	/*
18233db8731Seric 	**  If we have no substitution for the user name in the argument
18333db8731Seric 	**  list, we know that we must supply the names otherwise -- and
18433db8731Seric 	**  SMTP is the answer!!
18533db8731Seric 	*/
18633db8731Seric 
1875dfc646bSeric 	if (*mvp == NULL)
188c579ef51Seric 	{
189c579ef51Seric 		/* running SMTP */
1902c7e1b8dSeric # ifdef SMTP
191c579ef51Seric 		clever = TRUE;
192c579ef51Seric 		*pvp = NULL;
1936c2c3107Seric # else /* SMTP */
19433db8731Seric 		/* oops!  we don't implement SMTP */
19508b25121Seric 		syserr("554 SMTP style mailer");
1962c7e1b8dSeric 		return (EX_SOFTWARE);
1976c2c3107Seric # endif /* SMTP */
198c579ef51Seric 	}
1995dfc646bSeric 
2005dfc646bSeric 	/*
2015dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
2025dfc646bSeric 	**  run through our address list and append all the addresses
2035dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
2045dfc646bSeric 	**  always send another copy later.
2055dfc646bSeric 	*/
2065dfc646bSeric 
2075dfc646bSeric 	tobuf[0] = '\0';
208588cad61Seric 	e->e_to = tobuf;
2096259796dSeric 	ctladdr = NULL;
210e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
2115dfc646bSeric 	for (; to != NULL; to = to->q_next)
2125dfc646bSeric 	{
2135dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
21457fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
2155dfc646bSeric 			break;
2165dfc646bSeric 
2175dfc646bSeric 		/* if already sent or not for this host, don't send */
218ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
219e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
220e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
2215dfc646bSeric 			continue;
2226259796dSeric 
2234b22ea87Seric 		/* avoid overflowing tobuf */
224aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
2254b22ea87Seric 			break;
2264b22ea87Seric 
2276ef48975Seric 		if (tTd(10, 1))
228772e6e50Seric 		{
229772e6e50Seric 			printf("\nsend to ");
230772e6e50Seric 			printaddr(to, FALSE);
231772e6e50Seric 		}
232772e6e50Seric 
2336259796dSeric 		/* compute effective uid/gid when sending */
2347da1035fSeric 		if (to->q_mailer == ProgMailer)
2356259796dSeric 			ctladdr = getctladdr(to);
2366259796dSeric 
2375dfc646bSeric 		user = to->q_user;
238588cad61Seric 		e->e_to = to->q_paddr;
23975f1ade9Seric 		if (tTd(10, 5))
24075f1ade9Seric 		{
24175f1ade9Seric 			printf("deliver: QDONTSEND ");
24275f1ade9Seric 			printaddr(to, FALSE);
24375f1ade9Seric 		}
244ee4b0922Seric 		to->q_flags |= QDONTSEND;
2455dfc646bSeric 
2465dfc646bSeric 		/*
2475dfc646bSeric 		**  Check to see that these people are allowed to
2485dfc646bSeric 		**  talk to each other.
2492a6e0786Seric 		*/
2502a6e0786Seric 
25169582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
25269582d2fSeric 		{
25369582d2fSeric 			NoReturn = TRUE;
25408b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
25581161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
25669582d2fSeric 			continue;
25769582d2fSeric 		}
258fabb3bd4Seric 		rcode = checkcompat(to, e);
2591793c9c5Seric 		if (rcode != EX_OK)
2605dfc646bSeric 		{
26181161401Seric 			giveresponse(rcode, m, NULL, e);
2625dfc646bSeric 			continue;
2635dfc646bSeric 		}
2642a6e0786Seric 
2652a6e0786Seric 		/*
2669ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
2679ec9501bSeric 		**	about them.
26825a99e2eSeric 		*/
26925a99e2eSeric 
27057fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
27125a99e2eSeric 		{
2721d8f1806Seric 			stripquotes(user);
2731d8f1806Seric 			stripquotes(host);
27425a99e2eSeric 		}
27525a99e2eSeric 
276cdb828c5Seric 		/* hack attack -- delivermail compatibility */
277cdb828c5Seric 		if (m == ProgMailer && *user == '|')
278cdb828c5Seric 			user++;
279cdb828c5Seric 
28025a99e2eSeric 		/*
2813efaed6eSeric 		**  If an error message has already been given, don't
2823efaed6eSeric 		**	bother to send to this address.
2833efaed6eSeric 		**
2843efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
2853efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
2863efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
2873efaed6eSeric 		*/
2883efaed6eSeric 
2896cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
2903efaed6eSeric 			continue;
2913efaed6eSeric 
292f2fec898Seric 		/* save statistics.... */
293588cad61Seric 		markstats(e, to);
294f2fec898Seric 
2953efaed6eSeric 		/*
29625a99e2eSeric 		**  See if this user name is "special".
29725a99e2eSeric 		**	If the user name has a slash in it, assume that this
29851552439Seric 		**	is a file -- send it off without further ado.  Note
29951552439Seric 		**	that this type of addresses is not processed along
30051552439Seric 		**	with the others, so we fudge on the To person.
30125a99e2eSeric 		*/
30225a99e2eSeric 
3032c017f8dSeric 		if (m == FileMailer)
30425a99e2eSeric 		{
305b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
30681161401Seric 			giveresponse(rcode, m, NULL, e);
307dde5acadSeric 			if (rcode == EX_OK)
308dde5acadSeric 				to->q_flags |= QSENT;
3095dfc646bSeric 			continue;
31025a99e2eSeric 		}
31125a99e2eSeric 
31213bbc08cSeric 		/*
31313bbc08cSeric 		**  Address is verified -- add this user to mailer
31413bbc08cSeric 		**  argv, and add it to the print list of recipients.
31513bbc08cSeric 		*/
31613bbc08cSeric 
317508daeccSeric 		/* link together the chain of recipients */
318508daeccSeric 		to->q_tchain = tochain;
319508daeccSeric 		tochain = to;
320508daeccSeric 
3215dfc646bSeric 		/* create list of users for error messages */
322db8841e9Seric 		(void) strcat(tobuf, ",");
323db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
324588cad61Seric 		define('u', user, e);		/* to user */
325588cad61Seric 		define('z', to->q_home, e);	/* user's home */
3265dfc646bSeric 
327c579ef51Seric 		/*
328508daeccSeric 		**  Expand out this user into argument list.
329c579ef51Seric 		*/
330c579ef51Seric 
331508daeccSeric 		if (!clever)
332c579ef51Seric 		{
333588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
3345dfc646bSeric 			*pvp++ = newstr(buf);
3355dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
3365dfc646bSeric 			{
3375dfc646bSeric 				/* allow some space for trailing parms */
3385dfc646bSeric 				break;
3395dfc646bSeric 			}
3405dfc646bSeric 		}
341c579ef51Seric 	}
3425dfc646bSeric 
343145b49b1Seric 	/* see if any addresses still exist */
344145b49b1Seric 	if (tobuf[0] == '\0')
345c579ef51Seric 	{
346588cad61Seric 		define('g', (char *) NULL, e);
347145b49b1Seric 		return (0);
348c579ef51Seric 	}
349145b49b1Seric 
3505dfc646bSeric 	/* print out messages as full list */
35163780dbdSeric 	e->e_to = tobuf + 1;
3525dfc646bSeric 
3535dfc646bSeric 	/*
3545dfc646bSeric 	**  Fill out any parameters after the $u parameter.
3555dfc646bSeric 	*/
3565dfc646bSeric 
357c579ef51Seric 	while (!clever && *++mvp != NULL)
3585dfc646bSeric 	{
359588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
3605dfc646bSeric 		*pvp++ = newstr(buf);
3615dfc646bSeric 		if (pvp >= &pv[MAXPV])
36208b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
3635dfc646bSeric 	}
3645dfc646bSeric 	*pvp++ = NULL;
3655dfc646bSeric 
36625a99e2eSeric 	/*
36725a99e2eSeric 	**  Call the mailer.
3686328bdf7Seric 	**	The argument vector gets built, pipes
36925a99e2eSeric 	**	are created as necessary, and we fork & exec as
3706328bdf7Seric 	**	appropriate.
371c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
37225a99e2eSeric 	*/
37325a99e2eSeric 
3747ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
37586b26461Seric 		ctladdr = &e->e_from;
376134746fbSeric #ifdef NAMED_BIND
3772bcc6d2dSeric 	if (ConfigLevel < 2)
378912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
379134746fbSeric #endif
380b31e7f2bSeric 	mci = openmailer(m, pv, ctladdr, clever, e);
381b31e7f2bSeric 	if (mci == NULL)
382134746fbSeric 	{
383b31e7f2bSeric 		/* catastrophic error */
384845e533cSeric 		rcode = EX_OSERR;
385b31e7f2bSeric 		goto give_up;
386b31e7f2bSeric 	}
387b31e7f2bSeric 	else if (mci->mci_state != MCIS_OPEN)
388b31e7f2bSeric 	{
389b31e7f2bSeric 		/* couldn't open the mailer */
390b31e7f2bSeric 		rcode = mci->mci_exitstat;
3912a6bc25bSeric 		errno = mci->mci_errno;
392b31e7f2bSeric 		if (rcode == EX_OK)
393b31e7f2bSeric 		{
394b31e7f2bSeric 			/* shouldn't happen */
39508b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
3966b0f339dSeric 				rcode, mci->mci_state, firstsig);
397b31e7f2bSeric 			rcode = EX_SOFTWARE;
398b31e7f2bSeric 		}
399b31e7f2bSeric 	}
400b31e7f2bSeric 	else if (!clever)
401b31e7f2bSeric 	{
402b31e7f2bSeric 		/*
403b31e7f2bSeric 		**  Format and send message.
404b31e7f2bSeric 		*/
40515d084d5Seric 
406b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
407b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
408b31e7f2bSeric 		putline("\n", mci->mci_out, m);
409b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
410b31e7f2bSeric 
411b31e7f2bSeric 		/* get the exit status */
412b31e7f2bSeric 		rcode = endmailer(mci, pv[0]);
413134746fbSeric 	}
414134746fbSeric 	else
415b31e7f2bSeric #ifdef SMTP
416134746fbSeric 	{
417b31e7f2bSeric 		/*
418b31e7f2bSeric 		**  Send the MAIL FROM: protocol
419b31e7f2bSeric 		*/
42015d084d5Seric 
421b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
422b31e7f2bSeric 		if (rcode == EX_OK)
42375889e88Seric 		{
424ded0d3daSkarels 			register char *t = tobuf;
425ded0d3daSkarels 			register int i;
426ded0d3daSkarels 
427588cad61Seric 			/* send the recipient list */
42863780dbdSeric 			tobuf[0] = '\0';
42975889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
43075889e88Seric 			{
43163780dbdSeric 				e->e_to = to->q_paddr;
43215d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
43375889e88Seric 				{
43483b7ddc9Seric 					markfailure(e, to, i);
43581161401Seric 					giveresponse(i, m, mci, e);
43663780dbdSeric 				}
43775889e88Seric 				else
43875889e88Seric 				{
439911693bfSbostic 					*t++ = ',';
440b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
441b31e7f2bSeric 						continue;
442588cad61Seric 				}
443588cad61Seric 			}
444588cad61Seric 
44563780dbdSeric 			/* now send the data */
44663780dbdSeric 			if (tobuf[0] == '\0')
447b31e7f2bSeric 			{
44863780dbdSeric 				e->e_to = NULL;
449b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
450b31e7f2bSeric 					smtprset(m, mci, e);
451b31e7f2bSeric 			}
45275889e88Seric 			else
45375889e88Seric 			{
45463780dbdSeric 				e->e_to = tobuf + 1;
45575889e88Seric 				rcode = smtpdata(m, mci, e);
45663780dbdSeric 			}
45763780dbdSeric 
45863780dbdSeric 			/* now close the connection */
459b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
46015d084d5Seric 				smtpquit(m, mci, e);
46163780dbdSeric 		}
462c579ef51Seric 	}
463b31e7f2bSeric #else /* not SMTP */
464a05b3449Sbostic 	{
46508b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
466845e533cSeric 		rcode = EX_CONFIG;
467b31e7f2bSeric 		goto give_up;
468a05b3449Sbostic 	}
469b31e7f2bSeric #endif /* SMTP */
470134746fbSeric #ifdef NAMED_BIND
4712bcc6d2dSeric 	if (ConfigLevel < 2)
472912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
473134746fbSeric #endif
4745dfc646bSeric 
475b31e7f2bSeric 	/* arrange a return receipt if requested */
4768c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
477b31e7f2bSeric 	{
478b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
479b31e7f2bSeric 		/* do we want to send back more info? */
480b31e7f2bSeric 	}
481b31e7f2bSeric 
482c77d1c25Seric 	/*
48363780dbdSeric 	**  Do final status disposal.
48463780dbdSeric 	**	We check for something in tobuf for the SMTP case.
485c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
486c77d1c25Seric 	**		addressees.
487c77d1c25Seric 	*/
488c77d1c25Seric 
489b31e7f2bSeric   give_up:
49063780dbdSeric 	if (tobuf[0] != '\0')
49181161401Seric 		giveresponse(rcode, m, mci, e);
492772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
493b31e7f2bSeric 	{
494dde5acadSeric 		if (rcode != EX_OK)
49583b7ddc9Seric 			markfailure(e, to, rcode);
496dde5acadSeric 		else
497655518ecSeric 		{
498dde5acadSeric 			to->q_flags |= QSENT;
499655518ecSeric 			e->e_nsent++;
500655518ecSeric 		}
501b31e7f2bSeric 	}
502b31e7f2bSeric 
503b31e7f2bSeric 	/*
504b31e7f2bSeric 	**  Restore state and return.
505b31e7f2bSeric 	*/
506c77d1c25Seric 
50735490626Seric 	errno = 0;
508588cad61Seric 	define('g', (char *) NULL, e);
5095826d9d3Seric 	return (rcode);
51025a99e2eSeric }
5115dfc646bSeric /*
51283b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
51383b7ddc9Seric **
51483b7ddc9Seric **	Parameters:
51583b7ddc9Seric **		e -- the envelope we are sending.
51683b7ddc9Seric **		q -- the address to mark.
51783b7ddc9Seric **		rcode -- the code signifying the particular failure.
51883b7ddc9Seric **
51983b7ddc9Seric **	Returns:
52083b7ddc9Seric **		none.
52183b7ddc9Seric **
52283b7ddc9Seric **	Side Effects:
52383b7ddc9Seric **		marks the address (and possibly the envelope) with the
52483b7ddc9Seric **			failure so that an error will be returned or
52583b7ddc9Seric **			the message will be queued, as appropriate.
52683b7ddc9Seric */
52783b7ddc9Seric 
52883b7ddc9Seric markfailure(e, q, rcode)
52983b7ddc9Seric 	register ENVELOPE *e;
53083b7ddc9Seric 	register ADDRESS *q;
53183b7ddc9Seric 	int rcode;
53283b7ddc9Seric {
53383b7ddc9Seric 	if (rcode == EX_OK)
53483b7ddc9Seric 		return;
535ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
53683b7ddc9Seric 		q->q_flags |= QBADADDR;
53783b7ddc9Seric 	else if (curtime() > e->e_ctime + TimeOut)
53883b7ddc9Seric 	{
53983b7ddc9Seric 		extern char *pintvl();
540198d9be0Seric 		char buf[MAXLINE];
54183b7ddc9Seric 
54283b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
543198d9be0Seric 		{
544198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
54583b7ddc9Seric 				pintvl(TimeOut, FALSE));
546198d9be0Seric 			if (e->e_message != NULL)
547198d9be0Seric 				free(e->e_message);
548198d9be0Seric 			e->e_message = newstr(buf);
54908b25121Seric 			message(buf);
550198d9be0Seric 		}
55183b7ddc9Seric 		q->q_flags |= QBADADDR;
55283b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
553c13b5dfcSeric 		fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr);
55483b7ddc9Seric 	}
55583b7ddc9Seric 	else
55683b7ddc9Seric 		q->q_flags |= QQUEUEUP;
55783b7ddc9Seric }
55883b7ddc9Seric /*
55932d19d43Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
56032d19d43Seric **
56132d19d43Seric **	This MUST be a macro, since after a vfork we are running
56232d19d43Seric **	two processes on the same stack!!!
56332d19d43Seric **
56432d19d43Seric **	Parameters:
56532d19d43Seric **		none.
56632d19d43Seric **
56732d19d43Seric **	Returns:
56832d19d43Seric **		From a macro???  You've got to be kidding!
56932d19d43Seric **
57032d19d43Seric **	Side Effects:
57132d19d43Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
57232d19d43Seric **			pid of child in parent, zero in child.
57332d19d43Seric **			-1 on unrecoverable error.
57432d19d43Seric **
57532d19d43Seric **	Notes:
57632d19d43Seric **		I'm awfully sorry this looks so awful.  That's
57732d19d43Seric **		vfork for you.....
57832d19d43Seric */
57932d19d43Seric 
58032d19d43Seric # define NFORKTRIES	5
58184f7cd1bSeric 
58284f7cd1bSeric # ifndef FORK
58384f7cd1bSeric # define FORK	fork
58484f7cd1bSeric # endif
58532d19d43Seric 
58632d19d43Seric # define DOFORK(fORKfN) \
58732d19d43Seric {\
58832d19d43Seric 	register int i;\
58932d19d43Seric \
59011799049Seric 	for (i = NFORKTRIES; --i >= 0; )\
59132d19d43Seric 	{\
59232d19d43Seric 		pid = fORKfN();\
59332d19d43Seric 		if (pid >= 0)\
59432d19d43Seric 			break;\
59511799049Seric 		if (i > 0)\
5966c4635f6Seric 			sleep((unsigned) NFORKTRIES - i);\
59732d19d43Seric 	}\
59832d19d43Seric }
59932d19d43Seric /*
6002ed72599Seric **  DOFORK -- simple fork interface to DOFORK.
6012ed72599Seric **
6022ed72599Seric **	Parameters:
6032ed72599Seric **		none.
6042ed72599Seric **
6052ed72599Seric **	Returns:
6062ed72599Seric **		pid of child in parent.
6072ed72599Seric **		zero in child.
6082ed72599Seric **		-1 on error.
6092ed72599Seric **
6102ed72599Seric **	Side Effects:
6112ed72599Seric **		returns twice, once in parent and once in child.
6122ed72599Seric */
6132ed72599Seric 
6142ed72599Seric dofork()
6152ed72599Seric {
6162ed72599Seric 	register int pid;
6172ed72599Seric 
6182ed72599Seric 	DOFORK(fork);
6192ed72599Seric 	return (pid);
6202ed72599Seric }
6212ed72599Seric /*
622c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
623c579ef51Seric **
624c579ef51Seric **	We should never get fatal errors (e.g., segmentation
625c579ef51Seric **	violation), so we report those specially.  For other
626c579ef51Seric **	errors, we choose a status message (into statmsg),
627c579ef51Seric **	and if it represents an error, we print it.
628c579ef51Seric **
629c579ef51Seric **	Parameters:
630c579ef51Seric **		pid -- pid of mailer.
631c579ef51Seric **		name -- name of mailer (for error messages).
632c579ef51Seric **
633c579ef51Seric **	Returns:
634c579ef51Seric **		exit code of mailer.
635c579ef51Seric **
636c579ef51Seric **	Side Effects:
637c579ef51Seric **		none.
638c579ef51Seric */
639c579ef51Seric 
64075889e88Seric endmailer(mci, name)
641b31e7f2bSeric 	register MCI *mci;
642c579ef51Seric 	char *name;
643c579ef51Seric {
644588cad61Seric 	int st;
645c579ef51Seric 
64675889e88Seric 	/* close any connections */
64775889e88Seric 	if (mci->mci_in != NULL)
648ee4b0922Seric 		(void) xfclose(mci->mci_in, name, "mci_in");
64975889e88Seric 	if (mci->mci_out != NULL)
650ee4b0922Seric 		(void) xfclose(mci->mci_out, name, "mci_out");
65175889e88Seric 	mci->mci_in = mci->mci_out = NULL;
65275889e88Seric 	mci->mci_state = MCIS_CLOSED;
65375889e88Seric 
65433db8731Seric 	/* in the IPC case there is nothing to wait for */
65575889e88Seric 	if (mci->mci_pid == 0)
65633db8731Seric 		return (EX_OK);
65733db8731Seric 
65833db8731Seric 	/* wait for the mailer process to die and collect status */
65975889e88Seric 	st = waitfor(mci->mci_pid);
660588cad61Seric 	if (st == -1)
66178de67c1Seric 	{
662588cad61Seric 		syserr("endmailer %s: wait", name);
663588cad61Seric 		return (EX_SOFTWARE);
664c579ef51Seric 	}
66533db8731Seric 
66633db8731Seric 	/* see if it died a horrid death */
667c579ef51Seric 	if ((st & 0377) != 0)
668c579ef51Seric 	{
6695f73204aSeric 		syserr("mailer %s died with signal %o", name, st);
6705f73204aSeric 		ExitStat = EX_TEMPFAIL;
6715f73204aSeric 		return (EX_TEMPFAIL);
672c579ef51Seric 	}
67333db8731Seric 
67433db8731Seric 	/* normal death -- return status */
675588cad61Seric 	st = (st >> 8) & 0377;
676588cad61Seric 	return (st);
677c579ef51Seric }
678c579ef51Seric /*
679c579ef51Seric **  OPENMAILER -- open connection to mailer.
680c579ef51Seric **
681c579ef51Seric **	Parameters:
682c579ef51Seric **		m -- mailer descriptor.
683c579ef51Seric **		pvp -- parameter vector to pass to mailer.
684c579ef51Seric **		ctladdr -- controlling address for user.
685c579ef51Seric **		clever -- create a full duplex connection.
686c579ef51Seric **
687c579ef51Seric **	Returns:
68875889e88Seric **		The mail connection info struct for this connection.
68975889e88Seric **		NULL on failure.
690c579ef51Seric **
691c579ef51Seric **	Side Effects:
692c579ef51Seric **		creates a mailer in a subprocess.
693c579ef51Seric */
694c579ef51Seric 
695b31e7f2bSeric MCI *
696b31e7f2bSeric openmailer(m, pvp, ctladdr, clever, e)
697588cad61Seric 	MAILER *m;
698c579ef51Seric 	char **pvp;
699c579ef51Seric 	ADDRESS *ctladdr;
700c579ef51Seric 	bool clever;
701b31e7f2bSeric 	ENVELOPE *e;
702c579ef51Seric {
7035dfc646bSeric 	int pid;
704b31e7f2bSeric 	register MCI *mci;
705f8952a83Seric 	int mpvect[2];
706c579ef51Seric 	int rpvect[2];
7075dfc646bSeric 	extern FILE *fdopen();
7085dfc646bSeric 
7096ef48975Seric 	if (tTd(11, 1))
7105dfc646bSeric 	{
7118c57e552Seric 		printf("openmailer:");
7125dfc646bSeric 		printav(pvp);
7135dfc646bSeric 	}
71435490626Seric 	errno = 0;
7155dfc646bSeric 
716ef66a9d0Seric 	CurHostName = m->m_mailer;
717ef66a9d0Seric 
71833db8731Seric 	/*
71933db8731Seric 	**  Deal with the special case of mail handled through an IPC
72033db8731Seric 	**  connection.
72133db8731Seric 	**	In this case we don't actually fork.  We must be
72233db8731Seric 	**	running SMTP for this to work.  We will return a
72333db8731Seric 	**	zero pid to indicate that we are running IPC.
724e7c1bd78Seric 	**  We also handle a debug version that just talks to stdin/out.
72533db8731Seric 	*/
72633db8731Seric 
727e7c1bd78Seric 	/* check for Local Person Communication -- not for mortals!!! */
728e7c1bd78Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
729e7c1bd78Seric 	{
730b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
7312a087e90Seric 		bzero((char *) mci, sizeof *mci);
73275889e88Seric 		mci->mci_in = stdin;
73375889e88Seric 		mci->mci_out = stdout;
734b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
73515d084d5Seric 		mci->mci_mailer = m;
736e7c1bd78Seric 	}
737b31e7f2bSeric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
738914346b1Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
73933db8731Seric 	{
74084f7cd1bSeric #ifdef DAEMON
741e103b48fSeric 		register int i;
7421277f9a8Seric 		register u_short port;
743e103b48fSeric 		char *curhost;
744b31e7f2bSeric 		extern MCI *mci_get();
745e103b48fSeric 		extern char *hostsignature();
74633db8731Seric 
747ef66a9d0Seric 		CurHostName = pvp[1];
748e103b48fSeric 		curhost = hostsignature(m, pvp[1], e);
749b31e7f2bSeric 
75078bfb85bSeric 		if (curhost == NULL || curhost[0] == '\0')
75178bfb85bSeric 		{
75278bfb85bSeric 			syserr("null signature");
75378bfb85bSeric 			return NULL;
75478bfb85bSeric 		}
75578bfb85bSeric 
75633db8731Seric 		if (!clever)
75778bfb85bSeric 		{
75808b25121Seric 			syserr("554 non-clever IPC");
75978bfb85bSeric 			return NULL;
76078bfb85bSeric 		}
76193b6e3cfSeric 		if (pvp[2] != NULL)
7621277f9a8Seric 			port = atoi(pvp[2]);
76393b6e3cfSeric 		else
7641277f9a8Seric 			port = 0;
765e103b48fSeric 		while (*curhost != '\0')
766ebc61751Sbloom 		{
767e103b48fSeric 			register char *p;
7687d55540cSeric 			static char hostbuf[MAXNAME];
769e103b48fSeric 
770e103b48fSeric 			/* pull the next host from the signature */
771e103b48fSeric 			p = strchr(curhost, ':');
772e103b48fSeric 			if (p == NULL)
773e103b48fSeric 				p = &curhost[strlen(curhost)];
774e103b48fSeric 			strncpy(hostbuf, curhost, p - curhost);
775e103b48fSeric 			hostbuf[p - curhost] = '\0';
776e103b48fSeric 			if (*p != '\0')
777e103b48fSeric 				p++;
778e103b48fSeric 			curhost = p;
779e103b48fSeric 
78045a8316eSeric 			/* see if we already know that this host is fried */
781e103b48fSeric 			CurHostName = hostbuf;
782e103b48fSeric 			mci = mci_get(hostbuf, m);
783b31e7f2bSeric 			if (mci->mci_state != MCIS_CLOSED)
7841fa7c2ebSeric 			{
7851fa7c2ebSeric 				if (tTd(11, 1))
7861fa7c2ebSeric 				{
7871fa7c2ebSeric 					printf("openmailer: ");
7881fa7c2ebSeric 					mci_dump(mci);
7891fa7c2ebSeric 				}
79067088a9dSeric 				CurHostName = mci->mci_host;
79175889e88Seric 				return mci;
7921fa7c2ebSeric 			}
79315d084d5Seric 			mci->mci_mailer = m;
794b31e7f2bSeric 			if (mci->mci_exitstat != EX_OK)
795b31e7f2bSeric 				continue;
796b31e7f2bSeric 
79775889e88Seric 			/* try the connection */
798e103b48fSeric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
79908b25121Seric 			message("Connecting to %s (%s)...",
800e103b48fSeric 				hostbuf, m->m_name);
801e103b48fSeric 			i = makeconnection(hostbuf, port, mci,
802914346b1Seric 				bitnset(M_SECURE_PORT, m->m_flags));
80375889e88Seric 			mci->mci_exitstat = i;
80475889e88Seric 			mci->mci_errno = errno;
80575889e88Seric 			if (i == EX_OK)
806b31e7f2bSeric 			{
807b31e7f2bSeric 				mci->mci_state = MCIS_OPENING;
808b31e7f2bSeric 				mci_cache(mci);
809b31e7f2bSeric 				break;
810b31e7f2bSeric 			}
811b31e7f2bSeric 			else if (tTd(11, 1))
812b31e7f2bSeric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
813b31e7f2bSeric 					i, errno);
814b31e7f2bSeric 
81575889e88Seric 
8165f73204aSeric 			/* enter status of this host */
81775889e88Seric 			setstat(i);
818ed854c7bSeric 		}
8192a087e90Seric 		mci->mci_pid = 0;
820b31e7f2bSeric #else /* no DAEMON */
82108b25121Seric 		syserr("554 openmailer: no IPC");
8221fa7c2ebSeric 		if (tTd(11, 1))
8231fa7c2ebSeric 			printf("openmailer: NULL\n");
82475889e88Seric 		return NULL;
825b31e7f2bSeric #endif /* DAEMON */
826588cad61Seric 	}
827b31e7f2bSeric 	else
828b31e7f2bSeric 	{
8296328bdf7Seric 		/* create a pipe to shove the mail through */
830f8952a83Seric 		if (pipe(mpvect) < 0)
83125a99e2eSeric 		{
832588cad61Seric 			syserr("openmailer: pipe (to mailer)");
8331fa7c2ebSeric 			if (tTd(11, 1))
8341fa7c2ebSeric 				printf("openmailer: NULL\n");
83575889e88Seric 			return NULL;
83625a99e2eSeric 		}
837c579ef51Seric 
838c579ef51Seric 		/* if this mailer speaks smtp, create a return pipe */
839c579ef51Seric 		if (clever && pipe(rpvect) < 0)
840c579ef51Seric 		{
841588cad61Seric 			syserr("openmailer: pipe (from mailer)");
842c579ef51Seric 			(void) close(mpvect[0]);
843c579ef51Seric 			(void) close(mpvect[1]);
8441fa7c2ebSeric 			if (tTd(11, 1))
8451fa7c2ebSeric 				printf("openmailer: NULL\n");
84675889e88Seric 			return NULL;
847c579ef51Seric 		}
848c579ef51Seric 
84933db8731Seric 		/*
85033db8731Seric 		**  Actually fork the mailer process.
85133db8731Seric 		**	DOFORK is clever about retrying.
8526984bfddSeric 		**
8536984bfddSeric 		**	Dispose of SIGCHLD signal catchers that may be laying
8546984bfddSeric 		**	around so that endmail will get it.
85533db8731Seric 		*/
85633db8731Seric 
857b31e7f2bSeric 		if (e->e_xfp != NULL)
858b31e7f2bSeric 			(void) fflush(e->e_xfp);		/* for debugging */
859588cad61Seric 		(void) fflush(stdout);
8606984bfddSeric # ifdef SIGCHLD
8616984bfddSeric 		(void) signal(SIGCHLD, SIG_DFL);
8626c2c3107Seric # endif /* SIGCHLD */
86384f7cd1bSeric 		DOFORK(FORK);
864f129ec7dSeric 		/* pid is set by DOFORK */
86525a99e2eSeric 		if (pid < 0)
86625a99e2eSeric 		{
86733db8731Seric 			/* failure */
868588cad61Seric 			syserr("openmailer: cannot fork");
869f8952a83Seric 			(void) close(mpvect[0]);
870f8952a83Seric 			(void) close(mpvect[1]);
871c579ef51Seric 			if (clever)
872c579ef51Seric 			{
873c579ef51Seric 				(void) close(rpvect[0]);
874c579ef51Seric 				(void) close(rpvect[1]);
875c579ef51Seric 			}
8761fa7c2ebSeric 			if (tTd(11, 1))
8771fa7c2ebSeric 				printf("openmailer: NULL\n");
87875889e88Seric 			return NULL;
87925a99e2eSeric 		}
88025a99e2eSeric 		else if (pid == 0)
88125a99e2eSeric 		{
88213088b9fSeric 			int i;
883dafbc479Seric 			int saveerrno;
88480c5307eSeric 			char **ep;
88580c5307eSeric 			char *env[MAXUSERENVIRON];
88680c5307eSeric 			extern char **environ;
8875f73204aSeric 			extern int DtableSize;
88813088b9fSeric 
88925a99e2eSeric 			/* child -- set up input & exec mailer */
89003ab8e55Seric 			/* make diagnostic output be standard output */
8918f0e7860Seric 			(void) signal(SIGINT, SIG_IGN);
8928f0e7860Seric 			(void) signal(SIGHUP, SIG_IGN);
8930984da9fSeric 			(void) signal(SIGTERM, SIG_DFL);
894f8952a83Seric 
8951caa9a59Seric 			/* close any other cached connections */
8961caa9a59Seric 			mci_flush(FALSE, mci);
8971caa9a59Seric 
898b31e7f2bSeric 			/* arrange to filter std & diag output of command */
899c579ef51Seric 			if (clever)
900c579ef51Seric 			{
901c579ef51Seric 				(void) close(rpvect[0]);
902c579ef51Seric 				(void) close(1);
903c579ef51Seric 				(void) dup(rpvect[1]);
904c579ef51Seric 				(void) close(rpvect[1]);
905c579ef51Seric 			}
906276723a8Seric 			else if (OpMode == MD_SMTP || HoldErrs)
907f8952a83Seric 			{
908588cad61Seric 				/* put mailer output in transcript */
909f8952a83Seric 				(void) close(1);
910b31e7f2bSeric 				(void) dup(fileno(e->e_xfp));
911f8952a83Seric 			}
912db8841e9Seric 			(void) close(2);
913db8841e9Seric 			(void) dup(1);
914f8952a83Seric 
915f8952a83Seric 			/* arrange to get standard input */
916f8952a83Seric 			(void) close(mpvect[1]);
917db8841e9Seric 			(void) close(0);
918f8952a83Seric 			if (dup(mpvect[0]) < 0)
91925a99e2eSeric 			{
92025a99e2eSeric 				syserr("Cannot dup to zero!");
921a590b978Seric 				_exit(EX_OSERR);
92225a99e2eSeric 			}
923f8952a83Seric 			(void) close(mpvect[0]);
92457fc6f17Seric 			if (!bitnset(M_RESTR, m->m_flags))
9250984da9fSeric 			{
92653e3fa05Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
927e36b99e2Seric 				{
928e36b99e2Seric 					(void) setgid(DefGid);
929898a126bSbostic 					(void) initgroups(DefUser, DefGid);
930e36b99e2Seric 					(void) setuid(DefUid);
931e36b99e2Seric 				}
932e36b99e2Seric 				else
93369f29479Seric 				{
934e36b99e2Seric 					(void) setgid(ctladdr->q_gid);
935898a126bSbostic 					(void) initgroups(ctladdr->q_ruser?
936898a126bSbostic 						ctladdr->q_ruser: ctladdr->q_user,
937898a126bSbostic 						ctladdr->q_gid);
938e36b99e2Seric 					(void) setuid(ctladdr->q_uid);
93969f29479Seric 				}
9400984da9fSeric 			}
941588cad61Seric 
94213088b9fSeric 			/* arrange for all the files to be closed */
943b31e7f2bSeric 			for (i = 3; i < DtableSize; i++)
944b31e7f2bSeric 			{
945fccb3f7aSbostic 				register int j;
946fccb3f7aSbostic 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
947fccb3f7aSbostic 					(void)fcntl(i, F_SETFD, j|1);
948fccb3f7aSbostic 			}
94933db8731Seric 
95080c5307eSeric 			/* set up the mailer environment */
95180c5307eSeric 			i = 0;
95280c5307eSeric 			env[i++] = "AGENT=sendmail";
95380c5307eSeric 			for (ep = environ; *ep != NULL; ep++)
95480c5307eSeric 			{
95580c5307eSeric 				if (strncmp(*ep, "TZ=", 3) == 0)
95680c5307eSeric 					env[i++] = *ep;
95780c5307eSeric 			}
95880c5307eSeric 			env[i++] = NULL;
95980c5307eSeric 
96033db8731Seric 			/* try to execute the mailer */
961d7a0480eSeric 			execve(m->m_mailer, pvp, env);
962dafbc479Seric 			saveerrno = errno;
96313088b9fSeric 			syserr("Cannot exec %s", m->m_mailer);
96495b76e4bSeric 			if (m == LocalMailer)
96555f33c03Seric 				_exit(EX_TEMPFAIL);
966e2de2524Seric 			if (transienterror(saveerrno))
96795b76e4bSeric 				_exit(EX_TEMPFAIL);
968a590b978Seric 			_exit(EX_UNAVAILABLE);
96925a99e2eSeric 		}
97025a99e2eSeric 
971f8952a83Seric 		/*
972c579ef51Seric 		**  Set up return value.
973f8952a83Seric 		*/
974f8952a83Seric 
975b31e7f2bSeric 		mci = (MCI *) xalloc(sizeof *mci);
9762a087e90Seric 		bzero((char *) mci, sizeof *mci);
97715d084d5Seric 		mci->mci_mailer = m;
978b31e7f2bSeric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9792a087e90Seric 		mci->mci_pid = pid;
980f8952a83Seric 		(void) close(mpvect[0]);
98175889e88Seric 		mci->mci_out = fdopen(mpvect[1], "w");
982c579ef51Seric 		if (clever)
98325a99e2eSeric 		{
984c579ef51Seric 			(void) close(rpvect[1]);
98575889e88Seric 			mci->mci_in = fdopen(rpvect[0], "r");
98675889e88Seric 		}
98775889e88Seric 		else
98875889e88Seric 		{
98975889e88Seric 			mci->mci_flags |= MCIF_TEMP;
99075889e88Seric 			mci->mci_in = NULL;
99175889e88Seric 		}
992b31e7f2bSeric 	}
993b31e7f2bSeric 
994b31e7f2bSeric 	/*
995b31e7f2bSeric 	**  If we are in SMTP opening state, send initial protocol.
996b31e7f2bSeric 	*/
997b31e7f2bSeric 
998b31e7f2bSeric 	if (clever && mci->mci_state != MCIS_CLOSED)
999b31e7f2bSeric 	{
1000b31e7f2bSeric 		smtpinit(m, mci, e);
1001b31e7f2bSeric 	}
10021fa7c2ebSeric 	if (tTd(11, 1))
10031fa7c2ebSeric 	{
10041fa7c2ebSeric 		printf("openmailer: ");
10051fa7c2ebSeric 		mci_dump(mci);
10061fa7c2ebSeric 	}
1007c579ef51Seric 
100875889e88Seric 	return mci;
100925a99e2eSeric }
101025a99e2eSeric /*
101125a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
101225a99e2eSeric **
101325a99e2eSeric **	Parameters:
101425a99e2eSeric **		stat -- the status code from the mailer (high byte
101525a99e2eSeric **			only; core dumps must have been taken care of
101625a99e2eSeric **			already).
101781161401Seric **		m -- the mailer info for this mailer.
101881161401Seric **		mci -- the mailer connection info -- can be NULL if the
101981161401Seric **			response is given before the connection is made.
102081161401Seric **		e -- the current envelope.
102125a99e2eSeric **
102225a99e2eSeric **	Returns:
1023db8841e9Seric **		none.
102425a99e2eSeric **
102525a99e2eSeric **	Side Effects:
1026c1f9df2cSeric **		Errors may be incremented.
102725a99e2eSeric **		ExitStat may be set.
102825a99e2eSeric */
102925a99e2eSeric 
103081161401Seric giveresponse(stat, m, mci, e)
103125a99e2eSeric 	int stat;
1032588cad61Seric 	register MAILER *m;
103381161401Seric 	register MCI *mci;
1034198d9be0Seric 	ENVELOPE *e;
103525a99e2eSeric {
103625a99e2eSeric 	register char *statmsg;
103725a99e2eSeric 	extern char *SysExMsg[];
103825a99e2eSeric 	register int i;
1039d4bd8f0eSbostic 	extern int N_SysEx;
1040d4bd8f0eSbostic #ifdef NAMED_BIND
1041d4bd8f0eSbostic 	extern int h_errno;
1042d4bd8f0eSbostic #endif
1043198d9be0Seric 	char buf[MAXLINE];
10447d55540cSeric 	extern char *errstring();
104525a99e2eSeric 
104613bbc08cSeric 	/*
104713bbc08cSeric 	**  Compute status message from code.
104813bbc08cSeric 	*/
104913bbc08cSeric 
105025a99e2eSeric 	i = stat - EX__BASE;
1051588cad61Seric 	if (stat == 0)
1052588cad61Seric 		statmsg = "250 Sent";
1053588cad61Seric 	else if (i < 0 || i > N_SysEx)
1054588cad61Seric 	{
1055588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1056588cad61Seric 		stat = EX_UNAVAILABLE;
1057588cad61Seric 		statmsg = buf;
1058588cad61Seric 	}
1059198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1060198d9be0Seric 	{
10617d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1062d4bd8f0eSbostic #ifdef NAMED_BIND
1063f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1064f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1065f28da541Smiriam 		else
1066d4bd8f0eSbostic #endif
1067f28da541Smiriam 		{
10688557d168Seric 			if (errno != 0)
1069d87e85f3Seric 				statmsg = errstring(errno);
1070d87e85f3Seric 			else
1071d87e85f3Seric 			{
1072d87e85f3Seric #ifdef SMTP
1073d87e85f3Seric 				extern char SmtpError[];
1074d87e85f3Seric 
1075d87e85f3Seric 				statmsg = SmtpError;
10766c2c3107Seric #else /* SMTP */
1077d87e85f3Seric 				statmsg = NULL;
10786c2c3107Seric #endif /* SMTP */
1079d87e85f3Seric 			}
1080f28da541Smiriam 		}
1081d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1082d87e85f3Seric 		{
108387c9b3e7Seric 			(void) strcat(buf, ": ");
1084d87e85f3Seric 			(void) strcat(buf, statmsg);
10858557d168Seric 		}
1086198d9be0Seric 		statmsg = buf;
1087198d9be0Seric 	}
108825a99e2eSeric 	else
1089d87e85f3Seric 	{
109025a99e2eSeric 		statmsg = SysExMsg[i];
10917d55540cSeric 		if (*statmsg++ == ':')
10927d55540cSeric 		{
10937d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
10947d55540cSeric 			statmsg = buf;
10957d55540cSeric 		}
1096d87e85f3Seric 	}
1097588cad61Seric 
1098588cad61Seric 	/*
1099588cad61Seric 	**  Print the message as appropriate
1100588cad61Seric 	*/
1101588cad61Seric 
1102198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1103e12d03eeSeric 		message(&statmsg[4], errstring(errno));
110425a99e2eSeric 	else
110525a99e2eSeric 	{
1106c1f9df2cSeric 		Errors++;
1107e12d03eeSeric 		usrerr(statmsg, errstring(errno));
110825a99e2eSeric 	}
110925a99e2eSeric 
111025a99e2eSeric 	/*
111125a99e2eSeric 	**  Final cleanup.
111225a99e2eSeric 	**	Log a record of the transaction.  Compute the new
111325a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
111425a99e2eSeric 	**	that.
111525a99e2eSeric 	*/
111625a99e2eSeric 
11172f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
111881161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1119eb238f8cSeric 
1120eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1121eb238f8cSeric 		setstat(stat);
1122198d9be0Seric 	if (stat != EX_OK)
1123198d9be0Seric 	{
1124198d9be0Seric 		if (e->e_message != NULL)
1125198d9be0Seric 			free(e->e_message);
1126198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1127198d9be0Seric 	}
11288557d168Seric 	errno = 0;
1129d4bd8f0eSbostic #ifdef NAMED_BIND
1130f28da541Smiriam 	h_errno = 0;
1131d4bd8f0eSbostic #endif
1132eb238f8cSeric }
1133eb238f8cSeric /*
1134eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1135eb238f8cSeric **
1136eb238f8cSeric **	Parameters:
113781161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
113881161401Seric **		mci -- the mailer connection info -- can be NULL if the
113981161401Seric **			log is occuring when no connection is active.
114081161401Seric **		stat -- the message to print for the status.
114181161401Seric **		e -- the current envelope.
1142eb238f8cSeric **
1143eb238f8cSeric **	Returns:
1144eb238f8cSeric **		none
1145eb238f8cSeric **
1146eb238f8cSeric **	Side Effects:
1147eb238f8cSeric **		none
1148eb238f8cSeric */
1149eb238f8cSeric 
115081161401Seric logdelivery(m, mci, stat, e)
115181161401Seric 	MAILER *m;
115281161401Seric 	register MCI *mci;
1153eb238f8cSeric 	char *stat;
1154b31e7f2bSeric 	register ENVELOPE *e;
11555cf56be3Seric {
1156eb238f8cSeric # ifdef LOG
11579507d1f9Seric 	char *curhost;
1158d6acf3eeSeric 	char buf[512];
11599507d1f9Seric 	extern char *pintvl();
11609507d1f9Seric 	extern char *macvalue();
11619507d1f9Seric 
116248ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
116381161401Seric 
116448ed5d33Seric 	if (m != NULL)
116571ff6caaSeric 	{
116648ed5d33Seric 		(void) strcat(buf, ", mailer=");
116748ed5d33Seric 		(void) strcat(buf, m->m_name);
116871ff6caaSeric 	}
116948ed5d33Seric 
117048ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
117171ff6caaSeric 	{
117271ff6caaSeric # ifdef DAEMON
117371ff6caaSeric 		extern struct sockaddr_in CurHostAddr;
117471ff6caaSeric 		extern char *inet_ntoa();
117548ed5d33Seric # endif
117671ff6caaSeric 
117748ed5d33Seric 		(void) strcat(buf, ", relay=");
117848ed5d33Seric 		(void) strcat(buf, mci->mci_host);
117948ed5d33Seric 
118048ed5d33Seric # ifdef DAEMON
118148ed5d33Seric 		(void) strcat(buf, " (");
118248ed5d33Seric 		(void) strcat(buf, inet_ntoa(CurHostAddr.sin_addr));
118348ed5d33Seric 		(void) strcat(buf, ")");
118471ff6caaSeric # endif
118571ff6caaSeric 	}
11869507d1f9Seric 	else
118748ed5d33Seric 	{
118848ed5d33Seric 		char *p = macvalue('h', e);
11899507d1f9Seric 
119048ed5d33Seric 		if (p != NULL && p[0] != '\0')
119148ed5d33Seric 		{
119248ed5d33Seric 			(void) strcat(buf, ", relay=");
119348ed5d33Seric 			(void) strcat(buf, p);
119448ed5d33Seric 		}
119548ed5d33Seric 	}
1196d6acf3eeSeric 
1197d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1198d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
11996c2c3107Seric # endif /* LOG */
120025a99e2eSeric }
120125a99e2eSeric /*
120251552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
120325a99e2eSeric **
120451552439Seric **	This can be made an arbitrary message separator by changing $l
120551552439Seric **
12069b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
12079b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
12089b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
12099b6c17a6Seric **	this kind of antique garbage????
121025a99e2eSeric **
121125a99e2eSeric **	Parameters:
121251552439Seric **		fp -- the file to output to.
121351552439Seric **		m -- the mailer describing this entry.
121425a99e2eSeric **
121525a99e2eSeric **	Returns:
121651552439Seric **		none
121725a99e2eSeric **
121825a99e2eSeric **	Side Effects:
121951552439Seric **		outputs some text to fp.
122025a99e2eSeric */
122125a99e2eSeric 
1222b31e7f2bSeric putfromline(fp, m, e)
122351552439Seric 	register FILE *fp;
122451552439Seric 	register MAILER *m;
1225b31e7f2bSeric 	ENVELOPE *e;
122625a99e2eSeric {
12272bc47524Seric 	char *template = "\201l\n";
122851552439Seric 	char buf[MAXLINE];
122925a99e2eSeric 
123057fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
123151552439Seric 		return;
123213bbc08cSeric 
12332c7e1b8dSeric # ifdef UGLYUUCP
123457fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
123574b6e67bSeric 	{
1236ea09d6edSeric 		char *bang;
1237ea09d6edSeric 		char xbuf[MAXLINE];
123874b6e67bSeric 
1239ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
12406c2c3107Seric 		bang = strchr(buf, '!');
124174b6e67bSeric 		if (bang == NULL)
124208b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
124374b6e67bSeric 		else
1244588cad61Seric 		{
1245ea09d6edSeric 			*bang++ = '\0';
12462bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1247ea09d6edSeric 			template = xbuf;
124874b6e67bSeric 		}
1249588cad61Seric 	}
12506c2c3107Seric # endif /* UGLYUUCP */
1251b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
125277b52738Seric 	putline(buf, fp, m);
1253bc6e2962Seric }
1254bc6e2962Seric /*
125551552439Seric **  PUTBODY -- put the body of a message.
125651552439Seric **
125751552439Seric **	Parameters:
125851552439Seric **		fp -- file to output onto.
125977b52738Seric **		m -- a mailer descriptor to control output format.
12609a6a5f55Seric **		e -- the envelope to put out.
126151552439Seric **
126251552439Seric **	Returns:
126351552439Seric **		none.
126451552439Seric **
126551552439Seric **	Side Effects:
126651552439Seric **		The message is written onto fp.
126751552439Seric */
126851552439Seric 
126977b52738Seric putbody(fp, m, e)
127051552439Seric 	FILE *fp;
1271588cad61Seric 	MAILER *m;
12729a6a5f55Seric 	register ENVELOPE *e;
127351552439Seric {
127477b52738Seric 	char buf[MAXLINE];
127551552439Seric 
127651552439Seric 	/*
127751552439Seric 	**  Output the body of the message
127851552439Seric 	*/
127951552439Seric 
12809a6a5f55Seric 	if (e->e_dfp == NULL)
128151552439Seric 	{
12829a6a5f55Seric 		if (e->e_df != NULL)
12839a6a5f55Seric 		{
12849a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
12859a6a5f55Seric 			if (e->e_dfp == NULL)
12868f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
12878f9146b0Srick 				e->e_df, e->e_to, e->e_from);
12889a6a5f55Seric 		}
12899a6a5f55Seric 		else
129077b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
12919a6a5f55Seric 	}
12929a6a5f55Seric 	if (e->e_dfp != NULL)
12939a6a5f55Seric 	{
12949a6a5f55Seric 		rewind(e->e_dfp);
129577b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
129624fc8aeeSeric 		{
129724fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1298d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
12993462ad9eSeric 				(void) putc('>', fp);
130077b52738Seric 			putline(buf, fp, m);
130124fc8aeeSeric 		}
130251552439Seric 
13039a6a5f55Seric 		if (ferror(e->e_dfp))
130451552439Seric 		{
130551552439Seric 			syserr("putbody: read error");
130651552439Seric 			ExitStat = EX_IOERR;
130751552439Seric 		}
130851552439Seric 	}
130951552439Seric 
131051552439Seric 	(void) fflush(fp);
131151552439Seric 	if (ferror(fp) && errno != EPIPE)
131251552439Seric 	{
131351552439Seric 		syserr("putbody: write error");
131451552439Seric 		ExitStat = EX_IOERR;
131551552439Seric 	}
131651552439Seric 	errno = 0;
131725a99e2eSeric }
131825a99e2eSeric /*
131925a99e2eSeric **  MAILFILE -- Send a message to a file.
132025a99e2eSeric **
1321f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1322f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1323f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1324f129ec7dSeric **	sendmail runs as root.
1325f129ec7dSeric **
1326588cad61Seric **	This could be done as a subordinate mailer, except that it
1327588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1328588cad61Seric **	view this as being sufficiently important as to include it
1329588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1330588cad61Seric **	to create another process plus some pipes to save the message.
1331588cad61Seric **
133225a99e2eSeric **	Parameters:
133325a99e2eSeric **		filename -- the name of the file to send to.
13346259796dSeric **		ctladdr -- the controlling address header -- includes
13356259796dSeric **			the userid/groupid to be when sending.
133625a99e2eSeric **
133725a99e2eSeric **	Returns:
133825a99e2eSeric **		The exit code associated with the operation.
133925a99e2eSeric **
134025a99e2eSeric **	Side Effects:
134125a99e2eSeric **		none.
134225a99e2eSeric */
134325a99e2eSeric 
1344b31e7f2bSeric mailfile(filename, ctladdr, e)
134525a99e2eSeric 	char *filename;
13466259796dSeric 	ADDRESS *ctladdr;
1347b31e7f2bSeric 	register ENVELOPE *e;
134825a99e2eSeric {
134925a99e2eSeric 	register FILE *f;
135032d19d43Seric 	register int pid;
135115d084d5Seric 	int mode;
135225a99e2eSeric 
135332d19d43Seric 	/*
135432d19d43Seric 	**  Fork so we can change permissions here.
135532d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
135632d19d43Seric 	**	the complications of calling subroutines, etc.
135732d19d43Seric 	*/
135832d19d43Seric 
135932d19d43Seric 	DOFORK(fork);
136032d19d43Seric 
136132d19d43Seric 	if (pid < 0)
136232d19d43Seric 		return (EX_OSERR);
136332d19d43Seric 	else if (pid == 0)
136432d19d43Seric 	{
136532d19d43Seric 		/* child -- actually write to file */
1366f129ec7dSeric 		struct stat stb;
1367f129ec7dSeric 
13680984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
13690984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
13700984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
13713462ad9eSeric 		(void) umask(OldUmask);
137295f16dc0Seric 
1373f129ec7dSeric 		if (stat(filename, &stb) < 0)
1374e6e1265fSeric 			stb.st_mode = 0666;
137515d084d5Seric 		mode = stb.st_mode;
137695f16dc0Seric 
137795f16dc0Seric 		/* limit the errors to those actually caused in the child */
137895f16dc0Seric 		errno = 0;
137995f16dc0Seric 		ExitStat = EX_OK;
138095f16dc0Seric 
1381f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1382f129ec7dSeric 			exit(EX_CANTCREAT);
138303827b5fSeric 		if (ctladdr == NULL)
13848f9146b0Srick 			ctladdr = &e->e_from;
138515d084d5Seric 		else
138615d084d5Seric 		{
138715d084d5Seric 			/* ignore setuid and setgid bits */
138815d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
138915d084d5Seric 		}
139015d084d5Seric 
13918f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
13928f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
13938f9146b0Srick 		{
13948f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
139595f16dc0Seric 			if (e->e_dfp == NULL)
139695f16dc0Seric 			{
13978f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
13988f9146b0Srick 					e->e_df, e->e_to, e->e_from);
13998f9146b0Srick 			}
14008f9146b0Srick 		}
14018f9146b0Srick 
140215d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1403e36b99e2Seric 		{
140495f16dc0Seric 			if (ctladdr->q_uid == 0)
140595f16dc0Seric 			{
1406e36b99e2Seric 				(void) setgid(DefGid);
1407898a126bSbostic 				(void) initgroups(DefUser, DefGid);
140895f16dc0Seric 			}
140995f16dc0Seric 			else
141095f16dc0Seric 			{
14116259796dSeric 				(void) setgid(ctladdr->q_gid);
1412898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1413898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1414898a126bSbostic 					ctladdr->q_gid);
1415898a126bSbostic 			}
1416e36b99e2Seric 		}
141715d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1418e36b99e2Seric 		{
1419e36b99e2Seric 			if (ctladdr->q_uid == 0)
1420e36b99e2Seric 				(void) setuid(DefUid);
1421e36b99e2Seric 			else
14226259796dSeric 				(void) setuid(ctladdr->q_uid);
1423e36b99e2Seric 		}
142495f16dc0Seric 		FileName = filename;
142595f16dc0Seric 		LineNumber = 0;
142627628d59Seric 		f = dfopen(filename, "a");
142725a99e2eSeric 		if (f == NULL)
142895f16dc0Seric 		{
142908b25121Seric 			message("554 cannot open");
143032d19d43Seric 			exit(EX_CANTCREAT);
143195f16dc0Seric 		}
143225a99e2eSeric 
1433b31e7f2bSeric 		putfromline(f, ProgMailer, e);
1434b843d759Seric 		(*e->e_puthdr)(f, ProgMailer, e);
143577b52738Seric 		putline("\n", f, ProgMailer);
1436b843d759Seric 		(*e->e_putbody)(f, ProgMailer, e);
143777b52738Seric 		putline("\n", f, ProgMailer);
143895f16dc0Seric 		if (ferror(f))
143995f16dc0Seric 		{
144008b25121Seric 			message("451 I/O error");
144195f16dc0Seric 			setstat(EX_IOERR);
144295f16dc0Seric 		}
1443ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
144432d19d43Seric 		(void) fflush(stdout);
1445e36b99e2Seric 
144627628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1447c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
144895f16dc0Seric 		exit(ExitStat);
144913bbc08cSeric 		/*NOTREACHED*/
145032d19d43Seric 	}
145132d19d43Seric 	else
145232d19d43Seric 	{
145332d19d43Seric 		/* parent -- wait for exit status */
1454588cad61Seric 		int st;
145532d19d43Seric 
1456588cad61Seric 		st = waitfor(pid);
1457588cad61Seric 		if ((st & 0377) != 0)
1458588cad61Seric 			return (EX_UNAVAILABLE);
1459588cad61Seric 		else
1460588cad61Seric 			return ((st >> 8) & 0377);
14618f9146b0Srick 		/*NOTREACHED*/
146232d19d43Seric 	}
146325a99e2eSeric }
1464ea4dc939Seric /*
1465ea4dc939Seric **  SENDALL -- actually send all the messages.
1466ea4dc939Seric **
1467ea4dc939Seric **	Parameters:
14680c52a0b3Seric **		e -- the envelope to send.
14697b95031aSeric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
14707b95031aSeric **			the current SendMode.
1471ea4dc939Seric **
1472ea4dc939Seric **	Returns:
1473ea4dc939Seric **		none.
1474ea4dc939Seric **
1475ea4dc939Seric **	Side Effects:
1476ea4dc939Seric **		Scans the send lists and sends everything it finds.
14770c52a0b3Seric **		Delivers any appropriate error messages.
1478276723a8Seric **		If we are running in a non-interactive mode, takes the
1479276723a8Seric **			appropriate action.
1480ea4dc939Seric */
1481ea4dc939Seric 
1482276723a8Seric sendall(e, mode)
14830c52a0b3Seric 	ENVELOPE *e;
1484276723a8Seric 	char mode;
1485ea4dc939Seric {
1486e77e673fSeric 	register ADDRESS *q;
14879836bea2Seric 	char *owner;
14889836bea2Seric 	int otherowners;
14899836bea2Seric 	ENVELOPE *splitenv = NULL;
1490ea4dc939Seric 
14917b95031aSeric 	/* determine actual delivery mode */
14927b95031aSeric 	if (mode == SM_DEFAULT)
14937b95031aSeric 	{
14945f73204aSeric 		extern bool shouldqueue();
14957b95031aSeric 
14967b95031aSeric 		mode = SendMode;
14973f324333Seric 		if (mode != SM_VERIFY &&
14983f324333Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
14993f324333Seric 			mode = SM_QUEUE;
15007b95031aSeric 	}
15017b95031aSeric 
1502df864a8fSeric 	if (tTd(13, 1))
1503772e6e50Seric 	{
1504ee4b0922Seric 		printf("\nSENDALL: mode %c, e_from ", mode);
1505ee4b0922Seric 		printaddr(&e->e_from, FALSE);
1506ee4b0922Seric 		printf("sendqueue:\n");
15070c52a0b3Seric 		printaddr(e->e_sendqueue, TRUE);
1508772e6e50Seric 	}
1509ea4dc939Seric 
15100c52a0b3Seric 	/*
1511276723a8Seric 	**  Do any preprocessing necessary for the mode we are running.
1512588cad61Seric 	**	Check to make sure the hop count is reasonable.
1513588cad61Seric 	**	Delete sends to the sender in mailing lists.
1514276723a8Seric 	*/
1515276723a8Seric 
1516588cad61Seric 	CurEnv = e;
1517276723a8Seric 
15181d57450bSeric 	if (e->e_hopcount > MaxHopCount)
1519276723a8Seric 	{
15208f9146b0Srick 		errno = 0;
152108b25121Seric 		syserr("554 too many hops %d (%d max): from %s, to %s",
1522655518ecSeric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
1523655518ecSeric 			e->e_sendqueue->q_paddr);
1524588cad61Seric 		return;
1525588cad61Seric 	}
1526588cad61Seric 
1527588cad61Seric 	if (!MeToo)
1528276723a8Seric 	{
1529f3d6c55cSeric 		extern ADDRESS *recipient();
1530f3d6c55cSeric 
153175f1ade9Seric 		if (tTd(13, 5))
153275f1ade9Seric 		{
153375f1ade9Seric 			printf("sendall: QDONTSEND ");
153475f1ade9Seric 			printaddr(&e->e_from, FALSE);
153575f1ade9Seric 		}
1536ee4b0922Seric 		e->e_from.q_flags |= QDONTSEND;
1537b843d759Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1538276723a8Seric 	}
1539588cad61Seric 
15409836bea2Seric 	/*
15419836bea2Seric 	**  Handle alias owners.
15429836bea2Seric 	**
15439836bea2Seric 	**	We scan up the q_alias chain looking for owners.
15449836bea2Seric 	**	We discard owners that are the same as the return path.
15459836bea2Seric 	*/
15469836bea2Seric 
15479836bea2Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15489836bea2Seric 	{
15499836bea2Seric 		register struct address *a;
15509836bea2Seric 
15519836bea2Seric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
15529836bea2Seric 			continue;
15539836bea2Seric 		if (a != NULL)
15549836bea2Seric 			q->q_owner = a->q_owner;
15559836bea2Seric 
1556ee4b0922Seric 		if (q->q_owner != NULL &&
1557ee4b0922Seric 		    !bitset(QDONTSEND, q->q_flags) &&
1558ee4b0922Seric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
15599836bea2Seric 			q->q_owner = NULL;
15609836bea2Seric 	}
15619836bea2Seric 
15629836bea2Seric 	owner = "";
15639836bea2Seric 	otherowners = 1;
15649836bea2Seric 	while (owner != NULL && otherowners > 0)
15659836bea2Seric 	{
15669836bea2Seric 		owner = NULL;
15679836bea2Seric 		otherowners = 0;
15689836bea2Seric 
15699836bea2Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15709836bea2Seric 		{
15719836bea2Seric 			if (bitset(QDONTSEND, q->q_flags))
15729836bea2Seric 				continue;
15739836bea2Seric 
15749836bea2Seric 			if (q->q_owner != NULL)
15759836bea2Seric 			{
15769836bea2Seric 				if (owner == NULL)
15779836bea2Seric 					owner = q->q_owner;
15789836bea2Seric 				else if (owner != q->q_owner)
15799836bea2Seric 				{
15809836bea2Seric 					if (strcmp(owner, q->q_owner) == 0)
15819836bea2Seric 					{
15829836bea2Seric 						/* make future comparisons cheap */
15839836bea2Seric 						q->q_owner = owner;
15849836bea2Seric 					}
15859836bea2Seric 					else
15869836bea2Seric 					{
15879836bea2Seric 						otherowners++;
15889836bea2Seric 					}
15899836bea2Seric 					owner = q->q_owner;
15909836bea2Seric 				}
15919836bea2Seric 			}
15929836bea2Seric 			else
15939836bea2Seric 			{
15949836bea2Seric 				otherowners++;
15959836bea2Seric 			}
15969836bea2Seric 		}
15979836bea2Seric 
15989836bea2Seric 		if (owner != NULL && otherowners > 0)
15999836bea2Seric 		{
16009836bea2Seric 			register ENVELOPE *ee;
16019836bea2Seric 			extern HDR *copyheader();
16029836bea2Seric 			extern ADDRESS *copyqueue();
16039836bea2Seric 
1604ee4b0922Seric 			/*
1605ee4b0922Seric 			**  Split this envelope into two.
1606ee4b0922Seric 			*/
1607ee4b0922Seric 
16089836bea2Seric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
1609ee4b0922Seric 			*ee = *e;
16109836bea2Seric 			ee->e_id = NULL;
1611ee4b0922Seric 			(void) queuename(ee, '\0');
1612ee4b0922Seric 
1613ee4b0922Seric 			if (tTd(13, 1))
1614ee4b0922Seric 				printf("sendall: split %s into %s\n",
1615ee4b0922Seric 					e->e_id, ee->e_id);
1616ee4b0922Seric 
16179836bea2Seric 			ee->e_header = copyheader(e->e_header);
16189836bea2Seric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
16199836bea2Seric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
16209836bea2Seric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE);
1621*579ef0ddSeric 			setsender(owner, ee, NULL, TRUE);
1622ee4b0922Seric 			if (tTd(13, 5))
1623ee4b0922Seric 			{
1624ee4b0922Seric 				printf("sendall(split): QDONTSEND ");
1625ee4b0922Seric 				printaddr(&ee->e_from, FALSE);
1626ee4b0922Seric 			}
1627ee4b0922Seric 			ee->e_from.q_flags |= QDONTSEND;
1628ee4b0922Seric 			ee->e_dfp = NULL;
1629ee4b0922Seric 			ee->e_xfp = NULL;
1630ee4b0922Seric 			ee->e_lockfp = NULL;
1631ee4b0922Seric 			ee->e_df = NULL;
16329836bea2Seric 			ee->e_sibling = splitenv;
16339836bea2Seric 			splitenv = ee;
16349836bea2Seric 
16359836bea2Seric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
16369836bea2Seric 				if (q->q_owner == owner)
16379836bea2Seric 					q->q_flags |= QDONTSEND;
16389836bea2Seric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
16399836bea2Seric 				if (q->q_owner != owner)
16409836bea2Seric 					q->q_flags |= QDONTSEND;
16419836bea2Seric 
16429836bea2Seric 			if (e->e_df != NULL && mode != SM_VERIFY)
16439836bea2Seric 			{
16449836bea2Seric 				ee->e_dfp = NULL;
1645239b3abaSeric 				ee->e_df = newstr(queuename(ee, 'd'));
16469836bea2Seric 				if (link(e->e_df, ee->e_df) < 0)
16479836bea2Seric 				{
16489836bea2Seric 					syserr("sendall: link(%s, %s)",
16499836bea2Seric 						e->e_df, ee->e_df);
16509836bea2Seric 				}
16519836bea2Seric 			}
1652ee4b0922Seric 
1653ee4b0922Seric 			if (mode != SM_VERIFY)
1654ee4b0922Seric 			{
1655ee4b0922Seric 				char xfbuf1[20], xfbuf2[20];
1656ee4b0922Seric 
1657ee4b0922Seric 				(void) strcpy(xfbuf1, queuename(e, 'x'));
1658ee4b0922Seric 				(void) strcpy(xfbuf2, queuename(ee, 'x'));
1659ee4b0922Seric 				if (link(xfbuf1, xfbuf2) < 0)
1660ee4b0922Seric 				{
1661ee4b0922Seric 					syserr("sendall: link(%s, %s)",
1662ee4b0922Seric 						xfbuf1, xfbuf2);
1663ee4b0922Seric 				}
1664ee4b0922Seric 			}
16659836bea2Seric 		}
16669836bea2Seric 	}
16679836bea2Seric 
16689836bea2Seric 	if (owner != NULL)
1669ee4b0922Seric 	{
1670*579ef0ddSeric 		setsender(owner, e, NULL, TRUE);
1671ee4b0922Seric 		if (tTd(13, 5))
1672ee4b0922Seric 		{
1673ee4b0922Seric 			printf("sendall(owner): QDONTSEND ");
1674ee4b0922Seric 			printaddr(&e->e_from, FALSE);
1675ee4b0922Seric 		}
1676ee4b0922Seric 		e->e_from.q_flags |= QDONTSEND;
1677ee4b0922Seric 	}
16789836bea2Seric 
1679588cad61Seric # ifdef QUEUE
1680b254bcb6Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
1681b254bcb6Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
1682b254bcb6Seric 	    !bitset(EF_INQUEUE, e->e_flags))
1683e020b127Seric 		queueup(e, TRUE, mode == SM_QUEUE);
16846c2c3107Seric #endif /* QUEUE */
1685276723a8Seric 
16869836bea2Seric 	if (splitenv != NULL)
16879836bea2Seric 	{
1688ee4b0922Seric 		register ENVELOPE *ee;
1689ee4b0922Seric 
16909836bea2Seric 		if (tTd(13, 1))
16919836bea2Seric 		{
16929836bea2Seric 			printf("\nsendall: Split queue; remaining queue:\n");
16939836bea2Seric 			printaddr(e->e_sendqueue, TRUE);
16949836bea2Seric 		}
16959836bea2Seric 
1696ee4b0922Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
16979836bea2Seric 		{
1698ee4b0922Seric 			CurEnv = ee;
1699ee4b0922Seric 			sendenvelope(ee, mode);
17009836bea2Seric 		}
17019836bea2Seric 
17029836bea2Seric 		CurEnv = e;
17039836bea2Seric 	}
1704ee4b0922Seric 	sendenvelope(e, mode);
1705ee4b0922Seric 
1706ee4b0922Seric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
1707ee4b0922Seric 		dropenvelope(splitenv);
1708ee4b0922Seric }
1709ee4b0922Seric 
1710ee4b0922Seric sendenvelope(e, mode)
1711ee4b0922Seric 	register ENVELOPE *e;
1712ee4b0922Seric 	char mode;
1713ee4b0922Seric {
1714ee4b0922Seric 	bool oldverbose;
1715ee4b0922Seric 	int pid;
1716ee4b0922Seric 	register ADDRESS *q;
17175ff3c5eaSeric #ifdef LOCKF
17185ff3c5eaSeric 	struct flock lfd;
17195ff3c5eaSeric #endif
17209836bea2Seric 
1721276723a8Seric 	oldverbose = Verbose;
1722276723a8Seric 	switch (mode)
1723276723a8Seric 	{
1724276723a8Seric 	  case SM_VERIFY:
1725276723a8Seric 		Verbose = TRUE;
1726276723a8Seric 		break;
1727276723a8Seric 
1728276723a8Seric 	  case SM_QUEUE:
1729c7f5410dSeric   queueonly:
1730b254bcb6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
1731276723a8Seric 		return;
1732276723a8Seric 
1733276723a8Seric 	  case SM_FORK:
17349a6a5f55Seric 		if (e->e_xfp != NULL)
17359a6a5f55Seric 			(void) fflush(e->e_xfp);
1736c7f5410dSeric 
1737c7f5410dSeric # ifdef LOCKF
1738c7f5410dSeric 		/*
1739c7f5410dSeric 		**  Since lockf has the interesting semantic that the
17401c265a00Seric 		**  lock is lost when we fork, we have to risk losing
17411c265a00Seric 		**  the lock here by closing before the fork, and then
17421c265a00Seric 		**  trying to get it back in the child.
1743c7f5410dSeric 		*/
1744c7f5410dSeric 
1745e020b127Seric 		if (e->e_lockfp != NULL)
1746c7f5410dSeric 		{
1747ee4b0922Seric 			(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
1748e020b127Seric 			e->e_lockfp = NULL;
1749c7f5410dSeric 		}
1750c7f5410dSeric # endif /* LOCKF */
1751c7f5410dSeric 
1752276723a8Seric 		pid = fork();
1753276723a8Seric 		if (pid < 0)
1754276723a8Seric 		{
1755c7f5410dSeric 			goto queueonly;
1756276723a8Seric 		}
1757276723a8Seric 		else if (pid > 0)
1758a6fce3d8Seric 		{
1759a6fce3d8Seric 			/* be sure we leave the temp files to our child */
1760b254bcb6Seric 			e->e_id = e->e_df = NULL;
1761c7f5410dSeric # ifndef LOCKF
1762e020b127Seric 			if (e->e_lockfp != NULL)
176375f1ade9Seric 			{
1764ee4b0922Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
176575f1ade9Seric 				e->e_lockfp = NULL;
176675f1ade9Seric 			}
1767c7f5410dSeric # endif
176875f1ade9Seric 
176975f1ade9Seric 			/* close any random open files in the envelope */
177075f1ade9Seric 			if (e->e_dfp != NULL)
177175f1ade9Seric 			{
1772ee4b0922Seric 				(void) xfclose(e->e_dfp, "sendenvelope", "dfp");
177375f1ade9Seric 				e->e_dfp = NULL;
177475f1ade9Seric 			}
177575f1ade9Seric 			if (e->e_xfp != NULL)
177675f1ade9Seric 			{
1777ee4b0922Seric 				(void) xfclose(e->e_xfp, "sendenvelope", "xfp");
177875f1ade9Seric 				e->e_xfp = NULL;
177975f1ade9Seric 			}
1780276723a8Seric 			return;
1781a6fce3d8Seric 		}
1782276723a8Seric 
1783276723a8Seric 		/* double fork to avoid zombies */
1784276723a8Seric 		if (fork() > 0)
1785276723a8Seric 			exit(EX_OK);
1786276723a8Seric 
1787a6fce3d8Seric 		/* be sure we are immune from the terminal */
17888dadacffSeric 		disconnect(FALSE, e);
1789a6fce3d8Seric 
1790e6720d51Seric # ifdef LOCKF
1791e6720d51Seric 		/*
1792c7f5410dSeric 		**  Now try to get our lock back.
1793e6720d51Seric 		*/
1794e6720d51Seric 
17951c265a00Seric 		lfd.l_type = F_WRLCK;
17961c265a00Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
1797e020b127Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
1798e020b127Seric 		if (e->e_lockfp == NULL ||
17991c265a00Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
1800e6720d51Seric 		{
1801e6720d51Seric 			/* oops....  lost it */
1802e6720d51Seric # ifdef LOG
18032f624c86Seric 			if (LogLevel > 29)
1804c7f5410dSeric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
1805b31e7f2bSeric 					e->e_id);
1806e6720d51Seric # endif /* LOG */
1807e6720d51Seric 			exit(EX_OK);
1808e6720d51Seric 		}
1809e6720d51Seric # endif /* LOCKF */
1810e6720d51Seric 
18111caa9a59Seric 		/*
18121caa9a59Seric 		**  Close any cached connections.
18131caa9a59Seric 		**
18141caa9a59Seric 		**	We don't send the QUIT protocol because the parent
18151caa9a59Seric 		**	still knows about the connection.
18161caa9a59Seric 		**
18171caa9a59Seric 		**	This should only happen when delivering an error
18181caa9a59Seric 		**	message.
18191caa9a59Seric 		*/
18201caa9a59Seric 
18211caa9a59Seric 		mci_flush(FALSE, NULL);
18221caa9a59Seric 
1823276723a8Seric 		break;
1824276723a8Seric 	}
1825276723a8Seric 
1826276723a8Seric 	/*
18270c52a0b3Seric 	**  Run through the list and send everything.
18280c52a0b3Seric 	*/
18290c52a0b3Seric 
1830655518ecSeric 	e->e_nsent = 0;
18310c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1832ea4dc939Seric 	{
1833276723a8Seric 		if (mode == SM_VERIFY)
1834ea4dc939Seric 		{
1835a6fce3d8Seric 			e->e_to = q->q_paddr;
1836e77e673fSeric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
183708b25121Seric 				message("deliverable");
1838ea4dc939Seric 		}
1839ee4b0922Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1840dde5acadSeric 		{
1841de1179f5Seric # ifdef QUEUE
1842dde5acadSeric 			/*
1843dde5acadSeric 			**  Checkpoint the send list every few addresses
1844dde5acadSeric 			*/
1845dde5acadSeric 
1846655518ecSeric 			if (e->e_nsent >= CheckpointInterval)
1847dde5acadSeric 			{
1848e020b127Seric 				queueup(e, TRUE, FALSE);
1849655518ecSeric 				e->e_nsent = 0;
1850dde5acadSeric 			}
1851de1179f5Seric # endif /* QUEUE */
1852655518ecSeric 			(void) deliver(e, q);
1853dde5acadSeric 		}
1854ea4dc939Seric 	}
185514a8ed7aSeric 	Verbose = oldverbose;
18560c52a0b3Seric 
18570c52a0b3Seric 	/*
18580c52a0b3Seric 	**  Now run through and check for errors.
18590c52a0b3Seric 	*/
18600c52a0b3Seric 
1861e020b127Seric 	if (mode == SM_VERIFY)
1862ee4b0922Seric 	{
18630c52a0b3Seric 		return;
1864ee4b0922Seric 	}
18650c52a0b3Seric 
18660c52a0b3Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
18670c52a0b3Seric 	{
1868df864a8fSeric 		if (tTd(13, 3))
1869df864a8fSeric 		{
1870df864a8fSeric 			printf("Checking ");
1871df864a8fSeric 			printaddr(q, FALSE);
1872df864a8fSeric 		}
1873df864a8fSeric 
1874b254bcb6Seric 		/* only send errors if the message failed */
1875b254bcb6Seric 		if (!bitset(QBADADDR, q->q_flags))
1876b254bcb6Seric 			continue;
18770c52a0b3Seric 
1878ee4b0922Seric 		e->e_flags |= EF_FATALERRS;
1879ee4b0922Seric 
1880b8043ddbSeric 		if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0)
18819836bea2Seric 			(void) sendtolist(e->e_from.q_paddr, NULL,
18821caa9a59Seric 					  &e->e_errorqueue, e);
18830c52a0b3Seric 	}
1884276723a8Seric 
1885276723a8Seric 	if (mode == SM_FORK)
1886276723a8Seric 		finis();
18870c52a0b3Seric }
1888e103b48fSeric /*
1889e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1890e103b48fSeric **
1891e103b48fSeric **	The signature describes how we are going to send this -- it
1892e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1893e103b48fSeric **	an ordered list of MX hosts.
1894e103b48fSeric **
1895e103b48fSeric **	Parameters:
1896e103b48fSeric **		m -- the mailer describing this host.
1897e103b48fSeric **		host -- the host name.
1898e103b48fSeric **		e -- the current envelope.
1899e103b48fSeric **
1900e103b48fSeric **	Returns:
1901e103b48fSeric **		The signature for this host.
1902e103b48fSeric **
1903e103b48fSeric **	Side Effects:
1904e103b48fSeric **		Can tweak the symbol table.
1905e103b48fSeric */
1906e103b48fSeric 
1907e103b48fSeric char *
1908e103b48fSeric hostsignature(m, host, e)
1909e103b48fSeric 	register MAILER *m;
1910e103b48fSeric 	char *host;
1911e103b48fSeric 	ENVELOPE *e;
1912e103b48fSeric {
1913e103b48fSeric 	register char *p;
1914e103b48fSeric 	register STAB *s;
1915e103b48fSeric 	int i;
1916e103b48fSeric 	int len;
1917e103b48fSeric #ifdef NAMED_BIND
1918e103b48fSeric 	int nmx;
1919e103b48fSeric 	auto int rcode;
1920e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
1921e103b48fSeric 	static char myhostbuf[MAXNAME];
1922e103b48fSeric #endif
1923e103b48fSeric 
1924e103b48fSeric 	/*
1925e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
1926e103b48fSeric 	*/
1927e103b48fSeric 
1928e103b48fSeric 	p = m->m_mailer;
1929e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
1930e103b48fSeric 	{
1931e103b48fSeric 		/* just an ordinary mailer */
1932e103b48fSeric 		return host;
1933e103b48fSeric 	}
1934e103b48fSeric 
1935e103b48fSeric 	/*
1936e103b48fSeric 	**  If it is a numeric address, just return it.
1937e103b48fSeric 	*/
1938e103b48fSeric 
1939e103b48fSeric 	if (host[0] == '[')
1940e103b48fSeric 		return host;
1941e103b48fSeric 
1942e103b48fSeric 	/*
1943e103b48fSeric 	**  Look it up in the symbol table.
1944e103b48fSeric 	*/
1945e103b48fSeric 
1946e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
1947e103b48fSeric 	if (s->s_hostsig != NULL)
1948e103b48fSeric 		return s->s_hostsig;
1949e103b48fSeric 
1950e103b48fSeric 	/*
1951e103b48fSeric 	**  Not already there -- create a signature.
1952e103b48fSeric 	*/
1953e103b48fSeric 
1954e103b48fSeric #ifdef NAMED_BIND
1955e103b48fSeric 	if (myhostbuf[0] == '\0')
19562bc47524Seric 		expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e);
1957e103b48fSeric 
1958e103b48fSeric 	nmx = getmxrr(host, mxhosts, myhostbuf, &rcode);
19597d55540cSeric 
1960e103b48fSeric 	if (nmx <= 0)
1961e103b48fSeric 	{
1962e103b48fSeric 		register MCI *mci;
1963e103b48fSeric 		extern int errno;
1964e103b48fSeric 		extern MCI *mci_get();
1965e103b48fSeric 
1966e103b48fSeric 		/* update the connection info for this host */
1967e103b48fSeric 		mci = mci_get(host, m);
1968e103b48fSeric 		mci->mci_exitstat = rcode;
1969e103b48fSeric 		mci->mci_errno = errno;
1970e103b48fSeric 
1971e103b48fSeric 		/* and return the original host name as the signature */
1972e103b48fSeric 		s->s_hostsig = host;
1973e103b48fSeric 		return host;
1974e103b48fSeric 	}
1975e103b48fSeric 
1976e103b48fSeric 	len = 0;
1977e103b48fSeric 	for (i = 0; i < nmx; i++)
1978e103b48fSeric 	{
1979e103b48fSeric 		len += strlen(mxhosts[i]) + 1;
1980e103b48fSeric 	}
1981e103b48fSeric 	s->s_hostsig = p = xalloc(len);
1982e103b48fSeric 	for (i = 0; i < nmx; i++)
1983e103b48fSeric 	{
1984e103b48fSeric 		if (i != 0)
1985e103b48fSeric 			*p++ = ':';
1986e103b48fSeric 		strcpy(p, mxhosts[i]);
1987e103b48fSeric 		p += strlen(p);
1988e103b48fSeric 	}
1989e103b48fSeric 	makelower(s->s_hostsig);
1990e103b48fSeric #else
1991e103b48fSeric 	/* not using BIND -- the signature is just the host name */
1992e103b48fSeric 	s->s_hostsig = host;
1993e103b48fSeric #endif
1994e103b48fSeric 	if (tTd(17, 1))
1995e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
1996e103b48fSeric 	return s->s_hostsig;
1997e103b48fSeric }
1998