xref: /titanic_52/usr/src/cmd/idmap/idmapd/init.c (revision bf56214c0556fa6864189c826d39dbe156bb22a0)
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 
42 static const char *me = "idmapd";
43 
44 int
45 init_mapping_system() {
46 	if (rwlock_init(&_idmapdstate.rwlk_cfg, USYNC_THREAD, NULL) != 0)
47 		return (-1);
48 	if (load_config() < 0)
49 		return (-1);
50 	if (init_dbs() < 0) {
51 		fini_mapping_system();
52 		return (-1);
53 	}
54 	return (0);
55 }
56 
57 void
58 fini_mapping_system() {
59 	fini_dbs();
60 }
61 
62 int
63 load_config() {
64 	if ((_idmapdstate.cfg = idmap_cfg_init()) == NULL) {
65 		idmapdlog(LOG_ERR, "%s: config init failed - %s",
66 			me, CHECK_NULL(idmap_cfg_error()));
67 		return (-1);
68 	}
69 	if (_idmapdstate.ad != NULL)
70 		idmap_ad_free(&_idmapdstate.ad);
71 	if (idmap_cfg_load(_idmapdstate.cfg) < 0) {
72 		idmapdlog(LOG_ERR, "%s: config load failed - %s",
73 			me, CHECK_NULL(idmap_cfg_error()));
74 		return (-1);
75 	}
76 	if (_idmapdstate.cfg->pgcfg.mapping_domain == NULL ||
77 	    _idmapdstate.cfg->pgcfg.mapping_domain[0] == '\0') {
78 		idmapdlog(LOG_ERR, "%s: Joined AD domain not configured; name "
79 			"based and ephemeral mapping will not function", me);
80 	} else if (idmap_ad_alloc(&_idmapdstate.ad,
81 		    _idmapdstate.cfg->pgcfg.mapping_domain,
82 		    IDMAP_AD_GLOBAL_CATALOG) != 0) {
83 		idmapdlog(LOG_ERR, "%s: could not initialize AD context",
84 			me);
85 		return (-1);
86 	}
87 	if (_idmapdstate.cfg->pgcfg.global_catalog == NULL ||
88 	    _idmapdstate.cfg->pgcfg.global_catalog[0] == '\0') {
89 		idmapdlog(LOG_ERR, "%s: Global catalog DSnot configured; name "
90 			"based and ephemeral mapping will not function", me);
91 	} else if (idmap_add_ds(_idmapdstate.ad,
92 		    _idmapdstate.cfg->pgcfg.global_catalog, 0) != 0) {
93 		idmapdlog(LOG_ERR, "%s: could not initialize AD DS context",
94 			me);
95 		return (-1);
96 	}
97 	return (0);
98 }
99 
100 void
101 print_idmapdstate() {
102 	RDLOCK_CONFIG();
103 
104 	if (_idmapdstate.daemon_mode == FALSE) {
105 		(void) fprintf(stderr, "%s: daemon_mode=%s\n",
106 			me, _idmapdstate.daemon_mode == TRUE?"true":"false");
107 		(void) fprintf(stderr, "%s: hostname=%s\n",
108 			me, _idmapdstate.hostname);
109 		(void) fprintf(stderr, "%s; name service domain=%s\n", me,
110 			_idmapdstate.domainname);
111 
112 		(void) fprintf(stderr, "%s: config=%s\n", me,
113 			_idmapdstate.cfg?"not null":"null");
114 	}
115 	if (_idmapdstate.cfg == NULL || _idmapdstate.daemon_mode == TRUE)
116 		goto out;
117 	(void) fprintf(stderr, "%s: list_size_limit=%llu\n", me,
118 		_idmapdstate.cfg->pgcfg.list_size_limit);
119 	(void) fprintf(stderr, "%s: mapping_domain=%s\n", me,
120 		CHECK_NULL(_idmapdstate.cfg->pgcfg.mapping_domain));
121 	(void) fprintf(stderr, "%s: machine_sid=%s\n", me,
122 		CHECK_NULL(_idmapdstate.cfg->pgcfg.machine_sid));
123 	(void) fprintf(stderr, "%s: global_catalog=%s\n", me,
124 		CHECK_NULL(_idmapdstate.cfg->pgcfg.global_catalog));
125 	(void) fprintf(stderr, "%s: domain_controller=%s\n", me,
126 		CHECK_NULL(_idmapdstate.cfg->pgcfg.domain_controller));
127 out:
128 	UNLOCK_CONFIG();
129 }
130 
131 int
132 create_directory(const char *path, uid_t uid, gid_t gid) {
133 	int	rc;
134 
135 	if ((rc = mkdir(path, 0700)) < 0 && errno != EEXIST) {
136 		idmapdlog(LOG_ERR,
137 			"%s: Error creating directory %s (%s)",
138 			me, path, strerror(errno));
139 		return (-1);
140 	}
141 
142 	if (lchown(path, uid, gid) < 0) {
143 		idmapdlog(LOG_ERR,
144 			"%s: Error creating directory %s (%s)",
145 			me, path, strerror(errno));
146 		if (rc == 0)
147 			(void) rmdir(path);
148 		return (-1);
149 	}
150 	return (0);
151 }
152