14227346bSdist /*
2759969d8Seric  * Copyright (c) 1983, 1995 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*3e164d5bSeric static char sccsid[] = "@(#)deliver.c	8.160 (Berkeley) 06/21/95";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14911693bfSbostic #include <errno.h>
159d4a8008Seric #if NAMED_BIND
16912a731aSbostic #include <resolv.h>
17f170942cSeric 
18f170942cSeric extern int	h_errno;
19134746fbSeric #endif
2025a99e2eSeric 
215d437c4dSeric extern char	SmtpError[];
225d437c4dSeric 
2325a99e2eSeric /*
249c9e68d9Seric **  SENDALL -- actually send all the messages.
259c9e68d9Seric **
269c9e68d9Seric **	Parameters:
279c9e68d9Seric **		e -- the envelope to send.
289c9e68d9Seric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
299c9e68d9Seric **			the current e->e_sendmode.
309c9e68d9Seric **
319c9e68d9Seric **	Returns:
329c9e68d9Seric **		none.
339c9e68d9Seric **
349c9e68d9Seric **	Side Effects:
359c9e68d9Seric **		Scans the send lists and sends everything it finds.
369c9e68d9Seric **		Delivers any appropriate error messages.
379c9e68d9Seric **		If we are running in a non-interactive mode, takes the
389c9e68d9Seric **			appropriate action.
399c9e68d9Seric */
409c9e68d9Seric 
41b665faabSeric void
sendall(e,mode)429c9e68d9Seric sendall(e, mode)
439c9e68d9Seric 	ENVELOPE *e;
449c9e68d9Seric 	char mode;
459c9e68d9Seric {
469c9e68d9Seric 	register ADDRESS *q;
479c9e68d9Seric 	char *owner;
489c9e68d9Seric 	int otherowners;
49b056abd0Seric 	register ENVELOPE *ee;
50b056abd0Seric 	ENVELOPE *splitenv = NULL;
51b665faabSeric 	bool oldverbose = Verbose;
52b665faabSeric 	bool somedeliveries = FALSE;
53b665faabSeric 	int pid;
54b665faabSeric 	extern void sendenvelope();
559c9e68d9Seric 
567880f3e4Seric 	/*
577880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
587880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
597880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
607880f3e4Seric 	**  addresses to be sent.
617880f3e4Seric 	*/
627880f3e4Seric 
638d19a23dSeric 	if (bitset(EF_FATALERRS, e->e_flags) &&
648d19a23dSeric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
65896b16f6Seric 	{
66896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
67896b16f6Seric 		return;
68896b16f6Seric 	}
69896b16f6Seric 
709c9e68d9Seric 	/* determine actual delivery mode */
71fbe2f963Seric 	CurrentLA = getla();
729c9e68d9Seric 	if (mode == SM_DEFAULT)
739c9e68d9Seric 	{
749c9e68d9Seric 		mode = e->e_sendmode;
759c9e68d9Seric 		if (mode != SM_VERIFY &&
769c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
779c9e68d9Seric 			mode = SM_QUEUE;
789c9e68d9Seric 	}
799c9e68d9Seric 
809c9e68d9Seric 	if (tTd(13, 1))
819c9e68d9Seric 	{
82b665faabSeric 		extern void printenvflags();
83b665faabSeric 
840e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
850e484fe5Seric 			mode, e->e_id);
869c9e68d9Seric 		printaddr(&e->e_from, FALSE);
87b665faabSeric 		printf("\te_flags = ");
88b665faabSeric 		printenvflags(e);
899c9e68d9Seric 		printf("sendqueue:\n");
909c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
919c9e68d9Seric 	}
929c9e68d9Seric 
939c9e68d9Seric 	/*
949c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
959c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
969c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
979c9e68d9Seric 	*/
989c9e68d9Seric 
999c9e68d9Seric 	CurEnv = e;
1009c9e68d9Seric 
1019c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
1029c9e68d9Seric 	{
1039c9e68d9Seric 		errno = 0;
104b665faabSeric 		queueup(e, TRUE, mode == SM_QUEUE);
10527607809Seric 		e->e_flags |= EF_FATALERRS|EF_PM_NOTIFY|EF_CLRQUEUE;
106d95dad3eSeric 		syserr("554 Too many hops %d (%d max): from %s via %s, to %s",
1079c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
108f7869e68Seric 			RealHostName == NULL ? "localhost" : RealHostName,
109f7869e68Seric 			e->e_sendqueue->q_paddr);
110d95dad3eSeric 		e->e_sendqueue->q_status = "5.4.6";
1119c9e68d9Seric 		return;
1129c9e68d9Seric 	}
1139c9e68d9Seric 
114b8004690Seric 	/*
115b8004690Seric 	**  Do sender deletion.
116b8004690Seric 	**
117b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
118b8004690Seric 	**	This can happen if the name server is hosed when you
119b8004690Seric 	**	are trying to send mail.  The result is that the sender
120b8004690Seric 	**	is instantiated in the queue as a recipient.
121b8004690Seric 	*/
122b8004690Seric 
123e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
124e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1259c9e68d9Seric 	{
1269c9e68d9Seric 		if (tTd(13, 5))
1279c9e68d9Seric 		{
1289c9e68d9Seric 			printf("sendall: QDONTSEND ");
1299c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1309c9e68d9Seric 		}
1319c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
132b665faabSeric 		(void) recipient(&e->e_from, &e->e_sendqueue, 0, e);
1339c9e68d9Seric 	}
1349c9e68d9Seric 
135ce5531bdSeric 	/*
136ce5531bdSeric 	**  Handle alias owners.
137ce5531bdSeric 	**
138ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
139ce5531bdSeric 	**	We discard owners that are the same as the return path.
140ce5531bdSeric 	*/
141ce5531bdSeric 
142ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
143ce5531bdSeric 	{
144ce5531bdSeric 		register struct address *a;
145ce5531bdSeric 
146ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
147ce5531bdSeric 			continue;
148ce5531bdSeric 		if (a != NULL)
149ce5531bdSeric 			q->q_owner = a->q_owner;
150ce5531bdSeric 
151ce5531bdSeric 		if (q->q_owner != NULL &&
152ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
153ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
154ce5531bdSeric 			q->q_owner = NULL;
155ce5531bdSeric 	}
156ce5531bdSeric 
157ce5531bdSeric 	owner = "";
158ce5531bdSeric 	otherowners = 1;
159ce5531bdSeric 	while (owner != NULL && otherowners > 0)
160ce5531bdSeric 	{
161ce5531bdSeric 		owner = NULL;
162ce5531bdSeric 		otherowners = 0;
163ce5531bdSeric 
164ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
165ce5531bdSeric 		{
166ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
167ce5531bdSeric 				continue;
168ce5531bdSeric 
169ce5531bdSeric 			if (q->q_owner != NULL)
170ce5531bdSeric 			{
171ce5531bdSeric 				if (owner == NULL)
172ce5531bdSeric 					owner = q->q_owner;
173ce5531bdSeric 				else if (owner != q->q_owner)
174ce5531bdSeric 				{
175ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
176ce5531bdSeric 					{
177ce5531bdSeric 						/* make future comparisons cheap */
178ce5531bdSeric 						q->q_owner = owner;
179ce5531bdSeric 					}
180ce5531bdSeric 					else
181ce5531bdSeric 					{
182ce5531bdSeric 						otherowners++;
183ce5531bdSeric 					}
184ce5531bdSeric 					owner = q->q_owner;
185ce5531bdSeric 				}
186ce5531bdSeric 			}
187ce5531bdSeric 			else
188ce5531bdSeric 			{
189ce5531bdSeric 				otherowners++;
190ce5531bdSeric 			}
191b665faabSeric 
192b665faabSeric 			/*
193b665faabSeric 			**  If this mailer is expensive, and if we don't
194b665faabSeric 			**  want to make connections now, just mark these
195b665faabSeric 			**  addresses and return.  This is useful if we
196b665faabSeric 			**  want to batch connections to reduce load.  This
197b665faabSeric 			**  will cause the messages to be queued up, and a
198b665faabSeric 			**  daemon will come along to send the messages later.
199b665faabSeric 			*/
200b665faabSeric 
201b665faabSeric 			if (bitset(QBADADDR|QQUEUEUP, q->q_flags))
202b665faabSeric 				continue;
203b665faabSeric 			if (NoConnect && !Verbose &&
204b665faabSeric 			    bitnset(M_EXPENSIVE, q->q_mailer->m_flags))
205b665faabSeric 			{
206b665faabSeric 				q->q_flags |= QQUEUEUP;
207b665faabSeric 				e->e_to = q->q_paddr;
208b665faabSeric 				message("queued");
209b665faabSeric 				if (LogLevel > 8)
210b665faabSeric 					logdelivery(q->q_mailer, NULL,
211b665faabSeric 						    "queued", NULL,
212b665faabSeric 						    (time_t) 0, e);
213b665faabSeric 				e->e_to = NULL;
214b665faabSeric 			}
215b665faabSeric 			else
216b665faabSeric 			{
217b665faabSeric 				somedeliveries = TRUE;
218b665faabSeric 			}
219ce5531bdSeric 		}
220ce5531bdSeric 
221ce5531bdSeric 		if (owner != NULL && otherowners > 0)
222ce5531bdSeric 		{
223ce5531bdSeric 			extern HDR *copyheader();
224ce5531bdSeric 			extern ADDRESS *copyqueue();
225ce5531bdSeric 
226ce5531bdSeric 			/*
227ce5531bdSeric 			**  Split this envelope into two.
228ce5531bdSeric 			*/
229ce5531bdSeric 
230ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
231ce5531bdSeric 			*ee = *e;
232ce5531bdSeric 			ee->e_id = NULL;
233ce5531bdSeric 			(void) queuename(ee, '\0');
234ce5531bdSeric 
235ce5531bdSeric 			if (tTd(13, 1))
236ce5531bdSeric 				printf("sendall: split %s into %s\n",
237ce5531bdSeric 					e->e_id, ee->e_id);
238ce5531bdSeric 
239ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
240ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
241ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
242b665faabSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS|EF_SENDRECEIPT|EF_RET_PARAM);
243fce3c07bSeric 			ee->e_flags |= EF_NORECEIPT;
244ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
245ce5531bdSeric 			if (tTd(13, 5))
246ce5531bdSeric 			{
247ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
248ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
249ce5531bdSeric 			}
250ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
251ce5531bdSeric 			ee->e_dfp = NULL;
252ce5531bdSeric 			ee->e_xfp = NULL;
253ce5531bdSeric 			ee->e_errormode = EM_MAIL;
254b056abd0Seric 			ee->e_sibling = splitenv;
255b056abd0Seric 			splitenv = ee;
256ce5531bdSeric 
257ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
258b665faabSeric 			{
259ce5531bdSeric 				if (q->q_owner == owner)
260de1a6b1bSeric 				{
261ce5531bdSeric 					q->q_flags |= QDONTSEND;
262de1a6b1bSeric 					q->q_flags &= ~QQUEUEUP;
263de1a6b1bSeric 				}
264b665faabSeric 			}
265ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
266b665faabSeric 			{
267ce5531bdSeric 				if (q->q_owner != owner)
268de1a6b1bSeric 				{
269ce5531bdSeric 					q->q_flags |= QDONTSEND;
270de1a6b1bSeric 					q->q_flags &= ~QQUEUEUP;
271de1a6b1bSeric 				}
272b665faabSeric 				else
273ce5531bdSeric 				{
274b665faabSeric 					/* clear DSN parameters */
275b665faabSeric 					q->q_flags &= ~(QHASNOTIFY|QPINGONSUCCESS);
276b665faabSeric 					q->q_flags |= QPINGONFAILURE|QPINGONDELAY;
277b665faabSeric 				}
278b665faabSeric 			}
279b665faabSeric 
280b665faabSeric 			if (mode != SM_VERIFY && bitset(EF_HAS_DF, e->e_flags))
281b665faabSeric 			{
282b665faabSeric 				char df1buf[20], df2buf[20];
283b665faabSeric 
284ce5531bdSeric 				ee->e_dfp = NULL;
285b665faabSeric 				strcpy(df1buf, queuename(e, 'd'));
286b665faabSeric 				strcpy(df2buf, queuename(ee, 'd'));
287b665faabSeric 				if (link(df1buf, df2buf) < 0)
288ce5531bdSeric 				{
289ce5531bdSeric 					syserr("sendall: link(%s, %s)",
290b665faabSeric 						df1buf, df2buf);
291ce5531bdSeric 				}
292ce5531bdSeric 			}
293ce5531bdSeric #ifdef LOG
294ce5531bdSeric 			if (LogLevel > 4)
295c8b70947Seric 				syslog(LOG_INFO, "%s: clone %s, owner=%s",
296c8b70947Seric 					ee->e_id, e->e_id, owner);
297ce5531bdSeric #endif
298ce5531bdSeric 		}
299ce5531bdSeric 	}
300ce5531bdSeric 
301ce5531bdSeric 	if (owner != NULL)
302ce5531bdSeric 	{
303ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
304ce5531bdSeric 		if (tTd(13, 5))
305ce5531bdSeric 		{
306ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
307ce5531bdSeric 			printaddr(&e->e_from, FALSE);
308ce5531bdSeric 		}
309ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
310ce5531bdSeric 		e->e_errormode = EM_MAIL;
311fce3c07bSeric 		e->e_flags |= EF_NORECEIPT;
312ce5531bdSeric 	}
313ce5531bdSeric 
314b665faabSeric 	/* if nothing to be delivered, just queue up everything */
315b665faabSeric 	if (!somedeliveries && mode != SM_QUEUE && mode != SM_VERIFY)
316b665faabSeric 		mode = SM_QUEUE;
317b665faabSeric 
318c1ac89b1Seric # ifdef QUEUE
319c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
320c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
321c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
322c1ac89b1Seric 	{
323c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
324b665faabSeric 		queueup(e, TRUE, mode == SM_QUEUE);
325b056abd0Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
326b665faabSeric 			queueup(ee, TRUE, mode == SM_QUEUE);
327c1ac89b1Seric 	}
328c1ac89b1Seric #endif /* QUEUE */
329c1ac89b1Seric 
3307880f3e4Seric 	/*
331b665faabSeric 	**  If we belong in background, fork now.
3327880f3e4Seric 	*/
3337880f3e4Seric 
334c1ac89b1Seric 	switch (mode)
335c1ac89b1Seric 	{
336c1ac89b1Seric 	  case SM_VERIFY:
337c1ac89b1Seric 		Verbose = TRUE;
338c1ac89b1Seric 		break;
339c1ac89b1Seric 
340c1ac89b1Seric 	  case SM_QUEUE:
341c1ac89b1Seric   queueonly:
342b665faabSeric 		if (e->e_nrcpts > 0)
343c1ac89b1Seric 			e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
344c1ac89b1Seric 		return;
345c1ac89b1Seric 
346c1ac89b1Seric 	  case SM_FORK:
347c1ac89b1Seric 		if (e->e_xfp != NULL)
348c1ac89b1Seric 			(void) fflush(e->e_xfp);
349c1ac89b1Seric 
350b269aa8eSeric # if !HASFLOCK
351c1ac89b1Seric 		/*
3526b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3536b2765c6Seric 		**  the lock is owned by a process, not by an open file
3546b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3556b2765c6Seric 		**  then restart from scratch in the child.
356c1ac89b1Seric 		*/
357c1ac89b1Seric 
358b665faabSeric 		{
3596b2765c6Seric 			/* save id for future use */
360b665faabSeric 			char *qid = e->e_id;
3616b2765c6Seric 
3626b2765c6Seric 			/* now drop the envelope in the parent */
3636b2765c6Seric 			e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3646b2765c6Seric 			dropenvelope(e);
3656b2765c6Seric 
3666b2765c6Seric 			/* and reacquire in the child */
367b665faabSeric 			(void) dowork(qid, TRUE, FALSE, e);
368b665faabSeric 		}
3696b2765c6Seric 
3706b2765c6Seric 		return;
3716b2765c6Seric 
3726b2765c6Seric # else /* HASFLOCK */
373c1ac89b1Seric 
374c1ac89b1Seric 		pid = fork();
375c1ac89b1Seric 		if (pid < 0)
376c1ac89b1Seric 		{
377c1ac89b1Seric 			goto queueonly;
378c1ac89b1Seric 		}
379c1ac89b1Seric 		else if (pid > 0)
380c1ac89b1Seric 		{
3810e484fe5Seric 			/* be sure we leave the temp files to our child */
3820e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3830e484fe5Seric 			if (e->e_lockfp != NULL)
384b665faabSeric 				(void) xfclose(e->e_lockfp, "sendenvelope lockfp", e->e_id);
3850e484fe5Seric 			e->e_lockfp = NULL;
3860e484fe5Seric 
3870e484fe5Seric 			/* close any random open files in the envelope */
3880e484fe5Seric 			closexscript(e);
3890e484fe5Seric 			if (e->e_dfp != NULL)
390b665faabSeric 				(void) xfclose(e->e_dfp, "sendenvelope dfp", e->e_id);
3910e484fe5Seric 			e->e_dfp = NULL;
392b665faabSeric 			e->e_id = NULL;
393b665faabSeric 			e->e_flags &= ~EF_HAS_DF;
3949f9b003eSeric 
3959f9b003eSeric 			/* catch intermediate zombie */
3969f9b003eSeric 			(void) waitfor(pid);
397c1ac89b1Seric 			return;
398c1ac89b1Seric 		}
399c1ac89b1Seric 
400c1ac89b1Seric 		/* double fork to avoid zombies */
4019f9b003eSeric 		pid = fork();
4029f9b003eSeric 		if (pid > 0)
403c1ac89b1Seric 			exit(EX_OK);
404c1ac89b1Seric 
405c1ac89b1Seric 		/* be sure we are immune from the terminal */
4067880f3e4Seric 		disconnect(1, e);
407c1ac89b1Seric 
4089f9b003eSeric 		/* prevent parent from waiting if there was an error */
4099f9b003eSeric 		if (pid < 0)
4109f9b003eSeric 		{
4119f9b003eSeric 			e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
4129f9b003eSeric 			finis();
4139f9b003eSeric 		}
4149f9b003eSeric 
415c1ac89b1Seric 		/*
416c1ac89b1Seric 		**  Close any cached connections.
417c1ac89b1Seric 		**
418c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
419c1ac89b1Seric 		**	still knows about the connection.
420c1ac89b1Seric 		**
421c1ac89b1Seric 		**	This should only happen when delivering an error
422c1ac89b1Seric 		**	message.
423c1ac89b1Seric 		*/
424c1ac89b1Seric 
425c1ac89b1Seric 		mci_flush(FALSE, NULL);
426c1ac89b1Seric 
4276b2765c6Seric # endif /* HASFLOCK */
4286b2765c6Seric 
429c1ac89b1Seric 		break;
430c1ac89b1Seric 	}
431c1ac89b1Seric 
432b665faabSeric 	if (splitenv != NULL)
433b665faabSeric 	{
434b665faabSeric 		if (tTd(13, 1))
435b665faabSeric 		{
436b665faabSeric 			printf("\nsendall: Split queue; remaining queue:\n");
437b665faabSeric 			printaddr(e->e_sendqueue, TRUE);
438b665faabSeric 		}
439b665faabSeric 
440b665faabSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
441b665faabSeric 		{
442b665faabSeric 			CurEnv = ee;
443b665faabSeric 			if (mode != SM_VERIFY)
444b665faabSeric 				openxscript(ee);
445b665faabSeric 			sendenvelope(ee, mode);
446b665faabSeric 			dropenvelope(ee);
447b665faabSeric 		}
448b665faabSeric 
449b665faabSeric 		CurEnv = e;
450b665faabSeric 	}
451b665faabSeric 	sendenvelope(e, mode);
452b665faabSeric 	Verbose = oldverbose;
453b665faabSeric }
454b665faabSeric 
455b665faabSeric void
sendenvelope(e,mode)456b665faabSeric sendenvelope(e, mode)
457b665faabSeric 	register ENVELOPE *e;
458b665faabSeric 	char mode;
459b665faabSeric {
460b665faabSeric 	register ADDRESS *q;
461b665faabSeric 	bool didany;
462b665faabSeric 
463b665faabSeric 	/*
464b665faabSeric 	**  If we have had global, fatal errors, don't bother sending
465b665faabSeric 	**  the message at all if we are in SMTP mode.  Local errors
466b665faabSeric 	**  (e.g., a single address failing) will still cause the other
467b665faabSeric 	**  addresses to be sent.
468b665faabSeric 	*/
469b665faabSeric 
470b665faabSeric 	if (bitset(EF_FATALERRS, e->e_flags) &&
471b665faabSeric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
472b665faabSeric 	{
473b665faabSeric 		e->e_flags |= EF_CLRQUEUE;
474b665faabSeric 		return;
475b665faabSeric 	}
476b665faabSeric 
477c1ac89b1Seric 	/*
4789c9e68d9Seric 	**  Run through the list and send everything.
4795288e21aSeric 	**
4805288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4815288e21aSeric 	**	result in returned mail.
4829c9e68d9Seric 	*/
4839c9e68d9Seric 
4849c9e68d9Seric 	e->e_nsent = 0;
4855288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
486b665faabSeric 	didany = FALSE;
487faad2b16Seric 
488faad2b16Seric 	/* now run through the queue */
4899c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4909c9e68d9Seric 	{
49186aab53cSeric #if XDEBUG
492fdc75a0fSeric 		char wbuf[MAXNAME + 20];
493fdc75a0fSeric 
494fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
495fdc75a0fSeric 		checkfd012(wbuf);
496fdc75a0fSeric #endif
4979c9e68d9Seric 		if (mode == SM_VERIFY)
4989c9e68d9Seric 		{
4999c9e68d9Seric 			e->e_to = q->q_paddr;
5009c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
5018dfca105Seric 			{
502fafe46e1Seric 				if (q->q_host != NULL && q->q_host[0] != '\0')
5038dfca105Seric 					message("deliverable: mailer %s, host %s, user %s",
5048dfca105Seric 						q->q_mailer->m_name,
5058dfca105Seric 						q->q_host,
5068dfca105Seric 						q->q_user);
507fafe46e1Seric 				else
508fafe46e1Seric 					message("deliverable: mailer %s, user %s",
509fafe46e1Seric 						q->q_mailer->m_name,
510fafe46e1Seric 						q->q_user);
5118dfca105Seric 			}
5129c9e68d9Seric 		}
5139c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
5149c9e68d9Seric 		{
5159c9e68d9Seric # ifdef QUEUE
5169c9e68d9Seric 			/*
5179c9e68d9Seric 			**  Checkpoint the send list every few addresses
5189c9e68d9Seric 			*/
5199c9e68d9Seric 
5209c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
5219c9e68d9Seric 			{
5229c9e68d9Seric 				queueup(e, TRUE, FALSE);
5239c9e68d9Seric 				e->e_nsent = 0;
5249c9e68d9Seric 			}
5259c9e68d9Seric # endif /* QUEUE */
5269c9e68d9Seric 			(void) deliver(e, q);
527b665faabSeric 			didany = TRUE;
5289c9e68d9Seric 		}
5299c9e68d9Seric 	}
530b665faabSeric 	if (didany)
531b665faabSeric 	{
532b665faabSeric 		e->e_dtime = curtime();
533b665faabSeric 		e->e_ntries++;
534b665faabSeric 	}
5359c9e68d9Seric 
53686aab53cSeric #if XDEBUG
537fdc75a0fSeric 	checkfd012("end of sendenvelope");
538fdc75a0fSeric #endif
539fdc75a0fSeric 
5409c9e68d9Seric 	if (mode == SM_FORK)
5419c9e68d9Seric 		finis();
5429c9e68d9Seric }
5439c9e68d9Seric /*
5449c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
5459c9e68d9Seric **
5469c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
5479c9e68d9Seric **	two processes on the same stack!!!
5489c9e68d9Seric **
5499c9e68d9Seric **	Parameters:
5509c9e68d9Seric **		none.
5519c9e68d9Seric **
5529c9e68d9Seric **	Returns:
5539c9e68d9Seric **		From a macro???  You've got to be kidding!
5549c9e68d9Seric **
5559c9e68d9Seric **	Side Effects:
5569c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
5579c9e68d9Seric **			pid of child in parent, zero in child.
5589c9e68d9Seric **			-1 on unrecoverable error.
5599c9e68d9Seric **
5609c9e68d9Seric **	Notes:
5619c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
5629c9e68d9Seric **		vfork for you.....
5639c9e68d9Seric */
5649c9e68d9Seric 
5659c9e68d9Seric # define NFORKTRIES	5
5669c9e68d9Seric 
5679c9e68d9Seric # ifndef FORK
5689c9e68d9Seric # define FORK	fork
5699c9e68d9Seric # endif
5709c9e68d9Seric 
5719c9e68d9Seric # define DOFORK(fORKfN) \
5729c9e68d9Seric {\
5739c9e68d9Seric 	register int i;\
5749c9e68d9Seric \
5759c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
5769c9e68d9Seric 	{\
5779c9e68d9Seric 		pid = fORKfN();\
5789c9e68d9Seric 		if (pid >= 0)\
5799c9e68d9Seric 			break;\
5809c9e68d9Seric 		if (i > 0)\
5819c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5829c9e68d9Seric 	}\
5839c9e68d9Seric }
5849c9e68d9Seric /*
5859c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5869c9e68d9Seric **
5879c9e68d9Seric **	Parameters:
5889c9e68d9Seric **		none.
5899c9e68d9Seric **
5909c9e68d9Seric **	Returns:
5919c9e68d9Seric **		pid of child in parent.
5929c9e68d9Seric **		zero in child.
5939c9e68d9Seric **		-1 on error.
5949c9e68d9Seric **
5959c9e68d9Seric **	Side Effects:
5969c9e68d9Seric **		returns twice, once in parent and once in child.
5979c9e68d9Seric */
5989c9e68d9Seric 
599a6a073b5Seric int
dofork()6009c9e68d9Seric dofork()
6019c9e68d9Seric {
602b665faabSeric 	register int pid = -1;
6039c9e68d9Seric 
6049c9e68d9Seric 	DOFORK(fork);
6059c9e68d9Seric 	return (pid);
6069c9e68d9Seric }
6079c9e68d9Seric /*
60813bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
60913bbc08cSeric **
61013bbc08cSeric **	This routine delivers to everyone on the same host as the
61113bbc08cSeric **	user on the head of the list.  It is clever about mailers
61213bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
61313bbc08cSeric **	that it will deliver to all these addresses however -- so
61413bbc08cSeric **	deliver should be called once for each address on the
61513bbc08cSeric **	list.
61625a99e2eSeric **
61725a99e2eSeric **	Parameters:
618588cad61Seric **		e -- the envelope to deliver.
619c77d1c25Seric **		firstto -- head of the address list to deliver to.
62025a99e2eSeric **
62125a99e2eSeric **	Returns:
62225a99e2eSeric **		zero -- successfully delivered.
62325a99e2eSeric **		else -- some failure, see ExitStat for more info.
62425a99e2eSeric **
62525a99e2eSeric **	Side Effects:
62625a99e2eSeric **		The standard input is passed off to someone.
62725a99e2eSeric */
62825a99e2eSeric 
629b665faabSeric int
deliver(e,firstto)630588cad61Seric deliver(e, firstto)
631588cad61Seric 	register ENVELOPE *e;
632c77d1c25Seric 	ADDRESS *firstto;
63325a99e2eSeric {
63478442df3Seric 	char *host;			/* host being sent to */
63578442df3Seric 	char *user;			/* user being sent to */
63625a99e2eSeric 	char **pvp;
6375dfc646bSeric 	register char **mvp;
63825a99e2eSeric 	register char *p;
639588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
6406259796dSeric 	ADDRESS *ctladdr;
641b31e7f2bSeric 	register MCI *mci;
642c77d1c25Seric 	register ADDRESS *to = firstto;
643c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
644b665faabSeric 	ADDRESS *tochain = NULL;	/* users chain in this mailer call */
645911693bfSbostic 	int rcode;			/* response code */
646e103b48fSeric 	char *firstsig;			/* signature of firstto */
647b665faabSeric 	int pid = -1;
6489c9e68d9Seric 	char *curhost;
649b665faabSeric 	time_t xstart;
6509c9e68d9Seric 	int mpvect[2];
6519c9e68d9Seric 	int rpvect[2];
652ee6bf8dfSeric 	char *pv[MAXPV+1];
653579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
654b665faabSeric 	char buf[MAXNAME + 1];
655b665faabSeric 	char rpathbuf[MAXNAME + 1];	/* translated return path */
656fabb3bd4Seric 	extern int checkcompat();
657a6a073b5Seric 	extern void markfailure __P((ENVELOPE *, ADDRESS *, MCI *, int));
65825a99e2eSeric 
65935490626Seric 	errno = 0;
660ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
6615dfc646bSeric 		return (0);
66225a99e2eSeric 
6639d4a8008Seric #if NAMED_BIND
664912a731aSbostic 	/* unless interactive, try twice, over a minute */
6658d19a23dSeric 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP)
6668d19a23dSeric 	{
667912a731aSbostic 		_res.retrans = 30;
668912a731aSbostic 		_res.retry = 2;
669912a731aSbostic 	}
670d4bd8f0eSbostic #endif
671912a731aSbostic 
67251552439Seric 	m = to->q_mailer;
67351552439Seric 	host = to->q_host;
674c9be6216Seric 	CurEnv = e;			/* just in case */
6754384d521Seric 	e->e_statmsg = NULL;
676df106f0bSeric 	SmtpError[0] = '\0';
677b665faabSeric 	xstart = curtime();
67851552439Seric 
6796ef48975Seric 	if (tTd(10, 1))
680562ec147Seric 		printf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n",
681562ec147Seric 			e->e_id, m->m_name, host, to->q_user);
682ab81ee53Seric 	if (tTd(10, 100))
683ab81ee53Seric 		printopenfds(FALSE);
684f3dbc832Seric 
685f3dbc832Seric 	/*
6865dfc646bSeric 	**  Do initial argv setup.
6875dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6885dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6895dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6905dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6915dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6923bea8136Seric 	**		The from address rewrite is expected to make
6933bea8136Seric 	**		the address relative to the other end.
6945dfc646bSeric 	*/
6955dfc646bSeric 
69678442df3Seric 	/* rewrite from address, using rewriting rules */
697efe54562Seric 	rcode = EX_OK;
698b665faabSeric 	if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
699b665faabSeric 		p = e->e_sender;
700b665faabSeric 	else
701b665faabSeric 		p = e->e_from.q_paddr;
702b665faabSeric 	(void) strcpy(rpathbuf, remotename(p, m,
703efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
704efe54562Seric 					   &rcode, e));
705ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
706588cad61Seric 	define('h', host, e);			/* to host */
7075dfc646bSeric 	Errors = 0;
7085dfc646bSeric 	pvp = pv;
7095dfc646bSeric 	*pvp++ = m->m_argv[0];
7105dfc646bSeric 
7115dfc646bSeric 	/* insert -f or -r flag as appropriate */
71257fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
7135dfc646bSeric 	{
71457fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
7155dfc646bSeric 			*pvp++ = "-f";
7165dfc646bSeric 		else
7175dfc646bSeric 			*pvp++ = "-r";
718c23ed322Seric 		*pvp++ = newstr(rpathbuf);
7195dfc646bSeric 	}
7205dfc646bSeric 
7215dfc646bSeric 	/*
7225dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
7235dfc646bSeric 	**  up to the first entry containing "$u".  There can only
7245dfc646bSeric 	**  be one of these, and there are only a few more slots
7255dfc646bSeric 	**  in the pv after it.
7265dfc646bSeric 	*/
7275dfc646bSeric 
7285dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
7295dfc646bSeric 	{
7302bc47524Seric 		/* can't use strchr here because of sign extension problems */
7312bc47524Seric 		while (*p != '\0')
7322bc47524Seric 		{
7332bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
7342bc47524Seric 			{
7352bc47524Seric 				if (*p == 'u')
7365dfc646bSeric 					break;
7372bc47524Seric 			}
7382bc47524Seric 		}
7392bc47524Seric 
7402bc47524Seric 		if (*p != '\0')
7415dfc646bSeric 			break;
7425dfc646bSeric 
7435dfc646bSeric 		/* this entry is safe -- go ahead and process it */
744b665faabSeric 		expand(*mvp, buf, sizeof buf, e);
7455dfc646bSeric 		*pvp++ = newstr(buf);
7465dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
7475dfc646bSeric 		{
74808b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
7495dfc646bSeric 			return (-1);
7505dfc646bSeric 		}
7515dfc646bSeric 	}
752c579ef51Seric 
75333db8731Seric 	/*
75433db8731Seric 	**  If we have no substitution for the user name in the argument
75533db8731Seric 	**  list, we know that we must supply the names otherwise -- and
75633db8731Seric 	**  SMTP is the answer!!
75733db8731Seric 	*/
75833db8731Seric 
7595dfc646bSeric 	if (*mvp == NULL)
760c579ef51Seric 	{
761c579ef51Seric 		/* running SMTP */
7622c7e1b8dSeric # ifdef SMTP
763c579ef51Seric 		clever = TRUE;
764c579ef51Seric 		*pvp = NULL;
7656c2c3107Seric # else /* SMTP */
76633db8731Seric 		/* oops!  we don't implement SMTP */
767df106f0bSeric 		syserr("554 SMTP style mailer not implemented");
7682c7e1b8dSeric 		return (EX_SOFTWARE);
7696c2c3107Seric # endif /* SMTP */
770c579ef51Seric 	}
7715dfc646bSeric 
7725dfc646bSeric 	/*
7735dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7745dfc646bSeric 	**  run through our address list and append all the addresses
7755dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7765dfc646bSeric 	**  always send another copy later.
7775dfc646bSeric 	*/
7785dfc646bSeric 
7795dfc646bSeric 	tobuf[0] = '\0';
780588cad61Seric 	e->e_to = tobuf;
7816259796dSeric 	ctladdr = NULL;
782e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7835dfc646bSeric 	for (; to != NULL; to = to->q_next)
7845dfc646bSeric 	{
7855dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
78657fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7875dfc646bSeric 			break;
7885dfc646bSeric 
7895dfc646bSeric 		/* if already sent or not for this host, don't send */
790ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
791e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
792e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7935dfc646bSeric 			continue;
7946259796dSeric 
7954b22ea87Seric 		/* avoid overflowing tobuf */
796aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7974b22ea87Seric 			break;
7984b22ea87Seric 
7996ef48975Seric 		if (tTd(10, 1))
800772e6e50Seric 		{
801772e6e50Seric 			printf("\nsend to ");
802772e6e50Seric 			printaddr(to, FALSE);
803772e6e50Seric 		}
804772e6e50Seric 
8056259796dSeric 		/* compute effective uid/gid when sending */
806b665faabSeric 		if (bitnset(M_RUNASRCPT, to->q_mailer->m_flags))
8076259796dSeric 			ctladdr = getctladdr(to);
8086259796dSeric 
809b665faabSeric 		if (tTd(10, 2))
810b665faabSeric 		{
811b665faabSeric 			printf("ctladdr=");
812b665faabSeric 			printaddr(ctladdr, FALSE);
813b665faabSeric 		}
814b665faabSeric 
8155dfc646bSeric 		user = to->q_user;
816588cad61Seric 		e->e_to = to->q_paddr;
81775f1ade9Seric 		if (tTd(10, 5))
81875f1ade9Seric 		{
81975f1ade9Seric 			printf("deliver: QDONTSEND ");
82075f1ade9Seric 			printaddr(to, FALSE);
82175f1ade9Seric 		}
822ee4b0922Seric 		to->q_flags |= QDONTSEND;
8235dfc646bSeric 
8245dfc646bSeric 		/*
8255dfc646bSeric 		**  Check to see that these people are allowed to
8265dfc646bSeric 		**  talk to each other.
8272a6e0786Seric 		*/
8282a6e0786Seric 
82969582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
83069582d2fSeric 		{
831b665faabSeric 			e->e_flags |= EF_NO_BODY_RETN;
832a6a073b5Seric 			to->q_status = "5.2.3";
83308b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
834b665faabSeric 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, xstart, e);
83569582d2fSeric 			continue;
83669582d2fSeric 		}
837b665faabSeric #if NAMED_BIND
838b665faabSeric 		h_errno = 0;
839b665faabSeric #endif
840fabb3bd4Seric 		rcode = checkcompat(to, e);
8411793c9c5Seric 		if (rcode != EX_OK)
8425dfc646bSeric 		{
843b665faabSeric 			markfailure(e, to, NULL, rcode);
844b665faabSeric 			giveresponse(rcode, m, NULL, ctladdr, xstart, e);
8455dfc646bSeric 			continue;
8465dfc646bSeric 		}
8472a6e0786Seric 
8482a6e0786Seric 		/*
8499ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
8509ec9501bSeric 		**	about them.
85125a99e2eSeric 		*/
85225a99e2eSeric 
85357fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
85425a99e2eSeric 		{
8551d8f1806Seric 			stripquotes(user);
8561d8f1806Seric 			stripquotes(host);
85725a99e2eSeric 		}
85825a99e2eSeric 
859cdb828c5Seric 		/* hack attack -- delivermail compatibility */
860cdb828c5Seric 		if (m == ProgMailer && *user == '|')
861cdb828c5Seric 			user++;
862cdb828c5Seric 
86325a99e2eSeric 		/*
8643efaed6eSeric 		**  If an error message has already been given, don't
8653efaed6eSeric 		**	bother to send to this address.
8663efaed6eSeric 		**
8673efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
8683efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
8693efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
8703efaed6eSeric 		*/
8713efaed6eSeric 
8726cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8733efaed6eSeric 			continue;
8743efaed6eSeric 
875f2fec898Seric 		/* save statistics.... */
876588cad61Seric 		markstats(e, to);
877f2fec898Seric 
8783efaed6eSeric 		/*
87925a99e2eSeric 		**  See if this user name is "special".
88025a99e2eSeric 		**	If the user name has a slash in it, assume that this
88151552439Seric 		**	is a file -- send it off without further ado.  Note
88251552439Seric 		**	that this type of addresses is not processed along
88351552439Seric 		**	with the others, so we fudge on the To person.
88425a99e2eSeric 		*/
88525a99e2eSeric 
88623a75c08Seric 		if (strcmp(m->m_mailer, "[FILE]") == 0)
88725a99e2eSeric 		{
88804a36717Seric 			rcode = mailfile(user, ctladdr, SFF_CREAT, e);
889b665faabSeric 			giveresponse(rcode, m, NULL, ctladdr, xstart, e);
890b665faabSeric 			e->e_nsent++;
891dde5acadSeric 			if (rcode == EX_OK)
892b665faabSeric 			{
893dde5acadSeric 				to->q_flags |= QSENT;
894b665faabSeric 				if (bitnset(M_LOCALMAILER, m->m_flags) &&
895b665faabSeric 				    (e->e_receiptto != NULL ||
896b665faabSeric 				     bitset(QPINGONSUCCESS, to->q_flags)))
897b665faabSeric 				{
8989070d157Seric 					to->q_flags |= QDELIVERED;
899d95dad3eSeric 					to->q_status = "2.1.5";
900b665faabSeric 					fprintf(e->e_xfp, "%s... Successfully delivered\n",
901b665faabSeric 						to->q_paddr);
902b665faabSeric 				}
903b665faabSeric 			}
904b665faabSeric 			to->q_statdate = curtime();
9055dfc646bSeric 			continue;
90625a99e2eSeric 		}
90725a99e2eSeric 
90813bbc08cSeric 		/*
90913bbc08cSeric 		**  Address is verified -- add this user to mailer
91013bbc08cSeric 		**  argv, and add it to the print list of recipients.
91113bbc08cSeric 		*/
91213bbc08cSeric 
913508daeccSeric 		/* link together the chain of recipients */
914508daeccSeric 		to->q_tchain = tochain;
915508daeccSeric 		tochain = to;
916508daeccSeric 
9175dfc646bSeric 		/* create list of users for error messages */
918db8841e9Seric 		(void) strcat(tobuf, ",");
919db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
920588cad61Seric 		define('u', user, e);		/* to user */
921b6f89e6eSeric 		p = to->q_home;
922b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
923b6f89e6eSeric 			p = ctladdr->q_home;
924b6f89e6eSeric 		define('z', p, e);	/* user's home */
9255dfc646bSeric 
926c579ef51Seric 		/*
927508daeccSeric 		**  Expand out this user into argument list.
928c579ef51Seric 		*/
929c579ef51Seric 
930508daeccSeric 		if (!clever)
931c579ef51Seric 		{
932b665faabSeric 			expand(*mvp, buf, sizeof buf, e);
9335dfc646bSeric 			*pvp++ = newstr(buf);
9345dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
9355dfc646bSeric 			{
9365dfc646bSeric 				/* allow some space for trailing parms */
9375dfc646bSeric 				break;
9385dfc646bSeric 			}
9395dfc646bSeric 		}
940c579ef51Seric 	}
9415dfc646bSeric 
942145b49b1Seric 	/* see if any addresses still exist */
943145b49b1Seric 	if (tobuf[0] == '\0')
944c579ef51Seric 	{
945588cad61Seric 		define('g', (char *) NULL, e);
946145b49b1Seric 		return (0);
947c579ef51Seric 	}
948145b49b1Seric 
9495dfc646bSeric 	/* print out messages as full list */
95063780dbdSeric 	e->e_to = tobuf + 1;
9515dfc646bSeric 
9525dfc646bSeric 	/*
9535dfc646bSeric 	**  Fill out any parameters after the $u parameter.
9545dfc646bSeric 	*/
9555dfc646bSeric 
956c579ef51Seric 	while (!clever && *++mvp != NULL)
9575dfc646bSeric 	{
958b665faabSeric 		expand(*mvp, buf, sizeof buf, e);
9595dfc646bSeric 		*pvp++ = newstr(buf);
9605dfc646bSeric 		if (pvp >= &pv[MAXPV])
96108b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
9625dfc646bSeric 	}
9635dfc646bSeric 	*pvp++ = NULL;
9645dfc646bSeric 
96525a99e2eSeric 	/*
96625a99e2eSeric 	**  Call the mailer.
9676328bdf7Seric 	**	The argument vector gets built, pipes
96825a99e2eSeric 	**	are created as necessary, and we fork & exec as
9696328bdf7Seric 	**	appropriate.
970c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
97125a99e2eSeric 	*/
97225a99e2eSeric 
973960e665aSeric 	/*XXX this seems a bit wierd */
974c875d42bSeric 	if (ctladdr == NULL && m != ProgMailer && m != FileMailer &&
9754899f6b5Seric 	    bitset(QGOODUID, e->e_from.q_flags))
976960e665aSeric 		ctladdr = &e->e_from;
977960e665aSeric 
9789d4a8008Seric #if NAMED_BIND
9792bcc6d2dSeric 	if (ConfigLevel < 2)
980912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
981134746fbSeric #endif
9829c9e68d9Seric 
9839c9e68d9Seric 	if (tTd(11, 1))
984134746fbSeric 	{
9859c9e68d9Seric 		printf("openmailer:");
9869c9e68d9Seric 		printav(pv);
9879c9e68d9Seric 	}
9889c9e68d9Seric 	errno = 0;
989b665faabSeric #if NAMED_BIND
990b665faabSeric 	h_errno = 0;
991b665faabSeric #endif
9929c9e68d9Seric 
993b665faabSeric 	CurHostName = NULL;
9949c9e68d9Seric 
9959c9e68d9Seric 	/*
9969c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9979c9e68d9Seric 	**  connection.
9989c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9999c9e68d9Seric 	**	running SMTP for this to work.  We will return a
10009c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
10019c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
10029c9e68d9Seric 	*/
10039c9e68d9Seric 
10049c9e68d9Seric 	curhost = NULL;
1005c931b82bSeric 	SmtpPhase = NULL;
1006df106f0bSeric 	mci = NULL;
10079c9e68d9Seric 
100886aab53cSeric #if XDEBUG
10097febfd66Seric 	{
10107febfd66Seric 		char wbuf[MAXLINE];
10117febfd66Seric 
10127febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
10137febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
10147febfd66Seric 		checkfd012(wbuf);
10157febfd66Seric 	}
10167febfd66Seric #endif
10177febfd66Seric 
1018b665faabSeric 	/* check for 8-bit available */
1019b665faabSeric 	if (bitset(EF_HAS8BIT, e->e_flags) &&
1020b665faabSeric 	    bitnset(M_7BITS, m->m_flags) &&
1021a1999208Seric 	    (!bitset(MM_MIME8BIT, MimeMode) ||
1022a1999208Seric 	     bitset(EF_DONT_MIME, e->e_flags)))
1023b665faabSeric 	{
1024b665faabSeric 		usrerr("554 Cannot send 8-bit data to 7-bit destination");
1025b665faabSeric 		rcode = EX_DATAERR;
10265b068d51Seric 		e->e_status = "5.6.3";
1027b665faabSeric 		goto give_up;
1028b665faabSeric 	}
1029b665faabSeric 
10309c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
10319c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
10329c9e68d9Seric 	{
10339c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
10349c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
10359c9e68d9Seric 		mci->mci_in = stdin;
10369c9e68d9Seric 		mci->mci_out = stdout;
10379c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
10389c9e68d9Seric 		mci->mci_mailer = m;
10399c9e68d9Seric 	}
10409c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
10419c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
10429c9e68d9Seric 	{
10439c9e68d9Seric #ifdef DAEMON
10449c9e68d9Seric 		register int i;
1045b665faabSeric 		register u_short port = 0;
10469c9e68d9Seric 
1047a5d44642Seric 		if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
1048a5d44642Seric 		{
1049a5d44642Seric 			syserr("null host name for %s mailer", m->m_mailer);
1050a5d44642Seric 			rcode = EX_CONFIG;
1051a5d44642Seric 			goto give_up;
1052a5d44642Seric 		}
1053a5d44642Seric 
10549c9e68d9Seric 		CurHostName = pv[1];
10559c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
10569c9e68d9Seric 
10579c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
10589c9e68d9Seric 		{
1059a5d44642Seric 			syserr("null host signature for %s", pv[1]);
1060dfebe401Seric 			rcode = EX_CONFIG;
1061b31e7f2bSeric 			goto give_up;
1062b31e7f2bSeric 		}
10639c9e68d9Seric 
10649c9e68d9Seric 		if (!clever)
10659c9e68d9Seric 		{
10669c9e68d9Seric 			syserr("554 non-clever IPC");
1067df106f0bSeric 			rcode = EX_CONFIG;
10689c9e68d9Seric 			goto give_up;
10699c9e68d9Seric 		}
10709c9e68d9Seric 		if (pv[2] != NULL)
10710784fccbSeric 		{
10720784fccbSeric 			port = htons(atoi(pv[2]));
10730784fccbSeric 			if (port == 0)
10740784fccbSeric 			{
10750784fccbSeric 				struct servent *sp = getservbyname(pv[2], "tcp");
10760784fccbSeric 
10770784fccbSeric 				if (sp == NULL)
10780784fccbSeric 					syserr("Service %s unknown", pv[2]);
10790784fccbSeric 				else
10800784fccbSeric 					port = sp->s_port;
10810784fccbSeric 			}
10820784fccbSeric 		}
10839c9e68d9Seric tryhost:
10849c9e68d9Seric 		while (*curhost != '\0')
10859c9e68d9Seric 		{
10869c9e68d9Seric 			register char *p;
1087b665faabSeric 			static char hostbuf[MAXNAME + 1];
10889c9e68d9Seric 
10899c9e68d9Seric 			/* pull the next host from the signature */
10909c9e68d9Seric 			p = strchr(curhost, ':');
10919c9e68d9Seric 			if (p == NULL)
10929c9e68d9Seric 				p = &curhost[strlen(curhost)];
1093df106f0bSeric 			if (p == curhost)
1094df106f0bSeric 			{
1095df106f0bSeric 				syserr("deliver: null host name in signature");
10968087428aSeric 				curhost++;
1097df106f0bSeric 				continue;
1098df106f0bSeric 			}
10999c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
11009c9e68d9Seric 			hostbuf[p - curhost] = '\0';
11019c9e68d9Seric 			if (*p != '\0')
11029c9e68d9Seric 				p++;
11039c9e68d9Seric 			curhost = p;
11049c9e68d9Seric 
11059c9e68d9Seric 			/* see if we already know that this host is fried */
11069c9e68d9Seric 			CurHostName = hostbuf;
11079c9e68d9Seric 			mci = mci_get(hostbuf, m);
11089c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
11099c9e68d9Seric 			{
11109c9e68d9Seric 				if (tTd(11, 1))
11119c9e68d9Seric 				{
11129c9e68d9Seric 					printf("openmailer: ");
11134052916eSeric 					mci_dump(mci, FALSE);
11149c9e68d9Seric 				}
11159c9e68d9Seric 				CurHostName = mci->mci_host;
1116b665faabSeric 				message("Using cached connection to %s via %s...",
1117b665faabSeric 					hostbuf, m->m_name);
11189c9e68d9Seric 				break;
11199c9e68d9Seric 			}
11209c9e68d9Seric 			mci->mci_mailer = m;
11219c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
11229c9e68d9Seric 				continue;
11239c9e68d9Seric 
11249c9e68d9Seric 			/* try the connection */
11259c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
1126b665faabSeric 			message("Connecting to %s via %s...",
11279c9e68d9Seric 				hostbuf, m->m_name);
11289c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
11299c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
11309c9e68d9Seric 			mci->mci_exitstat = i;
11319c9e68d9Seric 			mci->mci_errno = errno;
11329d4a8008Seric #if NAMED_BIND
1133f170942cSeric 			mci->mci_herrno = h_errno;
1134f170942cSeric #endif
11359c9e68d9Seric 			if (i == EX_OK)
11369c9e68d9Seric 			{
11379c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
11389c9e68d9Seric 				mci_cache(mci);
1139f170942cSeric 				if (TrafficLogFile != NULL)
1140f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1141f170942cSeric 						getpid(), hostbuf);
11429c9e68d9Seric 				break;
11439c9e68d9Seric 			}
11449c9e68d9Seric 			else if (tTd(11, 1))
11459c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
11469c9e68d9Seric 					i, errno);
11479c9e68d9Seric 
11489c9e68d9Seric 			/* enter status of this host */
11499c9e68d9Seric 			setstat(i);
1150df106f0bSeric 
1151df106f0bSeric 			/* should print some message here for -v mode */
1152df106f0bSeric 		}
1153df106f0bSeric 		if (mci == NULL)
1154df106f0bSeric 		{
1155df106f0bSeric 			syserr("deliver: no host name");
1156df106f0bSeric 			rcode = EX_OSERR;
1157df106f0bSeric 			goto give_up;
11589c9e68d9Seric 		}
11599c9e68d9Seric 		mci->mci_pid = 0;
11609c9e68d9Seric #else /* no DAEMON */
11619c9e68d9Seric 		syserr("554 openmailer: no IPC");
11629c9e68d9Seric 		if (tTd(11, 1))
11639c9e68d9Seric 			printf("openmailer: NULL\n");
1164df106f0bSeric 		rcode = EX_UNAVAILABLE;
1165df106f0bSeric 		goto give_up;
11669c9e68d9Seric #endif /* DAEMON */
11679c9e68d9Seric 	}
11689c9e68d9Seric 	else
11699c9e68d9Seric 	{
1170b665faabSeric 		/* flush any expired connections */
1171b665faabSeric 		(void) mci_scan(NULL);
1172b665faabSeric 
1173b665faabSeric 		/* announce the connection to verbose listeners */
1174b665faabSeric 		if (host == NULL || host[0] == '\0')
1175b665faabSeric 			message("Connecting to %s...", m->m_name);
1176b665faabSeric 		else
1177b665faabSeric 			message("Connecting to %s via %s...", host, m->m_name);
1178f170942cSeric 		if (TrafficLogFile != NULL)
11790e3bfef5Seric 		{
1180f170942cSeric 			char **av;
1181f170942cSeric 
1182f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1183f170942cSeric 			for (av = pv; *av != NULL; av++)
1184f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1185f170942cSeric 			fprintf(TrafficLogFile, "\n");
11866fe8c3bcSeric 		}
11876fe8c3bcSeric 
11889c9e68d9Seric 		/* create a pipe to shove the mail through */
11899c9e68d9Seric 		if (pipe(mpvect) < 0)
11909c9e68d9Seric 		{
11910e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
11920e3bfef5Seric 				e->e_to, m->m_name);
11939c9e68d9Seric 			if (tTd(11, 1))
11949c9e68d9Seric 				printf("openmailer: NULL\n");
11959c9e68d9Seric 			rcode = EX_OSERR;
11969c9e68d9Seric 			goto give_up;
11979c9e68d9Seric 		}
11989c9e68d9Seric 
11999c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
12009c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
12019c9e68d9Seric 		{
12020e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
12030e3bfef5Seric 				e->e_to, m->m_name);
12049c9e68d9Seric 			(void) close(mpvect[0]);
12059c9e68d9Seric 			(void) close(mpvect[1]);
12069c9e68d9Seric 			if (tTd(11, 1))
12079c9e68d9Seric 				printf("openmailer: NULL\n");
12089c9e68d9Seric 			rcode = EX_OSERR;
12099c9e68d9Seric 			goto give_up;
12109c9e68d9Seric 		}
12119c9e68d9Seric 
12129c9e68d9Seric 		/*
12139c9e68d9Seric 		**  Actually fork the mailer process.
12149c9e68d9Seric 		**	DOFORK is clever about retrying.
12159c9e68d9Seric 		**
12169c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
12179c9e68d9Seric 		**	around so that endmail will get it.
12189c9e68d9Seric 		*/
12199c9e68d9Seric 
12209c9e68d9Seric 		if (e->e_xfp != NULL)
12219c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
12229c9e68d9Seric 		(void) fflush(stdout);
12239c9e68d9Seric # ifdef SIGCHLD
12242b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
12259c9e68d9Seric # endif /* SIGCHLD */
12269c9e68d9Seric 		DOFORK(FORK);
12279c9e68d9Seric 		/* pid is set by DOFORK */
12289c9e68d9Seric 		if (pid < 0)
12299c9e68d9Seric 		{
12309c9e68d9Seric 			/* failure */
12310e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
12320e3bfef5Seric 				e->e_to, m->m_name);
12339c9e68d9Seric 			(void) close(mpvect[0]);
12349c9e68d9Seric 			(void) close(mpvect[1]);
12359c9e68d9Seric 			if (clever)
12369c9e68d9Seric 			{
12379c9e68d9Seric 				(void) close(rpvect[0]);
12389c9e68d9Seric 				(void) close(rpvect[1]);
12399c9e68d9Seric 			}
12409c9e68d9Seric 			if (tTd(11, 1))
12419c9e68d9Seric 				printf("openmailer: NULL\n");
12429c9e68d9Seric 			rcode = EX_OSERR;
12439c9e68d9Seric 			goto give_up;
12449c9e68d9Seric 		}
12459c9e68d9Seric 		else if (pid == 0)
12469c9e68d9Seric 		{
12479c9e68d9Seric 			int i;
12489c9e68d9Seric 			int saveerrno;
1249f24c4f81Seric 			struct stat stb;
12509c9e68d9Seric 			extern int DtableSize;
12519c9e68d9Seric 
1252f2e4d5e8Seric 			if (e->e_lockfp != NULL)
1253ac5a49f9Seric 				(void) close(fileno(e->e_lockfp));
1254f2e4d5e8Seric 
12559c9e68d9Seric 			/* child -- set up input & exec mailer */
12562b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
12572b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
12582b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
12599c9e68d9Seric 
126092051585Seric 			if (m != FileMailer || stat(tochain->q_user, &stb) < 0)
1261f24c4f81Seric 				stb.st_mode = 0;
1262f24c4f81Seric 
126335427e30Seric 			/* tweak niceness */
126435427e30Seric 			if (m->m_nice != 0)
126535427e30Seric 				nice(m->m_nice);
126635427e30Seric 
1267f24c4f81Seric 			/* reset group id */
1268b665faabSeric 			if (bitnset(M_SPECIFIC_UID, m->m_flags))
1269b665faabSeric 				(void) setgid(m->m_gid);
1270f24c4f81Seric 			else if (bitset(S_ISGID, stb.st_mode))
1271f24c4f81Seric 				(void) setgid(stb.st_gid);
1272f24c4f81Seric 			else if (ctladdr != NULL && ctladdr->q_gid != 0)
127344f2317fSeric 			{
127444f2317fSeric 				(void) initgroups(ctladdr->q_ruser?
127544f2317fSeric 					ctladdr->q_ruser: ctladdr->q_user,
127644f2317fSeric 					ctladdr->q_gid);
127751cfe111Seric 				(void) setgid(ctladdr->q_gid);
127844f2317fSeric 			}
1279b665faabSeric 			else
1280b665faabSeric 			{
1281b665faabSeric 				(void) initgroups(DefUser, DefGid);
1282b665faabSeric 				if (m->m_gid == 0)
1283b665faabSeric 					(void) setgid(DefGid);
1284b665faabSeric 				else
1285b665faabSeric 					(void) setgid(m->m_gid);
1286f24c4f81Seric 			}
1287f24c4f81Seric 
1288f24c4f81Seric 			/* reset user id */
1289*3e164d5bSeric 			endpwent();
1290f24c4f81Seric 			if (bitnset(M_SPECIFIC_UID, m->m_flags))
1291f24c4f81Seric 				(void) setuid(m->m_uid);
1292f24c4f81Seric 			else if (bitset(S_ISUID, stb.st_mode))
1293f24c4f81Seric 				(void) setuid(stb.st_uid);
1294f24c4f81Seric 			else if (ctladdr != NULL && ctladdr->q_uid != 0)
1295f24c4f81Seric 				(void) setuid(ctladdr->q_uid);
1296f24c4f81Seric 			else
1297f24c4f81Seric 			{
1298b665faabSeric 				if (m->m_uid == 0)
1299b665faabSeric 					(void) setuid(DefUid);
1300b665faabSeric 				else
1301b665faabSeric 					(void) setuid(m->m_uid);
130244f2317fSeric 			}
130344f2317fSeric 
130444f2317fSeric 			if (tTd(11, 2))
130544f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
130644f2317fSeric 					getuid(), geteuid());
130744f2317fSeric 
1308b986f6aaSeric 			/* move into some "safe" directory */
1309b986f6aaSeric 			if (m->m_execdir != NULL)
1310b986f6aaSeric 			{
1311b986f6aaSeric 				char *p, *q;
1312b665faabSeric 				char buf[MAXLINE + 1];
1313b986f6aaSeric 
1314b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1315b986f6aaSeric 				{
1316b986f6aaSeric 					q = strchr(p, ':');
1317b986f6aaSeric 					if (q != NULL)
1318b986f6aaSeric 						*q = '\0';
1319b665faabSeric 					expand(p, buf, sizeof buf, e);
1320b986f6aaSeric 					if (q != NULL)
1321b986f6aaSeric 						*q++ = ':';
1322b986f6aaSeric 					if (tTd(11, 20))
1323b986f6aaSeric 						printf("openmailer: trydir %s\n",
1324b986f6aaSeric 							buf);
1325b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1326b986f6aaSeric 						break;
1327b986f6aaSeric 				}
1328b986f6aaSeric 			}
1329b986f6aaSeric 
13309c9e68d9Seric 			/* arrange to filter std & diag output of command */
13319c9e68d9Seric 			if (clever)
13329c9e68d9Seric 			{
13339c9e68d9Seric 				(void) close(rpvect[0]);
13346fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
13356fe8c3bcSeric 				{
13360e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
13370e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
13386fe8c3bcSeric 					_exit(EX_OSERR);
13396fe8c3bcSeric 				}
13409c9e68d9Seric 				(void) close(rpvect[1]);
13419c9e68d9Seric 			}
13428b40b0a4Seric 			else if (OpMode == MD_SMTP || OpMode == MD_DAEMON ||
13438b40b0a4Seric 				  HoldErrs || DisConnected)
13449c9e68d9Seric 			{
13459c9e68d9Seric 				/* put mailer output in transcript */
13466fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
13476fe8c3bcSeric 				{
13480e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
13490e3bfef5Seric 						e->e_to, m->m_name,
13506fe8c3bcSeric 						fileno(e->e_xfp));
13516fe8c3bcSeric 					_exit(EX_OSERR);
13529c9e68d9Seric 				}
13536fe8c3bcSeric 			}
13546fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
13556fe8c3bcSeric 			{
13560e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
13570e3bfef5Seric 					e->e_to, m->m_name);
13586fe8c3bcSeric 				_exit(EX_OSERR);
13596fe8c3bcSeric 			}
13609c9e68d9Seric 
13619c9e68d9Seric 			/* arrange to get standard input */
13629c9e68d9Seric 			(void) close(mpvect[1]);
13639c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
13649c9e68d9Seric 			{
13650e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
13660e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
13679c9e68d9Seric 				_exit(EX_OSERR);
13689c9e68d9Seric 			}
13699c9e68d9Seric 			(void) close(mpvect[0]);
13709c9e68d9Seric 
13719c9e68d9Seric 			/* arrange for all the files to be closed */
13729c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
13739c9e68d9Seric 			{
13749c9e68d9Seric 				register int j;
137544f2317fSeric 
13769c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
13779c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
13789c9e68d9Seric 			}
13799c9e68d9Seric 
1380929277f2Seric 			/* run disconnected from terminal */
1381929277f2Seric 			(void) setsid();
1382929277f2Seric 
13839c9e68d9Seric 			/* try to execute the mailer */
13845d5999efSeric 			execve(m->m_mailer, (ARGV_T) pv, (ARGV_T) UserEnviron);
13859c9e68d9Seric 			saveerrno = errno;
13869c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
1387b665faabSeric 			if (bitnset(M_LOCALMAILER, m->m_flags) ||
1388b665faabSeric 			    transienterror(saveerrno))
13897c941fd2Seric 				_exit(EX_OSERR);
13909c9e68d9Seric 			_exit(EX_UNAVAILABLE);
13919c9e68d9Seric 		}
13929c9e68d9Seric 
13939c9e68d9Seric 		/*
13949c9e68d9Seric 		**  Set up return value.
13959c9e68d9Seric 		*/
13969c9e68d9Seric 
13979c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
13989c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
13999c9e68d9Seric 		mci->mci_mailer = m;
14009c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
14019c9e68d9Seric 		mci->mci_pid = pid;
14029c9e68d9Seric 		(void) close(mpvect[0]);
14039c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1404335eae58Seric 		if (mci->mci_out == NULL)
1405335eae58Seric 		{
1406335eae58Seric 			syserr("deliver: cannot create mailer output channel, fd=%d",
1407335eae58Seric 				mpvect[1]);
1408335eae58Seric 			(void) close(mpvect[1]);
1409335eae58Seric 			if (clever)
1410335eae58Seric 			{
1411335eae58Seric 				(void) close(rpvect[0]);
1412335eae58Seric 				(void) close(rpvect[1]);
1413335eae58Seric 			}
1414335eae58Seric 			rcode = EX_OSERR;
1415335eae58Seric 			goto give_up;
1416335eae58Seric 		}
14179c9e68d9Seric 		if (clever)
14189c9e68d9Seric 		{
14199c9e68d9Seric 			(void) close(rpvect[1]);
14209c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
1421335eae58Seric 			if (mci->mci_in == NULL)
1422335eae58Seric 			{
1423335eae58Seric 				syserr("deliver: cannot create mailer input channel, fd=%d",
1424335eae58Seric 					mpvect[1]);
1425335eae58Seric 				(void) close(rpvect[0]);
1426335eae58Seric 				fclose(mci->mci_out);
1427335eae58Seric 				mci->mci_out = NULL;
1428335eae58Seric 				rcode = EX_OSERR;
1429335eae58Seric 				goto give_up;
1430335eae58Seric 			}
14319c9e68d9Seric 		}
14329c9e68d9Seric 		else
14339c9e68d9Seric 		{
14349c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
14359c9e68d9Seric 			mci->mci_in = NULL;
14369c9e68d9Seric 		}
14379c9e68d9Seric 	}
14389c9e68d9Seric 
14399c9e68d9Seric 	/*
14409c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
14419c9e68d9Seric 	*/
14429c9e68d9Seric 
14439c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
14449c9e68d9Seric 	{
14459c9e68d9Seric 		smtpinit(m, mci, e);
14469c9e68d9Seric 	}
1447b665faabSeric 
1448b665faabSeric 	if (bitset(EF_HAS8BIT, e->e_flags) && bitnset(M_7BITS, m->m_flags))
1449b665faabSeric 		mci->mci_flags |= MCIF_CVT8TO7;
1450b665faabSeric 	else
1451b665faabSeric 		mci->mci_flags &= ~MCIF_CVT8TO7;
1452b665faabSeric 
14539c9e68d9Seric 	if (tTd(11, 1))
14549c9e68d9Seric 	{
14559c9e68d9Seric 		printf("openmailer: ");
14564052916eSeric 		mci_dump(mci, FALSE);
14579c9e68d9Seric 	}
14589c9e68d9Seric 
14599c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1460b31e7f2bSeric 	{
1461b31e7f2bSeric 		/* couldn't open the mailer */
1462b31e7f2bSeric 		rcode = mci->mci_exitstat;
14632a6bc25bSeric 		errno = mci->mci_errno;
14649d4a8008Seric #if NAMED_BIND
1465f170942cSeric 		h_errno = mci->mci_herrno;
1466f170942cSeric #endif
1467b31e7f2bSeric 		if (rcode == EX_OK)
1468b31e7f2bSeric 		{
1469b31e7f2bSeric 			/* shouldn't happen */
147008b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
14716b0f339dSeric 				rcode, mci->mci_state, firstsig);
1472b31e7f2bSeric 			rcode = EX_SOFTWARE;
1473b31e7f2bSeric 		}
1474b665faabSeric 		else if (curhost != NULL && *curhost != '\0')
147590891494Seric 		{
147690891494Seric 			/* try next MX site */
147790891494Seric 			goto tryhost;
147890891494Seric 		}
1479b31e7f2bSeric 	}
1480b31e7f2bSeric 	else if (!clever)
1481b31e7f2bSeric 	{
1482b31e7f2bSeric 		/*
1483b31e7f2bSeric 		**  Format and send message.
1484b31e7f2bSeric 		*/
148515d084d5Seric 
14865aa0f353Seric 		putfromline(mci, e);
1487b665faabSeric 		(*e->e_puthdr)(mci, e->e_header, e);
14886ffc2dd1Seric 		(*e->e_putbody)(mci, e, NULL);
1489b31e7f2bSeric 
1490b31e7f2bSeric 		/* get the exit status */
1491c9be6216Seric 		rcode = endmailer(mci, e, pv);
1492134746fbSeric 	}
1493134746fbSeric 	else
1494b31e7f2bSeric #ifdef SMTP
1495134746fbSeric 	{
1496b31e7f2bSeric 		/*
1497b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1498b31e7f2bSeric 		*/
149915d084d5Seric 
1500b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1501b31e7f2bSeric 		if (rcode == EX_OK)
150275889e88Seric 		{
1503ded0d3daSkarels 			register char *t = tobuf;
1504ded0d3daSkarels 			register int i;
1505ded0d3daSkarels 
1506588cad61Seric 			/* send the recipient list */
150763780dbdSeric 			tobuf[0] = '\0';
150875889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
150975889e88Seric 			{
151063780dbdSeric 				e->e_to = to->q_paddr;
151115d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
151275889e88Seric 				{
1513b665faabSeric 					markfailure(e, to, mci, i);
1514b665faabSeric 					giveresponse(i, m, mci, ctladdr, xstart, e);
151563780dbdSeric 				}
151675889e88Seric 				else
151775889e88Seric 				{
1518911693bfSbostic 					*t++ = ',';
1519b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1520b31e7f2bSeric 						continue;
1521ee39df61Seric 					*t = '\0';
1522588cad61Seric 				}
1523588cad61Seric 			}
1524588cad61Seric 
152563780dbdSeric 			/* now send the data */
152663780dbdSeric 			if (tobuf[0] == '\0')
1527b31e7f2bSeric 			{
15289c9e68d9Seric 				rcode = EX_OK;
152963780dbdSeric 				e->e_to = NULL;
1530b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1531b31e7f2bSeric 					smtprset(m, mci, e);
1532b31e7f2bSeric 			}
153375889e88Seric 			else
153475889e88Seric 			{
153563780dbdSeric 				e->e_to = tobuf + 1;
153675889e88Seric 				rcode = smtpdata(m, mci, e);
153763780dbdSeric 			}
153863780dbdSeric 
153963780dbdSeric 			/* now close the connection */
1540b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
154115d084d5Seric 				smtpquit(m, mci, e);
154263780dbdSeric 		}
1543cb082c5bSeric 		if (rcode != EX_OK && curhost != NULL && *curhost != '\0')
15449c9e68d9Seric 		{
15459c9e68d9Seric 			/* try next MX site */
15469c9e68d9Seric 			goto tryhost;
15479c9e68d9Seric 		}
1548c579ef51Seric 	}
1549b31e7f2bSeric #else /* not SMTP */
1550a05b3449Sbostic 	{
155108b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1552845e533cSeric 		rcode = EX_CONFIG;
1553b31e7f2bSeric 		goto give_up;
1554a05b3449Sbostic 	}
1555b31e7f2bSeric #endif /* SMTP */
15569d4a8008Seric #if NAMED_BIND
15572bcc6d2dSeric 	if (ConfigLevel < 2)
1558912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1559134746fbSeric #endif
15605dfc646bSeric 
1561b31e7f2bSeric 	/* arrange a return receipt if requested */
1562df106f0bSeric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1563df106f0bSeric 	    bitnset(M_LOCALMAILER, m->m_flags))
1564b31e7f2bSeric 	{
1565b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1566b31e7f2bSeric 		/* do we want to send back more info? */
1567b31e7f2bSeric 	}
1568b31e7f2bSeric 
1569c77d1c25Seric 	/*
157063780dbdSeric 	**  Do final status disposal.
157163780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1572c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1573c77d1c25Seric 	**		addressees.
1574c77d1c25Seric 	*/
1575c77d1c25Seric 
1576b31e7f2bSeric   give_up:
157763780dbdSeric 	if (tobuf[0] != '\0')
1578b665faabSeric 		giveresponse(rcode, m, mci, ctladdr, xstart, e);
1579772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1580b31e7f2bSeric 	{
1581dde5acadSeric 		if (rcode != EX_OK)
1582b665faabSeric 			markfailure(e, to, mci, rcode);
1583b665faabSeric 		else if (!bitset(QBADADDR|QQUEUEUP, to->q_flags))
1584655518ecSeric 		{
1585dde5acadSeric 			to->q_flags |= QSENT;
1586b665faabSeric 			to->q_statdate = curtime();
1587655518ecSeric 			e->e_nsent++;
1588b665faabSeric 			if (bitnset(M_LOCALMAILER, m->m_flags) &&
1589b665faabSeric 			    (e->e_receiptto != NULL ||
1590b665faabSeric 			     bitset(QPINGONSUCCESS, to->q_flags)))
1591df106f0bSeric 			{
15929070d157Seric 				to->q_flags |= QDELIVERED;
1593d95dad3eSeric 				to->q_status = "2.1.5";
1594df106f0bSeric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1595df106f0bSeric 					to->q_paddr);
1596df106f0bSeric 			}
1597b665faabSeric 			else if (bitset(QPINGONSUCCESS, to->q_flags) &&
1598b665faabSeric 				 bitset(QPRIMARY, to->q_flags) &&
1599b665faabSeric 				 !bitset(MCIF_DSN, mci->mci_flags))
1600b665faabSeric 			{
1601b665faabSeric 				to->q_flags |= QRELAYED;
1602b665faabSeric 				fprintf(e->e_xfp, "%s... relayed; expect no further notifications\n",
1603b665faabSeric 					to->q_paddr);
1604b665faabSeric 			}
1605655518ecSeric 		}
1606b31e7f2bSeric 	}
1607b31e7f2bSeric 
1608b31e7f2bSeric 	/*
1609b31e7f2bSeric 	**  Restore state and return.
1610b31e7f2bSeric 	*/
1611c77d1c25Seric 
161286aab53cSeric #if XDEBUG
16137febfd66Seric 	{
16147febfd66Seric 		char wbuf[MAXLINE];
16157febfd66Seric 
16167febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
161744622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
161844622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
161944622ea3Seric 			m->m_name);
16207febfd66Seric 		checkfd012(wbuf);
16217febfd66Seric 	}
16227febfd66Seric #endif
16237febfd66Seric 
162435490626Seric 	errno = 0;
1625588cad61Seric 	define('g', (char *) NULL, e);
16265826d9d3Seric 	return (rcode);
162725a99e2eSeric }
16285dfc646bSeric /*
162983b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
163083b7ddc9Seric **
163183b7ddc9Seric **	Parameters:
163283b7ddc9Seric **		e -- the envelope we are sending.
163383b7ddc9Seric **		q -- the address to mark.
1634b665faabSeric **		mci -- mailer connection information.
163583b7ddc9Seric **		rcode -- the code signifying the particular failure.
163683b7ddc9Seric **
163783b7ddc9Seric **	Returns:
163883b7ddc9Seric **		none.
163983b7ddc9Seric **
164083b7ddc9Seric **	Side Effects:
164183b7ddc9Seric **		marks the address (and possibly the envelope) with the
164283b7ddc9Seric **			failure so that an error will be returned or
164383b7ddc9Seric **			the message will be queued, as appropriate.
164483b7ddc9Seric */
164583b7ddc9Seric 
1646a6a073b5Seric void
markfailure(e,q,mci,rcode)1647b665faabSeric markfailure(e, q, mci, rcode)
164883b7ddc9Seric 	register ENVELOPE *e;
164983b7ddc9Seric 	register ADDRESS *q;
1650b665faabSeric 	register MCI *mci;
165183b7ddc9Seric 	int rcode;
165283b7ddc9Seric {
1653b665faabSeric 	char *stat = NULL;
165419c47125Seric 
1655d5aafa3cSeric 	switch (rcode)
1656d5aafa3cSeric 	{
1657d5aafa3cSeric 	  case EX_OK:
1658d5aafa3cSeric 		break;
1659d5aafa3cSeric 
1660d5aafa3cSeric 	  case EX_TEMPFAIL:
1661d5aafa3cSeric 	  case EX_IOERR:
1662d5aafa3cSeric 	  case EX_OSERR:
166383b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1664d5aafa3cSeric 		break;
1665d5aafa3cSeric 
1666d5aafa3cSeric 	  default:
1667f170942cSeric 		q->q_flags |= QBADADDR;
1668d5aafa3cSeric 		break;
1669d5aafa3cSeric 	}
1670b665faabSeric 
16715b068d51Seric 	/* find most specific error code possible */
1672b665faabSeric 	if (q->q_status == NULL && mci != NULL)
1673b665faabSeric 		q->q_status = mci->mci_status;
16745b068d51Seric 	if (q->q_status == NULL)
16755b068d51Seric 		q->q_status = e->e_status;
16765b068d51Seric 	if (q->q_status == NULL)
16775b068d51Seric 	{
1678b665faabSeric 		switch (rcode)
1679b665faabSeric 		{
1680b665faabSeric 		  case EX_USAGE:
1681b665faabSeric 			stat = "5.5.4";
1682b665faabSeric 			break;
1683b665faabSeric 
1684b665faabSeric 		  case EX_DATAERR:
1685b665faabSeric 			stat = "5.5.2";
1686b665faabSeric 			break;
1687b665faabSeric 
1688b665faabSeric 		  case EX_NOUSER:
1689b665faabSeric 			stat = "5.1.1";
1690b665faabSeric 			break;
1691b665faabSeric 
16925b068d51Seric 		  case EX_NOHOST:
16935b068d51Seric 			stat = "5.1.2";
16945b068d51Seric 			break;
16955b068d51Seric 
1696b665faabSeric 		  case EX_NOINPUT:
1697b665faabSeric 		  case EX_CANTCREAT:
1698b665faabSeric 		  case EX_NOPERM:
1699b665faabSeric 			stat = "5.3.0";
1700b665faabSeric 			break;
1701b665faabSeric 
1702b665faabSeric 		  case EX_UNAVAILABLE:
1703b665faabSeric 		  case EX_SOFTWARE:
1704b665faabSeric 		  case EX_OSFILE:
1705b665faabSeric 		  case EX_PROTOCOL:
1706b665faabSeric 		  case EX_CONFIG:
1707b665faabSeric 			stat = "5.5.0";
1708b665faabSeric 			break;
1709b665faabSeric 
1710b665faabSeric 		  case EX_OSERR:
1711b665faabSeric 		  case EX_IOERR:
1712b665faabSeric 			stat = "4.5.0";
1713b665faabSeric 			break;
1714b665faabSeric 
1715b665faabSeric 		  case EX_TEMPFAIL:
1716b665faabSeric 			stat = "4.2.0";
1717b665faabSeric 			break;
1718b665faabSeric 		}
1719d95dad3eSeric 		if (stat != NULL)
1720b665faabSeric 			q->q_status = stat;
17215b068d51Seric 	}
1722b665faabSeric 
1723b665faabSeric 	q->q_statdate = curtime();
1724b665faabSeric 	if (CurHostName != NULL && CurHostName[0] != '\0')
1725b665faabSeric 		q->q_statmta = newstr(CurHostName);
1726b665faabSeric 	if (rcode != EX_OK && q->q_rstatus == NULL)
1727b665faabSeric 	{
1728b665faabSeric 		char buf[30];
1729b665faabSeric 
1730b665faabSeric 		(void) sprintf(buf, "%d", rcode);
1731b665faabSeric 		q->q_rstatus = newstr(buf);
1732b665faabSeric 	}
173383b7ddc9Seric }
173483b7ddc9Seric /*
1735c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1736c579ef51Seric **
1737c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1738c579ef51Seric **	violation), so we report those specially.  For other
1739c579ef51Seric **	errors, we choose a status message (into statmsg),
1740c579ef51Seric **	and if it represents an error, we print it.
1741c579ef51Seric **
1742c579ef51Seric **	Parameters:
1743c579ef51Seric **		pid -- pid of mailer.
1744c9be6216Seric **		e -- the current envelope.
1745c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1746c9be6216Seric **			(for error messages).
1747c579ef51Seric **
1748c579ef51Seric **	Returns:
1749c579ef51Seric **		exit code of mailer.
1750c579ef51Seric **
1751c579ef51Seric **	Side Effects:
1752c579ef51Seric **		none.
1753c579ef51Seric */
1754c579ef51Seric 
1755a6a073b5Seric int
endmailer(mci,e,pv)1756c9be6216Seric endmailer(mci, e, pv)
1757b31e7f2bSeric 	register MCI *mci;
1758c9be6216Seric 	register ENVELOPE *e;
1759c9be6216Seric 	char **pv;
1760c579ef51Seric {
1761588cad61Seric 	int st;
1762c579ef51Seric 
176375889e88Seric 	/* close any connections */
176475889e88Seric 	if (mci->mci_in != NULL)
176576f3d53fSeric 		(void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in");
176675889e88Seric 	if (mci->mci_out != NULL)
176776f3d53fSeric 		(void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out");
176875889e88Seric 	mci->mci_in = mci->mci_out = NULL;
176975889e88Seric 	mci->mci_state = MCIS_CLOSED;
177075889e88Seric 
177133db8731Seric 	/* in the IPC case there is nothing to wait for */
177275889e88Seric 	if (mci->mci_pid == 0)
177333db8731Seric 		return (EX_OK);
177433db8731Seric 
177533db8731Seric 	/* wait for the mailer process to die and collect status */
177675889e88Seric 	st = waitfor(mci->mci_pid);
1777588cad61Seric 	if (st == -1)
177878de67c1Seric 	{
1779c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1780588cad61Seric 		return (EX_SOFTWARE);
1781c579ef51Seric 	}
178233db8731Seric 
1783bf9bc890Seric 	if (WIFEXITED(st))
1784c579ef51Seric 	{
1785bf9bc890Seric 		/* normal death -- return status */
1786bf9bc890Seric 		return (WEXITSTATUS(st));
1787bf9bc890Seric 	}
1788bf9bc890Seric 
1789bf9bc890Seric 	/* it died a horrid death */
1790550092c3Seric 	syserr("451 mailer %s died with signal %o",
1791550092c3Seric 		mci->mci_mailer->m_name, st);
1792c9be6216Seric 
1793c9be6216Seric 	/* log the arguments */
17943c930db3Seric 	if (pv != NULL && e->e_xfp != NULL)
1795c9be6216Seric 	{
1796c9be6216Seric 		register char **av;
1797c9be6216Seric 
1798c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1799c9be6216Seric 		for (av = pv; *av != NULL; av++)
1800c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1801c9be6216Seric 		fprintf(e->e_xfp, "\n");
1802c9be6216Seric 	}
1803c9be6216Seric 
18045f73204aSeric 	ExitStat = EX_TEMPFAIL;
18055f73204aSeric 	return (EX_TEMPFAIL);
1806c579ef51Seric }
1807c579ef51Seric /*
180825a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
180925a99e2eSeric **
181025a99e2eSeric **	Parameters:
181125a99e2eSeric **		stat -- the status code from the mailer (high byte
181225a99e2eSeric **			only; core dumps must have been taken care of
181325a99e2eSeric **			already).
181481161401Seric **		m -- the mailer info for this mailer.
181581161401Seric **		mci -- the mailer connection info -- can be NULL if the
181681161401Seric **			response is given before the connection is made.
18174dee0003Seric **		ctladdr -- the controlling address for the recipient
18184dee0003Seric **			address(es).
1819b665faabSeric **		xstart -- the transaction start time, for computing
1820b665faabSeric **			transaction delays.
182181161401Seric **		e -- the current envelope.
182225a99e2eSeric **
182325a99e2eSeric **	Returns:
1824db8841e9Seric **		none.
182525a99e2eSeric **
182625a99e2eSeric **	Side Effects:
1827c1f9df2cSeric **		Errors may be incremented.
182825a99e2eSeric **		ExitStat may be set.
182925a99e2eSeric */
183025a99e2eSeric 
1831a6a073b5Seric void
giveresponse(stat,m,mci,ctladdr,xstart,e)1832b665faabSeric giveresponse(stat, m, mci, ctladdr, xstart, e)
183325a99e2eSeric 	int stat;
1834588cad61Seric 	register MAILER *m;
183581161401Seric 	register MCI *mci;
18364dee0003Seric 	ADDRESS *ctladdr;
1837b665faabSeric 	time_t xstart;
1838198d9be0Seric 	ENVELOPE *e;
183925a99e2eSeric {
18409c16475dSeric 	register const char *statmsg;
184125a99e2eSeric 	extern char *SysExMsg[];
184225a99e2eSeric 	register int i;
1843d4bd8f0eSbostic 	extern int N_SysEx;
1844198d9be0Seric 	char buf[MAXLINE];
184525a99e2eSeric 
184613bbc08cSeric 	/*
184713bbc08cSeric 	**  Compute status message from code.
184813bbc08cSeric 	*/
184913bbc08cSeric 
185025a99e2eSeric 	i = stat - EX__BASE;
1851588cad61Seric 	if (stat == 0)
18526fe8c3bcSeric 	{
1853588cad61Seric 		statmsg = "250 Sent";
1854ce5531bdSeric 		if (e->e_statmsg != NULL)
18556fe8c3bcSeric 		{
1856ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
18576fe8c3bcSeric 			statmsg = buf;
18586fe8c3bcSeric 		}
18596fe8c3bcSeric 	}
1860588cad61Seric 	else if (i < 0 || i > N_SysEx)
1861588cad61Seric 	{
1862588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1863588cad61Seric 		stat = EX_UNAVAILABLE;
1864588cad61Seric 		statmsg = buf;
1865588cad61Seric 	}
1866198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1867198d9be0Seric 	{
18687d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
18699d4a8008Seric #if NAMED_BIND
1870f28da541Smiriam 		if (h_errno == TRY_AGAIN)
18714d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1872f28da541Smiriam 		else
1873d4bd8f0eSbostic #endif
1874f28da541Smiriam 		{
18758557d168Seric 			if (errno != 0)
1876d87e85f3Seric 				statmsg = errstring(errno);
1877d87e85f3Seric 			else
1878d87e85f3Seric 			{
1879d87e85f3Seric #ifdef SMTP
1880d87e85f3Seric 				statmsg = SmtpError;
18816c2c3107Seric #else /* SMTP */
1882d87e85f3Seric 				statmsg = NULL;
18836c2c3107Seric #endif /* SMTP */
1884d87e85f3Seric 			}
1885f28da541Smiriam 		}
1886d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1887d87e85f3Seric 		{
188887c9b3e7Seric 			(void) strcat(buf, ": ");
1889d87e85f3Seric 			(void) strcat(buf, statmsg);
18908557d168Seric 		}
1891198d9be0Seric 		statmsg = buf;
1892198d9be0Seric 	}
18939d4a8008Seric #if NAMED_BIND
1894f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1895f170942cSeric 	{
18964d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1897862934b2Seric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i] + 1, statmsg);
1898f170942cSeric 		statmsg = buf;
1899f170942cSeric 	}
1900f170942cSeric #endif
190125a99e2eSeric 	else
1902d87e85f3Seric 	{
190325a99e2eSeric 		statmsg = SysExMsg[i];
19047d55540cSeric 		if (*statmsg++ == ':')
19057d55540cSeric 		{
19067d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
19077d55540cSeric 			statmsg = buf;
19087d55540cSeric 		}
1909d87e85f3Seric 	}
1910588cad61Seric 
1911588cad61Seric 	/*
1912588cad61Seric 	**  Print the message as appropriate
1913588cad61Seric 	*/
1914588cad61Seric 
1915198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1916df106f0bSeric 	{
1917df106f0bSeric 		extern char MsgBuf[];
1918df106f0bSeric 
19191f96bd0bSeric 		message("%s", &statmsg[4]);
1920df106f0bSeric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1921df106f0bSeric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1922df106f0bSeric 	}
192325a99e2eSeric 	else
192425a99e2eSeric 	{
1925862934b2Seric 		char mbuf[8];
1926862934b2Seric 
1927c1f9df2cSeric 		Errors++;
1928862934b2Seric 		sprintf(mbuf, "%.3s %%s", statmsg);
1929862934b2Seric 		usrerr(mbuf, &statmsg[4]);
193025a99e2eSeric 	}
193125a99e2eSeric 
193225a99e2eSeric 	/*
193325a99e2eSeric 	**  Final cleanup.
193425a99e2eSeric 	**	Log a record of the transaction.  Compute the new
193525a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
193625a99e2eSeric 	**	that.
193725a99e2eSeric 	*/
193825a99e2eSeric 
19392f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
1940b665faabSeric 		logdelivery(m, mci, &statmsg[4], ctladdr, xstart, e);
1941eb238f8cSeric 
19425d3f0ed4Seric 	if (tTd(11, 2))
19435d3f0ed4Seric 		printf("giveresponse: stat=%d, e->e_message=%s\n",
19442c8ac7e0Seric 			stat, e->e_message == NULL ? "<NULL>" : e->e_message);
19455d3f0ed4Seric 
1946eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1947eb238f8cSeric 		setstat(stat);
19481097d4c9Seric 	if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL))
1949198d9be0Seric 	{
1950198d9be0Seric 		if (e->e_message != NULL)
1951198d9be0Seric 			free(e->e_message);
1952198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1953198d9be0Seric 	}
19548557d168Seric 	errno = 0;
19559d4a8008Seric #if NAMED_BIND
1956f28da541Smiriam 	h_errno = 0;
1957d4bd8f0eSbostic #endif
1958eb238f8cSeric }
1959eb238f8cSeric /*
1960eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1961eb238f8cSeric **
19622c9b4f99Seric **	Care is taken to avoid logging lines that are too long, because
19632c9b4f99Seric **	some versions of syslog have an unfortunate proclivity for core
19642c9b4f99Seric **	dumping.  This is a hack, to be sure, that is at best empirical.
19652c9b4f99Seric **
1966eb238f8cSeric **	Parameters:
196781161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
196881161401Seric **		mci -- the mailer connection info -- can be NULL if the
196981161401Seric **			log is occuring when no connection is active.
197081161401Seric **		stat -- the message to print for the status.
19714dee0003Seric **		ctladdr -- the controlling address for the to list.
1972b665faabSeric **		xstart -- the transaction start time, used for
1973b665faabSeric **			computing transaction delay.
197481161401Seric **		e -- the current envelope.
1975eb238f8cSeric **
1976eb238f8cSeric **	Returns:
1977eb238f8cSeric **		none
1978eb238f8cSeric **
1979eb238f8cSeric **	Side Effects:
1980eb238f8cSeric **		none
1981eb238f8cSeric */
1982eb238f8cSeric 
1983a6a073b5Seric void
logdelivery(m,mci,stat,ctladdr,xstart,e)1984b665faabSeric logdelivery(m, mci, stat, ctladdr, xstart, e)
198581161401Seric 	MAILER *m;
198681161401Seric 	register MCI *mci;
1987a6a073b5Seric 	const char *stat;
19884dee0003Seric 	ADDRESS *ctladdr;
1989b665faabSeric 	time_t xstart;
1990b31e7f2bSeric 	register ENVELOPE *e;
19915cf56be3Seric {
1992eb238f8cSeric # ifdef LOG
19934dee0003Seric 	register char *bp;
19942c9b4f99Seric 	register char *p;
19952c9b4f99Seric 	int l;
1996d6acf3eeSeric 	char buf[512];
19979507d1f9Seric 
19983a100e8bSeric #  if (SYSLOG_BUFSIZE) >= 256
19994dee0003Seric 	bp = buf;
20004dee0003Seric 	if (ctladdr != NULL)
20014dee0003Seric 	{
20024dee0003Seric 		strcpy(bp, ", ctladdr=");
2003f62f08c7Seric 		strcat(bp, shortenstring(ctladdr->q_paddr, 83));
20044dee0003Seric 		bp += strlen(bp);
20054dee0003Seric 		if (bitset(QGOODUID, ctladdr->q_flags))
20064dee0003Seric 		{
20074dee0003Seric 			(void) sprintf(bp, " (%d/%d)",
20084dee0003Seric 					ctladdr->q_uid, ctladdr->q_gid);
20094dee0003Seric 			bp += strlen(bp);
20104dee0003Seric 		}
20114dee0003Seric 	}
20124dee0003Seric 
2013b665faabSeric 	sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
20144dee0003Seric 	bp += strlen(bp);
201581161401Seric 
2016b665faabSeric 	if (xstart != (time_t) 0)
2017b665faabSeric 	{
2018b665faabSeric 		sprintf(bp, ", xdelay=%s", pintvl(curtime() - xstart, TRUE));
2019b665faabSeric 		bp += strlen(bp);
2020b665faabSeric 	}
2021b665faabSeric 
202248ed5d33Seric 	if (m != NULL)
202371ff6caaSeric 	{
20244dee0003Seric 		(void) strcpy(bp, ", mailer=");
20254dee0003Seric 		(void) strcat(bp, m->m_name);
20264dee0003Seric 		bp += strlen(bp);
202771ff6caaSeric 	}
202848ed5d33Seric 
202948ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
203071ff6caaSeric 	{
203171ff6caaSeric # ifdef DAEMON
2032e2f2f828Seric 		extern SOCKADDR CurHostAddr;
203348ed5d33Seric # endif
203471ff6caaSeric 
20354dee0003Seric 		(void) strcpy(bp, ", relay=");
20364dee0003Seric 		(void) strcat(bp, mci->mci_host);
203748ed5d33Seric 
203848ed5d33Seric # ifdef DAEMON
20392bdfc6b9Seric 		(void) strcat(bp, " [");
20404dee0003Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
20412bdfc6b9Seric 		(void) strcat(bp, "]");
204271ff6caaSeric # endif
204371ff6caaSeric 	}
2044bcb9b028Seric 	else if (strcmp(stat, "queued") != 0)
204548ed5d33Seric 	{
204648ed5d33Seric 		char *p = macvalue('h', e);
20479507d1f9Seric 
204848ed5d33Seric 		if (p != NULL && p[0] != '\0')
204948ed5d33Seric 		{
20504dee0003Seric 			(void) strcpy(bp, ", relay=");
20514dee0003Seric 			(void) strcat(bp, p);
205248ed5d33Seric 		}
205348ed5d33Seric 	}
2054cb082c5bSeric 	bp += strlen(bp);
2055d6acf3eeSeric 
20563a100e8bSeric #define STATLEN		(((SYSLOG_BUFSIZE) - 100) / 4)
20573a100e8bSeric #if (STATLEN) < 63
20583a100e8bSeric # undef STATLEN
20593a100e8bSeric # define STATLEN	63
20603a100e8bSeric #endif
20613a100e8bSeric #if (STATLEN) > 203
20623a100e8bSeric # undef STATLEN
20633a100e8bSeric # define STATLEN	203
20643a100e8bSeric #endif
20653a100e8bSeric 
20663a100e8bSeric 	if ((bp - buf) > (sizeof buf - ((STATLEN) + 20)))
20672c9b4f99Seric 	{
20682c9b4f99Seric 		/* desperation move -- truncate data */
20693a100e8bSeric 		bp = buf + sizeof buf - ((STATLEN) + 17);
20702c9b4f99Seric 		strcpy(bp, "...");
20712c9b4f99Seric 		bp += 3;
20722c9b4f99Seric 	}
20732c9b4f99Seric 
20742c9b4f99Seric 	(void) strcpy(bp, ", stat=");
20752c9b4f99Seric 	bp += strlen(bp);
20763a100e8bSeric 
20773a100e8bSeric 	(void) strcpy(bp, shortenstring(stat, (STATLEN)));
20782c9b4f99Seric 
20792c9b4f99Seric 	l = SYSLOG_BUFSIZE - 100 - strlen(buf);
20802c9b4f99Seric 	p = e->e_to;
2081972b1942Seric 	while (strlen(p) >= (SIZE_T) l)
20822c9b4f99Seric 	{
20832c9b4f99Seric 		register char *q = strchr(p + l, ',');
20842c9b4f99Seric 
20852a1b6b73Seric 		if (q == NULL)
20862c9b4f99Seric 			break;
20872c9b4f99Seric 		syslog(LOG_INFO, "%s: to=%.*s [more]%s",
20882c9b4f99Seric 			e->e_id, ++q - p, p, buf);
20892c9b4f99Seric 		p = q;
20902c9b4f99Seric 	}
20912c9b4f99Seric 	syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf);
20923a100e8bSeric 
20933a100e8bSeric #  else		/* we have a very short log buffer size */
20943a100e8bSeric 
209527607809Seric 	l = SYSLOG_BUFSIZE - 85;
20963a100e8bSeric 	p = e->e_to;
20973a100e8bSeric 	while (strlen(p) >= l)
20983a100e8bSeric 	{
20993a100e8bSeric 		register char *q = strchr(p + l, ',');
21003a100e8bSeric 
21013a100e8bSeric 		if (q == NULL)
21023a100e8bSeric 			break;
21033a100e8bSeric 		syslog(LOG_INFO, "%s: to=%.*s [more]",
21043a100e8bSeric 			e->e_id, ++q - p, p);
21053a100e8bSeric 		p = q;
21063a100e8bSeric 	}
21073a100e8bSeric 	syslog(LOG_INFO, "%s: to=%s", e->e_id, p);
21083a100e8bSeric 
21093a100e8bSeric 	if (ctladdr != NULL)
21103a100e8bSeric 	{
21113a100e8bSeric 		bp = buf;
21123a100e8bSeric 		strcpy(buf, "ctladdr=");
21133a100e8bSeric 		bp += strlen(buf);
21143a100e8bSeric 		strcpy(bp, shortenstring(ctladdr->q_paddr, 83));
21153a100e8bSeric 		bp += strlen(buf);
21163a100e8bSeric 		if (bitset(QGOODUID, ctladdr->q_flags))
21173a100e8bSeric 		{
21183a100e8bSeric 			(void) sprintf(bp, " (%d/%d)",
21193a100e8bSeric 					ctladdr->q_uid, ctladdr->q_gid);
21203a100e8bSeric 			bp += strlen(bp);
21213a100e8bSeric 		}
21223a100e8bSeric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
21233a100e8bSeric 	}
21244e797715Seric 	bp = buf;
21254e797715Seric 	sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
21264e797715Seric 	bp += strlen(bp);
2127b665faabSeric 	if (xstart != (time_t) 0)
2128b665faabSeric 	{
2129b665faabSeric 		sprintf(bp, ", xdelay=%s", pintvl(curtime() - xstart, TRUE));
2130b665faabSeric 		bp += strlen(bp);
2131b665faabSeric 	}
21323a100e8bSeric 
21333a100e8bSeric 	if (m != NULL)
21344e797715Seric 	{
21354e797715Seric 		sprintf(bp, ", mailer=%s", m->m_name);
21364e797715Seric 		bp += strlen(bp);
21374e797715Seric 	}
2138b056abd0Seric 	syslog(LOG_INFO, "%s: %s", e->e_id, buf);
21393a100e8bSeric 
2140b056abd0Seric 	buf[0] = '\0';
21413a100e8bSeric 	if (mci != NULL && mci->mci_host != NULL)
21423a100e8bSeric 	{
21433a100e8bSeric # ifdef DAEMON
21443a100e8bSeric 		extern SOCKADDR CurHostAddr;
21453a100e8bSeric # endif
21463a100e8bSeric 
2147d2ece200Seric 		sprintf(buf, "relay=%s", mci->mci_host);
21483a100e8bSeric 
21493a100e8bSeric # ifdef DAEMON
2150b056abd0Seric 		(void) strcat(buf, " [");
2151b056abd0Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
2152b056abd0Seric 		(void) strcat(buf, "]");
21533a100e8bSeric # endif
21543a100e8bSeric 	}
2155bcb9b028Seric 	else if (strcmp(stat, "queued") != 0)
21563a100e8bSeric 	{
21573a100e8bSeric 		char *p = macvalue('h', e);
21583a100e8bSeric 
21593a100e8bSeric 		if (p != NULL && p[0] != '\0')
2160d2ece200Seric 			sprintf(buf, "relay=%s", p);
21613a100e8bSeric 	}
2162b056abd0Seric 	if (buf[0] != '\0')
21634e797715Seric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
21643a100e8bSeric 
21653a100e8bSeric 	syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63));
21663a100e8bSeric #  endif /* short log buffer */
21676c2c3107Seric # endif /* LOG */
216825a99e2eSeric }
216925a99e2eSeric /*
217051552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
217125a99e2eSeric **
217251552439Seric **	This can be made an arbitrary message separator by changing $l
217351552439Seric **
21749b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
21759b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
21769b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
21779b6c17a6Seric **	this kind of antique garbage????
217825a99e2eSeric **
217925a99e2eSeric **	Parameters:
21805aa0f353Seric **		mci -- the connection information.
21815aa0f353Seric **		e -- the envelope.
218225a99e2eSeric **
218325a99e2eSeric **	Returns:
218451552439Seric **		none
218525a99e2eSeric **
218625a99e2eSeric **	Side Effects:
218751552439Seric **		outputs some text to fp.
218825a99e2eSeric */
218925a99e2eSeric 
2190a6a073b5Seric void
putfromline(mci,e)21915aa0f353Seric putfromline(mci, e)
21925aa0f353Seric 	register MCI *mci;
2193b31e7f2bSeric 	ENVELOPE *e;
219425a99e2eSeric {
21952bc47524Seric 	char *template = "\201l\n";
219651552439Seric 	char buf[MAXLINE];
219725a99e2eSeric 
21985aa0f353Seric 	if (bitnset(M_NHDR, mci->mci_mailer->m_flags))
219951552439Seric 		return;
220013bbc08cSeric 
22015aa0f353Seric 	if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags))
220274b6e67bSeric 	{
2203ea09d6edSeric 		char *bang;
2204ea09d6edSeric 		char xbuf[MAXLINE];
220574b6e67bSeric 
2206b665faabSeric 		expand("\201g", buf, sizeof buf, e);
22076c2c3107Seric 		bang = strchr(buf, '!');
220874b6e67bSeric 		if (bang == NULL)
220934fcca25Seric 		{
221034fcca25Seric 			errno = 0;
221134fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
221234fcca25Seric 		}
221374b6e67bSeric 		else
2214588cad61Seric 		{
2215ea09d6edSeric 			*bang++ = '\0';
22162bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
2217ea09d6edSeric 			template = xbuf;
221874b6e67bSeric 		}
2219588cad61Seric 	}
2220b665faabSeric 	expand(template, buf, sizeof buf, e);
2221d95dad3eSeric 	putxline(buf, mci, PXLF_NOTHINGSPECIAL);
2222bc6e2962Seric }
2223bc6e2962Seric /*
222451552439Seric **  PUTBODY -- put the body of a message.
222551552439Seric **
222651552439Seric **	Parameters:
22275aa0f353Seric **		mci -- the connection information.
22289a6a5f55Seric **		e -- the envelope to put out.
222903c02fdeSeric **		separator -- if non-NULL, a message separator that must
223003c02fdeSeric **			not be permitted in the resulting message.
223151552439Seric **
223251552439Seric **	Returns:
223351552439Seric **		none.
223451552439Seric **
223551552439Seric **	Side Effects:
223651552439Seric **		The message is written onto fp.
223751552439Seric */
223851552439Seric 
2239b665faabSeric /* values for output state variable */
2240b665faabSeric #define OS_HEAD		0	/* at beginning of line */
2241b665faabSeric #define OS_CR		1	/* read a carriage return */
2242b665faabSeric #define OS_INLINE	2	/* putting rest of line */
2243b665faabSeric 
2244a6a073b5Seric void
putbody(mci,e,separator)22456ffc2dd1Seric putbody(mci, e, separator)
22465aa0f353Seric 	register MCI *mci;
22479a6a5f55Seric 	register ENVELOPE *e;
224803c02fdeSeric 	char *separator;
224951552439Seric {
225077b52738Seric 	char buf[MAXLINE];
225151552439Seric 
225251552439Seric 	/*
225351552439Seric 	**  Output the body of the message
225451552439Seric 	*/
225551552439Seric 
2256b665faabSeric 	if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags))
22579a6a5f55Seric 	{
2258b665faabSeric 		char *df = queuename(e, 'd');
2259b665faabSeric 
2260b665faabSeric 		e->e_dfp = fopen(df, "r");
22619a6a5f55Seric 		if (e->e_dfp == NULL)
22628f9146b0Srick 			syserr("putbody: Cannot open %s for %s from %s",
2263b665faabSeric 				df, e->e_to, e->e_from.q_paddr);
2264b665faabSeric 	}
2265b665faabSeric 	if (e->e_dfp == NULL)
2266b665faabSeric 	{
2267b665faabSeric 		if (bitset(MCIF_INHEADER, mci->mci_flags))
2268b665faabSeric 		{
2269b665faabSeric 			putline("", mci);
2270b665faabSeric 			mci->mci_flags &= ~MCIF_INHEADER;
2271b665faabSeric 		}
2272b665faabSeric 		putline("<<< No Message Collected >>>", mci);
2273b665faabSeric 		goto endofmessage;
2274b665faabSeric 	}
2275b665faabSeric 	if (e->e_dfino == (ino_t) 0)
2276b665faabSeric 	{
2277b665faabSeric 		struct stat stbuf;
2278b665faabSeric 
2279b665faabSeric 		if (fstat(fileno(e->e_dfp), &stbuf) < 0)
2280b665faabSeric 			e->e_dfino = -1;
2281b665faabSeric 		else
2282b665faabSeric 		{
2283b665faabSeric 			e->e_dfdev = stbuf.st_dev;
2284b665faabSeric 			e->e_dfino = stbuf.st_ino;
2285b665faabSeric 		}
2286b665faabSeric 	}
2287b665faabSeric 	rewind(e->e_dfp);
2288b665faabSeric 
228904cf0889Seric #if MIME8TO7
2290b665faabSeric 	if (bitset(MCIF_CVT8TO7, mci->mci_flags))
2291b665faabSeric 	{
2292b665faabSeric 		char *boundaries[MAXMIMENESTING + 1];
2293b665faabSeric 
2294b665faabSeric 		/*
2295b665faabSeric 		**  Do 8 to 7 bit MIME conversion.
2296b665faabSeric 		*/
2297b665faabSeric 
2298b665faabSeric 		/* make sure it looks like a MIME message */
2299b665faabSeric 		if (hvalue("MIME-Version", e->e_header) == NULL)
2300b665faabSeric 			putline("MIME-Version: 1.0", mci);
2301b665faabSeric 
2302b665faabSeric 		if (hvalue("Content-Type", e->e_header) == NULL)
2303b665faabSeric 		{
2304b665faabSeric 			sprintf(buf, "Content-Type: text/plain; charset=%s",
2305b665faabSeric 				defcharset(e));
2306b665faabSeric 			putline(buf, mci);
2307b665faabSeric 		}
2308b665faabSeric 
2309b665faabSeric 		/* now do the hard work */
2310b665faabSeric 		boundaries[0] = NULL;
2311b665faabSeric 		mime8to7(mci, e->e_header, e, boundaries, M87F_OUTER);
23129a6a5f55Seric 	}
2313bd4e74cdSeric 	else
231404cf0889Seric #endif
2315b665faabSeric 	{
2316b665faabSeric 		int ostate;
2317b665faabSeric 		register char *bp;
2318b665faabSeric 		register char *pbp;
2319b665faabSeric 		register int c;
2320b665faabSeric 		int padc;
2321b665faabSeric 		char *buflim;
2322b665faabSeric 		int pos = 0;
2323b665faabSeric 		char peekbuf[10];
2324b665faabSeric 
2325b665faabSeric 		/* we can pass it through unmodified */
2326b665faabSeric 		if (bitset(MCIF_INHEADER, mci->mci_flags))
2327b665faabSeric 		{
2328b665faabSeric 			putline("", mci);
2329b665faabSeric 			mci->mci_flags &= ~MCIF_INHEADER;
233064187506Seric 		}
2331b665faabSeric 
2332b665faabSeric 		/* determine end of buffer; allow for short mailer lines */
2333b665faabSeric 		buflim = &buf[sizeof buf - 1];
2334b665faabSeric 		if (mci->mci_mailer->m_linelimit > 0 &&
2335b665faabSeric 		    mci->mci_mailer->m_linelimit < sizeof buf - 1)
2336b665faabSeric 			buflim = &buf[mci->mci_mailer->m_linelimit - 1];
2337b665faabSeric 
2338b665faabSeric 		/* copy temp file to output with mapping */
2339b665faabSeric 		ostate = OS_HEAD;
2340b665faabSeric 		bp = buf;
2341b665faabSeric 		pbp = peekbuf;
2342b665faabSeric 		while (!ferror(mci->mci_out))
2343519e7d80Seric 		{
2344b665faabSeric 			register char *xp;
2345b665faabSeric 
2346b665faabSeric 			if (pbp > peekbuf)
2347b665faabSeric 				c = *--pbp;
23484ff9b170Seric 			else if ((c = getc(e->e_dfp)) == EOF)
2349b665faabSeric 				break;
2350b665faabSeric 			if (bitset(MCIF_7BIT, mci->mci_flags))
2351b665faabSeric 				c &= 0x7f;
2352b665faabSeric 			switch (ostate)
2353a6ce3008Seric 			{
2354b665faabSeric 			  case OS_HEAD:
2355b665faabSeric 				if (c != '\r' && c != '\n' && bp < buflim)
2356b665faabSeric 				{
2357b665faabSeric 					*bp++ = c;
2358b665faabSeric 					break;
2359b665faabSeric 				}
2360b665faabSeric 
2361b665faabSeric 				/* check beginning of line for special cases */
2362b665faabSeric 				*bp = '\0';
2363b665faabSeric 				pos = 0;
2364b665faabSeric 				padc = EOF;
23655aa0f353Seric 				if (buf[0] == 'F' &&
23665aa0f353Seric 				    bitnset(M_ESCFROM, mci->mci_mailer->m_flags) &&
2367d6fa2b58Sbostic 				    strncmp(buf, "From ", 5) == 0)
2368b665faabSeric 				{
2369b665faabSeric 					padc = '>';
2370b665faabSeric 				}
2371b665faabSeric 				if (buf[0] == '-' && buf[1] == '-' &&
2372b665faabSeric 				    separator != NULL)
237303c02fdeSeric 				{
237403c02fdeSeric 					/* possible separator */
237503c02fdeSeric 					int sl = strlen(separator);
237603c02fdeSeric 
237703c02fdeSeric 					if (strncmp(&buf[2], separator, sl) == 0)
2378b665faabSeric 						padc = ' ';
237903c02fdeSeric 				}
2380b665faabSeric 				if (buf[0] == '.' &&
2381b665faabSeric 				    bitnset(M_XDOT, mci->mci_mailer->m_flags))
2382b665faabSeric 				{
2383b665faabSeric 					padc = '.';
2384b665faabSeric 				}
2385b665faabSeric 
2386b665faabSeric 				/* now copy out saved line */
2387b665faabSeric 				if (TrafficLogFile != NULL)
2388b665faabSeric 				{
2389b665faabSeric 					fprintf(TrafficLogFile, "%05d >>> ", getpid());
2390b665faabSeric 					if (padc != EOF)
23914ff9b170Seric 						putc(padc, TrafficLogFile);
2392b665faabSeric 					for (xp = buf; xp < bp; xp++)
23934ff9b170Seric 						putc(*xp, TrafficLogFile);
2394b665faabSeric 					if (c == '\n')
2395b665faabSeric 						fputs(mci->mci_mailer->m_eol,
2396b665faabSeric 						      TrafficLogFile);
2397b665faabSeric 				}
2398b665faabSeric 				if (padc != EOF)
2399b665faabSeric 				{
24004ff9b170Seric 					putc(padc, mci->mci_out);
2401b665faabSeric 					pos++;
2402b665faabSeric 				}
2403b665faabSeric 				for (xp = buf; xp < bp; xp++)
24044ff9b170Seric 					putc(*xp, mci->mci_out);
2405b665faabSeric 				if (c == '\n')
2406b665faabSeric 				{
2407b665faabSeric 					fputs(mci->mci_mailer->m_eol,
2408b665faabSeric 					      mci->mci_out);
2409b665faabSeric 					pos = 0;
2410b665faabSeric 				}
2411b665faabSeric 				else
2412b665faabSeric 				{
2413b665faabSeric 					pos += bp - buf;
2414b665faabSeric 					if (c != '\r')
2415b665faabSeric 						*pbp++ = c;
2416b665faabSeric 				}
2417b665faabSeric 				bp = buf;
2418b665faabSeric 
2419b665faabSeric 				/* determine next state */
2420b665faabSeric 				if (c == '\n')
2421b665faabSeric 					ostate = OS_HEAD;
2422b665faabSeric 				else if (c == '\r')
2423b665faabSeric 					ostate = OS_CR;
2424b665faabSeric 				else
2425b665faabSeric 					ostate = OS_INLINE;
2426b665faabSeric 				continue;
2427b665faabSeric 
2428b665faabSeric 			  case OS_CR:
2429b665faabSeric 				if (c == '\n')
2430b665faabSeric 				{
2431b665faabSeric 					/* got CRLF */
2432b665faabSeric 					fputs(mci->mci_mailer->m_eol, mci->mci_out);
2433b665faabSeric 					if (TrafficLogFile != NULL)
2434b665faabSeric 					{
2435b665faabSeric 						fputs(mci->mci_mailer->m_eol,
2436b665faabSeric 						      TrafficLogFile);
2437b665faabSeric 					}
2438b665faabSeric 					ostate = OS_HEAD;
2439b665faabSeric 					continue;
2440b665faabSeric 				}
2441b665faabSeric 
2442b665faabSeric 				/* had a naked carriage return */
2443b665faabSeric 				*pbp++ = c;
2444b665faabSeric 				c = '\r';
2445b665faabSeric 				goto putch;
2446b665faabSeric 
2447b665faabSeric 			  case OS_INLINE:
2448b665faabSeric 				if (c == '\r')
2449b665faabSeric 				{
2450b665faabSeric 					ostate = OS_CR;
2451b665faabSeric 					continue;
2452b665faabSeric 				}
2453b665faabSeric putch:
2454b665faabSeric 				if (mci->mci_mailer->m_linelimit > 0 &&
2455b665faabSeric 				    pos > mci->mci_mailer->m_linelimit &&
2456b665faabSeric 				    c != '\n')
2457b665faabSeric 				{
2458b665faabSeric 					putc('!', mci->mci_out);
2459b665faabSeric 					fputs(mci->mci_mailer->m_eol, mci->mci_out);
2460b665faabSeric 					if (TrafficLogFile != NULL)
2461b665faabSeric 					{
2462b665faabSeric 						fprintf(TrafficLogFile, "!%s",
2463b665faabSeric 							mci->mci_mailer->m_eol);
2464b665faabSeric 					}
2465b665faabSeric 					ostate = OS_HEAD;
2466b665faabSeric 					*pbp++ = c;
2467b665faabSeric 					continue;
2468b665faabSeric 				}
2469b665faabSeric 				if (TrafficLogFile != NULL)
24704ff9b170Seric 					putc(c, TrafficLogFile);
2471b665faabSeric 				putc(c, mci->mci_out);
2472b665faabSeric 				pos++;
2473b665faabSeric 				ostate = c == '\n' ? OS_HEAD : OS_INLINE;
2474b665faabSeric 				break;
2475b665faabSeric 			}
2476b665faabSeric 		}
2477a6ce3008Seric 	}
247851552439Seric 
24799a6a5f55Seric 	if (ferror(e->e_dfp))
248051552439Seric 	{
2481b665faabSeric 		syserr("putbody: df%s: read error", e->e_id);
248251552439Seric 		ExitStat = EX_IOERR;
248351552439Seric 	}
248451552439Seric 
2485b665faabSeric endofmessage:
24860890ba1fSeric 	/* some mailers want extra blank line at end of message */
24875aa0f353Seric 	if (bitnset(M_BLANKEND, mci->mci_mailer->m_flags) &&
24885aa0f353Seric 	    buf[0] != '\0' && buf[0] != '\n')
24895aa0f353Seric 		putline("", mci);
24900890ba1fSeric 
24915aa0f353Seric 	(void) fflush(mci->mci_out);
24925aa0f353Seric 	if (ferror(mci->mci_out) && errno != EPIPE)
249351552439Seric 	{
249451552439Seric 		syserr("putbody: write error");
249551552439Seric 		ExitStat = EX_IOERR;
249651552439Seric 	}
249751552439Seric 	errno = 0;
249825a99e2eSeric }
249925a99e2eSeric /*
250025a99e2eSeric **  MAILFILE -- Send a message to a file.
250125a99e2eSeric **
2502f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
2503f129ec7dSeric **	bits, sendmail will try to become the owner of that file
2504f129ec7dSeric **	rather than the real user.  Obviously, this only works if
2505f129ec7dSeric **	sendmail runs as root.
2506f129ec7dSeric **
2507588cad61Seric **	This could be done as a subordinate mailer, except that it
2508588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
2509588cad61Seric **	view this as being sufficiently important as to include it
2510588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
2511588cad61Seric **	to create another process plus some pipes to save the message.
2512588cad61Seric **
251325a99e2eSeric **	Parameters:
251425a99e2eSeric **		filename -- the name of the file to send to.
25156259796dSeric **		ctladdr -- the controlling address header -- includes
25166259796dSeric **			the userid/groupid to be when sending.
251704a36717Seric **		sfflags -- flags for opening.
251804a36717Seric **		e -- the current envelope.
251925a99e2eSeric **
252025a99e2eSeric **	Returns:
252125a99e2eSeric **		The exit code associated with the operation.
252225a99e2eSeric **
252325a99e2eSeric **	Side Effects:
252425a99e2eSeric **		none.
252525a99e2eSeric */
252625a99e2eSeric 
2527a6a073b5Seric int
mailfile(filename,ctladdr,sfflags,e)252804a36717Seric mailfile(filename, ctladdr, sfflags, e)
252925a99e2eSeric 	char *filename;
25306259796dSeric 	ADDRESS *ctladdr;
253104a36717Seric 	int sfflags;
2532b31e7f2bSeric 	register ENVELOPE *e;
253325a99e2eSeric {
253425a99e2eSeric 	register FILE *f;
2535b665faabSeric 	register int pid = -1;
253615d084d5Seric 	int mode;
253725a99e2eSeric 
2538671745f3Seric 	if (tTd(11, 1))
2539671745f3Seric 	{
2540671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
2541671745f3Seric 		printaddr(ctladdr, FALSE);
2542671745f3Seric 	}
2543671745f3Seric 
2544f170942cSeric 	if (e->e_xfp != NULL)
2545f170942cSeric 		fflush(e->e_xfp);
2546f170942cSeric 
254732d19d43Seric 	/*
254832d19d43Seric 	**  Fork so we can change permissions here.
254932d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
255032d19d43Seric 	**	the complications of calling subroutines, etc.
255132d19d43Seric 	*/
255232d19d43Seric 
255332d19d43Seric 	DOFORK(fork);
255432d19d43Seric 
255532d19d43Seric 	if (pid < 0)
255632d19d43Seric 		return (EX_OSERR);
255732d19d43Seric 	else if (pid == 0)
255832d19d43Seric 	{
255932d19d43Seric 		/* child -- actually write to file */
2560f129ec7dSeric 		struct stat stb;
2561f2e4d5e8Seric 		struct stat fsb;
25625aa0f353Seric 		MCI mcibuf;
25634ef13b77Seric 		int oflags = O_WRONLY|O_APPEND;
2564f129ec7dSeric 
2565f2e4d5e8Seric 		if (e->e_lockfp != NULL)
256652099264Seric 			(void) close(fileno(e->e_lockfp));
2567f2e4d5e8Seric 
25682b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
25692b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
25702b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
25713462ad9eSeric 		(void) umask(OldUmask);
257204a36717Seric 		e->e_to = filename;
257304a36717Seric 		ExitStat = EX_OK;
257495f16dc0Seric 
2575b665faabSeric #ifdef HASLSTAT
2576b665faabSeric 		if ((SafeFileEnv != NULL ? lstat(filename, &stb)
2577b665faabSeric 					 : stat(filename, &stb)) < 0)
2578b665faabSeric #else
2579f129ec7dSeric 		if (stat(filename, &stb) < 0)
2580b665faabSeric #endif
25814ef13b77Seric 		{
25823a98e7eaSeric 			stb.st_mode = FileMode;
25834ef13b77Seric 			oflags |= O_CREAT|O_EXCL;
25844ef13b77Seric 		}
2585b665faabSeric 		else if (bitset(0111, stb.st_mode) || stb.st_nlink != 1 ||
2586b665faabSeric 			 (SafeFileEnv != NULL && !S_ISREG(stb.st_mode)))
2587f2e4d5e8Seric 			exit(EX_CANTCREAT);
258815d084d5Seric 		mode = stb.st_mode;
258995f16dc0Seric 
259095f16dc0Seric 		/* limit the errors to those actually caused in the child */
259195f16dc0Seric 		errno = 0;
259295f16dc0Seric 		ExitStat = EX_OK;
259395f16dc0Seric 
259404a36717Seric 		if (ctladdr != NULL || bitset(SFF_RUNASREALUID, sfflags))
259515d084d5Seric 		{
259615d084d5Seric 			/* ignore setuid and setgid bits */
259715d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
259815d084d5Seric 		}
259915d084d5Seric 
26008f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
2601b665faabSeric 		if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags))
26028f9146b0Srick 		{
2603b665faabSeric 			char *df = queuename(e, 'd');
2604b665faabSeric 
2605b665faabSeric 			e->e_dfp = fopen(df, "r");
260695f16dc0Seric 			if (e->e_dfp == NULL)
260795f16dc0Seric 			{
26088f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
2609b665faabSeric 					df, e->e_to, e->e_from.q_paddr);
26108f9146b0Srick 			}
26118f9146b0Srick 		}
26128f9146b0Srick 
2613b665faabSeric 		if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0')
2614b665faabSeric 		{
2615b665faabSeric 			int i;
2616b665faabSeric 
2617b665faabSeric 			if (chroot(SafeFileEnv) < 0)
2618b665faabSeric 			{
2619b665faabSeric 				syserr("mailfile: Cannot chroot(%s)",
2620b665faabSeric 					SafeFileEnv);
2621b665faabSeric 				exit(EX_CANTCREAT);
2622b665faabSeric 			}
2623b665faabSeric 			i = strlen(SafeFileEnv);
2624b665faabSeric 			if (strncmp(SafeFileEnv, filename, i) == 0)
2625b665faabSeric 				filename += i;
2626b665faabSeric 		}
2627b665faabSeric 		if (chdir("/") < 0)
2628b665faabSeric 			syserr("mailfile: cannot chdir(/)");
2629b665faabSeric 
263004a36717Seric 		/* select a new user to run as */
263104a36717Seric 		if (!bitset(SFF_RUNASREALUID, sfflags))
2632e36b99e2Seric 		{
263304a36717Seric 			if (bitset(S_ISUID, mode))
263404a36717Seric 			{
263504a36717Seric 				RealUserName = NULL;
263604a36717Seric 				RealUid = stb.st_uid;
2637e36b99e2Seric 			}
263804a36717Seric 			else if (ctladdr != NULL && ctladdr->q_uid != 0)
2639e36b99e2Seric 			{
264004a36717Seric 				if (ctladdr->q_ruser != NULL)
264104a36717Seric 					RealUserName = ctladdr->q_ruser;
264204a36717Seric 				else
264304a36717Seric 					RealUserName = ctladdr->q_user;
264404a36717Seric 				RealUid = ctladdr->q_uid;
264504a36717Seric 			}
2646b665faabSeric 			else if (FileMailer != NULL && FileMailer->m_uid != 0)
264704a36717Seric 			{
264804a36717Seric 				RealUserName = DefUser;
264904a36717Seric 				RealUid = FileMailer->m_uid;
2650e36b99e2Seric 			}
265104a36717Seric 			else
265204a36717Seric 			{
265304a36717Seric 				RealUserName = DefUser;
265404a36717Seric 				RealUid = DefUid;
265504a36717Seric 			}
265604a36717Seric 
265704a36717Seric 			/* select a new group to run as */
265804a36717Seric 			if (bitset(S_ISGID, mode))
265904a36717Seric 				RealGid = stb.st_gid;
266004a36717Seric 			else if (ctladdr != NULL && ctladdr->q_uid != 0)
266104a36717Seric 				RealGid = ctladdr->q_gid;
266204a36717Seric 			else if (FileMailer != NULL && FileMailer->m_gid != 0)
266304a36717Seric 				RealGid = FileMailer->m_gid;
266404a36717Seric 			else
266504a36717Seric 				RealGid = DefGid;
266604a36717Seric 		}
266704a36717Seric 
266804a36717Seric 		/* last ditch */
266904a36717Seric 		if (!bitset(SFF_ROOTOK, sfflags))
267004a36717Seric 		{
267104a36717Seric 			if (RealUid == 0)
267204a36717Seric 				RealUid = DefUid;
267304a36717Seric 			if (RealGid == 0)
267404a36717Seric 				RealGid = DefGid;
267504a36717Seric 		}
267604a36717Seric 
267704a36717Seric 		/* now set the group and user ids */
2678*3e164d5bSeric 		endpwent();
267904a36717Seric 		if (RealUserName != NULL)
268004a36717Seric 			(void) initgroups(RealUserName, RealGid);
268104a36717Seric 		else
268204a36717Seric 			(void) setgid(RealGid);
268304a36717Seric 		(void) setuid(RealUid);
268404a36717Seric 
268504a36717Seric 		sfflags |= SFF_NOPATHCHECK;
268604a36717Seric 		sfflags &= ~SFF_OPENASROOT;
268704a36717Seric 		f = safefopen(filename, oflags, FileMode, sfflags);
268825a99e2eSeric 		if (f == NULL)
268995f16dc0Seric 		{
2690b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
269132d19d43Seric 			exit(EX_CANTCREAT);
269295f16dc0Seric 		}
269325a99e2eSeric 
26945aa0f353Seric 		bzero(&mcibuf, sizeof mcibuf);
26955aa0f353Seric 		mcibuf.mci_mailer = FileMailer;
26965aa0f353Seric 		mcibuf.mci_out = f;
26975aa0f353Seric 		if (bitnset(M_7BITS, FileMailer->m_flags))
26985aa0f353Seric 			mcibuf.mci_flags |= MCIF_7BIT;
26995aa0f353Seric 
27005aa0f353Seric 		putfromline(&mcibuf, e);
2701b665faabSeric 		(*e->e_puthdr)(&mcibuf, e->e_header, e);
27026ffc2dd1Seric 		(*e->e_putbody)(&mcibuf, e, NULL);
27035aa0f353Seric 		putline("\n", &mcibuf);
270495f16dc0Seric 		if (ferror(f))
270595f16dc0Seric 		{
2706b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
270795f16dc0Seric 			setstat(EX_IOERR);
270895f16dc0Seric 		}
2709ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
271032d19d43Seric 		(void) fflush(stdout);
2711e36b99e2Seric 
271227628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
2713c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
271495f16dc0Seric 		exit(ExitStat);
271513bbc08cSeric 		/*NOTREACHED*/
271632d19d43Seric 	}
271732d19d43Seric 	else
271832d19d43Seric 	{
271932d19d43Seric 		/* parent -- wait for exit status */
2720588cad61Seric 		int st;
272132d19d43Seric 
2722588cad61Seric 		st = waitfor(pid);
2723bf9bc890Seric 		if (WIFEXITED(st))
2724bf9bc890Seric 			return (WEXITSTATUS(st));
2725588cad61Seric 		else
2726b6a0de9dSeric 		{
2727b6a0de9dSeric 			syserr("child died on signal %d", st);
2728bf9bc890Seric 			return (EX_UNAVAILABLE);
2729b6a0de9dSeric 		}
27308f9146b0Srick 		/*NOTREACHED*/
273132d19d43Seric 	}
273225a99e2eSeric }
2733ea4dc939Seric /*
2734e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2735e103b48fSeric **
2736e103b48fSeric **	The signature describes how we are going to send this -- it
2737e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2738e103b48fSeric **	an ordered list of MX hosts.
2739e103b48fSeric **
2740e103b48fSeric **	Parameters:
2741e103b48fSeric **		m -- the mailer describing this host.
2742e103b48fSeric **		host -- the host name.
2743e103b48fSeric **		e -- the current envelope.
2744e103b48fSeric **
2745e103b48fSeric **	Returns:
2746e103b48fSeric **		The signature for this host.
2747e103b48fSeric **
2748e103b48fSeric **	Side Effects:
2749e103b48fSeric **		Can tweak the symbol table.
2750e103b48fSeric */
2751e103b48fSeric 
2752e103b48fSeric char *
hostsignature(m,host,e)2753e103b48fSeric hostsignature(m, host, e)
2754e103b48fSeric 	register MAILER *m;
2755e103b48fSeric 	char *host;
2756e103b48fSeric 	ENVELOPE *e;
2757e103b48fSeric {
2758e103b48fSeric 	register char *p;
2759e103b48fSeric 	register STAB *s;
2760e103b48fSeric 	int i;
2761e103b48fSeric 	int len;
27629d4a8008Seric #if NAMED_BIND
2763e103b48fSeric 	int nmx;
2764e103b48fSeric 	auto int rcode;
2765bafdc4e5Seric 	char *hp;
2766bafdc4e5Seric 	char *endp;
2767b665faabSeric 	int oldoptions = _res.options;
2768e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2769e103b48fSeric #endif
2770e103b48fSeric 
2771e103b48fSeric 	/*
2772e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2773e103b48fSeric 	*/
2774e103b48fSeric 
2775e103b48fSeric 	p = m->m_mailer;
2776e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2777e103b48fSeric 	{
2778e103b48fSeric 		/* just an ordinary mailer */
2779e103b48fSeric 		return host;
2780e103b48fSeric 	}
2781e103b48fSeric 
2782e103b48fSeric 	/*
2783e103b48fSeric 	**  Look it up in the symbol table.
2784e103b48fSeric 	*/
2785e103b48fSeric 
2786e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2787e103b48fSeric 	if (s->s_hostsig != NULL)
2788e103b48fSeric 		return s->s_hostsig;
2789e103b48fSeric 
2790e103b48fSeric 	/*
2791e103b48fSeric 	**  Not already there -- create a signature.
2792e103b48fSeric 	*/
2793e103b48fSeric 
27949d4a8008Seric #if NAMED_BIND
2795516782b4Seric 	if (ConfigLevel < 2)
2796516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2797516782b4Seric 
2798bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2799bafdc4e5Seric 	{
2800bafdc4e5Seric 		endp = strchr(hp, ':');
2801bafdc4e5Seric 		if (endp != NULL)
2802bafdc4e5Seric 			*endp = '\0';
2803bafdc4e5Seric 
28047bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
28057d55540cSeric 
2806e103b48fSeric 		if (nmx <= 0)
2807e103b48fSeric 		{
2808e103b48fSeric 			register MCI *mci;
2809e103b48fSeric 
2810e103b48fSeric 			/* update the connection info for this host */
2811bafdc4e5Seric 			mci = mci_get(hp, m);
2812e103b48fSeric 			mci->mci_exitstat = rcode;
2813e103b48fSeric 			mci->mci_errno = errno;
2814f170942cSeric 			mci->mci_herrno = h_errno;
2815e103b48fSeric 
2816e103b48fSeric 			/* and return the original host name as the signature */
2817bafdc4e5Seric 			nmx = 1;
2818bafdc4e5Seric 			mxhosts[0] = hp;
2819e103b48fSeric 		}
2820e103b48fSeric 
2821e103b48fSeric 		len = 0;
2822e103b48fSeric 		for (i = 0; i < nmx; i++)
2823e103b48fSeric 		{
2824e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2825e103b48fSeric 		}
2826bafdc4e5Seric 		if (s->s_hostsig != NULL)
2827bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2828bafdc4e5Seric 		p = xalloc(len);
2829bafdc4e5Seric 		if (s->s_hostsig != NULL)
2830bafdc4e5Seric 		{
2831bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2832bafdc4e5Seric 			free(s->s_hostsig);
2833bafdc4e5Seric 			s->s_hostsig = p;
2834bafdc4e5Seric 			p += strlen(p);
2835bafdc4e5Seric 			*p++ = ':';
2836bafdc4e5Seric 		}
2837bafdc4e5Seric 		else
2838bafdc4e5Seric 			s->s_hostsig = p;
2839e103b48fSeric 		for (i = 0; i < nmx; i++)
2840e103b48fSeric 		{
2841e103b48fSeric 			if (i != 0)
2842e103b48fSeric 				*p++ = ':';
2843e103b48fSeric 			strcpy(p, mxhosts[i]);
2844e103b48fSeric 			p += strlen(p);
2845e103b48fSeric 		}
2846bafdc4e5Seric 		if (endp != NULL)
2847bafdc4e5Seric 			*endp++ = ':';
2848bafdc4e5Seric 	}
2849e103b48fSeric 	makelower(s->s_hostsig);
2850516782b4Seric 	if (ConfigLevel < 2)
2851516782b4Seric 		_res.options = oldoptions;
2852e103b48fSeric #else
2853e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2854e103b48fSeric 	s->s_hostsig = host;
2855e103b48fSeric #endif
2856e103b48fSeric 	if (tTd(17, 1))
2857e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2858e103b48fSeric 	return s->s_hostsig;
2859e103b48fSeric }
2860