xref: /onnv-gate/usr/src/cmd/idmap/idmapd/init.c (revision 6097)
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 /*
225908Sjp151216  * 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  * Initialization routines
304520Snw141292  */
314520Snw141292 
324520Snw141292 #include "idmapd.h"
334520Snw141292 #include <signal.h>
344520Snw141292 #include <thread.h>
354520Snw141292 #include <string.h>
364520Snw141292 #include <errno.h>
374520Snw141292 #include <assert.h>
384520Snw141292 #include <unistd.h>
394520Snw141292 #include <sys/types.h>
404520Snw141292 #include <sys/stat.h>
414695Sbaban #include <rpcsvc/daemon_utils.h>
424520Snw141292 
434520Snw141292 
444520Snw141292 int
455908Sjp151216 init_mapping_system()
465908Sjp151216 {
474695Sbaban 	int rc = 0;
484695Sbaban 
494520Snw141292 	if (rwlock_init(&_idmapdstate.rwlk_cfg, USYNC_THREAD, NULL) != 0)
504520Snw141292 		return (-1);
515731Sbaban 	if ((rc = load_config()) < 0)
525731Sbaban 		return (rc);
534695Sbaban 
544695Sbaban 	(void) setegid(DAEMON_GID);
554695Sbaban 	(void) seteuid(DAEMON_UID);
564520Snw141292 	if (init_dbs() < 0) {
574695Sbaban 		rc = -1;
584520Snw141292 		fini_mapping_system();
594520Snw141292 	}
604695Sbaban 	(void) seteuid(0);
614695Sbaban 	(void) setegid(0);
624695Sbaban 
634695Sbaban 	return (rc);
644520Snw141292 }
654520Snw141292 
664520Snw141292 void
675908Sjp151216 fini_mapping_system()
685908Sjp151216 {
694520Snw141292 	fini_dbs();
704520Snw141292 }
714520Snw141292 
724520Snw141292 int
735908Sjp151216 load_config()
745908Sjp151216 {
755447Snw141292 	int rc;
764520Snw141292 	if ((_idmapdstate.cfg = idmap_cfg_init()) == NULL) {
77*6097Snw141292 		degrade_svc(0, "failed to initialize config");
784520Snw141292 		return (-1);
794520Snw141292 	}
805317Sjp151216 
81*6097Snw141292 	rc = idmap_cfg_load(_idmapdstate.cfg, 0);
825447Snw141292 	if (rc < -1) {
835447Snw141292 		/* Total failure */
84*6097Snw141292 		degrade_svc(0, "fatal error while loading configuration");
855731Sbaban 		return (rc);
864520Snw141292 	}
875317Sjp151216 
885447Snw141292 	if (rc != 0)
895447Snw141292 		/* Partial failure */
906017Snw141292 		idmapdlog(LOG_ERR, "Various errors occurred while loading "
916017Snw141292 		    "the configuration; check the logs");
925447Snw141292 
935968Snw141292 	if ((rc = idmap_cfg_start_updates()) < 0) {
945968Snw141292 		/* Total failure */
95*6097Snw141292 		degrade_svc(0, "could not start config updater");
965968Snw141292 		return (rc);
975968Snw141292 	}
985447Snw141292 
996017Snw141292 	idmapdlog(LOG_DEBUG, "Initial configuration loaded");
1005447Snw141292 
1015317Sjp151216 	return (0);
1025317Sjp151216 }
1035317Sjp151216 
1045317Sjp151216 
105*6097Snw141292 void
1065908Sjp151216 reload_ad()
1075908Sjp151216 {
1085317Sjp151216 	int	i;
1095317Sjp151216 	ad_t	*old;
1105317Sjp151216 	ad_t	*new;
1115317Sjp151216 
1125317Sjp151216 	idmap_pg_config_t *pgcfg = &_idmapdstate.cfg->pgcfg;
1135317Sjp151216 
114*6097Snw141292 	if (pgcfg->global_catalog == NULL ||
115*6097Snw141292 	    pgcfg->global_catalog[0].host[0] == '\0') {
116*6097Snw141292 		/*
117*6097Snw141292 		 * No GCs.  Continue to use the previous AD config in case
118*6097Snw141292 		 * that's still good but auto-discovery had a transient failure.
119*6097Snw141292 		 * If that stops working we'll go into degraded mode anyways
120*6097Snw141292 		 * when it does.
121*6097Snw141292 		 */
122*6097Snw141292 		degrade_svc(0,
123*6097Snw141292 		    "Global Catalog servers not configured/discoverable");
124*6097Snw141292 		return;
1254520Snw141292 	}
1265317Sjp151216 
1275317Sjp151216 	old = _idmapdstate.ad;
1285317Sjp151216 
1295317Sjp151216 	if (idmap_ad_alloc(&new, pgcfg->default_domain,
1305317Sjp151216 	    IDMAP_AD_GLOBAL_CATALOG) != 0) {
131*6097Snw141292 		degrade_svc(0, "could not initialize AD context");
132*6097Snw141292 		return;
1334520Snw141292 	}
1345317Sjp151216 
1355317Sjp151216 	for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++) {
1365317Sjp151216 		if (idmap_add_ds(new,
1375317Sjp151216 		    pgcfg->global_catalog[i].host,
1385317Sjp151216 		    pgcfg->global_catalog[i].port) != 0) {
1395317Sjp151216 			idmap_ad_free(&new);
140*6097Snw141292 			degrade_svc(0, "could not initialize AD GC context");
141*6097Snw141292 			return;
1425317Sjp151216 		}
1435317Sjp151216 	}
1445317Sjp151216 
1455317Sjp151216 	_idmapdstate.ad = new;
1465317Sjp151216 
1475317Sjp151216 	if (old != NULL)
1485317Sjp151216 		idmap_ad_free(&old);
1494520Snw141292 }
1504520Snw141292 
1515317Sjp151216 
1524520Snw141292 void
1535908Sjp151216 print_idmapdstate()
1545908Sjp151216 {
1555317Sjp151216 	int i;
1565731Sbaban 	idmap_pg_config_t *pgcfg;
1575317Sjp151216 
1584520Snw141292 	RDLOCK_CONFIG();
1594520Snw141292 
1605317Sjp151216 	if (_idmapdstate.cfg == NULL) {
1616017Snw141292 		idmapdlog(LOG_INFO, "Null configuration");
1625317Sjp151216 		UNLOCK_CONFIG();
1635317Sjp151216 		return;
1644520Snw141292 	}
1655317Sjp151216 
1665731Sbaban 	pgcfg = &_idmapdstate.cfg->pgcfg;
1675731Sbaban 
1686017Snw141292 	idmapdlog(LOG_DEBUG, "list_size_limit=%llu", pgcfg->list_size_limit);
1696017Snw141292 	idmapdlog(LOG_DEBUG, "default_domain=%s",
1705317Sjp151216 	    CHECK_NULL(pgcfg->default_domain));
1716017Snw141292 	idmapdlog(LOG_DEBUG, "domain_name=%s", CHECK_NULL(pgcfg->domain_name));
1726017Snw141292 	idmapdlog(LOG_DEBUG, "machine_sid=%s", CHECK_NULL(pgcfg->machine_sid));
1735317Sjp151216 	if (pgcfg->domain_controller == NULL ||
1745317Sjp151216 	    pgcfg->domain_controller[0].host[0] == '\0') {
1756017Snw141292 		idmapdlog(LOG_DEBUG, "No domain controllers known");
1765317Sjp151216 	} else {
1775317Sjp151216 		for (i = 0; pgcfg->domain_controller[i].host[0] != '\0'; i++)
1786017Snw141292 			idmapdlog(LOG_DEBUG, "domain_controller=%s port=%d",
1796017Snw141292 			    pgcfg->domain_controller[i].host,
1805317Sjp151216 			    pgcfg->domain_controller[i].port);
1815317Sjp151216 	}
1826017Snw141292 	idmapdlog(LOG_DEBUG, "forest_name=%s", CHECK_NULL(pgcfg->forest_name));
1836017Snw141292 	idmapdlog(LOG_DEBUG, "site_name=%s", CHECK_NULL(pgcfg->site_name));
1845317Sjp151216 	if (pgcfg->global_catalog == NULL ||
1855317Sjp151216 	    pgcfg->global_catalog[0].host[0] == '\0') {
1866017Snw141292 		idmapdlog(LOG_DEBUG, "No global catalog servers known");
1875317Sjp151216 	} else {
1885317Sjp151216 		for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++)
1896017Snw141292 			idmapdlog(LOG_DEBUG, "global_catalog=%s port=%d",
1905317Sjp151216 			    pgcfg->global_catalog[i].host,
1915317Sjp151216 			    pgcfg->global_catalog[i].port);
1925317Sjp151216 	}
1936017Snw141292 	idmapdlog(LOG_DEBUG, "ds_name_mapping_enabled=%s",
1945731Sbaban 	    (pgcfg->ds_name_mapping_enabled == TRUE) ? "true" : "false");
1956017Snw141292 	idmapdlog(LOG_DEBUG, "ad_unixuser_attr=%s",
1965731Sbaban 	    CHECK_NULL(pgcfg->ad_unixuser_attr));
1976017Snw141292 	idmapdlog(LOG_DEBUG, "ad_unixgroup_attr=%s",
1985731Sbaban 	    CHECK_NULL(pgcfg->ad_unixgroup_attr));
1996017Snw141292 	idmapdlog(LOG_DEBUG, "nldap_winname_attr=%s",
2005731Sbaban 	    CHECK_NULL(pgcfg->nldap_winname_attr));
2015317Sjp151216 
2024520Snw141292 	UNLOCK_CONFIG();
2034520Snw141292 }
2044520Snw141292 
2054520Snw141292 int
2065908Sjp151216 create_directory(const char *path, uid_t uid, gid_t gid)
2075908Sjp151216 {
2084520Snw141292 	int	rc;
2094520Snw141292 
2104520Snw141292 	if ((rc = mkdir(path, 0700)) < 0 && errno != EEXIST) {
2116017Snw141292 		idmapdlog(LOG_ERR, "Error creating directory %s (%s)",
2126017Snw141292 		    path, strerror(errno));
2134520Snw141292 		return (-1);
2144520Snw141292 	}
2154520Snw141292 
2164520Snw141292 	if (lchown(path, uid, gid) < 0) {
2176017Snw141292 		idmapdlog(LOG_ERR, "Error creating directory %s (%s)",
2186017Snw141292 		    path, strerror(errno));
2194520Snw141292 		if (rc == 0)
2204520Snw141292 			(void) rmdir(path);
2214520Snw141292 		return (-1);
2224520Snw141292 	}
2234520Snw141292 	return (0);
2244520Snw141292 }
225