xref: /titanic_50/usr/src/cmd/idmap/idmapd/idmapd.h (revision a530e0a9c7875fde3c123c906ca193a70dfacc4f)
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	PID_UID_OR_UNKNOWN(pidtype) \
150 	((pidtype) == IDMAP_UID || (pidtype) == IDMAP_POSIXID)
151 #define	PID_GID_OR_UNKNOWN(pidtype) \
152 	((pidtype) == IDMAP_GID || (pidtype) == IDMAP_POSIXID)
153 
154 #define	NLDAP_OR_MIXED_MODE(pidtype, ls) \
155 	(NLDAP_MODE(pidtype, ls) || MIXED_MODE(pidtype, ls))
156 #define	AD_OR_MIXED_MODE(pidtype, ls)\
157 	(AD_MODE(pidtype, ls) || MIXED_MODE(pidtype, ls))
158 #define	NLDAP_MODE(pidtype, ls) \
159 	((PID_UID_OR_UNKNOWN(pidtype) && (ls)->nm_siduid == IDMAP_NM_NLDAP) || \
160 	(PID_GID_OR_UNKNOWN(pidtype) && (ls)->nm_sidgid == IDMAP_NM_NLDAP))
161 #define	AD_MODE(pidtype, ls) \
162 	((PID_UID_OR_UNKNOWN(pidtype) && (ls)->nm_siduid == IDMAP_NM_AD) || \
163 	(PID_GID_OR_UNKNOWN(pidtype) && (ls)->nm_sidgid == IDMAP_NM_AD))
164 #define	MIXED_MODE(pidtype, ls) \
165 	((PID_UID_OR_UNKNOWN(pidtype) && (ls)->nm_siduid == IDMAP_NM_MIXED) || \
166 	(PID_GID_OR_UNKNOWN(pidtype) && (ls)->nm_sidgid == IDMAP_NM_MIXED))
167 
168 
169 typedef struct list_cb_data {
170 	void		*result;
171 	uint64_t	next;
172 	uint64_t	len;
173 	uint64_t	limit;
174 	int		flag;
175 } list_cb_data_t;
176 
177 typedef struct msg_table {
178 	idmap_retcode	retcode;
179 	const char	*msg;
180 } msg_table_t;
181 
182 /*
183  * Data structure to store well-known SIDs and
184  * associated mappings (if any)
185  */
186 typedef struct wksids_table {
187 	const char	*sidprefix;
188 	uint32_t	rid;
189 	const char	*domain;
190 	const char	*winname;
191 	int		is_wuser;
192 	posix_id_t	pid;
193 	int		is_user;
194 	int		direction;
195 } wksids_table_t;
196 
197 #define	IDMAPD_SEARCH_TIMEOUT		3   /* seconds */
198 #define	IDMAPD_LDAP_OPEN_TIMEOUT	1   /* secs; initial, w/ exp backoff */
199 
200 /*
201  * The following flags are used by idmapd while processing a
202  * given mapping request. Note that idmapd uses multiple passes to
203  * process the request and the flags are used to pass information
204  * about the state of the request between these passes.
205  */
206 
207 /* Initial state. Done. Reset all flags. Remaining passes can be skipped */
208 #define	_IDMAP_F_DONE			0x00000000
209 /* Set when subsequent passes are required */
210 #define	_IDMAP_F_NOTDONE		0x00000001
211 /* Don't update name_cache. (e.g. set when winname,SID found in name_cache) */
212 #define	_IDMAP_F_DONT_UPDATE_NAMECACHE	0x00000002
213 /* Batch this request for AD lookup */
214 #define	_IDMAP_F_LOOKUP_AD		0x00000004
215 /* Batch this request for nldap directory lookup */
216 #define	_IDMAP_F_LOOKUP_NLDAP		0x00000008
217 /*
218  * Expired ephemeral mapping found in cache when processing sid2uid request.
219  * Use it if the given SID cannot be mapped by name
220  */
221 #define	_IDMAP_F_EXP_EPH_UID		0x00000010
222 /* Same as above. Used for sid2gid request */
223 #define	_IDMAP_F_EXP_EPH_GID		0x00000020
224 /* This request is not valid for the current forest */
225 #define	_IDMAP_F_LOOKUP_OTHER_AD	0x00000040
226 
227 
228 /*
229  * Check if we are done. If so, subsequent passes can be skipped
230  * when processing a given mapping request.
231  */
232 #define	ARE_WE_DONE(f)	((f & _IDMAP_F_NOTDONE) == 0)
233 
234 #define	SIZE_INCR	5
235 #define	MAX_TRIES	5
236 #define	IDMAP_DBDIR	"/var/idmap"
237 #define	IDMAP_CACHEDIR	"/var/run/idmap"
238 #define	IDMAP_DBNAME	IDMAP_DBDIR "/idmap.db"
239 #define	IDMAP_CACHENAME	IDMAP_CACHEDIR "/idmap.db"
240 
241 #define	IS_ID_NONE(id)	\
242 	((id).idtype == IDMAP_NONE)
243 
244 #define	IS_ID_SID(id)	\
245 	((id).idtype == IDMAP_SID ||	\
246 	(id).idtype == IDMAP_USID ||	\
247 	(id).idtype == IDMAP_GSID)	\
248 
249 #define	IS_ID_UID(id)	\
250 	((id).idtype == IDMAP_UID)
251 
252 #define	IS_ID_GID(id)	\
253 	((id).idtype == IDMAP_GID)
254 
255 #define	IS_ID_POSIX(id)	\
256 	((id).idtype == IDMAP_UID ||	\
257 	(id).idtype == IDMAP_GID ||	\
258 	(id).idtype == IDMAP_POSIXID)	\
259 
260 /*
261  * Local RID ranges
262  */
263 #define	LOCALRID_UID_MIN	1000U
264 #define	LOCALRID_UID_MAX	((uint32_t)INT32_MAX)
265 #define	LOCALRID_GID_MIN	(((uint32_t)INT32_MAX) + 1)
266 #define	LOCALRID_GID_MAX	UINT32_MAX
267 
268 /*
269  * Tracing.
270  *
271  * The tracing mechanism is intended to help the administrator understand
272  * why their mapping configuration is doing what it is.  Each interesting
273  * decision point during the mapping process calls TRACE() with the current
274  * request and response and a printf-style message.  The message, plus
275  * data from the request and the response, is logged to the service log
276  * (if debug/mapping is greater than zero) or reported to the caller
277  * (if IDMAP_REQ_FLG_TRACE was set in the request.  The primary consumer
278  * is the "-V" option to "idmap show".
279  *
280  * TRACING(req) says whether tracing is appropriate for the request, and
281  * is used to determine and record whether any request in a batch requested
282  * tracing, to control whether later code loops over the batch to do tracing
283  * for any of the requests.
284  *
285  * TRACE(req, res, fmt, ...) generates a trace entry if appropriate.
286  */
287 #define	TRACING(req)	\
288 	(DBG(MAPPING, 1) ||	\
289 	((req)->flag & IDMAP_REQ_FLG_TRACE) != 0)
290 #define	TRACE(req, res, ...)			\
291 	((void)(TRACING(req) && trace(req, res, __VA_ARGS__)))
292 extern int	trace(idmap_mapping *req, idmap_id_res *res, char *fmt, ...);
293 
294 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t);
295 typedef int (*list_svc_cb)(void *, int, char **, char **);
296 
297 extern void	idmap_prog_1(struct svc_req *, register SVCXPRT *);
298 extern void	idmapdlog(int, const char *, ...);
299 extern int	init_mapping_system();
300 extern void	fini_mapping_system();
301 extern void	print_idmapdstate();
302 extern int	create_directory(const char *, uid_t, gid_t);
303 extern int	load_config();
304 extern void	reload_ad();
305 extern void	idmap_init_tsd_key(void);
306 extern void	degrade_svc(int, const char *);
307 extern void	restore_svc(void);
308 
309 
310 extern int		init_dbs();
311 extern void		fini_dbs();
312 extern idmap_retcode	get_db_handle(sqlite **);
313 extern idmap_retcode	get_cache_handle(sqlite **);
314 extern idmap_retcode	sql_exec_no_cb(sqlite *, const char *, char *);
315 extern idmap_retcode	add_namerule(sqlite *, idmap_namerule *);
316 extern idmap_retcode	rm_namerule(sqlite *, idmap_namerule *);
317 extern idmap_retcode	flush_namerules(sqlite *);
318 
319 extern char 		*tolower_u8(const char *);
320 
321 extern idmap_retcode	gen_sql_expr_from_rule(idmap_namerule *, char **);
322 extern idmap_retcode	validate_list_cb_data(list_cb_data_t *, int,
323 				char **, int, uchar_t **, size_t);
324 extern idmap_retcode	process_list_svc_sql(sqlite *, const char *, char *,
325 				uint64_t, int, list_svc_cb, void *);
326 extern idmap_retcode	sid2pid_first_pass(lookup_state_t *,
327 				idmap_mapping *, idmap_id_res *);
328 extern idmap_retcode	sid2pid_second_pass(lookup_state_t *,
329 				idmap_mapping *, idmap_id_res *);
330 extern idmap_retcode	pid2sid_first_pass(lookup_state_t *,
331 				idmap_mapping *, idmap_id_res *, int);
332 extern idmap_retcode	pid2sid_second_pass(lookup_state_t *,
333 				idmap_mapping *, idmap_id_res *, int);
334 extern idmap_retcode	update_cache_sid2pid(lookup_state_t *,
335 				idmap_mapping *, idmap_id_res *);
336 extern idmap_retcode	update_cache_pid2sid(lookup_state_t *,
337 				idmap_mapping *, idmap_id_res *);
338 extern idmap_retcode	get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *,
339 				idmap_mapping *, int);
340 extern idmap_retcode	get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *,
341 				idmap_mapping *);
342 extern idmap_retcode	load_cfg_in_state(lookup_state_t *);
343 extern void		cleanup_lookup_state(lookup_state_t *);
344 
345 extern idmap_retcode	ad_lookup_batch(lookup_state_t *,
346 				idmap_mapping_batch *, idmap_ids_res *);
347 extern idmap_retcode	lookup_name2sid(sqlite *, const char *, const char *,
348 				int, char **, char **, char **,
349 				idmap_rid_t *, idmap_id_type *,
350 				idmap_mapping *, int);
351 extern idmap_retcode	lookup_wksids_name2sid(const char *, const char *,
352 				char **, char **, char **, idmap_rid_t *,
353 				idmap_id_type *);
354 extern idmap_retcode	idmap_cache_flush(idmap_flush_op);
355 
356 extern const wksids_table_t *find_wksid_by_pid(posix_id_t pid, int is_user);
357 extern const wksids_table_t *find_wksid_by_sid(const char *sid, int rid,
358     idmap_id_type type);
359 extern const wksids_table_t *find_wksid_by_name(const char *name,
360     const char *domain, idmap_id_type type);
361 extern const wksids_table_t *find_wk_by_sid(char *sid);
362 
363 #ifdef __cplusplus
364 }
365 #endif
366 
367 #endif /* _IDMAPD_H */
368