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*faad2b16Seric static char sccsid[] = "@(#)deliver.c	8.29 (Berkeley) 10/03/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;
411*faad2b16Seric 
412*faad2b16Seric 	/* be sure to use any new error message */
413*faad2b16Seric 	if (e->e_message != NULL)
414*faad2b16Seric 		free(e->e_message);
415*faad2b16Seric 	e->e_message = NULL;
416*faad2b16Seric 
417*faad2b16Seric 	/* now run through the queue */
4189c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4199c9e68d9Seric 	{
420fdc75a0fSeric #ifdef XDEBUG
421fdc75a0fSeric 		char wbuf[MAXNAME + 20];
422fdc75a0fSeric 
423fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
424fdc75a0fSeric 		checkfd012(wbuf);
425fdc75a0fSeric #endif
4269c9e68d9Seric 		if (mode == SM_VERIFY)
4279c9e68d9Seric 		{
4289c9e68d9Seric 			e->e_to = q->q_paddr;
4299c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4308dfca105Seric 			{
4318dfca105Seric 				message("deliverable: mailer %s, host %s, user %s",
4328dfca105Seric 					q->q_mailer->m_name,
4338dfca105Seric 					q->q_host,
4348dfca105Seric 					q->q_user);
4358dfca105Seric 			}
4369c9e68d9Seric 		}
4379c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4389c9e68d9Seric 		{
4399c9e68d9Seric # ifdef QUEUE
4409c9e68d9Seric 			/*
4419c9e68d9Seric 			**  Checkpoint the send list every few addresses
4429c9e68d9Seric 			*/
4439c9e68d9Seric 
4449c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4459c9e68d9Seric 			{
4469c9e68d9Seric 				queueup(e, TRUE, FALSE);
4479c9e68d9Seric 				e->e_nsent = 0;
4489c9e68d9Seric 			}
4499c9e68d9Seric # endif /* QUEUE */
4509c9e68d9Seric 			(void) deliver(e, q);
4519c9e68d9Seric 		}
4529c9e68d9Seric 	}
4539c9e68d9Seric 	Verbose = oldverbose;
4549c9e68d9Seric 
455fdc75a0fSeric #ifdef XDEBUG
456fdc75a0fSeric 	checkfd012("end of sendenvelope");
457fdc75a0fSeric #endif
458fdc75a0fSeric 
4599c9e68d9Seric 	if (mode == SM_FORK)
4609c9e68d9Seric 		finis();
4619c9e68d9Seric }
4629c9e68d9Seric /*
4639c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4649c9e68d9Seric **
4659c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4669c9e68d9Seric **	two processes on the same stack!!!
4679c9e68d9Seric **
4689c9e68d9Seric **	Parameters:
4699c9e68d9Seric **		none.
4709c9e68d9Seric **
4719c9e68d9Seric **	Returns:
4729c9e68d9Seric **		From a macro???  You've got to be kidding!
4739c9e68d9Seric **
4749c9e68d9Seric **	Side Effects:
4759c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4769c9e68d9Seric **			pid of child in parent, zero in child.
4779c9e68d9Seric **			-1 on unrecoverable error.
4789c9e68d9Seric **
4799c9e68d9Seric **	Notes:
4809c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4819c9e68d9Seric **		vfork for you.....
4829c9e68d9Seric */
4839c9e68d9Seric 
4849c9e68d9Seric # define NFORKTRIES	5
4859c9e68d9Seric 
4869c9e68d9Seric # ifndef FORK
4879c9e68d9Seric # define FORK	fork
4889c9e68d9Seric # endif
4899c9e68d9Seric 
4909c9e68d9Seric # define DOFORK(fORKfN) \
4919c9e68d9Seric {\
4929c9e68d9Seric 	register int i;\
4939c9e68d9Seric \
4949c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4959c9e68d9Seric 	{\
4969c9e68d9Seric 		pid = fORKfN();\
4979c9e68d9Seric 		if (pid >= 0)\
4989c9e68d9Seric 			break;\
4999c9e68d9Seric 		if (i > 0)\
5009c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5019c9e68d9Seric 	}\
5029c9e68d9Seric }
5039c9e68d9Seric /*
5049c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5059c9e68d9Seric **
5069c9e68d9Seric **	Parameters:
5079c9e68d9Seric **		none.
5089c9e68d9Seric **
5099c9e68d9Seric **	Returns:
5109c9e68d9Seric **		pid of child in parent.
5119c9e68d9Seric **		zero in child.
5129c9e68d9Seric **		-1 on error.
5139c9e68d9Seric **
5149c9e68d9Seric **	Side Effects:
5159c9e68d9Seric **		returns twice, once in parent and once in child.
5169c9e68d9Seric */
5179c9e68d9Seric 
5189c9e68d9Seric dofork()
5199c9e68d9Seric {
5209c9e68d9Seric 	register int pid;
5219c9e68d9Seric 
5229c9e68d9Seric 	DOFORK(fork);
5239c9e68d9Seric 	return (pid);
5249c9e68d9Seric }
5259c9e68d9Seric /*
52613bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
52713bbc08cSeric **
52813bbc08cSeric **	This routine delivers to everyone on the same host as the
52913bbc08cSeric **	user on the head of the list.  It is clever about mailers
53013bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
53113bbc08cSeric **	that it will deliver to all these addresses however -- so
53213bbc08cSeric **	deliver should be called once for each address on the
53313bbc08cSeric **	list.
53425a99e2eSeric **
53525a99e2eSeric **	Parameters:
536588cad61Seric **		e -- the envelope to deliver.
537c77d1c25Seric **		firstto -- head of the address list to deliver to.
53825a99e2eSeric **
53925a99e2eSeric **	Returns:
54025a99e2eSeric **		zero -- successfully delivered.
54125a99e2eSeric **		else -- some failure, see ExitStat for more info.
54225a99e2eSeric **
54325a99e2eSeric **	Side Effects:
54425a99e2eSeric **		The standard input is passed off to someone.
54525a99e2eSeric */
54625a99e2eSeric 
547588cad61Seric deliver(e, firstto)
548588cad61Seric 	register ENVELOPE *e;
549c77d1c25Seric 	ADDRESS *firstto;
55025a99e2eSeric {
55178442df3Seric 	char *host;			/* host being sent to */
55278442df3Seric 	char *user;			/* user being sent to */
55325a99e2eSeric 	char **pvp;
5545dfc646bSeric 	register char **mvp;
55525a99e2eSeric 	register char *p;
556588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5576259796dSeric 	ADDRESS *ctladdr;
558b31e7f2bSeric 	register MCI *mci;
559c77d1c25Seric 	register ADDRESS *to = firstto;
560c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
561772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
562911693bfSbostic 	int rcode;			/* response code */
563e103b48fSeric 	char *firstsig;			/* signature of firstto */
5649c9e68d9Seric 	int pid;
5659c9e68d9Seric 	char *curhost;
5669c9e68d9Seric 	int mpvect[2];
5679c9e68d9Seric 	int rpvect[2];
568ee6bf8dfSeric 	char *pv[MAXPV+1];
569579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
570ee6bf8dfSeric 	char buf[MAXNAME];
571c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
572fabb3bd4Seric 	extern int checkcompat();
5739c9e68d9Seric 	extern FILE *fdopen();
57425a99e2eSeric 
57535490626Seric 	errno = 0;
576ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5775dfc646bSeric 		return (0);
57825a99e2eSeric 
579134746fbSeric #ifdef NAMED_BIND
580912a731aSbostic 	/* unless interactive, try twice, over a minute */
581912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
582912a731aSbostic 		_res.retrans = 30;
583912a731aSbostic 		_res.retry = 2;
584912a731aSbostic 	}
585d4bd8f0eSbostic #endif
586912a731aSbostic 
58751552439Seric 	m = to->q_mailer;
58851552439Seric 	host = to->q_host;
589c9be6216Seric 	CurEnv = e;			/* just in case */
5904384d521Seric 	e->e_statmsg = NULL;
59151552439Seric 
5926ef48975Seric 	if (tTd(10, 1))
5935dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
59451552439Seric 			m->m_mno, host, to->q_user);
595f3dbc832Seric 
596f3dbc832Seric 	/*
597f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
598f3dbc832Seric 	**  connections now, just mark these addresses and return.
599f3dbc832Seric 	**	This is useful if we want to batch connections to
600f3dbc832Seric 	**	reduce load.  This will cause the messages to be
601f3dbc832Seric 	**	queued up, and a daemon will come along to send the
602f3dbc832Seric 	**	messages later.
603f3dbc832Seric 	**		This should be on a per-mailer basis.
604f3dbc832Seric 	*/
605f3dbc832Seric 
60679aa5155Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
607f3dbc832Seric 	{
608f3dbc832Seric 		for (; to != NULL; to = to->q_next)
609f4560e80Seric 		{
610ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
611c6e18ac5Seric 			    to->q_mailer != m)
612f4560e80Seric 				continue;
613268a25e1Seric 			to->q_flags |= QQUEUEUP;
614588cad61Seric 			e->e_to = to->q_paddr;
61508b25121Seric 			message("queued");
6162f624c86Seric 			if (LogLevel > 8)
61781161401Seric 				logdelivery(m, NULL, "queued", e);
618f4560e80Seric 		}
619588cad61Seric 		e->e_to = NULL;
620f3dbc832Seric 		return (0);
621f3dbc832Seric 	}
622f3dbc832Seric 
62325a99e2eSeric 	/*
6245dfc646bSeric 	**  Do initial argv setup.
6255dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6265dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6275dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6285dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6295dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6303bea8136Seric 	**		The from address rewrite is expected to make
6313bea8136Seric 	**		the address relative to the other end.
6325dfc646bSeric 	*/
6335dfc646bSeric 
63478442df3Seric 	/* rewrite from address, using rewriting rules */
635efe54562Seric 	rcode = EX_OK;
636efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
637efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
638efe54562Seric 					   &rcode, e));
639ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
640588cad61Seric 	define('h', host, e);			/* to host */
6415dfc646bSeric 	Errors = 0;
6425dfc646bSeric 	pvp = pv;
6435dfc646bSeric 	*pvp++ = m->m_argv[0];
6445dfc646bSeric 
6455dfc646bSeric 	/* insert -f or -r flag as appropriate */
64657fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6475dfc646bSeric 	{
64857fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6495dfc646bSeric 			*pvp++ = "-f";
6505dfc646bSeric 		else
6515dfc646bSeric 			*pvp++ = "-r";
652c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6535dfc646bSeric 	}
6545dfc646bSeric 
6555dfc646bSeric 	/*
6565dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6575dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6585dfc646bSeric 	**  be one of these, and there are only a few more slots
6595dfc646bSeric 	**  in the pv after it.
6605dfc646bSeric 	*/
6615dfc646bSeric 
6625dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6635dfc646bSeric 	{
6642bc47524Seric 		/* can't use strchr here because of sign extension problems */
6652bc47524Seric 		while (*p != '\0')
6662bc47524Seric 		{
6672bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6682bc47524Seric 			{
6692bc47524Seric 				if (*p == 'u')
6705dfc646bSeric 					break;
6712bc47524Seric 			}
6722bc47524Seric 		}
6732bc47524Seric 
6742bc47524Seric 		if (*p != '\0')
6755dfc646bSeric 			break;
6765dfc646bSeric 
6775dfc646bSeric 		/* this entry is safe -- go ahead and process it */
678588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6795dfc646bSeric 		*pvp++ = newstr(buf);
6805dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6815dfc646bSeric 		{
68208b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6835dfc646bSeric 			return (-1);
6845dfc646bSeric 		}
6855dfc646bSeric 	}
686c579ef51Seric 
68733db8731Seric 	/*
68833db8731Seric 	**  If we have no substitution for the user name in the argument
68933db8731Seric 	**  list, we know that we must supply the names otherwise -- and
69033db8731Seric 	**  SMTP is the answer!!
69133db8731Seric 	*/
69233db8731Seric 
6935dfc646bSeric 	if (*mvp == NULL)
694c579ef51Seric 	{
695c579ef51Seric 		/* running SMTP */
6962c7e1b8dSeric # ifdef SMTP
697c579ef51Seric 		clever = TRUE;
698c579ef51Seric 		*pvp = NULL;
6996c2c3107Seric # else /* SMTP */
70033db8731Seric 		/* oops!  we don't implement SMTP */
70108b25121Seric 		syserr("554 SMTP style mailer");
7022c7e1b8dSeric 		return (EX_SOFTWARE);
7036c2c3107Seric # endif /* SMTP */
704c579ef51Seric 	}
7055dfc646bSeric 
7065dfc646bSeric 	/*
7075dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7085dfc646bSeric 	**  run through our address list and append all the addresses
7095dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7105dfc646bSeric 	**  always send another copy later.
7115dfc646bSeric 	*/
7125dfc646bSeric 
7135dfc646bSeric 	tobuf[0] = '\0';
714588cad61Seric 	e->e_to = tobuf;
7156259796dSeric 	ctladdr = NULL;
716e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7175dfc646bSeric 	for (; to != NULL; to = to->q_next)
7185dfc646bSeric 	{
7195dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
72057fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7215dfc646bSeric 			break;
7225dfc646bSeric 
7235dfc646bSeric 		/* if already sent or not for this host, don't send */
724ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
725e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
726e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7275dfc646bSeric 			continue;
7286259796dSeric 
7294b22ea87Seric 		/* avoid overflowing tobuf */
730aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7314b22ea87Seric 			break;
7324b22ea87Seric 
7336ef48975Seric 		if (tTd(10, 1))
734772e6e50Seric 		{
735772e6e50Seric 			printf("\nsend to ");
736772e6e50Seric 			printaddr(to, FALSE);
737772e6e50Seric 		}
738772e6e50Seric 
7396259796dSeric 		/* compute effective uid/gid when sending */
7407da1035fSeric 		if (to->q_mailer == ProgMailer)
7416259796dSeric 			ctladdr = getctladdr(to);
7426259796dSeric 
7435dfc646bSeric 		user = to->q_user;
744588cad61Seric 		e->e_to = to->q_paddr;
74575f1ade9Seric 		if (tTd(10, 5))
74675f1ade9Seric 		{
74775f1ade9Seric 			printf("deliver: QDONTSEND ");
74875f1ade9Seric 			printaddr(to, FALSE);
74975f1ade9Seric 		}
750ee4b0922Seric 		to->q_flags |= QDONTSEND;
7515dfc646bSeric 
7525dfc646bSeric 		/*
7535dfc646bSeric 		**  Check to see that these people are allowed to
7545dfc646bSeric 		**  talk to each other.
7552a6e0786Seric 		*/
7562a6e0786Seric 
75769582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
75869582d2fSeric 		{
75969582d2fSeric 			NoReturn = TRUE;
76008b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
76181161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
76269582d2fSeric 			continue;
76369582d2fSeric 		}
764fabb3bd4Seric 		rcode = checkcompat(to, e);
7651793c9c5Seric 		if (rcode != EX_OK)
7665dfc646bSeric 		{
7671000c37aSeric 			markfailure(e, to, rcode);
76881161401Seric 			giveresponse(rcode, m, NULL, e);
7695dfc646bSeric 			continue;
7705dfc646bSeric 		}
7712a6e0786Seric 
7722a6e0786Seric 		/*
7739ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7749ec9501bSeric 		**	about them.
77525a99e2eSeric 		*/
77625a99e2eSeric 
77757fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
77825a99e2eSeric 		{
7791d8f1806Seric 			stripquotes(user);
7801d8f1806Seric 			stripquotes(host);
78125a99e2eSeric 		}
78225a99e2eSeric 
783cdb828c5Seric 		/* hack attack -- delivermail compatibility */
784cdb828c5Seric 		if (m == ProgMailer && *user == '|')
785cdb828c5Seric 			user++;
786cdb828c5Seric 
78725a99e2eSeric 		/*
7883efaed6eSeric 		**  If an error message has already been given, don't
7893efaed6eSeric 		**	bother to send to this address.
7903efaed6eSeric 		**
7913efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7923efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
7933efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
7943efaed6eSeric 		*/
7953efaed6eSeric 
7966cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7973efaed6eSeric 			continue;
7983efaed6eSeric 
799f2fec898Seric 		/* save statistics.... */
800588cad61Seric 		markstats(e, to);
801f2fec898Seric 
8023efaed6eSeric 		/*
80325a99e2eSeric 		**  See if this user name is "special".
80425a99e2eSeric 		**	If the user name has a slash in it, assume that this
80551552439Seric 		**	is a file -- send it off without further ado.  Note
80651552439Seric 		**	that this type of addresses is not processed along
80751552439Seric 		**	with the others, so we fudge on the To person.
80825a99e2eSeric 		*/
80925a99e2eSeric 
8102c017f8dSeric 		if (m == FileMailer)
81125a99e2eSeric 		{
812b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
81381161401Seric 			giveresponse(rcode, m, NULL, e);
814dde5acadSeric 			if (rcode == EX_OK)
815dde5acadSeric 				to->q_flags |= QSENT;
8165dfc646bSeric 			continue;
81725a99e2eSeric 		}
81825a99e2eSeric 
81913bbc08cSeric 		/*
82013bbc08cSeric 		**  Address is verified -- add this user to mailer
82113bbc08cSeric 		**  argv, and add it to the print list of recipients.
82213bbc08cSeric 		*/
82313bbc08cSeric 
824508daeccSeric 		/* link together the chain of recipients */
825508daeccSeric 		to->q_tchain = tochain;
826508daeccSeric 		tochain = to;
827508daeccSeric 
8285dfc646bSeric 		/* create list of users for error messages */
829db8841e9Seric 		(void) strcat(tobuf, ",");
830db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
831588cad61Seric 		define('u', user, e);		/* to user */
832b6f89e6eSeric 		p = to->q_home;
833b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
834b6f89e6eSeric 			p = ctladdr->q_home;
835b6f89e6eSeric 		define('z', p, e);	/* user's home */
8365dfc646bSeric 
837c579ef51Seric 		/*
838508daeccSeric 		**  Expand out this user into argument list.
839c579ef51Seric 		*/
840c579ef51Seric 
841508daeccSeric 		if (!clever)
842c579ef51Seric 		{
843588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8445dfc646bSeric 			*pvp++ = newstr(buf);
8455dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8465dfc646bSeric 			{
8475dfc646bSeric 				/* allow some space for trailing parms */
8485dfc646bSeric 				break;
8495dfc646bSeric 			}
8505dfc646bSeric 		}
851c579ef51Seric 	}
8525dfc646bSeric 
853145b49b1Seric 	/* see if any addresses still exist */
854145b49b1Seric 	if (tobuf[0] == '\0')
855c579ef51Seric 	{
856588cad61Seric 		define('g', (char *) NULL, e);
857145b49b1Seric 		return (0);
858c579ef51Seric 	}
859145b49b1Seric 
8605dfc646bSeric 	/* print out messages as full list */
86163780dbdSeric 	e->e_to = tobuf + 1;
8625dfc646bSeric 
8635dfc646bSeric 	/*
8645dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8655dfc646bSeric 	*/
8665dfc646bSeric 
867c579ef51Seric 	while (!clever && *++mvp != NULL)
8685dfc646bSeric 	{
869588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8705dfc646bSeric 		*pvp++ = newstr(buf);
8715dfc646bSeric 		if (pvp >= &pv[MAXPV])
87208b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8735dfc646bSeric 	}
8745dfc646bSeric 	*pvp++ = NULL;
8755dfc646bSeric 
87625a99e2eSeric 	/*
87725a99e2eSeric 	**  Call the mailer.
8786328bdf7Seric 	**	The argument vector gets built, pipes
87925a99e2eSeric 	**	are created as necessary, and we fork & exec as
8806328bdf7Seric 	**	appropriate.
881c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
88225a99e2eSeric 	*/
88325a99e2eSeric 
8847ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
88586b26461Seric 		ctladdr = &e->e_from;
886134746fbSeric #ifdef NAMED_BIND
8872bcc6d2dSeric 	if (ConfigLevel < 2)
888912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
889134746fbSeric #endif
8909c9e68d9Seric 
8919c9e68d9Seric 	if (tTd(11, 1))
892134746fbSeric 	{
8939c9e68d9Seric 		printf("openmailer:");
8949c9e68d9Seric 		printav(pv);
8959c9e68d9Seric 	}
8969c9e68d9Seric 	errno = 0;
8979c9e68d9Seric 
8989c9e68d9Seric 	CurHostName = m->m_mailer;
8999c9e68d9Seric 
9009c9e68d9Seric 	/*
9019c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9029c9e68d9Seric 	**  connection.
9039c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9049c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9059c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9069c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9079c9e68d9Seric 	*/
9089c9e68d9Seric 
9099c9e68d9Seric 	curhost = NULL;
910c931b82bSeric 	SmtpPhase = NULL;
9119c9e68d9Seric 
9127febfd66Seric #ifdef XDEBUG
9137febfd66Seric 	{
9147febfd66Seric 		char wbuf[MAXLINE];
9157febfd66Seric 
9167febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9177febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
9187febfd66Seric 		checkfd012(wbuf);
9197febfd66Seric 	}
9207febfd66Seric #endif
9217febfd66Seric 
9227febfd66Seric 
9239c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9249c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9259c9e68d9Seric 	{
9269c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9279c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9289c9e68d9Seric 		mci->mci_in = stdin;
9299c9e68d9Seric 		mci->mci_out = stdout;
9309c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9319c9e68d9Seric 		mci->mci_mailer = m;
9329c9e68d9Seric 	}
9339c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9349c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9359c9e68d9Seric 	{
9369c9e68d9Seric #ifdef DAEMON
9379c9e68d9Seric 		register int i;
9389c9e68d9Seric 		register u_short port;
9399c9e68d9Seric 
9409c9e68d9Seric 		CurHostName = pv[1];
9419c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9429c9e68d9Seric 
9439c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9449c9e68d9Seric 		{
9459c9e68d9Seric 			syserr("null signature");
946845e533cSeric 			rcode = EX_OSERR;
947b31e7f2bSeric 			goto give_up;
948b31e7f2bSeric 		}
9499c9e68d9Seric 
9509c9e68d9Seric 		if (!clever)
9519c9e68d9Seric 		{
9529c9e68d9Seric 			syserr("554 non-clever IPC");
9539c9e68d9Seric 			rcode = EX_OSERR;
9549c9e68d9Seric 			goto give_up;
9559c9e68d9Seric 		}
9569c9e68d9Seric 		if (pv[2] != NULL)
9579c9e68d9Seric 			port = atoi(pv[2]);
9589c9e68d9Seric 		else
9599c9e68d9Seric 			port = 0;
9609c9e68d9Seric tryhost:
9619c9e68d9Seric 		mci = NULL;
9629c9e68d9Seric 		while (*curhost != '\0')
9639c9e68d9Seric 		{
9649c9e68d9Seric 			register char *p;
9659c9e68d9Seric 			static char hostbuf[MAXNAME];
9669c9e68d9Seric 
9679c9e68d9Seric 			mci = NULL;
9689c9e68d9Seric 
9699c9e68d9Seric 			/* pull the next host from the signature */
9709c9e68d9Seric 			p = strchr(curhost, ':');
9719c9e68d9Seric 			if (p == NULL)
9729c9e68d9Seric 				p = &curhost[strlen(curhost)];
9739c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9749c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9759c9e68d9Seric 			if (*p != '\0')
9769c9e68d9Seric 				p++;
9779c9e68d9Seric 			curhost = p;
9789c9e68d9Seric 
9799c9e68d9Seric 			/* see if we already know that this host is fried */
9809c9e68d9Seric 			CurHostName = hostbuf;
9819c9e68d9Seric 			mci = mci_get(hostbuf, m);
9829c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9839c9e68d9Seric 			{
9849c9e68d9Seric 				if (tTd(11, 1))
9859c9e68d9Seric 				{
9869c9e68d9Seric 					printf("openmailer: ");
9879c9e68d9Seric 					mci_dump(mci);
9889c9e68d9Seric 				}
9899c9e68d9Seric 				CurHostName = mci->mci_host;
9909c9e68d9Seric 				break;
9919c9e68d9Seric 			}
9929c9e68d9Seric 			mci->mci_mailer = m;
9939c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9949c9e68d9Seric 				continue;
9959c9e68d9Seric 
9969c9e68d9Seric 			/* try the connection */
9979c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9989c9e68d9Seric 			message("Connecting to %s (%s)...",
9999c9e68d9Seric 				hostbuf, m->m_name);
10009c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
10019c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
10029c9e68d9Seric 			mci->mci_exitstat = i;
10039c9e68d9Seric 			mci->mci_errno = errno;
1004f170942cSeric #ifdef NAMED_BIND
1005f170942cSeric 			mci->mci_herrno = h_errno;
1006f170942cSeric #endif
10079c9e68d9Seric 			if (i == EX_OK)
10089c9e68d9Seric 			{
10099c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10109c9e68d9Seric 				mci_cache(mci);
1011f170942cSeric 				if (TrafficLogFile != NULL)
1012f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1013f170942cSeric 						getpid(), hostbuf);
10149c9e68d9Seric 				break;
10159c9e68d9Seric 			}
10169c9e68d9Seric 			else if (tTd(11, 1))
10179c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10189c9e68d9Seric 					i, errno);
10199c9e68d9Seric 
10209c9e68d9Seric 
10219c9e68d9Seric 			/* enter status of this host */
10229c9e68d9Seric 			setstat(i);
10239c9e68d9Seric 		}
10249c9e68d9Seric 		mci->mci_pid = 0;
10259c9e68d9Seric #else /* no DAEMON */
10269c9e68d9Seric 		syserr("554 openmailer: no IPC");
10279c9e68d9Seric 		if (tTd(11, 1))
10289c9e68d9Seric 			printf("openmailer: NULL\n");
10299c9e68d9Seric 		return NULL;
10309c9e68d9Seric #endif /* DAEMON */
10319c9e68d9Seric 	}
10329c9e68d9Seric 	else
10339c9e68d9Seric 	{
1034f170942cSeric 		if (TrafficLogFile != NULL)
10350e3bfef5Seric 		{
1036f170942cSeric 			char **av;
1037f170942cSeric 
1038f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1039f170942cSeric 			for (av = pv; *av != NULL; av++)
1040f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1041f170942cSeric 			fprintf(TrafficLogFile, "\n");
10426fe8c3bcSeric 		}
10436fe8c3bcSeric 
10449c9e68d9Seric 		/* create a pipe to shove the mail through */
10459c9e68d9Seric 		if (pipe(mpvect) < 0)
10469c9e68d9Seric 		{
10470e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10480e3bfef5Seric 				e->e_to, m->m_name);
10499c9e68d9Seric 			if (tTd(11, 1))
10509c9e68d9Seric 				printf("openmailer: NULL\n");
10519c9e68d9Seric 			rcode = EX_OSERR;
10529c9e68d9Seric 			goto give_up;
10539c9e68d9Seric 		}
10549c9e68d9Seric 
10559c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10569c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10579c9e68d9Seric 		{
10580e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10590e3bfef5Seric 				e->e_to, m->m_name);
10609c9e68d9Seric 			(void) close(mpvect[0]);
10619c9e68d9Seric 			(void) close(mpvect[1]);
10629c9e68d9Seric 			if (tTd(11, 1))
10639c9e68d9Seric 				printf("openmailer: NULL\n");
10649c9e68d9Seric 			rcode = EX_OSERR;
10659c9e68d9Seric 			goto give_up;
10669c9e68d9Seric 		}
10679c9e68d9Seric 
10689c9e68d9Seric 		/*
10699c9e68d9Seric 		**  Actually fork the mailer process.
10709c9e68d9Seric 		**	DOFORK is clever about retrying.
10719c9e68d9Seric 		**
10729c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10739c9e68d9Seric 		**	around so that endmail will get it.
10749c9e68d9Seric 		*/
10759c9e68d9Seric 
10769c9e68d9Seric 		if (e->e_xfp != NULL)
10779c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10789c9e68d9Seric 		(void) fflush(stdout);
10799c9e68d9Seric # ifdef SIGCHLD
10802b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
10819c9e68d9Seric # endif /* SIGCHLD */
10829c9e68d9Seric 		DOFORK(FORK);
10839c9e68d9Seric 		/* pid is set by DOFORK */
10849c9e68d9Seric 		if (pid < 0)
10859c9e68d9Seric 		{
10869c9e68d9Seric 			/* failure */
10870e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
10880e3bfef5Seric 				e->e_to, m->m_name);
10899c9e68d9Seric 			(void) close(mpvect[0]);
10909c9e68d9Seric 			(void) close(mpvect[1]);
10919c9e68d9Seric 			if (clever)
10929c9e68d9Seric 			{
10939c9e68d9Seric 				(void) close(rpvect[0]);
10949c9e68d9Seric 				(void) close(rpvect[1]);
10959c9e68d9Seric 			}
10969c9e68d9Seric 			if (tTd(11, 1))
10979c9e68d9Seric 				printf("openmailer: NULL\n");
10989c9e68d9Seric 			rcode = EX_OSERR;
10999c9e68d9Seric 			goto give_up;
11009c9e68d9Seric 		}
11019c9e68d9Seric 		else if (pid == 0)
11029c9e68d9Seric 		{
11039c9e68d9Seric 			int i;
11049c9e68d9Seric 			int saveerrno;
11059c9e68d9Seric 			char **ep;
11069c9e68d9Seric 			char *env[MAXUSERENVIRON];
11079c9e68d9Seric 			extern char **environ;
11089c9e68d9Seric 			extern int DtableSize;
11099c9e68d9Seric 
11109c9e68d9Seric 			/* child -- set up input & exec mailer */
11112b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
11122b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
11132b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11149c9e68d9Seric 
11159c9e68d9Seric 			/* close any other cached connections */
11169c9e68d9Seric 			mci_flush(FALSE, mci);
11179c9e68d9Seric 
111844f2317fSeric 			/* reset user and group */
111944f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
112044f2317fSeric 			{
112144f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
112244f2317fSeric 				{
112344f2317fSeric 					(void) initgroups(DefUser, DefGid);
112444f2317fSeric 					(void) setuid(DefUid);
112544f2317fSeric 				}
112644f2317fSeric 				else
112744f2317fSeric 				{
112844f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
112944f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
113044f2317fSeric 						ctladdr->q_gid);
113144f2317fSeric 					(void) setuid(ctladdr->q_uid);
113244f2317fSeric 				}
113344f2317fSeric 			}
113444f2317fSeric 
113544f2317fSeric 			if (tTd(11, 2))
113644f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
113744f2317fSeric 					getuid(), geteuid());
113844f2317fSeric 
1139b986f6aaSeric 			/* move into some "safe" directory */
1140b986f6aaSeric 			if (m->m_execdir != NULL)
1141b986f6aaSeric 			{
1142b986f6aaSeric 				char *p, *q;
1143b986f6aaSeric 				char buf[MAXLINE];
1144b986f6aaSeric 
1145b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1146b986f6aaSeric 				{
1147b986f6aaSeric 					q = strchr(p, ':');
1148b986f6aaSeric 					if (q != NULL)
1149b986f6aaSeric 						*q = '\0';
1150b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1151b986f6aaSeric 					if (q != NULL)
1152b986f6aaSeric 						*q++ = ':';
1153b986f6aaSeric 					if (tTd(11, 20))
1154b986f6aaSeric 						printf("openmailer: trydir %s\n",
1155b986f6aaSeric 							buf);
1156b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1157b986f6aaSeric 						break;
1158b986f6aaSeric 				}
1159b986f6aaSeric 			}
1160b986f6aaSeric 
11619c9e68d9Seric 			/* arrange to filter std & diag output of command */
11629c9e68d9Seric 			if (clever)
11639c9e68d9Seric 			{
11649c9e68d9Seric 				(void) close(rpvect[0]);
11656fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11666fe8c3bcSeric 				{
11670e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11680e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11696fe8c3bcSeric 					_exit(EX_OSERR);
11706fe8c3bcSeric 				}
11719c9e68d9Seric 				(void) close(rpvect[1]);
11729c9e68d9Seric 			}
11739c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11749c9e68d9Seric 			{
11759c9e68d9Seric 				/* put mailer output in transcript */
11766fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11776fe8c3bcSeric 				{
11780e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11790e3bfef5Seric 						e->e_to, m->m_name,
11806fe8c3bcSeric 						fileno(e->e_xfp));
11816fe8c3bcSeric 					_exit(EX_OSERR);
11829c9e68d9Seric 				}
11836fe8c3bcSeric 			}
11846fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11856fe8c3bcSeric 			{
11860e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
11870e3bfef5Seric 					e->e_to, m->m_name);
11886fe8c3bcSeric 				_exit(EX_OSERR);
11896fe8c3bcSeric 			}
11909c9e68d9Seric 
11919c9e68d9Seric 			/* arrange to get standard input */
11929c9e68d9Seric 			(void) close(mpvect[1]);
11939c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11949c9e68d9Seric 			{
11950e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
11960e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
11979c9e68d9Seric 				_exit(EX_OSERR);
11989c9e68d9Seric 			}
11999c9e68d9Seric 			(void) close(mpvect[0]);
12009c9e68d9Seric 
12019c9e68d9Seric 			/* arrange for all the files to be closed */
12029c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12039c9e68d9Seric 			{
12049c9e68d9Seric 				register int j;
120544f2317fSeric 
12069c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12079c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
12089c9e68d9Seric 			}
12099c9e68d9Seric 
12109c9e68d9Seric 			/* set up the mailer environment */
12119c9e68d9Seric 			i = 0;
12129c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12139c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12149c9e68d9Seric 			{
12159c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12169c9e68d9Seric 					env[i++] = *ep;
12179c9e68d9Seric 			}
12189c9e68d9Seric 			env[i++] = NULL;
12199c9e68d9Seric 
12209c9e68d9Seric 			/* try to execute the mailer */
12219c9e68d9Seric 			execve(m->m_mailer, pv, env);
12229c9e68d9Seric 			saveerrno = errno;
12239c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12247c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12257c941fd2Seric 				_exit(EX_OSERR);
12269c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12279c9e68d9Seric 		}
12289c9e68d9Seric 
12299c9e68d9Seric 		/*
12309c9e68d9Seric 		**  Set up return value.
12319c9e68d9Seric 		*/
12329c9e68d9Seric 
12339c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12349c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12359c9e68d9Seric 		mci->mci_mailer = m;
12369c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12379c9e68d9Seric 		mci->mci_pid = pid;
12389c9e68d9Seric 		(void) close(mpvect[0]);
12399c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
12409c9e68d9Seric 		if (clever)
12419c9e68d9Seric 		{
12429c9e68d9Seric 			(void) close(rpvect[1]);
12439c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
12449c9e68d9Seric 		}
12459c9e68d9Seric 		else
12469c9e68d9Seric 		{
12479c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12489c9e68d9Seric 			mci->mci_in = NULL;
12499c9e68d9Seric 		}
12509c9e68d9Seric 	}
12519c9e68d9Seric 
12529c9e68d9Seric 	/*
12539c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12549c9e68d9Seric 	*/
12559c9e68d9Seric 
12569c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12579c9e68d9Seric 	{
12589c9e68d9Seric 		smtpinit(m, mci, e);
12599c9e68d9Seric 	}
12609c9e68d9Seric 	if (tTd(11, 1))
12619c9e68d9Seric 	{
12629c9e68d9Seric 		printf("openmailer: ");
12639c9e68d9Seric 		mci_dump(mci);
12649c9e68d9Seric 	}
12659c9e68d9Seric 
12669c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1267b31e7f2bSeric 	{
1268b31e7f2bSeric 		/* couldn't open the mailer */
1269b31e7f2bSeric 		rcode = mci->mci_exitstat;
12702a6bc25bSeric 		errno = mci->mci_errno;
1271f170942cSeric #ifdef NAMED_BIND
1272f170942cSeric 		h_errno = mci->mci_herrno;
1273f170942cSeric #endif
1274b31e7f2bSeric 		if (rcode == EX_OK)
1275b31e7f2bSeric 		{
1276b31e7f2bSeric 			/* shouldn't happen */
127708b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12786b0f339dSeric 				rcode, mci->mci_state, firstsig);
1279b31e7f2bSeric 			rcode = EX_SOFTWARE;
1280b31e7f2bSeric 		}
1281e9277e33Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
128290891494Seric 		{
128390891494Seric 			/* try next MX site */
128490891494Seric 			goto tryhost;
128590891494Seric 		}
1286b31e7f2bSeric 	}
1287b31e7f2bSeric 	else if (!clever)
1288b31e7f2bSeric 	{
1289b31e7f2bSeric 		/*
1290b31e7f2bSeric 		**  Format and send message.
1291b31e7f2bSeric 		*/
129215d084d5Seric 
1293b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1294b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1295b31e7f2bSeric 		putline("\n", mci->mci_out, m);
129603c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1297b31e7f2bSeric 
1298b31e7f2bSeric 		/* get the exit status */
1299c9be6216Seric 		rcode = endmailer(mci, e, pv);
1300134746fbSeric 	}
1301134746fbSeric 	else
1302b31e7f2bSeric #ifdef SMTP
1303134746fbSeric 	{
1304b31e7f2bSeric 		/*
1305b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1306b31e7f2bSeric 		*/
130715d084d5Seric 
1308b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1309b31e7f2bSeric 		if (rcode == EX_OK)
131075889e88Seric 		{
1311ded0d3daSkarels 			register char *t = tobuf;
1312ded0d3daSkarels 			register int i;
1313ded0d3daSkarels 
1314588cad61Seric 			/* send the recipient list */
131563780dbdSeric 			tobuf[0] = '\0';
131675889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
131775889e88Seric 			{
131863780dbdSeric 				e->e_to = to->q_paddr;
131915d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
132075889e88Seric 				{
132183b7ddc9Seric 					markfailure(e, to, i);
132281161401Seric 					giveresponse(i, m, mci, e);
132363780dbdSeric 				}
132475889e88Seric 				else
132575889e88Seric 				{
1326911693bfSbostic 					*t++ = ',';
1327b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1328b31e7f2bSeric 						continue;
1329ee39df61Seric 					*t = '\0';
1330588cad61Seric 				}
1331588cad61Seric 			}
1332588cad61Seric 
133363780dbdSeric 			/* now send the data */
133463780dbdSeric 			if (tobuf[0] == '\0')
1335b31e7f2bSeric 			{
13369c9e68d9Seric 				rcode = EX_OK;
133763780dbdSeric 				e->e_to = NULL;
1338b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1339b31e7f2bSeric 					smtprset(m, mci, e);
1340b31e7f2bSeric 			}
134175889e88Seric 			else
134275889e88Seric 			{
134363780dbdSeric 				e->e_to = tobuf + 1;
134475889e88Seric 				rcode = smtpdata(m, mci, e);
134563780dbdSeric 			}
134663780dbdSeric 
134763780dbdSeric 			/* now close the connection */
1348b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
134915d084d5Seric 				smtpquit(m, mci, e);
135063780dbdSeric 		}
13519c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13529c9e68d9Seric 		{
13539c9e68d9Seric 			/* try next MX site */
13549c9e68d9Seric 			goto tryhost;
13559c9e68d9Seric 		}
1356c579ef51Seric 	}
1357b31e7f2bSeric #else /* not SMTP */
1358a05b3449Sbostic 	{
135908b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1360845e533cSeric 		rcode = EX_CONFIG;
1361b31e7f2bSeric 		goto give_up;
1362a05b3449Sbostic 	}
1363b31e7f2bSeric #endif /* SMTP */
1364134746fbSeric #ifdef NAMED_BIND
13652bcc6d2dSeric 	if (ConfigLevel < 2)
1366912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1367134746fbSeric #endif
13685dfc646bSeric 
1369b31e7f2bSeric 	/* arrange a return receipt if requested */
13708c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1371b31e7f2bSeric 	{
1372b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1373b31e7f2bSeric 		/* do we want to send back more info? */
1374b31e7f2bSeric 	}
1375b31e7f2bSeric 
1376c77d1c25Seric 	/*
137763780dbdSeric 	**  Do final status disposal.
137863780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1379c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1380c77d1c25Seric 	**		addressees.
1381c77d1c25Seric 	*/
1382c77d1c25Seric 
1383b31e7f2bSeric   give_up:
138463780dbdSeric 	if (tobuf[0] != '\0')
138581161401Seric 		giveresponse(rcode, m, mci, e);
1386772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1387b31e7f2bSeric 	{
1388dde5acadSeric 		if (rcode != EX_OK)
138983b7ddc9Seric 			markfailure(e, to, rcode);
1390dde5acadSeric 		else
1391655518ecSeric 		{
1392dde5acadSeric 			to->q_flags |= QSENT;
1393655518ecSeric 			e->e_nsent++;
1394655518ecSeric 		}
1395b31e7f2bSeric 	}
1396b31e7f2bSeric 
1397b31e7f2bSeric 	/*
1398b31e7f2bSeric 	**  Restore state and return.
1399b31e7f2bSeric 	*/
1400c77d1c25Seric 
14017febfd66Seric #ifdef XDEBUG
14027febfd66Seric 	{
14037febfd66Seric 		char wbuf[MAXLINE];
14047febfd66Seric 
14057febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
140644622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
140744622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
140844622ea3Seric 			m->m_name);
14097febfd66Seric 		checkfd012(wbuf);
14107febfd66Seric 	}
14117febfd66Seric #endif
14127febfd66Seric 
141335490626Seric 	errno = 0;
1414588cad61Seric 	define('g', (char *) NULL, e);
14155826d9d3Seric 	return (rcode);
141625a99e2eSeric }
14175dfc646bSeric /*
141883b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
141983b7ddc9Seric **
142083b7ddc9Seric **	Parameters:
142183b7ddc9Seric **		e -- the envelope we are sending.
142283b7ddc9Seric **		q -- the address to mark.
142383b7ddc9Seric **		rcode -- the code signifying the particular failure.
142483b7ddc9Seric **
142583b7ddc9Seric **	Returns:
142683b7ddc9Seric **		none.
142783b7ddc9Seric **
142883b7ddc9Seric **	Side Effects:
142983b7ddc9Seric **		marks the address (and possibly the envelope) with the
143083b7ddc9Seric **			failure so that an error will be returned or
143183b7ddc9Seric **			the message will be queued, as appropriate.
143283b7ddc9Seric */
143383b7ddc9Seric 
143483b7ddc9Seric markfailure(e, q, rcode)
143583b7ddc9Seric 	register ENVELOPE *e;
143683b7ddc9Seric 	register ADDRESS *q;
143783b7ddc9Seric 	int rcode;
143883b7ddc9Seric {
143919c47125Seric 	char buf[MAXLINE];
144019c47125Seric 
144183b7ddc9Seric 	if (rcode == EX_OK)
144283b7ddc9Seric 		return;
1443f170942cSeric 	else if (rcode == EX_TEMPFAIL)
144483b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1445f170942cSeric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
1446f170942cSeric 		q->q_flags |= QBADADDR;
144783b7ddc9Seric }
144883b7ddc9Seric /*
1449c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1450c579ef51Seric **
1451c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1452c579ef51Seric **	violation), so we report those specially.  For other
1453c579ef51Seric **	errors, we choose a status message (into statmsg),
1454c579ef51Seric **	and if it represents an error, we print it.
1455c579ef51Seric **
1456c579ef51Seric **	Parameters:
1457c579ef51Seric **		pid -- pid of mailer.
1458c9be6216Seric **		e -- the current envelope.
1459c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1460c9be6216Seric **			(for error messages).
1461c579ef51Seric **
1462c579ef51Seric **	Returns:
1463c579ef51Seric **		exit code of mailer.
1464c579ef51Seric **
1465c579ef51Seric **	Side Effects:
1466c579ef51Seric **		none.
1467c579ef51Seric */
1468c579ef51Seric 
1469c9be6216Seric endmailer(mci, e, pv)
1470b31e7f2bSeric 	register MCI *mci;
1471c9be6216Seric 	register ENVELOPE *e;
1472c9be6216Seric 	char **pv;
1473c579ef51Seric {
1474588cad61Seric 	int st;
1475c579ef51Seric 
147675889e88Seric 	/* close any connections */
147775889e88Seric 	if (mci->mci_in != NULL)
1478c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
147975889e88Seric 	if (mci->mci_out != NULL)
1480c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
148175889e88Seric 	mci->mci_in = mci->mci_out = NULL;
148275889e88Seric 	mci->mci_state = MCIS_CLOSED;
148375889e88Seric 
148433db8731Seric 	/* in the IPC case there is nothing to wait for */
148575889e88Seric 	if (mci->mci_pid == 0)
148633db8731Seric 		return (EX_OK);
148733db8731Seric 
148833db8731Seric 	/* wait for the mailer process to die and collect status */
148975889e88Seric 	st = waitfor(mci->mci_pid);
1490588cad61Seric 	if (st == -1)
149178de67c1Seric 	{
1492c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1493588cad61Seric 		return (EX_SOFTWARE);
1494c579ef51Seric 	}
149533db8731Seric 
1496bf9bc890Seric 	if (WIFEXITED(st))
1497c579ef51Seric 	{
1498bf9bc890Seric 		/* normal death -- return status */
1499bf9bc890Seric 		return (WEXITSTATUS(st));
1500bf9bc890Seric 	}
1501bf9bc890Seric 
1502bf9bc890Seric 	/* it died a horrid death */
1503c9be6216Seric 	syserr("mailer %s died with signal %o", pv[0], st);
1504c9be6216Seric 
1505c9be6216Seric 	/* log the arguments */
1506c9be6216Seric 	if (e->e_xfp != NULL)
1507c9be6216Seric 	{
1508c9be6216Seric 		register char **av;
1509c9be6216Seric 
1510c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1511c9be6216Seric 		for (av = pv; *av != NULL; av++)
1512c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1513c9be6216Seric 		fprintf(e->e_xfp, "\n");
1514c9be6216Seric 	}
1515c9be6216Seric 
15165f73204aSeric 	ExitStat = EX_TEMPFAIL;
15175f73204aSeric 	return (EX_TEMPFAIL);
1518c579ef51Seric }
1519c579ef51Seric /*
152025a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
152125a99e2eSeric **
152225a99e2eSeric **	Parameters:
152325a99e2eSeric **		stat -- the status code from the mailer (high byte
152425a99e2eSeric **			only; core dumps must have been taken care of
152525a99e2eSeric **			already).
152681161401Seric **		m -- the mailer info for this mailer.
152781161401Seric **		mci -- the mailer connection info -- can be NULL if the
152881161401Seric **			response is given before the connection is made.
152981161401Seric **		e -- the current envelope.
153025a99e2eSeric **
153125a99e2eSeric **	Returns:
1532db8841e9Seric **		none.
153325a99e2eSeric **
153425a99e2eSeric **	Side Effects:
1535c1f9df2cSeric **		Errors may be incremented.
153625a99e2eSeric **		ExitStat may be set.
153725a99e2eSeric */
153825a99e2eSeric 
153981161401Seric giveresponse(stat, m, mci, e)
154025a99e2eSeric 	int stat;
1541588cad61Seric 	register MAILER *m;
154281161401Seric 	register MCI *mci;
1543198d9be0Seric 	ENVELOPE *e;
154425a99e2eSeric {
15459c16475dSeric 	register const char *statmsg;
154625a99e2eSeric 	extern char *SysExMsg[];
154725a99e2eSeric 	register int i;
1548d4bd8f0eSbostic 	extern int N_SysEx;
1549198d9be0Seric 	char buf[MAXLINE];
155025a99e2eSeric 
155113bbc08cSeric 	/*
155213bbc08cSeric 	**  Compute status message from code.
155313bbc08cSeric 	*/
155413bbc08cSeric 
155525a99e2eSeric 	i = stat - EX__BASE;
1556588cad61Seric 	if (stat == 0)
15576fe8c3bcSeric 	{
1558588cad61Seric 		statmsg = "250 Sent";
1559ce5531bdSeric 		if (e->e_statmsg != NULL)
15606fe8c3bcSeric 		{
1561ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15626fe8c3bcSeric 			statmsg = buf;
15636fe8c3bcSeric 		}
15646fe8c3bcSeric 	}
1565588cad61Seric 	else if (i < 0 || i > N_SysEx)
1566588cad61Seric 	{
1567588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1568588cad61Seric 		stat = EX_UNAVAILABLE;
1569588cad61Seric 		statmsg = buf;
1570588cad61Seric 	}
1571198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1572198d9be0Seric 	{
15737d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1574d4bd8f0eSbostic #ifdef NAMED_BIND
1575f28da541Smiriam 		if (h_errno == TRY_AGAIN)
15764d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1577f28da541Smiriam 		else
1578d4bd8f0eSbostic #endif
1579f28da541Smiriam 		{
15808557d168Seric 			if (errno != 0)
1581d87e85f3Seric 				statmsg = errstring(errno);
1582d87e85f3Seric 			else
1583d87e85f3Seric 			{
1584d87e85f3Seric #ifdef SMTP
1585d87e85f3Seric 				extern char SmtpError[];
1586d87e85f3Seric 
1587d87e85f3Seric 				statmsg = SmtpError;
15886c2c3107Seric #else /* SMTP */
1589d87e85f3Seric 				statmsg = NULL;
15906c2c3107Seric #endif /* SMTP */
1591d87e85f3Seric 			}
1592f28da541Smiriam 		}
1593d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1594d87e85f3Seric 		{
159587c9b3e7Seric 			(void) strcat(buf, ": ");
1596d87e85f3Seric 			(void) strcat(buf, statmsg);
15978557d168Seric 		}
1598198d9be0Seric 		statmsg = buf;
1599198d9be0Seric 	}
1600f170942cSeric #ifdef NAMED_BIND
1601f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1602f170942cSeric 	{
16034d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1604f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1605f170942cSeric 		statmsg = buf;
1606f170942cSeric 	}
1607f170942cSeric #endif
160825a99e2eSeric 	else
1609d87e85f3Seric 	{
161025a99e2eSeric 		statmsg = SysExMsg[i];
16117d55540cSeric 		if (*statmsg++ == ':')
16127d55540cSeric 		{
16137d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16147d55540cSeric 			statmsg = buf;
16157d55540cSeric 		}
1616d87e85f3Seric 	}
1617588cad61Seric 
1618588cad61Seric 	/*
1619588cad61Seric 	**  Print the message as appropriate
1620588cad61Seric 	*/
1621588cad61Seric 
1622198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1623e12d03eeSeric 		message(&statmsg[4], errstring(errno));
162425a99e2eSeric 	else
162525a99e2eSeric 	{
1626c1f9df2cSeric 		Errors++;
1627e12d03eeSeric 		usrerr(statmsg, errstring(errno));
162825a99e2eSeric 	}
162925a99e2eSeric 
163025a99e2eSeric 	/*
163125a99e2eSeric 	**  Final cleanup.
163225a99e2eSeric 	**	Log a record of the transaction.  Compute the new
163325a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
163425a99e2eSeric 	**	that.
163525a99e2eSeric 	*/
163625a99e2eSeric 
16372f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
163881161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1639eb238f8cSeric 
1640eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1641eb238f8cSeric 		setstat(stat);
1642198d9be0Seric 	if (stat != EX_OK)
1643198d9be0Seric 	{
1644198d9be0Seric 		if (e->e_message != NULL)
1645198d9be0Seric 			free(e->e_message);
1646198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1647198d9be0Seric 	}
16488557d168Seric 	errno = 0;
1649d4bd8f0eSbostic #ifdef NAMED_BIND
1650f28da541Smiriam 	h_errno = 0;
1651d4bd8f0eSbostic #endif
1652eb238f8cSeric }
1653eb238f8cSeric /*
1654eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1655eb238f8cSeric **
1656eb238f8cSeric **	Parameters:
165781161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
165881161401Seric **		mci -- the mailer connection info -- can be NULL if the
165981161401Seric **			log is occuring when no connection is active.
166081161401Seric **		stat -- the message to print for the status.
166181161401Seric **		e -- the current envelope.
1662eb238f8cSeric **
1663eb238f8cSeric **	Returns:
1664eb238f8cSeric **		none
1665eb238f8cSeric **
1666eb238f8cSeric **	Side Effects:
1667eb238f8cSeric **		none
1668eb238f8cSeric */
1669eb238f8cSeric 
167081161401Seric logdelivery(m, mci, stat, e)
167181161401Seric 	MAILER *m;
167281161401Seric 	register MCI *mci;
1673eb238f8cSeric 	char *stat;
1674b31e7f2bSeric 	register ENVELOPE *e;
16755cf56be3Seric {
1676eb238f8cSeric # ifdef LOG
1677d6acf3eeSeric 	char buf[512];
16789507d1f9Seric 
167948ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
168081161401Seric 
168148ed5d33Seric 	if (m != NULL)
168271ff6caaSeric 	{
168348ed5d33Seric 		(void) strcat(buf, ", mailer=");
168448ed5d33Seric 		(void) strcat(buf, m->m_name);
168571ff6caaSeric 	}
168648ed5d33Seric 
168748ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
168871ff6caaSeric 	{
168971ff6caaSeric # ifdef DAEMON
1690e2f2f828Seric 		extern SOCKADDR CurHostAddr;
169148ed5d33Seric # endif
169271ff6caaSeric 
169348ed5d33Seric 		(void) strcat(buf, ", relay=");
169448ed5d33Seric 		(void) strcat(buf, mci->mci_host);
169548ed5d33Seric 
169648ed5d33Seric # ifdef DAEMON
169748ed5d33Seric 		(void) strcat(buf, " (");
1698e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
169948ed5d33Seric 		(void) strcat(buf, ")");
170071ff6caaSeric # endif
170171ff6caaSeric 	}
17029507d1f9Seric 	else
170348ed5d33Seric 	{
170448ed5d33Seric 		char *p = macvalue('h', e);
17059507d1f9Seric 
170648ed5d33Seric 		if (p != NULL && p[0] != '\0')
170748ed5d33Seric 		{
170848ed5d33Seric 			(void) strcat(buf, ", relay=");
170948ed5d33Seric 			(void) strcat(buf, p);
171048ed5d33Seric 		}
171148ed5d33Seric 	}
1712d6acf3eeSeric 
1713d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1714d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
17156c2c3107Seric # endif /* LOG */
171625a99e2eSeric }
171725a99e2eSeric /*
171851552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
171925a99e2eSeric **
172051552439Seric **	This can be made an arbitrary message separator by changing $l
172151552439Seric **
17229b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
17239b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
17249b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
17259b6c17a6Seric **	this kind of antique garbage????
172625a99e2eSeric **
172725a99e2eSeric **	Parameters:
172851552439Seric **		fp -- the file to output to.
172951552439Seric **		m -- the mailer describing this entry.
173025a99e2eSeric **
173125a99e2eSeric **	Returns:
173251552439Seric **		none
173325a99e2eSeric **
173425a99e2eSeric **	Side Effects:
173551552439Seric **		outputs some text to fp.
173625a99e2eSeric */
173725a99e2eSeric 
1738b31e7f2bSeric putfromline(fp, m, e)
173951552439Seric 	register FILE *fp;
174051552439Seric 	register MAILER *m;
1741b31e7f2bSeric 	ENVELOPE *e;
174225a99e2eSeric {
17432bc47524Seric 	char *template = "\201l\n";
174451552439Seric 	char buf[MAXLINE];
174525a99e2eSeric 
174657fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
174751552439Seric 		return;
174813bbc08cSeric 
17492c7e1b8dSeric # ifdef UGLYUUCP
175057fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
175174b6e67bSeric 	{
1752ea09d6edSeric 		char *bang;
1753ea09d6edSeric 		char xbuf[MAXLINE];
175474b6e67bSeric 
1755ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17566c2c3107Seric 		bang = strchr(buf, '!');
175774b6e67bSeric 		if (bang == NULL)
175834fcca25Seric 		{
175934fcca25Seric 			errno = 0;
176034fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
176134fcca25Seric 		}
176274b6e67bSeric 		else
1763588cad61Seric 		{
1764ea09d6edSeric 			*bang++ = '\0';
17652bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1766ea09d6edSeric 			template = xbuf;
176774b6e67bSeric 		}
1768588cad61Seric 	}
17696c2c3107Seric # endif /* UGLYUUCP */
1770b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
177177b52738Seric 	putline(buf, fp, m);
1772bc6e2962Seric }
1773bc6e2962Seric /*
177451552439Seric **  PUTBODY -- put the body of a message.
177551552439Seric **
177651552439Seric **	Parameters:
177751552439Seric **		fp -- file to output onto.
177877b52738Seric **		m -- a mailer descriptor to control output format.
17799a6a5f55Seric **		e -- the envelope to put out.
178003c02fdeSeric **		separator -- if non-NULL, a message separator that must
178103c02fdeSeric **			not be permitted in the resulting message.
178251552439Seric **
178351552439Seric **	Returns:
178451552439Seric **		none.
178551552439Seric **
178651552439Seric **	Side Effects:
178751552439Seric **		The message is written onto fp.
178851552439Seric */
178951552439Seric 
179003c02fdeSeric putbody(fp, m, e, separator)
179151552439Seric 	FILE *fp;
1792588cad61Seric 	MAILER *m;
17939a6a5f55Seric 	register ENVELOPE *e;
179403c02fdeSeric 	char *separator;
179551552439Seric {
179677b52738Seric 	char buf[MAXLINE];
179751552439Seric 
179851552439Seric 	/*
179951552439Seric 	**  Output the body of the message
180051552439Seric 	*/
180151552439Seric 
18029a6a5f55Seric 	if (e->e_dfp == NULL)
180351552439Seric 	{
18049a6a5f55Seric 		if (e->e_df != NULL)
18059a6a5f55Seric 		{
18069a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
18079a6a5f55Seric 			if (e->e_dfp == NULL)
18088f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
1809e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
18109a6a5f55Seric 		}
18119a6a5f55Seric 		else
181277b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
18139a6a5f55Seric 	}
18149a6a5f55Seric 	if (e->e_dfp != NULL)
18159a6a5f55Seric 	{
18169a6a5f55Seric 		rewind(e->e_dfp);
181777b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
181824fc8aeeSeric 		{
181924fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1820d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
18213462ad9eSeric 				(void) putc('>', fp);
182203c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
182303c02fdeSeric 			{
182403c02fdeSeric 				/* possible separator */
182503c02fdeSeric 				int sl = strlen(separator);
182603c02fdeSeric 
182703c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
182803c02fdeSeric 					(void) putc(' ', fp);
182903c02fdeSeric 			}
183077b52738Seric 			putline(buf, fp, m);
183124fc8aeeSeric 		}
183251552439Seric 
18339a6a5f55Seric 		if (ferror(e->e_dfp))
183451552439Seric 		{
183551552439Seric 			syserr("putbody: read error");
183651552439Seric 			ExitStat = EX_IOERR;
183751552439Seric 		}
183851552439Seric 	}
183951552439Seric 
18400890ba1fSeric 	/* some mailers want extra blank line at end of message */
18410890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
18420890ba1fSeric 		putline("", fp, m);
18430890ba1fSeric 
184451552439Seric 	(void) fflush(fp);
184551552439Seric 	if (ferror(fp) && errno != EPIPE)
184651552439Seric 	{
184751552439Seric 		syserr("putbody: write error");
184851552439Seric 		ExitStat = EX_IOERR;
184951552439Seric 	}
185051552439Seric 	errno = 0;
185125a99e2eSeric }
185225a99e2eSeric /*
185325a99e2eSeric **  MAILFILE -- Send a message to a file.
185425a99e2eSeric **
1855f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1856f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1857f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1858f129ec7dSeric **	sendmail runs as root.
1859f129ec7dSeric **
1860588cad61Seric **	This could be done as a subordinate mailer, except that it
1861588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1862588cad61Seric **	view this as being sufficiently important as to include it
1863588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1864588cad61Seric **	to create another process plus some pipes to save the message.
1865588cad61Seric **
186625a99e2eSeric **	Parameters:
186725a99e2eSeric **		filename -- the name of the file to send to.
18686259796dSeric **		ctladdr -- the controlling address header -- includes
18696259796dSeric **			the userid/groupid to be when sending.
187025a99e2eSeric **
187125a99e2eSeric **	Returns:
187225a99e2eSeric **		The exit code associated with the operation.
187325a99e2eSeric **
187425a99e2eSeric **	Side Effects:
187525a99e2eSeric **		none.
187625a99e2eSeric */
187725a99e2eSeric 
1878b31e7f2bSeric mailfile(filename, ctladdr, e)
187925a99e2eSeric 	char *filename;
18806259796dSeric 	ADDRESS *ctladdr;
1881b31e7f2bSeric 	register ENVELOPE *e;
188225a99e2eSeric {
188325a99e2eSeric 	register FILE *f;
188432d19d43Seric 	register int pid;
188515d084d5Seric 	int mode;
188625a99e2eSeric 
1887671745f3Seric 	if (tTd(11, 1))
1888671745f3Seric 	{
1889671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
1890671745f3Seric 		printaddr(ctladdr, FALSE);
1891671745f3Seric 	}
1892671745f3Seric 
1893f170942cSeric 	if (e->e_xfp != NULL)
1894f170942cSeric 		fflush(e->e_xfp);
1895f170942cSeric 
189632d19d43Seric 	/*
189732d19d43Seric 	**  Fork so we can change permissions here.
189832d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
189932d19d43Seric 	**	the complications of calling subroutines, etc.
190032d19d43Seric 	*/
190132d19d43Seric 
190232d19d43Seric 	DOFORK(fork);
190332d19d43Seric 
190432d19d43Seric 	if (pid < 0)
190532d19d43Seric 		return (EX_OSERR);
190632d19d43Seric 	else if (pid == 0)
190732d19d43Seric 	{
190832d19d43Seric 		/* child -- actually write to file */
1909f129ec7dSeric 		struct stat stb;
1910f129ec7dSeric 
19112b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
19122b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
19132b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
19143462ad9eSeric 		(void) umask(OldUmask);
191595f16dc0Seric 
1916f129ec7dSeric 		if (stat(filename, &stb) < 0)
19173a98e7eaSeric 			stb.st_mode = FileMode;
191815d084d5Seric 		mode = stb.st_mode;
191995f16dc0Seric 
192095f16dc0Seric 		/* limit the errors to those actually caused in the child */
192195f16dc0Seric 		errno = 0;
192295f16dc0Seric 		ExitStat = EX_OK;
192395f16dc0Seric 
1924f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1925f129ec7dSeric 			exit(EX_CANTCREAT);
192603827b5fSeric 		if (ctladdr == NULL)
19278f9146b0Srick 			ctladdr = &e->e_from;
192815d084d5Seric 		else
192915d084d5Seric 		{
193015d084d5Seric 			/* ignore setuid and setgid bits */
193115d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
193215d084d5Seric 		}
193315d084d5Seric 
19348f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
19358f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
19368f9146b0Srick 		{
19378f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
193895f16dc0Seric 			if (e->e_dfp == NULL)
193995f16dc0Seric 			{
19408f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
1941e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
19428f9146b0Srick 			}
19438f9146b0Srick 		}
19448f9146b0Srick 
194515d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1946e36b99e2Seric 		{
194795f16dc0Seric 			if (ctladdr->q_uid == 0)
194895f16dc0Seric 			{
1949898a126bSbostic 				(void) initgroups(DefUser, DefGid);
195095f16dc0Seric 			}
195195f16dc0Seric 			else
195295f16dc0Seric 			{
1953898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1954898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1955898a126bSbostic 					ctladdr->q_gid);
1956898a126bSbostic 			}
1957e36b99e2Seric 		}
195815d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1959e36b99e2Seric 		{
1960e36b99e2Seric 			if (ctladdr->q_uid == 0)
1961e36b99e2Seric 				(void) setuid(DefUid);
1962e36b99e2Seric 			else
19636259796dSeric 				(void) setuid(ctladdr->q_uid);
1964e36b99e2Seric 		}
196595f16dc0Seric 		FileName = filename;
196695f16dc0Seric 		LineNumber = 0;
19673a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
196825a99e2eSeric 		if (f == NULL)
196995f16dc0Seric 		{
1970b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
197132d19d43Seric 			exit(EX_CANTCREAT);
197295f16dc0Seric 		}
197325a99e2eSeric 
19740331ce05Seric 		putfromline(f, FileMailer, e);
19750331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
19760331ce05Seric 		putline("\n", f, FileMailer);
197703c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
19780331ce05Seric 		putline("\n", f, FileMailer);
197995f16dc0Seric 		if (ferror(f))
198095f16dc0Seric 		{
1981b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
198295f16dc0Seric 			setstat(EX_IOERR);
198395f16dc0Seric 		}
1984ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
198532d19d43Seric 		(void) fflush(stdout);
1986e36b99e2Seric 
198727628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1988c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
198995f16dc0Seric 		exit(ExitStat);
199013bbc08cSeric 		/*NOTREACHED*/
199132d19d43Seric 	}
199232d19d43Seric 	else
199332d19d43Seric 	{
199432d19d43Seric 		/* parent -- wait for exit status */
1995588cad61Seric 		int st;
199632d19d43Seric 
1997588cad61Seric 		st = waitfor(pid);
1998bf9bc890Seric 		if (WIFEXITED(st))
1999bf9bc890Seric 			return (WEXITSTATUS(st));
2000588cad61Seric 		else
2001b6a0de9dSeric 		{
2002b6a0de9dSeric 			syserr("child died on signal %d", st);
2003bf9bc890Seric 			return (EX_UNAVAILABLE);
2004b6a0de9dSeric 		}
20058f9146b0Srick 		/*NOTREACHED*/
200632d19d43Seric 	}
200725a99e2eSeric }
2008ea4dc939Seric /*
2009e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2010e103b48fSeric **
2011e103b48fSeric **	The signature describes how we are going to send this -- it
2012e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2013e103b48fSeric **	an ordered list of MX hosts.
2014e103b48fSeric **
2015e103b48fSeric **	Parameters:
2016e103b48fSeric **		m -- the mailer describing this host.
2017e103b48fSeric **		host -- the host name.
2018e103b48fSeric **		e -- the current envelope.
2019e103b48fSeric **
2020e103b48fSeric **	Returns:
2021e103b48fSeric **		The signature for this host.
2022e103b48fSeric **
2023e103b48fSeric **	Side Effects:
2024e103b48fSeric **		Can tweak the symbol table.
2025e103b48fSeric */
2026e103b48fSeric 
2027e103b48fSeric char *
2028e103b48fSeric hostsignature(m, host, e)
2029e103b48fSeric 	register MAILER *m;
2030e103b48fSeric 	char *host;
2031e103b48fSeric 	ENVELOPE *e;
2032e103b48fSeric {
2033e103b48fSeric 	register char *p;
2034e103b48fSeric 	register STAB *s;
2035e103b48fSeric 	int i;
2036e103b48fSeric 	int len;
2037e103b48fSeric #ifdef NAMED_BIND
2038e103b48fSeric 	int nmx;
2039e103b48fSeric 	auto int rcode;
2040bafdc4e5Seric 	char *hp;
2041bafdc4e5Seric 	char *endp;
2042516782b4Seric 	int oldoptions;
2043e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2044e103b48fSeric #endif
2045e103b48fSeric 
2046e103b48fSeric 	/*
2047e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2048e103b48fSeric 	*/
2049e103b48fSeric 
2050e103b48fSeric 	p = m->m_mailer;
2051e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2052e103b48fSeric 	{
2053e103b48fSeric 		/* just an ordinary mailer */
2054e103b48fSeric 		return host;
2055e103b48fSeric 	}
2056e103b48fSeric 
2057e103b48fSeric 	/*
2058e103b48fSeric 	**  Look it up in the symbol table.
2059e103b48fSeric 	*/
2060e103b48fSeric 
2061e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2062e103b48fSeric 	if (s->s_hostsig != NULL)
2063e103b48fSeric 		return s->s_hostsig;
2064e103b48fSeric 
2065e103b48fSeric 	/*
2066e103b48fSeric 	**  Not already there -- create a signature.
2067e103b48fSeric 	*/
2068e103b48fSeric 
2069e103b48fSeric #ifdef NAMED_BIND
2070516782b4Seric 	if (ConfigLevel < 2)
2071516782b4Seric 	{
2072516782b4Seric 		oldoptions = _res.options;
2073516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2074516782b4Seric 	}
2075516782b4Seric 
2076bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2077bafdc4e5Seric 	{
2078bafdc4e5Seric 		endp = strchr(hp, ':');
2079bafdc4e5Seric 		if (endp != NULL)
2080bafdc4e5Seric 			*endp = '\0';
2081bafdc4e5Seric 
20827bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
20837d55540cSeric 
2084e103b48fSeric 		if (nmx <= 0)
2085e103b48fSeric 		{
2086e103b48fSeric 			register MCI *mci;
2087e103b48fSeric 			extern int errno;
2088e103b48fSeric 
2089e103b48fSeric 			/* update the connection info for this host */
2090bafdc4e5Seric 			mci = mci_get(hp, m);
2091e103b48fSeric 			mci->mci_exitstat = rcode;
2092e103b48fSeric 			mci->mci_errno = errno;
2093f170942cSeric #ifdef NAMED_BIND
2094f170942cSeric 			mci->mci_herrno = h_errno;
2095f170942cSeric #endif
2096e103b48fSeric 
2097e103b48fSeric 			/* and return the original host name as the signature */
2098bafdc4e5Seric 			nmx = 1;
2099bafdc4e5Seric 			mxhosts[0] = hp;
2100e103b48fSeric 		}
2101e103b48fSeric 
2102e103b48fSeric 		len = 0;
2103e103b48fSeric 		for (i = 0; i < nmx; i++)
2104e103b48fSeric 		{
2105e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2106e103b48fSeric 		}
2107bafdc4e5Seric 		if (s->s_hostsig != NULL)
2108bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2109bafdc4e5Seric 		p = xalloc(len);
2110bafdc4e5Seric 		if (s->s_hostsig != NULL)
2111bafdc4e5Seric 		{
2112bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2113bafdc4e5Seric 			free(s->s_hostsig);
2114bafdc4e5Seric 			s->s_hostsig = p;
2115bafdc4e5Seric 			p += strlen(p);
2116bafdc4e5Seric 			*p++ = ':';
2117bafdc4e5Seric 		}
2118bafdc4e5Seric 		else
2119bafdc4e5Seric 			s->s_hostsig = p;
2120e103b48fSeric 		for (i = 0; i < nmx; i++)
2121e103b48fSeric 		{
2122e103b48fSeric 			if (i != 0)
2123e103b48fSeric 				*p++ = ':';
2124e103b48fSeric 			strcpy(p, mxhosts[i]);
2125e103b48fSeric 			p += strlen(p);
2126e103b48fSeric 		}
2127bafdc4e5Seric 		if (endp != NULL)
2128bafdc4e5Seric 			*endp++ = ':';
2129bafdc4e5Seric 	}
2130e103b48fSeric 	makelower(s->s_hostsig);
2131516782b4Seric 	if (ConfigLevel < 2)
2132516782b4Seric 		_res.options = oldoptions;
2133e103b48fSeric #else
2134e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2135e103b48fSeric 	s->s_hostsig = host;
2136e103b48fSeric #endif
2137e103b48fSeric 	if (tTd(17, 1))
2138e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2139e103b48fSeric 	return s->s_hostsig;
2140e103b48fSeric }
2141