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*43357751Seric static char sccsid[] = "@(#)deliver.c	8.131 (Berkeley) 03/21/95";
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 
4264187506Seric void
439c9e68d9Seric sendall(e, mode)
449c9e68d9Seric 	ENVELOPE *e;
459c9e68d9Seric 	char mode;
469c9e68d9Seric {
479c9e68d9Seric 	register ADDRESS *q;
489c9e68d9Seric 	char *owner;
499c9e68d9Seric 	int otherowners;
50b056abd0Seric 	register ENVELOPE *ee;
51b056abd0Seric 	ENVELOPE *splitenv = NULL;
526103d9b4Seric 	bool announcequeueup;
5364187506Seric 	bool oldverbose = Verbose;
5464187506Seric 	int pid;
5564187506Seric 	extern void sendenvelope();
569c9e68d9Seric 
577880f3e4Seric 	/*
587880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
597880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
607880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
617880f3e4Seric 	**  addresses to be sent.
627880f3e4Seric 	*/
637880f3e4Seric 
648d19a23dSeric 	if (bitset(EF_FATALERRS, e->e_flags) &&
658d19a23dSeric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
66896b16f6Seric 	{
67896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
68896b16f6Seric 		return;
69896b16f6Seric 	}
70896b16f6Seric 
719c9e68d9Seric 	/* determine actual delivery mode */
72fbe2f963Seric 	CurrentLA = getla();
739c9e68d9Seric 	if (mode == SM_DEFAULT)
749c9e68d9Seric 	{
759c9e68d9Seric 		mode = e->e_sendmode;
769c9e68d9Seric 		if (mode != SM_VERIFY &&
779c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
789c9e68d9Seric 			mode = SM_QUEUE;
796103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
809c9e68d9Seric 	}
816103d9b4Seric 	else
826103d9b4Seric 		announcequeueup = FALSE;
839c9e68d9Seric 
849c9e68d9Seric 	if (tTd(13, 1))
859c9e68d9Seric 	{
860e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
870e484fe5Seric 			mode, e->e_id);
889c9e68d9Seric 		printaddr(&e->e_from, FALSE);
899c9e68d9Seric 		printf("sendqueue:\n");
909c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
919c9e68d9Seric 	}
929c9e68d9Seric 
939c9e68d9Seric 	/*
949c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
959c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
969c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
979c9e68d9Seric 	*/
989c9e68d9Seric 
999c9e68d9Seric 	CurEnv = e;
1009c9e68d9Seric 
1019c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
1029c9e68d9Seric 	{
1039c9e68d9Seric 		errno = 0;
104f356687bSeric 		queueup(e, TRUE, announcequeueup);
10527607809Seric 		e->e_flags |= EF_FATALERRS|EF_PM_NOTIFY|EF_CLRQUEUE;
1064bb8b0fdSeric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
1079c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
108f7869e68Seric 			RealHostName == NULL ? "localhost" : RealHostName,
109f7869e68Seric 			e->e_sendqueue->q_paddr);
1109c9e68d9Seric 		return;
1119c9e68d9Seric 	}
1129c9e68d9Seric 
113b8004690Seric 	/*
114b8004690Seric 	**  Do sender deletion.
115b8004690Seric 	**
116b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
117b8004690Seric 	**	This can happen if the name server is hosed when you
118b8004690Seric 	**	are trying to send mail.  The result is that the sender
119b8004690Seric 	**	is instantiated in the queue as a recipient.
120b8004690Seric 	*/
121b8004690Seric 
122e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
123e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1249c9e68d9Seric 	{
1259c9e68d9Seric 		if (tTd(13, 5))
1269c9e68d9Seric 		{
1279c9e68d9Seric 			printf("sendall: QDONTSEND ");
1289c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1299c9e68d9Seric 		}
1309c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
13164187506Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, 0, e);
1329c9e68d9Seric 	}
1339c9e68d9Seric 
134ce5531bdSeric 	/*
135ce5531bdSeric 	**  Handle alias owners.
136ce5531bdSeric 	**
137ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
138ce5531bdSeric 	**	We discard owners that are the same as the return path.
139ce5531bdSeric 	*/
140ce5531bdSeric 
141ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
142ce5531bdSeric 	{
143ce5531bdSeric 		register struct address *a;
144ce5531bdSeric 
145ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
146ce5531bdSeric 			continue;
147ce5531bdSeric 		if (a != NULL)
148ce5531bdSeric 			q->q_owner = a->q_owner;
149ce5531bdSeric 
150ce5531bdSeric 		if (q->q_owner != NULL &&
151ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
152ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
153ce5531bdSeric 			q->q_owner = NULL;
154ce5531bdSeric 	}
155ce5531bdSeric 
156ce5531bdSeric 	owner = "";
157ce5531bdSeric 	otherowners = 1;
158ce5531bdSeric 	while (owner != NULL && otherowners > 0)
159ce5531bdSeric 	{
160ce5531bdSeric 		owner = NULL;
161ce5531bdSeric 		otherowners = 0;
162ce5531bdSeric 
163ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
164ce5531bdSeric 		{
165ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
166ce5531bdSeric 				continue;
167ce5531bdSeric 
168ce5531bdSeric 			if (q->q_owner != NULL)
169ce5531bdSeric 			{
170ce5531bdSeric 				if (owner == NULL)
171ce5531bdSeric 					owner = q->q_owner;
172ce5531bdSeric 				else if (owner != q->q_owner)
173ce5531bdSeric 				{
174ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
175ce5531bdSeric 					{
176ce5531bdSeric 						/* make future comparisons cheap */
177ce5531bdSeric 						q->q_owner = owner;
178ce5531bdSeric 					}
179ce5531bdSeric 					else
180ce5531bdSeric 					{
181ce5531bdSeric 						otherowners++;
182ce5531bdSeric 					}
183ce5531bdSeric 					owner = q->q_owner;
184ce5531bdSeric 				}
185ce5531bdSeric 			}
186ce5531bdSeric 			else
187ce5531bdSeric 			{
188ce5531bdSeric 				otherowners++;
189ce5531bdSeric 			}
190ce5531bdSeric 		}
191ce5531bdSeric 
192ce5531bdSeric 		if (owner != NULL && otherowners > 0)
193ce5531bdSeric 		{
194ce5531bdSeric 			extern HDR *copyheader();
195ce5531bdSeric 			extern ADDRESS *copyqueue();
196ce5531bdSeric 
197ce5531bdSeric 			/*
198ce5531bdSeric 			**  Split this envelope into two.
199ce5531bdSeric 			*/
200ce5531bdSeric 
201ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
202ce5531bdSeric 			*ee = *e;
203ce5531bdSeric 			ee->e_id = NULL;
204ce5531bdSeric 			(void) queuename(ee, '\0');
205ce5531bdSeric 
206ce5531bdSeric 			if (tTd(13, 1))
207ce5531bdSeric 				printf("sendall: split %s into %s\n",
208ce5531bdSeric 					e->e_id, ee->e_id);
209ce5531bdSeric 
210ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
211ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
212ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
213fce3c07bSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS|EF_SENDRECEIPT);
214fce3c07bSeric 			ee->e_flags |= EF_NORECEIPT;
215ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
216ce5531bdSeric 			if (tTd(13, 5))
217ce5531bdSeric 			{
218ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
219ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
220ce5531bdSeric 			}
221ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
222ce5531bdSeric 			ee->e_dfp = NULL;
223ce5531bdSeric 			ee->e_xfp = NULL;
224ce5531bdSeric 			ee->e_df = NULL;
225ce5531bdSeric 			ee->e_errormode = EM_MAIL;
226b056abd0Seric 			ee->e_sibling = splitenv;
227b056abd0Seric 			splitenv = ee;
228ce5531bdSeric 
229ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
230ce5531bdSeric 				if (q->q_owner == owner)
231de1a6b1bSeric 				{
232ce5531bdSeric 					q->q_flags |= QDONTSEND;
233de1a6b1bSeric 					q->q_flags &= ~QQUEUEUP;
234de1a6b1bSeric 				}
235ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
236ce5531bdSeric 				if (q->q_owner != owner)
237de1a6b1bSeric 				{
238ce5531bdSeric 					q->q_flags |= QDONTSEND;
239de1a6b1bSeric 					q->q_flags &= ~QQUEUEUP;
240de1a6b1bSeric 				}
241ce5531bdSeric 
242ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
243ce5531bdSeric 			{
244ce5531bdSeric 				ee->e_dfp = NULL;
245da662164Seric 				ee->e_df = queuename(ee, 'd');
246da662164Seric 				ee->e_df = newstr(ee->e_df);
247ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
248ce5531bdSeric 				{
249ce5531bdSeric 					syserr("sendall: link(%s, %s)",
250ce5531bdSeric 						e->e_df, ee->e_df);
251ce5531bdSeric 				}
252ce5531bdSeric 			}
253ce5531bdSeric #ifdef LOG
254ce5531bdSeric 			if (LogLevel > 4)
255c8b70947Seric 				syslog(LOG_INFO, "%s: clone %s, owner=%s",
256c8b70947Seric 					ee->e_id, e->e_id, owner);
257ce5531bdSeric #endif
258ce5531bdSeric 		}
259ce5531bdSeric 	}
260ce5531bdSeric 
261ce5531bdSeric 	if (owner != NULL)
262ce5531bdSeric 	{
263ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
264ce5531bdSeric 		if (tTd(13, 5))
265ce5531bdSeric 		{
266ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
267ce5531bdSeric 			printaddr(&e->e_from, FALSE);
268ce5531bdSeric 		}
269ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
270ce5531bdSeric 		e->e_errormode = EM_MAIL;
271fce3c07bSeric 		e->e_flags |= EF_NORECEIPT;
272ce5531bdSeric 	}
273ce5531bdSeric 
274c1ac89b1Seric # ifdef QUEUE
275c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
276c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
277c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
278c1ac89b1Seric 	{
279c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2806103d9b4Seric 		queueup(e, TRUE, announcequeueup);
281b056abd0Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
282b056abd0Seric 			queueup(ee, TRUE, announcequeueup);
283c1ac89b1Seric 	}
284c1ac89b1Seric #endif /* QUEUE */
285c1ac89b1Seric 
2867880f3e4Seric 	/*
28764187506Seric 	**  If we belong in background, fork now.
2887880f3e4Seric 	*/
2897880f3e4Seric 
290c1ac89b1Seric 	switch (mode)
291c1ac89b1Seric 	{
292c1ac89b1Seric 	  case SM_VERIFY:
293c1ac89b1Seric 		Verbose = TRUE;
294c1ac89b1Seric 		break;
295c1ac89b1Seric 
296c1ac89b1Seric 	  case SM_QUEUE:
297c1ac89b1Seric   queueonly:
298c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
299c1ac89b1Seric 		return;
300c1ac89b1Seric 
301c1ac89b1Seric 	  case SM_FORK:
302c1ac89b1Seric 		if (e->e_xfp != NULL)
303c1ac89b1Seric 			(void) fflush(e->e_xfp);
304c1ac89b1Seric 
305b269aa8eSeric # if !HASFLOCK
306c1ac89b1Seric 		/*
3076b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3086b2765c6Seric 		**  the lock is owned by a process, not by an open file
3096b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3106b2765c6Seric 		**  then restart from scratch in the child.
311c1ac89b1Seric 		*/
312c1ac89b1Seric 
3136b2765c6Seric 		/* save id for future use */
31464187506Seric 		qid = e->e_id;
3156b2765c6Seric 
3166b2765c6Seric 		/* now drop the envelope in the parent */
3176b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3186b2765c6Seric 		dropenvelope(e);
3196b2765c6Seric 
3206b2765c6Seric 		/* and reacquire in the child */
32164187506Seric 		(void) dowork(qid, TRUE, FALSE, e);
3226b2765c6Seric 
3236b2765c6Seric 		return;
3246b2765c6Seric 
3256b2765c6Seric # else /* HASFLOCK */
326c1ac89b1Seric 
327c1ac89b1Seric 		pid = fork();
328c1ac89b1Seric 		if (pid < 0)
329c1ac89b1Seric 		{
330c1ac89b1Seric 			goto queueonly;
331c1ac89b1Seric 		}
332c1ac89b1Seric 		else if (pid > 0)
333c1ac89b1Seric 		{
3340e484fe5Seric 			/* be sure we leave the temp files to our child */
3350e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3360e484fe5Seric 			if (e->e_lockfp != NULL)
3370e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3380e484fe5Seric 			e->e_lockfp = NULL;
3390e484fe5Seric 
3400e484fe5Seric 			/* close any random open files in the envelope */
3410e484fe5Seric 			closexscript(e);
3420e484fe5Seric 			if (e->e_dfp != NULL)
3430e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3440e484fe5Seric 			e->e_dfp = NULL;
3450e484fe5Seric 			e->e_id = e->e_df = NULL;
3469f9b003eSeric 
3479f9b003eSeric 			/* catch intermediate zombie */
3489f9b003eSeric 			(void) waitfor(pid);
349c1ac89b1Seric 			return;
350c1ac89b1Seric 		}
351c1ac89b1Seric 
352c1ac89b1Seric 		/* double fork to avoid zombies */
3539f9b003eSeric 		pid = fork();
3549f9b003eSeric 		if (pid > 0)
355c1ac89b1Seric 			exit(EX_OK);
356c1ac89b1Seric 
357c1ac89b1Seric 		/* be sure we are immune from the terminal */
3587880f3e4Seric 		disconnect(1, e);
359c1ac89b1Seric 
3609f9b003eSeric 		/* prevent parent from waiting if there was an error */
3619f9b003eSeric 		if (pid < 0)
3629f9b003eSeric 		{
3639f9b003eSeric 			e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3649f9b003eSeric 			finis();
3659f9b003eSeric 		}
3669f9b003eSeric 
367c1ac89b1Seric 		/*
368c1ac89b1Seric 		**  Close any cached connections.
369c1ac89b1Seric 		**
370c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
371c1ac89b1Seric 		**	still knows about the connection.
372c1ac89b1Seric 		**
373c1ac89b1Seric 		**	This should only happen when delivering an error
374c1ac89b1Seric 		**	message.
375c1ac89b1Seric 		*/
376c1ac89b1Seric 
377c1ac89b1Seric 		mci_flush(FALSE, NULL);
378c1ac89b1Seric 
3796b2765c6Seric # endif /* HASFLOCK */
3806b2765c6Seric 
381c1ac89b1Seric 		break;
382c1ac89b1Seric 	}
383c1ac89b1Seric 
38464187506Seric 	if (splitenv != NULL)
38564187506Seric 	{
38664187506Seric 		if (tTd(13, 1))
38764187506Seric 		{
38864187506Seric 			printf("\nsendall: Split queue; remaining queue:\n");
38964187506Seric 			printaddr(e->e_sendqueue, TRUE);
39064187506Seric 		}
39164187506Seric 
39264187506Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
39364187506Seric 		{
39464187506Seric 			CurEnv = ee;
39564187506Seric 			if (mode != SM_VERIFY)
39664187506Seric 				openxscript(ee);
39764187506Seric 			sendenvelope(ee, mode);
39864187506Seric 			dropenvelope(ee);
39964187506Seric 		}
40064187506Seric 
40164187506Seric 		CurEnv = e;
40264187506Seric 	}
40364187506Seric 	sendenvelope(e, mode);
40464187506Seric 	Verbose = oldverbose;
40564187506Seric }
40664187506Seric 
40764187506Seric void
40864187506Seric sendenvelope(e, mode)
40964187506Seric 	register ENVELOPE *e;
41064187506Seric 	char mode;
41164187506Seric {
41264187506Seric 	register ADDRESS *q;
41364187506Seric 	bool didany;
41464187506Seric 
41564187506Seric 	/*
41664187506Seric 	**  If we have had global, fatal errors, don't bother sending
41764187506Seric 	**  the message at all if we are in SMTP mode.  Local errors
41864187506Seric 	**  (e.g., a single address failing) will still cause the other
41964187506Seric 	**  addresses to be sent.
42064187506Seric 	*/
42164187506Seric 
42264187506Seric 	if (bitset(EF_FATALERRS, e->e_flags) &&
42364187506Seric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
42464187506Seric 	{
42564187506Seric 		e->e_flags |= EF_CLRQUEUE;
42664187506Seric 		return;
42764187506Seric 	}
42864187506Seric 
429c1ac89b1Seric 	/*
4309c9e68d9Seric 	**  Run through the list and send everything.
4315288e21aSeric 	**
4325288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4335288e21aSeric 	**	result in returned mail.
4349c9e68d9Seric 	*/
4359c9e68d9Seric 
4369c9e68d9Seric 	e->e_nsent = 0;
4375288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
43864187506Seric 	didany = FALSE;
439faad2b16Seric 
440faad2b16Seric 	/* now run through the queue */
4419c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4429c9e68d9Seric 	{
443fdc75a0fSeric #ifdef XDEBUG
444fdc75a0fSeric 		char wbuf[MAXNAME + 20];
445fdc75a0fSeric 
446fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
447fdc75a0fSeric 		checkfd012(wbuf);
448fdc75a0fSeric #endif
4499c9e68d9Seric 		if (mode == SM_VERIFY)
4509c9e68d9Seric 		{
4519c9e68d9Seric 			e->e_to = q->q_paddr;
4529c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4538dfca105Seric 			{
454fafe46e1Seric 				if (q->q_host != NULL && q->q_host[0] != '\0')
4558dfca105Seric 					message("deliverable: mailer %s, host %s, user %s",
4568dfca105Seric 						q->q_mailer->m_name,
4578dfca105Seric 						q->q_host,
4588dfca105Seric 						q->q_user);
459fafe46e1Seric 				else
460fafe46e1Seric 					message("deliverable: mailer %s, user %s",
461fafe46e1Seric 						q->q_mailer->m_name,
462fafe46e1Seric 						q->q_user);
4638dfca105Seric 			}
4649c9e68d9Seric 		}
4659c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4669c9e68d9Seric 		{
4679c9e68d9Seric # ifdef QUEUE
4689c9e68d9Seric 			/*
4699c9e68d9Seric 			**  Checkpoint the send list every few addresses
4709c9e68d9Seric 			*/
4719c9e68d9Seric 
4729c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4739c9e68d9Seric 			{
4749c9e68d9Seric 				queueup(e, TRUE, FALSE);
4759c9e68d9Seric 				e->e_nsent = 0;
4769c9e68d9Seric 			}
4779c9e68d9Seric # endif /* QUEUE */
4789c9e68d9Seric 			(void) deliver(e, q);
47964187506Seric 			didany = TRUE;
4809c9e68d9Seric 		}
4819c9e68d9Seric 	}
48264187506Seric 	if (didany)
48364187506Seric 	{
48464187506Seric 		e->e_dtime = curtime();
48564187506Seric 		e->e_ntries++;
48664187506Seric 	}
4879c9e68d9Seric 
488fdc75a0fSeric #ifdef XDEBUG
489fdc75a0fSeric 	checkfd012("end of sendenvelope");
490fdc75a0fSeric #endif
491fdc75a0fSeric 
4929c9e68d9Seric 	if (mode == SM_FORK)
4939c9e68d9Seric 		finis();
4949c9e68d9Seric }
4959c9e68d9Seric /*
4969c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4979c9e68d9Seric **
4989c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4999c9e68d9Seric **	two processes on the same stack!!!
5009c9e68d9Seric **
5019c9e68d9Seric **	Parameters:
5029c9e68d9Seric **		none.
5039c9e68d9Seric **
5049c9e68d9Seric **	Returns:
5059c9e68d9Seric **		From a macro???  You've got to be kidding!
5069c9e68d9Seric **
5079c9e68d9Seric **	Side Effects:
5089c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
5099c9e68d9Seric **			pid of child in parent, zero in child.
5109c9e68d9Seric **			-1 on unrecoverable error.
5119c9e68d9Seric **
5129c9e68d9Seric **	Notes:
5139c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
5149c9e68d9Seric **		vfork for you.....
5159c9e68d9Seric */
5169c9e68d9Seric 
5179c9e68d9Seric # define NFORKTRIES	5
5189c9e68d9Seric 
5199c9e68d9Seric # ifndef FORK
5209c9e68d9Seric # define FORK	fork
5219c9e68d9Seric # endif
5229c9e68d9Seric 
5239c9e68d9Seric # define DOFORK(fORKfN) \
5249c9e68d9Seric {\
5259c9e68d9Seric 	register int i;\
5269c9e68d9Seric \
5279c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
5289c9e68d9Seric 	{\
5299c9e68d9Seric 		pid = fORKfN();\
5309c9e68d9Seric 		if (pid >= 0)\
5319c9e68d9Seric 			break;\
5329c9e68d9Seric 		if (i > 0)\
5339c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5349c9e68d9Seric 	}\
5359c9e68d9Seric }
5369c9e68d9Seric /*
5379c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5389c9e68d9Seric **
5399c9e68d9Seric **	Parameters:
5409c9e68d9Seric **		none.
5419c9e68d9Seric **
5429c9e68d9Seric **	Returns:
5439c9e68d9Seric **		pid of child in parent.
5449c9e68d9Seric **		zero in child.
5459c9e68d9Seric **		-1 on error.
5469c9e68d9Seric **
5479c9e68d9Seric **	Side Effects:
5489c9e68d9Seric **		returns twice, once in parent and once in child.
5499c9e68d9Seric */
5509c9e68d9Seric 
5519c9e68d9Seric dofork()
5529c9e68d9Seric {
55364187506Seric 	register int pid = -1;
5549c9e68d9Seric 
5559c9e68d9Seric 	DOFORK(fork);
5569c9e68d9Seric 	return (pid);
5579c9e68d9Seric }
5589c9e68d9Seric /*
55913bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
56013bbc08cSeric **
56113bbc08cSeric **	This routine delivers to everyone on the same host as the
56213bbc08cSeric **	user on the head of the list.  It is clever about mailers
56313bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
56413bbc08cSeric **	that it will deliver to all these addresses however -- so
56513bbc08cSeric **	deliver should be called once for each address on the
56613bbc08cSeric **	list.
56725a99e2eSeric **
56825a99e2eSeric **	Parameters:
569588cad61Seric **		e -- the envelope to deliver.
570c77d1c25Seric **		firstto -- head of the address list to deliver to.
57125a99e2eSeric **
57225a99e2eSeric **	Returns:
57325a99e2eSeric **		zero -- successfully delivered.
57425a99e2eSeric **		else -- some failure, see ExitStat for more info.
57525a99e2eSeric **
57625a99e2eSeric **	Side Effects:
57725a99e2eSeric **		The standard input is passed off to someone.
57825a99e2eSeric */
57925a99e2eSeric 
58064187506Seric int
581588cad61Seric deliver(e, firstto)
582588cad61Seric 	register ENVELOPE *e;
583c77d1c25Seric 	ADDRESS *firstto;
58425a99e2eSeric {
58578442df3Seric 	char *host;			/* host being sent to */
58678442df3Seric 	char *user;			/* user being sent to */
58725a99e2eSeric 	char **pvp;
5885dfc646bSeric 	register char **mvp;
58925a99e2eSeric 	register char *p;
590588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5916259796dSeric 	ADDRESS *ctladdr;
592b31e7f2bSeric 	register MCI *mci;
593c77d1c25Seric 	register ADDRESS *to = firstto;
594c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
59564187506Seric 	ADDRESS *tochain = NULL;	/* users chain in this mailer call */
596911693bfSbostic 	int rcode;			/* response code */
597e103b48fSeric 	char *firstsig;			/* signature of firstto */
59864187506Seric 	int pid = -1;
5999c9e68d9Seric 	char *curhost;
60064187506Seric 	time_t xstart;
6019c9e68d9Seric 	int mpvect[2];
6029c9e68d9Seric 	int rpvect[2];
603ee6bf8dfSeric 	char *pv[MAXPV+1];
604579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
605a524e4d0Seric 	char buf[MAXNAME + 1];
606a524e4d0Seric 	char rpathbuf[MAXNAME + 1];	/* translated return path */
607fabb3bd4Seric 	extern int checkcompat();
60825a99e2eSeric 
60935490626Seric 	errno = 0;
610ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
6115dfc646bSeric 		return (0);
61225a99e2eSeric 
6139d4a8008Seric #if NAMED_BIND
614912a731aSbostic 	/* unless interactive, try twice, over a minute */
6158d19a23dSeric 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP)
6168d19a23dSeric 	{
617912a731aSbostic 		_res.retrans = 30;
618912a731aSbostic 		_res.retry = 2;
619912a731aSbostic 	}
620d4bd8f0eSbostic #endif
621912a731aSbostic 
62251552439Seric 	m = to->q_mailer;
62351552439Seric 	host = to->q_host;
624c9be6216Seric 	CurEnv = e;			/* just in case */
6254384d521Seric 	e->e_statmsg = NULL;
626df106f0bSeric 	SmtpError[0] = '\0';
62764187506Seric 	xstart = curtime();
62851552439Seric 
6296ef48975Seric 	if (tTd(10, 1))
630562ec147Seric 		printf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n",
631562ec147Seric 			e->e_id, m->m_name, host, to->q_user);
632ab81ee53Seric 	if (tTd(10, 100))
633ab81ee53Seric 		printopenfds(FALSE);
634f3dbc832Seric 
635f3dbc832Seric 	/*
636f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
637f3dbc832Seric 	**  connections now, just mark these addresses and return.
638f3dbc832Seric 	**	This is useful if we want to batch connections to
639f3dbc832Seric 	**	reduce load.  This will cause the messages to be
640f3dbc832Seric 	**	queued up, and a daemon will come along to send the
641f3dbc832Seric 	**	messages later.
642f3dbc832Seric 	**		This should be on a per-mailer basis.
643f3dbc832Seric 	*/
644f3dbc832Seric 
64579aa5155Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
646f3dbc832Seric 	{
647f3dbc832Seric 		for (; to != NULL; to = to->q_next)
648f4560e80Seric 		{
649ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
650c6e18ac5Seric 			    to->q_mailer != m)
651f4560e80Seric 				continue;
652268a25e1Seric 			to->q_flags |= QQUEUEUP;
653588cad61Seric 			e->e_to = to->q_paddr;
65408b25121Seric 			message("queued");
6552f624c86Seric 			if (LogLevel > 8)
65664187506Seric 				logdelivery(m, NULL, "queued", NULL, xstart, e);
657f4560e80Seric 		}
658588cad61Seric 		e->e_to = NULL;
659f3dbc832Seric 		return (0);
660f3dbc832Seric 	}
661f3dbc832Seric 
66225a99e2eSeric 	/*
6635dfc646bSeric 	**  Do initial argv setup.
6645dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6655dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6665dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6675dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6685dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6693bea8136Seric 	**		The from address rewrite is expected to make
6703bea8136Seric 	**		the address relative to the other end.
6715dfc646bSeric 	*/
6725dfc646bSeric 
67378442df3Seric 	/* rewrite from address, using rewriting rules */
674efe54562Seric 	rcode = EX_OK;
67564187506Seric 	if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
67664187506Seric 		p = e->e_sender;
67764187506Seric 	else
67864187506Seric 		p = e->e_from.q_paddr;
67964187506Seric 	(void) strcpy(rpathbuf, remotename(p, m,
680efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
681efe54562Seric 					   &rcode, e));
682ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
683588cad61Seric 	define('h', host, e);			/* to host */
6845dfc646bSeric 	Errors = 0;
6855dfc646bSeric 	pvp = pv;
6865dfc646bSeric 	*pvp++ = m->m_argv[0];
6875dfc646bSeric 
6885dfc646bSeric 	/* insert -f or -r flag as appropriate */
68957fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6905dfc646bSeric 	{
69157fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6925dfc646bSeric 			*pvp++ = "-f";
6935dfc646bSeric 		else
6945dfc646bSeric 			*pvp++ = "-r";
695c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6965dfc646bSeric 	}
6975dfc646bSeric 
6985dfc646bSeric 	/*
6995dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
7005dfc646bSeric 	**  up to the first entry containing "$u".  There can only
7015dfc646bSeric 	**  be one of these, and there are only a few more slots
7025dfc646bSeric 	**  in the pv after it.
7035dfc646bSeric 	*/
7045dfc646bSeric 
7055dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
7065dfc646bSeric 	{
7072bc47524Seric 		/* can't use strchr here because of sign extension problems */
7082bc47524Seric 		while (*p != '\0')
7092bc47524Seric 		{
7102bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
7112bc47524Seric 			{
7122bc47524Seric 				if (*p == 'u')
7135dfc646bSeric 					break;
7142bc47524Seric 			}
7152bc47524Seric 		}
7162bc47524Seric 
7172bc47524Seric 		if (*p != '\0')
7185dfc646bSeric 			break;
7195dfc646bSeric 
7205dfc646bSeric 		/* this entry is safe -- go ahead and process it */
721fbf6dabfSeric 		expand(*mvp, buf, sizeof buf, e);
7225dfc646bSeric 		*pvp++ = newstr(buf);
7235dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
7245dfc646bSeric 		{
72508b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
7265dfc646bSeric 			return (-1);
7275dfc646bSeric 		}
7285dfc646bSeric 	}
729c579ef51Seric 
73033db8731Seric 	/*
73133db8731Seric 	**  If we have no substitution for the user name in the argument
73233db8731Seric 	**  list, we know that we must supply the names otherwise -- and
73333db8731Seric 	**  SMTP is the answer!!
73433db8731Seric 	*/
73533db8731Seric 
7365dfc646bSeric 	if (*mvp == NULL)
737c579ef51Seric 	{
738c579ef51Seric 		/* running SMTP */
7392c7e1b8dSeric # ifdef SMTP
740c579ef51Seric 		clever = TRUE;
741c579ef51Seric 		*pvp = NULL;
7426c2c3107Seric # else /* SMTP */
74333db8731Seric 		/* oops!  we don't implement SMTP */
744df106f0bSeric 		syserr("554 SMTP style mailer not implemented");
7452c7e1b8dSeric 		return (EX_SOFTWARE);
7466c2c3107Seric # endif /* SMTP */
747c579ef51Seric 	}
7485dfc646bSeric 
7495dfc646bSeric 	/*
7505dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7515dfc646bSeric 	**  run through our address list and append all the addresses
7525dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7535dfc646bSeric 	**  always send another copy later.
7545dfc646bSeric 	*/
7555dfc646bSeric 
7565dfc646bSeric 	tobuf[0] = '\0';
757588cad61Seric 	e->e_to = tobuf;
7586259796dSeric 	ctladdr = NULL;
759e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7605dfc646bSeric 	for (; to != NULL; to = to->q_next)
7615dfc646bSeric 	{
7625dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
76357fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7645dfc646bSeric 			break;
7655dfc646bSeric 
7665dfc646bSeric 		/* if already sent or not for this host, don't send */
767ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
768e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
769e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7705dfc646bSeric 			continue;
7716259796dSeric 
7724b22ea87Seric 		/* avoid overflowing tobuf */
773aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7744b22ea87Seric 			break;
7754b22ea87Seric 
7766ef48975Seric 		if (tTd(10, 1))
777772e6e50Seric 		{
778772e6e50Seric 			printf("\nsend to ");
779772e6e50Seric 			printaddr(to, FALSE);
780772e6e50Seric 		}
781772e6e50Seric 
7826259796dSeric 		/* compute effective uid/gid when sending */
78364187506Seric 		if (bitnset(M_RUNASRCPT, to->q_mailer->m_flags))
7846259796dSeric 			ctladdr = getctladdr(to);
7856259796dSeric 
78664187506Seric 		if (tTd(10, 2))
78764187506Seric 		{
78864187506Seric 			printf("ctladdr=");
78964187506Seric 			printaddr(ctladdr, FALSE);
79064187506Seric 		}
79164187506Seric 
7925dfc646bSeric 		user = to->q_user;
793588cad61Seric 		e->e_to = to->q_paddr;
79475f1ade9Seric 		if (tTd(10, 5))
79575f1ade9Seric 		{
79675f1ade9Seric 			printf("deliver: QDONTSEND ");
79775f1ade9Seric 			printaddr(to, FALSE);
79875f1ade9Seric 		}
799ee4b0922Seric 		to->q_flags |= QDONTSEND;
8005dfc646bSeric 
8015dfc646bSeric 		/*
8025dfc646bSeric 		**  Check to see that these people are allowed to
8035dfc646bSeric 		**  talk to each other.
8042a6e0786Seric 		*/
8052a6e0786Seric 
80669582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
80769582d2fSeric 		{
808*43357751Seric 			e->e_flags |= EF_NO_BODY_RETN;
80908b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
81064187506Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, xstart, e);
81169582d2fSeric 			continue;
81269582d2fSeric 		}
81364187506Seric #if NAMED_BIND
81464187506Seric 		h_errno = 0;
81564187506Seric #endif
816fabb3bd4Seric 		rcode = checkcompat(to, e);
8171793c9c5Seric 		if (rcode != EX_OK)
8185dfc646bSeric 		{
81964187506Seric 			markfailure(e, to, NULL, rcode);
82064187506Seric 			giveresponse(rcode, m, NULL, ctladdr, xstart, e);
8215dfc646bSeric 			continue;
8225dfc646bSeric 		}
8232a6e0786Seric 
8242a6e0786Seric 		/*
8259ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
8269ec9501bSeric 		**	about them.
82725a99e2eSeric 		*/
82825a99e2eSeric 
82957fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
83025a99e2eSeric 		{
8311d8f1806Seric 			stripquotes(user);
8321d8f1806Seric 			stripquotes(host);
83325a99e2eSeric 		}
83425a99e2eSeric 
835cdb828c5Seric 		/* hack attack -- delivermail compatibility */
836cdb828c5Seric 		if (m == ProgMailer && *user == '|')
837cdb828c5Seric 			user++;
838cdb828c5Seric 
83925a99e2eSeric 		/*
8403efaed6eSeric 		**  If an error message has already been given, don't
8413efaed6eSeric 		**	bother to send to this address.
8423efaed6eSeric 		**
8433efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
8443efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
8453efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
8463efaed6eSeric 		*/
8473efaed6eSeric 
8486cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8493efaed6eSeric 			continue;
8503efaed6eSeric 
851f2fec898Seric 		/* save statistics.... */
852588cad61Seric 		markstats(e, to);
853f2fec898Seric 
8543efaed6eSeric 		/*
85525a99e2eSeric 		**  See if this user name is "special".
85625a99e2eSeric 		**	If the user name has a slash in it, assume that this
85751552439Seric 		**	is a file -- send it off without further ado.  Note
85851552439Seric 		**	that this type of addresses is not processed along
85951552439Seric 		**	with the others, so we fudge on the To person.
86025a99e2eSeric 		*/
86125a99e2eSeric 
8622c017f8dSeric 		if (m == FileMailer)
86325a99e2eSeric 		{
86403463a75Seric 			rcode = mailfile(user, ctladdr, e);
86564187506Seric 			giveresponse(rcode, m, NULL, ctladdr, xstart, e);
86664187506Seric 			e->e_nsent++;
867dde5acadSeric 			if (rcode == EX_OK)
86864187506Seric 			{
869dde5acadSeric 				to->q_flags |= QSENT;
87064187506Seric 				if (bitnset(M_LOCALMAILER, m->m_flags) &&
87164187506Seric 				    (e->e_receiptto != NULL ||
87264187506Seric 				     bitset(QPINGONSUCCESS, to->q_flags)))
87364187506Seric 				{
87464187506Seric 					to->q_flags |= QREPORT;
87564187506Seric 					fprintf(e->e_xfp, "%s... Successfully delivered\n",
87664187506Seric 						to->q_paddr);
87764187506Seric 				}
87864187506Seric 			}
87964187506Seric 			to->q_statdate = curtime();
8805dfc646bSeric 			continue;
88125a99e2eSeric 		}
88225a99e2eSeric 
88313bbc08cSeric 		/*
88413bbc08cSeric 		**  Address is verified -- add this user to mailer
88513bbc08cSeric 		**  argv, and add it to the print list of recipients.
88613bbc08cSeric 		*/
88713bbc08cSeric 
888508daeccSeric 		/* link together the chain of recipients */
889508daeccSeric 		to->q_tchain = tochain;
890508daeccSeric 		tochain = to;
891508daeccSeric 
8925dfc646bSeric 		/* create list of users for error messages */
893db8841e9Seric 		(void) strcat(tobuf, ",");
894db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
895588cad61Seric 		define('u', user, e);		/* to user */
896b6f89e6eSeric 		p = to->q_home;
897b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
898b6f89e6eSeric 			p = ctladdr->q_home;
899b6f89e6eSeric 		define('z', p, e);	/* user's home */
9005dfc646bSeric 
901c579ef51Seric 		/*
902508daeccSeric 		**  Expand out this user into argument list.
903c579ef51Seric 		*/
904c579ef51Seric 
905508daeccSeric 		if (!clever)
906c579ef51Seric 		{
907fbf6dabfSeric 			expand(*mvp, buf, sizeof buf, e);
9085dfc646bSeric 			*pvp++ = newstr(buf);
9095dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
9105dfc646bSeric 			{
9115dfc646bSeric 				/* allow some space for trailing parms */
9125dfc646bSeric 				break;
9135dfc646bSeric 			}
9145dfc646bSeric 		}
915c579ef51Seric 	}
9165dfc646bSeric 
917145b49b1Seric 	/* see if any addresses still exist */
918145b49b1Seric 	if (tobuf[0] == '\0')
919c579ef51Seric 	{
920588cad61Seric 		define('g', (char *) NULL, e);
921145b49b1Seric 		return (0);
922c579ef51Seric 	}
923145b49b1Seric 
9245dfc646bSeric 	/* print out messages as full list */
92563780dbdSeric 	e->e_to = tobuf + 1;
9265dfc646bSeric 
9275dfc646bSeric 	/*
9285dfc646bSeric 	**  Fill out any parameters after the $u parameter.
9295dfc646bSeric 	*/
9305dfc646bSeric 
931c579ef51Seric 	while (!clever && *++mvp != NULL)
9325dfc646bSeric 	{
933fbf6dabfSeric 		expand(*mvp, buf, sizeof buf, e);
9345dfc646bSeric 		*pvp++ = newstr(buf);
9355dfc646bSeric 		if (pvp >= &pv[MAXPV])
93608b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
9375dfc646bSeric 	}
9385dfc646bSeric 	*pvp++ = NULL;
9395dfc646bSeric 
94025a99e2eSeric 	/*
94125a99e2eSeric 	**  Call the mailer.
9426328bdf7Seric 	**	The argument vector gets built, pipes
94325a99e2eSeric 	**	are created as necessary, and we fork & exec as
9446328bdf7Seric 	**	appropriate.
945c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
94625a99e2eSeric 	*/
94725a99e2eSeric 
948960e665aSeric 	/*XXX this seems a bit wierd */
9494899f6b5Seric 	if (ctladdr == NULL && m != ProgMailer &&
9504899f6b5Seric 	    bitset(QGOODUID, e->e_from.q_flags))
951960e665aSeric 		ctladdr = &e->e_from;
952960e665aSeric 
9539d4a8008Seric #if NAMED_BIND
9542bcc6d2dSeric 	if (ConfigLevel < 2)
955912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
956134746fbSeric #endif
9579c9e68d9Seric 
9589c9e68d9Seric 	if (tTd(11, 1))
959134746fbSeric 	{
9609c9e68d9Seric 		printf("openmailer:");
9619c9e68d9Seric 		printav(pv);
9629c9e68d9Seric 	}
9639c9e68d9Seric 	errno = 0;
96464187506Seric #if NAMED_BIND
96564187506Seric 	h_errno = 0;
96664187506Seric #endif
9679c9e68d9Seric 
96864187506Seric 	CurHostName = NULL;
9699c9e68d9Seric 
9709c9e68d9Seric 	/*
9719c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9729c9e68d9Seric 	**  connection.
9739c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9749c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9759c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9769c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9779c9e68d9Seric 	*/
9789c9e68d9Seric 
9799c9e68d9Seric 	curhost = NULL;
980c931b82bSeric 	SmtpPhase = NULL;
981df106f0bSeric 	mci = NULL;
9829c9e68d9Seric 
9837febfd66Seric #ifdef XDEBUG
9847febfd66Seric 	{
9857febfd66Seric 		char wbuf[MAXLINE];
9867febfd66Seric 
9877febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9887febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
9897febfd66Seric 		checkfd012(wbuf);
9907febfd66Seric 	}
9917febfd66Seric #endif
9927febfd66Seric 
99364187506Seric 	/* check for 8-bit available */
99464187506Seric 	if (bitset(EF_HAS8BIT, e->e_flags) &&
99564187506Seric 	    bitnset(M_7BITS, m->m_flags) &&
99664187506Seric 	    !bitset(MM_MIME8BIT, MimeMode))
99764187506Seric 	{
99864187506Seric 		usrerr("554 Cannot send 8-bit data to 7-bit destination");
99964187506Seric 		rcode = EX_DATAERR;
100064187506Seric 		goto give_up;
100164187506Seric 	}
100264187506Seric 
10039c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
10049c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
10059c9e68d9Seric 	{
10069c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
10079c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
10089c9e68d9Seric 		mci->mci_in = stdin;
10099c9e68d9Seric 		mci->mci_out = stdout;
10109c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
10119c9e68d9Seric 		mci->mci_mailer = m;
10129c9e68d9Seric 	}
10139c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
10149c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
10159c9e68d9Seric 	{
10169c9e68d9Seric #ifdef DAEMON
10179c9e68d9Seric 		register int i;
101864187506Seric 		register u_short port = 0;
10199c9e68d9Seric 
1020a5d44642Seric 		if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
1021a5d44642Seric 		{
1022a5d44642Seric 			syserr("null host name for %s mailer", m->m_mailer);
1023a5d44642Seric 			rcode = EX_CONFIG;
1024a5d44642Seric 			goto give_up;
1025a5d44642Seric 		}
1026a5d44642Seric 
10279c9e68d9Seric 		CurHostName = pv[1];
10289c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
10299c9e68d9Seric 
10309c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
10319c9e68d9Seric 		{
1032a5d44642Seric 			syserr("null host signature for %s", pv[1]);
1033dfebe401Seric 			rcode = EX_CONFIG;
1034b31e7f2bSeric 			goto give_up;
1035b31e7f2bSeric 		}
10369c9e68d9Seric 
10379c9e68d9Seric 		if (!clever)
10389c9e68d9Seric 		{
10399c9e68d9Seric 			syserr("554 non-clever IPC");
1040df106f0bSeric 			rcode = EX_CONFIG;
10419c9e68d9Seric 			goto give_up;
10429c9e68d9Seric 		}
10439c9e68d9Seric 		if (pv[2] != NULL)
10449c9e68d9Seric 			port = atoi(pv[2]);
10459c9e68d9Seric tryhost:
10469c9e68d9Seric 		while (*curhost != '\0')
10479c9e68d9Seric 		{
10489c9e68d9Seric 			register char *p;
1049a524e4d0Seric 			static char hostbuf[MAXNAME + 1];
10509c9e68d9Seric 
10519c9e68d9Seric 			/* pull the next host from the signature */
10529c9e68d9Seric 			p = strchr(curhost, ':');
10539c9e68d9Seric 			if (p == NULL)
10549c9e68d9Seric 				p = &curhost[strlen(curhost)];
1055df106f0bSeric 			if (p == curhost)
1056df106f0bSeric 			{
1057df106f0bSeric 				syserr("deliver: null host name in signature");
10588087428aSeric 				curhost++;
1059df106f0bSeric 				continue;
1060df106f0bSeric 			}
10619c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
10629c9e68d9Seric 			hostbuf[p - curhost] = '\0';
10639c9e68d9Seric 			if (*p != '\0')
10649c9e68d9Seric 				p++;
10659c9e68d9Seric 			curhost = p;
10669c9e68d9Seric 
10679c9e68d9Seric 			/* see if we already know that this host is fried */
10689c9e68d9Seric 			CurHostName = hostbuf;
10699c9e68d9Seric 			mci = mci_get(hostbuf, m);
10709c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
10719c9e68d9Seric 			{
10729c9e68d9Seric 				if (tTd(11, 1))
10739c9e68d9Seric 				{
10749c9e68d9Seric 					printf("openmailer: ");
10754052916eSeric 					mci_dump(mci, FALSE);
10769c9e68d9Seric 				}
10779c9e68d9Seric 				CurHostName = mci->mci_host;
107864187506Seric 				message("Using cached connection to %s via %s...",
107964187506Seric 					hostbuf, m->m_name);
10809c9e68d9Seric 				break;
10819c9e68d9Seric 			}
10829c9e68d9Seric 			mci->mci_mailer = m;
10839c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
10849c9e68d9Seric 				continue;
10859c9e68d9Seric 
10869c9e68d9Seric 			/* try the connection */
10879c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
108864187506Seric 			message("Connecting to %s via %s...",
10899c9e68d9Seric 				hostbuf, m->m_name);
10909c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
10919c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
10929c9e68d9Seric 			mci->mci_exitstat = i;
10939c9e68d9Seric 			mci->mci_errno = errno;
10949d4a8008Seric #if NAMED_BIND
1095f170942cSeric 			mci->mci_herrno = h_errno;
1096f170942cSeric #endif
10979c9e68d9Seric 			if (i == EX_OK)
10989c9e68d9Seric 			{
10999c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
11009c9e68d9Seric 				mci_cache(mci);
1101f170942cSeric 				if (TrafficLogFile != NULL)
1102f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1103f170942cSeric 						getpid(), hostbuf);
11049c9e68d9Seric 				break;
11059c9e68d9Seric 			}
11069c9e68d9Seric 			else if (tTd(11, 1))
11079c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
11089c9e68d9Seric 					i, errno);
11099c9e68d9Seric 
11109c9e68d9Seric 			/* enter status of this host */
11119c9e68d9Seric 			setstat(i);
1112df106f0bSeric 
1113df106f0bSeric 			/* should print some message here for -v mode */
1114df106f0bSeric 		}
1115df106f0bSeric 		if (mci == NULL)
1116df106f0bSeric 		{
1117df106f0bSeric 			syserr("deliver: no host name");
1118df106f0bSeric 			rcode = EX_OSERR;
1119df106f0bSeric 			goto give_up;
11209c9e68d9Seric 		}
11219c9e68d9Seric 		mci->mci_pid = 0;
11229c9e68d9Seric #else /* no DAEMON */
11239c9e68d9Seric 		syserr("554 openmailer: no IPC");
11249c9e68d9Seric 		if (tTd(11, 1))
11259c9e68d9Seric 			printf("openmailer: NULL\n");
1126df106f0bSeric 		rcode = EX_UNAVAILABLE;
1127df106f0bSeric 		goto give_up;
11289c9e68d9Seric #endif /* DAEMON */
11299c9e68d9Seric 	}
11309c9e68d9Seric 	else
11319c9e68d9Seric 	{
113264187506Seric 		/* flush any expired connections */
113364187506Seric 		(void) mci_scan(NULL);
113464187506Seric 
113564187506Seric 		/* announce the connection to verbose listeners */
113664187506Seric 		if (host == NULL || host[0] == '\0')
113764187506Seric 			message("Connecting to %s...", m->m_name);
113864187506Seric 		else
113964187506Seric 			message("Connecting to %s via %s...", host, m->m_name);
1140f170942cSeric 		if (TrafficLogFile != NULL)
11410e3bfef5Seric 		{
1142f170942cSeric 			char **av;
1143f170942cSeric 
1144f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1145f170942cSeric 			for (av = pv; *av != NULL; av++)
1146f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1147f170942cSeric 			fprintf(TrafficLogFile, "\n");
11486fe8c3bcSeric 		}
11496fe8c3bcSeric 
11509c9e68d9Seric 		/* create a pipe to shove the mail through */
11519c9e68d9Seric 		if (pipe(mpvect) < 0)
11529c9e68d9Seric 		{
11530e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
11540e3bfef5Seric 				e->e_to, m->m_name);
11559c9e68d9Seric 			if (tTd(11, 1))
11569c9e68d9Seric 				printf("openmailer: NULL\n");
11579c9e68d9Seric 			rcode = EX_OSERR;
11589c9e68d9Seric 			goto give_up;
11599c9e68d9Seric 		}
11609c9e68d9Seric 
11619c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
11629c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
11639c9e68d9Seric 		{
11640e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
11650e3bfef5Seric 				e->e_to, m->m_name);
11669c9e68d9Seric 			(void) close(mpvect[0]);
11679c9e68d9Seric 			(void) close(mpvect[1]);
11689c9e68d9Seric 			if (tTd(11, 1))
11699c9e68d9Seric 				printf("openmailer: NULL\n");
11709c9e68d9Seric 			rcode = EX_OSERR;
11719c9e68d9Seric 			goto give_up;
11729c9e68d9Seric 		}
11739c9e68d9Seric 
11749c9e68d9Seric 		/*
11759c9e68d9Seric 		**  Actually fork the mailer process.
11769c9e68d9Seric 		**	DOFORK is clever about retrying.
11779c9e68d9Seric 		**
11789c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
11799c9e68d9Seric 		**	around so that endmail will get it.
11809c9e68d9Seric 		*/
11819c9e68d9Seric 
11829c9e68d9Seric 		if (e->e_xfp != NULL)
11839c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
11849c9e68d9Seric 		(void) fflush(stdout);
11859c9e68d9Seric # ifdef SIGCHLD
11862b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
11879c9e68d9Seric # endif /* SIGCHLD */
11889c9e68d9Seric 		DOFORK(FORK);
11899c9e68d9Seric 		/* pid is set by DOFORK */
11909c9e68d9Seric 		if (pid < 0)
11919c9e68d9Seric 		{
11929c9e68d9Seric 			/* failure */
11930e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
11940e3bfef5Seric 				e->e_to, m->m_name);
11959c9e68d9Seric 			(void) close(mpvect[0]);
11969c9e68d9Seric 			(void) close(mpvect[1]);
11979c9e68d9Seric 			if (clever)
11989c9e68d9Seric 			{
11999c9e68d9Seric 				(void) close(rpvect[0]);
12009c9e68d9Seric 				(void) close(rpvect[1]);
12019c9e68d9Seric 			}
12029c9e68d9Seric 			if (tTd(11, 1))
12039c9e68d9Seric 				printf("openmailer: NULL\n");
12049c9e68d9Seric 			rcode = EX_OSERR;
12059c9e68d9Seric 			goto give_up;
12069c9e68d9Seric 		}
12079c9e68d9Seric 		else if (pid == 0)
12089c9e68d9Seric 		{
12099c9e68d9Seric 			int i;
12109c9e68d9Seric 			int saveerrno;
12119c9e68d9Seric 			char **ep;
12129c9e68d9Seric 			char *env[MAXUSERENVIRON];
12139c9e68d9Seric 			extern char **environ;
12149c9e68d9Seric 			extern int DtableSize;
12159c9e68d9Seric 
1216f2e4d5e8Seric 			if (e->e_lockfp != NULL)
1217f2e4d5e8Seric 			{
1218f2e4d5e8Seric 				fclose(e->e_lockfp);
1219f2e4d5e8Seric 				e->e_lockfp = NULL;
1220f2e4d5e8Seric 			}
1221f2e4d5e8Seric 
12229c9e68d9Seric 			/* child -- set up input & exec mailer */
12232b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
12242b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
12252b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
12269c9e68d9Seric 
122744f2317fSeric 			/* reset user and group */
122864187506Seric 			if (bitnset(M_SPECIFIC_UID, m->m_flags))
1229acc57dbfSeric 			{
123064187506Seric 				(void) setgid(m->m_gid);
123164187506Seric 				(void) setuid(m->m_uid);
1232acc57dbfSeric 			}
123364187506Seric 			else if (ctladdr != NULL && ctladdr->q_uid != 0)
123444f2317fSeric 			{
123544f2317fSeric 				(void) initgroups(ctladdr->q_ruser?
123644f2317fSeric 					ctladdr->q_ruser: ctladdr->q_user,
123744f2317fSeric 					ctladdr->q_gid);
123851cfe111Seric 				(void) setgid(ctladdr->q_gid);
123944f2317fSeric 				(void) setuid(ctladdr->q_uid);
124044f2317fSeric 			}
124164187506Seric 			else
124264187506Seric 			{
124364187506Seric 				(void) initgroups(DefUser, DefGid);
124464187506Seric 				if (m->m_gid == 0)
124564187506Seric 					(void) setgid(DefGid);
124664187506Seric 				else
124764187506Seric 					(void) setgid(m->m_gid);
124864187506Seric 				if (m->m_uid == 0)
124964187506Seric 					(void) setuid(DefUid);
125064187506Seric 				else
125164187506Seric 					(void) setuid(m->m_uid);
125244f2317fSeric 			}
125344f2317fSeric 
125444f2317fSeric 			if (tTd(11, 2))
125544f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
125644f2317fSeric 					getuid(), geteuid());
125744f2317fSeric 
1258b986f6aaSeric 			/* move into some "safe" directory */
1259b986f6aaSeric 			if (m->m_execdir != NULL)
1260b986f6aaSeric 			{
1261b986f6aaSeric 				char *p, *q;
1262a524e4d0Seric 				char buf[MAXLINE + 1];
1263b986f6aaSeric 
1264b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1265b986f6aaSeric 				{
1266b986f6aaSeric 					q = strchr(p, ':');
1267b986f6aaSeric 					if (q != NULL)
1268b986f6aaSeric 						*q = '\0';
1269fbf6dabfSeric 					expand(p, buf, sizeof buf, e);
1270b986f6aaSeric 					if (q != NULL)
1271b986f6aaSeric 						*q++ = ':';
1272b986f6aaSeric 					if (tTd(11, 20))
1273b986f6aaSeric 						printf("openmailer: trydir %s\n",
1274b986f6aaSeric 							buf);
1275b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1276b986f6aaSeric 						break;
1277b986f6aaSeric 				}
1278b986f6aaSeric 			}
1279b986f6aaSeric 
12809c9e68d9Seric 			/* arrange to filter std & diag output of command */
12819c9e68d9Seric 			if (clever)
12829c9e68d9Seric 			{
12839c9e68d9Seric 				(void) close(rpvect[0]);
12846fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
12856fe8c3bcSeric 				{
12860e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
12870e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
12886fe8c3bcSeric 					_exit(EX_OSERR);
12896fe8c3bcSeric 				}
12909c9e68d9Seric 				(void) close(rpvect[1]);
12919c9e68d9Seric 			}
12928b40b0a4Seric 			else if (OpMode == MD_SMTP || OpMode == MD_DAEMON ||
12938b40b0a4Seric 				  HoldErrs || DisConnected)
12949c9e68d9Seric 			{
12959c9e68d9Seric 				/* put mailer output in transcript */
12966fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
12976fe8c3bcSeric 				{
12980e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
12990e3bfef5Seric 						e->e_to, m->m_name,
13006fe8c3bcSeric 						fileno(e->e_xfp));
13016fe8c3bcSeric 					_exit(EX_OSERR);
13029c9e68d9Seric 				}
13036fe8c3bcSeric 			}
13046fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
13056fe8c3bcSeric 			{
13060e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
13070e3bfef5Seric 					e->e_to, m->m_name);
13086fe8c3bcSeric 				_exit(EX_OSERR);
13096fe8c3bcSeric 			}
13109c9e68d9Seric 
13119c9e68d9Seric 			/* arrange to get standard input */
13129c9e68d9Seric 			(void) close(mpvect[1]);
13139c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
13149c9e68d9Seric 			{
13150e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
13160e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
13179c9e68d9Seric 				_exit(EX_OSERR);
13189c9e68d9Seric 			}
13199c9e68d9Seric 			(void) close(mpvect[0]);
13209c9e68d9Seric 
13219c9e68d9Seric 			/* arrange for all the files to be closed */
13229c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
13239c9e68d9Seric 			{
13249c9e68d9Seric 				register int j;
132544f2317fSeric 
13269c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
13279c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
13289c9e68d9Seric 			}
13299c9e68d9Seric 
13309f9b003eSeric 			/*
13319f9b003eSeric 			**  Set up the mailer environment
133264187506Seric 			**	_FORCE_MAIL_LOCAL_ is DG-UX equiv of -d flag.
13339f9b003eSeric 			**	TZ is timezone information.
13349f9b003eSeric 			**	SYSTYPE is Apollo software sys type (required).
13359f9b003eSeric 			**	ISP is Apollo hardware system type (required).
13369f9b003eSeric 			*/
13379f9b003eSeric 
13389c9e68d9Seric 			i = 0;
13399c9e68d9Seric 			env[i++] = "AGENT=sendmail";
134064187506Seric 			env[i++] = "_FORCE_MAIL_LOCAL_=yes";
13419c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
13429c9e68d9Seric 			{
13439f9b003eSeric 				if (strncmp(*ep, "TZ=", 3) == 0 ||
13449f9b003eSeric 				    strncmp(*ep, "ISP=", 4) == 0 ||
13459f9b003eSeric 				    strncmp(*ep, "SYSTYPE=", 8) == 0)
13469c9e68d9Seric 					env[i++] = *ep;
13479c9e68d9Seric 			}
134864187506Seric 			env[i] = NULL;
13499c9e68d9Seric 
1350929277f2Seric 			/* run disconnected from terminal */
1351929277f2Seric 			(void) setsid();
1352929277f2Seric 
13539c9e68d9Seric 			/* try to execute the mailer */
13549c9e68d9Seric 			execve(m->m_mailer, pv, env);
13559c9e68d9Seric 			saveerrno = errno;
13569c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
135764187506Seric 			if (bitnset(M_LOCALMAILER, m->m_flags) ||
135864187506Seric 			    transienterror(saveerrno))
13597c941fd2Seric 				_exit(EX_OSERR);
13609c9e68d9Seric 			_exit(EX_UNAVAILABLE);
13619c9e68d9Seric 		}
13629c9e68d9Seric 
13639c9e68d9Seric 		/*
13649c9e68d9Seric 		**  Set up return value.
13659c9e68d9Seric 		*/
13669c9e68d9Seric 
13679c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
13689c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
13699c9e68d9Seric 		mci->mci_mailer = m;
13709c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
13719c9e68d9Seric 		mci->mci_pid = pid;
13729c9e68d9Seric 		(void) close(mpvect[0]);
13739c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1374335eae58Seric 		if (mci->mci_out == NULL)
1375335eae58Seric 		{
1376335eae58Seric 			syserr("deliver: cannot create mailer output channel, fd=%d",
1377335eae58Seric 				mpvect[1]);
1378335eae58Seric 			(void) close(mpvect[1]);
1379335eae58Seric 			if (clever)
1380335eae58Seric 			{
1381335eae58Seric 				(void) close(rpvect[0]);
1382335eae58Seric 				(void) close(rpvect[1]);
1383335eae58Seric 			}
1384335eae58Seric 			rcode = EX_OSERR;
1385335eae58Seric 			goto give_up;
1386335eae58Seric 		}
13879c9e68d9Seric 		if (clever)
13889c9e68d9Seric 		{
13899c9e68d9Seric 			(void) close(rpvect[1]);
13909c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
1391335eae58Seric 			if (mci->mci_in == NULL)
1392335eae58Seric 			{
1393335eae58Seric 				syserr("deliver: cannot create mailer input channel, fd=%d",
1394335eae58Seric 					mpvect[1]);
1395335eae58Seric 				(void) close(rpvect[0]);
1396335eae58Seric 				fclose(mci->mci_out);
1397335eae58Seric 				mci->mci_out = NULL;
1398335eae58Seric 				rcode = EX_OSERR;
1399335eae58Seric 				goto give_up;
1400335eae58Seric 			}
14019c9e68d9Seric 		}
14029c9e68d9Seric 		else
14039c9e68d9Seric 		{
14049c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
14059c9e68d9Seric 			mci->mci_in = NULL;
14069c9e68d9Seric 		}
14079c9e68d9Seric 	}
14089c9e68d9Seric 
14099c9e68d9Seric 	/*
14109c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
14119c9e68d9Seric 	*/
14129c9e68d9Seric 
14139c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
14149c9e68d9Seric 	{
14159c9e68d9Seric 		smtpinit(m, mci, e);
14169c9e68d9Seric 	}
141764187506Seric 
141864187506Seric 	if (bitset(EF_HAS8BIT, e->e_flags) && bitnset(M_7BITS, m->m_flags))
141964187506Seric 		mci->mci_flags |= MCIF_CVT8TO7;
142064187506Seric 	else
142164187506Seric 		mci->mci_flags &= ~MCIF_CVT8TO7;
142264187506Seric 
14239c9e68d9Seric 	if (tTd(11, 1))
14249c9e68d9Seric 	{
14259c9e68d9Seric 		printf("openmailer: ");
14264052916eSeric 		mci_dump(mci, FALSE);
14279c9e68d9Seric 	}
14289c9e68d9Seric 
14299c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1430b31e7f2bSeric 	{
1431b31e7f2bSeric 		/* couldn't open the mailer */
1432b31e7f2bSeric 		rcode = mci->mci_exitstat;
14332a6bc25bSeric 		errno = mci->mci_errno;
14349d4a8008Seric #if NAMED_BIND
1435f170942cSeric 		h_errno = mci->mci_herrno;
1436f170942cSeric #endif
1437b31e7f2bSeric 		if (rcode == EX_OK)
1438b31e7f2bSeric 		{
1439b31e7f2bSeric 			/* shouldn't happen */
144008b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
14416b0f339dSeric 				rcode, mci->mci_state, firstsig);
1442b31e7f2bSeric 			rcode = EX_SOFTWARE;
1443b31e7f2bSeric 		}
1444*43357751Seric 		else if (curhost != NULL && *curhost != '\0')
144590891494Seric 		{
144690891494Seric 			/* try next MX site */
144790891494Seric 			goto tryhost;
144890891494Seric 		}
1449b31e7f2bSeric 	}
1450b31e7f2bSeric 	else if (!clever)
1451b31e7f2bSeric 	{
1452b31e7f2bSeric 		/*
1453b31e7f2bSeric 		**  Format and send message.
1454b31e7f2bSeric 		*/
145515d084d5Seric 
14565aa0f353Seric 		putfromline(mci, e);
145764187506Seric 		(*e->e_puthdr)(mci, e->e_header, e);
14586ffc2dd1Seric 		(*e->e_putbody)(mci, e, NULL);
1459b31e7f2bSeric 
1460b31e7f2bSeric 		/* get the exit status */
1461c9be6216Seric 		rcode = endmailer(mci, e, pv);
1462134746fbSeric 	}
1463134746fbSeric 	else
1464b31e7f2bSeric #ifdef SMTP
1465134746fbSeric 	{
1466b31e7f2bSeric 		/*
1467b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1468b31e7f2bSeric 		*/
146915d084d5Seric 
1470b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1471b31e7f2bSeric 		if (rcode == EX_OK)
147275889e88Seric 		{
1473ded0d3daSkarels 			register char *t = tobuf;
1474ded0d3daSkarels 			register int i;
1475ded0d3daSkarels 
1476588cad61Seric 			/* send the recipient list */
147763780dbdSeric 			tobuf[0] = '\0';
147875889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
147975889e88Seric 			{
148063780dbdSeric 				e->e_to = to->q_paddr;
148115d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
148275889e88Seric 				{
148364187506Seric 					markfailure(e, to, mci, i);
148464187506Seric 					giveresponse(i, m, mci, ctladdr, xstart, e);
148563780dbdSeric 				}
148675889e88Seric 				else
148775889e88Seric 				{
1488911693bfSbostic 					*t++ = ',';
1489b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1490b31e7f2bSeric 						continue;
1491ee39df61Seric 					*t = '\0';
1492588cad61Seric 				}
1493588cad61Seric 			}
1494588cad61Seric 
149563780dbdSeric 			/* now send the data */
149663780dbdSeric 			if (tobuf[0] == '\0')
1497b31e7f2bSeric 			{
14989c9e68d9Seric 				rcode = EX_OK;
149963780dbdSeric 				e->e_to = NULL;
1500b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1501b31e7f2bSeric 					smtprset(m, mci, e);
1502b31e7f2bSeric 			}
150375889e88Seric 			else
150475889e88Seric 			{
150563780dbdSeric 				e->e_to = tobuf + 1;
150675889e88Seric 				rcode = smtpdata(m, mci, e);
150763780dbdSeric 			}
150863780dbdSeric 
150963780dbdSeric 			/* now close the connection */
1510b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
151115d084d5Seric 				smtpquit(m, mci, e);
151263780dbdSeric 		}
1513cb082c5bSeric 		if (rcode != EX_OK && curhost != NULL && *curhost != '\0')
15149c9e68d9Seric 		{
15159c9e68d9Seric 			/* try next MX site */
15169c9e68d9Seric 			goto tryhost;
15179c9e68d9Seric 		}
1518c579ef51Seric 	}
1519b31e7f2bSeric #else /* not SMTP */
1520a05b3449Sbostic 	{
152108b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1522845e533cSeric 		rcode = EX_CONFIG;
1523b31e7f2bSeric 		goto give_up;
1524a05b3449Sbostic 	}
1525b31e7f2bSeric #endif /* SMTP */
15269d4a8008Seric #if NAMED_BIND
15272bcc6d2dSeric 	if (ConfigLevel < 2)
1528912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1529134746fbSeric #endif
15305dfc646bSeric 
1531b31e7f2bSeric 	/* arrange a return receipt if requested */
1532df106f0bSeric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1533df106f0bSeric 	    bitnset(M_LOCALMAILER, m->m_flags))
1534b31e7f2bSeric 	{
1535b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1536b31e7f2bSeric 		/* do we want to send back more info? */
1537b31e7f2bSeric 	}
1538b31e7f2bSeric 
1539c77d1c25Seric 	/*
154063780dbdSeric 	**  Do final status disposal.
154163780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1542c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1543c77d1c25Seric 	**		addressees.
1544c77d1c25Seric 	*/
1545c77d1c25Seric 
1546b31e7f2bSeric   give_up:
154763780dbdSeric 	if (tobuf[0] != '\0')
154864187506Seric 		giveresponse(rcode, m, mci, ctladdr, xstart, e);
1549772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1550b31e7f2bSeric 	{
1551dde5acadSeric 		if (rcode != EX_OK)
155264187506Seric 			markfailure(e, to, mci, rcode);
155364187506Seric 		else if (!bitset(QBADADDR|QQUEUEUP, to->q_flags))
1554655518ecSeric 		{
1555dde5acadSeric 			to->q_flags |= QSENT;
155664187506Seric 			to->q_statdate = curtime();
1557655518ecSeric 			e->e_nsent++;
155864187506Seric 			if (bitnset(M_LOCALMAILER, m->m_flags) &&
155964187506Seric 			    (e->e_receiptto != NULL ||
156064187506Seric 			     bitset(QPINGONSUCCESS, to->q_flags)))
1561df106f0bSeric 			{
156264187506Seric 				to->q_flags |= QREPORT;
1563df106f0bSeric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1564df106f0bSeric 					to->q_paddr);
1565df106f0bSeric 			}
156664187506Seric 			else if (bitset(QPINGONSUCCESS, to->q_flags) &&
156764187506Seric 				 !bitset(MCIF_DSN, mci->mci_flags))
156864187506Seric 			{
156964187506Seric 				to->q_flags |= QRELAYED;
157064187506Seric 				fprintf(e->e_xfp, "%s... relayed; expect no further notifications\n",
157164187506Seric 					to->q_paddr);
157264187506Seric 			}
1573655518ecSeric 		}
1574b31e7f2bSeric 	}
1575b31e7f2bSeric 
1576b31e7f2bSeric 	/*
1577b31e7f2bSeric 	**  Restore state and return.
1578b31e7f2bSeric 	*/
1579c77d1c25Seric 
15807febfd66Seric #ifdef XDEBUG
15817febfd66Seric 	{
15827febfd66Seric 		char wbuf[MAXLINE];
15837febfd66Seric 
15847febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
158544622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
158644622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
158744622ea3Seric 			m->m_name);
15887febfd66Seric 		checkfd012(wbuf);
15897febfd66Seric 	}
15907febfd66Seric #endif
15917febfd66Seric 
159235490626Seric 	errno = 0;
1593588cad61Seric 	define('g', (char *) NULL, e);
15945826d9d3Seric 	return (rcode);
159525a99e2eSeric }
15965dfc646bSeric /*
159783b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
159883b7ddc9Seric **
159983b7ddc9Seric **	Parameters:
160083b7ddc9Seric **		e -- the envelope we are sending.
160183b7ddc9Seric **		q -- the address to mark.
160264187506Seric **		mci -- mailer connection information.
160383b7ddc9Seric **		rcode -- the code signifying the particular failure.
160483b7ddc9Seric **
160583b7ddc9Seric **	Returns:
160683b7ddc9Seric **		none.
160783b7ddc9Seric **
160883b7ddc9Seric **	Side Effects:
160983b7ddc9Seric **		marks the address (and possibly the envelope) with the
161083b7ddc9Seric **			failure so that an error will be returned or
161183b7ddc9Seric **			the message will be queued, as appropriate.
161283b7ddc9Seric */
161383b7ddc9Seric 
161464187506Seric markfailure(e, q, mci, rcode)
161583b7ddc9Seric 	register ENVELOPE *e;
161683b7ddc9Seric 	register ADDRESS *q;
161764187506Seric 	register MCI *mci;
161883b7ddc9Seric 	int rcode;
161983b7ddc9Seric {
162064187506Seric 	char *stat = NULL;
162119c47125Seric 
1622d5aafa3cSeric 	switch (rcode)
1623d5aafa3cSeric 	{
1624d5aafa3cSeric 	  case EX_OK:
1625d5aafa3cSeric 		break;
1626d5aafa3cSeric 
1627d5aafa3cSeric 	  case EX_TEMPFAIL:
1628d5aafa3cSeric 	  case EX_IOERR:
1629d5aafa3cSeric 	  case EX_OSERR:
163083b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1631d5aafa3cSeric 		break;
1632d5aafa3cSeric 
1633d5aafa3cSeric 	  default:
1634f170942cSeric 		q->q_flags |= QBADADDR;
1635d5aafa3cSeric 		break;
1636d5aafa3cSeric 	}
163764187506Seric 
163864187506Seric 	if (q->q_status == NULL && mci != NULL)
163964187506Seric 		q->q_status = mci->mci_status;
164064187506Seric 	switch (rcode)
164164187506Seric 	{
164264187506Seric 	  case EX_USAGE:
164364187506Seric 		stat = "5.5.4";
164464187506Seric 		break;
164564187506Seric 
164664187506Seric 	  case EX_DATAERR:
164764187506Seric 		stat = "5.5.2";
164864187506Seric 		break;
164964187506Seric 
165064187506Seric 	  case EX_NOUSER:
165164187506Seric 	  case EX_NOHOST:
165264187506Seric 		stat = "5.1.1";
165364187506Seric 		break;
165464187506Seric 
165564187506Seric 	  case EX_NOINPUT:
165664187506Seric 	  case EX_CANTCREAT:
165764187506Seric 	  case EX_NOPERM:
165864187506Seric 		stat = "5.3.0";
165964187506Seric 		break;
166064187506Seric 
166164187506Seric 	  case EX_UNAVAILABLE:
166264187506Seric 	  case EX_SOFTWARE:
166364187506Seric 	  case EX_OSFILE:
166464187506Seric 	  case EX_PROTOCOL:
166564187506Seric 	  case EX_CONFIG:
166664187506Seric 		stat = "5.5.0";
166764187506Seric 		break;
166864187506Seric 
166964187506Seric 	  case EX_OSERR:
167064187506Seric 	  case EX_IOERR:
167164187506Seric 		stat = "4.5.0";
167264187506Seric 		break;
167364187506Seric 
167464187506Seric 	  case EX_TEMPFAIL:
167564187506Seric 		stat = "4.2.0";
167664187506Seric 		break;
167764187506Seric 	}
167864187506Seric 	if (stat != NULL && q->q_status == NULL)
167964187506Seric 		q->q_status = stat;
168064187506Seric 
168164187506Seric 	q->q_statdate = curtime();
168264187506Seric 	if (CurHostName != NULL && CurHostName[0] != '\0')
168364187506Seric 		q->q_statmta = newstr(CurHostName);
168464187506Seric 	if (rcode != EX_OK && q->q_rstatus == NULL)
168564187506Seric 	{
168664187506Seric 		char buf[30];
168764187506Seric 
168864187506Seric 		(void) sprintf(buf, "%d", rcode);
168964187506Seric 		q->q_rstatus = newstr(buf);
169064187506Seric 	}
169183b7ddc9Seric }
169283b7ddc9Seric /*
1693c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1694c579ef51Seric **
1695c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1696c579ef51Seric **	violation), so we report those specially.  For other
1697c579ef51Seric **	errors, we choose a status message (into statmsg),
1698c579ef51Seric **	and if it represents an error, we print it.
1699c579ef51Seric **
1700c579ef51Seric **	Parameters:
1701c579ef51Seric **		pid -- pid of mailer.
1702c9be6216Seric **		e -- the current envelope.
1703c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1704c9be6216Seric **			(for error messages).
1705c579ef51Seric **
1706c579ef51Seric **	Returns:
1707c579ef51Seric **		exit code of mailer.
1708c579ef51Seric **
1709c579ef51Seric **	Side Effects:
1710c579ef51Seric **		none.
1711c579ef51Seric */
1712c579ef51Seric 
1713c9be6216Seric endmailer(mci, e, pv)
1714b31e7f2bSeric 	register MCI *mci;
1715c9be6216Seric 	register ENVELOPE *e;
1716c9be6216Seric 	char **pv;
1717c579ef51Seric {
1718588cad61Seric 	int st;
1719c579ef51Seric 
172075889e88Seric 	/* close any connections */
172175889e88Seric 	if (mci->mci_in != NULL)
172276f3d53fSeric 		(void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in");
172375889e88Seric 	if (mci->mci_out != NULL)
172476f3d53fSeric 		(void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out");
172575889e88Seric 	mci->mci_in = mci->mci_out = NULL;
172675889e88Seric 	mci->mci_state = MCIS_CLOSED;
172775889e88Seric 
172833db8731Seric 	/* in the IPC case there is nothing to wait for */
172975889e88Seric 	if (mci->mci_pid == 0)
173033db8731Seric 		return (EX_OK);
173133db8731Seric 
173233db8731Seric 	/* wait for the mailer process to die and collect status */
173375889e88Seric 	st = waitfor(mci->mci_pid);
1734588cad61Seric 	if (st == -1)
173578de67c1Seric 	{
1736c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1737588cad61Seric 		return (EX_SOFTWARE);
1738c579ef51Seric 	}
173933db8731Seric 
1740bf9bc890Seric 	if (WIFEXITED(st))
1741c579ef51Seric 	{
1742bf9bc890Seric 		/* normal death -- return status */
1743bf9bc890Seric 		return (WEXITSTATUS(st));
1744bf9bc890Seric 	}
1745bf9bc890Seric 
1746bf9bc890Seric 	/* it died a horrid death */
1747550092c3Seric 	syserr("451 mailer %s died with signal %o",
1748550092c3Seric 		mci->mci_mailer->m_name, st);
1749c9be6216Seric 
1750c9be6216Seric 	/* log the arguments */
17513c930db3Seric 	if (pv != NULL && e->e_xfp != NULL)
1752c9be6216Seric 	{
1753c9be6216Seric 		register char **av;
1754c9be6216Seric 
1755c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1756c9be6216Seric 		for (av = pv; *av != NULL; av++)
1757c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1758c9be6216Seric 		fprintf(e->e_xfp, "\n");
1759c9be6216Seric 	}
1760c9be6216Seric 
17615f73204aSeric 	ExitStat = EX_TEMPFAIL;
17625f73204aSeric 	return (EX_TEMPFAIL);
1763c579ef51Seric }
1764c579ef51Seric /*
176525a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
176625a99e2eSeric **
176725a99e2eSeric **	Parameters:
176825a99e2eSeric **		stat -- the status code from the mailer (high byte
176925a99e2eSeric **			only; core dumps must have been taken care of
177025a99e2eSeric **			already).
177181161401Seric **		m -- the mailer info for this mailer.
177281161401Seric **		mci -- the mailer connection info -- can be NULL if the
177381161401Seric **			response is given before the connection is made.
17744dee0003Seric **		ctladdr -- the controlling address for the recipient
17754dee0003Seric **			address(es).
177664187506Seric **		xstart -- the transaction start time, for computing
177764187506Seric **			transaction delays.
177881161401Seric **		e -- the current envelope.
177925a99e2eSeric **
178025a99e2eSeric **	Returns:
1781db8841e9Seric **		none.
178225a99e2eSeric **
178325a99e2eSeric **	Side Effects:
1784c1f9df2cSeric **		Errors may be incremented.
178525a99e2eSeric **		ExitStat may be set.
178625a99e2eSeric */
178725a99e2eSeric 
178864187506Seric giveresponse(stat, m, mci, ctladdr, xstart, e)
178925a99e2eSeric 	int stat;
1790588cad61Seric 	register MAILER *m;
179181161401Seric 	register MCI *mci;
17924dee0003Seric 	ADDRESS *ctladdr;
179364187506Seric 	time_t xstart;
1794198d9be0Seric 	ENVELOPE *e;
179525a99e2eSeric {
17969c16475dSeric 	register const char *statmsg;
179725a99e2eSeric 	extern char *SysExMsg[];
179825a99e2eSeric 	register int i;
1799d4bd8f0eSbostic 	extern int N_SysEx;
1800198d9be0Seric 	char buf[MAXLINE];
180125a99e2eSeric 
180213bbc08cSeric 	/*
180313bbc08cSeric 	**  Compute status message from code.
180413bbc08cSeric 	*/
180513bbc08cSeric 
180625a99e2eSeric 	i = stat - EX__BASE;
1807588cad61Seric 	if (stat == 0)
18086fe8c3bcSeric 	{
1809588cad61Seric 		statmsg = "250 Sent";
1810ce5531bdSeric 		if (e->e_statmsg != NULL)
18116fe8c3bcSeric 		{
1812ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
18136fe8c3bcSeric 			statmsg = buf;
18146fe8c3bcSeric 		}
18156fe8c3bcSeric 	}
1816588cad61Seric 	else if (i < 0 || i > N_SysEx)
1817588cad61Seric 	{
1818588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1819588cad61Seric 		stat = EX_UNAVAILABLE;
1820588cad61Seric 		statmsg = buf;
1821588cad61Seric 	}
1822198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1823198d9be0Seric 	{
18247d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
18259d4a8008Seric #if NAMED_BIND
1826f28da541Smiriam 		if (h_errno == TRY_AGAIN)
18274d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1828f28da541Smiriam 		else
1829d4bd8f0eSbostic #endif
1830f28da541Smiriam 		{
18318557d168Seric 			if (errno != 0)
1832d87e85f3Seric 				statmsg = errstring(errno);
1833d87e85f3Seric 			else
1834d87e85f3Seric 			{
1835d87e85f3Seric #ifdef SMTP
1836d87e85f3Seric 				statmsg = SmtpError;
18376c2c3107Seric #else /* SMTP */
1838d87e85f3Seric 				statmsg = NULL;
18396c2c3107Seric #endif /* SMTP */
1840d87e85f3Seric 			}
1841f28da541Smiriam 		}
1842d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1843d87e85f3Seric 		{
184487c9b3e7Seric 			(void) strcat(buf, ": ");
1845d87e85f3Seric 			(void) strcat(buf, statmsg);
18468557d168Seric 		}
1847198d9be0Seric 		statmsg = buf;
1848198d9be0Seric 	}
18499d4a8008Seric #if NAMED_BIND
1850f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1851f170942cSeric 	{
18524d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1853862934b2Seric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i] + 1, statmsg);
1854f170942cSeric 		statmsg = buf;
1855f170942cSeric 	}
1856f170942cSeric #endif
185725a99e2eSeric 	else
1858d87e85f3Seric 	{
185925a99e2eSeric 		statmsg = SysExMsg[i];
18607d55540cSeric 		if (*statmsg++ == ':')
18617d55540cSeric 		{
18627d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
18637d55540cSeric 			statmsg = buf;
18647d55540cSeric 		}
1865d87e85f3Seric 	}
1866588cad61Seric 
1867588cad61Seric 	/*
1868588cad61Seric 	**  Print the message as appropriate
1869588cad61Seric 	*/
1870588cad61Seric 
1871198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1872df106f0bSeric 	{
1873df106f0bSeric 		extern char MsgBuf[];
1874df106f0bSeric 
18751f96bd0bSeric 		message("%s", &statmsg[4]);
1876df106f0bSeric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1877df106f0bSeric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1878df106f0bSeric 	}
187925a99e2eSeric 	else
188025a99e2eSeric 	{
1881862934b2Seric 		char mbuf[8];
1882862934b2Seric 
1883c1f9df2cSeric 		Errors++;
1884862934b2Seric 		sprintf(mbuf, "%.3s %%s", statmsg);
1885862934b2Seric 		usrerr(mbuf, &statmsg[4]);
188625a99e2eSeric 	}
188725a99e2eSeric 
188825a99e2eSeric 	/*
188925a99e2eSeric 	**  Final cleanup.
189025a99e2eSeric 	**	Log a record of the transaction.  Compute the new
189125a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
189225a99e2eSeric 	**	that.
189325a99e2eSeric 	*/
189425a99e2eSeric 
18952f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
189664187506Seric 		logdelivery(m, mci, &statmsg[4], ctladdr, xstart, e);
1897eb238f8cSeric 
18985d3f0ed4Seric 	if (tTd(11, 2))
18995d3f0ed4Seric 		printf("giveresponse: stat=%d, e->e_message=%s\n",
19002c8ac7e0Seric 			stat, e->e_message == NULL ? "<NULL>" : e->e_message);
19015d3f0ed4Seric 
1902eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1903eb238f8cSeric 		setstat(stat);
19041097d4c9Seric 	if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL))
1905198d9be0Seric 	{
1906198d9be0Seric 		if (e->e_message != NULL)
1907198d9be0Seric 			free(e->e_message);
1908198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1909198d9be0Seric 	}
19108557d168Seric 	errno = 0;
19119d4a8008Seric #if NAMED_BIND
1912f28da541Smiriam 	h_errno = 0;
1913d4bd8f0eSbostic #endif
1914eb238f8cSeric }
1915eb238f8cSeric /*
1916eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1917eb238f8cSeric **
19182c9b4f99Seric **	Care is taken to avoid logging lines that are too long, because
19192c9b4f99Seric **	some versions of syslog have an unfortunate proclivity for core
19202c9b4f99Seric **	dumping.  This is a hack, to be sure, that is at best empirical.
19212c9b4f99Seric **
1922eb238f8cSeric **	Parameters:
192381161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
192481161401Seric **		mci -- the mailer connection info -- can be NULL if the
192581161401Seric **			log is occuring when no connection is active.
192681161401Seric **		stat -- the message to print for the status.
19274dee0003Seric **		ctladdr -- the controlling address for the to list.
192864187506Seric **		xstart -- the transaction start time, used for
192964187506Seric **			computing transaction delay.
193081161401Seric **		e -- the current envelope.
1931eb238f8cSeric **
1932eb238f8cSeric **	Returns:
1933eb238f8cSeric **		none
1934eb238f8cSeric **
1935eb238f8cSeric **	Side Effects:
1936eb238f8cSeric **		none
1937eb238f8cSeric */
1938eb238f8cSeric 
193964187506Seric logdelivery(m, mci, stat, ctladdr, xstart, e)
194081161401Seric 	MAILER *m;
194181161401Seric 	register MCI *mci;
1942eb238f8cSeric 	char *stat;
19434dee0003Seric 	ADDRESS *ctladdr;
194464187506Seric 	time_t xstart;
1945b31e7f2bSeric 	register ENVELOPE *e;
19465cf56be3Seric {
1947eb238f8cSeric # ifdef LOG
19484dee0003Seric 	register char *bp;
19492c9b4f99Seric 	register char *p;
19502c9b4f99Seric 	int l;
1951d6acf3eeSeric 	char buf[512];
19529507d1f9Seric 
19533a100e8bSeric #  if (SYSLOG_BUFSIZE) >= 256
19544dee0003Seric 	bp = buf;
19554dee0003Seric 	if (ctladdr != NULL)
19564dee0003Seric 	{
19574dee0003Seric 		strcpy(bp, ", ctladdr=");
1958f62f08c7Seric 		strcat(bp, shortenstring(ctladdr->q_paddr, 83));
19594dee0003Seric 		bp += strlen(bp);
19604dee0003Seric 		if (bitset(QGOODUID, ctladdr->q_flags))
19614dee0003Seric 		{
19624dee0003Seric 			(void) sprintf(bp, " (%d/%d)",
19634dee0003Seric 					ctladdr->q_uid, ctladdr->q_gid);
19644dee0003Seric 			bp += strlen(bp);
19654dee0003Seric 		}
19664dee0003Seric 	}
19674dee0003Seric 
196864187506Seric 	sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
19694dee0003Seric 	bp += strlen(bp);
197081161401Seric 
197164187506Seric 	if (xstart != (time_t) 0)
197264187506Seric 	{
197364187506Seric 		sprintf(bp, ", xdelay=%s", pintvl(curtime() - xstart, TRUE));
197464187506Seric 		bp += strlen(bp);
197564187506Seric 	}
197664187506Seric 
197748ed5d33Seric 	if (m != NULL)
197871ff6caaSeric 	{
19794dee0003Seric 		(void) strcpy(bp, ", mailer=");
19804dee0003Seric 		(void) strcat(bp, m->m_name);
19814dee0003Seric 		bp += strlen(bp);
198271ff6caaSeric 	}
198348ed5d33Seric 
198448ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
198571ff6caaSeric 	{
198671ff6caaSeric # ifdef DAEMON
1987e2f2f828Seric 		extern SOCKADDR CurHostAddr;
198848ed5d33Seric # endif
198971ff6caaSeric 
19904dee0003Seric 		(void) strcpy(bp, ", relay=");
19914dee0003Seric 		(void) strcat(bp, mci->mci_host);
199248ed5d33Seric 
199348ed5d33Seric # ifdef DAEMON
19942bdfc6b9Seric 		(void) strcat(bp, " [");
19954dee0003Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
19962bdfc6b9Seric 		(void) strcat(bp, "]");
199771ff6caaSeric # endif
199871ff6caaSeric 	}
1999bcb9b028Seric 	else if (strcmp(stat, "queued") != 0)
200048ed5d33Seric 	{
200148ed5d33Seric 		char *p = macvalue('h', e);
20029507d1f9Seric 
200348ed5d33Seric 		if (p != NULL && p[0] != '\0')
200448ed5d33Seric 		{
20054dee0003Seric 			(void) strcpy(bp, ", relay=");
20064dee0003Seric 			(void) strcat(bp, p);
200748ed5d33Seric 		}
200848ed5d33Seric 	}
2009cb082c5bSeric 	bp += strlen(bp);
2010d6acf3eeSeric 
20113a100e8bSeric #define STATLEN		(((SYSLOG_BUFSIZE) - 100) / 4)
20123a100e8bSeric #if (STATLEN) < 63
20133a100e8bSeric # undef STATLEN
20143a100e8bSeric # define STATLEN	63
20153a100e8bSeric #endif
20163a100e8bSeric #if (STATLEN) > 203
20173a100e8bSeric # undef STATLEN
20183a100e8bSeric # define STATLEN	203
20193a100e8bSeric #endif
20203a100e8bSeric 
20213a100e8bSeric 	if ((bp - buf) > (sizeof buf - ((STATLEN) + 20)))
20222c9b4f99Seric 	{
20232c9b4f99Seric 		/* desperation move -- truncate data */
20243a100e8bSeric 		bp = buf + sizeof buf - ((STATLEN) + 17);
20252c9b4f99Seric 		strcpy(bp, "...");
20262c9b4f99Seric 		bp += 3;
20272c9b4f99Seric 	}
20282c9b4f99Seric 
20292c9b4f99Seric 	(void) strcpy(bp, ", stat=");
20302c9b4f99Seric 	bp += strlen(bp);
20313a100e8bSeric 
20323a100e8bSeric 	(void) strcpy(bp, shortenstring(stat, (STATLEN)));
20332c9b4f99Seric 
20342c9b4f99Seric 	l = SYSLOG_BUFSIZE - 100 - strlen(buf);
20352c9b4f99Seric 	p = e->e_to;
20362c9b4f99Seric 	while (strlen(p) >= l)
20372c9b4f99Seric 	{
20382c9b4f99Seric 		register char *q = strchr(p + l, ',');
20392c9b4f99Seric 
20402a1b6b73Seric 		if (q == NULL)
20412c9b4f99Seric 			break;
20422c9b4f99Seric 		syslog(LOG_INFO, "%s: to=%.*s [more]%s",
20432c9b4f99Seric 			e->e_id, ++q - p, p, buf);
20442c9b4f99Seric 		p = q;
20452c9b4f99Seric 	}
20462c9b4f99Seric 	syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf);
20473a100e8bSeric 
20483a100e8bSeric #  else		/* we have a very short log buffer size */
20493a100e8bSeric 
205027607809Seric 	l = SYSLOG_BUFSIZE - 85;
20513a100e8bSeric 	p = e->e_to;
20523a100e8bSeric 	while (strlen(p) >= l)
20533a100e8bSeric 	{
20543a100e8bSeric 		register char *q = strchr(p + l, ',');
20553a100e8bSeric 
20563a100e8bSeric 		if (q == NULL)
20573a100e8bSeric 			break;
20583a100e8bSeric 		syslog(LOG_INFO, "%s: to=%.*s [more]",
20593a100e8bSeric 			e->e_id, ++q - p, p);
20603a100e8bSeric 		p = q;
20613a100e8bSeric 	}
20623a100e8bSeric 	syslog(LOG_INFO, "%s: to=%s", e->e_id, p);
20633a100e8bSeric 
20643a100e8bSeric 	if (ctladdr != NULL)
20653a100e8bSeric 	{
20663a100e8bSeric 		bp = buf;
20673a100e8bSeric 		strcpy(buf, "ctladdr=");
20683a100e8bSeric 		bp += strlen(buf);
20693a100e8bSeric 		strcpy(bp, shortenstring(ctladdr->q_paddr, 83));
20703a100e8bSeric 		bp += strlen(buf);
20713a100e8bSeric 		if (bitset(QGOODUID, ctladdr->q_flags))
20723a100e8bSeric 		{
20733a100e8bSeric 			(void) sprintf(bp, " (%d/%d)",
20743a100e8bSeric 					ctladdr->q_uid, ctladdr->q_gid);
20753a100e8bSeric 			bp += strlen(bp);
20763a100e8bSeric 		}
20773a100e8bSeric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
20783a100e8bSeric 	}
20794e797715Seric 	bp = buf;
20804e797715Seric 	sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
20814e797715Seric 	bp += strlen(bp);
208264187506Seric 	if (xstart != (time_t) 0)
208364187506Seric 	{
208464187506Seric 		sprintf(bp, ", xdelay=%s", pintvl(curtime() - xstart, TRUE));
208564187506Seric 		bp += strlen(bp);
208664187506Seric 	}
20873a100e8bSeric 
20883a100e8bSeric 	if (m != NULL)
20894e797715Seric 	{
20904e797715Seric 		sprintf(bp, ", mailer=%s", m->m_name);
20914e797715Seric 		bp += strlen(bp);
20924e797715Seric 	}
2093b056abd0Seric 	syslog(LOG_INFO, "%s: %s", e->e_id, buf);
20943a100e8bSeric 
2095b056abd0Seric 	buf[0] = '\0';
20963a100e8bSeric 	if (mci != NULL && mci->mci_host != NULL)
20973a100e8bSeric 	{
20983a100e8bSeric # ifdef DAEMON
20993a100e8bSeric 		extern SOCKADDR CurHostAddr;
21003a100e8bSeric # endif
21013a100e8bSeric 
2102d2ece200Seric 		sprintf(buf, "relay=%s", mci->mci_host);
21033a100e8bSeric 
21043a100e8bSeric # ifdef DAEMON
2105b056abd0Seric 		(void) strcat(buf, " [");
2106b056abd0Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
2107b056abd0Seric 		(void) strcat(buf, "]");
21083a100e8bSeric # endif
21093a100e8bSeric 	}
2110bcb9b028Seric 	else if (strcmp(stat, "queued") != 0)
21113a100e8bSeric 	{
21123a100e8bSeric 		char *p = macvalue('h', e);
21133a100e8bSeric 
21143a100e8bSeric 		if (p != NULL && p[0] != '\0')
2115d2ece200Seric 			sprintf(buf, "relay=%s", p);
21163a100e8bSeric 	}
2117b056abd0Seric 	if (buf[0] != '\0')
21184e797715Seric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
21193a100e8bSeric 
21203a100e8bSeric 	syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63));
21213a100e8bSeric #  endif /* short log buffer */
21226c2c3107Seric # endif /* LOG */
212325a99e2eSeric }
212425a99e2eSeric /*
212551552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
212625a99e2eSeric **
212751552439Seric **	This can be made an arbitrary message separator by changing $l
212851552439Seric **
21299b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
21309b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
21319b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
21329b6c17a6Seric **	this kind of antique garbage????
213325a99e2eSeric **
213425a99e2eSeric **	Parameters:
21355aa0f353Seric **		mci -- the connection information.
21365aa0f353Seric **		e -- the envelope.
213725a99e2eSeric **
213825a99e2eSeric **	Returns:
213951552439Seric **		none
214025a99e2eSeric **
214125a99e2eSeric **	Side Effects:
214251552439Seric **		outputs some text to fp.
214325a99e2eSeric */
214425a99e2eSeric 
21455aa0f353Seric putfromline(mci, e)
21465aa0f353Seric 	register MCI *mci;
2147b31e7f2bSeric 	ENVELOPE *e;
214825a99e2eSeric {
21492bc47524Seric 	char *template = "\201l\n";
215051552439Seric 	char buf[MAXLINE];
215125a99e2eSeric 
21525aa0f353Seric 	if (bitnset(M_NHDR, mci->mci_mailer->m_flags))
215351552439Seric 		return;
215413bbc08cSeric 
21552c7e1b8dSeric # ifdef UGLYUUCP
21565aa0f353Seric 	if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags))
215774b6e67bSeric 	{
2158ea09d6edSeric 		char *bang;
2159ea09d6edSeric 		char xbuf[MAXLINE];
216074b6e67bSeric 
2161fbf6dabfSeric 		expand("\201g", buf, sizeof buf, e);
21626c2c3107Seric 		bang = strchr(buf, '!');
216374b6e67bSeric 		if (bang == NULL)
216434fcca25Seric 		{
216534fcca25Seric 			errno = 0;
216634fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
216734fcca25Seric 		}
216874b6e67bSeric 		else
2169588cad61Seric 		{
2170ea09d6edSeric 			*bang++ = '\0';
21712bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
2172ea09d6edSeric 			template = xbuf;
217374b6e67bSeric 		}
2174588cad61Seric 	}
21756c2c3107Seric # endif /* UGLYUUCP */
2176fbf6dabfSeric 	expand(template, buf, sizeof buf, e);
217764187506Seric 	putxline(buf, mci, FALSE);
2178bc6e2962Seric }
2179bc6e2962Seric /*
218051552439Seric **  PUTBODY -- put the body of a message.
218151552439Seric **
218251552439Seric **	Parameters:
21835aa0f353Seric **		mci -- the connection information.
21849a6a5f55Seric **		e -- the envelope to put out.
218503c02fdeSeric **		separator -- if non-NULL, a message separator that must
218603c02fdeSeric **			not be permitted in the resulting message.
218751552439Seric **
218851552439Seric **	Returns:
218951552439Seric **		none.
219051552439Seric **
219151552439Seric **	Side Effects:
219251552439Seric **		The message is written onto fp.
219351552439Seric */
219451552439Seric 
219564187506Seric /* values for output state variable */
219664187506Seric #define OS_HEAD		0	/* at beginning of line */
219764187506Seric #define OS_CR		1	/* read a carriage return */
219864187506Seric #define OS_INLINE	2	/* putting rest of line */
219964187506Seric 
22006ffc2dd1Seric putbody(mci, e, separator)
22015aa0f353Seric 	register MCI *mci;
22029a6a5f55Seric 	register ENVELOPE *e;
220303c02fdeSeric 	char *separator;
220451552439Seric {
220577b52738Seric 	char buf[MAXLINE];
220651552439Seric 
220751552439Seric 	/*
220851552439Seric 	**  Output the body of the message
220951552439Seric 	*/
221051552439Seric 
221164187506Seric 	if (e->e_dfp == NULL && e->e_df != NULL)
22129a6a5f55Seric 	{
22139a6a5f55Seric 		e->e_dfp = fopen(e->e_df, "r");
22149a6a5f55Seric 		if (e->e_dfp == NULL)
22158f9146b0Srick 			syserr("putbody: Cannot open %s for %s from %s",
2216e76a6a8fSeric 			e->e_df, e->e_to, e->e_from.q_paddr);
22179a6a5f55Seric 	}
221864187506Seric 	if (e->e_dfp == NULL)
221964187506Seric 	{
222064187506Seric 		if (bitset(MCIF_INHEADER, mci->mci_flags))
222164187506Seric 		{
222264187506Seric 			putline("", mci);
222364187506Seric 			mci->mci_flags &= ~MCIF_INHEADER;
2224ff33b231Seric 		}
222564187506Seric 		putline("<<< No Message Collected >>>", mci);
222664187506Seric 		goto endofmessage;
222764187506Seric 	}
222864187506Seric 	if (e->e_dfino == (ino_t) 0)
2229519e7d80Seric 	{
223064187506Seric 		struct stat stbuf;
223164187506Seric 
223264187506Seric 		if (fstat(fileno(e->e_dfp), &stbuf) < 0)
223364187506Seric 			e->e_dfino = -1;
223464187506Seric 		else
223564187506Seric 		{
223664187506Seric 			e->e_dfdev = stbuf.st_dev;
223764187506Seric 			e->e_dfino = stbuf.st_ino;
223864187506Seric 		}
223964187506Seric 	}
22409a6a5f55Seric 	rewind(e->e_dfp);
224164187506Seric 
224264187506Seric 	if (bitset(MCIF_CVT8TO7, mci->mci_flags))
2243a6ce3008Seric 	{
22440ea5f9e4Seric 		char *boundaries[MAXMIMENESTING + 1];
22450ea5f9e4Seric 
224664187506Seric 		/*
224764187506Seric 		**  Do 8 to 7 bit MIME conversion.
224864187506Seric 		*/
224964187506Seric 
225064187506Seric 		/* make sure it looks like a MIME message */
225164187506Seric 		if (hvalue("MIME-Version", e->e_header) == NULL)
225264187506Seric 			putline("MIME-Version: 1.0", mci);
225364187506Seric 
225464187506Seric 		if (hvalue("Content-Type", e->e_header) == NULL)
225564187506Seric 		{
225664187506Seric 			sprintf(buf, "Content-Type: text/plain; charset=%s",
225764187506Seric 				defcharset(e));
225864187506Seric 			putline(buf, mci);
225964187506Seric 		}
226064187506Seric 
226164187506Seric 		/* now do the hard work */
22620ea5f9e4Seric 		boundaries[0] = NULL;
22630ea5f9e4Seric 		mime8to7(mci, e->e_header, e, boundaries, M87F_OUTER);
226464187506Seric 	}
226564187506Seric 	else
226664187506Seric 	{
226764187506Seric 		int ostate;
226864187506Seric 		register char *bp;
226964187506Seric 		register char *pbp;
227064187506Seric 		register int c;
227164187506Seric 		int padc;
227264187506Seric 		char *buflim;
227364187506Seric 		int pos = 0;
227464187506Seric 		char peekbuf[10];
227564187506Seric 
227664187506Seric 		/* we can pass it through unmodified */
227764187506Seric 		if (bitset(MCIF_INHEADER, mci->mci_flags))
227864187506Seric 		{
227964187506Seric 			putline("", mci);
228064187506Seric 			mci->mci_flags &= ~MCIF_INHEADER;
228164187506Seric 		}
228264187506Seric 
228364187506Seric 		/* determine end of buffer; allow for short mailer lines */
228464187506Seric 		buflim = &buf[sizeof buf - 1];
228564187506Seric 		if (mci->mci_mailer->m_linelimit > 0 &&
228664187506Seric 		    mci->mci_mailer->m_linelimit < sizeof buf - 1)
228764187506Seric 			buflim = &buf[mci->mci_mailer->m_linelimit - 1];
228864187506Seric 
228964187506Seric 		/* copy temp file to output with mapping */
229064187506Seric 		ostate = OS_HEAD;
229164187506Seric 		bp = buf;
229264187506Seric 		pbp = peekbuf;
229364187506Seric 		while (!ferror(mci->mci_out))
229464187506Seric 		{
229564187506Seric 			register char *xp;
229664187506Seric 
229764187506Seric 			if (pbp > peekbuf)
229864187506Seric 				c = *--pbp;
229964187506Seric 			else if ((c = fgetc(e->e_dfp)) == EOF)
230064187506Seric 				break;
230164187506Seric 			if (bitset(MCIF_7BIT, mci->mci_flags))
230264187506Seric 				c &= 0x7f;
230364187506Seric 			switch (ostate)
230464187506Seric 			{
230564187506Seric 			  case OS_HEAD:
230664187506Seric 				if (c != '\r' && c != '\n' && bp < buflim)
230764187506Seric 				{
230864187506Seric 					*bp++ = c;
230964187506Seric 					break;
231064187506Seric 				}
231164187506Seric 
231264187506Seric 				/* check beginning of line for special cases */
231364187506Seric 				*bp = '\0';
231464187506Seric 				pos = 0;
231564187506Seric 				padc = EOF;
23165aa0f353Seric 				if (buf[0] == 'F' &&
23175aa0f353Seric 				    bitnset(M_ESCFROM, mci->mci_mailer->m_flags) &&
2318d6fa2b58Sbostic 				    strncmp(buf, "From ", 5) == 0)
231964187506Seric 				{
232064187506Seric 					padc = '>';
232164187506Seric 				}
232264187506Seric 				if (buf[0] == '-' && buf[1] == '-' &&
232364187506Seric 				    separator != NULL)
232403c02fdeSeric 				{
232503c02fdeSeric 					/* possible separator */
232603c02fdeSeric 					int sl = strlen(separator);
232703c02fdeSeric 
232803c02fdeSeric 					if (strncmp(&buf[2], separator, sl) == 0)
232964187506Seric 						padc = ' ';
233003c02fdeSeric 				}
233164187506Seric 				if (buf[0] == '.' &&
233264187506Seric 				    bitnset(M_XDOT, mci->mci_mailer->m_flags))
233364187506Seric 				{
233464187506Seric 					padc = '.';
233564187506Seric 				}
233664187506Seric 
233764187506Seric 				/* now copy out saved line */
233864187506Seric 				if (TrafficLogFile != NULL)
233964187506Seric 				{
234064187506Seric 					fprintf(TrafficLogFile, "%05d >>> ", getpid());
234164187506Seric 					if (padc != EOF)
234264187506Seric 						fputc(padc, TrafficLogFile);
234364187506Seric 					for (xp = buf; xp < bp; xp++)
234464187506Seric 						fputc(*xp, TrafficLogFile);
234564187506Seric 					if (c == '\n')
234664187506Seric 						fputs(mci->mci_mailer->m_eol,
234764187506Seric 						      TrafficLogFile);
234864187506Seric 				}
234964187506Seric 				if (padc != EOF)
235064187506Seric 				{
235164187506Seric 					fputc(padc, mci->mci_out);
235264187506Seric 					pos++;
235364187506Seric 				}
235464187506Seric 				for (xp = buf; xp < bp; xp++)
235564187506Seric 					fputc(*xp, mci->mci_out);
235664187506Seric 				if (c == '\n')
235764187506Seric 				{
235864187506Seric 					fputs(mci->mci_mailer->m_eol,
235964187506Seric 					      mci->mci_out);
236064187506Seric 					pos = 0;
236164187506Seric 				}
236264187506Seric 				else
236364187506Seric 				{
236464187506Seric 					pos += bp - buf;
236564187506Seric 					if (c != '\r')
236664187506Seric 						*pbp++ = c;
236764187506Seric 				}
236864187506Seric 				bp = buf;
236964187506Seric 
237064187506Seric 				/* determine next state */
237164187506Seric 				if (c == '\n')
237264187506Seric 					ostate = OS_HEAD;
237364187506Seric 				else if (c == '\r')
237464187506Seric 					ostate = OS_CR;
237564187506Seric 				else
237664187506Seric 					ostate = OS_INLINE;
237764187506Seric 				continue;
237864187506Seric 
237964187506Seric 			  case OS_CR:
238064187506Seric 				if (c == '\n')
238164187506Seric 				{
238264187506Seric 					/* got CRLF */
238364187506Seric 					fputs(mci->mci_mailer->m_eol, mci->mci_out);
238464187506Seric 					if (TrafficLogFile != NULL)
238564187506Seric 					{
238664187506Seric 						fputs(mci->mci_mailer->m_eol,
238764187506Seric 						      TrafficLogFile);
238864187506Seric 					}
238964187506Seric 					ostate = OS_HEAD;
239064187506Seric 					continue;
239164187506Seric 				}
239264187506Seric 
239364187506Seric 				/* had a naked carriage return */
239464187506Seric 				*pbp++ = c;
239564187506Seric 				c = '\r';
239664187506Seric 				goto putch;
239764187506Seric 
239864187506Seric 			  case OS_INLINE:
239964187506Seric 				if (c == '\r')
240064187506Seric 				{
240164187506Seric 					ostate = OS_CR;
240264187506Seric 					continue;
240364187506Seric 				}
240464187506Seric putch:
240564187506Seric 				if (mci->mci_mailer->m_linelimit > 0 &&
240664187506Seric 				    pos > mci->mci_mailer->m_linelimit &&
240764187506Seric 				    c != '\n')
240864187506Seric 				{
240964187506Seric 					putc('!', mci->mci_out);
241064187506Seric 					fputs(mci->mci_mailer->m_eol, mci->mci_out);
241164187506Seric 					if (TrafficLogFile != NULL)
241264187506Seric 					{
241364187506Seric 						fprintf(TrafficLogFile, "!%s",
241464187506Seric 							mci->mci_mailer->m_eol);
241564187506Seric 					}
241664187506Seric 					ostate = OS_HEAD;
241764187506Seric 					*pbp++ = c;
241864187506Seric 					continue;
241964187506Seric 				}
242064187506Seric 				if (TrafficLogFile != NULL)
242164187506Seric 					fputc(c, TrafficLogFile);
242264187506Seric 				putc(c, mci->mci_out);
242364187506Seric 				pos++;
242464187506Seric 				ostate = c == '\n' ? OS_HEAD : OS_INLINE;
242564187506Seric 				break;
242664187506Seric 			}
242764187506Seric 		}
2428a6ce3008Seric 	}
242951552439Seric 
24309a6a5f55Seric 	if (ferror(e->e_dfp))
243151552439Seric 	{
2432df106f0bSeric 		syserr("putbody: %s: read error", e->e_df);
243351552439Seric 		ExitStat = EX_IOERR;
243451552439Seric 	}
243551552439Seric 
243664187506Seric endofmessage:
24370890ba1fSeric 	/* some mailers want extra blank line at end of message */
24385aa0f353Seric 	if (bitnset(M_BLANKEND, mci->mci_mailer->m_flags) &&
24395aa0f353Seric 	    buf[0] != '\0' && buf[0] != '\n')
24405aa0f353Seric 		putline("", mci);
24410890ba1fSeric 
24425aa0f353Seric 	(void) fflush(mci->mci_out);
24435aa0f353Seric 	if (ferror(mci->mci_out) && errno != EPIPE)
244451552439Seric 	{
244551552439Seric 		syserr("putbody: write error");
244651552439Seric 		ExitStat = EX_IOERR;
244751552439Seric 	}
244851552439Seric 	errno = 0;
244925a99e2eSeric }
245025a99e2eSeric /*
245125a99e2eSeric **  MAILFILE -- Send a message to a file.
245225a99e2eSeric **
2453f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
2454f129ec7dSeric **	bits, sendmail will try to become the owner of that file
2455f129ec7dSeric **	rather than the real user.  Obviously, this only works if
2456f129ec7dSeric **	sendmail runs as root.
2457f129ec7dSeric **
2458588cad61Seric **	This could be done as a subordinate mailer, except that it
2459588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
2460588cad61Seric **	view this as being sufficiently important as to include it
2461588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
2462588cad61Seric **	to create another process plus some pipes to save the message.
2463588cad61Seric **
246425a99e2eSeric **	Parameters:
246525a99e2eSeric **		filename -- the name of the file to send to.
24666259796dSeric **		ctladdr -- the controlling address header -- includes
24676259796dSeric **			the userid/groupid to be when sending.
246825a99e2eSeric **
246925a99e2eSeric **	Returns:
247025a99e2eSeric **		The exit code associated with the operation.
247125a99e2eSeric **
247225a99e2eSeric **	Side Effects:
247325a99e2eSeric **		none.
247425a99e2eSeric */
247525a99e2eSeric 
2476b31e7f2bSeric mailfile(filename, ctladdr, e)
247725a99e2eSeric 	char *filename;
24786259796dSeric 	ADDRESS *ctladdr;
2479b31e7f2bSeric 	register ENVELOPE *e;
248025a99e2eSeric {
248125a99e2eSeric 	register FILE *f;
248264187506Seric 	register int pid = -1;
248315d084d5Seric 	int mode;
248425a99e2eSeric 
2485671745f3Seric 	if (tTd(11, 1))
2486671745f3Seric 	{
2487671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
2488671745f3Seric 		printaddr(ctladdr, FALSE);
2489671745f3Seric 	}
2490671745f3Seric 
2491f170942cSeric 	if (e->e_xfp != NULL)
2492f170942cSeric 		fflush(e->e_xfp);
2493f170942cSeric 
249432d19d43Seric 	/*
249532d19d43Seric 	**  Fork so we can change permissions here.
249632d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
249732d19d43Seric 	**	the complications of calling subroutines, etc.
249832d19d43Seric 	*/
249932d19d43Seric 
250032d19d43Seric 	DOFORK(fork);
250132d19d43Seric 
250232d19d43Seric 	if (pid < 0)
250332d19d43Seric 		return (EX_OSERR);
250432d19d43Seric 	else if (pid == 0)
250532d19d43Seric 	{
250632d19d43Seric 		/* child -- actually write to file */
2507f129ec7dSeric 		struct stat stb;
2508f2e4d5e8Seric 		struct stat fsb;
25095aa0f353Seric 		MCI mcibuf;
25104ef13b77Seric 		int oflags = O_WRONLY|O_APPEND;
2511f129ec7dSeric 
2512f2e4d5e8Seric 		if (e->e_lockfp != NULL)
2513f2e4d5e8Seric 		{
2514f2e4d5e8Seric 			fclose(e->e_lockfp);
2515f2e4d5e8Seric 			e->e_lockfp = NULL;
2516f2e4d5e8Seric 		}
2517f2e4d5e8Seric 
25182b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
25192b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
25202b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
25213462ad9eSeric 		(void) umask(OldUmask);
252295f16dc0Seric 
252364187506Seric #ifdef HASLSTAT
252464187506Seric 		if ((SafeFileEnv != NULL ? lstat(filename, &stb)
252564187506Seric 					 : stat(filename, &stb)) < 0)
252664187506Seric #else
2527f129ec7dSeric 		if (stat(filename, &stb) < 0)
252864187506Seric #endif
25294ef13b77Seric 		{
25303a98e7eaSeric 			stb.st_mode = FileMode;
25314ef13b77Seric 			oflags |= O_CREAT|O_EXCL;
25324ef13b77Seric 		}
253364187506Seric 		else if (bitset(0111, stb.st_mode) || stb.st_nlink != 1 ||
253464187506Seric 			 (SafeFileEnv != NULL && !S_ISREG(stb.st_mode)))
2535f2e4d5e8Seric 			exit(EX_CANTCREAT);
253615d084d5Seric 		mode = stb.st_mode;
253795f16dc0Seric 
253895f16dc0Seric 		/* limit the errors to those actually caused in the child */
253995f16dc0Seric 		errno = 0;
254095f16dc0Seric 		ExitStat = EX_OK;
254195f16dc0Seric 
254219428781Seric 		if (ctladdr != NULL)
254315d084d5Seric 		{
254415d084d5Seric 			/* ignore setuid and setgid bits */
254515d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
254615d084d5Seric 		}
254715d084d5Seric 
25488f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
25498f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
25508f9146b0Srick 		{
25518f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
255295f16dc0Seric 			if (e->e_dfp == NULL)
255395f16dc0Seric 			{
25548f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
2555e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
25568f9146b0Srick 			}
25578f9146b0Srick 		}
25588f9146b0Srick 
255964187506Seric 		if (SafeFileEnv != NULL && SafeFileEnv[0] != NULL)
256064187506Seric 		{
256164187506Seric 			int i;
256264187506Seric 
256364187506Seric 			if (chroot(SafeFileEnv) < 0)
256464187506Seric 			{
256564187506Seric 				syserr("mailfile: Cannot chroot(%s)",
256664187506Seric 					SafeFileEnv);
256764187506Seric 				exit(EX_CANTCREAT);
256864187506Seric 			}
256964187506Seric 			i = strlen(SafeFileEnv);
257064187506Seric 			if (strncmp(SafeFileEnv, filename, i) == 0)
257164187506Seric 				filename += i;
257264187506Seric 		}
257364187506Seric 		if (chdir("/") < 0)
257464187506Seric 			syserr("mailfile: cannot chdir(/)");
257564187506Seric 
257615d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
2577e36b99e2Seric 		{
257864187506Seric 			if (ctladdr != NULL && ctladdr->q_uid != 0)
2579898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
2580898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
2581898a126bSbostic 					ctladdr->q_gid);
258264187506Seric 			else if (FileMailer != NULL && FileMailer->m_gid != 0)
258364187506Seric 				(void) initgroups(DefUser, FileMailer->m_gid);
258464187506Seric 			else
258564187506Seric 				(void) initgroups(DefUser, DefGid);
2586e36b99e2Seric 		}
258715d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
2588e36b99e2Seric 		{
258964187506Seric 			if (ctladdr != NULL && ctladdr->q_uid != 0)
25906abe14bdSeric 				(void) setuid(ctladdr->q_uid);
259164187506Seric 			else if (FileMailer != NULL && FileMailer->m_uid != 0)
259264187506Seric 				(void) setuid(FileMailer->m_uid);
259364187506Seric 			else
259464187506Seric 				(void) setuid(DefUid);
2595e36b99e2Seric 		}
259695f16dc0Seric 		FileName = filename;
259795f16dc0Seric 		LineNumber = 0;
25984ef13b77Seric 		f = dfopen(filename, oflags, FileMode);
259925a99e2eSeric 		if (f == NULL)
260095f16dc0Seric 		{
2601b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
260232d19d43Seric 			exit(EX_CANTCREAT);
260395f16dc0Seric 		}
2604f2e4d5e8Seric 		if (fstat(fileno(f), &fsb) < 0 ||
260564187506Seric 		    (!bitset(O_CREAT, oflags) &&
26064ef13b77Seric 		     (stb.st_nlink != fsb.st_nlink ||
2607f2e4d5e8Seric 		      stb.st_dev != fsb.st_dev ||
2608f2e4d5e8Seric 		      stb.st_ino != fsb.st_ino ||
260964187506Seric 		      stb.st_uid != fsb.st_uid)))
2610f2e4d5e8Seric 		{
2611f2e4d5e8Seric 			message("554 cannot write: file changed after open");
2612f2e4d5e8Seric 			exit(EX_CANTCREAT);
2613f2e4d5e8Seric 		}
261425a99e2eSeric 
26155aa0f353Seric 		bzero(&mcibuf, sizeof mcibuf);
26165aa0f353Seric 		mcibuf.mci_mailer = FileMailer;
26175aa0f353Seric 		mcibuf.mci_out = f;
26185aa0f353Seric 		if (bitnset(M_7BITS, FileMailer->m_flags))
26195aa0f353Seric 			mcibuf.mci_flags |= MCIF_7BIT;
26205aa0f353Seric 
26215aa0f353Seric 		putfromline(&mcibuf, e);
262264187506Seric 		(*e->e_puthdr)(&mcibuf, e->e_header, e);
26236ffc2dd1Seric 		(*e->e_putbody)(&mcibuf, e, NULL);
26245aa0f353Seric 		putline("\n", &mcibuf);
262595f16dc0Seric 		if (ferror(f))
262695f16dc0Seric 		{
2627b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
262895f16dc0Seric 			setstat(EX_IOERR);
262995f16dc0Seric 		}
2630ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
263132d19d43Seric 		(void) fflush(stdout);
2632e36b99e2Seric 
263327628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
2634c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
263595f16dc0Seric 		exit(ExitStat);
263613bbc08cSeric 		/*NOTREACHED*/
263732d19d43Seric 	}
263832d19d43Seric 	else
263932d19d43Seric 	{
264032d19d43Seric 		/* parent -- wait for exit status */
2641588cad61Seric 		int st;
264232d19d43Seric 
2643588cad61Seric 		st = waitfor(pid);
2644bf9bc890Seric 		if (WIFEXITED(st))
2645bf9bc890Seric 			return (WEXITSTATUS(st));
2646588cad61Seric 		else
2647b6a0de9dSeric 		{
2648b6a0de9dSeric 			syserr("child died on signal %d", st);
2649bf9bc890Seric 			return (EX_UNAVAILABLE);
2650b6a0de9dSeric 		}
26518f9146b0Srick 		/*NOTREACHED*/
265232d19d43Seric 	}
265325a99e2eSeric }
2654ea4dc939Seric /*
2655e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2656e103b48fSeric **
2657e103b48fSeric **	The signature describes how we are going to send this -- it
2658e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2659e103b48fSeric **	an ordered list of MX hosts.
2660e103b48fSeric **
2661e103b48fSeric **	Parameters:
2662e103b48fSeric **		m -- the mailer describing this host.
2663e103b48fSeric **		host -- the host name.
2664e103b48fSeric **		e -- the current envelope.
2665e103b48fSeric **
2666e103b48fSeric **	Returns:
2667e103b48fSeric **		The signature for this host.
2668e103b48fSeric **
2669e103b48fSeric **	Side Effects:
2670e103b48fSeric **		Can tweak the symbol table.
2671e103b48fSeric */
2672e103b48fSeric 
2673e103b48fSeric char *
2674e103b48fSeric hostsignature(m, host, e)
2675e103b48fSeric 	register MAILER *m;
2676e103b48fSeric 	char *host;
2677e103b48fSeric 	ENVELOPE *e;
2678e103b48fSeric {
2679e103b48fSeric 	register char *p;
2680e103b48fSeric 	register STAB *s;
2681e103b48fSeric 	int i;
2682e103b48fSeric 	int len;
26839d4a8008Seric #if NAMED_BIND
2684e103b48fSeric 	int nmx;
2685e103b48fSeric 	auto int rcode;
2686bafdc4e5Seric 	char *hp;
2687bafdc4e5Seric 	char *endp;
268864187506Seric 	int oldoptions = _res.options;
2689e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2690e103b48fSeric #endif
2691e103b48fSeric 
2692e103b48fSeric 	/*
2693e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2694e103b48fSeric 	*/
2695e103b48fSeric 
2696e103b48fSeric 	p = m->m_mailer;
2697e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2698e103b48fSeric 	{
2699e103b48fSeric 		/* just an ordinary mailer */
2700e103b48fSeric 		return host;
2701e103b48fSeric 	}
2702e103b48fSeric 
2703e103b48fSeric 	/*
2704e103b48fSeric 	**  Look it up in the symbol table.
2705e103b48fSeric 	*/
2706e103b48fSeric 
2707e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2708e103b48fSeric 	if (s->s_hostsig != NULL)
2709e103b48fSeric 		return s->s_hostsig;
2710e103b48fSeric 
2711e103b48fSeric 	/*
2712e103b48fSeric 	**  Not already there -- create a signature.
2713e103b48fSeric 	*/
2714e103b48fSeric 
27159d4a8008Seric #if NAMED_BIND
2716516782b4Seric 	if (ConfigLevel < 2)
2717516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2718516782b4Seric 
2719bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2720bafdc4e5Seric 	{
2721bafdc4e5Seric 		endp = strchr(hp, ':');
2722bafdc4e5Seric 		if (endp != NULL)
2723bafdc4e5Seric 			*endp = '\0';
2724bafdc4e5Seric 
27257bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
27267d55540cSeric 
2727e103b48fSeric 		if (nmx <= 0)
2728e103b48fSeric 		{
2729e103b48fSeric 			register MCI *mci;
2730e103b48fSeric 
2731e103b48fSeric 			/* update the connection info for this host */
2732bafdc4e5Seric 			mci = mci_get(hp, m);
2733e103b48fSeric 			mci->mci_exitstat = rcode;
2734e103b48fSeric 			mci->mci_errno = errno;
2735f170942cSeric 			mci->mci_herrno = h_errno;
2736e103b48fSeric 
2737e103b48fSeric 			/* and return the original host name as the signature */
2738bafdc4e5Seric 			nmx = 1;
2739bafdc4e5Seric 			mxhosts[0] = hp;
2740e103b48fSeric 		}
2741e103b48fSeric 
2742e103b48fSeric 		len = 0;
2743e103b48fSeric 		for (i = 0; i < nmx; i++)
2744e103b48fSeric 		{
2745e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2746e103b48fSeric 		}
2747bafdc4e5Seric 		if (s->s_hostsig != NULL)
2748bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2749bafdc4e5Seric 		p = xalloc(len);
2750bafdc4e5Seric 		if (s->s_hostsig != NULL)
2751bafdc4e5Seric 		{
2752bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2753bafdc4e5Seric 			free(s->s_hostsig);
2754bafdc4e5Seric 			s->s_hostsig = p;
2755bafdc4e5Seric 			p += strlen(p);
2756bafdc4e5Seric 			*p++ = ':';
2757bafdc4e5Seric 		}
2758bafdc4e5Seric 		else
2759bafdc4e5Seric 			s->s_hostsig = p;
2760e103b48fSeric 		for (i = 0; i < nmx; i++)
2761e103b48fSeric 		{
2762e103b48fSeric 			if (i != 0)
2763e103b48fSeric 				*p++ = ':';
2764e103b48fSeric 			strcpy(p, mxhosts[i]);
2765e103b48fSeric 			p += strlen(p);
2766e103b48fSeric 		}
2767bafdc4e5Seric 		if (endp != NULL)
2768bafdc4e5Seric 			*endp++ = ':';
2769bafdc4e5Seric 	}
2770e103b48fSeric 	makelower(s->s_hostsig);
2771516782b4Seric 	if (ConfigLevel < 2)
2772516782b4Seric 		_res.options = oldoptions;
2773e103b48fSeric #else
2774e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2775e103b48fSeric 	s->s_hostsig = host;
2776e103b48fSeric #endif
2777e103b48fSeric 	if (tTd(17, 1))
2778e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2779e103b48fSeric 	return s->s_hostsig;
2780e103b48fSeric }
278164187506Seric /*
278264187506Seric **  SETSTATUS -- set the address status for return messages
278364187506Seric **
278464187506Seric **	Parameters:
278564187506Seric **		a -- the address to set.
278664187506Seric **		msg -- the text of the message, which must be in standard
278764187506Seric **			SMTP form (3 digits, a space, and a message).
278864187506Seric **
278964187506Seric **	Returns:
279064187506Seric **		none.
279164187506Seric */
279264187506Seric 
279364187506Seric setstatus(a, msg)
279464187506Seric 	register ADDRESS *a;
279564187506Seric 	char *msg;
279664187506Seric {
279764187506Seric 	char buf[MAXLINE];
279864187506Seric 
279964187506Seric 	if (a->q_rstatus != NULL)
280064187506Seric 		free(a->q_rstatus);
280164187506Seric 	if (strlen(msg) > 4)
280264187506Seric 	{
280364187506Seric 		register char *p, *q;
280464187506Seric 		int parenlev = 0;
280564187506Seric 
280664187506Seric 		strncpy(buf, msg, 4);
280764187506Seric 		p = &buf[4];
280864187506Seric 		*p++ = '(';
280964187506Seric 		for (q = &msg[4]; *q != '\0'; q++)
281064187506Seric 		{
281164187506Seric 			switch (*q)
281264187506Seric 			{
281364187506Seric 			  case '(':
281464187506Seric 				parenlev++;
281564187506Seric 				break;
281664187506Seric 
281764187506Seric 			  case ')':
281864187506Seric 				if (parenlev > 0)
281964187506Seric 					parenlev--;
282064187506Seric 				else
282164187506Seric 					*p++ = '\\';
282264187506Seric 				break;
282364187506Seric 			}
282464187506Seric 			*p++ = *q;
282564187506Seric 		}
282664187506Seric 		while (parenlev-- >= 0)
282764187506Seric 			*p++ = ')';
282864187506Seric 		*p++ = '\0';
282964187506Seric 		msg = buf;
283064187506Seric 	}
283164187506Seric 	a->q_rstatus = newstr(msg);
283264187506Seric }
2833