xref: /illumos-gate/usr/src/cmd/idmap/idmapd/init.c (revision e8c27ec8)
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 ((rc = load_config()) < 0)
52 		return (rc);
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 (rc);
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;
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 	pgcfg = &_idmapdstate.cfg->pgcfg;
184 
185 	idmapdlog(LOG_DEBUG, "%s: list_size_limit=%llu", me,
186 	    pgcfg->list_size_limit);
187 	idmapdlog(LOG_DEBUG, "%s: default_domain=%s", me,
188 	    CHECK_NULL(pgcfg->default_domain));
189 	idmapdlog(LOG_DEBUG, "%s: domain_name=%s", me,
190 	    CHECK_NULL(pgcfg->domain_name));
191 	idmapdlog(LOG_DEBUG, "%s: machine_sid=%s", me,
192 	    CHECK_NULL(pgcfg->machine_sid));
193 	if (pgcfg->domain_controller == NULL ||
194 	    pgcfg->domain_controller[0].host[0] == '\0') {
195 		idmapdlog(LOG_DEBUG, "%s: No domain controllers known", me);
196 	} else {
197 		for (i = 0; pgcfg->domain_controller[i].host[0] != '\0'; i++)
198 			idmapdlog(LOG_DEBUG, "%s: domain_controller=%s port=%d",
199 			    me, pgcfg->domain_controller[i].host,
200 			    pgcfg->domain_controller[i].port);
201 	}
202 	idmapdlog(LOG_DEBUG, "%s: forest_name=%s", me,
203 	    CHECK_NULL(pgcfg->forest_name));
204 	idmapdlog(LOG_DEBUG, "%s: site_name=%s", me,
205 	    CHECK_NULL(pgcfg->site_name));
206 	if (pgcfg->global_catalog == NULL ||
207 	    pgcfg->global_catalog[0].host[0] == '\0') {
208 		idmapdlog(LOG_DEBUG, "%s: No global catalog servers known", me);
209 	} else {
210 		for (i = 0; pgcfg->global_catalog[i].host[0] != '\0'; i++)
211 			idmapdlog(LOG_DEBUG, "%s: global_catalog=%s port=%d",
212 			    me,
213 			    pgcfg->global_catalog[i].host,
214 			    pgcfg->global_catalog[i].port);
215 	}
216 	idmapdlog(LOG_DEBUG, "%s: ds_name_mapping_enabled=%s", me,
217 	    (pgcfg->ds_name_mapping_enabled == TRUE) ? "true" : "false");
218 	idmapdlog(LOG_DEBUG, "%s: ad_unixuser_attr=%s", me,
219 	    CHECK_NULL(pgcfg->ad_unixuser_attr));
220 	idmapdlog(LOG_DEBUG, "%s: ad_unixgroup_attr=%s", me,
221 	    CHECK_NULL(pgcfg->ad_unixgroup_attr));
222 	idmapdlog(LOG_DEBUG, "%s: nldap_winname_attr=%s", me,
223 	    CHECK_NULL(pgcfg->nldap_winname_attr));
224 
225 	UNLOCK_CONFIG();
226 }
227 
228 int
229 create_directory(const char *path, uid_t uid, gid_t gid) {
230 	int	rc;
231 
232 	if ((rc = mkdir(path, 0700)) < 0 && errno != EEXIST) {
233 		idmapdlog(LOG_ERR,
234 			"%s: Error creating directory %s (%s)",
235 			me, path, strerror(errno));
236 		return (-1);
237 	}
238 
239 	if (lchown(path, uid, gid) < 0) {
240 		idmapdlog(LOG_ERR,
241 			"%s: Error creating directory %s (%s)",
242 			me, path, strerror(errno));
243 		if (rc == 0)
244 			(void) rmdir(path);
245 		return (-1);
246 	}
247 	return (0);
248 }
249