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*34fcca25Seric static char sccsid[] = "@(#)deliver.c	8.17 (Berkeley) 08/28/93";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14f28da541Smiriam #include <netdb.h>
15911693bfSbostic #include <errno.h>
16134746fbSeric #ifdef NAMED_BIND
17912a731aSbostic #include <arpa/nameser.h>
18912a731aSbostic #include <resolv.h>
19f170942cSeric 
20f170942cSeric extern int	h_errno;
21134746fbSeric #endif
2225a99e2eSeric 
2325a99e2eSeric /*
249c9e68d9Seric **  SENDALL -- actually send all the messages.
259c9e68d9Seric **
269c9e68d9Seric **	Parameters:
279c9e68d9Seric **		e -- the envelope to send.
289c9e68d9Seric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
299c9e68d9Seric **			the current e->e_sendmode.
309c9e68d9Seric **
319c9e68d9Seric **	Returns:
329c9e68d9Seric **		none.
339c9e68d9Seric **
349c9e68d9Seric **	Side Effects:
359c9e68d9Seric **		Scans the send lists and sends everything it finds.
369c9e68d9Seric **		Delivers any appropriate error messages.
379c9e68d9Seric **		If we are running in a non-interactive mode, takes the
389c9e68d9Seric **			appropriate action.
399c9e68d9Seric */
409c9e68d9Seric 
419c9e68d9Seric sendall(e, mode)
429c9e68d9Seric 	ENVELOPE *e;
439c9e68d9Seric 	char mode;
449c9e68d9Seric {
459c9e68d9Seric 	register ADDRESS *q;
469c9e68d9Seric 	char *owner;
479c9e68d9Seric 	int otherowners;
489c9e68d9Seric 	register ENVELOPE *ee;
499c9e68d9Seric 	ENVELOPE *splitenv = NULL;
506103d9b4Seric 	bool announcequeueup;
519c9e68d9Seric 
527880f3e4Seric 	/*
537880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
547880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
557880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
567880f3e4Seric 	**  addresses to be sent.
577880f3e4Seric 	*/
587880f3e4Seric 
597880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
60896b16f6Seric 	{
61896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
62896b16f6Seric 		return;
63896b16f6Seric 	}
64896b16f6Seric 
659c9e68d9Seric 	/* determine actual delivery mode */
669c9e68d9Seric 	if (mode == SM_DEFAULT)
679c9e68d9Seric 	{
689c9e68d9Seric 		mode = e->e_sendmode;
699c9e68d9Seric 		if (mode != SM_VERIFY &&
709c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
719c9e68d9Seric 			mode = SM_QUEUE;
726103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
739c9e68d9Seric 	}
746103d9b4Seric 	else
756103d9b4Seric 		announcequeueup = FALSE;
769c9e68d9Seric 
779c9e68d9Seric 	if (tTd(13, 1))
789c9e68d9Seric 	{
790e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
800e484fe5Seric 			mode, e->e_id);
819c9e68d9Seric 		printaddr(&e->e_from, FALSE);
829c9e68d9Seric 		printf("sendqueue:\n");
839c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
849c9e68d9Seric 	}
859c9e68d9Seric 
869c9e68d9Seric 	/*
879c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
889c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
899c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
909c9e68d9Seric 	*/
919c9e68d9Seric 
929c9e68d9Seric 	CurEnv = e;
939c9e68d9Seric 
949c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
959c9e68d9Seric 	{
969c9e68d9Seric 		errno = 0;
979c9e68d9Seric 		syserr("554 too many hops %d (%d max): from %s, to %s",
989c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
999c9e68d9Seric 			e->e_sendqueue->q_paddr);
1009c9e68d9Seric 		return;
1019c9e68d9Seric 	}
1029c9e68d9Seric 
103b8004690Seric 	/*
104b8004690Seric 	**  Do sender deletion.
105b8004690Seric 	**
106b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
107b8004690Seric 	**	This can happen if the name server is hosed when you
108b8004690Seric 	**	are trying to send mail.  The result is that the sender
109b8004690Seric 	**	is instantiated in the queue as a recipient.
110b8004690Seric 	*/
111b8004690Seric 
112e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
113e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1149c9e68d9Seric 	{
1159c9e68d9Seric 		if (tTd(13, 5))
1169c9e68d9Seric 		{
1179c9e68d9Seric 			printf("sendall: QDONTSEND ");
1189c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1199c9e68d9Seric 		}
1209c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1219c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1229c9e68d9Seric 	}
1239c9e68d9Seric 
124ce5531bdSeric 	/*
125ce5531bdSeric 	**  Handle alias owners.
126ce5531bdSeric 	**
127ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
128ce5531bdSeric 	**	We discard owners that are the same as the return path.
129ce5531bdSeric 	*/
130ce5531bdSeric 
131ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
132ce5531bdSeric 	{
133ce5531bdSeric 		register struct address *a;
134ce5531bdSeric 
135ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
136ce5531bdSeric 			continue;
137ce5531bdSeric 		if (a != NULL)
138ce5531bdSeric 			q->q_owner = a->q_owner;
139ce5531bdSeric 
140ce5531bdSeric 		if (q->q_owner != NULL &&
141ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
142ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
143ce5531bdSeric 			q->q_owner = NULL;
144ce5531bdSeric 	}
145ce5531bdSeric 
146ce5531bdSeric 	owner = "";
147ce5531bdSeric 	otherowners = 1;
148ce5531bdSeric 	while (owner != NULL && otherowners > 0)
149ce5531bdSeric 	{
150ce5531bdSeric 		owner = NULL;
151ce5531bdSeric 		otherowners = 0;
152ce5531bdSeric 
153ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
154ce5531bdSeric 		{
155ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
156ce5531bdSeric 				continue;
157ce5531bdSeric 
158ce5531bdSeric 			if (q->q_owner != NULL)
159ce5531bdSeric 			{
160ce5531bdSeric 				if (owner == NULL)
161ce5531bdSeric 					owner = q->q_owner;
162ce5531bdSeric 				else if (owner != q->q_owner)
163ce5531bdSeric 				{
164ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
165ce5531bdSeric 					{
166ce5531bdSeric 						/* make future comparisons cheap */
167ce5531bdSeric 						q->q_owner = owner;
168ce5531bdSeric 					}
169ce5531bdSeric 					else
170ce5531bdSeric 					{
171ce5531bdSeric 						otherowners++;
172ce5531bdSeric 					}
173ce5531bdSeric 					owner = q->q_owner;
174ce5531bdSeric 				}
175ce5531bdSeric 			}
176ce5531bdSeric 			else
177ce5531bdSeric 			{
178ce5531bdSeric 				otherowners++;
179ce5531bdSeric 			}
180ce5531bdSeric 		}
181ce5531bdSeric 
182ce5531bdSeric 		if (owner != NULL && otherowners > 0)
183ce5531bdSeric 		{
184ce5531bdSeric 			extern HDR *copyheader();
185ce5531bdSeric 			extern ADDRESS *copyqueue();
186ce5531bdSeric 
187ce5531bdSeric 			/*
188ce5531bdSeric 			**  Split this envelope into two.
189ce5531bdSeric 			*/
190ce5531bdSeric 
191ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
192ce5531bdSeric 			*ee = *e;
193ce5531bdSeric 			ee->e_id = NULL;
194ce5531bdSeric 			(void) queuename(ee, '\0');
195ce5531bdSeric 
196ce5531bdSeric 			if (tTd(13, 1))
197ce5531bdSeric 				printf("sendall: split %s into %s\n",
198ce5531bdSeric 					e->e_id, ee->e_id);
199ce5531bdSeric 
200ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
201ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
202ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
203ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
204ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
205ce5531bdSeric 			if (tTd(13, 5))
206ce5531bdSeric 			{
207ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
208ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
209ce5531bdSeric 			}
210ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
211ce5531bdSeric 			ee->e_dfp = NULL;
212ce5531bdSeric 			ee->e_xfp = NULL;
213ce5531bdSeric 			ee->e_lockfp = NULL;
214ce5531bdSeric 			ee->e_df = NULL;
215ce5531bdSeric 			ee->e_errormode = EM_MAIL;
216ce5531bdSeric 			ee->e_sibling = splitenv;
217ce5531bdSeric 			splitenv = ee;
218ce5531bdSeric 
219ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
220ce5531bdSeric 				if (q->q_owner == owner)
221ce5531bdSeric 					q->q_flags |= QDONTSEND;
222ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
223ce5531bdSeric 				if (q->q_owner != owner)
224ce5531bdSeric 					q->q_flags |= QDONTSEND;
225ce5531bdSeric 
226ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
227ce5531bdSeric 			{
228ce5531bdSeric 				ee->e_dfp = NULL;
229da662164Seric 				ee->e_df = queuename(ee, 'd');
230da662164Seric 				ee->e_df = newstr(ee->e_df);
231ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
232ce5531bdSeric 				{
233ce5531bdSeric 					syserr("sendall: link(%s, %s)",
234ce5531bdSeric 						e->e_df, ee->e_df);
235ce5531bdSeric 				}
236ce5531bdSeric 			}
237ce5531bdSeric 
238ce5531bdSeric 			if (mode != SM_VERIFY)
239ce5531bdSeric 				openxscript(ee);
240ce5531bdSeric #ifdef LOG
241ce5531bdSeric 			if (LogLevel > 4)
242ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
243ce5531bdSeric 					ee->e_id, e->e_id);
244ce5531bdSeric #endif
245ce5531bdSeric 		}
246ce5531bdSeric 	}
247ce5531bdSeric 
248ce5531bdSeric 	if (owner != NULL)
249ce5531bdSeric 	{
250ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
251ce5531bdSeric 		if (tTd(13, 5))
252ce5531bdSeric 		{
253ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
254ce5531bdSeric 			printaddr(&e->e_from, FALSE);
255ce5531bdSeric 		}
256ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
257ce5531bdSeric 		e->e_errormode = EM_MAIL;
258ce5531bdSeric 	}
259ce5531bdSeric 
260c1ac89b1Seric # ifdef QUEUE
261c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
262c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
263c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
264c1ac89b1Seric 	{
265c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2666103d9b4Seric 		queueup(e, TRUE, announcequeueup);
267ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2686103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
269c1ac89b1Seric 	}
270c1ac89b1Seric #endif /* QUEUE */
271c1ac89b1Seric 
272ce5531bdSeric 	if (splitenv != NULL)
273ce5531bdSeric 	{
274ce5531bdSeric 		if (tTd(13, 1))
275ce5531bdSeric 		{
276ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
277ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
278ce5531bdSeric 		}
279ce5531bdSeric 
280ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
281ce5531bdSeric 		{
282ce5531bdSeric 			CurEnv = ee;
283ce5531bdSeric 			sendenvelope(ee, mode);
284ce5531bdSeric 		}
285ce5531bdSeric 
286ce5531bdSeric 		CurEnv = e;
287ce5531bdSeric 	}
288ce5531bdSeric 	sendenvelope(e, mode);
289ce5531bdSeric 
290ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
291ce5531bdSeric 		dropenvelope(splitenv);
292ce5531bdSeric }
293ce5531bdSeric 
294ce5531bdSeric sendenvelope(e, mode)
295ce5531bdSeric 	register ENVELOPE *e;
296ce5531bdSeric 	char mode;
297ce5531bdSeric {
298ce5531bdSeric 	bool oldverbose;
299ce5531bdSeric 	int pid;
300ce5531bdSeric 	register ADDRESS *q;
3016b2765c6Seric 	char *qf;
3026b2765c6Seric 	char *id;
303ce5531bdSeric 
3047880f3e4Seric 	/*
3057880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3067880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3077880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3087880f3e4Seric 	**  addresses to be sent.
3097880f3e4Seric 	*/
3107880f3e4Seric 
3117880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
3127880f3e4Seric 	{
3137880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3147880f3e4Seric 		return;
3157880f3e4Seric 	}
3167880f3e4Seric 
317ce5531bdSeric 	oldverbose = Verbose;
318c1ac89b1Seric 	switch (mode)
319c1ac89b1Seric 	{
320c1ac89b1Seric 	  case SM_VERIFY:
321c1ac89b1Seric 		Verbose = TRUE;
322c1ac89b1Seric 		break;
323c1ac89b1Seric 
324c1ac89b1Seric 	  case SM_QUEUE:
325c1ac89b1Seric   queueonly:
326c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
327c1ac89b1Seric 		return;
328c1ac89b1Seric 
329c1ac89b1Seric 	  case SM_FORK:
330c1ac89b1Seric 		if (e->e_xfp != NULL)
331c1ac89b1Seric 			(void) fflush(e->e_xfp);
332c1ac89b1Seric 
3332b9178d3Seric # ifndef HASFLOCK
334c1ac89b1Seric 		/*
3356b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3366b2765c6Seric 		**  the lock is owned by a process, not by an open file
3376b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3386b2765c6Seric 		**  then restart from scratch in the child.
339c1ac89b1Seric 		*/
340c1ac89b1Seric 
3416b2765c6Seric 		/* save id for future use */
3426b2765c6Seric 		id = e->e_id;
3436b2765c6Seric 
3446b2765c6Seric 		/* now drop the envelope in the parent */
3456b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3466b2765c6Seric 		dropenvelope(e);
3476b2765c6Seric 
3486b2765c6Seric 		/* and reacquire in the child */
3496b2765c6Seric 		(void) dowork(id, TRUE, FALSE, e);
3506b2765c6Seric 
3516b2765c6Seric 		return;
3526b2765c6Seric 
3536b2765c6Seric # else /* HASFLOCK */
354c1ac89b1Seric 
355c1ac89b1Seric 		pid = fork();
356c1ac89b1Seric 		if (pid < 0)
357c1ac89b1Seric 		{
358c1ac89b1Seric 			goto queueonly;
359c1ac89b1Seric 		}
360c1ac89b1Seric 		else if (pid > 0)
361c1ac89b1Seric 		{
3620e484fe5Seric 			/* be sure we leave the temp files to our child */
3630e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3640e484fe5Seric 			if (e->e_lockfp != NULL)
3650e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3660e484fe5Seric 			e->e_lockfp = NULL;
3670e484fe5Seric 
3680e484fe5Seric 			/* close any random open files in the envelope */
3690e484fe5Seric 			closexscript(e);
3700e484fe5Seric 			if (e->e_dfp != NULL)
3710e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3720e484fe5Seric 			e->e_dfp = NULL;
3730e484fe5Seric 			e->e_id = e->e_df = NULL;
374c1ac89b1Seric 			return;
375c1ac89b1Seric 		}
376c1ac89b1Seric 
377c1ac89b1Seric 		/* double fork to avoid zombies */
378c1ac89b1Seric 		if (fork() > 0)
379c1ac89b1Seric 			exit(EX_OK);
380c1ac89b1Seric 
381c1ac89b1Seric 		/* be sure we are immune from the terminal */
3827880f3e4Seric 		disconnect(1, e);
383c1ac89b1Seric 
384c1ac89b1Seric 		/*
385c1ac89b1Seric 		**  Close any cached connections.
386c1ac89b1Seric 		**
387c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
388c1ac89b1Seric 		**	still knows about the connection.
389c1ac89b1Seric 		**
390c1ac89b1Seric 		**	This should only happen when delivering an error
391c1ac89b1Seric 		**	message.
392c1ac89b1Seric 		*/
393c1ac89b1Seric 
394c1ac89b1Seric 		mci_flush(FALSE, NULL);
395c1ac89b1Seric 
3966b2765c6Seric # endif /* HASFLOCK */
3976b2765c6Seric 
398c1ac89b1Seric 		break;
399c1ac89b1Seric 	}
400c1ac89b1Seric 
401c1ac89b1Seric 	/*
4029c9e68d9Seric 	**  Run through the list and send everything.
4035288e21aSeric 	**
4045288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4055288e21aSeric 	**	result in returned mail.
4069c9e68d9Seric 	*/
4079c9e68d9Seric 
4089c9e68d9Seric 	e->e_nsent = 0;
4095288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
4109c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4119c9e68d9Seric 	{
4129c9e68d9Seric 		if (mode == SM_VERIFY)
4139c9e68d9Seric 		{
4149c9e68d9Seric 			e->e_to = q->q_paddr;
4159c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4168dfca105Seric 			{
4178dfca105Seric 				message("deliverable: mailer %s, host %s, user %s",
4188dfca105Seric 					q->q_mailer->m_name,
4198dfca105Seric 					q->q_host,
4208dfca105Seric 					q->q_user);
4218dfca105Seric 			}
4229c9e68d9Seric 		}
4239c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4249c9e68d9Seric 		{
4259c9e68d9Seric # ifdef QUEUE
4269c9e68d9Seric 			/*
4279c9e68d9Seric 			**  Checkpoint the send list every few addresses
4289c9e68d9Seric 			*/
4299c9e68d9Seric 
4309c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4319c9e68d9Seric 			{
4329c9e68d9Seric 				queueup(e, TRUE, FALSE);
4339c9e68d9Seric 				e->e_nsent = 0;
4349c9e68d9Seric 			}
4359c9e68d9Seric # endif /* QUEUE */
4369c9e68d9Seric 			(void) deliver(e, q);
4379c9e68d9Seric 		}
4389c9e68d9Seric 	}
4399c9e68d9Seric 	Verbose = oldverbose;
4409c9e68d9Seric 
4419c9e68d9Seric 	if (mode == SM_FORK)
4429c9e68d9Seric 		finis();
4439c9e68d9Seric }
4449c9e68d9Seric /*
4459c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4469c9e68d9Seric **
4479c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4489c9e68d9Seric **	two processes on the same stack!!!
4499c9e68d9Seric **
4509c9e68d9Seric **	Parameters:
4519c9e68d9Seric **		none.
4529c9e68d9Seric **
4539c9e68d9Seric **	Returns:
4549c9e68d9Seric **		From a macro???  You've got to be kidding!
4559c9e68d9Seric **
4569c9e68d9Seric **	Side Effects:
4579c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4589c9e68d9Seric **			pid of child in parent, zero in child.
4599c9e68d9Seric **			-1 on unrecoverable error.
4609c9e68d9Seric **
4619c9e68d9Seric **	Notes:
4629c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4639c9e68d9Seric **		vfork for you.....
4649c9e68d9Seric */
4659c9e68d9Seric 
4669c9e68d9Seric # define NFORKTRIES	5
4679c9e68d9Seric 
4689c9e68d9Seric # ifndef FORK
4699c9e68d9Seric # define FORK	fork
4709c9e68d9Seric # endif
4719c9e68d9Seric 
4729c9e68d9Seric # define DOFORK(fORKfN) \
4739c9e68d9Seric {\
4749c9e68d9Seric 	register int i;\
4759c9e68d9Seric \
4769c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4779c9e68d9Seric 	{\
4789c9e68d9Seric 		pid = fORKfN();\
4799c9e68d9Seric 		if (pid >= 0)\
4809c9e68d9Seric 			break;\
4819c9e68d9Seric 		if (i > 0)\
4829c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
4839c9e68d9Seric 	}\
4849c9e68d9Seric }
4859c9e68d9Seric /*
4869c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
4879c9e68d9Seric **
4889c9e68d9Seric **	Parameters:
4899c9e68d9Seric **		none.
4909c9e68d9Seric **
4919c9e68d9Seric **	Returns:
4929c9e68d9Seric **		pid of child in parent.
4939c9e68d9Seric **		zero in child.
4949c9e68d9Seric **		-1 on error.
4959c9e68d9Seric **
4969c9e68d9Seric **	Side Effects:
4979c9e68d9Seric **		returns twice, once in parent and once in child.
4989c9e68d9Seric */
4999c9e68d9Seric 
5009c9e68d9Seric dofork()
5019c9e68d9Seric {
5029c9e68d9Seric 	register int pid;
5039c9e68d9Seric 
5049c9e68d9Seric 	DOFORK(fork);
5059c9e68d9Seric 	return (pid);
5069c9e68d9Seric }
5079c9e68d9Seric /*
50813bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
50913bbc08cSeric **
51013bbc08cSeric **	This routine delivers to everyone on the same host as the
51113bbc08cSeric **	user on the head of the list.  It is clever about mailers
51213bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
51313bbc08cSeric **	that it will deliver to all these addresses however -- so
51413bbc08cSeric **	deliver should be called once for each address on the
51513bbc08cSeric **	list.
51625a99e2eSeric **
51725a99e2eSeric **	Parameters:
518588cad61Seric **		e -- the envelope to deliver.
519c77d1c25Seric **		firstto -- head of the address list to deliver to.
52025a99e2eSeric **
52125a99e2eSeric **	Returns:
52225a99e2eSeric **		zero -- successfully delivered.
52325a99e2eSeric **		else -- some failure, see ExitStat for more info.
52425a99e2eSeric **
52525a99e2eSeric **	Side Effects:
52625a99e2eSeric **		The standard input is passed off to someone.
52725a99e2eSeric */
52825a99e2eSeric 
529588cad61Seric deliver(e, firstto)
530588cad61Seric 	register ENVELOPE *e;
531c77d1c25Seric 	ADDRESS *firstto;
53225a99e2eSeric {
53378442df3Seric 	char *host;			/* host being sent to */
53478442df3Seric 	char *user;			/* user being sent to */
53525a99e2eSeric 	char **pvp;
5365dfc646bSeric 	register char **mvp;
53725a99e2eSeric 	register char *p;
538588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5396259796dSeric 	ADDRESS *ctladdr;
540b31e7f2bSeric 	register MCI *mci;
541c77d1c25Seric 	register ADDRESS *to = firstto;
542c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
543772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
544911693bfSbostic 	int rcode;			/* response code */
545e103b48fSeric 	char *firstsig;			/* signature of firstto */
5469c9e68d9Seric 	int pid;
5479c9e68d9Seric 	char *curhost;
5489c9e68d9Seric 	int mpvect[2];
5499c9e68d9Seric 	int rpvect[2];
550ee6bf8dfSeric 	char *pv[MAXPV+1];
551579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
552ee6bf8dfSeric 	char buf[MAXNAME];
553c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
554fabb3bd4Seric 	extern int checkcompat();
5559c9e68d9Seric 	extern FILE *fdopen();
55625a99e2eSeric 
55735490626Seric 	errno = 0;
558ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5595dfc646bSeric 		return (0);
56025a99e2eSeric 
561134746fbSeric #ifdef NAMED_BIND
562912a731aSbostic 	/* unless interactive, try twice, over a minute */
563912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
564912a731aSbostic 		_res.retrans = 30;
565912a731aSbostic 		_res.retry = 2;
566912a731aSbostic 	}
567d4bd8f0eSbostic #endif
568912a731aSbostic 
56951552439Seric 	m = to->q_mailer;
57051552439Seric 	host = to->q_host;
571c9be6216Seric 	CurEnv = e;			/* just in case */
5724384d521Seric 	e->e_statmsg = NULL;
57351552439Seric 
5746ef48975Seric 	if (tTd(10, 1))
5755dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
57651552439Seric 			m->m_mno, host, to->q_user);
577f3dbc832Seric 
578f3dbc832Seric 	/*
579f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
580f3dbc832Seric 	**  connections now, just mark these addresses and return.
581f3dbc832Seric 	**	This is useful if we want to batch connections to
582f3dbc832Seric 	**	reduce load.  This will cause the messages to be
583f3dbc832Seric 	**	queued up, and a daemon will come along to send the
584f3dbc832Seric 	**	messages later.
585f3dbc832Seric 	**		This should be on a per-mailer basis.
586f3dbc832Seric 	*/
587f3dbc832Seric 
58819c47125Seric 	if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) &&
58919c47125Seric 	    bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
590f3dbc832Seric 	{
591f3dbc832Seric 		for (; to != NULL; to = to->q_next)
592f4560e80Seric 		{
593ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
594c6e18ac5Seric 			    to->q_mailer != m)
595f4560e80Seric 				continue;
596268a25e1Seric 			to->q_flags |= QQUEUEUP;
597588cad61Seric 			e->e_to = to->q_paddr;
59808b25121Seric 			message("queued");
5992f624c86Seric 			if (LogLevel > 8)
60081161401Seric 				logdelivery(m, NULL, "queued", e);
601f4560e80Seric 		}
602588cad61Seric 		e->e_to = NULL;
603f3dbc832Seric 		return (0);
604f3dbc832Seric 	}
605f3dbc832Seric 
60625a99e2eSeric 	/*
6075dfc646bSeric 	**  Do initial argv setup.
6085dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6095dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6105dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6115dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6125dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6133bea8136Seric 	**		The from address rewrite is expected to make
6143bea8136Seric 	**		the address relative to the other end.
6155dfc646bSeric 	*/
6165dfc646bSeric 
61778442df3Seric 	/* rewrite from address, using rewriting rules */
618efe54562Seric 	rcode = EX_OK;
619efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
620efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
621efe54562Seric 					   &rcode, e));
622ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
623588cad61Seric 	define('h', host, e);			/* to host */
6245dfc646bSeric 	Errors = 0;
6255dfc646bSeric 	pvp = pv;
6265dfc646bSeric 	*pvp++ = m->m_argv[0];
6275dfc646bSeric 
6285dfc646bSeric 	/* insert -f or -r flag as appropriate */
62957fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6305dfc646bSeric 	{
63157fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6325dfc646bSeric 			*pvp++ = "-f";
6335dfc646bSeric 		else
6345dfc646bSeric 			*pvp++ = "-r";
635c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6365dfc646bSeric 	}
6375dfc646bSeric 
6385dfc646bSeric 	/*
6395dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6405dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6415dfc646bSeric 	**  be one of these, and there are only a few more slots
6425dfc646bSeric 	**  in the pv after it.
6435dfc646bSeric 	*/
6445dfc646bSeric 
6455dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6465dfc646bSeric 	{
6472bc47524Seric 		/* can't use strchr here because of sign extension problems */
6482bc47524Seric 		while (*p != '\0')
6492bc47524Seric 		{
6502bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6512bc47524Seric 			{
6522bc47524Seric 				if (*p == 'u')
6535dfc646bSeric 					break;
6542bc47524Seric 			}
6552bc47524Seric 		}
6562bc47524Seric 
6572bc47524Seric 		if (*p != '\0')
6585dfc646bSeric 			break;
6595dfc646bSeric 
6605dfc646bSeric 		/* this entry is safe -- go ahead and process it */
661588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6625dfc646bSeric 		*pvp++ = newstr(buf);
6635dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6645dfc646bSeric 		{
66508b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6665dfc646bSeric 			return (-1);
6675dfc646bSeric 		}
6685dfc646bSeric 	}
669c579ef51Seric 
67033db8731Seric 	/*
67133db8731Seric 	**  If we have no substitution for the user name in the argument
67233db8731Seric 	**  list, we know that we must supply the names otherwise -- and
67333db8731Seric 	**  SMTP is the answer!!
67433db8731Seric 	*/
67533db8731Seric 
6765dfc646bSeric 	if (*mvp == NULL)
677c579ef51Seric 	{
678c579ef51Seric 		/* running SMTP */
6792c7e1b8dSeric # ifdef SMTP
680c579ef51Seric 		clever = TRUE;
681c579ef51Seric 		*pvp = NULL;
6826c2c3107Seric # else /* SMTP */
68333db8731Seric 		/* oops!  we don't implement SMTP */
68408b25121Seric 		syserr("554 SMTP style mailer");
6852c7e1b8dSeric 		return (EX_SOFTWARE);
6866c2c3107Seric # endif /* SMTP */
687c579ef51Seric 	}
6885dfc646bSeric 
6895dfc646bSeric 	/*
6905dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
6915dfc646bSeric 	**  run through our address list and append all the addresses
6925dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
6935dfc646bSeric 	**  always send another copy later.
6945dfc646bSeric 	*/
6955dfc646bSeric 
6965dfc646bSeric 	tobuf[0] = '\0';
697588cad61Seric 	e->e_to = tobuf;
6986259796dSeric 	ctladdr = NULL;
699e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7005dfc646bSeric 	for (; to != NULL; to = to->q_next)
7015dfc646bSeric 	{
7025dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
70357fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7045dfc646bSeric 			break;
7055dfc646bSeric 
7065dfc646bSeric 		/* if already sent or not for this host, don't send */
707ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
708e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
709e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7105dfc646bSeric 			continue;
7116259796dSeric 
7124b22ea87Seric 		/* avoid overflowing tobuf */
713aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7144b22ea87Seric 			break;
7154b22ea87Seric 
7166ef48975Seric 		if (tTd(10, 1))
717772e6e50Seric 		{
718772e6e50Seric 			printf("\nsend to ");
719772e6e50Seric 			printaddr(to, FALSE);
720772e6e50Seric 		}
721772e6e50Seric 
7226259796dSeric 		/* compute effective uid/gid when sending */
7237da1035fSeric 		if (to->q_mailer == ProgMailer)
7246259796dSeric 			ctladdr = getctladdr(to);
7256259796dSeric 
7265dfc646bSeric 		user = to->q_user;
727588cad61Seric 		e->e_to = to->q_paddr;
72875f1ade9Seric 		if (tTd(10, 5))
72975f1ade9Seric 		{
73075f1ade9Seric 			printf("deliver: QDONTSEND ");
73175f1ade9Seric 			printaddr(to, FALSE);
73275f1ade9Seric 		}
733ee4b0922Seric 		to->q_flags |= QDONTSEND;
7345dfc646bSeric 
7355dfc646bSeric 		/*
7365dfc646bSeric 		**  Check to see that these people are allowed to
7375dfc646bSeric 		**  talk to each other.
7382a6e0786Seric 		*/
7392a6e0786Seric 
74069582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
74169582d2fSeric 		{
74269582d2fSeric 			NoReturn = TRUE;
74308b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
74481161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
74569582d2fSeric 			continue;
74669582d2fSeric 		}
747fabb3bd4Seric 		rcode = checkcompat(to, e);
7481793c9c5Seric 		if (rcode != EX_OK)
7495dfc646bSeric 		{
7501000c37aSeric 			markfailure(e, to, rcode);
75181161401Seric 			giveresponse(rcode, m, NULL, e);
7525dfc646bSeric 			continue;
7535dfc646bSeric 		}
7542a6e0786Seric 
7552a6e0786Seric 		/*
7569ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7579ec9501bSeric 		**	about them.
75825a99e2eSeric 		*/
75925a99e2eSeric 
76057fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
76125a99e2eSeric 		{
7621d8f1806Seric 			stripquotes(user);
7631d8f1806Seric 			stripquotes(host);
76425a99e2eSeric 		}
76525a99e2eSeric 
766cdb828c5Seric 		/* hack attack -- delivermail compatibility */
767cdb828c5Seric 		if (m == ProgMailer && *user == '|')
768cdb828c5Seric 			user++;
769cdb828c5Seric 
77025a99e2eSeric 		/*
7713efaed6eSeric 		**  If an error message has already been given, don't
7723efaed6eSeric 		**	bother to send to this address.
7733efaed6eSeric 		**
7743efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7753efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
7763efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
7773efaed6eSeric 		*/
7783efaed6eSeric 
7796cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7803efaed6eSeric 			continue;
7813efaed6eSeric 
782f2fec898Seric 		/* save statistics.... */
783588cad61Seric 		markstats(e, to);
784f2fec898Seric 
7853efaed6eSeric 		/*
78625a99e2eSeric 		**  See if this user name is "special".
78725a99e2eSeric 		**	If the user name has a slash in it, assume that this
78851552439Seric 		**	is a file -- send it off without further ado.  Note
78951552439Seric 		**	that this type of addresses is not processed along
79051552439Seric 		**	with the others, so we fudge on the To person.
79125a99e2eSeric 		*/
79225a99e2eSeric 
7932c017f8dSeric 		if (m == FileMailer)
79425a99e2eSeric 		{
795b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
79681161401Seric 			giveresponse(rcode, m, NULL, e);
797dde5acadSeric 			if (rcode == EX_OK)
798dde5acadSeric 				to->q_flags |= QSENT;
7995dfc646bSeric 			continue;
80025a99e2eSeric 		}
80125a99e2eSeric 
80213bbc08cSeric 		/*
80313bbc08cSeric 		**  Address is verified -- add this user to mailer
80413bbc08cSeric 		**  argv, and add it to the print list of recipients.
80513bbc08cSeric 		*/
80613bbc08cSeric 
807508daeccSeric 		/* link together the chain of recipients */
808508daeccSeric 		to->q_tchain = tochain;
809508daeccSeric 		tochain = to;
810508daeccSeric 
8115dfc646bSeric 		/* create list of users for error messages */
812db8841e9Seric 		(void) strcat(tobuf, ",");
813db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
814588cad61Seric 		define('u', user, e);		/* to user */
815588cad61Seric 		define('z', to->q_home, e);	/* user's home */
8165dfc646bSeric 
817c579ef51Seric 		/*
818508daeccSeric 		**  Expand out this user into argument list.
819c579ef51Seric 		*/
820c579ef51Seric 
821508daeccSeric 		if (!clever)
822c579ef51Seric 		{
823588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8245dfc646bSeric 			*pvp++ = newstr(buf);
8255dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8265dfc646bSeric 			{
8275dfc646bSeric 				/* allow some space for trailing parms */
8285dfc646bSeric 				break;
8295dfc646bSeric 			}
8305dfc646bSeric 		}
831c579ef51Seric 	}
8325dfc646bSeric 
833145b49b1Seric 	/* see if any addresses still exist */
834145b49b1Seric 	if (tobuf[0] == '\0')
835c579ef51Seric 	{
836588cad61Seric 		define('g', (char *) NULL, e);
837145b49b1Seric 		return (0);
838c579ef51Seric 	}
839145b49b1Seric 
8405dfc646bSeric 	/* print out messages as full list */
84163780dbdSeric 	e->e_to = tobuf + 1;
8425dfc646bSeric 
8435dfc646bSeric 	/*
8445dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8455dfc646bSeric 	*/
8465dfc646bSeric 
847c579ef51Seric 	while (!clever && *++mvp != NULL)
8485dfc646bSeric 	{
849588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8505dfc646bSeric 		*pvp++ = newstr(buf);
8515dfc646bSeric 		if (pvp >= &pv[MAXPV])
85208b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8535dfc646bSeric 	}
8545dfc646bSeric 	*pvp++ = NULL;
8555dfc646bSeric 
85625a99e2eSeric 	/*
85725a99e2eSeric 	**  Call the mailer.
8586328bdf7Seric 	**	The argument vector gets built, pipes
85925a99e2eSeric 	**	are created as necessary, and we fork & exec as
8606328bdf7Seric 	**	appropriate.
861c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
86225a99e2eSeric 	*/
86325a99e2eSeric 
8647ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
86586b26461Seric 		ctladdr = &e->e_from;
866134746fbSeric #ifdef NAMED_BIND
8672bcc6d2dSeric 	if (ConfigLevel < 2)
868912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
869134746fbSeric #endif
8709c9e68d9Seric 
8719c9e68d9Seric 	if (tTd(11, 1))
872134746fbSeric 	{
8739c9e68d9Seric 		printf("openmailer:");
8749c9e68d9Seric 		printav(pv);
8759c9e68d9Seric 	}
8769c9e68d9Seric 	errno = 0;
8779c9e68d9Seric 
8789c9e68d9Seric 	CurHostName = m->m_mailer;
8799c9e68d9Seric 
8809c9e68d9Seric 	/*
8819c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
8829c9e68d9Seric 	**  connection.
8839c9e68d9Seric 	**	In this case we don't actually fork.  We must be
8849c9e68d9Seric 	**	running SMTP for this to work.  We will return a
8859c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
8869c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
8879c9e68d9Seric 	*/
8889c9e68d9Seric 
8899c9e68d9Seric 	curhost = NULL;
890c931b82bSeric 	SmtpPhase = NULL;
8919c9e68d9Seric 
8929c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
8939c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
8949c9e68d9Seric 	{
8959c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
8969c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
8979c9e68d9Seric 		mci->mci_in = stdin;
8989c9e68d9Seric 		mci->mci_out = stdout;
8999c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9009c9e68d9Seric 		mci->mci_mailer = m;
9019c9e68d9Seric 	}
9029c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9039c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9049c9e68d9Seric 	{
9059c9e68d9Seric #ifdef DAEMON
9069c9e68d9Seric 		register int i;
9079c9e68d9Seric 		register u_short port;
9089c9e68d9Seric 
9099c9e68d9Seric 		CurHostName = pv[1];
9109c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9119c9e68d9Seric 
9129c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9139c9e68d9Seric 		{
9149c9e68d9Seric 			syserr("null signature");
915845e533cSeric 			rcode = EX_OSERR;
916b31e7f2bSeric 			goto give_up;
917b31e7f2bSeric 		}
9189c9e68d9Seric 
9199c9e68d9Seric 		if (!clever)
9209c9e68d9Seric 		{
9219c9e68d9Seric 			syserr("554 non-clever IPC");
9229c9e68d9Seric 			rcode = EX_OSERR;
9239c9e68d9Seric 			goto give_up;
9249c9e68d9Seric 		}
9259c9e68d9Seric 		if (pv[2] != NULL)
9269c9e68d9Seric 			port = atoi(pv[2]);
9279c9e68d9Seric 		else
9289c9e68d9Seric 			port = 0;
9299c9e68d9Seric tryhost:
9309c9e68d9Seric 		mci = NULL;
9319c9e68d9Seric 		while (*curhost != '\0')
9329c9e68d9Seric 		{
9339c9e68d9Seric 			register char *p;
9349c9e68d9Seric 			static char hostbuf[MAXNAME];
9359c9e68d9Seric 
9369c9e68d9Seric 			mci = NULL;
9379c9e68d9Seric 
9389c9e68d9Seric 			/* pull the next host from the signature */
9399c9e68d9Seric 			p = strchr(curhost, ':');
9409c9e68d9Seric 			if (p == NULL)
9419c9e68d9Seric 				p = &curhost[strlen(curhost)];
9429c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9439c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9449c9e68d9Seric 			if (*p != '\0')
9459c9e68d9Seric 				p++;
9469c9e68d9Seric 			curhost = p;
9479c9e68d9Seric 
9489c9e68d9Seric 			/* see if we already know that this host is fried */
9499c9e68d9Seric 			CurHostName = hostbuf;
9509c9e68d9Seric 			mci = mci_get(hostbuf, m);
9519c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9529c9e68d9Seric 			{
9539c9e68d9Seric 				if (tTd(11, 1))
9549c9e68d9Seric 				{
9559c9e68d9Seric 					printf("openmailer: ");
9569c9e68d9Seric 					mci_dump(mci);
9579c9e68d9Seric 				}
9589c9e68d9Seric 				CurHostName = mci->mci_host;
9599c9e68d9Seric 				break;
9609c9e68d9Seric 			}
9619c9e68d9Seric 			mci->mci_mailer = m;
9629c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9639c9e68d9Seric 				continue;
9649c9e68d9Seric 
9659c9e68d9Seric 			/* try the connection */
9669c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9679c9e68d9Seric 			message("Connecting to %s (%s)...",
9689c9e68d9Seric 				hostbuf, m->m_name);
9699c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
9709c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
9719c9e68d9Seric 			mci->mci_exitstat = i;
9729c9e68d9Seric 			mci->mci_errno = errno;
973f170942cSeric #ifdef NAMED_BIND
974f170942cSeric 			mci->mci_herrno = h_errno;
975f170942cSeric #endif
9769c9e68d9Seric 			if (i == EX_OK)
9779c9e68d9Seric 			{
9789c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
9799c9e68d9Seric 				mci_cache(mci);
980f170942cSeric 				if (TrafficLogFile != NULL)
981f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
982f170942cSeric 						getpid(), hostbuf);
9839c9e68d9Seric 				break;
9849c9e68d9Seric 			}
9859c9e68d9Seric 			else if (tTd(11, 1))
9869c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
9879c9e68d9Seric 					i, errno);
9889c9e68d9Seric 
9899c9e68d9Seric 
9909c9e68d9Seric 			/* enter status of this host */
9919c9e68d9Seric 			setstat(i);
9929c9e68d9Seric 		}
9939c9e68d9Seric 		mci->mci_pid = 0;
9949c9e68d9Seric #else /* no DAEMON */
9959c9e68d9Seric 		syserr("554 openmailer: no IPC");
9969c9e68d9Seric 		if (tTd(11, 1))
9979c9e68d9Seric 			printf("openmailer: NULL\n");
9989c9e68d9Seric 		return NULL;
9999c9e68d9Seric #endif /* DAEMON */
10009c9e68d9Seric 	}
10019c9e68d9Seric 	else
10029c9e68d9Seric 	{
1003f170942cSeric #ifdef XDEBUG
1004f170942cSeric 		char wbuf[MAXLINE];
10056fe8c3bcSeric 
10066fe8c3bcSeric 		/* make absolutely certain 0, 1, and 2 are in use */
1007f170942cSeric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
1008f170942cSeric 		checkfd012(wbuf);
1009f170942cSeric #endif
10100e3bfef5Seric 
1011f170942cSeric 		if (TrafficLogFile != NULL)
10120e3bfef5Seric 		{
1013f170942cSeric 			char **av;
1014f170942cSeric 
1015f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1016f170942cSeric 			for (av = pv; *av != NULL; av++)
1017f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1018f170942cSeric 			fprintf(TrafficLogFile, "\n");
10196fe8c3bcSeric 		}
10206fe8c3bcSeric 
10219c9e68d9Seric 		/* create a pipe to shove the mail through */
10229c9e68d9Seric 		if (pipe(mpvect) < 0)
10239c9e68d9Seric 		{
10240e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10250e3bfef5Seric 				e->e_to, m->m_name);
10269c9e68d9Seric 			if (tTd(11, 1))
10279c9e68d9Seric 				printf("openmailer: NULL\n");
10289c9e68d9Seric 			rcode = EX_OSERR;
10299c9e68d9Seric 			goto give_up;
10309c9e68d9Seric 		}
10319c9e68d9Seric 
10329c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10339c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10349c9e68d9Seric 		{
10350e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10360e3bfef5Seric 				e->e_to, m->m_name);
10379c9e68d9Seric 			(void) close(mpvect[0]);
10389c9e68d9Seric 			(void) close(mpvect[1]);
10399c9e68d9Seric 			if (tTd(11, 1))
10409c9e68d9Seric 				printf("openmailer: NULL\n");
10419c9e68d9Seric 			rcode = EX_OSERR;
10429c9e68d9Seric 			goto give_up;
10439c9e68d9Seric 		}
10449c9e68d9Seric 
10459c9e68d9Seric 		/*
10469c9e68d9Seric 		**  Actually fork the mailer process.
10479c9e68d9Seric 		**	DOFORK is clever about retrying.
10489c9e68d9Seric 		**
10499c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10509c9e68d9Seric 		**	around so that endmail will get it.
10519c9e68d9Seric 		*/
10529c9e68d9Seric 
10539c9e68d9Seric 		if (e->e_xfp != NULL)
10549c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10559c9e68d9Seric 		(void) fflush(stdout);
10569c9e68d9Seric # ifdef SIGCHLD
10572b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
10589c9e68d9Seric # endif /* SIGCHLD */
10599c9e68d9Seric 		DOFORK(FORK);
10609c9e68d9Seric 		/* pid is set by DOFORK */
10619c9e68d9Seric 		if (pid < 0)
10629c9e68d9Seric 		{
10639c9e68d9Seric 			/* failure */
10640e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
10650e3bfef5Seric 				e->e_to, m->m_name);
10669c9e68d9Seric 			(void) close(mpvect[0]);
10679c9e68d9Seric 			(void) close(mpvect[1]);
10689c9e68d9Seric 			if (clever)
10699c9e68d9Seric 			{
10709c9e68d9Seric 				(void) close(rpvect[0]);
10719c9e68d9Seric 				(void) close(rpvect[1]);
10729c9e68d9Seric 			}
10739c9e68d9Seric 			if (tTd(11, 1))
10749c9e68d9Seric 				printf("openmailer: NULL\n");
10759c9e68d9Seric 			rcode = EX_OSERR;
10769c9e68d9Seric 			goto give_up;
10779c9e68d9Seric 		}
10789c9e68d9Seric 		else if (pid == 0)
10799c9e68d9Seric 		{
10809c9e68d9Seric 			int i;
10819c9e68d9Seric 			int saveerrno;
10829c9e68d9Seric 			char **ep;
10839c9e68d9Seric 			char *env[MAXUSERENVIRON];
10849c9e68d9Seric 			extern char **environ;
10859c9e68d9Seric 			extern int DtableSize;
10869c9e68d9Seric 
10879c9e68d9Seric 			/* child -- set up input & exec mailer */
10882b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
10892b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
10902b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
10919c9e68d9Seric 
10929c9e68d9Seric 			/* close any other cached connections */
10939c9e68d9Seric 			mci_flush(FALSE, mci);
10949c9e68d9Seric 
109544f2317fSeric 			/* reset user and group */
109644f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
109744f2317fSeric 			{
109844f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
109944f2317fSeric 				{
110044f2317fSeric 					(void) initgroups(DefUser, DefGid);
110144f2317fSeric 					(void) setuid(DefUid);
110244f2317fSeric 				}
110344f2317fSeric 				else
110444f2317fSeric 				{
110544f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
110644f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
110744f2317fSeric 						ctladdr->q_gid);
110844f2317fSeric 					(void) setuid(ctladdr->q_uid);
110944f2317fSeric 				}
111044f2317fSeric 			}
111144f2317fSeric 
111244f2317fSeric 			if (tTd(11, 2))
111344f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
111444f2317fSeric 					getuid(), geteuid());
111544f2317fSeric 
1116b986f6aaSeric 			/* move into some "safe" directory */
1117b986f6aaSeric 			if (m->m_execdir != NULL)
1118b986f6aaSeric 			{
1119b986f6aaSeric 				char *p, *q;
1120b986f6aaSeric 				char buf[MAXLINE];
1121b986f6aaSeric 
1122b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1123b986f6aaSeric 				{
1124b986f6aaSeric 					q = strchr(p, ':');
1125b986f6aaSeric 					if (q != NULL)
1126b986f6aaSeric 						*q = '\0';
1127b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1128b986f6aaSeric 					if (q != NULL)
1129b986f6aaSeric 						*q++ = ':';
1130b986f6aaSeric 					if (tTd(11, 20))
1131b986f6aaSeric 						printf("openmailer: trydir %s\n",
1132b986f6aaSeric 							buf);
1133b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1134b986f6aaSeric 						break;
1135b986f6aaSeric 				}
1136b986f6aaSeric 			}
1137b986f6aaSeric 
11389c9e68d9Seric 			/* arrange to filter std & diag output of command */
11399c9e68d9Seric 			if (clever)
11409c9e68d9Seric 			{
11419c9e68d9Seric 				(void) close(rpvect[0]);
11426fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11436fe8c3bcSeric 				{
11440e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11450e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11466fe8c3bcSeric 					_exit(EX_OSERR);
11476fe8c3bcSeric 				}
11489c9e68d9Seric 				(void) close(rpvect[1]);
11499c9e68d9Seric 			}
11509c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11519c9e68d9Seric 			{
11529c9e68d9Seric 				/* put mailer output in transcript */
11536fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11546fe8c3bcSeric 				{
11550e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11560e3bfef5Seric 						e->e_to, m->m_name,
11576fe8c3bcSeric 						fileno(e->e_xfp));
11586fe8c3bcSeric 					_exit(EX_OSERR);
11599c9e68d9Seric 				}
11606fe8c3bcSeric 			}
11616fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11626fe8c3bcSeric 			{
11630e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
11640e3bfef5Seric 					e->e_to, m->m_name);
11656fe8c3bcSeric 				_exit(EX_OSERR);
11666fe8c3bcSeric 			}
11679c9e68d9Seric 
11689c9e68d9Seric 			/* arrange to get standard input */
11699c9e68d9Seric 			(void) close(mpvect[1]);
11709c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11719c9e68d9Seric 			{
11720e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
11730e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
11749c9e68d9Seric 				_exit(EX_OSERR);
11759c9e68d9Seric 			}
11769c9e68d9Seric 			(void) close(mpvect[0]);
11779c9e68d9Seric 
11789c9e68d9Seric 			/* arrange for all the files to be closed */
11799c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
11809c9e68d9Seric 			{
11819c9e68d9Seric 				register int j;
118244f2317fSeric 
11839c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
11849c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
11859c9e68d9Seric 			}
11869c9e68d9Seric 
11879c9e68d9Seric 			/* set up the mailer environment */
11889c9e68d9Seric 			i = 0;
11899c9e68d9Seric 			env[i++] = "AGENT=sendmail";
11909c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
11919c9e68d9Seric 			{
11929c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
11939c9e68d9Seric 					env[i++] = *ep;
11949c9e68d9Seric 			}
11959c9e68d9Seric 			env[i++] = NULL;
11969c9e68d9Seric 
11979c9e68d9Seric 			/* try to execute the mailer */
11989c9e68d9Seric 			execve(m->m_mailer, pv, env);
11999c9e68d9Seric 			saveerrno = errno;
12009c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12017c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12027c941fd2Seric 				_exit(EX_OSERR);
12039c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12049c9e68d9Seric 		}
12059c9e68d9Seric 
12069c9e68d9Seric 		/*
12079c9e68d9Seric 		**  Set up return value.
12089c9e68d9Seric 		*/
12099c9e68d9Seric 
12109c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12119c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12129c9e68d9Seric 		mci->mci_mailer = m;
12139c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12149c9e68d9Seric 		mci->mci_pid = pid;
12159c9e68d9Seric 		(void) close(mpvect[0]);
12169c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
12179c9e68d9Seric 		if (clever)
12189c9e68d9Seric 		{
12199c9e68d9Seric 			(void) close(rpvect[1]);
12209c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
12219c9e68d9Seric 		}
12229c9e68d9Seric 		else
12239c9e68d9Seric 		{
12249c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12259c9e68d9Seric 			mci->mci_in = NULL;
12269c9e68d9Seric 		}
12279c9e68d9Seric 	}
12289c9e68d9Seric 
12299c9e68d9Seric 	/*
12309c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12319c9e68d9Seric 	*/
12329c9e68d9Seric 
12339c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12349c9e68d9Seric 	{
12359c9e68d9Seric 		smtpinit(m, mci, e);
12369c9e68d9Seric 	}
12379c9e68d9Seric 	if (tTd(11, 1))
12389c9e68d9Seric 	{
12399c9e68d9Seric 		printf("openmailer: ");
12409c9e68d9Seric 		mci_dump(mci);
12419c9e68d9Seric 	}
12429c9e68d9Seric 
12439c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1244b31e7f2bSeric 	{
1245b31e7f2bSeric 		/* couldn't open the mailer */
1246b31e7f2bSeric 		rcode = mci->mci_exitstat;
12472a6bc25bSeric 		errno = mci->mci_errno;
1248f170942cSeric #ifdef NAMED_BIND
1249f170942cSeric 		h_errno = mci->mci_herrno;
1250f170942cSeric #endif
1251b31e7f2bSeric 		if (rcode == EX_OK)
1252b31e7f2bSeric 		{
1253b31e7f2bSeric 			/* shouldn't happen */
125408b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12556b0f339dSeric 				rcode, mci->mci_state, firstsig);
1256b31e7f2bSeric 			rcode = EX_SOFTWARE;
1257b31e7f2bSeric 		}
1258e9277e33Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
125990891494Seric 		{
126090891494Seric 			/* try next MX site */
126190891494Seric 			goto tryhost;
126290891494Seric 		}
1263b31e7f2bSeric 	}
1264b31e7f2bSeric 	else if (!clever)
1265b31e7f2bSeric 	{
1266b31e7f2bSeric 		/*
1267b31e7f2bSeric 		**  Format and send message.
1268b31e7f2bSeric 		*/
126915d084d5Seric 
1270b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1271b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1272b31e7f2bSeric 		putline("\n", mci->mci_out, m);
127303c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1274b31e7f2bSeric 
1275b31e7f2bSeric 		/* get the exit status */
1276c9be6216Seric 		rcode = endmailer(mci, e, pv);
1277134746fbSeric 	}
1278134746fbSeric 	else
1279b31e7f2bSeric #ifdef SMTP
1280134746fbSeric 	{
1281b31e7f2bSeric 		/*
1282b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1283b31e7f2bSeric 		*/
128415d084d5Seric 
1285b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1286b31e7f2bSeric 		if (rcode == EX_OK)
128775889e88Seric 		{
1288ded0d3daSkarels 			register char *t = tobuf;
1289ded0d3daSkarels 			register int i;
1290ded0d3daSkarels 
1291588cad61Seric 			/* send the recipient list */
129263780dbdSeric 			tobuf[0] = '\0';
129375889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
129475889e88Seric 			{
129563780dbdSeric 				e->e_to = to->q_paddr;
129615d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
129775889e88Seric 				{
129883b7ddc9Seric 					markfailure(e, to, i);
129981161401Seric 					giveresponse(i, m, mci, e);
130063780dbdSeric 				}
130175889e88Seric 				else
130275889e88Seric 				{
1303911693bfSbostic 					*t++ = ',';
1304b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1305b31e7f2bSeric 						continue;
1306588cad61Seric 				}
1307588cad61Seric 			}
1308588cad61Seric 
130963780dbdSeric 			/* now send the data */
131063780dbdSeric 			if (tobuf[0] == '\0')
1311b31e7f2bSeric 			{
13129c9e68d9Seric 				rcode = EX_OK;
131363780dbdSeric 				e->e_to = NULL;
1314b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1315b31e7f2bSeric 					smtprset(m, mci, e);
1316b31e7f2bSeric 			}
131775889e88Seric 			else
131875889e88Seric 			{
131963780dbdSeric 				e->e_to = tobuf + 1;
132075889e88Seric 				rcode = smtpdata(m, mci, e);
132163780dbdSeric 			}
132263780dbdSeric 
132363780dbdSeric 			/* now close the connection */
1324b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
132515d084d5Seric 				smtpquit(m, mci, e);
132663780dbdSeric 		}
13279c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13289c9e68d9Seric 		{
13299c9e68d9Seric 			/* try next MX site */
13309c9e68d9Seric 			goto tryhost;
13319c9e68d9Seric 		}
1332c579ef51Seric 	}
1333b31e7f2bSeric #else /* not SMTP */
1334a05b3449Sbostic 	{
133508b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1336845e533cSeric 		rcode = EX_CONFIG;
1337b31e7f2bSeric 		goto give_up;
1338a05b3449Sbostic 	}
1339b31e7f2bSeric #endif /* SMTP */
1340134746fbSeric #ifdef NAMED_BIND
13412bcc6d2dSeric 	if (ConfigLevel < 2)
1342912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1343134746fbSeric #endif
13445dfc646bSeric 
1345b31e7f2bSeric 	/* arrange a return receipt if requested */
13468c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1347b31e7f2bSeric 	{
1348b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1349b31e7f2bSeric 		/* do we want to send back more info? */
1350b31e7f2bSeric 	}
1351b31e7f2bSeric 
1352c77d1c25Seric 	/*
135363780dbdSeric 	**  Do final status disposal.
135463780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1355c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1356c77d1c25Seric 	**		addressees.
1357c77d1c25Seric 	*/
1358c77d1c25Seric 
1359b31e7f2bSeric   give_up:
136063780dbdSeric 	if (tobuf[0] != '\0')
136181161401Seric 		giveresponse(rcode, m, mci, e);
1362772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1363b31e7f2bSeric 	{
1364dde5acadSeric 		if (rcode != EX_OK)
136583b7ddc9Seric 			markfailure(e, to, rcode);
1366dde5acadSeric 		else
1367655518ecSeric 		{
1368dde5acadSeric 			to->q_flags |= QSENT;
1369655518ecSeric 			e->e_nsent++;
1370655518ecSeric 		}
1371b31e7f2bSeric 	}
1372b31e7f2bSeric 
1373b31e7f2bSeric 	/*
1374b31e7f2bSeric 	**  Restore state and return.
1375b31e7f2bSeric 	*/
1376c77d1c25Seric 
137735490626Seric 	errno = 0;
1378588cad61Seric 	define('g', (char *) NULL, e);
13795826d9d3Seric 	return (rcode);
138025a99e2eSeric }
13815dfc646bSeric /*
138283b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
138383b7ddc9Seric **
138483b7ddc9Seric **	Parameters:
138583b7ddc9Seric **		e -- the envelope we are sending.
138683b7ddc9Seric **		q -- the address to mark.
138783b7ddc9Seric **		rcode -- the code signifying the particular failure.
138883b7ddc9Seric **
138983b7ddc9Seric **	Returns:
139083b7ddc9Seric **		none.
139183b7ddc9Seric **
139283b7ddc9Seric **	Side Effects:
139383b7ddc9Seric **		marks the address (and possibly the envelope) with the
139483b7ddc9Seric **			failure so that an error will be returned or
139583b7ddc9Seric **			the message will be queued, as appropriate.
139683b7ddc9Seric */
139783b7ddc9Seric 
139883b7ddc9Seric markfailure(e, q, rcode)
139983b7ddc9Seric 	register ENVELOPE *e;
140083b7ddc9Seric 	register ADDRESS *q;
140183b7ddc9Seric 	int rcode;
140283b7ddc9Seric {
140319c47125Seric 	char buf[MAXLINE];
140419c47125Seric 
140583b7ddc9Seric 	if (rcode == EX_OK)
140683b7ddc9Seric 		return;
1407f170942cSeric 	else if (rcode == EX_TEMPFAIL)
140883b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1409f170942cSeric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
1410f170942cSeric 		q->q_flags |= QBADADDR;
141183b7ddc9Seric }
141283b7ddc9Seric /*
1413c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1414c579ef51Seric **
1415c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1416c579ef51Seric **	violation), so we report those specially.  For other
1417c579ef51Seric **	errors, we choose a status message (into statmsg),
1418c579ef51Seric **	and if it represents an error, we print it.
1419c579ef51Seric **
1420c579ef51Seric **	Parameters:
1421c579ef51Seric **		pid -- pid of mailer.
1422c9be6216Seric **		e -- the current envelope.
1423c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1424c9be6216Seric **			(for error messages).
1425c579ef51Seric **
1426c579ef51Seric **	Returns:
1427c579ef51Seric **		exit code of mailer.
1428c579ef51Seric **
1429c579ef51Seric **	Side Effects:
1430c579ef51Seric **		none.
1431c579ef51Seric */
1432c579ef51Seric 
1433c9be6216Seric endmailer(mci, e, pv)
1434b31e7f2bSeric 	register MCI *mci;
1435c9be6216Seric 	register ENVELOPE *e;
1436c9be6216Seric 	char **pv;
1437c579ef51Seric {
1438588cad61Seric 	int st;
1439c579ef51Seric 
144075889e88Seric 	/* close any connections */
144175889e88Seric 	if (mci->mci_in != NULL)
1442c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
144375889e88Seric 	if (mci->mci_out != NULL)
1444c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
144575889e88Seric 	mci->mci_in = mci->mci_out = NULL;
144675889e88Seric 	mci->mci_state = MCIS_CLOSED;
144775889e88Seric 
144833db8731Seric 	/* in the IPC case there is nothing to wait for */
144975889e88Seric 	if (mci->mci_pid == 0)
145033db8731Seric 		return (EX_OK);
145133db8731Seric 
145233db8731Seric 	/* wait for the mailer process to die and collect status */
145375889e88Seric 	st = waitfor(mci->mci_pid);
1454588cad61Seric 	if (st == -1)
145578de67c1Seric 	{
1456c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1457588cad61Seric 		return (EX_SOFTWARE);
1458c579ef51Seric 	}
145933db8731Seric 
146033db8731Seric 	/* see if it died a horrid death */
1461c579ef51Seric 	if ((st & 0377) != 0)
1462c579ef51Seric 	{
1463c9be6216Seric 		syserr("mailer %s died with signal %o", pv[0], st);
1464c9be6216Seric 
1465c9be6216Seric 		/* log the arguments */
1466c9be6216Seric 		if (e->e_xfp != NULL)
1467c9be6216Seric 		{
1468c9be6216Seric 			register char **av;
1469c9be6216Seric 
1470c9be6216Seric 			fprintf(e->e_xfp, "Arguments:");
1471c9be6216Seric 			for (av = pv; *av != NULL; av++)
1472c9be6216Seric 				fprintf(e->e_xfp, " %s", *av);
1473c9be6216Seric 			fprintf(e->e_xfp, "\n");
1474c9be6216Seric 		}
1475c9be6216Seric 
14765f73204aSeric 		ExitStat = EX_TEMPFAIL;
14775f73204aSeric 		return (EX_TEMPFAIL);
1478c579ef51Seric 	}
147933db8731Seric 
148033db8731Seric 	/* normal death -- return status */
1481588cad61Seric 	st = (st >> 8) & 0377;
1482588cad61Seric 	return (st);
1483c579ef51Seric }
1484c579ef51Seric /*
148525a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
148625a99e2eSeric **
148725a99e2eSeric **	Parameters:
148825a99e2eSeric **		stat -- the status code from the mailer (high byte
148925a99e2eSeric **			only; core dumps must have been taken care of
149025a99e2eSeric **			already).
149181161401Seric **		m -- the mailer info for this mailer.
149281161401Seric **		mci -- the mailer connection info -- can be NULL if the
149381161401Seric **			response is given before the connection is made.
149481161401Seric **		e -- the current envelope.
149525a99e2eSeric **
149625a99e2eSeric **	Returns:
1497db8841e9Seric **		none.
149825a99e2eSeric **
149925a99e2eSeric **	Side Effects:
1500c1f9df2cSeric **		Errors may be incremented.
150125a99e2eSeric **		ExitStat may be set.
150225a99e2eSeric */
150325a99e2eSeric 
150481161401Seric giveresponse(stat, m, mci, e)
150525a99e2eSeric 	int stat;
1506588cad61Seric 	register MAILER *m;
150781161401Seric 	register MCI *mci;
1508198d9be0Seric 	ENVELOPE *e;
150925a99e2eSeric {
15109c16475dSeric 	register const char *statmsg;
151125a99e2eSeric 	extern char *SysExMsg[];
151225a99e2eSeric 	register int i;
1513d4bd8f0eSbostic 	extern int N_SysEx;
1514198d9be0Seric 	char buf[MAXLINE];
151525a99e2eSeric 
151613bbc08cSeric 	/*
151713bbc08cSeric 	**  Compute status message from code.
151813bbc08cSeric 	*/
151913bbc08cSeric 
152025a99e2eSeric 	i = stat - EX__BASE;
1521588cad61Seric 	if (stat == 0)
15226fe8c3bcSeric 	{
1523588cad61Seric 		statmsg = "250 Sent";
1524ce5531bdSeric 		if (e->e_statmsg != NULL)
15256fe8c3bcSeric 		{
1526ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15276fe8c3bcSeric 			statmsg = buf;
15286fe8c3bcSeric 		}
15296fe8c3bcSeric 	}
1530588cad61Seric 	else if (i < 0 || i > N_SysEx)
1531588cad61Seric 	{
1532588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1533588cad61Seric 		stat = EX_UNAVAILABLE;
1534588cad61Seric 		statmsg = buf;
1535588cad61Seric 	}
1536198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1537198d9be0Seric 	{
15387d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1539d4bd8f0eSbostic #ifdef NAMED_BIND
1540f28da541Smiriam 		if (h_errno == TRY_AGAIN)
15414d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1542f28da541Smiriam 		else
1543d4bd8f0eSbostic #endif
1544f28da541Smiriam 		{
15458557d168Seric 			if (errno != 0)
1546d87e85f3Seric 				statmsg = errstring(errno);
1547d87e85f3Seric 			else
1548d87e85f3Seric 			{
1549d87e85f3Seric #ifdef SMTP
1550d87e85f3Seric 				extern char SmtpError[];
1551d87e85f3Seric 
1552d87e85f3Seric 				statmsg = SmtpError;
15536c2c3107Seric #else /* SMTP */
1554d87e85f3Seric 				statmsg = NULL;
15556c2c3107Seric #endif /* SMTP */
1556d87e85f3Seric 			}
1557f28da541Smiriam 		}
1558d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1559d87e85f3Seric 		{
156087c9b3e7Seric 			(void) strcat(buf, ": ");
1561d87e85f3Seric 			(void) strcat(buf, statmsg);
15628557d168Seric 		}
1563198d9be0Seric 		statmsg = buf;
1564198d9be0Seric 	}
1565f170942cSeric #ifdef NAMED_BIND
1566f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1567f170942cSeric 	{
15684d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1569f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1570f170942cSeric 		statmsg = buf;
1571f170942cSeric 	}
1572f170942cSeric #endif
157325a99e2eSeric 	else
1574d87e85f3Seric 	{
157525a99e2eSeric 		statmsg = SysExMsg[i];
15767d55540cSeric 		if (*statmsg++ == ':')
15777d55540cSeric 		{
15787d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
15797d55540cSeric 			statmsg = buf;
15807d55540cSeric 		}
1581d87e85f3Seric 	}
1582588cad61Seric 
1583588cad61Seric 	/*
1584588cad61Seric 	**  Print the message as appropriate
1585588cad61Seric 	*/
1586588cad61Seric 
1587198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1588e12d03eeSeric 		message(&statmsg[4], errstring(errno));
158925a99e2eSeric 	else
159025a99e2eSeric 	{
1591c1f9df2cSeric 		Errors++;
1592e12d03eeSeric 		usrerr(statmsg, errstring(errno));
159325a99e2eSeric 	}
159425a99e2eSeric 
159525a99e2eSeric 	/*
159625a99e2eSeric 	**  Final cleanup.
159725a99e2eSeric 	**	Log a record of the transaction.  Compute the new
159825a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
159925a99e2eSeric 	**	that.
160025a99e2eSeric 	*/
160125a99e2eSeric 
16022f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
160381161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1604eb238f8cSeric 
1605eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1606eb238f8cSeric 		setstat(stat);
1607198d9be0Seric 	if (stat != EX_OK)
1608198d9be0Seric 	{
1609198d9be0Seric 		if (e->e_message != NULL)
1610198d9be0Seric 			free(e->e_message);
1611198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1612198d9be0Seric 	}
16138557d168Seric 	errno = 0;
1614d4bd8f0eSbostic #ifdef NAMED_BIND
1615f28da541Smiriam 	h_errno = 0;
1616d4bd8f0eSbostic #endif
1617eb238f8cSeric }
1618eb238f8cSeric /*
1619eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1620eb238f8cSeric **
1621eb238f8cSeric **	Parameters:
162281161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
162381161401Seric **		mci -- the mailer connection info -- can be NULL if the
162481161401Seric **			log is occuring when no connection is active.
162581161401Seric **		stat -- the message to print for the status.
162681161401Seric **		e -- the current envelope.
1627eb238f8cSeric **
1628eb238f8cSeric **	Returns:
1629eb238f8cSeric **		none
1630eb238f8cSeric **
1631eb238f8cSeric **	Side Effects:
1632eb238f8cSeric **		none
1633eb238f8cSeric */
1634eb238f8cSeric 
163581161401Seric logdelivery(m, mci, stat, e)
163681161401Seric 	MAILER *m;
163781161401Seric 	register MCI *mci;
1638eb238f8cSeric 	char *stat;
1639b31e7f2bSeric 	register ENVELOPE *e;
16405cf56be3Seric {
1641eb238f8cSeric # ifdef LOG
1642d6acf3eeSeric 	char buf[512];
16439507d1f9Seric 
164448ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
164581161401Seric 
164648ed5d33Seric 	if (m != NULL)
164771ff6caaSeric 	{
164848ed5d33Seric 		(void) strcat(buf, ", mailer=");
164948ed5d33Seric 		(void) strcat(buf, m->m_name);
165071ff6caaSeric 	}
165148ed5d33Seric 
165248ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
165371ff6caaSeric 	{
165471ff6caaSeric # ifdef DAEMON
1655e2f2f828Seric 		extern SOCKADDR CurHostAddr;
165648ed5d33Seric # endif
165771ff6caaSeric 
165848ed5d33Seric 		(void) strcat(buf, ", relay=");
165948ed5d33Seric 		(void) strcat(buf, mci->mci_host);
166048ed5d33Seric 
166148ed5d33Seric # ifdef DAEMON
166248ed5d33Seric 		(void) strcat(buf, " (");
1663e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
166448ed5d33Seric 		(void) strcat(buf, ")");
166571ff6caaSeric # endif
166671ff6caaSeric 	}
16679507d1f9Seric 	else
166848ed5d33Seric 	{
166948ed5d33Seric 		char *p = macvalue('h', e);
16709507d1f9Seric 
167148ed5d33Seric 		if (p != NULL && p[0] != '\0')
167248ed5d33Seric 		{
167348ed5d33Seric 			(void) strcat(buf, ", relay=");
167448ed5d33Seric 			(void) strcat(buf, p);
167548ed5d33Seric 		}
167648ed5d33Seric 	}
1677d6acf3eeSeric 
1678d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1679d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
16806c2c3107Seric # endif /* LOG */
168125a99e2eSeric }
168225a99e2eSeric /*
168351552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
168425a99e2eSeric **
168551552439Seric **	This can be made an arbitrary message separator by changing $l
168651552439Seric **
16879b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
16889b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
16899b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
16909b6c17a6Seric **	this kind of antique garbage????
169125a99e2eSeric **
169225a99e2eSeric **	Parameters:
169351552439Seric **		fp -- the file to output to.
169451552439Seric **		m -- the mailer describing this entry.
169525a99e2eSeric **
169625a99e2eSeric **	Returns:
169751552439Seric **		none
169825a99e2eSeric **
169925a99e2eSeric **	Side Effects:
170051552439Seric **		outputs some text to fp.
170125a99e2eSeric */
170225a99e2eSeric 
1703b31e7f2bSeric putfromline(fp, m, e)
170451552439Seric 	register FILE *fp;
170551552439Seric 	register MAILER *m;
1706b31e7f2bSeric 	ENVELOPE *e;
170725a99e2eSeric {
17082bc47524Seric 	char *template = "\201l\n";
170951552439Seric 	char buf[MAXLINE];
171025a99e2eSeric 
171157fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
171251552439Seric 		return;
171313bbc08cSeric 
17142c7e1b8dSeric # ifdef UGLYUUCP
171557fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
171674b6e67bSeric 	{
1717ea09d6edSeric 		char *bang;
1718ea09d6edSeric 		char xbuf[MAXLINE];
171974b6e67bSeric 
1720ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17216c2c3107Seric 		bang = strchr(buf, '!');
172274b6e67bSeric 		if (bang == NULL)
1723*34fcca25Seric 		{
1724*34fcca25Seric 			errno = 0;
1725*34fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
1726*34fcca25Seric 		}
172774b6e67bSeric 		else
1728588cad61Seric 		{
1729ea09d6edSeric 			*bang++ = '\0';
17302bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1731ea09d6edSeric 			template = xbuf;
173274b6e67bSeric 		}
1733588cad61Seric 	}
17346c2c3107Seric # endif /* UGLYUUCP */
1735b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
173677b52738Seric 	putline(buf, fp, m);
1737bc6e2962Seric }
1738bc6e2962Seric /*
173951552439Seric **  PUTBODY -- put the body of a message.
174051552439Seric **
174151552439Seric **	Parameters:
174251552439Seric **		fp -- file to output onto.
174377b52738Seric **		m -- a mailer descriptor to control output format.
17449a6a5f55Seric **		e -- the envelope to put out.
174503c02fdeSeric **		separator -- if non-NULL, a message separator that must
174603c02fdeSeric **			not be permitted in the resulting message.
174751552439Seric **
174851552439Seric **	Returns:
174951552439Seric **		none.
175051552439Seric **
175151552439Seric **	Side Effects:
175251552439Seric **		The message is written onto fp.
175351552439Seric */
175451552439Seric 
175503c02fdeSeric putbody(fp, m, e, separator)
175651552439Seric 	FILE *fp;
1757588cad61Seric 	MAILER *m;
17589a6a5f55Seric 	register ENVELOPE *e;
175903c02fdeSeric 	char *separator;
176051552439Seric {
176177b52738Seric 	char buf[MAXLINE];
176251552439Seric 
176351552439Seric 	/*
176451552439Seric 	**  Output the body of the message
176551552439Seric 	*/
176651552439Seric 
17679a6a5f55Seric 	if (e->e_dfp == NULL)
176851552439Seric 	{
17699a6a5f55Seric 		if (e->e_df != NULL)
17709a6a5f55Seric 		{
17719a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
17729a6a5f55Seric 			if (e->e_dfp == NULL)
17738f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
1774e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
17759a6a5f55Seric 		}
17769a6a5f55Seric 		else
177777b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
17789a6a5f55Seric 	}
17799a6a5f55Seric 	if (e->e_dfp != NULL)
17809a6a5f55Seric 	{
17819a6a5f55Seric 		rewind(e->e_dfp);
178277b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
178324fc8aeeSeric 		{
178424fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1785d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
17863462ad9eSeric 				(void) putc('>', fp);
178703c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
178803c02fdeSeric 			{
178903c02fdeSeric 				/* possible separator */
179003c02fdeSeric 				int sl = strlen(separator);
179103c02fdeSeric 
179203c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
179303c02fdeSeric 					(void) putc(' ', fp);
179403c02fdeSeric 			}
179577b52738Seric 			putline(buf, fp, m);
179624fc8aeeSeric 		}
179751552439Seric 
17989a6a5f55Seric 		if (ferror(e->e_dfp))
179951552439Seric 		{
180051552439Seric 			syserr("putbody: read error");
180151552439Seric 			ExitStat = EX_IOERR;
180251552439Seric 		}
180351552439Seric 	}
180451552439Seric 
18050890ba1fSeric 	/* some mailers want extra blank line at end of message */
18060890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
18070890ba1fSeric 		putline("", fp, m);
18080890ba1fSeric 
180951552439Seric 	(void) fflush(fp);
181051552439Seric 	if (ferror(fp) && errno != EPIPE)
181151552439Seric 	{
181251552439Seric 		syserr("putbody: write error");
181351552439Seric 		ExitStat = EX_IOERR;
181451552439Seric 	}
181551552439Seric 	errno = 0;
181625a99e2eSeric }
181725a99e2eSeric /*
181825a99e2eSeric **  MAILFILE -- Send a message to a file.
181925a99e2eSeric **
1820f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1821f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1822f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1823f129ec7dSeric **	sendmail runs as root.
1824f129ec7dSeric **
1825588cad61Seric **	This could be done as a subordinate mailer, except that it
1826588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1827588cad61Seric **	view this as being sufficiently important as to include it
1828588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1829588cad61Seric **	to create another process plus some pipes to save the message.
1830588cad61Seric **
183125a99e2eSeric **	Parameters:
183225a99e2eSeric **		filename -- the name of the file to send to.
18336259796dSeric **		ctladdr -- the controlling address header -- includes
18346259796dSeric **			the userid/groupid to be when sending.
183525a99e2eSeric **
183625a99e2eSeric **	Returns:
183725a99e2eSeric **		The exit code associated with the operation.
183825a99e2eSeric **
183925a99e2eSeric **	Side Effects:
184025a99e2eSeric **		none.
184125a99e2eSeric */
184225a99e2eSeric 
1843b31e7f2bSeric mailfile(filename, ctladdr, e)
184425a99e2eSeric 	char *filename;
18456259796dSeric 	ADDRESS *ctladdr;
1846b31e7f2bSeric 	register ENVELOPE *e;
184725a99e2eSeric {
184825a99e2eSeric 	register FILE *f;
184932d19d43Seric 	register int pid;
185015d084d5Seric 	int mode;
185125a99e2eSeric 
1852671745f3Seric 	if (tTd(11, 1))
1853671745f3Seric 	{
1854671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
1855671745f3Seric 		printaddr(ctladdr, FALSE);
1856671745f3Seric 	}
1857671745f3Seric 
1858f170942cSeric 	if (e->e_xfp != NULL)
1859f170942cSeric 		fflush(e->e_xfp);
1860f170942cSeric 
186132d19d43Seric 	/*
186232d19d43Seric 	**  Fork so we can change permissions here.
186332d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
186432d19d43Seric 	**	the complications of calling subroutines, etc.
186532d19d43Seric 	*/
186632d19d43Seric 
186732d19d43Seric 	DOFORK(fork);
186832d19d43Seric 
186932d19d43Seric 	if (pid < 0)
187032d19d43Seric 		return (EX_OSERR);
187132d19d43Seric 	else if (pid == 0)
187232d19d43Seric 	{
187332d19d43Seric 		/* child -- actually write to file */
1874f129ec7dSeric 		struct stat stb;
1875f129ec7dSeric 
18762b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
18772b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
18782b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
18793462ad9eSeric 		(void) umask(OldUmask);
188095f16dc0Seric 
1881f129ec7dSeric 		if (stat(filename, &stb) < 0)
18823a98e7eaSeric 			stb.st_mode = FileMode;
188315d084d5Seric 		mode = stb.st_mode;
188495f16dc0Seric 
188595f16dc0Seric 		/* limit the errors to those actually caused in the child */
188695f16dc0Seric 		errno = 0;
188795f16dc0Seric 		ExitStat = EX_OK;
188895f16dc0Seric 
1889f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1890f129ec7dSeric 			exit(EX_CANTCREAT);
189103827b5fSeric 		if (ctladdr == NULL)
18928f9146b0Srick 			ctladdr = &e->e_from;
189315d084d5Seric 		else
189415d084d5Seric 		{
189515d084d5Seric 			/* ignore setuid and setgid bits */
189615d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
189715d084d5Seric 		}
189815d084d5Seric 
18998f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
19008f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
19018f9146b0Srick 		{
19028f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
190395f16dc0Seric 			if (e->e_dfp == NULL)
190495f16dc0Seric 			{
19058f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
1906e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
19078f9146b0Srick 			}
19088f9146b0Srick 		}
19098f9146b0Srick 
191015d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1911e36b99e2Seric 		{
191295f16dc0Seric 			if (ctladdr->q_uid == 0)
191395f16dc0Seric 			{
1914898a126bSbostic 				(void) initgroups(DefUser, DefGid);
191595f16dc0Seric 			}
191695f16dc0Seric 			else
191795f16dc0Seric 			{
1918898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1919898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1920898a126bSbostic 					ctladdr->q_gid);
1921898a126bSbostic 			}
1922e36b99e2Seric 		}
192315d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1924e36b99e2Seric 		{
1925e36b99e2Seric 			if (ctladdr->q_uid == 0)
1926e36b99e2Seric 				(void) setuid(DefUid);
1927e36b99e2Seric 			else
19286259796dSeric 				(void) setuid(ctladdr->q_uid);
1929e36b99e2Seric 		}
193095f16dc0Seric 		FileName = filename;
193195f16dc0Seric 		LineNumber = 0;
19323a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
193325a99e2eSeric 		if (f == NULL)
193495f16dc0Seric 		{
193508b25121Seric 			message("554 cannot open");
193632d19d43Seric 			exit(EX_CANTCREAT);
193795f16dc0Seric 		}
193825a99e2eSeric 
19390331ce05Seric 		putfromline(f, FileMailer, e);
19400331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
19410331ce05Seric 		putline("\n", f, FileMailer);
194203c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
19430331ce05Seric 		putline("\n", f, FileMailer);
194495f16dc0Seric 		if (ferror(f))
194595f16dc0Seric 		{
194608b25121Seric 			message("451 I/O error");
194795f16dc0Seric 			setstat(EX_IOERR);
194895f16dc0Seric 		}
1949ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
195032d19d43Seric 		(void) fflush(stdout);
1951e36b99e2Seric 
195227628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1953c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
195495f16dc0Seric 		exit(ExitStat);
195513bbc08cSeric 		/*NOTREACHED*/
195632d19d43Seric 	}
195732d19d43Seric 	else
195832d19d43Seric 	{
195932d19d43Seric 		/* parent -- wait for exit status */
1960588cad61Seric 		int st;
196132d19d43Seric 
1962588cad61Seric 		st = waitfor(pid);
1963588cad61Seric 		if ((st & 0377) != 0)
1964588cad61Seric 			return (EX_UNAVAILABLE);
1965588cad61Seric 		else
1966588cad61Seric 			return ((st >> 8) & 0377);
19678f9146b0Srick 		/*NOTREACHED*/
196832d19d43Seric 	}
196925a99e2eSeric }
1970ea4dc939Seric /*
1971e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1972e103b48fSeric **
1973e103b48fSeric **	The signature describes how we are going to send this -- it
1974e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1975e103b48fSeric **	an ordered list of MX hosts.
1976e103b48fSeric **
1977e103b48fSeric **	Parameters:
1978e103b48fSeric **		m -- the mailer describing this host.
1979e103b48fSeric **		host -- the host name.
1980e103b48fSeric **		e -- the current envelope.
1981e103b48fSeric **
1982e103b48fSeric **	Returns:
1983e103b48fSeric **		The signature for this host.
1984e103b48fSeric **
1985e103b48fSeric **	Side Effects:
1986e103b48fSeric **		Can tweak the symbol table.
1987e103b48fSeric */
1988e103b48fSeric 
1989e103b48fSeric char *
1990e103b48fSeric hostsignature(m, host, e)
1991e103b48fSeric 	register MAILER *m;
1992e103b48fSeric 	char *host;
1993e103b48fSeric 	ENVELOPE *e;
1994e103b48fSeric {
1995e103b48fSeric 	register char *p;
1996e103b48fSeric 	register STAB *s;
1997e103b48fSeric 	int i;
1998e103b48fSeric 	int len;
1999e103b48fSeric #ifdef NAMED_BIND
2000e103b48fSeric 	int nmx;
2001e103b48fSeric 	auto int rcode;
2002bafdc4e5Seric 	char *hp;
2003bafdc4e5Seric 	char *endp;
2004516782b4Seric 	int oldoptions;
2005e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2006e103b48fSeric #endif
2007e103b48fSeric 
2008e103b48fSeric 	/*
2009e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2010e103b48fSeric 	*/
2011e103b48fSeric 
2012e103b48fSeric 	p = m->m_mailer;
2013e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2014e103b48fSeric 	{
2015e103b48fSeric 		/* just an ordinary mailer */
2016e103b48fSeric 		return host;
2017e103b48fSeric 	}
2018e103b48fSeric 
2019e103b48fSeric 	/*
2020e103b48fSeric 	**  If it is a numeric address, just return it.
2021e103b48fSeric 	*/
2022e103b48fSeric 
2023e103b48fSeric 	if (host[0] == '[')
2024e103b48fSeric 		return host;
2025e103b48fSeric 
2026e103b48fSeric 	/*
2027e103b48fSeric 	**  Look it up in the symbol table.
2028e103b48fSeric 	*/
2029e103b48fSeric 
2030e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2031e103b48fSeric 	if (s->s_hostsig != NULL)
2032e103b48fSeric 		return s->s_hostsig;
2033e103b48fSeric 
2034e103b48fSeric 	/*
2035e103b48fSeric 	**  Not already there -- create a signature.
2036e103b48fSeric 	*/
2037e103b48fSeric 
2038e103b48fSeric #ifdef NAMED_BIND
2039516782b4Seric 	if (ConfigLevel < 2)
2040516782b4Seric 	{
2041516782b4Seric 		oldoptions = _res.options;
2042516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2043516782b4Seric 	}
2044516782b4Seric 
2045bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2046bafdc4e5Seric 	{
2047bafdc4e5Seric 		endp = strchr(hp, ':');
2048bafdc4e5Seric 		if (endp != NULL)
2049bafdc4e5Seric 			*endp = '\0';
2050bafdc4e5Seric 
20517bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
20527d55540cSeric 
2053e103b48fSeric 		if (nmx <= 0)
2054e103b48fSeric 		{
2055e103b48fSeric 			register MCI *mci;
2056e103b48fSeric 			extern int errno;
2057e103b48fSeric 
2058e103b48fSeric 			/* update the connection info for this host */
2059bafdc4e5Seric 			mci = mci_get(hp, m);
2060e103b48fSeric 			mci->mci_exitstat = rcode;
2061e103b48fSeric 			mci->mci_errno = errno;
2062f170942cSeric #ifdef NAMED_BIND
2063f170942cSeric 			mci->mci_herrno = h_errno;
2064f170942cSeric #endif
2065e103b48fSeric 
2066e103b48fSeric 			/* and return the original host name as the signature */
2067bafdc4e5Seric 			nmx = 1;
2068bafdc4e5Seric 			mxhosts[0] = hp;
2069e103b48fSeric 		}
2070e103b48fSeric 
2071e103b48fSeric 		len = 0;
2072e103b48fSeric 		for (i = 0; i < nmx; i++)
2073e103b48fSeric 		{
2074e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2075e103b48fSeric 		}
2076bafdc4e5Seric 		if (s->s_hostsig != NULL)
2077bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2078bafdc4e5Seric 		p = xalloc(len);
2079bafdc4e5Seric 		if (s->s_hostsig != NULL)
2080bafdc4e5Seric 		{
2081bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2082bafdc4e5Seric 			free(s->s_hostsig);
2083bafdc4e5Seric 			s->s_hostsig = p;
2084bafdc4e5Seric 			p += strlen(p);
2085bafdc4e5Seric 			*p++ = ':';
2086bafdc4e5Seric 		}
2087bafdc4e5Seric 		else
2088bafdc4e5Seric 			s->s_hostsig = p;
2089e103b48fSeric 		for (i = 0; i < nmx; i++)
2090e103b48fSeric 		{
2091e103b48fSeric 			if (i != 0)
2092e103b48fSeric 				*p++ = ':';
2093e103b48fSeric 			strcpy(p, mxhosts[i]);
2094e103b48fSeric 			p += strlen(p);
2095e103b48fSeric 		}
2096bafdc4e5Seric 		if (endp != NULL)
2097bafdc4e5Seric 			*endp++ = ':';
2098bafdc4e5Seric 	}
2099e103b48fSeric 	makelower(s->s_hostsig);
2100516782b4Seric 	if (ConfigLevel < 2)
2101516782b4Seric 		_res.options = oldoptions;
2102e103b48fSeric #else
2103e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2104e103b48fSeric 	s->s_hostsig = host;
2105e103b48fSeric #endif
2106e103b48fSeric 	if (tTd(17, 1))
2107e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2108e103b48fSeric 	return s->s_hostsig;
2109e103b48fSeric }
2110