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*4d50702aSeric static char sccsid[] = "@(#)deliver.c	8.7 (Berkeley) 07/22/93";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14a8c080f0Seric #include <signal.h>
15f28da541Smiriam #include <netdb.h>
16911693bfSbostic #include <errno.h>
17134746fbSeric #ifdef NAMED_BIND
18912a731aSbostic #include <arpa/nameser.h>
19912a731aSbostic #include <resolv.h>
20f170942cSeric 
21f170942cSeric extern int	h_errno;
22134746fbSeric #endif
2325a99e2eSeric 
2425a99e2eSeric /*
259c9e68d9Seric **  SENDALL -- actually send all the messages.
269c9e68d9Seric **
279c9e68d9Seric **	Parameters:
289c9e68d9Seric **		e -- the envelope to send.
299c9e68d9Seric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
309c9e68d9Seric **			the current e->e_sendmode.
319c9e68d9Seric **
329c9e68d9Seric **	Returns:
339c9e68d9Seric **		none.
349c9e68d9Seric **
359c9e68d9Seric **	Side Effects:
369c9e68d9Seric **		Scans the send lists and sends everything it finds.
379c9e68d9Seric **		Delivers any appropriate error messages.
389c9e68d9Seric **		If we are running in a non-interactive mode, takes the
399c9e68d9Seric **			appropriate action.
409c9e68d9Seric */
419c9e68d9Seric 
429c9e68d9Seric sendall(e, mode)
439c9e68d9Seric 	ENVELOPE *e;
449c9e68d9Seric 	char mode;
459c9e68d9Seric {
469c9e68d9Seric 	register ADDRESS *q;
479c9e68d9Seric 	char *owner;
489c9e68d9Seric 	int otherowners;
499c9e68d9Seric 	register ENVELOPE *ee;
509c9e68d9Seric 	ENVELOPE *splitenv = NULL;
516103d9b4Seric 	bool announcequeueup;
529c9e68d9Seric 
537880f3e4Seric 	/*
547880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
557880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
567880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
577880f3e4Seric 	**  addresses to be sent.
587880f3e4Seric 	*/
597880f3e4Seric 
607880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
61896b16f6Seric 	{
62896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
63896b16f6Seric 		return;
64896b16f6Seric 	}
65896b16f6Seric 
669c9e68d9Seric 	/* determine actual delivery mode */
679c9e68d9Seric 	if (mode == SM_DEFAULT)
689c9e68d9Seric 	{
699c9e68d9Seric 		mode = e->e_sendmode;
709c9e68d9Seric 		if (mode != SM_VERIFY &&
719c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
729c9e68d9Seric 			mode = SM_QUEUE;
736103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
749c9e68d9Seric 	}
756103d9b4Seric 	else
766103d9b4Seric 		announcequeueup = FALSE;
779c9e68d9Seric 
789c9e68d9Seric 	if (tTd(13, 1))
799c9e68d9Seric 	{
809c9e68d9Seric 		printf("\nSENDALL: mode %c, e_from ", mode);
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;
979c9e68d9Seric 		syserr("554 too many hops %d (%d max): from %s, to %s",
989c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
999c9e68d9Seric 			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 
112b8004690Seric 	if (!MeToo && !bitset(QQUEUEUP, e->e_from.q_flags))
1139c9e68d9Seric 	{
1149c9e68d9Seric 		if (tTd(13, 5))
1159c9e68d9Seric 		{
1169c9e68d9Seric 			printf("sendall: QDONTSEND ");
1179c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1189c9e68d9Seric 		}
1199c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1209c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1219c9e68d9Seric 	}
1229c9e68d9Seric 
123ce5531bdSeric 	/*
124ce5531bdSeric 	**  Handle alias owners.
125ce5531bdSeric 	**
126ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
127ce5531bdSeric 	**	We discard owners that are the same as the return path.
128ce5531bdSeric 	*/
129ce5531bdSeric 
130ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
131ce5531bdSeric 	{
132ce5531bdSeric 		register struct address *a;
133ce5531bdSeric 
134ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
135ce5531bdSeric 			continue;
136ce5531bdSeric 		if (a != NULL)
137ce5531bdSeric 			q->q_owner = a->q_owner;
138ce5531bdSeric 
139ce5531bdSeric 		if (q->q_owner != NULL &&
140ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
141ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
142ce5531bdSeric 			q->q_owner = NULL;
143ce5531bdSeric 	}
144ce5531bdSeric 
145ce5531bdSeric 	owner = "";
146ce5531bdSeric 	otherowners = 1;
147ce5531bdSeric 	while (owner != NULL && otherowners > 0)
148ce5531bdSeric 	{
149ce5531bdSeric 		owner = NULL;
150ce5531bdSeric 		otherowners = 0;
151ce5531bdSeric 
152ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
153ce5531bdSeric 		{
154ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
155ce5531bdSeric 				continue;
156ce5531bdSeric 
157ce5531bdSeric 			if (q->q_owner != NULL)
158ce5531bdSeric 			{
159ce5531bdSeric 				if (owner == NULL)
160ce5531bdSeric 					owner = q->q_owner;
161ce5531bdSeric 				else if (owner != q->q_owner)
162ce5531bdSeric 				{
163ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
164ce5531bdSeric 					{
165ce5531bdSeric 						/* make future comparisons cheap */
166ce5531bdSeric 						q->q_owner = owner;
167ce5531bdSeric 					}
168ce5531bdSeric 					else
169ce5531bdSeric 					{
170ce5531bdSeric 						otherowners++;
171ce5531bdSeric 					}
172ce5531bdSeric 					owner = q->q_owner;
173ce5531bdSeric 				}
174ce5531bdSeric 			}
175ce5531bdSeric 			else
176ce5531bdSeric 			{
177ce5531bdSeric 				otherowners++;
178ce5531bdSeric 			}
179ce5531bdSeric 		}
180ce5531bdSeric 
181ce5531bdSeric 		if (owner != NULL && otherowners > 0)
182ce5531bdSeric 		{
183ce5531bdSeric 			extern HDR *copyheader();
184ce5531bdSeric 			extern ADDRESS *copyqueue();
185ce5531bdSeric 
186ce5531bdSeric 			/*
187ce5531bdSeric 			**  Split this envelope into two.
188ce5531bdSeric 			*/
189ce5531bdSeric 
190ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
191ce5531bdSeric 			*ee = *e;
192ce5531bdSeric 			ee->e_id = NULL;
193ce5531bdSeric 			(void) queuename(ee, '\0');
194ce5531bdSeric 
195ce5531bdSeric 			if (tTd(13, 1))
196ce5531bdSeric 				printf("sendall: split %s into %s\n",
197ce5531bdSeric 					e->e_id, ee->e_id);
198ce5531bdSeric 
199ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
200ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
201ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
202ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
203ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
204ce5531bdSeric 			if (tTd(13, 5))
205ce5531bdSeric 			{
206ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
207ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
208ce5531bdSeric 			}
209ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
210ce5531bdSeric 			ee->e_dfp = NULL;
211ce5531bdSeric 			ee->e_xfp = NULL;
212ce5531bdSeric 			ee->e_lockfp = NULL;
213ce5531bdSeric 			ee->e_df = NULL;
214ce5531bdSeric 			ee->e_errormode = EM_MAIL;
215ce5531bdSeric 			ee->e_sibling = splitenv;
216ce5531bdSeric 			splitenv = ee;
217ce5531bdSeric 
218ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
219ce5531bdSeric 				if (q->q_owner == owner)
220ce5531bdSeric 					q->q_flags |= QDONTSEND;
221ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
222ce5531bdSeric 				if (q->q_owner != owner)
223ce5531bdSeric 					q->q_flags |= QDONTSEND;
224ce5531bdSeric 
225ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
226ce5531bdSeric 			{
227ce5531bdSeric 				ee->e_dfp = NULL;
228ce5531bdSeric 				ee->e_df = newstr(queuename(ee, 'd'));
229ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
230ce5531bdSeric 				{
231ce5531bdSeric 					syserr("sendall: link(%s, %s)",
232ce5531bdSeric 						e->e_df, ee->e_df);
233ce5531bdSeric 				}
234ce5531bdSeric 			}
235ce5531bdSeric 
236ce5531bdSeric 			if (mode != SM_VERIFY)
237ce5531bdSeric 				openxscript(ee);
238ce5531bdSeric #ifdef LOG
239ce5531bdSeric 			if (LogLevel > 4)
240ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
241ce5531bdSeric 					ee->e_id, e->e_id);
242ce5531bdSeric #endif
243ce5531bdSeric 		}
244ce5531bdSeric 	}
245ce5531bdSeric 
246ce5531bdSeric 	if (owner != NULL)
247ce5531bdSeric 	{
248ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
249ce5531bdSeric 		if (tTd(13, 5))
250ce5531bdSeric 		{
251ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
252ce5531bdSeric 			printaddr(&e->e_from, FALSE);
253ce5531bdSeric 		}
254ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
255ce5531bdSeric 		e->e_errormode = EM_MAIL;
256ce5531bdSeric 	}
257ce5531bdSeric 
258c1ac89b1Seric # ifdef QUEUE
259c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
260c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
261c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
262c1ac89b1Seric 	{
263c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2646103d9b4Seric 		queueup(e, TRUE, announcequeueup);
265ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2666103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
267c1ac89b1Seric 	}
268c1ac89b1Seric #endif /* QUEUE */
269c1ac89b1Seric 
270ce5531bdSeric 	if (splitenv != NULL)
271ce5531bdSeric 	{
272ce5531bdSeric 		if (tTd(13, 1))
273ce5531bdSeric 		{
274ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
275ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
276ce5531bdSeric 		}
277ce5531bdSeric 
278ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
279ce5531bdSeric 		{
280ce5531bdSeric 			CurEnv = ee;
281ce5531bdSeric 			sendenvelope(ee, mode);
282ce5531bdSeric 		}
283ce5531bdSeric 
284ce5531bdSeric 		CurEnv = e;
285ce5531bdSeric 	}
286ce5531bdSeric 	sendenvelope(e, mode);
287ce5531bdSeric 
288ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
289ce5531bdSeric 		dropenvelope(splitenv);
290ce5531bdSeric }
291ce5531bdSeric 
292ce5531bdSeric sendenvelope(e, mode)
293ce5531bdSeric 	register ENVELOPE *e;
294ce5531bdSeric 	char mode;
295ce5531bdSeric {
296ce5531bdSeric 	bool oldverbose;
297ce5531bdSeric 	int pid;
298ce5531bdSeric 	register ADDRESS *q;
299ce5531bdSeric #ifdef LOCKF
300ce5531bdSeric 	struct flock lfd;
301ce5531bdSeric #endif
302ce5531bdSeric 
3037880f3e4Seric 	/*
3047880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3057880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3067880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3077880f3e4Seric 	**  addresses to be sent.
3087880f3e4Seric 	*/
3097880f3e4Seric 
3107880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
3117880f3e4Seric 	{
3127880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3137880f3e4Seric 		return;
3147880f3e4Seric 	}
3157880f3e4Seric 
316ce5531bdSeric 	oldverbose = Verbose;
317c1ac89b1Seric 	switch (mode)
318c1ac89b1Seric 	{
319c1ac89b1Seric 	  case SM_VERIFY:
320c1ac89b1Seric 		Verbose = TRUE;
321c1ac89b1Seric 		break;
322c1ac89b1Seric 
323c1ac89b1Seric 	  case SM_QUEUE:
324c1ac89b1Seric   queueonly:
325c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
326c1ac89b1Seric 		return;
327c1ac89b1Seric 
328c1ac89b1Seric 	  case SM_FORK:
329c1ac89b1Seric 		if (e->e_xfp != NULL)
330c1ac89b1Seric 			(void) fflush(e->e_xfp);
331c1ac89b1Seric 
332c1ac89b1Seric # ifdef LOCKF
333c1ac89b1Seric 		/*
334c1ac89b1Seric 		**  Since lockf has the interesting semantic that the
335c1ac89b1Seric 		**  lock is lost when we fork, we have to risk losing
336c1ac89b1Seric 		**  the lock here by closing before the fork, and then
337c1ac89b1Seric 		**  trying to get it back in the child.
338c1ac89b1Seric 		*/
339c1ac89b1Seric 
340c1ac89b1Seric 		if (e->e_lockfp != NULL)
341c1ac89b1Seric 		{
342c1ac89b1Seric 			(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
343c1ac89b1Seric 			e->e_lockfp = NULL;
344c1ac89b1Seric 		}
345c1ac89b1Seric # endif /* LOCKF */
346c1ac89b1Seric 
347c1ac89b1Seric 		pid = fork();
348c1ac89b1Seric 		if (pid < 0)
349c1ac89b1Seric 		{
350c1ac89b1Seric 			goto queueonly;
351c1ac89b1Seric 		}
352c1ac89b1Seric 		else if (pid > 0)
353c1ac89b1Seric 		{
354c1ac89b1Seric 			/* be sure we leave the temp files to our child */
355c1ac89b1Seric 			e->e_id = e->e_df = NULL;
356c1ac89b1Seric # ifndef LOCKF
357c1ac89b1Seric 			if (e->e_lockfp != NULL)
358c1ac89b1Seric 			{
359c1ac89b1Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
360c1ac89b1Seric 				e->e_lockfp = NULL;
361c1ac89b1Seric 			}
362c1ac89b1Seric # endif
363c1ac89b1Seric 
364c1ac89b1Seric 			/* close any random open files in the envelope */
365c1ac89b1Seric 			if (e->e_dfp != NULL)
366c1ac89b1Seric 			{
367c1ac89b1Seric 				(void) xfclose(e->e_dfp, "sendenvelope", "dfp");
368c1ac89b1Seric 				e->e_dfp = NULL;
369c1ac89b1Seric 			}
370c1ac89b1Seric 			if (e->e_xfp != NULL)
371c1ac89b1Seric 			{
372c1ac89b1Seric 				(void) xfclose(e->e_xfp, "sendenvelope", "xfp");
373c1ac89b1Seric 				e->e_xfp = NULL;
374c1ac89b1Seric 			}
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 # ifdef LOCKF
386c1ac89b1Seric 		/*
387c1ac89b1Seric 		**  Now try to get our lock back.
388c1ac89b1Seric 		*/
389c1ac89b1Seric 
390c1ac89b1Seric 		lfd.l_type = F_WRLCK;
391c1ac89b1Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
392c1ac89b1Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
393c1ac89b1Seric 		if (e->e_lockfp == NULL ||
394c1ac89b1Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
395c1ac89b1Seric 		{
396c1ac89b1Seric 			/* oops....  lost it */
397c1ac89b1Seric 			if (tTd(13, 1))
398c1ac89b1Seric 				printf("sendenvelope: %s lost lock: lockfp=%x, %s\n",
399c1ac89b1Seric 					e->e_id, e->e_lockfp, errstring(errno));
400c1ac89b1Seric 
401c1ac89b1Seric # ifdef LOG
402c1ac89b1Seric 			if (LogLevel > 29)
403c1ac89b1Seric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
404c1ac89b1Seric 					e->e_id);
405c1ac89b1Seric # endif /* LOG */
406c1ac89b1Seric 			exit(EX_OK);
407c1ac89b1Seric 		}
408c1ac89b1Seric # endif /* LOCKF */
409c1ac89b1Seric 
410c1ac89b1Seric 		/*
411c1ac89b1Seric 		**  Close any cached connections.
412c1ac89b1Seric 		**
413c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
414c1ac89b1Seric 		**	still knows about the connection.
415c1ac89b1Seric 		**
416c1ac89b1Seric 		**	This should only happen when delivering an error
417c1ac89b1Seric 		**	message.
418c1ac89b1Seric 		*/
419c1ac89b1Seric 
420c1ac89b1Seric 		mci_flush(FALSE, NULL);
421c1ac89b1Seric 
422c1ac89b1Seric 		break;
423c1ac89b1Seric 	}
424c1ac89b1Seric 
425c1ac89b1Seric 	/*
4269c9e68d9Seric 	**  Run through the list and send everything.
4275288e21aSeric 	**
4285288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4295288e21aSeric 	**	result in returned mail.
4309c9e68d9Seric 	*/
4319c9e68d9Seric 
4329c9e68d9Seric 	e->e_nsent = 0;
4335288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
4349c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4359c9e68d9Seric 	{
4369c9e68d9Seric 		if (mode == SM_VERIFY)
4379c9e68d9Seric 		{
4389c9e68d9Seric 			e->e_to = q->q_paddr;
4399c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4408dfca105Seric 			{
4418dfca105Seric 				message("deliverable: mailer %s, host %s, user %s",
4428dfca105Seric 					q->q_mailer->m_name,
4438dfca105Seric 					q->q_host,
4448dfca105Seric 					q->q_user);
4458dfca105Seric 			}
4469c9e68d9Seric 		}
4479c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4489c9e68d9Seric 		{
4499c9e68d9Seric # ifdef QUEUE
4509c9e68d9Seric 			/*
4519c9e68d9Seric 			**  Checkpoint the send list every few addresses
4529c9e68d9Seric 			*/
4539c9e68d9Seric 
4549c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4559c9e68d9Seric 			{
4569c9e68d9Seric 				queueup(e, TRUE, FALSE);
4579c9e68d9Seric 				e->e_nsent = 0;
4589c9e68d9Seric 			}
4599c9e68d9Seric # endif /* QUEUE */
4609c9e68d9Seric 			(void) deliver(e, q);
4619c9e68d9Seric 		}
4629c9e68d9Seric 	}
4639c9e68d9Seric 	Verbose = oldverbose;
4649c9e68d9Seric 
4659c9e68d9Seric 	if (mode == SM_FORK)
4669c9e68d9Seric 		finis();
4679c9e68d9Seric }
4689c9e68d9Seric /*
4699c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4709c9e68d9Seric **
4719c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4729c9e68d9Seric **	two processes on the same stack!!!
4739c9e68d9Seric **
4749c9e68d9Seric **	Parameters:
4759c9e68d9Seric **		none.
4769c9e68d9Seric **
4779c9e68d9Seric **	Returns:
4789c9e68d9Seric **		From a macro???  You've got to be kidding!
4799c9e68d9Seric **
4809c9e68d9Seric **	Side Effects:
4819c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4829c9e68d9Seric **			pid of child in parent, zero in child.
4839c9e68d9Seric **			-1 on unrecoverable error.
4849c9e68d9Seric **
4859c9e68d9Seric **	Notes:
4869c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4879c9e68d9Seric **		vfork for you.....
4889c9e68d9Seric */
4899c9e68d9Seric 
4909c9e68d9Seric # define NFORKTRIES	5
4919c9e68d9Seric 
4929c9e68d9Seric # ifndef FORK
4939c9e68d9Seric # define FORK	fork
4949c9e68d9Seric # endif
4959c9e68d9Seric 
4969c9e68d9Seric # define DOFORK(fORKfN) \
4979c9e68d9Seric {\
4989c9e68d9Seric 	register int i;\
4999c9e68d9Seric \
5009c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
5019c9e68d9Seric 	{\
5029c9e68d9Seric 		pid = fORKfN();\
5039c9e68d9Seric 		if (pid >= 0)\
5049c9e68d9Seric 			break;\
5059c9e68d9Seric 		if (i > 0)\
5069c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5079c9e68d9Seric 	}\
5089c9e68d9Seric }
5099c9e68d9Seric /*
5109c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5119c9e68d9Seric **
5129c9e68d9Seric **	Parameters:
5139c9e68d9Seric **		none.
5149c9e68d9Seric **
5159c9e68d9Seric **	Returns:
5169c9e68d9Seric **		pid of child in parent.
5179c9e68d9Seric **		zero in child.
5189c9e68d9Seric **		-1 on error.
5199c9e68d9Seric **
5209c9e68d9Seric **	Side Effects:
5219c9e68d9Seric **		returns twice, once in parent and once in child.
5229c9e68d9Seric */
5239c9e68d9Seric 
5249c9e68d9Seric dofork()
5259c9e68d9Seric {
5269c9e68d9Seric 	register int pid;
5279c9e68d9Seric 
5289c9e68d9Seric 	DOFORK(fork);
5299c9e68d9Seric 	return (pid);
5309c9e68d9Seric }
5319c9e68d9Seric /*
53213bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
53313bbc08cSeric **
53413bbc08cSeric **	This routine delivers to everyone on the same host as the
53513bbc08cSeric **	user on the head of the list.  It is clever about mailers
53613bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
53713bbc08cSeric **	that it will deliver to all these addresses however -- so
53813bbc08cSeric **	deliver should be called once for each address on the
53913bbc08cSeric **	list.
54025a99e2eSeric **
54125a99e2eSeric **	Parameters:
542588cad61Seric **		e -- the envelope to deliver.
543c77d1c25Seric **		firstto -- head of the address list to deliver to.
54425a99e2eSeric **
54525a99e2eSeric **	Returns:
54625a99e2eSeric **		zero -- successfully delivered.
54725a99e2eSeric **		else -- some failure, see ExitStat for more info.
54825a99e2eSeric **
54925a99e2eSeric **	Side Effects:
55025a99e2eSeric **		The standard input is passed off to someone.
55125a99e2eSeric */
55225a99e2eSeric 
553588cad61Seric deliver(e, firstto)
554588cad61Seric 	register ENVELOPE *e;
555c77d1c25Seric 	ADDRESS *firstto;
55625a99e2eSeric {
55778442df3Seric 	char *host;			/* host being sent to */
55878442df3Seric 	char *user;			/* user being sent to */
55925a99e2eSeric 	char **pvp;
5605dfc646bSeric 	register char **mvp;
56125a99e2eSeric 	register char *p;
562588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5636259796dSeric 	ADDRESS *ctladdr;
564b31e7f2bSeric 	register MCI *mci;
565c77d1c25Seric 	register ADDRESS *to = firstto;
566c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
567772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
568911693bfSbostic 	int rcode;			/* response code */
569e103b48fSeric 	char *firstsig;			/* signature of firstto */
5709c9e68d9Seric 	int pid;
5719c9e68d9Seric 	char *curhost;
5729c9e68d9Seric 	int mpvect[2];
5739c9e68d9Seric 	int rpvect[2];
574ee6bf8dfSeric 	char *pv[MAXPV+1];
575579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
576ee6bf8dfSeric 	char buf[MAXNAME];
577c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
578fabb3bd4Seric 	extern int checkcompat();
5799c9e68d9Seric 	extern FILE *fdopen();
58025a99e2eSeric 
58135490626Seric 	errno = 0;
582ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5835dfc646bSeric 		return (0);
58425a99e2eSeric 
585134746fbSeric #ifdef NAMED_BIND
586912a731aSbostic 	/* unless interactive, try twice, over a minute */
587912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
588912a731aSbostic 		_res.retrans = 30;
589912a731aSbostic 		_res.retry = 2;
590912a731aSbostic 	}
591d4bd8f0eSbostic #endif
592912a731aSbostic 
59351552439Seric 	m = to->q_mailer;
59451552439Seric 	host = to->q_host;
595c9be6216Seric 	CurEnv = e;			/* just in case */
5964384d521Seric 	e->e_statmsg = NULL;
59751552439Seric 
5986ef48975Seric 	if (tTd(10, 1))
5995dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
60051552439Seric 			m->m_mno, host, to->q_user);
601f3dbc832Seric 
602f3dbc832Seric 	/*
603f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
604f3dbc832Seric 	**  connections now, just mark these addresses and return.
605f3dbc832Seric 	**	This is useful if we want to batch connections to
606f3dbc832Seric 	**	reduce load.  This will cause the messages to be
607f3dbc832Seric 	**	queued up, and a daemon will come along to send the
608f3dbc832Seric 	**	messages later.
609f3dbc832Seric 	**		This should be on a per-mailer basis.
610f3dbc832Seric 	*/
611f3dbc832Seric 
61219c47125Seric 	if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) &&
61319c47125Seric 	    bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
614f3dbc832Seric 	{
615f3dbc832Seric 		for (; to != NULL; to = to->q_next)
616f4560e80Seric 		{
617ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
618c6e18ac5Seric 			    to->q_mailer != m)
619f4560e80Seric 				continue;
620268a25e1Seric 			to->q_flags |= QQUEUEUP;
621588cad61Seric 			e->e_to = to->q_paddr;
62208b25121Seric 			message("queued");
6232f624c86Seric 			if (LogLevel > 8)
62481161401Seric 				logdelivery(m, NULL, "queued", e);
625f4560e80Seric 		}
626588cad61Seric 		e->e_to = NULL;
627f3dbc832Seric 		return (0);
628f3dbc832Seric 	}
629f3dbc832Seric 
63025a99e2eSeric 	/*
6315dfc646bSeric 	**  Do initial argv setup.
6325dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6335dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6345dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6355dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6365dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6373bea8136Seric 	**		The from address rewrite is expected to make
6383bea8136Seric 	**		the address relative to the other end.
6395dfc646bSeric 	*/
6405dfc646bSeric 
64178442df3Seric 	/* rewrite from address, using rewriting rules */
642efe54562Seric 	rcode = EX_OK;
643efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
644efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
645efe54562Seric 					   &rcode, e));
646ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
647588cad61Seric 	define('h', host, e);			/* to host */
6485dfc646bSeric 	Errors = 0;
6495dfc646bSeric 	pvp = pv;
6505dfc646bSeric 	*pvp++ = m->m_argv[0];
6515dfc646bSeric 
6525dfc646bSeric 	/* insert -f or -r flag as appropriate */
65357fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6545dfc646bSeric 	{
65557fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6565dfc646bSeric 			*pvp++ = "-f";
6575dfc646bSeric 		else
6585dfc646bSeric 			*pvp++ = "-r";
659c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6605dfc646bSeric 	}
6615dfc646bSeric 
6625dfc646bSeric 	/*
6635dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6645dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6655dfc646bSeric 	**  be one of these, and there are only a few more slots
6665dfc646bSeric 	**  in the pv after it.
6675dfc646bSeric 	*/
6685dfc646bSeric 
6695dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6705dfc646bSeric 	{
6712bc47524Seric 		/* can't use strchr here because of sign extension problems */
6722bc47524Seric 		while (*p != '\0')
6732bc47524Seric 		{
6742bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6752bc47524Seric 			{
6762bc47524Seric 				if (*p == 'u')
6775dfc646bSeric 					break;
6782bc47524Seric 			}
6792bc47524Seric 		}
6802bc47524Seric 
6812bc47524Seric 		if (*p != '\0')
6825dfc646bSeric 			break;
6835dfc646bSeric 
6845dfc646bSeric 		/* this entry is safe -- go ahead and process it */
685588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6865dfc646bSeric 		*pvp++ = newstr(buf);
6875dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6885dfc646bSeric 		{
68908b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6905dfc646bSeric 			return (-1);
6915dfc646bSeric 		}
6925dfc646bSeric 	}
693c579ef51Seric 
69433db8731Seric 	/*
69533db8731Seric 	**  If we have no substitution for the user name in the argument
69633db8731Seric 	**  list, we know that we must supply the names otherwise -- and
69733db8731Seric 	**  SMTP is the answer!!
69833db8731Seric 	*/
69933db8731Seric 
7005dfc646bSeric 	if (*mvp == NULL)
701c579ef51Seric 	{
702c579ef51Seric 		/* running SMTP */
7032c7e1b8dSeric # ifdef SMTP
704c579ef51Seric 		clever = TRUE;
705c579ef51Seric 		*pvp = NULL;
7066c2c3107Seric # else /* SMTP */
70733db8731Seric 		/* oops!  we don't implement SMTP */
70808b25121Seric 		syserr("554 SMTP style mailer");
7092c7e1b8dSeric 		return (EX_SOFTWARE);
7106c2c3107Seric # endif /* SMTP */
711c579ef51Seric 	}
7125dfc646bSeric 
7135dfc646bSeric 	/*
7145dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7155dfc646bSeric 	**  run through our address list and append all the addresses
7165dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7175dfc646bSeric 	**  always send another copy later.
7185dfc646bSeric 	*/
7195dfc646bSeric 
7205dfc646bSeric 	tobuf[0] = '\0';
721588cad61Seric 	e->e_to = tobuf;
7226259796dSeric 	ctladdr = NULL;
723e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7245dfc646bSeric 	for (; to != NULL; to = to->q_next)
7255dfc646bSeric 	{
7265dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
72757fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7285dfc646bSeric 			break;
7295dfc646bSeric 
7305dfc646bSeric 		/* if already sent or not for this host, don't send */
731ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
732e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
733e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7345dfc646bSeric 			continue;
7356259796dSeric 
7364b22ea87Seric 		/* avoid overflowing tobuf */
737aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7384b22ea87Seric 			break;
7394b22ea87Seric 
7406ef48975Seric 		if (tTd(10, 1))
741772e6e50Seric 		{
742772e6e50Seric 			printf("\nsend to ");
743772e6e50Seric 			printaddr(to, FALSE);
744772e6e50Seric 		}
745772e6e50Seric 
7466259796dSeric 		/* compute effective uid/gid when sending */
7477da1035fSeric 		if (to->q_mailer == ProgMailer)
7486259796dSeric 			ctladdr = getctladdr(to);
7496259796dSeric 
7505dfc646bSeric 		user = to->q_user;
751588cad61Seric 		e->e_to = to->q_paddr;
75275f1ade9Seric 		if (tTd(10, 5))
75375f1ade9Seric 		{
75475f1ade9Seric 			printf("deliver: QDONTSEND ");
75575f1ade9Seric 			printaddr(to, FALSE);
75675f1ade9Seric 		}
757ee4b0922Seric 		to->q_flags |= QDONTSEND;
7585dfc646bSeric 
7595dfc646bSeric 		/*
7605dfc646bSeric 		**  Check to see that these people are allowed to
7615dfc646bSeric 		**  talk to each other.
7622a6e0786Seric 		*/
7632a6e0786Seric 
76469582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
76569582d2fSeric 		{
76669582d2fSeric 			NoReturn = TRUE;
76708b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
76881161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
76969582d2fSeric 			continue;
77069582d2fSeric 		}
771fabb3bd4Seric 		rcode = checkcompat(to, e);
7721793c9c5Seric 		if (rcode != EX_OK)
7735dfc646bSeric 		{
7741000c37aSeric 			markfailure(e, to, rcode);
77581161401Seric 			giveresponse(rcode, m, NULL, e);
7765dfc646bSeric 			continue;
7775dfc646bSeric 		}
7782a6e0786Seric 
7792a6e0786Seric 		/*
7809ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7819ec9501bSeric 		**	about them.
78225a99e2eSeric 		*/
78325a99e2eSeric 
78457fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
78525a99e2eSeric 		{
7861d8f1806Seric 			stripquotes(user);
7871d8f1806Seric 			stripquotes(host);
78825a99e2eSeric 		}
78925a99e2eSeric 
790cdb828c5Seric 		/* hack attack -- delivermail compatibility */
791cdb828c5Seric 		if (m == ProgMailer && *user == '|')
792cdb828c5Seric 			user++;
793cdb828c5Seric 
79425a99e2eSeric 		/*
7953efaed6eSeric 		**  If an error message has already been given, don't
7963efaed6eSeric 		**	bother to send to this address.
7973efaed6eSeric 		**
7983efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7993efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
8003efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
8013efaed6eSeric 		*/
8023efaed6eSeric 
8036cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8043efaed6eSeric 			continue;
8053efaed6eSeric 
806f2fec898Seric 		/* save statistics.... */
807588cad61Seric 		markstats(e, to);
808f2fec898Seric 
8093efaed6eSeric 		/*
81025a99e2eSeric 		**  See if this user name is "special".
81125a99e2eSeric 		**	If the user name has a slash in it, assume that this
81251552439Seric 		**	is a file -- send it off without further ado.  Note
81351552439Seric 		**	that this type of addresses is not processed along
81451552439Seric 		**	with the others, so we fudge on the To person.
81525a99e2eSeric 		*/
81625a99e2eSeric 
8172c017f8dSeric 		if (m == FileMailer)
81825a99e2eSeric 		{
819b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
82081161401Seric 			giveresponse(rcode, m, NULL, e);
821dde5acadSeric 			if (rcode == EX_OK)
822dde5acadSeric 				to->q_flags |= QSENT;
8235dfc646bSeric 			continue;
82425a99e2eSeric 		}
82525a99e2eSeric 
82613bbc08cSeric 		/*
82713bbc08cSeric 		**  Address is verified -- add this user to mailer
82813bbc08cSeric 		**  argv, and add it to the print list of recipients.
82913bbc08cSeric 		*/
83013bbc08cSeric 
831508daeccSeric 		/* link together the chain of recipients */
832508daeccSeric 		to->q_tchain = tochain;
833508daeccSeric 		tochain = to;
834508daeccSeric 
8355dfc646bSeric 		/* create list of users for error messages */
836db8841e9Seric 		(void) strcat(tobuf, ",");
837db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
838588cad61Seric 		define('u', user, e);		/* to user */
839588cad61Seric 		define('z', to->q_home, e);	/* user's home */
8405dfc646bSeric 
841c579ef51Seric 		/*
842508daeccSeric 		**  Expand out this user into argument list.
843c579ef51Seric 		*/
844c579ef51Seric 
845508daeccSeric 		if (!clever)
846c579ef51Seric 		{
847588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8485dfc646bSeric 			*pvp++ = newstr(buf);
8495dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8505dfc646bSeric 			{
8515dfc646bSeric 				/* allow some space for trailing parms */
8525dfc646bSeric 				break;
8535dfc646bSeric 			}
8545dfc646bSeric 		}
855c579ef51Seric 	}
8565dfc646bSeric 
857145b49b1Seric 	/* see if any addresses still exist */
858145b49b1Seric 	if (tobuf[0] == '\0')
859c579ef51Seric 	{
860588cad61Seric 		define('g', (char *) NULL, e);
861145b49b1Seric 		return (0);
862c579ef51Seric 	}
863145b49b1Seric 
8645dfc646bSeric 	/* print out messages as full list */
86563780dbdSeric 	e->e_to = tobuf + 1;
8665dfc646bSeric 
8675dfc646bSeric 	/*
8685dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8695dfc646bSeric 	*/
8705dfc646bSeric 
871c579ef51Seric 	while (!clever && *++mvp != NULL)
8725dfc646bSeric 	{
873588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8745dfc646bSeric 		*pvp++ = newstr(buf);
8755dfc646bSeric 		if (pvp >= &pv[MAXPV])
87608b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8775dfc646bSeric 	}
8785dfc646bSeric 	*pvp++ = NULL;
8795dfc646bSeric 
88025a99e2eSeric 	/*
88125a99e2eSeric 	**  Call the mailer.
8826328bdf7Seric 	**	The argument vector gets built, pipes
88325a99e2eSeric 	**	are created as necessary, and we fork & exec as
8846328bdf7Seric 	**	appropriate.
885c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
88625a99e2eSeric 	*/
88725a99e2eSeric 
8887ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
88986b26461Seric 		ctladdr = &e->e_from;
890134746fbSeric #ifdef NAMED_BIND
8912bcc6d2dSeric 	if (ConfigLevel < 2)
892912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
893134746fbSeric #endif
8949c9e68d9Seric 
8959c9e68d9Seric 	if (tTd(11, 1))
896134746fbSeric 	{
8979c9e68d9Seric 		printf("openmailer:");
8989c9e68d9Seric 		printav(pv);
8999c9e68d9Seric 	}
9009c9e68d9Seric 	errno = 0;
9019c9e68d9Seric 
9029c9e68d9Seric 	CurHostName = m->m_mailer;
9039c9e68d9Seric 
9049c9e68d9Seric 	/*
9059c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9069c9e68d9Seric 	**  connection.
9079c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9089c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9099c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9109c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9119c9e68d9Seric 	*/
9129c9e68d9Seric 
9139c9e68d9Seric 	curhost = NULL;
9149c9e68d9Seric 
9159c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9169c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9179c9e68d9Seric 	{
9189c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9199c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9209c9e68d9Seric 		mci->mci_in = stdin;
9219c9e68d9Seric 		mci->mci_out = stdout;
9229c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9239c9e68d9Seric 		mci->mci_mailer = m;
9249c9e68d9Seric 	}
9259c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9269c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9279c9e68d9Seric 	{
9289c9e68d9Seric #ifdef DAEMON
9299c9e68d9Seric 		register int i;
9309c9e68d9Seric 		register u_short port;
9319c9e68d9Seric 
9329c9e68d9Seric 		CurHostName = pv[1];
9339c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9349c9e68d9Seric 
9359c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9369c9e68d9Seric 		{
9379c9e68d9Seric 			syserr("null signature");
938845e533cSeric 			rcode = EX_OSERR;
939b31e7f2bSeric 			goto give_up;
940b31e7f2bSeric 		}
9419c9e68d9Seric 
9429c9e68d9Seric 		if (!clever)
9439c9e68d9Seric 		{
9449c9e68d9Seric 			syserr("554 non-clever IPC");
9459c9e68d9Seric 			rcode = EX_OSERR;
9469c9e68d9Seric 			goto give_up;
9479c9e68d9Seric 		}
9489c9e68d9Seric 		if (pv[2] != NULL)
9499c9e68d9Seric 			port = atoi(pv[2]);
9509c9e68d9Seric 		else
9519c9e68d9Seric 			port = 0;
9529c9e68d9Seric tryhost:
9539c9e68d9Seric 		mci = NULL;
9549c9e68d9Seric 		while (*curhost != '\0')
9559c9e68d9Seric 		{
9569c9e68d9Seric 			register char *p;
9579c9e68d9Seric 			static char hostbuf[MAXNAME];
9589c9e68d9Seric 
9599c9e68d9Seric 			mci = NULL;
9609c9e68d9Seric 
9619c9e68d9Seric 			/* pull the next host from the signature */
9629c9e68d9Seric 			p = strchr(curhost, ':');
9639c9e68d9Seric 			if (p == NULL)
9649c9e68d9Seric 				p = &curhost[strlen(curhost)];
9659c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9669c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9679c9e68d9Seric 			if (*p != '\0')
9689c9e68d9Seric 				p++;
9699c9e68d9Seric 			curhost = p;
9709c9e68d9Seric 
9719c9e68d9Seric 			/* see if we already know that this host is fried */
9729c9e68d9Seric 			CurHostName = hostbuf;
9739c9e68d9Seric 			mci = mci_get(hostbuf, m);
9749c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9759c9e68d9Seric 			{
9769c9e68d9Seric 				if (tTd(11, 1))
9779c9e68d9Seric 				{
9789c9e68d9Seric 					printf("openmailer: ");
9799c9e68d9Seric 					mci_dump(mci);
9809c9e68d9Seric 				}
9819c9e68d9Seric 				CurHostName = mci->mci_host;
9829c9e68d9Seric 				break;
9839c9e68d9Seric 			}
9849c9e68d9Seric 			mci->mci_mailer = m;
9859c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9869c9e68d9Seric 				continue;
9879c9e68d9Seric 
9889c9e68d9Seric 			/* try the connection */
9899c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9909c9e68d9Seric 			message("Connecting to %s (%s)...",
9919c9e68d9Seric 				hostbuf, m->m_name);
9929c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
9939c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
9949c9e68d9Seric 			mci->mci_exitstat = i;
9959c9e68d9Seric 			mci->mci_errno = errno;
996f170942cSeric #ifdef NAMED_BIND
997f170942cSeric 			mci->mci_herrno = h_errno;
998f170942cSeric #endif
9999c9e68d9Seric 			if (i == EX_OK)
10009c9e68d9Seric 			{
10019c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10029c9e68d9Seric 				mci_cache(mci);
1003f170942cSeric 				if (TrafficLogFile != NULL)
1004f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1005f170942cSeric 						getpid(), hostbuf);
10069c9e68d9Seric 				break;
10079c9e68d9Seric 			}
10089c9e68d9Seric 			else if (tTd(11, 1))
10099c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10109c9e68d9Seric 					i, errno);
10119c9e68d9Seric 
10129c9e68d9Seric 
10139c9e68d9Seric 			/* enter status of this host */
10149c9e68d9Seric 			setstat(i);
10159c9e68d9Seric 		}
10169c9e68d9Seric 		mci->mci_pid = 0;
10179c9e68d9Seric #else /* no DAEMON */
10189c9e68d9Seric 		syserr("554 openmailer: no IPC");
10199c9e68d9Seric 		if (tTd(11, 1))
10209c9e68d9Seric 			printf("openmailer: NULL\n");
10219c9e68d9Seric 		return NULL;
10229c9e68d9Seric #endif /* DAEMON */
10239c9e68d9Seric 	}
10249c9e68d9Seric 	else
10259c9e68d9Seric 	{
1026f170942cSeric #ifdef XDEBUG
1027f170942cSeric 		char wbuf[MAXLINE];
10286fe8c3bcSeric 
10296fe8c3bcSeric 		/* make absolutely certain 0, 1, and 2 are in use */
1030f170942cSeric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
1031f170942cSeric 		checkfd012(wbuf);
1032f170942cSeric #endif
10330e3bfef5Seric 
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
10809c9e68d9Seric 		(void) signal(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 */
11119c9e68d9Seric 			/* make diagnostic output be standard output */
11129c9e68d9Seric 			(void) signal(SIGINT, SIG_IGN);
11139c9e68d9Seric 			(void) signal(SIGHUP, SIG_IGN);
11149c9e68d9Seric 			(void) signal(SIGTERM, SIG_DFL);
11159c9e68d9Seric 
11169c9e68d9Seric 			/* close any other cached connections */
11179c9e68d9Seric 			mci_flush(FALSE, mci);
11189c9e68d9Seric 
1119b986f6aaSeric 			/* move into some "safe" directory */
1120b986f6aaSeric 			if (m->m_execdir != NULL)
1121b986f6aaSeric 			{
1122b986f6aaSeric 				char *p, *q;
1123b986f6aaSeric 				char buf[MAXLINE];
1124b986f6aaSeric 
1125b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1126b986f6aaSeric 				{
1127b986f6aaSeric 					q = strchr(p, ':');
1128b986f6aaSeric 					if (q != NULL)
1129b986f6aaSeric 						*q = '\0';
1130b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1131b986f6aaSeric 					if (q != NULL)
1132b986f6aaSeric 						*q++ = ':';
1133b986f6aaSeric 					if (tTd(11, 20))
1134b986f6aaSeric 						printf("openmailer: trydir %s\n",
1135b986f6aaSeric 							buf);
1136b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1137b986f6aaSeric 						break;
1138b986f6aaSeric 				}
1139b986f6aaSeric 			}
1140b986f6aaSeric 
11419c9e68d9Seric 			/* arrange to filter std & diag output of command */
11429c9e68d9Seric 			if (clever)
11439c9e68d9Seric 			{
11449c9e68d9Seric 				(void) close(rpvect[0]);
11456fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11466fe8c3bcSeric 				{
11470e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11480e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11496fe8c3bcSeric 					_exit(EX_OSERR);
11506fe8c3bcSeric 				}
11519c9e68d9Seric 				(void) close(rpvect[1]);
11529c9e68d9Seric 			}
11539c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11549c9e68d9Seric 			{
11559c9e68d9Seric 				/* put mailer output in transcript */
11566fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11576fe8c3bcSeric 				{
11580e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11590e3bfef5Seric 						e->e_to, m->m_name,
11606fe8c3bcSeric 						fileno(e->e_xfp));
11616fe8c3bcSeric 					_exit(EX_OSERR);
11629c9e68d9Seric 				}
11636fe8c3bcSeric 			}
11646fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11656fe8c3bcSeric 			{
11660e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
11670e3bfef5Seric 					e->e_to, m->m_name);
11686fe8c3bcSeric 				_exit(EX_OSERR);
11696fe8c3bcSeric 			}
11709c9e68d9Seric 
11719c9e68d9Seric 			/* arrange to get standard input */
11729c9e68d9Seric 			(void) close(mpvect[1]);
11739c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11749c9e68d9Seric 			{
11750e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
11760e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
11779c9e68d9Seric 				_exit(EX_OSERR);
11789c9e68d9Seric 			}
11799c9e68d9Seric 			(void) close(mpvect[0]);
11809c9e68d9Seric 			if (!bitnset(M_RESTR, m->m_flags))
11819c9e68d9Seric 			{
11829c9e68d9Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
11839c9e68d9Seric 				{
11849c9e68d9Seric 					(void) setgid(DefGid);
11859c9e68d9Seric 					(void) initgroups(DefUser, DefGid);
11869c9e68d9Seric 					(void) setuid(DefUid);
11879c9e68d9Seric 				}
11889c9e68d9Seric 				else
11899c9e68d9Seric 				{
11909c9e68d9Seric 					(void) setgid(ctladdr->q_gid);
11919c9e68d9Seric 					(void) initgroups(ctladdr->q_ruser?
11929c9e68d9Seric 						ctladdr->q_ruser: ctladdr->q_user,
11939c9e68d9Seric 						ctladdr->q_gid);
11949c9e68d9Seric 					(void) setuid(ctladdr->q_uid);
11959c9e68d9Seric 				}
11969c9e68d9Seric 			}
11979c9e68d9Seric 
11989c9e68d9Seric 			/* arrange for all the files to be closed */
11999c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12009c9e68d9Seric 			{
12019c9e68d9Seric 				register int j;
12029c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12039c9e68d9Seric 					(void)fcntl(i, F_SETFD, j|1);
12049c9e68d9Seric 			}
12059c9e68d9Seric 
12069c9e68d9Seric 			/* set up the mailer environment */
12079c9e68d9Seric 			i = 0;
12089c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12099c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12109c9e68d9Seric 			{
12119c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12129c9e68d9Seric 					env[i++] = *ep;
12139c9e68d9Seric 			}
12149c9e68d9Seric 			env[i++] = NULL;
12159c9e68d9Seric 
12169c9e68d9Seric 			/* try to execute the mailer */
12179c9e68d9Seric 			execve(m->m_mailer, pv, env);
12189c9e68d9Seric 			saveerrno = errno;
12199c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12207c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12217c941fd2Seric 				_exit(EX_OSERR);
12229c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12239c9e68d9Seric 		}
12249c9e68d9Seric 
12259c9e68d9Seric 		/*
12269c9e68d9Seric 		**  Set up return value.
12279c9e68d9Seric 		*/
12289c9e68d9Seric 
12299c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12309c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12319c9e68d9Seric 		mci->mci_mailer = m;
12329c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12339c9e68d9Seric 		mci->mci_pid = pid;
12349c9e68d9Seric 		(void) close(mpvect[0]);
12359c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
12369c9e68d9Seric 		if (clever)
12379c9e68d9Seric 		{
12389c9e68d9Seric 			(void) close(rpvect[1]);
12399c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
12409c9e68d9Seric 		}
12419c9e68d9Seric 		else
12429c9e68d9Seric 		{
12439c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12449c9e68d9Seric 			mci->mci_in = NULL;
12459c9e68d9Seric 		}
12469c9e68d9Seric 	}
12479c9e68d9Seric 
12489c9e68d9Seric 	/*
12499c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12509c9e68d9Seric 	*/
12519c9e68d9Seric 
12529c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12539c9e68d9Seric 	{
12549c9e68d9Seric 		smtpinit(m, mci, e);
12559c9e68d9Seric 	}
12569c9e68d9Seric 	if (tTd(11, 1))
12579c9e68d9Seric 	{
12589c9e68d9Seric 		printf("openmailer: ");
12599c9e68d9Seric 		mci_dump(mci);
12609c9e68d9Seric 	}
12619c9e68d9Seric 
12629c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1263b31e7f2bSeric 	{
1264b31e7f2bSeric 		/* couldn't open the mailer */
1265b31e7f2bSeric 		rcode = mci->mci_exitstat;
12662a6bc25bSeric 		errno = mci->mci_errno;
1267f170942cSeric #ifdef NAMED_BIND
1268f170942cSeric 		h_errno = mci->mci_herrno;
1269f170942cSeric #endif
1270b31e7f2bSeric 		if (rcode == EX_OK)
1271b31e7f2bSeric 		{
1272b31e7f2bSeric 			/* shouldn't happen */
127308b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12746b0f339dSeric 				rcode, mci->mci_state, firstsig);
1275b31e7f2bSeric 			rcode = EX_SOFTWARE;
1276b31e7f2bSeric 		}
1277e9277e33Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
127890891494Seric 		{
127990891494Seric 			/* try next MX site */
128090891494Seric 			goto tryhost;
128190891494Seric 		}
1282b31e7f2bSeric 	}
1283b31e7f2bSeric 	else if (!clever)
1284b31e7f2bSeric 	{
1285b31e7f2bSeric 		/*
1286b31e7f2bSeric 		**  Format and send message.
1287b31e7f2bSeric 		*/
128815d084d5Seric 
1289b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1290b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1291b31e7f2bSeric 		putline("\n", mci->mci_out, m);
129203c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1293b31e7f2bSeric 
1294b31e7f2bSeric 		/* get the exit status */
1295c9be6216Seric 		rcode = endmailer(mci, e, pv);
1296134746fbSeric 	}
1297134746fbSeric 	else
1298b31e7f2bSeric #ifdef SMTP
1299134746fbSeric 	{
1300b31e7f2bSeric 		/*
1301b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1302b31e7f2bSeric 		*/
130315d084d5Seric 
1304b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1305b31e7f2bSeric 		if (rcode == EX_OK)
130675889e88Seric 		{
1307ded0d3daSkarels 			register char *t = tobuf;
1308ded0d3daSkarels 			register int i;
1309ded0d3daSkarels 
1310588cad61Seric 			/* send the recipient list */
131163780dbdSeric 			tobuf[0] = '\0';
131275889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
131375889e88Seric 			{
131463780dbdSeric 				e->e_to = to->q_paddr;
131515d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
131675889e88Seric 				{
131783b7ddc9Seric 					markfailure(e, to, i);
131881161401Seric 					giveresponse(i, m, mci, e);
131963780dbdSeric 				}
132075889e88Seric 				else
132175889e88Seric 				{
1322911693bfSbostic 					*t++ = ',';
1323b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1324b31e7f2bSeric 						continue;
1325588cad61Seric 				}
1326588cad61Seric 			}
1327588cad61Seric 
132863780dbdSeric 			/* now send the data */
132963780dbdSeric 			if (tobuf[0] == '\0')
1330b31e7f2bSeric 			{
13319c9e68d9Seric 				rcode = EX_OK;
133263780dbdSeric 				e->e_to = NULL;
1333b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1334b31e7f2bSeric 					smtprset(m, mci, e);
1335b31e7f2bSeric 			}
133675889e88Seric 			else
133775889e88Seric 			{
133863780dbdSeric 				e->e_to = tobuf + 1;
133975889e88Seric 				rcode = smtpdata(m, mci, e);
134063780dbdSeric 			}
134163780dbdSeric 
134263780dbdSeric 			/* now close the connection */
1343b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
134415d084d5Seric 				smtpquit(m, mci, e);
134563780dbdSeric 		}
13469c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13479c9e68d9Seric 		{
13489c9e68d9Seric 			/* try next MX site */
13499c9e68d9Seric 			goto tryhost;
13509c9e68d9Seric 		}
1351c579ef51Seric 	}
1352b31e7f2bSeric #else /* not SMTP */
1353a05b3449Sbostic 	{
135408b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1355845e533cSeric 		rcode = EX_CONFIG;
1356b31e7f2bSeric 		goto give_up;
1357a05b3449Sbostic 	}
1358b31e7f2bSeric #endif /* SMTP */
1359134746fbSeric #ifdef NAMED_BIND
13602bcc6d2dSeric 	if (ConfigLevel < 2)
1361912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1362134746fbSeric #endif
13635dfc646bSeric 
1364b31e7f2bSeric 	/* arrange a return receipt if requested */
13658c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1366b31e7f2bSeric 	{
1367b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1368b31e7f2bSeric 		/* do we want to send back more info? */
1369b31e7f2bSeric 	}
1370b31e7f2bSeric 
1371c77d1c25Seric 	/*
137263780dbdSeric 	**  Do final status disposal.
137363780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1374c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1375c77d1c25Seric 	**		addressees.
1376c77d1c25Seric 	*/
1377c77d1c25Seric 
1378b31e7f2bSeric   give_up:
137963780dbdSeric 	if (tobuf[0] != '\0')
138081161401Seric 		giveresponse(rcode, m, mci, e);
1381772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1382b31e7f2bSeric 	{
1383dde5acadSeric 		if (rcode != EX_OK)
138483b7ddc9Seric 			markfailure(e, to, rcode);
1385dde5acadSeric 		else
1386655518ecSeric 		{
1387dde5acadSeric 			to->q_flags |= QSENT;
1388655518ecSeric 			e->e_nsent++;
1389655518ecSeric 		}
1390b31e7f2bSeric 	}
1391b31e7f2bSeric 
1392b31e7f2bSeric 	/*
1393b31e7f2bSeric 	**  Restore state and return.
1394b31e7f2bSeric 	*/
1395c77d1c25Seric 
139635490626Seric 	errno = 0;
1397588cad61Seric 	define('g', (char *) NULL, e);
13985826d9d3Seric 	return (rcode);
139925a99e2eSeric }
14005dfc646bSeric /*
140183b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
140283b7ddc9Seric **
140383b7ddc9Seric **	Parameters:
140483b7ddc9Seric **		e -- the envelope we are sending.
140583b7ddc9Seric **		q -- the address to mark.
140683b7ddc9Seric **		rcode -- the code signifying the particular failure.
140783b7ddc9Seric **
140883b7ddc9Seric **	Returns:
140983b7ddc9Seric **		none.
141083b7ddc9Seric **
141183b7ddc9Seric **	Side Effects:
141283b7ddc9Seric **		marks the address (and possibly the envelope) with the
141383b7ddc9Seric **			failure so that an error will be returned or
141483b7ddc9Seric **			the message will be queued, as appropriate.
141583b7ddc9Seric */
141683b7ddc9Seric 
141783b7ddc9Seric markfailure(e, q, rcode)
141883b7ddc9Seric 	register ENVELOPE *e;
141983b7ddc9Seric 	register ADDRESS *q;
142083b7ddc9Seric 	int rcode;
142183b7ddc9Seric {
142219c47125Seric 	char buf[MAXLINE];
142319c47125Seric 
142483b7ddc9Seric 	if (rcode == EX_OK)
142583b7ddc9Seric 		return;
1426f170942cSeric 	else if (rcode == EX_TEMPFAIL)
142783b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1428f170942cSeric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
1429f170942cSeric 		q->q_flags |= QBADADDR;
143083b7ddc9Seric }
143183b7ddc9Seric /*
1432c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1433c579ef51Seric **
1434c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1435c579ef51Seric **	violation), so we report those specially.  For other
1436c579ef51Seric **	errors, we choose a status message (into statmsg),
1437c579ef51Seric **	and if it represents an error, we print it.
1438c579ef51Seric **
1439c579ef51Seric **	Parameters:
1440c579ef51Seric **		pid -- pid of mailer.
1441c9be6216Seric **		e -- the current envelope.
1442c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1443c9be6216Seric **			(for error messages).
1444c579ef51Seric **
1445c579ef51Seric **	Returns:
1446c579ef51Seric **		exit code of mailer.
1447c579ef51Seric **
1448c579ef51Seric **	Side Effects:
1449c579ef51Seric **		none.
1450c579ef51Seric */
1451c579ef51Seric 
1452c9be6216Seric endmailer(mci, e, pv)
1453b31e7f2bSeric 	register MCI *mci;
1454c9be6216Seric 	register ENVELOPE *e;
1455c9be6216Seric 	char **pv;
1456c579ef51Seric {
1457588cad61Seric 	int st;
1458c579ef51Seric 
145975889e88Seric 	/* close any connections */
146075889e88Seric 	if (mci->mci_in != NULL)
1461c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
146275889e88Seric 	if (mci->mci_out != NULL)
1463c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
146475889e88Seric 	mci->mci_in = mci->mci_out = NULL;
146575889e88Seric 	mci->mci_state = MCIS_CLOSED;
146675889e88Seric 
146733db8731Seric 	/* in the IPC case there is nothing to wait for */
146875889e88Seric 	if (mci->mci_pid == 0)
146933db8731Seric 		return (EX_OK);
147033db8731Seric 
147133db8731Seric 	/* wait for the mailer process to die and collect status */
147275889e88Seric 	st = waitfor(mci->mci_pid);
1473588cad61Seric 	if (st == -1)
147478de67c1Seric 	{
1475c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1476588cad61Seric 		return (EX_SOFTWARE);
1477c579ef51Seric 	}
147833db8731Seric 
147933db8731Seric 	/* see if it died a horrid death */
1480c579ef51Seric 	if ((st & 0377) != 0)
1481c579ef51Seric 	{
1482c9be6216Seric 		syserr("mailer %s died with signal %o", pv[0], st);
1483c9be6216Seric 
1484c9be6216Seric 		/* log the arguments */
1485c9be6216Seric 		if (e->e_xfp != NULL)
1486c9be6216Seric 		{
1487c9be6216Seric 			register char **av;
1488c9be6216Seric 
1489c9be6216Seric 			fprintf(e->e_xfp, "Arguments:");
1490c9be6216Seric 			for (av = pv; *av != NULL; av++)
1491c9be6216Seric 				fprintf(e->e_xfp, " %s", *av);
1492c9be6216Seric 			fprintf(e->e_xfp, "\n");
1493c9be6216Seric 		}
1494c9be6216Seric 
14955f73204aSeric 		ExitStat = EX_TEMPFAIL;
14965f73204aSeric 		return (EX_TEMPFAIL);
1497c579ef51Seric 	}
149833db8731Seric 
149933db8731Seric 	/* normal death -- return status */
1500588cad61Seric 	st = (st >> 8) & 0377;
1501588cad61Seric 	return (st);
1502c579ef51Seric }
1503c579ef51Seric /*
150425a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
150525a99e2eSeric **
150625a99e2eSeric **	Parameters:
150725a99e2eSeric **		stat -- the status code from the mailer (high byte
150825a99e2eSeric **			only; core dumps must have been taken care of
150925a99e2eSeric **			already).
151081161401Seric **		m -- the mailer info for this mailer.
151181161401Seric **		mci -- the mailer connection info -- can be NULL if the
151281161401Seric **			response is given before the connection is made.
151381161401Seric **		e -- the current envelope.
151425a99e2eSeric **
151525a99e2eSeric **	Returns:
1516db8841e9Seric **		none.
151725a99e2eSeric **
151825a99e2eSeric **	Side Effects:
1519c1f9df2cSeric **		Errors may be incremented.
152025a99e2eSeric **		ExitStat may be set.
152125a99e2eSeric */
152225a99e2eSeric 
152381161401Seric giveresponse(stat, m, mci, e)
152425a99e2eSeric 	int stat;
1525588cad61Seric 	register MAILER *m;
152681161401Seric 	register MCI *mci;
1527198d9be0Seric 	ENVELOPE *e;
152825a99e2eSeric {
15299c16475dSeric 	register const char *statmsg;
153025a99e2eSeric 	extern char *SysExMsg[];
153125a99e2eSeric 	register int i;
1532d4bd8f0eSbostic 	extern int N_SysEx;
1533198d9be0Seric 	char buf[MAXLINE];
153425a99e2eSeric 
153513bbc08cSeric 	/*
153613bbc08cSeric 	**  Compute status message from code.
153713bbc08cSeric 	*/
153813bbc08cSeric 
153925a99e2eSeric 	i = stat - EX__BASE;
1540588cad61Seric 	if (stat == 0)
15416fe8c3bcSeric 	{
1542588cad61Seric 		statmsg = "250 Sent";
1543ce5531bdSeric 		if (e->e_statmsg != NULL)
15446fe8c3bcSeric 		{
1545ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15466fe8c3bcSeric 			statmsg = buf;
15476fe8c3bcSeric 		}
15486fe8c3bcSeric 	}
1549588cad61Seric 	else if (i < 0 || i > N_SysEx)
1550588cad61Seric 	{
1551588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1552588cad61Seric 		stat = EX_UNAVAILABLE;
1553588cad61Seric 		statmsg = buf;
1554588cad61Seric 	}
1555198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1556198d9be0Seric 	{
15577d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1558d4bd8f0eSbostic #ifdef NAMED_BIND
1559f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1560*4d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1561f28da541Smiriam 		else
1562d4bd8f0eSbostic #endif
1563f28da541Smiriam 		{
15648557d168Seric 			if (errno != 0)
1565d87e85f3Seric 				statmsg = errstring(errno);
1566d87e85f3Seric 			else
1567d87e85f3Seric 			{
1568d87e85f3Seric #ifdef SMTP
1569d87e85f3Seric 				extern char SmtpError[];
1570d87e85f3Seric 
1571d87e85f3Seric 				statmsg = SmtpError;
15726c2c3107Seric #else /* SMTP */
1573d87e85f3Seric 				statmsg = NULL;
15746c2c3107Seric #endif /* SMTP */
1575d87e85f3Seric 			}
1576f28da541Smiriam 		}
1577d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1578d87e85f3Seric 		{
157987c9b3e7Seric 			(void) strcat(buf, ": ");
1580d87e85f3Seric 			(void) strcat(buf, statmsg);
15818557d168Seric 		}
1582198d9be0Seric 		statmsg = buf;
1583198d9be0Seric 	}
1584f170942cSeric #ifdef NAMED_BIND
1585f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1586f170942cSeric 	{
1587*4d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1588f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1589f170942cSeric 		statmsg = buf;
1590f170942cSeric 	}
1591f170942cSeric #endif
159225a99e2eSeric 	else
1593d87e85f3Seric 	{
159425a99e2eSeric 		statmsg = SysExMsg[i];
15957d55540cSeric 		if (*statmsg++ == ':')
15967d55540cSeric 		{
15977d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
15987d55540cSeric 			statmsg = buf;
15997d55540cSeric 		}
1600d87e85f3Seric 	}
1601588cad61Seric 
1602588cad61Seric 	/*
1603588cad61Seric 	**  Print the message as appropriate
1604588cad61Seric 	*/
1605588cad61Seric 
1606198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1607e12d03eeSeric 		message(&statmsg[4], errstring(errno));
160825a99e2eSeric 	else
160925a99e2eSeric 	{
1610c1f9df2cSeric 		Errors++;
1611e12d03eeSeric 		usrerr(statmsg, errstring(errno));
161225a99e2eSeric 	}
161325a99e2eSeric 
161425a99e2eSeric 	/*
161525a99e2eSeric 	**  Final cleanup.
161625a99e2eSeric 	**	Log a record of the transaction.  Compute the new
161725a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
161825a99e2eSeric 	**	that.
161925a99e2eSeric 	*/
162025a99e2eSeric 
16212f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
162281161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1623eb238f8cSeric 
1624eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1625eb238f8cSeric 		setstat(stat);
1626198d9be0Seric 	if (stat != EX_OK)
1627198d9be0Seric 	{
1628198d9be0Seric 		if (e->e_message != NULL)
1629198d9be0Seric 			free(e->e_message);
1630198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1631198d9be0Seric 	}
16328557d168Seric 	errno = 0;
1633d4bd8f0eSbostic #ifdef NAMED_BIND
1634f28da541Smiriam 	h_errno = 0;
1635d4bd8f0eSbostic #endif
1636eb238f8cSeric }
1637eb238f8cSeric /*
1638eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1639eb238f8cSeric **
1640eb238f8cSeric **	Parameters:
164181161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
164281161401Seric **		mci -- the mailer connection info -- can be NULL if the
164381161401Seric **			log is occuring when no connection is active.
164481161401Seric **		stat -- the message to print for the status.
164581161401Seric **		e -- the current envelope.
1646eb238f8cSeric **
1647eb238f8cSeric **	Returns:
1648eb238f8cSeric **		none
1649eb238f8cSeric **
1650eb238f8cSeric **	Side Effects:
1651eb238f8cSeric **		none
1652eb238f8cSeric */
1653eb238f8cSeric 
165481161401Seric logdelivery(m, mci, stat, e)
165581161401Seric 	MAILER *m;
165681161401Seric 	register MCI *mci;
1657eb238f8cSeric 	char *stat;
1658b31e7f2bSeric 	register ENVELOPE *e;
16595cf56be3Seric {
1660eb238f8cSeric # ifdef LOG
1661d6acf3eeSeric 	char buf[512];
16629507d1f9Seric 
166348ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
166481161401Seric 
166548ed5d33Seric 	if (m != NULL)
166671ff6caaSeric 	{
166748ed5d33Seric 		(void) strcat(buf, ", mailer=");
166848ed5d33Seric 		(void) strcat(buf, m->m_name);
166971ff6caaSeric 	}
167048ed5d33Seric 
167148ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
167271ff6caaSeric 	{
167371ff6caaSeric # ifdef DAEMON
1674e2f2f828Seric 		extern SOCKADDR CurHostAddr;
167548ed5d33Seric # endif
167671ff6caaSeric 
167748ed5d33Seric 		(void) strcat(buf, ", relay=");
167848ed5d33Seric 		(void) strcat(buf, mci->mci_host);
167948ed5d33Seric 
168048ed5d33Seric # ifdef DAEMON
168148ed5d33Seric 		(void) strcat(buf, " (");
1682e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
168348ed5d33Seric 		(void) strcat(buf, ")");
168471ff6caaSeric # endif
168571ff6caaSeric 	}
16869507d1f9Seric 	else
168748ed5d33Seric 	{
168848ed5d33Seric 		char *p = macvalue('h', e);
16899507d1f9Seric 
169048ed5d33Seric 		if (p != NULL && p[0] != '\0')
169148ed5d33Seric 		{
169248ed5d33Seric 			(void) strcat(buf, ", relay=");
169348ed5d33Seric 			(void) strcat(buf, p);
169448ed5d33Seric 		}
169548ed5d33Seric 	}
1696d6acf3eeSeric 
1697d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1698d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
16996c2c3107Seric # endif /* LOG */
170025a99e2eSeric }
170125a99e2eSeric /*
170251552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
170325a99e2eSeric **
170451552439Seric **	This can be made an arbitrary message separator by changing $l
170551552439Seric **
17069b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
17079b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
17089b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
17099b6c17a6Seric **	this kind of antique garbage????
171025a99e2eSeric **
171125a99e2eSeric **	Parameters:
171251552439Seric **		fp -- the file to output to.
171351552439Seric **		m -- the mailer describing this entry.
171425a99e2eSeric **
171525a99e2eSeric **	Returns:
171651552439Seric **		none
171725a99e2eSeric **
171825a99e2eSeric **	Side Effects:
171951552439Seric **		outputs some text to fp.
172025a99e2eSeric */
172125a99e2eSeric 
1722b31e7f2bSeric putfromline(fp, m, e)
172351552439Seric 	register FILE *fp;
172451552439Seric 	register MAILER *m;
1725b31e7f2bSeric 	ENVELOPE *e;
172625a99e2eSeric {
17272bc47524Seric 	char *template = "\201l\n";
172851552439Seric 	char buf[MAXLINE];
172925a99e2eSeric 
173057fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
173151552439Seric 		return;
173213bbc08cSeric 
17332c7e1b8dSeric # ifdef UGLYUUCP
173457fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
173574b6e67bSeric 	{
1736ea09d6edSeric 		char *bang;
1737ea09d6edSeric 		char xbuf[MAXLINE];
173874b6e67bSeric 
1739ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17406c2c3107Seric 		bang = strchr(buf, '!');
174174b6e67bSeric 		if (bang == NULL)
174208b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
174374b6e67bSeric 		else
1744588cad61Seric 		{
1745ea09d6edSeric 			*bang++ = '\0';
17462bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1747ea09d6edSeric 			template = xbuf;
174874b6e67bSeric 		}
1749588cad61Seric 	}
17506c2c3107Seric # endif /* UGLYUUCP */
1751b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
175277b52738Seric 	putline(buf, fp, m);
1753bc6e2962Seric }
1754bc6e2962Seric /*
175551552439Seric **  PUTBODY -- put the body of a message.
175651552439Seric **
175751552439Seric **	Parameters:
175851552439Seric **		fp -- file to output onto.
175977b52738Seric **		m -- a mailer descriptor to control output format.
17609a6a5f55Seric **		e -- the envelope to put out.
176103c02fdeSeric **		separator -- if non-NULL, a message separator that must
176203c02fdeSeric **			not be permitted in the resulting message.
176351552439Seric **
176451552439Seric **	Returns:
176551552439Seric **		none.
176651552439Seric **
176751552439Seric **	Side Effects:
176851552439Seric **		The message is written onto fp.
176951552439Seric */
177051552439Seric 
177103c02fdeSeric putbody(fp, m, e, separator)
177251552439Seric 	FILE *fp;
1773588cad61Seric 	MAILER *m;
17749a6a5f55Seric 	register ENVELOPE *e;
177503c02fdeSeric 	char *separator;
177651552439Seric {
177777b52738Seric 	char buf[MAXLINE];
177851552439Seric 
177951552439Seric 	/*
178051552439Seric 	**  Output the body of the message
178151552439Seric 	*/
178251552439Seric 
17839a6a5f55Seric 	if (e->e_dfp == NULL)
178451552439Seric 	{
17859a6a5f55Seric 		if (e->e_df != NULL)
17869a6a5f55Seric 		{
17879a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
17889a6a5f55Seric 			if (e->e_dfp == NULL)
17898f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
17908f9146b0Srick 				e->e_df, e->e_to, e->e_from);
17919a6a5f55Seric 		}
17929a6a5f55Seric 		else
179377b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
17949a6a5f55Seric 	}
17959a6a5f55Seric 	if (e->e_dfp != NULL)
17969a6a5f55Seric 	{
17979a6a5f55Seric 		rewind(e->e_dfp);
179877b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
179924fc8aeeSeric 		{
180024fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1801d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
18023462ad9eSeric 				(void) putc('>', fp);
180303c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
180403c02fdeSeric 			{
180503c02fdeSeric 				/* possible separator */
180603c02fdeSeric 				int sl = strlen(separator);
180703c02fdeSeric 
180803c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
180903c02fdeSeric 					(void) putc(' ', fp);
181003c02fdeSeric 			}
181177b52738Seric 			putline(buf, fp, m);
181224fc8aeeSeric 		}
181351552439Seric 
18149a6a5f55Seric 		if (ferror(e->e_dfp))
181551552439Seric 		{
181651552439Seric 			syserr("putbody: read error");
181751552439Seric 			ExitStat = EX_IOERR;
181851552439Seric 		}
181951552439Seric 	}
182051552439Seric 
18210890ba1fSeric 	/* some mailers want extra blank line at end of message */
18220890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
18230890ba1fSeric 		putline("", fp, m);
18240890ba1fSeric 
182551552439Seric 	(void) fflush(fp);
182651552439Seric 	if (ferror(fp) && errno != EPIPE)
182751552439Seric 	{
182851552439Seric 		syserr("putbody: write error");
182951552439Seric 		ExitStat = EX_IOERR;
183051552439Seric 	}
183151552439Seric 	errno = 0;
183225a99e2eSeric }
183325a99e2eSeric /*
183425a99e2eSeric **  MAILFILE -- Send a message to a file.
183525a99e2eSeric **
1836f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1837f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1838f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1839f129ec7dSeric **	sendmail runs as root.
1840f129ec7dSeric **
1841588cad61Seric **	This could be done as a subordinate mailer, except that it
1842588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1843588cad61Seric **	view this as being sufficiently important as to include it
1844588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1845588cad61Seric **	to create another process plus some pipes to save the message.
1846588cad61Seric **
184725a99e2eSeric **	Parameters:
184825a99e2eSeric **		filename -- the name of the file to send to.
18496259796dSeric **		ctladdr -- the controlling address header -- includes
18506259796dSeric **			the userid/groupid to be when sending.
185125a99e2eSeric **
185225a99e2eSeric **	Returns:
185325a99e2eSeric **		The exit code associated with the operation.
185425a99e2eSeric **
185525a99e2eSeric **	Side Effects:
185625a99e2eSeric **		none.
185725a99e2eSeric */
185825a99e2eSeric 
1859b31e7f2bSeric mailfile(filename, ctladdr, e)
186025a99e2eSeric 	char *filename;
18616259796dSeric 	ADDRESS *ctladdr;
1862b31e7f2bSeric 	register ENVELOPE *e;
186325a99e2eSeric {
186425a99e2eSeric 	register FILE *f;
186532d19d43Seric 	register int pid;
186615d084d5Seric 	int mode;
186725a99e2eSeric 
1868671745f3Seric 	if (tTd(11, 1))
1869671745f3Seric 	{
1870671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
1871671745f3Seric 		printaddr(ctladdr, FALSE);
1872671745f3Seric 	}
1873671745f3Seric 
1874f170942cSeric 	if (e->e_xfp != NULL)
1875f170942cSeric 		fflush(e->e_xfp);
1876f170942cSeric 
187732d19d43Seric 	/*
187832d19d43Seric 	**  Fork so we can change permissions here.
187932d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
188032d19d43Seric 	**	the complications of calling subroutines, etc.
188132d19d43Seric 	*/
188232d19d43Seric 
188332d19d43Seric 	DOFORK(fork);
188432d19d43Seric 
188532d19d43Seric 	if (pid < 0)
188632d19d43Seric 		return (EX_OSERR);
188732d19d43Seric 	else if (pid == 0)
188832d19d43Seric 	{
188932d19d43Seric 		/* child -- actually write to file */
1890f129ec7dSeric 		struct stat stb;
1891f129ec7dSeric 
18920984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
18930984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
18940984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
18953462ad9eSeric 		(void) umask(OldUmask);
189695f16dc0Seric 
1897f129ec7dSeric 		if (stat(filename, &stb) < 0)
18983a98e7eaSeric 			stb.st_mode = FileMode;
189915d084d5Seric 		mode = stb.st_mode;
190095f16dc0Seric 
190195f16dc0Seric 		/* limit the errors to those actually caused in the child */
190295f16dc0Seric 		errno = 0;
190395f16dc0Seric 		ExitStat = EX_OK;
190495f16dc0Seric 
1905f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1906f129ec7dSeric 			exit(EX_CANTCREAT);
190703827b5fSeric 		if (ctladdr == NULL)
19088f9146b0Srick 			ctladdr = &e->e_from;
190915d084d5Seric 		else
191015d084d5Seric 		{
191115d084d5Seric 			/* ignore setuid and setgid bits */
191215d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
191315d084d5Seric 		}
191415d084d5Seric 
19158f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
19168f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
19178f9146b0Srick 		{
19188f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
191995f16dc0Seric 			if (e->e_dfp == NULL)
192095f16dc0Seric 			{
19218f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
19228f9146b0Srick 					e->e_df, e->e_to, e->e_from);
19238f9146b0Srick 			}
19248f9146b0Srick 		}
19258f9146b0Srick 
192615d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1927e36b99e2Seric 		{
192895f16dc0Seric 			if (ctladdr->q_uid == 0)
192995f16dc0Seric 			{
1930e36b99e2Seric 				(void) setgid(DefGid);
1931898a126bSbostic 				(void) initgroups(DefUser, DefGid);
193295f16dc0Seric 			}
193395f16dc0Seric 			else
193495f16dc0Seric 			{
19356259796dSeric 				(void) setgid(ctladdr->q_gid);
1936898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1937898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1938898a126bSbostic 					ctladdr->q_gid);
1939898a126bSbostic 			}
1940e36b99e2Seric 		}
194115d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1942e36b99e2Seric 		{
1943e36b99e2Seric 			if (ctladdr->q_uid == 0)
1944e36b99e2Seric 				(void) setuid(DefUid);
1945e36b99e2Seric 			else
19466259796dSeric 				(void) setuid(ctladdr->q_uid);
1947e36b99e2Seric 		}
194895f16dc0Seric 		FileName = filename;
194995f16dc0Seric 		LineNumber = 0;
19503a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
195125a99e2eSeric 		if (f == NULL)
195295f16dc0Seric 		{
195308b25121Seric 			message("554 cannot open");
195432d19d43Seric 			exit(EX_CANTCREAT);
195595f16dc0Seric 		}
195625a99e2eSeric 
19570331ce05Seric 		putfromline(f, FileMailer, e);
19580331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
19590331ce05Seric 		putline("\n", f, FileMailer);
196003c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
19610331ce05Seric 		putline("\n", f, FileMailer);
196295f16dc0Seric 		if (ferror(f))
196395f16dc0Seric 		{
196408b25121Seric 			message("451 I/O error");
196595f16dc0Seric 			setstat(EX_IOERR);
196695f16dc0Seric 		}
1967ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
196832d19d43Seric 		(void) fflush(stdout);
1969e36b99e2Seric 
197027628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1971c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
197295f16dc0Seric 		exit(ExitStat);
197313bbc08cSeric 		/*NOTREACHED*/
197432d19d43Seric 	}
197532d19d43Seric 	else
197632d19d43Seric 	{
197732d19d43Seric 		/* parent -- wait for exit status */
1978588cad61Seric 		int st;
197932d19d43Seric 
1980588cad61Seric 		st = waitfor(pid);
1981588cad61Seric 		if ((st & 0377) != 0)
1982588cad61Seric 			return (EX_UNAVAILABLE);
1983588cad61Seric 		else
1984588cad61Seric 			return ((st >> 8) & 0377);
19858f9146b0Srick 		/*NOTREACHED*/
198632d19d43Seric 	}
198725a99e2eSeric }
1988ea4dc939Seric /*
1989e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1990e103b48fSeric **
1991e103b48fSeric **	The signature describes how we are going to send this -- it
1992e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1993e103b48fSeric **	an ordered list of MX hosts.
1994e103b48fSeric **
1995e103b48fSeric **	Parameters:
1996e103b48fSeric **		m -- the mailer describing this host.
1997e103b48fSeric **		host -- the host name.
1998e103b48fSeric **		e -- the current envelope.
1999e103b48fSeric **
2000e103b48fSeric **	Returns:
2001e103b48fSeric **		The signature for this host.
2002e103b48fSeric **
2003e103b48fSeric **	Side Effects:
2004e103b48fSeric **		Can tweak the symbol table.
2005e103b48fSeric */
2006e103b48fSeric 
2007e103b48fSeric char *
2008e103b48fSeric hostsignature(m, host, e)
2009e103b48fSeric 	register MAILER *m;
2010e103b48fSeric 	char *host;
2011e103b48fSeric 	ENVELOPE *e;
2012e103b48fSeric {
2013e103b48fSeric 	register char *p;
2014e103b48fSeric 	register STAB *s;
2015e103b48fSeric 	int i;
2016e103b48fSeric 	int len;
2017e103b48fSeric #ifdef NAMED_BIND
2018e103b48fSeric 	int nmx;
2019e103b48fSeric 	auto int rcode;
2020bafdc4e5Seric 	char *hp;
2021bafdc4e5Seric 	char *endp;
2022516782b4Seric 	int oldoptions;
2023e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2024e103b48fSeric #endif
2025e103b48fSeric 
2026e103b48fSeric 	/*
2027e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2028e103b48fSeric 	*/
2029e103b48fSeric 
2030e103b48fSeric 	p = m->m_mailer;
2031e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2032e103b48fSeric 	{
2033e103b48fSeric 		/* just an ordinary mailer */
2034e103b48fSeric 		return host;
2035e103b48fSeric 	}
2036e103b48fSeric 
2037e103b48fSeric 	/*
2038e103b48fSeric 	**  If it is a numeric address, just return it.
2039e103b48fSeric 	*/
2040e103b48fSeric 
2041e103b48fSeric 	if (host[0] == '[')
2042e103b48fSeric 		return host;
2043e103b48fSeric 
2044e103b48fSeric 	/*
2045e103b48fSeric 	**  Look it up in the symbol table.
2046e103b48fSeric 	*/
2047e103b48fSeric 
2048e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2049e103b48fSeric 	if (s->s_hostsig != NULL)
2050e103b48fSeric 		return s->s_hostsig;
2051e103b48fSeric 
2052e103b48fSeric 	/*
2053e103b48fSeric 	**  Not already there -- create a signature.
2054e103b48fSeric 	*/
2055e103b48fSeric 
2056e103b48fSeric #ifdef NAMED_BIND
2057516782b4Seric 	if (ConfigLevel < 2)
2058516782b4Seric 	{
2059516782b4Seric 		oldoptions = _res.options;
2060516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2061516782b4Seric 	}
2062516782b4Seric 
2063bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2064bafdc4e5Seric 	{
2065bafdc4e5Seric 		endp = strchr(hp, ':');
2066bafdc4e5Seric 		if (endp != NULL)
2067bafdc4e5Seric 			*endp = '\0';
2068bafdc4e5Seric 
20697bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
20707d55540cSeric 
2071e103b48fSeric 		if (nmx <= 0)
2072e103b48fSeric 		{
2073e103b48fSeric 			register MCI *mci;
2074e103b48fSeric 			extern int errno;
2075e103b48fSeric 
2076e103b48fSeric 			/* update the connection info for this host */
2077bafdc4e5Seric 			mci = mci_get(hp, m);
2078e103b48fSeric 			mci->mci_exitstat = rcode;
2079e103b48fSeric 			mci->mci_errno = errno;
2080f170942cSeric #ifdef NAMED_BIND
2081f170942cSeric 			mci->mci_herrno = h_errno;
2082f170942cSeric #endif
2083e103b48fSeric 
2084e103b48fSeric 			/* and return the original host name as the signature */
2085bafdc4e5Seric 			nmx = 1;
2086bafdc4e5Seric 			mxhosts[0] = hp;
2087e103b48fSeric 		}
2088e103b48fSeric 
2089e103b48fSeric 		len = 0;
2090e103b48fSeric 		for (i = 0; i < nmx; i++)
2091e103b48fSeric 		{
2092e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2093e103b48fSeric 		}
2094bafdc4e5Seric 		if (s->s_hostsig != NULL)
2095bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2096bafdc4e5Seric 		p = xalloc(len);
2097bafdc4e5Seric 		if (s->s_hostsig != NULL)
2098bafdc4e5Seric 		{
2099bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2100bafdc4e5Seric 			free(s->s_hostsig);
2101bafdc4e5Seric 			s->s_hostsig = p;
2102bafdc4e5Seric 			p += strlen(p);
2103bafdc4e5Seric 			*p++ = ':';
2104bafdc4e5Seric 		}
2105bafdc4e5Seric 		else
2106bafdc4e5Seric 			s->s_hostsig = p;
2107e103b48fSeric 		for (i = 0; i < nmx; i++)
2108e103b48fSeric 		{
2109e103b48fSeric 			if (i != 0)
2110e103b48fSeric 				*p++ = ':';
2111e103b48fSeric 			strcpy(p, mxhosts[i]);
2112e103b48fSeric 			p += strlen(p);
2113e103b48fSeric 		}
2114bafdc4e5Seric 		if (endp != NULL)
2115bafdc4e5Seric 			*endp++ = ':';
2116bafdc4e5Seric 	}
2117e103b48fSeric 	makelower(s->s_hostsig);
2118516782b4Seric 	if (ConfigLevel < 2)
2119516782b4Seric 		_res.options = oldoptions;
2120e103b48fSeric #else
2121e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2122e103b48fSeric 	s->s_hostsig = host;
2123e103b48fSeric #endif
2124e103b48fSeric 	if (tTd(17, 1))
2125e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2126e103b48fSeric 	return s->s_hostsig;
2127e103b48fSeric }
2128