xref: /titanic_54/usr/src/cmd/sendmail/src/deliver.c (revision 7800901e60d340b6af88e94a2149805dcfcaaf56)
17c478bd9Sstevel@tonic-gate /*
24aac33d3Sjbeck  * Copyright (c) 1998-2007 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1993
67c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
97c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
107c478bd9Sstevel@tonic-gate  * the sendmail distribution.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #include <sendmail.h>
1749218d4fSjbeck #include <sm/time.h>
187c478bd9Sstevel@tonic-gate 
19*7800901eSjbeck SM_RCSID("@(#)$Id: deliver.c,v 8.1015 2007/10/17 21:35:30 ca Exp $")
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate #if HASSETUSERCONTEXT
227c478bd9Sstevel@tonic-gate # include <login_cap.h>
237c478bd9Sstevel@tonic-gate #endif /* HASSETUSERCONTEXT */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #if NETINET || NETINET6
267c478bd9Sstevel@tonic-gate # include <arpa/inet.h>
277c478bd9Sstevel@tonic-gate #endif /* NETINET || NETINET6 */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #if STARTTLS || SASL
307c478bd9Sstevel@tonic-gate # include "sfsasl.h"
317c478bd9Sstevel@tonic-gate #endif /* STARTTLS || SASL */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate static int	deliver __P((ENVELOPE *, ADDRESS *));
347c478bd9Sstevel@tonic-gate static void	dup_queue_file __P((ENVELOPE *, ENVELOPE *, int));
357c478bd9Sstevel@tonic-gate static void	mailfiletimeout __P((int));
367c478bd9Sstevel@tonic-gate static void	endwaittimeout __P((int));
377c478bd9Sstevel@tonic-gate static int	parse_hostsignature __P((char *, char **, MAILER *));
387c478bd9Sstevel@tonic-gate static void	sendenvelope __P((ENVELOPE *, int));
397c478bd9Sstevel@tonic-gate static int	coloncmp __P((const char *, const char *));
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #if STARTTLS
427c478bd9Sstevel@tonic-gate static int	starttls __P((MAILER *, MCI *, ENVELOPE *));
437c478bd9Sstevel@tonic-gate static int	endtlsclt __P((MCI *));
447c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
457c478bd9Sstevel@tonic-gate # if STARTTLS || SASL
467c478bd9Sstevel@tonic-gate static bool	iscltflgset __P((ENVELOPE *, int));
477c478bd9Sstevel@tonic-gate # endif /* STARTTLS || SASL */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate **  SENDALL -- actually send all the messages.
517c478bd9Sstevel@tonic-gate **
527c478bd9Sstevel@tonic-gate **	Parameters:
537c478bd9Sstevel@tonic-gate **		e -- the envelope to send.
547c478bd9Sstevel@tonic-gate **		mode -- the delivery mode to use.  If SM_DEFAULT, use
557c478bd9Sstevel@tonic-gate **			the current e->e_sendmode.
567c478bd9Sstevel@tonic-gate **
577c478bd9Sstevel@tonic-gate **	Returns:
587c478bd9Sstevel@tonic-gate **		none.
597c478bd9Sstevel@tonic-gate **
607c478bd9Sstevel@tonic-gate **	Side Effects:
617c478bd9Sstevel@tonic-gate **		Scans the send lists and sends everything it finds.
627c478bd9Sstevel@tonic-gate **		Delivers any appropriate error messages.
637c478bd9Sstevel@tonic-gate **		If we are running in a non-interactive mode, takes the
647c478bd9Sstevel@tonic-gate **			appropriate action.
657c478bd9Sstevel@tonic-gate */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate void
687c478bd9Sstevel@tonic-gate sendall(e, mode)
697c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
707c478bd9Sstevel@tonic-gate 	int mode;
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	register ADDRESS *q;
737c478bd9Sstevel@tonic-gate 	char *owner;
747c478bd9Sstevel@tonic-gate 	int otherowners;
757c478bd9Sstevel@tonic-gate 	int save_errno;
767c478bd9Sstevel@tonic-gate 	register ENVELOPE *ee;
777c478bd9Sstevel@tonic-gate 	ENVELOPE *splitenv = NULL;
787c478bd9Sstevel@tonic-gate 	int oldverbose = Verbose;
797c478bd9Sstevel@tonic-gate 	bool somedeliveries = false, expensive = false;
807c478bd9Sstevel@tonic-gate 	pid_t pid;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/*
837c478bd9Sstevel@tonic-gate 	**  If this message is to be discarded, don't bother sending
847c478bd9Sstevel@tonic-gate 	**  the message at all.
857c478bd9Sstevel@tonic-gate 	*/
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (bitset(EF_DISCARD, e->e_flags))
887c478bd9Sstevel@tonic-gate 	{
897c478bd9Sstevel@tonic-gate 		if (tTd(13, 1))
907c478bd9Sstevel@tonic-gate 			sm_dprintf("sendall: discarding id %s\n", e->e_id);
917c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_CLRQUEUE;
927c478bd9Sstevel@tonic-gate 		if (LogLevel > 9)
937c478bd9Sstevel@tonic-gate 			logundelrcpts(e, "discarded", 9, true);
947c478bd9Sstevel@tonic-gate 		else if (LogLevel > 4)
957c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_INFO, e->e_id, "discarded");
967c478bd9Sstevel@tonic-gate 		markstats(e, NULL, STATS_REJECT);
977c478bd9Sstevel@tonic-gate 		return;
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	**  If we have had global, fatal errors, don't bother sending
1027c478bd9Sstevel@tonic-gate 	**  the message at all if we are in SMTP mode.  Local errors
1037c478bd9Sstevel@tonic-gate 	**  (e.g., a single address failing) will still cause the other
1047c478bd9Sstevel@tonic-gate 	**  addresses to be sent.
1057c478bd9Sstevel@tonic-gate 	*/
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (bitset(EF_FATALERRS, e->e_flags) &&
1087c478bd9Sstevel@tonic-gate 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
1097c478bd9Sstevel@tonic-gate 	{
1107c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_CLRQUEUE;
1117c478bd9Sstevel@tonic-gate 		return;
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/* determine actual delivery mode */
1157c478bd9Sstevel@tonic-gate 	if (mode == SM_DEFAULT)
1167c478bd9Sstevel@tonic-gate 	{
1177c478bd9Sstevel@tonic-gate 		mode = e->e_sendmode;
1187c478bd9Sstevel@tonic-gate 		if (mode != SM_VERIFY && mode != SM_DEFER &&
1197c478bd9Sstevel@tonic-gate 		    shouldqueue(e->e_msgpriority, e->e_ctime))
1207c478bd9Sstevel@tonic-gate 			mode = SM_QUEUE;
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (tTd(13, 1))
1247c478bd9Sstevel@tonic-gate 	{
1257c478bd9Sstevel@tonic-gate 		sm_dprintf("\n===== SENDALL: mode %c, id %s, e_from ",
1267c478bd9Sstevel@tonic-gate 			mode, e->e_id);
1277c478bd9Sstevel@tonic-gate 		printaddr(sm_debug_file(), &e->e_from, false);
1287c478bd9Sstevel@tonic-gate 		sm_dprintf("\te_flags = ");
1297c478bd9Sstevel@tonic-gate 		printenvflags(e);
1307c478bd9Sstevel@tonic-gate 		sm_dprintf("sendqueue:\n");
1317c478bd9Sstevel@tonic-gate 		printaddr(sm_debug_file(), e->e_sendqueue, true);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/*
1357c478bd9Sstevel@tonic-gate 	**  Do any preprocessing necessary for the mode we are running.
1367c478bd9Sstevel@tonic-gate 	**	Check to make sure the hop count is reasonable.
1377c478bd9Sstevel@tonic-gate 	**	Delete sends to the sender in mailing lists.
1387c478bd9Sstevel@tonic-gate 	*/
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	CurEnv = e;
1417c478bd9Sstevel@tonic-gate 	if (tTd(62, 1))
1427c478bd9Sstevel@tonic-gate 		checkfds(NULL);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (e->e_hopcount > MaxHopCount)
1457c478bd9Sstevel@tonic-gate 	{
1467c478bd9Sstevel@tonic-gate 		char *recip;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		if (e->e_sendqueue != NULL &&
1497c478bd9Sstevel@tonic-gate 		    e->e_sendqueue->q_paddr != NULL)
1507c478bd9Sstevel@tonic-gate 			recip = e->e_sendqueue->q_paddr;
1517c478bd9Sstevel@tonic-gate 		else
1527c478bd9Sstevel@tonic-gate 			recip = "(nobody)";
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		errno = 0;
1557c478bd9Sstevel@tonic-gate 		queueup(e, WILL_BE_QUEUED(mode), false);
1567c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_FATALERRS|EF_PM_NOTIFY|EF_CLRQUEUE;
1577c478bd9Sstevel@tonic-gate 		ExitStat = EX_UNAVAILABLE;
1587c478bd9Sstevel@tonic-gate 		syserr("554 5.4.6 Too many hops %d (%d max): from %s via %s, to %s",
1597c478bd9Sstevel@tonic-gate 		       e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
1607c478bd9Sstevel@tonic-gate 		       RealHostName == NULL ? "localhost" : RealHostName,
1617c478bd9Sstevel@tonic-gate 		       recip);
1627c478bd9Sstevel@tonic-gate 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1637c478bd9Sstevel@tonic-gate 		{
1647c478bd9Sstevel@tonic-gate 			if (QS_IS_DEAD(q->q_state))
1657c478bd9Sstevel@tonic-gate 				continue;
1667c478bd9Sstevel@tonic-gate 			q->q_state = QS_BADADDR;
1677c478bd9Sstevel@tonic-gate 			q->q_status = "5.4.6";
1687c478bd9Sstevel@tonic-gate 			q->q_rstatus = "554 5.4.6 Too many hops";
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 		return;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	**  Do sender deletion.
1757c478bd9Sstevel@tonic-gate 	**
1767c478bd9Sstevel@tonic-gate 	**	If the sender should be queued up, skip this.
1777c478bd9Sstevel@tonic-gate 	**	This can happen if the name server is hosed when you
1787c478bd9Sstevel@tonic-gate 	**	are trying to send mail.  The result is that the sender
1797c478bd9Sstevel@tonic-gate 	**	is instantiated in the queue as a recipient.
1807c478bd9Sstevel@tonic-gate 	*/
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (!bitset(EF_METOO, e->e_flags) &&
1837c478bd9Sstevel@tonic-gate 	    !QS_IS_QUEUEUP(e->e_from.q_state))
1847c478bd9Sstevel@tonic-gate 	{
1857c478bd9Sstevel@tonic-gate 		if (tTd(13, 5))
1867c478bd9Sstevel@tonic-gate 		{
1877c478bd9Sstevel@tonic-gate 			sm_dprintf("sendall: QS_SENDER ");
1887c478bd9Sstevel@tonic-gate 			printaddr(sm_debug_file(), &e->e_from, false);
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 		e->e_from.q_state = QS_SENDER;
1917c478bd9Sstevel@tonic-gate 		(void) recipient(&e->e_from, &e->e_sendqueue, 0, e);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/*
1957c478bd9Sstevel@tonic-gate 	**  Handle alias owners.
1967c478bd9Sstevel@tonic-gate 	**
1977c478bd9Sstevel@tonic-gate 	**	We scan up the q_alias chain looking for owners.
1987c478bd9Sstevel@tonic-gate 	**	We discard owners that are the same as the return path.
1997c478bd9Sstevel@tonic-gate 	*/
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2027c478bd9Sstevel@tonic-gate 	{
2037c478bd9Sstevel@tonic-gate 		register struct address *a;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
2067c478bd9Sstevel@tonic-gate 			continue;
2077c478bd9Sstevel@tonic-gate 		if (a != NULL)
2087c478bd9Sstevel@tonic-gate 			q->q_owner = a->q_owner;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		if (q->q_owner != NULL &&
2117c478bd9Sstevel@tonic-gate 		    !QS_IS_DEAD(q->q_state) &&
2127c478bd9Sstevel@tonic-gate 		    strcmp(q->q_owner, e->e_from.q_paddr) == 0)
2137c478bd9Sstevel@tonic-gate 			q->q_owner = NULL;
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (tTd(13, 25))
2177c478bd9Sstevel@tonic-gate 	{
2187c478bd9Sstevel@tonic-gate 		sm_dprintf("\nAfter first owner pass, sendq =\n");
2197c478bd9Sstevel@tonic-gate 		printaddr(sm_debug_file(), e->e_sendqueue, true);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	owner = "";
2237c478bd9Sstevel@tonic-gate 	otherowners = 1;
2247c478bd9Sstevel@tonic-gate 	while (owner != NULL && otherowners > 0)
2257c478bd9Sstevel@tonic-gate 	{
2267c478bd9Sstevel@tonic-gate 		if (tTd(13, 28))
2277c478bd9Sstevel@tonic-gate 			sm_dprintf("owner = \"%s\", otherowners = %d\n",
2287c478bd9Sstevel@tonic-gate 				   owner, otherowners);
2297c478bd9Sstevel@tonic-gate 		owner = NULL;
2307c478bd9Sstevel@tonic-gate 		otherowners = bitset(EF_SENDRECEIPT, e->e_flags) ? 1 : 0;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2337c478bd9Sstevel@tonic-gate 		{
2347c478bd9Sstevel@tonic-gate 			if (tTd(13, 30))
2357c478bd9Sstevel@tonic-gate 			{
2367c478bd9Sstevel@tonic-gate 				sm_dprintf("Checking ");
2377c478bd9Sstevel@tonic-gate 				printaddr(sm_debug_file(), q, false);
2387c478bd9Sstevel@tonic-gate 			}
2397c478bd9Sstevel@tonic-gate 			if (QS_IS_DEAD(q->q_state))
2407c478bd9Sstevel@tonic-gate 			{
2417c478bd9Sstevel@tonic-gate 				if (tTd(13, 30))
2427c478bd9Sstevel@tonic-gate 					sm_dprintf("    ... QS_IS_DEAD\n");
2437c478bd9Sstevel@tonic-gate 				continue;
2447c478bd9Sstevel@tonic-gate 			}
2457c478bd9Sstevel@tonic-gate 			if (tTd(13, 29) && !tTd(13, 30))
2467c478bd9Sstevel@tonic-gate 			{
2477c478bd9Sstevel@tonic-gate 				sm_dprintf("Checking ");
2487c478bd9Sstevel@tonic-gate 				printaddr(sm_debug_file(), q, false);
2497c478bd9Sstevel@tonic-gate 			}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 			if (q->q_owner != NULL)
2527c478bd9Sstevel@tonic-gate 			{
2537c478bd9Sstevel@tonic-gate 				if (owner == NULL)
2547c478bd9Sstevel@tonic-gate 				{
2557c478bd9Sstevel@tonic-gate 					if (tTd(13, 40))
2567c478bd9Sstevel@tonic-gate 						sm_dprintf("    ... First owner = \"%s\"\n",
2577c478bd9Sstevel@tonic-gate 							   q->q_owner);
2587c478bd9Sstevel@tonic-gate 					owner = q->q_owner;
2597c478bd9Sstevel@tonic-gate 				}
2607c478bd9Sstevel@tonic-gate 				else if (owner != q->q_owner)
2617c478bd9Sstevel@tonic-gate 				{
2627c478bd9Sstevel@tonic-gate 					if (strcmp(owner, q->q_owner) == 0)
2637c478bd9Sstevel@tonic-gate 					{
2647c478bd9Sstevel@tonic-gate 						if (tTd(13, 40))
2657c478bd9Sstevel@tonic-gate 							sm_dprintf("    ... Same owner = \"%s\"\n",
2667c478bd9Sstevel@tonic-gate 								   owner);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 						/* make future comparisons cheap */
2697c478bd9Sstevel@tonic-gate 						q->q_owner = owner;
2707c478bd9Sstevel@tonic-gate 					}
2717c478bd9Sstevel@tonic-gate 					else
2727c478bd9Sstevel@tonic-gate 					{
2737c478bd9Sstevel@tonic-gate 						if (tTd(13, 40))
2747c478bd9Sstevel@tonic-gate 							sm_dprintf("    ... Another owner \"%s\"\n",
2757c478bd9Sstevel@tonic-gate 								   q->q_owner);
2767c478bd9Sstevel@tonic-gate 						otherowners++;
2777c478bd9Sstevel@tonic-gate 					}
2787c478bd9Sstevel@tonic-gate 					owner = q->q_owner;
2797c478bd9Sstevel@tonic-gate 				}
2807c478bd9Sstevel@tonic-gate 				else if (tTd(13, 40))
2817c478bd9Sstevel@tonic-gate 					sm_dprintf("    ... Same owner = \"%s\"\n",
2827c478bd9Sstevel@tonic-gate 						   owner);
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 			else
2857c478bd9Sstevel@tonic-gate 			{
2867c478bd9Sstevel@tonic-gate 				if (tTd(13, 40))
2877c478bd9Sstevel@tonic-gate 					sm_dprintf("    ... Null owner\n");
2887c478bd9Sstevel@tonic-gate 				otherowners++;
2897c478bd9Sstevel@tonic-gate 			}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 			if (QS_IS_BADADDR(q->q_state))
2927c478bd9Sstevel@tonic-gate 			{
2937c478bd9Sstevel@tonic-gate 				if (tTd(13, 30))
2947c478bd9Sstevel@tonic-gate 					sm_dprintf("    ... QS_IS_BADADDR\n");
2957c478bd9Sstevel@tonic-gate 				continue;
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 			if (QS_IS_QUEUEUP(q->q_state))
2997c478bd9Sstevel@tonic-gate 			{
3007c478bd9Sstevel@tonic-gate 				MAILER *m = q->q_mailer;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 				/*
3037c478bd9Sstevel@tonic-gate 				**  If we have temporary address failures
3047c478bd9Sstevel@tonic-gate 				**  (e.g., dns failure) and a fallback MX is
3057c478bd9Sstevel@tonic-gate 				**  set, send directly to the fallback MX host.
3067c478bd9Sstevel@tonic-gate 				*/
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 				if (FallbackMX != NULL &&
3097c478bd9Sstevel@tonic-gate 				    !wordinclass(FallbackMX, 'w') &&
3107c478bd9Sstevel@tonic-gate 				    mode != SM_VERIFY &&
3117c478bd9Sstevel@tonic-gate 				    !bitnset(M_NOMX, m->m_flags) &&
3127c478bd9Sstevel@tonic-gate 				    strcmp(m->m_mailer, "[IPC]") == 0 &&
3137c478bd9Sstevel@tonic-gate 				    m->m_argv[0] != NULL &&
3147c478bd9Sstevel@tonic-gate 				    strcmp(m->m_argv[0], "TCP") == 0)
3157c478bd9Sstevel@tonic-gate 				{
3167c478bd9Sstevel@tonic-gate 					int len;
3177c478bd9Sstevel@tonic-gate 					char *p;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 					if (tTd(13, 30))
3207c478bd9Sstevel@tonic-gate 						sm_dprintf("    ... FallbackMX\n");
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 					len = strlen(FallbackMX) + 1;
3237c478bd9Sstevel@tonic-gate 					p = sm_rpool_malloc_x(e->e_rpool, len);
3247c478bd9Sstevel@tonic-gate 					(void) sm_strlcpy(p, FallbackMX, len);
3257c478bd9Sstevel@tonic-gate 					q->q_state = QS_OK;
3267c478bd9Sstevel@tonic-gate 					q->q_host = p;
3277c478bd9Sstevel@tonic-gate 				}
3287c478bd9Sstevel@tonic-gate 				else
3297c478bd9Sstevel@tonic-gate 				{
3307c478bd9Sstevel@tonic-gate 					if (tTd(13, 30))
3317c478bd9Sstevel@tonic-gate 						sm_dprintf("    ... QS_IS_QUEUEUP\n");
3327c478bd9Sstevel@tonic-gate 					continue;
3337c478bd9Sstevel@tonic-gate 				}
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 			/*
3377c478bd9Sstevel@tonic-gate 			**  If this mailer is expensive, and if we don't
3387c478bd9Sstevel@tonic-gate 			**  want to make connections now, just mark these
3397c478bd9Sstevel@tonic-gate 			**  addresses and return.  This is useful if we
3407c478bd9Sstevel@tonic-gate 			**  want to batch connections to reduce load.  This
3417c478bd9Sstevel@tonic-gate 			**  will cause the messages to be queued up, and a
3427c478bd9Sstevel@tonic-gate 			**  daemon will come along to send the messages later.
3437c478bd9Sstevel@tonic-gate 			*/
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 			if (NoConnect && !Verbose &&
3467c478bd9Sstevel@tonic-gate 			    bitnset(M_EXPENSIVE, q->q_mailer->m_flags))
3477c478bd9Sstevel@tonic-gate 			{
3487c478bd9Sstevel@tonic-gate 				if (tTd(13, 30))
3497c478bd9Sstevel@tonic-gate 					sm_dprintf("    ... expensive\n");
3507c478bd9Sstevel@tonic-gate 				q->q_state = QS_QUEUEUP;
3517c478bd9Sstevel@tonic-gate 				expensive = true;
3527c478bd9Sstevel@tonic-gate 			}
3537c478bd9Sstevel@tonic-gate 			else if (bitnset(M_HOLD, q->q_mailer->m_flags) &&
3547c478bd9Sstevel@tonic-gate 				 QueueLimitId == NULL &&
3557c478bd9Sstevel@tonic-gate 				 QueueLimitSender == NULL &&
3567c478bd9Sstevel@tonic-gate 				 QueueLimitRecipient == NULL)
3577c478bd9Sstevel@tonic-gate 			{
3587c478bd9Sstevel@tonic-gate 				if (tTd(13, 30))
3597c478bd9Sstevel@tonic-gate 					sm_dprintf("    ... hold\n");
3607c478bd9Sstevel@tonic-gate 				q->q_state = QS_QUEUEUP;
3617c478bd9Sstevel@tonic-gate 				expensive = true;
3627c478bd9Sstevel@tonic-gate 			}
3637c478bd9Sstevel@tonic-gate 			else if (QueueMode != QM_QUARANTINE &&
3647c478bd9Sstevel@tonic-gate 				 e->e_quarmsg != NULL)
3657c478bd9Sstevel@tonic-gate 			{
3667c478bd9Sstevel@tonic-gate 				if (tTd(13, 30))
3677c478bd9Sstevel@tonic-gate 					sm_dprintf("    ... quarantine: %s\n",
3687c478bd9Sstevel@tonic-gate 						   e->e_quarmsg);
3697c478bd9Sstevel@tonic-gate 				q->q_state = QS_QUEUEUP;
3707c478bd9Sstevel@tonic-gate 				expensive = true;
3717c478bd9Sstevel@tonic-gate 			}
3727c478bd9Sstevel@tonic-gate 			else
3737c478bd9Sstevel@tonic-gate 			{
3747c478bd9Sstevel@tonic-gate 				if (tTd(13, 30))
3757c478bd9Sstevel@tonic-gate 					sm_dprintf("    ... deliverable\n");
3767c478bd9Sstevel@tonic-gate 				somedeliveries = true;
3777c478bd9Sstevel@tonic-gate 			}
3787c478bd9Sstevel@tonic-gate 		}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		if (owner != NULL && otherowners > 0)
3817c478bd9Sstevel@tonic-gate 		{
3827c478bd9Sstevel@tonic-gate 			/*
3837c478bd9Sstevel@tonic-gate 			**  Split this envelope into two.
3847c478bd9Sstevel@tonic-gate 			*/
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 			ee = (ENVELOPE *) sm_rpool_malloc_x(e->e_rpool,
387058561cbSjbeck 							    sizeof(*ee));
3887c478bd9Sstevel@tonic-gate 			STRUCTCOPY(*e, *ee);
3897c478bd9Sstevel@tonic-gate 			ee->e_message = NULL;
3907c478bd9Sstevel@tonic-gate 			ee->e_id = NULL;
3917c478bd9Sstevel@tonic-gate 			assign_queueid(ee);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 			if (tTd(13, 1))
3947c478bd9Sstevel@tonic-gate 				sm_dprintf("sendall: split %s into %s, owner = \"%s\", otherowners = %d\n",
3957c478bd9Sstevel@tonic-gate 					   e->e_id, ee->e_id, owner,
3967c478bd9Sstevel@tonic-gate 					   otherowners);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 			ee->e_header = copyheader(e->e_header, ee->e_rpool);
3997c478bd9Sstevel@tonic-gate 			ee->e_sendqueue = copyqueue(e->e_sendqueue,
4007c478bd9Sstevel@tonic-gate 						    ee->e_rpool);
4017c478bd9Sstevel@tonic-gate 			ee->e_errorqueue = copyqueue(e->e_errorqueue,
4027c478bd9Sstevel@tonic-gate 						     ee->e_rpool);
4037c478bd9Sstevel@tonic-gate 			ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS|EF_SENDRECEIPT|EF_RET_PARAM);
4047c478bd9Sstevel@tonic-gate 			ee->e_flags |= EF_NORECEIPT;
4057c478bd9Sstevel@tonic-gate 			setsender(owner, ee, NULL, '\0', true);
4067c478bd9Sstevel@tonic-gate 			if (tTd(13, 5))
4077c478bd9Sstevel@tonic-gate 			{
4087c478bd9Sstevel@tonic-gate 				sm_dprintf("sendall(split): QS_SENDER ");
4097c478bd9Sstevel@tonic-gate 				printaddr(sm_debug_file(), &ee->e_from, false);
4107c478bd9Sstevel@tonic-gate 			}
4117c478bd9Sstevel@tonic-gate 			ee->e_from.q_state = QS_SENDER;
4127c478bd9Sstevel@tonic-gate 			ee->e_dfp = NULL;
4137c478bd9Sstevel@tonic-gate 			ee->e_lockfp = NULL;
4147c478bd9Sstevel@tonic-gate 			ee->e_xfp = NULL;
4157c478bd9Sstevel@tonic-gate 			ee->e_qgrp = e->e_qgrp;
4167c478bd9Sstevel@tonic-gate 			ee->e_qdir = e->e_qdir;
4177c478bd9Sstevel@tonic-gate 			ee->e_errormode = EM_MAIL;
4187c478bd9Sstevel@tonic-gate 			ee->e_sibling = splitenv;
4197c478bd9Sstevel@tonic-gate 			ee->e_statmsg = NULL;
4207c478bd9Sstevel@tonic-gate 			if (e->e_quarmsg != NULL)
4217c478bd9Sstevel@tonic-gate 				ee->e_quarmsg = sm_rpool_strdup_x(ee->e_rpool,
4227c478bd9Sstevel@tonic-gate 								  e->e_quarmsg);
4237c478bd9Sstevel@tonic-gate 			splitenv = ee;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
4267c478bd9Sstevel@tonic-gate 			{
4277c478bd9Sstevel@tonic-gate 				if (q->q_owner == owner)
4287c478bd9Sstevel@tonic-gate 				{
4297c478bd9Sstevel@tonic-gate 					q->q_state = QS_CLONED;
4307c478bd9Sstevel@tonic-gate 					if (tTd(13, 6))
4317c478bd9Sstevel@tonic-gate 						sm_dprintf("\t... stripping %s from original envelope\n",
4327c478bd9Sstevel@tonic-gate 							   q->q_paddr);
4337c478bd9Sstevel@tonic-gate 				}
4347c478bd9Sstevel@tonic-gate 			}
4357c478bd9Sstevel@tonic-gate 			for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
4367c478bd9Sstevel@tonic-gate 			{
4377c478bd9Sstevel@tonic-gate 				if (q->q_owner != owner)
4387c478bd9Sstevel@tonic-gate 				{
4397c478bd9Sstevel@tonic-gate 					q->q_state = QS_CLONED;
4407c478bd9Sstevel@tonic-gate 					if (tTd(13, 6))
4417c478bd9Sstevel@tonic-gate 						sm_dprintf("\t... dropping %s from cloned envelope\n",
4427c478bd9Sstevel@tonic-gate 							   q->q_paddr);
4437c478bd9Sstevel@tonic-gate 				}
4447c478bd9Sstevel@tonic-gate 				else
4457c478bd9Sstevel@tonic-gate 				{
4467c478bd9Sstevel@tonic-gate 					/* clear DSN parameters */
4477c478bd9Sstevel@tonic-gate 					q->q_flags &= ~(QHASNOTIFY|Q_PINGFLAGS);
4487c478bd9Sstevel@tonic-gate 					q->q_flags |= DefaultNotify & ~QPINGONSUCCESS;
4497c478bd9Sstevel@tonic-gate 					if (tTd(13, 6))
4507c478bd9Sstevel@tonic-gate 						sm_dprintf("\t... moving %s to cloned envelope\n",
4517c478bd9Sstevel@tonic-gate 							   q->q_paddr);
4527c478bd9Sstevel@tonic-gate 				}
4537c478bd9Sstevel@tonic-gate 			}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 			if (mode != SM_VERIFY && bitset(EF_HAS_DF, e->e_flags))
4567c478bd9Sstevel@tonic-gate 				dup_queue_file(e, ee, DATAFL_LETTER);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 			/*
4597c478bd9Sstevel@tonic-gate 			**  Give the split envelope access to the parent
4607c478bd9Sstevel@tonic-gate 			**  transcript file for errors obtained while
4617c478bd9Sstevel@tonic-gate 			**  processing the recipients (done before the
4627c478bd9Sstevel@tonic-gate 			**  envelope splitting).
4637c478bd9Sstevel@tonic-gate 			*/
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 			if (e->e_xfp != NULL)
4667c478bd9Sstevel@tonic-gate 				ee->e_xfp = sm_io_dup(e->e_xfp);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 			/* failed to dup e->e_xfp, start a new transcript */
4697c478bd9Sstevel@tonic-gate 			if (ee->e_xfp == NULL)
4707c478bd9Sstevel@tonic-gate 				openxscript(ee);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 			if (mode != SM_VERIFY && LogLevel > 4)
4737c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
4747c478bd9Sstevel@tonic-gate 					  "%s: clone: owner=%s",
4757c478bd9Sstevel@tonic-gate 					  ee->e_id, owner);
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (owner != NULL)
4807c478bd9Sstevel@tonic-gate 	{
4817c478bd9Sstevel@tonic-gate 		setsender(owner, e, NULL, '\0', true);
4827c478bd9Sstevel@tonic-gate 		if (tTd(13, 5))
4837c478bd9Sstevel@tonic-gate 		{
4847c478bd9Sstevel@tonic-gate 			sm_dprintf("sendall(owner): QS_SENDER ");
4857c478bd9Sstevel@tonic-gate 			printaddr(sm_debug_file(), &e->e_from, false);
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 		e->e_from.q_state = QS_SENDER;
4887c478bd9Sstevel@tonic-gate 		e->e_errormode = EM_MAIL;
4897c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_NORECEIPT;
4907c478bd9Sstevel@tonic-gate 		e->e_flags &= ~EF_FATALERRS;
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	/* if nothing to be delivered, just queue up everything */
4947c478bd9Sstevel@tonic-gate 	if (!somedeliveries && !WILL_BE_QUEUED(mode) &&
4957c478bd9Sstevel@tonic-gate 	    mode != SM_VERIFY)
4967c478bd9Sstevel@tonic-gate 	{
4977c478bd9Sstevel@tonic-gate 		time_t now;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		if (tTd(13, 29))
500*7800901eSjbeck 			sm_dprintf("No deliveries: auto-queueing\n");
5017c478bd9Sstevel@tonic-gate 		mode = SM_QUEUE;
5027c478bd9Sstevel@tonic-gate 		now = curtime();
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 		/* treat this as a delivery in terms of counting tries */
5057c478bd9Sstevel@tonic-gate 		e->e_dtime = now;
5067c478bd9Sstevel@tonic-gate 		if (!expensive)
5077c478bd9Sstevel@tonic-gate 			e->e_ntries++;
5087c478bd9Sstevel@tonic-gate 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
5097c478bd9Sstevel@tonic-gate 		{
5107c478bd9Sstevel@tonic-gate 			ee->e_dtime = now;
5117c478bd9Sstevel@tonic-gate 			if (!expensive)
5127c478bd9Sstevel@tonic-gate 				ee->e_ntries++;
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	if ((WILL_BE_QUEUED(mode) || mode == SM_FORK ||
5177c478bd9Sstevel@tonic-gate 	     (mode != SM_VERIFY &&
5187c478bd9Sstevel@tonic-gate 	      (SuperSafe == SAFE_REALLY ||
5197c478bd9Sstevel@tonic-gate 	       SuperSafe == SAFE_REALLY_POSTMILTER))) &&
5207c478bd9Sstevel@tonic-gate 	    (!bitset(EF_INQUEUE, e->e_flags) || splitenv != NULL))
5217c478bd9Sstevel@tonic-gate 	{
5227c478bd9Sstevel@tonic-gate 		bool msync;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		/*
5257c478bd9Sstevel@tonic-gate 		**  Be sure everything is instantiated in the queue.
5267c478bd9Sstevel@tonic-gate 		**  Split envelopes first in case the machine crashes.
5277c478bd9Sstevel@tonic-gate 		**  If the original were done first, we may lose
5287c478bd9Sstevel@tonic-gate 		**  recipients.
5297c478bd9Sstevel@tonic-gate 		*/
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate #if !HASFLOCK
5327c478bd9Sstevel@tonic-gate 		msync = false;
5337c478bd9Sstevel@tonic-gate #else /* !HASFLOCK */
5347c478bd9Sstevel@tonic-gate 		msync = mode == SM_FORK;
5357c478bd9Sstevel@tonic-gate #endif /* !HASFLOCK */
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
5387c478bd9Sstevel@tonic-gate 			queueup(ee, WILL_BE_QUEUED(mode), msync);
5397c478bd9Sstevel@tonic-gate 		queueup(e, WILL_BE_QUEUED(mode), msync);
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	if (tTd(62, 10))
5437c478bd9Sstevel@tonic-gate 		checkfds("after envelope splitting");
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	**  If we belong in background, fork now.
5477c478bd9Sstevel@tonic-gate 	*/
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (tTd(13, 20))
5507c478bd9Sstevel@tonic-gate 	{
5517c478bd9Sstevel@tonic-gate 		sm_dprintf("sendall: final mode = %c\n", mode);
5527c478bd9Sstevel@tonic-gate 		if (tTd(13, 21))
5537c478bd9Sstevel@tonic-gate 		{
5547c478bd9Sstevel@tonic-gate 			sm_dprintf("\n================ Final Send Queue(s) =====================\n");
5557c478bd9Sstevel@tonic-gate 			sm_dprintf("\n  *** Envelope %s, e_from=%s ***\n",
5567c478bd9Sstevel@tonic-gate 				   e->e_id, e->e_from.q_paddr);
5577c478bd9Sstevel@tonic-gate 			printaddr(sm_debug_file(), e->e_sendqueue, true);
5587c478bd9Sstevel@tonic-gate 			for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
5597c478bd9Sstevel@tonic-gate 			{
5607c478bd9Sstevel@tonic-gate 				sm_dprintf("\n  *** Envelope %s, e_from=%s ***\n",
5617c478bd9Sstevel@tonic-gate 					   ee->e_id, ee->e_from.q_paddr);
5627c478bd9Sstevel@tonic-gate 				printaddr(sm_debug_file(), ee->e_sendqueue, true);
5637c478bd9Sstevel@tonic-gate 			}
5647c478bd9Sstevel@tonic-gate 			sm_dprintf("==========================================================\n\n");
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	switch (mode)
5687c478bd9Sstevel@tonic-gate 	{
5697c478bd9Sstevel@tonic-gate 	  case SM_VERIFY:
5707c478bd9Sstevel@tonic-gate 		Verbose = 2;
5717c478bd9Sstevel@tonic-gate 		break;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	  case SM_QUEUE:
5747c478bd9Sstevel@tonic-gate 	  case SM_DEFER:
5757c478bd9Sstevel@tonic-gate #if HASFLOCK
5767c478bd9Sstevel@tonic-gate   queueonly:
5777c478bd9Sstevel@tonic-gate #endif /* HASFLOCK */
5787c478bd9Sstevel@tonic-gate 		if (e->e_nrcpts > 0)
5797c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_INQUEUE;
5807c478bd9Sstevel@tonic-gate 		dropenvelope(e, splitenv != NULL, true);
5817c478bd9Sstevel@tonic-gate 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
5827c478bd9Sstevel@tonic-gate 		{
5837c478bd9Sstevel@tonic-gate 			if (ee->e_nrcpts > 0)
5847c478bd9Sstevel@tonic-gate 				ee->e_flags |= EF_INQUEUE;
5857c478bd9Sstevel@tonic-gate 			dropenvelope(ee, false, true);
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 		return;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	  case SM_FORK:
5907c478bd9Sstevel@tonic-gate 		if (e->e_xfp != NULL)
5917c478bd9Sstevel@tonic-gate 			(void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate #if !HASFLOCK
5947c478bd9Sstevel@tonic-gate 		/*
5957c478bd9Sstevel@tonic-gate 		**  Since fcntl locking has the interesting semantic that
5967c478bd9Sstevel@tonic-gate 		**  the lock is owned by a process, not by an open file
5977c478bd9Sstevel@tonic-gate 		**  descriptor, we have to flush this to the queue, and
5987c478bd9Sstevel@tonic-gate 		**  then restart from scratch in the child.
5997c478bd9Sstevel@tonic-gate 		*/
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		{
6027c478bd9Sstevel@tonic-gate 			/* save id for future use */
6037c478bd9Sstevel@tonic-gate 			char *qid = e->e_id;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 			/* now drop the envelope in the parent */
6067c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_INQUEUE;
6077c478bd9Sstevel@tonic-gate 			dropenvelope(e, splitenv != NULL, false);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 			/* arrange to reacquire lock after fork */
6107c478bd9Sstevel@tonic-gate 			e->e_id = qid;
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
6147c478bd9Sstevel@tonic-gate 		{
6157c478bd9Sstevel@tonic-gate 			/* save id for future use */
6167c478bd9Sstevel@tonic-gate 			char *qid = ee->e_id;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 			/* drop envelope in parent */
6197c478bd9Sstevel@tonic-gate 			ee->e_flags |= EF_INQUEUE;
6207c478bd9Sstevel@tonic-gate 			dropenvelope(ee, false, false);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 			/* and save qid for reacquisition */
6237c478bd9Sstevel@tonic-gate 			ee->e_id = qid;
6247c478bd9Sstevel@tonic-gate 		}
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate #endif /* !HASFLOCK */
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		/*
6297c478bd9Sstevel@tonic-gate 		**  Since the delivery may happen in a child and the parent
6307c478bd9Sstevel@tonic-gate 		**  does not wait, the parent may close the maps thereby
6317c478bd9Sstevel@tonic-gate 		**  removing any shared memory used by the map.  Therefore,
6327c478bd9Sstevel@tonic-gate 		**  close the maps now so the child will dynamically open
6337c478bd9Sstevel@tonic-gate 		**  them if necessary.
6347c478bd9Sstevel@tonic-gate 		*/
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		closemaps(false);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		pid = fork();
6397c478bd9Sstevel@tonic-gate 		if (pid < 0)
6407c478bd9Sstevel@tonic-gate 		{
6417c478bd9Sstevel@tonic-gate 			syserr("deliver: fork 1");
6427c478bd9Sstevel@tonic-gate #if HASFLOCK
6437c478bd9Sstevel@tonic-gate 			goto queueonly;
6447c478bd9Sstevel@tonic-gate #else /* HASFLOCK */
6457c478bd9Sstevel@tonic-gate 			e->e_id = NULL;
6467c478bd9Sstevel@tonic-gate 			for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
6477c478bd9Sstevel@tonic-gate 				ee->e_id = NULL;
6487c478bd9Sstevel@tonic-gate 			return;
6497c478bd9Sstevel@tonic-gate #endif /* HASFLOCK */
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		else if (pid > 0)
6527c478bd9Sstevel@tonic-gate 		{
6537c478bd9Sstevel@tonic-gate #if HASFLOCK
6547c478bd9Sstevel@tonic-gate 			/* be sure we leave the temp files to our child */
6557c478bd9Sstevel@tonic-gate 			/* close any random open files in the envelope */
6567c478bd9Sstevel@tonic-gate 			closexscript(e);
6577c478bd9Sstevel@tonic-gate 			if (e->e_dfp != NULL)
6587c478bd9Sstevel@tonic-gate 				(void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT);
6597c478bd9Sstevel@tonic-gate 			e->e_dfp = NULL;
6607c478bd9Sstevel@tonic-gate 			e->e_flags &= ~EF_HAS_DF;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 			/* can't call unlockqueue to avoid unlink of xfp */
6637c478bd9Sstevel@tonic-gate 			if (e->e_lockfp != NULL)
6647c478bd9Sstevel@tonic-gate 				(void) sm_io_close(e->e_lockfp, SM_TIME_DEFAULT);
6657c478bd9Sstevel@tonic-gate 			else
6667c478bd9Sstevel@tonic-gate 				syserr("%s: sendall: null lockfp", e->e_id);
6677c478bd9Sstevel@tonic-gate 			e->e_lockfp = NULL;
6687c478bd9Sstevel@tonic-gate #endif /* HASFLOCK */
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 			/* make sure the parent doesn't own the envelope */
6717c478bd9Sstevel@tonic-gate 			e->e_id = NULL;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate #if USE_DOUBLE_FORK
6747c478bd9Sstevel@tonic-gate 			/* catch intermediate zombie */
6757c478bd9Sstevel@tonic-gate 			(void) waitfor(pid);
6767c478bd9Sstevel@tonic-gate #endif /* USE_DOUBLE_FORK */
6777c478bd9Sstevel@tonic-gate 			return;
6787c478bd9Sstevel@tonic-gate 		}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		/* Reset global flags */
6817c478bd9Sstevel@tonic-gate 		RestartRequest = NULL;
6827c478bd9Sstevel@tonic-gate 		RestartWorkGroup = false;
6837c478bd9Sstevel@tonic-gate 		ShutdownRequest = NULL;
6847c478bd9Sstevel@tonic-gate 		PendingSignal = 0;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 		/*
6877c478bd9Sstevel@tonic-gate 		**  Initialize exception stack and default exception
6887c478bd9Sstevel@tonic-gate 		**  handler for child process.
6897c478bd9Sstevel@tonic-gate 		*/
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		sm_exc_newthread(fatal_error);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		/*
6947c478bd9Sstevel@tonic-gate 		**  Since we have accepted responsbility for the message,
6957c478bd9Sstevel@tonic-gate 		**  change the SIGTERM handler.  intsig() (the old handler)
6967c478bd9Sstevel@tonic-gate 		**  would remove the envelope if this was a command line
6977c478bd9Sstevel@tonic-gate 		**  message submission.
6987c478bd9Sstevel@tonic-gate 		*/
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		(void) sm_signal(SIGTERM, SIG_DFL);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate #if USE_DOUBLE_FORK
7037c478bd9Sstevel@tonic-gate 		/* double fork to avoid zombies */
7047c478bd9Sstevel@tonic-gate 		pid = fork();
7057c478bd9Sstevel@tonic-gate 		if (pid > 0)
7067c478bd9Sstevel@tonic-gate 			exit(EX_OK);
7077c478bd9Sstevel@tonic-gate 		save_errno = errno;
7087c478bd9Sstevel@tonic-gate #endif /* USE_DOUBLE_FORK */
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 		CurrentPid = getpid();
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		/* be sure we are immune from the terminal */
7137c478bd9Sstevel@tonic-gate 		disconnect(2, e);
7147c478bd9Sstevel@tonic-gate 		clearstats();
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		/* prevent parent from waiting if there was an error */
7177c478bd9Sstevel@tonic-gate 		if (pid < 0)
7187c478bd9Sstevel@tonic-gate 		{
7197c478bd9Sstevel@tonic-gate 			errno = save_errno;
7207c478bd9Sstevel@tonic-gate 			syserr("deliver: fork 2");
7217c478bd9Sstevel@tonic-gate #if HASFLOCK
7227c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_INQUEUE;
7237c478bd9Sstevel@tonic-gate #else /* HASFLOCK */
7247c478bd9Sstevel@tonic-gate 			e->e_id = NULL;
7257c478bd9Sstevel@tonic-gate #endif /* HASFLOCK */
7267c478bd9Sstevel@tonic-gate 			finis(true, true, ExitStat);
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		/* be sure to give error messages in child */
7307c478bd9Sstevel@tonic-gate 		QuickAbort = false;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		/*
7337c478bd9Sstevel@tonic-gate 		**  Close any cached connections.
7347c478bd9Sstevel@tonic-gate 		**
7357c478bd9Sstevel@tonic-gate 		**	We don't send the QUIT protocol because the parent
7367c478bd9Sstevel@tonic-gate 		**	still knows about the connection.
7377c478bd9Sstevel@tonic-gate 		**
7387c478bd9Sstevel@tonic-gate 		**	This should only happen when delivering an error
7397c478bd9Sstevel@tonic-gate 		**	message.
7407c478bd9Sstevel@tonic-gate 		*/
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		mci_flush(false, NULL);
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate #if HASFLOCK
7457c478bd9Sstevel@tonic-gate 		break;
7467c478bd9Sstevel@tonic-gate #else /* HASFLOCK */
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		/*
7497c478bd9Sstevel@tonic-gate 		**  Now reacquire and run the various queue files.
7507c478bd9Sstevel@tonic-gate 		*/
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 		for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
7537c478bd9Sstevel@tonic-gate 		{
7547c478bd9Sstevel@tonic-gate 			ENVELOPE *sibling = ee->e_sibling;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 			(void) dowork(ee->e_qgrp, ee->e_qdir, ee->e_id,
7577c478bd9Sstevel@tonic-gate 				      false, false, ee);
7587c478bd9Sstevel@tonic-gate 			ee->e_sibling = sibling;
7597c478bd9Sstevel@tonic-gate 		}
7607c478bd9Sstevel@tonic-gate 		(void) dowork(e->e_qgrp, e->e_qdir, e->e_id,
7617c478bd9Sstevel@tonic-gate 			      false, false, e);
7627c478bd9Sstevel@tonic-gate 		finis(true, true, ExitStat);
7637c478bd9Sstevel@tonic-gate #endif /* HASFLOCK */
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	sendenvelope(e, mode);
7677c478bd9Sstevel@tonic-gate 	dropenvelope(e, true, true);
7687c478bd9Sstevel@tonic-gate 	for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
7697c478bd9Sstevel@tonic-gate 	{
7707c478bd9Sstevel@tonic-gate 		CurEnv = ee;
7717c478bd9Sstevel@tonic-gate 		if (mode != SM_VERIFY)
7727c478bd9Sstevel@tonic-gate 			openxscript(ee);
7737c478bd9Sstevel@tonic-gate 		sendenvelope(ee, mode);
7747c478bd9Sstevel@tonic-gate 		dropenvelope(ee, true, true);
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 	CurEnv = e;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	Verbose = oldverbose;
7797c478bd9Sstevel@tonic-gate 	if (mode == SM_FORK)
7807c478bd9Sstevel@tonic-gate 		finis(true, true, ExitStat);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate static void
7847c478bd9Sstevel@tonic-gate sendenvelope(e, mode)
7857c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
7867c478bd9Sstevel@tonic-gate 	int mode;
7877c478bd9Sstevel@tonic-gate {
7887c478bd9Sstevel@tonic-gate 	register ADDRESS *q;
7897c478bd9Sstevel@tonic-gate 	bool didany;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	if (tTd(13, 10))
7927c478bd9Sstevel@tonic-gate 		sm_dprintf("sendenvelope(%s) e_flags=0x%lx\n",
7937c478bd9Sstevel@tonic-gate 			   e->e_id == NULL ? "[NOQUEUE]" : e->e_id,
7947c478bd9Sstevel@tonic-gate 			   e->e_flags);
7957c478bd9Sstevel@tonic-gate 	if (LogLevel > 80)
7967c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_DEBUG, e->e_id,
7977c478bd9Sstevel@tonic-gate 			  "sendenvelope, flags=0x%lx",
7987c478bd9Sstevel@tonic-gate 			  e->e_flags);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	/*
8017c478bd9Sstevel@tonic-gate 	**  If we have had global, fatal errors, don't bother sending
8027c478bd9Sstevel@tonic-gate 	**  the message at all if we are in SMTP mode.  Local errors
8037c478bd9Sstevel@tonic-gate 	**  (e.g., a single address failing) will still cause the other
8047c478bd9Sstevel@tonic-gate 	**  addresses to be sent.
8057c478bd9Sstevel@tonic-gate 	*/
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	if (bitset(EF_FATALERRS, e->e_flags) &&
8087c478bd9Sstevel@tonic-gate 	    (OpMode == MD_SMTP || OpMode == MD_DAEMON))
8097c478bd9Sstevel@tonic-gate 	{
8107c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_CLRQUEUE;
8117c478bd9Sstevel@tonic-gate 		return;
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
8157c478bd9Sstevel@tonic-gate 	**  Don't attempt deliveries if we want to bounce now
8167c478bd9Sstevel@tonic-gate 	**  or if deliver-by time is exceeded.
8177c478bd9Sstevel@tonic-gate 	*/
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if (!bitset(EF_RESPONSE, e->e_flags) &&
8207c478bd9Sstevel@tonic-gate 	    (TimeOuts.to_q_return[e->e_timeoutclass] == NOW ||
8217c478bd9Sstevel@tonic-gate 	     (IS_DLVR_RETURN(e) && e->e_deliver_by > 0 &&
8227c478bd9Sstevel@tonic-gate 	      curtime() > e->e_ctime + e->e_deliver_by)))
8237c478bd9Sstevel@tonic-gate 		return;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/*
8267c478bd9Sstevel@tonic-gate 	**  Run through the list and send everything.
8277c478bd9Sstevel@tonic-gate 	**
8287c478bd9Sstevel@tonic-gate 	**	Set EF_GLOBALERRS so that error messages during delivery
8297c478bd9Sstevel@tonic-gate 	**	result in returned mail.
8307c478bd9Sstevel@tonic-gate 	*/
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	e->e_nsent = 0;
8337c478bd9Sstevel@tonic-gate 	e->e_flags |= EF_GLOBALERRS;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	macdefine(&e->e_macro, A_PERM, macid("{envid}"), e->e_envid);
8367c478bd9Sstevel@tonic-gate 	macdefine(&e->e_macro, A_PERM, macid("{bodytype}"), e->e_bodytype);
8377c478bd9Sstevel@tonic-gate 	didany = false;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	if (!bitset(EF_SPLIT, e->e_flags))
8407c478bd9Sstevel@tonic-gate 	{
8417c478bd9Sstevel@tonic-gate 		ENVELOPE *oldsib;
8427c478bd9Sstevel@tonic-gate 		ENVELOPE *ee;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 		/*
8457c478bd9Sstevel@tonic-gate 		**  Save old sibling and set it to NULL to avoid
8467c478bd9Sstevel@tonic-gate 		**  queueing up the same envelopes again.
8477c478bd9Sstevel@tonic-gate 		**  This requires that envelopes in that list have
8487c478bd9Sstevel@tonic-gate 		**  been take care of before (or at some other place).
8497c478bd9Sstevel@tonic-gate 		*/
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 		oldsib = e->e_sibling;
8527c478bd9Sstevel@tonic-gate 		e->e_sibling = NULL;
8537c478bd9Sstevel@tonic-gate 		if (!split_by_recipient(e) &&
8547c478bd9Sstevel@tonic-gate 		    bitset(EF_FATALERRS, e->e_flags))
8557c478bd9Sstevel@tonic-gate 		{
8567c478bd9Sstevel@tonic-gate 			if (OpMode == MD_SMTP || OpMode == MD_DAEMON)
8577c478bd9Sstevel@tonic-gate 				e->e_flags |= EF_CLRQUEUE;
8587c478bd9Sstevel@tonic-gate 			return;
8597c478bd9Sstevel@tonic-gate 		}
8607c478bd9Sstevel@tonic-gate 		for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
8617c478bd9Sstevel@tonic-gate 			queueup(ee, false, true);
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		/* clean up */
8647c478bd9Sstevel@tonic-gate 		for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling)
8657c478bd9Sstevel@tonic-gate 		{
8667c478bd9Sstevel@tonic-gate 			/* now unlock the job */
8677c478bd9Sstevel@tonic-gate 			closexscript(ee);
8687c478bd9Sstevel@tonic-gate 			unlockqueue(ee);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 			/* this envelope is marked unused */
8717c478bd9Sstevel@tonic-gate 			if (ee->e_dfp != NULL)
8727c478bd9Sstevel@tonic-gate 			{
8737c478bd9Sstevel@tonic-gate 				(void) sm_io_close(ee->e_dfp, SM_TIME_DEFAULT);
8747c478bd9Sstevel@tonic-gate 				ee->e_dfp = NULL;
8757c478bd9Sstevel@tonic-gate 			}
8767c478bd9Sstevel@tonic-gate 			ee->e_id = NULL;
8777c478bd9Sstevel@tonic-gate 			ee->e_flags &= ~EF_HAS_DF;
8787c478bd9Sstevel@tonic-gate 		}
8797c478bd9Sstevel@tonic-gate 		e->e_sibling = oldsib;
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/* now run through the queue */
8837c478bd9Sstevel@tonic-gate 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
8847c478bd9Sstevel@tonic-gate 	{
8857c478bd9Sstevel@tonic-gate #if XDEBUG
8867c478bd9Sstevel@tonic-gate 		char wbuf[MAXNAME + 20];
8877c478bd9Sstevel@tonic-gate 
888058561cbSjbeck 		(void) sm_snprintf(wbuf, sizeof(wbuf), "sendall(%.*s)",
8897c478bd9Sstevel@tonic-gate 				   MAXNAME, q->q_paddr);
8907c478bd9Sstevel@tonic-gate 		checkfd012(wbuf);
8917c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
8927c478bd9Sstevel@tonic-gate 		if (mode == SM_VERIFY)
8937c478bd9Sstevel@tonic-gate 		{
8947c478bd9Sstevel@tonic-gate 			e->e_to = q->q_paddr;
8957c478bd9Sstevel@tonic-gate 			if (QS_IS_SENDABLE(q->q_state))
8967c478bd9Sstevel@tonic-gate 			{
8977c478bd9Sstevel@tonic-gate 				if (q->q_host != NULL && q->q_host[0] != '\0')
8987c478bd9Sstevel@tonic-gate 					message("deliverable: mailer %s, host %s, user %s",
8997c478bd9Sstevel@tonic-gate 						q->q_mailer->m_name,
9007c478bd9Sstevel@tonic-gate 						q->q_host,
9017c478bd9Sstevel@tonic-gate 						q->q_user);
9027c478bd9Sstevel@tonic-gate 				else
9037c478bd9Sstevel@tonic-gate 					message("deliverable: mailer %s, user %s",
9047c478bd9Sstevel@tonic-gate 						q->q_mailer->m_name,
9057c478bd9Sstevel@tonic-gate 						q->q_user);
9067c478bd9Sstevel@tonic-gate 			}
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 		else if (QS_IS_OK(q->q_state))
9097c478bd9Sstevel@tonic-gate 		{
9107c478bd9Sstevel@tonic-gate 			/*
9117c478bd9Sstevel@tonic-gate 			**  Checkpoint the send list every few addresses
9127c478bd9Sstevel@tonic-gate 			*/
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 			if (CheckpointInterval > 0 &&
9157c478bd9Sstevel@tonic-gate 			    e->e_nsent >= CheckpointInterval)
9167c478bd9Sstevel@tonic-gate 			{
9177c478bd9Sstevel@tonic-gate 				queueup(e, false, false);
9187c478bd9Sstevel@tonic-gate 				e->e_nsent = 0;
9197c478bd9Sstevel@tonic-gate 			}
9207c478bd9Sstevel@tonic-gate 			(void) deliver(e, q);
9217c478bd9Sstevel@tonic-gate 			didany = true;
9227c478bd9Sstevel@tonic-gate 		}
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 	if (didany)
9257c478bd9Sstevel@tonic-gate 	{
9267c478bd9Sstevel@tonic-gate 		e->e_dtime = curtime();
9277c478bd9Sstevel@tonic-gate 		e->e_ntries++;
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate #if XDEBUG
9317c478bd9Sstevel@tonic-gate 	checkfd012("end of sendenvelope");
9327c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate #if REQUIRES_DIR_FSYNC
9367c478bd9Sstevel@tonic-gate /*
9377c478bd9Sstevel@tonic-gate **  SYNC_DIR -- fsync a directory based on a filename
9387c478bd9Sstevel@tonic-gate **
9397c478bd9Sstevel@tonic-gate **	Parameters:
9407c478bd9Sstevel@tonic-gate **		filename -- path of file
9417c478bd9Sstevel@tonic-gate **		panic -- panic?
9427c478bd9Sstevel@tonic-gate **
9437c478bd9Sstevel@tonic-gate **	Returns:
9447c478bd9Sstevel@tonic-gate **		none
9457c478bd9Sstevel@tonic-gate */
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate void
9487c478bd9Sstevel@tonic-gate sync_dir(filename, panic)
9497c478bd9Sstevel@tonic-gate 	char *filename;
9507c478bd9Sstevel@tonic-gate 	bool panic;
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	int dirfd;
9537c478bd9Sstevel@tonic-gate 	char *dirp;
9547c478bd9Sstevel@tonic-gate 	char dir[MAXPATHLEN];
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	if (!RequiresDirfsync)
9577c478bd9Sstevel@tonic-gate 		return;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	/* filesystems which require the directory be synced */
9607c478bd9Sstevel@tonic-gate 	dirp = strrchr(filename, '/');
9617c478bd9Sstevel@tonic-gate 	if (dirp != NULL)
9627c478bd9Sstevel@tonic-gate 	{
963058561cbSjbeck 		if (sm_strlcpy(dir, filename, sizeof(dir)) >= sizeof(dir))
9647c478bd9Sstevel@tonic-gate 			return;
9657c478bd9Sstevel@tonic-gate 		dir[dirp - filename] = '\0';
9667c478bd9Sstevel@tonic-gate 		dirp = dir;
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 	else
9697c478bd9Sstevel@tonic-gate 		dirp = ".";
9707c478bd9Sstevel@tonic-gate 	dirfd = open(dirp, O_RDONLY, 0700);
9717c478bd9Sstevel@tonic-gate 	if (tTd(40,32))
9727c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_INFO, NOQID, "sync_dir: %s: fsync(%d)",
9737c478bd9Sstevel@tonic-gate 			  dirp, dirfd);
9747c478bd9Sstevel@tonic-gate 	if (dirfd >= 0)
9757c478bd9Sstevel@tonic-gate 	{
9767c478bd9Sstevel@tonic-gate 		if (fsync(dirfd) < 0)
9777c478bd9Sstevel@tonic-gate 		{
9787c478bd9Sstevel@tonic-gate 			if (panic)
9797c478bd9Sstevel@tonic-gate 				syserr("!sync_dir: cannot fsync directory %s",
9807c478bd9Sstevel@tonic-gate 				       dirp);
9817c478bd9Sstevel@tonic-gate 			else if (LogLevel > 1)
9827c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_ERR, NOQID,
9837c478bd9Sstevel@tonic-gate 					  "sync_dir: cannot fsync directory %s: %s",
9847c478bd9Sstevel@tonic-gate 					  dirp, sm_errstring(errno));
9857c478bd9Sstevel@tonic-gate 		}
9867c478bd9Sstevel@tonic-gate 		(void) close(dirfd);
9877c478bd9Sstevel@tonic-gate 	}
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate #endif /* REQUIRES_DIR_FSYNC */
9907c478bd9Sstevel@tonic-gate /*
9917c478bd9Sstevel@tonic-gate **  DUP_QUEUE_FILE -- duplicate a queue file into a split queue
9927c478bd9Sstevel@tonic-gate **
9937c478bd9Sstevel@tonic-gate **	Parameters:
9947c478bd9Sstevel@tonic-gate **		e -- the existing envelope
9957c478bd9Sstevel@tonic-gate **		ee -- the new envelope
9967c478bd9Sstevel@tonic-gate **		type -- the queue file type (e.g., DATAFL_LETTER)
9977c478bd9Sstevel@tonic-gate **
9987c478bd9Sstevel@tonic-gate **	Returns:
9997c478bd9Sstevel@tonic-gate **		none
10007c478bd9Sstevel@tonic-gate */
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate static void
10037c478bd9Sstevel@tonic-gate dup_queue_file(e, ee, type)
10047c478bd9Sstevel@tonic-gate 	ENVELOPE *e, *ee;
10057c478bd9Sstevel@tonic-gate 	int type;
10067c478bd9Sstevel@tonic-gate {
10077c478bd9Sstevel@tonic-gate 	char f1buf[MAXPATHLEN], f2buf[MAXPATHLEN];
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	ee->e_dfp = NULL;
10107c478bd9Sstevel@tonic-gate 	ee->e_xfp = NULL;
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	/*
10137c478bd9Sstevel@tonic-gate 	**  Make sure both are in the same directory.
10147c478bd9Sstevel@tonic-gate 	*/
10157c478bd9Sstevel@tonic-gate 
1016058561cbSjbeck 	(void) sm_strlcpy(f1buf, queuename(e, type), sizeof(f1buf));
1017058561cbSjbeck 	(void) sm_strlcpy(f2buf, queuename(ee, type), sizeof(f2buf));
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	/* Force the df to disk if it's not there yet */
10207c478bd9Sstevel@tonic-gate 	if (type == DATAFL_LETTER && e->e_dfp != NULL &&
10217c478bd9Sstevel@tonic-gate 	    sm_io_setinfo(e->e_dfp, SM_BF_COMMIT, NULL) < 0 &&
10227c478bd9Sstevel@tonic-gate 	    errno != EINVAL)
10237c478bd9Sstevel@tonic-gate 	{
10247c478bd9Sstevel@tonic-gate 		syserr("!dup_queue_file: can't commit %s", f1buf);
10257c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	if (link(f1buf, f2buf) < 0)
10297c478bd9Sstevel@tonic-gate 	{
10307c478bd9Sstevel@tonic-gate 		int save_errno = errno;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 		syserr("sendall: link(%s, %s)", f1buf, f2buf);
10337c478bd9Sstevel@tonic-gate 		if (save_errno == EEXIST)
10347c478bd9Sstevel@tonic-gate 		{
10357c478bd9Sstevel@tonic-gate 			if (unlink(f2buf) < 0)
10367c478bd9Sstevel@tonic-gate 			{
10377c478bd9Sstevel@tonic-gate 				syserr("!sendall: unlink(%s): permanent",
10387c478bd9Sstevel@tonic-gate 				       f2buf);
10397c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
10407c478bd9Sstevel@tonic-gate 			}
10417c478bd9Sstevel@tonic-gate 			if (link(f1buf, f2buf) < 0)
10427c478bd9Sstevel@tonic-gate 			{
10437c478bd9Sstevel@tonic-gate 				syserr("!sendall: link(%s, %s): permanent",
10447c478bd9Sstevel@tonic-gate 				       f1buf, f2buf);
10457c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
10467c478bd9Sstevel@tonic-gate 			}
10477c478bd9Sstevel@tonic-gate 		}
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 	SYNC_DIR(f2buf, true);
10507c478bd9Sstevel@tonic-gate }
10517c478bd9Sstevel@tonic-gate /*
10527c478bd9Sstevel@tonic-gate **  DOFORK -- do a fork, retrying a couple of times on failure.
10537c478bd9Sstevel@tonic-gate **
10547c478bd9Sstevel@tonic-gate **	This MUST be a macro, since after a vfork we are running
10557c478bd9Sstevel@tonic-gate **	two processes on the same stack!!!
10567c478bd9Sstevel@tonic-gate **
10577c478bd9Sstevel@tonic-gate **	Parameters:
10587c478bd9Sstevel@tonic-gate **		none.
10597c478bd9Sstevel@tonic-gate **
10607c478bd9Sstevel@tonic-gate **	Returns:
10617c478bd9Sstevel@tonic-gate **		From a macro???  You've got to be kidding!
10627c478bd9Sstevel@tonic-gate **
10637c478bd9Sstevel@tonic-gate **	Side Effects:
10647c478bd9Sstevel@tonic-gate **		Modifies the ==> LOCAL <== variable 'pid', leaving:
10657c478bd9Sstevel@tonic-gate **			pid of child in parent, zero in child.
10667c478bd9Sstevel@tonic-gate **			-1 on unrecoverable error.
10677c478bd9Sstevel@tonic-gate **
10687c478bd9Sstevel@tonic-gate **	Notes:
10697c478bd9Sstevel@tonic-gate **		I'm awfully sorry this looks so awful.  That's
10707c478bd9Sstevel@tonic-gate **		vfork for you.....
10717c478bd9Sstevel@tonic-gate */
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate #define NFORKTRIES	5
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate #ifndef FORK
10767c478bd9Sstevel@tonic-gate # define FORK	fork
10777c478bd9Sstevel@tonic-gate #endif /* ! FORK */
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate #define DOFORK(fORKfN) \
10807c478bd9Sstevel@tonic-gate {\
10817c478bd9Sstevel@tonic-gate 	register int i;\
10827c478bd9Sstevel@tonic-gate \
10837c478bd9Sstevel@tonic-gate 	for (i = NFORKTRIES; --i >= 0; )\
10847c478bd9Sstevel@tonic-gate 	{\
10857c478bd9Sstevel@tonic-gate 		pid = fORKfN();\
10867c478bd9Sstevel@tonic-gate 		if (pid >= 0)\
10877c478bd9Sstevel@tonic-gate 			break;\
10887c478bd9Sstevel@tonic-gate 		if (i > 0)\
10897c478bd9Sstevel@tonic-gate 			(void) sleep((unsigned) NFORKTRIES - i);\
10907c478bd9Sstevel@tonic-gate 	}\
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate /*
10937c478bd9Sstevel@tonic-gate **  DOFORK -- simple fork interface to DOFORK.
10947c478bd9Sstevel@tonic-gate **
10957c478bd9Sstevel@tonic-gate **	Parameters:
10967c478bd9Sstevel@tonic-gate **		none.
10977c478bd9Sstevel@tonic-gate **
10987c478bd9Sstevel@tonic-gate **	Returns:
10997c478bd9Sstevel@tonic-gate **		pid of child in parent.
11007c478bd9Sstevel@tonic-gate **		zero in child.
11017c478bd9Sstevel@tonic-gate **		-1 on error.
11027c478bd9Sstevel@tonic-gate **
11037c478bd9Sstevel@tonic-gate **	Side Effects:
11047c478bd9Sstevel@tonic-gate **		returns twice, once in parent and once in child.
11057c478bd9Sstevel@tonic-gate */
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate pid_t
11087c478bd9Sstevel@tonic-gate dofork()
11097c478bd9Sstevel@tonic-gate {
11107c478bd9Sstevel@tonic-gate 	register pid_t pid = -1;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	DOFORK(fork);
11137c478bd9Sstevel@tonic-gate 	return pid;
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate /*
11177c478bd9Sstevel@tonic-gate **  COLONCMP -- compare host-signatures up to first ':' or EOS
11187c478bd9Sstevel@tonic-gate **
11197c478bd9Sstevel@tonic-gate **	This takes two strings which happen to be host-signatures and
11207c478bd9Sstevel@tonic-gate **	compares them. If the lowest preference portions of the MX-RR's
11217c478bd9Sstevel@tonic-gate **	match (up to ':' or EOS, whichever is first), then we have
11227c478bd9Sstevel@tonic-gate **	match. This is used for coattail-piggybacking messages during
11237c478bd9Sstevel@tonic-gate **	message delivery.
11247c478bd9Sstevel@tonic-gate **	If the signatures are the same up to the first ':' the remainder of
11257c478bd9Sstevel@tonic-gate **	the signatures are then compared with a normal strcmp(). This saves
11267c478bd9Sstevel@tonic-gate **	re-examining the first part of the signatures.
11277c478bd9Sstevel@tonic-gate **
11287c478bd9Sstevel@tonic-gate **	Parameters:
11297c478bd9Sstevel@tonic-gate **		a - first host-signature
11307c478bd9Sstevel@tonic-gate **		b - second host-signature
11317c478bd9Sstevel@tonic-gate **
11327c478bd9Sstevel@tonic-gate **	Returns:
11337c478bd9Sstevel@tonic-gate **		HS_MATCH_NO -- no "match".
11347c478bd9Sstevel@tonic-gate **		HS_MATCH_FIRST -- "match" for the first MX preference
11357c478bd9Sstevel@tonic-gate **			(up to the first colon (':')).
11367c478bd9Sstevel@tonic-gate **		HS_MATCH_FULL -- match for the entire MX record.
11377c478bd9Sstevel@tonic-gate **
11387c478bd9Sstevel@tonic-gate **	Side Effects:
11397c478bd9Sstevel@tonic-gate **		none.
11407c478bd9Sstevel@tonic-gate */
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate #define HS_MATCH_NO	0
11437c478bd9Sstevel@tonic-gate #define HS_MATCH_FIRST	1
11447c478bd9Sstevel@tonic-gate #define HS_MATCH_FULL	2
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate static int
11477c478bd9Sstevel@tonic-gate coloncmp(a, b)
11487c478bd9Sstevel@tonic-gate 	register const char *a;
11497c478bd9Sstevel@tonic-gate 	register const char *b;
11507c478bd9Sstevel@tonic-gate {
11517c478bd9Sstevel@tonic-gate 	int ret = HS_MATCH_NO;
11527c478bd9Sstevel@tonic-gate 	int braclev = 0;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	while (*a == *b++)
11557c478bd9Sstevel@tonic-gate 	{
11567c478bd9Sstevel@tonic-gate 		/* Need to account for IPv6 bracketed addresses */
11577c478bd9Sstevel@tonic-gate 		if (*a == '[')
11587c478bd9Sstevel@tonic-gate 			braclev++;
11597c478bd9Sstevel@tonic-gate 		else if (*a == ']' && braclev > 0)
11607c478bd9Sstevel@tonic-gate 			braclev--;
11617c478bd9Sstevel@tonic-gate 		else if (*a == ':' && braclev <= 0)
11627c478bd9Sstevel@tonic-gate 		{
11637c478bd9Sstevel@tonic-gate 			ret = HS_MATCH_FIRST;
11647c478bd9Sstevel@tonic-gate 			a++;
11657c478bd9Sstevel@tonic-gate 			break;
11667c478bd9Sstevel@tonic-gate 		}
11677c478bd9Sstevel@tonic-gate 		else if (*a == '\0')
11687c478bd9Sstevel@tonic-gate 			return HS_MATCH_FULL; /* a full match */
11697c478bd9Sstevel@tonic-gate 		a++;
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 	if (ret == HS_MATCH_NO &&
11727c478bd9Sstevel@tonic-gate 	    braclev <= 0 &&
11737c478bd9Sstevel@tonic-gate 	    ((*a == '\0' && *(b - 1) == ':') ||
11747c478bd9Sstevel@tonic-gate 	     (*a == ':' && *(b - 1) == '\0')))
11757c478bd9Sstevel@tonic-gate 		return HS_MATCH_FIRST;
11767c478bd9Sstevel@tonic-gate 	if (ret == HS_MATCH_FIRST && strcmp(a, b) == 0)
11777c478bd9Sstevel@tonic-gate 		return HS_MATCH_FULL;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	return ret;
11807c478bd9Sstevel@tonic-gate }
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate /*
11837c478bd9Sstevel@tonic-gate **  SHOULD_TRY_FBSH -- Should try FallbackSmartHost?
11847c478bd9Sstevel@tonic-gate **
11857c478bd9Sstevel@tonic-gate **	Parameters:
11867c478bd9Sstevel@tonic-gate **		e -- envelope
11877c478bd9Sstevel@tonic-gate **		tried_fallbacksmarthost -- has been tried already? (in/out)
11887c478bd9Sstevel@tonic-gate **		hostbuf -- buffer for hostname (expand FallbackSmartHost) (out)
11897c478bd9Sstevel@tonic-gate **		hbsz -- size of hostbuf
11907c478bd9Sstevel@tonic-gate **		status -- current delivery status
11917c478bd9Sstevel@tonic-gate **
11927c478bd9Sstevel@tonic-gate **	Returns:
11937c478bd9Sstevel@tonic-gate **		true iff FallbackSmartHost should be tried.
11947c478bd9Sstevel@tonic-gate */
11957c478bd9Sstevel@tonic-gate 
1196058561cbSjbeck static bool should_try_fbsh __P((ENVELOPE *, bool *, char *, size_t, int));
1197058561cbSjbeck 
11987c478bd9Sstevel@tonic-gate static bool
11997c478bd9Sstevel@tonic-gate should_try_fbsh(e, tried_fallbacksmarthost, hostbuf, hbsz, status)
12007c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
12017c478bd9Sstevel@tonic-gate 	bool *tried_fallbacksmarthost;
12027c478bd9Sstevel@tonic-gate 	char *hostbuf;
12037c478bd9Sstevel@tonic-gate 	size_t hbsz;
12047c478bd9Sstevel@tonic-gate 	int status;
12057c478bd9Sstevel@tonic-gate {
12067c478bd9Sstevel@tonic-gate 	/*
120749218d4fSjbeck 	**  If the host was not found or a temporary failure occurred
120849218d4fSjbeck 	**  and a FallbackSmartHost is defined (and we have not yet
120949218d4fSjbeck 	**  tried it), then make one last try with it as the host.
12107c478bd9Sstevel@tonic-gate 	*/
12117c478bd9Sstevel@tonic-gate 
121249218d4fSjbeck 	if ((status == EX_NOHOST || status == EX_TEMPFAIL) &&
121349218d4fSjbeck 	    FallbackSmartHost != NULL && !*tried_fallbacksmarthost)
12147c478bd9Sstevel@tonic-gate 	{
12157c478bd9Sstevel@tonic-gate 		*tried_fallbacksmarthost = true;
12167c478bd9Sstevel@tonic-gate 		expand(FallbackSmartHost, hostbuf, hbsz, e);
12177c478bd9Sstevel@tonic-gate 		if (!wordinclass(hostbuf, 'w'))
12187c478bd9Sstevel@tonic-gate 		{
12197c478bd9Sstevel@tonic-gate 			if (tTd(11, 1))
12207c478bd9Sstevel@tonic-gate 				sm_dprintf("one last try with FallbackSmartHost %s\n",
12217c478bd9Sstevel@tonic-gate 					   hostbuf);
12227c478bd9Sstevel@tonic-gate 			return true;
12237c478bd9Sstevel@tonic-gate 		}
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 	return false;
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate /*
12287c478bd9Sstevel@tonic-gate **  DELIVER -- Deliver a message to a list of addresses.
12297c478bd9Sstevel@tonic-gate **
12307c478bd9Sstevel@tonic-gate **	This routine delivers to everyone on the same host as the
12317c478bd9Sstevel@tonic-gate **	user on the head of the list.  It is clever about mailers
12327c478bd9Sstevel@tonic-gate **	that don't handle multiple users.  It is NOT guaranteed
12337c478bd9Sstevel@tonic-gate **	that it will deliver to all these addresses however -- so
12347c478bd9Sstevel@tonic-gate **	deliver should be called once for each address on the
12357c478bd9Sstevel@tonic-gate **	list.
12367c478bd9Sstevel@tonic-gate **	Deliver tries to be as opportunistic as possible about piggybacking
12377c478bd9Sstevel@tonic-gate **	messages. Some definitions to make understanding easier follow below.
12387c478bd9Sstevel@tonic-gate **	Piggybacking occurs when an existing connection to a mail host can
12397c478bd9Sstevel@tonic-gate **	be used to send the same message to more than one recipient at the
12407c478bd9Sstevel@tonic-gate **	same time. So "no piggybacking" means one message for one recipient
12417c478bd9Sstevel@tonic-gate **	per connection. "Intentional piggybacking" happens when the
12427c478bd9Sstevel@tonic-gate **	recipients' host address (not the mail host address) is used to
12437c478bd9Sstevel@tonic-gate **	attempt piggybacking. Recipients with the same host address
12447c478bd9Sstevel@tonic-gate **	have the same mail host. "Coincidental piggybacking" relies on
12457c478bd9Sstevel@tonic-gate **	piggybacking based on all the mail host addresses in the MX-RR. This
12467c478bd9Sstevel@tonic-gate **	is "coincidental" in the fact it could not be predicted until the
12477c478bd9Sstevel@tonic-gate **	MX Resource Records for the hosts were obtained and examined. For
12487c478bd9Sstevel@tonic-gate **	example (preference order and equivalence is important, not values):
12497c478bd9Sstevel@tonic-gate **		domain1 IN MX 10 mxhost-A
12507c478bd9Sstevel@tonic-gate **			IN MX 20 mxhost-B
12517c478bd9Sstevel@tonic-gate **		domain2 IN MX  4 mxhost-A
12527c478bd9Sstevel@tonic-gate **			IN MX  8 mxhost-B
12537c478bd9Sstevel@tonic-gate **	Domain1 and domain2 can piggyback the same message to mxhost-A or
12547c478bd9Sstevel@tonic-gate **	mxhost-B (if mxhost-A cannot be reached).
12557c478bd9Sstevel@tonic-gate **	"Coattail piggybacking" relaxes the strictness of "coincidental
12567c478bd9Sstevel@tonic-gate **	piggybacking" in the hope that most significant (lowest value)
12577c478bd9Sstevel@tonic-gate **	MX preference host(s) can create more piggybacking. For example
12587c478bd9Sstevel@tonic-gate **	(again, preference order and equivalence is important, not values):
12597c478bd9Sstevel@tonic-gate **		domain3 IN MX 100 mxhost-C
12607c478bd9Sstevel@tonic-gate **			IN MX 100 mxhost-D
12617c478bd9Sstevel@tonic-gate **			IN MX 200 mxhost-E
12627c478bd9Sstevel@tonic-gate **		domain4 IN MX  50 mxhost-C
12637c478bd9Sstevel@tonic-gate **			IN MX  50 mxhost-D
12647c478bd9Sstevel@tonic-gate **			IN MX  80 mxhost-F
12657c478bd9Sstevel@tonic-gate **	A message for domain3 and domain4 can piggyback to mxhost-C if mxhost-C
12667c478bd9Sstevel@tonic-gate **	is available. Same with mxhost-D because in both RR's the preference
12677c478bd9Sstevel@tonic-gate **	value is the same as mxhost-C, respectively.
12687c478bd9Sstevel@tonic-gate **	So deliver attempts coattail piggybacking when possible. If the
12697c478bd9Sstevel@tonic-gate **	first MX preference level hosts cannot be used then the piggybacking
12707c478bd9Sstevel@tonic-gate **	reverts to coincidental piggybacking. Using the above example you
12717c478bd9Sstevel@tonic-gate **	cannot deliver to mxhost-F for domain3 regardless of preference value.
12727c478bd9Sstevel@tonic-gate **	("Coattail" from "riding on the coattails of your predecessor" meaning
12737c478bd9Sstevel@tonic-gate **	gaining benefit from a predecessor effort with no or little addition
12747c478bd9Sstevel@tonic-gate **	effort. The predecessor here being the preceding MX RR).
12757c478bd9Sstevel@tonic-gate **
12767c478bd9Sstevel@tonic-gate **	Parameters:
12777c478bd9Sstevel@tonic-gate **		e -- the envelope to deliver.
12787c478bd9Sstevel@tonic-gate **		firstto -- head of the address list to deliver to.
12797c478bd9Sstevel@tonic-gate **
12807c478bd9Sstevel@tonic-gate **	Returns:
12817c478bd9Sstevel@tonic-gate **		zero -- successfully delivered.
12827c478bd9Sstevel@tonic-gate **		else -- some failure, see ExitStat for more info.
12837c478bd9Sstevel@tonic-gate **
12847c478bd9Sstevel@tonic-gate **	Side Effects:
12857c478bd9Sstevel@tonic-gate **		The standard input is passed off to someone.
12867c478bd9Sstevel@tonic-gate */
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate static int
12897c478bd9Sstevel@tonic-gate deliver(e, firstto)
12907c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
12917c478bd9Sstevel@tonic-gate 	ADDRESS *firstto;
12927c478bd9Sstevel@tonic-gate {
12937c478bd9Sstevel@tonic-gate 	char *host;			/* host being sent to */
12947c478bd9Sstevel@tonic-gate 	char *user;			/* user being sent to */
12957c478bd9Sstevel@tonic-gate 	char **pvp;
12967c478bd9Sstevel@tonic-gate 	register char **mvp;
12977c478bd9Sstevel@tonic-gate 	register char *p;
12987c478bd9Sstevel@tonic-gate 	register MAILER *m;		/* mailer for this recipient */
12997c478bd9Sstevel@tonic-gate 	ADDRESS *volatile ctladdr;
13007c478bd9Sstevel@tonic-gate #if HASSETUSERCONTEXT
13017c478bd9Sstevel@tonic-gate 	ADDRESS *volatile contextaddr = NULL;
13027c478bd9Sstevel@tonic-gate #endif /* HASSETUSERCONTEXT */
13037c478bd9Sstevel@tonic-gate 	register MCI *volatile mci;
13047c478bd9Sstevel@tonic-gate 	register ADDRESS *SM_NONVOLATILE to = firstto;
13057c478bd9Sstevel@tonic-gate 	volatile bool clever = false;	/* running user smtp to this mailer */
13067c478bd9Sstevel@tonic-gate 	ADDRESS *volatile tochain = NULL; /* users chain in this mailer call */
13077c478bd9Sstevel@tonic-gate 	int rcode;			/* response code */
13087c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE int lmtp_rcode = EX_OK;
13097c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE int nummxhosts = 0; /* number of MX hosts available */
13107c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE int hostnum = 0;	/* current MX host index */
13117c478bd9Sstevel@tonic-gate 	char *firstsig;			/* signature of firstto */
13127c478bd9Sstevel@tonic-gate 	volatile pid_t pid = -1;
13137c478bd9Sstevel@tonic-gate 	char *volatile curhost;
13147c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE unsigned short port = 0;
13157c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE time_t enough = 0;
13167c478bd9Sstevel@tonic-gate #if NETUNIX
13177c478bd9Sstevel@tonic-gate 	char *SM_NONVOLATILE mux_path = NULL;	/* path to UNIX domain socket */
13187c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
13197c478bd9Sstevel@tonic-gate 	time_t xstart;
13207c478bd9Sstevel@tonic-gate 	bool suidwarn;
13217c478bd9Sstevel@tonic-gate 	bool anyok;			/* at least one address was OK */
13227c478bd9Sstevel@tonic-gate 	SM_NONVOLATILE bool goodmxfound = false; /* at least one MX was OK */
13237c478bd9Sstevel@tonic-gate 	bool ovr;
13247c478bd9Sstevel@tonic-gate 	bool quarantine;
13257c478bd9Sstevel@tonic-gate 	int strsize;
13267c478bd9Sstevel@tonic-gate 	int rcptcount;
13277c478bd9Sstevel@tonic-gate 	int ret;
13287c478bd9Sstevel@tonic-gate 	static int tobufsize = 0;
13297c478bd9Sstevel@tonic-gate 	static char *tobuf = NULL;
13307c478bd9Sstevel@tonic-gate 	char *rpath;	/* translated return path */
13317c478bd9Sstevel@tonic-gate 	int mpvect[2];
13327c478bd9Sstevel@tonic-gate 	int rpvect[2];
13337c478bd9Sstevel@tonic-gate 	char *mxhosts[MAXMXHOSTS + 1];
13347c478bd9Sstevel@tonic-gate 	char *pv[MAXPV + 1];
13357c478bd9Sstevel@tonic-gate 	char buf[MAXNAME + 1];
13367c478bd9Sstevel@tonic-gate 	char cbuf[MAXPATHLEN];
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	errno = 0;
13394aac33d3Sjbeck 	SM_REQUIRE(firstto != NULL);	/* same as to */
13407c478bd9Sstevel@tonic-gate 	if (!QS_IS_OK(to->q_state))
13417c478bd9Sstevel@tonic-gate 		return 0;
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	suidwarn = geteuid() == 0;
13447c478bd9Sstevel@tonic-gate 
13454aac33d3Sjbeck 	SM_REQUIRE(e != NULL);
13467c478bd9Sstevel@tonic-gate 	m = to->q_mailer;
13477c478bd9Sstevel@tonic-gate 	host = to->q_host;
13487c478bd9Sstevel@tonic-gate 	CurEnv = e;			/* just in case */
13497c478bd9Sstevel@tonic-gate 	e->e_statmsg = NULL;
13507c478bd9Sstevel@tonic-gate 	SmtpError[0] = '\0';
13517c478bd9Sstevel@tonic-gate 	xstart = curtime();
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	if (tTd(10, 1))
13547c478bd9Sstevel@tonic-gate 		sm_dprintf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n",
13557c478bd9Sstevel@tonic-gate 			e->e_id, m->m_name, host, to->q_user);
13567c478bd9Sstevel@tonic-gate 	if (tTd(10, 100))
13577c478bd9Sstevel@tonic-gate 		printopenfds(false);
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	/*
13607c478bd9Sstevel@tonic-gate 	**  Clear {client_*} macros if this is a bounce message to
13617c478bd9Sstevel@tonic-gate 	**  prevent rejection by check_compat ruleset.
13627c478bd9Sstevel@tonic-gate 	*/
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	if (bitset(EF_RESPONSE, e->e_flags))
13657c478bd9Sstevel@tonic-gate 	{
13667c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, macid("{client_name}"), "");
13677c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, macid("{client_ptr}"), "");
13687c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, macid("{client_addr}"), "");
13697c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, macid("{client_port}"), "");
13707c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, macid("{client_resolve}"), "");
13717c478bd9Sstevel@tonic-gate 	}
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	SM_TRY
13747c478bd9Sstevel@tonic-gate 	{
13757c478bd9Sstevel@tonic-gate 	ADDRESS *skip_back = NULL;
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	/*
13787c478bd9Sstevel@tonic-gate 	**  Do initial argv setup.
13797c478bd9Sstevel@tonic-gate 	**	Insert the mailer name.  Notice that $x expansion is
13807c478bd9Sstevel@tonic-gate 	**	NOT done on the mailer name.  Then, if the mailer has
13817c478bd9Sstevel@tonic-gate 	**	a picky -f flag, we insert it as appropriate.  This
13827c478bd9Sstevel@tonic-gate 	**	code does not check for 'pv' overflow; this places a
13837c478bd9Sstevel@tonic-gate 	**	manifest lower limit of 4 for MAXPV.
13847c478bd9Sstevel@tonic-gate 	**		The from address rewrite is expected to make
13857c478bd9Sstevel@tonic-gate 	**		the address relative to the other end.
13867c478bd9Sstevel@tonic-gate 	*/
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	/* rewrite from address, using rewriting rules */
13897c478bd9Sstevel@tonic-gate 	rcode = EX_OK;
13904aac33d3Sjbeck 	SM_ASSERT(e->e_from.q_mailer != NULL);
13917c478bd9Sstevel@tonic-gate 	if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
13927c478bd9Sstevel@tonic-gate 		p = e->e_sender;
13937c478bd9Sstevel@tonic-gate 	else
13947c478bd9Sstevel@tonic-gate 		p = e->e_from.q_paddr;
13957c478bd9Sstevel@tonic-gate 	rpath = remotename(p, m, RF_SENDERADDR|RF_CANONICAL, &rcode, e);
13967c478bd9Sstevel@tonic-gate 	if (strlen(rpath) > MAXSHORTSTR)
13977c478bd9Sstevel@tonic-gate 	{
13987c478bd9Sstevel@tonic-gate 		rpath = shortenstring(rpath, MAXSHORTSTR);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 		/* avoid bogus errno */
14017c478bd9Sstevel@tonic-gate 		errno = 0;
14027c478bd9Sstevel@tonic-gate 		syserr("remotename: huge return path %s", rpath);
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 	rpath = sm_rpool_strdup_x(e->e_rpool, rpath);
14057c478bd9Sstevel@tonic-gate 	macdefine(&e->e_macro, A_PERM, 'g', rpath);
14067c478bd9Sstevel@tonic-gate 	macdefine(&e->e_macro, A_PERM, 'h', host);
14077c478bd9Sstevel@tonic-gate 	Errors = 0;
14087c478bd9Sstevel@tonic-gate 	pvp = pv;
14097c478bd9Sstevel@tonic-gate 	*pvp++ = m->m_argv[0];
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	/* ignore long term host status information if mailer flag W is set */
14127c478bd9Sstevel@tonic-gate 	if (bitnset(M_NOHOSTSTAT, m->m_flags))
14137c478bd9Sstevel@tonic-gate 		IgnoreHostStatus = true;
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	/* insert -f or -r flag as appropriate */
14167c478bd9Sstevel@tonic-gate 	if (FromFlag &&
14177c478bd9Sstevel@tonic-gate 	    (bitnset(M_FOPT, m->m_flags) ||
14187c478bd9Sstevel@tonic-gate 	     bitnset(M_ROPT, m->m_flags)))
14197c478bd9Sstevel@tonic-gate 	{
14207c478bd9Sstevel@tonic-gate 		if (bitnset(M_FOPT, m->m_flags))
14217c478bd9Sstevel@tonic-gate 			*pvp++ = "-f";
14227c478bd9Sstevel@tonic-gate 		else
14237c478bd9Sstevel@tonic-gate 			*pvp++ = "-r";
14247c478bd9Sstevel@tonic-gate 		*pvp++ = rpath;
14257c478bd9Sstevel@tonic-gate 	}
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	/*
14287c478bd9Sstevel@tonic-gate 	**  Append the other fixed parts of the argv.  These run
14297c478bd9Sstevel@tonic-gate 	**  up to the first entry containing "$u".  There can only
14307c478bd9Sstevel@tonic-gate 	**  be one of these, and there are only a few more slots
14317c478bd9Sstevel@tonic-gate 	**  in the pv after it.
14327c478bd9Sstevel@tonic-gate 	*/
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
14357c478bd9Sstevel@tonic-gate 	{
14367c478bd9Sstevel@tonic-gate 		/* can't use strchr here because of sign extension problems */
14377c478bd9Sstevel@tonic-gate 		while (*p != '\0')
14387c478bd9Sstevel@tonic-gate 		{
14397c478bd9Sstevel@tonic-gate 			if ((*p++ & 0377) == MACROEXPAND)
14407c478bd9Sstevel@tonic-gate 			{
14417c478bd9Sstevel@tonic-gate 				if (*p == 'u')
14427c478bd9Sstevel@tonic-gate 					break;
14437c478bd9Sstevel@tonic-gate 			}
14447c478bd9Sstevel@tonic-gate 		}
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 		if (*p != '\0')
14477c478bd9Sstevel@tonic-gate 			break;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 		/* this entry is safe -- go ahead and process it */
1450058561cbSjbeck 		expand(*mvp, buf, sizeof(buf), e);
14517c478bd9Sstevel@tonic-gate 		*pvp++ = sm_rpool_strdup_x(e->e_rpool, buf);
14527c478bd9Sstevel@tonic-gate 		if (pvp >= &pv[MAXPV - 3])
14537c478bd9Sstevel@tonic-gate 		{
14547c478bd9Sstevel@tonic-gate 			syserr("554 5.3.5 Too many parameters to %s before $u",
14557c478bd9Sstevel@tonic-gate 			       pv[0]);
14567c478bd9Sstevel@tonic-gate 			rcode = -1;
14577c478bd9Sstevel@tonic-gate 			goto cleanup;
14587c478bd9Sstevel@tonic-gate 		}
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	/*
14627c478bd9Sstevel@tonic-gate 	**  If we have no substitution for the user name in the argument
14637c478bd9Sstevel@tonic-gate 	**  list, we know that we must supply the names otherwise -- and
14647c478bd9Sstevel@tonic-gate 	**  SMTP is the answer!!
14657c478bd9Sstevel@tonic-gate 	*/
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	if (*mvp == NULL)
14687c478bd9Sstevel@tonic-gate 	{
14697c478bd9Sstevel@tonic-gate 		/* running LMTP or SMTP */
14707c478bd9Sstevel@tonic-gate 		clever = true;
14717c478bd9Sstevel@tonic-gate 		*pvp = NULL;
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 	else if (bitnset(M_LMTP, m->m_flags))
14747c478bd9Sstevel@tonic-gate 	{
14757c478bd9Sstevel@tonic-gate 		/* not running LMTP */
14767c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_ERR, NULL,
14777c478bd9Sstevel@tonic-gate 			  "Warning: mailer %s: LMTP flag (F=z) turned off",
14787c478bd9Sstevel@tonic-gate 			  m->m_name);
14797c478bd9Sstevel@tonic-gate 		clrbitn(M_LMTP, m->m_flags);
14807c478bd9Sstevel@tonic-gate 	}
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	/*
14837c478bd9Sstevel@tonic-gate 	**  At this point *mvp points to the argument with $u.  We
14847c478bd9Sstevel@tonic-gate 	**  run through our address list and append all the addresses
14857c478bd9Sstevel@tonic-gate 	**  we can.  If we run out of space, do not fret!  We can
14867c478bd9Sstevel@tonic-gate 	**  always send another copy later.
14877c478bd9Sstevel@tonic-gate 	*/
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	e->e_to = NULL;
14907c478bd9Sstevel@tonic-gate 	strsize = 2;
14917c478bd9Sstevel@tonic-gate 	rcptcount = 0;
14927c478bd9Sstevel@tonic-gate 	ctladdr = NULL;
14937c478bd9Sstevel@tonic-gate 	if (firstto->q_signature == NULL)
14947c478bd9Sstevel@tonic-gate 		firstto->q_signature = hostsignature(firstto->q_mailer,
14957c478bd9Sstevel@tonic-gate 						     firstto->q_host);
14967c478bd9Sstevel@tonic-gate 	firstsig = firstto->q_signature;
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	for (; to != NULL; to = to->q_next)
14997c478bd9Sstevel@tonic-gate 	{
15007c478bd9Sstevel@tonic-gate 		/* avoid sending multiple recipients to dumb mailers */
15017c478bd9Sstevel@tonic-gate 		if (tochain != NULL && !bitnset(M_MUSER, m->m_flags))
15027c478bd9Sstevel@tonic-gate 			break;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 		/* if already sent or not for this host, don't send */
15057c478bd9Sstevel@tonic-gate 		if (!QS_IS_OK(to->q_state)) /* already sent; look at next */
15067c478bd9Sstevel@tonic-gate 			continue;
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 		/*
15097c478bd9Sstevel@tonic-gate 		**  Must be same mailer to keep grouping rcpts.
15107c478bd9Sstevel@tonic-gate 		**  If mailers don't match: continue; sendqueue is not
15117c478bd9Sstevel@tonic-gate 		**  sorted by mailers, so don't break;
15127c478bd9Sstevel@tonic-gate 		*/
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 		if (to->q_mailer != firstto->q_mailer)
15157c478bd9Sstevel@tonic-gate 			continue;
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 		if (to->q_signature == NULL) /* for safety */
15187c478bd9Sstevel@tonic-gate 			to->q_signature = hostsignature(to->q_mailer,
15197c478bd9Sstevel@tonic-gate 							to->q_host);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 		/*
15227c478bd9Sstevel@tonic-gate 		**  This is for coincidental and tailcoat piggybacking messages
15237c478bd9Sstevel@tonic-gate 		**  to the same mail host. While the signatures are identical
15247c478bd9Sstevel@tonic-gate 		**  (that's the MX-RR's are identical) we can do coincidental
15257c478bd9Sstevel@tonic-gate 		**  piggybacking. We try hard for coattail piggybacking
15267c478bd9Sstevel@tonic-gate 		**  with the same mail host when the next recipient has the
15277c478bd9Sstevel@tonic-gate 		**  same host at lowest preference. It may be that this
15287c478bd9Sstevel@tonic-gate 		**  won't work out, so 'skip_back' is maintained if a backup
15297c478bd9Sstevel@tonic-gate 		**  to coincidental piggybacking or full signature must happen.
15307c478bd9Sstevel@tonic-gate 		*/
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 		ret = firstto == to ? HS_MATCH_FULL :
15337c478bd9Sstevel@tonic-gate 				      coloncmp(to->q_signature, firstsig);
15347c478bd9Sstevel@tonic-gate 		if (ret == HS_MATCH_FULL)
15357c478bd9Sstevel@tonic-gate 			skip_back = to;
15367c478bd9Sstevel@tonic-gate 		else if (ret == HS_MATCH_NO)
15377c478bd9Sstevel@tonic-gate 			break;
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 		if (!clever)
15407c478bd9Sstevel@tonic-gate 		{
15417c478bd9Sstevel@tonic-gate 			/* avoid overflowing tobuf */
15427c478bd9Sstevel@tonic-gate 			strsize += strlen(to->q_paddr) + 1;
15437c478bd9Sstevel@tonic-gate 			if (strsize > TOBUFSIZE)
15447c478bd9Sstevel@tonic-gate 				break;
15457c478bd9Sstevel@tonic-gate 		}
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 		if (++rcptcount > to->q_mailer->m_maxrcpt)
15487c478bd9Sstevel@tonic-gate 			break;
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 		if (tTd(10, 1))
15517c478bd9Sstevel@tonic-gate 		{
15527c478bd9Sstevel@tonic-gate 			sm_dprintf("\nsend to ");
15537c478bd9Sstevel@tonic-gate 			printaddr(sm_debug_file(), to, false);
15547c478bd9Sstevel@tonic-gate 		}
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 		/* compute effective uid/gid when sending */
15577c478bd9Sstevel@tonic-gate 		if (bitnset(M_RUNASRCPT, to->q_mailer->m_flags))
15587c478bd9Sstevel@tonic-gate # if HASSETUSERCONTEXT
15597c478bd9Sstevel@tonic-gate 			contextaddr = ctladdr = getctladdr(to);
15607c478bd9Sstevel@tonic-gate # else /* HASSETUSERCONTEXT */
15617c478bd9Sstevel@tonic-gate 			ctladdr = getctladdr(to);
15627c478bd9Sstevel@tonic-gate # endif /* HASSETUSERCONTEXT */
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 		if (tTd(10, 2))
15657c478bd9Sstevel@tonic-gate 		{
15667c478bd9Sstevel@tonic-gate 			sm_dprintf("ctladdr=");
15677c478bd9Sstevel@tonic-gate 			printaddr(sm_debug_file(), ctladdr, false);
15687c478bd9Sstevel@tonic-gate 		}
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 		user = to->q_user;
15717c478bd9Sstevel@tonic-gate 		e->e_to = to->q_paddr;
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 		/*
15747c478bd9Sstevel@tonic-gate 		**  Check to see that these people are allowed to
15757c478bd9Sstevel@tonic-gate 		**  talk to each other.
15767c478bd9Sstevel@tonic-gate 		**  Check also for overflow of e_msgsize.
15777c478bd9Sstevel@tonic-gate 		*/
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 		if (m->m_maxsize != 0 &&
15807c478bd9Sstevel@tonic-gate 		    (e->e_msgsize > m->m_maxsize || e->e_msgsize < 0))
15817c478bd9Sstevel@tonic-gate 		{
15827c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_NO_BODY_RETN;
15837c478bd9Sstevel@tonic-gate 			if (bitnset(M_LOCALMAILER, to->q_mailer->m_flags))
15847c478bd9Sstevel@tonic-gate 				to->q_status = "5.2.3";
15857c478bd9Sstevel@tonic-gate 			else
15867c478bd9Sstevel@tonic-gate 				to->q_status = "5.3.4";
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 			/* set to->q_rstatus = NULL; or to the following? */
15897c478bd9Sstevel@tonic-gate 			usrerrenh(to->q_status,
15907c478bd9Sstevel@tonic-gate 				  "552 Message is too large; %ld bytes max",
15917c478bd9Sstevel@tonic-gate 				  m->m_maxsize);
15927c478bd9Sstevel@tonic-gate 			markfailure(e, to, NULL, EX_UNAVAILABLE, false);
15937c478bd9Sstevel@tonic-gate 			giveresponse(EX_UNAVAILABLE, to->q_status, m,
15947c478bd9Sstevel@tonic-gate 				     NULL, ctladdr, xstart, e, to);
15957c478bd9Sstevel@tonic-gate 			continue;
15967c478bd9Sstevel@tonic-gate 		}
15977c478bd9Sstevel@tonic-gate 		SM_SET_H_ERRNO(0);
15987c478bd9Sstevel@tonic-gate 		ovr = true;
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 		/* do config file checking of compatibility */
16017c478bd9Sstevel@tonic-gate 		quarantine = (e->e_quarmsg != NULL);
16027c478bd9Sstevel@tonic-gate 		rcode = rscheck("check_compat", e->e_from.q_paddr, to->q_paddr,
16037c478bd9Sstevel@tonic-gate 				e, RSF_RMCOMM|RSF_COUNT, 3, NULL,
1604058561cbSjbeck 				e->e_id, NULL);
16057c478bd9Sstevel@tonic-gate 		if (rcode == EX_OK)
16067c478bd9Sstevel@tonic-gate 		{
16077c478bd9Sstevel@tonic-gate 			/* do in-code checking if not discarding */
16087c478bd9Sstevel@tonic-gate 			if (!bitset(EF_DISCARD, e->e_flags))
16097c478bd9Sstevel@tonic-gate 			{
16107c478bd9Sstevel@tonic-gate 				rcode = checkcompat(to, e);
16117c478bd9Sstevel@tonic-gate 				ovr = false;
16127c478bd9Sstevel@tonic-gate 			}
16137c478bd9Sstevel@tonic-gate 		}
16147c478bd9Sstevel@tonic-gate 		if (rcode != EX_OK)
16157c478bd9Sstevel@tonic-gate 		{
16167c478bd9Sstevel@tonic-gate 			markfailure(e, to, NULL, rcode, ovr);
16177c478bd9Sstevel@tonic-gate 			giveresponse(rcode, to->q_status, m,
16187c478bd9Sstevel@tonic-gate 				     NULL, ctladdr, xstart, e, to);
16197c478bd9Sstevel@tonic-gate 			continue;
16207c478bd9Sstevel@tonic-gate 		}
16217c478bd9Sstevel@tonic-gate 		if (!quarantine && e->e_quarmsg != NULL)
16227c478bd9Sstevel@tonic-gate 		{
16237c478bd9Sstevel@tonic-gate 			/*
16247c478bd9Sstevel@tonic-gate 			**  check_compat or checkcompat() has tried
16257c478bd9Sstevel@tonic-gate 			**  to quarantine but that isn't supported.
16267c478bd9Sstevel@tonic-gate 			**  Revert the attempt.
16277c478bd9Sstevel@tonic-gate 			*/
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 			e->e_quarmsg = NULL;
16307c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM,
16317c478bd9Sstevel@tonic-gate 				  macid("{quarantine}"), "");
16327c478bd9Sstevel@tonic-gate 		}
16337c478bd9Sstevel@tonic-gate 		if (bitset(EF_DISCARD, e->e_flags))
16347c478bd9Sstevel@tonic-gate 		{
16357c478bd9Sstevel@tonic-gate 			if (tTd(10, 5))
16367c478bd9Sstevel@tonic-gate 			{
16377c478bd9Sstevel@tonic-gate 				sm_dprintf("deliver: discarding recipient ");
16387c478bd9Sstevel@tonic-gate 				printaddr(sm_debug_file(), to, false);
16397c478bd9Sstevel@tonic-gate 			}
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 			/* pretend the message was sent */
16427c478bd9Sstevel@tonic-gate 			/* XXX should we log something here? */
16437c478bd9Sstevel@tonic-gate 			to->q_state = QS_DISCARDED;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 			/*
16467c478bd9Sstevel@tonic-gate 			**  Remove discard bit to prevent discard of
16477c478bd9Sstevel@tonic-gate 			**  future recipients.  This is safe because the
16487c478bd9Sstevel@tonic-gate 			**  true "global discard" has been handled before
16497c478bd9Sstevel@tonic-gate 			**  we get here.
16507c478bd9Sstevel@tonic-gate 			*/
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 			e->e_flags &= ~EF_DISCARD;
16537c478bd9Sstevel@tonic-gate 			continue;
16547c478bd9Sstevel@tonic-gate 		}
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 		/*
16577c478bd9Sstevel@tonic-gate 		**  Strip quote bits from names if the mailer is dumb
16587c478bd9Sstevel@tonic-gate 		**	about them.
16597c478bd9Sstevel@tonic-gate 		*/
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 		if (bitnset(M_STRIPQ, m->m_flags))
16627c478bd9Sstevel@tonic-gate 		{
16637c478bd9Sstevel@tonic-gate 			stripquotes(user);
16647c478bd9Sstevel@tonic-gate 			stripquotes(host);
16657c478bd9Sstevel@tonic-gate 		}
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 		/*
16687c478bd9Sstevel@tonic-gate 		**  Strip all leading backslashes if requested and the
16697c478bd9Sstevel@tonic-gate 		**  next character is alphanumerical (the latter can
16707c478bd9Sstevel@tonic-gate 		**  probably relaxed a bit, see RFC2821).
16717c478bd9Sstevel@tonic-gate 		*/
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 		if (bitnset(M_STRIPBACKSL, m->m_flags) && user[0] == '\\')
16747c478bd9Sstevel@tonic-gate 			stripbackslash(user);
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 		/* hack attack -- delivermail compatibility */
16777c478bd9Sstevel@tonic-gate 		if (m == ProgMailer && *user == '|')
16787c478bd9Sstevel@tonic-gate 			user++;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 		/*
16817c478bd9Sstevel@tonic-gate 		**  If an error message has already been given, don't
16827c478bd9Sstevel@tonic-gate 		**	bother to send to this address.
16837c478bd9Sstevel@tonic-gate 		**
16847c478bd9Sstevel@tonic-gate 		**	>>>>>>>>>> This clause assumes that the local mailer
16857c478bd9Sstevel@tonic-gate 		**	>> NOTE >> cannot do any further aliasing; that
16867c478bd9Sstevel@tonic-gate 		**	>>>>>>>>>> function is subsumed by sendmail.
16877c478bd9Sstevel@tonic-gate 		*/
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 		if (!QS_IS_OK(to->q_state))
16907c478bd9Sstevel@tonic-gate 			continue;
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 		/*
16937c478bd9Sstevel@tonic-gate 		**  See if this user name is "special".
16947c478bd9Sstevel@tonic-gate 		**	If the user name has a slash in it, assume that this
16957c478bd9Sstevel@tonic-gate 		**	is a file -- send it off without further ado.  Note
16967c478bd9Sstevel@tonic-gate 		**	that this type of addresses is not processed along
16977c478bd9Sstevel@tonic-gate 		**	with the others, so we fudge on the To person.
16987c478bd9Sstevel@tonic-gate 		*/
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 		if (strcmp(m->m_mailer, "[FILE]") == 0)
17017c478bd9Sstevel@tonic-gate 		{
17027c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM, 'u', user);
17037c478bd9Sstevel@tonic-gate 			p = to->q_home;
17047c478bd9Sstevel@tonic-gate 			if (p == NULL && ctladdr != NULL)
17057c478bd9Sstevel@tonic-gate 				p = ctladdr->q_home;
17067c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM, 'z', p);
1707058561cbSjbeck 			expand(m->m_argv[1], buf, sizeof(buf), e);
17087c478bd9Sstevel@tonic-gate 			if (strlen(buf) > 0)
17097c478bd9Sstevel@tonic-gate 				rcode = mailfile(buf, m, ctladdr, SFF_CREAT, e);
17107c478bd9Sstevel@tonic-gate 			else
17117c478bd9Sstevel@tonic-gate 			{
17127c478bd9Sstevel@tonic-gate 				syserr("empty filename specification for mailer %s",
17137c478bd9Sstevel@tonic-gate 				       m->m_name);
17147c478bd9Sstevel@tonic-gate 				rcode = EX_CONFIG;
17157c478bd9Sstevel@tonic-gate 			}
17167c478bd9Sstevel@tonic-gate 			giveresponse(rcode, to->q_status, m, NULL,
17177c478bd9Sstevel@tonic-gate 				     ctladdr, xstart, e, to);
17187c478bd9Sstevel@tonic-gate 			markfailure(e, to, NULL, rcode, true);
17197c478bd9Sstevel@tonic-gate 			e->e_nsent++;
17207c478bd9Sstevel@tonic-gate 			if (rcode == EX_OK)
17217c478bd9Sstevel@tonic-gate 			{
17227c478bd9Sstevel@tonic-gate 				to->q_state = QS_SENT;
17237c478bd9Sstevel@tonic-gate 				if (bitnset(M_LOCALMAILER, m->m_flags) &&
17247c478bd9Sstevel@tonic-gate 				    bitset(QPINGONSUCCESS, to->q_flags))
17257c478bd9Sstevel@tonic-gate 				{
17267c478bd9Sstevel@tonic-gate 					to->q_flags |= QDELIVERED;
17277c478bd9Sstevel@tonic-gate 					to->q_status = "2.1.5";
17287c478bd9Sstevel@tonic-gate 					(void) sm_io_fprintf(e->e_xfp,
17297c478bd9Sstevel@tonic-gate 							     SM_TIME_DEFAULT,
17307c478bd9Sstevel@tonic-gate 							     "%s... Successfully delivered\n",
17317c478bd9Sstevel@tonic-gate 							     to->q_paddr);
17327c478bd9Sstevel@tonic-gate 				}
17337c478bd9Sstevel@tonic-gate 			}
17347c478bd9Sstevel@tonic-gate 			to->q_statdate = curtime();
17357c478bd9Sstevel@tonic-gate 			markstats(e, to, STATS_NORMAL);
17367c478bd9Sstevel@tonic-gate 			continue;
17377c478bd9Sstevel@tonic-gate 		}
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 		/*
17407c478bd9Sstevel@tonic-gate 		**  Address is verified -- add this user to mailer
17417c478bd9Sstevel@tonic-gate 		**  argv, and add it to the print list of recipients.
17427c478bd9Sstevel@tonic-gate 		*/
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 		/* link together the chain of recipients */
17457c478bd9Sstevel@tonic-gate 		to->q_tchain = tochain;
17467c478bd9Sstevel@tonic-gate 		tochain = to;
17477c478bd9Sstevel@tonic-gate 		e->e_to = "[CHAIN]";
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, 'u', user);  /* to user */
17507c478bd9Sstevel@tonic-gate 		p = to->q_home;
17517c478bd9Sstevel@tonic-gate 		if (p == NULL && ctladdr != NULL)
17527c478bd9Sstevel@tonic-gate 			p = ctladdr->q_home;
17537c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, 'z', p);  /* user's home */
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 		/* set the ${dsn_notify} macro if applicable */
17567c478bd9Sstevel@tonic-gate 		if (bitset(QHASNOTIFY, to->q_flags))
17577c478bd9Sstevel@tonic-gate 		{
17587c478bd9Sstevel@tonic-gate 			char notify[MAXLINE];
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 			notify[0] = '\0';
17617c478bd9Sstevel@tonic-gate 			if (bitset(QPINGONSUCCESS, to->q_flags))
17627c478bd9Sstevel@tonic-gate 				(void) sm_strlcat(notify, "SUCCESS,",
1763058561cbSjbeck 						  sizeof(notify));
17647c478bd9Sstevel@tonic-gate 			if (bitset(QPINGONFAILURE, to->q_flags))
17657c478bd9Sstevel@tonic-gate 				(void) sm_strlcat(notify, "FAILURE,",
1766058561cbSjbeck 						  sizeof(notify));
17677c478bd9Sstevel@tonic-gate 			if (bitset(QPINGONDELAY, to->q_flags))
17687c478bd9Sstevel@tonic-gate 				(void) sm_strlcat(notify, "DELAY,",
1769058561cbSjbeck 						  sizeof(notify));
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 			/* Set to NEVER or drop trailing comma */
17727c478bd9Sstevel@tonic-gate 			if (notify[0] == '\0')
17737c478bd9Sstevel@tonic-gate 				(void) sm_strlcat(notify, "NEVER",
1774058561cbSjbeck 						  sizeof(notify));
17757c478bd9Sstevel@tonic-gate 			else
17767c478bd9Sstevel@tonic-gate 				notify[strlen(notify) - 1] = '\0';
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_TEMP,
17797c478bd9Sstevel@tonic-gate 				macid("{dsn_notify}"), notify);
17807c478bd9Sstevel@tonic-gate 		}
17817c478bd9Sstevel@tonic-gate 		else
17827c478bd9Sstevel@tonic-gate 			macdefine(&e->e_macro, A_PERM,
17837c478bd9Sstevel@tonic-gate 				macid("{dsn_notify}"), NULL);
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 		/*
17867c478bd9Sstevel@tonic-gate 		**  Expand out this user into argument list.
17877c478bd9Sstevel@tonic-gate 		*/
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 		if (!clever)
17907c478bd9Sstevel@tonic-gate 		{
1791058561cbSjbeck 			expand(*mvp, buf, sizeof(buf), e);
17927c478bd9Sstevel@tonic-gate 			*pvp++ = sm_rpool_strdup_x(e->e_rpool, buf);
17937c478bd9Sstevel@tonic-gate 			if (pvp >= &pv[MAXPV - 2])
17947c478bd9Sstevel@tonic-gate 			{
17957c478bd9Sstevel@tonic-gate 				/* allow some space for trailing parms */
17967c478bd9Sstevel@tonic-gate 				break;
17977c478bd9Sstevel@tonic-gate 			}
17987c478bd9Sstevel@tonic-gate 		}
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	/* see if any addresses still exist */
18027c478bd9Sstevel@tonic-gate 	if (tochain == NULL)
18037c478bd9Sstevel@tonic-gate 	{
18047c478bd9Sstevel@tonic-gate 		rcode = 0;
18057c478bd9Sstevel@tonic-gate 		goto cleanup;
18067c478bd9Sstevel@tonic-gate 	}
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	/* print out messages as full list */
18097c478bd9Sstevel@tonic-gate 	strsize = 1;
18107c478bd9Sstevel@tonic-gate 	for (to = tochain; to != NULL; to = to->q_tchain)
18117c478bd9Sstevel@tonic-gate 		strsize += strlen(to->q_paddr) + 1;
18127c478bd9Sstevel@tonic-gate 	if (strsize < TOBUFSIZE)
18137c478bd9Sstevel@tonic-gate 		strsize = TOBUFSIZE;
18147c478bd9Sstevel@tonic-gate 	if (strsize > tobufsize)
18157c478bd9Sstevel@tonic-gate 	{
18167c478bd9Sstevel@tonic-gate 		SM_FREE_CLR(tobuf);
18177c478bd9Sstevel@tonic-gate 		tobuf = sm_pmalloc_x(strsize);
18187c478bd9Sstevel@tonic-gate 		tobufsize = strsize;
18197c478bd9Sstevel@tonic-gate 	}
18207c478bd9Sstevel@tonic-gate 	p = tobuf;
18217c478bd9Sstevel@tonic-gate 	*p = '\0';
18227c478bd9Sstevel@tonic-gate 	for (to = tochain; to != NULL; to = to->q_tchain)
18237c478bd9Sstevel@tonic-gate 	{
18247c478bd9Sstevel@tonic-gate 		(void) sm_strlcpyn(p, tobufsize - (p - tobuf), 2,
18257c478bd9Sstevel@tonic-gate 				   ",", to->q_paddr);
18267c478bd9Sstevel@tonic-gate 		p += strlen(p);
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate 	e->e_to = tobuf + 1;
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	/*
18317c478bd9Sstevel@tonic-gate 	**  Fill out any parameters after the $u parameter.
18327c478bd9Sstevel@tonic-gate 	*/
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	if (!clever)
18357c478bd9Sstevel@tonic-gate 	{
18367c478bd9Sstevel@tonic-gate 		while (*++mvp != NULL)
18377c478bd9Sstevel@tonic-gate 		{
1838058561cbSjbeck 			expand(*mvp, buf, sizeof(buf), e);
18397c478bd9Sstevel@tonic-gate 			*pvp++ = sm_rpool_strdup_x(e->e_rpool, buf);
18407c478bd9Sstevel@tonic-gate 			if (pvp >= &pv[MAXPV])
18417c478bd9Sstevel@tonic-gate 				syserr("554 5.3.0 deliver: pv overflow after $u for %s",
18427c478bd9Sstevel@tonic-gate 				       pv[0]);
18437c478bd9Sstevel@tonic-gate 		}
18447c478bd9Sstevel@tonic-gate 	}
18457c478bd9Sstevel@tonic-gate 	*pvp++ = NULL;
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	/*
18487c478bd9Sstevel@tonic-gate 	**  Call the mailer.
18497c478bd9Sstevel@tonic-gate 	**	The argument vector gets built, pipes
18507c478bd9Sstevel@tonic-gate 	**	are created as necessary, and we fork & exec as
18517c478bd9Sstevel@tonic-gate 	**	appropriate.
18527c478bd9Sstevel@tonic-gate 	**	If we are running SMTP, we just need to clean up.
18537c478bd9Sstevel@tonic-gate 	*/
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	/* XXX this seems a bit wierd */
18567c478bd9Sstevel@tonic-gate 	if (ctladdr == NULL && m != ProgMailer && m != FileMailer &&
18577c478bd9Sstevel@tonic-gate 	    bitset(QGOODUID, e->e_from.q_flags))
18587c478bd9Sstevel@tonic-gate 		ctladdr = &e->e_from;
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate #if NAMED_BIND
18617c478bd9Sstevel@tonic-gate 	if (ConfigLevel < 2)
18627c478bd9Sstevel@tonic-gate 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
18637c478bd9Sstevel@tonic-gate #endif /* NAMED_BIND */
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	if (tTd(11, 1))
18667c478bd9Sstevel@tonic-gate 	{
18677c478bd9Sstevel@tonic-gate 		sm_dprintf("openmailer:");
18687c478bd9Sstevel@tonic-gate 		printav(sm_debug_file(), pv);
18697c478bd9Sstevel@tonic-gate 	}
18707c478bd9Sstevel@tonic-gate 	errno = 0;
18717c478bd9Sstevel@tonic-gate 	SM_SET_H_ERRNO(0);
18727c478bd9Sstevel@tonic-gate 	CurHostName = NULL;
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	/*
18757c478bd9Sstevel@tonic-gate 	**  Deal with the special case of mail handled through an IPC
18767c478bd9Sstevel@tonic-gate 	**  connection.
18777c478bd9Sstevel@tonic-gate 	**	In this case we don't actually fork.  We must be
18787c478bd9Sstevel@tonic-gate 	**	running SMTP for this to work.  We will return a
18797c478bd9Sstevel@tonic-gate 	**	zero pid to indicate that we are running IPC.
18807c478bd9Sstevel@tonic-gate 	**  We also handle a debug version that just talks to stdin/out.
18817c478bd9Sstevel@tonic-gate 	*/
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	curhost = NULL;
18847c478bd9Sstevel@tonic-gate 	SmtpPhase = NULL;
18857c478bd9Sstevel@tonic-gate 	mci = NULL;
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate #if XDEBUG
18887c478bd9Sstevel@tonic-gate 	{
18897c478bd9Sstevel@tonic-gate 		char wbuf[MAXLINE];
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 		/* make absolutely certain 0, 1, and 2 are in use */
1892058561cbSjbeck 		(void) sm_snprintf(wbuf, sizeof(wbuf), "%s... openmailer(%s)",
18937c478bd9Sstevel@tonic-gate 				   shortenstring(e->e_to, MAXSHORTSTR),
18947c478bd9Sstevel@tonic-gate 				   m->m_name);
18957c478bd9Sstevel@tonic-gate 		checkfd012(wbuf);
18967c478bd9Sstevel@tonic-gate 	}
18977c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	/* check for 8-bit available */
19007c478bd9Sstevel@tonic-gate 	if (bitset(EF_HAS8BIT, e->e_flags) &&
19017c478bd9Sstevel@tonic-gate 	    bitnset(M_7BITS, m->m_flags) &&
19027c478bd9Sstevel@tonic-gate 	    (bitset(EF_DONT_MIME, e->e_flags) ||
19037c478bd9Sstevel@tonic-gate 	     !(bitset(MM_MIME8BIT, MimeMode) ||
19047c478bd9Sstevel@tonic-gate 	       (bitset(EF_IS_MIME, e->e_flags) &&
19057c478bd9Sstevel@tonic-gate 		bitset(MM_CVTMIME, MimeMode)))))
19067c478bd9Sstevel@tonic-gate 	{
19077c478bd9Sstevel@tonic-gate 		e->e_status = "5.6.3";
19087c478bd9Sstevel@tonic-gate 		usrerrenh(e->e_status,
19097c478bd9Sstevel@tonic-gate 			  "554 Cannot send 8-bit data to 7-bit destination");
19107c478bd9Sstevel@tonic-gate 		rcode = EX_DATAERR;
19117c478bd9Sstevel@tonic-gate 		goto give_up;
19127c478bd9Sstevel@tonic-gate 	}
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	if (tTd(62, 8))
19157c478bd9Sstevel@tonic-gate 		checkfds("before delivery");
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	/* check for Local Person Communication -- not for mortals!!! */
19187c478bd9Sstevel@tonic-gate 	if (strcmp(m->m_mailer, "[LPC]") == 0)
19197c478bd9Sstevel@tonic-gate 	{
19207c478bd9Sstevel@tonic-gate 		if (clever)
19217c478bd9Sstevel@tonic-gate 		{
19227c478bd9Sstevel@tonic-gate 			/* flush any expired connections */
19237c478bd9Sstevel@tonic-gate 			(void) mci_scan(NULL);
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 			/* try to get a cached connection or just a slot */
19267c478bd9Sstevel@tonic-gate 			mci = mci_get(m->m_name, m);
19277c478bd9Sstevel@tonic-gate 			if (mci->mci_host == NULL)
19287c478bd9Sstevel@tonic-gate 				mci->mci_host = m->m_name;
19297c478bd9Sstevel@tonic-gate 			CurHostName = mci->mci_host;
19307c478bd9Sstevel@tonic-gate 			if (mci->mci_state != MCIS_CLOSED)
19317c478bd9Sstevel@tonic-gate 			{
19327c478bd9Sstevel@tonic-gate 				message("Using cached SMTP/LPC connection for %s...",
19337c478bd9Sstevel@tonic-gate 					m->m_name);
19347c478bd9Sstevel@tonic-gate 				mci->mci_deliveries++;
19357c478bd9Sstevel@tonic-gate 				goto do_transfer;
19367c478bd9Sstevel@tonic-gate 			}
19377c478bd9Sstevel@tonic-gate 		}
19387c478bd9Sstevel@tonic-gate 		else
19397c478bd9Sstevel@tonic-gate 		{
19407c478bd9Sstevel@tonic-gate 			mci = mci_new(e->e_rpool);
19417c478bd9Sstevel@tonic-gate 		}
19427c478bd9Sstevel@tonic-gate 		mci->mci_in = smioin;
19437c478bd9Sstevel@tonic-gate 		mci->mci_out = smioout;
19447c478bd9Sstevel@tonic-gate 		mci->mci_mailer = m;
19457c478bd9Sstevel@tonic-gate 		mci->mci_host = m->m_name;
19467c478bd9Sstevel@tonic-gate 		if (clever)
19477c478bd9Sstevel@tonic-gate 		{
19487c478bd9Sstevel@tonic-gate 			mci->mci_state = MCIS_OPENING;
19497c478bd9Sstevel@tonic-gate 			mci_cache(mci);
19507c478bd9Sstevel@tonic-gate 		}
19517c478bd9Sstevel@tonic-gate 		else
19527c478bd9Sstevel@tonic-gate 			mci->mci_state = MCIS_OPEN;
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 	else if (strcmp(m->m_mailer, "[IPC]") == 0)
19557c478bd9Sstevel@tonic-gate 	{
19567c478bd9Sstevel@tonic-gate 		register int i;
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 		if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
19597c478bd9Sstevel@tonic-gate 		{
19607c478bd9Sstevel@tonic-gate 			syserr("null destination for %s mailer", m->m_mailer);
19617c478bd9Sstevel@tonic-gate 			rcode = EX_CONFIG;
19627c478bd9Sstevel@tonic-gate 			goto give_up;
19637c478bd9Sstevel@tonic-gate 		}
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate # if NETUNIX
19667c478bd9Sstevel@tonic-gate 		if (strcmp(pv[0], "FILE") == 0)
19677c478bd9Sstevel@tonic-gate 		{
19687c478bd9Sstevel@tonic-gate 			curhost = CurHostName = "localhost";
19697c478bd9Sstevel@tonic-gate 			mux_path = pv[1];
19707c478bd9Sstevel@tonic-gate 		}
19717c478bd9Sstevel@tonic-gate 		else
19727c478bd9Sstevel@tonic-gate # endif /* NETUNIX */
19737c478bd9Sstevel@tonic-gate 		{
19747c478bd9Sstevel@tonic-gate 			CurHostName = pv[1];
19757c478bd9Sstevel@tonic-gate 			curhost = hostsignature(m, pv[1]);
19767c478bd9Sstevel@tonic-gate 		}
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 		if (curhost == NULL || curhost[0] == '\0')
19797c478bd9Sstevel@tonic-gate 		{
19807c478bd9Sstevel@tonic-gate 			syserr("null host signature for %s", pv[1]);
19817c478bd9Sstevel@tonic-gate 			rcode = EX_CONFIG;
19827c478bd9Sstevel@tonic-gate 			goto give_up;
19837c478bd9Sstevel@tonic-gate 		}
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 		if (!clever)
19867c478bd9Sstevel@tonic-gate 		{
19877c478bd9Sstevel@tonic-gate 			syserr("554 5.3.5 non-clever IPC");
19887c478bd9Sstevel@tonic-gate 			rcode = EX_CONFIG;
19897c478bd9Sstevel@tonic-gate 			goto give_up;
19907c478bd9Sstevel@tonic-gate 		}
19917c478bd9Sstevel@tonic-gate 		if (pv[2] != NULL
19927c478bd9Sstevel@tonic-gate # if NETUNIX
19937c478bd9Sstevel@tonic-gate 		    && mux_path == NULL
19947c478bd9Sstevel@tonic-gate # endif /* NETUNIX */
19957c478bd9Sstevel@tonic-gate 		    )
19967c478bd9Sstevel@tonic-gate 		{
19977c478bd9Sstevel@tonic-gate 			port = htons((unsigned short) atoi(pv[2]));
19987c478bd9Sstevel@tonic-gate 			if (port == 0)
19997c478bd9Sstevel@tonic-gate 			{
20007c478bd9Sstevel@tonic-gate # ifdef NO_GETSERVBYNAME
20017c478bd9Sstevel@tonic-gate 				syserr("Invalid port number: %s", pv[2]);
20027c478bd9Sstevel@tonic-gate # else /* NO_GETSERVBYNAME */
20037c478bd9Sstevel@tonic-gate 				struct servent *sp = getservbyname(pv[2], "tcp");
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 				if (sp == NULL)
20067c478bd9Sstevel@tonic-gate 					syserr("Service %s unknown", pv[2]);
20077c478bd9Sstevel@tonic-gate 				else
20087c478bd9Sstevel@tonic-gate 					port = sp->s_port;
20097c478bd9Sstevel@tonic-gate # endif /* NO_GETSERVBYNAME */
20107c478bd9Sstevel@tonic-gate 			}
20117c478bd9Sstevel@tonic-gate 		}
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 		nummxhosts = parse_hostsignature(curhost, mxhosts, m);
20147c478bd9Sstevel@tonic-gate 		if (TimeOuts.to_aconnect > 0)
20157c478bd9Sstevel@tonic-gate 			enough = curtime() + TimeOuts.to_aconnect;
20167c478bd9Sstevel@tonic-gate tryhost:
20177c478bd9Sstevel@tonic-gate 		while (hostnum < nummxhosts)
20187c478bd9Sstevel@tonic-gate 		{
20197c478bd9Sstevel@tonic-gate 			char sep = ':';
20207c478bd9Sstevel@tonic-gate 			char *endp;
20217c478bd9Sstevel@tonic-gate 			static char hostbuf[MAXNAME + 1];
20227c478bd9Sstevel@tonic-gate 			bool tried_fallbacksmarthost = false;
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate # if NETINET6
20257c478bd9Sstevel@tonic-gate 			if (*mxhosts[hostnum] == '[')
20267c478bd9Sstevel@tonic-gate 			{
20277c478bd9Sstevel@tonic-gate 				endp = strchr(mxhosts[hostnum] + 1, ']');
20287c478bd9Sstevel@tonic-gate 				if (endp != NULL)
20297c478bd9Sstevel@tonic-gate 					endp = strpbrk(endp + 1, ":,");
20307c478bd9Sstevel@tonic-gate 			}
20317c478bd9Sstevel@tonic-gate 			else
20327c478bd9Sstevel@tonic-gate 				endp = strpbrk(mxhosts[hostnum], ":,");
20337c478bd9Sstevel@tonic-gate # else /* NETINET6 */
20347c478bd9Sstevel@tonic-gate 			endp = strpbrk(mxhosts[hostnum], ":,");
20357c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
20367c478bd9Sstevel@tonic-gate 			if (endp != NULL)
20377c478bd9Sstevel@tonic-gate 			{
20387c478bd9Sstevel@tonic-gate 				sep = *endp;
20397c478bd9Sstevel@tonic-gate 				*endp = '\0';
20407c478bd9Sstevel@tonic-gate 			}
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 			if (hostnum == 1 && skip_back != NULL)
20437c478bd9Sstevel@tonic-gate 			{
20447c478bd9Sstevel@tonic-gate 				/*
20457c478bd9Sstevel@tonic-gate 				**  Coattail piggybacking is no longer an
20467c478bd9Sstevel@tonic-gate 				**  option with the mail host next to be tried
20477c478bd9Sstevel@tonic-gate 				**  no longer the lowest MX preference
20487c478bd9Sstevel@tonic-gate 				**  (hostnum == 1 meaning we're on the second
20497c478bd9Sstevel@tonic-gate 				**  preference). We do not try to coattail
20507c478bd9Sstevel@tonic-gate 				**  piggyback more than the first MX preference.
20517c478bd9Sstevel@tonic-gate 				**  Revert 'tochain' to last location for
20527c478bd9Sstevel@tonic-gate 				**  coincidental piggybacking. This works this
20537c478bd9Sstevel@tonic-gate 				**  easily because the q_tchain kept getting
20547c478bd9Sstevel@tonic-gate 				**  added to the top of the linked list.
20557c478bd9Sstevel@tonic-gate 				*/
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 				tochain = skip_back;
20587c478bd9Sstevel@tonic-gate 			}
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 			if (*mxhosts[hostnum] == '\0')
20617c478bd9Sstevel@tonic-gate 			{
20627c478bd9Sstevel@tonic-gate 				syserr("deliver: null host name in signature");
20637c478bd9Sstevel@tonic-gate 				hostnum++;
20647c478bd9Sstevel@tonic-gate 				if (endp != NULL)
20657c478bd9Sstevel@tonic-gate 					*endp = sep;
20667c478bd9Sstevel@tonic-gate 				continue;
20677c478bd9Sstevel@tonic-gate 			}
20687c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(hostbuf, mxhosts[hostnum],
2069058561cbSjbeck 					  sizeof(hostbuf));
20707c478bd9Sstevel@tonic-gate 			hostnum++;
20717c478bd9Sstevel@tonic-gate 			if (endp != NULL)
20727c478bd9Sstevel@tonic-gate 				*endp = sep;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate   one_last_try:
20757c478bd9Sstevel@tonic-gate 			/* see if we already know that this host is fried */
20767c478bd9Sstevel@tonic-gate 			CurHostName = hostbuf;
20777c478bd9Sstevel@tonic-gate 			mci = mci_get(hostbuf, m);
20787c478bd9Sstevel@tonic-gate 			if (mci->mci_state != MCIS_CLOSED)
20797c478bd9Sstevel@tonic-gate 			{
20807c478bd9Sstevel@tonic-gate 				char *type;
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 				if (tTd(11, 1))
20837c478bd9Sstevel@tonic-gate 				{
20847c478bd9Sstevel@tonic-gate 					sm_dprintf("openmailer: ");
20857c478bd9Sstevel@tonic-gate 					mci_dump(sm_debug_file(), mci, false);
20867c478bd9Sstevel@tonic-gate 				}
20877c478bd9Sstevel@tonic-gate 				CurHostName = mci->mci_host;
20887c478bd9Sstevel@tonic-gate 				if (bitnset(M_LMTP, m->m_flags))
20897c478bd9Sstevel@tonic-gate 					type = "L";
20907c478bd9Sstevel@tonic-gate 				else if (bitset(MCIF_ESMTP, mci->mci_flags))
20917c478bd9Sstevel@tonic-gate 					type = "ES";
20927c478bd9Sstevel@tonic-gate 				else
20937c478bd9Sstevel@tonic-gate 					type = "S";
20947c478bd9Sstevel@tonic-gate 				message("Using cached %sMTP connection to %s via %s...",
20957c478bd9Sstevel@tonic-gate 					type, hostbuf, m->m_name);
20967c478bd9Sstevel@tonic-gate 				mci->mci_deliveries++;
20977c478bd9Sstevel@tonic-gate 				break;
20987c478bd9Sstevel@tonic-gate 			}
20997c478bd9Sstevel@tonic-gate 			mci->mci_mailer = m;
21007c478bd9Sstevel@tonic-gate 			if (mci->mci_exitstat != EX_OK)
21017c478bd9Sstevel@tonic-gate 			{
21027c478bd9Sstevel@tonic-gate 				if (mci->mci_exitstat == EX_TEMPFAIL)
21037c478bd9Sstevel@tonic-gate 					goodmxfound = true;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 				/* Try FallbackSmartHost? */
21067c478bd9Sstevel@tonic-gate 				if (should_try_fbsh(e, &tried_fallbacksmarthost,
2107058561cbSjbeck 						    hostbuf, sizeof(hostbuf),
21087c478bd9Sstevel@tonic-gate 						    mci->mci_exitstat))
21097c478bd9Sstevel@tonic-gate 					goto one_last_try;
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 				continue;
21127c478bd9Sstevel@tonic-gate 			}
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 			if (mci_lock_host(mci) != EX_OK)
21157c478bd9Sstevel@tonic-gate 			{
21167c478bd9Sstevel@tonic-gate 				mci_setstat(mci, EX_TEMPFAIL, "4.4.5", NULL);
21177c478bd9Sstevel@tonic-gate 				goodmxfound = true;
21187c478bd9Sstevel@tonic-gate 				continue;
21197c478bd9Sstevel@tonic-gate 			}
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 			/* try the connection */
21227c478bd9Sstevel@tonic-gate 			sm_setproctitle(true, e, "%s %s: %s",
21237c478bd9Sstevel@tonic-gate 					qid_printname(e),
21247c478bd9Sstevel@tonic-gate 					hostbuf, "user open");
21257c478bd9Sstevel@tonic-gate # if NETUNIX
21267c478bd9Sstevel@tonic-gate 			if (mux_path != NULL)
21277c478bd9Sstevel@tonic-gate 			{
21287c478bd9Sstevel@tonic-gate 				message("Connecting to %s via %s...",
21297c478bd9Sstevel@tonic-gate 					mux_path, m->m_name);
21307c478bd9Sstevel@tonic-gate 				i = makeconnection_ds((char *) mux_path, mci);
21317c478bd9Sstevel@tonic-gate 			}
21327c478bd9Sstevel@tonic-gate 			else
21337c478bd9Sstevel@tonic-gate # endif /* NETUNIX */
21347c478bd9Sstevel@tonic-gate 			{
21357c478bd9Sstevel@tonic-gate 				if (port == 0)
21367c478bd9Sstevel@tonic-gate 					message("Connecting to %s via %s...",
21377c478bd9Sstevel@tonic-gate 						hostbuf, m->m_name);
21387c478bd9Sstevel@tonic-gate 				else
21397c478bd9Sstevel@tonic-gate 					message("Connecting to %s port %d via %s...",
21407c478bd9Sstevel@tonic-gate 						hostbuf, ntohs(port),
21417c478bd9Sstevel@tonic-gate 						m->m_name);
21427c478bd9Sstevel@tonic-gate 				i = makeconnection(hostbuf, port, mci, e,
21437c478bd9Sstevel@tonic-gate 						   enough);
21447c478bd9Sstevel@tonic-gate 			}
21457c478bd9Sstevel@tonic-gate 			mci->mci_errno = errno;
21467c478bd9Sstevel@tonic-gate 			mci->mci_lastuse = curtime();
21477c478bd9Sstevel@tonic-gate 			mci->mci_deliveries = 0;
21487c478bd9Sstevel@tonic-gate 			mci->mci_exitstat = i;
21497c478bd9Sstevel@tonic-gate # if NAMED_BIND
21507c478bd9Sstevel@tonic-gate 			mci->mci_herrno = h_errno;
21517c478bd9Sstevel@tonic-gate # endif /* NAMED_BIND */
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 			/*
21547c478bd9Sstevel@tonic-gate 			**  Have we tried long enough to get a connection?
21557c478bd9Sstevel@tonic-gate 			**	If yes, skip to the fallback MX hosts
21567c478bd9Sstevel@tonic-gate 			**	(if existent).
21577c478bd9Sstevel@tonic-gate 			*/
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 			if (enough > 0 && mci->mci_lastuse >= enough)
21607c478bd9Sstevel@tonic-gate 			{
21617c478bd9Sstevel@tonic-gate 				int h;
21627c478bd9Sstevel@tonic-gate # if NAMED_BIND
21637c478bd9Sstevel@tonic-gate 				extern int NumFallbackMXHosts;
21647c478bd9Sstevel@tonic-gate # else /* NAMED_BIND */
21657c478bd9Sstevel@tonic-gate 				const int NumFallbackMXHosts = 0;
21667c478bd9Sstevel@tonic-gate # endif /* NAMED_BIND */
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 				if (hostnum < nummxhosts && LogLevel > 9)
21697c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, e->e_id,
21707c478bd9Sstevel@tonic-gate 						  "Timeout.to_aconnect occurred before exhausting all addresses");
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 				/* turn off timeout if fallback available */
21737c478bd9Sstevel@tonic-gate 				if (NumFallbackMXHosts > 0)
21747c478bd9Sstevel@tonic-gate 					enough = 0;
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 				/* skip to a fallback MX host */
21777c478bd9Sstevel@tonic-gate 				h = nummxhosts - NumFallbackMXHosts;
21787c478bd9Sstevel@tonic-gate 				if (hostnum < h)
21797c478bd9Sstevel@tonic-gate 					hostnum = h;
21807c478bd9Sstevel@tonic-gate 			}
21817c478bd9Sstevel@tonic-gate 			if (i == EX_OK)
21827c478bd9Sstevel@tonic-gate 			{
21837c478bd9Sstevel@tonic-gate 				goodmxfound = true;
21847c478bd9Sstevel@tonic-gate 				markstats(e, firstto, STATS_CONNECT);
21857c478bd9Sstevel@tonic-gate 				mci->mci_state = MCIS_OPENING;
21867c478bd9Sstevel@tonic-gate 				mci_cache(mci);
21877c478bd9Sstevel@tonic-gate 				if (TrafficLogFile != NULL)
21887c478bd9Sstevel@tonic-gate 					(void) sm_io_fprintf(TrafficLogFile,
21897c478bd9Sstevel@tonic-gate 							     SM_TIME_DEFAULT,
21907c478bd9Sstevel@tonic-gate 							     "%05d === CONNECT %s\n",
21917c478bd9Sstevel@tonic-gate 							     (int) CurrentPid,
21927c478bd9Sstevel@tonic-gate 							     hostbuf);
21937c478bd9Sstevel@tonic-gate 				break;
21947c478bd9Sstevel@tonic-gate 			}
21957c478bd9Sstevel@tonic-gate 			else
21967c478bd9Sstevel@tonic-gate 			{
21977c478bd9Sstevel@tonic-gate 				/* Try FallbackSmartHost? */
21987c478bd9Sstevel@tonic-gate 				if (should_try_fbsh(e, &tried_fallbacksmarthost,
2199058561cbSjbeck 						    hostbuf, sizeof(hostbuf), i))
22007c478bd9Sstevel@tonic-gate 					goto one_last_try;
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 				if (tTd(11, 1))
22037c478bd9Sstevel@tonic-gate 					sm_dprintf("openmailer: makeconnection => stat=%d, errno=%d\n",
22047c478bd9Sstevel@tonic-gate 						   i, errno);
22057c478bd9Sstevel@tonic-gate 				if (i == EX_TEMPFAIL)
22067c478bd9Sstevel@tonic-gate 					goodmxfound = true;
22077c478bd9Sstevel@tonic-gate 				mci_unlock_host(mci);
22087c478bd9Sstevel@tonic-gate 			}
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 			/* enter status of this host */
22117c478bd9Sstevel@tonic-gate 			setstat(i);
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 			/* should print some message here for -v mode */
22147c478bd9Sstevel@tonic-gate 		}
22157c478bd9Sstevel@tonic-gate 		if (mci == NULL)
22167c478bd9Sstevel@tonic-gate 		{
22177c478bd9Sstevel@tonic-gate 			syserr("deliver: no host name");
22187c478bd9Sstevel@tonic-gate 			rcode = EX_SOFTWARE;
22197c478bd9Sstevel@tonic-gate 			goto give_up;
22207c478bd9Sstevel@tonic-gate 		}
22217c478bd9Sstevel@tonic-gate 		mci->mci_pid = 0;
22227c478bd9Sstevel@tonic-gate 	}
22237c478bd9Sstevel@tonic-gate 	else
22247c478bd9Sstevel@tonic-gate 	{
22257c478bd9Sstevel@tonic-gate 		/* flush any expired connections */
22267c478bd9Sstevel@tonic-gate 		(void) mci_scan(NULL);
22277c478bd9Sstevel@tonic-gate 		mci = NULL;
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 		if (bitnset(M_LMTP, m->m_flags))
22307c478bd9Sstevel@tonic-gate 		{
22317c478bd9Sstevel@tonic-gate 			/* try to get a cached connection */
22327c478bd9Sstevel@tonic-gate 			mci = mci_get(m->m_name, m);
22337c478bd9Sstevel@tonic-gate 			if (mci->mci_host == NULL)
22347c478bd9Sstevel@tonic-gate 				mci->mci_host = m->m_name;
22357c478bd9Sstevel@tonic-gate 			CurHostName = mci->mci_host;
22367c478bd9Sstevel@tonic-gate 			if (mci->mci_state != MCIS_CLOSED)
22377c478bd9Sstevel@tonic-gate 			{
22387c478bd9Sstevel@tonic-gate 				message("Using cached LMTP connection for %s...",
22397c478bd9Sstevel@tonic-gate 					m->m_name);
22407c478bd9Sstevel@tonic-gate 				mci->mci_deliveries++;
22417c478bd9Sstevel@tonic-gate 				goto do_transfer;
22427c478bd9Sstevel@tonic-gate 			}
22437c478bd9Sstevel@tonic-gate 		}
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 		/* announce the connection to verbose listeners */
22467c478bd9Sstevel@tonic-gate 		if (host == NULL || host[0] == '\0')
22477c478bd9Sstevel@tonic-gate 			message("Connecting to %s...", m->m_name);
22487c478bd9Sstevel@tonic-gate 		else
22497c478bd9Sstevel@tonic-gate 			message("Connecting to %s via %s...", host, m->m_name);
22507c478bd9Sstevel@tonic-gate 		if (TrafficLogFile != NULL)
22517c478bd9Sstevel@tonic-gate 		{
22527c478bd9Sstevel@tonic-gate 			char **av;
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
22557c478bd9Sstevel@tonic-gate 					     "%05d === EXEC", (int) CurrentPid);
22567c478bd9Sstevel@tonic-gate 			for (av = pv; *av != NULL; av++)
22577c478bd9Sstevel@tonic-gate 				(void) sm_io_fprintf(TrafficLogFile,
22587c478bd9Sstevel@tonic-gate 						     SM_TIME_DEFAULT, " %s",
22597c478bd9Sstevel@tonic-gate 						     *av);
22607c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(TrafficLogFile, SM_TIME_DEFAULT,
22617c478bd9Sstevel@tonic-gate 					     "\n");
22627c478bd9Sstevel@tonic-gate 		}
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate #if XDEBUG
22657c478bd9Sstevel@tonic-gate 		checkfd012("before creating mail pipe");
22667c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 		/* create a pipe to shove the mail through */
22697c478bd9Sstevel@tonic-gate 		if (pipe(mpvect) < 0)
22707c478bd9Sstevel@tonic-gate 		{
22717c478bd9Sstevel@tonic-gate 			syserr("%s... openmailer(%s): pipe (to mailer)",
22727c478bd9Sstevel@tonic-gate 			       shortenstring(e->e_to, MAXSHORTSTR), m->m_name);
22737c478bd9Sstevel@tonic-gate 			if (tTd(11, 1))
22747c478bd9Sstevel@tonic-gate 				sm_dprintf("openmailer: NULL\n");
22757c478bd9Sstevel@tonic-gate 			rcode = EX_OSERR;
22767c478bd9Sstevel@tonic-gate 			goto give_up;
22777c478bd9Sstevel@tonic-gate 		}
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate #if XDEBUG
22807c478bd9Sstevel@tonic-gate 		/* make sure we didn't get one of the standard I/O files */
22817c478bd9Sstevel@tonic-gate 		if (mpvect[0] < 3 || mpvect[1] < 3)
22827c478bd9Sstevel@tonic-gate 		{
22837c478bd9Sstevel@tonic-gate 			syserr("%s... openmailer(%s): bogus mpvect %d %d",
22847c478bd9Sstevel@tonic-gate 			       shortenstring(e->e_to, MAXSHORTSTR), m->m_name,
22857c478bd9Sstevel@tonic-gate 			       mpvect[0], mpvect[1]);
22867c478bd9Sstevel@tonic-gate 			printopenfds(true);
22877c478bd9Sstevel@tonic-gate 			if (tTd(11, 1))
22887c478bd9Sstevel@tonic-gate 				sm_dprintf("openmailer: NULL\n");
22897c478bd9Sstevel@tonic-gate 			rcode = EX_OSERR;
22907c478bd9Sstevel@tonic-gate 			goto give_up;
22917c478bd9Sstevel@tonic-gate 		}
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 		/* make sure system call isn't dead meat */
22947c478bd9Sstevel@tonic-gate 		checkfdopen(mpvect[0], "mpvect[0]");
22957c478bd9Sstevel@tonic-gate 		checkfdopen(mpvect[1], "mpvect[1]");
22967c478bd9Sstevel@tonic-gate 		if (mpvect[0] == mpvect[1] ||
22977c478bd9Sstevel@tonic-gate 		    (e->e_lockfp != NULL &&
22987c478bd9Sstevel@tonic-gate 		     (mpvect[0] == sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD,
22997c478bd9Sstevel@tonic-gate 						 NULL) ||
23007c478bd9Sstevel@tonic-gate 		      mpvect[1] == sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD,
23017c478bd9Sstevel@tonic-gate 						 NULL))))
23027c478bd9Sstevel@tonic-gate 		{
23037c478bd9Sstevel@tonic-gate 			if (e->e_lockfp == NULL)
23047c478bd9Sstevel@tonic-gate 				syserr("%s... openmailer(%s): overlapping mpvect %d %d",
23057c478bd9Sstevel@tonic-gate 				       shortenstring(e->e_to, MAXSHORTSTR),
23067c478bd9Sstevel@tonic-gate 				       m->m_name, mpvect[0], mpvect[1]);
23077c478bd9Sstevel@tonic-gate 			else
23087c478bd9Sstevel@tonic-gate 				syserr("%s... openmailer(%s): overlapping mpvect %d %d, lockfp = %d",
23097c478bd9Sstevel@tonic-gate 				       shortenstring(e->e_to, MAXSHORTSTR),
23107c478bd9Sstevel@tonic-gate 				       m->m_name, mpvect[0], mpvect[1],
23117c478bd9Sstevel@tonic-gate 				       sm_io_getinfo(e->e_lockfp,
23127c478bd9Sstevel@tonic-gate 						     SM_IO_WHAT_FD, NULL));
23137c478bd9Sstevel@tonic-gate 		}
23147c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 		/* create a return pipe */
23177c478bd9Sstevel@tonic-gate 		if (pipe(rpvect) < 0)
23187c478bd9Sstevel@tonic-gate 		{
23197c478bd9Sstevel@tonic-gate 			syserr("%s... openmailer(%s): pipe (from mailer)",
23207c478bd9Sstevel@tonic-gate 			       shortenstring(e->e_to, MAXSHORTSTR),
23217c478bd9Sstevel@tonic-gate 			       m->m_name);
23227c478bd9Sstevel@tonic-gate 			(void) close(mpvect[0]);
23237c478bd9Sstevel@tonic-gate 			(void) close(mpvect[1]);
23247c478bd9Sstevel@tonic-gate 			if (tTd(11, 1))
23257c478bd9Sstevel@tonic-gate 				sm_dprintf("openmailer: NULL\n");
23267c478bd9Sstevel@tonic-gate 			rcode = EX_OSERR;
23277c478bd9Sstevel@tonic-gate 			goto give_up;
23287c478bd9Sstevel@tonic-gate 		}
23297c478bd9Sstevel@tonic-gate #if XDEBUG
23307c478bd9Sstevel@tonic-gate 		checkfdopen(rpvect[0], "rpvect[0]");
23317c478bd9Sstevel@tonic-gate 		checkfdopen(rpvect[1], "rpvect[1]");
23327c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 		/*
23357c478bd9Sstevel@tonic-gate 		**  Actually fork the mailer process.
23367c478bd9Sstevel@tonic-gate 		**	DOFORK is clever about retrying.
23377c478bd9Sstevel@tonic-gate 		**
23387c478bd9Sstevel@tonic-gate 		**	Dispose of SIGCHLD signal catchers that may be laying
23397c478bd9Sstevel@tonic-gate 		**	around so that endmailer will get it.
23407c478bd9Sstevel@tonic-gate 		*/
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 		if (e->e_xfp != NULL)	/* for debugging */
23437c478bd9Sstevel@tonic-gate 			(void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
23447c478bd9Sstevel@tonic-gate 		(void) sm_io_flush(smioout, SM_TIME_DEFAULT);
23457c478bd9Sstevel@tonic-gate 		(void) sm_signal(SIGCHLD, SIG_DFL);
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 		DOFORK(FORK);
23497c478bd9Sstevel@tonic-gate 		/* pid is set by DOFORK */
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 		if (pid < 0)
23527c478bd9Sstevel@tonic-gate 		{
23537c478bd9Sstevel@tonic-gate 			/* failure */
23547c478bd9Sstevel@tonic-gate 			syserr("%s... openmailer(%s): cannot fork",
23557c478bd9Sstevel@tonic-gate 			       shortenstring(e->e_to, MAXSHORTSTR), m->m_name);
23567c478bd9Sstevel@tonic-gate 			(void) close(mpvect[0]);
23577c478bd9Sstevel@tonic-gate 			(void) close(mpvect[1]);
23587c478bd9Sstevel@tonic-gate 			(void) close(rpvect[0]);
23597c478bd9Sstevel@tonic-gate 			(void) close(rpvect[1]);
23607c478bd9Sstevel@tonic-gate 			if (tTd(11, 1))
23617c478bd9Sstevel@tonic-gate 				sm_dprintf("openmailer: NULL\n");
23627c478bd9Sstevel@tonic-gate 			rcode = EX_OSERR;
23637c478bd9Sstevel@tonic-gate 			goto give_up;
23647c478bd9Sstevel@tonic-gate 		}
23657c478bd9Sstevel@tonic-gate 		else if (pid == 0)
23667c478bd9Sstevel@tonic-gate 		{
23677c478bd9Sstevel@tonic-gate 			int save_errno;
23687c478bd9Sstevel@tonic-gate 			int sff;
23697c478bd9Sstevel@tonic-gate 			int new_euid = NO_UID;
23707c478bd9Sstevel@tonic-gate 			int new_ruid = NO_UID;
23717c478bd9Sstevel@tonic-gate 			int new_gid = NO_GID;
23727c478bd9Sstevel@tonic-gate 			char *user = NULL;
23737c478bd9Sstevel@tonic-gate 			struct stat stb;
23747c478bd9Sstevel@tonic-gate 			extern int DtableSize;
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 			CurrentPid = getpid();
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 			/* clear the events to turn off SIGALRMs */
23797c478bd9Sstevel@tonic-gate 			sm_clear_events();
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 			/* Reset global flags */
23827c478bd9Sstevel@tonic-gate 			RestartRequest = NULL;
23837c478bd9Sstevel@tonic-gate 			RestartWorkGroup = false;
23847c478bd9Sstevel@tonic-gate 			ShutdownRequest = NULL;
23857c478bd9Sstevel@tonic-gate 			PendingSignal = 0;
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 			if (e->e_lockfp != NULL)
23887c478bd9Sstevel@tonic-gate 				(void) close(sm_io_getinfo(e->e_lockfp,
23897c478bd9Sstevel@tonic-gate 							   SM_IO_WHAT_FD,
23907c478bd9Sstevel@tonic-gate 							   NULL));
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 			/* child -- set up input & exec mailer */
23937c478bd9Sstevel@tonic-gate 			(void) sm_signal(SIGALRM, sm_signal_noop);
23947c478bd9Sstevel@tonic-gate 			(void) sm_signal(SIGCHLD, SIG_DFL);
23957c478bd9Sstevel@tonic-gate 			(void) sm_signal(SIGHUP, SIG_IGN);
23967c478bd9Sstevel@tonic-gate 			(void) sm_signal(SIGINT, SIG_IGN);
23977c478bd9Sstevel@tonic-gate 			(void) sm_signal(SIGTERM, SIG_DFL);
23987c478bd9Sstevel@tonic-gate # ifdef SIGUSR1
23997c478bd9Sstevel@tonic-gate 			(void) sm_signal(SIGUSR1, sm_signal_noop);
24007c478bd9Sstevel@tonic-gate # endif /* SIGUSR1 */
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 			if (m != FileMailer || stat(tochain->q_user, &stb) < 0)
24037c478bd9Sstevel@tonic-gate 				stb.st_mode = 0;
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate # if HASSETUSERCONTEXT
24067c478bd9Sstevel@tonic-gate 			/*
24077c478bd9Sstevel@tonic-gate 			**  Set user resources.
24087c478bd9Sstevel@tonic-gate 			*/
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 			if (contextaddr != NULL)
24117c478bd9Sstevel@tonic-gate 			{
24127c478bd9Sstevel@tonic-gate 				int sucflags;
24137c478bd9Sstevel@tonic-gate 				struct passwd *pwd;
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 				if (contextaddr->q_ruser != NULL)
24167c478bd9Sstevel@tonic-gate 					pwd = sm_getpwnam(contextaddr->q_ruser);
24177c478bd9Sstevel@tonic-gate 				else
24187c478bd9Sstevel@tonic-gate 					pwd = sm_getpwnam(contextaddr->q_user);
24197c478bd9Sstevel@tonic-gate 				sucflags = LOGIN_SETRESOURCES|LOGIN_SETPRIORITY;
24207c478bd9Sstevel@tonic-gate #ifdef LOGIN_SETMAC
24217c478bd9Sstevel@tonic-gate 				sucflags |= LOGIN_SETMAC;
24227c478bd9Sstevel@tonic-gate #endif /* LOGIN_SETMAC */
24237c478bd9Sstevel@tonic-gate 				if (pwd != NULL &&
24247c478bd9Sstevel@tonic-gate 				    setusercontext(NULL, pwd, pwd->pw_uid,
24257c478bd9Sstevel@tonic-gate 						   sucflags) == -1 &&
24267c478bd9Sstevel@tonic-gate 				    suidwarn)
24277c478bd9Sstevel@tonic-gate 				{
24287c478bd9Sstevel@tonic-gate 					syserr("openmailer: setusercontext() failed");
24297c478bd9Sstevel@tonic-gate 					exit(EX_TEMPFAIL);
24307c478bd9Sstevel@tonic-gate 				}
24317c478bd9Sstevel@tonic-gate 			}
24327c478bd9Sstevel@tonic-gate # endif /* HASSETUSERCONTEXT */
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate #if HASNICE
24357c478bd9Sstevel@tonic-gate 			/* tweak niceness */
24367c478bd9Sstevel@tonic-gate 			if (m->m_nice != 0)
24377c478bd9Sstevel@tonic-gate 				(void) nice(m->m_nice);
24387c478bd9Sstevel@tonic-gate #endif /* HASNICE */
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 			/* reset group id */
24417c478bd9Sstevel@tonic-gate 			if (bitnset(M_SPECIFIC_UID, m->m_flags))
24427c478bd9Sstevel@tonic-gate 			{
24437c478bd9Sstevel@tonic-gate 				if (m->m_gid == NO_GID)
24447c478bd9Sstevel@tonic-gate 					new_gid = RunAsGid;
24457c478bd9Sstevel@tonic-gate 				else
24467c478bd9Sstevel@tonic-gate 					new_gid = m->m_gid;
24477c478bd9Sstevel@tonic-gate 			}
24487c478bd9Sstevel@tonic-gate 			else if (bitset(S_ISGID, stb.st_mode))
24497c478bd9Sstevel@tonic-gate 				new_gid = stb.st_gid;
24507c478bd9Sstevel@tonic-gate 			else if (ctladdr != NULL && ctladdr->q_gid != 0)
24517c478bd9Sstevel@tonic-gate 			{
24527c478bd9Sstevel@tonic-gate 				if (!DontInitGroups)
24537c478bd9Sstevel@tonic-gate 				{
24547c478bd9Sstevel@tonic-gate 					user = ctladdr->q_ruser;
24557c478bd9Sstevel@tonic-gate 					if (user == NULL)
24567c478bd9Sstevel@tonic-gate 						user = ctladdr->q_user;
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 					if (initgroups(user,
24597c478bd9Sstevel@tonic-gate 						       ctladdr->q_gid) == -1
24607c478bd9Sstevel@tonic-gate 					    && suidwarn)
24617c478bd9Sstevel@tonic-gate 					{
24627c478bd9Sstevel@tonic-gate 						syserr("openmailer: initgroups(%s, %d) failed",
24637c478bd9Sstevel@tonic-gate 							user, ctladdr->q_gid);
24647c478bd9Sstevel@tonic-gate 						exit(EX_TEMPFAIL);
24657c478bd9Sstevel@tonic-gate 					}
24667c478bd9Sstevel@tonic-gate 				}
24677c478bd9Sstevel@tonic-gate 				else
24687c478bd9Sstevel@tonic-gate 				{
24697c478bd9Sstevel@tonic-gate 					GIDSET_T gidset[1];
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate 					gidset[0] = ctladdr->q_gid;
24727c478bd9Sstevel@tonic-gate 					if (setgroups(1, gidset) == -1
24737c478bd9Sstevel@tonic-gate 					    && suidwarn)
24747c478bd9Sstevel@tonic-gate 					{
24757c478bd9Sstevel@tonic-gate 						syserr("openmailer: setgroups() failed");
24767c478bd9Sstevel@tonic-gate 						exit(EX_TEMPFAIL);
24777c478bd9Sstevel@tonic-gate 					}
24787c478bd9Sstevel@tonic-gate 				}
24797c478bd9Sstevel@tonic-gate 				new_gid = ctladdr->q_gid;
24807c478bd9Sstevel@tonic-gate 			}
24817c478bd9Sstevel@tonic-gate 			else
24827c478bd9Sstevel@tonic-gate 			{
24837c478bd9Sstevel@tonic-gate 				if (!DontInitGroups)
24847c478bd9Sstevel@tonic-gate 				{
24857c478bd9Sstevel@tonic-gate 					user = DefUser;
24867c478bd9Sstevel@tonic-gate 					if (initgroups(DefUser, DefGid) == -1 &&
24877c478bd9Sstevel@tonic-gate 					    suidwarn)
24887c478bd9Sstevel@tonic-gate 					{
24897c478bd9Sstevel@tonic-gate 						syserr("openmailer: initgroups(%s, %d) failed",
24907c478bd9Sstevel@tonic-gate 						       DefUser, DefGid);
24917c478bd9Sstevel@tonic-gate 						exit(EX_TEMPFAIL);
24927c478bd9Sstevel@tonic-gate 					}
24937c478bd9Sstevel@tonic-gate 				}
24947c478bd9Sstevel@tonic-gate 				else
24957c478bd9Sstevel@tonic-gate 				{
24967c478bd9Sstevel@tonic-gate 					GIDSET_T gidset[1];
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate 					gidset[0] = DefGid;
24997c478bd9Sstevel@tonic-gate 					if (setgroups(1, gidset) == -1
25007c478bd9Sstevel@tonic-gate 					    && suidwarn)
25017c478bd9Sstevel@tonic-gate 					{
25027c478bd9Sstevel@tonic-gate 						syserr("openmailer: setgroups() failed");
25037c478bd9Sstevel@tonic-gate 						exit(EX_TEMPFAIL);
25047c478bd9Sstevel@tonic-gate 					}
25057c478bd9Sstevel@tonic-gate 				}
25067c478bd9Sstevel@tonic-gate 				if (m->m_gid == NO_GID)
25077c478bd9Sstevel@tonic-gate 					new_gid = DefGid;
25087c478bd9Sstevel@tonic-gate 				else
25097c478bd9Sstevel@tonic-gate 					new_gid = m->m_gid;
25107c478bd9Sstevel@tonic-gate 			}
25117c478bd9Sstevel@tonic-gate 			if (new_gid != NO_GID)
25127c478bd9Sstevel@tonic-gate 			{
25137c478bd9Sstevel@tonic-gate 				if (RunAsUid != 0 &&
25147c478bd9Sstevel@tonic-gate 				    bitnset(M_SPECIFIC_UID, m->m_flags) &&
25157c478bd9Sstevel@tonic-gate 				    new_gid != getgid() &&
25167c478bd9Sstevel@tonic-gate 				    new_gid != getegid())
25177c478bd9Sstevel@tonic-gate 				{
25187c478bd9Sstevel@tonic-gate 					/* Only root can change the gid */
25197c478bd9Sstevel@tonic-gate 					syserr("openmailer: insufficient privileges to change gid, RunAsUid=%d, new_gid=%d, gid=%d, egid=%d",
25207c478bd9Sstevel@tonic-gate 					       (int) RunAsUid, (int) new_gid,
25217c478bd9Sstevel@tonic-gate 					       (int) getgid(), (int) getegid());
25227c478bd9Sstevel@tonic-gate 					exit(EX_TEMPFAIL);
25237c478bd9Sstevel@tonic-gate 				}
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 				if (setgid(new_gid) < 0 && suidwarn)
25267c478bd9Sstevel@tonic-gate 				{
25277c478bd9Sstevel@tonic-gate 					syserr("openmailer: setgid(%ld) failed",
25287c478bd9Sstevel@tonic-gate 					       (long) new_gid);
25297c478bd9Sstevel@tonic-gate 					exit(EX_TEMPFAIL);
25307c478bd9Sstevel@tonic-gate 				}
25317c478bd9Sstevel@tonic-gate 			}
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 			/* change root to some "safe" directory */
25347c478bd9Sstevel@tonic-gate 			if (m->m_rootdir != NULL)
25357c478bd9Sstevel@tonic-gate 			{
2536058561cbSjbeck 				expand(m->m_rootdir, cbuf, sizeof(cbuf), e);
25377c478bd9Sstevel@tonic-gate 				if (tTd(11, 20))
25387c478bd9Sstevel@tonic-gate 					sm_dprintf("openmailer: chroot %s\n",
25397c478bd9Sstevel@tonic-gate 						   cbuf);
25407c478bd9Sstevel@tonic-gate 				if (chroot(cbuf) < 0)
25417c478bd9Sstevel@tonic-gate 				{
25427c478bd9Sstevel@tonic-gate 					syserr("openmailer: Cannot chroot(%s)",
25437c478bd9Sstevel@tonic-gate 					       cbuf);
25447c478bd9Sstevel@tonic-gate 					exit(EX_TEMPFAIL);
25457c478bd9Sstevel@tonic-gate 				}
25467c478bd9Sstevel@tonic-gate 				if (chdir("/") < 0)
25477c478bd9Sstevel@tonic-gate 				{
25487c478bd9Sstevel@tonic-gate 					syserr("openmailer: cannot chdir(/)");
25497c478bd9Sstevel@tonic-gate 					exit(EX_TEMPFAIL);
25507c478bd9Sstevel@tonic-gate 				}
25517c478bd9Sstevel@tonic-gate 			}
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 			/* reset user id */
25547c478bd9Sstevel@tonic-gate 			endpwent();
25557c478bd9Sstevel@tonic-gate 			sm_mbdb_terminate();
25567c478bd9Sstevel@tonic-gate 			if (bitnset(M_SPECIFIC_UID, m->m_flags))
25577c478bd9Sstevel@tonic-gate 			{
25587c478bd9Sstevel@tonic-gate 				if (m->m_uid == NO_UID)
25597c478bd9Sstevel@tonic-gate 					new_euid = RunAsUid;
25607c478bd9Sstevel@tonic-gate 				else
25617c478bd9Sstevel@tonic-gate 					new_euid = m->m_uid;
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 				/*
25647c478bd9Sstevel@tonic-gate 				**  Undo the effects of the uid change in main
25657c478bd9Sstevel@tonic-gate 				**  for signal handling.  The real uid may
25667c478bd9Sstevel@tonic-gate 				**  be used by mailer in adding a "From "
25677c478bd9Sstevel@tonic-gate 				**  line.
25687c478bd9Sstevel@tonic-gate 				*/
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 				if (RealUid != 0 && RealUid != getuid())
25717c478bd9Sstevel@tonic-gate 				{
25727c478bd9Sstevel@tonic-gate # if MAILER_SETUID_METHOD == USE_SETEUID
25737c478bd9Sstevel@tonic-gate #  if HASSETREUID
25747c478bd9Sstevel@tonic-gate 					if (setreuid(RealUid, geteuid()) < 0)
25757c478bd9Sstevel@tonic-gate 					{
25767c478bd9Sstevel@tonic-gate 						syserr("openmailer: setreuid(%d, %d) failed",
25777c478bd9Sstevel@tonic-gate 						       (int) RealUid, (int) geteuid());
25787c478bd9Sstevel@tonic-gate 						exit(EX_OSERR);
25797c478bd9Sstevel@tonic-gate 					}
25807c478bd9Sstevel@tonic-gate #  endif /* HASSETREUID */
25817c478bd9Sstevel@tonic-gate # endif /* MAILER_SETUID_METHOD == USE_SETEUID */
25827c478bd9Sstevel@tonic-gate # if MAILER_SETUID_METHOD == USE_SETREUID
25837c478bd9Sstevel@tonic-gate 					new_ruid = RealUid;
25847c478bd9Sstevel@tonic-gate # endif /* MAILER_SETUID_METHOD == USE_SETREUID */
25857c478bd9Sstevel@tonic-gate 				}
25867c478bd9Sstevel@tonic-gate 			}
25877c478bd9Sstevel@tonic-gate 			else if (bitset(S_ISUID, stb.st_mode))
25887c478bd9Sstevel@tonic-gate 				new_ruid = stb.st_uid;
25897c478bd9Sstevel@tonic-gate 			else if (ctladdr != NULL && ctladdr->q_uid != 0)
25907c478bd9Sstevel@tonic-gate 				new_ruid = ctladdr->q_uid;
25917c478bd9Sstevel@tonic-gate 			else if (m->m_uid != NO_UID)
25927c478bd9Sstevel@tonic-gate 				new_ruid = m->m_uid;
25937c478bd9Sstevel@tonic-gate 			else
25947c478bd9Sstevel@tonic-gate 				new_ruid = DefUid;
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate # if _FFR_USE_SETLOGIN
25977c478bd9Sstevel@tonic-gate 			/* run disconnected from terminal and set login name */
25987c478bd9Sstevel@tonic-gate 			if (setsid() >= 0 &&
25997c478bd9Sstevel@tonic-gate 			    ctladdr != NULL && ctladdr->q_uid != 0 &&
26007c478bd9Sstevel@tonic-gate 			    new_euid == ctladdr->q_uid)
26017c478bd9Sstevel@tonic-gate 			{
26027c478bd9Sstevel@tonic-gate 				struct passwd *pwd;
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate 				pwd = sm_getpwuid(ctladdr->q_uid);
26057c478bd9Sstevel@tonic-gate 				if (pwd != NULL && suidwarn)
26067c478bd9Sstevel@tonic-gate 					(void) setlogin(pwd->pw_name);
26077c478bd9Sstevel@tonic-gate 				endpwent();
26087c478bd9Sstevel@tonic-gate 			}
26097c478bd9Sstevel@tonic-gate # endif /* _FFR_USE_SETLOGIN */
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 			if (new_euid != NO_UID)
26127c478bd9Sstevel@tonic-gate 			{
26137c478bd9Sstevel@tonic-gate 				if (RunAsUid != 0 && new_euid != RunAsUid)
26147c478bd9Sstevel@tonic-gate 				{
26157c478bd9Sstevel@tonic-gate 					/* Only root can change the uid */
26167c478bd9Sstevel@tonic-gate 					syserr("openmailer: insufficient privileges to change uid, new_euid=%d, RunAsUid=%d",
26177c478bd9Sstevel@tonic-gate 					       (int) new_euid, (int) RunAsUid);
26187c478bd9Sstevel@tonic-gate 					exit(EX_TEMPFAIL);
26197c478bd9Sstevel@tonic-gate 				}
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 				vendor_set_uid(new_euid);
26227c478bd9Sstevel@tonic-gate # if MAILER_SETUID_METHOD == USE_SETEUID
26237c478bd9Sstevel@tonic-gate 				if (seteuid(new_euid) < 0 && suidwarn)
26247c478bd9Sstevel@tonic-gate 				{
26257c478bd9Sstevel@tonic-gate 					syserr("openmailer: seteuid(%ld) failed",
26267c478bd9Sstevel@tonic-gate 					       (long) new_euid);
26277c478bd9Sstevel@tonic-gate 					exit(EX_TEMPFAIL);
26287c478bd9Sstevel@tonic-gate 				}
26297c478bd9Sstevel@tonic-gate # endif /* MAILER_SETUID_METHOD == USE_SETEUID */
26307c478bd9Sstevel@tonic-gate # if MAILER_SETUID_METHOD == USE_SETREUID
26317c478bd9Sstevel@tonic-gate 				if (setreuid(new_ruid, new_euid) < 0 && suidwarn)
26327c478bd9Sstevel@tonic-gate 				{
26337c478bd9Sstevel@tonic-gate 					syserr("openmailer: setreuid(%ld, %ld) failed",
26347c478bd9Sstevel@tonic-gate 					       (long) new_ruid, (long) new_euid);
26357c478bd9Sstevel@tonic-gate 					exit(EX_TEMPFAIL);
26367c478bd9Sstevel@tonic-gate 				}
26377c478bd9Sstevel@tonic-gate # endif /* MAILER_SETUID_METHOD == USE_SETREUID */
26387c478bd9Sstevel@tonic-gate # if MAILER_SETUID_METHOD == USE_SETUID
26397c478bd9Sstevel@tonic-gate 				if (new_euid != geteuid() && setuid(new_euid) < 0 && suidwarn)
26407c478bd9Sstevel@tonic-gate 				{
26417c478bd9Sstevel@tonic-gate 					syserr("openmailer: setuid(%ld) failed",
26427c478bd9Sstevel@tonic-gate 					       (long) new_euid);
26437c478bd9Sstevel@tonic-gate 					exit(EX_TEMPFAIL);
26447c478bd9Sstevel@tonic-gate 				}
26457c478bd9Sstevel@tonic-gate # endif /* MAILER_SETUID_METHOD == USE_SETUID */
26467c478bd9Sstevel@tonic-gate 			}
26477c478bd9Sstevel@tonic-gate 			else if (new_ruid != NO_UID)
26487c478bd9Sstevel@tonic-gate 			{
26497c478bd9Sstevel@tonic-gate 				vendor_set_uid(new_ruid);
26507c478bd9Sstevel@tonic-gate 				if (setuid(new_ruid) < 0 && suidwarn)
26517c478bd9Sstevel@tonic-gate 				{
26527c478bd9Sstevel@tonic-gate 					syserr("openmailer: setuid(%ld) failed",
26537c478bd9Sstevel@tonic-gate 					       (long) new_ruid);
26547c478bd9Sstevel@tonic-gate 					exit(EX_TEMPFAIL);
26557c478bd9Sstevel@tonic-gate 				}
26567c478bd9Sstevel@tonic-gate 			}
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 			if (tTd(11, 2))
26597c478bd9Sstevel@tonic-gate 				sm_dprintf("openmailer: running as r/euid=%d/%d, r/egid=%d/%d\n",
26607c478bd9Sstevel@tonic-gate 					   (int) getuid(), (int) geteuid(),
26617c478bd9Sstevel@tonic-gate 					   (int) getgid(), (int) getegid());
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 			/* move into some "safe" directory */
26647c478bd9Sstevel@tonic-gate 			if (m->m_execdir != NULL)
26657c478bd9Sstevel@tonic-gate 			{
26667c478bd9Sstevel@tonic-gate 				char *q;
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 				for (p = m->m_execdir; p != NULL; p = q)
26697c478bd9Sstevel@tonic-gate 				{
26707c478bd9Sstevel@tonic-gate 					q = strchr(p, ':');
26717c478bd9Sstevel@tonic-gate 					if (q != NULL)
26727c478bd9Sstevel@tonic-gate 						*q = '\0';
2673058561cbSjbeck 					expand(p, cbuf, sizeof(cbuf), e);
26747c478bd9Sstevel@tonic-gate 					if (q != NULL)
26757c478bd9Sstevel@tonic-gate 						*q++ = ':';
26767c478bd9Sstevel@tonic-gate 					if (tTd(11, 20))
26777c478bd9Sstevel@tonic-gate 						sm_dprintf("openmailer: trydir %s\n",
26787c478bd9Sstevel@tonic-gate 							   cbuf);
26797c478bd9Sstevel@tonic-gate 					if (cbuf[0] != '\0' &&
26807c478bd9Sstevel@tonic-gate 					    chdir(cbuf) >= 0)
26817c478bd9Sstevel@tonic-gate 						break;
26827c478bd9Sstevel@tonic-gate 				}
26837c478bd9Sstevel@tonic-gate 			}
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 			/* Check safety of program to be run */
26867c478bd9Sstevel@tonic-gate 			sff = SFF_ROOTOK|SFF_EXECOK;
26877c478bd9Sstevel@tonic-gate 			if (!bitnset(DBS_RUNWRITABLEPROGRAM,
26887c478bd9Sstevel@tonic-gate 				     DontBlameSendmail))
26897c478bd9Sstevel@tonic-gate 				sff |= SFF_NOGWFILES|SFF_NOWWFILES;
26907c478bd9Sstevel@tonic-gate 			if (bitnset(DBS_RUNPROGRAMINUNSAFEDIRPATH,
26917c478bd9Sstevel@tonic-gate 				    DontBlameSendmail))
26927c478bd9Sstevel@tonic-gate 				sff |= SFF_NOPATHCHECK;
26937c478bd9Sstevel@tonic-gate 			else
26947c478bd9Sstevel@tonic-gate 				sff |= SFF_SAFEDIRPATH;
26957c478bd9Sstevel@tonic-gate 			ret = safefile(m->m_mailer, getuid(), getgid(),
26967c478bd9Sstevel@tonic-gate 				       user, sff, 0, NULL);
26977c478bd9Sstevel@tonic-gate 			if (ret != 0)
26987c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_INFO, e->e_id,
26997c478bd9Sstevel@tonic-gate 					  "Warning: program %s unsafe: %s",
27007c478bd9Sstevel@tonic-gate 					  m->m_mailer, sm_errstring(ret));
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 			/* arrange to filter std & diag output of command */
27037c478bd9Sstevel@tonic-gate 			(void) close(rpvect[0]);
27047c478bd9Sstevel@tonic-gate 			if (dup2(rpvect[1], STDOUT_FILENO) < 0)
27057c478bd9Sstevel@tonic-gate 			{
27067c478bd9Sstevel@tonic-gate 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
27077c478bd9Sstevel@tonic-gate 				       shortenstring(e->e_to, MAXSHORTSTR),
27087c478bd9Sstevel@tonic-gate 				       m->m_name, rpvect[1]);
27097c478bd9Sstevel@tonic-gate 				_exit(EX_OSERR);
27107c478bd9Sstevel@tonic-gate 			}
27117c478bd9Sstevel@tonic-gate 			(void) close(rpvect[1]);
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 			if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
27147c478bd9Sstevel@tonic-gate 			{
27157c478bd9Sstevel@tonic-gate 				syserr("%s... openmailer(%s): cannot dup stdout for stderr",
27167c478bd9Sstevel@tonic-gate 				       shortenstring(e->e_to, MAXSHORTSTR),
27177c478bd9Sstevel@tonic-gate 				       m->m_name);
27187c478bd9Sstevel@tonic-gate 				_exit(EX_OSERR);
27197c478bd9Sstevel@tonic-gate 			}
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate 			/* arrange to get standard input */
27227c478bd9Sstevel@tonic-gate 			(void) close(mpvect[1]);
27237c478bd9Sstevel@tonic-gate 			if (dup2(mpvect[0], STDIN_FILENO) < 0)
27247c478bd9Sstevel@tonic-gate 			{
27257c478bd9Sstevel@tonic-gate 				syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
27267c478bd9Sstevel@tonic-gate 				       shortenstring(e->e_to, MAXSHORTSTR),
27277c478bd9Sstevel@tonic-gate 				       m->m_name, mpvect[0]);
27287c478bd9Sstevel@tonic-gate 				_exit(EX_OSERR);
27297c478bd9Sstevel@tonic-gate 			}
27307c478bd9Sstevel@tonic-gate 			(void) close(mpvect[0]);
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 			/* arrange for all the files to be closed */
27337c478bd9Sstevel@tonic-gate 			sm_close_on_exec(STDERR_FILENO + 1, DtableSize);
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate # if !_FFR_USE_SETLOGIN
27367c478bd9Sstevel@tonic-gate 			/* run disconnected from terminal */
27377c478bd9Sstevel@tonic-gate 			(void) setsid();
27387c478bd9Sstevel@tonic-gate # endif /* !_FFR_USE_SETLOGIN */
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate 			/* try to execute the mailer */
27417c478bd9Sstevel@tonic-gate 			(void) execve(m->m_mailer, (ARGV_T) pv,
27427c478bd9Sstevel@tonic-gate 				      (ARGV_T) UserEnviron);
27437c478bd9Sstevel@tonic-gate 			save_errno = errno;
27447c478bd9Sstevel@tonic-gate 			syserr("Cannot exec %s", m->m_mailer);
27457c478bd9Sstevel@tonic-gate 			if (bitnset(M_LOCALMAILER, m->m_flags) ||
27467c478bd9Sstevel@tonic-gate 			    transienterror(save_errno))
27477c478bd9Sstevel@tonic-gate 				_exit(EX_OSERR);
27487c478bd9Sstevel@tonic-gate 			_exit(EX_UNAVAILABLE);
27497c478bd9Sstevel@tonic-gate 		}
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate 		/*
27527c478bd9Sstevel@tonic-gate 		**  Set up return value.
27537c478bd9Sstevel@tonic-gate 		*/
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate 		if (mci == NULL)
27567c478bd9Sstevel@tonic-gate 		{
27577c478bd9Sstevel@tonic-gate 			if (clever)
27587c478bd9Sstevel@tonic-gate 			{
27597c478bd9Sstevel@tonic-gate 				/*
27607c478bd9Sstevel@tonic-gate 				**  Allocate from general heap, not
27617c478bd9Sstevel@tonic-gate 				**  envelope rpool, because this mci
27627c478bd9Sstevel@tonic-gate 				**  is going to be cached.
27637c478bd9Sstevel@tonic-gate 				*/
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 				mci = mci_new(NULL);
27667c478bd9Sstevel@tonic-gate 			}
27677c478bd9Sstevel@tonic-gate 			else
27687c478bd9Sstevel@tonic-gate 			{
27697c478bd9Sstevel@tonic-gate 				/*
27707c478bd9Sstevel@tonic-gate 				**  Prevent a storage leak by allocating
27717c478bd9Sstevel@tonic-gate 				**  this from the envelope rpool.
27727c478bd9Sstevel@tonic-gate 				*/
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 				mci = mci_new(e->e_rpool);
27757c478bd9Sstevel@tonic-gate 			}
27767c478bd9Sstevel@tonic-gate 		}
27777c478bd9Sstevel@tonic-gate 		mci->mci_mailer = m;
27787c478bd9Sstevel@tonic-gate 		if (clever)
27797c478bd9Sstevel@tonic-gate 		{
27807c478bd9Sstevel@tonic-gate 			mci->mci_state = MCIS_OPENING;
27817c478bd9Sstevel@tonic-gate 			mci_cache(mci);
27827c478bd9Sstevel@tonic-gate 		}
27837c478bd9Sstevel@tonic-gate 		else
27847c478bd9Sstevel@tonic-gate 		{
27857c478bd9Sstevel@tonic-gate 			mci->mci_state = MCIS_OPEN;
27867c478bd9Sstevel@tonic-gate 		}
27877c478bd9Sstevel@tonic-gate 		mci->mci_pid = pid;
27887c478bd9Sstevel@tonic-gate 		(void) close(mpvect[0]);
27897c478bd9Sstevel@tonic-gate 		mci->mci_out = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
27907c478bd9Sstevel@tonic-gate 					  (void *) &(mpvect[1]), SM_IO_WRONLY_B,
27917c478bd9Sstevel@tonic-gate 					  NULL);
27927c478bd9Sstevel@tonic-gate 		if (mci->mci_out == NULL)
27937c478bd9Sstevel@tonic-gate 		{
27947c478bd9Sstevel@tonic-gate 			syserr("deliver: cannot create mailer output channel, fd=%d",
27957c478bd9Sstevel@tonic-gate 			       mpvect[1]);
27967c478bd9Sstevel@tonic-gate 			(void) close(mpvect[1]);
27977c478bd9Sstevel@tonic-gate 			(void) close(rpvect[0]);
27987c478bd9Sstevel@tonic-gate 			(void) close(rpvect[1]);
27997c478bd9Sstevel@tonic-gate 			rcode = EX_OSERR;
28007c478bd9Sstevel@tonic-gate 			goto give_up;
28017c478bd9Sstevel@tonic-gate 		}
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 		(void) close(rpvect[1]);
28047c478bd9Sstevel@tonic-gate 		mci->mci_in = sm_io_open(SmFtStdiofd, SM_TIME_DEFAULT,
28057c478bd9Sstevel@tonic-gate 					 (void *) &(rpvect[0]), SM_IO_RDONLY_B,
28067c478bd9Sstevel@tonic-gate 					 NULL);
28077c478bd9Sstevel@tonic-gate 		if (mci->mci_in == NULL)
28087c478bd9Sstevel@tonic-gate 		{
28097c478bd9Sstevel@tonic-gate 			syserr("deliver: cannot create mailer input channel, fd=%d",
28107c478bd9Sstevel@tonic-gate 			       mpvect[1]);
28117c478bd9Sstevel@tonic-gate 			(void) close(rpvect[0]);
28127c478bd9Sstevel@tonic-gate 			(void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
28137c478bd9Sstevel@tonic-gate 			mci->mci_out = NULL;
28147c478bd9Sstevel@tonic-gate 			rcode = EX_OSERR;
28157c478bd9Sstevel@tonic-gate 			goto give_up;
28167c478bd9Sstevel@tonic-gate 		}
28177c478bd9Sstevel@tonic-gate 	}
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	/*
28207c478bd9Sstevel@tonic-gate 	**  If we are in SMTP opening state, send initial protocol.
28217c478bd9Sstevel@tonic-gate 	*/
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	if (bitnset(M_7BITS, m->m_flags) &&
28247c478bd9Sstevel@tonic-gate 	    (!clever || mci->mci_state == MCIS_OPENING))
28257c478bd9Sstevel@tonic-gate 		mci->mci_flags |= MCIF_7BIT;
28267c478bd9Sstevel@tonic-gate 	if (clever && mci->mci_state != MCIS_CLOSED)
28277c478bd9Sstevel@tonic-gate 	{
28287c478bd9Sstevel@tonic-gate # if STARTTLS || SASL
28297c478bd9Sstevel@tonic-gate 		int dotpos;
28307c478bd9Sstevel@tonic-gate 		char *srvname;
28317c478bd9Sstevel@tonic-gate 		extern SOCKADDR CurHostAddr;
28327c478bd9Sstevel@tonic-gate # endif /* STARTTLS || SASL */
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate # if SASL
28357c478bd9Sstevel@tonic-gate #  define DONE_AUTH(f)		bitset(MCIF_AUTHACT, f)
28367c478bd9Sstevel@tonic-gate # endif /* SASL */
28377c478bd9Sstevel@tonic-gate # if STARTTLS
28387c478bd9Sstevel@tonic-gate #  define DONE_STARTTLS(f)	bitset(MCIF_TLSACT, f)
28397c478bd9Sstevel@tonic-gate # endif /* STARTTLS */
28407c478bd9Sstevel@tonic-gate # define ONLY_HELO(f)		bitset(MCIF_ONLY_EHLO, f)
28417c478bd9Sstevel@tonic-gate # define SET_HELO(f)		f |= MCIF_ONLY_EHLO
28427c478bd9Sstevel@tonic-gate # define CLR_HELO(f)		f &= ~MCIF_ONLY_EHLO
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate # if STARTTLS || SASL
28457c478bd9Sstevel@tonic-gate 		/* don't use CurHostName, it is changed in many places */
28467c478bd9Sstevel@tonic-gate 		if (mci->mci_host != NULL)
28477c478bd9Sstevel@tonic-gate 		{
28487c478bd9Sstevel@tonic-gate 			srvname = mci->mci_host;
28497c478bd9Sstevel@tonic-gate 			dotpos = strlen(srvname) - 1;
28507c478bd9Sstevel@tonic-gate 			if (dotpos >= 0)
28517c478bd9Sstevel@tonic-gate 			{
28527c478bd9Sstevel@tonic-gate 				if (srvname[dotpos] == '.')
28537c478bd9Sstevel@tonic-gate 					srvname[dotpos] = '\0';
28547c478bd9Sstevel@tonic-gate 				else
28557c478bd9Sstevel@tonic-gate 					dotpos = -1;
28567c478bd9Sstevel@tonic-gate 			}
28577c478bd9Sstevel@tonic-gate 		}
28587c478bd9Sstevel@tonic-gate 		else if (mci->mci_mailer != NULL)
28597c478bd9Sstevel@tonic-gate 		{
28607c478bd9Sstevel@tonic-gate 			srvname = mci->mci_mailer->m_name;
28617c478bd9Sstevel@tonic-gate 			dotpos = -1;
28627c478bd9Sstevel@tonic-gate 		}
28637c478bd9Sstevel@tonic-gate 		else
28647c478bd9Sstevel@tonic-gate 		{
28657c478bd9Sstevel@tonic-gate 			srvname = "local";
28667c478bd9Sstevel@tonic-gate 			dotpos = -1;
28677c478bd9Sstevel@tonic-gate 		}
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 		/* don't set {server_name} to NULL or "": see getauth() */
28707c478bd9Sstevel@tonic-gate 		macdefine(&mci->mci_macro, A_TEMP, macid("{server_name}"),
28717c478bd9Sstevel@tonic-gate 			  srvname);
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 		/* CurHostAddr is set by makeconnection() and mci_get() */
28747c478bd9Sstevel@tonic-gate 		if (CurHostAddr.sa.sa_family != 0)
28757c478bd9Sstevel@tonic-gate 		{
28767c478bd9Sstevel@tonic-gate 			macdefine(&mci->mci_macro, A_TEMP,
28777c478bd9Sstevel@tonic-gate 				  macid("{server_addr}"),
28787c478bd9Sstevel@tonic-gate 				  anynet_ntoa(&CurHostAddr));
28797c478bd9Sstevel@tonic-gate 		}
28807c478bd9Sstevel@tonic-gate 		else if (mci->mci_mailer != NULL)
28817c478bd9Sstevel@tonic-gate 		{
28827c478bd9Sstevel@tonic-gate 			/* mailer name is unique, use it as address */
28837c478bd9Sstevel@tonic-gate 			macdefine(&mci->mci_macro, A_PERM,
28847c478bd9Sstevel@tonic-gate 				  macid("{server_addr}"),
28857c478bd9Sstevel@tonic-gate 				  mci->mci_mailer->m_name);
28867c478bd9Sstevel@tonic-gate 		}
28877c478bd9Sstevel@tonic-gate 		else
28887c478bd9Sstevel@tonic-gate 		{
28897c478bd9Sstevel@tonic-gate 			/* don't set it to NULL or "": see getauth() */
28907c478bd9Sstevel@tonic-gate 			macdefine(&mci->mci_macro, A_PERM,
28917c478bd9Sstevel@tonic-gate 				  macid("{server_addr}"), "0");
28927c478bd9Sstevel@tonic-gate 		}
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 		/* undo change of srvname (mci->mci_host) */
28957c478bd9Sstevel@tonic-gate 		if (dotpos >= 0)
28967c478bd9Sstevel@tonic-gate 			srvname[dotpos] = '.';
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate reconnect:	/* after switching to an encrypted connection */
28997c478bd9Sstevel@tonic-gate # endif /* STARTTLS || SASL */
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 		/* set the current connection information */
29027c478bd9Sstevel@tonic-gate 		e->e_mci = mci;
29037c478bd9Sstevel@tonic-gate # if SASL
29047c478bd9Sstevel@tonic-gate 		mci->mci_saslcap = NULL;
29057c478bd9Sstevel@tonic-gate # endif /* SASL */
29067c478bd9Sstevel@tonic-gate 		smtpinit(m, mci, e, ONLY_HELO(mci->mci_flags));
29077c478bd9Sstevel@tonic-gate 		CLR_HELO(mci->mci_flags);
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 		if (IS_DLVR_RETURN(e))
29107c478bd9Sstevel@tonic-gate 		{
29117c478bd9Sstevel@tonic-gate 			/*
29127c478bd9Sstevel@tonic-gate 			**  Check whether other side can deliver e-mail
29137c478bd9Sstevel@tonic-gate 			**  fast enough
29147c478bd9Sstevel@tonic-gate 			*/
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 			if (!bitset(MCIF_DLVR_BY, mci->mci_flags))
29177c478bd9Sstevel@tonic-gate 			{
29187c478bd9Sstevel@tonic-gate 				e->e_status = "5.4.7";
29197c478bd9Sstevel@tonic-gate 				usrerrenh(e->e_status,
29207c478bd9Sstevel@tonic-gate 					  "554 Server does not support Deliver By");
29217c478bd9Sstevel@tonic-gate 				rcode = EX_UNAVAILABLE;
29227c478bd9Sstevel@tonic-gate 				goto give_up;
29237c478bd9Sstevel@tonic-gate 			}
29247c478bd9Sstevel@tonic-gate 			if (e->e_deliver_by > 0 &&
29257c478bd9Sstevel@tonic-gate 			    e->e_deliver_by - (curtime() - e->e_ctime) <
29267c478bd9Sstevel@tonic-gate 			    mci->mci_min_by)
29277c478bd9Sstevel@tonic-gate 			{
29287c478bd9Sstevel@tonic-gate 				e->e_status = "5.4.7";
29297c478bd9Sstevel@tonic-gate 				usrerrenh(e->e_status,
29307c478bd9Sstevel@tonic-gate 					  "554 Message can't be delivered in time; %ld < %ld",
29317c478bd9Sstevel@tonic-gate 					  e->e_deliver_by - (curtime() - e->e_ctime),
29327c478bd9Sstevel@tonic-gate 					  mci->mci_min_by);
29337c478bd9Sstevel@tonic-gate 				rcode = EX_UNAVAILABLE;
29347c478bd9Sstevel@tonic-gate 				goto give_up;
29357c478bd9Sstevel@tonic-gate 			}
29367c478bd9Sstevel@tonic-gate 		}
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate # if STARTTLS
29397c478bd9Sstevel@tonic-gate 		/* first TLS then AUTH to provide a security layer */
29407c478bd9Sstevel@tonic-gate 		if (mci->mci_state != MCIS_CLOSED &&
29417c478bd9Sstevel@tonic-gate 		    !DONE_STARTTLS(mci->mci_flags))
29427c478bd9Sstevel@tonic-gate 		{
29437c478bd9Sstevel@tonic-gate 			int olderrors;
29447c478bd9Sstevel@tonic-gate 			bool usetls;
29457c478bd9Sstevel@tonic-gate 			bool saveQuickAbort = QuickAbort;
29467c478bd9Sstevel@tonic-gate 			bool saveSuprErrs = SuprErrs;
29477c478bd9Sstevel@tonic-gate 			char *host = NULL;
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 			rcode = EX_OK;
29507c478bd9Sstevel@tonic-gate 			usetls = bitset(MCIF_TLS, mci->mci_flags);
29517c478bd9Sstevel@tonic-gate 			if (usetls)
29527c478bd9Sstevel@tonic-gate 				usetls = !iscltflgset(e, D_NOTLS);
29537c478bd9Sstevel@tonic-gate 
2954058561cbSjbeck 			host = macvalue(macid("{server_name}"), e);
29557c478bd9Sstevel@tonic-gate 			if (usetls)
29567c478bd9Sstevel@tonic-gate 			{
29577c478bd9Sstevel@tonic-gate 				olderrors = Errors;
29587c478bd9Sstevel@tonic-gate 				QuickAbort = false;
29597c478bd9Sstevel@tonic-gate 				SuprErrs = true;
29607c478bd9Sstevel@tonic-gate 				if (rscheck("try_tls", host, NULL, e,
2961058561cbSjbeck 					    RSF_RMCOMM, 7, host, NOQID, NULL)
2962058561cbSjbeck 								!= EX_OK
29637c478bd9Sstevel@tonic-gate 				    || Errors > olderrors)
2964058561cbSjbeck 				{
29657c478bd9Sstevel@tonic-gate 					usetls = false;
2966058561cbSjbeck 				}
29677c478bd9Sstevel@tonic-gate 				SuprErrs = saveSuprErrs;
29687c478bd9Sstevel@tonic-gate 				QuickAbort = saveQuickAbort;
29697c478bd9Sstevel@tonic-gate 			}
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 			if (usetls)
29727c478bd9Sstevel@tonic-gate 			{
29737c478bd9Sstevel@tonic-gate 				if ((rcode = starttls(m, mci, e)) == EX_OK)
29747c478bd9Sstevel@tonic-gate 				{
29757c478bd9Sstevel@tonic-gate 					/* start again without STARTTLS */
29767c478bd9Sstevel@tonic-gate 					mci->mci_flags |= MCIF_TLSACT;
29777c478bd9Sstevel@tonic-gate 				}
29787c478bd9Sstevel@tonic-gate 				else
29797c478bd9Sstevel@tonic-gate 				{
29807c478bd9Sstevel@tonic-gate 					char *s;
29817c478bd9Sstevel@tonic-gate 
29827c478bd9Sstevel@tonic-gate 					/*
29837c478bd9Sstevel@tonic-gate 					**  TLS negotation failed, what to do?
29847c478bd9Sstevel@tonic-gate 					**  fall back to unencrypted connection
29857c478bd9Sstevel@tonic-gate 					**  or abort? How to decide?
29867c478bd9Sstevel@tonic-gate 					**  set a macro and call a ruleset.
29877c478bd9Sstevel@tonic-gate 					*/
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 					mci->mci_flags &= ~MCIF_TLS;
29907c478bd9Sstevel@tonic-gate 					switch (rcode)
29917c478bd9Sstevel@tonic-gate 					{
29927c478bd9Sstevel@tonic-gate 					  case EX_TEMPFAIL:
29937c478bd9Sstevel@tonic-gate 						s = "TEMP";
29947c478bd9Sstevel@tonic-gate 						break;
29957c478bd9Sstevel@tonic-gate 					  case EX_USAGE:
29967c478bd9Sstevel@tonic-gate 						s = "USAGE";
29977c478bd9Sstevel@tonic-gate 						break;
29987c478bd9Sstevel@tonic-gate 					  case EX_PROTOCOL:
29997c478bd9Sstevel@tonic-gate 						s = "PROTOCOL";
30007c478bd9Sstevel@tonic-gate 						break;
30017c478bd9Sstevel@tonic-gate 					  case EX_SOFTWARE:
30027c478bd9Sstevel@tonic-gate 						s = "SOFTWARE";
30037c478bd9Sstevel@tonic-gate 						break;
3004445f2479Sjbeck 					  case EX_UNAVAILABLE:
3005445f2479Sjbeck 						s = "NONE";
3006445f2479Sjbeck 						break;
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 					  /* everything else is a failure */
30097c478bd9Sstevel@tonic-gate 					  default:
30107c478bd9Sstevel@tonic-gate 						s = "FAILURE";
30117c478bd9Sstevel@tonic-gate 						rcode = EX_TEMPFAIL;
30127c478bd9Sstevel@tonic-gate 					}
30137c478bd9Sstevel@tonic-gate 					macdefine(&e->e_macro, A_PERM,
30147c478bd9Sstevel@tonic-gate 						  macid("{verify}"), s);
30157c478bd9Sstevel@tonic-gate 				}
30167c478bd9Sstevel@tonic-gate 			}
30177c478bd9Sstevel@tonic-gate 			else
30187c478bd9Sstevel@tonic-gate 				macdefine(&e->e_macro, A_PERM,
30197c478bd9Sstevel@tonic-gate 					  macid("{verify}"), "NONE");
30207c478bd9Sstevel@tonic-gate 			olderrors = Errors;
30217c478bd9Sstevel@tonic-gate 			QuickAbort = false;
30227c478bd9Sstevel@tonic-gate 			SuprErrs = true;
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 			/*
30257c478bd9Sstevel@tonic-gate 			**  rcode == EX_SOFTWARE is special:
30267c478bd9Sstevel@tonic-gate 			**  the TLS negotation failed
30277c478bd9Sstevel@tonic-gate 			**  we have to drop the connection no matter what
30287c478bd9Sstevel@tonic-gate 			**  However, we call tls_server to give it the chance
30297c478bd9Sstevel@tonic-gate 			**  to log the problem and return an appropriate
30307c478bd9Sstevel@tonic-gate 			**  error code.
30317c478bd9Sstevel@tonic-gate 			*/
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate 			if (rscheck("tls_server",
30347c478bd9Sstevel@tonic-gate 				    macvalue(macid("{verify}"), e),
30357c478bd9Sstevel@tonic-gate 				    NULL, e, RSF_RMCOMM|RSF_COUNT, 5,
3036058561cbSjbeck 				    host, NOQID, NULL) != EX_OK ||
30377c478bd9Sstevel@tonic-gate 			    Errors > olderrors ||
30387c478bd9Sstevel@tonic-gate 			    rcode == EX_SOFTWARE)
30397c478bd9Sstevel@tonic-gate 			{
30407c478bd9Sstevel@tonic-gate 				char enhsc[ENHSCLEN];
30417c478bd9Sstevel@tonic-gate 				extern char MsgBuf[];
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate 				if (ISSMTPCODE(MsgBuf) &&
30447c478bd9Sstevel@tonic-gate 				    extenhsc(MsgBuf + 4, ' ', enhsc) > 0)
30457c478bd9Sstevel@tonic-gate 				{
30467c478bd9Sstevel@tonic-gate 					p = sm_rpool_strdup_x(e->e_rpool,
30477c478bd9Sstevel@tonic-gate 							      MsgBuf);
30487c478bd9Sstevel@tonic-gate 				}
30497c478bd9Sstevel@tonic-gate 				else
30507c478bd9Sstevel@tonic-gate 				{
30517c478bd9Sstevel@tonic-gate 					p = "403 4.7.0 server not authenticated.";
30527c478bd9Sstevel@tonic-gate 					(void) sm_strlcpy(enhsc, "4.7.0",
3053058561cbSjbeck 							  sizeof(enhsc));
30547c478bd9Sstevel@tonic-gate 				}
30557c478bd9Sstevel@tonic-gate 				SuprErrs = saveSuprErrs;
30567c478bd9Sstevel@tonic-gate 				QuickAbort = saveQuickAbort;
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 				if (rcode == EX_SOFTWARE)
30597c478bd9Sstevel@tonic-gate 				{
30607c478bd9Sstevel@tonic-gate 					/* drop the connection */
30617c478bd9Sstevel@tonic-gate 					mci->mci_state = MCIS_QUITING;
30627c478bd9Sstevel@tonic-gate 					if (mci->mci_in != NULL)
30637c478bd9Sstevel@tonic-gate 					{
30647c478bd9Sstevel@tonic-gate 						(void) sm_io_close(mci->mci_in,
30657c478bd9Sstevel@tonic-gate 								   SM_TIME_DEFAULT);
30667c478bd9Sstevel@tonic-gate 						mci->mci_in = NULL;
30677c478bd9Sstevel@tonic-gate 					}
30687c478bd9Sstevel@tonic-gate 					mci->mci_flags &= ~MCIF_TLSACT;
30697c478bd9Sstevel@tonic-gate 					(void) endmailer(mci, e, pv);
30707c478bd9Sstevel@tonic-gate 				}
30717c478bd9Sstevel@tonic-gate 				else
30727c478bd9Sstevel@tonic-gate 				{
30737c478bd9Sstevel@tonic-gate 					/* abort transfer */
30747c478bd9Sstevel@tonic-gate 					smtpquit(m, mci, e);
30757c478bd9Sstevel@tonic-gate 				}
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate 				/* avoid bogus error msg */
30787c478bd9Sstevel@tonic-gate 				mci->mci_errno = 0;
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate 				/* temp or permanent failure? */
30817c478bd9Sstevel@tonic-gate 				rcode = (*p == '4') ? EX_TEMPFAIL
30827c478bd9Sstevel@tonic-gate 						    : EX_UNAVAILABLE;
30837c478bd9Sstevel@tonic-gate 				mci_setstat(mci, rcode, enhsc, p);
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate 				/*
30867c478bd9Sstevel@tonic-gate 				**  hack to get the error message into
30877c478bd9Sstevel@tonic-gate 				**  the envelope (done in giveresponse())
30887c478bd9Sstevel@tonic-gate 				*/
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 				(void) sm_strlcpy(SmtpError, p,
3091058561cbSjbeck 						  sizeof(SmtpError));
30927c478bd9Sstevel@tonic-gate 			}
30934aac33d3Sjbeck 			else if (mci->mci_state == MCIS_CLOSED)
30944aac33d3Sjbeck 			{
30954aac33d3Sjbeck 				/* connection close caused by 421 */
30964aac33d3Sjbeck 				mci->mci_errno = 0;
30974aac33d3Sjbeck 				rcode = EX_TEMPFAIL;
30984aac33d3Sjbeck 				mci_setstat(mci, rcode, NULL, "421");
30994aac33d3Sjbeck 			}
31004aac33d3Sjbeck 			else
31014aac33d3Sjbeck 				rcode = 0;
31024aac33d3Sjbeck 
31037c478bd9Sstevel@tonic-gate 			QuickAbort = saveQuickAbort;
31047c478bd9Sstevel@tonic-gate 			SuprErrs = saveSuprErrs;
31057c478bd9Sstevel@tonic-gate 			if (DONE_STARTTLS(mci->mci_flags) &&
31067c478bd9Sstevel@tonic-gate 			    mci->mci_state != MCIS_CLOSED)
31077c478bd9Sstevel@tonic-gate 			{
31087c478bd9Sstevel@tonic-gate 				SET_HELO(mci->mci_flags);
31097c478bd9Sstevel@tonic-gate 				mci->mci_flags &= ~MCIF_EXTENS;
31107c478bd9Sstevel@tonic-gate 				goto reconnect;
31117c478bd9Sstevel@tonic-gate 			}
31127c478bd9Sstevel@tonic-gate 		}
31137c478bd9Sstevel@tonic-gate # endif /* STARTTLS */
31147c478bd9Sstevel@tonic-gate # if SASL
31157c478bd9Sstevel@tonic-gate 		/* if other server supports authentication let's authenticate */
31167c478bd9Sstevel@tonic-gate 		if (mci->mci_state != MCIS_CLOSED &&
31177c478bd9Sstevel@tonic-gate 		    mci->mci_saslcap != NULL &&
31187c478bd9Sstevel@tonic-gate 		    !DONE_AUTH(mci->mci_flags) && !iscltflgset(e, D_NOAUTH))
31197c478bd9Sstevel@tonic-gate 		{
31207c478bd9Sstevel@tonic-gate 			/* Should we require some minimum authentication? */
31217c478bd9Sstevel@tonic-gate 			if ((ret = smtpauth(m, mci, e)) == EX_OK)
31227c478bd9Sstevel@tonic-gate 			{
31237c478bd9Sstevel@tonic-gate 				int result;
31247c478bd9Sstevel@tonic-gate 				sasl_ssf_t *ssf = NULL;
31257c478bd9Sstevel@tonic-gate 
31267c478bd9Sstevel@tonic-gate 				/* Get security strength (features) */
31277c478bd9Sstevel@tonic-gate 				result = sasl_getprop(mci->mci_conn, SASL_SSF,
31287c478bd9Sstevel@tonic-gate # if SASL >= 20000
31297c478bd9Sstevel@tonic-gate 						      (const void **) &ssf);
31307c478bd9Sstevel@tonic-gate # else /* SASL >= 20000 */
31317c478bd9Sstevel@tonic-gate 						      (void **) &ssf);
31327c478bd9Sstevel@tonic-gate # endif /* SASL >= 20000 */
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate 				/* XXX authid? */
31357c478bd9Sstevel@tonic-gate 				if (LogLevel > 9)
31367c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, NOQID,
31377c478bd9Sstevel@tonic-gate 						  "AUTH=client, relay=%.100s, mech=%.16s, bits=%d",
31387c478bd9Sstevel@tonic-gate 						  mci->mci_host,
31397c478bd9Sstevel@tonic-gate 						  macvalue(macid("{auth_type}"), e),
31407c478bd9Sstevel@tonic-gate 						  result == SASL_OK ? *ssf : 0);
31417c478bd9Sstevel@tonic-gate 
31427c478bd9Sstevel@tonic-gate 				/*
31437c478bd9Sstevel@tonic-gate 				**  Only switch to encrypted connection
31447c478bd9Sstevel@tonic-gate 				**  if a security layer has been negotiated
31457c478bd9Sstevel@tonic-gate 				*/
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate 				if (result == SASL_OK && *ssf > 0)
31487c478bd9Sstevel@tonic-gate 				{
31493ee0e492Sjbeck 					int tmo;
31503ee0e492Sjbeck 
31517c478bd9Sstevel@tonic-gate 					/*
31527c478bd9Sstevel@tonic-gate 					**  Convert I/O layer to use SASL.
31537c478bd9Sstevel@tonic-gate 					**  If the call fails, the connection
31547c478bd9Sstevel@tonic-gate 					**  is aborted.
31557c478bd9Sstevel@tonic-gate 					*/
31567c478bd9Sstevel@tonic-gate 
31573ee0e492Sjbeck 					tmo = DATA_PROGRESS_TIMEOUT * 1000;
31587c478bd9Sstevel@tonic-gate 					if (sfdcsasl(&mci->mci_in,
31597c478bd9Sstevel@tonic-gate 						     &mci->mci_out,
31603ee0e492Sjbeck 						     mci->mci_conn, tmo) == 0)
31617c478bd9Sstevel@tonic-gate 					{
31627c478bd9Sstevel@tonic-gate 						mci->mci_flags &= ~MCIF_EXTENS;
31637c478bd9Sstevel@tonic-gate 						mci->mci_flags |= MCIF_AUTHACT|
31647c478bd9Sstevel@tonic-gate 								  MCIF_ONLY_EHLO;
31657c478bd9Sstevel@tonic-gate 						goto reconnect;
31667c478bd9Sstevel@tonic-gate 					}
31677c478bd9Sstevel@tonic-gate 					syserr("AUTH TLS switch failed in client");
31687c478bd9Sstevel@tonic-gate 				}
31697c478bd9Sstevel@tonic-gate 				/* else? XXX */
31707c478bd9Sstevel@tonic-gate 				mci->mci_flags |= MCIF_AUTHACT;
31717c478bd9Sstevel@tonic-gate 
31727c478bd9Sstevel@tonic-gate 			}
31737c478bd9Sstevel@tonic-gate 			else if (ret == EX_TEMPFAIL)
31747c478bd9Sstevel@tonic-gate 			{
31757c478bd9Sstevel@tonic-gate 				if (LogLevel > 8)
31767c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_ERR, NOQID,
31777c478bd9Sstevel@tonic-gate 						  "AUTH=client, relay=%.100s, temporary failure, connection abort",
31787c478bd9Sstevel@tonic-gate 						  mci->mci_host);
31797c478bd9Sstevel@tonic-gate 				smtpquit(m, mci, e);
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 				/* avoid bogus error msg */
31827c478bd9Sstevel@tonic-gate 				mci->mci_errno = 0;
31837c478bd9Sstevel@tonic-gate 				rcode = EX_TEMPFAIL;
31847c478bd9Sstevel@tonic-gate 				mci_setstat(mci, rcode, "4.3.0", p);
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 				/*
31877c478bd9Sstevel@tonic-gate 				**  hack to get the error message into
31887c478bd9Sstevel@tonic-gate 				**  the envelope (done in giveresponse())
31897c478bd9Sstevel@tonic-gate 				*/
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 				(void) sm_strlcpy(SmtpError,
31927c478bd9Sstevel@tonic-gate 						  "Temporary AUTH failure",
3193058561cbSjbeck 						  sizeof(SmtpError));
31947c478bd9Sstevel@tonic-gate 			}
31957c478bd9Sstevel@tonic-gate 		}
31967c478bd9Sstevel@tonic-gate # endif /* SASL */
31977c478bd9Sstevel@tonic-gate 	}
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 
32007c478bd9Sstevel@tonic-gate do_transfer:
32017c478bd9Sstevel@tonic-gate 	/* clear out per-message flags from connection structure */
32027c478bd9Sstevel@tonic-gate 	mci->mci_flags &= ~(MCIF_CVT7TO8|MCIF_CVT8TO7);
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 	if (bitset(EF_HAS8BIT, e->e_flags) &&
32057c478bd9Sstevel@tonic-gate 	    !bitset(EF_DONT_MIME, e->e_flags) &&
32067c478bd9Sstevel@tonic-gate 	    bitnset(M_7BITS, m->m_flags))
32077c478bd9Sstevel@tonic-gate 		mci->mci_flags |= MCIF_CVT8TO7;
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate #if MIME7TO8
32107c478bd9Sstevel@tonic-gate 	if (bitnset(M_MAKE8BIT, m->m_flags) &&
32117c478bd9Sstevel@tonic-gate 	    !bitset(MCIF_7BIT, mci->mci_flags) &&
32127c478bd9Sstevel@tonic-gate 	    (p = hvalue("Content-Transfer-Encoding", e->e_header)) != NULL &&
32137c478bd9Sstevel@tonic-gate 	     (sm_strcasecmp(p, "quoted-printable") == 0 ||
32147c478bd9Sstevel@tonic-gate 	      sm_strcasecmp(p, "base64") == 0) &&
32157c478bd9Sstevel@tonic-gate 	    (p = hvalue("Content-Type", e->e_header)) != NULL)
32167c478bd9Sstevel@tonic-gate 	{
32177c478bd9Sstevel@tonic-gate 		/* may want to convert 7 -> 8 */
32187c478bd9Sstevel@tonic-gate 		/* XXX should really parse it here -- and use a class XXX */
32197c478bd9Sstevel@tonic-gate 		if (sm_strncasecmp(p, "text/plain", 10) == 0 &&
32207c478bd9Sstevel@tonic-gate 		    (p[10] == '\0' || p[10] == ' ' || p[10] == ';'))
32217c478bd9Sstevel@tonic-gate 			mci->mci_flags |= MCIF_CVT7TO8;
32227c478bd9Sstevel@tonic-gate 	}
32237c478bd9Sstevel@tonic-gate #endif /* MIME7TO8 */
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate 	if (tTd(11, 1))
32267c478bd9Sstevel@tonic-gate 	{
32277c478bd9Sstevel@tonic-gate 		sm_dprintf("openmailer: ");
32287c478bd9Sstevel@tonic-gate 		mci_dump(sm_debug_file(), mci, false);
32297c478bd9Sstevel@tonic-gate 	}
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate #if _FFR_CLIENT_SIZE
32327c478bd9Sstevel@tonic-gate 	/*
32337c478bd9Sstevel@tonic-gate 	**  See if we know the maximum size and
32347c478bd9Sstevel@tonic-gate 	**  abort if the message is too big.
32357c478bd9Sstevel@tonic-gate 	**
32367c478bd9Sstevel@tonic-gate 	**  NOTE: _FFR_CLIENT_SIZE is untested.
32377c478bd9Sstevel@tonic-gate 	*/
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate 	if (bitset(MCIF_SIZE, mci->mci_flags) &&
32407c478bd9Sstevel@tonic-gate 	    mci->mci_maxsize > 0 &&
32417c478bd9Sstevel@tonic-gate 	    e->e_msgsize > mci->mci_maxsize)
32427c478bd9Sstevel@tonic-gate 	{
32437c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_NO_BODY_RETN;
32447c478bd9Sstevel@tonic-gate 		if (bitnset(M_LOCALMAILER, m->m_flags))
32457c478bd9Sstevel@tonic-gate 			e->e_status = "5.2.3";
32467c478bd9Sstevel@tonic-gate 		else
32477c478bd9Sstevel@tonic-gate 			e->e_status = "5.3.4";
32487c478bd9Sstevel@tonic-gate 
32497c478bd9Sstevel@tonic-gate 		usrerrenh(e->e_status,
32507c478bd9Sstevel@tonic-gate 			  "552 Message is too large; %ld bytes max",
32517c478bd9Sstevel@tonic-gate 			  mci->mci_maxsize);
32527c478bd9Sstevel@tonic-gate 		rcode = EX_DATAERR;
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 		/* Need an e_message for error */
3255058561cbSjbeck 		(void) sm_snprintf(SmtpError, sizeof(SmtpError),
32567c478bd9Sstevel@tonic-gate 				   "Message is too large; %ld bytes max",
32577c478bd9Sstevel@tonic-gate 				   mci->mci_maxsize);
32587c478bd9Sstevel@tonic-gate 		goto give_up;
32597c478bd9Sstevel@tonic-gate 	}
32607c478bd9Sstevel@tonic-gate #endif /* _FFR_CLIENT_SIZE */
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 	if (mci->mci_state != MCIS_OPEN)
32637c478bd9Sstevel@tonic-gate 	{
32647c478bd9Sstevel@tonic-gate 		/* couldn't open the mailer */
32657c478bd9Sstevel@tonic-gate 		rcode = mci->mci_exitstat;
32667c478bd9Sstevel@tonic-gate 		errno = mci->mci_errno;
32677c478bd9Sstevel@tonic-gate 		SM_SET_H_ERRNO(mci->mci_herrno);
32687c478bd9Sstevel@tonic-gate 		if (rcode == EX_OK)
32697c478bd9Sstevel@tonic-gate 		{
32707c478bd9Sstevel@tonic-gate 			/* shouldn't happen */
32717c478bd9Sstevel@tonic-gate 			syserr("554 5.3.5 deliver: mci=%lx rcode=%d errno=%d state=%d sig=%s",
32727c478bd9Sstevel@tonic-gate 			       (unsigned long) mci, rcode, errno,
32737c478bd9Sstevel@tonic-gate 			       mci->mci_state, firstsig);
32747c478bd9Sstevel@tonic-gate 			mci_dump_all(smioout, true);
32757c478bd9Sstevel@tonic-gate 			rcode = EX_SOFTWARE;
32767c478bd9Sstevel@tonic-gate 		}
32777c478bd9Sstevel@tonic-gate 		else if (nummxhosts > hostnum)
32787c478bd9Sstevel@tonic-gate 		{
32797c478bd9Sstevel@tonic-gate 			/* try next MX site */
32807c478bd9Sstevel@tonic-gate 			goto tryhost;
32817c478bd9Sstevel@tonic-gate 		}
32827c478bd9Sstevel@tonic-gate 	}
32837c478bd9Sstevel@tonic-gate 	else if (!clever)
32847c478bd9Sstevel@tonic-gate 	{
3285445f2479Sjbeck 		bool ok;
3286445f2479Sjbeck 
32877c478bd9Sstevel@tonic-gate 		/*
32887c478bd9Sstevel@tonic-gate 		**  Format and send message.
32897c478bd9Sstevel@tonic-gate 		*/
32907c478bd9Sstevel@tonic-gate 
3291445f2479Sjbeck 		rcode = EX_OK;
3292445f2479Sjbeck 		errno = 0;
3293445f2479Sjbeck 		ok = putfromline(mci, e);
3294445f2479Sjbeck 		if (ok)
3295445f2479Sjbeck 			ok = (*e->e_puthdr)(mci, e->e_header, e, M87F_OUTER);
3296445f2479Sjbeck 		if (ok)
3297445f2479Sjbeck 			ok = (*e->e_putbody)(mci, e, NULL);
3298*7800901eSjbeck 		if (ok && bitset(MCIF_INLONGLINE, mci->mci_flags))
3299*7800901eSjbeck 			ok = putline("", mci);
33007c478bd9Sstevel@tonic-gate 
3301445f2479Sjbeck 		/*
3302445f2479Sjbeck 		**  Ignore an I/O error that was caused by EPIPE.
3303445f2479Sjbeck 		**  Some broken mailers don't read the entire body
3304445f2479Sjbeck 		**  but just exit() thus causing an I/O error.
3305445f2479Sjbeck 		*/
3306445f2479Sjbeck 
3307445f2479Sjbeck 		if (!ok && (sm_io_error(mci->mci_out) && errno == EPIPE))
3308445f2479Sjbeck 			ok = true;
3309445f2479Sjbeck 
3310445f2479Sjbeck 		/* (always) get the exit status */
33117c478bd9Sstevel@tonic-gate 		rcode = endmailer(mci, e, pv);
3312445f2479Sjbeck 		if (!ok)
3313445f2479Sjbeck 			rcode = EX_TEMPFAIL;
33147c478bd9Sstevel@tonic-gate 		if (rcode == EX_TEMPFAIL && SmtpError[0] == '\0')
33157c478bd9Sstevel@tonic-gate 		{
33167c478bd9Sstevel@tonic-gate 			/*
33177c478bd9Sstevel@tonic-gate 			**  Need an e_message for mailq display.
33187c478bd9Sstevel@tonic-gate 			**  We set SmtpError as
33197c478bd9Sstevel@tonic-gate 			*/
33207c478bd9Sstevel@tonic-gate 
3321058561cbSjbeck 			(void) sm_snprintf(SmtpError, sizeof(SmtpError),
33227c478bd9Sstevel@tonic-gate 					   "%s mailer (%s) exited with EX_TEMPFAIL",
33237c478bd9Sstevel@tonic-gate 					   m->m_name, m->m_mailer);
33247c478bd9Sstevel@tonic-gate 		}
33257c478bd9Sstevel@tonic-gate 	}
33267c478bd9Sstevel@tonic-gate 	else
33277c478bd9Sstevel@tonic-gate 	{
33287c478bd9Sstevel@tonic-gate 		/*
33297c478bd9Sstevel@tonic-gate 		**  Send the MAIL FROM: protocol
33307c478bd9Sstevel@tonic-gate 		*/
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 		/* XXX this isn't pipelined... */
33337c478bd9Sstevel@tonic-gate 		rcode = smtpmailfrom(m, mci, e);
33347c478bd9Sstevel@tonic-gate 		if (rcode == EX_OK)
33357c478bd9Sstevel@tonic-gate 		{
33367c478bd9Sstevel@tonic-gate 			register int i;
33377c478bd9Sstevel@tonic-gate # if PIPELINING
33387c478bd9Sstevel@tonic-gate 			ADDRESS *volatile pchain;
33397c478bd9Sstevel@tonic-gate # endif /* PIPELINING */
33407c478bd9Sstevel@tonic-gate 
33417c478bd9Sstevel@tonic-gate 			/* send the recipient list */
33427c478bd9Sstevel@tonic-gate 			tobuf[0] = '\0';
33437c478bd9Sstevel@tonic-gate 			mci->mci_retryrcpt = false;
33447c478bd9Sstevel@tonic-gate 			mci->mci_tolist = tobuf;
33457c478bd9Sstevel@tonic-gate # if PIPELINING
33467c478bd9Sstevel@tonic-gate 			pchain = NULL;
33477c478bd9Sstevel@tonic-gate 			mci->mci_nextaddr = NULL;
33487c478bd9Sstevel@tonic-gate # endif /* PIPELINING */
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 			for (to = tochain; to != NULL; to = to->q_tchain)
33517c478bd9Sstevel@tonic-gate 			{
33527c478bd9Sstevel@tonic-gate 				if (!QS_IS_UNMARKED(to->q_state))
33537c478bd9Sstevel@tonic-gate 					continue;
33547c478bd9Sstevel@tonic-gate 
33557c478bd9Sstevel@tonic-gate 				/* mark recipient state as "ok so far" */
33567c478bd9Sstevel@tonic-gate 				to->q_state = QS_OK;
33577c478bd9Sstevel@tonic-gate 				e->e_to = to->q_paddr;
33587c478bd9Sstevel@tonic-gate # if STARTTLS
33597c478bd9Sstevel@tonic-gate 				i = rscheck("tls_rcpt", to->q_user, NULL, e,
33607c478bd9Sstevel@tonic-gate 					    RSF_RMCOMM|RSF_COUNT, 3,
3361058561cbSjbeck 					    mci->mci_host, e->e_id, NULL);
33627c478bd9Sstevel@tonic-gate 				if (i != EX_OK)
33637c478bd9Sstevel@tonic-gate 				{
33647c478bd9Sstevel@tonic-gate 					markfailure(e, to, mci, i, false);
33657c478bd9Sstevel@tonic-gate 					giveresponse(i, to->q_status,  m, mci,
33667c478bd9Sstevel@tonic-gate 						     ctladdr, xstart, e, to);
33677c478bd9Sstevel@tonic-gate 					if (i == EX_TEMPFAIL)
33687c478bd9Sstevel@tonic-gate 					{
33697c478bd9Sstevel@tonic-gate 						mci->mci_retryrcpt = true;
33707c478bd9Sstevel@tonic-gate 						to->q_state = QS_RETRY;
33717c478bd9Sstevel@tonic-gate 					}
33727c478bd9Sstevel@tonic-gate 					continue;
33737c478bd9Sstevel@tonic-gate 				}
33747c478bd9Sstevel@tonic-gate # endif /* STARTTLS */
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate 				i = smtprcpt(to, m, mci, e, ctladdr, xstart);
33777c478bd9Sstevel@tonic-gate # if PIPELINING
33787c478bd9Sstevel@tonic-gate 				if (i == EX_OK &&
33797c478bd9Sstevel@tonic-gate 				    bitset(MCIF_PIPELINED, mci->mci_flags))
33807c478bd9Sstevel@tonic-gate 				{
33817c478bd9Sstevel@tonic-gate 					/*
33827c478bd9Sstevel@tonic-gate 					**  Add new element to list of
33837c478bd9Sstevel@tonic-gate 					**  recipients for pipelining.
33847c478bd9Sstevel@tonic-gate 					*/
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 					to->q_pchain = NULL;
33877c478bd9Sstevel@tonic-gate 					if (mci->mci_nextaddr == NULL)
33887c478bd9Sstevel@tonic-gate 						mci->mci_nextaddr = to;
33897c478bd9Sstevel@tonic-gate 					if (pchain == NULL)
33907c478bd9Sstevel@tonic-gate 						pchain = to;
33917c478bd9Sstevel@tonic-gate 					else
33927c478bd9Sstevel@tonic-gate 					{
33937c478bd9Sstevel@tonic-gate 						pchain->q_pchain = to;
33947c478bd9Sstevel@tonic-gate 						pchain = pchain->q_pchain;
33957c478bd9Sstevel@tonic-gate 					}
33967c478bd9Sstevel@tonic-gate 				}
33977c478bd9Sstevel@tonic-gate # endif /* PIPELINING */
33987c478bd9Sstevel@tonic-gate 				if (i != EX_OK)
33997c478bd9Sstevel@tonic-gate 				{
34007c478bd9Sstevel@tonic-gate 					markfailure(e, to, mci, i, false);
34017c478bd9Sstevel@tonic-gate 					giveresponse(i, to->q_status, m, mci,
34027c478bd9Sstevel@tonic-gate 						     ctladdr, xstart, e, to);
34037c478bd9Sstevel@tonic-gate 					if (i == EX_TEMPFAIL)
34047c478bd9Sstevel@tonic-gate 						to->q_state = QS_RETRY;
34057c478bd9Sstevel@tonic-gate 				}
34067c478bd9Sstevel@tonic-gate 			}
34077c478bd9Sstevel@tonic-gate 
34087c478bd9Sstevel@tonic-gate 			/* No recipients in list and no missing responses? */
34097c478bd9Sstevel@tonic-gate 			if (tobuf[0] == '\0'
34107c478bd9Sstevel@tonic-gate # if PIPELINING
3411*7800901eSjbeck 			    && bitset(MCIF_PIPELINED, mci->mci_flags)
34127c478bd9Sstevel@tonic-gate 			    && mci->mci_nextaddr == NULL
34137c478bd9Sstevel@tonic-gate # endif /* PIPELINING */
34147c478bd9Sstevel@tonic-gate 			   )
34157c478bd9Sstevel@tonic-gate 			{
34167c478bd9Sstevel@tonic-gate 				rcode = EX_OK;
34177c478bd9Sstevel@tonic-gate 				e->e_to = NULL;
34187c478bd9Sstevel@tonic-gate 				if (bitset(MCIF_CACHED, mci->mci_flags))
34197c478bd9Sstevel@tonic-gate 					smtprset(m, mci, e);
34207c478bd9Sstevel@tonic-gate 			}
34217c478bd9Sstevel@tonic-gate 			else
34227c478bd9Sstevel@tonic-gate 			{
34237c478bd9Sstevel@tonic-gate 				e->e_to = tobuf + 1;
34247c478bd9Sstevel@tonic-gate 				rcode = smtpdata(m, mci, e, ctladdr, xstart);
34257c478bd9Sstevel@tonic-gate 			}
34267c478bd9Sstevel@tonic-gate 		}
34277c478bd9Sstevel@tonic-gate 		if (rcode == EX_TEMPFAIL && nummxhosts > hostnum)
34287c478bd9Sstevel@tonic-gate 		{
34297c478bd9Sstevel@tonic-gate 			/* try next MX site */
34307c478bd9Sstevel@tonic-gate 			goto tryhost;
34317c478bd9Sstevel@tonic-gate 		}
34327c478bd9Sstevel@tonic-gate 	}
34337c478bd9Sstevel@tonic-gate #if NAMED_BIND
34347c478bd9Sstevel@tonic-gate 	if (ConfigLevel < 2)
34357c478bd9Sstevel@tonic-gate 		_res.options |= RES_DEFNAMES | RES_DNSRCH;	/* XXX */
34367c478bd9Sstevel@tonic-gate #endif /* NAMED_BIND */
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 	if (tTd(62, 1))
34397c478bd9Sstevel@tonic-gate 		checkfds("after delivery");
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 	/*
34427c478bd9Sstevel@tonic-gate 	**  Do final status disposal.
34437c478bd9Sstevel@tonic-gate 	**	We check for something in tobuf for the SMTP case.
34447c478bd9Sstevel@tonic-gate 	**	If we got a temporary failure, arrange to queue the
34457c478bd9Sstevel@tonic-gate 	**		addressees.
34467c478bd9Sstevel@tonic-gate 	*/
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate   give_up:
34497c478bd9Sstevel@tonic-gate 	if (bitnset(M_LMTP, m->m_flags))
34507c478bd9Sstevel@tonic-gate 	{
34517c478bd9Sstevel@tonic-gate 		lmtp_rcode = rcode;
34527c478bd9Sstevel@tonic-gate 		tobuf[0] = '\0';
34537c478bd9Sstevel@tonic-gate 		anyok = false;
34547c478bd9Sstevel@tonic-gate 		strsize = 0;
34557c478bd9Sstevel@tonic-gate 	}
34567c478bd9Sstevel@tonic-gate 	else
34577c478bd9Sstevel@tonic-gate 		anyok = rcode == EX_OK;
34587c478bd9Sstevel@tonic-gate 
34597c478bd9Sstevel@tonic-gate 	for (to = tochain; to != NULL; to = to->q_tchain)
34607c478bd9Sstevel@tonic-gate 	{
34617c478bd9Sstevel@tonic-gate 		/* see if address already marked */
34627c478bd9Sstevel@tonic-gate 		if (!QS_IS_OK(to->q_state))
34637c478bd9Sstevel@tonic-gate 			continue;
34647c478bd9Sstevel@tonic-gate 
34657c478bd9Sstevel@tonic-gate 		/* if running LMTP, get the status for each address */
34667c478bd9Sstevel@tonic-gate 		if (bitnset(M_LMTP, m->m_flags))
34677c478bd9Sstevel@tonic-gate 		{
34687c478bd9Sstevel@tonic-gate 			if (lmtp_rcode == EX_OK)
34697c478bd9Sstevel@tonic-gate 				rcode = smtpgetstat(m, mci, e);
34707c478bd9Sstevel@tonic-gate 			if (rcode == EX_OK)
34717c478bd9Sstevel@tonic-gate 			{
34727c478bd9Sstevel@tonic-gate 				strsize += sm_strlcat2(tobuf + strsize, ",",
34737c478bd9Sstevel@tonic-gate 						to->q_paddr,
34747c478bd9Sstevel@tonic-gate 						tobufsize - strsize);
34757c478bd9Sstevel@tonic-gate 				SM_ASSERT(strsize < tobufsize);
34767c478bd9Sstevel@tonic-gate 				anyok = true;
34777c478bd9Sstevel@tonic-gate 			}
34787c478bd9Sstevel@tonic-gate 			else
34797c478bd9Sstevel@tonic-gate 			{
34807c478bd9Sstevel@tonic-gate 				e->e_to = to->q_paddr;
34817c478bd9Sstevel@tonic-gate 				markfailure(e, to, mci, rcode, true);
34827c478bd9Sstevel@tonic-gate 				giveresponse(rcode, to->q_status, m, mci,
34837c478bd9Sstevel@tonic-gate 					     ctladdr, xstart, e, to);
34847c478bd9Sstevel@tonic-gate 				e->e_to = tobuf + 1;
34857c478bd9Sstevel@tonic-gate 				continue;
34867c478bd9Sstevel@tonic-gate 			}
34877c478bd9Sstevel@tonic-gate 		}
34887c478bd9Sstevel@tonic-gate 		else
34897c478bd9Sstevel@tonic-gate 		{
34907c478bd9Sstevel@tonic-gate 			/* mark bad addresses */
34917c478bd9Sstevel@tonic-gate 			if (rcode != EX_OK)
34927c478bd9Sstevel@tonic-gate 			{
34937c478bd9Sstevel@tonic-gate 				if (goodmxfound && rcode == EX_NOHOST)
34947c478bd9Sstevel@tonic-gate 					rcode = EX_TEMPFAIL;
34957c478bd9Sstevel@tonic-gate 				markfailure(e, to, mci, rcode, true);
34967c478bd9Sstevel@tonic-gate 				continue;
34977c478bd9Sstevel@tonic-gate 			}
34987c478bd9Sstevel@tonic-gate 		}
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate 		/* successful delivery */
35017c478bd9Sstevel@tonic-gate 		to->q_state = QS_SENT;
35027c478bd9Sstevel@tonic-gate 		to->q_statdate = curtime();
35037c478bd9Sstevel@tonic-gate 		e->e_nsent++;
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate 		/*
35067c478bd9Sstevel@tonic-gate 		**  Checkpoint the send list every few addresses
35077c478bd9Sstevel@tonic-gate 		*/
35087c478bd9Sstevel@tonic-gate 
35097c478bd9Sstevel@tonic-gate 		if (CheckpointInterval > 0 && e->e_nsent >= CheckpointInterval)
35107c478bd9Sstevel@tonic-gate 		{
35117c478bd9Sstevel@tonic-gate 			queueup(e, false, false);
35127c478bd9Sstevel@tonic-gate 			e->e_nsent = 0;
35137c478bd9Sstevel@tonic-gate 		}
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 		if (bitnset(M_LOCALMAILER, m->m_flags) &&
35167c478bd9Sstevel@tonic-gate 		    bitset(QPINGONSUCCESS, to->q_flags))
35177c478bd9Sstevel@tonic-gate 		{
35187c478bd9Sstevel@tonic-gate 			to->q_flags |= QDELIVERED;
35197c478bd9Sstevel@tonic-gate 			to->q_status = "2.1.5";
35207c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
35217c478bd9Sstevel@tonic-gate 					     "%s... Successfully delivered\n",
35227c478bd9Sstevel@tonic-gate 					     to->q_paddr);
35237c478bd9Sstevel@tonic-gate 		}
35247c478bd9Sstevel@tonic-gate 		else if (bitset(QPINGONSUCCESS, to->q_flags) &&
35257c478bd9Sstevel@tonic-gate 			 bitset(QPRIMARY, to->q_flags) &&
35267c478bd9Sstevel@tonic-gate 			 !bitset(MCIF_DSN, mci->mci_flags))
35277c478bd9Sstevel@tonic-gate 		{
35287c478bd9Sstevel@tonic-gate 			to->q_flags |= QRELAYED;
35297c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
35307c478bd9Sstevel@tonic-gate 					     "%s... relayed; expect no further notifications\n",
35317c478bd9Sstevel@tonic-gate 					     to->q_paddr);
35327c478bd9Sstevel@tonic-gate 		}
35337c478bd9Sstevel@tonic-gate 		else if (IS_DLVR_NOTIFY(e) &&
35347c478bd9Sstevel@tonic-gate 			 !bitset(MCIF_DLVR_BY, mci->mci_flags) &&
35357c478bd9Sstevel@tonic-gate 			 bitset(QPRIMARY, to->q_flags) &&
35367c478bd9Sstevel@tonic-gate 			 (!bitset(QHASNOTIFY, to->q_flags) ||
35377c478bd9Sstevel@tonic-gate 			  bitset(QPINGONSUCCESS, to->q_flags) ||
35387c478bd9Sstevel@tonic-gate 			  bitset(QPINGONFAILURE, to->q_flags) ||
35397c478bd9Sstevel@tonic-gate 			  bitset(QPINGONDELAY, to->q_flags)))
35407c478bd9Sstevel@tonic-gate 		{
35417c478bd9Sstevel@tonic-gate 			/* RFC 2852, 4.1.4.2: no NOTIFY, or not NEVER */
35427c478bd9Sstevel@tonic-gate 			to->q_flags |= QBYNRELAY;
35437c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
35447c478bd9Sstevel@tonic-gate 					     "%s... Deliver-by notify: relayed\n",
35457c478bd9Sstevel@tonic-gate 					     to->q_paddr);
35467c478bd9Sstevel@tonic-gate 		}
35477c478bd9Sstevel@tonic-gate 		else if (IS_DLVR_TRACE(e) &&
35487c478bd9Sstevel@tonic-gate 			 (!bitset(QHASNOTIFY, to->q_flags) ||
35497c478bd9Sstevel@tonic-gate 			  bitset(QPINGONSUCCESS, to->q_flags) ||
35507c478bd9Sstevel@tonic-gate 			  bitset(QPINGONFAILURE, to->q_flags) ||
35517c478bd9Sstevel@tonic-gate 			  bitset(QPINGONDELAY, to->q_flags)) &&
35527c478bd9Sstevel@tonic-gate 			 bitset(QPRIMARY, to->q_flags))
35537c478bd9Sstevel@tonic-gate 		{
35547c478bd9Sstevel@tonic-gate 			/* RFC 2852, 4.1.4: no NOTIFY, or not NEVER */
35557c478bd9Sstevel@tonic-gate 			to->q_flags |= QBYTRACE;
35567c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT,
35577c478bd9Sstevel@tonic-gate 					     "%s... Deliver-By trace: relayed\n",
35587c478bd9Sstevel@tonic-gate 					     to->q_paddr);
35597c478bd9Sstevel@tonic-gate 		}
35607c478bd9Sstevel@tonic-gate 	}
35617c478bd9Sstevel@tonic-gate 
35627c478bd9Sstevel@tonic-gate 	if (bitnset(M_LMTP, m->m_flags))
35637c478bd9Sstevel@tonic-gate 	{
35647c478bd9Sstevel@tonic-gate 		/*
35657c478bd9Sstevel@tonic-gate 		**  Global information applies to the last recipient only;
35667c478bd9Sstevel@tonic-gate 		**  clear it out to avoid bogus errors.
35677c478bd9Sstevel@tonic-gate 		*/
35687c478bd9Sstevel@tonic-gate 
35697c478bd9Sstevel@tonic-gate 		rcode = EX_OK;
35707c478bd9Sstevel@tonic-gate 		e->e_statmsg = NULL;
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 		/* reset the mci state for the next transaction */
35737c478bd9Sstevel@tonic-gate 		if (mci != NULL &&
35747c478bd9Sstevel@tonic-gate 		    (mci->mci_state == MCIS_MAIL ||
35757c478bd9Sstevel@tonic-gate 		     mci->mci_state == MCIS_RCPT ||
35767c478bd9Sstevel@tonic-gate 		     mci->mci_state == MCIS_DATA))
35777c478bd9Sstevel@tonic-gate 		{
35787c478bd9Sstevel@tonic-gate 			mci->mci_state = MCIS_OPEN;
35797c478bd9Sstevel@tonic-gate 			SmtpPhase = mci->mci_phase = "idle";
35807c478bd9Sstevel@tonic-gate 			sm_setproctitle(true, e, "%s: %s", CurHostName,
35817c478bd9Sstevel@tonic-gate 					mci->mci_phase);
35827c478bd9Sstevel@tonic-gate 		}
35837c478bd9Sstevel@tonic-gate 	}
35847c478bd9Sstevel@tonic-gate 
35857c478bd9Sstevel@tonic-gate 	if (tobuf[0] != '\0')
35867c478bd9Sstevel@tonic-gate 	{
35877c478bd9Sstevel@tonic-gate 		giveresponse(rcode, NULL, m, mci, ctladdr, xstart, e, tochain);
35887c478bd9Sstevel@tonic-gate #if 0
35897c478bd9Sstevel@tonic-gate 		/*
35907c478bd9Sstevel@tonic-gate 		**  This code is disabled for now because I am not
35917c478bd9Sstevel@tonic-gate 		**  sure that copying status from the first recipient
35927c478bd9Sstevel@tonic-gate 		**  to all non-status'ed recipients is a good idea.
35937c478bd9Sstevel@tonic-gate 		*/
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 		if (tochain->q_message != NULL &&
35967c478bd9Sstevel@tonic-gate 		    !bitnset(M_LMTP, m->m_flags) && rcode != EX_OK)
35977c478bd9Sstevel@tonic-gate 		{
35987c478bd9Sstevel@tonic-gate 			for (to = tochain->q_tchain; to != NULL;
35997c478bd9Sstevel@tonic-gate 			     to = to->q_tchain)
36007c478bd9Sstevel@tonic-gate 			{
36017c478bd9Sstevel@tonic-gate 				/* see if address already marked */
36027c478bd9Sstevel@tonic-gate 				if (QS_IS_QUEUEUP(to->q_state) &&
36037c478bd9Sstevel@tonic-gate 				    to->q_message == NULL)
36047c478bd9Sstevel@tonic-gate 					to->q_message = sm_rpool_strdup_x(e->e_rpool,
36057c478bd9Sstevel@tonic-gate 							tochain->q_message);
36067c478bd9Sstevel@tonic-gate 			}
36077c478bd9Sstevel@tonic-gate 		}
36087c478bd9Sstevel@tonic-gate #endif /* 0 */
36097c478bd9Sstevel@tonic-gate 	}
36107c478bd9Sstevel@tonic-gate 	if (anyok)
36117c478bd9Sstevel@tonic-gate 		markstats(e, tochain, STATS_NORMAL);
36127c478bd9Sstevel@tonic-gate 	mci_store_persistent(mci);
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate 	/* Some recipients were tempfailed, try them on the next host */
36157c478bd9Sstevel@tonic-gate 	if (mci != NULL && mci->mci_retryrcpt && nummxhosts > hostnum)
36167c478bd9Sstevel@tonic-gate 	{
36177c478bd9Sstevel@tonic-gate 		/* try next MX site */
36187c478bd9Sstevel@tonic-gate 		goto tryhost;
36197c478bd9Sstevel@tonic-gate 	}
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	/* now close the connection */
36227c478bd9Sstevel@tonic-gate 	if (clever && mci != NULL && mci->mci_state != MCIS_CLOSED &&
36237c478bd9Sstevel@tonic-gate 	    !bitset(MCIF_CACHED, mci->mci_flags))
36247c478bd9Sstevel@tonic-gate 		smtpquit(m, mci, e);
36257c478bd9Sstevel@tonic-gate 
36267c478bd9Sstevel@tonic-gate cleanup: ;
36277c478bd9Sstevel@tonic-gate 	}
36287c478bd9Sstevel@tonic-gate 	SM_FINALLY
36297c478bd9Sstevel@tonic-gate 	{
36307c478bd9Sstevel@tonic-gate 		/*
36317c478bd9Sstevel@tonic-gate 		**  Restore state and return.
36327c478bd9Sstevel@tonic-gate 		*/
36337c478bd9Sstevel@tonic-gate #if XDEBUG
36347c478bd9Sstevel@tonic-gate 		char wbuf[MAXLINE];
36357c478bd9Sstevel@tonic-gate 
36367c478bd9Sstevel@tonic-gate 		/* make absolutely certain 0, 1, and 2 are in use */
3637058561cbSjbeck 		(void) sm_snprintf(wbuf, sizeof(wbuf),
36387c478bd9Sstevel@tonic-gate 				   "%s... end of deliver(%s)",
36397c478bd9Sstevel@tonic-gate 				   e->e_to == NULL ? "NO-TO-LIST"
36407c478bd9Sstevel@tonic-gate 						   : shortenstring(e->e_to,
36417c478bd9Sstevel@tonic-gate 								   MAXSHORTSTR),
36427c478bd9Sstevel@tonic-gate 				  m->m_name);
36437c478bd9Sstevel@tonic-gate 		checkfd012(wbuf);
36447c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
36457c478bd9Sstevel@tonic-gate 
36467c478bd9Sstevel@tonic-gate 		errno = 0;
36477c478bd9Sstevel@tonic-gate 
36487c478bd9Sstevel@tonic-gate 		/*
36497c478bd9Sstevel@tonic-gate 		**  It was originally necessary to set macro 'g' to NULL
36507c478bd9Sstevel@tonic-gate 		**  because it previously pointed to an auto buffer.
36517c478bd9Sstevel@tonic-gate 		**  We don't do this any more, so this may be unnecessary.
36527c478bd9Sstevel@tonic-gate 		*/
36537c478bd9Sstevel@tonic-gate 
36547c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_PERM, 'g', (char *) NULL);
36557c478bd9Sstevel@tonic-gate 		e->e_to = NULL;
36567c478bd9Sstevel@tonic-gate 	}
36577c478bd9Sstevel@tonic-gate 	SM_END_TRY
36587c478bd9Sstevel@tonic-gate 	return rcode;
36597c478bd9Sstevel@tonic-gate }
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate /*
36627c478bd9Sstevel@tonic-gate **  MARKFAILURE -- mark a failure on a specific address.
36637c478bd9Sstevel@tonic-gate **
36647c478bd9Sstevel@tonic-gate **	Parameters:
36657c478bd9Sstevel@tonic-gate **		e -- the envelope we are sending.
36667c478bd9Sstevel@tonic-gate **		q -- the address to mark.
36677c478bd9Sstevel@tonic-gate **		mci -- mailer connection information.
36687c478bd9Sstevel@tonic-gate **		rcode -- the code signifying the particular failure.
36697c478bd9Sstevel@tonic-gate **		ovr -- override an existing code?
36707c478bd9Sstevel@tonic-gate **
36717c478bd9Sstevel@tonic-gate **	Returns:
36727c478bd9Sstevel@tonic-gate **		none.
36737c478bd9Sstevel@tonic-gate **
36747c478bd9Sstevel@tonic-gate **	Side Effects:
36757c478bd9Sstevel@tonic-gate **		marks the address (and possibly the envelope) with the
36767c478bd9Sstevel@tonic-gate **			failure so that an error will be returned or
36777c478bd9Sstevel@tonic-gate **			the message will be queued, as appropriate.
36787c478bd9Sstevel@tonic-gate */
36797c478bd9Sstevel@tonic-gate 
36807c478bd9Sstevel@tonic-gate void
36817c478bd9Sstevel@tonic-gate markfailure(e, q, mci, rcode, ovr)
36827c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
36837c478bd9Sstevel@tonic-gate 	register ADDRESS *q;
36847c478bd9Sstevel@tonic-gate 	register MCI *mci;
36857c478bd9Sstevel@tonic-gate 	int rcode;
36867c478bd9Sstevel@tonic-gate 	bool ovr;
36877c478bd9Sstevel@tonic-gate {
36887c478bd9Sstevel@tonic-gate 	int save_errno = errno;
36897c478bd9Sstevel@tonic-gate 	char *status = NULL;
36907c478bd9Sstevel@tonic-gate 	char *rstatus = NULL;
36917c478bd9Sstevel@tonic-gate 
36927c478bd9Sstevel@tonic-gate 	switch (rcode)
36937c478bd9Sstevel@tonic-gate 	{
36947c478bd9Sstevel@tonic-gate 	  case EX_OK:
36957c478bd9Sstevel@tonic-gate 		break;
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate 	  case EX_TEMPFAIL:
36987c478bd9Sstevel@tonic-gate 	  case EX_IOERR:
36997c478bd9Sstevel@tonic-gate 	  case EX_OSERR:
37007c478bd9Sstevel@tonic-gate 		q->q_state = QS_QUEUEUP;
37017c478bd9Sstevel@tonic-gate 		break;
37027c478bd9Sstevel@tonic-gate 
37037c478bd9Sstevel@tonic-gate 	  default:
37047c478bd9Sstevel@tonic-gate 		q->q_state = QS_BADADDR;
37057c478bd9Sstevel@tonic-gate 		break;
37067c478bd9Sstevel@tonic-gate 	}
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	/* find most specific error code possible */
37097c478bd9Sstevel@tonic-gate 	if (mci != NULL && mci->mci_status != NULL)
37107c478bd9Sstevel@tonic-gate 	{
37117c478bd9Sstevel@tonic-gate 		status = sm_rpool_strdup_x(e->e_rpool, mci->mci_status);
37127c478bd9Sstevel@tonic-gate 		if (mci->mci_rstatus != NULL)
37137c478bd9Sstevel@tonic-gate 			rstatus = sm_rpool_strdup_x(e->e_rpool,
37147c478bd9Sstevel@tonic-gate 						    mci->mci_rstatus);
37157c478bd9Sstevel@tonic-gate 		else
37167c478bd9Sstevel@tonic-gate 			rstatus = NULL;
37177c478bd9Sstevel@tonic-gate 	}
37187c478bd9Sstevel@tonic-gate 	else if (e->e_status != NULL)
37197c478bd9Sstevel@tonic-gate 	{
37207c478bd9Sstevel@tonic-gate 		status = e->e_status;
37217c478bd9Sstevel@tonic-gate 		rstatus = NULL;
37227c478bd9Sstevel@tonic-gate 	}
37237c478bd9Sstevel@tonic-gate 	else
37247c478bd9Sstevel@tonic-gate 	{
37257c478bd9Sstevel@tonic-gate 		switch (rcode)
37267c478bd9Sstevel@tonic-gate 		{
37277c478bd9Sstevel@tonic-gate 		  case EX_USAGE:
37287c478bd9Sstevel@tonic-gate 			status = "5.5.4";
37297c478bd9Sstevel@tonic-gate 			break;
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate 		  case EX_DATAERR:
37327c478bd9Sstevel@tonic-gate 			status = "5.5.2";
37337c478bd9Sstevel@tonic-gate 			break;
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate 		  case EX_NOUSER:
37367c478bd9Sstevel@tonic-gate 			status = "5.1.1";
37377c478bd9Sstevel@tonic-gate 			break;
37387c478bd9Sstevel@tonic-gate 
37397c478bd9Sstevel@tonic-gate 		  case EX_NOHOST:
37407c478bd9Sstevel@tonic-gate 			status = "5.1.2";
37417c478bd9Sstevel@tonic-gate 			break;
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate 		  case EX_NOINPUT:
37447c478bd9Sstevel@tonic-gate 		  case EX_CANTCREAT:
37457c478bd9Sstevel@tonic-gate 		  case EX_NOPERM:
37467c478bd9Sstevel@tonic-gate 			status = "5.3.0";
37477c478bd9Sstevel@tonic-gate 			break;
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate 		  case EX_UNAVAILABLE:
37507c478bd9Sstevel@tonic-gate 		  case EX_SOFTWARE:
37517c478bd9Sstevel@tonic-gate 		  case EX_OSFILE:
37527c478bd9Sstevel@tonic-gate 		  case EX_PROTOCOL:
37537c478bd9Sstevel@tonic-gate 		  case EX_CONFIG:
37547c478bd9Sstevel@tonic-gate 			status = "5.5.0";
37557c478bd9Sstevel@tonic-gate 			break;
37567c478bd9Sstevel@tonic-gate 
37577c478bd9Sstevel@tonic-gate 		  case EX_OSERR:
37587c478bd9Sstevel@tonic-gate 		  case EX_IOERR:
37597c478bd9Sstevel@tonic-gate 			status = "4.5.0";
37607c478bd9Sstevel@tonic-gate 			break;
37617c478bd9Sstevel@tonic-gate 
37627c478bd9Sstevel@tonic-gate 		  case EX_TEMPFAIL:
37637c478bd9Sstevel@tonic-gate 			status = "4.2.0";
37647c478bd9Sstevel@tonic-gate 			break;
37657c478bd9Sstevel@tonic-gate 		}
37667c478bd9Sstevel@tonic-gate 	}
37677c478bd9Sstevel@tonic-gate 
37687c478bd9Sstevel@tonic-gate 	/* new status? */
37697c478bd9Sstevel@tonic-gate 	if (status != NULL && *status != '\0' && (ovr || q->q_status == NULL ||
37707c478bd9Sstevel@tonic-gate 	    *q->q_status == '\0' || *q->q_status < *status))
37717c478bd9Sstevel@tonic-gate 	{
37727c478bd9Sstevel@tonic-gate 		q->q_status = status;
37737c478bd9Sstevel@tonic-gate 		q->q_rstatus = rstatus;
37747c478bd9Sstevel@tonic-gate 	}
37757c478bd9Sstevel@tonic-gate 	if (rcode != EX_OK && q->q_rstatus == NULL &&
37767c478bd9Sstevel@tonic-gate 	    q->q_mailer != NULL && q->q_mailer->m_diagtype != NULL &&
37777c478bd9Sstevel@tonic-gate 	    sm_strcasecmp(q->q_mailer->m_diagtype, "X-UNIX") == 0)
37787c478bd9Sstevel@tonic-gate 	{
37797c478bd9Sstevel@tonic-gate 		char buf[16];
37807c478bd9Sstevel@tonic-gate 
3781058561cbSjbeck 		(void) sm_snprintf(buf, sizeof(buf), "%d", rcode);
37827c478bd9Sstevel@tonic-gate 		q->q_rstatus = sm_rpool_strdup_x(e->e_rpool, buf);
37837c478bd9Sstevel@tonic-gate 	}
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate 	q->q_statdate = curtime();
37867c478bd9Sstevel@tonic-gate 	if (CurHostName != NULL && CurHostName[0] != '\0' &&
37877c478bd9Sstevel@tonic-gate 	    mci != NULL && !bitset(M_LOCALMAILER, mci->mci_flags))
37887c478bd9Sstevel@tonic-gate 		q->q_statmta = sm_rpool_strdup_x(e->e_rpool, CurHostName);
37897c478bd9Sstevel@tonic-gate 
37907c478bd9Sstevel@tonic-gate 	/* restore errno */
37917c478bd9Sstevel@tonic-gate 	errno = save_errno;
37927c478bd9Sstevel@tonic-gate }
37937c478bd9Sstevel@tonic-gate /*
37947c478bd9Sstevel@tonic-gate **  ENDMAILER -- Wait for mailer to terminate.
37957c478bd9Sstevel@tonic-gate **
37967c478bd9Sstevel@tonic-gate **	We should never get fatal errors (e.g., segmentation
37977c478bd9Sstevel@tonic-gate **	violation), so we report those specially.  For other
37987c478bd9Sstevel@tonic-gate **	errors, we choose a status message (into statmsg),
37997c478bd9Sstevel@tonic-gate **	and if it represents an error, we print it.
38007c478bd9Sstevel@tonic-gate **
38017c478bd9Sstevel@tonic-gate **	Parameters:
38027c478bd9Sstevel@tonic-gate **		mci -- the mailer connection info.
38037c478bd9Sstevel@tonic-gate **		e -- the current envelope.
38047c478bd9Sstevel@tonic-gate **		pv -- the parameter vector that invoked the mailer
38057c478bd9Sstevel@tonic-gate **			(for error messages).
38067c478bd9Sstevel@tonic-gate **
38077c478bd9Sstevel@tonic-gate **	Returns:
38087c478bd9Sstevel@tonic-gate **		exit code of mailer.
38097c478bd9Sstevel@tonic-gate **
38107c478bd9Sstevel@tonic-gate **	Side Effects:
38117c478bd9Sstevel@tonic-gate **		none.
38127c478bd9Sstevel@tonic-gate */
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate static jmp_buf	EndWaitTimeout;
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate static void
38177c478bd9Sstevel@tonic-gate endwaittimeout(ignore)
38187c478bd9Sstevel@tonic-gate 	int ignore;
38197c478bd9Sstevel@tonic-gate {
38207c478bd9Sstevel@tonic-gate 	/*
38217c478bd9Sstevel@tonic-gate 	**  NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
38227c478bd9Sstevel@tonic-gate 	**	ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
38237c478bd9Sstevel@tonic-gate 	**	DOING.
38247c478bd9Sstevel@tonic-gate 	*/
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 	errno = ETIMEDOUT;
38277c478bd9Sstevel@tonic-gate 	longjmp(EndWaitTimeout, 1);
38287c478bd9Sstevel@tonic-gate }
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate int
38317c478bd9Sstevel@tonic-gate endmailer(mci, e, pv)
38327c478bd9Sstevel@tonic-gate 	register MCI *mci;
38337c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
38347c478bd9Sstevel@tonic-gate 	char **pv;
38357c478bd9Sstevel@tonic-gate {
38367c478bd9Sstevel@tonic-gate 	int st;
38377c478bd9Sstevel@tonic-gate 	int save_errno = errno;
38387c478bd9Sstevel@tonic-gate 	char buf[MAXLINE];
38397c478bd9Sstevel@tonic-gate 	SM_EVENT *ev = NULL;
38407c478bd9Sstevel@tonic-gate 
38417c478bd9Sstevel@tonic-gate 
38427c478bd9Sstevel@tonic-gate 	mci_unlock_host(mci);
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 	/* close output to mailer */
38457c478bd9Sstevel@tonic-gate 	if (mci->mci_out != NULL)
38467c478bd9Sstevel@tonic-gate 	{
38477c478bd9Sstevel@tonic-gate 		(void) sm_io_close(mci->mci_out, SM_TIME_DEFAULT);
38487c478bd9Sstevel@tonic-gate 		mci->mci_out = NULL;
38497c478bd9Sstevel@tonic-gate 	}
38507c478bd9Sstevel@tonic-gate 
38517c478bd9Sstevel@tonic-gate 	/* copy any remaining input to transcript */
38527c478bd9Sstevel@tonic-gate 	if (mci->mci_in != NULL && mci->mci_state != MCIS_ERROR &&
38537c478bd9Sstevel@tonic-gate 	    e->e_xfp != NULL)
38547c478bd9Sstevel@tonic-gate 	{
3855058561cbSjbeck 		while (sfgets(buf, sizeof(buf), mci->mci_in,
38567c478bd9Sstevel@tonic-gate 			      TimeOuts.to_quit, "Draining Input") != NULL)
38577c478bd9Sstevel@tonic-gate 			(void) sm_io_fputs(e->e_xfp, SM_TIME_DEFAULT, buf);
38587c478bd9Sstevel@tonic-gate 	}
38597c478bd9Sstevel@tonic-gate 
38607c478bd9Sstevel@tonic-gate #if SASL
38617c478bd9Sstevel@tonic-gate 	/* close SASL connection */
38627c478bd9Sstevel@tonic-gate 	if (bitset(MCIF_AUTHACT, mci->mci_flags))
38637c478bd9Sstevel@tonic-gate 	{
38647c478bd9Sstevel@tonic-gate 		sasl_dispose(&mci->mci_conn);
38657c478bd9Sstevel@tonic-gate 		mci->mci_flags &= ~MCIF_AUTHACT;
38667c478bd9Sstevel@tonic-gate 	}
38677c478bd9Sstevel@tonic-gate #endif /* SASL */
38687c478bd9Sstevel@tonic-gate 
38697c478bd9Sstevel@tonic-gate #if STARTTLS
38707c478bd9Sstevel@tonic-gate 	/* shutdown TLS */
38717c478bd9Sstevel@tonic-gate 	(void) endtlsclt(mci);
38727c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
38737c478bd9Sstevel@tonic-gate 
38747c478bd9Sstevel@tonic-gate 	/* now close the input */
38757c478bd9Sstevel@tonic-gate 	if (mci->mci_in != NULL)
38767c478bd9Sstevel@tonic-gate 	{
38777c478bd9Sstevel@tonic-gate 		(void) sm_io_close(mci->mci_in, SM_TIME_DEFAULT);
38787c478bd9Sstevel@tonic-gate 		mci->mci_in = NULL;
38797c478bd9Sstevel@tonic-gate 	}
38807c478bd9Sstevel@tonic-gate 	mci->mci_state = MCIS_CLOSED;
38817c478bd9Sstevel@tonic-gate 
38827c478bd9Sstevel@tonic-gate 	errno = save_errno;
38837c478bd9Sstevel@tonic-gate 
38847c478bd9Sstevel@tonic-gate 	/* in the IPC case there is nothing to wait for */
38857c478bd9Sstevel@tonic-gate 	if (mci->mci_pid == 0)
38867c478bd9Sstevel@tonic-gate 		return EX_OK;
38877c478bd9Sstevel@tonic-gate 
38887c478bd9Sstevel@tonic-gate 	/* put a timeout around the wait */
38897c478bd9Sstevel@tonic-gate 	if (mci->mci_mailer->m_wait > 0)
38907c478bd9Sstevel@tonic-gate 	{
38917c478bd9Sstevel@tonic-gate 		if (setjmp(EndWaitTimeout) == 0)
38927c478bd9Sstevel@tonic-gate 			ev = sm_setevent(mci->mci_mailer->m_wait,
38937c478bd9Sstevel@tonic-gate 					 endwaittimeout, 0);
38947c478bd9Sstevel@tonic-gate 		else
38957c478bd9Sstevel@tonic-gate 		{
38967c478bd9Sstevel@tonic-gate 			syserr("endmailer %s: wait timeout (%ld)",
38977c478bd9Sstevel@tonic-gate 			       mci->mci_mailer->m_name,
38987c478bd9Sstevel@tonic-gate 			       (long) mci->mci_mailer->m_wait);
38997c478bd9Sstevel@tonic-gate 			return EX_TEMPFAIL;
39007c478bd9Sstevel@tonic-gate 		}
39017c478bd9Sstevel@tonic-gate 	}
39027c478bd9Sstevel@tonic-gate 
39037c478bd9Sstevel@tonic-gate 	/* wait for the mailer process, collect status */
39047c478bd9Sstevel@tonic-gate 	st = waitfor(mci->mci_pid);
39057c478bd9Sstevel@tonic-gate 	save_errno = errno;
39067c478bd9Sstevel@tonic-gate 	if (ev != NULL)
39077c478bd9Sstevel@tonic-gate 		sm_clrevent(ev);
39087c478bd9Sstevel@tonic-gate 	errno = save_errno;
39097c478bd9Sstevel@tonic-gate 
39107c478bd9Sstevel@tonic-gate 	if (st == -1)
39117c478bd9Sstevel@tonic-gate 	{
39127c478bd9Sstevel@tonic-gate 		syserr("endmailer %s: wait", mci->mci_mailer->m_name);
39137c478bd9Sstevel@tonic-gate 		return EX_SOFTWARE;
39147c478bd9Sstevel@tonic-gate 	}
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 	if (WIFEXITED(st))
39177c478bd9Sstevel@tonic-gate 	{
39187c478bd9Sstevel@tonic-gate 		/* normal death -- return status */
39197c478bd9Sstevel@tonic-gate 		return (WEXITSTATUS(st));
39207c478bd9Sstevel@tonic-gate 	}
39217c478bd9Sstevel@tonic-gate 
39227c478bd9Sstevel@tonic-gate 	/* it died a horrid death */
39237c478bd9Sstevel@tonic-gate 	syserr("451 4.3.0 mailer %s died with signal %d%s",
39247c478bd9Sstevel@tonic-gate 		mci->mci_mailer->m_name, WTERMSIG(st),
39257c478bd9Sstevel@tonic-gate 		WCOREDUMP(st) ? " (core dumped)" :
39267c478bd9Sstevel@tonic-gate 		(WIFSTOPPED(st) ? " (stopped)" : ""));
39277c478bd9Sstevel@tonic-gate 
39287c478bd9Sstevel@tonic-gate 	/* log the arguments */
39297c478bd9Sstevel@tonic-gate 	if (pv != NULL && e->e_xfp != NULL)
39307c478bd9Sstevel@tonic-gate 	{
39317c478bd9Sstevel@tonic-gate 		register char **av;
39327c478bd9Sstevel@tonic-gate 
39337c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT, "Arguments:");
39347c478bd9Sstevel@tonic-gate 		for (av = pv; *av != NULL; av++)
39357c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT, " %s",
39367c478bd9Sstevel@tonic-gate 					     *av);
39377c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT, "\n");
39387c478bd9Sstevel@tonic-gate 	}
39397c478bd9Sstevel@tonic-gate 
39407c478bd9Sstevel@tonic-gate 	ExitStat = EX_TEMPFAIL;
39417c478bd9Sstevel@tonic-gate 	return EX_TEMPFAIL;
39427c478bd9Sstevel@tonic-gate }
39437c478bd9Sstevel@tonic-gate /*
39447c478bd9Sstevel@tonic-gate **  GIVERESPONSE -- Interpret an error response from a mailer
39457c478bd9Sstevel@tonic-gate **
39467c478bd9Sstevel@tonic-gate **	Parameters:
39477c478bd9Sstevel@tonic-gate **		status -- the status code from the mailer (high byte
39487c478bd9Sstevel@tonic-gate **			only; core dumps must have been taken care of
39497c478bd9Sstevel@tonic-gate **			already).
39507c478bd9Sstevel@tonic-gate **		dsn -- the DSN associated with the address, if any.
39517c478bd9Sstevel@tonic-gate **		m -- the mailer info for this mailer.
39527c478bd9Sstevel@tonic-gate **		mci -- the mailer connection info -- can be NULL if the
39537c478bd9Sstevel@tonic-gate **			response is given before the connection is made.
39547c478bd9Sstevel@tonic-gate **		ctladdr -- the controlling address for the recipient
39557c478bd9Sstevel@tonic-gate **			address(es).
39567c478bd9Sstevel@tonic-gate **		xstart -- the transaction start time, for computing
39577c478bd9Sstevel@tonic-gate **			transaction delays.
39587c478bd9Sstevel@tonic-gate **		e -- the current envelope.
39597c478bd9Sstevel@tonic-gate **		to -- the current recipient (NULL if none).
39607c478bd9Sstevel@tonic-gate **
39617c478bd9Sstevel@tonic-gate **	Returns:
39627c478bd9Sstevel@tonic-gate **		none.
39637c478bd9Sstevel@tonic-gate **
39647c478bd9Sstevel@tonic-gate **	Side Effects:
39657c478bd9Sstevel@tonic-gate **		Errors may be incremented.
39667c478bd9Sstevel@tonic-gate **		ExitStat may be set.
39677c478bd9Sstevel@tonic-gate */
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate void
39707c478bd9Sstevel@tonic-gate giveresponse(status, dsn, m, mci, ctladdr, xstart, e, to)
39717c478bd9Sstevel@tonic-gate 	int status;
39727c478bd9Sstevel@tonic-gate 	char *dsn;
39737c478bd9Sstevel@tonic-gate 	register MAILER *m;
39747c478bd9Sstevel@tonic-gate 	register MCI *mci;
39757c478bd9Sstevel@tonic-gate 	ADDRESS *ctladdr;
39767c478bd9Sstevel@tonic-gate 	time_t xstart;
39777c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
39787c478bd9Sstevel@tonic-gate 	ADDRESS *to;
39797c478bd9Sstevel@tonic-gate {
39807c478bd9Sstevel@tonic-gate 	register const char *statmsg;
39817c478bd9Sstevel@tonic-gate 	int errnum = errno;
39827c478bd9Sstevel@tonic-gate 	int off = 4;
39837c478bd9Sstevel@tonic-gate 	bool usestat = false;
39847c478bd9Sstevel@tonic-gate 	char dsnbuf[ENHSCLEN];
39857c478bd9Sstevel@tonic-gate 	char buf[MAXLINE];
39867c478bd9Sstevel@tonic-gate 	char *exmsg;
39877c478bd9Sstevel@tonic-gate 
39887c478bd9Sstevel@tonic-gate 	if (e == NULL)
39893ee0e492Sjbeck 	{
39907c478bd9Sstevel@tonic-gate 		syserr("giveresponse: null envelope");
39913ee0e492Sjbeck 		/* NOTREACHED */
39923ee0e492Sjbeck 		SM_ASSERT(0);
39933ee0e492Sjbeck 	}
39947c478bd9Sstevel@tonic-gate 
39957c478bd9Sstevel@tonic-gate 	/*
39967c478bd9Sstevel@tonic-gate 	**  Compute status message from code.
39977c478bd9Sstevel@tonic-gate 	*/
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate 	exmsg = sm_sysexmsg(status);
40007c478bd9Sstevel@tonic-gate 	if (status == 0)
40017c478bd9Sstevel@tonic-gate 	{
40027c478bd9Sstevel@tonic-gate 		statmsg = "250 2.0.0 Sent";
40037c478bd9Sstevel@tonic-gate 		if (e->e_statmsg != NULL)
40047c478bd9Sstevel@tonic-gate 		{
4005058561cbSjbeck 			(void) sm_snprintf(buf, sizeof(buf), "%s (%s)",
40067c478bd9Sstevel@tonic-gate 					   statmsg,
40077c478bd9Sstevel@tonic-gate 					   shortenstring(e->e_statmsg, 403));
40087c478bd9Sstevel@tonic-gate 			statmsg = buf;
40097c478bd9Sstevel@tonic-gate 		}
40107c478bd9Sstevel@tonic-gate 	}
40117c478bd9Sstevel@tonic-gate 	else if (exmsg == NULL)
40127c478bd9Sstevel@tonic-gate 	{
4013058561cbSjbeck 		(void) sm_snprintf(buf, sizeof(buf),
40147c478bd9Sstevel@tonic-gate 				   "554 5.3.0 unknown mailer error %d",
40157c478bd9Sstevel@tonic-gate 				   status);
40167c478bd9Sstevel@tonic-gate 		status = EX_UNAVAILABLE;
40177c478bd9Sstevel@tonic-gate 		statmsg = buf;
40187c478bd9Sstevel@tonic-gate 		usestat = true;
40197c478bd9Sstevel@tonic-gate 	}
40207c478bd9Sstevel@tonic-gate 	else if (status == EX_TEMPFAIL)
40217c478bd9Sstevel@tonic-gate 	{
40227c478bd9Sstevel@tonic-gate 		char *bp = buf;
40237c478bd9Sstevel@tonic-gate 
40247c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(bp, exmsg + 1, SPACELEFT(buf, bp));
40257c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
40267c478bd9Sstevel@tonic-gate #if NAMED_BIND
40277c478bd9Sstevel@tonic-gate 		if (h_errno == TRY_AGAIN)
40287c478bd9Sstevel@tonic-gate 			statmsg = sm_errstring(h_errno + E_DNSBASE);
40297c478bd9Sstevel@tonic-gate 		else
40307c478bd9Sstevel@tonic-gate #endif /* NAMED_BIND */
40317c478bd9Sstevel@tonic-gate 		{
40327c478bd9Sstevel@tonic-gate 			if (errnum != 0)
40337c478bd9Sstevel@tonic-gate 				statmsg = sm_errstring(errnum);
40347c478bd9Sstevel@tonic-gate 			else
40357c478bd9Sstevel@tonic-gate 				statmsg = SmtpError;
40367c478bd9Sstevel@tonic-gate 		}
40377c478bd9Sstevel@tonic-gate 		if (statmsg != NULL && statmsg[0] != '\0')
40387c478bd9Sstevel@tonic-gate 		{
40397c478bd9Sstevel@tonic-gate 			switch (errnum)
40407c478bd9Sstevel@tonic-gate 			{
40417c478bd9Sstevel@tonic-gate #ifdef ENETDOWN
40427c478bd9Sstevel@tonic-gate 			  case ENETDOWN:	/* Network is down */
40437c478bd9Sstevel@tonic-gate #endif /* ENETDOWN */
40447c478bd9Sstevel@tonic-gate #ifdef ENETUNREACH
40457c478bd9Sstevel@tonic-gate 			  case ENETUNREACH:	/* Network is unreachable */
40467c478bd9Sstevel@tonic-gate #endif /* ENETUNREACH */
40477c478bd9Sstevel@tonic-gate #ifdef ENETRESET
40487c478bd9Sstevel@tonic-gate 			  case ENETRESET:	/* Network dropped connection on reset */
40497c478bd9Sstevel@tonic-gate #endif /* ENETRESET */
40507c478bd9Sstevel@tonic-gate #ifdef ECONNABORTED
40517c478bd9Sstevel@tonic-gate 			  case ECONNABORTED:	/* Software caused connection abort */
40527c478bd9Sstevel@tonic-gate #endif /* ECONNABORTED */
40537c478bd9Sstevel@tonic-gate #ifdef EHOSTDOWN
40547c478bd9Sstevel@tonic-gate 			  case EHOSTDOWN:	/* Host is down */
40557c478bd9Sstevel@tonic-gate #endif /* EHOSTDOWN */
40567c478bd9Sstevel@tonic-gate #ifdef EHOSTUNREACH
40577c478bd9Sstevel@tonic-gate 			  case EHOSTUNREACH:	/* No route to host */
40587c478bd9Sstevel@tonic-gate #endif /* EHOSTUNREACH */
40597c478bd9Sstevel@tonic-gate 				if (mci != NULL && mci->mci_host != NULL)
40607c478bd9Sstevel@tonic-gate 				{
40617c478bd9Sstevel@tonic-gate 					(void) sm_strlcpyn(bp,
40627c478bd9Sstevel@tonic-gate 							   SPACELEFT(buf, bp),
40637c478bd9Sstevel@tonic-gate 							   2, ": ",
40647c478bd9Sstevel@tonic-gate 							   mci->mci_host);
40657c478bd9Sstevel@tonic-gate 					bp += strlen(bp);
40667c478bd9Sstevel@tonic-gate 				}
40677c478bd9Sstevel@tonic-gate 				break;
40687c478bd9Sstevel@tonic-gate 			}
40697c478bd9Sstevel@tonic-gate 			(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2, ": ",
40707c478bd9Sstevel@tonic-gate 					   statmsg);
40717c478bd9Sstevel@tonic-gate 			usestat = true;
40727c478bd9Sstevel@tonic-gate 		}
40737c478bd9Sstevel@tonic-gate 		statmsg = buf;
40747c478bd9Sstevel@tonic-gate 	}
40757c478bd9Sstevel@tonic-gate #if NAMED_BIND
40767c478bd9Sstevel@tonic-gate 	else if (status == EX_NOHOST && h_errno != 0)
40777c478bd9Sstevel@tonic-gate 	{
40787c478bd9Sstevel@tonic-gate 		statmsg = sm_errstring(h_errno + E_DNSBASE);
4079058561cbSjbeck 		(void) sm_snprintf(buf, sizeof(buf), "%s (%s)", exmsg + 1,
40807c478bd9Sstevel@tonic-gate 				   statmsg);
40817c478bd9Sstevel@tonic-gate 		statmsg = buf;
40827c478bd9Sstevel@tonic-gate 		usestat = true;
40837c478bd9Sstevel@tonic-gate 	}
40847c478bd9Sstevel@tonic-gate #endif /* NAMED_BIND */
40857c478bd9Sstevel@tonic-gate 	else
40867c478bd9Sstevel@tonic-gate 	{
40877c478bd9Sstevel@tonic-gate 		statmsg = exmsg;
40887c478bd9Sstevel@tonic-gate 		if (*statmsg++ == ':' && errnum != 0)
40897c478bd9Sstevel@tonic-gate 		{
4090058561cbSjbeck 			(void) sm_snprintf(buf, sizeof(buf), "%s: %s", statmsg,
40917c478bd9Sstevel@tonic-gate 					   sm_errstring(errnum));
40927c478bd9Sstevel@tonic-gate 			statmsg = buf;
40937c478bd9Sstevel@tonic-gate 			usestat = true;
40947c478bd9Sstevel@tonic-gate 		}
40957c478bd9Sstevel@tonic-gate 		else if (bitnset(M_LMTP, m->m_flags) && e->e_statmsg != NULL)
40967c478bd9Sstevel@tonic-gate 		{
4097058561cbSjbeck 			(void) sm_snprintf(buf, sizeof(buf), "%s (%s)", statmsg,
40987c478bd9Sstevel@tonic-gate 					   shortenstring(e->e_statmsg, 403));
40997c478bd9Sstevel@tonic-gate 			statmsg = buf;
41007c478bd9Sstevel@tonic-gate 			usestat = true;
41017c478bd9Sstevel@tonic-gate 		}
41027c478bd9Sstevel@tonic-gate 	}
41037c478bd9Sstevel@tonic-gate 
41047c478bd9Sstevel@tonic-gate 	/*
41057c478bd9Sstevel@tonic-gate 	**  Print the message as appropriate
41067c478bd9Sstevel@tonic-gate 	*/
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 	if (status == EX_OK || status == EX_TEMPFAIL)
41097c478bd9Sstevel@tonic-gate 	{
41107c478bd9Sstevel@tonic-gate 		extern char MsgBuf[];
41117c478bd9Sstevel@tonic-gate 
41127c478bd9Sstevel@tonic-gate 		if ((off = isenhsc(statmsg + 4, ' ')) > 0)
41137c478bd9Sstevel@tonic-gate 		{
41147c478bd9Sstevel@tonic-gate 			if (dsn == NULL)
41157c478bd9Sstevel@tonic-gate 			{
4116058561cbSjbeck 				(void) sm_snprintf(dsnbuf, sizeof(dsnbuf),
41177c478bd9Sstevel@tonic-gate 						   "%.*s", off, statmsg + 4);
41187c478bd9Sstevel@tonic-gate 				dsn = dsnbuf;
41197c478bd9Sstevel@tonic-gate 			}
41207c478bd9Sstevel@tonic-gate 			off += 5;
41217c478bd9Sstevel@tonic-gate 		}
41227c478bd9Sstevel@tonic-gate 		else
41237c478bd9Sstevel@tonic-gate 		{
41247c478bd9Sstevel@tonic-gate 			off = 4;
41257c478bd9Sstevel@tonic-gate 		}
41267c478bd9Sstevel@tonic-gate 		message("%s", statmsg + off);
41277c478bd9Sstevel@tonic-gate 		if (status == EX_TEMPFAIL && e->e_xfp != NULL)
41287c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT, "%s\n",
41297c478bd9Sstevel@tonic-gate 					     &MsgBuf[4]);
41307c478bd9Sstevel@tonic-gate 	}
41317c478bd9Sstevel@tonic-gate 	else
41327c478bd9Sstevel@tonic-gate 	{
41337c478bd9Sstevel@tonic-gate 		char mbuf[ENHSCLEN + 4];
41347c478bd9Sstevel@tonic-gate 
41357c478bd9Sstevel@tonic-gate 		Errors++;
41367c478bd9Sstevel@tonic-gate 		if ((off = isenhsc(statmsg + 4, ' ')) > 0 &&
4137058561cbSjbeck 		    off < sizeof(mbuf) - 4)
41387c478bd9Sstevel@tonic-gate 		{
41397c478bd9Sstevel@tonic-gate 			if (dsn == NULL)
41407c478bd9Sstevel@tonic-gate 			{
4141058561cbSjbeck 				(void) sm_snprintf(dsnbuf, sizeof(dsnbuf),
41427c478bd9Sstevel@tonic-gate 						   "%.*s", off, statmsg + 4);
41437c478bd9Sstevel@tonic-gate 				dsn = dsnbuf;
41447c478bd9Sstevel@tonic-gate 			}
41457c478bd9Sstevel@tonic-gate 			off += 5;
41467c478bd9Sstevel@tonic-gate 
41477c478bd9Sstevel@tonic-gate 			/* copy only part of statmsg to mbuf */
41487c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(mbuf, statmsg, off);
4149058561cbSjbeck 			(void) sm_strlcat(mbuf, " %s", sizeof(mbuf));
41507c478bd9Sstevel@tonic-gate 		}
41517c478bd9Sstevel@tonic-gate 		else
41527c478bd9Sstevel@tonic-gate 		{
41537c478bd9Sstevel@tonic-gate 			dsnbuf[0] = '\0';
4154058561cbSjbeck 			(void) sm_snprintf(mbuf, sizeof(mbuf), "%.3s %%s",
41557c478bd9Sstevel@tonic-gate 					   statmsg);
41567c478bd9Sstevel@tonic-gate 			off = 4;
41577c478bd9Sstevel@tonic-gate 		}
41587c478bd9Sstevel@tonic-gate 		usrerr(mbuf, &statmsg[off]);
41597c478bd9Sstevel@tonic-gate 	}
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate 	/*
41627c478bd9Sstevel@tonic-gate 	**  Final cleanup.
41637c478bd9Sstevel@tonic-gate 	**	Log a record of the transaction.  Compute the new
41647c478bd9Sstevel@tonic-gate 	**	ExitStat -- if we already had an error, stick with
41657c478bd9Sstevel@tonic-gate 	**	that.
41667c478bd9Sstevel@tonic-gate 	*/
41677c478bd9Sstevel@tonic-gate 
41687c478bd9Sstevel@tonic-gate 	if (OpMode != MD_VERIFY && !bitset(EF_VRFYONLY, e->e_flags) &&
41697c478bd9Sstevel@tonic-gate 	    LogLevel > ((status == EX_TEMPFAIL) ? 8 : (status == EX_OK) ? 7 : 6))
41707c478bd9Sstevel@tonic-gate 		logdelivery(m, mci, dsn, statmsg + off, ctladdr, xstart, e);
41717c478bd9Sstevel@tonic-gate 
41727c478bd9Sstevel@tonic-gate 	if (tTd(11, 2))
41737c478bd9Sstevel@tonic-gate 		sm_dprintf("giveresponse: status=%d, dsn=%s, e->e_message=%s, errnum=%d\n",
41747c478bd9Sstevel@tonic-gate 			   status,
41757c478bd9Sstevel@tonic-gate 			   dsn == NULL ? "<NULL>" : dsn,
41767c478bd9Sstevel@tonic-gate 			   e->e_message == NULL ? "<NULL>" : e->e_message,
41777c478bd9Sstevel@tonic-gate 			   errnum);
41787c478bd9Sstevel@tonic-gate 
41797c478bd9Sstevel@tonic-gate 	if (status != EX_TEMPFAIL)
41807c478bd9Sstevel@tonic-gate 		setstat(status);
41817c478bd9Sstevel@tonic-gate 	if (status != EX_OK && (status != EX_TEMPFAIL || e->e_message == NULL))
41827c478bd9Sstevel@tonic-gate 		e->e_message = sm_rpool_strdup_x(e->e_rpool, statmsg + off);
41837c478bd9Sstevel@tonic-gate 	if (status != EX_OK && to != NULL && to->q_message == NULL)
41847c478bd9Sstevel@tonic-gate 	{
41857c478bd9Sstevel@tonic-gate 		if (!usestat && e->e_message != NULL)
41867c478bd9Sstevel@tonic-gate 			to->q_message = sm_rpool_strdup_x(e->e_rpool,
41877c478bd9Sstevel@tonic-gate 							  e->e_message);
41887c478bd9Sstevel@tonic-gate 		else
41897c478bd9Sstevel@tonic-gate 			to->q_message = sm_rpool_strdup_x(e->e_rpool,
41907c478bd9Sstevel@tonic-gate 							  statmsg + off);
41917c478bd9Sstevel@tonic-gate 	}
41927c478bd9Sstevel@tonic-gate 	errno = 0;
41937c478bd9Sstevel@tonic-gate 	SM_SET_H_ERRNO(0);
41947c478bd9Sstevel@tonic-gate }
41957c478bd9Sstevel@tonic-gate /*
41967c478bd9Sstevel@tonic-gate **  LOGDELIVERY -- log the delivery in the system log
41977c478bd9Sstevel@tonic-gate **
41987c478bd9Sstevel@tonic-gate **	Care is taken to avoid logging lines that are too long, because
41997c478bd9Sstevel@tonic-gate **	some versions of syslog have an unfortunate proclivity for core
42007c478bd9Sstevel@tonic-gate **	dumping.  This is a hack, to be sure, that is at best empirical.
42017c478bd9Sstevel@tonic-gate **
42027c478bd9Sstevel@tonic-gate **	Parameters:
42037c478bd9Sstevel@tonic-gate **		m -- the mailer info.  Can be NULL for initial queue.
42047c478bd9Sstevel@tonic-gate **		mci -- the mailer connection info -- can be NULL if the
42057c478bd9Sstevel@tonic-gate **			log is occurring when no connection is active.
42067c478bd9Sstevel@tonic-gate **		dsn -- the DSN attached to the status.
42077c478bd9Sstevel@tonic-gate **		status -- the message to print for the status.
42087c478bd9Sstevel@tonic-gate **		ctladdr -- the controlling address for the to list.
42097c478bd9Sstevel@tonic-gate **		xstart -- the transaction start time, used for
42107c478bd9Sstevel@tonic-gate **			computing transaction delay.
42117c478bd9Sstevel@tonic-gate **		e -- the current envelope.
42127c478bd9Sstevel@tonic-gate **
42137c478bd9Sstevel@tonic-gate **	Returns:
42147c478bd9Sstevel@tonic-gate **		none
42157c478bd9Sstevel@tonic-gate **
42167c478bd9Sstevel@tonic-gate **	Side Effects:
42177c478bd9Sstevel@tonic-gate **		none
42187c478bd9Sstevel@tonic-gate */
42197c478bd9Sstevel@tonic-gate 
42207c478bd9Sstevel@tonic-gate void
42217c478bd9Sstevel@tonic-gate logdelivery(m, mci, dsn, status, ctladdr, xstart, e)
42227c478bd9Sstevel@tonic-gate 	MAILER *m;
42237c478bd9Sstevel@tonic-gate 	register MCI *mci;
42247c478bd9Sstevel@tonic-gate 	char *dsn;
42257c478bd9Sstevel@tonic-gate 	const char *status;
42267c478bd9Sstevel@tonic-gate 	ADDRESS *ctladdr;
42277c478bd9Sstevel@tonic-gate 	time_t xstart;
42287c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
42297c478bd9Sstevel@tonic-gate {
42307c478bd9Sstevel@tonic-gate 	register char *bp;
42317c478bd9Sstevel@tonic-gate 	register char *p;
42327c478bd9Sstevel@tonic-gate 	int l;
42337c478bd9Sstevel@tonic-gate 	time_t now = curtime();
42347c478bd9Sstevel@tonic-gate 	char buf[1024];
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate #if (SYSLOG_BUFSIZE) >= 256
42377c478bd9Sstevel@tonic-gate 	/* ctladdr: max 106 bytes */
42387c478bd9Sstevel@tonic-gate 	bp = buf;
42397c478bd9Sstevel@tonic-gate 	if (ctladdr != NULL)
42407c478bd9Sstevel@tonic-gate 	{
42417c478bd9Sstevel@tonic-gate 		(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2, ", ctladdr=",
42427c478bd9Sstevel@tonic-gate 				   shortenstring(ctladdr->q_paddr, 83));
42437c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
42447c478bd9Sstevel@tonic-gate 		if (bitset(QGOODUID, ctladdr->q_flags))
42457c478bd9Sstevel@tonic-gate 		{
42467c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(bp, SPACELEFT(buf, bp), " (%d/%d)",
42477c478bd9Sstevel@tonic-gate 					   (int) ctladdr->q_uid,
42487c478bd9Sstevel@tonic-gate 					   (int) ctladdr->q_gid);
42497c478bd9Sstevel@tonic-gate 			bp += strlen(bp);
42507c478bd9Sstevel@tonic-gate 		}
42517c478bd9Sstevel@tonic-gate 	}
42527c478bd9Sstevel@tonic-gate 
42537c478bd9Sstevel@tonic-gate 	/* delay & xdelay: max 41 bytes */
42547c478bd9Sstevel@tonic-gate 	(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2, ", delay=",
42557c478bd9Sstevel@tonic-gate 			   pintvl(now - e->e_ctime, true));
42567c478bd9Sstevel@tonic-gate 	bp += strlen(bp);
42577c478bd9Sstevel@tonic-gate 
42587c478bd9Sstevel@tonic-gate 	if (xstart != (time_t) 0)
42597c478bd9Sstevel@tonic-gate 	{
42607c478bd9Sstevel@tonic-gate 		(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2, ", xdelay=",
42617c478bd9Sstevel@tonic-gate 				   pintvl(now - xstart, true));
42627c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
42637c478bd9Sstevel@tonic-gate 	}
42647c478bd9Sstevel@tonic-gate 
42657c478bd9Sstevel@tonic-gate 	/* mailer: assume about 19 bytes (max 10 byte mailer name) */
42667c478bd9Sstevel@tonic-gate 	if (m != NULL)
42677c478bd9Sstevel@tonic-gate 	{
42687c478bd9Sstevel@tonic-gate 		(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2, ", mailer=",
42697c478bd9Sstevel@tonic-gate 				   m->m_name);
42707c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
42717c478bd9Sstevel@tonic-gate 	}
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 	/* pri: changes with each delivery attempt */
42747c478bd9Sstevel@tonic-gate 	(void) sm_snprintf(bp, SPACELEFT(buf, bp), ", pri=%ld",
42757c478bd9Sstevel@tonic-gate 		e->e_msgpriority);
42767c478bd9Sstevel@tonic-gate 	bp += strlen(bp);
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate 	/* relay: max 66 bytes for IPv4 addresses */
42797c478bd9Sstevel@tonic-gate 	if (mci != NULL && mci->mci_host != NULL)
42807c478bd9Sstevel@tonic-gate 	{
42817c478bd9Sstevel@tonic-gate 		extern SOCKADDR CurHostAddr;
42827c478bd9Sstevel@tonic-gate 
42837c478bd9Sstevel@tonic-gate 		(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2, ", relay=",
42847c478bd9Sstevel@tonic-gate 				   shortenstring(mci->mci_host, 40));
42857c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
42867c478bd9Sstevel@tonic-gate 
42877c478bd9Sstevel@tonic-gate 		if (CurHostAddr.sa.sa_family != 0)
42887c478bd9Sstevel@tonic-gate 		{
42897c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(bp, SPACELEFT(buf, bp), " [%s]",
42907c478bd9Sstevel@tonic-gate 					   anynet_ntoa(&CurHostAddr));
42917c478bd9Sstevel@tonic-gate 		}
42927c478bd9Sstevel@tonic-gate 	}
42937c478bd9Sstevel@tonic-gate 	else if (strcmp(status, "quarantined") == 0)
42947c478bd9Sstevel@tonic-gate 	{
42957c478bd9Sstevel@tonic-gate 		if (e->e_quarmsg != NULL)
42967c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(bp, SPACELEFT(buf, bp),
42977c478bd9Sstevel@tonic-gate 					   ", quarantine=%s",
42987c478bd9Sstevel@tonic-gate 					   shortenstring(e->e_quarmsg, 40));
42997c478bd9Sstevel@tonic-gate 	}
43007c478bd9Sstevel@tonic-gate 	else if (strcmp(status, "queued") != 0)
43017c478bd9Sstevel@tonic-gate 	{
43027c478bd9Sstevel@tonic-gate 		p = macvalue('h', e);
43037c478bd9Sstevel@tonic-gate 		if (p != NULL && p[0] != '\0')
43047c478bd9Sstevel@tonic-gate 		{
43057c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(bp, SPACELEFT(buf, bp),
43067c478bd9Sstevel@tonic-gate 					   ", relay=%s", shortenstring(p, 40));
43077c478bd9Sstevel@tonic-gate 		}
43087c478bd9Sstevel@tonic-gate 	}
43097c478bd9Sstevel@tonic-gate 	bp += strlen(bp);
43107c478bd9Sstevel@tonic-gate 
43117c478bd9Sstevel@tonic-gate 	/* dsn */
43127c478bd9Sstevel@tonic-gate 	if (dsn != NULL && *dsn != '\0')
43137c478bd9Sstevel@tonic-gate 	{
43147c478bd9Sstevel@tonic-gate 		(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2, ", dsn=",
43157c478bd9Sstevel@tonic-gate 				   shortenstring(dsn, ENHSCLEN));
43167c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
43177c478bd9Sstevel@tonic-gate 	}
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate #if _FFR_LOG_NTRIES
43207c478bd9Sstevel@tonic-gate 	/* ntries */
43217c478bd9Sstevel@tonic-gate 	if (e->e_ntries >= 0)
43227c478bd9Sstevel@tonic-gate 	{
43237c478bd9Sstevel@tonic-gate 		(void) sm_snprintf(bp, SPACELEFT(buf, bp),
43247c478bd9Sstevel@tonic-gate 				   ", ntries=%d", e->e_ntries + 1);
43257c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
43267c478bd9Sstevel@tonic-gate 	}
43277c478bd9Sstevel@tonic-gate #endif /* _FFR_LOG_NTRIES */
43287c478bd9Sstevel@tonic-gate 
43297c478bd9Sstevel@tonic-gate # define STATLEN		(((SYSLOG_BUFSIZE) - 100) / 4)
43307c478bd9Sstevel@tonic-gate # if (STATLEN) < 63
43317c478bd9Sstevel@tonic-gate #  undef STATLEN
43327c478bd9Sstevel@tonic-gate #  define STATLEN	63
43337c478bd9Sstevel@tonic-gate # endif /* (STATLEN) < 63 */
43347c478bd9Sstevel@tonic-gate # if (STATLEN) > 203
43357c478bd9Sstevel@tonic-gate #  undef STATLEN
43367c478bd9Sstevel@tonic-gate #  define STATLEN	203
43377c478bd9Sstevel@tonic-gate # endif /* (STATLEN) > 203 */
43387c478bd9Sstevel@tonic-gate 
43397c478bd9Sstevel@tonic-gate 	/* stat: max 210 bytes */
4340058561cbSjbeck 	if ((bp - buf) > (sizeof(buf) - ((STATLEN) + 20)))
43417c478bd9Sstevel@tonic-gate 	{
43427c478bd9Sstevel@tonic-gate 		/* desperation move -- truncate data */
4343058561cbSjbeck 		bp = buf + sizeof(buf) - ((STATLEN) + 17);
43447c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(bp, "...", SPACELEFT(buf, bp));
43457c478bd9Sstevel@tonic-gate 		bp += 3;
43467c478bd9Sstevel@tonic-gate 	}
43477c478bd9Sstevel@tonic-gate 
43487c478bd9Sstevel@tonic-gate 	(void) sm_strlcpy(bp, ", stat=", SPACELEFT(buf, bp));
43497c478bd9Sstevel@tonic-gate 	bp += strlen(bp);
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate 	(void) sm_strlcpy(bp, shortenstring(status, STATLEN),
43527c478bd9Sstevel@tonic-gate 			  SPACELEFT(buf, bp));
43537c478bd9Sstevel@tonic-gate 
43547c478bd9Sstevel@tonic-gate 	/* id, to: max 13 + TOBUFSIZE bytes */
43557c478bd9Sstevel@tonic-gate 	l = SYSLOG_BUFSIZE - 100 - strlen(buf);
43567c478bd9Sstevel@tonic-gate 	if (l < 0)
43577c478bd9Sstevel@tonic-gate 		l = 0;
43587c478bd9Sstevel@tonic-gate 	p = e->e_to == NULL ? "NO-TO-LIST" : e->e_to;
43597c478bd9Sstevel@tonic-gate 	while (strlen(p) >= l)
43607c478bd9Sstevel@tonic-gate 	{
43617c478bd9Sstevel@tonic-gate 		register char *q;
43627c478bd9Sstevel@tonic-gate 
43637c478bd9Sstevel@tonic-gate 		for (q = p + l; q > p; q--)
43647c478bd9Sstevel@tonic-gate 		{
43657c478bd9Sstevel@tonic-gate 			if (*q == ',')
43667c478bd9Sstevel@tonic-gate 				break;
43677c478bd9Sstevel@tonic-gate 		}
43687c478bd9Sstevel@tonic-gate 		if (p == q)
43697c478bd9Sstevel@tonic-gate 			break;
43707c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_INFO, e->e_id, "to=%.*s [more]%s",
43717c478bd9Sstevel@tonic-gate 			  (int) (++q - p), p, buf);
43727c478bd9Sstevel@tonic-gate 		p = q;
43737c478bd9Sstevel@tonic-gate 	}
43747c478bd9Sstevel@tonic-gate 	sm_syslog(LOG_INFO, e->e_id, "to=%.*s%s", l, p, buf);
43757c478bd9Sstevel@tonic-gate 
43767c478bd9Sstevel@tonic-gate #else /* (SYSLOG_BUFSIZE) >= 256 */
43777c478bd9Sstevel@tonic-gate 
43787c478bd9Sstevel@tonic-gate 	l = SYSLOG_BUFSIZE - 85;
43797c478bd9Sstevel@tonic-gate 	if (l < 0)
43807c478bd9Sstevel@tonic-gate 		l = 0;
43817c478bd9Sstevel@tonic-gate 	p = e->e_to == NULL ? "NO-TO-LIST" : e->e_to;
43827c478bd9Sstevel@tonic-gate 	while (strlen(p) >= l)
43837c478bd9Sstevel@tonic-gate 	{
43847c478bd9Sstevel@tonic-gate 		register char *q;
43857c478bd9Sstevel@tonic-gate 
43867c478bd9Sstevel@tonic-gate 		for (q = p + l; q > p; q--)
43877c478bd9Sstevel@tonic-gate 		{
43887c478bd9Sstevel@tonic-gate 			if (*q == ',')
43897c478bd9Sstevel@tonic-gate 				break;
43907c478bd9Sstevel@tonic-gate 		}
43917c478bd9Sstevel@tonic-gate 		if (p == q)
43927c478bd9Sstevel@tonic-gate 			break;
43937c478bd9Sstevel@tonic-gate 
43947c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_INFO, e->e_id, "to=%.*s [more]",
43957c478bd9Sstevel@tonic-gate 			  (int) (++q - p), p);
43967c478bd9Sstevel@tonic-gate 		p = q;
43977c478bd9Sstevel@tonic-gate 	}
43987c478bd9Sstevel@tonic-gate 	sm_syslog(LOG_INFO, e->e_id, "to=%.*s", l, p);
43997c478bd9Sstevel@tonic-gate 
44007c478bd9Sstevel@tonic-gate 	if (ctladdr != NULL)
44017c478bd9Sstevel@tonic-gate 	{
44027c478bd9Sstevel@tonic-gate 		bp = buf;
44037c478bd9Sstevel@tonic-gate 		(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2, "ctladdr=",
44047c478bd9Sstevel@tonic-gate 				   shortenstring(ctladdr->q_paddr, 83));
44057c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
44067c478bd9Sstevel@tonic-gate 		if (bitset(QGOODUID, ctladdr->q_flags))
44077c478bd9Sstevel@tonic-gate 		{
44087c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(bp, SPACELEFT(buf, bp), " (%d/%d)",
44097c478bd9Sstevel@tonic-gate 					   ctladdr->q_uid, ctladdr->q_gid);
44107c478bd9Sstevel@tonic-gate 			bp += strlen(bp);
44117c478bd9Sstevel@tonic-gate 		}
44127c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_INFO, e->e_id, "%s", buf);
44137c478bd9Sstevel@tonic-gate 	}
44147c478bd9Sstevel@tonic-gate 	bp = buf;
44157c478bd9Sstevel@tonic-gate 	(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2, "delay=",
44167c478bd9Sstevel@tonic-gate 			   pintvl(now - e->e_ctime, true));
44177c478bd9Sstevel@tonic-gate 	bp += strlen(bp);
44187c478bd9Sstevel@tonic-gate 	if (xstart != (time_t) 0)
44197c478bd9Sstevel@tonic-gate 	{
44207c478bd9Sstevel@tonic-gate 		(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2, ", xdelay=",
44217c478bd9Sstevel@tonic-gate 				   pintvl(now - xstart, true));
44227c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
44237c478bd9Sstevel@tonic-gate 	}
44247c478bd9Sstevel@tonic-gate 
44257c478bd9Sstevel@tonic-gate 	if (m != NULL)
44267c478bd9Sstevel@tonic-gate 	{
44277c478bd9Sstevel@tonic-gate 		(void) sm_strlcpyn(bp, SPACELEFT(buf, bp), 2, ", mailer=",
44287c478bd9Sstevel@tonic-gate 				   m->m_name);
44297c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
44307c478bd9Sstevel@tonic-gate 	}
44317c478bd9Sstevel@tonic-gate 	sm_syslog(LOG_INFO, e->e_id, "%.1000s", buf);
44327c478bd9Sstevel@tonic-gate 
44337c478bd9Sstevel@tonic-gate 	buf[0] = '\0';
44347c478bd9Sstevel@tonic-gate 	bp = buf;
44357c478bd9Sstevel@tonic-gate 	if (mci != NULL && mci->mci_host != NULL)
44367c478bd9Sstevel@tonic-gate 	{
44377c478bd9Sstevel@tonic-gate 		extern SOCKADDR CurHostAddr;
44387c478bd9Sstevel@tonic-gate 
44397c478bd9Sstevel@tonic-gate 		(void) sm_snprintf(bp, SPACELEFT(buf, bp), "relay=%.100s",
44407c478bd9Sstevel@tonic-gate 				   mci->mci_host);
44417c478bd9Sstevel@tonic-gate 		bp += strlen(bp);
44427c478bd9Sstevel@tonic-gate 
44437c478bd9Sstevel@tonic-gate 		if (CurHostAddr.sa.sa_family != 0)
44447c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(bp, SPACELEFT(buf, bp),
44457c478bd9Sstevel@tonic-gate 					   " [%.100s]",
44467c478bd9Sstevel@tonic-gate 					   anynet_ntoa(&CurHostAddr));
44477c478bd9Sstevel@tonic-gate 	}
44487c478bd9Sstevel@tonic-gate 	else if (strcmp(status, "quarantined") == 0)
44497c478bd9Sstevel@tonic-gate 	{
44507c478bd9Sstevel@tonic-gate 		if (e->e_quarmsg != NULL)
44517c478bd9Sstevel@tonic-gate 			(void) sm_snprintf(bp, SPACELEFT(buf, bp),
44527c478bd9Sstevel@tonic-gate 					   ", quarantine=%.100s",
44537c478bd9Sstevel@tonic-gate 					   e->e_quarmsg);
44547c478bd9Sstevel@tonic-gate 	}
44557c478bd9Sstevel@tonic-gate 	else if (strcmp(status, "queued") != 0)
44567c478bd9Sstevel@tonic-gate 	{
44577c478bd9Sstevel@tonic-gate 		p = macvalue('h', e);
44587c478bd9Sstevel@tonic-gate 		if (p != NULL && p[0] != '\0')
4459058561cbSjbeck 			(void) sm_snprintf(buf, sizeof(buf), "relay=%.100s", p);
44607c478bd9Sstevel@tonic-gate 	}
44617c478bd9Sstevel@tonic-gate 	if (buf[0] != '\0')
44627c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_INFO, e->e_id, "%.1000s", buf);
44637c478bd9Sstevel@tonic-gate 
44647c478bd9Sstevel@tonic-gate 	sm_syslog(LOG_INFO, e->e_id, "stat=%s", shortenstring(status, 63));
44657c478bd9Sstevel@tonic-gate #endif /* (SYSLOG_BUFSIZE) >= 256 */
44667c478bd9Sstevel@tonic-gate }
44677c478bd9Sstevel@tonic-gate /*
44687c478bd9Sstevel@tonic-gate **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
44697c478bd9Sstevel@tonic-gate **
44707c478bd9Sstevel@tonic-gate **	This can be made an arbitrary message separator by changing $l
44717c478bd9Sstevel@tonic-gate **
44727c478bd9Sstevel@tonic-gate **	One of the ugliest hacks seen by human eyes is contained herein:
44737c478bd9Sstevel@tonic-gate **	UUCP wants those stupid "remote from <host>" lines.  Why oh why
44747c478bd9Sstevel@tonic-gate **	does a well-meaning programmer such as myself have to deal with
44757c478bd9Sstevel@tonic-gate **	this kind of antique garbage????
44767c478bd9Sstevel@tonic-gate **
44777c478bd9Sstevel@tonic-gate **	Parameters:
44787c478bd9Sstevel@tonic-gate **		mci -- the connection information.
44797c478bd9Sstevel@tonic-gate **		e -- the envelope.
44807c478bd9Sstevel@tonic-gate **
44817c478bd9Sstevel@tonic-gate **	Returns:
4482445f2479Sjbeck **		true iff line was written successfully
44837c478bd9Sstevel@tonic-gate **
44847c478bd9Sstevel@tonic-gate **	Side Effects:
44857c478bd9Sstevel@tonic-gate **		outputs some text to fp.
44867c478bd9Sstevel@tonic-gate */
44877c478bd9Sstevel@tonic-gate 
4488445f2479Sjbeck bool
44897c478bd9Sstevel@tonic-gate putfromline(mci, e)
44907c478bd9Sstevel@tonic-gate 	register MCI *mci;
44917c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
44927c478bd9Sstevel@tonic-gate {
44937c478bd9Sstevel@tonic-gate 	char *template = UnixFromLine;
44947c478bd9Sstevel@tonic-gate 	char buf[MAXLINE];
44957c478bd9Sstevel@tonic-gate 	char xbuf[MAXLINE];
44967c478bd9Sstevel@tonic-gate 
44977c478bd9Sstevel@tonic-gate 	if (bitnset(M_NHDR, mci->mci_mailer->m_flags))
4498445f2479Sjbeck 		return true;
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 	mci->mci_flags |= MCIF_INHEADER;
45017c478bd9Sstevel@tonic-gate 
45027c478bd9Sstevel@tonic-gate 	if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags))
45037c478bd9Sstevel@tonic-gate 	{
45047c478bd9Sstevel@tonic-gate 		char *bang;
45057c478bd9Sstevel@tonic-gate 
4506058561cbSjbeck 		expand("\201g", buf, sizeof(buf), e);
45077c478bd9Sstevel@tonic-gate 		bang = strchr(buf, '!');
45087c478bd9Sstevel@tonic-gate 		if (bang == NULL)
45097c478bd9Sstevel@tonic-gate 		{
45107c478bd9Sstevel@tonic-gate 			char *at;
45117c478bd9Sstevel@tonic-gate 			char hname[MAXNAME];
45127c478bd9Sstevel@tonic-gate 
45137c478bd9Sstevel@tonic-gate 			/*
45147c478bd9Sstevel@tonic-gate 			**  If we can construct a UUCP path, do so
45157c478bd9Sstevel@tonic-gate 			*/
45167c478bd9Sstevel@tonic-gate 
45177c478bd9Sstevel@tonic-gate 			at = strrchr(buf, '@');
45187c478bd9Sstevel@tonic-gate 			if (at == NULL)
45197c478bd9Sstevel@tonic-gate 			{
4520058561cbSjbeck 				expand("\201k", hname, sizeof(hname), e);
45217c478bd9Sstevel@tonic-gate 				at = hname;
45227c478bd9Sstevel@tonic-gate 			}
45237c478bd9Sstevel@tonic-gate 			else
45247c478bd9Sstevel@tonic-gate 				*at++ = '\0';
4525058561cbSjbeck 			(void) sm_snprintf(xbuf, sizeof(xbuf),
45267c478bd9Sstevel@tonic-gate 					   "From %.800s  \201d remote from %.100s\n",
45277c478bd9Sstevel@tonic-gate 					   buf, at);
45287c478bd9Sstevel@tonic-gate 		}
45297c478bd9Sstevel@tonic-gate 		else
45307c478bd9Sstevel@tonic-gate 		{
45317c478bd9Sstevel@tonic-gate 			*bang++ = '\0';
4532058561cbSjbeck 			(void) sm_snprintf(xbuf, sizeof(xbuf),
45337c478bd9Sstevel@tonic-gate 					   "From %.800s  \201d remote from %.100s\n",
45347c478bd9Sstevel@tonic-gate 					   bang, buf);
45357c478bd9Sstevel@tonic-gate 			template = xbuf;
45367c478bd9Sstevel@tonic-gate 		}
45377c478bd9Sstevel@tonic-gate 	}
4538058561cbSjbeck 	expand(template, buf, sizeof(buf), e);
4539445f2479Sjbeck 	return putxline(buf, strlen(buf), mci, PXLF_HEADER);
45407c478bd9Sstevel@tonic-gate }
4541445f2479Sjbeck 
45427c478bd9Sstevel@tonic-gate /*
45437c478bd9Sstevel@tonic-gate **  PUTBODY -- put the body of a message.
45447c478bd9Sstevel@tonic-gate **
45457c478bd9Sstevel@tonic-gate **	Parameters:
45467c478bd9Sstevel@tonic-gate **		mci -- the connection information.
45477c478bd9Sstevel@tonic-gate **		e -- the envelope to put out.
45487c478bd9Sstevel@tonic-gate **		separator -- if non-NULL, a message separator that must
45497c478bd9Sstevel@tonic-gate **			not be permitted in the resulting message.
45507c478bd9Sstevel@tonic-gate **
45517c478bd9Sstevel@tonic-gate **	Returns:
4552445f2479Sjbeck **		true iff message was written successfully
45537c478bd9Sstevel@tonic-gate **
45547c478bd9Sstevel@tonic-gate **	Side Effects:
45557c478bd9Sstevel@tonic-gate **		The message is written onto fp.
45567c478bd9Sstevel@tonic-gate */
45577c478bd9Sstevel@tonic-gate 
45587c478bd9Sstevel@tonic-gate /* values for output state variable */
455949218d4fSjbeck #define OSTATE_HEAD	0	/* at beginning of line */
456049218d4fSjbeck #define OSTATE_CR	1	/* read a carriage return */
456149218d4fSjbeck #define OSTATE_INLINE	2	/* putting rest of line */
45627c478bd9Sstevel@tonic-gate 
4563445f2479Sjbeck bool
45647c478bd9Sstevel@tonic-gate putbody(mci, e, separator)
45657c478bd9Sstevel@tonic-gate 	register MCI *mci;
45667c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
45677c478bd9Sstevel@tonic-gate 	char *separator;
45687c478bd9Sstevel@tonic-gate {
45697c478bd9Sstevel@tonic-gate 	bool dead = false;
4570445f2479Sjbeck 	bool ioerr = false;
4571445f2479Sjbeck 	int save_errno;
45727c478bd9Sstevel@tonic-gate 	char buf[MAXLINE];
45737c478bd9Sstevel@tonic-gate #if MIME8TO7
45747c478bd9Sstevel@tonic-gate 	char *boundaries[MAXMIMENESTING + 1];
45757c478bd9Sstevel@tonic-gate #endif /* MIME8TO7 */
45767c478bd9Sstevel@tonic-gate 
45777c478bd9Sstevel@tonic-gate 	/*
45787c478bd9Sstevel@tonic-gate 	**  Output the body of the message
45797c478bd9Sstevel@tonic-gate 	*/
45807c478bd9Sstevel@tonic-gate 
45817c478bd9Sstevel@tonic-gate 	if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags))
45827c478bd9Sstevel@tonic-gate 	{
45837c478bd9Sstevel@tonic-gate 		char *df = queuename(e, DATAFL_LETTER);
45847c478bd9Sstevel@tonic-gate 
45857c478bd9Sstevel@tonic-gate 		e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, df,
45867c478bd9Sstevel@tonic-gate 				      SM_IO_RDONLY_B, NULL);
45877c478bd9Sstevel@tonic-gate 		if (e->e_dfp == NULL)
45887c478bd9Sstevel@tonic-gate 		{
45897c478bd9Sstevel@tonic-gate 			char *msg = "!putbody: Cannot open %s for %s from %s";
45907c478bd9Sstevel@tonic-gate 
45917c478bd9Sstevel@tonic-gate 			if (errno == ENOENT)
45927c478bd9Sstevel@tonic-gate 				msg++;
45937c478bd9Sstevel@tonic-gate 			syserr(msg, df, e->e_to, e->e_from.q_paddr);
45947c478bd9Sstevel@tonic-gate 		}
45957c478bd9Sstevel@tonic-gate 
45967c478bd9Sstevel@tonic-gate 	}
45977c478bd9Sstevel@tonic-gate 	if (e->e_dfp == NULL)
45987c478bd9Sstevel@tonic-gate 	{
45997c478bd9Sstevel@tonic-gate 		if (bitset(MCIF_INHEADER, mci->mci_flags))
46007c478bd9Sstevel@tonic-gate 		{
4601445f2479Sjbeck 			if (!putline("", mci))
4602445f2479Sjbeck 				goto writeerr;
46037c478bd9Sstevel@tonic-gate 			mci->mci_flags &= ~MCIF_INHEADER;
46047c478bd9Sstevel@tonic-gate 		}
4605445f2479Sjbeck 		if (!putline("<<< No Message Collected >>>", mci))
4606445f2479Sjbeck 			goto writeerr;
46077c478bd9Sstevel@tonic-gate 		goto endofmessage;
46087c478bd9Sstevel@tonic-gate 	}
46097c478bd9Sstevel@tonic-gate 
46107c478bd9Sstevel@tonic-gate 	if (e->e_dfino == (ino_t) 0)
46117c478bd9Sstevel@tonic-gate 	{
46127c478bd9Sstevel@tonic-gate 		struct stat stbuf;
46137c478bd9Sstevel@tonic-gate 
46147c478bd9Sstevel@tonic-gate 		if (fstat(sm_io_getinfo(e->e_dfp, SM_IO_WHAT_FD, NULL), &stbuf)
46157c478bd9Sstevel@tonic-gate 		    < 0)
46167c478bd9Sstevel@tonic-gate 			e->e_dfino = -1;
46177c478bd9Sstevel@tonic-gate 		else
46187c478bd9Sstevel@tonic-gate 		{
46197c478bd9Sstevel@tonic-gate 			e->e_dfdev = stbuf.st_dev;
46207c478bd9Sstevel@tonic-gate 			e->e_dfino = stbuf.st_ino;
46217c478bd9Sstevel@tonic-gate 		}
46227c478bd9Sstevel@tonic-gate 	}
46237c478bd9Sstevel@tonic-gate 
46247c478bd9Sstevel@tonic-gate 	/* paranoia: the data file should always be in a rewound state */
46257c478bd9Sstevel@tonic-gate 	(void) bfrewind(e->e_dfp);
46267c478bd9Sstevel@tonic-gate 
4627445f2479Sjbeck 	/* simulate an I/O timeout when used as source */
4628445f2479Sjbeck 	if (tTd(84, 101))
4629445f2479Sjbeck 		sleep(319);
4630445f2479Sjbeck 
46317c478bd9Sstevel@tonic-gate #if MIME8TO7
46327c478bd9Sstevel@tonic-gate 	if (bitset(MCIF_CVT8TO7, mci->mci_flags))
46337c478bd9Sstevel@tonic-gate 	{
46347c478bd9Sstevel@tonic-gate 		/*
46357c478bd9Sstevel@tonic-gate 		**  Do 8 to 7 bit MIME conversion.
46367c478bd9Sstevel@tonic-gate 		*/
46377c478bd9Sstevel@tonic-gate 
46387c478bd9Sstevel@tonic-gate 		/* make sure it looks like a MIME message */
4639445f2479Sjbeck 		if (hvalue("MIME-Version", e->e_header) == NULL &&
4640445f2479Sjbeck 		    !putline("MIME-Version: 1.0", mci))
4641445f2479Sjbeck 			goto writeerr;
46427c478bd9Sstevel@tonic-gate 
46437c478bd9Sstevel@tonic-gate 		if (hvalue("Content-Type", e->e_header) == NULL)
46447c478bd9Sstevel@tonic-gate 		{
4645058561cbSjbeck 			(void) sm_snprintf(buf, sizeof(buf),
46467c478bd9Sstevel@tonic-gate 					   "Content-Type: text/plain; charset=%s",
46477c478bd9Sstevel@tonic-gate 					   defcharset(e));
4648445f2479Sjbeck 			if (!putline(buf, mci))
4649445f2479Sjbeck 				goto writeerr;
46507c478bd9Sstevel@tonic-gate 		}
46517c478bd9Sstevel@tonic-gate 
46527c478bd9Sstevel@tonic-gate 		/* now do the hard work */
46537c478bd9Sstevel@tonic-gate 		boundaries[0] = NULL;
46547c478bd9Sstevel@tonic-gate 		mci->mci_flags |= MCIF_INHEADER;
46553ee0e492Sjbeck 		if (mime8to7(mci, e->e_header, e, boundaries, M87F_OUTER, 0) ==
4656445f2479Sjbeck 								SM_IO_EOF)
4657445f2479Sjbeck 			goto writeerr;
46587c478bd9Sstevel@tonic-gate 	}
46597c478bd9Sstevel@tonic-gate # if MIME7TO8
46607c478bd9Sstevel@tonic-gate 	else if (bitset(MCIF_CVT7TO8, mci->mci_flags))
46617c478bd9Sstevel@tonic-gate 	{
4662445f2479Sjbeck 		if (!mime7to8(mci, e->e_header, e))
4663445f2479Sjbeck 			goto writeerr;
46647c478bd9Sstevel@tonic-gate 	}
46657c478bd9Sstevel@tonic-gate # endif /* MIME7TO8 */
46667c478bd9Sstevel@tonic-gate 	else if (MaxMimeHeaderLength > 0 || MaxMimeFieldLength > 0)
46677c478bd9Sstevel@tonic-gate 	{
46687c478bd9Sstevel@tonic-gate 		bool oldsuprerrs = SuprErrs;
46697c478bd9Sstevel@tonic-gate 
46707c478bd9Sstevel@tonic-gate 		/* Use mime8to7 to check multipart for MIME header overflows */
46717c478bd9Sstevel@tonic-gate 		boundaries[0] = NULL;
46727c478bd9Sstevel@tonic-gate 		mci->mci_flags |= MCIF_INHEADER;
46737c478bd9Sstevel@tonic-gate 
46747c478bd9Sstevel@tonic-gate 		/*
46757c478bd9Sstevel@tonic-gate 		**  If EF_DONT_MIME is set, we have a broken MIME message
46767c478bd9Sstevel@tonic-gate 		**  and don't want to generate a new bounce message whose
46777c478bd9Sstevel@tonic-gate 		**  body propagates the broken MIME.  We can't just not call
46787c478bd9Sstevel@tonic-gate 		**  mime8to7() as is done above since we need the security
46797c478bd9Sstevel@tonic-gate 		**  checks.  The best we can do is suppress the errors.
46807c478bd9Sstevel@tonic-gate 		*/
46817c478bd9Sstevel@tonic-gate 
46827c478bd9Sstevel@tonic-gate 		if (bitset(EF_DONT_MIME, e->e_flags))
46837c478bd9Sstevel@tonic-gate 			SuprErrs = true;
46847c478bd9Sstevel@tonic-gate 
4685445f2479Sjbeck 		if (mime8to7(mci, e->e_header, e, boundaries,
46863ee0e492Sjbeck 				M87F_OUTER|M87F_NO8TO7, 0) == SM_IO_EOF)
4687445f2479Sjbeck 			goto writeerr;
46887c478bd9Sstevel@tonic-gate 
46897c478bd9Sstevel@tonic-gate 		/* restore SuprErrs */
46907c478bd9Sstevel@tonic-gate 		SuprErrs = oldsuprerrs;
46917c478bd9Sstevel@tonic-gate 	}
46927c478bd9Sstevel@tonic-gate 	else
46937c478bd9Sstevel@tonic-gate #endif /* MIME8TO7 */
46947c478bd9Sstevel@tonic-gate 	{
46957c478bd9Sstevel@tonic-gate 		int ostate;
46967c478bd9Sstevel@tonic-gate 		register char *bp;
46977c478bd9Sstevel@tonic-gate 		register char *pbp;
46987c478bd9Sstevel@tonic-gate 		register int c;
46997c478bd9Sstevel@tonic-gate 		register char *xp;
47007c478bd9Sstevel@tonic-gate 		int padc;
47017c478bd9Sstevel@tonic-gate 		char *buflim;
47027c478bd9Sstevel@tonic-gate 		int pos = 0;
47037c478bd9Sstevel@tonic-gate 		char peekbuf[12];
47047c478bd9Sstevel@tonic-gate 
47057c478bd9Sstevel@tonic-gate 		if (bitset(MCIF_INHEADER, mci->mci_flags))
47067c478bd9Sstevel@tonic-gate 		{
4707445f2479Sjbeck 			if (!putline("", mci))
4708445f2479Sjbeck 				goto writeerr;
47097c478bd9Sstevel@tonic-gate 			mci->mci_flags &= ~MCIF_INHEADER;
47107c478bd9Sstevel@tonic-gate 		}
47117c478bd9Sstevel@tonic-gate 
47127c478bd9Sstevel@tonic-gate 		/* determine end of buffer; allow for short mailer lines */
4713058561cbSjbeck 		buflim = &buf[sizeof(buf) - 1];
47147c478bd9Sstevel@tonic-gate 		if (mci->mci_mailer->m_linelimit > 0 &&
4715058561cbSjbeck 		    mci->mci_mailer->m_linelimit < sizeof(buf) - 1)
47167c478bd9Sstevel@tonic-gate 			buflim = &buf[mci->mci_mailer->m_linelimit - 1];
47177c478bd9Sstevel@tonic-gate 
47187c478bd9Sstevel@tonic-gate 		/* copy temp file to output with mapping */
471949218d4fSjbeck 		ostate = OSTATE_HEAD;
47207c478bd9Sstevel@tonic-gate 		bp = buf;
47217c478bd9Sstevel@tonic-gate 		pbp = peekbuf;
47227c478bd9Sstevel@tonic-gate 		while (!sm_io_error(mci->mci_out) && !dead)
47237c478bd9Sstevel@tonic-gate 		{
47247c478bd9Sstevel@tonic-gate 			if (pbp > peekbuf)
47257c478bd9Sstevel@tonic-gate 				c = *--pbp;
47267c478bd9Sstevel@tonic-gate 			else if ((c = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT))
47277c478bd9Sstevel@tonic-gate 				 == SM_IO_EOF)
47287c478bd9Sstevel@tonic-gate 				break;
47297c478bd9Sstevel@tonic-gate 			if (bitset(MCIF_7BIT, mci->mci_flags))
47307c478bd9Sstevel@tonic-gate 				c &= 0x7f;
47317c478bd9Sstevel@tonic-gate 			switch (ostate)
47327c478bd9Sstevel@tonic-gate 			{
473349218d4fSjbeck 			  case OSTATE_HEAD:
47347c478bd9Sstevel@tonic-gate 				if (c == '\0' &&
47357c478bd9Sstevel@tonic-gate 				    bitnset(M_NONULLS,
47367c478bd9Sstevel@tonic-gate 					    mci->mci_mailer->m_flags))
47377c478bd9Sstevel@tonic-gate 					break;
47387c478bd9Sstevel@tonic-gate 				if (c != '\r' && c != '\n' && bp < buflim)
47397c478bd9Sstevel@tonic-gate 				{
47407c478bd9Sstevel@tonic-gate 					*bp++ = c;
47417c478bd9Sstevel@tonic-gate 					break;
47427c478bd9Sstevel@tonic-gate 				}
47437c478bd9Sstevel@tonic-gate 
47447c478bd9Sstevel@tonic-gate 				/* check beginning of line for special cases */
47457c478bd9Sstevel@tonic-gate 				*bp = '\0';
47467c478bd9Sstevel@tonic-gate 				pos = 0;
47477c478bd9Sstevel@tonic-gate 				padc = SM_IO_EOF;
47487c478bd9Sstevel@tonic-gate 				if (buf[0] == 'F' &&
47497c478bd9Sstevel@tonic-gate 				    bitnset(M_ESCFROM, mci->mci_mailer->m_flags)
47507c478bd9Sstevel@tonic-gate 				    && strncmp(buf, "From ", 5) == 0)
47517c478bd9Sstevel@tonic-gate 				{
47527c478bd9Sstevel@tonic-gate 					padc = '>';
47537c478bd9Sstevel@tonic-gate 				}
47547c478bd9Sstevel@tonic-gate 				if (buf[0] == '-' && buf[1] == '-' &&
47557c478bd9Sstevel@tonic-gate 				    separator != NULL)
47567c478bd9Sstevel@tonic-gate 				{
47577c478bd9Sstevel@tonic-gate 					/* possible separator */
47587c478bd9Sstevel@tonic-gate 					int sl = strlen(separator);
47597c478bd9Sstevel@tonic-gate 
47607c478bd9Sstevel@tonic-gate 					if (strncmp(&buf[2], separator, sl)
47617c478bd9Sstevel@tonic-gate 					    == 0)
47627c478bd9Sstevel@tonic-gate 						padc = ' ';
47637c478bd9Sstevel@tonic-gate 				}
47647c478bd9Sstevel@tonic-gate 				if (buf[0] == '.' &&
47657c478bd9Sstevel@tonic-gate 				    bitnset(M_XDOT, mci->mci_mailer->m_flags))
47667c478bd9Sstevel@tonic-gate 				{
47677c478bd9Sstevel@tonic-gate 					padc = '.';
47687c478bd9Sstevel@tonic-gate 				}
47697c478bd9Sstevel@tonic-gate 
47707c478bd9Sstevel@tonic-gate 				/* now copy out saved line */
47717c478bd9Sstevel@tonic-gate 				if (TrafficLogFile != NULL)
47727c478bd9Sstevel@tonic-gate 				{
47737c478bd9Sstevel@tonic-gate 					(void) sm_io_fprintf(TrafficLogFile,
47747c478bd9Sstevel@tonic-gate 							     SM_TIME_DEFAULT,
47757c478bd9Sstevel@tonic-gate 							     "%05d >>> ",
47767c478bd9Sstevel@tonic-gate 							     (int) CurrentPid);
47777c478bd9Sstevel@tonic-gate 					if (padc != SM_IO_EOF)
47787c478bd9Sstevel@tonic-gate 						(void) sm_io_putc(TrafficLogFile,
47797c478bd9Sstevel@tonic-gate 								  SM_TIME_DEFAULT,
47807c478bd9Sstevel@tonic-gate 								  padc);
47817c478bd9Sstevel@tonic-gate 					for (xp = buf; xp < bp; xp++)
47827c478bd9Sstevel@tonic-gate 						(void) sm_io_putc(TrafficLogFile,
47837c478bd9Sstevel@tonic-gate 								  SM_TIME_DEFAULT,
47847c478bd9Sstevel@tonic-gate 								  (unsigned char) *xp);
47857c478bd9Sstevel@tonic-gate 					if (c == '\n')
47867c478bd9Sstevel@tonic-gate 						(void) sm_io_fputs(TrafficLogFile,
47877c478bd9Sstevel@tonic-gate 								   SM_TIME_DEFAULT,
47887c478bd9Sstevel@tonic-gate 								   mci->mci_mailer->m_eol);
47897c478bd9Sstevel@tonic-gate 				}
47907c478bd9Sstevel@tonic-gate 				if (padc != SM_IO_EOF)
47917c478bd9Sstevel@tonic-gate 				{
47927c478bd9Sstevel@tonic-gate 					if (sm_io_putc(mci->mci_out,
47937c478bd9Sstevel@tonic-gate 						       SM_TIME_DEFAULT, padc)
47947c478bd9Sstevel@tonic-gate 					    == SM_IO_EOF)
47957c478bd9Sstevel@tonic-gate 					{
47967c478bd9Sstevel@tonic-gate 						dead = true;
47977c478bd9Sstevel@tonic-gate 						continue;
47987c478bd9Sstevel@tonic-gate 					}
47997c478bd9Sstevel@tonic-gate 					pos++;
48007c478bd9Sstevel@tonic-gate 				}
48017c478bd9Sstevel@tonic-gate 				for (xp = buf; xp < bp; xp++)
48027c478bd9Sstevel@tonic-gate 				{
48037c478bd9Sstevel@tonic-gate 					if (sm_io_putc(mci->mci_out,
48047c478bd9Sstevel@tonic-gate 						       SM_TIME_DEFAULT,
48057c478bd9Sstevel@tonic-gate 						       (unsigned char) *xp)
48067c478bd9Sstevel@tonic-gate 					    == SM_IO_EOF)
48077c478bd9Sstevel@tonic-gate 					{
48087c478bd9Sstevel@tonic-gate 						dead = true;
48097c478bd9Sstevel@tonic-gate 						break;
48107c478bd9Sstevel@tonic-gate 					}
48117c478bd9Sstevel@tonic-gate 				}
48127c478bd9Sstevel@tonic-gate 				if (dead)
48137c478bd9Sstevel@tonic-gate 					continue;
48147c478bd9Sstevel@tonic-gate 				if (c == '\n')
48157c478bd9Sstevel@tonic-gate 				{
48167c478bd9Sstevel@tonic-gate 					if (sm_io_fputs(mci->mci_out,
48177c478bd9Sstevel@tonic-gate 							SM_TIME_DEFAULT,
48187c478bd9Sstevel@tonic-gate 							mci->mci_mailer->m_eol)
48197c478bd9Sstevel@tonic-gate 							== SM_IO_EOF)
48207c478bd9Sstevel@tonic-gate 						break;
48217c478bd9Sstevel@tonic-gate 					pos = 0;
48227c478bd9Sstevel@tonic-gate 				}
48237c478bd9Sstevel@tonic-gate 				else
48247c478bd9Sstevel@tonic-gate 				{
48257c478bd9Sstevel@tonic-gate 					pos += bp - buf;
48267c478bd9Sstevel@tonic-gate 					if (c != '\r')
48277c478bd9Sstevel@tonic-gate 					{
48287c478bd9Sstevel@tonic-gate 						SM_ASSERT(pbp < peekbuf +
48297c478bd9Sstevel@tonic-gate 								sizeof(peekbuf));
48307c478bd9Sstevel@tonic-gate 						*pbp++ = c;
48317c478bd9Sstevel@tonic-gate 					}
48327c478bd9Sstevel@tonic-gate 				}
48337c478bd9Sstevel@tonic-gate 
48347c478bd9Sstevel@tonic-gate 				bp = buf;
48357c478bd9Sstevel@tonic-gate 
48367c478bd9Sstevel@tonic-gate 				/* determine next state */
48377c478bd9Sstevel@tonic-gate 				if (c == '\n')
483849218d4fSjbeck 					ostate = OSTATE_HEAD;
48397c478bd9Sstevel@tonic-gate 				else if (c == '\r')
484049218d4fSjbeck 					ostate = OSTATE_CR;
48417c478bd9Sstevel@tonic-gate 				else
484249218d4fSjbeck 					ostate = OSTATE_INLINE;
48437c478bd9Sstevel@tonic-gate 				continue;
48447c478bd9Sstevel@tonic-gate 
484549218d4fSjbeck 			  case OSTATE_CR:
48467c478bd9Sstevel@tonic-gate 				if (c == '\n')
48477c478bd9Sstevel@tonic-gate 				{
48487c478bd9Sstevel@tonic-gate 					/* got CRLF */
48497c478bd9Sstevel@tonic-gate 					if (sm_io_fputs(mci->mci_out,
48507c478bd9Sstevel@tonic-gate 							SM_TIME_DEFAULT,
48517c478bd9Sstevel@tonic-gate 							mci->mci_mailer->m_eol)
48527c478bd9Sstevel@tonic-gate 							== SM_IO_EOF)
48537c478bd9Sstevel@tonic-gate 						continue;
48547c478bd9Sstevel@tonic-gate 
48557c478bd9Sstevel@tonic-gate 					if (TrafficLogFile != NULL)
48567c478bd9Sstevel@tonic-gate 					{
48577c478bd9Sstevel@tonic-gate 						(void) sm_io_fputs(TrafficLogFile,
48587c478bd9Sstevel@tonic-gate 								   SM_TIME_DEFAULT,
48597c478bd9Sstevel@tonic-gate 								   mci->mci_mailer->m_eol);
48607c478bd9Sstevel@tonic-gate 					}
4861058561cbSjbeck 					pos = 0;
486249218d4fSjbeck 					ostate = OSTATE_HEAD;
48637c478bd9Sstevel@tonic-gate 					continue;
48647c478bd9Sstevel@tonic-gate 				}
48657c478bd9Sstevel@tonic-gate 
48667c478bd9Sstevel@tonic-gate 				/* had a naked carriage return */
48677c478bd9Sstevel@tonic-gate 				SM_ASSERT(pbp < peekbuf + sizeof(peekbuf));
48687c478bd9Sstevel@tonic-gate 				*pbp++ = c;
48697c478bd9Sstevel@tonic-gate 				c = '\r';
487049218d4fSjbeck 				ostate = OSTATE_INLINE;
48717c478bd9Sstevel@tonic-gate 				goto putch;
48727c478bd9Sstevel@tonic-gate 
487349218d4fSjbeck 			  case OSTATE_INLINE:
48747c478bd9Sstevel@tonic-gate 				if (c == '\r')
48757c478bd9Sstevel@tonic-gate 				{
487649218d4fSjbeck 					ostate = OSTATE_CR;
48777c478bd9Sstevel@tonic-gate 					continue;
48787c478bd9Sstevel@tonic-gate 				}
48797c478bd9Sstevel@tonic-gate 				if (c == '\0' &&
48807c478bd9Sstevel@tonic-gate 				    bitnset(M_NONULLS,
48817c478bd9Sstevel@tonic-gate 					    mci->mci_mailer->m_flags))
48827c478bd9Sstevel@tonic-gate 					break;
48837c478bd9Sstevel@tonic-gate putch:
48847c478bd9Sstevel@tonic-gate 				if (mci->mci_mailer->m_linelimit > 0 &&
48857c478bd9Sstevel@tonic-gate 				    pos >= mci->mci_mailer->m_linelimit - 1 &&
48867c478bd9Sstevel@tonic-gate 				    c != '\n')
48877c478bd9Sstevel@tonic-gate 				{
48887c478bd9Sstevel@tonic-gate 					int d;
48897c478bd9Sstevel@tonic-gate 
48907c478bd9Sstevel@tonic-gate 					/* check next character for EOL */
48917c478bd9Sstevel@tonic-gate 					if (pbp > peekbuf)
48927c478bd9Sstevel@tonic-gate 						d = *(pbp - 1);
48937c478bd9Sstevel@tonic-gate 					else if ((d = sm_io_getc(e->e_dfp,
48947c478bd9Sstevel@tonic-gate 								 SM_TIME_DEFAULT))
48957c478bd9Sstevel@tonic-gate 						 != SM_IO_EOF)
48967c478bd9Sstevel@tonic-gate 					{
48977c478bd9Sstevel@tonic-gate 						SM_ASSERT(pbp < peekbuf +
48987c478bd9Sstevel@tonic-gate 								sizeof(peekbuf));
48997c478bd9Sstevel@tonic-gate 						*pbp++ = d;
49007c478bd9Sstevel@tonic-gate 					}
49017c478bd9Sstevel@tonic-gate 
49027c478bd9Sstevel@tonic-gate 					if (d == '\n' || d == SM_IO_EOF)
49037c478bd9Sstevel@tonic-gate 					{
49047c478bd9Sstevel@tonic-gate 						if (TrafficLogFile != NULL)
49057c478bd9Sstevel@tonic-gate 							(void) sm_io_putc(TrafficLogFile,
49067c478bd9Sstevel@tonic-gate 									  SM_TIME_DEFAULT,
49077c478bd9Sstevel@tonic-gate 									  (unsigned char) c);
49087c478bd9Sstevel@tonic-gate 						if (sm_io_putc(mci->mci_out,
49097c478bd9Sstevel@tonic-gate 							       SM_TIME_DEFAULT,
49107c478bd9Sstevel@tonic-gate 							       (unsigned char) c)
49117c478bd9Sstevel@tonic-gate 							       == SM_IO_EOF)
49127c478bd9Sstevel@tonic-gate 						{
49137c478bd9Sstevel@tonic-gate 							dead = true;
49147c478bd9Sstevel@tonic-gate 							continue;
49157c478bd9Sstevel@tonic-gate 						}
49167c478bd9Sstevel@tonic-gate 						pos++;
49177c478bd9Sstevel@tonic-gate 						continue;
49187c478bd9Sstevel@tonic-gate 					}
49197c478bd9Sstevel@tonic-gate 
49207c478bd9Sstevel@tonic-gate 					if (sm_io_putc(mci->mci_out,
49217c478bd9Sstevel@tonic-gate 						       SM_TIME_DEFAULT, '!')
49227c478bd9Sstevel@tonic-gate 					    == SM_IO_EOF ||
49237c478bd9Sstevel@tonic-gate 					    sm_io_fputs(mci->mci_out,
49247c478bd9Sstevel@tonic-gate 							SM_TIME_DEFAULT,
49257c478bd9Sstevel@tonic-gate 							mci->mci_mailer->m_eol)
49267c478bd9Sstevel@tonic-gate 					    == SM_IO_EOF)
49277c478bd9Sstevel@tonic-gate 					{
49287c478bd9Sstevel@tonic-gate 						dead = true;
49297c478bd9Sstevel@tonic-gate 						continue;
49307c478bd9Sstevel@tonic-gate 					}
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 					if (TrafficLogFile != NULL)
49337c478bd9Sstevel@tonic-gate 					{
49347c478bd9Sstevel@tonic-gate 						(void) sm_io_fprintf(TrafficLogFile,
49357c478bd9Sstevel@tonic-gate 								     SM_TIME_DEFAULT,
49367c478bd9Sstevel@tonic-gate 								     "!%s",
49377c478bd9Sstevel@tonic-gate 								     mci->mci_mailer->m_eol);
49387c478bd9Sstevel@tonic-gate 					}
493949218d4fSjbeck 					ostate = OSTATE_HEAD;
49407c478bd9Sstevel@tonic-gate 					SM_ASSERT(pbp < peekbuf +
49417c478bd9Sstevel@tonic-gate 							sizeof(peekbuf));
49427c478bd9Sstevel@tonic-gate 					*pbp++ = c;
49437c478bd9Sstevel@tonic-gate 					continue;
49447c478bd9Sstevel@tonic-gate 				}
49457c478bd9Sstevel@tonic-gate 				if (c == '\n')
49467c478bd9Sstevel@tonic-gate 				{
49477c478bd9Sstevel@tonic-gate 					if (TrafficLogFile != NULL)
49487c478bd9Sstevel@tonic-gate 						(void) sm_io_fputs(TrafficLogFile,
49497c478bd9Sstevel@tonic-gate 								   SM_TIME_DEFAULT,
49507c478bd9Sstevel@tonic-gate 								   mci->mci_mailer->m_eol);
49517c478bd9Sstevel@tonic-gate 					if (sm_io_fputs(mci->mci_out,
49527c478bd9Sstevel@tonic-gate 							SM_TIME_DEFAULT,
49537c478bd9Sstevel@tonic-gate 							mci->mci_mailer->m_eol)
49547c478bd9Sstevel@tonic-gate 							== SM_IO_EOF)
49557c478bd9Sstevel@tonic-gate 						continue;
49567c478bd9Sstevel@tonic-gate 					pos = 0;
495749218d4fSjbeck 					ostate = OSTATE_HEAD;
49587c478bd9Sstevel@tonic-gate 				}
49597c478bd9Sstevel@tonic-gate 				else
49607c478bd9Sstevel@tonic-gate 				{
49617c478bd9Sstevel@tonic-gate 					if (TrafficLogFile != NULL)
49627c478bd9Sstevel@tonic-gate 						(void) sm_io_putc(TrafficLogFile,
49637c478bd9Sstevel@tonic-gate 								  SM_TIME_DEFAULT,
49647c478bd9Sstevel@tonic-gate 								  (unsigned char) c);
49657c478bd9Sstevel@tonic-gate 					if (sm_io_putc(mci->mci_out,
49667c478bd9Sstevel@tonic-gate 						       SM_TIME_DEFAULT,
49677c478bd9Sstevel@tonic-gate 						       (unsigned char) c)
49687c478bd9Sstevel@tonic-gate 					    == SM_IO_EOF)
49697c478bd9Sstevel@tonic-gate 					{
49707c478bd9Sstevel@tonic-gate 						dead = true;
49717c478bd9Sstevel@tonic-gate 						continue;
49727c478bd9Sstevel@tonic-gate 					}
49737c478bd9Sstevel@tonic-gate 					pos++;
497449218d4fSjbeck 					ostate = OSTATE_INLINE;
49757c478bd9Sstevel@tonic-gate 				}
49767c478bd9Sstevel@tonic-gate 				break;
49777c478bd9Sstevel@tonic-gate 			}
49787c478bd9Sstevel@tonic-gate 		}
49797c478bd9Sstevel@tonic-gate 
49807c478bd9Sstevel@tonic-gate 		/* make sure we are at the beginning of a line */
49817c478bd9Sstevel@tonic-gate 		if (bp > buf)
49827c478bd9Sstevel@tonic-gate 		{
49837c478bd9Sstevel@tonic-gate 			if (TrafficLogFile != NULL)
49847c478bd9Sstevel@tonic-gate 			{
49857c478bd9Sstevel@tonic-gate 				for (xp = buf; xp < bp; xp++)
49867c478bd9Sstevel@tonic-gate 					(void) sm_io_putc(TrafficLogFile,
49877c478bd9Sstevel@tonic-gate 							  SM_TIME_DEFAULT,
49887c478bd9Sstevel@tonic-gate 							  (unsigned char) *xp);
49897c478bd9Sstevel@tonic-gate 			}
49907c478bd9Sstevel@tonic-gate 			for (xp = buf; xp < bp; xp++)
49917c478bd9Sstevel@tonic-gate 			{
49927c478bd9Sstevel@tonic-gate 				if (sm_io_putc(mci->mci_out, SM_TIME_DEFAULT,
49937c478bd9Sstevel@tonic-gate 					       (unsigned char) *xp)
49947c478bd9Sstevel@tonic-gate 				    == SM_IO_EOF)
49957c478bd9Sstevel@tonic-gate 				{
49967c478bd9Sstevel@tonic-gate 					dead = true;
49977c478bd9Sstevel@tonic-gate 					break;
49987c478bd9Sstevel@tonic-gate 				}
49997c478bd9Sstevel@tonic-gate 			}
50007c478bd9Sstevel@tonic-gate 			pos += bp - buf;
50017c478bd9Sstevel@tonic-gate 		}
50027c478bd9Sstevel@tonic-gate 		if (!dead && pos > 0)
50037c478bd9Sstevel@tonic-gate 		{
50047c478bd9Sstevel@tonic-gate 			if (TrafficLogFile != NULL)
50057c478bd9Sstevel@tonic-gate 				(void) sm_io_fputs(TrafficLogFile,
50067c478bd9Sstevel@tonic-gate 						   SM_TIME_DEFAULT,
50077c478bd9Sstevel@tonic-gate 						   mci->mci_mailer->m_eol);
5008445f2479Sjbeck 			if (sm_io_fputs(mci->mci_out, SM_TIME_DEFAULT,
5009445f2479Sjbeck 					   mci->mci_mailer->m_eol) == SM_IO_EOF)
5010445f2479Sjbeck 				goto writeerr;
50117c478bd9Sstevel@tonic-gate 		}
50127c478bd9Sstevel@tonic-gate 	}
50137c478bd9Sstevel@tonic-gate 
50147c478bd9Sstevel@tonic-gate 	if (sm_io_error(e->e_dfp))
50157c478bd9Sstevel@tonic-gate 	{
50167c478bd9Sstevel@tonic-gate 		syserr("putbody: %s/%cf%s: read error",
50177c478bd9Sstevel@tonic-gate 		       qid_printqueue(e->e_dfqgrp, e->e_dfqdir),
50187c478bd9Sstevel@tonic-gate 		       DATAFL_LETTER, e->e_id);
50197c478bd9Sstevel@tonic-gate 		ExitStat = EX_IOERR;
5020445f2479Sjbeck 		ioerr = true;
50217c478bd9Sstevel@tonic-gate 	}
50227c478bd9Sstevel@tonic-gate 
50237c478bd9Sstevel@tonic-gate endofmessage:
50247c478bd9Sstevel@tonic-gate 	/*
50257c478bd9Sstevel@tonic-gate 	**  Since mailfile() uses e_dfp in a child process,
50267c478bd9Sstevel@tonic-gate 	**  the file offset in the stdio library for the
50277c478bd9Sstevel@tonic-gate 	**  parent process will not agree with the in-kernel
50287c478bd9Sstevel@tonic-gate 	**  file offset since the file descriptor is shared
50297c478bd9Sstevel@tonic-gate 	**  between the processes.  Therefore, it is vital
50307c478bd9Sstevel@tonic-gate 	**  that the file always be rewound.  This forces the
50317c478bd9Sstevel@tonic-gate 	**  kernel offset (lseek) and stdio library (ftell)
50327c478bd9Sstevel@tonic-gate 	**  offset to match.
50337c478bd9Sstevel@tonic-gate 	*/
50347c478bd9Sstevel@tonic-gate 
5035445f2479Sjbeck 	save_errno = errno;
50367c478bd9Sstevel@tonic-gate 	if (e->e_dfp != NULL)
50377c478bd9Sstevel@tonic-gate 		(void) bfrewind(e->e_dfp);
50387c478bd9Sstevel@tonic-gate 
50397c478bd9Sstevel@tonic-gate 	/* some mailers want extra blank line at end of message */
50407c478bd9Sstevel@tonic-gate 	if (!dead && bitnset(M_BLANKEND, mci->mci_mailer->m_flags) &&
50417c478bd9Sstevel@tonic-gate 	    buf[0] != '\0' && buf[0] != '\n')
50427c478bd9Sstevel@tonic-gate 	{
5043445f2479Sjbeck 		if (!putline("", mci))
5044445f2479Sjbeck 			goto writeerr;
5045445f2479Sjbeck 	}
5046445f2479Sjbeck 
5047445f2479Sjbeck 	if (!dead &&
5048445f2479Sjbeck 	    (sm_io_flush(mci->mci_out, SM_TIME_DEFAULT) == SM_IO_EOF ||
5049445f2479Sjbeck 	     (sm_io_error(mci->mci_out) && errno != EPIPE)))
5050445f2479Sjbeck 	{
5051445f2479Sjbeck 		save_errno = errno;
50527c478bd9Sstevel@tonic-gate 		syserr("putbody: write error");
50537c478bd9Sstevel@tonic-gate 		ExitStat = EX_IOERR;
5054445f2479Sjbeck 		ioerr = true;
50557c478bd9Sstevel@tonic-gate 	}
50567c478bd9Sstevel@tonic-gate 
5057445f2479Sjbeck 	errno = save_errno;
5058445f2479Sjbeck 	return !dead && !ioerr;
5059445f2479Sjbeck 
5060445f2479Sjbeck   writeerr:
5061445f2479Sjbeck 	return false;
50627c478bd9Sstevel@tonic-gate }
5063445f2479Sjbeck 
50647c478bd9Sstevel@tonic-gate /*
50657c478bd9Sstevel@tonic-gate **  MAILFILE -- Send a message to a file.
50667c478bd9Sstevel@tonic-gate **
50677c478bd9Sstevel@tonic-gate **	If the file has the set-user-ID/set-group-ID bits set, but NO
50687c478bd9Sstevel@tonic-gate **	execute bits, sendmail will try to become the owner of that file
50697c478bd9Sstevel@tonic-gate **	rather than the real user.  Obviously, this only works if
50707c478bd9Sstevel@tonic-gate **	sendmail runs as root.
50717c478bd9Sstevel@tonic-gate **
50727c478bd9Sstevel@tonic-gate **	This could be done as a subordinate mailer, except that it
50737c478bd9Sstevel@tonic-gate **	is used implicitly to save messages in ~/dead.letter.  We
50747c478bd9Sstevel@tonic-gate **	view this as being sufficiently important as to include it
50757c478bd9Sstevel@tonic-gate **	here.  For example, if the system is dying, we shouldn't have
50767c478bd9Sstevel@tonic-gate **	to create another process plus some pipes to save the message.
50777c478bd9Sstevel@tonic-gate **
50787c478bd9Sstevel@tonic-gate **	Parameters:
50797c478bd9Sstevel@tonic-gate **		filename -- the name of the file to send to.
50807c478bd9Sstevel@tonic-gate **		mailer -- mailer definition for recipient -- if NULL,
50817c478bd9Sstevel@tonic-gate **			use FileMailer.
50827c478bd9Sstevel@tonic-gate **		ctladdr -- the controlling address header -- includes
50837c478bd9Sstevel@tonic-gate **			the userid/groupid to be when sending.
50847c478bd9Sstevel@tonic-gate **		sfflags -- flags for opening.
50857c478bd9Sstevel@tonic-gate **		e -- the current envelope.
50867c478bd9Sstevel@tonic-gate **
50877c478bd9Sstevel@tonic-gate **	Returns:
50887c478bd9Sstevel@tonic-gate **		The exit code associated with the operation.
50897c478bd9Sstevel@tonic-gate **
50907c478bd9Sstevel@tonic-gate **	Side Effects:
50917c478bd9Sstevel@tonic-gate **		none.
50927c478bd9Sstevel@tonic-gate */
50937c478bd9Sstevel@tonic-gate 
50947c478bd9Sstevel@tonic-gate # define RETURN(st)			exit(st);
50957c478bd9Sstevel@tonic-gate 
50967c478bd9Sstevel@tonic-gate static jmp_buf	CtxMailfileTimeout;
50977c478bd9Sstevel@tonic-gate 
50987c478bd9Sstevel@tonic-gate int
50997c478bd9Sstevel@tonic-gate mailfile(filename, mailer, ctladdr, sfflags, e)
51007c478bd9Sstevel@tonic-gate 	char *volatile filename;
51017c478bd9Sstevel@tonic-gate 	MAILER *volatile mailer;
51027c478bd9Sstevel@tonic-gate 	ADDRESS *ctladdr;
51037c478bd9Sstevel@tonic-gate 	volatile long sfflags;
51047c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
51057c478bd9Sstevel@tonic-gate {
51067c478bd9Sstevel@tonic-gate 	register SM_FILE_T *f;
51077c478bd9Sstevel@tonic-gate 	register pid_t pid = -1;
51087c478bd9Sstevel@tonic-gate 	volatile int mode;
51097c478bd9Sstevel@tonic-gate 	int len;
51107c478bd9Sstevel@tonic-gate 	off_t curoff;
51117c478bd9Sstevel@tonic-gate 	bool suidwarn = geteuid() == 0;
51127c478bd9Sstevel@tonic-gate 	char *p;
51137c478bd9Sstevel@tonic-gate 	char *volatile realfile;
51147c478bd9Sstevel@tonic-gate 	SM_EVENT *ev;
51157c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
51167c478bd9Sstevel@tonic-gate 	char targetfile[MAXPATHLEN];
51177c478bd9Sstevel@tonic-gate 
51187c478bd9Sstevel@tonic-gate 	if (tTd(11, 1))
51197c478bd9Sstevel@tonic-gate 	{
51207c478bd9Sstevel@tonic-gate 		sm_dprintf("mailfile %s\n  ctladdr=", filename);
51217c478bd9Sstevel@tonic-gate 		printaddr(sm_debug_file(), ctladdr, false);
51227c478bd9Sstevel@tonic-gate 	}
51237c478bd9Sstevel@tonic-gate 
51247c478bd9Sstevel@tonic-gate 	if (mailer == NULL)
51257c478bd9Sstevel@tonic-gate 		mailer = FileMailer;
51267c478bd9Sstevel@tonic-gate 
51277c478bd9Sstevel@tonic-gate 	if (e->e_xfp != NULL)
51287c478bd9Sstevel@tonic-gate 		(void) sm_io_flush(e->e_xfp, SM_TIME_DEFAULT);
51297c478bd9Sstevel@tonic-gate 
51307c478bd9Sstevel@tonic-gate 	/*
51317c478bd9Sstevel@tonic-gate 	**  Special case /dev/null.  This allows us to restrict file
51327c478bd9Sstevel@tonic-gate 	**  delivery to regular files only.
51337c478bd9Sstevel@tonic-gate 	*/
51347c478bd9Sstevel@tonic-gate 
51357c478bd9Sstevel@tonic-gate 	if (sm_path_isdevnull(filename))
51367c478bd9Sstevel@tonic-gate 		return EX_OK;
51377c478bd9Sstevel@tonic-gate 
51387c478bd9Sstevel@tonic-gate 	/* check for 8-bit available */
51397c478bd9Sstevel@tonic-gate 	if (bitset(EF_HAS8BIT, e->e_flags) &&
51407c478bd9Sstevel@tonic-gate 	    bitnset(M_7BITS, mailer->m_flags) &&
51417c478bd9Sstevel@tonic-gate 	    (bitset(EF_DONT_MIME, e->e_flags) ||
51427c478bd9Sstevel@tonic-gate 	     !(bitset(MM_MIME8BIT, MimeMode) ||
51437c478bd9Sstevel@tonic-gate 	       (bitset(EF_IS_MIME, e->e_flags) &&
51447c478bd9Sstevel@tonic-gate 		bitset(MM_CVTMIME, MimeMode)))))
51457c478bd9Sstevel@tonic-gate 	{
51467c478bd9Sstevel@tonic-gate 		e->e_status = "5.6.3";
51477c478bd9Sstevel@tonic-gate 		usrerrenh(e->e_status,
51487c478bd9Sstevel@tonic-gate 			  "554 Cannot send 8-bit data to 7-bit destination");
51497c478bd9Sstevel@tonic-gate 		errno = 0;
51507c478bd9Sstevel@tonic-gate 		return EX_DATAERR;
51517c478bd9Sstevel@tonic-gate 	}
51527c478bd9Sstevel@tonic-gate 
51537c478bd9Sstevel@tonic-gate 	/* Find the actual file */
51547c478bd9Sstevel@tonic-gate 	if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0')
51557c478bd9Sstevel@tonic-gate 	{
51567c478bd9Sstevel@tonic-gate 		len = strlen(SafeFileEnv);
51577c478bd9Sstevel@tonic-gate 
51587c478bd9Sstevel@tonic-gate 		if (strncmp(SafeFileEnv, filename, len) == 0)
51597c478bd9Sstevel@tonic-gate 			filename += len;
51607c478bd9Sstevel@tonic-gate 
5161058561cbSjbeck 		if (len + strlen(filename) + 1 >= sizeof(targetfile))
51627c478bd9Sstevel@tonic-gate 		{
51637c478bd9Sstevel@tonic-gate 			syserr("mailfile: filename too long (%s/%s)",
51647c478bd9Sstevel@tonic-gate 			       SafeFileEnv, filename);
51657c478bd9Sstevel@tonic-gate 			return EX_CANTCREAT;
51667c478bd9Sstevel@tonic-gate 		}
5167058561cbSjbeck 		(void) sm_strlcpy(targetfile, SafeFileEnv, sizeof(targetfile));
51687c478bd9Sstevel@tonic-gate 		realfile = targetfile + len;
51697c478bd9Sstevel@tonic-gate 		if (*filename == '/')
51707c478bd9Sstevel@tonic-gate 			filename++;
51717c478bd9Sstevel@tonic-gate 		if (*filename != '\0')
51727c478bd9Sstevel@tonic-gate 		{
51737c478bd9Sstevel@tonic-gate 			/* paranoia: trailing / should be removed in readcf */
51747c478bd9Sstevel@tonic-gate 			if (targetfile[len - 1] != '/')
51757c478bd9Sstevel@tonic-gate 				(void) sm_strlcat(targetfile,
5176058561cbSjbeck 						  "/", sizeof(targetfile));
51777c478bd9Sstevel@tonic-gate 			(void) sm_strlcat(targetfile, filename,
5178058561cbSjbeck 					  sizeof(targetfile));
51797c478bd9Sstevel@tonic-gate 		}
51807c478bd9Sstevel@tonic-gate 	}
51817c478bd9Sstevel@tonic-gate 	else if (mailer->m_rootdir != NULL)
51827c478bd9Sstevel@tonic-gate 	{
5183058561cbSjbeck 		expand(mailer->m_rootdir, targetfile, sizeof(targetfile), e);
51847c478bd9Sstevel@tonic-gate 		len = strlen(targetfile);
51857c478bd9Sstevel@tonic-gate 
51867c478bd9Sstevel@tonic-gate 		if (strncmp(targetfile, filename, len) == 0)
51877c478bd9Sstevel@tonic-gate 			filename += len;
51887c478bd9Sstevel@tonic-gate 
5189058561cbSjbeck 		if (len + strlen(filename) + 1 >= sizeof(targetfile))
51907c478bd9Sstevel@tonic-gate 		{
51917c478bd9Sstevel@tonic-gate 			syserr("mailfile: filename too long (%s/%s)",
51927c478bd9Sstevel@tonic-gate 			       targetfile, filename);
51937c478bd9Sstevel@tonic-gate 			return EX_CANTCREAT;
51947c478bd9Sstevel@tonic-gate 		}
51957c478bd9Sstevel@tonic-gate 		realfile = targetfile + len;
51967c478bd9Sstevel@tonic-gate 		if (targetfile[len - 1] != '/')
5197058561cbSjbeck 			(void) sm_strlcat(targetfile, "/", sizeof(targetfile));
51987c478bd9Sstevel@tonic-gate 		if (*filename == '/')
51997c478bd9Sstevel@tonic-gate 			(void) sm_strlcat(targetfile, filename + 1,
5200058561cbSjbeck 					  sizeof(targetfile));
52017c478bd9Sstevel@tonic-gate 		else
52027c478bd9Sstevel@tonic-gate 			(void) sm_strlcat(targetfile, filename,
5203058561cbSjbeck 					  sizeof(targetfile));
52047c478bd9Sstevel@tonic-gate 	}
52057c478bd9Sstevel@tonic-gate 	else
52067c478bd9Sstevel@tonic-gate 	{
5207058561cbSjbeck 		if (sm_strlcpy(targetfile, filename, sizeof(targetfile)) >=
5208058561cbSjbeck 		    sizeof(targetfile))
52097c478bd9Sstevel@tonic-gate 		{
52107c478bd9Sstevel@tonic-gate 			syserr("mailfile: filename too long (%s)", filename);
52117c478bd9Sstevel@tonic-gate 			return EX_CANTCREAT;
52127c478bd9Sstevel@tonic-gate 		}
52137c478bd9Sstevel@tonic-gate 		realfile = targetfile;
52147c478bd9Sstevel@tonic-gate 	}
52157c478bd9Sstevel@tonic-gate 
52167c478bd9Sstevel@tonic-gate 	/*
52177c478bd9Sstevel@tonic-gate 	**  Fork so we can change permissions here.
52187c478bd9Sstevel@tonic-gate 	**	Note that we MUST use fork, not vfork, because of
52197c478bd9Sstevel@tonic-gate 	**	the complications of calling subroutines, etc.
52207c478bd9Sstevel@tonic-gate 	*/
52217c478bd9Sstevel@tonic-gate 
52227c478bd9Sstevel@tonic-gate 
52237c478bd9Sstevel@tonic-gate 	/*
52247c478bd9Sstevel@tonic-gate 	**  Dispose of SIGCHLD signal catchers that may be laying
52257c478bd9Sstevel@tonic-gate 	**  around so that the waitfor() below will get it.
52267c478bd9Sstevel@tonic-gate 	*/
52277c478bd9Sstevel@tonic-gate 
52287c478bd9Sstevel@tonic-gate 	(void) sm_signal(SIGCHLD, SIG_DFL);
52297c478bd9Sstevel@tonic-gate 
52307c478bd9Sstevel@tonic-gate 	DOFORK(fork);
52317c478bd9Sstevel@tonic-gate 
52327c478bd9Sstevel@tonic-gate 	if (pid < 0)
52337c478bd9Sstevel@tonic-gate 		return EX_OSERR;
52347c478bd9Sstevel@tonic-gate 	else if (pid == 0)
52357c478bd9Sstevel@tonic-gate 	{
52367c478bd9Sstevel@tonic-gate 		/* child -- actually write to file */
52377c478bd9Sstevel@tonic-gate 		struct stat stb;
52387c478bd9Sstevel@tonic-gate 		MCI mcibuf;
52397c478bd9Sstevel@tonic-gate 		int err;
52407c478bd9Sstevel@tonic-gate 		volatile int oflags = O_WRONLY|O_APPEND;
52417c478bd9Sstevel@tonic-gate 
52427c478bd9Sstevel@tonic-gate 		/* Reset global flags */
52437c478bd9Sstevel@tonic-gate 		RestartRequest = NULL;
52447c478bd9Sstevel@tonic-gate 		RestartWorkGroup = false;
52457c478bd9Sstevel@tonic-gate 		ShutdownRequest = NULL;
52467c478bd9Sstevel@tonic-gate 		PendingSignal = 0;
52477c478bd9Sstevel@tonic-gate 		CurrentPid = getpid();
52487c478bd9Sstevel@tonic-gate 
52497c478bd9Sstevel@tonic-gate 		if (e->e_lockfp != NULL)
52503ee0e492Sjbeck 		{
52513ee0e492Sjbeck 			int fd;
52523ee0e492Sjbeck 
52533ee0e492Sjbeck 			fd = sm_io_getinfo(e->e_lockfp, SM_IO_WHAT_FD, NULL);
52543ee0e492Sjbeck 			/* SM_ASSERT(fd >= 0); */
52553ee0e492Sjbeck 			if (fd >= 0)
52563ee0e492Sjbeck 				(void) close(fd);
52573ee0e492Sjbeck 		}
52587c478bd9Sstevel@tonic-gate 
52597c478bd9Sstevel@tonic-gate 		(void) sm_signal(SIGINT, SIG_DFL);
52607c478bd9Sstevel@tonic-gate 		(void) sm_signal(SIGHUP, SIG_DFL);
52617c478bd9Sstevel@tonic-gate 		(void) sm_signal(SIGTERM, SIG_DFL);
52627c478bd9Sstevel@tonic-gate 		(void) umask(OldUmask);
52637c478bd9Sstevel@tonic-gate 		e->e_to = filename;
52647c478bd9Sstevel@tonic-gate 		ExitStat = EX_OK;
52657c478bd9Sstevel@tonic-gate 
52667c478bd9Sstevel@tonic-gate 		if (setjmp(CtxMailfileTimeout) != 0)
52677c478bd9Sstevel@tonic-gate 		{
52687c478bd9Sstevel@tonic-gate 			RETURN(EX_TEMPFAIL);
52697c478bd9Sstevel@tonic-gate 		}
52707c478bd9Sstevel@tonic-gate 
52717c478bd9Sstevel@tonic-gate 		if (TimeOuts.to_fileopen > 0)
52727c478bd9Sstevel@tonic-gate 			ev = sm_setevent(TimeOuts.to_fileopen, mailfiletimeout,
52737c478bd9Sstevel@tonic-gate 					 0);
52747c478bd9Sstevel@tonic-gate 		else
52757c478bd9Sstevel@tonic-gate 			ev = NULL;
52767c478bd9Sstevel@tonic-gate 
52777c478bd9Sstevel@tonic-gate 		/* check file mode to see if set-user-ID */
52787c478bd9Sstevel@tonic-gate 		if (stat(targetfile, &stb) < 0)
52797c478bd9Sstevel@tonic-gate 			mode = FileMode;
52807c478bd9Sstevel@tonic-gate 		else
52817c478bd9Sstevel@tonic-gate 			mode = stb.st_mode;
52827c478bd9Sstevel@tonic-gate 
52837c478bd9Sstevel@tonic-gate 		/* limit the errors to those actually caused in the child */
52847c478bd9Sstevel@tonic-gate 		errno = 0;
52857c478bd9Sstevel@tonic-gate 		ExitStat = EX_OK;
52867c478bd9Sstevel@tonic-gate 
52877c478bd9Sstevel@tonic-gate 		/* Allow alias expansions to use the S_IS{U,G}ID bits */
52887c478bd9Sstevel@tonic-gate 		if ((ctladdr != NULL && !bitset(QALIAS, ctladdr->q_flags)) ||
52897c478bd9Sstevel@tonic-gate 		    bitset(SFF_RUNASREALUID, sfflags))
52907c478bd9Sstevel@tonic-gate 		{
52917c478bd9Sstevel@tonic-gate 			/* ignore set-user-ID and set-group-ID bits */
52927c478bd9Sstevel@tonic-gate 			mode &= ~(S_ISGID|S_ISUID);
52937c478bd9Sstevel@tonic-gate 			if (tTd(11, 20))
52947c478bd9Sstevel@tonic-gate 				sm_dprintf("mailfile: ignoring set-user-ID/set-group-ID bits\n");
52957c478bd9Sstevel@tonic-gate 		}
52967c478bd9Sstevel@tonic-gate 
52977c478bd9Sstevel@tonic-gate 		/* we have to open the data file BEFORE setuid() */
52987c478bd9Sstevel@tonic-gate 		if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags))
52997c478bd9Sstevel@tonic-gate 		{
53007c478bd9Sstevel@tonic-gate 			char *df = queuename(e, DATAFL_LETTER);
53017c478bd9Sstevel@tonic-gate 
53027c478bd9Sstevel@tonic-gate 			e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, df,
53037c478bd9Sstevel@tonic-gate 					      SM_IO_RDONLY_B, NULL);
53047c478bd9Sstevel@tonic-gate 			if (e->e_dfp == NULL)
53057c478bd9Sstevel@tonic-gate 			{
53067c478bd9Sstevel@tonic-gate 				syserr("mailfile: Cannot open %s for %s from %s",
53077c478bd9Sstevel@tonic-gate 					df, e->e_to, e->e_from.q_paddr);
53087c478bd9Sstevel@tonic-gate 			}
53097c478bd9Sstevel@tonic-gate 		}
53107c478bd9Sstevel@tonic-gate 
53117c478bd9Sstevel@tonic-gate 		/* select a new user to run as */
53127c478bd9Sstevel@tonic-gate 		if (!bitset(SFF_RUNASREALUID, sfflags))
53137c478bd9Sstevel@tonic-gate 		{
53147c478bd9Sstevel@tonic-gate 			if (bitnset(M_SPECIFIC_UID, mailer->m_flags))
53157c478bd9Sstevel@tonic-gate 			{
53167c478bd9Sstevel@tonic-gate 				RealUserName = NULL;
53177c478bd9Sstevel@tonic-gate 				if (mailer->m_uid == NO_UID)
53187c478bd9Sstevel@tonic-gate 					RealUid = RunAsUid;
53197c478bd9Sstevel@tonic-gate 				else
53207c478bd9Sstevel@tonic-gate 					RealUid = mailer->m_uid;
53217c478bd9Sstevel@tonic-gate 				if (RunAsUid != 0 && RealUid != RunAsUid)
53227c478bd9Sstevel@tonic-gate 				{
53237c478bd9Sstevel@tonic-gate 					/* Only root can change the uid */
53247c478bd9Sstevel@tonic-gate 					syserr("mailfile: insufficient privileges to change uid, RunAsUid=%d, RealUid=%d",
53257c478bd9Sstevel@tonic-gate 						(int) RunAsUid, (int) RealUid);
53267c478bd9Sstevel@tonic-gate 					RETURN(EX_TEMPFAIL);
53277c478bd9Sstevel@tonic-gate 				}
53287c478bd9Sstevel@tonic-gate 			}
53297c478bd9Sstevel@tonic-gate 			else if (bitset(S_ISUID, mode))
53307c478bd9Sstevel@tonic-gate 			{
53317c478bd9Sstevel@tonic-gate 				RealUserName = NULL;
53327c478bd9Sstevel@tonic-gate 				RealUid = stb.st_uid;
53337c478bd9Sstevel@tonic-gate 			}
53347c478bd9Sstevel@tonic-gate 			else if (ctladdr != NULL && ctladdr->q_uid != 0)
53357c478bd9Sstevel@tonic-gate 			{
53367c478bd9Sstevel@tonic-gate 				if (ctladdr->q_ruser != NULL)
53377c478bd9Sstevel@tonic-gate 					RealUserName = ctladdr->q_ruser;
53387c478bd9Sstevel@tonic-gate 				else
53397c478bd9Sstevel@tonic-gate 					RealUserName = ctladdr->q_user;
53407c478bd9Sstevel@tonic-gate 				RealUid = ctladdr->q_uid;
53417c478bd9Sstevel@tonic-gate 			}
53427c478bd9Sstevel@tonic-gate 			else if (mailer != NULL && mailer->m_uid != NO_UID)
53437c478bd9Sstevel@tonic-gate 			{
53447c478bd9Sstevel@tonic-gate 				RealUserName = DefUser;
53457c478bd9Sstevel@tonic-gate 				RealUid = mailer->m_uid;
53467c478bd9Sstevel@tonic-gate 			}
53477c478bd9Sstevel@tonic-gate 			else
53487c478bd9Sstevel@tonic-gate 			{
53497c478bd9Sstevel@tonic-gate 				RealUserName = DefUser;
53507c478bd9Sstevel@tonic-gate 				RealUid = DefUid;
53517c478bd9Sstevel@tonic-gate 			}
53527c478bd9Sstevel@tonic-gate 
53537c478bd9Sstevel@tonic-gate 			/* select a new group to run as */
53547c478bd9Sstevel@tonic-gate 			if (bitnset(M_SPECIFIC_UID, mailer->m_flags))
53557c478bd9Sstevel@tonic-gate 			{
53567c478bd9Sstevel@tonic-gate 				if (mailer->m_gid == NO_GID)
53577c478bd9Sstevel@tonic-gate 					RealGid = RunAsGid;
53587c478bd9Sstevel@tonic-gate 				else
53597c478bd9Sstevel@tonic-gate 					RealGid = mailer->m_gid;
53607c478bd9Sstevel@tonic-gate 				if (RunAsUid != 0 &&
53617c478bd9Sstevel@tonic-gate 				    (RealGid != getgid() ||
53627c478bd9Sstevel@tonic-gate 				     RealGid != getegid()))
53637c478bd9Sstevel@tonic-gate 				{
53647c478bd9Sstevel@tonic-gate 					/* Only root can change the gid */
53657c478bd9Sstevel@tonic-gate 					syserr("mailfile: insufficient privileges to change gid, RealGid=%d, RunAsUid=%d, gid=%d, egid=%d",
53667c478bd9Sstevel@tonic-gate 					       (int) RealGid, (int) RunAsUid,
53677c478bd9Sstevel@tonic-gate 					       (int) getgid(), (int) getegid());
53687c478bd9Sstevel@tonic-gate 					RETURN(EX_TEMPFAIL);
53697c478bd9Sstevel@tonic-gate 				}
53707c478bd9Sstevel@tonic-gate 			}
53717c478bd9Sstevel@tonic-gate 			else if (bitset(S_ISGID, mode))
53727c478bd9Sstevel@tonic-gate 				RealGid = stb.st_gid;
53737c478bd9Sstevel@tonic-gate 			else if (ctladdr != NULL &&
53747c478bd9Sstevel@tonic-gate 				 ctladdr->q_uid == DefUid &&
53757c478bd9Sstevel@tonic-gate 				 ctladdr->q_gid == 0)
53767c478bd9Sstevel@tonic-gate 			{
53777c478bd9Sstevel@tonic-gate 				/*
53787c478bd9Sstevel@tonic-gate 				**  Special case:  This means it is an
53797c478bd9Sstevel@tonic-gate 				**  alias and we should act as DefaultUser.
53807c478bd9Sstevel@tonic-gate 				**  See alias()'s comments.
53817c478bd9Sstevel@tonic-gate 				*/
53827c478bd9Sstevel@tonic-gate 
53837c478bd9Sstevel@tonic-gate 				RealGid = DefGid;
53847c478bd9Sstevel@tonic-gate 				RealUserName = DefUser;
53857c478bd9Sstevel@tonic-gate 			}
53867c478bd9Sstevel@tonic-gate 			else if (ctladdr != NULL && ctladdr->q_uid != 0)
53877c478bd9Sstevel@tonic-gate 				RealGid = ctladdr->q_gid;
53887c478bd9Sstevel@tonic-gate 			else if (mailer != NULL && mailer->m_gid != NO_GID)
53897c478bd9Sstevel@tonic-gate 				RealGid = mailer->m_gid;
53907c478bd9Sstevel@tonic-gate 			else
53917c478bd9Sstevel@tonic-gate 				RealGid = DefGid;
53927c478bd9Sstevel@tonic-gate 		}
53937c478bd9Sstevel@tonic-gate 
53947c478bd9Sstevel@tonic-gate 		/* last ditch */
53957c478bd9Sstevel@tonic-gate 		if (!bitset(SFF_ROOTOK, sfflags))
53967c478bd9Sstevel@tonic-gate 		{
53977c478bd9Sstevel@tonic-gate 			if (RealUid == 0)
53987c478bd9Sstevel@tonic-gate 				RealUid = DefUid;
53997c478bd9Sstevel@tonic-gate 			if (RealGid == 0)
54007c478bd9Sstevel@tonic-gate 				RealGid = DefGid;
54017c478bd9Sstevel@tonic-gate 		}
54027c478bd9Sstevel@tonic-gate 
54037c478bd9Sstevel@tonic-gate 		/* set group id list (needs /etc/group access) */
54047c478bd9Sstevel@tonic-gate 		if (RealUserName != NULL && !DontInitGroups)
54057c478bd9Sstevel@tonic-gate 		{
54067c478bd9Sstevel@tonic-gate 			if (initgroups(RealUserName, RealGid) == -1 && suidwarn)
54077c478bd9Sstevel@tonic-gate 			{
54087c478bd9Sstevel@tonic-gate 				syserr("mailfile: initgroups(%s, %d) failed",
54097c478bd9Sstevel@tonic-gate 					RealUserName, RealGid);
54107c478bd9Sstevel@tonic-gate 				RETURN(EX_TEMPFAIL);
54117c478bd9Sstevel@tonic-gate 			}
54127c478bd9Sstevel@tonic-gate 		}
54137c478bd9Sstevel@tonic-gate 		else
54147c478bd9Sstevel@tonic-gate 		{
54157c478bd9Sstevel@tonic-gate 			GIDSET_T gidset[1];
54167c478bd9Sstevel@tonic-gate 
54177c478bd9Sstevel@tonic-gate 			gidset[0] = RealGid;
54187c478bd9Sstevel@tonic-gate 			if (setgroups(1, gidset) == -1 && suidwarn)
54197c478bd9Sstevel@tonic-gate 			{
54207c478bd9Sstevel@tonic-gate 				syserr("mailfile: setgroups() failed");
54217c478bd9Sstevel@tonic-gate 				RETURN(EX_TEMPFAIL);
54227c478bd9Sstevel@tonic-gate 			}
54237c478bd9Sstevel@tonic-gate 		}
54247c478bd9Sstevel@tonic-gate 
54257c478bd9Sstevel@tonic-gate 		/*
54267c478bd9Sstevel@tonic-gate 		**  If you have a safe environment, go into it.
54277c478bd9Sstevel@tonic-gate 		*/
54287c478bd9Sstevel@tonic-gate 
54297c478bd9Sstevel@tonic-gate 		if (realfile != targetfile)
54307c478bd9Sstevel@tonic-gate 		{
54317c478bd9Sstevel@tonic-gate 			char save;
54327c478bd9Sstevel@tonic-gate 
54337c478bd9Sstevel@tonic-gate 			save = *realfile;
54347c478bd9Sstevel@tonic-gate 			*realfile = '\0';
54357c478bd9Sstevel@tonic-gate 			if (tTd(11, 20))
54367c478bd9Sstevel@tonic-gate 				sm_dprintf("mailfile: chroot %s\n", targetfile);
54377c478bd9Sstevel@tonic-gate 			if (chroot(targetfile) < 0)
54387c478bd9Sstevel@tonic-gate 			{
54397c478bd9Sstevel@tonic-gate 				syserr("mailfile: Cannot chroot(%s)",
54407c478bd9Sstevel@tonic-gate 				       targetfile);
54417c478bd9Sstevel@tonic-gate 				RETURN(EX_CANTCREAT);
54427c478bd9Sstevel@tonic-gate 			}
54437c478bd9Sstevel@tonic-gate 			*realfile = save;
54447c478bd9Sstevel@tonic-gate 		}
54457c478bd9Sstevel@tonic-gate 
54467c478bd9Sstevel@tonic-gate 		if (tTd(11, 40))
54477c478bd9Sstevel@tonic-gate 			sm_dprintf("mailfile: deliver to %s\n", realfile);
54487c478bd9Sstevel@tonic-gate 
54497c478bd9Sstevel@tonic-gate 		if (chdir("/") < 0)
54507c478bd9Sstevel@tonic-gate 		{
54517c478bd9Sstevel@tonic-gate 			syserr("mailfile: cannot chdir(/)");
54527c478bd9Sstevel@tonic-gate 			RETURN(EX_CANTCREAT);
54537c478bd9Sstevel@tonic-gate 		}
54547c478bd9Sstevel@tonic-gate 
54557c478bd9Sstevel@tonic-gate 		/* now reset the group and user ids */
54567c478bd9Sstevel@tonic-gate 		endpwent();
54577c478bd9Sstevel@tonic-gate 		sm_mbdb_terminate();
54587c478bd9Sstevel@tonic-gate 		if (setgid(RealGid) < 0 && suidwarn)
54597c478bd9Sstevel@tonic-gate 		{
54607c478bd9Sstevel@tonic-gate 			syserr("mailfile: setgid(%ld) failed", (long) RealGid);
54617c478bd9Sstevel@tonic-gate 			RETURN(EX_TEMPFAIL);
54627c478bd9Sstevel@tonic-gate 		}
54637c478bd9Sstevel@tonic-gate 		vendor_set_uid(RealUid);
54647c478bd9Sstevel@tonic-gate 		if (setuid(RealUid) < 0 && suidwarn)
54657c478bd9Sstevel@tonic-gate 		{
54667c478bd9Sstevel@tonic-gate 			syserr("mailfile: setuid(%ld) failed", (long) RealUid);
54677c478bd9Sstevel@tonic-gate 			RETURN(EX_TEMPFAIL);
54687c478bd9Sstevel@tonic-gate 		}
54697c478bd9Sstevel@tonic-gate 
54707c478bd9Sstevel@tonic-gate 		if (tTd(11, 2))
54717c478bd9Sstevel@tonic-gate 			sm_dprintf("mailfile: running as r/euid=%d/%d, r/egid=%d/%d\n",
54727c478bd9Sstevel@tonic-gate 				(int) getuid(), (int) geteuid(),
54737c478bd9Sstevel@tonic-gate 				(int) getgid(), (int) getegid());
54747c478bd9Sstevel@tonic-gate 
54757c478bd9Sstevel@tonic-gate 
54767c478bd9Sstevel@tonic-gate 		/* move into some "safe" directory */
54777c478bd9Sstevel@tonic-gate 		if (mailer->m_execdir != NULL)
54787c478bd9Sstevel@tonic-gate 		{
54797c478bd9Sstevel@tonic-gate 			char *q;
54807c478bd9Sstevel@tonic-gate 
54817c478bd9Sstevel@tonic-gate 			for (p = mailer->m_execdir; p != NULL; p = q)
54827c478bd9Sstevel@tonic-gate 			{
54837c478bd9Sstevel@tonic-gate 				q = strchr(p, ':');
54847c478bd9Sstevel@tonic-gate 				if (q != NULL)
54857c478bd9Sstevel@tonic-gate 					*q = '\0';
5486058561cbSjbeck 				expand(p, buf, sizeof(buf), e);
54877c478bd9Sstevel@tonic-gate 				if (q != NULL)
54887c478bd9Sstevel@tonic-gate 					*q++ = ':';
54897c478bd9Sstevel@tonic-gate 				if (tTd(11, 20))
54907c478bd9Sstevel@tonic-gate 					sm_dprintf("mailfile: trydir %s\n",
54917c478bd9Sstevel@tonic-gate 						   buf);
54927c478bd9Sstevel@tonic-gate 				if (buf[0] != '\0' && chdir(buf) >= 0)
54937c478bd9Sstevel@tonic-gate 					break;
54947c478bd9Sstevel@tonic-gate 			}
54957c478bd9Sstevel@tonic-gate 		}
54967c478bd9Sstevel@tonic-gate 
54977c478bd9Sstevel@tonic-gate 		/*
54987c478bd9Sstevel@tonic-gate 		**  Recheck the file after we have assumed the ID of the
54997c478bd9Sstevel@tonic-gate 		**  delivery user to make sure we can deliver to it as
55007c478bd9Sstevel@tonic-gate 		**  that user.  This is necessary if sendmail is running
55017c478bd9Sstevel@tonic-gate 		**  as root and the file is on an NFS mount which treats
55027c478bd9Sstevel@tonic-gate 		**  root as nobody.
55037c478bd9Sstevel@tonic-gate 		*/
55047c478bd9Sstevel@tonic-gate 
55057c478bd9Sstevel@tonic-gate #if HASLSTAT
55067c478bd9Sstevel@tonic-gate 		if (bitnset(DBS_FILEDELIVERYTOSYMLINK, DontBlameSendmail))
55077c478bd9Sstevel@tonic-gate 			err = stat(realfile, &stb);
55087c478bd9Sstevel@tonic-gate 		else
55097c478bd9Sstevel@tonic-gate 			err = lstat(realfile, &stb);
55107c478bd9Sstevel@tonic-gate #else /* HASLSTAT */
55117c478bd9Sstevel@tonic-gate 		err = stat(realfile, &stb);
55127c478bd9Sstevel@tonic-gate #endif /* HASLSTAT */
55137c478bd9Sstevel@tonic-gate 
55147c478bd9Sstevel@tonic-gate 		if (err < 0)
55157c478bd9Sstevel@tonic-gate 		{
55167c478bd9Sstevel@tonic-gate 			stb.st_mode = ST_MODE_NOFILE;
55177c478bd9Sstevel@tonic-gate 			mode = FileMode;
55187c478bd9Sstevel@tonic-gate 			oflags |= O_CREAT|O_EXCL;
55197c478bd9Sstevel@tonic-gate 		}
55207c478bd9Sstevel@tonic-gate 		else if (bitset(S_IXUSR|S_IXGRP|S_IXOTH, mode) ||
55217c478bd9Sstevel@tonic-gate 			 (!bitnset(DBS_FILEDELIVERYTOHARDLINK,
55227c478bd9Sstevel@tonic-gate 				   DontBlameSendmail) &&
55237c478bd9Sstevel@tonic-gate 			  stb.st_nlink != 1) ||
55247c478bd9Sstevel@tonic-gate 			 (realfile != targetfile && !S_ISREG(mode)))
55257c478bd9Sstevel@tonic-gate 			exit(EX_CANTCREAT);
55267c478bd9Sstevel@tonic-gate 		else
55277c478bd9Sstevel@tonic-gate 			mode = stb.st_mode;
55287c478bd9Sstevel@tonic-gate 
55297c478bd9Sstevel@tonic-gate 		if (!bitnset(DBS_FILEDELIVERYTOSYMLINK, DontBlameSendmail))
55307c478bd9Sstevel@tonic-gate 			sfflags |= SFF_NOSLINK;
55317c478bd9Sstevel@tonic-gate 		if (!bitnset(DBS_FILEDELIVERYTOHARDLINK, DontBlameSendmail))
55327c478bd9Sstevel@tonic-gate 			sfflags |= SFF_NOHLINK;
55337c478bd9Sstevel@tonic-gate 		sfflags &= ~SFF_OPENASROOT;
55347c478bd9Sstevel@tonic-gate 		f = safefopen(realfile, oflags, mode, sfflags);
55357c478bd9Sstevel@tonic-gate 		if (f == NULL)
55367c478bd9Sstevel@tonic-gate 		{
55377c478bd9Sstevel@tonic-gate 			if (transienterror(errno))
55387c478bd9Sstevel@tonic-gate 			{
55397c478bd9Sstevel@tonic-gate 				usrerr("454 4.3.0 cannot open %s: %s",
55407c478bd9Sstevel@tonic-gate 				       shortenstring(realfile, MAXSHORTSTR),
55417c478bd9Sstevel@tonic-gate 				       sm_errstring(errno));
55427c478bd9Sstevel@tonic-gate 				RETURN(EX_TEMPFAIL);
55437c478bd9Sstevel@tonic-gate 			}
55447c478bd9Sstevel@tonic-gate 			else
55457c478bd9Sstevel@tonic-gate 			{
55467c478bd9Sstevel@tonic-gate 				usrerr("554 5.3.0 cannot open %s: %s",
55477c478bd9Sstevel@tonic-gate 				       shortenstring(realfile, MAXSHORTSTR),
55487c478bd9Sstevel@tonic-gate 				       sm_errstring(errno));
55497c478bd9Sstevel@tonic-gate 				RETURN(EX_CANTCREAT);
55507c478bd9Sstevel@tonic-gate 			}
55517c478bd9Sstevel@tonic-gate 		}
55527c478bd9Sstevel@tonic-gate 		if (filechanged(realfile, sm_io_getinfo(f, SM_IO_WHAT_FD, NULL),
55537c478bd9Sstevel@tonic-gate 		    &stb))
55547c478bd9Sstevel@tonic-gate 		{
55557c478bd9Sstevel@tonic-gate 			syserr("554 5.3.0 file changed after open");
55567c478bd9Sstevel@tonic-gate 			RETURN(EX_CANTCREAT);
55577c478bd9Sstevel@tonic-gate 		}
55587c478bd9Sstevel@tonic-gate 		if (fstat(sm_io_getinfo(f, SM_IO_WHAT_FD, NULL), &stb) < 0)
55597c478bd9Sstevel@tonic-gate 		{
55607c478bd9Sstevel@tonic-gate 			syserr("554 5.3.0 cannot fstat %s",
55617c478bd9Sstevel@tonic-gate 				sm_errstring(errno));
55627c478bd9Sstevel@tonic-gate 			RETURN(EX_CANTCREAT);
55637c478bd9Sstevel@tonic-gate 		}
55647c478bd9Sstevel@tonic-gate 
55657c478bd9Sstevel@tonic-gate 		curoff = stb.st_size;
55667c478bd9Sstevel@tonic-gate 
55677c478bd9Sstevel@tonic-gate 		if (ev != NULL)
55687c478bd9Sstevel@tonic-gate 			sm_clrevent(ev);
55697c478bd9Sstevel@tonic-gate 
5570058561cbSjbeck 		memset(&mcibuf, '\0', sizeof(mcibuf));
55717c478bd9Sstevel@tonic-gate 		mcibuf.mci_mailer = mailer;
55727c478bd9Sstevel@tonic-gate 		mcibuf.mci_out = f;
55737c478bd9Sstevel@tonic-gate 		if (bitnset(M_7BITS, mailer->m_flags))
55747c478bd9Sstevel@tonic-gate 			mcibuf.mci_flags |= MCIF_7BIT;
55757c478bd9Sstevel@tonic-gate 
55767c478bd9Sstevel@tonic-gate 		/* clear out per-message flags from connection structure */
55777c478bd9Sstevel@tonic-gate 		mcibuf.mci_flags &= ~(MCIF_CVT7TO8|MCIF_CVT8TO7);
55787c478bd9Sstevel@tonic-gate 
55797c478bd9Sstevel@tonic-gate 		if (bitset(EF_HAS8BIT, e->e_flags) &&
55807c478bd9Sstevel@tonic-gate 		    !bitset(EF_DONT_MIME, e->e_flags) &&
55817c478bd9Sstevel@tonic-gate 		    bitnset(M_7BITS, mailer->m_flags))
55827c478bd9Sstevel@tonic-gate 			mcibuf.mci_flags |= MCIF_CVT8TO7;
55837c478bd9Sstevel@tonic-gate 
55847c478bd9Sstevel@tonic-gate #if MIME7TO8
55857c478bd9Sstevel@tonic-gate 		if (bitnset(M_MAKE8BIT, mailer->m_flags) &&
55867c478bd9Sstevel@tonic-gate 		    !bitset(MCIF_7BIT, mcibuf.mci_flags) &&
55877c478bd9Sstevel@tonic-gate 		    (p = hvalue("Content-Transfer-Encoding", e->e_header)) != NULL &&
55887c478bd9Sstevel@tonic-gate 		    (sm_strcasecmp(p, "quoted-printable") == 0 ||
55897c478bd9Sstevel@tonic-gate 		     sm_strcasecmp(p, "base64") == 0) &&
55907c478bd9Sstevel@tonic-gate 		    (p = hvalue("Content-Type", e->e_header)) != NULL)
55917c478bd9Sstevel@tonic-gate 		{
55927c478bd9Sstevel@tonic-gate 			/* may want to convert 7 -> 8 */
55937c478bd9Sstevel@tonic-gate 			/* XXX should really parse it here -- and use a class XXX */
55947c478bd9Sstevel@tonic-gate 			if (sm_strncasecmp(p, "text/plain", 10) == 0 &&
55957c478bd9Sstevel@tonic-gate 			    (p[10] == '\0' || p[10] == ' ' || p[10] == ';'))
55967c478bd9Sstevel@tonic-gate 				mcibuf.mci_flags |= MCIF_CVT7TO8;
55977c478bd9Sstevel@tonic-gate 		}
55987c478bd9Sstevel@tonic-gate #endif /* MIME7TO8 */
55997c478bd9Sstevel@tonic-gate 
5600445f2479Sjbeck 		if (!putfromline(&mcibuf, e) ||
5601445f2479Sjbeck 		    !(*e->e_puthdr)(&mcibuf, e->e_header, e, M87F_OUTER) ||
5602445f2479Sjbeck 		    !(*e->e_putbody)(&mcibuf, e, NULL) ||
5603445f2479Sjbeck 		    !putline("\n", &mcibuf) ||
5604445f2479Sjbeck 		    (sm_io_flush(f, SM_TIME_DEFAULT) != 0 ||
56057c478bd9Sstevel@tonic-gate 		    (SuperSafe != SAFE_NO &&
56067c478bd9Sstevel@tonic-gate 		     fsync(sm_io_getinfo(f, SM_IO_WHAT_FD, NULL)) < 0) ||
5607445f2479Sjbeck 		    sm_io_error(f)))
56087c478bd9Sstevel@tonic-gate 		{
56097c478bd9Sstevel@tonic-gate 			setstat(EX_IOERR);
56107c478bd9Sstevel@tonic-gate #if !NOFTRUNCATE
56117c478bd9Sstevel@tonic-gate 			(void) ftruncate(sm_io_getinfo(f, SM_IO_WHAT_FD, NULL),
56127c478bd9Sstevel@tonic-gate 					 curoff);
56137c478bd9Sstevel@tonic-gate #endif /* !NOFTRUNCATE */
56147c478bd9Sstevel@tonic-gate 		}
56157c478bd9Sstevel@tonic-gate 
56167c478bd9Sstevel@tonic-gate 		/* reset ISUID & ISGID bits for paranoid systems */
56177c478bd9Sstevel@tonic-gate #if HASFCHMOD
56187c478bd9Sstevel@tonic-gate 		(void) fchmod(sm_io_getinfo(f, SM_IO_WHAT_FD, NULL),
56197c478bd9Sstevel@tonic-gate 			      (MODE_T) mode);
56207c478bd9Sstevel@tonic-gate #else /* HASFCHMOD */
56217c478bd9Sstevel@tonic-gate 		(void) chmod(filename, (MODE_T) mode);
56227c478bd9Sstevel@tonic-gate #endif /* HASFCHMOD */
56237c478bd9Sstevel@tonic-gate 		if (sm_io_close(f, SM_TIME_DEFAULT) < 0)
56247c478bd9Sstevel@tonic-gate 			setstat(EX_IOERR);
56257c478bd9Sstevel@tonic-gate 		(void) sm_io_flush(smioout, SM_TIME_DEFAULT);
56267c478bd9Sstevel@tonic-gate 		(void) setuid(RealUid);
56277c478bd9Sstevel@tonic-gate 		exit(ExitStat);
56287c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
56297c478bd9Sstevel@tonic-gate 	}
56307c478bd9Sstevel@tonic-gate 	else
56317c478bd9Sstevel@tonic-gate 	{
56327c478bd9Sstevel@tonic-gate 		/* parent -- wait for exit status */
56337c478bd9Sstevel@tonic-gate 		int st;
56347c478bd9Sstevel@tonic-gate 
56357c478bd9Sstevel@tonic-gate 		st = waitfor(pid);
56367c478bd9Sstevel@tonic-gate 		if (st == -1)
56377c478bd9Sstevel@tonic-gate 		{
56387c478bd9Sstevel@tonic-gate 			syserr("mailfile: %s: wait", mailer->m_name);
56397c478bd9Sstevel@tonic-gate 			return EX_SOFTWARE;
56407c478bd9Sstevel@tonic-gate 		}
56417c478bd9Sstevel@tonic-gate 		if (WIFEXITED(st))
56427c478bd9Sstevel@tonic-gate 		{
56437c478bd9Sstevel@tonic-gate 			errno = 0;
56447c478bd9Sstevel@tonic-gate 			return (WEXITSTATUS(st));
56457c478bd9Sstevel@tonic-gate 		}
56467c478bd9Sstevel@tonic-gate 		else
56477c478bd9Sstevel@tonic-gate 		{
56487c478bd9Sstevel@tonic-gate 			syserr("mailfile: %s: child died on signal %d",
56497c478bd9Sstevel@tonic-gate 			       mailer->m_name, st);
56507c478bd9Sstevel@tonic-gate 			return EX_UNAVAILABLE;
56517c478bd9Sstevel@tonic-gate 		}
56527c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
56537c478bd9Sstevel@tonic-gate 	}
56547c478bd9Sstevel@tonic-gate 	return EX_UNAVAILABLE;	/* avoid compiler warning on IRIX */
56557c478bd9Sstevel@tonic-gate }
56567c478bd9Sstevel@tonic-gate 
56577c478bd9Sstevel@tonic-gate static void
56587c478bd9Sstevel@tonic-gate mailfiletimeout(ignore)
56597c478bd9Sstevel@tonic-gate 	int ignore;
56607c478bd9Sstevel@tonic-gate {
56617c478bd9Sstevel@tonic-gate 	/*
56627c478bd9Sstevel@tonic-gate 	**  NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
56637c478bd9Sstevel@tonic-gate 	**	ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
56647c478bd9Sstevel@tonic-gate 	**	DOING.
56657c478bd9Sstevel@tonic-gate 	*/
56667c478bd9Sstevel@tonic-gate 
56677c478bd9Sstevel@tonic-gate 	errno = ETIMEDOUT;
56687c478bd9Sstevel@tonic-gate 	longjmp(CtxMailfileTimeout, 1);
56697c478bd9Sstevel@tonic-gate }
56707c478bd9Sstevel@tonic-gate /*
56717c478bd9Sstevel@tonic-gate **  HOSTSIGNATURE -- return the "signature" for a host.
56727c478bd9Sstevel@tonic-gate **
56737c478bd9Sstevel@tonic-gate **	The signature describes how we are going to send this -- it
56747c478bd9Sstevel@tonic-gate **	can be just the hostname (for non-Internet hosts) or can be
56757c478bd9Sstevel@tonic-gate **	an ordered list of MX hosts.
56767c478bd9Sstevel@tonic-gate **
56777c478bd9Sstevel@tonic-gate **	Parameters:
56787c478bd9Sstevel@tonic-gate **		m -- the mailer describing this host.
56797c478bd9Sstevel@tonic-gate **		host -- the host name.
56807c478bd9Sstevel@tonic-gate **
56817c478bd9Sstevel@tonic-gate **	Returns:
56827c478bd9Sstevel@tonic-gate **		The signature for this host.
56837c478bd9Sstevel@tonic-gate **
56847c478bd9Sstevel@tonic-gate **	Side Effects:
56857c478bd9Sstevel@tonic-gate **		Can tweak the symbol table.
56867c478bd9Sstevel@tonic-gate */
56877c478bd9Sstevel@tonic-gate 
56887c478bd9Sstevel@tonic-gate #define MAXHOSTSIGNATURE	8192	/* max len of hostsignature */
56897c478bd9Sstevel@tonic-gate 
56907c478bd9Sstevel@tonic-gate char *
56917c478bd9Sstevel@tonic-gate hostsignature(m, host)
56927c478bd9Sstevel@tonic-gate 	register MAILER *m;
56937c478bd9Sstevel@tonic-gate 	char *host;
56947c478bd9Sstevel@tonic-gate {
56957c478bd9Sstevel@tonic-gate 	register char *p;
56967c478bd9Sstevel@tonic-gate 	register STAB *s;
56977c478bd9Sstevel@tonic-gate 	time_t now;
56987c478bd9Sstevel@tonic-gate #if NAMED_BIND
56997c478bd9Sstevel@tonic-gate 	char sep = ':';
57007c478bd9Sstevel@tonic-gate 	char prevsep = ':';
57017c478bd9Sstevel@tonic-gate 	int i;
57027c478bd9Sstevel@tonic-gate 	int len;
57037c478bd9Sstevel@tonic-gate 	int nmx;
57047c478bd9Sstevel@tonic-gate 	int hl;
57057c478bd9Sstevel@tonic-gate 	char *hp;
57067c478bd9Sstevel@tonic-gate 	char *endp;
57077c478bd9Sstevel@tonic-gate 	int oldoptions = _res.options;
57087c478bd9Sstevel@tonic-gate 	char *mxhosts[MAXMXHOSTS + 1];
57097c478bd9Sstevel@tonic-gate 	unsigned short mxprefs[MAXMXHOSTS + 1];
57107c478bd9Sstevel@tonic-gate #endif /* NAMED_BIND */
57117c478bd9Sstevel@tonic-gate 
57127c478bd9Sstevel@tonic-gate 	if (tTd(17, 3))
57137c478bd9Sstevel@tonic-gate 		sm_dprintf("hostsignature(%s)\n", host);
57147c478bd9Sstevel@tonic-gate 
57157c478bd9Sstevel@tonic-gate 	/*
57167c478bd9Sstevel@tonic-gate 	**  If local delivery (and not remote), just return a constant.
57177c478bd9Sstevel@tonic-gate 	*/
57187c478bd9Sstevel@tonic-gate 
57197c478bd9Sstevel@tonic-gate 	if (bitnset(M_LOCALMAILER, m->m_flags) &&
57207c478bd9Sstevel@tonic-gate 	    strcmp(m->m_mailer, "[IPC]") != 0 &&
57217c478bd9Sstevel@tonic-gate 	    !(m->m_argv[0] != NULL && strcmp(m->m_argv[0], "TCP") == 0))
57227c478bd9Sstevel@tonic-gate 		return "localhost";
57237c478bd9Sstevel@tonic-gate 
57247c478bd9Sstevel@tonic-gate 	/* an empty host does not have MX records */
57257c478bd9Sstevel@tonic-gate 	if (*host == '\0')
57267c478bd9Sstevel@tonic-gate 		return "_empty_";
57277c478bd9Sstevel@tonic-gate 
57287c478bd9Sstevel@tonic-gate 	/*
57297c478bd9Sstevel@tonic-gate 	**  Check to see if this uses IPC -- if not, it can't have MX records.
57307c478bd9Sstevel@tonic-gate 	*/
57317c478bd9Sstevel@tonic-gate 
57327c478bd9Sstevel@tonic-gate 	if (strcmp(m->m_mailer, "[IPC]") != 0 ||
57337c478bd9Sstevel@tonic-gate 	    CurEnv->e_sendmode == SM_DEFER)
57347c478bd9Sstevel@tonic-gate 	{
57357c478bd9Sstevel@tonic-gate 		/* just an ordinary mailer or deferred mode */
57367c478bd9Sstevel@tonic-gate 		return host;
57377c478bd9Sstevel@tonic-gate 	}
57387c478bd9Sstevel@tonic-gate #if NETUNIX
57397c478bd9Sstevel@tonic-gate 	else if (m->m_argv[0] != NULL &&
57407c478bd9Sstevel@tonic-gate 		 strcmp(m->m_argv[0], "FILE") == 0)
57417c478bd9Sstevel@tonic-gate 	{
57427c478bd9Sstevel@tonic-gate 		/* rendezvous in the file system, no MX records */
57437c478bd9Sstevel@tonic-gate 		return host;
57447c478bd9Sstevel@tonic-gate 	}
57457c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
57467c478bd9Sstevel@tonic-gate 
57477c478bd9Sstevel@tonic-gate 	/*
57487c478bd9Sstevel@tonic-gate 	**  Look it up in the symbol table.
57497c478bd9Sstevel@tonic-gate 	*/
57507c478bd9Sstevel@tonic-gate 
57517c478bd9Sstevel@tonic-gate 	now = curtime();
57527c478bd9Sstevel@tonic-gate 	s = stab(host, ST_HOSTSIG, ST_ENTER);
57537c478bd9Sstevel@tonic-gate 	if (s->s_hostsig.hs_sig != NULL)
57547c478bd9Sstevel@tonic-gate 	{
57557c478bd9Sstevel@tonic-gate 		if (s->s_hostsig.hs_exp >= now)
57567c478bd9Sstevel@tonic-gate 		{
57577c478bd9Sstevel@tonic-gate 			if (tTd(17, 3))
57587c478bd9Sstevel@tonic-gate 				sm_dprintf("hostsignature(): stab(%s) found %s\n", host,
57597c478bd9Sstevel@tonic-gate 					   s->s_hostsig.hs_sig);
57607c478bd9Sstevel@tonic-gate 			return s->s_hostsig.hs_sig;
57617c478bd9Sstevel@tonic-gate 		}
57627c478bd9Sstevel@tonic-gate 
57637c478bd9Sstevel@tonic-gate 		/* signature is expired: clear it */
57647c478bd9Sstevel@tonic-gate 		sm_free(s->s_hostsig.hs_sig);
57657c478bd9Sstevel@tonic-gate 		s->s_hostsig.hs_sig = NULL;
57667c478bd9Sstevel@tonic-gate 	}
57677c478bd9Sstevel@tonic-gate 
57687c478bd9Sstevel@tonic-gate 	/* set default TTL */
57697c478bd9Sstevel@tonic-gate 	s->s_hostsig.hs_exp = now + SM_DEFAULT_TTL;
57707c478bd9Sstevel@tonic-gate 
57717c478bd9Sstevel@tonic-gate 	/*
57727c478bd9Sstevel@tonic-gate 	**  Not already there or expired -- create a signature.
57737c478bd9Sstevel@tonic-gate 	*/
57747c478bd9Sstevel@tonic-gate 
57757c478bd9Sstevel@tonic-gate #if NAMED_BIND
57767c478bd9Sstevel@tonic-gate 	if (ConfigLevel < 2)
57777c478bd9Sstevel@tonic-gate 		_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);	/* XXX */
57787c478bd9Sstevel@tonic-gate 
57797c478bd9Sstevel@tonic-gate 	for (hp = host; hp != NULL; hp = endp)
57807c478bd9Sstevel@tonic-gate 	{
57817c478bd9Sstevel@tonic-gate #if NETINET6
57827c478bd9Sstevel@tonic-gate 		if (*hp == '[')
57837c478bd9Sstevel@tonic-gate 		{
57847c478bd9Sstevel@tonic-gate 			endp = strchr(hp + 1, ']');
57857c478bd9Sstevel@tonic-gate 			if (endp != NULL)
57867c478bd9Sstevel@tonic-gate 				endp = strpbrk(endp + 1, ":,");
57877c478bd9Sstevel@tonic-gate 		}
57887c478bd9Sstevel@tonic-gate 		else
57897c478bd9Sstevel@tonic-gate 			endp = strpbrk(hp, ":,");
57907c478bd9Sstevel@tonic-gate #else /* NETINET6 */
57917c478bd9Sstevel@tonic-gate 		endp = strpbrk(hp, ":,");
57927c478bd9Sstevel@tonic-gate #endif /* NETINET6 */
57937c478bd9Sstevel@tonic-gate 		if (endp != NULL)
57947c478bd9Sstevel@tonic-gate 		{
57957c478bd9Sstevel@tonic-gate 			sep = *endp;
57967c478bd9Sstevel@tonic-gate 			*endp = '\0';
57977c478bd9Sstevel@tonic-gate 		}
57987c478bd9Sstevel@tonic-gate 
57997c478bd9Sstevel@tonic-gate 		if (bitnset(M_NOMX, m->m_flags))
58007c478bd9Sstevel@tonic-gate 		{
58017c478bd9Sstevel@tonic-gate 			/* skip MX lookups */
58027c478bd9Sstevel@tonic-gate 			nmx = 1;
58037c478bd9Sstevel@tonic-gate 			mxhosts[0] = hp;
58047c478bd9Sstevel@tonic-gate 		}
58057c478bd9Sstevel@tonic-gate 		else
58067c478bd9Sstevel@tonic-gate 		{
58077c478bd9Sstevel@tonic-gate 			auto int rcode;
58087c478bd9Sstevel@tonic-gate 			int ttl;
58097c478bd9Sstevel@tonic-gate 
58107c478bd9Sstevel@tonic-gate 			nmx = getmxrr(hp, mxhosts, mxprefs, true, &rcode, true,
58117c478bd9Sstevel@tonic-gate 				      &ttl);
58127c478bd9Sstevel@tonic-gate 			if (nmx <= 0)
58137c478bd9Sstevel@tonic-gate 			{
58147c478bd9Sstevel@tonic-gate 				int save_errno;
58157c478bd9Sstevel@tonic-gate 				register MCI *mci;
58167c478bd9Sstevel@tonic-gate 
58177c478bd9Sstevel@tonic-gate 				/* update the connection info for this host */
58187c478bd9Sstevel@tonic-gate 				save_errno = errno;
58197c478bd9Sstevel@tonic-gate 				mci = mci_get(hp, m);
58207c478bd9Sstevel@tonic-gate 				mci->mci_errno = save_errno;
58217c478bd9Sstevel@tonic-gate 				mci->mci_herrno = h_errno;
58227c478bd9Sstevel@tonic-gate 				mci->mci_lastuse = now;
58237c478bd9Sstevel@tonic-gate 				if (rcode == EX_NOHOST)
58247c478bd9Sstevel@tonic-gate 					mci_setstat(mci, rcode, "5.1.2",
58257c478bd9Sstevel@tonic-gate 						    "550 Host unknown");
58267c478bd9Sstevel@tonic-gate 				else
58277c478bd9Sstevel@tonic-gate 					mci_setstat(mci, rcode, NULL, NULL);
58287c478bd9Sstevel@tonic-gate 
58297c478bd9Sstevel@tonic-gate 				/* use the original host name as signature */
58307c478bd9Sstevel@tonic-gate 				nmx = 1;
58317c478bd9Sstevel@tonic-gate 				mxhosts[0] = hp;
58327c478bd9Sstevel@tonic-gate 			}
58337c478bd9Sstevel@tonic-gate 			if (tTd(17, 3))
58347c478bd9Sstevel@tonic-gate 				sm_dprintf("hostsignature(): getmxrr() returned %d, mxhosts[0]=%s\n",
58357c478bd9Sstevel@tonic-gate 					   nmx, mxhosts[0]);
58367c478bd9Sstevel@tonic-gate 
58377c478bd9Sstevel@tonic-gate 			/*
58387c478bd9Sstevel@tonic-gate 			**  Set new TTL: we use only one!
58397c478bd9Sstevel@tonic-gate 			**	We could try to use the minimum instead.
58407c478bd9Sstevel@tonic-gate 			*/
58417c478bd9Sstevel@tonic-gate 
58427c478bd9Sstevel@tonic-gate 			s->s_hostsig.hs_exp = now + SM_MIN(ttl, SM_DEFAULT_TTL);
58437c478bd9Sstevel@tonic-gate 		}
58447c478bd9Sstevel@tonic-gate 
58457c478bd9Sstevel@tonic-gate 		len = 0;
58467c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmx; i++)
58477c478bd9Sstevel@tonic-gate 			len += strlen(mxhosts[i]) + 1;
58487c478bd9Sstevel@tonic-gate 		if (s->s_hostsig.hs_sig != NULL)
58497c478bd9Sstevel@tonic-gate 			len += strlen(s->s_hostsig.hs_sig) + 1;
58507c478bd9Sstevel@tonic-gate 		if (len < 0 || len >= MAXHOSTSIGNATURE)
58517c478bd9Sstevel@tonic-gate 		{
58527c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID, "hostsignature for host '%s' exceeds maxlen (%d): %d",
58537c478bd9Sstevel@tonic-gate 				  host, MAXHOSTSIGNATURE, len);
58547c478bd9Sstevel@tonic-gate 			len = MAXHOSTSIGNATURE;
58557c478bd9Sstevel@tonic-gate 		}
58567c478bd9Sstevel@tonic-gate 		p = sm_pmalloc_x(len);
58577c478bd9Sstevel@tonic-gate 		if (s->s_hostsig.hs_sig != NULL)
58587c478bd9Sstevel@tonic-gate 		{
58597c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(p, s->s_hostsig.hs_sig, len);
58607c478bd9Sstevel@tonic-gate 			sm_free(s->s_hostsig.hs_sig); /* XXX */
58617c478bd9Sstevel@tonic-gate 			s->s_hostsig.hs_sig = p;
58627c478bd9Sstevel@tonic-gate 			hl = strlen(p);
58637c478bd9Sstevel@tonic-gate 			p += hl;
58647c478bd9Sstevel@tonic-gate 			*p++ = prevsep;
58657c478bd9Sstevel@tonic-gate 			len -= hl + 1;
58667c478bd9Sstevel@tonic-gate 		}
58677c478bd9Sstevel@tonic-gate 		else
58687c478bd9Sstevel@tonic-gate 			s->s_hostsig.hs_sig = p;
58697c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmx; i++)
58707c478bd9Sstevel@tonic-gate 		{
58717c478bd9Sstevel@tonic-gate 			hl = strlen(mxhosts[i]);
58727c478bd9Sstevel@tonic-gate 			if (len - 1 < hl || len <= 1)
58737c478bd9Sstevel@tonic-gate 			{
58747c478bd9Sstevel@tonic-gate 				/* force to drop out of outer loop */
58757c478bd9Sstevel@tonic-gate 				len = -1;
58767c478bd9Sstevel@tonic-gate 				break;
58777c478bd9Sstevel@tonic-gate 			}
58787c478bd9Sstevel@tonic-gate 			if (i != 0)
58797c478bd9Sstevel@tonic-gate 			{
58807c478bd9Sstevel@tonic-gate 				if (mxprefs[i] == mxprefs[i - 1])
58817c478bd9Sstevel@tonic-gate 					*p++ = ',';
58827c478bd9Sstevel@tonic-gate 				else
58837c478bd9Sstevel@tonic-gate 					*p++ = ':';
58847c478bd9Sstevel@tonic-gate 				len--;
58857c478bd9Sstevel@tonic-gate 			}
58867c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(p, mxhosts[i], len);
58877c478bd9Sstevel@tonic-gate 			p += hl;
58887c478bd9Sstevel@tonic-gate 			len -= hl;
58897c478bd9Sstevel@tonic-gate 		}
58907c478bd9Sstevel@tonic-gate 
58917c478bd9Sstevel@tonic-gate 		/*
58927c478bd9Sstevel@tonic-gate 		**  break out of loop if len exceeded MAXHOSTSIGNATURE
58937c478bd9Sstevel@tonic-gate 		**  because we won't have more space for further hosts
58947c478bd9Sstevel@tonic-gate 		**  anyway (separated by : in the .cf file).
58957c478bd9Sstevel@tonic-gate 		*/
58967c478bd9Sstevel@tonic-gate 
58977c478bd9Sstevel@tonic-gate 		if (len < 0)
58987c478bd9Sstevel@tonic-gate 			break;
58997c478bd9Sstevel@tonic-gate 		if (endp != NULL)
59007c478bd9Sstevel@tonic-gate 			*endp++ = sep;
59017c478bd9Sstevel@tonic-gate 		prevsep = sep;
59027c478bd9Sstevel@tonic-gate 	}
59037c478bd9Sstevel@tonic-gate 	makelower(s->s_hostsig.hs_sig);
59047c478bd9Sstevel@tonic-gate 	if (ConfigLevel < 2)
59057c478bd9Sstevel@tonic-gate 		_res.options = oldoptions;
59067c478bd9Sstevel@tonic-gate #else /* NAMED_BIND */
59077c478bd9Sstevel@tonic-gate 	/* not using BIND -- the signature is just the host name */
59087c478bd9Sstevel@tonic-gate 	/*
59097c478bd9Sstevel@tonic-gate 	**  'host' points to storage that will be freed after we are
59107c478bd9Sstevel@tonic-gate 	**  done processing the current envelope, so we copy it.
59117c478bd9Sstevel@tonic-gate 	*/
59127c478bd9Sstevel@tonic-gate 	s->s_hostsig.hs_sig = sm_pstrdup_x(host);
59137c478bd9Sstevel@tonic-gate #endif /* NAMED_BIND */
59147c478bd9Sstevel@tonic-gate 	if (tTd(17, 1))
59157c478bd9Sstevel@tonic-gate 		sm_dprintf("hostsignature(%s) = %s\n", host, s->s_hostsig.hs_sig);
59167c478bd9Sstevel@tonic-gate 	return s->s_hostsig.hs_sig;
59177c478bd9Sstevel@tonic-gate }
59187c478bd9Sstevel@tonic-gate /*
59197c478bd9Sstevel@tonic-gate **  PARSE_HOSTSIGNATURE -- parse the "signature" and return MX host array.
59207c478bd9Sstevel@tonic-gate **
59217c478bd9Sstevel@tonic-gate **	The signature describes how we are going to send this -- it
59227c478bd9Sstevel@tonic-gate **	can be just the hostname (for non-Internet hosts) or can be
59237c478bd9Sstevel@tonic-gate **	an ordered list of MX hosts which must be randomized for equal
59247c478bd9Sstevel@tonic-gate **	MX preference values.
59257c478bd9Sstevel@tonic-gate **
59267c478bd9Sstevel@tonic-gate **	Parameters:
59277c478bd9Sstevel@tonic-gate **		sig -- the host signature.
59287c478bd9Sstevel@tonic-gate **		mxhosts -- array to populate.
59297c478bd9Sstevel@tonic-gate **		mailer -- mailer.
59307c478bd9Sstevel@tonic-gate **
59317c478bd9Sstevel@tonic-gate **	Returns:
59327c478bd9Sstevel@tonic-gate **		The number of hosts inserted into mxhosts array.
59337c478bd9Sstevel@tonic-gate **
59347c478bd9Sstevel@tonic-gate **	Side Effects:
59357c478bd9Sstevel@tonic-gate **		Randomizes equal MX preference hosts in mxhosts.
59367c478bd9Sstevel@tonic-gate */
59377c478bd9Sstevel@tonic-gate 
59387c478bd9Sstevel@tonic-gate static int
59397c478bd9Sstevel@tonic-gate parse_hostsignature(sig, mxhosts, mailer)
59407c478bd9Sstevel@tonic-gate 	char *sig;
59417c478bd9Sstevel@tonic-gate 	char **mxhosts;
59427c478bd9Sstevel@tonic-gate 	MAILER *mailer;
59437c478bd9Sstevel@tonic-gate {
59447c478bd9Sstevel@tonic-gate 	unsigned short curpref = 0;
59457c478bd9Sstevel@tonic-gate 	int nmx = 0, i, j;	/* NOTE: i, j, and nmx must have same type */
59467c478bd9Sstevel@tonic-gate 	char *hp, *endp;
59477c478bd9Sstevel@tonic-gate 	unsigned short prefer[MAXMXHOSTS];
59487c478bd9Sstevel@tonic-gate 	long rndm[MAXMXHOSTS];
59497c478bd9Sstevel@tonic-gate 
59507c478bd9Sstevel@tonic-gate 	for (hp = sig; hp != NULL; hp = endp)
59517c478bd9Sstevel@tonic-gate 	{
59527c478bd9Sstevel@tonic-gate 		char sep = ':';
59537c478bd9Sstevel@tonic-gate 
59547c478bd9Sstevel@tonic-gate #if NETINET6
59557c478bd9Sstevel@tonic-gate 		if (*hp == '[')
59567c478bd9Sstevel@tonic-gate 		{
59577c478bd9Sstevel@tonic-gate 			endp = strchr(hp + 1, ']');
59587c478bd9Sstevel@tonic-gate 			if (endp != NULL)
59597c478bd9Sstevel@tonic-gate 				endp = strpbrk(endp + 1, ":,");
59607c478bd9Sstevel@tonic-gate 		}
59617c478bd9Sstevel@tonic-gate 		else
59627c478bd9Sstevel@tonic-gate 			endp = strpbrk(hp, ":,");
59637c478bd9Sstevel@tonic-gate #else /* NETINET6 */
59647c478bd9Sstevel@tonic-gate 		endp = strpbrk(hp, ":,");
59657c478bd9Sstevel@tonic-gate #endif /* NETINET6 */
59667c478bd9Sstevel@tonic-gate 		if (endp != NULL)
59677c478bd9Sstevel@tonic-gate 		{
59687c478bd9Sstevel@tonic-gate 			sep = *endp;
59697c478bd9Sstevel@tonic-gate 			*endp = '\0';
59707c478bd9Sstevel@tonic-gate 		}
59717c478bd9Sstevel@tonic-gate 
59727c478bd9Sstevel@tonic-gate 		mxhosts[nmx] = hp;
59737c478bd9Sstevel@tonic-gate 		prefer[nmx] = curpref;
59747c478bd9Sstevel@tonic-gate 		if (mci_match(hp, mailer))
59757c478bd9Sstevel@tonic-gate 			rndm[nmx] = 0;
59767c478bd9Sstevel@tonic-gate 		else
59777c478bd9Sstevel@tonic-gate 			rndm[nmx] = get_random();
59787c478bd9Sstevel@tonic-gate 
59797c478bd9Sstevel@tonic-gate 		if (endp != NULL)
59807c478bd9Sstevel@tonic-gate 		{
59817c478bd9Sstevel@tonic-gate 			/*
59827c478bd9Sstevel@tonic-gate 			**  Since we don't have the original MX prefs,
59837c478bd9Sstevel@tonic-gate 			**  make our own.  If the separator is a ':', that
59847c478bd9Sstevel@tonic-gate 			**  means the preference for the next host will be
59857c478bd9Sstevel@tonic-gate 			**  higher than this one, so simply increment curpref.
59867c478bd9Sstevel@tonic-gate 			*/
59877c478bd9Sstevel@tonic-gate 
59887c478bd9Sstevel@tonic-gate 			if (sep == ':')
59897c478bd9Sstevel@tonic-gate 				curpref++;
59907c478bd9Sstevel@tonic-gate 
59917c478bd9Sstevel@tonic-gate 			*endp++ = sep;
59927c478bd9Sstevel@tonic-gate 		}
59937c478bd9Sstevel@tonic-gate 		if (++nmx >= MAXMXHOSTS)
59947c478bd9Sstevel@tonic-gate 			break;
59957c478bd9Sstevel@tonic-gate 	}
59967c478bd9Sstevel@tonic-gate 
59977c478bd9Sstevel@tonic-gate 	/* sort the records using the random factor for equal preferences */
59987c478bd9Sstevel@tonic-gate 	for (i = 0; i < nmx; i++)
59997c478bd9Sstevel@tonic-gate 	{
60007c478bd9Sstevel@tonic-gate 		for (j = i + 1; j < nmx; j++)
60017c478bd9Sstevel@tonic-gate 		{
60027c478bd9Sstevel@tonic-gate 			/*
60037c478bd9Sstevel@tonic-gate 			**  List is already sorted by MX preference, only
60047c478bd9Sstevel@tonic-gate 			**  need to look for equal preference MX records
60057c478bd9Sstevel@tonic-gate 			*/
60067c478bd9Sstevel@tonic-gate 
60077c478bd9Sstevel@tonic-gate 			if (prefer[i] < prefer[j])
60087c478bd9Sstevel@tonic-gate 				break;
60097c478bd9Sstevel@tonic-gate 
60107c478bd9Sstevel@tonic-gate 			if (prefer[i] > prefer[j] ||
60117c478bd9Sstevel@tonic-gate 			    (prefer[i] == prefer[j] && rndm[i] > rndm[j]))
60127c478bd9Sstevel@tonic-gate 			{
60137c478bd9Sstevel@tonic-gate 				register unsigned short tempp;
60147c478bd9Sstevel@tonic-gate 				register long tempr;
60157c478bd9Sstevel@tonic-gate 				register char *temp1;
60167c478bd9Sstevel@tonic-gate 
60177c478bd9Sstevel@tonic-gate 				tempp = prefer[i];
60187c478bd9Sstevel@tonic-gate 				prefer[i] = prefer[j];
60197c478bd9Sstevel@tonic-gate 				prefer[j] = tempp;
60207c478bd9Sstevel@tonic-gate 				temp1 = mxhosts[i];
60217c478bd9Sstevel@tonic-gate 				mxhosts[i] = mxhosts[j];
60227c478bd9Sstevel@tonic-gate 				mxhosts[j] = temp1;
60237c478bd9Sstevel@tonic-gate 				tempr = rndm[i];
60247c478bd9Sstevel@tonic-gate 				rndm[i] = rndm[j];
60257c478bd9Sstevel@tonic-gate 				rndm[j] = tempr;
60267c478bd9Sstevel@tonic-gate 			}
60277c478bd9Sstevel@tonic-gate 		}
60287c478bd9Sstevel@tonic-gate 	}
60297c478bd9Sstevel@tonic-gate 	return nmx;
60307c478bd9Sstevel@tonic-gate }
60317c478bd9Sstevel@tonic-gate 
60327c478bd9Sstevel@tonic-gate # if STARTTLS
60337c478bd9Sstevel@tonic-gate static SSL_CTX	*clt_ctx = NULL;
60347c478bd9Sstevel@tonic-gate static bool	tls_ok_clt = true;
60357c478bd9Sstevel@tonic-gate 
60367c478bd9Sstevel@tonic-gate /*
60377c478bd9Sstevel@tonic-gate **  SETCLTTLS -- client side TLS: allow/disallow.
60387c478bd9Sstevel@tonic-gate **
60397c478bd9Sstevel@tonic-gate **	Parameters:
60407c478bd9Sstevel@tonic-gate **		tls_ok -- should tls be done?
60417c478bd9Sstevel@tonic-gate **
60427c478bd9Sstevel@tonic-gate **	Returns:
60437c478bd9Sstevel@tonic-gate **		none.
60447c478bd9Sstevel@tonic-gate **
60457c478bd9Sstevel@tonic-gate **	Side Effects:
60467c478bd9Sstevel@tonic-gate **		sets tls_ok_clt (static variable in this module)
60477c478bd9Sstevel@tonic-gate */
60487c478bd9Sstevel@tonic-gate 
60497c478bd9Sstevel@tonic-gate void
60507c478bd9Sstevel@tonic-gate setclttls(tls_ok)
60517c478bd9Sstevel@tonic-gate 	bool tls_ok;
60527c478bd9Sstevel@tonic-gate {
60537c478bd9Sstevel@tonic-gate 	tls_ok_clt = tls_ok;
60547c478bd9Sstevel@tonic-gate 	return;
60557c478bd9Sstevel@tonic-gate }
60567c478bd9Sstevel@tonic-gate /*
60577c478bd9Sstevel@tonic-gate **  INITCLTTLS -- initialize client side TLS
60587c478bd9Sstevel@tonic-gate **
60597c478bd9Sstevel@tonic-gate **	Parameters:
60607c478bd9Sstevel@tonic-gate **		tls_ok -- should tls initialization be done?
60617c478bd9Sstevel@tonic-gate **
60627c478bd9Sstevel@tonic-gate **	Returns:
60637c478bd9Sstevel@tonic-gate **		succeeded?
60647c478bd9Sstevel@tonic-gate **
60657c478bd9Sstevel@tonic-gate **	Side Effects:
60667c478bd9Sstevel@tonic-gate **		sets tls_ok_clt (static variable in this module)
60677c478bd9Sstevel@tonic-gate */
60687c478bd9Sstevel@tonic-gate 
60697c478bd9Sstevel@tonic-gate bool
60707c478bd9Sstevel@tonic-gate initclttls(tls_ok)
60717c478bd9Sstevel@tonic-gate 	bool tls_ok;
60727c478bd9Sstevel@tonic-gate {
60737c478bd9Sstevel@tonic-gate 	if (!tls_ok_clt)
60747c478bd9Sstevel@tonic-gate 		return false;
60757c478bd9Sstevel@tonic-gate 	tls_ok_clt = tls_ok;
60767c478bd9Sstevel@tonic-gate 	if (!tls_ok_clt)
60777c478bd9Sstevel@tonic-gate 		return false;
60787c478bd9Sstevel@tonic-gate 	if (clt_ctx != NULL)
60797c478bd9Sstevel@tonic-gate 		return true;	/* already done */
60807c478bd9Sstevel@tonic-gate 	tls_ok_clt = inittls(&clt_ctx, TLS_I_CLT, false, CltCertFile,
60817c478bd9Sstevel@tonic-gate 			     CltKeyFile, CACertPath, CACertFile, DHParams);
60827c478bd9Sstevel@tonic-gate 	return tls_ok_clt;
60837c478bd9Sstevel@tonic-gate }
60847c478bd9Sstevel@tonic-gate 
60857c478bd9Sstevel@tonic-gate /*
60867c478bd9Sstevel@tonic-gate **  STARTTLS -- try to start secure connection (client side)
60877c478bd9Sstevel@tonic-gate **
60887c478bd9Sstevel@tonic-gate **	Parameters:
60897c478bd9Sstevel@tonic-gate **		m -- the mailer.
60907c478bd9Sstevel@tonic-gate **		mci -- the mailer connection info.
60917c478bd9Sstevel@tonic-gate **		e -- the envelope.
60927c478bd9Sstevel@tonic-gate **
60937c478bd9Sstevel@tonic-gate **	Returns:
60947c478bd9Sstevel@tonic-gate **		success?
60957c478bd9Sstevel@tonic-gate **		(maybe this should be some other code than EX_
60967c478bd9Sstevel@tonic-gate **		that denotes which stage failed.)
60977c478bd9Sstevel@tonic-gate */
60987c478bd9Sstevel@tonic-gate 
60997c478bd9Sstevel@tonic-gate static int
61007c478bd9Sstevel@tonic-gate starttls(m, mci, e)
61017c478bd9Sstevel@tonic-gate 	MAILER *m;
61027c478bd9Sstevel@tonic-gate 	MCI *mci;
61037c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
61047c478bd9Sstevel@tonic-gate {
61057c478bd9Sstevel@tonic-gate 	int smtpresult;
61067c478bd9Sstevel@tonic-gate 	int result = 0;
61077c478bd9Sstevel@tonic-gate 	int rfd, wfd;
61087c478bd9Sstevel@tonic-gate 	SSL *clt_ssl = NULL;
61097c478bd9Sstevel@tonic-gate 	time_t tlsstart;
61107c478bd9Sstevel@tonic-gate 
61117c478bd9Sstevel@tonic-gate 	if (clt_ctx == NULL && !initclttls(true))
61127c478bd9Sstevel@tonic-gate 		return EX_TEMPFAIL;
61137c478bd9Sstevel@tonic-gate 	smtpmessage("STARTTLS", m, mci);
61147c478bd9Sstevel@tonic-gate 
61157c478bd9Sstevel@tonic-gate 	/* get the reply */
61167c478bd9Sstevel@tonic-gate 	smtpresult = reply(m, mci, e, TimeOuts.to_starttls, NULL, NULL,
61177c478bd9Sstevel@tonic-gate 			XS_STARTTLS);
61187c478bd9Sstevel@tonic-gate 
61197c478bd9Sstevel@tonic-gate 	/* check return code from server */
6120445f2479Sjbeck 	if (REPLYTYPE(smtpresult) == 4)
61217c478bd9Sstevel@tonic-gate 		return EX_TEMPFAIL;
61227c478bd9Sstevel@tonic-gate 	if (smtpresult == 501)
61237c478bd9Sstevel@tonic-gate 		return EX_USAGE;
61247c478bd9Sstevel@tonic-gate 	if (smtpresult == -1)
61257c478bd9Sstevel@tonic-gate 		return smtpresult;
6126445f2479Sjbeck 
6127445f2479Sjbeck 	/* not an expected reply but we have to deal with it */
6128445f2479Sjbeck 	if (REPLYTYPE(smtpresult) == 5)
6129445f2479Sjbeck 		return EX_UNAVAILABLE;
61307c478bd9Sstevel@tonic-gate 	if (smtpresult != 220)
61317c478bd9Sstevel@tonic-gate 		return EX_PROTOCOL;
61327c478bd9Sstevel@tonic-gate 
61337c478bd9Sstevel@tonic-gate 	if (LogLevel > 13)
61347c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_INFO, NOQID, "STARTTLS=client, start=ok");
61357c478bd9Sstevel@tonic-gate 
61367c478bd9Sstevel@tonic-gate 	/* start connection */
61377c478bd9Sstevel@tonic-gate 	if ((clt_ssl = SSL_new(clt_ctx)) == NULL)
61387c478bd9Sstevel@tonic-gate 	{
61397c478bd9Sstevel@tonic-gate 		if (LogLevel > 5)
61407c478bd9Sstevel@tonic-gate 		{
61417c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_ERR, NOQID,
61427c478bd9Sstevel@tonic-gate 				  "STARTTLS=client, error: SSL_new failed");
61437c478bd9Sstevel@tonic-gate 			if (LogLevel > 9)
61447c478bd9Sstevel@tonic-gate 				tlslogerr("client");
61457c478bd9Sstevel@tonic-gate 		}
61467c478bd9Sstevel@tonic-gate 		return EX_SOFTWARE;
61477c478bd9Sstevel@tonic-gate 	}
61487c478bd9Sstevel@tonic-gate 
61497c478bd9Sstevel@tonic-gate 	rfd = sm_io_getinfo(mci->mci_in, SM_IO_WHAT_FD, NULL);
61507c478bd9Sstevel@tonic-gate 	wfd = sm_io_getinfo(mci->mci_out, SM_IO_WHAT_FD, NULL);
61517c478bd9Sstevel@tonic-gate 
61527c478bd9Sstevel@tonic-gate 	/* SSL_clear(clt_ssl); ? */
61537c478bd9Sstevel@tonic-gate 	if (rfd < 0 || wfd < 0 ||
61547c478bd9Sstevel@tonic-gate 	    (result = SSL_set_rfd(clt_ssl, rfd)) != 1 ||
61557c478bd9Sstevel@tonic-gate 	    (result = SSL_set_wfd(clt_ssl, wfd)) != 1)
61567c478bd9Sstevel@tonic-gate 	{
61577c478bd9Sstevel@tonic-gate 		if (LogLevel > 5)
61587c478bd9Sstevel@tonic-gate 		{
61597c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_ERR, NOQID,
61607c478bd9Sstevel@tonic-gate 				  "STARTTLS=client, error: SSL_set_xfd failed=%d",
61617c478bd9Sstevel@tonic-gate 				  result);
61627c478bd9Sstevel@tonic-gate 			if (LogLevel > 9)
61637c478bd9Sstevel@tonic-gate 				tlslogerr("client");
61647c478bd9Sstevel@tonic-gate 		}
61657c478bd9Sstevel@tonic-gate 		return EX_SOFTWARE;
61667c478bd9Sstevel@tonic-gate 	}
61677c478bd9Sstevel@tonic-gate 	SSL_set_connect_state(clt_ssl);
61687c478bd9Sstevel@tonic-gate 	tlsstart = curtime();
61697c478bd9Sstevel@tonic-gate 
61707c478bd9Sstevel@tonic-gate ssl_retry:
61717c478bd9Sstevel@tonic-gate 	if ((result = SSL_connect(clt_ssl)) <= 0)
61727c478bd9Sstevel@tonic-gate 	{
6173445f2479Sjbeck 		int i, ssl_err;
61747c478bd9Sstevel@tonic-gate 
6175445f2479Sjbeck 		ssl_err = SSL_get_error(clt_ssl, result);
6176445f2479Sjbeck 		i = tls_retry(clt_ssl, rfd, wfd, tlsstart,
6177445f2479Sjbeck 			TimeOuts.to_starttls, ssl_err, "client");
6178445f2479Sjbeck 		if (i > 0)
6179445f2479Sjbeck 			goto ssl_retry;
61807c478bd9Sstevel@tonic-gate 
61817c478bd9Sstevel@tonic-gate 		if (LogLevel > 5)
61827c478bd9Sstevel@tonic-gate 		{
6183445f2479Sjbeck 			sm_syslog(LOG_WARNING, NOQID,
6184445f2479Sjbeck 				  "STARTTLS=client, error: connect failed=%d, SSL_error=%d, errno=%d, retry=%d",
6185445f2479Sjbeck 				  result, ssl_err, errno, i);
61867c478bd9Sstevel@tonic-gate 			if (LogLevel > 8)
61877c478bd9Sstevel@tonic-gate 				tlslogerr("client");
61887c478bd9Sstevel@tonic-gate 		}
61897c478bd9Sstevel@tonic-gate 
61907c478bd9Sstevel@tonic-gate 		SSL_free(clt_ssl);
61917c478bd9Sstevel@tonic-gate 		clt_ssl = NULL;
61927c478bd9Sstevel@tonic-gate 		return EX_SOFTWARE;
61937c478bd9Sstevel@tonic-gate 	}
61947c478bd9Sstevel@tonic-gate 	mci->mci_ssl = clt_ssl;
61957c478bd9Sstevel@tonic-gate 	result = tls_get_info(mci->mci_ssl, false, mci->mci_host,
61967c478bd9Sstevel@tonic-gate 			      &mci->mci_macro, true);
61977c478bd9Sstevel@tonic-gate 
61987c478bd9Sstevel@tonic-gate 	/* switch to use TLS... */
61997c478bd9Sstevel@tonic-gate 	if (sfdctls(&mci->mci_in, &mci->mci_out, mci->mci_ssl) == 0)
62007c478bd9Sstevel@tonic-gate 		return EX_OK;
62017c478bd9Sstevel@tonic-gate 
62027c478bd9Sstevel@tonic-gate 	/* failure */
62037c478bd9Sstevel@tonic-gate 	SSL_free(clt_ssl);
62047c478bd9Sstevel@tonic-gate 	clt_ssl = NULL;
62057c478bd9Sstevel@tonic-gate 	return EX_SOFTWARE;
62067c478bd9Sstevel@tonic-gate }
62077c478bd9Sstevel@tonic-gate /*
62087c478bd9Sstevel@tonic-gate **  ENDTLSCLT -- shutdown secure connection (client side)
62097c478bd9Sstevel@tonic-gate **
62107c478bd9Sstevel@tonic-gate **	Parameters:
62117c478bd9Sstevel@tonic-gate **		mci -- the mailer connection info.
62127c478bd9Sstevel@tonic-gate **
62137c478bd9Sstevel@tonic-gate **	Returns:
62147c478bd9Sstevel@tonic-gate **		success?
62157c478bd9Sstevel@tonic-gate */
62167c478bd9Sstevel@tonic-gate 
62177c478bd9Sstevel@tonic-gate static int
62187c478bd9Sstevel@tonic-gate endtlsclt(mci)
62197c478bd9Sstevel@tonic-gate 	MCI *mci;
62207c478bd9Sstevel@tonic-gate {
62217c478bd9Sstevel@tonic-gate 	int r;
62227c478bd9Sstevel@tonic-gate 
62237c478bd9Sstevel@tonic-gate 	if (!bitset(MCIF_TLSACT, mci->mci_flags))
62247c478bd9Sstevel@tonic-gate 		return EX_OK;
62257c478bd9Sstevel@tonic-gate 	r = endtls(mci->mci_ssl, "client");
62267c478bd9Sstevel@tonic-gate 	mci->mci_flags &= ~MCIF_TLSACT;
62277c478bd9Sstevel@tonic-gate 	return r;
62287c478bd9Sstevel@tonic-gate }
62297c478bd9Sstevel@tonic-gate # endif /* STARTTLS */
62307c478bd9Sstevel@tonic-gate # if STARTTLS || SASL
62317c478bd9Sstevel@tonic-gate /*
62327c478bd9Sstevel@tonic-gate **  ISCLTFLGSET -- check whether client flag is set.
62337c478bd9Sstevel@tonic-gate **
62347c478bd9Sstevel@tonic-gate **	Parameters:
62357c478bd9Sstevel@tonic-gate **		e -- envelope.
62367c478bd9Sstevel@tonic-gate **		flag -- flag to check in {client_flags}
62377c478bd9Sstevel@tonic-gate **
62387c478bd9Sstevel@tonic-gate **	Returns:
62397c478bd9Sstevel@tonic-gate **		true iff flag is set.
62407c478bd9Sstevel@tonic-gate */
62417c478bd9Sstevel@tonic-gate 
62427c478bd9Sstevel@tonic-gate static bool
62437c478bd9Sstevel@tonic-gate iscltflgset(e, flag)
62447c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
62457c478bd9Sstevel@tonic-gate 	int flag;
62467c478bd9Sstevel@tonic-gate {
62477c478bd9Sstevel@tonic-gate 	char *p;
62487c478bd9Sstevel@tonic-gate 
62497c478bd9Sstevel@tonic-gate 	p = macvalue(macid("{client_flags}"), e);
62507c478bd9Sstevel@tonic-gate 	if (p == NULL)
62517c478bd9Sstevel@tonic-gate 		return false;
62527c478bd9Sstevel@tonic-gate 	for (; *p != '\0'; p++)
62537c478bd9Sstevel@tonic-gate 	{
62547c478bd9Sstevel@tonic-gate 		/* look for just this one flag */
62557c478bd9Sstevel@tonic-gate 		if (*p == (char) flag)
62567c478bd9Sstevel@tonic-gate 			return true;
62577c478bd9Sstevel@tonic-gate 	}
62587c478bd9Sstevel@tonic-gate 	return false;
62597c478bd9Sstevel@tonic-gate }
62607c478bd9Sstevel@tonic-gate # endif /* STARTTLS || SASL */
6261