xref: /titanic_53/usr/src/cmd/idmap/idmapd/idmapd.c (revision 479ac37569625bae44ffb80071d4bc865fc710ed)
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>
58bcced03bSjp151216 #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 
76bcced03bSjp151216 
77bcced03bSjp151216 static uint32_t		num_threads = 0;
78bcced03bSjp151216 static pthread_key_t	create_threads_key;
79bcced03bSjp151216 static uint32_t		max_threads = 40;
80bcced03bSjp151216 
81bcced03bSjp151216 /*
82bcced03bSjp151216  * Server door thread start routine.
83bcced03bSjp151216  *
84bcced03bSjp151216  * Set a TSD value to the door thread. This enables the destructor to
85bcced03bSjp151216  * be called when this thread exits.
86bcced03bSjp151216  */
87bcced03bSjp151216 /*ARGSUSED*/
88bcced03bSjp151216 static void *
89bcced03bSjp151216 idmapd_door_thread_start(void *arg)
90bcced03bSjp151216 {
91bcced03bSjp151216 	static void *value = 0;
92bcced03bSjp151216 
93bcced03bSjp151216 	/*
94bcced03bSjp151216 	 * Disable cancellation to avoid memory leaks from not running
95bcced03bSjp151216 	 * the thread cleanup code.
96bcced03bSjp151216 	 */
97bcced03bSjp151216 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
98bcced03bSjp151216 	(void) pthread_setspecific(create_threads_key, value);
99bcced03bSjp151216 	(void) door_return(NULL, 0, NULL, 0);
100bcced03bSjp151216 
101bcced03bSjp151216 	/* make lint happy */
102bcced03bSjp151216 	return (NULL);
103bcced03bSjp151216 }
104bcced03bSjp151216 
105bcced03bSjp151216 /*
106bcced03bSjp151216  * Server door threads creation
107bcced03bSjp151216  */
108bcced03bSjp151216 /*ARGSUSED*/
109bcced03bSjp151216 static void
110bcced03bSjp151216 idmapd_door_thread_create(door_info_t *dip)
111bcced03bSjp151216 {
112bcced03bSjp151216 	int		num;
113bcced03bSjp151216 	pthread_t	thread_id;
114bcced03bSjp151216 
115bcced03bSjp151216 	if ((num = atomic_inc_32_nv(&num_threads)) > max_threads) {
116bcced03bSjp151216 		atomic_dec_32(&num_threads);
117bcced03bSjp151216 		idmapdlog(LOG_DEBUG,
118bcced03bSjp151216 		    "thread creation refused - %d threads currently active",
119bcced03bSjp151216 		    num - 1);
120bcced03bSjp151216 		return;
121bcced03bSjp151216 	}
122bcced03bSjp151216 	(void) pthread_create(&thread_id, NULL, idmapd_door_thread_start, NULL);
123bcced03bSjp151216 	idmapdlog(LOG_DEBUG,
124bcced03bSjp151216 	    "created thread ID %d - %d threads currently active",
125bcced03bSjp151216 	    thread_id, num);
126bcced03bSjp151216 }
127bcced03bSjp151216 
128bcced03bSjp151216 /*
129bcced03bSjp151216  * Server door thread cleanup
130bcced03bSjp151216  */
131bcced03bSjp151216 /*ARGSUSED*/
132bcced03bSjp151216 static void
133bcced03bSjp151216 idmapd_door_thread_cleanup(void *arg)
134bcced03bSjp151216 {
135bcced03bSjp151216 	int num;
136bcced03bSjp151216 
137bcced03bSjp151216 	num = atomic_dec_32_nv(&num_threads);
138bcced03bSjp151216 	idmapdlog(LOG_DEBUG,
139bcced03bSjp151216 	    "exiting thread ID %d - %d threads currently active",
140bcced03bSjp151216 	    pthread_self(), num);
141bcced03bSjp151216 }
142bcced03bSjp151216 
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;
189*479ac375Sdm199847 	idmap_log_stderr(LOG_DEBUG);
190*479ac375Sdm199847 
19171590c90Snw141292 	print_idmapdstate();
192*479ac375Sdm199847 
19371590c90Snw141292 	_idmapdstate.debug_mode = saved_debug_mode;
194*479ac375Sdm199847 	idmap_log_stderr(_idmapdstate.daemon_mode ? -1 : LOG_DEBUG);
195*479ac375Sdm199847 
19671590c90Snw141292 }
19771590c90Snw141292 
198c5c4113dSnw141292 static int pipe_fd = -1;
199c5c4113dSnw141292 
200c5c4113dSnw141292 static void
2014edd44c5Sjp151216 daemonize_ready(void)
2024edd44c5Sjp151216 {
203c5c4113dSnw141292 	char data = '\0';
204c5c4113dSnw141292 	/*
205c5c4113dSnw141292 	 * wake the parent
206c5c4113dSnw141292 	 */
207c5c4113dSnw141292 	(void) write(pipe_fd, &data, 1);
208c5c4113dSnw141292 	(void) close(pipe_fd);
209c5c4113dSnw141292 }
210c5c4113dSnw141292 
211c5c4113dSnw141292 static int
2124edd44c5Sjp151216 daemonize_start(void)
2134edd44c5Sjp151216 {
214c5c4113dSnw141292 	char	data;
215c5c4113dSnw141292 	int	status;
216c5c4113dSnw141292 	int	devnull;
217c5c4113dSnw141292 	int	filedes[2];
218c5c4113dSnw141292 	pid_t	pid;
219c5c4113dSnw141292 
22078b2cb9aSnw141292 	(void) sigset(SIGPIPE, SIG_IGN);
221c5c4113dSnw141292 	devnull = open("/dev/null", O_RDONLY);
222c5c4113dSnw141292 	if (devnull < 0)
223c5c4113dSnw141292 		return (-1);
224c5c4113dSnw141292 	(void) dup2(devnull, 0);
225c5c4113dSnw141292 	(void) dup2(2, 1);	/* stderr only */
226c5c4113dSnw141292 	if (pipe(filedes) < 0)
227c5c4113dSnw141292 		return (-1);
228c5c4113dSnw141292 	if ((pid = fork1()) < 0)
229c5c4113dSnw141292 		return (-1);
230c5c4113dSnw141292 	if (pid != 0) {
231c5c4113dSnw141292 		/*
232c5c4113dSnw141292 		 * parent
233c5c4113dSnw141292 		 */
234c5c4113dSnw141292 		(void) close(filedes[1]);
235c5c4113dSnw141292 		if (read(filedes[0], &data, 1) == 1) {
236c5c4113dSnw141292 			/* presume success */
237c5c4113dSnw141292 			_exit(0);
238c5c4113dSnw141292 		}
239c5c4113dSnw141292 		status = -1;
240c5c4113dSnw141292 		(void) wait4(pid, &status, 0, NULL);
241c5c4113dSnw141292 		if (WIFEXITED(status))
242c5c4113dSnw141292 			_exit(WEXITSTATUS(status));
243c5c4113dSnw141292 		else
244c5c4113dSnw141292 			_exit(-1);
245c5c4113dSnw141292 	}
246c5c4113dSnw141292 
247c5c4113dSnw141292 	/*
248c5c4113dSnw141292 	 * child
249c5c4113dSnw141292 	 */
250c5c4113dSnw141292 	pipe_fd = filedes[1];
251c5c4113dSnw141292 	(void) close(filedes[0]);
252c5c4113dSnw141292 	(void) setsid();
253c5c4113dSnw141292 	(void) umask(0077);
254c5c4113dSnw141292 	openlog("idmap", LOG_PID, LOG_DAEMON);
255*479ac375Sdm199847 
256c5c4113dSnw141292 	return (0);
257c5c4113dSnw141292 }
258c5c4113dSnw141292 
259c5c4113dSnw141292 
260c5c4113dSnw141292 int
261c5c4113dSnw141292 main(int argc, char **argv)
262c5c4113dSnw141292 {
263c5c4113dSnw141292 	int c;
2642b3ecdebSjp151216 	struct rlimit rl;
265c5c4113dSnw141292 
26671590c90Snw141292 	_idmapdstate.daemon_mode = TRUE;
26771590c90Snw141292 	_idmapdstate.debug_mode = FALSE;
26871590c90Snw141292 	while ((c = getopt(argc, argv, "d")) != -1) {
269c5c4113dSnw141292 		switch (c) {
270c5c4113dSnw141292 			case 'd':
27171590c90Snw141292 				_idmapdstate.daemon_mode = FALSE;
272c5c4113dSnw141292 				break;
273c5c4113dSnw141292 			default:
27471590c90Snw141292 				fprintf(stderr, "Usage: /usr/lib/idmapd");
27571590c90Snw141292 				return (SMF_EXIT_ERR_CONFIG);
276c5c4113dSnw141292 				break;
277c5c4113dSnw141292 		}
278c5c4113dSnw141292 	}
279c5c4113dSnw141292 
280c5c4113dSnw141292 	/* set locale and domain for internationalization */
281c5c4113dSnw141292 	(void) setlocale(LC_ALL, "");
282c5c4113dSnw141292 	(void) textdomain(TEXT_DOMAIN);
283c5c4113dSnw141292 
284*479ac375Sdm199847 	idmap_log_syslog(TRUE);
285*479ac375Sdm199847 	idmap_log_stderr(_idmapdstate.daemon_mode ? -1 : LOG_DEBUG);
286*479ac375Sdm199847 
287bda89588Sjp151216 	if (is_system_labeled() && getzoneid() != GLOBAL_ZONEID) {
28871590c90Snw141292 		idmapdlog(LOG_ERR,
28971590c90Snw141292 		    "with Trusted Extensions idmapd runs only in the "
290bda89588Sjp151216 		    "global zone");
291c5c4113dSnw141292 		exit(1);
292c5c4113dSnw141292 	}
293c5c4113dSnw141292 
2942b3ecdebSjp151216 	/*
2952b3ecdebSjp151216 	 * Raise the fd limit to max
2962b3ecdebSjp151216 	 */
2972b3ecdebSjp151216 	if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
2982b3ecdebSjp151216 		idmapdlog(LOG_ERR, "getrlimit failed");
2992b3ecdebSjp151216 	} else if (rl.rlim_cur < rl.rlim_max) {
3002b3ecdebSjp151216 		rl.rlim_cur = rl.rlim_max;
3012b3ecdebSjp151216 		if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
3022b3ecdebSjp151216 			idmapdlog(LOG_ERR,
3032b3ecdebSjp151216 			    "Unable to raise RLIMIT_NOFILE to %d",
3042b3ecdebSjp151216 			    rl.rlim_cur);
3052b3ecdebSjp151216 	}
3062b3ecdebSjp151216 
307c5c4113dSnw141292 	(void) mutex_init(&_svcstate_lock, USYNC_THREAD, NULL);
308c5c4113dSnw141292 
30971590c90Snw141292 	if (_idmapdstate.daemon_mode == TRUE) {
310c5c4113dSnw141292 		if (daemonize_start() < 0) {
31171590c90Snw141292 			(void) idmapdlog(LOG_ERR, "unable to daemonize");
312c5c4113dSnw141292 			exit(-1);
313c5c4113dSnw141292 		}
314c5c4113dSnw141292 	} else
315c5c4113dSnw141292 		(void) umask(0077);
316c5c4113dSnw141292 
31784decf41Sjp151216 	idmap_init_tsd_key();
31884decf41Sjp151216 
319c5c4113dSnw141292 	init_idmapd();
320c5c4113dSnw141292 
32178b2cb9aSnw141292 	/* signal handlers that should run only after we're initialized */
32278b2cb9aSnw141292 	(void) sigset(SIGTERM, term_handler);
32371590c90Snw141292 	(void) sigset(SIGUSR1, usr1_handler);
3240dcc7149Snw141292 	(void) sigset(SIGHUP, idmap_cfg_hup_handler);
32578b2cb9aSnw141292 
326c5c4113dSnw141292 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
327c5c4113dSnw141292 	    DAEMON_UID, DAEMON_GID,
328c5c4113dSnw141292 	    PRIV_PROC_AUDIT, PRIV_FILE_DAC_READ,
329c5c4113dSnw141292 	    (char *)NULL) == -1) {
33071590c90Snw141292 		idmapdlog(LOG_ERR, "unable to drop privileges");
331c5c4113dSnw141292 		exit(1);
332c5c4113dSnw141292 	}
333c5c4113dSnw141292 
334c5c4113dSnw141292 	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
335c5c4113dSnw141292 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
336c5c4113dSnw141292 
33771590c90Snw141292 	if (_idmapdstate.daemon_mode == TRUE)
338c5c4113dSnw141292 		daemonize_ready();
339c5c4113dSnw141292 
340c5c4113dSnw141292 	/* With doors RPC this just wastes this thread, oh well */
341c5c4113dSnw141292 	svc_run();
342c5c4113dSnw141292 	return (0);
343c5c4113dSnw141292 }
344c5c4113dSnw141292 
345c5c4113dSnw141292 static void
3464edd44c5Sjp151216 init_idmapd()
3474edd44c5Sjp151216 {
348c5c4113dSnw141292 	int	error;
349d3a612caSnw141292 	int	connmaxrec = IDMAP_MAX_DOOR_RPC;
350c5c4113dSnw141292 
351bcced03bSjp151216 
35278b2cb9aSnw141292 	/* create directories as root and chown to daemon uid */
35378b2cb9aSnw141292 	if (create_directory(IDMAP_DBDIR, DAEMON_UID, DAEMON_GID) < 0)
35478b2cb9aSnw141292 		exit(1);
35578b2cb9aSnw141292 	if (create_directory(IDMAP_CACHEDIR, DAEMON_UID, DAEMON_GID) < 0)
35678b2cb9aSnw141292 		exit(1);
35778b2cb9aSnw141292 
35878b2cb9aSnw141292 	/*
35978b2cb9aSnw141292 	 * Set KRB5CCNAME in the environment.  See app_krb5_user_uid()
360e3c2d6aaSnw141292 	 * for more details.  We blow away the existing one, if there is
361e3c2d6aaSnw141292 	 * one.
36278b2cb9aSnw141292 	 */
363e3c2d6aaSnw141292 	(void) unlink(IDMAP_CACHEDIR "/ccache");
36478b2cb9aSnw141292 	putenv("KRB5CCNAME=" IDMAP_CACHEDIR "/ccache");
36578b2cb9aSnw141292 
366c5c4113dSnw141292 	if (sysinfo(SI_HOSTNAME, _idmapdstate.hostname,
367c5c4113dSnw141292 	    sizeof (_idmapdstate.hostname)) == -1) {
368c5c4113dSnw141292 		error = errno;
36971590c90Snw141292 		idmapdlog(LOG_ERR, "unable to determine hostname, error: %d",
370c5c4113dSnw141292 		    error);
371c5c4113dSnw141292 		exit(1);
372c5c4113dSnw141292 	}
373c5c4113dSnw141292 
374e8c27ec8Sbaban 	if ((error = init_mapping_system()) < 0) {
37571590c90Snw141292 		idmapdlog(LOG_ERR, "unable to initialize mapping system");
376e8c27ec8Sbaban 		exit(error < -2 ? SMF_EXIT_ERR_CONFIG : 1);
377c5c4113dSnw141292 	}
378c5c4113dSnw141292 
379bcced03bSjp151216 	(void) door_server_create(idmapd_door_thread_create);
380bcced03bSjp151216 	if ((error = pthread_key_create(&create_threads_key,
381bcced03bSjp151216 	    idmapd_door_thread_cleanup)) != 0) {
382bcced03bSjp151216 		idmapdlog(LOG_ERR, "unable to create threads key (%s)",
383bcced03bSjp151216 		    strerror(error));
384bcced03bSjp151216 		goto errout;
385bcced03bSjp151216 	}
386bcced03bSjp151216 
387d3a612caSnw141292 	xprt = svc_door_create(idmap_prog_1, IDMAP_PROG, IDMAP_V1, connmaxrec);
388c5c4113dSnw141292 	if (xprt == NULL) {
38971590c90Snw141292 		idmapdlog(LOG_ERR, "unable to create door RPC service");
390c5c4113dSnw141292 		goto errout;
391c5c4113dSnw141292 	}
392c5c4113dSnw141292 
3938e228215Sdm199847 	if (!svc_control(xprt, SVCSET_CONNMAXREC, &connmaxrec)) {
39471590c90Snw141292 		idmapdlog(LOG_ERR, "unable to limit RPC request size");
3958e228215Sdm199847 		goto errout;
3968e228215Sdm199847 	}
3978e228215Sdm199847 
398c5c4113dSnw141292 	dfd = xprt->xp_fd;
399c5c4113dSnw141292 
400c5c4113dSnw141292 	if (dfd == -1) {
40171590c90Snw141292 		idmapdlog(LOG_ERR, "unable to register door");
402c5c4113dSnw141292 		goto errout;
403c5c4113dSnw141292 	}
404c5c4113dSnw141292 	if ((error = idmap_reg(dfd)) != 0) {
40571590c90Snw141292 		idmapdlog(LOG_ERR, "unable to register door (%s)",
406bda89588Sjp151216 		    strerror(errno));
407c5c4113dSnw141292 		goto errout;
408c5c4113dSnw141292 	}
409c5c4113dSnw141292 
410c5c4113dSnw141292 	if ((error = allocids(_idmapdstate.new_eph_db,
411c5c4113dSnw141292 	    8192, &_idmapdstate.next_uid,
412c5c4113dSnw141292 	    8192, &_idmapdstate.next_gid)) != 0) {
41371590c90Snw141292 		idmapdlog(LOG_ERR, "unable to allocate ephemeral IDs (%s)",
41471590c90Snw141292 		    strerror(errno));
415c5c4113dSnw141292 		_idmapdstate.next_uid = _idmapdstate.limit_uid = SENTINEL_PID;
416c5c4113dSnw141292 		_idmapdstate.next_gid = _idmapdstate.limit_gid = SENTINEL_PID;
417c5c4113dSnw141292 	} else {
418c5c4113dSnw141292 		_idmapdstate.limit_uid = _idmapdstate.next_uid + 8192;
419c5c4113dSnw141292 		_idmapdstate.limit_gid = _idmapdstate.next_gid + 8192;
420c5c4113dSnw141292 	}
421c5c4113dSnw141292 
422c5c4113dSnw141292 	print_idmapdstate();
423c5c4113dSnw141292 
424c5c4113dSnw141292 	return;
425c5c4113dSnw141292 
426c5c4113dSnw141292 errout:
427c5c4113dSnw141292 	fini_idmapd();
428c5c4113dSnw141292 	exit(1);
429c5c4113dSnw141292 }
430c5c4113dSnw141292 
431c5c4113dSnw141292 static void
4324edd44c5Sjp151216 fini_idmapd()
4334edd44c5Sjp151216 {
434c5c4113dSnw141292 	idmap_unreg(dfd);
435c5c4113dSnw141292 	fini_mapping_system();
436c5c4113dSnw141292 	if (xprt != NULL)
437c5c4113dSnw141292 		svc_destroy(xprt);
438c5c4113dSnw141292 }
439c5c4113dSnw141292 
44071590c90Snw141292 static
44171590c90Snw141292 const char *
44271590c90Snw141292 get_fmri(void)
44371590c90Snw141292 {
44471590c90Snw141292 	static char *fmri = NULL;
44571590c90Snw141292 	static char buf[60];
44671590c90Snw141292 	char *s;
44771590c90Snw141292 
44871590c90Snw141292 	membar_consumer();
44971590c90Snw141292 	s = fmri;
45071590c90Snw141292 	if (s != NULL && *s == '\0')
45171590c90Snw141292 		return (NULL);
45271590c90Snw141292 	else if (s != NULL)
45371590c90Snw141292 		return (s);
45471590c90Snw141292 
45571590c90Snw141292 	if ((s = getenv("SMF_FMRI")) == NULL || strlen(s) >= sizeof (buf))
45671590c90Snw141292 		buf[0] = '\0';
45771590c90Snw141292 	else
45871590c90Snw141292 		(void) strlcpy(buf, s, sizeof (buf));
45971590c90Snw141292 
46071590c90Snw141292 	membar_producer();
46171590c90Snw141292 	fmri = buf;
46271590c90Snw141292 
46371590c90Snw141292 	return (get_fmri());
46471590c90Snw141292 }
46571590c90Snw141292 
46671590c90Snw141292 /*
46771590c90Snw141292  * Wrappers for smf_degrade/restore_instance()
46871590c90Snw141292  *
46971590c90Snw141292  * smf_restore_instance() is too heavy duty to be calling every time we
47071590c90Snw141292  * have a successful AD name<->SID lookup.
47171590c90Snw141292  */
47271590c90Snw141292 void
473349d5d8fSnw141292 degrade_svc(int poke_discovery, const char *reason)
47471590c90Snw141292 {
47571590c90Snw141292 	const char *fmri;
47671590c90Snw141292 
47771590c90Snw141292 	/*
47871590c90Snw141292 	 * If the config update thread is in a state where auto-discovery could
47971590c90Snw141292 	 * be re-tried, then this will make it try it -- a sort of auto-refresh.
48071590c90Snw141292 	 */
481349d5d8fSnw141292 	if (poke_discovery)
48271590c90Snw141292 		idmap_cfg_poke_updates();
48371590c90Snw141292 
48471590c90Snw141292 	membar_consumer();
48571590c90Snw141292 	if (degraded)
48671590c90Snw141292 		return;
487349d5d8fSnw141292 
488349d5d8fSnw141292 	idmapdlog(LOG_ERR, "Degraded operation (%s).  If you are running an "
489349d5d8fSnw141292 	    "SMB server in workgroup mode, or if you're not running an SMB "
490349d5d8fSnw141292 	    "server, then you can ignore this message", reason);
491349d5d8fSnw141292 
49271590c90Snw141292 	membar_producer();
49371590c90Snw141292 	degraded = 1;
494*479ac375Sdm199847 	idmap_log_degraded(TRUE);
49571590c90Snw141292 
49671590c90Snw141292 	if ((fmri = get_fmri()) != NULL)
49771590c90Snw141292 		(void) smf_degrade_instance(fmri, 0);
49871590c90Snw141292 }
49971590c90Snw141292 
50071590c90Snw141292 void
50171590c90Snw141292 restore_svc(void)
50271590c90Snw141292 {
50371590c90Snw141292 	const char *fmri;
50471590c90Snw141292 
50571590c90Snw141292 	membar_consumer();
50671590c90Snw141292 	if (!degraded)
50771590c90Snw141292 		return;
50871590c90Snw141292 
50971590c90Snw141292 	if ((fmri = get_fmri()) == NULL)
51071590c90Snw141292 		(void) smf_restore_instance(fmri);
51171590c90Snw141292 
51271590c90Snw141292 	membar_producer();
51371590c90Snw141292 	degraded = 0;
514*479ac375Sdm199847 	idmap_log_degraded(FALSE);
515*479ac375Sdm199847 
516349d5d8fSnw141292 	idmapdlog(LOG_NOTICE, "Normal operation restored");
51771590c90Snw141292 }
51871590c90Snw141292 
519*479ac375Sdm199847 #if 0
520c5c4113dSnw141292 void
5214edd44c5Sjp151216 idmapdlog(int pri, const char *format, ...)
5224edd44c5Sjp151216 {
523c5c4113dSnw141292 	va_list args;
524c5c4113dSnw141292 
525c5c4113dSnw141292 	va_start(args, format);
52671590c90Snw141292 
52771590c90Snw141292 	if (_idmapdstate.debug_mode == TRUE ||
52871590c90Snw141292 	    _idmapdstate.daemon_mode == FALSE) {
529c5c4113dSnw141292 		(void) vfprintf(stderr, format, args);
530c5c4113dSnw141292 		(void) fprintf(stderr, "\n");
531c5c4113dSnw141292 	}
53271590c90Snw141292 
53371590c90Snw141292 	/*
53471590c90Snw141292 	 * We don't want to fill up the logs with useless messages when
53571590c90Snw141292 	 * we're degraded, but we still want to log.
53671590c90Snw141292 	 */
53771590c90Snw141292 	if (degraded)
53871590c90Snw141292 		pri = LOG_DEBUG;
53971590c90Snw141292 
540c5c4113dSnw141292 	(void) vsyslog(pri, format, args);
541c5c4113dSnw141292 	va_end(args);
542c5c4113dSnw141292 }
543*479ac375Sdm199847 #endif
544