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