xref: /titanic_51/usr/src/cmd/idmap/idmapd/init.c (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
1c5c4113dSnw141292 /*
2c5c4113dSnw141292  * CDDL HEADER START
3c5c4113dSnw141292  *
4c5c4113dSnw141292  * The contents of this file are subject to the terms of the
5c5c4113dSnw141292  * Common Development and Distribution License (the "License").
6c5c4113dSnw141292  * You may not use this file except in compliance with the License.
7c5c4113dSnw141292  *
8c5c4113dSnw141292  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c5c4113dSnw141292  * or http://www.opensolaris.org/os/licensing.
10c5c4113dSnw141292  * See the License for the specific language governing permissions
11c5c4113dSnw141292  * and limitations under the License.
12c5c4113dSnw141292  *
13c5c4113dSnw141292  * When distributing Covered Code, include this CDDL HEADER in each
14c5c4113dSnw141292  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c5c4113dSnw141292  * If applicable, add the following below this CDDL HEADER, with the
16c5c4113dSnw141292  * fields enclosed by brackets "[]" replaced with your own identifying
17c5c4113dSnw141292  * information: Portions Copyright [yyyy] [name of copyright owner]
18c5c4113dSnw141292  *
19c5c4113dSnw141292  * CDDL HEADER END
20c5c4113dSnw141292  */
21c5c4113dSnw141292 /*
224edd44c5Sjp151216  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23c5c4113dSnw141292  * Use is subject to license terms.
24c5c4113dSnw141292  */
25c5c4113dSnw141292 
26c5c4113dSnw141292 /*
27c5c4113dSnw141292  * Initialization routines
28c5c4113dSnw141292  */
29c5c4113dSnw141292 
30c5c4113dSnw141292 #include "idmapd.h"
31c5c4113dSnw141292 #include <signal.h>
32c5c4113dSnw141292 #include <thread.h>
33c5c4113dSnw141292 #include <string.h>
34c5c4113dSnw141292 #include <errno.h>
35c5c4113dSnw141292 #include <assert.h>
36c5c4113dSnw141292 #include <unistd.h>
37c5c4113dSnw141292 #include <sys/types.h>
38c5c4113dSnw141292 #include <sys/stat.h>
398edda628Sbaban #include <rpcsvc/daemon_utils.h>
40c5c4113dSnw141292 
41c5c4113dSnw141292 
42c5c4113dSnw141292 int
434edd44c5Sjp151216 init_mapping_system()
444edd44c5Sjp151216 {
458edda628Sbaban 	int rc = 0;
468edda628Sbaban 
47c5c4113dSnw141292 	if (rwlock_init(&_idmapdstate.rwlk_cfg, USYNC_THREAD, NULL) != 0)
48c5c4113dSnw141292 		return (-1);
49e8c27ec8Sbaban 	if ((rc = load_config()) < 0)
50e8c27ec8Sbaban 		return (rc);
518edda628Sbaban 
528edda628Sbaban 	(void) setegid(DAEMON_GID);
538edda628Sbaban 	(void) seteuid(DAEMON_UID);
54c5c4113dSnw141292 	if (init_dbs() < 0) {
558edda628Sbaban 		rc = -1;
56c5c4113dSnw141292 		fini_mapping_system();
57c5c4113dSnw141292 	}
588edda628Sbaban 	(void) seteuid(0);
598edda628Sbaban 	(void) setegid(0);
608edda628Sbaban 
618edda628Sbaban 	return (rc);
62c5c4113dSnw141292 }
63c5c4113dSnw141292 
64c5c4113dSnw141292 void
654edd44c5Sjp151216 fini_mapping_system()
664edd44c5Sjp151216 {
67c5c4113dSnw141292 	fini_dbs();
68c5c4113dSnw141292 }
69c5c4113dSnw141292 
70c5c4113dSnw141292 int
714edd44c5Sjp151216 load_config()
724edd44c5Sjp151216 {
73e3c2d6aaSnw141292 	int rc;
74c5c4113dSnw141292 	if ((_idmapdstate.cfg = idmap_cfg_init()) == NULL) {
75349d5d8fSnw141292 		degrade_svc(0, "failed to initialize config");
76c5c4113dSnw141292 		return (-1);
77c5c4113dSnw141292 	}
78c8e26105Sjp151216 
79349d5d8fSnw141292 	rc = idmap_cfg_load(_idmapdstate.cfg, 0);
80e3c2d6aaSnw141292 	if (rc < -1) {
81e3c2d6aaSnw141292 		/* Total failure */
82349d5d8fSnw141292 		degrade_svc(0, "fatal error while loading configuration");
83e8c27ec8Sbaban 		return (rc);
84c5c4113dSnw141292 	}
85c8e26105Sjp151216 
86e3c2d6aaSnw141292 	if (rc != 0)
87e3c2d6aaSnw141292 		/* Partial failure */
8871590c90Snw141292 		idmapdlog(LOG_ERR, "Various errors occurred while loading "
8971590c90Snw141292 		    "the configuration; check the logs");
90e3c2d6aaSnw141292 
910dcc7149Snw141292 	if ((rc = idmap_cfg_start_updates()) < 0) {
920dcc7149Snw141292 		/* Total failure */
93349d5d8fSnw141292 		degrade_svc(0, "could not start config updater");
940dcc7149Snw141292 		return (rc);
950dcc7149Snw141292 	}
96e3c2d6aaSnw141292 
9771590c90Snw141292 	idmapdlog(LOG_DEBUG, "Initial configuration loaded");
98e3c2d6aaSnw141292 
99c5c4113dSnw141292 	return (0);
100c5c4113dSnw141292 }
101c5c4113dSnw141292 
102c8e26105Sjp151216 
103349d5d8fSnw141292 void
1044edd44c5Sjp151216 reload_ad()
1054edd44c5Sjp151216 {
106c8e26105Sjp151216 	int		i;
107*2b4a7802SBaban Kenkre 	adutils_ad_t	*old;
108*2b4a7802SBaban Kenkre 	adutils_ad_t	*new;
109c8e26105Sjp151216 
110c8e26105Sjp151216 	idmap_pg_config_t *pgcfg = &_idmapdstate.cfg->pgcfg;
111c8e26105Sjp151216 
112349d5d8fSnw141292 	if (pgcfg->global_catalog == NULL ||
113349d5d8fSnw141292 	    pgcfg->global_catalog[0].host[0] == '\0') {
114349d5d8fSnw141292 		/*
115349d5d8fSnw141292 		 * No GCs.  Continue to use the previous AD config in case
116349d5d8fSnw141292 		 * that's still good but auto-discovery had a transient failure.
117349d5d8fSnw141292 		 * If that stops working we'll go into degraded mode anyways
118349d5d8fSnw141292 		 * when it does.
119349d5d8fSnw141292 		 */
120349d5d8fSnw141292 		degrade_svc(0,
121349d5d8fSnw141292 		    "Global Catalog servers not configured/discoverable");
122349d5d8fSnw141292 		return;
123c8e26105Sjp151216 	}
124c8e26105Sjp151216 
125c8e26105Sjp151216 	old = _idmapdstate.ad;
126c8e26105Sjp151216 
127*2b4a7802SBaban Kenkre 	if (adutils_ad_alloc(&new, pgcfg->default_domain,
128*2b4a7802SBaban Kenkre 	    ADUTILS_AD_GLOBAL_CATALOG) != ADUTILS_SUCCESS) {
129349d5d8fSnw141292 		degrade_svc(0, "could not initialize AD context");
130349d5d8fSnw141292 		return;
131c8e26105Sjp151216 	}
132c8e26105Sjp151216 
133c8e26105Sjp151216 	for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++) {
134c8e26105Sjp151216 		if (idmap_add_ds(new,
135c8e26105Sjp151216 		    pgcfg->global_catalog[i].host,
136c8e26105Sjp151216 		    pgcfg->global_catalog[i].port) != 0) {
137*2b4a7802SBaban Kenkre 			adutils_ad_free(&new);
138349d5d8fSnw141292 			degrade_svc(0, "could not initialize AD GC context");
139349d5d8fSnw141292 			return;
140c8e26105Sjp151216 		}
141c8e26105Sjp151216 	}
142c8e26105Sjp151216 
143c8e26105Sjp151216 	_idmapdstate.ad = new;
144c8e26105Sjp151216 
145c8e26105Sjp151216 	if (old != NULL)
146*2b4a7802SBaban Kenkre 		adutils_ad_free(&old);
147c8e26105Sjp151216 }
148c8e26105Sjp151216 
149c8e26105Sjp151216 
150c5c4113dSnw141292 void
1514edd44c5Sjp151216 print_idmapdstate()
1524edd44c5Sjp151216 {
153c8e26105Sjp151216 	int i;
154e8c27ec8Sbaban 	idmap_pg_config_t *pgcfg;
155c8e26105Sjp151216 
156c5c4113dSnw141292 	RDLOCK_CONFIG();
157c5c4113dSnw141292 
158c8e26105Sjp151216 	if (_idmapdstate.cfg == NULL) {
15971590c90Snw141292 		idmapdlog(LOG_INFO, "Null configuration");
160c8e26105Sjp151216 		UNLOCK_CONFIG();
161c8e26105Sjp151216 		return;
162c5c4113dSnw141292 	}
163c8e26105Sjp151216 
164e8c27ec8Sbaban 	pgcfg = &_idmapdstate.cfg->pgcfg;
165e8c27ec8Sbaban 
16671590c90Snw141292 	idmapdlog(LOG_DEBUG, "list_size_limit=%llu", pgcfg->list_size_limit);
16771590c90Snw141292 	idmapdlog(LOG_DEBUG, "default_domain=%s",
168c8e26105Sjp151216 	    CHECK_NULL(pgcfg->default_domain));
16971590c90Snw141292 	idmapdlog(LOG_DEBUG, "domain_name=%s", CHECK_NULL(pgcfg->domain_name));
17071590c90Snw141292 	idmapdlog(LOG_DEBUG, "machine_sid=%s", CHECK_NULL(pgcfg->machine_sid));
171c8e26105Sjp151216 	if (pgcfg->domain_controller == NULL ||
172c8e26105Sjp151216 	    pgcfg->domain_controller[0].host[0] == '\0') {
17371590c90Snw141292 		idmapdlog(LOG_DEBUG, "No domain controllers known");
174c8e26105Sjp151216 	} else {
175c8e26105Sjp151216 		for (i = 0; pgcfg->domain_controller[i].host[0] != '\0'; i++)
17671590c90Snw141292 			idmapdlog(LOG_DEBUG, "domain_controller=%s port=%d",
17771590c90Snw141292 			    pgcfg->domain_controller[i].host,
178c8e26105Sjp151216 			    pgcfg->domain_controller[i].port);
179c8e26105Sjp151216 	}
18071590c90Snw141292 	idmapdlog(LOG_DEBUG, "forest_name=%s", CHECK_NULL(pgcfg->forest_name));
18171590c90Snw141292 	idmapdlog(LOG_DEBUG, "site_name=%s", CHECK_NULL(pgcfg->site_name));
182c8e26105Sjp151216 	if (pgcfg->global_catalog == NULL ||
183c8e26105Sjp151216 	    pgcfg->global_catalog[0].host[0] == '\0') {
18471590c90Snw141292 		idmapdlog(LOG_DEBUG, "No global catalog servers known");
185c8e26105Sjp151216 	} else {
186c8e26105Sjp151216 		for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++)
18771590c90Snw141292 			idmapdlog(LOG_DEBUG, "global_catalog=%s port=%d",
188c8e26105Sjp151216 			    pgcfg->global_catalog[i].host,
189c8e26105Sjp151216 			    pgcfg->global_catalog[i].port);
190c8e26105Sjp151216 	}
19171590c90Snw141292 	idmapdlog(LOG_DEBUG, "ds_name_mapping_enabled=%s",
192e8c27ec8Sbaban 	    (pgcfg->ds_name_mapping_enabled == TRUE) ? "true" : "false");
19371590c90Snw141292 	idmapdlog(LOG_DEBUG, "ad_unixuser_attr=%s",
194e8c27ec8Sbaban 	    CHECK_NULL(pgcfg->ad_unixuser_attr));
19571590c90Snw141292 	idmapdlog(LOG_DEBUG, "ad_unixgroup_attr=%s",
196e8c27ec8Sbaban 	    CHECK_NULL(pgcfg->ad_unixgroup_attr));
19771590c90Snw141292 	idmapdlog(LOG_DEBUG, "nldap_winname_attr=%s",
198e8c27ec8Sbaban 	    CHECK_NULL(pgcfg->nldap_winname_attr));
199c8e26105Sjp151216 
200c5c4113dSnw141292 	UNLOCK_CONFIG();
201c5c4113dSnw141292 }
202c5c4113dSnw141292 
203c5c4113dSnw141292 int
2044edd44c5Sjp151216 create_directory(const char *path, uid_t uid, gid_t gid)
2054edd44c5Sjp151216 {
206c5c4113dSnw141292 	int	rc;
207c5c4113dSnw141292 
208c5c4113dSnw141292 	if ((rc = mkdir(path, 0700)) < 0 && errno != EEXIST) {
20971590c90Snw141292 		idmapdlog(LOG_ERR, "Error creating directory %s (%s)",
21071590c90Snw141292 		    path, strerror(errno));
211c5c4113dSnw141292 		return (-1);
212c5c4113dSnw141292 	}
213c5c4113dSnw141292 
214c5c4113dSnw141292 	if (lchown(path, uid, gid) < 0) {
21571590c90Snw141292 		idmapdlog(LOG_ERR, "Error creating directory %s (%s)",
21671590c90Snw141292 		    path, strerror(errno));
217c5c4113dSnw141292 		if (rc == 0)
218c5c4113dSnw141292 			(void) rmdir(path);
219c5c4113dSnw141292 		return (-1);
220c5c4113dSnw141292 	}
221c5c4113dSnw141292 	return (0);
222c5c4113dSnw141292 }
223