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*f170942cSeric static char sccsid[] = "@(#)deliver.c	8.2 (Berkeley) 07/11/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>
20*f170942cSeric 
21*f170942cSeric 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 
53896b16f6Seric 	if (bitset(EF_FATALERRS, e->e_flags))
54896b16f6Seric 	{
55896b16f6Seric 		/* this will get a return message -- so don't send it */
56896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
57896b16f6Seric 		return;
58896b16f6Seric 	}
59896b16f6Seric 
609c9e68d9Seric 	/* determine actual delivery mode */
619c9e68d9Seric 	if (mode == SM_DEFAULT)
629c9e68d9Seric 	{
639c9e68d9Seric 		mode = e->e_sendmode;
649c9e68d9Seric 		if (mode != SM_VERIFY &&
659c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
669c9e68d9Seric 			mode = SM_QUEUE;
676103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
689c9e68d9Seric 	}
696103d9b4Seric 	else
706103d9b4Seric 		announcequeueup = FALSE;
719c9e68d9Seric 
729c9e68d9Seric 	if (tTd(13, 1))
739c9e68d9Seric 	{
749c9e68d9Seric 		printf("\nSENDALL: mode %c, e_from ", mode);
759c9e68d9Seric 		printaddr(&e->e_from, FALSE);
769c9e68d9Seric 		printf("sendqueue:\n");
779c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
789c9e68d9Seric 	}
799c9e68d9Seric 
809c9e68d9Seric 	/*
819c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
829c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
839c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
849c9e68d9Seric 	*/
859c9e68d9Seric 
869c9e68d9Seric 	CurEnv = e;
879c9e68d9Seric 
889c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
899c9e68d9Seric 	{
909c9e68d9Seric 		errno = 0;
919c9e68d9Seric 		syserr("554 too many hops %d (%d max): from %s, to %s",
929c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
939c9e68d9Seric 			e->e_sendqueue->q_paddr);
949c9e68d9Seric 		return;
959c9e68d9Seric 	}
969c9e68d9Seric 
97b8004690Seric 	/*
98b8004690Seric 	**  Do sender deletion.
99b8004690Seric 	**
100b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
101b8004690Seric 	**	This can happen if the name server is hosed when you
102b8004690Seric 	**	are trying to send mail.  The result is that the sender
103b8004690Seric 	**	is instantiated in the queue as a recipient.
104b8004690Seric 	*/
105b8004690Seric 
106b8004690Seric 	if (!MeToo && !bitset(QQUEUEUP, e->e_from.q_flags))
1079c9e68d9Seric 	{
1089c9e68d9Seric 		if (tTd(13, 5))
1099c9e68d9Seric 		{
1109c9e68d9Seric 			printf("sendall: QDONTSEND ");
1119c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1129c9e68d9Seric 		}
1139c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1149c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1159c9e68d9Seric 	}
1169c9e68d9Seric 
117ce5531bdSeric 	/*
118ce5531bdSeric 	**  Handle alias owners.
119ce5531bdSeric 	**
120ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
121ce5531bdSeric 	**	We discard owners that are the same as the return path.
122ce5531bdSeric 	*/
123ce5531bdSeric 
124ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
125ce5531bdSeric 	{
126ce5531bdSeric 		register struct address *a;
127ce5531bdSeric 
128ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
129ce5531bdSeric 			continue;
130ce5531bdSeric 		if (a != NULL)
131ce5531bdSeric 			q->q_owner = a->q_owner;
132ce5531bdSeric 
133ce5531bdSeric 		if (q->q_owner != NULL &&
134ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
135ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
136ce5531bdSeric 			q->q_owner = NULL;
137ce5531bdSeric 	}
138ce5531bdSeric 
139ce5531bdSeric 	owner = "";
140ce5531bdSeric 	otherowners = 1;
141ce5531bdSeric 	while (owner != NULL && otherowners > 0)
142ce5531bdSeric 	{
143ce5531bdSeric 		owner = NULL;
144ce5531bdSeric 		otherowners = 0;
145ce5531bdSeric 
146ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
147ce5531bdSeric 		{
148ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
149ce5531bdSeric 				continue;
150ce5531bdSeric 
151ce5531bdSeric 			if (q->q_owner != NULL)
152ce5531bdSeric 			{
153ce5531bdSeric 				if (owner == NULL)
154ce5531bdSeric 					owner = q->q_owner;
155ce5531bdSeric 				else if (owner != q->q_owner)
156ce5531bdSeric 				{
157ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
158ce5531bdSeric 					{
159ce5531bdSeric 						/* make future comparisons cheap */
160ce5531bdSeric 						q->q_owner = owner;
161ce5531bdSeric 					}
162ce5531bdSeric 					else
163ce5531bdSeric 					{
164ce5531bdSeric 						otherowners++;
165ce5531bdSeric 					}
166ce5531bdSeric 					owner = q->q_owner;
167ce5531bdSeric 				}
168ce5531bdSeric 			}
169ce5531bdSeric 			else
170ce5531bdSeric 			{
171ce5531bdSeric 				otherowners++;
172ce5531bdSeric 			}
173ce5531bdSeric 		}
174ce5531bdSeric 
175ce5531bdSeric 		if (owner != NULL && otherowners > 0)
176ce5531bdSeric 		{
177ce5531bdSeric 			extern HDR *copyheader();
178ce5531bdSeric 			extern ADDRESS *copyqueue();
179ce5531bdSeric 
180ce5531bdSeric 			/*
181ce5531bdSeric 			**  Split this envelope into two.
182ce5531bdSeric 			*/
183ce5531bdSeric 
184ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
185ce5531bdSeric 			*ee = *e;
186ce5531bdSeric 			ee->e_id = NULL;
187ce5531bdSeric 			(void) queuename(ee, '\0');
188ce5531bdSeric 
189ce5531bdSeric 			if (tTd(13, 1))
190ce5531bdSeric 				printf("sendall: split %s into %s\n",
191ce5531bdSeric 					e->e_id, ee->e_id);
192ce5531bdSeric 
193ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
194ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
195ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
196ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
197ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
198ce5531bdSeric 			if (tTd(13, 5))
199ce5531bdSeric 			{
200ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
201ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
202ce5531bdSeric 			}
203ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
204ce5531bdSeric 			ee->e_dfp = NULL;
205ce5531bdSeric 			ee->e_xfp = NULL;
206ce5531bdSeric 			ee->e_lockfp = NULL;
207ce5531bdSeric 			ee->e_df = NULL;
208ce5531bdSeric 			ee->e_errormode = EM_MAIL;
209ce5531bdSeric 			ee->e_sibling = splitenv;
210ce5531bdSeric 			splitenv = ee;
211ce5531bdSeric 
212ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
213ce5531bdSeric 				if (q->q_owner == owner)
214ce5531bdSeric 					q->q_flags |= QDONTSEND;
215ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
216ce5531bdSeric 				if (q->q_owner != owner)
217ce5531bdSeric 					q->q_flags |= QDONTSEND;
218ce5531bdSeric 
219ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
220ce5531bdSeric 			{
221ce5531bdSeric 				ee->e_dfp = NULL;
222ce5531bdSeric 				ee->e_df = newstr(queuename(ee, 'd'));
223ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
224ce5531bdSeric 				{
225ce5531bdSeric 					syserr("sendall: link(%s, %s)",
226ce5531bdSeric 						e->e_df, ee->e_df);
227ce5531bdSeric 				}
228ce5531bdSeric 			}
229ce5531bdSeric 
230ce5531bdSeric 			if (mode != SM_VERIFY)
231ce5531bdSeric 				openxscript(ee);
232ce5531bdSeric #ifdef LOG
233ce5531bdSeric 			if (LogLevel > 4)
234ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
235ce5531bdSeric 					ee->e_id, e->e_id);
236ce5531bdSeric #endif
237ce5531bdSeric 		}
238ce5531bdSeric 	}
239ce5531bdSeric 
240ce5531bdSeric 	if (owner != NULL)
241ce5531bdSeric 	{
242ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
243ce5531bdSeric 		if (tTd(13, 5))
244ce5531bdSeric 		{
245ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
246ce5531bdSeric 			printaddr(&e->e_from, FALSE);
247ce5531bdSeric 		}
248ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
249ce5531bdSeric 		e->e_errormode = EM_MAIL;
250ce5531bdSeric 	}
251ce5531bdSeric 
252c1ac89b1Seric # ifdef QUEUE
253c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
254c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
255c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
256c1ac89b1Seric 	{
257c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2586103d9b4Seric 		queueup(e, TRUE, announcequeueup);
259ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2606103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
261c1ac89b1Seric 	}
262c1ac89b1Seric #endif /* QUEUE */
263c1ac89b1Seric 
264ce5531bdSeric 	if (splitenv != NULL)
265ce5531bdSeric 	{
266ce5531bdSeric 		if (tTd(13, 1))
267ce5531bdSeric 		{
268ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
269ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
270ce5531bdSeric 		}
271ce5531bdSeric 
272ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
273ce5531bdSeric 		{
274ce5531bdSeric 			CurEnv = ee;
275ce5531bdSeric 			sendenvelope(ee, mode);
276ce5531bdSeric 		}
277ce5531bdSeric 
278ce5531bdSeric 		CurEnv = e;
279ce5531bdSeric 	}
280ce5531bdSeric 	sendenvelope(e, mode);
281ce5531bdSeric 
282ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
283ce5531bdSeric 		dropenvelope(splitenv);
284ce5531bdSeric }
285ce5531bdSeric 
286ce5531bdSeric sendenvelope(e, mode)
287ce5531bdSeric 	register ENVELOPE *e;
288ce5531bdSeric 	char mode;
289ce5531bdSeric {
290ce5531bdSeric 	bool oldverbose;
291ce5531bdSeric 	int pid;
292ce5531bdSeric 	register ADDRESS *q;
293ce5531bdSeric #ifdef LOCKF
294ce5531bdSeric 	struct flock lfd;
295ce5531bdSeric #endif
296ce5531bdSeric 
297ce5531bdSeric 	oldverbose = Verbose;
298c1ac89b1Seric 	switch (mode)
299c1ac89b1Seric 	{
300c1ac89b1Seric 	  case SM_VERIFY:
301c1ac89b1Seric 		Verbose = TRUE;
302c1ac89b1Seric 		break;
303c1ac89b1Seric 
304c1ac89b1Seric 	  case SM_QUEUE:
305c1ac89b1Seric   queueonly:
306c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
307c1ac89b1Seric 		return;
308c1ac89b1Seric 
309c1ac89b1Seric 	  case SM_FORK:
310c1ac89b1Seric 		if (e->e_xfp != NULL)
311c1ac89b1Seric 			(void) fflush(e->e_xfp);
312c1ac89b1Seric 
313c1ac89b1Seric # ifdef LOCKF
314c1ac89b1Seric 		/*
315c1ac89b1Seric 		**  Since lockf has the interesting semantic that the
316c1ac89b1Seric 		**  lock is lost when we fork, we have to risk losing
317c1ac89b1Seric 		**  the lock here by closing before the fork, and then
318c1ac89b1Seric 		**  trying to get it back in the child.
319c1ac89b1Seric 		*/
320c1ac89b1Seric 
321c1ac89b1Seric 		if (e->e_lockfp != NULL)
322c1ac89b1Seric 		{
323c1ac89b1Seric 			(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
324c1ac89b1Seric 			e->e_lockfp = NULL;
325c1ac89b1Seric 		}
326c1ac89b1Seric # endif /* LOCKF */
327c1ac89b1Seric 
328c1ac89b1Seric 		pid = fork();
329c1ac89b1Seric 		if (pid < 0)
330c1ac89b1Seric 		{
331c1ac89b1Seric 			goto queueonly;
332c1ac89b1Seric 		}
333c1ac89b1Seric 		else if (pid > 0)
334c1ac89b1Seric 		{
335c1ac89b1Seric 			/* be sure we leave the temp files to our child */
336c1ac89b1Seric 			e->e_id = e->e_df = NULL;
337c1ac89b1Seric # ifndef LOCKF
338c1ac89b1Seric 			if (e->e_lockfp != NULL)
339c1ac89b1Seric 			{
340c1ac89b1Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
341c1ac89b1Seric 				e->e_lockfp = NULL;
342c1ac89b1Seric 			}
343c1ac89b1Seric # endif
344c1ac89b1Seric 
345c1ac89b1Seric 			/* close any random open files in the envelope */
346c1ac89b1Seric 			if (e->e_dfp != NULL)
347c1ac89b1Seric 			{
348c1ac89b1Seric 				(void) xfclose(e->e_dfp, "sendenvelope", "dfp");
349c1ac89b1Seric 				e->e_dfp = NULL;
350c1ac89b1Seric 			}
351c1ac89b1Seric 			if (e->e_xfp != NULL)
352c1ac89b1Seric 			{
353c1ac89b1Seric 				(void) xfclose(e->e_xfp, "sendenvelope", "xfp");
354c1ac89b1Seric 				e->e_xfp = NULL;
355c1ac89b1Seric 			}
356c1ac89b1Seric 			return;
357c1ac89b1Seric 		}
358c1ac89b1Seric 
359c1ac89b1Seric 		/* double fork to avoid zombies */
360c1ac89b1Seric 		if (fork() > 0)
361c1ac89b1Seric 			exit(EX_OK);
362c1ac89b1Seric 
363c1ac89b1Seric 		/* be sure we are immune from the terminal */
364c1ac89b1Seric 		disconnect(FALSE, e);
365c1ac89b1Seric 
366c1ac89b1Seric # ifdef LOCKF
367c1ac89b1Seric 		/*
368c1ac89b1Seric 		**  Now try to get our lock back.
369c1ac89b1Seric 		*/
370c1ac89b1Seric 
371c1ac89b1Seric 		lfd.l_type = F_WRLCK;
372c1ac89b1Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
373c1ac89b1Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
374c1ac89b1Seric 		if (e->e_lockfp == NULL ||
375c1ac89b1Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
376c1ac89b1Seric 		{
377c1ac89b1Seric 			/* oops....  lost it */
378c1ac89b1Seric 			if (tTd(13, 1))
379c1ac89b1Seric 				printf("sendenvelope: %s lost lock: lockfp=%x, %s\n",
380c1ac89b1Seric 					e->e_id, e->e_lockfp, errstring(errno));
381c1ac89b1Seric 
382c1ac89b1Seric # ifdef LOG
383c1ac89b1Seric 			if (LogLevel > 29)
384c1ac89b1Seric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
385c1ac89b1Seric 					e->e_id);
386c1ac89b1Seric # endif /* LOG */
387c1ac89b1Seric 			exit(EX_OK);
388c1ac89b1Seric 		}
389c1ac89b1Seric # endif /* LOCKF */
390c1ac89b1Seric 
391c1ac89b1Seric 		/*
392c1ac89b1Seric 		**  Close any cached connections.
393c1ac89b1Seric 		**
394c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
395c1ac89b1Seric 		**	still knows about the connection.
396c1ac89b1Seric 		**
397c1ac89b1Seric 		**	This should only happen when delivering an error
398c1ac89b1Seric 		**	message.
399c1ac89b1Seric 		*/
400c1ac89b1Seric 
401c1ac89b1Seric 		mci_flush(FALSE, NULL);
402c1ac89b1Seric 
403c1ac89b1Seric 		break;
404c1ac89b1Seric 	}
405c1ac89b1Seric 
406c1ac89b1Seric 	/*
4079c9e68d9Seric 	**  Run through the list and send everything.
4089c9e68d9Seric 	*/
4099c9e68d9Seric 
4109c9e68d9Seric 	e->e_nsent = 0;
4119c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4129c9e68d9Seric 	{
4139c9e68d9Seric 		if (mode == SM_VERIFY)
4149c9e68d9Seric 		{
4159c9e68d9Seric 			e->e_to = q->q_paddr;
4169c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4178dfca105Seric 			{
4188dfca105Seric 				message("deliverable: mailer %s, host %s, user %s",
4198dfca105Seric 					q->q_mailer->m_name,
4208dfca105Seric 					q->q_host,
4218dfca105Seric 					q->q_user);
4228dfca105Seric 			}
4239c9e68d9Seric 		}
4249c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4259c9e68d9Seric 		{
4269c9e68d9Seric # ifdef QUEUE
4279c9e68d9Seric 			/*
4289c9e68d9Seric 			**  Checkpoint the send list every few addresses
4299c9e68d9Seric 			*/
4309c9e68d9Seric 
4319c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4329c9e68d9Seric 			{
4339c9e68d9Seric 				queueup(e, TRUE, FALSE);
4349c9e68d9Seric 				e->e_nsent = 0;
4359c9e68d9Seric 			}
4369c9e68d9Seric # endif /* QUEUE */
4379c9e68d9Seric 			(void) deliver(e, q);
4389c9e68d9Seric 		}
4399c9e68d9Seric 	}
4409c9e68d9Seric 	Verbose = oldverbose;
4419c9e68d9Seric 
4429c9e68d9Seric 	/*
4439c9e68d9Seric 	**  Now run through and check for errors.
4449c9e68d9Seric 	*/
4459c9e68d9Seric 
4469c9e68d9Seric 	if (mode == SM_VERIFY)
4479c9e68d9Seric 	{
4489c9e68d9Seric 		return;
4499c9e68d9Seric 	}
4509c9e68d9Seric 
4519c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4529c9e68d9Seric 	{
4539c9e68d9Seric 		if (tTd(13, 3))
4549c9e68d9Seric 		{
4559c9e68d9Seric 			printf("Checking ");
4569c9e68d9Seric 			printaddr(q, FALSE);
4579c9e68d9Seric 		}
4589c9e68d9Seric 
4599c9e68d9Seric 		/* only send errors if the message failed */
460ce5531bdSeric 		if (!bitset(QBADADDR, q->q_flags) ||
461ce5531bdSeric 		    bitset(QDONTSEND, q->q_flags))
4629c9e68d9Seric 			continue;
4639c9e68d9Seric 
4649c9e68d9Seric 		e->e_flags |= EF_FATALERRS;
4659c9e68d9Seric 
4669c9e68d9Seric 		if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0)
4679c9e68d9Seric 			(void) sendtolist(e->e_from.q_paddr, NULL,
4689c9e68d9Seric 					  &e->e_errorqueue, e);
4699c9e68d9Seric 	}
4709c9e68d9Seric 
4719c9e68d9Seric 	if (mode == SM_FORK)
4729c9e68d9Seric 		finis();
4739c9e68d9Seric }
4749c9e68d9Seric /*
4759c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4769c9e68d9Seric **
4779c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4789c9e68d9Seric **	two processes on the same stack!!!
4799c9e68d9Seric **
4809c9e68d9Seric **	Parameters:
4819c9e68d9Seric **		none.
4829c9e68d9Seric **
4839c9e68d9Seric **	Returns:
4849c9e68d9Seric **		From a macro???  You've got to be kidding!
4859c9e68d9Seric **
4869c9e68d9Seric **	Side Effects:
4879c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4889c9e68d9Seric **			pid of child in parent, zero in child.
4899c9e68d9Seric **			-1 on unrecoverable error.
4909c9e68d9Seric **
4919c9e68d9Seric **	Notes:
4929c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4939c9e68d9Seric **		vfork for you.....
4949c9e68d9Seric */
4959c9e68d9Seric 
4969c9e68d9Seric # define NFORKTRIES	5
4979c9e68d9Seric 
4989c9e68d9Seric # ifndef FORK
4999c9e68d9Seric # define FORK	fork
5009c9e68d9Seric # endif
5019c9e68d9Seric 
5029c9e68d9Seric # define DOFORK(fORKfN) \
5039c9e68d9Seric {\
5049c9e68d9Seric 	register int i;\
5059c9e68d9Seric \
5069c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
5079c9e68d9Seric 	{\
5089c9e68d9Seric 		pid = fORKfN();\
5099c9e68d9Seric 		if (pid >= 0)\
5109c9e68d9Seric 			break;\
5119c9e68d9Seric 		if (i > 0)\
5129c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5139c9e68d9Seric 	}\
5149c9e68d9Seric }
5159c9e68d9Seric /*
5169c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5179c9e68d9Seric **
5189c9e68d9Seric **	Parameters:
5199c9e68d9Seric **		none.
5209c9e68d9Seric **
5219c9e68d9Seric **	Returns:
5229c9e68d9Seric **		pid of child in parent.
5239c9e68d9Seric **		zero in child.
5249c9e68d9Seric **		-1 on error.
5259c9e68d9Seric **
5269c9e68d9Seric **	Side Effects:
5279c9e68d9Seric **		returns twice, once in parent and once in child.
5289c9e68d9Seric */
5299c9e68d9Seric 
5309c9e68d9Seric dofork()
5319c9e68d9Seric {
5329c9e68d9Seric 	register int pid;
5339c9e68d9Seric 
5349c9e68d9Seric 	DOFORK(fork);
5359c9e68d9Seric 	return (pid);
5369c9e68d9Seric }
5379c9e68d9Seric /*
53813bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
53913bbc08cSeric **
54013bbc08cSeric **	This routine delivers to everyone on the same host as the
54113bbc08cSeric **	user on the head of the list.  It is clever about mailers
54213bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
54313bbc08cSeric **	that it will deliver to all these addresses however -- so
54413bbc08cSeric **	deliver should be called once for each address on the
54513bbc08cSeric **	list.
54625a99e2eSeric **
54725a99e2eSeric **	Parameters:
548588cad61Seric **		e -- the envelope to deliver.
549c77d1c25Seric **		firstto -- head of the address list to deliver to.
55025a99e2eSeric **
55125a99e2eSeric **	Returns:
55225a99e2eSeric **		zero -- successfully delivered.
55325a99e2eSeric **		else -- some failure, see ExitStat for more info.
55425a99e2eSeric **
55525a99e2eSeric **	Side Effects:
55625a99e2eSeric **		The standard input is passed off to someone.
55725a99e2eSeric */
55825a99e2eSeric 
559588cad61Seric deliver(e, firstto)
560588cad61Seric 	register ENVELOPE *e;
561c77d1c25Seric 	ADDRESS *firstto;
56225a99e2eSeric {
56378442df3Seric 	char *host;			/* host being sent to */
56478442df3Seric 	char *user;			/* user being sent to */
56525a99e2eSeric 	char **pvp;
5665dfc646bSeric 	register char **mvp;
56725a99e2eSeric 	register char *p;
568588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5696259796dSeric 	ADDRESS *ctladdr;
570b31e7f2bSeric 	register MCI *mci;
571c77d1c25Seric 	register ADDRESS *to = firstto;
572c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
573772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
574911693bfSbostic 	int rcode;			/* response code */
575e103b48fSeric 	char *firstsig;			/* signature of firstto */
5769c9e68d9Seric 	int pid;
5779c9e68d9Seric 	char *curhost;
5789c9e68d9Seric 	int mpvect[2];
5799c9e68d9Seric 	int rpvect[2];
580ee6bf8dfSeric 	char *pv[MAXPV+1];
581579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
582ee6bf8dfSeric 	char buf[MAXNAME];
583c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
584fabb3bd4Seric 	extern int checkcompat();
5859c9e68d9Seric 	extern FILE *fdopen();
58625a99e2eSeric 
58735490626Seric 	errno = 0;
588ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5895dfc646bSeric 		return (0);
59025a99e2eSeric 
591134746fbSeric #ifdef NAMED_BIND
592912a731aSbostic 	/* unless interactive, try twice, over a minute */
593912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
594912a731aSbostic 		_res.retrans = 30;
595912a731aSbostic 		_res.retry = 2;
596912a731aSbostic 	}
597d4bd8f0eSbostic #endif
598912a731aSbostic 
59951552439Seric 	m = to->q_mailer;
60051552439Seric 	host = to->q_host;
601c9be6216Seric 	CurEnv = e;			/* just in case */
6024384d521Seric 	e->e_statmsg = NULL;
60351552439Seric 
6046ef48975Seric 	if (tTd(10, 1))
6055dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
60651552439Seric 			m->m_mno, host, to->q_user);
607f3dbc832Seric 
608f3dbc832Seric 	/*
609f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
610f3dbc832Seric 	**  connections now, just mark these addresses and return.
611f3dbc832Seric 	**	This is useful if we want to batch connections to
612f3dbc832Seric 	**	reduce load.  This will cause the messages to be
613f3dbc832Seric 	**	queued up, and a daemon will come along to send the
614f3dbc832Seric 	**	messages later.
615f3dbc832Seric 	**		This should be on a per-mailer basis.
616f3dbc832Seric 	*/
617f3dbc832Seric 
61819c47125Seric 	if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) &&
61919c47125Seric 	    bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
620f3dbc832Seric 	{
621f3dbc832Seric 		for (; to != NULL; to = to->q_next)
622f4560e80Seric 		{
623ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
624c6e18ac5Seric 			    to->q_mailer != m)
625f4560e80Seric 				continue;
626f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
627588cad61Seric 			e->e_to = to->q_paddr;
62808b25121Seric 			message("queued");
6292f624c86Seric 			if (LogLevel > 8)
63081161401Seric 				logdelivery(m, NULL, "queued", e);
631f4560e80Seric 		}
632588cad61Seric 		e->e_to = NULL;
633f3dbc832Seric 		return (0);
634f3dbc832Seric 	}
635f3dbc832Seric 
63625a99e2eSeric 	/*
6375dfc646bSeric 	**  Do initial argv setup.
6385dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6395dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6405dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6415dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6425dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6433bea8136Seric 	**		The from address rewrite is expected to make
6443bea8136Seric 	**		the address relative to the other end.
6455dfc646bSeric 	*/
6465dfc646bSeric 
64778442df3Seric 	/* rewrite from address, using rewriting rules */
648efe54562Seric 	rcode = EX_OK;
649efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
650efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
651efe54562Seric 					   &rcode, e));
652ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
653588cad61Seric 	define('h', host, e);			/* to host */
6545dfc646bSeric 	Errors = 0;
6555dfc646bSeric 	pvp = pv;
6565dfc646bSeric 	*pvp++ = m->m_argv[0];
6575dfc646bSeric 
6585dfc646bSeric 	/* insert -f or -r flag as appropriate */
65957fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6605dfc646bSeric 	{
66157fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6625dfc646bSeric 			*pvp++ = "-f";
6635dfc646bSeric 		else
6645dfc646bSeric 			*pvp++ = "-r";
665c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6665dfc646bSeric 	}
6675dfc646bSeric 
6685dfc646bSeric 	/*
6695dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6705dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6715dfc646bSeric 	**  be one of these, and there are only a few more slots
6725dfc646bSeric 	**  in the pv after it.
6735dfc646bSeric 	*/
6745dfc646bSeric 
6755dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6765dfc646bSeric 	{
6772bc47524Seric 		/* can't use strchr here because of sign extension problems */
6782bc47524Seric 		while (*p != '\0')
6792bc47524Seric 		{
6802bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6812bc47524Seric 			{
6822bc47524Seric 				if (*p == 'u')
6835dfc646bSeric 					break;
6842bc47524Seric 			}
6852bc47524Seric 		}
6862bc47524Seric 
6872bc47524Seric 		if (*p != '\0')
6885dfc646bSeric 			break;
6895dfc646bSeric 
6905dfc646bSeric 		/* this entry is safe -- go ahead and process it */
691588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6925dfc646bSeric 		*pvp++ = newstr(buf);
6935dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6945dfc646bSeric 		{
69508b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6965dfc646bSeric 			return (-1);
6975dfc646bSeric 		}
6985dfc646bSeric 	}
699c579ef51Seric 
70033db8731Seric 	/*
70133db8731Seric 	**  If we have no substitution for the user name in the argument
70233db8731Seric 	**  list, we know that we must supply the names otherwise -- and
70333db8731Seric 	**  SMTP is the answer!!
70433db8731Seric 	*/
70533db8731Seric 
7065dfc646bSeric 	if (*mvp == NULL)
707c579ef51Seric 	{
708c579ef51Seric 		/* running SMTP */
7092c7e1b8dSeric # ifdef SMTP
710c579ef51Seric 		clever = TRUE;
711c579ef51Seric 		*pvp = NULL;
7126c2c3107Seric # else /* SMTP */
71333db8731Seric 		/* oops!  we don't implement SMTP */
71408b25121Seric 		syserr("554 SMTP style mailer");
7152c7e1b8dSeric 		return (EX_SOFTWARE);
7166c2c3107Seric # endif /* SMTP */
717c579ef51Seric 	}
7185dfc646bSeric 
7195dfc646bSeric 	/*
7205dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7215dfc646bSeric 	**  run through our address list and append all the addresses
7225dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7235dfc646bSeric 	**  always send another copy later.
7245dfc646bSeric 	*/
7255dfc646bSeric 
7265dfc646bSeric 	tobuf[0] = '\0';
727588cad61Seric 	e->e_to = tobuf;
7286259796dSeric 	ctladdr = NULL;
729e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7305dfc646bSeric 	for (; to != NULL; to = to->q_next)
7315dfc646bSeric 	{
7325dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
73357fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7345dfc646bSeric 			break;
7355dfc646bSeric 
7365dfc646bSeric 		/* if already sent or not for this host, don't send */
737ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
738e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
739e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7405dfc646bSeric 			continue;
7416259796dSeric 
7424b22ea87Seric 		/* avoid overflowing tobuf */
743aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7444b22ea87Seric 			break;
7454b22ea87Seric 
7466ef48975Seric 		if (tTd(10, 1))
747772e6e50Seric 		{
748772e6e50Seric 			printf("\nsend to ");
749772e6e50Seric 			printaddr(to, FALSE);
750772e6e50Seric 		}
751772e6e50Seric 
7526259796dSeric 		/* compute effective uid/gid when sending */
7537da1035fSeric 		if (to->q_mailer == ProgMailer)
7546259796dSeric 			ctladdr = getctladdr(to);
7556259796dSeric 
7565dfc646bSeric 		user = to->q_user;
757588cad61Seric 		e->e_to = to->q_paddr;
75875f1ade9Seric 		if (tTd(10, 5))
75975f1ade9Seric 		{
76075f1ade9Seric 			printf("deliver: QDONTSEND ");
76175f1ade9Seric 			printaddr(to, FALSE);
76275f1ade9Seric 		}
763ee4b0922Seric 		to->q_flags |= QDONTSEND;
7645dfc646bSeric 
7655dfc646bSeric 		/*
7665dfc646bSeric 		**  Check to see that these people are allowed to
7675dfc646bSeric 		**  talk to each other.
7682a6e0786Seric 		*/
7692a6e0786Seric 
77069582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
77169582d2fSeric 		{
77269582d2fSeric 			NoReturn = TRUE;
77308b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
77481161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
77569582d2fSeric 			continue;
77669582d2fSeric 		}
777fabb3bd4Seric 		rcode = checkcompat(to, e);
7781793c9c5Seric 		if (rcode != EX_OK)
7795dfc646bSeric 		{
78081161401Seric 			giveresponse(rcode, m, NULL, e);
7815dfc646bSeric 			continue;
7825dfc646bSeric 		}
7832a6e0786Seric 
7842a6e0786Seric 		/*
7859ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7869ec9501bSeric 		**	about them.
78725a99e2eSeric 		*/
78825a99e2eSeric 
78957fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
79025a99e2eSeric 		{
7911d8f1806Seric 			stripquotes(user);
7921d8f1806Seric 			stripquotes(host);
79325a99e2eSeric 		}
79425a99e2eSeric 
795cdb828c5Seric 		/* hack attack -- delivermail compatibility */
796cdb828c5Seric 		if (m == ProgMailer && *user == '|')
797cdb828c5Seric 			user++;
798cdb828c5Seric 
79925a99e2eSeric 		/*
8003efaed6eSeric 		**  If an error message has already been given, don't
8013efaed6eSeric 		**	bother to send to this address.
8023efaed6eSeric 		**
8033efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
8043efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
8053efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
8063efaed6eSeric 		*/
8073efaed6eSeric 
8086cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8093efaed6eSeric 			continue;
8103efaed6eSeric 
811f2fec898Seric 		/* save statistics.... */
812588cad61Seric 		markstats(e, to);
813f2fec898Seric 
8143efaed6eSeric 		/*
81525a99e2eSeric 		**  See if this user name is "special".
81625a99e2eSeric 		**	If the user name has a slash in it, assume that this
81751552439Seric 		**	is a file -- send it off without further ado.  Note
81851552439Seric 		**	that this type of addresses is not processed along
81951552439Seric 		**	with the others, so we fudge on the To person.
82025a99e2eSeric 		*/
82125a99e2eSeric 
8222c017f8dSeric 		if (m == FileMailer)
82325a99e2eSeric 		{
824b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
82581161401Seric 			giveresponse(rcode, m, NULL, e);
826dde5acadSeric 			if (rcode == EX_OK)
827dde5acadSeric 				to->q_flags |= QSENT;
8285dfc646bSeric 			continue;
82925a99e2eSeric 		}
83025a99e2eSeric 
83113bbc08cSeric 		/*
83213bbc08cSeric 		**  Address is verified -- add this user to mailer
83313bbc08cSeric 		**  argv, and add it to the print list of recipients.
83413bbc08cSeric 		*/
83513bbc08cSeric 
836508daeccSeric 		/* link together the chain of recipients */
837508daeccSeric 		to->q_tchain = tochain;
838508daeccSeric 		tochain = to;
839508daeccSeric 
8405dfc646bSeric 		/* create list of users for error messages */
841db8841e9Seric 		(void) strcat(tobuf, ",");
842db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
843588cad61Seric 		define('u', user, e);		/* to user */
844588cad61Seric 		define('z', to->q_home, e);	/* user's home */
8455dfc646bSeric 
846c579ef51Seric 		/*
847508daeccSeric 		**  Expand out this user into argument list.
848c579ef51Seric 		*/
849c579ef51Seric 
850508daeccSeric 		if (!clever)
851c579ef51Seric 		{
852588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8535dfc646bSeric 			*pvp++ = newstr(buf);
8545dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8555dfc646bSeric 			{
8565dfc646bSeric 				/* allow some space for trailing parms */
8575dfc646bSeric 				break;
8585dfc646bSeric 			}
8595dfc646bSeric 		}
860c579ef51Seric 	}
8615dfc646bSeric 
862145b49b1Seric 	/* see if any addresses still exist */
863145b49b1Seric 	if (tobuf[0] == '\0')
864c579ef51Seric 	{
865588cad61Seric 		define('g', (char *) NULL, e);
866145b49b1Seric 		return (0);
867c579ef51Seric 	}
868145b49b1Seric 
8695dfc646bSeric 	/* print out messages as full list */
87063780dbdSeric 	e->e_to = tobuf + 1;
8715dfc646bSeric 
8725dfc646bSeric 	/*
8735dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8745dfc646bSeric 	*/
8755dfc646bSeric 
876c579ef51Seric 	while (!clever && *++mvp != NULL)
8775dfc646bSeric 	{
878588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8795dfc646bSeric 		*pvp++ = newstr(buf);
8805dfc646bSeric 		if (pvp >= &pv[MAXPV])
88108b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8825dfc646bSeric 	}
8835dfc646bSeric 	*pvp++ = NULL;
8845dfc646bSeric 
88525a99e2eSeric 	/*
88625a99e2eSeric 	**  Call the mailer.
8876328bdf7Seric 	**	The argument vector gets built, pipes
88825a99e2eSeric 	**	are created as necessary, and we fork & exec as
8896328bdf7Seric 	**	appropriate.
890c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
89125a99e2eSeric 	*/
89225a99e2eSeric 
8937ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
89486b26461Seric 		ctladdr = &e->e_from;
895134746fbSeric #ifdef NAMED_BIND
8962bcc6d2dSeric 	if (ConfigLevel < 2)
897912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
898134746fbSeric #endif
8999c9e68d9Seric 
9009c9e68d9Seric 	if (tTd(11, 1))
901134746fbSeric 	{
9029c9e68d9Seric 		printf("openmailer:");
9039c9e68d9Seric 		printav(pv);
9049c9e68d9Seric 	}
9059c9e68d9Seric 	errno = 0;
9069c9e68d9Seric 
9079c9e68d9Seric 	CurHostName = m->m_mailer;
9089c9e68d9Seric 
9099c9e68d9Seric 	/*
9109c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9119c9e68d9Seric 	**  connection.
9129c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9139c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9149c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9159c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9169c9e68d9Seric 	*/
9179c9e68d9Seric 
9189c9e68d9Seric 	curhost = NULL;
9199c9e68d9Seric 
9209c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9219c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9229c9e68d9Seric 	{
9239c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9249c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9259c9e68d9Seric 		mci->mci_in = stdin;
9269c9e68d9Seric 		mci->mci_out = stdout;
9279c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9289c9e68d9Seric 		mci->mci_mailer = m;
9299c9e68d9Seric 	}
9309c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9319c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9329c9e68d9Seric 	{
9339c9e68d9Seric #ifdef DAEMON
9349c9e68d9Seric 		register int i;
9359c9e68d9Seric 		register u_short port;
9369c9e68d9Seric 
9379c9e68d9Seric 		CurHostName = pv[1];
9389c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9399c9e68d9Seric 
9409c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9419c9e68d9Seric 		{
9429c9e68d9Seric 			syserr("null signature");
943845e533cSeric 			rcode = EX_OSERR;
944b31e7f2bSeric 			goto give_up;
945b31e7f2bSeric 		}
9469c9e68d9Seric 
9479c9e68d9Seric 		if (!clever)
9489c9e68d9Seric 		{
9499c9e68d9Seric 			syserr("554 non-clever IPC");
9509c9e68d9Seric 			rcode = EX_OSERR;
9519c9e68d9Seric 			goto give_up;
9529c9e68d9Seric 		}
9539c9e68d9Seric 		if (pv[2] != NULL)
9549c9e68d9Seric 			port = atoi(pv[2]);
9559c9e68d9Seric 		else
9569c9e68d9Seric 			port = 0;
9579c9e68d9Seric tryhost:
9589c9e68d9Seric 		mci = NULL;
9599c9e68d9Seric 		while (*curhost != '\0')
9609c9e68d9Seric 		{
9619c9e68d9Seric 			register char *p;
9629c9e68d9Seric 			static char hostbuf[MAXNAME];
9639c9e68d9Seric 
9649c9e68d9Seric 			mci = NULL;
9659c9e68d9Seric 
9669c9e68d9Seric 			/* pull the next host from the signature */
9679c9e68d9Seric 			p = strchr(curhost, ':');
9689c9e68d9Seric 			if (p == NULL)
9699c9e68d9Seric 				p = &curhost[strlen(curhost)];
9709c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9719c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9729c9e68d9Seric 			if (*p != '\0')
9739c9e68d9Seric 				p++;
9749c9e68d9Seric 			curhost = p;
9759c9e68d9Seric 
9769c9e68d9Seric 			/* see if we already know that this host is fried */
9779c9e68d9Seric 			CurHostName = hostbuf;
9789c9e68d9Seric 			mci = mci_get(hostbuf, m);
9799c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9809c9e68d9Seric 			{
9819c9e68d9Seric 				if (tTd(11, 1))
9829c9e68d9Seric 				{
9839c9e68d9Seric 					printf("openmailer: ");
9849c9e68d9Seric 					mci_dump(mci);
9859c9e68d9Seric 				}
9869c9e68d9Seric 				CurHostName = mci->mci_host;
9879c9e68d9Seric 				break;
9889c9e68d9Seric 			}
9899c9e68d9Seric 			mci->mci_mailer = m;
9909c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9919c9e68d9Seric 				continue;
9929c9e68d9Seric 
9939c9e68d9Seric 			/* try the connection */
9949c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9959c9e68d9Seric 			message("Connecting to %s (%s)...",
9969c9e68d9Seric 				hostbuf, m->m_name);
9979c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
9989c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
9999c9e68d9Seric 			mci->mci_exitstat = i;
10009c9e68d9Seric 			mci->mci_errno = errno;
1001*f170942cSeric #ifdef NAMED_BIND
1002*f170942cSeric 			mci->mci_herrno = h_errno;
1003*f170942cSeric #endif
10049c9e68d9Seric 			if (i == EX_OK)
10059c9e68d9Seric 			{
10069c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10079c9e68d9Seric 				mci_cache(mci);
1008*f170942cSeric 				if (TrafficLogFile != NULL)
1009*f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1010*f170942cSeric 						getpid(), hostbuf);
10119c9e68d9Seric 				break;
10129c9e68d9Seric 			}
10139c9e68d9Seric 			else if (tTd(11, 1))
10149c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10159c9e68d9Seric 					i, errno);
10169c9e68d9Seric 
10179c9e68d9Seric 
10189c9e68d9Seric 			/* enter status of this host */
10199c9e68d9Seric 			setstat(i);
10209c9e68d9Seric 		}
10219c9e68d9Seric 		mci->mci_pid = 0;
10229c9e68d9Seric #else /* no DAEMON */
10239c9e68d9Seric 		syserr("554 openmailer: no IPC");
10249c9e68d9Seric 		if (tTd(11, 1))
10259c9e68d9Seric 			printf("openmailer: NULL\n");
10269c9e68d9Seric 		return NULL;
10279c9e68d9Seric #endif /* DAEMON */
10289c9e68d9Seric 	}
10299c9e68d9Seric 	else
10309c9e68d9Seric 	{
1031*f170942cSeric #ifdef XDEBUG
1032*f170942cSeric 		char wbuf[MAXLINE];
10336fe8c3bcSeric 
10346fe8c3bcSeric 		/* make absolutely certain 0, 1, and 2 are in use */
1035*f170942cSeric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
1036*f170942cSeric 		checkfd012(wbuf);
1037*f170942cSeric #endif
10380e3bfef5Seric 
1039*f170942cSeric 		if (TrafficLogFile != NULL)
10400e3bfef5Seric 		{
1041*f170942cSeric 			char **av;
1042*f170942cSeric 
1043*f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1044*f170942cSeric 			for (av = pv; *av != NULL; av++)
1045*f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1046*f170942cSeric 			fprintf(TrafficLogFile, "\n");
10476fe8c3bcSeric 		}
10486fe8c3bcSeric 
10499c9e68d9Seric 		/* create a pipe to shove the mail through */
10509c9e68d9Seric 		if (pipe(mpvect) < 0)
10519c9e68d9Seric 		{
10520e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10530e3bfef5Seric 				e->e_to, m->m_name);
10549c9e68d9Seric 			if (tTd(11, 1))
10559c9e68d9Seric 				printf("openmailer: NULL\n");
10569c9e68d9Seric 			rcode = EX_OSERR;
10579c9e68d9Seric 			goto give_up;
10589c9e68d9Seric 		}
10599c9e68d9Seric 
10609c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10619c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10629c9e68d9Seric 		{
10630e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10640e3bfef5Seric 				e->e_to, m->m_name);
10659c9e68d9Seric 			(void) close(mpvect[0]);
10669c9e68d9Seric 			(void) close(mpvect[1]);
10679c9e68d9Seric 			if (tTd(11, 1))
10689c9e68d9Seric 				printf("openmailer: NULL\n");
10699c9e68d9Seric 			rcode = EX_OSERR;
10709c9e68d9Seric 			goto give_up;
10719c9e68d9Seric 		}
10729c9e68d9Seric 
10739c9e68d9Seric 		/*
10749c9e68d9Seric 		**  Actually fork the mailer process.
10759c9e68d9Seric 		**	DOFORK is clever about retrying.
10769c9e68d9Seric 		**
10779c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10789c9e68d9Seric 		**	around so that endmail will get it.
10799c9e68d9Seric 		*/
10809c9e68d9Seric 
10819c9e68d9Seric 		if (e->e_xfp != NULL)
10829c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10839c9e68d9Seric 		(void) fflush(stdout);
10849c9e68d9Seric # ifdef SIGCHLD
10859c9e68d9Seric 		(void) signal(SIGCHLD, SIG_DFL);
10869c9e68d9Seric # endif /* SIGCHLD */
10879c9e68d9Seric 		DOFORK(FORK);
10889c9e68d9Seric 		/* pid is set by DOFORK */
10899c9e68d9Seric 		if (pid < 0)
10909c9e68d9Seric 		{
10919c9e68d9Seric 			/* failure */
10920e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
10930e3bfef5Seric 				e->e_to, m->m_name);
10949c9e68d9Seric 			(void) close(mpvect[0]);
10959c9e68d9Seric 			(void) close(mpvect[1]);
10969c9e68d9Seric 			if (clever)
10979c9e68d9Seric 			{
10989c9e68d9Seric 				(void) close(rpvect[0]);
10999c9e68d9Seric 				(void) close(rpvect[1]);
11009c9e68d9Seric 			}
11019c9e68d9Seric 			if (tTd(11, 1))
11029c9e68d9Seric 				printf("openmailer: NULL\n");
11039c9e68d9Seric 			rcode = EX_OSERR;
11049c9e68d9Seric 			goto give_up;
11059c9e68d9Seric 		}
11069c9e68d9Seric 		else if (pid == 0)
11079c9e68d9Seric 		{
11089c9e68d9Seric 			int i;
11099c9e68d9Seric 			int saveerrno;
11109c9e68d9Seric 			char **ep;
11119c9e68d9Seric 			char *env[MAXUSERENVIRON];
11129c9e68d9Seric 			extern char **environ;
11139c9e68d9Seric 			extern int DtableSize;
11149c9e68d9Seric 
11159c9e68d9Seric 			/* child -- set up input & exec mailer */
11169c9e68d9Seric 			/* make diagnostic output be standard output */
11179c9e68d9Seric 			(void) signal(SIGINT, SIG_IGN);
11189c9e68d9Seric 			(void) signal(SIGHUP, SIG_IGN);
11199c9e68d9Seric 			(void) signal(SIGTERM, SIG_DFL);
11209c9e68d9Seric 
11219c9e68d9Seric 			/* close any other cached connections */
11229c9e68d9Seric 			mci_flush(FALSE, mci);
11239c9e68d9Seric 
1124b986f6aaSeric 			/* move into some "safe" directory */
1125b986f6aaSeric 			if (m->m_execdir != NULL)
1126b986f6aaSeric 			{
1127b986f6aaSeric 				char *p, *q;
1128b986f6aaSeric 				char buf[MAXLINE];
1129b986f6aaSeric 
1130b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1131b986f6aaSeric 				{
1132b986f6aaSeric 					q = strchr(p, ':');
1133b986f6aaSeric 					if (q != NULL)
1134b986f6aaSeric 						*q = '\0';
1135b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1136b986f6aaSeric 					if (q != NULL)
1137b986f6aaSeric 						*q++ = ':';
1138b986f6aaSeric 					if (tTd(11, 20))
1139b986f6aaSeric 						printf("openmailer: trydir %s\n",
1140b986f6aaSeric 							buf);
1141b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1142b986f6aaSeric 						break;
1143b986f6aaSeric 				}
1144b986f6aaSeric 			}
1145b986f6aaSeric 
11469c9e68d9Seric 			/* arrange to filter std & diag output of command */
11479c9e68d9Seric 			if (clever)
11489c9e68d9Seric 			{
11499c9e68d9Seric 				(void) close(rpvect[0]);
11506fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11516fe8c3bcSeric 				{
11520e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11530e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11546fe8c3bcSeric 					_exit(EX_OSERR);
11556fe8c3bcSeric 				}
11569c9e68d9Seric 				(void) close(rpvect[1]);
11579c9e68d9Seric 			}
11589c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11599c9e68d9Seric 			{
11609c9e68d9Seric 				/* put mailer output in transcript */
11616fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11626fe8c3bcSeric 				{
11630e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11640e3bfef5Seric 						e->e_to, m->m_name,
11656fe8c3bcSeric 						fileno(e->e_xfp));
11666fe8c3bcSeric 					_exit(EX_OSERR);
11679c9e68d9Seric 				}
11686fe8c3bcSeric 			}
11696fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11706fe8c3bcSeric 			{
11710e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
11720e3bfef5Seric 					e->e_to, m->m_name);
11736fe8c3bcSeric 				_exit(EX_OSERR);
11746fe8c3bcSeric 			}
11759c9e68d9Seric 
11769c9e68d9Seric 			/* arrange to get standard input */
11779c9e68d9Seric 			(void) close(mpvect[1]);
11789c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11799c9e68d9Seric 			{
11800e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
11810e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
11829c9e68d9Seric 				_exit(EX_OSERR);
11839c9e68d9Seric 			}
11849c9e68d9Seric 			(void) close(mpvect[0]);
11859c9e68d9Seric 			if (!bitnset(M_RESTR, m->m_flags))
11869c9e68d9Seric 			{
11879c9e68d9Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
11889c9e68d9Seric 				{
11899c9e68d9Seric 					(void) setgid(DefGid);
11909c9e68d9Seric 					(void) initgroups(DefUser, DefGid);
11919c9e68d9Seric 					(void) setuid(DefUid);
11929c9e68d9Seric 				}
11939c9e68d9Seric 				else
11949c9e68d9Seric 				{
11959c9e68d9Seric 					(void) setgid(ctladdr->q_gid);
11969c9e68d9Seric 					(void) initgroups(ctladdr->q_ruser?
11979c9e68d9Seric 						ctladdr->q_ruser: ctladdr->q_user,
11989c9e68d9Seric 						ctladdr->q_gid);
11999c9e68d9Seric 					(void) setuid(ctladdr->q_uid);
12009c9e68d9Seric 				}
12019c9e68d9Seric 			}
12029c9e68d9Seric 
12039c9e68d9Seric 			/* arrange for all the files to be closed */
12049c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12059c9e68d9Seric 			{
12069c9e68d9Seric 				register int j;
12079c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12089c9e68d9Seric 					(void)fcntl(i, F_SETFD, j|1);
12099c9e68d9Seric 			}
12109c9e68d9Seric 
12119c9e68d9Seric 			/* set up the mailer environment */
12129c9e68d9Seric 			i = 0;
12139c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12149c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12159c9e68d9Seric 			{
12169c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12179c9e68d9Seric 					env[i++] = *ep;
12189c9e68d9Seric 			}
12199c9e68d9Seric 			env[i++] = NULL;
12209c9e68d9Seric 
12219c9e68d9Seric 			/* try to execute the mailer */
12229c9e68d9Seric 			execve(m->m_mailer, pv, env);
12239c9e68d9Seric 			saveerrno = errno;
12249c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12257c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12267c941fd2Seric 				_exit(EX_OSERR);
12279c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12289c9e68d9Seric 		}
12299c9e68d9Seric 
12309c9e68d9Seric 		/*
12319c9e68d9Seric 		**  Set up return value.
12329c9e68d9Seric 		*/
12339c9e68d9Seric 
12349c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12359c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12369c9e68d9Seric 		mci->mci_mailer = m;
12379c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12389c9e68d9Seric 		mci->mci_pid = pid;
12399c9e68d9Seric 		(void) close(mpvect[0]);
12409c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
12419c9e68d9Seric 		if (clever)
12429c9e68d9Seric 		{
12439c9e68d9Seric 			(void) close(rpvect[1]);
12449c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
12459c9e68d9Seric 		}
12469c9e68d9Seric 		else
12479c9e68d9Seric 		{
12489c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12499c9e68d9Seric 			mci->mci_in = NULL;
12509c9e68d9Seric 		}
12519c9e68d9Seric 	}
12529c9e68d9Seric 
12539c9e68d9Seric 	/*
12549c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12559c9e68d9Seric 	*/
12569c9e68d9Seric 
12579c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12589c9e68d9Seric 	{
12599c9e68d9Seric 		smtpinit(m, mci, e);
12609c9e68d9Seric 	}
12619c9e68d9Seric 	if (tTd(11, 1))
12629c9e68d9Seric 	{
12639c9e68d9Seric 		printf("openmailer: ");
12649c9e68d9Seric 		mci_dump(mci);
12659c9e68d9Seric 	}
12669c9e68d9Seric 
12679c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1268b31e7f2bSeric 	{
1269b31e7f2bSeric 		/* couldn't open the mailer */
1270b31e7f2bSeric 		rcode = mci->mci_exitstat;
12712a6bc25bSeric 		errno = mci->mci_errno;
1272*f170942cSeric #ifdef NAMED_BIND
1273*f170942cSeric 		h_errno = mci->mci_herrno;
1274*f170942cSeric #endif
1275b31e7f2bSeric 		if (rcode == EX_OK)
1276b31e7f2bSeric 		{
1277b31e7f2bSeric 			/* shouldn't happen */
127808b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12796b0f339dSeric 				rcode, mci->mci_state, firstsig);
1280b31e7f2bSeric 			rcode = EX_SOFTWARE;
1281b31e7f2bSeric 		}
1282e9277e33Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
128390891494Seric 		{
128490891494Seric 			/* try next MX site */
128590891494Seric 			goto tryhost;
128690891494Seric 		}
1287b31e7f2bSeric 	}
1288b31e7f2bSeric 	else if (!clever)
1289b31e7f2bSeric 	{
1290b31e7f2bSeric 		/*
1291b31e7f2bSeric 		**  Format and send message.
1292b31e7f2bSeric 		*/
129315d084d5Seric 
1294b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1295b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1296b31e7f2bSeric 		putline("\n", mci->mci_out, m);
129703c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1298b31e7f2bSeric 
1299b31e7f2bSeric 		/* get the exit status */
1300c9be6216Seric 		rcode = endmailer(mci, e, pv);
1301134746fbSeric 	}
1302134746fbSeric 	else
1303b31e7f2bSeric #ifdef SMTP
1304134746fbSeric 	{
1305b31e7f2bSeric 		/*
1306b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1307b31e7f2bSeric 		*/
130815d084d5Seric 
1309b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1310b31e7f2bSeric 		if (rcode == EX_OK)
131175889e88Seric 		{
1312ded0d3daSkarels 			register char *t = tobuf;
1313ded0d3daSkarels 			register int i;
1314ded0d3daSkarels 
1315588cad61Seric 			/* send the recipient list */
131663780dbdSeric 			tobuf[0] = '\0';
131775889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
131875889e88Seric 			{
131963780dbdSeric 				e->e_to = to->q_paddr;
132015d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
132175889e88Seric 				{
132283b7ddc9Seric 					markfailure(e, to, i);
132381161401Seric 					giveresponse(i, m, mci, e);
132463780dbdSeric 				}
132575889e88Seric 				else
132675889e88Seric 				{
1327911693bfSbostic 					*t++ = ',';
1328b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1329b31e7f2bSeric 						continue;
1330588cad61Seric 				}
1331588cad61Seric 			}
1332588cad61Seric 
133363780dbdSeric 			/* now send the data */
133463780dbdSeric 			if (tobuf[0] == '\0')
1335b31e7f2bSeric 			{
13369c9e68d9Seric 				rcode = EX_OK;
133763780dbdSeric 				e->e_to = NULL;
1338b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1339b31e7f2bSeric 					smtprset(m, mci, e);
1340b31e7f2bSeric 			}
134175889e88Seric 			else
134275889e88Seric 			{
134363780dbdSeric 				e->e_to = tobuf + 1;
134475889e88Seric 				rcode = smtpdata(m, mci, e);
134563780dbdSeric 			}
134663780dbdSeric 
134763780dbdSeric 			/* now close the connection */
1348b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
134915d084d5Seric 				smtpquit(m, mci, e);
135063780dbdSeric 		}
13519c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13529c9e68d9Seric 		{
13539c9e68d9Seric 			/* try next MX site */
13549c9e68d9Seric 			goto tryhost;
13559c9e68d9Seric 		}
1356c579ef51Seric 	}
1357b31e7f2bSeric #else /* not SMTP */
1358a05b3449Sbostic 	{
135908b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1360845e533cSeric 		rcode = EX_CONFIG;
1361b31e7f2bSeric 		goto give_up;
1362a05b3449Sbostic 	}
1363b31e7f2bSeric #endif /* SMTP */
1364134746fbSeric #ifdef NAMED_BIND
13652bcc6d2dSeric 	if (ConfigLevel < 2)
1366912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1367134746fbSeric #endif
13685dfc646bSeric 
1369b31e7f2bSeric 	/* arrange a return receipt if requested */
13708c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1371b31e7f2bSeric 	{
1372b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1373b31e7f2bSeric 		/* do we want to send back more info? */
1374b31e7f2bSeric 	}
1375b31e7f2bSeric 
1376c77d1c25Seric 	/*
137763780dbdSeric 	**  Do final status disposal.
137863780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1379c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1380c77d1c25Seric 	**		addressees.
1381c77d1c25Seric 	*/
1382c77d1c25Seric 
1383b31e7f2bSeric   give_up:
138463780dbdSeric 	if (tobuf[0] != '\0')
138581161401Seric 		giveresponse(rcode, m, mci, e);
1386772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1387b31e7f2bSeric 	{
1388dde5acadSeric 		if (rcode != EX_OK)
138983b7ddc9Seric 			markfailure(e, to, rcode);
1390dde5acadSeric 		else
1391655518ecSeric 		{
1392dde5acadSeric 			to->q_flags |= QSENT;
1393655518ecSeric 			e->e_nsent++;
1394655518ecSeric 		}
1395b31e7f2bSeric 	}
1396b31e7f2bSeric 
1397b31e7f2bSeric 	/*
1398b31e7f2bSeric 	**  Restore state and return.
1399b31e7f2bSeric 	*/
1400c77d1c25Seric 
140135490626Seric 	errno = 0;
1402588cad61Seric 	define('g', (char *) NULL, e);
14035826d9d3Seric 	return (rcode);
140425a99e2eSeric }
14055dfc646bSeric /*
140683b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
140783b7ddc9Seric **
140883b7ddc9Seric **	Parameters:
140983b7ddc9Seric **		e -- the envelope we are sending.
141083b7ddc9Seric **		q -- the address to mark.
141183b7ddc9Seric **		rcode -- the code signifying the particular failure.
141283b7ddc9Seric **
141383b7ddc9Seric **	Returns:
141483b7ddc9Seric **		none.
141583b7ddc9Seric **
141683b7ddc9Seric **	Side Effects:
141783b7ddc9Seric **		marks the address (and possibly the envelope) with the
141883b7ddc9Seric **			failure so that an error will be returned or
141983b7ddc9Seric **			the message will be queued, as appropriate.
142083b7ddc9Seric */
142183b7ddc9Seric 
142283b7ddc9Seric markfailure(e, q, rcode)
142383b7ddc9Seric 	register ENVELOPE *e;
142483b7ddc9Seric 	register ADDRESS *q;
142583b7ddc9Seric 	int rcode;
142683b7ddc9Seric {
142719c47125Seric 	char buf[MAXLINE];
142819c47125Seric 
142983b7ddc9Seric 	if (rcode == EX_OK)
143083b7ddc9Seric 		return;
1431*f170942cSeric 	else if (rcode == EX_TEMPFAIL)
143283b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1433*f170942cSeric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
1434*f170942cSeric 		q->q_flags |= QBADADDR;
143583b7ddc9Seric }
143683b7ddc9Seric /*
1437c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1438c579ef51Seric **
1439c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1440c579ef51Seric **	violation), so we report those specially.  For other
1441c579ef51Seric **	errors, we choose a status message (into statmsg),
1442c579ef51Seric **	and if it represents an error, we print it.
1443c579ef51Seric **
1444c579ef51Seric **	Parameters:
1445c579ef51Seric **		pid -- pid of mailer.
1446c9be6216Seric **		e -- the current envelope.
1447c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1448c9be6216Seric **			(for error messages).
1449c579ef51Seric **
1450c579ef51Seric **	Returns:
1451c579ef51Seric **		exit code of mailer.
1452c579ef51Seric **
1453c579ef51Seric **	Side Effects:
1454c579ef51Seric **		none.
1455c579ef51Seric */
1456c579ef51Seric 
1457c9be6216Seric endmailer(mci, e, pv)
1458b31e7f2bSeric 	register MCI *mci;
1459c9be6216Seric 	register ENVELOPE *e;
1460c9be6216Seric 	char **pv;
1461c579ef51Seric {
1462588cad61Seric 	int st;
1463c579ef51Seric 
146475889e88Seric 	/* close any connections */
146575889e88Seric 	if (mci->mci_in != NULL)
1466c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
146775889e88Seric 	if (mci->mci_out != NULL)
1468c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
146975889e88Seric 	mci->mci_in = mci->mci_out = NULL;
147075889e88Seric 	mci->mci_state = MCIS_CLOSED;
147175889e88Seric 
147233db8731Seric 	/* in the IPC case there is nothing to wait for */
147375889e88Seric 	if (mci->mci_pid == 0)
147433db8731Seric 		return (EX_OK);
147533db8731Seric 
147633db8731Seric 	/* wait for the mailer process to die and collect status */
147775889e88Seric 	st = waitfor(mci->mci_pid);
1478588cad61Seric 	if (st == -1)
147978de67c1Seric 	{
1480c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1481588cad61Seric 		return (EX_SOFTWARE);
1482c579ef51Seric 	}
148333db8731Seric 
148433db8731Seric 	/* see if it died a horrid death */
1485c579ef51Seric 	if ((st & 0377) != 0)
1486c579ef51Seric 	{
1487c9be6216Seric 		syserr("mailer %s died with signal %o", pv[0], st);
1488c9be6216Seric 
1489c9be6216Seric 		/* log the arguments */
1490c9be6216Seric 		if (e->e_xfp != NULL)
1491c9be6216Seric 		{
1492c9be6216Seric 			register char **av;
1493c9be6216Seric 
1494c9be6216Seric 			fprintf(e->e_xfp, "Arguments:");
1495c9be6216Seric 			for (av = pv; *av != NULL; av++)
1496c9be6216Seric 				fprintf(e->e_xfp, " %s", *av);
1497c9be6216Seric 			fprintf(e->e_xfp, "\n");
1498c9be6216Seric 		}
1499c9be6216Seric 
15005f73204aSeric 		ExitStat = EX_TEMPFAIL;
15015f73204aSeric 		return (EX_TEMPFAIL);
1502c579ef51Seric 	}
150333db8731Seric 
150433db8731Seric 	/* normal death -- return status */
1505588cad61Seric 	st = (st >> 8) & 0377;
1506588cad61Seric 	return (st);
1507c579ef51Seric }
1508c579ef51Seric /*
150925a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
151025a99e2eSeric **
151125a99e2eSeric **	Parameters:
151225a99e2eSeric **		stat -- the status code from the mailer (high byte
151325a99e2eSeric **			only; core dumps must have been taken care of
151425a99e2eSeric **			already).
151581161401Seric **		m -- the mailer info for this mailer.
151681161401Seric **		mci -- the mailer connection info -- can be NULL if the
151781161401Seric **			response is given before the connection is made.
151881161401Seric **		e -- the current envelope.
151925a99e2eSeric **
152025a99e2eSeric **	Returns:
1521db8841e9Seric **		none.
152225a99e2eSeric **
152325a99e2eSeric **	Side Effects:
1524c1f9df2cSeric **		Errors may be incremented.
152525a99e2eSeric **		ExitStat may be set.
152625a99e2eSeric */
152725a99e2eSeric 
152881161401Seric giveresponse(stat, m, mci, e)
152925a99e2eSeric 	int stat;
1530588cad61Seric 	register MAILER *m;
153181161401Seric 	register MCI *mci;
1532198d9be0Seric 	ENVELOPE *e;
153325a99e2eSeric {
15349c16475dSeric 	register const char *statmsg;
153525a99e2eSeric 	extern char *SysExMsg[];
153625a99e2eSeric 	register int i;
1537d4bd8f0eSbostic 	extern int N_SysEx;
1538198d9be0Seric 	char buf[MAXLINE];
153925a99e2eSeric 
154013bbc08cSeric 	/*
154113bbc08cSeric 	**  Compute status message from code.
154213bbc08cSeric 	*/
154313bbc08cSeric 
154425a99e2eSeric 	i = stat - EX__BASE;
1545588cad61Seric 	if (stat == 0)
15466fe8c3bcSeric 	{
1547588cad61Seric 		statmsg = "250 Sent";
1548ce5531bdSeric 		if (e->e_statmsg != NULL)
15496fe8c3bcSeric 		{
1550ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15516fe8c3bcSeric 			statmsg = buf;
15526fe8c3bcSeric 		}
15536fe8c3bcSeric 	}
1554588cad61Seric 	else if (i < 0 || i > N_SysEx)
1555588cad61Seric 	{
1556588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1557588cad61Seric 		stat = EX_UNAVAILABLE;
1558588cad61Seric 		statmsg = buf;
1559588cad61Seric 	}
1560198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1561198d9be0Seric 	{
15627d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1563d4bd8f0eSbostic #ifdef NAMED_BIND
1564f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1565f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1566f28da541Smiriam 		else
1567d4bd8f0eSbostic #endif
1568f28da541Smiriam 		{
15698557d168Seric 			if (errno != 0)
1570d87e85f3Seric 				statmsg = errstring(errno);
1571d87e85f3Seric 			else
1572d87e85f3Seric 			{
1573d87e85f3Seric #ifdef SMTP
1574d87e85f3Seric 				extern char SmtpError[];
1575d87e85f3Seric 
1576d87e85f3Seric 				statmsg = SmtpError;
15776c2c3107Seric #else /* SMTP */
1578d87e85f3Seric 				statmsg = NULL;
15796c2c3107Seric #endif /* SMTP */
1580d87e85f3Seric 			}
1581f28da541Smiriam 		}
1582d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1583d87e85f3Seric 		{
158487c9b3e7Seric 			(void) strcat(buf, ": ");
1585d87e85f3Seric 			(void) strcat(buf, statmsg);
15868557d168Seric 		}
1587198d9be0Seric 		statmsg = buf;
1588198d9be0Seric 	}
1589*f170942cSeric #ifdef NAMED_BIND
1590*f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1591*f170942cSeric 	{
1592*f170942cSeric 		statmsg = errstring(h_errno + MAX_ERRNO);
1593*f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1594*f170942cSeric 		statmsg = buf;
1595*f170942cSeric 	}
1596*f170942cSeric #endif
159725a99e2eSeric 	else
1598d87e85f3Seric 	{
159925a99e2eSeric 		statmsg = SysExMsg[i];
16007d55540cSeric 		if (*statmsg++ == ':')
16017d55540cSeric 		{
16027d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16037d55540cSeric 			statmsg = buf;
16047d55540cSeric 		}
1605d87e85f3Seric 	}
1606588cad61Seric 
1607588cad61Seric 	/*
1608588cad61Seric 	**  Print the message as appropriate
1609588cad61Seric 	*/
1610588cad61Seric 
1611198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1612e12d03eeSeric 		message(&statmsg[4], errstring(errno));
161325a99e2eSeric 	else
161425a99e2eSeric 	{
1615c1f9df2cSeric 		Errors++;
1616e12d03eeSeric 		usrerr(statmsg, errstring(errno));
161725a99e2eSeric 	}
161825a99e2eSeric 
161925a99e2eSeric 	/*
162025a99e2eSeric 	**  Final cleanup.
162125a99e2eSeric 	**	Log a record of the transaction.  Compute the new
162225a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
162325a99e2eSeric 	**	that.
162425a99e2eSeric 	*/
162525a99e2eSeric 
16262f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
162781161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1628eb238f8cSeric 
1629eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1630eb238f8cSeric 		setstat(stat);
1631198d9be0Seric 	if (stat != EX_OK)
1632198d9be0Seric 	{
1633198d9be0Seric 		if (e->e_message != NULL)
1634198d9be0Seric 			free(e->e_message);
1635198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1636198d9be0Seric 	}
16378557d168Seric 	errno = 0;
1638d4bd8f0eSbostic #ifdef NAMED_BIND
1639f28da541Smiriam 	h_errno = 0;
1640d4bd8f0eSbostic #endif
1641eb238f8cSeric }
1642eb238f8cSeric /*
1643eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1644eb238f8cSeric **
1645eb238f8cSeric **	Parameters:
164681161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
164781161401Seric **		mci -- the mailer connection info -- can be NULL if the
164881161401Seric **			log is occuring when no connection is active.
164981161401Seric **		stat -- the message to print for the status.
165081161401Seric **		e -- the current envelope.
1651eb238f8cSeric **
1652eb238f8cSeric **	Returns:
1653eb238f8cSeric **		none
1654eb238f8cSeric **
1655eb238f8cSeric **	Side Effects:
1656eb238f8cSeric **		none
1657eb238f8cSeric */
1658eb238f8cSeric 
165981161401Seric logdelivery(m, mci, stat, e)
166081161401Seric 	MAILER *m;
166181161401Seric 	register MCI *mci;
1662eb238f8cSeric 	char *stat;
1663b31e7f2bSeric 	register ENVELOPE *e;
16645cf56be3Seric {
1665eb238f8cSeric # ifdef LOG
1666d6acf3eeSeric 	char buf[512];
16679507d1f9Seric 
166848ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
166981161401Seric 
167048ed5d33Seric 	if (m != NULL)
167171ff6caaSeric 	{
167248ed5d33Seric 		(void) strcat(buf, ", mailer=");
167348ed5d33Seric 		(void) strcat(buf, m->m_name);
167471ff6caaSeric 	}
167548ed5d33Seric 
167648ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
167771ff6caaSeric 	{
167871ff6caaSeric # ifdef DAEMON
1679e2f2f828Seric 		extern SOCKADDR CurHostAddr;
168048ed5d33Seric # endif
168171ff6caaSeric 
168248ed5d33Seric 		(void) strcat(buf, ", relay=");
168348ed5d33Seric 		(void) strcat(buf, mci->mci_host);
168448ed5d33Seric 
168548ed5d33Seric # ifdef DAEMON
168648ed5d33Seric 		(void) strcat(buf, " (");
1687e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
168848ed5d33Seric 		(void) strcat(buf, ")");
168971ff6caaSeric # endif
169071ff6caaSeric 	}
16919507d1f9Seric 	else
169248ed5d33Seric 	{
169348ed5d33Seric 		char *p = macvalue('h', e);
16949507d1f9Seric 
169548ed5d33Seric 		if (p != NULL && p[0] != '\0')
169648ed5d33Seric 		{
169748ed5d33Seric 			(void) strcat(buf, ", relay=");
169848ed5d33Seric 			(void) strcat(buf, p);
169948ed5d33Seric 		}
170048ed5d33Seric 	}
1701d6acf3eeSeric 
1702d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1703d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
17046c2c3107Seric # endif /* LOG */
170525a99e2eSeric }
170625a99e2eSeric /*
170751552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
170825a99e2eSeric **
170951552439Seric **	This can be made an arbitrary message separator by changing $l
171051552439Seric **
17119b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
17129b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
17139b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
17149b6c17a6Seric **	this kind of antique garbage????
171525a99e2eSeric **
171625a99e2eSeric **	Parameters:
171751552439Seric **		fp -- the file to output to.
171851552439Seric **		m -- the mailer describing this entry.
171925a99e2eSeric **
172025a99e2eSeric **	Returns:
172151552439Seric **		none
172225a99e2eSeric **
172325a99e2eSeric **	Side Effects:
172451552439Seric **		outputs some text to fp.
172525a99e2eSeric */
172625a99e2eSeric 
1727b31e7f2bSeric putfromline(fp, m, e)
172851552439Seric 	register FILE *fp;
172951552439Seric 	register MAILER *m;
1730b31e7f2bSeric 	ENVELOPE *e;
173125a99e2eSeric {
17322bc47524Seric 	char *template = "\201l\n";
173351552439Seric 	char buf[MAXLINE];
173425a99e2eSeric 
173557fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
173651552439Seric 		return;
173713bbc08cSeric 
17382c7e1b8dSeric # ifdef UGLYUUCP
173957fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
174074b6e67bSeric 	{
1741ea09d6edSeric 		char *bang;
1742ea09d6edSeric 		char xbuf[MAXLINE];
174374b6e67bSeric 
1744ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17456c2c3107Seric 		bang = strchr(buf, '!');
174674b6e67bSeric 		if (bang == NULL)
174708b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
174874b6e67bSeric 		else
1749588cad61Seric 		{
1750ea09d6edSeric 			*bang++ = '\0';
17512bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1752ea09d6edSeric 			template = xbuf;
175374b6e67bSeric 		}
1754588cad61Seric 	}
17556c2c3107Seric # endif /* UGLYUUCP */
1756b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
175777b52738Seric 	putline(buf, fp, m);
1758bc6e2962Seric }
1759bc6e2962Seric /*
176051552439Seric **  PUTBODY -- put the body of a message.
176151552439Seric **
176251552439Seric **	Parameters:
176351552439Seric **		fp -- file to output onto.
176477b52738Seric **		m -- a mailer descriptor to control output format.
17659a6a5f55Seric **		e -- the envelope to put out.
176603c02fdeSeric **		separator -- if non-NULL, a message separator that must
176703c02fdeSeric **			not be permitted in the resulting message.
176851552439Seric **
176951552439Seric **	Returns:
177051552439Seric **		none.
177151552439Seric **
177251552439Seric **	Side Effects:
177351552439Seric **		The message is written onto fp.
177451552439Seric */
177551552439Seric 
177603c02fdeSeric putbody(fp, m, e, separator)
177751552439Seric 	FILE *fp;
1778588cad61Seric 	MAILER *m;
17799a6a5f55Seric 	register ENVELOPE *e;
178003c02fdeSeric 	char *separator;
178151552439Seric {
178277b52738Seric 	char buf[MAXLINE];
178351552439Seric 
178451552439Seric 	/*
178551552439Seric 	**  Output the body of the message
178651552439Seric 	*/
178751552439Seric 
17889a6a5f55Seric 	if (e->e_dfp == NULL)
178951552439Seric 	{
17909a6a5f55Seric 		if (e->e_df != NULL)
17919a6a5f55Seric 		{
17929a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
17939a6a5f55Seric 			if (e->e_dfp == NULL)
17948f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
17958f9146b0Srick 				e->e_df, e->e_to, e->e_from);
17969a6a5f55Seric 		}
17979a6a5f55Seric 		else
179877b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
17999a6a5f55Seric 	}
18009a6a5f55Seric 	if (e->e_dfp != NULL)
18019a6a5f55Seric 	{
18029a6a5f55Seric 		rewind(e->e_dfp);
180377b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
180424fc8aeeSeric 		{
180524fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1806d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
18073462ad9eSeric 				(void) putc('>', fp);
180803c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
180903c02fdeSeric 			{
181003c02fdeSeric 				/* possible separator */
181103c02fdeSeric 				int sl = strlen(separator);
181203c02fdeSeric 
181303c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
181403c02fdeSeric 					(void) putc(' ', fp);
181503c02fdeSeric 			}
181677b52738Seric 			putline(buf, fp, m);
181724fc8aeeSeric 		}
181851552439Seric 
18199a6a5f55Seric 		if (ferror(e->e_dfp))
182051552439Seric 		{
182151552439Seric 			syserr("putbody: read error");
182251552439Seric 			ExitStat = EX_IOERR;
182351552439Seric 		}
182451552439Seric 	}
182551552439Seric 
18260890ba1fSeric 	/* some mailers want extra blank line at end of message */
18270890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
18280890ba1fSeric 		putline("", fp, m);
18290890ba1fSeric 
183051552439Seric 	(void) fflush(fp);
183151552439Seric 	if (ferror(fp) && errno != EPIPE)
183251552439Seric 	{
183351552439Seric 		syserr("putbody: write error");
183451552439Seric 		ExitStat = EX_IOERR;
183551552439Seric 	}
183651552439Seric 	errno = 0;
183725a99e2eSeric }
183825a99e2eSeric /*
183925a99e2eSeric **  MAILFILE -- Send a message to a file.
184025a99e2eSeric **
1841f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1842f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1843f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1844f129ec7dSeric **	sendmail runs as root.
1845f129ec7dSeric **
1846588cad61Seric **	This could be done as a subordinate mailer, except that it
1847588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1848588cad61Seric **	view this as being sufficiently important as to include it
1849588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1850588cad61Seric **	to create another process plus some pipes to save the message.
1851588cad61Seric **
185225a99e2eSeric **	Parameters:
185325a99e2eSeric **		filename -- the name of the file to send to.
18546259796dSeric **		ctladdr -- the controlling address header -- includes
18556259796dSeric **			the userid/groupid to be when sending.
185625a99e2eSeric **
185725a99e2eSeric **	Returns:
185825a99e2eSeric **		The exit code associated with the operation.
185925a99e2eSeric **
186025a99e2eSeric **	Side Effects:
186125a99e2eSeric **		none.
186225a99e2eSeric */
186325a99e2eSeric 
1864b31e7f2bSeric mailfile(filename, ctladdr, e)
186525a99e2eSeric 	char *filename;
18666259796dSeric 	ADDRESS *ctladdr;
1867b31e7f2bSeric 	register ENVELOPE *e;
186825a99e2eSeric {
186925a99e2eSeric 	register FILE *f;
187032d19d43Seric 	register int pid;
187115d084d5Seric 	int mode;
187225a99e2eSeric 
1873671745f3Seric 	if (tTd(11, 1))
1874671745f3Seric 	{
1875671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
1876671745f3Seric 		printaddr(ctladdr, FALSE);
1877671745f3Seric 	}
1878671745f3Seric 
1879*f170942cSeric 	if (e->e_xfp != NULL)
1880*f170942cSeric 		fflush(e->e_xfp);
1881*f170942cSeric 
188232d19d43Seric 	/*
188332d19d43Seric 	**  Fork so we can change permissions here.
188432d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
188532d19d43Seric 	**	the complications of calling subroutines, etc.
188632d19d43Seric 	*/
188732d19d43Seric 
188832d19d43Seric 	DOFORK(fork);
188932d19d43Seric 
189032d19d43Seric 	if (pid < 0)
189132d19d43Seric 		return (EX_OSERR);
189232d19d43Seric 	else if (pid == 0)
189332d19d43Seric 	{
189432d19d43Seric 		/* child -- actually write to file */
1895f129ec7dSeric 		struct stat stb;
1896f129ec7dSeric 
18970984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
18980984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
18990984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
19003462ad9eSeric 		(void) umask(OldUmask);
190195f16dc0Seric 
1902f129ec7dSeric 		if (stat(filename, &stb) < 0)
19033a98e7eaSeric 			stb.st_mode = FileMode;
190415d084d5Seric 		mode = stb.st_mode;
190595f16dc0Seric 
190695f16dc0Seric 		/* limit the errors to those actually caused in the child */
190795f16dc0Seric 		errno = 0;
190895f16dc0Seric 		ExitStat = EX_OK;
190995f16dc0Seric 
1910f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1911f129ec7dSeric 			exit(EX_CANTCREAT);
191203827b5fSeric 		if (ctladdr == NULL)
19138f9146b0Srick 			ctladdr = &e->e_from;
191415d084d5Seric 		else
191515d084d5Seric 		{
191615d084d5Seric 			/* ignore setuid and setgid bits */
191715d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
191815d084d5Seric 		}
191915d084d5Seric 
19208f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
19218f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
19228f9146b0Srick 		{
19238f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
192495f16dc0Seric 			if (e->e_dfp == NULL)
192595f16dc0Seric 			{
19268f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
19278f9146b0Srick 					e->e_df, e->e_to, e->e_from);
19288f9146b0Srick 			}
19298f9146b0Srick 		}
19308f9146b0Srick 
193115d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1932e36b99e2Seric 		{
193395f16dc0Seric 			if (ctladdr->q_uid == 0)
193495f16dc0Seric 			{
1935e36b99e2Seric 				(void) setgid(DefGid);
1936898a126bSbostic 				(void) initgroups(DefUser, DefGid);
193795f16dc0Seric 			}
193895f16dc0Seric 			else
193995f16dc0Seric 			{
19406259796dSeric 				(void) setgid(ctladdr->q_gid);
1941898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1942898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1943898a126bSbostic 					ctladdr->q_gid);
1944898a126bSbostic 			}
1945e36b99e2Seric 		}
194615d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1947e36b99e2Seric 		{
1948e36b99e2Seric 			if (ctladdr->q_uid == 0)
1949e36b99e2Seric 				(void) setuid(DefUid);
1950e36b99e2Seric 			else
19516259796dSeric 				(void) setuid(ctladdr->q_uid);
1952e36b99e2Seric 		}
195395f16dc0Seric 		FileName = filename;
195495f16dc0Seric 		LineNumber = 0;
19553a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
195625a99e2eSeric 		if (f == NULL)
195795f16dc0Seric 		{
195808b25121Seric 			message("554 cannot open");
195932d19d43Seric 			exit(EX_CANTCREAT);
196095f16dc0Seric 		}
196125a99e2eSeric 
19620331ce05Seric 		putfromline(f, FileMailer, e);
19630331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
19640331ce05Seric 		putline("\n", f, FileMailer);
196503c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
19660331ce05Seric 		putline("\n", f, FileMailer);
196795f16dc0Seric 		if (ferror(f))
196895f16dc0Seric 		{
196908b25121Seric 			message("451 I/O error");
197095f16dc0Seric 			setstat(EX_IOERR);
197195f16dc0Seric 		}
1972ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
197332d19d43Seric 		(void) fflush(stdout);
1974e36b99e2Seric 
197527628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1976c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
197795f16dc0Seric 		exit(ExitStat);
197813bbc08cSeric 		/*NOTREACHED*/
197932d19d43Seric 	}
198032d19d43Seric 	else
198132d19d43Seric 	{
198232d19d43Seric 		/* parent -- wait for exit status */
1983588cad61Seric 		int st;
198432d19d43Seric 
1985588cad61Seric 		st = waitfor(pid);
1986588cad61Seric 		if ((st & 0377) != 0)
1987588cad61Seric 			return (EX_UNAVAILABLE);
1988588cad61Seric 		else
1989588cad61Seric 			return ((st >> 8) & 0377);
19908f9146b0Srick 		/*NOTREACHED*/
199132d19d43Seric 	}
199225a99e2eSeric }
1993ea4dc939Seric /*
1994e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1995e103b48fSeric **
1996e103b48fSeric **	The signature describes how we are going to send this -- it
1997e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1998e103b48fSeric **	an ordered list of MX hosts.
1999e103b48fSeric **
2000e103b48fSeric **	Parameters:
2001e103b48fSeric **		m -- the mailer describing this host.
2002e103b48fSeric **		host -- the host name.
2003e103b48fSeric **		e -- the current envelope.
2004e103b48fSeric **
2005e103b48fSeric **	Returns:
2006e103b48fSeric **		The signature for this host.
2007e103b48fSeric **
2008e103b48fSeric **	Side Effects:
2009e103b48fSeric **		Can tweak the symbol table.
2010e103b48fSeric */
2011e103b48fSeric 
2012e103b48fSeric char *
2013e103b48fSeric hostsignature(m, host, e)
2014e103b48fSeric 	register MAILER *m;
2015e103b48fSeric 	char *host;
2016e103b48fSeric 	ENVELOPE *e;
2017e103b48fSeric {
2018e103b48fSeric 	register char *p;
2019e103b48fSeric 	register STAB *s;
2020e103b48fSeric 	int i;
2021e103b48fSeric 	int len;
2022e103b48fSeric #ifdef NAMED_BIND
2023e103b48fSeric 	int nmx;
2024e103b48fSeric 	auto int rcode;
2025bafdc4e5Seric 	char *hp;
2026bafdc4e5Seric 	char *endp;
2027516782b4Seric 	int oldoptions;
2028e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2029e103b48fSeric #endif
2030e103b48fSeric 
2031e103b48fSeric 	/*
2032e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2033e103b48fSeric 	*/
2034e103b48fSeric 
2035e103b48fSeric 	p = m->m_mailer;
2036e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2037e103b48fSeric 	{
2038e103b48fSeric 		/* just an ordinary mailer */
2039e103b48fSeric 		return host;
2040e103b48fSeric 	}
2041e103b48fSeric 
2042e103b48fSeric 	/*
2043e103b48fSeric 	**  If it is a numeric address, just return it.
2044e103b48fSeric 	*/
2045e103b48fSeric 
2046e103b48fSeric 	if (host[0] == '[')
2047e103b48fSeric 		return host;
2048e103b48fSeric 
2049e103b48fSeric 	/*
2050e103b48fSeric 	**  Look it up in the symbol table.
2051e103b48fSeric 	*/
2052e103b48fSeric 
2053e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2054e103b48fSeric 	if (s->s_hostsig != NULL)
2055e103b48fSeric 		return s->s_hostsig;
2056e103b48fSeric 
2057e103b48fSeric 	/*
2058e103b48fSeric 	**  Not already there -- create a signature.
2059e103b48fSeric 	*/
2060e103b48fSeric 
2061e103b48fSeric #ifdef NAMED_BIND
2062516782b4Seric 	if (ConfigLevel < 2)
2063516782b4Seric 	{
2064516782b4Seric 		oldoptions = _res.options;
2065516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2066516782b4Seric 	}
2067516782b4Seric 
2068bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2069bafdc4e5Seric 	{
2070bafdc4e5Seric 		endp = strchr(hp, ':');
2071bafdc4e5Seric 		if (endp != NULL)
2072bafdc4e5Seric 			*endp = '\0';
2073bafdc4e5Seric 
20747bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
20757d55540cSeric 
2076e103b48fSeric 		if (nmx <= 0)
2077e103b48fSeric 		{
2078e103b48fSeric 			register MCI *mci;
2079e103b48fSeric 			extern int errno;
2080e103b48fSeric 
2081e103b48fSeric 			/* update the connection info for this host */
2082bafdc4e5Seric 			mci = mci_get(hp, m);
2083e103b48fSeric 			mci->mci_exitstat = rcode;
2084e103b48fSeric 			mci->mci_errno = errno;
2085*f170942cSeric #ifdef NAMED_BIND
2086*f170942cSeric 			mci->mci_herrno = h_errno;
2087*f170942cSeric #endif
2088e103b48fSeric 
2089e103b48fSeric 			/* and return the original host name as the signature */
2090bafdc4e5Seric 			nmx = 1;
2091bafdc4e5Seric 			mxhosts[0] = hp;
2092e103b48fSeric 		}
2093e103b48fSeric 
2094e103b48fSeric 		len = 0;
2095e103b48fSeric 		for (i = 0; i < nmx; i++)
2096e103b48fSeric 		{
2097e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2098e103b48fSeric 		}
2099bafdc4e5Seric 		if (s->s_hostsig != NULL)
2100bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2101bafdc4e5Seric 		p = xalloc(len);
2102bafdc4e5Seric 		if (s->s_hostsig != NULL)
2103bafdc4e5Seric 		{
2104bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2105bafdc4e5Seric 			free(s->s_hostsig);
2106bafdc4e5Seric 			s->s_hostsig = p;
2107bafdc4e5Seric 			p += strlen(p);
2108bafdc4e5Seric 			*p++ = ':';
2109bafdc4e5Seric 		}
2110bafdc4e5Seric 		else
2111bafdc4e5Seric 			s->s_hostsig = p;
2112e103b48fSeric 		for (i = 0; i < nmx; i++)
2113e103b48fSeric 		{
2114e103b48fSeric 			if (i != 0)
2115e103b48fSeric 				*p++ = ':';
2116e103b48fSeric 			strcpy(p, mxhosts[i]);
2117e103b48fSeric 			p += strlen(p);
2118e103b48fSeric 		}
2119bafdc4e5Seric 		if (endp != NULL)
2120bafdc4e5Seric 			*endp++ = ':';
2121bafdc4e5Seric 	}
2122e103b48fSeric 	makelower(s->s_hostsig);
2123516782b4Seric 	if (ConfigLevel < 2)
2124516782b4Seric 		_res.options = oldoptions;
2125e103b48fSeric #else
2126e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2127e103b48fSeric 	s->s_hostsig = host;
2128e103b48fSeric #endif
2129e103b48fSeric 	if (tTd(17, 1))
2130e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2131e103b48fSeric 	return s->s_hostsig;
2132e103b48fSeric }
2133