14227346bSdist /*
20942ea6aSbostic  * Copyright (c) 1983 Eric P. Allman
3e70a7521Sbostic  * Copyright (c) 1988 Regents of the University of California.
4e70a7521Sbostic  * All rights reserved.
5e70a7521Sbostic  *
63bc94712Sbostic  * %sccs.include.redist.c%
74227346bSdist  */
84227346bSdist 
94227346bSdist #ifndef lint
10*efe54562Seric static char sccsid[] = "@(#)deliver.c	6.64 (Berkeley) 04/19/93";
11e70a7521Sbostic #endif /* not lint */
124227346bSdist 
13fcde9cc8Sbostic #include "sendmail.h"
14a8c080f0Seric #include <signal.h>
15c77d1c25Seric #include <sys/stat.h>
16f28da541Smiriam #include <netdb.h>
17911693bfSbostic #include <errno.h>
18134746fbSeric #ifdef NAMED_BIND
19912a731aSbostic #include <arpa/nameser.h>
20912a731aSbostic #include <resolv.h>
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 
529c9e68d9Seric 	/* determine actual delivery mode */
539c9e68d9Seric 	if (mode == SM_DEFAULT)
549c9e68d9Seric 	{
559c9e68d9Seric 		extern bool shouldqueue();
569c9e68d9Seric 
579c9e68d9Seric 		mode = e->e_sendmode;
589c9e68d9Seric 		if (mode != SM_VERIFY &&
599c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
609c9e68d9Seric 			mode = SM_QUEUE;
616103d9b4Seric 		announcequeueup = mode == SM_QUEUE;
629c9e68d9Seric 	}
636103d9b4Seric 	else
646103d9b4Seric 		announcequeueup = FALSE;
659c9e68d9Seric 
669c9e68d9Seric 	if (tTd(13, 1))
679c9e68d9Seric 	{
689c9e68d9Seric 		printf("\nSENDALL: mode %c, e_from ", mode);
699c9e68d9Seric 		printaddr(&e->e_from, FALSE);
709c9e68d9Seric 		printf("sendqueue:\n");
719c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
729c9e68d9Seric 	}
739c9e68d9Seric 
749c9e68d9Seric 	/*
759c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
769c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
779c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
789c9e68d9Seric 	*/
799c9e68d9Seric 
809c9e68d9Seric 	CurEnv = e;
819c9e68d9Seric 
829c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
839c9e68d9Seric 	{
849c9e68d9Seric 		errno = 0;
859c9e68d9Seric 		syserr("554 too many hops %d (%d max): from %s, to %s",
869c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
879c9e68d9Seric 			e->e_sendqueue->q_paddr);
889c9e68d9Seric 		return;
899c9e68d9Seric 	}
909c9e68d9Seric 
919c9e68d9Seric 	if (!MeToo)
929c9e68d9Seric 	{
939c9e68d9Seric 		extern ADDRESS *recipient();
949c9e68d9Seric 
959c9e68d9Seric 		if (tTd(13, 5))
969c9e68d9Seric 		{
979c9e68d9Seric 			printf("sendall: QDONTSEND ");
989c9e68d9Seric 			printaddr(&e->e_from, FALSE);
999c9e68d9Seric 		}
1009c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
1019c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
1029c9e68d9Seric 	}
1039c9e68d9Seric 
104ce5531bdSeric 	/*
105ce5531bdSeric 	**  Handle alias owners.
106ce5531bdSeric 	**
107ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
108ce5531bdSeric 	**	We discard owners that are the same as the return path.
109ce5531bdSeric 	*/
110ce5531bdSeric 
111ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
112ce5531bdSeric 	{
113ce5531bdSeric 		register struct address *a;
114ce5531bdSeric 
115ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
116ce5531bdSeric 			continue;
117ce5531bdSeric 		if (a != NULL)
118ce5531bdSeric 			q->q_owner = a->q_owner;
119ce5531bdSeric 
120ce5531bdSeric 		if (q->q_owner != NULL &&
121ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
122ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
123ce5531bdSeric 			q->q_owner = NULL;
124ce5531bdSeric 	}
125ce5531bdSeric 
126ce5531bdSeric 	owner = "";
127ce5531bdSeric 	otherowners = 1;
128ce5531bdSeric 	while (owner != NULL && otherowners > 0)
129ce5531bdSeric 	{
130ce5531bdSeric 		owner = NULL;
131ce5531bdSeric 		otherowners = 0;
132ce5531bdSeric 
133ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
134ce5531bdSeric 		{
135ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
136ce5531bdSeric 				continue;
137ce5531bdSeric 
138ce5531bdSeric 			if (q->q_owner != NULL)
139ce5531bdSeric 			{
140ce5531bdSeric 				if (owner == NULL)
141ce5531bdSeric 					owner = q->q_owner;
142ce5531bdSeric 				else if (owner != q->q_owner)
143ce5531bdSeric 				{
144ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
145ce5531bdSeric 					{
146ce5531bdSeric 						/* make future comparisons cheap */
147ce5531bdSeric 						q->q_owner = owner;
148ce5531bdSeric 					}
149ce5531bdSeric 					else
150ce5531bdSeric 					{
151ce5531bdSeric 						otherowners++;
152ce5531bdSeric 					}
153ce5531bdSeric 					owner = q->q_owner;
154ce5531bdSeric 				}
155ce5531bdSeric 			}
156ce5531bdSeric 			else
157ce5531bdSeric 			{
158ce5531bdSeric 				otherowners++;
159ce5531bdSeric 			}
160ce5531bdSeric 		}
161ce5531bdSeric 
162ce5531bdSeric 		if (owner != NULL && otherowners > 0)
163ce5531bdSeric 		{
164ce5531bdSeric 			extern HDR *copyheader();
165ce5531bdSeric 			extern ADDRESS *copyqueue();
166ce5531bdSeric 
167ce5531bdSeric 			/*
168ce5531bdSeric 			**  Split this envelope into two.
169ce5531bdSeric 			*/
170ce5531bdSeric 
171ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
172ce5531bdSeric 			*ee = *e;
173ce5531bdSeric 			ee->e_id = NULL;
174ce5531bdSeric 			(void) queuename(ee, '\0');
175ce5531bdSeric 
176ce5531bdSeric 			if (tTd(13, 1))
177ce5531bdSeric 				printf("sendall: split %s into %s\n",
178ce5531bdSeric 					e->e_id, ee->e_id);
179ce5531bdSeric 
180ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
181ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
182ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
183ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
184ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
185ce5531bdSeric 			if (tTd(13, 5))
186ce5531bdSeric 			{
187ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
188ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
189ce5531bdSeric 			}
190ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
191ce5531bdSeric 			ee->e_dfp = NULL;
192ce5531bdSeric 			ee->e_xfp = NULL;
193ce5531bdSeric 			ee->e_lockfp = NULL;
194ce5531bdSeric 			ee->e_df = NULL;
195ce5531bdSeric 			ee->e_errormode = EM_MAIL;
196ce5531bdSeric 			ee->e_sibling = splitenv;
197ce5531bdSeric 			splitenv = ee;
198ce5531bdSeric 
199ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
200ce5531bdSeric 				if (q->q_owner == owner)
201ce5531bdSeric 					q->q_flags |= QDONTSEND;
202ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
203ce5531bdSeric 				if (q->q_owner != owner)
204ce5531bdSeric 					q->q_flags |= QDONTSEND;
205ce5531bdSeric 
206ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
207ce5531bdSeric 			{
208ce5531bdSeric 				ee->e_dfp = NULL;
209ce5531bdSeric 				ee->e_df = newstr(queuename(ee, 'd'));
210ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
211ce5531bdSeric 				{
212ce5531bdSeric 					syserr("sendall: link(%s, %s)",
213ce5531bdSeric 						e->e_df, ee->e_df);
214ce5531bdSeric 				}
215ce5531bdSeric 			}
216ce5531bdSeric 
217ce5531bdSeric 			if (mode != SM_VERIFY)
218ce5531bdSeric 				openxscript(ee);
219ce5531bdSeric #ifdef LOG
220ce5531bdSeric 			if (LogLevel > 4)
221ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
222ce5531bdSeric 					ee->e_id, e->e_id);
223ce5531bdSeric #endif
224ce5531bdSeric 		}
225ce5531bdSeric 	}
226ce5531bdSeric 
227ce5531bdSeric 	if (owner != NULL)
228ce5531bdSeric 	{
229ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
230ce5531bdSeric 		if (tTd(13, 5))
231ce5531bdSeric 		{
232ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
233ce5531bdSeric 			printaddr(&e->e_from, FALSE);
234ce5531bdSeric 		}
235ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
236ce5531bdSeric 		e->e_errormode = EM_MAIL;
237ce5531bdSeric 	}
238ce5531bdSeric 
239c1ac89b1Seric # ifdef QUEUE
240c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
241c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
242c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
243c1ac89b1Seric 	{
244c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
2456103d9b4Seric 		queueup(e, TRUE, announcequeueup);
246ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2476103d9b4Seric 			queueup(ee, TRUE, announcequeueup);
248c1ac89b1Seric 	}
249c1ac89b1Seric #endif /* QUEUE */
250c1ac89b1Seric 
251ce5531bdSeric 	if (splitenv != NULL)
252ce5531bdSeric 	{
253ce5531bdSeric 		if (tTd(13, 1))
254ce5531bdSeric 		{
255ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
256ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
257ce5531bdSeric 		}
258ce5531bdSeric 
259ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
260ce5531bdSeric 		{
261ce5531bdSeric 			CurEnv = ee;
262ce5531bdSeric 			sendenvelope(ee, mode);
263ce5531bdSeric 		}
264ce5531bdSeric 
265ce5531bdSeric 		CurEnv = e;
266ce5531bdSeric 	}
267ce5531bdSeric 	sendenvelope(e, mode);
268ce5531bdSeric 
269ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
270ce5531bdSeric 		dropenvelope(splitenv);
271ce5531bdSeric }
272ce5531bdSeric 
273ce5531bdSeric sendenvelope(e, mode)
274ce5531bdSeric 	register ENVELOPE *e;
275ce5531bdSeric 	char mode;
276ce5531bdSeric {
277ce5531bdSeric 	bool oldverbose;
278ce5531bdSeric 	int pid;
279ce5531bdSeric 	register ADDRESS *q;
280ce5531bdSeric #ifdef LOCKF
281ce5531bdSeric 	struct flock lfd;
282ce5531bdSeric #endif
283ce5531bdSeric 
284ce5531bdSeric 	oldverbose = Verbose;
285c1ac89b1Seric 	switch (mode)
286c1ac89b1Seric 	{
287c1ac89b1Seric 	  case SM_VERIFY:
288c1ac89b1Seric 		Verbose = TRUE;
289c1ac89b1Seric 		break;
290c1ac89b1Seric 
291c1ac89b1Seric 	  case SM_QUEUE:
292c1ac89b1Seric   queueonly:
293c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
294c1ac89b1Seric 		return;
295c1ac89b1Seric 
296c1ac89b1Seric 	  case SM_FORK:
297c1ac89b1Seric 		if (e->e_xfp != NULL)
298c1ac89b1Seric 			(void) fflush(e->e_xfp);
299c1ac89b1Seric 
300c1ac89b1Seric # ifdef LOCKF
301c1ac89b1Seric 		/*
302c1ac89b1Seric 		**  Since lockf has the interesting semantic that the
303c1ac89b1Seric 		**  lock is lost when we fork, we have to risk losing
304c1ac89b1Seric 		**  the lock here by closing before the fork, and then
305c1ac89b1Seric 		**  trying to get it back in the child.
306c1ac89b1Seric 		*/
307c1ac89b1Seric 
308c1ac89b1Seric 		if (e->e_lockfp != NULL)
309c1ac89b1Seric 		{
310c1ac89b1Seric 			(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
311c1ac89b1Seric 			e->e_lockfp = NULL;
312c1ac89b1Seric 		}
313c1ac89b1Seric # endif /* LOCKF */
314c1ac89b1Seric 
315c1ac89b1Seric 		pid = fork();
316c1ac89b1Seric 		if (pid < 0)
317c1ac89b1Seric 		{
318c1ac89b1Seric 			goto queueonly;
319c1ac89b1Seric 		}
320c1ac89b1Seric 		else if (pid > 0)
321c1ac89b1Seric 		{
322c1ac89b1Seric 			/* be sure we leave the temp files to our child */
323c1ac89b1Seric 			e->e_id = e->e_df = NULL;
324c1ac89b1Seric # ifndef LOCKF
325c1ac89b1Seric 			if (e->e_lockfp != NULL)
326c1ac89b1Seric 			{
327c1ac89b1Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
328c1ac89b1Seric 				e->e_lockfp = NULL;
329c1ac89b1Seric 			}
330c1ac89b1Seric # endif
331c1ac89b1Seric 
332c1ac89b1Seric 			/* close any random open files in the envelope */
333c1ac89b1Seric 			if (e->e_dfp != NULL)
334c1ac89b1Seric 			{
335c1ac89b1Seric 				(void) xfclose(e->e_dfp, "sendenvelope", "dfp");
336c1ac89b1Seric 				e->e_dfp = NULL;
337c1ac89b1Seric 			}
338c1ac89b1Seric 			if (e->e_xfp != NULL)
339c1ac89b1Seric 			{
340c1ac89b1Seric 				(void) xfclose(e->e_xfp, "sendenvelope", "xfp");
341c1ac89b1Seric 				e->e_xfp = NULL;
342c1ac89b1Seric 			}
343c1ac89b1Seric 			return;
344c1ac89b1Seric 		}
345c1ac89b1Seric 
346c1ac89b1Seric 		/* double fork to avoid zombies */
347c1ac89b1Seric 		if (fork() > 0)
348c1ac89b1Seric 			exit(EX_OK);
349c1ac89b1Seric 
350c1ac89b1Seric 		/* be sure we are immune from the terminal */
351c1ac89b1Seric 		disconnect(FALSE, e);
352c1ac89b1Seric 
353c1ac89b1Seric # ifdef LOCKF
354c1ac89b1Seric 		/*
355c1ac89b1Seric 		**  Now try to get our lock back.
356c1ac89b1Seric 		*/
357c1ac89b1Seric 
358c1ac89b1Seric 		lfd.l_type = F_WRLCK;
359c1ac89b1Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
360c1ac89b1Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
361c1ac89b1Seric 		if (e->e_lockfp == NULL ||
362c1ac89b1Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
363c1ac89b1Seric 		{
364c1ac89b1Seric 			/* oops....  lost it */
365c1ac89b1Seric 			if (tTd(13, 1))
366c1ac89b1Seric 				printf("sendenvelope: %s lost lock: lockfp=%x, %s\n",
367c1ac89b1Seric 					e->e_id, e->e_lockfp, errstring(errno));
368c1ac89b1Seric 
369c1ac89b1Seric # ifdef LOG
370c1ac89b1Seric 			if (LogLevel > 29)
371c1ac89b1Seric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
372c1ac89b1Seric 					e->e_id);
373c1ac89b1Seric # endif /* LOG */
374c1ac89b1Seric 			exit(EX_OK);
375c1ac89b1Seric 		}
376c1ac89b1Seric # endif /* LOCKF */
377c1ac89b1Seric 
378c1ac89b1Seric 		/*
379c1ac89b1Seric 		**  Close any cached connections.
380c1ac89b1Seric 		**
381c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
382c1ac89b1Seric 		**	still knows about the connection.
383c1ac89b1Seric 		**
384c1ac89b1Seric 		**	This should only happen when delivering an error
385c1ac89b1Seric 		**	message.
386c1ac89b1Seric 		*/
387c1ac89b1Seric 
388c1ac89b1Seric 		mci_flush(FALSE, NULL);
389c1ac89b1Seric 
390c1ac89b1Seric 		break;
391c1ac89b1Seric 	}
392c1ac89b1Seric 
393c1ac89b1Seric 	/*
3949c9e68d9Seric 	**  Run through the list and send everything.
3959c9e68d9Seric 	*/
3969c9e68d9Seric 
3979c9e68d9Seric 	e->e_nsent = 0;
3989c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
3999c9e68d9Seric 	{
4009c9e68d9Seric 		if (mode == SM_VERIFY)
4019c9e68d9Seric 		{
4029c9e68d9Seric 			e->e_to = q->q_paddr;
4039c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4049c9e68d9Seric 				message("deliverable");
4059c9e68d9Seric 		}
4069c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4079c9e68d9Seric 		{
4089c9e68d9Seric # ifdef QUEUE
4099c9e68d9Seric 			/*
4109c9e68d9Seric 			**  Checkpoint the send list every few addresses
4119c9e68d9Seric 			*/
4129c9e68d9Seric 
4139c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4149c9e68d9Seric 			{
4159c9e68d9Seric 				queueup(e, TRUE, FALSE);
4169c9e68d9Seric 				e->e_nsent = 0;
4179c9e68d9Seric 			}
4189c9e68d9Seric # endif /* QUEUE */
4199c9e68d9Seric 			(void) deliver(e, q);
4209c9e68d9Seric 		}
4219c9e68d9Seric 	}
4229c9e68d9Seric 	Verbose = oldverbose;
4239c9e68d9Seric 
4249c9e68d9Seric 	/*
4259c9e68d9Seric 	**  Now run through and check for errors.
4269c9e68d9Seric 	*/
4279c9e68d9Seric 
4289c9e68d9Seric 	if (mode == SM_VERIFY)
4299c9e68d9Seric 	{
4309c9e68d9Seric 		return;
4319c9e68d9Seric 	}
4329c9e68d9Seric 
4339c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4349c9e68d9Seric 	{
4359c9e68d9Seric 		if (tTd(13, 3))
4369c9e68d9Seric 		{
4379c9e68d9Seric 			printf("Checking ");
4389c9e68d9Seric 			printaddr(q, FALSE);
4399c9e68d9Seric 		}
4409c9e68d9Seric 
4419c9e68d9Seric 		/* only send errors if the message failed */
442ce5531bdSeric 		if (!bitset(QBADADDR, q->q_flags) ||
443ce5531bdSeric 		    bitset(QDONTSEND, q->q_flags))
4449c9e68d9Seric 			continue;
4459c9e68d9Seric 
4469c9e68d9Seric 		e->e_flags |= EF_FATALERRS;
4479c9e68d9Seric 
4489c9e68d9Seric 		if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0)
4499c9e68d9Seric 			(void) sendtolist(e->e_from.q_paddr, NULL,
4509c9e68d9Seric 					  &e->e_errorqueue, e);
4519c9e68d9Seric 	}
4529c9e68d9Seric 
4539c9e68d9Seric 	if (mode == SM_FORK)
4549c9e68d9Seric 		finis();
4559c9e68d9Seric }
4569c9e68d9Seric /*
4579c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4589c9e68d9Seric **
4599c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4609c9e68d9Seric **	two processes on the same stack!!!
4619c9e68d9Seric **
4629c9e68d9Seric **	Parameters:
4639c9e68d9Seric **		none.
4649c9e68d9Seric **
4659c9e68d9Seric **	Returns:
4669c9e68d9Seric **		From a macro???  You've got to be kidding!
4679c9e68d9Seric **
4689c9e68d9Seric **	Side Effects:
4699c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4709c9e68d9Seric **			pid of child in parent, zero in child.
4719c9e68d9Seric **			-1 on unrecoverable error.
4729c9e68d9Seric **
4739c9e68d9Seric **	Notes:
4749c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4759c9e68d9Seric **		vfork for you.....
4769c9e68d9Seric */
4779c9e68d9Seric 
4789c9e68d9Seric # define NFORKTRIES	5
4799c9e68d9Seric 
4809c9e68d9Seric # ifndef FORK
4819c9e68d9Seric # define FORK	fork
4829c9e68d9Seric # endif
4839c9e68d9Seric 
4849c9e68d9Seric # define DOFORK(fORKfN) \
4859c9e68d9Seric {\
4869c9e68d9Seric 	register int i;\
4879c9e68d9Seric \
4889c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4899c9e68d9Seric 	{\
4909c9e68d9Seric 		pid = fORKfN();\
4919c9e68d9Seric 		if (pid >= 0)\
4929c9e68d9Seric 			break;\
4939c9e68d9Seric 		if (i > 0)\
4949c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
4959c9e68d9Seric 	}\
4969c9e68d9Seric }
4979c9e68d9Seric /*
4989c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
4999c9e68d9Seric **
5009c9e68d9Seric **	Parameters:
5019c9e68d9Seric **		none.
5029c9e68d9Seric **
5039c9e68d9Seric **	Returns:
5049c9e68d9Seric **		pid of child in parent.
5059c9e68d9Seric **		zero in child.
5069c9e68d9Seric **		-1 on error.
5079c9e68d9Seric **
5089c9e68d9Seric **	Side Effects:
5099c9e68d9Seric **		returns twice, once in parent and once in child.
5109c9e68d9Seric */
5119c9e68d9Seric 
5129c9e68d9Seric dofork()
5139c9e68d9Seric {
5149c9e68d9Seric 	register int pid;
5159c9e68d9Seric 
5169c9e68d9Seric 	DOFORK(fork);
5179c9e68d9Seric 	return (pid);
5189c9e68d9Seric }
5199c9e68d9Seric /*
52013bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
52113bbc08cSeric **
52213bbc08cSeric **	This routine delivers to everyone on the same host as the
52313bbc08cSeric **	user on the head of the list.  It is clever about mailers
52413bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
52513bbc08cSeric **	that it will deliver to all these addresses however -- so
52613bbc08cSeric **	deliver should be called once for each address on the
52713bbc08cSeric **	list.
52825a99e2eSeric **
52925a99e2eSeric **	Parameters:
530588cad61Seric **		e -- the envelope to deliver.
531c77d1c25Seric **		firstto -- head of the address list to deliver to.
53225a99e2eSeric **
53325a99e2eSeric **	Returns:
53425a99e2eSeric **		zero -- successfully delivered.
53525a99e2eSeric **		else -- some failure, see ExitStat for more info.
53625a99e2eSeric **
53725a99e2eSeric **	Side Effects:
53825a99e2eSeric **		The standard input is passed off to someone.
53925a99e2eSeric */
54025a99e2eSeric 
541588cad61Seric deliver(e, firstto)
542588cad61Seric 	register ENVELOPE *e;
543c77d1c25Seric 	ADDRESS *firstto;
54425a99e2eSeric {
54578442df3Seric 	char *host;			/* host being sent to */
54678442df3Seric 	char *user;			/* user being sent to */
54725a99e2eSeric 	char **pvp;
5485dfc646bSeric 	register char **mvp;
54925a99e2eSeric 	register char *p;
550588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5516259796dSeric 	ADDRESS *ctladdr;
552b31e7f2bSeric 	register MCI *mci;
553c77d1c25Seric 	register ADDRESS *to = firstto;
554c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
555772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
556911693bfSbostic 	int rcode;			/* response code */
557e103b48fSeric 	char *firstsig;			/* signature of firstto */
5589c9e68d9Seric 	int pid;
5599c9e68d9Seric 	char *curhost;
5609c9e68d9Seric 	int mpvect[2];
5619c9e68d9Seric 	int rpvect[2];
562ee6bf8dfSeric 	char *pv[MAXPV+1];
563579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
564ee6bf8dfSeric 	char buf[MAXNAME];
565c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
566fabb3bd4Seric 	extern int checkcompat();
567ee6bf8dfSeric 	extern ADDRESS *getctladdr();
568ee6bf8dfSeric 	extern char *remotename();
569e103b48fSeric 	extern char *hostsignature();
5709c9e68d9Seric 	extern FILE *fdopen();
57125a99e2eSeric 
57235490626Seric 	errno = 0;
573ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5745dfc646bSeric 		return (0);
57525a99e2eSeric 
576134746fbSeric #ifdef NAMED_BIND
577912a731aSbostic 	/* unless interactive, try twice, over a minute */
578912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
579912a731aSbostic 		_res.retrans = 30;
580912a731aSbostic 		_res.retry = 2;
581912a731aSbostic 	}
582d4bd8f0eSbostic #endif
583912a731aSbostic 
58451552439Seric 	m = to->q_mailer;
58551552439Seric 	host = to->q_host;
586c9be6216Seric 	CurEnv = e;			/* just in case */
5874384d521Seric 	e->e_statmsg = NULL;
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 
60319c47125Seric 	if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) &&
60419c47125Seric 	    bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
605f3dbc832Seric 	{
606f3dbc832Seric 		for (; to != NULL; to = to->q_next)
607f4560e80Seric 		{
608ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
609c6e18ac5Seric 			    to->q_mailer != m)
610f4560e80Seric 				continue;
611f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
612588cad61Seric 			e->e_to = to->q_paddr;
61308b25121Seric 			message("queued");
6142f624c86Seric 			if (LogLevel > 8)
61581161401Seric 				logdelivery(m, NULL, "queued", e);
616f4560e80Seric 		}
617588cad61Seric 		e->e_to = NULL;
618f3dbc832Seric 		return (0);
619f3dbc832Seric 	}
620f3dbc832Seric 
62125a99e2eSeric 	/*
6225dfc646bSeric 	**  Do initial argv setup.
6235dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6245dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6255dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6265dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6275dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6283bea8136Seric 	**		The from address rewrite is expected to make
6293bea8136Seric 	**		the address relative to the other end.
6305dfc646bSeric 	*/
6315dfc646bSeric 
63278442df3Seric 	/* rewrite from address, using rewriting rules */
633*efe54562Seric 	rcode = EX_OK;
634*efe54562Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
635*efe54562Seric 					   RF_SENDERADDR|RF_CANONICAL,
636*efe54562Seric 					   &rcode, e));
637ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
638588cad61Seric 	define('h', host, e);			/* to host */
6395dfc646bSeric 	Errors = 0;
6405dfc646bSeric 	pvp = pv;
6415dfc646bSeric 	*pvp++ = m->m_argv[0];
6425dfc646bSeric 
6435dfc646bSeric 	/* insert -f or -r flag as appropriate */
64457fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6455dfc646bSeric 	{
64657fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6475dfc646bSeric 			*pvp++ = "-f";
6485dfc646bSeric 		else
6495dfc646bSeric 			*pvp++ = "-r";
650c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6515dfc646bSeric 	}
6525dfc646bSeric 
6535dfc646bSeric 	/*
6545dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6555dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6565dfc646bSeric 	**  be one of these, and there are only a few more slots
6575dfc646bSeric 	**  in the pv after it.
6585dfc646bSeric 	*/
6595dfc646bSeric 
6605dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6615dfc646bSeric 	{
6622bc47524Seric 		/* can't use strchr here because of sign extension problems */
6632bc47524Seric 		while (*p != '\0')
6642bc47524Seric 		{
6652bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6662bc47524Seric 			{
6672bc47524Seric 				if (*p == 'u')
6685dfc646bSeric 					break;
6692bc47524Seric 			}
6702bc47524Seric 		}
6712bc47524Seric 
6722bc47524Seric 		if (*p != '\0')
6735dfc646bSeric 			break;
6745dfc646bSeric 
6755dfc646bSeric 		/* this entry is safe -- go ahead and process it */
676588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6775dfc646bSeric 		*pvp++ = newstr(buf);
6785dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6795dfc646bSeric 		{
68008b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6815dfc646bSeric 			return (-1);
6825dfc646bSeric 		}
6835dfc646bSeric 	}
684c579ef51Seric 
68533db8731Seric 	/*
68633db8731Seric 	**  If we have no substitution for the user name in the argument
68733db8731Seric 	**  list, we know that we must supply the names otherwise -- and
68833db8731Seric 	**  SMTP is the answer!!
68933db8731Seric 	*/
69033db8731Seric 
6915dfc646bSeric 	if (*mvp == NULL)
692c579ef51Seric 	{
693c579ef51Seric 		/* running SMTP */
6942c7e1b8dSeric # ifdef SMTP
695c579ef51Seric 		clever = TRUE;
696c579ef51Seric 		*pvp = NULL;
6976c2c3107Seric # else /* SMTP */
69833db8731Seric 		/* oops!  we don't implement SMTP */
69908b25121Seric 		syserr("554 SMTP style mailer");
7002c7e1b8dSeric 		return (EX_SOFTWARE);
7016c2c3107Seric # endif /* SMTP */
702c579ef51Seric 	}
7035dfc646bSeric 
7045dfc646bSeric 	/*
7055dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7065dfc646bSeric 	**  run through our address list and append all the addresses
7075dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7085dfc646bSeric 	**  always send another copy later.
7095dfc646bSeric 	*/
7105dfc646bSeric 
7115dfc646bSeric 	tobuf[0] = '\0';
712588cad61Seric 	e->e_to = tobuf;
7136259796dSeric 	ctladdr = NULL;
714e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7155dfc646bSeric 	for (; to != NULL; to = to->q_next)
7165dfc646bSeric 	{
7175dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
71857fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7195dfc646bSeric 			break;
7205dfc646bSeric 
7215dfc646bSeric 		/* if already sent or not for this host, don't send */
722ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
723e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
724e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7255dfc646bSeric 			continue;
7266259796dSeric 
7274b22ea87Seric 		/* avoid overflowing tobuf */
728aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7294b22ea87Seric 			break;
7304b22ea87Seric 
7316ef48975Seric 		if (tTd(10, 1))
732772e6e50Seric 		{
733772e6e50Seric 			printf("\nsend to ");
734772e6e50Seric 			printaddr(to, FALSE);
735772e6e50Seric 		}
736772e6e50Seric 
7376259796dSeric 		/* compute effective uid/gid when sending */
7387da1035fSeric 		if (to->q_mailer == ProgMailer)
7396259796dSeric 			ctladdr = getctladdr(to);
7406259796dSeric 
7415dfc646bSeric 		user = to->q_user;
742588cad61Seric 		e->e_to = to->q_paddr;
74375f1ade9Seric 		if (tTd(10, 5))
74475f1ade9Seric 		{
74575f1ade9Seric 			printf("deliver: QDONTSEND ");
74675f1ade9Seric 			printaddr(to, FALSE);
74775f1ade9Seric 		}
748ee4b0922Seric 		to->q_flags |= QDONTSEND;
7495dfc646bSeric 
7505dfc646bSeric 		/*
7515dfc646bSeric 		**  Check to see that these people are allowed to
7525dfc646bSeric 		**  talk to each other.
7532a6e0786Seric 		*/
7542a6e0786Seric 
75569582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
75669582d2fSeric 		{
75769582d2fSeric 			NoReturn = TRUE;
75808b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
75981161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
76069582d2fSeric 			continue;
76169582d2fSeric 		}
762fabb3bd4Seric 		rcode = checkcompat(to, e);
7631793c9c5Seric 		if (rcode != EX_OK)
7645dfc646bSeric 		{
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 */
829588cad61Seric 		define('z', to->q_home, e);	/* user's home */
8305dfc646bSeric 
831c579ef51Seric 		/*
832508daeccSeric 		**  Expand out this user into argument list.
833c579ef51Seric 		*/
834c579ef51Seric 
835508daeccSeric 		if (!clever)
836c579ef51Seric 		{
837588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8385dfc646bSeric 			*pvp++ = newstr(buf);
8395dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8405dfc646bSeric 			{
8415dfc646bSeric 				/* allow some space for trailing parms */
8425dfc646bSeric 				break;
8435dfc646bSeric 			}
8445dfc646bSeric 		}
845c579ef51Seric 	}
8465dfc646bSeric 
847145b49b1Seric 	/* see if any addresses still exist */
848145b49b1Seric 	if (tobuf[0] == '\0')
849c579ef51Seric 	{
850588cad61Seric 		define('g', (char *) NULL, e);
851145b49b1Seric 		return (0);
852c579ef51Seric 	}
853145b49b1Seric 
8545dfc646bSeric 	/* print out messages as full list */
85563780dbdSeric 	e->e_to = tobuf + 1;
8565dfc646bSeric 
8575dfc646bSeric 	/*
8585dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8595dfc646bSeric 	*/
8605dfc646bSeric 
861c579ef51Seric 	while (!clever && *++mvp != NULL)
8625dfc646bSeric 	{
863588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8645dfc646bSeric 		*pvp++ = newstr(buf);
8655dfc646bSeric 		if (pvp >= &pv[MAXPV])
86608b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8675dfc646bSeric 	}
8685dfc646bSeric 	*pvp++ = NULL;
8695dfc646bSeric 
87025a99e2eSeric 	/*
87125a99e2eSeric 	**  Call the mailer.
8726328bdf7Seric 	**	The argument vector gets built, pipes
87325a99e2eSeric 	**	are created as necessary, and we fork & exec as
8746328bdf7Seric 	**	appropriate.
875c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
87625a99e2eSeric 	*/
87725a99e2eSeric 
8787ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
87986b26461Seric 		ctladdr = &e->e_from;
880134746fbSeric #ifdef NAMED_BIND
8812bcc6d2dSeric 	if (ConfigLevel < 2)
882912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
883134746fbSeric #endif
8849c9e68d9Seric 
8859c9e68d9Seric 	if (tTd(11, 1))
886134746fbSeric 	{
8879c9e68d9Seric 		printf("openmailer:");
8889c9e68d9Seric 		printav(pv);
8899c9e68d9Seric 	}
8909c9e68d9Seric 	errno = 0;
8919c9e68d9Seric 
8929c9e68d9Seric 	CurHostName = m->m_mailer;
8939c9e68d9Seric 
8949c9e68d9Seric 	/*
8959c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
8969c9e68d9Seric 	**  connection.
8979c9e68d9Seric 	**	In this case we don't actually fork.  We must be
8989c9e68d9Seric 	**	running SMTP for this to work.  We will return a
8999c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
9009c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
9019c9e68d9Seric 	*/
9029c9e68d9Seric 
9039c9e68d9Seric 	curhost = NULL;
9049c9e68d9Seric 
9059c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9069c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9079c9e68d9Seric 	{
9089c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9099c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9109c9e68d9Seric 		mci->mci_in = stdin;
9119c9e68d9Seric 		mci->mci_out = stdout;
9129c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9139c9e68d9Seric 		mci->mci_mailer = m;
9149c9e68d9Seric 	}
9159c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9169c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9179c9e68d9Seric 	{
9189c9e68d9Seric #ifdef DAEMON
9199c9e68d9Seric 		register int i;
9209c9e68d9Seric 		register u_short port;
9219c9e68d9Seric 		extern MCI *mci_get();
9229c9e68d9Seric 		extern char *hostsignature();
9239c9e68d9Seric 
9249c9e68d9Seric 		CurHostName = pv[1];
9259c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9269c9e68d9Seric 
9279c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9289c9e68d9Seric 		{
9299c9e68d9Seric 			syserr("null signature");
930845e533cSeric 			rcode = EX_OSERR;
931b31e7f2bSeric 			goto give_up;
932b31e7f2bSeric 		}
9339c9e68d9Seric 
9349c9e68d9Seric 		if (!clever)
9359c9e68d9Seric 		{
9369c9e68d9Seric 			syserr("554 non-clever IPC");
9379c9e68d9Seric 			rcode = EX_OSERR;
9389c9e68d9Seric 			goto give_up;
9399c9e68d9Seric 		}
9409c9e68d9Seric 		if (pv[2] != NULL)
9419c9e68d9Seric 			port = atoi(pv[2]);
9429c9e68d9Seric 		else
9439c9e68d9Seric 			port = 0;
9449c9e68d9Seric tryhost:
9459c9e68d9Seric 		mci = NULL;
9469c9e68d9Seric 		while (*curhost != '\0')
9479c9e68d9Seric 		{
9489c9e68d9Seric 			register char *p;
9499c9e68d9Seric 			static char hostbuf[MAXNAME];
9509c9e68d9Seric 
9519c9e68d9Seric 			mci = NULL;
9529c9e68d9Seric 
9539c9e68d9Seric 			/* pull the next host from the signature */
9549c9e68d9Seric 			p = strchr(curhost, ':');
9559c9e68d9Seric 			if (p == NULL)
9569c9e68d9Seric 				p = &curhost[strlen(curhost)];
9579c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9589c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9599c9e68d9Seric 			if (*p != '\0')
9609c9e68d9Seric 				p++;
9619c9e68d9Seric 			curhost = p;
9629c9e68d9Seric 
9639c9e68d9Seric 			/* see if we already know that this host is fried */
9649c9e68d9Seric 			CurHostName = hostbuf;
9659c9e68d9Seric 			mci = mci_get(hostbuf, m);
9669c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9679c9e68d9Seric 			{
9689c9e68d9Seric 				if (tTd(11, 1))
9699c9e68d9Seric 				{
9709c9e68d9Seric 					printf("openmailer: ");
9719c9e68d9Seric 					mci_dump(mci);
9729c9e68d9Seric 				}
9739c9e68d9Seric 				CurHostName = mci->mci_host;
9749c9e68d9Seric 				break;
9759c9e68d9Seric 			}
9769c9e68d9Seric 			mci->mci_mailer = m;
9779c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9789c9e68d9Seric 				continue;
9799c9e68d9Seric 
9809c9e68d9Seric 			/* try the connection */
9819c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9829c9e68d9Seric 			message("Connecting to %s (%s)...",
9839c9e68d9Seric 				hostbuf, m->m_name);
9849c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
9859c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
9869c9e68d9Seric 			mci->mci_exitstat = i;
9879c9e68d9Seric 			mci->mci_errno = errno;
9889c9e68d9Seric 			if (i == EX_OK)
9899c9e68d9Seric 			{
9909c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
9919c9e68d9Seric 				mci_cache(mci);
9929c9e68d9Seric 				break;
9939c9e68d9Seric 			}
9949c9e68d9Seric 			else if (tTd(11, 1))
9959c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
9969c9e68d9Seric 					i, errno);
9979c9e68d9Seric 
9989c9e68d9Seric 
9999c9e68d9Seric 			/* enter status of this host */
10009c9e68d9Seric 			setstat(i);
10019c9e68d9Seric 		}
10029c9e68d9Seric 		mci->mci_pid = 0;
10039c9e68d9Seric #else /* no DAEMON */
10049c9e68d9Seric 		syserr("554 openmailer: no IPC");
10059c9e68d9Seric 		if (tTd(11, 1))
10069c9e68d9Seric 			printf("openmailer: NULL\n");
10079c9e68d9Seric 		return NULL;
10089c9e68d9Seric #endif /* DAEMON */
10099c9e68d9Seric 	}
10109c9e68d9Seric 	else
10119c9e68d9Seric 	{
10126fe8c3bcSeric 		int i;
10136fe8c3bcSeric 		struct stat stbuf;
10146fe8c3bcSeric 
10156fe8c3bcSeric 		/* make absolutely certain 0, 1, and 2 are in use */
10166fe8c3bcSeric 		for (i = 0; i < 3; i++)
10176fe8c3bcSeric 		{
10186fe8c3bcSeric 			if (fstat(i, &stbuf) < 0)
10196fe8c3bcSeric 			{
10206fe8c3bcSeric 				/* oops.... */
10210e3bfef5Seric 				int fd;
10220e3bfef5Seric 
10230e3bfef5Seric 				syserr("%s... openmailer(%s): fd %d not open",
10240e3bfef5Seric 					e->e_to, m->m_name, i);
10250e3bfef5Seric 				fd = open("/dev/null", O_RDONLY, 0666);
10260e3bfef5Seric 				if (fd != i)
10270e3bfef5Seric 				{
10280e3bfef5Seric 					(void) dup2(fd, i);
10290e3bfef5Seric 					(void) close(fd);
10300e3bfef5Seric 				}
10316fe8c3bcSeric 			}
10326fe8c3bcSeric 		}
10336fe8c3bcSeric 
10349c9e68d9Seric 		/* create a pipe to shove the mail through */
10359c9e68d9Seric 		if (pipe(mpvect) < 0)
10369c9e68d9Seric 		{
10370e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10380e3bfef5Seric 				e->e_to, m->m_name);
10399c9e68d9Seric 			if (tTd(11, 1))
10409c9e68d9Seric 				printf("openmailer: NULL\n");
10419c9e68d9Seric 			rcode = EX_OSERR;
10429c9e68d9Seric 			goto give_up;
10439c9e68d9Seric 		}
10449c9e68d9Seric 
10459c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10469c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10479c9e68d9Seric 		{
10480e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10490e3bfef5Seric 				e->e_to, m->m_name);
10509c9e68d9Seric 			(void) close(mpvect[0]);
10519c9e68d9Seric 			(void) close(mpvect[1]);
10529c9e68d9Seric 			if (tTd(11, 1))
10539c9e68d9Seric 				printf("openmailer: NULL\n");
10549c9e68d9Seric 			rcode = EX_OSERR;
10559c9e68d9Seric 			goto give_up;
10569c9e68d9Seric 		}
10579c9e68d9Seric 
10589c9e68d9Seric 		/*
10599c9e68d9Seric 		**  Actually fork the mailer process.
10609c9e68d9Seric 		**	DOFORK is clever about retrying.
10619c9e68d9Seric 		**
10629c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10639c9e68d9Seric 		**	around so that endmail will get it.
10649c9e68d9Seric 		*/
10659c9e68d9Seric 
10669c9e68d9Seric 		if (e->e_xfp != NULL)
10679c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10689c9e68d9Seric 		(void) fflush(stdout);
10699c9e68d9Seric # ifdef SIGCHLD
10709c9e68d9Seric 		(void) signal(SIGCHLD, SIG_DFL);
10719c9e68d9Seric # endif /* SIGCHLD */
10729c9e68d9Seric 		DOFORK(FORK);
10739c9e68d9Seric 		/* pid is set by DOFORK */
10749c9e68d9Seric 		if (pid < 0)
10759c9e68d9Seric 		{
10769c9e68d9Seric 			/* failure */
10770e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
10780e3bfef5Seric 				e->e_to, m->m_name);
10799c9e68d9Seric 			(void) close(mpvect[0]);
10809c9e68d9Seric 			(void) close(mpvect[1]);
10819c9e68d9Seric 			if (clever)
10829c9e68d9Seric 			{
10839c9e68d9Seric 				(void) close(rpvect[0]);
10849c9e68d9Seric 				(void) close(rpvect[1]);
10859c9e68d9Seric 			}
10869c9e68d9Seric 			if (tTd(11, 1))
10879c9e68d9Seric 				printf("openmailer: NULL\n");
10889c9e68d9Seric 			rcode = EX_OSERR;
10899c9e68d9Seric 			goto give_up;
10909c9e68d9Seric 		}
10919c9e68d9Seric 		else if (pid == 0)
10929c9e68d9Seric 		{
10939c9e68d9Seric 			int i;
10949c9e68d9Seric 			int saveerrno;
10959c9e68d9Seric 			char **ep;
10969c9e68d9Seric 			char *env[MAXUSERENVIRON];
10979c9e68d9Seric 			extern char **environ;
10989c9e68d9Seric 			extern int DtableSize;
10999c9e68d9Seric 
11009c9e68d9Seric 			/* child -- set up input & exec mailer */
11019c9e68d9Seric 			/* make diagnostic output be standard output */
11029c9e68d9Seric 			(void) signal(SIGINT, SIG_IGN);
11039c9e68d9Seric 			(void) signal(SIGHUP, SIG_IGN);
11049c9e68d9Seric 			(void) signal(SIGTERM, SIG_DFL);
11059c9e68d9Seric 
11069c9e68d9Seric 			/* close any other cached connections */
11079c9e68d9Seric 			mci_flush(FALSE, mci);
11089c9e68d9Seric 
1109b986f6aaSeric 			/* move into some "safe" directory */
1110b986f6aaSeric 			if (m->m_execdir != NULL)
1111b986f6aaSeric 			{
1112b986f6aaSeric 				char *p, *q;
1113b986f6aaSeric 				char buf[MAXLINE];
1114b986f6aaSeric 
1115b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1116b986f6aaSeric 				{
1117b986f6aaSeric 					q = strchr(p, ':');
1118b986f6aaSeric 					if (q != NULL)
1119b986f6aaSeric 						*q = '\0';
1120b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1121b986f6aaSeric 					if (q != NULL)
1122b986f6aaSeric 						*q++ = ':';
1123b986f6aaSeric 					if (tTd(11, 20))
1124b986f6aaSeric 						printf("openmailer: trydir %s\n",
1125b986f6aaSeric 							buf);
1126b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1127b986f6aaSeric 						break;
1128b986f6aaSeric 				}
1129b986f6aaSeric 			}
1130b986f6aaSeric 
11319c9e68d9Seric 			/* arrange to filter std & diag output of command */
11329c9e68d9Seric 			if (clever)
11339c9e68d9Seric 			{
11349c9e68d9Seric 				(void) close(rpvect[0]);
11356fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11366fe8c3bcSeric 				{
11370e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11380e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11396fe8c3bcSeric 					_exit(EX_OSERR);
11406fe8c3bcSeric 				}
11419c9e68d9Seric 				(void) close(rpvect[1]);
11429c9e68d9Seric 			}
11439c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11449c9e68d9Seric 			{
11459c9e68d9Seric 				/* put mailer output in transcript */
11466fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11476fe8c3bcSeric 				{
11480e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11490e3bfef5Seric 						e->e_to, m->m_name,
11506fe8c3bcSeric 						fileno(e->e_xfp));
11516fe8c3bcSeric 					_exit(EX_OSERR);
11529c9e68d9Seric 				}
11536fe8c3bcSeric 			}
11546fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11556fe8c3bcSeric 			{
11560e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
11570e3bfef5Seric 					e->e_to, m->m_name);
11586fe8c3bcSeric 				_exit(EX_OSERR);
11596fe8c3bcSeric 			}
11609c9e68d9Seric 
11619c9e68d9Seric 			/* arrange to get standard input */
11629c9e68d9Seric 			(void) close(mpvect[1]);
11639c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11649c9e68d9Seric 			{
11650e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
11660e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
11679c9e68d9Seric 				_exit(EX_OSERR);
11689c9e68d9Seric 			}
11699c9e68d9Seric 			(void) close(mpvect[0]);
11709c9e68d9Seric 			if (!bitnset(M_RESTR, m->m_flags))
11719c9e68d9Seric 			{
11729c9e68d9Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
11739c9e68d9Seric 				{
11749c9e68d9Seric 					(void) setgid(DefGid);
11759c9e68d9Seric 					(void) initgroups(DefUser, DefGid);
11769c9e68d9Seric 					(void) setuid(DefUid);
11779c9e68d9Seric 				}
11789c9e68d9Seric 				else
11799c9e68d9Seric 				{
11809c9e68d9Seric 					(void) setgid(ctladdr->q_gid);
11819c9e68d9Seric 					(void) initgroups(ctladdr->q_ruser?
11829c9e68d9Seric 						ctladdr->q_ruser: ctladdr->q_user,
11839c9e68d9Seric 						ctladdr->q_gid);
11849c9e68d9Seric 					(void) setuid(ctladdr->q_uid);
11859c9e68d9Seric 				}
11869c9e68d9Seric 			}
11879c9e68d9Seric 
11889c9e68d9Seric 			/* arrange for all the files to be closed */
11899c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
11909c9e68d9Seric 			{
11919c9e68d9Seric 				register int j;
11929c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
11939c9e68d9Seric 					(void)fcntl(i, F_SETFD, j|1);
11949c9e68d9Seric 			}
11959c9e68d9Seric 
11969c9e68d9Seric 			/* set up the mailer environment */
11979c9e68d9Seric 			i = 0;
11989c9e68d9Seric 			env[i++] = "AGENT=sendmail";
11999c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
12009c9e68d9Seric 			{
12019c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12029c9e68d9Seric 					env[i++] = *ep;
12039c9e68d9Seric 			}
12049c9e68d9Seric 			env[i++] = NULL;
12059c9e68d9Seric 
12069c9e68d9Seric 			/* try to execute the mailer */
12079c9e68d9Seric 			execve(m->m_mailer, pv, env);
12089c9e68d9Seric 			saveerrno = errno;
12099c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12109c9e68d9Seric 			if (m == LocalMailer)
12119c9e68d9Seric 				_exit(EX_TEMPFAIL);
12129c9e68d9Seric 			if (transienterror(saveerrno))
12139c9e68d9Seric 				_exit(EX_TEMPFAIL);
12149c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12159c9e68d9Seric 		}
12169c9e68d9Seric 
12179c9e68d9Seric 		/*
12189c9e68d9Seric 		**  Set up return value.
12199c9e68d9Seric 		*/
12209c9e68d9Seric 
12219c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12229c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12239c9e68d9Seric 		mci->mci_mailer = m;
12249c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12259c9e68d9Seric 		mci->mci_pid = pid;
12269c9e68d9Seric 		(void) close(mpvect[0]);
12279c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
12289c9e68d9Seric 		if (clever)
12299c9e68d9Seric 		{
12309c9e68d9Seric 			(void) close(rpvect[1]);
12319c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
12329c9e68d9Seric 		}
12339c9e68d9Seric 		else
12349c9e68d9Seric 		{
12359c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12369c9e68d9Seric 			mci->mci_in = NULL;
12379c9e68d9Seric 		}
12389c9e68d9Seric 	}
12399c9e68d9Seric 
12409c9e68d9Seric 	/*
12419c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12429c9e68d9Seric 	*/
12439c9e68d9Seric 
12449c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12459c9e68d9Seric 	{
12469c9e68d9Seric 		smtpinit(m, mci, e);
12479c9e68d9Seric 	}
12489c9e68d9Seric 	if (tTd(11, 1))
12499c9e68d9Seric 	{
12509c9e68d9Seric 		printf("openmailer: ");
12519c9e68d9Seric 		mci_dump(mci);
12529c9e68d9Seric 	}
12539c9e68d9Seric 
12549c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1255b31e7f2bSeric 	{
1256b31e7f2bSeric 		/* couldn't open the mailer */
1257b31e7f2bSeric 		rcode = mci->mci_exitstat;
12582a6bc25bSeric 		errno = mci->mci_errno;
1259b31e7f2bSeric 		if (rcode == EX_OK)
1260b31e7f2bSeric 		{
1261b31e7f2bSeric 			/* shouldn't happen */
126208b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12636b0f339dSeric 				rcode, mci->mci_state, firstsig);
1264b31e7f2bSeric 			rcode = EX_SOFTWARE;
1265b31e7f2bSeric 		}
1266b31e7f2bSeric 	}
1267b31e7f2bSeric 	else if (!clever)
1268b31e7f2bSeric 	{
1269b31e7f2bSeric 		/*
1270b31e7f2bSeric 		**  Format and send message.
1271b31e7f2bSeric 		*/
127215d084d5Seric 
1273b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1274b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1275b31e7f2bSeric 		putline("\n", mci->mci_out, m);
1276b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
1277b31e7f2bSeric 
1278b31e7f2bSeric 		/* get the exit status */
1279c9be6216Seric 		rcode = endmailer(mci, e, pv);
1280134746fbSeric 	}
1281134746fbSeric 	else
1282b31e7f2bSeric #ifdef SMTP
1283134746fbSeric 	{
1284b31e7f2bSeric 		/*
1285b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1286b31e7f2bSeric 		*/
128715d084d5Seric 
1288b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1289b31e7f2bSeric 		if (rcode == EX_OK)
129075889e88Seric 		{
1291ded0d3daSkarels 			register char *t = tobuf;
1292ded0d3daSkarels 			register int i;
1293ded0d3daSkarels 
1294588cad61Seric 			/* send the recipient list */
129563780dbdSeric 			tobuf[0] = '\0';
129675889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
129775889e88Seric 			{
129863780dbdSeric 				e->e_to = to->q_paddr;
129915d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
130075889e88Seric 				{
130183b7ddc9Seric 					markfailure(e, to, i);
130281161401Seric 					giveresponse(i, m, mci, e);
130363780dbdSeric 				}
130475889e88Seric 				else
130575889e88Seric 				{
1306911693bfSbostic 					*t++ = ',';
1307b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1308b31e7f2bSeric 						continue;
1309588cad61Seric 				}
1310588cad61Seric 			}
1311588cad61Seric 
131263780dbdSeric 			/* now send the data */
131363780dbdSeric 			if (tobuf[0] == '\0')
1314b31e7f2bSeric 			{
13159c9e68d9Seric 				rcode = EX_OK;
131663780dbdSeric 				e->e_to = NULL;
1317b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1318b31e7f2bSeric 					smtprset(m, mci, e);
1319b31e7f2bSeric 			}
132075889e88Seric 			else
132175889e88Seric 			{
132263780dbdSeric 				e->e_to = tobuf + 1;
132375889e88Seric 				rcode = smtpdata(m, mci, e);
132463780dbdSeric 			}
132563780dbdSeric 
132663780dbdSeric 			/* now close the connection */
1327b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
132815d084d5Seric 				smtpquit(m, mci, e);
132963780dbdSeric 		}
13309c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13319c9e68d9Seric 		{
13329c9e68d9Seric 			/* try next MX site */
13339c9e68d9Seric 			goto tryhost;
13349c9e68d9Seric 		}
1335c579ef51Seric 	}
1336b31e7f2bSeric #else /* not SMTP */
1337a05b3449Sbostic 	{
133808b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1339845e533cSeric 		rcode = EX_CONFIG;
1340b31e7f2bSeric 		goto give_up;
1341a05b3449Sbostic 	}
1342b31e7f2bSeric #endif /* SMTP */
1343134746fbSeric #ifdef NAMED_BIND
13442bcc6d2dSeric 	if (ConfigLevel < 2)
1345912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1346134746fbSeric #endif
13475dfc646bSeric 
1348b31e7f2bSeric 	/* arrange a return receipt if requested */
13498c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1350b31e7f2bSeric 	{
1351b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1352b31e7f2bSeric 		/* do we want to send back more info? */
1353b31e7f2bSeric 	}
1354b31e7f2bSeric 
1355c77d1c25Seric 	/*
135663780dbdSeric 	**  Do final status disposal.
135763780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1358c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1359c77d1c25Seric 	**		addressees.
1360c77d1c25Seric 	*/
1361c77d1c25Seric 
1362b31e7f2bSeric   give_up:
136363780dbdSeric 	if (tobuf[0] != '\0')
136481161401Seric 		giveresponse(rcode, m, mci, e);
1365772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1366b31e7f2bSeric 	{
1367dde5acadSeric 		if (rcode != EX_OK)
136883b7ddc9Seric 			markfailure(e, to, rcode);
1369dde5acadSeric 		else
1370655518ecSeric 		{
1371dde5acadSeric 			to->q_flags |= QSENT;
1372655518ecSeric 			e->e_nsent++;
1373655518ecSeric 		}
1374b31e7f2bSeric 	}
1375b31e7f2bSeric 
1376b31e7f2bSeric 	/*
1377b31e7f2bSeric 	**  Restore state and return.
1378b31e7f2bSeric 	*/
1379c77d1c25Seric 
138035490626Seric 	errno = 0;
1381588cad61Seric 	define('g', (char *) NULL, e);
13825826d9d3Seric 	return (rcode);
138325a99e2eSeric }
13845dfc646bSeric /*
138583b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
138683b7ddc9Seric **
138783b7ddc9Seric **	Parameters:
138883b7ddc9Seric **		e -- the envelope we are sending.
138983b7ddc9Seric **		q -- the address to mark.
139083b7ddc9Seric **		rcode -- the code signifying the particular failure.
139183b7ddc9Seric **
139283b7ddc9Seric **	Returns:
139383b7ddc9Seric **		none.
139483b7ddc9Seric **
139583b7ddc9Seric **	Side Effects:
139683b7ddc9Seric **		marks the address (and possibly the envelope) with the
139783b7ddc9Seric **			failure so that an error will be returned or
139883b7ddc9Seric **			the message will be queued, as appropriate.
139983b7ddc9Seric */
140083b7ddc9Seric 
140183b7ddc9Seric markfailure(e, q, rcode)
140283b7ddc9Seric 	register ENVELOPE *e;
140383b7ddc9Seric 	register ADDRESS *q;
140483b7ddc9Seric 	int rcode;
140583b7ddc9Seric {
140619c47125Seric 	char buf[MAXLINE];
140719c47125Seric 	extern char *pintvl();
140819c47125Seric 
140983b7ddc9Seric 	if (rcode == EX_OK)
141083b7ddc9Seric 		return;
1411ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
141283b7ddc9Seric 		q->q_flags |= QBADADDR;
141319c47125Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
141483b7ddc9Seric 	{
141583b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
1416198d9be0Seric 		{
1417198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
141819c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
1419198d9be0Seric 			if (e->e_message != NULL)
1420198d9be0Seric 				free(e->e_message);
1421198d9be0Seric 			e->e_message = newstr(buf);
142208b25121Seric 			message(buf);
1423198d9be0Seric 		}
142483b7ddc9Seric 		q->q_flags |= QBADADDR;
142583b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
1426c13b5dfcSeric 		fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr);
142783b7ddc9Seric 	}
142883b7ddc9Seric 	else
142919c47125Seric 	{
143083b7ddc9Seric 		q->q_flags |= QQUEUEUP;
143119c47125Seric 		if (TimeOuts.to_q_warning > 0 &&
143219c47125Seric 		    curtime() > e->e_ctime + TimeOuts.to_q_warning)
143319c47125Seric 		{
143419c47125Seric 			if (!bitset(EF_WARNING, e->e_flags) &&
143519c47125Seric 			    e->e_class >= 0)
143619c47125Seric 			{
143719c47125Seric 				(void) sprintf(buf,
143819c47125Seric 					"warning: cannot send message for %s",
143919c47125Seric 					pintvl(TimeOuts.to_q_warning, FALSE));
144019c47125Seric 				if (e->e_message != NULL)
144119c47125Seric 					free(e->e_message);
144219c47125Seric 				e->e_message = newstr(buf);
144319c47125Seric 				message(buf);
144419c47125Seric 				e->e_flags |= EF_WARNING|EF_TIMEOUT;
144519c47125Seric 			}
144619c47125Seric 			fprintf(e->e_xfp,
144719c47125Seric 				"%s... Warning: message still undelivered after %s\n",
144819c47125Seric 				q->q_paddr, pintvl(TimeOuts.to_q_warning, FALSE));
144919c47125Seric 			fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
145019c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
145119c47125Seric 		}
145219c47125Seric 	}
145383b7ddc9Seric }
145483b7ddc9Seric /*
1455c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1456c579ef51Seric **
1457c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1458c579ef51Seric **	violation), so we report those specially.  For other
1459c579ef51Seric **	errors, we choose a status message (into statmsg),
1460c579ef51Seric **	and if it represents an error, we print it.
1461c579ef51Seric **
1462c579ef51Seric **	Parameters:
1463c579ef51Seric **		pid -- pid of mailer.
1464c9be6216Seric **		e -- the current envelope.
1465c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1466c9be6216Seric **			(for error messages).
1467c579ef51Seric **
1468c579ef51Seric **	Returns:
1469c579ef51Seric **		exit code of mailer.
1470c579ef51Seric **
1471c579ef51Seric **	Side Effects:
1472c579ef51Seric **		none.
1473c579ef51Seric */
1474c579ef51Seric 
1475c9be6216Seric endmailer(mci, e, pv)
1476b31e7f2bSeric 	register MCI *mci;
1477c9be6216Seric 	register ENVELOPE *e;
1478c9be6216Seric 	char **pv;
1479c579ef51Seric {
1480588cad61Seric 	int st;
1481c579ef51Seric 
148275889e88Seric 	/* close any connections */
148375889e88Seric 	if (mci->mci_in != NULL)
1484c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
148575889e88Seric 	if (mci->mci_out != NULL)
1486c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
148775889e88Seric 	mci->mci_in = mci->mci_out = NULL;
148875889e88Seric 	mci->mci_state = MCIS_CLOSED;
148975889e88Seric 
149033db8731Seric 	/* in the IPC case there is nothing to wait for */
149175889e88Seric 	if (mci->mci_pid == 0)
149233db8731Seric 		return (EX_OK);
149333db8731Seric 
149433db8731Seric 	/* wait for the mailer process to die and collect status */
149575889e88Seric 	st = waitfor(mci->mci_pid);
1496588cad61Seric 	if (st == -1)
149778de67c1Seric 	{
1498c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1499588cad61Seric 		return (EX_SOFTWARE);
1500c579ef51Seric 	}
150133db8731Seric 
150233db8731Seric 	/* see if it died a horrid death */
1503c579ef51Seric 	if ((st & 0377) != 0)
1504c579ef51Seric 	{
1505c9be6216Seric 		syserr("mailer %s died with signal %o", pv[0], st);
1506c9be6216Seric 
1507c9be6216Seric 		/* log the arguments */
1508c9be6216Seric 		if (e->e_xfp != NULL)
1509c9be6216Seric 		{
1510c9be6216Seric 			register char **av;
1511c9be6216Seric 
1512c9be6216Seric 			fprintf(e->e_xfp, "Arguments:");
1513c9be6216Seric 			for (av = pv; *av != NULL; av++)
1514c9be6216Seric 				fprintf(e->e_xfp, " %s", *av);
1515c9be6216Seric 			fprintf(e->e_xfp, "\n");
1516c9be6216Seric 		}
1517c9be6216Seric 
15185f73204aSeric 		ExitStat = EX_TEMPFAIL;
15195f73204aSeric 		return (EX_TEMPFAIL);
1520c579ef51Seric 	}
152133db8731Seric 
152233db8731Seric 	/* normal death -- return status */
1523588cad61Seric 	st = (st >> 8) & 0377;
1524588cad61Seric 	return (st);
1525c579ef51Seric }
1526c579ef51Seric /*
152725a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
152825a99e2eSeric **
152925a99e2eSeric **	Parameters:
153025a99e2eSeric **		stat -- the status code from the mailer (high byte
153125a99e2eSeric **			only; core dumps must have been taken care of
153225a99e2eSeric **			already).
153381161401Seric **		m -- the mailer info for this mailer.
153481161401Seric **		mci -- the mailer connection info -- can be NULL if the
153581161401Seric **			response is given before the connection is made.
153681161401Seric **		e -- the current envelope.
153725a99e2eSeric **
153825a99e2eSeric **	Returns:
1539db8841e9Seric **		none.
154025a99e2eSeric **
154125a99e2eSeric **	Side Effects:
1542c1f9df2cSeric **		Errors may be incremented.
154325a99e2eSeric **		ExitStat may be set.
154425a99e2eSeric */
154525a99e2eSeric 
154681161401Seric giveresponse(stat, m, mci, e)
154725a99e2eSeric 	int stat;
1548588cad61Seric 	register MAILER *m;
154981161401Seric 	register MCI *mci;
1550198d9be0Seric 	ENVELOPE *e;
155125a99e2eSeric {
155225a99e2eSeric 	register char *statmsg;
155325a99e2eSeric 	extern char *SysExMsg[];
155425a99e2eSeric 	register int i;
1555d4bd8f0eSbostic 	extern int N_SysEx;
1556d4bd8f0eSbostic #ifdef NAMED_BIND
1557d4bd8f0eSbostic 	extern int h_errno;
1558d4bd8f0eSbostic #endif
1559198d9be0Seric 	char buf[MAXLINE];
15607d55540cSeric 	extern char *errstring();
156125a99e2eSeric 
156213bbc08cSeric 	/*
156313bbc08cSeric 	**  Compute status message from code.
156413bbc08cSeric 	*/
156513bbc08cSeric 
156625a99e2eSeric 	i = stat - EX__BASE;
1567588cad61Seric 	if (stat == 0)
15686fe8c3bcSeric 	{
1569588cad61Seric 		statmsg = "250 Sent";
1570ce5531bdSeric 		if (e->e_statmsg != NULL)
15716fe8c3bcSeric 		{
1572ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15736fe8c3bcSeric 			statmsg = buf;
15746fe8c3bcSeric 		}
15756fe8c3bcSeric 	}
1576588cad61Seric 	else if (i < 0 || i > N_SysEx)
1577588cad61Seric 	{
1578588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1579588cad61Seric 		stat = EX_UNAVAILABLE;
1580588cad61Seric 		statmsg = buf;
1581588cad61Seric 	}
1582198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1583198d9be0Seric 	{
15847d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1585d4bd8f0eSbostic #ifdef NAMED_BIND
1586f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1587f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1588f28da541Smiriam 		else
1589d4bd8f0eSbostic #endif
1590f28da541Smiriam 		{
15918557d168Seric 			if (errno != 0)
1592d87e85f3Seric 				statmsg = errstring(errno);
1593d87e85f3Seric 			else
1594d87e85f3Seric 			{
1595d87e85f3Seric #ifdef SMTP
1596d87e85f3Seric 				extern char SmtpError[];
1597d87e85f3Seric 
1598d87e85f3Seric 				statmsg = SmtpError;
15996c2c3107Seric #else /* SMTP */
1600d87e85f3Seric 				statmsg = NULL;
16016c2c3107Seric #endif /* SMTP */
1602d87e85f3Seric 			}
1603f28da541Smiriam 		}
1604d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1605d87e85f3Seric 		{
160687c9b3e7Seric 			(void) strcat(buf, ": ");
1607d87e85f3Seric 			(void) strcat(buf, statmsg);
16088557d168Seric 		}
1609198d9be0Seric 		statmsg = buf;
1610198d9be0Seric 	}
161125a99e2eSeric 	else
1612d87e85f3Seric 	{
161325a99e2eSeric 		statmsg = SysExMsg[i];
16147d55540cSeric 		if (*statmsg++ == ':')
16157d55540cSeric 		{
16167d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16177d55540cSeric 			statmsg = buf;
16187d55540cSeric 		}
1619d87e85f3Seric 	}
1620588cad61Seric 
1621588cad61Seric 	/*
1622588cad61Seric 	**  Print the message as appropriate
1623588cad61Seric 	*/
1624588cad61Seric 
1625198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1626e12d03eeSeric 		message(&statmsg[4], errstring(errno));
162725a99e2eSeric 	else
162825a99e2eSeric 	{
1629c1f9df2cSeric 		Errors++;
1630e12d03eeSeric 		usrerr(statmsg, errstring(errno));
163125a99e2eSeric 	}
163225a99e2eSeric 
163325a99e2eSeric 	/*
163425a99e2eSeric 	**  Final cleanup.
163525a99e2eSeric 	**	Log a record of the transaction.  Compute the new
163625a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
163725a99e2eSeric 	**	that.
163825a99e2eSeric 	*/
163925a99e2eSeric 
16402f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
164181161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1642eb238f8cSeric 
1643eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1644eb238f8cSeric 		setstat(stat);
1645198d9be0Seric 	if (stat != EX_OK)
1646198d9be0Seric 	{
1647198d9be0Seric 		if (e->e_message != NULL)
1648198d9be0Seric 			free(e->e_message);
1649198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1650198d9be0Seric 	}
16518557d168Seric 	errno = 0;
1652d4bd8f0eSbostic #ifdef NAMED_BIND
1653f28da541Smiriam 	h_errno = 0;
1654d4bd8f0eSbostic #endif
1655eb238f8cSeric }
1656eb238f8cSeric /*
1657eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1658eb238f8cSeric **
1659eb238f8cSeric **	Parameters:
166081161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
166181161401Seric **		mci -- the mailer connection info -- can be NULL if the
166281161401Seric **			log is occuring when no connection is active.
166381161401Seric **		stat -- the message to print for the status.
166481161401Seric **		e -- the current envelope.
1665eb238f8cSeric **
1666eb238f8cSeric **	Returns:
1667eb238f8cSeric **		none
1668eb238f8cSeric **
1669eb238f8cSeric **	Side Effects:
1670eb238f8cSeric **		none
1671eb238f8cSeric */
1672eb238f8cSeric 
167381161401Seric logdelivery(m, mci, stat, e)
167481161401Seric 	MAILER *m;
167581161401Seric 	register MCI *mci;
1676eb238f8cSeric 	char *stat;
1677b31e7f2bSeric 	register ENVELOPE *e;
16785cf56be3Seric {
1679eb238f8cSeric # ifdef LOG
16809507d1f9Seric 	char *curhost;
1681d6acf3eeSeric 	char buf[512];
16829507d1f9Seric 	extern char *pintvl();
16839507d1f9Seric 	extern char *macvalue();
16849507d1f9Seric 
168548ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
168681161401Seric 
168748ed5d33Seric 	if (m != NULL)
168871ff6caaSeric 	{
168948ed5d33Seric 		(void) strcat(buf, ", mailer=");
169048ed5d33Seric 		(void) strcat(buf, m->m_name);
169171ff6caaSeric 	}
169248ed5d33Seric 
169348ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
169471ff6caaSeric 	{
169571ff6caaSeric # ifdef DAEMON
1696e2f2f828Seric 		extern SOCKADDR CurHostAddr;
1697e2f2f828Seric 		extern char *anynet_ntoa();
169848ed5d33Seric # endif
169971ff6caaSeric 
170048ed5d33Seric 		(void) strcat(buf, ", relay=");
170148ed5d33Seric 		(void) strcat(buf, mci->mci_host);
170248ed5d33Seric 
170348ed5d33Seric # ifdef DAEMON
170448ed5d33Seric 		(void) strcat(buf, " (");
1705e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
170648ed5d33Seric 		(void) strcat(buf, ")");
170771ff6caaSeric # endif
170871ff6caaSeric 	}
17099507d1f9Seric 	else
171048ed5d33Seric 	{
171148ed5d33Seric 		char *p = macvalue('h', e);
17129507d1f9Seric 
171348ed5d33Seric 		if (p != NULL && p[0] != '\0')
171448ed5d33Seric 		{
171548ed5d33Seric 			(void) strcat(buf, ", relay=");
171648ed5d33Seric 			(void) strcat(buf, p);
171748ed5d33Seric 		}
171848ed5d33Seric 	}
1719d6acf3eeSeric 
1720d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1721d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
17226c2c3107Seric # endif /* LOG */
172325a99e2eSeric }
172425a99e2eSeric /*
172551552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
172625a99e2eSeric **
172751552439Seric **	This can be made an arbitrary message separator by changing $l
172851552439Seric **
17299b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
17309b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
17319b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
17329b6c17a6Seric **	this kind of antique garbage????
173325a99e2eSeric **
173425a99e2eSeric **	Parameters:
173551552439Seric **		fp -- the file to output to.
173651552439Seric **		m -- the mailer describing this entry.
173725a99e2eSeric **
173825a99e2eSeric **	Returns:
173951552439Seric **		none
174025a99e2eSeric **
174125a99e2eSeric **	Side Effects:
174251552439Seric **		outputs some text to fp.
174325a99e2eSeric */
174425a99e2eSeric 
1745b31e7f2bSeric putfromline(fp, m, e)
174651552439Seric 	register FILE *fp;
174751552439Seric 	register MAILER *m;
1748b31e7f2bSeric 	ENVELOPE *e;
174925a99e2eSeric {
17502bc47524Seric 	char *template = "\201l\n";
175151552439Seric 	char buf[MAXLINE];
175225a99e2eSeric 
175357fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
175451552439Seric 		return;
175513bbc08cSeric 
17562c7e1b8dSeric # ifdef UGLYUUCP
175757fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
175874b6e67bSeric 	{
1759ea09d6edSeric 		char *bang;
1760ea09d6edSeric 		char xbuf[MAXLINE];
176174b6e67bSeric 
1762ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17636c2c3107Seric 		bang = strchr(buf, '!');
176474b6e67bSeric 		if (bang == NULL)
176508b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
176674b6e67bSeric 		else
1767588cad61Seric 		{
1768ea09d6edSeric 			*bang++ = '\0';
17692bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1770ea09d6edSeric 			template = xbuf;
177174b6e67bSeric 		}
1772588cad61Seric 	}
17736c2c3107Seric # endif /* UGLYUUCP */
1774b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
177577b52738Seric 	putline(buf, fp, m);
1776bc6e2962Seric }
1777bc6e2962Seric /*
177851552439Seric **  PUTBODY -- put the body of a message.
177951552439Seric **
178051552439Seric **	Parameters:
178151552439Seric **		fp -- file to output onto.
178277b52738Seric **		m -- a mailer descriptor to control output format.
17839a6a5f55Seric **		e -- the envelope to put out.
178451552439Seric **
178551552439Seric **	Returns:
178651552439Seric **		none.
178751552439Seric **
178851552439Seric **	Side Effects:
178951552439Seric **		The message is written onto fp.
179051552439Seric */
179151552439Seric 
179277b52738Seric putbody(fp, m, e)
179351552439Seric 	FILE *fp;
1794588cad61Seric 	MAILER *m;
17959a6a5f55Seric 	register ENVELOPE *e;
179651552439Seric {
179777b52738Seric 	char buf[MAXLINE];
179851552439Seric 
179951552439Seric 	/*
180051552439Seric 	**  Output the body of the message
180151552439Seric 	*/
180251552439Seric 
18039a6a5f55Seric 	if (e->e_dfp == NULL)
180451552439Seric 	{
18059a6a5f55Seric 		if (e->e_df != NULL)
18069a6a5f55Seric 		{
18079a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
18089a6a5f55Seric 			if (e->e_dfp == NULL)
18098f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
18108f9146b0Srick 				e->e_df, e->e_to, e->e_from);
18119a6a5f55Seric 		}
18129a6a5f55Seric 		else
181377b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
18149a6a5f55Seric 	}
18159a6a5f55Seric 	if (e->e_dfp != NULL)
18169a6a5f55Seric 	{
18179a6a5f55Seric 		rewind(e->e_dfp);
181877b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
181924fc8aeeSeric 		{
182024fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1821d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
18223462ad9eSeric 				(void) putc('>', fp);
182377b52738Seric 			putline(buf, fp, m);
182424fc8aeeSeric 		}
182551552439Seric 
18269a6a5f55Seric 		if (ferror(e->e_dfp))
182751552439Seric 		{
182851552439Seric 			syserr("putbody: read error");
182951552439Seric 			ExitStat = EX_IOERR;
183051552439Seric 		}
183151552439Seric 	}
183251552439Seric 
183351552439Seric 	(void) fflush(fp);
183451552439Seric 	if (ferror(fp) && errno != EPIPE)
183551552439Seric 	{
183651552439Seric 		syserr("putbody: write error");
183751552439Seric 		ExitStat = EX_IOERR;
183851552439Seric 	}
183951552439Seric 	errno = 0;
184025a99e2eSeric }
184125a99e2eSeric /*
184225a99e2eSeric **  MAILFILE -- Send a message to a file.
184325a99e2eSeric **
1844f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1845f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1846f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1847f129ec7dSeric **	sendmail runs as root.
1848f129ec7dSeric **
1849588cad61Seric **	This could be done as a subordinate mailer, except that it
1850588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1851588cad61Seric **	view this as being sufficiently important as to include it
1852588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1853588cad61Seric **	to create another process plus some pipes to save the message.
1854588cad61Seric **
185525a99e2eSeric **	Parameters:
185625a99e2eSeric **		filename -- the name of the file to send to.
18576259796dSeric **		ctladdr -- the controlling address header -- includes
18586259796dSeric **			the userid/groupid to be when sending.
185925a99e2eSeric **
186025a99e2eSeric **	Returns:
186125a99e2eSeric **		The exit code associated with the operation.
186225a99e2eSeric **
186325a99e2eSeric **	Side Effects:
186425a99e2eSeric **		none.
186525a99e2eSeric */
186625a99e2eSeric 
1867b31e7f2bSeric mailfile(filename, ctladdr, e)
186825a99e2eSeric 	char *filename;
18696259796dSeric 	ADDRESS *ctladdr;
1870b31e7f2bSeric 	register ENVELOPE *e;
187125a99e2eSeric {
187225a99e2eSeric 	register FILE *f;
187332d19d43Seric 	register int pid;
187415d084d5Seric 	int mode;
187525a99e2eSeric 
187632d19d43Seric 	/*
187732d19d43Seric 	**  Fork so we can change permissions here.
187832d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
187932d19d43Seric 	**	the complications of calling subroutines, etc.
188032d19d43Seric 	*/
188132d19d43Seric 
188232d19d43Seric 	DOFORK(fork);
188332d19d43Seric 
188432d19d43Seric 	if (pid < 0)
188532d19d43Seric 		return (EX_OSERR);
188632d19d43Seric 	else if (pid == 0)
188732d19d43Seric 	{
188832d19d43Seric 		/* child -- actually write to file */
1889f129ec7dSeric 		struct stat stb;
1890f129ec7dSeric 
18910984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
18920984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
18930984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
18943462ad9eSeric 		(void) umask(OldUmask);
189595f16dc0Seric 
1896f129ec7dSeric 		if (stat(filename, &stb) < 0)
1897e6e1265fSeric 			stb.st_mode = 0666;
189815d084d5Seric 		mode = stb.st_mode;
189995f16dc0Seric 
190095f16dc0Seric 		/* limit the errors to those actually caused in the child */
190195f16dc0Seric 		errno = 0;
190295f16dc0Seric 		ExitStat = EX_OK;
190395f16dc0Seric 
1904f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1905f129ec7dSeric 			exit(EX_CANTCREAT);
190603827b5fSeric 		if (ctladdr == NULL)
19078f9146b0Srick 			ctladdr = &e->e_from;
190815d084d5Seric 		else
190915d084d5Seric 		{
191015d084d5Seric 			/* ignore setuid and setgid bits */
191115d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
191215d084d5Seric 		}
191315d084d5Seric 
19148f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
19158f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
19168f9146b0Srick 		{
19178f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
191895f16dc0Seric 			if (e->e_dfp == NULL)
191995f16dc0Seric 			{
19208f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
19218f9146b0Srick 					e->e_df, e->e_to, e->e_from);
19228f9146b0Srick 			}
19238f9146b0Srick 		}
19248f9146b0Srick 
192515d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1926e36b99e2Seric 		{
192795f16dc0Seric 			if (ctladdr->q_uid == 0)
192895f16dc0Seric 			{
1929e36b99e2Seric 				(void) setgid(DefGid);
1930898a126bSbostic 				(void) initgroups(DefUser, DefGid);
193195f16dc0Seric 			}
193295f16dc0Seric 			else
193395f16dc0Seric 			{
19346259796dSeric 				(void) setgid(ctladdr->q_gid);
1935898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1936898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1937898a126bSbostic 					ctladdr->q_gid);
1938898a126bSbostic 			}
1939e36b99e2Seric 		}
194015d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1941e36b99e2Seric 		{
1942e36b99e2Seric 			if (ctladdr->q_uid == 0)
1943e36b99e2Seric 				(void) setuid(DefUid);
1944e36b99e2Seric 			else
19456259796dSeric 				(void) setuid(ctladdr->q_uid);
1946e36b99e2Seric 		}
194795f16dc0Seric 		FileName = filename;
194895f16dc0Seric 		LineNumber = 0;
194927628d59Seric 		f = dfopen(filename, "a");
195025a99e2eSeric 		if (f == NULL)
195195f16dc0Seric 		{
195208b25121Seric 			message("554 cannot open");
195332d19d43Seric 			exit(EX_CANTCREAT);
195495f16dc0Seric 		}
195525a99e2eSeric 
1956b31e7f2bSeric 		putfromline(f, ProgMailer, e);
1957b843d759Seric 		(*e->e_puthdr)(f, ProgMailer, e);
195877b52738Seric 		putline("\n", f, ProgMailer);
1959b843d759Seric 		(*e->e_putbody)(f, ProgMailer, e);
196077b52738Seric 		putline("\n", f, ProgMailer);
196195f16dc0Seric 		if (ferror(f))
196295f16dc0Seric 		{
196308b25121Seric 			message("451 I/O error");
196495f16dc0Seric 			setstat(EX_IOERR);
196595f16dc0Seric 		}
1966ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
196732d19d43Seric 		(void) fflush(stdout);
1968e36b99e2Seric 
196927628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1970c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
197195f16dc0Seric 		exit(ExitStat);
197213bbc08cSeric 		/*NOTREACHED*/
197332d19d43Seric 	}
197432d19d43Seric 	else
197532d19d43Seric 	{
197632d19d43Seric 		/* parent -- wait for exit status */
1977588cad61Seric 		int st;
197832d19d43Seric 
1979588cad61Seric 		st = waitfor(pid);
1980588cad61Seric 		if ((st & 0377) != 0)
1981588cad61Seric 			return (EX_UNAVAILABLE);
1982588cad61Seric 		else
1983588cad61Seric 			return ((st >> 8) & 0377);
19848f9146b0Srick 		/*NOTREACHED*/
198532d19d43Seric 	}
198625a99e2eSeric }
1987ea4dc939Seric /*
1988e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1989e103b48fSeric **
1990e103b48fSeric **	The signature describes how we are going to send this -- it
1991e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1992e103b48fSeric **	an ordered list of MX hosts.
1993e103b48fSeric **
1994e103b48fSeric **	Parameters:
1995e103b48fSeric **		m -- the mailer describing this host.
1996e103b48fSeric **		host -- the host name.
1997e103b48fSeric **		e -- the current envelope.
1998e103b48fSeric **
1999e103b48fSeric **	Returns:
2000e103b48fSeric **		The signature for this host.
2001e103b48fSeric **
2002e103b48fSeric **	Side Effects:
2003e103b48fSeric **		Can tweak the symbol table.
2004e103b48fSeric */
2005e103b48fSeric 
2006e103b48fSeric char *
2007e103b48fSeric hostsignature(m, host, e)
2008e103b48fSeric 	register MAILER *m;
2009e103b48fSeric 	char *host;
2010e103b48fSeric 	ENVELOPE *e;
2011e103b48fSeric {
2012e103b48fSeric 	register char *p;
2013e103b48fSeric 	register STAB *s;
2014e103b48fSeric 	int i;
2015e103b48fSeric 	int len;
2016e103b48fSeric #ifdef NAMED_BIND
2017e103b48fSeric 	int nmx;
2018e103b48fSeric 	auto int rcode;
2019bafdc4e5Seric 	char *hp;
2020bafdc4e5Seric 	char *endp;
2021516782b4Seric 	int oldoptions;
2022e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2023e103b48fSeric 	static char myhostbuf[MAXNAME];
2024e103b48fSeric #endif
2025e103b48fSeric 
2026e103b48fSeric 	/*
2027e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2028e103b48fSeric 	*/
2029e103b48fSeric 
2030e103b48fSeric 	p = m->m_mailer;
2031e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2032e103b48fSeric 	{
2033e103b48fSeric 		/* just an ordinary mailer */
2034e103b48fSeric 		return host;
2035e103b48fSeric 	}
2036e103b48fSeric 
2037e103b48fSeric 	/*
2038e103b48fSeric 	**  If it is a numeric address, just return it.
2039e103b48fSeric 	*/
2040e103b48fSeric 
2041e103b48fSeric 	if (host[0] == '[')
2042e103b48fSeric 		return host;
2043e103b48fSeric 
2044e103b48fSeric 	/*
2045e103b48fSeric 	**  Look it up in the symbol table.
2046e103b48fSeric 	*/
2047e103b48fSeric 
2048e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2049e103b48fSeric 	if (s->s_hostsig != NULL)
2050e103b48fSeric 		return s->s_hostsig;
2051e103b48fSeric 
2052e103b48fSeric 	/*
2053e103b48fSeric 	**  Not already there -- create a signature.
2054e103b48fSeric 	*/
2055e103b48fSeric 
2056e103b48fSeric #ifdef NAMED_BIND
2057516782b4Seric 	if (ConfigLevel < 2)
2058516782b4Seric 	{
2059516782b4Seric 		oldoptions = _res.options;
2060516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2061516782b4Seric 	}
2062516782b4Seric 
2063e103b48fSeric 	if (myhostbuf[0] == '\0')
20642bc47524Seric 		expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e);
2065e103b48fSeric 
2066bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2067bafdc4e5Seric 	{
2068bafdc4e5Seric 		endp = strchr(hp, ':');
2069bafdc4e5Seric 		if (endp != NULL)
2070bafdc4e5Seric 			*endp = '\0';
2071bafdc4e5Seric 
2072bafdc4e5Seric 		nmx = getmxrr(hp, mxhosts, myhostbuf, &rcode);
20737d55540cSeric 
2074e103b48fSeric 		if (nmx <= 0)
2075e103b48fSeric 		{
2076e103b48fSeric 			register MCI *mci;
2077e103b48fSeric 			extern int errno;
2078e103b48fSeric 			extern MCI *mci_get();
2079e103b48fSeric 
2080e103b48fSeric 			/* update the connection info for this host */
2081bafdc4e5Seric 			mci = mci_get(hp, m);
2082e103b48fSeric 			mci->mci_exitstat = rcode;
2083e103b48fSeric 			mci->mci_errno = errno;
2084e103b48fSeric 
2085e103b48fSeric 			/* and return the original host name as the signature */
2086bafdc4e5Seric 			nmx = 1;
2087bafdc4e5Seric 			mxhosts[0] = hp;
2088e103b48fSeric 		}
2089e103b48fSeric 
2090e103b48fSeric 		len = 0;
2091e103b48fSeric 		for (i = 0; i < nmx; i++)
2092e103b48fSeric 		{
2093e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2094e103b48fSeric 		}
2095bafdc4e5Seric 		if (s->s_hostsig != NULL)
2096bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2097bafdc4e5Seric 		p = xalloc(len);
2098bafdc4e5Seric 		if (s->s_hostsig != NULL)
2099bafdc4e5Seric 		{
2100bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2101bafdc4e5Seric 			free(s->s_hostsig);
2102bafdc4e5Seric 			s->s_hostsig = p;
2103bafdc4e5Seric 			p += strlen(p);
2104bafdc4e5Seric 			*p++ = ':';
2105bafdc4e5Seric 		}
2106bafdc4e5Seric 		else
2107bafdc4e5Seric 			s->s_hostsig = p;
2108e103b48fSeric 		for (i = 0; i < nmx; i++)
2109e103b48fSeric 		{
2110e103b48fSeric 			if (i != 0)
2111e103b48fSeric 				*p++ = ':';
2112e103b48fSeric 			strcpy(p, mxhosts[i]);
2113e103b48fSeric 			p += strlen(p);
2114e103b48fSeric 		}
2115bafdc4e5Seric 		if (endp != NULL)
2116bafdc4e5Seric 			*endp++ = ':';
2117bafdc4e5Seric 	}
2118e103b48fSeric 	makelower(s->s_hostsig);
2119516782b4Seric 	if (ConfigLevel < 2)
2120516782b4Seric 		_res.options = oldoptions;
2121e103b48fSeric #else
2122e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2123e103b48fSeric 	s->s_hostsig = host;
2124e103b48fSeric #endif
2125e103b48fSeric 	if (tTd(17, 1))
2126e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2127e103b48fSeric 	return s->s_hostsig;
2128e103b48fSeric }
2129