xref: /illumos-gate/usr/src/cmd/sendmail/src/daemon.c (revision 91b4b539)
17c478bd9Sstevel@tonic-gate /*
2e9af4bc0SJohn Beck  * Copyright (c) 1998-2007, 2009 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 #include <sendmail.h>
15058561cbSjbeck #include "map.h"
167c478bd9Sstevel@tonic-gate 
17e9af4bc0SJohn Beck SM_RCSID("@(#)$Id: daemon.c,v 8.683 2009/12/18 01:12:40 ca Exp $")
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate #if defined(SOCK_STREAM) || defined(__GNU_LIBRARY__)
207c478bd9Sstevel@tonic-gate # define USE_SOCK_STREAM	1
217c478bd9Sstevel@tonic-gate #endif /* defined(SOCK_STREAM) || defined(__GNU_LIBRARY__) */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #if defined(USE_SOCK_STREAM)
247c478bd9Sstevel@tonic-gate # if NETINET || NETINET6
257c478bd9Sstevel@tonic-gate #  include <arpa/inet.h>
267c478bd9Sstevel@tonic-gate # endif /* NETINET || NETINET6 */
277c478bd9Sstevel@tonic-gate # if NAMED_BIND
287c478bd9Sstevel@tonic-gate #  ifndef NO_DATA
297c478bd9Sstevel@tonic-gate #   define NO_DATA	NO_ADDRESS
307c478bd9Sstevel@tonic-gate #  endif /* ! NO_DATA */
317c478bd9Sstevel@tonic-gate # endif /* NAMED_BIND */
327c478bd9Sstevel@tonic-gate #endif /* defined(USE_SOCK_STREAM) */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #if STARTTLS
357c478bd9Sstevel@tonic-gate #  include <openssl/rand.h>
367c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
377c478bd9Sstevel@tonic-gate 
3849218d4fSjbeck #include <sm/time.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #if IP_SRCROUTE && NETINET
417c478bd9Sstevel@tonic-gate # include <netinet/in_systm.h>
427c478bd9Sstevel@tonic-gate # include <netinet/ip.h>
437c478bd9Sstevel@tonic-gate # if HAS_IN_H
447c478bd9Sstevel@tonic-gate #  include <netinet/in.h>
457c478bd9Sstevel@tonic-gate #  ifndef IPOPTION
467c478bd9Sstevel@tonic-gate #   define IPOPTION	ip_opts
477c478bd9Sstevel@tonic-gate #   define IP_LIST	ip_opts
487c478bd9Sstevel@tonic-gate #   define IP_DST	ip_dst
497c478bd9Sstevel@tonic-gate #  endif /* ! IPOPTION */
507c478bd9Sstevel@tonic-gate # else /* HAS_IN_H */
517c478bd9Sstevel@tonic-gate #  include <netinet/ip_var.h>
527c478bd9Sstevel@tonic-gate #  ifndef IPOPTION
537c478bd9Sstevel@tonic-gate #   define IPOPTION	ipoption
547c478bd9Sstevel@tonic-gate #   define IP_LIST	ipopt_list
557c478bd9Sstevel@tonic-gate #   define IP_DST	ipopt_dst
567c478bd9Sstevel@tonic-gate #  endif /* ! IPOPTION */
577c478bd9Sstevel@tonic-gate # endif /* HAS_IN_H */
587c478bd9Sstevel@tonic-gate #endif /* IP_SRCROUTE && NETINET */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <sm/fdset.h>
617c478bd9Sstevel@tonic-gate 
62058561cbSjbeck #define DAEMON_C 1
63058561cbSjbeck #include <daemon.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static void		connecttimeout __P((int));
667c478bd9Sstevel@tonic-gate static int		opendaemonsocket __P((DAEMON_T *, bool));
677c478bd9Sstevel@tonic-gate static unsigned short	setupdaemon __P((SOCKADDR *));
687c478bd9Sstevel@tonic-gate static void		getrequests_checkdiskspace __P((ENVELOPE *e));
697c478bd9Sstevel@tonic-gate static void		setsockaddroptions __P((char *, DAEMON_T *));
707c478bd9Sstevel@tonic-gate static void		printdaemonflags __P((DAEMON_T *));
717c478bd9Sstevel@tonic-gate static int		addr_family __P((char *));
727c478bd9Sstevel@tonic-gate static int		addrcmp __P((struct hostent *, char *, SOCKADDR *));
737c478bd9Sstevel@tonic-gate static void		authtimeout __P((int));
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate **  DAEMON.C -- routines to use when running as a daemon.
777c478bd9Sstevel@tonic-gate **
787c478bd9Sstevel@tonic-gate **	This entire file is highly dependent on the 4.2 BSD
797c478bd9Sstevel@tonic-gate **	interprocess communication primitives.  No attempt has
807c478bd9Sstevel@tonic-gate **	been made to make this file portable to Version 7,
817c478bd9Sstevel@tonic-gate **	Version 6, MPX files, etc.  If you should try such a
827c478bd9Sstevel@tonic-gate **	thing yourself, I recommend chucking the entire file
837c478bd9Sstevel@tonic-gate **	and starting from scratch.  Basic semantics are:
847c478bd9Sstevel@tonic-gate **
857c478bd9Sstevel@tonic-gate **	getrequests(e)
867c478bd9Sstevel@tonic-gate **		Opens a port and initiates a connection.
877c478bd9Sstevel@tonic-gate **		Returns in a child.  Must set InChannel and
887c478bd9Sstevel@tonic-gate **		OutChannel appropriately.
897c478bd9Sstevel@tonic-gate **	clrdaemon()
907c478bd9Sstevel@tonic-gate **		Close any open files associated with getting
917c478bd9Sstevel@tonic-gate **		the connection; this is used when running the queue,
927c478bd9Sstevel@tonic-gate **		etc., to avoid having extra file descriptors during
937c478bd9Sstevel@tonic-gate **		the queue run and to avoid confusing the network
947c478bd9Sstevel@tonic-gate **		code (if it cares).
957c478bd9Sstevel@tonic-gate **	makeconnection(host, port, mci, e, enough)
967c478bd9Sstevel@tonic-gate **		Make a connection to the named host on the given
977c478bd9Sstevel@tonic-gate **		port. Returns zero on success, else an exit status
987c478bd9Sstevel@tonic-gate **		describing the error.
997c478bd9Sstevel@tonic-gate **	host_map_lookup(map, hbuf, avp, pstat)
1007c478bd9Sstevel@tonic-gate **		Convert the entry in hbuf into a canonical form.
1017c478bd9Sstevel@tonic-gate */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static int	NDaemons = 0;			/* actual number of daemons */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static time_t	NextDiskSpaceCheck = 0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate **  GETREQUESTS -- open mail IPC port and get requests.
1097c478bd9Sstevel@tonic-gate **
1107c478bd9Sstevel@tonic-gate **	Parameters:
1117c478bd9Sstevel@tonic-gate **		e -- the current envelope.
1127c478bd9Sstevel@tonic-gate **
1137c478bd9Sstevel@tonic-gate **	Returns:
1147c478bd9Sstevel@tonic-gate **		pointer to flags.
1157c478bd9Sstevel@tonic-gate **
1167c478bd9Sstevel@tonic-gate **	Side Effects:
1177c478bd9Sstevel@tonic-gate **		Waits until some interesting activity occurs.  When
1187c478bd9Sstevel@tonic-gate **		it does, a child is created to process it, and the
1197c478bd9Sstevel@tonic-gate **		parent waits for completion.  Return from this
1207c478bd9Sstevel@tonic-gate **		routine is always in the child.  The file pointers
1217c478bd9Sstevel@tonic-gate **		"InChannel" and "OutChannel" should be set to point
1227c478bd9Sstevel@tonic-gate **		to the communication channel.
1237c478bd9Sstevel@tonic-gate **		May restart persistent queue runners if they have ended
1247c478bd9Sstevel@tonic-gate **		for some reason.
1257c478bd9Sstevel@tonic-gate */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate BITMAP256 *
getrequests(e)1287c478bd9Sstevel@tonic-gate getrequests(e)
1297c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	int t;
1327c478bd9Sstevel@tonic-gate 	int idx, curdaemon = -1;
1337c478bd9Sstevel@tonic-gate 	int i, olddaemon = 0;
1347c478bd9Sstevel@tonic-gate #if XDEBUG
1357c478bd9Sstevel@tonic-gate 	bool j_has_dot;
1367c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
1377c478bd9Sstevel@tonic-gate 	char status[MAXLINE];
1387c478bd9Sstevel@tonic-gate 	SOCKADDR sa;
139058561cbSjbeck 	SOCKADDR_LEN_T len = sizeof(sa);
1407c478bd9Sstevel@tonic-gate #if _FFR_QUEUE_RUN_PARANOIA
1417c478bd9Sstevel@tonic-gate 	time_t lastrun;
1427c478bd9Sstevel@tonic-gate #endif /* _FFR_QUEUE_RUN_PARANOIA */
1437c478bd9Sstevel@tonic-gate # if NETUNIX
1447c478bd9Sstevel@tonic-gate 	extern int ControlSocket;
1457c478bd9Sstevel@tonic-gate # endif /* NETUNIX */
1467c478bd9Sstevel@tonic-gate 	extern ENVELOPE BlankEnvelope;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/* initialize data for function that generates queue ids */
1507c478bd9Sstevel@tonic-gate 	init_qid_alg();
1517c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < NDaemons; idx++)
1527c478bd9Sstevel@tonic-gate 	{
1537c478bd9Sstevel@tonic-gate 		Daemons[idx].d_port = setupdaemon(&(Daemons[idx].d_addr));
1547c478bd9Sstevel@tonic-gate 		Daemons[idx].d_firsttime = true;
1557c478bd9Sstevel@tonic-gate 		Daemons[idx].d_refuse_connections_until = (time_t) 0;
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	**  Try to actually open the connection.
1607c478bd9Sstevel@tonic-gate 	*/
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (tTd(15, 1))
1637c478bd9Sstevel@tonic-gate 	{
1647c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < NDaemons; idx++)
1657c478bd9Sstevel@tonic-gate 		{
1667c478bd9Sstevel@tonic-gate 			sm_dprintf("getrequests: daemon %s: port %d\n",
1677c478bd9Sstevel@tonic-gate 				   Daemons[idx].d_name,
1687c478bd9Sstevel@tonic-gate 				   ntohs(Daemons[idx].d_port));
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/* get a socket for the SMTP connection */
1737c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < NDaemons; idx++)
1747c478bd9Sstevel@tonic-gate 		Daemons[idx].d_socksize = opendaemonsocket(&Daemons[idx], true);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (opencontrolsocket() < 0)
1777c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_WARNING, NOQID,
1787c478bd9Sstevel@tonic-gate 			  "daemon could not open control socket %s: %s",
1797c478bd9Sstevel@tonic-gate 			  ControlSocketName, sm_errstring(errno));
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* If there are any queue runners released reapchild() co-ord's */
1827c478bd9Sstevel@tonic-gate 	(void) sm_signal(SIGCHLD, reapchild);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/* write the pid to file, command line args to syslog */
1857c478bd9Sstevel@tonic-gate 	log_sendmail_pid(e);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate #if XDEBUG
1887c478bd9Sstevel@tonic-gate 	{
1897c478bd9Sstevel@tonic-gate 		char jbuf[MAXHOSTNAMELEN];
1907c478bd9Sstevel@tonic-gate 
191058561cbSjbeck 		expand("\201j", jbuf, sizeof(jbuf), e);
1927c478bd9Sstevel@tonic-gate 		j_has_dot = strchr(jbuf, '.') != NULL;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/* Add parent process as first item */
1977c478bd9Sstevel@tonic-gate 	proc_list_add(CurrentPid, "Sendmail daemon", PROC_DAEMON, 0, -1, NULL);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (tTd(15, 1))
2007c478bd9Sstevel@tonic-gate 	{
2017c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < NDaemons; idx++)
202e9af4bc0SJohn Beck 			sm_dprintf("getrequests: daemon %s: socket %d\n",
2037c478bd9Sstevel@tonic-gate 				Daemons[idx].d_name,
2047c478bd9Sstevel@tonic-gate 				Daemons[idx].d_socket);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	for (;;)
2087c478bd9Sstevel@tonic-gate 	{
2097c478bd9Sstevel@tonic-gate 		register pid_t pid;
2107c478bd9Sstevel@tonic-gate 		auto SOCKADDR_LEN_T lotherend;
2117c478bd9Sstevel@tonic-gate 		bool timedout = false;
2127c478bd9Sstevel@tonic-gate 		bool control = false;
2137c478bd9Sstevel@tonic-gate 		int save_errno;
2147c478bd9Sstevel@tonic-gate 		int pipefd[2];
2157c478bd9Sstevel@tonic-gate 		time_t now;
2167c478bd9Sstevel@tonic-gate #if STARTTLS
2177c478bd9Sstevel@tonic-gate 		long seed;
2187c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		/* see if we are rejecting connections */
2217c478bd9Sstevel@tonic-gate 		(void) sm_blocksignal(SIGALRM);
2227c478bd9Sstevel@tonic-gate 		CHECK_RESTART;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < NDaemons; idx++)
2257c478bd9Sstevel@tonic-gate 		{
2267c478bd9Sstevel@tonic-gate 			/*
2277c478bd9Sstevel@tonic-gate 			**  XXX do this call outside the loop?
2287c478bd9Sstevel@tonic-gate 			**	no: refuse_connections may sleep().
2297c478bd9Sstevel@tonic-gate 			*/
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 			now = curtime();
2327c478bd9Sstevel@tonic-gate 			if (now < Daemons[idx].d_refuse_connections_until)
2337c478bd9Sstevel@tonic-gate 				continue;
2347c478bd9Sstevel@tonic-gate 			if (bitnset(D_DISABLE, Daemons[idx].d_flags))
2357c478bd9Sstevel@tonic-gate 				continue;
236058561cbSjbeck 			if (refuseconnections(e, idx, curdaemon == idx))
2377c478bd9Sstevel@tonic-gate 			{
2387c478bd9Sstevel@tonic-gate 				if (Daemons[idx].d_socket >= 0)
2397c478bd9Sstevel@tonic-gate 				{
2407c478bd9Sstevel@tonic-gate 					/* close socket so peer fails quickly */
2417c478bd9Sstevel@tonic-gate 					(void) close(Daemons[idx].d_socket);
2427c478bd9Sstevel@tonic-gate 					Daemons[idx].d_socket = -1;
2437c478bd9Sstevel@tonic-gate 				}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 				/* refuse connections for next 15 seconds */
2467c478bd9Sstevel@tonic-gate 				Daemons[idx].d_refuse_connections_until = now + 15;
2477c478bd9Sstevel@tonic-gate 			}
2487c478bd9Sstevel@tonic-gate 			else if (Daemons[idx].d_socket < 0 ||
2497c478bd9Sstevel@tonic-gate 				 Daemons[idx].d_firsttime)
2507c478bd9Sstevel@tonic-gate 			{
2517c478bd9Sstevel@tonic-gate 				if (!Daemons[idx].d_firsttime && LogLevel > 8)
2527c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, NOQID,
2537c478bd9Sstevel@tonic-gate 						"accepting connections again for daemon %s",
2547c478bd9Sstevel@tonic-gate 						Daemons[idx].d_name);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 				/* arrange to (re)open the socket if needed */
2577c478bd9Sstevel@tonic-gate 				(void) opendaemonsocket(&Daemons[idx], false);
2587c478bd9Sstevel@tonic-gate 				Daemons[idx].d_firsttime = false;
2597c478bd9Sstevel@tonic-gate 			}
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		/* May have been sleeping above, check again */
2637c478bd9Sstevel@tonic-gate 		CHECK_RESTART;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		getrequests_checkdiskspace(e);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate #if XDEBUG
2687c478bd9Sstevel@tonic-gate 		/* check for disaster */
2697c478bd9Sstevel@tonic-gate 		{
2707c478bd9Sstevel@tonic-gate 			char jbuf[MAXHOSTNAMELEN];
2717c478bd9Sstevel@tonic-gate 
272058561cbSjbeck 			expand("\201j", jbuf, sizeof(jbuf), e);
2737c478bd9Sstevel@tonic-gate 			if (!wordinclass(jbuf, 'w'))
2747c478bd9Sstevel@tonic-gate 			{
2757c478bd9Sstevel@tonic-gate 				dumpstate("daemon lost $j");
2767c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_ALERT, NOQID,
2777c478bd9Sstevel@tonic-gate 					  "daemon process doesn't have $j in $=w; see syslog");
2787c478bd9Sstevel@tonic-gate 				abort();
2797c478bd9Sstevel@tonic-gate 			}
2807c478bd9Sstevel@tonic-gate 			else if (j_has_dot && strchr(jbuf, '.') == NULL)
2817c478bd9Sstevel@tonic-gate 			{
2827c478bd9Sstevel@tonic-gate 				dumpstate("daemon $j lost dot");
2837c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_ALERT, NOQID,
2847c478bd9Sstevel@tonic-gate 					  "daemon process $j lost dot; see syslog");
2857c478bd9Sstevel@tonic-gate 				abort();
2867c478bd9Sstevel@tonic-gate 			}
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate #endif /* XDEBUG */
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate #if 0
2917c478bd9Sstevel@tonic-gate 		/*
2927c478bd9Sstevel@tonic-gate 		**  Andrew Sun <asun@ieps-sun.ml.com> claims that this will
2937c478bd9Sstevel@tonic-gate 		**  fix the SVr4 problem.  But it seems to have gone away,
2947c478bd9Sstevel@tonic-gate 		**  so is it worth doing this?
2957c478bd9Sstevel@tonic-gate 		*/
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		if (DaemonSocket >= 0 &&
2987c478bd9Sstevel@tonic-gate 		    SetNonBlocking(DaemonSocket, false) < 0)
2997c478bd9Sstevel@tonic-gate 			log an error here;
3007c478bd9Sstevel@tonic-gate #endif /* 0 */
3017c478bd9Sstevel@tonic-gate 		(void) sm_releasesignal(SIGALRM);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		for (;;)
3047c478bd9Sstevel@tonic-gate 		{
3057c478bd9Sstevel@tonic-gate 			bool setproc = false;
3067c478bd9Sstevel@tonic-gate 			int highest = -1;
3077c478bd9Sstevel@tonic-gate 			fd_set readfds;
3087c478bd9Sstevel@tonic-gate 			struct timeval timeout;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 			CHECK_RESTART;
3117c478bd9Sstevel@tonic-gate 			FD_ZERO(&readfds);
3127c478bd9Sstevel@tonic-gate 			for (idx = 0; idx < NDaemons; idx++)
3137c478bd9Sstevel@tonic-gate 			{
3147c478bd9Sstevel@tonic-gate 				/* wait for a connection */
3157c478bd9Sstevel@tonic-gate 				if (Daemons[idx].d_socket >= 0)
3167c478bd9Sstevel@tonic-gate 				{
3177c478bd9Sstevel@tonic-gate 					if (!setproc &&
3187c478bd9Sstevel@tonic-gate 					    !bitnset(D_ETRNONLY,
3197c478bd9Sstevel@tonic-gate 						     Daemons[idx].d_flags))
3207c478bd9Sstevel@tonic-gate 					{
3217c478bd9Sstevel@tonic-gate 						sm_setproctitle(true, e,
3227c478bd9Sstevel@tonic-gate 								"accepting connections");
3237c478bd9Sstevel@tonic-gate 						setproc = true;
3247c478bd9Sstevel@tonic-gate 					}
3257c478bd9Sstevel@tonic-gate 					if (Daemons[idx].d_socket > highest)
3267c478bd9Sstevel@tonic-gate 						highest = Daemons[idx].d_socket;
3277c478bd9Sstevel@tonic-gate 					SM_FD_SET(Daemons[idx].d_socket,
3287c478bd9Sstevel@tonic-gate 						  &readfds);
3297c478bd9Sstevel@tonic-gate 				}
3307c478bd9Sstevel@tonic-gate 			}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate #if NETUNIX
3337c478bd9Sstevel@tonic-gate 			if (ControlSocket >= 0)
3347c478bd9Sstevel@tonic-gate 			{
3357c478bd9Sstevel@tonic-gate 				if (ControlSocket > highest)
3367c478bd9Sstevel@tonic-gate 					highest = ControlSocket;
3377c478bd9Sstevel@tonic-gate 				SM_FD_SET(ControlSocket, &readfds);
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 			timeout.tv_sec = 5;
3427c478bd9Sstevel@tonic-gate 			timeout.tv_usec = 0;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 			t = select(highest + 1, FDSET_CAST &readfds,
3457c478bd9Sstevel@tonic-gate 				   NULL, NULL, &timeout);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 			/* Did someone signal while waiting? */
3487c478bd9Sstevel@tonic-gate 			CHECK_RESTART;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 			curdaemon = -1;
3517c478bd9Sstevel@tonic-gate 			if (doqueuerun())
3527c478bd9Sstevel@tonic-gate 			{
3537c478bd9Sstevel@tonic-gate 				(void) runqueue(true, false, false, false);
3547c478bd9Sstevel@tonic-gate #if _FFR_QUEUE_RUN_PARANOIA
3557c478bd9Sstevel@tonic-gate 				lastrun = now;
3567c478bd9Sstevel@tonic-gate #endif /* _FFR_QUEUE_RUN_PARANOIA */
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate #if _FFR_QUEUE_RUN_PARANOIA
359445f2479Sjbeck 			else if (CheckQueueRunners > 0 && QueueIntvl > 0 &&
360445f2479Sjbeck 				 lastrun + QueueIntvl + CheckQueueRunners < now)
3617c478bd9Sstevel@tonic-gate 			{
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 				/*
3647c478bd9Sstevel@tonic-gate 				**  set lastrun unconditionally to avoid
3657c478bd9Sstevel@tonic-gate 				**  calling checkqueuerunner() all the time.
3667c478bd9Sstevel@tonic-gate 				**  That's also why we currently ignore the
3677c478bd9Sstevel@tonic-gate 				**  result of the function call.
3687c478bd9Sstevel@tonic-gate 				*/
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 				(void) checkqueuerunner();
3717c478bd9Sstevel@tonic-gate 				lastrun = now;
3727c478bd9Sstevel@tonic-gate 			}
3737c478bd9Sstevel@tonic-gate #endif /* _FFR_QUEUE_RUN_PARANOIA */
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 			if (t <= 0)
3767c478bd9Sstevel@tonic-gate 			{
3777c478bd9Sstevel@tonic-gate 				timedout = true;
3787c478bd9Sstevel@tonic-gate 				break;
3797c478bd9Sstevel@tonic-gate 			}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 			control = false;
3827c478bd9Sstevel@tonic-gate 			errno = 0;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 			/* look "round-robin" for an active socket */
3857c478bd9Sstevel@tonic-gate 			if ((idx = olddaemon + 1) >= NDaemons)
3867c478bd9Sstevel@tonic-gate 				idx = 0;
3877c478bd9Sstevel@tonic-gate 			for (i = 0; i < NDaemons; i++)
3887c478bd9Sstevel@tonic-gate 			{
3897c478bd9Sstevel@tonic-gate 				if (Daemons[idx].d_socket >= 0 &&
3907c478bd9Sstevel@tonic-gate 				    SM_FD_ISSET(Daemons[idx].d_socket,
3917c478bd9Sstevel@tonic-gate 						&readfds))
3927c478bd9Sstevel@tonic-gate 				{
3937c478bd9Sstevel@tonic-gate 					lotherend = Daemons[idx].d_socksize;
3947c478bd9Sstevel@tonic-gate 					memset(&RealHostAddr, '\0',
395058561cbSjbeck 					       sizeof(RealHostAddr));
3967c478bd9Sstevel@tonic-gate 					t = accept(Daemons[idx].d_socket,
3977c478bd9Sstevel@tonic-gate 						   (struct sockaddr *)&RealHostAddr,
3987c478bd9Sstevel@tonic-gate 						   &lotherend);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 					/*
4017c478bd9Sstevel@tonic-gate 					**  If remote side closes before
4027c478bd9Sstevel@tonic-gate 					**  accept() finishes, sockaddr
4037c478bd9Sstevel@tonic-gate 					**  might not be fully filled in.
4047c478bd9Sstevel@tonic-gate 					*/
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 					if (t >= 0 &&
4077c478bd9Sstevel@tonic-gate 					    (lotherend == 0 ||
4087c478bd9Sstevel@tonic-gate # ifdef BSD4_4_SOCKADDR
4097c478bd9Sstevel@tonic-gate 					     RealHostAddr.sa.sa_len == 0 ||
4107c478bd9Sstevel@tonic-gate # endif /* BSD4_4_SOCKADDR */
4117c478bd9Sstevel@tonic-gate 					     RealHostAddr.sa.sa_family != Daemons[idx].d_addr.sa.sa_family))
4127c478bd9Sstevel@tonic-gate 					{
4137c478bd9Sstevel@tonic-gate 						(void) close(t);
4147c478bd9Sstevel@tonic-gate 						t = -1;
4157c478bd9Sstevel@tonic-gate 						errno = EINVAL;
4167c478bd9Sstevel@tonic-gate 					}
4177c478bd9Sstevel@tonic-gate 					olddaemon = curdaemon = idx;
4187c478bd9Sstevel@tonic-gate 					break;
4197c478bd9Sstevel@tonic-gate 				}
4207c478bd9Sstevel@tonic-gate 				if (++idx >= NDaemons)
4217c478bd9Sstevel@tonic-gate 					idx = 0;
4227c478bd9Sstevel@tonic-gate 			}
4237c478bd9Sstevel@tonic-gate #if NETUNIX
4247c478bd9Sstevel@tonic-gate 			if (curdaemon == -1 && ControlSocket >= 0 &&
4257c478bd9Sstevel@tonic-gate 			    SM_FD_ISSET(ControlSocket, &readfds))
4267c478bd9Sstevel@tonic-gate 			{
4277c478bd9Sstevel@tonic-gate 				struct sockaddr_un sa_un;
4287c478bd9Sstevel@tonic-gate 
429058561cbSjbeck 				lotherend = sizeof(sa_un);
430058561cbSjbeck 				memset(&sa_un, '\0', sizeof(sa_un));
4317c478bd9Sstevel@tonic-gate 				t = accept(ControlSocket,
4327c478bd9Sstevel@tonic-gate 					   (struct sockaddr *)&sa_un,
4337c478bd9Sstevel@tonic-gate 					   &lotherend);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 				/*
4367c478bd9Sstevel@tonic-gate 				**  If remote side closes before
4377c478bd9Sstevel@tonic-gate 				**  accept() finishes, sockaddr
4387c478bd9Sstevel@tonic-gate 				**  might not be fully filled in.
4397c478bd9Sstevel@tonic-gate 				*/
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 				if (t >= 0 &&
4427c478bd9Sstevel@tonic-gate 				    (lotherend == 0 ||
4437c478bd9Sstevel@tonic-gate # ifdef BSD4_4_SOCKADDR
4447c478bd9Sstevel@tonic-gate 				     sa_un.sun_len == 0 ||
4457c478bd9Sstevel@tonic-gate # endif /* BSD4_4_SOCKADDR */
4467c478bd9Sstevel@tonic-gate 				     sa_un.sun_family != AF_UNIX))
4477c478bd9Sstevel@tonic-gate 				{
4487c478bd9Sstevel@tonic-gate 					(void) close(t);
4497c478bd9Sstevel@tonic-gate 					t = -1;
4507c478bd9Sstevel@tonic-gate 					errno = EINVAL;
4517c478bd9Sstevel@tonic-gate 				}
4527c478bd9Sstevel@tonic-gate 				if (t >= 0)
4537c478bd9Sstevel@tonic-gate 					control = true;
4547c478bd9Sstevel@tonic-gate 			}
4557c478bd9Sstevel@tonic-gate #else /* NETUNIX */
4567c478bd9Sstevel@tonic-gate 			if (curdaemon == -1)
4577c478bd9Sstevel@tonic-gate 			{
4587c478bd9Sstevel@tonic-gate 				/* No daemon to service */
4597c478bd9Sstevel@tonic-gate 				continue;
4607c478bd9Sstevel@tonic-gate 			}
4617c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
4627c478bd9Sstevel@tonic-gate 			if (t >= 0 || errno != EINTR)
4637c478bd9Sstevel@tonic-gate 				break;
4647c478bd9Sstevel@tonic-gate 		}
4657c478bd9Sstevel@tonic-gate 		if (timedout)
4667c478bd9Sstevel@tonic-gate 		{
4677c478bd9Sstevel@tonic-gate 			timedout = false;
4687c478bd9Sstevel@tonic-gate 			continue;
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 		save_errno = errno;
4717c478bd9Sstevel@tonic-gate 		(void) sm_blocksignal(SIGALRM);
4727c478bd9Sstevel@tonic-gate 		if (t < 0)
4737c478bd9Sstevel@tonic-gate 		{
4747c478bd9Sstevel@tonic-gate 			errno = save_errno;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 			/* let's ignore these temporary errors */
4777c478bd9Sstevel@tonic-gate 			if (save_errno == EINTR
4787c478bd9Sstevel@tonic-gate #ifdef EAGAIN
4797c478bd9Sstevel@tonic-gate 			    || save_errno == EAGAIN
4807c478bd9Sstevel@tonic-gate #endif /* EAGAIN */
4817c478bd9Sstevel@tonic-gate #ifdef ECONNABORTED
4827c478bd9Sstevel@tonic-gate 			    || save_errno == ECONNABORTED
4837c478bd9Sstevel@tonic-gate #endif /* ECONNABORTED */
4847c478bd9Sstevel@tonic-gate #ifdef EWOULDBLOCK
4857c478bd9Sstevel@tonic-gate 			    || save_errno == EWOULDBLOCK
4867c478bd9Sstevel@tonic-gate #endif /* EWOULDBLOCK */
4877c478bd9Sstevel@tonic-gate 			   )
4887c478bd9Sstevel@tonic-gate 				continue;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 			syserr("getrequests: accept");
4917c478bd9Sstevel@tonic-gate 
4923ee0e492Sjbeck 			if (curdaemon >= 0)
4933ee0e492Sjbeck 			{
4943ee0e492Sjbeck 				/* arrange to re-open socket next time around */
4953ee0e492Sjbeck 				(void) close(Daemons[curdaemon].d_socket);
4963ee0e492Sjbeck 				Daemons[curdaemon].d_socket = -1;
4977c478bd9Sstevel@tonic-gate #if SO_REUSEADDR_IS_BROKEN
4983ee0e492Sjbeck 				/*
4993ee0e492Sjbeck 				**  Give time for bound socket to be released.
5003ee0e492Sjbeck 				**  This creates a denial-of-service if you can
5013ee0e492Sjbeck 				**  force accept() to fail on affected systems.
5023ee0e492Sjbeck 				*/
5037c478bd9Sstevel@tonic-gate 
5043ee0e492Sjbeck 				Daemons[curdaemon].d_refuse_connections_until =
5053ee0e492Sjbeck 					curtime() + 15;
5067c478bd9Sstevel@tonic-gate #endif /* SO_REUSEADDR_IS_BROKEN */
5073ee0e492Sjbeck 			}
5087c478bd9Sstevel@tonic-gate 			continue;
5097c478bd9Sstevel@tonic-gate 		}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		if (!control)
5127c478bd9Sstevel@tonic-gate 		{
5137c478bd9Sstevel@tonic-gate 			/* set some daemon related macros */
5147c478bd9Sstevel@tonic-gate 			switch (Daemons[curdaemon].d_addr.sa.sa_family)
5157c478bd9Sstevel@tonic-gate 			{
5167c478bd9Sstevel@tonic-gate 			  case AF_UNSPEC:
5177c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
5187c478bd9Sstevel@tonic-gate 					macid("{daemon_family}"), "unspec");
5197c478bd9Sstevel@tonic-gate 				break;
5207c478bd9Sstevel@tonic-gate #if _FFR_DAEMON_NETUNIX
5217c478bd9Sstevel@tonic-gate # if NETUNIX
5227c478bd9Sstevel@tonic-gate 			  case AF_UNIX:
5237c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
5247c478bd9Sstevel@tonic-gate 					macid("{daemon_family}"), "local");
5257c478bd9Sstevel@tonic-gate 				break;
5267c478bd9Sstevel@tonic-gate # endif /* NETUNIX */
5277c478bd9Sstevel@tonic-gate #endif /* _FFR_DAEMON_NETUNIX */
5287c478bd9Sstevel@tonic-gate #if NETINET
5297c478bd9Sstevel@tonic-gate 			  case AF_INET:
5307c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
5317c478bd9Sstevel@tonic-gate 					macid("{daemon_family}"), "inet");
5327c478bd9Sstevel@tonic-gate 				break;
5337c478bd9Sstevel@tonic-gate #endif /* NETINET */
5347c478bd9Sstevel@tonic-gate #if NETINET6
5357c478bd9Sstevel@tonic-gate 			  case AF_INET6:
5367c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
5377c478bd9Sstevel@tonic-gate 					macid("{daemon_family}"), "inet6");
5387c478bd9Sstevel@tonic-gate 				break;
5397c478bd9Sstevel@tonic-gate #endif /* NETINET6 */
5407c478bd9Sstevel@tonic-gate #if NETISO
5417c478bd9Sstevel@tonic-gate 			  case AF_ISO:
5427c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
5437c478bd9Sstevel@tonic-gate 					macid("{daemon_family}"), "iso");
5447c478bd9Sstevel@tonic-gate 				break;
5457c478bd9Sstevel@tonic-gate #endif /* NETISO */
5467c478bd9Sstevel@tonic-gate #if NETNS
5477c478bd9Sstevel@tonic-gate 			  case AF_NS:
5487c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
5497c478bd9Sstevel@tonic-gate 					macid("{daemon_family}"), "ns");
5507c478bd9Sstevel@tonic-gate 				break;
5517c478bd9Sstevel@tonic-gate #endif /* NETNS */
5527c478bd9Sstevel@tonic-gate #if NETX25
5537c478bd9Sstevel@tonic-gate 			  case AF_CCITT:
5547c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
5557c478bd9Sstevel@tonic-gate 					macid("{daemon_family}"), "x.25");
5567c478bd9Sstevel@tonic-gate 				break;
5577c478bd9Sstevel@tonic-gate #endif /* NETX25 */
5587c478bd9Sstevel@tonic-gate 			}
5597c478bd9Sstevel@tonic-gate 			macdefine(&BlankEnvelope.e_macro, A_PERM,
5607c478bd9Sstevel@tonic-gate 				macid("{daemon_name}"),
5617c478bd9Sstevel@tonic-gate 				Daemons[curdaemon].d_name);
5627c478bd9Sstevel@tonic-gate 			if (Daemons[curdaemon].d_mflags != NULL)
5637c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
5647c478bd9Sstevel@tonic-gate 					macid("{daemon_flags}"),
5657c478bd9Sstevel@tonic-gate 					Daemons[curdaemon].d_mflags);
5667c478bd9Sstevel@tonic-gate 			else
5677c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
5687c478bd9Sstevel@tonic-gate 					macid("{daemon_flags}"), "");
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		/*
5727c478bd9Sstevel@tonic-gate 		**  If connection rate is exceeded here, connection shall be
5737c478bd9Sstevel@tonic-gate 		**  refused later by a new call after fork() by the
5747c478bd9Sstevel@tonic-gate 		**  validate_connection() function. Closing the connection
5757c478bd9Sstevel@tonic-gate 		**  at this point violates RFC 2821.
5767c478bd9Sstevel@tonic-gate 		**  Do NOT remove this call, its side effects are needed.
5777c478bd9Sstevel@tonic-gate 		*/
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		connection_rate_check(&RealHostAddr, NULL);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		/*
5827c478bd9Sstevel@tonic-gate 		**  Create a subprocess to process the mail.
5837c478bd9Sstevel@tonic-gate 		*/
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 		if (tTd(15, 2))
5867c478bd9Sstevel@tonic-gate 			sm_dprintf("getrequests: forking (fd = %d)\n", t);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 		/*
5897c478bd9Sstevel@tonic-gate 		**  Advance state of PRNG.
5907c478bd9Sstevel@tonic-gate 		**  This is necessary because otherwise all child processes
5917c478bd9Sstevel@tonic-gate 		**  will produce the same PRN sequence and hence the selection
5927c478bd9Sstevel@tonic-gate 		**  of a queue directory (and other things, e.g., MX selection)
5937c478bd9Sstevel@tonic-gate 		**  are not "really" random.
5947c478bd9Sstevel@tonic-gate 		*/
5957c478bd9Sstevel@tonic-gate #if STARTTLS
5967c478bd9Sstevel@tonic-gate 		/* XXX get some better "random" data? */
5977c478bd9Sstevel@tonic-gate 		seed = get_random();
5987c478bd9Sstevel@tonic-gate 		RAND_seed((void *) &NextDiskSpaceCheck,
599058561cbSjbeck 			  sizeof(NextDiskSpaceCheck));
600058561cbSjbeck 		RAND_seed((void *) &now, sizeof(now));
601058561cbSjbeck 		RAND_seed((void *) &seed, sizeof(seed));
6027c478bd9Sstevel@tonic-gate #else /* STARTTLS */
6037c478bd9Sstevel@tonic-gate 		(void) get_random();
6047c478bd9Sstevel@tonic-gate #endif /* STARTTLS */
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate #if NAMED_BIND
6077c478bd9Sstevel@tonic-gate 		/*
6087c478bd9Sstevel@tonic-gate 		**  Update MX records for FallbackMX.
6097c478bd9Sstevel@tonic-gate 		**  Let's hope this is fast otherwise we screw up the
6107c478bd9Sstevel@tonic-gate 		**  response time.
6117c478bd9Sstevel@tonic-gate 		*/
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		if (FallbackMX != NULL)
6147c478bd9Sstevel@tonic-gate 			(void) getfallbackmxrr(FallbackMX);
6157c478bd9Sstevel@tonic-gate #endif /* NAMED_BIND */
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 		if (tTd(93, 100))
6187c478bd9Sstevel@tonic-gate 		{
6197c478bd9Sstevel@tonic-gate 			/* don't fork, handle connection in this process */
6207c478bd9Sstevel@tonic-gate 			pid = 0;
6217c478bd9Sstevel@tonic-gate 			pipefd[0] = pipefd[1] = -1;
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 		else
6247c478bd9Sstevel@tonic-gate 		{
6257c478bd9Sstevel@tonic-gate 			/*
6267c478bd9Sstevel@tonic-gate 			**  Create a pipe to keep the child from writing to
6277c478bd9Sstevel@tonic-gate 			**  the socket until after the parent has closed
6287c478bd9Sstevel@tonic-gate 			**  it.  Otherwise the parent may hang if the child
6297c478bd9Sstevel@tonic-gate 			**  has closed it first.
6307c478bd9Sstevel@tonic-gate 			*/
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 			if (pipe(pipefd) < 0)
6337c478bd9Sstevel@tonic-gate 				pipefd[0] = pipefd[1] = -1;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 			(void) sm_blocksignal(SIGCHLD);
6367c478bd9Sstevel@tonic-gate 			pid = fork();
6377c478bd9Sstevel@tonic-gate 			if (pid < 0)
6387c478bd9Sstevel@tonic-gate 			{
6397c478bd9Sstevel@tonic-gate 				syserr("daemon: cannot fork");
6407c478bd9Sstevel@tonic-gate 				if (pipefd[0] != -1)
6417c478bd9Sstevel@tonic-gate 				{
6427c478bd9Sstevel@tonic-gate 					(void) close(pipefd[0]);
6437c478bd9Sstevel@tonic-gate 					(void) close(pipefd[1]);
6447c478bd9Sstevel@tonic-gate 				}
6457c478bd9Sstevel@tonic-gate 				(void) sm_releasesignal(SIGCHLD);
6467c478bd9Sstevel@tonic-gate 				(void) sleep(10);
6477c478bd9Sstevel@tonic-gate 				(void) close(t);
6487c478bd9Sstevel@tonic-gate 				continue;
6497c478bd9Sstevel@tonic-gate 			}
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		if (pid == 0)
6537c478bd9Sstevel@tonic-gate 		{
6547c478bd9Sstevel@tonic-gate 			char *p;
6557c478bd9Sstevel@tonic-gate 			SM_FILE_T *inchannel, *outchannel = NULL;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 			/*
6587c478bd9Sstevel@tonic-gate 			**  CHILD -- return to caller.
6597c478bd9Sstevel@tonic-gate 			**	Collect verified idea of sending host.
6607c478bd9Sstevel@tonic-gate 			**	Verify calling user id if possible here.
6617c478bd9Sstevel@tonic-gate 			*/
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 			/* Reset global flags */
6647c478bd9Sstevel@tonic-gate 			RestartRequest = NULL;
6657c478bd9Sstevel@tonic-gate 			RestartWorkGroup = false;
6667c478bd9Sstevel@tonic-gate 			ShutdownRequest = NULL;
6677c478bd9Sstevel@tonic-gate 			PendingSignal = 0;
6687c478bd9Sstevel@tonic-gate 			CurrentPid = getpid();
6697c478bd9Sstevel@tonic-gate 			close_sendmail_pid();
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 			(void) sm_releasesignal(SIGALRM);
6727c478bd9Sstevel@tonic-gate 			(void) sm_releasesignal(SIGCHLD);
6737c478bd9Sstevel@tonic-gate 			(void) sm_signal(SIGCHLD, SIG_DFL);
6747c478bd9Sstevel@tonic-gate 			(void) sm_signal(SIGHUP, SIG_DFL);
6757c478bd9Sstevel@tonic-gate 			(void) sm_signal(SIGTERM, intsig);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 			/* turn on profiling */
6787c478bd9Sstevel@tonic-gate 			/* SM_PROF(0); */
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 			/*
6817c478bd9Sstevel@tonic-gate 			**  Initialize exception stack and default exception
6827c478bd9Sstevel@tonic-gate 			**  handler for child process.
6837c478bd9Sstevel@tonic-gate 			*/
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 			sm_exc_newthread(fatal_error);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 			if (!control)
6887c478bd9Sstevel@tonic-gate 			{
6897c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_TEMP,
6907c478bd9Sstevel@tonic-gate 					macid("{daemon_addr}"),
6917c478bd9Sstevel@tonic-gate 					anynet_ntoa(&Daemons[curdaemon].d_addr));
692058561cbSjbeck 				(void) sm_snprintf(status, sizeof(status), "%d",
6937c478bd9Sstevel@tonic-gate 						ntohs(Daemons[curdaemon].d_port));
6947c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_TEMP,
6957c478bd9Sstevel@tonic-gate 					macid("{daemon_port}"), status);
6967c478bd9Sstevel@tonic-gate 			}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 			for (idx = 0; idx < NDaemons; idx++)
6997c478bd9Sstevel@tonic-gate 			{
7007c478bd9Sstevel@tonic-gate 				if (Daemons[idx].d_socket >= 0)
7017c478bd9Sstevel@tonic-gate 					(void) close(Daemons[idx].d_socket);
7027c478bd9Sstevel@tonic-gate 				Daemons[idx].d_socket = -1;
7037c478bd9Sstevel@tonic-gate 			}
7047c478bd9Sstevel@tonic-gate 			clrcontrol();
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 			/* Avoid SMTP daemon actions if control command */
7077c478bd9Sstevel@tonic-gate 			if (control)
7087c478bd9Sstevel@tonic-gate 			{
7097c478bd9Sstevel@tonic-gate 				/* Add control socket process */
7107c478bd9Sstevel@tonic-gate 				proc_list_add(CurrentPid,
7117c478bd9Sstevel@tonic-gate 					      "console socket child",
7127c478bd9Sstevel@tonic-gate 					      PROC_CONTROL_CHILD, 0, -1, NULL);
7137c478bd9Sstevel@tonic-gate 			}
7147c478bd9Sstevel@tonic-gate 			else
7157c478bd9Sstevel@tonic-gate 			{
7167c478bd9Sstevel@tonic-gate 				proc_list_clear();
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 				/* clean up background delivery children */
7197c478bd9Sstevel@tonic-gate 				(void) sm_signal(SIGCHLD, reapchild);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 				/* Add parent process as first child item */
7227c478bd9Sstevel@tonic-gate 				proc_list_add(CurrentPid, "daemon child",
7237c478bd9Sstevel@tonic-gate 					      PROC_DAEMON_CHILD, 0, -1, NULL);
7247c478bd9Sstevel@tonic-gate 				/* don't schedule queue runs if ETRN */
7257c478bd9Sstevel@tonic-gate 				QueueIntvl = 0;
726058561cbSjbeck 
727058561cbSjbeck 				/*
728058561cbSjbeck 				**  Hack: override global variables if
729058561cbSjbeck 				**	the corresponding DaemonPortOption
730058561cbSjbeck 				**	is set.
731058561cbSjbeck 				*/
7327c478bd9Sstevel@tonic-gate #if _FFR_SS_PER_DAEMON
7337c478bd9Sstevel@tonic-gate 				if (Daemons[curdaemon].d_supersafe !=
734058561cbSjbeck 				    DPO_NOTSET)
735058561cbSjbeck 					SuperSafe = Daemons[curdaemon].
736058561cbSjbeck 								d_supersafe;
7377c478bd9Sstevel@tonic-gate #endif /* _FFR_SS_PER_DAEMON */
7387c478bd9Sstevel@tonic-gate 				if (Daemons[curdaemon].d_dm != DM_NOTSET)
7397c478bd9Sstevel@tonic-gate 					set_delivery_mode(
7407c478bd9Sstevel@tonic-gate 						Daemons[curdaemon].d_dm, e);
741058561cbSjbeck 
742058561cbSjbeck 				if (Daemons[curdaemon].d_refuseLA !=
743058561cbSjbeck 				    DPO_NOTSET)
744058561cbSjbeck 					RefuseLA = Daemons[curdaemon].
745058561cbSjbeck 								d_refuseLA;
746058561cbSjbeck 				if (Daemons[curdaemon].d_queueLA != DPO_NOTSET)
747058561cbSjbeck 					QueueLA = Daemons[curdaemon].d_queueLA;
748058561cbSjbeck 				if (Daemons[curdaemon].d_delayLA != DPO_NOTSET)
749058561cbSjbeck 					DelayLA = Daemons[curdaemon].d_delayLA;
750058561cbSjbeck 				if (Daemons[curdaemon].d_maxchildren !=
751058561cbSjbeck 				    DPO_NOTSET)
752058561cbSjbeck 					MaxChildren = Daemons[curdaemon].
753058561cbSjbeck 								d_maxchildren;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 				sm_setproctitle(true, e, "startup with %s",
7567c478bd9Sstevel@tonic-gate 						anynet_ntoa(&RealHostAddr));
7577c478bd9Sstevel@tonic-gate 			}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 			if (pipefd[0] != -1)
7607c478bd9Sstevel@tonic-gate 			{
7617c478bd9Sstevel@tonic-gate 				auto char c;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 				/*
7647c478bd9Sstevel@tonic-gate 				**  Wait for the parent to close the write end
7657c478bd9Sstevel@tonic-gate 				**  of the pipe, which we will see as an EOF.
7667c478bd9Sstevel@tonic-gate 				**  This guarantees that we won't write to the
7677c478bd9Sstevel@tonic-gate 				**  socket until after the parent has closed
7687c478bd9Sstevel@tonic-gate 				**  the pipe.
7697c478bd9Sstevel@tonic-gate 				*/
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 				/* close the write end of the pipe */
7727c478bd9Sstevel@tonic-gate 				(void) close(pipefd[1]);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 				/* we shouldn't be interrupted, but ... */
7757c478bd9Sstevel@tonic-gate 				while (read(pipefd[0], &c, 1) < 0 &&
7767c478bd9Sstevel@tonic-gate 				       errno == EINTR)
7777c478bd9Sstevel@tonic-gate 					continue;
7787c478bd9Sstevel@tonic-gate 				(void) close(pipefd[0]);
7797c478bd9Sstevel@tonic-gate 			}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 			/* control socket processing */
7827c478bd9Sstevel@tonic-gate 			if (control)
7837c478bd9Sstevel@tonic-gate 			{
7847c478bd9Sstevel@tonic-gate 				control_command(t, e);
7857c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
7867c478bd9Sstevel@tonic-gate 				exit(EX_SOFTWARE);
7877c478bd9Sstevel@tonic-gate 			}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 			/* determine host name */
7907c478bd9Sstevel@tonic-gate 			p = hostnamebyanyaddr(&RealHostAddr);
7917c478bd9Sstevel@tonic-gate 			if (strlen(p) > MAXNAME) /* XXX  - 1 ? */
7927c478bd9Sstevel@tonic-gate 				p[MAXNAME] = '\0';
7937c478bd9Sstevel@tonic-gate 			RealHostName = newstr(p);
7947c478bd9Sstevel@tonic-gate 			if (RealHostName[0] == '[')
7957c478bd9Sstevel@tonic-gate 			{
7967c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
7977c478bd9Sstevel@tonic-gate 					macid("{client_resolve}"),
7987c478bd9Sstevel@tonic-gate 					h_errno == TRY_AGAIN ? "TEMP" : "FAIL");
7997c478bd9Sstevel@tonic-gate 			}
8007c478bd9Sstevel@tonic-gate 			else
8017c478bd9Sstevel@tonic-gate 			{
8027c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
8037c478bd9Sstevel@tonic-gate 					  macid("{client_resolve}"), "OK");
8047c478bd9Sstevel@tonic-gate 			}
8057c478bd9Sstevel@tonic-gate 			sm_setproctitle(true, e, "startup with %s", p);
8067c478bd9Sstevel@tonic-gate 			markstats(e, NULL, STATS_CONNECT);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 			if ((inchannel = sm_io_open(SmFtStdiofd,
8097c478bd9Sstevel@tonic-gate 						    SM_TIME_DEFAULT,
8107c478bd9Sstevel@tonic-gate 						    (void *) &t,
8117c478bd9Sstevel@tonic-gate 						    SM_IO_RDONLY_B,
8127c478bd9Sstevel@tonic-gate 						    NULL)) == NULL ||
8137c478bd9Sstevel@tonic-gate 			    (t = dup(t)) < 0 ||
8147c478bd9Sstevel@tonic-gate 			    (outchannel = sm_io_open(SmFtStdiofd,
8157c478bd9Sstevel@tonic-gate 						     SM_TIME_DEFAULT,
8167c478bd9Sstevel@tonic-gate 						     (void *) &t,
8177c478bd9Sstevel@tonic-gate 						     SM_IO_WRONLY_B,
8187c478bd9Sstevel@tonic-gate 						     NULL)) == NULL)
8197c478bd9Sstevel@tonic-gate 			{
8207c478bd9Sstevel@tonic-gate 				syserr("cannot open SMTP server channel, fd=%d",
8217c478bd9Sstevel@tonic-gate 					t);
8227c478bd9Sstevel@tonic-gate 				finis(false, true, EX_OK);
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 			sm_io_automode(inchannel, outchannel);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 			InChannel = inchannel;
8277c478bd9Sstevel@tonic-gate 			OutChannel = outchannel;
8287c478bd9Sstevel@tonic-gate 			DisConnected = false;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate #if XLA
8317c478bd9Sstevel@tonic-gate 			if (!xla_host_ok(RealHostName))
8327c478bd9Sstevel@tonic-gate 			{
8337c478bd9Sstevel@tonic-gate 				message("421 4.4.5 Too many SMTP sessions for this host");
8347c478bd9Sstevel@tonic-gate 				finis(false, true, EX_OK);
8357c478bd9Sstevel@tonic-gate 			}
8367c478bd9Sstevel@tonic-gate #endif /* XLA */
8377c478bd9Sstevel@tonic-gate 			/* find out name for interface of connection */
8387c478bd9Sstevel@tonic-gate 			if (getsockname(sm_io_getinfo(InChannel, SM_IO_WHAT_FD,
8397c478bd9Sstevel@tonic-gate 						      NULL), &sa.sa, &len) == 0)
8407c478bd9Sstevel@tonic-gate 			{
8417c478bd9Sstevel@tonic-gate 				p = hostnamebyanyaddr(&sa);
8427c478bd9Sstevel@tonic-gate 				if (tTd(15, 9))
8437c478bd9Sstevel@tonic-gate 					sm_dprintf("getreq: got name %s\n", p);
8447c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_TEMP,
8457c478bd9Sstevel@tonic-gate 					macid("{if_name}"), p);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 				/*
8487c478bd9Sstevel@tonic-gate 				**  Do this only if it is not the loopback
8497c478bd9Sstevel@tonic-gate 				**  interface.
8507c478bd9Sstevel@tonic-gate 				*/
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 				if (!isloopback(sa))
8537c478bd9Sstevel@tonic-gate 				{
8547c478bd9Sstevel@tonic-gate 					char *addr;
8557c478bd9Sstevel@tonic-gate 					char family[5];
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 					addr = anynet_ntoa(&sa);
8587c478bd9Sstevel@tonic-gate 					(void) sm_snprintf(family,
8597c478bd9Sstevel@tonic-gate 						sizeof(family),
8607c478bd9Sstevel@tonic-gate 						"%d", sa.sa.sa_family);
8617c478bd9Sstevel@tonic-gate 					macdefine(&BlankEnvelope.e_macro,
8627c478bd9Sstevel@tonic-gate 						A_TEMP,
8637c478bd9Sstevel@tonic-gate 						macid("{if_addr}"), addr);
8647c478bd9Sstevel@tonic-gate 					macdefine(&BlankEnvelope.e_macro,
8657c478bd9Sstevel@tonic-gate 						A_TEMP,
8667c478bd9Sstevel@tonic-gate 						macid("{if_family}"), family);
8677c478bd9Sstevel@tonic-gate 					if (tTd(15, 7))
8687c478bd9Sstevel@tonic-gate 						sm_dprintf("getreq: got addr %s and family %s\n",
8697c478bd9Sstevel@tonic-gate 							addr, family);
8707c478bd9Sstevel@tonic-gate 				}
8717c478bd9Sstevel@tonic-gate 				else
8727c478bd9Sstevel@tonic-gate 				{
8737c478bd9Sstevel@tonic-gate 					macdefine(&BlankEnvelope.e_macro,
8747c478bd9Sstevel@tonic-gate 						A_PERM,
8757c478bd9Sstevel@tonic-gate 						macid("{if_addr}"), NULL);
8767c478bd9Sstevel@tonic-gate 					macdefine(&BlankEnvelope.e_macro,
8777c478bd9Sstevel@tonic-gate 						A_PERM,
8787c478bd9Sstevel@tonic-gate 						macid("{if_family}"), NULL);
8797c478bd9Sstevel@tonic-gate 				}
8807c478bd9Sstevel@tonic-gate 			}
8817c478bd9Sstevel@tonic-gate 			else
8827c478bd9Sstevel@tonic-gate 			{
8837c478bd9Sstevel@tonic-gate 				if (tTd(15, 7))
8847c478bd9Sstevel@tonic-gate 					sm_dprintf("getreq: getsockname failed\n");
8857c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
8867c478bd9Sstevel@tonic-gate 					macid("{if_name}"), NULL);
8877c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
8887c478bd9Sstevel@tonic-gate 					macid("{if_addr}"), NULL);
8897c478bd9Sstevel@tonic-gate 				macdefine(&BlankEnvelope.e_macro, A_PERM,
8907c478bd9Sstevel@tonic-gate 					macid("{if_family}"), NULL);
8917c478bd9Sstevel@tonic-gate 			}
8927c478bd9Sstevel@tonic-gate 			break;
8937c478bd9Sstevel@tonic-gate 		}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 		/* parent -- keep track of children */
8967c478bd9Sstevel@tonic-gate 		if (control)
8977c478bd9Sstevel@tonic-gate 		{
898058561cbSjbeck 			(void) sm_snprintf(status, sizeof(status),
8997c478bd9Sstevel@tonic-gate 					   "control socket server child");
9007c478bd9Sstevel@tonic-gate 			proc_list_add(pid, status, PROC_CONTROL, 0, -1, NULL);
9017c478bd9Sstevel@tonic-gate 		}
9027c478bd9Sstevel@tonic-gate 		else
9037c478bd9Sstevel@tonic-gate 		{
904058561cbSjbeck 			(void) sm_snprintf(status, sizeof(status),
9057c478bd9Sstevel@tonic-gate 					   "SMTP server child for %s",
9067c478bd9Sstevel@tonic-gate 					   anynet_ntoa(&RealHostAddr));
9077c478bd9Sstevel@tonic-gate 			proc_list_add(pid, status, PROC_DAEMON, 0, -1,
9087c478bd9Sstevel@tonic-gate 					&RealHostAddr);
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 		(void) sm_releasesignal(SIGCHLD);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		/* close the read end of the synchronization pipe */
9137c478bd9Sstevel@tonic-gate 		if (pipefd[0] != -1)
9147c478bd9Sstevel@tonic-gate 		{
9157c478bd9Sstevel@tonic-gate 			(void) close(pipefd[0]);
9167c478bd9Sstevel@tonic-gate 			pipefd[0] = -1;
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 		/* close the port so that others will hang (for a while) */
9207c478bd9Sstevel@tonic-gate 		(void) close(t);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 		/* release the child by closing the read end of the sync pipe */
9237c478bd9Sstevel@tonic-gate 		if (pipefd[1] != -1)
9247c478bd9Sstevel@tonic-gate 		{
9257c478bd9Sstevel@tonic-gate 			(void) close(pipefd[1]);
9267c478bd9Sstevel@tonic-gate 			pipefd[1] = -1;
9277c478bd9Sstevel@tonic-gate 		}
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 	if (tTd(15, 2))
9307c478bd9Sstevel@tonic-gate 		sm_dprintf("getreq: returning\n");
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate #if MILTER
9337c478bd9Sstevel@tonic-gate 	/* set the filters for this daemon */
9347c478bd9Sstevel@tonic-gate 	if (Daemons[curdaemon].d_inputfilterlist != NULL)
9357c478bd9Sstevel@tonic-gate 	{
9367c478bd9Sstevel@tonic-gate 		for (i = 0;
9377c478bd9Sstevel@tonic-gate 		     (i < MAXFILTERS &&
9387c478bd9Sstevel@tonic-gate 		      Daemons[curdaemon].d_inputfilters[i] != NULL);
9397c478bd9Sstevel@tonic-gate 		     i++)
9407c478bd9Sstevel@tonic-gate 		{
9417c478bd9Sstevel@tonic-gate 			InputFilters[i] = Daemons[curdaemon].d_inputfilters[i];
9427c478bd9Sstevel@tonic-gate 		}
9437c478bd9Sstevel@tonic-gate 		if (i < MAXFILTERS)
9447c478bd9Sstevel@tonic-gate 			InputFilters[i] = NULL;
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate #endif /* MILTER */
9477c478bd9Sstevel@tonic-gate 	return &Daemons[curdaemon].d_flags;
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate /*
9517c478bd9Sstevel@tonic-gate **  GETREQUESTS_CHECKDISKSPACE -- check available diskspace.
9527c478bd9Sstevel@tonic-gate **
9537c478bd9Sstevel@tonic-gate **	Parameters:
9547c478bd9Sstevel@tonic-gate **		e -- envelope.
9557c478bd9Sstevel@tonic-gate **
9567c478bd9Sstevel@tonic-gate **	Returns:
9577c478bd9Sstevel@tonic-gate **		none.
9587c478bd9Sstevel@tonic-gate **
9597c478bd9Sstevel@tonic-gate **	Side Effects:
9607c478bd9Sstevel@tonic-gate **		Modifies Daemon flags (D_ETRNONLY) if not enough disk space.
9617c478bd9Sstevel@tonic-gate */
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate static void
getrequests_checkdiskspace(e)9647c478bd9Sstevel@tonic-gate getrequests_checkdiskspace(e)
9657c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate 	bool logged = false;
9687c478bd9Sstevel@tonic-gate 	int idx;
9697c478bd9Sstevel@tonic-gate 	time_t now;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	now = curtime();
9727c478bd9Sstevel@tonic-gate 	if (now < NextDiskSpaceCheck)
9737c478bd9Sstevel@tonic-gate 		return;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	/* Check if there is available disk space in all queue groups. */
9767c478bd9Sstevel@tonic-gate 	if (!enoughdiskspace(0, NULL))
9777c478bd9Sstevel@tonic-gate 	{
9787c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < NDaemons; ++idx)
9797c478bd9Sstevel@tonic-gate 		{
9807c478bd9Sstevel@tonic-gate 			if (bitnset(D_ETRNONLY, Daemons[idx].d_flags))
9817c478bd9Sstevel@tonic-gate 				continue;
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 			/* log only if not logged before */
9847c478bd9Sstevel@tonic-gate 			if (!logged)
9857c478bd9Sstevel@tonic-gate 			{
9867c478bd9Sstevel@tonic-gate 				if (LogLevel > 8)
9877c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, NOQID,
9887c478bd9Sstevel@tonic-gate 						  "rejecting new messages: min free: %ld",
9897c478bd9Sstevel@tonic-gate 						  MinBlocksFree);
9907c478bd9Sstevel@tonic-gate 				sm_setproctitle(true, e,
9917c478bd9Sstevel@tonic-gate 						"rejecting new messages: min free: %ld",
9927c478bd9Sstevel@tonic-gate 						MinBlocksFree);
9937c478bd9Sstevel@tonic-gate 				logged = true;
9947c478bd9Sstevel@tonic-gate 			}
9957c478bd9Sstevel@tonic-gate 			setbitn(D_ETRNONLY, Daemons[idx].d_flags);
9967c478bd9Sstevel@tonic-gate 		}
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate 	else
9997c478bd9Sstevel@tonic-gate 	{
10007c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < NDaemons; ++idx)
10017c478bd9Sstevel@tonic-gate 		{
10027c478bd9Sstevel@tonic-gate 			if (!bitnset(D_ETRNONLY, Daemons[idx].d_flags))
10037c478bd9Sstevel@tonic-gate 				continue;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 			/* log only if not logged before */
10067c478bd9Sstevel@tonic-gate 			if (!logged)
10077c478bd9Sstevel@tonic-gate 			{
10087c478bd9Sstevel@tonic-gate 				if (LogLevel > 8)
10097c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_INFO, NOQID,
10107c478bd9Sstevel@tonic-gate 						  "accepting new messages (again)");
10117c478bd9Sstevel@tonic-gate 				logged = true;
10127c478bd9Sstevel@tonic-gate 			}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 			/* title will be set later */
10157c478bd9Sstevel@tonic-gate 			clrbitn(D_ETRNONLY, Daemons[idx].d_flags);
10167c478bd9Sstevel@tonic-gate 		}
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	/* only check disk space once a minute */
10207c478bd9Sstevel@tonic-gate 	NextDiskSpaceCheck = now + 60;
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate /*
10247c478bd9Sstevel@tonic-gate **  OPENDAEMONSOCKET -- open SMTP socket
10257c478bd9Sstevel@tonic-gate **
10267c478bd9Sstevel@tonic-gate **	Deals with setting all appropriate options.
10277c478bd9Sstevel@tonic-gate **
10287c478bd9Sstevel@tonic-gate **	Parameters:
10297c478bd9Sstevel@tonic-gate **		d -- the structure for the daemon to open.
10307c478bd9Sstevel@tonic-gate **		firsttime -- set if this is the initial open.
10317c478bd9Sstevel@tonic-gate **
10327c478bd9Sstevel@tonic-gate **	Returns:
10337c478bd9Sstevel@tonic-gate **		Size in bytes of the daemon socket addr.
10347c478bd9Sstevel@tonic-gate **
10357c478bd9Sstevel@tonic-gate **	Side Effects:
10367c478bd9Sstevel@tonic-gate **		Leaves DaemonSocket set to the open socket.
10377c478bd9Sstevel@tonic-gate **		Exits if the socket cannot be created.
10387c478bd9Sstevel@tonic-gate */
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate #define MAXOPENTRIES	10	/* maximum number of tries to open connection */
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate static int
opendaemonsocket(d,firsttime)10437c478bd9Sstevel@tonic-gate opendaemonsocket(d, firsttime)
10447c478bd9Sstevel@tonic-gate 	DAEMON_T *d;
10457c478bd9Sstevel@tonic-gate 	bool firsttime;
10467c478bd9Sstevel@tonic-gate {
10477c478bd9Sstevel@tonic-gate 	int on = 1;
10487c478bd9Sstevel@tonic-gate 	int fdflags;
10497c478bd9Sstevel@tonic-gate 	SOCKADDR_LEN_T socksize = 0;
10507c478bd9Sstevel@tonic-gate 	int ntries = 0;
10517c478bd9Sstevel@tonic-gate 	int save_errno;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	if (tTd(15, 2))
10547c478bd9Sstevel@tonic-gate 		sm_dprintf("opendaemonsocket(%s)\n", d->d_name);
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	do
10577c478bd9Sstevel@tonic-gate 	{
10587c478bd9Sstevel@tonic-gate 		if (ntries > 0)
10597c478bd9Sstevel@tonic-gate 			(void) sleep(5);
10607c478bd9Sstevel@tonic-gate 		if (firsttime || d->d_socket < 0)
10617c478bd9Sstevel@tonic-gate 		{
10627c478bd9Sstevel@tonic-gate #if _FFR_DAEMON_NETUNIX
10637c478bd9Sstevel@tonic-gate # if NETUNIX
10647c478bd9Sstevel@tonic-gate 			if (d->d_addr.sa.sa_family == AF_UNIX)
10657c478bd9Sstevel@tonic-gate 			{
10667c478bd9Sstevel@tonic-gate 				int rval;
10677c478bd9Sstevel@tonic-gate 				long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_ROOTOK|SFF_EXECOK|SFF_CREAT;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 				/* if not safe, don't use it */
10707c478bd9Sstevel@tonic-gate 				rval = safefile(d->d_addr.sunix.sun_path,
10717c478bd9Sstevel@tonic-gate 						RunAsUid, RunAsGid,
10727c478bd9Sstevel@tonic-gate 						RunAsUserName, sff,
10737c478bd9Sstevel@tonic-gate 						S_IRUSR|S_IWUSR, NULL);
10747c478bd9Sstevel@tonic-gate 				if (rval != 0)
10757c478bd9Sstevel@tonic-gate 				{
10767c478bd9Sstevel@tonic-gate 					save_errno = errno;
10777c478bd9Sstevel@tonic-gate 					syserr("opendaemonsocket: daemon %s: unsafe domain socket %s",
10787c478bd9Sstevel@tonic-gate 					       d->d_name,
10797c478bd9Sstevel@tonic-gate 					       d->d_addr.sunix.sun_path);
10807c478bd9Sstevel@tonic-gate 					goto fail;
10817c478bd9Sstevel@tonic-gate 				}
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 				/* Don't try to overtake an existing socket */
10847c478bd9Sstevel@tonic-gate 				(void) unlink(d->d_addr.sunix.sun_path);
10857c478bd9Sstevel@tonic-gate 			}
10867c478bd9Sstevel@tonic-gate # endif /* NETUNIX */
10877c478bd9Sstevel@tonic-gate #endif /* _FFR_DOMAIN_NETUNIX */
10887c478bd9Sstevel@tonic-gate 			d->d_socket = socket(d->d_addr.sa.sa_family,
10897c478bd9Sstevel@tonic-gate 					     SOCK_STREAM, 0);
10907c478bd9Sstevel@tonic-gate 			if (d->d_socket < 0)
10917c478bd9Sstevel@tonic-gate 			{
10927c478bd9Sstevel@tonic-gate 				save_errno = errno;
10937c478bd9Sstevel@tonic-gate 				syserr("opendaemonsocket: daemon %s: can't create server SMTP socket",
10947c478bd9Sstevel@tonic-gate 				       d->d_name);
10957c478bd9Sstevel@tonic-gate 			  fail:
10967c478bd9Sstevel@tonic-gate 				if (bitnset(D_OPTIONAL, d->d_flags) &&
10977c478bd9Sstevel@tonic-gate 				    (!transienterror(save_errno) ||
10987c478bd9Sstevel@tonic-gate 				     ntries >= MAXOPENTRIES - 1))
10997c478bd9Sstevel@tonic-gate 				{
11007c478bd9Sstevel@tonic-gate 					syserr("opendaemonsocket: daemon %s: optional socket disabled",
11017c478bd9Sstevel@tonic-gate 					       d->d_name);
11027c478bd9Sstevel@tonic-gate 					setbitn(D_DISABLE, d->d_flags);
11037c478bd9Sstevel@tonic-gate 					d->d_socket = -1;
11047c478bd9Sstevel@tonic-gate 					return -1;
11057c478bd9Sstevel@tonic-gate 				}
11067c478bd9Sstevel@tonic-gate 			  severe:
11077c478bd9Sstevel@tonic-gate 				if (LogLevel > 0)
11087c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_ALERT, NOQID,
11097c478bd9Sstevel@tonic-gate 						  "daemon %s: problem creating SMTP socket",
11107c478bd9Sstevel@tonic-gate 						  d->d_name);
11117c478bd9Sstevel@tonic-gate 				d->d_socket = -1;
11127c478bd9Sstevel@tonic-gate 				continue;
11137c478bd9Sstevel@tonic-gate 			}
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 			if (SM_FD_SETSIZE > 0 && d->d_socket >= SM_FD_SETSIZE)
11167c478bd9Sstevel@tonic-gate 			{
11177c478bd9Sstevel@tonic-gate 				save_errno = EINVAL;
11187c478bd9Sstevel@tonic-gate 				syserr("opendaemonsocket: daemon %s: server SMTP socket (%d) too large",
11197c478bd9Sstevel@tonic-gate 				       d->d_name, d->d_socket);
11207c478bd9Sstevel@tonic-gate 				goto fail;
11217c478bd9Sstevel@tonic-gate 			}
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 			/* turn on network debugging? */
11247c478bd9Sstevel@tonic-gate 			if (tTd(15, 101))
11257c478bd9Sstevel@tonic-gate 				(void) setsockopt(d->d_socket, SOL_SOCKET,
11267c478bd9Sstevel@tonic-gate 						  SO_DEBUG, (char *)&on,
1127058561cbSjbeck 						  sizeof(on));
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 			(void) setsockopt(d->d_socket, SOL_SOCKET,
1130058561cbSjbeck 					  SO_REUSEADDR, (char *)&on, sizeof(on));
11317c478bd9Sstevel@tonic-gate 			(void) setsockopt(d->d_socket, SOL_SOCKET,
1132058561cbSjbeck 					  SO_KEEPALIVE, (char *)&on, sizeof(on));
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate #ifdef SO_RCVBUF
11357c478bd9Sstevel@tonic-gate 			if (d->d_tcprcvbufsize > 0)
11367c478bd9Sstevel@tonic-gate 			{
11377c478bd9Sstevel@tonic-gate 				if (setsockopt(d->d_socket, SOL_SOCKET,
11387c478bd9Sstevel@tonic-gate 					       SO_RCVBUF,
11397c478bd9Sstevel@tonic-gate 					       (char *) &d->d_tcprcvbufsize,
11407c478bd9Sstevel@tonic-gate 					       sizeof(d->d_tcprcvbufsize)) < 0)
11417c478bd9Sstevel@tonic-gate 					syserr("opendaemonsocket: daemon %s: setsockopt(SO_RCVBUF)", d->d_name);
11427c478bd9Sstevel@tonic-gate 			}
11437c478bd9Sstevel@tonic-gate #endif /* SO_RCVBUF */
11447c478bd9Sstevel@tonic-gate #ifdef SO_SNDBUF
11457c478bd9Sstevel@tonic-gate 			if (d->d_tcpsndbufsize > 0)
11467c478bd9Sstevel@tonic-gate 			{
11477c478bd9Sstevel@tonic-gate 				if (setsockopt(d->d_socket, SOL_SOCKET,
11487c478bd9Sstevel@tonic-gate 					       SO_SNDBUF,
11497c478bd9Sstevel@tonic-gate 					       (char *) &d->d_tcpsndbufsize,
11507c478bd9Sstevel@tonic-gate 					       sizeof(d->d_tcpsndbufsize)) < 0)
11517c478bd9Sstevel@tonic-gate 					syserr("opendaemonsocket: daemon %s: setsockopt(SO_SNDBUF)", d->d_name);
11527c478bd9Sstevel@tonic-gate 			}
11537c478bd9Sstevel@tonic-gate #endif /* SO_SNDBUF */
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 			if ((fdflags = fcntl(d->d_socket, F_GETFD, 0)) == -1 ||
11567c478bd9Sstevel@tonic-gate 			    fcntl(d->d_socket, F_SETFD,
11577c478bd9Sstevel@tonic-gate 				  fdflags | FD_CLOEXEC) == -1)
11587c478bd9Sstevel@tonic-gate 			{
11597c478bd9Sstevel@tonic-gate 				save_errno = errno;
11607c478bd9Sstevel@tonic-gate 				syserr("opendaemonsocket: daemon %s: failed to %s close-on-exec flag: %s",
11617c478bd9Sstevel@tonic-gate 				       d->d_name,
11627c478bd9Sstevel@tonic-gate 				       fdflags == -1 ? "get" : "set",
11637c478bd9Sstevel@tonic-gate 				       sm_errstring(save_errno));
11647c478bd9Sstevel@tonic-gate 				(void) close(d->d_socket);
11657c478bd9Sstevel@tonic-gate 				goto severe;
11667c478bd9Sstevel@tonic-gate 			}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 			switch (d->d_addr.sa.sa_family)
11697c478bd9Sstevel@tonic-gate 			{
11707c478bd9Sstevel@tonic-gate #if _FFR_DAEMON_NETUNIX
11717c478bd9Sstevel@tonic-gate # ifdef NETUNIX
11727c478bd9Sstevel@tonic-gate 			  case AF_UNIX:
1173058561cbSjbeck 				socksize = sizeof(d->d_addr.sunix);
11747c478bd9Sstevel@tonic-gate 				break;
11757c478bd9Sstevel@tonic-gate # endif /* NETUNIX */
11767c478bd9Sstevel@tonic-gate #endif /* _FFR_DAEMON_NETUNIX */
11777c478bd9Sstevel@tonic-gate #if NETINET
11787c478bd9Sstevel@tonic-gate 			  case AF_INET:
1179058561cbSjbeck 				socksize = sizeof(d->d_addr.sin);
11807c478bd9Sstevel@tonic-gate 				break;
11817c478bd9Sstevel@tonic-gate #endif /* NETINET */
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate #if NETINET6
11847c478bd9Sstevel@tonic-gate 			  case AF_INET6:
1185058561cbSjbeck 				socksize = sizeof(d->d_addr.sin6);
11867c478bd9Sstevel@tonic-gate 				break;
11877c478bd9Sstevel@tonic-gate #endif /* NETINET6 */
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate #if NETISO
11907c478bd9Sstevel@tonic-gate 			  case AF_ISO:
1191058561cbSjbeck 				socksize = sizeof(d->d_addr.siso);
11927c478bd9Sstevel@tonic-gate 				break;
11937c478bd9Sstevel@tonic-gate #endif /* NETISO */
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 			  default:
1196058561cbSjbeck 				socksize = sizeof(d->d_addr);
11977c478bd9Sstevel@tonic-gate 				break;
11987c478bd9Sstevel@tonic-gate 			}
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 			if (bind(d->d_socket, &d->d_addr.sa, socksize) < 0)
12017c478bd9Sstevel@tonic-gate 			{
12027c478bd9Sstevel@tonic-gate 				/* probably another daemon already */
12037c478bd9Sstevel@tonic-gate 				save_errno = errno;
12047c478bd9Sstevel@tonic-gate 				syserr("opendaemonsocket: daemon %s: cannot bind",
12057c478bd9Sstevel@tonic-gate 				       d->d_name);
12067c478bd9Sstevel@tonic-gate 				(void) close(d->d_socket);
12077c478bd9Sstevel@tonic-gate 				goto fail;
12087c478bd9Sstevel@tonic-gate 			}
12097c478bd9Sstevel@tonic-gate 		}
12107c478bd9Sstevel@tonic-gate 		if (!firsttime &&
12117c478bd9Sstevel@tonic-gate 		    listen(d->d_socket, d->d_listenqueue) < 0)
12127c478bd9Sstevel@tonic-gate 		{
12137c478bd9Sstevel@tonic-gate 			save_errno = errno;
12147c478bd9Sstevel@tonic-gate 			syserr("opendaemonsocket: daemon %s: cannot listen",
12157c478bd9Sstevel@tonic-gate 			       d->d_name);
12167c478bd9Sstevel@tonic-gate 			(void) close(d->d_socket);
12177c478bd9Sstevel@tonic-gate 			goto severe;
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 		return socksize;
12207c478bd9Sstevel@tonic-gate 	} while (ntries++ < MAXOPENTRIES && transienterror(save_errno));
12217c478bd9Sstevel@tonic-gate 	syserr("!opendaemonsocket: daemon %s: server SMTP socket wedged: exiting",
12227c478bd9Sstevel@tonic-gate 	       d->d_name);
12237c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
12247c478bd9Sstevel@tonic-gate 	return -1;  /* avoid compiler warning on IRIX */
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate /*
12277c478bd9Sstevel@tonic-gate **  SETUPDAEMON -- setup socket for daemon
12287c478bd9Sstevel@tonic-gate **
1229<