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.
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.
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]
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
37 #include <sqlite-sys/sqlite.h>
40 #include <rpcsvc/idmap_prot.h>
42 #include "idmap_priv.h"
43 #include "idmap_config.h"
44 #include "libadutils.h"
50 #define CHECK_NULL(s) (s != NULL ? s : "null")
52 extern mutex_t _svcstate_lock
; /* lock for _rpcsvcstate, _rpcsvccount */
54 typedef enum idmap_namemap_mode
{
59 } idmap_namemap_mode_t
;
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.
69 * A typical debugging output sequence would look like
71 * if (DBG(CONFIG, 2)) {
72 * idmapdlog(LOG_DEBUG,
73 * "some message about config at verbosity 2");
78 IDMAPD_DEBUG_CONFIG
= 1,
79 IDMAPD_DEBUG_MAPPING
= 2,
80 IDMAPD_DEBUG_DISC
= 3,
82 IDMAPD_DEBUG_LDAP
= 5,
86 #define DBG(type, lev) \
87 (_idmapdstate.debug[IDMAPD_DEBUG_##type] >= (lev) || \
88 _idmapdstate.debug[IDMAPD_DEBUG_ALL] >= (lev))
91 * Global state of idmapd daemon.
93 typedef struct idmapd_state
{
94 rwlock_t rwlk_cfg
; /* config lock */
95 idmap_cfg_t
*cfg
; /* config */
97 char hostname
[MAX_NAME_LEN
]; /* my hostname */
102 int new_eph_db
; /* was the ephem ID db [re-]created? */
110 int debug
[IDMAPD_DEBUG_MAX
+1];
112 extern idmapd_state_t _idmapdstate
;
114 #define ADDISC_ST_REQUESTED 1
115 #define ADDISC_ST_RUNNING 2
117 #define RDLOCK_CONFIG() \
118 (void) rw_rdlock(&_idmapdstate.rwlk_cfg);
119 #define WRLOCK_CONFIG() \
120 (void) rw_wrlock(&_idmapdstate.rwlk_cfg);
121 #define UNLOCK_CONFIG() \
122 (void) rw_unlock(&_idmapdstate.rwlk_cfg);
124 typedef struct hashentry
{
129 typedef struct lookup_state
{
134 bool_t eph_map_unres_sids
;
135 int directory_based_mapping
; /* enum */
136 uint_t id_cache_timeout
;
137 uint_t name_cache_timeout
;
139 hashentry_t
*sid_history
;
140 uint_t sid_history_size
;
141 idmap_mapping_batch
*batch
;
142 idmap_ids_res
*result
;
143 idmap_namemap_mode_t nm_siduid
;
144 idmap_namemap_mode_t nm_sidgid
;
145 char *ad_unixuser_attr
;
146 char *ad_unixgroup_attr
;
147 char *nldap_winname_attr
;
153 #define NLDAP_OR_MIXED(nm) \
154 ((nm) == IDMAP_NM_NLDAP || (nm) == IDMAP_NM_MIXED)
155 #define AD_OR_MIXED(nm) \
156 ((nm) == IDMAP_NM_AD || (nm) == IDMAP_NM_MIXED)
158 #define PID_UID_OR_UNKNOWN(pidtype) \
159 ((pidtype) == IDMAP_UID || (pidtype) == IDMAP_POSIXID)
160 #define PID_GID_OR_UNKNOWN(pidtype) \
161 ((pidtype) == IDMAP_GID || (pidtype) == IDMAP_POSIXID)
163 #define NLDAP_OR_MIXED_MODE(pidtype, ls) \
164 (NLDAP_MODE(pidtype, ls) || MIXED_MODE(pidtype, ls))
165 #define AD_OR_MIXED_MODE(pidtype, ls)\
166 (AD_MODE(pidtype, ls) || MIXED_MODE(pidtype, ls))
167 #define NLDAP_MODE(pidtype, ls) \
168 ((PID_UID_OR_UNKNOWN(pidtype) && (ls)->nm_siduid == IDMAP_NM_NLDAP) || \
169 (PID_GID_OR_UNKNOWN(pidtype) && (ls)->nm_sidgid == IDMAP_NM_NLDAP))
170 #define AD_MODE(pidtype, ls) \
171 ((PID_UID_OR_UNKNOWN(pidtype) && (ls)->nm_siduid == IDMAP_NM_AD) || \
172 (PID_GID_OR_UNKNOWN(pidtype) && (ls)->nm_sidgid == IDMAP_NM_AD))
173 #define MIXED_MODE(pidtype, ls) \
174 ((PID_UID_OR_UNKNOWN(pidtype) && (ls)->nm_siduid == IDMAP_NM_MIXED) || \
175 (PID_GID_OR_UNKNOWN(pidtype) && (ls)->nm_sidgid == IDMAP_NM_MIXED))
178 typedef struct list_cb_data
{
186 typedef struct msg_table
{
187 idmap_retcode retcode
;
192 * Data structure to store well-known SIDs and
193 * associated mappings (if any)
195 typedef struct wksids_table
{
196 const char *sidprefix
;
206 #define IDMAPD_SEARCH_TIMEOUT 3 /* seconds */
207 #define IDMAPD_LDAP_OPEN_TIMEOUT 1 /* secs; initial, w/ exp backoff */
210 * The following flags are used by idmapd while processing a
211 * given mapping request. Note that idmapd uses multiple passes to
212 * process the request and the flags are used to pass information
213 * about the state of the request between these passes.
216 /* Initial state. Done. Reset all flags. Remaining passes can be skipped */
217 #define _IDMAP_F_DONE 0x00000000
218 /* Set when subsequent passes are required */
219 #define _IDMAP_F_NOTDONE 0x00000001
220 /* Don't update name_cache. (e.g. set when winname,SID found in name_cache) */
221 #define _IDMAP_F_DONT_UPDATE_NAMECACHE 0x00000002
222 /* Batch this request for AD lookup */
223 #define _IDMAP_F_LOOKUP_AD 0x00000004
224 /* Batch this request for nldap directory lookup */
225 #define _IDMAP_F_LOOKUP_NLDAP 0x00000008
227 * Expired ephemeral mapping found in cache when processing sid2uid request.
228 * Use it if the given SID cannot be mapped by name
230 #define _IDMAP_F_EXP_EPH_UID 0x00000010
231 /* Same as above. Used for sid2gid request */
232 #define _IDMAP_F_EXP_EPH_GID 0x00000020
233 /* This request is not valid for the current forest */
234 #define _IDMAP_F_LOOKUP_OTHER_AD 0x00000040
238 * Check if we are done. If so, subsequent passes can be skipped
239 * when processing a given mapping request.
241 #define ARE_WE_DONE(f) ((f & _IDMAP_F_NOTDONE) == 0)
245 #define IDMAP_DBDIR "/var/idmap"
246 #define IDMAP_CACHEDIR "/var/run/idmap"
247 #define IDMAP_DBNAME IDMAP_DBDIR "/idmap.db"
248 #define IDMAP_CACHENAME IDMAP_CACHEDIR "/idmap.db"
250 #define IS_ID_NONE(id) \
251 ((id).idtype == IDMAP_NONE)
253 #define IS_ID_SID(id) \
254 ((id).idtype == IDMAP_SID || \
255 (id).idtype == IDMAP_USID || \
256 (id).idtype == IDMAP_GSID) \
258 #define IS_ID_UID(id) \
259 ((id).idtype == IDMAP_UID)
261 #define IS_ID_GID(id) \
262 ((id).idtype == IDMAP_GID)
264 #define IS_ID_POSIX(id) \
265 ((id).idtype == IDMAP_UID || \
266 (id).idtype == IDMAP_GID || \
267 (id).idtype == IDMAP_POSIXID) \
272 #define LOCALRID_UID_MIN 1000U
273 #define LOCALRID_UID_MAX ((uint32_t)INT32_MAX)
274 #define LOCALRID_GID_MIN (((uint32_t)INT32_MAX) + 1)
275 #define LOCALRID_GID_MAX UINT32_MAX
280 * The tracing mechanism is intended to help the administrator understand
281 * why their mapping configuration is doing what it is. Each interesting
282 * decision point during the mapping process calls TRACE() with the current
283 * request and response and a printf-style message. The message, plus
284 * data from the request and the response, is logged to the service log
285 * (if debug/mapping is greater than zero) or reported to the caller
286 * (if IDMAP_REQ_FLG_TRACE was set in the request. The primary consumer
287 * is the "-V" option to "idmap show".
289 * TRACING(req) says whether tracing is appropriate for the request, and
290 * is used to determine and record whether any request in a batch requested
291 * tracing, to control whether later code loops over the batch to do tracing
292 * for any of the requests.
294 * TRACE(req, res, fmt, ...) generates a trace entry if appropriate.
296 #define TRACING(req) \
297 (DBG(MAPPING, 1) || \
298 ((req)->flag & IDMAP_REQ_FLG_TRACE) != 0)
299 #define TRACE(req, res, ...) \
300 ((void)(TRACING(req) && trace(req, res, __VA_ARGS__)))
301 extern int trace(idmap_mapping
*req
, idmap_id_res
*res
, char *fmt
, ...);
303 typedef idmap_retcode (*update_list_res_cb
)(void *, const char **, uint64_t);
304 typedef int (*list_svc_cb
)(void *, int, char **, char **);
306 extern void idmap_prog_1(struct svc_req
*, register SVCXPRT
*);
307 extern void idmapdlog(int, const char *, ...);
308 extern int init_mapping_system();
309 extern void fini_mapping_system();
310 extern void print_idmapdstate();
311 extern int create_directory(const char *, uid_t
, gid_t
);
312 extern int load_config();
313 extern void reload_ad();
314 extern void idmap_init_tsd_key(void);
315 extern void degrade_svc(int, const char *);
316 extern void restore_svc(void);
317 extern void notify_dc_changed(void);
320 extern int init_dbs();
321 extern void fini_dbs();
322 extern idmap_retcode
get_db_handle(sqlite
**);
323 extern idmap_retcode
get_cache_handle(sqlite
**);
324 extern idmap_retcode
sql_exec_no_cb(sqlite
*, const char *, char *);
325 extern idmap_retcode
add_namerule(sqlite
*, idmap_namerule
*);
326 extern idmap_retcode
rm_namerule(sqlite
*, idmap_namerule
*);
327 extern idmap_retcode
flush_namerules(sqlite
*);
329 extern char *tolower_u8(const char *);
331 extern idmap_retcode
gen_sql_expr_from_rule(idmap_namerule
*, char **);
332 extern idmap_retcode
validate_list_cb_data(list_cb_data_t
*, int,
333 char **, int, uchar_t
**, size_t);
334 extern idmap_retcode
process_list_svc_sql(sqlite
*, const char *, char *,
335 uint64_t, int, list_svc_cb
, void *);
336 extern idmap_retcode
sid2pid_first_pass(lookup_state_t
*,
337 idmap_mapping
*, idmap_id_res
*);
338 extern idmap_retcode
sid2pid_second_pass(lookup_state_t
*,
339 idmap_mapping
*, idmap_id_res
*);
340 extern idmap_retcode
pid2sid_first_pass(lookup_state_t
*,
341 idmap_mapping
*, idmap_id_res
*, int);
342 extern idmap_retcode
pid2sid_second_pass(lookup_state_t
*,
343 idmap_mapping
*, idmap_id_res
*, int);
344 extern idmap_retcode
update_cache_sid2pid(lookup_state_t
*,
345 idmap_mapping
*, idmap_id_res
*);
346 extern idmap_retcode
update_cache_pid2sid(lookup_state_t
*,
347 idmap_mapping
*, idmap_id_res
*);
348 extern idmap_retcode
get_u2w_mapping(sqlite
*, sqlite
*, idmap_mapping
*,
349 idmap_mapping
*, int);
350 extern idmap_retcode
get_w2u_mapping(sqlite
*, sqlite
*, idmap_mapping
*,
352 extern idmap_retcode
load_cfg_in_state(lookup_state_t
*);
353 extern void cleanup_lookup_state(lookup_state_t
*);
355 extern idmap_retcode
ad_lookup_batch(lookup_state_t
*,
356 idmap_mapping_batch
*, idmap_ids_res
*);
357 extern idmap_retcode
lookup_name2sid(sqlite
*, const char *, const char *,
358 int, char **, char **, char **,
359 idmap_rid_t
*, idmap_id_type
*,
360 idmap_mapping
*, int);
361 extern idmap_retcode
lookup_wksids_name2sid(const char *, const char *,
362 char **, char **, char **, idmap_rid_t
*,
364 extern idmap_retcode
idmap_cache_flush(idmap_flush_op
);
366 extern const wksids_table_t
*find_wksid_by_pid(posix_id_t pid
, int is_user
);
367 extern const wksids_table_t
*find_wksid_by_sid(const char *sid
, int rid
,
369 extern const wksids_table_t
*find_wksid_by_name(const char *name
,
370 const char *domain
, idmap_id_type type
);
371 extern const wksids_table_t
*find_wk_by_sid(char *sid
);
377 #endif /* _IDMAPD_H */