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*27607809Seric static char sccsid[] = "@(#)deliver.c	8.74 (Berkeley) 02/26/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;
50b056abd0Seric 	register ENVELOPE *ee;
51b056abd0Seric 	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;
101*27607809Seric 		e->e_flags |= EF_FATALERRS|EF_PM_NOTIFY|EF_CLRQUEUE;
1024bb8b0fdSeric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
1039c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
104f7869e68Seric 			RealHostName == NULL ? "localhost" : RealHostName,
105f7869e68Seric 			e->e_sendqueue->q_paddr);
1069c9e68d9Seric 		return;
1079c9e68d9Seric 	}
1089c9e68d9Seric 
109b8004690Seric 	/*
110b8004690Seric 	**  Do sender deletion.
111b8004690Seric 	**
112b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
113b8004690Seric 	**	This can happen if the name server is hosed when you
114b8004690Seric 	**	are trying to send mail.  The result is that the sender
115b8004690Seric 	**	is instantiated in the queue as a recipient.
116b8004690Seric 	*/
117b8004690Seric 
118e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
119e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1209c9e68d9Seric 	{
1219c9e68d9Seric 		if (tTd(13, 5))
1229c9e68d9Seric 		{
1239c9e68d9Seric 			printf("sendall: QDONTSEND ");
1249c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1259c9e68d9Seric 		}
1269c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1279c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1289c9e68d9Seric 	}
1299c9e68d9Seric 
130ce5531bdSeric 	/*
131ce5531bdSeric 	**  Handle alias owners.
132ce5531bdSeric 	**
133ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
134ce5531bdSeric 	**	We discard owners that are the same as the return path.
135ce5531bdSeric 	*/
136ce5531bdSeric 
137ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
138ce5531bdSeric 	{
139ce5531bdSeric 		register struct address *a;
140ce5531bdSeric 
141ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
142ce5531bdSeric 			continue;
143ce5531bdSeric 		if (a != NULL)
144ce5531bdSeric 			q->q_owner = a->q_owner;
145ce5531bdSeric 
146ce5531bdSeric 		if (q->q_owner != NULL &&
147ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
148ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
149ce5531bdSeric 			q->q_owner = NULL;
150ce5531bdSeric 	}
151ce5531bdSeric 
152ce5531bdSeric 	owner = "";
153ce5531bdSeric 	otherowners = 1;
154ce5531bdSeric 	while (owner != NULL && otherowners > 0)
155ce5531bdSeric 	{
156ce5531bdSeric 		owner = NULL;
157ce5531bdSeric 		otherowners = 0;
158ce5531bdSeric 
159ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
160ce5531bdSeric 		{
161ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
162ce5531bdSeric 				continue;
163ce5531bdSeric 
164ce5531bdSeric 			if (q->q_owner != NULL)
165ce5531bdSeric 			{
166ce5531bdSeric 				if (owner == NULL)
167ce5531bdSeric 					owner = q->q_owner;
168ce5531bdSeric 				else if (owner != q->q_owner)
169ce5531bdSeric 				{
170ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
171ce5531bdSeric 					{
172ce5531bdSeric 						/* make future comparisons cheap */
173ce5531bdSeric 						q->q_owner = owner;
174ce5531bdSeric 					}
175ce5531bdSeric 					else
176ce5531bdSeric 					{
177ce5531bdSeric 						otherowners++;
178ce5531bdSeric 					}
179ce5531bdSeric 					owner = q->q_owner;
180ce5531bdSeric 				}
181ce5531bdSeric 			}
182ce5531bdSeric 			else
183ce5531bdSeric 			{
184ce5531bdSeric 				otherowners++;
185ce5531bdSeric 			}
186ce5531bdSeric 		}
187ce5531bdSeric 
188ce5531bdSeric 		if (owner != NULL && otherowners > 0)
189ce5531bdSeric 		{
190ce5531bdSeric 			extern HDR *copyheader();
191ce5531bdSeric 			extern ADDRESS *copyqueue();
192ce5531bdSeric 
193ce5531bdSeric 			/*
194ce5531bdSeric 			**  Split this envelope into two.
195ce5531bdSeric 			*/
196ce5531bdSeric 
197ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
198ce5531bdSeric 			*ee = *e;
199ce5531bdSeric 			ee->e_id = NULL;
200ce5531bdSeric 			(void) queuename(ee, '\0');
201ce5531bdSeric 
202ce5531bdSeric 			if (tTd(13, 1))
203ce5531bdSeric 				printf("sendall: split %s into %s\n",
204ce5531bdSeric 					e->e_id, ee->e_id);
205ce5531bdSeric 
206ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
207ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
208ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
209ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
210ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
211ce5531bdSeric 			if (tTd(13, 5))
212ce5531bdSeric 			{
213ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
214ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
215ce5531bdSeric 			}
216ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
217ce5531bdSeric 			ee->e_dfp = NULL;
218ce5531bdSeric 			ee->e_xfp = NULL;
219ce5531bdSeric 			ee->e_df = NULL;
220ce5531bdSeric 			ee->e_errormode = EM_MAIL;
221b056abd0Seric 			ee->e_sibling = splitenv;
222b056abd0Seric 			splitenv = ee;
223ce5531bdSeric 
224ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
225ce5531bdSeric 				if (q->q_owner == owner)
226ce5531bdSeric 					q->q_flags |= QDONTSEND;
227ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
228ce5531bdSeric 				if (q->q_owner != owner)
229ce5531bdSeric 					q->q_flags |= QDONTSEND;
230ce5531bdSeric 
231ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
232ce5531bdSeric 			{
233ce5531bdSeric 				ee->e_dfp = NULL;
234da662164Seric 				ee->e_df = queuename(ee, 'd');
235da662164Seric 				ee->e_df = newstr(ee->e_df);
236ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
237ce5531bdSeric 				{
238ce5531bdSeric 					syserr("sendall: link(%s, %s)",
239ce5531bdSeric 						e->e_df, ee->e_df);
240ce5531bdSeric 				}
241ce5531bdSeric 			}
242ce5531bdSeric #ifdef LOG
243ce5531bdSeric 			if (LogLevel > 4)
244c8b70947Seric 				syslog(LOG_INFO, "%s: clone %s, owner=%s",
245c8b70947Seric 					ee->e_id, e->e_id, owner);
246ce5531bdSeric #endif
247ce5531bdSeric 		}
248ce5531bdSeric 	}
249ce5531bdSeric 
250ce5531bdSeric 	if (owner != NULL)
251ce5531bdSeric 	{
252ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
253ce5531bdSeric 		if (tTd(13, 5))
254ce5531bdSeric 		{
255ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
256ce5531bdSeric 			printaddr(&e->e_from, FALSE);
257ce5531bdSeric 		}
258ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
259ce5531bdSeric 		e->e_errormode = EM_MAIL;
260ce5531bdSeric 	}
261ce5531bdSeric 
262c1ac89b1Seric # ifdef QUEUE
263c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
264c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
265c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
266c1ac89b1Seric 	{
267c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2686103d9b4Seric 		queueup(e, TRUE, announcequeueup);
269b056abd0Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
270b056abd0Seric 			queueup(ee, TRUE, announcequeueup);
271c1ac89b1Seric 	}
272c1ac89b1Seric #endif /* QUEUE */
273c1ac89b1Seric 
274b056abd0Seric 	if (splitenv != NULL)
275b056abd0Seric 	{
276b056abd0Seric 		if (tTd(13, 1))
277b056abd0Seric 		{
278b056abd0Seric 			printf("\nsendall: Split queue; remaining queue:\n");
279b056abd0Seric 			printaddr(e->e_sendqueue, TRUE);
280b056abd0Seric 		}
281b056abd0Seric 
282b056abd0Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
283b056abd0Seric 		{
284b056abd0Seric 			CurEnv = ee;
285562ec147Seric 			if (mode != SM_VERIFY)
286562ec147Seric 				openxscript(ee);
287b056abd0Seric 			sendenvelope(ee, mode);
288562ec147Seric 			dropenvelope(ee);
289b056abd0Seric 		}
290b056abd0Seric 
291b056abd0Seric 		CurEnv = e;
292b056abd0Seric 	}
293b056abd0Seric 	sendenvelope(e, mode);
294b056abd0Seric }
295b056abd0Seric 
296b056abd0Seric sendenvelope(e, mode)
297b056abd0Seric 	register ENVELOPE *e;
298b056abd0Seric 	char mode;
299b056abd0Seric {
300b056abd0Seric 	bool oldverbose;
301b056abd0Seric 	int pid;
302b056abd0Seric 	register ADDRESS *q;
303b056abd0Seric 	char *qf;
304b056abd0Seric 	char *id;
305b056abd0Seric 
3067880f3e4Seric 	/*
3077880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3087880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3097880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3107880f3e4Seric 	**  addresses to be sent.
3117880f3e4Seric 	*/
3127880f3e4Seric 
3138d19a23dSeric 	if (bitset(EF_FATALERRS, e->e_flags) &&
3148d19a23dSeric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
3157880f3e4Seric 	{
3167880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3177880f3e4Seric 		return;
3187880f3e4Seric 	}
3197880f3e4Seric 
320ce5531bdSeric 	oldverbose = Verbose;
321c1ac89b1Seric 	switch (mode)
322c1ac89b1Seric 	{
323c1ac89b1Seric 	  case SM_VERIFY:
324c1ac89b1Seric 		Verbose = TRUE;
325c1ac89b1Seric 		break;
326c1ac89b1Seric 
327c1ac89b1Seric 	  case SM_QUEUE:
328c1ac89b1Seric   queueonly:
329c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
330c1ac89b1Seric 		return;
331c1ac89b1Seric 
332c1ac89b1Seric 	  case SM_FORK:
333c1ac89b1Seric 		if (e->e_xfp != NULL)
334c1ac89b1Seric 			(void) fflush(e->e_xfp);
335c1ac89b1Seric 
336b269aa8eSeric # if !HASFLOCK
337c1ac89b1Seric 		/*
3386b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3396b2765c6Seric 		**  the lock is owned by a process, not by an open file
3406b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3416b2765c6Seric 		**  then restart from scratch in the child.
342c1ac89b1Seric 		*/
343c1ac89b1Seric 
3446b2765c6Seric 		/* save id for future use */
3456b2765c6Seric 		id = e->e_id;
3466b2765c6Seric 
3476b2765c6Seric 		/* now drop the envelope in the parent */
3486b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3496b2765c6Seric 		dropenvelope(e);
3506b2765c6Seric 
3516b2765c6Seric 		/* and reacquire in the child */
3526b2765c6Seric 		(void) dowork(id, TRUE, FALSE, e);
3536b2765c6Seric 
3546b2765c6Seric 		return;
3556b2765c6Seric 
3566b2765c6Seric # else /* HASFLOCK */
357c1ac89b1Seric 
358c1ac89b1Seric 		pid = fork();
359c1ac89b1Seric 		if (pid < 0)
360c1ac89b1Seric 		{
361c1ac89b1Seric 			goto queueonly;
362c1ac89b1Seric 		}
363c1ac89b1Seric 		else if (pid > 0)
364c1ac89b1Seric 		{
3650e484fe5Seric 			/* be sure we leave the temp files to our child */
3660e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3670e484fe5Seric 			if (e->e_lockfp != NULL)
3680e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3690e484fe5Seric 			e->e_lockfp = NULL;
3700e484fe5Seric 
3710e484fe5Seric 			/* close any random open files in the envelope */
3720e484fe5Seric 			closexscript(e);
3730e484fe5Seric 			if (e->e_dfp != NULL)
3740e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3750e484fe5Seric 			e->e_dfp = NULL;
3760e484fe5Seric 			e->e_id = e->e_df = NULL;
3779f9b003eSeric 
3789f9b003eSeric 			/* catch intermediate zombie */
3799f9b003eSeric 			(void) waitfor(pid);
380c1ac89b1Seric 			return;
381c1ac89b1Seric 		}
382c1ac89b1Seric 
383c1ac89b1Seric 		/* double fork to avoid zombies */
3849f9b003eSeric 		pid = fork();
3859f9b003eSeric 		if (pid > 0)
386c1ac89b1Seric 			exit(EX_OK);
387c1ac89b1Seric 
388c1ac89b1Seric 		/* be sure we are immune from the terminal */
3897880f3e4Seric 		disconnect(1, e);
390c1ac89b1Seric 
3919f9b003eSeric 		/* prevent parent from waiting if there was an error */
3929f9b003eSeric 		if (pid < 0)
3939f9b003eSeric 		{
3949f9b003eSeric 			e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3959f9b003eSeric 			finis();
3969f9b003eSeric 		}
3979f9b003eSeric 
398c1ac89b1Seric 		/*
399c1ac89b1Seric 		**  Close any cached connections.
400c1ac89b1Seric 		**
401c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
402c1ac89b1Seric 		**	still knows about the connection.
403c1ac89b1Seric 		**
404c1ac89b1Seric 		**	This should only happen when delivering an error
405c1ac89b1Seric 		**	message.
406c1ac89b1Seric 		*/
407c1ac89b1Seric 
408c1ac89b1Seric 		mci_flush(FALSE, NULL);
409c1ac89b1Seric 
4106b2765c6Seric # endif /* HASFLOCK */
4116b2765c6Seric 
412c1ac89b1Seric 		break;
413c1ac89b1Seric 	}
414c1ac89b1Seric 
415c1ac89b1Seric 	/*
4169c9e68d9Seric 	**  Run through the list and send everything.
4175288e21aSeric 	**
4185288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4195288e21aSeric 	**	result in returned mail.
4209c9e68d9Seric 	*/
4219c9e68d9Seric 
4229c9e68d9Seric 	e->e_nsent = 0;
4235288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
424faad2b16Seric 
425faad2b16Seric 	/* now run through the queue */
4269c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4279c9e68d9Seric 	{
428fdc75a0fSeric #ifdef XDEBUG
429fdc75a0fSeric 		char wbuf[MAXNAME + 20];
430fdc75a0fSeric 
431fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
432fdc75a0fSeric 		checkfd012(wbuf);
433fdc75a0fSeric #endif
4349c9e68d9Seric 		if (mode == SM_VERIFY)
4359c9e68d9Seric 		{
4369c9e68d9Seric 			e->e_to = q->q_paddr;
4379c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4388dfca105Seric 			{
439fafe46e1Seric 				if (q->q_host != NULL && q->q_host[0] != '\0')
4408dfca105Seric 					message("deliverable: mailer %s, host %s, user %s",
4418dfca105Seric 						q->q_mailer->m_name,
4428dfca105Seric 						q->q_host,
4438dfca105Seric 						q->q_user);
444fafe46e1Seric 				else
445fafe46e1Seric 					message("deliverable: mailer %s, user %s",
446fafe46e1Seric 						q->q_mailer->m_name,
447fafe46e1Seric 						q->q_user);
4488dfca105Seric 			}
4499c9e68d9Seric 		}
4509c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4519c9e68d9Seric 		{
4529c9e68d9Seric # ifdef QUEUE
4539c9e68d9Seric 			/*
4549c9e68d9Seric 			**  Checkpoint the send list every few addresses
4559c9e68d9Seric 			*/
4569c9e68d9Seric 
4579c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4589c9e68d9Seric 			{
4599c9e68d9Seric 				queueup(e, TRUE, FALSE);
4609c9e68d9Seric 				e->e_nsent = 0;
4619c9e68d9Seric 			}
4629c9e68d9Seric # endif /* QUEUE */
4639c9e68d9Seric 			(void) deliver(e, q);
4649c9e68d9Seric 		}
4659c9e68d9Seric 	}
4669c9e68d9Seric 	Verbose = oldverbose;
4679c9e68d9Seric 
468fdc75a0fSeric #ifdef XDEBUG
469fdc75a0fSeric 	checkfd012("end of sendenvelope");
470fdc75a0fSeric #endif
471fdc75a0fSeric 
4729c9e68d9Seric 	if (mode == SM_FORK)
4739c9e68d9Seric 		finis();
4749c9e68d9Seric }
4759c9e68d9Seric /*
4769c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4779c9e68d9Seric **
4789c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4799c9e68d9Seric **	two processes on the same stack!!!
4809c9e68d9Seric **
4819c9e68d9Seric **	Parameters:
4829c9e68d9Seric **		none.
4839c9e68d9Seric **
4849c9e68d9Seric **	Returns:
4859c9e68d9Seric **		From a macro???  You've got to be kidding!
4869c9e68d9Seric **
4879c9e68d9Seric **	Side Effects:
4889c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4899c9e68d9Seric **			pid of child in parent, zero in child.
4909c9e68d9Seric **			-1 on unrecoverable error.
4919c9e68d9Seric **
4929c9e68d9Seric **	Notes:
4939c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4949c9e68d9Seric **		vfork for you.....
4959c9e68d9Seric */
4969c9e68d9Seric 
4979c9e68d9Seric # define NFORKTRIES	5
4989c9e68d9Seric 
4999c9e68d9Seric # ifndef FORK
5009c9e68d9Seric # define FORK	fork
5019c9e68d9Seric # endif
5029c9e68d9Seric 
5039c9e68d9Seric # define DOFORK(fORKfN) \
5049c9e68d9Seric {\
5059c9e68d9Seric 	register int i;\
5069c9e68d9Seric \
5079c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
5089c9e68d9Seric 	{\
5099c9e68d9Seric 		pid = fORKfN();\
5109c9e68d9Seric 		if (pid >= 0)\
5119c9e68d9Seric 			break;\
5129c9e68d9Seric 		if (i > 0)\
5139c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5149c9e68d9Seric 	}\
5159c9e68d9Seric }
5169c9e68d9Seric /*
5179c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5189c9e68d9Seric **
5199c9e68d9Seric **	Parameters:
5209c9e68d9Seric **		none.
5219c9e68d9Seric **
5229c9e68d9Seric **	Returns:
5239c9e68d9Seric **		pid of child in parent.
5249c9e68d9Seric **		zero in child.
5259c9e68d9Seric **		-1 on error.
5269c9e68d9Seric **
5279c9e68d9Seric **	Side Effects:
5289c9e68d9Seric **		returns twice, once in parent and once in child.
5299c9e68d9Seric */
5309c9e68d9Seric 
5319c9e68d9Seric dofork()
5329c9e68d9Seric {
5339c9e68d9Seric 	register int pid;
5349c9e68d9Seric 
5359c9e68d9Seric 	DOFORK(fork);
5369c9e68d9Seric 	return (pid);
5379c9e68d9Seric }
5389c9e68d9Seric /*
53913bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
54013bbc08cSeric **
54113bbc08cSeric **	This routine delivers to everyone on the same host as the
54213bbc08cSeric **	user on the head of the list.  It is clever about mailers
54313bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
54413bbc08cSeric **	that it will deliver to all these addresses however -- so
54513bbc08cSeric **	deliver should be called once for each address on the
54613bbc08cSeric **	list.
54725a99e2eSeric **
54825a99e2eSeric **	Parameters:
549588cad61Seric **		e -- the envelope to deliver.
550c77d1c25Seric **		firstto -- head of the address list to deliver to.
55125a99e2eSeric **
55225a99e2eSeric **	Returns:
55325a99e2eSeric **		zero -- successfully delivered.
55425a99e2eSeric **		else -- some failure, see ExitStat for more info.
55525a99e2eSeric **
55625a99e2eSeric **	Side Effects:
55725a99e2eSeric **		The standard input is passed off to someone.
55825a99e2eSeric */
55925a99e2eSeric 
560588cad61Seric deliver(e, firstto)
561588cad61Seric 	register ENVELOPE *e;
562c77d1c25Seric 	ADDRESS *firstto;
56325a99e2eSeric {
56478442df3Seric 	char *host;			/* host being sent to */
56578442df3Seric 	char *user;			/* user being sent to */
56625a99e2eSeric 	char **pvp;
5675dfc646bSeric 	register char **mvp;
56825a99e2eSeric 	register char *p;
569588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5706259796dSeric 	ADDRESS *ctladdr;
571b31e7f2bSeric 	register MCI *mci;
572c77d1c25Seric 	register ADDRESS *to = firstto;
573c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
574772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
575911693bfSbostic 	int rcode;			/* response code */
576e103b48fSeric 	char *firstsig;			/* signature of firstto */
5779c9e68d9Seric 	int pid;
5789c9e68d9Seric 	char *curhost;
5799c9e68d9Seric 	int mpvect[2];
5809c9e68d9Seric 	int rpvect[2];
581ee6bf8dfSeric 	char *pv[MAXPV+1];
582579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
583ee6bf8dfSeric 	char buf[MAXNAME];
584c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
585fabb3bd4Seric 	extern int checkcompat();
58625a99e2eSeric 
58735490626Seric 	errno = 0;
588ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5895dfc646bSeric 		return (0);
59025a99e2eSeric 
591134746fbSeric #ifdef NAMED_BIND
592912a731aSbostic 	/* unless interactive, try twice, over a minute */
5938d19a23dSeric 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP)
5948d19a23dSeric 	{
595912a731aSbostic 		_res.retrans = 30;
596912a731aSbostic 		_res.retry = 2;
597912a731aSbostic 	}
598d4bd8f0eSbostic #endif
599912a731aSbostic 
60051552439Seric 	m = to->q_mailer;
60151552439Seric 	host = to->q_host;
602c9be6216Seric 	CurEnv = e;			/* just in case */
6034384d521Seric 	e->e_statmsg = NULL;
604df106f0bSeric 	SmtpError[0] = '\0';
60551552439Seric 
6066ef48975Seric 	if (tTd(10, 1))
607562ec147Seric 		printf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n",
608562ec147Seric 			e->e_id, m->m_name, host, to->q_user);
609ab81ee53Seric 	if (tTd(10, 100))
610ab81ee53Seric 		printopenfds(FALSE);
611f3dbc832Seric 
612f3dbc832Seric 	/*
613f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
614f3dbc832Seric 	**  connections now, just mark these addresses and return.
615f3dbc832Seric 	**	This is useful if we want to batch connections to
616f3dbc832Seric 	**	reduce load.  This will cause the messages to be
617f3dbc832Seric 	**	queued up, and a daemon will come along to send the
618f3dbc832Seric 	**	messages later.
619f3dbc832Seric 	**		This should be on a per-mailer basis.
620f3dbc832Seric 	*/
621f3dbc832Seric 
62279aa5155Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
623f3dbc832Seric 	{
624f3dbc832Seric 		for (; to != NULL; to = to->q_next)
625f4560e80Seric 		{
626ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
627c6e18ac5Seric 			    to->q_mailer != m)
628f4560e80Seric 				continue;
629268a25e1Seric 			to->q_flags |= QQUEUEUP;
630588cad61Seric 			e->e_to = to->q_paddr;
63108b25121Seric 			message("queued");
6322f624c86Seric 			if (LogLevel > 8)
6334dee0003Seric 				logdelivery(m, NULL, "queued", NULL, e);
634f4560e80Seric 		}
635588cad61Seric 		e->e_to = NULL;
636f3dbc832Seric 		return (0);
637f3dbc832Seric 	}
638f3dbc832Seric 
63925a99e2eSeric 	/*
6405dfc646bSeric 	**  Do initial argv setup.
6415dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6425dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6435dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6445dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6455dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6463bea8136Seric 	**		The from address rewrite is expected to make
6473bea8136Seric 	**		the address relative to the other end.
6485dfc646bSeric 	*/
6495dfc646bSeric 
65078442df3Seric 	/* rewrite from address, using rewriting rules */
651efe54562Seric 	rcode = EX_OK;
652efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
653efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
654efe54562Seric 					   &rcode, e));
655ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
656588cad61Seric 	define('h', host, e);			/* to host */
6575dfc646bSeric 	Errors = 0;
6585dfc646bSeric 	pvp = pv;
6595dfc646bSeric 	*pvp++ = m->m_argv[0];
6605dfc646bSeric 
6615dfc646bSeric 	/* insert -f or -r flag as appropriate */
66257fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6635dfc646bSeric 	{
66457fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6655dfc646bSeric 			*pvp++ = "-f";
6665dfc646bSeric 		else
6675dfc646bSeric 			*pvp++ = "-r";
668c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6695dfc646bSeric 	}
6705dfc646bSeric 
6715dfc646bSeric 	/*
6725dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6735dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6745dfc646bSeric 	**  be one of these, and there are only a few more slots
6755dfc646bSeric 	**  in the pv after it.
6765dfc646bSeric 	*/
6775dfc646bSeric 
6785dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6795dfc646bSeric 	{
6802bc47524Seric 		/* can't use strchr here because of sign extension problems */
6812bc47524Seric 		while (*p != '\0')
6822bc47524Seric 		{
6832bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6842bc47524Seric 			{
6852bc47524Seric 				if (*p == 'u')
6865dfc646bSeric 					break;
6872bc47524Seric 			}
6882bc47524Seric 		}
6892bc47524Seric 
6902bc47524Seric 		if (*p != '\0')
6915dfc646bSeric 			break;
6925dfc646bSeric 
6935dfc646bSeric 		/* this entry is safe -- go ahead and process it */
694588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6955dfc646bSeric 		*pvp++ = newstr(buf);
6965dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6975dfc646bSeric 		{
69808b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6995dfc646bSeric 			return (-1);
7005dfc646bSeric 		}
7015dfc646bSeric 	}
702c579ef51Seric 
70333db8731Seric 	/*
70433db8731Seric 	**  If we have no substitution for the user name in the argument
70533db8731Seric 	**  list, we know that we must supply the names otherwise -- and
70633db8731Seric 	**  SMTP is the answer!!
70733db8731Seric 	*/
70833db8731Seric 
7095dfc646bSeric 	if (*mvp == NULL)
710c579ef51Seric 	{
711c579ef51Seric 		/* running SMTP */
7122c7e1b8dSeric # ifdef SMTP
713c579ef51Seric 		clever = TRUE;
714c579ef51Seric 		*pvp = NULL;
7156c2c3107Seric # else /* SMTP */
71633db8731Seric 		/* oops!  we don't implement SMTP */
717df106f0bSeric 		syserr("554 SMTP style mailer not implemented");
7182c7e1b8dSeric 		return (EX_SOFTWARE);
7196c2c3107Seric # endif /* SMTP */
720c579ef51Seric 	}
7215dfc646bSeric 
7225dfc646bSeric 	/*
7235dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7245dfc646bSeric 	**  run through our address list and append all the addresses
7255dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7265dfc646bSeric 	**  always send another copy later.
7275dfc646bSeric 	*/
7285dfc646bSeric 
7295dfc646bSeric 	tobuf[0] = '\0';
730588cad61Seric 	e->e_to = tobuf;
7316259796dSeric 	ctladdr = NULL;
732e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7335dfc646bSeric 	for (; to != NULL; to = to->q_next)
7345dfc646bSeric 	{
7355dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
73657fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7375dfc646bSeric 			break;
7385dfc646bSeric 
7395dfc646bSeric 		/* if already sent or not for this host, don't send */
740ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
741e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
742e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7435dfc646bSeric 			continue;
7446259796dSeric 
7454b22ea87Seric 		/* avoid overflowing tobuf */
746aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7474b22ea87Seric 			break;
7484b22ea87Seric 
7496ef48975Seric 		if (tTd(10, 1))
750772e6e50Seric 		{
751772e6e50Seric 			printf("\nsend to ");
752772e6e50Seric 			printaddr(to, FALSE);
753772e6e50Seric 		}
754772e6e50Seric 
7556259796dSeric 		/* compute effective uid/gid when sending */
756960e665aSeric 		/* XXX perhaps this should be to->q_mailer != LocalMailer ?? */
757960e665aSeric 		/* XXX perhaps it should be a mailer flag? */
758960e665aSeric 		if (to->q_mailer == ProgMailer || to->q_mailer == FileMailer)
7596259796dSeric 			ctladdr = getctladdr(to);
7606259796dSeric 
7615dfc646bSeric 		user = to->q_user;
762588cad61Seric 		e->e_to = to->q_paddr;
76375f1ade9Seric 		if (tTd(10, 5))
76475f1ade9Seric 		{
76575f1ade9Seric 			printf("deliver: QDONTSEND ");
76675f1ade9Seric 			printaddr(to, FALSE);
76775f1ade9Seric 		}
768ee4b0922Seric 		to->q_flags |= QDONTSEND;
7695dfc646bSeric 
7705dfc646bSeric 		/*
7715dfc646bSeric 		**  Check to see that these people are allowed to
7725dfc646bSeric 		**  talk to each other.
7732a6e0786Seric 		*/
7742a6e0786Seric 
77569582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
77669582d2fSeric 		{
77769582d2fSeric 			NoReturn = TRUE;
77808b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
7794dee0003Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e);
78069582d2fSeric 			continue;
78169582d2fSeric 		}
782fabb3bd4Seric 		rcode = checkcompat(to, e);
7831793c9c5Seric 		if (rcode != EX_OK)
7845dfc646bSeric 		{
7851000c37aSeric 			markfailure(e, to, rcode);
7864dee0003Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
7875dfc646bSeric 			continue;
7885dfc646bSeric 		}
7892a6e0786Seric 
7902a6e0786Seric 		/*
7919ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7929ec9501bSeric 		**	about them.
79325a99e2eSeric 		*/
79425a99e2eSeric 
79557fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
79625a99e2eSeric 		{
7971d8f1806Seric 			stripquotes(user);
7981d8f1806Seric 			stripquotes(host);
79925a99e2eSeric 		}
80025a99e2eSeric 
801cdb828c5Seric 		/* hack attack -- delivermail compatibility */
802cdb828c5Seric 		if (m == ProgMailer && *user == '|')
803cdb828c5Seric 			user++;
804cdb828c5Seric 
80525a99e2eSeric 		/*
8063efaed6eSeric 		**  If an error message has already been given, don't
8073efaed6eSeric 		**	bother to send to this address.
8083efaed6eSeric 		**
8093efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
8103efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
8113efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
8123efaed6eSeric 		*/
8133efaed6eSeric 
8146cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8153efaed6eSeric 			continue;
8163efaed6eSeric 
817f2fec898Seric 		/* save statistics.... */
818588cad61Seric 		markstats(e, to);
819f2fec898Seric 
8203efaed6eSeric 		/*
82125a99e2eSeric 		**  See if this user name is "special".
82225a99e2eSeric 		**	If the user name has a slash in it, assume that this
82351552439Seric 		**	is a file -- send it off without further ado.  Note
82451552439Seric 		**	that this type of addresses is not processed along
82551552439Seric 		**	with the others, so we fudge on the To person.
82625a99e2eSeric 		*/
82725a99e2eSeric 
8282c017f8dSeric 		if (m == FileMailer)
82925a99e2eSeric 		{
83003463a75Seric 			rcode = mailfile(user, ctladdr, e);
83103463a75Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
832dde5acadSeric 			if (rcode == EX_OK)
833dde5acadSeric 				to->q_flags |= QSENT;
8345dfc646bSeric 			continue;
83525a99e2eSeric 		}
83625a99e2eSeric 
83713bbc08cSeric 		/*
83813bbc08cSeric 		**  Address is verified -- add this user to mailer
83913bbc08cSeric 		**  argv, and add it to the print list of recipients.
84013bbc08cSeric 		*/
84113bbc08cSeric 
842508daeccSeric 		/* link together the chain of recipients */
843508daeccSeric 		to->q_tchain = tochain;
844508daeccSeric 		tochain = to;
845508daeccSeric 
8465dfc646bSeric 		/* create list of users for error messages */
847db8841e9Seric 		(void) strcat(tobuf, ",");
848db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
849588cad61Seric 		define('u', user, e);		/* to user */
850b6f89e6eSeric 		p = to->q_home;
851b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
852b6f89e6eSeric 			p = ctladdr->q_home;
853b6f89e6eSeric 		define('z', p, e);	/* user's home */
8545dfc646bSeric 
855c579ef51Seric 		/*
856508daeccSeric 		**  Expand out this user into argument list.
857c579ef51Seric 		*/
858c579ef51Seric 
859508daeccSeric 		if (!clever)
860c579ef51Seric 		{
861588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8625dfc646bSeric 			*pvp++ = newstr(buf);
8635dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8645dfc646bSeric 			{
8655dfc646bSeric 				/* allow some space for trailing parms */
8665dfc646bSeric 				break;
8675dfc646bSeric 			}
8685dfc646bSeric 		}
869c579ef51Seric 	}
8705dfc646bSeric 
871145b49b1Seric 	/* see if any addresses still exist */
872145b49b1Seric 	if (tobuf[0] == '\0')
873c579ef51Seric 	{
874588cad61Seric 		define('g', (char *) NULL, e);
875145b49b1Seric 		return (0);
876c579ef51Seric 	}
877145b49b1Seric 
8785dfc646bSeric 	/* print out messages as full list */
87963780dbdSeric 	e->e_to = tobuf + 1;
8805dfc646bSeric 
8815dfc646bSeric 	/*
8825dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8835dfc646bSeric 	*/
8845dfc646bSeric 
885c579ef51Seric 	while (!clever && *++mvp != NULL)
8865dfc646bSeric 	{
887588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8885dfc646bSeric 		*pvp++ = newstr(buf);
8895dfc646bSeric 		if (pvp >= &pv[MAXPV])
89008b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8915dfc646bSeric 	}
8925dfc646bSeric 	*pvp++ = NULL;
8935dfc646bSeric 
89425a99e2eSeric 	/*
89525a99e2eSeric 	**  Call the mailer.
8966328bdf7Seric 	**	The argument vector gets built, pipes
89725a99e2eSeric 	**	are created as necessary, and we fork & exec as
8986328bdf7Seric 	**	appropriate.
899c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
90025a99e2eSeric 	*/
90125a99e2eSeric 
902960e665aSeric 	/*XXX this seems a bit wierd */
9034899f6b5Seric 	if (ctladdr == NULL && m != ProgMailer &&
9044899f6b5Seric 	    bitset(QGOODUID, e->e_from.q_flags))
905960e665aSeric 		ctladdr = &e->e_from;
906960e665aSeric 
907134746fbSeric #ifdef NAMED_BIND
9082bcc6d2dSeric 	if (ConfigLevel < 2)
909912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
910134746fbSeric #endif
9119c9e68d9Seric 
9129c9e68d9Seric 	if (tTd(11, 1))
913134746fbSeric 	{
9149c9e68d9Seric 		printf("openmailer:");
9159c9e68d9Seric 		printav(pv);
9169c9e68d9Seric 	}
9179c9e68d9Seric 	errno = 0;
9189c9e68d9Seric 
9199c9e68d9Seric 	CurHostName = m->m_mailer;
9209c9e68d9Seric 
9219c9e68d9Seric 	/*
9229c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9239c9e68d9Seric 	**  connection.
9249c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9259c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9269c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9279c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9289c9e68d9Seric 	*/
9299c9e68d9Seric 
9309c9e68d9Seric 	curhost = NULL;
931c931b82bSeric 	SmtpPhase = NULL;
932df106f0bSeric 	mci = NULL;
9339c9e68d9Seric 
9347febfd66Seric #ifdef XDEBUG
9357febfd66Seric 	{
9367febfd66Seric 		char wbuf[MAXLINE];
9377febfd66Seric 
9387febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9397febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
9407febfd66Seric 		checkfd012(wbuf);
9417febfd66Seric 	}
9427febfd66Seric #endif
9437febfd66Seric 
9449c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9459c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9469c9e68d9Seric 	{
9479c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9489c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9499c9e68d9Seric 		mci->mci_in = stdin;
9509c9e68d9Seric 		mci->mci_out = stdout;
9519c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9529c9e68d9Seric 		mci->mci_mailer = m;
9539c9e68d9Seric 	}
9549c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9559c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9569c9e68d9Seric 	{
9579c9e68d9Seric #ifdef DAEMON
9589c9e68d9Seric 		register int i;
9599c9e68d9Seric 		register u_short port;
9609c9e68d9Seric 
961a5d44642Seric 		if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
962a5d44642Seric 		{
963a5d44642Seric 			syserr("null host name for %s mailer", m->m_mailer);
964a5d44642Seric 			rcode = EX_CONFIG;
965a5d44642Seric 			goto give_up;
966a5d44642Seric 		}
967a5d44642Seric 
9689c9e68d9Seric 		CurHostName = pv[1];
9699c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9709c9e68d9Seric 
9719c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9729c9e68d9Seric 		{
973a5d44642Seric 			syserr("null host signature for %s", pv[1]);
974845e533cSeric 			rcode = EX_OSERR;
975b31e7f2bSeric 			goto give_up;
976b31e7f2bSeric 		}
9779c9e68d9Seric 
9789c9e68d9Seric 		if (!clever)
9799c9e68d9Seric 		{
9809c9e68d9Seric 			syserr("554 non-clever IPC");
981df106f0bSeric 			rcode = EX_CONFIG;
9829c9e68d9Seric 			goto give_up;
9839c9e68d9Seric 		}
9849c9e68d9Seric 		if (pv[2] != NULL)
9859c9e68d9Seric 			port = atoi(pv[2]);
9869c9e68d9Seric 		else
9879c9e68d9Seric 			port = 0;
9889c9e68d9Seric tryhost:
9899c9e68d9Seric 		while (*curhost != '\0')
9909c9e68d9Seric 		{
9919c9e68d9Seric 			register char *p;
9929c9e68d9Seric 			static char hostbuf[MAXNAME];
9939c9e68d9Seric 
9949c9e68d9Seric 			/* pull the next host from the signature */
9959c9e68d9Seric 			p = strchr(curhost, ':');
9969c9e68d9Seric 			if (p == NULL)
9979c9e68d9Seric 				p = &curhost[strlen(curhost)];
998df106f0bSeric 			if (p == curhost)
999df106f0bSeric 			{
1000df106f0bSeric 				syserr("deliver: null host name in signature");
10018087428aSeric 				curhost++;
1002df106f0bSeric 				continue;
1003df106f0bSeric 			}
10049c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
10059c9e68d9Seric 			hostbuf[p - curhost] = '\0';
10069c9e68d9Seric 			if (*p != '\0')
10079c9e68d9Seric 				p++;
10089c9e68d9Seric 			curhost = p;
10099c9e68d9Seric 
10109c9e68d9Seric 			/* see if we already know that this host is fried */
10119c9e68d9Seric 			CurHostName = hostbuf;
10129c9e68d9Seric 			mci = mci_get(hostbuf, m);
10139c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
10149c9e68d9Seric 			{
10159c9e68d9Seric 				if (tTd(11, 1))
10169c9e68d9Seric 				{
10179c9e68d9Seric 					printf("openmailer: ");
10184052916eSeric 					mci_dump(mci, FALSE);
10199c9e68d9Seric 				}
10209c9e68d9Seric 				CurHostName = mci->mci_host;
10219c9e68d9Seric 				break;
10229c9e68d9Seric 			}
10239c9e68d9Seric 			mci->mci_mailer = m;
10249c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
10259c9e68d9Seric 				continue;
10269c9e68d9Seric 
10279c9e68d9Seric 			/* try the connection */
10289c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
10299c9e68d9Seric 			message("Connecting to %s (%s)...",
10309c9e68d9Seric 				hostbuf, m->m_name);
10319c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
10329c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
10339c9e68d9Seric 			mci->mci_exitstat = i;
10349c9e68d9Seric 			mci->mci_errno = errno;
1035f170942cSeric #ifdef NAMED_BIND
1036f170942cSeric 			mci->mci_herrno = h_errno;
1037f170942cSeric #endif
10389c9e68d9Seric 			if (i == EX_OK)
10399c9e68d9Seric 			{
10409c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10419c9e68d9Seric 				mci_cache(mci);
1042f170942cSeric 				if (TrafficLogFile != NULL)
1043f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1044f170942cSeric 						getpid(), hostbuf);
10459c9e68d9Seric 				break;
10469c9e68d9Seric 			}
10479c9e68d9Seric 			else if (tTd(11, 1))
10489c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10499c9e68d9Seric 					i, errno);
10509c9e68d9Seric 
10519c9e68d9Seric 			/* enter status of this host */
10529c9e68d9Seric 			setstat(i);
1053df106f0bSeric 
1054df106f0bSeric 			/* should print some message here for -v mode */
1055df106f0bSeric 		}
1056df106f0bSeric 		if (mci == NULL)
1057df106f0bSeric 		{
1058df106f0bSeric 			syserr("deliver: no host name");
1059df106f0bSeric 			rcode = EX_OSERR;
1060df106f0bSeric 			goto give_up;
10619c9e68d9Seric 		}
10629c9e68d9Seric 		mci->mci_pid = 0;
10639c9e68d9Seric #else /* no DAEMON */
10649c9e68d9Seric 		syserr("554 openmailer: no IPC");
10659c9e68d9Seric 		if (tTd(11, 1))
10669c9e68d9Seric 			printf("openmailer: NULL\n");
1067df106f0bSeric 		rcode = EX_UNAVAILABLE;
1068df106f0bSeric 		goto give_up;
10699c9e68d9Seric #endif /* DAEMON */
10709c9e68d9Seric 	}
10719c9e68d9Seric 	else
10729c9e68d9Seric 	{
1073f170942cSeric 		if (TrafficLogFile != NULL)
10740e3bfef5Seric 		{
1075f170942cSeric 			char **av;
1076f170942cSeric 
1077f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1078f170942cSeric 			for (av = pv; *av != NULL; av++)
1079f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1080f170942cSeric 			fprintf(TrafficLogFile, "\n");
10816fe8c3bcSeric 		}
10826fe8c3bcSeric 
10839c9e68d9Seric 		/* create a pipe to shove the mail through */
10849c9e68d9Seric 		if (pipe(mpvect) < 0)
10859c9e68d9Seric 		{
10860e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10870e3bfef5Seric 				e->e_to, m->m_name);
10889c9e68d9Seric 			if (tTd(11, 1))
10899c9e68d9Seric 				printf("openmailer: NULL\n");
10909c9e68d9Seric 			rcode = EX_OSERR;
10919c9e68d9Seric 			goto give_up;
10929c9e68d9Seric 		}
10939c9e68d9Seric 
10949c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10959c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10969c9e68d9Seric 		{
10970e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10980e3bfef5Seric 				e->e_to, m->m_name);
10999c9e68d9Seric 			(void) close(mpvect[0]);
11009c9e68d9Seric 			(void) close(mpvect[1]);
11019c9e68d9Seric 			if (tTd(11, 1))
11029c9e68d9Seric 				printf("openmailer: NULL\n");
11039c9e68d9Seric 			rcode = EX_OSERR;
11049c9e68d9Seric 			goto give_up;
11059c9e68d9Seric 		}
11069c9e68d9Seric 
11079c9e68d9Seric 		/*
11089c9e68d9Seric 		**  Actually fork the mailer process.
11099c9e68d9Seric 		**	DOFORK is clever about retrying.
11109c9e68d9Seric 		**
11119c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
11129c9e68d9Seric 		**	around so that endmail will get it.
11139c9e68d9Seric 		*/
11149c9e68d9Seric 
11159c9e68d9Seric 		if (e->e_xfp != NULL)
11169c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
11179c9e68d9Seric 		(void) fflush(stdout);
11189c9e68d9Seric # ifdef SIGCHLD
11192b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
11209c9e68d9Seric # endif /* SIGCHLD */
11219c9e68d9Seric 		DOFORK(FORK);
11229c9e68d9Seric 		/* pid is set by DOFORK */
11239c9e68d9Seric 		if (pid < 0)
11249c9e68d9Seric 		{
11259c9e68d9Seric 			/* failure */
11260e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
11270e3bfef5Seric 				e->e_to, m->m_name);
11289c9e68d9Seric 			(void) close(mpvect[0]);
11299c9e68d9Seric 			(void) close(mpvect[1]);
11309c9e68d9Seric 			if (clever)
11319c9e68d9Seric 			{
11329c9e68d9Seric 				(void) close(rpvect[0]);
11339c9e68d9Seric 				(void) close(rpvect[1]);
11349c9e68d9Seric 			}
11359c9e68d9Seric 			if (tTd(11, 1))
11369c9e68d9Seric 				printf("openmailer: NULL\n");
11379c9e68d9Seric 			rcode = EX_OSERR;
11389c9e68d9Seric 			goto give_up;
11399c9e68d9Seric 		}
11409c9e68d9Seric 		else if (pid == 0)
11419c9e68d9Seric 		{
11429c9e68d9Seric 			int i;
11439c9e68d9Seric 			int saveerrno;
11449c9e68d9Seric 			char **ep;
11459c9e68d9Seric 			char *env[MAXUSERENVIRON];
11469c9e68d9Seric 			extern char **environ;
11479c9e68d9Seric 			extern int DtableSize;
11489c9e68d9Seric 
11499c9e68d9Seric 			/* child -- set up input & exec mailer */
11502b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
11512b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
11522b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11539c9e68d9Seric 
115444f2317fSeric 			/* reset user and group */
115544f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
115644f2317fSeric 			{
115744f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
115844f2317fSeric 				{
115944f2317fSeric 					(void) initgroups(DefUser, DefGid);
1160fc354902Seric 					(void) setgid(DefGid);
116144f2317fSeric 					(void) setuid(DefUid);
116244f2317fSeric 				}
116344f2317fSeric 				else
116444f2317fSeric 				{
116544f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
116644f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
116744f2317fSeric 						ctladdr->q_gid);
116851cfe111Seric 					(void) setgid(ctladdr->q_gid);
116944f2317fSeric 					(void) setuid(ctladdr->q_uid);
117044f2317fSeric 				}
117144f2317fSeric 			}
117244f2317fSeric 
117344f2317fSeric 			if (tTd(11, 2))
117444f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
117544f2317fSeric 					getuid(), geteuid());
117644f2317fSeric 
1177b986f6aaSeric 			/* move into some "safe" directory */
1178b986f6aaSeric 			if (m->m_execdir != NULL)
1179b986f6aaSeric 			{
1180b986f6aaSeric 				char *p, *q;
1181b986f6aaSeric 				char buf[MAXLINE];
1182b986f6aaSeric 
1183b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1184b986f6aaSeric 				{
1185b986f6aaSeric 					q = strchr(p, ':');
1186b986f6aaSeric 					if (q != NULL)
1187b986f6aaSeric 						*q = '\0';
1188b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1189b986f6aaSeric 					if (q != NULL)
1190b986f6aaSeric 						*q++ = ':';
1191b986f6aaSeric 					if (tTd(11, 20))
1192b986f6aaSeric 						printf("openmailer: trydir %s\n",
1193b986f6aaSeric 							buf);
1194b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1195b986f6aaSeric 						break;
1196b986f6aaSeric 				}
1197b986f6aaSeric 			}
1198b986f6aaSeric 
11999c9e68d9Seric 			/* arrange to filter std & diag output of command */
12009c9e68d9Seric 			if (clever)
12019c9e68d9Seric 			{
12029c9e68d9Seric 				(void) close(rpvect[0]);
12036fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
12046fe8c3bcSeric 				{
12050e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
12060e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
12076fe8c3bcSeric 					_exit(EX_OSERR);
12086fe8c3bcSeric 				}
12099c9e68d9Seric 				(void) close(rpvect[1]);
12109c9e68d9Seric 			}
12118b40b0a4Seric 			else if (OpMode == MD_SMTP || OpMode == MD_DAEMON ||
12128b40b0a4Seric 				  HoldErrs || DisConnected)
12139c9e68d9Seric 			{
12149c9e68d9Seric 				/* put mailer output in transcript */
12156fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
12166fe8c3bcSeric 				{
12170e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
12180e3bfef5Seric 						e->e_to, m->m_name,
12196fe8c3bcSeric 						fileno(e->e_xfp));
12206fe8c3bcSeric 					_exit(EX_OSERR);
12219c9e68d9Seric 				}
12226fe8c3bcSeric 			}
12236fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
12246fe8c3bcSeric 			{
12250e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
12260e3bfef5Seric 					e->e_to, m->m_name);
12276fe8c3bcSeric 				_exit(EX_OSERR);
12286fe8c3bcSeric 			}
12299c9e68d9Seric 
12309c9e68d9Seric 			/* arrange to get standard input */
12319c9e68d9Seric 			(void) close(mpvect[1]);
12329c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
12339c9e68d9Seric 			{
12340e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
12350e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
12369c9e68d9Seric 				_exit(EX_OSERR);
12379c9e68d9Seric 			}
12389c9e68d9Seric 			(void) close(mpvect[0]);
12399c9e68d9Seric 
12409c9e68d9Seric 			/* arrange for all the files to be closed */
12419c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12429c9e68d9Seric 			{
12439c9e68d9Seric 				register int j;
124444f2317fSeric 
12459c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12469c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
12479c9e68d9Seric 			}
12489c9e68d9Seric 
12499f9b003eSeric 			/*
12509f9b003eSeric 			**  Set up the mailer environment
12519f9b003eSeric 			**	TZ is timezone information.
12529f9b003eSeric 			**	SYSTYPE is Apollo software sys type (required).
12539f9b003eSeric 			**	ISP is Apollo hardware system type (required).
12549f9b003eSeric 			*/
12559f9b003eSeric 
12569c9e68d9Seric 			i = 0;
12579c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12589c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12599c9e68d9Seric 			{
12609f9b003eSeric 				if (strncmp(*ep, "TZ=", 3) == 0 ||
12619f9b003eSeric 				    strncmp(*ep, "ISP=", 4) == 0 ||
12629f9b003eSeric 				    strncmp(*ep, "SYSTYPE=", 8) == 0)
12639c9e68d9Seric 					env[i++] = *ep;
12649c9e68d9Seric 			}
12659c9e68d9Seric 			env[i++] = NULL;
12669c9e68d9Seric 
12679c9e68d9Seric 			/* try to execute the mailer */
12689c9e68d9Seric 			execve(m->m_mailer, pv, env);
12699c9e68d9Seric 			saveerrno = errno;
12709c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12717c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12727c941fd2Seric 				_exit(EX_OSERR);
12739c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12749c9e68d9Seric 		}
12759c9e68d9Seric 
12769c9e68d9Seric 		/*
12779c9e68d9Seric 		**  Set up return value.
12789c9e68d9Seric 		*/
12799c9e68d9Seric 
12809c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12819c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12829c9e68d9Seric 		mci->mci_mailer = m;
12839c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12849c9e68d9Seric 		mci->mci_pid = pid;
12859c9e68d9Seric 		(void) close(mpvect[0]);
12869c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1287335eae58Seric 		if (mci->mci_out == NULL)
1288335eae58Seric 		{
1289335eae58Seric 			syserr("deliver: cannot create mailer output channel, fd=%d",
1290335eae58Seric 				mpvect[1]);
1291335eae58Seric 			(void) close(mpvect[1]);
1292335eae58Seric 			if (clever)
1293335eae58Seric 			{
1294335eae58Seric 				(void) close(rpvect[0]);
1295335eae58Seric 				(void) close(rpvect[1]);
1296335eae58Seric 			}
1297335eae58Seric 			rcode = EX_OSERR;
1298335eae58Seric 			goto give_up;
1299335eae58Seric 		}
13009c9e68d9Seric 		if (clever)
13019c9e68d9Seric 		{
13029c9e68d9Seric 			(void) close(rpvect[1]);
13039c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
1304335eae58Seric 			if (mci->mci_in == NULL)
1305335eae58Seric 			{
1306335eae58Seric 				syserr("deliver: cannot create mailer input channel, fd=%d",
1307335eae58Seric 					mpvect[1]);
1308335eae58Seric 				(void) close(rpvect[0]);
1309335eae58Seric 				fclose(mci->mci_out);
1310335eae58Seric 				mci->mci_out = NULL;
1311335eae58Seric 				rcode = EX_OSERR;
1312335eae58Seric 				goto give_up;
1313335eae58Seric 			}
13149c9e68d9Seric 		}
13159c9e68d9Seric 		else
13169c9e68d9Seric 		{
13179c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
13189c9e68d9Seric 			mci->mci_in = NULL;
13199c9e68d9Seric 		}
13209c9e68d9Seric 	}
13219c9e68d9Seric 
13229c9e68d9Seric 	/*
13239c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
13249c9e68d9Seric 	*/
13259c9e68d9Seric 
13269c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
13279c9e68d9Seric 	{
13289c9e68d9Seric 		smtpinit(m, mci, e);
13299c9e68d9Seric 	}
13309c9e68d9Seric 	if (tTd(11, 1))
13319c9e68d9Seric 	{
13329c9e68d9Seric 		printf("openmailer: ");
13334052916eSeric 		mci_dump(mci, FALSE);
13349c9e68d9Seric 	}
13359c9e68d9Seric 
13369c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1337b31e7f2bSeric 	{
1338b31e7f2bSeric 		/* couldn't open the mailer */
1339b31e7f2bSeric 		rcode = mci->mci_exitstat;
13402a6bc25bSeric 		errno = mci->mci_errno;
1341f170942cSeric #ifdef NAMED_BIND
1342f170942cSeric 		h_errno = mci->mci_herrno;
1343f170942cSeric #endif
1344b31e7f2bSeric 		if (rcode == EX_OK)
1345b31e7f2bSeric 		{
1346b31e7f2bSeric 			/* shouldn't happen */
134708b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
13486b0f339dSeric 				rcode, mci->mci_state, firstsig);
1349b31e7f2bSeric 			rcode = EX_SOFTWARE;
1350b31e7f2bSeric 		}
1351cb082c5bSeric 		else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0')
135290891494Seric 		{
135390891494Seric 			/* try next MX site */
135490891494Seric 			goto tryhost;
135590891494Seric 		}
1356b31e7f2bSeric 	}
1357b31e7f2bSeric 	else if (!clever)
1358b31e7f2bSeric 	{
1359b31e7f2bSeric 		/*
1360b31e7f2bSeric 		**  Format and send message.
1361b31e7f2bSeric 		*/
136215d084d5Seric 
13635aa0f353Seric 		putfromline(mci, e);
13645aa0f353Seric 		(*e->e_puthdr)(mci, e);
13655aa0f353Seric 		putline("\n", mci);
13665aa0f353Seric 		(*e->e_putbody)(mci, e, NULL);
1367b31e7f2bSeric 
1368b31e7f2bSeric 		/* get the exit status */
1369c9be6216Seric 		rcode = endmailer(mci, e, pv);
1370134746fbSeric 	}
1371134746fbSeric 	else
1372b31e7f2bSeric #ifdef SMTP
1373134746fbSeric 	{
1374b31e7f2bSeric 		/*
1375b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1376b31e7f2bSeric 		*/
137715d084d5Seric 
1378b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1379b31e7f2bSeric 		if (rcode == EX_OK)
138075889e88Seric 		{
1381ded0d3daSkarels 			register char *t = tobuf;
1382ded0d3daSkarels 			register int i;
1383ded0d3daSkarels 
1384588cad61Seric 			/* send the recipient list */
138563780dbdSeric 			tobuf[0] = '\0';
138675889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
138775889e88Seric 			{
138863780dbdSeric 				e->e_to = to->q_paddr;
138915d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
139075889e88Seric 				{
139183b7ddc9Seric 					markfailure(e, to, i);
13924dee0003Seric 					giveresponse(i, m, mci, ctladdr, e);
139363780dbdSeric 				}
139475889e88Seric 				else
139575889e88Seric 				{
1396911693bfSbostic 					*t++ = ',';
1397b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1398b31e7f2bSeric 						continue;
1399ee39df61Seric 					*t = '\0';
1400588cad61Seric 				}
1401588cad61Seric 			}
1402588cad61Seric 
140363780dbdSeric 			/* now send the data */
140463780dbdSeric 			if (tobuf[0] == '\0')
1405b31e7f2bSeric 			{
14069c9e68d9Seric 				rcode = EX_OK;
140763780dbdSeric 				e->e_to = NULL;
1408b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1409b31e7f2bSeric 					smtprset(m, mci, e);
1410b31e7f2bSeric 			}
141175889e88Seric 			else
141275889e88Seric 			{
141363780dbdSeric 				e->e_to = tobuf + 1;
141475889e88Seric 				rcode = smtpdata(m, mci, e);
141563780dbdSeric 			}
141663780dbdSeric 
141763780dbdSeric 			/* now close the connection */
1418b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
141915d084d5Seric 				smtpquit(m, mci, e);
142063780dbdSeric 		}
1421cb082c5bSeric 		if (rcode != EX_OK && curhost != NULL && *curhost != '\0')
14229c9e68d9Seric 		{
14239c9e68d9Seric 			/* try next MX site */
14249c9e68d9Seric 			goto tryhost;
14259c9e68d9Seric 		}
1426c579ef51Seric 	}
1427b31e7f2bSeric #else /* not SMTP */
1428a05b3449Sbostic 	{
142908b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1430845e533cSeric 		rcode = EX_CONFIG;
1431b31e7f2bSeric 		goto give_up;
1432a05b3449Sbostic 	}
1433b31e7f2bSeric #endif /* SMTP */
1434134746fbSeric #ifdef NAMED_BIND
14352bcc6d2dSeric 	if (ConfigLevel < 2)
1436912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1437134746fbSeric #endif
14385dfc646bSeric 
1439b31e7f2bSeric 	/* arrange a return receipt if requested */
1440df106f0bSeric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1441df106f0bSeric 	    bitnset(M_LOCALMAILER, m->m_flags))
1442b31e7f2bSeric 	{
1443b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1444b31e7f2bSeric 		/* do we want to send back more info? */
1445b31e7f2bSeric 	}
1446b31e7f2bSeric 
1447c77d1c25Seric 	/*
144863780dbdSeric 	**  Do final status disposal.
144963780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1450c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1451c77d1c25Seric 	**		addressees.
1452c77d1c25Seric 	*/
1453c77d1c25Seric 
1454b31e7f2bSeric   give_up:
145563780dbdSeric 	if (tobuf[0] != '\0')
14564dee0003Seric 		giveresponse(rcode, m, mci, ctladdr, e);
1457772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1458b31e7f2bSeric 	{
1459dde5acadSeric 		if (rcode != EX_OK)
146083b7ddc9Seric 			markfailure(e, to, rcode);
1461dde5acadSeric 		else
1462655518ecSeric 		{
1463dde5acadSeric 			to->q_flags |= QSENT;
1464655518ecSeric 			e->e_nsent++;
1465df106f0bSeric 			if (e->e_receiptto != NULL &&
1466df106f0bSeric 			    bitnset(M_LOCALMAILER, m->m_flags))
1467df106f0bSeric 			{
1468df106f0bSeric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1469df106f0bSeric 					to->q_paddr);
1470df106f0bSeric 			}
1471655518ecSeric 		}
1472b31e7f2bSeric 	}
1473b31e7f2bSeric 
1474b31e7f2bSeric 	/*
1475b31e7f2bSeric 	**  Restore state and return.
1476b31e7f2bSeric 	*/
1477c77d1c25Seric 
14787febfd66Seric #ifdef XDEBUG
14797febfd66Seric 	{
14807febfd66Seric 		char wbuf[MAXLINE];
14817febfd66Seric 
14827febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
148344622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
148444622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
148544622ea3Seric 			m->m_name);
14867febfd66Seric 		checkfd012(wbuf);
14877febfd66Seric 	}
14887febfd66Seric #endif
14897febfd66Seric 
149035490626Seric 	errno = 0;
1491588cad61Seric 	define('g', (char *) NULL, e);
14925826d9d3Seric 	return (rcode);
149325a99e2eSeric }
14945dfc646bSeric /*
149583b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
149683b7ddc9Seric **
149783b7ddc9Seric **	Parameters:
149883b7ddc9Seric **		e -- the envelope we are sending.
149983b7ddc9Seric **		q -- the address to mark.
150083b7ddc9Seric **		rcode -- the code signifying the particular failure.
150183b7ddc9Seric **
150283b7ddc9Seric **	Returns:
150383b7ddc9Seric **		none.
150483b7ddc9Seric **
150583b7ddc9Seric **	Side Effects:
150683b7ddc9Seric **		marks the address (and possibly the envelope) with the
150783b7ddc9Seric **			failure so that an error will be returned or
150883b7ddc9Seric **			the message will be queued, as appropriate.
150983b7ddc9Seric */
151083b7ddc9Seric 
151183b7ddc9Seric markfailure(e, q, rcode)
151283b7ddc9Seric 	register ENVELOPE *e;
151383b7ddc9Seric 	register ADDRESS *q;
151483b7ddc9Seric 	int rcode;
151583b7ddc9Seric {
151619c47125Seric 	char buf[MAXLINE];
151719c47125Seric 
1518d5aafa3cSeric 	switch (rcode)
1519d5aafa3cSeric 	{
1520d5aafa3cSeric 	  case EX_OK:
1521d5aafa3cSeric 		break;
1522d5aafa3cSeric 
1523d5aafa3cSeric 	  case EX_TEMPFAIL:
1524d5aafa3cSeric 	  case EX_IOERR:
1525d5aafa3cSeric 	  case EX_OSERR:
152683b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1527d5aafa3cSeric 		break;
1528d5aafa3cSeric 
1529d5aafa3cSeric 	  default:
1530f170942cSeric 		q->q_flags |= QBADADDR;
1531d5aafa3cSeric 		break;
1532d5aafa3cSeric 	}
153383b7ddc9Seric }
153483b7ddc9Seric /*
1535c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1536c579ef51Seric **
1537c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1538c579ef51Seric **	violation), so we report those specially.  For other
1539c579ef51Seric **	errors, we choose a status message (into statmsg),
1540c579ef51Seric **	and if it represents an error, we print it.
1541c579ef51Seric **
1542c579ef51Seric **	Parameters:
1543c579ef51Seric **		pid -- pid of mailer.
1544c9be6216Seric **		e -- the current envelope.
1545c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1546c9be6216Seric **			(for error messages).
1547c579ef51Seric **
1548c579ef51Seric **	Returns:
1549c579ef51Seric **		exit code of mailer.
1550c579ef51Seric **
1551c579ef51Seric **	Side Effects:
1552c579ef51Seric **		none.
1553c579ef51Seric */
1554c579ef51Seric 
1555c9be6216Seric endmailer(mci, e, pv)
1556b31e7f2bSeric 	register MCI *mci;
1557c9be6216Seric 	register ENVELOPE *e;
1558c9be6216Seric 	char **pv;
1559c579ef51Seric {
1560588cad61Seric 	int st;
1561c579ef51Seric 
156275889e88Seric 	/* close any connections */
156375889e88Seric 	if (mci->mci_in != NULL)
156476f3d53fSeric 		(void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in");
156575889e88Seric 	if (mci->mci_out != NULL)
156676f3d53fSeric 		(void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out");
156775889e88Seric 	mci->mci_in = mci->mci_out = NULL;
156875889e88Seric 	mci->mci_state = MCIS_CLOSED;
156975889e88Seric 
157033db8731Seric 	/* in the IPC case there is nothing to wait for */
157175889e88Seric 	if (mci->mci_pid == 0)
157233db8731Seric 		return (EX_OK);
157333db8731Seric 
157433db8731Seric 	/* wait for the mailer process to die and collect status */
157575889e88Seric 	st = waitfor(mci->mci_pid);
1576588cad61Seric 	if (st == -1)
157778de67c1Seric 	{
1578c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1579588cad61Seric 		return (EX_SOFTWARE);
1580c579ef51Seric 	}
158133db8731Seric 
1582bf9bc890Seric 	if (WIFEXITED(st))
1583c579ef51Seric 	{
1584bf9bc890Seric 		/* normal death -- return status */
1585bf9bc890Seric 		return (WEXITSTATUS(st));
1586bf9bc890Seric 	}
1587bf9bc890Seric 
1588bf9bc890Seric 	/* it died a horrid death */
1589550092c3Seric 	syserr("451 mailer %s died with signal %o",
1590550092c3Seric 		mci->mci_mailer->m_name, st);
1591c9be6216Seric 
1592c9be6216Seric 	/* log the arguments */
15933c930db3Seric 	if (pv != NULL && e->e_xfp != NULL)
1594c9be6216Seric 	{
1595c9be6216Seric 		register char **av;
1596c9be6216Seric 
1597c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1598c9be6216Seric 		for (av = pv; *av != NULL; av++)
1599c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1600c9be6216Seric 		fprintf(e->e_xfp, "\n");
1601c9be6216Seric 	}
1602c9be6216Seric 
16035f73204aSeric 	ExitStat = EX_TEMPFAIL;
16045f73204aSeric 	return (EX_TEMPFAIL);
1605c579ef51Seric }
1606c579ef51Seric /*
160725a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
160825a99e2eSeric **
160925a99e2eSeric **	Parameters:
161025a99e2eSeric **		stat -- the status code from the mailer (high byte
161125a99e2eSeric **			only; core dumps must have been taken care of
161225a99e2eSeric **			already).
161381161401Seric **		m -- the mailer info for this mailer.
161481161401Seric **		mci -- the mailer connection info -- can be NULL if the
161581161401Seric **			response is given before the connection is made.
16164dee0003Seric **		ctladdr -- the controlling address for the recipient
16174dee0003Seric **			address(es).
161881161401Seric **		e -- the current envelope.
161925a99e2eSeric **
162025a99e2eSeric **	Returns:
1621db8841e9Seric **		none.
162225a99e2eSeric **
162325a99e2eSeric **	Side Effects:
1624c1f9df2cSeric **		Errors may be incremented.
162525a99e2eSeric **		ExitStat may be set.
162625a99e2eSeric */
162725a99e2eSeric 
16284dee0003Seric giveresponse(stat, m, mci, ctladdr, e)
162925a99e2eSeric 	int stat;
1630588cad61Seric 	register MAILER *m;
163181161401Seric 	register MCI *mci;
16324dee0003Seric 	ADDRESS *ctladdr;
1633198d9be0Seric 	ENVELOPE *e;
163425a99e2eSeric {
16359c16475dSeric 	register const char *statmsg;
163625a99e2eSeric 	extern char *SysExMsg[];
163725a99e2eSeric 	register int i;
1638d4bd8f0eSbostic 	extern int N_SysEx;
1639198d9be0Seric 	char buf[MAXLINE];
164025a99e2eSeric 
164113bbc08cSeric 	/*
164213bbc08cSeric 	**  Compute status message from code.
164313bbc08cSeric 	*/
164413bbc08cSeric 
164525a99e2eSeric 	i = stat - EX__BASE;
1646588cad61Seric 	if (stat == 0)
16476fe8c3bcSeric 	{
1648588cad61Seric 		statmsg = "250 Sent";
1649ce5531bdSeric 		if (e->e_statmsg != NULL)
16506fe8c3bcSeric 		{
1651ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
16526fe8c3bcSeric 			statmsg = buf;
16536fe8c3bcSeric 		}
16546fe8c3bcSeric 	}
1655588cad61Seric 	else if (i < 0 || i > N_SysEx)
1656588cad61Seric 	{
1657588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1658588cad61Seric 		stat = EX_UNAVAILABLE;
1659588cad61Seric 		statmsg = buf;
1660588cad61Seric 	}
1661198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1662198d9be0Seric 	{
16637d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1664d4bd8f0eSbostic #ifdef NAMED_BIND
1665f28da541Smiriam 		if (h_errno == TRY_AGAIN)
16664d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1667f28da541Smiriam 		else
1668d4bd8f0eSbostic #endif
1669f28da541Smiriam 		{
16708557d168Seric 			if (errno != 0)
1671d87e85f3Seric 				statmsg = errstring(errno);
1672d87e85f3Seric 			else
1673d87e85f3Seric 			{
1674d87e85f3Seric #ifdef SMTP
1675d87e85f3Seric 				statmsg = SmtpError;
16766c2c3107Seric #else /* SMTP */
1677d87e85f3Seric 				statmsg = NULL;
16786c2c3107Seric #endif /* SMTP */
1679d87e85f3Seric 			}
1680f28da541Smiriam 		}
1681d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1682d87e85f3Seric 		{
168387c9b3e7Seric 			(void) strcat(buf, ": ");
1684d87e85f3Seric 			(void) strcat(buf, statmsg);
16858557d168Seric 		}
1686198d9be0Seric 		statmsg = buf;
1687198d9be0Seric 	}
1688f170942cSeric #ifdef NAMED_BIND
1689f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1690f170942cSeric 	{
16914d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1692f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1693f170942cSeric 		statmsg = buf;
1694f170942cSeric 	}
1695f170942cSeric #endif
169625a99e2eSeric 	else
1697d87e85f3Seric 	{
169825a99e2eSeric 		statmsg = SysExMsg[i];
16997d55540cSeric 		if (*statmsg++ == ':')
17007d55540cSeric 		{
17017d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
17027d55540cSeric 			statmsg = buf;
17037d55540cSeric 		}
1704d87e85f3Seric 	}
1705588cad61Seric 
1706588cad61Seric 	/*
1707588cad61Seric 	**  Print the message as appropriate
1708588cad61Seric 	*/
1709588cad61Seric 
1710198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1711df106f0bSeric 	{
1712df106f0bSeric 		extern char MsgBuf[];
1713df106f0bSeric 
1714e12d03eeSeric 		message(&statmsg[4], errstring(errno));
1715df106f0bSeric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1716df106f0bSeric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1717df106f0bSeric 	}
171825a99e2eSeric 	else
171925a99e2eSeric 	{
1720c1f9df2cSeric 		Errors++;
1721e12d03eeSeric 		usrerr(statmsg, errstring(errno));
172225a99e2eSeric 	}
172325a99e2eSeric 
172425a99e2eSeric 	/*
172525a99e2eSeric 	**  Final cleanup.
172625a99e2eSeric 	**	Log a record of the transaction.  Compute the new
172725a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
172825a99e2eSeric 	**	that.
172925a99e2eSeric 	*/
173025a99e2eSeric 
17312f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
17324dee0003Seric 		logdelivery(m, mci, &statmsg[4], ctladdr, e);
1733eb238f8cSeric 
1734eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1735eb238f8cSeric 		setstat(stat);
17361097d4c9Seric 	if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL))
1737198d9be0Seric 	{
1738198d9be0Seric 		if (e->e_message != NULL)
1739198d9be0Seric 			free(e->e_message);
1740198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1741198d9be0Seric 	}
17428557d168Seric 	errno = 0;
1743d4bd8f0eSbostic #ifdef NAMED_BIND
1744f28da541Smiriam 	h_errno = 0;
1745d4bd8f0eSbostic #endif
1746eb238f8cSeric }
1747eb238f8cSeric /*
1748eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1749eb238f8cSeric **
17502c9b4f99Seric **	Care is taken to avoid logging lines that are too long, because
17512c9b4f99Seric **	some versions of syslog have an unfortunate proclivity for core
17522c9b4f99Seric **	dumping.  This is a hack, to be sure, that is at best empirical.
17532c9b4f99Seric **
1754eb238f8cSeric **	Parameters:
175581161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
175681161401Seric **		mci -- the mailer connection info -- can be NULL if the
175781161401Seric **			log is occuring when no connection is active.
175881161401Seric **		stat -- the message to print for the status.
17594dee0003Seric **		ctladdr -- the controlling address for the to list.
176081161401Seric **		e -- the current envelope.
1761eb238f8cSeric **
1762eb238f8cSeric **	Returns:
1763eb238f8cSeric **		none
1764eb238f8cSeric **
1765eb238f8cSeric **	Side Effects:
1766eb238f8cSeric **		none
1767eb238f8cSeric */
1768eb238f8cSeric 
17694dee0003Seric logdelivery(m, mci, stat, ctladdr, e)
177081161401Seric 	MAILER *m;
177181161401Seric 	register MCI *mci;
1772eb238f8cSeric 	char *stat;
17734dee0003Seric 	ADDRESS *ctladdr;
1774b31e7f2bSeric 	register ENVELOPE *e;
17755cf56be3Seric {
1776eb238f8cSeric # ifdef LOG
17774dee0003Seric 	register char *bp;
17782c9b4f99Seric 	register char *p;
17792c9b4f99Seric 	int l;
1780d6acf3eeSeric 	char buf[512];
17819507d1f9Seric 
17823a100e8bSeric #  if (SYSLOG_BUFSIZE) >= 256
17834dee0003Seric 	bp = buf;
17844dee0003Seric 	if (ctladdr != NULL)
17854dee0003Seric 	{
17864dee0003Seric 		strcpy(bp, ", ctladdr=");
1787f62f08c7Seric 		strcat(bp, shortenstring(ctladdr->q_paddr, 83));
17884dee0003Seric 		bp += strlen(bp);
17894dee0003Seric 		if (bitset(QGOODUID, ctladdr->q_flags))
17904dee0003Seric 		{
17914dee0003Seric 			(void) sprintf(bp, " (%d/%d)",
17924dee0003Seric 					ctladdr->q_uid, ctladdr->q_gid);
17934dee0003Seric 			bp += strlen(bp);
17944dee0003Seric 		}
17954dee0003Seric 	}
17964dee0003Seric 
17974dee0003Seric 	(void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
17984dee0003Seric 	bp += strlen(bp);
179981161401Seric 
180048ed5d33Seric 	if (m != NULL)
180171ff6caaSeric 	{
18024dee0003Seric 		(void) strcpy(bp, ", mailer=");
18034dee0003Seric 		(void) strcat(bp, m->m_name);
18044dee0003Seric 		bp += strlen(bp);
180571ff6caaSeric 	}
180648ed5d33Seric 
180748ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
180871ff6caaSeric 	{
180971ff6caaSeric # ifdef DAEMON
1810e2f2f828Seric 		extern SOCKADDR CurHostAddr;
181148ed5d33Seric # endif
181271ff6caaSeric 
18134dee0003Seric 		(void) strcpy(bp, ", relay=");
18144dee0003Seric 		(void) strcat(bp, mci->mci_host);
181548ed5d33Seric 
181648ed5d33Seric # ifdef DAEMON
18172bdfc6b9Seric 		(void) strcat(bp, " [");
18184dee0003Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
18192bdfc6b9Seric 		(void) strcat(bp, "]");
182071ff6caaSeric # endif
182171ff6caaSeric 	}
18229507d1f9Seric 	else
182348ed5d33Seric 	{
182448ed5d33Seric 		char *p = macvalue('h', e);
18259507d1f9Seric 
182648ed5d33Seric 		if (p != NULL && p[0] != '\0')
182748ed5d33Seric 		{
18284dee0003Seric 			(void) strcpy(bp, ", relay=");
18294dee0003Seric 			(void) strcat(bp, p);
183048ed5d33Seric 		}
183148ed5d33Seric 	}
1832cb082c5bSeric 	bp += strlen(bp);
1833d6acf3eeSeric 
18343a100e8bSeric #define STATLEN		(((SYSLOG_BUFSIZE) - 100) / 4)
18353a100e8bSeric #if (STATLEN) < 63
18363a100e8bSeric # undef STATLEN
18373a100e8bSeric # define STATLEN	63
18383a100e8bSeric #endif
18393a100e8bSeric #if (STATLEN) > 203
18403a100e8bSeric # undef STATLEN
18413a100e8bSeric # define STATLEN	203
18423a100e8bSeric #endif
18433a100e8bSeric 
18443a100e8bSeric 	if ((bp - buf) > (sizeof buf - ((STATLEN) + 20)))
18452c9b4f99Seric 	{
18462c9b4f99Seric 		/* desperation move -- truncate data */
18473a100e8bSeric 		bp = buf + sizeof buf - ((STATLEN) + 17);
18482c9b4f99Seric 		strcpy(bp, "...");
18492c9b4f99Seric 		bp += 3;
18502c9b4f99Seric 	}
18512c9b4f99Seric 
18522c9b4f99Seric 	(void) strcpy(bp, ", stat=");
18532c9b4f99Seric 	bp += strlen(bp);
18543a100e8bSeric 
18553a100e8bSeric 	(void) strcpy(bp, shortenstring(stat, (STATLEN)));
18562c9b4f99Seric 
18572c9b4f99Seric 	l = SYSLOG_BUFSIZE - 100 - strlen(buf);
18582c9b4f99Seric 	p = e->e_to;
18592c9b4f99Seric 	while (strlen(p) >= l)
18602c9b4f99Seric 	{
18612c9b4f99Seric 		register char *q = strchr(p + l, ',');
18622c9b4f99Seric 
18632a1b6b73Seric 		if (q == NULL)
18642c9b4f99Seric 			break;
18652c9b4f99Seric 		syslog(LOG_INFO, "%s: to=%.*s [more]%s",
18662c9b4f99Seric 			e->e_id, ++q - p, p, buf);
18672c9b4f99Seric 		p = q;
18682c9b4f99Seric 	}
18692c9b4f99Seric 	syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf);
18703a100e8bSeric 
18713a100e8bSeric #  else		/* we have a very short log buffer size */
18723a100e8bSeric 
1873*27607809Seric 	l = SYSLOG_BUFSIZE - 85;
18743a100e8bSeric 	p = e->e_to;
18753a100e8bSeric 	while (strlen(p) >= l)
18763a100e8bSeric 	{
18773a100e8bSeric 		register char *q = strchr(p + l, ',');
18783a100e8bSeric 
18793a100e8bSeric 		if (q == NULL)
18803a100e8bSeric 			break;
18813a100e8bSeric 		syslog(LOG_INFO, "%s: to=%.*s [more]",
18823a100e8bSeric 			e->e_id, ++q - p, p);
18833a100e8bSeric 		p = q;
18843a100e8bSeric 	}
18853a100e8bSeric 	syslog(LOG_INFO, "%s: to=%s", e->e_id, p);
18863a100e8bSeric 
18873a100e8bSeric 	if (ctladdr != NULL)
18883a100e8bSeric 	{
18893a100e8bSeric 		bp = buf;
18903a100e8bSeric 		strcpy(buf, "ctladdr=");
18913a100e8bSeric 		bp += strlen(buf);
18923a100e8bSeric 		strcpy(bp, shortenstring(ctladdr->q_paddr, 83));
18933a100e8bSeric 		bp += strlen(buf);
18943a100e8bSeric 		if (bitset(QGOODUID, ctladdr->q_flags))
18953a100e8bSeric 		{
18963a100e8bSeric 			(void) sprintf(bp, " (%d/%d)",
18973a100e8bSeric 					ctladdr->q_uid, ctladdr->q_gid);
18983a100e8bSeric 			bp += strlen(bp);
18993a100e8bSeric 		}
19003a100e8bSeric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
19013a100e8bSeric 	}
19024e797715Seric 	bp = buf;
19034e797715Seric 	sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
19044e797715Seric 	bp += strlen(bp);
19053a100e8bSeric 
19063a100e8bSeric 	if (m != NULL)
19074e797715Seric 	{
19084e797715Seric 		sprintf(bp, ", mailer=%s", m->m_name);
19094e797715Seric 		bp += strlen(bp);
19104e797715Seric 	}
1911b056abd0Seric 	syslog(LOG_INFO, "%s: %s", e->e_id, buf);
19123a100e8bSeric 
1913b056abd0Seric 	buf[0] = '\0';
19143a100e8bSeric 	if (mci != NULL && mci->mci_host != NULL)
19153a100e8bSeric 	{
19163a100e8bSeric # ifdef DAEMON
19173a100e8bSeric 		extern SOCKADDR CurHostAddr;
19183a100e8bSeric # endif
19193a100e8bSeric 
1920d2ece200Seric 		sprintf(buf, "relay=%s", mci->mci_host);
19213a100e8bSeric 
19223a100e8bSeric # ifdef DAEMON
1923b056abd0Seric 		(void) strcat(buf, " [");
1924b056abd0Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
1925b056abd0Seric 		(void) strcat(buf, "]");
19263a100e8bSeric # endif
19273a100e8bSeric 	}
19283a100e8bSeric 	else
19293a100e8bSeric 	{
19303a100e8bSeric 		char *p = macvalue('h', e);
19313a100e8bSeric 
19323a100e8bSeric 		if (p != NULL && p[0] != '\0')
1933d2ece200Seric 			sprintf(buf, "relay=%s", p);
19343a100e8bSeric 	}
1935b056abd0Seric 	if (buf[0] != '\0')
19364e797715Seric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
19373a100e8bSeric 
19383a100e8bSeric 	syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63));
19393a100e8bSeric #  endif /* short log buffer */
19406c2c3107Seric # endif /* LOG */
194125a99e2eSeric }
194225a99e2eSeric /*
194351552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
194425a99e2eSeric **
194551552439Seric **	This can be made an arbitrary message separator by changing $l
194651552439Seric **
19479b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
19489b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
19499b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
19509b6c17a6Seric **	this kind of antique garbage????
195125a99e2eSeric **
195225a99e2eSeric **	Parameters:
19535aa0f353Seric **		mci -- the connection information.
19545aa0f353Seric **		e -- the envelope.
195525a99e2eSeric **
195625a99e2eSeric **	Returns:
195751552439Seric **		none
195825a99e2eSeric **
195925a99e2eSeric **	Side Effects:
196051552439Seric **		outputs some text to fp.
196125a99e2eSeric */
196225a99e2eSeric 
19635aa0f353Seric putfromline(mci, e)
19645aa0f353Seric 	register MCI *mci;
1965b31e7f2bSeric 	ENVELOPE *e;
196625a99e2eSeric {
19672bc47524Seric 	char *template = "\201l\n";
196851552439Seric 	char buf[MAXLINE];
196925a99e2eSeric 
19705aa0f353Seric 	if (bitnset(M_NHDR, mci->mci_mailer->m_flags))
197151552439Seric 		return;
197213bbc08cSeric 
19732c7e1b8dSeric # ifdef UGLYUUCP
19745aa0f353Seric 	if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags))
197574b6e67bSeric 	{
1976ea09d6edSeric 		char *bang;
1977ea09d6edSeric 		char xbuf[MAXLINE];
197874b6e67bSeric 
1979ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
19806c2c3107Seric 		bang = strchr(buf, '!');
198174b6e67bSeric 		if (bang == NULL)
198234fcca25Seric 		{
198334fcca25Seric 			errno = 0;
198434fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
198534fcca25Seric 		}
198674b6e67bSeric 		else
1987588cad61Seric 		{
1988ea09d6edSeric 			*bang++ = '\0';
19892bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1990ea09d6edSeric 			template = xbuf;
199174b6e67bSeric 		}
1992588cad61Seric 	}
19936c2c3107Seric # endif /* UGLYUUCP */
1994b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
19955aa0f353Seric 	putline(buf, mci);
1996bc6e2962Seric }
1997bc6e2962Seric /*
199851552439Seric **  PUTBODY -- put the body of a message.
199951552439Seric **
200051552439Seric **	Parameters:
20015aa0f353Seric **		mci -- the connection information.
20029a6a5f55Seric **		e -- the envelope to put out.
200303c02fdeSeric **		separator -- if non-NULL, a message separator that must
200403c02fdeSeric **			not be permitted in the resulting message.
200551552439Seric **
200651552439Seric **	Returns:
200751552439Seric **		none.
200851552439Seric **
200951552439Seric **	Side Effects:
201051552439Seric **		The message is written onto fp.
201151552439Seric */
201251552439Seric 
20135aa0f353Seric putbody(mci, e, separator)
20145aa0f353Seric 	register MCI *mci;
20159a6a5f55Seric 	register ENVELOPE *e;
201603c02fdeSeric 	char *separator;
201751552439Seric {
201877b52738Seric 	char buf[MAXLINE];
201951552439Seric 
202051552439Seric 	/*
202151552439Seric 	**  Output the body of the message
202251552439Seric 	*/
202351552439Seric 
20249a6a5f55Seric 	if (e->e_dfp == NULL)
202551552439Seric 	{
20269a6a5f55Seric 		if (e->e_df != NULL)
20279a6a5f55Seric 		{
20289a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
20299a6a5f55Seric 			if (e->e_dfp == NULL)
20308f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
2031e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
20329a6a5f55Seric 		}
20339a6a5f55Seric 		else
20345aa0f353Seric 			putline("<<< No Message Collected >>>", mci);
20359a6a5f55Seric 	}
20369a6a5f55Seric 	if (e->e_dfp != NULL)
20379a6a5f55Seric 	{
20389a6a5f55Seric 		rewind(e->e_dfp);
20395aa0f353Seric 		while (!ferror(mci->mci_out) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
204024fc8aeeSeric 		{
20415aa0f353Seric 			if (buf[0] == 'F' &&
20425aa0f353Seric 			    bitnset(M_ESCFROM, mci->mci_mailer->m_flags) &&
2043d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
20445aa0f353Seric 				(void) putc('>', mci->mci_out);
204503c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
204603c02fdeSeric 			{
204703c02fdeSeric 				/* possible separator */
204803c02fdeSeric 				int sl = strlen(separator);
204903c02fdeSeric 
205003c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
20515aa0f353Seric 					(void) putc(' ', mci->mci_out);
205203c02fdeSeric 			}
20535aa0f353Seric 			putline(buf, mci);
205424fc8aeeSeric 		}
205551552439Seric 
20569a6a5f55Seric 		if (ferror(e->e_dfp))
205751552439Seric 		{
2058df106f0bSeric 			syserr("putbody: %s: read error", e->e_df);
205951552439Seric 			ExitStat = EX_IOERR;
206051552439Seric 		}
206151552439Seric 	}
206251552439Seric 
20630890ba1fSeric 	/* some mailers want extra blank line at end of message */
20645aa0f353Seric 	if (bitnset(M_BLANKEND, mci->mci_mailer->m_flags) &&
20655aa0f353Seric 	    buf[0] != '\0' && buf[0] != '\n')
20665aa0f353Seric 		putline("", mci);
20670890ba1fSeric 
20685aa0f353Seric 	(void) fflush(mci->mci_out);
20695aa0f353Seric 	if (ferror(mci->mci_out) && errno != EPIPE)
207051552439Seric 	{
207151552439Seric 		syserr("putbody: write error");
207251552439Seric 		ExitStat = EX_IOERR;
207351552439Seric 	}
207451552439Seric 	errno = 0;
207525a99e2eSeric }
207625a99e2eSeric /*
207725a99e2eSeric **  MAILFILE -- Send a message to a file.
207825a99e2eSeric **
2079f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
2080f129ec7dSeric **	bits, sendmail will try to become the owner of that file
2081f129ec7dSeric **	rather than the real user.  Obviously, this only works if
2082f129ec7dSeric **	sendmail runs as root.
2083f129ec7dSeric **
2084588cad61Seric **	This could be done as a subordinate mailer, except that it
2085588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
2086588cad61Seric **	view this as being sufficiently important as to include it
2087588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
2088588cad61Seric **	to create another process plus some pipes to save the message.
2089588cad61Seric **
209025a99e2eSeric **	Parameters:
209125a99e2eSeric **		filename -- the name of the file to send to.
20926259796dSeric **		ctladdr -- the controlling address header -- includes
20936259796dSeric **			the userid/groupid to be when sending.
209425a99e2eSeric **
209525a99e2eSeric **	Returns:
209625a99e2eSeric **		The exit code associated with the operation.
209725a99e2eSeric **
209825a99e2eSeric **	Side Effects:
209925a99e2eSeric **		none.
210025a99e2eSeric */
210125a99e2eSeric 
2102b31e7f2bSeric mailfile(filename, ctladdr, e)
210325a99e2eSeric 	char *filename;
21046259796dSeric 	ADDRESS *ctladdr;
2105b31e7f2bSeric 	register ENVELOPE *e;
210625a99e2eSeric {
210725a99e2eSeric 	register FILE *f;
210832d19d43Seric 	register int pid;
210915d084d5Seric 	int mode;
211025a99e2eSeric 
2111671745f3Seric 	if (tTd(11, 1))
2112671745f3Seric 	{
2113671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
2114671745f3Seric 		printaddr(ctladdr, FALSE);
2115671745f3Seric 	}
2116671745f3Seric 
2117f170942cSeric 	if (e->e_xfp != NULL)
2118f170942cSeric 		fflush(e->e_xfp);
2119f170942cSeric 
212032d19d43Seric 	/*
212132d19d43Seric 	**  Fork so we can change permissions here.
212232d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
212332d19d43Seric 	**	the complications of calling subroutines, etc.
212432d19d43Seric 	*/
212532d19d43Seric 
212632d19d43Seric 	DOFORK(fork);
212732d19d43Seric 
212832d19d43Seric 	if (pid < 0)
212932d19d43Seric 		return (EX_OSERR);
213032d19d43Seric 	else if (pid == 0)
213132d19d43Seric 	{
213232d19d43Seric 		/* child -- actually write to file */
2133f129ec7dSeric 		struct stat stb;
21345aa0f353Seric 		MCI mcibuf;
2135f129ec7dSeric 
21362b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
21372b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
21382b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
21393462ad9eSeric 		(void) umask(OldUmask);
214095f16dc0Seric 
2141f129ec7dSeric 		if (stat(filename, &stb) < 0)
21423a98e7eaSeric 			stb.st_mode = FileMode;
214315d084d5Seric 		mode = stb.st_mode;
214495f16dc0Seric 
214595f16dc0Seric 		/* limit the errors to those actually caused in the child */
214695f16dc0Seric 		errno = 0;
214795f16dc0Seric 		ExitStat = EX_OK;
214895f16dc0Seric 
2149f129ec7dSeric 		if (bitset(0111, stb.st_mode))
2150f129ec7dSeric 			exit(EX_CANTCREAT);
215119428781Seric 		if (ctladdr != NULL)
215215d084d5Seric 		{
215315d084d5Seric 			/* ignore setuid and setgid bits */
215415d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
215515d084d5Seric 		}
215615d084d5Seric 
21578f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
21588f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
21598f9146b0Srick 		{
21608f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
216195f16dc0Seric 			if (e->e_dfp == NULL)
216295f16dc0Seric 			{
21638f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
2164e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
21658f9146b0Srick 			}
21668f9146b0Srick 		}
21678f9146b0Srick 
216815d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
2169e36b99e2Seric 		{
217019428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
217195f16dc0Seric 			{
2172898a126bSbostic 				(void) initgroups(DefUser, DefGid);
217395f16dc0Seric 			}
217495f16dc0Seric 			else
217595f16dc0Seric 			{
2176898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
2177898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
2178898a126bSbostic 					ctladdr->q_gid);
2179898a126bSbostic 			}
2180e36b99e2Seric 		}
218115d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
2182e36b99e2Seric 		{
218319428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
2184e36b99e2Seric 				(void) setuid(DefUid);
2185e36b99e2Seric 			else
21866259796dSeric 				(void) setuid(ctladdr->q_uid);
2187e36b99e2Seric 		}
218895f16dc0Seric 		FileName = filename;
218995f16dc0Seric 		LineNumber = 0;
21903a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
219125a99e2eSeric 		if (f == NULL)
219295f16dc0Seric 		{
2193b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
219432d19d43Seric 			exit(EX_CANTCREAT);
219595f16dc0Seric 		}
219625a99e2eSeric 
21975aa0f353Seric 		bzero(&mcibuf, sizeof mcibuf);
21985aa0f353Seric 		mcibuf.mci_mailer = FileMailer;
21995aa0f353Seric 		mcibuf.mci_out = f;
22005aa0f353Seric 		if (bitnset(M_7BITS, FileMailer->m_flags))
22015aa0f353Seric 			mcibuf.mci_flags |= MCIF_7BIT;
22025aa0f353Seric 
22035aa0f353Seric 		putfromline(&mcibuf, e);
22045aa0f353Seric 		(*e->e_puthdr)(&mcibuf, e);
22055aa0f353Seric 		putline("\n", &mcibuf);
22065aa0f353Seric 		(*e->e_putbody)(&mcibuf, e, NULL);
22075aa0f353Seric 		putline("\n", &mcibuf);
220895f16dc0Seric 		if (ferror(f))
220995f16dc0Seric 		{
2210b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
221195f16dc0Seric 			setstat(EX_IOERR);
221295f16dc0Seric 		}
2213ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
221432d19d43Seric 		(void) fflush(stdout);
2215e36b99e2Seric 
221627628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
2217c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
221895f16dc0Seric 		exit(ExitStat);
221913bbc08cSeric 		/*NOTREACHED*/
222032d19d43Seric 	}
222132d19d43Seric 	else
222232d19d43Seric 	{
222332d19d43Seric 		/* parent -- wait for exit status */
2224588cad61Seric 		int st;
222532d19d43Seric 
2226588cad61Seric 		st = waitfor(pid);
2227bf9bc890Seric 		if (WIFEXITED(st))
2228bf9bc890Seric 			return (WEXITSTATUS(st));
2229588cad61Seric 		else
2230b6a0de9dSeric 		{
2231b6a0de9dSeric 			syserr("child died on signal %d", st);
2232bf9bc890Seric 			return (EX_UNAVAILABLE);
2233b6a0de9dSeric 		}
22348f9146b0Srick 		/*NOTREACHED*/
223532d19d43Seric 	}
223625a99e2eSeric }
2237ea4dc939Seric /*
2238e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2239e103b48fSeric **
2240e103b48fSeric **	The signature describes how we are going to send this -- it
2241e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2242e103b48fSeric **	an ordered list of MX hosts.
2243e103b48fSeric **
2244e103b48fSeric **	Parameters:
2245e103b48fSeric **		m -- the mailer describing this host.
2246e103b48fSeric **		host -- the host name.
2247e103b48fSeric **		e -- the current envelope.
2248e103b48fSeric **
2249e103b48fSeric **	Returns:
2250e103b48fSeric **		The signature for this host.
2251e103b48fSeric **
2252e103b48fSeric **	Side Effects:
2253e103b48fSeric **		Can tweak the symbol table.
2254e103b48fSeric */
2255e103b48fSeric 
2256e103b48fSeric char *
2257e103b48fSeric hostsignature(m, host, e)
2258e103b48fSeric 	register MAILER *m;
2259e103b48fSeric 	char *host;
2260e103b48fSeric 	ENVELOPE *e;
2261e103b48fSeric {
2262e103b48fSeric 	register char *p;
2263e103b48fSeric 	register STAB *s;
2264e103b48fSeric 	int i;
2265e103b48fSeric 	int len;
2266e103b48fSeric #ifdef NAMED_BIND
2267e103b48fSeric 	int nmx;
2268e103b48fSeric 	auto int rcode;
2269bafdc4e5Seric 	char *hp;
2270bafdc4e5Seric 	char *endp;
2271516782b4Seric 	int oldoptions;
2272e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2273e103b48fSeric #endif
2274e103b48fSeric 
2275e103b48fSeric 	/*
2276e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2277e103b48fSeric 	*/
2278e103b48fSeric 
2279e103b48fSeric 	p = m->m_mailer;
2280e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2281e103b48fSeric 	{
2282e103b48fSeric 		/* just an ordinary mailer */
2283e103b48fSeric 		return host;
2284e103b48fSeric 	}
2285e103b48fSeric 
2286e103b48fSeric 	/*
2287e103b48fSeric 	**  Look it up in the symbol table.
2288e103b48fSeric 	*/
2289e103b48fSeric 
2290e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2291e103b48fSeric 	if (s->s_hostsig != NULL)
2292e103b48fSeric 		return s->s_hostsig;
2293e103b48fSeric 
2294e103b48fSeric 	/*
2295e103b48fSeric 	**  Not already there -- create a signature.
2296e103b48fSeric 	*/
2297e103b48fSeric 
2298e103b48fSeric #ifdef NAMED_BIND
2299516782b4Seric 	if (ConfigLevel < 2)
2300516782b4Seric 	{
2301516782b4Seric 		oldoptions = _res.options;
2302516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2303516782b4Seric 	}
2304516782b4Seric 
2305bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2306bafdc4e5Seric 	{
2307bafdc4e5Seric 		endp = strchr(hp, ':');
2308bafdc4e5Seric 		if (endp != NULL)
2309bafdc4e5Seric 			*endp = '\0';
2310bafdc4e5Seric 
23117bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
23127d55540cSeric 
2313e103b48fSeric 		if (nmx <= 0)
2314e103b48fSeric 		{
2315e103b48fSeric 			register MCI *mci;
2316e103b48fSeric 
2317e103b48fSeric 			/* update the connection info for this host */
2318bafdc4e5Seric 			mci = mci_get(hp, m);
2319e103b48fSeric 			mci->mci_exitstat = rcode;
2320e103b48fSeric 			mci->mci_errno = errno;
2321f170942cSeric #ifdef NAMED_BIND
2322f170942cSeric 			mci->mci_herrno = h_errno;
2323f170942cSeric #endif
2324e103b48fSeric 
2325e103b48fSeric 			/* and return the original host name as the signature */
2326bafdc4e5Seric 			nmx = 1;
2327bafdc4e5Seric 			mxhosts[0] = hp;
2328e103b48fSeric 		}
2329e103b48fSeric 
2330e103b48fSeric 		len = 0;
2331e103b48fSeric 		for (i = 0; i < nmx; i++)
2332e103b48fSeric 		{
2333e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2334e103b48fSeric 		}
2335bafdc4e5Seric 		if (s->s_hostsig != NULL)
2336bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2337bafdc4e5Seric 		p = xalloc(len);
2338bafdc4e5Seric 		if (s->s_hostsig != NULL)
2339bafdc4e5Seric 		{
2340bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2341bafdc4e5Seric 			free(s->s_hostsig);
2342bafdc4e5Seric 			s->s_hostsig = p;
2343bafdc4e5Seric 			p += strlen(p);
2344bafdc4e5Seric 			*p++ = ':';
2345bafdc4e5Seric 		}
2346bafdc4e5Seric 		else
2347bafdc4e5Seric 			s->s_hostsig = p;
2348e103b48fSeric 		for (i = 0; i < nmx; i++)
2349e103b48fSeric 		{
2350e103b48fSeric 			if (i != 0)
2351e103b48fSeric 				*p++ = ':';
2352e103b48fSeric 			strcpy(p, mxhosts[i]);
2353e103b48fSeric 			p += strlen(p);
2354e103b48fSeric 		}
2355bafdc4e5Seric 		if (endp != NULL)
2356bafdc4e5Seric 			*endp++ = ':';
2357bafdc4e5Seric 	}
2358e103b48fSeric 	makelower(s->s_hostsig);
2359516782b4Seric 	if (ConfigLevel < 2)
2360516782b4Seric 		_res.options = oldoptions;
2361e103b48fSeric #else
2362e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2363e103b48fSeric 	s->s_hostsig = host;
2364e103b48fSeric #endif
2365e103b48fSeric 	if (tTd(17, 1))
2366e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2367e103b48fSeric 	return s->s_hostsig;
2368e103b48fSeric }
2369