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"
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	term_handler(int);
594520Snw141292 static void	init_idmapd();
604520Snw141292 static void	fini_idmapd();
614520Snw141292 
624520Snw141292 #ifndef SIG_PF
634520Snw141292 #define	SIG_PF void(*)(int)
644520Snw141292 #endif
654520Snw141292 
664520Snw141292 #define	_RPCSVC_CLOSEDOWN 120
674520Snw141292 
684520Snw141292 int _rpcsvcstate = _IDLE;	/* Set when a request is serviced */
694520Snw141292 int _rpcsvccount = 0;		/* Number of requests being serviced */
704520Snw141292 mutex_t _svcstate_lock;		/* lock for _rpcsvcstate, _rpcsvccount */
714520Snw141292 idmapd_state_t	_idmapdstate;
724520Snw141292 
734520Snw141292 SVCXPRT *xprt = NULL;
744520Snw141292 
754520Snw141292 static int dfd = -1;		/* our door server fildes, for unregistration */
764520Snw141292 
774520Snw141292 #ifdef DEBUG
784520Snw141292 #define	RPC_SVC_FG
794520Snw141292 #endif
804520Snw141292 
814520Snw141292 /*
824520Snw141292  * This is needed for mech_krb5 -- we run as daemon, yes, but we want
835034Snw141292  * mech_krb5 to think we're root so it can get host/nodename.fqdn
845034Snw141292  * tickets for us so we can authenticate to AD as the machine account
855034Snw141292  * that we are.  For more details look at the entry point in mech_krb5
865034Snw141292  * corresponding to gss_init_sec_context().
875034Snw141292  *
885034Snw141292  * As a side effect of faking our effective UID to mech_krb5 we will use
895034Snw141292  * root's default ccache (/tmp/krb5cc_0).  But if that's created by
905034Snw141292  * another process then we won't have access to it: we run as daemon and
915034Snw141292  * keep PRIV_FILE_DAC_READ, which is insufficient to share the ccache
925034Snw141292  * with others.  We putenv("KRB5CCNAME=/var/run/idmap/ccache") in main()
935034Snw141292  * to avoid this issue; see main().
944520Snw141292  *
954520Snw141292  * Someday we'll have gss/mech_krb5 extensions for acquiring initiator
964520Snw141292  * creds with keytabs/raw keys, and someday we'll have extensions to
974520Snw141292  * libsasl to specify creds/name to use on the initiator side, and
984520Snw141292  * someday we'll have extensions to libldap to pass those through to
994520Snw141292  * libsasl.  Until then this interposer will have to do.
1004520Snw141292  *
1014520Snw141292  * Also, we have to tell lint to shut up: it thinks app_krb5_user_uid()
1024520Snw141292  * is defined but not used.
1034520Snw141292  */
1044520Snw141292 /*LINTLIBRARY*/
1054520Snw141292 uid_t
1064520Snw141292 app_krb5_user_uid(void)
1074520Snw141292 {
1084520Snw141292 	return (0);
1094520Snw141292 }
1104520Snw141292 
1114520Snw141292 /*ARGSUSED*/
1124520Snw141292 static void
1135908Sjp151216 term_handler(int sig)
1145908Sjp151216 {
1154520Snw141292 	(void) idmapdlog(LOG_INFO, "idmapd: Terminating.");
1164520Snw141292 	fini_idmapd();
1174520Snw141292 	_exit(0);
1184520Snw141292 }
1194520Snw141292 
1204520Snw141292 static int pipe_fd = -1;
1214520Snw141292 
1224520Snw141292 static void
1235908Sjp151216 daemonize_ready(void)
1245908Sjp151216 {
1254520Snw141292 	char data = '\0';
1264520Snw141292 	/*
1274520Snw141292 	 * wake the parent
1284520Snw141292 	 */
1294520Snw141292 	(void) write(pipe_fd, &data, 1);
1304520Snw141292 	(void) close(pipe_fd);
1314520Snw141292 }
1324520Snw141292 
1334520Snw141292 static int
1345908Sjp151216 daemonize_start(void)
1355908Sjp151216 {
1364520Snw141292 	char	data;
1374520Snw141292 	int	status;
1384520Snw141292 	int	devnull;
1394520Snw141292 	int	filedes[2];
1404520Snw141292 	pid_t	pid;
1414520Snw141292 
1425034Snw141292 	(void) sigset(SIGPIPE, SIG_IGN);
1434520Snw141292 	devnull = open("/dev/null", O_RDONLY);
1444520Snw141292 	if (devnull < 0)
1454520Snw141292 		return (-1);
1464520Snw141292 	(void) dup2(devnull, 0);
1474520Snw141292 	(void) dup2(2, 1);	/* stderr only */
1484520Snw141292 	if (pipe(filedes) < 0)
1494520Snw141292 		return (-1);
1504520Snw141292 	if ((pid = fork1()) < 0)
1514520Snw141292 		return (-1);
1524520Snw141292 	if (pid != 0) {
1534520Snw141292 		/*
1544520Snw141292 		 * parent
1554520Snw141292 		 */
1564520Snw141292 		(void) close(filedes[1]);
1574520Snw141292 		if (read(filedes[0], &data, 1) == 1) {
1584520Snw141292 			/* presume success */
1594520Snw141292 			_exit(0);
1604520Snw141292 		}
1614520Snw141292 		status = -1;
1624520Snw141292 		(void) wait4(pid, &status, 0, NULL);
1634520Snw141292 		if (WIFEXITED(status))
1644520Snw141292 			_exit(WEXITSTATUS(status));
1654520Snw141292 		else
1664520Snw141292 			_exit(-1);
1674520Snw141292 	}
1684520Snw141292 
1694520Snw141292 	/*
1704520Snw141292 	 * child
1714520Snw141292 	 */
1724520Snw141292 	pipe_fd = filedes[1];
1734520Snw141292 	(void) close(filedes[0]);
1744520Snw141292 	(void) setsid();
1754520Snw141292 	(void) umask(0077);
1764520Snw141292 	openlog("idmap", LOG_PID, LOG_DAEMON);
1774520Snw141292 	_idmapdstate.daemon_mode = TRUE;
1784520Snw141292 	return (0);
1794520Snw141292 }
1804520Snw141292 
1814520Snw141292 
1824520Snw141292 int
1834520Snw141292 main(int argc, char **argv)
1844520Snw141292 {
1854520Snw141292 	int c;
1864520Snw141292 #ifdef RPC_SVC_FG
1874520Snw141292 	bool_t daemonize = FALSE;
1884520Snw141292 #else
1894520Snw141292 	bool_t daemonize = TRUE;
1904520Snw141292 #endif
1914520Snw141292 
1924520Snw141292 	while ((c = getopt(argc, argv, "d")) != EOF) {
1934520Snw141292 		switch (c) {
1944520Snw141292 			case 'd':
1954520Snw141292 				daemonize = FALSE;
1964520Snw141292 				break;
1974520Snw141292 			default:
1984520Snw141292 				break;
1994520Snw141292 		}
2004520Snw141292 	}
2014520Snw141292 
2024520Snw141292 	/* set locale and domain for internationalization */
2034520Snw141292 	(void) setlocale(LC_ALL, "");
2044520Snw141292 	(void) textdomain(TEXT_DOMAIN);
2054520Snw141292 
2065771Sjp151216 	if (is_system_labeled() && getzoneid() != GLOBAL_ZONEID) {
2074520Snw141292 		(void) idmapdlog(LOG_ERR,
2085771Sjp151216 		    "idmapd: with Trusted Extensions idmapd runs only in the "
2095771Sjp151216 		    "global zone");
2104520Snw141292 		exit(1);
2114520Snw141292 	}
2124520Snw141292 
2134520Snw141292 	(void) mutex_init(&_svcstate_lock, USYNC_THREAD, NULL);
2144520Snw141292 
2154520Snw141292 	if (daemonize == TRUE) {
2164520Snw141292 		if (daemonize_start() < 0) {
2174520Snw141292 			(void) perror("idmapd: unable to daemonize");
2184520Snw141292 			exit(-1);
2194520Snw141292 		}
2204520Snw141292 	} else
2214520Snw141292 		(void) umask(0077);
2224520Snw141292 
2234884Sjp151216 	idmap_init_tsd_key();
2244884Sjp151216 
2254520Snw141292 	init_idmapd();
2264520Snw141292 
2275034Snw141292 	/* signal handlers that should run only after we're initialized */
2285034Snw141292 	(void) sigset(SIGTERM, term_handler);
229*5968Snw141292 	(void) sigset(SIGHUP, idmap_cfg_hup_handler);
2305034Snw141292 
2314520Snw141292 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
2324520Snw141292 	    DAEMON_UID, DAEMON_GID,
2334520Snw141292 	    PRIV_PROC_AUDIT, PRIV_FILE_DAC_READ,
2344520Snw141292 	    (char *)NULL) == -1) {
2354644Sbaban 		(void) idmapdlog(LOG_ERR, "idmapd: unable to drop privileges");
2364520Snw141292 		exit(1);
2374520Snw141292 	}
2384520Snw141292 
2394520Snw141292 	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
2404520Snw141292 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
2414520Snw141292 
2424520Snw141292 	if (daemonize == TRUE)
2434520Snw141292 		daemonize_ready();
2444520Snw141292 
2454520Snw141292 	/* With doors RPC this just wastes this thread, oh well */
2464520Snw141292 	svc_run();
2474520Snw141292 	return (0);
2484520Snw141292 }
2494520Snw141292 
2504520Snw141292 static void
2515908Sjp151216 init_idmapd()
2525908Sjp151216 {
2534520Snw141292 	int	error;
2545232Snw141292 	int connmaxrec = IDMAP_MAX_DOOR_RPC;
2554520Snw141292 
2565034Snw141292 	/* create directories as root and chown to daemon uid */
2575034Snw141292 	if (create_directory(IDMAP_DBDIR, DAEMON_UID, DAEMON_GID) < 0)
2585034Snw141292 		exit(1);
2595034Snw141292 	if (create_directory(IDMAP_CACHEDIR, DAEMON_UID, DAEMON_GID) < 0)
2605034Snw141292 		exit(1);
2615034Snw141292 
2625034Snw141292 	/*
2635034Snw141292 	 * Set KRB5CCNAME in the environment.  See app_krb5_user_uid()
2645447Snw141292 	 * for more details.  We blow away the existing one, if there is
2655447Snw141292 	 * one.
2665034Snw141292 	 */
2675447Snw141292 	(void) unlink(IDMAP_CACHEDIR "/ccache");
2685034Snw141292 	putenv("KRB5CCNAME=" IDMAP_CACHEDIR "/ccache");
2695034Snw141292 
2704520Snw141292 	memset(&_idmapdstate, 0, sizeof (_idmapdstate));
2714520Snw141292 
2724520Snw141292 	if (sysinfo(SI_HOSTNAME, _idmapdstate.hostname,
2735908Sjp151216 	    sizeof (_idmapdstate.hostname)) == -1) {
2744520Snw141292 		error = errno;
2754520Snw141292 		idmapdlog(LOG_ERR,
2765908Sjp151216 		    "idmapd: unable to determine hostname, error: %d",
2775908Sjp151216 		    error);
2784520Snw141292 		exit(1);
2794520Snw141292 	}
2804520Snw141292 
2815731Sbaban 	if ((error = init_mapping_system()) < 0) {
2824520Snw141292 		idmapdlog(LOG_ERR,
2835908Sjp151216 		    "idmapd: 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) {
2894520Snw141292 		idmapdlog(LOG_ERR,
2905908Sjp151216 		    "idmapd: unable to create door RPC service");
2914520Snw141292 		goto errout;
2924520Snw141292 	}
2934520Snw141292 
2945064Sdm199847 	if (!svc_control(xprt, SVCSET_CONNMAXREC, &connmaxrec)) {
2955064Sdm199847 		idmapdlog(LOG_ERR,
2965064Sdm199847 		    "idmapd: unable to limit RPC request size");
2975064Sdm199847 		goto errout;
2985064Sdm199847 	}
2995064Sdm199847 
3004520Snw141292 	dfd = xprt->xp_fd;
3014520Snw141292 
3024520Snw141292 	if (dfd == -1) {
3034520Snw141292 		idmapdlog(LOG_ERR, "idmapd: unable to register door");
3044520Snw141292 		goto errout;
3054520Snw141292 	}
3064520Snw141292 	if ((error = idmap_reg(dfd)) != 0) {
3074520Snw141292 		idmapdlog(LOG_ERR, "idmapd: unable to register door (%s)",
3085908Sjp151216 		    strerror(errno));
3094520Snw141292 		goto errout;
3104520Snw141292 	}
3114520Snw141292 
3124520Snw141292 	if ((error = allocids(_idmapdstate.new_eph_db,
3135908Sjp151216 	    8192, &_idmapdstate.next_uid,
3145908Sjp151216 	    8192, &_idmapdstate.next_gid)) != 0) {
3154520Snw141292 		idmapdlog(LOG_ERR, "idmapd: unable to allocate ephemeral IDs "
3165908Sjp151216 		    "(%s)", strerror(errno));
3174520Snw141292 		_idmapdstate.next_uid = _idmapdstate.limit_uid = SENTINEL_PID;
3184520Snw141292 		_idmapdstate.next_gid = _idmapdstate.limit_gid = SENTINEL_PID;
3194520Snw141292 	} else {
3204520Snw141292 		_idmapdstate.limit_uid = _idmapdstate.next_uid + 8192;
3214520Snw141292 		_idmapdstate.limit_gid = _idmapdstate.next_gid + 8192;
3224520Snw141292 	}
3234520Snw141292 
3244520Snw141292 	print_idmapdstate();
3254520Snw141292 
3264520Snw141292 	return;
3274520Snw141292 
3284520Snw141292 errout:
3294520Snw141292 	fini_idmapd();
3304520Snw141292 	exit(1);
3314520Snw141292 }
3324520Snw141292 
3334520Snw141292 static void
3345908Sjp151216 fini_idmapd()
3355908Sjp151216 {
3364520Snw141292 	idmap_unreg(dfd);
3374520Snw141292 	fini_mapping_system();
3384520Snw141292 	if (xprt != NULL)
3394520Snw141292 		svc_destroy(xprt);
3404520Snw141292 }
3414520Snw141292 
3424520Snw141292 void
3435908Sjp151216 idmapdlog(int pri, const char *format, ...)
3445908Sjp151216 {
3454520Snw141292 	va_list args;
3464520Snw141292 
3474520Snw141292 	va_start(args, format);
3484520Snw141292 	if (_idmapdstate.daemon_mode == FALSE) {
3494520Snw141292 		(void) vfprintf(stderr, format, args);
3504520Snw141292 		(void) fprintf(stderr, "\n");
3514520Snw141292 	}
3524520Snw141292 	(void) vsyslog(pri, format, args);
3534520Snw141292 	va_end(args);
3544520Snw141292 }
355