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*5771Sjp151216  * 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"
334520Snw141292 #include <signal.h>
344520Snw141292 #include <rpc/pmap_clnt.h> /* for pmap_unset */
354520Snw141292 #include <string.h> /* strcmp */
364520Snw141292 #include <unistd.h> /* setsid */
374520Snw141292 #include <sys/types.h>
384520Snw141292 #include <memory.h>
394520Snw141292 #include <stropts.h>
404520Snw141292 #include <netconfig.h>
414520Snw141292 #include <sys/resource.h> /* rlimit */
424520Snw141292 #include <syslog.h>
434520Snw141292 #include <rpcsvc/daemon_utils.h> /* DAEMON_UID and DAEMON_GID */
444520Snw141292 #include <priv_utils.h> /* privileges */
454520Snw141292 #include <locale.h>
464520Snw141292 #include <sys/systeminfo.h>
474520Snw141292 #include <errno.h>
484520Snw141292 #include <sys/wait.h>
494520Snw141292 #include <sys/time.h>
504520Snw141292 #include <zone.h>
514520Snw141292 #include <door.h>
525317Sjp151216 #include <port.h>
534520Snw141292 #include <tsol/label.h>
544520Snw141292 #include <sys/resource.h>
554520Snw141292 #include <sys/sid.h>
564520Snw141292 #include <sys/idmap.h>
574520Snw141292 
584520Snw141292 static void	hup_handler(int);
594520Snw141292 static void	term_handler(int);
604520Snw141292 static void	init_idmapd();
614520Snw141292 static void	fini_idmapd();
624520Snw141292 
634520Snw141292 #ifndef SIG_PF
644520Snw141292 #define	SIG_PF void(*)(int)
654520Snw141292 #endif
664520Snw141292 
674520Snw141292 #define	_RPCSVC_CLOSEDOWN 120
684520Snw141292 
694520Snw141292 int _rpcsvcstate = _IDLE;	/* Set when a request is serviced */
704520Snw141292 int _rpcsvccount = 0;		/* Number of requests being serviced */
714520Snw141292 mutex_t _svcstate_lock;		/* lock for _rpcsvcstate, _rpcsvccount */
724520Snw141292 idmapd_state_t	_idmapdstate;
734520Snw141292 
745317Sjp151216 int hupped;
755317Sjp151216 extern int hup_ev_port;
765317Sjp151216 
774520Snw141292 SVCXPRT *xprt = NULL;
784520Snw141292 
794520Snw141292 static int dfd = -1;		/* our door server fildes, for unregistration */
804520Snw141292 
814520Snw141292 #ifdef DEBUG
824520Snw141292 #define	RPC_SVC_FG
834520Snw141292 #endif
844520Snw141292 
854520Snw141292 /*
864520Snw141292  * This is needed for mech_krb5 -- we run as daemon, yes, but we want
875034Snw141292  * mech_krb5 to think we're root so it can get host/nodename.fqdn
885034Snw141292  * tickets for us so we can authenticate to AD as the machine account
895034Snw141292  * that we are.  For more details look at the entry point in mech_krb5
905034Snw141292  * corresponding to gss_init_sec_context().
915034Snw141292  *
925034Snw141292  * As a side effect of faking our effective UID to mech_krb5 we will use
935034Snw141292  * root's default ccache (/tmp/krb5cc_0).  But if that's created by
945034Snw141292  * another process then we won't have access to it: we run as daemon and
955034Snw141292  * keep PRIV_FILE_DAC_READ, which is insufficient to share the ccache
965034Snw141292  * with others.  We putenv("KRB5CCNAME=/var/run/idmap/ccache") in main()
975034Snw141292  * to avoid this issue; see main().
984520Snw141292  *
994520Snw141292  * Someday we'll have gss/mech_krb5 extensions for acquiring initiator
1004520Snw141292  * creds with keytabs/raw keys, and someday we'll have extensions to
1014520Snw141292  * libsasl to specify creds/name to use on the initiator side, and
1024520Snw141292  * someday we'll have extensions to libldap to pass those through to
1034520Snw141292  * libsasl.  Until then this interposer will have to do.
1044520Snw141292  *
1054520Snw141292  * Also, we have to tell lint to shut up: it thinks app_krb5_user_uid()
1064520Snw141292  * is defined but not used.
1074520Snw141292  */
1084520Snw141292 /*LINTLIBRARY*/
1094520Snw141292 uid_t
1104520Snw141292 app_krb5_user_uid(void)
1114520Snw141292 {
1124520Snw141292 	return (0);
1134520Snw141292 }
1144520Snw141292 
1154520Snw141292 /*ARGSUSED*/
1164520Snw141292 static void
1174520Snw141292 hup_handler(int sig) {
1185317Sjp151216 	hupped = 1;
1195317Sjp151216 	if (hup_ev_port >= 0)
1205317Sjp151216 		(void) port_send(hup_ev_port, 1, &sig /* any ptr will do */);
1214520Snw141292 }
1224520Snw141292 
1235317Sjp151216 
1244520Snw141292 /*ARGSUSED*/
1254520Snw141292 static void
1264520Snw141292 term_handler(int sig) {
1274520Snw141292 	(void) idmapdlog(LOG_INFO, "idmapd: Terminating.");
1284520Snw141292 	fini_idmapd();
1294520Snw141292 	_exit(0);
1304520Snw141292 }
1314520Snw141292 
1324520Snw141292 static int pipe_fd = -1;
1334520Snw141292 
1344520Snw141292 static void
1354520Snw141292 daemonize_ready(void) {
1364520Snw141292 	char data = '\0';
1374520Snw141292 	/*
1384520Snw141292 	 * wake the parent
1394520Snw141292 	 */
1404520Snw141292 	(void) write(pipe_fd, &data, 1);
1414520Snw141292 	(void) close(pipe_fd);
1424520Snw141292 }
1434520Snw141292 
1444520Snw141292 static int
1454520Snw141292 daemonize_start(void) {
1464520Snw141292 	char	data;
1474520Snw141292 	int	status;
1484520Snw141292 	int	devnull;
1494520Snw141292 	int	filedes[2];
1504520Snw141292 	pid_t	pid;
1514520Snw141292 
1525034Snw141292 	(void) sigset(SIGPIPE, SIG_IGN);
1534520Snw141292 	devnull = open("/dev/null", O_RDONLY);
1544520Snw141292 	if (devnull < 0)
1554520Snw141292 		return (-1);
1564520Snw141292 	(void) dup2(devnull, 0);
1574520Snw141292 	(void) dup2(2, 1);	/* stderr only */
1584520Snw141292 	if (pipe(filedes) < 0)
1594520Snw141292 		return (-1);
1604520Snw141292 	if ((pid = fork1()) < 0)
1614520Snw141292 		return (-1);
1624520Snw141292 	if (pid != 0) {
1634520Snw141292 		/*
1644520Snw141292 		 * parent
1654520Snw141292 		 */
1664520Snw141292 		(void) close(filedes[1]);
1674520Snw141292 		if (read(filedes[0], &data, 1) == 1) {
1684520Snw141292 			/* presume success */
1694520Snw141292 			_exit(0);
1704520Snw141292 		}
1714520Snw141292 		status = -1;
1724520Snw141292 		(void) wait4(pid, &status, 0, NULL);
1734520Snw141292 		if (WIFEXITED(status))
1744520Snw141292 			_exit(WEXITSTATUS(status));
1754520Snw141292 		else
1764520Snw141292 			_exit(-1);
1774520Snw141292 	}
1784520Snw141292 
1794520Snw141292 	/*
1804520Snw141292 	 * child
1814520Snw141292 	 */
1824520Snw141292 	pipe_fd = filedes[1];
1834520Snw141292 	(void) close(filedes[0]);
1844520Snw141292 	(void) setsid();
1854520Snw141292 	(void) umask(0077);
1864520Snw141292 	openlog("idmap", LOG_PID, LOG_DAEMON);
1874520Snw141292 	_idmapdstate.daemon_mode = TRUE;
1884520Snw141292 	return (0);
1894520Snw141292 }
1904520Snw141292 
1914520Snw141292 
1924520Snw141292 int
1934520Snw141292 main(int argc, char **argv)
1944520Snw141292 {
1954520Snw141292 	int c;
1964520Snw141292 #ifdef RPC_SVC_FG
1974520Snw141292 	bool_t daemonize = FALSE;
1984520Snw141292 #else
1994520Snw141292 	bool_t daemonize = TRUE;
2004520Snw141292 #endif
2014520Snw141292 
2024520Snw141292 	while ((c = getopt(argc, argv, "d")) != EOF) {
2034520Snw141292 		switch (c) {
2044520Snw141292 			case 'd':
2054520Snw141292 				daemonize = FALSE;
2064520Snw141292 				break;
2074520Snw141292 			default:
2084520Snw141292 				break;
2094520Snw141292 		}
2104520Snw141292 	}
2114520Snw141292 
2124520Snw141292 	/* set locale and domain for internationalization */
2134520Snw141292 	(void) setlocale(LC_ALL, "");
2144520Snw141292 	(void) textdomain(TEXT_DOMAIN);
2154520Snw141292 
216*5771Sjp151216 	if (is_system_labeled() && getzoneid() != GLOBAL_ZONEID) {
2174520Snw141292 		(void) idmapdlog(LOG_ERR,
218*5771Sjp151216 		    "idmapd: with Trusted Extensions idmapd runs only in the "
219*5771Sjp151216 		    "global zone");
2204520Snw141292 		exit(1);
2214520Snw141292 	}
2224520Snw141292 
2234520Snw141292 	(void) mutex_init(&_svcstate_lock, USYNC_THREAD, NULL);
2244520Snw141292 
2254520Snw141292 	if (daemonize == TRUE) {
2264520Snw141292 		if (daemonize_start() < 0) {
2274520Snw141292 			(void) perror("idmapd: unable to daemonize");
2284520Snw141292 			exit(-1);
2294520Snw141292 		}
2304520Snw141292 	} else
2314520Snw141292 		(void) umask(0077);
2324520Snw141292 
2334884Sjp151216 	idmap_init_tsd_key();
2344884Sjp151216 
2354520Snw141292 	init_idmapd();
2364520Snw141292 
2375034Snw141292 	/* signal handlers that should run only after we're initialized */
2385034Snw141292 	(void) sigset(SIGTERM, term_handler);
2395034Snw141292 	(void) sigset(SIGHUP, hup_handler);
2405034Snw141292 
2414520Snw141292 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
2424520Snw141292 	    DAEMON_UID, DAEMON_GID,
2434520Snw141292 	    PRIV_PROC_AUDIT, PRIV_FILE_DAC_READ,
2444520Snw141292 	    (char *)NULL) == -1) {
2454644Sbaban 		(void) idmapdlog(LOG_ERR, "idmapd: unable to drop privileges");
2464520Snw141292 		exit(1);
2474520Snw141292 	}
2484520Snw141292 
2494520Snw141292 	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
2504520Snw141292 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
2514520Snw141292 
2524520Snw141292 	if (daemonize == TRUE)
2534520Snw141292 		daemonize_ready();
2544520Snw141292 
2554520Snw141292 	/* With doors RPC this just wastes this thread, oh well */
2564520Snw141292 	svc_run();
2574520Snw141292 	return (0);
2584520Snw141292 }
2594520Snw141292 
2604520Snw141292 static void
2614520Snw141292 init_idmapd() {
2624520Snw141292 	int	error;
2635232Snw141292 	int connmaxrec = IDMAP_MAX_DOOR_RPC;
2644520Snw141292 
2655034Snw141292 	/* create directories as root and chown to daemon uid */
2665034Snw141292 	if (create_directory(IDMAP_DBDIR, DAEMON_UID, DAEMON_GID) < 0)
2675034Snw141292 		exit(1);
2685034Snw141292 	if (create_directory(IDMAP_CACHEDIR, DAEMON_UID, DAEMON_GID) < 0)
2695034Snw141292 		exit(1);
2705034Snw141292 
2715034Snw141292 	/*
2725034Snw141292 	 * Set KRB5CCNAME in the environment.  See app_krb5_user_uid()
2735447Snw141292 	 * for more details.  We blow away the existing one, if there is
2745447Snw141292 	 * one.
2755034Snw141292 	 */
2765447Snw141292 	(void) unlink(IDMAP_CACHEDIR "/ccache");
2775034Snw141292 	putenv("KRB5CCNAME=" IDMAP_CACHEDIR "/ccache");
2785034Snw141292 
2794520Snw141292 	memset(&_idmapdstate, 0, sizeof (_idmapdstate));
2804520Snw141292 
2814520Snw141292 	if (sysinfo(SI_HOSTNAME, _idmapdstate.hostname,
2824520Snw141292 			sizeof (_idmapdstate.hostname)) == -1) {
2834520Snw141292 		error = errno;
2844520Snw141292 		idmapdlog(LOG_ERR,
2854520Snw141292 	"idmapd: unable to determine hostname, error: %d",
2864520Snw141292 			error);
2874520Snw141292 		exit(1);
2884520Snw141292 	}
2894520Snw141292 
2905731Sbaban 	if ((error = init_mapping_system()) < 0) {
2914520Snw141292 		idmapdlog(LOG_ERR,
2924520Snw141292 		"idmapd: unable to initialize mapping system");
2935731Sbaban 		exit(error < -2 ? SMF_EXIT_ERR_CONFIG : 1);
2944520Snw141292 	}
2954520Snw141292 
2965232Snw141292 	xprt = svc_door_create(idmap_prog_1, IDMAP_PROG, IDMAP_V1, connmaxrec);
2974520Snw141292 	if (xprt == NULL) {
2984520Snw141292 		idmapdlog(LOG_ERR,
2994520Snw141292 		"idmapd: unable to create door RPC service");
3004520Snw141292 		goto errout;
3014520Snw141292 	}
3024520Snw141292 
3035064Sdm199847 	if (!svc_control(xprt, SVCSET_CONNMAXREC, &connmaxrec)) {
3045064Sdm199847 		idmapdlog(LOG_ERR,
3055064Sdm199847 		    "idmapd: unable to limit RPC request size");
3065064Sdm199847 		goto errout;
3075064Sdm199847 	}
3085064Sdm199847 
3094520Snw141292 	dfd = xprt->xp_fd;
3104520Snw141292 
3114520Snw141292 	if (dfd == -1) {
3124520Snw141292 		idmapdlog(LOG_ERR, "idmapd: unable to register door");
3134520Snw141292 		goto errout;
3144520Snw141292 	}
3154520Snw141292 	if ((error = idmap_reg(dfd)) != 0) {
3164520Snw141292 		idmapdlog(LOG_ERR, "idmapd: unable to register door (%s)",
317*5771Sjp151216 				strerror(errno));
3184520Snw141292 		goto errout;
3194520Snw141292 	}
3204520Snw141292 
3214520Snw141292 	if ((error = allocids(_idmapdstate.new_eph_db,
3224520Snw141292 			8192, &_idmapdstate.next_uid,
3234520Snw141292 			8192, &_idmapdstate.next_gid)) != 0) {
3244520Snw141292 		idmapdlog(LOG_ERR, "idmapd: unable to allocate ephemeral IDs "
325*5771Sjp151216 			"(%s)", strerror(errno));
3264520Snw141292 		_idmapdstate.next_uid = _idmapdstate.limit_uid = SENTINEL_PID;
3274520Snw141292 		_idmapdstate.next_gid = _idmapdstate.limit_gid = SENTINEL_PID;
3284520Snw141292 	} else {
3294520Snw141292 		_idmapdstate.limit_uid = _idmapdstate.next_uid + 8192;
3304520Snw141292 		_idmapdstate.limit_gid = _idmapdstate.next_gid + 8192;
3314520Snw141292 	}
3324520Snw141292 
3334520Snw141292 	print_idmapdstate();
3344520Snw141292 
3354520Snw141292 	return;
3364520Snw141292 
3374520Snw141292 errout:
3384520Snw141292 	fini_idmapd();
3394520Snw141292 	exit(1);
3404520Snw141292 }
3414520Snw141292 
3424520Snw141292 static void
3434520Snw141292 fini_idmapd() {
3444520Snw141292 	idmap_unreg(dfd);
3454520Snw141292 	fini_mapping_system();
3464520Snw141292 	if (xprt != NULL)
3474520Snw141292 		svc_destroy(xprt);
3484520Snw141292 }
3494520Snw141292 
3504520Snw141292 void
3514520Snw141292 idmapdlog(int pri, const char *format, ...) {
3524520Snw141292 	va_list args;
3534520Snw141292 
3544520Snw141292 	va_start(args, format);
3554520Snw141292 	if (_idmapdstate.daemon_mode == FALSE) {
3564520Snw141292 		(void) vfprintf(stderr, format, args);
3574520Snw141292 		(void) fprintf(stderr, "\n");
3584520Snw141292 	}
3594520Snw141292 	(void) vsyslog(pri, format, args);
3604520Snw141292 	va_end(args);
3614520Snw141292 }
362