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*ce5531bdSeric static char sccsid[] = "@(#)deliver.c	6.57 (Berkeley) 04/01/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;
509c9e68d9Seric 
519c9e68d9Seric 	/* determine actual delivery mode */
529c9e68d9Seric 	if (mode == SM_DEFAULT)
539c9e68d9Seric 	{
549c9e68d9Seric 		extern bool shouldqueue();
559c9e68d9Seric 
569c9e68d9Seric 		mode = e->e_sendmode;
579c9e68d9Seric 		if (mode != SM_VERIFY &&
589c9e68d9Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
599c9e68d9Seric 			mode = SM_QUEUE;
609c9e68d9Seric 	}
619c9e68d9Seric 
629c9e68d9Seric 	if (tTd(13, 1))
639c9e68d9Seric 	{
649c9e68d9Seric 		printf("\nSENDALL: mode %c, e_from ", mode);
659c9e68d9Seric 		printaddr(&e->e_from, FALSE);
669c9e68d9Seric 		printf("sendqueue:\n");
679c9e68d9Seric 		printaddr(e->e_sendqueue, TRUE);
689c9e68d9Seric 	}
699c9e68d9Seric 
709c9e68d9Seric 	/*
719c9e68d9Seric 	**  Do any preprocessing necessary for the mode we are running.
729c9e68d9Seric 	**	Check to make sure the hop count is reasonable.
739c9e68d9Seric 	**	Delete sends to the sender in mailing lists.
749c9e68d9Seric 	*/
759c9e68d9Seric 
769c9e68d9Seric 	CurEnv = e;
779c9e68d9Seric 
789c9e68d9Seric 	if (e->e_hopcount > MaxHopCount)
799c9e68d9Seric 	{
809c9e68d9Seric 		errno = 0;
819c9e68d9Seric 		syserr("554 too many hops %d (%d max): from %s, to %s",
829c9e68d9Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
839c9e68d9Seric 			e->e_sendqueue->q_paddr);
849c9e68d9Seric 		return;
859c9e68d9Seric 	}
869c9e68d9Seric 
879c9e68d9Seric 	if (!MeToo)
889c9e68d9Seric 	{
899c9e68d9Seric 		extern ADDRESS *recipient();
909c9e68d9Seric 
919c9e68d9Seric 		if (tTd(13, 5))
929c9e68d9Seric 		{
939c9e68d9Seric 			printf("sendall: QDONTSEND ");
949c9e68d9Seric 			printaddr(&e->e_from, FALSE);
959c9e68d9Seric 		}
969c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
979c9e68d9Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
989c9e68d9Seric 	}
999c9e68d9Seric 
100*ce5531bdSeric 	/*
101*ce5531bdSeric 	**  Handle alias owners.
102*ce5531bdSeric 	**
103*ce5531bdSeric 	**	We scan up the q_alias chain looking for owners.
104*ce5531bdSeric 	**	We discard owners that are the same as the return path.
105*ce5531bdSeric 	*/
106*ce5531bdSeric 
107*ce5531bdSeric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
108*ce5531bdSeric 	{
109*ce5531bdSeric 		register struct address *a;
110*ce5531bdSeric 
111*ce5531bdSeric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
112*ce5531bdSeric 			continue;
113*ce5531bdSeric 		if (a != NULL)
114*ce5531bdSeric 			q->q_owner = a->q_owner;
115*ce5531bdSeric 
116*ce5531bdSeric 		if (q->q_owner != NULL &&
117*ce5531bdSeric 		    !bitset(QDONTSEND, q->q_flags) &&
118*ce5531bdSeric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
119*ce5531bdSeric 			q->q_owner = NULL;
120*ce5531bdSeric 	}
121*ce5531bdSeric 
122*ce5531bdSeric 	owner = "";
123*ce5531bdSeric 	otherowners = 1;
124*ce5531bdSeric 	while (owner != NULL && otherowners > 0)
125*ce5531bdSeric 	{
126*ce5531bdSeric 		owner = NULL;
127*ce5531bdSeric 		otherowners = 0;
128*ce5531bdSeric 
129*ce5531bdSeric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
130*ce5531bdSeric 		{
131*ce5531bdSeric 			if (bitset(QDONTSEND, q->q_flags))
132*ce5531bdSeric 				continue;
133*ce5531bdSeric 
134*ce5531bdSeric 			if (q->q_owner != NULL)
135*ce5531bdSeric 			{
136*ce5531bdSeric 				if (owner == NULL)
137*ce5531bdSeric 					owner = q->q_owner;
138*ce5531bdSeric 				else if (owner != q->q_owner)
139*ce5531bdSeric 				{
140*ce5531bdSeric 					if (strcmp(owner, q->q_owner) == 0)
141*ce5531bdSeric 					{
142*ce5531bdSeric 						/* make future comparisons cheap */
143*ce5531bdSeric 						q->q_owner = owner;
144*ce5531bdSeric 					}
145*ce5531bdSeric 					else
146*ce5531bdSeric 					{
147*ce5531bdSeric 						otherowners++;
148*ce5531bdSeric 					}
149*ce5531bdSeric 					owner = q->q_owner;
150*ce5531bdSeric 				}
151*ce5531bdSeric 			}
152*ce5531bdSeric 			else
153*ce5531bdSeric 			{
154*ce5531bdSeric 				otherowners++;
155*ce5531bdSeric 			}
156*ce5531bdSeric 		}
157*ce5531bdSeric 
158*ce5531bdSeric 		if (owner != NULL && otherowners > 0)
159*ce5531bdSeric 		{
160*ce5531bdSeric 			extern HDR *copyheader();
161*ce5531bdSeric 			extern ADDRESS *copyqueue();
162*ce5531bdSeric 
163*ce5531bdSeric 			/*
164*ce5531bdSeric 			**  Split this envelope into two.
165*ce5531bdSeric 			*/
166*ce5531bdSeric 
167*ce5531bdSeric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
168*ce5531bdSeric 			*ee = *e;
169*ce5531bdSeric 			ee->e_id = NULL;
170*ce5531bdSeric 			(void) queuename(ee, '\0');
171*ce5531bdSeric 
172*ce5531bdSeric 			if (tTd(13, 1))
173*ce5531bdSeric 				printf("sendall: split %s into %s\n",
174*ce5531bdSeric 					e->e_id, ee->e_id);
175*ce5531bdSeric 
176*ce5531bdSeric 			ee->e_header = copyheader(e->e_header);
177*ce5531bdSeric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
178*ce5531bdSeric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
179*ce5531bdSeric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
180*ce5531bdSeric 			setsender(owner, ee, NULL, TRUE);
181*ce5531bdSeric 			if (tTd(13, 5))
182*ce5531bdSeric 			{
183*ce5531bdSeric 				printf("sendall(split): QDONTSEND ");
184*ce5531bdSeric 				printaddr(&ee->e_from, FALSE);
185*ce5531bdSeric 			}
186*ce5531bdSeric 			ee->e_from.q_flags |= QDONTSEND;
187*ce5531bdSeric 			ee->e_dfp = NULL;
188*ce5531bdSeric 			ee->e_xfp = NULL;
189*ce5531bdSeric 			ee->e_lockfp = NULL;
190*ce5531bdSeric 			ee->e_df = NULL;
191*ce5531bdSeric 			ee->e_errormode = EM_MAIL;
192*ce5531bdSeric 			ee->e_sibling = splitenv;
193*ce5531bdSeric 			splitenv = ee;
194*ce5531bdSeric 
195*ce5531bdSeric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
196*ce5531bdSeric 				if (q->q_owner == owner)
197*ce5531bdSeric 					q->q_flags |= QDONTSEND;
198*ce5531bdSeric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
199*ce5531bdSeric 				if (q->q_owner != owner)
200*ce5531bdSeric 					q->q_flags |= QDONTSEND;
201*ce5531bdSeric 
202*ce5531bdSeric 			if (e->e_df != NULL && mode != SM_VERIFY)
203*ce5531bdSeric 			{
204*ce5531bdSeric 				ee->e_dfp = NULL;
205*ce5531bdSeric 				ee->e_df = newstr(queuename(ee, 'd'));
206*ce5531bdSeric 				if (link(e->e_df, ee->e_df) < 0)
207*ce5531bdSeric 				{
208*ce5531bdSeric 					syserr("sendall: link(%s, %s)",
209*ce5531bdSeric 						e->e_df, ee->e_df);
210*ce5531bdSeric 				}
211*ce5531bdSeric 			}
212*ce5531bdSeric 
213*ce5531bdSeric 			if (mode != SM_VERIFY)
214*ce5531bdSeric 				openxscript(ee);
215*ce5531bdSeric #ifdef LOG
216*ce5531bdSeric 			if (LogLevel > 4)
217*ce5531bdSeric 				syslog(LOG_INFO, "%s: clone %s",
218*ce5531bdSeric 					ee->e_id, e->e_id);
219*ce5531bdSeric #endif
220*ce5531bdSeric 		}
221*ce5531bdSeric 	}
222*ce5531bdSeric 
223*ce5531bdSeric 	if (owner != NULL)
224*ce5531bdSeric 	{
225*ce5531bdSeric 		setsender(owner, e, NULL, TRUE);
226*ce5531bdSeric 		if (tTd(13, 5))
227*ce5531bdSeric 		{
228*ce5531bdSeric 			printf("sendall(owner): QDONTSEND ");
229*ce5531bdSeric 			printaddr(&e->e_from, FALSE);
230*ce5531bdSeric 		}
231*ce5531bdSeric 		e->e_from.q_flags |= QDONTSEND;
232*ce5531bdSeric 		e->e_errormode = EM_MAIL;
233*ce5531bdSeric 	}
234*ce5531bdSeric 
235c1ac89b1Seric # ifdef QUEUE
236c1ac89b1Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
237c1ac89b1Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
238c1ac89b1Seric 	    !bitset(EF_INQUEUE, e->e_flags))
239c1ac89b1Seric 	{
240c1ac89b1Seric 		/* be sure everything is instantiated in the queue */
241c1ac89b1Seric 		queueup(e, TRUE, mode == SM_QUEUE);
242*ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
243*ce5531bdSeric 			queueup(ee, TRUE, mode == SM_QUEUE);
244c1ac89b1Seric 	}
245c1ac89b1Seric #endif /* QUEUE */
246c1ac89b1Seric 
247*ce5531bdSeric 	if (splitenv != NULL)
248*ce5531bdSeric 	{
249*ce5531bdSeric 		if (tTd(13, 1))
250*ce5531bdSeric 		{
251*ce5531bdSeric 			printf("\nsendall: Split queue; remaining queue:\n");
252*ce5531bdSeric 			printaddr(e->e_sendqueue, TRUE);
253*ce5531bdSeric 		}
254*ce5531bdSeric 
255*ce5531bdSeric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
256*ce5531bdSeric 		{
257*ce5531bdSeric 			CurEnv = ee;
258*ce5531bdSeric 			sendenvelope(ee, mode);
259*ce5531bdSeric 		}
260*ce5531bdSeric 
261*ce5531bdSeric 		CurEnv = e;
262*ce5531bdSeric 	}
263*ce5531bdSeric 	sendenvelope(e, mode);
264*ce5531bdSeric 
265*ce5531bdSeric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
266*ce5531bdSeric 		dropenvelope(splitenv);
267*ce5531bdSeric }
268*ce5531bdSeric 
269*ce5531bdSeric sendenvelope(e, mode)
270*ce5531bdSeric 	register ENVELOPE *e;
271*ce5531bdSeric 	char mode;
272*ce5531bdSeric {
273*ce5531bdSeric 	bool oldverbose;
274*ce5531bdSeric 	int pid;
275*ce5531bdSeric 	register ADDRESS *q;
276*ce5531bdSeric #ifdef LOCKF
277*ce5531bdSeric 	struct flock lfd;
278*ce5531bdSeric #endif
279*ce5531bdSeric 
280*ce5531bdSeric 	oldverbose = Verbose;
281*ce5531bdSeric 	e->e_statmsg = NULL;
282c1ac89b1Seric 	switch (mode)
283c1ac89b1Seric 	{
284c1ac89b1Seric 	  case SM_VERIFY:
285c1ac89b1Seric 		Verbose = TRUE;
286c1ac89b1Seric 		break;
287c1ac89b1Seric 
288c1ac89b1Seric 	  case SM_QUEUE:
289c1ac89b1Seric   queueonly:
290c1ac89b1Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
291c1ac89b1Seric 		return;
292c1ac89b1Seric 
293c1ac89b1Seric 	  case SM_FORK:
294c1ac89b1Seric 		if (e->e_xfp != NULL)
295c1ac89b1Seric 			(void) fflush(e->e_xfp);
296c1ac89b1Seric 
297c1ac89b1Seric # ifdef LOCKF
298c1ac89b1Seric 		/*
299c1ac89b1Seric 		**  Since lockf has the interesting semantic that the
300c1ac89b1Seric 		**  lock is lost when we fork, we have to risk losing
301c1ac89b1Seric 		**  the lock here by closing before the fork, and then
302c1ac89b1Seric 		**  trying to get it back in the child.
303c1ac89b1Seric 		*/
304c1ac89b1Seric 
305c1ac89b1Seric 		if (e->e_lockfp != NULL)
306c1ac89b1Seric 		{
307c1ac89b1Seric 			(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
308c1ac89b1Seric 			e->e_lockfp = NULL;
309c1ac89b1Seric 		}
310c1ac89b1Seric # endif /* LOCKF */
311c1ac89b1Seric 
312c1ac89b1Seric 		pid = fork();
313c1ac89b1Seric 		if (pid < 0)
314c1ac89b1Seric 		{
315c1ac89b1Seric 			goto queueonly;
316c1ac89b1Seric 		}
317c1ac89b1Seric 		else if (pid > 0)
318c1ac89b1Seric 		{
319c1ac89b1Seric 			/* be sure we leave the temp files to our child */
320c1ac89b1Seric 			e->e_id = e->e_df = NULL;
321c1ac89b1Seric # ifndef LOCKF
322c1ac89b1Seric 			if (e->e_lockfp != NULL)
323c1ac89b1Seric 			{
324c1ac89b1Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
325c1ac89b1Seric 				e->e_lockfp = NULL;
326c1ac89b1Seric 			}
327c1ac89b1Seric # endif
328c1ac89b1Seric 
329c1ac89b1Seric 			/* close any random open files in the envelope */
330c1ac89b1Seric 			if (e->e_dfp != NULL)
331c1ac89b1Seric 			{
332c1ac89b1Seric 				(void) xfclose(e->e_dfp, "sendenvelope", "dfp");
333c1ac89b1Seric 				e->e_dfp = NULL;
334c1ac89b1Seric 			}
335c1ac89b1Seric 			if (e->e_xfp != NULL)
336c1ac89b1Seric 			{
337c1ac89b1Seric 				(void) xfclose(e->e_xfp, "sendenvelope", "xfp");
338c1ac89b1Seric 				e->e_xfp = NULL;
339c1ac89b1Seric 			}
340c1ac89b1Seric 			return;
341c1ac89b1Seric 		}
342c1ac89b1Seric 
343c1ac89b1Seric 		/* double fork to avoid zombies */
344c1ac89b1Seric 		if (fork() > 0)
345c1ac89b1Seric 			exit(EX_OK);
346c1ac89b1Seric 
347c1ac89b1Seric 		/* be sure we are immune from the terminal */
348c1ac89b1Seric 		disconnect(FALSE, e);
349c1ac89b1Seric 
350c1ac89b1Seric # ifdef LOCKF
351c1ac89b1Seric 		/*
352c1ac89b1Seric 		**  Now try to get our lock back.
353c1ac89b1Seric 		*/
354c1ac89b1Seric 
355c1ac89b1Seric 		lfd.l_type = F_WRLCK;
356c1ac89b1Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
357c1ac89b1Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
358c1ac89b1Seric 		if (e->e_lockfp == NULL ||
359c1ac89b1Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
360c1ac89b1Seric 		{
361c1ac89b1Seric 			/* oops....  lost it */
362c1ac89b1Seric 			if (tTd(13, 1))
363c1ac89b1Seric 				printf("sendenvelope: %s lost lock: lockfp=%x, %s\n",
364c1ac89b1Seric 					e->e_id, e->e_lockfp, errstring(errno));
365c1ac89b1Seric 
366c1ac89b1Seric # ifdef LOG
367c1ac89b1Seric 			if (LogLevel > 29)
368c1ac89b1Seric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
369c1ac89b1Seric 					e->e_id);
370c1ac89b1Seric # endif /* LOG */
371c1ac89b1Seric 			exit(EX_OK);
372c1ac89b1Seric 		}
373c1ac89b1Seric # endif /* LOCKF */
374c1ac89b1Seric 
375c1ac89b1Seric 		/*
376c1ac89b1Seric 		**  Close any cached connections.
377c1ac89b1Seric 		**
378c1ac89b1Seric 		**	We don't send the QUIT protocol because the parent
379c1ac89b1Seric 		**	still knows about the connection.
380c1ac89b1Seric 		**
381c1ac89b1Seric 		**	This should only happen when delivering an error
382c1ac89b1Seric 		**	message.
383c1ac89b1Seric 		*/
384c1ac89b1Seric 
385c1ac89b1Seric 		mci_flush(FALSE, NULL);
386c1ac89b1Seric 
387c1ac89b1Seric 		break;
388c1ac89b1Seric 	}
389c1ac89b1Seric 
390c1ac89b1Seric 	/*
3919c9e68d9Seric 	**  Run through the list and send everything.
3929c9e68d9Seric 	*/
3939c9e68d9Seric 
3949c9e68d9Seric 	e->e_nsent = 0;
3959c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
3969c9e68d9Seric 	{
3979c9e68d9Seric 		if (mode == SM_VERIFY)
3989c9e68d9Seric 		{
3999c9e68d9Seric 			e->e_to = q->q_paddr;
4009c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4019c9e68d9Seric 				message("deliverable");
4029c9e68d9Seric 		}
4039c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4049c9e68d9Seric 		{
4059c9e68d9Seric # ifdef QUEUE
4069c9e68d9Seric 			/*
4079c9e68d9Seric 			**  Checkpoint the send list every few addresses
4089c9e68d9Seric 			*/
4099c9e68d9Seric 
4109c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4119c9e68d9Seric 			{
4129c9e68d9Seric 				queueup(e, TRUE, FALSE);
4139c9e68d9Seric 				e->e_nsent = 0;
4149c9e68d9Seric 			}
4159c9e68d9Seric # endif /* QUEUE */
4169c9e68d9Seric 			(void) deliver(e, q);
4179c9e68d9Seric 		}
4189c9e68d9Seric 	}
4199c9e68d9Seric 	Verbose = oldverbose;
4209c9e68d9Seric 
4219c9e68d9Seric 	/*
4229c9e68d9Seric 	**  Now run through and check for errors.
4239c9e68d9Seric 	*/
4249c9e68d9Seric 
4259c9e68d9Seric 	if (mode == SM_VERIFY)
4269c9e68d9Seric 	{
4279c9e68d9Seric 		return;
4289c9e68d9Seric 	}
4299c9e68d9Seric 
4309c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4319c9e68d9Seric 	{
4329c9e68d9Seric 		if (tTd(13, 3))
4339c9e68d9Seric 		{
4349c9e68d9Seric 			printf("Checking ");
4359c9e68d9Seric 			printaddr(q, FALSE);
4369c9e68d9Seric 		}
4379c9e68d9Seric 
4389c9e68d9Seric 		/* only send errors if the message failed */
439*ce5531bdSeric 		if (!bitset(QBADADDR, q->q_flags) ||
440*ce5531bdSeric 		    bitset(QDONTSEND, q->q_flags))
4419c9e68d9Seric 			continue;
4429c9e68d9Seric 
4439c9e68d9Seric 		e->e_flags |= EF_FATALERRS;
4449c9e68d9Seric 
4459c9e68d9Seric 		if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0)
4469c9e68d9Seric 			(void) sendtolist(e->e_from.q_paddr, NULL,
4479c9e68d9Seric 					  &e->e_errorqueue, e);
4489c9e68d9Seric 	}
4499c9e68d9Seric 
4509c9e68d9Seric 	if (mode == SM_FORK)
4519c9e68d9Seric 		finis();
4529c9e68d9Seric }
4539c9e68d9Seric /*
4549c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4559c9e68d9Seric **
4569c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4579c9e68d9Seric **	two processes on the same stack!!!
4589c9e68d9Seric **
4599c9e68d9Seric **	Parameters:
4609c9e68d9Seric **		none.
4619c9e68d9Seric **
4629c9e68d9Seric **	Returns:
4639c9e68d9Seric **		From a macro???  You've got to be kidding!
4649c9e68d9Seric **
4659c9e68d9Seric **	Side Effects:
4669c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4679c9e68d9Seric **			pid of child in parent, zero in child.
4689c9e68d9Seric **			-1 on unrecoverable error.
4699c9e68d9Seric **
4709c9e68d9Seric **	Notes:
4719c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4729c9e68d9Seric **		vfork for you.....
4739c9e68d9Seric */
4749c9e68d9Seric 
4759c9e68d9Seric # define NFORKTRIES	5
4769c9e68d9Seric 
4779c9e68d9Seric # ifndef FORK
4789c9e68d9Seric # define FORK	fork
4799c9e68d9Seric # endif
4809c9e68d9Seric 
4819c9e68d9Seric # define DOFORK(fORKfN) \
4829c9e68d9Seric {\
4839c9e68d9Seric 	register int i;\
4849c9e68d9Seric \
4859c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4869c9e68d9Seric 	{\
4879c9e68d9Seric 		pid = fORKfN();\
4889c9e68d9Seric 		if (pid >= 0)\
4899c9e68d9Seric 			break;\
4909c9e68d9Seric 		if (i > 0)\
4919c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
4929c9e68d9Seric 	}\
4939c9e68d9Seric }
4949c9e68d9Seric /*
4959c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
4969c9e68d9Seric **
4979c9e68d9Seric **	Parameters:
4989c9e68d9Seric **		none.
4999c9e68d9Seric **
5009c9e68d9Seric **	Returns:
5019c9e68d9Seric **		pid of child in parent.
5029c9e68d9Seric **		zero in child.
5039c9e68d9Seric **		-1 on error.
5049c9e68d9Seric **
5059c9e68d9Seric **	Side Effects:
5069c9e68d9Seric **		returns twice, once in parent and once in child.
5079c9e68d9Seric */
5089c9e68d9Seric 
5099c9e68d9Seric dofork()
5109c9e68d9Seric {
5119c9e68d9Seric 	register int pid;
5129c9e68d9Seric 
5139c9e68d9Seric 	DOFORK(fork);
5149c9e68d9Seric 	return (pid);
5159c9e68d9Seric }
5169c9e68d9Seric /*
51713bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
51813bbc08cSeric **
51913bbc08cSeric **	This routine delivers to everyone on the same host as the
52013bbc08cSeric **	user on the head of the list.  It is clever about mailers
52113bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
52213bbc08cSeric **	that it will deliver to all these addresses however -- so
52313bbc08cSeric **	deliver should be called once for each address on the
52413bbc08cSeric **	list.
52525a99e2eSeric **
52625a99e2eSeric **	Parameters:
527588cad61Seric **		e -- the envelope to deliver.
528c77d1c25Seric **		firstto -- head of the address list to deliver to.
52925a99e2eSeric **
53025a99e2eSeric **	Returns:
53125a99e2eSeric **		zero -- successfully delivered.
53225a99e2eSeric **		else -- some failure, see ExitStat for more info.
53325a99e2eSeric **
53425a99e2eSeric **	Side Effects:
53525a99e2eSeric **		The standard input is passed off to someone.
53625a99e2eSeric */
53725a99e2eSeric 
538588cad61Seric deliver(e, firstto)
539588cad61Seric 	register ENVELOPE *e;
540c77d1c25Seric 	ADDRESS *firstto;
54125a99e2eSeric {
54278442df3Seric 	char *host;			/* host being sent to */
54378442df3Seric 	char *user;			/* user being sent to */
54425a99e2eSeric 	char **pvp;
5455dfc646bSeric 	register char **mvp;
54625a99e2eSeric 	register char *p;
547588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5486259796dSeric 	ADDRESS *ctladdr;
549b31e7f2bSeric 	register MCI *mci;
550c77d1c25Seric 	register ADDRESS *to = firstto;
551c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
552772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
553911693bfSbostic 	int rcode;			/* response code */
554e103b48fSeric 	char *firstsig;			/* signature of firstto */
5559c9e68d9Seric 	int pid;
5569c9e68d9Seric 	char *curhost;
5579c9e68d9Seric 	int mpvect[2];
5589c9e68d9Seric 	int rpvect[2];
559ee6bf8dfSeric 	char *pv[MAXPV+1];
560579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
561ee6bf8dfSeric 	char buf[MAXNAME];
562c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
563fabb3bd4Seric 	extern int checkcompat();
564ee6bf8dfSeric 	extern ADDRESS *getctladdr();
565ee6bf8dfSeric 	extern char *remotename();
566e103b48fSeric 	extern char *hostsignature();
5679c9e68d9Seric 	extern FILE *fdopen();
56825a99e2eSeric 
56935490626Seric 	errno = 0;
570ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5715dfc646bSeric 		return (0);
57225a99e2eSeric 
573134746fbSeric #ifdef NAMED_BIND
574912a731aSbostic 	/* unless interactive, try twice, over a minute */
575912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
576912a731aSbostic 		_res.retrans = 30;
577912a731aSbostic 		_res.retry = 2;
578912a731aSbostic 	}
579d4bd8f0eSbostic #endif
580912a731aSbostic 
58151552439Seric 	m = to->q_mailer;
58251552439Seric 	host = to->q_host;
583c9be6216Seric 	CurEnv = e;			/* just in case */
58451552439Seric 
5856ef48975Seric 	if (tTd(10, 1))
5865dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
58751552439Seric 			m->m_mno, host, to->q_user);
588f3dbc832Seric 
589f3dbc832Seric 	/*
590f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
591f3dbc832Seric 	**  connections now, just mark these addresses and return.
592f3dbc832Seric 	**	This is useful if we want to batch connections to
593f3dbc832Seric 	**	reduce load.  This will cause the messages to be
594f3dbc832Seric 	**	queued up, and a daemon will come along to send the
595f3dbc832Seric 	**	messages later.
596f3dbc832Seric 	**		This should be on a per-mailer basis.
597f3dbc832Seric 	*/
598f3dbc832Seric 
59919c47125Seric 	if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) &&
60019c47125Seric 	    bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
601f3dbc832Seric 	{
602f3dbc832Seric 		for (; to != NULL; to = to->q_next)
603f4560e80Seric 		{
604ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
605c6e18ac5Seric 			    to->q_mailer != m)
606f4560e80Seric 				continue;
607f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
608588cad61Seric 			e->e_to = to->q_paddr;
60908b25121Seric 			message("queued");
6102f624c86Seric 			if (LogLevel > 8)
61181161401Seric 				logdelivery(m, NULL, "queued", e);
612f4560e80Seric 		}
613588cad61Seric 		e->e_to = NULL;
614f3dbc832Seric 		return (0);
615f3dbc832Seric 	}
616f3dbc832Seric 
61725a99e2eSeric 	/*
6185dfc646bSeric 	**  Do initial argv setup.
6195dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6205dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6215dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6225dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6235dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6243bea8136Seric 	**		The from address rewrite is expected to make
6253bea8136Seric 	**		the address relative to the other end.
6265dfc646bSeric 	*/
6275dfc646bSeric 
62878442df3Seric 	/* rewrite from address, using rewriting rules */
629ee4b0922Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, TRUE, FALSE,
6309c69ca01Seric 					   TRUE, FALSE, e));
631ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
632588cad61Seric 	define('h', host, e);			/* to host */
6335dfc646bSeric 	Errors = 0;
6345dfc646bSeric 	pvp = pv;
6355dfc646bSeric 	*pvp++ = m->m_argv[0];
6365dfc646bSeric 
6375dfc646bSeric 	/* insert -f or -r flag as appropriate */
63857fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6395dfc646bSeric 	{
64057fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6415dfc646bSeric 			*pvp++ = "-f";
6425dfc646bSeric 		else
6435dfc646bSeric 			*pvp++ = "-r";
644c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6455dfc646bSeric 	}
6465dfc646bSeric 
6475dfc646bSeric 	/*
6485dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6495dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6505dfc646bSeric 	**  be one of these, and there are only a few more slots
6515dfc646bSeric 	**  in the pv after it.
6525dfc646bSeric 	*/
6535dfc646bSeric 
6545dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6555dfc646bSeric 	{
6562bc47524Seric 		/* can't use strchr here because of sign extension problems */
6572bc47524Seric 		while (*p != '\0')
6582bc47524Seric 		{
6592bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6602bc47524Seric 			{
6612bc47524Seric 				if (*p == 'u')
6625dfc646bSeric 					break;
6632bc47524Seric 			}
6642bc47524Seric 		}
6652bc47524Seric 
6662bc47524Seric 		if (*p != '\0')
6675dfc646bSeric 			break;
6685dfc646bSeric 
6695dfc646bSeric 		/* this entry is safe -- go ahead and process it */
670588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6715dfc646bSeric 		*pvp++ = newstr(buf);
6725dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6735dfc646bSeric 		{
67408b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6755dfc646bSeric 			return (-1);
6765dfc646bSeric 		}
6775dfc646bSeric 	}
678c579ef51Seric 
67933db8731Seric 	/*
68033db8731Seric 	**  If we have no substitution for the user name in the argument
68133db8731Seric 	**  list, we know that we must supply the names otherwise -- and
68233db8731Seric 	**  SMTP is the answer!!
68333db8731Seric 	*/
68433db8731Seric 
6855dfc646bSeric 	if (*mvp == NULL)
686c579ef51Seric 	{
687c579ef51Seric 		/* running SMTP */
6882c7e1b8dSeric # ifdef SMTP
689c579ef51Seric 		clever = TRUE;
690c579ef51Seric 		*pvp = NULL;
6916c2c3107Seric # else /* SMTP */
69233db8731Seric 		/* oops!  we don't implement SMTP */
69308b25121Seric 		syserr("554 SMTP style mailer");
6942c7e1b8dSeric 		return (EX_SOFTWARE);
6956c2c3107Seric # endif /* SMTP */
696c579ef51Seric 	}
6975dfc646bSeric 
6985dfc646bSeric 	/*
6995dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
7005dfc646bSeric 	**  run through our address list and append all the addresses
7015dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7025dfc646bSeric 	**  always send another copy later.
7035dfc646bSeric 	*/
7045dfc646bSeric 
7055dfc646bSeric 	tobuf[0] = '\0';
706588cad61Seric 	e->e_to = tobuf;
7076259796dSeric 	ctladdr = NULL;
708e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7095dfc646bSeric 	for (; to != NULL; to = to->q_next)
7105dfc646bSeric 	{
7115dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
71257fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7135dfc646bSeric 			break;
7145dfc646bSeric 
7155dfc646bSeric 		/* if already sent or not for this host, don't send */
716ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
717e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
718e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7195dfc646bSeric 			continue;
7206259796dSeric 
7214b22ea87Seric 		/* avoid overflowing tobuf */
722aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7234b22ea87Seric 			break;
7244b22ea87Seric 
7256ef48975Seric 		if (tTd(10, 1))
726772e6e50Seric 		{
727772e6e50Seric 			printf("\nsend to ");
728772e6e50Seric 			printaddr(to, FALSE);
729772e6e50Seric 		}
730772e6e50Seric 
7316259796dSeric 		/* compute effective uid/gid when sending */
7327da1035fSeric 		if (to->q_mailer == ProgMailer)
7336259796dSeric 			ctladdr = getctladdr(to);
7346259796dSeric 
7355dfc646bSeric 		user = to->q_user;
736588cad61Seric 		e->e_to = to->q_paddr;
73775f1ade9Seric 		if (tTd(10, 5))
73875f1ade9Seric 		{
73975f1ade9Seric 			printf("deliver: QDONTSEND ");
74075f1ade9Seric 			printaddr(to, FALSE);
74175f1ade9Seric 		}
742ee4b0922Seric 		to->q_flags |= QDONTSEND;
7435dfc646bSeric 
7445dfc646bSeric 		/*
7455dfc646bSeric 		**  Check to see that these people are allowed to
7465dfc646bSeric 		**  talk to each other.
7472a6e0786Seric 		*/
7482a6e0786Seric 
74969582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
75069582d2fSeric 		{
75169582d2fSeric 			NoReturn = TRUE;
75208b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
75381161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
75469582d2fSeric 			continue;
75569582d2fSeric 		}
756fabb3bd4Seric 		rcode = checkcompat(to, e);
7571793c9c5Seric 		if (rcode != EX_OK)
7585dfc646bSeric 		{
75981161401Seric 			giveresponse(rcode, m, NULL, e);
7605dfc646bSeric 			continue;
7615dfc646bSeric 		}
7622a6e0786Seric 
7632a6e0786Seric 		/*
7649ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7659ec9501bSeric 		**	about them.
76625a99e2eSeric 		*/
76725a99e2eSeric 
76857fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
76925a99e2eSeric 		{
7701d8f1806Seric 			stripquotes(user);
7711d8f1806Seric 			stripquotes(host);
77225a99e2eSeric 		}
77325a99e2eSeric 
774cdb828c5Seric 		/* hack attack -- delivermail compatibility */
775cdb828c5Seric 		if (m == ProgMailer && *user == '|')
776cdb828c5Seric 			user++;
777cdb828c5Seric 
77825a99e2eSeric 		/*
7793efaed6eSeric 		**  If an error message has already been given, don't
7803efaed6eSeric 		**	bother to send to this address.
7813efaed6eSeric 		**
7823efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7833efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
7843efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
7853efaed6eSeric 		*/
7863efaed6eSeric 
7876cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7883efaed6eSeric 			continue;
7893efaed6eSeric 
790f2fec898Seric 		/* save statistics.... */
791588cad61Seric 		markstats(e, to);
792f2fec898Seric 
7933efaed6eSeric 		/*
79425a99e2eSeric 		**  See if this user name is "special".
79525a99e2eSeric 		**	If the user name has a slash in it, assume that this
79651552439Seric 		**	is a file -- send it off without further ado.  Note
79751552439Seric 		**	that this type of addresses is not processed along
79851552439Seric 		**	with the others, so we fudge on the To person.
79925a99e2eSeric 		*/
80025a99e2eSeric 
8012c017f8dSeric 		if (m == FileMailer)
80225a99e2eSeric 		{
803b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
80481161401Seric 			giveresponse(rcode, m, NULL, e);
805dde5acadSeric 			if (rcode == EX_OK)
806dde5acadSeric 				to->q_flags |= QSENT;
8075dfc646bSeric 			continue;
80825a99e2eSeric 		}
80925a99e2eSeric 
81013bbc08cSeric 		/*
81113bbc08cSeric 		**  Address is verified -- add this user to mailer
81213bbc08cSeric 		**  argv, and add it to the print list of recipients.
81313bbc08cSeric 		*/
81413bbc08cSeric 
815508daeccSeric 		/* link together the chain of recipients */
816508daeccSeric 		to->q_tchain = tochain;
817508daeccSeric 		tochain = to;
818508daeccSeric 
8195dfc646bSeric 		/* create list of users for error messages */
820db8841e9Seric 		(void) strcat(tobuf, ",");
821db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
822588cad61Seric 		define('u', user, e);		/* to user */
823588cad61Seric 		define('z', to->q_home, e);	/* user's home */
8245dfc646bSeric 
825c579ef51Seric 		/*
826508daeccSeric 		**  Expand out this user into argument list.
827c579ef51Seric 		*/
828c579ef51Seric 
829508daeccSeric 		if (!clever)
830c579ef51Seric 		{
831588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8325dfc646bSeric 			*pvp++ = newstr(buf);
8335dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8345dfc646bSeric 			{
8355dfc646bSeric 				/* allow some space for trailing parms */
8365dfc646bSeric 				break;
8375dfc646bSeric 			}
8385dfc646bSeric 		}
839c579ef51Seric 	}
8405dfc646bSeric 
841145b49b1Seric 	/* see if any addresses still exist */
842145b49b1Seric 	if (tobuf[0] == '\0')
843c579ef51Seric 	{
844588cad61Seric 		define('g', (char *) NULL, e);
845145b49b1Seric 		return (0);
846c579ef51Seric 	}
847145b49b1Seric 
8485dfc646bSeric 	/* print out messages as full list */
84963780dbdSeric 	e->e_to = tobuf + 1;
8505dfc646bSeric 
8515dfc646bSeric 	/*
8525dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8535dfc646bSeric 	*/
8545dfc646bSeric 
855c579ef51Seric 	while (!clever && *++mvp != NULL)
8565dfc646bSeric 	{
857588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8585dfc646bSeric 		*pvp++ = newstr(buf);
8595dfc646bSeric 		if (pvp >= &pv[MAXPV])
86008b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8615dfc646bSeric 	}
8625dfc646bSeric 	*pvp++ = NULL;
8635dfc646bSeric 
86425a99e2eSeric 	/*
86525a99e2eSeric 	**  Call the mailer.
8666328bdf7Seric 	**	The argument vector gets built, pipes
86725a99e2eSeric 	**	are created as necessary, and we fork & exec as
8686328bdf7Seric 	**	appropriate.
869c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
87025a99e2eSeric 	*/
87125a99e2eSeric 
8727ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
87386b26461Seric 		ctladdr = &e->e_from;
874134746fbSeric #ifdef NAMED_BIND
8752bcc6d2dSeric 	if (ConfigLevel < 2)
876912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
877134746fbSeric #endif
8789c9e68d9Seric 
8799c9e68d9Seric 	if (tTd(11, 1))
880134746fbSeric 	{
8819c9e68d9Seric 		printf("openmailer:");
8829c9e68d9Seric 		printav(pv);
8839c9e68d9Seric 	}
8849c9e68d9Seric 	errno = 0;
8859c9e68d9Seric 
8869c9e68d9Seric 	CurHostName = m->m_mailer;
8879c9e68d9Seric 
8889c9e68d9Seric 	/*
8899c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
8909c9e68d9Seric 	**  connection.
8919c9e68d9Seric 	**	In this case we don't actually fork.  We must be
8929c9e68d9Seric 	**	running SMTP for this to work.  We will return a
8939c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
8949c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
8959c9e68d9Seric 	*/
8969c9e68d9Seric 
8979c9e68d9Seric 	curhost = NULL;
8989c9e68d9Seric 
8999c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
9009c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
9019c9e68d9Seric 	{
9029c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9039c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9049c9e68d9Seric 		mci->mci_in = stdin;
9059c9e68d9Seric 		mci->mci_out = stdout;
9069c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9079c9e68d9Seric 		mci->mci_mailer = m;
9089c9e68d9Seric 	}
9099c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9109c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9119c9e68d9Seric 	{
9129c9e68d9Seric #ifdef DAEMON
9139c9e68d9Seric 		register int i;
9149c9e68d9Seric 		register u_short port;
9159c9e68d9Seric 		extern MCI *mci_get();
9169c9e68d9Seric 		extern char *hostsignature();
9179c9e68d9Seric 
9189c9e68d9Seric 		CurHostName = pv[1];
9199c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9209c9e68d9Seric 
9219c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9229c9e68d9Seric 		{
9239c9e68d9Seric 			syserr("null signature");
924845e533cSeric 			rcode = EX_OSERR;
925b31e7f2bSeric 			goto give_up;
926b31e7f2bSeric 		}
9279c9e68d9Seric 
9289c9e68d9Seric 		if (!clever)
9299c9e68d9Seric 		{
9309c9e68d9Seric 			syserr("554 non-clever IPC");
9319c9e68d9Seric 			rcode = EX_OSERR;
9329c9e68d9Seric 			goto give_up;
9339c9e68d9Seric 		}
9349c9e68d9Seric 		if (pv[2] != NULL)
9359c9e68d9Seric 			port = atoi(pv[2]);
9369c9e68d9Seric 		else
9379c9e68d9Seric 			port = 0;
9389c9e68d9Seric tryhost:
9399c9e68d9Seric 		mci = NULL;
9409c9e68d9Seric 		while (*curhost != '\0')
9419c9e68d9Seric 		{
9429c9e68d9Seric 			register char *p;
9439c9e68d9Seric 			static char hostbuf[MAXNAME];
9449c9e68d9Seric 
9459c9e68d9Seric 			mci = NULL;
9469c9e68d9Seric 
9479c9e68d9Seric 			/* pull the next host from the signature */
9489c9e68d9Seric 			p = strchr(curhost, ':');
9499c9e68d9Seric 			if (p == NULL)
9509c9e68d9Seric 				p = &curhost[strlen(curhost)];
9519c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9529c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9539c9e68d9Seric 			if (*p != '\0')
9549c9e68d9Seric 				p++;
9559c9e68d9Seric 			curhost = p;
9569c9e68d9Seric 
9579c9e68d9Seric 			/* see if we already know that this host is fried */
9589c9e68d9Seric 			CurHostName = hostbuf;
9599c9e68d9Seric 			mci = mci_get(hostbuf, m);
9609c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9619c9e68d9Seric 			{
9629c9e68d9Seric 				if (tTd(11, 1))
9639c9e68d9Seric 				{
9649c9e68d9Seric 					printf("openmailer: ");
9659c9e68d9Seric 					mci_dump(mci);
9669c9e68d9Seric 				}
9679c9e68d9Seric 				CurHostName = mci->mci_host;
9689c9e68d9Seric 				break;
9699c9e68d9Seric 			}
9709c9e68d9Seric 			mci->mci_mailer = m;
9719c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9729c9e68d9Seric 				continue;
9739c9e68d9Seric 
9749c9e68d9Seric 			/* try the connection */
9759c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9769c9e68d9Seric 			message("Connecting to %s (%s)...",
9779c9e68d9Seric 				hostbuf, m->m_name);
9789c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
9799c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
9809c9e68d9Seric 			mci->mci_exitstat = i;
9819c9e68d9Seric 			mci->mci_errno = errno;
9829c9e68d9Seric 			if (i == EX_OK)
9839c9e68d9Seric 			{
9849c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
9859c9e68d9Seric 				mci_cache(mci);
9869c9e68d9Seric 				break;
9879c9e68d9Seric 			}
9889c9e68d9Seric 			else if (tTd(11, 1))
9899c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
9909c9e68d9Seric 					i, errno);
9919c9e68d9Seric 
9929c9e68d9Seric 
9939c9e68d9Seric 			/* enter status of this host */
9949c9e68d9Seric 			setstat(i);
9959c9e68d9Seric 		}
9969c9e68d9Seric 		mci->mci_pid = 0;
9979c9e68d9Seric #else /* no DAEMON */
9989c9e68d9Seric 		syserr("554 openmailer: no IPC");
9999c9e68d9Seric 		if (tTd(11, 1))
10009c9e68d9Seric 			printf("openmailer: NULL\n");
10019c9e68d9Seric 		return NULL;
10029c9e68d9Seric #endif /* DAEMON */
10039c9e68d9Seric 	}
10049c9e68d9Seric 	else
10059c9e68d9Seric 	{
10066fe8c3bcSeric 		int i;
10076fe8c3bcSeric 		struct stat stbuf;
10086fe8c3bcSeric 
10096fe8c3bcSeric 		/* make absolutely certain 0, 1, and 2 are in use */
10106fe8c3bcSeric 		for (i = 0; i < 3; i++)
10116fe8c3bcSeric 		{
10126fe8c3bcSeric 			if (fstat(i, &stbuf) < 0)
10136fe8c3bcSeric 			{
10146fe8c3bcSeric 				/* oops.... */
10156fe8c3bcSeric 				syserr("openmailer: fd %d not open", i);
10166fe8c3bcSeric 				(void) open("/dev/null", O_RDONLY, 0666);
10176fe8c3bcSeric 			}
10186fe8c3bcSeric 		}
10196fe8c3bcSeric 
10209c9e68d9Seric 		/* create a pipe to shove the mail through */
10219c9e68d9Seric 		if (pipe(mpvect) < 0)
10229c9e68d9Seric 		{
10239c9e68d9Seric 			syserr("openmailer: pipe (to mailer)");
10249c9e68d9Seric 			if (tTd(11, 1))
10259c9e68d9Seric 				printf("openmailer: NULL\n");
10269c9e68d9Seric 			rcode = EX_OSERR;
10279c9e68d9Seric 			goto give_up;
10289c9e68d9Seric 		}
10299c9e68d9Seric 
10309c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10319c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10329c9e68d9Seric 		{
10339c9e68d9Seric 			syserr("openmailer: pipe (from mailer)");
10349c9e68d9Seric 			(void) close(mpvect[0]);
10359c9e68d9Seric 			(void) close(mpvect[1]);
10369c9e68d9Seric 			if (tTd(11, 1))
10379c9e68d9Seric 				printf("openmailer: NULL\n");
10389c9e68d9Seric 			rcode = EX_OSERR;
10399c9e68d9Seric 			goto give_up;
10409c9e68d9Seric 		}
10419c9e68d9Seric 
10429c9e68d9Seric 		/*
10439c9e68d9Seric 		**  Actually fork the mailer process.
10449c9e68d9Seric 		**	DOFORK is clever about retrying.
10459c9e68d9Seric 		**
10469c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10479c9e68d9Seric 		**	around so that endmail will get it.
10489c9e68d9Seric 		*/
10499c9e68d9Seric 
10509c9e68d9Seric 		if (e->e_xfp != NULL)
10519c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10529c9e68d9Seric 		(void) fflush(stdout);
10539c9e68d9Seric # ifdef SIGCHLD
10549c9e68d9Seric 		(void) signal(SIGCHLD, SIG_DFL);
10559c9e68d9Seric # endif /* SIGCHLD */
10569c9e68d9Seric 		DOFORK(FORK);
10579c9e68d9Seric 		/* pid is set by DOFORK */
10589c9e68d9Seric 		if (pid < 0)
10599c9e68d9Seric 		{
10609c9e68d9Seric 			/* failure */
10619c9e68d9Seric 			syserr("openmailer: cannot fork");
10629c9e68d9Seric 			(void) close(mpvect[0]);
10639c9e68d9Seric 			(void) close(mpvect[1]);
10649c9e68d9Seric 			if (clever)
10659c9e68d9Seric 			{
10669c9e68d9Seric 				(void) close(rpvect[0]);
10679c9e68d9Seric 				(void) close(rpvect[1]);
10689c9e68d9Seric 			}
10699c9e68d9Seric 			if (tTd(11, 1))
10709c9e68d9Seric 				printf("openmailer: NULL\n");
10719c9e68d9Seric 			rcode = EX_OSERR;
10729c9e68d9Seric 			goto give_up;
10739c9e68d9Seric 		}
10749c9e68d9Seric 		else if (pid == 0)
10759c9e68d9Seric 		{
10769c9e68d9Seric 			int i;
10779c9e68d9Seric 			int saveerrno;
10789c9e68d9Seric 			char **ep;
10799c9e68d9Seric 			char *env[MAXUSERENVIRON];
10809c9e68d9Seric 			extern char **environ;
10819c9e68d9Seric 			extern int DtableSize;
10829c9e68d9Seric 
10839c9e68d9Seric 			/* child -- set up input & exec mailer */
10849c9e68d9Seric 			/* make diagnostic output be standard output */
10859c9e68d9Seric 			(void) signal(SIGINT, SIG_IGN);
10869c9e68d9Seric 			(void) signal(SIGHUP, SIG_IGN);
10879c9e68d9Seric 			(void) signal(SIGTERM, SIG_DFL);
10889c9e68d9Seric 
10899c9e68d9Seric 			/* close any other cached connections */
10909c9e68d9Seric 			mci_flush(FALSE, mci);
10919c9e68d9Seric 
10929c9e68d9Seric 			/* arrange to filter std & diag output of command */
10939c9e68d9Seric 			if (clever)
10949c9e68d9Seric 			{
10959c9e68d9Seric 				(void) close(rpvect[0]);
10966fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
10976fe8c3bcSeric 				{
10986fe8c3bcSeric 					syserr("Cannot dup pipe %d for stdout",
10996fe8c3bcSeric 						rpvect[1]);
11006fe8c3bcSeric 					_exit(EX_OSERR);
11016fe8c3bcSeric 				}
11029c9e68d9Seric 				(void) close(rpvect[1]);
11039c9e68d9Seric 			}
11049c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11059c9e68d9Seric 			{
11069c9e68d9Seric 				/* put mailer output in transcript */
11076fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
11086fe8c3bcSeric 				{
11096fe8c3bcSeric 					syserr("Cannot dup xscript %d for stdout",
11106fe8c3bcSeric 						fileno(e->e_xfp));
11116fe8c3bcSeric 					_exit(EX_OSERR);
11129c9e68d9Seric 				}
11136fe8c3bcSeric 			}
11146fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
11156fe8c3bcSeric 			{
11166fe8c3bcSeric 				syserr("Cannot dup stdout for stderr");
11176fe8c3bcSeric 				_exit(EX_OSERR);
11186fe8c3bcSeric 			}
11199c9e68d9Seric 
11209c9e68d9Seric 			/* arrange to get standard input */
11219c9e68d9Seric 			(void) close(mpvect[1]);
11229c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11239c9e68d9Seric 			{
11246fe8c3bcSeric 				syserr("Cannot dup pipe %d for stdin",
11256fe8c3bcSeric 					mpvect[0]);
11269c9e68d9Seric 				_exit(EX_OSERR);
11279c9e68d9Seric 			}
11289c9e68d9Seric 			(void) close(mpvect[0]);
11299c9e68d9Seric 			if (!bitnset(M_RESTR, m->m_flags))
11309c9e68d9Seric 			{
11319c9e68d9Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
11329c9e68d9Seric 				{
11339c9e68d9Seric 					(void) setgid(DefGid);
11349c9e68d9Seric 					(void) initgroups(DefUser, DefGid);
11359c9e68d9Seric 					(void) setuid(DefUid);
11369c9e68d9Seric 				}
11379c9e68d9Seric 				else
11389c9e68d9Seric 				{
11399c9e68d9Seric 					(void) setgid(ctladdr->q_gid);
11409c9e68d9Seric 					(void) initgroups(ctladdr->q_ruser?
11419c9e68d9Seric 						ctladdr->q_ruser: ctladdr->q_user,
11429c9e68d9Seric 						ctladdr->q_gid);
11439c9e68d9Seric 					(void) setuid(ctladdr->q_uid);
11449c9e68d9Seric 				}
11459c9e68d9Seric 			}
11469c9e68d9Seric 
11479c9e68d9Seric 			/* arrange for all the files to be closed */
11489c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
11499c9e68d9Seric 			{
11509c9e68d9Seric 				register int j;
11519c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
11529c9e68d9Seric 					(void)fcntl(i, F_SETFD, j|1);
11539c9e68d9Seric 			}
11549c9e68d9Seric 
11559c9e68d9Seric 			/* set up the mailer environment */
11569c9e68d9Seric 			i = 0;
11579c9e68d9Seric 			env[i++] = "AGENT=sendmail";
11589c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
11599c9e68d9Seric 			{
11609c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
11619c9e68d9Seric 					env[i++] = *ep;
11629c9e68d9Seric 			}
11639c9e68d9Seric 			env[i++] = NULL;
11649c9e68d9Seric 
11659c9e68d9Seric 			/* try to execute the mailer */
11669c9e68d9Seric 			execve(m->m_mailer, pv, env);
11679c9e68d9Seric 			saveerrno = errno;
11689c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
11699c9e68d9Seric 			if (m == LocalMailer)
11709c9e68d9Seric 				_exit(EX_TEMPFAIL);
11719c9e68d9Seric 			if (transienterror(saveerrno))
11729c9e68d9Seric 				_exit(EX_TEMPFAIL);
11739c9e68d9Seric 			_exit(EX_UNAVAILABLE);
11749c9e68d9Seric 		}
11759c9e68d9Seric 
11769c9e68d9Seric 		/*
11779c9e68d9Seric 		**  Set up return value.
11789c9e68d9Seric 		*/
11799c9e68d9Seric 
11809c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
11819c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
11829c9e68d9Seric 		mci->mci_mailer = m;
11839c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
11849c9e68d9Seric 		mci->mci_pid = pid;
11859c9e68d9Seric 		(void) close(mpvect[0]);
11869c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
11879c9e68d9Seric 		if (clever)
11889c9e68d9Seric 		{
11899c9e68d9Seric 			(void) close(rpvect[1]);
11909c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
11919c9e68d9Seric 		}
11929c9e68d9Seric 		else
11939c9e68d9Seric 		{
11949c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
11959c9e68d9Seric 			mci->mci_in = NULL;
11969c9e68d9Seric 		}
11979c9e68d9Seric 	}
11989c9e68d9Seric 
11999c9e68d9Seric 	/*
12009c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
12019c9e68d9Seric 	*/
12029c9e68d9Seric 
12039c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12049c9e68d9Seric 	{
12059c9e68d9Seric 		smtpinit(m, mci, e);
12069c9e68d9Seric 	}
12079c9e68d9Seric 	if (tTd(11, 1))
12089c9e68d9Seric 	{
12099c9e68d9Seric 		printf("openmailer: ");
12109c9e68d9Seric 		mci_dump(mci);
12119c9e68d9Seric 	}
12129c9e68d9Seric 
12139c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1214b31e7f2bSeric 	{
1215b31e7f2bSeric 		/* couldn't open the mailer */
1216b31e7f2bSeric 		rcode = mci->mci_exitstat;
12172a6bc25bSeric 		errno = mci->mci_errno;
1218b31e7f2bSeric 		if (rcode == EX_OK)
1219b31e7f2bSeric 		{
1220b31e7f2bSeric 			/* shouldn't happen */
122108b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12226b0f339dSeric 				rcode, mci->mci_state, firstsig);
1223b31e7f2bSeric 			rcode = EX_SOFTWARE;
1224b31e7f2bSeric 		}
1225b31e7f2bSeric 	}
1226b31e7f2bSeric 	else if (!clever)
1227b31e7f2bSeric 	{
1228b31e7f2bSeric 		/*
1229b31e7f2bSeric 		**  Format and send message.
1230b31e7f2bSeric 		*/
123115d084d5Seric 
1232b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1233b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1234b31e7f2bSeric 		putline("\n", mci->mci_out, m);
1235b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
1236b31e7f2bSeric 
1237b31e7f2bSeric 		/* get the exit status */
1238c9be6216Seric 		rcode = endmailer(mci, e, pv);
1239134746fbSeric 	}
1240134746fbSeric 	else
1241b31e7f2bSeric #ifdef SMTP
1242134746fbSeric 	{
1243b31e7f2bSeric 		/*
1244b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1245b31e7f2bSeric 		*/
124615d084d5Seric 
1247b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1248b31e7f2bSeric 		if (rcode == EX_OK)
124975889e88Seric 		{
1250ded0d3daSkarels 			register char *t = tobuf;
1251ded0d3daSkarels 			register int i;
1252ded0d3daSkarels 
1253588cad61Seric 			/* send the recipient list */
125463780dbdSeric 			tobuf[0] = '\0';
125575889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
125675889e88Seric 			{
125763780dbdSeric 				e->e_to = to->q_paddr;
125815d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
125975889e88Seric 				{
126083b7ddc9Seric 					markfailure(e, to, i);
126181161401Seric 					giveresponse(i, m, mci, e);
126263780dbdSeric 				}
126375889e88Seric 				else
126475889e88Seric 				{
1265911693bfSbostic 					*t++ = ',';
1266b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1267b31e7f2bSeric 						continue;
1268588cad61Seric 				}
1269588cad61Seric 			}
1270588cad61Seric 
127163780dbdSeric 			/* now send the data */
127263780dbdSeric 			if (tobuf[0] == '\0')
1273b31e7f2bSeric 			{
12749c9e68d9Seric 				rcode = EX_OK;
127563780dbdSeric 				e->e_to = NULL;
1276b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1277b31e7f2bSeric 					smtprset(m, mci, e);
1278b31e7f2bSeric 			}
127975889e88Seric 			else
128075889e88Seric 			{
128163780dbdSeric 				e->e_to = tobuf + 1;
128275889e88Seric 				rcode = smtpdata(m, mci, e);
128363780dbdSeric 			}
128463780dbdSeric 
128563780dbdSeric 			/* now close the connection */
1286b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
128715d084d5Seric 				smtpquit(m, mci, e);
128863780dbdSeric 		}
12899c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
12909c9e68d9Seric 		{
12919c9e68d9Seric 			/* try next MX site */
12929c9e68d9Seric 			goto tryhost;
12939c9e68d9Seric 		}
1294c579ef51Seric 	}
1295b31e7f2bSeric #else /* not SMTP */
1296a05b3449Sbostic 	{
129708b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1298845e533cSeric 		rcode = EX_CONFIG;
1299b31e7f2bSeric 		goto give_up;
1300a05b3449Sbostic 	}
1301b31e7f2bSeric #endif /* SMTP */
1302134746fbSeric #ifdef NAMED_BIND
13032bcc6d2dSeric 	if (ConfigLevel < 2)
1304912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1305134746fbSeric #endif
13065dfc646bSeric 
1307b31e7f2bSeric 	/* arrange a return receipt if requested */
13088c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1309b31e7f2bSeric 	{
1310b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1311b31e7f2bSeric 		/* do we want to send back more info? */
1312b31e7f2bSeric 	}
1313b31e7f2bSeric 
1314c77d1c25Seric 	/*
131563780dbdSeric 	**  Do final status disposal.
131663780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1317c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1318c77d1c25Seric 	**		addressees.
1319c77d1c25Seric 	*/
1320c77d1c25Seric 
1321b31e7f2bSeric   give_up:
132263780dbdSeric 	if (tobuf[0] != '\0')
132381161401Seric 		giveresponse(rcode, m, mci, e);
1324772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1325b31e7f2bSeric 	{
1326dde5acadSeric 		if (rcode != EX_OK)
132783b7ddc9Seric 			markfailure(e, to, rcode);
1328dde5acadSeric 		else
1329655518ecSeric 		{
1330dde5acadSeric 			to->q_flags |= QSENT;
1331655518ecSeric 			e->e_nsent++;
1332655518ecSeric 		}
1333b31e7f2bSeric 	}
1334b31e7f2bSeric 
1335b31e7f2bSeric 	/*
1336b31e7f2bSeric 	**  Restore state and return.
1337b31e7f2bSeric 	*/
1338c77d1c25Seric 
133935490626Seric 	errno = 0;
1340588cad61Seric 	define('g', (char *) NULL, e);
13415826d9d3Seric 	return (rcode);
134225a99e2eSeric }
13435dfc646bSeric /*
134483b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
134583b7ddc9Seric **
134683b7ddc9Seric **	Parameters:
134783b7ddc9Seric **		e -- the envelope we are sending.
134883b7ddc9Seric **		q -- the address to mark.
134983b7ddc9Seric **		rcode -- the code signifying the particular failure.
135083b7ddc9Seric **
135183b7ddc9Seric **	Returns:
135283b7ddc9Seric **		none.
135383b7ddc9Seric **
135483b7ddc9Seric **	Side Effects:
135583b7ddc9Seric **		marks the address (and possibly the envelope) with the
135683b7ddc9Seric **			failure so that an error will be returned or
135783b7ddc9Seric **			the message will be queued, as appropriate.
135883b7ddc9Seric */
135983b7ddc9Seric 
136083b7ddc9Seric markfailure(e, q, rcode)
136183b7ddc9Seric 	register ENVELOPE *e;
136283b7ddc9Seric 	register ADDRESS *q;
136383b7ddc9Seric 	int rcode;
136483b7ddc9Seric {
136519c47125Seric 	char buf[MAXLINE];
136619c47125Seric 	extern char *pintvl();
136719c47125Seric 
136883b7ddc9Seric 	if (rcode == EX_OK)
136983b7ddc9Seric 		return;
1370ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
137183b7ddc9Seric 		q->q_flags |= QBADADDR;
137219c47125Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
137383b7ddc9Seric 	{
137483b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
1375198d9be0Seric 		{
1376198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
137719c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
1378198d9be0Seric 			if (e->e_message != NULL)
1379198d9be0Seric 				free(e->e_message);
1380198d9be0Seric 			e->e_message = newstr(buf);
138108b25121Seric 			message(buf);
1382198d9be0Seric 		}
138383b7ddc9Seric 		q->q_flags |= QBADADDR;
138483b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
1385c13b5dfcSeric 		fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr);
138683b7ddc9Seric 	}
138783b7ddc9Seric 	else
138819c47125Seric 	{
138983b7ddc9Seric 		q->q_flags |= QQUEUEUP;
139019c47125Seric 		if (TimeOuts.to_q_warning > 0 &&
139119c47125Seric 		    curtime() > e->e_ctime + TimeOuts.to_q_warning)
139219c47125Seric 		{
139319c47125Seric 			if (!bitset(EF_WARNING, e->e_flags) &&
139419c47125Seric 			    e->e_class >= 0)
139519c47125Seric 			{
139619c47125Seric 				(void) sprintf(buf,
139719c47125Seric 					"warning: cannot send message for %s",
139819c47125Seric 					pintvl(TimeOuts.to_q_warning, FALSE));
139919c47125Seric 				if (e->e_message != NULL)
140019c47125Seric 					free(e->e_message);
140119c47125Seric 				e->e_message = newstr(buf);
140219c47125Seric 				message(buf);
140319c47125Seric 				e->e_flags |= EF_WARNING|EF_TIMEOUT;
140419c47125Seric 			}
140519c47125Seric 			fprintf(e->e_xfp,
140619c47125Seric 				"%s... Warning: message still undelivered after %s\n",
140719c47125Seric 				q->q_paddr, pintvl(TimeOuts.to_q_warning, FALSE));
140819c47125Seric 			fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
140919c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
141019c47125Seric 		}
141119c47125Seric 	}
141283b7ddc9Seric }
141383b7ddc9Seric /*
1414c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1415c579ef51Seric **
1416c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1417c579ef51Seric **	violation), so we report those specially.  For other
1418c579ef51Seric **	errors, we choose a status message (into statmsg),
1419c579ef51Seric **	and if it represents an error, we print it.
1420c579ef51Seric **
1421c579ef51Seric **	Parameters:
1422c579ef51Seric **		pid -- pid of mailer.
1423c9be6216Seric **		e -- the current envelope.
1424c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1425c9be6216Seric **			(for error messages).
1426c579ef51Seric **
1427c579ef51Seric **	Returns:
1428c579ef51Seric **		exit code of mailer.
1429c579ef51Seric **
1430c579ef51Seric **	Side Effects:
1431c579ef51Seric **		none.
1432c579ef51Seric */
1433c579ef51Seric 
1434c9be6216Seric endmailer(mci, e, pv)
1435b31e7f2bSeric 	register MCI *mci;
1436c9be6216Seric 	register ENVELOPE *e;
1437c9be6216Seric 	char **pv;
1438c579ef51Seric {
1439588cad61Seric 	int st;
1440c579ef51Seric 
144175889e88Seric 	/* close any connections */
144275889e88Seric 	if (mci->mci_in != NULL)
1443c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
144475889e88Seric 	if (mci->mci_out != NULL)
1445c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
144675889e88Seric 	mci->mci_in = mci->mci_out = NULL;
144775889e88Seric 	mci->mci_state = MCIS_CLOSED;
144875889e88Seric 
144933db8731Seric 	/* in the IPC case there is nothing to wait for */
145075889e88Seric 	if (mci->mci_pid == 0)
145133db8731Seric 		return (EX_OK);
145233db8731Seric 
145333db8731Seric 	/* wait for the mailer process to die and collect status */
145475889e88Seric 	st = waitfor(mci->mci_pid);
1455588cad61Seric 	if (st == -1)
145678de67c1Seric 	{
1457c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1458588cad61Seric 		return (EX_SOFTWARE);
1459c579ef51Seric 	}
146033db8731Seric 
146133db8731Seric 	/* see if it died a horrid death */
1462c579ef51Seric 	if ((st & 0377) != 0)
1463c579ef51Seric 	{
1464c9be6216Seric 		syserr("mailer %s died with signal %o", pv[0], st);
1465c9be6216Seric 
1466c9be6216Seric 		/* log the arguments */
1467c9be6216Seric 		if (e->e_xfp != NULL)
1468c9be6216Seric 		{
1469c9be6216Seric 			register char **av;
1470c9be6216Seric 
1471c9be6216Seric 			fprintf(e->e_xfp, "Arguments:");
1472c9be6216Seric 			for (av = pv; *av != NULL; av++)
1473c9be6216Seric 				fprintf(e->e_xfp, " %s", *av);
1474c9be6216Seric 			fprintf(e->e_xfp, "\n");
1475c9be6216Seric 		}
1476c9be6216Seric 
14775f73204aSeric 		ExitStat = EX_TEMPFAIL;
14785f73204aSeric 		return (EX_TEMPFAIL);
1479c579ef51Seric 	}
148033db8731Seric 
148133db8731Seric 	/* normal death -- return status */
1482588cad61Seric 	st = (st >> 8) & 0377;
1483588cad61Seric 	return (st);
1484c579ef51Seric }
1485c579ef51Seric /*
148625a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
148725a99e2eSeric **
148825a99e2eSeric **	Parameters:
148925a99e2eSeric **		stat -- the status code from the mailer (high byte
149025a99e2eSeric **			only; core dumps must have been taken care of
149125a99e2eSeric **			already).
149281161401Seric **		m -- the mailer info for this mailer.
149381161401Seric **		mci -- the mailer connection info -- can be NULL if the
149481161401Seric **			response is given before the connection is made.
149581161401Seric **		e -- the current envelope.
149625a99e2eSeric **
149725a99e2eSeric **	Returns:
1498db8841e9Seric **		none.
149925a99e2eSeric **
150025a99e2eSeric **	Side Effects:
1501c1f9df2cSeric **		Errors may be incremented.
150225a99e2eSeric **		ExitStat may be set.
150325a99e2eSeric */
150425a99e2eSeric 
150581161401Seric giveresponse(stat, m, mci, e)
150625a99e2eSeric 	int stat;
1507588cad61Seric 	register MAILER *m;
150881161401Seric 	register MCI *mci;
1509198d9be0Seric 	ENVELOPE *e;
151025a99e2eSeric {
151125a99e2eSeric 	register char *statmsg;
151225a99e2eSeric 	extern char *SysExMsg[];
151325a99e2eSeric 	register int i;
1514d4bd8f0eSbostic 	extern int N_SysEx;
1515d4bd8f0eSbostic #ifdef NAMED_BIND
1516d4bd8f0eSbostic 	extern int h_errno;
1517d4bd8f0eSbostic #endif
1518198d9be0Seric 	char buf[MAXLINE];
15197d55540cSeric 	extern char *errstring();
152025a99e2eSeric 
152113bbc08cSeric 	/*
152213bbc08cSeric 	**  Compute status message from code.
152313bbc08cSeric 	*/
152413bbc08cSeric 
152525a99e2eSeric 	i = stat - EX__BASE;
1526588cad61Seric 	if (stat == 0)
15276fe8c3bcSeric 	{
1528588cad61Seric 		statmsg = "250 Sent";
1529*ce5531bdSeric 		if (e->e_statmsg != NULL)
15306fe8c3bcSeric 		{
1531*ce5531bdSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
15326fe8c3bcSeric 			statmsg = buf;
15336fe8c3bcSeric 		}
15346fe8c3bcSeric 	}
1535588cad61Seric 	else if (i < 0 || i > N_SysEx)
1536588cad61Seric 	{
1537588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1538588cad61Seric 		stat = EX_UNAVAILABLE;
1539588cad61Seric 		statmsg = buf;
1540588cad61Seric 	}
1541198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1542198d9be0Seric 	{
15437d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1544d4bd8f0eSbostic #ifdef NAMED_BIND
1545f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1546f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1547f28da541Smiriam 		else
1548d4bd8f0eSbostic #endif
1549f28da541Smiriam 		{
15508557d168Seric 			if (errno != 0)
1551d87e85f3Seric 				statmsg = errstring(errno);
1552d87e85f3Seric 			else
1553d87e85f3Seric 			{
1554d87e85f3Seric #ifdef SMTP
1555d87e85f3Seric 				extern char SmtpError[];
1556d87e85f3Seric 
1557d87e85f3Seric 				statmsg = SmtpError;
15586c2c3107Seric #else /* SMTP */
1559d87e85f3Seric 				statmsg = NULL;
15606c2c3107Seric #endif /* SMTP */
1561d87e85f3Seric 			}
1562f28da541Smiriam 		}
1563d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1564d87e85f3Seric 		{
156587c9b3e7Seric 			(void) strcat(buf, ": ");
1566d87e85f3Seric 			(void) strcat(buf, statmsg);
15678557d168Seric 		}
1568198d9be0Seric 		statmsg = buf;
1569198d9be0Seric 	}
157025a99e2eSeric 	else
1571d87e85f3Seric 	{
157225a99e2eSeric 		statmsg = SysExMsg[i];
15737d55540cSeric 		if (*statmsg++ == ':')
15747d55540cSeric 		{
15757d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
15767d55540cSeric 			statmsg = buf;
15777d55540cSeric 		}
1578d87e85f3Seric 	}
1579588cad61Seric 
1580588cad61Seric 	/*
1581588cad61Seric 	**  Print the message as appropriate
1582588cad61Seric 	*/
1583588cad61Seric 
1584198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1585e12d03eeSeric 		message(&statmsg[4], errstring(errno));
158625a99e2eSeric 	else
158725a99e2eSeric 	{
1588c1f9df2cSeric 		Errors++;
1589e12d03eeSeric 		usrerr(statmsg, errstring(errno));
159025a99e2eSeric 	}
159125a99e2eSeric 
159225a99e2eSeric 	/*
159325a99e2eSeric 	**  Final cleanup.
159425a99e2eSeric 	**	Log a record of the transaction.  Compute the new
159525a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
159625a99e2eSeric 	**	that.
159725a99e2eSeric 	*/
159825a99e2eSeric 
15992f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
160081161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1601eb238f8cSeric 
1602eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1603eb238f8cSeric 		setstat(stat);
1604198d9be0Seric 	if (stat != EX_OK)
1605198d9be0Seric 	{
1606198d9be0Seric 		if (e->e_message != NULL)
1607198d9be0Seric 			free(e->e_message);
1608198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1609198d9be0Seric 	}
16108557d168Seric 	errno = 0;
1611d4bd8f0eSbostic #ifdef NAMED_BIND
1612f28da541Smiriam 	h_errno = 0;
1613d4bd8f0eSbostic #endif
1614eb238f8cSeric }
1615eb238f8cSeric /*
1616eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1617eb238f8cSeric **
1618eb238f8cSeric **	Parameters:
161981161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
162081161401Seric **		mci -- the mailer connection info -- can be NULL if the
162181161401Seric **			log is occuring when no connection is active.
162281161401Seric **		stat -- the message to print for the status.
162381161401Seric **		e -- the current envelope.
1624eb238f8cSeric **
1625eb238f8cSeric **	Returns:
1626eb238f8cSeric **		none
1627eb238f8cSeric **
1628eb238f8cSeric **	Side Effects:
1629eb238f8cSeric **		none
1630eb238f8cSeric */
1631eb238f8cSeric 
163281161401Seric logdelivery(m, mci, stat, e)
163381161401Seric 	MAILER *m;
163481161401Seric 	register MCI *mci;
1635eb238f8cSeric 	char *stat;
1636b31e7f2bSeric 	register ENVELOPE *e;
16375cf56be3Seric {
1638eb238f8cSeric # ifdef LOG
16399507d1f9Seric 	char *curhost;
1640d6acf3eeSeric 	char buf[512];
16419507d1f9Seric 	extern char *pintvl();
16429507d1f9Seric 	extern char *macvalue();
16439507d1f9Seric 
164448ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
164581161401Seric 
164648ed5d33Seric 	if (m != NULL)
164771ff6caaSeric 	{
164848ed5d33Seric 		(void) strcat(buf, ", mailer=");
164948ed5d33Seric 		(void) strcat(buf, m->m_name);
165071ff6caaSeric 	}
165148ed5d33Seric 
165248ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
165371ff6caaSeric 	{
165471ff6caaSeric # ifdef DAEMON
1655e2f2f828Seric 		extern SOCKADDR CurHostAddr;
1656e2f2f828Seric 		extern char *anynet_ntoa();
165748ed5d33Seric # endif
165871ff6caaSeric 
165948ed5d33Seric 		(void) strcat(buf, ", relay=");
166048ed5d33Seric 		(void) strcat(buf, mci->mci_host);
166148ed5d33Seric 
166248ed5d33Seric # ifdef DAEMON
166348ed5d33Seric 		(void) strcat(buf, " (");
1664e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
166548ed5d33Seric 		(void) strcat(buf, ")");
166671ff6caaSeric # endif
166771ff6caaSeric 	}
16689507d1f9Seric 	else
166948ed5d33Seric 	{
167048ed5d33Seric 		char *p = macvalue('h', e);
16719507d1f9Seric 
167248ed5d33Seric 		if (p != NULL && p[0] != '\0')
167348ed5d33Seric 		{
167448ed5d33Seric 			(void) strcat(buf, ", relay=");
167548ed5d33Seric 			(void) strcat(buf, p);
167648ed5d33Seric 		}
167748ed5d33Seric 	}
1678d6acf3eeSeric 
1679d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1680d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
16816c2c3107Seric # endif /* LOG */
168225a99e2eSeric }
168325a99e2eSeric /*
168451552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
168525a99e2eSeric **
168651552439Seric **	This can be made an arbitrary message separator by changing $l
168751552439Seric **
16889b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
16899b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
16909b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
16919b6c17a6Seric **	this kind of antique garbage????
169225a99e2eSeric **
169325a99e2eSeric **	Parameters:
169451552439Seric **		fp -- the file to output to.
169551552439Seric **		m -- the mailer describing this entry.
169625a99e2eSeric **
169725a99e2eSeric **	Returns:
169851552439Seric **		none
169925a99e2eSeric **
170025a99e2eSeric **	Side Effects:
170151552439Seric **		outputs some text to fp.
170225a99e2eSeric */
170325a99e2eSeric 
1704b31e7f2bSeric putfromline(fp, m, e)
170551552439Seric 	register FILE *fp;
170651552439Seric 	register MAILER *m;
1707b31e7f2bSeric 	ENVELOPE *e;
170825a99e2eSeric {
17092bc47524Seric 	char *template = "\201l\n";
171051552439Seric 	char buf[MAXLINE];
171125a99e2eSeric 
171257fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
171351552439Seric 		return;
171413bbc08cSeric 
17152c7e1b8dSeric # ifdef UGLYUUCP
171657fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
171774b6e67bSeric 	{
1718ea09d6edSeric 		char *bang;
1719ea09d6edSeric 		char xbuf[MAXLINE];
172074b6e67bSeric 
1721ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17226c2c3107Seric 		bang = strchr(buf, '!');
172374b6e67bSeric 		if (bang == NULL)
172408b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
172574b6e67bSeric 		else
1726588cad61Seric 		{
1727ea09d6edSeric 			*bang++ = '\0';
17282bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1729ea09d6edSeric 			template = xbuf;
173074b6e67bSeric 		}
1731588cad61Seric 	}
17326c2c3107Seric # endif /* UGLYUUCP */
1733b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
173477b52738Seric 	putline(buf, fp, m);
1735bc6e2962Seric }
1736bc6e2962Seric /*
173751552439Seric **  PUTBODY -- put the body of a message.
173851552439Seric **
173951552439Seric **	Parameters:
174051552439Seric **		fp -- file to output onto.
174177b52738Seric **		m -- a mailer descriptor to control output format.
17429a6a5f55Seric **		e -- the envelope to put out.
174351552439Seric **
174451552439Seric **	Returns:
174551552439Seric **		none.
174651552439Seric **
174751552439Seric **	Side Effects:
174851552439Seric **		The message is written onto fp.
174951552439Seric */
175051552439Seric 
175177b52738Seric putbody(fp, m, e)
175251552439Seric 	FILE *fp;
1753588cad61Seric 	MAILER *m;
17549a6a5f55Seric 	register ENVELOPE *e;
175551552439Seric {
175677b52738Seric 	char buf[MAXLINE];
175751552439Seric 
175851552439Seric 	/*
175951552439Seric 	**  Output the body of the message
176051552439Seric 	*/
176151552439Seric 
17629a6a5f55Seric 	if (e->e_dfp == NULL)
176351552439Seric 	{
17649a6a5f55Seric 		if (e->e_df != NULL)
17659a6a5f55Seric 		{
17669a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
17679a6a5f55Seric 			if (e->e_dfp == NULL)
17688f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
17698f9146b0Srick 				e->e_df, e->e_to, e->e_from);
17709a6a5f55Seric 		}
17719a6a5f55Seric 		else
177277b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
17739a6a5f55Seric 	}
17749a6a5f55Seric 	if (e->e_dfp != NULL)
17759a6a5f55Seric 	{
17769a6a5f55Seric 		rewind(e->e_dfp);
177777b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
177824fc8aeeSeric 		{
177924fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1780d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
17813462ad9eSeric 				(void) putc('>', fp);
178277b52738Seric 			putline(buf, fp, m);
178324fc8aeeSeric 		}
178451552439Seric 
17859a6a5f55Seric 		if (ferror(e->e_dfp))
178651552439Seric 		{
178751552439Seric 			syserr("putbody: read error");
178851552439Seric 			ExitStat = EX_IOERR;
178951552439Seric 		}
179051552439Seric 	}
179151552439Seric 
179251552439Seric 	(void) fflush(fp);
179351552439Seric 	if (ferror(fp) && errno != EPIPE)
179451552439Seric 	{
179551552439Seric 		syserr("putbody: write error");
179651552439Seric 		ExitStat = EX_IOERR;
179751552439Seric 	}
179851552439Seric 	errno = 0;
179925a99e2eSeric }
180025a99e2eSeric /*
180125a99e2eSeric **  MAILFILE -- Send a message to a file.
180225a99e2eSeric **
1803f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1804f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1805f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1806f129ec7dSeric **	sendmail runs as root.
1807f129ec7dSeric **
1808588cad61Seric **	This could be done as a subordinate mailer, except that it
1809588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1810588cad61Seric **	view this as being sufficiently important as to include it
1811588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1812588cad61Seric **	to create another process plus some pipes to save the message.
1813588cad61Seric **
181425a99e2eSeric **	Parameters:
181525a99e2eSeric **		filename -- the name of the file to send to.
18166259796dSeric **		ctladdr -- the controlling address header -- includes
18176259796dSeric **			the userid/groupid to be when sending.
181825a99e2eSeric **
181925a99e2eSeric **	Returns:
182025a99e2eSeric **		The exit code associated with the operation.
182125a99e2eSeric **
182225a99e2eSeric **	Side Effects:
182325a99e2eSeric **		none.
182425a99e2eSeric */
182525a99e2eSeric 
1826b31e7f2bSeric mailfile(filename, ctladdr, e)
182725a99e2eSeric 	char *filename;
18286259796dSeric 	ADDRESS *ctladdr;
1829b31e7f2bSeric 	register ENVELOPE *e;
183025a99e2eSeric {
183125a99e2eSeric 	register FILE *f;
183232d19d43Seric 	register int pid;
183315d084d5Seric 	int mode;
183425a99e2eSeric 
183532d19d43Seric 	/*
183632d19d43Seric 	**  Fork so we can change permissions here.
183732d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
183832d19d43Seric 	**	the complications of calling subroutines, etc.
183932d19d43Seric 	*/
184032d19d43Seric 
184132d19d43Seric 	DOFORK(fork);
184232d19d43Seric 
184332d19d43Seric 	if (pid < 0)
184432d19d43Seric 		return (EX_OSERR);
184532d19d43Seric 	else if (pid == 0)
184632d19d43Seric 	{
184732d19d43Seric 		/* child -- actually write to file */
1848f129ec7dSeric 		struct stat stb;
1849f129ec7dSeric 
18500984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
18510984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
18520984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
18533462ad9eSeric 		(void) umask(OldUmask);
185495f16dc0Seric 
1855f129ec7dSeric 		if (stat(filename, &stb) < 0)
1856e6e1265fSeric 			stb.st_mode = 0666;
185715d084d5Seric 		mode = stb.st_mode;
185895f16dc0Seric 
185995f16dc0Seric 		/* limit the errors to those actually caused in the child */
186095f16dc0Seric 		errno = 0;
186195f16dc0Seric 		ExitStat = EX_OK;
186295f16dc0Seric 
1863f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1864f129ec7dSeric 			exit(EX_CANTCREAT);
186503827b5fSeric 		if (ctladdr == NULL)
18668f9146b0Srick 			ctladdr = &e->e_from;
186715d084d5Seric 		else
186815d084d5Seric 		{
186915d084d5Seric 			/* ignore setuid and setgid bits */
187015d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
187115d084d5Seric 		}
187215d084d5Seric 
18738f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
18748f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
18758f9146b0Srick 		{
18768f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
187795f16dc0Seric 			if (e->e_dfp == NULL)
187895f16dc0Seric 			{
18798f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
18808f9146b0Srick 					e->e_df, e->e_to, e->e_from);
18818f9146b0Srick 			}
18828f9146b0Srick 		}
18838f9146b0Srick 
188415d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1885e36b99e2Seric 		{
188695f16dc0Seric 			if (ctladdr->q_uid == 0)
188795f16dc0Seric 			{
1888e36b99e2Seric 				(void) setgid(DefGid);
1889898a126bSbostic 				(void) initgroups(DefUser, DefGid);
189095f16dc0Seric 			}
189195f16dc0Seric 			else
189295f16dc0Seric 			{
18936259796dSeric 				(void) setgid(ctladdr->q_gid);
1894898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1895898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1896898a126bSbostic 					ctladdr->q_gid);
1897898a126bSbostic 			}
1898e36b99e2Seric 		}
189915d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1900e36b99e2Seric 		{
1901e36b99e2Seric 			if (ctladdr->q_uid == 0)
1902e36b99e2Seric 				(void) setuid(DefUid);
1903e36b99e2Seric 			else
19046259796dSeric 				(void) setuid(ctladdr->q_uid);
1905e36b99e2Seric 		}
190695f16dc0Seric 		FileName = filename;
190795f16dc0Seric 		LineNumber = 0;
190827628d59Seric 		f = dfopen(filename, "a");
190925a99e2eSeric 		if (f == NULL)
191095f16dc0Seric 		{
191108b25121Seric 			message("554 cannot open");
191232d19d43Seric 			exit(EX_CANTCREAT);
191395f16dc0Seric 		}
191425a99e2eSeric 
1915b31e7f2bSeric 		putfromline(f, ProgMailer, e);
1916b843d759Seric 		(*e->e_puthdr)(f, ProgMailer, e);
191777b52738Seric 		putline("\n", f, ProgMailer);
1918b843d759Seric 		(*e->e_putbody)(f, ProgMailer, e);
191977b52738Seric 		putline("\n", f, ProgMailer);
192095f16dc0Seric 		if (ferror(f))
192195f16dc0Seric 		{
192208b25121Seric 			message("451 I/O error");
192395f16dc0Seric 			setstat(EX_IOERR);
192495f16dc0Seric 		}
1925ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
192632d19d43Seric 		(void) fflush(stdout);
1927e36b99e2Seric 
192827628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1929c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
193095f16dc0Seric 		exit(ExitStat);
193113bbc08cSeric 		/*NOTREACHED*/
193232d19d43Seric 	}
193332d19d43Seric 	else
193432d19d43Seric 	{
193532d19d43Seric 		/* parent -- wait for exit status */
1936588cad61Seric 		int st;
193732d19d43Seric 
1938588cad61Seric 		st = waitfor(pid);
1939588cad61Seric 		if ((st & 0377) != 0)
1940588cad61Seric 			return (EX_UNAVAILABLE);
1941588cad61Seric 		else
1942588cad61Seric 			return ((st >> 8) & 0377);
19438f9146b0Srick 		/*NOTREACHED*/
194432d19d43Seric 	}
194525a99e2eSeric }
1946ea4dc939Seric /*
1947e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1948e103b48fSeric **
1949e103b48fSeric **	The signature describes how we are going to send this -- it
1950e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1951e103b48fSeric **	an ordered list of MX hosts.
1952e103b48fSeric **
1953e103b48fSeric **	Parameters:
1954e103b48fSeric **		m -- the mailer describing this host.
1955e103b48fSeric **		host -- the host name.
1956e103b48fSeric **		e -- the current envelope.
1957e103b48fSeric **
1958e103b48fSeric **	Returns:
1959e103b48fSeric **		The signature for this host.
1960e103b48fSeric **
1961e103b48fSeric **	Side Effects:
1962e103b48fSeric **		Can tweak the symbol table.
1963e103b48fSeric */
1964e103b48fSeric 
1965e103b48fSeric char *
1966e103b48fSeric hostsignature(m, host, e)
1967e103b48fSeric 	register MAILER *m;
1968e103b48fSeric 	char *host;
1969e103b48fSeric 	ENVELOPE *e;
1970e103b48fSeric {
1971e103b48fSeric 	register char *p;
1972e103b48fSeric 	register STAB *s;
1973e103b48fSeric 	int i;
1974e103b48fSeric 	int len;
1975e103b48fSeric #ifdef NAMED_BIND
1976e103b48fSeric 	int nmx;
1977e103b48fSeric 	auto int rcode;
1978e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
1979e103b48fSeric 	static char myhostbuf[MAXNAME];
1980e103b48fSeric #endif
1981e103b48fSeric 
1982e103b48fSeric 	/*
1983e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
1984e103b48fSeric 	*/
1985e103b48fSeric 
1986e103b48fSeric 	p = m->m_mailer;
1987e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
1988e103b48fSeric 	{
1989e103b48fSeric 		/* just an ordinary mailer */
1990e103b48fSeric 		return host;
1991e103b48fSeric 	}
1992e103b48fSeric 
1993e103b48fSeric 	/*
1994e103b48fSeric 	**  If it is a numeric address, just return it.
1995e103b48fSeric 	*/
1996e103b48fSeric 
1997e103b48fSeric 	if (host[0] == '[')
1998e103b48fSeric 		return host;
1999e103b48fSeric 
2000e103b48fSeric 	/*
2001e103b48fSeric 	**  Look it up in the symbol table.
2002e103b48fSeric 	*/
2003e103b48fSeric 
2004e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2005e103b48fSeric 	if (s->s_hostsig != NULL)
2006e103b48fSeric 		return s->s_hostsig;
2007e103b48fSeric 
2008e103b48fSeric 	/*
2009e103b48fSeric 	**  Not already there -- create a signature.
2010e103b48fSeric 	*/
2011e103b48fSeric 
2012e103b48fSeric #ifdef NAMED_BIND
2013e103b48fSeric 	if (myhostbuf[0] == '\0')
20142bc47524Seric 		expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e);
2015e103b48fSeric 
2016e103b48fSeric 	nmx = getmxrr(host, mxhosts, myhostbuf, &rcode);
20177d55540cSeric 
2018e103b48fSeric 	if (nmx <= 0)
2019e103b48fSeric 	{
2020e103b48fSeric 		register MCI *mci;
2021e103b48fSeric 		extern int errno;
2022e103b48fSeric 		extern MCI *mci_get();
2023e103b48fSeric 
2024e103b48fSeric 		/* update the connection info for this host */
2025e103b48fSeric 		mci = mci_get(host, m);
2026e103b48fSeric 		mci->mci_exitstat = rcode;
2027e103b48fSeric 		mci->mci_errno = errno;
2028e103b48fSeric 
2029e103b48fSeric 		/* and return the original host name as the signature */
2030e103b48fSeric 		s->s_hostsig = host;
2031e103b48fSeric 		return host;
2032e103b48fSeric 	}
2033e103b48fSeric 
2034e103b48fSeric 	len = 0;
2035e103b48fSeric 	for (i = 0; i < nmx; i++)
2036e103b48fSeric 	{
2037e103b48fSeric 		len += strlen(mxhosts[i]) + 1;
2038e103b48fSeric 	}
2039e103b48fSeric 	s->s_hostsig = p = xalloc(len);
2040e103b48fSeric 	for (i = 0; i < nmx; i++)
2041e103b48fSeric 	{
2042e103b48fSeric 		if (i != 0)
2043e103b48fSeric 			*p++ = ':';
2044e103b48fSeric 		strcpy(p, mxhosts[i]);
2045e103b48fSeric 		p += strlen(p);
2046e103b48fSeric 	}
2047e103b48fSeric 	makelower(s->s_hostsig);
2048e103b48fSeric #else
2049e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2050e103b48fSeric 	s->s_hostsig = host;
2051e103b48fSeric #endif
2052e103b48fSeric 	if (tTd(17, 1))
2053e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2054e103b48fSeric 	return s->s_hostsig;
2055e103b48fSeric }
2056