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 /*
225771Sjp151216  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
234520Snw141292  * Use is subject to license terms.
244520Snw141292  */
254520Snw141292 
264520Snw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
274520Snw141292 
284520Snw141292 /*
294520Snw141292  * main() of idmapd(1M)
304520Snw141292  */
314520Snw141292 
324520Snw141292 #include "idmapd.h"
33*6017Snw141292 #include <atomic.h>
344520Snw141292 #include <signal.h>
354520Snw141292 #include <rpc/pmap_clnt.h> /* for pmap_unset */
364520Snw141292 #include <string.h> /* strcmp */
374520Snw141292 #include <unistd.h> /* setsid */
384520Snw141292 #include <sys/types.h>
394520Snw141292 #include <memory.h>
404520Snw141292 #include <stropts.h>
414520Snw141292 #include <netconfig.h>
424520Snw141292 #include <sys/resource.h> /* rlimit */
434520Snw141292 #include <syslog.h>
444520Snw141292 #include <rpcsvc/daemon_utils.h> /* DAEMON_UID and DAEMON_GID */
454520Snw141292 #include <priv_utils.h> /* privileges */
464520Snw141292 #include <locale.h>
474520Snw141292 #include <sys/systeminfo.h>
484520Snw141292 #include <errno.h>
494520Snw141292 #include <sys/wait.h>
504520Snw141292 #include <sys/time.h>
514520Snw141292 #include <zone.h>
524520Snw141292 #include <door.h>
535317Sjp151216 #include <port.h>
544520Snw141292 #include <tsol/label.h>
554520Snw141292 #include <sys/resource.h>
564520Snw141292 #include <sys/sid.h>
574520Snw141292 #include <sys/idmap.h>
584520Snw141292 
594520Snw141292 static void	term_handler(int);
604520Snw141292 static void	init_idmapd();
614520Snw141292 static void	fini_idmapd();
624520Snw141292 
634520Snw141292 #define	_RPCSVC_CLOSEDOWN 120
644520Snw141292 
654520Snw141292 int _rpcsvcstate = _IDLE;	/* Set when a request is serviced */
664520Snw141292 int _rpcsvccount = 0;		/* Number of requests being serviced */
674520Snw141292 mutex_t _svcstate_lock;		/* lock for _rpcsvcstate, _rpcsvccount */
684520Snw141292 idmapd_state_t	_idmapdstate;
694520Snw141292 
704520Snw141292 SVCXPRT *xprt = NULL;
714520Snw141292 
724520Snw141292 static int dfd = -1;		/* our door server fildes, for unregistration */
73*6017Snw141292 static int degraded = 0;	/* whether the FMRI has been marked degraded */
744520Snw141292 
754520Snw141292 /*
764520Snw141292  * This is needed for mech_krb5 -- we run as daemon, yes, but we want
775034Snw141292  * mech_krb5 to think we're root so it can get host/nodename.fqdn
785034Snw141292  * tickets for us so we can authenticate to AD as the machine account
795034Snw141292  * that we are.  For more details look at the entry point in mech_krb5
805034Snw141292  * corresponding to gss_init_sec_context().
815034Snw141292  *
825034Snw141292  * As a side effect of faking our effective UID to mech_krb5 we will use
835034Snw141292  * root's default ccache (/tmp/krb5cc_0).  But if that's created by
845034Snw141292  * another process then we won't have access to it: we run as daemon and
855034Snw141292  * keep PRIV_FILE_DAC_READ, which is insufficient to share the ccache
865034Snw141292  * with others.  We putenv("KRB5CCNAME=/var/run/idmap/ccache") in main()
875034Snw141292  * to avoid this issue; see main().
884520Snw141292  *
894520Snw141292  * Someday we'll have gss/mech_krb5 extensions for acquiring initiator
904520Snw141292  * creds with keytabs/raw keys, and someday we'll have extensions to
914520Snw141292  * libsasl to specify creds/name to use on the initiator side, and
924520Snw141292  * someday we'll have extensions to libldap to pass those through to
934520Snw141292  * libsasl.  Until then this interposer will have to do.
944520Snw141292  *
954520Snw141292  * Also, we have to tell lint to shut up: it thinks app_krb5_user_uid()
964520Snw141292  * is defined but not used.
974520Snw141292  */
984520Snw141292 /*LINTLIBRARY*/
994520Snw141292 uid_t
1004520Snw141292 app_krb5_user_uid(void)
1014520Snw141292 {
1024520Snw141292 	return (0);
1034520Snw141292 }
1044520Snw141292 
1054520Snw141292 /*ARGSUSED*/
1064520Snw141292 static void
1075908Sjp151216 term_handler(int sig)
1085908Sjp151216 {
109*6017Snw141292 	idmapdlog(LOG_INFO, "Terminating.");
1104520Snw141292 	fini_idmapd();
1114520Snw141292 	_exit(0);
1124520Snw141292 }
1134520Snw141292 
114*6017Snw141292 /*ARGSUSED*/
115*6017Snw141292 static void
116*6017Snw141292 usr1_handler(int sig)
117*6017Snw141292 {
118*6017Snw141292 	bool_t saved_debug_mode = _idmapdstate.debug_mode;
119*6017Snw141292 
120*6017Snw141292 	_idmapdstate.debug_mode = TRUE;
121*6017Snw141292 	print_idmapdstate();
122*6017Snw141292 	_idmapdstate.debug_mode = saved_debug_mode;
123*6017Snw141292 }
124*6017Snw141292 
1254520Snw141292 static int pipe_fd = -1;
1264520Snw141292 
1274520Snw141292 static void
1285908Sjp151216 daemonize_ready(void)
1295908Sjp151216 {
1304520Snw141292 	char data = '\0';
1314520Snw141292 	/*
1324520Snw141292 	 * wake the parent
1334520Snw141292 	 */
1344520Snw141292 	(void) write(pipe_fd, &data, 1);
1354520Snw141292 	(void) close(pipe_fd);
1364520Snw141292 }
1374520Snw141292 
1384520Snw141292 static int
1395908Sjp151216 daemonize_start(void)
1405908Sjp151216 {
1414520Snw141292 	char	data;
1424520Snw141292 	int	status;
1434520Snw141292 	int	devnull;
1444520Snw141292 	int	filedes[2];
1454520Snw141292 	pid_t	pid;
1464520Snw141292 
1475034Snw141292 	(void) sigset(SIGPIPE, SIG_IGN);
1484520Snw141292 	devnull = open("/dev/null", O_RDONLY);
1494520Snw141292 	if (devnull < 0)
1504520Snw141292 		return (-1);
1514520Snw141292 	(void) dup2(devnull, 0);
1524520Snw141292 	(void) dup2(2, 1);	/* stderr only */
1534520Snw141292 	if (pipe(filedes) < 0)
1544520Snw141292 		return (-1);
1554520Snw141292 	if ((pid = fork1()) < 0)
1564520Snw141292 		return (-1);
1574520Snw141292 	if (pid != 0) {
1584520Snw141292 		/*
1594520Snw141292 		 * parent
1604520Snw141292 		 */
1614520Snw141292 		(void) close(filedes[1]);
1624520Snw141292 		if (read(filedes[0], &data, 1) == 1) {
1634520Snw141292 			/* presume success */
1644520Snw141292 			_exit(0);
1654520Snw141292 		}
1664520Snw141292 		status = -1;
1674520Snw141292 		(void) wait4(pid, &status, 0, NULL);
1684520Snw141292 		if (WIFEXITED(status))
1694520Snw141292 			_exit(WEXITSTATUS(status));
1704520Snw141292 		else
1714520Snw141292 			_exit(-1);
1724520Snw141292 	}
1734520Snw141292 
1744520Snw141292 	/*
1754520Snw141292 	 * child
1764520Snw141292 	 */
1774520Snw141292 	pipe_fd = filedes[1];
1784520Snw141292 	(void) close(filedes[0]);
1794520Snw141292 	(void) setsid();
1804520Snw141292 	(void) umask(0077);
1814520Snw141292 	openlog("idmap", LOG_PID, LOG_DAEMON);
1824520Snw141292 	return (0);
1834520Snw141292 }
1844520Snw141292 
1854520Snw141292 
1864520Snw141292 int
1874520Snw141292 main(int argc, char **argv)
1884520Snw141292 {
1894520Snw141292 	int c;
1904520Snw141292 
191*6017Snw141292 	_idmapdstate.daemon_mode = TRUE;
192*6017Snw141292 	_idmapdstate.debug_mode = FALSE;
193*6017Snw141292 	while ((c = getopt(argc, argv, "d")) != -1) {
1944520Snw141292 		switch (c) {
1954520Snw141292 			case 'd':
196*6017Snw141292 				_idmapdstate.daemon_mode = FALSE;
1974520Snw141292 				break;
1984520Snw141292 			default:
199*6017Snw141292 				fprintf(stderr, "Usage: /usr/lib/idmapd");
200*6017Snw141292 				return (SMF_EXIT_ERR_CONFIG);
2014520Snw141292 				break;
2024520Snw141292 		}
2034520Snw141292 	}
2044520Snw141292 
2054520Snw141292 	/* set locale and domain for internationalization */
2064520Snw141292 	(void) setlocale(LC_ALL, "");
2074520Snw141292 	(void) textdomain(TEXT_DOMAIN);
2084520Snw141292 
2095771Sjp151216 	if (is_system_labeled() && getzoneid() != GLOBAL_ZONEID) {
210*6017Snw141292 		idmapdlog(LOG_ERR,
211*6017Snw141292 		    "with Trusted Extensions idmapd runs only in the "
2125771Sjp151216 		    "global zone");
2134520Snw141292 		exit(1);
2144520Snw141292 	}
2154520Snw141292 
2164520Snw141292 	(void) mutex_init(&_svcstate_lock, USYNC_THREAD, NULL);
2174520Snw141292 
218*6017Snw141292 	if (_idmapdstate.daemon_mode == TRUE) {
2194520Snw141292 		if (daemonize_start() < 0) {
220*6017Snw141292 			(void) idmapdlog(LOG_ERR, "unable to daemonize");
2214520Snw141292 			exit(-1);
2224520Snw141292 		}
2234520Snw141292 	} else
2244520Snw141292 		(void) umask(0077);
2254520Snw141292 
2264884Sjp151216 	idmap_init_tsd_key();
2274884Sjp151216 
2284520Snw141292 	init_idmapd();
2294520Snw141292 
2305034Snw141292 	/* signal handlers that should run only after we're initialized */
2315034Snw141292 	(void) sigset(SIGTERM, term_handler);
232*6017Snw141292 	(void) sigset(SIGUSR1, usr1_handler);
2335968Snw141292 	(void) sigset(SIGHUP, idmap_cfg_hup_handler);
2345034Snw141292 
2354520Snw141292 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
2364520Snw141292 	    DAEMON_UID, DAEMON_GID,
2374520Snw141292 	    PRIV_PROC_AUDIT, PRIV_FILE_DAC_READ,
2384520Snw141292 	    (char *)NULL) == -1) {
239*6017Snw141292 		idmapdlog(LOG_ERR, "unable to drop privileges");
2404520Snw141292 		exit(1);
2414520Snw141292 	}
2424520Snw141292 
2434520Snw141292 	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
2444520Snw141292 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
2454520Snw141292 
246*6017Snw141292 	if (_idmapdstate.daemon_mode == TRUE)
2474520Snw141292 		daemonize_ready();
2484520Snw141292 
2494520Snw141292 	/* With doors RPC this just wastes this thread, oh well */
2504520Snw141292 	svc_run();
2514520Snw141292 	return (0);
2524520Snw141292 }
2534520Snw141292 
2544520Snw141292 static void
2555908Sjp151216 init_idmapd()
2565908Sjp151216 {
2574520Snw141292 	int	error;
2585232Snw141292 	int connmaxrec = IDMAP_MAX_DOOR_RPC;
2594520Snw141292 
2605034Snw141292 	/* create directories as root and chown to daemon uid */
2615034Snw141292 	if (create_directory(IDMAP_DBDIR, DAEMON_UID, DAEMON_GID) < 0)
2625034Snw141292 		exit(1);
2635034Snw141292 	if (create_directory(IDMAP_CACHEDIR, DAEMON_UID, DAEMON_GID) < 0)
2645034Snw141292 		exit(1);
2655034Snw141292 
2665034Snw141292 	/*
2675034Snw141292 	 * Set KRB5CCNAME in the environment.  See app_krb5_user_uid()
2685447Snw141292 	 * for more details.  We blow away the existing one, if there is
2695447Snw141292 	 * one.
2705034Snw141292 	 */
2715447Snw141292 	(void) unlink(IDMAP_CACHEDIR "/ccache");
2725034Snw141292 	putenv("KRB5CCNAME=" IDMAP_CACHEDIR "/ccache");
2735034Snw141292 
2744520Snw141292 	if (sysinfo(SI_HOSTNAME, _idmapdstate.hostname,
2755908Sjp151216 	    sizeof (_idmapdstate.hostname)) == -1) {
2764520Snw141292 		error = errno;
277*6017Snw141292 		idmapdlog(LOG_ERR, "unable to determine hostname, error: %d",
2785908Sjp151216 		    error);
2794520Snw141292 		exit(1);
2804520Snw141292 	}
2814520Snw141292 
2825731Sbaban 	if ((error = init_mapping_system()) < 0) {
283*6017Snw141292 		idmapdlog(LOG_ERR, "unable to initialize mapping system");
2845731Sbaban 		exit(error < -2 ? SMF_EXIT_ERR_CONFIG : 1);
2854520Snw141292 	}
2864520Snw141292 
2875232Snw141292 	xprt = svc_door_create(idmap_prog_1, IDMAP_PROG, IDMAP_V1, connmaxrec);
2884520Snw141292 	if (xprt == NULL) {
289*6017Snw141292 		idmapdlog(LOG_ERR, "unable to create door RPC service");
2904520Snw141292 		goto errout;
2914520Snw141292 	}
2924520Snw141292 
2935064Sdm199847 	if (!svc_control(xprt, SVCSET_CONNMAXREC, &connmaxrec)) {
294*6017Snw141292 		idmapdlog(LOG_ERR, "unable to limit RPC request size");
2955064Sdm199847 		goto errout;
2965064Sdm199847 	}
2975064Sdm199847 
2984520Snw141292 	dfd = xprt->xp_fd;
2994520Snw141292 
3004520Snw141292 	if (dfd == -1) {
301*6017Snw141292 		idmapdlog(LOG_ERR, "unable to register door");
3024520Snw141292 		goto errout;
3034520Snw141292 	}
3044520Snw141292 	if ((error = idmap_reg(dfd)) != 0) {
305*6017Snw141292 		idmapdlog(LOG_ERR, "unable to register door (%s)",
3065908Sjp151216 		    strerror(errno));
3074520Snw141292 		goto errout;
3084520Snw141292 	}
3094520Snw141292 
3104520Snw141292 	if ((error = allocids(_idmapdstate.new_eph_db,
3115908Sjp151216 	    8192, &_idmapdstate.next_uid,
3125908Sjp151216 	    8192, &_idmapdstate.next_gid)) != 0) {
313*6017Snw141292 		idmapdlog(LOG_ERR, "unable to allocate ephemeral IDs (%s)",
314*6017Snw141292 		    strerror(errno));
3154520Snw141292 		_idmapdstate.next_uid = _idmapdstate.limit_uid = SENTINEL_PID;
3164520Snw141292 		_idmapdstate.next_gid = _idmapdstate.limit_gid = SENTINEL_PID;
3174520Snw141292 	} else {
3184520Snw141292 		_idmapdstate.limit_uid = _idmapdstate.next_uid + 8192;
3194520Snw141292 		_idmapdstate.limit_gid = _idmapdstate.next_gid + 8192;
3204520Snw141292 	}
3214520Snw141292 
3224520Snw141292 	print_idmapdstate();
3234520Snw141292 
3244520Snw141292 	return;
3254520Snw141292 
3264520Snw141292 errout:
3274520Snw141292 	fini_idmapd();
3284520Snw141292 	exit(1);
3294520Snw141292 }
3304520Snw141292 
3314520Snw141292 static void
3325908Sjp151216 fini_idmapd()
3335908Sjp151216 {
3344520Snw141292 	idmap_unreg(dfd);
3354520Snw141292 	fini_mapping_system();
3364520Snw141292 	if (xprt != NULL)
3374520Snw141292 		svc_destroy(xprt);
3384520Snw141292 }
3394520Snw141292 
340*6017Snw141292 static
341*6017Snw141292 const char *
342*6017Snw141292 get_fmri(void)
343*6017Snw141292 {
344*6017Snw141292 	static char *fmri = NULL;
345*6017Snw141292 	static char buf[60];
346*6017Snw141292 	char *s;
347*6017Snw141292 
348*6017Snw141292 	membar_consumer();
349*6017Snw141292 	s = fmri;
350*6017Snw141292 	if (s != NULL && *s == '\0')
351*6017Snw141292 		return (NULL);
352*6017Snw141292 	else if (s != NULL)
353*6017Snw141292 		return (s);
354*6017Snw141292 
355*6017Snw141292 	if ((s = getenv("SMF_FMRI")) == NULL || strlen(s) >= sizeof (buf))
356*6017Snw141292 		buf[0] = '\0';
357*6017Snw141292 	else
358*6017Snw141292 		(void) strlcpy(buf, s, sizeof (buf));
359*6017Snw141292 
360*6017Snw141292 	membar_producer();
361*6017Snw141292 	fmri = buf;
362*6017Snw141292 
363*6017Snw141292 	return (get_fmri());
364*6017Snw141292 }
365*6017Snw141292 
366*6017Snw141292 /*
367*6017Snw141292  * Wrappers for smf_degrade/restore_instance()
368*6017Snw141292  *
369*6017Snw141292  * smf_restore_instance() is too heavy duty to be calling every time we
370*6017Snw141292  * have a successful AD name<->SID lookup.
371*6017Snw141292  */
372*6017Snw141292 void
373*6017Snw141292 degrade_svc(const char *reason)
374*6017Snw141292 {
375*6017Snw141292 	const char *fmri;
376*6017Snw141292 
377*6017Snw141292 	/*
378*6017Snw141292 	 * If the config update thread is in a state where auto-discovery could
379*6017Snw141292 	 * be re-tried, then this will make it try it -- a sort of auto-refresh.
380*6017Snw141292 	 */
381*6017Snw141292 	idmap_cfg_poke_updates();
382*6017Snw141292 
383*6017Snw141292 	membar_consumer();
384*6017Snw141292 	if (degraded)
385*6017Snw141292 		return;
386*6017Snw141292 	membar_producer();
387*6017Snw141292 	degraded = 1;
388*6017Snw141292 
389*6017Snw141292 	if ((fmri = get_fmri()) != NULL)
390*6017Snw141292 		(void) smf_degrade_instance(fmri, 0);
391*6017Snw141292 
392*6017Snw141292 	idmapdlog(LOG_ERR, "Degraded operation (%s)", reason);
393*6017Snw141292 }
394*6017Snw141292 
395*6017Snw141292 void
396*6017Snw141292 restore_svc(void)
397*6017Snw141292 {
398*6017Snw141292 	const char *fmri;
399*6017Snw141292 
400*6017Snw141292 	membar_consumer();
401*6017Snw141292 	if (!degraded)
402*6017Snw141292 		return;
403*6017Snw141292 
404*6017Snw141292 	if ((fmri = get_fmri()) == NULL)
405*6017Snw141292 		(void) smf_restore_instance(fmri);
406*6017Snw141292 
407*6017Snw141292 	membar_producer();
408*6017Snw141292 	degraded = 0;
409*6017Snw141292 	idmapdlog(LOG_INFO, "Normal operation restored");
410*6017Snw141292 }
411*6017Snw141292 
4124520Snw141292 void
4135908Sjp151216 idmapdlog(int pri, const char *format, ...)
4145908Sjp151216 {
4154520Snw141292 	va_list args;
4164520Snw141292 
4174520Snw141292 	va_start(args, format);
418*6017Snw141292 
419*6017Snw141292 	if (_idmapdstate.debug_mode == TRUE ||
420*6017Snw141292 	    _idmapdstate.daemon_mode == FALSE) {
4214520Snw141292 		(void) vfprintf(stderr, format, args);
4224520Snw141292 		(void) fprintf(stderr, "\n");
4234520Snw141292 	}
424*6017Snw141292 
425*6017Snw141292 	/*
426*6017Snw141292 	 * We don't want to fill up the logs with useless messages when
427*6017Snw141292 	 * we're degraded, but we still want to log.
428*6017Snw141292 	 */
429*6017Snw141292 	if (degraded)
430*6017Snw141292 		pri = LOG_DEBUG;
431*6017Snw141292 
4324520Snw141292 	(void) vsyslog(pri, format, args);
4334520Snw141292 	va_end(args);
4344520Snw141292 }
435