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*516782b4Seric static char sccsid[] = "@(#)deliver.c	6.63 (Berkeley) 04/17/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 */
633ee4b0922Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, TRUE, FALSE,
6349c69ca01Seric 					   TRUE, FALSE, e));
635ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
636588cad61Seric 	define('h', host, e);			/* to host */
6375dfc646bSeric 	Errors = 0;
6385dfc646bSeric 	pvp = pv;
6395dfc646bSeric 	*pvp++ = m->m_argv[0];
6405dfc646bSeric 
6415dfc646bSeric 	/* insert -f or -r flag as appropriate */
64257fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6435dfc646bSeric 	{
64457fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6455dfc646bSeric 			*pvp++ = "-f";
6465dfc646bSeric 		else
6475dfc646bSeric 			*pvp++ = "-r";
648c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6495dfc646bSeric 	}
6505dfc646bSeric 
6515dfc646bSeric 	/*
6525dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6535dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6545dfc646bSeric 	**  be one of these, and there are only a few more slots
6555dfc646bSeric 	**  in the pv after it.
6565dfc646bSeric 	*/
6575dfc646bSeric 
6585dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6595dfc646bSeric 	{
6602bc47524Seric 		/* can't use strchr here because of sign extension problems */
6612bc47524Seric 		while (*p != '\0')
6622bc47524Seric 		{
6632bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6642bc47524Seric 			{
6652bc47524Seric 				if (*p == 'u')
6665dfc646bSeric 					break;
6672bc47524Seric 			}
6682bc47524Seric 		}
6692bc47524Seric 
6702bc47524Seric 		if (*p != '\0')
6715dfc646bSeric 			break;
6725dfc646bSeric 
6735dfc646bSeric 		/* this entry is safe -- go ahead and process it */
674588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6755dfc646bSeric 		*pvp++ = newstr(buf);
6765dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6775dfc646bSeric 		{
67808b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6795dfc646bSeric 			return (-1);
6805dfc646bSeric 		}
6815dfc646bSeric 	}
682c579ef51Seric 
68333db8731Seric 	/*
68433db8731Seric 	**  If we have no substitution for the user name in the argument
68533db8731Seric 	**  list, we know that we must supply the names otherwise -- and
68633db8731Seric 	**  SMTP is the answer!!
68733db8731Seric 	*/
68833db8731Seric 
6895dfc646bSeric 	if (*mvp == NULL)
690c579ef51Seric 	{
691c579ef51Seric 		/* running SMTP */
6922c7e1b8dSeric # ifdef SMTP
693c579ef51Seric 		clever = TRUE;
694c579ef51Seric 		*pvp = NULL;
6956c2c3107Seric # else /* SMTP */
69633db8731Seric 		/* oops!  we don't implement SMTP */
69708b25121Seric 		syserr("554 SMTP style mailer");
6982c7e1b8dSeric 		return (EX_SOFTWARE);
6996c2c3107Seric # endif /* SMTP */
700c579ef51Seric 	}
7015dfc646bSeric 
7025dfc646bSeric 	/*
7035dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7045dfc646bSeric 	**  run through our address list and append all the addresses
7055dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7065dfc646bSeric 	**  always send another copy later.
7075dfc646bSeric 	*/
7085dfc646bSeric 
7095dfc646bSeric 	tobuf[0] = '\0';
710588cad61Seric 	e->e_to = tobuf;
7116259796dSeric 	ctladdr = NULL;
712e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7135dfc646bSeric 	for (; to != NULL; to = to->q_next)
7145dfc646bSeric 	{
7155dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
71657fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7175dfc646bSeric 			break;
7185dfc646bSeric 
7195dfc646bSeric 		/* if already sent or not for this host, don't send */
720ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
721e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
722e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7235dfc646bSeric 			continue;
7246259796dSeric 
7254b22ea87Seric 		/* avoid overflowing tobuf */
726aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7274b22ea87Seric 			break;
7284b22ea87Seric 
7296ef48975Seric 		if (tTd(10, 1))
730772e6e50Seric 		{
731772e6e50Seric 			printf("\nsend to ");
732772e6e50Seric 			printaddr(to, FALSE);
733772e6e50Seric 		}
734772e6e50Seric 
7356259796dSeric 		/* compute effective uid/gid when sending */
7367da1035fSeric 		if (to->q_mailer == ProgMailer)
7376259796dSeric 			ctladdr = getctladdr(to);
7386259796dSeric 
7395dfc646bSeric 		user = to->q_user;
740588cad61Seric 		e->e_to = to->q_paddr;
74175f1ade9Seric 		if (tTd(10, 5))
74275f1ade9Seric 		{
74375f1ade9Seric 			printf("deliver: QDONTSEND ");
74475f1ade9Seric 			printaddr(to, FALSE);
74575f1ade9Seric 		}
746ee4b0922Seric 		to->q_flags |= QDONTSEND;
7475dfc646bSeric 
7485dfc646bSeric 		/*
7495dfc646bSeric 		**  Check to see that these people are allowed to
7505dfc646bSeric 		**  talk to each other.
7512a6e0786Seric 		*/
7522a6e0786Seric 
75369582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
75469582d2fSeric 		{
75569582d2fSeric 			NoReturn = TRUE;
75608b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
75781161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
75869582d2fSeric 			continue;
75969582d2fSeric 		}
760fabb3bd4Seric 		rcode = checkcompat(to, e);
7611793c9c5Seric 		if (rcode != EX_OK)
7625dfc646bSeric 		{
76381161401Seric 			giveresponse(rcode, m, NULL, e);
7645dfc646bSeric 			continue;
7655dfc646bSeric 		}
7662a6e0786Seric 
7672a6e0786Seric 		/*
7689ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7699ec9501bSeric 		**	about them.
77025a99e2eSeric 		*/
77125a99e2eSeric 
77257fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
77325a99e2eSeric 		{
7741d8f1806Seric 			stripquotes(user);
7751d8f1806Seric 			stripquotes(host);
77625a99e2eSeric 		}
77725a99e2eSeric 
778cdb828c5Seric 		/* hack attack -- delivermail compatibility */
779cdb828c5Seric 		if (m == ProgMailer && *user == '|')
780cdb828c5Seric 			user++;
781cdb828c5Seric 
78225a99e2eSeric 		/*
7833efaed6eSeric 		**  If an error message has already been given, don't
7843efaed6eSeric 		**	bother to send to this address.
7853efaed6eSeric 		**
7863efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7873efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
7883efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
7893efaed6eSeric 		*/
7903efaed6eSeric 
7916cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7923efaed6eSeric 			continue;
7933efaed6eSeric 
794f2fec898Seric 		/* save statistics.... */
795588cad61Seric 		markstats(e, to);
796f2fec898Seric 
7973efaed6eSeric 		/*
79825a99e2eSeric 		**  See if this user name is "special".
79925a99e2eSeric 		**	If the user name has a slash in it, assume that this
80051552439Seric 		**	is a file -- send it off without further ado.  Note
80151552439Seric 		**	that this type of addresses is not processed along
80251552439Seric 		**	with the others, so we fudge on the To person.
80325a99e2eSeric 		*/
80425a99e2eSeric 
8052c017f8dSeric 		if (m == FileMailer)
80625a99e2eSeric 		{
807b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
80881161401Seric 			giveresponse(rcode, m, NULL, e);
809dde5acadSeric 			if (rcode == EX_OK)
810dde5acadSeric 				to->q_flags |= QSENT;
8115dfc646bSeric 			continue;
81225a99e2eSeric 		}
81325a99e2eSeric 
81413bbc08cSeric 		/*
81513bbc08cSeric 		**  Address is verified -- add this user to mailer
81613bbc08cSeric 		**  argv, and add it to the print list of recipients.
81713bbc08cSeric 		*/
81813bbc08cSeric 
819508daeccSeric 		/* link together the chain of recipients */
820508daeccSeric 		to->q_tchain = tochain;
821508daeccSeric 		tochain = to;
822508daeccSeric 
8235dfc646bSeric 		/* create list of users for error messages */
824db8841e9Seric 		(void) strcat(tobuf, ",");
825db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
826588cad61Seric 		define('u', user, e);		/* to user */
827588cad61Seric 		define('z', to->q_home, e);	/* user's home */
8285dfc646bSeric 
829c579ef51Seric 		/*
830508daeccSeric 		**  Expand out this user into argument list.
831c579ef51Seric 		*/
832c579ef51Seric 
833508daeccSeric 		if (!clever)
834c579ef51Seric 		{
835588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8365dfc646bSeric 			*pvp++ = newstr(buf);
8375dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8385dfc646bSeric 			{
8395dfc646bSeric 				/* allow some space for trailing parms */
8405dfc646bSeric 				break;
8415dfc646bSeric 			}
8425dfc646bSeric 		}
843c579ef51Seric 	}
8445dfc646bSeric 
845145b49b1Seric 	/* see if any addresses still exist */
846145b49b1Seric 	if (tobuf[0] == '\0')
847c579ef51Seric 	{
848588cad61Seric 		define('g', (char *) NULL, e);
849145b49b1Seric 		return (0);
850c579ef51Seric 	}
851145b49b1Seric 
8525dfc646bSeric 	/* print out messages as full list */
85363780dbdSeric 	e->e_to = tobuf + 1;
8545dfc646bSeric 
8555dfc646bSeric 	/*
8565dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8575dfc646bSeric 	*/
8585dfc646bSeric 
859c579ef51Seric 	while (!clever && *++mvp != NULL)
8605dfc646bSeric 	{
861588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8625dfc646bSeric 		*pvp++ = newstr(buf);
8635dfc646bSeric 		if (pvp >= &pv[MAXPV])
86408b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8655dfc646bSeric 	}
8665dfc646bSeric 	*pvp++ = NULL;
8675dfc646bSeric 
86825a99e2eSeric 	/*
86925a99e2eSeric 	**  Call the mailer.
8706328bdf7Seric 	**	The argument vector gets built, pipes
87125a99e2eSeric 	**	are created as necessary, and we fork & exec as
8726328bdf7Seric 	**	appropriate.
873c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
87425a99e2eSeric 	*/
87525a99e2eSeric 
8767ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
87786b26461Seric 		ctladdr = &e->e_from;
878134746fbSeric #ifdef NAMED_BIND
8792bcc6d2dSeric 	if (ConfigLevel < 2)
880912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
881134746fbSeric #endif
8829c9e68d9Seric 
8839c9e68d9Seric 	if (tTd(11, 1))
884134746fbSeric 	{
8859c9e68d9Seric 		printf("openmailer:");
8869c9e68d9Seric 		printav(pv);
8879c9e68d9Seric 	}
8889c9e68d9Seric 	errno = 0;
8899c9e68d9Seric 
8909c9e68d9Seric 	CurHostName = m->m_mailer;
8919c9e68d9Seric 
8929c9e68d9Seric 	/*
8939c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
8949c9e68d9Seric 	**  connection.
8959c9e68d9Seric 	**	In this case we don't actually fork.  We must be
8969c9e68d9Seric 	**	running SMTP for this to work.  We will return a
8979c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
8989c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
8999c9e68d9Seric 	*/
9009c9e68d9Seric 
9019c9e68d9Seric 	curhost = NULL;
9029c9e68d9Seric 
9039c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9049c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9059c9e68d9Seric 	{
9069c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9079c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9089c9e68d9Seric 		mci->mci_in = stdin;
9099c9e68d9Seric 		mci->mci_out = stdout;
9109c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9119c9e68d9Seric 		mci->mci_mailer = m;
9129c9e68d9Seric 	}
9139c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9149c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9159c9e68d9Seric 	{
9169c9e68d9Seric #ifdef DAEMON
9179c9e68d9Seric 		register int i;
9189c9e68d9Seric 		register u_short port;
9199c9e68d9Seric 		extern MCI *mci_get();
9209c9e68d9Seric 		extern char *hostsignature();
9219c9e68d9Seric 
9229c9e68d9Seric 		CurHostName = pv[1];
9239c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9249c9e68d9Seric 
9259c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9269c9e68d9Seric 		{
9279c9e68d9Seric 			syserr("null signature");
928845e533cSeric 			rcode = EX_OSERR;
929b31e7f2bSeric 			goto give_up;
930b31e7f2bSeric 		}
9319c9e68d9Seric 
9329c9e68d9Seric 		if (!clever)
9339c9e68d9Seric 		{
9349c9e68d9Seric 			syserr("554 non-clever IPC");
9359c9e68d9Seric 			rcode = EX_OSERR;
9369c9e68d9Seric 			goto give_up;
9379c9e68d9Seric 		}
9389c9e68d9Seric 		if (pv[2] != NULL)
9399c9e68d9Seric 			port = atoi(pv[2]);
9409c9e68d9Seric 		else
9419c9e68d9Seric 			port = 0;
9429c9e68d9Seric tryhost:
9439c9e68d9Seric 		mci = NULL;
9449c9e68d9Seric 		while (*curhost != '\0')
9459c9e68d9Seric 		{
9469c9e68d9Seric 			register char *p;
9479c9e68d9Seric 			static char hostbuf[MAXNAME];
9489c9e68d9Seric 
9499c9e68d9Seric 			mci = NULL;
9509c9e68d9Seric 
9519c9e68d9Seric 			/* pull the next host from the signature */
9529c9e68d9Seric 			p = strchr(curhost, ':');
9539c9e68d9Seric 			if (p == NULL)
9549c9e68d9Seric 				p = &curhost[strlen(curhost)];
9559c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9569c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9579c9e68d9Seric 			if (*p != '\0')
9589c9e68d9Seric 				p++;
9599c9e68d9Seric 			curhost = p;
9609c9e68d9Seric 
9619c9e68d9Seric 			/* see if we already know that this host is fried */
9629c9e68d9Seric 			CurHostName = hostbuf;
9639c9e68d9Seric 			mci = mci_get(hostbuf, m);
9649c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9659c9e68d9Seric 			{
9669c9e68d9Seric 				if (tTd(11, 1))
9679c9e68d9Seric 				{
9689c9e68d9Seric 					printf("openmailer: ");
9699c9e68d9Seric 					mci_dump(mci);
9709c9e68d9Seric 				}
9719c9e68d9Seric 				CurHostName = mci->mci_host;
9729c9e68d9Seric 				break;
9739c9e68d9Seric 			}
9749c9e68d9Seric 			mci->mci_mailer = m;
9759c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9769c9e68d9Seric 				continue;
9779c9e68d9Seric 
9789c9e68d9Seric 			/* try the connection */
9799c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9809c9e68d9Seric 			message("Connecting to %s (%s)...",
9819c9e68d9Seric 				hostbuf, m->m_name);
9829c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
9839c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
9849c9e68d9Seric 			mci->mci_exitstat = i;
9859c9e68d9Seric 			mci->mci_errno = errno;
9869c9e68d9Seric 			if (i == EX_OK)
9879c9e68d9Seric 			{
9889c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
9899c9e68d9Seric 				mci_cache(mci);
9909c9e68d9Seric 				break;
9919c9e68d9Seric 			}
9929c9e68d9Seric 			else if (tTd(11, 1))
9939c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
9949c9e68d9Seric 					i, errno);
9959c9e68d9Seric 
9969c9e68d9Seric 
9979c9e68d9Seric 			/* enter status of this host */
9989c9e68d9Seric 			setstat(i);
9999c9e68d9Seric 		}
10009c9e68d9Seric 		mci->mci_pid = 0;
10019c9e68d9Seric #else /* no DAEMON */
10029c9e68d9Seric 		syserr("554 openmailer: no IPC");
10039c9e68d9Seric 		if (tTd(11, 1))
10049c9e68d9Seric 			printf("openmailer: NULL\n");
10059c9e68d9Seric 		return NULL;
10069c9e68d9Seric #endif /* DAEMON */
10079c9e68d9Seric 	}
10089c9e68d9Seric 	else
10099c9e68d9Seric 	{
10106fe8c3bcSeric 		int i;
10116fe8c3bcSeric 		struct stat stbuf;
10126fe8c3bcSeric 
10136fe8c3bcSeric 		/* make absolutely certain 0, 1, and 2 are in use */
10146fe8c3bcSeric 		for (i = 0; i < 3; i++)
10156fe8c3bcSeric 		{
10166fe8c3bcSeric 			if (fstat(i, &stbuf) < 0)
10176fe8c3bcSeric 			{
10186fe8c3bcSeric 				/* oops.... */
10190e3bfef5Seric 				int fd;
10200e3bfef5Seric 
10210e3bfef5Seric 				syserr("%s... openmailer(%s): fd %d not open",
10220e3bfef5Seric 					e->e_to, m->m_name, i);
10230e3bfef5Seric 				fd = open("/dev/null", O_RDONLY, 0666);
10240e3bfef5Seric 				if (fd != i)
10250e3bfef5Seric 				{
10260e3bfef5Seric 					(void) dup2(fd, i);
10270e3bfef5Seric 					(void) close(fd);
10280e3bfef5Seric 				}
10296fe8c3bcSeric 			}
10306fe8c3bcSeric 		}
10316fe8c3bcSeric 
10329c9e68d9Seric 		/* create a pipe to shove the mail through */
10339c9e68d9Seric 		if (pipe(mpvect) < 0)
10349c9e68d9Seric 		{
10350e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
10360e3bfef5Seric 				e->e_to, m->m_name);
10379c9e68d9Seric 			if (tTd(11, 1))
10389c9e68d9Seric 				printf("openmailer: NULL\n");
10399c9e68d9Seric 			rcode = EX_OSERR;
10409c9e68d9Seric 			goto give_up;
10419c9e68d9Seric 		}
10429c9e68d9Seric 
10439c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10449c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10459c9e68d9Seric 		{
10460e3bfef5Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
10470e3bfef5Seric 				e->e_to, m->m_name);
10489c9e68d9Seric 			(void) close(mpvect[0]);
10499c9e68d9Seric 			(void) close(mpvect[1]);
10509c9e68d9Seric 			if (tTd(11, 1))
10519c9e68d9Seric 				printf("openmailer: NULL\n");
10529c9e68d9Seric 			rcode = EX_OSERR;
10539c9e68d9Seric 			goto give_up;
10549c9e68d9Seric 		}
10559c9e68d9Seric 
10569c9e68d9Seric 		/*
10579c9e68d9Seric 		**  Actually fork the mailer process.
10589c9e68d9Seric 		**	DOFORK is clever about retrying.
10599c9e68d9Seric 		**
10609c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10619c9e68d9Seric 		**	around so that endmail will get it.
10629c9e68d9Seric 		*/
10639c9e68d9Seric 
10649c9e68d9Seric 		if (e->e_xfp != NULL)
10659c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10669c9e68d9Seric 		(void) fflush(stdout);
10679c9e68d9Seric # ifdef SIGCHLD
10689c9e68d9Seric 		(void) signal(SIGCHLD, SIG_DFL);
10699c9e68d9Seric # endif /* SIGCHLD */
10709c9e68d9Seric 		DOFORK(FORK);
10719c9e68d9Seric 		/* pid is set by DOFORK */
10729c9e68d9Seric 		if (pid < 0)
10739c9e68d9Seric 		{
10749c9e68d9Seric 			/* failure */
10750e3bfef5Seric 			syserr("%s... openmailer(%s): cannot fork",
10760e3bfef5Seric 				e->e_to, m->m_name);
10779c9e68d9Seric 			(void) close(mpvect[0]);
10789c9e68d9Seric 			(void) close(mpvect[1]);
10799c9e68d9Seric 			if (clever)
10809c9e68d9Seric 			{
10819c9e68d9Seric 				(void) close(rpvect[0]);
10829c9e68d9Seric 				(void) close(rpvect[1]);
10839c9e68d9Seric 			}
10849c9e68d9Seric 			if (tTd(11, 1))
10859c9e68d9Seric 				printf("openmailer: NULL\n");
10869c9e68d9Seric 			rcode = EX_OSERR;
10879c9e68d9Seric 			goto give_up;
10889c9e68d9Seric 		}
10899c9e68d9Seric 		else if (pid == 0)
10909c9e68d9Seric 		{
10919c9e68d9Seric 			int i;
10929c9e68d9Seric 			int saveerrno;
10939c9e68d9Seric 			char **ep;
10949c9e68d9Seric 			char *env[MAXUSERENVIRON];
10959c9e68d9Seric 			extern char **environ;
10969c9e68d9Seric 			extern int DtableSize;
10979c9e68d9Seric 
10989c9e68d9Seric 			/* child -- set up input & exec mailer */
10999c9e68d9Seric 			/* make diagnostic output be standard output */
11009c9e68d9Seric 			(void) signal(SIGINT, SIG_IGN);
11019c9e68d9Seric 			(void) signal(SIGHUP, SIG_IGN);
11029c9e68d9Seric 			(void) signal(SIGTERM, SIG_DFL);
11039c9e68d9Seric 
11049c9e68d9Seric 			/* close any other cached connections */
11059c9e68d9Seric 			mci_flush(FALSE, mci);
11069c9e68d9Seric 
1107b986f6aaSeric 			/* move into some "safe" directory */
1108b986f6aaSeric 			if (m->m_execdir != NULL)
1109b986f6aaSeric 			{
1110b986f6aaSeric 				char *p, *q;
1111b986f6aaSeric 				char buf[MAXLINE];
1112b986f6aaSeric 
1113b986f6aaSeric 				for (p = m->m_execdir; p != NULL; p = q)
1114b986f6aaSeric 				{
1115b986f6aaSeric 					q = strchr(p, ':');
1116b986f6aaSeric 					if (q != NULL)
1117b986f6aaSeric 						*q = '\0';
1118b986f6aaSeric 					expand(p, buf, &buf[sizeof buf] - 1, e);
1119b986f6aaSeric 					if (q != NULL)
1120b986f6aaSeric 						*q++ = ':';
1121b986f6aaSeric 					if (tTd(11, 20))
1122b986f6aaSeric 						printf("openmailer: trydir %s\n",
1123b986f6aaSeric 							buf);
1124b986f6aaSeric 					if (buf[0] != '\0' && chdir(buf) >= 0)
1125b986f6aaSeric 						break;
1126b986f6aaSeric 				}
1127b986f6aaSeric 			}
1128b986f6aaSeric 
11299c9e68d9Seric 			/* arrange to filter std & diag output of command */
11309c9e68d9Seric 			if (clever)
11319c9e68d9Seric 			{
11329c9e68d9Seric 				(void) close(rpvect[0]);
11336fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
11346fe8c3bcSeric 				{
11350e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
11360e3bfef5Seric 						e->e_to, m->m_name, rpvect[1]);
11376fe8c3bcSeric 					_exit(EX_OSERR);
11386fe8c3bcSeric 				}
11399c9e68d9Seric 				(void) close(rpvect[1]);
11409c9e68d9Seric 			}
11419c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11429c9e68d9Seric 			{
11439c9e68d9Seric 				/* put mailer output in transcript */
11446fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11456fe8c3bcSeric 				{
11460e3bfef5Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
11470e3bfef5Seric 						e->e_to, m->m_name,
11486fe8c3bcSeric 						fileno(e->e_xfp));
11496fe8c3bcSeric 					_exit(EX_OSERR);
11509c9e68d9Seric 				}
11516fe8c3bcSeric 			}
11526fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11536fe8c3bcSeric 			{
11540e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
11550e3bfef5Seric 					e->e_to, m->m_name);
11566fe8c3bcSeric 				_exit(EX_OSERR);
11576fe8c3bcSeric 			}
11589c9e68d9Seric 
11599c9e68d9Seric 			/* arrange to get standard input */
11609c9e68d9Seric 			(void) close(mpvect[1]);
11619c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11629c9e68d9Seric 			{
11630e3bfef5Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
11640e3bfef5Seric 					e->e_to, m->m_name, mpvect[0]);
11659c9e68d9Seric 				_exit(EX_OSERR);
11669c9e68d9Seric 			}
11679c9e68d9Seric 			(void) close(mpvect[0]);
11689c9e68d9Seric 			if (!bitnset(M_RESTR, m->m_flags))
11699c9e68d9Seric 			{
11709c9e68d9Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
11719c9e68d9Seric 				{
11729c9e68d9Seric 					(void) setgid(DefGid);
11739c9e68d9Seric 					(void) initgroups(DefUser, DefGid);
11749c9e68d9Seric 					(void) setuid(DefUid);
11759c9e68d9Seric 				}
11769c9e68d9Seric 				else
11779c9e68d9Seric 				{
11789c9e68d9Seric 					(void) setgid(ctladdr->q_gid);
11799c9e68d9Seric 					(void) initgroups(ctladdr->q_ruser?
11809c9e68d9Seric 						ctladdr->q_ruser: ctladdr->q_user,
11819c9e68d9Seric 						ctladdr->q_gid);
11829c9e68d9Seric 					(void) setuid(ctladdr->q_uid);
11839c9e68d9Seric 				}
11849c9e68d9Seric 			}
11859c9e68d9Seric 
11869c9e68d9Seric 			/* arrange for all the files to be closed */
11879c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
11889c9e68d9Seric 			{
11899c9e68d9Seric 				register int j;
11909c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
11919c9e68d9Seric 					(void)fcntl(i, F_SETFD, j|1);
11929c9e68d9Seric 			}
11939c9e68d9Seric 
11949c9e68d9Seric 			/* set up the mailer environment */
11959c9e68d9Seric 			i = 0;
11969c9e68d9Seric 			env[i++] = "AGENT=sendmail";
11979c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
11989c9e68d9Seric 			{
11999c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
12009c9e68d9Seric 					env[i++] = *ep;
12019c9e68d9Seric 			}
12029c9e68d9Seric 			env[i++] = NULL;
12039c9e68d9Seric 
12049c9e68d9Seric 			/* try to execute the mailer */
12059c9e68d9Seric 			execve(m->m_mailer, pv, env);
12069c9e68d9Seric 			saveerrno = errno;
12079c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
12089c9e68d9Seric 			if (m == LocalMailer)
12099c9e68d9Seric 				_exit(EX_TEMPFAIL);
12109c9e68d9Seric 			if (transienterror(saveerrno))
12119c9e68d9Seric 				_exit(EX_TEMPFAIL);
12129c9e68d9Seric 			_exit(EX_UNAVAILABLE);
12139c9e68d9Seric 		}
12149c9e68d9Seric 
12159c9e68d9Seric 		/*
12169c9e68d9Seric 		**  Set up return value.
12179c9e68d9Seric 		*/
12189c9e68d9Seric 
12199c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
12209c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
12219c9e68d9Seric 		mci->mci_mailer = m;
12229c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
12239c9e68d9Seric 		mci->mci_pid = pid;
12249c9e68d9Seric 		(void) close(mpvect[0]);
12259c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
12269c9e68d9Seric 		if (clever)
12279c9e68d9Seric 		{
12289c9e68d9Seric 			(void) close(rpvect[1]);
12299c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
12309c9e68d9Seric 		}
12319c9e68d9Seric 		else
12329c9e68d9Seric 		{
12339c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
12349c9e68d9Seric 			mci->mci_in = NULL;
12359c9e68d9Seric 		}
12369c9e68d9Seric 	}
12379c9e68d9Seric 
12389c9e68d9Seric 	/*
12399c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12409c9e68d9Seric 	*/
12419c9e68d9Seric 
12429c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12439c9e68d9Seric 	{
12449c9e68d9Seric 		smtpinit(m, mci, e);
12459c9e68d9Seric 	}
12469c9e68d9Seric 	if (tTd(11, 1))
12479c9e68d9Seric 	{
12489c9e68d9Seric 		printf("openmailer: ");
12499c9e68d9Seric 		mci_dump(mci);
12509c9e68d9Seric 	}
12519c9e68d9Seric 
12529c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1253b31e7f2bSeric 	{
1254b31e7f2bSeric 		/* couldn't open the mailer */
1255b31e7f2bSeric 		rcode = mci->mci_exitstat;
12562a6bc25bSeric 		errno = mci->mci_errno;
1257b31e7f2bSeric 		if (rcode == EX_OK)
1258b31e7f2bSeric 		{
1259b31e7f2bSeric 			/* shouldn't happen */
126008b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12616b0f339dSeric 				rcode, mci->mci_state, firstsig);
1262b31e7f2bSeric 			rcode = EX_SOFTWARE;
1263b31e7f2bSeric 		}
1264b31e7f2bSeric 	}
1265b31e7f2bSeric 	else if (!clever)
1266b31e7f2bSeric 	{
1267b31e7f2bSeric 		/*
1268b31e7f2bSeric 		**  Format and send message.
1269b31e7f2bSeric 		*/
127015d084d5Seric 
1271b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1272b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1273b31e7f2bSeric 		putline("\n", mci->mci_out, m);
1274b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
1275b31e7f2bSeric 
1276b31e7f2bSeric 		/* get the exit status */
1277c9be6216Seric 		rcode = endmailer(mci, e, pv);
1278134746fbSeric 	}
1279134746fbSeric 	else
1280b31e7f2bSeric #ifdef SMTP
1281134746fbSeric 	{
1282b31e7f2bSeric 		/*
1283b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1284b31e7f2bSeric 		*/
128515d084d5Seric 
1286b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1287b31e7f2bSeric 		if (rcode == EX_OK)
128875889e88Seric 		{
1289ded0d3daSkarels 			register char *t = tobuf;
1290ded0d3daSkarels 			register int i;
1291ded0d3daSkarels 
1292588cad61Seric 			/* send the recipient list */
129363780dbdSeric 			tobuf[0] = '\0';
129475889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
129575889e88Seric 			{
129663780dbdSeric 				e->e_to = to->q_paddr;
129715d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
129875889e88Seric 				{
129983b7ddc9Seric 					markfailure(e, to, i);
130081161401Seric 					giveresponse(i, m, mci, e);
130163780dbdSeric 				}
130275889e88Seric 				else
130375889e88Seric 				{
1304911693bfSbostic 					*t++ = ',';
1305b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1306b31e7f2bSeric 						continue;
1307588cad61Seric 				}
1308588cad61Seric 			}
1309588cad61Seric 
131063780dbdSeric 			/* now send the data */
131163780dbdSeric 			if (tobuf[0] == '\0')
1312b31e7f2bSeric 			{
13139c9e68d9Seric 				rcode = EX_OK;
131463780dbdSeric 				e->e_to = NULL;
1315b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1316b31e7f2bSeric 					smtprset(m, mci, e);
1317b31e7f2bSeric 			}
131875889e88Seric 			else
131975889e88Seric 			{
132063780dbdSeric 				e->e_to = tobuf + 1;
132175889e88Seric 				rcode = smtpdata(m, mci, e);
132263780dbdSeric 			}
132363780dbdSeric 
132463780dbdSeric 			/* now close the connection */
1325b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
132615d084d5Seric 				smtpquit(m, mci, e);
132763780dbdSeric 		}
13289c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
13299c9e68d9Seric 		{
13309c9e68d9Seric 			/* try next MX site */
13319c9e68d9Seric 			goto tryhost;
13329c9e68d9Seric 		}
1333c579ef51Seric 	}
1334b31e7f2bSeric #else /* not SMTP */
1335a05b3449Sbostic 	{
133608b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1337845e533cSeric 		rcode = EX_CONFIG;
1338b31e7f2bSeric 		goto give_up;
1339a05b3449Sbostic 	}
1340b31e7f2bSeric #endif /* SMTP */
1341134746fbSeric #ifdef NAMED_BIND
13422bcc6d2dSeric 	if (ConfigLevel < 2)
1343912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1344134746fbSeric #endif
13455dfc646bSeric 
1346b31e7f2bSeric 	/* arrange a return receipt if requested */
13478c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1348b31e7f2bSeric 	{
1349b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1350b31e7f2bSeric 		/* do we want to send back more info? */
1351b31e7f2bSeric 	}
1352b31e7f2bSeric 
1353c77d1c25Seric 	/*
135463780dbdSeric 	**  Do final status disposal.
135563780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1356c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1357c77d1c25Seric 	**		addressees.
1358c77d1c25Seric 	*/
1359c77d1c25Seric 
1360b31e7f2bSeric   give_up:
136163780dbdSeric 	if (tobuf[0] != '\0')
136281161401Seric 		giveresponse(rcode, m, mci, e);
1363772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1364b31e7f2bSeric 	{
1365dde5acadSeric 		if (rcode != EX_OK)
136683b7ddc9Seric 			markfailure(e, to, rcode);
1367dde5acadSeric 		else
1368655518ecSeric 		{
1369dde5acadSeric 			to->q_flags |= QSENT;
1370655518ecSeric 			e->e_nsent++;
1371655518ecSeric 		}
1372b31e7f2bSeric 	}
1373b31e7f2bSeric 
1374b31e7f2bSeric 	/*
1375b31e7f2bSeric 	**  Restore state and return.
1376b31e7f2bSeric 	*/
1377c77d1c25Seric 
137835490626Seric 	errno = 0;
1379588cad61Seric 	define('g', (char *) NULL, e);
13805826d9d3Seric 	return (rcode);
138125a99e2eSeric }
13825dfc646bSeric /*
138383b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
138483b7ddc9Seric **
138583b7ddc9Seric **	Parameters:
138683b7ddc9Seric **		e -- the envelope we are sending.
138783b7ddc9Seric **		q -- the address to mark.
138883b7ddc9Seric **		rcode -- the code signifying the particular failure.
138983b7ddc9Seric **
139083b7ddc9Seric **	Returns:
139183b7ddc9Seric **		none.
139283b7ddc9Seric **
139383b7ddc9Seric **	Side Effects:
139483b7ddc9Seric **		marks the address (and possibly the envelope) with the
139583b7ddc9Seric **			failure so that an error will be returned or
139683b7ddc9Seric **			the message will be queued, as appropriate.
139783b7ddc9Seric */
139883b7ddc9Seric 
139983b7ddc9Seric markfailure(e, q, rcode)
140083b7ddc9Seric 	register ENVELOPE *e;
140183b7ddc9Seric 	register ADDRESS *q;
140283b7ddc9Seric 	int rcode;
140383b7ddc9Seric {
140419c47125Seric 	char buf[MAXLINE];
140519c47125Seric 	extern char *pintvl();
140619c47125Seric 
140783b7ddc9Seric 	if (rcode == EX_OK)
140883b7ddc9Seric 		return;
1409ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
141083b7ddc9Seric 		q->q_flags |= QBADADDR;
141119c47125Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
141283b7ddc9Seric 	{
141383b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
1414198d9be0Seric 		{
1415198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
141619c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
1417198d9be0Seric 			if (e->e_message != NULL)
1418198d9be0Seric 				free(e->e_message);
1419198d9be0Seric 			e->e_message = newstr(buf);
142008b25121Seric 			message(buf);
1421198d9be0Seric 		}
142283b7ddc9Seric 		q->q_flags |= QBADADDR;
142383b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
1424c13b5dfcSeric 		fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr);
142583b7ddc9Seric 	}
142683b7ddc9Seric 	else
142719c47125Seric 	{
142883b7ddc9Seric 		q->q_flags |= QQUEUEUP;
142919c47125Seric 		if (TimeOuts.to_q_warning > 0 &&
143019c47125Seric 		    curtime() > e->e_ctime + TimeOuts.to_q_warning)
143119c47125Seric 		{
143219c47125Seric 			if (!bitset(EF_WARNING, e->e_flags) &&
143319c47125Seric 			    e->e_class >= 0)
143419c47125Seric 			{
143519c47125Seric 				(void) sprintf(buf,
143619c47125Seric 					"warning: cannot send message for %s",
143719c47125Seric 					pintvl(TimeOuts.to_q_warning, FALSE));
143819c47125Seric 				if (e->e_message != NULL)
143919c47125Seric 					free(e->e_message);
144019c47125Seric 				e->e_message = newstr(buf);
144119c47125Seric 				message(buf);
144219c47125Seric 				e->e_flags |= EF_WARNING|EF_TIMEOUT;
144319c47125Seric 			}
144419c47125Seric 			fprintf(e->e_xfp,
144519c47125Seric 				"%s... Warning: message still undelivered after %s\n",
144619c47125Seric 				q->q_paddr, pintvl(TimeOuts.to_q_warning, FALSE));
144719c47125Seric 			fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
144819c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
144919c47125Seric 		}
145019c47125Seric 	}
145183b7ddc9Seric }
145283b7ddc9Seric /*
1453c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1454c579ef51Seric **
1455c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1456c579ef51Seric **	violation), so we report those specially.  For other
1457c579ef51Seric **	errors, we choose a status message (into statmsg),
1458c579ef51Seric **	and if it represents an error, we print it.
1459c579ef51Seric **
1460c579ef51Seric **	Parameters:
1461c579ef51Seric **		pid -- pid of mailer.
1462c9be6216Seric **		e -- the current envelope.
1463c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1464c9be6216Seric **			(for error messages).
1465c579ef51Seric **
1466c579ef51Seric **	Returns:
1467c579ef51Seric **		exit code of mailer.
1468c579ef51Seric **
1469c579ef51Seric **	Side Effects:
1470c579ef51Seric **		none.
1471c579ef51Seric */
1472c579ef51Seric 
1473c9be6216Seric endmailer(mci, e, pv)
1474b31e7f2bSeric 	register MCI *mci;
1475c9be6216Seric 	register ENVELOPE *e;
1476c9be6216Seric 	char **pv;
1477c579ef51Seric {
1478588cad61Seric 	int st;
1479c579ef51Seric 
148075889e88Seric 	/* close any connections */
148175889e88Seric 	if (mci->mci_in != NULL)
1482c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
148375889e88Seric 	if (mci->mci_out != NULL)
1484c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
148575889e88Seric 	mci->mci_in = mci->mci_out = NULL;
148675889e88Seric 	mci->mci_state = MCIS_CLOSED;
148775889e88Seric 
148833db8731Seric 	/* in the IPC case there is nothing to wait for */
148975889e88Seric 	if (mci->mci_pid == 0)
149033db8731Seric 		return (EX_OK);
149133db8731Seric 
149233db8731Seric 	/* wait for the mailer process to die and collect status */
149375889e88Seric 	st = waitfor(mci->mci_pid);
1494588cad61Seric 	if (st == -1)
149578de67c1Seric 	{
1496c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1497588cad61Seric 		return (EX_SOFTWARE);
1498c579ef51Seric 	}
149933db8731Seric 
150033db8731Seric 	/* see if it died a horrid death */
1501c579ef51Seric 	if ((st & 0377) != 0)
1502c579ef51Seric 	{
1503c9be6216Seric 		syserr("mailer %s died with signal %o", pv[0], st);
1504c9be6216Seric 
1505c9be6216Seric 		/* log the arguments */
1506c9be6216Seric 		if (e->e_xfp != NULL)
1507c9be6216Seric 		{
1508c9be6216Seric 			register char **av;
1509c9be6216Seric 
1510c9be6216Seric 			fprintf(e->e_xfp, "Arguments:");
1511c9be6216Seric 			for (av = pv; *av != NULL; av++)
1512c9be6216Seric 				fprintf(e->e_xfp, " %s", *av);
1513c9be6216Seric 			fprintf(e->e_xfp, "\n");
1514c9be6216Seric 		}
1515c9be6216Seric 
15165f73204aSeric 		ExitStat = EX_TEMPFAIL;
15175f73204aSeric 		return (EX_TEMPFAIL);
1518c579ef51Seric 	}
151933db8731Seric 
152033db8731Seric 	/* normal death -- return status */
1521588cad61Seric 	st = (st >> 8) & 0377;
1522588cad61Seric 	return (st);
1523c579ef51Seric }
1524c579ef51Seric /*
152525a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
152625a99e2eSeric **
152725a99e2eSeric **	Parameters:
152825a99e2eSeric **		stat -- the status code from the mailer (high byte
152925a99e2eSeric **			only; core dumps must have been taken care of
153025a99e2eSeric **			already).
153181161401Seric **		m -- the mailer info for this mailer.
153281161401Seric **		mci -- the mailer connection info -- can be NULL if the
153381161401Seric **			response is given before the connection is made.
153481161401Seric **		e -- the current envelope.
153525a99e2eSeric **
153625a99e2eSeric **	Returns:
1537db8841e9Seric **		none.
153825a99e2eSeric **
153925a99e2eSeric **	Side Effects:
1540c1f9df2cSeric **		Errors may be incremented.
154125a99e2eSeric **		ExitStat may be set.
154225a99e2eSeric */
154325a99e2eSeric 
154481161401Seric giveresponse(stat, m, mci, e)
154525a99e2eSeric 	int stat;
1546588cad61Seric 	register MAILER *m;
154781161401Seric 	register MCI *mci;
1548198d9be0Seric 	ENVELOPE *e;
154925a99e2eSeric {
155025a99e2eSeric 	register char *statmsg;
155125a99e2eSeric 	extern char *SysExMsg[];
155225a99e2eSeric 	register int i;
1553d4bd8f0eSbostic 	extern int N_SysEx;
1554d4bd8f0eSbostic #ifdef NAMED_BIND
1555d4bd8f0eSbostic 	extern int h_errno;
1556d4bd8f0eSbostic #endif
1557198d9be0Seric 	char buf[MAXLINE];
15587d55540cSeric 	extern char *errstring();
155925a99e2eSeric 
156013bbc08cSeric 	/*
156113bbc08cSeric 	**  Compute status message from code.
156213bbc08cSeric 	*/
156313bbc08cSeric 
156425a99e2eSeric 	i = stat - EX__BASE;
1565588cad61Seric 	if (stat == 0)
15666fe8c3bcSeric 	{
1567588cad61Seric 		statmsg = "250 Sent";
1568ce5531bdSeric 		if (e->e_statmsg != NULL)
15696fe8c3bcSeric 		{
1570ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15716fe8c3bcSeric 			statmsg = buf;
15726fe8c3bcSeric 		}
15736fe8c3bcSeric 	}
1574588cad61Seric 	else if (i < 0 || i > N_SysEx)
1575588cad61Seric 	{
1576588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1577588cad61Seric 		stat = EX_UNAVAILABLE;
1578588cad61Seric 		statmsg = buf;
1579588cad61Seric 	}
1580198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1581198d9be0Seric 	{
15827d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1583d4bd8f0eSbostic #ifdef NAMED_BIND
1584f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1585f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1586f28da541Smiriam 		else
1587d4bd8f0eSbostic #endif
1588f28da541Smiriam 		{
15898557d168Seric 			if (errno != 0)
1590d87e85f3Seric 				statmsg = errstring(errno);
1591d87e85f3Seric 			else
1592d87e85f3Seric 			{
1593d87e85f3Seric #ifdef SMTP
1594d87e85f3Seric 				extern char SmtpError[];
1595d87e85f3Seric 
1596d87e85f3Seric 				statmsg = SmtpError;
15976c2c3107Seric #else /* SMTP */
1598d87e85f3Seric 				statmsg = NULL;
15996c2c3107Seric #endif /* SMTP */
1600d87e85f3Seric 			}
1601f28da541Smiriam 		}
1602d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1603d87e85f3Seric 		{
160487c9b3e7Seric 			(void) strcat(buf, ": ");
1605d87e85f3Seric 			(void) strcat(buf, statmsg);
16068557d168Seric 		}
1607198d9be0Seric 		statmsg = buf;
1608198d9be0Seric 	}
160925a99e2eSeric 	else
1610d87e85f3Seric 	{
161125a99e2eSeric 		statmsg = SysExMsg[i];
16127d55540cSeric 		if (*statmsg++ == ':')
16137d55540cSeric 		{
16147d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
16157d55540cSeric 			statmsg = buf;
16167d55540cSeric 		}
1617d87e85f3Seric 	}
1618588cad61Seric 
1619588cad61Seric 	/*
1620588cad61Seric 	**  Print the message as appropriate
1621588cad61Seric 	*/
1622588cad61Seric 
1623198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1624e12d03eeSeric 		message(&statmsg[4], errstring(errno));
162525a99e2eSeric 	else
162625a99e2eSeric 	{
1627c1f9df2cSeric 		Errors++;
1628e12d03eeSeric 		usrerr(statmsg, errstring(errno));
162925a99e2eSeric 	}
163025a99e2eSeric 
163125a99e2eSeric 	/*
163225a99e2eSeric 	**  Final cleanup.
163325a99e2eSeric 	**	Log a record of the transaction.  Compute the new
163425a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
163525a99e2eSeric 	**	that.
163625a99e2eSeric 	*/
163725a99e2eSeric 
16382f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
163981161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1640eb238f8cSeric 
1641eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1642eb238f8cSeric 		setstat(stat);
1643198d9be0Seric 	if (stat != EX_OK)
1644198d9be0Seric 	{
1645198d9be0Seric 		if (e->e_message != NULL)
1646198d9be0Seric 			free(e->e_message);
1647198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1648198d9be0Seric 	}
16498557d168Seric 	errno = 0;
1650d4bd8f0eSbostic #ifdef NAMED_BIND
1651f28da541Smiriam 	h_errno = 0;
1652d4bd8f0eSbostic #endif
1653eb238f8cSeric }
1654eb238f8cSeric /*
1655eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1656eb238f8cSeric **
1657eb238f8cSeric **	Parameters:
165881161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
165981161401Seric **		mci -- the mailer connection info -- can be NULL if the
166081161401Seric **			log is occuring when no connection is active.
166181161401Seric **		stat -- the message to print for the status.
166281161401Seric **		e -- the current envelope.
1663eb238f8cSeric **
1664eb238f8cSeric **	Returns:
1665eb238f8cSeric **		none
1666eb238f8cSeric **
1667eb238f8cSeric **	Side Effects:
1668eb238f8cSeric **		none
1669eb238f8cSeric */
1670eb238f8cSeric 
167181161401Seric logdelivery(m, mci, stat, e)
167281161401Seric 	MAILER *m;
167381161401Seric 	register MCI *mci;
1674eb238f8cSeric 	char *stat;
1675b31e7f2bSeric 	register ENVELOPE *e;
16765cf56be3Seric {
1677eb238f8cSeric # ifdef LOG
16789507d1f9Seric 	char *curhost;
1679d6acf3eeSeric 	char buf[512];
16809507d1f9Seric 	extern char *pintvl();
16819507d1f9Seric 	extern char *macvalue();
16829507d1f9Seric 
168348ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
168481161401Seric 
168548ed5d33Seric 	if (m != NULL)
168671ff6caaSeric 	{
168748ed5d33Seric 		(void) strcat(buf, ", mailer=");
168848ed5d33Seric 		(void) strcat(buf, m->m_name);
168971ff6caaSeric 	}
169048ed5d33Seric 
169148ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
169271ff6caaSeric 	{
169371ff6caaSeric # ifdef DAEMON
1694e2f2f828Seric 		extern SOCKADDR CurHostAddr;
1695e2f2f828Seric 		extern char *anynet_ntoa();
169648ed5d33Seric # endif
169771ff6caaSeric 
169848ed5d33Seric 		(void) strcat(buf, ", relay=");
169948ed5d33Seric 		(void) strcat(buf, mci->mci_host);
170048ed5d33Seric 
170148ed5d33Seric # ifdef DAEMON
170248ed5d33Seric 		(void) strcat(buf, " (");
1703e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
170448ed5d33Seric 		(void) strcat(buf, ")");
170571ff6caaSeric # endif
170671ff6caaSeric 	}
17079507d1f9Seric 	else
170848ed5d33Seric 	{
170948ed5d33Seric 		char *p = macvalue('h', e);
17109507d1f9Seric 
171148ed5d33Seric 		if (p != NULL && p[0] != '\0')
171248ed5d33Seric 		{
171348ed5d33Seric 			(void) strcat(buf, ", relay=");
171448ed5d33Seric 			(void) strcat(buf, p);
171548ed5d33Seric 		}
171648ed5d33Seric 	}
1717d6acf3eeSeric 
1718d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1719d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
17206c2c3107Seric # endif /* LOG */
172125a99e2eSeric }
172225a99e2eSeric /*
172351552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
172425a99e2eSeric **
172551552439Seric **	This can be made an arbitrary message separator by changing $l
172651552439Seric **
17279b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
17289b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
17299b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
17309b6c17a6Seric **	this kind of antique garbage????
173125a99e2eSeric **
173225a99e2eSeric **	Parameters:
173351552439Seric **		fp -- the file to output to.
173451552439Seric **		m -- the mailer describing this entry.
173525a99e2eSeric **
173625a99e2eSeric **	Returns:
173751552439Seric **		none
173825a99e2eSeric **
173925a99e2eSeric **	Side Effects:
174051552439Seric **		outputs some text to fp.
174125a99e2eSeric */
174225a99e2eSeric 
1743b31e7f2bSeric putfromline(fp, m, e)
174451552439Seric 	register FILE *fp;
174551552439Seric 	register MAILER *m;
1746b31e7f2bSeric 	ENVELOPE *e;
174725a99e2eSeric {
17482bc47524Seric 	char *template = "\201l\n";
174951552439Seric 	char buf[MAXLINE];
175025a99e2eSeric 
175157fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
175251552439Seric 		return;
175313bbc08cSeric 
17542c7e1b8dSeric # ifdef UGLYUUCP
175557fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
175674b6e67bSeric 	{
1757ea09d6edSeric 		char *bang;
1758ea09d6edSeric 		char xbuf[MAXLINE];
175974b6e67bSeric 
1760ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17616c2c3107Seric 		bang = strchr(buf, '!');
176274b6e67bSeric 		if (bang == NULL)
176308b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
176474b6e67bSeric 		else
1765588cad61Seric 		{
1766ea09d6edSeric 			*bang++ = '\0';
17672bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1768ea09d6edSeric 			template = xbuf;
176974b6e67bSeric 		}
1770588cad61Seric 	}
17716c2c3107Seric # endif /* UGLYUUCP */
1772b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
177377b52738Seric 	putline(buf, fp, m);
1774bc6e2962Seric }
1775bc6e2962Seric /*
177651552439Seric **  PUTBODY -- put the body of a message.
177751552439Seric **
177851552439Seric **	Parameters:
177951552439Seric **		fp -- file to output onto.
178077b52738Seric **		m -- a mailer descriptor to control output format.
17819a6a5f55Seric **		e -- the envelope to put out.
178251552439Seric **
178351552439Seric **	Returns:
178451552439Seric **		none.
178551552439Seric **
178651552439Seric **	Side Effects:
178751552439Seric **		The message is written onto fp.
178851552439Seric */
178951552439Seric 
179077b52738Seric putbody(fp, m, e)
179151552439Seric 	FILE *fp;
1792588cad61Seric 	MAILER *m;
17939a6a5f55Seric 	register ENVELOPE *e;
179451552439Seric {
179577b52738Seric 	char buf[MAXLINE];
179651552439Seric 
179751552439Seric 	/*
179851552439Seric 	**  Output the body of the message
179951552439Seric 	*/
180051552439Seric 
18019a6a5f55Seric 	if (e->e_dfp == NULL)
180251552439Seric 	{
18039a6a5f55Seric 		if (e->e_df != NULL)
18049a6a5f55Seric 		{
18059a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
18069a6a5f55Seric 			if (e->e_dfp == NULL)
18078f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
18088f9146b0Srick 				e->e_df, e->e_to, e->e_from);
18099a6a5f55Seric 		}
18109a6a5f55Seric 		else
181177b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
18129a6a5f55Seric 	}
18139a6a5f55Seric 	if (e->e_dfp != NULL)
18149a6a5f55Seric 	{
18159a6a5f55Seric 		rewind(e->e_dfp);
181677b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
181724fc8aeeSeric 		{
181824fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1819d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
18203462ad9eSeric 				(void) putc('>', fp);
182177b52738Seric 			putline(buf, fp, m);
182224fc8aeeSeric 		}
182351552439Seric 
18249a6a5f55Seric 		if (ferror(e->e_dfp))
182551552439Seric 		{
182651552439Seric 			syserr("putbody: read error");
182751552439Seric 			ExitStat = EX_IOERR;
182851552439Seric 		}
182951552439Seric 	}
183051552439Seric 
183151552439Seric 	(void) fflush(fp);
183251552439Seric 	if (ferror(fp) && errno != EPIPE)
183351552439Seric 	{
183451552439Seric 		syserr("putbody: write error");
183551552439Seric 		ExitStat = EX_IOERR;
183651552439Seric 	}
183751552439Seric 	errno = 0;
183825a99e2eSeric }
183925a99e2eSeric /*
184025a99e2eSeric **  MAILFILE -- Send a message to a file.
184125a99e2eSeric **
1842f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1843f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1844f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1845f129ec7dSeric **	sendmail runs as root.
1846f129ec7dSeric **
1847588cad61Seric **	This could be done as a subordinate mailer, except that it
1848588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1849588cad61Seric **	view this as being sufficiently important as to include it
1850588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1851588cad61Seric **	to create another process plus some pipes to save the message.
1852588cad61Seric **
185325a99e2eSeric **	Parameters:
185425a99e2eSeric **		filename -- the name of the file to send to.
18556259796dSeric **		ctladdr -- the controlling address header -- includes
18566259796dSeric **			the userid/groupid to be when sending.
185725a99e2eSeric **
185825a99e2eSeric **	Returns:
185925a99e2eSeric **		The exit code associated with the operation.
186025a99e2eSeric **
186125a99e2eSeric **	Side Effects:
186225a99e2eSeric **		none.
186325a99e2eSeric */
186425a99e2eSeric 
1865b31e7f2bSeric mailfile(filename, ctladdr, e)
186625a99e2eSeric 	char *filename;
18676259796dSeric 	ADDRESS *ctladdr;
1868b31e7f2bSeric 	register ENVELOPE *e;
186925a99e2eSeric {
187025a99e2eSeric 	register FILE *f;
187132d19d43Seric 	register int pid;
187215d084d5Seric 	int mode;
187325a99e2eSeric 
187432d19d43Seric 	/*
187532d19d43Seric 	**  Fork so we can change permissions here.
187632d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
187732d19d43Seric 	**	the complications of calling subroutines, etc.
187832d19d43Seric 	*/
187932d19d43Seric 
188032d19d43Seric 	DOFORK(fork);
188132d19d43Seric 
188232d19d43Seric 	if (pid < 0)
188332d19d43Seric 		return (EX_OSERR);
188432d19d43Seric 	else if (pid == 0)
188532d19d43Seric 	{
188632d19d43Seric 		/* child -- actually write to file */
1887f129ec7dSeric 		struct stat stb;
1888f129ec7dSeric 
18890984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
18900984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
18910984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
18923462ad9eSeric 		(void) umask(OldUmask);
189395f16dc0Seric 
1894f129ec7dSeric 		if (stat(filename, &stb) < 0)
1895e6e1265fSeric 			stb.st_mode = 0666;
189615d084d5Seric 		mode = stb.st_mode;
189795f16dc0Seric 
189895f16dc0Seric 		/* limit the errors to those actually caused in the child */
189995f16dc0Seric 		errno = 0;
190095f16dc0Seric 		ExitStat = EX_OK;
190195f16dc0Seric 
1902f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1903f129ec7dSeric 			exit(EX_CANTCREAT);
190403827b5fSeric 		if (ctladdr == NULL)
19058f9146b0Srick 			ctladdr = &e->e_from;
190615d084d5Seric 		else
190715d084d5Seric 		{
190815d084d5Seric 			/* ignore setuid and setgid bits */
190915d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
191015d084d5Seric 		}
191115d084d5Seric 
19128f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
19138f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
19148f9146b0Srick 		{
19158f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
191695f16dc0Seric 			if (e->e_dfp == NULL)
191795f16dc0Seric 			{
19188f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
19198f9146b0Srick 					e->e_df, e->e_to, e->e_from);
19208f9146b0Srick 			}
19218f9146b0Srick 		}
19228f9146b0Srick 
192315d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1924e36b99e2Seric 		{
192595f16dc0Seric 			if (ctladdr->q_uid == 0)
192695f16dc0Seric 			{
1927e36b99e2Seric 				(void) setgid(DefGid);
1928898a126bSbostic 				(void) initgroups(DefUser, DefGid);
192995f16dc0Seric 			}
193095f16dc0Seric 			else
193195f16dc0Seric 			{
19326259796dSeric 				(void) setgid(ctladdr->q_gid);
1933898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1934898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1935898a126bSbostic 					ctladdr->q_gid);
1936898a126bSbostic 			}
1937e36b99e2Seric 		}
193815d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1939e36b99e2Seric 		{
1940e36b99e2Seric 			if (ctladdr->q_uid == 0)
1941e36b99e2Seric 				(void) setuid(DefUid);
1942e36b99e2Seric 			else
19436259796dSeric 				(void) setuid(ctladdr->q_uid);
1944e36b99e2Seric 		}
194595f16dc0Seric 		FileName = filename;
194695f16dc0Seric 		LineNumber = 0;
194727628d59Seric 		f = dfopen(filename, "a");
194825a99e2eSeric 		if (f == NULL)
194995f16dc0Seric 		{
195008b25121Seric 			message("554 cannot open");
195132d19d43Seric 			exit(EX_CANTCREAT);
195295f16dc0Seric 		}
195325a99e2eSeric 
1954b31e7f2bSeric 		putfromline(f, ProgMailer, e);
1955b843d759Seric 		(*e->e_puthdr)(f, ProgMailer, e);
195677b52738Seric 		putline("\n", f, ProgMailer);
1957b843d759Seric 		(*e->e_putbody)(f, ProgMailer, e);
195877b52738Seric 		putline("\n", f, ProgMailer);
195995f16dc0Seric 		if (ferror(f))
196095f16dc0Seric 		{
196108b25121Seric 			message("451 I/O error");
196295f16dc0Seric 			setstat(EX_IOERR);
196395f16dc0Seric 		}
1964ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
196532d19d43Seric 		(void) fflush(stdout);
1966e36b99e2Seric 
196727628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1968c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
196995f16dc0Seric 		exit(ExitStat);
197013bbc08cSeric 		/*NOTREACHED*/
197132d19d43Seric 	}
197232d19d43Seric 	else
197332d19d43Seric 	{
197432d19d43Seric 		/* parent -- wait for exit status */
1975588cad61Seric 		int st;
197632d19d43Seric 
1977588cad61Seric 		st = waitfor(pid);
1978588cad61Seric 		if ((st & 0377) != 0)
1979588cad61Seric 			return (EX_UNAVAILABLE);
1980588cad61Seric 		else
1981588cad61Seric 			return ((st >> 8) & 0377);
19828f9146b0Srick 		/*NOTREACHED*/
198332d19d43Seric 	}
198425a99e2eSeric }
1985ea4dc939Seric /*
1986e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1987e103b48fSeric **
1988e103b48fSeric **	The signature describes how we are going to send this -- it
1989e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1990e103b48fSeric **	an ordered list of MX hosts.
1991e103b48fSeric **
1992e103b48fSeric **	Parameters:
1993e103b48fSeric **		m -- the mailer describing this host.
1994e103b48fSeric **		host -- the host name.
1995e103b48fSeric **		e -- the current envelope.
1996e103b48fSeric **
1997e103b48fSeric **	Returns:
1998e103b48fSeric **		The signature for this host.
1999e103b48fSeric **
2000e103b48fSeric **	Side Effects:
2001e103b48fSeric **		Can tweak the symbol table.
2002e103b48fSeric */
2003e103b48fSeric 
2004e103b48fSeric char *
2005e103b48fSeric hostsignature(m, host, e)
2006e103b48fSeric 	register MAILER *m;
2007e103b48fSeric 	char *host;
2008e103b48fSeric 	ENVELOPE *e;
2009e103b48fSeric {
2010e103b48fSeric 	register char *p;
2011e103b48fSeric 	register STAB *s;
2012e103b48fSeric 	int i;
2013e103b48fSeric 	int len;
2014e103b48fSeric #ifdef NAMED_BIND
2015e103b48fSeric 	int nmx;
2016e103b48fSeric 	auto int rcode;
2017bafdc4e5Seric 	char *hp;
2018bafdc4e5Seric 	char *endp;
2019*516782b4Seric 	int oldoptions;
2020e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
2021e103b48fSeric 	static char myhostbuf[MAXNAME];
2022e103b48fSeric #endif
2023e103b48fSeric 
2024e103b48fSeric 	/*
2025e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
2026e103b48fSeric 	*/
2027e103b48fSeric 
2028e103b48fSeric 	p = m->m_mailer;
2029e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
2030e103b48fSeric 	{
2031e103b48fSeric 		/* just an ordinary mailer */
2032e103b48fSeric 		return host;
2033e103b48fSeric 	}
2034e103b48fSeric 
2035e103b48fSeric 	/*
2036e103b48fSeric 	**  If it is a numeric address, just return it.
2037e103b48fSeric 	*/
2038e103b48fSeric 
2039e103b48fSeric 	if (host[0] == '[')
2040e103b48fSeric 		return host;
2041e103b48fSeric 
2042e103b48fSeric 	/*
2043e103b48fSeric 	**  Look it up in the symbol table.
2044e103b48fSeric 	*/
2045e103b48fSeric 
2046e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2047e103b48fSeric 	if (s->s_hostsig != NULL)
2048e103b48fSeric 		return s->s_hostsig;
2049e103b48fSeric 
2050e103b48fSeric 	/*
2051e103b48fSeric 	**  Not already there -- create a signature.
2052e103b48fSeric 	*/
2053e103b48fSeric 
2054e103b48fSeric #ifdef NAMED_BIND
2055*516782b4Seric 	if (ConfigLevel < 2)
2056*516782b4Seric 	{
2057*516782b4Seric 		oldoptions = _res.options;
2058*516782b4Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
2059*516782b4Seric 	}
2060*516782b4Seric 
2061e103b48fSeric 	if (myhostbuf[0] == '\0')
20622bc47524Seric 		expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e);
2063e103b48fSeric 
2064bafdc4e5Seric 	for (hp = host; hp != NULL; hp = endp)
2065bafdc4e5Seric 	{
2066bafdc4e5Seric 		endp = strchr(hp, ':');
2067bafdc4e5Seric 		if (endp != NULL)
2068bafdc4e5Seric 			*endp = '\0';
2069bafdc4e5Seric 
2070bafdc4e5Seric 		nmx = getmxrr(hp, mxhosts, myhostbuf, &rcode);
20717d55540cSeric 
2072e103b48fSeric 		if (nmx <= 0)
2073e103b48fSeric 		{
2074e103b48fSeric 			register MCI *mci;
2075e103b48fSeric 			extern int errno;
2076e103b48fSeric 			extern MCI *mci_get();
2077e103b48fSeric 
2078e103b48fSeric 			/* update the connection info for this host */
2079bafdc4e5Seric 			mci = mci_get(hp, m);
2080e103b48fSeric 			mci->mci_exitstat = rcode;
2081e103b48fSeric 			mci->mci_errno = errno;
2082e103b48fSeric 
2083e103b48fSeric 			/* and return the original host name as the signature */
2084bafdc4e5Seric 			nmx = 1;
2085bafdc4e5Seric 			mxhosts[0] = hp;
2086e103b48fSeric 		}
2087e103b48fSeric 
2088e103b48fSeric 		len = 0;
2089e103b48fSeric 		for (i = 0; i < nmx; i++)
2090e103b48fSeric 		{
2091e103b48fSeric 			len += strlen(mxhosts[i]) + 1;
2092e103b48fSeric 		}
2093bafdc4e5Seric 		if (s->s_hostsig != NULL)
2094bafdc4e5Seric 			len += strlen(s->s_hostsig) + 1;
2095bafdc4e5Seric 		p = xalloc(len);
2096bafdc4e5Seric 		if (s->s_hostsig != NULL)
2097bafdc4e5Seric 		{
2098bafdc4e5Seric 			(void) strcpy(p, s->s_hostsig);
2099bafdc4e5Seric 			free(s->s_hostsig);
2100bafdc4e5Seric 			s->s_hostsig = p;
2101bafdc4e5Seric 			p += strlen(p);
2102bafdc4e5Seric 			*p++ = ':';
2103bafdc4e5Seric 		}
2104bafdc4e5Seric 		else
2105bafdc4e5Seric 			s->s_hostsig = p;
2106e103b48fSeric 		for (i = 0; i < nmx; i++)
2107e103b48fSeric 		{
2108e103b48fSeric 			if (i != 0)
2109e103b48fSeric 				*p++ = ':';
2110e103b48fSeric 			strcpy(p, mxhosts[i]);
2111e103b48fSeric 			p += strlen(p);
2112e103b48fSeric 		}
2113bafdc4e5Seric 		if (endp != NULL)
2114bafdc4e5Seric 			*endp++ = ':';
2115bafdc4e5Seric 	}
2116e103b48fSeric 	makelower(s->s_hostsig);
2117*516782b4Seric 	if (ConfigLevel < 2)
2118*516782b4Seric 		_res.options = oldoptions;
2119e103b48fSeric #else
2120e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2121e103b48fSeric 	s->s_hostsig = host;
2122e103b48fSeric #endif
2123e103b48fSeric 	if (tTd(17, 1))
2124e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2125e103b48fSeric 	return s->s_hostsig;
2126e103b48fSeric }
2127