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