1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Initialization routines 28 */ 29 30 #include "idmapd.h" 31 #include <signal.h> 32 #include <thread.h> 33 #include <string.h> 34 #include <errno.h> 35 #include <assert.h> 36 #include <unistd.h> 37 #include <sys/types.h> 38 #include <sys/stat.h> 39 #include <rpcsvc/daemon_utils.h> 40 41 42 int 43 init_mapping_system() 44 { 45 int rc = 0; 46 47 if (rwlock_init(&_idmapdstate.rwlk_cfg, USYNC_THREAD, NULL) != 0) 48 return (-1); 49 if ((rc = load_config()) < 0) 50 return (rc); 51 52 (void) setegid(DAEMON_GID); 53 (void) seteuid(DAEMON_UID); 54 if (init_dbs() < 0) { 55 rc = -1; 56 fini_mapping_system(); 57 } 58 (void) seteuid(0); 59 (void) setegid(0); 60 61 return (rc); 62 } 63 64 void 65 fini_mapping_system() 66 { 67 fini_dbs(); 68 } 69 70 int 71 load_config() 72 { 73 int rc; 74 if ((_idmapdstate.cfg = idmap_cfg_init()) == NULL) { 75 degrade_svc(0, "failed to initialize config"); 76 return (-1); 77 } 78 79 rc = idmap_cfg_upgrade(_idmapdstate.cfg); 80 if (rc != 0) { 81 degrade_svc(0, "fatal error while upgrading configuration"); 82 return (rc); 83 } 84 85 rc = idmap_cfg_load(_idmapdstate.cfg, 0); 86 if (rc < -1) { 87 /* Total failure */ 88 degrade_svc(0, "fatal error while loading configuration"); 89 return (rc); 90 } 91 92 if (rc != 0) 93 /* Partial failure */ 94 idmapdlog(LOG_ERR, "Various errors occurred while loading " 95 "the configuration; check the logs"); 96 97 if ((rc = idmap_cfg_start_updates()) < 0) { 98 /* Total failure */ 99 degrade_svc(0, "could not start config updater"); 100 return (rc); 101 } 102 103 idmapdlog(LOG_DEBUG, "Initial configuration loaded"); 104 105 return (0); 106 } 107 108 109 void 110 reload_gcs() 111 { 112 int i, j; 113 adutils_ad_t **new_gcs; 114 adutils_ad_t **old_gcs; 115 int new_num_gcs; 116 int old_num_gcs; 117 idmap_pg_config_t *pgcfg = &_idmapdstate.cfg->pgcfg; 118 idmap_trustedforest_t *trustfor = pgcfg->trusted_forests; 119 int num_trustfor = pgcfg->num_trusted_forests; 120 ad_disc_domainsinforest_t *domain_in_forest; 121 122 if (pgcfg->global_catalog == NULL || 123 pgcfg->global_catalog[0].host[0] == '\0') { 124 /* 125 * No GCs. Continue to use the previous AD config in case 126 * that's still good but auto-discovery had a transient failure. 127 * If that stops working we'll go into degraded mode anyways 128 * when it does. 129 */ 130 degrade_svc(0, 131 "Global Catalog servers not configured/discoverable"); 132 return; 133 } 134 135 old_gcs = _idmapdstate.gcs; 136 old_num_gcs = _idmapdstate.num_gcs; 137 138 new_num_gcs = 1 + num_trustfor; 139 new_gcs = calloc(new_num_gcs, sizeof (adutils_ad_t *)); 140 if (new_gcs == NULL) { 141 degrade_svc(0, "could not allocate AD context array " 142 "(out of memory)"); 143 return; 144 } 145 146 if (adutils_ad_alloc(&new_gcs[0], NULL, ADUTILS_AD_GLOBAL_CATALOG) != 147 ADUTILS_SUCCESS) { 148 free(new_gcs); 149 degrade_svc(0, "could not initialize AD context " 150 "(out of memory)"); 151 return; 152 } 153 154 for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++) { 155 if (idmap_add_ds(new_gcs[0], 156 pgcfg->global_catalog[i].host, 157 pgcfg->global_catalog[i].port) != 0) { 158 adutils_ad_free(&new_gcs[0]); 159 free(new_gcs); 160 degrade_svc(0, "could not set AD hosts " 161 "(out of memory)"); 162 return; 163 } 164 } 165 166 if (pgcfg->domains_in_forest != NULL) { 167 for (i = 0; pgcfg->domains_in_forest[i].domain[0] != '\0'; 168 i++) { 169 if (adutils_add_domain(new_gcs[0], 170 pgcfg->domains_in_forest[i].domain, 171 pgcfg->domains_in_forest[i].sid) != 0) { 172 adutils_ad_free(&new_gcs[0]); 173 free(new_gcs); 174 degrade_svc(0, "could not set AD domains " 175 "(out of memory)"); 176 return; 177 } 178 } 179 } 180 181 for (i = 0; i < num_trustfor; i++) { 182 if (adutils_ad_alloc(&new_gcs[i + 1], NULL, 183 ADUTILS_AD_GLOBAL_CATALOG) != ADUTILS_SUCCESS) { 184 degrade_svc(0, "could not initialize trusted AD " 185 "context (out of memory)"); 186 new_num_gcs = i + 1; 187 goto out; 188 } 189 for (j = 0; trustfor[i].global_catalog[j].host[0] != '\0'; 190 j++) { 191 if (idmap_add_ds(new_gcs[i + 1], 192 trustfor[i].global_catalog[j].host, 193 trustfor[i].global_catalog[j].port) != 0) { 194 adutils_ad_free(&new_gcs[i + 1]); 195 degrade_svc(0, "could not set trusted " 196 "AD hosts (out of memory)"); 197 new_num_gcs = i + 1; 198 goto out; 199 } 200 } 201 for (j = 0; trustfor[i].domains_in_forest[j].domain[0] != '\0'; 202 j++) { 203 domain_in_forest = &trustfor[i].domains_in_forest[j]; 204 /* Only add domains which are marked */ 205 if (domain_in_forest->trusted) { 206 if (adutils_add_domain(new_gcs[i + 1], 207 domain_in_forest->domain, 208 domain_in_forest->sid) != 0) { 209 adutils_ad_free(&new_gcs[i + 1]); 210 degrade_svc(0, "could not set trusted " 211 "AD domains (out of memory)"); 212 new_num_gcs = i + 1; 213 goto out; 214 } 215 } 216 } 217 } 218 219 out: 220 _idmapdstate.gcs = new_gcs; 221 _idmapdstate.num_gcs = new_num_gcs; 222 223 224 if (old_gcs != NULL) { 225 for (i = 0; i < old_num_gcs; i++) 226 adutils_ad_free(&old_gcs[i]); 227 free(old_gcs); 228 } 229 } 230 231 /* 232 * NEEDSWORK: This should load entries for domain servers for all known 233 * domains - the joined domain, other domains in the forest, and trusted 234 * domains in other forests. However, we don't yet discover any DCs other 235 * than the DCs for the joined domain. 236 */ 237 static 238 void 239 reload_dcs(void) 240 { 241 int i; 242 adutils_ad_t **new_dcs; 243 adutils_ad_t **old_dcs; 244 int new_num_dcs; 245 int old_num_dcs; 246 idmap_pg_config_t *pgcfg = &_idmapdstate.cfg->pgcfg; 247 248 if (pgcfg->domain_controller == NULL || 249 pgcfg->domain_controller[0].host[0] == '\0') { 250 /* 251 * No DCs. Continue to use the previous AD config in case 252 * that's still good but auto-discovery had a transient failure. 253 * If that stops working we'll go into degraded mode anyways 254 * when it does. 255 */ 256 degrade_svc(0, 257 "Domain controller servers not configured/discoverable"); 258 return; 259 } 260 261 old_dcs = _idmapdstate.dcs; 262 old_num_dcs = _idmapdstate.num_dcs; 263 264 new_num_dcs = 1; 265 new_dcs = calloc(new_num_dcs, sizeof (adutils_ad_t *)); 266 if (new_dcs == NULL) 267 goto nomem; 268 269 if (adutils_ad_alloc(&new_dcs[0], pgcfg->domain_name, 270 ADUTILS_AD_DATA) != ADUTILS_SUCCESS) 271 goto nomem; 272 273 for (i = 0; pgcfg->domain_controller[i].host[0] != '\0'; i++) { 274 if (idmap_add_ds(new_dcs[0], 275 pgcfg->domain_controller[i].host, 276 pgcfg->domain_controller[i].port) != 0) 277 goto nomem; 278 } 279 280 /* NEEDSWORK: isn't there an easier way to find the domain SID? */ 281 for (i = 0; pgcfg->domains_in_forest[i].domain[0] != '\0'; i++) { 282 if (domain_eq(pgcfg->domain_name, 283 pgcfg->domains_in_forest[i].domain)) { 284 if (adutils_add_domain(new_dcs[0], pgcfg->domain_name, 285 pgcfg->domains_in_forest[i].sid) != 0) 286 goto nomem; 287 break; 288 } 289 } 290 291 _idmapdstate.dcs = new_dcs; 292 _idmapdstate.num_dcs = new_num_dcs; 293 294 if (old_dcs != NULL) { 295 for (i = 0; i < old_num_dcs; i++) 296 adutils_ad_free(&old_dcs[i]); 297 free(old_dcs); 298 } 299 300 return; 301 302 nomem: 303 degrade_svc(0, "out of memory"); 304 305 if (new_dcs != NULL) { 306 if (new_dcs[0] != NULL) 307 adutils_ad_free(&new_dcs[0]); 308 free(new_dcs); 309 } 310 } 311 312 313 void 314 reload_ad(void) 315 { 316 reload_gcs(); 317 reload_dcs(); 318 } 319 320 void 321 print_idmapdstate() 322 { 323 int i, j; 324 idmap_pg_config_t *pgcfg; 325 idmap_trustedforest_t *tf; 326 327 RDLOCK_CONFIG(); 328 329 if (_idmapdstate.cfg == NULL) { 330 idmapdlog(LOG_INFO, "Null configuration"); 331 UNLOCK_CONFIG(); 332 return; 333 } 334 335 pgcfg = &_idmapdstate.cfg->pgcfg; 336 337 idmapdlog(LOG_DEBUG, "list_size_limit=%llu", pgcfg->list_size_limit); 338 idmapdlog(LOG_DEBUG, "default_domain=%s", 339 CHECK_NULL(pgcfg->default_domain)); 340 idmapdlog(LOG_DEBUG, "domain_name=%s", CHECK_NULL(pgcfg->domain_name)); 341 idmapdlog(LOG_DEBUG, "machine_sid=%s", CHECK_NULL(pgcfg->machine_sid)); 342 if (pgcfg->domain_controller == NULL || 343 pgcfg->domain_controller[0].host[0] == '\0') { 344 idmapdlog(LOG_DEBUG, "No domain controllers known"); 345 } else { 346 for (i = 0; pgcfg->domain_controller[i].host[0] != '\0'; i++) 347 idmapdlog(LOG_DEBUG, "domain_controller=%s port=%d", 348 pgcfg->domain_controller[i].host, 349 pgcfg->domain_controller[i].port); 350 } 351 idmapdlog(LOG_DEBUG, "forest_name=%s", CHECK_NULL(pgcfg->forest_name)); 352 idmapdlog(LOG_DEBUG, "site_name=%s", CHECK_NULL(pgcfg->site_name)); 353 if (pgcfg->global_catalog == NULL || 354 pgcfg->global_catalog[0].host[0] == '\0') { 355 idmapdlog(LOG_DEBUG, "No global catalog servers known"); 356 } else { 357 for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++) 358 idmapdlog(LOG_DEBUG, "global_catalog=%s port=%d", 359 pgcfg->global_catalog[i].host, 360 pgcfg->global_catalog[i].port); 361 } 362 if (pgcfg->domains_in_forest == NULL || 363 pgcfg->domains_in_forest[0].domain[0] == '\0') { 364 idmapdlog(LOG_DEBUG, "No domains in forest %s known", 365 CHECK_NULL(pgcfg->forest_name)); 366 } else { 367 for (i = 0; pgcfg->domains_in_forest[i].domain[0] != '\0'; i++) 368 idmapdlog(LOG_DEBUG, "domains in forest %s = %s", 369 CHECK_NULL(pgcfg->forest_name), 370 pgcfg->domains_in_forest[i].domain); 371 } 372 if (pgcfg->trusted_domains == NULL || 373 pgcfg->trusted_domains[0].domain[0] == '\0') { 374 idmapdlog(LOG_DEBUG, "No trusted domains known"); 375 } else { 376 for (i = 0; pgcfg->trusted_domains[i].domain[0] != '\0'; i++) 377 idmapdlog(LOG_DEBUG, "trusted domain = %s", 378 pgcfg->trusted_domains[i].domain); 379 } 380 381 for (i = 0; i < pgcfg->num_trusted_forests; i++) { 382 tf = &pgcfg->trusted_forests[i]; 383 for (j = 0; tf->global_catalog[j].host[0] != '\0'; j++) 384 idmapdlog(LOG_DEBUG, 385 "trusted forest %s global_catalog=%s port=%d", 386 tf->forest_name, 387 tf->global_catalog[j].host, 388 tf->global_catalog[j].port); 389 for (j = 0; tf->domains_in_forest[j].domain[0] != '\0'; j++) { 390 if (tf->domains_in_forest[j].trusted) { 391 idmapdlog(LOG_DEBUG, 392 "trusted forest %s domain=%s", 393 tf->forest_name, 394 tf->domains_in_forest[j].domain); 395 } 396 } 397 } 398 399 idmapdlog(LOG_DEBUG, "directory_based_mapping=%s", 400 enum_lookup(pgcfg->directory_based_mapping, directory_mapping_map)); 401 idmapdlog(LOG_DEBUG, "ad_unixuser_attr=%s", 402 CHECK_NULL(pgcfg->ad_unixuser_attr)); 403 idmapdlog(LOG_DEBUG, "ad_unixgroup_attr=%s", 404 CHECK_NULL(pgcfg->ad_unixgroup_attr)); 405 idmapdlog(LOG_DEBUG, "nldap_winname_attr=%s", 406 CHECK_NULL(pgcfg->nldap_winname_attr)); 407 408 UNLOCK_CONFIG(); 409 } 410 411 int 412 create_directory(const char *path, uid_t uid, gid_t gid) 413 { 414 int rc; 415 416 if ((rc = mkdir(path, 0700)) < 0 && errno != EEXIST) { 417 idmapdlog(LOG_ERR, "Error creating directory %s (%s)", 418 path, strerror(errno)); 419 return (-1); 420 } 421 422 if (lchown(path, uid, gid) < 0) { 423 idmapdlog(LOG_ERR, "Error creating directory %s (%s)", 424 path, strerror(errno)); 425 if (rc == 0) 426 (void) rmdir(path); 427 return (-1); 428 } 429 return (0); 430 } 431