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*550092c3Seric static char sccsid[] = "@(#)deliver.c	8.60 (Berkeley) 01/06/94";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14f28da541Smiriam #include <netdb.h>
15911693bfSbostic #include <errno.h>
16134746fbSeric #ifdef NAMED_BIND
17912a731aSbostic #include <arpa/nameser.h>
18912a731aSbostic #include <resolv.h>
19f170942cSeric 
20f170942cSeric extern int	h_errno;
21134746fbSeric #endif
2225a99e2eSeric 
235d437c4dSeric extern char	SmtpError[];
245d437c4dSeric 
2525a99e2eSeric /*
269c9e68d9Seric **  SENDALL -- actually send all the messages.
279c9e68d9Seric **
289c9e68d9Seric **	Parameters:
299c9e68d9Seric **		e -- the envelope to send.
309c9e68d9Seric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
319c9e68d9Seric **			the current e->e_sendmode.
329c9e68d9Seric **
339c9e68d9Seric **	Returns:
349c9e68d9Seric **		none.
359c9e68d9Seric **
369c9e68d9Seric **	Side Effects:
379c9e68d9Seric **		Scans the send lists and sends everything it finds.
389c9e68d9Seric **		Delivers any appropriate error messages.
399c9e68d9Seric **		If we are running in a non-interactive mode, takes the
409c9e68d9Seric **			appropriate action.
419c9e68d9Seric */
429c9e68d9Seric 
439c9e68d9Seric sendall(e, mode)
449c9e68d9Seric 	ENVELOPE *e;
459c9e68d9Seric 	char mode;
469c9e68d9Seric {
479c9e68d9Seric 	register ADDRESS *q;
489c9e68d9Seric 	char *owner;
499c9e68d9Seric 	int otherowners;
509c9e68d9Seric 	register ENVELOPE *ee;
519c9e68d9Seric 	ENVELOPE *splitenv = NULL;
526103d9b4Seric 	bool announcequeueup;
539c9e68d9Seric 
547880f3e4Seric 	/*
557880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
567880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
577880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
587880f3e4Seric 	**  addresses to be sent.
597880f3e4Seric 	*/
607880f3e4Seric 
617880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
62896b16f6Seric 	{
63896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
64896b16f6Seric 		return;
65896b16f6Seric 	}
66896b16f6Seric 
679c9e68d9Seric 	/* determine actual delivery mode */
68fbe2f963Seric 	CurrentLA = getla();
699c9e68d9Seric 	if (mode == SM_DEFAULT)
709c9e68d9Seric 	{
719c9e68d9Seric 		mode = e->e_sendmode;
729c9e68d9Seric 		if (mode != SM_VERIFY &&
739c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
749c9e68d9Seric 			mode = SM_QUEUE;
756103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
769c9e68d9Seric 	}
776103d9b4Seric 	else
786103d9b4Seric 		announcequeueup = FALSE;
799c9e68d9Seric 
809c9e68d9Seric 	if (tTd(13, 1))
819c9e68d9Seric 	{
820e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
830e484fe5Seric 			mode, e->e_id);
849c9e68d9Seric 		printaddr(&e->e_from, FALSE);
859c9e68d9Seric 		printf("sendqueue:\n");
869c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
879c9e68d9Seric 	}
889c9e68d9Seric 
899c9e68d9Seric 	/*
909c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
919c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
929c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
939c9e68d9Seric 	*/
949c9e68d9Seric 
959c9e68d9Seric 	CurEnv = e;
969c9e68d9Seric 
979c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
989c9e68d9Seric 	{
999c9e68d9Seric 		errno = 0;
1004bb8b0fdSeric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
1019c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
1024bb8b0fdSeric 			RealHostName, e->e_sendqueue->q_paddr);
1039c9e68d9Seric 		return;
1049c9e68d9Seric 	}
1059c9e68d9Seric 
106b8004690Seric 	/*
107b8004690Seric 	**  Do sender deletion.
108b8004690Seric 	**
109b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
110b8004690Seric 	**	This can happen if the name server is hosed when you
111b8004690Seric 	**	are trying to send mail.  The result is that the sender
112b8004690Seric 	**	is instantiated in the queue as a recipient.
113b8004690Seric 	*/
114b8004690Seric 
115e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
116e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1179c9e68d9Seric 	{
1189c9e68d9Seric 		if (tTd(13, 5))
1199c9e68d9Seric 		{
1209c9e68d9Seric 			printf("sendall: QDONTSEND ");
1219c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1229c9e68d9Seric 		}
1239c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1249c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1259c9e68d9Seric 	}
1269c9e68d9Seric 
127ce5531bdSeric 	/*
128ce5531bdSeric 	**  Handle alias owners.
129ce5531bdSeric 	**
130ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
131ce5531bdSeric 	**	We discard owners that are the same as the return path.
132ce5531bdSeric 	*/
133ce5531bdSeric 
134ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
135ce5531bdSeric 	{
136ce5531bdSeric 		register struct address *a;
137ce5531bdSeric 
138ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
139ce5531bdSeric 			continue;
140ce5531bdSeric 		if (a != NULL)
141ce5531bdSeric 			q->q_owner = a->q_owner;
142ce5531bdSeric 
143ce5531bdSeric 		if (q->q_owner != NULL &&
144ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
145ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
146ce5531bdSeric 			q->q_owner = NULL;
147ce5531bdSeric 	}
148ce5531bdSeric 
149ce5531bdSeric 	owner = "";
150ce5531bdSeric 	otherowners = 1;
151ce5531bdSeric 	while (owner != NULL && otherowners > 0)
152ce5531bdSeric 	{
153ce5531bdSeric 		owner = NULL;
154ce5531bdSeric 		otherowners = 0;
155ce5531bdSeric 
156ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
157ce5531bdSeric 		{
158ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
159ce5531bdSeric 				continue;
160ce5531bdSeric 
161ce5531bdSeric 			if (q->q_owner != NULL)
162ce5531bdSeric 			{
163ce5531bdSeric 				if (owner == NULL)
164ce5531bdSeric 					owner = q->q_owner;
165ce5531bdSeric 				else if (owner != q->q_owner)
166ce5531bdSeric 				{
167ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
168ce5531bdSeric 					{
169ce5531bdSeric 						/* make future comparisons cheap */
170ce5531bdSeric 						q->q_owner = owner;
171ce5531bdSeric 					}
172ce5531bdSeric 					else
173ce5531bdSeric 					{
174ce5531bdSeric 						otherowners++;
175ce5531bdSeric 					}
176ce5531bdSeric 					owner = q->q_owner;
177ce5531bdSeric 				}
178ce5531bdSeric 			}
179ce5531bdSeric 			else
180ce5531bdSeric 			{
181ce5531bdSeric 				otherowners++;
182ce5531bdSeric 			}
183ce5531bdSeric 		}
184ce5531bdSeric 
185ce5531bdSeric 		if (owner != NULL && otherowners > 0)
186ce5531bdSeric 		{
187ce5531bdSeric 			extern HDR *copyheader();
188ce5531bdSeric 			extern ADDRESS *copyqueue();
189ce5531bdSeric 
190ce5531bdSeric 			/*
191ce5531bdSeric 			**  Split this envelope into two.
192ce5531bdSeric 			*/
193ce5531bdSeric 
194ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
195ce5531bdSeric 			*ee = *e;
196ce5531bdSeric 			ee->e_id = NULL;
197ce5531bdSeric 			(void) queuename(ee, '\0');
198ce5531bdSeric 
199ce5531bdSeric 			if (tTd(13, 1))
200ce5531bdSeric 				printf("sendall: split %s into %s\n",
201ce5531bdSeric 					e->e_id, ee->e_id);
202ce5531bdSeric 
203ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
204ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
205ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
206ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
207ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
208ce5531bdSeric 			if (tTd(13, 5))
209ce5531bdSeric 			{
210ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
211ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
212ce5531bdSeric 			}
213ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
214ce5531bdSeric 			ee->e_dfp = NULL;
215ce5531bdSeric 			ee->e_xfp = NULL;
216ce5531bdSeric 			ee->e_df = NULL;
217ce5531bdSeric 			ee->e_errormode = EM_MAIL;
218ce5531bdSeric 			ee->e_sibling = splitenv;
219ce5531bdSeric 			splitenv = ee;
220ce5531bdSeric 
221ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
222ce5531bdSeric 				if (q->q_owner == owner)
223ce5531bdSeric 					q->q_flags |= QDONTSEND;
224ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
225ce5531bdSeric 				if (q->q_owner != owner)
226ce5531bdSeric 					q->q_flags |= QDONTSEND;
227ce5531bdSeric 
228ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
229ce5531bdSeric 			{
230ce5531bdSeric 				ee->e_dfp = NULL;
231da662164Seric 				ee->e_df = queuename(ee, 'd');
232da662164Seric 				ee->e_df = newstr(ee->e_df);
233ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
234ce5531bdSeric 				{
235ce5531bdSeric 					syserr("sendall: link(%s, %s)",
236ce5531bdSeric 						e->e_df, ee->e_df);
237ce5531bdSeric 				}
238ce5531bdSeric 			}
239ce5531bdSeric 
240ce5531bdSeric 			if (mode != SM_VERIFY)
241ce5531bdSeric 				openxscript(ee);
242ce5531bdSeric #ifdef LOG
243ce5531bdSeric 			if (LogLevel > 4)
244ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
245ce5531bdSeric 					ee->e_id, e->e_id);
246ce5531bdSeric #endif
247ce5531bdSeric 		}
248ce5531bdSeric 	}
249ce5531bdSeric 
250ce5531bdSeric 	if (owner != NULL)
251ce5531bdSeric 	{
252ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
253ce5531bdSeric 		if (tTd(13, 5))
254ce5531bdSeric 		{
255ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
256ce5531bdSeric 			printaddr(&e->e_from, FALSE);
257ce5531bdSeric 		}
258ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
259ce5531bdSeric 		e->e_errormode = EM_MAIL;
260ce5531bdSeric 	}
261ce5531bdSeric 
262c1ac89b1Seric # ifdef QUEUE
263c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
264c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
265c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
266c1ac89b1Seric 	{
267c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2686103d9b4Seric 		queueup(e, TRUE, announcequeueup);
269ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2706103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
271c1ac89b1Seric 	}
272c1ac89b1Seric #endif /* QUEUE */
273c1ac89b1Seric 
274ce5531bdSeric 	if (splitenv != NULL)
275ce5531bdSeric 	{
276ce5531bdSeric 		if (tTd(13, 1))
277ce5531bdSeric 		{
278ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
279ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
280ce5531bdSeric 		}
281ce5531bdSeric 
282ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
283ce5531bdSeric 		{
284ce5531bdSeric 			CurEnv = ee;
285ce5531bdSeric 			sendenvelope(ee, mode);
286ce5531bdSeric 		}
287ce5531bdSeric 
288ce5531bdSeric 		CurEnv = e;
289ce5531bdSeric 	}
290ce5531bdSeric 	sendenvelope(e, mode);
291ce5531bdSeric 
292ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
293ce5531bdSeric 		dropenvelope(splitenv);
294ce5531bdSeric }
295ce5531bdSeric 
296ce5531bdSeric sendenvelope(e, mode)
297ce5531bdSeric 	register ENVELOPE *e;
298ce5531bdSeric 	char mode;
299ce5531bdSeric {
300ce5531bdSeric 	bool oldverbose;
301ce5531bdSeric 	int pid;
302ce5531bdSeric 	register ADDRESS *q;
3036b2765c6Seric 	char *qf;
3046b2765c6Seric 	char *id;
305ce5531bdSeric 
3067880f3e4Seric 	/*
3077880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3087880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3097880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3107880f3e4Seric 	**  addresses to be sent.
3117880f3e4Seric 	*/
3127880f3e4Seric 
3137880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
3147880f3e4Seric 	{
3157880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3167880f3e4Seric 		return;
3177880f3e4Seric 	}
3187880f3e4Seric 
319ce5531bdSeric 	oldverbose = Verbose;
320c1ac89b1Seric 	switch (mode)
321c1ac89b1Seric 	{
322c1ac89b1Seric 	  case SM_VERIFY:
323c1ac89b1Seric 		Verbose = TRUE;
324c1ac89b1Seric 		break;
325c1ac89b1Seric 
326c1ac89b1Seric 	  case SM_QUEUE:
327c1ac89b1Seric   queueonly:
328c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
329c1ac89b1Seric 		return;
330c1ac89b1Seric 
331c1ac89b1Seric 	  case SM_FORK:
332c1ac89b1Seric 		if (e->e_xfp != NULL)
333c1ac89b1Seric 			(void) fflush(e->e_xfp);
334c1ac89b1Seric 
3352b9178d3Seric # ifndef HASFLOCK
336c1ac89b1Seric 		/*
3376b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3386b2765c6Seric 		**  the lock is owned by a process, not by an open file
3396b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3406b2765c6Seric 		**  then restart from scratch in the child.
341c1ac89b1Seric 		*/
342c1ac89b1Seric 
3436b2765c6Seric 		/* save id for future use */
3446b2765c6Seric 		id = e->e_id;
3456b2765c6Seric 
3466b2765c6Seric 		/* now drop the envelope in the parent */
3476b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3486b2765c6Seric 		dropenvelope(e);
3496b2765c6Seric 
3506b2765c6Seric 		/* and reacquire in the child */
3516b2765c6Seric 		(void) dowork(id, TRUE, FALSE, e);
3526b2765c6Seric 
3536b2765c6Seric 		return;
3546b2765c6Seric 
3556b2765c6Seric # else /* HASFLOCK */
356c1ac89b1Seric 
357c1ac89b1Seric 		pid = fork();
358c1ac89b1Seric 		if (pid < 0)
359c1ac89b1Seric 		{
360c1ac89b1Seric 			goto queueonly;
361c1ac89b1Seric 		}
362c1ac89b1Seric 		else if (pid > 0)
363c1ac89b1Seric 		{
3640e484fe5Seric 			/* be sure we leave the temp files to our child */
3650e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3660e484fe5Seric 			if (e->e_lockfp != NULL)
3670e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3680e484fe5Seric 			e->e_lockfp = NULL;
3690e484fe5Seric 
3700e484fe5Seric 			/* close any random open files in the envelope */
3710e484fe5Seric 			closexscript(e);
3720e484fe5Seric 			if (e->e_dfp != NULL)
3730e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3740e484fe5Seric 			e->e_dfp = NULL;
3750e484fe5Seric 			e->e_id = e->e_df = NULL;
376c1ac89b1Seric 			return;
377c1ac89b1Seric 		}
378c1ac89b1Seric 
379c1ac89b1Seric 		/* double fork to avoid zombies */
380c1ac89b1Seric 		if (fork() > 0)
381c1ac89b1Seric 			exit(EX_OK);
382c1ac89b1Seric 
383c1ac89b1Seric 		/* be sure we are immune from the terminal */
3847880f3e4Seric 		disconnect(1, e);
385c1ac89b1Seric 
386c1ac89b1Seric 		/*
387c1ac89b1Seric 		**  Close any cached connections.
388c1ac89b1Seric 		**
389c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
390c1ac89b1Seric 		**	still knows about the connection.
391c1ac89b1Seric 		**
392c1ac89b1Seric 		**	This should only happen when delivering an error
393c1ac89b1Seric 		**	message.
394c1ac89b1Seric 		*/
395c1ac89b1Seric 
396c1ac89b1Seric 		mci_flush(FALSE, NULL);
397c1ac89b1Seric 
3986b2765c6Seric # endif /* HASFLOCK */
3996b2765c6Seric 
400c1ac89b1Seric 		break;
401c1ac89b1Seric 	}
402c1ac89b1Seric 
403c1ac89b1Seric 	/*
4049c9e68d9Seric 	**  Run through the list and send everything.
4055288e21aSeric 	**
4065288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4075288e21aSeric 	**	result in returned mail.
4089c9e68d9Seric 	*/
4099c9e68d9Seric 
4109c9e68d9Seric 	e->e_nsent = 0;
4115288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
412faad2b16Seric 
413faad2b16Seric 	/* now run through the queue */
4149c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4159c9e68d9Seric 	{
416fdc75a0fSeric #ifdef XDEBUG
417fdc75a0fSeric 		char wbuf[MAXNAME + 20];
418fdc75a0fSeric 
419fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
420fdc75a0fSeric 		checkfd012(wbuf);
421fdc75a0fSeric #endif
4229c9e68d9Seric 		if (mode == SM_VERIFY)
4239c9e68d9Seric 		{
4249c9e68d9Seric 			e->e_to = q->q_paddr;
4259c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4268dfca105Seric 			{
427fafe46e1Seric 				if (q->q_host != NULL && q->q_host[0] != '\0')
4288dfca105Seric 					message("deliverable: mailer %s, host %s, user %s",
4298dfca105Seric 						q->q_mailer->m_name,
4308dfca105Seric 						q->q_host,
4318dfca105Seric 						q->q_user);
432fafe46e1Seric 				else
433fafe46e1Seric 					message("deliverable: mailer %s, user %s",
434fafe46e1Seric 						q->q_mailer->m_name,
435fafe46e1Seric 						q->q_user);
4368dfca105Seric 			}
4379c9e68d9Seric 		}
4389c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4399c9e68d9Seric 		{
4409c9e68d9Seric # ifdef QUEUE
4419c9e68d9Seric 			/*
4429c9e68d9Seric 			**  Checkpoint the send list every few addresses
4439c9e68d9Seric 			*/
4449c9e68d9Seric 
4459c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4469c9e68d9Seric 			{
4479c9e68d9Seric 				queueup(e, TRUE, FALSE);
4489c9e68d9Seric 				e->e_nsent = 0;
4499c9e68d9Seric 			}
4509c9e68d9Seric # endif /* QUEUE */
4519c9e68d9Seric 			(void) deliver(e, q);
4529c9e68d9Seric 		}
4539c9e68d9Seric 	}
4549c9e68d9Seric 	Verbose = oldverbose;
4559c9e68d9Seric 
456fdc75a0fSeric #ifdef XDEBUG
457fdc75a0fSeric 	checkfd012("end of sendenvelope");
458fdc75a0fSeric #endif
459fdc75a0fSeric 
4609c9e68d9Seric 	if (mode == SM_FORK)
4619c9e68d9Seric 		finis();
4629c9e68d9Seric }
4639c9e68d9Seric /*
4649c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4659c9e68d9Seric **
4669c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4679c9e68d9Seric **	two processes on the same stack!!!
4689c9e68d9Seric **
4699c9e68d9Seric **	Parameters:
4709c9e68d9Seric **		none.
4719c9e68d9Seric **
4729c9e68d9Seric **	Returns:
4739c9e68d9Seric **		From a macro???  You've got to be kidding!
4749c9e68d9Seric **
4759c9e68d9Seric **	Side Effects:
4769c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4779c9e68d9Seric **			pid of child in parent, zero in child.
4789c9e68d9Seric **			-1 on unrecoverable error.
4799c9e68d9Seric **
4809c9e68d9Seric **	Notes:
4819c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4829c9e68d9Seric **		vfork for you.....
4839c9e68d9Seric */
4849c9e68d9Seric 
4859c9e68d9Seric # define NFORKTRIES	5
4869c9e68d9Seric 
4879c9e68d9Seric # ifndef FORK
4889c9e68d9Seric # define FORK	fork
4899c9e68d9Seric # endif
4909c9e68d9Seric 
4919c9e68d9Seric # define DOFORK(fORKfN) \
4929c9e68d9Seric {\
4939c9e68d9Seric 	register int i;\
4949c9e68d9Seric \
4959c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4969c9e68d9Seric 	{\
4979c9e68d9Seric 		pid = fORKfN();\
4989c9e68d9Seric 		if (pid >= 0)\
4999c9e68d9Seric 			break;\
5009c9e68d9Seric 		if (i > 0)\
5019c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5029c9e68d9Seric 	}\
5039c9e68d9Seric }
5049c9e68d9Seric /*
5059c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5069c9e68d9Seric **
5079c9e68d9Seric **	Parameters:
5089c9e68d9Seric **		none.
5099c9e68d9Seric **
5109c9e68d9Seric **	Returns:
5119c9e68d9Seric **		pid of child in parent.
5129c9e68d9Seric **		zero in child.
5139c9e68d9Seric **		-1 on error.
5149c9e68d9Seric **
5159c9e68d9Seric **	Side Effects:
5169c9e68d9Seric **		returns twice, once in parent and once in child.
5179c9e68d9Seric */
5189c9e68d9Seric 
5199c9e68d9Seric dofork()
5209c9e68d9Seric {
5219c9e68d9Seric 	register int pid;
5229c9e68d9Seric 
5239c9e68d9Seric 	DOFORK(fork);
5249c9e68d9Seric 	return (pid);
5259c9e68d9Seric }
5269c9e68d9Seric /*
52713bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
52813bbc08cSeric **
52913bbc08cSeric **	This routine delivers to everyone on the same host as the
53013bbc08cSeric **	user on the head of the list.  It is clever about mailers
53113bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
53213bbc08cSeric **	that it will deliver to all these addresses however -- so
53313bbc08cSeric **	deliver should be called once for each address on the
53413bbc08cSeric **	list.
53525a99e2eSeric **
53625a99e2eSeric **	Parameters:
537588cad61Seric **		e -- the envelope to deliver.
538c77d1c25Seric **		firstto -- head of the address list to deliver to.
53925a99e2eSeric **
54025a99e2eSeric **	Returns:
54125a99e2eSeric **		zero -- successfully delivered.
54225a99e2eSeric **		else -- some failure, see ExitStat for more info.
54325a99e2eSeric **
54425a99e2eSeric **	Side Effects:
54525a99e2eSeric **		The standard input is passed off to someone.
54625a99e2eSeric */
54725a99e2eSeric 
548588cad61Seric deliver(e, firstto)
549588cad61Seric 	register ENVELOPE *e;
550c77d1c25Seric 	ADDRESS *firstto;
55125a99e2eSeric {
55278442df3Seric 	char *host;			/* host being sent to */
55378442df3Seric 	char *user;			/* user being sent to */
55425a99e2eSeric 	char **pvp;
5555dfc646bSeric 	register char **mvp;
55625a99e2eSeric 	register char *p;
557588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5586259796dSeric 	ADDRESS *ctladdr;
559b31e7f2bSeric 	register MCI *mci;
560c77d1c25Seric 	register ADDRESS *to = firstto;
561c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
562772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
563911693bfSbostic 	int rcode;			/* response code */
564e103b48fSeric 	char *firstsig;			/* signature of firstto */
5659c9e68d9Seric 	int pid;
5669c9e68d9Seric 	char *curhost;
5679c9e68d9Seric 	int mpvect[2];
5689c9e68d9Seric 	int rpvect[2];
569ee6bf8dfSeric 	char *pv[MAXPV+1];
570579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
571ee6bf8dfSeric 	char buf[MAXNAME];
572c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
573fabb3bd4Seric 	extern int checkcompat();
57425a99e2eSeric 
57535490626Seric 	errno = 0;
576ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5775dfc646bSeric 		return (0);
57825a99e2eSeric 
579134746fbSeric #ifdef NAMED_BIND
580912a731aSbostic 	/* unless interactive, try twice, over a minute */
581912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
582912a731aSbostic 		_res.retrans = 30;
583912a731aSbostic 		_res.retry = 2;
584912a731aSbostic 	}
585d4bd8f0eSbostic #endif
586912a731aSbostic 
58751552439Seric 	m = to->q_mailer;
58851552439Seric 	host = to->q_host;
589c9be6216Seric 	CurEnv = e;			/* just in case */
5904384d521Seric 	e->e_statmsg = NULL;
591df106f0bSeric 	SmtpError[0] = '\0';
59251552439Seric 
5936ef48975Seric 	if (tTd(10, 1))
5945dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
59551552439Seric 			m->m_mno, host, to->q_user);
596ab81ee53Seric 	if (tTd(10, 100))
597ab81ee53Seric 		printopenfds(FALSE);
598f3dbc832Seric 
599f3dbc832Seric 	/*
600f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
601f3dbc832Seric 	**  connections now, just mark these addresses and return.
602f3dbc832Seric 	**	This is useful if we want to batch connections to
603f3dbc832Seric 	**	reduce load.  This will cause the messages to be
604f3dbc832Seric 	**	queued up, and a daemon will come along to send the
605f3dbc832Seric 	**	messages later.
606f3dbc832Seric 	**		This should be on a per-mailer basis.
607f3dbc832Seric 	*/
608f3dbc832Seric 
60979aa5155Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
610f3dbc832Seric 	{
611f3dbc832Seric 		for (; to != NULL; to = to->q_next)
612f4560e80Seric 		{
613ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
614c6e18ac5Seric 			    to->q_mailer != m)
615f4560e80Seric 				continue;
616268a25e1Seric 			to->q_flags |= QQUEUEUP;
617588cad61Seric 			e->e_to = to->q_paddr;
61808b25121Seric 			message("queued");
6192f624c86Seric 			if (LogLevel > 8)
6204dee0003Seric 				logdelivery(m, NULL, "queued", NULL, e);
621f4560e80Seric 		}
622588cad61Seric 		e->e_to = NULL;
623f3dbc832Seric 		return (0);
624f3dbc832Seric 	}
625f3dbc832Seric 
62625a99e2eSeric 	/*
6275dfc646bSeric 	**  Do initial argv setup.
6285dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6295dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6305dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6315dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6325dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6333bea8136Seric 	**		The from address rewrite is expected to make
6343bea8136Seric 	**		the address relative to the other end.
6355dfc646bSeric 	*/
6365dfc646bSeric 
63778442df3Seric 	/* rewrite from address, using rewriting rules */
638efe54562Seric 	rcode = EX_OK;
639efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
640efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
641efe54562Seric 					   &rcode, e));
642ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
643588cad61Seric 	define('h', host, e);			/* to host */
6445dfc646bSeric 	Errors = 0;
6455dfc646bSeric 	pvp = pv;
6465dfc646bSeric 	*pvp++ = m->m_argv[0];
6475dfc646bSeric 
6485dfc646bSeric 	/* insert -f or -r flag as appropriate */
64957fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6505dfc646bSeric 	{
65157fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6525dfc646bSeric 			*pvp++ = "-f";
6535dfc646bSeric 		else
6545dfc646bSeric 			*pvp++ = "-r";
655c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6565dfc646bSeric 	}
6575dfc646bSeric 
6585dfc646bSeric 	/*
6595dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6605dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6615dfc646bSeric 	**  be one of these, and there are only a few more slots
6625dfc646bSeric 	**  in the pv after it.
6635dfc646bSeric 	*/
6645dfc646bSeric 
6655dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6665dfc646bSeric 	{
6672bc47524Seric 		/* can't use strchr here because of sign extension problems */
6682bc47524Seric 		while (*p != '\0')
6692bc47524Seric 		{
6702bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6712bc47524Seric 			{
6722bc47524Seric 				if (*p == 'u')
6735dfc646bSeric 					break;
6742bc47524Seric 			}
6752bc47524Seric 		}
6762bc47524Seric 
6772bc47524Seric 		if (*p != '\0')
6785dfc646bSeric 			break;
6795dfc646bSeric 
6805dfc646bSeric 		/* this entry is safe -- go ahead and process it */
681588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6825dfc646bSeric 		*pvp++ = newstr(buf);
6835dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6845dfc646bSeric 		{
68508b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6865dfc646bSeric 			return (-1);
6875dfc646bSeric 		}
6885dfc646bSeric 	}
689c579ef51Seric 
69033db8731Seric 	/*
69133db8731Seric 	**  If we have no substitution for the user name in the argument
69233db8731Seric 	**  list, we know that we must supply the names otherwise -- and
69333db8731Seric 	**  SMTP is the answer!!
69433db8731Seric 	*/
69533db8731Seric 
6965dfc646bSeric 	if (*mvp == NULL)
697c579ef51Seric 	{
698c579ef51Seric 		/* running SMTP */
6992c7e1b8dSeric # ifdef SMTP
700c579ef51Seric 		clever = TRUE;
701c579ef51Seric 		*pvp = NULL;
7026c2c3107Seric # else /* SMTP */
70333db8731Seric 		/* oops!  we don't implement SMTP */
704df106f0bSeric 		syserr("554 SMTP style mailer not implemented");
7052c7e1b8dSeric 		return (EX_SOFTWARE);
7066c2c3107Seric # endif /* SMTP */
707c579ef51Seric 	}
7085dfc646bSeric 
7095dfc646bSeric 	/*
7105dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7115dfc646bSeric 	**  run through our address list and append all the addresses
7125dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7135dfc646bSeric 	**  always send another copy later.
7145dfc646bSeric 	*/
7155dfc646bSeric 
7165dfc646bSeric 	tobuf[0] = '\0';
717588cad61Seric 	e->e_to = tobuf;
7186259796dSeric 	ctladdr = NULL;
719e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7205dfc646bSeric 	for (; to != NULL; to = to->q_next)
7215dfc646bSeric 	{
7225dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
72357fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7245dfc646bSeric 			break;
7255dfc646bSeric 
7265dfc646bSeric 		/* if already sent or not for this host, don't send */
727ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
728e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
729e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7305dfc646bSeric 			continue;
7316259796dSeric 
7324b22ea87Seric 		/* avoid overflowing tobuf */
733aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7344b22ea87Seric 			break;
7354b22ea87Seric 
7366ef48975Seric 		if (tTd(10, 1))
737772e6e50Seric 		{
738772e6e50Seric 			printf("\nsend to ");
739772e6e50Seric 			printaddr(to, FALSE);
740772e6e50Seric 		}
741772e6e50Seric 
7426259796dSeric 		/* compute effective uid/gid when sending */
743960e665aSeric 		/* XXX perhaps this should be to->q_mailer != LocalMailer ?? */
744960e665aSeric 		/* XXX perhaps it should be a mailer flag? */
745960e665aSeric 		if (to->q_mailer == ProgMailer || to->q_mailer == FileMailer)
7466259796dSeric 			ctladdr = getctladdr(to);
7476259796dSeric 
7485dfc646bSeric 		user = to->q_user;
749588cad61Seric 		e->e_to = to->q_paddr;
75075f1ade9Seric 		if (tTd(10, 5))
75175f1ade9Seric 		{
75275f1ade9Seric 			printf("deliver: QDONTSEND ");
75375f1ade9Seric 			printaddr(to, FALSE);
75475f1ade9Seric 		}
755ee4b0922Seric 		to->q_flags |= QDONTSEND;
7565dfc646bSeric 
7575dfc646bSeric 		/*
7585dfc646bSeric 		**  Check to see that these people are allowed to
7595dfc646bSeric 		**  talk to each other.
7602a6e0786Seric 		*/
7612a6e0786Seric 
76269582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
76369582d2fSeric 		{
76469582d2fSeric 			NoReturn = TRUE;
76508b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
7664dee0003Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e);
76769582d2fSeric 			continue;
76869582d2fSeric 		}
769fabb3bd4Seric 		rcode = checkcompat(to, e);
7701793c9c5Seric 		if (rcode != EX_OK)
7715dfc646bSeric 		{
7721000c37aSeric 			markfailure(e, to, rcode);
7734dee0003Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
7745dfc646bSeric 			continue;
7755dfc646bSeric 		}
7762a6e0786Seric 
7772a6e0786Seric 		/*
7789ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7799ec9501bSeric 		**	about them.
78025a99e2eSeric 		*/
78125a99e2eSeric 
78257fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
78325a99e2eSeric 		{
7841d8f1806Seric 			stripquotes(user);
7851d8f1806Seric 			stripquotes(host);
78625a99e2eSeric 		}
78725a99e2eSeric 
788cdb828c5Seric 		/* hack attack -- delivermail compatibility */
789cdb828c5Seric 		if (m == ProgMailer && *user == '|')
790cdb828c5Seric 			user++;
791cdb828c5Seric 
79225a99e2eSeric 		/*
7933efaed6eSeric 		**  If an error message has already been given, don't
7943efaed6eSeric 		**	bother to send to this address.
7953efaed6eSeric 		**
7963efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7973efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
7983efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
7993efaed6eSeric 		*/
8003efaed6eSeric 
8016cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8023efaed6eSeric 			continue;
8033efaed6eSeric 
804f2fec898Seric 		/* save statistics.... */
805588cad61Seric 		markstats(e, to);
806f2fec898Seric 
8073efaed6eSeric 		/*
80825a99e2eSeric 		**  See if this user name is "special".
80925a99e2eSeric 		**	If the user name has a slash in it, assume that this
81051552439Seric 		**	is a file -- send it off without further ado.  Note
81151552439Seric 		**	that this type of addresses is not processed along
81251552439Seric 		**	with the others, so we fudge on the To person.
81325a99e2eSeric 		*/
81425a99e2eSeric 
8152c017f8dSeric 		if (m == FileMailer)
81625a99e2eSeric 		{
81703463a75Seric 			rcode = mailfile(user, ctladdr, e);
81803463a75Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
819dde5acadSeric 			if (rcode == EX_OK)
820dde5acadSeric 				to->q_flags |= QSENT;
8215dfc646bSeric 			continue;
82225a99e2eSeric 		}
82325a99e2eSeric 
82413bbc08cSeric 		/*
82513bbc08cSeric 		**  Address is verified -- add this user to mailer
82613bbc08cSeric 		**  argv, and add it to the print list of recipients.
82713bbc08cSeric 		*/
82813bbc08cSeric 
829508daeccSeric 		/* link together the chain of recipients */
830508daeccSeric 		to->q_tchain = tochain;
831508daeccSeric 		tochain = to;
832508daeccSeric 
8335dfc646bSeric 		/* create list of users for error messages */
834db8841e9Seric 		(void) strcat(tobuf, ",");
835db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
836588cad61Seric 		define('u', user, e);		/* to user */
837b6f89e6eSeric 		p = to->q_home;
838b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
839b6f89e6eSeric 			p = ctladdr->q_home;
840b6f89e6eSeric 		define('z', p, e);	/* user's home */
8415dfc646bSeric 
842c579ef51Seric 		/*
843508daeccSeric 		**  Expand out this user into argument list.
844c579ef51Seric 		*/
845c579ef51Seric 
846508daeccSeric 		if (!clever)
847c579ef51Seric 		{
848588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8495dfc646bSeric 			*pvp++ = newstr(buf);
8505dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8515dfc646bSeric 			{
8525dfc646bSeric 				/* allow some space for trailing parms */
8535dfc646bSeric 				break;
8545dfc646bSeric 			}
8555dfc646bSeric 		}
856c579ef51Seric 	}
8575dfc646bSeric 
858145b49b1Seric 	/* see if any addresses still exist */
859145b49b1Seric 	if (tobuf[0] == '\0')
860c579ef51Seric 	{
861588cad61Seric 		define('g', (char *) NULL, e);
862145b49b1Seric 		return (0);
863c579ef51Seric 	}
864145b49b1Seric 
8655dfc646bSeric 	/* print out messages as full list */
86663780dbdSeric 	e->e_to = tobuf + 1;
8675dfc646bSeric 
8685dfc646bSeric 	/*
8695dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8705dfc646bSeric 	*/
8715dfc646bSeric 
872c579ef51Seric 	while (!clever && *++mvp != NULL)
8735dfc646bSeric 	{
874588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8755dfc646bSeric 		*pvp++ = newstr(buf);
8765dfc646bSeric 		if (pvp >= &pv[MAXPV])
87708b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8785dfc646bSeric 	}
8795dfc646bSeric 	*pvp++ = NULL;
8805dfc646bSeric 
88125a99e2eSeric 	/*
88225a99e2eSeric 	**  Call the mailer.
8836328bdf7Seric 	**	The argument vector gets built, pipes
88425a99e2eSeric 	**	are created as necessary, and we fork & exec as
8856328bdf7Seric 	**	appropriate.
886c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
88725a99e2eSeric 	*/
88825a99e2eSeric 
889960e665aSeric 	/*XXX this seems a bit wierd */
890960e665aSeric 	if (ctladdr == NULL && bitset(QGOODUID, e->e_from.q_flags))
891960e665aSeric 		ctladdr = &e->e_from;
892960e665aSeric 
893134746fbSeric #ifdef NAMED_BIND
8942bcc6d2dSeric 	if (ConfigLevel < 2)
895912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
896134746fbSeric #endif
8979c9e68d9Seric 
8989c9e68d9Seric 	if (tTd(11, 1))
899134746fbSeric 	{
9009c9e68d9Seric 		printf("openmailer:");
9019c9e68d9Seric 		printav(pv);
9029c9e68d9Seric 	}
9039c9e68d9Seric 	errno = 0;
9049c9e68d9Seric 
9059c9e68d9Seric 	CurHostName = m->m_mailer;
9069c9e68d9Seric 
9079c9e68d9Seric 	/*
9089c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9099c9e68d9Seric 	**  connection.
9109c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9119c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9129c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9139c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9149c9e68d9Seric 	*/
9159c9e68d9Seric 
9169c9e68d9Seric 	curhost = NULL;
917c931b82bSeric 	SmtpPhase = NULL;
918df106f0bSeric 	mci = NULL;
9199c9e68d9Seric 
9207febfd66Seric #ifdef XDEBUG
9217febfd66Seric 	{
9227febfd66Seric 		char wbuf[MAXLINE];
9237febfd66Seric 
9247febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9257febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
9267febfd66Seric 		checkfd012(wbuf);
9277febfd66Seric 	}
9287febfd66Seric #endif
9297febfd66Seric 
9309c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9319c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9329c9e68d9Seric 	{
9339c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9349c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9359c9e68d9Seric 		mci->mci_in = stdin;
9369c9e68d9Seric 		mci->mci_out = stdout;
9379c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9389c9e68d9Seric 		mci->mci_mailer = m;
9399c9e68d9Seric 	}
9409c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9419c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9429c9e68d9Seric 	{
9439c9e68d9Seric #ifdef DAEMON
9449c9e68d9Seric 		register int i;
9459c9e68d9Seric 		register u_short port;
9469c9e68d9Seric 
947a5d44642Seric 		if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
948a5d44642Seric 		{
949a5d44642Seric 			syserr("null host name for %s mailer", m->m_mailer);
950a5d44642Seric 			rcode = EX_CONFIG;
951a5d44642Seric 			goto give_up;
952a5d44642Seric 		}
953a5d44642Seric 
9549c9e68d9Seric 		CurHostName = pv[1];
9559c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9569c9e68d9Seric 
9579c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9589c9e68d9Seric 		{
959a5d44642Seric 			syserr("null host signature for %s", pv[1]);
960845e533cSeric 			rcode = EX_OSERR;
961b31e7f2bSeric 			goto give_up;
962b31e7f2bSeric 		}
9639c9e68d9Seric 
9649c9e68d9Seric 		if (!clever)
9659c9e68d9Seric 		{
9669c9e68d9Seric 			syserr("554 non-clever IPC");
967df106f0bSeric 			rcode = EX_CONFIG;
9689c9e68d9Seric 			goto give_up;
9699c9e68d9Seric 		}
9709c9e68d9Seric 		if (pv[2] != NULL)
9719c9e68d9Seric 			port = atoi(pv[2]);
9729c9e68d9Seric 		else
9739c9e68d9Seric 			port = 0;
9749c9e68d9Seric tryhost:
9759c9e68d9Seric 		while (*curhost != '\0')
9769c9e68d9Seric 		{
9779c9e68d9Seric 			register char *p;
9789c9e68d9Seric 			static char hostbuf[MAXNAME];
9799c9e68d9Seric 
9809c9e68d9Seric 			/* pull the next host from the signature */
9819c9e68d9Seric 			p = strchr(curhost, ':');
9829c9e68d9Seric 			if (p == NULL)
9839c9e68d9Seric 				p = &curhost[strlen(curhost)];
984df106f0bSeric 			if (p == curhost)
985df106f0bSeric 			{
986df106f0bSeric 				syserr("deliver: null host name in signature");
9878087428aSeric 				curhost++;
988df106f0bSeric 				continue;
989df106f0bSeric 			}
9909c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9919c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9929c9e68d9Seric 			if (*p != '\0')
9939c9e68d9Seric 				p++;
9949c9e68d9Seric 			curhost = p;
9959c9e68d9Seric 
9969c9e68d9Seric 			/* see if we already know that this host is fried */
9979c9e68d9Seric 			CurHostName = hostbuf;
9989c9e68d9Seric 			mci = mci_get(hostbuf, m);
9999c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
10009c9e68d9Seric 			{
10019c9e68d9Seric 				if (tTd(11, 1))
10029c9e68d9Seric 				{
10039c9e68d9Seric 					printf("openmailer: ");
10044052916eSeric 					mci_dump(mci, FALSE);
10059c9e68d9Seric 				}
10069c9e68d9Seric 				CurHostName = mci->mci_host;
10079c9e68d9Seric 				break;
10089c9e68d9Seric 			}
10099c9e68d9Seric 			mci->mci_mailer = m;
10109c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
10119c9e68d9Seric 				continue;
10129c9e68d9Seric 
10139c9e68d9Seric 			/* try the connection */
10149c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
10159c9e68d9Seric 			message("Connecting to %s (%s)...",
10169c9e68d9Seric 				hostbuf, m->m_name);
10179c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
10189c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
10199c9e68d9Seric 			mci->mci_exitstat = i;
10209c9e68d9Seric 			mci->mci_errno = errno;
1021f170942cSeric #ifdef NAMED_BIND
1022f170942cSeric 			mci->mci_herrno = h_errno;
1023f170942cSeric #endif
10249c9e68d9Seric 			if (i == EX_OK)
10259c9e68d9Seric 			{
10269c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10279c9e68d9Seric 				mci_cache(mci);
1028f170942cSeric 				if (TrafficLogFile != NULL)
1029f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1030f170942cSeric 						getpid(), hostbuf);
10319c9e68d9Seric 				break;
10329c9e68d9Seric 			}
10339c9e68d9Seric 			else if (tTd(11, 1))
10349c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10359c9e68d9Seric 					i, errno);
10369c9e68d9Seric 
10379c9e68d9Seric 			/* enter status of this host */
10389c9e68d9Seric 			setstat(i);
1039df106f0bSeric 
1040df106f0bSeric 			/* should print some message here for -v mode */
1041df106f0bSeric 		}
1042df106f0bSeric 		if (mci == NULL)
1043df106f0bSeric 		{
1044df106f0bSeric 			syserr("deliver: no host name");
1045df106f0bSeric 			rcode = EX_OSERR;
1046df106f0bSeric 			goto give_up;
10479c9e68d9Seric 		}
10489c9e68d9Seric 		mci->mci_pid = 0;
10499c9e68d9Seric #else /* no DAEMON */
10509c9e68d9Seric 		syserr("554 openmailer: no IPC");
10519c9e68d9Seric 		if (tTd(11, 1))
10529c9e68d9Seric 			printf("openmailer: NULL\n");
1053df106f0bSeric 		rcode = EX_UNAVAILABLE;
1054df106f0bSeric 		goto give_up;
10559c9e68d9Seric #endif /* DAEMON */
10569c9e68d9Seric 	}
10579c9e68d9Seric 	else
10589c9e68d9Seric 	{
1059f170942cSeric 		if (TrafficLogFile != NULL)
10600e3bfef5Seric 		{
1061f170942cSeric 			char **av;
1062f170942cSeric 
1063f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1064f170942cSeric 			for (av = pv; *av != NULL; av++)
1065f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1066f170942cSeric 			fprintf(TrafficLogFile, "\n");
10676fe8c3bcSeric 		}
10686fe8c3bcSeric 
10699c9e68d9Seric 		/* create a pipe to shove the mail through */
10709c9e68d9Seric 		if (pipe(mpvect) < 0)
10719c9e68d9Seric 		{
10720e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10730e3bfef5Seric 				e->e_to, m->m_name);
10749c9e68d9Seric 			if (tTd(11, 1))
10759c9e68d9Seric 				printf("openmailer: NULL\n");
10769c9e68d9Seric 			rcode = EX_OSERR;
10779c9e68d9Seric 			goto give_up;
10789c9e68d9Seric 		}
10799c9e68d9Seric 
10809c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10819c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10829c9e68d9Seric 		{
10830e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10840e3bfef5Seric 				e->e_to, m->m_name);
10859c9e68d9Seric 			(void) close(mpvect[0]);
10869c9e68d9Seric 			(void) close(mpvect[1]);
10879c9e68d9Seric 			if (tTd(11, 1))
10889c9e68d9Seric 				printf("openmailer: NULL\n");
10899c9e68d9Seric 			rcode = EX_OSERR;
10909c9e68d9Seric 			goto give_up;
10919c9e68d9Seric 		}
10929c9e68d9Seric 
10939c9e68d9Seric 		/*
10949c9e68d9Seric 		**  Actually fork the mailer process.
10959c9e68d9Seric 		**	DOFORK is clever about retrying.
10969c9e68d9Seric 		**
10979c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10989c9e68d9Seric 		**	around so that endmail will get it.
10999c9e68d9Seric 		*/
11009c9e68d9Seric 
11019c9e68d9Seric 		if (e->e_xfp != NULL)
11029c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
11039c9e68d9Seric 		(void) fflush(stdout);
11049c9e68d9Seric # ifdef SIGCHLD
11052b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
11069c9e68d9Seric # endif /* SIGCHLD */
11079c9e68d9Seric 		DOFORK(FORK);
11089c9e68d9Seric 		/* pid is set by DOFORK */
11099c9e68d9Seric 		if (pid < 0)
11109c9e68d9Seric 		{
11119c9e68d9Seric 			/* failure */
11120e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
11130e3bfef5Seric 				e->e_to, m->m_name);
11149c9e68d9Seric 			(void) close(mpvect[0]);
11159c9e68d9Seric 			(void) close(mpvect[1]);
11169c9e68d9Seric 			if (clever)
11179c9e68d9Seric 			{
11189c9e68d9Seric 				(void) close(rpvect[0]);
11199c9e68d9Seric 				(void) close(rpvect[1]);
11209c9e68d9Seric 			}
11219c9e68d9Seric 			if (tTd(11, 1))
11229c9e68d9Seric 				printf("openmailer: NULL\n");
11239c9e68d9Seric 			rcode = EX_OSERR;
11249c9e68d9Seric 			goto give_up;
11259c9e68d9Seric 		}
11269c9e68d9Seric 		else if (pid == 0)
11279c9e68d9Seric 		{
11289c9e68d9Seric 			int i;
11299c9e68d9Seric 			int saveerrno;
11309c9e68d9Seric 			char **ep;
11319c9e68d9Seric 			char *env[MAXUSERENVIRON];
11329c9e68d9Seric 			extern char **environ;
11339c9e68d9Seric 			extern int DtableSize;
11349c9e68d9Seric 
11359c9e68d9Seric 			/* child -- set up input & exec mailer */
11362b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
11372b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
11382b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11399c9e68d9Seric 
114044f2317fSeric 			/* reset user and group */
114144f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
114244f2317fSeric 			{
114344f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
114444f2317fSeric 				{
114544f2317fSeric 					(void) initgroups(DefUser, DefGid);
1146fc354902Seric 					(void) setgid(DefGid);
114744f2317fSeric 					(void) setuid(DefUid);
114844f2317fSeric 				}
114944f2317fSeric 				else
115044f2317fSeric 				{
115144f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
115244f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
115344f2317fSeric 						ctladdr->q_gid);
115451cfe111Seric 					(void) setgid(ctladdr->q_gid);
115544f2317fSeric 					(void) setuid(ctladdr->q_uid);
115644f2317fSeric 				}
115744f2317fSeric 			}
115844f2317fSeric 
115944f2317fSeric 			if (tTd(11, 2))
116044f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
116144f2317fSeric 					getuid(), geteuid());
116244f2317fSeric 
1163b986f6aaSeric 			/* move into some "safe" directory */
1164b986f6aaSeric 			if (m->m_execdir != NULL)
1165b986f6aaSeric 			{
1166b986f6aaSeric 				char *p, *q;
1167b986f6aaSeric 				char buf[MAXLINE];
1168b986f6aaSeric 
1169b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1170b986f6aaSeric 				{
1171b986f6aaSeric 					q = strchr(p, ':');
1172b986f6aaSeric 					if (q != NULL)
1173b986f6aaSeric 						*q = '\0';
1174b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1175b986f6aaSeric 					if (q != NULL)
1176b986f6aaSeric 						*q++ = ':';
1177b986f6aaSeric 					if (tTd(11, 20))
1178b986f6aaSeric 						printf("openmailer: trydir %s\n",
1179b986f6aaSeric 							buf);
1180b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1181b986f6aaSeric 						break;
1182b986f6aaSeric 				}
1183b986f6aaSeric 			}
1184b986f6aaSeric 
11859c9e68d9Seric 			/* arrange to filter std & diag output of command */
11869c9e68d9Seric 			if (clever)
11879c9e68d9Seric 			{
11889c9e68d9Seric 				(void) close(rpvect[0]);
11896fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11906fe8c3bcSeric 				{
11910e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11920e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11936fe8c3bcSeric 					_exit(EX_OSERR);
11946fe8c3bcSeric 				}
11959c9e68d9Seric 				(void) close(rpvect[1]);
11969c9e68d9Seric 			}
11979c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11989c9e68d9Seric 			{
11999c9e68d9Seric 				/* put mailer output in transcript */
12006fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
12016fe8c3bcSeric 				{
12020e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
12030e3bfef5Seric 						e->e_to, m->m_name,
12046fe8c3bcSeric 						fileno(e->e_xfp));
12056fe8c3bcSeric 					_exit(EX_OSERR);
12069c9e68d9Seric 				}
12076fe8c3bcSeric 			}
12086fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
12096fe8c3bcSeric 			{
12100e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
12110e3bfef5Seric 					e->e_to, m->m_name);
12126fe8c3bcSeric 				_exit(EX_OSERR);
12136fe8c3bcSeric 			}
12149c9e68d9Seric 
12159c9e68d9Seric 			/* arrange to get standard input */
12169c9e68d9Seric 			(void) close(mpvect[1]);
12179c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
12189c9e68d9Seric 			{
12190e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
12200e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
12219c9e68d9Seric 				_exit(EX_OSERR);
12229c9e68d9Seric 			}
12239c9e68d9Seric 			(void) close(mpvect[0]);
12249c9e68d9Seric 
12259c9e68d9Seric 			/* arrange for all the files to be closed */
12269c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12279c9e68d9Seric 			{
12289c9e68d9Seric 				register int j;
122944f2317fSeric 
12309c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12319c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
12329c9e68d9Seric 			}
12339c9e68d9Seric 
12349c9e68d9Seric 			/* set up the mailer environment */
12359c9e68d9Seric 			i = 0;
12369c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12379c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12389c9e68d9Seric 			{
12399c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12409c9e68d9Seric 					env[i++] = *ep;
12419c9e68d9Seric 			}
12429c9e68d9Seric 			env[i++] = NULL;
12439c9e68d9Seric 
12449c9e68d9Seric 			/* try to execute the mailer */
12459c9e68d9Seric 			execve(m->m_mailer, pv, env);
12469c9e68d9Seric 			saveerrno = errno;
12479c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12487c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12497c941fd2Seric 				_exit(EX_OSERR);
12509c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12519c9e68d9Seric 		}
12529c9e68d9Seric 
12539c9e68d9Seric 		/*
12549c9e68d9Seric 		**  Set up return value.
12559c9e68d9Seric 		*/
12569c9e68d9Seric 
12579c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12589c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12599c9e68d9Seric 		mci->mci_mailer = m;
12609c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12619c9e68d9Seric 		mci->mci_pid = pid;
12629c9e68d9Seric 		(void) close(mpvect[0]);
12639c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1264335eae58Seric 		if (mci->mci_out == NULL)
1265335eae58Seric 		{
1266335eae58Seric 			syserr("deliver: cannot create mailer output channel, fd=%d",
1267335eae58Seric 				mpvect[1]);
1268335eae58Seric 			(void) close(mpvect[1]);
1269335eae58Seric 			if (clever)
1270335eae58Seric 			{
1271335eae58Seric 				(void) close(rpvect[0]);
1272335eae58Seric 				(void) close(rpvect[1]);
1273335eae58Seric 			}
1274335eae58Seric 			rcode = EX_OSERR;
1275335eae58Seric 			goto give_up;
1276335eae58Seric 		}
12779c9e68d9Seric 		if (clever)
12789c9e68d9Seric 		{
12799c9e68d9Seric 			(void) close(rpvect[1]);
12809c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
1281335eae58Seric 			if (mci->mci_in == NULL)
1282335eae58Seric 			{
1283335eae58Seric 				syserr("deliver: cannot create mailer input channel, fd=%d",
1284335eae58Seric 					mpvect[1]);
1285335eae58Seric 				(void) close(rpvect[0]);
1286335eae58Seric 				fclose(mci->mci_out);
1287335eae58Seric 				mci->mci_out = NULL;
1288335eae58Seric 				rcode = EX_OSERR;
1289335eae58Seric 				goto give_up;
1290335eae58Seric 			}
12919c9e68d9Seric 		}
12929c9e68d9Seric 		else
12939c9e68d9Seric 		{
12949c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12959c9e68d9Seric 			mci->mci_in = NULL;
12969c9e68d9Seric 		}
12979c9e68d9Seric 	}
12989c9e68d9Seric 
12999c9e68d9Seric 	/*
13009c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
13019c9e68d9Seric 	*/
13029c9e68d9Seric 
13039c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
13049c9e68d9Seric 	{
13059c9e68d9Seric 		smtpinit(m, mci, e);
13069c9e68d9Seric 	}
13079c9e68d9Seric 	if (tTd(11, 1))
13089c9e68d9Seric 	{
13099c9e68d9Seric 		printf("openmailer: ");
13104052916eSeric 		mci_dump(mci, FALSE);
13119c9e68d9Seric 	}
13129c9e68d9Seric 
13139c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1314b31e7f2bSeric 	{
1315b31e7f2bSeric 		/* couldn't open the mailer */
1316b31e7f2bSeric 		rcode = mci->mci_exitstat;
13172a6bc25bSeric 		errno = mci->mci_errno;
1318f170942cSeric #ifdef NAMED_BIND
1319f170942cSeric 		h_errno = mci->mci_herrno;
1320f170942cSeric #endif
1321b31e7f2bSeric 		if (rcode == EX_OK)
1322b31e7f2bSeric 		{
1323b31e7f2bSeric 			/* shouldn't happen */
132408b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
13256b0f339dSeric 				rcode, mci->mci_state, firstsig);
1326b31e7f2bSeric 			rcode = EX_SOFTWARE;
1327b31e7f2bSeric 		}
1328cb082c5bSeric 		else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0')
132990891494Seric 		{
133090891494Seric 			/* try next MX site */
133190891494Seric 			goto tryhost;
133290891494Seric 		}
1333b31e7f2bSeric 	}
1334b31e7f2bSeric 	else if (!clever)
1335b31e7f2bSeric 	{
1336b31e7f2bSeric 		/*
1337b31e7f2bSeric 		**  Format and send message.
1338b31e7f2bSeric 		*/
133915d084d5Seric 
1340b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1341b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1342b31e7f2bSeric 		putline("\n", mci->mci_out, m);
134303c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1344b31e7f2bSeric 
1345b31e7f2bSeric 		/* get the exit status */
1346c9be6216Seric 		rcode = endmailer(mci, e, pv);
1347134746fbSeric 	}
1348134746fbSeric 	else
1349b31e7f2bSeric #ifdef SMTP
1350134746fbSeric 	{
1351b31e7f2bSeric 		/*
1352b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1353b31e7f2bSeric 		*/
135415d084d5Seric 
1355b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1356b31e7f2bSeric 		if (rcode == EX_OK)
135775889e88Seric 		{
1358ded0d3daSkarels 			register char *t = tobuf;
1359ded0d3daSkarels 			register int i;
1360ded0d3daSkarels 
1361588cad61Seric 			/* send the recipient list */
136263780dbdSeric 			tobuf[0] = '\0';
136375889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
136475889e88Seric 			{
136563780dbdSeric 				e->e_to = to->q_paddr;
136615d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
136775889e88Seric 				{
136883b7ddc9Seric 					markfailure(e, to, i);
13694dee0003Seric 					giveresponse(i, m, mci, ctladdr, e);
137063780dbdSeric 				}
137175889e88Seric 				else
137275889e88Seric 				{
1373911693bfSbostic 					*t++ = ',';
1374b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1375b31e7f2bSeric 						continue;
1376ee39df61Seric 					*t = '\0';
1377588cad61Seric 				}
1378588cad61Seric 			}
1379588cad61Seric 
138063780dbdSeric 			/* now send the data */
138163780dbdSeric 			if (tobuf[0] == '\0')
1382b31e7f2bSeric 			{
13839c9e68d9Seric 				rcode = EX_OK;
138463780dbdSeric 				e->e_to = NULL;
1385b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1386b31e7f2bSeric 					smtprset(m, mci, e);
1387b31e7f2bSeric 			}
138875889e88Seric 			else
138975889e88Seric 			{
139063780dbdSeric 				e->e_to = tobuf + 1;
139175889e88Seric 				rcode = smtpdata(m, mci, e);
139263780dbdSeric 			}
139363780dbdSeric 
139463780dbdSeric 			/* now close the connection */
1395b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
139615d084d5Seric 				smtpquit(m, mci, e);
139763780dbdSeric 		}
1398cb082c5bSeric 		if (rcode != EX_OK && curhost != NULL && *curhost != '\0')
13999c9e68d9Seric 		{
14009c9e68d9Seric 			/* try next MX site */
14019c9e68d9Seric 			goto tryhost;
14029c9e68d9Seric 		}
1403c579ef51Seric 	}
1404b31e7f2bSeric #else /* not SMTP */
1405a05b3449Sbostic 	{
140608b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1407845e533cSeric 		rcode = EX_CONFIG;
1408b31e7f2bSeric 		goto give_up;
1409a05b3449Sbostic 	}
1410b31e7f2bSeric #endif /* SMTP */
1411134746fbSeric #ifdef NAMED_BIND
14122bcc6d2dSeric 	if (ConfigLevel < 2)
1413912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1414134746fbSeric #endif
14155dfc646bSeric 
1416b31e7f2bSeric 	/* arrange a return receipt if requested */
1417df106f0bSeric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1418df106f0bSeric 	    bitnset(M_LOCALMAILER, m->m_flags))
1419b31e7f2bSeric 	{
1420b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1421b31e7f2bSeric 		/* do we want to send back more info? */
1422b31e7f2bSeric 	}
1423b31e7f2bSeric 
1424c77d1c25Seric 	/*
142563780dbdSeric 	**  Do final status disposal.
142663780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1427c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1428c77d1c25Seric 	**		addressees.
1429c77d1c25Seric 	*/
1430c77d1c25Seric 
1431b31e7f2bSeric   give_up:
143263780dbdSeric 	if (tobuf[0] != '\0')
14334dee0003Seric 		giveresponse(rcode, m, mci, ctladdr, e);
1434772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1435b31e7f2bSeric 	{
1436dde5acadSeric 		if (rcode != EX_OK)
143783b7ddc9Seric 			markfailure(e, to, rcode);
1438dde5acadSeric 		else
1439655518ecSeric 		{
1440dde5acadSeric 			to->q_flags |= QSENT;
1441655518ecSeric 			e->e_nsent++;
1442df106f0bSeric 			if (e->e_receiptto != NULL &&
1443df106f0bSeric 			    bitnset(M_LOCALMAILER, m->m_flags))
1444df106f0bSeric 			{
1445df106f0bSeric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1446df106f0bSeric 					to->q_paddr);
1447df106f0bSeric 			}
1448655518ecSeric 		}
1449b31e7f2bSeric 	}
1450b31e7f2bSeric 
1451b31e7f2bSeric 	/*
1452b31e7f2bSeric 	**  Restore state and return.
1453b31e7f2bSeric 	*/
1454c77d1c25Seric 
14557febfd66Seric #ifdef XDEBUG
14567febfd66Seric 	{
14577febfd66Seric 		char wbuf[MAXLINE];
14587febfd66Seric 
14597febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
146044622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
146144622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
146244622ea3Seric 			m->m_name);
14637febfd66Seric 		checkfd012(wbuf);
14647febfd66Seric 	}
14657febfd66Seric #endif
14667febfd66Seric 
146735490626Seric 	errno = 0;
1468588cad61Seric 	define('g', (char *) NULL, e);
14695826d9d3Seric 	return (rcode);
147025a99e2eSeric }
14715dfc646bSeric /*
147283b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
147383b7ddc9Seric **
147483b7ddc9Seric **	Parameters:
147583b7ddc9Seric **		e -- the envelope we are sending.
147683b7ddc9Seric **		q -- the address to mark.
147783b7ddc9Seric **		rcode -- the code signifying the particular failure.
147883b7ddc9Seric **
147983b7ddc9Seric **	Returns:
148083b7ddc9Seric **		none.
148183b7ddc9Seric **
148283b7ddc9Seric **	Side Effects:
148383b7ddc9Seric **		marks the address (and possibly the envelope) with the
148483b7ddc9Seric **			failure so that an error will be returned or
148583b7ddc9Seric **			the message will be queued, as appropriate.
148683b7ddc9Seric */
148783b7ddc9Seric 
148883b7ddc9Seric markfailure(e, q, rcode)
148983b7ddc9Seric 	register ENVELOPE *e;
149083b7ddc9Seric 	register ADDRESS *q;
149183b7ddc9Seric 	int rcode;
149283b7ddc9Seric {
149319c47125Seric 	char buf[MAXLINE];
149419c47125Seric 
1495d5aafa3cSeric 	switch (rcode)
1496d5aafa3cSeric 	{
1497d5aafa3cSeric 	  case EX_OK:
1498d5aafa3cSeric 		break;
1499d5aafa3cSeric 
1500d5aafa3cSeric 	  case EX_TEMPFAIL:
1501d5aafa3cSeric 	  case EX_IOERR:
1502d5aafa3cSeric 	  case EX_OSERR:
150383b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1504d5aafa3cSeric 		break;
1505d5aafa3cSeric 
1506d5aafa3cSeric 	  default:
1507f170942cSeric 		q->q_flags |= QBADADDR;
1508d5aafa3cSeric 		break;
1509d5aafa3cSeric 	}
151083b7ddc9Seric }
151183b7ddc9Seric /*
1512c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1513c579ef51Seric **
1514c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1515c579ef51Seric **	violation), so we report those specially.  For other
1516c579ef51Seric **	errors, we choose a status message (into statmsg),
1517c579ef51Seric **	and if it represents an error, we print it.
1518c579ef51Seric **
1519c579ef51Seric **	Parameters:
1520c579ef51Seric **		pid -- pid of mailer.
1521c9be6216Seric **		e -- the current envelope.
1522c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1523c9be6216Seric **			(for error messages).
1524c579ef51Seric **
1525c579ef51Seric **	Returns:
1526c579ef51Seric **		exit code of mailer.
1527c579ef51Seric **
1528c579ef51Seric **	Side Effects:
1529c579ef51Seric **		none.
1530c579ef51Seric */
1531c579ef51Seric 
1532c9be6216Seric endmailer(mci, e, pv)
1533b31e7f2bSeric 	register MCI *mci;
1534c9be6216Seric 	register ENVELOPE *e;
1535c9be6216Seric 	char **pv;
1536c579ef51Seric {
1537588cad61Seric 	int st;
1538c579ef51Seric 
153975889e88Seric 	/* close any connections */
154075889e88Seric 	if (mci->mci_in != NULL)
154176f3d53fSeric 		(void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in");
154275889e88Seric 	if (mci->mci_out != NULL)
154376f3d53fSeric 		(void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out");
154475889e88Seric 	mci->mci_in = mci->mci_out = NULL;
154575889e88Seric 	mci->mci_state = MCIS_CLOSED;
154675889e88Seric 
154733db8731Seric 	/* in the IPC case there is nothing to wait for */
154875889e88Seric 	if (mci->mci_pid == 0)
154933db8731Seric 		return (EX_OK);
155033db8731Seric 
155133db8731Seric 	/* wait for the mailer process to die and collect status */
155275889e88Seric 	st = waitfor(mci->mci_pid);
1553588cad61Seric 	if (st == -1)
155478de67c1Seric 	{
1555c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1556588cad61Seric 		return (EX_SOFTWARE);
1557c579ef51Seric 	}
155833db8731Seric 
1559bf9bc890Seric 	if (WIFEXITED(st))
1560c579ef51Seric 	{
1561bf9bc890Seric 		/* normal death -- return status */
1562bf9bc890Seric 		return (WEXITSTATUS(st));
1563bf9bc890Seric 	}
1564bf9bc890Seric 
1565bf9bc890Seric 	/* it died a horrid death */
1566*550092c3Seric 	syserr("451 mailer %s died with signal %o",
1567*550092c3Seric 		mci->mci_mailer->m_name, st);
1568c9be6216Seric 
1569c9be6216Seric 	/* log the arguments */
15703c930db3Seric 	if (pv != NULL && e->e_xfp != NULL)
1571c9be6216Seric 	{
1572c9be6216Seric 		register char **av;
1573c9be6216Seric 
1574c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1575c9be6216Seric 		for (av = pv; *av != NULL; av++)
1576c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1577c9be6216Seric 		fprintf(e->e_xfp, "\n");
1578c9be6216Seric 	}
1579c9be6216Seric 
15805f73204aSeric 	ExitStat = EX_TEMPFAIL;
15815f73204aSeric 	return (EX_TEMPFAIL);
1582c579ef51Seric }
1583c579ef51Seric /*
158425a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
158525a99e2eSeric **
158625a99e2eSeric **	Parameters:
158725a99e2eSeric **		stat -- the status code from the mailer (high byte
158825a99e2eSeric **			only; core dumps must have been taken care of
158925a99e2eSeric **			already).
159081161401Seric **		m -- the mailer info for this mailer.
159181161401Seric **		mci -- the mailer connection info -- can be NULL if the
159281161401Seric **			response is given before the connection is made.
15934dee0003Seric **		ctladdr -- the controlling address for the recipient
15944dee0003Seric **			address(es).
159581161401Seric **		e -- the current envelope.
159625a99e2eSeric **
159725a99e2eSeric **	Returns:
1598db8841e9Seric **		none.
159925a99e2eSeric **
160025a99e2eSeric **	Side Effects:
1601c1f9df2cSeric **		Errors may be incremented.
160225a99e2eSeric **		ExitStat may be set.
160325a99e2eSeric */
160425a99e2eSeric 
16054dee0003Seric giveresponse(stat, m, mci, ctladdr, e)
160625a99e2eSeric 	int stat;
1607588cad61Seric 	register MAILER *m;
160881161401Seric 	register MCI *mci;
16094dee0003Seric 	ADDRESS *ctladdr;
1610198d9be0Seric 	ENVELOPE *e;
161125a99e2eSeric {
16129c16475dSeric 	register const char *statmsg;
161325a99e2eSeric 	extern char *SysExMsg[];
161425a99e2eSeric 	register int i;
1615d4bd8f0eSbostic 	extern int N_SysEx;
1616198d9be0Seric 	char buf[MAXLINE];
161725a99e2eSeric 
161813bbc08cSeric 	/*
161913bbc08cSeric 	**  Compute status message from code.
162013bbc08cSeric 	*/
162113bbc08cSeric 
162225a99e2eSeric 	i = stat - EX__BASE;
1623588cad61Seric 	if (stat == 0)
16246fe8c3bcSeric 	{
1625588cad61Seric 		statmsg = "250 Sent";
1626ce5531bdSeric 		if (e->e_statmsg != NULL)
16276fe8c3bcSeric 		{
1628ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
16296fe8c3bcSeric 			statmsg = buf;
16306fe8c3bcSeric 		}
16316fe8c3bcSeric 	}
1632588cad61Seric 	else if (i < 0 || i > N_SysEx)
1633588cad61Seric 	{
1634588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1635588cad61Seric 		stat = EX_UNAVAILABLE;
1636588cad61Seric 		statmsg = buf;
1637588cad61Seric 	}
1638198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1639198d9be0Seric 	{
16407d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1641d4bd8f0eSbostic #ifdef NAMED_BIND
1642f28da541Smiriam 		if (h_errno == TRY_AGAIN)
16434d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1644f28da541Smiriam 		else
1645d4bd8f0eSbostic #endif
1646f28da541Smiriam 		{
16478557d168Seric 			if (errno != 0)
1648d87e85f3Seric 				statmsg = errstring(errno);
1649d87e85f3Seric 			else
1650d87e85f3Seric 			{
1651d87e85f3Seric #ifdef SMTP
1652d87e85f3Seric 				statmsg = SmtpError;
16536c2c3107Seric #else /* SMTP */
1654d87e85f3Seric 				statmsg = NULL;
16556c2c3107Seric #endif /* SMTP */
1656d87e85f3Seric 			}
1657f28da541Smiriam 		}
1658d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1659d87e85f3Seric 		{
166087c9b3e7Seric 			(void) strcat(buf, ": ");
1661d87e85f3Seric 			(void) strcat(buf, statmsg);
16628557d168Seric 		}
1663198d9be0Seric 		statmsg = buf;
1664198d9be0Seric 	}
1665f170942cSeric #ifdef NAMED_BIND
1666f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1667f170942cSeric 	{
16684d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1669f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1670f170942cSeric 		statmsg = buf;
1671f170942cSeric 	}
1672f170942cSeric #endif
167325a99e2eSeric 	else
1674d87e85f3Seric 	{
167525a99e2eSeric 		statmsg = SysExMsg[i];
16767d55540cSeric 		if (*statmsg++ == ':')
16777d55540cSeric 		{
16787d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16797d55540cSeric 			statmsg = buf;
16807d55540cSeric 		}
1681d87e85f3Seric 	}
1682588cad61Seric 
1683588cad61Seric 	/*
1684588cad61Seric 	**  Print the message as appropriate
1685588cad61Seric 	*/
1686588cad61Seric 
1687198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1688df106f0bSeric 	{
1689df106f0bSeric 		extern char MsgBuf[];
1690df106f0bSeric 
1691e12d03eeSeric 		message(&statmsg[4], errstring(errno));
1692df106f0bSeric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1693df106f0bSeric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1694df106f0bSeric 	}
169525a99e2eSeric 	else
169625a99e2eSeric 	{
1697c1f9df2cSeric 		Errors++;
1698e12d03eeSeric 		usrerr(statmsg, errstring(errno));
169925a99e2eSeric 	}
170025a99e2eSeric 
170125a99e2eSeric 	/*
170225a99e2eSeric 	**  Final cleanup.
170325a99e2eSeric 	**	Log a record of the transaction.  Compute the new
170425a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
170525a99e2eSeric 	**	that.
170625a99e2eSeric 	*/
170725a99e2eSeric 
17082f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
17094dee0003Seric 		logdelivery(m, mci, &statmsg[4], ctladdr, e);
1710eb238f8cSeric 
1711eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1712eb238f8cSeric 		setstat(stat);
17131097d4c9Seric 	if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL))
1714198d9be0Seric 	{
1715198d9be0Seric 		if (e->e_message != NULL)
1716198d9be0Seric 			free(e->e_message);
1717198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1718198d9be0Seric 	}
17198557d168Seric 	errno = 0;
1720d4bd8f0eSbostic #ifdef NAMED_BIND
1721f28da541Smiriam 	h_errno = 0;
1722d4bd8f0eSbostic #endif
1723eb238f8cSeric }
1724eb238f8cSeric /*
1725eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1726eb238f8cSeric **
17272c9b4f99Seric **	Care is taken to avoid logging lines that are too long, because
17282c9b4f99Seric **	some versions of syslog have an unfortunate proclivity for core
17292c9b4f99Seric **	dumping.  This is a hack, to be sure, that is at best empirical.
17302c9b4f99Seric **
1731eb238f8cSeric **	Parameters:
173281161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
173381161401Seric **		mci -- the mailer connection info -- can be NULL if the
173481161401Seric **			log is occuring when no connection is active.
173581161401Seric **		stat -- the message to print for the status.
17364dee0003Seric **		ctladdr -- the controlling address for the to list.
173781161401Seric **		e -- the current envelope.
1738eb238f8cSeric **
1739eb238f8cSeric **	Returns:
1740eb238f8cSeric **		none
1741eb238f8cSeric **
1742eb238f8cSeric **	Side Effects:
1743eb238f8cSeric **		none
1744eb238f8cSeric */
1745eb238f8cSeric 
17464dee0003Seric logdelivery(m, mci, stat, ctladdr, e)
174781161401Seric 	MAILER *m;
174881161401Seric 	register MCI *mci;
1749eb238f8cSeric 	char *stat;
17504dee0003Seric 	ADDRESS *ctladdr;
1751b31e7f2bSeric 	register ENVELOPE *e;
17525cf56be3Seric {
1753eb238f8cSeric # ifdef LOG
17544dee0003Seric 	register char *bp;
17552c9b4f99Seric 	register char *p;
17562c9b4f99Seric 	int l;
1757d6acf3eeSeric 	char buf[512];
17589507d1f9Seric 
17593a100e8bSeric #  if (SYSLOG_BUFSIZE) >= 256
17604dee0003Seric 	bp = buf;
17614dee0003Seric 	if (ctladdr != NULL)
17624dee0003Seric 	{
17634dee0003Seric 		strcpy(bp, ", ctladdr=");
1764f62f08c7Seric 		strcat(bp, shortenstring(ctladdr->q_paddr, 83));
17654dee0003Seric 		bp += strlen(bp);
17664dee0003Seric 		if (bitset(QGOODUID, ctladdr->q_flags))
17674dee0003Seric 		{
17684dee0003Seric 			(void) sprintf(bp, " (%d/%d)",
17694dee0003Seric 					ctladdr->q_uid, ctladdr->q_gid);
17704dee0003Seric 			bp += strlen(bp);
17714dee0003Seric 		}
17724dee0003Seric 	}
17734dee0003Seric 
17744dee0003Seric 	(void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
17754dee0003Seric 	bp += strlen(bp);
177681161401Seric 
177748ed5d33Seric 	if (m != NULL)
177871ff6caaSeric 	{
17794dee0003Seric 		(void) strcpy(bp, ", mailer=");
17804dee0003Seric 		(void) strcat(bp, m->m_name);
17814dee0003Seric 		bp += strlen(bp);
178271ff6caaSeric 	}
178348ed5d33Seric 
178448ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
178571ff6caaSeric 	{
178671ff6caaSeric # ifdef DAEMON
1787e2f2f828Seric 		extern SOCKADDR CurHostAddr;
178848ed5d33Seric # endif
178971ff6caaSeric 
17904dee0003Seric 		(void) strcpy(bp, ", relay=");
17914dee0003Seric 		(void) strcat(bp, mci->mci_host);
179248ed5d33Seric 
179348ed5d33Seric # ifdef DAEMON
17944dee0003Seric 		(void) strcat(bp, " (");
17954dee0003Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
17964dee0003Seric 		(void) strcat(bp, ")");
179771ff6caaSeric # endif
179871ff6caaSeric 	}
17999507d1f9Seric 	else
180048ed5d33Seric 	{
180148ed5d33Seric 		char *p = macvalue('h', e);
18029507d1f9Seric 
180348ed5d33Seric 		if (p != NULL && p[0] != '\0')
180448ed5d33Seric 		{
18054dee0003Seric 			(void) strcpy(bp, ", relay=");
18064dee0003Seric 			(void) strcat(bp, p);
180748ed5d33Seric 		}
180848ed5d33Seric 	}
1809cb082c5bSeric 	bp += strlen(bp);
1810d6acf3eeSeric 
18113a100e8bSeric #define STATLEN		(((SYSLOG_BUFSIZE) - 100) / 4)
18123a100e8bSeric #if (STATLEN) < 63
18133a100e8bSeric # undef STATLEN
18143a100e8bSeric # define STATLEN	63
18153a100e8bSeric #endif
18163a100e8bSeric #if (STATLEN) > 203
18173a100e8bSeric # undef STATLEN
18183a100e8bSeric # define STATLEN	203
18193a100e8bSeric #endif
18203a100e8bSeric 
18213a100e8bSeric 	if ((bp - buf) > (sizeof buf - ((STATLEN) + 20)))
18222c9b4f99Seric 	{
18232c9b4f99Seric 		/* desperation move -- truncate data */
18243a100e8bSeric 		bp = buf + sizeof buf - ((STATLEN) + 17);
18252c9b4f99Seric 		strcpy(bp, "...");
18262c9b4f99Seric 		bp += 3;
18272c9b4f99Seric 	}
18282c9b4f99Seric 
18292c9b4f99Seric 	(void) strcpy(bp, ", stat=");
18302c9b4f99Seric 	bp += strlen(bp);
18313a100e8bSeric 
18323a100e8bSeric 	(void) strcpy(bp, shortenstring(stat, (STATLEN)));
18332c9b4f99Seric 
18342c9b4f99Seric 	l = SYSLOG_BUFSIZE - 100 - strlen(buf);
18352c9b4f99Seric 	p = e->e_to;
18362c9b4f99Seric 	while (strlen(p) >= l)
18372c9b4f99Seric 	{
18382c9b4f99Seric 		register char *q = strchr(p + l, ',');
18392c9b4f99Seric 
18402a1b6b73Seric 		if (q == NULL)
18412c9b4f99Seric 			break;
18422c9b4f99Seric 		syslog(LOG_INFO, "%s: to=%.*s [more]%s",
18432c9b4f99Seric 			e->e_id, ++q - p, p, buf);
18442c9b4f99Seric 		p = q;
18452c9b4f99Seric 	}
18462c9b4f99Seric 	syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf);
18473a100e8bSeric 
18483a100e8bSeric #  else		/* we have a very short log buffer size */
18493a100e8bSeric 
18503a100e8bSeric 	l = SYSLOG_BUFSIZE - 40;
18513a100e8bSeric 	p = e->e_to;
18523a100e8bSeric 	while (strlen(p) >= l)
18533a100e8bSeric 	{
18543a100e8bSeric 		register char *q = strchr(p + l, ',');
18553a100e8bSeric 
18563a100e8bSeric 		if (q == NULL)
18573a100e8bSeric 			break;
18583a100e8bSeric 		syslog(LOG_INFO, "%s: to=%.*s [more]",
18593a100e8bSeric 			e->e_id, ++q - p, p);
18603a100e8bSeric 		p = q;
18613a100e8bSeric 	}
18623a100e8bSeric 	syslog(LOG_INFO, "%s: to=%s", e->e_id, p);
18633a100e8bSeric 
18643a100e8bSeric 	if (ctladdr != NULL)
18653a100e8bSeric 	{
18663a100e8bSeric 		bp = buf;
18673a100e8bSeric 		strcpy(buf, "ctladdr=");
18683a100e8bSeric 		bp += strlen(buf);
18693a100e8bSeric 		strcpy(bp, shortenstring(ctladdr->q_paddr, 83));
18703a100e8bSeric 		bp += strlen(buf);
18713a100e8bSeric 		if (bitset(QGOODUID, ctladdr->q_flags))
18723a100e8bSeric 		{
18733a100e8bSeric 			(void) sprintf(bp, " (%d/%d)",
18743a100e8bSeric 					ctladdr->q_uid, ctladdr->q_gid);
18753a100e8bSeric 			bp += strlen(bp);
18763a100e8bSeric 		}
18773a100e8bSeric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
18783a100e8bSeric 	}
18793a100e8bSeric 	syslog(LOG_INFO, "%s: delay=%s",
18803a100e8bSeric 		e->e_id, pintvl(curtime() - e->e_ctime, TRUE));
18813a100e8bSeric 
18823a100e8bSeric 	if (m != NULL)
18833a100e8bSeric 		syslog(LOG_INFO, "%s: mailer=%s", e->e_id, m->m_name);
18843a100e8bSeric 
18853a100e8bSeric 	if (mci != NULL && mci->mci_host != NULL)
18863a100e8bSeric 	{
18873a100e8bSeric # ifdef DAEMON
18883a100e8bSeric 		extern SOCKADDR CurHostAddr;
18893a100e8bSeric # endif
18903a100e8bSeric 
18913a100e8bSeric 		(void) strcpy(buf, mci->mci_host);
18923a100e8bSeric 
18933a100e8bSeric # ifdef DAEMON
18943a100e8bSeric 		(void) strcat(buf, " (");
18953a100e8bSeric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
18963a100e8bSeric 		(void) strcat(buf, ")");
18973a100e8bSeric # endif
18983a100e8bSeric 		syslog(LOG_INFO, "%s: relay=%s", e->e_id, buf);
18993a100e8bSeric 	}
19003a100e8bSeric 	else
19013a100e8bSeric 	{
19023a100e8bSeric 		char *p = macvalue('h', e);
19033a100e8bSeric 
19043a100e8bSeric 		if (p != NULL && p[0] != '\0')
19053a100e8bSeric 			syslog(LOG_INFO, "%s: relay=%s", e->e_id, p);
19063a100e8bSeric 	}
19073a100e8bSeric 
19083a100e8bSeric 	syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63));
19093a100e8bSeric #  endif /* short log buffer */
19106c2c3107Seric # endif /* LOG */
191125a99e2eSeric }
191225a99e2eSeric /*
191351552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
191425a99e2eSeric **
191551552439Seric **	This can be made an arbitrary message separator by changing $l
191651552439Seric **
19179b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
19189b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
19199b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
19209b6c17a6Seric **	this kind of antique garbage????
192125a99e2eSeric **
192225a99e2eSeric **	Parameters:
192351552439Seric **		fp -- the file to output to.
192451552439Seric **		m -- the mailer describing this entry.
192525a99e2eSeric **
192625a99e2eSeric **	Returns:
192751552439Seric **		none
192825a99e2eSeric **
192925a99e2eSeric **	Side Effects:
193051552439Seric **		outputs some text to fp.
193125a99e2eSeric */
193225a99e2eSeric 
1933b31e7f2bSeric putfromline(fp, m, e)
193451552439Seric 	register FILE *fp;
193551552439Seric 	register MAILER *m;
1936b31e7f2bSeric 	ENVELOPE *e;
193725a99e2eSeric {
19382bc47524Seric 	char *template = "\201l\n";
193951552439Seric 	char buf[MAXLINE];
194025a99e2eSeric 
194157fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
194251552439Seric 		return;
194313bbc08cSeric 
19442c7e1b8dSeric # ifdef UGLYUUCP
194557fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
194674b6e67bSeric 	{
1947ea09d6edSeric 		char *bang;
1948ea09d6edSeric 		char xbuf[MAXLINE];
194974b6e67bSeric 
1950ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
19516c2c3107Seric 		bang = strchr(buf, '!');
195274b6e67bSeric 		if (bang == NULL)
195334fcca25Seric 		{
195434fcca25Seric 			errno = 0;
195534fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
195634fcca25Seric 		}
195774b6e67bSeric 		else
1958588cad61Seric 		{
1959ea09d6edSeric 			*bang++ = '\0';
19602bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1961ea09d6edSeric 			template = xbuf;
196274b6e67bSeric 		}
1963588cad61Seric 	}
19646c2c3107Seric # endif /* UGLYUUCP */
1965b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
196677b52738Seric 	putline(buf, fp, m);
1967bc6e2962Seric }
1968bc6e2962Seric /*
196951552439Seric **  PUTBODY -- put the body of a message.
197051552439Seric **
197151552439Seric **	Parameters:
197251552439Seric **		fp -- file to output onto.
197377b52738Seric **		m -- a mailer descriptor to control output format.
19749a6a5f55Seric **		e -- the envelope to put out.
197503c02fdeSeric **		separator -- if non-NULL, a message separator that must
197603c02fdeSeric **			not be permitted in the resulting message.
197751552439Seric **
197851552439Seric **	Returns:
197951552439Seric **		none.
198051552439Seric **
198151552439Seric **	Side Effects:
198251552439Seric **		The message is written onto fp.
198351552439Seric */
198451552439Seric 
198503c02fdeSeric putbody(fp, m, e, separator)
198651552439Seric 	FILE *fp;
1987588cad61Seric 	MAILER *m;
19889a6a5f55Seric 	register ENVELOPE *e;
198903c02fdeSeric 	char *separator;
199051552439Seric {
199177b52738Seric 	char buf[MAXLINE];
199251552439Seric 
199351552439Seric 	/*
199451552439Seric 	**  Output the body of the message
199551552439Seric 	*/
199651552439Seric 
19979a6a5f55Seric 	if (e->e_dfp == NULL)
199851552439Seric 	{
19999a6a5f55Seric 		if (e->e_df != NULL)
20009a6a5f55Seric 		{
20019a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
20029a6a5f55Seric 			if (e->e_dfp == NULL)
20038f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
2004e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
20059a6a5f55Seric 		}
20069a6a5f55Seric 		else
200777b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
20089a6a5f55Seric 	}
20099a6a5f55Seric 	if (e->e_dfp != NULL)
20109a6a5f55Seric 	{
20119a6a5f55Seric 		rewind(e->e_dfp);
201277b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
201324fc8aeeSeric 		{
201424fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
2015d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
20163462ad9eSeric 				(void) putc('>', fp);
201703c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
201803c02fdeSeric 			{
201903c02fdeSeric 				/* possible separator */
202003c02fdeSeric 				int sl = strlen(separator);
202103c02fdeSeric 
202203c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
202303c02fdeSeric 					(void) putc(' ', fp);
202403c02fdeSeric 			}
202577b52738Seric 			putline(buf, fp, m);
202624fc8aeeSeric 		}
202751552439Seric 
20289a6a5f55Seric 		if (ferror(e->e_dfp))
202951552439Seric 		{
2030df106f0bSeric 			syserr("putbody: %s: read error", e->e_df);
203151552439Seric 			ExitStat = EX_IOERR;
203251552439Seric 		}
203351552439Seric 	}
203451552439Seric 
20350890ba1fSeric 	/* some mailers want extra blank line at end of message */
20360890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
20370890ba1fSeric 		putline("", fp, m);
20380890ba1fSeric 
203951552439Seric 	(void) fflush(fp);
204051552439Seric 	if (ferror(fp) && errno != EPIPE)
204151552439Seric 	{
204251552439Seric 		syserr("putbody: write error");
204351552439Seric 		ExitStat = EX_IOERR;
204451552439Seric 	}
204551552439Seric 	errno = 0;
204625a99e2eSeric }
204725a99e2eSeric /*
204825a99e2eSeric **  MAILFILE -- Send a message to a file.
204925a99e2eSeric **
2050f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
2051f129ec7dSeric **	bits, sendmail will try to become the owner of that file
2052f129ec7dSeric **	rather than the real user.  Obviously, this only works if
2053f129ec7dSeric **	sendmail runs as root.
2054f129ec7dSeric **
2055588cad61Seric **	This could be done as a subordinate mailer, except that it
2056588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
2057588cad61Seric **	view this as being sufficiently important as to include it
2058588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
2059588cad61Seric **	to create another process plus some pipes to save the message.
2060588cad61Seric **
206125a99e2eSeric **	Parameters:
206225a99e2eSeric **		filename -- the name of the file to send to.
20636259796dSeric **		ctladdr -- the controlling address header -- includes
20646259796dSeric **			the userid/groupid to be when sending.
206525a99e2eSeric **
206625a99e2eSeric **	Returns:
206725a99e2eSeric **		The exit code associated with the operation.
206825a99e2eSeric **
206925a99e2eSeric **	Side Effects:
207025a99e2eSeric **		none.
207125a99e2eSeric */
207225a99e2eSeric 
2073b31e7f2bSeric mailfile(filename, ctladdr, e)
207425a99e2eSeric 	char *filename;
20756259796dSeric 	ADDRESS *ctladdr;
2076b31e7f2bSeric 	register ENVELOPE *e;
207725a99e2eSeric {
207825a99e2eSeric 	register FILE *f;
207932d19d43Seric 	register int pid;
208015d084d5Seric 	int mode;
208125a99e2eSeric 
2082671745f3Seric 	if (tTd(11, 1))
2083671745f3Seric 	{
2084671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
2085671745f3Seric 		printaddr(ctladdr, FALSE);
2086671745f3Seric 	}
2087671745f3Seric 
2088f170942cSeric 	if (e->e_xfp != NULL)
2089f170942cSeric 		fflush(e->e_xfp);
2090f170942cSeric 
209132d19d43Seric 	/*
209232d19d43Seric 	**  Fork so we can change permissions here.
209332d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
209432d19d43Seric 	**	the complications of calling subroutines, etc.
209532d19d43Seric 	*/
209632d19d43Seric 
209732d19d43Seric 	DOFORK(fork);
209832d19d43Seric 
209932d19d43Seric 	if (pid < 0)
210032d19d43Seric 		return (EX_OSERR);
210132d19d43Seric 	else if (pid == 0)
210232d19d43Seric 	{
210332d19d43Seric 		/* child -- actually write to file */
2104f129ec7dSeric 		struct stat stb;
2105f129ec7dSeric 
21062b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
21072b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
21082b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
21093462ad9eSeric 		(void) umask(OldUmask);
211095f16dc0Seric 
2111f129ec7dSeric 		if (stat(filename, &stb) < 0)
21123a98e7eaSeric 			stb.st_mode = FileMode;
211315d084d5Seric 		mode = stb.st_mode;
211495f16dc0Seric 
211595f16dc0Seric 		/* limit the errors to those actually caused in the child */
211695f16dc0Seric 		errno = 0;
211795f16dc0Seric 		ExitStat = EX_OK;
211895f16dc0Seric 
2119f129ec7dSeric 		if (bitset(0111, stb.st_mode))
2120f129ec7dSeric 			exit(EX_CANTCREAT);
212119428781Seric 		if (ctladdr != NULL)
212215d084d5Seric 		{
212315d084d5Seric 			/* ignore setuid and setgid bits */
212415d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
212515d084d5Seric 		}
212615d084d5Seric 
21278f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
21288f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
21298f9146b0Srick 		{
21308f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
213195f16dc0Seric 			if (e->e_dfp == NULL)
213295f16dc0Seric 			{
21338f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
2134e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
21358f9146b0Srick 			}
21368f9146b0Srick 		}
21378f9146b0Srick 
213815d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
2139e36b99e2Seric 		{
214019428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
214195f16dc0Seric 			{
2142898a126bSbostic 				(void) initgroups(DefUser, DefGid);
214395f16dc0Seric 			}
214495f16dc0Seric 			else
214595f16dc0Seric 			{
2146898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
2147898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
2148898a126bSbostic 					ctladdr->q_gid);
2149898a126bSbostic 			}
2150e36b99e2Seric 		}
215115d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
2152e36b99e2Seric 		{
215319428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
2154e36b99e2Seric 				(void) setuid(DefUid);
2155e36b99e2Seric 			else
21566259796dSeric 				(void) setuid(ctladdr->q_uid);
2157e36b99e2Seric 		}
215895f16dc0Seric 		FileName = filename;
215995f16dc0Seric 		LineNumber = 0;
21603a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
216125a99e2eSeric 		if (f == NULL)
216295f16dc0Seric 		{
2163b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
216432d19d43Seric 			exit(EX_CANTCREAT);
216595f16dc0Seric 		}
216625a99e2eSeric 
21670331ce05Seric 		putfromline(f, FileMailer, e);
21680331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
21690331ce05Seric 		putline("\n", f, FileMailer);
217003c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
21710331ce05Seric 		putline("\n", f, FileMailer);
217295f16dc0Seric 		if (ferror(f))
217395f16dc0Seric 		{
2174b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
217595f16dc0Seric 			setstat(EX_IOERR);
217695f16dc0Seric 		}
2177ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
217832d19d43Seric 		(void) fflush(stdout);
2179e36b99e2Seric 
218027628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
2181c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
218295f16dc0Seric 		exit(ExitStat);
218313bbc08cSeric 		/*NOTREACHED*/
218432d19d43Seric 	}
218532d19d43Seric 	else
218632d19d43Seric 	{
218732d19d43Seric 		/* parent -- wait for exit status */
2188588cad61Seric 		int st;
218932d19d43Seric 
2190588cad61Seric 		st = waitfor(pid);
2191bf9bc890Seric 		if (WIFEXITED(st))
2192bf9bc890Seric 			return (WEXITSTATUS(st));
2193588cad61Seric 		else
2194b6a0de9dSeric 		{
2195b6a0de9dSeric 			syserr("child died on signal %d", st);
2196bf9bc890Seric 			return (EX_UNAVAILABLE);
2197b6a0de9dSeric 		}
21988f9146b0Srick 		/*NOTREACHED*/
219932d19d43Seric 	}
220025a99e2eSeric }
2201ea4dc939Seric /*
2202e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2203e103b48fSeric **
2204e103b48fSeric **	The signature describes how we are going to send this -- it
2205e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2206e103b48fSeric **	an ordered list of MX hosts.
2207e103b48fSeric **
2208e103b48fSeric **	Parameters:
2209e103b48fSeric **		m -- the mailer describing this host.
2210e103b48fSeric **		host -- the host name.
2211e103b48fSeric **		e -- the current envelope.
2212e103b48fSeric **
2213e103b48fSeric **	Returns:
2214e103b48fSeric **		The signature for this host.
2215e103b48fSeric **
2216e103b48fSeric **	Side Effects:
2217e103b48fSeric **		Can tweak the symbol table.
2218e103b48fSeric */
2219e103b48fSeric 
2220e103b48fSeric char *
2221e103b48fSeric hostsignature(m, host, e)
2222e103b48fSeric 	register MAILER *m;
2223e103b48fSeric 	char *host;
2224e103b48fSeric 	ENVELOPE *e;
2225e103b48fSeric {
2226e103b48fSeric 	register char *p;
2227e103b48fSeric 	register STAB *s;
2228e103b48fSeric 	int i;
2229e103b48fSeric 	int len;
2230e103b48fSeric #ifdef NAMED_BIND
2231e103b48fSeric 	int nmx;
2232e103b48fSeric 	auto int rcode;
2233bafdc4e5Seric 	char *hp;
2234bafdc4e5Seric 	char *endp;
2235516782b4Seric 	int oldoptions;
2236e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2237e103b48fSeric #endif
2238e103b48fSeric 
2239e103b48fSeric 	/*
2240e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2241e103b48fSeric 	*/
2242e103b48fSeric 
2243e103b48fSeric 	p = m->m_mailer;
2244e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2245e103b48fSeric 	{
2246e103b48fSeric 		/* just an ordinary mailer */
2247e103b48fSeric 		return host;
2248e103b48fSeric 	}
2249e103b48fSeric 
2250e103b48fSeric 	/*
2251e103b48fSeric 	**  Look it up in the symbol table.
2252e103b48fSeric 	*/
2253e103b48fSeric 
2254e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2255e103b48fSeric 	if (s->s_hostsig != NULL)
2256e103b48fSeric 		return s->s_hostsig;
2257e103b48fSeric 
2258e103b48fSeric 	/*
2259e103b48fSeric 	**  Not already there -- create a signature.
2260e103b48fSeric 	*/
2261e103b48fSeric 
2262e103b48fSeric #ifdef NAMED_BIND
2263516782b4Seric 	if (ConfigLevel < 2)
2264516782b4Seric 	{
2265516782b4Seric 		oldoptions = _res.options;
2266516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2267516782b4Seric 	}
2268516782b4Seric 
2269bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2270bafdc4e5Seric 	{
2271bafdc4e5Seric 		endp = strchr(hp, ':');
2272bafdc4e5Seric 		if (endp != NULL)
2273bafdc4e5Seric 			*endp = '\0';
2274bafdc4e5Seric 
22757bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
22767d55540cSeric 
2277e103b48fSeric 		if (nmx <= 0)
2278e103b48fSeric 		{
2279e103b48fSeric 			register MCI *mci;
2280e103b48fSeric 
2281e103b48fSeric 			/* update the connection info for this host */
2282bafdc4e5Seric 			mci = mci_get(hp, m);
2283e103b48fSeric 			mci->mci_exitstat = rcode;
2284e103b48fSeric 			mci->mci_errno = errno;
2285f170942cSeric #ifdef NAMED_BIND
2286f170942cSeric 			mci->mci_herrno = h_errno;
2287f170942cSeric #endif
2288e103b48fSeric 
2289e103b48fSeric 			/* and return the original host name as the signature */
2290bafdc4e5Seric 			nmx = 1;
2291bafdc4e5Seric 			mxhosts[0] = hp;
2292e103b48fSeric 		}
2293e103b48fSeric 
2294e103b48fSeric 		len = 0;
2295e103b48fSeric 		for (i = 0; i < nmx; i++)
2296e103b48fSeric 		{
2297e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2298e103b48fSeric 		}
2299bafdc4e5Seric 		if (s->s_hostsig != NULL)
2300bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2301bafdc4e5Seric 		p = xalloc(len);
2302bafdc4e5Seric 		if (s->s_hostsig != NULL)
2303bafdc4e5Seric 		{
2304bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2305bafdc4e5Seric 			free(s->s_hostsig);
2306bafdc4e5Seric 			s->s_hostsig = p;
2307bafdc4e5Seric 			p += strlen(p);
2308bafdc4e5Seric 			*p++ = ':';
2309bafdc4e5Seric 		}
2310bafdc4e5Seric 		else
2311bafdc4e5Seric 			s->s_hostsig = p;
2312e103b48fSeric 		for (i = 0; i < nmx; i++)
2313e103b48fSeric 		{
2314e103b48fSeric 			if (i != 0)
2315e103b48fSeric 				*p++ = ':';
2316e103b48fSeric 			strcpy(p, mxhosts[i]);
2317e103b48fSeric 			p += strlen(p);
2318e103b48fSeric 		}
2319bafdc4e5Seric 		if (endp != NULL)
2320bafdc4e5Seric 			*endp++ = ':';
2321bafdc4e5Seric 	}
2322e103b48fSeric 	makelower(s->s_hostsig);
2323516782b4Seric 	if (ConfigLevel < 2)
2324516782b4Seric 		_res.options = oldoptions;
2325e103b48fSeric #else
2326e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2327e103b48fSeric 	s->s_hostsig = host;
2328e103b48fSeric #endif
2329e103b48fSeric 	if (tTd(17, 1))
2330e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2331e103b48fSeric 	return s->s_hostsig;
2332e103b48fSeric }
2333