xref: /titanic_51/usr/src/cmd/idmap/idmapd/idmapd.h (revision 37714ae43602c675f9dc59b070bfdf9fa702872c)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _IDMAPD_H
27 #define	_IDMAPD_H
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <rpc/rpc.h>
33 #include <synch.h>
34 #include <thread.h>
35 #include <libintl.h>
36 #include <strings.h>
37 #include <sqlite/sqlite.h>
38 #include <syslog.h>
39 #include <inttypes.h>
40 #include <rpcsvc/idmap_prot.h>
41 #include "adutils.h"
42 #include "idmap_priv.h"
43 #include "idmap_config.h"
44 #include "libadutils.h"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 #define	SENTINEL_PID	UINT32_MAX
51 #define	CHECK_NULL(s)	(s != NULL ? s : "null")
52 
53 extern mutex_t _svcstate_lock;	/* lock for _rpcsvcstate, _rpcsvccount */
54 
55 typedef enum idmap_namemap_mode {
56 	IDMAP_NM_NONE = 0,
57 	IDMAP_NM_AD,
58 	IDMAP_NM_NLDAP,
59 	IDMAP_NM_MIXED
60 } idmap_namemap_mode_t;
61 
62 /*
63  * Global state of idmapd daemon.
64  */
65 #define	IDMAP_MAX_NAME_LEN	512
66 typedef struct idmapd_state {
67 	rwlock_t	rwlk_cfg;		/* config lock */
68 	idmap_cfg_t	*cfg;			/* config */
69 	bool_t		daemon_mode;
70 	bool_t		debug_mode;
71 	char		hostname[MAX_NAME_LEN];	/* my hostname */
72 	uid_t		next_uid;
73 	gid_t		next_gid;
74 	uid_t		limit_uid;
75 	gid_t		limit_gid;
76 	int		new_eph_db;	/* was the ephem ID db [re-]created? */
77 	bool_t		eph_map_unres_sids;
78 	int		num_ads;
79 	adutils_ad_t	**ads;
80 } idmapd_state_t;
81 extern idmapd_state_t	_idmapdstate;
82 
83 #define	RDLOCK_CONFIG() \
84 	(void) rw_rdlock(&_idmapdstate.rwlk_cfg);
85 #define	WRLOCK_CONFIG() \
86 	(void) rw_wrlock(&_idmapdstate.rwlk_cfg);
87 #define	UNLOCK_CONFIG() \
88 	(void) rw_unlock(&_idmapdstate.rwlk_cfg);
89 
90 typedef struct hashentry {
91 	uint_t	key;
92 	uint_t	next;
93 } hashentry_t;
94 
95 typedef struct lookup_state {
96 	bool_t			sid2pid_done;
97 	bool_t			pid2sid_done;
98 	int			ad_nqueries;
99 	int			nldap_nqueries;
100 	bool_t			eph_map_unres_sids;
101 	uint_t			curpos;
102 	hashentry_t		*sid_history;
103 	uint_t			sid_history_size;
104 	idmap_mapping_batch	*batch;
105 	idmap_ids_res		*result;
106 	idmap_namemap_mode_t	nm_siduid;
107 	idmap_namemap_mode_t	nm_sidgid;
108 	char			*ad_unixuser_attr;
109 	char			*ad_unixgroup_attr;
110 	char			*nldap_winname_attr;
111 	char			*defdom;
112 	sqlite			*cache;
113 	sqlite			*db;
114 } lookup_state_t;
115 
116 #define	NLDAP_OR_MIXED(nm) \
117 	(nm == IDMAP_NM_NLDAP || nm == IDMAP_NM_MIXED)
118 #define	AD_OR_MIXED(nm) \
119 	(nm == IDMAP_NM_AD || nm == IDMAP_NM_MIXED)
120 
121 #define	NLDAP_OR_MIXED_MODE(pidtype, ls) \
122 	((pidtype == IDMAP_UID && NLDAP_OR_MIXED(ls->nm_siduid)) || \
123 	(pidtype == IDMAP_GID && NLDAP_OR_MIXED(ls->nm_sidgid)))
124 #define	AD_OR_MIXED_MODE(pidtype, ls)\
125 	((pidtype == IDMAP_UID && AD_OR_MIXED(ls->nm_siduid)) || \
126 	(pidtype == IDMAP_GID && AD_OR_MIXED(ls->nm_sidgid)))
127 #define	NLDAP_MODE(pidtype, ls) \
128 	((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_NLDAP) || \
129 	(pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_NLDAP))
130 #define	AD_MODE(pidtype, ls) \
131 	((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_AD) || \
132 	(pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_AD))
133 #define	MIXED_MODE(pidtype, ls) \
134 	((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_MIXED) || \
135 	(pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_MIXED))
136 
137 
138 typedef struct list_cb_data {
139 	void		*result;
140 	uint64_t	next;
141 	uint64_t	len;
142 	uint64_t	limit;
143 	int		flag;
144 } list_cb_data_t;
145 
146 typedef struct msg_table {
147 	idmap_retcode	retcode;
148 	const char	*msg;
149 } msg_table_t;
150 
151 /*
152  * Data structure to store well-known SIDs and
153  * associated mappings (if any)
154  */
155 typedef struct wksids_table {
156 	const char	*sidprefix;
157 	uint32_t	rid;
158 	const char	*domain;
159 	const char	*winname;
160 	int		is_wuser;
161 	uid_t		pid;
162 	int		is_user;
163 	int		direction;
164 } wksids_table_t;
165 
166 #define	IDMAPD_SEARCH_TIMEOUT		3   /* seconds */
167 #define	IDMAPD_LDAP_OPEN_TIMEOUT	1   /* secs; initial, w/ exp backoff */
168 
169 /*
170  * The following flags are used by idmapd while processing a
171  * given mapping request. Note that idmapd uses multiple passes to
172  * process the request and the flags are used to pass information
173  * about the state of the request between these passes.
174  */
175 
176 /* Initial state. Done. Reset all flags. Remaining passes can be skipped */
177 #define	_IDMAP_F_DONE			0x00000000
178 /* Set when subsequent passes are required */
179 #define	_IDMAP_F_NOTDONE		0x00000001
180 /* Don't update name_cache. (e.g. set when winname,SID found in name_cache) */
181 #define	_IDMAP_F_DONT_UPDATE_NAMECACHE	0x00000002
182 /* Batch this request for AD lookup */
183 #define	_IDMAP_F_LOOKUP_AD		0x00000004
184 /* Batch this request for nldap directory lookup */
185 #define	_IDMAP_F_LOOKUP_NLDAP		0x00000008
186 /*
187  * Expired ephemeral mapping found in cache when processing sid2uid request.
188  * Use it if the given SID cannot be mapped by name
189  */
190 #define	_IDMAP_F_EXP_EPH_UID		0x00000010
191 /* Same as above. Used for sid2gid request */
192 #define	_IDMAP_F_EXP_EPH_GID		0x00000020
193 /* This request is not valid for the current forest */
194 #define	_IDMAP_F_LOOKUP_OTHER_AD	0x00000040
195 
196 
197 /*
198  * Check if we are done. If so, subsequent passes can be skipped
199  * when processing a given mapping request.
200  */
201 #define	ARE_WE_DONE(f)	((f & _IDMAP_F_NOTDONE) == 0)
202 
203 #define	SIZE_INCR	5
204 #define	MAX_TRIES	5
205 #define	IDMAP_DBDIR	"/var/idmap"
206 #define	IDMAP_CACHEDIR	"/var/run/idmap"
207 #define	IDMAP_DBNAME	IDMAP_DBDIR "/idmap.db"
208 #define	IDMAP_CACHENAME	IDMAP_CACHEDIR "/idmap.db"
209 
210 #define	IS_BATCH_SID(batch, i) \
211 	(batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID ||	\
212 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_USID ||	\
213 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GSID)
214 
215 #define	IS_BATCH_UID(batch, i) \
216 	(batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID)
217 
218 #define	IS_BATCH_GID(batch, i) \
219 	(batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID)
220 
221 #define	IS_REQUEST_SID(req, n) \
222 	((req).id##n.idtype == IDMAP_SID ||	\
223 	(req).id##n.idtype == IDMAP_USID ||	\
224 	(req).id##n.idtype == IDMAP_GSID)	\
225 
226 
227 #define	IS_REQUEST_UID(request) \
228 	((request).id1.idtype == IDMAP_UID)
229 
230 #define	IS_REQUEST_GID(request) \
231 	((request).id1.idtype == IDMAP_GID)
232 
233 /*
234  * Local RID ranges
235  */
236 #define	LOCALRID_UID_MIN	1000U
237 #define	LOCALRID_UID_MAX	((uint32_t)INT32_MAX)
238 #define	LOCALRID_GID_MIN	(((uint32_t)INT32_MAX) + 1)
239 #define	LOCALRID_GID_MAX	UINT32_MAX
240 
241 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t);
242 typedef int (*list_svc_cb)(void *, int, char **, char **);
243 
244 extern void	idmap_prog_1(struct svc_req *, register SVCXPRT *);
245 extern void	idmapdlog(int, const char *, ...);
246 extern int	init_mapping_system();
247 extern void	fini_mapping_system();
248 extern void	print_idmapdstate();
249 extern int	create_directory(const char *, uid_t, gid_t);
250 extern int	load_config();
251 extern void	reload_ad();
252 extern int	idmap_init_tsd_key(void);
253 extern void	degrade_svc(int, const char *);
254 extern void	restore_svc(void);
255 
256 
257 extern int		init_dbs();
258 extern void		fini_dbs();
259 extern idmap_retcode	get_db_handle(sqlite **);
260 extern idmap_retcode	get_cache_handle(sqlite **);
261 extern idmap_retcode	sql_exec_no_cb(sqlite *, const char *, char *);
262 extern idmap_retcode	add_namerule(sqlite *, idmap_namerule *);
263 extern idmap_retcode	rm_namerule(sqlite *, idmap_namerule *);
264 extern idmap_retcode	flush_namerules(sqlite *);
265 
266 extern char 		*tolower_u8(const char *);
267 
268 extern idmap_retcode	gen_sql_expr_from_rule(idmap_namerule *, char **);
269 extern idmap_retcode	validate_list_cb_data(list_cb_data_t *, int,
270 				char **, int, uchar_t **, size_t);
271 extern idmap_retcode	process_list_svc_sql(sqlite *, const char *, char *,
272 				uint64_t, int, list_svc_cb, void *);
273 extern idmap_retcode	sid2pid_first_pass(lookup_state_t *,
274 				idmap_mapping *, idmap_id_res *);
275 extern idmap_retcode	sid2pid_second_pass(lookup_state_t *,
276 				idmap_mapping *, idmap_id_res *);
277 extern idmap_retcode	pid2sid_first_pass(lookup_state_t *,
278 				idmap_mapping *, idmap_id_res *, int, int);
279 extern idmap_retcode	pid2sid_second_pass(lookup_state_t *,
280 				idmap_mapping *, idmap_id_res *, int);
281 extern idmap_retcode	update_cache_sid2pid(lookup_state_t *,
282 				idmap_mapping *, idmap_id_res *);
283 extern idmap_retcode	update_cache_pid2sid(lookup_state_t *,
284 				idmap_mapping *, idmap_id_res *);
285 extern idmap_retcode	get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *,
286 				idmap_mapping *, int);
287 extern idmap_retcode	get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *,
288 				idmap_mapping *);
289 extern idmap_retcode	load_cfg_in_state(lookup_state_t *);
290 extern void		cleanup_lookup_state(lookup_state_t *);
291 
292 extern idmap_retcode	ad_lookup_batch(lookup_state_t *,
293 				idmap_mapping_batch *, idmap_ids_res *);
294 extern idmap_retcode	lookup_name2sid(sqlite *, const char *, const char *,
295 				int *, char **, char **, char **,
296 				idmap_rid_t *, idmap_mapping *, int);
297 extern idmap_retcode	lookup_wksids_name2sid(const char *, const char *,
298 				char **, char **, char **, idmap_rid_t *,
299 				int *);
300 
301 
302 extern void 	idmap_log_stderr(int);
303 extern void	idmap_log_syslog(boolean_t);
304 extern void	idmap_log_degraded(boolean_t);
305 
306 extern const wksids_table_t *find_wksid_by_pid(uid_t pid, int is_user);
307 extern const wksids_table_t *find_wksid_by_sid(const char *sid, int rid,
308     int type);
309 extern const wksids_table_t *find_wksid_by_name(const char *name,
310     const char *domain, int type);
311 extern const wksids_table_t *find_wk_by_sid(char *sid);
312 
313 #ifdef __cplusplus
314 }
315 #endif
316 
317 #endif /* _IDMAPD_H */
318