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*cb082c5bSeric static char sccsid[] = "@(#)deliver.c	8.42 (Berkeley) 11/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 */
66fbe2f963Seric 	CurrentLA = getla();
679c9e68d9Seric 	if (mode == SM_DEFAULT)
689c9e68d9Seric 	{
699c9e68d9Seric 		mode = e->e_sendmode;
709c9e68d9Seric 		if (mode != SM_VERIFY &&
719c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
729c9e68d9Seric 			mode = SM_QUEUE;
736103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
749c9e68d9Seric 	}
756103d9b4Seric 	else
766103d9b4Seric 		announcequeueup = FALSE;
779c9e68d9Seric 
789c9e68d9Seric 	if (tTd(13, 1))
799c9e68d9Seric 	{
800e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
810e484fe5Seric 			mode, e->e_id);
829c9e68d9Seric 		printaddr(&e->e_from, FALSE);
839c9e68d9Seric 		printf("sendqueue:\n");
849c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
859c9e68d9Seric 	}
869c9e68d9Seric 
879c9e68d9Seric 	/*
889c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
899c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
909c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
919c9e68d9Seric 	*/
929c9e68d9Seric 
939c9e68d9Seric 	CurEnv = e;
949c9e68d9Seric 
959c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
969c9e68d9Seric 	{
979c9e68d9Seric 		errno = 0;
984bb8b0fdSeric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
999c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
1004bb8b0fdSeric 			RealHostName, e->e_sendqueue->q_paddr);
1019c9e68d9Seric 		return;
1029c9e68d9Seric 	}
1039c9e68d9Seric 
104b8004690Seric 	/*
105b8004690Seric 	**  Do sender deletion.
106b8004690Seric 	**
107b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
108b8004690Seric 	**	This can happen if the name server is hosed when you
109b8004690Seric 	**	are trying to send mail.  The result is that the sender
110b8004690Seric 	**	is instantiated in the queue as a recipient.
111b8004690Seric 	*/
112b8004690Seric 
113e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
114e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1159c9e68d9Seric 	{
1169c9e68d9Seric 		if (tTd(13, 5))
1179c9e68d9Seric 		{
1189c9e68d9Seric 			printf("sendall: QDONTSEND ");
1199c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1209c9e68d9Seric 		}
1219c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1229c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1239c9e68d9Seric 	}
1249c9e68d9Seric 
125ce5531bdSeric 	/*
126ce5531bdSeric 	**  Handle alias owners.
127ce5531bdSeric 	**
128ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
129ce5531bdSeric 	**	We discard owners that are the same as the return path.
130ce5531bdSeric 	*/
131ce5531bdSeric 
132ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
133ce5531bdSeric 	{
134ce5531bdSeric 		register struct address *a;
135ce5531bdSeric 
136ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
137ce5531bdSeric 			continue;
138ce5531bdSeric 		if (a != NULL)
139ce5531bdSeric 			q->q_owner = a->q_owner;
140ce5531bdSeric 
141ce5531bdSeric 		if (q->q_owner != NULL &&
142ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
143ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
144ce5531bdSeric 			q->q_owner = NULL;
145ce5531bdSeric 	}
146ce5531bdSeric 
147ce5531bdSeric 	owner = "";
148ce5531bdSeric 	otherowners = 1;
149ce5531bdSeric 	while (owner != NULL && otherowners > 0)
150ce5531bdSeric 	{
151ce5531bdSeric 		owner = NULL;
152ce5531bdSeric 		otherowners = 0;
153ce5531bdSeric 
154ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
155ce5531bdSeric 		{
156ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
157ce5531bdSeric 				continue;
158ce5531bdSeric 
159ce5531bdSeric 			if (q->q_owner != NULL)
160ce5531bdSeric 			{
161ce5531bdSeric 				if (owner == NULL)
162ce5531bdSeric 					owner = q->q_owner;
163ce5531bdSeric 				else if (owner != q->q_owner)
164ce5531bdSeric 				{
165ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
166ce5531bdSeric 					{
167ce5531bdSeric 						/* make future comparisons cheap */
168ce5531bdSeric 						q->q_owner = owner;
169ce5531bdSeric 					}
170ce5531bdSeric 					else
171ce5531bdSeric 					{
172ce5531bdSeric 						otherowners++;
173ce5531bdSeric 					}
174ce5531bdSeric 					owner = q->q_owner;
175ce5531bdSeric 				}
176ce5531bdSeric 			}
177ce5531bdSeric 			else
178ce5531bdSeric 			{
179ce5531bdSeric 				otherowners++;
180ce5531bdSeric 			}
181ce5531bdSeric 		}
182ce5531bdSeric 
183ce5531bdSeric 		if (owner != NULL && otherowners > 0)
184ce5531bdSeric 		{
185ce5531bdSeric 			extern HDR *copyheader();
186ce5531bdSeric 			extern ADDRESS *copyqueue();
187ce5531bdSeric 
188ce5531bdSeric 			/*
189ce5531bdSeric 			**  Split this envelope into two.
190ce5531bdSeric 			*/
191ce5531bdSeric 
192ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
193ce5531bdSeric 			*ee = *e;
194ce5531bdSeric 			ee->e_id = NULL;
195ce5531bdSeric 			(void) queuename(ee, '\0');
196ce5531bdSeric 
197ce5531bdSeric 			if (tTd(13, 1))
198ce5531bdSeric 				printf("sendall: split %s into %s\n",
199ce5531bdSeric 					e->e_id, ee->e_id);
200ce5531bdSeric 
201ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
202ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
203ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
204ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
205ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
206ce5531bdSeric 			if (tTd(13, 5))
207ce5531bdSeric 			{
208ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
209ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
210ce5531bdSeric 			}
211ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
212ce5531bdSeric 			ee->e_dfp = NULL;
213ce5531bdSeric 			ee->e_xfp = NULL;
214ce5531bdSeric 			ee->e_df = NULL;
215ce5531bdSeric 			ee->e_errormode = EM_MAIL;
216ce5531bdSeric 			ee->e_sibling = splitenv;
217ce5531bdSeric 			splitenv = ee;
218ce5531bdSeric 
219ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
220ce5531bdSeric 				if (q->q_owner == owner)
221ce5531bdSeric 					q->q_flags |= QDONTSEND;
222ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
223ce5531bdSeric 				if (q->q_owner != owner)
224ce5531bdSeric 					q->q_flags |= QDONTSEND;
225ce5531bdSeric 
226ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
227ce5531bdSeric 			{
228ce5531bdSeric 				ee->e_dfp = NULL;
229da662164Seric 				ee->e_df = queuename(ee, 'd');
230da662164Seric 				ee->e_df = newstr(ee->e_df);
231ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
232ce5531bdSeric 				{
233ce5531bdSeric 					syserr("sendall: link(%s, %s)",
234ce5531bdSeric 						e->e_df, ee->e_df);
235ce5531bdSeric 				}
236ce5531bdSeric 			}
237ce5531bdSeric 
238ce5531bdSeric 			if (mode != SM_VERIFY)
239ce5531bdSeric 				openxscript(ee);
240ce5531bdSeric #ifdef LOG
241ce5531bdSeric 			if (LogLevel > 4)
242ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
243ce5531bdSeric 					ee->e_id, e->e_id);
244ce5531bdSeric #endif
245ce5531bdSeric 		}
246ce5531bdSeric 	}
247ce5531bdSeric 
248ce5531bdSeric 	if (owner != NULL)
249ce5531bdSeric 	{
250ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
251ce5531bdSeric 		if (tTd(13, 5))
252ce5531bdSeric 		{
253ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
254ce5531bdSeric 			printaddr(&e->e_from, FALSE);
255ce5531bdSeric 		}
256ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
257ce5531bdSeric 		e->e_errormode = EM_MAIL;
258ce5531bdSeric 	}
259ce5531bdSeric 
260c1ac89b1Seric # ifdef QUEUE
261c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
262c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
263c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
264c1ac89b1Seric 	{
265c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2666103d9b4Seric 		queueup(e, TRUE, announcequeueup);
267ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2686103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
269c1ac89b1Seric 	}
270c1ac89b1Seric #endif /* QUEUE */
271c1ac89b1Seric 
272ce5531bdSeric 	if (splitenv != NULL)
273ce5531bdSeric 	{
274ce5531bdSeric 		if (tTd(13, 1))
275ce5531bdSeric 		{
276ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
277ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
278ce5531bdSeric 		}
279ce5531bdSeric 
280ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
281ce5531bdSeric 		{
282ce5531bdSeric 			CurEnv = ee;
283ce5531bdSeric 			sendenvelope(ee, mode);
284ce5531bdSeric 		}
285ce5531bdSeric 
286ce5531bdSeric 		CurEnv = e;
287ce5531bdSeric 	}
288ce5531bdSeric 	sendenvelope(e, mode);
289ce5531bdSeric 
290ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
291ce5531bdSeric 		dropenvelope(splitenv);
292ce5531bdSeric }
293ce5531bdSeric 
294ce5531bdSeric sendenvelope(e, mode)
295ce5531bdSeric 	register ENVELOPE *e;
296ce5531bdSeric 	char mode;
297ce5531bdSeric {
298ce5531bdSeric 	bool oldverbose;
299ce5531bdSeric 	int pid;
300ce5531bdSeric 	register ADDRESS *q;
3016b2765c6Seric 	char *qf;
3026b2765c6Seric 	char *id;
303ce5531bdSeric 
3047880f3e4Seric 	/*
3057880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3067880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3077880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3087880f3e4Seric 	**  addresses to be sent.
3097880f3e4Seric 	*/
3107880f3e4Seric 
3117880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
3127880f3e4Seric 	{
3137880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3147880f3e4Seric 		return;
3157880f3e4Seric 	}
3167880f3e4Seric 
317ce5531bdSeric 	oldverbose = Verbose;
318c1ac89b1Seric 	switch (mode)
319c1ac89b1Seric 	{
320c1ac89b1Seric 	  case SM_VERIFY:
321c1ac89b1Seric 		Verbose = TRUE;
322c1ac89b1Seric 		break;
323c1ac89b1Seric 
324c1ac89b1Seric 	  case SM_QUEUE:
325c1ac89b1Seric   queueonly:
326c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
327c1ac89b1Seric 		return;
328c1ac89b1Seric 
329c1ac89b1Seric 	  case SM_FORK:
330c1ac89b1Seric 		if (e->e_xfp != NULL)
331c1ac89b1Seric 			(void) fflush(e->e_xfp);
332c1ac89b1Seric 
3332b9178d3Seric # ifndef HASFLOCK
334c1ac89b1Seric 		/*
3356b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3366b2765c6Seric 		**  the lock is owned by a process, not by an open file
3376b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3386b2765c6Seric 		**  then restart from scratch in the child.
339c1ac89b1Seric 		*/
340c1ac89b1Seric 
3416b2765c6Seric 		/* save id for future use */
3426b2765c6Seric 		id = e->e_id;
3436b2765c6Seric 
3446b2765c6Seric 		/* now drop the envelope in the parent */
3456b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3466b2765c6Seric 		dropenvelope(e);
3476b2765c6Seric 
3486b2765c6Seric 		/* and reacquire in the child */
3496b2765c6Seric 		(void) dowork(id, TRUE, FALSE, e);
3506b2765c6Seric 
3516b2765c6Seric 		return;
3526b2765c6Seric 
3536b2765c6Seric # else /* HASFLOCK */
354c1ac89b1Seric 
355c1ac89b1Seric 		pid = fork();
356c1ac89b1Seric 		if (pid < 0)
357c1ac89b1Seric 		{
358c1ac89b1Seric 			goto queueonly;
359c1ac89b1Seric 		}
360c1ac89b1Seric 		else if (pid > 0)
361c1ac89b1Seric 		{
3620e484fe5Seric 			/* be sure we leave the temp files to our child */
3630e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3640e484fe5Seric 			if (e->e_lockfp != NULL)
3650e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3660e484fe5Seric 			e->e_lockfp = NULL;
3670e484fe5Seric 
3680e484fe5Seric 			/* close any random open files in the envelope */
3690e484fe5Seric 			closexscript(e);
3700e484fe5Seric 			if (e->e_dfp != NULL)
3710e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3720e484fe5Seric 			e->e_dfp = NULL;
3730e484fe5Seric 			e->e_id = e->e_df = NULL;
374c1ac89b1Seric 			return;
375c1ac89b1Seric 		}
376c1ac89b1Seric 
377c1ac89b1Seric 		/* double fork to avoid zombies */
378c1ac89b1Seric 		if (fork() > 0)
379c1ac89b1Seric 			exit(EX_OK);
380c1ac89b1Seric 
381c1ac89b1Seric 		/* be sure we are immune from the terminal */
3827880f3e4Seric 		disconnect(1, e);
383c1ac89b1Seric 
384c1ac89b1Seric 		/*
385c1ac89b1Seric 		**  Close any cached connections.
386c1ac89b1Seric 		**
387c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
388c1ac89b1Seric 		**	still knows about the connection.
389c1ac89b1Seric 		**
390c1ac89b1Seric 		**	This should only happen when delivering an error
391c1ac89b1Seric 		**	message.
392c1ac89b1Seric 		*/
393c1ac89b1Seric 
394c1ac89b1Seric 		mci_flush(FALSE, NULL);
395c1ac89b1Seric 
3966b2765c6Seric # endif /* HASFLOCK */
3976b2765c6Seric 
398c1ac89b1Seric 		break;
399c1ac89b1Seric 	}
400c1ac89b1Seric 
401c1ac89b1Seric 	/*
4029c9e68d9Seric 	**  Run through the list and send everything.
4035288e21aSeric 	**
4045288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4055288e21aSeric 	**	result in returned mail.
4069c9e68d9Seric 	*/
4079c9e68d9Seric 
4089c9e68d9Seric 	e->e_nsent = 0;
4095288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
410faad2b16Seric 
411faad2b16Seric 	/* now run through the queue */
4129c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4139c9e68d9Seric 	{
414fdc75a0fSeric #ifdef XDEBUG
415fdc75a0fSeric 		char wbuf[MAXNAME + 20];
416fdc75a0fSeric 
417fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
418fdc75a0fSeric 		checkfd012(wbuf);
419fdc75a0fSeric #endif
4209c9e68d9Seric 		if (mode == SM_VERIFY)
4219c9e68d9Seric 		{
4229c9e68d9Seric 			e->e_to = q->q_paddr;
4239c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4248dfca105Seric 			{
4258dfca105Seric 				message("deliverable: mailer %s, host %s, user %s",
4268dfca105Seric 					q->q_mailer->m_name,
4278dfca105Seric 					q->q_host,
4288dfca105Seric 					q->q_user);
4298dfca105Seric 			}
4309c9e68d9Seric 		}
4319c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4329c9e68d9Seric 		{
4339c9e68d9Seric # ifdef QUEUE
4349c9e68d9Seric 			/*
4359c9e68d9Seric 			**  Checkpoint the send list every few addresses
4369c9e68d9Seric 			*/
4379c9e68d9Seric 
4389c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4399c9e68d9Seric 			{
4409c9e68d9Seric 				queueup(e, TRUE, FALSE);
4419c9e68d9Seric 				e->e_nsent = 0;
4429c9e68d9Seric 			}
4439c9e68d9Seric # endif /* QUEUE */
4449c9e68d9Seric 			(void) deliver(e, q);
4459c9e68d9Seric 		}
4469c9e68d9Seric 	}
4479c9e68d9Seric 	Verbose = oldverbose;
4489c9e68d9Seric 
449fdc75a0fSeric #ifdef XDEBUG
450fdc75a0fSeric 	checkfd012("end of sendenvelope");
451fdc75a0fSeric #endif
452fdc75a0fSeric 
4539c9e68d9Seric 	if (mode == SM_FORK)
4549c9e68d9Seric 		finis();
4559c9e68d9Seric }
4569c9e68d9Seric /*
4579c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4589c9e68d9Seric **
4599c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4609c9e68d9Seric **	two processes on the same stack!!!
4619c9e68d9Seric **
4629c9e68d9Seric **	Parameters:
4639c9e68d9Seric **		none.
4649c9e68d9Seric **
4659c9e68d9Seric **	Returns:
4669c9e68d9Seric **		From a macro???  You've got to be kidding!
4679c9e68d9Seric **
4689c9e68d9Seric **	Side Effects:
4699c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4709c9e68d9Seric **			pid of child in parent, zero in child.
4719c9e68d9Seric **			-1 on unrecoverable error.
4729c9e68d9Seric **
4739c9e68d9Seric **	Notes:
4749c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4759c9e68d9Seric **		vfork for you.....
4769c9e68d9Seric */
4779c9e68d9Seric 
4789c9e68d9Seric # define NFORKTRIES	5
4799c9e68d9Seric 
4809c9e68d9Seric # ifndef FORK
4819c9e68d9Seric # define FORK	fork
4829c9e68d9Seric # endif
4839c9e68d9Seric 
4849c9e68d9Seric # define DOFORK(fORKfN) \
4859c9e68d9Seric {\
4869c9e68d9Seric 	register int i;\
4879c9e68d9Seric \
4889c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4899c9e68d9Seric 	{\
4909c9e68d9Seric 		pid = fORKfN();\
4919c9e68d9Seric 		if (pid >= 0)\
4929c9e68d9Seric 			break;\
4939c9e68d9Seric 		if (i > 0)\
4949c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
4959c9e68d9Seric 	}\
4969c9e68d9Seric }
4979c9e68d9Seric /*
4989c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
4999c9e68d9Seric **
5009c9e68d9Seric **	Parameters:
5019c9e68d9Seric **		none.
5029c9e68d9Seric **
5039c9e68d9Seric **	Returns:
5049c9e68d9Seric **		pid of child in parent.
5059c9e68d9Seric **		zero in child.
5069c9e68d9Seric **		-1 on error.
5079c9e68d9Seric **
5089c9e68d9Seric **	Side Effects:
5099c9e68d9Seric **		returns twice, once in parent and once in child.
5109c9e68d9Seric */
5119c9e68d9Seric 
5129c9e68d9Seric dofork()
5139c9e68d9Seric {
5149c9e68d9Seric 	register int pid;
5159c9e68d9Seric 
5169c9e68d9Seric 	DOFORK(fork);
5179c9e68d9Seric 	return (pid);
5189c9e68d9Seric }
5199c9e68d9Seric /*
52013bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
52113bbc08cSeric **
52213bbc08cSeric **	This routine delivers to everyone on the same host as the
52313bbc08cSeric **	user on the head of the list.  It is clever about mailers
52413bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
52513bbc08cSeric **	that it will deliver to all these addresses however -- so
52613bbc08cSeric **	deliver should be called once for each address on the
52713bbc08cSeric **	list.
52825a99e2eSeric **
52925a99e2eSeric **	Parameters:
530588cad61Seric **		e -- the envelope to deliver.
531c77d1c25Seric **		firstto -- head of the address list to deliver to.
53225a99e2eSeric **
53325a99e2eSeric **	Returns:
53425a99e2eSeric **		zero -- successfully delivered.
53525a99e2eSeric **		else -- some failure, see ExitStat for more info.
53625a99e2eSeric **
53725a99e2eSeric **	Side Effects:
53825a99e2eSeric **		The standard input is passed off to someone.
53925a99e2eSeric */
54025a99e2eSeric 
541588cad61Seric deliver(e, firstto)
542588cad61Seric 	register ENVELOPE *e;
543c77d1c25Seric 	ADDRESS *firstto;
54425a99e2eSeric {
54578442df3Seric 	char *host;			/* host being sent to */
54678442df3Seric 	char *user;			/* user being sent to */
54725a99e2eSeric 	char **pvp;
5485dfc646bSeric 	register char **mvp;
54925a99e2eSeric 	register char *p;
550588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5516259796dSeric 	ADDRESS *ctladdr;
552b31e7f2bSeric 	register MCI *mci;
553c77d1c25Seric 	register ADDRESS *to = firstto;
554c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
555772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
556911693bfSbostic 	int rcode;			/* response code */
557e103b48fSeric 	char *firstsig;			/* signature of firstto */
5589c9e68d9Seric 	int pid;
5599c9e68d9Seric 	char *curhost;
5609c9e68d9Seric 	int mpvect[2];
5619c9e68d9Seric 	int rpvect[2];
562ee6bf8dfSeric 	char *pv[MAXPV+1];
563579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
564ee6bf8dfSeric 	char buf[MAXNAME];
565c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
566fabb3bd4Seric 	extern int checkcompat();
567df106f0bSeric 	extern char SmtpError[];
56825a99e2eSeric 
56935490626Seric 	errno = 0;
570ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5715dfc646bSeric 		return (0);
57225a99e2eSeric 
573134746fbSeric #ifdef NAMED_BIND
574912a731aSbostic 	/* unless interactive, try twice, over a minute */
575912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
576912a731aSbostic 		_res.retrans = 30;
577912a731aSbostic 		_res.retry = 2;
578912a731aSbostic 	}
579d4bd8f0eSbostic #endif
580912a731aSbostic 
58151552439Seric 	m = to->q_mailer;
58251552439Seric 	host = to->q_host;
583c9be6216Seric 	CurEnv = e;			/* just in case */
5844384d521Seric 	e->e_statmsg = NULL;
585df106f0bSeric 	SmtpError[0] = '\0';
58651552439Seric 
5876ef48975Seric 	if (tTd(10, 1))
5885dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
58951552439Seric 			m->m_mno, host, to->q_user);
590ab81ee53Seric 	if (tTd(10, 100))
591ab81ee53Seric 		printopenfds(FALSE);
592f3dbc832Seric 
593f3dbc832Seric 	/*
594f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
595f3dbc832Seric 	**  connections now, just mark these addresses and return.
596f3dbc832Seric 	**	This is useful if we want to batch connections to
597f3dbc832Seric 	**	reduce load.  This will cause the messages to be
598f3dbc832Seric 	**	queued up, and a daemon will come along to send the
599f3dbc832Seric 	**	messages later.
600f3dbc832Seric 	**		This should be on a per-mailer basis.
601f3dbc832Seric 	*/
602f3dbc832Seric 
60379aa5155Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
604f3dbc832Seric 	{
605f3dbc832Seric 		for (; to != NULL; to = to->q_next)
606f4560e80Seric 		{
607ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
608c6e18ac5Seric 			    to->q_mailer != m)
609f4560e80Seric 				continue;
610268a25e1Seric 			to->q_flags |= QQUEUEUP;
611588cad61Seric 			e->e_to = to->q_paddr;
61208b25121Seric 			message("queued");
6132f624c86Seric 			if (LogLevel > 8)
6144dee0003Seric 				logdelivery(m, NULL, "queued", NULL, e);
615f4560e80Seric 		}
616588cad61Seric 		e->e_to = NULL;
617f3dbc832Seric 		return (0);
618f3dbc832Seric 	}
619f3dbc832Seric 
62025a99e2eSeric 	/*
6215dfc646bSeric 	**  Do initial argv setup.
6225dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6235dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6245dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6255dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6265dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6273bea8136Seric 	**		The from address rewrite is expected to make
6283bea8136Seric 	**		the address relative to the other end.
6295dfc646bSeric 	*/
6305dfc646bSeric 
63178442df3Seric 	/* rewrite from address, using rewriting rules */
632efe54562Seric 	rcode = EX_OK;
633efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
634efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
635efe54562Seric 					   &rcode, e));
636ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
637588cad61Seric 	define('h', host, e);			/* to host */
6385dfc646bSeric 	Errors = 0;
6395dfc646bSeric 	pvp = pv;
6405dfc646bSeric 	*pvp++ = m->m_argv[0];
6415dfc646bSeric 
6425dfc646bSeric 	/* insert -f or -r flag as appropriate */
64357fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6445dfc646bSeric 	{
64557fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6465dfc646bSeric 			*pvp++ = "-f";
6475dfc646bSeric 		else
6485dfc646bSeric 			*pvp++ = "-r";
649c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6505dfc646bSeric 	}
6515dfc646bSeric 
6525dfc646bSeric 	/*
6535dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6545dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6555dfc646bSeric 	**  be one of these, and there are only a few more slots
6565dfc646bSeric 	**  in the pv after it.
6575dfc646bSeric 	*/
6585dfc646bSeric 
6595dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6605dfc646bSeric 	{
6612bc47524Seric 		/* can't use strchr here because of sign extension problems */
6622bc47524Seric 		while (*p != '\0')
6632bc47524Seric 		{
6642bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6652bc47524Seric 			{
6662bc47524Seric 				if (*p == 'u')
6675dfc646bSeric 					break;
6682bc47524Seric 			}
6692bc47524Seric 		}
6702bc47524Seric 
6712bc47524Seric 		if (*p != '\0')
6725dfc646bSeric 			break;
6735dfc646bSeric 
6745dfc646bSeric 		/* this entry is safe -- go ahead and process it */
675588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6765dfc646bSeric 		*pvp++ = newstr(buf);
6775dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6785dfc646bSeric 		{
67908b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6805dfc646bSeric 			return (-1);
6815dfc646bSeric 		}
6825dfc646bSeric 	}
683c579ef51Seric 
68433db8731Seric 	/*
68533db8731Seric 	**  If we have no substitution for the user name in the argument
68633db8731Seric 	**  list, we know that we must supply the names otherwise -- and
68733db8731Seric 	**  SMTP is the answer!!
68833db8731Seric 	*/
68933db8731Seric 
6905dfc646bSeric 	if (*mvp == NULL)
691c579ef51Seric 	{
692c579ef51Seric 		/* running SMTP */
6932c7e1b8dSeric # ifdef SMTP
694c579ef51Seric 		clever = TRUE;
695c579ef51Seric 		*pvp = NULL;
6966c2c3107Seric # else /* SMTP */
69733db8731Seric 		/* oops!  we don't implement SMTP */
698df106f0bSeric 		syserr("554 SMTP style mailer not implemented");
6992c7e1b8dSeric 		return (EX_SOFTWARE);
7006c2c3107Seric # endif /* SMTP */
701c579ef51Seric 	}
7025dfc646bSeric 
7035dfc646bSeric 	/*
7045dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7055dfc646bSeric 	**  run through our address list and append all the addresses
7065dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7075dfc646bSeric 	**  always send another copy later.
7085dfc646bSeric 	*/
7095dfc646bSeric 
7105dfc646bSeric 	tobuf[0] = '\0';
711588cad61Seric 	e->e_to = tobuf;
7126259796dSeric 	ctladdr = NULL;
713e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7145dfc646bSeric 	for (; to != NULL; to = to->q_next)
7155dfc646bSeric 	{
7165dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
71757fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7185dfc646bSeric 			break;
7195dfc646bSeric 
7205dfc646bSeric 		/* if already sent or not for this host, don't send */
721ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
722e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
723e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7245dfc646bSeric 			continue;
7256259796dSeric 
7264b22ea87Seric 		/* avoid overflowing tobuf */
727aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7284b22ea87Seric 			break;
7294b22ea87Seric 
7306ef48975Seric 		if (tTd(10, 1))
731772e6e50Seric 		{
732772e6e50Seric 			printf("\nsend to ");
733772e6e50Seric 			printaddr(to, FALSE);
734772e6e50Seric 		}
735772e6e50Seric 
7366259796dSeric 		/* compute effective uid/gid when sending */
7377da1035fSeric 		if (to->q_mailer == ProgMailer)
7386259796dSeric 			ctladdr = getctladdr(to);
7396259796dSeric 
7405dfc646bSeric 		user = to->q_user;
741588cad61Seric 		e->e_to = to->q_paddr;
74275f1ade9Seric 		if (tTd(10, 5))
74375f1ade9Seric 		{
74475f1ade9Seric 			printf("deliver: QDONTSEND ");
74575f1ade9Seric 			printaddr(to, FALSE);
74675f1ade9Seric 		}
747ee4b0922Seric 		to->q_flags |= QDONTSEND;
7485dfc646bSeric 
7495dfc646bSeric 		/*
7505dfc646bSeric 		**  Check to see that these people are allowed to
7515dfc646bSeric 		**  talk to each other.
7522a6e0786Seric 		*/
7532a6e0786Seric 
75469582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
75569582d2fSeric 		{
75669582d2fSeric 			NoReturn = TRUE;
75708b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
7584dee0003Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e);
75969582d2fSeric 			continue;
76069582d2fSeric 		}
761fabb3bd4Seric 		rcode = checkcompat(to, e);
7621793c9c5Seric 		if (rcode != EX_OK)
7635dfc646bSeric 		{
7641000c37aSeric 			markfailure(e, to, rcode);
7654dee0003Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
7665dfc646bSeric 			continue;
7675dfc646bSeric 		}
7682a6e0786Seric 
7692a6e0786Seric 		/*
7709ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7719ec9501bSeric 		**	about them.
77225a99e2eSeric 		*/
77325a99e2eSeric 
77457fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
77525a99e2eSeric 		{
7761d8f1806Seric 			stripquotes(user);
7771d8f1806Seric 			stripquotes(host);
77825a99e2eSeric 		}
77925a99e2eSeric 
780cdb828c5Seric 		/* hack attack -- delivermail compatibility */
781cdb828c5Seric 		if (m == ProgMailer && *user == '|')
782cdb828c5Seric 			user++;
783cdb828c5Seric 
78425a99e2eSeric 		/*
7853efaed6eSeric 		**  If an error message has already been given, don't
7863efaed6eSeric 		**	bother to send to this address.
7873efaed6eSeric 		**
7883efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7893efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
7903efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
7913efaed6eSeric 		*/
7923efaed6eSeric 
7936cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7943efaed6eSeric 			continue;
7953efaed6eSeric 
796f2fec898Seric 		/* save statistics.... */
797588cad61Seric 		markstats(e, to);
798f2fec898Seric 
7993efaed6eSeric 		/*
80025a99e2eSeric 		**  See if this user name is "special".
80125a99e2eSeric 		**	If the user name has a slash in it, assume that this
80251552439Seric 		**	is a file -- send it off without further ado.  Note
80351552439Seric 		**	that this type of addresses is not processed along
80451552439Seric 		**	with the others, so we fudge on the To person.
80525a99e2eSeric 		*/
80625a99e2eSeric 
8072c017f8dSeric 		if (m == FileMailer)
80825a99e2eSeric 		{
8094dee0003Seric 			ADDRESS *caddr = getctladdr(to);
8104dee0003Seric 
8114dee0003Seric 			rcode = mailfile(user, caddr, e);
8124dee0003Seric 			giveresponse(rcode, m, NULL, caddr, e);
813dde5acadSeric 			if (rcode == EX_OK)
814dde5acadSeric 				to->q_flags |= QSENT;
8155dfc646bSeric 			continue;
81625a99e2eSeric 		}
81725a99e2eSeric 
81813bbc08cSeric 		/*
81913bbc08cSeric 		**  Address is verified -- add this user to mailer
82013bbc08cSeric 		**  argv, and add it to the print list of recipients.
82113bbc08cSeric 		*/
82213bbc08cSeric 
823508daeccSeric 		/* link together the chain of recipients */
824508daeccSeric 		to->q_tchain = tochain;
825508daeccSeric 		tochain = to;
826508daeccSeric 
8275dfc646bSeric 		/* create list of users for error messages */
828db8841e9Seric 		(void) strcat(tobuf, ",");
829db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
830588cad61Seric 		define('u', user, e);		/* to user */
831b6f89e6eSeric 		p = to->q_home;
832b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
833b6f89e6eSeric 			p = ctladdr->q_home;
834b6f89e6eSeric 		define('z', p, e);	/* user's home */
8355dfc646bSeric 
836c579ef51Seric 		/*
837508daeccSeric 		**  Expand out this user into argument list.
838c579ef51Seric 		*/
839c579ef51Seric 
840508daeccSeric 		if (!clever)
841c579ef51Seric 		{
842588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8435dfc646bSeric 			*pvp++ = newstr(buf);
8445dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8455dfc646bSeric 			{
8465dfc646bSeric 				/* allow some space for trailing parms */
8475dfc646bSeric 				break;
8485dfc646bSeric 			}
8495dfc646bSeric 		}
850c579ef51Seric 	}
8515dfc646bSeric 
852145b49b1Seric 	/* see if any addresses still exist */
853145b49b1Seric 	if (tobuf[0] == '\0')
854c579ef51Seric 	{
855588cad61Seric 		define('g', (char *) NULL, e);
856145b49b1Seric 		return (0);
857c579ef51Seric 	}
858145b49b1Seric 
8595dfc646bSeric 	/* print out messages as full list */
86063780dbdSeric 	e->e_to = tobuf + 1;
8615dfc646bSeric 
8625dfc646bSeric 	/*
8635dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8645dfc646bSeric 	*/
8655dfc646bSeric 
866c579ef51Seric 	while (!clever && *++mvp != NULL)
8675dfc646bSeric 	{
868588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8695dfc646bSeric 		*pvp++ = newstr(buf);
8705dfc646bSeric 		if (pvp >= &pv[MAXPV])
87108b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8725dfc646bSeric 	}
8735dfc646bSeric 	*pvp++ = NULL;
8745dfc646bSeric 
87525a99e2eSeric 	/*
87625a99e2eSeric 	**  Call the mailer.
8776328bdf7Seric 	**	The argument vector gets built, pipes
87825a99e2eSeric 	**	are created as necessary, and we fork & exec as
8796328bdf7Seric 	**	appropriate.
880c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
88125a99e2eSeric 	*/
88225a99e2eSeric 
8837ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
88486b26461Seric 		ctladdr = &e->e_from;
885134746fbSeric #ifdef NAMED_BIND
8862bcc6d2dSeric 	if (ConfigLevel < 2)
887912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
888134746fbSeric #endif
8899c9e68d9Seric 
8909c9e68d9Seric 	if (tTd(11, 1))
891134746fbSeric 	{
8929c9e68d9Seric 		printf("openmailer:");
8939c9e68d9Seric 		printav(pv);
8949c9e68d9Seric 	}
8959c9e68d9Seric 	errno = 0;
8969c9e68d9Seric 
8979c9e68d9Seric 	CurHostName = m->m_mailer;
8989c9e68d9Seric 
8999c9e68d9Seric 	/*
9009c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9019c9e68d9Seric 	**  connection.
9029c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9039c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9049c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9059c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9069c9e68d9Seric 	*/
9079c9e68d9Seric 
9089c9e68d9Seric 	curhost = NULL;
909c931b82bSeric 	SmtpPhase = NULL;
910df106f0bSeric 	mci = NULL;
9119c9e68d9Seric 
9127febfd66Seric #ifdef XDEBUG
9137febfd66Seric 	{
9147febfd66Seric 		char wbuf[MAXLINE];
9157febfd66Seric 
9167febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9177febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
9187febfd66Seric 		checkfd012(wbuf);
9197febfd66Seric 	}
9207febfd66Seric #endif
9217febfd66Seric 
9229c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9239c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9249c9e68d9Seric 	{
9259c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9269c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9279c9e68d9Seric 		mci->mci_in = stdin;
9289c9e68d9Seric 		mci->mci_out = stdout;
9299c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9309c9e68d9Seric 		mci->mci_mailer = m;
9319c9e68d9Seric 	}
9329c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9339c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9349c9e68d9Seric 	{
9359c9e68d9Seric #ifdef DAEMON
9369c9e68d9Seric 		register int i;
9379c9e68d9Seric 		register u_short port;
9389c9e68d9Seric 
939a5d44642Seric 		if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
940a5d44642Seric 		{
941a5d44642Seric 			syserr("null host name for %s mailer", m->m_mailer);
942a5d44642Seric 			rcode = EX_CONFIG;
943a5d44642Seric 			goto give_up;
944a5d44642Seric 		}
945a5d44642Seric 
9469c9e68d9Seric 		CurHostName = pv[1];
9479c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9489c9e68d9Seric 
9499c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9509c9e68d9Seric 		{
951a5d44642Seric 			syserr("null host signature for %s", pv[1]);
952845e533cSeric 			rcode = EX_OSERR;
953b31e7f2bSeric 			goto give_up;
954b31e7f2bSeric 		}
9559c9e68d9Seric 
9569c9e68d9Seric 		if (!clever)
9579c9e68d9Seric 		{
9589c9e68d9Seric 			syserr("554 non-clever IPC");
959df106f0bSeric 			rcode = EX_CONFIG;
9609c9e68d9Seric 			goto give_up;
9619c9e68d9Seric 		}
9629c9e68d9Seric 		if (pv[2] != NULL)
9639c9e68d9Seric 			port = atoi(pv[2]);
9649c9e68d9Seric 		else
9659c9e68d9Seric 			port = 0;
9669c9e68d9Seric tryhost:
9679c9e68d9Seric 		while (*curhost != '\0')
9689c9e68d9Seric 		{
9699c9e68d9Seric 			register char *p;
9709c9e68d9Seric 			static char hostbuf[MAXNAME];
9719c9e68d9Seric 
9729c9e68d9Seric 			/* pull the next host from the signature */
9739c9e68d9Seric 			p = strchr(curhost, ':');
9749c9e68d9Seric 			if (p == NULL)
9759c9e68d9Seric 				p = &curhost[strlen(curhost)];
976df106f0bSeric 			if (p == curhost)
977df106f0bSeric 			{
978df106f0bSeric 				syserr("deliver: null host name in signature");
9798087428aSeric 				curhost++;
980df106f0bSeric 				continue;
981df106f0bSeric 			}
9829c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9839c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9849c9e68d9Seric 			if (*p != '\0')
9859c9e68d9Seric 				p++;
9869c9e68d9Seric 			curhost = p;
9879c9e68d9Seric 
9889c9e68d9Seric 			/* see if we already know that this host is fried */
9899c9e68d9Seric 			CurHostName = hostbuf;
9909c9e68d9Seric 			mci = mci_get(hostbuf, m);
9919c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9929c9e68d9Seric 			{
9939c9e68d9Seric 				if (tTd(11, 1))
9949c9e68d9Seric 				{
9959c9e68d9Seric 					printf("openmailer: ");
9964052916eSeric 					mci_dump(mci, FALSE);
9979c9e68d9Seric 				}
9989c9e68d9Seric 				CurHostName = mci->mci_host;
9999c9e68d9Seric 				break;
10009c9e68d9Seric 			}
10019c9e68d9Seric 			mci->mci_mailer = m;
10029c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
10039c9e68d9Seric 				continue;
10049c9e68d9Seric 
10059c9e68d9Seric 			/* try the connection */
10069c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
10079c9e68d9Seric 			message("Connecting to %s (%s)...",
10089c9e68d9Seric 				hostbuf, m->m_name);
10099c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
10109c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
10119c9e68d9Seric 			mci->mci_exitstat = i;
10129c9e68d9Seric 			mci->mci_errno = errno;
1013f170942cSeric #ifdef NAMED_BIND
1014f170942cSeric 			mci->mci_herrno = h_errno;
1015f170942cSeric #endif
10169c9e68d9Seric 			if (i == EX_OK)
10179c9e68d9Seric 			{
10189c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10199c9e68d9Seric 				mci_cache(mci);
1020f170942cSeric 				if (TrafficLogFile != NULL)
1021f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1022f170942cSeric 						getpid(), hostbuf);
10239c9e68d9Seric 				break;
10249c9e68d9Seric 			}
10259c9e68d9Seric 			else if (tTd(11, 1))
10269c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10279c9e68d9Seric 					i, errno);
10289c9e68d9Seric 
10299c9e68d9Seric 			/* enter status of this host */
10309c9e68d9Seric 			setstat(i);
1031df106f0bSeric 
1032df106f0bSeric 			/* should print some message here for -v mode */
1033df106f0bSeric 		}
1034df106f0bSeric 		if (mci == NULL)
1035df106f0bSeric 		{
1036df106f0bSeric 			syserr("deliver: no host name");
1037df106f0bSeric 			rcode = EX_OSERR;
1038df106f0bSeric 			goto give_up;
10399c9e68d9Seric 		}
10409c9e68d9Seric 		mci->mci_pid = 0;
10419c9e68d9Seric #else /* no DAEMON */
10429c9e68d9Seric 		syserr("554 openmailer: no IPC");
10439c9e68d9Seric 		if (tTd(11, 1))
10449c9e68d9Seric 			printf("openmailer: NULL\n");
1045df106f0bSeric 		rcode = EX_UNAVAILABLE;
1046df106f0bSeric 		goto give_up;
10479c9e68d9Seric #endif /* DAEMON */
10489c9e68d9Seric 	}
10499c9e68d9Seric 	else
10509c9e68d9Seric 	{
1051f170942cSeric 		if (TrafficLogFile != NULL)
10520e3bfef5Seric 		{
1053f170942cSeric 			char **av;
1054f170942cSeric 
1055f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1056f170942cSeric 			for (av = pv; *av != NULL; av++)
1057f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1058f170942cSeric 			fprintf(TrafficLogFile, "\n");
10596fe8c3bcSeric 		}
10606fe8c3bcSeric 
10619c9e68d9Seric 		/* create a pipe to shove the mail through */
10629c9e68d9Seric 		if (pipe(mpvect) < 0)
10639c9e68d9Seric 		{
10640e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10650e3bfef5Seric 				e->e_to, m->m_name);
10669c9e68d9Seric 			if (tTd(11, 1))
10679c9e68d9Seric 				printf("openmailer: NULL\n");
10689c9e68d9Seric 			rcode = EX_OSERR;
10699c9e68d9Seric 			goto give_up;
10709c9e68d9Seric 		}
10719c9e68d9Seric 
10729c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10739c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10749c9e68d9Seric 		{
10750e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10760e3bfef5Seric 				e->e_to, m->m_name);
10779c9e68d9Seric 			(void) close(mpvect[0]);
10789c9e68d9Seric 			(void) close(mpvect[1]);
10799c9e68d9Seric 			if (tTd(11, 1))
10809c9e68d9Seric 				printf("openmailer: NULL\n");
10819c9e68d9Seric 			rcode = EX_OSERR;
10829c9e68d9Seric 			goto give_up;
10839c9e68d9Seric 		}
10849c9e68d9Seric 
10859c9e68d9Seric 		/*
10869c9e68d9Seric 		**  Actually fork the mailer process.
10879c9e68d9Seric 		**	DOFORK is clever about retrying.
10889c9e68d9Seric 		**
10899c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10909c9e68d9Seric 		**	around so that endmail will get it.
10919c9e68d9Seric 		*/
10929c9e68d9Seric 
10939c9e68d9Seric 		if (e->e_xfp != NULL)
10949c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10959c9e68d9Seric 		(void) fflush(stdout);
10969c9e68d9Seric # ifdef SIGCHLD
10972b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
10989c9e68d9Seric # endif /* SIGCHLD */
10999c9e68d9Seric 		DOFORK(FORK);
11009c9e68d9Seric 		/* pid is set by DOFORK */
11019c9e68d9Seric 		if (pid < 0)
11029c9e68d9Seric 		{
11039c9e68d9Seric 			/* failure */
11040e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
11050e3bfef5Seric 				e->e_to, m->m_name);
11069c9e68d9Seric 			(void) close(mpvect[0]);
11079c9e68d9Seric 			(void) close(mpvect[1]);
11089c9e68d9Seric 			if (clever)
11099c9e68d9Seric 			{
11109c9e68d9Seric 				(void) close(rpvect[0]);
11119c9e68d9Seric 				(void) close(rpvect[1]);
11129c9e68d9Seric 			}
11139c9e68d9Seric 			if (tTd(11, 1))
11149c9e68d9Seric 				printf("openmailer: NULL\n");
11159c9e68d9Seric 			rcode = EX_OSERR;
11169c9e68d9Seric 			goto give_up;
11179c9e68d9Seric 		}
11189c9e68d9Seric 		else if (pid == 0)
11199c9e68d9Seric 		{
11209c9e68d9Seric 			int i;
11219c9e68d9Seric 			int saveerrno;
11229c9e68d9Seric 			char **ep;
11239c9e68d9Seric 			char *env[MAXUSERENVIRON];
11249c9e68d9Seric 			extern char **environ;
11259c9e68d9Seric 			extern int DtableSize;
11269c9e68d9Seric 
11279c9e68d9Seric 			/* child -- set up input & exec mailer */
11282b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
11292b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
11302b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11319c9e68d9Seric 
113244f2317fSeric 			/* reset user and group */
113344f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
113444f2317fSeric 			{
113544f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
113644f2317fSeric 				{
113744f2317fSeric 					(void) initgroups(DefUser, DefGid);
1138fc354902Seric 					(void) setgid(DefGid);
113944f2317fSeric 					(void) setuid(DefUid);
114044f2317fSeric 				}
114144f2317fSeric 				else
114244f2317fSeric 				{
114344f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
114444f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
114544f2317fSeric 						ctladdr->q_gid);
1146fc354902Seric 					(void) setuid(ctladdr->q_gid);
114744f2317fSeric 					(void) setuid(ctladdr->q_uid);
114844f2317fSeric 				}
114944f2317fSeric 			}
115044f2317fSeric 
115144f2317fSeric 			if (tTd(11, 2))
115244f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
115344f2317fSeric 					getuid(), geteuid());
115444f2317fSeric 
1155b986f6aaSeric 			/* move into some "safe" directory */
1156b986f6aaSeric 			if (m->m_execdir != NULL)
1157b986f6aaSeric 			{
1158b986f6aaSeric 				char *p, *q;
1159b986f6aaSeric 				char buf[MAXLINE];
1160b986f6aaSeric 
1161b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1162b986f6aaSeric 				{
1163b986f6aaSeric 					q = strchr(p, ':');
1164b986f6aaSeric 					if (q != NULL)
1165b986f6aaSeric 						*q = '\0';
1166b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1167b986f6aaSeric 					if (q != NULL)
1168b986f6aaSeric 						*q++ = ':';
1169b986f6aaSeric 					if (tTd(11, 20))
1170b986f6aaSeric 						printf("openmailer: trydir %s\n",
1171b986f6aaSeric 							buf);
1172b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1173b986f6aaSeric 						break;
1174b986f6aaSeric 				}
1175b986f6aaSeric 			}
1176b986f6aaSeric 
11779c9e68d9Seric 			/* arrange to filter std & diag output of command */
11789c9e68d9Seric 			if (clever)
11799c9e68d9Seric 			{
11809c9e68d9Seric 				(void) close(rpvect[0]);
11816fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11826fe8c3bcSeric 				{
11830e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11840e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11856fe8c3bcSeric 					_exit(EX_OSERR);
11866fe8c3bcSeric 				}
11879c9e68d9Seric 				(void) close(rpvect[1]);
11889c9e68d9Seric 			}
11899c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11909c9e68d9Seric 			{
11919c9e68d9Seric 				/* put mailer output in transcript */
11926fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11936fe8c3bcSeric 				{
11940e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11950e3bfef5Seric 						e->e_to, m->m_name,
11966fe8c3bcSeric 						fileno(e->e_xfp));
11976fe8c3bcSeric 					_exit(EX_OSERR);
11989c9e68d9Seric 				}
11996fe8c3bcSeric 			}
12006fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
12016fe8c3bcSeric 			{
12020e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
12030e3bfef5Seric 					e->e_to, m->m_name);
12046fe8c3bcSeric 				_exit(EX_OSERR);
12056fe8c3bcSeric 			}
12069c9e68d9Seric 
12079c9e68d9Seric 			/* arrange to get standard input */
12089c9e68d9Seric 			(void) close(mpvect[1]);
12099c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
12109c9e68d9Seric 			{
12110e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
12120e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
12139c9e68d9Seric 				_exit(EX_OSERR);
12149c9e68d9Seric 			}
12159c9e68d9Seric 			(void) close(mpvect[0]);
12169c9e68d9Seric 
12179c9e68d9Seric 			/* arrange for all the files to be closed */
12189c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12199c9e68d9Seric 			{
12209c9e68d9Seric 				register int j;
122144f2317fSeric 
12229c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12239c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
12249c9e68d9Seric 			}
12259c9e68d9Seric 
12269c9e68d9Seric 			/* set up the mailer environment */
12279c9e68d9Seric 			i = 0;
12289c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12299c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12309c9e68d9Seric 			{
12319c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12329c9e68d9Seric 					env[i++] = *ep;
12339c9e68d9Seric 			}
12349c9e68d9Seric 			env[i++] = NULL;
12359c9e68d9Seric 
12369c9e68d9Seric 			/* try to execute the mailer */
12379c9e68d9Seric 			execve(m->m_mailer, pv, env);
12389c9e68d9Seric 			saveerrno = errno;
12399c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12407c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12417c941fd2Seric 				_exit(EX_OSERR);
12429c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12439c9e68d9Seric 		}
12449c9e68d9Seric 
12459c9e68d9Seric 		/*
12469c9e68d9Seric 		**  Set up return value.
12479c9e68d9Seric 		*/
12489c9e68d9Seric 
12499c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12509c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12519c9e68d9Seric 		mci->mci_mailer = m;
12529c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12539c9e68d9Seric 		mci->mci_pid = pid;
12549c9e68d9Seric 		(void) close(mpvect[0]);
12559c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1256335eae58Seric 		if (mci->mci_out == NULL)
1257335eae58Seric 		{
1258335eae58Seric 			syserr("deliver: cannot create mailer output channel, fd=%d",
1259335eae58Seric 				mpvect[1]);
1260335eae58Seric 			(void) close(mpvect[1]);
1261335eae58Seric 			if (clever)
1262335eae58Seric 			{
1263335eae58Seric 				(void) close(rpvect[0]);
1264335eae58Seric 				(void) close(rpvect[1]);
1265335eae58Seric 			}
1266335eae58Seric 			rcode = EX_OSERR;
1267335eae58Seric 			goto give_up;
1268335eae58Seric 		}
12699c9e68d9Seric 		if (clever)
12709c9e68d9Seric 		{
12719c9e68d9Seric 			(void) close(rpvect[1]);
12729c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
1273335eae58Seric 			if (mci->mci_in == NULL)
1274335eae58Seric 			{
1275335eae58Seric 				syserr("deliver: cannot create mailer input channel, fd=%d",
1276335eae58Seric 					mpvect[1]);
1277335eae58Seric 				(void) close(rpvect[0]);
1278335eae58Seric 				fclose(mci->mci_out);
1279335eae58Seric 				mci->mci_out = NULL;
1280335eae58Seric 				rcode = EX_OSERR;
1281335eae58Seric 				goto give_up;
1282335eae58Seric 			}
12839c9e68d9Seric 		}
12849c9e68d9Seric 		else
12859c9e68d9Seric 		{
12869c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12879c9e68d9Seric 			mci->mci_in = NULL;
12889c9e68d9Seric 		}
12899c9e68d9Seric 	}
12909c9e68d9Seric 
12919c9e68d9Seric 	/*
12929c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12939c9e68d9Seric 	*/
12949c9e68d9Seric 
12959c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12969c9e68d9Seric 	{
12979c9e68d9Seric 		smtpinit(m, mci, e);
12989c9e68d9Seric 	}
12999c9e68d9Seric 	if (tTd(11, 1))
13009c9e68d9Seric 	{
13019c9e68d9Seric 		printf("openmailer: ");
13024052916eSeric 		mci_dump(mci, FALSE);
13039c9e68d9Seric 	}
13049c9e68d9Seric 
13059c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1306b31e7f2bSeric 	{
1307b31e7f2bSeric 		/* couldn't open the mailer */
1308b31e7f2bSeric 		rcode = mci->mci_exitstat;
13092a6bc25bSeric 		errno = mci->mci_errno;
1310f170942cSeric #ifdef NAMED_BIND
1311f170942cSeric 		h_errno = mci->mci_herrno;
1312f170942cSeric #endif
1313b31e7f2bSeric 		if (rcode == EX_OK)
1314b31e7f2bSeric 		{
1315b31e7f2bSeric 			/* shouldn't happen */
131608b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
13176b0f339dSeric 				rcode, mci->mci_state, firstsig);
1318b31e7f2bSeric 			rcode = EX_SOFTWARE;
1319b31e7f2bSeric 		}
1320*cb082c5bSeric 		else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0')
132190891494Seric 		{
132290891494Seric 			/* try next MX site */
132390891494Seric 			goto tryhost;
132490891494Seric 		}
1325b31e7f2bSeric 	}
1326b31e7f2bSeric 	else if (!clever)
1327b31e7f2bSeric 	{
1328b31e7f2bSeric 		/*
1329b31e7f2bSeric 		**  Format and send message.
1330b31e7f2bSeric 		*/
133115d084d5Seric 
1332b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1333b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1334b31e7f2bSeric 		putline("\n", mci->mci_out, m);
133503c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1336b31e7f2bSeric 
1337b31e7f2bSeric 		/* get the exit status */
1338c9be6216Seric 		rcode = endmailer(mci, e, pv);
1339134746fbSeric 	}
1340134746fbSeric 	else
1341b31e7f2bSeric #ifdef SMTP
1342134746fbSeric 	{
1343b31e7f2bSeric 		/*
1344b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1345b31e7f2bSeric 		*/
134615d084d5Seric 
1347b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1348b31e7f2bSeric 		if (rcode == EX_OK)
134975889e88Seric 		{
1350ded0d3daSkarels 			register char *t = tobuf;
1351ded0d3daSkarels 			register int i;
1352ded0d3daSkarels 
1353588cad61Seric 			/* send the recipient list */
135463780dbdSeric 			tobuf[0] = '\0';
135575889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
135675889e88Seric 			{
135763780dbdSeric 				e->e_to = to->q_paddr;
135815d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
135975889e88Seric 				{
136083b7ddc9Seric 					markfailure(e, to, i);
13614dee0003Seric 					giveresponse(i, m, mci, ctladdr, e);
136263780dbdSeric 				}
136375889e88Seric 				else
136475889e88Seric 				{
1365911693bfSbostic 					*t++ = ',';
1366b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1367b31e7f2bSeric 						continue;
1368ee39df61Seric 					*t = '\0';
1369588cad61Seric 				}
1370588cad61Seric 			}
1371588cad61Seric 
137263780dbdSeric 			/* now send the data */
137363780dbdSeric 			if (tobuf[0] == '\0')
1374b31e7f2bSeric 			{
13759c9e68d9Seric 				rcode = EX_OK;
137663780dbdSeric 				e->e_to = NULL;
1377b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1378b31e7f2bSeric 					smtprset(m, mci, e);
1379b31e7f2bSeric 			}
138075889e88Seric 			else
138175889e88Seric 			{
138263780dbdSeric 				e->e_to = tobuf + 1;
138375889e88Seric 				rcode = smtpdata(m, mci, e);
138463780dbdSeric 			}
138563780dbdSeric 
138663780dbdSeric 			/* now close the connection */
1387b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
138815d084d5Seric 				smtpquit(m, mci, e);
138963780dbdSeric 		}
1390*cb082c5bSeric 		if (rcode != EX_OK && curhost != NULL && *curhost != '\0')
13919c9e68d9Seric 		{
13929c9e68d9Seric 			/* try next MX site */
13939c9e68d9Seric 			goto tryhost;
13949c9e68d9Seric 		}
1395c579ef51Seric 	}
1396b31e7f2bSeric #else /* not SMTP */
1397a05b3449Sbostic 	{
139808b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1399845e533cSeric 		rcode = EX_CONFIG;
1400b31e7f2bSeric 		goto give_up;
1401a05b3449Sbostic 	}
1402b31e7f2bSeric #endif /* SMTP */
1403134746fbSeric #ifdef NAMED_BIND
14042bcc6d2dSeric 	if (ConfigLevel < 2)
1405912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1406134746fbSeric #endif
14075dfc646bSeric 
1408b31e7f2bSeric 	/* arrange a return receipt if requested */
1409df106f0bSeric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1410df106f0bSeric 	    bitnset(M_LOCALMAILER, m->m_flags))
1411b31e7f2bSeric 	{
1412b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1413b31e7f2bSeric 		/* do we want to send back more info? */
1414b31e7f2bSeric 	}
1415b31e7f2bSeric 
1416c77d1c25Seric 	/*
141763780dbdSeric 	**  Do final status disposal.
141863780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1419c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1420c77d1c25Seric 	**		addressees.
1421c77d1c25Seric 	*/
1422c77d1c25Seric 
1423b31e7f2bSeric   give_up:
142463780dbdSeric 	if (tobuf[0] != '\0')
14254dee0003Seric 		giveresponse(rcode, m, mci, ctladdr, e);
1426772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1427b31e7f2bSeric 	{
1428dde5acadSeric 		if (rcode != EX_OK)
142983b7ddc9Seric 			markfailure(e, to, rcode);
1430dde5acadSeric 		else
1431655518ecSeric 		{
1432dde5acadSeric 			to->q_flags |= QSENT;
1433655518ecSeric 			e->e_nsent++;
1434df106f0bSeric 			if (e->e_receiptto != NULL &&
1435df106f0bSeric 			    bitnset(M_LOCALMAILER, m->m_flags))
1436df106f0bSeric 			{
1437df106f0bSeric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1438df106f0bSeric 					to->q_paddr);
1439df106f0bSeric 			}
1440655518ecSeric 		}
1441b31e7f2bSeric 	}
1442b31e7f2bSeric 
1443b31e7f2bSeric 	/*
1444b31e7f2bSeric 	**  Restore state and return.
1445b31e7f2bSeric 	*/
1446c77d1c25Seric 
14477febfd66Seric #ifdef XDEBUG
14487febfd66Seric 	{
14497febfd66Seric 		char wbuf[MAXLINE];
14507febfd66Seric 
14517febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
145244622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
145344622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
145444622ea3Seric 			m->m_name);
14557febfd66Seric 		checkfd012(wbuf);
14567febfd66Seric 	}
14577febfd66Seric #endif
14587febfd66Seric 
145935490626Seric 	errno = 0;
1460588cad61Seric 	define('g', (char *) NULL, e);
14615826d9d3Seric 	return (rcode);
146225a99e2eSeric }
14635dfc646bSeric /*
146483b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
146583b7ddc9Seric **
146683b7ddc9Seric **	Parameters:
146783b7ddc9Seric **		e -- the envelope we are sending.
146883b7ddc9Seric **		q -- the address to mark.
146983b7ddc9Seric **		rcode -- the code signifying the particular failure.
147083b7ddc9Seric **
147183b7ddc9Seric **	Returns:
147283b7ddc9Seric **		none.
147383b7ddc9Seric **
147483b7ddc9Seric **	Side Effects:
147583b7ddc9Seric **		marks the address (and possibly the envelope) with the
147683b7ddc9Seric **			failure so that an error will be returned or
147783b7ddc9Seric **			the message will be queued, as appropriate.
147883b7ddc9Seric */
147983b7ddc9Seric 
148083b7ddc9Seric markfailure(e, q, rcode)
148183b7ddc9Seric 	register ENVELOPE *e;
148283b7ddc9Seric 	register ADDRESS *q;
148383b7ddc9Seric 	int rcode;
148483b7ddc9Seric {
148519c47125Seric 	char buf[MAXLINE];
148619c47125Seric 
148783b7ddc9Seric 	if (rcode == EX_OK)
148883b7ddc9Seric 		return;
1489f170942cSeric 	else if (rcode == EX_TEMPFAIL)
149083b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1491f170942cSeric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
1492f170942cSeric 		q->q_flags |= QBADADDR;
149383b7ddc9Seric }
149483b7ddc9Seric /*
1495c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1496c579ef51Seric **
1497c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1498c579ef51Seric **	violation), so we report those specially.  For other
1499c579ef51Seric **	errors, we choose a status message (into statmsg),
1500c579ef51Seric **	and if it represents an error, we print it.
1501c579ef51Seric **
1502c579ef51Seric **	Parameters:
1503c579ef51Seric **		pid -- pid of mailer.
1504c9be6216Seric **		e -- the current envelope.
1505c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1506c9be6216Seric **			(for error messages).
1507c579ef51Seric **
1508c579ef51Seric **	Returns:
1509c579ef51Seric **		exit code of mailer.
1510c579ef51Seric **
1511c579ef51Seric **	Side Effects:
1512c579ef51Seric **		none.
1513c579ef51Seric */
1514c579ef51Seric 
1515c9be6216Seric endmailer(mci, e, pv)
1516b31e7f2bSeric 	register MCI *mci;
1517c9be6216Seric 	register ENVELOPE *e;
1518c9be6216Seric 	char **pv;
1519c579ef51Seric {
1520588cad61Seric 	int st;
1521c579ef51Seric 
152275889e88Seric 	/* close any connections */
152375889e88Seric 	if (mci->mci_in != NULL)
1524c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
152575889e88Seric 	if (mci->mci_out != NULL)
1526c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
152775889e88Seric 	mci->mci_in = mci->mci_out = NULL;
152875889e88Seric 	mci->mci_state = MCIS_CLOSED;
152975889e88Seric 
153033db8731Seric 	/* in the IPC case there is nothing to wait for */
153175889e88Seric 	if (mci->mci_pid == 0)
153233db8731Seric 		return (EX_OK);
153333db8731Seric 
153433db8731Seric 	/* wait for the mailer process to die and collect status */
153575889e88Seric 	st = waitfor(mci->mci_pid);
1536588cad61Seric 	if (st == -1)
153778de67c1Seric 	{
1538c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1539588cad61Seric 		return (EX_SOFTWARE);
1540c579ef51Seric 	}
154133db8731Seric 
1542bf9bc890Seric 	if (WIFEXITED(st))
1543c579ef51Seric 	{
1544bf9bc890Seric 		/* normal death -- return status */
1545bf9bc890Seric 		return (WEXITSTATUS(st));
1546bf9bc890Seric 	}
1547bf9bc890Seric 
1548bf9bc890Seric 	/* it died a horrid death */
1549c9be6216Seric 	syserr("mailer %s died with signal %o", pv[0], st);
1550c9be6216Seric 
1551c9be6216Seric 	/* log the arguments */
1552c9be6216Seric 	if (e->e_xfp != NULL)
1553c9be6216Seric 	{
1554c9be6216Seric 		register char **av;
1555c9be6216Seric 
1556c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1557c9be6216Seric 		for (av = pv; *av != NULL; av++)
1558c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1559c9be6216Seric 		fprintf(e->e_xfp, "\n");
1560c9be6216Seric 	}
1561c9be6216Seric 
15625f73204aSeric 	ExitStat = EX_TEMPFAIL;
15635f73204aSeric 	return (EX_TEMPFAIL);
1564c579ef51Seric }
1565c579ef51Seric /*
156625a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
156725a99e2eSeric **
156825a99e2eSeric **	Parameters:
156925a99e2eSeric **		stat -- the status code from the mailer (high byte
157025a99e2eSeric **			only; core dumps must have been taken care of
157125a99e2eSeric **			already).
157281161401Seric **		m -- the mailer info for this mailer.
157381161401Seric **		mci -- the mailer connection info -- can be NULL if the
157481161401Seric **			response is given before the connection is made.
15754dee0003Seric **		ctladdr -- the controlling address for the recipient
15764dee0003Seric **			address(es).
157781161401Seric **		e -- the current envelope.
157825a99e2eSeric **
157925a99e2eSeric **	Returns:
1580db8841e9Seric **		none.
158125a99e2eSeric **
158225a99e2eSeric **	Side Effects:
1583c1f9df2cSeric **		Errors may be incremented.
158425a99e2eSeric **		ExitStat may be set.
158525a99e2eSeric */
158625a99e2eSeric 
15874dee0003Seric giveresponse(stat, m, mci, ctladdr, e)
158825a99e2eSeric 	int stat;
1589588cad61Seric 	register MAILER *m;
159081161401Seric 	register MCI *mci;
15914dee0003Seric 	ADDRESS *ctladdr;
1592198d9be0Seric 	ENVELOPE *e;
159325a99e2eSeric {
15949c16475dSeric 	register const char *statmsg;
159525a99e2eSeric 	extern char *SysExMsg[];
159625a99e2eSeric 	register int i;
1597d4bd8f0eSbostic 	extern int N_SysEx;
1598198d9be0Seric 	char buf[MAXLINE];
159925a99e2eSeric 
160013bbc08cSeric 	/*
160113bbc08cSeric 	**  Compute status message from code.
160213bbc08cSeric 	*/
160313bbc08cSeric 
160425a99e2eSeric 	i = stat - EX__BASE;
1605588cad61Seric 	if (stat == 0)
16066fe8c3bcSeric 	{
1607588cad61Seric 		statmsg = "250 Sent";
1608ce5531bdSeric 		if (e->e_statmsg != NULL)
16096fe8c3bcSeric 		{
1610ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
16116fe8c3bcSeric 			statmsg = buf;
16126fe8c3bcSeric 		}
16136fe8c3bcSeric 	}
1614588cad61Seric 	else if (i < 0 || i > N_SysEx)
1615588cad61Seric 	{
1616588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1617588cad61Seric 		stat = EX_UNAVAILABLE;
1618588cad61Seric 		statmsg = buf;
1619588cad61Seric 	}
1620198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1621198d9be0Seric 	{
16227d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1623d4bd8f0eSbostic #ifdef NAMED_BIND
1624f28da541Smiriam 		if (h_errno == TRY_AGAIN)
16254d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1626f28da541Smiriam 		else
1627d4bd8f0eSbostic #endif
1628f28da541Smiriam 		{
16298557d168Seric 			if (errno != 0)
1630d87e85f3Seric 				statmsg = errstring(errno);
1631d87e85f3Seric 			else
1632d87e85f3Seric 			{
1633d87e85f3Seric #ifdef SMTP
1634d87e85f3Seric 				extern char SmtpError[];
1635d87e85f3Seric 
1636d87e85f3Seric 				statmsg = SmtpError;
16376c2c3107Seric #else /* SMTP */
1638d87e85f3Seric 				statmsg = NULL;
16396c2c3107Seric #endif /* SMTP */
1640d87e85f3Seric 			}
1641f28da541Smiriam 		}
1642d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1643d87e85f3Seric 		{
164487c9b3e7Seric 			(void) strcat(buf, ": ");
1645d87e85f3Seric 			(void) strcat(buf, statmsg);
16468557d168Seric 		}
1647198d9be0Seric 		statmsg = buf;
1648198d9be0Seric 	}
1649f170942cSeric #ifdef NAMED_BIND
1650f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1651f170942cSeric 	{
16524d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1653f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1654f170942cSeric 		statmsg = buf;
1655f170942cSeric 	}
1656f170942cSeric #endif
165725a99e2eSeric 	else
1658d87e85f3Seric 	{
165925a99e2eSeric 		statmsg = SysExMsg[i];
16607d55540cSeric 		if (*statmsg++ == ':')
16617d55540cSeric 		{
16627d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16637d55540cSeric 			statmsg = buf;
16647d55540cSeric 		}
1665d87e85f3Seric 	}
1666588cad61Seric 
1667588cad61Seric 	/*
1668588cad61Seric 	**  Print the message as appropriate
1669588cad61Seric 	*/
1670588cad61Seric 
1671198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1672df106f0bSeric 	{
1673df106f0bSeric 		extern char MsgBuf[];
1674df106f0bSeric 
1675e12d03eeSeric 		message(&statmsg[4], errstring(errno));
1676df106f0bSeric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1677df106f0bSeric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1678df106f0bSeric 	}
167925a99e2eSeric 	else
168025a99e2eSeric 	{
1681c1f9df2cSeric 		Errors++;
1682e12d03eeSeric 		usrerr(statmsg, errstring(errno));
168325a99e2eSeric 	}
168425a99e2eSeric 
168525a99e2eSeric 	/*
168625a99e2eSeric 	**  Final cleanup.
168725a99e2eSeric 	**	Log a record of the transaction.  Compute the new
168825a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
168925a99e2eSeric 	**	that.
169025a99e2eSeric 	*/
169125a99e2eSeric 
16922f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
16934dee0003Seric 		logdelivery(m, mci, &statmsg[4], ctladdr, e);
1694eb238f8cSeric 
1695eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1696eb238f8cSeric 		setstat(stat);
1697198d9be0Seric 	if (stat != EX_OK)
1698198d9be0Seric 	{
1699198d9be0Seric 		if (e->e_message != NULL)
1700198d9be0Seric 			free(e->e_message);
1701198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1702198d9be0Seric 	}
17038557d168Seric 	errno = 0;
1704d4bd8f0eSbostic #ifdef NAMED_BIND
1705f28da541Smiriam 	h_errno = 0;
1706d4bd8f0eSbostic #endif
1707eb238f8cSeric }
1708eb238f8cSeric /*
1709eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1710eb238f8cSeric **
1711eb238f8cSeric **	Parameters:
171281161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
171381161401Seric **		mci -- the mailer connection info -- can be NULL if the
171481161401Seric **			log is occuring when no connection is active.
171581161401Seric **		stat -- the message to print for the status.
17164dee0003Seric **		ctladdr -- the controlling address for the to list.
171781161401Seric **		e -- the current envelope.
1718eb238f8cSeric **
1719eb238f8cSeric **	Returns:
1720eb238f8cSeric **		none
1721eb238f8cSeric **
1722eb238f8cSeric **	Side Effects:
1723eb238f8cSeric **		none
1724eb238f8cSeric */
1725eb238f8cSeric 
17264dee0003Seric logdelivery(m, mci, stat, ctladdr, e)
172781161401Seric 	MAILER *m;
172881161401Seric 	register MCI *mci;
1729eb238f8cSeric 	char *stat;
17304dee0003Seric 	ADDRESS *ctladdr;
1731b31e7f2bSeric 	register ENVELOPE *e;
17325cf56be3Seric {
1733eb238f8cSeric # ifdef LOG
17344dee0003Seric 	register char *bp;
1735d6acf3eeSeric 	char buf[512];
17369507d1f9Seric 
17374dee0003Seric 	bp = buf;
17384dee0003Seric 	if (ctladdr != NULL)
17394dee0003Seric 	{
17404dee0003Seric 		strcpy(bp, ", ctladdr=");
17414dee0003Seric 		strcat(bp, ctladdr->q_paddr);
17424dee0003Seric 		bp += strlen(bp);
17434dee0003Seric 		if (bitset(QGOODUID, ctladdr->q_flags))
17444dee0003Seric 		{
17454dee0003Seric 			(void) sprintf(bp, " (%d/%d)",
17464dee0003Seric 					ctladdr->q_uid, ctladdr->q_gid);
17474dee0003Seric 			bp += strlen(bp);
17484dee0003Seric 		}
17494dee0003Seric 	}
17504dee0003Seric 
17514dee0003Seric 	(void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
17524dee0003Seric 	bp += strlen(bp);
175381161401Seric 
175448ed5d33Seric 	if (m != NULL)
175571ff6caaSeric 	{
17564dee0003Seric 		(void) strcpy(bp, ", mailer=");
17574dee0003Seric 		(void) strcat(bp, m->m_name);
17584dee0003Seric 		bp += strlen(bp);
175971ff6caaSeric 	}
176048ed5d33Seric 
176148ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
176271ff6caaSeric 	{
176371ff6caaSeric # ifdef DAEMON
1764e2f2f828Seric 		extern SOCKADDR CurHostAddr;
176548ed5d33Seric # endif
176671ff6caaSeric 
17674dee0003Seric 		(void) strcpy(bp, ", relay=");
17684dee0003Seric 		(void) strcat(bp, mci->mci_host);
176948ed5d33Seric 
177048ed5d33Seric # ifdef DAEMON
17714dee0003Seric 		(void) strcat(bp, " (");
17724dee0003Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
17734dee0003Seric 		(void) strcat(bp, ")");
177471ff6caaSeric # endif
177571ff6caaSeric 	}
17769507d1f9Seric 	else
177748ed5d33Seric 	{
177848ed5d33Seric 		char *p = macvalue('h', e);
17799507d1f9Seric 
178048ed5d33Seric 		if (p != NULL && p[0] != '\0')
178148ed5d33Seric 		{
17824dee0003Seric 			(void) strcpy(bp, ", relay=");
17834dee0003Seric 			(void) strcat(bp, p);
178448ed5d33Seric 		}
178548ed5d33Seric 	}
1786*cb082c5bSeric 	bp += strlen(bp);
1787d6acf3eeSeric 
17884dee0003Seric 	syslog(LOG_INFO, "%s: to=%s%s, stat=%s",
1789d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
17906c2c3107Seric # endif /* LOG */
179125a99e2eSeric }
179225a99e2eSeric /*
179351552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
179425a99e2eSeric **
179551552439Seric **	This can be made an arbitrary message separator by changing $l
179651552439Seric **
17979b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
17989b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
17999b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
18009b6c17a6Seric **	this kind of antique garbage????
180125a99e2eSeric **
180225a99e2eSeric **	Parameters:
180351552439Seric **		fp -- the file to output to.
180451552439Seric **		m -- the mailer describing this entry.
180525a99e2eSeric **
180625a99e2eSeric **	Returns:
180751552439Seric **		none
180825a99e2eSeric **
180925a99e2eSeric **	Side Effects:
181051552439Seric **		outputs some text to fp.
181125a99e2eSeric */
181225a99e2eSeric 
1813b31e7f2bSeric putfromline(fp, m, e)
181451552439Seric 	register FILE *fp;
181551552439Seric 	register MAILER *m;
1816b31e7f2bSeric 	ENVELOPE *e;
181725a99e2eSeric {
18182bc47524Seric 	char *template = "\201l\n";
181951552439Seric 	char buf[MAXLINE];
182025a99e2eSeric 
182157fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
182251552439Seric 		return;
182313bbc08cSeric 
18242c7e1b8dSeric # ifdef UGLYUUCP
182557fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
182674b6e67bSeric 	{
1827ea09d6edSeric 		char *bang;
1828ea09d6edSeric 		char xbuf[MAXLINE];
182974b6e67bSeric 
1830ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
18316c2c3107Seric 		bang = strchr(buf, '!');
183274b6e67bSeric 		if (bang == NULL)
183334fcca25Seric 		{
183434fcca25Seric 			errno = 0;
183534fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
183634fcca25Seric 		}
183774b6e67bSeric 		else
1838588cad61Seric 		{
1839ea09d6edSeric 			*bang++ = '\0';
18402bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1841ea09d6edSeric 			template = xbuf;
184274b6e67bSeric 		}
1843588cad61Seric 	}
18446c2c3107Seric # endif /* UGLYUUCP */
1845b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
184677b52738Seric 	putline(buf, fp, m);
1847bc6e2962Seric }
1848bc6e2962Seric /*
184951552439Seric **  PUTBODY -- put the body of a message.
185051552439Seric **
185151552439Seric **	Parameters:
185251552439Seric **		fp -- file to output onto.
185377b52738Seric **		m -- a mailer descriptor to control output format.
18549a6a5f55Seric **		e -- the envelope to put out.
185503c02fdeSeric **		separator -- if non-NULL, a message separator that must
185603c02fdeSeric **			not be permitted in the resulting message.
185751552439Seric **
185851552439Seric **	Returns:
185951552439Seric **		none.
186051552439Seric **
186151552439Seric **	Side Effects:
186251552439Seric **		The message is written onto fp.
186351552439Seric */
186451552439Seric 
186503c02fdeSeric putbody(fp, m, e, separator)
186651552439Seric 	FILE *fp;
1867588cad61Seric 	MAILER *m;
18689a6a5f55Seric 	register ENVELOPE *e;
186903c02fdeSeric 	char *separator;
187051552439Seric {
187177b52738Seric 	char buf[MAXLINE];
187251552439Seric 
187351552439Seric 	/*
187451552439Seric 	**  Output the body of the message
187551552439Seric 	*/
187651552439Seric 
18779a6a5f55Seric 	if (e->e_dfp == NULL)
187851552439Seric 	{
18799a6a5f55Seric 		if (e->e_df != NULL)
18809a6a5f55Seric 		{
18819a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
18829a6a5f55Seric 			if (e->e_dfp == NULL)
18838f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
1884e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
18859a6a5f55Seric 		}
18869a6a5f55Seric 		else
188777b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
18889a6a5f55Seric 	}
18899a6a5f55Seric 	if (e->e_dfp != NULL)
18909a6a5f55Seric 	{
18919a6a5f55Seric 		rewind(e->e_dfp);
189277b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
189324fc8aeeSeric 		{
189424fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1895d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
18963462ad9eSeric 				(void) putc('>', fp);
189703c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
189803c02fdeSeric 			{
189903c02fdeSeric 				/* possible separator */
190003c02fdeSeric 				int sl = strlen(separator);
190103c02fdeSeric 
190203c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
190303c02fdeSeric 					(void) putc(' ', fp);
190403c02fdeSeric 			}
190577b52738Seric 			putline(buf, fp, m);
190624fc8aeeSeric 		}
190751552439Seric 
19089a6a5f55Seric 		if (ferror(e->e_dfp))
190951552439Seric 		{
1910df106f0bSeric 			syserr("putbody: %s: read error", e->e_df);
191151552439Seric 			ExitStat = EX_IOERR;
191251552439Seric 		}
191351552439Seric 	}
191451552439Seric 
19150890ba1fSeric 	/* some mailers want extra blank line at end of message */
19160890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
19170890ba1fSeric 		putline("", fp, m);
19180890ba1fSeric 
191951552439Seric 	(void) fflush(fp);
192051552439Seric 	if (ferror(fp) && errno != EPIPE)
192151552439Seric 	{
192251552439Seric 		syserr("putbody: write error");
192351552439Seric 		ExitStat = EX_IOERR;
192451552439Seric 	}
192551552439Seric 	errno = 0;
192625a99e2eSeric }
192725a99e2eSeric /*
192825a99e2eSeric **  MAILFILE -- Send a message to a file.
192925a99e2eSeric **
1930f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1931f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1932f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1933f129ec7dSeric **	sendmail runs as root.
1934f129ec7dSeric **
1935588cad61Seric **	This could be done as a subordinate mailer, except that it
1936588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1937588cad61Seric **	view this as being sufficiently important as to include it
1938588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1939588cad61Seric **	to create another process plus some pipes to save the message.
1940588cad61Seric **
194125a99e2eSeric **	Parameters:
194225a99e2eSeric **		filename -- the name of the file to send to.
19436259796dSeric **		ctladdr -- the controlling address header -- includes
19446259796dSeric **			the userid/groupid to be when sending.
194525a99e2eSeric **
194625a99e2eSeric **	Returns:
194725a99e2eSeric **		The exit code associated with the operation.
194825a99e2eSeric **
194925a99e2eSeric **	Side Effects:
195025a99e2eSeric **		none.
195125a99e2eSeric */
195225a99e2eSeric 
1953b31e7f2bSeric mailfile(filename, ctladdr, e)
195425a99e2eSeric 	char *filename;
19556259796dSeric 	ADDRESS *ctladdr;
1956b31e7f2bSeric 	register ENVELOPE *e;
195725a99e2eSeric {
195825a99e2eSeric 	register FILE *f;
195932d19d43Seric 	register int pid;
196015d084d5Seric 	int mode;
196125a99e2eSeric 
1962671745f3Seric 	if (tTd(11, 1))
1963671745f3Seric 	{
1964671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
1965671745f3Seric 		printaddr(ctladdr, FALSE);
1966671745f3Seric 	}
1967671745f3Seric 
1968f170942cSeric 	if (e->e_xfp != NULL)
1969f170942cSeric 		fflush(e->e_xfp);
1970f170942cSeric 
197132d19d43Seric 	/*
197232d19d43Seric 	**  Fork so we can change permissions here.
197332d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
197432d19d43Seric 	**	the complications of calling subroutines, etc.
197532d19d43Seric 	*/
197632d19d43Seric 
197732d19d43Seric 	DOFORK(fork);
197832d19d43Seric 
197932d19d43Seric 	if (pid < 0)
198032d19d43Seric 		return (EX_OSERR);
198132d19d43Seric 	else if (pid == 0)
198232d19d43Seric 	{
198332d19d43Seric 		/* child -- actually write to file */
1984f129ec7dSeric 		struct stat stb;
1985f129ec7dSeric 
19862b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
19872b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
19882b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
19893462ad9eSeric 		(void) umask(OldUmask);
199095f16dc0Seric 
1991f129ec7dSeric 		if (stat(filename, &stb) < 0)
19923a98e7eaSeric 			stb.st_mode = FileMode;
199315d084d5Seric 		mode = stb.st_mode;
199495f16dc0Seric 
199595f16dc0Seric 		/* limit the errors to those actually caused in the child */
199695f16dc0Seric 		errno = 0;
199795f16dc0Seric 		ExitStat = EX_OK;
199895f16dc0Seric 
1999f129ec7dSeric 		if (bitset(0111, stb.st_mode))
2000f129ec7dSeric 			exit(EX_CANTCREAT);
200119428781Seric 		if (ctladdr != NULL)
200215d084d5Seric 		{
200315d084d5Seric 			/* ignore setuid and setgid bits */
200415d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
200515d084d5Seric 		}
200615d084d5Seric 
20078f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
20088f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
20098f9146b0Srick 		{
20108f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
201195f16dc0Seric 			if (e->e_dfp == NULL)
201295f16dc0Seric 			{
20138f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
2014e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
20158f9146b0Srick 			}
20168f9146b0Srick 		}
20178f9146b0Srick 
201815d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
2019e36b99e2Seric 		{
202019428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
202195f16dc0Seric 			{
2022898a126bSbostic 				(void) initgroups(DefUser, DefGid);
202395f16dc0Seric 			}
202495f16dc0Seric 			else
202595f16dc0Seric 			{
2026898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
2027898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
2028898a126bSbostic 					ctladdr->q_gid);
2029898a126bSbostic 			}
2030e36b99e2Seric 		}
203115d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
2032e36b99e2Seric 		{
203319428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
2034e36b99e2Seric 				(void) setuid(DefUid);
2035e36b99e2Seric 			else
20366259796dSeric 				(void) setuid(ctladdr->q_uid);
2037e36b99e2Seric 		}
203895f16dc0Seric 		FileName = filename;
203995f16dc0Seric 		LineNumber = 0;
20403a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
204125a99e2eSeric 		if (f == NULL)
204295f16dc0Seric 		{
2043b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
204432d19d43Seric 			exit(EX_CANTCREAT);
204595f16dc0Seric 		}
204625a99e2eSeric 
20470331ce05Seric 		putfromline(f, FileMailer, e);
20480331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
20490331ce05Seric 		putline("\n", f, FileMailer);
205003c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
20510331ce05Seric 		putline("\n", f, FileMailer);
205295f16dc0Seric 		if (ferror(f))
205395f16dc0Seric 		{
2054b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
205595f16dc0Seric 			setstat(EX_IOERR);
205695f16dc0Seric 		}
2057ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
205832d19d43Seric 		(void) fflush(stdout);
2059e36b99e2Seric 
206027628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
2061c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
206295f16dc0Seric 		exit(ExitStat);
206313bbc08cSeric 		/*NOTREACHED*/
206432d19d43Seric 	}
206532d19d43Seric 	else
206632d19d43Seric 	{
206732d19d43Seric 		/* parent -- wait for exit status */
2068588cad61Seric 		int st;
206932d19d43Seric 
2070588cad61Seric 		st = waitfor(pid);
2071bf9bc890Seric 		if (WIFEXITED(st))
2072bf9bc890Seric 			return (WEXITSTATUS(st));
2073588cad61Seric 		else
2074b6a0de9dSeric 		{
2075b6a0de9dSeric 			syserr("child died on signal %d", st);
2076bf9bc890Seric 			return (EX_UNAVAILABLE);
2077b6a0de9dSeric 		}
20788f9146b0Srick 		/*NOTREACHED*/
207932d19d43Seric 	}
208025a99e2eSeric }
2081ea4dc939Seric /*
2082e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2083e103b48fSeric **
2084e103b48fSeric **	The signature describes how we are going to send this -- it
2085e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2086e103b48fSeric **	an ordered list of MX hosts.
2087e103b48fSeric **
2088e103b48fSeric **	Parameters:
2089e103b48fSeric **		m -- the mailer describing this host.
2090e103b48fSeric **		host -- the host name.
2091e103b48fSeric **		e -- the current envelope.
2092e103b48fSeric **
2093e103b48fSeric **	Returns:
2094e103b48fSeric **		The signature for this host.
2095e103b48fSeric **
2096e103b48fSeric **	Side Effects:
2097e103b48fSeric **		Can tweak the symbol table.
2098e103b48fSeric */
2099e103b48fSeric 
2100e103b48fSeric char *
2101e103b48fSeric hostsignature(m, host, e)
2102e103b48fSeric 	register MAILER *m;
2103e103b48fSeric 	char *host;
2104e103b48fSeric 	ENVELOPE *e;
2105e103b48fSeric {
2106e103b48fSeric 	register char *p;
2107e103b48fSeric 	register STAB *s;
2108e103b48fSeric 	int i;
2109e103b48fSeric 	int len;
2110e103b48fSeric #ifdef NAMED_BIND
2111e103b48fSeric 	int nmx;
2112e103b48fSeric 	auto int rcode;
2113bafdc4e5Seric 	char *hp;
2114bafdc4e5Seric 	char *endp;
2115516782b4Seric 	int oldoptions;
2116e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2117e103b48fSeric #endif
2118e103b48fSeric 
2119e103b48fSeric 	/*
2120e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2121e103b48fSeric 	*/
2122e103b48fSeric 
2123e103b48fSeric 	p = m->m_mailer;
2124e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2125e103b48fSeric 	{
2126e103b48fSeric 		/* just an ordinary mailer */
2127e103b48fSeric 		return host;
2128e103b48fSeric 	}
2129e103b48fSeric 
2130e103b48fSeric 	/*
2131e103b48fSeric 	**  Look it up in the symbol table.
2132e103b48fSeric 	*/
2133e103b48fSeric 
2134e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2135e103b48fSeric 	if (s->s_hostsig != NULL)
2136e103b48fSeric 		return s->s_hostsig;
2137e103b48fSeric 
2138e103b48fSeric 	/*
2139e103b48fSeric 	**  Not already there -- create a signature.
2140e103b48fSeric 	*/
2141e103b48fSeric 
2142e103b48fSeric #ifdef NAMED_BIND
2143516782b4Seric 	if (ConfigLevel < 2)
2144516782b4Seric 	{
2145516782b4Seric 		oldoptions = _res.options;
2146516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2147516782b4Seric 	}
2148516782b4Seric 
2149bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2150bafdc4e5Seric 	{
2151bafdc4e5Seric 		endp = strchr(hp, ':');
2152bafdc4e5Seric 		if (endp != NULL)
2153bafdc4e5Seric 			*endp = '\0';
2154bafdc4e5Seric 
21557bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
21567d55540cSeric 
2157e103b48fSeric 		if (nmx <= 0)
2158e103b48fSeric 		{
2159e103b48fSeric 			register MCI *mci;
2160e103b48fSeric 
2161e103b48fSeric 			/* update the connection info for this host */
2162bafdc4e5Seric 			mci = mci_get(hp, m);
2163e103b48fSeric 			mci->mci_exitstat = rcode;
2164e103b48fSeric 			mci->mci_errno = errno;
2165f170942cSeric #ifdef NAMED_BIND
2166f170942cSeric 			mci->mci_herrno = h_errno;
2167f170942cSeric #endif
2168e103b48fSeric 
2169e103b48fSeric 			/* and return the original host name as the signature */
2170bafdc4e5Seric 			nmx = 1;
2171bafdc4e5Seric 			mxhosts[0] = hp;
2172e103b48fSeric 		}
2173e103b48fSeric 
2174e103b48fSeric 		len = 0;
2175e103b48fSeric 		for (i = 0; i < nmx; i++)
2176e103b48fSeric 		{
2177e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2178e103b48fSeric 		}
2179bafdc4e5Seric 		if (s->s_hostsig != NULL)
2180bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2181bafdc4e5Seric 		p = xalloc(len);
2182bafdc4e5Seric 		if (s->s_hostsig != NULL)
2183bafdc4e5Seric 		{
2184bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2185bafdc4e5Seric 			free(s->s_hostsig);
2186bafdc4e5Seric 			s->s_hostsig = p;
2187bafdc4e5Seric 			p += strlen(p);
2188bafdc4e5Seric 			*p++ = ':';
2189bafdc4e5Seric 		}
2190bafdc4e5Seric 		else
2191bafdc4e5Seric 			s->s_hostsig = p;
2192e103b48fSeric 		for (i = 0; i < nmx; i++)
2193e103b48fSeric 		{
2194e103b48fSeric 			if (i != 0)
2195e103b48fSeric 				*p++ = ':';
2196e103b48fSeric 			strcpy(p, mxhosts[i]);
2197e103b48fSeric 			p += strlen(p);
2198e103b48fSeric 		}
2199bafdc4e5Seric 		if (endp != NULL)
2200bafdc4e5Seric 			*endp++ = ':';
2201bafdc4e5Seric 	}
2202e103b48fSeric 	makelower(s->s_hostsig);
2203516782b4Seric 	if (ConfigLevel < 2)
2204516782b4Seric 		_res.options = oldoptions;
2205e103b48fSeric #else
2206e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2207e103b48fSeric 	s->s_hostsig = host;
2208e103b48fSeric #endif
2209e103b48fSeric 	if (tTd(17, 1))
2210e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2211e103b48fSeric 	return s->s_hostsig;
2212e103b48fSeric }
2213