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*b269aa8eSeric static char sccsid[] = "@(#)deliver.c	8.64 (Berkeley) 01/22/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 
618d19a23dSeric 	if (bitset(EF_FATALERRS, e->e_flags) &&
628d19a23dSeric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
63896b16f6Seric 	{
64896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
65896b16f6Seric 		return;
66896b16f6Seric 	}
67896b16f6Seric 
689c9e68d9Seric 	/* determine actual delivery mode */
69fbe2f963Seric 	CurrentLA = getla();
709c9e68d9Seric 	if (mode == SM_DEFAULT)
719c9e68d9Seric 	{
729c9e68d9Seric 		mode = e->e_sendmode;
739c9e68d9Seric 		if (mode != SM_VERIFY &&
749c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
759c9e68d9Seric 			mode = SM_QUEUE;
766103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
779c9e68d9Seric 	}
786103d9b4Seric 	else
796103d9b4Seric 		announcequeueup = FALSE;
809c9e68d9Seric 
819c9e68d9Seric 	if (tTd(13, 1))
829c9e68d9Seric 	{
830e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
840e484fe5Seric 			mode, e->e_id);
859c9e68d9Seric 		printaddr(&e->e_from, FALSE);
869c9e68d9Seric 		printf("sendqueue:\n");
879c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
889c9e68d9Seric 	}
899c9e68d9Seric 
909c9e68d9Seric 	/*
919c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
929c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
939c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
949c9e68d9Seric 	*/
959c9e68d9Seric 
969c9e68d9Seric 	CurEnv = e;
979c9e68d9Seric 
989c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
999c9e68d9Seric 	{
1009c9e68d9Seric 		errno = 0;
1014bb8b0fdSeric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
1029c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
1034bb8b0fdSeric 			RealHostName, e->e_sendqueue->q_paddr);
1049c9e68d9Seric 		return;
1059c9e68d9Seric 	}
1069c9e68d9Seric 
107b8004690Seric 	/*
108b8004690Seric 	**  Do sender deletion.
109b8004690Seric 	**
110b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
111b8004690Seric 	**	This can happen if the name server is hosed when you
112b8004690Seric 	**	are trying to send mail.  The result is that the sender
113b8004690Seric 	**	is instantiated in the queue as a recipient.
114b8004690Seric 	*/
115b8004690Seric 
116e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
117e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1189c9e68d9Seric 	{
1199c9e68d9Seric 		if (tTd(13, 5))
1209c9e68d9Seric 		{
1219c9e68d9Seric 			printf("sendall: QDONTSEND ");
1229c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1239c9e68d9Seric 		}
1249c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1259c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1269c9e68d9Seric 	}
1279c9e68d9Seric 
128ce5531bdSeric 	/*
129ce5531bdSeric 	**  Handle alias owners.
130ce5531bdSeric 	**
131ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
132ce5531bdSeric 	**	We discard owners that are the same as the return path.
133ce5531bdSeric 	*/
134ce5531bdSeric 
135ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
136ce5531bdSeric 	{
137ce5531bdSeric 		register struct address *a;
138ce5531bdSeric 
139ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
140ce5531bdSeric 			continue;
141ce5531bdSeric 		if (a != NULL)
142ce5531bdSeric 			q->q_owner = a->q_owner;
143ce5531bdSeric 
144ce5531bdSeric 		if (q->q_owner != NULL &&
145ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
146ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
147ce5531bdSeric 			q->q_owner = NULL;
148ce5531bdSeric 	}
149ce5531bdSeric 
150ce5531bdSeric 	owner = "";
151ce5531bdSeric 	otherowners = 1;
152ce5531bdSeric 	while (owner != NULL && otherowners > 0)
153ce5531bdSeric 	{
154ce5531bdSeric 		owner = NULL;
155ce5531bdSeric 		otherowners = 0;
156ce5531bdSeric 
157ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
158ce5531bdSeric 		{
159ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
160ce5531bdSeric 				continue;
161ce5531bdSeric 
162ce5531bdSeric 			if (q->q_owner != NULL)
163ce5531bdSeric 			{
164ce5531bdSeric 				if (owner == NULL)
165ce5531bdSeric 					owner = q->q_owner;
166ce5531bdSeric 				else if (owner != q->q_owner)
167ce5531bdSeric 				{
168ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
169ce5531bdSeric 					{
170ce5531bdSeric 						/* make future comparisons cheap */
171ce5531bdSeric 						q->q_owner = owner;
172ce5531bdSeric 					}
173ce5531bdSeric 					else
174ce5531bdSeric 					{
175ce5531bdSeric 						otherowners++;
176ce5531bdSeric 					}
177ce5531bdSeric 					owner = q->q_owner;
178ce5531bdSeric 				}
179ce5531bdSeric 			}
180ce5531bdSeric 			else
181ce5531bdSeric 			{
182ce5531bdSeric 				otherowners++;
183ce5531bdSeric 			}
184ce5531bdSeric 		}
185ce5531bdSeric 
186ce5531bdSeric 		if (owner != NULL && otherowners > 0)
187ce5531bdSeric 		{
188ce5531bdSeric 			extern HDR *copyheader();
189ce5531bdSeric 			extern ADDRESS *copyqueue();
190ce5531bdSeric 
191ce5531bdSeric 			/*
192ce5531bdSeric 			**  Split this envelope into two.
193ce5531bdSeric 			*/
194ce5531bdSeric 
195ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
196ce5531bdSeric 			*ee = *e;
197ce5531bdSeric 			ee->e_id = NULL;
198ce5531bdSeric 			(void) queuename(ee, '\0');
199ce5531bdSeric 
200ce5531bdSeric 			if (tTd(13, 1))
201ce5531bdSeric 				printf("sendall: split %s into %s\n",
202ce5531bdSeric 					e->e_id, ee->e_id);
203ce5531bdSeric 
204ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
205ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
206ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
207ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
208ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
209ce5531bdSeric 			if (tTd(13, 5))
210ce5531bdSeric 			{
211ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
212ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
213ce5531bdSeric 			}
214ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
215ce5531bdSeric 			ee->e_dfp = NULL;
216ce5531bdSeric 			ee->e_xfp = NULL;
217ce5531bdSeric 			ee->e_df = NULL;
218ce5531bdSeric 			ee->e_errormode = EM_MAIL;
219ce5531bdSeric 			ee->e_sibling = splitenv;
220ce5531bdSeric 			splitenv = ee;
221ce5531bdSeric 
222ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
223ce5531bdSeric 				if (q->q_owner == owner)
224ce5531bdSeric 					q->q_flags |= QDONTSEND;
225ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
226ce5531bdSeric 				if (q->q_owner != owner)
227ce5531bdSeric 					q->q_flags |= QDONTSEND;
228ce5531bdSeric 
229ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
230ce5531bdSeric 			{
231ce5531bdSeric 				ee->e_dfp = NULL;
232da662164Seric 				ee->e_df = queuename(ee, 'd');
233da662164Seric 				ee->e_df = newstr(ee->e_df);
234ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
235ce5531bdSeric 				{
236ce5531bdSeric 					syserr("sendall: link(%s, %s)",
237ce5531bdSeric 						e->e_df, ee->e_df);
238ce5531bdSeric 				}
239ce5531bdSeric 			}
240ce5531bdSeric 
241ce5531bdSeric 			if (mode != SM_VERIFY)
242ce5531bdSeric 				openxscript(ee);
243ce5531bdSeric #ifdef LOG
244ce5531bdSeric 			if (LogLevel > 4)
245ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
246ce5531bdSeric 					ee->e_id, e->e_id);
247ce5531bdSeric #endif
248ce5531bdSeric 		}
249ce5531bdSeric 	}
250ce5531bdSeric 
251ce5531bdSeric 	if (owner != NULL)
252ce5531bdSeric 	{
253ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
254ce5531bdSeric 		if (tTd(13, 5))
255ce5531bdSeric 		{
256ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
257ce5531bdSeric 			printaddr(&e->e_from, FALSE);
258ce5531bdSeric 		}
259ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
260ce5531bdSeric 		e->e_errormode = EM_MAIL;
261ce5531bdSeric 	}
262ce5531bdSeric 
263c1ac89b1Seric # ifdef QUEUE
264c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
265c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
266c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
267c1ac89b1Seric 	{
268c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2696103d9b4Seric 		queueup(e, TRUE, announcequeueup);
270ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2716103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
272c1ac89b1Seric 	}
273c1ac89b1Seric #endif /* QUEUE */
274c1ac89b1Seric 
275ce5531bdSeric 	if (splitenv != NULL)
276ce5531bdSeric 	{
277ce5531bdSeric 		if (tTd(13, 1))
278ce5531bdSeric 		{
279ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
280ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
281ce5531bdSeric 		}
282ce5531bdSeric 
283ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
284ce5531bdSeric 		{
285ce5531bdSeric 			CurEnv = ee;
286ce5531bdSeric 			sendenvelope(ee, mode);
287ce5531bdSeric 		}
288ce5531bdSeric 
289ce5531bdSeric 		CurEnv = e;
290ce5531bdSeric 	}
291ce5531bdSeric 	sendenvelope(e, mode);
292ce5531bdSeric 
293ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
294ce5531bdSeric 		dropenvelope(splitenv);
295ce5531bdSeric }
296ce5531bdSeric 
297ce5531bdSeric sendenvelope(e, mode)
298ce5531bdSeric 	register ENVELOPE *e;
299ce5531bdSeric 	char mode;
300ce5531bdSeric {
301ce5531bdSeric 	bool oldverbose;
302ce5531bdSeric 	int pid;
303ce5531bdSeric 	register ADDRESS *q;
3046b2765c6Seric 	char *qf;
3056b2765c6Seric 	char *id;
306ce5531bdSeric 
3077880f3e4Seric 	/*
3087880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3097880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3107880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3117880f3e4Seric 	**  addresses to be sent.
3127880f3e4Seric 	*/
3137880f3e4Seric 
3148d19a23dSeric 	if (bitset(EF_FATALERRS, e->e_flags) &&
3158d19a23dSeric 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
3167880f3e4Seric 	{
3177880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3187880f3e4Seric 		return;
3197880f3e4Seric 	}
3207880f3e4Seric 
321ce5531bdSeric 	oldverbose = Verbose;
322c1ac89b1Seric 	switch (mode)
323c1ac89b1Seric 	{
324c1ac89b1Seric 	  case SM_VERIFY:
325c1ac89b1Seric 		Verbose = TRUE;
326c1ac89b1Seric 		break;
327c1ac89b1Seric 
328c1ac89b1Seric 	  case SM_QUEUE:
329c1ac89b1Seric   queueonly:
330c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
331c1ac89b1Seric 		return;
332c1ac89b1Seric 
333c1ac89b1Seric 	  case SM_FORK:
334c1ac89b1Seric 		if (e->e_xfp != NULL)
335c1ac89b1Seric 			(void) fflush(e->e_xfp);
336c1ac89b1Seric 
337*b269aa8eSeric # if !HASFLOCK
338c1ac89b1Seric 		/*
3396b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3406b2765c6Seric 		**  the lock is owned by a process, not by an open file
3416b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3426b2765c6Seric 		**  then restart from scratch in the child.
343c1ac89b1Seric 		*/
344c1ac89b1Seric 
3456b2765c6Seric 		/* save id for future use */
3466b2765c6Seric 		id = e->e_id;
3476b2765c6Seric 
3486b2765c6Seric 		/* now drop the envelope in the parent */
3496b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
3506b2765c6Seric 		dropenvelope(e);
3516b2765c6Seric 
3526b2765c6Seric 		/* and reacquire in the child */
3536b2765c6Seric 		(void) dowork(id, TRUE, FALSE, e);
3546b2765c6Seric 
3556b2765c6Seric 		return;
3566b2765c6Seric 
3576b2765c6Seric # else /* HASFLOCK */
358c1ac89b1Seric 
359c1ac89b1Seric 		pid = fork();
360c1ac89b1Seric 		if (pid < 0)
361c1ac89b1Seric 		{
362c1ac89b1Seric 			goto queueonly;
363c1ac89b1Seric 		}
364c1ac89b1Seric 		else if (pid > 0)
365c1ac89b1Seric 		{
3660e484fe5Seric 			/* be sure we leave the temp files to our child */
3670e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3680e484fe5Seric 			if (e->e_lockfp != NULL)
3690e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3700e484fe5Seric 			e->e_lockfp = NULL;
3710e484fe5Seric 
3720e484fe5Seric 			/* close any random open files in the envelope */
3730e484fe5Seric 			closexscript(e);
3740e484fe5Seric 			if (e->e_dfp != NULL)
3750e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3760e484fe5Seric 			e->e_dfp = NULL;
3770e484fe5Seric 			e->e_id = e->e_df = NULL;
378c1ac89b1Seric 			return;
379c1ac89b1Seric 		}
380c1ac89b1Seric 
381c1ac89b1Seric 		/* double fork to avoid zombies */
382c1ac89b1Seric 		if (fork() > 0)
383c1ac89b1Seric 			exit(EX_OK);
384c1ac89b1Seric 
385c1ac89b1Seric 		/* be sure we are immune from the terminal */
3867880f3e4Seric 		disconnect(1, e);
387c1ac89b1Seric 
388c1ac89b1Seric 		/*
389c1ac89b1Seric 		**  Close any cached connections.
390c1ac89b1Seric 		**
391c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
392c1ac89b1Seric 		**	still knows about the connection.
393c1ac89b1Seric 		**
394c1ac89b1Seric 		**	This should only happen when delivering an error
395c1ac89b1Seric 		**	message.
396c1ac89b1Seric 		*/
397c1ac89b1Seric 
398c1ac89b1Seric 		mci_flush(FALSE, NULL);
399c1ac89b1Seric 
4006b2765c6Seric # endif /* HASFLOCK */
4016b2765c6Seric 
402c1ac89b1Seric 		break;
403c1ac89b1Seric 	}
404c1ac89b1Seric 
405c1ac89b1Seric 	/*
4069c9e68d9Seric 	**  Run through the list and send everything.
4075288e21aSeric 	**
4085288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4095288e21aSeric 	**	result in returned mail.
4109c9e68d9Seric 	*/
4119c9e68d9Seric 
4129c9e68d9Seric 	e->e_nsent = 0;
4135288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
414faad2b16Seric 
415faad2b16Seric 	/* now run through the queue */
4169c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4179c9e68d9Seric 	{
418fdc75a0fSeric #ifdef XDEBUG
419fdc75a0fSeric 		char wbuf[MAXNAME + 20];
420fdc75a0fSeric 
421fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
422fdc75a0fSeric 		checkfd012(wbuf);
423fdc75a0fSeric #endif
4249c9e68d9Seric 		if (mode == SM_VERIFY)
4259c9e68d9Seric 		{
4269c9e68d9Seric 			e->e_to = q->q_paddr;
4279c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4288dfca105Seric 			{
429fafe46e1Seric 				if (q->q_host != NULL && q->q_host[0] != '\0')
4308dfca105Seric 					message("deliverable: mailer %s, host %s, user %s",
4318dfca105Seric 						q->q_mailer->m_name,
4328dfca105Seric 						q->q_host,
4338dfca105Seric 						q->q_user);
434fafe46e1Seric 				else
435fafe46e1Seric 					message("deliverable: mailer %s, user %s",
436fafe46e1Seric 						q->q_mailer->m_name,
437fafe46e1Seric 						q->q_user);
4388dfca105Seric 			}
4399c9e68d9Seric 		}
4409c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4419c9e68d9Seric 		{
4429c9e68d9Seric # ifdef QUEUE
4439c9e68d9Seric 			/*
4449c9e68d9Seric 			**  Checkpoint the send list every few addresses
4459c9e68d9Seric 			*/
4469c9e68d9Seric 
4479c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4489c9e68d9Seric 			{
4499c9e68d9Seric 				queueup(e, TRUE, FALSE);
4509c9e68d9Seric 				e->e_nsent = 0;
4519c9e68d9Seric 			}
4529c9e68d9Seric # endif /* QUEUE */
4539c9e68d9Seric 			(void) deliver(e, q);
4549c9e68d9Seric 		}
4559c9e68d9Seric 	}
4569c9e68d9Seric 	Verbose = oldverbose;
4579c9e68d9Seric 
458fdc75a0fSeric #ifdef XDEBUG
459fdc75a0fSeric 	checkfd012("end of sendenvelope");
460fdc75a0fSeric #endif
461fdc75a0fSeric 
4629c9e68d9Seric 	if (mode == SM_FORK)
4639c9e68d9Seric 		finis();
4649c9e68d9Seric }
4659c9e68d9Seric /*
4669c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4679c9e68d9Seric **
4689c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4699c9e68d9Seric **	two processes on the same stack!!!
4709c9e68d9Seric **
4719c9e68d9Seric **	Parameters:
4729c9e68d9Seric **		none.
4739c9e68d9Seric **
4749c9e68d9Seric **	Returns:
4759c9e68d9Seric **		From a macro???  You've got to be kidding!
4769c9e68d9Seric **
4779c9e68d9Seric **	Side Effects:
4789c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4799c9e68d9Seric **			pid of child in parent, zero in child.
4809c9e68d9Seric **			-1 on unrecoverable error.
4819c9e68d9Seric **
4829c9e68d9Seric **	Notes:
4839c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4849c9e68d9Seric **		vfork for you.....
4859c9e68d9Seric */
4869c9e68d9Seric 
4879c9e68d9Seric # define NFORKTRIES	5
4889c9e68d9Seric 
4899c9e68d9Seric # ifndef FORK
4909c9e68d9Seric # define FORK	fork
4919c9e68d9Seric # endif
4929c9e68d9Seric 
4939c9e68d9Seric # define DOFORK(fORKfN) \
4949c9e68d9Seric {\
4959c9e68d9Seric 	register int i;\
4969c9e68d9Seric \
4979c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4989c9e68d9Seric 	{\
4999c9e68d9Seric 		pid = fORKfN();\
5009c9e68d9Seric 		if (pid >= 0)\
5019c9e68d9Seric 			break;\
5029c9e68d9Seric 		if (i > 0)\
5039c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
5049c9e68d9Seric 	}\
5059c9e68d9Seric }
5069c9e68d9Seric /*
5079c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5089c9e68d9Seric **
5099c9e68d9Seric **	Parameters:
5109c9e68d9Seric **		none.
5119c9e68d9Seric **
5129c9e68d9Seric **	Returns:
5139c9e68d9Seric **		pid of child in parent.
5149c9e68d9Seric **		zero in child.
5159c9e68d9Seric **		-1 on error.
5169c9e68d9Seric **
5179c9e68d9Seric **	Side Effects:
5189c9e68d9Seric **		returns twice, once in parent and once in child.
5199c9e68d9Seric */
5209c9e68d9Seric 
5219c9e68d9Seric dofork()
5229c9e68d9Seric {
5239c9e68d9Seric 	register int pid;
5249c9e68d9Seric 
5259c9e68d9Seric 	DOFORK(fork);
5269c9e68d9Seric 	return (pid);
5279c9e68d9Seric }
5289c9e68d9Seric /*
52913bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
53013bbc08cSeric **
53113bbc08cSeric **	This routine delivers to everyone on the same host as the
53213bbc08cSeric **	user on the head of the list.  It is clever about mailers
53313bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
53413bbc08cSeric **	that it will deliver to all these addresses however -- so
53513bbc08cSeric **	deliver should be called once for each address on the
53613bbc08cSeric **	list.
53725a99e2eSeric **
53825a99e2eSeric **	Parameters:
539588cad61Seric **		e -- the envelope to deliver.
540c77d1c25Seric **		firstto -- head of the address list to deliver to.
54125a99e2eSeric **
54225a99e2eSeric **	Returns:
54325a99e2eSeric **		zero -- successfully delivered.
54425a99e2eSeric **		else -- some failure, see ExitStat for more info.
54525a99e2eSeric **
54625a99e2eSeric **	Side Effects:
54725a99e2eSeric **		The standard input is passed off to someone.
54825a99e2eSeric */
54925a99e2eSeric 
550588cad61Seric deliver(e, firstto)
551588cad61Seric 	register ENVELOPE *e;
552c77d1c25Seric 	ADDRESS *firstto;
55325a99e2eSeric {
55478442df3Seric 	char *host;			/* host being sent to */
55578442df3Seric 	char *user;			/* user being sent to */
55625a99e2eSeric 	char **pvp;
5575dfc646bSeric 	register char **mvp;
55825a99e2eSeric 	register char *p;
559588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5606259796dSeric 	ADDRESS *ctladdr;
561b31e7f2bSeric 	register MCI *mci;
562c77d1c25Seric 	register ADDRESS *to = firstto;
563c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
564772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
565911693bfSbostic 	int rcode;			/* response code */
566e103b48fSeric 	char *firstsig;			/* signature of firstto */
5679c9e68d9Seric 	int pid;
5689c9e68d9Seric 	char *curhost;
5699c9e68d9Seric 	int mpvect[2];
5709c9e68d9Seric 	int rpvect[2];
571ee6bf8dfSeric 	char *pv[MAXPV+1];
572579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
573ee6bf8dfSeric 	char buf[MAXNAME];
574c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
575fabb3bd4Seric 	extern int checkcompat();
57625a99e2eSeric 
57735490626Seric 	errno = 0;
578ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5795dfc646bSeric 		return (0);
58025a99e2eSeric 
581134746fbSeric #ifdef NAMED_BIND
582912a731aSbostic 	/* unless interactive, try twice, over a minute */
5838d19a23dSeric 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP)
5848d19a23dSeric 	{
585912a731aSbostic 		_res.retrans = 30;
586912a731aSbostic 		_res.retry = 2;
587912a731aSbostic 	}
588d4bd8f0eSbostic #endif
589912a731aSbostic 
59051552439Seric 	m = to->q_mailer;
59151552439Seric 	host = to->q_host;
592c9be6216Seric 	CurEnv = e;			/* just in case */
5934384d521Seric 	e->e_statmsg = NULL;
594df106f0bSeric 	SmtpError[0] = '\0';
59551552439Seric 
5966ef48975Seric 	if (tTd(10, 1))
5975dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
59851552439Seric 			m->m_mno, host, to->q_user);
599ab81ee53Seric 	if (tTd(10, 100))
600ab81ee53Seric 		printopenfds(FALSE);
601f3dbc832Seric 
602f3dbc832Seric 	/*
603f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
604f3dbc832Seric 	**  connections now, just mark these addresses and return.
605f3dbc832Seric 	**	This is useful if we want to batch connections to
606f3dbc832Seric 	**	reduce load.  This will cause the messages to be
607f3dbc832Seric 	**	queued up, and a daemon will come along to send the
608f3dbc832Seric 	**	messages later.
609f3dbc832Seric 	**		This should be on a per-mailer basis.
610f3dbc832Seric 	*/
611f3dbc832Seric 
61279aa5155Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
613f3dbc832Seric 	{
614f3dbc832Seric 		for (; to != NULL; to = to->q_next)
615f4560e80Seric 		{
616ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
617c6e18ac5Seric 			    to->q_mailer != m)
618f4560e80Seric 				continue;
619268a25e1Seric 			to->q_flags |= QQUEUEUP;
620588cad61Seric 			e->e_to = to->q_paddr;
62108b25121Seric 			message("queued");
6222f624c86Seric 			if (LogLevel > 8)
6234dee0003Seric 				logdelivery(m, NULL, "queued", NULL, e);
624f4560e80Seric 		}
625588cad61Seric 		e->e_to = NULL;
626f3dbc832Seric 		return (0);
627f3dbc832Seric 	}
628f3dbc832Seric 
62925a99e2eSeric 	/*
6305dfc646bSeric 	**  Do initial argv setup.
6315dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6325dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6335dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6345dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6355dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6363bea8136Seric 	**		The from address rewrite is expected to make
6373bea8136Seric 	**		the address relative to the other end.
6385dfc646bSeric 	*/
6395dfc646bSeric 
64078442df3Seric 	/* rewrite from address, using rewriting rules */
641efe54562Seric 	rcode = EX_OK;
642efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
643efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
644efe54562Seric 					   &rcode, e));
645ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
646588cad61Seric 	define('h', host, e);			/* to host */
6475dfc646bSeric 	Errors = 0;
6485dfc646bSeric 	pvp = pv;
6495dfc646bSeric 	*pvp++ = m->m_argv[0];
6505dfc646bSeric 
6515dfc646bSeric 	/* insert -f or -r flag as appropriate */
65257fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6535dfc646bSeric 	{
65457fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6555dfc646bSeric 			*pvp++ = "-f";
6565dfc646bSeric 		else
6575dfc646bSeric 			*pvp++ = "-r";
658c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6595dfc646bSeric 	}
6605dfc646bSeric 
6615dfc646bSeric 	/*
6625dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6635dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6645dfc646bSeric 	**  be one of these, and there are only a few more slots
6655dfc646bSeric 	**  in the pv after it.
6665dfc646bSeric 	*/
6675dfc646bSeric 
6685dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6695dfc646bSeric 	{
6702bc47524Seric 		/* can't use strchr here because of sign extension problems */
6712bc47524Seric 		while (*p != '\0')
6722bc47524Seric 		{
6732bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6742bc47524Seric 			{
6752bc47524Seric 				if (*p == 'u')
6765dfc646bSeric 					break;
6772bc47524Seric 			}
6782bc47524Seric 		}
6792bc47524Seric 
6802bc47524Seric 		if (*p != '\0')
6815dfc646bSeric 			break;
6825dfc646bSeric 
6835dfc646bSeric 		/* this entry is safe -- go ahead and process it */
684588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6855dfc646bSeric 		*pvp++ = newstr(buf);
6865dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6875dfc646bSeric 		{
68808b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6895dfc646bSeric 			return (-1);
6905dfc646bSeric 		}
6915dfc646bSeric 	}
692c579ef51Seric 
69333db8731Seric 	/*
69433db8731Seric 	**  If we have no substitution for the user name in the argument
69533db8731Seric 	**  list, we know that we must supply the names otherwise -- and
69633db8731Seric 	**  SMTP is the answer!!
69733db8731Seric 	*/
69833db8731Seric 
6995dfc646bSeric 	if (*mvp == NULL)
700c579ef51Seric 	{
701c579ef51Seric 		/* running SMTP */
7022c7e1b8dSeric # ifdef SMTP
703c579ef51Seric 		clever = TRUE;
704c579ef51Seric 		*pvp = NULL;
7056c2c3107Seric # else /* SMTP */
70633db8731Seric 		/* oops!  we don't implement SMTP */
707df106f0bSeric 		syserr("554 SMTP style mailer not implemented");
7082c7e1b8dSeric 		return (EX_SOFTWARE);
7096c2c3107Seric # endif /* SMTP */
710c579ef51Seric 	}
7115dfc646bSeric 
7125dfc646bSeric 	/*
7135dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7145dfc646bSeric 	**  run through our address list and append all the addresses
7155dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7165dfc646bSeric 	**  always send another copy later.
7175dfc646bSeric 	*/
7185dfc646bSeric 
7195dfc646bSeric 	tobuf[0] = '\0';
720588cad61Seric 	e->e_to = tobuf;
7216259796dSeric 	ctladdr = NULL;
722e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7235dfc646bSeric 	for (; to != NULL; to = to->q_next)
7245dfc646bSeric 	{
7255dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
72657fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7275dfc646bSeric 			break;
7285dfc646bSeric 
7295dfc646bSeric 		/* if already sent or not for this host, don't send */
730ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
731e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
732e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7335dfc646bSeric 			continue;
7346259796dSeric 
7354b22ea87Seric 		/* avoid overflowing tobuf */
736aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7374b22ea87Seric 			break;
7384b22ea87Seric 
7396ef48975Seric 		if (tTd(10, 1))
740772e6e50Seric 		{
741772e6e50Seric 			printf("\nsend to ");
742772e6e50Seric 			printaddr(to, FALSE);
743772e6e50Seric 		}
744772e6e50Seric 
7456259796dSeric 		/* compute effective uid/gid when sending */
746960e665aSeric 		/* XXX perhaps this should be to->q_mailer != LocalMailer ?? */
747960e665aSeric 		/* XXX perhaps it should be a mailer flag? */
748960e665aSeric 		if (to->q_mailer == ProgMailer || to->q_mailer == FileMailer)
7496259796dSeric 			ctladdr = getctladdr(to);
7506259796dSeric 
7515dfc646bSeric 		user = to->q_user;
752588cad61Seric 		e->e_to = to->q_paddr;
75375f1ade9Seric 		if (tTd(10, 5))
75475f1ade9Seric 		{
75575f1ade9Seric 			printf("deliver: QDONTSEND ");
75675f1ade9Seric 			printaddr(to, FALSE);
75775f1ade9Seric 		}
758ee4b0922Seric 		to->q_flags |= QDONTSEND;
7595dfc646bSeric 
7605dfc646bSeric 		/*
7615dfc646bSeric 		**  Check to see that these people are allowed to
7625dfc646bSeric 		**  talk to each other.
7632a6e0786Seric 		*/
7642a6e0786Seric 
76569582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
76669582d2fSeric 		{
76769582d2fSeric 			NoReturn = TRUE;
76808b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
7694dee0003Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e);
77069582d2fSeric 			continue;
77169582d2fSeric 		}
772fabb3bd4Seric 		rcode = checkcompat(to, e);
7731793c9c5Seric 		if (rcode != EX_OK)
7745dfc646bSeric 		{
7751000c37aSeric 			markfailure(e, to, rcode);
7764dee0003Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
7775dfc646bSeric 			continue;
7785dfc646bSeric 		}
7792a6e0786Seric 
7802a6e0786Seric 		/*
7819ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7829ec9501bSeric 		**	about them.
78325a99e2eSeric 		*/
78425a99e2eSeric 
78557fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
78625a99e2eSeric 		{
7871d8f1806Seric 			stripquotes(user);
7881d8f1806Seric 			stripquotes(host);
78925a99e2eSeric 		}
79025a99e2eSeric 
791cdb828c5Seric 		/* hack attack -- delivermail compatibility */
792cdb828c5Seric 		if (m == ProgMailer && *user == '|')
793cdb828c5Seric 			user++;
794cdb828c5Seric 
79525a99e2eSeric 		/*
7963efaed6eSeric 		**  If an error message has already been given, don't
7973efaed6eSeric 		**	bother to send to this address.
7983efaed6eSeric 		**
7993efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
8003efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
8013efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
8023efaed6eSeric 		*/
8033efaed6eSeric 
8046cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8053efaed6eSeric 			continue;
8063efaed6eSeric 
807f2fec898Seric 		/* save statistics.... */
808588cad61Seric 		markstats(e, to);
809f2fec898Seric 
8103efaed6eSeric 		/*
81125a99e2eSeric 		**  See if this user name is "special".
81225a99e2eSeric 		**	If the user name has a slash in it, assume that this
81351552439Seric 		**	is a file -- send it off without further ado.  Note
81451552439Seric 		**	that this type of addresses is not processed along
81551552439Seric 		**	with the others, so we fudge on the To person.
81625a99e2eSeric 		*/
81725a99e2eSeric 
8182c017f8dSeric 		if (m == FileMailer)
81925a99e2eSeric 		{
82003463a75Seric 			rcode = mailfile(user, ctladdr, e);
82103463a75Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
822dde5acadSeric 			if (rcode == EX_OK)
823dde5acadSeric 				to->q_flags |= QSENT;
8245dfc646bSeric 			continue;
82525a99e2eSeric 		}
82625a99e2eSeric 
82713bbc08cSeric 		/*
82813bbc08cSeric 		**  Address is verified -- add this user to mailer
82913bbc08cSeric 		**  argv, and add it to the print list of recipients.
83013bbc08cSeric 		*/
83113bbc08cSeric 
832508daeccSeric 		/* link together the chain of recipients */
833508daeccSeric 		to->q_tchain = tochain;
834508daeccSeric 		tochain = to;
835508daeccSeric 
8365dfc646bSeric 		/* create list of users for error messages */
837db8841e9Seric 		(void) strcat(tobuf, ",");
838db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
839588cad61Seric 		define('u', user, e);		/* to user */
840b6f89e6eSeric 		p = to->q_home;
841b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
842b6f89e6eSeric 			p = ctladdr->q_home;
843b6f89e6eSeric 		define('z', p, e);	/* user's home */
8445dfc646bSeric 
845c579ef51Seric 		/*
846508daeccSeric 		**  Expand out this user into argument list.
847c579ef51Seric 		*/
848c579ef51Seric 
849508daeccSeric 		if (!clever)
850c579ef51Seric 		{
851588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8525dfc646bSeric 			*pvp++ = newstr(buf);
8535dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8545dfc646bSeric 			{
8555dfc646bSeric 				/* allow some space for trailing parms */
8565dfc646bSeric 				break;
8575dfc646bSeric 			}
8585dfc646bSeric 		}
859c579ef51Seric 	}
8605dfc646bSeric 
861145b49b1Seric 	/* see if any addresses still exist */
862145b49b1Seric 	if (tobuf[0] == '\0')
863c579ef51Seric 	{
864588cad61Seric 		define('g', (char *) NULL, e);
865145b49b1Seric 		return (0);
866c579ef51Seric 	}
867145b49b1Seric 
8685dfc646bSeric 	/* print out messages as full list */
86963780dbdSeric 	e->e_to = tobuf + 1;
8705dfc646bSeric 
8715dfc646bSeric 	/*
8725dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8735dfc646bSeric 	*/
8745dfc646bSeric 
875c579ef51Seric 	while (!clever && *++mvp != NULL)
8765dfc646bSeric 	{
877588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8785dfc646bSeric 		*pvp++ = newstr(buf);
8795dfc646bSeric 		if (pvp >= &pv[MAXPV])
88008b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8815dfc646bSeric 	}
8825dfc646bSeric 	*pvp++ = NULL;
8835dfc646bSeric 
88425a99e2eSeric 	/*
88525a99e2eSeric 	**  Call the mailer.
8866328bdf7Seric 	**	The argument vector gets built, pipes
88725a99e2eSeric 	**	are created as necessary, and we fork & exec as
8886328bdf7Seric 	**	appropriate.
889c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
89025a99e2eSeric 	*/
89125a99e2eSeric 
892960e665aSeric 	/*XXX this seems a bit wierd */
8934899f6b5Seric 	if (ctladdr == NULL && m != ProgMailer &&
8944899f6b5Seric 	    bitset(QGOODUID, e->e_from.q_flags))
895960e665aSeric 		ctladdr = &e->e_from;
896960e665aSeric 
897134746fbSeric #ifdef NAMED_BIND
8982bcc6d2dSeric 	if (ConfigLevel < 2)
899912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
900134746fbSeric #endif
9019c9e68d9Seric 
9029c9e68d9Seric 	if (tTd(11, 1))
903134746fbSeric 	{
9049c9e68d9Seric 		printf("openmailer:");
9059c9e68d9Seric 		printav(pv);
9069c9e68d9Seric 	}
9079c9e68d9Seric 	errno = 0;
9089c9e68d9Seric 
9099c9e68d9Seric 	CurHostName = m->m_mailer;
9109c9e68d9Seric 
9119c9e68d9Seric 	/*
9129c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
9139c9e68d9Seric 	**  connection.
9149c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9159c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9169c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9179c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9189c9e68d9Seric 	*/
9199c9e68d9Seric 
9209c9e68d9Seric 	curhost = NULL;
921c931b82bSeric 	SmtpPhase = NULL;
922df106f0bSeric 	mci = NULL;
9239c9e68d9Seric 
9247febfd66Seric #ifdef XDEBUG
9257febfd66Seric 	{
9267febfd66Seric 		char wbuf[MAXLINE];
9277febfd66Seric 
9287febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9297febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
9307febfd66Seric 		checkfd012(wbuf);
9317febfd66Seric 	}
9327febfd66Seric #endif
9337febfd66Seric 
9349c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9359c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9369c9e68d9Seric 	{
9379c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9389c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9399c9e68d9Seric 		mci->mci_in = stdin;
9409c9e68d9Seric 		mci->mci_out = stdout;
9419c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9429c9e68d9Seric 		mci->mci_mailer = m;
9439c9e68d9Seric 	}
9449c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9459c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9469c9e68d9Seric 	{
9479c9e68d9Seric #ifdef DAEMON
9489c9e68d9Seric 		register int i;
9499c9e68d9Seric 		register u_short port;
9509c9e68d9Seric 
951a5d44642Seric 		if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
952a5d44642Seric 		{
953a5d44642Seric 			syserr("null host name for %s mailer", m->m_mailer);
954a5d44642Seric 			rcode = EX_CONFIG;
955a5d44642Seric 			goto give_up;
956a5d44642Seric 		}
957a5d44642Seric 
9589c9e68d9Seric 		CurHostName = pv[1];
9599c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9609c9e68d9Seric 
9619c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9629c9e68d9Seric 		{
963a5d44642Seric 			syserr("null host signature for %s", pv[1]);
964845e533cSeric 			rcode = EX_OSERR;
965b31e7f2bSeric 			goto give_up;
966b31e7f2bSeric 		}
9679c9e68d9Seric 
9689c9e68d9Seric 		if (!clever)
9699c9e68d9Seric 		{
9709c9e68d9Seric 			syserr("554 non-clever IPC");
971df106f0bSeric 			rcode = EX_CONFIG;
9729c9e68d9Seric 			goto give_up;
9739c9e68d9Seric 		}
9749c9e68d9Seric 		if (pv[2] != NULL)
9759c9e68d9Seric 			port = atoi(pv[2]);
9769c9e68d9Seric 		else
9779c9e68d9Seric 			port = 0;
9789c9e68d9Seric tryhost:
9799c9e68d9Seric 		while (*curhost != '\0')
9809c9e68d9Seric 		{
9819c9e68d9Seric 			register char *p;
9829c9e68d9Seric 			static char hostbuf[MAXNAME];
9839c9e68d9Seric 
9849c9e68d9Seric 			/* pull the next host from the signature */
9859c9e68d9Seric 			p = strchr(curhost, ':');
9869c9e68d9Seric 			if (p == NULL)
9879c9e68d9Seric 				p = &curhost[strlen(curhost)];
988df106f0bSeric 			if (p == curhost)
989df106f0bSeric 			{
990df106f0bSeric 				syserr("deliver: null host name in signature");
9918087428aSeric 				curhost++;
992df106f0bSeric 				continue;
993df106f0bSeric 			}
9949c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9959c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9969c9e68d9Seric 			if (*p != '\0')
9979c9e68d9Seric 				p++;
9989c9e68d9Seric 			curhost = p;
9999c9e68d9Seric 
10009c9e68d9Seric 			/* see if we already know that this host is fried */
10019c9e68d9Seric 			CurHostName = hostbuf;
10029c9e68d9Seric 			mci = mci_get(hostbuf, m);
10039c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
10049c9e68d9Seric 			{
10059c9e68d9Seric 				if (tTd(11, 1))
10069c9e68d9Seric 				{
10079c9e68d9Seric 					printf("openmailer: ");
10084052916eSeric 					mci_dump(mci, FALSE);
10099c9e68d9Seric 				}
10109c9e68d9Seric 				CurHostName = mci->mci_host;
10119c9e68d9Seric 				break;
10129c9e68d9Seric 			}
10139c9e68d9Seric 			mci->mci_mailer = m;
10149c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
10159c9e68d9Seric 				continue;
10169c9e68d9Seric 
10179c9e68d9Seric 			/* try the connection */
10189c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
10199c9e68d9Seric 			message("Connecting to %s (%s)...",
10209c9e68d9Seric 				hostbuf, m->m_name);
10219c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
10229c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
10239c9e68d9Seric 			mci->mci_exitstat = i;
10249c9e68d9Seric 			mci->mci_errno = errno;
1025f170942cSeric #ifdef NAMED_BIND
1026f170942cSeric 			mci->mci_herrno = h_errno;
1027f170942cSeric #endif
10289c9e68d9Seric 			if (i == EX_OK)
10299c9e68d9Seric 			{
10309c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10319c9e68d9Seric 				mci_cache(mci);
1032f170942cSeric 				if (TrafficLogFile != NULL)
1033f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1034f170942cSeric 						getpid(), hostbuf);
10359c9e68d9Seric 				break;
10369c9e68d9Seric 			}
10379c9e68d9Seric 			else if (tTd(11, 1))
10389c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10399c9e68d9Seric 					i, errno);
10409c9e68d9Seric 
10419c9e68d9Seric 			/* enter status of this host */
10429c9e68d9Seric 			setstat(i);
1043df106f0bSeric 
1044df106f0bSeric 			/* should print some message here for -v mode */
1045df106f0bSeric 		}
1046df106f0bSeric 		if (mci == NULL)
1047df106f0bSeric 		{
1048df106f0bSeric 			syserr("deliver: no host name");
1049df106f0bSeric 			rcode = EX_OSERR;
1050df106f0bSeric 			goto give_up;
10519c9e68d9Seric 		}
10529c9e68d9Seric 		mci->mci_pid = 0;
10539c9e68d9Seric #else /* no DAEMON */
10549c9e68d9Seric 		syserr("554 openmailer: no IPC");
10559c9e68d9Seric 		if (tTd(11, 1))
10569c9e68d9Seric 			printf("openmailer: NULL\n");
1057df106f0bSeric 		rcode = EX_UNAVAILABLE;
1058df106f0bSeric 		goto give_up;
10599c9e68d9Seric #endif /* DAEMON */
10609c9e68d9Seric 	}
10619c9e68d9Seric 	else
10629c9e68d9Seric 	{
1063f170942cSeric 		if (TrafficLogFile != NULL)
10640e3bfef5Seric 		{
1065f170942cSeric 			char **av;
1066f170942cSeric 
1067f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1068f170942cSeric 			for (av = pv; *av != NULL; av++)
1069f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1070f170942cSeric 			fprintf(TrafficLogFile, "\n");
10716fe8c3bcSeric 		}
10726fe8c3bcSeric 
10739c9e68d9Seric 		/* create a pipe to shove the mail through */
10749c9e68d9Seric 		if (pipe(mpvect) < 0)
10759c9e68d9Seric 		{
10760e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10770e3bfef5Seric 				e->e_to, m->m_name);
10789c9e68d9Seric 			if (tTd(11, 1))
10799c9e68d9Seric 				printf("openmailer: NULL\n");
10809c9e68d9Seric 			rcode = EX_OSERR;
10819c9e68d9Seric 			goto give_up;
10829c9e68d9Seric 		}
10839c9e68d9Seric 
10849c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10859c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10869c9e68d9Seric 		{
10870e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10880e3bfef5Seric 				e->e_to, m->m_name);
10899c9e68d9Seric 			(void) close(mpvect[0]);
10909c9e68d9Seric 			(void) close(mpvect[1]);
10919c9e68d9Seric 			if (tTd(11, 1))
10929c9e68d9Seric 				printf("openmailer: NULL\n");
10939c9e68d9Seric 			rcode = EX_OSERR;
10949c9e68d9Seric 			goto give_up;
10959c9e68d9Seric 		}
10969c9e68d9Seric 
10979c9e68d9Seric 		/*
10989c9e68d9Seric 		**  Actually fork the mailer process.
10999c9e68d9Seric 		**	DOFORK is clever about retrying.
11009c9e68d9Seric 		**
11019c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
11029c9e68d9Seric 		**	around so that endmail will get it.
11039c9e68d9Seric 		*/
11049c9e68d9Seric 
11059c9e68d9Seric 		if (e->e_xfp != NULL)
11069c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
11079c9e68d9Seric 		(void) fflush(stdout);
11089c9e68d9Seric # ifdef SIGCHLD
11092b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
11109c9e68d9Seric # endif /* SIGCHLD */
11119c9e68d9Seric 		DOFORK(FORK);
11129c9e68d9Seric 		/* pid is set by DOFORK */
11139c9e68d9Seric 		if (pid < 0)
11149c9e68d9Seric 		{
11159c9e68d9Seric 			/* failure */
11160e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
11170e3bfef5Seric 				e->e_to, m->m_name);
11189c9e68d9Seric 			(void) close(mpvect[0]);
11199c9e68d9Seric 			(void) close(mpvect[1]);
11209c9e68d9Seric 			if (clever)
11219c9e68d9Seric 			{
11229c9e68d9Seric 				(void) close(rpvect[0]);
11239c9e68d9Seric 				(void) close(rpvect[1]);
11249c9e68d9Seric 			}
11259c9e68d9Seric 			if (tTd(11, 1))
11269c9e68d9Seric 				printf("openmailer: NULL\n");
11279c9e68d9Seric 			rcode = EX_OSERR;
11289c9e68d9Seric 			goto give_up;
11299c9e68d9Seric 		}
11309c9e68d9Seric 		else if (pid == 0)
11319c9e68d9Seric 		{
11329c9e68d9Seric 			int i;
11339c9e68d9Seric 			int saveerrno;
11349c9e68d9Seric 			char **ep;
11359c9e68d9Seric 			char *env[MAXUSERENVIRON];
11369c9e68d9Seric 			extern char **environ;
11379c9e68d9Seric 			extern int DtableSize;
11389c9e68d9Seric 
11399c9e68d9Seric 			/* child -- set up input & exec mailer */
11402b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
11412b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
11422b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11439c9e68d9Seric 
114444f2317fSeric 			/* reset user and group */
114544f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
114644f2317fSeric 			{
114744f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
114844f2317fSeric 				{
114944f2317fSeric 					(void) initgroups(DefUser, DefGid);
1150fc354902Seric 					(void) setgid(DefGid);
115144f2317fSeric 					(void) setuid(DefUid);
115244f2317fSeric 				}
115344f2317fSeric 				else
115444f2317fSeric 				{
115544f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
115644f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
115744f2317fSeric 						ctladdr->q_gid);
115851cfe111Seric 					(void) setgid(ctladdr->q_gid);
115944f2317fSeric 					(void) setuid(ctladdr->q_uid);
116044f2317fSeric 				}
116144f2317fSeric 			}
116244f2317fSeric 
116344f2317fSeric 			if (tTd(11, 2))
116444f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
116544f2317fSeric 					getuid(), geteuid());
116644f2317fSeric 
1167b986f6aaSeric 			/* move into some "safe" directory */
1168b986f6aaSeric 			if (m->m_execdir != NULL)
1169b986f6aaSeric 			{
1170b986f6aaSeric 				char *p, *q;
1171b986f6aaSeric 				char buf[MAXLINE];
1172b986f6aaSeric 
1173b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1174b986f6aaSeric 				{
1175b986f6aaSeric 					q = strchr(p, ':');
1176b986f6aaSeric 					if (q != NULL)
1177b986f6aaSeric 						*q = '\0';
1178b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1179b986f6aaSeric 					if (q != NULL)
1180b986f6aaSeric 						*q++ = ':';
1181b986f6aaSeric 					if (tTd(11, 20))
1182b986f6aaSeric 						printf("openmailer: trydir %s\n",
1183b986f6aaSeric 							buf);
1184b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1185b986f6aaSeric 						break;
1186b986f6aaSeric 				}
1187b986f6aaSeric 			}
1188b986f6aaSeric 
11899c9e68d9Seric 			/* arrange to filter std & diag output of command */
11909c9e68d9Seric 			if (clever)
11919c9e68d9Seric 			{
11929c9e68d9Seric 				(void) close(rpvect[0]);
11936fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11946fe8c3bcSeric 				{
11950e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11960e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11976fe8c3bcSeric 					_exit(EX_OSERR);
11986fe8c3bcSeric 				}
11999c9e68d9Seric 				(void) close(rpvect[1]);
12009c9e68d9Seric 			}
12018d19a23dSeric 			else if (OpMode == MD_SMTP || OpMode == MD_DAEMON || HoldErrs)
12029c9e68d9Seric 			{
12039c9e68d9Seric 				/* put mailer output in transcript */
12046fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
12056fe8c3bcSeric 				{
12060e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
12070e3bfef5Seric 						e->e_to, m->m_name,
12086fe8c3bcSeric 						fileno(e->e_xfp));
12096fe8c3bcSeric 					_exit(EX_OSERR);
12109c9e68d9Seric 				}
12116fe8c3bcSeric 			}
12126fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
12136fe8c3bcSeric 			{
12140e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
12150e3bfef5Seric 					e->e_to, m->m_name);
12166fe8c3bcSeric 				_exit(EX_OSERR);
12176fe8c3bcSeric 			}
12189c9e68d9Seric 
12199c9e68d9Seric 			/* arrange to get standard input */
12209c9e68d9Seric 			(void) close(mpvect[1]);
12219c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
12229c9e68d9Seric 			{
12230e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
12240e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
12259c9e68d9Seric 				_exit(EX_OSERR);
12269c9e68d9Seric 			}
12279c9e68d9Seric 			(void) close(mpvect[0]);
12289c9e68d9Seric 
12299c9e68d9Seric 			/* arrange for all the files to be closed */
12309c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12319c9e68d9Seric 			{
12329c9e68d9Seric 				register int j;
123344f2317fSeric 
12349c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12359c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
12369c9e68d9Seric 			}
12379c9e68d9Seric 
12389c9e68d9Seric 			/* set up the mailer environment */
12399c9e68d9Seric 			i = 0;
12409c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12419c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12429c9e68d9Seric 			{
12439c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12449c9e68d9Seric 					env[i++] = *ep;
12459c9e68d9Seric 			}
12469c9e68d9Seric 			env[i++] = NULL;
12479c9e68d9Seric 
12489c9e68d9Seric 			/* try to execute the mailer */
12499c9e68d9Seric 			execve(m->m_mailer, pv, env);
12509c9e68d9Seric 			saveerrno = errno;
12519c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12527c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12537c941fd2Seric 				_exit(EX_OSERR);
12549c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12559c9e68d9Seric 		}
12569c9e68d9Seric 
12579c9e68d9Seric 		/*
12589c9e68d9Seric 		**  Set up return value.
12599c9e68d9Seric 		*/
12609c9e68d9Seric 
12619c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12629c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12639c9e68d9Seric 		mci->mci_mailer = m;
12649c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12659c9e68d9Seric 		mci->mci_pid = pid;
12669c9e68d9Seric 		(void) close(mpvect[0]);
12679c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
1268335eae58Seric 		if (mci->mci_out == NULL)
1269335eae58Seric 		{
1270335eae58Seric 			syserr("deliver: cannot create mailer output channel, fd=%d",
1271335eae58Seric 				mpvect[1]);
1272335eae58Seric 			(void) close(mpvect[1]);
1273335eae58Seric 			if (clever)
1274335eae58Seric 			{
1275335eae58Seric 				(void) close(rpvect[0]);
1276335eae58Seric 				(void) close(rpvect[1]);
1277335eae58Seric 			}
1278335eae58Seric 			rcode = EX_OSERR;
1279335eae58Seric 			goto give_up;
1280335eae58Seric 		}
12819c9e68d9Seric 		if (clever)
12829c9e68d9Seric 		{
12839c9e68d9Seric 			(void) close(rpvect[1]);
12849c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
1285335eae58Seric 			if (mci->mci_in == NULL)
1286335eae58Seric 			{
1287335eae58Seric 				syserr("deliver: cannot create mailer input channel, fd=%d",
1288335eae58Seric 					mpvect[1]);
1289335eae58Seric 				(void) close(rpvect[0]);
1290335eae58Seric 				fclose(mci->mci_out);
1291335eae58Seric 				mci->mci_out = NULL;
1292335eae58Seric 				rcode = EX_OSERR;
1293335eae58Seric 				goto give_up;
1294335eae58Seric 			}
12959c9e68d9Seric 		}
12969c9e68d9Seric 		else
12979c9e68d9Seric 		{
12989c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12999c9e68d9Seric 			mci->mci_in = NULL;
13009c9e68d9Seric 		}
13019c9e68d9Seric 	}
13029c9e68d9Seric 
13039c9e68d9Seric 	/*
13049c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
13059c9e68d9Seric 	*/
13069c9e68d9Seric 
13079c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
13089c9e68d9Seric 	{
13099c9e68d9Seric 		smtpinit(m, mci, e);
13109c9e68d9Seric 	}
13119c9e68d9Seric 	if (tTd(11, 1))
13129c9e68d9Seric 	{
13139c9e68d9Seric 		printf("openmailer: ");
13144052916eSeric 		mci_dump(mci, FALSE);
13159c9e68d9Seric 	}
13169c9e68d9Seric 
13179c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1318b31e7f2bSeric 	{
1319b31e7f2bSeric 		/* couldn't open the mailer */
1320b31e7f2bSeric 		rcode = mci->mci_exitstat;
13212a6bc25bSeric 		errno = mci->mci_errno;
1322f170942cSeric #ifdef NAMED_BIND
1323f170942cSeric 		h_errno = mci->mci_herrno;
1324f170942cSeric #endif
1325b31e7f2bSeric 		if (rcode == EX_OK)
1326b31e7f2bSeric 		{
1327b31e7f2bSeric 			/* shouldn't happen */
132808b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
13296b0f339dSeric 				rcode, mci->mci_state, firstsig);
1330b31e7f2bSeric 			rcode = EX_SOFTWARE;
1331b31e7f2bSeric 		}
1332cb082c5bSeric 		else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0')
133390891494Seric 		{
133490891494Seric 			/* try next MX site */
133590891494Seric 			goto tryhost;
133690891494Seric 		}
1337b31e7f2bSeric 	}
1338b31e7f2bSeric 	else if (!clever)
1339b31e7f2bSeric 	{
1340b31e7f2bSeric 		/*
1341b31e7f2bSeric 		**  Format and send message.
1342b31e7f2bSeric 		*/
134315d084d5Seric 
1344b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1345b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1346b31e7f2bSeric 		putline("\n", mci->mci_out, m);
134703c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1348b31e7f2bSeric 
1349b31e7f2bSeric 		/* get the exit status */
1350c9be6216Seric 		rcode = endmailer(mci, e, pv);
1351134746fbSeric 	}
1352134746fbSeric 	else
1353b31e7f2bSeric #ifdef SMTP
1354134746fbSeric 	{
1355b31e7f2bSeric 		/*
1356b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1357b31e7f2bSeric 		*/
135815d084d5Seric 
1359b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1360b31e7f2bSeric 		if (rcode == EX_OK)
136175889e88Seric 		{
1362ded0d3daSkarels 			register char *t = tobuf;
1363ded0d3daSkarels 			register int i;
1364ded0d3daSkarels 
1365588cad61Seric 			/* send the recipient list */
136663780dbdSeric 			tobuf[0] = '\0';
136775889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
136875889e88Seric 			{
136963780dbdSeric 				e->e_to = to->q_paddr;
137015d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
137175889e88Seric 				{
137283b7ddc9Seric 					markfailure(e, to, i);
13734dee0003Seric 					giveresponse(i, m, mci, ctladdr, e);
137463780dbdSeric 				}
137575889e88Seric 				else
137675889e88Seric 				{
1377911693bfSbostic 					*t++ = ',';
1378b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1379b31e7f2bSeric 						continue;
1380ee39df61Seric 					*t = '\0';
1381588cad61Seric 				}
1382588cad61Seric 			}
1383588cad61Seric 
138463780dbdSeric 			/* now send the data */
138563780dbdSeric 			if (tobuf[0] == '\0')
1386b31e7f2bSeric 			{
13879c9e68d9Seric 				rcode = EX_OK;
138863780dbdSeric 				e->e_to = NULL;
1389b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1390b31e7f2bSeric 					smtprset(m, mci, e);
1391b31e7f2bSeric 			}
139275889e88Seric 			else
139375889e88Seric 			{
139463780dbdSeric 				e->e_to = tobuf + 1;
139575889e88Seric 				rcode = smtpdata(m, mci, e);
139663780dbdSeric 			}
139763780dbdSeric 
139863780dbdSeric 			/* now close the connection */
1399b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
140015d084d5Seric 				smtpquit(m, mci, e);
140163780dbdSeric 		}
1402cb082c5bSeric 		if (rcode != EX_OK && curhost != NULL && *curhost != '\0')
14039c9e68d9Seric 		{
14049c9e68d9Seric 			/* try next MX site */
14059c9e68d9Seric 			goto tryhost;
14069c9e68d9Seric 		}
1407c579ef51Seric 	}
1408b31e7f2bSeric #else /* not SMTP */
1409a05b3449Sbostic 	{
141008b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1411845e533cSeric 		rcode = EX_CONFIG;
1412b31e7f2bSeric 		goto give_up;
1413a05b3449Sbostic 	}
1414b31e7f2bSeric #endif /* SMTP */
1415134746fbSeric #ifdef NAMED_BIND
14162bcc6d2dSeric 	if (ConfigLevel < 2)
1417912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1418134746fbSeric #endif
14195dfc646bSeric 
1420b31e7f2bSeric 	/* arrange a return receipt if requested */
1421df106f0bSeric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1422df106f0bSeric 	    bitnset(M_LOCALMAILER, m->m_flags))
1423b31e7f2bSeric 	{
1424b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1425b31e7f2bSeric 		/* do we want to send back more info? */
1426b31e7f2bSeric 	}
1427b31e7f2bSeric 
1428c77d1c25Seric 	/*
142963780dbdSeric 	**  Do final status disposal.
143063780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1431c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1432c77d1c25Seric 	**		addressees.
1433c77d1c25Seric 	*/
1434c77d1c25Seric 
1435b31e7f2bSeric   give_up:
143663780dbdSeric 	if (tobuf[0] != '\0')
14374dee0003Seric 		giveresponse(rcode, m, mci, ctladdr, e);
1438772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1439b31e7f2bSeric 	{
1440dde5acadSeric 		if (rcode != EX_OK)
144183b7ddc9Seric 			markfailure(e, to, rcode);
1442dde5acadSeric 		else
1443655518ecSeric 		{
1444dde5acadSeric 			to->q_flags |= QSENT;
1445655518ecSeric 			e->e_nsent++;
1446df106f0bSeric 			if (e->e_receiptto != NULL &&
1447df106f0bSeric 			    bitnset(M_LOCALMAILER, m->m_flags))
1448df106f0bSeric 			{
1449df106f0bSeric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1450df106f0bSeric 					to->q_paddr);
1451df106f0bSeric 			}
1452655518ecSeric 		}
1453b31e7f2bSeric 	}
1454b31e7f2bSeric 
1455b31e7f2bSeric 	/*
1456b31e7f2bSeric 	**  Restore state and return.
1457b31e7f2bSeric 	*/
1458c77d1c25Seric 
14597febfd66Seric #ifdef XDEBUG
14607febfd66Seric 	{
14617febfd66Seric 		char wbuf[MAXLINE];
14627febfd66Seric 
14637febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
146444622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
146544622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
146644622ea3Seric 			m->m_name);
14677febfd66Seric 		checkfd012(wbuf);
14687febfd66Seric 	}
14697febfd66Seric #endif
14707febfd66Seric 
147135490626Seric 	errno = 0;
1472588cad61Seric 	define('g', (char *) NULL, e);
14735826d9d3Seric 	return (rcode);
147425a99e2eSeric }
14755dfc646bSeric /*
147683b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
147783b7ddc9Seric **
147883b7ddc9Seric **	Parameters:
147983b7ddc9Seric **		e -- the envelope we are sending.
148083b7ddc9Seric **		q -- the address to mark.
148183b7ddc9Seric **		rcode -- the code signifying the particular failure.
148283b7ddc9Seric **
148383b7ddc9Seric **	Returns:
148483b7ddc9Seric **		none.
148583b7ddc9Seric **
148683b7ddc9Seric **	Side Effects:
148783b7ddc9Seric **		marks the address (and possibly the envelope) with the
148883b7ddc9Seric **			failure so that an error will be returned or
148983b7ddc9Seric **			the message will be queued, as appropriate.
149083b7ddc9Seric */
149183b7ddc9Seric 
149283b7ddc9Seric markfailure(e, q, rcode)
149383b7ddc9Seric 	register ENVELOPE *e;
149483b7ddc9Seric 	register ADDRESS *q;
149583b7ddc9Seric 	int rcode;
149683b7ddc9Seric {
149719c47125Seric 	char buf[MAXLINE];
149819c47125Seric 
1499d5aafa3cSeric 	switch (rcode)
1500d5aafa3cSeric 	{
1501d5aafa3cSeric 	  case EX_OK:
1502d5aafa3cSeric 		break;
1503d5aafa3cSeric 
1504d5aafa3cSeric 	  case EX_TEMPFAIL:
1505d5aafa3cSeric 	  case EX_IOERR:
1506d5aafa3cSeric 	  case EX_OSERR:
150783b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1508d5aafa3cSeric 		break;
1509d5aafa3cSeric 
1510d5aafa3cSeric 	  default:
1511f170942cSeric 		q->q_flags |= QBADADDR;
1512d5aafa3cSeric 		break;
1513d5aafa3cSeric 	}
151483b7ddc9Seric }
151583b7ddc9Seric /*
1516c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1517c579ef51Seric **
1518c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1519c579ef51Seric **	violation), so we report those specially.  For other
1520c579ef51Seric **	errors, we choose a status message (into statmsg),
1521c579ef51Seric **	and if it represents an error, we print it.
1522c579ef51Seric **
1523c579ef51Seric **	Parameters:
1524c579ef51Seric **		pid -- pid of mailer.
1525c9be6216Seric **		e -- the current envelope.
1526c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1527c9be6216Seric **			(for error messages).
1528c579ef51Seric **
1529c579ef51Seric **	Returns:
1530c579ef51Seric **		exit code of mailer.
1531c579ef51Seric **
1532c579ef51Seric **	Side Effects:
1533c579ef51Seric **		none.
1534c579ef51Seric */
1535c579ef51Seric 
1536c9be6216Seric endmailer(mci, e, pv)
1537b31e7f2bSeric 	register MCI *mci;
1538c9be6216Seric 	register ENVELOPE *e;
1539c9be6216Seric 	char **pv;
1540c579ef51Seric {
1541588cad61Seric 	int st;
1542c579ef51Seric 
154375889e88Seric 	/* close any connections */
154475889e88Seric 	if (mci->mci_in != NULL)
154576f3d53fSeric 		(void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in");
154675889e88Seric 	if (mci->mci_out != NULL)
154776f3d53fSeric 		(void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out");
154875889e88Seric 	mci->mci_in = mci->mci_out = NULL;
154975889e88Seric 	mci->mci_state = MCIS_CLOSED;
155075889e88Seric 
155133db8731Seric 	/* in the IPC case there is nothing to wait for */
155275889e88Seric 	if (mci->mci_pid == 0)
155333db8731Seric 		return (EX_OK);
155433db8731Seric 
155533db8731Seric 	/* wait for the mailer process to die and collect status */
155675889e88Seric 	st = waitfor(mci->mci_pid);
1557588cad61Seric 	if (st == -1)
155878de67c1Seric 	{
1559c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1560588cad61Seric 		return (EX_SOFTWARE);
1561c579ef51Seric 	}
156233db8731Seric 
1563bf9bc890Seric 	if (WIFEXITED(st))
1564c579ef51Seric 	{
1565bf9bc890Seric 		/* normal death -- return status */
1566bf9bc890Seric 		return (WEXITSTATUS(st));
1567bf9bc890Seric 	}
1568bf9bc890Seric 
1569bf9bc890Seric 	/* it died a horrid death */
1570550092c3Seric 	syserr("451 mailer %s died with signal %o",
1571550092c3Seric 		mci->mci_mailer->m_name, st);
1572c9be6216Seric 
1573c9be6216Seric 	/* log the arguments */
15743c930db3Seric 	if (pv != NULL && e->e_xfp != NULL)
1575c9be6216Seric 	{
1576c9be6216Seric 		register char **av;
1577c9be6216Seric 
1578c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1579c9be6216Seric 		for (av = pv; *av != NULL; av++)
1580c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1581c9be6216Seric 		fprintf(e->e_xfp, "\n");
1582c9be6216Seric 	}
1583c9be6216Seric 
15845f73204aSeric 	ExitStat = EX_TEMPFAIL;
15855f73204aSeric 	return (EX_TEMPFAIL);
1586c579ef51Seric }
1587c579ef51Seric /*
158825a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
158925a99e2eSeric **
159025a99e2eSeric **	Parameters:
159125a99e2eSeric **		stat -- the status code from the mailer (high byte
159225a99e2eSeric **			only; core dumps must have been taken care of
159325a99e2eSeric **			already).
159481161401Seric **		m -- the mailer info for this mailer.
159581161401Seric **		mci -- the mailer connection info -- can be NULL if the
159681161401Seric **			response is given before the connection is made.
15974dee0003Seric **		ctladdr -- the controlling address for the recipient
15984dee0003Seric **			address(es).
159981161401Seric **		e -- the current envelope.
160025a99e2eSeric **
160125a99e2eSeric **	Returns:
1602db8841e9Seric **		none.
160325a99e2eSeric **
160425a99e2eSeric **	Side Effects:
1605c1f9df2cSeric **		Errors may be incremented.
160625a99e2eSeric **		ExitStat may be set.
160725a99e2eSeric */
160825a99e2eSeric 
16094dee0003Seric giveresponse(stat, m, mci, ctladdr, e)
161025a99e2eSeric 	int stat;
1611588cad61Seric 	register MAILER *m;
161281161401Seric 	register MCI *mci;
16134dee0003Seric 	ADDRESS *ctladdr;
1614198d9be0Seric 	ENVELOPE *e;
161525a99e2eSeric {
16169c16475dSeric 	register const char *statmsg;
161725a99e2eSeric 	extern char *SysExMsg[];
161825a99e2eSeric 	register int i;
1619d4bd8f0eSbostic 	extern int N_SysEx;
1620198d9be0Seric 	char buf[MAXLINE];
162125a99e2eSeric 
162213bbc08cSeric 	/*
162313bbc08cSeric 	**  Compute status message from code.
162413bbc08cSeric 	*/
162513bbc08cSeric 
162625a99e2eSeric 	i = stat - EX__BASE;
1627588cad61Seric 	if (stat == 0)
16286fe8c3bcSeric 	{
1629588cad61Seric 		statmsg = "250 Sent";
1630ce5531bdSeric 		if (e->e_statmsg != NULL)
16316fe8c3bcSeric 		{
1632ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
16336fe8c3bcSeric 			statmsg = buf;
16346fe8c3bcSeric 		}
16356fe8c3bcSeric 	}
1636588cad61Seric 	else if (i < 0 || i > N_SysEx)
1637588cad61Seric 	{
1638588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1639588cad61Seric 		stat = EX_UNAVAILABLE;
1640588cad61Seric 		statmsg = buf;
1641588cad61Seric 	}
1642198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1643198d9be0Seric 	{
16447d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1645d4bd8f0eSbostic #ifdef NAMED_BIND
1646f28da541Smiriam 		if (h_errno == TRY_AGAIN)
16474d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1648f28da541Smiriam 		else
1649d4bd8f0eSbostic #endif
1650f28da541Smiriam 		{
16518557d168Seric 			if (errno != 0)
1652d87e85f3Seric 				statmsg = errstring(errno);
1653d87e85f3Seric 			else
1654d87e85f3Seric 			{
1655d87e85f3Seric #ifdef SMTP
1656d87e85f3Seric 				statmsg = SmtpError;
16576c2c3107Seric #else /* SMTP */
1658d87e85f3Seric 				statmsg = NULL;
16596c2c3107Seric #endif /* SMTP */
1660d87e85f3Seric 			}
1661f28da541Smiriam 		}
1662d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1663d87e85f3Seric 		{
166487c9b3e7Seric 			(void) strcat(buf, ": ");
1665d87e85f3Seric 			(void) strcat(buf, statmsg);
16668557d168Seric 		}
1667198d9be0Seric 		statmsg = buf;
1668198d9be0Seric 	}
1669f170942cSeric #ifdef NAMED_BIND
1670f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1671f170942cSeric 	{
16724d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1673f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1674f170942cSeric 		statmsg = buf;
1675f170942cSeric 	}
1676f170942cSeric #endif
167725a99e2eSeric 	else
1678d87e85f3Seric 	{
167925a99e2eSeric 		statmsg = SysExMsg[i];
16807d55540cSeric 		if (*statmsg++ == ':')
16817d55540cSeric 		{
16827d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16837d55540cSeric 			statmsg = buf;
16847d55540cSeric 		}
1685d87e85f3Seric 	}
1686588cad61Seric 
1687588cad61Seric 	/*
1688588cad61Seric 	**  Print the message as appropriate
1689588cad61Seric 	*/
1690588cad61Seric 
1691198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1692df106f0bSeric 	{
1693df106f0bSeric 		extern char MsgBuf[];
1694df106f0bSeric 
1695e12d03eeSeric 		message(&statmsg[4], errstring(errno));
1696df106f0bSeric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1697df106f0bSeric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1698df106f0bSeric 	}
169925a99e2eSeric 	else
170025a99e2eSeric 	{
1701c1f9df2cSeric 		Errors++;
1702e12d03eeSeric 		usrerr(statmsg, errstring(errno));
170325a99e2eSeric 	}
170425a99e2eSeric 
170525a99e2eSeric 	/*
170625a99e2eSeric 	**  Final cleanup.
170725a99e2eSeric 	**	Log a record of the transaction.  Compute the new
170825a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
170925a99e2eSeric 	**	that.
171025a99e2eSeric 	*/
171125a99e2eSeric 
17122f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
17134dee0003Seric 		logdelivery(m, mci, &statmsg[4], ctladdr, e);
1714eb238f8cSeric 
1715eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1716eb238f8cSeric 		setstat(stat);
17171097d4c9Seric 	if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL))
1718198d9be0Seric 	{
1719198d9be0Seric 		if (e->e_message != NULL)
1720198d9be0Seric 			free(e->e_message);
1721198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1722198d9be0Seric 	}
17238557d168Seric 	errno = 0;
1724d4bd8f0eSbostic #ifdef NAMED_BIND
1725f28da541Smiriam 	h_errno = 0;
1726d4bd8f0eSbostic #endif
1727eb238f8cSeric }
1728eb238f8cSeric /*
1729eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1730eb238f8cSeric **
17312c9b4f99Seric **	Care is taken to avoid logging lines that are too long, because
17322c9b4f99Seric **	some versions of syslog have an unfortunate proclivity for core
17332c9b4f99Seric **	dumping.  This is a hack, to be sure, that is at best empirical.
17342c9b4f99Seric **
1735eb238f8cSeric **	Parameters:
173681161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
173781161401Seric **		mci -- the mailer connection info -- can be NULL if the
173881161401Seric **			log is occuring when no connection is active.
173981161401Seric **		stat -- the message to print for the status.
17404dee0003Seric **		ctladdr -- the controlling address for the to list.
174181161401Seric **		e -- the current envelope.
1742eb238f8cSeric **
1743eb238f8cSeric **	Returns:
1744eb238f8cSeric **		none
1745eb238f8cSeric **
1746eb238f8cSeric **	Side Effects:
1747eb238f8cSeric **		none
1748eb238f8cSeric */
1749eb238f8cSeric 
17504dee0003Seric logdelivery(m, mci, stat, ctladdr, e)
175181161401Seric 	MAILER *m;
175281161401Seric 	register MCI *mci;
1753eb238f8cSeric 	char *stat;
17544dee0003Seric 	ADDRESS *ctladdr;
1755b31e7f2bSeric 	register ENVELOPE *e;
17565cf56be3Seric {
1757eb238f8cSeric # ifdef LOG
17584dee0003Seric 	register char *bp;
17592c9b4f99Seric 	register char *p;
17602c9b4f99Seric 	int l;
1761d6acf3eeSeric 	char buf[512];
17629507d1f9Seric 
17633a100e8bSeric #  if (SYSLOG_BUFSIZE) >= 256
17644dee0003Seric 	bp = buf;
17654dee0003Seric 	if (ctladdr != NULL)
17664dee0003Seric 	{
17674dee0003Seric 		strcpy(bp, ", ctladdr=");
1768f62f08c7Seric 		strcat(bp, shortenstring(ctladdr->q_paddr, 83));
17694dee0003Seric 		bp += strlen(bp);
17704dee0003Seric 		if (bitset(QGOODUID, ctladdr->q_flags))
17714dee0003Seric 		{
17724dee0003Seric 			(void) sprintf(bp, " (%d/%d)",
17734dee0003Seric 					ctladdr->q_uid, ctladdr->q_gid);
17744dee0003Seric 			bp += strlen(bp);
17754dee0003Seric 		}
17764dee0003Seric 	}
17774dee0003Seric 
17784dee0003Seric 	(void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
17794dee0003Seric 	bp += strlen(bp);
178081161401Seric 
178148ed5d33Seric 	if (m != NULL)
178271ff6caaSeric 	{
17834dee0003Seric 		(void) strcpy(bp, ", mailer=");
17844dee0003Seric 		(void) strcat(bp, m->m_name);
17854dee0003Seric 		bp += strlen(bp);
178671ff6caaSeric 	}
178748ed5d33Seric 
178848ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
178971ff6caaSeric 	{
179071ff6caaSeric # ifdef DAEMON
1791e2f2f828Seric 		extern SOCKADDR CurHostAddr;
179248ed5d33Seric # endif
179371ff6caaSeric 
17944dee0003Seric 		(void) strcpy(bp, ", relay=");
17954dee0003Seric 		(void) strcat(bp, mci->mci_host);
179648ed5d33Seric 
179748ed5d33Seric # ifdef DAEMON
17984dee0003Seric 		(void) strcat(bp, " (");
17994dee0003Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
18004dee0003Seric 		(void) strcat(bp, ")");
180171ff6caaSeric # endif
180271ff6caaSeric 	}
18039507d1f9Seric 	else
180448ed5d33Seric 	{
180548ed5d33Seric 		char *p = macvalue('h', e);
18069507d1f9Seric 
180748ed5d33Seric 		if (p != NULL && p[0] != '\0')
180848ed5d33Seric 		{
18094dee0003Seric 			(void) strcpy(bp, ", relay=");
18104dee0003Seric 			(void) strcat(bp, p);
181148ed5d33Seric 		}
181248ed5d33Seric 	}
1813cb082c5bSeric 	bp += strlen(bp);
1814d6acf3eeSeric 
18153a100e8bSeric #define STATLEN		(((SYSLOG_BUFSIZE) - 100) / 4)
18163a100e8bSeric #if (STATLEN) < 63
18173a100e8bSeric # undef STATLEN
18183a100e8bSeric # define STATLEN	63
18193a100e8bSeric #endif
18203a100e8bSeric #if (STATLEN) > 203
18213a100e8bSeric # undef STATLEN
18223a100e8bSeric # define STATLEN	203
18233a100e8bSeric #endif
18243a100e8bSeric 
18253a100e8bSeric 	if ((bp - buf) > (sizeof buf - ((STATLEN) + 20)))
18262c9b4f99Seric 	{
18272c9b4f99Seric 		/* desperation move -- truncate data */
18283a100e8bSeric 		bp = buf + sizeof buf - ((STATLEN) + 17);
18292c9b4f99Seric 		strcpy(bp, "...");
18302c9b4f99Seric 		bp += 3;
18312c9b4f99Seric 	}
18322c9b4f99Seric 
18332c9b4f99Seric 	(void) strcpy(bp, ", stat=");
18342c9b4f99Seric 	bp += strlen(bp);
18353a100e8bSeric 
18363a100e8bSeric 	(void) strcpy(bp, shortenstring(stat, (STATLEN)));
18372c9b4f99Seric 
18382c9b4f99Seric 	l = SYSLOG_BUFSIZE - 100 - strlen(buf);
18392c9b4f99Seric 	p = e->e_to;
18402c9b4f99Seric 	while (strlen(p) >= l)
18412c9b4f99Seric 	{
18422c9b4f99Seric 		register char *q = strchr(p + l, ',');
18432c9b4f99Seric 
18442a1b6b73Seric 		if (q == NULL)
18452c9b4f99Seric 			break;
18462c9b4f99Seric 		syslog(LOG_INFO, "%s: to=%.*s [more]%s",
18472c9b4f99Seric 			e->e_id, ++q - p, p, buf);
18482c9b4f99Seric 		p = q;
18492c9b4f99Seric 	}
18502c9b4f99Seric 	syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf);
18513a100e8bSeric 
18523a100e8bSeric #  else		/* we have a very short log buffer size */
18533a100e8bSeric 
18544e797715Seric 	l = SYSLOG_BUFSIZE - 80;
18553a100e8bSeric 	p = e->e_to;
18563a100e8bSeric 	while (strlen(p) >= l)
18573a100e8bSeric 	{
18583a100e8bSeric 		register char *q = strchr(p + l, ',');
18593a100e8bSeric 
18603a100e8bSeric 		if (q == NULL)
18613a100e8bSeric 			break;
18623a100e8bSeric 		syslog(LOG_INFO, "%s: to=%.*s [more]",
18633a100e8bSeric 			e->e_id, ++q - p, p);
18643a100e8bSeric 		p = q;
18653a100e8bSeric 	}
18663a100e8bSeric 	syslog(LOG_INFO, "%s: to=%s", e->e_id, p);
18673a100e8bSeric 
18683a100e8bSeric 	if (ctladdr != NULL)
18693a100e8bSeric 	{
18703a100e8bSeric 		bp = buf;
18713a100e8bSeric 		strcpy(buf, "ctladdr=");
18723a100e8bSeric 		bp += strlen(buf);
18733a100e8bSeric 		strcpy(bp, shortenstring(ctladdr->q_paddr, 83));
18743a100e8bSeric 		bp += strlen(buf);
18753a100e8bSeric 		if (bitset(QGOODUID, ctladdr->q_flags))
18763a100e8bSeric 		{
18773a100e8bSeric 			(void) sprintf(bp, " (%d/%d)",
18783a100e8bSeric 					ctladdr->q_uid, ctladdr->q_gid);
18793a100e8bSeric 			bp += strlen(bp);
18803a100e8bSeric 		}
18813a100e8bSeric 		syslog(LOG_INFO, "%s: %s", e->e_id, buf);
18823a100e8bSeric 	}
18834e797715Seric 	bp = buf;
18844e797715Seric 	sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
18854e797715Seric 	bp += strlen(bp);
18863a100e8bSeric 
18873a100e8bSeric 	if (m != NULL)
18884e797715Seric 	{
18894e797715Seric 		sprintf(bp, ", mailer=%s", m->m_name);
18904e797715Seric 		bp += strlen(bp);
18914e797715Seric 	}
18923a100e8bSeric 
18933a100e8bSeric 	if (mci != NULL && mci->mci_host != NULL)
18943a100e8bSeric 	{
18953a100e8bSeric # ifdef DAEMON
18963a100e8bSeric 		extern SOCKADDR CurHostAddr;
18973a100e8bSeric # endif
18983a100e8bSeric 
18994e797715Seric 		sprintf(bp, ", relay=%s", mci->mci_host);
19003a100e8bSeric 
19013a100e8bSeric # ifdef DAEMON
19024e797715Seric 		(void) strcat(bp, " (");
19034e797715Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
19044e797715Seric 		(void) strcat(bp, ")");
19053a100e8bSeric # endif
19063a100e8bSeric 	}
19073a100e8bSeric 	else
19083a100e8bSeric 	{
19093a100e8bSeric 		char *p = macvalue('h', e);
19103a100e8bSeric 
19113a100e8bSeric 		if (p != NULL && p[0] != '\0')
19124e797715Seric 			sprintf(bp, ", relay=%s", p);
19133a100e8bSeric 	}
19144e797715Seric 	syslog(LOG_INFO, "%s: %s", e->e_id, buf);
19153a100e8bSeric 
19163a100e8bSeric 	syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63));
19173a100e8bSeric #  endif /* short log buffer */
19186c2c3107Seric # endif /* LOG */
191925a99e2eSeric }
192025a99e2eSeric /*
192151552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
192225a99e2eSeric **
192351552439Seric **	This can be made an arbitrary message separator by changing $l
192451552439Seric **
19259b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
19269b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
19279b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
19289b6c17a6Seric **	this kind of antique garbage????
192925a99e2eSeric **
193025a99e2eSeric **	Parameters:
193151552439Seric **		fp -- the file to output to.
193251552439Seric **		m -- the mailer describing this entry.
193325a99e2eSeric **
193425a99e2eSeric **	Returns:
193551552439Seric **		none
193625a99e2eSeric **
193725a99e2eSeric **	Side Effects:
193851552439Seric **		outputs some text to fp.
193925a99e2eSeric */
194025a99e2eSeric 
1941b31e7f2bSeric putfromline(fp, m, e)
194251552439Seric 	register FILE *fp;
194351552439Seric 	register MAILER *m;
1944b31e7f2bSeric 	ENVELOPE *e;
194525a99e2eSeric {
19462bc47524Seric 	char *template = "\201l\n";
194751552439Seric 	char buf[MAXLINE];
194825a99e2eSeric 
194957fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
195051552439Seric 		return;
195113bbc08cSeric 
19522c7e1b8dSeric # ifdef UGLYUUCP
195357fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
195474b6e67bSeric 	{
1955ea09d6edSeric 		char *bang;
1956ea09d6edSeric 		char xbuf[MAXLINE];
195774b6e67bSeric 
1958ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
19596c2c3107Seric 		bang = strchr(buf, '!');
196074b6e67bSeric 		if (bang == NULL)
196134fcca25Seric 		{
196234fcca25Seric 			errno = 0;
196334fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
196434fcca25Seric 		}
196574b6e67bSeric 		else
1966588cad61Seric 		{
1967ea09d6edSeric 			*bang++ = '\0';
19682bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1969ea09d6edSeric 			template = xbuf;
197074b6e67bSeric 		}
1971588cad61Seric 	}
19726c2c3107Seric # endif /* UGLYUUCP */
1973b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
197477b52738Seric 	putline(buf, fp, m);
1975bc6e2962Seric }
1976bc6e2962Seric /*
197751552439Seric **  PUTBODY -- put the body of a message.
197851552439Seric **
197951552439Seric **	Parameters:
198051552439Seric **		fp -- file to output onto.
198177b52738Seric **		m -- a mailer descriptor to control output format.
19829a6a5f55Seric **		e -- the envelope to put out.
198303c02fdeSeric **		separator -- if non-NULL, a message separator that must
198403c02fdeSeric **			not be permitted in the resulting message.
198551552439Seric **
198651552439Seric **	Returns:
198751552439Seric **		none.
198851552439Seric **
198951552439Seric **	Side Effects:
199051552439Seric **		The message is written onto fp.
199151552439Seric */
199251552439Seric 
199303c02fdeSeric putbody(fp, m, e, separator)
199451552439Seric 	FILE *fp;
1995588cad61Seric 	MAILER *m;
19969a6a5f55Seric 	register ENVELOPE *e;
199703c02fdeSeric 	char *separator;
199851552439Seric {
199977b52738Seric 	char buf[MAXLINE];
200051552439Seric 
200151552439Seric 	/*
200251552439Seric 	**  Output the body of the message
200351552439Seric 	*/
200451552439Seric 
20059a6a5f55Seric 	if (e->e_dfp == NULL)
200651552439Seric 	{
20079a6a5f55Seric 		if (e->e_df != NULL)
20089a6a5f55Seric 		{
20099a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
20109a6a5f55Seric 			if (e->e_dfp == NULL)
20118f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
2012e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
20139a6a5f55Seric 		}
20149a6a5f55Seric 		else
201577b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
20169a6a5f55Seric 	}
20179a6a5f55Seric 	if (e->e_dfp != NULL)
20189a6a5f55Seric 	{
20199a6a5f55Seric 		rewind(e->e_dfp);
202077b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
202124fc8aeeSeric 		{
202224fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
2023d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
20243462ad9eSeric 				(void) putc('>', fp);
202503c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
202603c02fdeSeric 			{
202703c02fdeSeric 				/* possible separator */
202803c02fdeSeric 				int sl = strlen(separator);
202903c02fdeSeric 
203003c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
203103c02fdeSeric 					(void) putc(' ', fp);
203203c02fdeSeric 			}
203377b52738Seric 			putline(buf, fp, m);
203424fc8aeeSeric 		}
203551552439Seric 
20369a6a5f55Seric 		if (ferror(e->e_dfp))
203751552439Seric 		{
2038df106f0bSeric 			syserr("putbody: %s: read error", e->e_df);
203951552439Seric 			ExitStat = EX_IOERR;
204051552439Seric 		}
204151552439Seric 	}
204251552439Seric 
20430890ba1fSeric 	/* some mailers want extra blank line at end of message */
20440890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
20450890ba1fSeric 		putline("", fp, m);
20460890ba1fSeric 
204751552439Seric 	(void) fflush(fp);
204851552439Seric 	if (ferror(fp) && errno != EPIPE)
204951552439Seric 	{
205051552439Seric 		syserr("putbody: write error");
205151552439Seric 		ExitStat = EX_IOERR;
205251552439Seric 	}
205351552439Seric 	errno = 0;
205425a99e2eSeric }
205525a99e2eSeric /*
205625a99e2eSeric **  MAILFILE -- Send a message to a file.
205725a99e2eSeric **
2058f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
2059f129ec7dSeric **	bits, sendmail will try to become the owner of that file
2060f129ec7dSeric **	rather than the real user.  Obviously, this only works if
2061f129ec7dSeric **	sendmail runs as root.
2062f129ec7dSeric **
2063588cad61Seric **	This could be done as a subordinate mailer, except that it
2064588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
2065588cad61Seric **	view this as being sufficiently important as to include it
2066588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
2067588cad61Seric **	to create another process plus some pipes to save the message.
2068588cad61Seric **
206925a99e2eSeric **	Parameters:
207025a99e2eSeric **		filename -- the name of the file to send to.
20716259796dSeric **		ctladdr -- the controlling address header -- includes
20726259796dSeric **			the userid/groupid to be when sending.
207325a99e2eSeric **
207425a99e2eSeric **	Returns:
207525a99e2eSeric **		The exit code associated with the operation.
207625a99e2eSeric **
207725a99e2eSeric **	Side Effects:
207825a99e2eSeric **		none.
207925a99e2eSeric */
208025a99e2eSeric 
2081b31e7f2bSeric mailfile(filename, ctladdr, e)
208225a99e2eSeric 	char *filename;
20836259796dSeric 	ADDRESS *ctladdr;
2084b31e7f2bSeric 	register ENVELOPE *e;
208525a99e2eSeric {
208625a99e2eSeric 	register FILE *f;
208732d19d43Seric 	register int pid;
208815d084d5Seric 	int mode;
208925a99e2eSeric 
2090671745f3Seric 	if (tTd(11, 1))
2091671745f3Seric 	{
2092671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
2093671745f3Seric 		printaddr(ctladdr, FALSE);
2094671745f3Seric 	}
2095671745f3Seric 
2096f170942cSeric 	if (e->e_xfp != NULL)
2097f170942cSeric 		fflush(e->e_xfp);
2098f170942cSeric 
209932d19d43Seric 	/*
210032d19d43Seric 	**  Fork so we can change permissions here.
210132d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
210232d19d43Seric 	**	the complications of calling subroutines, etc.
210332d19d43Seric 	*/
210432d19d43Seric 
210532d19d43Seric 	DOFORK(fork);
210632d19d43Seric 
210732d19d43Seric 	if (pid < 0)
210832d19d43Seric 		return (EX_OSERR);
210932d19d43Seric 	else if (pid == 0)
211032d19d43Seric 	{
211132d19d43Seric 		/* child -- actually write to file */
2112f129ec7dSeric 		struct stat stb;
2113f129ec7dSeric 
21142b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
21152b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
21162b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
21173462ad9eSeric 		(void) umask(OldUmask);
211895f16dc0Seric 
2119f129ec7dSeric 		if (stat(filename, &stb) < 0)
21203a98e7eaSeric 			stb.st_mode = FileMode;
212115d084d5Seric 		mode = stb.st_mode;
212295f16dc0Seric 
212395f16dc0Seric 		/* limit the errors to those actually caused in the child */
212495f16dc0Seric 		errno = 0;
212595f16dc0Seric 		ExitStat = EX_OK;
212695f16dc0Seric 
2127f129ec7dSeric 		if (bitset(0111, stb.st_mode))
2128f129ec7dSeric 			exit(EX_CANTCREAT);
212919428781Seric 		if (ctladdr != NULL)
213015d084d5Seric 		{
213115d084d5Seric 			/* ignore setuid and setgid bits */
213215d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
213315d084d5Seric 		}
213415d084d5Seric 
21358f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
21368f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
21378f9146b0Srick 		{
21388f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
213995f16dc0Seric 			if (e->e_dfp == NULL)
214095f16dc0Seric 			{
21418f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
2142e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
21438f9146b0Srick 			}
21448f9146b0Srick 		}
21458f9146b0Srick 
214615d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
2147e36b99e2Seric 		{
214819428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
214995f16dc0Seric 			{
2150898a126bSbostic 				(void) initgroups(DefUser, DefGid);
215195f16dc0Seric 			}
215295f16dc0Seric 			else
215395f16dc0Seric 			{
2154898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
2155898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
2156898a126bSbostic 					ctladdr->q_gid);
2157898a126bSbostic 			}
2158e36b99e2Seric 		}
215915d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
2160e36b99e2Seric 		{
216119428781Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
2162e36b99e2Seric 				(void) setuid(DefUid);
2163e36b99e2Seric 			else
21646259796dSeric 				(void) setuid(ctladdr->q_uid);
2165e36b99e2Seric 		}
216695f16dc0Seric 		FileName = filename;
216795f16dc0Seric 		LineNumber = 0;
21683a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
216925a99e2eSeric 		if (f == NULL)
217095f16dc0Seric 		{
2171b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
217232d19d43Seric 			exit(EX_CANTCREAT);
217395f16dc0Seric 		}
217425a99e2eSeric 
21750331ce05Seric 		putfromline(f, FileMailer, e);
21760331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
21770331ce05Seric 		putline("\n", f, FileMailer);
217803c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
21790331ce05Seric 		putline("\n", f, FileMailer);
218095f16dc0Seric 		if (ferror(f))
218195f16dc0Seric 		{
2182b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
218395f16dc0Seric 			setstat(EX_IOERR);
218495f16dc0Seric 		}
2185ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
218632d19d43Seric 		(void) fflush(stdout);
2187e36b99e2Seric 
218827628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
2189c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
219095f16dc0Seric 		exit(ExitStat);
219113bbc08cSeric 		/*NOTREACHED*/
219232d19d43Seric 	}
219332d19d43Seric 	else
219432d19d43Seric 	{
219532d19d43Seric 		/* parent -- wait for exit status */
2196588cad61Seric 		int st;
219732d19d43Seric 
2198588cad61Seric 		st = waitfor(pid);
2199bf9bc890Seric 		if (WIFEXITED(st))
2200bf9bc890Seric 			return (WEXITSTATUS(st));
2201588cad61Seric 		else
2202b6a0de9dSeric 		{
2203b6a0de9dSeric 			syserr("child died on signal %d", st);
2204bf9bc890Seric 			return (EX_UNAVAILABLE);
2205b6a0de9dSeric 		}
22068f9146b0Srick 		/*NOTREACHED*/
220732d19d43Seric 	}
220825a99e2eSeric }
2209ea4dc939Seric /*
2210e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2211e103b48fSeric **
2212e103b48fSeric **	The signature describes how we are going to send this -- it
2213e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2214e103b48fSeric **	an ordered list of MX hosts.
2215e103b48fSeric **
2216e103b48fSeric **	Parameters:
2217e103b48fSeric **		m -- the mailer describing this host.
2218e103b48fSeric **		host -- the host name.
2219e103b48fSeric **		e -- the current envelope.
2220e103b48fSeric **
2221e103b48fSeric **	Returns:
2222e103b48fSeric **		The signature for this host.
2223e103b48fSeric **
2224e103b48fSeric **	Side Effects:
2225e103b48fSeric **		Can tweak the symbol table.
2226e103b48fSeric */
2227e103b48fSeric 
2228e103b48fSeric char *
2229e103b48fSeric hostsignature(m, host, e)
2230e103b48fSeric 	register MAILER *m;
2231e103b48fSeric 	char *host;
2232e103b48fSeric 	ENVELOPE *e;
2233e103b48fSeric {
2234e103b48fSeric 	register char *p;
2235e103b48fSeric 	register STAB *s;
2236e103b48fSeric 	int i;
2237e103b48fSeric 	int len;
2238e103b48fSeric #ifdef NAMED_BIND
2239e103b48fSeric 	int nmx;
2240e103b48fSeric 	auto int rcode;
2241bafdc4e5Seric 	char *hp;
2242bafdc4e5Seric 	char *endp;
2243516782b4Seric 	int oldoptions;
2244e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2245e103b48fSeric #endif
2246e103b48fSeric 
2247e103b48fSeric 	/*
2248e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2249e103b48fSeric 	*/
2250e103b48fSeric 
2251e103b48fSeric 	p = m->m_mailer;
2252e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2253e103b48fSeric 	{
2254e103b48fSeric 		/* just an ordinary mailer */
2255e103b48fSeric 		return host;
2256e103b48fSeric 	}
2257e103b48fSeric 
2258e103b48fSeric 	/*
2259e103b48fSeric 	**  Look it up in the symbol table.
2260e103b48fSeric 	*/
2261e103b48fSeric 
2262e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2263e103b48fSeric 	if (s->s_hostsig != NULL)
2264e103b48fSeric 		return s->s_hostsig;
2265e103b48fSeric 
2266e103b48fSeric 	/*
2267e103b48fSeric 	**  Not already there -- create a signature.
2268e103b48fSeric 	*/
2269e103b48fSeric 
2270e103b48fSeric #ifdef NAMED_BIND
2271516782b4Seric 	if (ConfigLevel < 2)
2272516782b4Seric 	{
2273516782b4Seric 		oldoptions = _res.options;
2274516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2275516782b4Seric 	}
2276516782b4Seric 
2277bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2278bafdc4e5Seric 	{
2279bafdc4e5Seric 		endp = strchr(hp, ':');
2280bafdc4e5Seric 		if (endp != NULL)
2281bafdc4e5Seric 			*endp = '\0';
2282bafdc4e5Seric 
22837bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
22847d55540cSeric 
2285e103b48fSeric 		if (nmx <= 0)
2286e103b48fSeric 		{
2287e103b48fSeric 			register MCI *mci;
2288e103b48fSeric 
2289e103b48fSeric 			/* update the connection info for this host */
2290bafdc4e5Seric 			mci = mci_get(hp, m);
2291e103b48fSeric 			mci->mci_exitstat = rcode;
2292e103b48fSeric 			mci->mci_errno = errno;
2293f170942cSeric #ifdef NAMED_BIND
2294f170942cSeric 			mci->mci_herrno = h_errno;
2295f170942cSeric #endif
2296e103b48fSeric 
2297e103b48fSeric 			/* and return the original host name as the signature */
2298bafdc4e5Seric 			nmx = 1;
2299bafdc4e5Seric 			mxhosts[0] = hp;
2300e103b48fSeric 		}
2301e103b48fSeric 
2302e103b48fSeric 		len = 0;
2303e103b48fSeric 		for (i = 0; i < nmx; i++)
2304e103b48fSeric 		{
2305e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2306e103b48fSeric 		}
2307bafdc4e5Seric 		if (s->s_hostsig != NULL)
2308bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2309bafdc4e5Seric 		p = xalloc(len);
2310bafdc4e5Seric 		if (s->s_hostsig != NULL)
2311bafdc4e5Seric 		{
2312bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2313bafdc4e5Seric 			free(s->s_hostsig);
2314bafdc4e5Seric 			s->s_hostsig = p;
2315bafdc4e5Seric 			p += strlen(p);
2316bafdc4e5Seric 			*p++ = ':';
2317bafdc4e5Seric 		}
2318bafdc4e5Seric 		else
2319bafdc4e5Seric 			s->s_hostsig = p;
2320e103b48fSeric 		for (i = 0; i < nmx; i++)
2321e103b48fSeric 		{
2322e103b48fSeric 			if (i != 0)
2323e103b48fSeric 				*p++ = ':';
2324e103b48fSeric 			strcpy(p, mxhosts[i]);
2325e103b48fSeric 			p += strlen(p);
2326e103b48fSeric 		}
2327bafdc4e5Seric 		if (endp != NULL)
2328bafdc4e5Seric 			*endp++ = ':';
2329bafdc4e5Seric 	}
2330e103b48fSeric 	makelower(s->s_hostsig);
2331516782b4Seric 	if (ConfigLevel < 2)
2332516782b4Seric 		_res.options = oldoptions;
2333e103b48fSeric #else
2334e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2335e103b48fSeric 	s->s_hostsig = host;
2336e103b48fSeric #endif
2337e103b48fSeric 	if (tTd(17, 1))
2338e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2339e103b48fSeric 	return s->s_hostsig;
2340e103b48fSeric }
2341