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*6fe8c3bcSeric static char sccsid[] = "@(#)deliver.c	6.56 (Berkeley) 03/29/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 
1009c9e68d9Seric 	/*
1019c9e68d9Seric 	**  Handle alias owners.
1029c9e68d9Seric 	**
1039c9e68d9Seric 	**	We scan up the q_alias chain looking for owners.
1049c9e68d9Seric 	**	We discard owners that are the same as the return path.
1059c9e68d9Seric 	*/
1069c9e68d9Seric 
1079c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1089c9e68d9Seric 	{
1099c9e68d9Seric 		register struct address *a;
1109c9e68d9Seric 
1119c9e68d9Seric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
1129c9e68d9Seric 			continue;
1139c9e68d9Seric 		if (a != NULL)
1149c9e68d9Seric 			q->q_owner = a->q_owner;
1159c9e68d9Seric 
1169c9e68d9Seric 		if (q->q_owner != NULL &&
1179c9e68d9Seric 		    !bitset(QDONTSEND, q->q_flags) &&
1189c9e68d9Seric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
1199c9e68d9Seric 			q->q_owner = NULL;
1209c9e68d9Seric 	}
1219c9e68d9Seric 
1229c9e68d9Seric 	owner = "";
1239c9e68d9Seric 	otherowners = 1;
1249c9e68d9Seric 	while (owner != NULL && otherowners > 0)
1259c9e68d9Seric 	{
1269c9e68d9Seric 		owner = NULL;
1279c9e68d9Seric 		otherowners = 0;
1289c9e68d9Seric 
1299c9e68d9Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1309c9e68d9Seric 		{
1319c9e68d9Seric 			if (bitset(QDONTSEND, q->q_flags))
1329c9e68d9Seric 				continue;
1339c9e68d9Seric 
1349c9e68d9Seric 			if (q->q_owner != NULL)
1359c9e68d9Seric 			{
1369c9e68d9Seric 				if (owner == NULL)
1379c9e68d9Seric 					owner = q->q_owner;
1389c9e68d9Seric 				else if (owner != q->q_owner)
1399c9e68d9Seric 				{
1409c9e68d9Seric 					if (strcmp(owner, q->q_owner) == 0)
1419c9e68d9Seric 					{
1429c9e68d9Seric 						/* make future comparisons cheap */
1439c9e68d9Seric 						q->q_owner = owner;
1449c9e68d9Seric 					}
1459c9e68d9Seric 					else
1469c9e68d9Seric 					{
1479c9e68d9Seric 						otherowners++;
1489c9e68d9Seric 					}
1499c9e68d9Seric 					owner = q->q_owner;
1509c9e68d9Seric 				}
1519c9e68d9Seric 			}
1529c9e68d9Seric 			else
1539c9e68d9Seric 			{
1549c9e68d9Seric 				otherowners++;
1559c9e68d9Seric 			}
1569c9e68d9Seric 		}
1579c9e68d9Seric 
1589c9e68d9Seric 		if (owner != NULL && otherowners > 0)
1599c9e68d9Seric 		{
1609c9e68d9Seric 			extern HDR *copyheader();
1619c9e68d9Seric 			extern ADDRESS *copyqueue();
1629c9e68d9Seric 
1639c9e68d9Seric 			/*
1649c9e68d9Seric 			**  Split this envelope into two.
1659c9e68d9Seric 			*/
1669c9e68d9Seric 
1679c9e68d9Seric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
1689c9e68d9Seric 			*ee = *e;
1699c9e68d9Seric 			ee->e_id = NULL;
1709c9e68d9Seric 			(void) queuename(ee, '\0');
1719c9e68d9Seric 
1729c9e68d9Seric 			if (tTd(13, 1))
1739c9e68d9Seric 				printf("sendall: split %s into %s\n",
1749c9e68d9Seric 					e->e_id, ee->e_id);
1759c9e68d9Seric 
1769c9e68d9Seric 			ee->e_header = copyheader(e->e_header);
1779c9e68d9Seric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
1789c9e68d9Seric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
1799c9e68d9Seric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE);
1809c9e68d9Seric 			setsender(owner, ee, NULL, TRUE);
1819c9e68d9Seric 			if (tTd(13, 5))
1829c9e68d9Seric 			{
1839c9e68d9Seric 				printf("sendall(split): QDONTSEND ");
1849c9e68d9Seric 				printaddr(&ee->e_from, FALSE);
1859c9e68d9Seric 			}
1869c9e68d9Seric 			ee->e_from.q_flags |= QDONTSEND;
1879c9e68d9Seric 			ee->e_dfp = NULL;
1889c9e68d9Seric 			ee->e_xfp = NULL;
1899c9e68d9Seric 			ee->e_lockfp = NULL;
1909c9e68d9Seric 			ee->e_df = NULL;
1919c9e68d9Seric 			ee->e_errormode = EM_MAIL;
1929c9e68d9Seric 			ee->e_sibling = splitenv;
1939c9e68d9Seric 			splitenv = ee;
1949c9e68d9Seric 
1959c9e68d9Seric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1969c9e68d9Seric 				if (q->q_owner == owner)
1979c9e68d9Seric 					q->q_flags |= QDONTSEND;
1989c9e68d9Seric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
1999c9e68d9Seric 				if (q->q_owner != owner)
2009c9e68d9Seric 					q->q_flags |= QDONTSEND;
2019c9e68d9Seric 
2029c9e68d9Seric 			if (e->e_df != NULL && mode != SM_VERIFY)
2039c9e68d9Seric 			{
2049c9e68d9Seric 				ee->e_dfp = NULL;
2059c9e68d9Seric 				ee->e_df = newstr(queuename(ee, 'd'));
2069c9e68d9Seric 				if (link(e->e_df, ee->e_df) < 0)
2079c9e68d9Seric 				{
2089c9e68d9Seric 					syserr("sendall: link(%s, %s)",
2099c9e68d9Seric 						e->e_df, ee->e_df);
2109c9e68d9Seric 				}
2119c9e68d9Seric 			}
2129c9e68d9Seric 
2139c9e68d9Seric 			if (mode != SM_VERIFY)
2149c9e68d9Seric 				openxscript(ee);
2159c9e68d9Seric #ifdef LOG
2169c9e68d9Seric 			if (LogLevel > 4)
2179c9e68d9Seric 				syslog(LOG_INFO, "%s: clone %s",
2189c9e68d9Seric 					ee->e_id, e->e_id);
2199c9e68d9Seric #endif
2209c9e68d9Seric 		}
2219c9e68d9Seric 	}
2229c9e68d9Seric 
2239c9e68d9Seric 	if (owner != NULL)
2249c9e68d9Seric 	{
2259c9e68d9Seric 		setsender(owner, e, NULL, TRUE);
2269c9e68d9Seric 		if (tTd(13, 5))
2279c9e68d9Seric 		{
2289c9e68d9Seric 			printf("sendall(owner): QDONTSEND ");
2299c9e68d9Seric 			printaddr(&e->e_from, FALSE);
2309c9e68d9Seric 		}
2319c9e68d9Seric 		e->e_from.q_flags |= QDONTSEND;
2329c9e68d9Seric 		e->e_errormode = EM_MAIL;
2339c9e68d9Seric 	}
2349c9e68d9Seric 
2359c9e68d9Seric # ifdef QUEUE
2369c9e68d9Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
2379c9e68d9Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
2389c9e68d9Seric 	    !bitset(EF_INQUEUE, e->e_flags))
2399c9e68d9Seric 	{
2409c9e68d9Seric 		/* be sure everything is instantiated in the queue */
2419c9e68d9Seric 		queueup(e, TRUE, mode == SM_QUEUE);
2429c9e68d9Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2439c9e68d9Seric 			queueup(ee, TRUE, mode == SM_QUEUE);
2449c9e68d9Seric 	}
2459c9e68d9Seric #endif /* QUEUE */
2469c9e68d9Seric 
2479c9e68d9Seric 	if (splitenv != NULL)
2489c9e68d9Seric 	{
2499c9e68d9Seric 		if (tTd(13, 1))
2509c9e68d9Seric 		{
2519c9e68d9Seric 			printf("\nsendall: Split queue; remaining queue:\n");
2529c9e68d9Seric 			printaddr(e->e_sendqueue, TRUE);
2539c9e68d9Seric 		}
2549c9e68d9Seric 
2559c9e68d9Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
2569c9e68d9Seric 		{
2579c9e68d9Seric 			CurEnv = ee;
2589c9e68d9Seric 			sendenvelope(ee, mode);
2599c9e68d9Seric 		}
2609c9e68d9Seric 
2619c9e68d9Seric 		CurEnv = e;
2629c9e68d9Seric 	}
2639c9e68d9Seric 	sendenvelope(e, mode);
2649c9e68d9Seric 
2659c9e68d9Seric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
2669c9e68d9Seric 		dropenvelope(splitenv);
2679c9e68d9Seric }
2689c9e68d9Seric 
2699c9e68d9Seric sendenvelope(e, mode)
2709c9e68d9Seric 	register ENVELOPE *e;
2719c9e68d9Seric 	char mode;
2729c9e68d9Seric {
2739c9e68d9Seric 	bool oldverbose;
2749c9e68d9Seric 	int pid;
2759c9e68d9Seric 	register ADDRESS *q;
2769c9e68d9Seric #ifdef LOCKF
2779c9e68d9Seric 	struct flock lfd;
2789c9e68d9Seric #endif
2799c9e68d9Seric 
2809c9e68d9Seric 	oldverbose = Verbose;
2819c9e68d9Seric 	switch (mode)
2829c9e68d9Seric 	{
2839c9e68d9Seric 	  case SM_VERIFY:
2849c9e68d9Seric 		Verbose = TRUE;
2859c9e68d9Seric 		break;
2869c9e68d9Seric 
2879c9e68d9Seric 	  case SM_QUEUE:
2889c9e68d9Seric   queueonly:
2899c9e68d9Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
2909c9e68d9Seric 		return;
2919c9e68d9Seric 
2929c9e68d9Seric 	  case SM_FORK:
2939c9e68d9Seric 		if (e->e_xfp != NULL)
2949c9e68d9Seric 			(void) fflush(e->e_xfp);
2959c9e68d9Seric 
2969c9e68d9Seric # ifdef LOCKF
2979c9e68d9Seric 		/*
2989c9e68d9Seric 		**  Since lockf has the interesting semantic that the
2999c9e68d9Seric 		**  lock is lost when we fork, we have to risk losing
3009c9e68d9Seric 		**  the lock here by closing before the fork, and then
3019c9e68d9Seric 		**  trying to get it back in the child.
3029c9e68d9Seric 		*/
3039c9e68d9Seric 
3049c9e68d9Seric 		if (e->e_lockfp != NULL)
3059c9e68d9Seric 		{
3069c9e68d9Seric 			(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3079c9e68d9Seric 			e->e_lockfp = NULL;
3089c9e68d9Seric 		}
3099c9e68d9Seric # endif /* LOCKF */
3109c9e68d9Seric 
3119c9e68d9Seric 		pid = fork();
3129c9e68d9Seric 		if (pid < 0)
3139c9e68d9Seric 		{
3149c9e68d9Seric 			goto queueonly;
3159c9e68d9Seric 		}
3169c9e68d9Seric 		else if (pid > 0)
3179c9e68d9Seric 		{
3189c9e68d9Seric 			/* be sure we leave the temp files to our child */
3199c9e68d9Seric 			e->e_id = e->e_df = NULL;
3209c9e68d9Seric # ifndef LOCKF
3219c9e68d9Seric 			if (e->e_lockfp != NULL)
3229c9e68d9Seric 			{
3239c9e68d9Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
3249c9e68d9Seric 				e->e_lockfp = NULL;
3259c9e68d9Seric 			}
3269c9e68d9Seric # endif
3279c9e68d9Seric 
3289c9e68d9Seric 			/* close any random open files in the envelope */
3299c9e68d9Seric 			if (e->e_dfp != NULL)
3309c9e68d9Seric 			{
3319c9e68d9Seric 				(void) xfclose(e->e_dfp, "sendenvelope", "dfp");
3329c9e68d9Seric 				e->e_dfp = NULL;
3339c9e68d9Seric 			}
3349c9e68d9Seric 			if (e->e_xfp != NULL)
3359c9e68d9Seric 			{
3369c9e68d9Seric 				(void) xfclose(e->e_xfp, "sendenvelope", "xfp");
3379c9e68d9Seric 				e->e_xfp = NULL;
3389c9e68d9Seric 			}
3399c9e68d9Seric 			return;
3409c9e68d9Seric 		}
3419c9e68d9Seric 
3429c9e68d9Seric 		/* double fork to avoid zombies */
3439c9e68d9Seric 		if (fork() > 0)
3449c9e68d9Seric 			exit(EX_OK);
3459c9e68d9Seric 
3469c9e68d9Seric 		/* be sure we are immune from the terminal */
3479c9e68d9Seric 		disconnect(FALSE, e);
3489c9e68d9Seric 
3499c9e68d9Seric # ifdef LOCKF
3509c9e68d9Seric 		/*
3519c9e68d9Seric 		**  Now try to get our lock back.
3529c9e68d9Seric 		*/
3539c9e68d9Seric 
3549c9e68d9Seric 		lfd.l_type = F_WRLCK;
3559c9e68d9Seric 		lfd.l_whence = lfd.l_start = lfd.l_len = 0;
3569c9e68d9Seric 		e->e_lockfp = fopen(queuename(e, 'q'), "r+");
3579c9e68d9Seric 		if (e->e_lockfp == NULL ||
3589c9e68d9Seric 		    fcntl(fileno(e->e_lockfp), F_SETLK, &lfd) < 0)
3599c9e68d9Seric 		{
3609c9e68d9Seric 			/* oops....  lost it */
3619c9e68d9Seric 			if (tTd(13, 1))
3629c9e68d9Seric 				printf("sendenvelope: %s lost lock: lockfp=%x, %s\n",
3639c9e68d9Seric 					e->e_id, e->e_lockfp, errstring(errno));
3649c9e68d9Seric 
3659c9e68d9Seric # ifdef LOG
3669c9e68d9Seric 			if (LogLevel > 29)
3679c9e68d9Seric 				syslog(LOG_NOTICE, "%s: lost lock: %m",
3689c9e68d9Seric 					e->e_id);
3699c9e68d9Seric # endif /* LOG */
3709c9e68d9Seric 			exit(EX_OK);
3719c9e68d9Seric 		}
3729c9e68d9Seric # endif /* LOCKF */
3739c9e68d9Seric 
3749c9e68d9Seric 		/*
3759c9e68d9Seric 		**  Close any cached connections.
3769c9e68d9Seric 		**
3779c9e68d9Seric 		**	We don't send the QUIT protocol because the parent
3789c9e68d9Seric 		**	still knows about the connection.
3799c9e68d9Seric 		**
3809c9e68d9Seric 		**	This should only happen when delivering an error
3819c9e68d9Seric 		**	message.
3829c9e68d9Seric 		*/
3839c9e68d9Seric 
3849c9e68d9Seric 		mci_flush(FALSE, NULL);
3859c9e68d9Seric 
3869c9e68d9Seric 		break;
3879c9e68d9Seric 	}
3889c9e68d9Seric 
3899c9e68d9Seric 	/*
3909c9e68d9Seric 	**  Run through the list and send everything.
3919c9e68d9Seric 	*/
3929c9e68d9Seric 
3939c9e68d9Seric 	e->e_nsent = 0;
3949c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
3959c9e68d9Seric 	{
3969c9e68d9Seric 		if (mode == SM_VERIFY)
3979c9e68d9Seric 		{
3989c9e68d9Seric 			e->e_to = q->q_paddr;
3999c9e68d9Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4009c9e68d9Seric 				message("deliverable");
4019c9e68d9Seric 		}
4029c9e68d9Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
4039c9e68d9Seric 		{
4049c9e68d9Seric # ifdef QUEUE
4059c9e68d9Seric 			/*
4069c9e68d9Seric 			**  Checkpoint the send list every few addresses
4079c9e68d9Seric 			*/
4089c9e68d9Seric 
4099c9e68d9Seric 			if (e->e_nsent >= CheckpointInterval)
4109c9e68d9Seric 			{
4119c9e68d9Seric 				queueup(e, TRUE, FALSE);
4129c9e68d9Seric 				e->e_nsent = 0;
4139c9e68d9Seric 			}
4149c9e68d9Seric # endif /* QUEUE */
4159c9e68d9Seric 			(void) deliver(e, q);
4169c9e68d9Seric 		}
4179c9e68d9Seric 	}
4189c9e68d9Seric 	Verbose = oldverbose;
4199c9e68d9Seric 
4209c9e68d9Seric 	/*
4219c9e68d9Seric 	**  Now run through and check for errors.
4229c9e68d9Seric 	*/
4239c9e68d9Seric 
4249c9e68d9Seric 	if (mode == SM_VERIFY)
4259c9e68d9Seric 	{
4269c9e68d9Seric 		return;
4279c9e68d9Seric 	}
4289c9e68d9Seric 
4299c9e68d9Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4309c9e68d9Seric 	{
4319c9e68d9Seric 		if (tTd(13, 3))
4329c9e68d9Seric 		{
4339c9e68d9Seric 			printf("Checking ");
4349c9e68d9Seric 			printaddr(q, FALSE);
4359c9e68d9Seric 		}
4369c9e68d9Seric 
4379c9e68d9Seric 		/* only send errors if the message failed */
4389c9e68d9Seric 		if (!bitset(QBADADDR, q->q_flags))
4399c9e68d9Seric 			continue;
4409c9e68d9Seric 
4419c9e68d9Seric 		e->e_flags |= EF_FATALERRS;
4429c9e68d9Seric 
4439c9e68d9Seric 		if (q->q_owner == NULL && strcmp(e->e_from.q_paddr, "<>") != 0)
4449c9e68d9Seric 			(void) sendtolist(e->e_from.q_paddr, NULL,
4459c9e68d9Seric 					  &e->e_errorqueue, e);
4469c9e68d9Seric 	}
4479c9e68d9Seric 
4489c9e68d9Seric 	if (mode == SM_FORK)
4499c9e68d9Seric 		finis();
4509c9e68d9Seric }
4519c9e68d9Seric /*
4529c9e68d9Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
4539c9e68d9Seric **
4549c9e68d9Seric **	This MUST be a macro, since after a vfork we are running
4559c9e68d9Seric **	two processes on the same stack!!!
4569c9e68d9Seric **
4579c9e68d9Seric **	Parameters:
4589c9e68d9Seric **		none.
4599c9e68d9Seric **
4609c9e68d9Seric **	Returns:
4619c9e68d9Seric **		From a macro???  You've got to be kidding!
4629c9e68d9Seric **
4639c9e68d9Seric **	Side Effects:
4649c9e68d9Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
4659c9e68d9Seric **			pid of child in parent, zero in child.
4669c9e68d9Seric **			-1 on unrecoverable error.
4679c9e68d9Seric **
4689c9e68d9Seric **	Notes:
4699c9e68d9Seric **		I'm awfully sorry this looks so awful.  That's
4709c9e68d9Seric **		vfork for you.....
4719c9e68d9Seric */
4729c9e68d9Seric 
4739c9e68d9Seric # define NFORKTRIES	5
4749c9e68d9Seric 
4759c9e68d9Seric # ifndef FORK
4769c9e68d9Seric # define FORK	fork
4779c9e68d9Seric # endif
4789c9e68d9Seric 
4799c9e68d9Seric # define DOFORK(fORKfN) \
4809c9e68d9Seric {\
4819c9e68d9Seric 	register int i;\
4829c9e68d9Seric \
4839c9e68d9Seric 	for (i = NFORKTRIES; --i >= 0; )\
4849c9e68d9Seric 	{\
4859c9e68d9Seric 		pid = fORKfN();\
4869c9e68d9Seric 		if (pid >= 0)\
4879c9e68d9Seric 			break;\
4889c9e68d9Seric 		if (i > 0)\
4899c9e68d9Seric 			sleep((unsigned) NFORKTRIES - i);\
4909c9e68d9Seric 	}\
4919c9e68d9Seric }
4929c9e68d9Seric /*
4939c9e68d9Seric **  DOFORK -- simple fork interface to DOFORK.
4949c9e68d9Seric **
4959c9e68d9Seric **	Parameters:
4969c9e68d9Seric **		none.
4979c9e68d9Seric **
4989c9e68d9Seric **	Returns:
4999c9e68d9Seric **		pid of child in parent.
5009c9e68d9Seric **		zero in child.
5019c9e68d9Seric **		-1 on error.
5029c9e68d9Seric **
5039c9e68d9Seric **	Side Effects:
5049c9e68d9Seric **		returns twice, once in parent and once in child.
5059c9e68d9Seric */
5069c9e68d9Seric 
5079c9e68d9Seric dofork()
5089c9e68d9Seric {
5099c9e68d9Seric 	register int pid;
5109c9e68d9Seric 
5119c9e68d9Seric 	DOFORK(fork);
5129c9e68d9Seric 	return (pid);
5139c9e68d9Seric }
5149c9e68d9Seric /*
51513bbc08cSeric **  DELIVER -- Deliver a message to a list of addresses.
51613bbc08cSeric **
51713bbc08cSeric **	This routine delivers to everyone on the same host as the
51813bbc08cSeric **	user on the head of the list.  It is clever about mailers
51913bbc08cSeric **	that don't handle multiple users.  It is NOT guaranteed
52013bbc08cSeric **	that it will deliver to all these addresses however -- so
52113bbc08cSeric **	deliver should be called once for each address on the
52213bbc08cSeric **	list.
52325a99e2eSeric **
52425a99e2eSeric **	Parameters:
525588cad61Seric **		e -- the envelope to deliver.
526c77d1c25Seric **		firstto -- head of the address list to deliver to.
52725a99e2eSeric **
52825a99e2eSeric **	Returns:
52925a99e2eSeric **		zero -- successfully delivered.
53025a99e2eSeric **		else -- some failure, see ExitStat for more info.
53125a99e2eSeric **
53225a99e2eSeric **	Side Effects:
53325a99e2eSeric **		The standard input is passed off to someone.
53425a99e2eSeric */
53525a99e2eSeric 
536588cad61Seric deliver(e, firstto)
537588cad61Seric 	register ENVELOPE *e;
538c77d1c25Seric 	ADDRESS *firstto;
53925a99e2eSeric {
54078442df3Seric 	char *host;			/* host being sent to */
54178442df3Seric 	char *user;			/* user being sent to */
54225a99e2eSeric 	char **pvp;
5435dfc646bSeric 	register char **mvp;
54425a99e2eSeric 	register char *p;
545588cad61Seric 	register MAILER *m;		/* mailer for this recipient */
5466259796dSeric 	ADDRESS *ctladdr;
547b31e7f2bSeric 	register MCI *mci;
548c77d1c25Seric 	register ADDRESS *to = firstto;
549c579ef51Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
550772e6e50Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
551911693bfSbostic 	int rcode;			/* response code */
552e103b48fSeric 	char *firstsig;			/* signature of firstto */
5539c9e68d9Seric 	int pid;
5549c9e68d9Seric 	char *curhost;
5559c9e68d9Seric 	int mpvect[2];
5569c9e68d9Seric 	int rpvect[2];
557ee6bf8dfSeric 	char *pv[MAXPV+1];
558579ef0ddSeric 	char tobuf[TOBUFSIZE];		/* text line of to people */
559ee6bf8dfSeric 	char buf[MAXNAME];
560c23ed322Seric 	char rpathbuf[MAXNAME];		/* translated return path */
561fabb3bd4Seric 	extern int checkcompat();
562ee6bf8dfSeric 	extern ADDRESS *getctladdr();
563ee6bf8dfSeric 	extern char *remotename();
564e103b48fSeric 	extern char *hostsignature();
5659c9e68d9Seric 	extern FILE *fdopen();
56625a99e2eSeric 
56735490626Seric 	errno = 0;
568ee4b0922Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5695dfc646bSeric 		return (0);
57025a99e2eSeric 
571134746fbSeric #ifdef NAMED_BIND
572912a731aSbostic 	/* unless interactive, try twice, over a minute */
573912a731aSbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
574912a731aSbostic 		_res.retrans = 30;
575912a731aSbostic 		_res.retry = 2;
576912a731aSbostic 	}
577d4bd8f0eSbostic #endif
578912a731aSbostic 
57951552439Seric 	m = to->q_mailer;
58051552439Seric 	host = to->q_host;
581c9be6216Seric 	CurEnv = e;			/* just in case */
58251552439Seric 
5836ef48975Seric 	if (tTd(10, 1))
5845dfc646bSeric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
58551552439Seric 			m->m_mno, host, to->q_user);
586f3dbc832Seric 
587f3dbc832Seric 	/*
588f3dbc832Seric 	**  If this mailer is expensive, and if we don't want to make
589f3dbc832Seric 	**  connections now, just mark these addresses and return.
590f3dbc832Seric 	**	This is useful if we want to batch connections to
591f3dbc832Seric 	**	reduce load.  This will cause the messages to be
592f3dbc832Seric 	**	queued up, and a daemon will come along to send the
593f3dbc832Seric 	**	messages later.
594f3dbc832Seric 	**		This should be on a per-mailer basis.
595f3dbc832Seric 	*/
596f3dbc832Seric 
59719c47125Seric 	if (NoConnect && !bitset(EF_QUEUERUN, e->e_flags) &&
59819c47125Seric 	    bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
599f3dbc832Seric 	{
600f3dbc832Seric 		for (; to != NULL; to = to->q_next)
601f4560e80Seric 		{
602ee4b0922Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
603c6e18ac5Seric 			    to->q_mailer != m)
604f4560e80Seric 				continue;
605f3dbc832Seric 			to->q_flags |= QQUEUEUP|QDONTSEND;
606588cad61Seric 			e->e_to = to->q_paddr;
60708b25121Seric 			message("queued");
6082f624c86Seric 			if (LogLevel > 8)
60981161401Seric 				logdelivery(m, NULL, "queued", e);
610f4560e80Seric 		}
611588cad61Seric 		e->e_to = NULL;
612f3dbc832Seric 		return (0);
613f3dbc832Seric 	}
614f3dbc832Seric 
61525a99e2eSeric 	/*
6165dfc646bSeric 	**  Do initial argv setup.
6175dfc646bSeric 	**	Insert the mailer name.  Notice that $x expansion is
6185dfc646bSeric 	**	NOT done on the mailer name.  Then, if the mailer has
6195dfc646bSeric 	**	a picky -f flag, we insert it as appropriate.  This
6205dfc646bSeric 	**	code does not check for 'pv' overflow; this places a
6215dfc646bSeric 	**	manifest lower limit of 4 for MAXPV.
6223bea8136Seric 	**		The from address rewrite is expected to make
6233bea8136Seric 	**		the address relative to the other end.
6245dfc646bSeric 	*/
6255dfc646bSeric 
62678442df3Seric 	/* rewrite from address, using rewriting rules */
627ee4b0922Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, TRUE, FALSE,
6289c69ca01Seric 					   TRUE, FALSE, e));
629ee4b0922Seric 	define('g', rpathbuf, e);		/* translated return path */
630588cad61Seric 	define('h', host, e);			/* to host */
6315dfc646bSeric 	Errors = 0;
6325dfc646bSeric 	pvp = pv;
6335dfc646bSeric 	*pvp++ = m->m_argv[0];
6345dfc646bSeric 
6355dfc646bSeric 	/* insert -f or -r flag as appropriate */
63657fc6f17Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6375dfc646bSeric 	{
63857fc6f17Seric 		if (bitnset(M_FOPT, m->m_flags))
6395dfc646bSeric 			*pvp++ = "-f";
6405dfc646bSeric 		else
6415dfc646bSeric 			*pvp++ = "-r";
642c23ed322Seric 		*pvp++ = newstr(rpathbuf);
6435dfc646bSeric 	}
6445dfc646bSeric 
6455dfc646bSeric 	/*
6465dfc646bSeric 	**  Append the other fixed parts of the argv.  These run
6475dfc646bSeric 	**  up to the first entry containing "$u".  There can only
6485dfc646bSeric 	**  be one of these, and there are only a few more slots
6495dfc646bSeric 	**  in the pv after it.
6505dfc646bSeric 	*/
6515dfc646bSeric 
6525dfc646bSeric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
6535dfc646bSeric 	{
6542bc47524Seric 		/* can't use strchr here because of sign extension problems */
6552bc47524Seric 		while (*p != '\0')
6562bc47524Seric 		{
6572bc47524Seric 			if ((*p++ & 0377) == MACROEXPAND)
6582bc47524Seric 			{
6592bc47524Seric 				if (*p == 'u')
6605dfc646bSeric 					break;
6612bc47524Seric 			}
6622bc47524Seric 		}
6632bc47524Seric 
6642bc47524Seric 		if (*p != '\0')
6655dfc646bSeric 			break;
6665dfc646bSeric 
6675dfc646bSeric 		/* this entry is safe -- go ahead and process it */
668588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6695dfc646bSeric 		*pvp++ = newstr(buf);
6705dfc646bSeric 		if (pvp >= &pv[MAXPV - 3])
6715dfc646bSeric 		{
67208b25121Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6735dfc646bSeric 			return (-1);
6745dfc646bSeric 		}
6755dfc646bSeric 	}
676c579ef51Seric 
67733db8731Seric 	/*
67833db8731Seric 	**  If we have no substitution for the user name in the argument
67933db8731Seric 	**  list, we know that we must supply the names otherwise -- and
68033db8731Seric 	**  SMTP is the answer!!
68133db8731Seric 	*/
68233db8731Seric 
6835dfc646bSeric 	if (*mvp == NULL)
684c579ef51Seric 	{
685c579ef51Seric 		/* running SMTP */
6862c7e1b8dSeric # ifdef SMTP
687c579ef51Seric 		clever = TRUE;
688c579ef51Seric 		*pvp = NULL;
6896c2c3107Seric # else /* SMTP */
69033db8731Seric 		/* oops!  we don't implement SMTP */
69108b25121Seric 		syserr("554 SMTP style mailer");
6922c7e1b8dSeric 		return (EX_SOFTWARE);
6936c2c3107Seric # endif /* SMTP */
694c579ef51Seric 	}
6955dfc646bSeric 
6965dfc646bSeric 	/*
6975dfc646bSeric 	**  At this point *mvp points to the argument with $u.  We
6985dfc646bSeric 	**  run through our address list and append all the addresses
6995dfc646bSeric 	**  we can.  If we run out of space, do not fret!  We can
7005dfc646bSeric 	**  always send another copy later.
7015dfc646bSeric 	*/
7025dfc646bSeric 
7035dfc646bSeric 	tobuf[0] = '\0';
704588cad61Seric 	e->e_to = tobuf;
7056259796dSeric 	ctladdr = NULL;
706e103b48fSeric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7075dfc646bSeric 	for (; to != NULL; to = to->q_next)
7085dfc646bSeric 	{
7095dfc646bSeric 		/* avoid sending multiple recipients to dumb mailers */
71057fc6f17Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7115dfc646bSeric 			break;
7125dfc646bSeric 
7135dfc646bSeric 		/* if already sent or not for this host, don't send */
714ee4b0922Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
715e103b48fSeric 		    to->q_mailer != firstto->q_mailer ||
716e103b48fSeric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7175dfc646bSeric 			continue;
7186259796dSeric 
7194b22ea87Seric 		/* avoid overflowing tobuf */
720aa50a568Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7214b22ea87Seric 			break;
7224b22ea87Seric 
7236ef48975Seric 		if (tTd(10, 1))
724772e6e50Seric 		{
725772e6e50Seric 			printf("\nsend to ");
726772e6e50Seric 			printaddr(to, FALSE);
727772e6e50Seric 		}
728772e6e50Seric 
7296259796dSeric 		/* compute effective uid/gid when sending */
7307da1035fSeric 		if (to->q_mailer == ProgMailer)
7316259796dSeric 			ctladdr = getctladdr(to);
7326259796dSeric 
7335dfc646bSeric 		user = to->q_user;
734588cad61Seric 		e->e_to = to->q_paddr;
73575f1ade9Seric 		if (tTd(10, 5))
73675f1ade9Seric 		{
73775f1ade9Seric 			printf("deliver: QDONTSEND ");
73875f1ade9Seric 			printaddr(to, FALSE);
73975f1ade9Seric 		}
740ee4b0922Seric 		to->q_flags |= QDONTSEND;
7415dfc646bSeric 
7425dfc646bSeric 		/*
7435dfc646bSeric 		**  Check to see that these people are allowed to
7445dfc646bSeric 		**  talk to each other.
7452a6e0786Seric 		*/
7462a6e0786Seric 
74769582d2fSeric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
74869582d2fSeric 		{
74969582d2fSeric 			NoReturn = TRUE;
75008b25121Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
75181161401Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
75269582d2fSeric 			continue;
75369582d2fSeric 		}
754fabb3bd4Seric 		rcode = checkcompat(to, e);
7551793c9c5Seric 		if (rcode != EX_OK)
7565dfc646bSeric 		{
75781161401Seric 			giveresponse(rcode, m, NULL, e);
7585dfc646bSeric 			continue;
7595dfc646bSeric 		}
7602a6e0786Seric 
7612a6e0786Seric 		/*
7629ec9501bSeric 		**  Strip quote bits from names if the mailer is dumb
7639ec9501bSeric 		**	about them.
76425a99e2eSeric 		*/
76525a99e2eSeric 
76657fc6f17Seric 		if (bitnset(M_STRIPQ, m->m_flags))
76725a99e2eSeric 		{
7681d8f1806Seric 			stripquotes(user);
7691d8f1806Seric 			stripquotes(host);
77025a99e2eSeric 		}
77125a99e2eSeric 
772cdb828c5Seric 		/* hack attack -- delivermail compatibility */
773cdb828c5Seric 		if (m == ProgMailer && *user == '|')
774cdb828c5Seric 			user++;
775cdb828c5Seric 
77625a99e2eSeric 		/*
7773efaed6eSeric 		**  If an error message has already been given, don't
7783efaed6eSeric 		**	bother to send to this address.
7793efaed6eSeric 		**
7803efaed6eSeric 		**	>>>>>>>>>> This clause assumes that the local mailer
7813efaed6eSeric 		**	>> NOTE >> cannot do any further aliasing; that
7823efaed6eSeric 		**	>>>>>>>>>> function is subsumed by sendmail.
7833efaed6eSeric 		*/
7843efaed6eSeric 
7856cae517dSeric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7863efaed6eSeric 			continue;
7873efaed6eSeric 
788f2fec898Seric 		/* save statistics.... */
789588cad61Seric 		markstats(e, to);
790f2fec898Seric 
7913efaed6eSeric 		/*
79225a99e2eSeric 		**  See if this user name is "special".
79325a99e2eSeric 		**	If the user name has a slash in it, assume that this
79451552439Seric 		**	is a file -- send it off without further ado.  Note
79551552439Seric 		**	that this type of addresses is not processed along
79651552439Seric 		**	with the others, so we fudge on the To person.
79725a99e2eSeric 		*/
79825a99e2eSeric 
7992c017f8dSeric 		if (m == FileMailer)
80025a99e2eSeric 		{
801b31e7f2bSeric 			rcode = mailfile(user, getctladdr(to), e);
80281161401Seric 			giveresponse(rcode, m, NULL, e);
803dde5acadSeric 			if (rcode == EX_OK)
804dde5acadSeric 				to->q_flags |= QSENT;
8055dfc646bSeric 			continue;
80625a99e2eSeric 		}
80725a99e2eSeric 
80813bbc08cSeric 		/*
80913bbc08cSeric 		**  Address is verified -- add this user to mailer
81013bbc08cSeric 		**  argv, and add it to the print list of recipients.
81113bbc08cSeric 		*/
81213bbc08cSeric 
813508daeccSeric 		/* link together the chain of recipients */
814508daeccSeric 		to->q_tchain = tochain;
815508daeccSeric 		tochain = to;
816508daeccSeric 
8175dfc646bSeric 		/* create list of users for error messages */
818db8841e9Seric 		(void) strcat(tobuf, ",");
819db8841e9Seric 		(void) strcat(tobuf, to->q_paddr);
820588cad61Seric 		define('u', user, e);		/* to user */
821588cad61Seric 		define('z', to->q_home, e);	/* user's home */
8225dfc646bSeric 
823c579ef51Seric 		/*
824508daeccSeric 		**  Expand out this user into argument list.
825c579ef51Seric 		*/
826c579ef51Seric 
827508daeccSeric 		if (!clever)
828c579ef51Seric 		{
829588cad61Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8305dfc646bSeric 			*pvp++ = newstr(buf);
8315dfc646bSeric 			if (pvp >= &pv[MAXPV - 2])
8325dfc646bSeric 			{
8335dfc646bSeric 				/* allow some space for trailing parms */
8345dfc646bSeric 				break;
8355dfc646bSeric 			}
8365dfc646bSeric 		}
837c579ef51Seric 	}
8385dfc646bSeric 
839145b49b1Seric 	/* see if any addresses still exist */
840145b49b1Seric 	if (tobuf[0] == '\0')
841c579ef51Seric 	{
842588cad61Seric 		define('g', (char *) NULL, e);
843145b49b1Seric 		return (0);
844c579ef51Seric 	}
845145b49b1Seric 
8465dfc646bSeric 	/* print out messages as full list */
84763780dbdSeric 	e->e_to = tobuf + 1;
8485dfc646bSeric 
8495dfc646bSeric 	/*
8505dfc646bSeric 	**  Fill out any parameters after the $u parameter.
8515dfc646bSeric 	*/
8525dfc646bSeric 
853c579ef51Seric 	while (!clever && *++mvp != NULL)
8545dfc646bSeric 	{
855588cad61Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8565dfc646bSeric 		*pvp++ = newstr(buf);
8575dfc646bSeric 		if (pvp >= &pv[MAXPV])
85808b25121Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
8595dfc646bSeric 	}
8605dfc646bSeric 	*pvp++ = NULL;
8615dfc646bSeric 
86225a99e2eSeric 	/*
86325a99e2eSeric 	**  Call the mailer.
8646328bdf7Seric 	**	The argument vector gets built, pipes
86525a99e2eSeric 	**	are created as necessary, and we fork & exec as
8666328bdf7Seric 	**	appropriate.
867c579ef51Seric 	**	If we are running SMTP, we just need to clean up.
86825a99e2eSeric 	*/
86925a99e2eSeric 
8707ee87e5fSeric 	if (ctladdr == NULL && m != ProgMailer)
87186b26461Seric 		ctladdr = &e->e_from;
872134746fbSeric #ifdef NAMED_BIND
8732bcc6d2dSeric 	if (ConfigLevel < 2)
874912a731aSbostic 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
875134746fbSeric #endif
8769c9e68d9Seric 
8779c9e68d9Seric 	if (tTd(11, 1))
878134746fbSeric 	{
8799c9e68d9Seric 		printf("openmailer:");
8809c9e68d9Seric 		printav(pv);
8819c9e68d9Seric 	}
8829c9e68d9Seric 	errno = 0;
8839c9e68d9Seric 
8849c9e68d9Seric 	CurHostName = m->m_mailer;
8859c9e68d9Seric 
8869c9e68d9Seric 	/*
8879c9e68d9Seric 	**  Deal with the special case of mail handled through an IPC
8889c9e68d9Seric 	**  connection.
8899c9e68d9Seric 	**	In this case we don't actually fork.  We must be
8909c9e68d9Seric 	**	running SMTP for this to work.  We will return a
8919c9e68d9Seric 	**	zero pid to indicate that we are running IPC.
8929c9e68d9Seric 	**  We also handle a debug version that just talks to stdin/out.
8939c9e68d9Seric 	*/
8949c9e68d9Seric 
8959c9e68d9Seric 	curhost = NULL;
8969c9e68d9Seric 
8979c9e68d9Seric 	/* check for Local Person Communication -- not for mortals!!! */
8989c9e68d9Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
8999c9e68d9Seric 	{
9009c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
9019c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
9029c9e68d9Seric 		mci->mci_in = stdin;
9039c9e68d9Seric 		mci->mci_out = stdout;
9049c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
9059c9e68d9Seric 		mci->mci_mailer = m;
9069c9e68d9Seric 	}
9079c9e68d9Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
9089c9e68d9Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9099c9e68d9Seric 	{
9109c9e68d9Seric #ifdef DAEMON
9119c9e68d9Seric 		register int i;
9129c9e68d9Seric 		register u_short port;
9139c9e68d9Seric 		extern MCI *mci_get();
9149c9e68d9Seric 		extern char *hostsignature();
9159c9e68d9Seric 
9169c9e68d9Seric 		CurHostName = pv[1];
9179c9e68d9Seric 		curhost = hostsignature(m, pv[1], e);
9189c9e68d9Seric 
9199c9e68d9Seric 		if (curhost == NULL || curhost[0] == '\0')
9209c9e68d9Seric 		{
9219c9e68d9Seric 			syserr("null signature");
922845e533cSeric 			rcode = EX_OSERR;
923b31e7f2bSeric 			goto give_up;
924b31e7f2bSeric 		}
9259c9e68d9Seric 
9269c9e68d9Seric 		if (!clever)
9279c9e68d9Seric 		{
9289c9e68d9Seric 			syserr("554 non-clever IPC");
9299c9e68d9Seric 			rcode = EX_OSERR;
9309c9e68d9Seric 			goto give_up;
9319c9e68d9Seric 		}
9329c9e68d9Seric 		if (pv[2] != NULL)
9339c9e68d9Seric 			port = atoi(pv[2]);
9349c9e68d9Seric 		else
9359c9e68d9Seric 			port = 0;
9369c9e68d9Seric tryhost:
9379c9e68d9Seric 		mci = NULL;
9389c9e68d9Seric 		while (*curhost != '\0')
9399c9e68d9Seric 		{
9409c9e68d9Seric 			register char *p;
9419c9e68d9Seric 			static char hostbuf[MAXNAME];
9429c9e68d9Seric 
9439c9e68d9Seric 			mci = NULL;
9449c9e68d9Seric 
9459c9e68d9Seric 			/* pull the next host from the signature */
9469c9e68d9Seric 			p = strchr(curhost, ':');
9479c9e68d9Seric 			if (p == NULL)
9489c9e68d9Seric 				p = &curhost[strlen(curhost)];
9499c9e68d9Seric 			strncpy(hostbuf, curhost, p - curhost);
9509c9e68d9Seric 			hostbuf[p - curhost] = '\0';
9519c9e68d9Seric 			if (*p != '\0')
9529c9e68d9Seric 				p++;
9539c9e68d9Seric 			curhost = p;
9549c9e68d9Seric 
9559c9e68d9Seric 			/* see if we already know that this host is fried */
9569c9e68d9Seric 			CurHostName = hostbuf;
9579c9e68d9Seric 			mci = mci_get(hostbuf, m);
9589c9e68d9Seric 			if (mci->mci_state != MCIS_CLOSED)
9599c9e68d9Seric 			{
9609c9e68d9Seric 				if (tTd(11, 1))
9619c9e68d9Seric 				{
9629c9e68d9Seric 					printf("openmailer: ");
9639c9e68d9Seric 					mci_dump(mci);
9649c9e68d9Seric 				}
9659c9e68d9Seric 				CurHostName = mci->mci_host;
9669c9e68d9Seric 				break;
9679c9e68d9Seric 			}
9689c9e68d9Seric 			mci->mci_mailer = m;
9699c9e68d9Seric 			if (mci->mci_exitstat != EX_OK)
9709c9e68d9Seric 				continue;
9719c9e68d9Seric 
9729c9e68d9Seric 			/* try the connection */
9739c9e68d9Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
9749c9e68d9Seric 			message("Connecting to %s (%s)...",
9759c9e68d9Seric 				hostbuf, m->m_name);
9769c9e68d9Seric 			i = makeconnection(hostbuf, port, mci,
9779c9e68d9Seric 				bitnset(M_SECURE_PORT, m->m_flags));
9789c9e68d9Seric 			mci->mci_exitstat = i;
9799c9e68d9Seric 			mci->mci_errno = errno;
9809c9e68d9Seric 			if (i == EX_OK)
9819c9e68d9Seric 			{
9829c9e68d9Seric 				mci->mci_state = MCIS_OPENING;
9839c9e68d9Seric 				mci_cache(mci);
9849c9e68d9Seric 				break;
9859c9e68d9Seric 			}
9869c9e68d9Seric 			else if (tTd(11, 1))
9879c9e68d9Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
9889c9e68d9Seric 					i, errno);
9899c9e68d9Seric 
9909c9e68d9Seric 
9919c9e68d9Seric 			/* enter status of this host */
9929c9e68d9Seric 			setstat(i);
9939c9e68d9Seric 		}
9949c9e68d9Seric 		mci->mci_pid = 0;
9959c9e68d9Seric #else /* no DAEMON */
9969c9e68d9Seric 		syserr("554 openmailer: no IPC");
9979c9e68d9Seric 		if (tTd(11, 1))
9989c9e68d9Seric 			printf("openmailer: NULL\n");
9999c9e68d9Seric 		return NULL;
10009c9e68d9Seric #endif /* DAEMON */
10019c9e68d9Seric 	}
10029c9e68d9Seric 	else
10039c9e68d9Seric 	{
1004*6fe8c3bcSeric 		int i;
1005*6fe8c3bcSeric 		struct stat stbuf;
1006*6fe8c3bcSeric 
1007*6fe8c3bcSeric 		/* make absolutely certain 0, 1, and 2 are in use */
1008*6fe8c3bcSeric 		for (i = 0; i < 3; i++)
1009*6fe8c3bcSeric 		{
1010*6fe8c3bcSeric 			if (fstat(i, &stbuf) < 0)
1011*6fe8c3bcSeric 			{
1012*6fe8c3bcSeric 				/* oops.... */
1013*6fe8c3bcSeric 				syserr("openmailer: fd %d not open", i);
1014*6fe8c3bcSeric 				(void) open("/dev/null", O_RDONLY, 0666);
1015*6fe8c3bcSeric 			}
1016*6fe8c3bcSeric 		}
1017*6fe8c3bcSeric 
10189c9e68d9Seric 		/* create a pipe to shove the mail through */
10199c9e68d9Seric 		if (pipe(mpvect) < 0)
10209c9e68d9Seric 		{
10219c9e68d9Seric 			syserr("openmailer: pipe (to mailer)");
10229c9e68d9Seric 			if (tTd(11, 1))
10239c9e68d9Seric 				printf("openmailer: NULL\n");
10249c9e68d9Seric 			rcode = EX_OSERR;
10259c9e68d9Seric 			goto give_up;
10269c9e68d9Seric 		}
10279c9e68d9Seric 
10289c9e68d9Seric 		/* if this mailer speaks smtp, create a return pipe */
10299c9e68d9Seric 		if (clever && pipe(rpvect) < 0)
10309c9e68d9Seric 		{
10319c9e68d9Seric 			syserr("openmailer: pipe (from mailer)");
10329c9e68d9Seric 			(void) close(mpvect[0]);
10339c9e68d9Seric 			(void) close(mpvect[1]);
10349c9e68d9Seric 			if (tTd(11, 1))
10359c9e68d9Seric 				printf("openmailer: NULL\n");
10369c9e68d9Seric 			rcode = EX_OSERR;
10379c9e68d9Seric 			goto give_up;
10389c9e68d9Seric 		}
10399c9e68d9Seric 
10409c9e68d9Seric 		/*
10419c9e68d9Seric 		**  Actually fork the mailer process.
10429c9e68d9Seric 		**	DOFORK is clever about retrying.
10439c9e68d9Seric 		**
10449c9e68d9Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
10459c9e68d9Seric 		**	around so that endmail will get it.
10469c9e68d9Seric 		*/
10479c9e68d9Seric 
10489c9e68d9Seric 		if (e->e_xfp != NULL)
10499c9e68d9Seric 			(void) fflush(e->e_xfp);		/* for debugging */
10509c9e68d9Seric 		(void) fflush(stdout);
10519c9e68d9Seric # ifdef SIGCHLD
10529c9e68d9Seric 		(void) signal(SIGCHLD, SIG_DFL);
10539c9e68d9Seric # endif /* SIGCHLD */
10549c9e68d9Seric 		DOFORK(FORK);
10559c9e68d9Seric 		/* pid is set by DOFORK */
10569c9e68d9Seric 		if (pid < 0)
10579c9e68d9Seric 		{
10589c9e68d9Seric 			/* failure */
10599c9e68d9Seric 			syserr("openmailer: cannot fork");
10609c9e68d9Seric 			(void) close(mpvect[0]);
10619c9e68d9Seric 			(void) close(mpvect[1]);
10629c9e68d9Seric 			if (clever)
10639c9e68d9Seric 			{
10649c9e68d9Seric 				(void) close(rpvect[0]);
10659c9e68d9Seric 				(void) close(rpvect[1]);
10669c9e68d9Seric 			}
10679c9e68d9Seric 			if (tTd(11, 1))
10689c9e68d9Seric 				printf("openmailer: NULL\n");
10699c9e68d9Seric 			rcode = EX_OSERR;
10709c9e68d9Seric 			goto give_up;
10719c9e68d9Seric 		}
10729c9e68d9Seric 		else if (pid == 0)
10739c9e68d9Seric 		{
10749c9e68d9Seric 			int i;
10759c9e68d9Seric 			int saveerrno;
10769c9e68d9Seric 			char **ep;
10779c9e68d9Seric 			char *env[MAXUSERENVIRON];
10789c9e68d9Seric 			extern char **environ;
10799c9e68d9Seric 			extern int DtableSize;
10809c9e68d9Seric 
10819c9e68d9Seric 			/* child -- set up input & exec mailer */
10829c9e68d9Seric 			/* make diagnostic output be standard output */
10839c9e68d9Seric 			(void) signal(SIGINT, SIG_IGN);
10849c9e68d9Seric 			(void) signal(SIGHUP, SIG_IGN);
10859c9e68d9Seric 			(void) signal(SIGTERM, SIG_DFL);
10869c9e68d9Seric 
10879c9e68d9Seric 			/* close any other cached connections */
10889c9e68d9Seric 			mci_flush(FALSE, mci);
10899c9e68d9Seric 
10909c9e68d9Seric 			/* arrange to filter std & diag output of command */
10919c9e68d9Seric 			if (clever)
10929c9e68d9Seric 			{
10939c9e68d9Seric 				(void) close(rpvect[0]);
1094*6fe8c3bcSeric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
1095*6fe8c3bcSeric 				{
1096*6fe8c3bcSeric 					syserr("Cannot dup pipe %d for stdout",
1097*6fe8c3bcSeric 						rpvect[1]);
1098*6fe8c3bcSeric 					_exit(EX_OSERR);
1099*6fe8c3bcSeric 				}
11009c9e68d9Seric 				(void) close(rpvect[1]);
11019c9e68d9Seric 			}
11029c9e68d9Seric 			else if (OpMode == MD_SMTP || HoldErrs)
11039c9e68d9Seric 			{
11049c9e68d9Seric 				/* put mailer output in transcript */
1105*6fe8c3bcSeric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
1106*6fe8c3bcSeric 				{
1107*6fe8c3bcSeric 					syserr("Cannot dup xscript %d for stdout",
1108*6fe8c3bcSeric 						fileno(e->e_xfp));
1109*6fe8c3bcSeric 					_exit(EX_OSERR);
11109c9e68d9Seric 				}
1111*6fe8c3bcSeric 			}
1112*6fe8c3bcSeric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
1113*6fe8c3bcSeric 			{
1114*6fe8c3bcSeric 				syserr("Cannot dup stdout for stderr");
1115*6fe8c3bcSeric 				_exit(EX_OSERR);
1116*6fe8c3bcSeric 			}
11179c9e68d9Seric 
11189c9e68d9Seric 			/* arrange to get standard input */
11199c9e68d9Seric 			(void) close(mpvect[1]);
11209c9e68d9Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11219c9e68d9Seric 			{
1122*6fe8c3bcSeric 				syserr("Cannot dup pipe %d for stdin",
1123*6fe8c3bcSeric 					mpvect[0]);
11249c9e68d9Seric 				_exit(EX_OSERR);
11259c9e68d9Seric 			}
11269c9e68d9Seric 			(void) close(mpvect[0]);
11279c9e68d9Seric 			if (!bitnset(M_RESTR, m->m_flags))
11289c9e68d9Seric 			{
11299c9e68d9Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
11309c9e68d9Seric 				{
11319c9e68d9Seric 					(void) setgid(DefGid);
11329c9e68d9Seric 					(void) initgroups(DefUser, DefGid);
11339c9e68d9Seric 					(void) setuid(DefUid);
11349c9e68d9Seric 				}
11359c9e68d9Seric 				else
11369c9e68d9Seric 				{
11379c9e68d9Seric 					(void) setgid(ctladdr->q_gid);
11389c9e68d9Seric 					(void) initgroups(ctladdr->q_ruser?
11399c9e68d9Seric 						ctladdr->q_ruser: ctladdr->q_user,
11409c9e68d9Seric 						ctladdr->q_gid);
11419c9e68d9Seric 					(void) setuid(ctladdr->q_uid);
11429c9e68d9Seric 				}
11439c9e68d9Seric 			}
11449c9e68d9Seric 
11459c9e68d9Seric 			/* arrange for all the files to be closed */
11469c9e68d9Seric 			for (i = 3; i < DtableSize; i++)
11479c9e68d9Seric 			{
11489c9e68d9Seric 				register int j;
11499c9e68d9Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
11509c9e68d9Seric 					(void)fcntl(i, F_SETFD, j|1);
11519c9e68d9Seric 			}
11529c9e68d9Seric 
11539c9e68d9Seric 			/* set up the mailer environment */
11549c9e68d9Seric 			i = 0;
11559c9e68d9Seric 			env[i++] = "AGENT=sendmail";
11569c9e68d9Seric 			for (ep = environ; *ep != NULL; ep++)
11579c9e68d9Seric 			{
11589c9e68d9Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
11599c9e68d9Seric 					env[i++] = *ep;
11609c9e68d9Seric 			}
11619c9e68d9Seric 			env[i++] = NULL;
11629c9e68d9Seric 
11639c9e68d9Seric 			/* try to execute the mailer */
11649c9e68d9Seric 			execve(m->m_mailer, pv, env);
11659c9e68d9Seric 			saveerrno = errno;
11669c9e68d9Seric 			syserr("Cannot exec %s", m->m_mailer);
11679c9e68d9Seric 			if (m == LocalMailer)
11689c9e68d9Seric 				_exit(EX_TEMPFAIL);
11699c9e68d9Seric 			if (transienterror(saveerrno))
11709c9e68d9Seric 				_exit(EX_TEMPFAIL);
11719c9e68d9Seric 			_exit(EX_UNAVAILABLE);
11729c9e68d9Seric 		}
11739c9e68d9Seric 
11749c9e68d9Seric 		/*
11759c9e68d9Seric 		**  Set up return value.
11769c9e68d9Seric 		*/
11779c9e68d9Seric 
11789c9e68d9Seric 		mci = (MCI *) xalloc(sizeof *mci);
11799c9e68d9Seric 		bzero((char *) mci, sizeof *mci);
11809c9e68d9Seric 		mci->mci_mailer = m;
11819c9e68d9Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
11829c9e68d9Seric 		mci->mci_pid = pid;
11839c9e68d9Seric 		(void) close(mpvect[0]);
11849c9e68d9Seric 		mci->mci_out = fdopen(mpvect[1], "w");
11859c9e68d9Seric 		if (clever)
11869c9e68d9Seric 		{
11879c9e68d9Seric 			(void) close(rpvect[1]);
11889c9e68d9Seric 			mci->mci_in = fdopen(rpvect[0], "r");
11899c9e68d9Seric 		}
11909c9e68d9Seric 		else
11919c9e68d9Seric 		{
11929c9e68d9Seric 			mci->mci_flags |= MCIF_TEMP;
11939c9e68d9Seric 			mci->mci_in = NULL;
11949c9e68d9Seric 		}
11959c9e68d9Seric 	}
11969c9e68d9Seric 
11979c9e68d9Seric 	/*
11989c9e68d9Seric 	**  If we are in SMTP opening state, send initial protocol.
11999c9e68d9Seric 	*/
12009c9e68d9Seric 
12019c9e68d9Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12029c9e68d9Seric 	{
12039c9e68d9Seric 		smtpinit(m, mci, e);
12049c9e68d9Seric 	}
12059c9e68d9Seric 	if (tTd(11, 1))
12069c9e68d9Seric 	{
12079c9e68d9Seric 		printf("openmailer: ");
12089c9e68d9Seric 		mci_dump(mci);
12099c9e68d9Seric 	}
12109c9e68d9Seric 
12119c9e68d9Seric 	if (mci->mci_state != MCIS_OPEN)
1212b31e7f2bSeric 	{
1213b31e7f2bSeric 		/* couldn't open the mailer */
1214b31e7f2bSeric 		rcode = mci->mci_exitstat;
12152a6bc25bSeric 		errno = mci->mci_errno;
1216b31e7f2bSeric 		if (rcode == EX_OK)
1217b31e7f2bSeric 		{
1218b31e7f2bSeric 			/* shouldn't happen */
121908b25121Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
12206b0f339dSeric 				rcode, mci->mci_state, firstsig);
1221b31e7f2bSeric 			rcode = EX_SOFTWARE;
1222b31e7f2bSeric 		}
1223b31e7f2bSeric 	}
1224b31e7f2bSeric 	else if (!clever)
1225b31e7f2bSeric 	{
1226b31e7f2bSeric 		/*
1227b31e7f2bSeric 		**  Format and send message.
1228b31e7f2bSeric 		*/
122915d084d5Seric 
1230b31e7f2bSeric 		putfromline(mci->mci_out, m, e);
1231b31e7f2bSeric 		(*e->e_puthdr)(mci->mci_out, m, e);
1232b31e7f2bSeric 		putline("\n", mci->mci_out, m);
1233b31e7f2bSeric 		(*e->e_putbody)(mci->mci_out, m, e);
1234b31e7f2bSeric 
1235b31e7f2bSeric 		/* get the exit status */
1236c9be6216Seric 		rcode = endmailer(mci, e, pv);
1237134746fbSeric 	}
1238134746fbSeric 	else
1239b31e7f2bSeric #ifdef SMTP
1240134746fbSeric 	{
1241b31e7f2bSeric 		/*
1242b31e7f2bSeric 		**  Send the MAIL FROM: protocol
1243b31e7f2bSeric 		*/
124415d084d5Seric 
1245b31e7f2bSeric 		rcode = smtpmailfrom(m, mci, e);
1246b31e7f2bSeric 		if (rcode == EX_OK)
124775889e88Seric 		{
1248ded0d3daSkarels 			register char *t = tobuf;
1249ded0d3daSkarels 			register int i;
1250ded0d3daSkarels 
1251588cad61Seric 			/* send the recipient list */
125263780dbdSeric 			tobuf[0] = '\0';
125375889e88Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
125475889e88Seric 			{
125563780dbdSeric 				e->e_to = to->q_paddr;
125615d084d5Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
125775889e88Seric 				{
125883b7ddc9Seric 					markfailure(e, to, i);
125981161401Seric 					giveresponse(i, m, mci, e);
126063780dbdSeric 				}
126175889e88Seric 				else
126275889e88Seric 				{
1263911693bfSbostic 					*t++ = ',';
1264b31e7f2bSeric 					for (p = to->q_paddr; *p; *t++ = *p++)
1265b31e7f2bSeric 						continue;
1266588cad61Seric 				}
1267588cad61Seric 			}
1268588cad61Seric 
126963780dbdSeric 			/* now send the data */
127063780dbdSeric 			if (tobuf[0] == '\0')
1271b31e7f2bSeric 			{
12729c9e68d9Seric 				rcode = EX_OK;
127363780dbdSeric 				e->e_to = NULL;
1274b31e7f2bSeric 				if (bitset(MCIF_CACHED, mci->mci_flags))
1275b31e7f2bSeric 					smtprset(m, mci, e);
1276b31e7f2bSeric 			}
127775889e88Seric 			else
127875889e88Seric 			{
127963780dbdSeric 				e->e_to = tobuf + 1;
128075889e88Seric 				rcode = smtpdata(m, mci, e);
128163780dbdSeric 			}
128263780dbdSeric 
128363780dbdSeric 			/* now close the connection */
1284b31e7f2bSeric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
128515d084d5Seric 				smtpquit(m, mci, e);
128663780dbdSeric 		}
12879c9e68d9Seric 		if (rcode != EX_OK && *curhost != '\0')
12889c9e68d9Seric 		{
12899c9e68d9Seric 			/* try next MX site */
12909c9e68d9Seric 			goto tryhost;
12919c9e68d9Seric 		}
1292c579ef51Seric 	}
1293b31e7f2bSeric #else /* not SMTP */
1294a05b3449Sbostic 	{
129508b25121Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
1296845e533cSeric 		rcode = EX_CONFIG;
1297b31e7f2bSeric 		goto give_up;
1298a05b3449Sbostic 	}
1299b31e7f2bSeric #endif /* SMTP */
1300134746fbSeric #ifdef NAMED_BIND
13012bcc6d2dSeric 	if (ConfigLevel < 2)
1302912a731aSbostic 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
1303134746fbSeric #endif
13045dfc646bSeric 
1305b31e7f2bSeric 	/* arrange a return receipt if requested */
13068c13bf07Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
1307b31e7f2bSeric 	{
1308b31e7f2bSeric 		e->e_flags |= EF_SENDRECEIPT;
1309b31e7f2bSeric 		/* do we want to send back more info? */
1310b31e7f2bSeric 	}
1311b31e7f2bSeric 
1312c77d1c25Seric 	/*
131363780dbdSeric 	**  Do final status disposal.
131463780dbdSeric 	**	We check for something in tobuf for the SMTP case.
1315c77d1c25Seric 	**	If we got a temporary failure, arrange to queue the
1316c77d1c25Seric 	**		addressees.
1317c77d1c25Seric 	*/
1318c77d1c25Seric 
1319b31e7f2bSeric   give_up:
132063780dbdSeric 	if (tobuf[0] != '\0')
132181161401Seric 		giveresponse(rcode, m, mci, e);
1322772e6e50Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
1323b31e7f2bSeric 	{
1324dde5acadSeric 		if (rcode != EX_OK)
132583b7ddc9Seric 			markfailure(e, to, rcode);
1326dde5acadSeric 		else
1327655518ecSeric 		{
1328dde5acadSeric 			to->q_flags |= QSENT;
1329655518ecSeric 			e->e_nsent++;
1330655518ecSeric 		}
1331b31e7f2bSeric 	}
1332b31e7f2bSeric 
1333b31e7f2bSeric 	/*
1334b31e7f2bSeric 	**  Restore state and return.
1335b31e7f2bSeric 	*/
1336c77d1c25Seric 
133735490626Seric 	errno = 0;
1338588cad61Seric 	define('g', (char *) NULL, e);
13395826d9d3Seric 	return (rcode);
134025a99e2eSeric }
13415dfc646bSeric /*
134283b7ddc9Seric **  MARKFAILURE -- mark a failure on a specific address.
134383b7ddc9Seric **
134483b7ddc9Seric **	Parameters:
134583b7ddc9Seric **		e -- the envelope we are sending.
134683b7ddc9Seric **		q -- the address to mark.
134783b7ddc9Seric **		rcode -- the code signifying the particular failure.
134883b7ddc9Seric **
134983b7ddc9Seric **	Returns:
135083b7ddc9Seric **		none.
135183b7ddc9Seric **
135283b7ddc9Seric **	Side Effects:
135383b7ddc9Seric **		marks the address (and possibly the envelope) with the
135483b7ddc9Seric **			failure so that an error will be returned or
135583b7ddc9Seric **			the message will be queued, as appropriate.
135683b7ddc9Seric */
135783b7ddc9Seric 
135883b7ddc9Seric markfailure(e, q, rcode)
135983b7ddc9Seric 	register ENVELOPE *e;
136083b7ddc9Seric 	register ADDRESS *q;
136183b7ddc9Seric 	int rcode;
136283b7ddc9Seric {
136319c47125Seric 	char buf[MAXLINE];
136419c47125Seric 	extern char *pintvl();
136519c47125Seric 
136683b7ddc9Seric 	if (rcode == EX_OK)
136783b7ddc9Seric 		return;
1368ef137175Sbostic 	else if (rcode != EX_TEMPFAIL && rcode != EX_IOERR && rcode != EX_OSERR)
136983b7ddc9Seric 		q->q_flags |= QBADADDR;
137019c47125Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
137183b7ddc9Seric 	{
137283b7ddc9Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
1373198d9be0Seric 		{
1374198d9be0Seric 			(void) sprintf(buf, "Cannot send message for %s",
137519c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
1376198d9be0Seric 			if (e->e_message != NULL)
1377198d9be0Seric 				free(e->e_message);
1378198d9be0Seric 			e->e_message = newstr(buf);
137908b25121Seric 			message(buf);
1380198d9be0Seric 		}
138183b7ddc9Seric 		q->q_flags |= QBADADDR;
138283b7ddc9Seric 		e->e_flags |= EF_TIMEOUT;
1383c13b5dfcSeric 		fprintf(e->e_xfp, "421 %s... Message timed out\n", q->q_paddr);
138483b7ddc9Seric 	}
138583b7ddc9Seric 	else
138619c47125Seric 	{
138783b7ddc9Seric 		q->q_flags |= QQUEUEUP;
138819c47125Seric 		if (TimeOuts.to_q_warning > 0 &&
138919c47125Seric 		    curtime() > e->e_ctime + TimeOuts.to_q_warning)
139019c47125Seric 		{
139119c47125Seric 			if (!bitset(EF_WARNING, e->e_flags) &&
139219c47125Seric 			    e->e_class >= 0)
139319c47125Seric 			{
139419c47125Seric 				(void) sprintf(buf,
139519c47125Seric 					"warning: cannot send message for %s",
139619c47125Seric 					pintvl(TimeOuts.to_q_warning, FALSE));
139719c47125Seric 				if (e->e_message != NULL)
139819c47125Seric 					free(e->e_message);
139919c47125Seric 				e->e_message = newstr(buf);
140019c47125Seric 				message(buf);
140119c47125Seric 				e->e_flags |= EF_WARNING|EF_TIMEOUT;
140219c47125Seric 			}
140319c47125Seric 			fprintf(e->e_xfp,
140419c47125Seric 				"%s... Warning: message still undelivered after %s\n",
140519c47125Seric 				q->q_paddr, pintvl(TimeOuts.to_q_warning, FALSE));
140619c47125Seric 			fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
140719c47125Seric 				pintvl(TimeOuts.to_q_return, FALSE));
140819c47125Seric 		}
140919c47125Seric 	}
141083b7ddc9Seric }
141183b7ddc9Seric /*
1412c579ef51Seric **  ENDMAILER -- Wait for mailer to terminate.
1413c579ef51Seric **
1414c579ef51Seric **	We should never get fatal errors (e.g., segmentation
1415c579ef51Seric **	violation), so we report those specially.  For other
1416c579ef51Seric **	errors, we choose a status message (into statmsg),
1417c579ef51Seric **	and if it represents an error, we print it.
1418c579ef51Seric **
1419c579ef51Seric **	Parameters:
1420c579ef51Seric **		pid -- pid of mailer.
1421c9be6216Seric **		e -- the current envelope.
1422c9be6216Seric **		pv -- the parameter vector that invoked the mailer
1423c9be6216Seric **			(for error messages).
1424c579ef51Seric **
1425c579ef51Seric **	Returns:
1426c579ef51Seric **		exit code of mailer.
1427c579ef51Seric **
1428c579ef51Seric **	Side Effects:
1429c579ef51Seric **		none.
1430c579ef51Seric */
1431c579ef51Seric 
1432c9be6216Seric endmailer(mci, e, pv)
1433b31e7f2bSeric 	register MCI *mci;
1434c9be6216Seric 	register ENVELOPE *e;
1435c9be6216Seric 	char **pv;
1436c579ef51Seric {
1437588cad61Seric 	int st;
1438c579ef51Seric 
143975889e88Seric 	/* close any connections */
144075889e88Seric 	if (mci->mci_in != NULL)
1441c9be6216Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
144275889e88Seric 	if (mci->mci_out != NULL)
1443c9be6216Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
144475889e88Seric 	mci->mci_in = mci->mci_out = NULL;
144575889e88Seric 	mci->mci_state = MCIS_CLOSED;
144675889e88Seric 
144733db8731Seric 	/* in the IPC case there is nothing to wait for */
144875889e88Seric 	if (mci->mci_pid == 0)
144933db8731Seric 		return (EX_OK);
145033db8731Seric 
145133db8731Seric 	/* wait for the mailer process to die and collect status */
145275889e88Seric 	st = waitfor(mci->mci_pid);
1453588cad61Seric 	if (st == -1)
145478de67c1Seric 	{
1455c9be6216Seric 		syserr("endmailer %s: wait", pv[0]);
1456588cad61Seric 		return (EX_SOFTWARE);
1457c579ef51Seric 	}
145833db8731Seric 
145933db8731Seric 	/* see if it died a horrid death */
1460c579ef51Seric 	if ((st & 0377) != 0)
1461c579ef51Seric 	{
1462c9be6216Seric 		syserr("mailer %s died with signal %o", pv[0], st);
1463c9be6216Seric 
1464c9be6216Seric 		/* log the arguments */
1465c9be6216Seric 		if (e->e_xfp != NULL)
1466c9be6216Seric 		{
1467c9be6216Seric 			register char **av;
1468c9be6216Seric 
1469c9be6216Seric 			fprintf(e->e_xfp, "Arguments:");
1470c9be6216Seric 			for (av = pv; *av != NULL; av++)
1471c9be6216Seric 				fprintf(e->e_xfp, " %s", *av);
1472c9be6216Seric 			fprintf(e->e_xfp, "\n");
1473c9be6216Seric 		}
1474c9be6216Seric 
14755f73204aSeric 		ExitStat = EX_TEMPFAIL;
14765f73204aSeric 		return (EX_TEMPFAIL);
1477c579ef51Seric 	}
147833db8731Seric 
147933db8731Seric 	/* normal death -- return status */
1480588cad61Seric 	st = (st >> 8) & 0377;
1481588cad61Seric 	return (st);
1482c579ef51Seric }
1483c579ef51Seric /*
148425a99e2eSeric **  GIVERESPONSE -- Interpret an error response from a mailer
148525a99e2eSeric **
148625a99e2eSeric **	Parameters:
148725a99e2eSeric **		stat -- the status code from the mailer (high byte
148825a99e2eSeric **			only; core dumps must have been taken care of
148925a99e2eSeric **			already).
149081161401Seric **		m -- the mailer info for this mailer.
149181161401Seric **		mci -- the mailer connection info -- can be NULL if the
149281161401Seric **			response is given before the connection is made.
149381161401Seric **		e -- the current envelope.
149425a99e2eSeric **
149525a99e2eSeric **	Returns:
1496db8841e9Seric **		none.
149725a99e2eSeric **
149825a99e2eSeric **	Side Effects:
1499c1f9df2cSeric **		Errors may be incremented.
150025a99e2eSeric **		ExitStat may be set.
150125a99e2eSeric */
150225a99e2eSeric 
150381161401Seric giveresponse(stat, m, mci, e)
150425a99e2eSeric 	int stat;
1505588cad61Seric 	register MAILER *m;
150681161401Seric 	register MCI *mci;
1507198d9be0Seric 	ENVELOPE *e;
150825a99e2eSeric {
150925a99e2eSeric 	register char *statmsg;
151025a99e2eSeric 	extern char *SysExMsg[];
151125a99e2eSeric 	register int i;
1512d4bd8f0eSbostic 	extern int N_SysEx;
1513d4bd8f0eSbostic #ifdef NAMED_BIND
1514d4bd8f0eSbostic 	extern int h_errno;
1515d4bd8f0eSbostic #endif
1516198d9be0Seric 	char buf[MAXLINE];
15177d55540cSeric 	extern char *errstring();
151825a99e2eSeric 
151913bbc08cSeric 	/*
152013bbc08cSeric 	**  Compute status message from code.
152113bbc08cSeric 	*/
152213bbc08cSeric 
152325a99e2eSeric 	i = stat - EX__BASE;
1524588cad61Seric 	if (stat == 0)
1525*6fe8c3bcSeric 	{
1526588cad61Seric 		statmsg = "250 Sent";
1527*6fe8c3bcSeric 		if (e->e_message != NULL)
1528*6fe8c3bcSeric 		{
1529*6fe8c3bcSeric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_message);
1530*6fe8c3bcSeric 			statmsg = buf;
1531*6fe8c3bcSeric 		}
1532*6fe8c3bcSeric 	}
1533588cad61Seric 	else if (i < 0 || i > N_SysEx)
1534588cad61Seric 	{
1535588cad61Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
1536588cad61Seric 		stat = EX_UNAVAILABLE;
1537588cad61Seric 		statmsg = buf;
1538588cad61Seric 	}
1539198d9be0Seric 	else if (stat == EX_TEMPFAIL)
1540198d9be0Seric 	{
15417d55540cSeric 		(void) strcpy(buf, SysExMsg[i] + 1);
1542d4bd8f0eSbostic #ifdef NAMED_BIND
1543f28da541Smiriam 		if (h_errno == TRY_AGAIN)
1544f28da541Smiriam 			statmsg = errstring(h_errno+MAX_ERRNO);
1545f28da541Smiriam 		else
1546d4bd8f0eSbostic #endif
1547f28da541Smiriam 		{
15488557d168Seric 			if (errno != 0)
1549d87e85f3Seric 				statmsg = errstring(errno);
1550d87e85f3Seric 			else
1551d87e85f3Seric 			{
1552d87e85f3Seric #ifdef SMTP
1553d87e85f3Seric 				extern char SmtpError[];
1554d87e85f3Seric 
1555d87e85f3Seric 				statmsg = SmtpError;
15566c2c3107Seric #else /* SMTP */
1557d87e85f3Seric 				statmsg = NULL;
15586c2c3107Seric #endif /* SMTP */
1559d87e85f3Seric 			}
1560f28da541Smiriam 		}
1561d87e85f3Seric 		if (statmsg != NULL && statmsg[0] != '\0')
1562d87e85f3Seric 		{
156387c9b3e7Seric 			(void) strcat(buf, ": ");
1564d87e85f3Seric 			(void) strcat(buf, statmsg);
15658557d168Seric 		}
1566198d9be0Seric 		statmsg = buf;
1567198d9be0Seric 	}
156825a99e2eSeric 	else
1569d87e85f3Seric 	{
157025a99e2eSeric 		statmsg = SysExMsg[i];
15717d55540cSeric 		if (*statmsg++ == ':')
15727d55540cSeric 		{
15737d55540cSeric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
15747d55540cSeric 			statmsg = buf;
15757d55540cSeric 		}
1576d87e85f3Seric 	}
1577588cad61Seric 
1578588cad61Seric 	/*
1579588cad61Seric 	**  Print the message as appropriate
1580588cad61Seric 	*/
1581588cad61Seric 
1582198d9be0Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
1583e12d03eeSeric 		message(&statmsg[4], errstring(errno));
158425a99e2eSeric 	else
158525a99e2eSeric 	{
1586c1f9df2cSeric 		Errors++;
1587e12d03eeSeric 		usrerr(statmsg, errstring(errno));
158825a99e2eSeric 	}
158925a99e2eSeric 
159025a99e2eSeric 	/*
159125a99e2eSeric 	**  Final cleanup.
159225a99e2eSeric 	**	Log a record of the transaction.  Compute the new
159325a99e2eSeric 	**	ExitStat -- if we already had an error, stick with
159425a99e2eSeric 	**	that.
159525a99e2eSeric 	*/
159625a99e2eSeric 
15972f624c86Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
159881161401Seric 		logdelivery(m, mci, &statmsg[4], e);
1599eb238f8cSeric 
1600eb238f8cSeric 	if (stat != EX_TEMPFAIL)
1601eb238f8cSeric 		setstat(stat);
1602198d9be0Seric 	if (stat != EX_OK)
1603198d9be0Seric 	{
1604198d9be0Seric 		if (e->e_message != NULL)
1605198d9be0Seric 			free(e->e_message);
1606198d9be0Seric 		e->e_message = newstr(&statmsg[4]);
1607198d9be0Seric 	}
16088557d168Seric 	errno = 0;
1609d4bd8f0eSbostic #ifdef NAMED_BIND
1610f28da541Smiriam 	h_errno = 0;
1611d4bd8f0eSbostic #endif
1612eb238f8cSeric }
1613eb238f8cSeric /*
1614eb238f8cSeric **  LOGDELIVERY -- log the delivery in the system log
1615eb238f8cSeric **
1616eb238f8cSeric **	Parameters:
161781161401Seric **		m -- the mailer info.  Can be NULL for initial queue.
161881161401Seric **		mci -- the mailer connection info -- can be NULL if the
161981161401Seric **			log is occuring when no connection is active.
162081161401Seric **		stat -- the message to print for the status.
162181161401Seric **		e -- the current envelope.
1622eb238f8cSeric **
1623eb238f8cSeric **	Returns:
1624eb238f8cSeric **		none
1625eb238f8cSeric **
1626eb238f8cSeric **	Side Effects:
1627eb238f8cSeric **		none
1628eb238f8cSeric */
1629eb238f8cSeric 
163081161401Seric logdelivery(m, mci, stat, e)
163181161401Seric 	MAILER *m;
163281161401Seric 	register MCI *mci;
1633eb238f8cSeric 	char *stat;
1634b31e7f2bSeric 	register ENVELOPE *e;
16355cf56be3Seric {
1636eb238f8cSeric # ifdef LOG
16379507d1f9Seric 	char *curhost;
1638d6acf3eeSeric 	char buf[512];
16399507d1f9Seric 	extern char *pintvl();
16409507d1f9Seric 	extern char *macvalue();
16419507d1f9Seric 
164248ed5d33Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
164381161401Seric 
164448ed5d33Seric 	if (m != NULL)
164571ff6caaSeric 	{
164648ed5d33Seric 		(void) strcat(buf, ", mailer=");
164748ed5d33Seric 		(void) strcat(buf, m->m_name);
164871ff6caaSeric 	}
164948ed5d33Seric 
165048ed5d33Seric 	if (mci != NULL && mci->mci_host != NULL)
165171ff6caaSeric 	{
165271ff6caaSeric # ifdef DAEMON
1653e2f2f828Seric 		extern SOCKADDR CurHostAddr;
1654e2f2f828Seric 		extern char *anynet_ntoa();
165548ed5d33Seric # endif
165671ff6caaSeric 
165748ed5d33Seric 		(void) strcat(buf, ", relay=");
165848ed5d33Seric 		(void) strcat(buf, mci->mci_host);
165948ed5d33Seric 
166048ed5d33Seric # ifdef DAEMON
166148ed5d33Seric 		(void) strcat(buf, " (");
1662e2f2f828Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
166348ed5d33Seric 		(void) strcat(buf, ")");
166471ff6caaSeric # endif
166571ff6caaSeric 	}
16669507d1f9Seric 	else
166748ed5d33Seric 	{
166848ed5d33Seric 		char *p = macvalue('h', e);
16699507d1f9Seric 
167048ed5d33Seric 		if (p != NULL && p[0] != '\0')
167148ed5d33Seric 		{
167248ed5d33Seric 			(void) strcat(buf, ", relay=");
167348ed5d33Seric 			(void) strcat(buf, p);
167448ed5d33Seric 		}
167548ed5d33Seric 	}
1676d6acf3eeSeric 
1677d6acf3eeSeric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
1678d6acf3eeSeric 	       e->e_id, e->e_to, buf, stat);
16796c2c3107Seric # endif /* LOG */
168025a99e2eSeric }
168125a99e2eSeric /*
168251552439Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
168325a99e2eSeric **
168451552439Seric **	This can be made an arbitrary message separator by changing $l
168551552439Seric **
16869b6c17a6Seric **	One of the ugliest hacks seen by human eyes is contained herein:
16879b6c17a6Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
16889b6c17a6Seric **	does a well-meaning programmer such as myself have to deal with
16899b6c17a6Seric **	this kind of antique garbage????
169025a99e2eSeric **
169125a99e2eSeric **	Parameters:
169251552439Seric **		fp -- the file to output to.
169351552439Seric **		m -- the mailer describing this entry.
169425a99e2eSeric **
169525a99e2eSeric **	Returns:
169651552439Seric **		none
169725a99e2eSeric **
169825a99e2eSeric **	Side Effects:
169951552439Seric **		outputs some text to fp.
170025a99e2eSeric */
170125a99e2eSeric 
1702b31e7f2bSeric putfromline(fp, m, e)
170351552439Seric 	register FILE *fp;
170451552439Seric 	register MAILER *m;
1705b31e7f2bSeric 	ENVELOPE *e;
170625a99e2eSeric {
17072bc47524Seric 	char *template = "\201l\n";
170851552439Seric 	char buf[MAXLINE];
170925a99e2eSeric 
171057fc6f17Seric 	if (bitnset(M_NHDR, m->m_flags))
171151552439Seric 		return;
171213bbc08cSeric 
17132c7e1b8dSeric # ifdef UGLYUUCP
171457fc6f17Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
171574b6e67bSeric 	{
1716ea09d6edSeric 		char *bang;
1717ea09d6edSeric 		char xbuf[MAXLINE];
171874b6e67bSeric 
1719ee4b0922Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
17206c2c3107Seric 		bang = strchr(buf, '!');
172174b6e67bSeric 		if (bang == NULL)
172208b25121Seric 			syserr("554 No ! in UUCP! (%s)", buf);
172374b6e67bSeric 		else
1724588cad61Seric 		{
1725ea09d6edSeric 			*bang++ = '\0';
17262bc47524Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
1727ea09d6edSeric 			template = xbuf;
172874b6e67bSeric 		}
1729588cad61Seric 	}
17306c2c3107Seric # endif /* UGLYUUCP */
1731b31e7f2bSeric 	expand(template, buf, &buf[sizeof buf - 1], e);
173277b52738Seric 	putline(buf, fp, m);
1733bc6e2962Seric }
1734bc6e2962Seric /*
173551552439Seric **  PUTBODY -- put the body of a message.
173651552439Seric **
173751552439Seric **	Parameters:
173851552439Seric **		fp -- file to output onto.
173977b52738Seric **		m -- a mailer descriptor to control output format.
17409a6a5f55Seric **		e -- the envelope to put out.
174151552439Seric **
174251552439Seric **	Returns:
174351552439Seric **		none.
174451552439Seric **
174551552439Seric **	Side Effects:
174651552439Seric **		The message is written onto fp.
174751552439Seric */
174851552439Seric 
174977b52738Seric putbody(fp, m, e)
175051552439Seric 	FILE *fp;
1751588cad61Seric 	MAILER *m;
17529a6a5f55Seric 	register ENVELOPE *e;
175351552439Seric {
175477b52738Seric 	char buf[MAXLINE];
175551552439Seric 
175651552439Seric 	/*
175751552439Seric 	**  Output the body of the message
175851552439Seric 	*/
175951552439Seric 
17609a6a5f55Seric 	if (e->e_dfp == NULL)
176151552439Seric 	{
17629a6a5f55Seric 		if (e->e_df != NULL)
17639a6a5f55Seric 		{
17649a6a5f55Seric 			e->e_dfp = fopen(e->e_df, "r");
17659a6a5f55Seric 			if (e->e_dfp == NULL)
17668f9146b0Srick 				syserr("putbody: Cannot open %s for %s from %s",
17678f9146b0Srick 				e->e_df, e->e_to, e->e_from);
17689a6a5f55Seric 		}
17699a6a5f55Seric 		else
177077b52738Seric 			putline("<<< No Message Collected >>>", fp, m);
17719a6a5f55Seric 	}
17729a6a5f55Seric 	if (e->e_dfp != NULL)
17739a6a5f55Seric 	{
17749a6a5f55Seric 		rewind(e->e_dfp);
177577b52738Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
177624fc8aeeSeric 		{
177724fc8aeeSeric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
1778d6fa2b58Sbostic 			    strncmp(buf, "From ", 5) == 0)
17793462ad9eSeric 				(void) putc('>', fp);
178077b52738Seric 			putline(buf, fp, m);
178124fc8aeeSeric 		}
178251552439Seric 
17839a6a5f55Seric 		if (ferror(e->e_dfp))
178451552439Seric 		{
178551552439Seric 			syserr("putbody: read error");
178651552439Seric 			ExitStat = EX_IOERR;
178751552439Seric 		}
178851552439Seric 	}
178951552439Seric 
179051552439Seric 	(void) fflush(fp);
179151552439Seric 	if (ferror(fp) && errno != EPIPE)
179251552439Seric 	{
179351552439Seric 		syserr("putbody: write error");
179451552439Seric 		ExitStat = EX_IOERR;
179551552439Seric 	}
179651552439Seric 	errno = 0;
179725a99e2eSeric }
179825a99e2eSeric /*
179925a99e2eSeric **  MAILFILE -- Send a message to a file.
180025a99e2eSeric **
1801f129ec7dSeric **	If the file has the setuid/setgid bits set, but NO execute
1802f129ec7dSeric **	bits, sendmail will try to become the owner of that file
1803f129ec7dSeric **	rather than the real user.  Obviously, this only works if
1804f129ec7dSeric **	sendmail runs as root.
1805f129ec7dSeric **
1806588cad61Seric **	This could be done as a subordinate mailer, except that it
1807588cad61Seric **	is used implicitly to save messages in ~/dead.letter.  We
1808588cad61Seric **	view this as being sufficiently important as to include it
1809588cad61Seric **	here.  For example, if the system is dying, we shouldn't have
1810588cad61Seric **	to create another process plus some pipes to save the message.
1811588cad61Seric **
181225a99e2eSeric **	Parameters:
181325a99e2eSeric **		filename -- the name of the file to send to.
18146259796dSeric **		ctladdr -- the controlling address header -- includes
18156259796dSeric **			the userid/groupid to be when sending.
181625a99e2eSeric **
181725a99e2eSeric **	Returns:
181825a99e2eSeric **		The exit code associated with the operation.
181925a99e2eSeric **
182025a99e2eSeric **	Side Effects:
182125a99e2eSeric **		none.
182225a99e2eSeric */
182325a99e2eSeric 
1824b31e7f2bSeric mailfile(filename, ctladdr, e)
182525a99e2eSeric 	char *filename;
18266259796dSeric 	ADDRESS *ctladdr;
1827b31e7f2bSeric 	register ENVELOPE *e;
182825a99e2eSeric {
182925a99e2eSeric 	register FILE *f;
183032d19d43Seric 	register int pid;
183115d084d5Seric 	int mode;
183225a99e2eSeric 
183332d19d43Seric 	/*
183432d19d43Seric 	**  Fork so we can change permissions here.
183532d19d43Seric 	**	Note that we MUST use fork, not vfork, because of
183632d19d43Seric 	**	the complications of calling subroutines, etc.
183732d19d43Seric 	*/
183832d19d43Seric 
183932d19d43Seric 	DOFORK(fork);
184032d19d43Seric 
184132d19d43Seric 	if (pid < 0)
184232d19d43Seric 		return (EX_OSERR);
184332d19d43Seric 	else if (pid == 0)
184432d19d43Seric 	{
184532d19d43Seric 		/* child -- actually write to file */
1846f129ec7dSeric 		struct stat stb;
1847f129ec7dSeric 
18480984da9fSeric 		(void) signal(SIGINT, SIG_DFL);
18490984da9fSeric 		(void) signal(SIGHUP, SIG_DFL);
18500984da9fSeric 		(void) signal(SIGTERM, SIG_DFL);
18513462ad9eSeric 		(void) umask(OldUmask);
185295f16dc0Seric 
1853f129ec7dSeric 		if (stat(filename, &stb) < 0)
1854e6e1265fSeric 			stb.st_mode = 0666;
185515d084d5Seric 		mode = stb.st_mode;
185695f16dc0Seric 
185795f16dc0Seric 		/* limit the errors to those actually caused in the child */
185895f16dc0Seric 		errno = 0;
185995f16dc0Seric 		ExitStat = EX_OK;
186095f16dc0Seric 
1861f129ec7dSeric 		if (bitset(0111, stb.st_mode))
1862f129ec7dSeric 			exit(EX_CANTCREAT);
186303827b5fSeric 		if (ctladdr == NULL)
18648f9146b0Srick 			ctladdr = &e->e_from;
186515d084d5Seric 		else
186615d084d5Seric 		{
186715d084d5Seric 			/* ignore setuid and setgid bits */
186815d084d5Seric 			mode &= ~(S_ISGID|S_ISUID);
186915d084d5Seric 		}
187015d084d5Seric 
18718f9146b0Srick 		/* we have to open the dfile BEFORE setuid */
18728f9146b0Srick 		if (e->e_dfp == NULL && e->e_df != NULL)
18738f9146b0Srick 		{
18748f9146b0Srick 			e->e_dfp = fopen(e->e_df, "r");
187595f16dc0Seric 			if (e->e_dfp == NULL)
187695f16dc0Seric 			{
18778f9146b0Srick 				syserr("mailfile: Cannot open %s for %s from %s",
18788f9146b0Srick 					e->e_df, e->e_to, e->e_from);
18798f9146b0Srick 			}
18808f9146b0Srick 		}
18818f9146b0Srick 
188215d084d5Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
1883e36b99e2Seric 		{
188495f16dc0Seric 			if (ctladdr->q_uid == 0)
188595f16dc0Seric 			{
1886e36b99e2Seric 				(void) setgid(DefGid);
1887898a126bSbostic 				(void) initgroups(DefUser, DefGid);
188895f16dc0Seric 			}
188995f16dc0Seric 			else
189095f16dc0Seric 			{
18916259796dSeric 				(void) setgid(ctladdr->q_gid);
1892898a126bSbostic 				(void) initgroups(ctladdr->q_ruser ?
1893898a126bSbostic 					ctladdr->q_ruser : ctladdr->q_user,
1894898a126bSbostic 					ctladdr->q_gid);
1895898a126bSbostic 			}
1896e36b99e2Seric 		}
189715d084d5Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
1898e36b99e2Seric 		{
1899e36b99e2Seric 			if (ctladdr->q_uid == 0)
1900e36b99e2Seric 				(void) setuid(DefUid);
1901e36b99e2Seric 			else
19026259796dSeric 				(void) setuid(ctladdr->q_uid);
1903e36b99e2Seric 		}
190495f16dc0Seric 		FileName = filename;
190595f16dc0Seric 		LineNumber = 0;
190627628d59Seric 		f = dfopen(filename, "a");
190725a99e2eSeric 		if (f == NULL)
190895f16dc0Seric 		{
190908b25121Seric 			message("554 cannot open");
191032d19d43Seric 			exit(EX_CANTCREAT);
191195f16dc0Seric 		}
191225a99e2eSeric 
1913b31e7f2bSeric 		putfromline(f, ProgMailer, e);
1914b843d759Seric 		(*e->e_puthdr)(f, ProgMailer, e);
191577b52738Seric 		putline("\n", f, ProgMailer);
1916b843d759Seric 		(*e->e_putbody)(f, ProgMailer, e);
191777b52738Seric 		putline("\n", f, ProgMailer);
191895f16dc0Seric 		if (ferror(f))
191995f16dc0Seric 		{
192008b25121Seric 			message("451 I/O error");
192195f16dc0Seric 			setstat(EX_IOERR);
192295f16dc0Seric 		}
1923ee4b0922Seric 		(void) xfclose(f, "mailfile", filename);
192432d19d43Seric 		(void) fflush(stdout);
1925e36b99e2Seric 
192627628d59Seric 		/* reset ISUID & ISGID bits for paranoid systems */
1927c77d1c25Seric 		(void) chmod(filename, (int) stb.st_mode);
192895f16dc0Seric 		exit(ExitStat);
192913bbc08cSeric 		/*NOTREACHED*/
193032d19d43Seric 	}
193132d19d43Seric 	else
193232d19d43Seric 	{
193332d19d43Seric 		/* parent -- wait for exit status */
1934588cad61Seric 		int st;
193532d19d43Seric 
1936588cad61Seric 		st = waitfor(pid);
1937588cad61Seric 		if ((st & 0377) != 0)
1938588cad61Seric 			return (EX_UNAVAILABLE);
1939588cad61Seric 		else
1940588cad61Seric 			return ((st >> 8) & 0377);
19418f9146b0Srick 		/*NOTREACHED*/
194232d19d43Seric 	}
194325a99e2eSeric }
1944ea4dc939Seric /*
1945e103b48fSeric **  HOSTSIGNATURE -- return the "signature" for a host.
1946e103b48fSeric **
1947e103b48fSeric **	The signature describes how we are going to send this -- it
1948e103b48fSeric **	can be just the hostname (for non-Internet hosts) or can be
1949e103b48fSeric **	an ordered list of MX hosts.
1950e103b48fSeric **
1951e103b48fSeric **	Parameters:
1952e103b48fSeric **		m -- the mailer describing this host.
1953e103b48fSeric **		host -- the host name.
1954e103b48fSeric **		e -- the current envelope.
1955e103b48fSeric **
1956e103b48fSeric **	Returns:
1957e103b48fSeric **		The signature for this host.
1958e103b48fSeric **
1959e103b48fSeric **	Side Effects:
1960e103b48fSeric **		Can tweak the symbol table.
1961e103b48fSeric */
1962e103b48fSeric 
1963e103b48fSeric char *
1964e103b48fSeric hostsignature(m, host, e)
1965e103b48fSeric 	register MAILER *m;
1966e103b48fSeric 	char *host;
1967e103b48fSeric 	ENVELOPE *e;
1968e103b48fSeric {
1969e103b48fSeric 	register char *p;
1970e103b48fSeric 	register STAB *s;
1971e103b48fSeric 	int i;
1972e103b48fSeric 	int len;
1973e103b48fSeric #ifdef NAMED_BIND
1974e103b48fSeric 	int nmx;
1975e103b48fSeric 	auto int rcode;
1976e103b48fSeric 	char *mxhosts[MAXMXHOSTS + 1];
1977e103b48fSeric 	static char myhostbuf[MAXNAME];
1978e103b48fSeric #endif
1979e103b48fSeric 
1980e103b48fSeric 	/*
1981e103b48fSeric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
1982e103b48fSeric 	*/
1983e103b48fSeric 
1984e103b48fSeric 	p = m->m_mailer;
1985e103b48fSeric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
1986e103b48fSeric 	{
1987e103b48fSeric 		/* just an ordinary mailer */
1988e103b48fSeric 		return host;
1989e103b48fSeric 	}
1990e103b48fSeric 
1991e103b48fSeric 	/*
1992e103b48fSeric 	**  If it is a numeric address, just return it.
1993e103b48fSeric 	*/
1994e103b48fSeric 
1995e103b48fSeric 	if (host[0] == '[')
1996e103b48fSeric 		return host;
1997e103b48fSeric 
1998e103b48fSeric 	/*
1999e103b48fSeric 	**  Look it up in the symbol table.
2000e103b48fSeric 	*/
2001e103b48fSeric 
2002e103b48fSeric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
2003e103b48fSeric 	if (s->s_hostsig != NULL)
2004e103b48fSeric 		return s->s_hostsig;
2005e103b48fSeric 
2006e103b48fSeric 	/*
2007e103b48fSeric 	**  Not already there -- create a signature.
2008e103b48fSeric 	*/
2009e103b48fSeric 
2010e103b48fSeric #ifdef NAMED_BIND
2011e103b48fSeric 	if (myhostbuf[0] == '\0')
20122bc47524Seric 		expand("\201j", myhostbuf, &myhostbuf[sizeof myhostbuf - 1], e);
2013e103b48fSeric 
2014e103b48fSeric 	nmx = getmxrr(host, mxhosts, myhostbuf, &rcode);
20157d55540cSeric 
2016e103b48fSeric 	if (nmx <= 0)
2017e103b48fSeric 	{
2018e103b48fSeric 		register MCI *mci;
2019e103b48fSeric 		extern int errno;
2020e103b48fSeric 		extern MCI *mci_get();
2021e103b48fSeric 
2022e103b48fSeric 		/* update the connection info for this host */
2023e103b48fSeric 		mci = mci_get(host, m);
2024e103b48fSeric 		mci->mci_exitstat = rcode;
2025e103b48fSeric 		mci->mci_errno = errno;
2026e103b48fSeric 
2027e103b48fSeric 		/* and return the original host name as the signature */
2028e103b48fSeric 		s->s_hostsig = host;
2029e103b48fSeric 		return host;
2030e103b48fSeric 	}
2031e103b48fSeric 
2032e103b48fSeric 	len = 0;
2033e103b48fSeric 	for (i = 0; i < nmx; i++)
2034e103b48fSeric 	{
2035e103b48fSeric 		len += strlen(mxhosts[i]) + 1;
2036e103b48fSeric 	}
2037e103b48fSeric 	s->s_hostsig = p = xalloc(len);
2038e103b48fSeric 	for (i = 0; i < nmx; i++)
2039e103b48fSeric 	{
2040e103b48fSeric 		if (i != 0)
2041e103b48fSeric 			*p++ = ':';
2042e103b48fSeric 		strcpy(p, mxhosts[i]);
2043e103b48fSeric 		p += strlen(p);
2044e103b48fSeric 	}
2045e103b48fSeric 	makelower(s->s_hostsig);
2046e103b48fSeric #else
2047e103b48fSeric 	/* not using BIND -- the signature is just the host name */
2048e103b48fSeric 	s->s_hostsig = host;
2049e103b48fSeric #endif
2050e103b48fSeric 	if (tTd(17, 1))
2051e103b48fSeric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
2052e103b48fSeric 	return s->s_hostsig;
2053e103b48fSeric }
2054