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