xref: /titanic_52/usr/src/cmd/idmap/idmapd/init.c (revision a83cadce5d3331b64803bfc641036cec23602c74)
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, "%s: Global catalog DSnot configured; name "
97 			"based and ephemeral mapping will not function", me);
98 	} else if (idmap_add_ds(_idmapdstate.ad,
99 		    _idmapdstate.cfg->pgcfg.global_catalog, 0) != 0) {
100 		idmapdlog(LOG_ERR, "%s: could not initialize AD DS context",
101 			me);
102 		return (-1);
103 	}
104 	return (0);
105 }
106 
107 void
108 print_idmapdstate() {
109 	RDLOCK_CONFIG();
110 
111 	if (_idmapdstate.daemon_mode == FALSE) {
112 		(void) fprintf(stderr, "%s: daemon_mode=%s\n",
113 			me, _idmapdstate.daemon_mode == TRUE?"true":"false");
114 		(void) fprintf(stderr, "%s: hostname=%s\n",
115 			me, _idmapdstate.hostname);
116 		(void) fprintf(stderr, "%s; name service domain=%s\n", me,
117 			_idmapdstate.domainname);
118 
119 		(void) fprintf(stderr, "%s: config=%s\n", me,
120 			_idmapdstate.cfg?"not null":"null");
121 	}
122 	if (_idmapdstate.cfg == NULL || _idmapdstate.daemon_mode == TRUE)
123 		goto out;
124 	(void) fprintf(stderr, "%s: list_size_limit=%llu\n", me,
125 		_idmapdstate.cfg->pgcfg.list_size_limit);
126 	(void) fprintf(stderr, "%s: mapping_domain=%s\n", me,
127 		CHECK_NULL(_idmapdstate.cfg->pgcfg.mapping_domain));
128 	(void) fprintf(stderr, "%s: machine_sid=%s\n", me,
129 		CHECK_NULL(_idmapdstate.cfg->pgcfg.machine_sid));
130 	(void) fprintf(stderr, "%s: global_catalog=%s\n", me,
131 		CHECK_NULL(_idmapdstate.cfg->pgcfg.global_catalog));
132 	(void) fprintf(stderr, "%s: domain_controller=%s\n", me,
133 		CHECK_NULL(_idmapdstate.cfg->pgcfg.domain_controller));
134 out:
135 	UNLOCK_CONFIG();
136 }
137 
138 int
139 create_directory(const char *path, uid_t uid, gid_t gid) {
140 	int	rc;
141 
142 	if ((rc = mkdir(path, 0700)) < 0 && errno != EEXIST) {
143 		idmapdlog(LOG_ERR,
144 			"%s: Error creating directory %s (%s)",
145 			me, path, strerror(errno));
146 		return (-1);
147 	}
148 
149 	if (lchown(path, uid, gid) < 0) {
150 		idmapdlog(LOG_ERR,
151 			"%s: Error creating directory %s (%s)",
152 			me, path, strerror(errno));
153 		if (rc == 0)
154 			(void) rmdir(path);
155 		return (-1);
156 	}
157 	return (0);
158 }
159