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*0e484fe5Seric static char sccsid[] = "@(#)deliver.c	8.14 (Berkeley) 08/20/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 	{
79*0e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
80*0e484fe5Seric 			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 		{
362*0e484fe5Seric 			/* be sure we leave the temp files to our child */
363*0e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
364*0e484fe5Seric 			if (e->e_lockfp != NULL)
365*0e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
366*0e484fe5Seric 			e->e_lockfp = NULL;
367*0e484fe5Seric 
368*0e484fe5Seric 			/* close any random open files in the envelope */
369*0e484fe5Seric 			closexscript(e);
370*0e484fe5Seric 			if (e->e_dfp != NULL)
371*0e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
372*0e484fe5Seric 			e->e_dfp = NULL;
373*0e484fe5Seric 			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;
8909c9e68d9Seric 
8919c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
8929c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
8939c9e68d9Seric 	{
8949c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
8959c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
8969c9e68d9Seric 		mci->mci_in = stdin;
8979c9e68d9Seric 		mci->mci_out = stdout;
8989c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
8999c9e68d9Seric 		mci->mci_mailer = m;
9009c9e68d9Seric 	}
9019c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9029c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9039c9e68d9Seric 	{
9049c9e68d9Seric #ifdef DAEMON
9059c9e68d9Seric 		register int i;
9069c9e68d9Seric 		register u_short port;
9079c9e68d9Seric 
9089c9e68d9Seric 		CurHostName = pv[1];
9099c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9109c9e68d9Seric 
9119c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9129c9e68d9Seric 		{
9139c9e68d9Seric 			syserr("null signature");
914845e533cSeric 			rcode = EX_OSERR;
915b31e7f2bSeric 			goto give_up;
916b31e7f2bSeric 		}
9179c9e68d9Seric 
9189c9e68d9Seric 		if (!clever)
9199c9e68d9Seric 		{
9209c9e68d9Seric 			syserr("554 non-clever IPC");
9219c9e68d9Seric 			rcode = EX_OSERR;
9229c9e68d9Seric 			goto give_up;
9239c9e68d9Seric 		}
9249c9e68d9Seric 		if (pv[2] != NULL)
9259c9e68d9Seric 			port = atoi(pv[2]);
9269c9e68d9Seric 		else
9279c9e68d9Seric 			port = 0;
9289c9e68d9Seric tryhost:
9299c9e68d9Seric 		mci = NULL;
9309c9e68d9Seric 		while (*curhost != '\0')
9319c9e68d9Seric 		{
9329c9e68d9Seric 			register char *p;
9339c9e68d9Seric 			static char hostbuf[MAXNAME];
9349c9e68d9Seric 
9359c9e68d9Seric 			mci = NULL;
9369c9e68d9Seric 
9379c9e68d9Seric 			/* pull the next host from the signature */
9389c9e68d9Seric 			p = strchr(curhost, ':');
9399c9e68d9Seric 			if (p == NULL)
9409c9e68d9Seric 				p = &curhost[strlen(curhost)];
9419c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9429c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9439c9e68d9Seric 			if (*p != '\0')
9449c9e68d9Seric 				p++;
9459c9e68d9Seric 			curhost = p;
9469c9e68d9Seric 
9479c9e68d9Seric 			/* see if we already know that this host is fried */
9489c9e68d9Seric 			CurHostName = hostbuf;
9499c9e68d9Seric 			mci = mci_get(hostbuf, m);
9509c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9519c9e68d9Seric 			{
9529c9e68d9Seric 				if (tTd(11, 1))
9539c9e68d9Seric 				{
9549c9e68d9Seric 					printf("openmailer: ");
9559c9e68d9Seric 					mci_dump(mci);
9569c9e68d9Seric 				}
9579c9e68d9Seric 				CurHostName = mci->mci_host;
9589c9e68d9Seric 				break;
9599c9e68d9Seric 			}
9609c9e68d9Seric 			mci->mci_mailer = m;
9619c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9629c9e68d9Seric 				continue;
9639c9e68d9Seric 
9649c9e68d9Seric 			/* try the connection */
9659c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9669c9e68d9Seric 			message("Connecting to %s (%s)...",
9679c9e68d9Seric 				hostbuf, m->m_name);
9689c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
9699c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
9709c9e68d9Seric 			mci->mci_exitstat = i;
9719c9e68d9Seric 			mci->mci_errno = errno;
972f170942cSeric #ifdef NAMED_BIND
973f170942cSeric 			mci->mci_herrno = h_errno;
974f170942cSeric #endif
9759c9e68d9Seric 			if (i == EX_OK)
9769c9e68d9Seric 			{
9779c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
9789c9e68d9Seric 				mci_cache(mci);
979f170942cSeric 				if (TrafficLogFile != NULL)
980f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
981f170942cSeric 						getpid(), hostbuf);
9829c9e68d9Seric 				break;
9839c9e68d9Seric 			}
9849c9e68d9Seric 			else if (tTd(11, 1))
9859c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
9869c9e68d9Seric 					i, errno);
9879c9e68d9Seric 
9889c9e68d9Seric 
9899c9e68d9Seric 			/* enter status of this host */
9909c9e68d9Seric 			setstat(i);
9919c9e68d9Seric 		}
9929c9e68d9Seric 		mci->mci_pid = 0;
9939c9e68d9Seric #else /* no DAEMON */
9949c9e68d9Seric 		syserr("554 openmailer: no IPC");
9959c9e68d9Seric 		if (tTd(11, 1))
9969c9e68d9Seric 			printf("openmailer: NULL\n");
9979c9e68d9Seric 		return NULL;
9989c9e68d9Seric #endif /* DAEMON */
9999c9e68d9Seric 	}
10009c9e68d9Seric 	else
10019c9e68d9Seric 	{
1002f170942cSeric #ifdef XDEBUG
1003f170942cSeric 		char wbuf[MAXLINE];
10046fe8c3bcSeric 
10056fe8c3bcSeric 		/* make absolutely certain 0, 1, and 2 are in use */
1006f170942cSeric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
1007f170942cSeric 		checkfd012(wbuf);
1008f170942cSeric #endif
10090e3bfef5Seric 
1010f170942cSeric 		if (TrafficLogFile != NULL)
10110e3bfef5Seric 		{
1012f170942cSeric 			char **av;
1013f170942cSeric 
1014f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1015f170942cSeric 			for (av = pv; *av != NULL; av++)
1016f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1017f170942cSeric 			fprintf(TrafficLogFile, "\n");
10186fe8c3bcSeric 		}
10196fe8c3bcSeric 
10209c9e68d9Seric 		/* create a pipe to shove the mail through */
10219c9e68d9Seric 		if (pipe(mpvect) < 0)
10229c9e68d9Seric 		{
10230e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10240e3bfef5Seric 				e->e_to, m->m_name);
10259c9e68d9Seric 			if (tTd(11, 1))
10269c9e68d9Seric 				printf("openmailer: NULL\n");
10279c9e68d9Seric 			rcode = EX_OSERR;
10289c9e68d9Seric 			goto give_up;
10299c9e68d9Seric 		}
10309c9e68d9Seric 
10319c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10329c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10339c9e68d9Seric 		{
10340e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10350e3bfef5Seric 				e->e_to, m->m_name);
10369c9e68d9Seric 			(void) close(mpvect[0]);
10379c9e68d9Seric 			(void) close(mpvect[1]);
10389c9e68d9Seric 			if (tTd(11, 1))
10399c9e68d9Seric 				printf("openmailer: NULL\n");
10409c9e68d9Seric 			rcode = EX_OSERR;
10419c9e68d9Seric 			goto give_up;
10429c9e68d9Seric 		}
10439c9e68d9Seric 
10449c9e68d9Seric 		/*
10459c9e68d9Seric 		**  Actually fork the mailer process.
10469c9e68d9Seric 		**	DOFORK is clever about retrying.
10479c9e68d9Seric 		**
10489c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10499c9e68d9Seric 		**	around so that endmail will get it.
10509c9e68d9Seric 		*/
10519c9e68d9Seric 
10529c9e68d9Seric 		if (e->e_xfp != NULL)
10539c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10549c9e68d9Seric 		(void) fflush(stdout);
10559c9e68d9Seric # ifdef SIGCHLD
10562b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
10579c9e68d9Seric # endif /* SIGCHLD */
10589c9e68d9Seric 		DOFORK(FORK);
10599c9e68d9Seric 		/* pid is set by DOFORK */
10609c9e68d9Seric 		if (pid < 0)
10619c9e68d9Seric 		{
10629c9e68d9Seric 			/* failure */
10630e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
10640e3bfef5Seric 				e->e_to, m->m_name);
10659c9e68d9Seric 			(void) close(mpvect[0]);
10669c9e68d9Seric 			(void) close(mpvect[1]);
10679c9e68d9Seric 			if (clever)
10689c9e68d9Seric 			{
10699c9e68d9Seric 				(void) close(rpvect[0]);
10709c9e68d9Seric 				(void) close(rpvect[1]);
10719c9e68d9Seric 			}
10729c9e68d9Seric 			if (tTd(11, 1))
10739c9e68d9Seric 				printf("openmailer: NULL\n");
10749c9e68d9Seric 			rcode = EX_OSERR;
10759c9e68d9Seric 			goto give_up;
10769c9e68d9Seric 		}
10779c9e68d9Seric 		else if (pid == 0)
10789c9e68d9Seric 		{
10799c9e68d9Seric 			int i;
10809c9e68d9Seric 			int saveerrno;
10819c9e68d9Seric 			char **ep;
10829c9e68d9Seric 			char *env[MAXUSERENVIRON];
10839c9e68d9Seric 			extern char **environ;
10849c9e68d9Seric 			extern int DtableSize;
10859c9e68d9Seric 
10869c9e68d9Seric 			/* child -- set up input & exec mailer */
10872b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
10882b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
10892b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
10909c9e68d9Seric 
10919c9e68d9Seric 			/* close any other cached connections */
10929c9e68d9Seric 			mci_flush(FALSE, mci);
10939c9e68d9Seric 
109444f2317fSeric 			/* reset user and group */
109544f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
109644f2317fSeric 			{
109744f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
109844f2317fSeric 				{
109944f2317fSeric 					(void) setgid(DefGid);
110044f2317fSeric 					(void) initgroups(DefUser, DefGid);
110144f2317fSeric 					(void) setuid(DefUid);
110244f2317fSeric 				}
110344f2317fSeric 				else
110444f2317fSeric 				{
110544f2317fSeric 					(void) setgid(ctladdr->q_gid);
110644f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
110744f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
110844f2317fSeric 						ctladdr->q_gid);
110944f2317fSeric 					(void) setuid(ctladdr->q_uid);
111044f2317fSeric 				}
111144f2317fSeric 			}
111244f2317fSeric 
111344f2317fSeric 			if (tTd(11, 2))
111444f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
111544f2317fSeric 					getuid(), geteuid());
111644f2317fSeric 
1117b986f6aaSeric 			/* move into some "safe" directory */
1118b986f6aaSeric 			if (m->m_execdir != NULL)
1119b986f6aaSeric 			{
1120b986f6aaSeric 				char *p, *q;
1121b986f6aaSeric 				char buf[MAXLINE];
1122b986f6aaSeric 
1123b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1124b986f6aaSeric 				{
1125b986f6aaSeric 					q = strchr(p, ':');
1126b986f6aaSeric 					if (q != NULL)
1127b986f6aaSeric 						*q = '\0';
1128b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1129b986f6aaSeric 					if (q != NULL)
1130b986f6aaSeric 						*q++ = ':';
1131b986f6aaSeric 					if (tTd(11, 20))
1132b986f6aaSeric 						printf("openmailer: trydir %s\n",
1133b986f6aaSeric 							buf);
1134b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1135b986f6aaSeric 						break;
1136b986f6aaSeric 				}
1137b986f6aaSeric 			}
1138b986f6aaSeric 
11399c9e68d9Seric 			/* arrange to filter std & diag output of command */
11409c9e68d9Seric 			if (clever)
11419c9e68d9Seric 			{
11429c9e68d9Seric 				(void) close(rpvect[0]);
11436fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11446fe8c3bcSeric 				{
11450e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11460e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11476fe8c3bcSeric 					_exit(EX_OSERR);
11486fe8c3bcSeric 				}
11499c9e68d9Seric 				(void) close(rpvect[1]);
11509c9e68d9Seric 			}
11519c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11529c9e68d9Seric 			{
11539c9e68d9Seric 				/* put mailer output in transcript */
11546fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11556fe8c3bcSeric 				{
11560e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11570e3bfef5Seric 						e->e_to, m->m_name,
11586fe8c3bcSeric 						fileno(e->e_xfp));
11596fe8c3bcSeric 					_exit(EX_OSERR);
11609c9e68d9Seric 				}
11616fe8c3bcSeric 			}
11626fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11636fe8c3bcSeric 			{
11640e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
11650e3bfef5Seric 					e->e_to, m->m_name);
11666fe8c3bcSeric 				_exit(EX_OSERR);
11676fe8c3bcSeric 			}
11689c9e68d9Seric 
11699c9e68d9Seric 			/* arrange to get standard input */
11709c9e68d9Seric 			(void) close(mpvect[1]);
11719c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11729c9e68d9Seric 			{
11730e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
11740e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
11759c9e68d9Seric 				_exit(EX_OSERR);
11769c9e68d9Seric 			}
11779c9e68d9Seric 			(void) close(mpvect[0]);
11789c9e68d9Seric 
11799c9e68d9Seric 			/* arrange for all the files to be closed */
11809c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
11819c9e68d9Seric 			{
11829c9e68d9Seric 				register int j;
118344f2317fSeric 
11849c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
11859c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
11869c9e68d9Seric 			}
11879c9e68d9Seric 
11889c9e68d9Seric 			/* set up the mailer environment */
11899c9e68d9Seric 			i = 0;
11909c9e68d9Seric 			env[i++] = "AGENT=sendmail";
11919c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
11929c9e68d9Seric 			{
11939c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
11949c9e68d9Seric 					env[i++] = *ep;
11959c9e68d9Seric 			}
11969c9e68d9Seric 			env[i++] = NULL;
11979c9e68d9Seric 
11989c9e68d9Seric 			/* try to execute the mailer */
11999c9e68d9Seric 			execve(m->m_mailer, pv, env);
12009c9e68d9Seric 			saveerrno = errno;
12019c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12027c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12037c941fd2Seric 				_exit(EX_OSERR);
12049c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12059c9e68d9Seric 		}
12069c9e68d9Seric 
12079c9e68d9Seric 		/*
12089c9e68d9Seric 		**  Set up return value.
12099c9e68d9Seric 		*/
12109c9e68d9Seric 
12119c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12129c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12139c9e68d9Seric 		mci->mci_mailer = m;
12149c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12159c9e68d9Seric 		mci->mci_pid = pid;
12169c9e68d9Seric 		(void) close(mpvect[0]);
12179c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
12189c9e68d9Seric 		if (clever)
12199c9e68d9Seric 		{
12209c9e68d9Seric 			(void) close(rpvect[1]);
12219c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
12229c9e68d9Seric 		}
12239c9e68d9Seric 		else
12249c9e68d9Seric 		{
12259c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12269c9e68d9Seric 			mci->mci_in = NULL;
12279c9e68d9Seric 		}
12289c9e68d9Seric 	}
12299c9e68d9Seric 
12309c9e68d9Seric 	/*
12319c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12329c9e68d9Seric 	*/
12339c9e68d9Seric 
12349c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12359c9e68d9Seric 	{
12369c9e68d9Seric 		smtpinit(m, mci, e);
12379c9e68d9Seric 	}
12389c9e68d9Seric 	if (tTd(11, 1))
12399c9e68d9Seric 	{
12409c9e68d9Seric 		printf("openmailer: ");
12419c9e68d9Seric 		mci_dump(mci);
12429c9e68d9Seric 	}
12439c9e68d9Seric 
12449c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1245b31e7f2bSeric 	{
1246b31e7f2bSeric 		/* couldn't open the mailer */
1247b31e7f2bSeric 		rcode = mci->mci_exitstat;
12482a6bc25bSeric 		errno = mci->mci_errno;
1249f170942cSeric #ifdef NAMED_BIND
1250f170942cSeric 		h_errno = mci->mci_herrno;
1251f170942cSeric #endif
1252b31e7f2bSeric 		if (rcode == EX_OK)
1253b31e7f2bSeric 		{
1254b31e7f2bSeric 			/* shouldn't happen */
125508b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12566b0f339dSeric 				rcode, mci->mci_state, firstsig);
1257b31e7f2bSeric 			rcode = EX_SOFTWARE;
1258b31e7f2bSeric 		}
1259e9277e33Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
126090891494Seric 		{
126190891494Seric 			/* try next MX site */
126290891494Seric 			goto tryhost;
126390891494Seric 		}
1264b31e7f2bSeric 	}
1265b31e7f2bSeric 	else if (!clever)
1266b31e7f2bSeric 	{
1267b31e7f2bSeric 		/*
1268b31e7f2bSeric 		**  Format and send message.
1269b31e7f2bSeric 		*/
127015d084d5Seric 
1271b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1272b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1273b31e7f2bSeric 		putline("\n", mci->mci_out, m);
127403c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1275b31e7f2bSeric 
1276b31e7f2bSeric 		/* get the exit status */
1277c9be6216Seric 		rcode = endmailer(mci, e, pv);
1278134746fbSeric 	}
1279134746fbSeric 	else
1280b31e7f2bSeric #ifdef SMTP
1281134746fbSeric 	{
1282b31e7f2bSeric 		/*
1283b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1284b31e7f2bSeric 		*/
128515d084d5Seric 
1286b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1287b31e7f2bSeric 		if (rcode == EX_OK)
128875889e88Seric 		{
1289ded0d3daSkarels 			register char *t = tobuf;
1290ded0d3daSkarels 			register int i;
1291ded0d3daSkarels 
1292588cad61Seric 			/* send the recipient list */
129363780dbdSeric 			tobuf[0] = '\0';
129475889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
129575889e88Seric 			{
129663780dbdSeric 				e->e_to = to->q_paddr;
129715d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
129875889e88Seric 				{
129983b7ddc9Seric 					markfailure(e, to, i);
130081161401Seric 					giveresponse(i, m, mci, e);
130163780dbdSeric 				}
130275889e88Seric 				else
130375889e88Seric 				{
1304911693bfSbostic 					*t++ = ',';
1305b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1306b31e7f2bSeric 						continue;
1307588cad61Seric 				}
1308588cad61Seric 			}
1309588cad61Seric 
131063780dbdSeric 			/* now send the data */
131163780dbdSeric 			if (tobuf[0] == '\0')
1312b31e7f2bSeric 			{
13139c9e68d9Seric 				rcode = EX_OK;
131463780dbdSeric 				e->e_to = NULL;
1315b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1316b31e7f2bSeric 					smtprset(m, mci, e);
1317b31e7f2bSeric 			}
131875889e88Seric 			else
131975889e88Seric 			{
132063780dbdSeric 				e->e_to = tobuf + 1;
132175889e88Seric 				rcode = smtpdata(m, mci, e);
132263780dbdSeric 			}
132363780dbdSeric 
132463780dbdSeric 			/* now close the connection */
1325b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
132615d084d5Seric 				smtpquit(m, mci, e);
132763780dbdSeric 		}
13289c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13299c9e68d9Seric 		{
13309c9e68d9Seric 			/* try next MX site */
13319c9e68d9Seric 			goto tryhost;
13329c9e68d9Seric 		}
1333c579ef51Seric 	}
1334b31e7f2bSeric #else /* not SMTP */
1335a05b3449Sbostic 	{
133608b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1337845e533cSeric 		rcode = EX_CONFIG;
1338b31e7f2bSeric 		goto give_up;
1339a05b3449Sbostic 	}
1340b31e7f2bSeric #endif /* SMTP */
1341134746fbSeric #ifdef NAMED_BIND
13422bcc6d2dSeric 	if (ConfigLevel < 2)
1343912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1344134746fbSeric #endif
13455dfc646bSeric 
1346b31e7f2bSeric 	/* arrange a return receipt if requested */
13478c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1348b31e7f2bSeric 	{
1349b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1350b31e7f2bSeric 		/* do we want to send back more info? */
1351b31e7f2bSeric 	}
1352b31e7f2bSeric 
1353c77d1c25Seric 	/*
135463780dbdSeric 	**  Do final status disposal.
135563780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1356c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1357c77d1c25Seric 	**		addressees.
1358c77d1c25Seric 	*/
1359c77d1c25Seric 
1360b31e7f2bSeric   give_up:
136163780dbdSeric 	if (tobuf[0] != '\0')
136281161401Seric 		giveresponse(rcode, m, mci, e);
1363772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1364b31e7f2bSeric 	{
1365dde5acadSeric 		if (rcode != EX_OK)
136683b7ddc9Seric 			markfailure(e, to, rcode);
1367dde5acadSeric 		else
1368655518ecSeric 		{
1369dde5acadSeric 			to->q_flags |= QSENT;
1370655518ecSeric 			e->e_nsent++;
1371655518ecSeric 		}
1372b31e7f2bSeric 	}
1373b31e7f2bSeric 
1374b31e7f2bSeric 	/*
1375b31e7f2bSeric 	**  Restore state and return.
1376b31e7f2bSeric 	*/
1377c77d1c25Seric 
137835490626Seric 	errno = 0;
1379588cad61Seric 	define('g', (char *) NULL, e);
13805826d9d3Seric 	return (rcode);
138125a99e2eSeric }
13825dfc646bSeric /*
138383b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
138483b7ddc9Seric **
138583b7ddc9Seric **	Parameters:
138683b7ddc9Seric **		e -- the envelope we are sending.
138783b7ddc9Seric **		q -- the address to mark.
138883b7ddc9Seric **		rcode -- the code signifying the particular failure.
138983b7ddc9Seric **
139083b7ddc9Seric **	Returns:
139183b7ddc9Seric **		none.
139283b7ddc9Seric **
139383b7ddc9Seric **	Side Effects:
139483b7ddc9Seric **		marks the address (and possibly the envelope) with the
139583b7ddc9Seric **			failure so that an error will be returned or
139683b7ddc9Seric **			the message will be queued, as appropriate.
139783b7ddc9Seric */
139883b7ddc9Seric 
139983b7ddc9Seric markfailure(e, q, rcode)
140083b7ddc9Seric 	register ENVELOPE *e;
140183b7ddc9Seric 	register ADDRESS *q;
140283b7ddc9Seric 	int rcode;
140383b7ddc9Seric {
140419c47125Seric 	char buf[MAXLINE];
140519c47125Seric 
140683b7ddc9Seric 	if (rcode == EX_OK)
140783b7ddc9Seric 		return;
1408f170942cSeric 	else if (rcode == EX_TEMPFAIL)
140983b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1410f170942cSeric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
1411f170942cSeric 		q->q_flags |= QBADADDR;
141283b7ddc9Seric }
141383b7ddc9Seric /*
1414c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1415c579ef51Seric **
1416c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1417c579ef51Seric **	violation), so we report those specially.  For other
1418c579ef51Seric **	errors, we choose a status message (into statmsg),
1419c579ef51Seric **	and if it represents an error, we print it.
1420c579ef51Seric **
1421c579ef51Seric **	Parameters:
1422c579ef51Seric **		pid -- pid of mailer.
1423c9be6216Seric **		e -- the current envelope.
1424c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1425c9be6216Seric **			(for error messages).
1426c579ef51Seric **
1427c579ef51Seric **	Returns:
1428c579ef51Seric **		exit code of mailer.
1429c579ef51Seric **
1430c579ef51Seric **	Side Effects:
1431c579ef51Seric **		none.
1432c579ef51Seric */
1433c579ef51Seric 
1434c9be6216Seric endmailer(mci, e, pv)
1435b31e7f2bSeric 	register MCI *mci;
1436c9be6216Seric 	register ENVELOPE *e;
1437c9be6216Seric 	char **pv;
1438c579ef51Seric {
1439588cad61Seric 	int st;
1440c579ef51Seric 
144175889e88Seric 	/* close any connections */
144275889e88Seric 	if (mci->mci_in != NULL)
1443c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
144475889e88Seric 	if (mci->mci_out != NULL)
1445c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
144675889e88Seric 	mci->mci_in = mci->mci_out = NULL;
144775889e88Seric 	mci->mci_state = MCIS_CLOSED;
144875889e88Seric 
144933db8731Seric 	/* in the IPC case there is nothing to wait for */
145075889e88Seric 	if (mci->mci_pid == 0)
145133db8731Seric 		return (EX_OK);
145233db8731Seric 
145333db8731Seric 	/* wait for the mailer process to die and collect status */
145475889e88Seric 	st = waitfor(mci->mci_pid);
1455588cad61Seric 	if (st == -1)
145678de67c1Seric 	{
1457c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1458588cad61Seric 		return (EX_SOFTWARE);
1459c579ef51Seric 	}
146033db8731Seric 
146133db8731Seric 	/* see if it died a horrid death */
1462c579ef51Seric 	if ((st & 0377) != 0)
1463c579ef51Seric 	{
1464c9be6216Seric 		syserr("mailer %s died with signal %o", pv[0], st);
1465c9be6216Seric 
1466c9be6216Seric 		/* log the arguments */
1467c9be6216Seric 		if (e->e_xfp != NULL)
1468c9be6216Seric 		{
1469c9be6216Seric 			register char **av;
1470c9be6216Seric 
1471c9be6216Seric 			fprintf(e->e_xfp, "Arguments:");
1472c9be6216Seric 			for (av = pv; *av != NULL; av++)
1473c9be6216Seric 				fprintf(e->e_xfp, " %s", *av);
1474c9be6216Seric 			fprintf(e->e_xfp, "\n");
1475c9be6216Seric 		}
1476c9be6216Seric 
14775f73204aSeric 		ExitStat = EX_TEMPFAIL;
14785f73204aSeric 		return (EX_TEMPFAIL);
1479c579ef51Seric 	}
148033db8731Seric 
148133db8731Seric 	/* normal death -- return status */
1482588cad61Seric 	st = (st >> 8) & 0377;
1483588cad61Seric 	return (st);
1484c579ef51Seric }
1485c579ef51Seric /*
148625a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
148725a99e2eSeric **
148825a99e2eSeric **	Parameters:
148925a99e2eSeric **		stat -- the status code from the mailer (high byte
149025a99e2eSeric **			only; core dumps must have been taken care of
149125a99e2eSeric **			already).
149281161401Seric **		m -- the mailer info for this mailer.
149381161401Seric **		mci -- the mailer connection info -- can be NULL if the
149481161401Seric **			response is given before the connection is made.
149581161401Seric **		e -- the current envelope.
149625a99e2eSeric **
149725a99e2eSeric **	Returns:
1498db8841e9Seric **		none.
149925a99e2eSeric **
150025a99e2eSeric **	Side Effects:
1501c1f9df2cSeric **		Errors may be incremented.
150225a99e2eSeric **		ExitStat may be set.
150325a99e2eSeric */
150425a99e2eSeric 
150581161401Seric giveresponse(stat, m, mci, e)
150625a99e2eSeric 	int stat;
1507588cad61Seric 	register MAILER *m;
150881161401Seric 	register MCI *mci;
1509198d9be0Seric 	ENVELOPE *e;
151025a99e2eSeric {
15119c16475dSeric 	register const char *statmsg;
151225a99e2eSeric 	extern char *SysExMsg[];
151325a99e2eSeric 	register int i;
1514d4bd8f0eSbostic 	extern int N_SysEx;
1515198d9be0Seric 	char buf[MAXLINE];
151625a99e2eSeric 
151713bbc08cSeric 	/*
151813bbc08cSeric 	**  Compute status message from code.
151913bbc08cSeric 	*/
152013bbc08cSeric 
152125a99e2eSeric 	i = stat - EX__BASE;
1522588cad61Seric 	if (stat == 0)
15236fe8c3bcSeric 	{
1524588cad61Seric 		statmsg = "250 Sent";
1525ce5531bdSeric 		if (e->e_statmsg != NULL)
15266fe8c3bcSeric 		{
1527ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15286fe8c3bcSeric 			statmsg = buf;
15296fe8c3bcSeric 		}
15306fe8c3bcSeric 	}
1531588cad61Seric 	else if (i < 0 || i > N_SysEx)
1532588cad61Seric 	{
1533588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1534588cad61Seric 		stat = EX_UNAVAILABLE;
1535588cad61Seric 		statmsg = buf;
1536588cad61Seric 	}
1537198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1538198d9be0Seric 	{
15397d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1540d4bd8f0eSbostic #ifdef NAMED_BIND
1541f28da541Smiriam 		if (h_errno == TRY_AGAIN)
15424d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1543f28da541Smiriam 		else
1544d4bd8f0eSbostic #endif
1545f28da541Smiriam 		{
15468557d168Seric 			if (errno != 0)
1547d87e85f3Seric 				statmsg = errstring(errno);
1548d87e85f3Seric 			else
1549d87e85f3Seric 			{
1550d87e85f3Seric #ifdef SMTP
1551d87e85f3Seric 				extern char SmtpError[];
1552d87e85f3Seric 
1553d87e85f3Seric 				statmsg = SmtpError;
15546c2c3107Seric #else /* SMTP */
1555d87e85f3Seric 				statmsg = NULL;
15566c2c3107Seric #endif /* SMTP */
1557d87e85f3Seric 			}
1558f28da541Smiriam 		}
1559d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1560d87e85f3Seric 		{
156187c9b3e7Seric 			(void) strcat(buf, ": ");
1562d87e85f3Seric 			(void) strcat(buf, statmsg);
15638557d168Seric 		}
1564198d9be0Seric 		statmsg = buf;
1565198d9be0Seric 	}
1566f170942cSeric #ifdef NAMED_BIND
1567f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1568f170942cSeric 	{
15694d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1570f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1571f170942cSeric 		statmsg = buf;
1572f170942cSeric 	}
1573f170942cSeric #endif
157425a99e2eSeric 	else
1575d87e85f3Seric 	{
157625a99e2eSeric 		statmsg = SysExMsg[i];
15777d55540cSeric 		if (*statmsg++ == ':')
15787d55540cSeric 		{
15797d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
15807d55540cSeric 			statmsg = buf;
15817d55540cSeric 		}
1582d87e85f3Seric 	}
1583588cad61Seric 
1584588cad61Seric 	/*
1585588cad61Seric 	**  Print the message as appropriate
1586588cad61Seric 	*/
1587588cad61Seric 
1588198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1589e12d03eeSeric 		message(&statmsg[4], errstring(errno));
159025a99e2eSeric 	else
159125a99e2eSeric 	{
1592c1f9df2cSeric 		Errors++;
1593e12d03eeSeric 		usrerr(statmsg, errstring(errno));
159425a99e2eSeric 	}
159525a99e2eSeric 
159625a99e2eSeric 	/*
159725a99e2eSeric 	**  Final cleanup.
159825a99e2eSeric 	**	Log a record of the transaction.  Compute the new
159925a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
160025a99e2eSeric 	**	that.
160125a99e2eSeric 	*/
160225a99e2eSeric 
16032f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
160481161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1605eb238f8cSeric 
1606eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1607eb238f8cSeric 		setstat(stat);
1608198d9be0Seric 	if (stat != EX_OK)
1609198d9be0Seric 	{
1610198d9be0Seric 		if (e->e_message != NULL)
1611198d9be0Seric 			free(e->e_message);
1612198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1613198d9be0Seric 	}
16148557d168Seric 	errno = 0;
1615d4bd8f0eSbostic #ifdef NAMED_BIND
1616f28da541Smiriam 	h_errno = 0;
1617d4bd8f0eSbostic #endif
1618eb238f8cSeric }
1619eb238f8cSeric /*
1620eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1621eb238f8cSeric **
1622eb238f8cSeric **	Parameters:
162381161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
162481161401Seric **		mci -- the mailer connection info -- can be NULL if the
162581161401Seric **			log is occuring when no connection is active.
162681161401Seric **		stat -- the message to print for the status.
162781161401Seric **		e -- the current envelope.
1628eb238f8cSeric **
1629eb238f8cSeric **	Returns:
1630eb238f8cSeric **		none
1631eb238f8cSeric **
1632eb238f8cSeric **	Side Effects:
1633eb238f8cSeric **		none
1634eb238f8cSeric */
1635eb238f8cSeric 
163681161401Seric logdelivery(m, mci, stat, e)
163781161401Seric 	MAILER *m;
163881161401Seric 	register MCI *mci;
1639eb238f8cSeric 	char *stat;
1640b31e7f2bSeric 	register ENVELOPE *e;
16415cf56be3Seric {
1642eb238f8cSeric # ifdef LOG
1643d6acf3eeSeric 	char buf[512];
16449507d1f9Seric 
164548ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
164681161401Seric 
164748ed5d33Seric 	if (m != NULL)
164871ff6caaSeric 	{
164948ed5d33Seric 		(void) strcat(buf, ", mailer=");
165048ed5d33Seric 		(void) strcat(buf, m->m_name);
165171ff6caaSeric 	}
165248ed5d33Seric 
165348ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
165471ff6caaSeric 	{
165571ff6caaSeric # ifdef DAEMON
1656e2f2f828Seric 		extern SOCKADDR CurHostAddr;
165748ed5d33Seric # endif
165871ff6caaSeric 
165948ed5d33Seric 		(void) strcat(buf, ", relay=");
166048ed5d33Seric 		(void) strcat(buf, mci->mci_host);
166148ed5d33Seric 
166248ed5d33Seric # ifdef DAEMON
166348ed5d33Seric 		(void) strcat(buf, " (");
1664e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
166548ed5d33Seric 		(void) strcat(buf, ")");
166671ff6caaSeric # endif
166771ff6caaSeric 	}
16689507d1f9Seric 	else
166948ed5d33Seric 	{
167048ed5d33Seric 		char *p = macvalue('h', e);
16719507d1f9Seric 
167248ed5d33Seric 		if (p != NULL && p[0] != '\0')
167348ed5d33Seric 		{
167448ed5d33Seric 			(void) strcat(buf, ", relay=");
167548ed5d33Seric 			(void) strcat(buf, p);
167648ed5d33Seric 		}
167748ed5d33Seric 	}
1678d6acf3eeSeric 
1679d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1680d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
16816c2c3107Seric # endif /* LOG */
168225a99e2eSeric }
168325a99e2eSeric /*
168451552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
168525a99e2eSeric **
168651552439Seric **	This can be made an arbitrary message separator by changing $l
168751552439Seric **
16889b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
16899b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
16909b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
16919b6c17a6Seric **	this kind of antique garbage????
169225a99e2eSeric **
169325a99e2eSeric **	Parameters:
169451552439Seric **		fp -- the file to output to.
169551552439Seric **		m -- the mailer describing this entry.
169625a99e2eSeric **
169725a99e2eSeric **	Returns:
169851552439Seric **		none
169925a99e2eSeric **
170025a99e2eSeric **	Side Effects:
170151552439Seric **		outputs some text to fp.
170225a99e2eSeric */
170325a99e2eSeric 
1704b31e7f2bSeric putfromline(fp, m, e)
170551552439Seric 	register FILE *fp;
170651552439Seric 	register MAILER *m;
1707b31e7f2bSeric 	ENVELOPE *e;
170825a99e2eSeric {
17092bc47524Seric 	char *template = "\201l\n";
171051552439Seric 	char buf[MAXLINE];
171125a99e2eSeric 
171257fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
171351552439Seric 		return;
171413bbc08cSeric 
17152c7e1b8dSeric # ifdef UGLYUUCP
171657fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
171774b6e67bSeric 	{
1718ea09d6edSeric 		char *bang;
1719ea09d6edSeric 		char xbuf[MAXLINE];
172074b6e67bSeric 
1721ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17226c2c3107Seric 		bang = strchr(buf, '!');
172374b6e67bSeric 		if (bang == NULL)
172408b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
172574b6e67bSeric 		else
1726588cad61Seric 		{
1727ea09d6edSeric 			*bang++ = '\0';
17282bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1729ea09d6edSeric 			template = xbuf;
173074b6e67bSeric 		}
1731588cad61Seric 	}
17326c2c3107Seric # endif /* UGLYUUCP */
1733b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
173477b52738Seric 	putline(buf, fp, m);
1735bc6e2962Seric }
1736bc6e2962Seric /*
173751552439Seric **  PUTBODY -- put the body of a message.
173851552439Seric **
173951552439Seric **	Parameters:
174051552439Seric **		fp -- file to output onto.
174177b52738Seric **		m -- a mailer descriptor to control output format.
17429a6a5f55Seric **		e -- the envelope to put out.
174303c02fdeSeric **		separator -- if non-NULL, a message separator that must
174403c02fdeSeric **			not be permitted in the resulting message.
174551552439Seric **
174651552439Seric **	Returns:
174751552439Seric **		none.
174851552439Seric **
174951552439Seric **	Side Effects:
175051552439Seric **		The message is written onto fp.
175151552439Seric */
175251552439Seric 
175303c02fdeSeric putbody(fp, m, e, separator)
175451552439Seric 	FILE *fp;
1755588cad61Seric 	MAILER *m;
17569a6a5f55Seric 	register ENVELOPE *e;
175703c02fdeSeric 	char *separator;
175851552439Seric {
175977b52738Seric 	char buf[MAXLINE];
176051552439Seric 
176151552439Seric 	/*
176251552439Seric 	**  Output the body of the message
176351552439Seric 	*/
176451552439Seric 
17659a6a5f55Seric 	if (e->e_dfp == NULL)
176651552439Seric 	{
17679a6a5f55Seric 		if (e->e_df != NULL)
17689a6a5f55Seric 		{
17699a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
17709a6a5f55Seric 			if (e->e_dfp == NULL)
17718f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
1772e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
17739a6a5f55Seric 		}
17749a6a5f55Seric 		else
177577b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
17769a6a5f55Seric 	}
17779a6a5f55Seric 	if (e->e_dfp != NULL)
17789a6a5f55Seric 	{
17799a6a5f55Seric 		rewind(e->e_dfp);
178077b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
178124fc8aeeSeric 		{
178224fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1783d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
17843462ad9eSeric 				(void) putc('>', fp);
178503c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
178603c02fdeSeric 			{
178703c02fdeSeric 				/* possible separator */
178803c02fdeSeric 				int sl = strlen(separator);
178903c02fdeSeric 
179003c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
179103c02fdeSeric 					(void) putc(' ', fp);
179203c02fdeSeric 			}
179377b52738Seric 			putline(buf, fp, m);
179424fc8aeeSeric 		}
179551552439Seric 
17969a6a5f55Seric 		if (ferror(e->e_dfp))
179751552439Seric 		{
179851552439Seric 			syserr("putbody: read error");
179951552439Seric 			ExitStat = EX_IOERR;
180051552439Seric 		}
180151552439Seric 	}
180251552439Seric 
18030890ba1fSeric 	/* some mailers want extra blank line at end of message */
18040890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
18050890ba1fSeric 		putline("", fp, m);
18060890ba1fSeric 
180751552439Seric 	(void) fflush(fp);
180851552439Seric 	if (ferror(fp) && errno != EPIPE)
180951552439Seric 	{
181051552439Seric 		syserr("putbody: write error");
181151552439Seric 		ExitStat = EX_IOERR;
181251552439Seric 	}
181351552439Seric 	errno = 0;
181425a99e2eSeric }
181525a99e2eSeric /*
181625a99e2eSeric **  MAILFILE -- Send a message to a file.
181725a99e2eSeric **
1818f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1819f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1820f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1821f129ec7dSeric **	sendmail runs as root.
1822f129ec7dSeric **
1823588cad61Seric **	This could be done as a subordinate mailer, except that it
1824588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1825588cad61Seric **	view this as being sufficiently important as to include it
1826588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1827588cad61Seric **	to create another process plus some pipes to save the message.
1828588cad61Seric **
182925a99e2eSeric **	Parameters:
183025a99e2eSeric **		filename -- the name of the file to send to.
18316259796dSeric **		ctladdr -- the controlling address header -- includes
18326259796dSeric **			the userid/groupid to be when sending.
183325a99e2eSeric **
183425a99e2eSeric **	Returns:
183525a99e2eSeric **		The exit code associated with the operation.
183625a99e2eSeric **
183725a99e2eSeric **	Side Effects:
183825a99e2eSeric **		none.
183925a99e2eSeric */
184025a99e2eSeric 
1841b31e7f2bSeric mailfile(filename, ctladdr, e)
184225a99e2eSeric 	char *filename;
18436259796dSeric 	ADDRESS *ctladdr;
1844b31e7f2bSeric 	register ENVELOPE *e;
184525a99e2eSeric {
184625a99e2eSeric 	register FILE *f;
184732d19d43Seric 	register int pid;
184815d084d5Seric 	int mode;
184925a99e2eSeric 
1850671745f3Seric 	if (tTd(11, 1))
1851671745f3Seric 	{
1852671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
1853671745f3Seric 		printaddr(ctladdr, FALSE);
1854671745f3Seric 	}
1855671745f3Seric 
1856f170942cSeric 	if (e->e_xfp != NULL)
1857f170942cSeric 		fflush(e->e_xfp);
1858f170942cSeric 
185932d19d43Seric 	/*
186032d19d43Seric 	**  Fork so we can change permissions here.
186132d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
186232d19d43Seric 	**	the complications of calling subroutines, etc.
186332d19d43Seric 	*/
186432d19d43Seric 
186532d19d43Seric 	DOFORK(fork);
186632d19d43Seric 
186732d19d43Seric 	if (pid < 0)
186832d19d43Seric 		return (EX_OSERR);
186932d19d43Seric 	else if (pid == 0)
187032d19d43Seric 	{
187132d19d43Seric 		/* child -- actually write to file */
1872f129ec7dSeric 		struct stat stb;
1873f129ec7dSeric 
18742b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
18752b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
18762b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
18773462ad9eSeric 		(void) umask(OldUmask);
187895f16dc0Seric 
1879f129ec7dSeric 		if (stat(filename, &stb) < 0)
18803a98e7eaSeric 			stb.st_mode = FileMode;
188115d084d5Seric 		mode = stb.st_mode;
188295f16dc0Seric 
188395f16dc0Seric 		/* limit the errors to those actually caused in the child */
188495f16dc0Seric 		errno = 0;
188595f16dc0Seric 		ExitStat = EX_OK;
188695f16dc0Seric 
1887f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1888f129ec7dSeric 			exit(EX_CANTCREAT);
188903827b5fSeric 		if (ctladdr == NULL)
18908f9146b0Srick 			ctladdr = &e->e_from;
189115d084d5Seric 		else
189215d084d5Seric 		{
189315d084d5Seric 			/* ignore setuid and setgid bits */
189415d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
189515d084d5Seric 		}
189615d084d5Seric 
18978f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
18988f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
18998f9146b0Srick 		{
19008f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
190195f16dc0Seric 			if (e->e_dfp == NULL)
190295f16dc0Seric 			{
19038f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
1904e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
19058f9146b0Srick 			}
19068f9146b0Srick 		}
19078f9146b0Srick 
190815d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1909e36b99e2Seric 		{
191095f16dc0Seric 			if (ctladdr->q_uid == 0)
191195f16dc0Seric 			{
1912e36b99e2Seric 				(void) setgid(DefGid);
1913898a126bSbostic 				(void) initgroups(DefUser, DefGid);
191495f16dc0Seric 			}
191595f16dc0Seric 			else
191695f16dc0Seric 			{
19176259796dSeric 				(void) setgid(ctladdr->q_gid);
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