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*64694Seric static char sccsid[] = "@(#)deliver.c	8.28 (Berkeley) 10/02/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_lockfp = NULL;
21458820Seric 			ee->e_df = NULL;
21558820Seric 			ee->e_errormode = EM_MAIL;
21658820Seric 			ee->e_sibling = splitenv;
21758820Seric 			splitenv = ee;
21858820Seric 
21958820Seric 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
22058820Seric 				if (q->q_owner == owner)
22158820Seric 					q->q_flags |= QDONTSEND;
22258820Seric 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
22358820Seric 				if (q->q_owner != owner)
22458820Seric 					q->q_flags |= QDONTSEND;
22558820Seric 
22658820Seric 			if (e->e_df != NULL && mode != SM_VERIFY)
22758820Seric 			{
22858820Seric 				ee->e_dfp = NULL;
22964086Seric 				ee->e_df = queuename(ee, 'd');
23064086Seric 				ee->e_df = newstr(ee->e_df);
23158820Seric 				if (link(e->e_df, ee->e_df) < 0)
23258820Seric 				{
23358820Seric 					syserr("sendall: link(%s, %s)",
23458820Seric 						e->e_df, ee->e_df);
23558820Seric 				}
23658820Seric 			}
23758820Seric 
23858820Seric 			if (mode != SM_VERIFY)
23958820Seric 				openxscript(ee);
24058820Seric #ifdef LOG
24158820Seric 			if (LogLevel > 4)
24258820Seric 				syslog(LOG_INFO, "%s: clone %s",
24358820Seric 					ee->e_id, e->e_id);
24458820Seric #endif
24558820Seric 		}
24658820Seric 	}
24758820Seric 
24858820Seric 	if (owner != NULL)
24958820Seric 	{
25058820Seric 		setsender(owner, e, NULL, TRUE);
25158820Seric 		if (tTd(13, 5))
25258820Seric 		{
25358820Seric 			printf("sendall(owner): QDONTSEND ");
25458820Seric 			printaddr(&e->e_from, FALSE);
25558820Seric 		}
25658820Seric 		e->e_from.q_flags |= QDONTSEND;
25758820Seric 		e->e_errormode = EM_MAIL;
25858820Seric 	}
25958820Seric 
26058916Seric # ifdef QUEUE
26158916Seric 	if ((mode == SM_QUEUE || mode == SM_FORK ||
26258916Seric 	     (mode != SM_VERIFY && SuperSafe)) &&
26358916Seric 	    !bitset(EF_INQUEUE, e->e_flags))
26458916Seric 	{
26558916Seric 		/* be sure everything is instantiated in the queue */
26658929Seric 		queueup(e, TRUE, announcequeueup);
26758916Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
26858929Seric 			queueup(ee, TRUE, announcequeueup);
26958916Seric 	}
27058916Seric #endif /* QUEUE */
27158916Seric 
27258820Seric 	if (splitenv != NULL)
27358820Seric 	{
27458820Seric 		if (tTd(13, 1))
27558820Seric 		{
27658820Seric 			printf("\nsendall: Split queue; remaining queue:\n");
27758820Seric 			printaddr(e->e_sendqueue, TRUE);
27858820Seric 		}
27958820Seric 
28058820Seric 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
28158820Seric 		{
28258820Seric 			CurEnv = ee;
28358820Seric 			sendenvelope(ee, mode);
28458820Seric 		}
28558820Seric 
28658820Seric 		CurEnv = e;
28758820Seric 	}
28858820Seric 	sendenvelope(e, mode);
28958820Seric 
29058820Seric 	for (; splitenv != NULL; splitenv = splitenv->e_sibling)
29158820Seric 		dropenvelope(splitenv);
29258820Seric }
29358820Seric 
29458820Seric sendenvelope(e, mode)
29558820Seric 	register ENVELOPE *e;
29658820Seric 	char mode;
29758820Seric {
29858916Seric 	bool oldverbose;
29958916Seric 	int pid;
30058820Seric 	register ADDRESS *q;
30164296Seric 	char *qf;
30264296Seric 	char *id;
30358820Seric 
30463839Seric 	/*
30563839Seric 	**  If we have had global, fatal errors, don't bother sending
30663839Seric 	**  the message at all if we are in SMTP mode.  Local errors
30763839Seric 	**  (e.g., a single address failing) will still cause the other
30863839Seric 	**  addresses to be sent.
30963839Seric 	*/
31063839Seric 
31163839Seric 	if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP)
31263839Seric 	{
31363839Seric 		e->e_flags |= EF_CLRQUEUE;
31463839Seric 		return;
31563839Seric 	}
31663839Seric 
31758916Seric 	oldverbose = Verbose;
31858916Seric 	switch (mode)
31958916Seric 	{
32058916Seric 	  case SM_VERIFY:
32158916Seric 		Verbose = TRUE;
32258916Seric 		break;
32358916Seric 
32458916Seric 	  case SM_QUEUE:
32558916Seric   queueonly:
32658916Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
32758916Seric 		return;
32858916Seric 
32958916Seric 	  case SM_FORK:
33058916Seric 		if (e->e_xfp != NULL)
33158916Seric 			(void) fflush(e->e_xfp);
33258916Seric 
33364035Seric # ifndef HASFLOCK
33458916Seric 		/*
33564296Seric 		**  Since fcntl locking has the interesting semantic that
33664296Seric 		**  the lock is owned by a process, not by an open file
33764296Seric 		**  descriptor, we have to flush this to the queue, and
33864296Seric 		**  then restart from scratch in the child.
33958916Seric 		*/
34058916Seric 
34164296Seric 		/* save id for future use */
34264296Seric 		id = e->e_id;
34358916Seric 
34464296Seric 		/* now drop the envelope in the parent */
34564296Seric 		e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
34664554Seric 		e->e_flags &= ~EF_FATALERRS;
34764296Seric 		dropenvelope(e);
34864296Seric 
34964296Seric 		/* and reacquire in the child */
35064296Seric 		(void) dowork(id, TRUE, FALSE, e);
35164296Seric 
35264296Seric 		return;
35364296Seric 
35464296Seric # else /* HASFLOCK */
35564296Seric 
35658916Seric 		pid = fork();
35758916Seric 		if (pid < 0)
35858916Seric 		{
35958916Seric 			goto queueonly;
36058916Seric 		}
36158916Seric 		else if (pid > 0)
36258916Seric 		{
36364310Seric 			/* be sure we leave the temp files to our child */
36464310Seric 			/* can't call unlockqueue to avoid unlink of xfp */
36564310Seric 			if (e->e_lockfp != NULL)
36664310Seric 				(void) xfclose(e->e_lockfp, "sendenvelope", "lockfp");
36764310Seric 			e->e_lockfp = NULL;
36864310Seric 
36964310Seric 			/* close any random open files in the envelope */
37064310Seric 			closexscript(e);
37164310Seric 			if (e->e_dfp != NULL)
37264310Seric 				(void) xfclose(e->e_dfp, "sendenvelope", e->e_df);
37364310Seric 			e->e_dfp = NULL;
37464310Seric 			e->e_id = e->e_df = NULL;
37558916Seric 			return;
37658916Seric 		}
37758916Seric 
37858916Seric 		/* double fork to avoid zombies */
37958916Seric 		if (fork() > 0)
38058916Seric 			exit(EX_OK);
38158916Seric 
38258916Seric 		/* be sure we are immune from the terminal */
38363839Seric 		disconnect(1, e);
38458916Seric 
38558916Seric 		/*
38658916Seric 		**  Close any cached connections.
38758916Seric 		**
38858916Seric 		**	We don't send the QUIT protocol because the parent
38958916Seric 		**	still knows about the connection.
39058916Seric 		**
39158916Seric 		**	This should only happen when delivering an error
39258916Seric 		**	message.
39358916Seric 		*/
39458916Seric 
39558916Seric 		mci_flush(FALSE, NULL);
39658916Seric 
39764296Seric # endif /* HASFLOCK */
39864296Seric 
39958916Seric 		break;
40058916Seric 	}
40158916Seric 
40258820Seric 	/*
40358820Seric 	**  Run through the list and send everything.
40463965Seric 	**
40563965Seric 	**	Set EF_GLOBALERRS so that error messages during delivery
40663965Seric 	**	result in returned mail.
40758820Seric 	*/
40858820Seric 
40958820Seric 	e->e_nsent = 0;
41063965Seric 	e->e_flags |= EF_GLOBALERRS;
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();
56658820Seric 	extern FILE *fdopen();
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;
5846974Seric 
5857672Seric 	if (tTd(10, 1))
5863233Seric 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
5876974Seric 			m->m_mno, host, to->q_user);
588294Seric 
589294Seric 	/*
5905903Seric 	**  If this mailer is expensive, and if we don't want to make
5915903Seric 	**  connections now, just mark these addresses and return.
5925903Seric 	**	This is useful if we want to batch connections to
5935903Seric 	**	reduce load.  This will cause the messages to be
5945903Seric 	**	queued up, and a daemon will come along to send the
5955903Seric 	**	messages later.
5965903Seric 	**		This should be on a per-mailer basis.
5975903Seric 	*/
5985903Seric 
59964658Seric 	if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose)
6005903Seric 	{
6015903Seric 		for (; to != NULL; to = to->q_next)
6028431Seric 		{
60358680Seric 			if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
60458247Seric 			    to->q_mailer != m)
6058431Seric 				continue;
60663853Seric 			to->q_flags |= QQUEUEUP;
6079370Seric 			e->e_to = to->q_paddr;
60858151Seric 			message("queued");
60958020Seric 			if (LogLevel > 8)
61058337Seric 				logdelivery(m, NULL, "queued", e);
6118431Seric 		}
6129370Seric 		e->e_to = NULL;
6135903Seric 		return (0);
6145903Seric 	}
6155903Seric 
6165903Seric 	/*
6173233Seric 	**  Do initial argv setup.
6183233Seric 	**	Insert the mailer name.  Notice that $x expansion is
6193233Seric 	**	NOT done on the mailer name.  Then, if the mailer has
6203233Seric 	**	a picky -f flag, we insert it as appropriate.  This
6213233Seric 	**	code does not check for 'pv' overflow; this places a
6223233Seric 	**	manifest lower limit of 4 for MAXPV.
6238062Seric 	**		The from address rewrite is expected to make
6248062Seric 	**		the address relative to the other end.
6252968Seric 	*/
6262968Seric 
6274452Seric 	/* rewrite from address, using rewriting rules */
62859163Seric 	rcode = EX_OK;
62959163Seric 	(void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m,
63059163Seric 					   RF_SENDERADDR|RF_CANONICAL,
63159163Seric 					   &rcode, e));
63258680Seric 	define('g', rpathbuf, e);		/* translated return path */
6339370Seric 	define('h', host, e);			/* to host */
6343233Seric 	Errors = 0;
6353233Seric 	pvp = pv;
6363233Seric 	*pvp++ = m->m_argv[0];
6372968Seric 
6383233Seric 	/* insert -f or -r flag as appropriate */
63910682Seric 	if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
6403233Seric 	{
64110682Seric 		if (bitnset(M_FOPT, m->m_flags))
6423233Seric 			*pvp++ = "-f";
6433233Seric 		else
6443233Seric 			*pvp++ = "-r";
64551951Seric 		*pvp++ = newstr(rpathbuf);
6463233Seric 	}
647294Seric 
648294Seric 	/*
6493233Seric 	**  Append the other fixed parts of the argv.  These run
6503233Seric 	**  up to the first entry containing "$u".  There can only
6513233Seric 	**  be one of these, and there are only a few more slots
6523233Seric 	**  in the pv after it.
653294Seric 	*/
654294Seric 
6553233Seric 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
656294Seric 	{
65758050Seric 		/* can't use strchr here because of sign extension problems */
65858050Seric 		while (*p != '\0')
65958050Seric 		{
66058050Seric 			if ((*p++ & 0377) == MACROEXPAND)
66158050Seric 			{
66258050Seric 				if (*p == 'u')
66358050Seric 					break;
66458050Seric 			}
66558050Seric 		}
66658050Seric 
66758050Seric 		if (*p != '\0')
6683233Seric 			break;
6693233Seric 
6703233Seric 		/* this entry is safe -- go ahead and process it */
6719370Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
6723233Seric 		*pvp++ = newstr(buf);
6733233Seric 		if (pvp >= &pv[MAXPV - 3])
6743233Seric 		{
67558151Seric 			syserr("554 Too many parameters to %s before $u", pv[0]);
6763233Seric 			return (-1);
6773233Seric 		}
678294Seric 	}
6794863Seric 
6806038Seric 	/*
6816038Seric 	**  If we have no substitution for the user name in the argument
6826038Seric 	**  list, we know that we must supply the names otherwise -- and
6836038Seric 	**  SMTP is the answer!!
6846038Seric 	*/
6856038Seric 
6863233Seric 	if (*mvp == NULL)
6874863Seric 	{
6884863Seric 		/* running SMTP */
6895179Seric # ifdef SMTP
6904863Seric 		clever = TRUE;
6914863Seric 		*pvp = NULL;
69256795Seric # else /* SMTP */
6936038Seric 		/* oops!  we don't implement SMTP */
69458151Seric 		syserr("554 SMTP style mailer");
6955179Seric 		return (EX_SOFTWARE);
69656795Seric # endif /* SMTP */
6974863Seric 	}
698294Seric 
699294Seric 	/*
7003233Seric 	**  At this point *mvp points to the argument with $u.  We
7013233Seric 	**  run through our address list and append all the addresses
7023233Seric 	**  we can.  If we run out of space, do not fret!  We can
7033233Seric 	**  always send another copy later.
704294Seric 	*/
705294Seric 
7063233Seric 	tobuf[0] = '\0';
7079370Seric 	e->e_to = tobuf;
7084397Seric 	ctladdr = NULL;
70957454Seric 	firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7103233Seric 	for (; to != NULL; to = to->q_next)
711294Seric 	{
7123233Seric 		/* avoid sending multiple recipients to dumb mailers */
71310682Seric 		if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7143233Seric 			break;
7153233Seric 
7163233Seric 		/* if already sent or not for this host, don't send */
71758680Seric 		if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
71857454Seric 		    to->q_mailer != firstto->q_mailer ||
71957454Seric 		    strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7203233Seric 			continue;
7214397Seric 
7228225Seric 		/* avoid overflowing tobuf */
72342462Sbostic 		if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7248225Seric 			break;
7258225Seric 
7267672Seric 		if (tTd(10, 1))
7275032Seric 		{
7285032Seric 			printf("\nsend to ");
7295032Seric 			printaddr(to, FALSE);
7305032Seric 		}
7315032Seric 
7324397Seric 		/* compute effective uid/gid when sending */
7334596Seric 		if (to->q_mailer == ProgMailer)
7344397Seric 			ctladdr = getctladdr(to);
7354397Seric 
7363233Seric 		user = to->q_user;
7379370Seric 		e->e_to = to->q_paddr;
73857731Seric 		if (tTd(10, 5))
73957731Seric 		{
74057731Seric 			printf("deliver: QDONTSEND ");
74157731Seric 			printaddr(to, FALSE);
74257731Seric 		}
74358680Seric 		to->q_flags |= QDONTSEND;
7443233Seric 
7453233Seric 		/*
7463233Seric 		**  Check to see that these people are allowed to
7473233Seric 		**  talk to each other.
7483233Seric 		*/
7493233Seric 
75010699Seric 		if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
75110699Seric 		{
75229914Seric 			NoReturn = TRUE;
75358151Seric 			usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
75458337Seric 			giveresponse(EX_UNAVAILABLE, m, NULL, e);
75510699Seric 			continue;
75610699Seric 		}
75757441Seric 		rcode = checkcompat(to, e);
75857459Seric 		if (rcode != EX_OK)
759294Seric 		{
76063787Seric 			markfailure(e, to, rcode);
76158337Seric 			giveresponse(rcode, m, NULL, e);
7623233Seric 			continue;
763294Seric 		}
7643233Seric 
7653233Seric 		/*
7664099Seric 		**  Strip quote bits from names if the mailer is dumb
7674099Seric 		**	about them.
7683233Seric 		*/
7693233Seric 
77010682Seric 		if (bitnset(M_STRIPQ, m->m_flags))
771294Seric 		{
77254983Seric 			stripquotes(user);
77354983Seric 			stripquotes(host);
7743233Seric 		}
7753233Seric 
7769206Seric 		/* hack attack -- delivermail compatibility */
7779206Seric 		if (m == ProgMailer && *user == '|')
7789206Seric 			user++;
7799206Seric 
7803233Seric 		/*
7814161Seric 		**  If an error message has already been given, don't
7824161Seric 		**	bother to send to this address.
7834161Seric 		**
7844161Seric 		**	>>>>>>>>>> This clause assumes that the local mailer
7854161Seric 		**	>> NOTE >> cannot do any further aliasing; that
7864161Seric 		**	>>>>>>>>>> function is subsumed by sendmail.
7874161Seric 		*/
7884161Seric 
7897293Seric 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
7904161Seric 			continue;
7914161Seric 
7924283Seric 		/* save statistics.... */
7939370Seric 		markstats(e, to);
7944283Seric 
7954161Seric 		/*
7963233Seric 		**  See if this user name is "special".
7973233Seric 		**	If the user name has a slash in it, assume that this
7986974Seric 		**	is a file -- send it off without further ado.  Note
7996974Seric 		**	that this type of addresses is not processed along
8006974Seric 		**	with the others, so we fudge on the To person.
8013233Seric 		*/
8023233Seric 
80357402Seric 		if (m == FileMailer)
8043233Seric 		{
80557402Seric 			rcode = mailfile(user, getctladdr(to), e);
80658337Seric 			giveresponse(rcode, m, NULL, e);
80757402Seric 			if (rcode == EX_OK)
80857402Seric 				to->q_flags |= QSENT;
80957402Seric 			continue;
810294Seric 		}
8113233Seric 
8124315Seric 		/*
8134315Seric 		**  Address is verified -- add this user to mailer
8144315Seric 		**  argv, and add it to the print list of recipients.
8154315Seric 		*/
8164315Seric 
8176059Seric 		/* link together the chain of recipients */
8186272Seric 		to->q_tchain = tochain;
8196272Seric 		tochain = to;
8206059Seric 
8213233Seric 		/* create list of users for error messages */
8229388Seric 		(void) strcat(tobuf, ",");
8234082Seric 		(void) strcat(tobuf, to->q_paddr);
8249370Seric 		define('u', user, e);		/* to user */
825*64694Seric 		p = to->q_home;
826*64694Seric 		if (p == NULL && ctladdr != NULL)
827*64694Seric 			p = ctladdr->q_home;
828*64694Seric 		define('z', p, e);	/* user's home */
8293233Seric 
8304863Seric 		/*
8316059Seric 		**  Expand out this user into argument list.
8324863Seric 		*/
8334863Seric 
8346059Seric 		if (!clever)
8353233Seric 		{
8369370Seric 			expand(*mvp, buf, &buf[sizeof buf - 1], e);
8374863Seric 			*pvp++ = newstr(buf);
8384863Seric 			if (pvp >= &pv[MAXPV - 2])
8394863Seric 			{
8404863Seric 				/* allow some space for trailing parms */
8414863Seric 				break;
8424863Seric 			}
8434863Seric 		}
844294Seric 	}
845294Seric 
8464067Seric 	/* see if any addresses still exist */
8474067Seric 	if (tobuf[0] == '\0')
8484863Seric 	{
8499370Seric 		define('g', (char *) NULL, e);
8504067Seric 		return (0);
8514863Seric 	}
8524067Seric 
8533233Seric 	/* print out messages as full list */
8549388Seric 	e->e_to = tobuf + 1;
8553233Seric 
856294Seric 	/*
8573233Seric 	**  Fill out any parameters after the $u parameter.
858294Seric 	*/
859294Seric 
8604863Seric 	while (!clever && *++mvp != NULL)
861294Seric 	{
8629370Seric 		expand(*mvp, buf, &buf[sizeof buf - 1], e);
8633233Seric 		*pvp++ = newstr(buf);
8643233Seric 		if (pvp >= &pv[MAXPV])
86558151Seric 			syserr("554 deliver: pv overflow after $u for %s", pv[0]);
866294Seric 	}
8673233Seric 	*pvp++ = NULL;
868294Seric 
869294Seric 	/*
870294Seric 	**  Call the mailer.
8712898Seric 	**	The argument vector gets built, pipes
872294Seric 	**	are created as necessary, and we fork & exec as
8732898Seric 	**	appropriate.
8744863Seric 	**	If we are running SMTP, we just need to clean up.
875294Seric 	*/
876294Seric 
87758309Seric 	if (ctladdr == NULL && m != ProgMailer)
87841050Seric 		ctladdr = &e->e_from;
87935651Seric #ifdef NAMED_BIND
88051313Seric 	if (ConfigLevel < 2)
88151313Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
88235651Seric #endif
88354967Seric 
8847672Seric 	if (tTd(11, 1))
885294Seric 	{
8868178Seric 		printf("openmailer:");
88758820Seric 		printav(pv);
888294Seric 	}
8894488Seric 	errno = 0;
8903233Seric 
89125050Seric 	CurHostName = m->m_mailer;
89225050Seric 
8936038Seric 	/*
8946038Seric 	**  Deal with the special case of mail handled through an IPC
8956038Seric 	**  connection.
8966038Seric 	**	In this case we don't actually fork.  We must be
8976038Seric 	**	running SMTP for this to work.  We will return a
8986038Seric 	**	zero pid to indicate that we are running IPC.
89911160Seric 	**  We also handle a debug version that just talks to stdin/out.
9006038Seric 	*/
9016038Seric 
90258820Seric 	curhost = NULL;
90364334Seric 	SmtpPhase = NULL;
90458820Seric 
90564401Seric #ifdef XDEBUG
90664401Seric 	{
90764401Seric 		char wbuf[MAXLINE];
90864401Seric 
90964401Seric 		/* make absolutely certain 0, 1, and 2 are in use */
91064401Seric 		sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
91164401Seric 		checkfd012(wbuf);
91264401Seric 	}
91364401Seric #endif
91464401Seric 
91564401Seric 
91611160Seric 	/* check for Local Person Communication -- not for mortals!!! */
91711160Seric 	if (strcmp(m->m_mailer, "[LPC]") == 0)
91811160Seric 	{
91954967Seric 		mci = (MCI *) xalloc(sizeof *mci);
92054993Seric 		bzero((char *) mci, sizeof *mci);
92153738Seric 		mci->mci_in = stdin;
92253738Seric 		mci->mci_out = stdout;
92354967Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
92453751Seric 		mci->mci_mailer = m;
92511160Seric 	}
92654967Seric 	else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
92754967Seric 		 strcmp(m->m_mailer, "[TCP]") == 0)
9286038Seric 	{
92952107Seric #ifdef DAEMON
93057454Seric 		register int i;
9317285Seric 		register u_short port;
9326038Seric 
93358820Seric 		CurHostName = pv[1];
93458820Seric 		curhost = hostsignature(m, pv[1], e);
93554967Seric 
93658479Seric 		if (curhost == NULL || curhost[0] == '\0')
93758479Seric 		{
93858479Seric 			syserr("null signature");
93958820Seric 			rcode = EX_OSERR;
94058820Seric 			goto give_up;
94158479Seric 		}
94258479Seric 
9436038Seric 		if (!clever)
94458479Seric 		{
94558151Seric 			syserr("554 non-clever IPC");
94658820Seric 			rcode = EX_OSERR;
94758820Seric 			goto give_up;
94858479Seric 		}
94958820Seric 		if (pv[2] != NULL)
95058820Seric 			port = atoi(pv[2]);
9516632Seric 		else
9527285Seric 			port = 0;
95358820Seric tryhost:
95458820Seric 		mci = NULL;
95557454Seric 		while (*curhost != '\0')
95629433Sbloom 		{
95757454Seric 			register char *p;
95858664Seric 			static char hostbuf[MAXNAME];
95957454Seric 
96058820Seric 			mci = NULL;
96158820Seric 
96257454Seric 			/* pull the next host from the signature */
96357454Seric 			p = strchr(curhost, ':');
96457454Seric 			if (p == NULL)
96557454Seric 				p = &curhost[strlen(curhost)];
96657454Seric 			strncpy(hostbuf, curhost, p - curhost);
96757454Seric 			hostbuf[p - curhost] = '\0';
96857454Seric 			if (*p != '\0')
96957454Seric 				p++;
97057454Seric 			curhost = p;
97157454Seric 
97253738Seric 			/* see if we already know that this host is fried */
97357454Seric 			CurHostName = hostbuf;
97457454Seric 			mci = mci_get(hostbuf, m);
97554967Seric 			if (mci->mci_state != MCIS_CLOSED)
97657387Seric 			{
97757387Seric 				if (tTd(11, 1))
97857387Seric 				{
97957387Seric 					printf("openmailer: ");
98057387Seric 					mci_dump(mci);
98157387Seric 				}
98257943Seric 				CurHostName = mci->mci_host;
98358820Seric 				break;
98457387Seric 			}
98553751Seric 			mci->mci_mailer = m;
98654967Seric 			if (mci->mci_exitstat != EX_OK)
98754967Seric 				continue;
98854967Seric 
98954967Seric 			/* try the connection */
99057454Seric 			setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
99158151Seric 			message("Connecting to %s (%s)...",
99257454Seric 				hostbuf, m->m_name);
99357454Seric 			i = makeconnection(hostbuf, port, mci,
99454967Seric 				bitnset(M_SECURE_PORT, m->m_flags));
99554967Seric 			mci->mci_exitstat = i;
99654967Seric 			mci->mci_errno = errno;
99763753Seric #ifdef NAMED_BIND
99863753Seric 			mci->mci_herrno = h_errno;
99963753Seric #endif
100054967Seric 			if (i == EX_OK)
100152106Seric 			{
100254967Seric 				mci->mci_state = MCIS_OPENING;
100354967Seric 				mci_cache(mci);
100463753Seric 				if (TrafficLogFile != NULL)
100563753Seric 					fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
100663753Seric 						getpid(), hostbuf);
100754967Seric 				break;
100834022Sbostic 			}
100954967Seric 			else if (tTd(11, 1))
101054967Seric 				printf("openmailer: makeconnection => stat=%d, errno=%d\n",
101154967Seric 					i, errno);
101253738Seric 
101354967Seric 
101453738Seric 			/* enter status of this host */
101553738Seric 			setstat(i);
10166047Seric 		}
101754993Seric 		mci->mci_pid = 0;
101854967Seric #else /* no DAEMON */
101958151Seric 		syserr("554 openmailer: no IPC");
102057387Seric 		if (tTd(11, 1))
102157387Seric 			printf("openmailer: NULL\n");
102253738Seric 		return NULL;
102354967Seric #endif /* DAEMON */
10246038Seric 	}
102554967Seric 	else
1026294Seric 	{
102763753Seric 		if (TrafficLogFile != NULL)
102858852Seric 		{
102963753Seric 			char **av;
103058925Seric 
103163753Seric 			fprintf(TrafficLogFile, "%05d === EXEC", getpid());
103263753Seric 			for (av = pv; *av != NULL; av++)
103363753Seric 				fprintf(TrafficLogFile, " %s", *av);
103463753Seric 			fprintf(TrafficLogFile, "\n");
103558852Seric 		}
103658852Seric 
103754967Seric 		/* create a pipe to shove the mail through */
103854967Seric 		if (pipe(mpvect) < 0)
103954967Seric 		{
104058925Seric 			syserr("%s... openmailer(%s): pipe (to mailer)",
104158925Seric 				e->e_to, m->m_name);
104257387Seric 			if (tTd(11, 1))
104357387Seric 				printf("openmailer: NULL\n");
104458820Seric 			rcode = EX_OSERR;
104558820Seric 			goto give_up;
104654967Seric 		}
10474863Seric 
104854967Seric 		/* if this mailer speaks smtp, create a return pipe */
104954967Seric 		if (clever && pipe(rpvect) < 0)
105054967Seric 		{
105158925Seric 			syserr("%s... openmailer(%s): pipe (from mailer)",
105258925Seric 				e->e_to, m->m_name);
105354967Seric 			(void) close(mpvect[0]);
105454967Seric 			(void) close(mpvect[1]);
105557387Seric 			if (tTd(11, 1))
105657387Seric 				printf("openmailer: NULL\n");
105758820Seric 			rcode = EX_OSERR;
105858820Seric 			goto give_up;
105954967Seric 		}
10604863Seric 
106154967Seric 		/*
106254967Seric 		**  Actually fork the mailer process.
106354967Seric 		**	DOFORK is clever about retrying.
106454967Seric 		**
106554967Seric 		**	Dispose of SIGCHLD signal catchers that may be laying
106654967Seric 		**	around so that endmail will get it.
106754967Seric 		*/
10686038Seric 
106954967Seric 		if (e->e_xfp != NULL)
107054967Seric 			(void) fflush(e->e_xfp);		/* for debugging */
107154967Seric 		(void) fflush(stdout);
107226434Seric # ifdef SIGCHLD
107364035Seric 		(void) setsignal(SIGCHLD, SIG_DFL);
107456795Seric # endif /* SIGCHLD */
107554967Seric 		DOFORK(FORK);
107654967Seric 		/* pid is set by DOFORK */
107754967Seric 		if (pid < 0)
10784863Seric 		{
107954967Seric 			/* failure */
108058925Seric 			syserr("%s... openmailer(%s): cannot fork",
108158925Seric 				e->e_to, m->m_name);
108254967Seric 			(void) close(mpvect[0]);
108354967Seric 			(void) close(mpvect[1]);
108454967Seric 			if (clever)
108554967Seric 			{
108654967Seric 				(void) close(rpvect[0]);
108754967Seric 				(void) close(rpvect[1]);
108854967Seric 			}
108957387Seric 			if (tTd(11, 1))
109057387Seric 				printf("openmailer: NULL\n");
109158820Seric 			rcode = EX_OSERR;
109258820Seric 			goto give_up;
10934863Seric 		}
109454967Seric 		else if (pid == 0)
109554967Seric 		{
109654967Seric 			int i;
109756678Seric 			int saveerrno;
109858675Seric 			char **ep;
109958675Seric 			char *env[MAXUSERENVIRON];
110058675Seric 			extern char **environ;
110154967Seric 			extern int DtableSize;
110215772Seric 
110354967Seric 			/* child -- set up input & exec mailer */
110464035Seric 			(void) setsignal(SIGINT, SIG_IGN);
110564035Seric 			(void) setsignal(SIGHUP, SIG_IGN);
110664035Seric 			(void) setsignal(SIGTERM, SIG_DFL);
11074709Seric 
110858082Seric 			/* close any other cached connections */
110958082Seric 			mci_flush(FALSE, mci);
111058082Seric 
111164145Seric 			/* reset user and group */
111264145Seric 			if (!bitnset(M_RESTR, m->m_flags))
111364145Seric 			{
111464145Seric 				if (ctladdr == NULL || ctladdr->q_uid == 0)
111564145Seric 				{
111664145Seric 					(void) initgroups(DefUser, DefGid);
111764145Seric 					(void) setuid(DefUid);
111864145Seric 				}
111964145Seric 				else
112064145Seric 				{
112164145Seric 					(void) initgroups(ctladdr->q_ruser?
112264145Seric 						ctladdr->q_ruser: ctladdr->q_user,
112364145Seric 						ctladdr->q_gid);
112464145Seric 					(void) setuid(ctladdr->q_uid);
112564145Seric 				}
112664145Seric 			}
112764145Seric 
112864145Seric 			if (tTd(11, 2))
112964145Seric 				printf("openmailer: running as r/euid=%d/%d\n",
113064145Seric 					getuid(), geteuid());
113164145Seric 
113258935Seric 			/* move into some "safe" directory */
113358935Seric 			if (m->m_execdir != NULL)
113458935Seric 			{
113558935Seric 				char *p, *q;
113658935Seric 				char buf[MAXLINE];
113758935Seric 
113858935Seric 				for (p = m->m_execdir; p != NULL; p = q)
113958935Seric 				{
114058935Seric 					q = strchr(p, ':');
114158935Seric 					if (q != NULL)
114258935Seric 						*q = '\0';
114358935Seric 					expand(p, buf, &buf[sizeof buf] - 1, e);
114458935Seric 					if (q != NULL)
114558935Seric 						*q++ = ':';
114658935Seric 					if (tTd(11, 20))
114758935Seric 						printf("openmailer: trydir %s\n",
114858935Seric 							buf);
114958935Seric 					if (buf[0] != '\0' && chdir(buf) >= 0)
115058935Seric 						break;
115158935Seric 				}
115258935Seric 			}
115358935Seric 
115454967Seric 			/* arrange to filter std & diag output of command */
115554967Seric 			if (clever)
115654967Seric 			{
115754967Seric 				(void) close(rpvect[0]);
115858852Seric 				if (dup2(rpvect[1], STDOUT_FILENO) < 0)
115958852Seric 				{
116058925Seric 					syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
116158925Seric 						e->e_to, m->m_name, rpvect[1]);
116258852Seric 					_exit(EX_OSERR);
116358852Seric 				}
116454967Seric 				(void) close(rpvect[1]);
116554967Seric 			}
116654967Seric 			else if (OpMode == MD_SMTP || HoldErrs)
116754967Seric 			{
116854967Seric 				/* put mailer output in transcript */
116958852Seric 				if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
117058852Seric 				{
117158925Seric 					syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
117258925Seric 						e->e_to, m->m_name,
117358852Seric 						fileno(e->e_xfp));
117458852Seric 					_exit(EX_OSERR);
117558852Seric 				}
117654967Seric 			}
117758852Seric 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
117858852Seric 			{
117958925Seric 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
118058925Seric 					e->e_to, m->m_name);
118158852Seric 				_exit(EX_OSERR);
118258852Seric 			}
11834709Seric 
118454967Seric 			/* arrange to get standard input */
118554967Seric 			(void) close(mpvect[1]);
118658731Seric 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
11874417Seric 			{
118858925Seric 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
118958925Seric 					e->e_to, m->m_name, mpvect[0]);
119054967Seric 				_exit(EX_OSERR);
11914417Seric 			}
119254967Seric 			(void) close(mpvect[0]);
11939370Seric 
119454967Seric 			/* arrange for all the files to be closed */
119554967Seric 			for (i = 3; i < DtableSize; i++)
119654967Seric 			{
119754967Seric 				register int j;
119864145Seric 
119954967Seric 				if ((j = fcntl(i, F_GETFD, 0)) != -1)
120064145Seric 					(void) fcntl(i, F_SETFD, j | 1);
120154967Seric 			}
12022774Seric 
120358675Seric 			/* set up the mailer environment */
120458675Seric 			i = 0;
120558675Seric 			env[i++] = "AGENT=sendmail";
120658675Seric 			for (ep = environ; *ep != NULL; ep++)
120758675Seric 			{
120858675Seric 				if (strncmp(*ep, "TZ=", 3) == 0)
120958675Seric 					env[i++] = *ep;
121058675Seric 			}
121158675Seric 			env[i++] = NULL;
121258675Seric 
121354967Seric 			/* try to execute the mailer */
121458820Seric 			execve(m->m_mailer, pv, env);
121556678Seric 			saveerrno = errno;
121654967Seric 			syserr("Cannot exec %s", m->m_mailer);
121760008Seric 			if (m == LocalMailer || transienterror(saveerrno))
121860008Seric 				_exit(EX_OSERR);
121954967Seric 			_exit(EX_UNAVAILABLE);
122051835Seric 		}
122154967Seric 
122254967Seric 		/*
122354967Seric 		**  Set up return value.
122454967Seric 		*/
122554967Seric 
122654967Seric 		mci = (MCI *) xalloc(sizeof *mci);
122754993Seric 		bzero((char *) mci, sizeof *mci);
122854967Seric 		mci->mci_mailer = m;
122954967Seric 		mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
123054993Seric 		mci->mci_pid = pid;
123154967Seric 		(void) close(mpvect[0]);
123254967Seric 		mci->mci_out = fdopen(mpvect[1], "w");
123354967Seric 		if (clever)
123454967Seric 		{
123554967Seric 			(void) close(rpvect[1]);
123654967Seric 			mci->mci_in = fdopen(rpvect[0], "r");
123754967Seric 		}
123854967Seric 		else
123954967Seric 		{
124054967Seric 			mci->mci_flags |= MCIF_TEMP;
124154967Seric 			mci->mci_in = NULL;
124254967Seric 		}
1243294Seric 	}
1244294Seric 
12454709Seric 	/*
124654967Seric 	**  If we are in SMTP opening state, send initial protocol.
12474709Seric 	*/
12484709Seric 
124954967Seric 	if (clever && mci->mci_state != MCIS_CLOSED)
12504863Seric 	{
125154967Seric 		smtpinit(m, mci, e);
125253738Seric 	}
125357387Seric 	if (tTd(11, 1))
125457387Seric 	{
125557387Seric 		printf("openmailer: ");
125657387Seric 		mci_dump(mci);
125757387Seric 	}
1258294Seric 
125958820Seric 	if (mci->mci_state != MCIS_OPEN)
126058820Seric 	{
126158820Seric 		/* couldn't open the mailer */
126258820Seric 		rcode = mci->mci_exitstat;
126358820Seric 		errno = mci->mci_errno;
126463753Seric #ifdef NAMED_BIND
126563753Seric 		h_errno = mci->mci_herrno;
126663753Seric #endif
126758820Seric 		if (rcode == EX_OK)
126858820Seric 		{
126958820Seric 			/* shouldn't happen */
127058820Seric 			syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
127158820Seric 				rcode, mci->mci_state, firstsig);
127258820Seric 			rcode = EX_SOFTWARE;
127358820Seric 		}
127459993Seric 		else if (rcode == EX_TEMPFAIL && *curhost != '\0')
127559958Seric 		{
127659958Seric 			/* try next MX site */
127759958Seric 			goto tryhost;
127859958Seric 		}
127958820Seric 	}
128058820Seric 	else if (!clever)
128158820Seric 	{
128258820Seric 		/*
128358820Seric 		**  Format and send message.
128458820Seric 		*/
128558820Seric 
128658820Seric 		putfromline(mci->mci_out, m, e);
128758820Seric 		(*e->e_puthdr)(mci->mci_out, m, e);
128858820Seric 		putline("\n", mci->mci_out, m);
128959730Seric 		(*e->e_putbody)(mci->mci_out, m, e, NULL);
129058820Seric 
129158820Seric 		/* get the exit status */
129258820Seric 		rcode = endmailer(mci, e, pv);
129358820Seric 	}
129458820Seric 	else
129558820Seric #ifdef SMTP
129658820Seric 	{
129758820Seric 		/*
129858820Seric 		**  Send the MAIL FROM: protocol
129958820Seric 		*/
130058820Seric 
130158820Seric 		rcode = smtpmailfrom(m, mci, e);
130258820Seric 		if (rcode == EX_OK)
130358820Seric 		{
130458820Seric 			register char *t = tobuf;
130558820Seric 			register int i;
130658820Seric 
130758820Seric 			/* send the recipient list */
130858820Seric 			tobuf[0] = '\0';
130958820Seric 			for (to = tochain; to != NULL; to = to->q_tchain)
131058820Seric 			{
131158820Seric 				e->e_to = to->q_paddr;
131258820Seric 				if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
131358820Seric 				{
131458820Seric 					markfailure(e, to, i);
131558820Seric 					giveresponse(i, m, mci, e);
131658820Seric 				}
131758820Seric 				else
131858820Seric 				{
131958820Seric 					*t++ = ',';
132058820Seric 					for (p = to->q_paddr; *p; *t++ = *p++)
132158820Seric 						continue;
132264372Seric 					*t = '\0';
132358820Seric 				}
132458820Seric 			}
132558820Seric 
132658820Seric 			/* now send the data */
132758820Seric 			if (tobuf[0] == '\0')
132858820Seric 			{
132958820Seric 				rcode = EX_OK;
133058820Seric 				e->e_to = NULL;
133158820Seric 				if (bitset(MCIF_CACHED, mci->mci_flags))
133258820Seric 					smtprset(m, mci, e);
133358820Seric 			}
133458820Seric 			else
133558820Seric 			{
133658820Seric 				e->e_to = tobuf + 1;
133758820Seric 				rcode = smtpdata(m, mci, e);
133858820Seric 			}
133958820Seric 
134058820Seric 			/* now close the connection */
134158820Seric 			if (!bitset(MCIF_CACHED, mci->mci_flags))
134258820Seric 				smtpquit(m, mci, e);
134358820Seric 		}
134458820Seric 		if (rcode != EX_OK && *curhost != '\0')
134558820Seric 		{
134658820Seric 			/* try next MX site */
134758820Seric 			goto tryhost;
134858820Seric 		}
134958820Seric 	}
135058820Seric #else /* not SMTP */
135158820Seric 	{
135258820Seric 		syserr("554 deliver: need SMTP compiled to use clever mailer");
135358820Seric 		rcode = EX_CONFIG;
135458820Seric 		goto give_up;
135558820Seric 	}
135658820Seric #endif /* SMTP */
135758820Seric #ifdef NAMED_BIND
135858820Seric 	if (ConfigLevel < 2)
135958820Seric 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
136058820Seric #endif
136158820Seric 
136258820Seric 	/* arrange a return receipt if requested */
136358820Seric 	if (e->e_receiptto != NULL && bitnset(M_LOCALMAILER, m->m_flags))
136458820Seric 	{
136558820Seric 		e->e_flags |= EF_SENDRECEIPT;
136658820Seric 		/* do we want to send back more info? */
136758820Seric 	}
136858820Seric 
136958820Seric 	/*
137058820Seric 	**  Do final status disposal.
137158820Seric 	**	We check for something in tobuf for the SMTP case.
137258820Seric 	**	If we got a temporary failure, arrange to queue the
137358820Seric 	**		addressees.
137458820Seric 	*/
137558820Seric 
137658820Seric   give_up:
137758820Seric 	if (tobuf[0] != '\0')
137858820Seric 		giveresponse(rcode, m, mci, e);
137958820Seric 	for (to = tochain; to != NULL; to = to->q_tchain)
138058820Seric 	{
138158820Seric 		if (rcode != EX_OK)
138258820Seric 			markfailure(e, to, rcode);
138358820Seric 		else
138458820Seric 		{
138558820Seric 			to->q_flags |= QSENT;
138658820Seric 			e->e_nsent++;
138758820Seric 		}
138858820Seric 	}
138958820Seric 
139058820Seric 	/*
139158820Seric 	**  Restore state and return.
139258820Seric 	*/
139358820Seric 
139464401Seric #ifdef XDEBUG
139564401Seric 	{
139664401Seric 		char wbuf[MAXLINE];
139764401Seric 
139864401Seric 		/* make absolutely certain 0, 1, and 2 are in use */
139964554Seric 		sprintf(wbuf, "%s... end of deliver(%s)",
140064554Seric 			e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
140164554Seric 			m->m_name);
140264401Seric 		checkfd012(wbuf);
140364401Seric 	}
140464401Seric #endif
140564401Seric 
140658820Seric 	errno = 0;
140758820Seric 	define('g', (char *) NULL, e);
140858820Seric 	return (rcode);
1409294Seric }
1410294Seric /*
141158820Seric **  MARKFAILURE -- mark a failure on a specific address.
141258820Seric **
141358820Seric **	Parameters:
141458820Seric **		e -- the envelope we are sending.
141558820Seric **		q -- the address to mark.
141658820Seric **		rcode -- the code signifying the particular failure.
141758820Seric **
141858820Seric **	Returns:
141958820Seric **		none.
142058820Seric **
142158820Seric **	Side Effects:
142258820Seric **		marks the address (and possibly the envelope) with the
142358820Seric **			failure so that an error will be returned or
142458820Seric **			the message will be queued, as appropriate.
142558820Seric */
142658820Seric 
142758820Seric markfailure(e, q, rcode)
142858820Seric 	register ENVELOPE *e;
142958820Seric 	register ADDRESS *q;
143058820Seric 	int rcode;
143158820Seric {
143258820Seric 	char buf[MAXLINE];
143358820Seric 
143458820Seric 	if (rcode == EX_OK)
143558820Seric 		return;
143663753Seric 	else if (rcode == EX_TEMPFAIL)
143763753Seric 		q->q_flags |= QQUEUEUP;
143863753Seric 	else if (rcode != EX_IOERR && rcode != EX_OSERR)
143958820Seric 		q->q_flags |= QBADADDR;
144058820Seric }
144158820Seric /*
144258820Seric **  ENDMAILER -- Wait for mailer to terminate.
144358820Seric **
144458820Seric **	We should never get fatal errors (e.g., segmentation
144558820Seric **	violation), so we report those specially.  For other
144658820Seric **	errors, we choose a status message (into statmsg),
144758820Seric **	and if it represents an error, we print it.
144858820Seric **
144958820Seric **	Parameters:
145058820Seric **		pid -- pid of mailer.
145158820Seric **		e -- the current envelope.
145258820Seric **		pv -- the parameter vector that invoked the mailer
145358820Seric **			(for error messages).
145458820Seric **
145558820Seric **	Returns:
145658820Seric **		exit code of mailer.
145758820Seric **
145858820Seric **	Side Effects:
145958820Seric **		none.
146058820Seric */
146158820Seric 
146258820Seric endmailer(mci, e, pv)
146358820Seric 	register MCI *mci;
146458820Seric 	register ENVELOPE *e;
146558820Seric 	char **pv;
146658820Seric {
146758820Seric 	int st;
146858820Seric 
146958820Seric 	/* close any connections */
147058820Seric 	if (mci->mci_in != NULL)
147158820Seric 		(void) xfclose(mci->mci_in, pv[0], "mci_in");
147258820Seric 	if (mci->mci_out != NULL)
147358820Seric 		(void) xfclose(mci->mci_out, pv[0], "mci_out");
147458820Seric 	mci->mci_in = mci->mci_out = NULL;
147558820Seric 	mci->mci_state = MCIS_CLOSED;
147658820Seric 
147758820Seric 	/* in the IPC case there is nothing to wait for */
147858820Seric 	if (mci->mci_pid == 0)
147958820Seric 		return (EX_OK);
148058820Seric 
148158820Seric 	/* wait for the mailer process to die and collect status */
148258820Seric 	st = waitfor(mci->mci_pid);
148358820Seric 	if (st == -1)
148458820Seric 	{
148558820Seric 		syserr("endmailer %s: wait", pv[0]);
148658820Seric 		return (EX_SOFTWARE);
148758820Seric 	}
148858820Seric 
148964379Seric 	if (WIFEXITED(st))
149058820Seric 	{
149164379Seric 		/* normal death -- return status */
149264379Seric 		return (WEXITSTATUS(st));
149364379Seric 	}
149458820Seric 
149564379Seric 	/* it died a horrid death */
149664379Seric 	syserr("mailer %s died with signal %o", pv[0], st);
149758820Seric 
149864379Seric 	/* log the arguments */
149964379Seric 	if (e->e_xfp != NULL)
150064379Seric 	{
150164379Seric 		register char **av;
150258820Seric 
150364379Seric 		fprintf(e->e_xfp, "Arguments:");
150464379Seric 		for (av = pv; *av != NULL; av++)
150564379Seric 			fprintf(e->e_xfp, " %s", *av);
150664379Seric 		fprintf(e->e_xfp, "\n");
150758820Seric 	}
150858820Seric 
150964379Seric 	ExitStat = EX_TEMPFAIL;
151064379Seric 	return (EX_TEMPFAIL);
151158820Seric }
151258820Seric /*
1513294Seric **  GIVERESPONSE -- Interpret an error response from a mailer
1514294Seric **
1515294Seric **	Parameters:
1516294Seric **		stat -- the status code from the mailer (high byte
1517294Seric **			only; core dumps must have been taken care of
1518294Seric **			already).
151958337Seric **		m -- the mailer info for this mailer.
152058337Seric **		mci -- the mailer connection info -- can be NULL if the
152158337Seric **			response is given before the connection is made.
152258337Seric **		e -- the current envelope.
1523294Seric **
1524294Seric **	Returns:
15254082Seric **		none.
1526294Seric **
1527294Seric **	Side Effects:
15281518Seric **		Errors may be incremented.
1529294Seric **		ExitStat may be set.
1530294Seric */
1531294Seric 
153258337Seric giveresponse(stat, m, mci, e)
1533294Seric 	int stat;
15349370Seric 	register MAILER *m;
153558337Seric 	register MCI *mci;
153610105Seric 	ENVELOPE *e;
1537294Seric {
153860094Seric 	register const char *statmsg;
1539294Seric 	extern char *SysExMsg[];
1540294Seric 	register int i;
154136788Sbostic 	extern int N_SysEx;
154210105Seric 	char buf[MAXLINE];
1543294Seric 
15444315Seric 	/*
15454315Seric 	**  Compute status message from code.
15464315Seric 	*/
15474315Seric 
1548294Seric 	i = stat - EX__BASE;
15499370Seric 	if (stat == 0)
155058852Seric 	{
15519370Seric 		statmsg = "250 Sent";
155258916Seric 		if (e->e_statmsg != NULL)
155358852Seric 		{
155458916Seric 			(void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
155558852Seric 			statmsg = buf;
155658852Seric 		}
155758852Seric 	}
15589370Seric 	else if (i < 0 || i > N_SysEx)
15599370Seric 	{
15609370Seric 		(void) sprintf(buf, "554 unknown mailer error %d", stat);
15619370Seric 		stat = EX_UNAVAILABLE;
15629370Seric 		statmsg = buf;
15639370Seric 	}
156410105Seric 	else if (stat == EX_TEMPFAIL)
156510105Seric 	{
156658664Seric 		(void) strcpy(buf, SysExMsg[i] + 1);
156736788Sbostic #ifdef NAMED_BIND
156825527Smiriam 		if (h_errno == TRY_AGAIN)
156963993Seric 			statmsg = errstring(h_errno+E_DNSBASE);
157021061Seric 		else
157136788Sbostic #endif
157221061Seric 		{
157325527Smiriam 			if (errno != 0)
157425527Smiriam 				statmsg = errstring(errno);
157525527Smiriam 			else
157625527Smiriam 			{
157721061Seric #ifdef SMTP
157825527Smiriam 				extern char SmtpError[];
157921061Seric 
158025527Smiriam 				statmsg = SmtpError;
158156795Seric #else /* SMTP */
158225527Smiriam 				statmsg = NULL;
158356795Seric #endif /* SMTP */
158425527Smiriam 			}
158521061Seric 		}
158621061Seric 		if (statmsg != NULL && statmsg[0] != '\0')
158721061Seric 		{
158810124Seric 			(void) strcat(buf, ": ");
158921061Seric 			(void) strcat(buf, statmsg);
159010105Seric 		}
159110105Seric 		statmsg = buf;
159210105Seric 	}
159363753Seric #ifdef NAMED_BIND
159463753Seric 	else if (stat == EX_NOHOST && h_errno != 0)
159563753Seric 	{
159663993Seric 		statmsg = errstring(h_errno + E_DNSBASE);
159763753Seric 		(void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg);
159863753Seric 		statmsg = buf;
159963753Seric 	}
160063753Seric #endif
1601294Seric 	else
160221061Seric 	{
1603294Seric 		statmsg = SysExMsg[i];
160458664Seric 		if (*statmsg++ == ':')
160558664Seric 		{
160658664Seric 			(void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
160758664Seric 			statmsg = buf;
160858664Seric 		}
160921061Seric 	}
16109370Seric 
16119370Seric 	/*
16129370Seric 	**  Print the message as appropriate
16139370Seric 	*/
16149370Seric 
161510105Seric 	if (stat == EX_OK || stat == EX_TEMPFAIL)
161658524Seric 		message(&statmsg[4], errstring(errno));
1617294Seric 	else
1618294Seric 	{
16191518Seric 		Errors++;
162058524Seric 		usrerr(statmsg, errstring(errno));
1621294Seric 	}
1622294Seric 
1623294Seric 	/*
1624294Seric 	**  Final cleanup.
1625294Seric 	**	Log a record of the transaction.  Compute the new
1626294Seric 	**	ExitStat -- if we already had an error, stick with
1627294Seric 	**	that.
1628294Seric 	*/
1629294Seric 
163058020Seric 	if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
163158337Seric 		logdelivery(m, mci, &statmsg[4], e);
16327858Seric 
16334621Seric 	if (stat != EX_TEMPFAIL)
16344621Seric 		setstat(stat);
163510105Seric 	if (stat != EX_OK)
163610105Seric 	{
163710105Seric 		if (e->e_message != NULL)
163810105Seric 			free(e->e_message);
163910105Seric 		e->e_message = newstr(&statmsg[4]);
164010105Seric 	}
164110124Seric 	errno = 0;
164236788Sbostic #ifdef NAMED_BIND
164325527Smiriam 	h_errno = 0;
164436788Sbostic #endif
1645294Seric }
1646294Seric /*
16478496Seric **  LOGDELIVERY -- log the delivery in the system log
16488496Seric **
16498496Seric **	Parameters:
165058337Seric **		m -- the mailer info.  Can be NULL for initial queue.
165158337Seric **		mci -- the mailer connection info -- can be NULL if the
165258337Seric **			log is occuring when no connection is active.
165358337Seric **		stat -- the message to print for the status.
165458337Seric **		e -- the current envelope.
16558496Seric **
16568496Seric **	Returns:
16578496Seric **		none
16588496Seric **
16598496Seric **	Side Effects:
16608496Seric **		none
16618496Seric */
16628496Seric 
166358337Seric logdelivery(m, mci, stat, e)
166458337Seric 	MAILER *m;
166558337Seric 	register MCI *mci;
16668496Seric 	char *stat;
166754967Seric 	register ENVELOPE *e;
16688496Seric {
166958343Seric # ifdef LOG
167058418Seric 	char buf[512];
16718496Seric 
167258513Seric 	(void) sprintf(buf, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
167358337Seric 
167458513Seric 	if (m != NULL)
167558305Seric 	{
167658513Seric 		(void) strcat(buf, ", mailer=");
167758513Seric 		(void) strcat(buf, m->m_name);
167858305Seric 	}
167958513Seric 
168058513Seric 	if (mci != NULL && mci->mci_host != NULL)
168158305Seric 	{
168258305Seric # ifdef DAEMON
168358755Seric 		extern SOCKADDR CurHostAddr;
168458513Seric # endif
168558305Seric 
168658513Seric 		(void) strcat(buf, ", relay=");
168758513Seric 		(void) strcat(buf, mci->mci_host);
168858513Seric 
168958513Seric # ifdef DAEMON
169058513Seric 		(void) strcat(buf, " (");
169158755Seric 		(void) strcat(buf, anynet_ntoa(&CurHostAddr));
169258513Seric 		(void) strcat(buf, ")");
169358305Seric # endif
169458305Seric 	}
169558343Seric 	else
169658513Seric 	{
169758513Seric 		char *p = macvalue('h', e);
169858343Seric 
169958513Seric 		if (p != NULL && p[0] != '\0')
170058513Seric 		{
170158513Seric 			(void) strcat(buf, ", relay=");
170258513Seric 			(void) strcat(buf, p);
170358513Seric 		}
170458513Seric 	}
170558418Seric 
170658418Seric 	syslog(LOG_INFO, "%s: to=%s, %s, stat=%s",
170758418Seric 	       e->e_id, e->e_to, buf, stat);
170856795Seric # endif /* LOG */
17098496Seric }
17108496Seric /*
17116974Seric **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
1712294Seric **
17136974Seric **	This can be made an arbitrary message separator by changing $l
1714294Seric **
171516150Seric **	One of the ugliest hacks seen by human eyes is contained herein:
171616150Seric **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
171716150Seric **	does a well-meaning programmer such as myself have to deal with
171816150Seric **	this kind of antique garbage????
17196974Seric **
1720294Seric **	Parameters:
17216974Seric **		fp -- the file to output to.
17226974Seric **		m -- the mailer describing this entry.
1723294Seric **
1724294Seric **	Returns:
17256974Seric **		none
1726294Seric **
1727294Seric **	Side Effects:
17286974Seric **		outputs some text to fp.
1729294Seric */
1730294Seric 
173154967Seric putfromline(fp, m, e)
17326974Seric 	register FILE *fp;
17336974Seric 	register MAILER *m;
173454967Seric 	ENVELOPE *e;
1735294Seric {
173658050Seric 	char *template = "\201l\n";
17376974Seric 	char buf[MAXLINE];
1738294Seric 
173910682Seric 	if (bitnset(M_NHDR, m->m_flags))
17406974Seric 		return;
17414315Seric 
17426974Seric # ifdef UGLYUUCP
174310682Seric 	if (bitnset(M_UGLYUUCP, m->m_flags))
17444205Seric 	{
174512223Seric 		char *bang;
174612223Seric 		char xbuf[MAXLINE];
17476041Seric 
174858680Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
174956795Seric 		bang = strchr(buf, '!');
17506974Seric 		if (bang == NULL)
175164370Seric 		{
175264370Seric 			errno = 0;
175364370Seric 			syserr("554 No ! in UUCP From address! (%s given)", buf);
175464370Seric 		}
17555099Seric 		else
17569370Seric 		{
175712223Seric 			*bang++ = '\0';
175858050Seric 			(void) sprintf(xbuf, "From %s  \201d remote from %s\n", bang, buf);
175912223Seric 			template = xbuf;
17609370Seric 		}
17616974Seric 	}
176256795Seric # endif /* UGLYUUCP */
176354967Seric 	expand(template, buf, &buf[sizeof buf - 1], e);
176410168Seric 	putline(buf, fp, m);
17655981Seric }
17665981Seric /*
17676974Seric **  PUTBODY -- put the body of a message.
17686974Seric **
17696974Seric **	Parameters:
17706974Seric **		fp -- file to output onto.
177110168Seric **		m -- a mailer descriptor to control output format.
17729538Seric **		e -- the envelope to put out.
177359730Seric **		separator -- if non-NULL, a message separator that must
177459730Seric **			not be permitted in the resulting message.
17756974Seric **
17766974Seric **	Returns:
17776974Seric **		none.
17786974Seric **
17796974Seric **	Side Effects:
17806974Seric **		The message is written onto fp.
17816974Seric */
17826974Seric 
178359730Seric putbody(fp, m, e, separator)
17846974Seric 	FILE *fp;
17859370Seric 	MAILER *m;
17869538Seric 	register ENVELOPE *e;
178759730Seric 	char *separator;
17886974Seric {
178910168Seric 	char buf[MAXLINE];
17906974Seric 
17916974Seric 	/*
17926974Seric 	**  Output the body of the message
17936974Seric 	*/
17946974Seric 
17959538Seric 	if (e->e_dfp == NULL)
17966974Seric 	{
17979538Seric 		if (e->e_df != NULL)
17989538Seric 		{
17999538Seric 			e->e_dfp = fopen(e->e_df, "r");
18009538Seric 			if (e->e_dfp == NULL)
180140931Srick 				syserr("putbody: Cannot open %s for %s from %s",
180264118Seric 				e->e_df, e->e_to, e->e_from.q_paddr);
18039538Seric 		}
18049538Seric 		else
180510168Seric 			putline("<<< No Message Collected >>>", fp, m);
18069538Seric 	}
18079538Seric 	if (e->e_dfp != NULL)
18089538Seric 	{
18099538Seric 		rewind(e->e_dfp);
181010168Seric 		while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL)
181116875Seric 		{
181216875Seric 			if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) &&
181340995Sbostic 			    strncmp(buf, "From ", 5) == 0)
181423102Seric 				(void) putc('>', fp);
181559730Seric 			if (buf[0] == '-' && buf[1] == '-' && separator != NULL)
181659730Seric 			{
181759730Seric 				/* possible separator */
181859730Seric 				int sl = strlen(separator);
181959730Seric 
182059730Seric 				if (strncmp(&buf[2], separator, sl) == 0)
182159730Seric 					(void) putc(' ', fp);
182259730Seric 			}
182310168Seric 			putline(buf, fp, m);
182416875Seric 		}
18256974Seric 
18269538Seric 		if (ferror(e->e_dfp))
18276974Seric 		{
18286974Seric 			syserr("putbody: read error");
18296974Seric 			ExitStat = EX_IOERR;
18306974Seric 		}
18316974Seric 	}
18326974Seric 
183359542Seric 	/* some mailers want extra blank line at end of message */
183459542Seric 	if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n')
183559542Seric 		putline("", fp, m);
183659542Seric 
18376974Seric 	(void) fflush(fp);
18386974Seric 	if (ferror(fp) && errno != EPIPE)
18396974Seric 	{
18406974Seric 		syserr("putbody: write error");
18416974Seric 		ExitStat = EX_IOERR;
18426974Seric 	}
18436974Seric 	errno = 0;
18446974Seric }
18456974Seric /*
1846294Seric **  MAILFILE -- Send a message to a file.
1847294Seric **
18484327Seric **	If the file has the setuid/setgid bits set, but NO execute
18494327Seric **	bits, sendmail will try to become the owner of that file
18504327Seric **	rather than the real user.  Obviously, this only works if
18514327Seric **	sendmail runs as root.
18524327Seric **
18539370Seric **	This could be done as a subordinate mailer, except that it
18549370Seric **	is used implicitly to save messages in ~/dead.letter.  We
18559370Seric **	view this as being sufficiently important as to include it
18569370Seric **	here.  For example, if the system is dying, we shouldn't have
18579370Seric **	to create another process plus some pipes to save the message.
18589370Seric **
1859294Seric **	Parameters:
1860294Seric **		filename -- the name of the file to send to.
18614397Seric **		ctladdr -- the controlling address header -- includes
18624397Seric **			the userid/groupid to be when sending.
1863294Seric **
1864294Seric **	Returns:
1865294Seric **		The exit code associated with the operation.
1866294Seric **
1867294Seric **	Side Effects:
1868294Seric **		none.
1869294Seric */
1870294Seric 
187154967Seric mailfile(filename, ctladdr, e)
1872294Seric 	char *filename;
18734397Seric 	ADDRESS *ctladdr;
187454967Seric 	register ENVELOPE *e;
1875294Seric {
1876294Seric 	register FILE *f;
18774214Seric 	register int pid;
187853751Seric 	int mode;
1879294Seric 
188059267Seric 	if (tTd(11, 1))
188159267Seric 	{
188259267Seric 		printf("mailfile %s\n  ctladdr=", filename);
188359267Seric 		printaddr(ctladdr, FALSE);
188459267Seric 	}
188559267Seric 
188663753Seric 	if (e->e_xfp != NULL)
188763753Seric 		fflush(e->e_xfp);
188863753Seric 
18894214Seric 	/*
18904214Seric 	**  Fork so we can change permissions here.
18914214Seric 	**	Note that we MUST use fork, not vfork, because of
18924214Seric 	**	the complications of calling subroutines, etc.
18934214Seric 	*/
18944067Seric 
18954214Seric 	DOFORK(fork);
18964214Seric 
18974214Seric 	if (pid < 0)
18984214Seric 		return (EX_OSERR);
18994214Seric 	else if (pid == 0)
19004214Seric 	{
19014214Seric 		/* child -- actually write to file */
19024327Seric 		struct stat stb;
19034327Seric 
190464035Seric 		(void) setsignal(SIGINT, SIG_DFL);
190564035Seric 		(void) setsignal(SIGHUP, SIG_DFL);
190664035Seric 		(void) setsignal(SIGTERM, SIG_DFL);
190723102Seric 		(void) umask(OldUmask);
190852673Seric 
19094327Seric 		if (stat(filename, &stb) < 0)
191059745Seric 			stb.st_mode = FileMode;
191153751Seric 		mode = stb.st_mode;
191252673Seric 
191352673Seric 		/* limit the errors to those actually caused in the child */
191452673Seric 		errno = 0;
191552673Seric 		ExitStat = EX_OK;
191652673Seric 
19174327Seric 		if (bitset(0111, stb.st_mode))
19184327Seric 			exit(EX_CANTCREAT);
19194401Seric 		if (ctladdr == NULL)
192040931Srick 			ctladdr = &e->e_from;
192153751Seric 		else
192253751Seric 		{
192353751Seric 			/* ignore setuid and setgid bits */
192453751Seric 			mode &= ~(S_ISGID|S_ISUID);
192553751Seric 		}
192653751Seric 
192740931Srick 		/* we have to open the dfile BEFORE setuid */
192853751Seric 		if (e->e_dfp == NULL && e->e_df != NULL)
192940931Srick 		{
193040931Srick 			e->e_dfp = fopen(e->e_df, "r");
193152673Seric 			if (e->e_dfp == NULL)
193252673Seric 			{
193340931Srick 				syserr("mailfile: Cannot open %s for %s from %s",
193464118Seric 					e->e_df, e->e_to, e->e_from.q_paddr);
193540931Srick 			}
193640931Srick 		}
193740931Srick 
193853751Seric 		if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0)
19394417Seric 		{
194052673Seric 			if (ctladdr->q_uid == 0)
194152673Seric 			{
194240972Sbostic 				(void) initgroups(DefUser, DefGid);
194352673Seric 			}
194452673Seric 			else
194552673Seric 			{
194653751Seric 				(void) initgroups(ctladdr->q_ruser ?
194753751Seric 					ctladdr->q_ruser : ctladdr->q_user,
194840972Sbostic 					ctladdr->q_gid);
194940972Sbostic 			}
19504417Seric 		}
195153751Seric 		if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0)
19524417Seric 		{
19534417Seric 			if (ctladdr->q_uid == 0)
19544417Seric 				(void) setuid(DefUid);
19554417Seric 			else
19564417Seric 				(void) setuid(ctladdr->q_uid);
19574417Seric 		}
195852673Seric 		FileName = filename;
195952673Seric 		LineNumber = 0;
196059745Seric 		f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode);
19614214Seric 		if (f == NULL)
196252673Seric 		{
196364387Seric 			message("554 cannot open: %s", errstring(errno));
19644214Seric 			exit(EX_CANTCREAT);
196552673Seric 		}
19664214Seric 
196759275Seric 		putfromline(f, FileMailer, e);
196859275Seric 		(*e->e_puthdr)(f, FileMailer, e);
196959275Seric 		putline("\n", f, FileMailer);
197059730Seric 		(*e->e_putbody)(f, FileMailer, e, NULL);
197159275Seric 		putline("\n", f, FileMailer);
197252673Seric 		if (ferror(f))
197352673Seric 		{
197464387Seric 			message("451 I/O error: %s", errstring(errno));
197552673Seric 			setstat(EX_IOERR);
197652673Seric 		}
197758680Seric 		(void) xfclose(f, "mailfile", filename);
19784214Seric 		(void) fflush(stdout);
19794417Seric 
19806887Seric 		/* reset ISUID & ISGID bits for paranoid systems */
19814621Seric 		(void) chmod(filename, (int) stb.st_mode);
198252673Seric 		exit(ExitStat);
19834315Seric 		/*NOTREACHED*/
19844214Seric 	}
19854214Seric 	else
19864214Seric 	{
19874214Seric 		/* parent -- wait for exit status */
19889370Seric 		int st;
19894214Seric 
19909370Seric 		st = waitfor(pid);
199164379Seric 		if (WIFEXITED(st))
199264379Seric 			return (WEXITSTATUS(st));
199364379Seric 		else
199464387Seric 		{
199564387Seric 			syserr("child died on signal %d", st);
19969370Seric 			return (EX_UNAVAILABLE);
199764387Seric 		}
199840931Srick 		/*NOTREACHED*/
19994214Seric 	}
2000294Seric }
20014550Seric /*
200257454Seric **  HOSTSIGNATURE -- return the "signature" for a host.
200357454Seric **
200457454Seric **	The signature describes how we are going to send this -- it
200557454Seric **	can be just the hostname (for non-Internet hosts) or can be
200657454Seric **	an ordered list of MX hosts.
200757454Seric **
200857454Seric **	Parameters:
200957454Seric **		m -- the mailer describing this host.
201057454Seric **		host -- the host name.
201157454Seric **		e -- the current envelope.
201257454Seric **
201357454Seric **	Returns:
201457454Seric **		The signature for this host.
201557454Seric **
201657454Seric **	Side Effects:
201757454Seric **		Can tweak the symbol table.
201857454Seric */
201957454Seric 
202057454Seric char *
202157454Seric hostsignature(m, host, e)
202257454Seric 	register MAILER *m;
202357454Seric 	char *host;
202457454Seric 	ENVELOPE *e;
202557454Seric {
202657454Seric 	register char *p;
202757454Seric 	register STAB *s;
202857454Seric 	int i;
202957454Seric 	int len;
203057454Seric #ifdef NAMED_BIND
203157454Seric 	int nmx;
203257454Seric 	auto int rcode;
203359076Seric 	char *hp;
203459076Seric 	char *endp;
203559111Seric 	int oldoptions;
203657454Seric 	char *mxhosts[MAXMXHOSTS + 1];
203757454Seric #endif
203857454Seric 
203957454Seric 	/*
204057454Seric 	**  Check to see if this uses IPC -- if not, it can't have MX records.
204157454Seric 	*/
204257454Seric 
204357454Seric 	p = m->m_mailer;
204457454Seric 	if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
204557454Seric 	{
204657454Seric 		/* just an ordinary mailer */
204757454Seric 		return host;
204857454Seric 	}
204957454Seric 
205057454Seric 	/*
205157454Seric 	**  Look it up in the symbol table.
205257454Seric 	*/
205357454Seric 
205457454Seric 	s = stab(host, ST_HOSTSIG, ST_ENTER);
205557454Seric 	if (s->s_hostsig != NULL)
205657454Seric 		return s->s_hostsig;
205757454Seric 
205857454Seric 	/*
205957454Seric 	**  Not already there -- create a signature.
206057454Seric 	*/
206157454Seric 
206257454Seric #ifdef NAMED_BIND
206359111Seric 	if (ConfigLevel < 2)
206459111Seric 	{
206559111Seric 		oldoptions = _res.options;
206659111Seric 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
206759111Seric 	}
206859111Seric 
206959076Seric 	for (hp = host; hp != NULL; hp = endp)
207057454Seric 	{
207159076Seric 		endp = strchr(hp, ':');
207259076Seric 		if (endp != NULL)
207359076Seric 			*endp = '\0';
207457454Seric 
207559273Seric 		nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
207657454Seric 
207759076Seric 		if (nmx <= 0)
207859076Seric 		{
207959076Seric 			register MCI *mci;
208059076Seric 			extern int errno;
208157454Seric 
208259076Seric 			/* update the connection info for this host */
208359076Seric 			mci = mci_get(hp, m);
208459076Seric 			mci->mci_exitstat = rcode;
208559076Seric 			mci->mci_errno = errno;
208663753Seric #ifdef NAMED_BIND
208763753Seric 			mci->mci_herrno = h_errno;
208863753Seric #endif
208959076Seric 
209059076Seric 			/* and return the original host name as the signature */
209159076Seric 			nmx = 1;
209259076Seric 			mxhosts[0] = hp;
209359076Seric 		}
209459076Seric 
209559076Seric 		len = 0;
209659076Seric 		for (i = 0; i < nmx; i++)
209759076Seric 		{
209859076Seric 			len += strlen(mxhosts[i]) + 1;
209959076Seric 		}
210059076Seric 		if (s->s_hostsig != NULL)
210159076Seric 			len += strlen(s->s_hostsig) + 1;
210259076Seric 		p = xalloc(len);
210359076Seric 		if (s->s_hostsig != NULL)
210459076Seric 		{
210559076Seric 			(void) strcpy(p, s->s_hostsig);
210659076Seric 			free(s->s_hostsig);
210759076Seric 			s->s_hostsig = p;
210859076Seric 			p += strlen(p);
210957454Seric 			*p++ = ':';
211059076Seric 		}
211159076Seric 		else
211259076Seric 			s->s_hostsig = p;
211359076Seric 		for (i = 0; i < nmx; i++)
211459076Seric 		{
211559076Seric 			if (i != 0)
211659076Seric 				*p++ = ':';
211759076Seric 			strcpy(p, mxhosts[i]);
211859076Seric 			p += strlen(p);
211959076Seric 		}
212059076Seric 		if (endp != NULL)
212159076Seric 			*endp++ = ':';
212257454Seric 	}
212357454Seric 	makelower(s->s_hostsig);
212459111Seric 	if (ConfigLevel < 2)
212559111Seric 		_res.options = oldoptions;
212657454Seric #else
212757454Seric 	/* not using BIND -- the signature is just the host name */
212857454Seric 	s->s_hostsig = host;
212957454Seric #endif
213057454Seric 	if (tTd(17, 1))
213157454Seric 		printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
213257454Seric 	return s->s_hostsig;
213357454Seric }
2134