14227346bSdist /*
20942ea6aSbostic  * Copyright (c) 1983 Eric P. Allman
32b191fa3Sbostic  * Copyright (c) 1988, 1993
42b191fa3Sbostic  *	The Regents of the University of California.  All rights reserved.
5e70a7521Sbostic  *
63bc94712Sbostic  * %sccs.include.redist.c%
74227346bSdist  */
84227346bSdist 
94227346bSdist #ifndef lint
10*b6f89e6eSeric static char sccsid[] = "@(#)deliver.c	8.28 (Berkeley) 10/02/93";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14f28da541Smiriam #include <netdb.h>
15911693bfSbostic #include <errno.h>
16134746fbSeric #ifdef NAMED_BIND
17912a731aSbostic #include <arpa/nameser.h>
18912a731aSbostic #include <resolv.h>
19f170942cSeric 
20f170942cSeric extern int	h_errno;
21134746fbSeric #endif
2225a99e2eSeric 
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 
419c9e68d9Seric sendall(e, mode)
429c9e68d9Seric 	ENVELOPE *e;
439c9e68d9Seric 	char mode;
449c9e68d9Seric {
459c9e68d9Seric 	register ADDRESS *q;
469c9e68d9Seric 	char *owner;
479c9e68d9Seric 	int otherowners;
489c9e68d9Seric 	register ENVELOPE *ee;
499c9e68d9Seric 	ENVELOPE *splitenv = NULL;
506103d9b4Seric 	bool announcequeueup;
519c9e68d9Seric 
527880f3e4Seric 	/*
537880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
547880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
557880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
567880f3e4Seric 	**  addresses to be sent.
577880f3e4Seric 	*/
587880f3e4Seric 
597880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
60896b16f6Seric 	{
61896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
62896b16f6Seric 		return;
63896b16f6Seric 	}
64896b16f6Seric 
659c9e68d9Seric 	/* determine actual delivery mode */
669c9e68d9Seric 	if (mode == SM_DEFAULT)
679c9e68d9Seric 	{
689c9e68d9Seric 		mode = e->e_sendmode;
699c9e68d9Seric 		if (mode != SM_VERIFY &&
709c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
719c9e68d9Seric 			mode = SM_QUEUE;
726103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
739c9e68d9Seric 	}
746103d9b4Seric 	else
756103d9b4Seric 		announcequeueup = FALSE;
769c9e68d9Seric 
779c9e68d9Seric 	if (tTd(13, 1))
789c9e68d9Seric 	{
790e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
800e484fe5Seric 			mode, e->e_id);
819c9e68d9Seric 		printaddr(&e->e_from, FALSE);
829c9e68d9Seric 		printf("sendqueue:\n");
839c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
849c9e68d9Seric 	}
859c9e68d9Seric 
869c9e68d9Seric 	/*
879c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
889c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
899c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
909c9e68d9Seric 	*/
919c9e68d9Seric 
929c9e68d9Seric 	CurEnv = e;
939c9e68d9Seric 
949c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
959c9e68d9Seric 	{
969c9e68d9Seric 		errno = 0;
974bb8b0fdSeric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
989c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
994bb8b0fdSeric 			RealHostName, e->e_sendqueue->q_paddr);
1009c9e68d9Seric 		return;
1019c9e68d9Seric 	}
1029c9e68d9Seric 
103b8004690Seric 	/*
104b8004690Seric 	**  Do sender deletion.
105b8004690Seric 	**
106b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
107b8004690Seric 	**	This can happen if the name server is hosed when you
108b8004690Seric 	**	are trying to send mail.  The result is that the sender
109b8004690Seric 	**	is instantiated in the queue as a recipient.
110b8004690Seric 	*/
111b8004690Seric 
112e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
113e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1149c9e68d9Seric 	{
1159c9e68d9Seric 		if (tTd(13, 5))
1169c9e68d9Seric 		{
1179c9e68d9Seric 			printf("sendall: QDONTSEND ");
1189c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1199c9e68d9Seric 		}
1209c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1219c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1229c9e68d9Seric 	}
1239c9e68d9Seric 
124ce5531bdSeric 	/*
125ce5531bdSeric 	**  Handle alias owners.
126ce5531bdSeric 	**
127ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
128ce5531bdSeric 	**	We discard owners that are the same as the return path.
129ce5531bdSeric 	*/
130ce5531bdSeric 
131ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
132ce5531bdSeric 	{
133ce5531bdSeric 		register struct address *a;
134ce5531bdSeric 
135ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
136ce5531bdSeric 			continue;
137ce5531bdSeric 		if (a != NULL)
138ce5531bdSeric 			q->q_owner = a->q_owner;
139ce5531bdSeric 
140ce5531bdSeric 		if (q->q_owner != NULL &&
141ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
142ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
143ce5531bdSeric 			q->q_owner = NULL;
144ce5531bdSeric 	}
145ce5531bdSeric 
146ce5531bdSeric 	owner = "";
147ce5531bdSeric 	otherowners = 1;
148ce5531bdSeric 	while (owner != NULL && otherowners > 0)
149ce5531bdSeric 	{
150ce5531bdSeric 		owner = NULL;
151ce5531bdSeric 		otherowners = 0;
152ce5531bdSeric 
153ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
154ce5531bdSeric 		{
155ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
156ce5531bdSeric 				continue;
157ce5531bdSeric 
158ce5531bdSeric 			if (q->q_owner != NULL)
159ce5531bdSeric 			{
160ce5531bdSeric 				if (owner == NULL)
161ce5531bdSeric 					owner = q->q_owner;
162ce5531bdSeric 				else if (owner != q->q_owner)
163ce5531bdSeric 				{
164ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
165ce5531bdSeric 					{
166ce5531bdSeric 						/* make future comparisons cheap */
167ce5531bdSeric 						q->q_owner = owner;
168ce5531bdSeric 					}
169ce5531bdSeric 					else
170ce5531bdSeric 					{
171ce5531bdSeric 						otherowners++;
172ce5531bdSeric 					}
173ce5531bdSeric 					owner = q->q_owner;
174ce5531bdSeric 				}
175ce5531bdSeric 			}
176ce5531bdSeric 			else
177ce5531bdSeric 			{
178ce5531bdSeric 				otherowners++;
179ce5531bdSeric 			}
180ce5531bdSeric 		}
181ce5531bdSeric 
182ce5531bdSeric 		if (owner != NULL && otherowners > 0)
183ce5531bdSeric 		{
184ce5531bdSeric 			extern HDR *copyheader();
185ce5531bdSeric 			extern ADDRESS *copyqueue();
186ce5531bdSeric 
187ce5531bdSeric 			/*
188ce5531bdSeric 			**  Split this envelope into two.
189ce5531bdSeric 			*/
190ce5531bdSeric 
191ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
192ce5531bdSeric 			*ee = *e;
193ce5531bdSeric 			ee->e_id = NULL;
194ce5531bdSeric 			(void) queuename(ee, '\0');
195ce5531bdSeric 
196ce5531bdSeric 			if (tTd(13, 1))
197ce5531bdSeric 				printf("sendall: split %s into %s\n",
198ce5531bdSeric 					e->e_id, ee->e_id);
199ce5531bdSeric 
200ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
201ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
202ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
203ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
204ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
205ce5531bdSeric 			if (tTd(13, 5))
206ce5531bdSeric 			{
207ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
208ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
209ce5531bdSeric 			}
210ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
211ce5531bdSeric 			ee->e_dfp = NULL;
212ce5531bdSeric 			ee->e_xfp = NULL;
213ce5531bdSeric 			ee->e_lockfp = NULL;
214ce5531bdSeric 			ee->e_df = NULL;
215ce5531bdSeric 			ee->e_errormode = EM_MAIL;
216ce5531bdSeric 			ee->e_sibling = splitenv;
217ce5531bdSeric 			splitenv = ee;
218ce5531bdSeric 
219ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
220ce5531bdSeric 				if (q->q_owner == owner)
221ce5531bdSeric 					q->q_flags |= QDONTSEND;
222ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
223ce5531bdSeric 				if (q->q_owner != owner)
224ce5531bdSeric 					q->q_flags |= QDONTSEND;
225ce5531bdSeric 
226ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
227ce5531bdSeric 			{
228ce5531bdSeric 				ee->e_dfp = NULL;
229da662164Seric 				ee->e_df = queuename(ee, 'd');
230da662164Seric 				ee->e_df = newstr(ee->e_df);
231ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
232ce5531bdSeric 				{
233ce5531bdSeric 					syserr("sendall: link(%s, %s)",
234ce5531bdSeric 						e->e_df, ee->e_df);
235ce5531bdSeric 				}
236ce5531bdSeric 			}
237ce5531bdSeric 
238ce5531bdSeric 			if (mode != SM_VERIFY)
239ce5531bdSeric 				openxscript(ee);
240ce5531bdSeric #ifdef LOG
241ce5531bdSeric 			if (LogLevel > 4)
242ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
243ce5531bdSeric 					ee->e_id, e->e_id);
244ce5531bdSeric #endif
245ce5531bdSeric 		}
246ce5531bdSeric 	}
247ce5531bdSeric 
248ce5531bdSeric 	if (owner != NULL)
249ce5531bdSeric 	{
250ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
251ce5531bdSeric 		if (tTd(13, 5))
252ce5531bdSeric 		{
253ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
254ce5531bdSeric 			printaddr(&e->e_from, FALSE);
255ce5531bdSeric 		}
256ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
257ce5531bdSeric 		e->e_errormode = EM_MAIL;
258ce5531bdSeric 	}
259ce5531bdSeric 
260c1ac89b1Seric # ifdef QUEUE
261c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
262c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
263c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
264c1ac89b1Seric 	{
265c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2666103d9b4Seric 		queueup(e, TRUE, announcequeueup);
267ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2686103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
269c1ac89b1Seric 	}
270c1ac89b1Seric #endif /* QUEUE */
271c1ac89b1Seric 
272ce5531bdSeric 	if (splitenv != NULL)
273ce5531bdSeric 	{
274ce5531bdSeric 		if (tTd(13, 1))
275ce5531bdSeric 		{
276ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
277ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
278ce5531bdSeric 		}
279ce5531bdSeric 
280ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
281ce5531bdSeric 		{
282ce5531bdSeric 			CurEnv = ee;
283ce5531bdSeric 			sendenvelope(ee, mode);
284ce5531bdSeric 		}
285ce5531bdSeric 
286ce5531bdSeric 		CurEnv = e;
287ce5531bdSeric 	}
288ce5531bdSeric 	sendenvelope(e, mode);
289ce5531bdSeric 
290ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
291ce5531bdSeric 		dropenvelope(splitenv);
292ce5531bdSeric }
293ce5531bdSeric 
294ce5531bdSeric sendenvelope(e, mode)
295ce5531bdSeric 	register ENVELOPE *e;
296ce5531bdSeric 	char mode;
297ce5531bdSeric {
298ce5531bdSeric 	bool oldverbose;
299ce5531bdSeric 	int pid;
300ce5531bdSeric 	register ADDRESS *q;
3016b2765c6Seric 	char *qf;
3026b2765c6Seric 	char *id;
303ce5531bdSeric 
3047880f3e4Seric 	/*
3057880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3067880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3077880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3087880f3e4Seric 	**  addresses to be sent.
3097880f3e4Seric 	*/
3107880f3e4Seric 
3117880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
3127880f3e4Seric 	{
3137880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3147880f3e4Seric 		return;
3157880f3e4Seric 	}
3167880f3e4Seric 
317ce5531bdSeric 	oldverbose = Verbose;
318c1ac89b1Seric 	switch (mode)
319c1ac89b1Seric 	{
320c1ac89b1Seric 	  case SM_VERIFY:
321c1ac89b1Seric 		Verbose = TRUE;
322c1ac89b1Seric 		break;
323c1ac89b1Seric 
324c1ac89b1Seric 	  case SM_QUEUE:
325c1ac89b1Seric   queueonly:
326c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
327c1ac89b1Seric 		return;
328c1ac89b1Seric 
329c1ac89b1Seric 	  case SM_FORK:
330c1ac89b1Seric 		if (e->e_xfp != NULL)
331c1ac89b1Seric 			(void) fflush(e->e_xfp);
332c1ac89b1Seric 
3332b9178d3Seric # ifndef HASFLOCK
334c1ac89b1Seric 		/*
3356b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3366b2765c6Seric 		**  the lock is owned by a process, not by an open file
3376b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3386b2765c6Seric 		**  then restart from scratch in the child.
339c1ac89b1Seric 		*/
340c1ac89b1Seric 
3416b2765c6Seric 		/* save id for future use */
3426b2765c6Seric 		id = e->e_id;
3436b2765c6Seric 
3446b2765c6Seric 		/* now drop the envelope in the parent */
3456b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
34644622ea3Seric 		e->e_flags &= ~EF_FATALERRS;
3476b2765c6Seric 		dropenvelope(e);
3486b2765c6Seric 
3496b2765c6Seric 		/* and reacquire in the child */
3506b2765c6Seric 		(void) dowork(id, TRUE, FALSE, e);
3516b2765c6Seric 
3526b2765c6Seric 		return;
3536b2765c6Seric 
3546b2765c6Seric # else /* HASFLOCK */
355c1ac89b1Seric 
356c1ac89b1Seric 		pid = fork();
357c1ac89b1Seric 		if (pid < 0)
358c1ac89b1Seric 		{
359c1ac89b1Seric 			goto queueonly;
360c1ac89b1Seric 		}
361c1ac89b1Seric 		else if (pid > 0)
362c1ac89b1Seric 		{
3630e484fe5Seric 			/* be sure we leave the temp files to our child */
3640e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3650e484fe5Seric 			if (e->e_lockfp != NULL)
3660e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3670e484fe5Seric 			e->e_lockfp = NULL;
3680e484fe5Seric 
3690e484fe5Seric 			/* close any random open files in the envelope */
3700e484fe5Seric 			closexscript(e);
3710e484fe5Seric 			if (e->e_dfp != NULL)
3720e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3730e484fe5Seric 			e->e_dfp = NULL;
3740e484fe5Seric 			e->e_id = e->e_df = NULL;
375c1ac89b1Seric 			return;
376c1ac89b1Seric 		}
377c1ac89b1Seric 
378c1ac89b1Seric 		/* double fork to avoid zombies */
379c1ac89b1Seric 		if (fork() > 0)
380c1ac89b1Seric 			exit(EX_OK);
381c1ac89b1Seric 
382c1ac89b1Seric 		/* be sure we are immune from the terminal */
3837880f3e4Seric 		disconnect(1, e);
384c1ac89b1Seric 
385c1ac89b1Seric 		/*
386c1ac89b1Seric 		**  Close any cached connections.
387c1ac89b1Seric 		**
388c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
389c1ac89b1Seric 		**	still knows about the connection.
390c1ac89b1Seric 		**
391c1ac89b1Seric 		**	This should only happen when delivering an error
392c1ac89b1Seric 		**	message.
393c1ac89b1Seric 		*/
394c1ac89b1Seric 
395c1ac89b1Seric 		mci_flush(FALSE, NULL);
396c1ac89b1Seric 
3976b2765c6Seric # endif /* HASFLOCK */
3986b2765c6Seric 
399c1ac89b1Seric 		break;
400c1ac89b1Seric 	}
401c1ac89b1Seric 
402c1ac89b1Seric 	/*
4039c9e68d9Seric 	**  Run through the list and send everything.
4045288e21aSeric 	**
4055288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4065288e21aSeric 	**	result in returned mail.
4079c9e68d9Seric 	*/
4089c9e68d9Seric 
4099c9e68d9Seric 	e->e_nsent = 0;
4105288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
4119c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4129c9e68d9Seric 	{
413fdc75a0fSeric #ifdef XDEBUG
414fdc75a0fSeric 		char wbuf[MAXNAME + 20];
415fdc75a0fSeric 
416fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
417fdc75a0fSeric 		checkfd012(wbuf);
418fdc75a0fSeric #endif
4199c9e68d9Seric 		if (mode == SM_VERIFY)
4209c9e68d9Seric 		{
4219c9e68d9Seric 			e->e_to = q->q_paddr;
4229c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4238dfca105Seric 			{
4248dfca105Seric 				message("deliverable: mailer %s, host %s, user %s",
4258dfca105Seric 					q->q_mailer->m_name,
4268dfca105Seric 					q->q_host,
4278dfca105Seric 					q->q_user);
4288dfca105Seric 			}
4299c9e68d9Seric 		}
4309c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4319c9e68d9Seric 		{
4329c9e68d9Seric # ifdef QUEUE
4339c9e68d9Seric 			/*
4349c9e68d9Seric 			**  Checkpoint the send list every few addresses
4359c9e68d9Seric 			*/
4369c9e68d9Seric 
4379c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4389c9e68d9Seric 			{
4399c9e68d9Seric 				queueup(e, TRUE, FALSE);
4409c9e68d9Seric 				e->e_nsent = 0;
4419c9e68d9Seric 			}
4429c9e68d9Seric # endif /* QUEUE */
4439c9e68d9Seric 			(void) deliver(e, q);
4449c9e68d9Seric 		}
4459c9e68d9Seric 	}
4469c9e68d9Seric 	Verbose = oldverbose;
4479c9e68d9Seric 
448fdc75a0fSeric #ifdef XDEBUG
449fdc75a0fSeric 	checkfd012("end of sendenvelope");
450fdc75a0fSeric #endif
451fdc75a0fSeric 
4529c9e68d9Seric 	if (mode == SM_FORK)
4539c9e68d9Seric 		finis();
4549c9e68d9Seric }
4559c9e68d9Seric /*
4569c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4579c9e68d9Seric **
4589c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4599c9e68d9Seric **	two processes on the same stack!!!
4609c9e68d9Seric **
4619c9e68d9Seric **	Parameters:
4629c9e68d9Seric **		none.
4639c9e68d9Seric **
4649c9e68d9Seric **	Returns:
4659c9e68d9Seric **		From a macro???  You've got to be kidding!
4669c9e68d9Seric **
4679c9e68d9Seric **	Side Effects:
4689c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4699c9e68d9Seric **			pid of child in parent, zero in child.
4709c9e68d9Seric **			-1 on unrecoverable error.
4719c9e68d9Seric **
4729c9e68d9Seric **	Notes:
4739c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4749c9e68d9Seric **		vfork for you.....
4759c9e68d9Seric */
4769c9e68d9Seric 
4779c9e68d9Seric # define NFORKTRIES	5
4789c9e68d9Seric 
4799c9e68d9Seric # ifndef FORK
4809c9e68d9Seric # define FORK	fork
4819c9e68d9Seric # endif
4829c9e68d9Seric 
4839c9e68d9Seric # define DOFORK(fORKfN) \
4849c9e68d9Seric {\
4859c9e68d9Seric 	register int i;\
4869c9e68d9Seric \
4879c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4889c9e68d9Seric 	{\
4899c9e68d9Seric 		pid = fORKfN();\
4909c9e68d9Seric 		if (pid >= 0)\
4919c9e68d9Seric 			break;\
4929c9e68d9Seric 		if (i > 0)\
4939c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
4949c9e68d9Seric 	}\
4959c9e68d9Seric }
4969c9e68d9Seric /*
4979c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
4989c9e68d9Seric **
4999c9e68d9Seric **	Parameters:
5009c9e68d9Seric **		none.
5019c9e68d9Seric **
5029c9e68d9Seric **	Returns:
5039c9e68d9Seric **		pid of child in parent.
5049c9e68d9Seric **		zero in child.
5059c9e68d9Seric **		-1 on error.
5069c9e68d9Seric **
5079c9e68d9Seric **	Side Effects:
5089c9e68d9Seric **		returns twice, once in parent and once in child.
5099c9e68d9Seric */
5109c9e68d9Seric 
5119c9e68d9Seric dofork()
5129c9e68d9Seric {
5139c9e68d9Seric 	register int pid;
5149c9e68d9Seric 
5159c9e68d9Seric 	DOFORK(fork);
5169c9e68d9Seric 	return (pid);
5179c9e68d9Seric }
5189c9e68d9Seric /*
51913bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
52013bbc08cSeric **
52113bbc08cSeric **	This routine delivers to everyone on the same host as the
52213bbc08cSeric **	user on the head of the list.  It is clever about mailers
52313bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
52413bbc08cSeric **	that it will deliver to all these addresses however -- so
52513bbc08cSeric **	deliver should be called once for each address on the
52613bbc08cSeric **	list.
52725a99e2eSeric **
52825a99e2eSeric **	Parameters:
529588cad61Seric **		e -- the envelope to deliver.
530c77d1c25Seric **		firstto -- head of the address list to deliver to.
53125a99e2eSeric **
53225a99e2eSeric **	Returns:
53325a99e2eSeric **		zero -- successfully delivered.
53425a99e2eSeric **		else -- some failure, see ExitStat for more info.
53525a99e2eSeric **
53625a99e2eSeric **	Side Effects:
53725a99e2eSeric **		The standard input is passed off to someone.
53825a99e2eSeric */
53925a99e2eSeric 
540588cad61Seric deliver(e, firstto)
541588cad61Seric 	register ENVELOPE *e;
542c77d1c25Seric 	ADDRESS *firstto;
54325a99e2eSeric {
54478442df3Seric 	char *host;			/* host being sent to */
54578442df3Seric 	char *user;			/* user being sent to */
54625a99e2eSeric 	char **pvp;
5475dfc646bSeric 	register char **mvp;
54825a99e2eSeric 	register char *p;
549588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5506259796dSeric 	ADDRESS *ctladdr;
551b31e7f2bSeric 	register MCI *mci;
552c77d1c25Seric 	register ADDRESS *to = firstto;
553c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
554772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
555911693bfSbostic 	int rcode;			/* response code */
556e103b48fSeric 	char *firstsig;			/* signature of firstto */
5579c9e68d9Seric 	int pid;
5589c9e68d9Seric 	char *curhost;
5599c9e68d9Seric 	int mpvect[2];
5609c9e68d9Seric 	int rpvect[2];
561ee6bf8dfSeric 	char *pv[MAXPV+1];
562579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
563ee6bf8dfSeric 	char buf[MAXNAME];
564c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
565fabb3bd4Seric 	extern int checkcompat();
5669c9e68d9Seric 	extern FILE *fdopen();
56725a99e2eSeric 
56835490626Seric 	errno = 0;
569ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5705dfc646bSeric 		return (0);
57125a99e2eSeric 
572134746fbSeric #ifdef NAMED_BIND
573912a731aSbostic 	/* unless interactive, try twice, over a minute */
574912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
575912a731aSbostic 		_res.retrans = 30;
576912a731aSbostic 		_res.retry = 2;
577912a731aSbostic 	}
578d4bd8f0eSbostic #endif
579912a731aSbostic 
58051552439Seric 	m = to->q_mailer;
58151552439Seric 	host = to->q_host;
582c9be6216Seric 	CurEnv = e;			/* just in case */
5834384d521Seric 	e->e_statmsg = NULL;
58451552439Seric 
5856ef48975Seric 	if (tTd(10, 1))
5865dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
58751552439Seric 			m->m_mno, host, to->q_user);
588f3dbc832Seric 
589f3dbc832Seric 	/*
590f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
591f3dbc832Seric 	**  connections now, just mark these addresses and return.
592f3dbc832Seric 	**	This is useful if we want to batch connections to
593f3dbc832Seric 	**	reduce load.  This will cause the messages to be
594f3dbc832Seric 	**	queued up, and a daemon will come along to send the
595f3dbc832Seric 	**	messages later.
596f3dbc832Seric 	**		This should be on a per-mailer basis.
597f3dbc832Seric 	*/
598f3dbc832Seric 
59979aa5155Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
600f3dbc832Seric 	{
601f3dbc832Seric 		for (; to != NULL; to = to->q_next)
602f4560e80Seric 		{
603ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
604c6e18ac5Seric 			    to->q_mailer != m)
605f4560e80Seric 				continue;
606268a25e1Seric 			to->q_flags |= QQUEUEUP;
607588cad61Seric 			e->e_to = to->q_paddr;
60808b25121Seric 			message("queued");
6092f624c86Seric 			if (LogLevel > 8)
61081161401Seric 				logdelivery(m, NULL, "queued", e);
611f4560e80Seric 		}
612588cad61Seric 		e->e_to = NULL;
613f3dbc832Seric 		return (0);
614f3dbc832Seric 	}
615f3dbc832Seric 
61625a99e2eSeric 	/*
6175dfc646bSeric 	**  Do initial argv setup.
6185dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6195dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6205dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6215dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6225dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6233bea8136Seric 	**		The from address rewrite is expected to make
6243bea8136Seric 	**		the address relative to the other end.
6255dfc646bSeric 	*/
6265dfc646bSeric 
62778442df3Seric 	/* rewrite from address, using rewriting rules */
628efe54562Seric 	rcode = EX_OK;
629efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
630efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
631efe54562Seric 					   &rcode, e));
632ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
633588cad61Seric 	define('h', host, e);			/* to host */
6345dfc646bSeric 	Errors = 0;
6355dfc646bSeric 	pvp = pv;
6365dfc646bSeric 	*pvp++ = m->m_argv[0];
6375dfc646bSeric 
6385dfc646bSeric 	/* insert -f or -r flag as appropriate */
63957fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6405dfc646bSeric 	{
64157fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6425dfc646bSeric 			*pvp++ = "-f";
6435dfc646bSeric 		else
6445dfc646bSeric 			*pvp++ = "-r";
645c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6465dfc646bSeric 	}
6475dfc646bSeric 
6485dfc646bSeric 	/*
6495dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6505dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6515dfc646bSeric 	**  be one of these, and there are only a few more slots
6525dfc646bSeric 	**  in the pv after it.
6535dfc646bSeric 	*/
6545dfc646bSeric 
6555dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6565dfc646bSeric 	{
6572bc47524Seric 		/* can't use strchr here because of sign extension problems */
6582bc47524Seric 		while (*p != '\0')
6592bc47524Seric 		{
6602bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6612bc47524Seric 			{
6622bc47524Seric 				if (*p == 'u')
6635dfc646bSeric 					break;
6642bc47524Seric 			}
6652bc47524Seric 		}
6662bc47524Seric 
6672bc47524Seric 		if (*p != '\0')
6685dfc646bSeric 			break;
6695dfc646bSeric 
6705dfc646bSeric 		/* this entry is safe -- go ahead and process it */
671588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6725dfc646bSeric 		*pvp++ = newstr(buf);
6735dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6745dfc646bSeric 		{
67508b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6765dfc646bSeric 			return (-1);
6775dfc646bSeric 		}
6785dfc646bSeric 	}
679c579ef51Seric 
68033db8731Seric 	/*
68133db8731Seric 	**  If we have no substitution for the user name in the argument
68233db8731Seric 	**  list, we know that we must supply the names otherwise -- and
68333db8731Seric 	**  SMTP is the answer!!
68433db8731Seric 	*/
68533db8731Seric 
6865dfc646bSeric 	if (*mvp == NULL)
687c579ef51Seric 	{
688c579ef51Seric 		/* running SMTP */
6892c7e1b8dSeric # ifdef SMTP
690c579ef51Seric 		clever = TRUE;
691c579ef51Seric 		*pvp = NULL;
6926c2c3107Seric # else /* SMTP */
69333db8731Seric 		/* oops!  we don't implement SMTP */
69408b25121Seric 		syserr("554 SMTP style mailer");
6952c7e1b8dSeric 		return (EX_SOFTWARE);
6966c2c3107Seric # endif /* SMTP */
697c579ef51Seric 	}
6985dfc646bSeric 
6995dfc646bSeric 	/*
7005dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7015dfc646bSeric 	**  run through our address list and append all the addresses
7025dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7035dfc646bSeric 	**  always send another copy later.
7045dfc646bSeric 	*/
7055dfc646bSeric 
7065dfc646bSeric 	tobuf[0] = '\0';
707588cad61Seric 	e->e_to = tobuf;
7086259796dSeric 	ctladdr = NULL;
709e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7105dfc646bSeric 	for (; to != NULL; to = to->q_next)
7115dfc646bSeric 	{
7125dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
71357fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7145dfc646bSeric 			break;
7155dfc646bSeric 
7165dfc646bSeric 		/* if already sent or not for this host, don't send */
717ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
718e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
719e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7205dfc646bSeric 			continue;
7216259796dSeric 
7224b22ea87Seric 		/* avoid overflowing tobuf */
723aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7244b22ea87Seric 			break;
7254b22ea87Seric 
7266ef48975Seric 		if (tTd(10, 1))
727772e6e50Seric 		{
728772e6e50Seric 			printf("\nsend to ");
729772e6e50Seric 			printaddr(to, FALSE);
730772e6e50Seric 		}
731772e6e50Seric 
7326259796dSeric 		/* compute effective uid/gid when sending */
7337da1035fSeric 		if (to->q_mailer == ProgMailer)
7346259796dSeric 			ctladdr = getctladdr(to);
7356259796dSeric 
7365dfc646bSeric 		user = to->q_user;
737588cad61Seric 		e->e_to = to->q_paddr;
73875f1ade9Seric 		if (tTd(10, 5))
73975f1ade9Seric 		{
74075f1ade9Seric 			printf("deliver: QDONTSEND ");
74175f1ade9Seric 			printaddr(to, FALSE);
74275f1ade9Seric 		}
743ee4b0922Seric 		to->q_flags |= QDONTSEND;
7445dfc646bSeric 
7455dfc646bSeric 		/*
7465dfc646bSeric 		**  Check to see that these people are allowed to
7475dfc646bSeric 		**  talk to each other.
7482a6e0786Seric 		*/
7492a6e0786Seric 
75069582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
75169582d2fSeric 		{
75269582d2fSeric 			NoReturn = TRUE;
75308b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
75481161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
75569582d2fSeric 			continue;
75669582d2fSeric 		}
757fabb3bd4Seric 		rcode = checkcompat(to, e);
7581793c9c5Seric 		if (rcode != EX_OK)
7595dfc646bSeric 		{
7601000c37aSeric 			markfailure(e, to, rcode);
76181161401Seric 			giveresponse(rcode, m, NULL, e);
7625dfc646bSeric 			continue;
7635dfc646bSeric 		}
7642a6e0786Seric 
7652a6e0786Seric 		/*
7669ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7679ec9501bSeric 		**	about them.
76825a99e2eSeric 		*/
76925a99e2eSeric 
77057fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
77125a99e2eSeric 		{
7721d8f1806Seric 			stripquotes(user);
7731d8f1806Seric 			stripquotes(host);
77425a99e2eSeric 		}
77525a99e2eSeric 
776cdb828c5Seric 		/* hack attack -- delivermail compatibility */
777cdb828c5Seric 		if (m == ProgMailer && *user == '|')
778cdb828c5Seric 			user++;
779cdb828c5Seric 
78025a99e2eSeric 		/*
7813efaed6eSeric 		**  If an error message has already been given, don't
7823efaed6eSeric 		**	bother to send to this address.
7833efaed6eSeric 		**
7843efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7853efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
7863efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
7873efaed6eSeric 		*/
7883efaed6eSeric 
7896cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7903efaed6eSeric 			continue;
7913efaed6eSeric 
792f2fec898Seric 		/* save statistics.... */
793588cad61Seric 		markstats(e, to);
794f2fec898Seric 
7953efaed6eSeric 		/*
79625a99e2eSeric 		**  See if this user name is "special".
79725a99e2eSeric 		**	If the user name has a slash in it, assume that this
79851552439Seric 		**	is a file -- send it off without further ado.  Note
79951552439Seric 		**	that this type of addresses is not processed along
80051552439Seric 		**	with the others, so we fudge on the To person.
80125a99e2eSeric 		*/
80225a99e2eSeric 
8032c017f8dSeric 		if (m == FileMailer)
80425a99e2eSeric 		{
805b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
80681161401Seric 			giveresponse(rcode, m, NULL, e);
807dde5acadSeric 			if (rcode == EX_OK)
808dde5acadSeric 				to->q_flags |= QSENT;
8095dfc646bSeric 			continue;
81025a99e2eSeric 		}
81125a99e2eSeric 
81213bbc08cSeric 		/*
81313bbc08cSeric 		**  Address is verified -- add this user to mailer
81413bbc08cSeric 		**  argv, and add it to the print list of recipients.
81513bbc08cSeric 		*/
81613bbc08cSeric 
817508daeccSeric 		/* link together the chain of recipients */
818508daeccSeric 		to->q_tchain = tochain;
819508daeccSeric 		tochain = to;
820508daeccSeric 
8215dfc646bSeric 		/* create list of users for error messages */
822db8841e9Seric 		(void) strcat(tobuf, ",");
823db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
824588cad61Seric 		define('u', user, e);		/* to user */
825*b6f89e6eSeric 		p = to->q_home;
826*b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
827*b6f89e6eSeric 			p = ctladdr->q_home;
828*b6f89e6eSeric 		define('z', p, e);	/* user's home */
8295dfc646bSeric 
830c579ef51Seric 		/*
831508daeccSeric 		**  Expand out this user into argument list.
832c579ef51Seric 		*/
833c579ef51Seric 
834508daeccSeric 		if (!clever)
835c579ef51Seric 		{
836588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8375dfc646bSeric 			*pvp++ = newstr(buf);
8385dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8395dfc646bSeric 			{
8405dfc646bSeric 				/* allow some space for trailing parms */
8415dfc646bSeric 				break;
8425dfc646bSeric 			}
8435dfc646bSeric 		}
844c579ef51Seric 	}
8455dfc646bSeric 
846145b49b1Seric 	/* see if any addresses still exist */
847145b49b1Seric 	if (tobuf[0] == '\0')
848c579ef51Seric 	{
849588cad61Seric 		define('g', (char *) NULL, e);
850145b49b1Seric 		return (0);
851c579ef51Seric 	}
852145b49b1Seric 
8535dfc646bSeric 	/* print out messages as full list */
85463780dbdSeric 	e->e_to = tobuf + 1;
8555dfc646bSeric 
8565dfc646bSeric 	/*
8575dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8585dfc646bSeric 	*/
8595dfc646bSeric 
860c579ef51Seric 	while (!clever && *++mvp != NULL)
8615dfc646bSeric 	{
862588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8635dfc646bSeric 		*pvp++ = newstr(buf);
8645dfc646bSeric 		if (pvp >= &pv[MAXPV])
86508b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8665dfc646bSeric 	}
8675dfc646bSeric 	*pvp++ = NULL;
8685dfc646bSeric 
86925a99e2eSeric 	/*
87025a99e2eSeric 	**  Call the mailer.
8716328bdf7Seric 	**	The argument vector gets built, pipes
87225a99e2eSeric 	**	are created as necessary, and we fork & exec as
8736328bdf7Seric 	**	appropriate.
874c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
87525a99e2eSeric 	*/
87625a99e2eSeric 
8777ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
87886b26461Seric 		ctladdr = &e->e_from;
879134746fbSeric #ifdef NAMED_BIND
8802bcc6d2dSeric 	if (ConfigLevel < 2)
881912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
882134746fbSeric #endif
8839c9e68d9Seric 
8849c9e68d9Seric 	if (tTd(11, 1))
885134746fbSeric 	{
8869c9e68d9Seric 		printf("openmailer:");
8879c9e68d9Seric 		printav(pv);
8889c9e68d9Seric 	}
8899c9e68d9Seric 	errno = 0;
8909c9e68d9Seric 
8919c9e68d9Seric 	CurHostName = m->m_mailer;
8929c9e68d9Seric 
8939c9e68d9Seric 	/*
8949c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
8959c9e68d9Seric 	**  connection.
8969c9e68d9Seric 	**	In this case we don't actually fork.  We must be
8979c9e68d9Seric 	**	running SMTP for this to work.  We will return a
8989c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
8999c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9009c9e68d9Seric 	*/
9019c9e68d9Seric 
9029c9e68d9Seric 	curhost = NULL;
903c931b82bSeric 	SmtpPhase = NULL;
9049c9e68d9Seric 
9057febfd66Seric #ifdef XDEBUG
9067febfd66Seric 	{
9077febfd66Seric 		char wbuf[MAXLINE];
9087febfd66Seric 
9097febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9107febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
9117febfd66Seric 		checkfd012(wbuf);
9127febfd66Seric 	}
9137febfd66Seric #endif
9147febfd66Seric 
9157febfd66Seric 
9169c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9179c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9189c9e68d9Seric 	{
9199c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9209c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9219c9e68d9Seric 		mci->mci_in = stdin;
9229c9e68d9Seric 		mci->mci_out = stdout;
9239c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9249c9e68d9Seric 		mci->mci_mailer = m;
9259c9e68d9Seric 	}
9269c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9279c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9289c9e68d9Seric 	{
9299c9e68d9Seric #ifdef DAEMON
9309c9e68d9Seric 		register int i;
9319c9e68d9Seric 		register u_short port;
9329c9e68d9Seric 
9339c9e68d9Seric 		CurHostName = pv[1];
9349c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9359c9e68d9Seric 
9369c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9379c9e68d9Seric 		{
9389c9e68d9Seric 			syserr("null signature");
939845e533cSeric 			rcode = EX_OSERR;
940b31e7f2bSeric 			goto give_up;
941b31e7f2bSeric 		}
9429c9e68d9Seric 
9439c9e68d9Seric 		if (!clever)
9449c9e68d9Seric 		{
9459c9e68d9Seric 			syserr("554 non-clever IPC");
9469c9e68d9Seric 			rcode = EX_OSERR;
9479c9e68d9Seric 			goto give_up;
9489c9e68d9Seric 		}
9499c9e68d9Seric 		if (pv[2] != NULL)
9509c9e68d9Seric 			port = atoi(pv[2]);
9519c9e68d9Seric 		else
9529c9e68d9Seric 			port = 0;
9539c9e68d9Seric tryhost:
9549c9e68d9Seric 		mci = NULL;
9559c9e68d9Seric 		while (*curhost != '\0')
9569c9e68d9Seric 		{
9579c9e68d9Seric 			register char *p;
9589c9e68d9Seric 			static char hostbuf[MAXNAME];
9599c9e68d9Seric 
9609c9e68d9Seric 			mci = NULL;
9619c9e68d9Seric 
9629c9e68d9Seric 			/* pull the next host from the signature */
9639c9e68d9Seric 			p = strchr(curhost, ':');
9649c9e68d9Seric 			if (p == NULL)
9659c9e68d9Seric 				p = &curhost[strlen(curhost)];
9669c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9679c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9689c9e68d9Seric 			if (*p != '\0')
9699c9e68d9Seric 				p++;
9709c9e68d9Seric 			curhost = p;
9719c9e68d9Seric 
9729c9e68d9Seric 			/* see if we already know that this host is fried */
9739c9e68d9Seric 			CurHostName = hostbuf;
9749c9e68d9Seric 			mci = mci_get(hostbuf, m);
9759c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9769c9e68d9Seric 			{
9779c9e68d9Seric 				if (tTd(11, 1))
9789c9e68d9Seric 				{
9799c9e68d9Seric 					printf("openmailer: ");
9809c9e68d9Seric 					mci_dump(mci);
9819c9e68d9Seric 				}
9829c9e68d9Seric 				CurHostName = mci->mci_host;
9839c9e68d9Seric 				break;
9849c9e68d9Seric 			}
9859c9e68d9Seric 			mci->mci_mailer = m;
9869c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9879c9e68d9Seric 				continue;
9889c9e68d9Seric 
9899c9e68d9Seric 			/* try the connection */
9909c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9919c9e68d9Seric 			message("Connecting to %s (%s)...",
9929c9e68d9Seric 				hostbuf, m->m_name);
9939c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
9949c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
9959c9e68d9Seric 			mci->mci_exitstat = i;
9969c9e68d9Seric 			mci->mci_errno = errno;
997f170942cSeric #ifdef NAMED_BIND
998f170942cSeric 			mci->mci_herrno = h_errno;
999f170942cSeric #endif
10009c9e68d9Seric 			if (i == EX_OK)
10019c9e68d9Seric 			{
10029c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10039c9e68d9Seric 				mci_cache(mci);
1004f170942cSeric 				if (TrafficLogFile != NULL)
1005f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1006f170942cSeric 						getpid(), hostbuf);
10079c9e68d9Seric 				break;
10089c9e68d9Seric 			}
10099c9e68d9Seric 			else if (tTd(11, 1))
10109c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10119c9e68d9Seric 					i, errno);
10129c9e68d9Seric 
10139c9e68d9Seric 
10149c9e68d9Seric 			/* enter status of this host */
10159c9e68d9Seric 			setstat(i);
10169c9e68d9Seric 		}
10179c9e68d9Seric 		mci->mci_pid = 0;
10189c9e68d9Seric #else /* no DAEMON */
10199c9e68d9Seric 		syserr("554 openmailer: no IPC");
10209c9e68d9Seric 		if (tTd(11, 1))
10219c9e68d9Seric 			printf("openmailer: NULL\n");
10229c9e68d9Seric 		return NULL;
10239c9e68d9Seric #endif /* DAEMON */
10249c9e68d9Seric 	}
10259c9e68d9Seric 	else
10269c9e68d9Seric 	{
1027f170942cSeric 		if (TrafficLogFile != NULL)
10280e3bfef5Seric 		{
1029f170942cSeric 			char **av;
1030f170942cSeric 
1031f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1032f170942cSeric 			for (av = pv; *av != NULL; av++)
1033f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1034f170942cSeric 			fprintf(TrafficLogFile, "\n");
10356fe8c3bcSeric 		}
10366fe8c3bcSeric 
10379c9e68d9Seric 		/* create a pipe to shove the mail through */
10389c9e68d9Seric 		if (pipe(mpvect) < 0)
10399c9e68d9Seric 		{
10400e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10410e3bfef5Seric 				e->e_to, m->m_name);
10429c9e68d9Seric 			if (tTd(11, 1))
10439c9e68d9Seric 				printf("openmailer: NULL\n");
10449c9e68d9Seric 			rcode = EX_OSERR;
10459c9e68d9Seric 			goto give_up;
10469c9e68d9Seric 		}
10479c9e68d9Seric 
10489c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10499c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10509c9e68d9Seric 		{
10510e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10520e3bfef5Seric 				e->e_to, m->m_name);
10539c9e68d9Seric 			(void) close(mpvect[0]);
10549c9e68d9Seric 			(void) close(mpvect[1]);
10559c9e68d9Seric 			if (tTd(11, 1))
10569c9e68d9Seric 				printf("openmailer: NULL\n");
10579c9e68d9Seric 			rcode = EX_OSERR;
10589c9e68d9Seric 			goto give_up;
10599c9e68d9Seric 		}
10609c9e68d9Seric 
10619c9e68d9Seric 		/*
10629c9e68d9Seric 		**  Actually fork the mailer process.
10639c9e68d9Seric 		**	DOFORK is clever about retrying.
10649c9e68d9Seric 		**
10659c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10669c9e68d9Seric 		**	around so that endmail will get it.
10679c9e68d9Seric 		*/
10689c9e68d9Seric 
10699c9e68d9Seric 		if (e->e_xfp != NULL)
10709c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10719c9e68d9Seric 		(void) fflush(stdout);
10729c9e68d9Seric # ifdef SIGCHLD
10732b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
10749c9e68d9Seric # endif /* SIGCHLD */
10759c9e68d9Seric 		DOFORK(FORK);
10769c9e68d9Seric 		/* pid is set by DOFORK */
10779c9e68d9Seric 		if (pid < 0)
10789c9e68d9Seric 		{
10799c9e68d9Seric 			/* failure */
10800e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
10810e3bfef5Seric 				e->e_to, m->m_name);
10829c9e68d9Seric 			(void) close(mpvect[0]);
10839c9e68d9Seric 			(void) close(mpvect[1]);
10849c9e68d9Seric 			if (clever)
10859c9e68d9Seric 			{
10869c9e68d9Seric 				(void) close(rpvect[0]);
10879c9e68d9Seric 				(void) close(rpvect[1]);
10889c9e68d9Seric 			}
10899c9e68d9Seric 			if (tTd(11, 1))
10909c9e68d9Seric 				printf("openmailer: NULL\n");
10919c9e68d9Seric 			rcode = EX_OSERR;
10929c9e68d9Seric 			goto give_up;
10939c9e68d9Seric 		}
10949c9e68d9Seric 		else if (pid == 0)
10959c9e68d9Seric 		{
10969c9e68d9Seric 			int i;
10979c9e68d9Seric 			int saveerrno;
10989c9e68d9Seric 			char **ep;
10999c9e68d9Seric 			char *env[MAXUSERENVIRON];
11009c9e68d9Seric 			extern char **environ;
11019c9e68d9Seric 			extern int DtableSize;
11029c9e68d9Seric 
11039c9e68d9Seric 			/* child -- set up input & exec mailer */
11042b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
11052b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
11062b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11079c9e68d9Seric 
11089c9e68d9Seric 			/* close any other cached connections */
11099c9e68d9Seric 			mci_flush(FALSE, mci);
11109c9e68d9Seric 
111144f2317fSeric 			/* reset user and group */
111244f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
111344f2317fSeric 			{
111444f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
111544f2317fSeric 				{
111644f2317fSeric 					(void) initgroups(DefUser, DefGid);
111744f2317fSeric 					(void) setuid(DefUid);
111844f2317fSeric 				}
111944f2317fSeric 				else
112044f2317fSeric 				{
112144f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
112244f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
112344f2317fSeric 						ctladdr->q_gid);
112444f2317fSeric 					(void) setuid(ctladdr->q_uid);
112544f2317fSeric 				}
112644f2317fSeric 			}
112744f2317fSeric 
112844f2317fSeric 			if (tTd(11, 2))
112944f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
113044f2317fSeric 					getuid(), geteuid());
113144f2317fSeric 
1132b986f6aaSeric 			/* move into some "safe" directory */
1133b986f6aaSeric 			if (m->m_execdir != NULL)
1134b986f6aaSeric 			{
1135b986f6aaSeric 				char *p, *q;
1136b986f6aaSeric 				char buf[MAXLINE];
1137b986f6aaSeric 
1138b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1139b986f6aaSeric 				{
1140b986f6aaSeric 					q = strchr(p, ':');
1141b986f6aaSeric 					if (q != NULL)
1142b986f6aaSeric 						*q = '\0';
1143b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1144b986f6aaSeric 					if (q != NULL)
1145b986f6aaSeric 						*q++ = ':';
1146b986f6aaSeric 					if (tTd(11, 20))
1147b986f6aaSeric 						printf("openmailer: trydir %s\n",
1148b986f6aaSeric 							buf);
1149b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1150b986f6aaSeric 						break;
1151b986f6aaSeric 				}
1152b986f6aaSeric 			}
1153b986f6aaSeric 
11549c9e68d9Seric 			/* arrange to filter std & diag output of command */
11559c9e68d9Seric 			if (clever)
11569c9e68d9Seric 			{
11579c9e68d9Seric 				(void) close(rpvect[0]);
11586fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11596fe8c3bcSeric 				{
11600e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11610e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11626fe8c3bcSeric 					_exit(EX_OSERR);
11636fe8c3bcSeric 				}
11649c9e68d9Seric 				(void) close(rpvect[1]);
11659c9e68d9Seric 			}
11669c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11679c9e68d9Seric 			{
11689c9e68d9Seric 				/* put mailer output in transcript */
11696fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11706fe8c3bcSeric 				{
11710e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11720e3bfef5Seric 						e->e_to, m->m_name,
11736fe8c3bcSeric 						fileno(e->e_xfp));
11746fe8c3bcSeric 					_exit(EX_OSERR);
11759c9e68d9Seric 				}
11766fe8c3bcSeric 			}
11776fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11786fe8c3bcSeric 			{
11790e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
11800e3bfef5Seric 					e->e_to, m->m_name);
11816fe8c3bcSeric 				_exit(EX_OSERR);
11826fe8c3bcSeric 			}
11839c9e68d9Seric 
11849c9e68d9Seric 			/* arrange to get standard input */
11859c9e68d9Seric 			(void) close(mpvect[1]);
11869c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11879c9e68d9Seric 			{
11880e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
11890e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
11909c9e68d9Seric 				_exit(EX_OSERR);
11919c9e68d9Seric 			}
11929c9e68d9Seric 			(void) close(mpvect[0]);
11939c9e68d9Seric 
11949c9e68d9Seric 			/* arrange for all the files to be closed */
11959c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
11969c9e68d9Seric 			{
11979c9e68d9Seric 				register int j;
119844f2317fSeric 
11999c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12009c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
12019c9e68d9Seric 			}
12029c9e68d9Seric 
12039c9e68d9Seric 			/* set up the mailer environment */
12049c9e68d9Seric 			i = 0;
12059c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12069c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12079c9e68d9Seric 			{
12089c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12099c9e68d9Seric 					env[i++] = *ep;
12109c9e68d9Seric 			}
12119c9e68d9Seric 			env[i++] = NULL;
12129c9e68d9Seric 
12139c9e68d9Seric 			/* try to execute the mailer */
12149c9e68d9Seric 			execve(m->m_mailer, pv, env);
12159c9e68d9Seric 			saveerrno = errno;
12169c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12177c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12187c941fd2Seric 				_exit(EX_OSERR);
12199c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12209c9e68d9Seric 		}
12219c9e68d9Seric 
12229c9e68d9Seric 		/*
12239c9e68d9Seric 		**  Set up return value.
12249c9e68d9Seric 		*/
12259c9e68d9Seric 
12269c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12279c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12289c9e68d9Seric 		mci->mci_mailer = m;
12299c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12309c9e68d9Seric 		mci->mci_pid = pid;
12319c9e68d9Seric 		(void) close(mpvect[0]);
12329c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
12339c9e68d9Seric 		if (clever)
12349c9e68d9Seric 		{
12359c9e68d9Seric 			(void) close(rpvect[1]);
12369c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
12379c9e68d9Seric 		}
12389c9e68d9Seric 		else
12399c9e68d9Seric 		{
12409c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12419c9e68d9Seric 			mci->mci_in = NULL;
12429c9e68d9Seric 		}
12439c9e68d9Seric 	}
12449c9e68d9Seric 
12459c9e68d9Seric 	/*
12469c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12479c9e68d9Seric 	*/
12489c9e68d9Seric 
12499c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12509c9e68d9Seric 	{
12519c9e68d9Seric 		smtpinit(m, mci, e);
12529c9e68d9Seric 	}
12539c9e68d9Seric 	if (tTd(11, 1))
12549c9e68d9Seric 	{
12559c9e68d9Seric 		printf("openmailer: ");
12569c9e68d9Seric 		mci_dump(mci);
12579c9e68d9Seric 	}
12589c9e68d9Seric 
12599c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1260b31e7f2bSeric 	{
1261b31e7f2bSeric 		/* couldn't open the mailer */
1262b31e7f2bSeric 		rcode = mci->mci_exitstat;
12632a6bc25bSeric 		errno = mci->mci_errno;
1264f170942cSeric #ifdef NAMED_BIND
1265f170942cSeric 		h_errno = mci->mci_herrno;
1266f170942cSeric #endif
1267b31e7f2bSeric 		if (rcode == EX_OK)
1268b31e7f2bSeric 		{
1269b31e7f2bSeric 			/* shouldn't happen */
127008b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12716b0f339dSeric 				rcode, mci->mci_state, firstsig);
1272b31e7f2bSeric 			rcode = EX_SOFTWARE;
1273b31e7f2bSeric 		}
1274e9277e33Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
127590891494Seric 		{
127690891494Seric 			/* try next MX site */
127790891494Seric 			goto tryhost;
127890891494Seric 		}
1279b31e7f2bSeric 	}
1280b31e7f2bSeric 	else if (!clever)
1281b31e7f2bSeric 	{
1282b31e7f2bSeric 		/*
1283b31e7f2bSeric 		**  Format and send message.
1284b31e7f2bSeric 		*/
128515d084d5Seric 
1286b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1287b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1288b31e7f2bSeric 		putline("\n", mci->mci_out, m);
128903c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1290b31e7f2bSeric 
1291b31e7f2bSeric 		/* get the exit status */
1292c9be6216Seric 		rcode = endmailer(mci, e, pv);
1293134746fbSeric 	}
1294134746fbSeric 	else
1295b31e7f2bSeric #ifdef SMTP
1296134746fbSeric 	{
1297b31e7f2bSeric 		/*
1298b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1299b31e7f2bSeric 		*/
130015d084d5Seric 
1301b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1302b31e7f2bSeric 		if (rcode == EX_OK)
130375889e88Seric 		{
1304ded0d3daSkarels 			register char *t = tobuf;
1305ded0d3daSkarels 			register int i;
1306ded0d3daSkarels 
1307588cad61Seric 			/* send the recipient list */
130863780dbdSeric 			tobuf[0] = '\0';
130975889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
131075889e88Seric 			{
131163780dbdSeric 				e->e_to = to->q_paddr;
131215d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
131375889e88Seric 				{
131483b7ddc9Seric 					markfailure(e, to, i);
131581161401Seric 					giveresponse(i, m, mci, e);
131663780dbdSeric 				}
131775889e88Seric 				else
131875889e88Seric 				{
1319911693bfSbostic 					*t++ = ',';
1320b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1321b31e7f2bSeric 						continue;
1322ee39df61Seric 					*t = '\0';
1323588cad61Seric 				}
1324588cad61Seric 			}
1325588cad61Seric 
132663780dbdSeric 			/* now send the data */
132763780dbdSeric 			if (tobuf[0] == '\0')
1328b31e7f2bSeric 			{
13299c9e68d9Seric 				rcode = EX_OK;
133063780dbdSeric 				e->e_to = NULL;
1331b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1332b31e7f2bSeric 					smtprset(m, mci, e);
1333b31e7f2bSeric 			}
133475889e88Seric 			else
133575889e88Seric 			{
133663780dbdSeric 				e->e_to = tobuf + 1;
133775889e88Seric 				rcode = smtpdata(m, mci, e);
133863780dbdSeric 			}
133963780dbdSeric 
134063780dbdSeric 			/* now close the connection */
1341b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
134215d084d5Seric 				smtpquit(m, mci, e);
134363780dbdSeric 		}
13449c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13459c9e68d9Seric 		{
13469c9e68d9Seric 			/* try next MX site */
13479c9e68d9Seric 			goto tryhost;
13489c9e68d9Seric 		}
1349c579ef51Seric 	}
1350b31e7f2bSeric #else /* not SMTP */
1351a05b3449Sbostic 	{
135208b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1353845e533cSeric 		rcode = EX_CONFIG;
1354b31e7f2bSeric 		goto give_up;
1355a05b3449Sbostic 	}
1356b31e7f2bSeric #endif /* SMTP */
1357134746fbSeric #ifdef NAMED_BIND
13582bcc6d2dSeric 	if (ConfigLevel < 2)
1359912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1360134746fbSeric #endif
13615dfc646bSeric 
1362b31e7f2bSeric 	/* arrange a return receipt if requested */
13638c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1364b31e7f2bSeric 	{
1365b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1366b31e7f2bSeric 		/* do we want to send back more info? */
1367b31e7f2bSeric 	}
1368b31e7f2bSeric 
1369c77d1c25Seric 	/*
137063780dbdSeric 	**  Do final status disposal.
137163780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1372c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1373c77d1c25Seric 	**		addressees.
1374c77d1c25Seric 	*/
1375c77d1c25Seric 
1376b31e7f2bSeric   give_up:
137763780dbdSeric 	if (tobuf[0] != '\0')
137881161401Seric 		giveresponse(rcode, m, mci, e);
1379772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1380b31e7f2bSeric 	{
1381dde5acadSeric 		if (rcode != EX_OK)
138283b7ddc9Seric 			markfailure(e, to, rcode);
1383dde5acadSeric 		else
1384655518ecSeric 		{
1385dde5acadSeric 			to->q_flags |= QSENT;
1386655518ecSeric 			e->e_nsent++;
1387655518ecSeric 		}
1388b31e7f2bSeric 	}
1389b31e7f2bSeric 
1390b31e7f2bSeric 	/*
1391b31e7f2bSeric 	**  Restore state and return.
1392b31e7f2bSeric 	*/
1393c77d1c25Seric 
13947febfd66Seric #ifdef XDEBUG
13957febfd66Seric 	{
13967febfd66Seric 		char wbuf[MAXLINE];
13977febfd66Seric 
13987febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
139944622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
140044622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
140144622ea3Seric 			m->m_name);
14027febfd66Seric 		checkfd012(wbuf);
14037febfd66Seric 	}
14047febfd66Seric #endif
14057febfd66Seric 
140635490626Seric 	errno = 0;
1407588cad61Seric 	define('g', (char *) NULL, e);
14085826d9d3Seric 	return (rcode);
140925a99e2eSeric }
14105dfc646bSeric /*
141183b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
141283b7ddc9Seric **
141383b7ddc9Seric **	Parameters:
141483b7ddc9Seric **		e -- the envelope we are sending.
141583b7ddc9Seric **		q -- the address to mark.
141683b7ddc9Seric **		rcode -- the code signifying the particular failure.
141783b7ddc9Seric **
141883b7ddc9Seric **	Returns:
141983b7ddc9Seric **		none.
142083b7ddc9Seric **
142183b7ddc9Seric **	Side Effects:
142283b7ddc9Seric **		marks the address (and possibly the envelope) with the
142383b7ddc9Seric **			failure so that an error will be returned or
142483b7ddc9Seric **			the message will be queued, as appropriate.
142583b7ddc9Seric */
142683b7ddc9Seric 
142783b7ddc9Seric markfailure(e, q, rcode)
142883b7ddc9Seric 	register ENVELOPE *e;
142983b7ddc9Seric 	register ADDRESS *q;
143083b7ddc9Seric 	int rcode;
143183b7ddc9Seric {
143219c47125Seric 	char buf[MAXLINE];
143319c47125Seric 
143483b7ddc9Seric 	if (rcode == EX_OK)
143583b7ddc9Seric 		return;
1436f170942cSeric 	else if (rcode == EX_TEMPFAIL)
143783b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1438f170942cSeric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
1439f170942cSeric 		q->q_flags |= QBADADDR;
144083b7ddc9Seric }
144183b7ddc9Seric /*
1442c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1443c579ef51Seric **
1444c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1445c579ef51Seric **	violation), so we report those specially.  For other
1446c579ef51Seric **	errors, we choose a status message (into statmsg),
1447c579ef51Seric **	and if it represents an error, we print it.
1448c579ef51Seric **
1449c579ef51Seric **	Parameters:
1450c579ef51Seric **		pid -- pid of mailer.
1451c9be6216Seric **		e -- the current envelope.
1452c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1453c9be6216Seric **			(for error messages).
1454c579ef51Seric **
1455c579ef51Seric **	Returns:
1456c579ef51Seric **		exit code of mailer.
1457c579ef51Seric **
1458c579ef51Seric **	Side Effects:
1459c579ef51Seric **		none.
1460c579ef51Seric */
1461c579ef51Seric 
1462c9be6216Seric endmailer(mci, e, pv)
1463b31e7f2bSeric 	register MCI *mci;
1464c9be6216Seric 	register ENVELOPE *e;
1465c9be6216Seric 	char **pv;
1466c579ef51Seric {
1467588cad61Seric 	int st;
1468c579ef51Seric 
146975889e88Seric 	/* close any connections */
147075889e88Seric 	if (mci->mci_in != NULL)
1471c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
147275889e88Seric 	if (mci->mci_out != NULL)
1473c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
147475889e88Seric 	mci->mci_in = mci->mci_out = NULL;
147575889e88Seric 	mci->mci_state = MCIS_CLOSED;
147675889e88Seric 
147733db8731Seric 	/* in the IPC case there is nothing to wait for */
147875889e88Seric 	if (mci->mci_pid == 0)
147933db8731Seric 		return (EX_OK);
148033db8731Seric 
148133db8731Seric 	/* wait for the mailer process to die and collect status */
148275889e88Seric 	st = waitfor(mci->mci_pid);
1483588cad61Seric 	if (st == -1)
148478de67c1Seric 	{
1485c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1486588cad61Seric 		return (EX_SOFTWARE);
1487c579ef51Seric 	}
148833db8731Seric 
1489bf9bc890Seric 	if (WIFEXITED(st))
1490c579ef51Seric 	{
1491bf9bc890Seric 		/* normal death -- return status */
1492bf9bc890Seric 		return (WEXITSTATUS(st));
1493bf9bc890Seric 	}
1494bf9bc890Seric 
1495bf9bc890Seric 	/* it died a horrid death */
1496c9be6216Seric 	syserr("mailer %s died with signal %o", pv[0], st);
1497c9be6216Seric 
1498c9be6216Seric 	/* log the arguments */
1499c9be6216Seric 	if (e->e_xfp != NULL)
1500c9be6216Seric 	{
1501c9be6216Seric 		register char **av;
1502c9be6216Seric 
1503c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1504c9be6216Seric 		for (av = pv; *av != NULL; av++)
1505c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1506c9be6216Seric 		fprintf(e->e_xfp, "\n");
1507c9be6216Seric 	}
1508c9be6216Seric 
15095f73204aSeric 	ExitStat = EX_TEMPFAIL;
15105f73204aSeric 	return (EX_TEMPFAIL);
1511c579ef51Seric }
1512c579ef51Seric /*
151325a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
151425a99e2eSeric **
151525a99e2eSeric **	Parameters:
151625a99e2eSeric **		stat -- the status code from the mailer (high byte
151725a99e2eSeric **			only; core dumps must have been taken care of
151825a99e2eSeric **			already).
151981161401Seric **		m -- the mailer info for this mailer.
152081161401Seric **		mci -- the mailer connection info -- can be NULL if the
152181161401Seric **			response is given before the connection is made.
152281161401Seric **		e -- the current envelope.
152325a99e2eSeric **
152425a99e2eSeric **	Returns:
1525db8841e9Seric **		none.
152625a99e2eSeric **
152725a99e2eSeric **	Side Effects:
1528c1f9df2cSeric **		Errors may be incremented.
152925a99e2eSeric **		ExitStat may be set.
153025a99e2eSeric */
153125a99e2eSeric 
153281161401Seric giveresponse(stat, m, mci, e)
153325a99e2eSeric 	int stat;
1534588cad61Seric 	register MAILER *m;
153581161401Seric 	register MCI *mci;
1536198d9be0Seric 	ENVELOPE *e;
153725a99e2eSeric {
15389c16475dSeric 	register const char *statmsg;
153925a99e2eSeric 	extern char *SysExMsg[];
154025a99e2eSeric 	register int i;
1541d4bd8f0eSbostic 	extern int N_SysEx;
1542198d9be0Seric 	char buf[MAXLINE];
154325a99e2eSeric 
154413bbc08cSeric 	/*
154513bbc08cSeric 	**  Compute status message from code.
154613bbc08cSeric 	*/
154713bbc08cSeric 
154825a99e2eSeric 	i = stat - EX__BASE;
1549588cad61Seric 	if (stat == 0)
15506fe8c3bcSeric 	{
1551588cad61Seric 		statmsg = "250 Sent";
1552ce5531bdSeric 		if (e->e_statmsg != NULL)
15536fe8c3bcSeric 		{
1554ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15556fe8c3bcSeric 			statmsg = buf;
15566fe8c3bcSeric 		}
15576fe8c3bcSeric 	}
1558588cad61Seric 	else if (i < 0 || i > N_SysEx)
1559588cad61Seric 	{
1560588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1561588cad61Seric 		stat = EX_UNAVAILABLE;
1562588cad61Seric 		statmsg = buf;
1563588cad61Seric 	}
1564198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1565198d9be0Seric 	{
15667d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1567d4bd8f0eSbostic #ifdef NAMED_BIND
1568f28da541Smiriam 		if (h_errno == TRY_AGAIN)
15694d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1570f28da541Smiriam 		else
1571d4bd8f0eSbostic #endif
1572f28da541Smiriam 		{
15738557d168Seric 			if (errno != 0)
1574d87e85f3Seric 				statmsg = errstring(errno);
1575d87e85f3Seric 			else
1576d87e85f3Seric 			{
1577d87e85f3Seric #ifdef SMTP
1578d87e85f3Seric 				extern char SmtpError[];
1579d87e85f3Seric 
1580d87e85f3Seric 				statmsg = SmtpError;
15816c2c3107Seric #else /* SMTP */
1582d87e85f3Seric 				statmsg = NULL;
15836c2c3107Seric #endif /* SMTP */
1584d87e85f3Seric 			}
1585f28da541Smiriam 		}
1586d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1587d87e85f3Seric 		{
158887c9b3e7Seric 			(void) strcat(buf, ": ");
1589d87e85f3Seric 			(void) strcat(buf, statmsg);
15908557d168Seric 		}
1591198d9be0Seric 		statmsg = buf;
1592198d9be0Seric 	}
1593f170942cSeric #ifdef NAMED_BIND
1594f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1595f170942cSeric 	{
15964d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1597f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1598f170942cSeric 		statmsg = buf;
1599f170942cSeric 	}
1600f170942cSeric #endif
160125a99e2eSeric 	else
1602d87e85f3Seric 	{
160325a99e2eSeric 		statmsg = SysExMsg[i];
16047d55540cSeric 		if (*statmsg++ == ':')
16057d55540cSeric 		{
16067d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16077d55540cSeric 			statmsg = buf;
16087d55540cSeric 		}
1609d87e85f3Seric 	}
1610588cad61Seric 
1611588cad61Seric 	/*
1612588cad61Seric 	**  Print the message as appropriate
1613588cad61Seric 	*/
1614588cad61Seric 
1615198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1616e12d03eeSeric 		message(&statmsg[4], errstring(errno));
161725a99e2eSeric 	else
161825a99e2eSeric 	{
1619c1f9df2cSeric 		Errors++;
1620e12d03eeSeric 		usrerr(statmsg, errstring(errno));
162125a99e2eSeric 	}
162225a99e2eSeric 
162325a99e2eSeric 	/*
162425a99e2eSeric 	**  Final cleanup.
162525a99e2eSeric 	**	Log a record of the transaction.  Compute the new
162625a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
162725a99e2eSeric 	**	that.
162825a99e2eSeric 	*/
162925a99e2eSeric 
16302f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
163181161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1632eb238f8cSeric 
1633eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1634eb238f8cSeric 		setstat(stat);
1635198d9be0Seric 	if (stat != EX_OK)
1636198d9be0Seric 	{
1637198d9be0Seric 		if (e->e_message != NULL)
1638198d9be0Seric 			free(e->e_message);
1639198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1640198d9be0Seric 	}
16418557d168Seric 	errno = 0;
1642d4bd8f0eSbostic #ifdef NAMED_BIND
1643f28da541Smiriam 	h_errno = 0;
1644d4bd8f0eSbostic #endif
1645eb238f8cSeric }
1646eb238f8cSeric /*
1647eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1648eb238f8cSeric **
1649eb238f8cSeric **	Parameters:
165081161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
165181161401Seric **		mci -- the mailer connection info -- can be NULL if the
165281161401Seric **			log is occuring when no connection is active.
165381161401Seric **		stat -- the message to print for the status.
165481161401Seric **		e -- the current envelope.
1655eb238f8cSeric **
1656eb238f8cSeric **	Returns:
1657eb238f8cSeric **		none
1658eb238f8cSeric **
1659eb238f8cSeric **	Side Effects:
1660eb238f8cSeric **		none
1661eb238f8cSeric */
1662eb238f8cSeric 
166381161401Seric logdelivery(m, mci, stat, e)
166481161401Seric 	MAILER *m;
166581161401Seric 	register MCI *mci;
1666eb238f8cSeric 	char *stat;
1667b31e7f2bSeric 	register ENVELOPE *e;
16685cf56be3Seric {
1669eb238f8cSeric # ifdef LOG
1670d6acf3eeSeric 	char buf[512];
16719507d1f9Seric 
167248ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
167381161401Seric 
167448ed5d33Seric 	if (m != NULL)
167571ff6caaSeric 	{
167648ed5d33Seric 		(void) strcat(buf, ", mailer=");
167748ed5d33Seric 		(void) strcat(buf, m->m_name);
167871ff6caaSeric 	}
167948ed5d33Seric 
168048ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
168171ff6caaSeric 	{
168271ff6caaSeric # ifdef DAEMON
1683e2f2f828Seric 		extern SOCKADDR CurHostAddr;
168448ed5d33Seric # endif
168571ff6caaSeric 
168648ed5d33Seric 		(void) strcat(buf, ", relay=");
168748ed5d33Seric 		(void) strcat(buf, mci->mci_host);
168848ed5d33Seric 
168948ed5d33Seric # ifdef DAEMON
169048ed5d33Seric 		(void) strcat(buf, " (");
1691e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
169248ed5d33Seric 		(void) strcat(buf, ")");
169371ff6caaSeric # endif
169471ff6caaSeric 	}
16959507d1f9Seric 	else
169648ed5d33Seric 	{
169748ed5d33Seric 		char *p = macvalue('h', e);
16989507d1f9Seric 
169948ed5d33Seric 		if (p != NULL && p[0] != '\0')
170048ed5d33Seric 		{
170148ed5d33Seric 			(void) strcat(buf, ", relay=");
170248ed5d33Seric 			(void) strcat(buf, p);
170348ed5d33Seric 		}
170448ed5d33Seric 	}
1705d6acf3eeSeric 
1706d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1707d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
17086c2c3107Seric # endif /* LOG */
170925a99e2eSeric }
171025a99e2eSeric /*
171151552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
171225a99e2eSeric **
171351552439Seric **	This can be made an arbitrary message separator by changing $l
171451552439Seric **
17159b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
17169b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
17179b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
17189b6c17a6Seric **	this kind of antique garbage????
171925a99e2eSeric **
172025a99e2eSeric **	Parameters:
172151552439Seric **		fp -- the file to output to.
172251552439Seric **		m -- the mailer describing this entry.
172325a99e2eSeric **
172425a99e2eSeric **	Returns:
172551552439Seric **		none
172625a99e2eSeric **
172725a99e2eSeric **	Side Effects:
172851552439Seric **		outputs some text to fp.
172925a99e2eSeric */
173025a99e2eSeric 
1731b31e7f2bSeric putfromline(fp, m, e)
173251552439Seric 	register FILE *fp;
173351552439Seric 	register MAILER *m;
1734b31e7f2bSeric 	ENVELOPE *e;
173525a99e2eSeric {
17362bc47524Seric 	char *template = "\201l\n";
173751552439Seric 	char buf[MAXLINE];
173825a99e2eSeric 
173957fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
174051552439Seric 		return;
174113bbc08cSeric 
17422c7e1b8dSeric # ifdef UGLYUUCP
174357fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
174474b6e67bSeric 	{
1745ea09d6edSeric 		char *bang;
1746ea09d6edSeric 		char xbuf[MAXLINE];
174774b6e67bSeric 
1748ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17496c2c3107Seric 		bang = strchr(buf, '!');
175074b6e67bSeric 		if (bang == NULL)
175134fcca25Seric 		{
175234fcca25Seric 			errno = 0;
175334fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
175434fcca25Seric 		}
175574b6e67bSeric 		else
1756588cad61Seric 		{
1757ea09d6edSeric 			*bang++ = '\0';
17582bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1759ea09d6edSeric 			template = xbuf;
176074b6e67bSeric 		}
1761588cad61Seric 	}
17626c2c3107Seric # endif /* UGLYUUCP */
1763b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
176477b52738Seric 	putline(buf, fp, m);
1765bc6e2962Seric }
1766bc6e2962Seric /*
176751552439Seric **  PUTBODY -- put the body of a message.
176851552439Seric **
176951552439Seric **	Parameters:
177051552439Seric **		fp -- file to output onto.
177177b52738Seric **		m -- a mailer descriptor to control output format.
17729a6a5f55Seric **		e -- the envelope to put out.
177303c02fdeSeric **		separator -- if non-NULL, a message separator that must
177403c02fdeSeric **			not be permitted in the resulting message.
177551552439Seric **
177651552439Seric **	Returns:
177751552439Seric **		none.
177851552439Seric **
177951552439Seric **	Side Effects:
178051552439Seric **		The message is written onto fp.
178151552439Seric */
178251552439Seric 
178303c02fdeSeric putbody(fp, m, e, separator)
178451552439Seric 	FILE *fp;
1785588cad61Seric 	MAILER *m;
17869a6a5f55Seric 	register ENVELOPE *e;
178703c02fdeSeric 	char *separator;
178851552439Seric {
178977b52738Seric 	char buf[MAXLINE];
179051552439Seric 
179151552439Seric 	/*
179251552439Seric 	**  Output the body of the message
179351552439Seric 	*/
179451552439Seric 
17959a6a5f55Seric 	if (e->e_dfp == NULL)
179651552439Seric 	{
17979a6a5f55Seric 		if (e->e_df != NULL)
17989a6a5f55Seric 		{
17999a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
18009a6a5f55Seric 			if (e->e_dfp == NULL)
18018f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
1802e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
18039a6a5f55Seric 		}
18049a6a5f55Seric 		else
180577b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
18069a6a5f55Seric 	}
18079a6a5f55Seric 	if (e->e_dfp != NULL)
18089a6a5f55Seric 	{
18099a6a5f55Seric 		rewind(e->e_dfp);
181077b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
181124fc8aeeSeric 		{
181224fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1813d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
18143462ad9eSeric 				(void) putc('>', fp);
181503c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
181603c02fdeSeric 			{
181703c02fdeSeric 				/* possible separator */
181803c02fdeSeric 				int sl = strlen(separator);
181903c02fdeSeric 
182003c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
182103c02fdeSeric 					(void) putc(' ', fp);
182203c02fdeSeric 			}
182377b52738Seric 			putline(buf, fp, m);
182424fc8aeeSeric 		}
182551552439Seric 
18269a6a5f55Seric 		if (ferror(e->e_dfp))
182751552439Seric 		{
182851552439Seric 			syserr("putbody: read error");
182951552439Seric 			ExitStat = EX_IOERR;
183051552439Seric 		}
183151552439Seric 	}
183251552439Seric 
18330890ba1fSeric 	/* some mailers want extra blank line at end of message */
18340890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
18350890ba1fSeric 		putline("", fp, m);
18360890ba1fSeric 
183751552439Seric 	(void) fflush(fp);
183851552439Seric 	if (ferror(fp) && errno != EPIPE)
183951552439Seric 	{
184051552439Seric 		syserr("putbody: write error");
184151552439Seric 		ExitStat = EX_IOERR;
184251552439Seric 	}
184351552439Seric 	errno = 0;
184425a99e2eSeric }
184525a99e2eSeric /*
184625a99e2eSeric **  MAILFILE -- Send a message to a file.
184725a99e2eSeric **
1848f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1849f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1850f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1851f129ec7dSeric **	sendmail runs as root.
1852f129ec7dSeric **
1853588cad61Seric **	This could be done as a subordinate mailer, except that it
1854588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1855588cad61Seric **	view this as being sufficiently important as to include it
1856588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1857588cad61Seric **	to create another process plus some pipes to save the message.
1858588cad61Seric **
185925a99e2eSeric **	Parameters:
186025a99e2eSeric **		filename -- the name of the file to send to.
18616259796dSeric **		ctladdr -- the controlling address header -- includes
18626259796dSeric **			the userid/groupid to be when sending.
186325a99e2eSeric **
186425a99e2eSeric **	Returns:
186525a99e2eSeric **		The exit code associated with the operation.
186625a99e2eSeric **
186725a99e2eSeric **	Side Effects:
186825a99e2eSeric **		none.
186925a99e2eSeric */
187025a99e2eSeric 
1871b31e7f2bSeric mailfile(filename, ctladdr, e)
187225a99e2eSeric 	char *filename;
18736259796dSeric 	ADDRESS *ctladdr;
1874b31e7f2bSeric 	register ENVELOPE *e;
187525a99e2eSeric {
187625a99e2eSeric 	register FILE *f;
187732d19d43Seric 	register int pid;
187815d084d5Seric 	int mode;
187925a99e2eSeric 
1880671745f3Seric 	if (tTd(11, 1))
1881671745f3Seric 	{
1882671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
1883671745f3Seric 		printaddr(ctladdr, FALSE);
1884671745f3Seric 	}
1885671745f3Seric 
1886f170942cSeric 	if (e->e_xfp != NULL)
1887f170942cSeric 		fflush(e->e_xfp);
1888f170942cSeric 
188932d19d43Seric 	/*
189032d19d43Seric 	**  Fork so we can change permissions here.
189132d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
189232d19d43Seric 	**	the complications of calling subroutines, etc.
189332d19d43Seric 	*/
189432d19d43Seric 
189532d19d43Seric 	DOFORK(fork);
189632d19d43Seric 
189732d19d43Seric 	if (pid < 0)
189832d19d43Seric 		return (EX_OSERR);
189932d19d43Seric 	else if (pid == 0)
190032d19d43Seric 	{
190132d19d43Seric 		/* child -- actually write to file */
1902f129ec7dSeric 		struct stat stb;
1903f129ec7dSeric 
19042b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
19052b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
19062b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
19073462ad9eSeric 		(void) umask(OldUmask);
190895f16dc0Seric 
1909f129ec7dSeric 		if (stat(filename, &stb) < 0)
19103a98e7eaSeric 			stb.st_mode = FileMode;
191115d084d5Seric 		mode = stb.st_mode;
191295f16dc0Seric 
191395f16dc0Seric 		/* limit the errors to those actually caused in the child */
191495f16dc0Seric 		errno = 0;
191595f16dc0Seric 		ExitStat = EX_OK;
191695f16dc0Seric 
1917f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1918f129ec7dSeric 			exit(EX_CANTCREAT);
191903827b5fSeric 		if (ctladdr == NULL)
19208f9146b0Srick 			ctladdr = &e->e_from;
192115d084d5Seric 		else
192215d084d5Seric 		{
192315d084d5Seric 			/* ignore setuid and setgid bits */
192415d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
192515d084d5Seric 		}
192615d084d5Seric 
19278f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
19288f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
19298f9146b0Srick 		{
19308f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
193195f16dc0Seric 			if (e->e_dfp == NULL)
193295f16dc0Seric 			{
19338f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
1934e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
19358f9146b0Srick 			}
19368f9146b0Srick 		}
19378f9146b0Srick 
193815d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1939e36b99e2Seric 		{
194095f16dc0Seric 			if (ctladdr->q_uid == 0)
194195f16dc0Seric 			{
1942898a126bSbostic 				(void) initgroups(DefUser, DefGid);
194395f16dc0Seric 			}
194495f16dc0Seric 			else
194595f16dc0Seric 			{
1946898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1947898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1948898a126bSbostic 					ctladdr->q_gid);
1949898a126bSbostic 			}
1950e36b99e2Seric 		}
195115d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1952e36b99e2Seric 		{
1953e36b99e2Seric 			if (ctladdr->q_uid == 0)
1954e36b99e2Seric 				(void) setuid(DefUid);
1955e36b99e2Seric 			else
19566259796dSeric 				(void) setuid(ctladdr->q_uid);
1957e36b99e2Seric 		}
195895f16dc0Seric 		FileName = filename;
195995f16dc0Seric 		LineNumber = 0;
19603a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
196125a99e2eSeric 		if (f == NULL)
196295f16dc0Seric 		{
1963b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
196432d19d43Seric 			exit(EX_CANTCREAT);
196595f16dc0Seric 		}
196625a99e2eSeric 
19670331ce05Seric 		putfromline(f, FileMailer, e);
19680331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
19690331ce05Seric 		putline("\n", f, FileMailer);
197003c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
19710331ce05Seric 		putline("\n", f, FileMailer);
197295f16dc0Seric 		if (ferror(f))
197395f16dc0Seric 		{
1974b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
197595f16dc0Seric 			setstat(EX_IOERR);
197695f16dc0Seric 		}
1977ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
197832d19d43Seric 		(void) fflush(stdout);
1979e36b99e2Seric 
198027628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1981c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
198295f16dc0Seric 		exit(ExitStat);
198313bbc08cSeric 		/*NOTREACHED*/
198432d19d43Seric 	}
198532d19d43Seric 	else
198632d19d43Seric 	{
198732d19d43Seric 		/* parent -- wait for exit status */
1988588cad61Seric 		int st;
198932d19d43Seric 
1990588cad61Seric 		st = waitfor(pid);
1991bf9bc890Seric 		if (WIFEXITED(st))
1992bf9bc890Seric 			return (WEXITSTATUS(st));
1993588cad61Seric 		else
1994b6a0de9dSeric 		{
1995b6a0de9dSeric 			syserr("child died on signal %d", st);
1996bf9bc890Seric 			return (EX_UNAVAILABLE);
1997b6a0de9dSeric 		}
19988f9146b0Srick 		/*NOTREACHED*/
199932d19d43Seric 	}
200025a99e2eSeric }
2001ea4dc939Seric /*
2002e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2003e103b48fSeric **
2004e103b48fSeric **	The signature describes how we are going to send this -- it
2005e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2006e103b48fSeric **	an ordered list of MX hosts.
2007e103b48fSeric **
2008e103b48fSeric **	Parameters:
2009e103b48fSeric **		m -- the mailer describing this host.
2010e103b48fSeric **		host -- the host name.
2011e103b48fSeric **		e -- the current envelope.
2012e103b48fSeric **
2013e103b48fSeric **	Returns:
2014e103b48fSeric **		The signature for this host.
2015e103b48fSeric **
2016e103b48fSeric **	Side Effects:
2017e103b48fSeric **		Can tweak the symbol table.
2018e103b48fSeric */
2019e103b48fSeric 
2020e103b48fSeric char *
2021e103b48fSeric hostsignature(m, host, e)
2022e103b48fSeric 	register MAILER *m;
2023e103b48fSeric 	char *host;
2024e103b48fSeric 	ENVELOPE *e;
2025e103b48fSeric {
2026e103b48fSeric 	register char *p;
2027e103b48fSeric 	register STAB *s;
2028e103b48fSeric 	int i;
2029e103b48fSeric 	int len;
2030e103b48fSeric #ifdef NAMED_BIND
2031e103b48fSeric 	int nmx;
2032e103b48fSeric 	auto int rcode;
2033bafdc4e5Seric 	char *hp;
2034bafdc4e5Seric 	char *endp;
2035516782b4Seric 	int oldoptions;
2036e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2037e103b48fSeric #endif
2038e103b48fSeric 
2039e103b48fSeric 	/*
2040e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2041e103b48fSeric 	*/
2042e103b48fSeric 
2043e103b48fSeric 	p = m->m_mailer;
2044e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2045e103b48fSeric 	{
2046e103b48fSeric 		/* just an ordinary mailer */
2047e103b48fSeric 		return host;
2048e103b48fSeric 	}
2049e103b48fSeric 
2050e103b48fSeric 	/*
2051e103b48fSeric 	**  Look it up in the symbol table.
2052e103b48fSeric 	*/
2053e103b48fSeric 
2054e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2055e103b48fSeric 	if (s->s_hostsig != NULL)
2056e103b48fSeric 		return s->s_hostsig;
2057e103b48fSeric 
2058e103b48fSeric 	/*
2059e103b48fSeric 	**  Not already there -- create a signature.
2060e103b48fSeric 	*/
2061e103b48fSeric 
2062e103b48fSeric #ifdef NAMED_BIND
2063516782b4Seric 	if (ConfigLevel < 2)
2064516782b4Seric 	{
2065516782b4Seric 		oldoptions = _res.options;
2066516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2067516782b4Seric 	}
2068516782b4Seric 
2069bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2070bafdc4e5Seric 	{
2071bafdc4e5Seric 		endp = strchr(hp, ':');
2072bafdc4e5Seric 		if (endp != NULL)
2073bafdc4e5Seric 			*endp = '\0';
2074bafdc4e5Seric 
20757bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
20767d55540cSeric 
2077e103b48fSeric 		if (nmx <= 0)
2078e103b48fSeric 		{
2079e103b48fSeric 			register MCI *mci;
2080e103b48fSeric 			extern int errno;
2081e103b48fSeric 
2082e103b48fSeric 			/* update the connection info for this host */
2083bafdc4e5Seric 			mci = mci_get(hp, m);
2084e103b48fSeric 			mci->mci_exitstat = rcode;
2085e103b48fSeric 			mci->mci_errno = errno;
2086f170942cSeric #ifdef NAMED_BIND
2087f170942cSeric 			mci->mci_herrno = h_errno;
2088f170942cSeric #endif
2089e103b48fSeric 
2090e103b48fSeric 			/* and return the original host name as the signature */
2091bafdc4e5Seric 			nmx = 1;
2092bafdc4e5Seric 			mxhosts[0] = hp;
2093e103b48fSeric 		}
2094e103b48fSeric 
2095e103b48fSeric 		len = 0;
2096e103b48fSeric 		for (i = 0; i < nmx; i++)
2097e103b48fSeric 		{
2098e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2099e103b48fSeric 		}
2100bafdc4e5Seric 		if (s->s_hostsig != NULL)
2101bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2102bafdc4e5Seric 		p = xalloc(len);
2103bafdc4e5Seric 		if (s->s_hostsig != NULL)
2104bafdc4e5Seric 		{
2105bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2106bafdc4e5Seric 			free(s->s_hostsig);
2107bafdc4e5Seric 			s->s_hostsig = p;
2108bafdc4e5Seric 			p += strlen(p);
2109bafdc4e5Seric 			*p++ = ':';
2110bafdc4e5Seric 		}
2111bafdc4e5Seric 		else
2112bafdc4e5Seric 			s->s_hostsig = p;
2113e103b48fSeric 		for (i = 0; i < nmx; i++)
2114e103b48fSeric 		{
2115e103b48fSeric 			if (i != 0)
2116e103b48fSeric 				*p++ = ':';
2117e103b48fSeric 			strcpy(p, mxhosts[i]);
2118e103b48fSeric 			p += strlen(p);
2119e103b48fSeric 		}
2120bafdc4e5Seric 		if (endp != NULL)
2121bafdc4e5Seric 			*endp++ = ':';
2122bafdc4e5Seric 	}
2123e103b48fSeric 	makelower(s->s_hostsig);
2124516782b4Seric 	if (ConfigLevel < 2)
2125516782b4Seric 		_res.options = oldoptions;
2126e103b48fSeric #else
2127e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2128e103b48fSeric 	s->s_hostsig = host;
2129e103b48fSeric #endif
2130e103b48fSeric 	if (tTd(17, 1))
2131e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2132e103b48fSeric 	return s->s_hostsig;
2133e103b48fSeric }
2134