14227346bSdist /*
20942ea6aSbostic  * Copyright (c) 1983 Eric P. Allman
32b191fa3Sbostic  * Copyright (c) 1988, 1993
42b191fa3Sbostic  *	The Regents of the University of California.  All rights reserved.
5e70a7521Sbostic  *
63bc94712Sbostic  * %sccs.include.redist.c%
74227346bSdist  */
84227346bSdist 
94227346bSdist #ifndef lint
10*b056abd0Seric static char sccsid[] = "@(#)deliver.c	8.68 (Berkeley) 02/06/94";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14f28da541Smiriam #include <netdb.h>
15911693bfSbostic #include <errno.h>
16134746fbSeric #ifdef NAMED_BIND
17912a731aSbostic #include <arpa/nameser.h>
18912a731aSbostic #include <resolv.h>
19f170942cSeric 
20f170942cSeric extern int	h_errno;
21134746fbSeric #endif
2225a99e2eSeric 
235d437c4dSeric extern char	SmtpError[];
245d437c4dSeric 
2525a99e2eSeric /*
269c9e68d9Seric **  SENDALL -- actually send all the messages.
279c9e68d9Seric **
289c9e68d9Seric **	Parameters:
299c9e68d9Seric **		e -- the envelope to send.
309c9e68d9Seric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
319c9e68d9Seric **			the current e->e_sendmode.
329c9e68d9Seric **
339c9e68d9Seric **	Returns:
349c9e68d9Seric **		none.
359c9e68d9Seric **
369c9e68d9Seric **	Side Effects:
379c9e68d9Seric **		Scans the send lists and sends everything it finds.
389c9e68d9Seric **		Delivers any appropriate error messages.
399c9e68d9Seric **		If we are running in a non-interactive mode, takes the
409c9e68d9Seric **			appropriate action.
419c9e68d9Seric */
429c9e68d9Seric 
439c9e68d9Seric sendall(e, mode)
449c9e68d9Seric 	ENVELOPE *e;
459c9e68d9Seric 	char mode;
469c9e68d9Seric {
479c9e68d9Seric 	register ADDRESS *q;
489c9e68d9Seric 	char *owner;
499c9e68d9Seric 	int otherowners;
50*b056abd0Seric 	register ENVELOPE *ee;
51*b056abd0Seric 	ENVELOPE *splitenv = NULL;
526103d9b4Seric 	bool announcequeueup;
539c9e68d9Seric 
547880f3e4Seric 	/*
557880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
567880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
577880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
587880f3e4Seric 	**  addresses to be sent.
597880f3e4Seric 	*/
607880f3e4Seric 
618d19a23dSeric 	if (bitset(EF_FATALERRS, e->e_flags) &&
628d19a23dSeric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
63896b16f6Seric 	{
64896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
65896b16f6Seric 		return;
66896b16f6Seric 	}
67896b16f6Seric 
689c9e68d9Seric 	/* determine actual delivery mode */
69fbe2f963Seric 	CurrentLA = getla();
709c9e68d9Seric 	if (mode == SM_DEFAULT)
719c9e68d9Seric 	{
729c9e68d9Seric 		mode = e->e_sendmode;
739c9e68d9Seric 		if (mode != SM_VERIFY &&
749c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
759c9e68d9Seric 			mode = SM_QUEUE;
766103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
779c9e68d9Seric 	}
786103d9b4Seric 	else
796103d9b4Seric 		announcequeueup = FALSE;
809c9e68d9Seric 
819c9e68d9Seric 	if (tTd(13, 1))
829c9e68d9Seric 	{
830e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
840e484fe5Seric 			mode, e->e_id);
859c9e68d9Seric 		printaddr(&e->e_from, FALSE);
869c9e68d9Seric 		printf("sendqueue:\n");
879c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
889c9e68d9Seric 	}
899c9e68d9Seric 
909c9e68d9Seric 	/*
919c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
929c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
939c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
949c9e68d9Seric 	*/
959c9e68d9Seric 
969c9e68d9Seric 	CurEnv = e;
979c9e68d9Seric 
989c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
999c9e68d9Seric 	{
1009c9e68d9Seric 		errno = 0;
1014bb8b0fdSeric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
1029c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
103f7869e68Seric 			RealHostName == NULL ? "localhost" : RealHostName,
104f7869e68Seric 			e->e_sendqueue->q_paddr);
1059c9e68d9Seric 		return;
1069c9e68d9Seric 	}
1079c9e68d9Seric 
108b8004690Seric 	/*
109b8004690Seric 	**  Do sender deletion.
110b8004690Seric 	**
111b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
112b8004690Seric 	**	This can happen if the name server is hosed when you
113b8004690Seric 	**	are trying to send mail.  The result is that the sender
114b8004690Seric 	**	is instantiated in the queue as a recipient.
115b8004690Seric 	*/
116b8004690Seric 
117e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
118e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1199c9e68d9Seric 	{
1209c9e68d9Seric 		if (tTd(13, 5))
1219c9e68d9Seric 		{
1229c9e68d9Seric 			printf("sendall: QDONTSEND ");
1239c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1249c9e68d9Seric 		}
1259c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1269c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1279c9e68d9Seric 	}
1289c9e68d9Seric 
129ce5531bdSeric 	/*
130ce5531bdSeric 	**  Handle alias owners.
131ce5531bdSeric 	**
132ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
133ce5531bdSeric 	**	We discard owners that are the same as the return path.
134ce5531bdSeric 	*/
135ce5531bdSeric 
136ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
137ce5531bdSeric 	{
138ce5531bdSeric 		register struct address *a;
139ce5531bdSeric 
140ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
141ce5531bdSeric 			continue;
142ce5531bdSeric 		if (a != NULL)
143ce5531bdSeric 			q->q_owner = a->q_owner;
144ce5531bdSeric 
145ce5531bdSeric 		if (q->q_owner != NULL &&
146ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
147ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
148ce5531bdSeric 			q->q_owner = NULL;
149ce5531bdSeric 	}
150ce5531bdSeric 
151ce5531bdSeric 	owner = "";
152ce5531bdSeric 	otherowners = 1;
153ce5531bdSeric 	while (owner != NULL && otherowners > 0)
154ce5531bdSeric 	{
155ce5531bdSeric 		owner = NULL;
156ce5531bdSeric 		otherowners = 0;
157ce5531bdSeric 
158ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
159ce5531bdSeric 		{
160ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
161ce5531bdSeric 				continue;
162ce5531bdSeric 
163ce5531bdSeric 			if (q->q_owner != NULL)
164ce5531bdSeric 			{
165ce5531bdSeric 				if (owner == NULL)
166ce5531bdSeric 					owner = q->q_owner;
167ce5531bdSeric 				else if (owner != q->q_owner)
168ce5531bdSeric 				{
169ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
170ce5531bdSeric 					{
171ce5531bdSeric 						/* make future comparisons cheap */
172ce5531bdSeric 						q->q_owner = owner;
173ce5531bdSeric 					}
174ce5531bdSeric 					else
175ce5531bdSeric 					{
176ce5531bdSeric 						otherowners++;
177ce5531bdSeric 					}
178ce5531bdSeric 					owner = q->q_owner;
179ce5531bdSeric 				}
180ce5531bdSeric 			}
181ce5531bdSeric 			else
182ce5531bdSeric 			{
183ce5531bdSeric 				otherowners++;
184ce5531bdSeric 			}
185ce5531bdSeric 		}
186ce5531bdSeric 
187ce5531bdSeric 		if (owner != NULL && otherowners > 0)
188ce5531bdSeric 		{
189ce5531bdSeric 			extern HDR *copyheader();
190ce5531bdSeric 			extern ADDRESS *copyqueue();
191ce5531bdSeric 
192ce5531bdSeric 			/*
193ce5531bdSeric 			**  Split this envelope into two.
194ce5531bdSeric 			*/
195ce5531bdSeric 
196ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
197ce5531bdSeric 			*ee = *e;
198ce5531bdSeric 			ee->e_id = NULL;
199ce5531bdSeric 			(void) queuename(ee, '\0');
200ce5531bdSeric 
201ce5531bdSeric 			if (tTd(13, 1))
202ce5531bdSeric 				printf("sendall: split %s into %s\n",
203ce5531bdSeric 					e->e_id, ee->e_id);
204ce5531bdSeric 
205ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
206ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
207ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
208ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
209ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
210ce5531bdSeric 			if (tTd(13, 5))
211ce5531bdSeric 			{
212ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
213ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
214ce5531bdSeric 			}
215ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
216ce5531bdSeric 			ee->e_dfp = NULL;
217ce5531bdSeric 			ee->e_xfp = NULL;
218ce5531bdSeric 			ee->e_df = NULL;
219ce5531bdSeric 			ee->e_errormode = EM_MAIL;
220*b056abd0Seric 			ee->e_sibling = splitenv;
221*b056abd0Seric 			splitenv = ee;
222ce5531bdSeric 
223ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
224ce5531bdSeric 				if (q->q_owner == owner)
225ce5531bdSeric 					q->q_flags |= QDONTSEND;
226ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
227ce5531bdSeric 				if (q->q_owner != owner)
228ce5531bdSeric 					q->q_flags |= QDONTSEND;
229ce5531bdSeric 
230ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
231ce5531bdSeric 			{
232ce5531bdSeric 				ee->e_dfp = NULL;
233da662164Seric 				ee->e_df = queuename(ee, 'd');
234da662164Seric 				ee->e_df = newstr(ee->e_df);
235ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
236ce5531bdSeric 				{
237ce5531bdSeric 					syserr("sendall: link(%s, %s)",
238ce5531bdSeric 						e->e_df, ee->e_df);
239ce5531bdSeric 				}
240ce5531bdSeric 			}
241ce5531bdSeric 
242ce5531bdSeric 			if (mode != SM_VERIFY)
243ce5531bdSeric 				openxscript(ee);
244ce5531bdSeric #ifdef LOG
245ce5531bdSeric 			if (LogLevel > 4)
246ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
247ce5531bdSeric 					ee->e_id, e->e_id);
248ce5531bdSeric #endif
249ce5531bdSeric 		}
250ce5531bdSeric 	}
251ce5531bdSeric 
252ce5531bdSeric 	if (owner != NULL)
253ce5531bdSeric 	{
254ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
255ce5531bdSeric 		if (tTd(13, 5))
256ce5531bdSeric 		{
257ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
258ce5531bdSeric 			printaddr(&e->e_from, FALSE);
259ce5531bdSeric 		}
260ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
261ce5531bdSeric 		e->e_errormode = EM_MAIL;
262ce5531bdSeric 	}
263ce5531bdSeric 
264c1ac89b1Seric # ifdef QUEUE
265c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
266c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
267c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
268c1ac89b1Seric 	{
269c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2706103d9b4Seric 		queueup(e, TRUE, announcequeueup);
271*b056abd0Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
272*b056abd0Seric 			queueup(ee, TRUE, announcequeueup);
273c1ac89b1Seric 	}
274c1ac89b1Seric #endif /* QUEUE */
275c1ac89b1Seric 
276*b056abd0Seric 	if (splitenv != NULL)
277*b056abd0Seric 	{
278*b056abd0Seric 		if (tTd(13, 1))
279*b056abd0Seric 		{
280*b056abd0Seric 			printf("\nsendall: Split queue; remaining queue:\n");
281*b056abd0Seric 			printaddr(e->e_sendqueue, TRUE);
282*b056abd0Seric 		}
283*b056abd0Seric 
284*b056abd0Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
285*b056abd0Seric 		{
286*b056abd0Seric 			CurEnv = ee;
287*b056abd0Seric 			sendenvelope(ee, mode);
288*b056abd0Seric 		}
289*b056abd0Seric 
290*b056abd0Seric 		CurEnv = e;
291*b056abd0Seric 	}
292*b056abd0Seric 	sendenvelope(e, mode);
293*b056abd0Seric 
294*b056abd0Seric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
295*b056abd0Seric 		dropenvelope(splitenv);
296*b056abd0Seric }
297*b056abd0Seric 
298*b056abd0Seric sendenvelope(e, mode)
299*b056abd0Seric 	register ENVELOPE *e;
300*b056abd0Seric 	char mode;
301*b056abd0Seric {
302*b056abd0Seric 	bool oldverbose;
303*b056abd0Seric 	int pid;
304*b056abd0Seric 	register ADDRESS *q;
305*b056abd0Seric 	char *qf;
306*b056abd0Seric 	char *id;
307*b056abd0Seric 
3087880f3e4Seric 	/*
3097880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3107880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3117880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3127880f3e4Seric 	**  addresses to be sent.
3137880f3e4Seric 	*/
3147880f3e4Seric 
3158d19a23dSeric 	if (bitset(EF_FATALERRS, e->e_flags) &&
3168d19a23dSeric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
3177880f3e4Seric 	{
3187880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3197880f3e4Seric 		return;
3207880f3e4Seric 	}
3217880f3e4Seric 
322ce5531bdSeric 	oldverbose = Verbose;
323c1ac89b1Seric 	switch (mode)
324c1ac89b1Seric 	{
325c1ac89b1Seric 	  case SM_VERIFY:
326c1ac89b1Seric 		Verbose = TRUE;
327c1ac89b1Seric 		break;
328c1ac89b1Seric 
329c1ac89b1Seric 	  case SM_QUEUE:
330c1ac89b1Seric   queueonly:
331c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
332c1ac89b1Seric 		return;
333c1ac89b1Seric 
334c1ac89b1Seric 	  case SM_FORK:
335c1ac89b1Seric 		if (e->e_xfp != NULL)
336c1ac89b1Seric 			(void) fflush(e->e_xfp);
337c1ac89b1Seric 
338b269aa8eSeric # if !HASFLOCK
339c1ac89b1Seric 		/*
3406b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3416b2765c6Seric 		**  the lock is owned by a process, not by an open file
3426b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3436b2765c6Seric 		**  then restart from scratch in the child.
344c1ac89b1Seric 		*/
345c1ac89b1Seric 
3466b2765c6Seric 		/* save id for future use */
3476b2765c6Seric 		id = e->e_id;
3486b2765c6Seric 
3496b2765c6Seric 		/* now drop the envelope in the parent */
3506b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3516b2765c6Seric 		dropenvelope(e);
3526b2765c6Seric 
3536b2765c6Seric 		/* and reacquire in the child */
3546b2765c6Seric 		(void) dowork(id, TRUE, FALSE, e);
3556b2765c6Seric 
3566b2765c6Seric 		return;
3576b2765c6Seric 
3586b2765c6Seric # else /* HASFLOCK */
359c1ac89b1Seric 
360c1ac89b1Seric 		pid = fork();
361c1ac89b1Seric 		if (pid < 0)
362c1ac89b1Seric 		{
363c1ac89b1Seric 			goto queueonly;
364c1ac89b1Seric 		}
365c1ac89b1Seric 		else if (pid > 0)
366c1ac89b1Seric 		{
3670e484fe5Seric 			/* be sure we leave the temp files to our child */
3680e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3690e484fe5Seric 			if (e->e_lockfp != NULL)
3700e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3710e484fe5Seric 			e->e_lockfp = NULL;
3720e484fe5Seric 
3730e484fe5Seric 			/* close any random open files in the envelope */
3740e484fe5Seric 			closexscript(e);
3750e484fe5Seric 			if (e->e_dfp != NULL)
3760e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3770e484fe5Seric 			e->e_dfp = NULL;
3780e484fe5Seric 			e->e_id = e->e_df = NULL;
379c1ac89b1Seric 			return;
380c1ac89b1Seric 		}
381c1ac89b1Seric 
382c1ac89b1Seric 		/* double fork to avoid zombies */
383c1ac89b1Seric 		if (fork() > 0)
384c1ac89b1Seric 			exit(EX_OK);
385c1ac89b1Seric 
386c1ac89b1Seric 		/* be sure we are immune from the terminal */
3877880f3e4Seric 		disconnect(1, e);
388c1ac89b1Seric 
389c1ac89b1Seric 		/*
390c1ac89b1Seric 		**  Close any cached connections.
391c1ac89b1Seric 		**
392c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
393c1ac89b1Seric 		**	still knows about the connection.
394c1ac89b1Seric 		**
395c1ac89b1Seric 		**	This should only happen when delivering an error
396c1ac89b1Seric 		**	message.
397c1ac89b1Seric 		*/
398c1ac89b1Seric 
399c1ac89b1Seric 		mci_flush(FALSE, NULL);
400c1ac89b1Seric 
4016b2765c6Seric # endif /* HASFLOCK */
4026b2765c6Seric 
403c1ac89b1Seric 		break;
404c1ac89b1Seric 	}
405c1ac89b1Seric 
406c1ac89b1Seric 	/*
4079c9e68d9Seric 	**  Run through the list and send everything.
4085288e21aSeric 	**
4095288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4105288e21aSeric 	**	result in returned mail.
4119c9e68d9Seric 	*/
4129c9e68d9Seric 
4139c9e68d9Seric 	e->e_nsent = 0;
4145288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
415faad2b16Seric 
416faad2b16Seric 	/* now run through the queue */
4179c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4189c9e68d9Seric 	{
419fdc75a0fSeric #ifdef XDEBUG
420fdc75a0fSeric 		char wbuf[MAXNAME + 20];
421fdc75a0fSeric 
422fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
423fdc75a0fSeric 		checkfd012(wbuf);
424fdc75a0fSeric #endif
4259c9e68d9Seric 		if (mode == SM_VERIFY)
4269c9e68d9Seric 		{
4279c9e68d9Seric 			e->e_to = q->q_paddr;
4289c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4298dfca105Seric 			{
430fafe46e1Seric 				if (q->q_host != NULL && q->q_host[0] != '\0')
4318dfca105Seric 					message("deliverable: mailer %s, host %s, user %s",
4328dfca105Seric 						q->q_mailer->m_name,
4338dfca105Seric 						q->q_host,
4348dfca105Seric 						q->q_user);
435fafe46e1Seric 				else
436fafe46e1Seric 					message("deliverable: mailer %s, user %s",
437fafe46e1Seric 						q->q_mailer->m_name,
438fafe46e1Seric 						q->q_user);
4398dfca105Seric 			}
4409c9e68d9Seric 		}
4419c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4429c9e68d9Seric 		{
4439c9e68d9Seric # ifdef QUEUE
4449c9e68d9Seric 			/*
4459c9e68d9Seric 			**  Checkpoint the send list every few addresses
4469c9e68d9Seric 			*/
4479c9e68d9Seric 
4489c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4499c9e68d9Seric 			{
4509c9e68d9Seric 				queueup(e, TRUE, FALSE);
4519c9e68d9Seric 				e->e_nsent = 0;
4529c9e68d9Seric 			}
4539c9e68d9Seric # endif /* QUEUE */
4549c9e68d9Seric 			(void) deliver(e, q);
4559c9e68d9Seric 		}
4569c9e68d9Seric 	}
4579c9e68d9Seric 	Verbose = oldverbose;
4589c9e68d9Seric 
459fdc75a0fSeric #ifdef XDEBUG
460fdc75a0fSeric 	checkfd012("end of sendenvelope");
461fdc75a0fSeric #endif
462fdc75a0fSeric 
4639c9e68d9Seric 	if (mode == SM_FORK)
4649c9e68d9Seric 		finis();
4659c9e68d9Seric }
4669c9e68d9Seric /*
4679c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4689c9e68d9Seric **
4699c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4709c9e68d9Seric **	two processes on the same stack!!!
4719c9e68d9Seric **
4729c9e68d9Seric **	Parameters:
4739c9e68d9Seric **		none.
4749c9e68d9Seric **
4759c9e68d9Seric **	Returns:
4769c9e68d9Seric **		From a macro???  You've got to be kidding!
4779c9e68d9Seric **
4789c9e68d9Seric **	Side Effects:
4799c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4809c9e68d9Seric **			pid of child in parent, zero in child.
4819c9e68d9Seric **			-1 on unrecoverable error.
4829c9e68d9Seric **
4839c9e68d9Seric **	Notes:
4849c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4859c9e68d9Seric **		vfork for you.....
4869c9e68d9Seric */
4879c9e68d9Seric 
4889c9e68d9Seric # define NFORKTRIES	5
4899c9e68d9Seric 
4909c9e68d9Seric # ifndef FORK
4919c9e68d9Seric # define FORK	fork
4929c9e68d9Seric # endif
4939c9e68d9Seric 
4949c9e68d9Seric # define DOFORK(fORKfN) \
4959c9e68d9Seric {\
4969c9e68d9Seric 	register int i;\
4979c9e68d9Seric \
4989c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4999c9e68d9Seric 	{\
5009c9e68d9Seric 		pid = fORKfN();\
5019c9e68d9Seric 		if (pid >= 0)\
5029c9e68d9Seric 			break;\
5039c9e68d9Seric 		if (i > 0)\
5049c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5059c9e68d9Seric 	}\
5069c9e68d9Seric }
5079c9e68d9Seric /*
5089c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5099c9e68d9Seric **
5109c9e68d9Seric **	Parameters:
5119c9e68d9Seric **		none.
5129c9e68d9Seric **
5139c9e68d9Seric **	Returns:
5149c9e68d9Seric **		pid of child in parent.
5159c9e68d9Seric **		zero in child.
5169c9e68d9Seric **		-1 on error.
5179c9e68d9Seric **
5189c9e68d9Seric **	Side Effects:
5199c9e68d9Seric **		returns twice, once in parent and once in child.
5209c9e68d9Seric */
5219c9e68d9Seric 
5229c9e68d9Seric dofork()
5239c9e68d9Seric {
5249c9e68d9Seric 	register int pid;
5259c9e68d9Seric 
5269c9e68d9Seric 	DOFORK(fork);
5279c9e68d9Seric 	return (pid);
5289c9e68d9Seric }
5299c9e68d9Seric /*
53013bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
53113bbc08cSeric **
53213bbc08cSeric **	This routine delivers to everyone on the same host as the
53313bbc08cSeric **	user on the head of the list.  It is clever about mailers
53413bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
53513bbc08cSeric **	that it will deliver to all these addresses however -- so
53613bbc08cSeric **	deliver should be called once for each address on the
53713bbc08cSeric **	list.
53825a99e2eSeric **
53925a99e2eSeric **	Parameters:
540588cad61Seric **		e -- the envelope to deliver.
541c77d1c25Seric **		firstto -- head of the address list to deliver to.
54225a99e2eSeric **
54325a99e2eSeric **	Returns:
54425a99e2eSeric **		zero -- successfully delivered.
54525a99e2eSeric **		else -- some failure, see ExitStat for more info.
54625a99e2eSeric **
54725a99e2eSeric **	Side Effects:
54825a99e2eSeric **		The standard input is passed off to someone.
54925a99e2eSeric */
55025a99e2eSeric 
551588cad61Seric deliver(e, firstto)
552588cad61Seric 	register ENVELOPE *e;
553c77d1c25Seric 	ADDRESS *firstto;
55425a99e2eSeric {
55578442df3Seric 	char *host;			/* host being sent to */
55678442df3Seric 	char *user;			/* user being sent to */
55725a99e2eSeric 	char **pvp;
5585dfc646bSeric 	register char **mvp;
55925a99e2eSeric 	register char *p;
560588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5616259796dSeric 	ADDRESS *ctladdr;
562b31e7f2bSeric 	register MCI *mci;
563c77d1c25Seric 	register ADDRESS *to = firstto;
564c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
565772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
566911693bfSbostic 	int rcode;			/* response code */
567e103b48fSeric 	char *firstsig;			/* signature of firstto */
5689c9e68d9Seric 	int pid;
5699c9e68d9Seric 	char *curhost;
5709c9e68d9Seric 	int mpvect[2];
5719c9e68d9Seric 	int rpvect[2];
572ee6bf8dfSeric 	char *pv[MAXPV+1];
573579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
574ee6bf8dfSeric 	char buf[MAXNAME];
575c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
576fabb3bd4Seric 	extern int checkcompat();
57725a99e2eSeric 
57835490626Seric 	errno = 0;
579ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5805dfc646bSeric 		return (0);
58125a99e2eSeric 
582134746fbSeric #ifdef NAMED_BIND
583912a731aSbostic 	/* unless interactive, try twice, over a minute */
5848d19a23dSeric 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP)
5858d19a23dSeric 	{
586912a731aSbostic 		_res.retrans = 30;
587912a731aSbostic 		_res.retry = 2;
588912a731aSbostic 	}
589d4bd8f0eSbostic #endif
590912a731aSbostic 
59151552439Seric 	m = to->q_mailer;
59251552439Seric 	host = to->q_host;
593c9be6216Seric 	CurEnv = e;			/* just in case */
5944384d521Seric 	e->e_statmsg = NULL;
595df106f0bSeric 	SmtpError[0] = '\0';
59651552439Seric 
5976ef48975Seric 	if (tTd(10, 1))
5985dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
59951552439Seric 			m->m_mno, host, to->q_user);
600ab81ee53Seric 	if (tTd(10, 100))
601ab81ee53Seric 		printopenfds(FALSE);
602f3dbc832Seric 
603f3dbc832Seric 	/*
604f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
605f3dbc832Seric 	**  connections now, just mark these addresses and return.
606f3dbc832Seric 	**	This is useful if we want to batch connections to
607f3dbc832Seric 	**	reduce load.  This will cause the messages to be
608f3dbc832Seric 	**	queued up, and a daemon will come along to send the
609f3dbc832Seric 	**	messages later.
610f3dbc832Seric 	**		This should be on a per-mailer basis.
611f3dbc832Seric 	*/
612f3dbc832Seric 
61379aa5155Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
614f3dbc832Seric 	{
615f3dbc832Seric 		for (; to != NULL; to = to->q_next)
616f4560e80Seric 		{
617ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
618c6e18ac5Seric 			    to->q_mailer != m)
619f4560e80Seric 				continue;
620268a25e1Seric 			to->q_flags |= QQUEUEUP;
621588cad61Seric 			e->e_to = to->q_paddr;
62208b25121Seric 			message("queued");
6232f624c86Seric 			if (LogLevel > 8)
6244dee0003Seric 				logdelivery(m, NULL, "queued", NULL, e);
625f4560e80Seric 		}
626588cad61Seric 		e->e_to = NULL;
627f3dbc832Seric 		return (0);
628f3dbc832Seric 	}
629f3dbc832Seric 
63025a99e2eSeric 	/*
6315dfc646bSeric 	**  Do initial argv setup.
6325dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6335dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6345dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6355dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6365dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6373bea8136Seric 	**		The from address rewrite is expected to make
6383bea8136Seric 	**		the address relative to the other end.
6395dfc646bSeric 	*/
6405dfc646bSeric 
64178442df3Seric 	/* rewrite from address, using rewriting rules */
642efe54562Seric 	rcode = EX_OK;
643efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
644efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
645efe54562Seric 					   &rcode, e));
646ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
647588cad61Seric 	define('h', host, e);			/* to host */
6485dfc646bSeric 	Errors = 0;
6495dfc646bSeric 	pvp = pv;
6505dfc646bSeric 	*pvp++ = m->m_argv[0];
6515dfc646bSeric 
6525dfc646bSeric 	/* insert -f or -r flag as appropriate */
65357fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6545dfc646bSeric 	{
65557fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6565dfc646bSeric 			*pvp++ = "-f";
6575dfc646bSeric 		else
6585dfc646bSeric 			*pvp++ = "-r";
659c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6605dfc646bSeric 	}
6615dfc646bSeric 
6625dfc646bSeric 	/*
6635dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6645dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6655dfc646bSeric 	**  be one of these, and there are only a few more slots
6665dfc646bSeric 	**  in the pv after it.
6675dfc646bSeric 	*/
6685dfc646bSeric 
6695dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6705dfc646bSeric 	{
6712bc47524Seric 		/* can't use strchr here because of sign extension problems */
6722bc47524Seric 		while (*p != '\0')
6732bc47524Seric 		{
6742bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6752bc47524Seric 			{
6762bc47524Seric 				if (*p == 'u')
6775dfc646bSeric 					break;
6782bc47524Seric 			}
6792bc47524Seric 		}
6802bc47524Seric 
6812bc47524Seric 		if (*p != '\0')
6825dfc646bSeric 			break;
6835dfc646bSeric 
6845dfc646bSeric 		/* this entry is safe -- go ahead and process it */
685588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6865dfc646bSeric 		*pvp++ = newstr(buf);
6875dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6885dfc646bSeric 		{
68908b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6905dfc646bSeric 			return (-1);
6915dfc646bSeric 		}
6925dfc646bSeric 	}
693c579ef51Seric 
69433db8731Seric 	/*
69533db8731Seric 	**  If we have no substitution for the user name in the argument
69633db8731Seric 	**  list, we know that we must supply the names otherwise -- and
69733db8731Seric 	**  SMTP is the answer!!
69833db8731Seric 	*/
69933db8731Seric 
7005dfc646bSeric 	if (*mvp == NULL)
701c579ef51Seric 	{
702c579ef51Seric 		/* running SMTP */
7032c7e1b8dSeric # ifdef SMTP
704c579ef51Seric 		clever = TRUE;
705c579ef51Seric 		*pvp = NULL;
7066c2c3107Seric # else /* SMTP */
70733db8731Seric 		/* oops!  we don't implement SMTP */
708df106f0bSeric 		syserr("554 SMTP style mailer not implemented");
7092c7e1b8dSeric 		return (EX_SOFTWARE);
7106c2c3107Seric # endif /* SMTP */
711c579ef51Seric 	}
7125dfc646bSeric 
7135dfc646bSeric 	/*
7145dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7155dfc646bSeric 	**  run through our address list and append all the addresses
7165dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7175dfc646bSeric 	**  always send another copy later.
7185dfc646bSeric 	*/
7195dfc646bSeric 
7205dfc646bSeric 	tobuf[0] = '\0';
721588cad61Seric 	e->e_to = tobuf;
7226259796dSeric 	ctladdr = NULL;
723e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7245dfc646bSeric 	for (; to != NULL; to = to->q_next)
7255dfc646bSeric 	{
7265dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
72757fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7285dfc646bSeric 			break;
7295dfc646bSeric 
7305dfc646bSeric 		/* if already sent or not for this host, don't send */
731ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
732e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
733e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7345dfc646bSeric 			continue;
7356259796dSeric 
7364b22ea87Seric 		/* avoid overflowing tobuf */
737aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7384b22ea87Seric 			break;
7394b22ea87Seric 
7406ef48975Seric 		if (tTd(10, 1))
741772e6e50Seric 		{
742772e6e50Seric 			printf("\nsend to ");
743772e6e50Seric 			printaddr(to, FALSE);
744772e6e50Seric 		}
745772e6e50Seric 
7466259796dSeric 		/* compute effective uid/gid when sending */
747960e665aSeric 		/* XXX perhaps this should be to->q_mailer != LocalMailer ?? */
748960e665aSeric 		/* XXX perhaps it should be a mailer flag? */
749960e665aSeric 		if (to->q_mailer == ProgMailer || to->q_mailer == FileMailer)
7506259796dSeric 			ctladdr = getctladdr(to);
7516259796dSeric 
7525dfc646bSeric 		user = to->q_user;
753588cad61Seric 		e->e_to = to->q_paddr;
75475f1ade9Seric 		if (tTd(10, 5))
75575f1ade9Seric 		{
75675f1ade9Seric 			printf("deliver: QDONTSEND ");
75775f1ade9Seric 			printaddr(to, FALSE);
75875f1ade9Seric 		}
759ee4b0922Seric 		to->q_flags |= QDONTSEND;
7605dfc646bSeric 
7615dfc646bSeric 		/*
7625dfc646bSeric 		**  Check to see that these people are allowed to
7635dfc646bSeric 		**  talk to each other.
7642a6e0786Seric 		*/
7652a6e0786Seric 
76669582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
76769582d2fSeric 		{
76869582d2fSeric 			NoReturn = TRUE;
76908b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
7704dee0003Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e);
77169582d2fSeric 			continue;
77269582d2fSeric 		}
773fabb3bd4Seric 		rcode = checkcompat(to, e);
7741793c9c5Seric 		if (rcode != EX_OK)
7755dfc646bSeric 		{
7761000c37aSeric 			markfailure(e, to, rcode);
7774dee0003Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
7785dfc646bSeric 			continue;
7795dfc646bSeric 		}
7802a6e0786Seric 
7812a6e0786Seric 		/*
7829ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7839ec9501bSeric 		**	about them.
78425a99e2eSeric 		*/
78525a99e2eSeric 
78657fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
78725a99e2eSeric 		{
7881d8f1806Seric 			stripquotes(user);
7891d8f1806Seric 			stripquotes(host);
79025a99e2eSeric 		}
79125a99e2eSeric 
792cdb828c5Seric 		/* hack attack -- delivermail compatibility */
793cdb828c5Seric 		if (m == ProgMailer && *user == '|')
794cdb828c5Seric 			user++;
795cdb828c5Seric 
79625a99e2eSeric 		/*
7973efaed6eSeric 		**  If an error message has already been given, don't
7983efaed6eSeric 		**	bother to send to this address.
7993efaed6eSeric 		**
8003efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
8013efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
8023efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
8033efaed6eSeric 		*/
8043efaed6eSeric 
8056cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8063efaed6eSeric 			continue;
8073efaed6eSeric 
808f2fec898Seric 		/* save statistics.... */
809588cad61Seric 		markstats(e, to);
810f2fec898Seric 
8113efaed6eSeric 		/*
81225a99e2eSeric 		**  See if this user name is "special".
81325a99e2eSeric 		**	If the user name has a slash in it, assume that this
81451552439Seric 		**	is a file -- send it off without further ado.  Note
81551552439Seric 		**	that this type of addresses is not processed along
81651552439Seric 		**	with the others, so we fudge on the To person.
81725a99e2eSeric 		*/
81825a99e2eSeric 
8192c017f8dSeric 		if (m == FileMailer)
82025a99e2eSeric 		{
82103463a75Seric 			rcode = mailfile(user, ctladdr, e);
82203463a75Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
823dde5acadSeric 			if (rcode == EX_OK)
824dde5acadSeric 				to->q_flags |= QSENT;
8255dfc646bSeric 			continue;
82625a99e2eSeric 		}
82725a99e2eSeric 
82813bbc08cSeric 		/*
82913bbc08cSeric 		**  Address is verified -- add this user to mailer
83013bbc08cSeric 		**  argv, and add it to the print list of recipients.
83113bbc08cSeric 		*/
83213bbc08cSeric 
833508daeccSeric 		/* link together the chain of recipients */
834508daeccSeric 		to->q_tchain = tochain;
835508daeccSeric 		tochain = to;
836508daeccSeric 
8375dfc646bSeric 		/* create list of users for error messages */
838db8841e9Seric 		(void) strcat(tobuf, ",");
839db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
840588cad61Seric 		define('u', user, e);		/* to user */
841b6f89e6eSeric 		p = to->q_home;
842b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
843b6f89e6eSeric 			p = ctladdr->q_home;
844b6f89e6eSeric 		define('z', p, e);	/* user's home */
8455dfc646bSeric 
846c579ef51Seric 		/*
847508daeccSeric 		**  Expand out this user into argument list.
848c579ef51Seric 		*/
849c579ef51Seric 
850508daeccSeric 		if (!clever)
851c579ef51Seric 		{
852588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8535dfc646bSeric 			*pvp++ = newstr(buf);
8545dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8555dfc646bSeric 			{
8565dfc646bSeric 				/* allow some space for trailing parms */
8575dfc646bSeric 				break;
8585dfc646bSeric 			}
8595dfc646bSeric 		}
860c579ef51Seric 	}
8615dfc646bSeric 
862145b49b1Seric 	/* see if any addresses still exist */
863145b49b1Seric 	if (tobuf[0] == '\0')
864c579ef51Seric 	{
865588cad61Seric 		define('g', (char *) NULL, e);
866145b49b1Seric 		return (0);
867c579ef51Seric 	}
868145b49b1Seric 
8695dfc646bSeric 	/* print out messages as full list */
87063780dbdSeric 	e->e_to = tobuf + 1;
8715dfc646bSeric 
8725dfc646bSeric 	/*
8735dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8745dfc646bSeric 	*/
8755dfc646bSeric 
876c579ef51Seric 	while (!clever && *++mvp != NULL)
8775dfc646bSeric 	{
878588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8795dfc646bSeric 		*pvp++ = newstr(buf);
8805dfc646bSeric 		if (pvp >= &pv[MAXPV])
88108b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8825dfc646bSeric 	}
8835dfc646bSeric 	*pvp++ = NULL;
8845dfc646bSeric 
88525a99e2eSeric 	/*
88625a99e2eSeric 	**  Call the mailer.
8876328bdf7Seric 	**	The argument vector gets built, pipes
88825a99e2eSeric 	**	are created as necessary, and we fork & exec as
8896328bdf7Seric 	**	appropriate.
890c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
89125a99e2eSeric 	*/
89225a99e2eSeric 
893960e665aSeric 	/*XXX this seems a bit wierd */
8944899f6b5Seric 	if (ctladdr == NULL && m != ProgMailer &&
8954899f6b5Seric 	    bitset(QGOODUID, e->e_from.q_flags))
896960e665aSeric 		ctladdr = &e->e_from;
897960e665aSeric 
898134746fbSeric #ifdef NAMED_BIND
8992bcc6d2dSeric 	if (ConfigLevel < 2)
900912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
901134746fbSeric #endif
9029c9e68d9Seric 
9039c9e68d9Seric 	if (tTd(11, 1))
904134746fbSeric 	{
9059c9e68d9Seric 		printf("openmailer:");
9069c9e68d9Seric 		printav(pv);
9079c9e68d9Seric 	}
9089c9e68d9Seric 	errno = 0;
9099c9e68d9Seric 
9109c9e68d9Seric 	CurHostName = m->m_mailer;
9119c9e68d9Seric 
9129c9e68d9Seric 	/*
9139c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9149c9e68d9Seric 	**  connection.
9159c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9169c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9179c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9189c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9199c9e68d9Seric 	*/
9209c9e68d9Seric 
9219c9e68d9Seric 	curhost = NULL;
922c931b82bSeric 	SmtpPhase = NULL;
923df106f0bSeric 	mci = NULL;
9249c9e68d9Seric 
9257febfd66Seric #ifdef XDEBUG
9267febfd66Seric 	{
9277febfd66Seric 		char wbuf[MAXLINE];
9287febfd66Seric 
9297febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9307febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
9317febfd66Seric 		checkfd012(wbuf);
9327febfd66Seric 	}
9337febfd66Seric #endif
9347febfd66Seric 
9359c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9369c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9379c9e68d9Seric 	{
9389c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9399c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9409c9e68d9Seric 		mci->mci_in = stdin;
9419c9e68d9Seric 		mci->mci_out = stdout;
9429c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9439c9e68d9Seric 		mci->mci_mailer = m;
9449c9e68d9Seric 	}
9459c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9469c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9479c9e68d9Seric 	{
9489c9e68d9Seric #ifdef DAEMON
9499c9e68d9Seric 		register int i;
9509c9e68d9Seric 		register u_short port;
9519c9e68d9Seric 
952a5d44642Seric 		if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
953a5d44642Seric 		{
954a5d44642Seric 			syserr("null host name for %s mailer", m->m_mailer);
955a5d44642Seric 			rcode = EX_CONFIG;
956a5d44642Seric 			goto give_up;
957a5d44642Seric 		}
958a5d44642Seric 
9599c9e68d9Seric 		CurHostName = pv[1];
9609c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9619c9e68d9Seric 
9629c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9639c9e68d9Seric 		{
964a5d44642Seric 			syserr("null host signature for %s", pv[1]);
965845e533cSeric 			rcode = EX_OSERR;
966b31e7f2bSeric 			goto give_up;
967b31e7f2bSeric 		}
9689c9e68d9Seric 
9699c9e68d9Seric 		if (!clever)
9709c9e68d9Seric 		{
9719c9e68d9Seric 			syserr("554 non-clever IPC");
972df106f0bSeric 			rcode = EX_CONFIG;
9739c9e68d9Seric 			goto give_up;
9749c9e68d9Seric 		}
9759c9e68d9Seric 		if (pv[2] != NULL)
9769c9e68d9Seric 			port = atoi(pv[2]);
9779c9e68d9Seric 		else
9789c9e68d9Seric 			port = 0;
9799c9e68d9Seric tryhost:
9809c9e68d9Seric 		while (*curhost != '\0')
9819c9e68d9Seric 		{
9829c9e68d9Seric 			register char *p;
9839c9e68d9Seric 			static char hostbuf[MAXNAME];
9849c9e68d9Seric 
9859c9e68d9Seric 			/* pull the next host from the signature */
9869c9e68d9Seric 			p = strchr(curhost, ':');
9879c9e68d9Seric 			if (p == NULL)
9889c9e68d9Seric 				p = &curhost[strlen(curhost)];
989df106f0bSeric 			if (p == curhost)
990df106f0bSeric 			{
991df106f0bSeric 				syserr("deliver: null host name in signature");
9928087428aSeric 				curhost++;
993df106f0bSeric 				continue;
994df106f0bSeric 			}
9959c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9969c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9979c9e68d9Seric 			if (*p != '\0')
9989c9e68d9Seric 				p++;
9999c9e68d9Seric 			curhost = p;
10009c9e68d9Seric 
10019c9e68d9Seric 			/* see if we already know that this host is fried */
10029c9e68d9Seric 			CurHostName = hostbuf;
10039c9e68d9Seric 			mci = mci_get(hostbuf, m);
10049c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
10059c9e68d9Seric 			{
10069c9e68d9Seric 				if (tTd(11, 1))
10079c9e68d9Seric 				{
10089c9e68d9Seric 					printf("openmailer: ");
10094052916eSeric 					mci_dump(mci, FALSE);
10109c9e68d9Seric 				}
10119c9e68d9Seric 				CurHostName = mci->mci_host;
10129c9e68d9Seric 				break;
10139c9e68d9Seric 			}
10149c9e68d9Seric 			mci->mci_mailer = m;
10159c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
10169c9e68d9Seric 				continue;
10179c9e68d9Seric 
10189c9e68d9Seric 			/* try the connection */
10199c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
10209c9e68d9Seric 			message("Connecting to %s (%s)...",
10219c9e68d9Seric 				hostbuf, m->m_name);
10229c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
10239c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
10249c9e68d9Seric 			mci->mci_exitstat = i;
10259c9e68d9Seric 			mci->mci_errno = errno;
1026f170942cSeric #ifdef NAMED_BIND
1027f170942cSeric 			mci->mci_herrno = h_errno;
1028f170942cSeric #endif
10299c9e68d9Seric 			if (i == EX_OK)
10309c9e68d9Seric 			{
10319c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10329c9e68d9Seric 				mci_cache(mci);
1033f170942cSeric 				if (TrafficLogFile != NULL)
1034f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1035f170942cSeric 						getpid(), hostbuf);
10369c9e68d9Seric 				break;
10379c9e68d9Seric 			}
10389c9e68d9Seric 			else if (tTd(11, 1))
10399c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10409c9e68d9Seric 					i, errno);
10419c9e68d9Seric 
10429c9e68d9Seric 			/* enter status of this host */
10439c9e68d9Seric 			setstat(i);
1044df106f0bSeric 
1045df106f0bSeric 			/* should print some message here for -v mode */
1046df106f0bSeric 		}
1047df106f0bSeric 		if (mci == NULL)
1048df106f0bSeric 		{
1049df106f0bSeric 			syserr("deliver: no host name");
1050df106f0bSeric 			rcode = EX_OSERR;
1051df106f0bSeric 			goto give_up;
10529c9e68d9Seric 		}
10539c9e68d9Seric 		mci->mci_pid = 0;
10549c9e68d9Seric #else /* no DAEMON */
10559c9e68d9Seric 		syserr("554 openmailer: no IPC");
10569c9e68d9Seric 		if (tTd(11, 1))
10579c9e68d9Seric 			printf("openmailer: NULL\n");
1058df106f0bSeric 		rcode = EX_UNAVAILABLE;
1059df106f0bSeric 		goto give_up;
10609c9e68d9Seric #endif /* DAEMON */
10619c9e68d9Seric 	}
10629c9e68d9Seric 	else
10639c9e68d9Seric 	{
1064f170942cSeric 		if (TrafficLogFile != NULL)
10650e3bfef5Seric 		{
1066f170942cSeric 			char **av;
1067f170942cSeric 
1068f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1069f170942cSeric 			for (av = pv; *av != NULL; av++)
1070f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1071f170942cSeric 			fprintf(TrafficLogFile, "\n");
10726fe8c3bcSeric 		}
10736fe8c3bcSeric 
10749c9e68d9Seric 		/* create a pipe to shove the mail through */
10759c9e68d9Seric 		if (pipe(mpvect) < 0)
10769c9e68d9Seric 		{
10770e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10780e3bfef5Seric 				e->e_to, m->m_name);
10799c9e68d9Seric 			if (tTd(11, 1))
10809c9e68d9Seric 				printf("openmailer: NULL\n");
10819c9e68d9Seric 			rcode = EX_OSERR;
10829c9e68d9Seric 			goto give_up;
10839c9e68d9Seric 		}
10849c9e68d9Seric 
10859c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10869c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10879c9e68d9Seric 		{
10880e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10890e3bfef5Seric 				e->e_to, m->m_name);
10909c9e68d9Seric 			(void) close(mpvect[0]);
10919c9e68d9Seric 			(void) close(mpvect[1]);
10929c9e68d9Seric 			if (tTd(11, 1))
10939c9e68d9Seric 				printf("openmailer: NULL\n");
10949c9e68d9Seric 			rcode = EX_OSERR;
10959c9e68d9Seric 			goto give_up;
10969c9e68d9Seric 		}
10979c9e68d9Seric 
10989c9e68d9Seric 		/*
10999c9e68d9Seric 		**  Actually fork the mailer process.
11009c9e68d9Seric 		**	DOFORK is clever about retrying.
11019c9e68d9Seric 		**
11029c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
11039c9e68d9Seric 		**	around so that endmail will get it.
11049c9e68d9Seric 		*/
11059c9e68d9Seric 
11069c9e68d9Seric 		if (e->e_xfp != NULL)
11079c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
11089c9e68d9Seric 		(void) fflush(stdout);
11099c9e68d9Seric # ifdef SIGCHLD
11102b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
11119c9e68d9Seric # endif /* SIGCHLD */
11129c9e68d9Seric 		DOFORK(FORK);
11139c9e68d9Seric 		/* pid is set by DOFORK */
11149c9e68d9Seric 		if (pid < 0)
11159c9e68d9Seric 		{
11169c9e68d9Seric 			/* failure */
11170e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
11180e3bfef5Seric 				e->e_to, m->m_name);
11199c9e68d9Seric 			(void) close(mpvect[0]);
11209c9e68d9Seric 			(void) close(mpvect[1]);
11219c9e68d9Seric 			if (clever)
11229c9e68d9Seric 			{
11239c9e68d9Seric 				(void) close(rpvect[0]);
11249c9e68d9Seric 				(void) close(rpvect[1]);
11259c9e68d9Seric 			}
11269c9e68d9Seric 			if (tTd(11, 1))
11279c9e68d9Seric 				printf("openmailer: NULL\n");
11289c9e68d9Seric 			rcode = EX_OSERR;
11299c9e68d9Seric 			goto give_up;
11309c9e68d9Seric 		}
11319c9e68d9Seric 		else if (pid == 0)
11329c9e68d9Seric 		{
11339c9e68d9Seric 			int i;
11349c9e68d9Seric 			int saveerrno;
11359c9e68d9Seric 			char **ep;
11369c9e68d9Seric 			char *env[MAXUSERENVIRON];
11379c9e68d9Seric 			extern char **environ;
11389c9e68d9Seric 			extern int DtableSize;
11399c9e68d9Seric 
11409c9e68d9Seric 			/* child -- set up input & exec mailer */
11412b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
11422b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
11432b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11449c9e68d9Seric 
114544f2317fSeric 			/* reset user and group */
114644f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
114744f2317fSeric 			{
114844f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
114944f2317fSeric 				{
115044f2317fSeric 					(void) initgroups(DefUser, DefGid);
1151fc354902Seric 					(void) setgid(DefGid);
115244f2317fSeric 					(void) setuid(DefUid);
115344f2317fSeric 				}
115444f2317fSeric 				else
115544f2317fSeric 				{
115644f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
115744f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
115844f2317fSeric 						ctladdr->q_gid);
115951cfe111Seric 					(void) setgid(ctladdr->q_gid);
116044f2317fSeric 					(void) setuid(ctladdr->q_uid);
116144f2317fSeric 				}
116244f2317fSeric 			}
116344f2317fSeric 
116444f2317fSeric 			if (tTd(11, 2))
116544f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
116644f2317fSeric 					getuid(), geteuid());
116744f2317fSeric 
1168b986f6aaSeric 			/* move into some "safe" directory */
1169b986f6aaSeric 			if (m->m_execdir != NULL)
1170b986f6aaSeric 			{
1171b986f6aaSeric 				char *p, *q;
1172b986f6aaSeric 				char buf[MAXLINE];
1173b986f6aaSeric 
1174b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1175b986f6aaSeric 				{
1176b986f6aaSeric 					q = strchr(p, ':');
1177b986f6aaSeric 					if (q != NULL)
1178b986f6aaSeric 						*q = '\0';
1179b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1180b986f6aaSeric 					if (q != NULL)
1181b986f6aaSeric 						*q++ = ':';
1182b986f6aaSeric 					if (tTd(11, 20))
1183b986f6aaSeric 						printf("openmailer: trydir %s\n",
1184b986f6aaSeric 							buf);
1185b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1186b986f6aaSeric 						break;
1187b986f6aaSeric 				}
1188b986f6aaSeric 			}
1189b986f6aaSeric 
11909c9e68d9Seric 			/* arrange to filter std & diag output of command */
11919c9e68d9Seric 			if (clever)
11929c9e68d9Seric 			{
11939c9e68d9Seric 				(void) close(rpvect[0]);
11946fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11956fe8c3bcSeric 				{
11960e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11970e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11986fe8c3bcSeric 					_exit(EX_OSERR);
11996fe8c3bcSeric 				}
12009c9e68d9Seric 				(void) close(rpvect[1]);
12019c9e68d9Seric 			}
12028d19a23dSeric 			else if (OpMode == MD_SMTP || OpMode == MD_DAEMON || HoldErrs)
12039c9e68d9Seric 			{
12049c9e68d9Seric 				/* put mailer output in transcript */
12056fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
12066fe8c3bcSeric 				{
12070e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
12080e3bfef5Seric 						e->e_to, m->m_name,
12096fe8c3bcSeric 						fileno(e->e_xfp));
12106fe8c3bcSeric 					_exit(EX_OSERR);
12119c9e68d9Seric 				}
12126fe8c3bcSeric 			}
12136fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
12146fe8c3bcSeric 			{
12150e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
12160e3bfef5Seric 					e->e_to, m->m_name);
12176fe8c3bcSeric 				_exit(EX_OSERR);
12186fe8c3bcSeric 			}
12199c9e68d9Seric 
12209c9e68d9Seric 			/* arrange to get standard input */
12219c9e68d9Seric 			(void) close(mpvect[1]);
12229c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
12239c9e68d9Seric 			{
12240e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
12250e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
12269c9e68d9Seric 				_exit(EX_OSERR);
12279c9e68d9Seric 			}
12289c9e68d9Seric 			(void) close(mpvect[0]);
12299c9e68d9Seric 
12309c9e68d9Seric 			/* arrange for all the files to be closed */
12319c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12329c9e68d9Seric 			{
12339c9e68d9Seric 				register int j;
123444f2317fSeric 
12359c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12369c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
12379c9e68d9Seric 			}
12389c9e68d9Seric 
12399c9e68d9Seric 			/* set up the mailer environment */
12409c9e68d9Seric 			i = 0;
12419c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12429c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12439c9e68d9Seric 			{
12449c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12459c9e68d9Seric 					env[i++] = *ep;
12469c9e68d9Seric 			}
12479c9e68d9Seric 			env[i++] = NULL;
12489c9e68d9Seric 
12499c9e68d9Seric 			/* try to execute the mailer */
12509c9e68d9Seric 			execve(m->m_mailer, pv, env);
12519c9e68d9Seric 			saveerrno = errno;
12529c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12537c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12547c941fd2Seric 				_exit(EX_OSERR);
12559c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12569c9e68d9Seric 		}
12579c9e68d9Seric 
12589c9e68d9Seric 		/*
12599c9e68d9Seric 		**  Set up return value.
12609c9e68d9Seric 		*/
12619c9e68d9Seric 
12629c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12639c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12649c9e68d9Seric 		mci->mci_mailer = m;
12659c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12669c9e68d9Seric 		mci->mci_pid = pid;
12679c9e68d9Seric 		(void) close(mpvect[0]);
12689c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1269335eae58Seric 		if (mci->mci_out == NULL)
1270335eae58Seric 		{
1271335eae58Seric 			syserr("deliver: cannot create mailer output channel, fd=%d",
1272335eae58Seric 				mpvect[1]);
1273335eae58Seric 			(void) close(mpvect[1]);
1274335eae58Seric 			if (clever)
1275335eae58Seric 			{
1276335eae58Seric 				(void) close(rpvect[0]);
1277335eae58Seric 				(void) close(rpvect[1]);
1278335eae58Seric 			}
1279335eae58Seric 			rcode = EX_OSERR;
1280335eae58Seric 			goto give_up;
1281335eae58Seric 		}
12829c9e68d9Seric 		if (clever)
12839c9e68d9Seric 		{
12849c9e68d9Seric 			(void) close(rpvect[1]);
12859c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
1286335eae58Seric 			if (mci->mci_in == NULL)
1287335eae58Seric 			{
1288335eae58Seric 				syserr("deliver: cannot create mailer input channel, fd=%d",
1289335eae58Seric 					mpvect[1]);
1290335eae58Seric 				(void) close(rpvect[0]);
1291335eae58Seric 				fclose(mci->mci_out);
1292335eae58Seric 				mci->mci_out = NULL;
1293335eae58Seric 				rcode = EX_OSERR;
1294335eae58Seric 				goto give_up;
1295335eae58Seric 			}
12969c9e68d9Seric 		}
12979c9e68d9Seric 		else
12989c9e68d9Seric 		{
12999c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
13009c9e68d9Seric 			mci->mci_in = NULL;
13019c9e68d9Seric 		}
13029c9e68d9Seric 	}
13039c9e68d9Seric 
13049c9e68d9Seric 	/*
13059c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
13069c9e68d9Seric 	*/
13079c9e68d9Seric 
13089c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
13099c9e68d9Seric 	{
13109c9e68d9Seric 		smtpinit(m, mci, e);
13119c9e68d9Seric 	}
13129c9e68d9Seric 	if (tTd(11, 1))
13139c9e68d9Seric 	{
13149c9e68d9Seric 		printf("openmailer: ");
13154052916eSeric 		mci_dump(mci, FALSE);
13169c9e68d9Seric 	}
13179c9e68d9Seric 
13189c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1319b31e7f2bSeric 	{
1320b31e7f2bSeric 		/* couldn't open the mailer */
1321b31e7f2bSeric 		rcode = mci->mci_exitstat;
13222a6bc25bSeric 		errno = mci->mci_errno;
1323f170942cSeric #ifdef NAMED_BIND
1324f170942cSeric 		h_errno = mci->mci_herrno;
1325f170942cSeric #endif
1326b31e7f2bSeric 		if (rcode == EX_OK)
1327b31e7f2bSeric 		{
1328b31e7f2bSeric 			/* shouldn't happen */
132908b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
13306b0f339dSeric 				rcode, mci->mci_state, firstsig);
1331b31e7f2bSeric 			rcode = EX_SOFTWARE;
1332b31e7f2bSeric 		}
1333cb082c5bSeric 		else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0')
133490891494Seric 		{
133590891494Seric 			/* try next MX site */
133690891494Seric 			goto tryhost;
133790891494Seric 		}
1338b31e7f2bSeric 	}
1339b31e7f2bSeric 	else if (!clever)
1340b31e7f2bSeric 	{
1341b31e7f2bSeric 		/*
1342b31e7f2bSeric 		**  Format and send message.
1343b31e7f2bSeric 		*/
134415d084d5Seric 
13455aa0f353Seric 		putfromline(mci, e);
13465aa0f353Seric 		(*e->e_puthdr)(mci, e);
13475aa0f353Seric 		putline("\n", mci);
13485aa0f353Seric 		(*e->e_putbody)(mci, e, NULL);
1349b31e7f2bSeric 
1350b31e7f2bSeric 		/* get the exit status */
1351c9be6216Seric 		rcode = endmailer(mci, e, pv);
1352134746fbSeric 	}
1353134746fbSeric 	else
1354b31e7f2bSeric #ifdef SMTP
1355134746fbSeric 	{
1356b31e7f2bSeric 		/*
1357b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1358b31e7f2bSeric 		*/
135915d084d5Seric 
1360b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1361b31e7f2bSeric 		if (rcode == EX_OK)
136275889e88Seric 		{
1363ded0d3daSkarels 			register char *t = tobuf;
1364ded0d3daSkarels 			register int i;
1365ded0d3daSkarels 
1366588cad61Seric 			/* send the recipient list */
136763780dbdSeric 			tobuf[0] = '\0';
136875889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
136975889e88Seric 			{
137063780dbdSeric 				e->e_to = to->q_paddr;
137115d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
137275889e88Seric 				{
137383b7ddc9Seric 					markfailure(e, to, i);
13744dee0003Seric 					giveresponse(i, m, mci, ctladdr, e);
137563780dbdSeric 				}
137675889e88Seric 				else
137775889e88Seric 				{
1378911693bfSbostic 					*t++ = ',';
1379b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1380b31e7f2bSeric 						continue;
1381ee39df61Seric 					*t = '\0';
1382588cad61Seric 				}
1383588cad61Seric 			}
1384588cad61Seric 
138563780dbdSeric 			/* now send the data */
138663780dbdSeric 			if (tobuf[0] == '\0')
1387b31e7f2bSeric 			{
13889c9e68d9Seric 				rcode = EX_OK;
138963780dbdSeric 				e->e_to = NULL;
1390b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1391b31e7f2bSeric 					smtprset(m, mci, e);
1392b31e7f2bSeric 			}
139375889e88Seric 			else
139475889e88Seric 			{
139563780dbdSeric 				e->e_to = tobuf + 1;
139675889e88Seric 				rcode = smtpdata(m, mci, e);
139763780dbdSeric 			}
139863780dbdSeric 
139963780dbdSeric 			/* now close the connection */
1400b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
140115d084d5Seric 				smtpquit(m, mci, e);
140263780dbdSeric 		}
1403cb082c5bSeric 		if (rcode != EX_OK && curhost != NULL && *curhost != '\0')
14049c9e68d9Seric 		{
14059c9e68d9Seric 			/* try next MX site */
14069c9e68d9Seric 			goto tryhost;
14079c9e68d9Seric 		}
1408c579ef51Seric 	}
1409b31e7f2bSeric #else /* not SMTP */
1410a05b3449Sbostic 	{
141108b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1412845e533cSeric 		rcode = EX_CONFIG;
1413b31e7f2bSeric 		goto give_up;
1414a05b3449Sbostic 	}
1415b31e7f2bSeric #endif /* SMTP */
1416134746fbSeric #ifdef NAMED_BIND
14172bcc6d2dSeric 	if (ConfigLevel < 2)
1418912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1419134746fbSeric #endif
14205dfc646bSeric 
1421b31e7f2bSeric 	/* arrange a return receipt if requested */
1422df106f0bSeric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1423df106f0bSeric 	    bitnset(M_LOCALMAILER, m->m_flags))
1424b31e7f2bSeric 	{
1425b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1426b31e7f2bSeric 		/* do we want to send back more info? */
1427b31e7f2bSeric 	}
1428b31e7f2bSeric 
1429c77d1c25Seric 	/*
143063780dbdSeric 	**  Do final status disposal.
143163780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1432c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1433c77d1c25Seric 	**		addressees.
1434c77d1c25Seric 	*/
1435c77d1c25Seric 
1436b31e7f2bSeric   give_up:
143763780dbdSeric 	if (tobuf[0] != '\0')
14384dee0003Seric 		giveresponse(rcode, m, mci, ctladdr, e);
1439772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1440b31e7f2bSeric 	{
1441dde5acadSeric 		if (rcode != EX_OK)
144283b7ddc9Seric 			markfailure(e, to, rcode);
1443dde5acadSeric 		else
1444655518ecSeric 		{
1445dde5acadSeric 			to->q_flags |= QSENT;
1446655518ecSeric 			e->e_nsent++;
1447df106f0bSeric 			if (e->e_receiptto != NULL &&
1448df106f0bSeric 			    bitnset(M_LOCALMAILER, m->m_flags))
1449df106f0bSeric 			{
1450df106f0bSeric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1451df106f0bSeric 					to->q_paddr);
1452df106f0bSeric 			}
1453655518ecSeric 		}
1454b31e7f2bSeric 	}
1455b31e7f2bSeric 
1456b31e7f2bSeric 	/*
1457b31e7f2bSeric 	**  Restore state and return.
1458b31e7f2bSeric 	*/
1459c77d1c25Seric 
14607febfd66Seric #ifdef XDEBUG
14617febfd66Seric 	{
14627febfd66Seric 		char wbuf[MAXLINE];
14637febfd66Seric 
14647febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
146544622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
146644622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
146744622ea3Seric 			m->m_name);
14687febfd66Seric 		checkfd012(wbuf);
14697febfd66Seric 	}
14707febfd66Seric #endif
14717febfd66Seric 
147235490626Seric 	errno = 0;
1473588cad61Seric 	define('g', (char *) NULL, e);
14745826d9d3Seric 	return (rcode);
147525a99e2eSeric }
14765dfc646bSeric /*
147783b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
147883b7ddc9Seric **
147983b7ddc9Seric **	Parameters:
148083b7ddc9Seric **		e -- the envelope we are sending.
148183b7ddc9Seric **		q -- the address to mark.
148283b7ddc9Seric **		rcode -- the code signifying the particular failure.
148383b7ddc9Seric **
148483b7ddc9Seric **	Returns:
148583b7ddc9Seric **		none.
148683b7ddc9Seric **
148783b7ddc9Seric **	Side Effects:
148883b7ddc9Seric **		marks the address (and possibly the envelope) with the
148983b7ddc9Seric **			failure so that an error will be returned or
149083b7ddc9Seric **			the message will be queued, as appropriate.
149183b7ddc9Seric */
149283b7ddc9Seric 
149383b7ddc9Seric markfailure(e, q, rcode)
149483b7ddc9Seric 	register ENVELOPE *e;
149583b7ddc9Seric 	register ADDRESS *q;
149683b7ddc9Seric 	int rcode;
149783b7ddc9Seric {
149819c47125Seric 	char buf[MAXLINE];
149919c47125Seric 
1500d5aafa3cSeric 	switch (rcode)
1501d5aafa3cSeric 	{
1502d5aafa3cSeric 	  case EX_OK:
1503d5aafa3cSeric 		break;
1504d5aafa3cSeric 
1505d5aafa3cSeric 	  case EX_TEMPFAIL:
1506d5aafa3cSeric 	  case EX_IOERR:
1507d5aafa3cSeric 	  case EX_OSERR:
150883b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1509d5aafa3cSeric 		break;
1510d5aafa3cSeric 
1511d5aafa3cSeric 	  default:
1512f170942cSeric 		q->q_flags |= QBADADDR;
1513d5aafa3cSeric 		break;
1514d5aafa3cSeric 	}
151583b7ddc9Seric }
151683b7ddc9Seric /*
1517c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1518c579ef51Seric **
1519c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1520c579ef51Seric **	violation), so we report those specially.  For other
1521c579ef51Seric **	errors, we choose a status message (into statmsg),
1522c579ef51Seric **	and if it represents an error, we print it.
1523c579ef51Seric **
1524c579ef51Seric **	Parameters:
1525c579ef51Seric **		pid -- pid of mailer.
1526c9be6216Seric **		e -- the current envelope.
1527c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1528c9be6216Seric **			(for error messages).
1529c579ef51Seric **
1530c579ef51Seric **	Returns:
1531c579ef51Seric **		exit code of mailer.
1532c579ef51Seric **
1533c579ef51Seric **	Side Effects:
1534c579ef51Seric **		none.
1535c579ef51Seric */
1536c579ef51Seric 
1537c9be6216Seric endmailer(mci, e, pv)
1538b31e7f2bSeric 	register MCI *mci;
1539c9be6216Seric 	register ENVELOPE *e;
1540c9be6216Seric 	char **pv;
1541c579ef51Seric {
1542588cad61Seric 	int st;
1543c579ef51Seric 
154475889e88Seric 	/* close any connections */
154575889e88Seric 	if (mci->mci_in != NULL)
154676f3d53fSeric 		(void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in");
154775889e88Seric 	if (mci->mci_out != NULL)
154876f3d53fSeric 		(void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out");
154975889e88Seric 	mci->mci_in = mci->mci_out = NULL;
155075889e88Seric 	mci->mci_state = MCIS_CLOSED;
155175889e88Seric 
155233db8731Seric 	/* in the IPC case there is nothing to wait for */
155375889e88Seric 	if (mci->mci_pid == 0)
155433db8731Seric 		return (EX_OK);
155533db8731Seric 
155633db8731Seric 	/* wait for the mailer process to die and collect status */
155775889e88Seric 	st = waitfor(mci->mci_pid);
1558588cad61Seric 	if (st == -1)
155978de67c1Seric 	{
1560c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1561588cad61Seric 		return (EX_SOFTWARE);
1562c579ef51Seric 	}
156333db8731Seric 
1564bf9bc890Seric 	if (WIFEXITED(st))
1565c579ef51Seric 	{
1566bf9bc890Seric 		/* normal death -- return status */
1567bf9bc890Seric 		return (WEXITSTATUS(st));
1568bf9bc890Seric 	}
1569bf9bc890Seric 
1570bf9bc890Seric 	/* it died a horrid death */
1571550092c3Seric 	syserr("451 mailer %s died with signal %o",
1572550092c3Seric 		mci->mci_mailer->m_name, st);
1573c9be6216Seric 
1574c9be6216Seric 	/* log the arguments */
15753c930db3Seric 	if (pv != NULL && e->e_xfp != NULL)
1576c9be6216Seric 	{
1577c9be6216Seric 		register char **av;
1578c9be6216Seric 
1579c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1580c9be6216Seric 		for (av = pv; *av != NULL; av++)
1581c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1582c9be6216Seric 		fprintf(e->e_xfp, "\n");
1583c9be6216Seric 	}
1584c9be6216Seric 
15855f73204aSeric 	ExitStat = EX_TEMPFAIL;
15865f73204aSeric 	return (EX_TEMPFAIL);
1587c579ef51Seric }
1588c579ef51Seric /*
158925a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
159025a99e2eSeric **
159125a99e2eSeric **	Parameters:
159225a99e2eSeric **		stat -- the status code from the mailer (high byte
159325a99e2eSeric **			only; core dumps must have been taken care of
159425a99e2eSeric **			already).
159581161401Seric **		m -- the mailer info for this mailer.
159681161401Seric **		mci -- the mailer connection info -- can be NULL if the
159781161401Seric **			response is given before the connection is made.
15984dee0003Seric **		ctladdr -- the controlling address for the recipient
15994dee0003Seric **			address(es).
160081161401Seric **		e -- the current envelope.
160125a99e2eSeric **
160225a99e2eSeric **	Returns:
1603db8841e9Seric **		none.
160425a99e2eSeric **
160525a99e2eSeric **	Side Effects:
1606c1f9df2cSeric **		Errors may be incremented.
160725a99e2eSeric **		ExitStat may be set.
160825a99e2eSeric */
160925a99e2eSeric 
16104dee0003Seric giveresponse(stat, m, mci, ctladdr, e)
161125a99e2eSeric 	int stat;
1612588cad61Seric 	register MAILER *m;
161381161401Seric 	register MCI *mci;
16144dee0003Seric 	ADDRESS *ctladdr;
1615198d9be0Seric 	ENVELOPE *e;
161625a99e2eSeric {
16179c16475dSeric 	register const char *statmsg;
161825a99e2eSeric 	extern char *SysExMsg[];
161925a99e2eSeric 	register int i;
1620d4bd8f0eSbostic 	extern int N_SysEx;
1621198d9be0Seric 	char buf[MAXLINE];
162225a99e2eSeric 
162313bbc08cSeric 	/*
162413bbc08cSeric 	**  Compute status message from code.
162513bbc08cSeric 	*/
162613bbc08cSeric 
162725a99e2eSeric 	i = stat - EX__BASE;
1628588cad61Seric 	if (stat == 0)
16296fe8c3bcSeric 	{
1630588cad61Seric 		statmsg = "250 Sent";
1631ce5531bdSeric 		if (e->e_statmsg != NULL)
16326fe8c3bcSeric 		{
1633ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
16346fe8c3bcSeric 			statmsg = buf;
16356fe8c3bcSeric 		}
16366fe8c3bcSeric 	}
1637588cad61Seric 	else if (i < 0 || i > N_SysEx)
1638588cad61Seric 	{
1639588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1640588cad61Seric 		stat = EX_UNAVAILABLE;
1641588cad61Seric 		statmsg = buf;
1642588cad61Seric 	}
1643198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1644198d9be0Seric 	{
16457d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1646d4bd8f0eSbostic #ifdef NAMED_BIND
1647f28da541Smiriam 		if (h_errno == TRY_AGAIN)
16484d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1649f28da541Smiriam 		else
1650d4bd8f0eSbostic #endif
1651f28da541Smiriam 		{
16528557d168Seric 			if (errno != 0)
1653d87e85f3Seric 				statmsg = errstring(errno);
1654d87e85f3Seric 			else
1655d87e85f3Seric 			{
1656d87e85f3Seric #ifdef SMTP
1657d87e85f3Seric 				statmsg = SmtpError;
16586c2c3107Seric #else /* SMTP */
1659d87e85f3Seric 				statmsg = NULL;
16606c2c3107Seric #endif /* SMTP */
1661d87e85f3Seric 			}
1662f28da541Smiriam 		}
1663d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1664d87e85f3Seric 		{
166587c9b3e7Seric 			(void) strcat(buf, ": ");
1666d87e85f3Seric 			(void) strcat(buf, statmsg);
16678557d168Seric 		}
1668198d9be0Seric 		statmsg = buf;
1669198d9be0Seric 	}
1670f170942cSeric #ifdef NAMED_BIND
1671f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1672f170942cSeric 	{
16734d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1674f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1675f170942cSeric 		statmsg = buf;
1676f170942cSeric 	}
1677f170942cSeric #endif
167825a99e2eSeric 	else
1679d87e85f3Seric 	{
168025a99e2eSeric 		statmsg = SysExMsg[i];
16817d55540cSeric 		if (*statmsg++ == ':')
16827d55540cSeric 		{
16837d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16847d55540cSeric 			statmsg = buf;
16857d55540cSeric 		}
1686d87e85f3Seric 	}
1687588cad61Seric 
1688588cad61Seric 	/*
1689588cad61Seric 	**  Print the message as appropriate
1690588cad61Seric 	*/
1691588cad61Seric 
1692198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1693df106f0bSeric 	{
1694df106f0bSeric 		extern char MsgBuf[];
1695df106f0bSeric 
1696e12d03eeSeric 		message(&statmsg[4], errstring(errno));
1697df106f0bSeric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1698df106f0bSeric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1699df106f0bSeric 	}
170025a99e2eSeric 	else
170125a99e2eSeric 	{
1702c1f9df2cSeric 		Errors++;
1703e12d03eeSeric 		usrerr(statmsg, errstring(errno));
170425a99e2eSeric 	}
170525a99e2eSeric 
170625a99e2eSeric 	/*
170725a99e2eSeric 	**  Final cleanup.
170825a99e2eSeric 	**	Log a record of the transaction.  Compute the new
170925a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
171025a99e2eSeric 	**	that.
171125a99e2eSeric 	*/
171225a99e2eSeric 
17132f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
17144dee0003Seric 		logdelivery(m, mci, &statmsg[4], ctladdr, e);
1715eb238f8cSeric 
1716eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1717eb238f8cSeric 		setstat(stat);
17181097d4c9Seric 	if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL))
1719198d9be0Seric 	{
1720198d9be0Seric 		if (e->e_message != NULL)
1721198d9be0Seric 			free(e->e_message);
1722198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1723198d9be0Seric 	}
17248557d168Seric 	errno = 0;
1725d4bd8f0eSbostic #ifdef NAMED_BIND
1726f28da541Smiriam 	h_errno = 0;
1727d4bd8f0eSbostic #endif
1728eb238f8cSeric }
1729eb238f8cSeric /*
1730eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1731eb238f8cSeric **
17322c9b4f99Seric **	Care is taken to avoid logging lines that are too long, because
17332c9b4f99Seric **	some versions of syslog have an unfortunate proclivity for core
17342c9b4f99Seric **	dumping.  This is a hack, to be sure, that is at best empirical.
17352c9b4f99Seric **
1736eb238f8cSeric **	Parameters:
173781161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
173881161401Seric **		mci -- the mailer connection info -- can be NULL if the
173981161401Seric **			log is occuring when no connection is active.
174081161401Seric **		stat -- the message to print for the status.
17414dee0003Seric **		ctladdr -- the controlling address for the to list.
174281161401Seric **		e -- the current envelope.
1743eb238f8cSeric **
1744eb238f8cSeric **	Returns:
1745eb238f8cSeric **		none
1746eb238f8cSeric **
1747eb238f8cSeric **	Side Effects:
1748eb238f8cSeric **		none
1749eb238f8cSeric */
1750eb238f8cSeric 
17514dee0003Seric logdelivery(m, mci, stat, ctladdr, e)
175281161401Seric 	MAILER *m;
175381161401Seric 	register MCI *mci;
1754eb238f8cSeric 	char *stat;
17554dee0003Seric 	ADDRESS *ctladdr;
1756b31e7f2bSeric 	register ENVELOPE *e;
17575cf56be3Seric {
1758eb238f8cSeric # ifdef LOG
17594dee0003Seric 	register char *bp;
17602c9b4f99Seric 	register char *p;
17612c9b4f99Seric 	int l;
1762d6acf3eeSeric 	char buf[512];
17639507d1f9Seric 
17643a100e8bSeric #  if (SYSLOG_BUFSIZE) >= 256
17654dee0003Seric 	bp = buf;
17664dee0003Seric 	if (ctladdr != NULL)
17674dee0003Seric 	{
17684dee0003Seric 		strcpy(bp, ", ctladdr=");
1769f62f08c7Seric 		strcat(bp, shortenstring(ctladdr->q_paddr, 83));
17704dee0003Seric 		bp += strlen(bp);
17714dee0003Seric 		if (bitset(QGOODUID, ctladdr->q_flags))
17724dee0003Seric 		{
17734dee0003Seric 			(void) sprintf(bp, " (%d/%d)",
17744dee0003Seric 					ctladdr->q_uid, ctladdr->q_gid);
17754dee0003Seric 			bp += strlen(bp);
17764dee0003Seric 		}
17774dee0003Seric 	}
17784dee0003Seric 
17794dee0003Seric 	(void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
17804dee0003Seric 	bp += strlen(bp);
178181161401Seric 
178248ed5d33Seric 	if (m != NULL)
178371ff6caaSeric 	{
17844dee0003Seric 		(void) strcpy(bp, ", mailer=");
17854dee0003Seric 		(void) strcat(bp, m->m_name);
17864dee0003Seric 		bp += strlen(bp);
178771ff6caaSeric 	}
178848ed5d33Seric 
178948ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
179071ff6caaSeric 	{
179171ff6caaSeric # ifdef DAEMON
1792e2f2f828Seric 		extern SOCKADDR CurHostAddr;
179348ed5d33Seric # endif
179471ff6caaSeric 
17954dee0003Seric 		(void) strcpy(bp, ", relay=");
17964dee0003Seric 		(void) strcat(bp, mci->mci_host);
179748ed5d33Seric 
179848ed5d33Seric # ifdef DAEMON
17992bdfc6b9Seric 		(void) strcat(bp, " [");
18004dee0003Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
18012bdfc6b9Seric 		(void) strcat(bp, "]");
180271ff6caaSeric # endif
180371ff6caaSeric 	}
18049507d1f9Seric 	else
180548ed5d33Seric 	{
180648ed5d33Seric 		char *p = macvalue('h', e);
18079507d1f9Seric 
180848ed5d33Seric 		if (p != NULL && p[0] != '\0')
180948ed5d33Seric 		{
18104dee0003Seric 			(void) strcpy(bp, ", relay=");
18114dee0003Seric 			(void) strcat(bp, p);
181248ed5d33Seric 		}
181348ed5d33Seric 	}
1814cb082c5bSeric 	bp += strlen(bp);
1815d6acf3eeSeric 
18163a100e8bSeric #define STATLEN		(((SYSLOG_BUFSIZE) - 100) / 4)
18173a100e8bSeric #if (STATLEN) < 63
18183a100e8bSeric # undef STATLEN
18193a100e8bSeric # define STATLEN	63
18203a100e8bSeric #endif
18213a100e8bSeric #if (STATLEN) > 203
18223a100e8bSeric # undef STATLEN
18233a100e8bSeric # define STATLEN	203
18243a100e8bSeric #endif
18253a100e8bSeric 
18263a100e8bSeric 	if ((bp - buf) > (sizeof buf - ((STATLEN) + 20)))
18272c9b4f99Seric 	{
18282c9b4f99Seric 		/* desperation move -- truncate data */
18293a100e8bSeric 		bp = buf + sizeof buf - ((STATLEN) + 17);
18302c9b4f99Seric 		strcpy(bp, "...");
18312c9b4f99Seric 		bp += 3;
18322c9b4f99Seric 	}
18332c9b4f99Seric 
18342c9b4f99Seric 	(void) strcpy(bp, ", stat=");
18352c9b4f99Seric 	bp += strlen(bp);
18363a100e8bSeric 
18373a100e8bSeric 	(void) strcpy(bp, shortenstring(stat, (STATLEN)));
18382c9b4f99Seric 
18392c9b4f99Seric 	l = SYSLOG_BUFSIZE - 100 - strlen(buf);
18402c9b4f99Seric 	p = e->e_to;
18412c9b4f99Seric 	while (strlen(p) >= l)
18422c9b4f99Seric 	{
18432c9b4f99Seric 		register char *q = strchr(p + l, ',');
18442c9b4f99Seric 
18452a1b6b73Seric 		if (q == NULL)
18462c9b4f99Seric 			break;
18472c9b4f99Seric 		syslog(LOG_INFO, "%s: to=%.*s [more]%s",
18482c9b4f99Seric 			e->e_id, ++q - p, p, buf);
18492c9b4f99Seric 		p = q;
18502c9b4f99Seric 	}
18512c9b4f99Seric 	syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf);
18523a100e8bSeric 
18533a100e8bSeric #  else		/* we have a very short log buffer size */
18543a100e8bSeric 
18554e797715Seric 	l = SYSLOG_BUFSIZE - 80;
18563a100e8bSeric 	p = e->e_to;
18573a100e8bSeric 	while (strlen(p) >= l)
18583a100e8bSeric 	{
18593a100e8bSeric 		register char *q = strchr(p + l, ',');
18603a100e8bSeric 
18613a100e8bSeric 		if (q == NULL)
18623a100e8bSeric 			break;
18633a100e8bSeric 		syslog(LOG_INFO, "%s: to=%.*s [more]",
18643a100e8bSeric 			e->e_id, ++q - p, p);
18653a100e8bSeric 		p = q;
18663a100e8bSeric 	}
18673a100e8bSeric 	syslog(LOG_INFO, "%s: to=%s", e->e_id, p);
18683a100e8bSeric 
18693a100e8bSeric 	if (ctladdr != NULL)
18703a100e8bSeric 	{
18713a100e8bSeric 		bp = buf;
18723a100e8bSeric 		strcpy(buf, "ctladdr=");
18733a100e8bSeric 		bp += strlen(buf);
18743a100e8bSeric 		strcpy(bp, shortenstring(ctladdr->q_paddr, 83));
18753a100e8bSeric 		bp += strlen(buf);
18763a100e8bSeric 		if (bitset(QGOODUID, ctladdr->q_flags))
18773a100e8bSeric 		{
18783a100e8bSeric 			(void) sprintf(bp, " (%d/%d)",
18793a100e8bSeric 					ctladdr->q_uid, ctladdr->q_gid);
18803a100e8bSeric 			bp += strlen(bp);
18813a100e8bSeric 		}
18823a100e8bSeric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
18833a100e8bSeric 	}
18844e797715Seric 	bp = buf;
18854e797715Seric 	sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
18864e797715Seric 	bp += strlen(bp);
18873a100e8bSeric 
18883a100e8bSeric 	if (m != NULL)
18894e797715Seric 	{
18904e797715Seric 		sprintf(bp, ", mailer=%s", m->m_name);
18914e797715Seric 		bp += strlen(bp);
18924e797715Seric 	}
1893*b056abd0Seric 	syslog(LOG_INFO, "%s: %s", e->e_id, buf);
18943a100e8bSeric 
1895*b056abd0Seric 	buf[0] = '\0';
18963a100e8bSeric 	if (mci != NULL && mci->mci_host != NULL)
18973a100e8bSeric 	{
18983a100e8bSeric # ifdef DAEMON
18993a100e8bSeric 		extern SOCKADDR CurHostAddr;
19003a100e8bSeric # endif
19013a100e8bSeric 
1902*b056abd0Seric 		sprintf(buf, ", relay=%s", mci->mci_host);
19033a100e8bSeric 
19043a100e8bSeric # ifdef DAEMON
1905*b056abd0Seric 		(void) strcat(buf, " [");
1906*b056abd0Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
1907*b056abd0Seric 		(void) strcat(buf, "]");
19083a100e8bSeric # endif
19093a100e8bSeric 	}
19103a100e8bSeric 	else
19113a100e8bSeric 	{
19123a100e8bSeric 		char *p = macvalue('h', e);
19133a100e8bSeric 
19143a100e8bSeric 		if (p != NULL && p[0] != '\0')
1915*b056abd0Seric 			sprintf(buf, ", relay=%s", p);
19163a100e8bSeric 	}
1917*b056abd0Seric 	if (buf[0] != '\0')
19184e797715Seric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
19193a100e8bSeric 
19203a100e8bSeric 	syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63));
19213a100e8bSeric #  endif /* short log buffer */
19226c2c3107Seric # endif /* LOG */
192325a99e2eSeric }
192425a99e2eSeric /*
192551552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
192625a99e2eSeric **
192751552439Seric **	This can be made an arbitrary message separator by changing $l
192851552439Seric **
19299b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
19309b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
19319b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
19329b6c17a6Seric **	this kind of antique garbage????
193325a99e2eSeric **
193425a99e2eSeric **	Parameters:
19355aa0f353Seric **		mci -- the connection information.
19365aa0f353Seric **		e -- the envelope.
193725a99e2eSeric **
193825a99e2eSeric **	Returns:
193951552439Seric **		none
194025a99e2eSeric **
194125a99e2eSeric **	Side Effects:
194251552439Seric **		outputs some text to fp.
194325a99e2eSeric */
194425a99e2eSeric 
19455aa0f353Seric putfromline(mci, e)
19465aa0f353Seric 	register MCI *mci;
1947b31e7f2bSeric 	ENVELOPE *e;
194825a99e2eSeric {
19492bc47524Seric 	char *template = "\201l\n";
195051552439Seric 	char buf[MAXLINE];
195125a99e2eSeric 
19525aa0f353Seric 	if (bitnset(M_NHDR, mci->mci_mailer->m_flags))
195351552439Seric 		return;
195413bbc08cSeric 
19552c7e1b8dSeric # ifdef UGLYUUCP
19565aa0f353Seric 	if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags))
195774b6e67bSeric 	{
1958ea09d6edSeric 		char *bang;
1959ea09d6edSeric 		char xbuf[MAXLINE];
196074b6e67bSeric 
1961ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
19626c2c3107Seric 		bang = strchr(buf, '!');
196374b6e67bSeric 		if (bang == NULL)
196434fcca25Seric 		{
196534fcca25Seric 			errno = 0;
196634fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
196734fcca25Seric 		}
196874b6e67bSeric 		else
1969588cad61Seric 		{
1970ea09d6edSeric 			*bang++ = '\0';
19712bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1972ea09d6edSeric 			template = xbuf;
197374b6e67bSeric 		}
1974588cad61Seric 	}
19756c2c3107Seric # endif /* UGLYUUCP */
1976b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
19775aa0f353Seric 	putline(buf, mci);
1978bc6e2962Seric }
1979bc6e2962Seric /*
198051552439Seric **  PUTBODY -- put the body of a message.
198151552439Seric **
198251552439Seric **	Parameters:
19835aa0f353Seric **		mci -- the connection information.
19849a6a5f55Seric **		e -- the envelope to put out.
198503c02fdeSeric **		separator -- if non-NULL, a message separator that must
198603c02fdeSeric **			not be permitted in the resulting message.
198751552439Seric **
198851552439Seric **	Returns:
198951552439Seric **		none.
199051552439Seric **
199151552439Seric **	Side Effects:
199251552439Seric **		The message is written onto fp.
199351552439Seric */
199451552439Seric 
19955aa0f353Seric putbody(mci, e, separator)
19965aa0f353Seric 	register MCI *mci;
19979a6a5f55Seric 	register ENVELOPE *e;
199803c02fdeSeric 	char *separator;
199951552439Seric {
200077b52738Seric 	char buf[MAXLINE];
200151552439Seric 
200251552439Seric 	/*
200351552439Seric 	**  Output the body of the message
200451552439Seric 	*/
200551552439Seric 
20069a6a5f55Seric 	if (e->e_dfp == NULL)
200751552439Seric 	{
20089a6a5f55Seric 		if (e->e_df != NULL)
20099a6a5f55Seric 		{
20109a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
20119a6a5f55Seric 			if (e->e_dfp == NULL)
20128f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
2013e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
20149a6a5f55Seric 		}
20159a6a5f55Seric 		else
20165aa0f353Seric 			putline("<<< No Message Collected >>>", mci);
20179a6a5f55Seric 	}
20189a6a5f55Seric 	if (e->e_dfp != NULL)
20199a6a5f55Seric 	{
20209a6a5f55Seric 		rewind(e->e_dfp);
20215aa0f353Seric 		while (!ferror(mci->mci_out) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
202224fc8aeeSeric 		{
20235aa0f353Seric 			if (buf[0] == 'F' &&
20245aa0f353Seric 			    bitnset(M_ESCFROM, mci->mci_mailer->m_flags) &&
2025d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
20265aa0f353Seric 				(void) putc('>', mci->mci_out);
202703c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
202803c02fdeSeric 			{
202903c02fdeSeric 				/* possible separator */
203003c02fdeSeric 				int sl = strlen(separator);
203103c02fdeSeric 
203203c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
20335aa0f353Seric 					(void) putc(' ', mci->mci_out);
203403c02fdeSeric 			}
20355aa0f353Seric 			putline(buf, mci);
203624fc8aeeSeric 		}
203751552439Seric 
20389a6a5f55Seric 		if (ferror(e->e_dfp))
203951552439Seric 		{
2040df106f0bSeric 			syserr("putbody: %s: read error", e->e_df);
204151552439Seric 			ExitStat = EX_IOERR;
204251552439Seric 		}
204351552439Seric 	}
204451552439Seric 
20450890ba1fSeric 	/* some mailers want extra blank line at end of message */
20465aa0f353Seric 	if (bitnset(M_BLANKEND, mci->mci_mailer->m_flags) &&
20475aa0f353Seric 	    buf[0] != '\0' && buf[0] != '\n')
20485aa0f353Seric 		putline("", mci);
20490890ba1fSeric 
20505aa0f353Seric 	(void) fflush(mci->mci_out);
20515aa0f353Seric 	if (ferror(mci->mci_out) && errno != EPIPE)
205251552439Seric 	{
205351552439Seric 		syserr("putbody: write error");
205451552439Seric 		ExitStat = EX_IOERR;
205551552439Seric 	}
205651552439Seric 	errno = 0;
205725a99e2eSeric }
205825a99e2eSeric /*
205925a99e2eSeric **  MAILFILE -- Send a message to a file.
206025a99e2eSeric **
2061f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
2062f129ec7dSeric **	bits, sendmail will try to become the owner of that file
2063f129ec7dSeric **	rather than the real user.  Obviously, this only works if
2064f129ec7dSeric **	sendmail runs as root.
2065f129ec7dSeric **
2066588cad61Seric **	This could be done as a subordinate mailer, except that it
2067588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
2068588cad61Seric **	view this as being sufficiently important as to include it
2069588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
2070588cad61Seric **	to create another process plus some pipes to save the message.
2071588cad61Seric **
207225a99e2eSeric **	Parameters:
207325a99e2eSeric **		filename -- the name of the file to send to.
20746259796dSeric **		ctladdr -- the controlling address header -- includes
20756259796dSeric **			the userid/groupid to be when sending.
207625a99e2eSeric **
207725a99e2eSeric **	Returns:
207825a99e2eSeric **		The exit code associated with the operation.
207925a99e2eSeric **
208025a99e2eSeric **	Side Effects:
208125a99e2eSeric **		none.
208225a99e2eSeric */
208325a99e2eSeric 
2084b31e7f2bSeric mailfile(filename, ctladdr, e)
208525a99e2eSeric 	char *filename;
20866259796dSeric 	ADDRESS *ctladdr;
2087b31e7f2bSeric 	register ENVELOPE *e;
208825a99e2eSeric {
208925a99e2eSeric 	register FILE *f;
209032d19d43Seric 	register int pid;
209115d084d5Seric 	int mode;
209225a99e2eSeric 
2093671745f3Seric 	if (tTd(11, 1))
2094671745f3Seric 	{
2095671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
2096671745f3Seric 		printaddr(ctladdr, FALSE);
2097671745f3Seric 	}
2098671745f3Seric 
2099f170942cSeric 	if (e->e_xfp != NULL)
2100f170942cSeric 		fflush(e->e_xfp);
2101f170942cSeric 
210232d19d43Seric 	/*
210332d19d43Seric 	**  Fork so we can change permissions here.
210432d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
210532d19d43Seric 	**	the complications of calling subroutines, etc.
210632d19d43Seric 	*/
210732d19d43Seric 
210832d19d43Seric 	DOFORK(fork);
210932d19d43Seric 
211032d19d43Seric 	if (pid < 0)
211132d19d43Seric 		return (EX_OSERR);
211232d19d43Seric 	else if (pid == 0)
211332d19d43Seric 	{
211432d19d43Seric 		/* child -- actually write to file */
2115f129ec7dSeric 		struct stat stb;
21165aa0f353Seric 		MCI mcibuf;
2117f129ec7dSeric 
21182b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
21192b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
21202b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
21213462ad9eSeric 		(void) umask(OldUmask);
212295f16dc0Seric 
2123f129ec7dSeric 		if (stat(filename, &stb) < 0)
21243a98e7eaSeric 			stb.st_mode = FileMode;
212515d084d5Seric 		mode = stb.st_mode;
212695f16dc0Seric 
212795f16dc0Seric 		/* limit the errors to those actually caused in the child */
212895f16dc0Seric 		errno = 0;
212995f16dc0Seric 		ExitStat = EX_OK;
213095f16dc0Seric 
2131f129ec7dSeric 		if (bitset(0111, stb.st_mode))
2132f129ec7dSeric 			exit(EX_CANTCREAT);
213319428781Seric 		if (ctladdr != NULL)
213415d084d5Seric 		{
213515d084d5Seric 			/* ignore setuid and setgid bits */
213615d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
213715d084d5Seric 		}
213815d084d5Seric 
21398f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
21408f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
21418f9146b0Srick 		{
21428f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
214395f16dc0Seric 			if (e->e_dfp == NULL)
214495f16dc0Seric 			{
21458f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
2146e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
21478f9146b0Srick 			}
21488f9146b0Srick 		}
21498f9146b0Srick 
215015d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
2151e36b99e2Seric 		{
215219428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
215395f16dc0Seric 			{
2154898a126bSbostic 				(void) initgroups(DefUser, DefGid);
215595f16dc0Seric 			}
215695f16dc0Seric 			else
215795f16dc0Seric 			{
2158898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
2159898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
2160898a126bSbostic 					ctladdr->q_gid);
2161898a126bSbostic 			}
2162e36b99e2Seric 		}
216315d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
2164e36b99e2Seric 		{
216519428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
2166e36b99e2Seric 				(void) setuid(DefUid);
2167e36b99e2Seric 			else
21686259796dSeric 				(void) setuid(ctladdr->q_uid);
2169e36b99e2Seric 		}
217095f16dc0Seric 		FileName = filename;
217195f16dc0Seric 		LineNumber = 0;
21723a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
217325a99e2eSeric 		if (f == NULL)
217495f16dc0Seric 		{
2175b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
217632d19d43Seric 			exit(EX_CANTCREAT);
217795f16dc0Seric 		}
217825a99e2eSeric 
21795aa0f353Seric 		bzero(&mcibuf, sizeof mcibuf);
21805aa0f353Seric 		mcibuf.mci_mailer = FileMailer;
21815aa0f353Seric 		mcibuf.mci_out = f;
21825aa0f353Seric 		if (bitnset(M_7BITS, FileMailer->m_flags))
21835aa0f353Seric 			mcibuf.mci_flags |= MCIF_7BIT;
21845aa0f353Seric 
21855aa0f353Seric 		putfromline(&mcibuf, e);
21865aa0f353Seric 		(*e->e_puthdr)(&mcibuf, e);
21875aa0f353Seric 		putline("\n", &mcibuf);
21885aa0f353Seric 		(*e->e_putbody)(&mcibuf, e, NULL);
21895aa0f353Seric 		putline("\n", &mcibuf);
219095f16dc0Seric 		if (ferror(f))
219195f16dc0Seric 		{
2192b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
219395f16dc0Seric 			setstat(EX_IOERR);
219495f16dc0Seric 		}
2195ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
219632d19d43Seric 		(void) fflush(stdout);
2197e36b99e2Seric 
219827628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
2199c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
220095f16dc0Seric 		exit(ExitStat);
220113bbc08cSeric 		/*NOTREACHED*/
220232d19d43Seric 	}
220332d19d43Seric 	else
220432d19d43Seric 	{
220532d19d43Seric 		/* parent -- wait for exit status */
2206588cad61Seric 		int st;
220732d19d43Seric 
2208588cad61Seric 		st = waitfor(pid);
2209bf9bc890Seric 		if (WIFEXITED(st))
2210bf9bc890Seric 			return (WEXITSTATUS(st));
2211588cad61Seric 		else
2212b6a0de9dSeric 		{
2213b6a0de9dSeric 			syserr("child died on signal %d", st);
2214bf9bc890Seric 			return (EX_UNAVAILABLE);
2215b6a0de9dSeric 		}
22168f9146b0Srick 		/*NOTREACHED*/
221732d19d43Seric 	}
221825a99e2eSeric }
2219ea4dc939Seric /*
2220e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2221e103b48fSeric **
2222e103b48fSeric **	The signature describes how we are going to send this -- it
2223e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2224e103b48fSeric **	an ordered list of MX hosts.
2225e103b48fSeric **
2226e103b48fSeric **	Parameters:
2227e103b48fSeric **		m -- the mailer describing this host.
2228e103b48fSeric **		host -- the host name.
2229e103b48fSeric **		e -- the current envelope.
2230e103b48fSeric **
2231e103b48fSeric **	Returns:
2232e103b48fSeric **		The signature for this host.
2233e103b48fSeric **
2234e103b48fSeric **	Side Effects:
2235e103b48fSeric **		Can tweak the symbol table.
2236e103b48fSeric */
2237e103b48fSeric 
2238e103b48fSeric char *
2239e103b48fSeric hostsignature(m, host, e)
2240e103b48fSeric 	register MAILER *m;
2241e103b48fSeric 	char *host;
2242e103b48fSeric 	ENVELOPE *e;
2243e103b48fSeric {
2244e103b48fSeric 	register char *p;
2245e103b48fSeric 	register STAB *s;
2246e103b48fSeric 	int i;
2247e103b48fSeric 	int len;
2248e103b48fSeric #ifdef NAMED_BIND
2249e103b48fSeric 	int nmx;
2250e103b48fSeric 	auto int rcode;
2251bafdc4e5Seric 	char *hp;
2252bafdc4e5Seric 	char *endp;
2253516782b4Seric 	int oldoptions;
2254e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2255e103b48fSeric #endif
2256e103b48fSeric 
2257e103b48fSeric 	/*
2258e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2259e103b48fSeric 	*/
2260e103b48fSeric 
2261e103b48fSeric 	p = m->m_mailer;
2262e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2263e103b48fSeric 	{
2264e103b48fSeric 		/* just an ordinary mailer */
2265e103b48fSeric 		return host;
2266e103b48fSeric 	}
2267e103b48fSeric 
2268e103b48fSeric 	/*
2269e103b48fSeric 	**  Look it up in the symbol table.
2270e103b48fSeric 	*/
2271e103b48fSeric 
2272e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2273e103b48fSeric 	if (s->s_hostsig != NULL)
2274e103b48fSeric 		return s->s_hostsig;
2275e103b48fSeric 
2276e103b48fSeric 	/*
2277e103b48fSeric 	**  Not already there -- create a signature.
2278e103b48fSeric 	*/
2279e103b48fSeric 
2280e103b48fSeric #ifdef NAMED_BIND
2281516782b4Seric 	if (ConfigLevel < 2)
2282516782b4Seric 	{
2283516782b4Seric 		oldoptions = _res.options;
2284516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2285516782b4Seric 	}
2286516782b4Seric 
2287bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2288bafdc4e5Seric 	{
2289bafdc4e5Seric 		endp = strchr(hp, ':');
2290bafdc4e5Seric 		if (endp != NULL)
2291bafdc4e5Seric 			*endp = '\0';
2292bafdc4e5Seric 
22937bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
22947d55540cSeric 
2295e103b48fSeric 		if (nmx <= 0)
2296e103b48fSeric 		{
2297e103b48fSeric 			register MCI *mci;
2298e103b48fSeric 
2299e103b48fSeric 			/* update the connection info for this host */
2300bafdc4e5Seric 			mci = mci_get(hp, m);
2301e103b48fSeric 			mci->mci_exitstat = rcode;
2302e103b48fSeric 			mci->mci_errno = errno;
2303f170942cSeric #ifdef NAMED_BIND
2304f170942cSeric 			mci->mci_herrno = h_errno;
2305f170942cSeric #endif
2306e103b48fSeric 
2307e103b48fSeric 			/* and return the original host name as the signature */
2308bafdc4e5Seric 			nmx = 1;
2309bafdc4e5Seric 			mxhosts[0] = hp;
2310e103b48fSeric 		}
2311e103b48fSeric 
2312e103b48fSeric 		len = 0;
2313e103b48fSeric 		for (i = 0; i < nmx; i++)
2314e103b48fSeric 		{
2315e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2316e103b48fSeric 		}
2317bafdc4e5Seric 		if (s->s_hostsig != NULL)
2318bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2319bafdc4e5Seric 		p = xalloc(len);
2320bafdc4e5Seric 		if (s->s_hostsig != NULL)
2321bafdc4e5Seric 		{
2322bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2323bafdc4e5Seric 			free(s->s_hostsig);
2324bafdc4e5Seric 			s->s_hostsig = p;
2325bafdc4e5Seric 			p += strlen(p);
2326bafdc4e5Seric 			*p++ = ':';
2327bafdc4e5Seric 		}
2328bafdc4e5Seric 		else
2329bafdc4e5Seric 			s->s_hostsig = p;
2330e103b48fSeric 		for (i = 0; i < nmx; i++)
2331e103b48fSeric 		{
2332e103b48fSeric 			if (i != 0)
2333e103b48fSeric 				*p++ = ':';
2334e103b48fSeric 			strcpy(p, mxhosts[i]);
2335e103b48fSeric 			p += strlen(p);
2336e103b48fSeric 		}
2337bafdc4e5Seric 		if (endp != NULL)
2338bafdc4e5Seric 			*endp++ = ':';
2339bafdc4e5Seric 	}
2340e103b48fSeric 	makelower(s->s_hostsig);
2341516782b4Seric 	if (ConfigLevel < 2)
2342516782b4Seric 		_res.options = oldoptions;
2343e103b48fSeric #else
2344e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2345e103b48fSeric 	s->s_hostsig = host;
2346e103b48fSeric #endif
2347e103b48fSeric 	if (tTd(17, 1))
2348e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2349e103b48fSeric 	return s->s_hostsig;
2350e103b48fSeric }
2351