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 /* 22bda89588Sjp151216 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23c5c4113dSnw141292 * Use is subject to license terms. 24c5c4113dSnw141292 */ 25c5c4113dSnw141292 26c5c4113dSnw141292 #pragma ident "%Z%%M% %I% %E% SMI" 27c5c4113dSnw141292 28c5c4113dSnw141292 /* 29c5c4113dSnw141292 * main() of idmapd(1M) 30c5c4113dSnw141292 */ 31c5c4113dSnw141292 32c5c4113dSnw141292 #include "idmapd.h" 3371590c90Snw141292 #include <atomic.h> 34c5c4113dSnw141292 #include <signal.h> 35c5c4113dSnw141292 #include <rpc/pmap_clnt.h> /* for pmap_unset */ 36c5c4113dSnw141292 #include <string.h> /* strcmp */ 37c5c4113dSnw141292 #include <unistd.h> /* setsid */ 38c5c4113dSnw141292 #include <sys/types.h> 39c5c4113dSnw141292 #include <memory.h> 40c5c4113dSnw141292 #include <stropts.h> 41c5c4113dSnw141292 #include <netconfig.h> 42c5c4113dSnw141292 #include <sys/resource.h> /* rlimit */ 43c5c4113dSnw141292 #include <syslog.h> 44c5c4113dSnw141292 #include <rpcsvc/daemon_utils.h> /* DAEMON_UID and DAEMON_GID */ 45c5c4113dSnw141292 #include <priv_utils.h> /* privileges */ 46c5c4113dSnw141292 #include <locale.h> 47c5c4113dSnw141292 #include <sys/systeminfo.h> 48c5c4113dSnw141292 #include <errno.h> 49c5c4113dSnw141292 #include <sys/wait.h> 50c5c4113dSnw141292 #include <sys/time.h> 51c5c4113dSnw141292 #include <zone.h> 52c5c4113dSnw141292 #include <door.h> 53c8e26105Sjp151216 #include <port.h> 54c5c4113dSnw141292 #include <tsol/label.h> 55c5c4113dSnw141292 #include <sys/resource.h> 56c5c4113dSnw141292 #include <sys/sid.h> 57c5c4113dSnw141292 #include <sys/idmap.h> 58*bcced03bSjp151216 #include <pthread.h> 59c5c4113dSnw141292 60c5c4113dSnw141292 static void term_handler(int); 61c5c4113dSnw141292 static void init_idmapd(); 62c5c4113dSnw141292 static void fini_idmapd(); 63c5c4113dSnw141292 64c5c4113dSnw141292 #define _RPCSVC_CLOSEDOWN 120 65c5c4113dSnw141292 66c5c4113dSnw141292 int _rpcsvcstate = _IDLE; /* Set when a request is serviced */ 67c5c4113dSnw141292 int _rpcsvccount = 0; /* Number of requests being serviced */ 68c5c4113dSnw141292 mutex_t _svcstate_lock; /* lock for _rpcsvcstate, _rpcsvccount */ 69c5c4113dSnw141292 idmapd_state_t _idmapdstate; 70c5c4113dSnw141292 71c5c4113dSnw141292 SVCXPRT *xprt = NULL; 72c5c4113dSnw141292 73c5c4113dSnw141292 static int dfd = -1; /* our door server fildes, for unregistration */ 7471590c90Snw141292 static int degraded = 0; /* whether the FMRI has been marked degraded */ 75c5c4113dSnw141292 76*bcced03bSjp151216 77*bcced03bSjp151216 static uint32_t num_threads = 0; 78*bcced03bSjp151216 static pthread_key_t create_threads_key; 79*bcced03bSjp151216 static uint32_t max_threads = 40; 80*bcced03bSjp151216 81*bcced03bSjp151216 /* 82*bcced03bSjp151216 * Server door thread start routine. 83*bcced03bSjp151216 * 84*bcced03bSjp151216 * Set a TSD value to the door thread. This enables the destructor to 85*bcced03bSjp151216 * be called when this thread exits. 86*bcced03bSjp151216 */ 87*bcced03bSjp151216 /*ARGSUSED*/ 88*bcced03bSjp151216 static void * 89*bcced03bSjp151216 idmapd_door_thread_start(void *arg) 90*bcced03bSjp151216 { 91*bcced03bSjp151216 static void *value = 0; 92*bcced03bSjp151216 93*bcced03bSjp151216 /* 94*bcced03bSjp151216 * Disable cancellation to avoid memory leaks from not running 95*bcced03bSjp151216 * the thread cleanup code. 96*bcced03bSjp151216 */ 97*bcced03bSjp151216 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); 98*bcced03bSjp151216 (void) pthread_setspecific(create_threads_key, value); 99*bcced03bSjp151216 (void) door_return(NULL, 0, NULL, 0); 100*bcced03bSjp151216 101*bcced03bSjp151216 /* make lint happy */ 102*bcced03bSjp151216 return (NULL); 103*bcced03bSjp151216 } 104*bcced03bSjp151216 105*bcced03bSjp151216 /* 106*bcced03bSjp151216 * Server door threads creation 107*bcced03bSjp151216 */ 108*bcced03bSjp151216 /*ARGSUSED*/ 109*bcced03bSjp151216 static void 110*bcced03bSjp151216 idmapd_door_thread_create(door_info_t *dip) 111*bcced03bSjp151216 { 112*bcced03bSjp151216 int num; 113*bcced03bSjp151216 pthread_t thread_id; 114*bcced03bSjp151216 115*bcced03bSjp151216 if ((num = atomic_inc_32_nv(&num_threads)) > max_threads) { 116*bcced03bSjp151216 atomic_dec_32(&num_threads); 117*bcced03bSjp151216 idmapdlog(LOG_DEBUG, 118*bcced03bSjp151216 "thread creation refused - %d threads currently active", 119*bcced03bSjp151216 num - 1); 120*bcced03bSjp151216 return; 121*bcced03bSjp151216 } 122*bcced03bSjp151216 (void) pthread_create(&thread_id, NULL, idmapd_door_thread_start, NULL); 123*bcced03bSjp151216 idmapdlog(LOG_DEBUG, 124*bcced03bSjp151216 "created thread ID %d - %d threads currently active", 125*bcced03bSjp151216 thread_id, num); 126*bcced03bSjp151216 } 127*bcced03bSjp151216 128*bcced03bSjp151216 /* 129*bcced03bSjp151216 * Server door thread cleanup 130*bcced03bSjp151216 */ 131*bcced03bSjp151216 /*ARGSUSED*/ 132*bcced03bSjp151216 static void 133*bcced03bSjp151216 idmapd_door_thread_cleanup(void *arg) 134*bcced03bSjp151216 { 135*bcced03bSjp151216 int num; 136*bcced03bSjp151216 137*bcced03bSjp151216 num = atomic_dec_32_nv(&num_threads); 138*bcced03bSjp151216 idmapdlog(LOG_DEBUG, 139*bcced03bSjp151216 "exiting thread ID %d - %d threads currently active", 140*bcced03bSjp151216 pthread_self(), num); 141*bcced03bSjp151216 } 142*bcced03bSjp151216 143c5c4113dSnw141292 /* 144c5c4113dSnw141292 * This is needed for mech_krb5 -- we run as daemon, yes, but we want 14578b2cb9aSnw141292 * mech_krb5 to think we're root so it can get host/nodename.fqdn 14678b2cb9aSnw141292 * tickets for us so we can authenticate to AD as the machine account 14778b2cb9aSnw141292 * that we are. For more details look at the entry point in mech_krb5 14878b2cb9aSnw141292 * corresponding to gss_init_sec_context(). 14978b2cb9aSnw141292 * 15078b2cb9aSnw141292 * As a side effect of faking our effective UID to mech_krb5 we will use 15178b2cb9aSnw141292 * root's default ccache (/tmp/krb5cc_0). But if that's created by 15278b2cb9aSnw141292 * another process then we won't have access to it: we run as daemon and 15378b2cb9aSnw141292 * keep PRIV_FILE_DAC_READ, which is insufficient to share the ccache 15478b2cb9aSnw141292 * with others. We putenv("KRB5CCNAME=/var/run/idmap/ccache") in main() 15578b2cb9aSnw141292 * to avoid this issue; see main(). 156c5c4113dSnw141292 * 157c5c4113dSnw141292 * Someday we'll have gss/mech_krb5 extensions for acquiring initiator 158c5c4113dSnw141292 * creds with keytabs/raw keys, and someday we'll have extensions to 159c5c4113dSnw141292 * libsasl to specify creds/name to use on the initiator side, and 160c5c4113dSnw141292 * someday we'll have extensions to libldap to pass those through to 161c5c4113dSnw141292 * libsasl. Until then this interposer will have to do. 162c5c4113dSnw141292 * 163c5c4113dSnw141292 * Also, we have to tell lint to shut up: it thinks app_krb5_user_uid() 164c5c4113dSnw141292 * is defined but not used. 165c5c4113dSnw141292 */ 166c5c4113dSnw141292 /*LINTLIBRARY*/ 167c5c4113dSnw141292 uid_t 168c5c4113dSnw141292 app_krb5_user_uid(void) 169c5c4113dSnw141292 { 170c5c4113dSnw141292 return (0); 171c5c4113dSnw141292 } 172c5c4113dSnw141292 173c5c4113dSnw141292 /*ARGSUSED*/ 174c5c4113dSnw141292 static void 1754edd44c5Sjp151216 term_handler(int sig) 1764edd44c5Sjp151216 { 17771590c90Snw141292 idmapdlog(LOG_INFO, "Terminating."); 178c5c4113dSnw141292 fini_idmapd(); 179c5c4113dSnw141292 _exit(0); 180c5c4113dSnw141292 } 181c5c4113dSnw141292 18271590c90Snw141292 /*ARGSUSED*/ 18371590c90Snw141292 static void 18471590c90Snw141292 usr1_handler(int sig) 18571590c90Snw141292 { 18671590c90Snw141292 bool_t saved_debug_mode = _idmapdstate.debug_mode; 18771590c90Snw141292 18871590c90Snw141292 _idmapdstate.debug_mode = TRUE; 18971590c90Snw141292 print_idmapdstate(); 19071590c90Snw141292 _idmapdstate.debug_mode = saved_debug_mode; 19171590c90Snw141292 } 19271590c90Snw141292 193c5c4113dSnw141292 static int pipe_fd = -1; 194c5c4113dSnw141292 195c5c4113dSnw141292 static void 1964edd44c5Sjp151216 daemonize_ready(void) 1974edd44c5Sjp151216 { 198c5c4113dSnw141292 char data = '\0'; 199c5c4113dSnw141292 /* 200c5c4113dSnw141292 * wake the parent 201c5c4113dSnw141292 */ 202c5c4113dSnw141292 (void) write(pipe_fd, &data, 1); 203c5c4113dSnw141292 (void) close(pipe_fd); 204c5c4113dSnw141292 } 205c5c4113dSnw141292 206c5c4113dSnw141292 static int 2074edd44c5Sjp151216 daemonize_start(void) 2084edd44c5Sjp151216 { 209c5c4113dSnw141292 char data; 210c5c4113dSnw141292 int status; 211c5c4113dSnw141292 int devnull; 212c5c4113dSnw141292 int filedes[2]; 213c5c4113dSnw141292 pid_t pid; 214c5c4113dSnw141292 21578b2cb9aSnw141292 (void) sigset(SIGPIPE, SIG_IGN); 216c5c4113dSnw141292 devnull = open("/dev/null", O_RDONLY); 217c5c4113dSnw141292 if (devnull < 0) 218c5c4113dSnw141292 return (-1); 219c5c4113dSnw141292 (void) dup2(devnull, 0); 220c5c4113dSnw141292 (void) dup2(2, 1); /* stderr only */ 221c5c4113dSnw141292 if (pipe(filedes) < 0) 222c5c4113dSnw141292 return (-1); 223c5c4113dSnw141292 if ((pid = fork1()) < 0) 224c5c4113dSnw141292 return (-1); 225c5c4113dSnw141292 if (pid != 0) { 226c5c4113dSnw141292 /* 227c5c4113dSnw141292 * parent 228c5c4113dSnw141292 */ 229c5c4113dSnw141292 (void) close(filedes[1]); 230c5c4113dSnw141292 if (read(filedes[0], &data, 1) == 1) { 231c5c4113dSnw141292 /* presume success */ 232c5c4113dSnw141292 _exit(0); 233c5c4113dSnw141292 } 234c5c4113dSnw141292 status = -1; 235c5c4113dSnw141292 (void) wait4(pid, &status, 0, NULL); 236c5c4113dSnw141292 if (WIFEXITED(status)) 237c5c4113dSnw141292 _exit(WEXITSTATUS(status)); 238c5c4113dSnw141292 else 239c5c4113dSnw141292 _exit(-1); 240c5c4113dSnw141292 } 241c5c4113dSnw141292 242c5c4113dSnw141292 /* 243c5c4113dSnw141292 * child 244c5c4113dSnw141292 */ 245c5c4113dSnw141292 pipe_fd = filedes[1]; 246c5c4113dSnw141292 (void) close(filedes[0]); 247c5c4113dSnw141292 (void) setsid(); 248c5c4113dSnw141292 (void) umask(0077); 249c5c4113dSnw141292 openlog("idmap", LOG_PID, LOG_DAEMON); 250c5c4113dSnw141292 return (0); 251c5c4113dSnw141292 } 252c5c4113dSnw141292 253c5c4113dSnw141292 254c5c4113dSnw141292 int 255c5c4113dSnw141292 main(int argc, char **argv) 256c5c4113dSnw141292 { 257c5c4113dSnw141292 int c; 2582b3ecdebSjp151216 struct rlimit rl; 259c5c4113dSnw141292 26071590c90Snw141292 _idmapdstate.daemon_mode = TRUE; 26171590c90Snw141292 _idmapdstate.debug_mode = FALSE; 26271590c90Snw141292 while ((c = getopt(argc, argv, "d")) != -1) { 263c5c4113dSnw141292 switch (c) { 264c5c4113dSnw141292 case 'd': 26571590c90Snw141292 _idmapdstate.daemon_mode = FALSE; 266c5c4113dSnw141292 break; 267c5c4113dSnw141292 default: 26871590c90Snw141292 fprintf(stderr, "Usage: /usr/lib/idmapd"); 26971590c90Snw141292 return (SMF_EXIT_ERR_CONFIG); 270c5c4113dSnw141292 break; 271c5c4113dSnw141292 } 272c5c4113dSnw141292 } 273c5c4113dSnw141292 274c5c4113dSnw141292 /* set locale and domain for internationalization */ 275c5c4113dSnw141292 (void) setlocale(LC_ALL, ""); 276c5c4113dSnw141292 (void) textdomain(TEXT_DOMAIN); 277c5c4113dSnw141292 278bda89588Sjp151216 if (is_system_labeled() && getzoneid() != GLOBAL_ZONEID) { 27971590c90Snw141292 idmapdlog(LOG_ERR, 28071590c90Snw141292 "with Trusted Extensions idmapd runs only in the " 281bda89588Sjp151216 "global zone"); 282c5c4113dSnw141292 exit(1); 283c5c4113dSnw141292 } 284c5c4113dSnw141292 2852b3ecdebSjp151216 /* 2862b3ecdebSjp151216 * Raise the fd limit to max 2872b3ecdebSjp151216 */ 2882b3ecdebSjp151216 if (getrlimit(RLIMIT_NOFILE, &rl) != 0) { 2892b3ecdebSjp151216 idmapdlog(LOG_ERR, "getrlimit failed"); 2902b3ecdebSjp151216 } else if (rl.rlim_cur < rl.rlim_max) { 2912b3ecdebSjp151216 rl.rlim_cur = rl.rlim_max; 2922b3ecdebSjp151216 if (setrlimit(RLIMIT_NOFILE, &rl) != 0) 2932b3ecdebSjp151216 idmapdlog(LOG_ERR, 2942b3ecdebSjp151216 "Unable to raise RLIMIT_NOFILE to %d", 2952b3ecdebSjp151216 rl.rlim_cur); 2962b3ecdebSjp151216 } 2972b3ecdebSjp151216 298c5c4113dSnw141292 (void) mutex_init(&_svcstate_lock, USYNC_THREAD, NULL); 299c5c4113dSnw141292 30071590c90Snw141292 if (_idmapdstate.daemon_mode == TRUE) { 301c5c4113dSnw141292 if (daemonize_start() < 0) { 30271590c90Snw141292 (void) idmapdlog(LOG_ERR, "unable to daemonize"); 303c5c4113dSnw141292 exit(-1); 304c5c4113dSnw141292 } 305c5c4113dSnw141292 } else 306c5c4113dSnw141292 (void) umask(0077); 307c5c4113dSnw141292 30884decf41Sjp151216 idmap_init_tsd_key(); 30984decf41Sjp151216 310c5c4113dSnw141292 init_idmapd(); 311c5c4113dSnw141292 31278b2cb9aSnw141292 /* signal handlers that should run only after we're initialized */ 31378b2cb9aSnw141292 (void) sigset(SIGTERM, term_handler); 31471590c90Snw141292 (void) sigset(SIGUSR1, usr1_handler); 3150dcc7149Snw141292 (void) sigset(SIGHUP, idmap_cfg_hup_handler); 31678b2cb9aSnw141292 317c5c4113dSnw141292 if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, 318c5c4113dSnw141292 DAEMON_UID, DAEMON_GID, 319c5c4113dSnw141292 PRIV_PROC_AUDIT, PRIV_FILE_DAC_READ, 320c5c4113dSnw141292 (char *)NULL) == -1) { 32171590c90Snw141292 idmapdlog(LOG_ERR, "unable to drop privileges"); 322c5c4113dSnw141292 exit(1); 323c5c4113dSnw141292 } 324c5c4113dSnw141292 325c5c4113dSnw141292 __fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION, 326c5c4113dSnw141292 PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL); 327c5c4113dSnw141292 32871590c90Snw141292 if (_idmapdstate.daemon_mode == TRUE) 329c5c4113dSnw141292 daemonize_ready(); 330c5c4113dSnw141292 331c5c4113dSnw141292 /* With doors RPC this just wastes this thread, oh well */ 332c5c4113dSnw141292 svc_run(); 333c5c4113dSnw141292 return (0); 334c5c4113dSnw141292 } 335c5c4113dSnw141292 336c5c4113dSnw141292 static void 3374edd44c5Sjp151216 init_idmapd() 3384edd44c5Sjp151216 { 339c5c4113dSnw141292 int error; 340d3a612caSnw141292 int connmaxrec = IDMAP_MAX_DOOR_RPC; 341c5c4113dSnw141292 342*bcced03bSjp151216 34378b2cb9aSnw141292 /* create directories as root and chown to daemon uid */ 34478b2cb9aSnw141292 if (create_directory(IDMAP_DBDIR, DAEMON_UID, DAEMON_GID) < 0) 34578b2cb9aSnw141292 exit(1); 34678b2cb9aSnw141292 if (create_directory(IDMAP_CACHEDIR, DAEMON_UID, DAEMON_GID) < 0) 34778b2cb9aSnw141292 exit(1); 34878b2cb9aSnw141292 34978b2cb9aSnw141292 /* 35078b2cb9aSnw141292 * Set KRB5CCNAME in the environment. See app_krb5_user_uid() 351e3c2d6aaSnw141292 * for more details. We blow away the existing one, if there is 352e3c2d6aaSnw141292 * one. 35378b2cb9aSnw141292 */ 354e3c2d6aaSnw141292 (void) unlink(IDMAP_CACHEDIR "/ccache"); 35578b2cb9aSnw141292 putenv("KRB5CCNAME=" IDMAP_CACHEDIR "/ccache"); 35678b2cb9aSnw141292 357c5c4113dSnw141292 if (sysinfo(SI_HOSTNAME, _idmapdstate.hostname, 358c5c4113dSnw141292 sizeof (_idmapdstate.hostname)) == -1) { 359c5c4113dSnw141292 error = errno; 36071590c90Snw141292 idmapdlog(LOG_ERR, "unable to determine hostname, error: %d", 361c5c4113dSnw141292 error); 362c5c4113dSnw141292 exit(1); 363c5c4113dSnw141292 } 364c5c4113dSnw141292 365e8c27ec8Sbaban if ((error = init_mapping_system()) < 0) { 36671590c90Snw141292 idmapdlog(LOG_ERR, "unable to initialize mapping system"); 367e8c27ec8Sbaban exit(error < -2 ? SMF_EXIT_ERR_CONFIG : 1); 368c5c4113dSnw141292 } 369c5c4113dSnw141292 370*bcced03bSjp151216 (void) door_server_create(idmapd_door_thread_create); 371*bcced03bSjp151216 if ((error = pthread_key_create(&create_threads_key, 372*bcced03bSjp151216 idmapd_door_thread_cleanup)) != 0) { 373*bcced03bSjp151216 idmapdlog(LOG_ERR, "unable to create threads key (%s)", 374*bcced03bSjp151216 strerror(error)); 375*bcced03bSjp151216 goto errout; 376*bcced03bSjp151216 } 377*bcced03bSjp151216 378d3a612caSnw141292 xprt = svc_door_create(idmap_prog_1, IDMAP_PROG, IDMAP_V1, connmaxrec); 379c5c4113dSnw141292 if (xprt == NULL) { 38071590c90Snw141292 idmapdlog(LOG_ERR, "unable to create door RPC service"); 381c5c4113dSnw141292 goto errout; 382c5c4113dSnw141292 } 383c5c4113dSnw141292 3848e228215Sdm199847 if (!svc_control(xprt, SVCSET_CONNMAXREC, &connmaxrec)) { 38571590c90Snw141292 idmapdlog(LOG_ERR, "unable to limit RPC request size"); 3868e228215Sdm199847 goto errout; 3878e228215Sdm199847 } 3888e228215Sdm199847 389c5c4113dSnw141292 dfd = xprt->xp_fd; 390c5c4113dSnw141292 391c5c4113dSnw141292 if (dfd == -1) { 39271590c90Snw141292 idmapdlog(LOG_ERR, "unable to register door"); 393c5c4113dSnw141292 goto errout; 394c5c4113dSnw141292 } 395c5c4113dSnw141292 if ((error = idmap_reg(dfd)) != 0) { 39671590c90Snw141292 idmapdlog(LOG_ERR, "unable to register door (%s)", 397bda89588Sjp151216 strerror(errno)); 398c5c4113dSnw141292 goto errout; 399c5c4113dSnw141292 } 400c5c4113dSnw141292 401c5c4113dSnw141292 if ((error = allocids(_idmapdstate.new_eph_db, 402c5c4113dSnw141292 8192, &_idmapdstate.next_uid, 403c5c4113dSnw141292 8192, &_idmapdstate.next_gid)) != 0) { 40471590c90Snw141292 idmapdlog(LOG_ERR, "unable to allocate ephemeral IDs (%s)", 40571590c90Snw141292 strerror(errno)); 406c5c4113dSnw141292 _idmapdstate.next_uid = _idmapdstate.limit_uid = SENTINEL_PID; 407c5c4113dSnw141292 _idmapdstate.next_gid = _idmapdstate.limit_gid = SENTINEL_PID; 408c5c4113dSnw141292 } else { 409c5c4113dSnw141292 _idmapdstate.limit_uid = _idmapdstate.next_uid + 8192; 410c5c4113dSnw141292 _idmapdstate.limit_gid = _idmapdstate.next_gid + 8192; 411c5c4113dSnw141292 } 412c5c4113dSnw141292 413c5c4113dSnw141292 print_idmapdstate(); 414c5c4113dSnw141292 415c5c4113dSnw141292 return; 416c5c4113dSnw141292 417c5c4113dSnw141292 errout: 418c5c4113dSnw141292 fini_idmapd(); 419c5c4113dSnw141292 exit(1); 420c5c4113dSnw141292 } 421c5c4113dSnw141292 422c5c4113dSnw141292 static void 4234edd44c5Sjp151216 fini_idmapd() 4244edd44c5Sjp151216 { 425c5c4113dSnw141292 idmap_unreg(dfd); 426c5c4113dSnw141292 fini_mapping_system(); 427c5c4113dSnw141292 if (xprt != NULL) 428c5c4113dSnw141292 svc_destroy(xprt); 429c5c4113dSnw141292 } 430c5c4113dSnw141292 43171590c90Snw141292 static 43271590c90Snw141292 const char * 43371590c90Snw141292 get_fmri(void) 43471590c90Snw141292 { 43571590c90Snw141292 static char *fmri = NULL; 43671590c90Snw141292 static char buf[60]; 43771590c90Snw141292 char *s; 43871590c90Snw141292 43971590c90Snw141292 membar_consumer(); 44071590c90Snw141292 s = fmri; 44171590c90Snw141292 if (s != NULL && *s == '\0') 44271590c90Snw141292 return (NULL); 44371590c90Snw141292 else if (s != NULL) 44471590c90Snw141292 return (s); 44571590c90Snw141292 44671590c90Snw141292 if ((s = getenv("SMF_FMRI")) == NULL || strlen(s) >= sizeof (buf)) 44771590c90Snw141292 buf[0] = '\0'; 44871590c90Snw141292 else 44971590c90Snw141292 (void) strlcpy(buf, s, sizeof (buf)); 45071590c90Snw141292 45171590c90Snw141292 membar_producer(); 45271590c90Snw141292 fmri = buf; 45371590c90Snw141292 45471590c90Snw141292 return (get_fmri()); 45571590c90Snw141292 } 45671590c90Snw141292 45771590c90Snw141292 /* 45871590c90Snw141292 * Wrappers for smf_degrade/restore_instance() 45971590c90Snw141292 * 46071590c90Snw141292 * smf_restore_instance() is too heavy duty to be calling every time we 46171590c90Snw141292 * have a successful AD name<->SID lookup. 46271590c90Snw141292 */ 46371590c90Snw141292 void 464349d5d8fSnw141292 degrade_svc(int poke_discovery, const char *reason) 46571590c90Snw141292 { 46671590c90Snw141292 const char *fmri; 46771590c90Snw141292 46871590c90Snw141292 /* 46971590c90Snw141292 * If the config update thread is in a state where auto-discovery could 47071590c90Snw141292 * be re-tried, then this will make it try it -- a sort of auto-refresh. 47171590c90Snw141292 */ 472349d5d8fSnw141292 if (poke_discovery) 47371590c90Snw141292 idmap_cfg_poke_updates(); 47471590c90Snw141292 47571590c90Snw141292 membar_consumer(); 47671590c90Snw141292 if (degraded) 47771590c90Snw141292 return; 478349d5d8fSnw141292 479349d5d8fSnw141292 idmapdlog(LOG_ERR, "Degraded operation (%s). If you are running an " 480349d5d8fSnw141292 "SMB server in workgroup mode, or if you're not running an SMB " 481349d5d8fSnw141292 "server, then you can ignore this message", reason); 482349d5d8fSnw141292 48371590c90Snw141292 membar_producer(); 48471590c90Snw141292 degraded = 1; 48571590c90Snw141292 48671590c90Snw141292 if ((fmri = get_fmri()) != NULL) 48771590c90Snw141292 (void) smf_degrade_instance(fmri, 0); 48871590c90Snw141292 } 48971590c90Snw141292 49071590c90Snw141292 void 49171590c90Snw141292 restore_svc(void) 49271590c90Snw141292 { 49371590c90Snw141292 const char *fmri; 49471590c90Snw141292 49571590c90Snw141292 membar_consumer(); 49671590c90Snw141292 if (!degraded) 49771590c90Snw141292 return; 49871590c90Snw141292 49971590c90Snw141292 if ((fmri = get_fmri()) == NULL) 50071590c90Snw141292 (void) smf_restore_instance(fmri); 50171590c90Snw141292 50271590c90Snw141292 membar_producer(); 50371590c90Snw141292 degraded = 0; 504349d5d8fSnw141292 idmapdlog(LOG_NOTICE, "Normal operation restored"); 50571590c90Snw141292 } 50671590c90Snw141292 507c5c4113dSnw141292 void 5084edd44c5Sjp151216 idmapdlog(int pri, const char *format, ...) 5094edd44c5Sjp151216 { 510c5c4113dSnw141292 va_list args; 511c5c4113dSnw141292 512c5c4113dSnw141292 va_start(args, format); 51371590c90Snw141292 51471590c90Snw141292 if (_idmapdstate.debug_mode == TRUE || 51571590c90Snw141292 _idmapdstate.daemon_mode == FALSE) { 516c5c4113dSnw141292 (void) vfprintf(stderr, format, args); 517c5c4113dSnw141292 (void) fprintf(stderr, "\n"); 518c5c4113dSnw141292 } 51971590c90Snw141292 52071590c90Snw141292 /* 52171590c90Snw141292 * We don't want to fill up the logs with useless messages when 52271590c90Snw141292 * we're degraded, but we still want to log. 52371590c90Snw141292 */ 52471590c90Snw141292 if (degraded) 52571590c90Snw141292 pri = LOG_DEBUG; 52671590c90Snw141292 527c5c4113dSnw141292 (void) vsyslog(pri, format, args); 528c5c4113dSnw141292 va_end(args); 529c5c4113dSnw141292 } 530