1c5c4113dSnw141292 /* 2c5c4113dSnw141292 * CDDL HEADER START 3c5c4113dSnw141292 * 4c5c4113dSnw141292 * The contents of this file are subject to the terms of the 5c5c4113dSnw141292 * Common Development and Distribution License (the "License"). 6c5c4113dSnw141292 * You may not use this file except in compliance with the License. 7c5c4113dSnw141292 * 8c5c4113dSnw141292 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9c5c4113dSnw141292 * or http://www.opensolaris.org/os/licensing. 10c5c4113dSnw141292 * See the License for the specific language governing permissions 11c5c4113dSnw141292 * and limitations under the License. 12c5c4113dSnw141292 * 13c5c4113dSnw141292 * When distributing Covered Code, include this CDDL HEADER in each 14c5c4113dSnw141292 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15c5c4113dSnw141292 * If applicable, add the following below this CDDL HEADER, with the 16c5c4113dSnw141292 * fields enclosed by brackets "[]" replaced with your own identifying 17c5c4113dSnw141292 * information: Portions Copyright [yyyy] [name of copyright owner] 18c5c4113dSnw141292 * 19c5c4113dSnw141292 * CDDL HEADER END 20c5c4113dSnw141292 */ 21c5c4113dSnw141292 /* 22*7a8a68f5SJulian Pullen * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23c5c4113dSnw141292 * Use is subject to license terms. 24c5c4113dSnw141292 */ 25c5c4113dSnw141292 26c5c4113dSnw141292 27c5c4113dSnw141292 /* 28c5c4113dSnw141292 * main() of idmapd(1M) 29c5c4113dSnw141292 */ 30c5c4113dSnw141292 31c5c4113dSnw141292 #include "idmapd.h" 3271590c90Snw141292 #include <atomic.h> 33c5c4113dSnw141292 #include <signal.h> 34c5c4113dSnw141292 #include <rpc/pmap_clnt.h> /* for pmap_unset */ 35c5c4113dSnw141292 #include <string.h> /* strcmp */ 36c5c4113dSnw141292 #include <unistd.h> /* setsid */ 37c5c4113dSnw141292 #include <sys/types.h> 38c5c4113dSnw141292 #include <memory.h> 39c5c4113dSnw141292 #include <stropts.h> 40c5c4113dSnw141292 #include <netconfig.h> 41c5c4113dSnw141292 #include <sys/resource.h> /* rlimit */ 42c5c4113dSnw141292 #include <rpcsvc/daemon_utils.h> /* DAEMON_UID and DAEMON_GID */ 43c5c4113dSnw141292 #include <priv_utils.h> /* privileges */ 44c5c4113dSnw141292 #include <locale.h> 45c5c4113dSnw141292 #include <sys/systeminfo.h> 46c5c4113dSnw141292 #include <errno.h> 47c5c4113dSnw141292 #include <sys/wait.h> 48c5c4113dSnw141292 #include <sys/time.h> 49c5c4113dSnw141292 #include <zone.h> 50c5c4113dSnw141292 #include <door.h> 51c8e26105Sjp151216 #include <port.h> 52c5c4113dSnw141292 #include <tsol/label.h> 53c5c4113dSnw141292 #include <sys/resource.h> 54c5c4113dSnw141292 #include <sys/sid.h> 55c5c4113dSnw141292 #include <sys/idmap.h> 56bcced03bSjp151216 #include <pthread.h> 57c5c4113dSnw141292 58c5c4113dSnw141292 static void term_handler(int); 59c5c4113dSnw141292 static void init_idmapd(); 60c5c4113dSnw141292 static void fini_idmapd(); 61c5c4113dSnw141292 62*7a8a68f5SJulian Pullen 63c5c4113dSnw141292 #define _RPCSVC_CLOSEDOWN 120 64c5c4113dSnw141292 65c5c4113dSnw141292 int _rpcsvcstate = _IDLE; /* Set when a request is serviced */ 66c5c4113dSnw141292 int _rpcsvccount = 0; /* Number of requests being serviced */ 67c5c4113dSnw141292 mutex_t _svcstate_lock; /* lock for _rpcsvcstate, _rpcsvccount */ 68c5c4113dSnw141292 idmapd_state_t _idmapdstate; 69c5c4113dSnw141292 70c5c4113dSnw141292 SVCXPRT *xprt = NULL; 71c5c4113dSnw141292 72c5c4113dSnw141292 static int dfd = -1; /* our door server fildes, for unregistration */ 7371590c90Snw141292 static int degraded = 0; /* whether the FMRI has been marked degraded */ 74c5c4113dSnw141292 75bcced03bSjp151216 76bcced03bSjp151216 static uint32_t num_threads = 0; 77bcced03bSjp151216 static pthread_key_t create_threads_key; 78bcced03bSjp151216 static uint32_t max_threads = 40; 79bcced03bSjp151216 80*7a8a68f5SJulian Pullen 81*7a8a68f5SJulian Pullen /* 82*7a8a68f5SJulian Pullen * The following structure determines where the log messages from idmapdlog() 83*7a8a68f5SJulian Pullen * go to. It can be stderr (idmapd -d) and/or the real idmapdlog (idmapd). 84*7a8a68f5SJulian Pullen * 85*7a8a68f5SJulian Pullen * logstate.max_pri is integer cutoff necessary to silence low-priority 86*7a8a68f5SJulian Pullen * messages to stderr. idmapdlog has its own means so there a boolean 87*7a8a68f5SJulian Pullen * logstate.write_idmapdlog is enough. 88*7a8a68f5SJulian Pullen * 89*7a8a68f5SJulian Pullen * logstate.degraded is a mode used by idmapd in its degraded state. 90*7a8a68f5SJulian Pullen */ 91*7a8a68f5SJulian Pullen 92*7a8a68f5SJulian Pullen static struct { 93*7a8a68f5SJulian Pullen boolean_t write_syslog; 94*7a8a68f5SJulian Pullen int max_pri; /* Max priority written to stderr */ 95*7a8a68f5SJulian Pullen boolean_t degraded; 96*7a8a68f5SJulian Pullen } logstate = {B_FALSE, LOG_DEBUG, B_FALSE}; 97*7a8a68f5SJulian Pullen 98bcced03bSjp151216 /* 99bcced03bSjp151216 * Server door thread start routine. 100bcced03bSjp151216 * 101bcced03bSjp151216 * Set a TSD value to the door thread. This enables the destructor to 102bcced03bSjp151216 * be called when this thread exits. 103bcced03bSjp151216 */ 104bcced03bSjp151216 /*ARGSUSED*/ 105bcced03bSjp151216 static void * 106bcced03bSjp151216 idmapd_door_thread_start(void *arg) 107bcced03bSjp151216 { 108bcced03bSjp151216 static void *value = 0; 109bcced03bSjp151216 110bcced03bSjp151216 /* 111bcced03bSjp151216 * Disable cancellation to avoid memory leaks from not running 112bcced03bSjp151216 * the thread cleanup code. 113bcced03bSjp151216 */ 114bcced03bSjp151216 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); 115bcced03bSjp151216 (void) pthread_setspecific(create_threads_key, value); 116bcced03bSjp151216 (void) door_return(NULL, 0, NULL, 0); 117bcced03bSjp151216 118bcced03bSjp151216 /* make lint happy */ 119bcced03bSjp151216 return (NULL); 120bcced03bSjp151216 } 121bcced03bSjp151216 122bcced03bSjp151216 /* 123bcced03bSjp151216 * Server door threads creation 124bcced03bSjp151216 */ 125bcced03bSjp151216 /*ARGSUSED*/ 126bcced03bSjp151216 static void 127bcced03bSjp151216 idmapd_door_thread_create(door_info_t *dip) 128bcced03bSjp151216 { 129bcced03bSjp151216 int num; 130bcced03bSjp151216 pthread_t thread_id; 131bcced03bSjp151216 132bcced03bSjp151216 if ((num = atomic_inc_32_nv(&num_threads)) > max_threads) { 133bcced03bSjp151216 atomic_dec_32(&num_threads); 134bcced03bSjp151216 idmapdlog(LOG_DEBUG, 135bcced03bSjp151216 "thread creation refused - %d threads currently active", 136bcced03bSjp151216 num - 1); 137bcced03bSjp151216 return; 138bcced03bSjp151216 } 139bcced03bSjp151216 (void) pthread_create(&thread_id, NULL, idmapd_door_thread_start, NULL); 140bcced03bSjp151216 idmapdlog(LOG_DEBUG, 141bcced03bSjp151216 "created thread ID %d - %d threads currently active", 142bcced03bSjp151216 thread_id, num); 143bcced03bSjp151216 } 144bcced03bSjp151216 145bcced03bSjp151216 /* 146bcced03bSjp151216 * Server door thread cleanup 147bcced03bSjp151216 */ 148bcced03bSjp151216 /*ARGSUSED*/ 149bcced03bSjp151216 static void 150bcced03bSjp151216 idmapd_door_thread_cleanup(void *arg) 151bcced03bSjp151216 { 152bcced03bSjp151216 int num; 153bcced03bSjp151216 154bcced03bSjp151216 num = atomic_dec_32_nv(&num_threads); 155bcced03bSjp151216 idmapdlog(LOG_DEBUG, 156bcced03bSjp151216 "exiting thread ID %d - %d threads currently active", 157bcced03bSjp151216 pthread_self(), num); 158bcced03bSjp151216 } 159bcced03bSjp151216 160c5c4113dSnw141292 /* 161c5c4113dSnw141292 * This is needed for mech_krb5 -- we run as daemon, yes, but we want 16278b2cb9aSnw141292 * mech_krb5 to think we're root so it can get host/nodename.fqdn 16378b2cb9aSnw141292 * tickets for us so we can authenticate to AD as the machine account 16478b2cb9aSnw141292 * that we are. For more details look at the entry point in mech_krb5 16578b2cb9aSnw141292 * corresponding to gss_init_sec_context(). 16678b2cb9aSnw141292 * 16778b2cb9aSnw141292 * As a side effect of faking our effective UID to mech_krb5 we will use 16878b2cb9aSnw141292 * root's default ccache (/tmp/krb5cc_0). But if that's created by 16978b2cb9aSnw141292 * another process then we won't have access to it: we run as daemon and 17078b2cb9aSnw141292 * keep PRIV_FILE_DAC_READ, which is insufficient to share the ccache 17178b2cb9aSnw141292 * with others. We putenv("KRB5CCNAME=/var/run/idmap/ccache") in main() 17278b2cb9aSnw141292 * to avoid this issue; see main(). 173c5c4113dSnw141292 * 174c5c4113dSnw141292 * Someday we'll have gss/mech_krb5 extensions for acquiring initiator 175c5c4113dSnw141292 * creds with keytabs/raw keys, and someday we'll have extensions to 176c5c4113dSnw141292 * libsasl to specify creds/name to use on the initiator side, and 177c5c4113dSnw141292 * someday we'll have extensions to libldap to pass those through to 178c5c4113dSnw141292 * libsasl. Until then this interposer will have to do. 179c5c4113dSnw141292 * 180c5c4113dSnw141292 * Also, we have to tell lint to shut up: it thinks app_krb5_user_uid() 181c5c4113dSnw141292 * is defined but not used. 182c5c4113dSnw141292 */ 183c5c4113dSnw141292 /*LINTLIBRARY*/ 184c5c4113dSnw141292 uid_t 185c5c4113dSnw141292 app_krb5_user_uid(void) 186c5c4113dSnw141292 { 187c5c4113dSnw141292 return (0); 188c5c4113dSnw141292 } 189c5c4113dSnw141292 190c5c4113dSnw141292 /*ARGSUSED*/ 191c5c4113dSnw141292 static void 1924edd44c5Sjp151216 term_handler(int sig) 1934edd44c5Sjp151216 { 19471590c90Snw141292 idmapdlog(LOG_INFO, "Terminating."); 195c5c4113dSnw141292 fini_idmapd(); 196c5c4113dSnw141292 _exit(0); 197c5c4113dSnw141292 } 198c5c4113dSnw141292 19971590c90Snw141292 /*ARGSUSED*/ 20071590c90Snw141292 static void 20171590c90Snw141292 usr1_handler(int sig) 20271590c90Snw141292 { 203*7a8a68f5SJulian Pullen boolean_t saved_debug_mode = _idmapdstate.debug_mode; 20471590c90Snw141292 205*7a8a68f5SJulian Pullen _idmapdstate.debug_mode = B_TRUE; 206479ac375Sdm199847 idmap_log_stderr(LOG_DEBUG); 207479ac375Sdm199847 20871590c90Snw141292 print_idmapdstate(); 209479ac375Sdm199847 21071590c90Snw141292 _idmapdstate.debug_mode = saved_debug_mode; 211479ac375Sdm199847 idmap_log_stderr(_idmapdstate.daemon_mode ? -1 : LOG_DEBUG); 212479ac375Sdm199847 21371590c90Snw141292 } 21471590c90Snw141292 215c5c4113dSnw141292 static int pipe_fd = -1; 216c5c4113dSnw141292 217c5c4113dSnw141292 static void 2184edd44c5Sjp151216 daemonize_ready(void) 2194edd44c5Sjp151216 { 220c5c4113dSnw141292 char data = '\0'; 221c5c4113dSnw141292 /* 222c5c4113dSnw141292 * wake the parent 223c5c4113dSnw141292 */ 224c5c4113dSnw141292 (void) write(pipe_fd, &data, 1); 225c5c4113dSnw141292 (void) close(pipe_fd); 226c5c4113dSnw141292 } 227c5c4113dSnw141292 228c5c4113dSnw141292 static int 2294edd44c5Sjp151216 daemonize_start(void) 2304edd44c5Sjp151216 { 231c5c4113dSnw141292 char data; 232c5c4113dSnw141292 int status; 233c5c4113dSnw141292 int devnull; 234c5c4113dSnw141292 int filedes[2]; 235c5c4113dSnw141292 pid_t pid; 236c5c4113dSnw141292 23778b2cb9aSnw141292 (void) sigset(SIGPIPE, SIG_IGN); 238c5c4113dSnw141292 devnull = open("/dev/null", O_RDONLY); 239c5c4113dSnw141292 if (devnull < 0) 240c5c4113dSnw141292 return (-1); 241c5c4113dSnw141292 (void) dup2(devnull, 0); 242c5c4113dSnw141292 (void) dup2(2, 1); /* stderr only */ 243c5c4113dSnw141292 if (pipe(filedes) < 0) 244c5c4113dSnw141292 return (-1); 245c5c4113dSnw141292 if ((pid = fork1()) < 0) 246c5c4113dSnw141292 return (-1); 247c5c4113dSnw141292 if (pid != 0) { 248c5c4113dSnw141292 /* 249c5c4113dSnw141292 * parent 250c5c4113dSnw141292 */ 251c5c4113dSnw141292 (void) close(filedes[1]); 252c5c4113dSnw141292 if (read(filedes[0], &data, 1) == 1) { 253c5c4113dSnw141292 /* presume success */ 254c5c4113dSnw141292 _exit(0); 255c5c4113dSnw141292 } 256c5c4113dSnw141292 status = -1; 257c5c4113dSnw141292 (void) wait4(pid, &status, 0, NULL); 258c5c4113dSnw141292 if (WIFEXITED(status)) 259c5c4113dSnw141292 _exit(WEXITSTATUS(status)); 260c5c4113dSnw141292 else 261c5c4113dSnw141292 _exit(-1); 262c5c4113dSnw141292 } 263c5c4113dSnw141292 264c5c4113dSnw141292 /* 265c5c4113dSnw141292 * child 266c5c4113dSnw141292 */ 267c5c4113dSnw141292 pipe_fd = filedes[1]; 268c5c4113dSnw141292 (void) close(filedes[0]); 269c5c4113dSnw141292 (void) setsid(); 270c5c4113dSnw141292 (void) umask(0077); 271c5c4113dSnw141292 openlog("idmap", LOG_PID, LOG_DAEMON); 272479ac375Sdm199847 273c5c4113dSnw141292 return (0); 274c5c4113dSnw141292 } 275c5c4113dSnw141292 276c5c4113dSnw141292 277c5c4113dSnw141292 int 278c5c4113dSnw141292 main(int argc, char **argv) 279c5c4113dSnw141292 { 280c5c4113dSnw141292 int c; 2812b3ecdebSjp151216 struct rlimit rl; 282c5c4113dSnw141292 28371590c90Snw141292 _idmapdstate.daemon_mode = TRUE; 28471590c90Snw141292 _idmapdstate.debug_mode = FALSE; 28571590c90Snw141292 while ((c = getopt(argc, argv, "d")) != -1) { 286c5c4113dSnw141292 switch (c) { 287c5c4113dSnw141292 case 'd': 28871590c90Snw141292 _idmapdstate.daemon_mode = FALSE; 289c5c4113dSnw141292 break; 290c5c4113dSnw141292 default: 29171590c90Snw141292 fprintf(stderr, "Usage: /usr/lib/idmapd"); 29271590c90Snw141292 return (SMF_EXIT_ERR_CONFIG); 293c5c4113dSnw141292 break; 294c5c4113dSnw141292 } 295c5c4113dSnw141292 } 296c5c4113dSnw141292 297c5c4113dSnw141292 /* set locale and domain for internationalization */ 298c5c4113dSnw141292 (void) setlocale(LC_ALL, ""); 299c5c4113dSnw141292 (void) textdomain(TEXT_DOMAIN); 300c5c4113dSnw141292 301*7a8a68f5SJulian Pullen idmap_set_logger(idmapdlog); 302*7a8a68f5SJulian Pullen idmap_log_syslog(B_TRUE); 303479ac375Sdm199847 idmap_log_stderr(_idmapdstate.daemon_mode ? -1 : LOG_DEBUG); 304479ac375Sdm199847 305bda89588Sjp151216 if (is_system_labeled() && getzoneid() != GLOBAL_ZONEID) { 30671590c90Snw141292 idmapdlog(LOG_ERR, 30771590c90Snw141292 "with Trusted Extensions idmapd runs only in the " 308bda89588Sjp151216 "global zone"); 309c5c4113dSnw141292 exit(1); 310c5c4113dSnw141292 } 311c5c4113dSnw141292 3122b3ecdebSjp151216 /* 3132b3ecdebSjp151216 * Raise the fd limit to max 3142b3ecdebSjp151216 */ 3152b3ecdebSjp151216 if (getrlimit(RLIMIT_NOFILE, &rl) != 0) { 3162b3ecdebSjp151216 idmapdlog(LOG_ERR, "getrlimit failed"); 3172b3ecdebSjp151216 } else if (rl.rlim_cur < rl.rlim_max) { 3182b3ecdebSjp151216 rl.rlim_cur = rl.rlim_max; 3192b3ecdebSjp151216 if (setrlimit(RLIMIT_NOFILE, &rl) != 0) 3202b3ecdebSjp151216 idmapdlog(LOG_ERR, 3212b3ecdebSjp151216 "Unable to raise RLIMIT_NOFILE to %d", 3222b3ecdebSjp151216 rl.rlim_cur); 3232b3ecdebSjp151216 } 3242b3ecdebSjp151216 325c5c4113dSnw141292 (void) mutex_init(&_svcstate_lock, USYNC_THREAD, NULL); 326c5c4113dSnw141292 32771590c90Snw141292 if (_idmapdstate.daemon_mode == TRUE) { 328c5c4113dSnw141292 if (daemonize_start() < 0) { 329*7a8a68f5SJulian Pullen idmapdlog(LOG_ERR, "unable to daemonize"); 330c5c4113dSnw141292 exit(-1); 331c5c4113dSnw141292 } 332c5c4113dSnw141292 } else 333c5c4113dSnw141292 (void) umask(0077); 334c5c4113dSnw141292 33584decf41Sjp151216 idmap_init_tsd_key(); 33684decf41Sjp151216 337c5c4113dSnw141292 init_idmapd(); 338c5c4113dSnw141292 33978b2cb9aSnw141292 /* signal handlers that should run only after we're initialized */ 34078b2cb9aSnw141292 (void) sigset(SIGTERM, term_handler); 34171590c90Snw141292 (void) sigset(SIGUSR1, usr1_handler); 3420dcc7149Snw141292 (void) sigset(SIGHUP, idmap_cfg_hup_handler); 34378b2cb9aSnw141292 344c5c4113dSnw141292 if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, 345c5c4113dSnw141292 DAEMON_UID, DAEMON_GID, 346c5c4113dSnw141292 PRIV_PROC_AUDIT, PRIV_FILE_DAC_READ, 347c5c4113dSnw141292 (char *)NULL) == -1) { 34871590c90Snw141292 idmapdlog(LOG_ERR, "unable to drop privileges"); 349c5c4113dSnw141292 exit(1); 350c5c4113dSnw141292 } 351c5c4113dSnw141292 352c5c4113dSnw141292 __fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION, 353c5c4113dSnw141292 PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL); 354c5c4113dSnw141292 35571590c90Snw141292 if (_idmapdstate.daemon_mode == TRUE) 356c5c4113dSnw141292 daemonize_ready(); 357c5c4113dSnw141292 358c5c4113dSnw141292 /* With doors RPC this just wastes this thread, oh well */ 359c5c4113dSnw141292 svc_run(); 360c5c4113dSnw141292 return (0); 361c5c4113dSnw141292 } 362c5c4113dSnw141292 363c5c4113dSnw141292 static void 3644edd44c5Sjp151216 init_idmapd() 3654edd44c5Sjp151216 { 366c5c4113dSnw141292 int error; 367d3a612caSnw141292 int connmaxrec = IDMAP_MAX_DOOR_RPC; 368c5c4113dSnw141292 369bcced03bSjp151216 37078b2cb9aSnw141292 /* create directories as root and chown to daemon uid */ 37178b2cb9aSnw141292 if (create_directory(IDMAP_DBDIR, DAEMON_UID, DAEMON_GID) < 0) 37278b2cb9aSnw141292 exit(1); 37378b2cb9aSnw141292 if (create_directory(IDMAP_CACHEDIR, DAEMON_UID, DAEMON_GID) < 0) 37478b2cb9aSnw141292 exit(1); 37578b2cb9aSnw141292 37678b2cb9aSnw141292 /* 37778b2cb9aSnw141292 * Set KRB5CCNAME in the environment. See app_krb5_user_uid() 378e3c2d6aaSnw141292 * for more details. We blow away the existing one, if there is 379e3c2d6aaSnw141292 * one. 38078b2cb9aSnw141292 */ 381e3c2d6aaSnw141292 (void) unlink(IDMAP_CACHEDIR "/ccache"); 38278b2cb9aSnw141292 putenv("KRB5CCNAME=" IDMAP_CACHEDIR "/ccache"); 38378b2cb9aSnw141292 384c5c4113dSnw141292 if (sysinfo(SI_HOSTNAME, _idmapdstate.hostname, 385c5c4113dSnw141292 sizeof (_idmapdstate.hostname)) == -1) { 386c5c4113dSnw141292 error = errno; 38771590c90Snw141292 idmapdlog(LOG_ERR, "unable to determine hostname, error: %d", 388c5c4113dSnw141292 error); 389c5c4113dSnw141292 exit(1); 390c5c4113dSnw141292 } 391c5c4113dSnw141292 392e8c27ec8Sbaban if ((error = init_mapping_system()) < 0) { 39371590c90Snw141292 idmapdlog(LOG_ERR, "unable to initialize mapping system"); 394e8c27ec8Sbaban exit(error < -2 ? SMF_EXIT_ERR_CONFIG : 1); 395c5c4113dSnw141292 } 396c5c4113dSnw141292 397bcced03bSjp151216 (void) door_server_create(idmapd_door_thread_create); 398bcced03bSjp151216 if ((error = pthread_key_create(&create_threads_key, 399bcced03bSjp151216 idmapd_door_thread_cleanup)) != 0) { 400bcced03bSjp151216 idmapdlog(LOG_ERR, "unable to create threads key (%s)", 401bcced03bSjp151216 strerror(error)); 402bcced03bSjp151216 goto errout; 403bcced03bSjp151216 } 404bcced03bSjp151216 405d3a612caSnw141292 xprt = svc_door_create(idmap_prog_1, IDMAP_PROG, IDMAP_V1, connmaxrec); 406c5c4113dSnw141292 if (xprt == NULL) { 40771590c90Snw141292 idmapdlog(LOG_ERR, "unable to create door RPC service"); 408c5c4113dSnw141292 goto errout; 409c5c4113dSnw141292 } 410c5c4113dSnw141292 4118e228215Sdm199847 if (!svc_control(xprt, SVCSET_CONNMAXREC, &connmaxrec)) { 41271590c90Snw141292 idmapdlog(LOG_ERR, "unable to limit RPC request size"); 4138e228215Sdm199847 goto errout; 4148e228215Sdm199847 } 4158e228215Sdm199847 416c5c4113dSnw141292 dfd = xprt->xp_fd; 417c5c4113dSnw141292 418c5c4113dSnw141292 if (dfd == -1) { 41971590c90Snw141292 idmapdlog(LOG_ERR, "unable to register door"); 420c5c4113dSnw141292 goto errout; 421c5c4113dSnw141292 } 422c5c4113dSnw141292 if ((error = idmap_reg(dfd)) != 0) { 42371590c90Snw141292 idmapdlog(LOG_ERR, "unable to register door (%s)", 424bda89588Sjp151216 strerror(errno)); 425c5c4113dSnw141292 goto errout; 426c5c4113dSnw141292 } 427c5c4113dSnw141292 428c5c4113dSnw141292 if ((error = allocids(_idmapdstate.new_eph_db, 429c5c4113dSnw141292 8192, &_idmapdstate.next_uid, 430c5c4113dSnw141292 8192, &_idmapdstate.next_gid)) != 0) { 43171590c90Snw141292 idmapdlog(LOG_ERR, "unable to allocate ephemeral IDs (%s)", 43271590c90Snw141292 strerror(errno)); 433c5c4113dSnw141292 _idmapdstate.next_uid = _idmapdstate.limit_uid = SENTINEL_PID; 434c5c4113dSnw141292 _idmapdstate.next_gid = _idmapdstate.limit_gid = SENTINEL_PID; 435c5c4113dSnw141292 } else { 436c5c4113dSnw141292 _idmapdstate.limit_uid = _idmapdstate.next_uid + 8192; 437c5c4113dSnw141292 _idmapdstate.limit_gid = _idmapdstate.next_gid + 8192; 438c5c4113dSnw141292 } 439c5c4113dSnw141292 440c5c4113dSnw141292 print_idmapdstate(); 441c5c4113dSnw141292 442c5c4113dSnw141292 return; 443c5c4113dSnw141292 444c5c4113dSnw141292 errout: 445c5c4113dSnw141292 fini_idmapd(); 446c5c4113dSnw141292 exit(1); 447c5c4113dSnw141292 } 448c5c4113dSnw141292 449c5c4113dSnw141292 static void 4504edd44c5Sjp151216 fini_idmapd() 4514edd44c5Sjp151216 { 452c5c4113dSnw141292 idmap_unreg(dfd); 453c5c4113dSnw141292 fini_mapping_system(); 454c5c4113dSnw141292 if (xprt != NULL) 455c5c4113dSnw141292 svc_destroy(xprt); 456c5c4113dSnw141292 } 457c5c4113dSnw141292 45871590c90Snw141292 static 45971590c90Snw141292 const char * 46071590c90Snw141292 get_fmri(void) 46171590c90Snw141292 { 46271590c90Snw141292 static char *fmri = NULL; 46371590c90Snw141292 static char buf[60]; 46471590c90Snw141292 char *s; 46571590c90Snw141292 46671590c90Snw141292 membar_consumer(); 46771590c90Snw141292 s = fmri; 46871590c90Snw141292 if (s != NULL && *s == '\0') 46971590c90Snw141292 return (NULL); 47071590c90Snw141292 else if (s != NULL) 47171590c90Snw141292 return (s); 47271590c90Snw141292 47371590c90Snw141292 if ((s = getenv("SMF_FMRI")) == NULL || strlen(s) >= sizeof (buf)) 47471590c90Snw141292 buf[0] = '\0'; 47571590c90Snw141292 else 47671590c90Snw141292 (void) strlcpy(buf, s, sizeof (buf)); 47771590c90Snw141292 47871590c90Snw141292 membar_producer(); 47971590c90Snw141292 fmri = buf; 48071590c90Snw141292 48171590c90Snw141292 return (get_fmri()); 48271590c90Snw141292 } 48371590c90Snw141292 48471590c90Snw141292 /* 48571590c90Snw141292 * Wrappers for smf_degrade/restore_instance() 48671590c90Snw141292 * 48771590c90Snw141292 * smf_restore_instance() is too heavy duty to be calling every time we 48871590c90Snw141292 * have a successful AD name<->SID lookup. 48971590c90Snw141292 */ 49071590c90Snw141292 void 491349d5d8fSnw141292 degrade_svc(int poke_discovery, const char *reason) 49271590c90Snw141292 { 49371590c90Snw141292 const char *fmri; 49471590c90Snw141292 49571590c90Snw141292 /* 49671590c90Snw141292 * If the config update thread is in a state where auto-discovery could 49771590c90Snw141292 * be re-tried, then this will make it try it -- a sort of auto-refresh. 49871590c90Snw141292 */ 499349d5d8fSnw141292 if (poke_discovery) 50071590c90Snw141292 idmap_cfg_poke_updates(); 50171590c90Snw141292 50271590c90Snw141292 membar_consumer(); 50371590c90Snw141292 if (degraded) 50471590c90Snw141292 return; 505349d5d8fSnw141292 506349d5d8fSnw141292 idmapdlog(LOG_ERR, "Degraded operation (%s). If you are running an " 507349d5d8fSnw141292 "SMB server in workgroup mode, or if you're not running an SMB " 508349d5d8fSnw141292 "server, then you can ignore this message", reason); 509349d5d8fSnw141292 51071590c90Snw141292 membar_producer(); 51171590c90Snw141292 degraded = 1; 512*7a8a68f5SJulian Pullen idmap_log_degraded(B_TRUE); 51371590c90Snw141292 51471590c90Snw141292 if ((fmri = get_fmri()) != NULL) 51571590c90Snw141292 (void) smf_degrade_instance(fmri, 0); 51671590c90Snw141292 } 51771590c90Snw141292 51871590c90Snw141292 void 51971590c90Snw141292 restore_svc(void) 52071590c90Snw141292 { 52171590c90Snw141292 const char *fmri; 52271590c90Snw141292 52371590c90Snw141292 membar_consumer(); 52471590c90Snw141292 if (!degraded) 52571590c90Snw141292 return; 52671590c90Snw141292 52771590c90Snw141292 if ((fmri = get_fmri()) == NULL) 52871590c90Snw141292 (void) smf_restore_instance(fmri); 52971590c90Snw141292 53071590c90Snw141292 membar_producer(); 53171590c90Snw141292 degraded = 0; 532*7a8a68f5SJulian Pullen idmap_log_degraded(B_FALSE); 533479ac375Sdm199847 534349d5d8fSnw141292 idmapdlog(LOG_NOTICE, "Normal operation restored"); 53571590c90Snw141292 } 53671590c90Snw141292 537*7a8a68f5SJulian Pullen 538*7a8a68f5SJulian Pullen /* printflike */ 539c5c4113dSnw141292 void 540*7a8a68f5SJulian Pullen idmapdlog(int pri, const char *format, ...) { 541c5c4113dSnw141292 va_list args; 542c5c4113dSnw141292 543*7a8a68f5SJulian Pullen if (pri <= logstate.max_pri) { 544c5c4113dSnw141292 va_start(args, format); 545c5c4113dSnw141292 (void) vfprintf(stderr, format, args); 546c5c4113dSnw141292 (void) fprintf(stderr, "\n"); 547*7a8a68f5SJulian Pullen va_end(args); 548c5c4113dSnw141292 } 54971590c90Snw141292 55071590c90Snw141292 /* 55171590c90Snw141292 * We don't want to fill up the logs with useless messages when 55271590c90Snw141292 * we're degraded, but we still want to log. 55371590c90Snw141292 */ 554*7a8a68f5SJulian Pullen if (logstate.degraded) 55571590c90Snw141292 pri = LOG_DEBUG; 55671590c90Snw141292 557*7a8a68f5SJulian Pullen if (logstate.write_syslog) { 558*7a8a68f5SJulian Pullen va_start(args, format); 559*7a8a68f5SJulian Pullen vsyslog(pri, format, args); 560c5c4113dSnw141292 va_end(args); 561c5c4113dSnw141292 } 562*7a8a68f5SJulian Pullen } 563*7a8a68f5SJulian Pullen 564*7a8a68f5SJulian Pullen void 565*7a8a68f5SJulian Pullen idmap_log_stderr(int pri) 566*7a8a68f5SJulian Pullen { 567*7a8a68f5SJulian Pullen logstate.max_pri = pri; 568*7a8a68f5SJulian Pullen } 569*7a8a68f5SJulian Pullen 570*7a8a68f5SJulian Pullen void 571*7a8a68f5SJulian Pullen idmap_log_syslog(boolean_t what) 572*7a8a68f5SJulian Pullen { 573*7a8a68f5SJulian Pullen logstate.write_syslog = what; 574*7a8a68f5SJulian Pullen } 575*7a8a68f5SJulian Pullen 576*7a8a68f5SJulian Pullen void 577*7a8a68f5SJulian Pullen idmap_log_degraded(boolean_t what) 578*7a8a68f5SJulian Pullen { 579*7a8a68f5SJulian Pullen logstate.degraded = what; 580*7a8a68f5SJulian Pullen } 581