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*ac5a49f9Seric static char sccsid[] = "@(#)deliver.c	8.135 (Berkeley) 03/23/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;
5264187506Seric 	bool oldverbose = Verbose;
53c088ca03Seric 	bool somedeliveries = FALSE;
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;
799c9e68d9Seric 	}
809c9e68d9Seric 
819c9e68d9Seric 	if (tTd(13, 1))
829c9e68d9Seric 	{
83c088ca03Seric 		extern void printenvflags();
84c088ca03Seric 
850e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
860e484fe5Seric 			mode, e->e_id);
879c9e68d9Seric 		printaddr(&e->e_from, FALSE);
88c088ca03Seric 		printf("\te_flags = ");
89c088ca03Seric 		printenvflags(e);
909c9e68d9Seric 		printf("sendqueue:\n");
919c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
929c9e68d9Seric 	}
939c9e68d9Seric 
949c9e68d9Seric 	/*
959c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
969c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
979c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
989c9e68d9Seric 	*/
999c9e68d9Seric 
1009c9e68d9Seric 	CurEnv = e;
1019c9e68d9Seric 
1029c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
1039c9e68d9Seric 	{
1049c9e68d9Seric 		errno = 0;
105c088ca03Seric 		queueup(e, TRUE, mode == SM_QUEUE);
10627607809Seric 		e->e_flags |= EF_FATALERRS|EF_PM_NOTIFY|EF_CLRQUEUE;
1074bb8b0fdSeric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
1089c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
109f7869e68Seric 			RealHostName == NULL ? "localhost" : RealHostName,
110f7869e68Seric 			e->e_sendqueue->q_paddr);
1119c9e68d9Seric 		return;
1129c9e68d9Seric 	}
1139c9e68d9Seric 
114b8004690Seric 	/*
115b8004690Seric 	**  Do sender deletion.
116b8004690Seric 	**
117b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
118b8004690Seric 	**	This can happen if the name server is hosed when you
119b8004690Seric 	**	are trying to send mail.  The result is that the sender
120b8004690Seric 	**	is instantiated in the queue as a recipient.
121b8004690Seric 	*/
122b8004690Seric 
123e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
124e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1259c9e68d9Seric 	{
1269c9e68d9Seric 		if (tTd(13, 5))
1279c9e68d9Seric 		{
1289c9e68d9Seric 			printf("sendall: QDONTSEND ");
1299c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1309c9e68d9Seric 		}
1319c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
13264187506Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, 0, e);
1339c9e68d9Seric 	}
1349c9e68d9Seric 
135ce5531bdSeric 	/*
136ce5531bdSeric 	**  Handle alias owners.
137ce5531bdSeric 	**
138ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
139ce5531bdSeric 	**	We discard owners that are the same as the return path.
140ce5531bdSeric 	*/
141ce5531bdSeric 
142ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
143ce5531bdSeric 	{
144ce5531bdSeric 		register struct address *a;
145ce5531bdSeric 
146ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
147ce5531bdSeric 			continue;
148ce5531bdSeric 		if (a != NULL)
149ce5531bdSeric 			q->q_owner = a->q_owner;
150ce5531bdSeric 
151ce5531bdSeric 		if (q->q_owner != NULL &&
152ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
153ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
154ce5531bdSeric 			q->q_owner = NULL;
155ce5531bdSeric 	}
156ce5531bdSeric 
157ce5531bdSeric 	owner = "";
158ce5531bdSeric 	otherowners = 1;
159ce5531bdSeric 	while (owner != NULL && otherowners > 0)
160ce5531bdSeric 	{
161ce5531bdSeric 		owner = NULL;
162ce5531bdSeric 		otherowners = 0;
163ce5531bdSeric 
164ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
165ce5531bdSeric 		{
166ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
167ce5531bdSeric 				continue;
168ce5531bdSeric 
169ce5531bdSeric 			if (q->q_owner != NULL)
170ce5531bdSeric 			{
171ce5531bdSeric 				if (owner == NULL)
172ce5531bdSeric 					owner = q->q_owner;
173ce5531bdSeric 				else if (owner != q->q_owner)
174ce5531bdSeric 				{
175ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
176ce5531bdSeric 					{
177ce5531bdSeric 						/* make future comparisons cheap */
178ce5531bdSeric 						q->q_owner = owner;
179ce5531bdSeric 					}
180ce5531bdSeric 					else
181ce5531bdSeric 					{
182ce5531bdSeric 						otherowners++;
183ce5531bdSeric 					}
184ce5531bdSeric 					owner = q->q_owner;
185ce5531bdSeric 				}
186ce5531bdSeric 			}
187ce5531bdSeric 			else
188ce5531bdSeric 			{
189ce5531bdSeric 				otherowners++;
190ce5531bdSeric 			}
191c088ca03Seric 
192c088ca03Seric 			/*
193c088ca03Seric 			**  If this mailer is expensive, and if we don't
194c088ca03Seric 			**  want to make connections now, just mark these
195c088ca03Seric 			**  addresses and return.  This is useful if we
196c088ca03Seric 			**  want to batch connections to reduce load.  This
197c088ca03Seric 			**  will cause the messages to be queued up, and a
198c088ca03Seric 			**  daemon will come along to send the messages later.
199c088ca03Seric 			*/
200c088ca03Seric 
201c088ca03Seric 			if (bitset(QBADADDR|QQUEUEUP, q->q_flags))
202c088ca03Seric 				continue;
203c088ca03Seric 			if (NoConnect && !Verbose &&
204c088ca03Seric 			    bitnset(M_EXPENSIVE, q->q_mailer->m_flags))
205c088ca03Seric 			{
206c088ca03Seric 				q->q_flags |= QQUEUEUP;
207c088ca03Seric 				e->e_to = q->q_paddr;
208c088ca03Seric 				message("queued");
209c088ca03Seric 				if (LogLevel > 8)
210c088ca03Seric 					logdelivery(q->q_mailer, NULL,
211c088ca03Seric 						    "queued", NULL,
212c088ca03Seric 						    (time_t) 0, e);
213c088ca03Seric 				e->e_to = NULL;
214c088ca03Seric 			}
215c088ca03Seric 			else
216c088ca03Seric 			{
217c088ca03Seric 				somedeliveries = TRUE;
218c088ca03Seric 			}
219ce5531bdSeric 		}
220ce5531bdSeric 
221ce5531bdSeric 		if (owner != NULL && otherowners > 0)
222ce5531bdSeric 		{
223ce5531bdSeric 			extern HDR *copyheader();
224ce5531bdSeric 			extern ADDRESS *copyqueue();
225ce5531bdSeric 
226ce5531bdSeric 			/*
227ce5531bdSeric 			**  Split this envelope into two.
228ce5531bdSeric 			*/
229ce5531bdSeric 
230ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
231ce5531bdSeric 			*ee = *e;
232ce5531bdSeric 			ee->e_id = NULL;
233ce5531bdSeric 			(void) queuename(ee, '\0');
234ce5531bdSeric 
235ce5531bdSeric 			if (tTd(13, 1))
236ce5531bdSeric 				printf("sendall: split %s into %s\n",
237ce5531bdSeric 					e->e_id, ee->e_id);
238ce5531bdSeric 
239ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
240ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
241ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
242fce3c07bSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS|EF_SENDRECEIPT);
243fce3c07bSeric 			ee->e_flags |= EF_NORECEIPT;
244ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
245ce5531bdSeric 			if (tTd(13, 5))
246ce5531bdSeric 			{
247ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
248ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
249ce5531bdSeric 			}
250ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
251ce5531bdSeric 			ee->e_dfp = NULL;
252ce5531bdSeric 			ee->e_xfp = NULL;
253ce5531bdSeric 			ee->e_errormode = EM_MAIL;
254b056abd0Seric 			ee->e_sibling = splitenv;
255b056abd0Seric 			splitenv = ee;
256ce5531bdSeric 
257ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
258ce5531bdSeric 				if (q->q_owner == owner)
259de1a6b1bSeric 				{
260ce5531bdSeric 					q->q_flags |= QDONTSEND;
261de1a6b1bSeric 					q->q_flags &= ~QQUEUEUP;
262de1a6b1bSeric 				}
263ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
264ce5531bdSeric 				if (q->q_owner != owner)
265de1a6b1bSeric 				{
266ce5531bdSeric 					q->q_flags |= QDONTSEND;
267de1a6b1bSeric 					q->q_flags &= ~QQUEUEUP;
268de1a6b1bSeric 				}
269ce5531bdSeric 
27057736a0dSeric 			if (mode != SM_VERIFY && bitset(EF_HAS_DF, e->e_flags))
271ce5531bdSeric 			{
27257736a0dSeric 				char df1buf[20], df2buf[20];
27357736a0dSeric 
274ce5531bdSeric 				ee->e_dfp = NULL;
27557736a0dSeric 				strcpy(df1buf, queuename(e, 'd'));
27657736a0dSeric 				strcpy(df2buf, queuename(ee, 'd'));
27757736a0dSeric 				if (link(df1buf, df2buf) < 0)
278ce5531bdSeric 				{
279ce5531bdSeric 					syserr("sendall: link(%s, %s)",
28057736a0dSeric 						df1buf, df2buf);
281ce5531bdSeric 				}
282ce5531bdSeric 			}
283ce5531bdSeric #ifdef LOG
284ce5531bdSeric 			if (LogLevel > 4)
285c8b70947Seric 				syslog(LOG_INFO, "%s: clone %s, owner=%s",
286c8b70947Seric 					ee->e_id, e->e_id, owner);
287ce5531bdSeric #endif
288ce5531bdSeric 		}
289ce5531bdSeric 	}
290ce5531bdSeric 
291ce5531bdSeric 	if (owner != NULL)
292ce5531bdSeric 	{
293ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
294ce5531bdSeric 		if (tTd(13, 5))
295ce5531bdSeric 		{
296ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
297ce5531bdSeric 			printaddr(&e->e_from, FALSE);
298ce5531bdSeric 		}
299ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
300ce5531bdSeric 		e->e_errormode = EM_MAIL;
301fce3c07bSeric 		e->e_flags |= EF_NORECEIPT;
302ce5531bdSeric 	}
303ce5531bdSeric 
304c088ca03Seric 	/* if nothing to be delivered, just queue up everything */
305c088ca03Seric 	if (!somedeliveries && mode != SM_QUEUE && mode != SM_VERIFY)
306c088ca03Seric 		mode = SM_QUEUE;
307c088ca03Seric 
308c1ac89b1Seric # ifdef QUEUE
309c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
310c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
311c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
312c1ac89b1Seric 	{
313c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
314c088ca03Seric 		queueup(e, TRUE, mode == SM_QUEUE);
315b056abd0Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
316c088ca03Seric 			queueup(ee, TRUE, mode == SM_QUEUE);
317c1ac89b1Seric 	}
318c1ac89b1Seric #endif /* QUEUE */
319c1ac89b1Seric 
3207880f3e4Seric 	/*
32164187506Seric 	**  If we belong in background, fork now.
3227880f3e4Seric 	*/
3237880f3e4Seric 
324c1ac89b1Seric 	switch (mode)
325c1ac89b1Seric 	{
326c1ac89b1Seric 	  case SM_VERIFY:
327c1ac89b1Seric 		Verbose = TRUE;
328c1ac89b1Seric 		break;
329c1ac89b1Seric 
330c1ac89b1Seric 	  case SM_QUEUE:
331c1ac89b1Seric   queueonly:
332c088ca03Seric 		if (e->e_nrcpts > 0)
333c1ac89b1Seric 			e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
334c1ac89b1Seric 		return;
335c1ac89b1Seric 
336c1ac89b1Seric 	  case SM_FORK:
337c1ac89b1Seric 		if (e->e_xfp != NULL)
338c1ac89b1Seric 			(void) fflush(e->e_xfp);
339c1ac89b1Seric 
340b269aa8eSeric # if !HASFLOCK
341c1ac89b1Seric 		/*
3426b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3436b2765c6Seric 		**  the lock is owned by a process, not by an open file
3446b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3456b2765c6Seric 		**  then restart from scratch in the child.
346c1ac89b1Seric 		*/
347c1ac89b1Seric 
348f1994586Seric 		{
3496b2765c6Seric 			/* save id for future use */
350f1994586Seric 			char *qid = e->e_id;
3516b2765c6Seric 
3526b2765c6Seric 			/* now drop the envelope in the parent */
3536b2765c6Seric 			e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3546b2765c6Seric 			dropenvelope(e);
3556b2765c6Seric 
3566b2765c6Seric 			/* and reacquire in the child */
35764187506Seric 			(void) dowork(qid, TRUE, FALSE, e);
358f1994586Seric 		}
3596b2765c6Seric 
3606b2765c6Seric 		return;
3616b2765c6Seric 
3626b2765c6Seric # else /* HASFLOCK */
363c1ac89b1Seric 
364c1ac89b1Seric 		pid = fork();
365c1ac89b1Seric 		if (pid < 0)
366c1ac89b1Seric 		{
367c1ac89b1Seric 			goto queueonly;
368c1ac89b1Seric 		}
369c1ac89b1Seric 		else if (pid > 0)
370c1ac89b1Seric 		{
3710e484fe5Seric 			/* be sure we leave the temp files to our child */
3720e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3730e484fe5Seric 			if (e->e_lockfp != NULL)
37457736a0dSeric 				(void) xfclose(e->e_lockfp, "sendenvelope lockfp", e->e_id);
3750e484fe5Seric 			e->e_lockfp = NULL;
3760e484fe5Seric 
3770e484fe5Seric 			/* close any random open files in the envelope */
3780e484fe5Seric 			closexscript(e);
3790e484fe5Seric 			if (e->e_dfp != NULL)
38057736a0dSeric 				(void) xfclose(e->e_dfp, "sendenvelope dfp", e->e_id);
3810e484fe5Seric 			e->e_dfp = NULL;
38257736a0dSeric 			e->e_id = NULL;
38357736a0dSeric 			e->e_flags &= ~EF_HAS_DF;
3849f9b003eSeric 
3859f9b003eSeric 			/* catch intermediate zombie */
3869f9b003eSeric 			(void) waitfor(pid);
387c1ac89b1Seric 			return;
388c1ac89b1Seric 		}
389c1ac89b1Seric 
390c1ac89b1Seric 		/* double fork to avoid zombies */
3919f9b003eSeric 		pid = fork();
3929f9b003eSeric 		if (pid > 0)
393c1ac89b1Seric 			exit(EX_OK);
394c1ac89b1Seric 
395c1ac89b1Seric 		/* be sure we are immune from the terminal */
3967880f3e4Seric 		disconnect(1, e);
397c1ac89b1Seric 
3989f9b003eSeric 		/* prevent parent from waiting if there was an error */
3999f9b003eSeric 		if (pid < 0)
4009f9b003eSeric 		{
4019f9b003eSeric 			e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
4029f9b003eSeric 			finis();
4039f9b003eSeric 		}
4049f9b003eSeric 
405c1ac89b1Seric 		/*
406c1ac89b1Seric 		**  Close any cached connections.
407c1ac89b1Seric 		**
408c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
409c1ac89b1Seric 		**	still knows about the connection.
410c1ac89b1Seric 		**
411c1ac89b1Seric 		**	This should only happen when delivering an error
412c1ac89b1Seric 		**	message.
413c1ac89b1Seric 		*/
414c1ac89b1Seric 
415c1ac89b1Seric 		mci_flush(FALSE, NULL);
416c1ac89b1Seric 
4176b2765c6Seric # endif /* HASFLOCK */
4186b2765c6Seric 
419c1ac89b1Seric 		break;
420c1ac89b1Seric 	}
421c1ac89b1Seric 
42264187506Seric 	if (splitenv != NULL)
42364187506Seric 	{
42464187506Seric 		if (tTd(13, 1))
42564187506Seric 		{
42664187506Seric 			printf("\nsendall: Split queue; remaining queue:\n");
42764187506Seric 			printaddr(e->e_sendqueue, TRUE);
42864187506Seric 		}
42964187506Seric 
43064187506Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
43164187506Seric 		{
43264187506Seric 			CurEnv = ee;
43364187506Seric 			if (mode != SM_VERIFY)
43464187506Seric 				openxscript(ee);
43564187506Seric 			sendenvelope(ee, mode);
43664187506Seric 			dropenvelope(ee);
43764187506Seric 		}
43864187506Seric 
43964187506Seric 		CurEnv = e;
44064187506Seric 	}
44164187506Seric 	sendenvelope(e, mode);
44264187506Seric 	Verbose = oldverbose;
44364187506Seric }
44464187506Seric 
44564187506Seric void
44664187506Seric sendenvelope(e, mode)
44764187506Seric 	register ENVELOPE *e;
44864187506Seric 	char mode;
44964187506Seric {
45064187506Seric 	register ADDRESS *q;
45164187506Seric 	bool didany;
45264187506Seric 
45364187506Seric 	/*
45464187506Seric 	**  If we have had global, fatal errors, don't bother sending
45564187506Seric 	**  the message at all if we are in SMTP mode.  Local errors
45664187506Seric 	**  (e.g., a single address failing) will still cause the other
45764187506Seric 	**  addresses to be sent.
45864187506Seric 	*/
45964187506Seric 
46064187506Seric 	if (bitset(EF_FATALERRS, e->e_flags) &&
46164187506Seric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
46264187506Seric 	{
46364187506Seric 		e->e_flags |= EF_CLRQUEUE;
46464187506Seric 		return;
46564187506Seric 	}
46664187506Seric 
467c1ac89b1Seric 	/*
4689c9e68d9Seric 	**  Run through the list and send everything.
4695288e21aSeric 	**
4705288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4715288e21aSeric 	**	result in returned mail.
4729c9e68d9Seric 	*/
4739c9e68d9Seric 
4749c9e68d9Seric 	e->e_nsent = 0;
4755288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
47664187506Seric 	didany = FALSE;
477faad2b16Seric 
478faad2b16Seric 	/* now run through the queue */
4799c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4809c9e68d9Seric 	{
481fdc75a0fSeric #ifdef XDEBUG
482fdc75a0fSeric 		char wbuf[MAXNAME + 20];
483fdc75a0fSeric 
484fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
485fdc75a0fSeric 		checkfd012(wbuf);
486fdc75a0fSeric #endif
4879c9e68d9Seric 		if (mode == SM_VERIFY)
4889c9e68d9Seric 		{
4899c9e68d9Seric 			e->e_to = q->q_paddr;
4909c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4918dfca105Seric 			{
492fafe46e1Seric 				if (q->q_host != NULL && q->q_host[0] != '\0')
4938dfca105Seric 					message("deliverable: mailer %s, host %s, user %s",
4948dfca105Seric 						q->q_mailer->m_name,
4958dfca105Seric 						q->q_host,
4968dfca105Seric 						q->q_user);
497fafe46e1Seric 				else
498fafe46e1Seric 					message("deliverable: mailer %s, user %s",
499fafe46e1Seric 						q->q_mailer->m_name,
500fafe46e1Seric 						q->q_user);
5018dfca105Seric 			}
5029c9e68d9Seric 		}
5039c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
5049c9e68d9Seric 		{
5059c9e68d9Seric # ifdef QUEUE
5069c9e68d9Seric 			/*
5079c9e68d9Seric 			**  Checkpoint the send list every few addresses
5089c9e68d9Seric 			*/
5099c9e68d9Seric 
5109c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
5119c9e68d9Seric 			{
5129c9e68d9Seric 				queueup(e, TRUE, FALSE);
5139c9e68d9Seric 				e->e_nsent = 0;
5149c9e68d9Seric 			}
5159c9e68d9Seric # endif /* QUEUE */
5169c9e68d9Seric 			(void) deliver(e, q);
51764187506Seric 			didany = TRUE;
5189c9e68d9Seric 		}
5199c9e68d9Seric 	}
52064187506Seric 	if (didany)
52164187506Seric 	{
52264187506Seric 		e->e_dtime = curtime();
52364187506Seric 		e->e_ntries++;
52464187506Seric 	}
5259c9e68d9Seric 
526fdc75a0fSeric #ifdef XDEBUG
527fdc75a0fSeric 	checkfd012("end of sendenvelope");
528fdc75a0fSeric #endif
529fdc75a0fSeric 
5309c9e68d9Seric 	if (mode == SM_FORK)
5319c9e68d9Seric 		finis();
5329c9e68d9Seric }
5339c9e68d9Seric /*
5349c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
5359c9e68d9Seric **
5369c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
5379c9e68d9Seric **	two processes on the same stack!!!
5389c9e68d9Seric **
5399c9e68d9Seric **	Parameters:
5409c9e68d9Seric **		none.
5419c9e68d9Seric **
5429c9e68d9Seric **	Returns:
5439c9e68d9Seric **		From a macro???  You've got to be kidding!
5449c9e68d9Seric **
5459c9e68d9Seric **	Side Effects:
5469c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
5479c9e68d9Seric **			pid of child in parent, zero in child.
5489c9e68d9Seric **			-1 on unrecoverable error.
5499c9e68d9Seric **
5509c9e68d9Seric **	Notes:
5519c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
5529c9e68d9Seric **		vfork for you.....
5539c9e68d9Seric */
5549c9e68d9Seric 
5559c9e68d9Seric # define NFORKTRIES	5
5569c9e68d9Seric 
5579c9e68d9Seric # ifndef FORK
5589c9e68d9Seric # define FORK	fork
5599c9e68d9Seric # endif
5609c9e68d9Seric 
5619c9e68d9Seric # define DOFORK(fORKfN) \
5629c9e68d9Seric {\
5639c9e68d9Seric 	register int i;\
5649c9e68d9Seric \
5659c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
5669c9e68d9Seric 	{\
5679c9e68d9Seric 		pid = fORKfN();\
5689c9e68d9Seric 		if (pid >= 0)\
5699c9e68d9Seric 			break;\
5709c9e68d9Seric 		if (i > 0)\
5719c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5729c9e68d9Seric 	}\
5739c9e68d9Seric }
5749c9e68d9Seric /*
5759c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5769c9e68d9Seric **
5779c9e68d9Seric **	Parameters:
5789c9e68d9Seric **		none.
5799c9e68d9Seric **
5809c9e68d9Seric **	Returns:
5819c9e68d9Seric **		pid of child in parent.
5829c9e68d9Seric **		zero in child.
5839c9e68d9Seric **		-1 on error.
5849c9e68d9Seric **
5859c9e68d9Seric **	Side Effects:
5869c9e68d9Seric **		returns twice, once in parent and once in child.
5879c9e68d9Seric */
5889c9e68d9Seric 
5899c9e68d9Seric dofork()
5909c9e68d9Seric {
59164187506Seric 	register int pid = -1;
5929c9e68d9Seric 
5939c9e68d9Seric 	DOFORK(fork);
5949c9e68d9Seric 	return (pid);
5959c9e68d9Seric }
5969c9e68d9Seric /*
59713bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
59813bbc08cSeric **
59913bbc08cSeric **	This routine delivers to everyone on the same host as the
60013bbc08cSeric **	user on the head of the list.  It is clever about mailers
60113bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
60213bbc08cSeric **	that it will deliver to all these addresses however -- so
60313bbc08cSeric **	deliver should be called once for each address on the
60413bbc08cSeric **	list.
60525a99e2eSeric **
60625a99e2eSeric **	Parameters:
607588cad61Seric **		e -- the envelope to deliver.
608c77d1c25Seric **		firstto -- head of the address list to deliver to.
60925a99e2eSeric **
61025a99e2eSeric **	Returns:
61125a99e2eSeric **		zero -- successfully delivered.
61225a99e2eSeric **		else -- some failure, see ExitStat for more info.
61325a99e2eSeric **
61425a99e2eSeric **	Side Effects:
61525a99e2eSeric **		The standard input is passed off to someone.
61625a99e2eSeric */
61725a99e2eSeric 
61864187506Seric int
619588cad61Seric deliver(e, firstto)
620588cad61Seric 	register ENVELOPE *e;
621c77d1c25Seric 	ADDRESS *firstto;
62225a99e2eSeric {
62378442df3Seric 	char *host;			/* host being sent to */
62478442df3Seric 	char *user;			/* user being sent to */
62525a99e2eSeric 	char **pvp;
6265dfc646bSeric 	register char **mvp;
62725a99e2eSeric 	register char *p;
628588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
6296259796dSeric 	ADDRESS *ctladdr;
630b31e7f2bSeric 	register MCI *mci;
631c77d1c25Seric 	register ADDRESS *to = firstto;
632c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
63364187506Seric 	ADDRESS *tochain = NULL;	/* users chain in this mailer call */
634911693bfSbostic 	int rcode;			/* response code */
635e103b48fSeric 	char *firstsig;			/* signature of firstto */
63664187506Seric 	int pid = -1;
6379c9e68d9Seric 	char *curhost;
63864187506Seric 	time_t xstart;
6399c9e68d9Seric 	int mpvect[2];
6409c9e68d9Seric 	int rpvect[2];
641ee6bf8dfSeric 	char *pv[MAXPV+1];
642579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
643a524e4d0Seric 	char buf[MAXNAME + 1];
644a524e4d0Seric 	char rpathbuf[MAXNAME + 1];	/* translated return path */
645fabb3bd4Seric 	extern int checkcompat();
64625a99e2eSeric 
64735490626Seric 	errno = 0;
648ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
6495dfc646bSeric 		return (0);
65025a99e2eSeric 
6519d4a8008Seric #if NAMED_BIND
652912a731aSbostic 	/* unless interactive, try twice, over a minute */
6538d19a23dSeric 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP)
6548d19a23dSeric 	{
655912a731aSbostic 		_res.retrans = 30;
656912a731aSbostic 		_res.retry = 2;
657912a731aSbostic 	}
658d4bd8f0eSbostic #endif
659912a731aSbostic 
66051552439Seric 	m = to->q_mailer;
66151552439Seric 	host = to->q_host;
662c9be6216Seric 	CurEnv = e;			/* just in case */
6634384d521Seric 	e->e_statmsg = NULL;
664df106f0bSeric 	SmtpError[0] = '\0';
66564187506Seric 	xstart = curtime();
66651552439Seric 
6676ef48975Seric 	if (tTd(10, 1))
668562ec147Seric 		printf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n",
669562ec147Seric 			e->e_id, m->m_name, host, to->q_user);
670ab81ee53Seric 	if (tTd(10, 100))
671ab81ee53Seric 		printopenfds(FALSE);
672f3dbc832Seric 
673f3dbc832Seric 	/*
6745dfc646bSeric 	**  Do initial argv setup.
6755dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6765dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6775dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6785dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6795dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6803bea8136Seric 	**		The from address rewrite is expected to make
6813bea8136Seric 	**		the address relative to the other end.
6825dfc646bSeric 	*/
6835dfc646bSeric 
68478442df3Seric 	/* rewrite from address, using rewriting rules */
685efe54562Seric 	rcode = EX_OK;
68664187506Seric 	if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
68764187506Seric 		p = e->e_sender;
68864187506Seric 	else
68964187506Seric 		p = e->e_from.q_paddr;
69064187506Seric 	(void) strcpy(rpathbuf, remotename(p, m,
691efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
692efe54562Seric 					   &rcode, e));
693ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
694588cad61Seric 	define('h', host, e);			/* to host */
6955dfc646bSeric 	Errors = 0;
6965dfc646bSeric 	pvp = pv;
6975dfc646bSeric 	*pvp++ = m->m_argv[0];
6985dfc646bSeric 
6995dfc646bSeric 	/* insert -f or -r flag as appropriate */
70057fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
7015dfc646bSeric 	{
70257fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
7035dfc646bSeric 			*pvp++ = "-f";
7045dfc646bSeric 		else
7055dfc646bSeric 			*pvp++ = "-r";
706c23ed322Seric 		*pvp++ = newstr(rpathbuf);
7075dfc646bSeric 	}
7085dfc646bSeric 
7095dfc646bSeric 	/*
7105dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
7115dfc646bSeric 	**  up to the first entry containing "$u".  There can only
7125dfc646bSeric 	**  be one of these, and there are only a few more slots
7135dfc646bSeric 	**  in the pv after it.
7145dfc646bSeric 	*/
7155dfc646bSeric 
7165dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
7175dfc646bSeric 	{
7182bc47524Seric 		/* can't use strchr here because of sign extension problems */
7192bc47524Seric 		while (*p != '\0')
7202bc47524Seric 		{
7212bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
7222bc47524Seric 			{
7232bc47524Seric 				if (*p == 'u')
7245dfc646bSeric 					break;
7252bc47524Seric 			}
7262bc47524Seric 		}
7272bc47524Seric 
7282bc47524Seric 		if (*p != '\0')
7295dfc646bSeric 			break;
7305dfc646bSeric 
7315dfc646bSeric 		/* this entry is safe -- go ahead and process it */
732fbf6dabfSeric 		expand(*mvp, buf, sizeof buf, e);
7335dfc646bSeric 		*pvp++ = newstr(buf);
7345dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
7355dfc646bSeric 		{
73608b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
7375dfc646bSeric 			return (-1);
7385dfc646bSeric 		}
7395dfc646bSeric 	}
740c579ef51Seric 
74133db8731Seric 	/*
74233db8731Seric 	**  If we have no substitution for the user name in the argument
74333db8731Seric 	**  list, we know that we must supply the names otherwise -- and
74433db8731Seric 	**  SMTP is the answer!!
74533db8731Seric 	*/
74633db8731Seric 
7475dfc646bSeric 	if (*mvp == NULL)
748c579ef51Seric 	{
749c579ef51Seric 		/* running SMTP */
7502c7e1b8dSeric # ifdef SMTP
751c579ef51Seric 		clever = TRUE;
752c579ef51Seric 		*pvp = NULL;
7536c2c3107Seric # else /* SMTP */
75433db8731Seric 		/* oops!  we don't implement SMTP */
755df106f0bSeric 		syserr("554 SMTP style mailer not implemented");
7562c7e1b8dSeric 		return (EX_SOFTWARE);
7576c2c3107Seric # endif /* SMTP */
758c579ef51Seric 	}
7595dfc646bSeric 
7605dfc646bSeric 	/*
7615dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7625dfc646bSeric 	**  run through our address list and append all the addresses
7635dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7645dfc646bSeric 	**  always send another copy later.
7655dfc646bSeric 	*/
7665dfc646bSeric 
7675dfc646bSeric 	tobuf[0] = '\0';
768588cad61Seric 	e->e_to = tobuf;
7696259796dSeric 	ctladdr = NULL;
770e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7715dfc646bSeric 	for (; to != NULL; to = to->q_next)
7725dfc646bSeric 	{
7735dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
77457fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7755dfc646bSeric 			break;
7765dfc646bSeric 
7775dfc646bSeric 		/* if already sent or not for this host, don't send */
778ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
779e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
780e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7815dfc646bSeric 			continue;
7826259796dSeric 
7834b22ea87Seric 		/* avoid overflowing tobuf */
784aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7854b22ea87Seric 			break;
7864b22ea87Seric 
7876ef48975Seric 		if (tTd(10, 1))
788772e6e50Seric 		{
789772e6e50Seric 			printf("\nsend to ");
790772e6e50Seric 			printaddr(to, FALSE);
791772e6e50Seric 		}
792772e6e50Seric 
7936259796dSeric 		/* compute effective uid/gid when sending */
79464187506Seric 		if (bitnset(M_RUNASRCPT, to->q_mailer->m_flags))
7956259796dSeric 			ctladdr = getctladdr(to);
7966259796dSeric 
79764187506Seric 		if (tTd(10, 2))
79864187506Seric 		{
79964187506Seric 			printf("ctladdr=");
80064187506Seric 			printaddr(ctladdr, FALSE);
80164187506Seric 		}
80264187506Seric 
8035dfc646bSeric 		user = to->q_user;
804588cad61Seric 		e->e_to = to->q_paddr;
80575f1ade9Seric 		if (tTd(10, 5))
80675f1ade9Seric 		{
80775f1ade9Seric 			printf("deliver: QDONTSEND ");
80875f1ade9Seric 			printaddr(to, FALSE);
80975f1ade9Seric 		}
810ee4b0922Seric 		to->q_flags |= QDONTSEND;
8115dfc646bSeric 
8125dfc646bSeric 		/*
8135dfc646bSeric 		**  Check to see that these people are allowed to
8145dfc646bSeric 		**  talk to each other.
8152a6e0786Seric 		*/
8162a6e0786Seric 
81769582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
81869582d2fSeric 		{
81943357751Seric 			e->e_flags |= EF_NO_BODY_RETN;
82008b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
82164187506Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, xstart, e);
82269582d2fSeric 			continue;
82369582d2fSeric 		}
82464187506Seric #if NAMED_BIND
82564187506Seric 		h_errno = 0;
82664187506Seric #endif
827fabb3bd4Seric 		rcode = checkcompat(to, e);
8281793c9c5Seric 		if (rcode != EX_OK)
8295dfc646bSeric 		{
83064187506Seric 			markfailure(e, to, NULL, rcode);
83164187506Seric 			giveresponse(rcode, m, NULL, ctladdr, xstart, e);
8325dfc646bSeric 			continue;
8335dfc646bSeric 		}
8342a6e0786Seric 
8352a6e0786Seric 		/*
8369ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
8379ec9501bSeric 		**	about them.
83825a99e2eSeric 		*/
83925a99e2eSeric 
84057fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
84125a99e2eSeric 		{
8421d8f1806Seric 			stripquotes(user);
8431d8f1806Seric 			stripquotes(host);
84425a99e2eSeric 		}
84525a99e2eSeric 
846cdb828c5Seric 		/* hack attack -- delivermail compatibility */
847cdb828c5Seric 		if (m == ProgMailer && *user == '|')
848cdb828c5Seric 			user++;
849cdb828c5Seric 
85025a99e2eSeric 		/*
8513efaed6eSeric 		**  If an error message has already been given, don't
8523efaed6eSeric 		**	bother to send to this address.
8533efaed6eSeric 		**
8543efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
8553efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
8563efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
8573efaed6eSeric 		*/
8583efaed6eSeric 
8596cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8603efaed6eSeric 			continue;
8613efaed6eSeric 
862f2fec898Seric 		/* save statistics.... */
863588cad61Seric 		markstats(e, to);
864f2fec898Seric 
8653efaed6eSeric 		/*
86625a99e2eSeric 		**  See if this user name is "special".
86725a99e2eSeric 		**	If the user name has a slash in it, assume that this
86851552439Seric 		**	is a file -- send it off without further ado.  Note
86951552439Seric 		**	that this type of addresses is not processed along
87051552439Seric 		**	with the others, so we fudge on the To person.
87125a99e2eSeric 		*/
87225a99e2eSeric 
8732c017f8dSeric 		if (m == FileMailer)
87425a99e2eSeric 		{
87503463a75Seric 			rcode = mailfile(user, ctladdr, e);
87664187506Seric 			giveresponse(rcode, m, NULL, ctladdr, xstart, e);
87764187506Seric 			e->e_nsent++;
878dde5acadSeric 			if (rcode == EX_OK)
87964187506Seric 			{
880dde5acadSeric 				to->q_flags |= QSENT;
88164187506Seric 				if (bitnset(M_LOCALMAILER, m->m_flags) &&
88264187506Seric 				    (e->e_receiptto != NULL ||
88364187506Seric 				     bitset(QPINGONSUCCESS, to->q_flags)))
88464187506Seric 				{
88564187506Seric 					to->q_flags |= QREPORT;
88664187506Seric 					fprintf(e->e_xfp, "%s... Successfully delivered\n",
88764187506Seric 						to->q_paddr);
88864187506Seric 				}
88964187506Seric 			}
89064187506Seric 			to->q_statdate = curtime();
8915dfc646bSeric 			continue;
89225a99e2eSeric 		}
89325a99e2eSeric 
89413bbc08cSeric 		/*
89513bbc08cSeric 		**  Address is verified -- add this user to mailer
89613bbc08cSeric 		**  argv, and add it to the print list of recipients.
89713bbc08cSeric 		*/
89813bbc08cSeric 
899508daeccSeric 		/* link together the chain of recipients */
900508daeccSeric 		to->q_tchain = tochain;
901508daeccSeric 		tochain = to;
902508daeccSeric 
9035dfc646bSeric 		/* create list of users for error messages */
904db8841e9Seric 		(void) strcat(tobuf, ",");
905db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
906588cad61Seric 		define('u', user, e);		/* to user */
907b6f89e6eSeric 		p = to->q_home;
908b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
909b6f89e6eSeric 			p = ctladdr->q_home;
910b6f89e6eSeric 		define('z', p, e);	/* user's home */
9115dfc646bSeric 
912c579ef51Seric 		/*
913508daeccSeric 		**  Expand out this user into argument list.
914c579ef51Seric 		*/
915c579ef51Seric 
916508daeccSeric 		if (!clever)
917c579ef51Seric 		{
918fbf6dabfSeric 			expand(*mvp, buf, sizeof buf, e);
9195dfc646bSeric 			*pvp++ = newstr(buf);
9205dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
9215dfc646bSeric 			{
9225dfc646bSeric 				/* allow some space for trailing parms */
9235dfc646bSeric 				break;
9245dfc646bSeric 			}
9255dfc646bSeric 		}
926c579ef51Seric 	}
9275dfc646bSeric 
928145b49b1Seric 	/* see if any addresses still exist */
929145b49b1Seric 	if (tobuf[0] == '\0')
930c579ef51Seric 	{
931588cad61Seric 		define('g', (char *) NULL, e);
932145b49b1Seric 		return (0);
933c579ef51Seric 	}
934145b49b1Seric 
9355dfc646bSeric 	/* print out messages as full list */
93663780dbdSeric 	e->e_to = tobuf + 1;
9375dfc646bSeric 
9385dfc646bSeric 	/*
9395dfc646bSeric 	**  Fill out any parameters after the $u parameter.
9405dfc646bSeric 	*/
9415dfc646bSeric 
942c579ef51Seric 	while (!clever && *++mvp != NULL)
9435dfc646bSeric 	{
944fbf6dabfSeric 		expand(*mvp, buf, sizeof buf, e);
9455dfc646bSeric 		*pvp++ = newstr(buf);
9465dfc646bSeric 		if (pvp >= &pv[MAXPV])
94708b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
9485dfc646bSeric 	}
9495dfc646bSeric 	*pvp++ = NULL;
9505dfc646bSeric 
95125a99e2eSeric 	/*
95225a99e2eSeric 	**  Call the mailer.
9536328bdf7Seric 	**	The argument vector gets built, pipes
95425a99e2eSeric 	**	are created as necessary, and we fork & exec as
9556328bdf7Seric 	**	appropriate.
956c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
95725a99e2eSeric 	*/
95825a99e2eSeric 
959960e665aSeric 	/*XXX this seems a bit wierd */
9604899f6b5Seric 	if (ctladdr == NULL && m != ProgMailer &&
9614899f6b5Seric 	    bitset(QGOODUID, e->e_from.q_flags))
962960e665aSeric 		ctladdr = &e->e_from;
963960e665aSeric 
9649d4a8008Seric #if NAMED_BIND
9652bcc6d2dSeric 	if (ConfigLevel < 2)
966912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
967134746fbSeric #endif
9689c9e68d9Seric 
9699c9e68d9Seric 	if (tTd(11, 1))
970134746fbSeric 	{
9719c9e68d9Seric 		printf("openmailer:");
9729c9e68d9Seric 		printav(pv);
9739c9e68d9Seric 	}
9749c9e68d9Seric 	errno = 0;
97564187506Seric #if NAMED_BIND
97664187506Seric 	h_errno = 0;
97764187506Seric #endif
9789c9e68d9Seric 
97964187506Seric 	CurHostName = NULL;
9809c9e68d9Seric 
9819c9e68d9Seric 	/*
9829c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9839c9e68d9Seric 	**  connection.
9849c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9859c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9869c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9879c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9889c9e68d9Seric 	*/
9899c9e68d9Seric 
9909c9e68d9Seric 	curhost = NULL;
991c931b82bSeric 	SmtpPhase = NULL;
992df106f0bSeric 	mci = NULL;
9939c9e68d9Seric 
9947febfd66Seric #ifdef XDEBUG
9957febfd66Seric 	{
9967febfd66Seric 		char wbuf[MAXLINE];
9977febfd66Seric 
9987febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9997febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
10007febfd66Seric 		checkfd012(wbuf);
10017febfd66Seric 	}
10027febfd66Seric #endif
10037febfd66Seric 
100464187506Seric 	/* check for 8-bit available */
100564187506Seric 	if (bitset(EF_HAS8BIT, e->e_flags) &&
100664187506Seric 	    bitnset(M_7BITS, m->m_flags) &&
100764187506Seric 	    !bitset(MM_MIME8BIT, MimeMode))
100864187506Seric 	{
100964187506Seric 		usrerr("554 Cannot send 8-bit data to 7-bit destination");
101064187506Seric 		rcode = EX_DATAERR;
101164187506Seric 		goto give_up;
101264187506Seric 	}
101364187506Seric 
10149c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
10159c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
10169c9e68d9Seric 	{
10179c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
10189c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
10199c9e68d9Seric 		mci->mci_in = stdin;
10209c9e68d9Seric 		mci->mci_out = stdout;
10219c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
10229c9e68d9Seric 		mci->mci_mailer = m;
10239c9e68d9Seric 	}
10249c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
10259c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
10269c9e68d9Seric 	{
10279c9e68d9Seric #ifdef DAEMON
10289c9e68d9Seric 		register int i;
102964187506Seric 		register u_short port = 0;
10309c9e68d9Seric 
1031a5d44642Seric 		if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
1032a5d44642Seric 		{
1033a5d44642Seric 			syserr("null host name for %s mailer", m->m_mailer);
1034a5d44642Seric 			rcode = EX_CONFIG;
1035a5d44642Seric 			goto give_up;
1036a5d44642Seric 		}
1037a5d44642Seric 
10389c9e68d9Seric 		CurHostName = pv[1];
10399c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
10409c9e68d9Seric 
10419c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
10429c9e68d9Seric 		{
1043a5d44642Seric 			syserr("null host signature for %s", pv[1]);
1044dfebe401Seric 			rcode = EX_CONFIG;
1045b31e7f2bSeric 			goto give_up;
1046b31e7f2bSeric 		}
10479c9e68d9Seric 
10489c9e68d9Seric 		if (!clever)
10499c9e68d9Seric 		{
10509c9e68d9Seric 			syserr("554 non-clever IPC");
1051df106f0bSeric 			rcode = EX_CONFIG;
10529c9e68d9Seric 			goto give_up;
10539c9e68d9Seric 		}
10549c9e68d9Seric 		if (pv[2] != NULL)
10559c9e68d9Seric 			port = atoi(pv[2]);
10569c9e68d9Seric tryhost:
10579c9e68d9Seric 		while (*curhost != '\0')
10589c9e68d9Seric 		{
10599c9e68d9Seric 			register char *p;
1060a524e4d0Seric 			static char hostbuf[MAXNAME + 1];
10619c9e68d9Seric 
10629c9e68d9Seric 			/* pull the next host from the signature */
10639c9e68d9Seric 			p = strchr(curhost, ':');
10649c9e68d9Seric 			if (p == NULL)
10659c9e68d9Seric 				p = &curhost[strlen(curhost)];
1066df106f0bSeric 			if (p == curhost)
1067df106f0bSeric 			{
1068df106f0bSeric 				syserr("deliver: null host name in signature");
10698087428aSeric 				curhost++;
1070df106f0bSeric 				continue;
1071df106f0bSeric 			}
10729c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
10739c9e68d9Seric 			hostbuf[p - curhost] = '\0';
10749c9e68d9Seric 			if (*p != '\0')
10759c9e68d9Seric 				p++;
10769c9e68d9Seric 			curhost = p;
10779c9e68d9Seric 
10789c9e68d9Seric 			/* see if we already know that this host is fried */
10799c9e68d9Seric 			CurHostName = hostbuf;
10809c9e68d9Seric 			mci = mci_get(hostbuf, m);
10819c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
10829c9e68d9Seric 			{
10839c9e68d9Seric 				if (tTd(11, 1))
10849c9e68d9Seric 				{
10859c9e68d9Seric 					printf("openmailer: ");
10864052916eSeric 					mci_dump(mci, FALSE);
10879c9e68d9Seric 				}
10889c9e68d9Seric 				CurHostName = mci->mci_host;
108964187506Seric 				message("Using cached connection to %s via %s...",
109064187506Seric 					hostbuf, m->m_name);
10919c9e68d9Seric 				break;
10929c9e68d9Seric 			}
10939c9e68d9Seric 			mci->mci_mailer = m;
10949c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
10959c9e68d9Seric 				continue;
10969c9e68d9Seric 
10979c9e68d9Seric 			/* try the connection */
10989c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
109964187506Seric 			message("Connecting to %s via %s...",
11009c9e68d9Seric 				hostbuf, m->m_name);
11019c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
11029c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
11039c9e68d9Seric 			mci->mci_exitstat = i;
11049c9e68d9Seric 			mci->mci_errno = errno;
11059d4a8008Seric #if NAMED_BIND
1106f170942cSeric 			mci->mci_herrno = h_errno;
1107f170942cSeric #endif
11089c9e68d9Seric 			if (i == EX_OK)
11099c9e68d9Seric 			{
11109c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
11119c9e68d9Seric 				mci_cache(mci);
1112f170942cSeric 				if (TrafficLogFile != NULL)
1113f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1114f170942cSeric 						getpid(), hostbuf);
11159c9e68d9Seric 				break;
11169c9e68d9Seric 			}
11179c9e68d9Seric 			else if (tTd(11, 1))
11189c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
11199c9e68d9Seric 					i, errno);
11209c9e68d9Seric 
11219c9e68d9Seric 			/* enter status of this host */
11229c9e68d9Seric 			setstat(i);
1123df106f0bSeric 
1124df106f0bSeric 			/* should print some message here for -v mode */
1125df106f0bSeric 		}
1126df106f0bSeric 		if (mci == NULL)
1127df106f0bSeric 		{
1128df106f0bSeric 			syserr("deliver: no host name");
1129df106f0bSeric 			rcode = EX_OSERR;
1130df106f0bSeric 			goto give_up;
11319c9e68d9Seric 		}
11329c9e68d9Seric 		mci->mci_pid = 0;
11339c9e68d9Seric #else /* no DAEMON */
11349c9e68d9Seric 		syserr("554 openmailer: no IPC");
11359c9e68d9Seric 		if (tTd(11, 1))
11369c9e68d9Seric 			printf("openmailer: NULL\n");
1137df106f0bSeric 		rcode = EX_UNAVAILABLE;
1138df106f0bSeric 		goto give_up;
11399c9e68d9Seric #endif /* DAEMON */
11409c9e68d9Seric 	}
11419c9e68d9Seric 	else
11429c9e68d9Seric 	{
114364187506Seric 		/* flush any expired connections */
114464187506Seric 		(void) mci_scan(NULL);
114564187506Seric 
114664187506Seric 		/* announce the connection to verbose listeners */
114764187506Seric 		if (host == NULL || host[0] == '\0')
114864187506Seric 			message("Connecting to %s...", m->m_name);
114964187506Seric 		else
115064187506Seric 			message("Connecting to %s via %s...", host, m->m_name);
1151f170942cSeric 		if (TrafficLogFile != NULL)
11520e3bfef5Seric 		{
1153f170942cSeric 			char **av;
1154f170942cSeric 
1155f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1156f170942cSeric 			for (av = pv; *av != NULL; av++)
1157f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1158f170942cSeric 			fprintf(TrafficLogFile, "\n");
11596fe8c3bcSeric 		}
11606fe8c3bcSeric 
11619c9e68d9Seric 		/* create a pipe to shove the mail through */
11629c9e68d9Seric 		if (pipe(mpvect) < 0)
11639c9e68d9Seric 		{
11640e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
11650e3bfef5Seric 				e->e_to, m->m_name);
11669c9e68d9Seric 			if (tTd(11, 1))
11679c9e68d9Seric 				printf("openmailer: NULL\n");
11689c9e68d9Seric 			rcode = EX_OSERR;
11699c9e68d9Seric 			goto give_up;
11709c9e68d9Seric 		}
11719c9e68d9Seric 
11729c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
11739c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
11749c9e68d9Seric 		{
11750e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
11760e3bfef5Seric 				e->e_to, m->m_name);
11779c9e68d9Seric 			(void) close(mpvect[0]);
11789c9e68d9Seric 			(void) close(mpvect[1]);
11799c9e68d9Seric 			if (tTd(11, 1))
11809c9e68d9Seric 				printf("openmailer: NULL\n");
11819c9e68d9Seric 			rcode = EX_OSERR;
11829c9e68d9Seric 			goto give_up;
11839c9e68d9Seric 		}
11849c9e68d9Seric 
11859c9e68d9Seric 		/*
11869c9e68d9Seric 		**  Actually fork the mailer process.
11879c9e68d9Seric 		**	DOFORK is clever about retrying.
11889c9e68d9Seric 		**
11899c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
11909c9e68d9Seric 		**	around so that endmail will get it.
11919c9e68d9Seric 		*/
11929c9e68d9Seric 
11939c9e68d9Seric 		if (e->e_xfp != NULL)
11949c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
11959c9e68d9Seric 		(void) fflush(stdout);
11969c9e68d9Seric # ifdef SIGCHLD
11972b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
11989c9e68d9Seric # endif /* SIGCHLD */
11999c9e68d9Seric 		DOFORK(FORK);
12009c9e68d9Seric 		/* pid is set by DOFORK */
12019c9e68d9Seric 		if (pid < 0)
12029c9e68d9Seric 		{
12039c9e68d9Seric 			/* failure */
12040e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
12050e3bfef5Seric 				e->e_to, m->m_name);
12069c9e68d9Seric 			(void) close(mpvect[0]);
12079c9e68d9Seric 			(void) close(mpvect[1]);
12089c9e68d9Seric 			if (clever)
12099c9e68d9Seric 			{
12109c9e68d9Seric 				(void) close(rpvect[0]);
12119c9e68d9Seric 				(void) close(rpvect[1]);
12129c9e68d9Seric 			}
12139c9e68d9Seric 			if (tTd(11, 1))
12149c9e68d9Seric 				printf("openmailer: NULL\n");
12159c9e68d9Seric 			rcode = EX_OSERR;
12169c9e68d9Seric 			goto give_up;
12179c9e68d9Seric 		}
12189c9e68d9Seric 		else if (pid == 0)
12199c9e68d9Seric 		{
12209c9e68d9Seric 			int i;
12219c9e68d9Seric 			int saveerrno;
12229c9e68d9Seric 			char **ep;
12239c9e68d9Seric 			char *env[MAXUSERENVIRON];
12249c9e68d9Seric 			extern char **environ;
12259c9e68d9Seric 			extern int DtableSize;
12269c9e68d9Seric 
1227f2e4d5e8Seric 			if (e->e_lockfp != NULL)
1228*ac5a49f9Seric 				(void) close(fileno(e->e_lockfp));
1229f2e4d5e8Seric 
12309c9e68d9Seric 			/* child -- set up input & exec mailer */
12312b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
12322b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
12332b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
12349c9e68d9Seric 
123544f2317fSeric 			/* reset user and group */
123664187506Seric 			if (bitnset(M_SPECIFIC_UID, m->m_flags))
1237acc57dbfSeric 			{
123864187506Seric 				(void) setgid(m->m_gid);
123964187506Seric 				(void) setuid(m->m_uid);
1240acc57dbfSeric 			}
124164187506Seric 			else if (ctladdr != NULL && ctladdr->q_uid != 0)
124244f2317fSeric 			{
124344f2317fSeric 				(void) initgroups(ctladdr->q_ruser?
124444f2317fSeric 					ctladdr->q_ruser: ctladdr->q_user,
124544f2317fSeric 					ctladdr->q_gid);
124651cfe111Seric 				(void) setgid(ctladdr->q_gid);
124744f2317fSeric 				(void) setuid(ctladdr->q_uid);
124844f2317fSeric 			}
124964187506Seric 			else
125064187506Seric 			{
125164187506Seric 				(void) initgroups(DefUser, DefGid);
125264187506Seric 				if (m->m_gid == 0)
125364187506Seric 					(void) setgid(DefGid);
125464187506Seric 				else
125564187506Seric 					(void) setgid(m->m_gid);
125664187506Seric 				if (m->m_uid == 0)
125764187506Seric 					(void) setuid(DefUid);
125864187506Seric 				else
125964187506Seric 					(void) setuid(m->m_uid);
126044f2317fSeric 			}
126144f2317fSeric 
126244f2317fSeric 			if (tTd(11, 2))
126344f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
126444f2317fSeric 					getuid(), geteuid());
126544f2317fSeric 
1266b986f6aaSeric 			/* move into some "safe" directory */
1267b986f6aaSeric 			if (m->m_execdir != NULL)
1268b986f6aaSeric 			{
1269b986f6aaSeric 				char *p, *q;
1270a524e4d0Seric 				char buf[MAXLINE + 1];
1271b986f6aaSeric 
1272b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1273b986f6aaSeric 				{
1274b986f6aaSeric 					q = strchr(p, ':');
1275b986f6aaSeric 					if (q != NULL)
1276b986f6aaSeric 						*q = '\0';
1277fbf6dabfSeric 					expand(p, buf, sizeof buf, e);
1278b986f6aaSeric 					if (q != NULL)
1279b986f6aaSeric 						*q++ = ':';
1280b986f6aaSeric 					if (tTd(11, 20))
1281b986f6aaSeric 						printf("openmailer: trydir %s\n",
1282b986f6aaSeric 							buf);
1283b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1284b986f6aaSeric 						break;
1285b986f6aaSeric 				}
1286b986f6aaSeric 			}
1287b986f6aaSeric 
12889c9e68d9Seric 			/* arrange to filter std & diag output of command */
12899c9e68d9Seric 			if (clever)
12909c9e68d9Seric 			{
12919c9e68d9Seric 				(void) close(rpvect[0]);
12926fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
12936fe8c3bcSeric 				{
12940e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
12950e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
12966fe8c3bcSeric 					_exit(EX_OSERR);
12976fe8c3bcSeric 				}
12989c9e68d9Seric 				(void) close(rpvect[1]);
12999c9e68d9Seric 			}
13008b40b0a4Seric 			else if (OpMode == MD_SMTP || OpMode == MD_DAEMON ||
13018b40b0a4Seric 				  HoldErrs || DisConnected)
13029c9e68d9Seric 			{
13039c9e68d9Seric 				/* put mailer output in transcript */
13046fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
13056fe8c3bcSeric 				{
13060e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
13070e3bfef5Seric 						e->e_to, m->m_name,
13086fe8c3bcSeric 						fileno(e->e_xfp));
13096fe8c3bcSeric 					_exit(EX_OSERR);
13109c9e68d9Seric 				}
13116fe8c3bcSeric 			}
13126fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
13136fe8c3bcSeric 			{
13140e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
13150e3bfef5Seric 					e->e_to, m->m_name);
13166fe8c3bcSeric 				_exit(EX_OSERR);
13176fe8c3bcSeric 			}
13189c9e68d9Seric 
13199c9e68d9Seric 			/* arrange to get standard input */
13209c9e68d9Seric 			(void) close(mpvect[1]);
13219c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
13229c9e68d9Seric 			{
13230e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
13240e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
13259c9e68d9Seric 				_exit(EX_OSERR);
13269c9e68d9Seric 			}
13279c9e68d9Seric 			(void) close(mpvect[0]);
13289c9e68d9Seric 
13299c9e68d9Seric 			/* arrange for all the files to be closed */
13309c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
13319c9e68d9Seric 			{
13329c9e68d9Seric 				register int j;
133344f2317fSeric 
13349c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
13359c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
13369c9e68d9Seric 			}
13379c9e68d9Seric 
13389f9b003eSeric 			/*
13399f9b003eSeric 			**  Set up the mailer environment
134064187506Seric 			**	_FORCE_MAIL_LOCAL_ is DG-UX equiv of -d flag.
13419f9b003eSeric 			**	TZ is timezone information.
13429f9b003eSeric 			**	SYSTYPE is Apollo software sys type (required).
13439f9b003eSeric 			**	ISP is Apollo hardware system type (required).
13449f9b003eSeric 			*/
13459f9b003eSeric 
13469c9e68d9Seric 			i = 0;
13479c9e68d9Seric 			env[i++] = "AGENT=sendmail";
134864187506Seric 			env[i++] = "_FORCE_MAIL_LOCAL_=yes";
13499c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
13509c9e68d9Seric 			{
13519f9b003eSeric 				if (strncmp(*ep, "TZ=", 3) == 0 ||
13529f9b003eSeric 				    strncmp(*ep, "ISP=", 4) == 0 ||
13539f9b003eSeric 				    strncmp(*ep, "SYSTYPE=", 8) == 0)
13549c9e68d9Seric 					env[i++] = *ep;
13559c9e68d9Seric 			}
135664187506Seric 			env[i] = NULL;
13579c9e68d9Seric 
1358929277f2Seric 			/* run disconnected from terminal */
1359929277f2Seric 			(void) setsid();
1360929277f2Seric 
13619c9e68d9Seric 			/* try to execute the mailer */
13629c9e68d9Seric 			execve(m->m_mailer, pv, env);
13639c9e68d9Seric 			saveerrno = errno;
13649c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
136564187506Seric 			if (bitnset(M_LOCALMAILER, m->m_flags) ||
136664187506Seric 			    transienterror(saveerrno))
13677c941fd2Seric 				_exit(EX_OSERR);
13689c9e68d9Seric 			_exit(EX_UNAVAILABLE);
13699c9e68d9Seric 		}
13709c9e68d9Seric 
13719c9e68d9Seric 		/*
13729c9e68d9Seric 		**  Set up return value.
13739c9e68d9Seric 		*/
13749c9e68d9Seric 
13759c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
13769c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
13779c9e68d9Seric 		mci->mci_mailer = m;
13789c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
13799c9e68d9Seric 		mci->mci_pid = pid;
13809c9e68d9Seric 		(void) close(mpvect[0]);
13819c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1382335eae58Seric 		if (mci->mci_out == NULL)
1383335eae58Seric 		{
1384335eae58Seric 			syserr("deliver: cannot create mailer output channel, fd=%d",
1385335eae58Seric 				mpvect[1]);
1386335eae58Seric 			(void) close(mpvect[1]);
1387335eae58Seric 			if (clever)
1388335eae58Seric 			{
1389335eae58Seric 				(void) close(rpvect[0]);
1390335eae58Seric 				(void) close(rpvect[1]);
1391335eae58Seric 			}
1392335eae58Seric 			rcode = EX_OSERR;
1393335eae58Seric 			goto give_up;
1394335eae58Seric 		}
13959c9e68d9Seric 		if (clever)
13969c9e68d9Seric 		{
13979c9e68d9Seric 			(void) close(rpvect[1]);
13989c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
1399335eae58Seric 			if (mci->mci_in == NULL)
1400335eae58Seric 			{
1401335eae58Seric 				syserr("deliver: cannot create mailer input channel, fd=%d",
1402335eae58Seric 					mpvect[1]);
1403335eae58Seric 				(void) close(rpvect[0]);
1404335eae58Seric 				fclose(mci->mci_out);
1405335eae58Seric 				mci->mci_out = NULL;
1406335eae58Seric 				rcode = EX_OSERR;
1407335eae58Seric 				goto give_up;
1408335eae58Seric 			}
14099c9e68d9Seric 		}
14109c9e68d9Seric 		else
14119c9e68d9Seric 		{
14129c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
14139c9e68d9Seric 			mci->mci_in = NULL;
14149c9e68d9Seric 		}
14159c9e68d9Seric 	}
14169c9e68d9Seric 
14179c9e68d9Seric 	/*
14189c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
14199c9e68d9Seric 	*/
14209c9e68d9Seric 
14219c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
14229c9e68d9Seric 	{
14239c9e68d9Seric 		smtpinit(m, mci, e);
14249c9e68d9Seric 	}
142564187506Seric 
142664187506Seric 	if (bitset(EF_HAS8BIT, e->e_flags) && bitnset(M_7BITS, m->m_flags))
142764187506Seric 		mci->mci_flags |= MCIF_CVT8TO7;
142864187506Seric 	else
142964187506Seric 		mci->mci_flags &= ~MCIF_CVT8TO7;
143064187506Seric 
14319c9e68d9Seric 	if (tTd(11, 1))
14329c9e68d9Seric 	{
14339c9e68d9Seric 		printf("openmailer: ");
14344052916eSeric 		mci_dump(mci, FALSE);
14359c9e68d9Seric 	}
14369c9e68d9Seric 
14379c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1438b31e7f2bSeric 	{
1439b31e7f2bSeric 		/* couldn't open the mailer */
1440b31e7f2bSeric 		rcode = mci->mci_exitstat;
14412a6bc25bSeric 		errno = mci->mci_errno;
14429d4a8008Seric #if NAMED_BIND
1443f170942cSeric 		h_errno = mci->mci_herrno;
1444f170942cSeric #endif
1445b31e7f2bSeric 		if (rcode == EX_OK)
1446b31e7f2bSeric 		{
1447b31e7f2bSeric 			/* shouldn't happen */
144808b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
14496b0f339dSeric 				rcode, mci->mci_state, firstsig);
1450b31e7f2bSeric 			rcode = EX_SOFTWARE;
1451b31e7f2bSeric 		}
145243357751Seric 		else if (curhost != NULL && *curhost != '\0')
145390891494Seric 		{
145490891494Seric 			/* try next MX site */
145590891494Seric 			goto tryhost;
145690891494Seric 		}
1457b31e7f2bSeric 	}
1458b31e7f2bSeric 	else if (!clever)
1459b31e7f2bSeric 	{
1460b31e7f2bSeric 		/*
1461b31e7f2bSeric 		**  Format and send message.
1462b31e7f2bSeric 		*/
146315d084d5Seric 
14645aa0f353Seric 		putfromline(mci, e);
146564187506Seric 		(*e->e_puthdr)(mci, e->e_header, e);
14666ffc2dd1Seric 		(*e->e_putbody)(mci, e, NULL);
1467b31e7f2bSeric 
1468b31e7f2bSeric 		/* get the exit status */
1469c9be6216Seric 		rcode = endmailer(mci, e, pv);
1470134746fbSeric 	}
1471134746fbSeric 	else
1472b31e7f2bSeric #ifdef SMTP
1473134746fbSeric 	{
1474b31e7f2bSeric 		/*
1475b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1476b31e7f2bSeric 		*/
147715d084d5Seric 
1478b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1479b31e7f2bSeric 		if (rcode == EX_OK)
148075889e88Seric 		{
1481ded0d3daSkarels 			register char *t = tobuf;
1482ded0d3daSkarels 			register int i;
1483ded0d3daSkarels 
1484588cad61Seric 			/* send the recipient list */
148563780dbdSeric 			tobuf[0] = '\0';
148675889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
148775889e88Seric 			{
148863780dbdSeric 				e->e_to = to->q_paddr;
148915d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
149075889e88Seric 				{
149164187506Seric 					markfailure(e, to, mci, i);
149264187506Seric 					giveresponse(i, m, mci, ctladdr, xstart, e);
149363780dbdSeric 				}
149475889e88Seric 				else
149575889e88Seric 				{
1496911693bfSbostic 					*t++ = ',';
1497b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1498b31e7f2bSeric 						continue;
1499ee39df61Seric 					*t = '\0';
1500588cad61Seric 				}
1501588cad61Seric 			}
1502588cad61Seric 
150363780dbdSeric 			/* now send the data */
150463780dbdSeric 			if (tobuf[0] == '\0')
1505b31e7f2bSeric 			{
15069c9e68d9Seric 				rcode = EX_OK;
150763780dbdSeric 				e->e_to = NULL;
1508b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1509b31e7f2bSeric 					smtprset(m, mci, e);
1510b31e7f2bSeric 			}
151175889e88Seric 			else
151275889e88Seric 			{
151363780dbdSeric 				e->e_to = tobuf + 1;
151475889e88Seric 				rcode = smtpdata(m, mci, e);
151563780dbdSeric 			}
151663780dbdSeric 
151763780dbdSeric 			/* now close the connection */
1518b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
151915d084d5Seric 				smtpquit(m, mci, e);
152063780dbdSeric 		}
1521cb082c5bSeric 		if (rcode != EX_OK && curhost != NULL && *curhost != '\0')
15229c9e68d9Seric 		{
15239c9e68d9Seric 			/* try next MX site */
15249c9e68d9Seric 			goto tryhost;
15259c9e68d9Seric 		}
1526c579ef51Seric 	}
1527b31e7f2bSeric #else /* not SMTP */
1528a05b3449Sbostic 	{
152908b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1530845e533cSeric 		rcode = EX_CONFIG;
1531b31e7f2bSeric 		goto give_up;
1532a05b3449Sbostic 	}
1533b31e7f2bSeric #endif /* SMTP */
15349d4a8008Seric #if NAMED_BIND
15352bcc6d2dSeric 	if (ConfigLevel < 2)
1536912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1537134746fbSeric #endif
15385dfc646bSeric 
1539b31e7f2bSeric 	/* arrange a return receipt if requested */
1540df106f0bSeric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1541df106f0bSeric 	    bitnset(M_LOCALMAILER, m->m_flags))
1542b31e7f2bSeric 	{
1543b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1544b31e7f2bSeric 		/* do we want to send back more info? */
1545b31e7f2bSeric 	}
1546b31e7f2bSeric 
1547c77d1c25Seric 	/*
154863780dbdSeric 	**  Do final status disposal.
154963780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1550c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1551c77d1c25Seric 	**		addressees.
1552c77d1c25Seric 	*/
1553c77d1c25Seric 
1554b31e7f2bSeric   give_up:
155563780dbdSeric 	if (tobuf[0] != '\0')
155664187506Seric 		giveresponse(rcode, m, mci, ctladdr, xstart, e);
1557772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1558b31e7f2bSeric 	{
1559dde5acadSeric 		if (rcode != EX_OK)
156064187506Seric 			markfailure(e, to, mci, rcode);
156164187506Seric 		else if (!bitset(QBADADDR|QQUEUEUP, to->q_flags))
1562655518ecSeric 		{
1563dde5acadSeric 			to->q_flags |= QSENT;
156464187506Seric 			to->q_statdate = curtime();
1565655518ecSeric 			e->e_nsent++;
156664187506Seric 			if (bitnset(M_LOCALMAILER, m->m_flags) &&
156764187506Seric 			    (e->e_receiptto != NULL ||
156864187506Seric 			     bitset(QPINGONSUCCESS, to->q_flags)))
1569df106f0bSeric 			{
157064187506Seric 				to->q_flags |= QREPORT;
1571df106f0bSeric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1572df106f0bSeric 					to->q_paddr);
1573df106f0bSeric 			}
157464187506Seric 			else if (bitset(QPINGONSUCCESS, to->q_flags) &&
157564187506Seric 				 !bitset(MCIF_DSN, mci->mci_flags))
157664187506Seric 			{
157764187506Seric 				to->q_flags |= QRELAYED;
157864187506Seric 				fprintf(e->e_xfp, "%s... relayed; expect no further notifications\n",
157964187506Seric 					to->q_paddr);
158064187506Seric 			}
1581655518ecSeric 		}
1582b31e7f2bSeric 	}
1583b31e7f2bSeric 
1584b31e7f2bSeric 	/*
1585b31e7f2bSeric 	**  Restore state and return.
1586b31e7f2bSeric 	*/
1587c77d1c25Seric 
15887febfd66Seric #ifdef XDEBUG
15897febfd66Seric 	{
15907febfd66Seric 		char wbuf[MAXLINE];
15917febfd66Seric 
15927febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
159344622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
159444622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
159544622ea3Seric 			m->m_name);
15967febfd66Seric 		checkfd012(wbuf);
15977febfd66Seric 	}
15987febfd66Seric #endif
15997febfd66Seric 
160035490626Seric 	errno = 0;
1601588cad61Seric 	define('g', (char *) NULL, e);
16025826d9d3Seric 	return (rcode);
160325a99e2eSeric }
16045dfc646bSeric /*
160583b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
160683b7ddc9Seric **
160783b7ddc9Seric **	Parameters:
160883b7ddc9Seric **		e -- the envelope we are sending.
160983b7ddc9Seric **		q -- the address to mark.
161064187506Seric **		mci -- mailer connection information.
161183b7ddc9Seric **		rcode -- the code signifying the particular failure.
161283b7ddc9Seric **
161383b7ddc9Seric **	Returns:
161483b7ddc9Seric **		none.
161583b7ddc9Seric **
161683b7ddc9Seric **	Side Effects:
161783b7ddc9Seric **		marks the address (and possibly the envelope) with the
161883b7ddc9Seric **			failure so that an error will be returned or
161983b7ddc9Seric **			the message will be queued, as appropriate.
162083b7ddc9Seric */
162183b7ddc9Seric 
162264187506Seric markfailure(e, q, mci, rcode)
162383b7ddc9Seric 	register ENVELOPE *e;
162483b7ddc9Seric 	register ADDRESS *q;
162564187506Seric 	register MCI *mci;
162683b7ddc9Seric 	int rcode;
162783b7ddc9Seric {
162864187506Seric 	char *stat = NULL;
162919c47125Seric 
1630d5aafa3cSeric 	switch (rcode)
1631d5aafa3cSeric 	{
1632d5aafa3cSeric 	  case EX_OK:
1633d5aafa3cSeric 		break;
1634d5aafa3cSeric 
1635d5aafa3cSeric 	  case EX_TEMPFAIL:
1636d5aafa3cSeric 	  case EX_IOERR:
1637d5aafa3cSeric 	  case EX_OSERR:
163883b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1639d5aafa3cSeric 		break;
1640d5aafa3cSeric 
1641d5aafa3cSeric 	  default:
1642f170942cSeric 		q->q_flags |= QBADADDR;
1643d5aafa3cSeric 		break;
1644d5aafa3cSeric 	}
164564187506Seric 
164664187506Seric 	if (q->q_status == NULL && mci != NULL)
164764187506Seric 		q->q_status = mci->mci_status;
164864187506Seric 	switch (rcode)
164964187506Seric 	{
165064187506Seric 	  case EX_USAGE:
165164187506Seric 		stat = "5.5.4";
165264187506Seric 		break;
165364187506Seric 
165464187506Seric 	  case EX_DATAERR:
165564187506Seric 		stat = "5.5.2";
165664187506Seric 		break;
165764187506Seric 
165864187506Seric 	  case EX_NOUSER:
165964187506Seric 	  case EX_NOHOST:
166064187506Seric 		stat = "5.1.1";
166164187506Seric 		break;
166264187506Seric 
166364187506Seric 	  case EX_NOINPUT:
166464187506Seric 	  case EX_CANTCREAT:
166564187506Seric 	  case EX_NOPERM:
166664187506Seric 		stat = "5.3.0";
166764187506Seric 		break;
166864187506Seric 
166964187506Seric 	  case EX_UNAVAILABLE:
167064187506Seric 	  case EX_SOFTWARE:
167164187506Seric 	  case EX_OSFILE:
167264187506Seric 	  case EX_PROTOCOL:
167364187506Seric 	  case EX_CONFIG:
167464187506Seric 		stat = "5.5.0";
167564187506Seric 		break;
167664187506Seric 
167764187506Seric 	  case EX_OSERR:
167864187506Seric 	  case EX_IOERR:
167964187506Seric 		stat = "4.5.0";
168064187506Seric 		break;
168164187506Seric 
168264187506Seric 	  case EX_TEMPFAIL:
168364187506Seric 		stat = "4.2.0";
168464187506Seric 		break;
168564187506Seric 	}
168664187506Seric 	if (stat != NULL && q->q_status == NULL)
168764187506Seric 		q->q_status = stat;
168864187506Seric 
168964187506Seric 	q->q_statdate = curtime();
169064187506Seric 	if (CurHostName != NULL && CurHostName[0] != '\0')
169164187506Seric 		q->q_statmta = newstr(CurHostName);
169264187506Seric 	if (rcode != EX_OK && q->q_rstatus == NULL)
169364187506Seric 	{
169464187506Seric 		char buf[30];
169564187506Seric 
169664187506Seric 		(void) sprintf(buf, "%d", rcode);
169764187506Seric 		q->q_rstatus = newstr(buf);
169864187506Seric 	}
169983b7ddc9Seric }
170083b7ddc9Seric /*
1701c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1702c579ef51Seric **
1703c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1704c579ef51Seric **	violation), so we report those specially.  For other
1705c579ef51Seric **	errors, we choose a status message (into statmsg),
1706c579ef51Seric **	and if it represents an error, we print it.
1707c579ef51Seric **
1708c579ef51Seric **	Parameters:
1709c579ef51Seric **		pid -- pid of mailer.
1710c9be6216Seric **		e -- the current envelope.
1711c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1712c9be6216Seric **			(for error messages).
1713c579ef51Seric **
1714c579ef51Seric **	Returns:
1715c579ef51Seric **		exit code of mailer.
1716c579ef51Seric **
1717c579ef51Seric **	Side Effects:
1718c579ef51Seric **		none.
1719c579ef51Seric */
1720c579ef51Seric 
1721c9be6216Seric endmailer(mci, e, pv)
1722b31e7f2bSeric 	register MCI *mci;
1723c9be6216Seric 	register ENVELOPE *e;
1724c9be6216Seric 	char **pv;
1725c579ef51Seric {
1726588cad61Seric 	int st;
1727c579ef51Seric 
172875889e88Seric 	/* close any connections */
172975889e88Seric 	if (mci->mci_in != NULL)
173076f3d53fSeric 		(void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in");
173175889e88Seric 	if (mci->mci_out != NULL)
173276f3d53fSeric 		(void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out");
173375889e88Seric 	mci->mci_in = mci->mci_out = NULL;
173475889e88Seric 	mci->mci_state = MCIS_CLOSED;
173575889e88Seric 
173633db8731Seric 	/* in the IPC case there is nothing to wait for */
173775889e88Seric 	if (mci->mci_pid == 0)
173833db8731Seric 		return (EX_OK);
173933db8731Seric 
174033db8731Seric 	/* wait for the mailer process to die and collect status */
174175889e88Seric 	st = waitfor(mci->mci_pid);
1742588cad61Seric 	if (st == -1)
174378de67c1Seric 	{
1744c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1745588cad61Seric 		return (EX_SOFTWARE);
1746c579ef51Seric 	}
174733db8731Seric 
1748bf9bc890Seric 	if (WIFEXITED(st))
1749c579ef51Seric 	{
1750bf9bc890Seric 		/* normal death -- return status */
1751bf9bc890Seric 		return (WEXITSTATUS(st));
1752bf9bc890Seric 	}
1753bf9bc890Seric 
1754bf9bc890Seric 	/* it died a horrid death */
1755550092c3Seric 	syserr("451 mailer %s died with signal %o",
1756550092c3Seric 		mci->mci_mailer->m_name, st);
1757c9be6216Seric 
1758c9be6216Seric 	/* log the arguments */
17593c930db3Seric 	if (pv != NULL && e->e_xfp != NULL)
1760c9be6216Seric 	{
1761c9be6216Seric 		register char **av;
1762c9be6216Seric 
1763c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1764c9be6216Seric 		for (av = pv; *av != NULL; av++)
1765c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1766c9be6216Seric 		fprintf(e->e_xfp, "\n");
1767c9be6216Seric 	}
1768c9be6216Seric 
17695f73204aSeric 	ExitStat = EX_TEMPFAIL;
17705f73204aSeric 	return (EX_TEMPFAIL);
1771c579ef51Seric }
1772c579ef51Seric /*
177325a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
177425a99e2eSeric **
177525a99e2eSeric **	Parameters:
177625a99e2eSeric **		stat -- the status code from the mailer (high byte
177725a99e2eSeric **			only; core dumps must have been taken care of
177825a99e2eSeric **			already).
177981161401Seric **		m -- the mailer info for this mailer.
178081161401Seric **		mci -- the mailer connection info -- can be NULL if the
178181161401Seric **			response is given before the connection is made.
17824dee0003Seric **		ctladdr -- the controlling address for the recipient
17834dee0003Seric **			address(es).
178464187506Seric **		xstart -- the transaction start time, for computing
178564187506Seric **			transaction delays.
178681161401Seric **		e -- the current envelope.
178725a99e2eSeric **
178825a99e2eSeric **	Returns:
1789db8841e9Seric **		none.
179025a99e2eSeric **
179125a99e2eSeric **	Side Effects:
1792c1f9df2cSeric **		Errors may be incremented.
179325a99e2eSeric **		ExitStat may be set.
179425a99e2eSeric */
179525a99e2eSeric 
179664187506Seric giveresponse(stat, m, mci, ctladdr, xstart, e)
179725a99e2eSeric 	int stat;
1798588cad61Seric 	register MAILER *m;
179981161401Seric 	register MCI *mci;
18004dee0003Seric 	ADDRESS *ctladdr;
180164187506Seric 	time_t xstart;
1802198d9be0Seric 	ENVELOPE *e;
180325a99e2eSeric {
18049c16475dSeric 	register const char *statmsg;
180525a99e2eSeric 	extern char *SysExMsg[];
180625a99e2eSeric 	register int i;
1807d4bd8f0eSbostic 	extern int N_SysEx;
1808198d9be0Seric 	char buf[MAXLINE];
180925a99e2eSeric 
181013bbc08cSeric 	/*
181113bbc08cSeric 	**  Compute status message from code.
181213bbc08cSeric 	*/
181313bbc08cSeric 
181425a99e2eSeric 	i = stat - EX__BASE;
1815588cad61Seric 	if (stat == 0)
18166fe8c3bcSeric 	{
1817588cad61Seric 		statmsg = "250 Sent";
1818ce5531bdSeric 		if (e->e_statmsg != NULL)
18196fe8c3bcSeric 		{
1820ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
18216fe8c3bcSeric 			statmsg = buf;
18226fe8c3bcSeric 		}
18236fe8c3bcSeric 	}
1824588cad61Seric 	else if (i < 0 || i > N_SysEx)
1825588cad61Seric 	{
1826588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1827588cad61Seric 		stat = EX_UNAVAILABLE;
1828588cad61Seric 		statmsg = buf;
1829588cad61Seric 	}
1830198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1831198d9be0Seric 	{
18327d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
18339d4a8008Seric #if NAMED_BIND
1834f28da541Smiriam 		if (h_errno == TRY_AGAIN)
18354d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1836f28da541Smiriam 		else
1837d4bd8f0eSbostic #endif
1838f28da541Smiriam 		{
18398557d168Seric 			if (errno != 0)
1840d87e85f3Seric 				statmsg = errstring(errno);
1841d87e85f3Seric 			else
1842d87e85f3Seric 			{
1843d87e85f3Seric #ifdef SMTP
1844d87e85f3Seric 				statmsg = SmtpError;
18456c2c3107Seric #else /* SMTP */
1846d87e85f3Seric 				statmsg = NULL;
18476c2c3107Seric #endif /* SMTP */
1848d87e85f3Seric 			}
1849f28da541Smiriam 		}
1850d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1851d87e85f3Seric 		{
185287c9b3e7Seric 			(void) strcat(buf, ": ");
1853d87e85f3Seric 			(void) strcat(buf, statmsg);
18548557d168Seric 		}
1855198d9be0Seric 		statmsg = buf;
1856198d9be0Seric 	}
18579d4a8008Seric #if NAMED_BIND
1858f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1859f170942cSeric 	{
18604d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1861862934b2Seric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i] + 1, statmsg);
1862f170942cSeric 		statmsg = buf;
1863f170942cSeric 	}
1864f170942cSeric #endif
186525a99e2eSeric 	else
1866d87e85f3Seric 	{
186725a99e2eSeric 		statmsg = SysExMsg[i];
18687d55540cSeric 		if (*statmsg++ == ':')
18697d55540cSeric 		{
18707d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
18717d55540cSeric 			statmsg = buf;
18727d55540cSeric 		}
1873d87e85f3Seric 	}
1874588cad61Seric 
1875588cad61Seric 	/*
1876588cad61Seric 	**  Print the message as appropriate
1877588cad61Seric 	*/
1878588cad61Seric 
1879198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1880df106f0bSeric 	{
1881df106f0bSeric 		extern char MsgBuf[];
1882df106f0bSeric 
18831f96bd0bSeric 		message("%s", &statmsg[4]);
1884df106f0bSeric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1885df106f0bSeric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1886df106f0bSeric 	}
188725a99e2eSeric 	else
188825a99e2eSeric 	{
1889862934b2Seric 		char mbuf[8];
1890862934b2Seric 
1891c1f9df2cSeric 		Errors++;
1892862934b2Seric 		sprintf(mbuf, "%.3s %%s", statmsg);
1893862934b2Seric 		usrerr(mbuf, &statmsg[4]);
189425a99e2eSeric 	}
189525a99e2eSeric 
189625a99e2eSeric 	/*
189725a99e2eSeric 	**  Final cleanup.
189825a99e2eSeric 	**	Log a record of the transaction.  Compute the new
189925a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
190025a99e2eSeric 	**	that.
190125a99e2eSeric 	*/
190225a99e2eSeric 
19032f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
190464187506Seric 		logdelivery(m, mci, &statmsg[4], ctladdr, xstart, e);
1905eb238f8cSeric 
19065d3f0ed4Seric 	if (tTd(11, 2))
19075d3f0ed4Seric 		printf("giveresponse: stat=%d, e->e_message=%s\n",
19082c8ac7e0Seric 			stat, e->e_message == NULL ? "<NULL>" : e->e_message);
19095d3f0ed4Seric 
1910eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1911eb238f8cSeric 		setstat(stat);
19121097d4c9Seric 	if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL))
1913198d9be0Seric 	{
1914198d9be0Seric 		if (e->e_message != NULL)
1915198d9be0Seric 			free(e->e_message);
1916198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1917198d9be0Seric 	}
19188557d168Seric 	errno = 0;
19199d4a8008Seric #if NAMED_BIND
1920f28da541Smiriam 	h_errno = 0;
1921d4bd8f0eSbostic #endif
1922eb238f8cSeric }
1923eb238f8cSeric /*
1924eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1925eb238f8cSeric **
19262c9b4f99Seric **	Care is taken to avoid logging lines that are too long, because
19272c9b4f99Seric **	some versions of syslog have an unfortunate proclivity for core
19282c9b4f99Seric **	dumping.  This is a hack, to be sure, that is at best empirical.
19292c9b4f99Seric **
1930eb238f8cSeric **	Parameters:
193181161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
193281161401Seric **		mci -- the mailer connection info -- can be NULL if the
193381161401Seric **			log is occuring when no connection is active.
193481161401Seric **		stat -- the message to print for the status.
19354dee0003Seric **		ctladdr -- the controlling address for the to list.
193664187506Seric **		xstart -- the transaction start time, used for
193764187506Seric **			computing transaction delay.
193881161401Seric **		e -- the current envelope.
1939eb238f8cSeric **
1940eb238f8cSeric **	Returns:
1941eb238f8cSeric **		none
1942eb238f8cSeric **
1943eb238f8cSeric **	Side Effects:
1944eb238f8cSeric **		none
1945eb238f8cSeric */
1946eb238f8cSeric 
194764187506Seric logdelivery(m, mci, stat, ctladdr, xstart, e)
194881161401Seric 	MAILER *m;
194981161401Seric 	register MCI *mci;
1950eb238f8cSeric 	char *stat;
19514dee0003Seric 	ADDRESS *ctladdr;
195264187506Seric 	time_t xstart;
1953b31e7f2bSeric 	register ENVELOPE *e;
19545cf56be3Seric {
1955eb238f8cSeric # ifdef LOG
19564dee0003Seric 	register char *bp;
19572c9b4f99Seric 	register char *p;
19582c9b4f99Seric 	int l;
1959d6acf3eeSeric 	char buf[512];
19609507d1f9Seric 
19613a100e8bSeric #  if (SYSLOG_BUFSIZE) >= 256
19624dee0003Seric 	bp = buf;
19634dee0003Seric 	if (ctladdr != NULL)
19644dee0003Seric 	{
19654dee0003Seric 		strcpy(bp, ", ctladdr=");
1966f62f08c7Seric 		strcat(bp, shortenstring(ctladdr->q_paddr, 83));
19674dee0003Seric 		bp += strlen(bp);
19684dee0003Seric 		if (bitset(QGOODUID, ctladdr->q_flags))
19694dee0003Seric 		{
19704dee0003Seric 			(void) sprintf(bp, " (%d/%d)",
19714dee0003Seric 					ctladdr->q_uid, ctladdr->q_gid);
19724dee0003Seric 			bp += strlen(bp);
19734dee0003Seric 		}
19744dee0003Seric 	}
19754dee0003Seric 
197664187506Seric 	sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
19774dee0003Seric 	bp += strlen(bp);
197881161401Seric 
197964187506Seric 	if (xstart != (time_t) 0)
198064187506Seric 	{
198164187506Seric 		sprintf(bp, ", xdelay=%s", pintvl(curtime() - xstart, TRUE));
198264187506Seric 		bp += strlen(bp);
198364187506Seric 	}
198464187506Seric 
198548ed5d33Seric 	if (m != NULL)
198671ff6caaSeric 	{
19874dee0003Seric 		(void) strcpy(bp, ", mailer=");
19884dee0003Seric 		(void) strcat(bp, m->m_name);
19894dee0003Seric 		bp += strlen(bp);
199071ff6caaSeric 	}
199148ed5d33Seric 
199248ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
199371ff6caaSeric 	{
199471ff6caaSeric # ifdef DAEMON
1995e2f2f828Seric 		extern SOCKADDR CurHostAddr;
199648ed5d33Seric # endif
199771ff6caaSeric 
19984dee0003Seric 		(void) strcpy(bp, ", relay=");
19994dee0003Seric 		(void) strcat(bp, mci->mci_host);
200048ed5d33Seric 
200148ed5d33Seric # ifdef DAEMON
20022bdfc6b9Seric 		(void) strcat(bp, " [");
20034dee0003Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
20042bdfc6b9Seric 		(void) strcat(bp, "]");
200571ff6caaSeric # endif
200671ff6caaSeric 	}
2007bcb9b028Seric 	else if (strcmp(stat, "queued") != 0)
200848ed5d33Seric 	{
200948ed5d33Seric 		char *p = macvalue('h', e);
20109507d1f9Seric 
201148ed5d33Seric 		if (p != NULL && p[0] != '\0')
201248ed5d33Seric 		{
20134dee0003Seric 			(void) strcpy(bp, ", relay=");
20144dee0003Seric 			(void) strcat(bp, p);
201548ed5d33Seric 		}
201648ed5d33Seric 	}
2017cb082c5bSeric 	bp += strlen(bp);
2018d6acf3eeSeric 
20193a100e8bSeric #define STATLEN		(((SYSLOG_BUFSIZE) - 100) / 4)
20203a100e8bSeric #if (STATLEN) < 63
20213a100e8bSeric # undef STATLEN
20223a100e8bSeric # define STATLEN	63
20233a100e8bSeric #endif
20243a100e8bSeric #if (STATLEN) > 203
20253a100e8bSeric # undef STATLEN
20263a100e8bSeric # define STATLEN	203
20273a100e8bSeric #endif
20283a100e8bSeric 
20293a100e8bSeric 	if ((bp - buf) > (sizeof buf - ((STATLEN) + 20)))
20302c9b4f99Seric 	{
20312c9b4f99Seric 		/* desperation move -- truncate data */
20323a100e8bSeric 		bp = buf + sizeof buf - ((STATLEN) + 17);
20332c9b4f99Seric 		strcpy(bp, "...");
20342c9b4f99Seric 		bp += 3;
20352c9b4f99Seric 	}
20362c9b4f99Seric 
20372c9b4f99Seric 	(void) strcpy(bp, ", stat=");
20382c9b4f99Seric 	bp += strlen(bp);
20393a100e8bSeric 
20403a100e8bSeric 	(void) strcpy(bp, shortenstring(stat, (STATLEN)));
20412c9b4f99Seric 
20422c9b4f99Seric 	l = SYSLOG_BUFSIZE - 100 - strlen(buf);
20432c9b4f99Seric 	p = e->e_to;
20442c9b4f99Seric 	while (strlen(p) >= l)
20452c9b4f99Seric 	{
20462c9b4f99Seric 		register char *q = strchr(p + l, ',');
20472c9b4f99Seric 
20482a1b6b73Seric 		if (q == NULL)
20492c9b4f99Seric 			break;
20502c9b4f99Seric 		syslog(LOG_INFO, "%s: to=%.*s [more]%s",
20512c9b4f99Seric 			e->e_id, ++q - p, p, buf);
20522c9b4f99Seric 		p = q;
20532c9b4f99Seric 	}
20542c9b4f99Seric 	syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf);
20553a100e8bSeric 
20563a100e8bSeric #  else		/* we have a very short log buffer size */
20573a100e8bSeric 
205827607809Seric 	l = SYSLOG_BUFSIZE - 85;
20593a100e8bSeric 	p = e->e_to;
20603a100e8bSeric 	while (strlen(p) >= l)
20613a100e8bSeric 	{
20623a100e8bSeric 		register char *q = strchr(p + l, ',');
20633a100e8bSeric 
20643a100e8bSeric 		if (q == NULL)
20653a100e8bSeric 			break;
20663a100e8bSeric 		syslog(LOG_INFO, "%s: to=%.*s [more]",
20673a100e8bSeric 			e->e_id, ++q - p, p);
20683a100e8bSeric 		p = q;
20693a100e8bSeric 	}
20703a100e8bSeric 	syslog(LOG_INFO, "%s: to=%s", e->e_id, p);
20713a100e8bSeric 
20723a100e8bSeric 	if (ctladdr != NULL)
20733a100e8bSeric 	{
20743a100e8bSeric 		bp = buf;
20753a100e8bSeric 		strcpy(buf, "ctladdr=");
20763a100e8bSeric 		bp += strlen(buf);
20773a100e8bSeric 		strcpy(bp, shortenstring(ctladdr->q_paddr, 83));
20783a100e8bSeric 		bp += strlen(buf);
20793a100e8bSeric 		if (bitset(QGOODUID, ctladdr->q_flags))
20803a100e8bSeric 		{
20813a100e8bSeric 			(void) sprintf(bp, " (%d/%d)",
20823a100e8bSeric 					ctladdr->q_uid, ctladdr->q_gid);
20833a100e8bSeric 			bp += strlen(bp);
20843a100e8bSeric 		}
20853a100e8bSeric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
20863a100e8bSeric 	}
20874e797715Seric 	bp = buf;
20884e797715Seric 	sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
20894e797715Seric 	bp += strlen(bp);
209064187506Seric 	if (xstart != (time_t) 0)
209164187506Seric 	{
209264187506Seric 		sprintf(bp, ", xdelay=%s", pintvl(curtime() - xstart, TRUE));
209364187506Seric 		bp += strlen(bp);
209464187506Seric 	}
20953a100e8bSeric 
20963a100e8bSeric 	if (m != NULL)
20974e797715Seric 	{
20984e797715Seric 		sprintf(bp, ", mailer=%s", m->m_name);
20994e797715Seric 		bp += strlen(bp);
21004e797715Seric 	}
2101b056abd0Seric 	syslog(LOG_INFO, "%s: %s", e->e_id, buf);
21023a100e8bSeric 
2103b056abd0Seric 	buf[0] = '\0';
21043a100e8bSeric 	if (mci != NULL && mci->mci_host != NULL)
21053a100e8bSeric 	{
21063a100e8bSeric # ifdef DAEMON
21073a100e8bSeric 		extern SOCKADDR CurHostAddr;
21083a100e8bSeric # endif
21093a100e8bSeric 
2110d2ece200Seric 		sprintf(buf, "relay=%s", mci->mci_host);
21113a100e8bSeric 
21123a100e8bSeric # ifdef DAEMON
2113b056abd0Seric 		(void) strcat(buf, " [");
2114b056abd0Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
2115b056abd0Seric 		(void) strcat(buf, "]");
21163a100e8bSeric # endif
21173a100e8bSeric 	}
2118bcb9b028Seric 	else if (strcmp(stat, "queued") != 0)
21193a100e8bSeric 	{
21203a100e8bSeric 		char *p = macvalue('h', e);
21213a100e8bSeric 
21223a100e8bSeric 		if (p != NULL && p[0] != '\0')
2123d2ece200Seric 			sprintf(buf, "relay=%s", p);
21243a100e8bSeric 	}
2125b056abd0Seric 	if (buf[0] != '\0')
21264e797715Seric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
21273a100e8bSeric 
21283a100e8bSeric 	syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63));
21293a100e8bSeric #  endif /* short log buffer */
21306c2c3107Seric # endif /* LOG */
213125a99e2eSeric }
213225a99e2eSeric /*
213351552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
213425a99e2eSeric **
213551552439Seric **	This can be made an arbitrary message separator by changing $l
213651552439Seric **
21379b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
21389b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
21399b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
21409b6c17a6Seric **	this kind of antique garbage????
214125a99e2eSeric **
214225a99e2eSeric **	Parameters:
21435aa0f353Seric **		mci -- the connection information.
21445aa0f353Seric **		e -- the envelope.
214525a99e2eSeric **
214625a99e2eSeric **	Returns:
214751552439Seric **		none
214825a99e2eSeric **
214925a99e2eSeric **	Side Effects:
215051552439Seric **		outputs some text to fp.
215125a99e2eSeric */
215225a99e2eSeric 
21535aa0f353Seric putfromline(mci, e)
21545aa0f353Seric 	register MCI *mci;
2155b31e7f2bSeric 	ENVELOPE *e;
215625a99e2eSeric {
21572bc47524Seric 	char *template = "\201l\n";
215851552439Seric 	char buf[MAXLINE];
215925a99e2eSeric 
21605aa0f353Seric 	if (bitnset(M_NHDR, mci->mci_mailer->m_flags))
216151552439Seric 		return;
216213bbc08cSeric 
21632c7e1b8dSeric # ifdef UGLYUUCP
21645aa0f353Seric 	if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags))
216574b6e67bSeric 	{
2166ea09d6edSeric 		char *bang;
2167ea09d6edSeric 		char xbuf[MAXLINE];
216874b6e67bSeric 
2169fbf6dabfSeric 		expand("\201g", buf, sizeof buf, e);
21706c2c3107Seric 		bang = strchr(buf, '!');
217174b6e67bSeric 		if (bang == NULL)
217234fcca25Seric 		{
217334fcca25Seric 			errno = 0;
217434fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
217534fcca25Seric 		}
217674b6e67bSeric 		else
2177588cad61Seric 		{
2178ea09d6edSeric 			*bang++ = '\0';
21792bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
2180ea09d6edSeric 			template = xbuf;
218174b6e67bSeric 		}
2182588cad61Seric 	}
21836c2c3107Seric # endif /* UGLYUUCP */
2184fbf6dabfSeric 	expand(template, buf, sizeof buf, e);
218564187506Seric 	putxline(buf, mci, FALSE);
2186bc6e2962Seric }
2187bc6e2962Seric /*
218851552439Seric **  PUTBODY -- put the body of a message.
218951552439Seric **
219051552439Seric **	Parameters:
21915aa0f353Seric **		mci -- the connection information.
21929a6a5f55Seric **		e -- the envelope to put out.
219303c02fdeSeric **		separator -- if non-NULL, a message separator that must
219403c02fdeSeric **			not be permitted in the resulting message.
219551552439Seric **
219651552439Seric **	Returns:
219751552439Seric **		none.
219851552439Seric **
219951552439Seric **	Side Effects:
220051552439Seric **		The message is written onto fp.
220151552439Seric */
220251552439Seric 
220364187506Seric /* values for output state variable */
220464187506Seric #define OS_HEAD		0	/* at beginning of line */
220564187506Seric #define OS_CR		1	/* read a carriage return */
220664187506Seric #define OS_INLINE	2	/* putting rest of line */
220764187506Seric 
22086ffc2dd1Seric putbody(mci, e, separator)
22095aa0f353Seric 	register MCI *mci;
22109a6a5f55Seric 	register ENVELOPE *e;
221103c02fdeSeric 	char *separator;
221251552439Seric {
221377b52738Seric 	char buf[MAXLINE];
221451552439Seric 
221551552439Seric 	/*
221651552439Seric 	**  Output the body of the message
221751552439Seric 	*/
221851552439Seric 
221957736a0dSeric 	if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags))
22209a6a5f55Seric 	{
222157736a0dSeric 		char *df = queuename(e, 'd');
222257736a0dSeric 
222357736a0dSeric 		e->e_dfp = fopen(df, "r");
22249a6a5f55Seric 		if (e->e_dfp == NULL)
22258f9146b0Srick 			syserr("putbody: Cannot open %s for %s from %s",
222657736a0dSeric 				df, e->e_to, e->e_from.q_paddr);
22279a6a5f55Seric 	}
222864187506Seric 	if (e->e_dfp == NULL)
222964187506Seric 	{
223064187506Seric 		if (bitset(MCIF_INHEADER, mci->mci_flags))
223164187506Seric 		{
223264187506Seric 			putline("", mci);
223364187506Seric 			mci->mci_flags &= ~MCIF_INHEADER;
2234ff33b231Seric 		}
223564187506Seric 		putline("<<< No Message Collected >>>", mci);
223664187506Seric 		goto endofmessage;
223764187506Seric 	}
223864187506Seric 	if (e->e_dfino == (ino_t) 0)
2239519e7d80Seric 	{
224064187506Seric 		struct stat stbuf;
224164187506Seric 
224264187506Seric 		if (fstat(fileno(e->e_dfp), &stbuf) < 0)
224364187506Seric 			e->e_dfino = -1;
224464187506Seric 		else
224564187506Seric 		{
224664187506Seric 			e->e_dfdev = stbuf.st_dev;
224764187506Seric 			e->e_dfino = stbuf.st_ino;
224864187506Seric 		}
224964187506Seric 	}
22509a6a5f55Seric 	rewind(e->e_dfp);
225164187506Seric 
225264187506Seric 	if (bitset(MCIF_CVT8TO7, mci->mci_flags))
2253a6ce3008Seric 	{
22540ea5f9e4Seric 		char *boundaries[MAXMIMENESTING + 1];
22550ea5f9e4Seric 
225664187506Seric 		/*
225764187506Seric 		**  Do 8 to 7 bit MIME conversion.
225864187506Seric 		*/
225964187506Seric 
226064187506Seric 		/* make sure it looks like a MIME message */
226164187506Seric 		if (hvalue("MIME-Version", e->e_header) == NULL)
226264187506Seric 			putline("MIME-Version: 1.0", mci);
226364187506Seric 
226464187506Seric 		if (hvalue("Content-Type", e->e_header) == NULL)
226564187506Seric 		{
226664187506Seric 			sprintf(buf, "Content-Type: text/plain; charset=%s",
226764187506Seric 				defcharset(e));
226864187506Seric 			putline(buf, mci);
226964187506Seric 		}
227064187506Seric 
227164187506Seric 		/* now do the hard work */
22720ea5f9e4Seric 		boundaries[0] = NULL;
22730ea5f9e4Seric 		mime8to7(mci, e->e_header, e, boundaries, M87F_OUTER);
227464187506Seric 	}
227564187506Seric 	else
227664187506Seric 	{
227764187506Seric 		int ostate;
227864187506Seric 		register char *bp;
227964187506Seric 		register char *pbp;
228064187506Seric 		register int c;
228164187506Seric 		int padc;
228264187506Seric 		char *buflim;
228364187506Seric 		int pos = 0;
228464187506Seric 		char peekbuf[10];
228564187506Seric 
228664187506Seric 		/* we can pass it through unmodified */
228764187506Seric 		if (bitset(MCIF_INHEADER, mci->mci_flags))
228864187506Seric 		{
228964187506Seric 			putline("", mci);
229064187506Seric 			mci->mci_flags &= ~MCIF_INHEADER;
229164187506Seric 		}
229264187506Seric 
229364187506Seric 		/* determine end of buffer; allow for short mailer lines */
229464187506Seric 		buflim = &buf[sizeof buf - 1];
229564187506Seric 		if (mci->mci_mailer->m_linelimit > 0 &&
229664187506Seric 		    mci->mci_mailer->m_linelimit < sizeof buf - 1)
229764187506Seric 			buflim = &buf[mci->mci_mailer->m_linelimit - 1];
229864187506Seric 
229964187506Seric 		/* copy temp file to output with mapping */
230064187506Seric 		ostate = OS_HEAD;
230164187506Seric 		bp = buf;
230264187506Seric 		pbp = peekbuf;
230364187506Seric 		while (!ferror(mci->mci_out))
230464187506Seric 		{
230564187506Seric 			register char *xp;
230664187506Seric 
230764187506Seric 			if (pbp > peekbuf)
230864187506Seric 				c = *--pbp;
230964187506Seric 			else if ((c = fgetc(e->e_dfp)) == EOF)
231064187506Seric 				break;
231164187506Seric 			if (bitset(MCIF_7BIT, mci->mci_flags))
231264187506Seric 				c &= 0x7f;
231364187506Seric 			switch (ostate)
231464187506Seric 			{
231564187506Seric 			  case OS_HEAD:
231664187506Seric 				if (c != '\r' && c != '\n' && bp < buflim)
231764187506Seric 				{
231864187506Seric 					*bp++ = c;
231964187506Seric 					break;
232064187506Seric 				}
232164187506Seric 
232264187506Seric 				/* check beginning of line for special cases */
232364187506Seric 				*bp = '\0';
232464187506Seric 				pos = 0;
232564187506Seric 				padc = EOF;
23265aa0f353Seric 				if (buf[0] == 'F' &&
23275aa0f353Seric 				    bitnset(M_ESCFROM, mci->mci_mailer->m_flags) &&
2328d6fa2b58Sbostic 				    strncmp(buf, "From ", 5) == 0)
232964187506Seric 				{
233064187506Seric 					padc = '>';
233164187506Seric 				}
233264187506Seric 				if (buf[0] == '-' && buf[1] == '-' &&
233364187506Seric 				    separator != NULL)
233403c02fdeSeric 				{
233503c02fdeSeric 					/* possible separator */
233603c02fdeSeric 					int sl = strlen(separator);
233703c02fdeSeric 
233803c02fdeSeric 					if (strncmp(&buf[2], separator, sl) == 0)
233964187506Seric 						padc = ' ';
234003c02fdeSeric 				}
234164187506Seric 				if (buf[0] == '.' &&
234264187506Seric 				    bitnset(M_XDOT, mci->mci_mailer->m_flags))
234364187506Seric 				{
234464187506Seric 					padc = '.';
234564187506Seric 				}
234664187506Seric 
234764187506Seric 				/* now copy out saved line */
234864187506Seric 				if (TrafficLogFile != NULL)
234964187506Seric 				{
235064187506Seric 					fprintf(TrafficLogFile, "%05d >>> ", getpid());
235164187506Seric 					if (padc != EOF)
235264187506Seric 						fputc(padc, TrafficLogFile);
235364187506Seric 					for (xp = buf; xp < bp; xp++)
235464187506Seric 						fputc(*xp, TrafficLogFile);
235564187506Seric 					if (c == '\n')
235664187506Seric 						fputs(mci->mci_mailer->m_eol,
235764187506Seric 						      TrafficLogFile);
235864187506Seric 				}
235964187506Seric 				if (padc != EOF)
236064187506Seric 				{
236164187506Seric 					fputc(padc, mci->mci_out);
236264187506Seric 					pos++;
236364187506Seric 				}
236464187506Seric 				for (xp = buf; xp < bp; xp++)
236564187506Seric 					fputc(*xp, mci->mci_out);
236664187506Seric 				if (c == '\n')
236764187506Seric 				{
236864187506Seric 					fputs(mci->mci_mailer->m_eol,
236964187506Seric 					      mci->mci_out);
237064187506Seric 					pos = 0;
237164187506Seric 				}
237264187506Seric 				else
237364187506Seric 				{
237464187506Seric 					pos += bp - buf;
237564187506Seric 					if (c != '\r')
237664187506Seric 						*pbp++ = c;
237764187506Seric 				}
237864187506Seric 				bp = buf;
237964187506Seric 
238064187506Seric 				/* determine next state */
238164187506Seric 				if (c == '\n')
238264187506Seric 					ostate = OS_HEAD;
238364187506Seric 				else if (c == '\r')
238464187506Seric 					ostate = OS_CR;
238564187506Seric 				else
238664187506Seric 					ostate = OS_INLINE;
238764187506Seric 				continue;
238864187506Seric 
238964187506Seric 			  case OS_CR:
239064187506Seric 				if (c == '\n')
239164187506Seric 				{
239264187506Seric 					/* got CRLF */
239364187506Seric 					fputs(mci->mci_mailer->m_eol, mci->mci_out);
239464187506Seric 					if (TrafficLogFile != NULL)
239564187506Seric 					{
239664187506Seric 						fputs(mci->mci_mailer->m_eol,
239764187506Seric 						      TrafficLogFile);
239864187506Seric 					}
239964187506Seric 					ostate = OS_HEAD;
240064187506Seric 					continue;
240164187506Seric 				}
240264187506Seric 
240364187506Seric 				/* had a naked carriage return */
240464187506Seric 				*pbp++ = c;
240564187506Seric 				c = '\r';
240664187506Seric 				goto putch;
240764187506Seric 
240864187506Seric 			  case OS_INLINE:
240964187506Seric 				if (c == '\r')
241064187506Seric 				{
241164187506Seric 					ostate = OS_CR;
241264187506Seric 					continue;
241364187506Seric 				}
241464187506Seric putch:
241564187506Seric 				if (mci->mci_mailer->m_linelimit > 0 &&
241664187506Seric 				    pos > mci->mci_mailer->m_linelimit &&
241764187506Seric 				    c != '\n')
241864187506Seric 				{
241964187506Seric 					putc('!', mci->mci_out);
242064187506Seric 					fputs(mci->mci_mailer->m_eol, mci->mci_out);
242164187506Seric 					if (TrafficLogFile != NULL)
242264187506Seric 					{
242364187506Seric 						fprintf(TrafficLogFile, "!%s",
242464187506Seric 							mci->mci_mailer->m_eol);
242564187506Seric 					}
242664187506Seric 					ostate = OS_HEAD;
242764187506Seric 					*pbp++ = c;
242864187506Seric 					continue;
242964187506Seric 				}
243064187506Seric 				if (TrafficLogFile != NULL)
243164187506Seric 					fputc(c, TrafficLogFile);
243264187506Seric 				putc(c, mci->mci_out);
243364187506Seric 				pos++;
243464187506Seric 				ostate = c == '\n' ? OS_HEAD : OS_INLINE;
243564187506Seric 				break;
243664187506Seric 			}
243764187506Seric 		}
2438a6ce3008Seric 	}
243951552439Seric 
24409a6a5f55Seric 	if (ferror(e->e_dfp))
244151552439Seric 	{
244257736a0dSeric 		syserr("putbody: df%s: read error", e->e_id);
244351552439Seric 		ExitStat = EX_IOERR;
244451552439Seric 	}
244551552439Seric 
244664187506Seric endofmessage:
24470890ba1fSeric 	/* some mailers want extra blank line at end of message */
24485aa0f353Seric 	if (bitnset(M_BLANKEND, mci->mci_mailer->m_flags) &&
24495aa0f353Seric 	    buf[0] != '\0' && buf[0] != '\n')
24505aa0f353Seric 		putline("", mci);
24510890ba1fSeric 
24525aa0f353Seric 	(void) fflush(mci->mci_out);
24535aa0f353Seric 	if (ferror(mci->mci_out) && errno != EPIPE)
245451552439Seric 	{
245551552439Seric 		syserr("putbody: write error");
245651552439Seric 		ExitStat = EX_IOERR;
245751552439Seric 	}
245851552439Seric 	errno = 0;
245925a99e2eSeric }
246025a99e2eSeric /*
246125a99e2eSeric **  MAILFILE -- Send a message to a file.
246225a99e2eSeric **
2463f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
2464f129ec7dSeric **	bits, sendmail will try to become the owner of that file
2465f129ec7dSeric **	rather than the real user.  Obviously, this only works if
2466f129ec7dSeric **	sendmail runs as root.
2467f129ec7dSeric **
2468588cad61Seric **	This could be done as a subordinate mailer, except that it
2469588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
2470588cad61Seric **	view this as being sufficiently important as to include it
2471588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
2472588cad61Seric **	to create another process plus some pipes to save the message.
2473588cad61Seric **
247425a99e2eSeric **	Parameters:
247525a99e2eSeric **		filename -- the name of the file to send to.
24766259796dSeric **		ctladdr -- the controlling address header -- includes
24776259796dSeric **			the userid/groupid to be when sending.
247825a99e2eSeric **
247925a99e2eSeric **	Returns:
248025a99e2eSeric **		The exit code associated with the operation.
248125a99e2eSeric **
248225a99e2eSeric **	Side Effects:
248325a99e2eSeric **		none.
248425a99e2eSeric */
248525a99e2eSeric 
2486b31e7f2bSeric mailfile(filename, ctladdr, e)
248725a99e2eSeric 	char *filename;
24886259796dSeric 	ADDRESS *ctladdr;
2489b31e7f2bSeric 	register ENVELOPE *e;
249025a99e2eSeric {
249125a99e2eSeric 	register FILE *f;
249264187506Seric 	register int pid = -1;
249315d084d5Seric 	int mode;
249425a99e2eSeric 
2495671745f3Seric 	if (tTd(11, 1))
2496671745f3Seric 	{
2497671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
2498671745f3Seric 		printaddr(ctladdr, FALSE);
2499671745f3Seric 	}
2500671745f3Seric 
2501f170942cSeric 	if (e->e_xfp != NULL)
2502f170942cSeric 		fflush(e->e_xfp);
2503f170942cSeric 
250432d19d43Seric 	/*
250532d19d43Seric 	**  Fork so we can change permissions here.
250632d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
250732d19d43Seric 	**	the complications of calling subroutines, etc.
250832d19d43Seric 	*/
250932d19d43Seric 
251032d19d43Seric 	DOFORK(fork);
251132d19d43Seric 
251232d19d43Seric 	if (pid < 0)
251332d19d43Seric 		return (EX_OSERR);
251432d19d43Seric 	else if (pid == 0)
251532d19d43Seric 	{
251632d19d43Seric 		/* child -- actually write to file */
2517f129ec7dSeric 		struct stat stb;
2518f2e4d5e8Seric 		struct stat fsb;
25195aa0f353Seric 		MCI mcibuf;
25204ef13b77Seric 		int oflags = O_WRONLY|O_APPEND;
2521f129ec7dSeric 
2522f2e4d5e8Seric 		if (e->e_lockfp != NULL)
2523f2e4d5e8Seric 		{
2524f2e4d5e8Seric 			fclose(e->e_lockfp);
2525f2e4d5e8Seric 			e->e_lockfp = NULL;
2526f2e4d5e8Seric 		}
2527f2e4d5e8Seric 
25282b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
25292b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
25302b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
25313462ad9eSeric 		(void) umask(OldUmask);
253295f16dc0Seric 
253364187506Seric #ifdef HASLSTAT
253464187506Seric 		if ((SafeFileEnv != NULL ? lstat(filename, &stb)
253564187506Seric 					 : stat(filename, &stb)) < 0)
253664187506Seric #else
2537f129ec7dSeric 		if (stat(filename, &stb) < 0)
253864187506Seric #endif
25394ef13b77Seric 		{
25403a98e7eaSeric 			stb.st_mode = FileMode;
25414ef13b77Seric 			oflags |= O_CREAT|O_EXCL;
25424ef13b77Seric 		}
254364187506Seric 		else if (bitset(0111, stb.st_mode) || stb.st_nlink != 1 ||
254464187506Seric 			 (SafeFileEnv != NULL && !S_ISREG(stb.st_mode)))
2545f2e4d5e8Seric 			exit(EX_CANTCREAT);
254615d084d5Seric 		mode = stb.st_mode;
254795f16dc0Seric 
254895f16dc0Seric 		/* limit the errors to those actually caused in the child */
254995f16dc0Seric 		errno = 0;
255095f16dc0Seric 		ExitStat = EX_OK;
255195f16dc0Seric 
255219428781Seric 		if (ctladdr != NULL)
255315d084d5Seric 		{
255415d084d5Seric 			/* ignore setuid and setgid bits */
255515d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
255615d084d5Seric 		}
255715d084d5Seric 
25588f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
255957736a0dSeric 		if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags))
25608f9146b0Srick 		{
256157736a0dSeric 			char *df = queuename(e, 'd');
256257736a0dSeric 
256357736a0dSeric 			e->e_dfp = fopen(df, "r");
256495f16dc0Seric 			if (e->e_dfp == NULL)
256595f16dc0Seric 			{
25668f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
256757736a0dSeric 					df, e->e_to, e->e_from.q_paddr);
25688f9146b0Srick 			}
25698f9146b0Srick 		}
25708f9146b0Srick 
257164187506Seric 		if (SafeFileEnv != NULL && SafeFileEnv[0] != NULL)
257264187506Seric 		{
257364187506Seric 			int i;
257464187506Seric 
257564187506Seric 			if (chroot(SafeFileEnv) < 0)
257664187506Seric 			{
257764187506Seric 				syserr("mailfile: Cannot chroot(%s)",
257864187506Seric 					SafeFileEnv);
257964187506Seric 				exit(EX_CANTCREAT);
258064187506Seric 			}
258164187506Seric 			i = strlen(SafeFileEnv);
258264187506Seric 			if (strncmp(SafeFileEnv, filename, i) == 0)
258364187506Seric 				filename += i;
258464187506Seric 		}
258564187506Seric 		if (chdir("/") < 0)
258664187506Seric 			syserr("mailfile: cannot chdir(/)");
258764187506Seric 
258815d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
2589e36b99e2Seric 		{
259064187506Seric 			if (ctladdr != NULL && ctladdr->q_uid != 0)
2591898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
2592898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
2593898a126bSbostic 					ctladdr->q_gid);
259464187506Seric 			else if (FileMailer != NULL && FileMailer->m_gid != 0)
259564187506Seric 				(void) initgroups(DefUser, FileMailer->m_gid);
259664187506Seric 			else
259764187506Seric 				(void) initgroups(DefUser, DefGid);
2598e36b99e2Seric 		}
259915d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
2600e36b99e2Seric 		{
260164187506Seric 			if (ctladdr != NULL && ctladdr->q_uid != 0)
26026abe14bdSeric 				(void) setuid(ctladdr->q_uid);
260364187506Seric 			else if (FileMailer != NULL && FileMailer->m_uid != 0)
260464187506Seric 				(void) setuid(FileMailer->m_uid);
260564187506Seric 			else
260664187506Seric 				(void) setuid(DefUid);
2607e36b99e2Seric 		}
260895f16dc0Seric 		FileName = filename;
260995f16dc0Seric 		LineNumber = 0;
26104ef13b77Seric 		f = dfopen(filename, oflags, FileMode);
261125a99e2eSeric 		if (f == NULL)
261295f16dc0Seric 		{
2613b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
261432d19d43Seric 			exit(EX_CANTCREAT);
261595f16dc0Seric 		}
2616f2e4d5e8Seric 		if (fstat(fileno(f), &fsb) < 0 ||
261764187506Seric 		    (!bitset(O_CREAT, oflags) &&
26184ef13b77Seric 		     (stb.st_nlink != fsb.st_nlink ||
2619f2e4d5e8Seric 		      stb.st_dev != fsb.st_dev ||
2620f2e4d5e8Seric 		      stb.st_ino != fsb.st_ino ||
262164187506Seric 		      stb.st_uid != fsb.st_uid)))
2622f2e4d5e8Seric 		{
2623f2e4d5e8Seric 			message("554 cannot write: file changed after open");
2624f2e4d5e8Seric 			exit(EX_CANTCREAT);
2625f2e4d5e8Seric 		}
262625a99e2eSeric 
26275aa0f353Seric 		bzero(&mcibuf, sizeof mcibuf);
26285aa0f353Seric 		mcibuf.mci_mailer = FileMailer;
26295aa0f353Seric 		mcibuf.mci_out = f;
26305aa0f353Seric 		if (bitnset(M_7BITS, FileMailer->m_flags))
26315aa0f353Seric 			mcibuf.mci_flags |= MCIF_7BIT;
26325aa0f353Seric 
26335aa0f353Seric 		putfromline(&mcibuf, e);
263464187506Seric 		(*e->e_puthdr)(&mcibuf, e->e_header, e);
26356ffc2dd1Seric 		(*e->e_putbody)(&mcibuf, e, NULL);
26365aa0f353Seric 		putline("\n", &mcibuf);
263795f16dc0Seric 		if (ferror(f))
263895f16dc0Seric 		{
2639b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
264095f16dc0Seric 			setstat(EX_IOERR);
264195f16dc0Seric 		}
2642ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
264332d19d43Seric 		(void) fflush(stdout);
2644e36b99e2Seric 
264527628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
2646c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
264795f16dc0Seric 		exit(ExitStat);
264813bbc08cSeric 		/*NOTREACHED*/
264932d19d43Seric 	}
265032d19d43Seric 	else
265132d19d43Seric 	{
265232d19d43Seric 		/* parent -- wait for exit status */
2653588cad61Seric 		int st;
265432d19d43Seric 
2655588cad61Seric 		st = waitfor(pid);
2656bf9bc890Seric 		if (WIFEXITED(st))
2657bf9bc890Seric 			return (WEXITSTATUS(st));
2658588cad61Seric 		else
2659b6a0de9dSeric 		{
2660b6a0de9dSeric 			syserr("child died on signal %d", st);
2661bf9bc890Seric 			return (EX_UNAVAILABLE);
2662b6a0de9dSeric 		}
26638f9146b0Srick 		/*NOTREACHED*/
266432d19d43Seric 	}
266525a99e2eSeric }
2666ea4dc939Seric /*
2667e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2668e103b48fSeric **
2669e103b48fSeric **	The signature describes how we are going to send this -- it
2670e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2671e103b48fSeric **	an ordered list of MX hosts.
2672e103b48fSeric **
2673e103b48fSeric **	Parameters:
2674e103b48fSeric **		m -- the mailer describing this host.
2675e103b48fSeric **		host -- the host name.
2676e103b48fSeric **		e -- the current envelope.
2677e103b48fSeric **
2678e103b48fSeric **	Returns:
2679e103b48fSeric **		The signature for this host.
2680e103b48fSeric **
2681e103b48fSeric **	Side Effects:
2682e103b48fSeric **		Can tweak the symbol table.
2683e103b48fSeric */
2684e103b48fSeric 
2685e103b48fSeric char *
2686e103b48fSeric hostsignature(m, host, e)
2687e103b48fSeric 	register MAILER *m;
2688e103b48fSeric 	char *host;
2689e103b48fSeric 	ENVELOPE *e;
2690e103b48fSeric {
2691e103b48fSeric 	register char *p;
2692e103b48fSeric 	register STAB *s;
2693e103b48fSeric 	int i;
2694e103b48fSeric 	int len;
26959d4a8008Seric #if NAMED_BIND
2696e103b48fSeric 	int nmx;
2697e103b48fSeric 	auto int rcode;
2698bafdc4e5Seric 	char *hp;
2699bafdc4e5Seric 	char *endp;
270064187506Seric 	int oldoptions = _res.options;
2701e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2702e103b48fSeric #endif
2703e103b48fSeric 
2704e103b48fSeric 	/*
2705e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2706e103b48fSeric 	*/
2707e103b48fSeric 
2708e103b48fSeric 	p = m->m_mailer;
2709e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2710e103b48fSeric 	{
2711e103b48fSeric 		/* just an ordinary mailer */
2712e103b48fSeric 		return host;
2713e103b48fSeric 	}
2714e103b48fSeric 
2715e103b48fSeric 	/*
2716e103b48fSeric 	**  Look it up in the symbol table.
2717e103b48fSeric 	*/
2718e103b48fSeric 
2719e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2720e103b48fSeric 	if (s->s_hostsig != NULL)
2721e103b48fSeric 		return s->s_hostsig;
2722e103b48fSeric 
2723e103b48fSeric 	/*
2724e103b48fSeric 	**  Not already there -- create a signature.
2725e103b48fSeric 	*/
2726e103b48fSeric 
27279d4a8008Seric #if NAMED_BIND
2728516782b4Seric 	if (ConfigLevel < 2)
2729516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2730516782b4Seric 
2731bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2732bafdc4e5Seric 	{
2733bafdc4e5Seric 		endp = strchr(hp, ':');
2734bafdc4e5Seric 		if (endp != NULL)
2735bafdc4e5Seric 			*endp = '\0';
2736bafdc4e5Seric 
27377bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
27387d55540cSeric 
2739e103b48fSeric 		if (nmx <= 0)
2740e103b48fSeric 		{
2741e103b48fSeric 			register MCI *mci;
2742e103b48fSeric 
2743e103b48fSeric 			/* update the connection info for this host */
2744bafdc4e5Seric 			mci = mci_get(hp, m);
2745e103b48fSeric 			mci->mci_exitstat = rcode;
2746e103b48fSeric 			mci->mci_errno = errno;
2747f170942cSeric 			mci->mci_herrno = h_errno;
2748e103b48fSeric 
2749e103b48fSeric 			/* and return the original host name as the signature */
2750bafdc4e5Seric 			nmx = 1;
2751bafdc4e5Seric 			mxhosts[0] = hp;
2752e103b48fSeric 		}
2753e103b48fSeric 
2754e103b48fSeric 		len = 0;
2755e103b48fSeric 		for (i = 0; i < nmx; i++)
2756e103b48fSeric 		{
2757e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2758e103b48fSeric 		}
2759bafdc4e5Seric 		if (s->s_hostsig != NULL)
2760bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2761bafdc4e5Seric 		p = xalloc(len);
2762bafdc4e5Seric 		if (s->s_hostsig != NULL)
2763bafdc4e5Seric 		{
2764bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2765bafdc4e5Seric 			free(s->s_hostsig);
2766bafdc4e5Seric 			s->s_hostsig = p;
2767bafdc4e5Seric 			p += strlen(p);
2768bafdc4e5Seric 			*p++ = ':';
2769bafdc4e5Seric 		}
2770bafdc4e5Seric 		else
2771bafdc4e5Seric 			s->s_hostsig = p;
2772e103b48fSeric 		for (i = 0; i < nmx; i++)
2773e103b48fSeric 		{
2774e103b48fSeric 			if (i != 0)
2775e103b48fSeric 				*p++ = ':';
2776e103b48fSeric 			strcpy(p, mxhosts[i]);
2777e103b48fSeric 			p += strlen(p);
2778e103b48fSeric 		}
2779bafdc4e5Seric 		if (endp != NULL)
2780bafdc4e5Seric 			*endp++ = ':';
2781bafdc4e5Seric 	}
2782e103b48fSeric 	makelower(s->s_hostsig);
2783516782b4Seric 	if (ConfigLevel < 2)
2784516782b4Seric 		_res.options = oldoptions;
2785e103b48fSeric #else
2786e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2787e103b48fSeric 	s->s_hostsig = host;
2788e103b48fSeric #endif
2789e103b48fSeric 	if (tTd(17, 1))
2790e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2791e103b48fSeric 	return s->s_hostsig;
2792e103b48fSeric }
279364187506Seric /*
279464187506Seric **  SETSTATUS -- set the address status for return messages
279564187506Seric **
279664187506Seric **	Parameters:
279764187506Seric **		a -- the address to set.
279864187506Seric **		msg -- the text of the message, which must be in standard
279964187506Seric **			SMTP form (3 digits, a space, and a message).
280064187506Seric **
280164187506Seric **	Returns:
280264187506Seric **		none.
280364187506Seric */
280464187506Seric 
280564187506Seric setstatus(a, msg)
280664187506Seric 	register ADDRESS *a;
280764187506Seric 	char *msg;
280864187506Seric {
280964187506Seric 	char buf[MAXLINE];
281064187506Seric 
281164187506Seric 	if (a->q_rstatus != NULL)
281264187506Seric 		free(a->q_rstatus);
281364187506Seric 	if (strlen(msg) > 4)
281464187506Seric 	{
281564187506Seric 		register char *p, *q;
281664187506Seric 		int parenlev = 0;
281764187506Seric 
281864187506Seric 		strncpy(buf, msg, 4);
281964187506Seric 		p = &buf[4];
282064187506Seric 		*p++ = '(';
282164187506Seric 		for (q = &msg[4]; *q != '\0'; q++)
282264187506Seric 		{
282364187506Seric 			switch (*q)
282464187506Seric 			{
282564187506Seric 			  case '(':
282664187506Seric 				parenlev++;
282764187506Seric 				break;
282864187506Seric 
282964187506Seric 			  case ')':
283064187506Seric 				if (parenlev > 0)
283164187506Seric 					parenlev--;
283264187506Seric 				else
283364187506Seric 					*p++ = '\\';
283464187506Seric 				break;
283564187506Seric 			}
283664187506Seric 			*p++ = *q;
283764187506Seric 		}
283864187506Seric 		while (parenlev-- >= 0)
283964187506Seric 			*p++ = ')';
284064187506Seric 		*p++ = '\0';
284164187506Seric 		msg = buf;
284264187506Seric 	}
284364187506Seric 	a->q_rstatus = newstr(msg);
284464187506Seric }
2845