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 /* 274520Snw141292 * Initialization routines 284520Snw141292 */ 294520Snw141292 304520Snw141292 #include "idmapd.h" 314520Snw141292 #include <signal.h> 324520Snw141292 #include <thread.h> 334520Snw141292 #include <string.h> 344520Snw141292 #include <errno.h> 354520Snw141292 #include <assert.h> 364520Snw141292 #include <unistd.h> 374520Snw141292 #include <sys/types.h> 384520Snw141292 #include <sys/stat.h> 394695Sbaban #include <rpcsvc/daemon_utils.h> 404520Snw141292 414520Snw141292 424520Snw141292 int 435908Sjp151216 init_mapping_system() 445908Sjp151216 { 454695Sbaban int rc = 0; 464695Sbaban 474520Snw141292 if (rwlock_init(&_idmapdstate.rwlk_cfg, USYNC_THREAD, NULL) != 0) 484520Snw141292 return (-1); 495731Sbaban if ((rc = load_config()) < 0) 505731Sbaban return (rc); 514695Sbaban 524695Sbaban (void) setegid(DAEMON_GID); 534695Sbaban (void) seteuid(DAEMON_UID); 544520Snw141292 if (init_dbs() < 0) { 554695Sbaban rc = -1; 564520Snw141292 fini_mapping_system(); 574520Snw141292 } 584695Sbaban (void) seteuid(0); 594695Sbaban (void) setegid(0); 604695Sbaban 614695Sbaban return (rc); 624520Snw141292 } 634520Snw141292 644520Snw141292 void 655908Sjp151216 fini_mapping_system() 665908Sjp151216 { 674520Snw141292 fini_dbs(); 684520Snw141292 } 694520Snw141292 704520Snw141292 int 715908Sjp151216 load_config() 725908Sjp151216 { 735447Snw141292 int rc; 744520Snw141292 if ((_idmapdstate.cfg = idmap_cfg_init()) == NULL) { 756097Snw141292 degrade_svc(0, "failed to initialize config"); 764520Snw141292 return (-1); 774520Snw141292 } 785317Sjp151216 796097Snw141292 rc = idmap_cfg_load(_idmapdstate.cfg, 0); 805447Snw141292 if (rc < -1) { 815447Snw141292 /* Total failure */ 826097Snw141292 degrade_svc(0, "fatal error while loading configuration"); 835731Sbaban return (rc); 844520Snw141292 } 855317Sjp151216 865447Snw141292 if (rc != 0) 875447Snw141292 /* Partial failure */ 886017Snw141292 idmapdlog(LOG_ERR, "Various errors occurred while loading " 896017Snw141292 "the configuration; check the logs"); 905447Snw141292 915968Snw141292 if ((rc = idmap_cfg_start_updates()) < 0) { 925968Snw141292 /* Total failure */ 936097Snw141292 degrade_svc(0, "could not start config updater"); 945968Snw141292 return (rc); 955968Snw141292 } 965447Snw141292 976017Snw141292 idmapdlog(LOG_DEBUG, "Initial configuration loaded"); 985447Snw141292 995317Sjp151216 return (0); 1005317Sjp151216 } 1015317Sjp151216 1025317Sjp151216 1036097Snw141292 void 1045908Sjp151216 reload_ad() 1055908Sjp151216 { 106*8040SBaban.Kenkre@Sun.COM int i; 107*8040SBaban.Kenkre@Sun.COM adutils_ad_t *old; 108*8040SBaban.Kenkre@Sun.COM adutils_ad_t *new; 1095317Sjp151216 1105317Sjp151216 idmap_pg_config_t *pgcfg = &_idmapdstate.cfg->pgcfg; 1115317Sjp151216 1126097Snw141292 if (pgcfg->global_catalog == NULL || 1136097Snw141292 pgcfg->global_catalog[0].host[0] == '\0') { 1146097Snw141292 /* 1156097Snw141292 * No GCs. Continue to use the previous AD config in case 1166097Snw141292 * that's still good but auto-discovery had a transient failure. 1176097Snw141292 * If that stops working we'll go into degraded mode anyways 1186097Snw141292 * when it does. 1196097Snw141292 */ 1206097Snw141292 degrade_svc(0, 1216097Snw141292 "Global Catalog servers not configured/discoverable"); 1226097Snw141292 return; 1234520Snw141292 } 1245317Sjp151216 1255317Sjp151216 old = _idmapdstate.ad; 1265317Sjp151216 127*8040SBaban.Kenkre@Sun.COM if (adutils_ad_alloc(&new, pgcfg->default_domain, 128*8040SBaban.Kenkre@Sun.COM ADUTILS_AD_GLOBAL_CATALOG) != ADUTILS_SUCCESS) { 1296097Snw141292 degrade_svc(0, "could not initialize AD context"); 1306097Snw141292 return; 1314520Snw141292 } 1325317Sjp151216 1335317Sjp151216 for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++) { 1345317Sjp151216 if (idmap_add_ds(new, 1355317Sjp151216 pgcfg->global_catalog[i].host, 1365317Sjp151216 pgcfg->global_catalog[i].port) != 0) { 137*8040SBaban.Kenkre@Sun.COM adutils_ad_free(&new); 1386097Snw141292 degrade_svc(0, "could not initialize AD GC context"); 1396097Snw141292 return; 1405317Sjp151216 } 1415317Sjp151216 } 1425317Sjp151216 1435317Sjp151216 _idmapdstate.ad = new; 1445317Sjp151216 1455317Sjp151216 if (old != NULL) 146*8040SBaban.Kenkre@Sun.COM adutils_ad_free(&old); 1474520Snw141292 } 1484520Snw141292 1495317Sjp151216 1504520Snw141292 void 1515908Sjp151216 print_idmapdstate() 1525908Sjp151216 { 1535317Sjp151216 int i; 1545731Sbaban idmap_pg_config_t *pgcfg; 1555317Sjp151216 1564520Snw141292 RDLOCK_CONFIG(); 1574520Snw141292 1585317Sjp151216 if (_idmapdstate.cfg == NULL) { 1596017Snw141292 idmapdlog(LOG_INFO, "Null configuration"); 1605317Sjp151216 UNLOCK_CONFIG(); 1615317Sjp151216 return; 1624520Snw141292 } 1635317Sjp151216 1645731Sbaban pgcfg = &_idmapdstate.cfg->pgcfg; 1655731Sbaban 1666017Snw141292 idmapdlog(LOG_DEBUG, "list_size_limit=%llu", pgcfg->list_size_limit); 1676017Snw141292 idmapdlog(LOG_DEBUG, "default_domain=%s", 1685317Sjp151216 CHECK_NULL(pgcfg->default_domain)); 1696017Snw141292 idmapdlog(LOG_DEBUG, "domain_name=%s", CHECK_NULL(pgcfg->domain_name)); 1706017Snw141292 idmapdlog(LOG_DEBUG, "machine_sid=%s", CHECK_NULL(pgcfg->machine_sid)); 1715317Sjp151216 if (pgcfg->domain_controller == NULL || 1725317Sjp151216 pgcfg->domain_controller[0].host[0] == '\0') { 1736017Snw141292 idmapdlog(LOG_DEBUG, "No domain controllers known"); 1745317Sjp151216 } else { 1755317Sjp151216 for (i = 0; pgcfg->domain_controller[i].host[0] != '\0'; i++) 1766017Snw141292 idmapdlog(LOG_DEBUG, "domain_controller=%s port=%d", 1776017Snw141292 pgcfg->domain_controller[i].host, 1785317Sjp151216 pgcfg->domain_controller[i].port); 1795317Sjp151216 } 1806017Snw141292 idmapdlog(LOG_DEBUG, "forest_name=%s", CHECK_NULL(pgcfg->forest_name)); 1816017Snw141292 idmapdlog(LOG_DEBUG, "site_name=%s", CHECK_NULL(pgcfg->site_name)); 1825317Sjp151216 if (pgcfg->global_catalog == NULL || 1835317Sjp151216 pgcfg->global_catalog[0].host[0] == '\0') { 1846017Snw141292 idmapdlog(LOG_DEBUG, "No global catalog servers known"); 1855317Sjp151216 } else { 1865317Sjp151216 for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++) 1876017Snw141292 idmapdlog(LOG_DEBUG, "global_catalog=%s port=%d", 1885317Sjp151216 pgcfg->global_catalog[i].host, 1895317Sjp151216 pgcfg->global_catalog[i].port); 1905317Sjp151216 } 1916017Snw141292 idmapdlog(LOG_DEBUG, "ds_name_mapping_enabled=%s", 1925731Sbaban (pgcfg->ds_name_mapping_enabled == TRUE) ? "true" : "false"); 1936017Snw141292 idmapdlog(LOG_DEBUG, "ad_unixuser_attr=%s", 1945731Sbaban CHECK_NULL(pgcfg->ad_unixuser_attr)); 1956017Snw141292 idmapdlog(LOG_DEBUG, "ad_unixgroup_attr=%s", 1965731Sbaban CHECK_NULL(pgcfg->ad_unixgroup_attr)); 1976017Snw141292 idmapdlog(LOG_DEBUG, "nldap_winname_attr=%s", 1985731Sbaban CHECK_NULL(pgcfg->nldap_winname_attr)); 1995317Sjp151216 2004520Snw141292 UNLOCK_CONFIG(); 2014520Snw141292 } 2024520Snw141292 2034520Snw141292 int 2045908Sjp151216 create_directory(const char *path, uid_t uid, gid_t gid) 2055908Sjp151216 { 2064520Snw141292 int rc; 2074520Snw141292 2084520Snw141292 if ((rc = mkdir(path, 0700)) < 0 && errno != EEXIST) { 2096017Snw141292 idmapdlog(LOG_ERR, "Error creating directory %s (%s)", 2106017Snw141292 path, strerror(errno)); 2114520Snw141292 return (-1); 2124520Snw141292 } 2134520Snw141292 2144520Snw141292 if (lchown(path, uid, gid) < 0) { 2156017Snw141292 idmapdlog(LOG_ERR, "Error creating directory %s (%s)", 2166017Snw141292 path, strerror(errno)); 2174520Snw141292 if (rc == 0) 2184520Snw141292 (void) rmdir(path); 2194520Snw141292 return (-1); 2204520Snw141292 } 2214520Snw141292 return (0); 2224520Snw141292 } 223