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*df106f0bSeric static char sccsid[] = "@(#)deliver.c	8.31 (Berkeley) 10/15/93";
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 
2325a99e2eSeric /*
249c9e68d9Seric **  SENDALL -- actually send all the messages.
259c9e68d9Seric **
269c9e68d9Seric **	Parameters:
279c9e68d9Seric **		e -- the envelope to send.
289c9e68d9Seric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
299c9e68d9Seric **			the current e->e_sendmode.
309c9e68d9Seric **
319c9e68d9Seric **	Returns:
329c9e68d9Seric **		none.
339c9e68d9Seric **
349c9e68d9Seric **	Side Effects:
359c9e68d9Seric **		Scans the send lists and sends everything it finds.
369c9e68d9Seric **		Delivers any appropriate error messages.
379c9e68d9Seric **		If we are running in a non-interactive mode, takes the
389c9e68d9Seric **			appropriate action.
399c9e68d9Seric */
409c9e68d9Seric 
419c9e68d9Seric sendall(e, mode)
429c9e68d9Seric 	ENVELOPE *e;
439c9e68d9Seric 	char mode;
449c9e68d9Seric {
459c9e68d9Seric 	register ADDRESS *q;
469c9e68d9Seric 	char *owner;
479c9e68d9Seric 	int otherowners;
489c9e68d9Seric 	register ENVELOPE *ee;
499c9e68d9Seric 	ENVELOPE *splitenv = NULL;
506103d9b4Seric 	bool announcequeueup;
519c9e68d9Seric 
527880f3e4Seric 	/*
537880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
547880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
557880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
567880f3e4Seric 	**  addresses to be sent.
577880f3e4Seric 	*/
587880f3e4Seric 
597880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
60896b16f6Seric 	{
61896b16f6Seric 		e->e_flags |= EF_CLRQUEUE;
62896b16f6Seric 		return;
63896b16f6Seric 	}
64896b16f6Seric 
659c9e68d9Seric 	/* determine actual delivery mode */
669c9e68d9Seric 	if (mode == SM_DEFAULT)
679c9e68d9Seric 	{
689c9e68d9Seric 		mode = e->e_sendmode;
699c9e68d9Seric 		if (mode != SM_VERIFY &&
709c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
719c9e68d9Seric 			mode = SM_QUEUE;
726103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
739c9e68d9Seric 	}
746103d9b4Seric 	else
756103d9b4Seric 		announcequeueup = FALSE;
769c9e68d9Seric 
779c9e68d9Seric 	if (tTd(13, 1))
789c9e68d9Seric 	{
790e484fe5Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
800e484fe5Seric 			mode, e->e_id);
819c9e68d9Seric 		printaddr(&e->e_from, FALSE);
829c9e68d9Seric 		printf("sendqueue:\n");
839c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
849c9e68d9Seric 	}
859c9e68d9Seric 
869c9e68d9Seric 	/*
879c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
889c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
899c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
909c9e68d9Seric 	*/
919c9e68d9Seric 
929c9e68d9Seric 	CurEnv = e;
939c9e68d9Seric 
949c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
959c9e68d9Seric 	{
969c9e68d9Seric 		errno = 0;
974bb8b0fdSeric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
989c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
994bb8b0fdSeric 			RealHostName, e->e_sendqueue->q_paddr);
1009c9e68d9Seric 		return;
1019c9e68d9Seric 	}
1029c9e68d9Seric 
103b8004690Seric 	/*
104b8004690Seric 	**  Do sender deletion.
105b8004690Seric 	**
106b8004690Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
107b8004690Seric 	**	This can happen if the name server is hosed when you
108b8004690Seric 	**	are trying to send mail.  The result is that the sender
109b8004690Seric 	**	is instantiated in the queue as a recipient.
110b8004690Seric 	*/
111b8004690Seric 
112e76a6a8fSeric 	if (!bitset(EF_METOO, e->e_flags) &&
113e76a6a8fSeric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
1149c9e68d9Seric 	{
1159c9e68d9Seric 		if (tTd(13, 5))
1169c9e68d9Seric 		{
1179c9e68d9Seric 			printf("sendall: QDONTSEND ");
1189c9e68d9Seric 			printaddr(&e->e_from, FALSE);
1199c9e68d9Seric 		}
1209c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1219c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1229c9e68d9Seric 	}
1239c9e68d9Seric 
124ce5531bdSeric 	/*
125ce5531bdSeric 	**  Handle alias owners.
126ce5531bdSeric 	**
127ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
128ce5531bdSeric 	**	We discard owners that are the same as the return path.
129ce5531bdSeric 	*/
130ce5531bdSeric 
131ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
132ce5531bdSeric 	{
133ce5531bdSeric 		register struct address *a;
134ce5531bdSeric 
135ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
136ce5531bdSeric 			continue;
137ce5531bdSeric 		if (a != NULL)
138ce5531bdSeric 			q->q_owner = a->q_owner;
139ce5531bdSeric 
140ce5531bdSeric 		if (q->q_owner != NULL &&
141ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
142ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
143ce5531bdSeric 			q->q_owner = NULL;
144ce5531bdSeric 	}
145ce5531bdSeric 
146ce5531bdSeric 	owner = "";
147ce5531bdSeric 	otherowners = 1;
148ce5531bdSeric 	while (owner != NULL && otherowners > 0)
149ce5531bdSeric 	{
150ce5531bdSeric 		owner = NULL;
151ce5531bdSeric 		otherowners = 0;
152ce5531bdSeric 
153ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
154ce5531bdSeric 		{
155ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
156ce5531bdSeric 				continue;
157ce5531bdSeric 
158ce5531bdSeric 			if (q->q_owner != NULL)
159ce5531bdSeric 			{
160ce5531bdSeric 				if (owner == NULL)
161ce5531bdSeric 					owner = q->q_owner;
162ce5531bdSeric 				else if (owner != q->q_owner)
163ce5531bdSeric 				{
164ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
165ce5531bdSeric 					{
166ce5531bdSeric 						/* make future comparisons cheap */
167ce5531bdSeric 						q->q_owner = owner;
168ce5531bdSeric 					}
169ce5531bdSeric 					else
170ce5531bdSeric 					{
171ce5531bdSeric 						otherowners++;
172ce5531bdSeric 					}
173ce5531bdSeric 					owner = q->q_owner;
174ce5531bdSeric 				}
175ce5531bdSeric 			}
176ce5531bdSeric 			else
177ce5531bdSeric 			{
178ce5531bdSeric 				otherowners++;
179ce5531bdSeric 			}
180ce5531bdSeric 		}
181ce5531bdSeric 
182ce5531bdSeric 		if (owner != NULL && otherowners > 0)
183ce5531bdSeric 		{
184ce5531bdSeric 			extern HDR *copyheader();
185ce5531bdSeric 			extern ADDRESS *copyqueue();
186ce5531bdSeric 
187ce5531bdSeric 			/*
188ce5531bdSeric 			**  Split this envelope into two.
189ce5531bdSeric 			*/
190ce5531bdSeric 
191ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
192ce5531bdSeric 			*ee = *e;
193ce5531bdSeric 			ee->e_id = NULL;
194ce5531bdSeric 			(void) queuename(ee, '\0');
195ce5531bdSeric 
196ce5531bdSeric 			if (tTd(13, 1))
197ce5531bdSeric 				printf("sendall: split %s into %s\n",
198ce5531bdSeric 					e->e_id, ee->e_id);
199ce5531bdSeric 
200ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
201ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
202ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
203ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
204ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
205ce5531bdSeric 			if (tTd(13, 5))
206ce5531bdSeric 			{
207ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
208ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
209ce5531bdSeric 			}
210ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
211ce5531bdSeric 			ee->e_dfp = NULL;
212ce5531bdSeric 			ee->e_xfp = NULL;
213ce5531bdSeric 			ee->e_lockfp = NULL;
214ce5531bdSeric 			ee->e_df = NULL;
215ce5531bdSeric 			ee->e_errormode = EM_MAIL;
216ce5531bdSeric 			ee->e_sibling = splitenv;
217ce5531bdSeric 			splitenv = ee;
218ce5531bdSeric 
219ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
220ce5531bdSeric 				if (q->q_owner == owner)
221ce5531bdSeric 					q->q_flags |= QDONTSEND;
222ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
223ce5531bdSeric 				if (q->q_owner != owner)
224ce5531bdSeric 					q->q_flags |= QDONTSEND;
225ce5531bdSeric 
226ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
227ce5531bdSeric 			{
228ce5531bdSeric 				ee->e_dfp = NULL;
229da662164Seric 				ee->e_df = queuename(ee, 'd');
230da662164Seric 				ee->e_df = newstr(ee->e_df);
231ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
232ce5531bdSeric 				{
233ce5531bdSeric 					syserr("sendall: link(%s, %s)",
234ce5531bdSeric 						e->e_df, ee->e_df);
235ce5531bdSeric 				}
236ce5531bdSeric 			}
237ce5531bdSeric 
238ce5531bdSeric 			if (mode != SM_VERIFY)
239ce5531bdSeric 				openxscript(ee);
240ce5531bdSeric #ifdef LOG
241ce5531bdSeric 			if (LogLevel > 4)
242ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
243ce5531bdSeric 					ee->e_id, e->e_id);
244ce5531bdSeric #endif
245ce5531bdSeric 		}
246ce5531bdSeric 	}
247ce5531bdSeric 
248ce5531bdSeric 	if (owner != NULL)
249ce5531bdSeric 	{
250ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
251ce5531bdSeric 		if (tTd(13, 5))
252ce5531bdSeric 		{
253ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
254ce5531bdSeric 			printaddr(&e->e_from, FALSE);
255ce5531bdSeric 		}
256ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
257ce5531bdSeric 		e->e_errormode = EM_MAIL;
258ce5531bdSeric 	}
259ce5531bdSeric 
260c1ac89b1Seric # ifdef QUEUE
261c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
262c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
263c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
264c1ac89b1Seric 	{
265c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2666103d9b4Seric 		queueup(e, TRUE, announcequeueup);
267ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2686103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
269c1ac89b1Seric 	}
270c1ac89b1Seric #endif /* QUEUE */
271c1ac89b1Seric 
272ce5531bdSeric 	if (splitenv != NULL)
273ce5531bdSeric 	{
274ce5531bdSeric 		if (tTd(13, 1))
275ce5531bdSeric 		{
276ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
277ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
278ce5531bdSeric 		}
279ce5531bdSeric 
280ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
281ce5531bdSeric 		{
282ce5531bdSeric 			CurEnv = ee;
283ce5531bdSeric 			sendenvelope(ee, mode);
284ce5531bdSeric 		}
285ce5531bdSeric 
286ce5531bdSeric 		CurEnv = e;
287ce5531bdSeric 	}
288ce5531bdSeric 	sendenvelope(e, mode);
289ce5531bdSeric 
290ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
291ce5531bdSeric 		dropenvelope(splitenv);
292ce5531bdSeric }
293ce5531bdSeric 
294ce5531bdSeric sendenvelope(e, mode)
295ce5531bdSeric 	register ENVELOPE *e;
296ce5531bdSeric 	char mode;
297ce5531bdSeric {
298ce5531bdSeric 	bool oldverbose;
299ce5531bdSeric 	int pid;
300ce5531bdSeric 	register ADDRESS *q;
3016b2765c6Seric 	char *qf;
3026b2765c6Seric 	char *id;
303ce5531bdSeric 
3047880f3e4Seric 	/*
3057880f3e4Seric 	**  If we have had global, fatal errors, don't bother sending
3067880f3e4Seric 	**  the message at all if we are in SMTP mode.  Local errors
3077880f3e4Seric 	**  (e.g., a single address failing) will still cause the other
3087880f3e4Seric 	**  addresses to be sent.
3097880f3e4Seric 	*/
3107880f3e4Seric 
3117880f3e4Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
3127880f3e4Seric 	{
3137880f3e4Seric 		e->e_flags |= EF_CLRQUEUE;
3147880f3e4Seric 		return;
3157880f3e4Seric 	}
3167880f3e4Seric 
317ce5531bdSeric 	oldverbose = Verbose;
318c1ac89b1Seric 	switch (mode)
319c1ac89b1Seric 	{
320c1ac89b1Seric 	  case SM_VERIFY:
321c1ac89b1Seric 		Verbose = TRUE;
322c1ac89b1Seric 		break;
323c1ac89b1Seric 
324c1ac89b1Seric 	  case SM_QUEUE:
325c1ac89b1Seric   queueonly:
326c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
327c1ac89b1Seric 		return;
328c1ac89b1Seric 
329c1ac89b1Seric 	  case SM_FORK:
330c1ac89b1Seric 		if (e->e_xfp != NULL)
331c1ac89b1Seric 			(void) fflush(e->e_xfp);
332c1ac89b1Seric 
3332b9178d3Seric # ifndef HASFLOCK
334c1ac89b1Seric 		/*
3356b2765c6Seric 		**  Since fcntl locking has the interesting semantic that
3366b2765c6Seric 		**  the lock is owned by a process, not by an open file
3376b2765c6Seric 		**  descriptor, we have to flush this to the queue, and
3386b2765c6Seric 		**  then restart from scratch in the child.
339c1ac89b1Seric 		*/
340c1ac89b1Seric 
3416b2765c6Seric 		/* save id for future use */
3426b2765c6Seric 		id = e->e_id;
3436b2765c6Seric 
3446b2765c6Seric 		/* now drop the envelope in the parent */
3456b2765c6Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
34644622ea3Seric 		e->e_flags &= ~EF_FATALERRS;
3476b2765c6Seric 		dropenvelope(e);
3486b2765c6Seric 
3496b2765c6Seric 		/* and reacquire in the child */
3506b2765c6Seric 		(void) dowork(id, TRUE, FALSE, e);
3516b2765c6Seric 
3526b2765c6Seric 		return;
3536b2765c6Seric 
3546b2765c6Seric # else /* HASFLOCK */
355c1ac89b1Seric 
356c1ac89b1Seric 		pid = fork();
357c1ac89b1Seric 		if (pid < 0)
358c1ac89b1Seric 		{
359c1ac89b1Seric 			goto queueonly;
360c1ac89b1Seric 		}
361c1ac89b1Seric 		else if (pid > 0)
362c1ac89b1Seric 		{
3630e484fe5Seric 			/* be sure we leave the temp files to our child */
3640e484fe5Seric 			/* can't call unlockqueue to avoid unlink of xfp */
3650e484fe5Seric 			if (e->e_lockfp != NULL)
3660e484fe5Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3670e484fe5Seric 			e->e_lockfp = NULL;
3680e484fe5Seric 
3690e484fe5Seric 			/* close any random open files in the envelope */
3700e484fe5Seric 			closexscript(e);
3710e484fe5Seric 			if (e->e_dfp != NULL)
3720e484fe5Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
3730e484fe5Seric 			e->e_dfp = NULL;
3740e484fe5Seric 			e->e_id = e->e_df = NULL;
375c1ac89b1Seric 			return;
376c1ac89b1Seric 		}
377c1ac89b1Seric 
378c1ac89b1Seric 		/* double fork to avoid zombies */
379c1ac89b1Seric 		if (fork() > 0)
380c1ac89b1Seric 			exit(EX_OK);
381c1ac89b1Seric 
382c1ac89b1Seric 		/* be sure we are immune from the terminal */
3837880f3e4Seric 		disconnect(1, e);
384c1ac89b1Seric 
385c1ac89b1Seric 		/*
386c1ac89b1Seric 		**  Close any cached connections.
387c1ac89b1Seric 		**
388c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
389c1ac89b1Seric 		**	still knows about the connection.
390c1ac89b1Seric 		**
391c1ac89b1Seric 		**	This should only happen when delivering an error
392c1ac89b1Seric 		**	message.
393c1ac89b1Seric 		*/
394c1ac89b1Seric 
395c1ac89b1Seric 		mci_flush(FALSE, NULL);
396c1ac89b1Seric 
3976b2765c6Seric # endif /* HASFLOCK */
3986b2765c6Seric 
399c1ac89b1Seric 		break;
400c1ac89b1Seric 	}
401c1ac89b1Seric 
402c1ac89b1Seric 	/*
4039c9e68d9Seric 	**  Run through the list and send everything.
4045288e21aSeric 	**
4055288e21aSeric 	**	Set EF_GLOBALERRS so that error messages during delivery
4065288e21aSeric 	**	result in returned mail.
4079c9e68d9Seric 	*/
4089c9e68d9Seric 
4099c9e68d9Seric 	e->e_nsent = 0;
4105288e21aSeric 	e->e_flags |= EF_GLOBALERRS;
411faad2b16Seric 
412faad2b16Seric 	/* now run through the queue */
4139c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4149c9e68d9Seric 	{
415fdc75a0fSeric #ifdef XDEBUG
416fdc75a0fSeric 		char wbuf[MAXNAME + 20];
417fdc75a0fSeric 
418fdc75a0fSeric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
419fdc75a0fSeric 		checkfd012(wbuf);
420fdc75a0fSeric #endif
4219c9e68d9Seric 		if (mode == SM_VERIFY)
4229c9e68d9Seric 		{
4239c9e68d9Seric 			e->e_to = q->q_paddr;
4249c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4258dfca105Seric 			{
4268dfca105Seric 				message("deliverable: mailer %s, host %s, user %s",
4278dfca105Seric 					q->q_mailer->m_name,
4288dfca105Seric 					q->q_host,
4298dfca105Seric 					q->q_user);
4308dfca105Seric 			}
4319c9e68d9Seric 		}
4329c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4339c9e68d9Seric 		{
4349c9e68d9Seric # ifdef QUEUE
4359c9e68d9Seric 			/*
4369c9e68d9Seric 			**  Checkpoint the send list every few addresses
4379c9e68d9Seric 			*/
4389c9e68d9Seric 
4399c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4409c9e68d9Seric 			{
4419c9e68d9Seric 				queueup(e, TRUE, FALSE);
4429c9e68d9Seric 				e->e_nsent = 0;
4439c9e68d9Seric 			}
4449c9e68d9Seric # endif /* QUEUE */
4459c9e68d9Seric 			(void) deliver(e, q);
4469c9e68d9Seric 		}
4479c9e68d9Seric 	}
4489c9e68d9Seric 	Verbose = oldverbose;
4499c9e68d9Seric 
450fdc75a0fSeric #ifdef XDEBUG
451fdc75a0fSeric 	checkfd012("end of sendenvelope");
452fdc75a0fSeric #endif
453fdc75a0fSeric 
4549c9e68d9Seric 	if (mode == SM_FORK)
4559c9e68d9Seric 		finis();
4569c9e68d9Seric }
4579c9e68d9Seric /*
4589c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4599c9e68d9Seric **
4609c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4619c9e68d9Seric **	two processes on the same stack!!!
4629c9e68d9Seric **
4639c9e68d9Seric **	Parameters:
4649c9e68d9Seric **		none.
4659c9e68d9Seric **
4669c9e68d9Seric **	Returns:
4679c9e68d9Seric **		From a macro???  You've got to be kidding!
4689c9e68d9Seric **
4699c9e68d9Seric **	Side Effects:
4709c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4719c9e68d9Seric **			pid of child in parent, zero in child.
4729c9e68d9Seric **			-1 on unrecoverable error.
4739c9e68d9Seric **
4749c9e68d9Seric **	Notes:
4759c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4769c9e68d9Seric **		vfork for you.....
4779c9e68d9Seric */
4789c9e68d9Seric 
4799c9e68d9Seric # define NFORKTRIES	5
4809c9e68d9Seric 
4819c9e68d9Seric # ifndef FORK
4829c9e68d9Seric # define FORK	fork
4839c9e68d9Seric # endif
4849c9e68d9Seric 
4859c9e68d9Seric # define DOFORK(fORKfN) \
4869c9e68d9Seric {\
4879c9e68d9Seric 	register int i;\
4889c9e68d9Seric \
4899c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4909c9e68d9Seric 	{\
4919c9e68d9Seric 		pid = fORKfN();\
4929c9e68d9Seric 		if (pid >= 0)\
4939c9e68d9Seric 			break;\
4949c9e68d9Seric 		if (i > 0)\
4959c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
4969c9e68d9Seric 	}\
4979c9e68d9Seric }
4989c9e68d9Seric /*
4999c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
5009c9e68d9Seric **
5019c9e68d9Seric **	Parameters:
5029c9e68d9Seric **		none.
5039c9e68d9Seric **
5049c9e68d9Seric **	Returns:
5059c9e68d9Seric **		pid of child in parent.
5069c9e68d9Seric **		zero in child.
5079c9e68d9Seric **		-1 on error.
5089c9e68d9Seric **
5099c9e68d9Seric **	Side Effects:
5109c9e68d9Seric **		returns twice, once in parent and once in child.
5119c9e68d9Seric */
5129c9e68d9Seric 
5139c9e68d9Seric dofork()
5149c9e68d9Seric {
5159c9e68d9Seric 	register int pid;
5169c9e68d9Seric 
5179c9e68d9Seric 	DOFORK(fork);
5189c9e68d9Seric 	return (pid);
5199c9e68d9Seric }
5209c9e68d9Seric /*
52113bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
52213bbc08cSeric **
52313bbc08cSeric **	This routine delivers to everyone on the same host as the
52413bbc08cSeric **	user on the head of the list.  It is clever about mailers
52513bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
52613bbc08cSeric **	that it will deliver to all these addresses however -- so
52713bbc08cSeric **	deliver should be called once for each address on the
52813bbc08cSeric **	list.
52925a99e2eSeric **
53025a99e2eSeric **	Parameters:
531588cad61Seric **		e -- the envelope to deliver.
532c77d1c25Seric **		firstto -- head of the address list to deliver to.
53325a99e2eSeric **
53425a99e2eSeric **	Returns:
53525a99e2eSeric **		zero -- successfully delivered.
53625a99e2eSeric **		else -- some failure, see ExitStat for more info.
53725a99e2eSeric **
53825a99e2eSeric **	Side Effects:
53925a99e2eSeric **		The standard input is passed off to someone.
54025a99e2eSeric */
54125a99e2eSeric 
542588cad61Seric deliver(e, firstto)
543588cad61Seric 	register ENVELOPE *e;
544c77d1c25Seric 	ADDRESS *firstto;
54525a99e2eSeric {
54678442df3Seric 	char *host;			/* host being sent to */
54778442df3Seric 	char *user;			/* user being sent to */
54825a99e2eSeric 	char **pvp;
5495dfc646bSeric 	register char **mvp;
55025a99e2eSeric 	register char *p;
551588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5526259796dSeric 	ADDRESS *ctladdr;
553b31e7f2bSeric 	register MCI *mci;
554c77d1c25Seric 	register ADDRESS *to = firstto;
555c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
556772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
557911693bfSbostic 	int rcode;			/* response code */
558e103b48fSeric 	char *firstsig;			/* signature of firstto */
5599c9e68d9Seric 	int pid;
5609c9e68d9Seric 	char *curhost;
5619c9e68d9Seric 	int mpvect[2];
5629c9e68d9Seric 	int rpvect[2];
563ee6bf8dfSeric 	char *pv[MAXPV+1];
564579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
565ee6bf8dfSeric 	char buf[MAXNAME];
566c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
567fabb3bd4Seric 	extern int checkcompat();
5689c9e68d9Seric 	extern FILE *fdopen();
569*df106f0bSeric 	extern char SmtpError[];
57025a99e2eSeric 
57135490626Seric 	errno = 0;
572ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5735dfc646bSeric 		return (0);
57425a99e2eSeric 
575134746fbSeric #ifdef NAMED_BIND
576912a731aSbostic 	/* unless interactive, try twice, over a minute */
577912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
578912a731aSbostic 		_res.retrans = 30;
579912a731aSbostic 		_res.retry = 2;
580912a731aSbostic 	}
581d4bd8f0eSbostic #endif
582912a731aSbostic 
58351552439Seric 	m = to->q_mailer;
58451552439Seric 	host = to->q_host;
585c9be6216Seric 	CurEnv = e;			/* just in case */
5864384d521Seric 	e->e_statmsg = NULL;
587*df106f0bSeric 	SmtpError[0] = '\0';
58851552439Seric 
5896ef48975Seric 	if (tTd(10, 1))
5905dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
59151552439Seric 			m->m_mno, host, to->q_user);
592f3dbc832Seric 
593f3dbc832Seric 	/*
594f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
595f3dbc832Seric 	**  connections now, just mark these addresses and return.
596f3dbc832Seric 	**	This is useful if we want to batch connections to
597f3dbc832Seric 	**	reduce load.  This will cause the messages to be
598f3dbc832Seric 	**	queued up, and a daemon will come along to send the
599f3dbc832Seric 	**	messages later.
600f3dbc832Seric 	**		This should be on a per-mailer basis.
601f3dbc832Seric 	*/
602f3dbc832Seric 
60379aa5155Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
604f3dbc832Seric 	{
605f3dbc832Seric 		for (; to != NULL; to = to->q_next)
606f4560e80Seric 		{
607ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
608c6e18ac5Seric 			    to->q_mailer != m)
609f4560e80Seric 				continue;
610268a25e1Seric 			to->q_flags |= QQUEUEUP;
611588cad61Seric 			e->e_to = to->q_paddr;
61208b25121Seric 			message("queued");
6132f624c86Seric 			if (LogLevel > 8)
61481161401Seric 				logdelivery(m, NULL, "queued", e);
615f4560e80Seric 		}
616588cad61Seric 		e->e_to = NULL;
617f3dbc832Seric 		return (0);
618f3dbc832Seric 	}
619f3dbc832Seric 
62025a99e2eSeric 	/*
6215dfc646bSeric 	**  Do initial argv setup.
6225dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6235dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6245dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6255dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6265dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6273bea8136Seric 	**		The from address rewrite is expected to make
6283bea8136Seric 	**		the address relative to the other end.
6295dfc646bSeric 	*/
6305dfc646bSeric 
63178442df3Seric 	/* rewrite from address, using rewriting rules */
632efe54562Seric 	rcode = EX_OK;
633efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
634efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
635efe54562Seric 					   &rcode, e));
636ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
637588cad61Seric 	define('h', host, e);			/* to host */
6385dfc646bSeric 	Errors = 0;
6395dfc646bSeric 	pvp = pv;
6405dfc646bSeric 	*pvp++ = m->m_argv[0];
6415dfc646bSeric 
6425dfc646bSeric 	/* insert -f or -r flag as appropriate */
64357fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6445dfc646bSeric 	{
64557fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6465dfc646bSeric 			*pvp++ = "-f";
6475dfc646bSeric 		else
6485dfc646bSeric 			*pvp++ = "-r";
649c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6505dfc646bSeric 	}
6515dfc646bSeric 
6525dfc646bSeric 	/*
6535dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6545dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6555dfc646bSeric 	**  be one of these, and there are only a few more slots
6565dfc646bSeric 	**  in the pv after it.
6575dfc646bSeric 	*/
6585dfc646bSeric 
6595dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6605dfc646bSeric 	{
6612bc47524Seric 		/* can't use strchr here because of sign extension problems */
6622bc47524Seric 		while (*p != '\0')
6632bc47524Seric 		{
6642bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6652bc47524Seric 			{
6662bc47524Seric 				if (*p == 'u')
6675dfc646bSeric 					break;
6682bc47524Seric 			}
6692bc47524Seric 		}
6702bc47524Seric 
6712bc47524Seric 		if (*p != '\0')
6725dfc646bSeric 			break;
6735dfc646bSeric 
6745dfc646bSeric 		/* this entry is safe -- go ahead and process it */
675588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6765dfc646bSeric 		*pvp++ = newstr(buf);
6775dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6785dfc646bSeric 		{
67908b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6805dfc646bSeric 			return (-1);
6815dfc646bSeric 		}
6825dfc646bSeric 	}
683c579ef51Seric 
68433db8731Seric 	/*
68533db8731Seric 	**  If we have no substitution for the user name in the argument
68633db8731Seric 	**  list, we know that we must supply the names otherwise -- and
68733db8731Seric 	**  SMTP is the answer!!
68833db8731Seric 	*/
68933db8731Seric 
6905dfc646bSeric 	if (*mvp == NULL)
691c579ef51Seric 	{
692c579ef51Seric 		/* running SMTP */
6932c7e1b8dSeric # ifdef SMTP
694c579ef51Seric 		clever = TRUE;
695c579ef51Seric 		*pvp = NULL;
6966c2c3107Seric # else /* SMTP */
69733db8731Seric 		/* oops!  we don't implement SMTP */
698*df106f0bSeric 		syserr("554 SMTP style mailer not implemented");
6992c7e1b8dSeric 		return (EX_SOFTWARE);
7006c2c3107Seric # endif /* SMTP */
701c579ef51Seric 	}
7025dfc646bSeric 
7035dfc646bSeric 	/*
7045dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7055dfc646bSeric 	**  run through our address list and append all the addresses
7065dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7075dfc646bSeric 	**  always send another copy later.
7085dfc646bSeric 	*/
7095dfc646bSeric 
7105dfc646bSeric 	tobuf[0] = '\0';
711588cad61Seric 	e->e_to = tobuf;
7126259796dSeric 	ctladdr = NULL;
713e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7145dfc646bSeric 	for (; to != NULL; to = to->q_next)
7155dfc646bSeric 	{
7165dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
71757fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7185dfc646bSeric 			break;
7195dfc646bSeric 
7205dfc646bSeric 		/* if already sent or not for this host, don't send */
721ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
722e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
723e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7245dfc646bSeric 			continue;
7256259796dSeric 
7264b22ea87Seric 		/* avoid overflowing tobuf */
727aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7284b22ea87Seric 			break;
7294b22ea87Seric 
7306ef48975Seric 		if (tTd(10, 1))
731772e6e50Seric 		{
732772e6e50Seric 			printf("\nsend to ");
733772e6e50Seric 			printaddr(to, FALSE);
734772e6e50Seric 		}
735772e6e50Seric 
7366259796dSeric 		/* compute effective uid/gid when sending */
7377da1035fSeric 		if (to->q_mailer == ProgMailer)
7386259796dSeric 			ctladdr = getctladdr(to);
7396259796dSeric 
7405dfc646bSeric 		user = to->q_user;
741588cad61Seric 		e->e_to = to->q_paddr;
74275f1ade9Seric 		if (tTd(10, 5))
74375f1ade9Seric 		{
74475f1ade9Seric 			printf("deliver: QDONTSEND ");
74575f1ade9Seric 			printaddr(to, FALSE);
74675f1ade9Seric 		}
747ee4b0922Seric 		to->q_flags |= QDONTSEND;
7485dfc646bSeric 
7495dfc646bSeric 		/*
7505dfc646bSeric 		**  Check to see that these people are allowed to
7515dfc646bSeric 		**  talk to each other.
7522a6e0786Seric 		*/
7532a6e0786Seric 
75469582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
75569582d2fSeric 		{
75669582d2fSeric 			NoReturn = TRUE;
75708b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
75881161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
75969582d2fSeric 			continue;
76069582d2fSeric 		}
761fabb3bd4Seric 		rcode = checkcompat(to, e);
7621793c9c5Seric 		if (rcode != EX_OK)
7635dfc646bSeric 		{
7641000c37aSeric 			markfailure(e, to, rcode);
76581161401Seric 			giveresponse(rcode, m, NULL, e);
7665dfc646bSeric 			continue;
7675dfc646bSeric 		}
7682a6e0786Seric 
7692a6e0786Seric 		/*
7709ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7719ec9501bSeric 		**	about them.
77225a99e2eSeric 		*/
77325a99e2eSeric 
77457fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
77525a99e2eSeric 		{
7761d8f1806Seric 			stripquotes(user);
7771d8f1806Seric 			stripquotes(host);
77825a99e2eSeric 		}
77925a99e2eSeric 
780cdb828c5Seric 		/* hack attack -- delivermail compatibility */
781cdb828c5Seric 		if (m == ProgMailer && *user == '|')
782cdb828c5Seric 			user++;
783cdb828c5Seric 
78425a99e2eSeric 		/*
7853efaed6eSeric 		**  If an error message has already been given, don't
7863efaed6eSeric 		**	bother to send to this address.
7873efaed6eSeric 		**
7883efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7893efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
7903efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
7913efaed6eSeric 		*/
7923efaed6eSeric 
7936cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7943efaed6eSeric 			continue;
7953efaed6eSeric 
796f2fec898Seric 		/* save statistics.... */
797588cad61Seric 		markstats(e, to);
798f2fec898Seric 
7993efaed6eSeric 		/*
80025a99e2eSeric 		**  See if this user name is "special".
80125a99e2eSeric 		**	If the user name has a slash in it, assume that this
80251552439Seric 		**	is a file -- send it off without further ado.  Note
80351552439Seric 		**	that this type of addresses is not processed along
80451552439Seric 		**	with the others, so we fudge on the To person.
80525a99e2eSeric 		*/
80625a99e2eSeric 
8072c017f8dSeric 		if (m == FileMailer)
80825a99e2eSeric 		{
809b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
81081161401Seric 			giveresponse(rcode, m, NULL, e);
811dde5acadSeric 			if (rcode == EX_OK)
812dde5acadSeric 				to->q_flags |= QSENT;
8135dfc646bSeric 			continue;
81425a99e2eSeric 		}
81525a99e2eSeric 
81613bbc08cSeric 		/*
81713bbc08cSeric 		**  Address is verified -- add this user to mailer
81813bbc08cSeric 		**  argv, and add it to the print list of recipients.
81913bbc08cSeric 		*/
82013bbc08cSeric 
821508daeccSeric 		/* link together the chain of recipients */
822508daeccSeric 		to->q_tchain = tochain;
823508daeccSeric 		tochain = to;
824508daeccSeric 
8255dfc646bSeric 		/* create list of users for error messages */
826db8841e9Seric 		(void) strcat(tobuf, ",");
827db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
828588cad61Seric 		define('u', user, e);		/* to user */
829b6f89e6eSeric 		p = to->q_home;
830b6f89e6eSeric 		if (p == NULL && ctladdr != NULL)
831b6f89e6eSeric 			p = ctladdr->q_home;
832b6f89e6eSeric 		define('z', p, e);	/* user's home */
8335dfc646bSeric 
834c579ef51Seric 		/*
835508daeccSeric 		**  Expand out this user into argument list.
836c579ef51Seric 		*/
837c579ef51Seric 
838508daeccSeric 		if (!clever)
839c579ef51Seric 		{
840588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8415dfc646bSeric 			*pvp++ = newstr(buf);
8425dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8435dfc646bSeric 			{
8445dfc646bSeric 				/* allow some space for trailing parms */
8455dfc646bSeric 				break;
8465dfc646bSeric 			}
8475dfc646bSeric 		}
848c579ef51Seric 	}
8495dfc646bSeric 
850145b49b1Seric 	/* see if any addresses still exist */
851145b49b1Seric 	if (tobuf[0] == '\0')
852c579ef51Seric 	{
853588cad61Seric 		define('g', (char *) NULL, e);
854145b49b1Seric 		return (0);
855c579ef51Seric 	}
856145b49b1Seric 
8575dfc646bSeric 	/* print out messages as full list */
85863780dbdSeric 	e->e_to = tobuf + 1;
8595dfc646bSeric 
8605dfc646bSeric 	/*
8615dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8625dfc646bSeric 	*/
8635dfc646bSeric 
864c579ef51Seric 	while (!clever && *++mvp != NULL)
8655dfc646bSeric 	{
866588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8675dfc646bSeric 		*pvp++ = newstr(buf);
8685dfc646bSeric 		if (pvp >= &pv[MAXPV])
86908b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8705dfc646bSeric 	}
8715dfc646bSeric 	*pvp++ = NULL;
8725dfc646bSeric 
87325a99e2eSeric 	/*
87425a99e2eSeric 	**  Call the mailer.
8756328bdf7Seric 	**	The argument vector gets built, pipes
87625a99e2eSeric 	**	are created as necessary, and we fork & exec as
8776328bdf7Seric 	**	appropriate.
878c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
87925a99e2eSeric 	*/
88025a99e2eSeric 
8817ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
88286b26461Seric 		ctladdr = &e->e_from;
883134746fbSeric #ifdef NAMED_BIND
8842bcc6d2dSeric 	if (ConfigLevel < 2)
885912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
886134746fbSeric #endif
8879c9e68d9Seric 
8889c9e68d9Seric 	if (tTd(11, 1))
889134746fbSeric 	{
8909c9e68d9Seric 		printf("openmailer:");
8919c9e68d9Seric 		printav(pv);
8929c9e68d9Seric 	}
8939c9e68d9Seric 	errno = 0;
8949c9e68d9Seric 
8959c9e68d9Seric 	CurHostName = m->m_mailer;
8969c9e68d9Seric 
8979c9e68d9Seric 	/*
8989c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
8999c9e68d9Seric 	**  connection.
9009c9e68d9Seric 	**	In this case we don't actually fork.  We must be
9019c9e68d9Seric 	**	running SMTP for this to work.  We will return a
9029c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9039c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9049c9e68d9Seric 	*/
9059c9e68d9Seric 
9069c9e68d9Seric 	curhost = NULL;
907c931b82bSeric 	SmtpPhase = NULL;
908*df106f0bSeric 	mci = NULL;
9099c9e68d9Seric 
9107febfd66Seric #ifdef XDEBUG
9117febfd66Seric 	{
9127febfd66Seric 		char wbuf[MAXLINE];
9137febfd66Seric 
9147febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
9157febfd66Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
9167febfd66Seric 		checkfd012(wbuf);
9177febfd66Seric 	}
9187febfd66Seric #endif
9197febfd66Seric 
9209c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9219c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9229c9e68d9Seric 	{
9239c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9249c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9259c9e68d9Seric 		mci->mci_in = stdin;
9269c9e68d9Seric 		mci->mci_out = stdout;
9279c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9289c9e68d9Seric 		mci->mci_mailer = m;
9299c9e68d9Seric 	}
9309c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9319c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9329c9e68d9Seric 	{
9339c9e68d9Seric #ifdef DAEMON
9349c9e68d9Seric 		register int i;
9359c9e68d9Seric 		register u_short port;
9369c9e68d9Seric 
9379c9e68d9Seric 		CurHostName = pv[1];
9389c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9399c9e68d9Seric 
9409c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9419c9e68d9Seric 		{
9429c9e68d9Seric 			syserr("null signature");
943845e533cSeric 			rcode = EX_OSERR;
944b31e7f2bSeric 			goto give_up;
945b31e7f2bSeric 		}
9469c9e68d9Seric 
9479c9e68d9Seric 		if (!clever)
9489c9e68d9Seric 		{
9499c9e68d9Seric 			syserr("554 non-clever IPC");
950*df106f0bSeric 			rcode = EX_CONFIG;
9519c9e68d9Seric 			goto give_up;
9529c9e68d9Seric 		}
9539c9e68d9Seric 		if (pv[2] != NULL)
9549c9e68d9Seric 			port = atoi(pv[2]);
9559c9e68d9Seric 		else
9569c9e68d9Seric 			port = 0;
9579c9e68d9Seric tryhost:
9589c9e68d9Seric 		while (*curhost != '\0')
9599c9e68d9Seric 		{
9609c9e68d9Seric 			register char *p;
9619c9e68d9Seric 			static char hostbuf[MAXNAME];
9629c9e68d9Seric 
9639c9e68d9Seric 			/* pull the next host from the signature */
9649c9e68d9Seric 			p = strchr(curhost, ':');
9659c9e68d9Seric 			if (p == NULL)
9669c9e68d9Seric 				p = &curhost[strlen(curhost)];
967*df106f0bSeric 			if (p == curhost)
968*df106f0bSeric 			{
969*df106f0bSeric 				syserr("deliver: null host name in signature");
970*df106f0bSeric 				continue;
971*df106f0bSeric 			}
9729c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9739c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9749c9e68d9Seric 			if (*p != '\0')
9759c9e68d9Seric 				p++;
9769c9e68d9Seric 			curhost = p;
9779c9e68d9Seric 
9789c9e68d9Seric 			/* see if we already know that this host is fried */
9799c9e68d9Seric 			CurHostName = hostbuf;
9809c9e68d9Seric 			mci = mci_get(hostbuf, m);
9819c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9829c9e68d9Seric 			{
9839c9e68d9Seric 				if (tTd(11, 1))
9849c9e68d9Seric 				{
9859c9e68d9Seric 					printf("openmailer: ");
9869c9e68d9Seric 					mci_dump(mci);
9879c9e68d9Seric 				}
9889c9e68d9Seric 				CurHostName = mci->mci_host;
9899c9e68d9Seric 				break;
9909c9e68d9Seric 			}
9919c9e68d9Seric 			mci->mci_mailer = m;
9929c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9939c9e68d9Seric 				continue;
9949c9e68d9Seric 
9959c9e68d9Seric 			/* try the connection */
9969c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9979c9e68d9Seric 			message("Connecting to %s (%s)...",
9989c9e68d9Seric 				hostbuf, m->m_name);
9999c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
10009c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
10019c9e68d9Seric 			mci->mci_exitstat = i;
10029c9e68d9Seric 			mci->mci_errno = errno;
1003f170942cSeric #ifdef NAMED_BIND
1004f170942cSeric 			mci->mci_herrno = h_errno;
1005f170942cSeric #endif
10069c9e68d9Seric 			if (i == EX_OK)
10079c9e68d9Seric 			{
10089c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
10099c9e68d9Seric 				mci_cache(mci);
1010f170942cSeric 				if (TrafficLogFile != NULL)
1011f170942cSeric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
1012f170942cSeric 						getpid(), hostbuf);
10139c9e68d9Seric 				break;
10149c9e68d9Seric 			}
10159c9e68d9Seric 			else if (tTd(11, 1))
10169c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
10179c9e68d9Seric 					i, errno);
10189c9e68d9Seric 
10199c9e68d9Seric 			/* enter status of this host */
10209c9e68d9Seric 			setstat(i);
1021*df106f0bSeric 
1022*df106f0bSeric 			/* should print some message here for -v mode */
1023*df106f0bSeric 		}
1024*df106f0bSeric 		if (mci == NULL)
1025*df106f0bSeric 		{
1026*df106f0bSeric 			syserr("deliver: no host name");
1027*df106f0bSeric 			rcode = EX_OSERR;
1028*df106f0bSeric 			goto give_up;
10299c9e68d9Seric 		}
10309c9e68d9Seric 		mci->mci_pid = 0;
10319c9e68d9Seric #else /* no DAEMON */
10329c9e68d9Seric 		syserr("554 openmailer: no IPC");
10339c9e68d9Seric 		if (tTd(11, 1))
10349c9e68d9Seric 			printf("openmailer: NULL\n");
1035*df106f0bSeric 		rcode = EX_UNAVAILABLE;
1036*df106f0bSeric 		goto give_up;
10379c9e68d9Seric #endif /* DAEMON */
10389c9e68d9Seric 	}
10399c9e68d9Seric 	else
10409c9e68d9Seric 	{
1041f170942cSeric 		if (TrafficLogFile != NULL)
10420e3bfef5Seric 		{
1043f170942cSeric 			char **av;
1044f170942cSeric 
1045f170942cSeric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
1046f170942cSeric 			for (av = pv; *av != NULL; av++)
1047f170942cSeric 				fprintf(TrafficLogFile, " %s", *av);
1048f170942cSeric 			fprintf(TrafficLogFile, "\n");
10496fe8c3bcSeric 		}
10506fe8c3bcSeric 
10519c9e68d9Seric 		/* create a pipe to shove the mail through */
10529c9e68d9Seric 		if (pipe(mpvect) < 0)
10539c9e68d9Seric 		{
10540e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10550e3bfef5Seric 				e->e_to, m->m_name);
10569c9e68d9Seric 			if (tTd(11, 1))
10579c9e68d9Seric 				printf("openmailer: NULL\n");
10589c9e68d9Seric 			rcode = EX_OSERR;
10599c9e68d9Seric 			goto give_up;
10609c9e68d9Seric 		}
10619c9e68d9Seric 
10629c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10639c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10649c9e68d9Seric 		{
10650e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10660e3bfef5Seric 				e->e_to, m->m_name);
10679c9e68d9Seric 			(void) close(mpvect[0]);
10689c9e68d9Seric 			(void) close(mpvect[1]);
10699c9e68d9Seric 			if (tTd(11, 1))
10709c9e68d9Seric 				printf("openmailer: NULL\n");
10719c9e68d9Seric 			rcode = EX_OSERR;
10729c9e68d9Seric 			goto give_up;
10739c9e68d9Seric 		}
10749c9e68d9Seric 
10759c9e68d9Seric 		/*
10769c9e68d9Seric 		**  Actually fork the mailer process.
10779c9e68d9Seric 		**	DOFORK is clever about retrying.
10789c9e68d9Seric 		**
10799c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10809c9e68d9Seric 		**	around so that endmail will get it.
10819c9e68d9Seric 		*/
10829c9e68d9Seric 
10839c9e68d9Seric 		if (e->e_xfp != NULL)
10849c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10859c9e68d9Seric 		(void) fflush(stdout);
10869c9e68d9Seric # ifdef SIGCHLD
10872b9178d3Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
10889c9e68d9Seric # endif /* SIGCHLD */
10899c9e68d9Seric 		DOFORK(FORK);
10909c9e68d9Seric 		/* pid is set by DOFORK */
10919c9e68d9Seric 		if (pid < 0)
10929c9e68d9Seric 		{
10939c9e68d9Seric 			/* failure */
10940e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
10950e3bfef5Seric 				e->e_to, m->m_name);
10969c9e68d9Seric 			(void) close(mpvect[0]);
10979c9e68d9Seric 			(void) close(mpvect[1]);
10989c9e68d9Seric 			if (clever)
10999c9e68d9Seric 			{
11009c9e68d9Seric 				(void) close(rpvect[0]);
11019c9e68d9Seric 				(void) close(rpvect[1]);
11029c9e68d9Seric 			}
11039c9e68d9Seric 			if (tTd(11, 1))
11049c9e68d9Seric 				printf("openmailer: NULL\n");
11059c9e68d9Seric 			rcode = EX_OSERR;
11069c9e68d9Seric 			goto give_up;
11079c9e68d9Seric 		}
11089c9e68d9Seric 		else if (pid == 0)
11099c9e68d9Seric 		{
11109c9e68d9Seric 			int i;
11119c9e68d9Seric 			int saveerrno;
11129c9e68d9Seric 			char **ep;
11139c9e68d9Seric 			char *env[MAXUSERENVIRON];
11149c9e68d9Seric 			extern char **environ;
11159c9e68d9Seric 			extern int DtableSize;
11169c9e68d9Seric 
11179c9e68d9Seric 			/* child -- set up input & exec mailer */
11182b9178d3Seric 			(void) setsignal(SIGINT, SIG_IGN);
11192b9178d3Seric 			(void) setsignal(SIGHUP, SIG_IGN);
11202b9178d3Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11219c9e68d9Seric 
11229c9e68d9Seric 			/* close any other cached connections */
11239c9e68d9Seric 			mci_flush(FALSE, mci);
11249c9e68d9Seric 
112544f2317fSeric 			/* reset user and group */
112644f2317fSeric 			if (!bitnset(M_RESTR, m->m_flags))
112744f2317fSeric 			{
112844f2317fSeric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
112944f2317fSeric 				{
113044f2317fSeric 					(void) initgroups(DefUser, DefGid);
113144f2317fSeric 					(void) setuid(DefUid);
113244f2317fSeric 				}
113344f2317fSeric 				else
113444f2317fSeric 				{
113544f2317fSeric 					(void) initgroups(ctladdr->q_ruser?
113644f2317fSeric 						ctladdr->q_ruser: ctladdr->q_user,
113744f2317fSeric 						ctladdr->q_gid);
113844f2317fSeric 					(void) setuid(ctladdr->q_uid);
113944f2317fSeric 				}
114044f2317fSeric 			}
114144f2317fSeric 
114244f2317fSeric 			if (tTd(11, 2))
114344f2317fSeric 				printf("openmailer: running as r/euid=%d/%d\n",
114444f2317fSeric 					getuid(), geteuid());
114544f2317fSeric 
1146b986f6aaSeric 			/* move into some "safe" directory */
1147b986f6aaSeric 			if (m->m_execdir != NULL)
1148b986f6aaSeric 			{
1149b986f6aaSeric 				char *p, *q;
1150b986f6aaSeric 				char buf[MAXLINE];
1151b986f6aaSeric 
1152b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1153b986f6aaSeric 				{
1154b986f6aaSeric 					q = strchr(p, ':');
1155b986f6aaSeric 					if (q != NULL)
1156b986f6aaSeric 						*q = '\0';
1157b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1158b986f6aaSeric 					if (q != NULL)
1159b986f6aaSeric 						*q++ = ':';
1160b986f6aaSeric 					if (tTd(11, 20))
1161b986f6aaSeric 						printf("openmailer: trydir %s\n",
1162b986f6aaSeric 							buf);
1163b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1164b986f6aaSeric 						break;
1165b986f6aaSeric 				}
1166b986f6aaSeric 			}
1167b986f6aaSeric 
11689c9e68d9Seric 			/* arrange to filter std & diag output of command */
11699c9e68d9Seric 			if (clever)
11709c9e68d9Seric 			{
11719c9e68d9Seric 				(void) close(rpvect[0]);
11726fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11736fe8c3bcSeric 				{
11740e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11750e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11766fe8c3bcSeric 					_exit(EX_OSERR);
11776fe8c3bcSeric 				}
11789c9e68d9Seric 				(void) close(rpvect[1]);
11799c9e68d9Seric 			}
11809c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11819c9e68d9Seric 			{
11829c9e68d9Seric 				/* put mailer output in transcript */
11836fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11846fe8c3bcSeric 				{
11850e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11860e3bfef5Seric 						e->e_to, m->m_name,
11876fe8c3bcSeric 						fileno(e->e_xfp));
11886fe8c3bcSeric 					_exit(EX_OSERR);
11899c9e68d9Seric 				}
11906fe8c3bcSeric 			}
11916fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11926fe8c3bcSeric 			{
11930e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
11940e3bfef5Seric 					e->e_to, m->m_name);
11956fe8c3bcSeric 				_exit(EX_OSERR);
11966fe8c3bcSeric 			}
11979c9e68d9Seric 
11989c9e68d9Seric 			/* arrange to get standard input */
11999c9e68d9Seric 			(void) close(mpvect[1]);
12009c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
12019c9e68d9Seric 			{
12020e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
12030e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
12049c9e68d9Seric 				_exit(EX_OSERR);
12059c9e68d9Seric 			}
12069c9e68d9Seric 			(void) close(mpvect[0]);
12079c9e68d9Seric 
12089c9e68d9Seric 			/* arrange for all the files to be closed */
12099c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
12109c9e68d9Seric 			{
12119c9e68d9Seric 				register int j;
121244f2317fSeric 
12139c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
12149c9e68d9Seric 					(void) fcntl(i, F_SETFD, j | 1);
12159c9e68d9Seric 			}
12169c9e68d9Seric 
12179c9e68d9Seric 			/* set up the mailer environment */
12189c9e68d9Seric 			i = 0;
12199c9e68d9Seric 			env[i++] = "AGENT=sendmail";
12209c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12219c9e68d9Seric 			{
12229c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12239c9e68d9Seric 					env[i++] = *ep;
12249c9e68d9Seric 			}
12259c9e68d9Seric 			env[i++] = NULL;
12269c9e68d9Seric 
12279c9e68d9Seric 			/* try to execute the mailer */
12289c9e68d9Seric 			execve(m->m_mailer, pv, env);
12299c9e68d9Seric 			saveerrno = errno;
12309c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12317c941fd2Seric 			if (m == LocalMailer || transienterror(saveerrno))
12327c941fd2Seric 				_exit(EX_OSERR);
12339c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12349c9e68d9Seric 		}
12359c9e68d9Seric 
12369c9e68d9Seric 		/*
12379c9e68d9Seric 		**  Set up return value.
12389c9e68d9Seric 		*/
12399c9e68d9Seric 
12409c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12419c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12429c9e68d9Seric 		mci->mci_mailer = m;
12439c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12449c9e68d9Seric 		mci->mci_pid = pid;
12459c9e68d9Seric 		(void) close(mpvect[0]);
12469c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
12479c9e68d9Seric 		if (clever)
12489c9e68d9Seric 		{
12499c9e68d9Seric 			(void) close(rpvect[1]);
12509c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
12519c9e68d9Seric 		}
12529c9e68d9Seric 		else
12539c9e68d9Seric 		{
12549c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12559c9e68d9Seric 			mci->mci_in = NULL;
12569c9e68d9Seric 		}
12579c9e68d9Seric 	}
12589c9e68d9Seric 
12599c9e68d9Seric 	/*
12609c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12619c9e68d9Seric 	*/
12629c9e68d9Seric 
12639c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12649c9e68d9Seric 	{
12659c9e68d9Seric 		smtpinit(m, mci, e);
12669c9e68d9Seric 	}
12679c9e68d9Seric 	if (tTd(11, 1))
12689c9e68d9Seric 	{
12699c9e68d9Seric 		printf("openmailer: ");
12709c9e68d9Seric 		mci_dump(mci);
12719c9e68d9Seric 	}
12729c9e68d9Seric 
12739c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1274b31e7f2bSeric 	{
1275b31e7f2bSeric 		/* couldn't open the mailer */
1276b31e7f2bSeric 		rcode = mci->mci_exitstat;
12772a6bc25bSeric 		errno = mci->mci_errno;
1278f170942cSeric #ifdef NAMED_BIND
1279f170942cSeric 		h_errno = mci->mci_herrno;
1280f170942cSeric #endif
1281b31e7f2bSeric 		if (rcode == EX_OK)
1282b31e7f2bSeric 		{
1283b31e7f2bSeric 			/* shouldn't happen */
128408b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12856b0f339dSeric 				rcode, mci->mci_state, firstsig);
1286b31e7f2bSeric 			rcode = EX_SOFTWARE;
1287b31e7f2bSeric 		}
1288e9277e33Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
128990891494Seric 		{
129090891494Seric 			/* try next MX site */
129190891494Seric 			goto tryhost;
129290891494Seric 		}
1293b31e7f2bSeric 	}
1294b31e7f2bSeric 	else if (!clever)
1295b31e7f2bSeric 	{
1296b31e7f2bSeric 		/*
1297b31e7f2bSeric 		**  Format and send message.
1298b31e7f2bSeric 		*/
129915d084d5Seric 
1300b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1301b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1302b31e7f2bSeric 		putline("\n", mci->mci_out, m);
130303c02fdeSeric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
1304b31e7f2bSeric 
1305b31e7f2bSeric 		/* get the exit status */
1306c9be6216Seric 		rcode = endmailer(mci, e, pv);
1307134746fbSeric 	}
1308134746fbSeric 	else
1309b31e7f2bSeric #ifdef SMTP
1310134746fbSeric 	{
1311b31e7f2bSeric 		/*
1312b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1313b31e7f2bSeric 		*/
131415d084d5Seric 
1315b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1316b31e7f2bSeric 		if (rcode == EX_OK)
131775889e88Seric 		{
1318ded0d3daSkarels 			register char *t = tobuf;
1319ded0d3daSkarels 			register int i;
1320ded0d3daSkarels 
1321588cad61Seric 			/* send the recipient list */
132263780dbdSeric 			tobuf[0] = '\0';
132375889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
132475889e88Seric 			{
132563780dbdSeric 				e->e_to = to->q_paddr;
132615d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
132775889e88Seric 				{
132883b7ddc9Seric 					markfailure(e, to, i);
132981161401Seric 					giveresponse(i, m, mci, e);
133063780dbdSeric 				}
133175889e88Seric 				else
133275889e88Seric 				{
1333911693bfSbostic 					*t++ = ',';
1334b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1335b31e7f2bSeric 						continue;
1336ee39df61Seric 					*t = '\0';
1337588cad61Seric 				}
1338588cad61Seric 			}
1339588cad61Seric 
134063780dbdSeric 			/* now send the data */
134163780dbdSeric 			if (tobuf[0] == '\0')
1342b31e7f2bSeric 			{
13439c9e68d9Seric 				rcode = EX_OK;
134463780dbdSeric 				e->e_to = NULL;
1345b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1346b31e7f2bSeric 					smtprset(m, mci, e);
1347b31e7f2bSeric 			}
134875889e88Seric 			else
134975889e88Seric 			{
135063780dbdSeric 				e->e_to = tobuf + 1;
135175889e88Seric 				rcode = smtpdata(m, mci, e);
135263780dbdSeric 			}
135363780dbdSeric 
135463780dbdSeric 			/* now close the connection */
1355b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
135615d084d5Seric 				smtpquit(m, mci, e);
135763780dbdSeric 		}
13589c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13599c9e68d9Seric 		{
13609c9e68d9Seric 			/* try next MX site */
13619c9e68d9Seric 			goto tryhost;
13629c9e68d9Seric 		}
1363c579ef51Seric 	}
1364b31e7f2bSeric #else /* not SMTP */
1365a05b3449Sbostic 	{
136608b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1367845e533cSeric 		rcode = EX_CONFIG;
1368b31e7f2bSeric 		goto give_up;
1369a05b3449Sbostic 	}
1370b31e7f2bSeric #endif /* SMTP */
1371134746fbSeric #ifdef NAMED_BIND
13722bcc6d2dSeric 	if (ConfigLevel < 2)
1373912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1374134746fbSeric #endif
13755dfc646bSeric 
1376b31e7f2bSeric 	/* arrange a return receipt if requested */
1377*df106f0bSeric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
1378*df106f0bSeric 	    bitnset(M_LOCALMAILER, m->m_flags))
1379b31e7f2bSeric 	{
1380b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1381b31e7f2bSeric 		/* do we want to send back more info? */
1382b31e7f2bSeric 	}
1383b31e7f2bSeric 
1384c77d1c25Seric 	/*
138563780dbdSeric 	**  Do final status disposal.
138663780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1387c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1388c77d1c25Seric 	**		addressees.
1389c77d1c25Seric 	*/
1390c77d1c25Seric 
1391b31e7f2bSeric   give_up:
139263780dbdSeric 	if (tobuf[0] != '\0')
139381161401Seric 		giveresponse(rcode, m, mci, e);
1394772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1395b31e7f2bSeric 	{
1396dde5acadSeric 		if (rcode != EX_OK)
139783b7ddc9Seric 			markfailure(e, to, rcode);
1398dde5acadSeric 		else
1399655518ecSeric 		{
1400dde5acadSeric 			to->q_flags |= QSENT;
1401655518ecSeric 			e->e_nsent++;
1402*df106f0bSeric 			if (e->e_receiptto != NULL &&
1403*df106f0bSeric 			    bitnset(M_LOCALMAILER, m->m_flags))
1404*df106f0bSeric 			{
1405*df106f0bSeric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
1406*df106f0bSeric 					to->q_paddr);
1407*df106f0bSeric 			}
1408655518ecSeric 		}
1409b31e7f2bSeric 	}
1410b31e7f2bSeric 
1411b31e7f2bSeric 	/*
1412b31e7f2bSeric 	**  Restore state and return.
1413b31e7f2bSeric 	*/
1414c77d1c25Seric 
14157febfd66Seric #ifdef XDEBUG
14167febfd66Seric 	{
14177febfd66Seric 		char wbuf[MAXLINE];
14187febfd66Seric 
14197febfd66Seric 		/* make absolutely certain 0, 1, and 2 are in use */
142044622ea3Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
142144622ea3Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
142244622ea3Seric 			m->m_name);
14237febfd66Seric 		checkfd012(wbuf);
14247febfd66Seric 	}
14257febfd66Seric #endif
14267febfd66Seric 
142735490626Seric 	errno = 0;
1428588cad61Seric 	define('g', (char *) NULL, e);
14295826d9d3Seric 	return (rcode);
143025a99e2eSeric }
14315dfc646bSeric /*
143283b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
143383b7ddc9Seric **
143483b7ddc9Seric **	Parameters:
143583b7ddc9Seric **		e -- the envelope we are sending.
143683b7ddc9Seric **		q -- the address to mark.
143783b7ddc9Seric **		rcode -- the code signifying the particular failure.
143883b7ddc9Seric **
143983b7ddc9Seric **	Returns:
144083b7ddc9Seric **		none.
144183b7ddc9Seric **
144283b7ddc9Seric **	Side Effects:
144383b7ddc9Seric **		marks the address (and possibly the envelope) with the
144483b7ddc9Seric **			failure so that an error will be returned or
144583b7ddc9Seric **			the message will be queued, as appropriate.
144683b7ddc9Seric */
144783b7ddc9Seric 
144883b7ddc9Seric markfailure(e, q, rcode)
144983b7ddc9Seric 	register ENVELOPE *e;
145083b7ddc9Seric 	register ADDRESS *q;
145183b7ddc9Seric 	int rcode;
145283b7ddc9Seric {
145319c47125Seric 	char buf[MAXLINE];
145419c47125Seric 
145583b7ddc9Seric 	if (rcode == EX_OK)
145683b7ddc9Seric 		return;
1457f170942cSeric 	else if (rcode == EX_TEMPFAIL)
145883b7ddc9Seric 		q->q_flags |= QQUEUEUP;
1459f170942cSeric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
1460f170942cSeric 		q->q_flags |= QBADADDR;
146183b7ddc9Seric }
146283b7ddc9Seric /*
1463c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1464c579ef51Seric **
1465c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1466c579ef51Seric **	violation), so we report those specially.  For other
1467c579ef51Seric **	errors, we choose a status message (into statmsg),
1468c579ef51Seric **	and if it represents an error, we print it.
1469c579ef51Seric **
1470c579ef51Seric **	Parameters:
1471c579ef51Seric **		pid -- pid of mailer.
1472c9be6216Seric **		e -- the current envelope.
1473c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1474c9be6216Seric **			(for error messages).
1475c579ef51Seric **
1476c579ef51Seric **	Returns:
1477c579ef51Seric **		exit code of mailer.
1478c579ef51Seric **
1479c579ef51Seric **	Side Effects:
1480c579ef51Seric **		none.
1481c579ef51Seric */
1482c579ef51Seric 
1483c9be6216Seric endmailer(mci, e, pv)
1484b31e7f2bSeric 	register MCI *mci;
1485c9be6216Seric 	register ENVELOPE *e;
1486c9be6216Seric 	char **pv;
1487c579ef51Seric {
1488588cad61Seric 	int st;
1489c579ef51Seric 
149075889e88Seric 	/* close any connections */
149175889e88Seric 	if (mci->mci_in != NULL)
1492c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
149375889e88Seric 	if (mci->mci_out != NULL)
1494c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
149575889e88Seric 	mci->mci_in = mci->mci_out = NULL;
149675889e88Seric 	mci->mci_state = MCIS_CLOSED;
149775889e88Seric 
149833db8731Seric 	/* in the IPC case there is nothing to wait for */
149975889e88Seric 	if (mci->mci_pid == 0)
150033db8731Seric 		return (EX_OK);
150133db8731Seric 
150233db8731Seric 	/* wait for the mailer process to die and collect status */
150375889e88Seric 	st = waitfor(mci->mci_pid);
1504588cad61Seric 	if (st == -1)
150578de67c1Seric 	{
1506c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1507588cad61Seric 		return (EX_SOFTWARE);
1508c579ef51Seric 	}
150933db8731Seric 
1510bf9bc890Seric 	if (WIFEXITED(st))
1511c579ef51Seric 	{
1512bf9bc890Seric 		/* normal death -- return status */
1513bf9bc890Seric 		return (WEXITSTATUS(st));
1514bf9bc890Seric 	}
1515bf9bc890Seric 
1516bf9bc890Seric 	/* it died a horrid death */
1517c9be6216Seric 	syserr("mailer %s died with signal %o", pv[0], st);
1518c9be6216Seric 
1519c9be6216Seric 	/* log the arguments */
1520c9be6216Seric 	if (e->e_xfp != NULL)
1521c9be6216Seric 	{
1522c9be6216Seric 		register char **av;
1523c9be6216Seric 
1524c9be6216Seric 		fprintf(e->e_xfp, "Arguments:");
1525c9be6216Seric 		for (av = pv; *av != NULL; av++)
1526c9be6216Seric 			fprintf(e->e_xfp, " %s", *av);
1527c9be6216Seric 		fprintf(e->e_xfp, "\n");
1528c9be6216Seric 	}
1529c9be6216Seric 
15305f73204aSeric 	ExitStat = EX_TEMPFAIL;
15315f73204aSeric 	return (EX_TEMPFAIL);
1532c579ef51Seric }
1533c579ef51Seric /*
153425a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
153525a99e2eSeric **
153625a99e2eSeric **	Parameters:
153725a99e2eSeric **		stat -- the status code from the mailer (high byte
153825a99e2eSeric **			only; core dumps must have been taken care of
153925a99e2eSeric **			already).
154081161401Seric **		m -- the mailer info for this mailer.
154181161401Seric **		mci -- the mailer connection info -- can be NULL if the
154281161401Seric **			response is given before the connection is made.
154381161401Seric **		e -- the current envelope.
154425a99e2eSeric **
154525a99e2eSeric **	Returns:
1546db8841e9Seric **		none.
154725a99e2eSeric **
154825a99e2eSeric **	Side Effects:
1549c1f9df2cSeric **		Errors may be incremented.
155025a99e2eSeric **		ExitStat may be set.
155125a99e2eSeric */
155225a99e2eSeric 
155381161401Seric giveresponse(stat, m, mci, e)
155425a99e2eSeric 	int stat;
1555588cad61Seric 	register MAILER *m;
155681161401Seric 	register MCI *mci;
1557198d9be0Seric 	ENVELOPE *e;
155825a99e2eSeric {
15599c16475dSeric 	register const char *statmsg;
156025a99e2eSeric 	extern char *SysExMsg[];
156125a99e2eSeric 	register int i;
1562d4bd8f0eSbostic 	extern int N_SysEx;
1563198d9be0Seric 	char buf[MAXLINE];
156425a99e2eSeric 
156513bbc08cSeric 	/*
156613bbc08cSeric 	**  Compute status message from code.
156713bbc08cSeric 	*/
156813bbc08cSeric 
156925a99e2eSeric 	i = stat - EX__BASE;
1570588cad61Seric 	if (stat == 0)
15716fe8c3bcSeric 	{
1572588cad61Seric 		statmsg = "250 Sent";
1573ce5531bdSeric 		if (e->e_statmsg != NULL)
15746fe8c3bcSeric 		{
1575ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15766fe8c3bcSeric 			statmsg = buf;
15776fe8c3bcSeric 		}
15786fe8c3bcSeric 	}
1579588cad61Seric 	else if (i < 0 || i > N_SysEx)
1580588cad61Seric 	{
1581588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1582588cad61Seric 		stat = EX_UNAVAILABLE;
1583588cad61Seric 		statmsg = buf;
1584588cad61Seric 	}
1585198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1586198d9be0Seric 	{
15877d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1588d4bd8f0eSbostic #ifdef NAMED_BIND
1589f28da541Smiriam 		if (h_errno == TRY_AGAIN)
15904d50702aSeric 			statmsg = errstring(h_errno+E_DNSBASE);
1591f28da541Smiriam 		else
1592d4bd8f0eSbostic #endif
1593f28da541Smiriam 		{
15948557d168Seric 			if (errno != 0)
1595d87e85f3Seric 				statmsg = errstring(errno);
1596d87e85f3Seric 			else
1597d87e85f3Seric 			{
1598d87e85f3Seric #ifdef SMTP
1599d87e85f3Seric 				extern char SmtpError[];
1600d87e85f3Seric 
1601d87e85f3Seric 				statmsg = SmtpError;
16026c2c3107Seric #else /* SMTP */
1603d87e85f3Seric 				statmsg = NULL;
16046c2c3107Seric #endif /* SMTP */
1605d87e85f3Seric 			}
1606f28da541Smiriam 		}
1607d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1608d87e85f3Seric 		{
160987c9b3e7Seric 			(void) strcat(buf, ": ");
1610d87e85f3Seric 			(void) strcat(buf, statmsg);
16118557d168Seric 		}
1612198d9be0Seric 		statmsg = buf;
1613198d9be0Seric 	}
1614f170942cSeric #ifdef NAMED_BIND
1615f170942cSeric 	else if (stat == EX_NOHOST && h_errno != 0)
1616f170942cSeric 	{
16174d50702aSeric 		statmsg = errstring(h_errno + E_DNSBASE);
1618f170942cSeric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
1619f170942cSeric 		statmsg = buf;
1620f170942cSeric 	}
1621f170942cSeric #endif
162225a99e2eSeric 	else
1623d87e85f3Seric 	{
162425a99e2eSeric 		statmsg = SysExMsg[i];
16257d55540cSeric 		if (*statmsg++ == ':')
16267d55540cSeric 		{
16277d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16287d55540cSeric 			statmsg = buf;
16297d55540cSeric 		}
1630d87e85f3Seric 	}
1631588cad61Seric 
1632588cad61Seric 	/*
1633588cad61Seric 	**  Print the message as appropriate
1634588cad61Seric 	*/
1635588cad61Seric 
1636198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1637*df106f0bSeric 	{
1638*df106f0bSeric 		extern char MsgBuf[];
1639*df106f0bSeric 
1640e12d03eeSeric 		message(&statmsg[4], errstring(errno));
1641*df106f0bSeric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
1642*df106f0bSeric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
1643*df106f0bSeric 	}
164425a99e2eSeric 	else
164525a99e2eSeric 	{
1646c1f9df2cSeric 		Errors++;
1647e12d03eeSeric 		usrerr(statmsg, errstring(errno));
164825a99e2eSeric 	}
164925a99e2eSeric 
165025a99e2eSeric 	/*
165125a99e2eSeric 	**  Final cleanup.
165225a99e2eSeric 	**	Log a record of the transaction.  Compute the new
165325a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
165425a99e2eSeric 	**	that.
165525a99e2eSeric 	*/
165625a99e2eSeric 
16572f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
165881161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1659eb238f8cSeric 
1660eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1661eb238f8cSeric 		setstat(stat);
1662198d9be0Seric 	if (stat != EX_OK)
1663198d9be0Seric 	{
1664198d9be0Seric 		if (e->e_message != NULL)
1665198d9be0Seric 			free(e->e_message);
1666198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1667198d9be0Seric 	}
16688557d168Seric 	errno = 0;
1669d4bd8f0eSbostic #ifdef NAMED_BIND
1670f28da541Smiriam 	h_errno = 0;
1671d4bd8f0eSbostic #endif
1672eb238f8cSeric }
1673eb238f8cSeric /*
1674eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1675eb238f8cSeric **
1676eb238f8cSeric **	Parameters:
167781161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
167881161401Seric **		mci -- the mailer connection info -- can be NULL if the
167981161401Seric **			log is occuring when no connection is active.
168081161401Seric **		stat -- the message to print for the status.
168181161401Seric **		e -- the current envelope.
1682eb238f8cSeric **
1683eb238f8cSeric **	Returns:
1684eb238f8cSeric **		none
1685eb238f8cSeric **
1686eb238f8cSeric **	Side Effects:
1687eb238f8cSeric **		none
1688eb238f8cSeric */
1689eb238f8cSeric 
169081161401Seric logdelivery(m, mci, stat, e)
169181161401Seric 	MAILER *m;
169281161401Seric 	register MCI *mci;
1693eb238f8cSeric 	char *stat;
1694b31e7f2bSeric 	register ENVELOPE *e;
16955cf56be3Seric {
1696eb238f8cSeric # ifdef LOG
1697d6acf3eeSeric 	char buf[512];
16989507d1f9Seric 
169948ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
170081161401Seric 
170148ed5d33Seric 	if (m != NULL)
170271ff6caaSeric 	{
170348ed5d33Seric 		(void) strcat(buf, ", mailer=");
170448ed5d33Seric 		(void) strcat(buf, m->m_name);
170571ff6caaSeric 	}
170648ed5d33Seric 
170748ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
170871ff6caaSeric 	{
170971ff6caaSeric # ifdef DAEMON
1710e2f2f828Seric 		extern SOCKADDR CurHostAddr;
171148ed5d33Seric # endif
171271ff6caaSeric 
171348ed5d33Seric 		(void) strcat(buf, ", relay=");
171448ed5d33Seric 		(void) strcat(buf, mci->mci_host);
171548ed5d33Seric 
171648ed5d33Seric # ifdef DAEMON
171748ed5d33Seric 		(void) strcat(buf, " (");
1718e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
171948ed5d33Seric 		(void) strcat(buf, ")");
172071ff6caaSeric # endif
172171ff6caaSeric 	}
17229507d1f9Seric 	else
172348ed5d33Seric 	{
172448ed5d33Seric 		char *p = macvalue('h', e);
17259507d1f9Seric 
172648ed5d33Seric 		if (p != NULL && p[0] != '\0')
172748ed5d33Seric 		{
172848ed5d33Seric 			(void) strcat(buf, ", relay=");
172948ed5d33Seric 			(void) strcat(buf, p);
173048ed5d33Seric 		}
173148ed5d33Seric 	}
1732d6acf3eeSeric 
1733d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1734d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
17356c2c3107Seric # endif /* LOG */
173625a99e2eSeric }
173725a99e2eSeric /*
173851552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
173925a99e2eSeric **
174051552439Seric **	This can be made an arbitrary message separator by changing $l
174151552439Seric **
17429b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
17439b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
17449b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
17459b6c17a6Seric **	this kind of antique garbage????
174625a99e2eSeric **
174725a99e2eSeric **	Parameters:
174851552439Seric **		fp -- the file to output to.
174951552439Seric **		m -- the mailer describing this entry.
175025a99e2eSeric **
175125a99e2eSeric **	Returns:
175251552439Seric **		none
175325a99e2eSeric **
175425a99e2eSeric **	Side Effects:
175551552439Seric **		outputs some text to fp.
175625a99e2eSeric */
175725a99e2eSeric 
1758b31e7f2bSeric putfromline(fp, m, e)
175951552439Seric 	register FILE *fp;
176051552439Seric 	register MAILER *m;
1761b31e7f2bSeric 	ENVELOPE *e;
176225a99e2eSeric {
17632bc47524Seric 	char *template = "\201l\n";
176451552439Seric 	char buf[MAXLINE];
176525a99e2eSeric 
176657fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
176751552439Seric 		return;
176813bbc08cSeric 
17692c7e1b8dSeric # ifdef UGLYUUCP
177057fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
177174b6e67bSeric 	{
1772ea09d6edSeric 		char *bang;
1773ea09d6edSeric 		char xbuf[MAXLINE];
177474b6e67bSeric 
1775ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17766c2c3107Seric 		bang = strchr(buf, '!');
177774b6e67bSeric 		if (bang == NULL)
177834fcca25Seric 		{
177934fcca25Seric 			errno = 0;
178034fcca25Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
178134fcca25Seric 		}
178274b6e67bSeric 		else
1783588cad61Seric 		{
1784ea09d6edSeric 			*bang++ = '\0';
17852bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1786ea09d6edSeric 			template = xbuf;
178774b6e67bSeric 		}
1788588cad61Seric 	}
17896c2c3107Seric # endif /* UGLYUUCP */
1790b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
179177b52738Seric 	putline(buf, fp, m);
1792bc6e2962Seric }
1793bc6e2962Seric /*
179451552439Seric **  PUTBODY -- put the body of a message.
179551552439Seric **
179651552439Seric **	Parameters:
179751552439Seric **		fp -- file to output onto.
179877b52738Seric **		m -- a mailer descriptor to control output format.
17999a6a5f55Seric **		e -- the envelope to put out.
180003c02fdeSeric **		separator -- if non-NULL, a message separator that must
180103c02fdeSeric **			not be permitted in the resulting message.
180251552439Seric **
180351552439Seric **	Returns:
180451552439Seric **		none.
180551552439Seric **
180651552439Seric **	Side Effects:
180751552439Seric **		The message is written onto fp.
180851552439Seric */
180951552439Seric 
181003c02fdeSeric putbody(fp, m, e, separator)
181151552439Seric 	FILE *fp;
1812588cad61Seric 	MAILER *m;
18139a6a5f55Seric 	register ENVELOPE *e;
181403c02fdeSeric 	char *separator;
181551552439Seric {
181677b52738Seric 	char buf[MAXLINE];
181751552439Seric 
181851552439Seric 	/*
181951552439Seric 	**  Output the body of the message
182051552439Seric 	*/
182151552439Seric 
18229a6a5f55Seric 	if (e->e_dfp == NULL)
182351552439Seric 	{
18249a6a5f55Seric 		if (e->e_df != NULL)
18259a6a5f55Seric 		{
18269a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
18279a6a5f55Seric 			if (e->e_dfp == NULL)
18288f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
1829e76a6a8fSeric 				e->e_df, e->e_to, e->e_from.q_paddr);
18309a6a5f55Seric 		}
18319a6a5f55Seric 		else
183277b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
18339a6a5f55Seric 	}
18349a6a5f55Seric 	if (e->e_dfp != NULL)
18359a6a5f55Seric 	{
18369a6a5f55Seric 		rewind(e->e_dfp);
183777b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
183824fc8aeeSeric 		{
183924fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1840d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
18413462ad9eSeric 				(void) putc('>', fp);
184203c02fdeSeric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
184303c02fdeSeric 			{
184403c02fdeSeric 				/* possible separator */
184503c02fdeSeric 				int sl = strlen(separator);
184603c02fdeSeric 
184703c02fdeSeric 				if (strncmp(&buf[2], separator, sl) == 0)
184803c02fdeSeric 					(void) putc(' ', fp);
184903c02fdeSeric 			}
185077b52738Seric 			putline(buf, fp, m);
185124fc8aeeSeric 		}
185251552439Seric 
18539a6a5f55Seric 		if (ferror(e->e_dfp))
185451552439Seric 		{
1855*df106f0bSeric 			syserr("putbody: %s: read error", e->e_df);
185651552439Seric 			ExitStat = EX_IOERR;
185751552439Seric 		}
185851552439Seric 	}
185951552439Seric 
18600890ba1fSeric 	/* some mailers want extra blank line at end of message */
18610890ba1fSeric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
18620890ba1fSeric 		putline("", fp, m);
18630890ba1fSeric 
186451552439Seric 	(void) fflush(fp);
186551552439Seric 	if (ferror(fp) && errno != EPIPE)
186651552439Seric 	{
186751552439Seric 		syserr("putbody: write error");
186851552439Seric 		ExitStat = EX_IOERR;
186951552439Seric 	}
187051552439Seric 	errno = 0;
187125a99e2eSeric }
187225a99e2eSeric /*
187325a99e2eSeric **  MAILFILE -- Send a message to a file.
187425a99e2eSeric **
1875f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1876f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1877f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1878f129ec7dSeric **	sendmail runs as root.
1879f129ec7dSeric **
1880588cad61Seric **	This could be done as a subordinate mailer, except that it
1881588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1882588cad61Seric **	view this as being sufficiently important as to include it
1883588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1884588cad61Seric **	to create another process plus some pipes to save the message.
1885588cad61Seric **
188625a99e2eSeric **	Parameters:
188725a99e2eSeric **		filename -- the name of the file to send to.
18886259796dSeric **		ctladdr -- the controlling address header -- includes
18896259796dSeric **			the userid/groupid to be when sending.
189025a99e2eSeric **
189125a99e2eSeric **	Returns:
189225a99e2eSeric **		The exit code associated with the operation.
189325a99e2eSeric **
189425a99e2eSeric **	Side Effects:
189525a99e2eSeric **		none.
189625a99e2eSeric */
189725a99e2eSeric 
1898b31e7f2bSeric mailfile(filename, ctladdr, e)
189925a99e2eSeric 	char *filename;
19006259796dSeric 	ADDRESS *ctladdr;
1901b31e7f2bSeric 	register ENVELOPE *e;
190225a99e2eSeric {
190325a99e2eSeric 	register FILE *f;
190432d19d43Seric 	register int pid;
190515d084d5Seric 	int mode;
190625a99e2eSeric 
1907671745f3Seric 	if (tTd(11, 1))
1908671745f3Seric 	{
1909671745f3Seric 		printf("mailfile %s\n  ctladdr=", filename);
1910671745f3Seric 		printaddr(ctladdr, FALSE);
1911671745f3Seric 	}
1912671745f3Seric 
1913f170942cSeric 	if (e->e_xfp != NULL)
1914f170942cSeric 		fflush(e->e_xfp);
1915f170942cSeric 
191632d19d43Seric 	/*
191732d19d43Seric 	**  Fork so we can change permissions here.
191832d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
191932d19d43Seric 	**	the complications of calling subroutines, etc.
192032d19d43Seric 	*/
192132d19d43Seric 
192232d19d43Seric 	DOFORK(fork);
192332d19d43Seric 
192432d19d43Seric 	if (pid < 0)
192532d19d43Seric 		return (EX_OSERR);
192632d19d43Seric 	else if (pid == 0)
192732d19d43Seric 	{
192832d19d43Seric 		/* child -- actually write to file */
1929f129ec7dSeric 		struct stat stb;
1930f129ec7dSeric 
19312b9178d3Seric 		(void) setsignal(SIGINT, SIG_DFL);
19322b9178d3Seric 		(void) setsignal(SIGHUP, SIG_DFL);
19332b9178d3Seric 		(void) setsignal(SIGTERM, SIG_DFL);
19343462ad9eSeric 		(void) umask(OldUmask);
193595f16dc0Seric 
1936f129ec7dSeric 		if (stat(filename, &stb) < 0)
19373a98e7eaSeric 			stb.st_mode = FileMode;
193815d084d5Seric 		mode = stb.st_mode;
193995f16dc0Seric 
194095f16dc0Seric 		/* limit the errors to those actually caused in the child */
194195f16dc0Seric 		errno = 0;
194295f16dc0Seric 		ExitStat = EX_OK;
194395f16dc0Seric 
1944f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1945f129ec7dSeric 			exit(EX_CANTCREAT);
194603827b5fSeric 		if (ctladdr == NULL)
19478f9146b0Srick 			ctladdr = &e->e_from;
194815d084d5Seric 		else
194915d084d5Seric 		{
195015d084d5Seric 			/* ignore setuid and setgid bits */
195115d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
195215d084d5Seric 		}
195315d084d5Seric 
19548f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
19558f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
19568f9146b0Srick 		{
19578f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
195895f16dc0Seric 			if (e->e_dfp == NULL)
195995f16dc0Seric 			{
19608f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
1961e76a6a8fSeric 					e->e_df, e->e_to, e->e_from.q_paddr);
19628f9146b0Srick 			}
19638f9146b0Srick 		}
19648f9146b0Srick 
196515d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1966e36b99e2Seric 		{
196795f16dc0Seric 			if (ctladdr->q_uid == 0)
196895f16dc0Seric 			{
1969898a126bSbostic 				(void) initgroups(DefUser, DefGid);
197095f16dc0Seric 			}
197195f16dc0Seric 			else
197295f16dc0Seric 			{
1973898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1974898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1975898a126bSbostic 					ctladdr->q_gid);
1976898a126bSbostic 			}
1977e36b99e2Seric 		}
197815d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1979e36b99e2Seric 		{
1980e36b99e2Seric 			if (ctladdr->q_uid == 0)
1981e36b99e2Seric 				(void) setuid(DefUid);
1982e36b99e2Seric 			else
19836259796dSeric 				(void) setuid(ctladdr->q_uid);
1984e36b99e2Seric 		}
198595f16dc0Seric 		FileName = filename;
198695f16dc0Seric 		LineNumber = 0;
19873a98e7eaSeric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
198825a99e2eSeric 		if (f == NULL)
198995f16dc0Seric 		{
1990b6a0de9dSeric 			message("554 cannot open: %s", errstring(errno));
199132d19d43Seric 			exit(EX_CANTCREAT);
199295f16dc0Seric 		}
199325a99e2eSeric 
19940331ce05Seric 		putfromline(f, FileMailer, e);
19950331ce05Seric 		(*e->e_puthdr)(f, FileMailer, e);
19960331ce05Seric 		putline("\n", f, FileMailer);
199703c02fdeSeric 		(*e->e_putbody)(f, FileMailer, e, NULL);
19980331ce05Seric 		putline("\n", f, FileMailer);
199995f16dc0Seric 		if (ferror(f))
200095f16dc0Seric 		{
2001b6a0de9dSeric 			message("451 I/O error: %s", errstring(errno));
200295f16dc0Seric 			setstat(EX_IOERR);
200395f16dc0Seric 		}
2004ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
200532d19d43Seric 		(void) fflush(stdout);
2006e36b99e2Seric 
200727628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
2008c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
200995f16dc0Seric 		exit(ExitStat);
201013bbc08cSeric 		/*NOTREACHED*/
201132d19d43Seric 	}
201232d19d43Seric 	else
201332d19d43Seric 	{
201432d19d43Seric 		/* parent -- wait for exit status */
2015588cad61Seric 		int st;
201632d19d43Seric 
2017588cad61Seric 		st = waitfor(pid);
2018bf9bc890Seric 		if (WIFEXITED(st))
2019bf9bc890Seric 			return (WEXITSTATUS(st));
2020588cad61Seric 		else
2021b6a0de9dSeric 		{
2022b6a0de9dSeric 			syserr("child died on signal %d", st);
2023bf9bc890Seric 			return (EX_UNAVAILABLE);
2024b6a0de9dSeric 		}
20258f9146b0Srick 		/*NOTREACHED*/
202632d19d43Seric 	}
202725a99e2eSeric }
2028ea4dc939Seric /*
2029e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
2030e103b48fSeric **
2031e103b48fSeric **	The signature describes how we are going to send this -- it
2032e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
2033e103b48fSeric **	an ordered list of MX hosts.
2034e103b48fSeric **
2035e103b48fSeric **	Parameters:
2036e103b48fSeric **		m -- the mailer describing this host.
2037e103b48fSeric **		host -- the host name.
2038e103b48fSeric **		e -- the current envelope.
2039e103b48fSeric **
2040e103b48fSeric **	Returns:
2041e103b48fSeric **		The signature for this host.
2042e103b48fSeric **
2043e103b48fSeric **	Side Effects:
2044e103b48fSeric **		Can tweak the symbol table.
2045e103b48fSeric */
2046e103b48fSeric 
2047e103b48fSeric char *
2048e103b48fSeric hostsignature(m, host, e)
2049e103b48fSeric 	register MAILER *m;
2050e103b48fSeric 	char *host;
2051e103b48fSeric 	ENVELOPE *e;
2052e103b48fSeric {
2053e103b48fSeric 	register char *p;
2054e103b48fSeric 	register STAB *s;
2055e103b48fSeric 	int i;
2056e103b48fSeric 	int len;
2057e103b48fSeric #ifdef NAMED_BIND
2058e103b48fSeric 	int nmx;
2059e103b48fSeric 	auto int rcode;
2060bafdc4e5Seric 	char *hp;
2061bafdc4e5Seric 	char *endp;
2062516782b4Seric 	int oldoptions;
2063e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2064e103b48fSeric #endif
2065e103b48fSeric 
2066e103b48fSeric 	/*
2067e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2068e103b48fSeric 	*/
2069e103b48fSeric 
2070e103b48fSeric 	p = m->m_mailer;
2071e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2072e103b48fSeric 	{
2073e103b48fSeric 		/* just an ordinary mailer */
2074e103b48fSeric 		return host;
2075e103b48fSeric 	}
2076e103b48fSeric 
2077e103b48fSeric 	/*
2078e103b48fSeric 	**  Look it up in the symbol table.
2079e103b48fSeric 	*/
2080e103b48fSeric 
2081e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2082e103b48fSeric 	if (s->s_hostsig != NULL)
2083e103b48fSeric 		return s->s_hostsig;
2084e103b48fSeric 
2085e103b48fSeric 	/*
2086e103b48fSeric 	**  Not already there -- create a signature.
2087e103b48fSeric 	*/
2088e103b48fSeric 
2089e103b48fSeric #ifdef NAMED_BIND
2090516782b4Seric 	if (ConfigLevel < 2)
2091516782b4Seric 	{
2092516782b4Seric 		oldoptions = _res.options;
2093516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2094516782b4Seric 	}
2095516782b4Seric 
2096bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2097bafdc4e5Seric 	{
2098bafdc4e5Seric 		endp = strchr(hp, ':');
2099bafdc4e5Seric 		if (endp != NULL)
2100bafdc4e5Seric 			*endp = '\0';
2101bafdc4e5Seric 
21027bf809e6Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
21037d55540cSeric 
2104e103b48fSeric 		if (nmx <= 0)
2105e103b48fSeric 		{
2106e103b48fSeric 			register MCI *mci;
2107e103b48fSeric 			extern int errno;
2108e103b48fSeric 
2109e103b48fSeric 			/* update the connection info for this host */
2110bafdc4e5Seric 			mci = mci_get(hp, m);
2111e103b48fSeric 			mci->mci_exitstat = rcode;
2112e103b48fSeric 			mci->mci_errno = errno;
2113f170942cSeric #ifdef NAMED_BIND
2114f170942cSeric 			mci->mci_herrno = h_errno;
2115f170942cSeric #endif
2116e103b48fSeric 
2117e103b48fSeric 			/* and return the original host name as the signature */
2118bafdc4e5Seric 			nmx = 1;
2119bafdc4e5Seric 			mxhosts[0] = hp;
2120e103b48fSeric 		}
2121e103b48fSeric 
2122e103b48fSeric 		len = 0;
2123e103b48fSeric 		for (i = 0; i < nmx; i++)
2124e103b48fSeric 		{
2125e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2126e103b48fSeric 		}
2127bafdc4e5Seric 		if (s->s_hostsig != NULL)
2128bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2129bafdc4e5Seric 		p = xalloc(len);
2130bafdc4e5Seric 		if (s->s_hostsig != NULL)
2131bafdc4e5Seric 		{
2132bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2133bafdc4e5Seric 			free(s->s_hostsig);
2134bafdc4e5Seric 			s->s_hostsig = p;
2135bafdc4e5Seric 			p += strlen(p);
2136bafdc4e5Seric 			*p++ = ':';
2137bafdc4e5Seric 		}
2138bafdc4e5Seric 		else
2139bafdc4e5Seric 			s->s_hostsig = p;
2140e103b48fSeric 		for (i = 0; i < nmx; i++)
2141e103b48fSeric 		{
2142e103b48fSeric 			if (i != 0)
2143e103b48fSeric 				*p++ = ':';
2144e103b48fSeric 			strcpy(p, mxhosts[i]);
2145e103b48fSeric 			p += strlen(p);
2146e103b48fSeric 		}
2147bafdc4e5Seric 		if (endp != NULL)
2148bafdc4e5Seric 			*endp++ = ':';
2149bafdc4e5Seric 	}
2150e103b48fSeric 	makelower(s->s_hostsig);
2151516782b4Seric 	if (ConfigLevel < 2)
2152516782b4Seric 		_res.options = oldoptions;
2153e103b48fSeric #else
2154e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2155e103b48fSeric 	s->s_hostsig = host;
2156e103b48fSeric #endif
2157e103b48fSeric 	if (tTd(17, 1))
2158e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2159e103b48fSeric 	return s->s_hostsig;
2160e103b48fSeric }
2161