1c5c4113dSnw141292 /* 2c5c4113dSnw141292 * CDDL HEADER START 3c5c4113dSnw141292 * 4c5c4113dSnw141292 * The contents of this file are subject to the terms of the 5c5c4113dSnw141292 * Common Development and Distribution License (the "License"). 6c5c4113dSnw141292 * You may not use this file except in compliance with the License. 7c5c4113dSnw141292 * 8c5c4113dSnw141292 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9c5c4113dSnw141292 * or http://www.opensolaris.org/os/licensing. 10c5c4113dSnw141292 * See the License for the specific language governing permissions 11c5c4113dSnw141292 * and limitations under the License. 12c5c4113dSnw141292 * 13c5c4113dSnw141292 * When distributing Covered Code, include this CDDL HEADER in each 14c5c4113dSnw141292 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15c5c4113dSnw141292 * If applicable, add the following below this CDDL HEADER, with the 16c5c4113dSnw141292 * fields enclosed by brackets "[]" replaced with your own identifying 17c5c4113dSnw141292 * information: Portions Copyright [yyyy] [name of copyright owner] 18c5c4113dSnw141292 * 19c5c4113dSnw141292 * CDDL HEADER END 20c5c4113dSnw141292 */ 21c5c4113dSnw141292 /* 220dcc7149Snw141292 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23c5c4113dSnw141292 * Use is subject to license terms. 24c5c4113dSnw141292 */ 25c5c4113dSnw141292 26c5c4113dSnw141292 #ifndef _IDMAPD_H 27c5c4113dSnw141292 #define _IDMAPD_H 28c5c4113dSnw141292 29c5c4113dSnw141292 #include <stdio.h> 30c5c4113dSnw141292 #include <stdlib.h> 31c5c4113dSnw141292 #include <stdarg.h> 32c5c4113dSnw141292 #include <rpc/rpc.h> 33c5c4113dSnw141292 #include <synch.h> 34c5c4113dSnw141292 #include <thread.h> 35c5c4113dSnw141292 #include <libintl.h> 36c5c4113dSnw141292 #include <strings.h> 37c5c4113dSnw141292 #include <sqlite/sqlite.h> 38c5c4113dSnw141292 #include <inttypes.h> 39c5c4113dSnw141292 #include "idmap_prot.h" 40c5c4113dSnw141292 #include "adutils.h" 41c5c4113dSnw141292 #include "idmap_config.h" 42*2b4a7802SBaban Kenkre #include "libadutils.h" 43c5c4113dSnw141292 44c5c4113dSnw141292 #ifdef __cplusplus 45c5c4113dSnw141292 extern "C" { 46c5c4113dSnw141292 #endif 47c5c4113dSnw141292 48c5c4113dSnw141292 /* States a server can be in wrt request */ 49c5c4113dSnw141292 #define _IDLE 0 50c5c4113dSnw141292 #define _SERVED 1 51c5c4113dSnw141292 52c5c4113dSnw141292 #define SENTINEL_PID UINT32_MAX 53479ac375Sdm199847 #define CHECK_NULL(s) (s != NULL ? s : "null") 54c5c4113dSnw141292 55c5c4113dSnw141292 extern int _rpcsvcstate; /* set when a request is serviced */ 56c5c4113dSnw141292 extern int _rpcsvccount; /* number of requests being serviced */ 57c5c4113dSnw141292 extern mutex_t _svcstate_lock; /* lock for _rpcsvcstate, _rpcsvccount */ 58c5c4113dSnw141292 59e8c27ec8Sbaban typedef enum idmap_namemap_mode { 60e8c27ec8Sbaban IDMAP_NM_NONE = 0, 61e8c27ec8Sbaban IDMAP_NM_AD, 62e8c27ec8Sbaban IDMAP_NM_NLDAP, 63e8c27ec8Sbaban IDMAP_NM_MIXED 64e8c27ec8Sbaban } idmap_namemap_mode_t; 65e8c27ec8Sbaban 66c5c4113dSnw141292 /* 67c5c4113dSnw141292 * Global state of idmapd daemon. 68c5c4113dSnw141292 */ 69c5c4113dSnw141292 #define IDMAP_MAX_NAME_LEN 512 70c5c4113dSnw141292 typedef struct idmapd_state { 71c5c4113dSnw141292 rwlock_t rwlk_cfg; /* config lock */ 72c5c4113dSnw141292 idmap_cfg_t *cfg; /* config */ 7371590c90Snw141292 bool_t daemon_mode; 7471590c90Snw141292 bool_t debug_mode; 75c5c4113dSnw141292 char hostname[MAX_NAME_LEN]; /* my hostname */ 76c5c4113dSnw141292 uid_t next_uid; 77c5c4113dSnw141292 gid_t next_gid; 78c5c4113dSnw141292 uid_t limit_uid; 79c5c4113dSnw141292 gid_t limit_gid; 80c5c4113dSnw141292 int new_eph_db; /* was the ephem ID db [re-]created? */ 814aa0a5e7Snw141292 bool_t eph_map_unres_sids; 82*2b4a7802SBaban Kenkre adutils_ad_t *ad; 83c5c4113dSnw141292 } idmapd_state_t; 84c5c4113dSnw141292 extern idmapd_state_t _idmapdstate; 85c5c4113dSnw141292 86c5c4113dSnw141292 #define RDLOCK_CONFIG() \ 87c5c4113dSnw141292 (void) rw_rdlock(&_idmapdstate.rwlk_cfg); 88c5c4113dSnw141292 #define WRLOCK_CONFIG() \ 89c5c4113dSnw141292 (void) rw_wrlock(&_idmapdstate.rwlk_cfg); 90c5c4113dSnw141292 #define UNLOCK_CONFIG() \ 91c5c4113dSnw141292 (void) rw_unlock(&_idmapdstate.rwlk_cfg); 92c5c4113dSnw141292 9362c60062Sbaban typedef struct hashentry { 9462c60062Sbaban uint_t key; 9562c60062Sbaban uint_t next; 9662c60062Sbaban } hashentry_t; 9762c60062Sbaban 98c5c4113dSnw141292 typedef struct lookup_state { 99c5c4113dSnw141292 bool_t sid2pid_done; 100c5c4113dSnw141292 bool_t pid2sid_done; 101c5c4113dSnw141292 int ad_nqueries; 102e8c27ec8Sbaban int nldap_nqueries; 1034aa0a5e7Snw141292 bool_t eph_map_unres_sids; 10462c60062Sbaban uint_t curpos; 10562c60062Sbaban hashentry_t *sid_history; 10662c60062Sbaban uint_t sid_history_size; 10762c60062Sbaban idmap_mapping_batch *batch; 10862c60062Sbaban idmap_ids_res *result; 109e8c27ec8Sbaban idmap_namemap_mode_t nm_siduid; 110e8c27ec8Sbaban idmap_namemap_mode_t nm_sidgid; 111e8c27ec8Sbaban char *ad_unixuser_attr; 112e8c27ec8Sbaban char *ad_unixgroup_attr; 113479ac375Sdm199847 char *nldap_winname_attr; 114479ac375Sdm199847 char *defdom; 115479ac375Sdm199847 sqlite *cache; 116479ac375Sdm199847 sqlite *db; 117c5c4113dSnw141292 } lookup_state_t; 118c5c4113dSnw141292 119e8c27ec8Sbaban #define NLDAP_OR_MIXED(nm) \ 120e8c27ec8Sbaban (nm == IDMAP_NM_NLDAP || nm == IDMAP_NM_MIXED) 121e8c27ec8Sbaban #define AD_OR_MIXED(nm) \ 122e8c27ec8Sbaban (nm == IDMAP_NM_AD || nm == IDMAP_NM_MIXED) 123e8c27ec8Sbaban 124e8c27ec8Sbaban #define NLDAP_OR_MIXED_MODE(pidtype, ls) \ 125e8c27ec8Sbaban ((pidtype == IDMAP_UID && NLDAP_OR_MIXED(ls->nm_siduid)) || \ 126e8c27ec8Sbaban (pidtype == IDMAP_GID && NLDAP_OR_MIXED(ls->nm_sidgid))) 127e8c27ec8Sbaban #define AD_OR_MIXED_MODE(pidtype, ls)\ 128e8c27ec8Sbaban ((pidtype == IDMAP_UID && AD_OR_MIXED(ls->nm_siduid)) || \ 129e8c27ec8Sbaban (pidtype == IDMAP_GID && AD_OR_MIXED(ls->nm_sidgid))) 130e8c27ec8Sbaban #define NLDAP_MODE(pidtype, ls) \ 131e8c27ec8Sbaban ((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_NLDAP) || \ 132e8c27ec8Sbaban (pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_NLDAP)) 133e8c27ec8Sbaban #define AD_MODE(pidtype, ls) \ 134e8c27ec8Sbaban ((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_AD) || \ 135e8c27ec8Sbaban (pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_AD)) 136e8c27ec8Sbaban #define MIXED_MODE(pidtype, ls) \ 137e8c27ec8Sbaban ((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_MIXED) || \ 138e8c27ec8Sbaban (pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_MIXED)) 139e8c27ec8Sbaban 140e8c27ec8Sbaban 141c5c4113dSnw141292 typedef struct list_cb_data { 142c5c4113dSnw141292 void *result; 143c5c4113dSnw141292 uint64_t next; 144c5c4113dSnw141292 uint64_t len; 145c5c4113dSnw141292 uint64_t limit; 14648258c6bSjp151216 int flag; 147c5c4113dSnw141292 } list_cb_data_t; 148c5c4113dSnw141292 149c5c4113dSnw141292 typedef struct msg_table { 150c5c4113dSnw141292 idmap_retcode retcode; 151c5c4113dSnw141292 const char *msg; 152c5c4113dSnw141292 } msg_table_t; 153c5c4113dSnw141292 1540dcc7149Snw141292 #define IDMAPD_SEARCH_TIMEOUT 3 /* seconds */ 1550dcc7149Snw141292 #define IDMAPD_LDAP_OPEN_TIMEOUT 1 /* secs; initial, w/ exp backoff */ 1560dcc7149Snw141292 15762c60062Sbaban /* 158e8c27ec8Sbaban * The following flags are used by idmapd while processing a 159e8c27ec8Sbaban * given mapping request. Note that idmapd uses multiple passes to 160e8c27ec8Sbaban * process the request and the flags are used to pass information 161e8c27ec8Sbaban * about the state of the request between these passes. 16262c60062Sbaban */ 163c5c4113dSnw141292 164e8c27ec8Sbaban /* Initial state. Done. Reset all flags. Remaining passes can be skipped */ 165c5c4113dSnw141292 #define _IDMAP_F_DONE 0x00000000 166e8c27ec8Sbaban /* Set when subsequent passes are required */ 167e8c27ec8Sbaban #define _IDMAP_F_NOTDONE 0x00000001 168e8c27ec8Sbaban /* Don't update name_cache. (e.g. set when winname,SID found in name_cache) */ 169e8c27ec8Sbaban #define _IDMAP_F_DONT_UPDATE_NAMECACHE 0x00000002 170e8c27ec8Sbaban /* Batch this request for AD lookup */ 171e8c27ec8Sbaban #define _IDMAP_F_LOOKUP_AD 0x00000004 172e8c27ec8Sbaban /* Batch this request for nldap directory lookup */ 173e8c27ec8Sbaban #define _IDMAP_F_LOOKUP_NLDAP 0x00000008 174e8c27ec8Sbaban /* 175e8c27ec8Sbaban * Expired ephemeral mapping found in cache when processing sid2uid request. 176e8c27ec8Sbaban * Use it if the given SID cannot be mapped by name 177e8c27ec8Sbaban */ 178e8c27ec8Sbaban #define _IDMAP_F_EXP_EPH_UID 0x00000010 179e8c27ec8Sbaban /* Same as above. Used for sid2gid request */ 180e8c27ec8Sbaban #define _IDMAP_F_EXP_EPH_GID 0x00000020 181e8c27ec8Sbaban 182e8c27ec8Sbaban /* 183e8c27ec8Sbaban * Check if we are done. If so, subsequent passes can be skipped 184e8c27ec8Sbaban * when processing a given mapping request. 185e8c27ec8Sbaban */ 186e8c27ec8Sbaban #define ARE_WE_DONE(f) ((f & _IDMAP_F_NOTDONE) == 0) 187c5c4113dSnw141292 188c5c4113dSnw141292 #define SIZE_INCR 5 189c5c4113dSnw141292 #define MAX_TRIES 5 190c5c4113dSnw141292 #define IDMAP_DBDIR "/var/idmap" 191c5c4113dSnw141292 #define IDMAP_CACHEDIR "/var/run/idmap" 192c5c4113dSnw141292 #define IDMAP_DBNAME IDMAP_DBDIR "/idmap.db" 193c5c4113dSnw141292 #define IDMAP_CACHENAME IDMAP_CACHEDIR "/idmap.db" 1948e228215Sdm199847 195cd37da74Snw141292 #define IS_BATCH_SID(batch, i) \ 196cd37da74Snw141292 (batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID || \ 197cd37da74Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_USID || \ 198cd37da74Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GSID) 199cd37da74Snw141292 200cd37da74Snw141292 #define IS_BATCH_UID(batch, i) \ 2010dcc7149Snw141292 (batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID) 202cd37da74Snw141292 203cd37da74Snw141292 #define IS_BATCH_GID(batch, i) \ 2040dcc7149Snw141292 (batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID) 205cd37da74Snw141292 206cd37da74Snw141292 #define IS_REQUEST_SID(req, n) \ 207cd37da74Snw141292 ((req).id##n.idtype == IDMAP_SID || \ 208cd37da74Snw141292 (req).id##n.idtype == IDMAP_USID || \ 209cd37da74Snw141292 (req).id##n.idtype == IDMAP_GSID) \ 210cd37da74Snw141292 211cd37da74Snw141292 212cd37da74Snw141292 #define IS_REQUEST_UID(request) \ 2130dcc7149Snw141292 ((request).id1.idtype == IDMAP_UID) 214cd37da74Snw141292 215cd37da74Snw141292 #define IS_REQUEST_GID(request) \ 2160dcc7149Snw141292 ((request).id1.idtype == IDMAP_GID) 217cd37da74Snw141292 218c5c4113dSnw141292 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t); 219c5c4113dSnw141292 typedef int (*list_svc_cb)(void *, int, char **, char **); 220c5c4113dSnw141292 221c5c4113dSnw141292 extern void idmap_prog_1(struct svc_req *, register SVCXPRT *); 222c5c4113dSnw141292 extern void idmapdlog(int, const char *, ...); 223c5c4113dSnw141292 extern int init_mapping_system(); 224c5c4113dSnw141292 extern void fini_mapping_system(); 225c5c4113dSnw141292 extern void print_idmapdstate(); 226c5c4113dSnw141292 extern int create_directory(const char *, uid_t, gid_t); 227c5c4113dSnw141292 extern int load_config(); 228349d5d8fSnw141292 extern void reload_ad(); 22984decf41Sjp151216 extern int idmap_init_tsd_key(void); 230349d5d8fSnw141292 extern void degrade_svc(int, const char *); 231c8e26105Sjp151216 extern void restore_svc(void); 232c5c4113dSnw141292 233c5c4113dSnw141292 234c5c4113dSnw141292 extern int init_dbs(); 235c5c4113dSnw141292 extern void fini_dbs(); 236c5c4113dSnw141292 extern idmap_retcode get_db_handle(sqlite **); 237c5c4113dSnw141292 extern idmap_retcode get_cache_handle(sqlite **); 23871590c90Snw141292 extern idmap_retcode sql_exec_no_cb(sqlite *, const char *, char *); 239c5c4113dSnw141292 extern idmap_retcode add_namerule(sqlite *, idmap_namerule *); 240c5c4113dSnw141292 extern idmap_retcode rm_namerule(sqlite *, idmap_namerule *); 241cd37da74Snw141292 extern idmap_retcode flush_namerules(sqlite *); 242c5c4113dSnw141292 243cd37da74Snw141292 extern char *tolower_u8(const char *); 244cd37da74Snw141292 245cd37da74Snw141292 extern idmap_retcode gen_sql_expr_from_rule(idmap_namerule *, char **); 246c5c4113dSnw141292 extern idmap_retcode validate_list_cb_data(list_cb_data_t *, int, 247c5c4113dSnw141292 char **, int, uchar_t **, size_t); 24871590c90Snw141292 extern idmap_retcode process_list_svc_sql(sqlite *, const char *, char *, 24948258c6bSjp151216 uint64_t, int, list_svc_cb, void *); 250479ac375Sdm199847 extern idmap_retcode sid2pid_first_pass(lookup_state_t *, 251c5c4113dSnw141292 idmap_mapping *, idmap_id_res *); 252479ac375Sdm199847 extern idmap_retcode sid2pid_second_pass(lookup_state_t *, 253479ac375Sdm199847 idmap_mapping *, idmap_id_res *); 254479ac375Sdm199847 extern idmap_retcode pid2sid_first_pass(lookup_state_t *, 255e8c27ec8Sbaban idmap_mapping *, idmap_id_res *, int, int); 256479ac375Sdm199847 extern idmap_retcode pid2sid_second_pass(lookup_state_t *, 257479ac375Sdm199847 idmap_mapping *, idmap_id_res *, int); 258479ac375Sdm199847 extern idmap_retcode update_cache_sid2pid(lookup_state_t *, 259c5c4113dSnw141292 idmap_mapping *, idmap_id_res *); 260479ac375Sdm199847 extern idmap_retcode update_cache_pid2sid(lookup_state_t *, 261c5c4113dSnw141292 idmap_mapping *, idmap_id_res *); 262c5c4113dSnw141292 extern idmap_retcode get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *, 263c5c4113dSnw141292 idmap_mapping *, int); 264c5c4113dSnw141292 extern idmap_retcode get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *, 265c5c4113dSnw141292 idmap_mapping *); 266479ac375Sdm199847 extern idmap_retcode load_cfg_in_state(lookup_state_t *); 267e8c27ec8Sbaban extern void cleanup_lookup_state(lookup_state_t *); 268c5c4113dSnw141292 269e8c27ec8Sbaban extern idmap_retcode ad_lookup_batch(lookup_state_t *, 270c5c4113dSnw141292 idmap_mapping_batch *, idmap_ids_res *); 271479ac375Sdm199847 extern idmap_retcode lookup_name2sid(sqlite *, const char *, const char *, 272479ac375Sdm199847 int *, char **, char **, idmap_rid_t *, 273479ac375Sdm199847 idmap_mapping *, int); 274479ac375Sdm199847 extern idmap_retcode lookup_wksids_name2sid(const char *, char **, char **, 275479ac375Sdm199847 idmap_rid_t *, int *); 276c5c4113dSnw141292 277c5c4113dSnw141292 #ifdef __cplusplus 278c5c4113dSnw141292 } 279c5c4113dSnw141292 #endif 280c5c4113dSnw141292 281c5c4113dSnw141292 #endif /* _IDMAPD_H */ 282