xref: /onnv-gate/usr/src/cmd/idmap/idmapd/idmapd.c (revision 12065:0e89d02a32ea)
14520Snw141292 /*
24520Snw141292  * CDDL HEADER START
34520Snw141292  *
44520Snw141292  * The contents of this file are subject to the terms of the
54520Snw141292  * Common Development and Distribution License (the "License").
64520Snw141292  * You may not use this file except in compliance with the License.
74520Snw141292  *
84520Snw141292  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94520Snw141292  * or http://www.opensolaris.org/os/licensing.
104520Snw141292  * See the License for the specific language governing permissions
114520Snw141292  * and limitations under the License.
124520Snw141292  *
134520Snw141292  * When distributing Covered Code, include this CDDL HEADER in each
144520Snw141292  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154520Snw141292  * If applicable, add the following below this CDDL HEADER, with the
164520Snw141292  * fields enclosed by brackets "[]" replaced with your own identifying
174520Snw141292  * information: Portions Copyright [yyyy] [name of copyright owner]
184520Snw141292  *
194520Snw141292  * CDDL HEADER END
204520Snw141292  */
214520Snw141292 /*
22*12065SKeyur.Desai@Sun.COM  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
234520Snw141292  */
244520Snw141292 
254520Snw141292 
264520Snw141292 /*
274520Snw141292  * main() of idmapd(1M)
284520Snw141292  */
294520Snw141292 
304520Snw141292 #include "idmapd.h"
316017Snw141292 #include <atomic.h>
324520Snw141292 #include <signal.h>
334520Snw141292 #include <rpc/pmap_clnt.h> /* for pmap_unset */
344520Snw141292 #include <string.h> /* strcmp */
354520Snw141292 #include <unistd.h> /* setsid */
364520Snw141292 #include <sys/types.h>
374520Snw141292 #include <memory.h>
384520Snw141292 #include <stropts.h>
394520Snw141292 #include <netconfig.h>
404520Snw141292 #include <sys/resource.h> /* rlimit */
414520Snw141292 #include <rpcsvc/daemon_utils.h> /* DAEMON_UID and DAEMON_GID */
424520Snw141292 #include <priv_utils.h> /* privileges */
434520Snw141292 #include <locale.h>
444520Snw141292 #include <sys/systeminfo.h>
454520Snw141292 #include <errno.h>
464520Snw141292 #include <sys/wait.h>
474520Snw141292 #include <sys/time.h>
484520Snw141292 #include <zone.h>
494520Snw141292 #include <door.h>
505317Sjp151216 #include <port.h>
514520Snw141292 #include <tsol/label.h>
524520Snw141292 #include <sys/resource.h>
534520Snw141292 #include <sys/sid.h>
544520Snw141292 #include <sys/idmap.h>
556531Sjp151216 #include <pthread.h>
564520Snw141292 
574520Snw141292 static void	term_handler(int);
584520Snw141292 static void	init_idmapd();
594520Snw141292 static void	fini_idmapd();
604520Snw141292 
614520Snw141292 idmapd_state_t	_idmapdstate;
624520Snw141292 
634520Snw141292 SVCXPRT *xprt = NULL;
644520Snw141292 
654520Snw141292 static int dfd = -1;		/* our door server fildes, for unregistration */
666017Snw141292 static int degraded = 0;	/* whether the FMRI has been marked degraded */
674520Snw141292 
686531Sjp151216 
696531Sjp151216 static uint32_t		num_threads = 0;
706531Sjp151216 static pthread_key_t	create_threads_key;
716531Sjp151216 static uint32_t		max_threads = 40;
726531Sjp151216 
738671SJulian.Pullen@Sun.COM 
748671SJulian.Pullen@Sun.COM /*
758671SJulian.Pullen@Sun.COM  * The following structure determines where the log messages from idmapdlog()
768671SJulian.Pullen@Sun.COM  * go to. It can be stderr (idmapd -d) and/or the real idmapdlog (idmapd).
778671SJulian.Pullen@Sun.COM  *
788671SJulian.Pullen@Sun.COM  * logstate.max_pri is integer cutoff necessary to silence low-priority
798671SJulian.Pullen@Sun.COM  * messages to stderr. idmapdlog has its own means so there a boolean
808671SJulian.Pullen@Sun.COM  * logstate.write_idmapdlog is enough.
818671SJulian.Pullen@Sun.COM  *
828671SJulian.Pullen@Sun.COM  * logstate.degraded is a mode used by idmapd in its degraded state.
838671SJulian.Pullen@Sun.COM  */
848671SJulian.Pullen@Sun.COM 
858671SJulian.Pullen@Sun.COM static struct {
868671SJulian.Pullen@Sun.COM 	boolean_t write_syslog;
878671SJulian.Pullen@Sun.COM 	int max_pri; /* Max priority written to stderr */
888671SJulian.Pullen@Sun.COM 	boolean_t degraded;
898671SJulian.Pullen@Sun.COM } logstate = {B_FALSE, LOG_DEBUG, B_FALSE};
908671SJulian.Pullen@Sun.COM 
916531Sjp151216 /*
926531Sjp151216  * Server door thread start routine.
936531Sjp151216  *
946531Sjp151216  * Set a TSD value to the door thread. This enables the destructor to
956531Sjp151216  * be called when this thread exits.
966531Sjp151216  */
976531Sjp151216 /*ARGSUSED*/
986531Sjp151216 static void *
996531Sjp151216 idmapd_door_thread_start(void *arg)
1006531Sjp151216 {
1016531Sjp151216 	static void *value = 0;
1026531Sjp151216 
1036531Sjp151216 	/*
1046531Sjp151216 	 * Disable cancellation to avoid memory leaks from not running
1056531Sjp151216 	 * the thread cleanup code.
1066531Sjp151216 	 */
1076531Sjp151216 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1086531Sjp151216 	(void) pthread_setspecific(create_threads_key, value);
1096531Sjp151216 	(void) door_return(NULL, 0, NULL, 0);
1106531Sjp151216 
1116531Sjp151216 	/* make lint happy */
1126531Sjp151216 	return (NULL);
1136531Sjp151216 }
1146531Sjp151216 
1156531Sjp151216 /*
1166531Sjp151216  * Server door threads creation
1176531Sjp151216  */
1186531Sjp151216 /*ARGSUSED*/
1196531Sjp151216 static void
1206531Sjp151216 idmapd_door_thread_create(door_info_t *dip)
1216531Sjp151216 {
1226531Sjp151216 	int		num;
1236531Sjp151216 	pthread_t	thread_id;
1246531Sjp151216 
1256531Sjp151216 	if ((num = atomic_inc_32_nv(&num_threads)) > max_threads) {
1266531Sjp151216 		atomic_dec_32(&num_threads);
1276531Sjp151216 		idmapdlog(LOG_DEBUG,
1286531Sjp151216 		    "thread creation refused - %d threads currently active",
1296531Sjp151216 		    num - 1);
1306531Sjp151216 		return;
1316531Sjp151216 	}
1326531Sjp151216 	(void) pthread_create(&thread_id, NULL, idmapd_door_thread_start, NULL);
1336531Sjp151216 	idmapdlog(LOG_DEBUG,
1346531Sjp151216 	    "created thread ID %d - %d threads currently active",
1356531Sjp151216 	    thread_id, num);
1366531Sjp151216 }
1376531Sjp151216 
1386531Sjp151216 /*
1396531Sjp151216  * Server door thread cleanup
1406531Sjp151216  */
1416531Sjp151216 /*ARGSUSED*/
1426531Sjp151216 static void
1436531Sjp151216 idmapd_door_thread_cleanup(void *arg)
1446531Sjp151216 {
1456531Sjp151216 	int num;
1466531Sjp151216 
1476531Sjp151216 	num = atomic_dec_32_nv(&num_threads);
1486531Sjp151216 	idmapdlog(LOG_DEBUG,
1496531Sjp151216 	    "exiting thread ID %d - %d threads currently active",
1506531Sjp151216 	    pthread_self(), num);
1516531Sjp151216 }
1526531Sjp151216 
1534520Snw141292 /*
1544520Snw141292  * This is needed for mech_krb5 -- we run as daemon, yes, but we want
1555034Snw141292  * mech_krb5 to think we're root so it can get host/nodename.fqdn
1565034Snw141292  * tickets for us so we can authenticate to AD as the machine account
1575034Snw141292  * that we are.  For more details look at the entry point in mech_krb5
1585034Snw141292  * corresponding to gss_init_sec_context().
1595034Snw141292  *
1605034Snw141292  * As a side effect of faking our effective UID to mech_krb5 we will use
1615034Snw141292  * root's default ccache (/tmp/krb5cc_0).  But if that's created by
1625034Snw141292  * another process then we won't have access to it: we run as daemon and
1635034Snw141292  * keep PRIV_FILE_DAC_READ, which is insufficient to share the ccache
1645034Snw141292  * with others.  We putenv("KRB5CCNAME=/var/run/idmap/ccache") in main()
1655034Snw141292  * to avoid this issue; see main().
1664520Snw141292  *
1674520Snw141292  * Someday we'll have gss/mech_krb5 extensions for acquiring initiator
1684520Snw141292  * creds with keytabs/raw keys, and someday we'll have extensions to
1694520Snw141292  * libsasl to specify creds/name to use on the initiator side, and
1704520Snw141292  * someday we'll have extensions to libldap to pass those through to
1714520Snw141292  * libsasl.  Until then this interposer will have to do.
1724520Snw141292  *
1734520Snw141292  * Also, we have to tell lint to shut up: it thinks app_krb5_user_uid()
1744520Snw141292  * is defined but not used.
1754520Snw141292  */
1764520Snw141292 /*LINTLIBRARY*/
1774520Snw141292 uid_t
1784520Snw141292 app_krb5_user_uid(void)
1794520Snw141292 {
1804520Snw141292 	return (0);
1814520Snw141292 }
1824520Snw141292 
1834520Snw141292 /*ARGSUSED*/
1844520Snw141292 static void
1855908Sjp151216 term_handler(int sig)
1865908Sjp151216 {
1876017Snw141292 	idmapdlog(LOG_INFO, "Terminating.");
1884520Snw141292 	fini_idmapd();
1894520Snw141292 	_exit(0);
1904520Snw141292 }
1914520Snw141292 
1926017Snw141292 /*ARGSUSED*/
1936017Snw141292 static void
1946017Snw141292 usr1_handler(int sig)
1956017Snw141292 {
1968671SJulian.Pullen@Sun.COM 	boolean_t saved_debug_mode = _idmapdstate.debug_mode;
1976017Snw141292 
1988671SJulian.Pullen@Sun.COM 	_idmapdstate.debug_mode = B_TRUE;
1996616Sdm199847 	idmap_log_stderr(LOG_DEBUG);
2006616Sdm199847 
2016017Snw141292 	print_idmapdstate();
2026616Sdm199847 
2036017Snw141292 	_idmapdstate.debug_mode = saved_debug_mode;
2046616Sdm199847 	idmap_log_stderr(_idmapdstate.daemon_mode ? -1 : LOG_DEBUG);
2056616Sdm199847 
2066017Snw141292 }
2076017Snw141292 
2084520Snw141292 static int pipe_fd = -1;
2094520Snw141292 
2104520Snw141292 static void
2115908Sjp151216 daemonize_ready(void)
2125908Sjp151216 {
2134520Snw141292 	char data = '\0';
2144520Snw141292 	/*
2154520Snw141292 	 * wake the parent
2164520Snw141292 	 */
2174520Snw141292 	(void) write(pipe_fd, &data, 1);
2184520Snw141292 	(void) close(pipe_fd);
2194520Snw141292 }
2204520Snw141292 
2214520Snw141292 static int
2225908Sjp151216 daemonize_start(void)
2235908Sjp151216 {
2244520Snw141292 	char	data;
2254520Snw141292 	int	status;
2264520Snw141292 	int	devnull;
2274520Snw141292 	int	filedes[2];
2284520Snw141292 	pid_t	pid;
2294520Snw141292 
2305034Snw141292 	(void) sigset(SIGPIPE, SIG_IGN);
2314520Snw141292 	devnull = open("/dev/null", O_RDONLY);
2324520Snw141292 	if (devnull < 0)
2334520Snw141292 		return (-1);
2344520Snw141292 	(void) dup2(devnull, 0);
2354520Snw141292 	(void) dup2(2, 1);	/* stderr only */
2364520Snw141292 	if (pipe(filedes) < 0)
2374520Snw141292 		return (-1);
2384520Snw141292 	if ((pid = fork1()) < 0)
2394520Snw141292 		return (-1);
2404520Snw141292 	if (pid != 0) {
2414520Snw141292 		/*
2424520Snw141292 		 * parent
2434520Snw141292 		 */
2444520Snw141292 		(void) close(filedes[1]);
2454520Snw141292 		if (read(filedes[0], &data, 1) == 1) {
2464520Snw141292 			/* presume success */
2474520Snw141292 			_exit(0);
2484520Snw141292 		}
2494520Snw141292 		status = -1;
2504520Snw141292 		(void) wait4(pid, &status, 0, NULL);
2514520Snw141292 		if (WIFEXITED(status))
2524520Snw141292 			_exit(WEXITSTATUS(status));
2534520Snw141292 		else
2544520Snw141292 			_exit(-1);
2554520Snw141292 	}
2564520Snw141292 
2574520Snw141292 	/*
2584520Snw141292 	 * child
2594520Snw141292 	 */
2604520Snw141292 	pipe_fd = filedes[1];
2614520Snw141292 	(void) close(filedes[0]);
2624520Snw141292 	(void) setsid();
2634520Snw141292 	(void) umask(0077);
2644520Snw141292 	openlog("idmap", LOG_PID, LOG_DAEMON);
2656616Sdm199847 
2664520Snw141292 	return (0);
2674520Snw141292 }
2684520Snw141292 
2694520Snw141292 
2704520Snw141292 int
2714520Snw141292 main(int argc, char **argv)
2724520Snw141292 {
2734520Snw141292 	int c;
2746414Sjp151216 	struct rlimit rl;
2754520Snw141292 
2766017Snw141292 	_idmapdstate.daemon_mode = TRUE;
2776017Snw141292 	_idmapdstate.debug_mode = FALSE;
2786017Snw141292 	while ((c = getopt(argc, argv, "d")) != -1) {
2794520Snw141292 		switch (c) {
2804520Snw141292 			case 'd':
2816017Snw141292 				_idmapdstate.daemon_mode = FALSE;
2824520Snw141292 				break;
2834520Snw141292 			default:
28410966SJordan.Brown@Sun.COM 				(void) fprintf(stderr,
28510966SJordan.Brown@Sun.COM 				    "Usage: /usr/lib/idmapd [-d]\n");
2866017Snw141292 				return (SMF_EXIT_ERR_CONFIG);
2874520Snw141292 		}
2884520Snw141292 	}
2894520Snw141292 
2904520Snw141292 	/* set locale and domain for internationalization */
2914520Snw141292 	(void) setlocale(LC_ALL, "");
2924520Snw141292 	(void) textdomain(TEXT_DOMAIN);
2934520Snw141292 
2948671SJulian.Pullen@Sun.COM 	idmap_set_logger(idmapdlog);
295*12065SKeyur.Desai@Sun.COM 	adutils_set_logger(idmapdlog);
2968671SJulian.Pullen@Sun.COM 	idmap_log_syslog(B_TRUE);
2976616Sdm199847 	idmap_log_stderr(_idmapdstate.daemon_mode ? -1 : LOG_DEBUG);
2986616Sdm199847 
2995771Sjp151216 	if (is_system_labeled() && getzoneid() != GLOBAL_ZONEID) {
3006017Snw141292 		idmapdlog(LOG_ERR,
3016017Snw141292 		    "with Trusted Extensions idmapd runs only in the "
3025771Sjp151216 		    "global zone");
3034520Snw141292 		exit(1);
3044520Snw141292 	}
3054520Snw141292 
3066414Sjp151216 	/*
3076414Sjp151216 	 * Raise the fd limit to max
3086414Sjp151216 	 */
3096414Sjp151216 	if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
3106414Sjp151216 		idmapdlog(LOG_ERR, "getrlimit failed");
3116414Sjp151216 	} else if (rl.rlim_cur < rl.rlim_max) {
3126414Sjp151216 		rl.rlim_cur = rl.rlim_max;
3136414Sjp151216 		if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
3146414Sjp151216 			idmapdlog(LOG_ERR,
3156414Sjp151216 			    "Unable to raise RLIMIT_NOFILE to %d",
3166414Sjp151216 			    rl.rlim_cur);
3176414Sjp151216 	}
3186414Sjp151216 
3194520Snw141292 	(void) mutex_init(&_svcstate_lock, USYNC_THREAD, NULL);
3204520Snw141292 
3216017Snw141292 	if (_idmapdstate.daemon_mode == TRUE) {
3224520Snw141292 		if (daemonize_start() < 0) {
3238671SJulian.Pullen@Sun.COM 			idmapdlog(LOG_ERR, "unable to daemonize");
3244520Snw141292 			exit(-1);
3254520Snw141292 		}
3264520Snw141292 	} else
3274520Snw141292 		(void) umask(0077);
3284520Snw141292 
3294884Sjp151216 	idmap_init_tsd_key();
3304884Sjp151216 
3314520Snw141292 	init_idmapd();
3324520Snw141292 
3335034Snw141292 	/* signal handlers that should run only after we're initialized */
3345034Snw141292 	(void) sigset(SIGTERM, term_handler);
3356017Snw141292 	(void) sigset(SIGUSR1, usr1_handler);
3365968Snw141292 	(void) sigset(SIGHUP, idmap_cfg_hup_handler);
3375034Snw141292 
3384520Snw141292 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
3394520Snw141292 	    DAEMON_UID, DAEMON_GID,
3404520Snw141292 	    PRIV_PROC_AUDIT, PRIV_FILE_DAC_READ,
3414520Snw141292 	    (char *)NULL) == -1) {
3426017Snw141292 		idmapdlog(LOG_ERR, "unable to drop privileges");
3434520Snw141292 		exit(1);
3444520Snw141292 	}
3454520Snw141292 
3464520Snw141292 	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
3474520Snw141292 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
3484520Snw141292 
3496017Snw141292 	if (_idmapdstate.daemon_mode == TRUE)
3504520Snw141292 		daemonize_ready();
3514520Snw141292 
3524520Snw141292 	/* With doors RPC this just wastes this thread, oh well */
3534520Snw141292 	svc_run();
3544520Snw141292 	return (0);
3554520Snw141292 }
3564520Snw141292 
3574520Snw141292 static void
3585908Sjp151216 init_idmapd()
3595908Sjp151216 {
3604520Snw141292 	int	error;
3616531Sjp151216 	int	connmaxrec = IDMAP_MAX_DOOR_RPC;
3626531Sjp151216 
3634520Snw141292 
3645034Snw141292 	/* create directories as root and chown to daemon uid */
3655034Snw141292 	if (create_directory(IDMAP_DBDIR, DAEMON_UID, DAEMON_GID) < 0)
3665034Snw141292 		exit(1);
3675034Snw141292 	if (create_directory(IDMAP_CACHEDIR, DAEMON_UID, DAEMON_GID) < 0)
3685034Snw141292 		exit(1);
3695034Snw141292 
3705034Snw141292 	/*
3715034Snw141292 	 * Set KRB5CCNAME in the environment.  See app_krb5_user_uid()
3725447Snw141292 	 * for more details.  We blow away the existing one, if there is
3735447Snw141292 	 * one.
3745034Snw141292 	 */
3755447Snw141292 	(void) unlink(IDMAP_CACHEDIR "/ccache");
3765034Snw141292 	putenv("KRB5CCNAME=" IDMAP_CACHEDIR "/ccache");
3775034Snw141292 
3784520Snw141292 	if (sysinfo(SI_HOSTNAME, _idmapdstate.hostname,
3795908Sjp151216 	    sizeof (_idmapdstate.hostname)) == -1) {
3804520Snw141292 		error = errno;
3816017Snw141292 		idmapdlog(LOG_ERR, "unable to determine hostname, error: %d",
3825908Sjp151216 		    error);
3834520Snw141292 		exit(1);
3844520Snw141292 	}
3854520Snw141292 
3865731Sbaban 	if ((error = init_mapping_system()) < 0) {
3876017Snw141292 		idmapdlog(LOG_ERR, "unable to initialize mapping system");
3885731Sbaban 		exit(error < -2 ? SMF_EXIT_ERR_CONFIG : 1);
3894520Snw141292 	}
3904520Snw141292 
3916531Sjp151216 	(void) door_server_create(idmapd_door_thread_create);
3926531Sjp151216 	if ((error = pthread_key_create(&create_threads_key,
3936531Sjp151216 	    idmapd_door_thread_cleanup)) != 0) {
3946531Sjp151216 		idmapdlog(LOG_ERR, "unable to create threads key (%s)",
3956531Sjp151216 		    strerror(error));
3966531Sjp151216 		goto errout;
3976531Sjp151216 	}
3986531Sjp151216 
3995232Snw141292 	xprt = svc_door_create(idmap_prog_1, IDMAP_PROG, IDMAP_V1, connmaxrec);
4004520Snw141292 	if (xprt == NULL) {
4016017Snw141292 		idmapdlog(LOG_ERR, "unable to create door RPC service");
4024520Snw141292 		goto errout;
4034520Snw141292 	}
4044520Snw141292 
4055064Sdm199847 	if (!svc_control(xprt, SVCSET_CONNMAXREC, &connmaxrec)) {
4066017Snw141292 		idmapdlog(LOG_ERR, "unable to limit RPC request size");
4075064Sdm199847 		goto errout;
4085064Sdm199847 	}
4095064Sdm199847 
4104520Snw141292 	dfd = xprt->xp_fd;
4114520Snw141292 
4124520Snw141292 	if (dfd == -1) {
4136017Snw141292 		idmapdlog(LOG_ERR, "unable to register door");
4144520Snw141292 		goto errout;
4154520Snw141292 	}
41611963SAfshin.Ardakani@Sun.COM 	if ((error = __idmap_reg(dfd)) != 0) {
4176017Snw141292 		idmapdlog(LOG_ERR, "unable to register door (%s)",
4185908Sjp151216 		    strerror(errno));
4194520Snw141292 		goto errout;
4204520Snw141292 	}
4214520Snw141292 
4224520Snw141292 	if ((error = allocids(_idmapdstate.new_eph_db,
4235908Sjp151216 	    8192, &_idmapdstate.next_uid,
4245908Sjp151216 	    8192, &_idmapdstate.next_gid)) != 0) {
4256017Snw141292 		idmapdlog(LOG_ERR, "unable to allocate ephemeral IDs (%s)",
4266017Snw141292 		    strerror(errno));
42711963SAfshin.Ardakani@Sun.COM 		_idmapdstate.next_uid = IDMAP_SENTINEL_PID;
42811963SAfshin.Ardakani@Sun.COM 		_idmapdstate.limit_uid = IDMAP_SENTINEL_PID;
42911963SAfshin.Ardakani@Sun.COM 		_idmapdstate.next_gid = IDMAP_SENTINEL_PID;
43011963SAfshin.Ardakani@Sun.COM 		_idmapdstate.limit_gid = IDMAP_SENTINEL_PID;
4314520Snw141292 	} else {
4324520Snw141292 		_idmapdstate.limit_uid = _idmapdstate.next_uid + 8192;
4334520Snw141292 		_idmapdstate.limit_gid = _idmapdstate.next_gid + 8192;
4344520Snw141292 	}
4354520Snw141292 
4364520Snw141292 	print_idmapdstate();
4374520Snw141292 
4384520Snw141292 	return;
4394520Snw141292 
4404520Snw141292 errout:
4414520Snw141292 	fini_idmapd();
4424520Snw141292 	exit(1);
4434520Snw141292 }
4444520Snw141292 
4454520Snw141292 static void
4465908Sjp151216 fini_idmapd()
4475908Sjp151216 {
44811963SAfshin.Ardakani@Sun.COM 	__idmap_unreg(dfd);
4494520Snw141292 	fini_mapping_system();
4504520Snw141292 	if (xprt != NULL)
4514520Snw141292 		svc_destroy(xprt);
4524520Snw141292 }
4534520Snw141292 
4546017Snw141292 static
4556017Snw141292 const char *
4566017Snw141292 get_fmri(void)
4576017Snw141292 {
4586017Snw141292 	static char *fmri = NULL;
4596017Snw141292 	static char buf[60];
4606017Snw141292 	char *s;
4616017Snw141292 
4626017Snw141292 	membar_consumer();
4636017Snw141292 	s = fmri;
4646017Snw141292 	if (s != NULL && *s == '\0')
4656017Snw141292 		return (NULL);
4666017Snw141292 	else if (s != NULL)
4676017Snw141292 		return (s);
4686017Snw141292 
4696017Snw141292 	if ((s = getenv("SMF_FMRI")) == NULL || strlen(s) >= sizeof (buf))
4706017Snw141292 		buf[0] = '\0';
4716017Snw141292 	else
4726017Snw141292 		(void) strlcpy(buf, s, sizeof (buf));
4736017Snw141292 
4746017Snw141292 	membar_producer();
4756017Snw141292 	fmri = buf;
4766017Snw141292 
4776017Snw141292 	return (get_fmri());
4786017Snw141292 }
4796017Snw141292 
4806017Snw141292 /*
4816017Snw141292  * Wrappers for smf_degrade/restore_instance()
4826017Snw141292  *
4836017Snw141292  * smf_restore_instance() is too heavy duty to be calling every time we
4846017Snw141292  * have a successful AD name<->SID lookup.
4856017Snw141292  */
4866017Snw141292 void
4876097Snw141292 degrade_svc(int poke_discovery, const char *reason)
4886017Snw141292 {
4896017Snw141292 	const char *fmri;
4906017Snw141292 
4916017Snw141292 	membar_consumer();
4926017Snw141292 	if (degraded)
4936017Snw141292 		return;
4946097Snw141292 
4956097Snw141292 	idmapdlog(LOG_ERR, "Degraded operation (%s).  If you are running an "
4966097Snw141292 	    "SMB server in workgroup mode, or if you're not running an SMB "
4976097Snw141292 	    "server, then you can ignore this message", reason);
4986097Snw141292 
4996017Snw141292 	membar_producer();
5006017Snw141292 	degraded = 1;
5018671SJulian.Pullen@Sun.COM 	idmap_log_degraded(B_TRUE);
5026017Snw141292 
5036017Snw141292 	if ((fmri = get_fmri()) != NULL)
5046017Snw141292 		(void) smf_degrade_instance(fmri, 0);
505*12065SKeyur.Desai@Sun.COM 
506*12065SKeyur.Desai@Sun.COM 	/*
507*12065SKeyur.Desai@Sun.COM 	 * If the config update thread is in a state where auto-discovery could
508*12065SKeyur.Desai@Sun.COM 	 * be re-tried, then this will make it try it -- a sort of auto-refresh.
509*12065SKeyur.Desai@Sun.COM 	 */
510*12065SKeyur.Desai@Sun.COM 	if (poke_discovery)
511*12065SKeyur.Desai@Sun.COM 		idmap_cfg_poke_updates();
5126017Snw141292 }
5136017Snw141292 
5146017Snw141292 void
5156017Snw141292 restore_svc(void)
5166017Snw141292 {
5176017Snw141292 	const char *fmri;
5186017Snw141292 
5196017Snw141292 	membar_consumer();
5206017Snw141292 	if (!degraded)
5216017Snw141292 		return;
5226017Snw141292 
5236017Snw141292 	if ((fmri = get_fmri()) == NULL)
5246017Snw141292 		(void) smf_restore_instance(fmri);
5256017Snw141292 
5266017Snw141292 	membar_producer();
5276017Snw141292 	degraded = 0;
5288671SJulian.Pullen@Sun.COM 	idmap_log_degraded(B_FALSE);
5296616Sdm199847 
5306097Snw141292 	idmapdlog(LOG_NOTICE, "Normal operation restored");
5316017Snw141292 }
5326017Snw141292 
5338671SJulian.Pullen@Sun.COM 
5348671SJulian.Pullen@Sun.COM /* printflike */
5354520Snw141292 void
5368671SJulian.Pullen@Sun.COM idmapdlog(int pri, const char *format, ...) {
5374520Snw141292 	va_list args;
5384520Snw141292 
5398671SJulian.Pullen@Sun.COM 	if (pri <= logstate.max_pri) {
5408671SJulian.Pullen@Sun.COM 		va_start(args, format);
5414520Snw141292 		(void) vfprintf(stderr, format, args);
5424520Snw141292 		(void) fprintf(stderr, "\n");
5438671SJulian.Pullen@Sun.COM 		va_end(args);
5444520Snw141292 	}
5456017Snw141292 
5466017Snw141292 	/*
5476017Snw141292 	 * We don't want to fill up the logs with useless messages when
5486017Snw141292 	 * we're degraded, but we still want to log.
5496017Snw141292 	 */
5508671SJulian.Pullen@Sun.COM 	if (logstate.degraded)
5516017Snw141292 		pri = LOG_DEBUG;
5526017Snw141292 
5538671SJulian.Pullen@Sun.COM 	if (logstate.write_syslog) {
5548671SJulian.Pullen@Sun.COM 		va_start(args, format);
5558671SJulian.Pullen@Sun.COM 		vsyslog(pri, format, args);
5568671SJulian.Pullen@Sun.COM 		va_end(args);
5578671SJulian.Pullen@Sun.COM 	}
5588671SJulian.Pullen@Sun.COM }
5598671SJulian.Pullen@Sun.COM 
5608671SJulian.Pullen@Sun.COM void
5618671SJulian.Pullen@Sun.COM idmap_log_stderr(int pri)
5628671SJulian.Pullen@Sun.COM {
5638671SJulian.Pullen@Sun.COM 	logstate.max_pri = pri;
5644520Snw141292 }
5658671SJulian.Pullen@Sun.COM 
5668671SJulian.Pullen@Sun.COM void
5678671SJulian.Pullen@Sun.COM idmap_log_syslog(boolean_t what)
5688671SJulian.Pullen@Sun.COM {
5698671SJulian.Pullen@Sun.COM 	logstate.write_syslog = what;
5708671SJulian.Pullen@Sun.COM }
5718671SJulian.Pullen@Sun.COM 
5728671SJulian.Pullen@Sun.COM void
5738671SJulian.Pullen@Sun.COM idmap_log_degraded(boolean_t what)
5748671SJulian.Pullen@Sun.COM {
5758671SJulian.Pullen@Sun.COM 	logstate.degraded = what;
5768671SJulian.Pullen@Sun.COM }
577