xref: /illumos-gate/usr/src/cmd/sendmail/libmilter/listener.c (revision 445f2479fe3d7435daab18bf2cdc310b86cd6738)
17c478bd9Sstevel@tonic-gate /*
2*445f2479Sjbeck  *  Copyright (c) 1999-2006 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
67c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
77c478bd9Sstevel@tonic-gate  * the sendmail distribution.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  */
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #include <sm/gen.h>
14*445f2479Sjbeck SM_RCSID("@(#)$Id: listener.c,v 8.115 2006/01/24 00:48:39 ca Exp $")
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate /*
177c478bd9Sstevel@tonic-gate **  listener.c -- threaded network listener
187c478bd9Sstevel@tonic-gate */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include "libmilter.h"
217c478bd9Sstevel@tonic-gate #include <sm/errstring.h>
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #include <sys/types.h>
247c478bd9Sstevel@tonic-gate #include <sys/stat.h>
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate # if NETINET || NETINET6
287c478bd9Sstevel@tonic-gate #  include <arpa/inet.h>
297c478bd9Sstevel@tonic-gate # endif /* NETINET || NETINET6 */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate static smutex_t L_Mutex;
327c478bd9Sstevel@tonic-gate static int L_family;
337c478bd9Sstevel@tonic-gate static SOCKADDR_LEN_T L_socksize;
347c478bd9Sstevel@tonic-gate static socket_t listenfd = INVALID_SOCKET;
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static socket_t mi_milteropen __P((char *, int, bool, char *));
377c478bd9Sstevel@tonic-gate static void *mi_thread_handle_wrapper __P((void *));
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate **  MI_OPENSOCKET -- create the socket where this filter and the MTA will meet
417c478bd9Sstevel@tonic-gate **
427c478bd9Sstevel@tonic-gate **	Parameters:
437c478bd9Sstevel@tonic-gate **		conn -- connection description
447c478bd9Sstevel@tonic-gate **		backlog -- listen backlog
457c478bd9Sstevel@tonic-gate **		dbg -- debug level
467c478bd9Sstevel@tonic-gate **		rmsocket -- if true, try to unlink() the socket first
477c478bd9Sstevel@tonic-gate **			(UNIX domain sockets only)
487c478bd9Sstevel@tonic-gate **		smfi -- filter structure to use
497c478bd9Sstevel@tonic-gate **
507c478bd9Sstevel@tonic-gate **	Return value:
517c478bd9Sstevel@tonic-gate **		MI_SUCCESS/MI_FAILURE
527c478bd9Sstevel@tonic-gate */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate int
557c478bd9Sstevel@tonic-gate mi_opensocket(conn, backlog, dbg, rmsocket, smfi)
567c478bd9Sstevel@tonic-gate 	char *conn;
577c478bd9Sstevel@tonic-gate 	int backlog;
587c478bd9Sstevel@tonic-gate 	int dbg;
597c478bd9Sstevel@tonic-gate 	bool rmsocket;
607c478bd9Sstevel@tonic-gate 	smfiDesc_ptr smfi;
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	if (smfi == NULL || conn == NULL)
637c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (ValidSocket(listenfd))
667c478bd9Sstevel@tonic-gate 		return MI_SUCCESS;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (dbg > 0)
697c478bd9Sstevel@tonic-gate 	{
707c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_DEBUG,
717c478bd9Sstevel@tonic-gate 			"%s: Opening listen socket on conn %s",
727c478bd9Sstevel@tonic-gate 			smfi->xxfi_name, conn);
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	(void) smutex_init(&L_Mutex);
757c478bd9Sstevel@tonic-gate 	(void) smutex_lock(&L_Mutex);
767c478bd9Sstevel@tonic-gate 	listenfd = mi_milteropen(conn, backlog, rmsocket, smfi->xxfi_name);
777c478bd9Sstevel@tonic-gate 	if (!ValidSocket(listenfd))
787c478bd9Sstevel@tonic-gate 	{
797c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_FATAL,
807c478bd9Sstevel@tonic-gate 			"%s: Unable to create listening socket on conn %s",
817c478bd9Sstevel@tonic-gate 			smfi->xxfi_name, conn);
827c478bd9Sstevel@tonic-gate 		(void) smutex_unlock(&L_Mutex);
837c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate #if !SM_CONF_POLL
867c478bd9Sstevel@tonic-gate 	if (!SM_FD_OK_SELECT(listenfd))
877c478bd9Sstevel@tonic-gate 	{
887c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR, "%s: fd %d is larger than FD_SETSIZE %d",
897c478bd9Sstevel@tonic-gate 			smfi->xxfi_name, listenfd, FD_SETSIZE);
907c478bd9Sstevel@tonic-gate 		(void) smutex_unlock(&L_Mutex);
917c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate #endif /* !SM_CONF_POLL */
947c478bd9Sstevel@tonic-gate 	(void) smutex_unlock(&L_Mutex);
957c478bd9Sstevel@tonic-gate 	return MI_SUCCESS;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate **  MI_MILTEROPEN -- setup socket to listen on
1007c478bd9Sstevel@tonic-gate **
1017c478bd9Sstevel@tonic-gate **	Parameters:
1027c478bd9Sstevel@tonic-gate **		conn -- connection description
1037c478bd9Sstevel@tonic-gate **		backlog -- listen backlog
1047c478bd9Sstevel@tonic-gate **		rmsocket -- if true, try to unlink() the socket first
1057c478bd9Sstevel@tonic-gate **			(UNIX domain sockets only)
1067c478bd9Sstevel@tonic-gate **		name -- name for logging
1077c478bd9Sstevel@tonic-gate **
1087c478bd9Sstevel@tonic-gate **	Returns:
1097c478bd9Sstevel@tonic-gate **		socket upon success, error code otherwise.
1107c478bd9Sstevel@tonic-gate **
1117c478bd9Sstevel@tonic-gate **	Side effect:
1127c478bd9Sstevel@tonic-gate **		sets sockpath if UNIX socket.
1137c478bd9Sstevel@tonic-gate */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #if NETUNIX
1167c478bd9Sstevel@tonic-gate static char	*sockpath = NULL;
1177c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static socket_t
1207c478bd9Sstevel@tonic-gate mi_milteropen(conn, backlog, rmsocket, name)
1217c478bd9Sstevel@tonic-gate 	char *conn;
1227c478bd9Sstevel@tonic-gate 	int backlog;
1237c478bd9Sstevel@tonic-gate 	bool rmsocket;
1247c478bd9Sstevel@tonic-gate 	char *name;
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	socket_t sock;
1277c478bd9Sstevel@tonic-gate 	int sockopt = 1;
1287c478bd9Sstevel@tonic-gate 	int fdflags;
1297c478bd9Sstevel@tonic-gate 	size_t len = 0;
1307c478bd9Sstevel@tonic-gate 	char *p;
1317c478bd9Sstevel@tonic-gate 	char *colon;
1327c478bd9Sstevel@tonic-gate 	char *at;
1337c478bd9Sstevel@tonic-gate 	SOCKADDR addr;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if (conn == NULL || conn[0] == '\0')
1367c478bd9Sstevel@tonic-gate 	{
1377c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR, "%s: empty or missing socket information",
1387c478bd9Sstevel@tonic-gate 			name);
1397c478bd9Sstevel@tonic-gate 		return INVALID_SOCKET;
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	(void) memset(&addr, '\0', sizeof addr);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/* protocol:filename or protocol:port@host */
1447c478bd9Sstevel@tonic-gate 	p = conn;
1457c478bd9Sstevel@tonic-gate 	colon = strchr(p, ':');
1467c478bd9Sstevel@tonic-gate 	if (colon != NULL)
1477c478bd9Sstevel@tonic-gate 	{
1487c478bd9Sstevel@tonic-gate 		*colon = '\0';
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		if (*p == '\0')
1517c478bd9Sstevel@tonic-gate 		{
1527c478bd9Sstevel@tonic-gate #if NETUNIX
1537c478bd9Sstevel@tonic-gate 			/* default to AF_UNIX */
1547c478bd9Sstevel@tonic-gate 			addr.sa.sa_family = AF_UNIX;
1557c478bd9Sstevel@tonic-gate 			L_socksize = sizeof (struct sockaddr_un);
1567c478bd9Sstevel@tonic-gate #else /* NETUNIX */
1577c478bd9Sstevel@tonic-gate # if NETINET
1587c478bd9Sstevel@tonic-gate 			/* default to AF_INET */
1597c478bd9Sstevel@tonic-gate 			addr.sa.sa_family = AF_INET;
1607c478bd9Sstevel@tonic-gate 			L_socksize = sizeof addr.sin;
1617c478bd9Sstevel@tonic-gate # else /* NETINET */
1627c478bd9Sstevel@tonic-gate #  if NETINET6
1637c478bd9Sstevel@tonic-gate 			/* default to AF_INET6 */
1647c478bd9Sstevel@tonic-gate 			addr.sa.sa_family = AF_INET6;
1657c478bd9Sstevel@tonic-gate 			L_socksize = sizeof addr.sin6;
1667c478bd9Sstevel@tonic-gate #  else /* NETINET6 */
1677c478bd9Sstevel@tonic-gate 			/* no protocols available */
1687c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
1697c478bd9Sstevel@tonic-gate 				"%s: no valid socket protocols available",
1707c478bd9Sstevel@tonic-gate 				name);
1717c478bd9Sstevel@tonic-gate 			return INVALID_SOCKET;
1727c478bd9Sstevel@tonic-gate #  endif /* NETINET6 */
1737c478bd9Sstevel@tonic-gate # endif /* NETINET */
1747c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate #if NETUNIX
1777c478bd9Sstevel@tonic-gate 		else if (strcasecmp(p, "unix") == 0 ||
1787c478bd9Sstevel@tonic-gate 			 strcasecmp(p, "local") == 0)
1797c478bd9Sstevel@tonic-gate 		{
1807c478bd9Sstevel@tonic-gate 			addr.sa.sa_family = AF_UNIX;
1817c478bd9Sstevel@tonic-gate 			L_socksize = sizeof (struct sockaddr_un);
1827c478bd9Sstevel@tonic-gate 		}
1837c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
1847c478bd9Sstevel@tonic-gate #if NETINET
1857c478bd9Sstevel@tonic-gate 		else if (strcasecmp(p, "inet") == 0)
1867c478bd9Sstevel@tonic-gate 		{
1877c478bd9Sstevel@tonic-gate 			addr.sa.sa_family = AF_INET;
1887c478bd9Sstevel@tonic-gate 			L_socksize = sizeof addr.sin;
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate #endif /* NETINET */
1917c478bd9Sstevel@tonic-gate #if NETINET6
1927c478bd9Sstevel@tonic-gate 		else if (strcasecmp(p, "inet6") == 0)
1937c478bd9Sstevel@tonic-gate 		{
1947c478bd9Sstevel@tonic-gate 			addr.sa.sa_family = AF_INET6;
1957c478bd9Sstevel@tonic-gate 			L_socksize = sizeof addr.sin6;
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate #endif /* NETINET6 */
1987c478bd9Sstevel@tonic-gate 		else
1997c478bd9Sstevel@tonic-gate 		{
2007c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR, "%s: unknown socket type %s",
2017c478bd9Sstevel@tonic-gate 				name, p);
2027c478bd9Sstevel@tonic-gate 			return INVALID_SOCKET;
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 		*colon++ = ':';
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 	else
2077c478bd9Sstevel@tonic-gate 	{
2087c478bd9Sstevel@tonic-gate 		colon = p;
2097c478bd9Sstevel@tonic-gate #if NETUNIX
2107c478bd9Sstevel@tonic-gate 		/* default to AF_UNIX */
2117c478bd9Sstevel@tonic-gate 		addr.sa.sa_family = AF_UNIX;
2127c478bd9Sstevel@tonic-gate 		L_socksize = sizeof (struct sockaddr_un);
2137c478bd9Sstevel@tonic-gate #else /* NETUNIX */
2147c478bd9Sstevel@tonic-gate # if NETINET
2157c478bd9Sstevel@tonic-gate 		/* default to AF_INET */
2167c478bd9Sstevel@tonic-gate 		addr.sa.sa_family = AF_INET;
2177c478bd9Sstevel@tonic-gate 		L_socksize = sizeof addr.sin;
2187c478bd9Sstevel@tonic-gate # else /* NETINET */
2197c478bd9Sstevel@tonic-gate #  if NETINET6
2207c478bd9Sstevel@tonic-gate 		/* default to AF_INET6 */
2217c478bd9Sstevel@tonic-gate 		addr.sa.sa_family = AF_INET6;
2227c478bd9Sstevel@tonic-gate 		L_socksize = sizeof addr.sin6;
2237c478bd9Sstevel@tonic-gate #  else /* NETINET6 */
2247c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR, "%s: unknown socket type %s",
2257c478bd9Sstevel@tonic-gate 			name, p);
2267c478bd9Sstevel@tonic-gate 		return INVALID_SOCKET;
2277c478bd9Sstevel@tonic-gate #  endif /* NETINET6 */
2287c478bd9Sstevel@tonic-gate # endif /* NETINET */
2297c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate #if NETUNIX
2337c478bd9Sstevel@tonic-gate 	if (addr.sa.sa_family == AF_UNIX)
2347c478bd9Sstevel@tonic-gate 	{
2357c478bd9Sstevel@tonic-gate # if 0
2367c478bd9Sstevel@tonic-gate 		long sff = SFF_SAFEDIRPATH|SFF_OPENASROOT|SFF_NOLINK|SFF_CREAT|SFF_MUSTOWN;
2377c478bd9Sstevel@tonic-gate # endif /* 0 */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		at = colon;
2407c478bd9Sstevel@tonic-gate 		len = strlen(colon) + 1;
2417c478bd9Sstevel@tonic-gate 		if (len >= sizeof addr.sunix.sun_path)
2427c478bd9Sstevel@tonic-gate 		{
2437c478bd9Sstevel@tonic-gate 			errno = EINVAL;
2447c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR, "%s: UNIX socket name %s too long",
2457c478bd9Sstevel@tonic-gate 				name, colon);
2467c478bd9Sstevel@tonic-gate 			return INVALID_SOCKET;
2477c478bd9Sstevel@tonic-gate 		}
2487c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(addr.sunix.sun_path, colon,
2497c478bd9Sstevel@tonic-gate 				sizeof addr.sunix.sun_path);
2507c478bd9Sstevel@tonic-gate # if 0
2517c478bd9Sstevel@tonic-gate 		errno = safefile(colon, RunAsUid, RunAsGid, RunAsUserName, sff,
2527c478bd9Sstevel@tonic-gate 				 S_IRUSR|S_IWUSR, NULL);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		/* if not safe, don't create */
2557c478bd9Sstevel@tonic-gate 		if (errno != 0)
2567c478bd9Sstevel@tonic-gate 		{
2577c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
2587c478bd9Sstevel@tonic-gate 				"%s: UNIX socket name %s unsafe",
2597c478bd9Sstevel@tonic-gate 				name, colon);
2607c478bd9Sstevel@tonic-gate 			return INVALID_SOCKET;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate # endif /* 0 */
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate #if NETINET || NETINET6
2677c478bd9Sstevel@tonic-gate 	if (
2687c478bd9Sstevel@tonic-gate # if NETINET
2697c478bd9Sstevel@tonic-gate 	    addr.sa.sa_family == AF_INET
2707c478bd9Sstevel@tonic-gate # endif /* NETINET */
2717c478bd9Sstevel@tonic-gate # if NETINET && NETINET6
2727c478bd9Sstevel@tonic-gate 	    ||
2737c478bd9Sstevel@tonic-gate # endif /* NETINET && NETINET6 */
2747c478bd9Sstevel@tonic-gate # if NETINET6
2757c478bd9Sstevel@tonic-gate 	    addr.sa.sa_family == AF_INET6
2767c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
2777c478bd9Sstevel@tonic-gate 	   )
2787c478bd9Sstevel@tonic-gate 	{
2797c478bd9Sstevel@tonic-gate 		unsigned short port;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		/* Parse port@host */
2827c478bd9Sstevel@tonic-gate 		at = strchr(colon, '@');
2837c478bd9Sstevel@tonic-gate 		if (at == NULL)
2847c478bd9Sstevel@tonic-gate 		{
2857c478bd9Sstevel@tonic-gate 			switch (addr.sa.sa_family)
2867c478bd9Sstevel@tonic-gate 			{
2877c478bd9Sstevel@tonic-gate # if NETINET
2887c478bd9Sstevel@tonic-gate 			  case AF_INET:
2897c478bd9Sstevel@tonic-gate 				addr.sin.sin_addr.s_addr = INADDR_ANY;
2907c478bd9Sstevel@tonic-gate 				break;
2917c478bd9Sstevel@tonic-gate # endif /* NETINET */
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate # if NETINET6
2947c478bd9Sstevel@tonic-gate 			  case AF_INET6:
2957c478bd9Sstevel@tonic-gate 				addr.sin6.sin6_addr = in6addr_any;
2967c478bd9Sstevel@tonic-gate 				break;
2977c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 		else
3017c478bd9Sstevel@tonic-gate 			*at = '\0';
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		if (isascii(*colon) && isdigit(*colon))
3047c478bd9Sstevel@tonic-gate 			port = htons((unsigned short) atoi(colon));
3057c478bd9Sstevel@tonic-gate 		else
3067c478bd9Sstevel@tonic-gate 		{
3077c478bd9Sstevel@tonic-gate # ifdef NO_GETSERVBYNAME
3087c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR, "%s: invalid port number %s",
3097c478bd9Sstevel@tonic-gate 				name, colon);
3107c478bd9Sstevel@tonic-gate 			return INVALID_SOCKET;
3117c478bd9Sstevel@tonic-gate # else /* NO_GETSERVBYNAME */
3127c478bd9Sstevel@tonic-gate 			register struct servent *sp;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 			sp = getservbyname(colon, "tcp");
3157c478bd9Sstevel@tonic-gate 			if (sp == NULL)
3167c478bd9Sstevel@tonic-gate 			{
3177c478bd9Sstevel@tonic-gate 				smi_log(SMI_LOG_ERR,
3187c478bd9Sstevel@tonic-gate 					"%s: unknown port name %s",
3197c478bd9Sstevel@tonic-gate 					name, colon);
3207c478bd9Sstevel@tonic-gate 				return INVALID_SOCKET;
3217c478bd9Sstevel@tonic-gate 			}
3227c478bd9Sstevel@tonic-gate 			port = sp->s_port;
3237c478bd9Sstevel@tonic-gate # endif /* NO_GETSERVBYNAME */
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 		if (at != NULL)
3267c478bd9Sstevel@tonic-gate 		{
3277c478bd9Sstevel@tonic-gate 			*at++ = '@';
3287c478bd9Sstevel@tonic-gate 			if (*at == '[')
3297c478bd9Sstevel@tonic-gate 			{
3307c478bd9Sstevel@tonic-gate 				char *end;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 				end = strchr(at, ']');
3337c478bd9Sstevel@tonic-gate 				if (end != NULL)
3347c478bd9Sstevel@tonic-gate 				{
3357c478bd9Sstevel@tonic-gate 					bool found = false;
3367c478bd9Sstevel@tonic-gate # if NETINET
3377c478bd9Sstevel@tonic-gate 					unsigned long hid = INADDR_NONE;
3387c478bd9Sstevel@tonic-gate # endif /* NETINET */
3397c478bd9Sstevel@tonic-gate # if NETINET6
3407c478bd9Sstevel@tonic-gate 					struct sockaddr_in6 hid6;
3417c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 					*end = '\0';
3447c478bd9Sstevel@tonic-gate # if NETINET
3457c478bd9Sstevel@tonic-gate 					if (addr.sa.sa_family == AF_INET &&
3467c478bd9Sstevel@tonic-gate 					    (hid = inet_addr(&at[1])) != INADDR_NONE)
3477c478bd9Sstevel@tonic-gate 					{
3487c478bd9Sstevel@tonic-gate 						addr.sin.sin_addr.s_addr = hid;
3497c478bd9Sstevel@tonic-gate 						addr.sin.sin_port = port;
3507c478bd9Sstevel@tonic-gate 						found = true;
3517c478bd9Sstevel@tonic-gate 					}
3527c478bd9Sstevel@tonic-gate # endif /* NETINET */
3537c478bd9Sstevel@tonic-gate # if NETINET6
3547c478bd9Sstevel@tonic-gate 					(void) memset(&hid6, '\0', sizeof hid6);
3557c478bd9Sstevel@tonic-gate 					if (addr.sa.sa_family == AF_INET6 &&
3567c478bd9Sstevel@tonic-gate 					    mi_inet_pton(AF_INET6, &at[1],
3577c478bd9Sstevel@tonic-gate 							 &hid6.sin6_addr) == 1)
3587c478bd9Sstevel@tonic-gate 					{
3597c478bd9Sstevel@tonic-gate 						addr.sin6.sin6_addr = hid6.sin6_addr;
3607c478bd9Sstevel@tonic-gate 						addr.sin6.sin6_port = port;
3617c478bd9Sstevel@tonic-gate 						found = true;
3627c478bd9Sstevel@tonic-gate 					}
3637c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
3647c478bd9Sstevel@tonic-gate 					*end = ']';
3657c478bd9Sstevel@tonic-gate 					if (!found)
3667c478bd9Sstevel@tonic-gate 					{
3677c478bd9Sstevel@tonic-gate 						smi_log(SMI_LOG_ERR,
3687c478bd9Sstevel@tonic-gate 							"%s: Invalid numeric domain spec \"%s\"",
3697c478bd9Sstevel@tonic-gate 							name, at);
3707c478bd9Sstevel@tonic-gate 						return INVALID_SOCKET;
3717c478bd9Sstevel@tonic-gate 					}
3727c478bd9Sstevel@tonic-gate 				}
3737c478bd9Sstevel@tonic-gate 				else
3747c478bd9Sstevel@tonic-gate 				{
3757c478bd9Sstevel@tonic-gate 					smi_log(SMI_LOG_ERR,
3767c478bd9Sstevel@tonic-gate 						"%s: Invalid numeric domain spec \"%s\"",
3777c478bd9Sstevel@tonic-gate 						name, at);
3787c478bd9Sstevel@tonic-gate 					return INVALID_SOCKET;
3797c478bd9Sstevel@tonic-gate 				}
3807c478bd9Sstevel@tonic-gate 			}
3817c478bd9Sstevel@tonic-gate 			else
3827c478bd9Sstevel@tonic-gate 			{
3837c478bd9Sstevel@tonic-gate 				struct hostent *hp = NULL;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 				hp = mi_gethostbyname(at, addr.sa.sa_family);
3867c478bd9Sstevel@tonic-gate 				if (hp == NULL)
3877c478bd9Sstevel@tonic-gate 				{
3887c478bd9Sstevel@tonic-gate 					smi_log(SMI_LOG_ERR,
3897c478bd9Sstevel@tonic-gate 						"%s: Unknown host name %s",
3907c478bd9Sstevel@tonic-gate 						name, at);
3917c478bd9Sstevel@tonic-gate 					return INVALID_SOCKET;
3927c478bd9Sstevel@tonic-gate 				}
3937c478bd9Sstevel@tonic-gate 				addr.sa.sa_family = hp->h_addrtype;
3947c478bd9Sstevel@tonic-gate 				switch (hp->h_addrtype)
3957c478bd9Sstevel@tonic-gate 				{
3967c478bd9Sstevel@tonic-gate # if NETINET
3977c478bd9Sstevel@tonic-gate 				  case AF_INET:
3987c478bd9Sstevel@tonic-gate 					(void) memmove(&addr.sin.sin_addr,
3997c478bd9Sstevel@tonic-gate 						       hp->h_addr,
4007c478bd9Sstevel@tonic-gate 						       INADDRSZ);
4017c478bd9Sstevel@tonic-gate 					addr.sin.sin_port = port;
4027c478bd9Sstevel@tonic-gate 					break;
4037c478bd9Sstevel@tonic-gate # endif /* NETINET */
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate # if NETINET6
4067c478bd9Sstevel@tonic-gate 				  case AF_INET6:
4077c478bd9Sstevel@tonic-gate 					(void) memmove(&addr.sin6.sin6_addr,
4087c478bd9Sstevel@tonic-gate 						       hp->h_addr,
4097c478bd9Sstevel@tonic-gate 						       IN6ADDRSZ);
4107c478bd9Sstevel@tonic-gate 					addr.sin6.sin6_port = port;
4117c478bd9Sstevel@tonic-gate 					break;
4127c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 				  default:
4157c478bd9Sstevel@tonic-gate 					smi_log(SMI_LOG_ERR,
4167c478bd9Sstevel@tonic-gate 						"%s: Unknown protocol for %s (%d)",
4177c478bd9Sstevel@tonic-gate 						name, at, hp->h_addrtype);
4187c478bd9Sstevel@tonic-gate 					return INVALID_SOCKET;
4197c478bd9Sstevel@tonic-gate 				}
4207c478bd9Sstevel@tonic-gate # if NETINET6
4217c478bd9Sstevel@tonic-gate 				freehostent(hp);
4227c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
4237c478bd9Sstevel@tonic-gate 			}
4247c478bd9Sstevel@tonic-gate 		}
4257c478bd9Sstevel@tonic-gate 		else
4267c478bd9Sstevel@tonic-gate 		{
4277c478bd9Sstevel@tonic-gate 			switch (addr.sa.sa_family)
4287c478bd9Sstevel@tonic-gate 			{
4297c478bd9Sstevel@tonic-gate # if NETINET
4307c478bd9Sstevel@tonic-gate 			  case AF_INET:
4317c478bd9Sstevel@tonic-gate 				addr.sin.sin_port = port;
4327c478bd9Sstevel@tonic-gate 				break;
4337c478bd9Sstevel@tonic-gate # endif /* NETINET */
4347c478bd9Sstevel@tonic-gate # if NETINET6
4357c478bd9Sstevel@tonic-gate 			  case AF_INET6:
4367c478bd9Sstevel@tonic-gate 				addr.sin6.sin6_port = port;
4377c478bd9Sstevel@tonic-gate 				break;
4387c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
4397c478bd9Sstevel@tonic-gate 			}
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate #endif /* NETINET || NETINET6 */
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	sock = socket(addr.sa.sa_family, SOCK_STREAM, 0);
4457c478bd9Sstevel@tonic-gate 	if (!ValidSocket(sock))
4467c478bd9Sstevel@tonic-gate 	{
4477c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
4487c478bd9Sstevel@tonic-gate 			"%s: Unable to create new socket: %s",
4497c478bd9Sstevel@tonic-gate 			name, sm_errstring(errno));
4507c478bd9Sstevel@tonic-gate 		return INVALID_SOCKET;
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if ((fdflags = fcntl(sock, F_GETFD, 0)) == -1 ||
4547c478bd9Sstevel@tonic-gate 	    fcntl(sock, F_SETFD, fdflags | FD_CLOEXEC) == -1)
4557c478bd9Sstevel@tonic-gate 	{
4567c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
4577c478bd9Sstevel@tonic-gate 			"%s: Unable to set close-on-exec: %s", name,
4587c478bd9Sstevel@tonic-gate 			sm_errstring(errno));
4597c478bd9Sstevel@tonic-gate 		(void) closesocket(sock);
4607c478bd9Sstevel@tonic-gate 		return INVALID_SOCKET;
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
463*445f2479Sjbeck 	if (
464*445f2479Sjbeck #if NETUNIX
465*445f2479Sjbeck 	    addr.sa.sa_family != AF_UNIX &&
466*445f2479Sjbeck #endif /* NETUNIX */
467*445f2479Sjbeck 	    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &sockopt,
4687c478bd9Sstevel@tonic-gate 		       sizeof(sockopt)) == -1)
4697c478bd9Sstevel@tonic-gate 	{
4707c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
471*445f2479Sjbeck 			"%s: set reuseaddr failed (%s)", name,
4727c478bd9Sstevel@tonic-gate 			sm_errstring(errno));
4737c478bd9Sstevel@tonic-gate 		(void) closesocket(sock);
4747c478bd9Sstevel@tonic-gate 		return INVALID_SOCKET;
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate #if NETUNIX
4787c478bd9Sstevel@tonic-gate 	if (addr.sa.sa_family == AF_UNIX && rmsocket)
4797c478bd9Sstevel@tonic-gate 	{
4807c478bd9Sstevel@tonic-gate 		struct stat s;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 		if (stat(colon, &s) != 0)
4837c478bd9Sstevel@tonic-gate 		{
4847c478bd9Sstevel@tonic-gate 			if (errno != ENOENT)
4857c478bd9Sstevel@tonic-gate 			{
4867c478bd9Sstevel@tonic-gate 				smi_log(SMI_LOG_ERR,
4877c478bd9Sstevel@tonic-gate 					"%s: Unable to stat() %s: %s",
4887c478bd9Sstevel@tonic-gate 					name, colon, sm_errstring(errno));
4897c478bd9Sstevel@tonic-gate 				(void) closesocket(sock);
4907c478bd9Sstevel@tonic-gate 				return INVALID_SOCKET;
4917c478bd9Sstevel@tonic-gate 			}
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 		else if (!S_ISSOCK(s.st_mode))
4947c478bd9Sstevel@tonic-gate 		{
4957c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
4967c478bd9Sstevel@tonic-gate 				"%s: %s is not a UNIX domain socket",
4977c478bd9Sstevel@tonic-gate 				name, colon);
4987c478bd9Sstevel@tonic-gate 			(void) closesocket(sock);
4997c478bd9Sstevel@tonic-gate 			return INVALID_SOCKET;
5007c478bd9Sstevel@tonic-gate 		}
5017c478bd9Sstevel@tonic-gate 		else if (unlink(colon) != 0)
5027c478bd9Sstevel@tonic-gate 		{
5037c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
5047c478bd9Sstevel@tonic-gate 				"%s: Unable to remove %s: %s",
5057c478bd9Sstevel@tonic-gate 				name, colon, sm_errstring(errno));
5067c478bd9Sstevel@tonic-gate 			(void) closesocket(sock);
5077c478bd9Sstevel@tonic-gate 			return INVALID_SOCKET;
5087c478bd9Sstevel@tonic-gate 		}
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	if (bind(sock, &addr.sa, L_socksize) < 0)
5137c478bd9Sstevel@tonic-gate 	{
5147c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
5157c478bd9Sstevel@tonic-gate 			"%s: Unable to bind to port %s: %s",
5167c478bd9Sstevel@tonic-gate 			name, conn, sm_errstring(errno));
5177c478bd9Sstevel@tonic-gate 		(void) closesocket(sock);
5187c478bd9Sstevel@tonic-gate 		return INVALID_SOCKET;
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (listen(sock, backlog) < 0)
5227c478bd9Sstevel@tonic-gate 	{
5237c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
5247c478bd9Sstevel@tonic-gate 			"%s: listen call failed: %s", name,
5257c478bd9Sstevel@tonic-gate 			sm_errstring(errno));
5267c478bd9Sstevel@tonic-gate 		(void) closesocket(sock);
5277c478bd9Sstevel@tonic-gate 		return INVALID_SOCKET;
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate #if NETUNIX
5317c478bd9Sstevel@tonic-gate 	if (addr.sa.sa_family == AF_UNIX && len > 0)
5327c478bd9Sstevel@tonic-gate 	{
5337c478bd9Sstevel@tonic-gate 		/*
5347c478bd9Sstevel@tonic-gate 		**  Set global variable sockpath so the UNIX socket can be
5357c478bd9Sstevel@tonic-gate 		**  unlink()ed at exit.
5367c478bd9Sstevel@tonic-gate 		*/
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		sockpath = (char *) malloc(len);
5397c478bd9Sstevel@tonic-gate 		if (sockpath != NULL)
5407c478bd9Sstevel@tonic-gate 			(void) sm_strlcpy(sockpath, colon, len);
5417c478bd9Sstevel@tonic-gate 		else
5427c478bd9Sstevel@tonic-gate 		{
5437c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
5447c478bd9Sstevel@tonic-gate 				"%s: can't malloc(%d) for sockpath: %s",
5457c478bd9Sstevel@tonic-gate 				name, (int) len, sm_errstring(errno));
5467c478bd9Sstevel@tonic-gate 			(void) closesocket(sock);
5477c478bd9Sstevel@tonic-gate 			return INVALID_SOCKET;
5487c478bd9Sstevel@tonic-gate 		}
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
5517c478bd9Sstevel@tonic-gate 	L_family = addr.sa.sa_family;
5527c478bd9Sstevel@tonic-gate 	return sock;
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate **  MI_THREAD_HANDLE_WRAPPER -- small wrapper to handle session
5567c478bd9Sstevel@tonic-gate **
5577c478bd9Sstevel@tonic-gate **	Parameters:
5587c478bd9Sstevel@tonic-gate **		arg -- argument to pass to mi_handle_session()
5597c478bd9Sstevel@tonic-gate **
5607c478bd9Sstevel@tonic-gate **	Returns:
5617c478bd9Sstevel@tonic-gate **		results from mi_handle_session()
5627c478bd9Sstevel@tonic-gate */
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate static void *
5657c478bd9Sstevel@tonic-gate mi_thread_handle_wrapper(arg)
5667c478bd9Sstevel@tonic-gate 	void *arg;
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	return (void *) mi_handle_session(arg);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /*
5727c478bd9Sstevel@tonic-gate **  MI_CLOSENER -- close listen socket
5737c478bd9Sstevel@tonic-gate **
5747c478bd9Sstevel@tonic-gate **	Parameters:
5757c478bd9Sstevel@tonic-gate **		none.
5767c478bd9Sstevel@tonic-gate **
5777c478bd9Sstevel@tonic-gate **	Returns:
5787c478bd9Sstevel@tonic-gate **		none.
5797c478bd9Sstevel@tonic-gate */
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate void
5827c478bd9Sstevel@tonic-gate mi_closener()
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	(void) smutex_lock(&L_Mutex);
5857c478bd9Sstevel@tonic-gate 	if (ValidSocket(listenfd))
5867c478bd9Sstevel@tonic-gate 	{
5877c478bd9Sstevel@tonic-gate #if NETUNIX
5887c478bd9Sstevel@tonic-gate 		bool removable;
5897c478bd9Sstevel@tonic-gate 		struct stat sockinfo;
5907c478bd9Sstevel@tonic-gate 		struct stat fileinfo;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 		removable = sockpath != NULL &&
5937c478bd9Sstevel@tonic-gate 			    geteuid() != 0 &&
5947c478bd9Sstevel@tonic-gate 			    fstat(listenfd, &sockinfo) == 0 &&
5957c478bd9Sstevel@tonic-gate 			    (S_ISFIFO(sockinfo.st_mode)
5967c478bd9Sstevel@tonic-gate # ifdef S_ISSOCK
5977c478bd9Sstevel@tonic-gate 			     || S_ISSOCK(sockinfo.st_mode)
5987c478bd9Sstevel@tonic-gate # endif /* S_ISSOCK */
5997c478bd9Sstevel@tonic-gate 			    );
6007c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		(void) closesocket(listenfd);
6037c478bd9Sstevel@tonic-gate 		listenfd = INVALID_SOCKET;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate #if NETUNIX
6067c478bd9Sstevel@tonic-gate 		/* XXX sleep() some time before doing this? */
6077c478bd9Sstevel@tonic-gate 		if (sockpath != NULL)
6087c478bd9Sstevel@tonic-gate 		{
6097c478bd9Sstevel@tonic-gate 			if (removable &&
6107c478bd9Sstevel@tonic-gate 			    stat(sockpath, &fileinfo) == 0 &&
6117c478bd9Sstevel@tonic-gate 			    ((fileinfo.st_dev == sockinfo.st_dev &&
6127c478bd9Sstevel@tonic-gate 			      fileinfo.st_ino == sockinfo.st_ino)
6137c478bd9Sstevel@tonic-gate # ifdef S_ISSOCK
6147c478bd9Sstevel@tonic-gate 			     || S_ISSOCK(fileinfo.st_mode)
6157c478bd9Sstevel@tonic-gate # endif /* S_ISSOCK */
6167c478bd9Sstevel@tonic-gate 			    )
6177c478bd9Sstevel@tonic-gate 			    &&
6187c478bd9Sstevel@tonic-gate 			    (S_ISFIFO(fileinfo.st_mode)
6197c478bd9Sstevel@tonic-gate # ifdef S_ISSOCK
6207c478bd9Sstevel@tonic-gate 			     || S_ISSOCK(fileinfo.st_mode)
6217c478bd9Sstevel@tonic-gate # endif /* S_ISSOCK */
6227c478bd9Sstevel@tonic-gate 			     ))
6237c478bd9Sstevel@tonic-gate 				(void) unlink(sockpath);
6247c478bd9Sstevel@tonic-gate 			free(sockpath);
6257c478bd9Sstevel@tonic-gate 			sockpath = NULL;
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate #endif /* NETUNIX */
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 	(void) smutex_unlock(&L_Mutex);
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate /*
6337c478bd9Sstevel@tonic-gate **  MI_LISTENER -- Generic listener harness
6347c478bd9Sstevel@tonic-gate **
6357c478bd9Sstevel@tonic-gate **	Open up listen port
6367c478bd9Sstevel@tonic-gate **	Wait for connections
6377c478bd9Sstevel@tonic-gate **
6387c478bd9Sstevel@tonic-gate **	Parameters:
6397c478bd9Sstevel@tonic-gate **		conn -- connection description
6407c478bd9Sstevel@tonic-gate **		dbg -- debug level
6417c478bd9Sstevel@tonic-gate **		smfi -- filter structure to use
6427c478bd9Sstevel@tonic-gate **		timeout -- timeout for reads/writes
6437c478bd9Sstevel@tonic-gate **		backlog -- listen queue backlog size
6447c478bd9Sstevel@tonic-gate **
6457c478bd9Sstevel@tonic-gate **	Returns:
6467c478bd9Sstevel@tonic-gate **		MI_SUCCESS -- Exited normally
6477c478bd9Sstevel@tonic-gate **			   (session finished or we were told to exit)
6487c478bd9Sstevel@tonic-gate **		MI_FAILURE -- Network initialization failed.
6497c478bd9Sstevel@tonic-gate */
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate #if BROKEN_PTHREAD_SLEEP
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate /*
6547c478bd9Sstevel@tonic-gate **  Solaris 2.6, perhaps others, gets an internal threads library panic
6557c478bd9Sstevel@tonic-gate **  when sleep() is used:
6567c478bd9Sstevel@tonic-gate **
6577c478bd9Sstevel@tonic-gate **  thread_create() failed, returned 11 (EINVAL)
6587c478bd9Sstevel@tonic-gate **  co_enable, thr_create() returned error = 24
6597c478bd9Sstevel@tonic-gate **  libthread panic: co_enable failed (PID: 17793 LWP 1)
6607c478bd9Sstevel@tonic-gate **  stacktrace:
6617c478bd9Sstevel@tonic-gate **	ef526b10
6627c478bd9Sstevel@tonic-gate **	ef52646c
6637c478bd9Sstevel@tonic-gate **	ef534cbc
6647c478bd9Sstevel@tonic-gate **	156a4
6657c478bd9Sstevel@tonic-gate **	14644
6667c478bd9Sstevel@tonic-gate **	1413c
6677c478bd9Sstevel@tonic-gate **	135e0
6687c478bd9Sstevel@tonic-gate **	0
6697c478bd9Sstevel@tonic-gate */
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate # define MI_SLEEP(s)							\
6727c478bd9Sstevel@tonic-gate {									\
6737c478bd9Sstevel@tonic-gate 	int rs = 0;							\
6747c478bd9Sstevel@tonic-gate 	struct timeval st;						\
6757c478bd9Sstevel@tonic-gate 									\
6767c478bd9Sstevel@tonic-gate 	st.tv_sec = (s);						\
6777c478bd9Sstevel@tonic-gate 	st.tv_usec = 0;							\
6787c478bd9Sstevel@tonic-gate 	if (st.tv_sec > 0)						\
6797c478bd9Sstevel@tonic-gate 	{								\
6807c478bd9Sstevel@tonic-gate 		for (;;)						\
6817c478bd9Sstevel@tonic-gate 		{							\
6827c478bd9Sstevel@tonic-gate 			rs = select(0, NULL, NULL, NULL, &st);		\
6837c478bd9Sstevel@tonic-gate 			if (rs < 0 && errno == EINTR)			\
6847c478bd9Sstevel@tonic-gate 				continue;				\
6857c478bd9Sstevel@tonic-gate 			if (rs != 0)					\
6867c478bd9Sstevel@tonic-gate 			{						\
6877c478bd9Sstevel@tonic-gate 				smi_log(SMI_LOG_ERR,			\
6887c478bd9Sstevel@tonic-gate 					"MI_SLEEP(): select() returned non-zero result %d, errno = %d",	\
6897c478bd9Sstevel@tonic-gate 					rs, errno);			\
6907c478bd9Sstevel@tonic-gate 			}						\
6917c478bd9Sstevel@tonic-gate 			break;						\
6927c478bd9Sstevel@tonic-gate 		}							\
6937c478bd9Sstevel@tonic-gate 	}								\
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate #else /* BROKEN_PTHREAD_SLEEP */
6967c478bd9Sstevel@tonic-gate # define MI_SLEEP(s)	sleep((s))
6977c478bd9Sstevel@tonic-gate #endif /* BROKEN_PTHREAD_SLEEP */
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate int
7007c478bd9Sstevel@tonic-gate mi_listener(conn, dbg, smfi, timeout, backlog)
7017c478bd9Sstevel@tonic-gate 	char *conn;
7027c478bd9Sstevel@tonic-gate 	int dbg;
7037c478bd9Sstevel@tonic-gate 	smfiDesc_ptr smfi;
7047c478bd9Sstevel@tonic-gate 	time_t timeout;
7057c478bd9Sstevel@tonic-gate 	int backlog;
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	socket_t connfd = INVALID_SOCKET;
7087c478bd9Sstevel@tonic-gate #if _FFR_DUP_FD
7097c478bd9Sstevel@tonic-gate 	socket_t dupfd = INVALID_SOCKET;
7107c478bd9Sstevel@tonic-gate #endif /* _FFR_DUP_FD */
7117c478bd9Sstevel@tonic-gate 	int sockopt = 1;
7127c478bd9Sstevel@tonic-gate 	int r, mistop;
7137c478bd9Sstevel@tonic-gate 	int ret = MI_SUCCESS;
7147c478bd9Sstevel@tonic-gate 	int mcnt = 0;	/* error count for malloc() failures */
7157c478bd9Sstevel@tonic-gate 	int tcnt = 0;	/* error count for thread_create() failures */
7167c478bd9Sstevel@tonic-gate 	int acnt = 0;	/* error count for accept() failures */
7177c478bd9Sstevel@tonic-gate 	int scnt = 0;	/* error count for select() failures */
7187c478bd9Sstevel@tonic-gate 	int save_errno = 0;
7197c478bd9Sstevel@tonic-gate 	sthread_t thread_id;
7207c478bd9Sstevel@tonic-gate 	_SOCK_ADDR cliaddr;
7217c478bd9Sstevel@tonic-gate 	SOCKADDR_LEN_T clilen;
7227c478bd9Sstevel@tonic-gate 	SMFICTX_PTR ctx;
7237c478bd9Sstevel@tonic-gate 	FD_RD_VAR(rds, excs);
7247c478bd9Sstevel@tonic-gate 	struct timeval chktime;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (mi_opensocket(conn, backlog, dbg, false, smfi) == MI_FAILURE)
7277c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	clilen = L_socksize;
7307c478bd9Sstevel@tonic-gate 	while ((mistop = mi_stop()) == MILTER_CONT)
7317c478bd9Sstevel@tonic-gate 	{
7327c478bd9Sstevel@tonic-gate 		(void) smutex_lock(&L_Mutex);
7337c478bd9Sstevel@tonic-gate 		if (!ValidSocket(listenfd))
7347c478bd9Sstevel@tonic-gate 		{
7357c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
7367c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
7377c478bd9Sstevel@tonic-gate 				"%s: listenfd=%d corrupted, terminating, errno=%d",
7387c478bd9Sstevel@tonic-gate 				smfi->xxfi_name, listenfd, errno);
7397c478bd9Sstevel@tonic-gate 			(void) smutex_unlock(&L_Mutex);
7407c478bd9Sstevel@tonic-gate 			break;
7417c478bd9Sstevel@tonic-gate 		}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		/* select on interface ports */
7447c478bd9Sstevel@tonic-gate 		FD_RD_INIT(listenfd, rds, excs);
7457c478bd9Sstevel@tonic-gate 		chktime.tv_sec = MI_CHK_TIME;
7467c478bd9Sstevel@tonic-gate 		chktime.tv_usec = 0;
7477c478bd9Sstevel@tonic-gate 		r = FD_RD_READY(listenfd, rds, excs, &chktime);
7487c478bd9Sstevel@tonic-gate 		if (r == 0)		/* timeout */
7497c478bd9Sstevel@tonic-gate 		{
7507c478bd9Sstevel@tonic-gate 			(void) smutex_unlock(&L_Mutex);
7517c478bd9Sstevel@tonic-gate 			continue;	/* just check mi_stop() */
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 		if (r < 0)
7547c478bd9Sstevel@tonic-gate 		{
7557c478bd9Sstevel@tonic-gate 			save_errno = errno;
7567c478bd9Sstevel@tonic-gate 			(void) smutex_unlock(&L_Mutex);
7577c478bd9Sstevel@tonic-gate 			if (save_errno == EINTR)
7587c478bd9Sstevel@tonic-gate 				continue;
7597c478bd9Sstevel@tonic-gate 			scnt++;
7607c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
7617c478bd9Sstevel@tonic-gate 				"%s: select() failed (%s), %s",
7627c478bd9Sstevel@tonic-gate 				smfi->xxfi_name, sm_errstring(save_errno),
7637c478bd9Sstevel@tonic-gate 				scnt >= MAX_FAILS_S ? "abort" : "try again");
7647c478bd9Sstevel@tonic-gate 			MI_SLEEP(scnt);
7657c478bd9Sstevel@tonic-gate 			if (scnt >= MAX_FAILS_S)
7667c478bd9Sstevel@tonic-gate 			{
7677c478bd9Sstevel@tonic-gate 				ret = MI_FAILURE;
7687c478bd9Sstevel@tonic-gate 				break;
7697c478bd9Sstevel@tonic-gate 			}
7707c478bd9Sstevel@tonic-gate 			continue;
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 		if (!FD_IS_RD_RDY(listenfd, rds, excs))
7737c478bd9Sstevel@tonic-gate 		{
7747c478bd9Sstevel@tonic-gate 			/* some error: just stop for now... */
7757c478bd9Sstevel@tonic-gate 			ret = MI_FAILURE;
7767c478bd9Sstevel@tonic-gate 			(void) smutex_unlock(&L_Mutex);
7777c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
7787c478bd9Sstevel@tonic-gate 				"%s: %s() returned exception for socket, abort",
7797c478bd9Sstevel@tonic-gate 				smfi->xxfi_name, MI_POLLSELECT);
7807c478bd9Sstevel@tonic-gate 			break;
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 		scnt = 0;	/* reset error counter for select() */
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		(void) memset(&cliaddr, '\0', sizeof cliaddr);
7857c478bd9Sstevel@tonic-gate 		connfd = accept(listenfd, (struct sockaddr *) &cliaddr,
7867c478bd9Sstevel@tonic-gate 				&clilen);
7877c478bd9Sstevel@tonic-gate 		save_errno = errno;
7887c478bd9Sstevel@tonic-gate 		(void) smutex_unlock(&L_Mutex);
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 		/*
7917c478bd9Sstevel@tonic-gate 		**  If remote side closes before
7927c478bd9Sstevel@tonic-gate 		**  accept() finishes, sockaddr
7937c478bd9Sstevel@tonic-gate 		**  might not be fully filled in.
7947c478bd9Sstevel@tonic-gate 		*/
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 		if (ValidSocket(connfd) &&
7977c478bd9Sstevel@tonic-gate 		    (clilen == 0 ||
7987c478bd9Sstevel@tonic-gate # ifdef BSD4_4_SOCKADDR
7997c478bd9Sstevel@tonic-gate 		     cliaddr.sa.sa_len == 0 ||
8007c478bd9Sstevel@tonic-gate # endif /* BSD4_4_SOCKADDR */
8017c478bd9Sstevel@tonic-gate 		     cliaddr.sa.sa_family != L_family))
8027c478bd9Sstevel@tonic-gate 		{
8037c478bd9Sstevel@tonic-gate 			(void) closesocket(connfd);
8047c478bd9Sstevel@tonic-gate 			connfd = INVALID_SOCKET;
8057c478bd9Sstevel@tonic-gate 			save_errno = EINVAL;
8067c478bd9Sstevel@tonic-gate 		}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate #if !SM_CONF_POLL
8097c478bd9Sstevel@tonic-gate 		/* check if acceptable for select() */
8107c478bd9Sstevel@tonic-gate 		if (ValidSocket(connfd) && !SM_FD_OK_SELECT(connfd))
8117c478bd9Sstevel@tonic-gate 		{
8127c478bd9Sstevel@tonic-gate 			(void) closesocket(connfd);
8137c478bd9Sstevel@tonic-gate 			connfd = INVALID_SOCKET;
8147c478bd9Sstevel@tonic-gate 			save_errno = ERANGE;
8157c478bd9Sstevel@tonic-gate 		}
8167c478bd9Sstevel@tonic-gate #endif /* !SM_CONF_POLL */
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 		if (!ValidSocket(connfd))
8197c478bd9Sstevel@tonic-gate 		{
8207c478bd9Sstevel@tonic-gate 			if (save_errno == EINTR
8217c478bd9Sstevel@tonic-gate #ifdef EAGAIN
8227c478bd9Sstevel@tonic-gate 			    || save_errno == EAGAIN
8237c478bd9Sstevel@tonic-gate #endif /* EAGAIN */
8247c478bd9Sstevel@tonic-gate #ifdef ECONNABORTED
8257c478bd9Sstevel@tonic-gate 			    || save_errno == ECONNABORTED
8267c478bd9Sstevel@tonic-gate #endif /* ECONNABORTED */
8277c478bd9Sstevel@tonic-gate #ifdef EMFILE
8287c478bd9Sstevel@tonic-gate 			    || save_errno == EMFILE
8297c478bd9Sstevel@tonic-gate #endif /* EMFILE */
8307c478bd9Sstevel@tonic-gate #ifdef ENFILE
8317c478bd9Sstevel@tonic-gate 			    || save_errno == ENFILE
8327c478bd9Sstevel@tonic-gate #endif /* ENFILE */
8337c478bd9Sstevel@tonic-gate #ifdef ENOBUFS
8347c478bd9Sstevel@tonic-gate 			    || save_errno == ENOBUFS
8357c478bd9Sstevel@tonic-gate #endif /* ENOBUFS */
8367c478bd9Sstevel@tonic-gate #ifdef ENOMEM
8377c478bd9Sstevel@tonic-gate 			    || save_errno == ENOMEM
8387c478bd9Sstevel@tonic-gate #endif /* ENOMEM */
8397c478bd9Sstevel@tonic-gate #ifdef ENOSR
8407c478bd9Sstevel@tonic-gate 			    || save_errno == ENOSR
8417c478bd9Sstevel@tonic-gate #endif /* ENOSR */
8427c478bd9Sstevel@tonic-gate #ifdef EWOULDBLOCK
8437c478bd9Sstevel@tonic-gate 			    || save_errno == EWOULDBLOCK
8447c478bd9Sstevel@tonic-gate #endif /* EWOULDBLOCK */
8457c478bd9Sstevel@tonic-gate 			   )
8467c478bd9Sstevel@tonic-gate 				continue;
8477c478bd9Sstevel@tonic-gate 			acnt++;
8487c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
8497c478bd9Sstevel@tonic-gate 				"%s: accept() returned invalid socket (%s), %s",
8507c478bd9Sstevel@tonic-gate 				smfi->xxfi_name, sm_errstring(save_errno),
8517c478bd9Sstevel@tonic-gate 				acnt >= MAX_FAILS_A ? "abort" : "try again");
8527c478bd9Sstevel@tonic-gate 			MI_SLEEP(acnt);
8537c478bd9Sstevel@tonic-gate 			if (acnt >= MAX_FAILS_A)
8547c478bd9Sstevel@tonic-gate 			{
8557c478bd9Sstevel@tonic-gate 				ret = MI_FAILURE;
8567c478bd9Sstevel@tonic-gate 				break;
8577c478bd9Sstevel@tonic-gate 			}
8587c478bd9Sstevel@tonic-gate 			continue;
8597c478bd9Sstevel@tonic-gate 		}
8607c478bd9Sstevel@tonic-gate 		acnt = 0;	/* reset error counter for accept() */
8617c478bd9Sstevel@tonic-gate #if _FFR_DUP_FD
8627c478bd9Sstevel@tonic-gate 		dupfd = fcntl(connfd, F_DUPFD, 256);
8637c478bd9Sstevel@tonic-gate 		if (ValidSocket(dupfd)
8647c478bd9Sstevel@tonic-gate # if !SM_CONF_POLL
8657c478bd9Sstevel@tonic-gate 		    && SM_FD_OK_SELECT(dupfd)
8667c478bd9Sstevel@tonic-gate # endif /* !SM_CONF_POLL */
8677c478bd9Sstevel@tonic-gate 		   )
8687c478bd9Sstevel@tonic-gate 		{
8697c478bd9Sstevel@tonic-gate 			close(connfd);
8707c478bd9Sstevel@tonic-gate 			connfd = dupfd;
8717c478bd9Sstevel@tonic-gate 			dupfd = INVALID_SOCKET;
8727c478bd9Sstevel@tonic-gate 		}
8737c478bd9Sstevel@tonic-gate #endif /* _FFR_DUP_FD */
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 		if (setsockopt(connfd, SOL_SOCKET, SO_KEEPALIVE,
8767c478bd9Sstevel@tonic-gate 				(void *) &sockopt, sizeof sockopt) < 0)
8777c478bd9Sstevel@tonic-gate 		{
878*445f2479Sjbeck 			smi_log(SMI_LOG_WARN,
879*445f2479Sjbeck 				"%s: set keepalive failed (%s)",
8807c478bd9Sstevel@tonic-gate 				smfi->xxfi_name, sm_errstring(errno));
8817c478bd9Sstevel@tonic-gate 			/* XXX: continue? */
8827c478bd9Sstevel@tonic-gate 		}
8837c478bd9Sstevel@tonic-gate 		if ((ctx = (SMFICTX_PTR) malloc(sizeof *ctx)) == NULL)
8847c478bd9Sstevel@tonic-gate 		{
8857c478bd9Sstevel@tonic-gate 			(void) closesocket(connfd);
8867c478bd9Sstevel@tonic-gate 			mcnt++;
8877c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR, "%s: malloc(ctx) failed (%s), %s",
8887c478bd9Sstevel@tonic-gate 				smfi->xxfi_name, sm_errstring(save_errno),
8897c478bd9Sstevel@tonic-gate 				mcnt >= MAX_FAILS_M ? "abort" : "try again");
8907c478bd9Sstevel@tonic-gate 			MI_SLEEP(mcnt);
8917c478bd9Sstevel@tonic-gate 			if (mcnt >= MAX_FAILS_M)
8927c478bd9Sstevel@tonic-gate 			{
8937c478bd9Sstevel@tonic-gate 				ret = MI_FAILURE;
8947c478bd9Sstevel@tonic-gate 				break;
8957c478bd9Sstevel@tonic-gate 			}
8967c478bd9Sstevel@tonic-gate 			continue;
8977c478bd9Sstevel@tonic-gate 		}
8987c478bd9Sstevel@tonic-gate 		mcnt = 0;	/* reset error counter for malloc() */
8997c478bd9Sstevel@tonic-gate 		(void) memset(ctx, '\0', sizeof *ctx);
9007c478bd9Sstevel@tonic-gate 		ctx->ctx_sd = connfd;
9017c478bd9Sstevel@tonic-gate 		ctx->ctx_dbg = dbg;
9027c478bd9Sstevel@tonic-gate 		ctx->ctx_timeout = timeout;
9037c478bd9Sstevel@tonic-gate 		ctx->ctx_smfi = smfi;
9047c478bd9Sstevel@tonic-gate #if 0
9057c478bd9Sstevel@tonic-gate 		if (smfi->xxfi_eoh == NULL)
9067c478bd9Sstevel@tonic-gate 		if (smfi->xxfi_eom == NULL)
9077c478bd9Sstevel@tonic-gate 		if (smfi->xxfi_abort == NULL)
9087c478bd9Sstevel@tonic-gate 		if (smfi->xxfi_close == NULL)
9097c478bd9Sstevel@tonic-gate #endif /* 0 */
9107c478bd9Sstevel@tonic-gate 		if (smfi->xxfi_connect == NULL)
9117c478bd9Sstevel@tonic-gate 			ctx->ctx_pflags |= SMFIP_NOCONNECT;
9127c478bd9Sstevel@tonic-gate 		if (smfi->xxfi_helo == NULL)
9137c478bd9Sstevel@tonic-gate 			ctx->ctx_pflags |= SMFIP_NOHELO;
9147c478bd9Sstevel@tonic-gate 		if (smfi->xxfi_envfrom == NULL)
9157c478bd9Sstevel@tonic-gate 			ctx->ctx_pflags |= SMFIP_NOMAIL;
9167c478bd9Sstevel@tonic-gate 		if (smfi->xxfi_envrcpt == NULL)
9177c478bd9Sstevel@tonic-gate 			ctx->ctx_pflags |= SMFIP_NORCPT;
9187c478bd9Sstevel@tonic-gate 		if (smfi->xxfi_header == NULL)
9197c478bd9Sstevel@tonic-gate 			ctx->ctx_pflags |= SMFIP_NOHDRS;
9207c478bd9Sstevel@tonic-gate 		if (smfi->xxfi_eoh == NULL)
9217c478bd9Sstevel@tonic-gate 			ctx->ctx_pflags |= SMFIP_NOEOH;
9227c478bd9Sstevel@tonic-gate 		if (smfi->xxfi_body == NULL)
9237c478bd9Sstevel@tonic-gate 			ctx->ctx_pflags |= SMFIP_NOBODY;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		if ((r = thread_create(&thread_id,
9267c478bd9Sstevel@tonic-gate 					mi_thread_handle_wrapper,
9277c478bd9Sstevel@tonic-gate 					(void *) ctx)) != 0)
9287c478bd9Sstevel@tonic-gate 		{
9297c478bd9Sstevel@tonic-gate 			tcnt++;
9307c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
9317c478bd9Sstevel@tonic-gate 				"%s: thread_create() failed: %d, %s",
9327c478bd9Sstevel@tonic-gate 				smfi->xxfi_name,  r,
9337c478bd9Sstevel@tonic-gate 				tcnt >= MAX_FAILS_T ? "abort" : "try again");
9347c478bd9Sstevel@tonic-gate 			MI_SLEEP(tcnt);
9357c478bd9Sstevel@tonic-gate 			(void) closesocket(connfd);
9367c478bd9Sstevel@tonic-gate 			free(ctx);
9377c478bd9Sstevel@tonic-gate 			if (tcnt >= MAX_FAILS_T)
9387c478bd9Sstevel@tonic-gate 			{
9397c478bd9Sstevel@tonic-gate 				ret = MI_FAILURE;
9407c478bd9Sstevel@tonic-gate 				break;
9417c478bd9Sstevel@tonic-gate 			}
9427c478bd9Sstevel@tonic-gate 			continue;
9437c478bd9Sstevel@tonic-gate 		}
9447c478bd9Sstevel@tonic-gate 		tcnt = 0;
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 	if (ret != MI_SUCCESS)
9477c478bd9Sstevel@tonic-gate 		mi_stop_milters(MILTER_ABRT);
9487c478bd9Sstevel@tonic-gate 	else
9497c478bd9Sstevel@tonic-gate 	{
9507c478bd9Sstevel@tonic-gate 		if (mistop != MILTER_CONT)
9517c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_INFO, "%s: mi_stop=%d",
9527c478bd9Sstevel@tonic-gate 				smfi->xxfi_name, mistop);
9537c478bd9Sstevel@tonic-gate 		mi_closener();
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 	(void) smutex_destroy(&L_Mutex);
9567c478bd9Sstevel@tonic-gate 	return ret;
9577c478bd9Sstevel@tonic-gate }
958