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*519e7d80Seric static char sccsid[] = "@(#)deliver.c	8.93 (Berkeley) 08/07/94";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14f28da541Smiriam #include <netdb.h>
15911693bfSbostic #include <errno.h>
169d4a8008Seric #if NAMED_BIND
17912a731aSbostic #include <resolv.h>
18f170942cSeric 
19f170942cSeric extern int	h_errno;
20134746fbSeric #endif
2125a99e2eSeric 
225d437c4dSeric extern char	SmtpError[];
235d437c4dSeric 
2425a99e2eSeric /*
259c9e68d9Seric **  SENDALL -- actually send all the messages.
269c9e68d9Seric **
279c9e68d9Seric **	Parameters:
289c9e68d9Seric **		e -- the envelope to send.
299c9e68d9Seric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
309c9e68d9Seric **			the current e->e_sendmode.
319c9e68d9Seric **
329c9e68d9Seric **	Returns:
339c9e68d9Seric **		none.
349c9e68d9Seric **
359c9e68d9Seric **	Side Effects:
369c9e68d9Seric **		Scans the send lists and sends everything it finds.
379c9e68d9Seric **		Delivers any appropriate error messages.
389c9e68d9Seric **		If we are running in a non-interactive mode, takes the
399c9e68d9Seric **			appropriate action.
409c9e68d9Seric */
419c9e68d9Seric 
429c9e68d9Seric sendall(e, mode)
439c9e68d9Seric 	ENVELOPE *e;
449c9e68d9Seric 	char mode;
459c9e68d9Seric {
469c9e68d9Seric 	register ADDRESS *q;
479c9e68d9Seric 	char *owner;
489c9e68d9Seric 	int otherowners;
49b056abd0Seric 	register ENVELOPE *ee;
50b056abd0Seric 	ENVELOPE *splitenv = NULL;
516103d9b4Seric 	bool announcequeueup;
529c9e68d9Seric 
537880f3e4Seric 	/*
547880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
557880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
567880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
577880f3e4Seric 	**  addresses to be sent.
587880f3e4Seric 	*/
597880f3e4Seric 
608d19a23dSeric 	if (bitset(EF_FATALERRS, e->e_flags) &&
618d19a23dSeric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
62896b16f6Seric 	{
63896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
64896b16f6Seric 		return;
65896b16f6Seric 	}
66896b16f6Seric 
679c9e68d9Seric 	/* determine actual delivery mode */
68fbe2f963Seric 	CurrentLA = getla();
699c9e68d9Seric 	if (mode == SM_DEFAULT)
709c9e68d9Seric 	{
719c9e68d9Seric 		mode = e->e_sendmode;
729c9e68d9Seric 		if (mode != SM_VERIFY &&
739c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
749c9e68d9Seric 			mode = SM_QUEUE;
756103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
769c9e68d9Seric 	}
776103d9b4Seric 	else
786103d9b4Seric 		announcequeueup = FALSE;
799c9e68d9Seric 
809c9e68d9Seric 	if (tTd(13, 1))
819c9e68d9Seric 	{
820e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
830e484fe5Seric 			mode, e->e_id);
849c9e68d9Seric 		printaddr(&e->e_from, FALSE);
859c9e68d9Seric 		printf("sendqueue:\n");
869c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
879c9e68d9Seric 	}
889c9e68d9Seric 
899c9e68d9Seric 	/*
909c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
919c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
929c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
939c9e68d9Seric 	*/
949c9e68d9Seric 
959c9e68d9Seric 	CurEnv = e;
969c9e68d9Seric 
979c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
989c9e68d9Seric 	{
999c9e68d9Seric 		errno = 0;
10027607809Seric 		e->e_flags |= EF_FATALERRS|EF_PM_NOTIFY|EF_CLRQUEUE;
1014bb8b0fdSeric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
1029c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
103f7869e68Seric 			RealHostName == NULL ? "localhost" : RealHostName,
104f7869e68Seric 			e->e_sendqueue->q_paddr);
1059c9e68d9Seric 		return;
1069c9e68d9Seric 	}
1079c9e68d9Seric 
108b8004690Seric 	/*
109b8004690Seric 	**  Do sender deletion.
110b8004690Seric 	**
111b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
112b8004690Seric 	**	This can happen if the name server is hosed when you
113b8004690Seric 	**	are trying to send mail.  The result is that the sender
114b8004690Seric 	**	is instantiated in the queue as a recipient.
115b8004690Seric 	*/
116b8004690Seric 
117e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
118e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1199c9e68d9Seric 	{
1209c9e68d9Seric 		if (tTd(13, 5))
1219c9e68d9Seric 		{
1229c9e68d9Seric 			printf("sendall: QDONTSEND ");
1239c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1249c9e68d9Seric 		}
1259c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1269c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1279c9e68d9Seric 	}
1289c9e68d9Seric 
129ce5531bdSeric 	/*
130ce5531bdSeric 	**  Handle alias owners.
131ce5531bdSeric 	**
132ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
133ce5531bdSeric 	**	We discard owners that are the same as the return path.
134ce5531bdSeric 	*/
135ce5531bdSeric 
136ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
137ce5531bdSeric 	{
138ce5531bdSeric 		register struct address *a;
139ce5531bdSeric 
140ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
141ce5531bdSeric 			continue;
142ce5531bdSeric 		if (a != NULL)
143ce5531bdSeric 			q->q_owner = a->q_owner;
144ce5531bdSeric 
145ce5531bdSeric 		if (q->q_owner != NULL &&
146ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
147ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
148ce5531bdSeric 			q->q_owner = NULL;
149ce5531bdSeric 	}
150ce5531bdSeric 
151ce5531bdSeric 	owner = "";
152ce5531bdSeric 	otherowners = 1;
153ce5531bdSeric 	while (owner != NULL && otherowners > 0)
154ce5531bdSeric 	{
155ce5531bdSeric 		owner = NULL;
156ce5531bdSeric 		otherowners = 0;
157ce5531bdSeric 
158ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
159ce5531bdSeric 		{
160ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
161ce5531bdSeric 				continue;
162ce5531bdSeric 
163ce5531bdSeric 			if (q->q_owner != NULL)
164ce5531bdSeric 			{
165ce5531bdSeric 				if (owner == NULL)
166ce5531bdSeric 					owner = q->q_owner;
167ce5531bdSeric 				else if (owner != q->q_owner)
168ce5531bdSeric 				{
169ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
170ce5531bdSeric 					{
171ce5531bdSeric 						/* make future comparisons cheap */
172ce5531bdSeric 						q->q_owner = owner;
173ce5531bdSeric 					}
174ce5531bdSeric 					else
175ce5531bdSeric 					{
176ce5531bdSeric 						otherowners++;
177ce5531bdSeric 					}
178ce5531bdSeric 					owner = q->q_owner;
179ce5531bdSeric 				}
180ce5531bdSeric 			}
181ce5531bdSeric 			else
182ce5531bdSeric 			{
183ce5531bdSeric 				otherowners++;
184ce5531bdSeric 			}
185ce5531bdSeric 		}
186ce5531bdSeric 
187ce5531bdSeric 		if (owner != NULL && otherowners > 0)
188ce5531bdSeric 		{
189ce5531bdSeric 			extern HDR *copyheader();
190ce5531bdSeric 			extern ADDRESS *copyqueue();
191ce5531bdSeric 
192ce5531bdSeric 			/*
193ce5531bdSeric 			**  Split this envelope into two.
194ce5531bdSeric 			*/
195ce5531bdSeric 
196ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
197ce5531bdSeric 			*ee = *e;
198ce5531bdSeric 			ee->e_id = NULL;
199ce5531bdSeric 			(void) queuename(ee, '\0');
200ce5531bdSeric 
201ce5531bdSeric 			if (tTd(13, 1))
202ce5531bdSeric 				printf("sendall: split %s into %s\n",
203ce5531bdSeric 					e->e_id, ee->e_id);
204ce5531bdSeric 
205ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
206ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
207ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
208fce3c07bSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS|EF_SENDRECEIPT);
209fce3c07bSeric 			ee->e_flags |= EF_NORECEIPT;
210ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
211ce5531bdSeric 			if (tTd(13, 5))
212ce5531bdSeric 			{
213ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
214ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
215ce5531bdSeric 			}
216ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
217ce5531bdSeric 			ee->e_dfp = NULL;
218ce5531bdSeric 			ee->e_xfp = NULL;
219ce5531bdSeric 			ee->e_df = NULL;
220ce5531bdSeric 			ee->e_errormode = EM_MAIL;
221b056abd0Seric 			ee->e_sibling = splitenv;
222b056abd0Seric 			splitenv = ee;
223ce5531bdSeric 
224ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
225ce5531bdSeric 				if (q->q_owner == owner)
226de1a6b1bSeric 				{
227ce5531bdSeric 					q->q_flags |= QDONTSEND;
228de1a6b1bSeric 					q->q_flags &= ~QQUEUEUP;
229de1a6b1bSeric 				}
230ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
231ce5531bdSeric 				if (q->q_owner != owner)
232de1a6b1bSeric 				{
233ce5531bdSeric 					q->q_flags |= QDONTSEND;
234de1a6b1bSeric 					q->q_flags &= ~QQUEUEUP;
235de1a6b1bSeric 				}
236ce5531bdSeric 
237ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
238ce5531bdSeric 			{
239ce5531bdSeric 				ee->e_dfp = NULL;
240da662164Seric 				ee->e_df = queuename(ee, 'd');
241da662164Seric 				ee->e_df = newstr(ee->e_df);
242ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
243ce5531bdSeric 				{
244ce5531bdSeric 					syserr("sendall: link(%s, %s)",
245ce5531bdSeric 						e->e_df, ee->e_df);
246ce5531bdSeric 				}
247ce5531bdSeric 			}
248ce5531bdSeric #ifdef LOG
249ce5531bdSeric 			if (LogLevel > 4)
250c8b70947Seric 				syslog(LOG_INFO, "%s: clone %s, owner=%s",
251c8b70947Seric 					ee->e_id, e->e_id, owner);
252ce5531bdSeric #endif
253ce5531bdSeric 		}
254ce5531bdSeric 	}
255ce5531bdSeric 
256ce5531bdSeric 	if (owner != NULL)
257ce5531bdSeric 	{
258ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
259ce5531bdSeric 		if (tTd(13, 5))
260ce5531bdSeric 		{
261ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
262ce5531bdSeric 			printaddr(&e->e_from, FALSE);
263ce5531bdSeric 		}
264ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
265ce5531bdSeric 		e->e_errormode = EM_MAIL;
266fce3c07bSeric 		e->e_flags |= EF_NORECEIPT;
267ce5531bdSeric 	}
268ce5531bdSeric 
269c1ac89b1Seric # ifdef QUEUE
270c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
271c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
272c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
273c1ac89b1Seric 	{
274c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2756103d9b4Seric 		queueup(e, TRUE, announcequeueup);
276b056abd0Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
277b056abd0Seric 			queueup(ee, TRUE, announcequeueup);
278c1ac89b1Seric 	}
279c1ac89b1Seric #endif /* QUEUE */
280c1ac89b1Seric 
281b056abd0Seric 	if (splitenv != NULL)
282b056abd0Seric 	{
283b056abd0Seric 		if (tTd(13, 1))
284b056abd0Seric 		{
285b056abd0Seric 			printf("\nsendall: Split queue; remaining queue:\n");
286b056abd0Seric 			printaddr(e->e_sendqueue, TRUE);
287b056abd0Seric 		}
288b056abd0Seric 
289b056abd0Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
290b056abd0Seric 		{
291b056abd0Seric 			CurEnv = ee;
292562ec147Seric 			if (mode != SM_VERIFY)
293562ec147Seric 				openxscript(ee);
294b056abd0Seric 			sendenvelope(ee, mode);
295562ec147Seric 			dropenvelope(ee);
296b056abd0Seric 		}
297b056abd0Seric 
298b056abd0Seric 		CurEnv = e;
299b056abd0Seric 	}
300b056abd0Seric 	sendenvelope(e, mode);
301b056abd0Seric }
302b056abd0Seric 
303b056abd0Seric sendenvelope(e, mode)
304b056abd0Seric 	register ENVELOPE *e;
305b056abd0Seric 	char mode;
306b056abd0Seric {
307b056abd0Seric 	bool oldverbose;
308b056abd0Seric 	int pid;
309b056abd0Seric 	register ADDRESS *q;
310b056abd0Seric 	char *qf;
311b056abd0Seric 	char *id;
312*519e7d80Seric 	bool didany;
313b056abd0Seric 
3147880f3e4Seric 	/*
3157880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3167880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3177880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3187880f3e4Seric 	**  addresses to be sent.
3197880f3e4Seric 	*/
3207880f3e4Seric 
3218d19a23dSeric 	if (bitset(EF_FATALERRS, e->e_flags) &&
3228d19a23dSeric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
3237880f3e4Seric 	{
3247880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3257880f3e4Seric 		return;
3267880f3e4Seric 	}
3277880f3e4Seric 
328ce5531bdSeric 	oldverbose = Verbose;
329c1ac89b1Seric 	switch (mode)
330c1ac89b1Seric 	{
331c1ac89b1Seric 	  case SM_VERIFY:
332c1ac89b1Seric 		Verbose = TRUE;
333c1ac89b1Seric 		break;
334c1ac89b1Seric 
335c1ac89b1Seric 	  case SM_QUEUE:
336c1ac89b1Seric   queueonly:
337c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
338c1ac89b1Seric 		return;
339c1ac89b1Seric 
340c1ac89b1Seric 	  case SM_FORK:
341c1ac89b1Seric 		if (e->e_xfp != NULL)
342c1ac89b1Seric 			(void) fflush(e->e_xfp);
343c1ac89b1Seric 
344b269aa8eSeric # if !HASFLOCK
345c1ac89b1Seric 		/*
3466b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3476b2765c6Seric 		**  the lock is owned by a process, not by an open file
3486b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3496b2765c6Seric 		**  then restart from scratch in the child.
350c1ac89b1Seric 		*/
351c1ac89b1Seric 
3526b2765c6Seric 		/* save id for future use */
3536b2765c6Seric 		id = e->e_id;
3546b2765c6Seric 
3556b2765c6Seric 		/* now drop the envelope in the parent */
3566b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3576b2765c6Seric 		dropenvelope(e);
3586b2765c6Seric 
3596b2765c6Seric 		/* and reacquire in the child */
3606b2765c6Seric 		(void) dowork(id, TRUE, FALSE, e);
3616b2765c6Seric 
3626b2765c6Seric 		return;
3636b2765c6Seric 
3646b2765c6Seric # else /* HASFLOCK */
365c1ac89b1Seric 
366c1ac89b1Seric 		pid = fork();
367c1ac89b1Seric 		if (pid < 0)
368c1ac89b1Seric 		{
369c1ac89b1Seric 			goto queueonly;
370c1ac89b1Seric 		}
371c1ac89b1Seric 		else if (pid > 0)
372c1ac89b1Seric 		{
3730e484fe5Seric 			/* be sure we leave the temp files to our child */
3740e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3750e484fe5Seric 			if (e->e_lockfp != NULL)
3760e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3770e484fe5Seric 			e->e_lockfp = NULL;
3780e484fe5Seric 
3790e484fe5Seric 			/* close any random open files in the envelope */
3800e484fe5Seric 			closexscript(e);
3810e484fe5Seric 			if (e->e_dfp != NULL)
3820e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3830e484fe5Seric 			e->e_dfp = NULL;
3840e484fe5Seric 			e->e_id = e->e_df = NULL;
3859f9b003eSeric 
3869f9b003eSeric 			/* catch intermediate zombie */
3879f9b003eSeric 			(void) waitfor(pid);
388c1ac89b1Seric 			return;
389c1ac89b1Seric 		}
390c1ac89b1Seric 
391c1ac89b1Seric 		/* double fork to avoid zombies */
3929f9b003eSeric 		pid = fork();
3939f9b003eSeric 		if (pid > 0)
394c1ac89b1Seric 			exit(EX_OK);
395c1ac89b1Seric 
396c1ac89b1Seric 		/* be sure we are immune from the terminal */
3977880f3e4Seric 		disconnect(1, e);
398c1ac89b1Seric 
3999f9b003eSeric 		/* prevent parent from waiting if there was an error */
4009f9b003eSeric 		if (pid < 0)
4019f9b003eSeric 		{
4029f9b003eSeric 			e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
4039f9b003eSeric 			finis();
4049f9b003eSeric 		}
4059f9b003eSeric 
406c1ac89b1Seric 		/*
407c1ac89b1Seric 		**  Close any cached connections.
408c1ac89b1Seric 		**
409c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
410c1ac89b1Seric 		**	still knows about the connection.
411c1ac89b1Seric 		**
412c1ac89b1Seric 		**	This should only happen when delivering an error
413c1ac89b1Seric 		**	message.
414c1ac89b1Seric 		*/
415c1ac89b1Seric 
416c1ac89b1Seric 		mci_flush(FALSE, NULL);
417c1ac89b1Seric 
4186b2765c6Seric # endif /* HASFLOCK */
4196b2765c6Seric 
420c1ac89b1Seric 		break;
421c1ac89b1Seric 	}
422c1ac89b1Seric 
423c1ac89b1Seric 	/*
4249c9e68d9Seric 	**  Run through the list and send everything.
4255288e21aSeric 	**
4265288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4275288e21aSeric 	**	result in returned mail.
4289c9e68d9Seric 	*/
4299c9e68d9Seric 
4309c9e68d9Seric 	e->e_nsent = 0;
4315288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
432*519e7d80Seric 	didany = FALSE;
433faad2b16Seric 
434faad2b16Seric 	/* now run through the queue */
4359c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4369c9e68d9Seric 	{
437fdc75a0fSeric #ifdef XDEBUG
438fdc75a0fSeric 		char wbuf[MAXNAME + 20];
439fdc75a0fSeric 
440fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
441fdc75a0fSeric 		checkfd012(wbuf);
442fdc75a0fSeric #endif
4439c9e68d9Seric 		if (mode == SM_VERIFY)
4449c9e68d9Seric 		{
4459c9e68d9Seric 			e->e_to = q->q_paddr;
4469c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4478dfca105Seric 			{
448fafe46e1Seric 				if (q->q_host != NULL && q->q_host[0] != '\0')
4498dfca105Seric 					message("deliverable: mailer %s, host %s, user %s",
4508dfca105Seric 						q->q_mailer->m_name,
4518dfca105Seric 						q->q_host,
4528dfca105Seric 						q->q_user);
453fafe46e1Seric 				else
454fafe46e1Seric 					message("deliverable: mailer %s, user %s",
455fafe46e1Seric 						q->q_mailer->m_name,
456fafe46e1Seric 						q->q_user);
4578dfca105Seric 			}
4589c9e68d9Seric 		}
4599c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4609c9e68d9Seric 		{
4619c9e68d9Seric # ifdef QUEUE
4629c9e68d9Seric 			/*
4639c9e68d9Seric 			**  Checkpoint the send list every few addresses
4649c9e68d9Seric 			*/
4659c9e68d9Seric 
4669c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4679c9e68d9Seric 			{
4689c9e68d9Seric 				queueup(e, TRUE, FALSE);
4699c9e68d9Seric 				e->e_nsent = 0;
4709c9e68d9Seric 			}
4719c9e68d9Seric # endif /* QUEUE */
4729c9e68d9Seric 			(void) deliver(e, q);
473*519e7d80Seric 			didany = TRUE;
4749c9e68d9Seric 		}
4759c9e68d9Seric 	}
4769c9e68d9Seric 	Verbose = oldverbose;
477*519e7d80Seric 	if (didany)
478*519e7d80Seric 	{
479*519e7d80Seric 		e->e_dtime = curtime();
480*519e7d80Seric 		e->e_ntries++;
481*519e7d80Seric 	}
4829c9e68d9Seric 
483fdc75a0fSeric #ifdef XDEBUG
484fdc75a0fSeric 	checkfd012("end of sendenvelope");
485fdc75a0fSeric #endif
486fdc75a0fSeric 
4879c9e68d9Seric 	if (mode == SM_FORK)
4889c9e68d9Seric 		finis();
4899c9e68d9Seric }
4909c9e68d9Seric /*
4919c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4929c9e68d9Seric **
4939c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4949c9e68d9Seric **	two processes on the same stack!!!
4959c9e68d9Seric **
4969c9e68d9Seric **	Parameters:
4979c9e68d9Seric **		none.
4989c9e68d9Seric **
4999c9e68d9Seric **	Returns:
5009c9e68d9Seric **		From a macro???  You've got to be kidding!
5019c9e68d9Seric **
5029c9e68d9Seric **	Side Effects:
5039c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
5049c9e68d9Seric **			pid of child in parent, zero in child.
5059c9e68d9Seric **			-1 on unrecoverable error.
5069c9e68d9Seric **
5079c9e68d9Seric **	Notes:
5089c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
5099c9e68d9Seric **		vfork for you.....
5109c9e68d9Seric */
5119c9e68d9Seric 
5129c9e68d9Seric # define NFORKTRIES	5
5139c9e68d9Seric 
5149c9e68d9Seric # ifndef FORK
5159c9e68d9Seric # define FORK	fork
5169c9e68d9Seric # endif
5179c9e68d9Seric 
5189c9e68d9Seric # define DOFORK(fORKfN) \
5199c9e68d9Seric {\
5209c9e68d9Seric 	register int i;\
5219c9e68d9Seric \
5229c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
5239c9e68d9Seric 	{\
5249c9e68d9Seric 		pid = fORKfN();\
5259c9e68d9Seric 		if (pid >= 0)\
5269c9e68d9Seric 			break;\
5279c9e68d9Seric 		if (i > 0)\
5289c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5299c9e68d9Seric 	}\
5309c9e68d9Seric }
5319c9e68d9Seric /*
5329c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5339c9e68d9Seric **
5349c9e68d9Seric **	Parameters:
5359c9e68d9Seric **		none.
5369c9e68d9Seric **
5379c9e68d9Seric **	Returns:
5389c9e68d9Seric **		pid of child in parent.
5399c9e68d9Seric **		zero in child.
5409c9e68d9Seric **		-1 on error.
5419c9e68d9Seric **
5429c9e68d9Seric **	Side Effects:
5439c9e68d9Seric **		returns twice, once in parent and once in child.
5449c9e68d9Seric */
5459c9e68d9Seric 
5469c9e68d9Seric dofork()
5479c9e68d9Seric {
5489c9e68d9Seric 	register int pid;
5499c9e68d9Seric 
5509c9e68d9Seric 	DOFORK(fork);
5519c9e68d9Seric 	return (pid);
5529c9e68d9Seric }
5539c9e68d9Seric /*
55413bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
55513bbc08cSeric **
55613bbc08cSeric **	This routine delivers to everyone on the same host as the
55713bbc08cSeric **	user on the head of the list.  It is clever about mailers
55813bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
55913bbc08cSeric **	that it will deliver to all these addresses however -- so
56013bbc08cSeric **	deliver should be called once for each address on the
56113bbc08cSeric **	list.
56225a99e2eSeric **
56325a99e2eSeric **	Parameters:
564588cad61Seric **		e -- the envelope to deliver.
565c77d1c25Seric **		firstto -- head of the address list to deliver to.
56625a99e2eSeric **
56725a99e2eSeric **	Returns:
56825a99e2eSeric **		zero -- successfully delivered.
56925a99e2eSeric **		else -- some failure, see ExitStat for more info.
57025a99e2eSeric **
57125a99e2eSeric **	Side Effects:
57225a99e2eSeric **		The standard input is passed off to someone.
57325a99e2eSeric */
57425a99e2eSeric 
575588cad61Seric deliver(e, firstto)
576588cad61Seric 	register ENVELOPE *e;
577c77d1c25Seric 	ADDRESS *firstto;
57825a99e2eSeric {
57978442df3Seric 	char *host;			/* host being sent to */
58078442df3Seric 	char *user;			/* user being sent to */
58125a99e2eSeric 	char **pvp;
5825dfc646bSeric 	register char **mvp;
58325a99e2eSeric 	register char *p;
584588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5856259796dSeric 	ADDRESS *ctladdr;
586b31e7f2bSeric 	register MCI *mci;
587c77d1c25Seric 	register ADDRESS *to = firstto;
588c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
589772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
590911693bfSbostic 	int rcode;			/* response code */
591e103b48fSeric 	char *firstsig;			/* signature of firstto */
5929c9e68d9Seric 	int pid;
5939c9e68d9Seric 	char *curhost;
5949c9e68d9Seric 	int mpvect[2];
5959c9e68d9Seric 	int rpvect[2];
596ee6bf8dfSeric 	char *pv[MAXPV+1];
597579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
598ee6bf8dfSeric 	char buf[MAXNAME];
599c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
600fabb3bd4Seric 	extern int checkcompat();
60125a99e2eSeric 
60235490626Seric 	errno = 0;
603ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
6045dfc646bSeric 		return (0);
60525a99e2eSeric 
6069d4a8008Seric #if NAMED_BIND
607912a731aSbostic 	/* unless interactive, try twice, over a minute */
6088d19a23dSeric 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP)
6098d19a23dSeric 	{
610912a731aSbostic 		_res.retrans = 30;
611912a731aSbostic 		_res.retry = 2;
612912a731aSbostic 	}
613d4bd8f0eSbostic #endif
614912a731aSbostic 
61551552439Seric 	m = to->q_mailer;
61651552439Seric 	host = to->q_host;
617c9be6216Seric 	CurEnv = e;			/* just in case */
6184384d521Seric 	e->e_statmsg = NULL;
619df106f0bSeric 	SmtpError[0] = '\0';
62051552439Seric 
6216ef48975Seric 	if (tTd(10, 1))
622562ec147Seric 		printf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n",
623562ec147Seric 			e->e_id, m->m_name, host, to->q_user);
624ab81ee53Seric 	if (tTd(10, 100))
625ab81ee53Seric 		printopenfds(FALSE);
626f3dbc832Seric 
627f3dbc832Seric 	/*
628f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
629f3dbc832Seric 	**  connections now, just mark these addresses and return.
630f3dbc832Seric 	**	This is useful if we want to batch connections to
631f3dbc832Seric 	**	reduce load.  This will cause the messages to be
632f3dbc832Seric 	**	queued up, and a daemon will come along to send the
633f3dbc832Seric 	**	messages later.
634f3dbc832Seric 	**		This should be on a per-mailer basis.
635f3dbc832Seric 	*/
636f3dbc832Seric 
63779aa5155Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
638f3dbc832Seric 	{
639f3dbc832Seric 		for (; to != NULL; to = to->q_next)
640f4560e80Seric 		{
641ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
642c6e18ac5Seric 			    to->q_mailer != m)
643f4560e80Seric 				continue;
644268a25e1Seric 			to->q_flags |= QQUEUEUP;
645588cad61Seric 			e->e_to = to->q_paddr;
64608b25121Seric 			message("queued");
6472f624c86Seric 			if (LogLevel > 8)
6484dee0003Seric 				logdelivery(m, NULL, "queued", NULL, e);
649f4560e80Seric 		}
650588cad61Seric 		e->e_to = NULL;
651f3dbc832Seric 		return (0);
652f3dbc832Seric 	}
653f3dbc832Seric 
65425a99e2eSeric 	/*
6555dfc646bSeric 	**  Do initial argv setup.
6565dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6575dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6585dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6595dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6605dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6613bea8136Seric 	**		The from address rewrite is expected to make
6623bea8136Seric 	**		the address relative to the other end.
6635dfc646bSeric 	*/
6645dfc646bSeric 
66578442df3Seric 	/* rewrite from address, using rewriting rules */
666efe54562Seric 	rcode = EX_OK;
667efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
668efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
669efe54562Seric 					   &rcode, e));
670ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
671588cad61Seric 	define('h', host, e);			/* to host */
6725dfc646bSeric 	Errors = 0;
6735dfc646bSeric 	pvp = pv;
6745dfc646bSeric 	*pvp++ = m->m_argv[0];
6755dfc646bSeric 
6765dfc646bSeric 	/* insert -f or -r flag as appropriate */
67757fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6785dfc646bSeric 	{
67957fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6805dfc646bSeric 			*pvp++ = "-f";
6815dfc646bSeric 		else
6825dfc646bSeric 			*pvp++ = "-r";
683c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6845dfc646bSeric 	}
6855dfc646bSeric 
6865dfc646bSeric 	/*
6875dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6885dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6895dfc646bSeric 	**  be one of these, and there are only a few more slots
6905dfc646bSeric 	**  in the pv after it.
6915dfc646bSeric 	*/
6925dfc646bSeric 
6935dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6945dfc646bSeric 	{
6952bc47524Seric 		/* can't use strchr here because of sign extension problems */
6962bc47524Seric 		while (*p != '\0')
6972bc47524Seric 		{
6982bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6992bc47524Seric 			{
7002bc47524Seric 				if (*p == 'u')
7015dfc646bSeric 					break;
7022bc47524Seric 			}
7032bc47524Seric 		}
7042bc47524Seric 
7052bc47524Seric 		if (*p != '\0')
7065dfc646bSeric 			break;
7075dfc646bSeric 
7085dfc646bSeric 		/* this entry is safe -- go ahead and process it */
709588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
7105dfc646bSeric 		*pvp++ = newstr(buf);
7115dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
7125dfc646bSeric 		{
71308b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
7145dfc646bSeric 			return (-1);
7155dfc646bSeric 		}
7165dfc646bSeric 	}
717c579ef51Seric 
71833db8731Seric 	/*
71933db8731Seric 	**  If we have no substitution for the user name in the argument
72033db8731Seric 	**  list, we know that we must supply the names otherwise -- and
72133db8731Seric 	**  SMTP is the answer!!
72233db8731Seric 	*/
72333db8731Seric 
7245dfc646bSeric 	if (*mvp == NULL)
725c579ef51Seric 	{
726c579ef51Seric 		/* running SMTP */
7272c7e1b8dSeric # ifdef SMTP
728c579ef51Seric 		clever = TRUE;
729c579ef51Seric 		*pvp = NULL;
7306c2c3107Seric # else /* SMTP */
73133db8731Seric 		/* oops!  we don't implement SMTP */
732df106f0bSeric 		syserr("554 SMTP style mailer not implemented");
7332c7e1b8dSeric 		return (EX_SOFTWARE);
7346c2c3107Seric # endif /* SMTP */
735c579ef51Seric 	}
7365dfc646bSeric 
7375dfc646bSeric 	/*
7385dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7395dfc646bSeric 	**  run through our address list and append all the addresses
7405dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7415dfc646bSeric 	**  always send another copy later.
7425dfc646bSeric 	*/
7435dfc646bSeric 
7445dfc646bSeric 	tobuf[0] = '\0';
745588cad61Seric 	e->e_to = tobuf;
7466259796dSeric 	ctladdr = NULL;
747e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7485dfc646bSeric 	for (; to != NULL; to = to->q_next)
7495dfc646bSeric 	{
7505dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
75157fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7525dfc646bSeric 			break;
7535dfc646bSeric 
7545dfc646bSeric 		/* if already sent or not for this host, don't send */
755ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
756e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
757e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7585dfc646bSeric 			continue;
7596259796dSeric 
7604b22ea87Seric 		/* avoid overflowing tobuf */
761aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7624b22ea87Seric 			break;
7634b22ea87Seric 
7646ef48975Seric 		if (tTd(10, 1))
765772e6e50Seric 		{
766772e6e50Seric 			printf("\nsend to ");
767772e6e50Seric 			printaddr(to, FALSE);
768772e6e50Seric 		}
769772e6e50Seric 
7706259796dSeric 		/* compute effective uid/gid when sending */
7710ed512e3Seric 		if (bitnset(M_RUNASRCPT, to->q_mailer->m_flags))
7726259796dSeric 			ctladdr = getctladdr(to);
7736259796dSeric 
7745dfc646bSeric 		user = to->q_user;
775588cad61Seric 		e->e_to = to->q_paddr;
77675f1ade9Seric 		if (tTd(10, 5))
77775f1ade9Seric 		{
77875f1ade9Seric 			printf("deliver: QDONTSEND ");
77975f1ade9Seric 			printaddr(to, FALSE);
78075f1ade9Seric 		}
781ee4b0922Seric 		to->q_flags |= QDONTSEND;
7825dfc646bSeric 
7835dfc646bSeric 		/*
7845dfc646bSeric 		**  Check to see that these people are allowed to
7855dfc646bSeric 		**  talk to each other.
7862a6e0786Seric 		*/
7872a6e0786Seric 
78869582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
78969582d2fSeric 		{
7900ed512e3Seric 			e->e_flags |= EF_NORETURN;
79108b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
7924dee0003Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e);
79369582d2fSeric 			continue;
79469582d2fSeric 		}
7959bfcea71Seric #if NAMED_BIND
7969bfcea71Seric 		h_errno = 0;
7979bfcea71Seric #endif
798fabb3bd4Seric 		rcode = checkcompat(to, e);
7991793c9c5Seric 		if (rcode != EX_OK)
8005dfc646bSeric 		{
8011000c37aSeric 			markfailure(e, to, rcode);
8024dee0003Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
8035dfc646bSeric 			continue;
8045dfc646bSeric 		}
8052a6e0786Seric 
8062a6e0786Seric 		/*
8079ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
8089ec9501bSeric 		**	about them.
80925a99e2eSeric 		*/
81025a99e2eSeric 
81157fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
81225a99e2eSeric 		{
8131d8f1806Seric 			stripquotes(user);
8141d8f1806Seric 			stripquotes(host);
81525a99e2eSeric 		}
81625a99e2eSeric 
817cdb828c5Seric 		/* hack attack -- delivermail compatibility */
818cdb828c5Seric 		if (m == ProgMailer && *user == '|')
819cdb828c5Seric 			user++;
820cdb828c5Seric 
82125a99e2eSeric 		/*
8223efaed6eSeric 		**  If an error message has already been given, don't
8233efaed6eSeric 		**	bother to send to this address.
8243efaed6eSeric 		**
8253efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
8263efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
8273efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
8283efaed6eSeric 		*/
8293efaed6eSeric 
8306cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8313efaed6eSeric 			continue;
8323efaed6eSeric 
833f2fec898Seric 		/* save statistics.... */
834588cad61Seric 		markstats(e, to);
835f2fec898Seric 
8363efaed6eSeric 		/*
83725a99e2eSeric 		**  See if this user name is "special".
83825a99e2eSeric 		**	If the user name has a slash in it, assume that this
83951552439Seric 		**	is a file -- send it off without further ado.  Note
84051552439Seric 		**	that this type of addresses is not processed along
84151552439Seric 		**	with the others, so we fudge on the To person.
84225a99e2eSeric 		*/
84325a99e2eSeric 
8442c017f8dSeric 		if (m == FileMailer)
84525a99e2eSeric 		{
84603463a75Seric 			rcode = mailfile(user, ctladdr, e);
84703463a75Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
848dde5acadSeric 			if (rcode == EX_OK)
849dde5acadSeric 				to->q_flags |= QSENT;
8505dfc646bSeric 			continue;
85125a99e2eSeric 		}
85225a99e2eSeric 
85313bbc08cSeric 		/*
85413bbc08cSeric 		**  Address is verified -- add this user to mailer
85513bbc08cSeric 		**  argv, and add it to the print list of recipients.
85613bbc08cSeric 		*/
85713bbc08cSeric 
858508daeccSeric 		/* link together the chain of recipients */
859508daeccSeric 		to->q_tchain = tochain;
860508daeccSeric 		tochain = to;
861508daeccSeric 
8625dfc646bSeric 		/* create list of users for error messages */
863db8841e9Seric 		(void) strcat(tobuf, ",");
864db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
865588cad61Seric 		define('u', user, e);		/* to user */
866b6f89e6eSeric 		p = to->q_home;
867b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
868b6f89e6eSeric 			p = ctladdr->q_home;
869b6f89e6eSeric 		define('z', p, e);	/* user's home */
8705dfc646bSeric 
871c579ef51Seric 		/*
872508daeccSeric 		**  Expand out this user into argument list.
873c579ef51Seric 		*/
874c579ef51Seric 
875508daeccSeric 		if (!clever)
876c579ef51Seric 		{
877588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8785dfc646bSeric 			*pvp++ = newstr(buf);
8795dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8805dfc646bSeric 			{
8815dfc646bSeric 				/* allow some space for trailing parms */
8825dfc646bSeric 				break;
8835dfc646bSeric 			}
8845dfc646bSeric 		}
885c579ef51Seric 	}
8865dfc646bSeric 
887145b49b1Seric 	/* see if any addresses still exist */
888145b49b1Seric 	if (tobuf[0] == '\0')
889c579ef51Seric 	{
890588cad61Seric 		define('g', (char *) NULL, e);
891145b49b1Seric 		return (0);
892c579ef51Seric 	}
893145b49b1Seric 
8945dfc646bSeric 	/* print out messages as full list */
89563780dbdSeric 	e->e_to = tobuf + 1;
8965dfc646bSeric 
8975dfc646bSeric 	/*
8985dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8995dfc646bSeric 	*/
9005dfc646bSeric 
901c579ef51Seric 	while (!clever && *++mvp != NULL)
9025dfc646bSeric 	{
903588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
9045dfc646bSeric 		*pvp++ = newstr(buf);
9055dfc646bSeric 		if (pvp >= &pv[MAXPV])
90608b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
9075dfc646bSeric 	}
9085dfc646bSeric 	*pvp++ = NULL;
9095dfc646bSeric 
91025a99e2eSeric 	/*
91125a99e2eSeric 	**  Call the mailer.
9126328bdf7Seric 	**	The argument vector gets built, pipes
91325a99e2eSeric 	**	are created as necessary, and we fork & exec as
9146328bdf7Seric 	**	appropriate.
915c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
91625a99e2eSeric 	*/
91725a99e2eSeric 
918960e665aSeric 	/*XXX this seems a bit wierd */
9194899f6b5Seric 	if (ctladdr == NULL && m != ProgMailer &&
9204899f6b5Seric 	    bitset(QGOODUID, e->e_from.q_flags))
921960e665aSeric 		ctladdr = &e->e_from;
922960e665aSeric 
9239d4a8008Seric #if NAMED_BIND
9242bcc6d2dSeric 	if (ConfigLevel < 2)
925912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
926134746fbSeric #endif
9279c9e68d9Seric 
9289c9e68d9Seric 	if (tTd(11, 1))
929134746fbSeric 	{
9309c9e68d9Seric 		printf("openmailer:");
9319c9e68d9Seric 		printav(pv);
9329c9e68d9Seric 	}
9339c9e68d9Seric 	errno = 0;
9349bfcea71Seric #if NAMED_BIND
9359bfcea71Seric 	h_errno = 0;
9369bfcea71Seric #endif
9379c9e68d9Seric 
9389c9e68d9Seric 	CurHostName = m->m_mailer;
9399c9e68d9Seric 
9409c9e68d9Seric 	/*
9419c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9429c9e68d9Seric 	**  connection.
9439c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9449c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9459c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9469c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9479c9e68d9Seric 	*/
9489c9e68d9Seric 
9499c9e68d9Seric 	curhost = NULL;
950c931b82bSeric 	SmtpPhase = NULL;
951df106f0bSeric 	mci = NULL;
9529c9e68d9Seric 
9537febfd66Seric #ifdef XDEBUG
9547febfd66Seric 	{
9557febfd66Seric 		char wbuf[MAXLINE];
9567febfd66Seric 
9577febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9587febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
9597febfd66Seric 		checkfd012(wbuf);
9607febfd66Seric 	}
9617febfd66Seric #endif
9627febfd66Seric 
963cc7496f5Seric 	/* check for 8-bit available */
964cc7496f5Seric 	if (bitset(EF_HAS8BIT, e->e_flags) &&
965cc7496f5Seric 	    bitnset(M_7BITS, m->m_flags) &&
966cc7496f5Seric 	    !bitset(MM_MIME8BIT, MimeMode))
967cc7496f5Seric 	{
968cc7496f5Seric 		usrerr("554 Cannot send 8-bit data to 7-bit destination");
969cc7496f5Seric 		rcode = EX_DATAERR;
970cc7496f5Seric 		goto give_up;
971cc7496f5Seric 	}
972cc7496f5Seric 
9739c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9749c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9759c9e68d9Seric 	{
9769c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9779c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9789c9e68d9Seric 		mci->mci_in = stdin;
9799c9e68d9Seric 		mci->mci_out = stdout;
9809c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9819c9e68d9Seric 		mci->mci_mailer = m;
9829c9e68d9Seric 	}
9839c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9849c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9859c9e68d9Seric 	{
9869c9e68d9Seric #ifdef DAEMON
9879c9e68d9Seric 		register int i;
9889c9e68d9Seric 		register u_short port;
9899c9e68d9Seric 
990a5d44642Seric 		if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
991a5d44642Seric 		{
992a5d44642Seric 			syserr("null host name for %s mailer", m->m_mailer);
993a5d44642Seric 			rcode = EX_CONFIG;
994a5d44642Seric 			goto give_up;
995a5d44642Seric 		}
996a5d44642Seric 
9979c9e68d9Seric 		CurHostName = pv[1];
9989c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9999c9e68d9Seric 
10009c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
10019c9e68d9Seric 		{
1002a5d44642Seric 			syserr("null host signature for %s", pv[1]);
1003dfebe401Seric 			rcode = EX_CONFIG;
1004b31e7f2bSeric 			goto give_up;
1005b31e7f2bSeric 		}
10069c9e68d9Seric 
10079c9e68d9Seric 		if (!clever)
10089c9e68d9Seric 		{
10099c9e68d9Seric 			syserr("554 non-clever IPC");
1010df106f0bSeric 			rcode = EX_CONFIG;
10119c9e68d9Seric 			goto give_up;
10129c9e68d9Seric 		}
10139c9e68d9Seric 		if (pv[2] != NULL)
10149c9e68d9Seric 			port = atoi(pv[2]);
10159c9e68d9Seric 		else
10169c9e68d9Seric 			port = 0;
10179c9e68d9Seric tryhost:
10189c9e68d9Seric 		while (*curhost != '\0')
10199c9e68d9Seric 		{
10209c9e68d9Seric 			register char *p;
10219c9e68d9Seric 			static char hostbuf[MAXNAME];
10229c9e68d9Seric 
10239c9e68d9Seric 			/* pull the next host from the signature */
10249c9e68d9Seric 			p = strchr(curhost, ':');
10259c9e68d9Seric 			if (p == NULL)
10269c9e68d9Seric 				p = &curhost[strlen(curhost)];
1027df106f0bSeric 			if (p == curhost)
1028df106f0bSeric 			{
1029df106f0bSeric 				syserr("deliver: null host name in signature");
10308087428aSeric 				curhost++;
1031df106f0bSeric 				continue;
1032df106f0bSeric 			}
10339c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
10349c9e68d9Seric 			hostbuf[p - curhost] = '\0';
10359c9e68d9Seric 			if (*p != '\0')
10369c9e68d9Seric 				p++;
10379c9e68d9Seric 			curhost = p;
10389c9e68d9Seric 
10399c9e68d9Seric 			/* see if we already know that this host is fried */
10409c9e68d9Seric 			CurHostName = hostbuf;
10419c9e68d9Seric 			mci = mci_get(hostbuf, m);
10429c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
10439c9e68d9Seric 			{
10449c9e68d9Seric 				if (tTd(11, 1))
10459c9e68d9Seric 				{
10469c9e68d9Seric 					printf("openmailer: ");
10474052916eSeric 					mci_dump(mci, FALSE);
10489c9e68d9Seric 				}
10499c9e68d9Seric 				CurHostName = mci->mci_host;
10509c9e68d9Seric 				break;
10519c9e68d9Seric 			}
10529c9e68d9Seric 			mci->mci_mailer = m;
10539c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
10549c9e68d9Seric 				continue;
10559c9e68d9Seric 
10569c9e68d9Seric 			/* try the connection */
10579c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
10589c9e68d9Seric 			message("Connecting to %s (%s)...",
10599c9e68d9Seric 				hostbuf, m->m_name);
10609c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
10619c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
10629c9e68d9Seric 			mci->mci_exitstat = i;
10639c9e68d9Seric 			mci->mci_errno = errno;
10649d4a8008Seric #if NAMED_BIND
1065f170942cSeric 			mci->mci_herrno = h_errno;
1066f170942cSeric #endif
10679c9e68d9Seric 			if (i == EX_OK)
10689c9e68d9Seric 			{
10699c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10709c9e68d9Seric 				mci_cache(mci);
1071f170942cSeric 				if (TrafficLogFile != NULL)
1072f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1073f170942cSeric 						getpid(), hostbuf);
10749c9e68d9Seric 				break;
10759c9e68d9Seric 			}
10769c9e68d9Seric 			else if (tTd(11, 1))
10779c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10789c9e68d9Seric 					i, errno);
10799c9e68d9Seric 
10809c9e68d9Seric 			/* enter status of this host */
10819c9e68d9Seric 			setstat(i);
1082df106f0bSeric 
1083df106f0bSeric 			/* should print some message here for -v mode */
1084df106f0bSeric 		}
1085df106f0bSeric 		if (mci == NULL)
1086df106f0bSeric 		{
1087df106f0bSeric 			syserr("deliver: no host name");
1088df106f0bSeric 			rcode = EX_OSERR;
1089df106f0bSeric 			goto give_up;
10909c9e68d9Seric 		}
10919c9e68d9Seric 		mci->mci_pid = 0;
10929c9e68d9Seric #else /* no DAEMON */
10939c9e68d9Seric 		syserr("554 openmailer: no IPC");
10949c9e68d9Seric 		if (tTd(11, 1))
10959c9e68d9Seric 			printf("openmailer: NULL\n");
1096df106f0bSeric 		rcode = EX_UNAVAILABLE;
1097df106f0bSeric 		goto give_up;
10989c9e68d9Seric #endif /* DAEMON */
10999c9e68d9Seric 	}
11009c9e68d9Seric 	else
11019c9e68d9Seric 	{
1102f170942cSeric 		if (TrafficLogFile != NULL)
11030e3bfef5Seric 		{
1104f170942cSeric 			char **av;
1105f170942cSeric 
1106f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1107f170942cSeric 			for (av = pv; *av != NULL; av++)
1108f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1109f170942cSeric 			fprintf(TrafficLogFile, "\n");
11106fe8c3bcSeric 		}
11116fe8c3bcSeric 
11129c9e68d9Seric 		/* create a pipe to shove the mail through */
11139c9e68d9Seric 		if (pipe(mpvect) < 0)
11149c9e68d9Seric 		{
11150e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
11160e3bfef5Seric 				e->e_to, m->m_name);
11179c9e68d9Seric 			if (tTd(11, 1))
11189c9e68d9Seric 				printf("openmailer: NULL\n");
11199c9e68d9Seric 			rcode = EX_OSERR;
11209c9e68d9Seric 			goto give_up;
11219c9e68d9Seric 		}
11229c9e68d9Seric 
11239c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
11249c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
11259c9e68d9Seric 		{
11260e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
11270e3bfef5Seric 				e->e_to, m->m_name);
11289c9e68d9Seric 			(void) close(mpvect[0]);
11299c9e68d9Seric 			(void) close(mpvect[1]);
11309c9e68d9Seric 			if (tTd(11, 1))
11319c9e68d9Seric 				printf("openmailer: NULL\n");
11329c9e68d9Seric 			rcode = EX_OSERR;
11339c9e68d9Seric 			goto give_up;
11349c9e68d9Seric 		}
11359c9e68d9Seric 
11369c9e68d9Seric 		/*
11379c9e68d9Seric 		**  Actually fork the mailer process.
11389c9e68d9Seric 		**	DOFORK is clever about retrying.
11399c9e68d9Seric 		**
11409c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
11419c9e68d9Seric 		**	around so that endmail will get it.
11429c9e68d9Seric 		*/
11439c9e68d9Seric 
11449c9e68d9Seric 		if (e->e_xfp != NULL)
11459c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
11469c9e68d9Seric 		(void) fflush(stdout);
11479c9e68d9Seric # ifdef SIGCHLD
11482b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
11499c9e68d9Seric # endif /* SIGCHLD */
11509c9e68d9Seric 		DOFORK(FORK);
11519c9e68d9Seric 		/* pid is set by DOFORK */
11529c9e68d9Seric 		if (pid < 0)
11539c9e68d9Seric 		{
11549c9e68d9Seric 			/* failure */
11550e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
11560e3bfef5Seric 				e->e_to, m->m_name);
11579c9e68d9Seric 			(void) close(mpvect[0]);
11589c9e68d9Seric 			(void) close(mpvect[1]);
11599c9e68d9Seric 			if (clever)
11609c9e68d9Seric 			{
11619c9e68d9Seric 				(void) close(rpvect[0]);
11629c9e68d9Seric 				(void) close(rpvect[1]);
11639c9e68d9Seric 			}
11649c9e68d9Seric 			if (tTd(11, 1))
11659c9e68d9Seric 				printf("openmailer: NULL\n");
11669c9e68d9Seric 			rcode = EX_OSERR;
11679c9e68d9Seric 			goto give_up;
11689c9e68d9Seric 		}
11699c9e68d9Seric 		else if (pid == 0)
11709c9e68d9Seric 		{
11719c9e68d9Seric 			int i;
11729c9e68d9Seric 			int saveerrno;
11739c9e68d9Seric 			char **ep;
11749c9e68d9Seric 			char *env[MAXUSERENVIRON];
11759c9e68d9Seric 			extern char **environ;
11769c9e68d9Seric 			extern int DtableSize;
11779c9e68d9Seric 
11789c9e68d9Seric 			/* child -- set up input & exec mailer */
11792b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
11802b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
11812b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11829c9e68d9Seric 
118344f2317fSeric 			/* reset user and group */
1184acc57dbfSeric 			if (bitnset(M_SPECIFIC_UID, m->m_flags))
1185acc57dbfSeric 			{
1186acc57dbfSeric 				(void) setgid(m->m_gid);
1187acc57dbfSeric 				(void) setuid(m->m_uid);
1188acc57dbfSeric 			}
1189ab9cdf66Seric 			else if (ctladdr != NULL && ctladdr->q_uid != 0)
119044f2317fSeric 			{
119144f2317fSeric 				(void) initgroups(ctladdr->q_ruser?
119244f2317fSeric 					ctladdr->q_ruser: ctladdr->q_user,
119344f2317fSeric 					ctladdr->q_gid);
119451cfe111Seric 				(void) setgid(ctladdr->q_gid);
119544f2317fSeric 				(void) setuid(ctladdr->q_uid);
119644f2317fSeric 			}
1197ab9cdf66Seric 			else
1198ab9cdf66Seric 			{
1199ab9cdf66Seric 				(void) initgroups(DefUser, DefGid);
1200ab9cdf66Seric 				if (m->m_gid == 0)
1201ab9cdf66Seric 					(void) setgid(DefGid);
1202ab9cdf66Seric 				else
1203ab9cdf66Seric 					(void) setgid(m->m_gid);
1204ab9cdf66Seric 				if (m->m_uid == 0)
1205ab9cdf66Seric 					(void) setuid(DefUid);
1206ab9cdf66Seric 				else
1207ab9cdf66Seric 					(void) setuid(m->m_uid);
120844f2317fSeric 			}
120944f2317fSeric 
121044f2317fSeric 			if (tTd(11, 2))
121144f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
121244f2317fSeric 					getuid(), geteuid());
121344f2317fSeric 
1214b986f6aaSeric 			/* move into some "safe" directory */
1215b986f6aaSeric 			if (m->m_execdir != NULL)
1216b986f6aaSeric 			{
1217b986f6aaSeric 				char *p, *q;
1218b986f6aaSeric 				char buf[MAXLINE];
1219b986f6aaSeric 
1220b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1221b986f6aaSeric 				{
1222b986f6aaSeric 					q = strchr(p, ':');
1223b986f6aaSeric 					if (q != NULL)
1224b986f6aaSeric 						*q = '\0';
1225b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1226b986f6aaSeric 					if (q != NULL)
1227b986f6aaSeric 						*q++ = ':';
1228b986f6aaSeric 					if (tTd(11, 20))
1229b986f6aaSeric 						printf("openmailer: trydir %s\n",
1230b986f6aaSeric 							buf);
1231b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1232b986f6aaSeric 						break;
1233b986f6aaSeric 				}
1234b986f6aaSeric 			}
1235b986f6aaSeric 
12369c9e68d9Seric 			/* arrange to filter std & diag output of command */
12379c9e68d9Seric 			if (clever)
12389c9e68d9Seric 			{
12399c9e68d9Seric 				(void) close(rpvect[0]);
12406fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
12416fe8c3bcSeric 				{
12420e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
12430e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
12446fe8c3bcSeric 					_exit(EX_OSERR);
12456fe8c3bcSeric 				}
12469c9e68d9Seric 				(void) close(rpvect[1]);
12479c9e68d9Seric 			}
12488b40b0a4Seric 			else if (OpMode == MD_SMTP || OpMode == MD_DAEMON ||
12498b40b0a4Seric 				  HoldErrs || DisConnected)
12509c9e68d9Seric 			{
12519c9e68d9Seric 				/* put mailer output in transcript */
12526fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
12536fe8c3bcSeric 				{
12540e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
12550e3bfef5Seric 						e->e_to, m->m_name,
12566fe8c3bcSeric 						fileno(e->e_xfp));
12576fe8c3bcSeric 					_exit(EX_OSERR);
12589c9e68d9Seric 				}
12596fe8c3bcSeric 			}
12606fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
12616fe8c3bcSeric 			{
12620e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
12630e3bfef5Seric 					e->e_to, m->m_name);
12646fe8c3bcSeric 				_exit(EX_OSERR);
12656fe8c3bcSeric 			}
12669c9e68d9Seric 
12679c9e68d9Seric 			/* arrange to get standard input */
12689c9e68d9Seric 			(void) close(mpvect[1]);
12699c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
12709c9e68d9Seric 			{
12710e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
12720e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
12739c9e68d9Seric 				_exit(EX_OSERR);
12749c9e68d9Seric 			}
12759c9e68d9Seric 			(void) close(mpvect[0]);
12769c9e68d9Seric 
12779c9e68d9Seric 			/* arrange for all the files to be closed */
12789c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12799c9e68d9Seric 			{
12809c9e68d9Seric 				register int j;
128144f2317fSeric 
12829c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12839c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
12849c9e68d9Seric 			}
12859c9e68d9Seric 
12869f9b003eSeric 			/*
12879f9b003eSeric 			**  Set up the mailer environment
12889f9b003eSeric 			**	TZ is timezone information.
12899f9b003eSeric 			**	SYSTYPE is Apollo software sys type (required).
12909f9b003eSeric 			**	ISP is Apollo hardware system type (required).
12919f9b003eSeric 			*/
12929f9b003eSeric 
12939c9e68d9Seric 			i = 0;
12949c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12959c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12969c9e68d9Seric 			{
12979f9b003eSeric 				if (strncmp(*ep, "TZ=", 3) == 0 ||
12989f9b003eSeric 				    strncmp(*ep, "ISP=", 4) == 0 ||
12999f9b003eSeric 				    strncmp(*ep, "SYSTYPE=", 8) == 0)
13009c9e68d9Seric 					env[i++] = *ep;
13019c9e68d9Seric 			}
13029c9e68d9Seric 			env[i++] = NULL;
13039c9e68d9Seric 
1304929277f2Seric 			/* run disconnected from terminal */
1305929277f2Seric 			(void) setsid();
1306929277f2Seric 
13079c9e68d9Seric 			/* try to execute the mailer */
13089c9e68d9Seric 			execve(m->m_mailer, pv, env);
13099c9e68d9Seric 			saveerrno = errno;
13109c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
13110ed512e3Seric 			if (bitnset(M_LOCALMAILER, m->m_flags) ||
13120ed512e3Seric 			    transienterror(saveerrno))
13137c941fd2Seric 				_exit(EX_OSERR);
13149c9e68d9Seric 			_exit(EX_UNAVAILABLE);
13159c9e68d9Seric 		}
13169c9e68d9Seric 
13179c9e68d9Seric 		/*
13189c9e68d9Seric 		**  Set up return value.
13199c9e68d9Seric 		*/
13209c9e68d9Seric 
13219c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
13229c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
13239c9e68d9Seric 		mci->mci_mailer = m;
13249c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
13259c9e68d9Seric 		mci->mci_pid = pid;
13269c9e68d9Seric 		(void) close(mpvect[0]);
13279c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1328335eae58Seric 		if (mci->mci_out == NULL)
1329335eae58Seric 		{
1330335eae58Seric 			syserr("deliver: cannot create mailer output channel, fd=%d",
1331335eae58Seric 				mpvect[1]);
1332335eae58Seric 			(void) close(mpvect[1]);
1333335eae58Seric 			if (clever)
1334335eae58Seric 			{
1335335eae58Seric 				(void) close(rpvect[0]);
1336335eae58Seric 				(void) close(rpvect[1]);
1337335eae58Seric 			}
1338335eae58Seric 			rcode = EX_OSERR;
1339335eae58Seric 			goto give_up;
1340335eae58Seric 		}
13419c9e68d9Seric 		if (clever)
13429c9e68d9Seric 		{
13439c9e68d9Seric 			(void) close(rpvect[1]);
13449c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
1345335eae58Seric 			if (mci->mci_in == NULL)
1346335eae58Seric 			{
1347335eae58Seric 				syserr("deliver: cannot create mailer input channel, fd=%d",
1348335eae58Seric 					mpvect[1]);
1349335eae58Seric 				(void) close(rpvect[0]);
1350335eae58Seric 				fclose(mci->mci_out);
1351335eae58Seric 				mci->mci_out = NULL;
1352335eae58Seric 				rcode = EX_OSERR;
1353335eae58Seric 				goto give_up;
1354335eae58Seric 			}
13559c9e68d9Seric 		}
13569c9e68d9Seric 		else
13579c9e68d9Seric 		{
13589c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
13599c9e68d9Seric 			mci->mci_in = NULL;
13609c9e68d9Seric 		}
13619c9e68d9Seric 	}
13629c9e68d9Seric 
1363cc7496f5Seric 	if (bitset(EF_HAS8BIT, e->e_flags) && bitnset(M_7BITS, m->m_flags))
1364cc7496f5Seric 		mci->mci_flags |= MCIF_CVT8TO7;
1365cc7496f5Seric 
13669c9e68d9Seric 	/*
13679c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
13689c9e68d9Seric 	*/
13699c9e68d9Seric 
13709c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
13719c9e68d9Seric 	{
13729c9e68d9Seric 		smtpinit(m, mci, e);
13739c9e68d9Seric 	}
13749c9e68d9Seric 	if (tTd(11, 1))
13759c9e68d9Seric 	{
13769c9e68d9Seric 		printf("openmailer: ");
13774052916eSeric 		mci_dump(mci, FALSE);
13789c9e68d9Seric 	}
13799c9e68d9Seric 
13809c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1381b31e7f2bSeric 	{
1382b31e7f2bSeric 		/* couldn't open the mailer */
1383b31e7f2bSeric 		rcode = mci->mci_exitstat;
13842a6bc25bSeric 		errno = mci->mci_errno;
13859d4a8008Seric #if NAMED_BIND
1386f170942cSeric 		h_errno = mci->mci_herrno;
1387f170942cSeric #endif
1388b31e7f2bSeric 		if (rcode == EX_OK)
1389b31e7f2bSeric 		{
1390b31e7f2bSeric 			/* shouldn't happen */
139108b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
13926b0f339dSeric 				rcode, mci->mci_state, firstsig);
1393b31e7f2bSeric 			rcode = EX_SOFTWARE;
1394b31e7f2bSeric 		}
1395cb082c5bSeric 		else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0')
139690891494Seric 		{
139790891494Seric 			/* try next MX site */
139890891494Seric 			goto tryhost;
139990891494Seric 		}
1400b31e7f2bSeric 	}
1401b31e7f2bSeric 	else if (!clever)
1402b31e7f2bSeric 	{
1403b31e7f2bSeric 		/*
1404b31e7f2bSeric 		**  Format and send message.
1405b31e7f2bSeric 		*/
140615d084d5Seric 
14075aa0f353Seric 		putfromline(mci, e);
1408a6ce3008Seric 		(*e->e_puthdr)(mci, e->e_header, e);
14095aa0f353Seric 		(*e->e_putbody)(mci, e, NULL);
1410b31e7f2bSeric 
1411b31e7f2bSeric 		/* get the exit status */
1412c9be6216Seric 		rcode = endmailer(mci, e, pv);
1413134746fbSeric 	}
1414134746fbSeric 	else
1415b31e7f2bSeric #ifdef SMTP
1416134746fbSeric 	{
1417b31e7f2bSeric 		/*
1418b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1419b31e7f2bSeric 		*/
142015d084d5Seric 
1421b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1422b31e7f2bSeric 		if (rcode == EX_OK)
142375889e88Seric 		{
1424ded0d3daSkarels 			register char *t = tobuf;
1425ded0d3daSkarels 			register int i;
1426ded0d3daSkarels 
1427588cad61Seric 			/* send the recipient list */
142863780dbdSeric 			tobuf[0] = '\0';
142975889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
143075889e88Seric 			{
143163780dbdSeric 				e->e_to = to->q_paddr;
143215d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
143375889e88Seric 				{
143483b7ddc9Seric 					markfailure(e, to, i);
14354dee0003Seric 					giveresponse(i, m, mci, ctladdr, e);
143663780dbdSeric 				}
143775889e88Seric 				else
143875889e88Seric 				{
1439911693bfSbostic 					*t++ = ',';
1440b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1441b31e7f2bSeric 						continue;
1442ee39df61Seric 					*t = '\0';
1443588cad61Seric 				}
1444588cad61Seric 			}
1445588cad61Seric 
144663780dbdSeric 			/* now send the data */
144763780dbdSeric 			if (tobuf[0] == '\0')
1448b31e7f2bSeric 			{
14499c9e68d9Seric 				rcode = EX_OK;
145063780dbdSeric 				e->e_to = NULL;
1451b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1452b31e7f2bSeric 					smtprset(m, mci, e);
1453b31e7f2bSeric 			}
145475889e88Seric 			else
145575889e88Seric 			{
145663780dbdSeric 				e->e_to = tobuf + 1;
145775889e88Seric 				rcode = smtpdata(m, mci, e);
145863780dbdSeric 			}
145963780dbdSeric 
146063780dbdSeric 			/* now close the connection */
1461b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
146215d084d5Seric 				smtpquit(m, mci, e);
146363780dbdSeric 		}
1464cb082c5bSeric 		if (rcode != EX_OK && curhost != NULL && *curhost != '\0')
14659c9e68d9Seric 		{
14669c9e68d9Seric 			/* try next MX site */
14679c9e68d9Seric 			goto tryhost;
14689c9e68d9Seric 		}
1469c579ef51Seric 	}
1470b31e7f2bSeric #else /* not SMTP */
1471a05b3449Sbostic 	{
147208b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1473845e533cSeric 		rcode = EX_CONFIG;
1474b31e7f2bSeric 		goto give_up;
1475a05b3449Sbostic 	}
1476b31e7f2bSeric #endif /* SMTP */
14779d4a8008Seric #if NAMED_BIND
14782bcc6d2dSeric 	if (ConfigLevel < 2)
1479912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1480134746fbSeric #endif
14815dfc646bSeric 
1482b31e7f2bSeric 	/* arrange a return receipt if requested */
1483df106f0bSeric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1484df106f0bSeric 	    bitnset(M_LOCALMAILER, m->m_flags))
1485b31e7f2bSeric 	{
1486b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1487b31e7f2bSeric 		/* do we want to send back more info? */
1488b31e7f2bSeric 	}
1489b31e7f2bSeric 
1490c77d1c25Seric 	/*
149163780dbdSeric 	**  Do final status disposal.
149263780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1493c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1494c77d1c25Seric 	**		addressees.
1495c77d1c25Seric 	*/
1496c77d1c25Seric 
1497b31e7f2bSeric   give_up:
149863780dbdSeric 	if (tobuf[0] != '\0')
14994dee0003Seric 		giveresponse(rcode, m, mci, ctladdr, e);
1500772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1501b31e7f2bSeric 	{
1502dde5acadSeric 		if (rcode != EX_OK)
150383b7ddc9Seric 			markfailure(e, to, rcode);
1504dde5acadSeric 		else
1505655518ecSeric 		{
1506dde5acadSeric 			to->q_flags |= QSENT;
1507655518ecSeric 			e->e_nsent++;
1508df106f0bSeric 			if (e->e_receiptto != NULL &&
1509df106f0bSeric 			    bitnset(M_LOCALMAILER, m->m_flags))
1510df106f0bSeric 			{
1511df106f0bSeric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1512df106f0bSeric 					to->q_paddr);
1513df106f0bSeric 			}
1514655518ecSeric 		}
1515b31e7f2bSeric 	}
1516b31e7f2bSeric 
1517b31e7f2bSeric 	/*
1518b31e7f2bSeric 	**  Restore state and return.
1519b31e7f2bSeric 	*/
1520c77d1c25Seric 
15217febfd66Seric #ifdef XDEBUG
15227febfd66Seric 	{
15237febfd66Seric 		char wbuf[MAXLINE];
15247febfd66Seric 
15257febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
152644622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
152744622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
152844622ea3Seric 			m->m_name);
15297febfd66Seric 		checkfd012(wbuf);
15307febfd66Seric 	}
15317febfd66Seric #endif
15327febfd66Seric 
153335490626Seric 	errno = 0;
1534588cad61Seric 	define('g', (char *) NULL, e);
15355826d9d3Seric 	return (rcode);
153625a99e2eSeric }
15375dfc646bSeric /*
153883b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
153983b7ddc9Seric **
154083b7ddc9Seric **	Parameters:
154183b7ddc9Seric **		e -- the envelope we are sending.
154283b7ddc9Seric **		q -- the address to mark.
154383b7ddc9Seric **		rcode -- the code signifying the particular failure.
154483b7ddc9Seric **
154583b7ddc9Seric **	Returns:
154683b7ddc9Seric **		none.
154783b7ddc9Seric **
154883b7ddc9Seric **	Side Effects:
154983b7ddc9Seric **		marks the address (and possibly the envelope) with the
155083b7ddc9Seric **			failure so that an error will be returned or
155183b7ddc9Seric **			the message will be queued, as appropriate.
155283b7ddc9Seric */
155383b7ddc9Seric 
155483b7ddc9Seric markfailure(e, q, rcode)
155583b7ddc9Seric 	register ENVELOPE *e;
155683b7ddc9Seric 	register ADDRESS *q;
155783b7ddc9Seric 	int rcode;
155883b7ddc9Seric {
155919c47125Seric 	char buf[MAXLINE];
156019c47125Seric 
1561d5aafa3cSeric 	switch (rcode)
1562d5aafa3cSeric 	{
1563d5aafa3cSeric 	  case EX_OK:
1564d5aafa3cSeric 		break;
1565d5aafa3cSeric 
1566d5aafa3cSeric 	  case EX_TEMPFAIL:
1567d5aafa3cSeric 	  case EX_IOERR:
1568d5aafa3cSeric 	  case EX_OSERR:
156983b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1570d5aafa3cSeric 		break;
1571d5aafa3cSeric 
1572d5aafa3cSeric 	  default:
1573f170942cSeric 		q->q_flags |= QBADADDR;
1574d5aafa3cSeric 		break;
1575d5aafa3cSeric 	}
157683b7ddc9Seric }
157783b7ddc9Seric /*
1578c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1579c579ef51Seric **
1580c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1581c579ef51Seric **	violation), so we report those specially.  For other
1582c579ef51Seric **	errors, we choose a status message (into statmsg),
1583c579ef51Seric **	and if it represents an error, we print it.
1584c579ef51Seric **
1585c579ef51Seric **	Parameters:
1586c579ef51Seric **		pid -- pid of mailer.
1587c9be6216Seric **		e -- the current envelope.
1588c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1589c9be6216Seric **			(for error messages).
1590c579ef51Seric **
1591c579ef51Seric **	Returns:
1592c579ef51Seric **		exit code of mailer.
1593c579ef51Seric **
1594c579ef51Seric **	Side Effects:
1595c579ef51Seric **		none.
1596c579ef51Seric */
1597c579ef51Seric 
1598c9be6216Seric endmailer(mci, e, pv)
1599b31e7f2bSeric 	register MCI *mci;
1600c9be6216Seric 	register ENVELOPE *e;
1601c9be6216Seric 	char **pv;
1602c579ef51Seric {
1603588cad61Seric 	int st;
1604c579ef51Seric 
160575889e88Seric 	/* close any connections */
160675889e88Seric 	if (mci->mci_in != NULL)
160776f3d53fSeric 		(void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in");
160875889e88Seric 	if (mci->mci_out != NULL)
160976f3d53fSeric 		(void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out");
161075889e88Seric 	mci->mci_in = mci->mci_out = NULL;
161175889e88Seric 	mci->mci_state = MCIS_CLOSED;
161275889e88Seric 
161333db8731Seric 	/* in the IPC case there is nothing to wait for */
161475889e88Seric 	if (mci->mci_pid == 0)
161533db8731Seric 		return (EX_OK);
161633db8731Seric 
161733db8731Seric 	/* wait for the mailer process to die and collect status */
161875889e88Seric 	st = waitfor(mci->mci_pid);
1619588cad61Seric 	if (st == -1)
162078de67c1Seric 	{
1621c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1622588cad61Seric 		return (EX_SOFTWARE);
1623c579ef51Seric 	}
162433db8731Seric 
1625bf9bc890Seric 	if (WIFEXITED(st))
1626c579ef51Seric 	{
1627bf9bc890Seric 		/* normal death -- return status */
1628bf9bc890Seric 		return (WEXITSTATUS(st));
1629bf9bc890Seric 	}
1630bf9bc890Seric 
1631bf9bc890Seric 	/* it died a horrid death */
1632550092c3Seric 	syserr("451 mailer %s died with signal %o",
1633550092c3Seric 		mci->mci_mailer->m_name, st);
1634c9be6216Seric 
1635c9be6216Seric 	/* log the arguments */
16363c930db3Seric 	if (pv != NULL && e->e_xfp != NULL)
1637c9be6216Seric 	{
1638c9be6216Seric 		register char **av;
1639c9be6216Seric 
1640c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1641c9be6216Seric 		for (av = pv; *av != NULL; av++)
1642c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1643c9be6216Seric 		fprintf(e->e_xfp, "\n");
1644c9be6216Seric 	}
1645c9be6216Seric 
16465f73204aSeric 	ExitStat = EX_TEMPFAIL;
16475f73204aSeric 	return (EX_TEMPFAIL);
1648c579ef51Seric }
1649c579ef51Seric /*
165025a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
165125a99e2eSeric **
165225a99e2eSeric **	Parameters:
165325a99e2eSeric **		stat -- the status code from the mailer (high byte
165425a99e2eSeric **			only; core dumps must have been taken care of
165525a99e2eSeric **			already).
165681161401Seric **		m -- the mailer info for this mailer.
165781161401Seric **		mci -- the mailer connection info -- can be NULL if the
165881161401Seric **			response is given before the connection is made.
16594dee0003Seric **		ctladdr -- the controlling address for the recipient
16604dee0003Seric **			address(es).
166181161401Seric **		e -- the current envelope.
166225a99e2eSeric **
166325a99e2eSeric **	Returns:
1664db8841e9Seric **		none.
166525a99e2eSeric **
166625a99e2eSeric **	Side Effects:
1667c1f9df2cSeric **		Errors may be incremented.
166825a99e2eSeric **		ExitStat may be set.
166925a99e2eSeric */
167025a99e2eSeric 
16714dee0003Seric giveresponse(stat, m, mci, ctladdr, e)
167225a99e2eSeric 	int stat;
1673588cad61Seric 	register MAILER *m;
167481161401Seric 	register MCI *mci;
16754dee0003Seric 	ADDRESS *ctladdr;
1676198d9be0Seric 	ENVELOPE *e;
167725a99e2eSeric {
16789c16475dSeric 	register const char *statmsg;
167925a99e2eSeric 	extern char *SysExMsg[];
168025a99e2eSeric 	register int i;
1681d4bd8f0eSbostic 	extern int N_SysEx;
1682198d9be0Seric 	char buf[MAXLINE];
168325a99e2eSeric 
168413bbc08cSeric 	/*
168513bbc08cSeric 	**  Compute status message from code.
168613bbc08cSeric 	*/
168713bbc08cSeric 
168825a99e2eSeric 	i = stat - EX__BASE;
1689588cad61Seric 	if (stat == 0)
16906fe8c3bcSeric 	{
1691588cad61Seric 		statmsg = "250 Sent";
1692ce5531bdSeric 		if (e->e_statmsg != NULL)
16936fe8c3bcSeric 		{
1694ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
16956fe8c3bcSeric 			statmsg = buf;
16966fe8c3bcSeric 		}
16976fe8c3bcSeric 	}
1698588cad61Seric 	else if (i < 0 || i > N_SysEx)
1699588cad61Seric 	{
1700588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1701588cad61Seric 		stat = EX_UNAVAILABLE;
1702588cad61Seric 		statmsg = buf;
1703588cad61Seric 	}
1704198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1705198d9be0Seric 	{
17067d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
17079d4a8008Seric #if NAMED_BIND
1708f28da541Smiriam 		if (h_errno == TRY_AGAIN)
17094d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1710f28da541Smiriam 		else
1711d4bd8f0eSbostic #endif
1712f28da541Smiriam 		{
17138557d168Seric 			if (errno != 0)
1714d87e85f3Seric 				statmsg = errstring(errno);
1715d87e85f3Seric 			else
1716d87e85f3Seric 			{
1717d87e85f3Seric #ifdef SMTP
1718d87e85f3Seric 				statmsg = SmtpError;
17196c2c3107Seric #else /* SMTP */
1720d87e85f3Seric 				statmsg = NULL;
17216c2c3107Seric #endif /* SMTP */
1722d87e85f3Seric 			}
1723f28da541Smiriam 		}
1724d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1725d87e85f3Seric 		{
172687c9b3e7Seric 			(void) strcat(buf, ": ");
1727d87e85f3Seric 			(void) strcat(buf, statmsg);
17288557d168Seric 		}
1729198d9be0Seric 		statmsg = buf;
1730198d9be0Seric 	}
17319d4a8008Seric #if NAMED_BIND
1732f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1733f170942cSeric 	{
17344d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1735862934b2Seric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i] + 1, statmsg);
1736f170942cSeric 		statmsg = buf;
1737f170942cSeric 	}
1738f170942cSeric #endif
173925a99e2eSeric 	else
1740d87e85f3Seric 	{
174125a99e2eSeric 		statmsg = SysExMsg[i];
17427d55540cSeric 		if (*statmsg++ == ':')
17437d55540cSeric 		{
17447d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
17457d55540cSeric 			statmsg = buf;
17467d55540cSeric 		}
1747d87e85f3Seric 	}
1748588cad61Seric 
1749588cad61Seric 	/*
1750588cad61Seric 	**  Print the message as appropriate
1751588cad61Seric 	*/
1752588cad61Seric 
1753198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1754df106f0bSeric 	{
1755df106f0bSeric 		extern char MsgBuf[];
1756df106f0bSeric 
17571f96bd0bSeric 		message("%s", &statmsg[4]);
1758df106f0bSeric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1759df106f0bSeric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1760df106f0bSeric 	}
176125a99e2eSeric 	else
176225a99e2eSeric 	{
1763862934b2Seric 		char mbuf[8];
1764862934b2Seric 
1765c1f9df2cSeric 		Errors++;
1766862934b2Seric 		sprintf(mbuf, "%.3s %%s", statmsg);
1767862934b2Seric 		usrerr(mbuf, &statmsg[4]);
176825a99e2eSeric 	}
176925a99e2eSeric 
177025a99e2eSeric 	/*
177125a99e2eSeric 	**  Final cleanup.
177225a99e2eSeric 	**	Log a record of the transaction.  Compute the new
177325a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
177425a99e2eSeric 	**	that.
177525a99e2eSeric 	*/
177625a99e2eSeric 
17772f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
17784dee0003Seric 		logdelivery(m, mci, &statmsg[4], ctladdr, e);
1779eb238f8cSeric 
17805d3f0ed4Seric 	if (tTd(11, 2))
17815d3f0ed4Seric 		printf("giveresponse: stat=%d, e->e_message=%s\n",
17825d3f0ed4Seric 			stat, e->e_message);
17835d3f0ed4Seric 
1784eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1785eb238f8cSeric 		setstat(stat);
17861097d4c9Seric 	if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL))
1787198d9be0Seric 	{
1788198d9be0Seric 		if (e->e_message != NULL)
1789198d9be0Seric 			free(e->e_message);
1790198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1791198d9be0Seric 	}
17928557d168Seric 	errno = 0;
17939d4a8008Seric #if NAMED_BIND
1794f28da541Smiriam 	h_errno = 0;
1795d4bd8f0eSbostic #endif
1796eb238f8cSeric }
1797eb238f8cSeric /*
1798eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1799eb238f8cSeric **
18002c9b4f99Seric **	Care is taken to avoid logging lines that are too long, because
18012c9b4f99Seric **	some versions of syslog have an unfortunate proclivity for core
18022c9b4f99Seric **	dumping.  This is a hack, to be sure, that is at best empirical.
18032c9b4f99Seric **
1804eb238f8cSeric **	Parameters:
180581161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
180681161401Seric **		mci -- the mailer connection info -- can be NULL if the
180781161401Seric **			log is occuring when no connection is active.
180881161401Seric **		stat -- the message to print for the status.
18094dee0003Seric **		ctladdr -- the controlling address for the to list.
181081161401Seric **		e -- the current envelope.
1811eb238f8cSeric **
1812eb238f8cSeric **	Returns:
1813eb238f8cSeric **		none
1814eb238f8cSeric **
1815eb238f8cSeric **	Side Effects:
1816eb238f8cSeric **		none
1817eb238f8cSeric */
1818eb238f8cSeric 
18194dee0003Seric logdelivery(m, mci, stat, ctladdr, e)
182081161401Seric 	MAILER *m;
182181161401Seric 	register MCI *mci;
1822eb238f8cSeric 	char *stat;
18234dee0003Seric 	ADDRESS *ctladdr;
1824b31e7f2bSeric 	register ENVELOPE *e;
18255cf56be3Seric {
1826eb238f8cSeric # ifdef LOG
18274dee0003Seric 	register char *bp;
18282c9b4f99Seric 	register char *p;
18292c9b4f99Seric 	int l;
1830d6acf3eeSeric 	char buf[512];
18319507d1f9Seric 
18323a100e8bSeric #  if (SYSLOG_BUFSIZE) >= 256
18334dee0003Seric 	bp = buf;
18344dee0003Seric 	if (ctladdr != NULL)
18354dee0003Seric 	{
18364dee0003Seric 		strcpy(bp, ", ctladdr=");
1837f62f08c7Seric 		strcat(bp, shortenstring(ctladdr->q_paddr, 83));
18384dee0003Seric 		bp += strlen(bp);
18394dee0003Seric 		if (bitset(QGOODUID, ctladdr->q_flags))
18404dee0003Seric 		{
18414dee0003Seric 			(void) sprintf(bp, " (%d/%d)",
18424dee0003Seric 					ctladdr->q_uid, ctladdr->q_gid);
18434dee0003Seric 			bp += strlen(bp);
18444dee0003Seric 		}
18454dee0003Seric 	}
18464dee0003Seric 
18474dee0003Seric 	(void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
18484dee0003Seric 	bp += strlen(bp);
184981161401Seric 
185048ed5d33Seric 	if (m != NULL)
185171ff6caaSeric 	{
18524dee0003Seric 		(void) strcpy(bp, ", mailer=");
18534dee0003Seric 		(void) strcat(bp, m->m_name);
18544dee0003Seric 		bp += strlen(bp);
185571ff6caaSeric 	}
185648ed5d33Seric 
185748ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
185871ff6caaSeric 	{
185971ff6caaSeric # ifdef DAEMON
1860e2f2f828Seric 		extern SOCKADDR CurHostAddr;
186148ed5d33Seric # endif
186271ff6caaSeric 
18634dee0003Seric 		(void) strcpy(bp, ", relay=");
18644dee0003Seric 		(void) strcat(bp, mci->mci_host);
186548ed5d33Seric 
186648ed5d33Seric # ifdef DAEMON
18672bdfc6b9Seric 		(void) strcat(bp, " [");
18684dee0003Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
18692bdfc6b9Seric 		(void) strcat(bp, "]");
187071ff6caaSeric # endif
187171ff6caaSeric 	}
1872bcb9b028Seric 	else if (strcmp(stat, "queued") != 0)
187348ed5d33Seric 	{
187448ed5d33Seric 		char *p = macvalue('h', e);
18759507d1f9Seric 
187648ed5d33Seric 		if (p != NULL && p[0] != '\0')
187748ed5d33Seric 		{
18784dee0003Seric 			(void) strcpy(bp, ", relay=");
18794dee0003Seric 			(void) strcat(bp, p);
188048ed5d33Seric 		}
188148ed5d33Seric 	}
1882cb082c5bSeric 	bp += strlen(bp);
1883d6acf3eeSeric 
18843a100e8bSeric #define STATLEN		(((SYSLOG_BUFSIZE) - 100) / 4)
18853a100e8bSeric #if (STATLEN) < 63
18863a100e8bSeric # undef STATLEN
18873a100e8bSeric # define STATLEN	63
18883a100e8bSeric #endif
18893a100e8bSeric #if (STATLEN) > 203
18903a100e8bSeric # undef STATLEN
18913a100e8bSeric # define STATLEN	203
18923a100e8bSeric #endif
18933a100e8bSeric 
18943a100e8bSeric 	if ((bp - buf) > (sizeof buf - ((STATLEN) + 20)))
18952c9b4f99Seric 	{
18962c9b4f99Seric 		/* desperation move -- truncate data */
18973a100e8bSeric 		bp = buf + sizeof buf - ((STATLEN) + 17);
18982c9b4f99Seric 		strcpy(bp, "...");
18992c9b4f99Seric 		bp += 3;
19002c9b4f99Seric 	}
19012c9b4f99Seric 
19022c9b4f99Seric 	(void) strcpy(bp, ", stat=");
19032c9b4f99Seric 	bp += strlen(bp);
19043a100e8bSeric 
19053a100e8bSeric 	(void) strcpy(bp, shortenstring(stat, (STATLEN)));
19062c9b4f99Seric 
19072c9b4f99Seric 	l = SYSLOG_BUFSIZE - 100 - strlen(buf);
19082c9b4f99Seric 	p = e->e_to;
19092c9b4f99Seric 	while (strlen(p) >= l)
19102c9b4f99Seric 	{
19112c9b4f99Seric 		register char *q = strchr(p + l, ',');
19122c9b4f99Seric 
19132a1b6b73Seric 		if (q == NULL)
19142c9b4f99Seric 			break;
19152c9b4f99Seric 		syslog(LOG_INFO, "%s: to=%.*s [more]%s",
19162c9b4f99Seric 			e->e_id, ++q - p, p, buf);
19172c9b4f99Seric 		p = q;
19182c9b4f99Seric 	}
19192c9b4f99Seric 	syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf);
19203a100e8bSeric 
19213a100e8bSeric #  else		/* we have a very short log buffer size */
19223a100e8bSeric 
192327607809Seric 	l = SYSLOG_BUFSIZE - 85;
19243a100e8bSeric 	p = e->e_to;
19253a100e8bSeric 	while (strlen(p) >= l)
19263a100e8bSeric 	{
19273a100e8bSeric 		register char *q = strchr(p + l, ',');
19283a100e8bSeric 
19293a100e8bSeric 		if (q == NULL)
19303a100e8bSeric 			break;
19313a100e8bSeric 		syslog(LOG_INFO, "%s: to=%.*s [more]",
19323a100e8bSeric 			e->e_id, ++q - p, p);
19333a100e8bSeric 		p = q;
19343a100e8bSeric 	}
19353a100e8bSeric 	syslog(LOG_INFO, "%s: to=%s", e->e_id, p);
19363a100e8bSeric 
19373a100e8bSeric 	if (ctladdr != NULL)
19383a100e8bSeric 	{
19393a100e8bSeric 		bp = buf;
19403a100e8bSeric 		strcpy(buf, "ctladdr=");
19413a100e8bSeric 		bp += strlen(buf);
19423a100e8bSeric 		strcpy(bp, shortenstring(ctladdr->q_paddr, 83));
19433a100e8bSeric 		bp += strlen(buf);
19443a100e8bSeric 		if (bitset(QGOODUID, ctladdr->q_flags))
19453a100e8bSeric 		{
19463a100e8bSeric 			(void) sprintf(bp, " (%d/%d)",
19473a100e8bSeric 					ctladdr->q_uid, ctladdr->q_gid);
19483a100e8bSeric 			bp += strlen(bp);
19493a100e8bSeric 		}
19503a100e8bSeric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
19513a100e8bSeric 	}
19524e797715Seric 	bp = buf;
19534e797715Seric 	sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
19544e797715Seric 	bp += strlen(bp);
19553a100e8bSeric 
19563a100e8bSeric 	if (m != NULL)
19574e797715Seric 	{
19584e797715Seric 		sprintf(bp, ", mailer=%s", m->m_name);
19594e797715Seric 		bp += strlen(bp);
19604e797715Seric 	}
1961b056abd0Seric 	syslog(LOG_INFO, "%s: %s", e->e_id, buf);
19623a100e8bSeric 
1963b056abd0Seric 	buf[0] = '\0';
19643a100e8bSeric 	if (mci != NULL && mci->mci_host != NULL)
19653a100e8bSeric 	{
19663a100e8bSeric # ifdef DAEMON
19673a100e8bSeric 		extern SOCKADDR CurHostAddr;
19683a100e8bSeric # endif
19693a100e8bSeric 
1970d2ece200Seric 		sprintf(buf, "relay=%s", mci->mci_host);
19713a100e8bSeric 
19723a100e8bSeric # ifdef DAEMON
1973b056abd0Seric 		(void) strcat(buf, " [");
1974b056abd0Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
1975b056abd0Seric 		(void) strcat(buf, "]");
19763a100e8bSeric # endif
19773a100e8bSeric 	}
1978bcb9b028Seric 	else if (strcmp(stat, "queued") != 0)
19793a100e8bSeric 	{
19803a100e8bSeric 		char *p = macvalue('h', e);
19813a100e8bSeric 
19823a100e8bSeric 		if (p != NULL && p[0] != '\0')
1983d2ece200Seric 			sprintf(buf, "relay=%s", p);
19843a100e8bSeric 	}
1985b056abd0Seric 	if (buf[0] != '\0')
19864e797715Seric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
19873a100e8bSeric 
19883a100e8bSeric 	syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63));
19893a100e8bSeric #  endif /* short log buffer */
19906c2c3107Seric # endif /* LOG */
199125a99e2eSeric }
199225a99e2eSeric /*
199351552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
199425a99e2eSeric **
199551552439Seric **	This can be made an arbitrary message separator by changing $l
199651552439Seric **
19979b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
19989b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
19999b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
20009b6c17a6Seric **	this kind of antique garbage????
200125a99e2eSeric **
200225a99e2eSeric **	Parameters:
20035aa0f353Seric **		mci -- the connection information.
20045aa0f353Seric **		e -- the envelope.
200525a99e2eSeric **
200625a99e2eSeric **	Returns:
200751552439Seric **		none
200825a99e2eSeric **
200925a99e2eSeric **	Side Effects:
201051552439Seric **		outputs some text to fp.
201125a99e2eSeric */
201225a99e2eSeric 
20135aa0f353Seric putfromline(mci, e)
20145aa0f353Seric 	register MCI *mci;
2015b31e7f2bSeric 	ENVELOPE *e;
201625a99e2eSeric {
20172bc47524Seric 	char *template = "\201l\n";
201851552439Seric 	char buf[MAXLINE];
201925a99e2eSeric 
20205aa0f353Seric 	if (bitnset(M_NHDR, mci->mci_mailer->m_flags))
202151552439Seric 		return;
202213bbc08cSeric 
20232c7e1b8dSeric # ifdef UGLYUUCP
20245aa0f353Seric 	if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags))
202574b6e67bSeric 	{
2026ea09d6edSeric 		char *bang;
2027ea09d6edSeric 		char xbuf[MAXLINE];
202874b6e67bSeric 
2029ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
20306c2c3107Seric 		bang = strchr(buf, '!');
203174b6e67bSeric 		if (bang == NULL)
203234fcca25Seric 		{
203334fcca25Seric 			errno = 0;
203434fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
203534fcca25Seric 		}
203674b6e67bSeric 		else
2037588cad61Seric 		{
2038ea09d6edSeric 			*bang++ = '\0';
20392bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
2040ea09d6edSeric 			template = xbuf;
204174b6e67bSeric 		}
2042588cad61Seric 	}
20436c2c3107Seric # endif /* UGLYUUCP */
2044b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
20455aa0f353Seric 	putline(buf, mci);
2046bc6e2962Seric }
2047bc6e2962Seric /*
204851552439Seric **  PUTBODY -- put the body of a message.
204951552439Seric **
205051552439Seric **	Parameters:
20515aa0f353Seric **		mci -- the connection information.
20529a6a5f55Seric **		e -- the envelope to put out.
205303c02fdeSeric **		separator -- if non-NULL, a message separator that must
205403c02fdeSeric **			not be permitted in the resulting message.
205551552439Seric **
205651552439Seric **	Returns:
205751552439Seric **		none.
205851552439Seric **
205951552439Seric **	Side Effects:
206051552439Seric **		The message is written onto fp.
206151552439Seric */
206251552439Seric 
20638d0f8cdfSeric /* values for output state variable */
20648d0f8cdfSeric #define OS_HEAD		0	/* at beginning of line */
20658d0f8cdfSeric #define OS_CR		1	/* read a carriage return */
20668d0f8cdfSeric #define OS_INLINE	2	/* putting rest of line */
20678d0f8cdfSeric 
20685aa0f353Seric putbody(mci, e, separator)
20695aa0f353Seric 	register MCI *mci;
20709a6a5f55Seric 	register ENVELOPE *e;
207103c02fdeSeric 	char *separator;
207251552439Seric {
207377b52738Seric 	char buf[MAXLINE];
207451552439Seric 
207551552439Seric 	/*
207651552439Seric 	**  Output the body of the message
207751552439Seric 	*/
207851552439Seric 
20799a6a5f55Seric 	if (e->e_dfp == NULL)
208051552439Seric 	{
20819a6a5f55Seric 		if (e->e_df != NULL)
20829a6a5f55Seric 		{
20839a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
20849a6a5f55Seric 			if (e->e_dfp == NULL)
20858f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
2086e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
20879a6a5f55Seric 		}
20889a6a5f55Seric 		else
2089a6ce3008Seric 		{
2090a6ce3008Seric 			if (bitset(MCIF_INHEADER, mci->mci_flags))
2091a6ce3008Seric 			{
2092a6ce3008Seric 				putline("", mci);
2093a6ce3008Seric 				mci->mci_flags &= ~MCIF_INHEADER;
2094a6ce3008Seric 			}
20955aa0f353Seric 			putline("<<< No Message Collected >>>", mci);
20968d0f8cdfSeric 			goto endofmessage;
20979a6a5f55Seric 		}
2098a6ce3008Seric 	}
2099*519e7d80Seric 	if (e->e_dfp != NULL && e->e_dfino == (ino_t) 0)
2100*519e7d80Seric 	{
2101*519e7d80Seric 		struct stat stbuf;
2102*519e7d80Seric 
2103*519e7d80Seric 		if (fstat(fileno(e->e_dfp), &stbuf) < 0)
2104*519e7d80Seric 			e->e_dfino = -1;
2105*519e7d80Seric 		else
2106*519e7d80Seric 			e->e_dfino = stbuf.st_ino;
2107*519e7d80Seric 	}
21089a6a5f55Seric 	rewind(e->e_dfp);
2109a6ce3008Seric 
2110a6ce3008Seric 	if (bitset(MCIF_CVT8TO7, mci->mci_flags))
2111a6ce3008Seric 	{
2112a6ce3008Seric 		/* do 8 to 7 bit MIME conversion */
2113a6ce3008Seric 		if (hvalue("MIME-Version", e->e_header) == NULL)
2114a6ce3008Seric 			putline("MIME-Version: 1.0", mci);
2115a6ce3008Seric 		mime8to7(mci, e->e_header, e, NULL);
2116a6ce3008Seric 	}
2117a6ce3008Seric 	else
2118a6ce3008Seric 	{
21198d0f8cdfSeric 		int ostate;
21208d0f8cdfSeric 		register char *bp;
21218d0f8cdfSeric 		register char *pbp;
21228d0f8cdfSeric 		register int c;
21238d0f8cdfSeric 		int padc;
21248d0f8cdfSeric 		char *buflim;
21258d0f8cdfSeric 		int pos;
21268d0f8cdfSeric 		char peekbuf[10];
21278d0f8cdfSeric 
2128a6ce3008Seric 		/* we can pass it through unmodified */
2129a6ce3008Seric 		if (bitset(MCIF_INHEADER, mci->mci_flags))
2130a6ce3008Seric 		{
2131a6ce3008Seric 			putline("", mci);
2132a6ce3008Seric 			mci->mci_flags &= ~MCIF_INHEADER;
2133a6ce3008Seric 		}
21348d0f8cdfSeric 
21358d0f8cdfSeric 		/* determine end of buffer; allow for short mailer lines */
21368d0f8cdfSeric 		buflim = &buf[sizeof buf - 1];
21378d0f8cdfSeric 		if (mci->mci_mailer->m_linelimit < sizeof buf - 1)
21388d0f8cdfSeric 			buflim = &buf[mci->mci_mailer->m_linelimit - 1];
21398d0f8cdfSeric 
21408d0f8cdfSeric 		/* copy temp file to output with mapping */
21418d0f8cdfSeric 		ostate = OS_HEAD;
21428d0f8cdfSeric 		bp = buf;
21438d0f8cdfSeric 		pbp = peekbuf;
21448d0f8cdfSeric 		while (!ferror(mci->mci_out))
214524fc8aeeSeric 		{
21468d0f8cdfSeric 			register char *xp;
21478d0f8cdfSeric 
21488d0f8cdfSeric 			if (pbp > peekbuf)
21498d0f8cdfSeric 				c = *--pbp;
21508d0f8cdfSeric 			else if ((c = fgetc(e->e_dfp)) == EOF)
21518d0f8cdfSeric 				break;
21528d0f8cdfSeric 			if (bitset(MCIF_7BIT, mci->mci_flags))
21538d0f8cdfSeric 				c &= 0x7f;
21548d0f8cdfSeric 			switch (ostate)
21558d0f8cdfSeric 			{
21568d0f8cdfSeric 			  case OS_HEAD:
21578d0f8cdfSeric 				if (c != '\r' && c != '\n' && bp < buflim)
21588d0f8cdfSeric 				{
21598d0f8cdfSeric 					*bp++ = c;
21608d0f8cdfSeric 					break;
21618d0f8cdfSeric 				}
21628d0f8cdfSeric 
21638d0f8cdfSeric 				/* check beginning of line for special cases */
21648d0f8cdfSeric 				*bp = '\0';
21658d0f8cdfSeric 				pos = 0;
21668d0f8cdfSeric 				padc = EOF;
21675aa0f353Seric 				if (buf[0] == 'F' &&
21685aa0f353Seric 				    bitnset(M_ESCFROM, mci->mci_mailer->m_flags) &&
2169d6fa2b58Sbostic 				    strncmp(buf, "From ", 5) == 0)
21708d0f8cdfSeric 				{
21718d0f8cdfSeric 					padc = '>';
21728d0f8cdfSeric 				}
2173a6ce3008Seric 				if (buf[0] == '-' && buf[1] == '-' &&
2174a6ce3008Seric 				    separator != NULL)
217503c02fdeSeric 				{
217603c02fdeSeric 					/* possible separator */
217703c02fdeSeric 					int sl = strlen(separator);
217803c02fdeSeric 
217903c02fdeSeric 					if (strncmp(&buf[2], separator, sl) == 0)
21808d0f8cdfSeric 						padc = ' ';
218103c02fdeSeric 				}
21828d0f8cdfSeric 				if (buf[0] == '.' &&
21838d0f8cdfSeric 				    bitnset(M_XDOT, mci->mci_mailer->m_flags))
21848d0f8cdfSeric 				{
21858d0f8cdfSeric 					padc = '.';
21868d0f8cdfSeric 				}
21878d0f8cdfSeric 
21888d0f8cdfSeric 				/* now copy out saved line */
21898d0f8cdfSeric 				if (TrafficLogFile != NULL)
21908d0f8cdfSeric 				{
21918d0f8cdfSeric 					fprintf(TrafficLogFile, "%05d >>> ", getpid());
21928d0f8cdfSeric 					if (padc != EOF)
21938d0f8cdfSeric 						fputc(padc, TrafficLogFile);
21948d0f8cdfSeric 					for (xp = buf; xp < bp; xp++)
21958d0f8cdfSeric 						fputc(*xp, TrafficLogFile);
21968d0f8cdfSeric 					if (c == '\n')
21978d0f8cdfSeric 						fputs(mci->mci_mailer->m_eol,
21988d0f8cdfSeric 						      TrafficLogFile);
21998d0f8cdfSeric 				}
22008d0f8cdfSeric 				if (padc != EOF)
22018d0f8cdfSeric 				{
22028d0f8cdfSeric 					fputc(padc, mci->mci_out);
22038d0f8cdfSeric 					pos++;
22048d0f8cdfSeric 				}
22058d0f8cdfSeric 				for (xp = buf; xp < bp; xp++)
22068d0f8cdfSeric 					fputc(*xp, mci->mci_out);
22078d0f8cdfSeric 				if (c == '\n')
22088d0f8cdfSeric 				{
22098d0f8cdfSeric 					fputs(mci->mci_mailer->m_eol,
22108d0f8cdfSeric 					      mci->mci_out);
22118d0f8cdfSeric 					pos = 0;
22128d0f8cdfSeric 				}
22138d0f8cdfSeric 				else
22148d0f8cdfSeric 				{
22158d0f8cdfSeric 					pos += bp - buf;
22168d0f8cdfSeric 					*pbp++ = c;
22178d0f8cdfSeric 				}
22188d0f8cdfSeric 				bp = buf;
22198d0f8cdfSeric 
22208d0f8cdfSeric 				/* determine next state */
22218d0f8cdfSeric 				if (c == '\n')
22228d0f8cdfSeric 					ostate = OS_HEAD;
22238d0f8cdfSeric 				else if (c == '\r')
22248d0f8cdfSeric 					ostate = OS_CR;
22258d0f8cdfSeric 				else
22268d0f8cdfSeric 					ostate = OS_INLINE;
22278d0f8cdfSeric 				continue;
22288d0f8cdfSeric 
22298d0f8cdfSeric 			  case OS_CR:
22308d0f8cdfSeric 				if (c == '\n')
22318d0f8cdfSeric 				{
22328d0f8cdfSeric 					/* got CRLF */
22338d0f8cdfSeric 					fputs(mci->mci_mailer->m_eol, mci->mci_out);
22348d0f8cdfSeric 					if (TrafficLogFile != NULL)
22358d0f8cdfSeric 					{
22368d0f8cdfSeric 						fputs(mci->mci_mailer->m_eol,
22378d0f8cdfSeric 						      TrafficLogFile);
22388d0f8cdfSeric 					}
22398d0f8cdfSeric 					ostate = OS_HEAD;
22408d0f8cdfSeric 					continue;
22418d0f8cdfSeric 				}
22428d0f8cdfSeric 
22438d0f8cdfSeric 				/* had a naked carriage return */
22448d0f8cdfSeric 				*pbp++ = c;
22458d0f8cdfSeric 				c = '\r';
22468d0f8cdfSeric 				goto putchar;
22478d0f8cdfSeric 
22488d0f8cdfSeric 			  case OS_INLINE:
22498d0f8cdfSeric 				if (c == '\r')
22508d0f8cdfSeric 				{
22518d0f8cdfSeric 					ostate = OS_CR;
22528d0f8cdfSeric 					continue;
22538d0f8cdfSeric 				}
22548d0f8cdfSeric putchar:
22558d0f8cdfSeric 				if (pos > mci->mci_mailer->m_linelimit &&
22568d0f8cdfSeric 				    c != '\n')
22578d0f8cdfSeric 				{
22588d0f8cdfSeric 					putc('!', mci->mci_out);
22598d0f8cdfSeric 					fputs(mci->mci_mailer->m_eol, mci->mci_out);
22608d0f8cdfSeric 					if (TrafficLogFile != NULL)
22618d0f8cdfSeric 					{
22628d0f8cdfSeric 						fprintf(TrafficLogFile, "!%s",
22638d0f8cdfSeric 							mci->mci_mailer->m_eol);
22648d0f8cdfSeric 					}
22658d0f8cdfSeric 					ostate = OS_HEAD;
22668d0f8cdfSeric 					*pbp++ = c;
22678d0f8cdfSeric 					continue;
22688d0f8cdfSeric 				}
22698d0f8cdfSeric 				if (TrafficLogFile != NULL)
22708d0f8cdfSeric 					fputc(c, TrafficLogFile);
22718d0f8cdfSeric 				putc(c, mci->mci_out);
22728d0f8cdfSeric 				pos++;
22738d0f8cdfSeric 				if (c == '\n')
22748d0f8cdfSeric 					ostate = OS_HEAD;
22758d0f8cdfSeric 				break;
22768d0f8cdfSeric 			}
227724fc8aeeSeric 		}
2278a6ce3008Seric 	}
227951552439Seric 
22809a6a5f55Seric 	if (ferror(e->e_dfp))
228151552439Seric 	{
2282df106f0bSeric 		syserr("putbody: %s: read error", e->e_df);
228351552439Seric 		ExitStat = EX_IOERR;
228451552439Seric 	}
228551552439Seric 
22868d0f8cdfSeric endofmessage:
22870890ba1fSeric 	/* some mailers want extra blank line at end of message */
22885aa0f353Seric 	if (bitnset(M_BLANKEND, mci->mci_mailer->m_flags) &&
22895aa0f353Seric 	    buf[0] != '\0' && buf[0] != '\n')
22905aa0f353Seric 		putline("", mci);
22910890ba1fSeric 
22925aa0f353Seric 	(void) fflush(mci->mci_out);
22935aa0f353Seric 	if (ferror(mci->mci_out) && errno != EPIPE)
229451552439Seric 	{
229551552439Seric 		syserr("putbody: write error");
229651552439Seric 		ExitStat = EX_IOERR;
229751552439Seric 	}
229851552439Seric 	errno = 0;
229925a99e2eSeric }
230025a99e2eSeric /*
230125a99e2eSeric **  MAILFILE -- Send a message to a file.
230225a99e2eSeric **
2303f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
2304f129ec7dSeric **	bits, sendmail will try to become the owner of that file
2305f129ec7dSeric **	rather than the real user.  Obviously, this only works if
2306f129ec7dSeric **	sendmail runs as root.
2307f129ec7dSeric **
2308588cad61Seric **	This could be done as a subordinate mailer, except that it
2309588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
2310588cad61Seric **	view this as being sufficiently important as to include it
2311588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
2312588cad61Seric **	to create another process plus some pipes to save the message.
2313588cad61Seric **
231425a99e2eSeric **	Parameters:
231525a99e2eSeric **		filename -- the name of the file to send to.
23166259796dSeric **		ctladdr -- the controlling address header -- includes
23176259796dSeric **			the userid/groupid to be when sending.
231825a99e2eSeric **
231925a99e2eSeric **	Returns:
232025a99e2eSeric **		The exit code associated with the operation.
232125a99e2eSeric **
232225a99e2eSeric **	Side Effects:
232325a99e2eSeric **		none.
232425a99e2eSeric */
232525a99e2eSeric 
2326b31e7f2bSeric mailfile(filename, ctladdr, e)
232725a99e2eSeric 	char *filename;
23286259796dSeric 	ADDRESS *ctladdr;
2329b31e7f2bSeric 	register ENVELOPE *e;
233025a99e2eSeric {
233125a99e2eSeric 	register FILE *f;
233232d19d43Seric 	register int pid;
233315d084d5Seric 	int mode;
233425a99e2eSeric 
2335671745f3Seric 	if (tTd(11, 1))
2336671745f3Seric 	{
2337671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
2338671745f3Seric 		printaddr(ctladdr, FALSE);
2339671745f3Seric 	}
2340671745f3Seric 
2341f170942cSeric 	if (e->e_xfp != NULL)
2342f170942cSeric 		fflush(e->e_xfp);
2343f170942cSeric 
234432d19d43Seric 	/*
234532d19d43Seric 	**  Fork so we can change permissions here.
234632d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
234732d19d43Seric 	**	the complications of calling subroutines, etc.
234832d19d43Seric 	*/
234932d19d43Seric 
235032d19d43Seric 	DOFORK(fork);
235132d19d43Seric 
235232d19d43Seric 	if (pid < 0)
235332d19d43Seric 		return (EX_OSERR);
235432d19d43Seric 	else if (pid == 0)
235532d19d43Seric 	{
235632d19d43Seric 		/* child -- actually write to file */
2357f129ec7dSeric 		struct stat stb;
23585aa0f353Seric 		MCI mcibuf;
2359f129ec7dSeric 
23602b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
23612b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
23622b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
23633462ad9eSeric 		(void) umask(OldUmask);
236495f16dc0Seric 
2365f129ec7dSeric 		if (stat(filename, &stb) < 0)
23663a98e7eaSeric 			stb.st_mode = FileMode;
236715d084d5Seric 		mode = stb.st_mode;
236895f16dc0Seric 
236995f16dc0Seric 		/* limit the errors to those actually caused in the child */
237095f16dc0Seric 		errno = 0;
237195f16dc0Seric 		ExitStat = EX_OK;
237295f16dc0Seric 
2373f129ec7dSeric 		if (bitset(0111, stb.st_mode))
2374f129ec7dSeric 			exit(EX_CANTCREAT);
237519428781Seric 		if (ctladdr != NULL)
237615d084d5Seric 		{
237715d084d5Seric 			/* ignore setuid and setgid bits */
237815d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
237915d084d5Seric 		}
238015d084d5Seric 
23818f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
23828f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
23838f9146b0Srick 		{
23848f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
238595f16dc0Seric 			if (e->e_dfp == NULL)
238695f16dc0Seric 			{
23878f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
2388e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
23898f9146b0Srick 			}
23908f9146b0Srick 		}
23918f9146b0Srick 
239215d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
2393e36b99e2Seric 		{
239419428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
239595f16dc0Seric 			{
2396898a126bSbostic 				(void) initgroups(DefUser, DefGid);
239795f16dc0Seric 			}
239895f16dc0Seric 			else
239995f16dc0Seric 			{
2400898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
2401898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
2402898a126bSbostic 					ctladdr->q_gid);
2403898a126bSbostic 			}
2404e36b99e2Seric 		}
240515d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
2406e36b99e2Seric 		{
240719428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
2408e36b99e2Seric 				(void) setuid(DefUid);
2409e36b99e2Seric 			else
24106259796dSeric 				(void) setuid(ctladdr->q_uid);
2411e36b99e2Seric 		}
241295f16dc0Seric 		FileName = filename;
241395f16dc0Seric 		LineNumber = 0;
24143a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
241525a99e2eSeric 		if (f == NULL)
241695f16dc0Seric 		{
2417b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
241832d19d43Seric 			exit(EX_CANTCREAT);
241995f16dc0Seric 		}
242025a99e2eSeric 
24215aa0f353Seric 		bzero(&mcibuf, sizeof mcibuf);
24225aa0f353Seric 		mcibuf.mci_mailer = FileMailer;
24235aa0f353Seric 		mcibuf.mci_out = f;
24245aa0f353Seric 		if (bitnset(M_7BITS, FileMailer->m_flags))
24255aa0f353Seric 			mcibuf.mci_flags |= MCIF_7BIT;
24265aa0f353Seric 
24275aa0f353Seric 		putfromline(&mcibuf, e);
2428a6ce3008Seric 		(*e->e_puthdr)(&mcibuf, e->e_header, e);
24295aa0f353Seric 		(*e->e_putbody)(&mcibuf, e, NULL);
24305aa0f353Seric 		putline("\n", &mcibuf);
243195f16dc0Seric 		if (ferror(f))
243295f16dc0Seric 		{
2433b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
243495f16dc0Seric 			setstat(EX_IOERR);
243595f16dc0Seric 		}
2436ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
243732d19d43Seric 		(void) fflush(stdout);
2438e36b99e2Seric 
243927628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
2440c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
244195f16dc0Seric 		exit(ExitStat);
244213bbc08cSeric 		/*NOTREACHED*/
244332d19d43Seric 	}
244432d19d43Seric 	else
244532d19d43Seric 	{
244632d19d43Seric 		/* parent -- wait for exit status */
2447588cad61Seric 		int st;
244832d19d43Seric 
2449588cad61Seric 		st = waitfor(pid);
2450bf9bc890Seric 		if (WIFEXITED(st))
2451bf9bc890Seric 			return (WEXITSTATUS(st));
2452588cad61Seric 		else
2453b6a0de9dSeric 		{
2454b6a0de9dSeric 			syserr("child died on signal %d", st);
2455bf9bc890Seric 			return (EX_UNAVAILABLE);
2456b6a0de9dSeric 		}
24578f9146b0Srick 		/*NOTREACHED*/
245832d19d43Seric 	}
245925a99e2eSeric }
2460ea4dc939Seric /*
2461e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2462e103b48fSeric **
2463e103b48fSeric **	The signature describes how we are going to send this -- it
2464e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2465e103b48fSeric **	an ordered list of MX hosts.
2466e103b48fSeric **
2467e103b48fSeric **	Parameters:
2468e103b48fSeric **		m -- the mailer describing this host.
2469e103b48fSeric **		host -- the host name.
2470e103b48fSeric **		e -- the current envelope.
2471e103b48fSeric **
2472e103b48fSeric **	Returns:
2473e103b48fSeric **		The signature for this host.
2474e103b48fSeric **
2475e103b48fSeric **	Side Effects:
2476e103b48fSeric **		Can tweak the symbol table.
2477e103b48fSeric */
2478e103b48fSeric 
2479e103b48fSeric char *
2480e103b48fSeric hostsignature(m, host, e)
2481e103b48fSeric 	register MAILER *m;
2482e103b48fSeric 	char *host;
2483e103b48fSeric 	ENVELOPE *e;
2484e103b48fSeric {
2485e103b48fSeric 	register char *p;
2486e103b48fSeric 	register STAB *s;
2487e103b48fSeric 	int i;
2488e103b48fSeric 	int len;
24899d4a8008Seric #if NAMED_BIND
2490e103b48fSeric 	int nmx;
2491e103b48fSeric 	auto int rcode;
2492bafdc4e5Seric 	char *hp;
2493bafdc4e5Seric 	char *endp;
2494516782b4Seric 	int oldoptions;
2495e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2496e103b48fSeric #endif
2497e103b48fSeric 
2498e103b48fSeric 	/*
2499e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2500e103b48fSeric 	*/
2501e103b48fSeric 
2502e103b48fSeric 	p = m->m_mailer;
2503e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2504e103b48fSeric 	{
2505e103b48fSeric 		/* just an ordinary mailer */
2506e103b48fSeric 		return host;
2507e103b48fSeric 	}
2508e103b48fSeric 
2509e103b48fSeric 	/*
2510e103b48fSeric 	**  Look it up in the symbol table.
2511e103b48fSeric 	*/
2512e103b48fSeric 
2513e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2514e103b48fSeric 	if (s->s_hostsig != NULL)
2515e103b48fSeric 		return s->s_hostsig;
2516e103b48fSeric 
2517e103b48fSeric 	/*
2518e103b48fSeric 	**  Not already there -- create a signature.
2519e103b48fSeric 	*/
2520e103b48fSeric 
25219d4a8008Seric #if NAMED_BIND
2522516782b4Seric 	if (ConfigLevel < 2)
2523516782b4Seric 	{
2524516782b4Seric 		oldoptions = _res.options;
2525516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2526516782b4Seric 	}
2527516782b4Seric 
2528bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2529bafdc4e5Seric 	{
2530bafdc4e5Seric 		endp = strchr(hp, ':');
2531bafdc4e5Seric 		if (endp != NULL)
2532bafdc4e5Seric 			*endp = '\0';
2533bafdc4e5Seric 
25347bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
25357d55540cSeric 
2536e103b48fSeric 		if (nmx <= 0)
2537e103b48fSeric 		{
2538e103b48fSeric 			register MCI *mci;
2539e103b48fSeric 
2540e103b48fSeric 			/* update the connection info for this host */
2541bafdc4e5Seric 			mci = mci_get(hp, m);
2542e103b48fSeric 			mci->mci_exitstat = rcode;
2543e103b48fSeric 			mci->mci_errno = errno;
25449d4a8008Seric #if NAMED_BIND
2545f170942cSeric 			mci->mci_herrno = h_errno;
2546f170942cSeric #endif
2547e103b48fSeric 
2548e103b48fSeric 			/* and return the original host name as the signature */
2549bafdc4e5Seric 			nmx = 1;
2550bafdc4e5Seric 			mxhosts[0] = hp;
2551e103b48fSeric 		}
2552e103b48fSeric 
2553e103b48fSeric 		len = 0;
2554e103b48fSeric 		for (i = 0; i < nmx; i++)
2555e103b48fSeric 		{
2556e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2557e103b48fSeric 		}
2558bafdc4e5Seric 		if (s->s_hostsig != NULL)
2559bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2560bafdc4e5Seric 		p = xalloc(len);
2561bafdc4e5Seric 		if (s->s_hostsig != NULL)
2562bafdc4e5Seric 		{
2563bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2564bafdc4e5Seric 			free(s->s_hostsig);
2565bafdc4e5Seric 			s->s_hostsig = p;
2566bafdc4e5Seric 			p += strlen(p);
2567bafdc4e5Seric 			*p++ = ':';
2568bafdc4e5Seric 		}
2569bafdc4e5Seric 		else
2570bafdc4e5Seric 			s->s_hostsig = p;
2571e103b48fSeric 		for (i = 0; i < nmx; i++)
2572e103b48fSeric 		{
2573e103b48fSeric 			if (i != 0)
2574e103b48fSeric 				*p++ = ':';
2575e103b48fSeric 			strcpy(p, mxhosts[i]);
2576e103b48fSeric 			p += strlen(p);
2577e103b48fSeric 		}
2578bafdc4e5Seric 		if (endp != NULL)
2579bafdc4e5Seric 			*endp++ = ':';
2580bafdc4e5Seric 	}
2581e103b48fSeric 	makelower(s->s_hostsig);
2582516782b4Seric 	if (ConfigLevel < 2)
2583516782b4Seric 		_res.options = oldoptions;
2584e103b48fSeric #else
2585e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2586e103b48fSeric 	s->s_hostsig = host;
2587e103b48fSeric #endif
2588e103b48fSeric 	if (tTd(17, 1))
2589e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2590e103b48fSeric 	return s->s_hostsig;
2591e103b48fSeric }
2592