xref: /titanic_51/usr/src/cmd/idmap/idmapd/init.c (revision e8c27ec857e6e2db8c4fe56938b70a89b5bed9f3)
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 /*
22c5c4113dSnw141292  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23c5c4113dSnw141292  * Use is subject to license terms.
24c5c4113dSnw141292  */
25c5c4113dSnw141292 
26c5c4113dSnw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27c5c4113dSnw141292 
28c5c4113dSnw141292 /*
29c5c4113dSnw141292  * Initialization routines
30c5c4113dSnw141292  */
31c5c4113dSnw141292 
32c5c4113dSnw141292 #include "idmapd.h"
33c5c4113dSnw141292 #include <signal.h>
34c5c4113dSnw141292 #include <thread.h>
35c5c4113dSnw141292 #include <string.h>
36c5c4113dSnw141292 #include <errno.h>
37c5c4113dSnw141292 #include <assert.h>
38c5c4113dSnw141292 #include <unistd.h>
39c5c4113dSnw141292 #include <sys/types.h>
40c5c4113dSnw141292 #include <sys/stat.h>
418edda628Sbaban #include <rpcsvc/daemon_utils.h>
42c5c4113dSnw141292 
43c5c4113dSnw141292 static const char *me = "idmapd";
44c5c4113dSnw141292 
45c5c4113dSnw141292 int
46c5c4113dSnw141292 init_mapping_system() {
478edda628Sbaban 	int rc = 0;
488edda628Sbaban 
49c5c4113dSnw141292 	if (rwlock_init(&_idmapdstate.rwlk_cfg, USYNC_THREAD, NULL) != 0)
50c5c4113dSnw141292 		return (-1);
51*e8c27ec8Sbaban 	if ((rc = load_config()) < 0)
52*e8c27ec8Sbaban 		return (rc);
538edda628Sbaban 
548edda628Sbaban 	(void) setegid(DAEMON_GID);
558edda628Sbaban 	(void) seteuid(DAEMON_UID);
56c5c4113dSnw141292 	if (init_dbs() < 0) {
578edda628Sbaban 		rc = -1;
58c5c4113dSnw141292 		fini_mapping_system();
59c5c4113dSnw141292 	}
608edda628Sbaban 	(void) seteuid(0);
618edda628Sbaban 	(void) setegid(0);
628edda628Sbaban 
638edda628Sbaban 	return (rc);
64c5c4113dSnw141292 }
65c5c4113dSnw141292 
66c5c4113dSnw141292 void
67c5c4113dSnw141292 fini_mapping_system() {
68c5c4113dSnw141292 	fini_dbs();
69c5c4113dSnw141292 }
70c5c4113dSnw141292 
71c5c4113dSnw141292 int
72c5c4113dSnw141292 load_config() {
73e3c2d6aaSnw141292 	int rc;
74c8e26105Sjp151216 	idmap_pg_config_t *pgcfg;
75c5c4113dSnw141292 	if ((_idmapdstate.cfg = idmap_cfg_init()) == NULL) {
76651c0131Sbaban 		idmapdlog(LOG_ERR, "%s: failed to initialize config", me);
77c8e26105Sjp151216 		degrade_svc();
78c5c4113dSnw141292 		return (-1);
79c5c4113dSnw141292 	}
80c8e26105Sjp151216 	pgcfg = &_idmapdstate.cfg->pgcfg;
81c8e26105Sjp151216 
82e3c2d6aaSnw141292 	rc = idmap_cfg_load(&_idmapdstate.cfg->handles,
83e3c2d6aaSnw141292 	    &_idmapdstate.cfg->pgcfg, 0);
84e3c2d6aaSnw141292 	if (rc < -1) {
85e3c2d6aaSnw141292 		/* Total failure */
86c8e26105Sjp151216 		degrade_svc();
87e3c2d6aaSnw141292 		idmapdlog(LOG_ERR, "%s: Fatal error while loading "
88e3c2d6aaSnw141292 		    "configuration", me);
89*e8c27ec8Sbaban 		return (rc);
90c5c4113dSnw141292 	}
91c8e26105Sjp151216 
92e3c2d6aaSnw141292 	if (rc != 0)
93e3c2d6aaSnw141292 		/* Partial failure */
94e3c2d6aaSnw141292 		idmapdlog(LOG_ERR, "%s: Various errors occurred while loading "
95e3c2d6aaSnw141292 			"the configuration; check the logs", me);
96e3c2d6aaSnw141292 
97c8e26105Sjp151216 	if (pgcfg->global_catalog == NULL ||
98c8e26105Sjp151216 	    pgcfg->global_catalog[0].host[0] == '\0') {
99c8e26105Sjp151216 		degrade_svc();
100e3c2d6aaSnw141292 		idmapdlog(LOG_INFO,
101e3c2d6aaSnw141292 		    "%s: Global catalog server is not configured; AD lookup "
102e3c2d6aaSnw141292 		    "will fail until one or more global catalog server names "
103e3c2d6aaSnw141292 		    "are configured or discovered; auto-discovery will begin "
104e3c2d6aaSnw141292 		    "shortly", me);
105e3c2d6aaSnw141292 	} else {
106e3c2d6aaSnw141292 		restore_svc();
107c5c4113dSnw141292 	}
108c8e26105Sjp151216 
109c8e26105Sjp151216 	(void) reload_ad();
110c8e26105Sjp151216 
111c8e26105Sjp151216 	if (idmap_cfg_start_updates(_idmapdstate.cfg) < 0)
112c8e26105Sjp151216 		idmapdlog(LOG_ERR, "%s: could not start config updater",
113c8e26105Sjp151216 			me);
114e3c2d6aaSnw141292 
115e3c2d6aaSnw141292 	idmapdlog(LOG_DEBUG, "%s: initial configuration loaded", me);
116e3c2d6aaSnw141292 
117c5c4113dSnw141292 	return (0);
118c5c4113dSnw141292 }
119c5c4113dSnw141292 
120c8e26105Sjp151216 
121c8e26105Sjp151216 int
122c8e26105Sjp151216 reload_ad() {
123c8e26105Sjp151216 	int	i;
124c8e26105Sjp151216 	ad_t	*old;
125c8e26105Sjp151216 	ad_t	*new;
126c8e26105Sjp151216 
127c8e26105Sjp151216 	idmap_pg_config_t *pgcfg = &_idmapdstate.cfg->pgcfg;
128c8e26105Sjp151216 
129c8e26105Sjp151216 	if (pgcfg->default_domain == NULL ||
130c8e26105Sjp151216 	    pgcfg->global_catalog == NULL) {
131c8e26105Sjp151216 		if (_idmapdstate.ad == NULL)
132c8e26105Sjp151216 			idmapdlog(LOG_ERR, "%s: AD lookup disabled", me);
133c8e26105Sjp151216 		else
134c8e26105Sjp151216 			idmapdlog(LOG_ERR, "%s: cannot update AD context", me);
135c8e26105Sjp151216 		return (-1);
136c8e26105Sjp151216 	}
137c8e26105Sjp151216 
138c8e26105Sjp151216 	old = _idmapdstate.ad;
139c8e26105Sjp151216 
140c8e26105Sjp151216 	if (idmap_ad_alloc(&new, pgcfg->default_domain,
141c8e26105Sjp151216 	    IDMAP_AD_GLOBAL_CATALOG) != 0) {
142c8e26105Sjp151216 		if (old == NULL)
143c8e26105Sjp151216 			degrade_svc();
144c8e26105Sjp151216 		idmapdlog(LOG_ERR, "%s: could not initialize AD context", me);
145c8e26105Sjp151216 		return (-1);
146c8e26105Sjp151216 	}
147c8e26105Sjp151216 
148c8e26105Sjp151216 	for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++) {
149c8e26105Sjp151216 		if (idmap_add_ds(new,
150c8e26105Sjp151216 		    pgcfg->global_catalog[i].host,
151c8e26105Sjp151216 		    pgcfg->global_catalog[i].port) != 0) {
152c8e26105Sjp151216 			idmap_ad_free(&new);
153c8e26105Sjp151216 			if (old == NULL)
154c8e26105Sjp151216 				degrade_svc();
155c8e26105Sjp151216 			idmapdlog(LOG_ERR,
156c8e26105Sjp151216 			    "%s: could not initialize AD DS context", me);
157c8e26105Sjp151216 			return (-1);
158c8e26105Sjp151216 		}
159c8e26105Sjp151216 	}
160c8e26105Sjp151216 
161c8e26105Sjp151216 	_idmapdstate.ad = new;
162c8e26105Sjp151216 
163c8e26105Sjp151216 	if (old != NULL)
164c8e26105Sjp151216 		idmap_ad_free(&old);
165c8e26105Sjp151216 
166c8e26105Sjp151216 	return (0);
167c8e26105Sjp151216 }
168c8e26105Sjp151216 
169c8e26105Sjp151216 
170c5c4113dSnw141292 void
171c5c4113dSnw141292 print_idmapdstate() {
172c8e26105Sjp151216 	int i;
173*e8c27ec8Sbaban 	idmap_pg_config_t *pgcfg;
174c8e26105Sjp151216 
175c5c4113dSnw141292 	RDLOCK_CONFIG();
176c5c4113dSnw141292 
177c8e26105Sjp151216 	if (_idmapdstate.cfg == NULL) {
178c8e26105Sjp151216 		idmapdlog(LOG_INFO, "%s: Null configuration", me);
179c8e26105Sjp151216 		UNLOCK_CONFIG();
180c8e26105Sjp151216 		return;
181c5c4113dSnw141292 	}
182c8e26105Sjp151216 
183*e8c27ec8Sbaban 	pgcfg = &_idmapdstate.cfg->pgcfg;
184*e8c27ec8Sbaban 
185c8e26105Sjp151216 	idmapdlog(LOG_DEBUG, "%s: list_size_limit=%llu", me,
186c8e26105Sjp151216 	    pgcfg->list_size_limit);
187c8e26105Sjp151216 	idmapdlog(LOG_DEBUG, "%s: default_domain=%s", me,
188c8e26105Sjp151216 	    CHECK_NULL(pgcfg->default_domain));
189c8e26105Sjp151216 	idmapdlog(LOG_DEBUG, "%s: domain_name=%s", me,
190c8e26105Sjp151216 	    CHECK_NULL(pgcfg->domain_name));
191c8e26105Sjp151216 	idmapdlog(LOG_DEBUG, "%s: machine_sid=%s", me,
192c8e26105Sjp151216 	    CHECK_NULL(pgcfg->machine_sid));
193c8e26105Sjp151216 	if (pgcfg->domain_controller == NULL ||
194c8e26105Sjp151216 	    pgcfg->domain_controller[0].host[0] == '\0') {
195c8e26105Sjp151216 		idmapdlog(LOG_DEBUG, "%s: No domain controllers known", me);
196c8e26105Sjp151216 	} else {
197c8e26105Sjp151216 		for (i = 0; pgcfg->domain_controller[i].host[0] != '\0'; i++)
198c8e26105Sjp151216 			idmapdlog(LOG_DEBUG, "%s: domain_controller=%s port=%d",
199c8e26105Sjp151216 			    me, pgcfg->domain_controller[i].host,
200c8e26105Sjp151216 			    pgcfg->domain_controller[i].port);
201c8e26105Sjp151216 	}
202c8e26105Sjp151216 	idmapdlog(LOG_DEBUG, "%s: forest_name=%s", me,
203c8e26105Sjp151216 	    CHECK_NULL(pgcfg->forest_name));
204c8e26105Sjp151216 	idmapdlog(LOG_DEBUG, "%s: site_name=%s", me,
205c8e26105Sjp151216 	    CHECK_NULL(pgcfg->site_name));
206c8e26105Sjp151216 	if (pgcfg->global_catalog == NULL ||
207c8e26105Sjp151216 	    pgcfg->global_catalog[0].host[0] == '\0') {
208c8e26105Sjp151216 		idmapdlog(LOG_DEBUG, "%s: No global catalog servers known", me);
209c8e26105Sjp151216 	} else {
210c8e26105Sjp151216 		for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++)
211c8e26105Sjp151216 			idmapdlog(LOG_DEBUG, "%s: global_catalog=%s port=%d",
212c8e26105Sjp151216 			    me,
213c8e26105Sjp151216 			    pgcfg->global_catalog[i].host,
214c8e26105Sjp151216 			    pgcfg->global_catalog[i].port);
215c8e26105Sjp151216 	}
216*e8c27ec8Sbaban 	idmapdlog(LOG_DEBUG, "%s: ds_name_mapping_enabled=%s", me,
217*e8c27ec8Sbaban 	    (pgcfg->ds_name_mapping_enabled == TRUE) ? "true" : "false");
218*e8c27ec8Sbaban 	idmapdlog(LOG_DEBUG, "%s: ad_unixuser_attr=%s", me,
219*e8c27ec8Sbaban 	    CHECK_NULL(pgcfg->ad_unixuser_attr));
220*e8c27ec8Sbaban 	idmapdlog(LOG_DEBUG, "%s: ad_unixgroup_attr=%s", me,
221*e8c27ec8Sbaban 	    CHECK_NULL(pgcfg->ad_unixgroup_attr));
222*e8c27ec8Sbaban 	idmapdlog(LOG_DEBUG, "%s: nldap_winname_attr=%s", me,
223*e8c27ec8Sbaban 	    CHECK_NULL(pgcfg->nldap_winname_attr));
224c8e26105Sjp151216 
225c5c4113dSnw141292 	UNLOCK_CONFIG();
226c5c4113dSnw141292 }
227c5c4113dSnw141292 
228c5c4113dSnw141292 int
229c5c4113dSnw141292 create_directory(const char *path, uid_t uid, gid_t gid) {
230c5c4113dSnw141292 	int	rc;
231c5c4113dSnw141292 
232c5c4113dSnw141292 	if ((rc = mkdir(path, 0700)) < 0 && errno != EEXIST) {
233c5c4113dSnw141292 		idmapdlog(LOG_ERR,
234c5c4113dSnw141292 			"%s: Error creating directory %s (%s)",
235c5c4113dSnw141292 			me, path, strerror(errno));
236c5c4113dSnw141292 		return (-1);
237c5c4113dSnw141292 	}
238c5c4113dSnw141292 
239c5c4113dSnw141292 	if (lchown(path, uid, gid) < 0) {
240c5c4113dSnw141292 		idmapdlog(LOG_ERR,
241c5c4113dSnw141292 			"%s: Error creating directory %s (%s)",
242c5c4113dSnw141292 			me, path, strerror(errno));
243c5c4113dSnw141292 		if (rc == 0)
244c5c4113dSnw141292 			(void) rmdir(path);
245c5c4113dSnw141292 		return (-1);
246c5c4113dSnw141292 	}
247c5c4113dSnw141292 	return (0);
248c5c4113dSnw141292 }
249