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*4dee0003Seric static char sccsid[] = "@(#)deliver.c	8.37 (Berkeley) 10/29/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;
974bb8b0fdSeric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
989c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
994bb8b0fdSeric 			RealHostName, 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_df = NULL;
214ce5531bdSeric 			ee->e_errormode = EM_MAIL;
215ce5531bdSeric 			ee->e_sibling = splitenv;
216ce5531bdSeric 			splitenv = ee;
217ce5531bdSeric 
218ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
219ce5531bdSeric 				if (q->q_owner == owner)
220ce5531bdSeric 					q->q_flags |= QDONTSEND;
221ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
222ce5531bdSeric 				if (q->q_owner != owner)
223ce5531bdSeric 					q->q_flags |= QDONTSEND;
224ce5531bdSeric 
225ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
226ce5531bdSeric 			{
227ce5531bdSeric 				ee->e_dfp = NULL;
228da662164Seric 				ee->e_df = queuename(ee, 'd');
229da662164Seric 				ee->e_df = newstr(ee->e_df);
230ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
231ce5531bdSeric 				{
232ce5531bdSeric 					syserr("sendall: link(%s, %s)",
233ce5531bdSeric 						e->e_df, ee->e_df);
234ce5531bdSeric 				}
235ce5531bdSeric 			}
236ce5531bdSeric 
237ce5531bdSeric 			if (mode != SM_VERIFY)
238ce5531bdSeric 				openxscript(ee);
239ce5531bdSeric #ifdef LOG
240ce5531bdSeric 			if (LogLevel > 4)
241ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
242ce5531bdSeric 					ee->e_id, e->e_id);
243ce5531bdSeric #endif
244ce5531bdSeric 		}
245ce5531bdSeric 	}
246ce5531bdSeric 
247ce5531bdSeric 	if (owner != NULL)
248ce5531bdSeric 	{
249ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
250ce5531bdSeric 		if (tTd(13, 5))
251ce5531bdSeric 		{
252ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
253ce5531bdSeric 			printaddr(&e->e_from, FALSE);
254ce5531bdSeric 		}
255ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
256ce5531bdSeric 		e->e_errormode = EM_MAIL;
257ce5531bdSeric 	}
258ce5531bdSeric 
259c1ac89b1Seric # ifdef QUEUE
260c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
261c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
262c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
263c1ac89b1Seric 	{
264c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2656103d9b4Seric 		queueup(e, TRUE, announcequeueup);
266ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2676103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
268c1ac89b1Seric 	}
269c1ac89b1Seric #endif /* QUEUE */
270c1ac89b1Seric 
271ce5531bdSeric 	if (splitenv != NULL)
272ce5531bdSeric 	{
273ce5531bdSeric 		if (tTd(13, 1))
274ce5531bdSeric 		{
275ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
276ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
277ce5531bdSeric 		}
278ce5531bdSeric 
279ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
280ce5531bdSeric 		{
281ce5531bdSeric 			CurEnv = ee;
282ce5531bdSeric 			sendenvelope(ee, mode);
283ce5531bdSeric 		}
284ce5531bdSeric 
285ce5531bdSeric 		CurEnv = e;
286ce5531bdSeric 	}
287ce5531bdSeric 	sendenvelope(e, mode);
288ce5531bdSeric 
289ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
290ce5531bdSeric 		dropenvelope(splitenv);
291ce5531bdSeric }
292ce5531bdSeric 
293ce5531bdSeric sendenvelope(e, mode)
294ce5531bdSeric 	register ENVELOPE *e;
295ce5531bdSeric 	char mode;
296ce5531bdSeric {
297ce5531bdSeric 	bool oldverbose;
298ce5531bdSeric 	int pid;
299ce5531bdSeric 	register ADDRESS *q;
3006b2765c6Seric 	char *qf;
3016b2765c6Seric 	char *id;
302ce5531bdSeric 
3037880f3e4Seric 	/*
3047880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3057880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3067880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3077880f3e4Seric 	**  addresses to be sent.
3087880f3e4Seric 	*/
3097880f3e4Seric 
3107880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
3117880f3e4Seric 	{
3127880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3137880f3e4Seric 		return;
3147880f3e4Seric 	}
3157880f3e4Seric 
316ce5531bdSeric 	oldverbose = Verbose;
317c1ac89b1Seric 	switch (mode)
318c1ac89b1Seric 	{
319c1ac89b1Seric 	  case SM_VERIFY:
320c1ac89b1Seric 		Verbose = TRUE;
321c1ac89b1Seric 		break;
322c1ac89b1Seric 
323c1ac89b1Seric 	  case SM_QUEUE:
324c1ac89b1Seric   queueonly:
325c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
326c1ac89b1Seric 		return;
327c1ac89b1Seric 
328c1ac89b1Seric 	  case SM_FORK:
329c1ac89b1Seric 		if (e->e_xfp != NULL)
330c1ac89b1Seric 			(void) fflush(e->e_xfp);
331c1ac89b1Seric 
3322b9178d3Seric # ifndef HASFLOCK
333c1ac89b1Seric 		/*
3346b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3356b2765c6Seric 		**  the lock is owned by a process, not by an open file
3366b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3376b2765c6Seric 		**  then restart from scratch in the child.
338c1ac89b1Seric 		*/
339c1ac89b1Seric 
3406b2765c6Seric 		/* save id for future use */
3416b2765c6Seric 		id = e->e_id;
3426b2765c6Seric 
3436b2765c6Seric 		/* now drop the envelope in the parent */
3446b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3456b2765c6Seric 		dropenvelope(e);
3466b2765c6Seric 
3476b2765c6Seric 		/* and reacquire in the child */
3486b2765c6Seric 		(void) dowork(id, TRUE, FALSE, e);
3496b2765c6Seric 
3506b2765c6Seric 		return;
3516b2765c6Seric 
3526b2765c6Seric # else /* HASFLOCK */
353c1ac89b1Seric 
354c1ac89b1Seric 		pid = fork();
355c1ac89b1Seric 		if (pid < 0)
356c1ac89b1Seric 		{
357c1ac89b1Seric 			goto queueonly;
358c1ac89b1Seric 		}
359c1ac89b1Seric 		else if (pid > 0)
360c1ac89b1Seric 		{
3610e484fe5Seric 			/* be sure we leave the temp files to our child */
3620e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3630e484fe5Seric 			if (e->e_lockfp != NULL)
3640e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3650e484fe5Seric 			e->e_lockfp = NULL;
3660e484fe5Seric 
3670e484fe5Seric 			/* close any random open files in the envelope */
3680e484fe5Seric 			closexscript(e);
3690e484fe5Seric 			if (e->e_dfp != NULL)
3700e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3710e484fe5Seric 			e->e_dfp = NULL;
3720e484fe5Seric 			e->e_id = e->e_df = NULL;
373c1ac89b1Seric 			return;
374c1ac89b1Seric 		}
375c1ac89b1Seric 
376c1ac89b1Seric 		/* double fork to avoid zombies */
377c1ac89b1Seric 		if (fork() > 0)
378c1ac89b1Seric 			exit(EX_OK);
379c1ac89b1Seric 
380c1ac89b1Seric 		/* be sure we are immune from the terminal */
3817880f3e4Seric 		disconnect(1, e);
382c1ac89b1Seric 
383c1ac89b1Seric 		/*
384c1ac89b1Seric 		**  Close any cached connections.
385c1ac89b1Seric 		**
386c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
387c1ac89b1Seric 		**	still knows about the connection.
388c1ac89b1Seric 		**
389c1ac89b1Seric 		**	This should only happen when delivering an error
390c1ac89b1Seric 		**	message.
391c1ac89b1Seric 		*/
392c1ac89b1Seric 
393c1ac89b1Seric 		mci_flush(FALSE, NULL);
394c1ac89b1Seric 
3956b2765c6Seric # endif /* HASFLOCK */
3966b2765c6Seric 
397c1ac89b1Seric 		break;
398c1ac89b1Seric 	}
399c1ac89b1Seric 
400c1ac89b1Seric 	/*
4019c9e68d9Seric 	**  Run through the list and send everything.
4025288e21aSeric 	**
4035288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4045288e21aSeric 	**	result in returned mail.
4059c9e68d9Seric 	*/
4069c9e68d9Seric 
4079c9e68d9Seric 	e->e_nsent = 0;
4085288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
409faad2b16Seric 
410faad2b16Seric 	/* now run through the queue */
4119c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4129c9e68d9Seric 	{
413fdc75a0fSeric #ifdef XDEBUG
414fdc75a0fSeric 		char wbuf[MAXNAME + 20];
415fdc75a0fSeric 
416fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
417fdc75a0fSeric 		checkfd012(wbuf);
418fdc75a0fSeric #endif
4199c9e68d9Seric 		if (mode == SM_VERIFY)
4209c9e68d9Seric 		{
4219c9e68d9Seric 			e->e_to = q->q_paddr;
4229c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4238dfca105Seric 			{
4248dfca105Seric 				message("deliverable: mailer %s, host %s, user %s",
4258dfca105Seric 					q->q_mailer->m_name,
4268dfca105Seric 					q->q_host,
4278dfca105Seric 					q->q_user);
4288dfca105Seric 			}
4299c9e68d9Seric 		}
4309c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4319c9e68d9Seric 		{
4329c9e68d9Seric # ifdef QUEUE
4339c9e68d9Seric 			/*
4349c9e68d9Seric 			**  Checkpoint the send list every few addresses
4359c9e68d9Seric 			*/
4369c9e68d9Seric 
4379c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4389c9e68d9Seric 			{
4399c9e68d9Seric 				queueup(e, TRUE, FALSE);
4409c9e68d9Seric 				e->e_nsent = 0;
4419c9e68d9Seric 			}
4429c9e68d9Seric # endif /* QUEUE */
4439c9e68d9Seric 			(void) deliver(e, q);
4449c9e68d9Seric 		}
4459c9e68d9Seric 	}
4469c9e68d9Seric 	Verbose = oldverbose;
4479c9e68d9Seric 
448fdc75a0fSeric #ifdef XDEBUG
449fdc75a0fSeric 	checkfd012("end of sendenvelope");
450fdc75a0fSeric #endif
451fdc75a0fSeric 
4529c9e68d9Seric 	if (mode == SM_FORK)
4539c9e68d9Seric 		finis();
4549c9e68d9Seric }
4559c9e68d9Seric /*
4569c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4579c9e68d9Seric **
4589c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4599c9e68d9Seric **	two processes on the same stack!!!
4609c9e68d9Seric **
4619c9e68d9Seric **	Parameters:
4629c9e68d9Seric **		none.
4639c9e68d9Seric **
4649c9e68d9Seric **	Returns:
4659c9e68d9Seric **		From a macro???  You've got to be kidding!
4669c9e68d9Seric **
4679c9e68d9Seric **	Side Effects:
4689c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4699c9e68d9Seric **			pid of child in parent, zero in child.
4709c9e68d9Seric **			-1 on unrecoverable error.
4719c9e68d9Seric **
4729c9e68d9Seric **	Notes:
4739c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4749c9e68d9Seric **		vfork for you.....
4759c9e68d9Seric */
4769c9e68d9Seric 
4779c9e68d9Seric # define NFORKTRIES	5
4789c9e68d9Seric 
4799c9e68d9Seric # ifndef FORK
4809c9e68d9Seric # define FORK	fork
4819c9e68d9Seric # endif
4829c9e68d9Seric 
4839c9e68d9Seric # define DOFORK(fORKfN) \
4849c9e68d9Seric {\
4859c9e68d9Seric 	register int i;\
4869c9e68d9Seric \
4879c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4889c9e68d9Seric 	{\
4899c9e68d9Seric 		pid = fORKfN();\
4909c9e68d9Seric 		if (pid >= 0)\
4919c9e68d9Seric 			break;\
4929c9e68d9Seric 		if (i > 0)\
4939c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
4949c9e68d9Seric 	}\
4959c9e68d9Seric }
4969c9e68d9Seric /*
4979c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
4989c9e68d9Seric **
4999c9e68d9Seric **	Parameters:
5009c9e68d9Seric **		none.
5019c9e68d9Seric **
5029c9e68d9Seric **	Returns:
5039c9e68d9Seric **		pid of child in parent.
5049c9e68d9Seric **		zero in child.
5059c9e68d9Seric **		-1 on error.
5069c9e68d9Seric **
5079c9e68d9Seric **	Side Effects:
5089c9e68d9Seric **		returns twice, once in parent and once in child.
5099c9e68d9Seric */
5109c9e68d9Seric 
5119c9e68d9Seric dofork()
5129c9e68d9Seric {
5139c9e68d9Seric 	register int pid;
5149c9e68d9Seric 
5159c9e68d9Seric 	DOFORK(fork);
5169c9e68d9Seric 	return (pid);
5179c9e68d9Seric }
5189c9e68d9Seric /*
51913bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
52013bbc08cSeric **
52113bbc08cSeric **	This routine delivers to everyone on the same host as the
52213bbc08cSeric **	user on the head of the list.  It is clever about mailers
52313bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
52413bbc08cSeric **	that it will deliver to all these addresses however -- so
52513bbc08cSeric **	deliver should be called once for each address on the
52613bbc08cSeric **	list.
52725a99e2eSeric **
52825a99e2eSeric **	Parameters:
529588cad61Seric **		e -- the envelope to deliver.
530c77d1c25Seric **		firstto -- head of the address list to deliver to.
53125a99e2eSeric **
53225a99e2eSeric **	Returns:
53325a99e2eSeric **		zero -- successfully delivered.
53425a99e2eSeric **		else -- some failure, see ExitStat for more info.
53525a99e2eSeric **
53625a99e2eSeric **	Side Effects:
53725a99e2eSeric **		The standard input is passed off to someone.
53825a99e2eSeric */
53925a99e2eSeric 
540588cad61Seric deliver(e, firstto)
541588cad61Seric 	register ENVELOPE *e;
542c77d1c25Seric 	ADDRESS *firstto;
54325a99e2eSeric {
54478442df3Seric 	char *host;			/* host being sent to */
54578442df3Seric 	char *user;			/* user being sent to */
54625a99e2eSeric 	char **pvp;
5475dfc646bSeric 	register char **mvp;
54825a99e2eSeric 	register char *p;
549588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5506259796dSeric 	ADDRESS *ctladdr;
551b31e7f2bSeric 	register MCI *mci;
552c77d1c25Seric 	register ADDRESS *to = firstto;
553c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
554772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
555911693bfSbostic 	int rcode;			/* response code */
556e103b48fSeric 	char *firstsig;			/* signature of firstto */
5579c9e68d9Seric 	int pid;
5589c9e68d9Seric 	char *curhost;
5599c9e68d9Seric 	int mpvect[2];
5609c9e68d9Seric 	int rpvect[2];
561ee6bf8dfSeric 	char *pv[MAXPV+1];
562579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
563ee6bf8dfSeric 	char buf[MAXNAME];
564c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
565fabb3bd4Seric 	extern int checkcompat();
566df106f0bSeric 	extern char SmtpError[];
56725a99e2eSeric 
56835490626Seric 	errno = 0;
569ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5705dfc646bSeric 		return (0);
57125a99e2eSeric 
572134746fbSeric #ifdef NAMED_BIND
573912a731aSbostic 	/* unless interactive, try twice, over a minute */
574912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
575912a731aSbostic 		_res.retrans = 30;
576912a731aSbostic 		_res.retry = 2;
577912a731aSbostic 	}
578d4bd8f0eSbostic #endif
579912a731aSbostic 
58051552439Seric 	m = to->q_mailer;
58151552439Seric 	host = to->q_host;
582c9be6216Seric 	CurEnv = e;			/* just in case */
5834384d521Seric 	e->e_statmsg = NULL;
584df106f0bSeric 	SmtpError[0] = '\0';
58551552439Seric 
5866ef48975Seric 	if (tTd(10, 1))
5875dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
58851552439Seric 			m->m_mno, host, to->q_user);
589ab81ee53Seric 	if (tTd(10, 100))
590ab81ee53Seric 		printopenfds(FALSE);
591f3dbc832Seric 
592f3dbc832Seric 	/*
593f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
594f3dbc832Seric 	**  connections now, just mark these addresses and return.
595f3dbc832Seric 	**	This is useful if we want to batch connections to
596f3dbc832Seric 	**	reduce load.  This will cause the messages to be
597f3dbc832Seric 	**	queued up, and a daemon will come along to send the
598f3dbc832Seric 	**	messages later.
599f3dbc832Seric 	**		This should be on a per-mailer basis.
600f3dbc832Seric 	*/
601f3dbc832Seric 
60279aa5155Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
603f3dbc832Seric 	{
604f3dbc832Seric 		for (; to != NULL; to = to->q_next)
605f4560e80Seric 		{
606ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
607c6e18ac5Seric 			    to->q_mailer != m)
608f4560e80Seric 				continue;
609268a25e1Seric 			to->q_flags |= QQUEUEUP;
610588cad61Seric 			e->e_to = to->q_paddr;
61108b25121Seric 			message("queued");
6122f624c86Seric 			if (LogLevel > 8)
613*4dee0003Seric 				logdelivery(m, NULL, "queued", NULL, e);
614f4560e80Seric 		}
615588cad61Seric 		e->e_to = NULL;
616f3dbc832Seric 		return (0);
617f3dbc832Seric 	}
618f3dbc832Seric 
61925a99e2eSeric 	/*
6205dfc646bSeric 	**  Do initial argv setup.
6215dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6225dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6235dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6245dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6255dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6263bea8136Seric 	**		The from address rewrite is expected to make
6273bea8136Seric 	**		the address relative to the other end.
6285dfc646bSeric 	*/
6295dfc646bSeric 
63078442df3Seric 	/* rewrite from address, using rewriting rules */
631efe54562Seric 	rcode = EX_OK;
632efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
633efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
634efe54562Seric 					   &rcode, e));
635ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
636588cad61Seric 	define('h', host, e);			/* to host */
6375dfc646bSeric 	Errors = 0;
6385dfc646bSeric 	pvp = pv;
6395dfc646bSeric 	*pvp++ = m->m_argv[0];
6405dfc646bSeric 
6415dfc646bSeric 	/* insert -f or -r flag as appropriate */
64257fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6435dfc646bSeric 	{
64457fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6455dfc646bSeric 			*pvp++ = "-f";
6465dfc646bSeric 		else
6475dfc646bSeric 			*pvp++ = "-r";
648c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6495dfc646bSeric 	}
6505dfc646bSeric 
6515dfc646bSeric 	/*
6525dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6535dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6545dfc646bSeric 	**  be one of these, and there are only a few more slots
6555dfc646bSeric 	**  in the pv after it.
6565dfc646bSeric 	*/
6575dfc646bSeric 
6585dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6595dfc646bSeric 	{
6602bc47524Seric 		/* can't use strchr here because of sign extension problems */
6612bc47524Seric 		while (*p != '\0')
6622bc47524Seric 		{
6632bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6642bc47524Seric 			{
6652bc47524Seric 				if (*p == 'u')
6665dfc646bSeric 					break;
6672bc47524Seric 			}
6682bc47524Seric 		}
6692bc47524Seric 
6702bc47524Seric 		if (*p != '\0')
6715dfc646bSeric 			break;
6725dfc646bSeric 
6735dfc646bSeric 		/* this entry is safe -- go ahead and process it */
674588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6755dfc646bSeric 		*pvp++ = newstr(buf);
6765dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6775dfc646bSeric 		{
67808b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6795dfc646bSeric 			return (-1);
6805dfc646bSeric 		}
6815dfc646bSeric 	}
682c579ef51Seric 
68333db8731Seric 	/*
68433db8731Seric 	**  If we have no substitution for the user name in the argument
68533db8731Seric 	**  list, we know that we must supply the names otherwise -- and
68633db8731Seric 	**  SMTP is the answer!!
68733db8731Seric 	*/
68833db8731Seric 
6895dfc646bSeric 	if (*mvp == NULL)
690c579ef51Seric 	{
691c579ef51Seric 		/* running SMTP */
6922c7e1b8dSeric # ifdef SMTP
693c579ef51Seric 		clever = TRUE;
694c579ef51Seric 		*pvp = NULL;
6956c2c3107Seric # else /* SMTP */
69633db8731Seric 		/* oops!  we don't implement SMTP */
697df106f0bSeric 		syserr("554 SMTP style mailer not implemented");
6982c7e1b8dSeric 		return (EX_SOFTWARE);
6996c2c3107Seric # endif /* SMTP */
700c579ef51Seric 	}
7015dfc646bSeric 
7025dfc646bSeric 	/*
7035dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7045dfc646bSeric 	**  run through our address list and append all the addresses
7055dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7065dfc646bSeric 	**  always send another copy later.
7075dfc646bSeric 	*/
7085dfc646bSeric 
7095dfc646bSeric 	tobuf[0] = '\0';
710588cad61Seric 	e->e_to = tobuf;
7116259796dSeric 	ctladdr = NULL;
712e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7135dfc646bSeric 	for (; to != NULL; to = to->q_next)
7145dfc646bSeric 	{
7155dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
71657fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7175dfc646bSeric 			break;
7185dfc646bSeric 
7195dfc646bSeric 		/* if already sent or not for this host, don't send */
720ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
721e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
722e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7235dfc646bSeric 			continue;
7246259796dSeric 
7254b22ea87Seric 		/* avoid overflowing tobuf */
726aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7274b22ea87Seric 			break;
7284b22ea87Seric 
7296ef48975Seric 		if (tTd(10, 1))
730772e6e50Seric 		{
731772e6e50Seric 			printf("\nsend to ");
732772e6e50Seric 			printaddr(to, FALSE);
733772e6e50Seric 		}
734772e6e50Seric 
7356259796dSeric 		/* compute effective uid/gid when sending */
7367da1035fSeric 		if (to->q_mailer == ProgMailer)
7376259796dSeric 			ctladdr = getctladdr(to);
7386259796dSeric 
7395dfc646bSeric 		user = to->q_user;
740588cad61Seric 		e->e_to = to->q_paddr;
74175f1ade9Seric 		if (tTd(10, 5))
74275f1ade9Seric 		{
74375f1ade9Seric 			printf("deliver: QDONTSEND ");
74475f1ade9Seric 			printaddr(to, FALSE);
74575f1ade9Seric 		}
746ee4b0922Seric 		to->q_flags |= QDONTSEND;
7475dfc646bSeric 
7485dfc646bSeric 		/*
7495dfc646bSeric 		**  Check to see that these people are allowed to
7505dfc646bSeric 		**  talk to each other.
7512a6e0786Seric 		*/
7522a6e0786Seric 
75369582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
75469582d2fSeric 		{
75569582d2fSeric 			NoReturn = TRUE;
75608b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
757*4dee0003Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e);
75869582d2fSeric 			continue;
75969582d2fSeric 		}
760fabb3bd4Seric 		rcode = checkcompat(to, e);
7611793c9c5Seric 		if (rcode != EX_OK)
7625dfc646bSeric 		{
7631000c37aSeric 			markfailure(e, to, rcode);
764*4dee0003Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
7655dfc646bSeric 			continue;
7665dfc646bSeric 		}
7672a6e0786Seric 
7682a6e0786Seric 		/*
7699ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7709ec9501bSeric 		**	about them.
77125a99e2eSeric 		*/
77225a99e2eSeric 
77357fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
77425a99e2eSeric 		{
7751d8f1806Seric 			stripquotes(user);
7761d8f1806Seric 			stripquotes(host);
77725a99e2eSeric 		}
77825a99e2eSeric 
779cdb828c5Seric 		/* hack attack -- delivermail compatibility */
780cdb828c5Seric 		if (m == ProgMailer && *user == '|')
781cdb828c5Seric 			user++;
782cdb828c5Seric 
78325a99e2eSeric 		/*
7843efaed6eSeric 		**  If an error message has already been given, don't
7853efaed6eSeric 		**	bother to send to this address.
7863efaed6eSeric 		**
7873efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7883efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
7893efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
7903efaed6eSeric 		*/
7913efaed6eSeric 
7926cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7933efaed6eSeric 			continue;
7943efaed6eSeric 
795f2fec898Seric 		/* save statistics.... */
796588cad61Seric 		markstats(e, to);
797f2fec898Seric 
7983efaed6eSeric 		/*
79925a99e2eSeric 		**  See if this user name is "special".
80025a99e2eSeric 		**	If the user name has a slash in it, assume that this
80151552439Seric 		**	is a file -- send it off without further ado.  Note
80251552439Seric 		**	that this type of addresses is not processed along
80351552439Seric 		**	with the others, so we fudge on the To person.
80425a99e2eSeric 		*/
80525a99e2eSeric 
8062c017f8dSeric 		if (m == FileMailer)
80725a99e2eSeric 		{
808*4dee0003Seric 			ADDRESS *caddr = getctladdr(to);
809*4dee0003Seric 
810*4dee0003Seric 			rcode = mailfile(user, caddr, e);
811*4dee0003Seric 			giveresponse(rcode, m, NULL, caddr, e);
812dde5acadSeric 			if (rcode == EX_OK)
813dde5acadSeric 				to->q_flags |= QSENT;
8145dfc646bSeric 			continue;
81525a99e2eSeric 		}
81625a99e2eSeric 
81713bbc08cSeric 		/*
81813bbc08cSeric 		**  Address is verified -- add this user to mailer
81913bbc08cSeric 		**  argv, and add it to the print list of recipients.
82013bbc08cSeric 		*/
82113bbc08cSeric 
822508daeccSeric 		/* link together the chain of recipients */
823508daeccSeric 		to->q_tchain = tochain;
824508daeccSeric 		tochain = to;
825508daeccSeric 
8265dfc646bSeric 		/* create list of users for error messages */
827db8841e9Seric 		(void) strcat(tobuf, ",");
828db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
829588cad61Seric 		define('u', user, e);		/* to user */
830b6f89e6eSeric 		p = to->q_home;
831b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
832b6f89e6eSeric 			p = ctladdr->q_home;
833b6f89e6eSeric 		define('z', p, e);	/* user's home */
8345dfc646bSeric 
835c579ef51Seric 		/*
836508daeccSeric 		**  Expand out this user into argument list.
837c579ef51Seric 		*/
838c579ef51Seric 
839508daeccSeric 		if (!clever)
840c579ef51Seric 		{
841588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8425dfc646bSeric 			*pvp++ = newstr(buf);
8435dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8445dfc646bSeric 			{
8455dfc646bSeric 				/* allow some space for trailing parms */
8465dfc646bSeric 				break;
8475dfc646bSeric 			}
8485dfc646bSeric 		}
849c579ef51Seric 	}
8505dfc646bSeric 
851145b49b1Seric 	/* see if any addresses still exist */
852145b49b1Seric 	if (tobuf[0] == '\0')
853c579ef51Seric 	{
854588cad61Seric 		define('g', (char *) NULL, e);
855145b49b1Seric 		return (0);
856c579ef51Seric 	}
857145b49b1Seric 
8585dfc646bSeric 	/* print out messages as full list */
85963780dbdSeric 	e->e_to = tobuf + 1;
8605dfc646bSeric 
8615dfc646bSeric 	/*
8625dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8635dfc646bSeric 	*/
8645dfc646bSeric 
865c579ef51Seric 	while (!clever && *++mvp != NULL)
8665dfc646bSeric 	{
867588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8685dfc646bSeric 		*pvp++ = newstr(buf);
8695dfc646bSeric 		if (pvp >= &pv[MAXPV])
87008b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8715dfc646bSeric 	}
8725dfc646bSeric 	*pvp++ = NULL;
8735dfc646bSeric 
87425a99e2eSeric 	/*
87525a99e2eSeric 	**  Call the mailer.
8766328bdf7Seric 	**	The argument vector gets built, pipes
87725a99e2eSeric 	**	are created as necessary, and we fork & exec as
8786328bdf7Seric 	**	appropriate.
879c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
88025a99e2eSeric 	*/
88125a99e2eSeric 
8827ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
88386b26461Seric 		ctladdr = &e->e_from;
884134746fbSeric #ifdef NAMED_BIND
8852bcc6d2dSeric 	if (ConfigLevel < 2)
886912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
887134746fbSeric #endif
8889c9e68d9Seric 
8899c9e68d9Seric 	if (tTd(11, 1))
890134746fbSeric 	{
8919c9e68d9Seric 		printf("openmailer:");
8929c9e68d9Seric 		printav(pv);
8939c9e68d9Seric 	}
8949c9e68d9Seric 	errno = 0;
8959c9e68d9Seric 
8969c9e68d9Seric 	CurHostName = m->m_mailer;
8979c9e68d9Seric 
8989c9e68d9Seric 	/*
8999c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9009c9e68d9Seric 	**  connection.
9019c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9029c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9039c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9049c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9059c9e68d9Seric 	*/
9069c9e68d9Seric 
9079c9e68d9Seric 	curhost = NULL;
908c931b82bSeric 	SmtpPhase = NULL;
909df106f0bSeric 	mci = NULL;
9109c9e68d9Seric 
9117febfd66Seric #ifdef XDEBUG
9127febfd66Seric 	{
9137febfd66Seric 		char wbuf[MAXLINE];
9147febfd66Seric 
9157febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9167febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
9177febfd66Seric 		checkfd012(wbuf);
9187febfd66Seric 	}
9197febfd66Seric #endif
9207febfd66Seric 
9219c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9229c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9239c9e68d9Seric 	{
9249c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9259c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9269c9e68d9Seric 		mci->mci_in = stdin;
9279c9e68d9Seric 		mci->mci_out = stdout;
9289c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9299c9e68d9Seric 		mci->mci_mailer = m;
9309c9e68d9Seric 	}
9319c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9329c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9339c9e68d9Seric 	{
9349c9e68d9Seric #ifdef DAEMON
9359c9e68d9Seric 		register int i;
9369c9e68d9Seric 		register u_short port;
9379c9e68d9Seric 
9389c9e68d9Seric 		CurHostName = pv[1];
9399c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9409c9e68d9Seric 
9419c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9429c9e68d9Seric 		{
9439c9e68d9Seric 			syserr("null signature");
944845e533cSeric 			rcode = EX_OSERR;
945b31e7f2bSeric 			goto give_up;
946b31e7f2bSeric 		}
9479c9e68d9Seric 
9489c9e68d9Seric 		if (!clever)
9499c9e68d9Seric 		{
9509c9e68d9Seric 			syserr("554 non-clever IPC");
951df106f0bSeric 			rcode = EX_CONFIG;
9529c9e68d9Seric 			goto give_up;
9539c9e68d9Seric 		}
9549c9e68d9Seric 		if (pv[2] != NULL)
9559c9e68d9Seric 			port = atoi(pv[2]);
9569c9e68d9Seric 		else
9579c9e68d9Seric 			port = 0;
9589c9e68d9Seric tryhost:
9599c9e68d9Seric 		while (*curhost != '\0')
9609c9e68d9Seric 		{
9619c9e68d9Seric 			register char *p;
9629c9e68d9Seric 			static char hostbuf[MAXNAME];
9639c9e68d9Seric 
9649c9e68d9Seric 			/* pull the next host from the signature */
9659c9e68d9Seric 			p = strchr(curhost, ':');
9669c9e68d9Seric 			if (p == NULL)
9679c9e68d9Seric 				p = &curhost[strlen(curhost)];
968df106f0bSeric 			if (p == curhost)
969df106f0bSeric 			{
970df106f0bSeric 				syserr("deliver: null host name in signature");
9718087428aSeric 				curhost++;
972df106f0bSeric 				continue;
973df106f0bSeric 			}
9749c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9759c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9769c9e68d9Seric 			if (*p != '\0')
9779c9e68d9Seric 				p++;
9789c9e68d9Seric 			curhost = p;
9799c9e68d9Seric 
9809c9e68d9Seric 			/* see if we already know that this host is fried */
9819c9e68d9Seric 			CurHostName = hostbuf;
9829c9e68d9Seric 			mci = mci_get(hostbuf, m);
9839c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9849c9e68d9Seric 			{
9859c9e68d9Seric 				if (tTd(11, 1))
9869c9e68d9Seric 				{
9879c9e68d9Seric 					printf("openmailer: ");
9884052916eSeric 					mci_dump(mci, FALSE);
9899c9e68d9Seric 				}
9909c9e68d9Seric 				CurHostName = mci->mci_host;
9919c9e68d9Seric 				break;
9929c9e68d9Seric 			}
9939c9e68d9Seric 			mci->mci_mailer = m;
9949c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9959c9e68d9Seric 				continue;
9969c9e68d9Seric 
9979c9e68d9Seric 			/* try the connection */
9989c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9999c9e68d9Seric 			message("Connecting to %s (%s)...",
10009c9e68d9Seric 				hostbuf, m->m_name);
10019c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
10029c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
10039c9e68d9Seric 			mci->mci_exitstat = i;
10049c9e68d9Seric 			mci->mci_errno = errno;
1005f170942cSeric #ifdef NAMED_BIND
1006f170942cSeric 			mci->mci_herrno = h_errno;
1007f170942cSeric #endif
10089c9e68d9Seric 			if (i == EX_OK)
10099c9e68d9Seric 			{
10109c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10119c9e68d9Seric 				mci_cache(mci);
1012f170942cSeric 				if (TrafficLogFile != NULL)
1013f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1014f170942cSeric 						getpid(), hostbuf);
10159c9e68d9Seric 				break;
10169c9e68d9Seric 			}
10179c9e68d9Seric 			else if (tTd(11, 1))
10189c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10199c9e68d9Seric 					i, errno);
10209c9e68d9Seric 
10219c9e68d9Seric 			/* enter status of this host */
10229c9e68d9Seric 			setstat(i);
1023df106f0bSeric 
1024df106f0bSeric 			/* should print some message here for -v mode */
1025df106f0bSeric 		}
1026df106f0bSeric 		if (mci == NULL)
1027df106f0bSeric 		{
1028df106f0bSeric 			syserr("deliver: no host name");
1029df106f0bSeric 			rcode = EX_OSERR;
1030df106f0bSeric 			goto give_up;
10319c9e68d9Seric 		}
10329c9e68d9Seric 		mci->mci_pid = 0;
10339c9e68d9Seric #else /* no DAEMON */
10349c9e68d9Seric 		syserr("554 openmailer: no IPC");
10359c9e68d9Seric 		if (tTd(11, 1))
10369c9e68d9Seric 			printf("openmailer: NULL\n");
1037df106f0bSeric 		rcode = EX_UNAVAILABLE;
1038df106f0bSeric 		goto give_up;
10399c9e68d9Seric #endif /* DAEMON */
10409c9e68d9Seric 	}
10419c9e68d9Seric 	else
10429c9e68d9Seric 	{
1043f170942cSeric 		if (TrafficLogFile != NULL)
10440e3bfef5Seric 		{
1045f170942cSeric 			char **av;
1046f170942cSeric 
1047f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1048f170942cSeric 			for (av = pv; *av != NULL; av++)
1049f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1050f170942cSeric 			fprintf(TrafficLogFile, "\n");
10516fe8c3bcSeric 		}
10526fe8c3bcSeric 
10539c9e68d9Seric 		/* create a pipe to shove the mail through */
10549c9e68d9Seric 		if (pipe(mpvect) < 0)
10559c9e68d9Seric 		{
10560e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10570e3bfef5Seric 				e->e_to, m->m_name);
10589c9e68d9Seric 			if (tTd(11, 1))
10599c9e68d9Seric 				printf("openmailer: NULL\n");
10609c9e68d9Seric 			rcode = EX_OSERR;
10619c9e68d9Seric 			goto give_up;
10629c9e68d9Seric 		}
10639c9e68d9Seric 
10649c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10659c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10669c9e68d9Seric 		{
10670e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10680e3bfef5Seric 				e->e_to, m->m_name);
10699c9e68d9Seric 			(void) close(mpvect[0]);
10709c9e68d9Seric 			(void) close(mpvect[1]);
10719c9e68d9Seric 			if (tTd(11, 1))
10729c9e68d9Seric 				printf("openmailer: NULL\n");
10739c9e68d9Seric 			rcode = EX_OSERR;
10749c9e68d9Seric 			goto give_up;
10759c9e68d9Seric 		}
10769c9e68d9Seric 
10779c9e68d9Seric 		/*
10789c9e68d9Seric 		**  Actually fork the mailer process.
10799c9e68d9Seric 		**	DOFORK is clever about retrying.
10809c9e68d9Seric 		**
10819c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10829c9e68d9Seric 		**	around so that endmail will get it.
10839c9e68d9Seric 		*/
10849c9e68d9Seric 
10859c9e68d9Seric 		if (e->e_xfp != NULL)
10869c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10879c9e68d9Seric 		(void) fflush(stdout);
10889c9e68d9Seric # ifdef SIGCHLD
10892b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
10909c9e68d9Seric # endif /* SIGCHLD */
10919c9e68d9Seric 		DOFORK(FORK);
10929c9e68d9Seric 		/* pid is set by DOFORK */
10939c9e68d9Seric 		if (pid < 0)
10949c9e68d9Seric 		{
10959c9e68d9Seric 			/* failure */
10960e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
10970e3bfef5Seric 				e->e_to, m->m_name);
10989c9e68d9Seric 			(void) close(mpvect[0]);
10999c9e68d9Seric 			(void) close(mpvect[1]);
11009c9e68d9Seric 			if (clever)
11019c9e68d9Seric 			{
11029c9e68d9Seric 				(void) close(rpvect[0]);
11039c9e68d9Seric 				(void) close(rpvect[1]);
11049c9e68d9Seric 			}
11059c9e68d9Seric 			if (tTd(11, 1))
11069c9e68d9Seric 				printf("openmailer: NULL\n");
11079c9e68d9Seric 			rcode = EX_OSERR;
11089c9e68d9Seric 			goto give_up;
11099c9e68d9Seric 		}
11109c9e68d9Seric 		else if (pid == 0)
11119c9e68d9Seric 		{
11129c9e68d9Seric 			int i;
11139c9e68d9Seric 			int saveerrno;
11149c9e68d9Seric 			char **ep;
11159c9e68d9Seric 			char *env[MAXUSERENVIRON];
11169c9e68d9Seric 			extern char **environ;
11179c9e68d9Seric 			extern int DtableSize;
11189c9e68d9Seric 
11199c9e68d9Seric 			/* child -- set up input & exec mailer */
11202b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
11212b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
11222b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11239c9e68d9Seric 
112444f2317fSeric 			/* reset user and group */
112544f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
112644f2317fSeric 			{
112744f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
112844f2317fSeric 				{
112944f2317fSeric 					(void) initgroups(DefUser, DefGid);
113044f2317fSeric 					(void) setuid(DefUid);
113144f2317fSeric 				}
113244f2317fSeric 				else
113344f2317fSeric 				{
113444f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
113544f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
113644f2317fSeric 						ctladdr->q_gid);
113744f2317fSeric 					(void) setuid(ctladdr->q_uid);
113844f2317fSeric 				}
113944f2317fSeric 			}
114044f2317fSeric 
114144f2317fSeric 			if (tTd(11, 2))
114244f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
114344f2317fSeric 					getuid(), geteuid());
114444f2317fSeric 
1145b986f6aaSeric 			/* move into some "safe" directory */
1146b986f6aaSeric 			if (m->m_execdir != NULL)
1147b986f6aaSeric 			{
1148b986f6aaSeric 				char *p, *q;
1149b986f6aaSeric 				char buf[MAXLINE];
1150b986f6aaSeric 
1151b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1152b986f6aaSeric 				{
1153b986f6aaSeric 					q = strchr(p, ':');
1154b986f6aaSeric 					if (q != NULL)
1155b986f6aaSeric 						*q = '\0';
1156b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1157b986f6aaSeric 					if (q != NULL)
1158b986f6aaSeric 						*q++ = ':';
1159b986f6aaSeric 					if (tTd(11, 20))
1160b986f6aaSeric 						printf("openmailer: trydir %s\n",
1161b986f6aaSeric 							buf);
1162b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1163b986f6aaSeric 						break;
1164b986f6aaSeric 				}
1165b986f6aaSeric 			}
1166b986f6aaSeric 
11679c9e68d9Seric 			/* arrange to filter std & diag output of command */
11689c9e68d9Seric 			if (clever)
11699c9e68d9Seric 			{
11709c9e68d9Seric 				(void) close(rpvect[0]);
11716fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11726fe8c3bcSeric 				{
11730e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11740e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11756fe8c3bcSeric 					_exit(EX_OSERR);
11766fe8c3bcSeric 				}
11779c9e68d9Seric 				(void) close(rpvect[1]);
11789c9e68d9Seric 			}
11799c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11809c9e68d9Seric 			{
11819c9e68d9Seric 				/* put mailer output in transcript */
11826fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11836fe8c3bcSeric 				{
11840e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11850e3bfef5Seric 						e->e_to, m->m_name,
11866fe8c3bcSeric 						fileno(e->e_xfp));
11876fe8c3bcSeric 					_exit(EX_OSERR);
11889c9e68d9Seric 				}
11896fe8c3bcSeric 			}
11906fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11916fe8c3bcSeric 			{
11920e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
11930e3bfef5Seric 					e->e_to, m->m_name);
11946fe8c3bcSeric 				_exit(EX_OSERR);
11956fe8c3bcSeric 			}
11969c9e68d9Seric 
11979c9e68d9Seric 			/* arrange to get standard input */
11989c9e68d9Seric 			(void) close(mpvect[1]);
11999c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
12009c9e68d9Seric 			{
12010e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
12020e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
12039c9e68d9Seric 				_exit(EX_OSERR);
12049c9e68d9Seric 			}
12059c9e68d9Seric 			(void) close(mpvect[0]);
12069c9e68d9Seric 
12079c9e68d9Seric 			/* arrange for all the files to be closed */
12089c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12099c9e68d9Seric 			{
12109c9e68d9Seric 				register int j;
121144f2317fSeric 
12129c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12139c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
12149c9e68d9Seric 			}
12159c9e68d9Seric 
12169c9e68d9Seric 			/* set up the mailer environment */
12179c9e68d9Seric 			i = 0;
12189c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12199c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12209c9e68d9Seric 			{
12219c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12229c9e68d9Seric 					env[i++] = *ep;
12239c9e68d9Seric 			}
12249c9e68d9Seric 			env[i++] = NULL;
12259c9e68d9Seric 
12269c9e68d9Seric 			/* try to execute the mailer */
12279c9e68d9Seric 			execve(m->m_mailer, pv, env);
12289c9e68d9Seric 			saveerrno = errno;
12299c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12307c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12317c941fd2Seric 				_exit(EX_OSERR);
12329c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12339c9e68d9Seric 		}
12349c9e68d9Seric 
12359c9e68d9Seric 		/*
12369c9e68d9Seric 		**  Set up return value.
12379c9e68d9Seric 		*/
12389c9e68d9Seric 
12399c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12409c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12419c9e68d9Seric 		mci->mci_mailer = m;
12429c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12439c9e68d9Seric 		mci->mci_pid = pid;
12449c9e68d9Seric 		(void) close(mpvect[0]);
12459c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1246335eae58Seric 		if (mci->mci_out == NULL)
1247335eae58Seric 		{
1248335eae58Seric 			syserr("deliver: cannot create mailer output channel, fd=%d",
1249335eae58Seric 				mpvect[1]);
1250335eae58Seric 			(void) close(mpvect[1]);
1251335eae58Seric 			if (clever)
1252335eae58Seric 			{
1253335eae58Seric 				(void) close(rpvect[0]);
1254335eae58Seric 				(void) close(rpvect[1]);
1255335eae58Seric 			}
1256335eae58Seric 			rcode = EX_OSERR;
1257335eae58Seric 			goto give_up;
1258335eae58Seric 		}
12599c9e68d9Seric 		if (clever)
12609c9e68d9Seric 		{
12619c9e68d9Seric 			(void) close(rpvect[1]);
12629c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
1263335eae58Seric 			if (mci->mci_in == NULL)
1264335eae58Seric 			{
1265335eae58Seric 				syserr("deliver: cannot create mailer input channel, fd=%d",
1266335eae58Seric 					mpvect[1]);
1267335eae58Seric 				(void) close(rpvect[0]);
1268335eae58Seric 				fclose(mci->mci_out);
1269335eae58Seric 				mci->mci_out = NULL;
1270335eae58Seric 				rcode = EX_OSERR;
1271335eae58Seric 				goto give_up;
1272335eae58Seric 			}
12739c9e68d9Seric 		}
12749c9e68d9Seric 		else
12759c9e68d9Seric 		{
12769c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12779c9e68d9Seric 			mci->mci_in = NULL;
12789c9e68d9Seric 		}
12799c9e68d9Seric 	}
12809c9e68d9Seric 
12819c9e68d9Seric 	/*
12829c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12839c9e68d9Seric 	*/
12849c9e68d9Seric 
12859c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12869c9e68d9Seric 	{
12879c9e68d9Seric 		smtpinit(m, mci, e);
12889c9e68d9Seric 	}
12899c9e68d9Seric 	if (tTd(11, 1))
12909c9e68d9Seric 	{
12919c9e68d9Seric 		printf("openmailer: ");
12924052916eSeric 		mci_dump(mci, FALSE);
12939c9e68d9Seric 	}
12949c9e68d9Seric 
12959c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1296b31e7f2bSeric 	{
1297b31e7f2bSeric 		/* couldn't open the mailer */
1298b31e7f2bSeric 		rcode = mci->mci_exitstat;
12992a6bc25bSeric 		errno = mci->mci_errno;
1300f170942cSeric #ifdef NAMED_BIND
1301f170942cSeric 		h_errno = mci->mci_herrno;
1302f170942cSeric #endif
1303b31e7f2bSeric 		if (rcode == EX_OK)
1304b31e7f2bSeric 		{
1305b31e7f2bSeric 			/* shouldn't happen */
130608b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
13076b0f339dSeric 				rcode, mci->mci_state, firstsig);
1308b31e7f2bSeric 			rcode = EX_SOFTWARE;
1309b31e7f2bSeric 		}
1310e9277e33Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
131190891494Seric 		{
131290891494Seric 			/* try next MX site */
131390891494Seric 			goto tryhost;
131490891494Seric 		}
1315b31e7f2bSeric 	}
1316b31e7f2bSeric 	else if (!clever)
1317b31e7f2bSeric 	{
1318b31e7f2bSeric 		/*
1319b31e7f2bSeric 		**  Format and send message.
1320b31e7f2bSeric 		*/
132115d084d5Seric 
1322b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1323b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1324b31e7f2bSeric 		putline("\n", mci->mci_out, m);
132503c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1326b31e7f2bSeric 
1327b31e7f2bSeric 		/* get the exit status */
1328c9be6216Seric 		rcode = endmailer(mci, e, pv);
1329134746fbSeric 	}
1330134746fbSeric 	else
1331b31e7f2bSeric #ifdef SMTP
1332134746fbSeric 	{
1333b31e7f2bSeric 		/*
1334b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1335b31e7f2bSeric 		*/
133615d084d5Seric 
1337b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1338b31e7f2bSeric 		if (rcode == EX_OK)
133975889e88Seric 		{
1340ded0d3daSkarels 			register char *t = tobuf;
1341ded0d3daSkarels 			register int i;
1342ded0d3daSkarels 
1343588cad61Seric 			/* send the recipient list */
134463780dbdSeric 			tobuf[0] = '\0';
134575889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
134675889e88Seric 			{
134763780dbdSeric 				e->e_to = to->q_paddr;
134815d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
134975889e88Seric 				{
135083b7ddc9Seric 					markfailure(e, to, i);
1351*4dee0003Seric 					giveresponse(i, m, mci, ctladdr, e);
135263780dbdSeric 				}
135375889e88Seric 				else
135475889e88Seric 				{
1355911693bfSbostic 					*t++ = ',';
1356b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1357b31e7f2bSeric 						continue;
1358ee39df61Seric 					*t = '\0';
1359588cad61Seric 				}
1360588cad61Seric 			}
1361588cad61Seric 
136263780dbdSeric 			/* now send the data */
136363780dbdSeric 			if (tobuf[0] == '\0')
1364b31e7f2bSeric 			{
13659c9e68d9Seric 				rcode = EX_OK;
136663780dbdSeric 				e->e_to = NULL;
1367b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1368b31e7f2bSeric 					smtprset(m, mci, e);
1369b31e7f2bSeric 			}
137075889e88Seric 			else
137175889e88Seric 			{
137263780dbdSeric 				e->e_to = tobuf + 1;
137375889e88Seric 				rcode = smtpdata(m, mci, e);
137463780dbdSeric 			}
137563780dbdSeric 
137663780dbdSeric 			/* now close the connection */
1377b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
137815d084d5Seric 				smtpquit(m, mci, e);
137963780dbdSeric 		}
13809c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13819c9e68d9Seric 		{
13829c9e68d9Seric 			/* try next MX site */
13839c9e68d9Seric 			goto tryhost;
13849c9e68d9Seric 		}
1385c579ef51Seric 	}
1386b31e7f2bSeric #else /* not SMTP */
1387a05b3449Sbostic 	{
138808b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1389845e533cSeric 		rcode = EX_CONFIG;
1390b31e7f2bSeric 		goto give_up;
1391a05b3449Sbostic 	}
1392b31e7f2bSeric #endif /* SMTP */
1393134746fbSeric #ifdef NAMED_BIND
13942bcc6d2dSeric 	if (ConfigLevel < 2)
1395912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1396134746fbSeric #endif
13975dfc646bSeric 
1398b31e7f2bSeric 	/* arrange a return receipt if requested */
1399df106f0bSeric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1400df106f0bSeric 	    bitnset(M_LOCALMAILER, m->m_flags))
1401b31e7f2bSeric 	{
1402b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1403b31e7f2bSeric 		/* do we want to send back more info? */
1404b31e7f2bSeric 	}
1405b31e7f2bSeric 
1406c77d1c25Seric 	/*
140763780dbdSeric 	**  Do final status disposal.
140863780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1409c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1410c77d1c25Seric 	**		addressees.
1411c77d1c25Seric 	*/
1412c77d1c25Seric 
1413b31e7f2bSeric   give_up:
141463780dbdSeric 	if (tobuf[0] != '\0')
1415*4dee0003Seric 		giveresponse(rcode, m, mci, ctladdr, e);
1416772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1417b31e7f2bSeric 	{
1418dde5acadSeric 		if (rcode != EX_OK)
141983b7ddc9Seric 			markfailure(e, to, rcode);
1420dde5acadSeric 		else
1421655518ecSeric 		{
1422dde5acadSeric 			to->q_flags |= QSENT;
1423655518ecSeric 			e->e_nsent++;
1424df106f0bSeric 			if (e->e_receiptto != NULL &&
1425df106f0bSeric 			    bitnset(M_LOCALMAILER, m->m_flags))
1426df106f0bSeric 			{
1427df106f0bSeric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1428df106f0bSeric 					to->q_paddr);
1429df106f0bSeric 			}
1430655518ecSeric 		}
1431b31e7f2bSeric 	}
1432b31e7f2bSeric 
1433b31e7f2bSeric 	/*
1434b31e7f2bSeric 	**  Restore state and return.
1435b31e7f2bSeric 	*/
1436c77d1c25Seric 
14377febfd66Seric #ifdef XDEBUG
14387febfd66Seric 	{
14397febfd66Seric 		char wbuf[MAXLINE];
14407febfd66Seric 
14417febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
144244622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
144344622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
144444622ea3Seric 			m->m_name);
14457febfd66Seric 		checkfd012(wbuf);
14467febfd66Seric 	}
14477febfd66Seric #endif
14487febfd66Seric 
144935490626Seric 	errno = 0;
1450588cad61Seric 	define('g', (char *) NULL, e);
14515826d9d3Seric 	return (rcode);
145225a99e2eSeric }
14535dfc646bSeric /*
145483b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
145583b7ddc9Seric **
145683b7ddc9Seric **	Parameters:
145783b7ddc9Seric **		e -- the envelope we are sending.
145883b7ddc9Seric **		q -- the address to mark.
145983b7ddc9Seric **		rcode -- the code signifying the particular failure.
146083b7ddc9Seric **
146183b7ddc9Seric **	Returns:
146283b7ddc9Seric **		none.
146383b7ddc9Seric **
146483b7ddc9Seric **	Side Effects:
146583b7ddc9Seric **		marks the address (and possibly the envelope) with the
146683b7ddc9Seric **			failure so that an error will be returned or
146783b7ddc9Seric **			the message will be queued, as appropriate.
146883b7ddc9Seric */
146983b7ddc9Seric 
147083b7ddc9Seric markfailure(e, q, rcode)
147183b7ddc9Seric 	register ENVELOPE *e;
147283b7ddc9Seric 	register ADDRESS *q;
147383b7ddc9Seric 	int rcode;
147483b7ddc9Seric {
147519c47125Seric 	char buf[MAXLINE];
147619c47125Seric 
147783b7ddc9Seric 	if (rcode == EX_OK)
147883b7ddc9Seric 		return;
1479f170942cSeric 	else if (rcode == EX_TEMPFAIL)
148083b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1481f170942cSeric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
1482f170942cSeric 		q->q_flags |= QBADADDR;
148383b7ddc9Seric }
148483b7ddc9Seric /*
1485c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1486c579ef51Seric **
1487c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1488c579ef51Seric **	violation), so we report those specially.  For other
1489c579ef51Seric **	errors, we choose a status message (into statmsg),
1490c579ef51Seric **	and if it represents an error, we print it.
1491c579ef51Seric **
1492c579ef51Seric **	Parameters:
1493c579ef51Seric **		pid -- pid of mailer.
1494c9be6216Seric **		e -- the current envelope.
1495c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1496c9be6216Seric **			(for error messages).
1497c579ef51Seric **
1498c579ef51Seric **	Returns:
1499c579ef51Seric **		exit code of mailer.
1500c579ef51Seric **
1501c579ef51Seric **	Side Effects:
1502c579ef51Seric **		none.
1503c579ef51Seric */
1504c579ef51Seric 
1505c9be6216Seric endmailer(mci, e, pv)
1506b31e7f2bSeric 	register MCI *mci;
1507c9be6216Seric 	register ENVELOPE *e;
1508c9be6216Seric 	char **pv;
1509c579ef51Seric {
1510588cad61Seric 	int st;
1511c579ef51Seric 
151275889e88Seric 	/* close any connections */
151375889e88Seric 	if (mci->mci_in != NULL)
1514c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
151575889e88Seric 	if (mci->mci_out != NULL)
1516c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
151775889e88Seric 	mci->mci_in = mci->mci_out = NULL;
151875889e88Seric 	mci->mci_state = MCIS_CLOSED;
151975889e88Seric 
152033db8731Seric 	/* in the IPC case there is nothing to wait for */
152175889e88Seric 	if (mci->mci_pid == 0)
152233db8731Seric 		return (EX_OK);
152333db8731Seric 
152433db8731Seric 	/* wait for the mailer process to die and collect status */
152575889e88Seric 	st = waitfor(mci->mci_pid);
1526588cad61Seric 	if (st == -1)
152778de67c1Seric 	{
1528c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1529588cad61Seric 		return (EX_SOFTWARE);
1530c579ef51Seric 	}
153133db8731Seric 
1532bf9bc890Seric 	if (WIFEXITED(st))
1533c579ef51Seric 	{
1534bf9bc890Seric 		/* normal death -- return status */
1535bf9bc890Seric 		return (WEXITSTATUS(st));
1536bf9bc890Seric 	}
1537bf9bc890Seric 
1538bf9bc890Seric 	/* it died a horrid death */
1539c9be6216Seric 	syserr("mailer %s died with signal %o", pv[0], st);
1540c9be6216Seric 
1541c9be6216Seric 	/* log the arguments */
1542c9be6216Seric 	if (e->e_xfp != NULL)
1543c9be6216Seric 	{
1544c9be6216Seric 		register char **av;
1545c9be6216Seric 
1546c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1547c9be6216Seric 		for (av = pv; *av != NULL; av++)
1548c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1549c9be6216Seric 		fprintf(e->e_xfp, "\n");
1550c9be6216Seric 	}
1551c9be6216Seric 
15525f73204aSeric 	ExitStat = EX_TEMPFAIL;
15535f73204aSeric 	return (EX_TEMPFAIL);
1554c579ef51Seric }
1555c579ef51Seric /*
155625a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
155725a99e2eSeric **
155825a99e2eSeric **	Parameters:
155925a99e2eSeric **		stat -- the status code from the mailer (high byte
156025a99e2eSeric **			only; core dumps must have been taken care of
156125a99e2eSeric **			already).
156281161401Seric **		m -- the mailer info for this mailer.
156381161401Seric **		mci -- the mailer connection info -- can be NULL if the
156481161401Seric **			response is given before the connection is made.
1565*4dee0003Seric **		ctladdr -- the controlling address for the recipient
1566*4dee0003Seric **			address(es).
156781161401Seric **		e -- the current envelope.
156825a99e2eSeric **
156925a99e2eSeric **	Returns:
1570db8841e9Seric **		none.
157125a99e2eSeric **
157225a99e2eSeric **	Side Effects:
1573c1f9df2cSeric **		Errors may be incremented.
157425a99e2eSeric **		ExitStat may be set.
157525a99e2eSeric */
157625a99e2eSeric 
1577*4dee0003Seric giveresponse(stat, m, mci, ctladdr, e)
157825a99e2eSeric 	int stat;
1579588cad61Seric 	register MAILER *m;
158081161401Seric 	register MCI *mci;
1581*4dee0003Seric 	ADDRESS *ctladdr;
1582198d9be0Seric 	ENVELOPE *e;
158325a99e2eSeric {
15849c16475dSeric 	register const char *statmsg;
158525a99e2eSeric 	extern char *SysExMsg[];
158625a99e2eSeric 	register int i;
1587d4bd8f0eSbostic 	extern int N_SysEx;
1588198d9be0Seric 	char buf[MAXLINE];
158925a99e2eSeric 
159013bbc08cSeric 	/*
159113bbc08cSeric 	**  Compute status message from code.
159213bbc08cSeric 	*/
159313bbc08cSeric 
159425a99e2eSeric 	i = stat - EX__BASE;
1595588cad61Seric 	if (stat == 0)
15966fe8c3bcSeric 	{
1597588cad61Seric 		statmsg = "250 Sent";
1598ce5531bdSeric 		if (e->e_statmsg != NULL)
15996fe8c3bcSeric 		{
1600ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
16016fe8c3bcSeric 			statmsg = buf;
16026fe8c3bcSeric 		}
16036fe8c3bcSeric 	}
1604588cad61Seric 	else if (i < 0 || i > N_SysEx)
1605588cad61Seric 	{
1606588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1607588cad61Seric 		stat = EX_UNAVAILABLE;
1608588cad61Seric 		statmsg = buf;
1609588cad61Seric 	}
1610198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1611198d9be0Seric 	{
16127d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1613d4bd8f0eSbostic #ifdef NAMED_BIND
1614f28da541Smiriam 		if (h_errno == TRY_AGAIN)
16154d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1616f28da541Smiriam 		else
1617d4bd8f0eSbostic #endif
1618f28da541Smiriam 		{
16198557d168Seric 			if (errno != 0)
1620d87e85f3Seric 				statmsg = errstring(errno);
1621d87e85f3Seric 			else
1622d87e85f3Seric 			{
1623d87e85f3Seric #ifdef SMTP
1624d87e85f3Seric 				extern char SmtpError[];
1625d87e85f3Seric 
1626d87e85f3Seric 				statmsg = SmtpError;
16276c2c3107Seric #else /* SMTP */
1628d87e85f3Seric 				statmsg = NULL;
16296c2c3107Seric #endif /* SMTP */
1630d87e85f3Seric 			}
1631f28da541Smiriam 		}
1632d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1633d87e85f3Seric 		{
163487c9b3e7Seric 			(void) strcat(buf, ": ");
1635d87e85f3Seric 			(void) strcat(buf, statmsg);
16368557d168Seric 		}
1637198d9be0Seric 		statmsg = buf;
1638198d9be0Seric 	}
1639f170942cSeric #ifdef NAMED_BIND
1640f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1641f170942cSeric 	{
16424d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1643f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1644f170942cSeric 		statmsg = buf;
1645f170942cSeric 	}
1646f170942cSeric #endif
164725a99e2eSeric 	else
1648d87e85f3Seric 	{
164925a99e2eSeric 		statmsg = SysExMsg[i];
16507d55540cSeric 		if (*statmsg++ == ':')
16517d55540cSeric 		{
16527d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16537d55540cSeric 			statmsg = buf;
16547d55540cSeric 		}
1655d87e85f3Seric 	}
1656588cad61Seric 
1657588cad61Seric 	/*
1658588cad61Seric 	**  Print the message as appropriate
1659588cad61Seric 	*/
1660588cad61Seric 
1661198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1662df106f0bSeric 	{
1663df106f0bSeric 		extern char MsgBuf[];
1664df106f0bSeric 
1665e12d03eeSeric 		message(&statmsg[4], errstring(errno));
1666df106f0bSeric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1667df106f0bSeric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1668df106f0bSeric 	}
166925a99e2eSeric 	else
167025a99e2eSeric 	{
1671c1f9df2cSeric 		Errors++;
1672e12d03eeSeric 		usrerr(statmsg, errstring(errno));
167325a99e2eSeric 	}
167425a99e2eSeric 
167525a99e2eSeric 	/*
167625a99e2eSeric 	**  Final cleanup.
167725a99e2eSeric 	**	Log a record of the transaction.  Compute the new
167825a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
167925a99e2eSeric 	**	that.
168025a99e2eSeric 	*/
168125a99e2eSeric 
16822f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
1683*4dee0003Seric 		logdelivery(m, mci, &statmsg[4], ctladdr, e);
1684eb238f8cSeric 
1685eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1686eb238f8cSeric 		setstat(stat);
1687198d9be0Seric 	if (stat != EX_OK)
1688198d9be0Seric 	{
1689198d9be0Seric 		if (e->e_message != NULL)
1690198d9be0Seric 			free(e->e_message);
1691198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1692198d9be0Seric 	}
16938557d168Seric 	errno = 0;
1694d4bd8f0eSbostic #ifdef NAMED_BIND
1695f28da541Smiriam 	h_errno = 0;
1696d4bd8f0eSbostic #endif
1697eb238f8cSeric }
1698eb238f8cSeric /*
1699eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1700eb238f8cSeric **
1701eb238f8cSeric **	Parameters:
170281161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
170381161401Seric **		mci -- the mailer connection info -- can be NULL if the
170481161401Seric **			log is occuring when no connection is active.
170581161401Seric **		stat -- the message to print for the status.
1706*4dee0003Seric **		ctladdr -- the controlling address for the to list.
170781161401Seric **		e -- the current envelope.
1708eb238f8cSeric **
1709eb238f8cSeric **	Returns:
1710eb238f8cSeric **		none
1711eb238f8cSeric **
1712eb238f8cSeric **	Side Effects:
1713eb238f8cSeric **		none
1714eb238f8cSeric */
1715eb238f8cSeric 
1716*4dee0003Seric logdelivery(m, mci, stat, ctladdr, e)
171781161401Seric 	MAILER *m;
171881161401Seric 	register MCI *mci;
1719eb238f8cSeric 	char *stat;
1720*4dee0003Seric 	ADDRESS *ctladdr;
1721b31e7f2bSeric 	register ENVELOPE *e;
17225cf56be3Seric {
1723eb238f8cSeric # ifdef LOG
1724*4dee0003Seric 	register char *bp;
1725d6acf3eeSeric 	char buf[512];
17269507d1f9Seric 
1727*4dee0003Seric 	bp = buf;
1728*4dee0003Seric 	if (ctladdr != NULL)
1729*4dee0003Seric 	{
1730*4dee0003Seric 		strcpy(bp, ", ctladdr=");
1731*4dee0003Seric 		strcat(bp, ctladdr->q_paddr);
1732*4dee0003Seric 		bp += strlen(bp);
1733*4dee0003Seric 		if (bitset(QGOODUID, ctladdr->q_flags))
1734*4dee0003Seric 		{
1735*4dee0003Seric 			(void) sprintf(bp, " (%d/%d)",
1736*4dee0003Seric 					ctladdr->q_uid, ctladdr->q_gid);
1737*4dee0003Seric 			bp += strlen(bp);
1738*4dee0003Seric 		}
1739*4dee0003Seric 	}
1740*4dee0003Seric 
1741*4dee0003Seric 	(void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
1742*4dee0003Seric 	bp += strlen(bp);
174381161401Seric 
174448ed5d33Seric 	if (m != NULL)
174571ff6caaSeric 	{
1746*4dee0003Seric 		(void) strcpy(bp, ", mailer=");
1747*4dee0003Seric 		(void) strcat(bp, m->m_name);
1748*4dee0003Seric 		bp += strlen(bp);
174971ff6caaSeric 	}
175048ed5d33Seric 
175148ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
175271ff6caaSeric 	{
175371ff6caaSeric # ifdef DAEMON
1754e2f2f828Seric 		extern SOCKADDR CurHostAddr;
175548ed5d33Seric # endif
175671ff6caaSeric 
1757*4dee0003Seric 		(void) strcpy(bp, ", relay=");
1758*4dee0003Seric 		(void) strcat(bp, mci->mci_host);
175948ed5d33Seric 
176048ed5d33Seric # ifdef DAEMON
1761*4dee0003Seric 		(void) strcat(bp, " (");
1762*4dee0003Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
1763*4dee0003Seric 		(void) strcat(bp, ")");
176471ff6caaSeric # endif
176571ff6caaSeric 	}
17669507d1f9Seric 	else
176748ed5d33Seric 	{
176848ed5d33Seric 		char *p = macvalue('h', e);
17699507d1f9Seric 
177048ed5d33Seric 		if (p != NULL && p[0] != '\0')
177148ed5d33Seric 		{
1772*4dee0003Seric 			(void) strcpy(bp, ", relay=");
1773*4dee0003Seric 			(void) strcat(bp, p);
177448ed5d33Seric 		}
177548ed5d33Seric 	}
1776d6acf3eeSeric 
1777*4dee0003Seric 	syslog(LOG_INFO, "%s: to=%s%s, stat=%s",
1778d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
17796c2c3107Seric # endif /* LOG */
178025a99e2eSeric }
178125a99e2eSeric /*
178251552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
178325a99e2eSeric **
178451552439Seric **	This can be made an arbitrary message separator by changing $l
178551552439Seric **
17869b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
17879b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
17889b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
17899b6c17a6Seric **	this kind of antique garbage????
179025a99e2eSeric **
179125a99e2eSeric **	Parameters:
179251552439Seric **		fp -- the file to output to.
179351552439Seric **		m -- the mailer describing this entry.
179425a99e2eSeric **
179525a99e2eSeric **	Returns:
179651552439Seric **		none
179725a99e2eSeric **
179825a99e2eSeric **	Side Effects:
179951552439Seric **		outputs some text to fp.
180025a99e2eSeric */
180125a99e2eSeric 
1802b31e7f2bSeric putfromline(fp, m, e)
180351552439Seric 	register FILE *fp;
180451552439Seric 	register MAILER *m;
1805b31e7f2bSeric 	ENVELOPE *e;
180625a99e2eSeric {
18072bc47524Seric 	char *template = "\201l\n";
180851552439Seric 	char buf[MAXLINE];
180925a99e2eSeric 
181057fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
181151552439Seric 		return;
181213bbc08cSeric 
18132c7e1b8dSeric # ifdef UGLYUUCP
181457fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
181574b6e67bSeric 	{
1816ea09d6edSeric 		char *bang;
1817ea09d6edSeric 		char xbuf[MAXLINE];
181874b6e67bSeric 
1819ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
18206c2c3107Seric 		bang = strchr(buf, '!');
182174b6e67bSeric 		if (bang == NULL)
182234fcca25Seric 		{
182334fcca25Seric 			errno = 0;
182434fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
182534fcca25Seric 		}
182674b6e67bSeric 		else
1827588cad61Seric 		{
1828ea09d6edSeric 			*bang++ = '\0';
18292bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1830ea09d6edSeric 			template = xbuf;
183174b6e67bSeric 		}
1832588cad61Seric 	}
18336c2c3107Seric # endif /* UGLYUUCP */
1834b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
183577b52738Seric 	putline(buf, fp, m);
1836bc6e2962Seric }
1837bc6e2962Seric /*
183851552439Seric **  PUTBODY -- put the body of a message.
183951552439Seric **
184051552439Seric **	Parameters:
184151552439Seric **		fp -- file to output onto.
184277b52738Seric **		m -- a mailer descriptor to control output format.
18439a6a5f55Seric **		e -- the envelope to put out.
184403c02fdeSeric **		separator -- if non-NULL, a message separator that must
184503c02fdeSeric **			not be permitted in the resulting message.
184651552439Seric **
184751552439Seric **	Returns:
184851552439Seric **		none.
184951552439Seric **
185051552439Seric **	Side Effects:
185151552439Seric **		The message is written onto fp.
185251552439Seric */
185351552439Seric 
185403c02fdeSeric putbody(fp, m, e, separator)
185551552439Seric 	FILE *fp;
1856588cad61Seric 	MAILER *m;
18579a6a5f55Seric 	register ENVELOPE *e;
185803c02fdeSeric 	char *separator;
185951552439Seric {
186077b52738Seric 	char buf[MAXLINE];
186151552439Seric 
186251552439Seric 	/*
186351552439Seric 	**  Output the body of the message
186451552439Seric 	*/
186551552439Seric 
18669a6a5f55Seric 	if (e->e_dfp == NULL)
186751552439Seric 	{
18689a6a5f55Seric 		if (e->e_df != NULL)
18699a6a5f55Seric 		{
18709a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
18719a6a5f55Seric 			if (e->e_dfp == NULL)
18728f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
1873e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
18749a6a5f55Seric 		}
18759a6a5f55Seric 		else
187677b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
18779a6a5f55Seric 	}
18789a6a5f55Seric 	if (e->e_dfp != NULL)
18799a6a5f55Seric 	{
18809a6a5f55Seric 		rewind(e->e_dfp);
188177b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
188224fc8aeeSeric 		{
188324fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1884d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
18853462ad9eSeric 				(void) putc('>', fp);
188603c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
188703c02fdeSeric 			{
188803c02fdeSeric 				/* possible separator */
188903c02fdeSeric 				int sl = strlen(separator);
189003c02fdeSeric 
189103c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
189203c02fdeSeric 					(void) putc(' ', fp);
189303c02fdeSeric 			}
189477b52738Seric 			putline(buf, fp, m);
189524fc8aeeSeric 		}
189651552439Seric 
18979a6a5f55Seric 		if (ferror(e->e_dfp))
189851552439Seric 		{
1899df106f0bSeric 			syserr("putbody: %s: read error", e->e_df);
190051552439Seric 			ExitStat = EX_IOERR;
190151552439Seric 		}
190251552439Seric 	}
190351552439Seric 
19040890ba1fSeric 	/* some mailers want extra blank line at end of message */
19050890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
19060890ba1fSeric 		putline("", fp, m);
19070890ba1fSeric 
190851552439Seric 	(void) fflush(fp);
190951552439Seric 	if (ferror(fp) && errno != EPIPE)
191051552439Seric 	{
191151552439Seric 		syserr("putbody: write error");
191251552439Seric 		ExitStat = EX_IOERR;
191351552439Seric 	}
191451552439Seric 	errno = 0;
191525a99e2eSeric }
191625a99e2eSeric /*
191725a99e2eSeric **  MAILFILE -- Send a message to a file.
191825a99e2eSeric **
1919f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1920f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1921f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1922f129ec7dSeric **	sendmail runs as root.
1923f129ec7dSeric **
1924588cad61Seric **	This could be done as a subordinate mailer, except that it
1925588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1926588cad61Seric **	view this as being sufficiently important as to include it
1927588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1928588cad61Seric **	to create another process plus some pipes to save the message.
1929588cad61Seric **
193025a99e2eSeric **	Parameters:
193125a99e2eSeric **		filename -- the name of the file to send to.
19326259796dSeric **		ctladdr -- the controlling address header -- includes
19336259796dSeric **			the userid/groupid to be when sending.
193425a99e2eSeric **
193525a99e2eSeric **	Returns:
193625a99e2eSeric **		The exit code associated with the operation.
193725a99e2eSeric **
193825a99e2eSeric **	Side Effects:
193925a99e2eSeric **		none.
194025a99e2eSeric */
194125a99e2eSeric 
1942b31e7f2bSeric mailfile(filename, ctladdr, e)
194325a99e2eSeric 	char *filename;
19446259796dSeric 	ADDRESS *ctladdr;
1945b31e7f2bSeric 	register ENVELOPE *e;
194625a99e2eSeric {
194725a99e2eSeric 	register FILE *f;
194832d19d43Seric 	register int pid;
194915d084d5Seric 	int mode;
195025a99e2eSeric 
1951671745f3Seric 	if (tTd(11, 1))
1952671745f3Seric 	{
1953671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
1954671745f3Seric 		printaddr(ctladdr, FALSE);
1955671745f3Seric 	}
1956671745f3Seric 
1957f170942cSeric 	if (e->e_xfp != NULL)
1958f170942cSeric 		fflush(e->e_xfp);
1959f170942cSeric 
196032d19d43Seric 	/*
196132d19d43Seric 	**  Fork so we can change permissions here.
196232d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
196332d19d43Seric 	**	the complications of calling subroutines, etc.
196432d19d43Seric 	*/
196532d19d43Seric 
196632d19d43Seric 	DOFORK(fork);
196732d19d43Seric 
196832d19d43Seric 	if (pid < 0)
196932d19d43Seric 		return (EX_OSERR);
197032d19d43Seric 	else if (pid == 0)
197132d19d43Seric 	{
197232d19d43Seric 		/* child -- actually write to file */
1973f129ec7dSeric 		struct stat stb;
1974f129ec7dSeric 
19752b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
19762b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
19772b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
19783462ad9eSeric 		(void) umask(OldUmask);
197995f16dc0Seric 
1980f129ec7dSeric 		if (stat(filename, &stb) < 0)
19813a98e7eaSeric 			stb.st_mode = FileMode;
198215d084d5Seric 		mode = stb.st_mode;
198395f16dc0Seric 
198495f16dc0Seric 		/* limit the errors to those actually caused in the child */
198595f16dc0Seric 		errno = 0;
198695f16dc0Seric 		ExitStat = EX_OK;
198795f16dc0Seric 
1988f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1989f129ec7dSeric 			exit(EX_CANTCREAT);
199003827b5fSeric 		if (ctladdr == NULL)
19918f9146b0Srick 			ctladdr = &e->e_from;
199215d084d5Seric 		else
199315d084d5Seric 		{
199415d084d5Seric 			/* ignore setuid and setgid bits */
199515d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
199615d084d5Seric 		}
199715d084d5Seric 
19988f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
19998f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
20008f9146b0Srick 		{
20018f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
200295f16dc0Seric 			if (e->e_dfp == NULL)
200395f16dc0Seric 			{
20048f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
2005e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
20068f9146b0Srick 			}
20078f9146b0Srick 		}
20088f9146b0Srick 
200915d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
2010e36b99e2Seric 		{
201195f16dc0Seric 			if (ctladdr->q_uid == 0)
201295f16dc0Seric 			{
2013898a126bSbostic 				(void) initgroups(DefUser, DefGid);
201495f16dc0Seric 			}
201595f16dc0Seric 			else
201695f16dc0Seric 			{
2017898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
2018898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
2019898a126bSbostic 					ctladdr->q_gid);
2020898a126bSbostic 			}
2021e36b99e2Seric 		}
202215d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
2023e36b99e2Seric 		{
2024e36b99e2Seric 			if (ctladdr->q_uid == 0)
2025e36b99e2Seric 				(void) setuid(DefUid);
2026e36b99e2Seric 			else
20276259796dSeric 				(void) setuid(ctladdr->q_uid);
2028e36b99e2Seric 		}
202995f16dc0Seric 		FileName = filename;
203095f16dc0Seric 		LineNumber = 0;
20313a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
203225a99e2eSeric 		if (f == NULL)
203395f16dc0Seric 		{
2034b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
203532d19d43Seric 			exit(EX_CANTCREAT);
203695f16dc0Seric 		}
203725a99e2eSeric 
20380331ce05Seric 		putfromline(f, FileMailer, e);
20390331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
20400331ce05Seric 		putline("\n", f, FileMailer);
204103c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
20420331ce05Seric 		putline("\n", f, FileMailer);
204395f16dc0Seric 		if (ferror(f))
204495f16dc0Seric 		{
2045b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
204695f16dc0Seric 			setstat(EX_IOERR);
204795f16dc0Seric 		}
2048ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
204932d19d43Seric 		(void) fflush(stdout);
2050e36b99e2Seric 
205127628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
2052c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
205395f16dc0Seric 		exit(ExitStat);
205413bbc08cSeric 		/*NOTREACHED*/
205532d19d43Seric 	}
205632d19d43Seric 	else
205732d19d43Seric 	{
205832d19d43Seric 		/* parent -- wait for exit status */
2059588cad61Seric 		int st;
206032d19d43Seric 
2061588cad61Seric 		st = waitfor(pid);
2062bf9bc890Seric 		if (WIFEXITED(st))
2063bf9bc890Seric 			return (WEXITSTATUS(st));
2064588cad61Seric 		else
2065b6a0de9dSeric 		{
2066b6a0de9dSeric 			syserr("child died on signal %d", st);
2067bf9bc890Seric 			return (EX_UNAVAILABLE);
2068b6a0de9dSeric 		}
20698f9146b0Srick 		/*NOTREACHED*/
207032d19d43Seric 	}
207125a99e2eSeric }
2072ea4dc939Seric /*
2073e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2074e103b48fSeric **
2075e103b48fSeric **	The signature describes how we are going to send this -- it
2076e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2077e103b48fSeric **	an ordered list of MX hosts.
2078e103b48fSeric **
2079e103b48fSeric **	Parameters:
2080e103b48fSeric **		m -- the mailer describing this host.
2081e103b48fSeric **		host -- the host name.
2082e103b48fSeric **		e -- the current envelope.
2083e103b48fSeric **
2084e103b48fSeric **	Returns:
2085e103b48fSeric **		The signature for this host.
2086e103b48fSeric **
2087e103b48fSeric **	Side Effects:
2088e103b48fSeric **		Can tweak the symbol table.
2089e103b48fSeric */
2090e103b48fSeric 
2091e103b48fSeric char *
2092e103b48fSeric hostsignature(m, host, e)
2093e103b48fSeric 	register MAILER *m;
2094e103b48fSeric 	char *host;
2095e103b48fSeric 	ENVELOPE *e;
2096e103b48fSeric {
2097e103b48fSeric 	register char *p;
2098e103b48fSeric 	register STAB *s;
2099e103b48fSeric 	int i;
2100e103b48fSeric 	int len;
2101e103b48fSeric #ifdef NAMED_BIND
2102e103b48fSeric 	int nmx;
2103e103b48fSeric 	auto int rcode;
2104bafdc4e5Seric 	char *hp;
2105bafdc4e5Seric 	char *endp;
2106516782b4Seric 	int oldoptions;
2107e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2108e103b48fSeric #endif
2109e103b48fSeric 
2110e103b48fSeric 	/*
2111e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2112e103b48fSeric 	*/
2113e103b48fSeric 
2114e103b48fSeric 	p = m->m_mailer;
2115e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2116e103b48fSeric 	{
2117e103b48fSeric 		/* just an ordinary mailer */
2118e103b48fSeric 		return host;
2119e103b48fSeric 	}
2120e103b48fSeric 
2121e103b48fSeric 	/*
2122e103b48fSeric 	**  Look it up in the symbol table.
2123e103b48fSeric 	*/
2124e103b48fSeric 
2125e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2126e103b48fSeric 	if (s->s_hostsig != NULL)
2127e103b48fSeric 		return s->s_hostsig;
2128e103b48fSeric 
2129e103b48fSeric 	/*
2130e103b48fSeric 	**  Not already there -- create a signature.
2131e103b48fSeric 	*/
2132e103b48fSeric 
2133e103b48fSeric #ifdef NAMED_BIND
2134516782b4Seric 	if (ConfigLevel < 2)
2135516782b4Seric 	{
2136516782b4Seric 		oldoptions = _res.options;
2137516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2138516782b4Seric 	}
2139516782b4Seric 
2140bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2141bafdc4e5Seric 	{
2142bafdc4e5Seric 		endp = strchr(hp, ':');
2143bafdc4e5Seric 		if (endp != NULL)
2144bafdc4e5Seric 			*endp = '\0';
2145bafdc4e5Seric 
21467bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
21477d55540cSeric 
2148e103b48fSeric 		if (nmx <= 0)
2149e103b48fSeric 		{
2150e103b48fSeric 			register MCI *mci;
2151e103b48fSeric 			extern int errno;
2152e103b48fSeric 
2153e103b48fSeric 			/* update the connection info for this host */
2154bafdc4e5Seric 			mci = mci_get(hp, m);
2155e103b48fSeric 			mci->mci_exitstat = rcode;
2156e103b48fSeric 			mci->mci_errno = errno;
2157f170942cSeric #ifdef NAMED_BIND
2158f170942cSeric 			mci->mci_herrno = h_errno;
2159f170942cSeric #endif
2160e103b48fSeric 
2161e103b48fSeric 			/* and return the original host name as the signature */
2162bafdc4e5Seric 			nmx = 1;
2163bafdc4e5Seric 			mxhosts[0] = hp;
2164e103b48fSeric 		}
2165e103b48fSeric 
2166e103b48fSeric 		len = 0;
2167e103b48fSeric 		for (i = 0; i < nmx; i++)
2168e103b48fSeric 		{
2169e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2170e103b48fSeric 		}
2171bafdc4e5Seric 		if (s->s_hostsig != NULL)
2172bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2173bafdc4e5Seric 		p = xalloc(len);
2174bafdc4e5Seric 		if (s->s_hostsig != NULL)
2175bafdc4e5Seric 		{
2176bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2177bafdc4e5Seric 			free(s->s_hostsig);
2178bafdc4e5Seric 			s->s_hostsig = p;
2179bafdc4e5Seric 			p += strlen(p);
2180bafdc4e5Seric 			*p++ = ':';
2181bafdc4e5Seric 		}
2182bafdc4e5Seric 		else
2183bafdc4e5Seric 			s->s_hostsig = p;
2184e103b48fSeric 		for (i = 0; i < nmx; i++)
2185e103b48fSeric 		{
2186e103b48fSeric 			if (i != 0)
2187e103b48fSeric 				*p++ = ':';
2188e103b48fSeric 			strcpy(p, mxhosts[i]);
2189e103b48fSeric 			p += strlen(p);
2190e103b48fSeric 		}
2191bafdc4e5Seric 		if (endp != NULL)
2192bafdc4e5Seric 			*endp++ = ':';
2193bafdc4e5Seric 	}
2194e103b48fSeric 	makelower(s->s_hostsig);
2195516782b4Seric 	if (ConfigLevel < 2)
2196516782b4Seric 		_res.options = oldoptions;
2197e103b48fSeric #else
2198e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2199e103b48fSeric 	s->s_hostsig = host;
2200e103b48fSeric #endif
2201e103b48fSeric 	if (tTd(17, 1))
2202e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2203e103b48fSeric 	return s->s_hostsig;
2204e103b48fSeric }
2205