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*7febfd66Seric static char sccsid[] = "@(#)deliver.c	8.21 (Berkeley) 09/04/93";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14f28da541Smiriam #include <netdb.h>
15911693bfSbostic #include <errno.h>
16bf9bc890Seric #include <sys/wait.h>
17134746fbSeric #ifdef NAMED_BIND
18912a731aSbostic #include <arpa/nameser.h>
19912a731aSbostic #include <resolv.h>
20f170942cSeric 
21f170942cSeric extern int	h_errno;
22134746fbSeric #endif
2325a99e2eSeric 
24bf9bc890Seric #ifndef WEXITSTATUS
25bf9bc890Seric # define WEXITSTATUS(st)	(((st) >> 8) & 0377)
26bf9bc890Seric #endif
27bf9bc890Seric 
2825a99e2eSeric /*
299c9e68d9Seric **  SENDALL -- actually send all the messages.
309c9e68d9Seric **
319c9e68d9Seric **	Parameters:
329c9e68d9Seric **		e -- the envelope to send.
339c9e68d9Seric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
349c9e68d9Seric **			the current e->e_sendmode.
359c9e68d9Seric **
369c9e68d9Seric **	Returns:
379c9e68d9Seric **		none.
389c9e68d9Seric **
399c9e68d9Seric **	Side Effects:
409c9e68d9Seric **		Scans the send lists and sends everything it finds.
419c9e68d9Seric **		Delivers any appropriate error messages.
429c9e68d9Seric **		If we are running in a non-interactive mode, takes the
439c9e68d9Seric **			appropriate action.
449c9e68d9Seric */
459c9e68d9Seric 
469c9e68d9Seric sendall(e, mode)
479c9e68d9Seric 	ENVELOPE *e;
489c9e68d9Seric 	char mode;
499c9e68d9Seric {
509c9e68d9Seric 	register ADDRESS *q;
519c9e68d9Seric 	char *owner;
529c9e68d9Seric 	int otherowners;
539c9e68d9Seric 	register ENVELOPE *ee;
549c9e68d9Seric 	ENVELOPE *splitenv = NULL;
556103d9b4Seric 	bool announcequeueup;
569c9e68d9Seric 
577880f3e4Seric 	/*
587880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
597880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
607880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
617880f3e4Seric 	**  addresses to be sent.
627880f3e4Seric 	*/
637880f3e4Seric 
647880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
65896b16f6Seric 	{
66896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
67896b16f6Seric 		return;
68896b16f6Seric 	}
69896b16f6Seric 
709c9e68d9Seric 	/* determine actual delivery mode */
719c9e68d9Seric 	if (mode == SM_DEFAULT)
729c9e68d9Seric 	{
739c9e68d9Seric 		mode = e->e_sendmode;
749c9e68d9Seric 		if (mode != SM_VERIFY &&
759c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
769c9e68d9Seric 			mode = SM_QUEUE;
776103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
789c9e68d9Seric 	}
796103d9b4Seric 	else
806103d9b4Seric 		announcequeueup = FALSE;
819c9e68d9Seric 
829c9e68d9Seric 	if (tTd(13, 1))
839c9e68d9Seric 	{
840e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
850e484fe5Seric 			mode, e->e_id);
869c9e68d9Seric 		printaddr(&e->e_from, FALSE);
879c9e68d9Seric 		printf("sendqueue:\n");
889c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
899c9e68d9Seric 	}
909c9e68d9Seric 
919c9e68d9Seric 	/*
929c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
939c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
949c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
959c9e68d9Seric 	*/
969c9e68d9Seric 
979c9e68d9Seric 	CurEnv = e;
989c9e68d9Seric 
999c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
1009c9e68d9Seric 	{
1019c9e68d9Seric 		errno = 0;
1029c9e68d9Seric 		syserr("554 too many hops %d (%d max): from %s, to %s",
1039c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
1049c9e68d9Seric 			e->e_sendqueue->q_paddr);
1059c9e68d9Seric 		return;
1069c9e68d9Seric 	}
1079c9e68d9Seric 
108b8004690Seric 	/*
109b8004690Seric 	**  Do sender deletion.
110b8004690Seric 	**
111b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
112b8004690Seric 	**	This can happen if the name server is hosed when you
113b8004690Seric 	**	are trying to send mail.  The result is that the sender
114b8004690Seric 	**	is instantiated in the queue as a recipient.
115b8004690Seric 	*/
116b8004690Seric 
117e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
118e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1199c9e68d9Seric 	{
1209c9e68d9Seric 		if (tTd(13, 5))
1219c9e68d9Seric 		{
1229c9e68d9Seric 			printf("sendall: QDONTSEND ");
1239c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1249c9e68d9Seric 		}
1259c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1269c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1279c9e68d9Seric 	}
1289c9e68d9Seric 
129ce5531bdSeric 	/*
130ce5531bdSeric 	**  Handle alias owners.
131ce5531bdSeric 	**
132ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
133ce5531bdSeric 	**	We discard owners that are the same as the return path.
134ce5531bdSeric 	*/
135ce5531bdSeric 
136ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
137ce5531bdSeric 	{
138ce5531bdSeric 		register struct address *a;
139ce5531bdSeric 
140ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
141ce5531bdSeric 			continue;
142ce5531bdSeric 		if (a != NULL)
143ce5531bdSeric 			q->q_owner = a->q_owner;
144ce5531bdSeric 
145ce5531bdSeric 		if (q->q_owner != NULL &&
146ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
147ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
148ce5531bdSeric 			q->q_owner = NULL;
149ce5531bdSeric 	}
150ce5531bdSeric 
151ce5531bdSeric 	owner = "";
152ce5531bdSeric 	otherowners = 1;
153ce5531bdSeric 	while (owner != NULL && otherowners > 0)
154ce5531bdSeric 	{
155ce5531bdSeric 		owner = NULL;
156ce5531bdSeric 		otherowners = 0;
157ce5531bdSeric 
158ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
159ce5531bdSeric 		{
160ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
161ce5531bdSeric 				continue;
162ce5531bdSeric 
163ce5531bdSeric 			if (q->q_owner != NULL)
164ce5531bdSeric 			{
165ce5531bdSeric 				if (owner == NULL)
166ce5531bdSeric 					owner = q->q_owner;
167ce5531bdSeric 				else if (owner != q->q_owner)
168ce5531bdSeric 				{
169ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
170ce5531bdSeric 					{
171ce5531bdSeric 						/* make future comparisons cheap */
172ce5531bdSeric 						q->q_owner = owner;
173ce5531bdSeric 					}
174ce5531bdSeric 					else
175ce5531bdSeric 					{
176ce5531bdSeric 						otherowners++;
177ce5531bdSeric 					}
178ce5531bdSeric 					owner = q->q_owner;
179ce5531bdSeric 				}
180ce5531bdSeric 			}
181ce5531bdSeric 			else
182ce5531bdSeric 			{
183ce5531bdSeric 				otherowners++;
184ce5531bdSeric 			}
185ce5531bdSeric 		}
186ce5531bdSeric 
187ce5531bdSeric 		if (owner != NULL && otherowners > 0)
188ce5531bdSeric 		{
189ce5531bdSeric 			extern HDR *copyheader();
190ce5531bdSeric 			extern ADDRESS *copyqueue();
191ce5531bdSeric 
192ce5531bdSeric 			/*
193ce5531bdSeric 			**  Split this envelope into two.
194ce5531bdSeric 			*/
195ce5531bdSeric 
196ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
197ce5531bdSeric 			*ee = *e;
198ce5531bdSeric 			ee->e_id = NULL;
199ce5531bdSeric 			(void) queuename(ee, '\0');
200ce5531bdSeric 
201ce5531bdSeric 			if (tTd(13, 1))
202ce5531bdSeric 				printf("sendall: split %s into %s\n",
203ce5531bdSeric 					e->e_id, ee->e_id);
204ce5531bdSeric 
205ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
206ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
207ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
208ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
209ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
210ce5531bdSeric 			if (tTd(13, 5))
211ce5531bdSeric 			{
212ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
213ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
214ce5531bdSeric 			}
215ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
216ce5531bdSeric 			ee->e_dfp = NULL;
217ce5531bdSeric 			ee->e_xfp = NULL;
218ce5531bdSeric 			ee->e_lockfp = NULL;
219ce5531bdSeric 			ee->e_df = NULL;
220ce5531bdSeric 			ee->e_errormode = EM_MAIL;
221ce5531bdSeric 			ee->e_sibling = splitenv;
222ce5531bdSeric 			splitenv = ee;
223ce5531bdSeric 
224ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
225ce5531bdSeric 				if (q->q_owner == owner)
226ce5531bdSeric 					q->q_flags |= QDONTSEND;
227ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
228ce5531bdSeric 				if (q->q_owner != owner)
229ce5531bdSeric 					q->q_flags |= QDONTSEND;
230ce5531bdSeric 
231ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
232ce5531bdSeric 			{
233ce5531bdSeric 				ee->e_dfp = NULL;
234da662164Seric 				ee->e_df = queuename(ee, 'd');
235da662164Seric 				ee->e_df = newstr(ee->e_df);
236ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
237ce5531bdSeric 				{
238ce5531bdSeric 					syserr("sendall: link(%s, %s)",
239ce5531bdSeric 						e->e_df, ee->e_df);
240ce5531bdSeric 				}
241ce5531bdSeric 			}
242ce5531bdSeric 
243ce5531bdSeric 			if (mode != SM_VERIFY)
244ce5531bdSeric 				openxscript(ee);
245ce5531bdSeric #ifdef LOG
246ce5531bdSeric 			if (LogLevel > 4)
247ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
248ce5531bdSeric 					ee->e_id, e->e_id);
249ce5531bdSeric #endif
250ce5531bdSeric 		}
251ce5531bdSeric 	}
252ce5531bdSeric 
253ce5531bdSeric 	if (owner != NULL)
254ce5531bdSeric 	{
255ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
256ce5531bdSeric 		if (tTd(13, 5))
257ce5531bdSeric 		{
258ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
259ce5531bdSeric 			printaddr(&e->e_from, FALSE);
260ce5531bdSeric 		}
261ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
262ce5531bdSeric 		e->e_errormode = EM_MAIL;
263ce5531bdSeric 	}
264ce5531bdSeric 
265c1ac89b1Seric # ifdef QUEUE
266c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
267c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
268c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
269c1ac89b1Seric 	{
270c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2716103d9b4Seric 		queueup(e, TRUE, announcequeueup);
272ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2736103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
274c1ac89b1Seric 	}
275c1ac89b1Seric #endif /* QUEUE */
276c1ac89b1Seric 
277ce5531bdSeric 	if (splitenv != NULL)
278ce5531bdSeric 	{
279ce5531bdSeric 		if (tTd(13, 1))
280ce5531bdSeric 		{
281ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
282ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
283ce5531bdSeric 		}
284ce5531bdSeric 
285ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
286ce5531bdSeric 		{
287ce5531bdSeric 			CurEnv = ee;
288ce5531bdSeric 			sendenvelope(ee, mode);
289ce5531bdSeric 		}
290ce5531bdSeric 
291ce5531bdSeric 		CurEnv = e;
292ce5531bdSeric 	}
293ce5531bdSeric 	sendenvelope(e, mode);
294ce5531bdSeric 
295ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
296ce5531bdSeric 		dropenvelope(splitenv);
297ce5531bdSeric }
298ce5531bdSeric 
299ce5531bdSeric sendenvelope(e, mode)
300ce5531bdSeric 	register ENVELOPE *e;
301ce5531bdSeric 	char mode;
302ce5531bdSeric {
303ce5531bdSeric 	bool oldverbose;
304ce5531bdSeric 	int pid;
305ce5531bdSeric 	register ADDRESS *q;
3066b2765c6Seric 	char *qf;
3076b2765c6Seric 	char *id;
308ce5531bdSeric 
3097880f3e4Seric 	/*
3107880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3117880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3127880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3137880f3e4Seric 	**  addresses to be sent.
3147880f3e4Seric 	*/
3157880f3e4Seric 
3167880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
3177880f3e4Seric 	{
3187880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3197880f3e4Seric 		return;
3207880f3e4Seric 	}
3217880f3e4Seric 
322ce5531bdSeric 	oldverbose = Verbose;
323c1ac89b1Seric 	switch (mode)
324c1ac89b1Seric 	{
325c1ac89b1Seric 	  case SM_VERIFY:
326c1ac89b1Seric 		Verbose = TRUE;
327c1ac89b1Seric 		break;
328c1ac89b1Seric 
329c1ac89b1Seric 	  case SM_QUEUE:
330c1ac89b1Seric   queueonly:
331c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
332c1ac89b1Seric 		return;
333c1ac89b1Seric 
334c1ac89b1Seric 	  case SM_FORK:
335c1ac89b1Seric 		if (e->e_xfp != NULL)
336c1ac89b1Seric 			(void) fflush(e->e_xfp);
337c1ac89b1Seric 
3382b9178d3Seric # ifndef HASFLOCK
339c1ac89b1Seric 		/*
3406b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3416b2765c6Seric 		**  the lock is owned by a process, not by an open file
3426b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3436b2765c6Seric 		**  then restart from scratch in the child.
344c1ac89b1Seric 		*/
345c1ac89b1Seric 
3466b2765c6Seric 		/* save id for future use */
3476b2765c6Seric 		id = e->e_id;
3486b2765c6Seric 
3496b2765c6Seric 		/* now drop the envelope in the parent */
3506b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3516b2765c6Seric 		dropenvelope(e);
3526b2765c6Seric 
3536b2765c6Seric 		/* and reacquire in the child */
3546b2765c6Seric 		(void) dowork(id, TRUE, FALSE, e);
3556b2765c6Seric 
3566b2765c6Seric 		return;
3576b2765c6Seric 
3586b2765c6Seric # else /* HASFLOCK */
359c1ac89b1Seric 
360c1ac89b1Seric 		pid = fork();
361c1ac89b1Seric 		if (pid < 0)
362c1ac89b1Seric 		{
363c1ac89b1Seric 			goto queueonly;
364c1ac89b1Seric 		}
365c1ac89b1Seric 		else if (pid > 0)
366c1ac89b1Seric 		{
3670e484fe5Seric 			/* be sure we leave the temp files to our child */
3680e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3690e484fe5Seric 			if (e->e_lockfp != NULL)
3700e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3710e484fe5Seric 			e->e_lockfp = NULL;
3720e484fe5Seric 
3730e484fe5Seric 			/* close any random open files in the envelope */
3740e484fe5Seric 			closexscript(e);
3750e484fe5Seric 			if (e->e_dfp != NULL)
3760e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3770e484fe5Seric 			e->e_dfp = NULL;
3780e484fe5Seric 			e->e_id = e->e_df = NULL;
379c1ac89b1Seric 			return;
380c1ac89b1Seric 		}
381c1ac89b1Seric 
382c1ac89b1Seric 		/* double fork to avoid zombies */
383c1ac89b1Seric 		if (fork() > 0)
384c1ac89b1Seric 			exit(EX_OK);
385c1ac89b1Seric 
386c1ac89b1Seric 		/* be sure we are immune from the terminal */
3877880f3e4Seric 		disconnect(1, e);
388c1ac89b1Seric 
389c1ac89b1Seric 		/*
390c1ac89b1Seric 		**  Close any cached connections.
391c1ac89b1Seric 		**
392c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
393c1ac89b1Seric 		**	still knows about the connection.
394c1ac89b1Seric 		**
395c1ac89b1Seric 		**	This should only happen when delivering an error
396c1ac89b1Seric 		**	message.
397c1ac89b1Seric 		*/
398c1ac89b1Seric 
399c1ac89b1Seric 		mci_flush(FALSE, NULL);
400c1ac89b1Seric 
4016b2765c6Seric # endif /* HASFLOCK */
4026b2765c6Seric 
403c1ac89b1Seric 		break;
404c1ac89b1Seric 	}
405c1ac89b1Seric 
406c1ac89b1Seric 	/*
4079c9e68d9Seric 	**  Run through the list and send everything.
4085288e21aSeric 	**
4095288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4105288e21aSeric 	**	result in returned mail.
4119c9e68d9Seric 	*/
4129c9e68d9Seric 
4139c9e68d9Seric 	e->e_nsent = 0;
4145288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
4159c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4169c9e68d9Seric 	{
4179c9e68d9Seric 		if (mode == SM_VERIFY)
4189c9e68d9Seric 		{
4199c9e68d9Seric 			e->e_to = q->q_paddr;
4209c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4218dfca105Seric 			{
4228dfca105Seric 				message("deliverable: mailer %s, host %s, user %s",
4238dfca105Seric 					q->q_mailer->m_name,
4248dfca105Seric 					q->q_host,
4258dfca105Seric 					q->q_user);
4268dfca105Seric 			}
4279c9e68d9Seric 		}
4289c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4299c9e68d9Seric 		{
4309c9e68d9Seric # ifdef QUEUE
4319c9e68d9Seric 			/*
4329c9e68d9Seric 			**  Checkpoint the send list every few addresses
4339c9e68d9Seric 			*/
4349c9e68d9Seric 
4359c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4369c9e68d9Seric 			{
4379c9e68d9Seric 				queueup(e, TRUE, FALSE);
4389c9e68d9Seric 				e->e_nsent = 0;
4399c9e68d9Seric 			}
4409c9e68d9Seric # endif /* QUEUE */
4419c9e68d9Seric 			(void) deliver(e, q);
4429c9e68d9Seric 		}
4439c9e68d9Seric 	}
4449c9e68d9Seric 	Verbose = oldverbose;
4459c9e68d9Seric 
4469c9e68d9Seric 	if (mode == SM_FORK)
4479c9e68d9Seric 		finis();
4489c9e68d9Seric }
4499c9e68d9Seric /*
4509c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4519c9e68d9Seric **
4529c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4539c9e68d9Seric **	two processes on the same stack!!!
4549c9e68d9Seric **
4559c9e68d9Seric **	Parameters:
4569c9e68d9Seric **		none.
4579c9e68d9Seric **
4589c9e68d9Seric **	Returns:
4599c9e68d9Seric **		From a macro???  You've got to be kidding!
4609c9e68d9Seric **
4619c9e68d9Seric **	Side Effects:
4629c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4639c9e68d9Seric **			pid of child in parent, zero in child.
4649c9e68d9Seric **			-1 on unrecoverable error.
4659c9e68d9Seric **
4669c9e68d9Seric **	Notes:
4679c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4689c9e68d9Seric **		vfork for you.....
4699c9e68d9Seric */
4709c9e68d9Seric 
4719c9e68d9Seric # define NFORKTRIES	5
4729c9e68d9Seric 
4739c9e68d9Seric # ifndef FORK
4749c9e68d9Seric # define FORK	fork
4759c9e68d9Seric # endif
4769c9e68d9Seric 
4779c9e68d9Seric # define DOFORK(fORKfN) \
4789c9e68d9Seric {\
4799c9e68d9Seric 	register int i;\
4809c9e68d9Seric \
4819c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4829c9e68d9Seric 	{\
4839c9e68d9Seric 		pid = fORKfN();\
4849c9e68d9Seric 		if (pid >= 0)\
4859c9e68d9Seric 			break;\
4869c9e68d9Seric 		if (i > 0)\
4879c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
4889c9e68d9Seric 	}\
4899c9e68d9Seric }
4909c9e68d9Seric /*
4919c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
4929c9e68d9Seric **
4939c9e68d9Seric **	Parameters:
4949c9e68d9Seric **		none.
4959c9e68d9Seric **
4969c9e68d9Seric **	Returns:
4979c9e68d9Seric **		pid of child in parent.
4989c9e68d9Seric **		zero in child.
4999c9e68d9Seric **		-1 on error.
5009c9e68d9Seric **
5019c9e68d9Seric **	Side Effects:
5029c9e68d9Seric **		returns twice, once in parent and once in child.
5039c9e68d9Seric */
5049c9e68d9Seric 
5059c9e68d9Seric dofork()
5069c9e68d9Seric {
5079c9e68d9Seric 	register int pid;
5089c9e68d9Seric 
5099c9e68d9Seric 	DOFORK(fork);
5109c9e68d9Seric 	return (pid);
5119c9e68d9Seric }
5129c9e68d9Seric /*
51313bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
51413bbc08cSeric **
51513bbc08cSeric **	This routine delivers to everyone on the same host as the
51613bbc08cSeric **	user on the head of the list.  It is clever about mailers
51713bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
51813bbc08cSeric **	that it will deliver to all these addresses however -- so
51913bbc08cSeric **	deliver should be called once for each address on the
52013bbc08cSeric **	list.
52125a99e2eSeric **
52225a99e2eSeric **	Parameters:
523588cad61Seric **		e -- the envelope to deliver.
524c77d1c25Seric **		firstto -- head of the address list to deliver to.
52525a99e2eSeric **
52625a99e2eSeric **	Returns:
52725a99e2eSeric **		zero -- successfully delivered.
52825a99e2eSeric **		else -- some failure, see ExitStat for more info.
52925a99e2eSeric **
53025a99e2eSeric **	Side Effects:
53125a99e2eSeric **		The standard input is passed off to someone.
53225a99e2eSeric */
53325a99e2eSeric 
534588cad61Seric deliver(e, firstto)
535588cad61Seric 	register ENVELOPE *e;
536c77d1c25Seric 	ADDRESS *firstto;
53725a99e2eSeric {
53878442df3Seric 	char *host;			/* host being sent to */
53978442df3Seric 	char *user;			/* user being sent to */
54025a99e2eSeric 	char **pvp;
5415dfc646bSeric 	register char **mvp;
54225a99e2eSeric 	register char *p;
543588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5446259796dSeric 	ADDRESS *ctladdr;
545b31e7f2bSeric 	register MCI *mci;
546c77d1c25Seric 	register ADDRESS *to = firstto;
547c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
548772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
549911693bfSbostic 	int rcode;			/* response code */
550e103b48fSeric 	char *firstsig;			/* signature of firstto */
5519c9e68d9Seric 	int pid;
5529c9e68d9Seric 	char *curhost;
5539c9e68d9Seric 	int mpvect[2];
5549c9e68d9Seric 	int rpvect[2];
555ee6bf8dfSeric 	char *pv[MAXPV+1];
556579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
557ee6bf8dfSeric 	char buf[MAXNAME];
558c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
559fabb3bd4Seric 	extern int checkcompat();
5609c9e68d9Seric 	extern FILE *fdopen();
56125a99e2eSeric 
56235490626Seric 	errno = 0;
563ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5645dfc646bSeric 		return (0);
56525a99e2eSeric 
566134746fbSeric #ifdef NAMED_BIND
567912a731aSbostic 	/* unless interactive, try twice, over a minute */
568912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
569912a731aSbostic 		_res.retrans = 30;
570912a731aSbostic 		_res.retry = 2;
571912a731aSbostic 	}
572d4bd8f0eSbostic #endif
573912a731aSbostic 
57451552439Seric 	m = to->q_mailer;
57551552439Seric 	host = to->q_host;
576c9be6216Seric 	CurEnv = e;			/* just in case */
5774384d521Seric 	e->e_statmsg = NULL;
57851552439Seric 
5796ef48975Seric 	if (tTd(10, 1))
5805dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
58151552439Seric 			m->m_mno, host, to->q_user);
582f3dbc832Seric 
583f3dbc832Seric 	/*
584f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
585f3dbc832Seric 	**  connections now, just mark these addresses and return.
586f3dbc832Seric 	**	This is useful if we want to batch connections to
587f3dbc832Seric 	**	reduce load.  This will cause the messages to be
588f3dbc832Seric 	**	queued up, and a daemon will come along to send the
589f3dbc832Seric 	**	messages later.
590f3dbc832Seric 	**		This should be on a per-mailer basis.
591f3dbc832Seric 	*/
592f3dbc832Seric 
59319c47125Seric 	if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) &&
59419c47125Seric 	    bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
595f3dbc832Seric 	{
596f3dbc832Seric 		for (; to != NULL; to = to->q_next)
597f4560e80Seric 		{
598ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
599c6e18ac5Seric 			    to->q_mailer != m)
600f4560e80Seric 				continue;
601268a25e1Seric 			to->q_flags |= QQUEUEUP;
602588cad61Seric 			e->e_to = to->q_paddr;
60308b25121Seric 			message("queued");
6042f624c86Seric 			if (LogLevel > 8)
60581161401Seric 				logdelivery(m, NULL, "queued", e);
606f4560e80Seric 		}
607588cad61Seric 		e->e_to = NULL;
608f3dbc832Seric 		return (0);
609f3dbc832Seric 	}
610f3dbc832Seric 
61125a99e2eSeric 	/*
6125dfc646bSeric 	**  Do initial argv setup.
6135dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6145dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6155dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6165dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6175dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6183bea8136Seric 	**		The from address rewrite is expected to make
6193bea8136Seric 	**		the address relative to the other end.
6205dfc646bSeric 	*/
6215dfc646bSeric 
62278442df3Seric 	/* rewrite from address, using rewriting rules */
623efe54562Seric 	rcode = EX_OK;
624efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
625efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
626efe54562Seric 					   &rcode, e));
627ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
628588cad61Seric 	define('h', host, e);			/* to host */
6295dfc646bSeric 	Errors = 0;
6305dfc646bSeric 	pvp = pv;
6315dfc646bSeric 	*pvp++ = m->m_argv[0];
6325dfc646bSeric 
6335dfc646bSeric 	/* insert -f or -r flag as appropriate */
63457fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6355dfc646bSeric 	{
63657fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6375dfc646bSeric 			*pvp++ = "-f";
6385dfc646bSeric 		else
6395dfc646bSeric 			*pvp++ = "-r";
640c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6415dfc646bSeric 	}
6425dfc646bSeric 
6435dfc646bSeric 	/*
6445dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6455dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6465dfc646bSeric 	**  be one of these, and there are only a few more slots
6475dfc646bSeric 	**  in the pv after it.
6485dfc646bSeric 	*/
6495dfc646bSeric 
6505dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6515dfc646bSeric 	{
6522bc47524Seric 		/* can't use strchr here because of sign extension problems */
6532bc47524Seric 		while (*p != '\0')
6542bc47524Seric 		{
6552bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6562bc47524Seric 			{
6572bc47524Seric 				if (*p == 'u')
6585dfc646bSeric 					break;
6592bc47524Seric 			}
6602bc47524Seric 		}
6612bc47524Seric 
6622bc47524Seric 		if (*p != '\0')
6635dfc646bSeric 			break;
6645dfc646bSeric 
6655dfc646bSeric 		/* this entry is safe -- go ahead and process it */
666588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6675dfc646bSeric 		*pvp++ = newstr(buf);
6685dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6695dfc646bSeric 		{
67008b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6715dfc646bSeric 			return (-1);
6725dfc646bSeric 		}
6735dfc646bSeric 	}
674c579ef51Seric 
67533db8731Seric 	/*
67633db8731Seric 	**  If we have no substitution for the user name in the argument
67733db8731Seric 	**  list, we know that we must supply the names otherwise -- and
67833db8731Seric 	**  SMTP is the answer!!
67933db8731Seric 	*/
68033db8731Seric 
6815dfc646bSeric 	if (*mvp == NULL)
682c579ef51Seric 	{
683c579ef51Seric 		/* running SMTP */
6842c7e1b8dSeric # ifdef SMTP
685c579ef51Seric 		clever = TRUE;
686c579ef51Seric 		*pvp = NULL;
6876c2c3107Seric # else /* SMTP */
68833db8731Seric 		/* oops!  we don't implement SMTP */
68908b25121Seric 		syserr("554 SMTP style mailer");
6902c7e1b8dSeric 		return (EX_SOFTWARE);
6916c2c3107Seric # endif /* SMTP */
692c579ef51Seric 	}
6935dfc646bSeric 
6945dfc646bSeric 	/*
6955dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
6965dfc646bSeric 	**  run through our address list and append all the addresses
6975dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
6985dfc646bSeric 	**  always send another copy later.
6995dfc646bSeric 	*/
7005dfc646bSeric 
7015dfc646bSeric 	tobuf[0] = '\0';
702588cad61Seric 	e->e_to = tobuf;
7036259796dSeric 	ctladdr = NULL;
704e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7055dfc646bSeric 	for (; to != NULL; to = to->q_next)
7065dfc646bSeric 	{
7075dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
70857fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7095dfc646bSeric 			break;
7105dfc646bSeric 
7115dfc646bSeric 		/* if already sent or not for this host, don't send */
712ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
713e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
714e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7155dfc646bSeric 			continue;
7166259796dSeric 
7174b22ea87Seric 		/* avoid overflowing tobuf */
718aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7194b22ea87Seric 			break;
7204b22ea87Seric 
7216ef48975Seric 		if (tTd(10, 1))
722772e6e50Seric 		{
723772e6e50Seric 			printf("\nsend to ");
724772e6e50Seric 			printaddr(to, FALSE);
725772e6e50Seric 		}
726772e6e50Seric 
7276259796dSeric 		/* compute effective uid/gid when sending */
7287da1035fSeric 		if (to->q_mailer == ProgMailer)
7296259796dSeric 			ctladdr = getctladdr(to);
7306259796dSeric 
7315dfc646bSeric 		user = to->q_user;
732588cad61Seric 		e->e_to = to->q_paddr;
73375f1ade9Seric 		if (tTd(10, 5))
73475f1ade9Seric 		{
73575f1ade9Seric 			printf("deliver: QDONTSEND ");
73675f1ade9Seric 			printaddr(to, FALSE);
73775f1ade9Seric 		}
738ee4b0922Seric 		to->q_flags |= QDONTSEND;
7395dfc646bSeric 
7405dfc646bSeric 		/*
7415dfc646bSeric 		**  Check to see that these people are allowed to
7425dfc646bSeric 		**  talk to each other.
7432a6e0786Seric 		*/
7442a6e0786Seric 
74569582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
74669582d2fSeric 		{
74769582d2fSeric 			NoReturn = TRUE;
74808b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
74981161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
75069582d2fSeric 			continue;
75169582d2fSeric 		}
752fabb3bd4Seric 		rcode = checkcompat(to, e);
7531793c9c5Seric 		if (rcode != EX_OK)
7545dfc646bSeric 		{
7551000c37aSeric 			markfailure(e, to, rcode);
75681161401Seric 			giveresponse(rcode, m, NULL, e);
7575dfc646bSeric 			continue;
7585dfc646bSeric 		}
7592a6e0786Seric 
7602a6e0786Seric 		/*
7619ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7629ec9501bSeric 		**	about them.
76325a99e2eSeric 		*/
76425a99e2eSeric 
76557fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
76625a99e2eSeric 		{
7671d8f1806Seric 			stripquotes(user);
7681d8f1806Seric 			stripquotes(host);
76925a99e2eSeric 		}
77025a99e2eSeric 
771cdb828c5Seric 		/* hack attack -- delivermail compatibility */
772cdb828c5Seric 		if (m == ProgMailer && *user == '|')
773cdb828c5Seric 			user++;
774cdb828c5Seric 
77525a99e2eSeric 		/*
7763efaed6eSeric 		**  If an error message has already been given, don't
7773efaed6eSeric 		**	bother to send to this address.
7783efaed6eSeric 		**
7793efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7803efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
7813efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
7823efaed6eSeric 		*/
7833efaed6eSeric 
7846cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7853efaed6eSeric 			continue;
7863efaed6eSeric 
787f2fec898Seric 		/* save statistics.... */
788588cad61Seric 		markstats(e, to);
789f2fec898Seric 
7903efaed6eSeric 		/*
79125a99e2eSeric 		**  See if this user name is "special".
79225a99e2eSeric 		**	If the user name has a slash in it, assume that this
79351552439Seric 		**	is a file -- send it off without further ado.  Note
79451552439Seric 		**	that this type of addresses is not processed along
79551552439Seric 		**	with the others, so we fudge on the To person.
79625a99e2eSeric 		*/
79725a99e2eSeric 
7982c017f8dSeric 		if (m == FileMailer)
79925a99e2eSeric 		{
800b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
80181161401Seric 			giveresponse(rcode, m, NULL, e);
802dde5acadSeric 			if (rcode == EX_OK)
803dde5acadSeric 				to->q_flags |= QSENT;
8045dfc646bSeric 			continue;
80525a99e2eSeric 		}
80625a99e2eSeric 
80713bbc08cSeric 		/*
80813bbc08cSeric 		**  Address is verified -- add this user to mailer
80913bbc08cSeric 		**  argv, and add it to the print list of recipients.
81013bbc08cSeric 		*/
81113bbc08cSeric 
812508daeccSeric 		/* link together the chain of recipients */
813508daeccSeric 		to->q_tchain = tochain;
814508daeccSeric 		tochain = to;
815508daeccSeric 
8165dfc646bSeric 		/* create list of users for error messages */
817db8841e9Seric 		(void) strcat(tobuf, ",");
818db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
819588cad61Seric 		define('u', user, e);		/* to user */
820588cad61Seric 		define('z', to->q_home, e);	/* user's home */
8215dfc646bSeric 
822c579ef51Seric 		/*
823508daeccSeric 		**  Expand out this user into argument list.
824c579ef51Seric 		*/
825c579ef51Seric 
826508daeccSeric 		if (!clever)
827c579ef51Seric 		{
828588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8295dfc646bSeric 			*pvp++ = newstr(buf);
8305dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8315dfc646bSeric 			{
8325dfc646bSeric 				/* allow some space for trailing parms */
8335dfc646bSeric 				break;
8345dfc646bSeric 			}
8355dfc646bSeric 		}
836c579ef51Seric 	}
8375dfc646bSeric 
838145b49b1Seric 	/* see if any addresses still exist */
839145b49b1Seric 	if (tobuf[0] == '\0')
840c579ef51Seric 	{
841588cad61Seric 		define('g', (char *) NULL, e);
842145b49b1Seric 		return (0);
843c579ef51Seric 	}
844145b49b1Seric 
8455dfc646bSeric 	/* print out messages as full list */
84663780dbdSeric 	e->e_to = tobuf + 1;
8475dfc646bSeric 
8485dfc646bSeric 	/*
8495dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8505dfc646bSeric 	*/
8515dfc646bSeric 
852c579ef51Seric 	while (!clever && *++mvp != NULL)
8535dfc646bSeric 	{
854588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8555dfc646bSeric 		*pvp++ = newstr(buf);
8565dfc646bSeric 		if (pvp >= &pv[MAXPV])
85708b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8585dfc646bSeric 	}
8595dfc646bSeric 	*pvp++ = NULL;
8605dfc646bSeric 
86125a99e2eSeric 	/*
86225a99e2eSeric 	**  Call the mailer.
8636328bdf7Seric 	**	The argument vector gets built, pipes
86425a99e2eSeric 	**	are created as necessary, and we fork & exec as
8656328bdf7Seric 	**	appropriate.
866c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
86725a99e2eSeric 	*/
86825a99e2eSeric 
8697ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
87086b26461Seric 		ctladdr = &e->e_from;
871134746fbSeric #ifdef NAMED_BIND
8722bcc6d2dSeric 	if (ConfigLevel < 2)
873912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
874134746fbSeric #endif
8759c9e68d9Seric 
8769c9e68d9Seric 	if (tTd(11, 1))
877134746fbSeric 	{
8789c9e68d9Seric 		printf("openmailer:");
8799c9e68d9Seric 		printav(pv);
8809c9e68d9Seric 	}
8819c9e68d9Seric 	errno = 0;
8829c9e68d9Seric 
8839c9e68d9Seric 	CurHostName = m->m_mailer;
8849c9e68d9Seric 
8859c9e68d9Seric 	/*
8869c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
8879c9e68d9Seric 	**  connection.
8889c9e68d9Seric 	**	In this case we don't actually fork.  We must be
8899c9e68d9Seric 	**	running SMTP for this to work.  We will return a
8909c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
8919c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
8929c9e68d9Seric 	*/
8939c9e68d9Seric 
8949c9e68d9Seric 	curhost = NULL;
895c931b82bSeric 	SmtpPhase = NULL;
8969c9e68d9Seric 
897*7febfd66Seric #ifdef XDEBUG
898*7febfd66Seric 	{
899*7febfd66Seric 		char wbuf[MAXLINE];
900*7febfd66Seric 
901*7febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
902*7febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
903*7febfd66Seric 		checkfd012(wbuf);
904*7febfd66Seric 	}
905*7febfd66Seric #endif
906*7febfd66Seric 
907*7febfd66Seric 
9089c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9099c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9109c9e68d9Seric 	{
9119c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9129c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9139c9e68d9Seric 		mci->mci_in = stdin;
9149c9e68d9Seric 		mci->mci_out = stdout;
9159c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9169c9e68d9Seric 		mci->mci_mailer = m;
9179c9e68d9Seric 	}
9189c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9199c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9209c9e68d9Seric 	{
9219c9e68d9Seric #ifdef DAEMON
9229c9e68d9Seric 		register int i;
9239c9e68d9Seric 		register u_short port;
9249c9e68d9Seric 
9259c9e68d9Seric 		CurHostName = pv[1];
9269c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9279c9e68d9Seric 
9289c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9299c9e68d9Seric 		{
9309c9e68d9Seric 			syserr("null signature");
931845e533cSeric 			rcode = EX_OSERR;
932b31e7f2bSeric 			goto give_up;
933b31e7f2bSeric 		}
9349c9e68d9Seric 
9359c9e68d9Seric 		if (!clever)
9369c9e68d9Seric 		{
9379c9e68d9Seric 			syserr("554 non-clever IPC");
9389c9e68d9Seric 			rcode = EX_OSERR;
9399c9e68d9Seric 			goto give_up;
9409c9e68d9Seric 		}
9419c9e68d9Seric 		if (pv[2] != NULL)
9429c9e68d9Seric 			port = atoi(pv[2]);
9439c9e68d9Seric 		else
9449c9e68d9Seric 			port = 0;
9459c9e68d9Seric tryhost:
9469c9e68d9Seric 		mci = NULL;
9479c9e68d9Seric 		while (*curhost != '\0')
9489c9e68d9Seric 		{
9499c9e68d9Seric 			register char *p;
9509c9e68d9Seric 			static char hostbuf[MAXNAME];
9519c9e68d9Seric 
9529c9e68d9Seric 			mci = NULL;
9539c9e68d9Seric 
9549c9e68d9Seric 			/* pull the next host from the signature */
9559c9e68d9Seric 			p = strchr(curhost, ':');
9569c9e68d9Seric 			if (p == NULL)
9579c9e68d9Seric 				p = &curhost[strlen(curhost)];
9589c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9599c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9609c9e68d9Seric 			if (*p != '\0')
9619c9e68d9Seric 				p++;
9629c9e68d9Seric 			curhost = p;
9639c9e68d9Seric 
9649c9e68d9Seric 			/* see if we already know that this host is fried */
9659c9e68d9Seric 			CurHostName = hostbuf;
9669c9e68d9Seric 			mci = mci_get(hostbuf, m);
9679c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9689c9e68d9Seric 			{
9699c9e68d9Seric 				if (tTd(11, 1))
9709c9e68d9Seric 				{
9719c9e68d9Seric 					printf("openmailer: ");
9729c9e68d9Seric 					mci_dump(mci);
9739c9e68d9Seric 				}
9749c9e68d9Seric 				CurHostName = mci->mci_host;
9759c9e68d9Seric 				break;
9769c9e68d9Seric 			}
9779c9e68d9Seric 			mci->mci_mailer = m;
9789c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9799c9e68d9Seric 				continue;
9809c9e68d9Seric 
9819c9e68d9Seric 			/* try the connection */
9829c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9839c9e68d9Seric 			message("Connecting to %s (%s)...",
9849c9e68d9Seric 				hostbuf, m->m_name);
9859c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
9869c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
9879c9e68d9Seric 			mci->mci_exitstat = i;
9889c9e68d9Seric 			mci->mci_errno = errno;
989f170942cSeric #ifdef NAMED_BIND
990f170942cSeric 			mci->mci_herrno = h_errno;
991f170942cSeric #endif
9929c9e68d9Seric 			if (i == EX_OK)
9939c9e68d9Seric 			{
9949c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
9959c9e68d9Seric 				mci_cache(mci);
996f170942cSeric 				if (TrafficLogFile != NULL)
997f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
998f170942cSeric 						getpid(), hostbuf);
9999c9e68d9Seric 				break;
10009c9e68d9Seric 			}
10019c9e68d9Seric 			else if (tTd(11, 1))
10029c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10039c9e68d9Seric 					i, errno);
10049c9e68d9Seric 
10059c9e68d9Seric 
10069c9e68d9Seric 			/* enter status of this host */
10079c9e68d9Seric 			setstat(i);
10089c9e68d9Seric 		}
10099c9e68d9Seric 		mci->mci_pid = 0;
10109c9e68d9Seric #else /* no DAEMON */
10119c9e68d9Seric 		syserr("554 openmailer: no IPC");
10129c9e68d9Seric 		if (tTd(11, 1))
10139c9e68d9Seric 			printf("openmailer: NULL\n");
10149c9e68d9Seric 		return NULL;
10159c9e68d9Seric #endif /* DAEMON */
10169c9e68d9Seric 	}
10179c9e68d9Seric 	else
10189c9e68d9Seric 	{
1019f170942cSeric 		if (TrafficLogFile != NULL)
10200e3bfef5Seric 		{
1021f170942cSeric 			char **av;
1022f170942cSeric 
1023f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1024f170942cSeric 			for (av = pv; *av != NULL; av++)
1025f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1026f170942cSeric 			fprintf(TrafficLogFile, "\n");
10276fe8c3bcSeric 		}
10286fe8c3bcSeric 
10299c9e68d9Seric 		/* create a pipe to shove the mail through */
10309c9e68d9Seric 		if (pipe(mpvect) < 0)
10319c9e68d9Seric 		{
10320e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10330e3bfef5Seric 				e->e_to, m->m_name);
10349c9e68d9Seric 			if (tTd(11, 1))
10359c9e68d9Seric 				printf("openmailer: NULL\n");
10369c9e68d9Seric 			rcode = EX_OSERR;
10379c9e68d9Seric 			goto give_up;
10389c9e68d9Seric 		}
10399c9e68d9Seric 
10409c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10419c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10429c9e68d9Seric 		{
10430e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10440e3bfef5Seric 				e->e_to, m->m_name);
10459c9e68d9Seric 			(void) close(mpvect[0]);
10469c9e68d9Seric 			(void) close(mpvect[1]);
10479c9e68d9Seric 			if (tTd(11, 1))
10489c9e68d9Seric 				printf("openmailer: NULL\n");
10499c9e68d9Seric 			rcode = EX_OSERR;
10509c9e68d9Seric 			goto give_up;
10519c9e68d9Seric 		}
10529c9e68d9Seric 
10539c9e68d9Seric 		/*
10549c9e68d9Seric 		**  Actually fork the mailer process.
10559c9e68d9Seric 		**	DOFORK is clever about retrying.
10569c9e68d9Seric 		**
10579c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10589c9e68d9Seric 		**	around so that endmail will get it.
10599c9e68d9Seric 		*/
10609c9e68d9Seric 
10619c9e68d9Seric 		if (e->e_xfp != NULL)
10629c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10639c9e68d9Seric 		(void) fflush(stdout);
10649c9e68d9Seric # ifdef SIGCHLD
10652b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
10669c9e68d9Seric # endif /* SIGCHLD */
10679c9e68d9Seric 		DOFORK(FORK);
10689c9e68d9Seric 		/* pid is set by DOFORK */
10699c9e68d9Seric 		if (pid < 0)
10709c9e68d9Seric 		{
10719c9e68d9Seric 			/* failure */
10720e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
10730e3bfef5Seric 				e->e_to, m->m_name);
10749c9e68d9Seric 			(void) close(mpvect[0]);
10759c9e68d9Seric 			(void) close(mpvect[1]);
10769c9e68d9Seric 			if (clever)
10779c9e68d9Seric 			{
10789c9e68d9Seric 				(void) close(rpvect[0]);
10799c9e68d9Seric 				(void) close(rpvect[1]);
10809c9e68d9Seric 			}
10819c9e68d9Seric 			if (tTd(11, 1))
10829c9e68d9Seric 				printf("openmailer: NULL\n");
10839c9e68d9Seric 			rcode = EX_OSERR;
10849c9e68d9Seric 			goto give_up;
10859c9e68d9Seric 		}
10869c9e68d9Seric 		else if (pid == 0)
10879c9e68d9Seric 		{
10889c9e68d9Seric 			int i;
10899c9e68d9Seric 			int saveerrno;
10909c9e68d9Seric 			char **ep;
10919c9e68d9Seric 			char *env[MAXUSERENVIRON];
10929c9e68d9Seric 			extern char **environ;
10939c9e68d9Seric 			extern int DtableSize;
10949c9e68d9Seric 
10959c9e68d9Seric 			/* child -- set up input & exec mailer */
10962b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
10972b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
10982b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
10999c9e68d9Seric 
11009c9e68d9Seric 			/* close any other cached connections */
11019c9e68d9Seric 			mci_flush(FALSE, mci);
11029c9e68d9Seric 
110344f2317fSeric 			/* reset user and group */
110444f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
110544f2317fSeric 			{
110644f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
110744f2317fSeric 				{
110844f2317fSeric 					(void) initgroups(DefUser, DefGid);
110944f2317fSeric 					(void) setuid(DefUid);
111044f2317fSeric 				}
111144f2317fSeric 				else
111244f2317fSeric 				{
111344f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
111444f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
111544f2317fSeric 						ctladdr->q_gid);
111644f2317fSeric 					(void) setuid(ctladdr->q_uid);
111744f2317fSeric 				}
111844f2317fSeric 			}
111944f2317fSeric 
112044f2317fSeric 			if (tTd(11, 2))
112144f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
112244f2317fSeric 					getuid(), geteuid());
112344f2317fSeric 
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 
11869c9e68d9Seric 			/* arrange for all the files to be closed */
11879c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
11889c9e68d9Seric 			{
11899c9e68d9Seric 				register int j;
119044f2317fSeric 
11919c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
11929c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
11939c9e68d9Seric 			}
11949c9e68d9Seric 
11959c9e68d9Seric 			/* set up the mailer environment */
11969c9e68d9Seric 			i = 0;
11979c9e68d9Seric 			env[i++] = "AGENT=sendmail";
11989c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
11999c9e68d9Seric 			{
12009c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12019c9e68d9Seric 					env[i++] = *ep;
12029c9e68d9Seric 			}
12039c9e68d9Seric 			env[i++] = NULL;
12049c9e68d9Seric 
12059c9e68d9Seric 			/* try to execute the mailer */
12069c9e68d9Seric 			execve(m->m_mailer, pv, env);
12079c9e68d9Seric 			saveerrno = errno;
12089c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12097c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12107c941fd2Seric 				_exit(EX_OSERR);
12119c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12129c9e68d9Seric 		}
12139c9e68d9Seric 
12149c9e68d9Seric 		/*
12159c9e68d9Seric 		**  Set up return value.
12169c9e68d9Seric 		*/
12179c9e68d9Seric 
12189c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12199c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12209c9e68d9Seric 		mci->mci_mailer = m;
12219c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12229c9e68d9Seric 		mci->mci_pid = pid;
12239c9e68d9Seric 		(void) close(mpvect[0]);
12249c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
12259c9e68d9Seric 		if (clever)
12269c9e68d9Seric 		{
12279c9e68d9Seric 			(void) close(rpvect[1]);
12289c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
12299c9e68d9Seric 		}
12309c9e68d9Seric 		else
12319c9e68d9Seric 		{
12329c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12339c9e68d9Seric 			mci->mci_in = NULL;
12349c9e68d9Seric 		}
12359c9e68d9Seric 	}
12369c9e68d9Seric 
12379c9e68d9Seric 	/*
12389c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12399c9e68d9Seric 	*/
12409c9e68d9Seric 
12419c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12429c9e68d9Seric 	{
12439c9e68d9Seric 		smtpinit(m, mci, e);
12449c9e68d9Seric 	}
12459c9e68d9Seric 	if (tTd(11, 1))
12469c9e68d9Seric 	{
12479c9e68d9Seric 		printf("openmailer: ");
12489c9e68d9Seric 		mci_dump(mci);
12499c9e68d9Seric 	}
12509c9e68d9Seric 
12519c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1252b31e7f2bSeric 	{
1253b31e7f2bSeric 		/* couldn't open the mailer */
1254b31e7f2bSeric 		rcode = mci->mci_exitstat;
12552a6bc25bSeric 		errno = mci->mci_errno;
1256f170942cSeric #ifdef NAMED_BIND
1257f170942cSeric 		h_errno = mci->mci_herrno;
1258f170942cSeric #endif
1259b31e7f2bSeric 		if (rcode == EX_OK)
1260b31e7f2bSeric 		{
1261b31e7f2bSeric 			/* shouldn't happen */
126208b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12636b0f339dSeric 				rcode, mci->mci_state, firstsig);
1264b31e7f2bSeric 			rcode = EX_SOFTWARE;
1265b31e7f2bSeric 		}
1266e9277e33Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
126790891494Seric 		{
126890891494Seric 			/* try next MX site */
126990891494Seric 			goto tryhost;
127090891494Seric 		}
1271b31e7f2bSeric 	}
1272b31e7f2bSeric 	else if (!clever)
1273b31e7f2bSeric 	{
1274b31e7f2bSeric 		/*
1275b31e7f2bSeric 		**  Format and send message.
1276b31e7f2bSeric 		*/
127715d084d5Seric 
1278b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1279b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1280b31e7f2bSeric 		putline("\n", mci->mci_out, m);
128103c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1282b31e7f2bSeric 
1283b31e7f2bSeric 		/* get the exit status */
1284c9be6216Seric 		rcode = endmailer(mci, e, pv);
1285134746fbSeric 	}
1286134746fbSeric 	else
1287b31e7f2bSeric #ifdef SMTP
1288134746fbSeric 	{
1289b31e7f2bSeric 		/*
1290b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1291b31e7f2bSeric 		*/
129215d084d5Seric 
1293b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1294b31e7f2bSeric 		if (rcode == EX_OK)
129575889e88Seric 		{
1296ded0d3daSkarels 			register char *t = tobuf;
1297ded0d3daSkarels 			register int i;
1298ded0d3daSkarels 
1299588cad61Seric 			/* send the recipient list */
130063780dbdSeric 			tobuf[0] = '\0';
130175889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
130275889e88Seric 			{
130363780dbdSeric 				e->e_to = to->q_paddr;
130415d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
130575889e88Seric 				{
130683b7ddc9Seric 					markfailure(e, to, i);
130781161401Seric 					giveresponse(i, m, mci, e);
130863780dbdSeric 				}
130975889e88Seric 				else
131075889e88Seric 				{
1311911693bfSbostic 					*t++ = ',';
1312b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1313b31e7f2bSeric 						continue;
1314ee39df61Seric 					*t = '\0';
1315588cad61Seric 				}
1316588cad61Seric 			}
1317588cad61Seric 
131863780dbdSeric 			/* now send the data */
131963780dbdSeric 			if (tobuf[0] == '\0')
1320b31e7f2bSeric 			{
13219c9e68d9Seric 				rcode = EX_OK;
132263780dbdSeric 				e->e_to = NULL;
1323b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1324b31e7f2bSeric 					smtprset(m, mci, e);
1325b31e7f2bSeric 			}
132675889e88Seric 			else
132775889e88Seric 			{
132863780dbdSeric 				e->e_to = tobuf + 1;
132975889e88Seric 				rcode = smtpdata(m, mci, e);
133063780dbdSeric 			}
133163780dbdSeric 
133263780dbdSeric 			/* now close the connection */
1333b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
133415d084d5Seric 				smtpquit(m, mci, e);
133563780dbdSeric 		}
13369c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13379c9e68d9Seric 		{
13389c9e68d9Seric 			/* try next MX site */
13399c9e68d9Seric 			goto tryhost;
13409c9e68d9Seric 		}
1341c579ef51Seric 	}
1342b31e7f2bSeric #else /* not SMTP */
1343a05b3449Sbostic 	{
134408b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1345845e533cSeric 		rcode = EX_CONFIG;
1346b31e7f2bSeric 		goto give_up;
1347a05b3449Sbostic 	}
1348b31e7f2bSeric #endif /* SMTP */
1349134746fbSeric #ifdef NAMED_BIND
13502bcc6d2dSeric 	if (ConfigLevel < 2)
1351912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1352134746fbSeric #endif
13535dfc646bSeric 
1354b31e7f2bSeric 	/* arrange a return receipt if requested */
13558c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1356b31e7f2bSeric 	{
1357b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1358b31e7f2bSeric 		/* do we want to send back more info? */
1359b31e7f2bSeric 	}
1360b31e7f2bSeric 
1361c77d1c25Seric 	/*
136263780dbdSeric 	**  Do final status disposal.
136363780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1364c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1365c77d1c25Seric 	**		addressees.
1366c77d1c25Seric 	*/
1367c77d1c25Seric 
1368b31e7f2bSeric   give_up:
136963780dbdSeric 	if (tobuf[0] != '\0')
137081161401Seric 		giveresponse(rcode, m, mci, e);
1371772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1372b31e7f2bSeric 	{
1373dde5acadSeric 		if (rcode != EX_OK)
137483b7ddc9Seric 			markfailure(e, to, rcode);
1375dde5acadSeric 		else
1376655518ecSeric 		{
1377dde5acadSeric 			to->q_flags |= QSENT;
1378655518ecSeric 			e->e_nsent++;
1379655518ecSeric 		}
1380b31e7f2bSeric 	}
1381b31e7f2bSeric 
1382b31e7f2bSeric 	/*
1383b31e7f2bSeric 	**  Restore state and return.
1384b31e7f2bSeric 	*/
1385c77d1c25Seric 
1386*7febfd66Seric #ifdef XDEBUG
1387*7febfd66Seric 	{
1388*7febfd66Seric 		char wbuf[MAXLINE];
1389*7febfd66Seric 
1390*7febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
1391*7febfd66Seric 		sprintf(wbuf, "%s... end of deliver(%s)", e->e_to, m->m_name);
1392*7febfd66Seric 		checkfd012(wbuf);
1393*7febfd66Seric 	}
1394*7febfd66Seric #endif
1395*7febfd66Seric 
139635490626Seric 	errno = 0;
1397588cad61Seric 	define('g', (char *) NULL, e);
13985826d9d3Seric 	return (rcode);
139925a99e2eSeric }
14005dfc646bSeric /*
140183b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
140283b7ddc9Seric **
140383b7ddc9Seric **	Parameters:
140483b7ddc9Seric **		e -- the envelope we are sending.
140583b7ddc9Seric **		q -- the address to mark.
140683b7ddc9Seric **		rcode -- the code signifying the particular failure.
140783b7ddc9Seric **
140883b7ddc9Seric **	Returns:
140983b7ddc9Seric **		none.
141083b7ddc9Seric **
141183b7ddc9Seric **	Side Effects:
141283b7ddc9Seric **		marks the address (and possibly the envelope) with the
141383b7ddc9Seric **			failure so that an error will be returned or
141483b7ddc9Seric **			the message will be queued, as appropriate.
141583b7ddc9Seric */
141683b7ddc9Seric 
141783b7ddc9Seric markfailure(e, q, rcode)
141883b7ddc9Seric 	register ENVELOPE *e;
141983b7ddc9Seric 	register ADDRESS *q;
142083b7ddc9Seric 	int rcode;
142183b7ddc9Seric {
142219c47125Seric 	char buf[MAXLINE];
142319c47125Seric 
142483b7ddc9Seric 	if (rcode == EX_OK)
142583b7ddc9Seric 		return;
1426f170942cSeric 	else if (rcode == EX_TEMPFAIL)
142783b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1428f170942cSeric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
1429f170942cSeric 		q->q_flags |= QBADADDR;
143083b7ddc9Seric }
143183b7ddc9Seric /*
1432c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1433c579ef51Seric **
1434c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1435c579ef51Seric **	violation), so we report those specially.  For other
1436c579ef51Seric **	errors, we choose a status message (into statmsg),
1437c579ef51Seric **	and if it represents an error, we print it.
1438c579ef51Seric **
1439c579ef51Seric **	Parameters:
1440c579ef51Seric **		pid -- pid of mailer.
1441c9be6216Seric **		e -- the current envelope.
1442c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1443c9be6216Seric **			(for error messages).
1444c579ef51Seric **
1445c579ef51Seric **	Returns:
1446c579ef51Seric **		exit code of mailer.
1447c579ef51Seric **
1448c579ef51Seric **	Side Effects:
1449c579ef51Seric **		none.
1450c579ef51Seric */
1451c579ef51Seric 
1452c9be6216Seric endmailer(mci, e, pv)
1453b31e7f2bSeric 	register MCI *mci;
1454c9be6216Seric 	register ENVELOPE *e;
1455c9be6216Seric 	char **pv;
1456c579ef51Seric {
1457588cad61Seric 	int st;
1458c579ef51Seric 
145975889e88Seric 	/* close any connections */
146075889e88Seric 	if (mci->mci_in != NULL)
1461c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
146275889e88Seric 	if (mci->mci_out != NULL)
1463c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
146475889e88Seric 	mci->mci_in = mci->mci_out = NULL;
146575889e88Seric 	mci->mci_state = MCIS_CLOSED;
146675889e88Seric 
146733db8731Seric 	/* in the IPC case there is nothing to wait for */
146875889e88Seric 	if (mci->mci_pid == 0)
146933db8731Seric 		return (EX_OK);
147033db8731Seric 
147133db8731Seric 	/* wait for the mailer process to die and collect status */
147275889e88Seric 	st = waitfor(mci->mci_pid);
1473588cad61Seric 	if (st == -1)
147478de67c1Seric 	{
1475c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1476588cad61Seric 		return (EX_SOFTWARE);
1477c579ef51Seric 	}
147833db8731Seric 
1479bf9bc890Seric 	if (WIFEXITED(st))
1480c579ef51Seric 	{
1481bf9bc890Seric 		/* normal death -- return status */
1482bf9bc890Seric 		return (WEXITSTATUS(st));
1483bf9bc890Seric 	}
1484bf9bc890Seric 
1485bf9bc890Seric 	/* it died a horrid death */
1486c9be6216Seric 	syserr("mailer %s died with signal %o", pv[0], st);
1487c9be6216Seric 
1488c9be6216Seric 	/* log the arguments */
1489c9be6216Seric 	if (e->e_xfp != NULL)
1490c9be6216Seric 	{
1491c9be6216Seric 		register char **av;
1492c9be6216Seric 
1493c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1494c9be6216Seric 		for (av = pv; *av != NULL; av++)
1495c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1496c9be6216Seric 		fprintf(e->e_xfp, "\n");
1497c9be6216Seric 	}
1498c9be6216Seric 
14995f73204aSeric 	ExitStat = EX_TEMPFAIL;
15005f73204aSeric 	return (EX_TEMPFAIL);
1501c579ef51Seric }
1502c579ef51Seric /*
150325a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
150425a99e2eSeric **
150525a99e2eSeric **	Parameters:
150625a99e2eSeric **		stat -- the status code from the mailer (high byte
150725a99e2eSeric **			only; core dumps must have been taken care of
150825a99e2eSeric **			already).
150981161401Seric **		m -- the mailer info for this mailer.
151081161401Seric **		mci -- the mailer connection info -- can be NULL if the
151181161401Seric **			response is given before the connection is made.
151281161401Seric **		e -- the current envelope.
151325a99e2eSeric **
151425a99e2eSeric **	Returns:
1515db8841e9Seric **		none.
151625a99e2eSeric **
151725a99e2eSeric **	Side Effects:
1518c1f9df2cSeric **		Errors may be incremented.
151925a99e2eSeric **		ExitStat may be set.
152025a99e2eSeric */
152125a99e2eSeric 
152281161401Seric giveresponse(stat, m, mci, e)
152325a99e2eSeric 	int stat;
1524588cad61Seric 	register MAILER *m;
152581161401Seric 	register MCI *mci;
1526198d9be0Seric 	ENVELOPE *e;
152725a99e2eSeric {
15289c16475dSeric 	register const char *statmsg;
152925a99e2eSeric 	extern char *SysExMsg[];
153025a99e2eSeric 	register int i;
1531d4bd8f0eSbostic 	extern int N_SysEx;
1532198d9be0Seric 	char buf[MAXLINE];
153325a99e2eSeric 
153413bbc08cSeric 	/*
153513bbc08cSeric 	**  Compute status message from code.
153613bbc08cSeric 	*/
153713bbc08cSeric 
153825a99e2eSeric 	i = stat - EX__BASE;
1539588cad61Seric 	if (stat == 0)
15406fe8c3bcSeric 	{
1541588cad61Seric 		statmsg = "250 Sent";
1542ce5531bdSeric 		if (e->e_statmsg != NULL)
15436fe8c3bcSeric 		{
1544ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15456fe8c3bcSeric 			statmsg = buf;
15466fe8c3bcSeric 		}
15476fe8c3bcSeric 	}
1548588cad61Seric 	else if (i < 0 || i > N_SysEx)
1549588cad61Seric 	{
1550588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1551588cad61Seric 		stat = EX_UNAVAILABLE;
1552588cad61Seric 		statmsg = buf;
1553588cad61Seric 	}
1554198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1555198d9be0Seric 	{
15567d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1557d4bd8f0eSbostic #ifdef NAMED_BIND
1558f28da541Smiriam 		if (h_errno == TRY_AGAIN)
15594d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1560f28da541Smiriam 		else
1561d4bd8f0eSbostic #endif
1562f28da541Smiriam 		{
15638557d168Seric 			if (errno != 0)
1564d87e85f3Seric 				statmsg = errstring(errno);
1565d87e85f3Seric 			else
1566d87e85f3Seric 			{
1567d87e85f3Seric #ifdef SMTP
1568d87e85f3Seric 				extern char SmtpError[];
1569d87e85f3Seric 
1570d87e85f3Seric 				statmsg = SmtpError;
15716c2c3107Seric #else /* SMTP */
1572d87e85f3Seric 				statmsg = NULL;
15736c2c3107Seric #endif /* SMTP */
1574d87e85f3Seric 			}
1575f28da541Smiriam 		}
1576d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1577d87e85f3Seric 		{
157887c9b3e7Seric 			(void) strcat(buf, ": ");
1579d87e85f3Seric 			(void) strcat(buf, statmsg);
15808557d168Seric 		}
1581198d9be0Seric 		statmsg = buf;
1582198d9be0Seric 	}
1583f170942cSeric #ifdef NAMED_BIND
1584f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1585f170942cSeric 	{
15864d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1587f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1588f170942cSeric 		statmsg = buf;
1589f170942cSeric 	}
1590f170942cSeric #endif
159125a99e2eSeric 	else
1592d87e85f3Seric 	{
159325a99e2eSeric 		statmsg = SysExMsg[i];
15947d55540cSeric 		if (*statmsg++ == ':')
15957d55540cSeric 		{
15967d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
15977d55540cSeric 			statmsg = buf;
15987d55540cSeric 		}
1599d87e85f3Seric 	}
1600588cad61Seric 
1601588cad61Seric 	/*
1602588cad61Seric 	**  Print the message as appropriate
1603588cad61Seric 	*/
1604588cad61Seric 
1605198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1606e12d03eeSeric 		message(&statmsg[4], errstring(errno));
160725a99e2eSeric 	else
160825a99e2eSeric 	{
1609c1f9df2cSeric 		Errors++;
1610e12d03eeSeric 		usrerr(statmsg, errstring(errno));
161125a99e2eSeric 	}
161225a99e2eSeric 
161325a99e2eSeric 	/*
161425a99e2eSeric 	**  Final cleanup.
161525a99e2eSeric 	**	Log a record of the transaction.  Compute the new
161625a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
161725a99e2eSeric 	**	that.
161825a99e2eSeric 	*/
161925a99e2eSeric 
16202f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
162181161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1622eb238f8cSeric 
1623eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1624eb238f8cSeric 		setstat(stat);
1625198d9be0Seric 	if (stat != EX_OK)
1626198d9be0Seric 	{
1627198d9be0Seric 		if (e->e_message != NULL)
1628198d9be0Seric 			free(e->e_message);
1629198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1630198d9be0Seric 	}
16318557d168Seric 	errno = 0;
1632d4bd8f0eSbostic #ifdef NAMED_BIND
1633f28da541Smiriam 	h_errno = 0;
1634d4bd8f0eSbostic #endif
1635eb238f8cSeric }
1636eb238f8cSeric /*
1637eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1638eb238f8cSeric **
1639eb238f8cSeric **	Parameters:
164081161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
164181161401Seric **		mci -- the mailer connection info -- can be NULL if the
164281161401Seric **			log is occuring when no connection is active.
164381161401Seric **		stat -- the message to print for the status.
164481161401Seric **		e -- the current envelope.
1645eb238f8cSeric **
1646eb238f8cSeric **	Returns:
1647eb238f8cSeric **		none
1648eb238f8cSeric **
1649eb238f8cSeric **	Side Effects:
1650eb238f8cSeric **		none
1651eb238f8cSeric */
1652eb238f8cSeric 
165381161401Seric logdelivery(m, mci, stat, e)
165481161401Seric 	MAILER *m;
165581161401Seric 	register MCI *mci;
1656eb238f8cSeric 	char *stat;
1657b31e7f2bSeric 	register ENVELOPE *e;
16585cf56be3Seric {
1659eb238f8cSeric # ifdef LOG
1660d6acf3eeSeric 	char buf[512];
16619507d1f9Seric 
166248ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
166381161401Seric 
166448ed5d33Seric 	if (m != NULL)
166571ff6caaSeric 	{
166648ed5d33Seric 		(void) strcat(buf, ", mailer=");
166748ed5d33Seric 		(void) strcat(buf, m->m_name);
166871ff6caaSeric 	}
166948ed5d33Seric 
167048ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
167171ff6caaSeric 	{
167271ff6caaSeric # ifdef DAEMON
1673e2f2f828Seric 		extern SOCKADDR CurHostAddr;
167448ed5d33Seric # endif
167571ff6caaSeric 
167648ed5d33Seric 		(void) strcat(buf, ", relay=");
167748ed5d33Seric 		(void) strcat(buf, mci->mci_host);
167848ed5d33Seric 
167948ed5d33Seric # ifdef DAEMON
168048ed5d33Seric 		(void) strcat(buf, " (");
1681e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
168248ed5d33Seric 		(void) strcat(buf, ")");
168371ff6caaSeric # endif
168471ff6caaSeric 	}
16859507d1f9Seric 	else
168648ed5d33Seric 	{
168748ed5d33Seric 		char *p = macvalue('h', e);
16889507d1f9Seric 
168948ed5d33Seric 		if (p != NULL && p[0] != '\0')
169048ed5d33Seric 		{
169148ed5d33Seric 			(void) strcat(buf, ", relay=");
169248ed5d33Seric 			(void) strcat(buf, p);
169348ed5d33Seric 		}
169448ed5d33Seric 	}
1695d6acf3eeSeric 
1696d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1697d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
16986c2c3107Seric # endif /* LOG */
169925a99e2eSeric }
170025a99e2eSeric /*
170151552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
170225a99e2eSeric **
170351552439Seric **	This can be made an arbitrary message separator by changing $l
170451552439Seric **
17059b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
17069b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
17079b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
17089b6c17a6Seric **	this kind of antique garbage????
170925a99e2eSeric **
171025a99e2eSeric **	Parameters:
171151552439Seric **		fp -- the file to output to.
171251552439Seric **		m -- the mailer describing this entry.
171325a99e2eSeric **
171425a99e2eSeric **	Returns:
171551552439Seric **		none
171625a99e2eSeric **
171725a99e2eSeric **	Side Effects:
171851552439Seric **		outputs some text to fp.
171925a99e2eSeric */
172025a99e2eSeric 
1721b31e7f2bSeric putfromline(fp, m, e)
172251552439Seric 	register FILE *fp;
172351552439Seric 	register MAILER *m;
1724b31e7f2bSeric 	ENVELOPE *e;
172525a99e2eSeric {
17262bc47524Seric 	char *template = "\201l\n";
172751552439Seric 	char buf[MAXLINE];
172825a99e2eSeric 
172957fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
173051552439Seric 		return;
173113bbc08cSeric 
17322c7e1b8dSeric # ifdef UGLYUUCP
173357fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
173474b6e67bSeric 	{
1735ea09d6edSeric 		char *bang;
1736ea09d6edSeric 		char xbuf[MAXLINE];
173774b6e67bSeric 
1738ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17396c2c3107Seric 		bang = strchr(buf, '!');
174074b6e67bSeric 		if (bang == NULL)
174134fcca25Seric 		{
174234fcca25Seric 			errno = 0;
174334fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
174434fcca25Seric 		}
174574b6e67bSeric 		else
1746588cad61Seric 		{
1747ea09d6edSeric 			*bang++ = '\0';
17482bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1749ea09d6edSeric 			template = xbuf;
175074b6e67bSeric 		}
1751588cad61Seric 	}
17526c2c3107Seric # endif /* UGLYUUCP */
1753b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
175477b52738Seric 	putline(buf, fp, m);
1755bc6e2962Seric }
1756bc6e2962Seric /*
175751552439Seric **  PUTBODY -- put the body of a message.
175851552439Seric **
175951552439Seric **	Parameters:
176051552439Seric **		fp -- file to output onto.
176177b52738Seric **		m -- a mailer descriptor to control output format.
17629a6a5f55Seric **		e -- the envelope to put out.
176303c02fdeSeric **		separator -- if non-NULL, a message separator that must
176403c02fdeSeric **			not be permitted in the resulting message.
176551552439Seric **
176651552439Seric **	Returns:
176751552439Seric **		none.
176851552439Seric **
176951552439Seric **	Side Effects:
177051552439Seric **		The message is written onto fp.
177151552439Seric */
177251552439Seric 
177303c02fdeSeric putbody(fp, m, e, separator)
177451552439Seric 	FILE *fp;
1775588cad61Seric 	MAILER *m;
17769a6a5f55Seric 	register ENVELOPE *e;
177703c02fdeSeric 	char *separator;
177851552439Seric {
177977b52738Seric 	char buf[MAXLINE];
178051552439Seric 
178151552439Seric 	/*
178251552439Seric 	**  Output the body of the message
178351552439Seric 	*/
178451552439Seric 
17859a6a5f55Seric 	if (e->e_dfp == NULL)
178651552439Seric 	{
17879a6a5f55Seric 		if (e->e_df != NULL)
17889a6a5f55Seric 		{
17899a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
17909a6a5f55Seric 			if (e->e_dfp == NULL)
17918f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
1792e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
17939a6a5f55Seric 		}
17949a6a5f55Seric 		else
179577b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
17969a6a5f55Seric 	}
17979a6a5f55Seric 	if (e->e_dfp != NULL)
17989a6a5f55Seric 	{
17999a6a5f55Seric 		rewind(e->e_dfp);
180077b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
180124fc8aeeSeric 		{
180224fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1803d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
18043462ad9eSeric 				(void) putc('>', fp);
180503c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
180603c02fdeSeric 			{
180703c02fdeSeric 				/* possible separator */
180803c02fdeSeric 				int sl = strlen(separator);
180903c02fdeSeric 
181003c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
181103c02fdeSeric 					(void) putc(' ', fp);
181203c02fdeSeric 			}
181377b52738Seric 			putline(buf, fp, m);
181424fc8aeeSeric 		}
181551552439Seric 
18169a6a5f55Seric 		if (ferror(e->e_dfp))
181751552439Seric 		{
181851552439Seric 			syserr("putbody: read error");
181951552439Seric 			ExitStat = EX_IOERR;
182051552439Seric 		}
182151552439Seric 	}
182251552439Seric 
18230890ba1fSeric 	/* some mailers want extra blank line at end of message */
18240890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
18250890ba1fSeric 		putline("", fp, m);
18260890ba1fSeric 
182751552439Seric 	(void) fflush(fp);
182851552439Seric 	if (ferror(fp) && errno != EPIPE)
182951552439Seric 	{
183051552439Seric 		syserr("putbody: write error");
183151552439Seric 		ExitStat = EX_IOERR;
183251552439Seric 	}
183351552439Seric 	errno = 0;
183425a99e2eSeric }
183525a99e2eSeric /*
183625a99e2eSeric **  MAILFILE -- Send a message to a file.
183725a99e2eSeric **
1838f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1839f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1840f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1841f129ec7dSeric **	sendmail runs as root.
1842f129ec7dSeric **
1843588cad61Seric **	This could be done as a subordinate mailer, except that it
1844588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1845588cad61Seric **	view this as being sufficiently important as to include it
1846588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1847588cad61Seric **	to create another process plus some pipes to save the message.
1848588cad61Seric **
184925a99e2eSeric **	Parameters:
185025a99e2eSeric **		filename -- the name of the file to send to.
18516259796dSeric **		ctladdr -- the controlling address header -- includes
18526259796dSeric **			the userid/groupid to be when sending.
185325a99e2eSeric **
185425a99e2eSeric **	Returns:
185525a99e2eSeric **		The exit code associated with the operation.
185625a99e2eSeric **
185725a99e2eSeric **	Side Effects:
185825a99e2eSeric **		none.
185925a99e2eSeric */
186025a99e2eSeric 
1861b31e7f2bSeric mailfile(filename, ctladdr, e)
186225a99e2eSeric 	char *filename;
18636259796dSeric 	ADDRESS *ctladdr;
1864b31e7f2bSeric 	register ENVELOPE *e;
186525a99e2eSeric {
186625a99e2eSeric 	register FILE *f;
186732d19d43Seric 	register int pid;
186815d084d5Seric 	int mode;
186925a99e2eSeric 
1870671745f3Seric 	if (tTd(11, 1))
1871671745f3Seric 	{
1872671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
1873671745f3Seric 		printaddr(ctladdr, FALSE);
1874671745f3Seric 	}
1875671745f3Seric 
1876f170942cSeric 	if (e->e_xfp != NULL)
1877f170942cSeric 		fflush(e->e_xfp);
1878f170942cSeric 
187932d19d43Seric 	/*
188032d19d43Seric 	**  Fork so we can change permissions here.
188132d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
188232d19d43Seric 	**	the complications of calling subroutines, etc.
188332d19d43Seric 	*/
188432d19d43Seric 
188532d19d43Seric 	DOFORK(fork);
188632d19d43Seric 
188732d19d43Seric 	if (pid < 0)
188832d19d43Seric 		return (EX_OSERR);
188932d19d43Seric 	else if (pid == 0)
189032d19d43Seric 	{
189132d19d43Seric 		/* child -- actually write to file */
1892f129ec7dSeric 		struct stat stb;
1893f129ec7dSeric 
18942b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
18952b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
18962b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
18973462ad9eSeric 		(void) umask(OldUmask);
189895f16dc0Seric 
1899f129ec7dSeric 		if (stat(filename, &stb) < 0)
19003a98e7eaSeric 			stb.st_mode = FileMode;
190115d084d5Seric 		mode = stb.st_mode;
190295f16dc0Seric 
190395f16dc0Seric 		/* limit the errors to those actually caused in the child */
190495f16dc0Seric 		errno = 0;
190595f16dc0Seric 		ExitStat = EX_OK;
190695f16dc0Seric 
1907f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1908f129ec7dSeric 			exit(EX_CANTCREAT);
190903827b5fSeric 		if (ctladdr == NULL)
19108f9146b0Srick 			ctladdr = &e->e_from;
191115d084d5Seric 		else
191215d084d5Seric 		{
191315d084d5Seric 			/* ignore setuid and setgid bits */
191415d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
191515d084d5Seric 		}
191615d084d5Seric 
19178f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
19188f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
19198f9146b0Srick 		{
19208f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
192195f16dc0Seric 			if (e->e_dfp == NULL)
192295f16dc0Seric 			{
19238f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
1924e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
19258f9146b0Srick 			}
19268f9146b0Srick 		}
19278f9146b0Srick 
192815d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1929e36b99e2Seric 		{
193095f16dc0Seric 			if (ctladdr->q_uid == 0)
193195f16dc0Seric 			{
1932898a126bSbostic 				(void) initgroups(DefUser, DefGid);
193395f16dc0Seric 			}
193495f16dc0Seric 			else
193595f16dc0Seric 			{
1936898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1937898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1938898a126bSbostic 					ctladdr->q_gid);
1939898a126bSbostic 			}
1940e36b99e2Seric 		}
194115d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1942e36b99e2Seric 		{
1943e36b99e2Seric 			if (ctladdr->q_uid == 0)
1944e36b99e2Seric 				(void) setuid(DefUid);
1945e36b99e2Seric 			else
19466259796dSeric 				(void) setuid(ctladdr->q_uid);
1947e36b99e2Seric 		}
194895f16dc0Seric 		FileName = filename;
194995f16dc0Seric 		LineNumber = 0;
19503a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
195125a99e2eSeric 		if (f == NULL)
195295f16dc0Seric 		{
1953b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
195432d19d43Seric 			exit(EX_CANTCREAT);
195595f16dc0Seric 		}
195625a99e2eSeric 
19570331ce05Seric 		putfromline(f, FileMailer, e);
19580331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
19590331ce05Seric 		putline("\n", f, FileMailer);
196003c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
19610331ce05Seric 		putline("\n", f, FileMailer);
196295f16dc0Seric 		if (ferror(f))
196395f16dc0Seric 		{
1964b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
196595f16dc0Seric 			setstat(EX_IOERR);
196695f16dc0Seric 		}
1967ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
196832d19d43Seric 		(void) fflush(stdout);
1969e36b99e2Seric 
197027628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1971c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
197295f16dc0Seric 		exit(ExitStat);
197313bbc08cSeric 		/*NOTREACHED*/
197432d19d43Seric 	}
197532d19d43Seric 	else
197632d19d43Seric 	{
197732d19d43Seric 		/* parent -- wait for exit status */
1978588cad61Seric 		int st;
197932d19d43Seric 
1980588cad61Seric 		st = waitfor(pid);
1981bf9bc890Seric 		if (WIFEXITED(st))
1982bf9bc890Seric 			return (WEXITSTATUS(st));
1983588cad61Seric 		else
1984b6a0de9dSeric 		{
1985b6a0de9dSeric 			syserr("child died on signal %d", st);
1986bf9bc890Seric 			return (EX_UNAVAILABLE);
1987b6a0de9dSeric 		}
19888f9146b0Srick 		/*NOTREACHED*/
198932d19d43Seric 	}
199025a99e2eSeric }
1991ea4dc939Seric /*
1992e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1993e103b48fSeric **
1994e103b48fSeric **	The signature describes how we are going to send this -- it
1995e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1996e103b48fSeric **	an ordered list of MX hosts.
1997e103b48fSeric **
1998e103b48fSeric **	Parameters:
1999e103b48fSeric **		m -- the mailer describing this host.
2000e103b48fSeric **		host -- the host name.
2001e103b48fSeric **		e -- the current envelope.
2002e103b48fSeric **
2003e103b48fSeric **	Returns:
2004e103b48fSeric **		The signature for this host.
2005e103b48fSeric **
2006e103b48fSeric **	Side Effects:
2007e103b48fSeric **		Can tweak the symbol table.
2008e103b48fSeric */
2009e103b48fSeric 
2010e103b48fSeric char *
2011e103b48fSeric hostsignature(m, host, e)
2012e103b48fSeric 	register MAILER *m;
2013e103b48fSeric 	char *host;
2014e103b48fSeric 	ENVELOPE *e;
2015e103b48fSeric {
2016e103b48fSeric 	register char *p;
2017e103b48fSeric 	register STAB *s;
2018e103b48fSeric 	int i;
2019e103b48fSeric 	int len;
2020e103b48fSeric #ifdef NAMED_BIND
2021e103b48fSeric 	int nmx;
2022e103b48fSeric 	auto int rcode;
2023bafdc4e5Seric 	char *hp;
2024bafdc4e5Seric 	char *endp;
2025516782b4Seric 	int oldoptions;
2026e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2027e103b48fSeric #endif
2028e103b48fSeric 
2029e103b48fSeric 	/*
2030e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2031e103b48fSeric 	*/
2032e103b48fSeric 
2033e103b48fSeric 	p = m->m_mailer;
2034e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2035e103b48fSeric 	{
2036e103b48fSeric 		/* just an ordinary mailer */
2037e103b48fSeric 		return host;
2038e103b48fSeric 	}
2039e103b48fSeric 
2040e103b48fSeric 	/*
2041e103b48fSeric 	**  If it is a numeric address, just return it.
2042e103b48fSeric 	*/
2043e103b48fSeric 
2044e103b48fSeric 	if (host[0] == '[')
2045e103b48fSeric 		return host;
2046e103b48fSeric 
2047e103b48fSeric 	/*
2048e103b48fSeric 	**  Look it up in the symbol table.
2049e103b48fSeric 	*/
2050e103b48fSeric 
2051e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2052e103b48fSeric 	if (s->s_hostsig != NULL)
2053e103b48fSeric 		return s->s_hostsig;
2054e103b48fSeric 
2055e103b48fSeric 	/*
2056e103b48fSeric 	**  Not already there -- create a signature.
2057e103b48fSeric 	*/
2058e103b48fSeric 
2059e103b48fSeric #ifdef NAMED_BIND
2060516782b4Seric 	if (ConfigLevel < 2)
2061516782b4Seric 	{
2062516782b4Seric 		oldoptions = _res.options;
2063516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2064516782b4Seric 	}
2065516782b4Seric 
2066bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2067bafdc4e5Seric 	{
2068bafdc4e5Seric 		endp = strchr(hp, ':');
2069bafdc4e5Seric 		if (endp != NULL)
2070bafdc4e5Seric 			*endp = '\0';
2071bafdc4e5Seric 
20727bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
20737d55540cSeric 
2074e103b48fSeric 		if (nmx <= 0)
2075e103b48fSeric 		{
2076e103b48fSeric 			register MCI *mci;
2077e103b48fSeric 			extern int errno;
2078e103b48fSeric 
2079e103b48fSeric 			/* update the connection info for this host */
2080bafdc4e5Seric 			mci = mci_get(hp, m);
2081e103b48fSeric 			mci->mci_exitstat = rcode;
2082e103b48fSeric 			mci->mci_errno = errno;
2083f170942cSeric #ifdef NAMED_BIND
2084f170942cSeric 			mci->mci_herrno = h_errno;
2085f170942cSeric #endif
2086e103b48fSeric 
2087e103b48fSeric 			/* and return the original host name as the signature */
2088bafdc4e5Seric 			nmx = 1;
2089bafdc4e5Seric 			mxhosts[0] = hp;
2090e103b48fSeric 		}
2091e103b48fSeric 
2092e103b48fSeric 		len = 0;
2093e103b48fSeric 		for (i = 0; i < nmx; i++)
2094e103b48fSeric 		{
2095e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2096e103b48fSeric 		}
2097bafdc4e5Seric 		if (s->s_hostsig != NULL)
2098bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2099bafdc4e5Seric 		p = xalloc(len);
2100bafdc4e5Seric 		if (s->s_hostsig != NULL)
2101bafdc4e5Seric 		{
2102bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2103bafdc4e5Seric 			free(s->s_hostsig);
2104bafdc4e5Seric 			s->s_hostsig = p;
2105bafdc4e5Seric 			p += strlen(p);
2106bafdc4e5Seric 			*p++ = ':';
2107bafdc4e5Seric 		}
2108bafdc4e5Seric 		else
2109bafdc4e5Seric 			s->s_hostsig = p;
2110e103b48fSeric 		for (i = 0; i < nmx; i++)
2111e103b48fSeric 		{
2112e103b48fSeric 			if (i != 0)
2113e103b48fSeric 				*p++ = ':';
2114e103b48fSeric 			strcpy(p, mxhosts[i]);
2115e103b48fSeric 			p += strlen(p);
2116e103b48fSeric 		}
2117bafdc4e5Seric 		if (endp != NULL)
2118bafdc4e5Seric 			*endp++ = ':';
2119bafdc4e5Seric 	}
2120e103b48fSeric 	makelower(s->s_hostsig);
2121516782b4Seric 	if (ConfigLevel < 2)
2122516782b4Seric 		_res.options = oldoptions;
2123e103b48fSeric #else
2124e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2125e103b48fSeric 	s->s_hostsig = host;
2126e103b48fSeric #endif
2127e103b48fSeric 	if (tTd(17, 1))
2128e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2129e103b48fSeric 	return s->s_hostsig;
2130e103b48fSeric }
2131