xref: /onnv-gate/usr/src/cmd/idmap/idmapd/init.c (revision 5731:dd7b211a2da6)
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 /*
224520Snw141292  * Copyright 2007 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 static const char *me = "idmapd";
444520Snw141292 
454520Snw141292 int
464520Snw141292 init_mapping_system() {
474695Sbaban 	int rc = 0;
484695Sbaban 
494520Snw141292 	if (rwlock_init(&_idmapdstate.rwlk_cfg, USYNC_THREAD, NULL) != 0)
504520Snw141292 		return (-1);
51*5731Sbaban 	if ((rc = load_config()) < 0)
52*5731Sbaban 		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
674520Snw141292 fini_mapping_system() {
684520Snw141292 	fini_dbs();
694520Snw141292 }
704520Snw141292 
714520Snw141292 int
724520Snw141292 load_config() {
735447Snw141292 	int rc;
745317Sjp151216 	idmap_pg_config_t *pgcfg;
754520Snw141292 	if ((_idmapdstate.cfg = idmap_cfg_init()) == NULL) {
764644Sbaban 		idmapdlog(LOG_ERR, "%s: failed to initialize config", me);
775317Sjp151216 		degrade_svc();
784520Snw141292 		return (-1);
794520Snw141292 	}
805317Sjp151216 	pgcfg = &_idmapdstate.cfg->pgcfg;
815317Sjp151216 
825447Snw141292 	rc = idmap_cfg_load(&_idmapdstate.cfg->handles,
835447Snw141292 	    &_idmapdstate.cfg->pgcfg, 0);
845447Snw141292 	if (rc < -1) {
855447Snw141292 		/* Total failure */
865317Sjp151216 		degrade_svc();
875447Snw141292 		idmapdlog(LOG_ERR, "%s: Fatal error while loading "
885447Snw141292 		    "configuration", me);
89*5731Sbaban 		return (rc);
904520Snw141292 	}
915317Sjp151216 
925447Snw141292 	if (rc != 0)
935447Snw141292 		/* Partial failure */
945447Snw141292 		idmapdlog(LOG_ERR, "%s: Various errors occurred while loading "
955447Snw141292 			"the configuration; check the logs", me);
965447Snw141292 
975317Sjp151216 	if (pgcfg->global_catalog == NULL ||
985317Sjp151216 	    pgcfg->global_catalog[0].host[0] == '\0') {
995317Sjp151216 		degrade_svc();
1005447Snw141292 		idmapdlog(LOG_INFO,
1015447Snw141292 		    "%s: Global catalog server is not configured; AD lookup "
1025447Snw141292 		    "will fail until one or more global catalog server names "
1035447Snw141292 		    "are configured or discovered; auto-discovery will begin "
1045447Snw141292 		    "shortly", me);
1055447Snw141292 	} else {
1065447Snw141292 		restore_svc();
1075317Sjp151216 	}
1085317Sjp151216 
1095317Sjp151216 	(void) reload_ad();
1105317Sjp151216 
1115317Sjp151216 	if (idmap_cfg_start_updates(_idmapdstate.cfg) < 0)
1125317Sjp151216 		idmapdlog(LOG_ERR, "%s: could not start config updater",
1134520Snw141292 			me);
1145447Snw141292 
1155447Snw141292 	idmapdlog(LOG_DEBUG, "%s: initial configuration loaded", me);
1165447Snw141292 
1175317Sjp151216 	return (0);
1185317Sjp151216 }
1195317Sjp151216 
1205317Sjp151216 
1215317Sjp151216 int
1225317Sjp151216 reload_ad() {
1235317Sjp151216 	int	i;
1245317Sjp151216 	ad_t	*old;
1255317Sjp151216 	ad_t	*new;
1265317Sjp151216 
1275317Sjp151216 	idmap_pg_config_t *pgcfg = &_idmapdstate.cfg->pgcfg;
1285317Sjp151216 
1295317Sjp151216 	if (pgcfg->default_domain == NULL ||
1305317Sjp151216 	    pgcfg->global_catalog == NULL) {
1315317Sjp151216 		if (_idmapdstate.ad == NULL)
1325317Sjp151216 			idmapdlog(LOG_ERR, "%s: AD lookup disabled", me);
1335317Sjp151216 		else
1345317Sjp151216 			idmapdlog(LOG_ERR, "%s: cannot update AD context", me);
1354520Snw141292 		return (-1);
1364520Snw141292 	}
1375317Sjp151216 
1385317Sjp151216 	old = _idmapdstate.ad;
1395317Sjp151216 
1405317Sjp151216 	if (idmap_ad_alloc(&new, pgcfg->default_domain,
1415317Sjp151216 	    IDMAP_AD_GLOBAL_CATALOG) != 0) {
1425317Sjp151216 		if (old == NULL)
1435317Sjp151216 			degrade_svc();
1445317Sjp151216 		idmapdlog(LOG_ERR, "%s: could not initialize AD context", me);
1454520Snw141292 		return (-1);
1464520Snw141292 	}
1475317Sjp151216 
1485317Sjp151216 	for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++) {
1495317Sjp151216 		if (idmap_add_ds(new,
1505317Sjp151216 		    pgcfg->global_catalog[i].host,
1515317Sjp151216 		    pgcfg->global_catalog[i].port) != 0) {
1525317Sjp151216 			idmap_ad_free(&new);
1535317Sjp151216 			if (old == NULL)
1545317Sjp151216 				degrade_svc();
1555317Sjp151216 			idmapdlog(LOG_ERR,
1565317Sjp151216 			    "%s: could not initialize AD DS context", me);
1575317Sjp151216 			return (-1);
1585317Sjp151216 		}
1595317Sjp151216 	}
1605317Sjp151216 
1615317Sjp151216 	_idmapdstate.ad = new;
1625317Sjp151216 
1635317Sjp151216 	if (old != NULL)
1645317Sjp151216 		idmap_ad_free(&old);
1655317Sjp151216 
1664520Snw141292 	return (0);
1674520Snw141292 }
1684520Snw141292 
1695317Sjp151216 
1704520Snw141292 void
1714520Snw141292 print_idmapdstate() {
1725317Sjp151216 	int i;
173*5731Sbaban 	idmap_pg_config_t *pgcfg;
1745317Sjp151216 
1754520Snw141292 	RDLOCK_CONFIG();
1764520Snw141292 
1775317Sjp151216 	if (_idmapdstate.cfg == NULL) {
1785317Sjp151216 		idmapdlog(LOG_INFO, "%s: Null configuration", me);
1795317Sjp151216 		UNLOCK_CONFIG();
1805317Sjp151216 		return;
1814520Snw141292 	}
1825317Sjp151216 
183*5731Sbaban 	pgcfg = &_idmapdstate.cfg->pgcfg;
184*5731Sbaban 
1855317Sjp151216 	idmapdlog(LOG_DEBUG, "%s: list_size_limit=%llu", me,
1865317Sjp151216 	    pgcfg->list_size_limit);
1875317Sjp151216 	idmapdlog(LOG_DEBUG, "%s: default_domain=%s", me,
1885317Sjp151216 	    CHECK_NULL(pgcfg->default_domain));
1895317Sjp151216 	idmapdlog(LOG_DEBUG, "%s: domain_name=%s", me,
1905317Sjp151216 	    CHECK_NULL(pgcfg->domain_name));
1915317Sjp151216 	idmapdlog(LOG_DEBUG, "%s: machine_sid=%s", me,
1925317Sjp151216 	    CHECK_NULL(pgcfg->machine_sid));
1935317Sjp151216 	if (pgcfg->domain_controller == NULL ||
1945317Sjp151216 	    pgcfg->domain_controller[0].host[0] == '\0') {
1955317Sjp151216 		idmapdlog(LOG_DEBUG, "%s: No domain controllers known", me);
1965317Sjp151216 	} else {
1975317Sjp151216 		for (i = 0; pgcfg->domain_controller[i].host[0] != '\0'; i++)
1985317Sjp151216 			idmapdlog(LOG_DEBUG, "%s: domain_controller=%s port=%d",
1995317Sjp151216 			    me, pgcfg->domain_controller[i].host,
2005317Sjp151216 			    pgcfg->domain_controller[i].port);
2015317Sjp151216 	}
2025317Sjp151216 	idmapdlog(LOG_DEBUG, "%s: forest_name=%s", me,
2035317Sjp151216 	    CHECK_NULL(pgcfg->forest_name));
2045317Sjp151216 	idmapdlog(LOG_DEBUG, "%s: site_name=%s", me,
2055317Sjp151216 	    CHECK_NULL(pgcfg->site_name));
2065317Sjp151216 	if (pgcfg->global_catalog == NULL ||
2075317Sjp151216 	    pgcfg->global_catalog[0].host[0] == '\0') {
2085317Sjp151216 		idmapdlog(LOG_DEBUG, "%s: No global catalog servers known", me);
2095317Sjp151216 	} else {
2105317Sjp151216 		for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++)
2115317Sjp151216 			idmapdlog(LOG_DEBUG, "%s: global_catalog=%s port=%d",
2125317Sjp151216 			    me,
2135317Sjp151216 			    pgcfg->global_catalog[i].host,
2145317Sjp151216 			    pgcfg->global_catalog[i].port);
2155317Sjp151216 	}
216*5731Sbaban 	idmapdlog(LOG_DEBUG, "%s: ds_name_mapping_enabled=%s", me,
217*5731Sbaban 	    (pgcfg->ds_name_mapping_enabled == TRUE) ? "true" : "false");
218*5731Sbaban 	idmapdlog(LOG_DEBUG, "%s: ad_unixuser_attr=%s", me,
219*5731Sbaban 	    CHECK_NULL(pgcfg->ad_unixuser_attr));
220*5731Sbaban 	idmapdlog(LOG_DEBUG, "%s: ad_unixgroup_attr=%s", me,
221*5731Sbaban 	    CHECK_NULL(pgcfg->ad_unixgroup_attr));
222*5731Sbaban 	idmapdlog(LOG_DEBUG, "%s: nldap_winname_attr=%s", me,
223*5731Sbaban 	    CHECK_NULL(pgcfg->nldap_winname_attr));
2245317Sjp151216 
2254520Snw141292 	UNLOCK_CONFIG();
2264520Snw141292 }
2274520Snw141292 
2284520Snw141292 int
2294520Snw141292 create_directory(const char *path, uid_t uid, gid_t gid) {
2304520Snw141292 	int	rc;
2314520Snw141292 
2324520Snw141292 	if ((rc = mkdir(path, 0700)) < 0 && errno != EEXIST) {
2334520Snw141292 		idmapdlog(LOG_ERR,
2344520Snw141292 			"%s: Error creating directory %s (%s)",
2354520Snw141292 			me, path, strerror(errno));
2364520Snw141292 		return (-1);
2374520Snw141292 	}
2384520Snw141292 
2394520Snw141292 	if (lchown(path, uid, gid) < 0) {
2404520Snw141292 		idmapdlog(LOG_ERR,
2414520Snw141292 			"%s: Error creating directory %s (%s)",
2424520Snw141292 			me, path, strerror(errno));
2434520Snw141292 		if (rc == 0)
2444520Snw141292 			(void) rmdir(path);
2454520Snw141292 		return (-1);
2464520Snw141292 	}
2474520Snw141292 	return (0);
2484520Snw141292 }
249