122701Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
362523Sbostic  * Copyright (c) 1988, 1993
462523Sbostic  *	The Regents of the University of California.  All rights reserved.
533728Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822701Sdist 
922701Sdist #ifndef lint
10*64823Seric static char sccsid[] = "@(#)deliver.c	8.38 (Berkeley) 11/08/93";
1133728Sbostic #endif /* not lint */
1222701Sdist 
1340961Sbostic #include "sendmail.h"
1433931Sbostic #include <netdb.h>
1533931Sbostic #include <errno.h>
1635651Seric #ifdef NAMED_BIND
1734022Sbostic #include <arpa/nameser.h>
1834022Sbostic #include <resolv.h>
1963753Seric 
2063753Seric extern int	h_errno;
2135651Seric #endif
22294Seric 
23294Seric /*
2458820Seric **  SENDALL -- actually send all the messages.
2558820Seric **
2658820Seric **	Parameters:
2758820Seric **		e -- the envelope to send.
2858820Seric **		mode -- the delivery mode to use.  If SM_DEFAULT, use
2958820Seric **			the current e->e_sendmode.
3058820Seric **
3158820Seric **	Returns:
3258820Seric **		none.
3358820Seric **
3458820Seric **	Side Effects:
3558820Seric **		Scans the send lists and sends everything it finds.
3658820Seric **		Delivers any appropriate error messages.
3758820Seric **		If we are running in a non-interactive mode, takes the
3858820Seric **			appropriate action.
3958820Seric */
4058820Seric 
4158820Seric sendall(e, mode)
4258820Seric 	ENVELOPE *e;
4358820Seric 	char mode;
4458820Seric {
4558820Seric 	register ADDRESS *q;
4658820Seric 	char *owner;
4758820Seric 	int otherowners;
4858820Seric 	register ENVELOPE *ee;
4958820Seric 	ENVELOPE *splitenv = NULL;
5058929Seric 	bool announcequeueup;
5158820Seric 
5263839Seric 	/*
5363839Seric 	**  If we have had global, fatal errors, don't bother sending
5463839Seric 	**  the message at all if we are in SMTP mode.  Local errors
5563839Seric 	**  (e.g., a single address failing) will still cause the other
5663839Seric 	**  addresses to be sent.
5763839Seric 	*/
5863839Seric 
5963839Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
6061092Seric 	{
6161092Seric 		e->e_flags |= EF_CLRQUEUE;
6261092Seric 		return;
6361092Seric 	}
6461092Seric 
6558820Seric 	/* determine actual delivery mode */
6658820Seric 	if (mode == SM_DEFAULT)
6758820Seric 	{
6858820Seric 		mode = e->e_sendmode;
6958820Seric 		if (mode != SM_VERIFY &&
7058820Seric 		    shouldqueue(e->e_msgpriority, e->e_ctime))
7158820Seric 			mode = SM_QUEUE;
7258929Seric 		announcequeueup = mode == SM_QUEUE;
7358820Seric 	}
7458929Seric 	else
7558929Seric 		announcequeueup = FALSE;
7658820Seric 
7758820Seric 	if (tTd(13, 1))
7858820Seric 	{
7964310Seric 		printf("\n===== SENDALL: mode %c, id %s, e_from ",
8064310Seric 			mode, e->e_id);
8158820Seric 		printaddr(&e->e_from, FALSE);
8258820Seric 		printf("sendqueue:\n");
8358820Seric 		printaddr(e->e_sendqueue, TRUE);
8458820Seric 	}
8558820Seric 
8658820Seric 	/*
8758820Seric 	**  Do any preprocessing necessary for the mode we are running.
8858820Seric 	**	Check to make sure the hop count is reasonable.
8958820Seric 	**	Delete sends to the sender in mailing lists.
9058820Seric 	*/
9158820Seric 
9258820Seric 	CurEnv = e;
9358820Seric 
9458820Seric 	if (e->e_hopcount > MaxHopCount)
9558820Seric 	{
9658820Seric 		errno = 0;
9764495Seric 		syserr("554 too many hops %d (%d max): from %s via %s, to %s",
9858820Seric 			e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
9964495Seric 			RealHostName, e->e_sendqueue->q_paddr);
10058820Seric 		return;
10158820Seric 	}
10258820Seric 
10359435Seric 	/*
10459435Seric 	**  Do sender deletion.
10559435Seric 	**
10659435Seric 	**	If the sender has the QQUEUEUP flag set, skip this.
10759435Seric 	**	This can happen if the name server is hosed when you
10859435Seric 	**	are trying to send mail.  The result is that the sender
10959435Seric 	**	is instantiated in the queue as a recipient.
11059435Seric 	*/
11159435Seric 
11264118Seric 	if (!bitset(EF_METOO, e->e_flags) &&
11364118Seric 	    !bitset(QQUEUEUP, e->e_from.q_flags))
11458820Seric 	{
11558820Seric 		if (tTd(13, 5))
11658820Seric 		{
11758820Seric 			printf("sendall: QDONTSEND ");
11858820Seric 			printaddr(&e->e_from, FALSE);
11958820Seric 		}
12058820Seric 		e->e_from.q_flags |= QDONTSEND;
12158820Seric 		(void) recipient(&e->e_from, &e->e_sendqueue, e);
12258820Seric 	}
12358820Seric 
12458820Seric 	/*
12558820Seric 	**  Handle alias owners.
12658820Seric 	**
12758820Seric 	**	We scan up the q_alias chain looking for owners.
12858820Seric 	**	We discard owners that are the same as the return path.
12958820Seric 	*/
13058820Seric 
13158820Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
13258820Seric 	{
13358820Seric 		register struct address *a;
13458820Seric 
13558820Seric 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
13658820Seric 			continue;
13758820Seric 		if (a != NULL)
13858820Seric 			q->q_owner = a->q_owner;
13958820Seric 
14058820Seric 		if (q->q_owner != NULL &&
14158820Seric 		    !bitset(QDONTSEND, q->q_flags) &&
14258820Seric 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
14358820Seric 			q->q_owner = NULL;
14458820Seric 	}
14558820Seric 
14658820Seric 	owner = "";
14758820Seric 	otherowners = 1;
14858820Seric 	while (owner != NULL && otherowners > 0)
14958820Seric 	{
15058820Seric 		owner = NULL;
15158820Seric 		otherowners = 0;
15258820Seric 
15358820Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15458820Seric 		{
15558820Seric 			if (bitset(QDONTSEND, q->q_flags))
15658820Seric 				continue;
15758820Seric 
15858820Seric 			if (q->q_owner != NULL)
15958820Seric 			{
16058820Seric 				if (owner == NULL)
16158820Seric 					owner = q->q_owner;
16258820Seric 				else if (owner != q->q_owner)
16358820Seric 				{
16458820Seric 					if (strcmp(owner, q->q_owner) == 0)
16558820Seric 					{
16658820Seric 						/* make future comparisons cheap */
16758820Seric 						q->q_owner = owner;
16858820Seric 					}
16958820Seric 					else
17058820Seric 					{
17158820Seric 						otherowners++;
17258820Seric 					}
17358820Seric 					owner = q->q_owner;
17458820Seric 				}
17558820Seric 			}
17658820Seric 			else
17758820Seric 			{
17858820Seric 				otherowners++;
17958820Seric 			}
18058820Seric 		}
18158820Seric 
18258820Seric 		if (owner != NULL && otherowners > 0)
18358820Seric 		{
18458820Seric 			extern HDR *copyheader();
18558820Seric 			extern ADDRESS *copyqueue();
18658820Seric 
18758820Seric 			/*
18858820Seric 			**  Split this envelope into two.
18958820Seric 			*/
19058820Seric 
19158820Seric 			ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
19258820Seric 			*ee = *e;
19358820Seric 			ee->e_id = NULL;
19458820Seric 			(void) queuename(ee, '\0');
19558820Seric 
19658820Seric 			if (tTd(13, 1))
19758820Seric 				printf("sendall: split %s into %s\n",
19858820Seric 					e->e_id, ee->e_id);
19958820Seric 
20058820Seric 			ee->e_header = copyheader(e->e_header);
20158820Seric 			ee->e_sendqueue = copyqueue(e->e_sendqueue);
20258820Seric 			ee->e_errorqueue = copyqueue(e->e_errorqueue);
20358916Seric 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS);
20458820Seric 			setsender(owner, ee, NULL, TRUE);
20558820Seric 			if (tTd(13, 5))
20658820Seric 			{
20758820Seric 				printf("sendall(split): QDONTSEND ");
20858820Seric 				printaddr(&ee->e_from, FALSE);
20958820Seric 			}
21058820Seric 			ee->e_from.q_flags |= QDONTSEND;
21158820Seric 			ee->e_dfp = NULL;
21258820Seric 			ee->e_xfp = NULL;
21358820Seric 			ee->e_df = NULL;
21458820Seric 			ee->e_errormode = EM_MAIL;
21558820Seric 			ee->e_sibling = splitenv;
21658820Seric 			splitenv = ee;
21758820Seric 
21858820Seric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
21958820Seric 				if (q->q_owner == owner)
22058820Seric 					q->q_flags |= QDONTSEND;
22158820Seric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
22258820Seric 				if (q->q_owner != owner)
22358820Seric 					q->q_flags |= QDONTSEND;
22458820Seric 
22558820Seric 			if (e->e_df != NULL && mode != SM_VERIFY)
22658820Seric 			{
22758820Seric 				ee->e_dfp = NULL;
22864086Seric 				ee->e_df = queuename(ee, 'd');
22964086Seric 				ee->e_df = newstr(ee->e_df);
23058820Seric 				if (link(e->e_df, ee->e_df) < 0)
23158820Seric 				{
23258820Seric 					syserr("sendall: link(%s, %s)",
23358820Seric 						e->e_df, ee->e_df);
23458820Seric 				}
23558820Seric 			}
23658820Seric 
23758820Seric 			if (mode != SM_VERIFY)
23858820Seric 				openxscript(ee);
23958820Seric #ifdef LOG
24058820Seric 			if (LogLevel > 4)
24158820Seric 				syslog(LOG_INFO, "%s: clone %s",
24258820Seric 					ee->e_id, e->e_id);
24358820Seric #endif
24458820Seric 		}
24558820Seric 	}
24658820Seric 
24758820Seric 	if (owner != NULL)
24858820Seric 	{
24958820Seric 		setsender(owner, e, NULL, TRUE);
25058820Seric 		if (tTd(13, 5))
25158820Seric 		{
25258820Seric 			printf("sendall(owner): QDONTSEND ");
25358820Seric 			printaddr(&e->e_from, FALSE);
25458820Seric 		}
25558820Seric 		e->e_from.q_flags |= QDONTSEND;
25658820Seric 		e->e_errormode = EM_MAIL;
25758820Seric 	}
25858820Seric 
25958916Seric # ifdef QUEUE
26058916Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
26158916Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
26258916Seric 	    !bitset(EF_INQUEUE, e->e_flags))
26358916Seric 	{
26458916Seric 		/* be sure everything is instantiated in the queue */
26558929Seric 		queueup(e, TRUE, announcequeueup);
26658916Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
26758929Seric 			queueup(ee, TRUE, announcequeueup);
26858916Seric 	}
26958916Seric #endif /* QUEUE */
27058916Seric 
27158820Seric 	if (splitenv != NULL)
27258820Seric 	{
27358820Seric 		if (tTd(13, 1))
27458820Seric 		{
27558820Seric 			printf("\nsendall: Split queue; remaining queue:\n");
27658820Seric 			printaddr(e->e_sendqueue, TRUE);
27758820Seric 		}
27858820Seric 
27958820Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
28058820Seric 		{
28158820Seric 			CurEnv = ee;
28258820Seric 			sendenvelope(ee, mode);
28358820Seric 		}
28458820Seric 
28558820Seric 		CurEnv = e;
28658820Seric 	}
28758820Seric 	sendenvelope(e, mode);
28858820Seric 
28958820Seric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
29058820Seric 		dropenvelope(splitenv);
29158820Seric }
29258820Seric 
29358820Seric sendenvelope(e, mode)
29458820Seric 	register ENVELOPE *e;
29558820Seric 	char mode;
29658820Seric {
29758916Seric 	bool oldverbose;
29858916Seric 	int pid;
29958820Seric 	register ADDRESS *q;
30064296Seric 	char *qf;
30164296Seric 	char *id;
30258820Seric 
30363839Seric 	/*
30463839Seric 	**  If we have had global, fatal errors, don't bother sending
30563839Seric 	**  the message at all if we are in SMTP mode.  Local errors
30663839Seric 	**  (e.g., a single address failing) will still cause the other
30763839Seric 	**  addresses to be sent.
30863839Seric 	*/
30963839Seric 
31063839Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
31163839Seric 	{
31263839Seric 		e->e_flags |= EF_CLRQUEUE;
31363839Seric 		return;
31463839Seric 	}
31563839Seric 
31658916Seric 	oldverbose = Verbose;
31758916Seric 	switch (mode)
31858916Seric 	{
31958916Seric 	  case SM_VERIFY:
32058916Seric 		Verbose = TRUE;
32158916Seric 		break;
32258916Seric 
32358916Seric 	  case SM_QUEUE:
32458916Seric   queueonly:
32558916Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
32658916Seric 		return;
32758916Seric 
32858916Seric 	  case SM_FORK:
32958916Seric 		if (e->e_xfp != NULL)
33058916Seric 			(void) fflush(e->e_xfp);
33158916Seric 
33264035Seric # ifndef HASFLOCK
33358916Seric 		/*
33464296Seric 		**  Since fcntl locking has the interesting semantic that
33564296Seric 		**  the lock is owned by a process, not by an open file
33664296Seric 		**  descriptor, we have to flush this to the queue, and
33764296Seric 		**  then restart from scratch in the child.
33858916Seric 		*/
33958916Seric 
34064296Seric 		/* save id for future use */
34164296Seric 		id = e->e_id;
34258916Seric 
34364296Seric 		/* now drop the envelope in the parent */
34464296Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
34564296Seric 		dropenvelope(e);
34664296Seric 
34764296Seric 		/* and reacquire in the child */
34864296Seric 		(void) dowork(id, TRUE, FALSE, e);
34964296Seric 
35064296Seric 		return;
35164296Seric 
35264296Seric # else /* HASFLOCK */
35364296Seric 
35458916Seric 		pid = fork();
35558916Seric 		if (pid < 0)
35658916Seric 		{
35758916Seric 			goto queueonly;
35858916Seric 		}
35958916Seric 		else if (pid > 0)
36058916Seric 		{
36164310Seric 			/* be sure we leave the temp files to our child */
36264310Seric 			/* can't call unlockqueue to avoid unlink of xfp */
36364310Seric 			if (e->e_lockfp != NULL)
36464310Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
36564310Seric 			e->e_lockfp = NULL;
36664310Seric 
36764310Seric 			/* close any random open files in the envelope */
36864310Seric 			closexscript(e);
36964310Seric 			if (e->e_dfp != NULL)
37064310Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
37164310Seric 			e->e_dfp = NULL;
37264310Seric 			e->e_id = e->e_df = NULL;
37358916Seric 			return;
37458916Seric 		}
37558916Seric 
37658916Seric 		/* double fork to avoid zombies */
37758916Seric 		if (fork() > 0)
37858916Seric 			exit(EX_OK);
37958916Seric 
38058916Seric 		/* be sure we are immune from the terminal */
38163839Seric 		disconnect(1, e);
38258916Seric 
38358916Seric 		/*
38458916Seric 		**  Close any cached connections.
38558916Seric 		**
38658916Seric 		**	We don't send the QUIT protocol because the parent
38758916Seric 		**	still knows about the connection.
38858916Seric 		**
38958916Seric 		**	This should only happen when delivering an error
39058916Seric 		**	message.
39158916Seric 		*/
39258916Seric 
39358916Seric 		mci_flush(FALSE, NULL);
39458916Seric 
39564296Seric # endif /* HASFLOCK */
39664296Seric 
39758916Seric 		break;
39858916Seric 	}
39958916Seric 
40058820Seric 	/*
40158820Seric 	**  Run through the list and send everything.
40263965Seric 	**
40363965Seric 	**	Set EF_GLOBALERRS so that error messages during delivery
40463965Seric 	**	result in returned mail.
40558820Seric 	*/
40658820Seric 
40758820Seric 	e->e_nsent = 0;
40863965Seric 	e->e_flags |= EF_GLOBALERRS;
40964696Seric 
41064696Seric 	/* now run through the queue */
41158820Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
41258820Seric 	{
41364441Seric #ifdef XDEBUG
41464441Seric 		char wbuf[MAXNAME + 20];
41564441Seric 
41664441Seric 		(void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
41764441Seric 		checkfd012(wbuf);
41864441Seric #endif
41958820Seric 		if (mode == SM_VERIFY)
42058820Seric 		{
42158820Seric 			e->e_to = q->q_paddr;
42258820Seric 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
42360173Seric 			{
42460173Seric 				message("deliverable: mailer %s, host %s, user %s",
42560173Seric 					q->q_mailer->m_name,
42660173Seric 					q->q_host,
42760173Seric 					q->q_user);
42860173Seric 			}
42958820Seric 		}
43058820Seric 		else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
43158820Seric 		{
43258820Seric # ifdef QUEUE
43358820Seric 			/*
43458820Seric 			**  Checkpoint the send list every few addresses
43558820Seric 			*/
43658820Seric 
43758820Seric 			if (e->e_nsent >= CheckpointInterval)
43858820Seric 			{
43958820Seric 				queueup(e, TRUE, FALSE);
44058820Seric 				e->e_nsent = 0;
44158820Seric 			}
44258820Seric # endif /* QUEUE */
44358820Seric 			(void) deliver(e, q);
44458820Seric 		}
44558820Seric 	}
44658820Seric 	Verbose = oldverbose;
44758820Seric 
44864441Seric #ifdef XDEBUG
44964441Seric 	checkfd012("end of sendenvelope");
45064441Seric #endif
45164441Seric 
45258820Seric 	if (mode == SM_FORK)
45358820Seric 		finis();
45458820Seric }
45558820Seric /*
45658820Seric **  DOFORK -- do a fork, retrying a couple of times on failure.
45758820Seric **
45858820Seric **	This MUST be a macro, since after a vfork we are running
45958820Seric **	two processes on the same stack!!!
46058820Seric **
46158820Seric **	Parameters:
46258820Seric **		none.
46358820Seric **
46458820Seric **	Returns:
46558820Seric **		From a macro???  You've got to be kidding!
46658820Seric **
46758820Seric **	Side Effects:
46858820Seric **		Modifies the ==> LOCAL <== variable 'pid', leaving:
46958820Seric **			pid of child in parent, zero in child.
47058820Seric **			-1 on unrecoverable error.
47158820Seric **
47258820Seric **	Notes:
47358820Seric **		I'm awfully sorry this looks so awful.  That's
47458820Seric **		vfork for you.....
47558820Seric */
47658820Seric 
47758820Seric # define NFORKTRIES	5
47858820Seric 
47958820Seric # ifndef FORK
48058820Seric # define FORK	fork
48158820Seric # endif
48258820Seric 
48358820Seric # define DOFORK(fORKfN) \
48458820Seric {\
48558820Seric 	register int i;\
48658820Seric \
48758820Seric 	for (i = NFORKTRIES; --i >= 0; )\
48858820Seric 	{\
48958820Seric 		pid = fORKfN();\
49058820Seric 		if (pid >= 0)\
49158820Seric 			break;\
49258820Seric 		if (i > 0)\
49358820Seric 			sleep((unsigned) NFORKTRIES - i);\
49458820Seric 	}\
49558820Seric }
49658820Seric /*
49758820Seric **  DOFORK -- simple fork interface to DOFORK.
49858820Seric **
49958820Seric **	Parameters:
50058820Seric **		none.
50158820Seric **
50258820Seric **	Returns:
50358820Seric **		pid of child in parent.
50458820Seric **		zero in child.
50558820Seric **		-1 on error.
50658820Seric **
50758820Seric **	Side Effects:
50858820Seric **		returns twice, once in parent and once in child.
50958820Seric */
51058820Seric 
51158820Seric dofork()
51258820Seric {
51358820Seric 	register int pid;
51458820Seric 
51558820Seric 	DOFORK(fork);
51658820Seric 	return (pid);
51758820Seric }
51858820Seric /*
5194315Seric **  DELIVER -- Deliver a message to a list of addresses.
520294Seric **
5214315Seric **	This routine delivers to everyone on the same host as the
5224315Seric **	user on the head of the list.  It is clever about mailers
5234315Seric **	that don't handle multiple users.  It is NOT guaranteed
5244315Seric **	that it will deliver to all these addresses however -- so
5254315Seric **	deliver should be called once for each address on the
5264315Seric **	list.
5274315Seric **
528294Seric **	Parameters:
5299370Seric **		e -- the envelope to deliver.
5304621Seric **		firstto -- head of the address list to deliver to.
531294Seric **
532294Seric **	Returns:
533294Seric **		zero -- successfully delivered.
534294Seric **		else -- some failure, see ExitStat for more info.
535294Seric **
536294Seric **	Side Effects:
537294Seric **		The standard input is passed off to someone.
538294Seric */
539294Seric 
5409370Seric deliver(e, firstto)
5419370Seric 	register ENVELOPE *e;
5424621Seric 	ADDRESS *firstto;
543294Seric {
5444452Seric 	char *host;			/* host being sent to */
5454452Seric 	char *user;			/* user being sent to */
546294Seric 	char **pvp;
5473233Seric 	register char **mvp;
5483233Seric 	register char *p;
54910306Seric 	register MAILER *m;		/* mailer for this recipient */
5504397Seric 	ADDRESS *ctladdr;
55154967Seric 	register MCI *mci;
5524621Seric 	register ADDRESS *to = firstto;
5534863Seric 	bool clever = FALSE;		/* running user smtp to this mailer */
5545032Seric 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
55551951Seric 	int rcode;			/* response code */
55657454Seric 	char *firstsig;			/* signature of firstto */
55758820Seric 	int pid;
55858820Seric 	char *curhost;
55958820Seric 	int mpvect[2];
56058820Seric 	int rpvect[2];
56110306Seric 	char *pv[MAXPV+1];
56258704Seric 	char tobuf[TOBUFSIZE];		/* text line of to people */
56310306Seric 	char buf[MAXNAME];
56451951Seric 	char rpathbuf[MAXNAME];		/* translated return path */
56557441Seric 	extern int checkcompat();
56664718Seric 	extern char SmtpError[];
567294Seric 
5684488Seric 	errno = 0;
56958680Seric 	if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
5703233Seric 		return (0);
571294Seric 
57235651Seric #ifdef NAMED_BIND
57334022Sbostic 	/* unless interactive, try twice, over a minute */
57434022Sbostic 	if (OpMode == MD_DAEMON || OpMode == MD_SMTP) {
57534022Sbostic 		_res.retrans = 30;
57634022Sbostic 		_res.retry = 2;
57734022Sbostic 	}
57836788Sbostic #endif
57934022Sbostic 
5806974Seric 	m = to->q_mailer;
5816974Seric 	host = to->q_host;
58258803Seric 	CurEnv = e;			/* just in case */
58359044Seric 	e->e_statmsg = NULL;
58464718Seric 	SmtpError[0] = '\0';
5856974Seric 
5867672Seric 	if (tTd(10, 1))
5873233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
5886974Seric 			m->m_mno, host, to->q_user);
58964725Seric 	if (tTd(10, 100))
59064725Seric 		printopenfds(FALSE);
591294Seric 
592294Seric 	/*
5935903Seric 	**  If this mailer is expensive, and if we don't want to make
5945903Seric 	**  connections now, just mark these addresses and return.
5955903Seric 	**	This is useful if we want to batch connections to
5965903Seric 	**	reduce load.  This will cause the messages to be
5975903Seric 	**	queued up, and a daemon will come along to send the
5985903Seric 	**	messages later.
5995903Seric 	**		This should be on a per-mailer basis.
6005903Seric 	*/
6015903Seric 
60264658Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
6035903Seric 	{
6045903Seric 		for (; to != NULL; to = to->q_next)
6058431Seric 		{
60658680Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
60758247Seric 			    to->q_mailer != m)
6088431Seric 				continue;
60963853Seric 			to->q_flags |= QQUEUEUP;
6109370Seric 			e->e_to = to->q_paddr;
61158151Seric 			message("queued");
61258020Seric 			if (LogLevel > 8)
61364771Seric 				logdelivery(m, NULL, "queued", NULL, e);
6148431Seric 		}
6159370Seric 		e->e_to = NULL;
6165903Seric 		return (0);
6175903Seric 	}
6185903Seric 
6195903Seric 	/*
6203233Seric 	**  Do initial argv setup.
6213233Seric 	**	Insert the mailer name.  Notice that $x expansion is
6223233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
6233233Seric 	**	a picky -f flag, we insert it as appropriate.  This
6243233Seric 	**	code does not check for 'pv' overflow; this places a
6253233Seric 	**	manifest lower limit of 4 for MAXPV.
6268062Seric 	**		The from address rewrite is expected to make
6278062Seric 	**		the address relative to the other end.
6282968Seric 	*/
6292968Seric 
6304452Seric 	/* rewrite from address, using rewriting rules */
63159163Seric 	rcode = EX_OK;
63259163Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
63359163Seric 					   RF_SENDERADDR|RF_CANONICAL,
63459163Seric 					   &rcode, e));
63558680Seric 	define('g', rpathbuf, e);		/* translated return path */
6369370Seric 	define('h', host, e);			/* to host */
6373233Seric 	Errors = 0;
6383233Seric 	pvp = pv;
6393233Seric 	*pvp++ = m->m_argv[0];
6402968Seric 
6413233Seric 	/* insert -f or -r flag as appropriate */
64210682Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6433233Seric 	{
64410682Seric 		if (bitnset(M_FOPT, m->m_flags))
6453233Seric 			*pvp++ = "-f";
6463233Seric 		else
6473233Seric 			*pvp++ = "-r";
64851951Seric 		*pvp++ = newstr(rpathbuf);
6493233Seric 	}
650294Seric 
651294Seric 	/*
6523233Seric 	**  Append the other fixed parts of the argv.  These run
6533233Seric 	**  up to the first entry containing "$u".  There can only
6543233Seric 	**  be one of these, and there are only a few more slots
6553233Seric 	**  in the pv after it.
656294Seric 	*/
657294Seric 
6583233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
659294Seric 	{
66058050Seric 		/* can't use strchr here because of sign extension problems */
66158050Seric 		while (*p != '\0')
66258050Seric 		{
66358050Seric 			if ((*p++ & 0377) == MACROEXPAND)
66458050Seric 			{
66558050Seric 				if (*p == 'u')
66658050Seric 					break;
66758050Seric 			}
66858050Seric 		}
66958050Seric 
67058050Seric 		if (*p != '\0')
6713233Seric 			break;
6723233Seric 
6733233Seric 		/* this entry is safe -- go ahead and process it */
6749370Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6753233Seric 		*pvp++ = newstr(buf);
6763233Seric 		if (pvp >= &pv[MAXPV - 3])
6773233Seric 		{
67858151Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6793233Seric 			return (-1);
6803233Seric 		}
681294Seric 	}
6824863Seric 
6836038Seric 	/*
6846038Seric 	**  If we have no substitution for the user name in the argument
6856038Seric 	**  list, we know that we must supply the names otherwise -- and
6866038Seric 	**  SMTP is the answer!!
6876038Seric 	*/
6886038Seric 
6893233Seric 	if (*mvp == NULL)
6904863Seric 	{
6914863Seric 		/* running SMTP */
6925179Seric # ifdef SMTP
6934863Seric 		clever = TRUE;
6944863Seric 		*pvp = NULL;
69556795Seric # else /* SMTP */
6966038Seric 		/* oops!  we don't implement SMTP */
69764718Seric 		syserr("554 SMTP style mailer not implemented");
6985179Seric 		return (EX_SOFTWARE);
69956795Seric # endif /* SMTP */
7004863Seric 	}
701294Seric 
702294Seric 	/*
7033233Seric 	**  At this point *mvp points to the argument with $u.  We
7043233Seric 	**  run through our address list and append all the addresses
7053233Seric 	**  we can.  If we run out of space, do not fret!  We can
7063233Seric 	**  always send another copy later.
707294Seric 	*/
708294Seric 
7093233Seric 	tobuf[0] = '\0';
7109370Seric 	e->e_to = tobuf;
7114397Seric 	ctladdr = NULL;
71257454Seric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7133233Seric 	for (; to != NULL; to = to->q_next)
714294Seric 	{
7153233Seric 		/* avoid sending multiple recipients to dumb mailers */
71610682Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7173233Seric 			break;
7183233Seric 
7193233Seric 		/* if already sent or not for this host, don't send */
72058680Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
72157454Seric 		    to->q_mailer != firstto->q_mailer ||
72257454Seric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7233233Seric 			continue;
7244397Seric 
7258225Seric 		/* avoid overflowing tobuf */
72642462Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7278225Seric 			break;
7288225Seric 
7297672Seric 		if (tTd(10, 1))
7305032Seric 		{
7315032Seric 			printf("\nsend to ");
7325032Seric 			printaddr(to, FALSE);
7335032Seric 		}
7345032Seric 
7354397Seric 		/* compute effective uid/gid when sending */
7364596Seric 		if (to->q_mailer == ProgMailer)
7374397Seric 			ctladdr = getctladdr(to);
7384397Seric 
7393233Seric 		user = to->q_user;
7409370Seric 		e->e_to = to->q_paddr;
74157731Seric 		if (tTd(10, 5))
74257731Seric 		{
74357731Seric 			printf("deliver: QDONTSEND ");
74457731Seric 			printaddr(to, FALSE);
74557731Seric 		}
74658680Seric 		to->q_flags |= QDONTSEND;
7473233Seric 
7483233Seric 		/*
7493233Seric 		**  Check to see that these people are allowed to
7503233Seric 		**  talk to each other.
7513233Seric 		*/
7523233Seric 
75310699Seric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
75410699Seric 		{
75529914Seric 			NoReturn = TRUE;
75658151Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
75764771Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e);
75810699Seric 			continue;
75910699Seric 		}
76057441Seric 		rcode = checkcompat(to, e);
76157459Seric 		if (rcode != EX_OK)
762294Seric 		{
76363787Seric 			markfailure(e, to, rcode);
76464771Seric 			giveresponse(rcode, m, NULL, ctladdr, e);
7653233Seric 			continue;
766294Seric 		}
7673233Seric 
7683233Seric 		/*
7694099Seric 		**  Strip quote bits from names if the mailer is dumb
7704099Seric 		**	about them.
7713233Seric 		*/
7723233Seric 
77310682Seric 		if (bitnset(M_STRIPQ, m->m_flags))
774294Seric 		{
77554983Seric 			stripquotes(user);
77654983Seric 			stripquotes(host);
7773233Seric 		}
7783233Seric 
7799206Seric 		/* hack attack -- delivermail compatibility */
7809206Seric 		if (m == ProgMailer && *user == '|')
7819206Seric 			user++;
7829206Seric 
7833233Seric 		/*
7844161Seric 		**  If an error message has already been given, don't
7854161Seric 		**	bother to send to this address.
7864161Seric 		**
7874161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
7884161Seric 		**	>> NOTE >> cannot do any further aliasing; that
7894161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
7904161Seric 		*/
7914161Seric 
7927293Seric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7934161Seric 			continue;
7944161Seric 
7954283Seric 		/* save statistics.... */
7969370Seric 		markstats(e, to);
7974283Seric 
7984161Seric 		/*
7993233Seric 		**  See if this user name is "special".
8003233Seric 		**	If the user name has a slash in it, assume that this
8016974Seric 		**	is a file -- send it off without further ado.  Note
8026974Seric 		**	that this type of addresses is not processed along
8036974Seric 		**	with the others, so we fudge on the To person.
8043233Seric 		*/
8053233Seric 
80657402Seric 		if (m == FileMailer)
8073233Seric 		{
80864771Seric 			ADDRESS *caddr = getctladdr(to);
80964771Seric 
81064771Seric 			rcode = mailfile(user, caddr, e);
81164771Seric 			giveresponse(rcode, m, NULL, caddr, e);
81257402Seric 			if (rcode == EX_OK)
81357402Seric 				to->q_flags |= QSENT;
81457402Seric 			continue;
815294Seric 		}
8163233Seric 
8174315Seric 		/*
8184315Seric 		**  Address is verified -- add this user to mailer
8194315Seric 		**  argv, and add it to the print list of recipients.
8204315Seric 		*/
8214315Seric 
8226059Seric 		/* link together the chain of recipients */
8236272Seric 		to->q_tchain = tochain;
8246272Seric 		tochain = to;
8256059Seric 
8263233Seric 		/* create list of users for error messages */
8279388Seric 		(void) strcat(tobuf, ",");
8284082Seric 		(void) strcat(tobuf, to->q_paddr);
8299370Seric 		define('u', user, e);		/* to user */
83064694Seric 		p = to->q_home;
83164694Seric 		if (p == NULL && ctladdr != NULL)
83264694Seric 			p = ctladdr->q_home;
83364694Seric 		define('z', p, e);	/* user's home */
8343233Seric 
8354863Seric 		/*
8366059Seric 		**  Expand out this user into argument list.
8374863Seric 		*/
8384863Seric 
8396059Seric 		if (!clever)
8403233Seric 		{
8419370Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8424863Seric 			*pvp++ = newstr(buf);
8434863Seric 			if (pvp >= &pv[MAXPV - 2])
8444863Seric 			{
8454863Seric 				/* allow some space for trailing parms */
8464863Seric 				break;
8474863Seric 			}
8484863Seric 		}
849294Seric 	}
850294Seric 
8514067Seric 	/* see if any addresses still exist */
8524067Seric 	if (tobuf[0] == '\0')
8534863Seric 	{
8549370Seric 		define('g', (char *) NULL, e);
8554067Seric 		return (0);
8564863Seric 	}
8574067Seric 
8583233Seric 	/* print out messages as full list */
8599388Seric 	e->e_to = tobuf + 1;
8603233Seric 
861294Seric 	/*
8623233Seric 	**  Fill out any parameters after the $u parameter.
863294Seric 	*/
864294Seric 
8654863Seric 	while (!clever && *++mvp != NULL)
866294Seric 	{
8679370Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8683233Seric 		*pvp++ = newstr(buf);
8693233Seric 		if (pvp >= &pv[MAXPV])
87058151Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
871294Seric 	}
8723233Seric 	*pvp++ = NULL;
873294Seric 
874294Seric 	/*
875294Seric 	**  Call the mailer.
8762898Seric 	**	The argument vector gets built, pipes
877294Seric 	**	are created as necessary, and we fork & exec as
8782898Seric 	**	appropriate.
8794863Seric 	**	If we are running SMTP, we just need to clean up.
880294Seric 	*/
881294Seric 
88258309Seric 	if (ctladdr == NULL && m != ProgMailer)
88341050Seric 		ctladdr = &e->e_from;
88435651Seric #ifdef NAMED_BIND
88551313Seric 	if (ConfigLevel < 2)
88651313Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
88735651Seric #endif
88854967Seric 
8897672Seric 	if (tTd(11, 1))
890294Seric 	{
8918178Seric 		printf("openmailer:");
89258820Seric 		printav(pv);
893294Seric 	}
8944488Seric 	errno = 0;
8953233Seric 
89625050Seric 	CurHostName = m->m_mailer;
89725050Seric 
8986038Seric 	/*
8996038Seric 	**  Deal with the special case of mail handled through an IPC
9006038Seric 	**  connection.
9016038Seric 	**	In this case we don't actually fork.  We must be
9026038Seric 	**	running SMTP for this to work.  We will return a
9036038Seric 	**	zero pid to indicate that we are running IPC.
90411160Seric 	**  We also handle a debug version that just talks to stdin/out.
9056038Seric 	*/
9066038Seric 
90758820Seric 	curhost = NULL;
90864334Seric 	SmtpPhase = NULL;
90964718Seric 	mci = NULL;
91058820Seric 
91164401Seric #ifdef XDEBUG
91264401Seric 	{
91364401Seric 		char wbuf[MAXLINE];
91464401Seric 
91564401Seric 		/* make absolutely certain 0, 1, and 2 are in use */
91664401Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
91764401Seric 		checkfd012(wbuf);
91864401Seric 	}
91964401Seric #endif
92064401Seric 
92111160Seric 	/* check for Local Person Communication -- not for mortals!!! */
92211160Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
92311160Seric 	{
92454967Seric 		mci = (MCI *) xalloc(sizeof *mci);
92554993Seric 		bzero((char *) mci, sizeof *mci);
92653738Seric 		mci->mci_in = stdin;
92753738Seric 		mci->mci_out = stdout;
92854967Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
92953751Seric 		mci->mci_mailer = m;
93011160Seric 	}
93154967Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
93254967Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9336038Seric 	{
93452107Seric #ifdef DAEMON
93557454Seric 		register int i;
9367285Seric 		register u_short port;
9376038Seric 
93858820Seric 		CurHostName = pv[1];
93958820Seric 		curhost = hostsignature(m, pv[1], e);
94054967Seric 
94158479Seric 		if (curhost == NULL || curhost[0] == '\0')
94258479Seric 		{
94358479Seric 			syserr("null signature");
94458820Seric 			rcode = EX_OSERR;
94558820Seric 			goto give_up;
94658479Seric 		}
94758479Seric 
9486038Seric 		if (!clever)
94958479Seric 		{
95058151Seric 			syserr("554 non-clever IPC");
95164718Seric 			rcode = EX_CONFIG;
95258820Seric 			goto give_up;
95358479Seric 		}
95458820Seric 		if (pv[2] != NULL)
95558820Seric 			port = atoi(pv[2]);
9566632Seric 		else
9577285Seric 			port = 0;
95858820Seric tryhost:
95957454Seric 		while (*curhost != '\0')
96029433Sbloom 		{
96157454Seric 			register char *p;
96258664Seric 			static char hostbuf[MAXNAME];
96357454Seric 
96457454Seric 			/* pull the next host from the signature */
96557454Seric 			p = strchr(curhost, ':');
96657454Seric 			if (p == NULL)
96757454Seric 				p = &curhost[strlen(curhost)];
96864718Seric 			if (p == curhost)
96964718Seric 			{
97064718Seric 				syserr("deliver: null host name in signature");
97164726Seric 				curhost++;
97264718Seric 				continue;
97364718Seric 			}
97457454Seric 			strncpy(hostbuf, curhost, p - curhost);
97557454Seric 			hostbuf[p - curhost] = '\0';
97657454Seric 			if (*p != '\0')
97757454Seric 				p++;
97857454Seric 			curhost = p;
97957454Seric 
98053738Seric 			/* see if we already know that this host is fried */
98157454Seric 			CurHostName = hostbuf;
98257454Seric 			mci = mci_get(hostbuf, m);
98354967Seric 			if (mci->mci_state != MCIS_CLOSED)
98457387Seric 			{
98557387Seric 				if (tTd(11, 1))
98657387Seric 				{
98757387Seric 					printf("openmailer: ");
98864731Seric 					mci_dump(mci, FALSE);
98957387Seric 				}
99057943Seric 				CurHostName = mci->mci_host;
99158820Seric 				break;
99257387Seric 			}
99353751Seric 			mci->mci_mailer = m;
99454967Seric 			if (mci->mci_exitstat != EX_OK)
99554967Seric 				continue;
99654967Seric 
99754967Seric 			/* try the connection */
99857454Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
99958151Seric 			message("Connecting to %s (%s)...",
100057454Seric 				hostbuf, m->m_name);
100157454Seric 			i = makeconnection(hostbuf, port, mci,
100254967Seric 				bitnset(M_SECURE_PORT, m->m_flags));
100354967Seric 			mci->mci_exitstat = i;
100454967Seric 			mci->mci_errno = errno;
100563753Seric #ifdef NAMED_BIND
100663753Seric 			mci->mci_herrno = h_errno;
100763753Seric #endif
100854967Seric 			if (i == EX_OK)
100952106Seric 			{
101054967Seric 				mci->mci_state = MCIS_OPENING;
101154967Seric 				mci_cache(mci);
101263753Seric 				if (TrafficLogFile != NULL)
101363753Seric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
101463753Seric 						getpid(), hostbuf);
101554967Seric 				break;
101634022Sbostic 			}
101754967Seric 			else if (tTd(11, 1))
101854967Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
101954967Seric 					i, errno);
102053738Seric 
102153738Seric 			/* enter status of this host */
102253738Seric 			setstat(i);
102364718Seric 
102464718Seric 			/* should print some message here for -v mode */
10256047Seric 		}
102664718Seric 		if (mci == NULL)
102764718Seric 		{
102864718Seric 			syserr("deliver: no host name");
102964718Seric 			rcode = EX_OSERR;
103064718Seric 			goto give_up;
103164718Seric 		}
103254993Seric 		mci->mci_pid = 0;
103354967Seric #else /* no DAEMON */
103458151Seric 		syserr("554 openmailer: no IPC");
103557387Seric 		if (tTd(11, 1))
103657387Seric 			printf("openmailer: NULL\n");
103764718Seric 		rcode = EX_UNAVAILABLE;
103864718Seric 		goto give_up;
103954967Seric #endif /* DAEMON */
10406038Seric 	}
104154967Seric 	else
1042294Seric 	{
104363753Seric 		if (TrafficLogFile != NULL)
104458852Seric 		{
104563753Seric 			char **av;
104658925Seric 
104763753Seric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
104863753Seric 			for (av = pv; *av != NULL; av++)
104963753Seric 				fprintf(TrafficLogFile, " %s", *av);
105063753Seric 			fprintf(TrafficLogFile, "\n");
105158852Seric 		}
105258852Seric 
105354967Seric 		/* create a pipe to shove the mail through */
105454967Seric 		if (pipe(mpvect) < 0)
105554967Seric 		{
105658925Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
105758925Seric 				e->e_to, m->m_name);
105857387Seric 			if (tTd(11, 1))
105957387Seric 				printf("openmailer: NULL\n");
106058820Seric 			rcode = EX_OSERR;
106158820Seric 			goto give_up;
106254967Seric 		}
10634863Seric 
106454967Seric 		/* if this mailer speaks smtp, create a return pipe */
106554967Seric 		if (clever && pipe(rpvect) < 0)
106654967Seric 		{
106758925Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
106858925Seric 				e->e_to, m->m_name);
106954967Seric 			(void) close(mpvect[0]);
107054967Seric 			(void) close(mpvect[1]);
107157387Seric 			if (tTd(11, 1))
107257387Seric 				printf("openmailer: NULL\n");
107358820Seric 			rcode = EX_OSERR;
107458820Seric 			goto give_up;
107554967Seric 		}
10764863Seric 
107754967Seric 		/*
107854967Seric 		**  Actually fork the mailer process.
107954967Seric 		**	DOFORK is clever about retrying.
108054967Seric 		**
108154967Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
108254967Seric 		**	around so that endmail will get it.
108354967Seric 		*/
10846038Seric 
108554967Seric 		if (e->e_xfp != NULL)
108654967Seric 			(void) fflush(e->e_xfp);		/* for debugging */
108754967Seric 		(void) fflush(stdout);
108826434Seric # ifdef SIGCHLD
108964035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
109056795Seric # endif /* SIGCHLD */
109154967Seric 		DOFORK(FORK);
109254967Seric 		/* pid is set by DOFORK */
109354967Seric 		if (pid < 0)
10944863Seric 		{
109554967Seric 			/* failure */
109658925Seric 			syserr("%s... openmailer(%s): cannot fork",
109758925Seric 				e->e_to, m->m_name);
109854967Seric 			(void) close(mpvect[0]);
109954967Seric 			(void) close(mpvect[1]);
110054967Seric 			if (clever)
110154967Seric 			{
110254967Seric 				(void) close(rpvect[0]);
110354967Seric 				(void) close(rpvect[1]);
110454967Seric 			}
110557387Seric 			if (tTd(11, 1))
110657387Seric 				printf("openmailer: NULL\n");
110758820Seric 			rcode = EX_OSERR;
110858820Seric 			goto give_up;
11094863Seric 		}
111054967Seric 		else if (pid == 0)
111154967Seric 		{
111254967Seric 			int i;
111356678Seric 			int saveerrno;
111458675Seric 			char **ep;
111558675Seric 			char *env[MAXUSERENVIRON];
111658675Seric 			extern char **environ;
111754967Seric 			extern int DtableSize;
111815772Seric 
111954967Seric 			/* child -- set up input & exec mailer */
112064035Seric 			(void) setsignal(SIGINT, SIG_IGN);
112164035Seric 			(void) setsignal(SIGHUP, SIG_IGN);
112264035Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11234709Seric 
112464145Seric 			/* reset user and group */
112564145Seric 			if (!bitnset(M_RESTR, m->m_flags))
112664145Seric 			{
112764145Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
112864145Seric 				{
112964145Seric 					(void) initgroups(DefUser, DefGid);
113064145Seric 					(void) setuid(DefUid);
113164145Seric 				}
113264145Seric 				else
113364145Seric 				{
113464145Seric 					(void) initgroups(ctladdr->q_ruser?
113564145Seric 						ctladdr->q_ruser: ctladdr->q_user,
113664145Seric 						ctladdr->q_gid);
113764145Seric 					(void) setuid(ctladdr->q_uid);
113864145Seric 				}
113964145Seric 			}
114064145Seric 
114164145Seric 			if (tTd(11, 2))
114264145Seric 				printf("openmailer: running as r/euid=%d/%d\n",
114364145Seric 					getuid(), geteuid());
114464145Seric 
114558935Seric 			/* move into some "safe" directory */
114658935Seric 			if (m->m_execdir != NULL)
114758935Seric 			{
114858935Seric 				char *p, *q;
114958935Seric 				char buf[MAXLINE];
115058935Seric 
115158935Seric 				for (p = m->m_execdir; p != NULL; p = q)
115258935Seric 				{
115358935Seric 					q = strchr(p, ':');
115458935Seric 					if (q != NULL)
115558935Seric 						*q = '\0';
115658935Seric 					expand(p, buf, &buf[sizeof buf] - 1, e);
115758935Seric 					if (q != NULL)
115858935Seric 						*q++ = ':';
115958935Seric 					if (tTd(11, 20))
116058935Seric 						printf("openmailer: trydir %s\n",
116158935Seric 							buf);
116258935Seric 					if (buf[0] != '\0' && chdir(buf) >= 0)
116358935Seric 						break;
116458935Seric 				}
116558935Seric 			}
116658935Seric 
116754967Seric 			/* arrange to filter std & diag output of command */
116854967Seric 			if (clever)
116954967Seric 			{
117054967Seric 				(void) close(rpvect[0]);
117158852Seric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
117258852Seric 				{
117358925Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
117458925Seric 						e->e_to, m->m_name, rpvect[1]);
117558852Seric 					_exit(EX_OSERR);
117658852Seric 				}
117754967Seric 				(void) close(rpvect[1]);
117854967Seric 			}
117954967Seric 			else if (OpMode == MD_SMTP || HoldErrs)
118054967Seric 			{
118154967Seric 				/* put mailer output in transcript */
118258852Seric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
118358852Seric 				{
118458925Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
118558925Seric 						e->e_to, m->m_name,
118658852Seric 						fileno(e->e_xfp));
118758852Seric 					_exit(EX_OSERR);
118858852Seric 				}
118954967Seric 			}
119058852Seric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
119158852Seric 			{
119258925Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
119358925Seric 					e->e_to, m->m_name);
119458852Seric 				_exit(EX_OSERR);
119558852Seric 			}
11964709Seric 
119754967Seric 			/* arrange to get standard input */
119854967Seric 			(void) close(mpvect[1]);
119958731Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
12004417Seric 			{
120158925Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
120258925Seric 					e->e_to, m->m_name, mpvect[0]);
120354967Seric 				_exit(EX_OSERR);
12044417Seric 			}
120554967Seric 			(void) close(mpvect[0]);
12069370Seric 
120754967Seric 			/* arrange for all the files to be closed */
120854967Seric 			for (i = 3; i < DtableSize; i++)
120954967Seric 			{
121054967Seric 				register int j;
121164145Seric 
121254967Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
121364145Seric 					(void) fcntl(i, F_SETFD, j | 1);
121454967Seric 			}
12152774Seric 
121658675Seric 			/* set up the mailer environment */
121758675Seric 			i = 0;
121858675Seric 			env[i++] = "AGENT=sendmail";
121958675Seric 			for (ep = environ; *ep != NULL; ep++)
122058675Seric 			{
122158675Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
122258675Seric 					env[i++] = *ep;
122358675Seric 			}
122458675Seric 			env[i++] = NULL;
122558675Seric 
122654967Seric 			/* try to execute the mailer */
122758820Seric 			execve(m->m_mailer, pv, env);
122856678Seric 			saveerrno = errno;
122954967Seric 			syserr("Cannot exec %s", m->m_mailer);
123060008Seric 			if (m == LocalMailer || transienterror(saveerrno))
123160008Seric 				_exit(EX_OSERR);
123254967Seric 			_exit(EX_UNAVAILABLE);
123351835Seric 		}
123454967Seric 
123554967Seric 		/*
123654967Seric 		**  Set up return value.
123754967Seric 		*/
123854967Seric 
123954967Seric 		mci = (MCI *) xalloc(sizeof *mci);
124054993Seric 		bzero((char *) mci, sizeof *mci);
124154967Seric 		mci->mci_mailer = m;
124254967Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
124354993Seric 		mci->mci_pid = pid;
124454967Seric 		(void) close(mpvect[0]);
124554967Seric 		mci->mci_out = fdopen(mpvect[1], "w");
124664724Seric 		if (mci->mci_out == NULL)
124764724Seric 		{
124864724Seric 			syserr("deliver: cannot create mailer output channel, fd=%d",
124964724Seric 				mpvect[1]);
125064724Seric 			(void) close(mpvect[1]);
125164724Seric 			if (clever)
125264724Seric 			{
125364724Seric 				(void) close(rpvect[0]);
125464724Seric 				(void) close(rpvect[1]);
125564724Seric 			}
125664724Seric 			rcode = EX_OSERR;
125764724Seric 			goto give_up;
125864724Seric 		}
125954967Seric 		if (clever)
126054967Seric 		{
126154967Seric 			(void) close(rpvect[1]);
126254967Seric 			mci->mci_in = fdopen(rpvect[0], "r");
126364724Seric 			if (mci->mci_in == NULL)
126464724Seric 			{
126564724Seric 				syserr("deliver: cannot create mailer input channel, fd=%d",
126664724Seric 					mpvect[1]);
126764724Seric 				(void) close(rpvect[0]);
126864724Seric 				fclose(mci->mci_out);
126964724Seric 				mci->mci_out = NULL;
127064724Seric 				rcode = EX_OSERR;
127164724Seric 				goto give_up;
127264724Seric 			}
127354967Seric 		}
127454967Seric 		else
127554967Seric 		{
127654967Seric 			mci->mci_flags |= MCIF_TEMP;
127754967Seric 			mci->mci_in = NULL;
127854967Seric 		}
1279294Seric 	}
1280294Seric 
12814709Seric 	/*
128254967Seric 	**  If we are in SMTP opening state, send initial protocol.
12834709Seric 	*/
12844709Seric 
128554967Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12864863Seric 	{
128754967Seric 		smtpinit(m, mci, e);
128853738Seric 	}
128957387Seric 	if (tTd(11, 1))
129057387Seric 	{
129157387Seric 		printf("openmailer: ");
129264731Seric 		mci_dump(mci, FALSE);
129357387Seric 	}
1294294Seric 
129558820Seric 	if (mci->mci_state != MCIS_OPEN)
129658820Seric 	{
129758820Seric 		/* couldn't open the mailer */
129858820Seric 		rcode = mci->mci_exitstat;
129958820Seric 		errno = mci->mci_errno;
130063753Seric #ifdef NAMED_BIND
130163753Seric 		h_errno = mci->mci_herrno;
130263753Seric #endif
130358820Seric 		if (rcode == EX_OK)
130458820Seric 		{
130558820Seric 			/* shouldn't happen */
130658820Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
130758820Seric 				rcode, mci->mci_state, firstsig);
130858820Seric 			rcode = EX_SOFTWARE;
130958820Seric 		}
131059993Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
131159958Seric 		{
131259958Seric 			/* try next MX site */
131359958Seric 			goto tryhost;
131459958Seric 		}
131558820Seric 	}
131658820Seric 	else if (!clever)
131758820Seric 	{
131858820Seric 		/*
131958820Seric 		**  Format and send message.
132058820Seric 		*/
132158820Seric 
132258820Seric 		putfromline(mci->mci_out, m, e);
132358820Seric 		(*e->e_puthdr)(mci->mci_out, m, e);
132458820Seric 		putline("\n", mci->mci_out, m);
132559730Seric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
132658820Seric 
132758820Seric 		/* get the exit status */
132858820Seric 		rcode = endmailer(mci, e, pv);
132958820Seric 	}
133058820Seric 	else
133158820Seric #ifdef SMTP
133258820Seric 	{
133358820Seric 		/*
133458820Seric 		**  Send the MAIL FROM: protocol
133558820Seric 		*/
133658820Seric 
133758820Seric 		rcode = smtpmailfrom(m, mci, e);
133858820Seric 		if (rcode == EX_OK)
133958820Seric 		{
134058820Seric 			register char *t = tobuf;
134158820Seric 			register int i;
134258820Seric 
134358820Seric 			/* send the recipient list */
134458820Seric 			tobuf[0] = '\0';
134558820Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
134658820Seric 			{
134758820Seric 				e->e_to = to->q_paddr;
134858820Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
134958820Seric 				{
135058820Seric 					markfailure(e, to, i);
135164771Seric 					giveresponse(i, m, mci, ctladdr, e);
135258820Seric 				}
135358820Seric 				else
135458820Seric 				{
135558820Seric 					*t++ = ',';
135658820Seric 					for (p = to->q_paddr; *p; *t++ = *p++)
135758820Seric 						continue;
135864372Seric 					*t = '\0';
135958820Seric 				}
136058820Seric 			}
136158820Seric 
136258820Seric 			/* now send the data */
136358820Seric 			if (tobuf[0] == '\0')
136458820Seric 			{
136558820Seric 				rcode = EX_OK;
136658820Seric 				e->e_to = NULL;
136758820Seric 				if (bitset(MCIF_CACHED, mci->mci_flags))
136858820Seric 					smtprset(m, mci, e);
136958820Seric 			}
137058820Seric 			else
137158820Seric 			{
137258820Seric 				e->e_to = tobuf + 1;
137358820Seric 				rcode = smtpdata(m, mci, e);
137458820Seric 			}
137558820Seric 
137658820Seric 			/* now close the connection */
137758820Seric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
137858820Seric 				smtpquit(m, mci, e);
137958820Seric 		}
138058820Seric 		if (rcode != EX_OK && *curhost != '\0')
138158820Seric 		{
138258820Seric 			/* try next MX site */
138358820Seric 			goto tryhost;
138458820Seric 		}
138558820Seric 	}
138658820Seric #else /* not SMTP */
138758820Seric 	{
138858820Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
138958820Seric 		rcode = EX_CONFIG;
139058820Seric 		goto give_up;
139158820Seric 	}
139258820Seric #endif /* SMTP */
139358820Seric #ifdef NAMED_BIND
139458820Seric 	if (ConfigLevel < 2)
139558820Seric 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
139658820Seric #endif
139758820Seric 
139858820Seric 	/* arrange a return receipt if requested */
139964718Seric 	if (rcode == EX_OK && e->e_receiptto != NULL &&
140064718Seric 	    bitnset(M_LOCALMAILER, m->m_flags))
140158820Seric 	{
140258820Seric 		e->e_flags |= EF_SENDRECEIPT;
140358820Seric 		/* do we want to send back more info? */
140458820Seric 	}
140558820Seric 
140658820Seric 	/*
140758820Seric 	**  Do final status disposal.
140858820Seric 	**	We check for something in tobuf for the SMTP case.
140958820Seric 	**	If we got a temporary failure, arrange to queue the
141058820Seric 	**		addressees.
141158820Seric 	*/
141258820Seric 
141358820Seric   give_up:
141458820Seric 	if (tobuf[0] != '\0')
141564771Seric 		giveresponse(rcode, m, mci, ctladdr, e);
141658820Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
141758820Seric 	{
141858820Seric 		if (rcode != EX_OK)
141958820Seric 			markfailure(e, to, rcode);
142058820Seric 		else
142158820Seric 		{
142258820Seric 			to->q_flags |= QSENT;
142358820Seric 			e->e_nsent++;
142464718Seric 			if (e->e_receiptto != NULL &&
142564718Seric 			    bitnset(M_LOCALMAILER, m->m_flags))
142664718Seric 			{
142764718Seric 				fprintf(e->e_xfp, "%s... Successfully delivered\n",
142864718Seric 					to->q_paddr);
142964718Seric 			}
143058820Seric 		}
143158820Seric 	}
143258820Seric 
143358820Seric 	/*
143458820Seric 	**  Restore state and return.
143558820Seric 	*/
143658820Seric 
143764401Seric #ifdef XDEBUG
143864401Seric 	{
143964401Seric 		char wbuf[MAXLINE];
144064401Seric 
144164401Seric 		/* make absolutely certain 0, 1, and 2 are in use */
144264554Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
144364554Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
144464554Seric 			m->m_name);
144564401Seric 		checkfd012(wbuf);
144664401Seric 	}
144764401Seric #endif
144864401Seric 
144958820Seric 	errno = 0;
145058820Seric 	define('g', (char *) NULL, e);
145158820Seric 	return (rcode);
1452294Seric }
1453294Seric /*
145458820Seric **  MARKFAILURE -- mark a failure on a specific address.
145558820Seric **
145658820Seric **	Parameters:
145758820Seric **		e -- the envelope we are sending.
145858820Seric **		q -- the address to mark.
145958820Seric **		rcode -- the code signifying the particular failure.
146058820Seric **
146158820Seric **	Returns:
146258820Seric **		none.
146358820Seric **
146458820Seric **	Side Effects:
146558820Seric **		marks the address (and possibly the envelope) with the
146658820Seric **			failure so that an error will be returned or
146758820Seric **			the message will be queued, as appropriate.
146858820Seric */
146958820Seric 
147058820Seric markfailure(e, q, rcode)
147158820Seric 	register ENVELOPE *e;
147258820Seric 	register ADDRESS *q;
147358820Seric 	int rcode;
147458820Seric {
147558820Seric 	char buf[MAXLINE];
147658820Seric 
147758820Seric 	if (rcode == EX_OK)
147858820Seric 		return;
147963753Seric 	else if (rcode == EX_TEMPFAIL)
148063753Seric 		q->q_flags |= QQUEUEUP;
148163753Seric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
148258820Seric 		q->q_flags |= QBADADDR;
148358820Seric }
148458820Seric /*
148558820Seric **  ENDMAILER -- Wait for mailer to terminate.
148658820Seric **
148758820Seric **	We should never get fatal errors (e.g., segmentation
148858820Seric **	violation), so we report those specially.  For other
148958820Seric **	errors, we choose a status message (into statmsg),
149058820Seric **	and if it represents an error, we print it.
149158820Seric **
149258820Seric **	Parameters:
149358820Seric **		pid -- pid of mailer.
149458820Seric **		e -- the current envelope.
149558820Seric **		pv -- the parameter vector that invoked the mailer
149658820Seric **			(for error messages).
149758820Seric **
149858820Seric **	Returns:
149958820Seric **		exit code of mailer.
150058820Seric **
150158820Seric **	Side Effects:
150258820Seric **		none.
150358820Seric */
150458820Seric 
150558820Seric endmailer(mci, e, pv)
150658820Seric 	register MCI *mci;
150758820Seric 	register ENVELOPE *e;
150858820Seric 	char **pv;
150958820Seric {
151058820Seric 	int st;
151158820Seric 
151258820Seric 	/* close any connections */
151358820Seric 	if (mci->mci_in != NULL)
151458820Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
151558820Seric 	if (mci->mci_out != NULL)
151658820Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
151758820Seric 	mci->mci_in = mci->mci_out = NULL;
151858820Seric 	mci->mci_state = MCIS_CLOSED;
151958820Seric 
152058820Seric 	/* in the IPC case there is nothing to wait for */
152158820Seric 	if (mci->mci_pid == 0)
152258820Seric 		return (EX_OK);
152358820Seric 
152458820Seric 	/* wait for the mailer process to die and collect status */
152558820Seric 	st = waitfor(mci->mci_pid);
152658820Seric 	if (st == -1)
152758820Seric 	{
152858820Seric 		syserr("endmailer %s: wait", pv[0]);
152958820Seric 		return (EX_SOFTWARE);
153058820Seric 	}
153158820Seric 
153264379Seric 	if (WIFEXITED(st))
153358820Seric 	{
153464379Seric 		/* normal death -- return status */
153564379Seric 		return (WEXITSTATUS(st));
153664379Seric 	}
153758820Seric 
153864379Seric 	/* it died a horrid death */
153964379Seric 	syserr("mailer %s died with signal %o", pv[0], st);
154058820Seric 
154164379Seric 	/* log the arguments */
154264379Seric 	if (e->e_xfp != NULL)
154364379Seric 	{
154464379Seric 		register char **av;
154558820Seric 
154664379Seric 		fprintf(e->e_xfp, "Arguments:");
154764379Seric 		for (av = pv; *av != NULL; av++)
154864379Seric 			fprintf(e->e_xfp, " %s", *av);
154964379Seric 		fprintf(e->e_xfp, "\n");
155058820Seric 	}
155158820Seric 
155264379Seric 	ExitStat = EX_TEMPFAIL;
155364379Seric 	return (EX_TEMPFAIL);
155458820Seric }
155558820Seric /*
1556294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
1557294Seric **
1558294Seric **	Parameters:
1559294Seric **		stat -- the status code from the mailer (high byte
1560294Seric **			only; core dumps must have been taken care of
1561294Seric **			already).
156258337Seric **		m -- the mailer info for this mailer.
156358337Seric **		mci -- the mailer connection info -- can be NULL if the
156458337Seric **			response is given before the connection is made.
156564771Seric **		ctladdr -- the controlling address for the recipient
156664771Seric **			address(es).
156758337Seric **		e -- the current envelope.
1568294Seric **
1569294Seric **	Returns:
15704082Seric **		none.
1571294Seric **
1572294Seric **	Side Effects:
15731518Seric **		Errors may be incremented.
1574294Seric **		ExitStat may be set.
1575294Seric */
1576294Seric 
157764771Seric giveresponse(stat, m, mci, ctladdr, e)
1578294Seric 	int stat;
15799370Seric 	register MAILER *m;
158058337Seric 	register MCI *mci;
158164771Seric 	ADDRESS *ctladdr;
158210105Seric 	ENVELOPE *e;
1583294Seric {
158460094Seric 	register const char *statmsg;
1585294Seric 	extern char *SysExMsg[];
1586294Seric 	register int i;
158736788Sbostic 	extern int N_SysEx;
158810105Seric 	char buf[MAXLINE];
1589294Seric 
15904315Seric 	/*
15914315Seric 	**  Compute status message from code.
15924315Seric 	*/
15934315Seric 
1594294Seric 	i = stat - EX__BASE;
15959370Seric 	if (stat == 0)
159658852Seric 	{
15979370Seric 		statmsg = "250 Sent";
159858916Seric 		if (e->e_statmsg != NULL)
159958852Seric 		{
160058916Seric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
160158852Seric 			statmsg = buf;
160258852Seric 		}
160358852Seric 	}
16049370Seric 	else if (i < 0 || i > N_SysEx)
16059370Seric 	{
16069370Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
16079370Seric 		stat = EX_UNAVAILABLE;
16089370Seric 		statmsg = buf;
16099370Seric 	}
161010105Seric 	else if (stat == EX_TEMPFAIL)
161110105Seric 	{
161258664Seric 		(void) strcpy(buf, SysExMsg[i] + 1);
161336788Sbostic #ifdef NAMED_BIND
161425527Smiriam 		if (h_errno == TRY_AGAIN)
161563993Seric 			statmsg = errstring(h_errno+E_DNSBASE);
161621061Seric 		else
161736788Sbostic #endif
161821061Seric 		{
161925527Smiriam 			if (errno != 0)
162025527Smiriam 				statmsg = errstring(errno);
162125527Smiriam 			else
162225527Smiriam 			{
162321061Seric #ifdef SMTP
162425527Smiriam 				extern char SmtpError[];
162521061Seric 
162625527Smiriam 				statmsg = SmtpError;
162756795Seric #else /* SMTP */
162825527Smiriam 				statmsg = NULL;
162956795Seric #endif /* SMTP */
163025527Smiriam 			}
163121061Seric 		}
163221061Seric 		if (statmsg != NULL && statmsg[0] != '\0')
163321061Seric 		{
163410124Seric 			(void) strcat(buf, ": ");
163521061Seric 			(void) strcat(buf, statmsg);
163610105Seric 		}
163710105Seric 		statmsg = buf;
163810105Seric 	}
163963753Seric #ifdef NAMED_BIND
164063753Seric 	else if (stat == EX_NOHOST && h_errno != 0)
164163753Seric 	{
164263993Seric 		statmsg = errstring(h_errno + E_DNSBASE);
164363753Seric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
164463753Seric 		statmsg = buf;
164563753Seric 	}
164663753Seric #endif
1647294Seric 	else
164821061Seric 	{
1649294Seric 		statmsg = SysExMsg[i];
165058664Seric 		if (*statmsg++ == ':')
165158664Seric 		{
165258664Seric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
165358664Seric 			statmsg = buf;
165458664Seric 		}
165521061Seric 	}
16569370Seric 
16579370Seric 	/*
16589370Seric 	**  Print the message as appropriate
16599370Seric 	*/
16609370Seric 
166110105Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
166264718Seric 	{
166364718Seric 		extern char MsgBuf[];
166464718Seric 
166558524Seric 		message(&statmsg[4], errstring(errno));
166664718Seric 		if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
166764718Seric 			fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
166864718Seric 	}
1669294Seric 	else
1670294Seric 	{
16711518Seric 		Errors++;
167258524Seric 		usrerr(statmsg, errstring(errno));
1673294Seric 	}
1674294Seric 
1675294Seric 	/*
1676294Seric 	**  Final cleanup.
1677294Seric 	**	Log a record of the transaction.  Compute the new
1678294Seric 	**	ExitStat -- if we already had an error, stick with
1679294Seric 	**	that.
1680294Seric 	*/
1681294Seric 
168258020Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
168364771Seric 		logdelivery(m, mci, &statmsg[4], ctladdr, e);
16847858Seric 
16854621Seric 	if (stat != EX_TEMPFAIL)
16864621Seric 		setstat(stat);
168710105Seric 	if (stat != EX_OK)
168810105Seric 	{
168910105Seric 		if (e->e_message != NULL)
169010105Seric 			free(e->e_message);
169110105Seric 		e->e_message = newstr(&statmsg[4]);
169210105Seric 	}
169310124Seric 	errno = 0;
169436788Sbostic #ifdef NAMED_BIND
169525527Smiriam 	h_errno = 0;
169636788Sbostic #endif
1697294Seric }
1698294Seric /*
16998496Seric **  LOGDELIVERY -- log the delivery in the system log
17008496Seric **
17018496Seric **	Parameters:
170258337Seric **		m -- the mailer info.  Can be NULL for initial queue.
170358337Seric **		mci -- the mailer connection info -- can be NULL if the
170458337Seric **			log is occuring when no connection is active.
170558337Seric **		stat -- the message to print for the status.
170664771Seric **		ctladdr -- the controlling address for the to list.
170758337Seric **		e -- the current envelope.
17088496Seric **
17098496Seric **	Returns:
17108496Seric **		none
17118496Seric **
17128496Seric **	Side Effects:
17138496Seric **		none
17148496Seric */
17158496Seric 
171664771Seric logdelivery(m, mci, stat, ctladdr, e)
171758337Seric 	MAILER *m;
171858337Seric 	register MCI *mci;
17198496Seric 	char *stat;
172064771Seric 	ADDRESS *ctladdr;
172154967Seric 	register ENVELOPE *e;
17228496Seric {
172358343Seric # ifdef LOG
172464771Seric 	register char *bp;
172558418Seric 	char buf[512];
17268496Seric 
172764771Seric 	bp = buf;
172864771Seric 	if (ctladdr != NULL)
172964771Seric 	{
173064771Seric 		strcpy(bp, ", ctladdr=");
173164771Seric 		strcat(bp, ctladdr->q_paddr);
173264771Seric 		bp += strlen(bp);
173364771Seric 		if (bitset(QGOODUID, ctladdr->q_flags))
173464771Seric 		{
173564771Seric 			(void) sprintf(bp, " (%d/%d)",
173664771Seric 					ctladdr->q_uid, ctladdr->q_gid);
173764771Seric 			bp += strlen(bp);
173864771Seric 		}
173964771Seric 	}
174058337Seric 
174164771Seric 	(void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
174264771Seric 	bp += strlen(bp);
174364771Seric 
174458513Seric 	if (m != NULL)
174558305Seric 	{
174664771Seric 		(void) strcpy(bp, ", mailer=");
174764771Seric 		(void) strcat(bp, m->m_name);
174864771Seric 		bp += strlen(bp);
174958305Seric 	}
175058513Seric 
175158513Seric 	if (mci != NULL && mci->mci_host != NULL)
175258305Seric 	{
175358305Seric # ifdef DAEMON
175458755Seric 		extern SOCKADDR CurHostAddr;
175558513Seric # endif
175658305Seric 
175764771Seric 		(void) strcpy(bp, ", relay=");
175864771Seric 		(void) strcat(bp, mci->mci_host);
175958513Seric 
176058513Seric # ifdef DAEMON
176164771Seric 		(void) strcat(bp, " (");
176264771Seric 		(void) strcat(bp, anynet_ntoa(&CurHostAddr));
176364771Seric 		(void) strcat(bp, ")");
176458305Seric # endif
176558305Seric 	}
176658343Seric 	else
176758513Seric 	{
176858513Seric 		char *p = macvalue('h', e);
176958343Seric 
177058513Seric 		if (p != NULL && p[0] != '\0')
177158513Seric 		{
177264771Seric 			(void) strcpy(bp, ", relay=");
177364771Seric 			(void) strcat(bp, p);
177458513Seric 		}
177558513Seric 	}
177658418Seric 
177764771Seric 	syslog(LOG_INFO, "%s: to=%s%s, stat=%s",
177858418Seric 	       e->e_id, e->e_to, buf, stat);
177956795Seric # endif /* LOG */
17808496Seric }
17818496Seric /*
17826974Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
1783294Seric **
17846974Seric **	This can be made an arbitrary message separator by changing $l
1785294Seric **
178616150Seric **	One of the ugliest hacks seen by human eyes is contained herein:
178716150Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
178816150Seric **	does a well-meaning programmer such as myself have to deal with
178916150Seric **	this kind of antique garbage????
17906974Seric **
1791294Seric **	Parameters:
17926974Seric **		fp -- the file to output to.
17936974Seric **		m -- the mailer describing this entry.
1794294Seric **
1795294Seric **	Returns:
17966974Seric **		none
1797294Seric **
1798294Seric **	Side Effects:
17996974Seric **		outputs some text to fp.
1800294Seric */
1801294Seric 
180254967Seric putfromline(fp, m, e)
18036974Seric 	register FILE *fp;
18046974Seric 	register MAILER *m;
180554967Seric 	ENVELOPE *e;
1806294Seric {
180758050Seric 	char *template = "\201l\n";
18086974Seric 	char buf[MAXLINE];
1809294Seric 
181010682Seric 	if (bitnset(M_NHDR, m->m_flags))
18116974Seric 		return;
18124315Seric 
18136974Seric # ifdef UGLYUUCP
181410682Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
18154205Seric 	{
181612223Seric 		char *bang;
181712223Seric 		char xbuf[MAXLINE];
18186041Seric 
181958680Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
182056795Seric 		bang = strchr(buf, '!');
18216974Seric 		if (bang == NULL)
182264370Seric 		{
182364370Seric 			errno = 0;
182464370Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
182564370Seric 		}
18265099Seric 		else
18279370Seric 		{
182812223Seric 			*bang++ = '\0';
182958050Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
183012223Seric 			template = xbuf;
18319370Seric 		}
18326974Seric 	}
183356795Seric # endif /* UGLYUUCP */
183454967Seric 	expand(template, buf, &buf[sizeof buf - 1], e);
183510168Seric 	putline(buf, fp, m);
18365981Seric }
18375981Seric /*
18386974Seric **  PUTBODY -- put the body of a message.
18396974Seric **
18406974Seric **	Parameters:
18416974Seric **		fp -- file to output onto.
184210168Seric **		m -- a mailer descriptor to control output format.
18439538Seric **		e -- the envelope to put out.
184459730Seric **		separator -- if non-NULL, a message separator that must
184559730Seric **			not be permitted in the resulting message.
18466974Seric **
18476974Seric **	Returns:
18486974Seric **		none.
18496974Seric **
18506974Seric **	Side Effects:
18516974Seric **		The message is written onto fp.
18526974Seric */
18536974Seric 
185459730Seric putbody(fp, m, e, separator)
18556974Seric 	FILE *fp;
18569370Seric 	MAILER *m;
18579538Seric 	register ENVELOPE *e;
185859730Seric 	char *separator;
18596974Seric {
186010168Seric 	char buf[MAXLINE];
18616974Seric 
18626974Seric 	/*
18636974Seric 	**  Output the body of the message
18646974Seric 	*/
18656974Seric 
18669538Seric 	if (e->e_dfp == NULL)
18676974Seric 	{
18689538Seric 		if (e->e_df != NULL)
18699538Seric 		{
18709538Seric 			e->e_dfp = fopen(e->e_df, "r");
18719538Seric 			if (e->e_dfp == NULL)
187240931Srick 				syserr("putbody: Cannot open %s for %s from %s",
187364118Seric 				e->e_df, e->e_to, e->e_from.q_paddr);
18749538Seric 		}
18759538Seric 		else
187610168Seric 			putline("<<< No Message Collected >>>", fp, m);
18779538Seric 	}
18789538Seric 	if (e->e_dfp != NULL)
18799538Seric 	{
18809538Seric 		rewind(e->e_dfp);
188110168Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
188216875Seric 		{
188316875Seric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
188440995Sbostic 			    strncmp(buf, "From ", 5) == 0)
188523102Seric 				(void) putc('>', fp);
188659730Seric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
188759730Seric 			{
188859730Seric 				/* possible separator */
188959730Seric 				int sl = strlen(separator);
189059730Seric 
189159730Seric 				if (strncmp(&buf[2], separator, sl) == 0)
189259730Seric 					(void) putc(' ', fp);
189359730Seric 			}
189410168Seric 			putline(buf, fp, m);
189516875Seric 		}
18966974Seric 
18979538Seric 		if (ferror(e->e_dfp))
18986974Seric 		{
189964718Seric 			syserr("putbody: %s: read error", e->e_df);
19006974Seric 			ExitStat = EX_IOERR;
19016974Seric 		}
19026974Seric 	}
19036974Seric 
190459542Seric 	/* some mailers want extra blank line at end of message */
190559542Seric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
190659542Seric 		putline("", fp, m);
190759542Seric 
19086974Seric 	(void) fflush(fp);
19096974Seric 	if (ferror(fp) && errno != EPIPE)
19106974Seric 	{
19116974Seric 		syserr("putbody: write error");
19126974Seric 		ExitStat = EX_IOERR;
19136974Seric 	}
19146974Seric 	errno = 0;
19156974Seric }
19166974Seric /*
1917294Seric **  MAILFILE -- Send a message to a file.
1918294Seric **
19194327Seric **	If the file has the setuid/setgid bits set, but NO execute
19204327Seric **	bits, sendmail will try to become the owner of that file
19214327Seric **	rather than the real user.  Obviously, this only works if
19224327Seric **	sendmail runs as root.
19234327Seric **
19249370Seric **	This could be done as a subordinate mailer, except that it
19259370Seric **	is used implicitly to save messages in ~/dead.letter.  We
19269370Seric **	view this as being sufficiently important as to include it
19279370Seric **	here.  For example, if the system is dying, we shouldn't have
19289370Seric **	to create another process plus some pipes to save the message.
19299370Seric **
1930294Seric **	Parameters:
1931294Seric **		filename -- the name of the file to send to.
19324397Seric **		ctladdr -- the controlling address header -- includes
19334397Seric **			the userid/groupid to be when sending.
1934294Seric **
1935294Seric **	Returns:
1936294Seric **		The exit code associated with the operation.
1937294Seric **
1938294Seric **	Side Effects:
1939294Seric **		none.
1940294Seric */
1941294Seric 
194254967Seric mailfile(filename, ctladdr, e)
1943294Seric 	char *filename;
19444397Seric 	ADDRESS *ctladdr;
194554967Seric 	register ENVELOPE *e;
1946294Seric {
1947294Seric 	register FILE *f;
19484214Seric 	register int pid;
194953751Seric 	int mode;
1950294Seric 
195159267Seric 	if (tTd(11, 1))
195259267Seric 	{
195359267Seric 		printf("mailfile %s\n  ctladdr=", filename);
195459267Seric 		printaddr(ctladdr, FALSE);
195559267Seric 	}
195659267Seric 
195763753Seric 	if (e->e_xfp != NULL)
195863753Seric 		fflush(e->e_xfp);
195963753Seric 
19604214Seric 	/*
19614214Seric 	**  Fork so we can change permissions here.
19624214Seric 	**	Note that we MUST use fork, not vfork, because of
19634214Seric 	**	the complications of calling subroutines, etc.
19644214Seric 	*/
19654067Seric 
19664214Seric 	DOFORK(fork);
19674214Seric 
19684214Seric 	if (pid < 0)
19694214Seric 		return (EX_OSERR);
19704214Seric 	else if (pid == 0)
19714214Seric 	{
19724214Seric 		/* child -- actually write to file */
19734327Seric 		struct stat stb;
19744327Seric 
197564035Seric 		(void) setsignal(SIGINT, SIG_DFL);
197664035Seric 		(void) setsignal(SIGHUP, SIG_DFL);
197764035Seric 		(void) setsignal(SIGTERM, SIG_DFL);
197823102Seric 		(void) umask(OldUmask);
197952673Seric 
19804327Seric 		if (stat(filename, &stb) < 0)
198159745Seric 			stb.st_mode = FileMode;
198253751Seric 		mode = stb.st_mode;
198352673Seric 
198452673Seric 		/* limit the errors to those actually caused in the child */
198552673Seric 		errno = 0;
198652673Seric 		ExitStat = EX_OK;
198752673Seric 
19884327Seric 		if (bitset(0111, stb.st_mode))
19894327Seric 			exit(EX_CANTCREAT);
1990*64823Seric 		if (ctladdr != NULL)
199153751Seric 		{
199253751Seric 			/* ignore setuid and setgid bits */
199353751Seric 			mode &= ~(S_ISGID|S_ISUID);
199453751Seric 		}
199553751Seric 
199640931Srick 		/* we have to open the dfile BEFORE setuid */
199753751Seric 		if (e->e_dfp == NULL && e->e_df != NULL)
199840931Srick 		{
199940931Srick 			e->e_dfp = fopen(e->e_df, "r");
200052673Seric 			if (e->e_dfp == NULL)
200152673Seric 			{
200240931Srick 				syserr("mailfile: Cannot open %s for %s from %s",
200364118Seric 					e->e_df, e->e_to, e->e_from.q_paddr);
200440931Srick 			}
200540931Srick 		}
200640931Srick 
200753751Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
20084417Seric 		{
2009*64823Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
201052673Seric 			{
201140972Sbostic 				(void) initgroups(DefUser, DefGid);
201252673Seric 			}
201352673Seric 			else
201452673Seric 			{
201553751Seric 				(void) initgroups(ctladdr->q_ruser ?
201653751Seric 					ctladdr->q_ruser : ctladdr->q_user,
201740972Sbostic 					ctladdr->q_gid);
201840972Sbostic 			}
20194417Seric 		}
202053751Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
20214417Seric 		{
2022*64823Seric 			if (ctladdr == NULL || ctladdr->q_uid == 0)
20234417Seric 				(void) setuid(DefUid);
20244417Seric 			else
20254417Seric 				(void) setuid(ctladdr->q_uid);
20264417Seric 		}
202752673Seric 		FileName = filename;
202852673Seric 		LineNumber = 0;
202959745Seric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
20304214Seric 		if (f == NULL)
203152673Seric 		{
203264387Seric 			message("554 cannot open: %s", errstring(errno));
20334214Seric 			exit(EX_CANTCREAT);
203452673Seric 		}
20354214Seric 
203659275Seric 		putfromline(f, FileMailer, e);
203759275Seric 		(*e->e_puthdr)(f, FileMailer, e);
203859275Seric 		putline("\n", f, FileMailer);
203959730Seric 		(*e->e_putbody)(f, FileMailer, e, NULL);
204059275Seric 		putline("\n", f, FileMailer);
204152673Seric 		if (ferror(f))
204252673Seric 		{
204364387Seric 			message("451 I/O error: %s", errstring(errno));
204452673Seric 			setstat(EX_IOERR);
204552673Seric 		}
204658680Seric 		(void) xfclose(f, "mailfile", filename);
20474214Seric 		(void) fflush(stdout);
20484417Seric 
20496887Seric 		/* reset ISUID & ISGID bits for paranoid systems */
20504621Seric 		(void) chmod(filename, (int) stb.st_mode);
205152673Seric 		exit(ExitStat);
20524315Seric 		/*NOTREACHED*/
20534214Seric 	}
20544214Seric 	else
20554214Seric 	{
20564214Seric 		/* parent -- wait for exit status */
20579370Seric 		int st;
20584214Seric 
20599370Seric 		st = waitfor(pid);
206064379Seric 		if (WIFEXITED(st))
206164379Seric 			return (WEXITSTATUS(st));
206264379Seric 		else
206364387Seric 		{
206464387Seric 			syserr("child died on signal %d", st);
20659370Seric 			return (EX_UNAVAILABLE);
206664387Seric 		}
206740931Srick 		/*NOTREACHED*/
20684214Seric 	}
2069294Seric }
20704550Seric /*
207157454Seric **  HOSTSIGNATURE -- return the "signature" for a host.
207257454Seric **
207357454Seric **	The signature describes how we are going to send this -- it
207457454Seric **	can be just the hostname (for non-Internet hosts) or can be
207557454Seric **	an ordered list of MX hosts.
207657454Seric **
207757454Seric **	Parameters:
207857454Seric **		m -- the mailer describing this host.
207957454Seric **		host -- the host name.
208057454Seric **		e -- the current envelope.
208157454Seric **
208257454Seric **	Returns:
208357454Seric **		The signature for this host.
208457454Seric **
208557454Seric **	Side Effects:
208657454Seric **		Can tweak the symbol table.
208757454Seric */
208857454Seric 
208957454Seric char *
209057454Seric hostsignature(m, host, e)
209157454Seric 	register MAILER *m;
209257454Seric 	char *host;
209357454Seric 	ENVELOPE *e;
209457454Seric {
209557454Seric 	register char *p;
209657454Seric 	register STAB *s;
209757454Seric 	int i;
209857454Seric 	int len;
209957454Seric #ifdef NAMED_BIND
210057454Seric 	int nmx;
210157454Seric 	auto int rcode;
210259076Seric 	char *hp;
210359076Seric 	char *endp;
210459111Seric 	int oldoptions;
210557454Seric 	char *mxhosts[MAXMXHOSTS + 1];
210657454Seric #endif
210757454Seric 
210857454Seric 	/*
210957454Seric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
211057454Seric 	*/
211157454Seric 
211257454Seric 	p = m->m_mailer;
211357454Seric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
211457454Seric 	{
211557454Seric 		/* just an ordinary mailer */
211657454Seric 		return host;
211757454Seric 	}
211857454Seric 
211957454Seric 	/*
212057454Seric 	**  Look it up in the symbol table.
212157454Seric 	*/
212257454Seric 
212357454Seric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
212457454Seric 	if (s->s_hostsig != NULL)
212557454Seric 		return s->s_hostsig;
212657454Seric 
212757454Seric 	/*
212857454Seric 	**  Not already there -- create a signature.
212957454Seric 	*/
213057454Seric 
213157454Seric #ifdef NAMED_BIND
213259111Seric 	if (ConfigLevel < 2)
213359111Seric 	{
213459111Seric 		oldoptions = _res.options;
213559111Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
213659111Seric 	}
213759111Seric 
213859076Seric 	for (hp = host; hp != NULL; hp = endp)
213957454Seric 	{
214059076Seric 		endp = strchr(hp, ':');
214159076Seric 		if (endp != NULL)
214259076Seric 			*endp = '\0';
214357454Seric 
214459273Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
214557454Seric 
214659076Seric 		if (nmx <= 0)
214759076Seric 		{
214859076Seric 			register MCI *mci;
214957454Seric 
215059076Seric 			/* update the connection info for this host */
215159076Seric 			mci = mci_get(hp, m);
215259076Seric 			mci->mci_exitstat = rcode;
215359076Seric 			mci->mci_errno = errno;
215463753Seric #ifdef NAMED_BIND
215563753Seric 			mci->mci_herrno = h_errno;
215663753Seric #endif
215759076Seric 
215859076Seric 			/* and return the original host name as the signature */
215959076Seric 			nmx = 1;
216059076Seric 			mxhosts[0] = hp;
216159076Seric 		}
216259076Seric 
216359076Seric 		len = 0;
216459076Seric 		for (i = 0; i < nmx; i++)
216559076Seric 		{
216659076Seric 			len += strlen(mxhosts[i]) + 1;
216759076Seric 		}
216859076Seric 		if (s->s_hostsig != NULL)
216959076Seric 			len += strlen(s->s_hostsig) + 1;
217059076Seric 		p = xalloc(len);
217159076Seric 		if (s->s_hostsig != NULL)
217259076Seric 		{
217359076Seric 			(void) strcpy(p, s->s_hostsig);
217459076Seric 			free(s->s_hostsig);
217559076Seric 			s->s_hostsig = p;
217659076Seric 			p += strlen(p);
217757454Seric 			*p++ = ':';
217859076Seric 		}
217959076Seric 		else
218059076Seric 			s->s_hostsig = p;
218159076Seric 		for (i = 0; i < nmx; i++)
218259076Seric 		{
218359076Seric 			if (i != 0)
218459076Seric 				*p++ = ':';
218559076Seric 			strcpy(p, mxhosts[i]);
218659076Seric 			p += strlen(p);
218759076Seric 		}
218859076Seric 		if (endp != NULL)
218959076Seric 			*endp++ = ':';
219057454Seric 	}
219157454Seric 	makelower(s->s_hostsig);
219259111Seric 	if (ConfigLevel < 2)
219359111Seric 		_res.options = oldoptions;
219457454Seric #else
219557454Seric 	/* not using BIND -- the signature is just the host name */
219657454Seric 	s->s_hostsig = host;
219757454Seric #endif
219857454Seric 	if (tTd(17, 1))
219957454Seric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
220057454Seric 	return s->s_hostsig;
220157454Seric }
2202