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*44f2317fSeric static char sccsid[] = "@(#)deliver.c	8.11 (Berkeley) 08/08/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 	{
799c9e68d9Seric 		printf("\nSENDALL: mode %c, e_from ", mode);
809c9e68d9Seric 		printaddr(&e->e_from, FALSE);
819c9e68d9Seric 		printf("sendqueue:\n");
829c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
839c9e68d9Seric 	}
849c9e68d9Seric 
859c9e68d9Seric 	/*
869c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
879c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
889c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
899c9e68d9Seric 	*/
909c9e68d9Seric 
919c9e68d9Seric 	CurEnv = e;
929c9e68d9Seric 
939c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
949c9e68d9Seric 	{
959c9e68d9Seric 		errno = 0;
969c9e68d9Seric 		syserr("554 too many hops %d (%d max): from %s, to %s",
979c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
989c9e68d9Seric 			e->e_sendqueue->q_paddr);
999c9e68d9Seric 		return;
1009c9e68d9Seric 	}
1019c9e68d9Seric 
102b8004690Seric 	/*
103b8004690Seric 	**  Do sender deletion.
104b8004690Seric 	**
105b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
106b8004690Seric 	**	This can happen if the name server is hosed when you
107b8004690Seric 	**	are trying to send mail.  The result is that the sender
108b8004690Seric 	**	is instantiated in the queue as a recipient.
109b8004690Seric 	*/
110b8004690Seric 
111e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
112e76a6a8fSeric 	    !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;
228da662164Seric 				ee->e_df = queuename(ee, 'd');
229da662164Seric 				ee->e_df = newstr(ee->e_df);
230ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
231ce5531bdSeric 				{
232ce5531bdSeric 					syserr("sendall: link(%s, %s)",
233ce5531bdSeric 						e->e_df, ee->e_df);
234ce5531bdSeric 				}
235ce5531bdSeric 			}
236ce5531bdSeric 
237ce5531bdSeric 			if (mode != SM_VERIFY)
238ce5531bdSeric 				openxscript(ee);
239ce5531bdSeric #ifdef LOG
240ce5531bdSeric 			if (LogLevel > 4)
241ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
242ce5531bdSeric 					ee->e_id, e->e_id);
243ce5531bdSeric #endif
244ce5531bdSeric 		}
245ce5531bdSeric 	}
246ce5531bdSeric 
247ce5531bdSeric 	if (owner != NULL)
248ce5531bdSeric 	{
249ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
250ce5531bdSeric 		if (tTd(13, 5))
251ce5531bdSeric 		{
252ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
253ce5531bdSeric 			printaddr(&e->e_from, FALSE);
254ce5531bdSeric 		}
255ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
256ce5531bdSeric 		e->e_errormode = EM_MAIL;
257ce5531bdSeric 	}
258ce5531bdSeric 
259c1ac89b1Seric # ifdef QUEUE
260c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
261c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
262c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
263c1ac89b1Seric 	{
264c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2656103d9b4Seric 		queueup(e, TRUE, announcequeueup);
266ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2676103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
268c1ac89b1Seric 	}
269c1ac89b1Seric #endif /* QUEUE */
270c1ac89b1Seric 
271ce5531bdSeric 	if (splitenv != NULL)
272ce5531bdSeric 	{
273ce5531bdSeric 		if (tTd(13, 1))
274ce5531bdSeric 		{
275ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
276ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
277ce5531bdSeric 		}
278ce5531bdSeric 
279ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
280ce5531bdSeric 		{
281ce5531bdSeric 			CurEnv = ee;
282ce5531bdSeric 			sendenvelope(ee, mode);
283ce5531bdSeric 		}
284ce5531bdSeric 
285ce5531bdSeric 		CurEnv = e;
286ce5531bdSeric 	}
287ce5531bdSeric 	sendenvelope(e, mode);
288ce5531bdSeric 
289ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
290ce5531bdSeric 		dropenvelope(splitenv);
291ce5531bdSeric }
292ce5531bdSeric 
293ce5531bdSeric sendenvelope(e, mode)
294ce5531bdSeric 	register ENVELOPE *e;
295ce5531bdSeric 	char mode;
296ce5531bdSeric {
297ce5531bdSeric 	bool oldverbose;
298ce5531bdSeric 	int pid;
299ce5531bdSeric 	register ADDRESS *q;
3002b9178d3Seric #ifndef HASFLOCK
301ce5531bdSeric 	struct flock lfd;
302ce5531bdSeric #endif
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 		/*
3352b9178d3Seric 		**  Since fcntl lockin has the interesting semantic that
3362b9178d3Seric 		**  the lock is lost when we fork, we have to risk losing
337c1ac89b1Seric 		**  the lock here by closing before the fork, and then
338c1ac89b1Seric 		**  trying to get it back in the child.
339c1ac89b1Seric 		*/
340c1ac89b1Seric 
341c1ac89b1Seric 		if (e->e_lockfp != NULL)
342c1ac89b1Seric 		{
343c1ac89b1Seric 			(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
344c1ac89b1Seric 			e->e_lockfp = NULL;
345c1ac89b1Seric 		}
3462b9178d3Seric # endif /* HASFLOCK */
347c1ac89b1Seric 
348c1ac89b1Seric 		pid = fork();
349c1ac89b1Seric 		if (pid < 0)
350c1ac89b1Seric 		{
351c1ac89b1Seric 			goto queueonly;
352c1ac89b1Seric 		}
353c1ac89b1Seric 		else if (pid > 0)
354c1ac89b1Seric 		{
355c1ac89b1Seric 			/* be sure we leave the temp files to our child */
356c1ac89b1Seric 			e->e_id = e->e_df = NULL;
3572b9178d3Seric # ifdef HASFLOCK
358c1ac89b1Seric 			if (e->e_lockfp != NULL)
359c1ac89b1Seric 			{
360c1ac89b1Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
361c1ac89b1Seric 				e->e_lockfp = NULL;
362c1ac89b1Seric 			}
363c1ac89b1Seric # endif
364c1ac89b1Seric 
365c1ac89b1Seric 			/* close any random open files in the envelope */
366c1ac89b1Seric 			if (e->e_dfp != NULL)
367c1ac89b1Seric 			{
368c1ac89b1Seric 				(void) xfclose(e->e_dfp, "sendenvelope", "dfp");
369c1ac89b1Seric 				e->e_dfp = NULL;
370c1ac89b1Seric 			}
371c1ac89b1Seric 			if (e->e_xfp != NULL)
372c1ac89b1Seric 			{
373c1ac89b1Seric 				(void) xfclose(e->e_xfp, "sendenvelope", "xfp");
374c1ac89b1Seric 				e->e_xfp = NULL;
375c1ac89b1Seric 			}
376c1ac89b1Seric 			return;
377c1ac89b1Seric 		}
378c1ac89b1Seric 
379c1ac89b1Seric 		/* double fork to avoid zombies */
380c1ac89b1Seric 		if (fork() > 0)
381c1ac89b1Seric 			exit(EX_OK);
382c1ac89b1Seric 
383c1ac89b1Seric 		/* be sure we are immune from the terminal */
3847880f3e4Seric 		disconnect(1, e);
385c1ac89b1Seric 
3862b9178d3Seric # ifndef HASFLOCK
387c1ac89b1Seric 		/*
388c1ac89b1Seric 		**  Now try to get our lock back.
389c1ac89b1Seric 		*/
390c1ac89b1Seric 
391c1ac89b1Seric 		lfd.l_type = F_WRLCK;
392c1ac89b1Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
393c1ac89b1Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
394c1ac89b1Seric 		if (e->e_lockfp == NULL ||
395c1ac89b1Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
396c1ac89b1Seric 		{
397c1ac89b1Seric 			/* oops....  lost it */
398c1ac89b1Seric 			if (tTd(13, 1))
399c1ac89b1Seric 				printf("sendenvelope: %s lost lock: lockfp=%x, %s\n",
400c1ac89b1Seric 					e->e_id, e->e_lockfp, errstring(errno));
401c1ac89b1Seric 
402c1ac89b1Seric # ifdef LOG
403c1ac89b1Seric 			if (LogLevel > 29)
404c1ac89b1Seric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
405c1ac89b1Seric 					e->e_id);
406c1ac89b1Seric # endif /* LOG */
407c1ac89b1Seric 			exit(EX_OK);
408c1ac89b1Seric 		}
4092b9178d3Seric # endif /* HASFLOCK */
410c1ac89b1Seric 
411c1ac89b1Seric 		/*
412c1ac89b1Seric 		**  Close any cached connections.
413c1ac89b1Seric 		**
414c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
415c1ac89b1Seric 		**	still knows about the connection.
416c1ac89b1Seric 		**
417c1ac89b1Seric 		**	This should only happen when delivering an error
418c1ac89b1Seric 		**	message.
419c1ac89b1Seric 		*/
420c1ac89b1Seric 
421c1ac89b1Seric 		mci_flush(FALSE, NULL);
422c1ac89b1Seric 
423c1ac89b1Seric 		break;
424c1ac89b1Seric 	}
425c1ac89b1Seric 
426c1ac89b1Seric 	/*
4279c9e68d9Seric 	**  Run through the list and send everything.
4285288e21aSeric 	**
4295288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4305288e21aSeric 	**	result in returned mail.
4319c9e68d9Seric 	*/
4329c9e68d9Seric 
4339c9e68d9Seric 	e->e_nsent = 0;
4345288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
4359c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4369c9e68d9Seric 	{
4379c9e68d9Seric 		if (mode == SM_VERIFY)
4389c9e68d9Seric 		{
4399c9e68d9Seric 			e->e_to = q->q_paddr;
4409c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4418dfca105Seric 			{
4428dfca105Seric 				message("deliverable: mailer %s, host %s, user %s",
4438dfca105Seric 					q->q_mailer->m_name,
4448dfca105Seric 					q->q_host,
4458dfca105Seric 					q->q_user);
4468dfca105Seric 			}
4479c9e68d9Seric 		}
4489c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4499c9e68d9Seric 		{
4509c9e68d9Seric # ifdef QUEUE
4519c9e68d9Seric 			/*
4529c9e68d9Seric 			**  Checkpoint the send list every few addresses
4539c9e68d9Seric 			*/
4549c9e68d9Seric 
4559c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4569c9e68d9Seric 			{
4579c9e68d9Seric 				queueup(e, TRUE, FALSE);
4589c9e68d9Seric 				e->e_nsent = 0;
4599c9e68d9Seric 			}
4609c9e68d9Seric # endif /* QUEUE */
4619c9e68d9Seric 			(void) deliver(e, q);
4629c9e68d9Seric 		}
4639c9e68d9Seric 	}
4649c9e68d9Seric 	Verbose = oldverbose;
4659c9e68d9Seric 
4669c9e68d9Seric 	if (mode == SM_FORK)
4679c9e68d9Seric 		finis();
4689c9e68d9Seric }
4699c9e68d9Seric /*
4709c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4719c9e68d9Seric **
4729c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4739c9e68d9Seric **	two processes on the same stack!!!
4749c9e68d9Seric **
4759c9e68d9Seric **	Parameters:
4769c9e68d9Seric **		none.
4779c9e68d9Seric **
4789c9e68d9Seric **	Returns:
4799c9e68d9Seric **		From a macro???  You've got to be kidding!
4809c9e68d9Seric **
4819c9e68d9Seric **	Side Effects:
4829c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4839c9e68d9Seric **			pid of child in parent, zero in child.
4849c9e68d9Seric **			-1 on unrecoverable error.
4859c9e68d9Seric **
4869c9e68d9Seric **	Notes:
4879c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4889c9e68d9Seric **		vfork for you.....
4899c9e68d9Seric */
4909c9e68d9Seric 
4919c9e68d9Seric # define NFORKTRIES	5
4929c9e68d9Seric 
4939c9e68d9Seric # ifndef FORK
4949c9e68d9Seric # define FORK	fork
4959c9e68d9Seric # endif
4969c9e68d9Seric 
4979c9e68d9Seric # define DOFORK(fORKfN) \
4989c9e68d9Seric {\
4999c9e68d9Seric 	register int i;\
5009c9e68d9Seric \
5019c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
5029c9e68d9Seric 	{\
5039c9e68d9Seric 		pid = fORKfN();\
5049c9e68d9Seric 		if (pid >= 0)\
5059c9e68d9Seric 			break;\
5069c9e68d9Seric 		if (i > 0)\
5079c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5089c9e68d9Seric 	}\
5099c9e68d9Seric }
5109c9e68d9Seric /*
5119c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5129c9e68d9Seric **
5139c9e68d9Seric **	Parameters:
5149c9e68d9Seric **		none.
5159c9e68d9Seric **
5169c9e68d9Seric **	Returns:
5179c9e68d9Seric **		pid of child in parent.
5189c9e68d9Seric **		zero in child.
5199c9e68d9Seric **		-1 on error.
5209c9e68d9Seric **
5219c9e68d9Seric **	Side Effects:
5229c9e68d9Seric **		returns twice, once in parent and once in child.
5239c9e68d9Seric */
5249c9e68d9Seric 
5259c9e68d9Seric dofork()
5269c9e68d9Seric {
5279c9e68d9Seric 	register int pid;
5289c9e68d9Seric 
5299c9e68d9Seric 	DOFORK(fork);
5309c9e68d9Seric 	return (pid);
5319c9e68d9Seric }
5329c9e68d9Seric /*
53313bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
53413bbc08cSeric **
53513bbc08cSeric **	This routine delivers to everyone on the same host as the
53613bbc08cSeric **	user on the head of the list.  It is clever about mailers
53713bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
53813bbc08cSeric **	that it will deliver to all these addresses however -- so
53913bbc08cSeric **	deliver should be called once for each address on the
54013bbc08cSeric **	list.
54125a99e2eSeric **
54225a99e2eSeric **	Parameters:
543588cad61Seric **		e -- the envelope to deliver.
544c77d1c25Seric **		firstto -- head of the address list to deliver to.
54525a99e2eSeric **
54625a99e2eSeric **	Returns:
54725a99e2eSeric **		zero -- successfully delivered.
54825a99e2eSeric **		else -- some failure, see ExitStat for more info.
54925a99e2eSeric **
55025a99e2eSeric **	Side Effects:
55125a99e2eSeric **		The standard input is passed off to someone.
55225a99e2eSeric */
55325a99e2eSeric 
554588cad61Seric deliver(e, firstto)
555588cad61Seric 	register ENVELOPE *e;
556c77d1c25Seric 	ADDRESS *firstto;
55725a99e2eSeric {
55878442df3Seric 	char *host;			/* host being sent to */
55978442df3Seric 	char *user;			/* user being sent to */
56025a99e2eSeric 	char **pvp;
5615dfc646bSeric 	register char **mvp;
56225a99e2eSeric 	register char *p;
563588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5646259796dSeric 	ADDRESS *ctladdr;
565b31e7f2bSeric 	register MCI *mci;
566c77d1c25Seric 	register ADDRESS *to = firstto;
567c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
568772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
569911693bfSbostic 	int rcode;			/* response code */
570e103b48fSeric 	char *firstsig;			/* signature of firstto */
5719c9e68d9Seric 	int pid;
5729c9e68d9Seric 	char *curhost;
5739c9e68d9Seric 	int mpvect[2];
5749c9e68d9Seric 	int rpvect[2];
575ee6bf8dfSeric 	char *pv[MAXPV+1];
576579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
577ee6bf8dfSeric 	char buf[MAXNAME];
578c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
579fabb3bd4Seric 	extern int checkcompat();
5809c9e68d9Seric 	extern FILE *fdopen();
58125a99e2eSeric 
58235490626Seric 	errno = 0;
583ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5845dfc646bSeric 		return (0);
58525a99e2eSeric 
586134746fbSeric #ifdef NAMED_BIND
587912a731aSbostic 	/* unless interactive, try twice, over a minute */
588912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
589912a731aSbostic 		_res.retrans = 30;
590912a731aSbostic 		_res.retry = 2;
591912a731aSbostic 	}
592d4bd8f0eSbostic #endif
593912a731aSbostic 
59451552439Seric 	m = to->q_mailer;
59551552439Seric 	host = to->q_host;
596c9be6216Seric 	CurEnv = e;			/* just in case */
5974384d521Seric 	e->e_statmsg = NULL;
59851552439Seric 
5996ef48975Seric 	if (tTd(10, 1))
6005dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
60151552439Seric 			m->m_mno, host, to->q_user);
602f3dbc832Seric 
603f3dbc832Seric 	/*
604f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
605f3dbc832Seric 	**  connections now, just mark these addresses and return.
606f3dbc832Seric 	**	This is useful if we want to batch connections to
607f3dbc832Seric 	**	reduce load.  This will cause the messages to be
608f3dbc832Seric 	**	queued up, and a daemon will come along to send the
609f3dbc832Seric 	**	messages later.
610f3dbc832Seric 	**		This should be on a per-mailer basis.
611f3dbc832Seric 	*/
612f3dbc832Seric 
61319c47125Seric 	if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) &&
61419c47125Seric 	    bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
615f3dbc832Seric 	{
616f3dbc832Seric 		for (; to != NULL; to = to->q_next)
617f4560e80Seric 		{
618ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
619c6e18ac5Seric 			    to->q_mailer != m)
620f4560e80Seric 				continue;
621268a25e1Seric 			to->q_flags |= QQUEUEUP;
622588cad61Seric 			e->e_to = to->q_paddr;
62308b25121Seric 			message("queued");
6242f624c86Seric 			if (LogLevel > 8)
62581161401Seric 				logdelivery(m, NULL, "queued", e);
626f4560e80Seric 		}
627588cad61Seric 		e->e_to = NULL;
628f3dbc832Seric 		return (0);
629f3dbc832Seric 	}
630f3dbc832Seric 
63125a99e2eSeric 	/*
6325dfc646bSeric 	**  Do initial argv setup.
6335dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6345dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6355dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6365dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6375dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6383bea8136Seric 	**		The from address rewrite is expected to make
6393bea8136Seric 	**		the address relative to the other end.
6405dfc646bSeric 	*/
6415dfc646bSeric 
64278442df3Seric 	/* rewrite from address, using rewriting rules */
643efe54562Seric 	rcode = EX_OK;
644efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
645efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
646efe54562Seric 					   &rcode, e));
647ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
648588cad61Seric 	define('h', host, e);			/* to host */
6495dfc646bSeric 	Errors = 0;
6505dfc646bSeric 	pvp = pv;
6515dfc646bSeric 	*pvp++ = m->m_argv[0];
6525dfc646bSeric 
6535dfc646bSeric 	/* insert -f or -r flag as appropriate */
65457fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6555dfc646bSeric 	{
65657fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6575dfc646bSeric 			*pvp++ = "-f";
6585dfc646bSeric 		else
6595dfc646bSeric 			*pvp++ = "-r";
660c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6615dfc646bSeric 	}
6625dfc646bSeric 
6635dfc646bSeric 	/*
6645dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6655dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6665dfc646bSeric 	**  be one of these, and there are only a few more slots
6675dfc646bSeric 	**  in the pv after it.
6685dfc646bSeric 	*/
6695dfc646bSeric 
6705dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6715dfc646bSeric 	{
6722bc47524Seric 		/* can't use strchr here because of sign extension problems */
6732bc47524Seric 		while (*p != '\0')
6742bc47524Seric 		{
6752bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6762bc47524Seric 			{
6772bc47524Seric 				if (*p == 'u')
6785dfc646bSeric 					break;
6792bc47524Seric 			}
6802bc47524Seric 		}
6812bc47524Seric 
6822bc47524Seric 		if (*p != '\0')
6835dfc646bSeric 			break;
6845dfc646bSeric 
6855dfc646bSeric 		/* this entry is safe -- go ahead and process it */
686588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6875dfc646bSeric 		*pvp++ = newstr(buf);
6885dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6895dfc646bSeric 		{
69008b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6915dfc646bSeric 			return (-1);
6925dfc646bSeric 		}
6935dfc646bSeric 	}
694c579ef51Seric 
69533db8731Seric 	/*
69633db8731Seric 	**  If we have no substitution for the user name in the argument
69733db8731Seric 	**  list, we know that we must supply the names otherwise -- and
69833db8731Seric 	**  SMTP is the answer!!
69933db8731Seric 	*/
70033db8731Seric 
7015dfc646bSeric 	if (*mvp == NULL)
702c579ef51Seric 	{
703c579ef51Seric 		/* running SMTP */
7042c7e1b8dSeric # ifdef SMTP
705c579ef51Seric 		clever = TRUE;
706c579ef51Seric 		*pvp = NULL;
7076c2c3107Seric # else /* SMTP */
70833db8731Seric 		/* oops!  we don't implement SMTP */
70908b25121Seric 		syserr("554 SMTP style mailer");
7102c7e1b8dSeric 		return (EX_SOFTWARE);
7116c2c3107Seric # endif /* SMTP */
712c579ef51Seric 	}
7135dfc646bSeric 
7145dfc646bSeric 	/*
7155dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7165dfc646bSeric 	**  run through our address list and append all the addresses
7175dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7185dfc646bSeric 	**  always send another copy later.
7195dfc646bSeric 	*/
7205dfc646bSeric 
7215dfc646bSeric 	tobuf[0] = '\0';
722588cad61Seric 	e->e_to = tobuf;
7236259796dSeric 	ctladdr = NULL;
724e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7255dfc646bSeric 	for (; to != NULL; to = to->q_next)
7265dfc646bSeric 	{
7275dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
72857fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7295dfc646bSeric 			break;
7305dfc646bSeric 
7315dfc646bSeric 		/* if already sent or not for this host, don't send */
732ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
733e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
734e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7355dfc646bSeric 			continue;
7366259796dSeric 
7374b22ea87Seric 		/* avoid overflowing tobuf */
738aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7394b22ea87Seric 			break;
7404b22ea87Seric 
7416ef48975Seric 		if (tTd(10, 1))
742772e6e50Seric 		{
743772e6e50Seric 			printf("\nsend to ");
744772e6e50Seric 			printaddr(to, FALSE);
745772e6e50Seric 		}
746772e6e50Seric 
7476259796dSeric 		/* compute effective uid/gid when sending */
7487da1035fSeric 		if (to->q_mailer == ProgMailer)
7496259796dSeric 			ctladdr = getctladdr(to);
7506259796dSeric 
7515dfc646bSeric 		user = to->q_user;
752588cad61Seric 		e->e_to = to->q_paddr;
75375f1ade9Seric 		if (tTd(10, 5))
75475f1ade9Seric 		{
75575f1ade9Seric 			printf("deliver: QDONTSEND ");
75675f1ade9Seric 			printaddr(to, FALSE);
75775f1ade9Seric 		}
758ee4b0922Seric 		to->q_flags |= QDONTSEND;
7595dfc646bSeric 
7605dfc646bSeric 		/*
7615dfc646bSeric 		**  Check to see that these people are allowed to
7625dfc646bSeric 		**  talk to each other.
7632a6e0786Seric 		*/
7642a6e0786Seric 
76569582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
76669582d2fSeric 		{
76769582d2fSeric 			NoReturn = TRUE;
76808b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
76981161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
77069582d2fSeric 			continue;
77169582d2fSeric 		}
772fabb3bd4Seric 		rcode = checkcompat(to, e);
7731793c9c5Seric 		if (rcode != EX_OK)
7745dfc646bSeric 		{
7751000c37aSeric 			markfailure(e, to, rcode);
77681161401Seric 			giveresponse(rcode, m, NULL, e);
7775dfc646bSeric 			continue;
7785dfc646bSeric 		}
7792a6e0786Seric 
7802a6e0786Seric 		/*
7819ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7829ec9501bSeric 		**	about them.
78325a99e2eSeric 		*/
78425a99e2eSeric 
78557fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
78625a99e2eSeric 		{
7871d8f1806Seric 			stripquotes(user);
7881d8f1806Seric 			stripquotes(host);
78925a99e2eSeric 		}
79025a99e2eSeric 
791cdb828c5Seric 		/* hack attack -- delivermail compatibility */
792cdb828c5Seric 		if (m == ProgMailer && *user == '|')
793cdb828c5Seric 			user++;
794cdb828c5Seric 
79525a99e2eSeric 		/*
7963efaed6eSeric 		**  If an error message has already been given, don't
7973efaed6eSeric 		**	bother to send to this address.
7983efaed6eSeric 		**
7993efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
8003efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
8013efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
8023efaed6eSeric 		*/
8033efaed6eSeric 
8046cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8053efaed6eSeric 			continue;
8063efaed6eSeric 
807f2fec898Seric 		/* save statistics.... */
808588cad61Seric 		markstats(e, to);
809f2fec898Seric 
8103efaed6eSeric 		/*
81125a99e2eSeric 		**  See if this user name is "special".
81225a99e2eSeric 		**	If the user name has a slash in it, assume that this
81351552439Seric 		**	is a file -- send it off without further ado.  Note
81451552439Seric 		**	that this type of addresses is not processed along
81551552439Seric 		**	with the others, so we fudge on the To person.
81625a99e2eSeric 		*/
81725a99e2eSeric 
8182c017f8dSeric 		if (m == FileMailer)
81925a99e2eSeric 		{
820b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
82181161401Seric 			giveresponse(rcode, m, NULL, e);
822dde5acadSeric 			if (rcode == EX_OK)
823dde5acadSeric 				to->q_flags |= QSENT;
8245dfc646bSeric 			continue;
82525a99e2eSeric 		}
82625a99e2eSeric 
82713bbc08cSeric 		/*
82813bbc08cSeric 		**  Address is verified -- add this user to mailer
82913bbc08cSeric 		**  argv, and add it to the print list of recipients.
83013bbc08cSeric 		*/
83113bbc08cSeric 
832508daeccSeric 		/* link together the chain of recipients */
833508daeccSeric 		to->q_tchain = tochain;
834508daeccSeric 		tochain = to;
835508daeccSeric 
8365dfc646bSeric 		/* create list of users for error messages */
837db8841e9Seric 		(void) strcat(tobuf, ",");
838db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
839588cad61Seric 		define('u', user, e);		/* to user */
840588cad61Seric 		define('z', to->q_home, e);	/* user's home */
8415dfc646bSeric 
842c579ef51Seric 		/*
843508daeccSeric 		**  Expand out this user into argument list.
844c579ef51Seric 		*/
845c579ef51Seric 
846508daeccSeric 		if (!clever)
847c579ef51Seric 		{
848588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8495dfc646bSeric 			*pvp++ = newstr(buf);
8505dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8515dfc646bSeric 			{
8525dfc646bSeric 				/* allow some space for trailing parms */
8535dfc646bSeric 				break;
8545dfc646bSeric 			}
8555dfc646bSeric 		}
856c579ef51Seric 	}
8575dfc646bSeric 
858145b49b1Seric 	/* see if any addresses still exist */
859145b49b1Seric 	if (tobuf[0] == '\0')
860c579ef51Seric 	{
861588cad61Seric 		define('g', (char *) NULL, e);
862145b49b1Seric 		return (0);
863c579ef51Seric 	}
864145b49b1Seric 
8655dfc646bSeric 	/* print out messages as full list */
86663780dbdSeric 	e->e_to = tobuf + 1;
8675dfc646bSeric 
8685dfc646bSeric 	/*
8695dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8705dfc646bSeric 	*/
8715dfc646bSeric 
872c579ef51Seric 	while (!clever && *++mvp != NULL)
8735dfc646bSeric 	{
874588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8755dfc646bSeric 		*pvp++ = newstr(buf);
8765dfc646bSeric 		if (pvp >= &pv[MAXPV])
87708b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8785dfc646bSeric 	}
8795dfc646bSeric 	*pvp++ = NULL;
8805dfc646bSeric 
88125a99e2eSeric 	/*
88225a99e2eSeric 	**  Call the mailer.
8836328bdf7Seric 	**	The argument vector gets built, pipes
88425a99e2eSeric 	**	are created as necessary, and we fork & exec as
8856328bdf7Seric 	**	appropriate.
886c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
88725a99e2eSeric 	*/
88825a99e2eSeric 
8897ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
89086b26461Seric 		ctladdr = &e->e_from;
891134746fbSeric #ifdef NAMED_BIND
8922bcc6d2dSeric 	if (ConfigLevel < 2)
893912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
894134746fbSeric #endif
8959c9e68d9Seric 
8969c9e68d9Seric 	if (tTd(11, 1))
897134746fbSeric 	{
8989c9e68d9Seric 		printf("openmailer:");
8999c9e68d9Seric 		printav(pv);
9009c9e68d9Seric 	}
9019c9e68d9Seric 	errno = 0;
9029c9e68d9Seric 
9039c9e68d9Seric 	CurHostName = m->m_mailer;
9049c9e68d9Seric 
9059c9e68d9Seric 	/*
9069c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9079c9e68d9Seric 	**  connection.
9089c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9099c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9109c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9119c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9129c9e68d9Seric 	*/
9139c9e68d9Seric 
9149c9e68d9Seric 	curhost = NULL;
9159c9e68d9Seric 
9169c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9179c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9189c9e68d9Seric 	{
9199c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9209c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9219c9e68d9Seric 		mci->mci_in = stdin;
9229c9e68d9Seric 		mci->mci_out = stdout;
9239c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9249c9e68d9Seric 		mci->mci_mailer = m;
9259c9e68d9Seric 	}
9269c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9279c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9289c9e68d9Seric 	{
9299c9e68d9Seric #ifdef DAEMON
9309c9e68d9Seric 		register int i;
9319c9e68d9Seric 		register u_short port;
9329c9e68d9Seric 
9339c9e68d9Seric 		CurHostName = pv[1];
9349c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9359c9e68d9Seric 
9369c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9379c9e68d9Seric 		{
9389c9e68d9Seric 			syserr("null signature");
939845e533cSeric 			rcode = EX_OSERR;
940b31e7f2bSeric 			goto give_up;
941b31e7f2bSeric 		}
9429c9e68d9Seric 
9439c9e68d9Seric 		if (!clever)
9449c9e68d9Seric 		{
9459c9e68d9Seric 			syserr("554 non-clever IPC");
9469c9e68d9Seric 			rcode = EX_OSERR;
9479c9e68d9Seric 			goto give_up;
9489c9e68d9Seric 		}
9499c9e68d9Seric 		if (pv[2] != NULL)
9509c9e68d9Seric 			port = atoi(pv[2]);
9519c9e68d9Seric 		else
9529c9e68d9Seric 			port = 0;
9539c9e68d9Seric tryhost:
9549c9e68d9Seric 		mci = NULL;
9559c9e68d9Seric 		while (*curhost != '\0')
9569c9e68d9Seric 		{
9579c9e68d9Seric 			register char *p;
9589c9e68d9Seric 			static char hostbuf[MAXNAME];
9599c9e68d9Seric 
9609c9e68d9Seric 			mci = NULL;
9619c9e68d9Seric 
9629c9e68d9Seric 			/* pull the next host from the signature */
9639c9e68d9Seric 			p = strchr(curhost, ':');
9649c9e68d9Seric 			if (p == NULL)
9659c9e68d9Seric 				p = &curhost[strlen(curhost)];
9669c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9679c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9689c9e68d9Seric 			if (*p != '\0')
9699c9e68d9Seric 				p++;
9709c9e68d9Seric 			curhost = p;
9719c9e68d9Seric 
9729c9e68d9Seric 			/* see if we already know that this host is fried */
9739c9e68d9Seric 			CurHostName = hostbuf;
9749c9e68d9Seric 			mci = mci_get(hostbuf, m);
9759c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9769c9e68d9Seric 			{
9779c9e68d9Seric 				if (tTd(11, 1))
9789c9e68d9Seric 				{
9799c9e68d9Seric 					printf("openmailer: ");
9809c9e68d9Seric 					mci_dump(mci);
9819c9e68d9Seric 				}
9829c9e68d9Seric 				CurHostName = mci->mci_host;
9839c9e68d9Seric 				break;
9849c9e68d9Seric 			}
9859c9e68d9Seric 			mci->mci_mailer = m;
9869c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9879c9e68d9Seric 				continue;
9889c9e68d9Seric 
9899c9e68d9Seric 			/* try the connection */
9909c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9919c9e68d9Seric 			message("Connecting to %s (%s)...",
9929c9e68d9Seric 				hostbuf, m->m_name);
9939c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
9949c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
9959c9e68d9Seric 			mci->mci_exitstat = i;
9969c9e68d9Seric 			mci->mci_errno = errno;
997f170942cSeric #ifdef NAMED_BIND
998f170942cSeric 			mci->mci_herrno = h_errno;
999f170942cSeric #endif
10009c9e68d9Seric 			if (i == EX_OK)
10019c9e68d9Seric 			{
10029c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10039c9e68d9Seric 				mci_cache(mci);
1004f170942cSeric 				if (TrafficLogFile != NULL)
1005f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1006f170942cSeric 						getpid(), hostbuf);
10079c9e68d9Seric 				break;
10089c9e68d9Seric 			}
10099c9e68d9Seric 			else if (tTd(11, 1))
10109c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10119c9e68d9Seric 					i, errno);
10129c9e68d9Seric 
10139c9e68d9Seric 
10149c9e68d9Seric 			/* enter status of this host */
10159c9e68d9Seric 			setstat(i);
10169c9e68d9Seric 		}
10179c9e68d9Seric 		mci->mci_pid = 0;
10189c9e68d9Seric #else /* no DAEMON */
10199c9e68d9Seric 		syserr("554 openmailer: no IPC");
10209c9e68d9Seric 		if (tTd(11, 1))
10219c9e68d9Seric 			printf("openmailer: NULL\n");
10229c9e68d9Seric 		return NULL;
10239c9e68d9Seric #endif /* DAEMON */
10249c9e68d9Seric 	}
10259c9e68d9Seric 	else
10269c9e68d9Seric 	{
1027f170942cSeric #ifdef XDEBUG
1028f170942cSeric 		char wbuf[MAXLINE];
10296fe8c3bcSeric 
10306fe8c3bcSeric 		/* make absolutely certain 0, 1, and 2 are in use */
1031f170942cSeric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
1032f170942cSeric 		checkfd012(wbuf);
1033f170942cSeric #endif
10340e3bfef5Seric 
1035f170942cSeric 		if (TrafficLogFile != NULL)
10360e3bfef5Seric 		{
1037f170942cSeric 			char **av;
1038f170942cSeric 
1039f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1040f170942cSeric 			for (av = pv; *av != NULL; av++)
1041f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1042f170942cSeric 			fprintf(TrafficLogFile, "\n");
10436fe8c3bcSeric 		}
10446fe8c3bcSeric 
10459c9e68d9Seric 		/* create a pipe to shove the mail through */
10469c9e68d9Seric 		if (pipe(mpvect) < 0)
10479c9e68d9Seric 		{
10480e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10490e3bfef5Seric 				e->e_to, m->m_name);
10509c9e68d9Seric 			if (tTd(11, 1))
10519c9e68d9Seric 				printf("openmailer: NULL\n");
10529c9e68d9Seric 			rcode = EX_OSERR;
10539c9e68d9Seric 			goto give_up;
10549c9e68d9Seric 		}
10559c9e68d9Seric 
10569c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10579c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10589c9e68d9Seric 		{
10590e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10600e3bfef5Seric 				e->e_to, m->m_name);
10619c9e68d9Seric 			(void) close(mpvect[0]);
10629c9e68d9Seric 			(void) close(mpvect[1]);
10639c9e68d9Seric 			if (tTd(11, 1))
10649c9e68d9Seric 				printf("openmailer: NULL\n");
10659c9e68d9Seric 			rcode = EX_OSERR;
10669c9e68d9Seric 			goto give_up;
10679c9e68d9Seric 		}
10689c9e68d9Seric 
10699c9e68d9Seric 		/*
10709c9e68d9Seric 		**  Actually fork the mailer process.
10719c9e68d9Seric 		**	DOFORK is clever about retrying.
10729c9e68d9Seric 		**
10739c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10749c9e68d9Seric 		**	around so that endmail will get it.
10759c9e68d9Seric 		*/
10769c9e68d9Seric 
10779c9e68d9Seric 		if (e->e_xfp != NULL)
10789c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10799c9e68d9Seric 		(void) fflush(stdout);
10809c9e68d9Seric # ifdef SIGCHLD
10812b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
10829c9e68d9Seric # endif /* SIGCHLD */
10839c9e68d9Seric 		DOFORK(FORK);
10849c9e68d9Seric 		/* pid is set by DOFORK */
10859c9e68d9Seric 		if (pid < 0)
10869c9e68d9Seric 		{
10879c9e68d9Seric 			/* failure */
10880e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
10890e3bfef5Seric 				e->e_to, m->m_name);
10909c9e68d9Seric 			(void) close(mpvect[0]);
10919c9e68d9Seric 			(void) close(mpvect[1]);
10929c9e68d9Seric 			if (clever)
10939c9e68d9Seric 			{
10949c9e68d9Seric 				(void) close(rpvect[0]);
10959c9e68d9Seric 				(void) close(rpvect[1]);
10969c9e68d9Seric 			}
10979c9e68d9Seric 			if (tTd(11, 1))
10989c9e68d9Seric 				printf("openmailer: NULL\n");
10999c9e68d9Seric 			rcode = EX_OSERR;
11009c9e68d9Seric 			goto give_up;
11019c9e68d9Seric 		}
11029c9e68d9Seric 		else if (pid == 0)
11039c9e68d9Seric 		{
11049c9e68d9Seric 			int i;
11059c9e68d9Seric 			int saveerrno;
11069c9e68d9Seric 			char **ep;
11079c9e68d9Seric 			char *env[MAXUSERENVIRON];
11089c9e68d9Seric 			extern char **environ;
11099c9e68d9Seric 			extern int DtableSize;
11109c9e68d9Seric 
11119c9e68d9Seric 			/* child -- set up input & exec mailer */
11122b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
11132b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
11142b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11159c9e68d9Seric 
11169c9e68d9Seric 			/* close any other cached connections */
11179c9e68d9Seric 			mci_flush(FALSE, mci);
11189c9e68d9Seric 
1119*44f2317fSeric 			/* reset user and group */
1120*44f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
1121*44f2317fSeric 			{
1122*44f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
1123*44f2317fSeric 				{
1124*44f2317fSeric 					(void) setgid(DefGid);
1125*44f2317fSeric 					(void) initgroups(DefUser, DefGid);
1126*44f2317fSeric 					(void) setuid(DefUid);
1127*44f2317fSeric 				}
1128*44f2317fSeric 				else
1129*44f2317fSeric 				{
1130*44f2317fSeric 					(void) setgid(ctladdr->q_gid);
1131*44f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
1132*44f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
1133*44f2317fSeric 						ctladdr->q_gid);
1134*44f2317fSeric 					(void) setuid(ctladdr->q_uid);
1135*44f2317fSeric 				}
1136*44f2317fSeric 			}
1137*44f2317fSeric 
1138*44f2317fSeric 			if (tTd(11, 2))
1139*44f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
1140*44f2317fSeric 					getuid(), geteuid());
1141*44f2317fSeric 
1142b986f6aaSeric 			/* move into some "safe" directory */
1143b986f6aaSeric 			if (m->m_execdir != NULL)
1144b986f6aaSeric 			{
1145b986f6aaSeric 				char *p, *q;
1146b986f6aaSeric 				char buf[MAXLINE];
1147b986f6aaSeric 
1148b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1149b986f6aaSeric 				{
1150b986f6aaSeric 					q = strchr(p, ':');
1151b986f6aaSeric 					if (q != NULL)
1152b986f6aaSeric 						*q = '\0';
1153b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1154b986f6aaSeric 					if (q != NULL)
1155b986f6aaSeric 						*q++ = ':';
1156b986f6aaSeric 					if (tTd(11, 20))
1157b986f6aaSeric 						printf("openmailer: trydir %s\n",
1158b986f6aaSeric 							buf);
1159b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1160b986f6aaSeric 						break;
1161b986f6aaSeric 				}
1162b986f6aaSeric 			}
1163b986f6aaSeric 
11649c9e68d9Seric 			/* arrange to filter std & diag output of command */
11659c9e68d9Seric 			if (clever)
11669c9e68d9Seric 			{
11679c9e68d9Seric 				(void) close(rpvect[0]);
11686fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11696fe8c3bcSeric 				{
11700e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11710e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11726fe8c3bcSeric 					_exit(EX_OSERR);
11736fe8c3bcSeric 				}
11749c9e68d9Seric 				(void) close(rpvect[1]);
11759c9e68d9Seric 			}
11769c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11779c9e68d9Seric 			{
11789c9e68d9Seric 				/* put mailer output in transcript */
11796fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11806fe8c3bcSeric 				{
11810e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11820e3bfef5Seric 						e->e_to, m->m_name,
11836fe8c3bcSeric 						fileno(e->e_xfp));
11846fe8c3bcSeric 					_exit(EX_OSERR);
11859c9e68d9Seric 				}
11866fe8c3bcSeric 			}
11876fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11886fe8c3bcSeric 			{
11890e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
11900e3bfef5Seric 					e->e_to, m->m_name);
11916fe8c3bcSeric 				_exit(EX_OSERR);
11926fe8c3bcSeric 			}
11939c9e68d9Seric 
11949c9e68d9Seric 			/* arrange to get standard input */
11959c9e68d9Seric 			(void) close(mpvect[1]);
11969c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11979c9e68d9Seric 			{
11980e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
11990e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
12009c9e68d9Seric 				_exit(EX_OSERR);
12019c9e68d9Seric 			}
12029c9e68d9Seric 			(void) close(mpvect[0]);
12039c9e68d9Seric 
12049c9e68d9Seric 			/* arrange for all the files to be closed */
12059c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12069c9e68d9Seric 			{
12079c9e68d9Seric 				register int j;
1208*44f2317fSeric 
12099c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12109c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
12119c9e68d9Seric 			}
12129c9e68d9Seric 
12139c9e68d9Seric 			/* set up the mailer environment */
12149c9e68d9Seric 			i = 0;
12159c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12169c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12179c9e68d9Seric 			{
12189c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12199c9e68d9Seric 					env[i++] = *ep;
12209c9e68d9Seric 			}
12219c9e68d9Seric 			env[i++] = NULL;
12229c9e68d9Seric 
12239c9e68d9Seric 			/* try to execute the mailer */
12249c9e68d9Seric 			execve(m->m_mailer, pv, env);
12259c9e68d9Seric 			saveerrno = errno;
12269c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12277c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12287c941fd2Seric 				_exit(EX_OSERR);
12299c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12309c9e68d9Seric 		}
12319c9e68d9Seric 
12329c9e68d9Seric 		/*
12339c9e68d9Seric 		**  Set up return value.
12349c9e68d9Seric 		*/
12359c9e68d9Seric 
12369c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12379c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12389c9e68d9Seric 		mci->mci_mailer = m;
12399c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12409c9e68d9Seric 		mci->mci_pid = pid;
12419c9e68d9Seric 		(void) close(mpvect[0]);
12429c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
12439c9e68d9Seric 		if (clever)
12449c9e68d9Seric 		{
12459c9e68d9Seric 			(void) close(rpvect[1]);
12469c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
12479c9e68d9Seric 		}
12489c9e68d9Seric 		else
12499c9e68d9Seric 		{
12509c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12519c9e68d9Seric 			mci->mci_in = NULL;
12529c9e68d9Seric 		}
12539c9e68d9Seric 	}
12549c9e68d9Seric 
12559c9e68d9Seric 	/*
12569c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12579c9e68d9Seric 	*/
12589c9e68d9Seric 
12599c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12609c9e68d9Seric 	{
12619c9e68d9Seric 		smtpinit(m, mci, e);
12629c9e68d9Seric 	}
12639c9e68d9Seric 	if (tTd(11, 1))
12649c9e68d9Seric 	{
12659c9e68d9Seric 		printf("openmailer: ");
12669c9e68d9Seric 		mci_dump(mci);
12679c9e68d9Seric 	}
12689c9e68d9Seric 
12699c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1270b31e7f2bSeric 	{
1271b31e7f2bSeric 		/* couldn't open the mailer */
1272b31e7f2bSeric 		rcode = mci->mci_exitstat;
12732a6bc25bSeric 		errno = mci->mci_errno;
1274f170942cSeric #ifdef NAMED_BIND
1275f170942cSeric 		h_errno = mci->mci_herrno;
1276f170942cSeric #endif
1277b31e7f2bSeric 		if (rcode == EX_OK)
1278b31e7f2bSeric 		{
1279b31e7f2bSeric 			/* shouldn't happen */
128008b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12816b0f339dSeric 				rcode, mci->mci_state, firstsig);
1282b31e7f2bSeric 			rcode = EX_SOFTWARE;
1283b31e7f2bSeric 		}
1284e9277e33Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
128590891494Seric 		{
128690891494Seric 			/* try next MX site */
128790891494Seric 			goto tryhost;
128890891494Seric 		}
1289b31e7f2bSeric 	}
1290b31e7f2bSeric 	else if (!clever)
1291b31e7f2bSeric 	{
1292b31e7f2bSeric 		/*
1293b31e7f2bSeric 		**  Format and send message.
1294b31e7f2bSeric 		*/
129515d084d5Seric 
1296b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1297b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1298b31e7f2bSeric 		putline("\n", mci->mci_out, m);
129903c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1300b31e7f2bSeric 
1301b31e7f2bSeric 		/* get the exit status */
1302c9be6216Seric 		rcode = endmailer(mci, e, pv);
1303134746fbSeric 	}
1304134746fbSeric 	else
1305b31e7f2bSeric #ifdef SMTP
1306134746fbSeric 	{
1307b31e7f2bSeric 		/*
1308b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1309b31e7f2bSeric 		*/
131015d084d5Seric 
1311b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1312b31e7f2bSeric 		if (rcode == EX_OK)
131375889e88Seric 		{
1314ded0d3daSkarels 			register char *t = tobuf;
1315ded0d3daSkarels 			register int i;
1316ded0d3daSkarels 
1317588cad61Seric 			/* send the recipient list */
131863780dbdSeric 			tobuf[0] = '\0';
131975889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
132075889e88Seric 			{
132163780dbdSeric 				e->e_to = to->q_paddr;
132215d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
132375889e88Seric 				{
132483b7ddc9Seric 					markfailure(e, to, i);
132581161401Seric 					giveresponse(i, m, mci, e);
132663780dbdSeric 				}
132775889e88Seric 				else
132875889e88Seric 				{
1329911693bfSbostic 					*t++ = ',';
1330b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1331b31e7f2bSeric 						continue;
1332588cad61Seric 				}
1333588cad61Seric 			}
1334588cad61Seric 
133563780dbdSeric 			/* now send the data */
133663780dbdSeric 			if (tobuf[0] == '\0')
1337b31e7f2bSeric 			{
13389c9e68d9Seric 				rcode = EX_OK;
133963780dbdSeric 				e->e_to = NULL;
1340b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1341b31e7f2bSeric 					smtprset(m, mci, e);
1342b31e7f2bSeric 			}
134375889e88Seric 			else
134475889e88Seric 			{
134563780dbdSeric 				e->e_to = tobuf + 1;
134675889e88Seric 				rcode = smtpdata(m, mci, e);
134763780dbdSeric 			}
134863780dbdSeric 
134963780dbdSeric 			/* now close the connection */
1350b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
135115d084d5Seric 				smtpquit(m, mci, e);
135263780dbdSeric 		}
13539c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13549c9e68d9Seric 		{
13559c9e68d9Seric 			/* try next MX site */
13569c9e68d9Seric 			goto tryhost;
13579c9e68d9Seric 		}
1358c579ef51Seric 	}
1359b31e7f2bSeric #else /* not SMTP */
1360a05b3449Sbostic 	{
136108b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1362845e533cSeric 		rcode = EX_CONFIG;
1363b31e7f2bSeric 		goto give_up;
1364a05b3449Sbostic 	}
1365b31e7f2bSeric #endif /* SMTP */
1366134746fbSeric #ifdef NAMED_BIND
13672bcc6d2dSeric 	if (ConfigLevel < 2)
1368912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1369134746fbSeric #endif
13705dfc646bSeric 
1371b31e7f2bSeric 	/* arrange a return receipt if requested */
13728c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1373b31e7f2bSeric 	{
1374b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1375b31e7f2bSeric 		/* do we want to send back more info? */
1376b31e7f2bSeric 	}
1377b31e7f2bSeric 
1378c77d1c25Seric 	/*
137963780dbdSeric 	**  Do final status disposal.
138063780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1381c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1382c77d1c25Seric 	**		addressees.
1383c77d1c25Seric 	*/
1384c77d1c25Seric 
1385b31e7f2bSeric   give_up:
138663780dbdSeric 	if (tobuf[0] != '\0')
138781161401Seric 		giveresponse(rcode, m, mci, e);
1388772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1389b31e7f2bSeric 	{
1390dde5acadSeric 		if (rcode != EX_OK)
139183b7ddc9Seric 			markfailure(e, to, rcode);
1392dde5acadSeric 		else
1393655518ecSeric 		{
1394dde5acadSeric 			to->q_flags |= QSENT;
1395655518ecSeric 			e->e_nsent++;
1396655518ecSeric 		}
1397b31e7f2bSeric 	}
1398b31e7f2bSeric 
1399b31e7f2bSeric 	/*
1400b31e7f2bSeric 	**  Restore state and return.
1401b31e7f2bSeric 	*/
1402c77d1c25Seric 
140335490626Seric 	errno = 0;
1404588cad61Seric 	define('g', (char *) NULL, e);
14055826d9d3Seric 	return (rcode);
140625a99e2eSeric }
14075dfc646bSeric /*
140883b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
140983b7ddc9Seric **
141083b7ddc9Seric **	Parameters:
141183b7ddc9Seric **		e -- the envelope we are sending.
141283b7ddc9Seric **		q -- the address to mark.
141383b7ddc9Seric **		rcode -- the code signifying the particular failure.
141483b7ddc9Seric **
141583b7ddc9Seric **	Returns:
141683b7ddc9Seric **		none.
141783b7ddc9Seric **
141883b7ddc9Seric **	Side Effects:
141983b7ddc9Seric **		marks the address (and possibly the envelope) with the
142083b7ddc9Seric **			failure so that an error will be returned or
142183b7ddc9Seric **			the message will be queued, as appropriate.
142283b7ddc9Seric */
142383b7ddc9Seric 
142483b7ddc9Seric markfailure(e, q, rcode)
142583b7ddc9Seric 	register ENVELOPE *e;
142683b7ddc9Seric 	register ADDRESS *q;
142783b7ddc9Seric 	int rcode;
142883b7ddc9Seric {
142919c47125Seric 	char buf[MAXLINE];
143019c47125Seric 
143183b7ddc9Seric 	if (rcode == EX_OK)
143283b7ddc9Seric 		return;
1433f170942cSeric 	else if (rcode == EX_TEMPFAIL)
143483b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1435f170942cSeric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
1436f170942cSeric 		q->q_flags |= QBADADDR;
143783b7ddc9Seric }
143883b7ddc9Seric /*
1439c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1440c579ef51Seric **
1441c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1442c579ef51Seric **	violation), so we report those specially.  For other
1443c579ef51Seric **	errors, we choose a status message (into statmsg),
1444c579ef51Seric **	and if it represents an error, we print it.
1445c579ef51Seric **
1446c579ef51Seric **	Parameters:
1447c579ef51Seric **		pid -- pid of mailer.
1448c9be6216Seric **		e -- the current envelope.
1449c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1450c9be6216Seric **			(for error messages).
1451c579ef51Seric **
1452c579ef51Seric **	Returns:
1453c579ef51Seric **		exit code of mailer.
1454c579ef51Seric **
1455c579ef51Seric **	Side Effects:
1456c579ef51Seric **		none.
1457c579ef51Seric */
1458c579ef51Seric 
1459c9be6216Seric endmailer(mci, e, pv)
1460b31e7f2bSeric 	register MCI *mci;
1461c9be6216Seric 	register ENVELOPE *e;
1462c9be6216Seric 	char **pv;
1463c579ef51Seric {
1464588cad61Seric 	int st;
1465c579ef51Seric 
146675889e88Seric 	/* close any connections */
146775889e88Seric 	if (mci->mci_in != NULL)
1468c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
146975889e88Seric 	if (mci->mci_out != NULL)
1470c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
147175889e88Seric 	mci->mci_in = mci->mci_out = NULL;
147275889e88Seric 	mci->mci_state = MCIS_CLOSED;
147375889e88Seric 
147433db8731Seric 	/* in the IPC case there is nothing to wait for */
147575889e88Seric 	if (mci->mci_pid == 0)
147633db8731Seric 		return (EX_OK);
147733db8731Seric 
147833db8731Seric 	/* wait for the mailer process to die and collect status */
147975889e88Seric 	st = waitfor(mci->mci_pid);
1480588cad61Seric 	if (st == -1)
148178de67c1Seric 	{
1482c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1483588cad61Seric 		return (EX_SOFTWARE);
1484c579ef51Seric 	}
148533db8731Seric 
148633db8731Seric 	/* see if it died a horrid death */
1487c579ef51Seric 	if ((st & 0377) != 0)
1488c579ef51Seric 	{
1489c9be6216Seric 		syserr("mailer %s died with signal %o", pv[0], st);
1490c9be6216Seric 
1491c9be6216Seric 		/* log the arguments */
1492c9be6216Seric 		if (e->e_xfp != NULL)
1493c9be6216Seric 		{
1494c9be6216Seric 			register char **av;
1495c9be6216Seric 
1496c9be6216Seric 			fprintf(e->e_xfp, "Arguments:");
1497c9be6216Seric 			for (av = pv; *av != NULL; av++)
1498c9be6216Seric 				fprintf(e->e_xfp, " %s", *av);
1499c9be6216Seric 			fprintf(e->e_xfp, "\n");
1500c9be6216Seric 		}
1501c9be6216Seric 
15025f73204aSeric 		ExitStat = EX_TEMPFAIL;
15035f73204aSeric 		return (EX_TEMPFAIL);
1504c579ef51Seric 	}
150533db8731Seric 
150633db8731Seric 	/* normal death -- return status */
1507588cad61Seric 	st = (st >> 8) & 0377;
1508588cad61Seric 	return (st);
1509c579ef51Seric }
1510c579ef51Seric /*
151125a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
151225a99e2eSeric **
151325a99e2eSeric **	Parameters:
151425a99e2eSeric **		stat -- the status code from the mailer (high byte
151525a99e2eSeric **			only; core dumps must have been taken care of
151625a99e2eSeric **			already).
151781161401Seric **		m -- the mailer info for this mailer.
151881161401Seric **		mci -- the mailer connection info -- can be NULL if the
151981161401Seric **			response is given before the connection is made.
152081161401Seric **		e -- the current envelope.
152125a99e2eSeric **
152225a99e2eSeric **	Returns:
1523db8841e9Seric **		none.
152425a99e2eSeric **
152525a99e2eSeric **	Side Effects:
1526c1f9df2cSeric **		Errors may be incremented.
152725a99e2eSeric **		ExitStat may be set.
152825a99e2eSeric */
152925a99e2eSeric 
153081161401Seric giveresponse(stat, m, mci, e)
153125a99e2eSeric 	int stat;
1532588cad61Seric 	register MAILER *m;
153381161401Seric 	register MCI *mci;
1534198d9be0Seric 	ENVELOPE *e;
153525a99e2eSeric {
15369c16475dSeric 	register const char *statmsg;
153725a99e2eSeric 	extern char *SysExMsg[];
153825a99e2eSeric 	register int i;
1539d4bd8f0eSbostic 	extern int N_SysEx;
1540198d9be0Seric 	char buf[MAXLINE];
154125a99e2eSeric 
154213bbc08cSeric 	/*
154313bbc08cSeric 	**  Compute status message from code.
154413bbc08cSeric 	*/
154513bbc08cSeric 
154625a99e2eSeric 	i = stat - EX__BASE;
1547588cad61Seric 	if (stat == 0)
15486fe8c3bcSeric 	{
1549588cad61Seric 		statmsg = "250 Sent";
1550ce5531bdSeric 		if (e->e_statmsg != NULL)
15516fe8c3bcSeric 		{
1552ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15536fe8c3bcSeric 			statmsg = buf;
15546fe8c3bcSeric 		}
15556fe8c3bcSeric 	}
1556588cad61Seric 	else if (i < 0 || i > N_SysEx)
1557588cad61Seric 	{
1558588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1559588cad61Seric 		stat = EX_UNAVAILABLE;
1560588cad61Seric 		statmsg = buf;
1561588cad61Seric 	}
1562198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1563198d9be0Seric 	{
15647d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1565d4bd8f0eSbostic #ifdef NAMED_BIND
1566f28da541Smiriam 		if (h_errno == TRY_AGAIN)
15674d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1568f28da541Smiriam 		else
1569d4bd8f0eSbostic #endif
1570f28da541Smiriam 		{
15718557d168Seric 			if (errno != 0)
1572d87e85f3Seric 				statmsg = errstring(errno);
1573d87e85f3Seric 			else
1574d87e85f3Seric 			{
1575d87e85f3Seric #ifdef SMTP
1576d87e85f3Seric 				extern char SmtpError[];
1577d87e85f3Seric 
1578d87e85f3Seric 				statmsg = SmtpError;
15796c2c3107Seric #else /* SMTP */
1580d87e85f3Seric 				statmsg = NULL;
15816c2c3107Seric #endif /* SMTP */
1582d87e85f3Seric 			}
1583f28da541Smiriam 		}
1584d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1585d87e85f3Seric 		{
158687c9b3e7Seric 			(void) strcat(buf, ": ");
1587d87e85f3Seric 			(void) strcat(buf, statmsg);
15888557d168Seric 		}
1589198d9be0Seric 		statmsg = buf;
1590198d9be0Seric 	}
1591f170942cSeric #ifdef NAMED_BIND
1592f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1593f170942cSeric 	{
15944d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1595f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1596f170942cSeric 		statmsg = buf;
1597f170942cSeric 	}
1598f170942cSeric #endif
159925a99e2eSeric 	else
1600d87e85f3Seric 	{
160125a99e2eSeric 		statmsg = SysExMsg[i];
16027d55540cSeric 		if (*statmsg++ == ':')
16037d55540cSeric 		{
16047d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16057d55540cSeric 			statmsg = buf;
16067d55540cSeric 		}
1607d87e85f3Seric 	}
1608588cad61Seric 
1609588cad61Seric 	/*
1610588cad61Seric 	**  Print the message as appropriate
1611588cad61Seric 	*/
1612588cad61Seric 
1613198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1614e12d03eeSeric 		message(&statmsg[4], errstring(errno));
161525a99e2eSeric 	else
161625a99e2eSeric 	{
1617c1f9df2cSeric 		Errors++;
1618e12d03eeSeric 		usrerr(statmsg, errstring(errno));
161925a99e2eSeric 	}
162025a99e2eSeric 
162125a99e2eSeric 	/*
162225a99e2eSeric 	**  Final cleanup.
162325a99e2eSeric 	**	Log a record of the transaction.  Compute the new
162425a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
162525a99e2eSeric 	**	that.
162625a99e2eSeric 	*/
162725a99e2eSeric 
16282f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
162981161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1630eb238f8cSeric 
1631eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1632eb238f8cSeric 		setstat(stat);
1633198d9be0Seric 	if (stat != EX_OK)
1634198d9be0Seric 	{
1635198d9be0Seric 		if (e->e_message != NULL)
1636198d9be0Seric 			free(e->e_message);
1637198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1638198d9be0Seric 	}
16398557d168Seric 	errno = 0;
1640d4bd8f0eSbostic #ifdef NAMED_BIND
1641f28da541Smiriam 	h_errno = 0;
1642d4bd8f0eSbostic #endif
1643eb238f8cSeric }
1644eb238f8cSeric /*
1645eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1646eb238f8cSeric **
1647eb238f8cSeric **	Parameters:
164881161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
164981161401Seric **		mci -- the mailer connection info -- can be NULL if the
165081161401Seric **			log is occuring when no connection is active.
165181161401Seric **		stat -- the message to print for the status.
165281161401Seric **		e -- the current envelope.
1653eb238f8cSeric **
1654eb238f8cSeric **	Returns:
1655eb238f8cSeric **		none
1656eb238f8cSeric **
1657eb238f8cSeric **	Side Effects:
1658eb238f8cSeric **		none
1659eb238f8cSeric */
1660eb238f8cSeric 
166181161401Seric logdelivery(m, mci, stat, e)
166281161401Seric 	MAILER *m;
166381161401Seric 	register MCI *mci;
1664eb238f8cSeric 	char *stat;
1665b31e7f2bSeric 	register ENVELOPE *e;
16665cf56be3Seric {
1667eb238f8cSeric # ifdef LOG
1668d6acf3eeSeric 	char buf[512];
16699507d1f9Seric 
167048ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
167181161401Seric 
167248ed5d33Seric 	if (m != NULL)
167371ff6caaSeric 	{
167448ed5d33Seric 		(void) strcat(buf, ", mailer=");
167548ed5d33Seric 		(void) strcat(buf, m->m_name);
167671ff6caaSeric 	}
167748ed5d33Seric 
167848ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
167971ff6caaSeric 	{
168071ff6caaSeric # ifdef DAEMON
1681e2f2f828Seric 		extern SOCKADDR CurHostAddr;
168248ed5d33Seric # endif
168371ff6caaSeric 
168448ed5d33Seric 		(void) strcat(buf, ", relay=");
168548ed5d33Seric 		(void) strcat(buf, mci->mci_host);
168648ed5d33Seric 
168748ed5d33Seric # ifdef DAEMON
168848ed5d33Seric 		(void) strcat(buf, " (");
1689e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
169048ed5d33Seric 		(void) strcat(buf, ")");
169171ff6caaSeric # endif
169271ff6caaSeric 	}
16939507d1f9Seric 	else
169448ed5d33Seric 	{
169548ed5d33Seric 		char *p = macvalue('h', e);
16969507d1f9Seric 
169748ed5d33Seric 		if (p != NULL && p[0] != '\0')
169848ed5d33Seric 		{
169948ed5d33Seric 			(void) strcat(buf, ", relay=");
170048ed5d33Seric 			(void) strcat(buf, p);
170148ed5d33Seric 		}
170248ed5d33Seric 	}
1703d6acf3eeSeric 
1704d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1705d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
17066c2c3107Seric # endif /* LOG */
170725a99e2eSeric }
170825a99e2eSeric /*
170951552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
171025a99e2eSeric **
171151552439Seric **	This can be made an arbitrary message separator by changing $l
171251552439Seric **
17139b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
17149b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
17159b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
17169b6c17a6Seric **	this kind of antique garbage????
171725a99e2eSeric **
171825a99e2eSeric **	Parameters:
171951552439Seric **		fp -- the file to output to.
172051552439Seric **		m -- the mailer describing this entry.
172125a99e2eSeric **
172225a99e2eSeric **	Returns:
172351552439Seric **		none
172425a99e2eSeric **
172525a99e2eSeric **	Side Effects:
172651552439Seric **		outputs some text to fp.
172725a99e2eSeric */
172825a99e2eSeric 
1729b31e7f2bSeric putfromline(fp, m, e)
173051552439Seric 	register FILE *fp;
173151552439Seric 	register MAILER *m;
1732b31e7f2bSeric 	ENVELOPE *e;
173325a99e2eSeric {
17342bc47524Seric 	char *template = "\201l\n";
173551552439Seric 	char buf[MAXLINE];
173625a99e2eSeric 
173757fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
173851552439Seric 		return;
173913bbc08cSeric 
17402c7e1b8dSeric # ifdef UGLYUUCP
174157fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
174274b6e67bSeric 	{
1743ea09d6edSeric 		char *bang;
1744ea09d6edSeric 		char xbuf[MAXLINE];
174574b6e67bSeric 
1746ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17476c2c3107Seric 		bang = strchr(buf, '!');
174874b6e67bSeric 		if (bang == NULL)
174908b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
175074b6e67bSeric 		else
1751588cad61Seric 		{
1752ea09d6edSeric 			*bang++ = '\0';
17532bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1754ea09d6edSeric 			template = xbuf;
175574b6e67bSeric 		}
1756588cad61Seric 	}
17576c2c3107Seric # endif /* UGLYUUCP */
1758b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
175977b52738Seric 	putline(buf, fp, m);
1760bc6e2962Seric }
1761bc6e2962Seric /*
176251552439Seric **  PUTBODY -- put the body of a message.
176351552439Seric **
176451552439Seric **	Parameters:
176551552439Seric **		fp -- file to output onto.
176677b52738Seric **		m -- a mailer descriptor to control output format.
17679a6a5f55Seric **		e -- the envelope to put out.
176803c02fdeSeric **		separator -- if non-NULL, a message separator that must
176903c02fdeSeric **			not be permitted in the resulting message.
177051552439Seric **
177151552439Seric **	Returns:
177251552439Seric **		none.
177351552439Seric **
177451552439Seric **	Side Effects:
177551552439Seric **		The message is written onto fp.
177651552439Seric */
177751552439Seric 
177803c02fdeSeric putbody(fp, m, e, separator)
177951552439Seric 	FILE *fp;
1780588cad61Seric 	MAILER *m;
17819a6a5f55Seric 	register ENVELOPE *e;
178203c02fdeSeric 	char *separator;
178351552439Seric {
178477b52738Seric 	char buf[MAXLINE];
178551552439Seric 
178651552439Seric 	/*
178751552439Seric 	**  Output the body of the message
178851552439Seric 	*/
178951552439Seric 
17909a6a5f55Seric 	if (e->e_dfp == NULL)
179151552439Seric 	{
17929a6a5f55Seric 		if (e->e_df != NULL)
17939a6a5f55Seric 		{
17949a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
17959a6a5f55Seric 			if (e->e_dfp == NULL)
17968f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
1797e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
17989a6a5f55Seric 		}
17999a6a5f55Seric 		else
180077b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
18019a6a5f55Seric 	}
18029a6a5f55Seric 	if (e->e_dfp != NULL)
18039a6a5f55Seric 	{
18049a6a5f55Seric 		rewind(e->e_dfp);
180577b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
180624fc8aeeSeric 		{
180724fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1808d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
18093462ad9eSeric 				(void) putc('>', fp);
181003c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
181103c02fdeSeric 			{
181203c02fdeSeric 				/* possible separator */
181303c02fdeSeric 				int sl = strlen(separator);
181403c02fdeSeric 
181503c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
181603c02fdeSeric 					(void) putc(' ', fp);
181703c02fdeSeric 			}
181877b52738Seric 			putline(buf, fp, m);
181924fc8aeeSeric 		}
182051552439Seric 
18219a6a5f55Seric 		if (ferror(e->e_dfp))
182251552439Seric 		{
182351552439Seric 			syserr("putbody: read error");
182451552439Seric 			ExitStat = EX_IOERR;
182551552439Seric 		}
182651552439Seric 	}
182751552439Seric 
18280890ba1fSeric 	/* some mailers want extra blank line at end of message */
18290890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
18300890ba1fSeric 		putline("", fp, m);
18310890ba1fSeric 
183251552439Seric 	(void) fflush(fp);
183351552439Seric 	if (ferror(fp) && errno != EPIPE)
183451552439Seric 	{
183551552439Seric 		syserr("putbody: write error");
183651552439Seric 		ExitStat = EX_IOERR;
183751552439Seric 	}
183851552439Seric 	errno = 0;
183925a99e2eSeric }
184025a99e2eSeric /*
184125a99e2eSeric **  MAILFILE -- Send a message to a file.
184225a99e2eSeric **
1843f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1844f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1845f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1846f129ec7dSeric **	sendmail runs as root.
1847f129ec7dSeric **
1848588cad61Seric **	This could be done as a subordinate mailer, except that it
1849588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1850588cad61Seric **	view this as being sufficiently important as to include it
1851588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1852588cad61Seric **	to create another process plus some pipes to save the message.
1853588cad61Seric **
185425a99e2eSeric **	Parameters:
185525a99e2eSeric **		filename -- the name of the file to send to.
18566259796dSeric **		ctladdr -- the controlling address header -- includes
18576259796dSeric **			the userid/groupid to be when sending.
185825a99e2eSeric **
185925a99e2eSeric **	Returns:
186025a99e2eSeric **		The exit code associated with the operation.
186125a99e2eSeric **
186225a99e2eSeric **	Side Effects:
186325a99e2eSeric **		none.
186425a99e2eSeric */
186525a99e2eSeric 
1866b31e7f2bSeric mailfile(filename, ctladdr, e)
186725a99e2eSeric 	char *filename;
18686259796dSeric 	ADDRESS *ctladdr;
1869b31e7f2bSeric 	register ENVELOPE *e;
187025a99e2eSeric {
187125a99e2eSeric 	register FILE *f;
187232d19d43Seric 	register int pid;
187315d084d5Seric 	int mode;
187425a99e2eSeric 
1875671745f3Seric 	if (tTd(11, 1))
1876671745f3Seric 	{
1877671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
1878671745f3Seric 		printaddr(ctladdr, FALSE);
1879671745f3Seric 	}
1880671745f3Seric 
1881f170942cSeric 	if (e->e_xfp != NULL)
1882f170942cSeric 		fflush(e->e_xfp);
1883f170942cSeric 
188432d19d43Seric 	/*
188532d19d43Seric 	**  Fork so we can change permissions here.
188632d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
188732d19d43Seric 	**	the complications of calling subroutines, etc.
188832d19d43Seric 	*/
188932d19d43Seric 
189032d19d43Seric 	DOFORK(fork);
189132d19d43Seric 
189232d19d43Seric 	if (pid < 0)
189332d19d43Seric 		return (EX_OSERR);
189432d19d43Seric 	else if (pid == 0)
189532d19d43Seric 	{
189632d19d43Seric 		/* child -- actually write to file */
1897f129ec7dSeric 		struct stat stb;
1898f129ec7dSeric 
18992b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
19002b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
19012b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
19023462ad9eSeric 		(void) umask(OldUmask);
190395f16dc0Seric 
1904f129ec7dSeric 		if (stat(filename, &stb) < 0)
19053a98e7eaSeric 			stb.st_mode = FileMode;
190615d084d5Seric 		mode = stb.st_mode;
190795f16dc0Seric 
190895f16dc0Seric 		/* limit the errors to those actually caused in the child */
190995f16dc0Seric 		errno = 0;
191095f16dc0Seric 		ExitStat = EX_OK;
191195f16dc0Seric 
1912f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1913f129ec7dSeric 			exit(EX_CANTCREAT);
191403827b5fSeric 		if (ctladdr == NULL)
19158f9146b0Srick 			ctladdr = &e->e_from;
191615d084d5Seric 		else
191715d084d5Seric 		{
191815d084d5Seric 			/* ignore setuid and setgid bits */
191915d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
192015d084d5Seric 		}
192115d084d5Seric 
19228f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
19238f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
19248f9146b0Srick 		{
19258f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
192695f16dc0Seric 			if (e->e_dfp == NULL)
192795f16dc0Seric 			{
19288f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
1929e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
19308f9146b0Srick 			}
19318f9146b0Srick 		}
19328f9146b0Srick 
193315d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1934e36b99e2Seric 		{
193595f16dc0Seric 			if (ctladdr->q_uid == 0)
193695f16dc0Seric 			{
1937e36b99e2Seric 				(void) setgid(DefGid);
1938898a126bSbostic 				(void) initgroups(DefUser, DefGid);
193995f16dc0Seric 			}
194095f16dc0Seric 			else
194195f16dc0Seric 			{
19426259796dSeric 				(void) setgid(ctladdr->q_gid);
1943898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1944898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1945898a126bSbostic 					ctladdr->q_gid);
1946898a126bSbostic 			}
1947e36b99e2Seric 		}
194815d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1949e36b99e2Seric 		{
1950e36b99e2Seric 			if (ctladdr->q_uid == 0)
1951e36b99e2Seric 				(void) setuid(DefUid);
1952e36b99e2Seric 			else
19536259796dSeric 				(void) setuid(ctladdr->q_uid);
1954e36b99e2Seric 		}
195595f16dc0Seric 		FileName = filename;
195695f16dc0Seric 		LineNumber = 0;
19573a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
195825a99e2eSeric 		if (f == NULL)
195995f16dc0Seric 		{
196008b25121Seric 			message("554 cannot open");
196132d19d43Seric 			exit(EX_CANTCREAT);
196295f16dc0Seric 		}
196325a99e2eSeric 
19640331ce05Seric 		putfromline(f, FileMailer, e);
19650331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
19660331ce05Seric 		putline("\n", f, FileMailer);
196703c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
19680331ce05Seric 		putline("\n", f, FileMailer);
196995f16dc0Seric 		if (ferror(f))
197095f16dc0Seric 		{
197108b25121Seric 			message("451 I/O error");
197295f16dc0Seric 			setstat(EX_IOERR);
197395f16dc0Seric 		}
1974ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
197532d19d43Seric 		(void) fflush(stdout);
1976e36b99e2Seric 
197727628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1978c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
197995f16dc0Seric 		exit(ExitStat);
198013bbc08cSeric 		/*NOTREACHED*/
198132d19d43Seric 	}
198232d19d43Seric 	else
198332d19d43Seric 	{
198432d19d43Seric 		/* parent -- wait for exit status */
1985588cad61Seric 		int st;
198632d19d43Seric 
1987588cad61Seric 		st = waitfor(pid);
1988588cad61Seric 		if ((st & 0377) != 0)
1989588cad61Seric 			return (EX_UNAVAILABLE);
1990588cad61Seric 		else
1991588cad61Seric 			return ((st >> 8) & 0377);
19928f9146b0Srick 		/*NOTREACHED*/
199332d19d43Seric 	}
199425a99e2eSeric }
1995ea4dc939Seric /*
1996e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1997e103b48fSeric **
1998e103b48fSeric **	The signature describes how we are going to send this -- it
1999e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2000e103b48fSeric **	an ordered list of MX hosts.
2001e103b48fSeric **
2002e103b48fSeric **	Parameters:
2003e103b48fSeric **		m -- the mailer describing this host.
2004e103b48fSeric **		host -- the host name.
2005e103b48fSeric **		e -- the current envelope.
2006e103b48fSeric **
2007e103b48fSeric **	Returns:
2008e103b48fSeric **		The signature for this host.
2009e103b48fSeric **
2010e103b48fSeric **	Side Effects:
2011e103b48fSeric **		Can tweak the symbol table.
2012e103b48fSeric */
2013e103b48fSeric 
2014e103b48fSeric char *
2015e103b48fSeric hostsignature(m, host, e)
2016e103b48fSeric 	register MAILER *m;
2017e103b48fSeric 	char *host;
2018e103b48fSeric 	ENVELOPE *e;
2019e103b48fSeric {
2020e103b48fSeric 	register char *p;
2021e103b48fSeric 	register STAB *s;
2022e103b48fSeric 	int i;
2023e103b48fSeric 	int len;
2024e103b48fSeric #ifdef NAMED_BIND
2025e103b48fSeric 	int nmx;
2026e103b48fSeric 	auto int rcode;
2027bafdc4e5Seric 	char *hp;
2028bafdc4e5Seric 	char *endp;
2029516782b4Seric 	int oldoptions;
2030e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2031e103b48fSeric #endif
2032e103b48fSeric 
2033e103b48fSeric 	/*
2034e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2035e103b48fSeric 	*/
2036e103b48fSeric 
2037e103b48fSeric 	p = m->m_mailer;
2038e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2039e103b48fSeric 	{
2040e103b48fSeric 		/* just an ordinary mailer */
2041e103b48fSeric 		return host;
2042e103b48fSeric 	}
2043e103b48fSeric 
2044e103b48fSeric 	/*
2045e103b48fSeric 	**  If it is a numeric address, just return it.
2046e103b48fSeric 	*/
2047e103b48fSeric 
2048e103b48fSeric 	if (host[0] == '[')
2049e103b48fSeric 		return host;
2050e103b48fSeric 
2051e103b48fSeric 	/*
2052e103b48fSeric 	**  Look it up in the symbol table.
2053e103b48fSeric 	*/
2054e103b48fSeric 
2055e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2056e103b48fSeric 	if (s->s_hostsig != NULL)
2057e103b48fSeric 		return s->s_hostsig;
2058e103b48fSeric 
2059e103b48fSeric 	/*
2060e103b48fSeric 	**  Not already there -- create a signature.
2061e103b48fSeric 	*/
2062e103b48fSeric 
2063e103b48fSeric #ifdef NAMED_BIND
2064516782b4Seric 	if (ConfigLevel < 2)
2065516782b4Seric 	{
2066516782b4Seric 		oldoptions = _res.options;
2067516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2068516782b4Seric 	}
2069516782b4Seric 
2070bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2071bafdc4e5Seric 	{
2072bafdc4e5Seric 		endp = strchr(hp, ':');
2073bafdc4e5Seric 		if (endp != NULL)
2074bafdc4e5Seric 			*endp = '\0';
2075bafdc4e5Seric 
20767bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
20777d55540cSeric 
2078e103b48fSeric 		if (nmx <= 0)
2079e103b48fSeric 		{
2080e103b48fSeric 			register MCI *mci;
2081e103b48fSeric 			extern int errno;
2082e103b48fSeric 
2083e103b48fSeric 			/* update the connection info for this host */
2084bafdc4e5Seric 			mci = mci_get(hp, m);
2085e103b48fSeric 			mci->mci_exitstat = rcode;
2086e103b48fSeric 			mci->mci_errno = errno;
2087f170942cSeric #ifdef NAMED_BIND
2088f170942cSeric 			mci->mci_herrno = h_errno;
2089f170942cSeric #endif
2090e103b48fSeric 
2091e103b48fSeric 			/* and return the original host name as the signature */
2092bafdc4e5Seric 			nmx = 1;
2093bafdc4e5Seric 			mxhosts[0] = hp;
2094e103b48fSeric 		}
2095e103b48fSeric 
2096e103b48fSeric 		len = 0;
2097e103b48fSeric 		for (i = 0; i < nmx; i++)
2098e103b48fSeric 		{
2099e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2100e103b48fSeric 		}
2101bafdc4e5Seric 		if (s->s_hostsig != NULL)
2102bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2103bafdc4e5Seric 		p = xalloc(len);
2104bafdc4e5Seric 		if (s->s_hostsig != NULL)
2105bafdc4e5Seric 		{
2106bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2107bafdc4e5Seric 			free(s->s_hostsig);
2108bafdc4e5Seric 			s->s_hostsig = p;
2109bafdc4e5Seric 			p += strlen(p);
2110bafdc4e5Seric 			*p++ = ':';
2111bafdc4e5Seric 		}
2112bafdc4e5Seric 		else
2113bafdc4e5Seric 			s->s_hostsig = p;
2114e103b48fSeric 		for (i = 0; i < nmx; i++)
2115e103b48fSeric 		{
2116e103b48fSeric 			if (i != 0)
2117e103b48fSeric 				*p++ = ':';
2118e103b48fSeric 			strcpy(p, mxhosts[i]);
2119e103b48fSeric 			p += strlen(p);
2120e103b48fSeric 		}
2121bafdc4e5Seric 		if (endp != NULL)
2122bafdc4e5Seric 			*endp++ = ':';
2123bafdc4e5Seric 	}
2124e103b48fSeric 	makelower(s->s_hostsig);
2125516782b4Seric 	if (ConfigLevel < 2)
2126516782b4Seric 		_res.options = oldoptions;
2127e103b48fSeric #else
2128e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2129e103b48fSeric 	s->s_hostsig = host;
2130e103b48fSeric #endif
2131e103b48fSeric 	if (tTd(17, 1))
2132e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2133e103b48fSeric 	return s->s_hostsig;
2134e103b48fSeric }
2135