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