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