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