xref: /illumos-gate/usr/src/cmd/idmap/idmapd/idmapd.h (revision 09a48d4ca0ddda4ad26cc885769745870d989baf)
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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
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	CHECK_NULL(s)	(s != NULL ? s : "null")
51 
52 extern mutex_t _svcstate_lock;	/* lock for _rpcsvcstate, _rpcsvccount */
53 
54 typedef enum idmap_namemap_mode {
55 	IDMAP_NM_NONE = 0,
56 	IDMAP_NM_AD,
57 	IDMAP_NM_NLDAP,
58 	IDMAP_NM_MIXED
59 } idmap_namemap_mode_t;
60 
61 /*
62  * Debugging output.
63  *
64  * There are some number of areas - configuration, mapping, discovery, et
65  * cetera - and for each area there is a verbosity level controlled through
66  * an SMF property.  The default is zero, and "debug/all" provides a master
67  * control allowing you to turn on all debugging output with one setting.
68  *
69  * A typical debugging output sequence would look like
70  *
71  * 	if (DBG(CONFIG, 2)) {
72  *		idmapdlog(LOG_DEBUG,
73  *		    "some message about config at verbosity 2");
74  *	}
75  */
76 enum idmapd_debug {
77 	IDMAPD_DEBUG_ALL = 0,
78 	IDMAPD_DEBUG_CONFIG = 1,
79 	IDMAPD_DEBUG_MAPPING = 2,
80 	IDMAPD_DEBUG_DISC = 3,
81 	IDMAPD_DEBUG_DNS = 4,
82 	IDMAPD_DEBUG_LDAP = 5,
83 	IDMAPD_DEBUG_MAX = 5
84 };
85 
86 #define	DBG(type, lev)	\
87 	(_idmapdstate.debug[IDMAPD_DEBUG_##type] >= (lev) || \
88 	    _idmapdstate.debug[IDMAPD_DEBUG_ALL] >= (lev))
89 
90 /*
91  * Global state of idmapd daemon.
92  */
93 typedef struct idmapd_state {
94 	rwlock_t	rwlk_cfg;		/* config lock */
95 	idmap_cfg_t	*cfg;			/* config */
96 	bool_t		daemon_mode;
97 	char		hostname[MAX_NAME_LEN];	/* my hostname */
98 	uid_t		next_uid;
99 	gid_t		next_gid;
100 	uid_t		limit_uid;
101 	gid_t		limit_gid;
102 	int		new_eph_db;	/* was the ephem ID db [re-]created? */
103 	int		num_gcs;
104 	adutils_ad_t	**gcs;
105 	int		num_dcs;
106 	adutils_ad_t	**dcs;
107 	int		debug[IDMAPD_DEBUG_MAX+1];
108 } idmapd_state_t;
109 extern idmapd_state_t	_idmapdstate;
110 
111 #define	RDLOCK_CONFIG() \
112 	(void) rw_rdlock(&_idmapdstate.rwlk_cfg);
113 #define	WRLOCK_CONFIG() \
114 	(void) rw_wrlock(&_idmapdstate.rwlk_cfg);
115 #define	UNLOCK_CONFIG() \
116 	(void) rw_unlock(&_idmapdstate.rwlk_cfg);
117 
118 typedef struct hashentry {
119 	uint_t	key;
120 	uint_t	next;
121 } hashentry_t;
122 
123 typedef struct lookup_state {
124 	bool_t			sid2pid_done;
125 	bool_t			pid2sid_done;
126 	int			ad_nqueries;
127 	int			nldap_nqueries;
128 	bool_t			eph_map_unres_sids;
129 	int			directory_based_mapping;	/* enum */
130 	uint_t			id_cache_timeout;
131 	uint_t			name_cache_timeout;
132 	uint_t			curpos;
133 	hashentry_t		*sid_history;
134 	uint_t			sid_history_size;
135 	idmap_mapping_batch	*batch;
136 	idmap_ids_res		*result;
137 	idmap_namemap_mode_t	nm_siduid;
138 	idmap_namemap_mode_t	nm_sidgid;
139 	char			*ad_unixuser_attr;
140 	char			*ad_unixgroup_attr;
141 	char			*nldap_winname_attr;
142 	char			*defdom;
143 	sqlite			*cache;
144 	sqlite			*db;
145 } lookup_state_t;
146 
147 #define	NLDAP_OR_MIXED(nm) \
148 	((nm) == IDMAP_NM_NLDAP || (nm) == IDMAP_NM_MIXED)
149 #define	AD_OR_MIXED(nm) \
150 	((nm) == IDMAP_NM_AD || (nm) == IDMAP_NM_MIXED)
151 
152 #define	PID_UID_OR_UNKNOWN(pidtype) \
153 	((pidtype) == IDMAP_UID || (pidtype) == IDMAP_POSIXID)
154 #define	PID_GID_OR_UNKNOWN(pidtype) \
155 	((pidtype) == IDMAP_GID || (pidtype) == IDMAP_POSIXID)
156 
157 #define	NLDAP_OR_MIXED_MODE(pidtype, ls) \
158 	(NLDAP_MODE(pidtype, ls) || MIXED_MODE(pidtype, ls))
159 #define	AD_OR_MIXED_MODE(pidtype, ls)\
160 	(AD_MODE(pidtype, ls) || MIXED_MODE(pidtype, ls))
161 #define	NLDAP_MODE(pidtype, ls) \
162 	((PID_UID_OR_UNKNOWN(pidtype) && (ls)->nm_siduid == IDMAP_NM_NLDAP) || \
163 	(PID_GID_OR_UNKNOWN(pidtype) && (ls)->nm_sidgid == IDMAP_NM_NLDAP))
164 #define	AD_MODE(pidtype, ls) \
165 	((PID_UID_OR_UNKNOWN(pidtype) && (ls)->nm_siduid == IDMAP_NM_AD) || \
166 	(PID_GID_OR_UNKNOWN(pidtype) && (ls)->nm_sidgid == IDMAP_NM_AD))
167 #define	MIXED_MODE(pidtype, ls) \
168 	((PID_UID_OR_UNKNOWN(pidtype) && (ls)->nm_siduid == IDMAP_NM_MIXED) || \
169 	(PID_GID_OR_UNKNOWN(pidtype) && (ls)->nm_sidgid == IDMAP_NM_MIXED))
170 
171 
172 typedef struct list_cb_data {
173 	void		*result;
174 	uint64_t	next;
175 	uint64_t	len;
176 	uint64_t	limit;
177 	int		flag;
178 } list_cb_data_t;
179 
180 typedef struct msg_table {
181 	idmap_retcode	retcode;
182 	const char	*msg;
183 } msg_table_t;
184 
185 /*
186  * Data structure to store well-known SIDs and
187  * associated mappings (if any)
188  */
189 typedef struct wksids_table {
190 	const char	*sidprefix;
191 	uint32_t	rid;
192 	const char	*domain;
193 	const char	*winname;
194 	int		is_wuser;
195 	posix_id_t	pid;
196 	int		is_user;
197 	int		direction;
198 } wksids_table_t;
199 
200 #define	IDMAPD_SEARCH_TIMEOUT		3   /* seconds */
201 #define	IDMAPD_LDAP_OPEN_TIMEOUT	1   /* secs; initial, w/ exp backoff */
202 
203 /*
204  * The following flags are used by idmapd while processing a
205  * given mapping request. Note that idmapd uses multiple passes to
206  * process the request and the flags are used to pass information
207  * about the state of the request between these passes.
208  */
209 
210 /* Initial state. Done. Reset all flags. Remaining passes can be skipped */
211 #define	_IDMAP_F_DONE			0x00000000
212 /* Set when subsequent passes are required */
213 #define	_IDMAP_F_NOTDONE		0x00000001
214 /* Don't update name_cache. (e.g. set when winname,SID found in name_cache) */
215 #define	_IDMAP_F_DONT_UPDATE_NAMECACHE	0x00000002
216 /* Batch this request for AD lookup */
217 #define	_IDMAP_F_LOOKUP_AD		0x00000004
218 /* Batch this request for nldap directory lookup */
219 #define	_IDMAP_F_LOOKUP_NLDAP		0x00000008
220 /*
221  * Expired ephemeral mapping found in cache when processing sid2uid request.
222  * Use it if the given SID cannot be mapped by name
223  */
224 #define	_IDMAP_F_EXP_EPH_UID		0x00000010
225 /* Same as above. Used for sid2gid request */
226 #define	_IDMAP_F_EXP_EPH_GID		0x00000020
227 /* This request is not valid for the current forest */
228 #define	_IDMAP_F_LOOKUP_OTHER_AD	0x00000040
229 
230 
231 /*
232  * Check if we are done. If so, subsequent passes can be skipped
233  * when processing a given mapping request.
234  */
235 #define	ARE_WE_DONE(f)	((f & _IDMAP_F_NOTDONE) == 0)
236 
237 #define	SIZE_INCR	5
238 #define	MAX_TRIES	5
239 #define	IDMAP_DBDIR	"/var/idmap"
240 #define	IDMAP_CACHEDIR	"/var/run/idmap"
241 #define	IDMAP_DBNAME	IDMAP_DBDIR "/idmap.db"
242 #define	IDMAP_CACHENAME	IDMAP_CACHEDIR "/idmap.db"
243 
244 #define	IS_ID_NONE(id)	\
245 	((id).idtype == IDMAP_NONE)
246 
247 #define	IS_ID_SID(id)	\
248 	((id).idtype == IDMAP_SID ||	\
249 	(id).idtype == IDMAP_USID ||	\
250 	(id).idtype == IDMAP_GSID)	\
251 
252 #define	IS_ID_UID(id)	\
253 	((id).idtype == IDMAP_UID)
254 
255 #define	IS_ID_GID(id)	\
256 	((id).idtype == IDMAP_GID)
257 
258 #define	IS_ID_POSIX(id)	\
259 	((id).idtype == IDMAP_UID ||	\
260 	(id).idtype == IDMAP_GID ||	\
261 	(id).idtype == IDMAP_POSIXID)	\
262 
263 /*
264  * Local RID ranges
265  */
266 #define	LOCALRID_UID_MIN	1000U
267 #define	LOCALRID_UID_MAX	((uint32_t)INT32_MAX)
268 #define	LOCALRID_GID_MIN	(((uint32_t)INT32_MAX) + 1)
269 #define	LOCALRID_GID_MAX	UINT32_MAX
270 
271 /*
272  * Tracing.
273  *
274  * The tracing mechanism is intended to help the administrator understand
275  * why their mapping configuration is doing what it is.  Each interesting
276  * decision point during the mapping process calls TRACE() with the current
277  * request and response and a printf-style message.  The message, plus
278  * data from the request and the response, is logged to the service log
279  * (if debug/mapping is greater than zero) or reported to the caller
280  * (if IDMAP_REQ_FLG_TRACE was set in the request.  The primary consumer
281  * is the "-V" option to "idmap show".
282  *
283  * TRACING(req) says whether tracing is appropriate for the request, and
284  * is used to determine and record whether any request in a batch requested
285  * tracing, to control whether later code loops over the batch to do tracing
286  * for any of the requests.
287  *
288  * TRACE(req, res, fmt, ...) generates a trace entry if appropriate.
289  */
290 #define	TRACING(req)	\
291 	(DBG(MAPPING, 1) ||	\
292 	((req)->flag & IDMAP_REQ_FLG_TRACE) != 0)
293 #define	TRACE(req, res, ...)			\
294 	((void)(TRACING(req) && trace(req, res, __VA_ARGS__)))
295 extern int	trace(idmap_mapping *req, idmap_id_res *res, char *fmt, ...);
296 
297 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t);
298 typedef int (*list_svc_cb)(void *, int, char **, char **);
299 
300 extern void	idmap_prog_1(struct svc_req *, register SVCXPRT *);
301 extern void	idmapdlog(int, const char *, ...);
302 extern int	init_mapping_system();
303 extern void	fini_mapping_system();
304 extern void	print_idmapdstate();
305 extern int	create_directory(const char *, uid_t, gid_t);
306 extern int	load_config();
307 extern void	reload_ad();
308 extern void	idmap_init_tsd_key(void);
309 extern void	degrade_svc(int, const char *);
310 extern void	restore_svc(void);
311 
312 
313 extern int		init_dbs();
314 extern void		fini_dbs();
315 extern idmap_retcode	get_db_handle(sqlite **);
316 extern idmap_retcode	get_cache_handle(sqlite **);
317 extern idmap_retcode	sql_exec_no_cb(sqlite *, const char *, char *);
318 extern idmap_retcode	add_namerule(sqlite *, idmap_namerule *);
319 extern idmap_retcode	rm_namerule(sqlite *, idmap_namerule *);
320 extern idmap_retcode	flush_namerules(sqlite *);
321 
322 extern char 		*tolower_u8(const char *);
323 
324 extern idmap_retcode	gen_sql_expr_from_rule(idmap_namerule *, char **);
325 extern idmap_retcode	validate_list_cb_data(list_cb_data_t *, int,
326 				char **, int, uchar_t **, size_t);
327 extern idmap_retcode	process_list_svc_sql(sqlite *, const char *, char *,
328 				uint64_t, int, list_svc_cb, void *);
329 extern idmap_retcode	sid2pid_first_pass(lookup_state_t *,
330 				idmap_mapping *, idmap_id_res *);
331 extern idmap_retcode	sid2pid_second_pass(lookup_state_t *,
332 				idmap_mapping *, idmap_id_res *);
333 extern idmap_retcode	pid2sid_first_pass(lookup_state_t *,
334 				idmap_mapping *, idmap_id_res *, int);
335 extern idmap_retcode	pid2sid_second_pass(lookup_state_t *,
336 				idmap_mapping *, idmap_id_res *, int);
337 extern idmap_retcode	update_cache_sid2pid(lookup_state_t *,
338 				idmap_mapping *, idmap_id_res *);
339 extern idmap_retcode	update_cache_pid2sid(lookup_state_t *,
340 				idmap_mapping *, idmap_id_res *);
341 extern idmap_retcode	get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *,
342 				idmap_mapping *, int);
343 extern idmap_retcode	get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *,
344 				idmap_mapping *);
345 extern idmap_retcode	load_cfg_in_state(lookup_state_t *);
346 extern void		cleanup_lookup_state(lookup_state_t *);
347 
348 extern idmap_retcode	ad_lookup_batch(lookup_state_t *,
349 				idmap_mapping_batch *, idmap_ids_res *);
350 extern idmap_retcode	lookup_name2sid(sqlite *, const char *, const char *,
351 				int, char **, char **, char **,
352 				idmap_rid_t *, idmap_id_type *,
353 				idmap_mapping *, int);
354 extern idmap_retcode	lookup_wksids_name2sid(const char *, const char *,
355 				char **, char **, char **, idmap_rid_t *,
356 				idmap_id_type *);
357 extern idmap_retcode	idmap_cache_flush(idmap_flush_op);
358 
359 extern const wksids_table_t *find_wksid_by_pid(posix_id_t pid, int is_user);
360 extern const wksids_table_t *find_wksid_by_sid(const char *sid, int rid,
361     idmap_id_type type);
362 extern const wksids_table_t *find_wksid_by_name(const char *name,
363     const char *domain, idmap_id_type type);
364 extern const wksids_table_t *find_wk_by_sid(char *sid);
365 
366 #ifdef __cplusplus
367 }
368 #endif
369 
370 #endif /* _IDMAPD_H */
371