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" 422b4a7802SBaban 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*4d61c878SJulian Pullen int num_ads; 83*4d61c878SJulian Pullen adutils_ad_t **ads; 84c5c4113dSnw141292 } idmapd_state_t; 85c5c4113dSnw141292 extern idmapd_state_t _idmapdstate; 86c5c4113dSnw141292 87c5c4113dSnw141292 #define RDLOCK_CONFIG() \ 88c5c4113dSnw141292 (void) rw_rdlock(&_idmapdstate.rwlk_cfg); 89c5c4113dSnw141292 #define WRLOCK_CONFIG() \ 90c5c4113dSnw141292 (void) rw_wrlock(&_idmapdstate.rwlk_cfg); 91c5c4113dSnw141292 #define UNLOCK_CONFIG() \ 92c5c4113dSnw141292 (void) rw_unlock(&_idmapdstate.rwlk_cfg); 93c5c4113dSnw141292 9462c60062Sbaban typedef struct hashentry { 9562c60062Sbaban uint_t key; 9662c60062Sbaban uint_t next; 9762c60062Sbaban } hashentry_t; 9862c60062Sbaban 99c5c4113dSnw141292 typedef struct lookup_state { 100c5c4113dSnw141292 bool_t sid2pid_done; 101c5c4113dSnw141292 bool_t pid2sid_done; 102c5c4113dSnw141292 int ad_nqueries; 103e8c27ec8Sbaban int nldap_nqueries; 1044aa0a5e7Snw141292 bool_t eph_map_unres_sids; 10562c60062Sbaban uint_t curpos; 10662c60062Sbaban hashentry_t *sid_history; 10762c60062Sbaban uint_t sid_history_size; 10862c60062Sbaban idmap_mapping_batch *batch; 10962c60062Sbaban idmap_ids_res *result; 110e8c27ec8Sbaban idmap_namemap_mode_t nm_siduid; 111e8c27ec8Sbaban idmap_namemap_mode_t nm_sidgid; 112e8c27ec8Sbaban char *ad_unixuser_attr; 113e8c27ec8Sbaban char *ad_unixgroup_attr; 114479ac375Sdm199847 char *nldap_winname_attr; 115479ac375Sdm199847 char *defdom; 116479ac375Sdm199847 sqlite *cache; 117479ac375Sdm199847 sqlite *db; 118c5c4113dSnw141292 } lookup_state_t; 119c5c4113dSnw141292 120e8c27ec8Sbaban #define NLDAP_OR_MIXED(nm) \ 121e8c27ec8Sbaban (nm == IDMAP_NM_NLDAP || nm == IDMAP_NM_MIXED) 122e8c27ec8Sbaban #define AD_OR_MIXED(nm) \ 123e8c27ec8Sbaban (nm == IDMAP_NM_AD || nm == IDMAP_NM_MIXED) 124e8c27ec8Sbaban 125e8c27ec8Sbaban #define NLDAP_OR_MIXED_MODE(pidtype, ls) \ 126e8c27ec8Sbaban ((pidtype == IDMAP_UID && NLDAP_OR_MIXED(ls->nm_siduid)) || \ 127e8c27ec8Sbaban (pidtype == IDMAP_GID && NLDAP_OR_MIXED(ls->nm_sidgid))) 128e8c27ec8Sbaban #define AD_OR_MIXED_MODE(pidtype, ls)\ 129e8c27ec8Sbaban ((pidtype == IDMAP_UID && AD_OR_MIXED(ls->nm_siduid)) || \ 130e8c27ec8Sbaban (pidtype == IDMAP_GID && AD_OR_MIXED(ls->nm_sidgid))) 131e8c27ec8Sbaban #define NLDAP_MODE(pidtype, ls) \ 132e8c27ec8Sbaban ((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_NLDAP) || \ 133e8c27ec8Sbaban (pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_NLDAP)) 134e8c27ec8Sbaban #define AD_MODE(pidtype, ls) \ 135e8c27ec8Sbaban ((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_AD) || \ 136e8c27ec8Sbaban (pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_AD)) 137e8c27ec8Sbaban #define MIXED_MODE(pidtype, ls) \ 138e8c27ec8Sbaban ((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_MIXED) || \ 139e8c27ec8Sbaban (pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_MIXED)) 140e8c27ec8Sbaban 141e8c27ec8Sbaban 142c5c4113dSnw141292 typedef struct list_cb_data { 143c5c4113dSnw141292 void *result; 144c5c4113dSnw141292 uint64_t next; 145c5c4113dSnw141292 uint64_t len; 146c5c4113dSnw141292 uint64_t limit; 14748258c6bSjp151216 int flag; 148c5c4113dSnw141292 } list_cb_data_t; 149c5c4113dSnw141292 150c5c4113dSnw141292 typedef struct msg_table { 151c5c4113dSnw141292 idmap_retcode retcode; 152c5c4113dSnw141292 const char *msg; 153c5c4113dSnw141292 } msg_table_t; 154c5c4113dSnw141292 1550dcc7149Snw141292 #define IDMAPD_SEARCH_TIMEOUT 3 /* seconds */ 1560dcc7149Snw141292 #define IDMAPD_LDAP_OPEN_TIMEOUT 1 /* secs; initial, w/ exp backoff */ 1570dcc7149Snw141292 15862c60062Sbaban /* 159e8c27ec8Sbaban * The following flags are used by idmapd while processing a 160e8c27ec8Sbaban * given mapping request. Note that idmapd uses multiple passes to 161e8c27ec8Sbaban * process the request and the flags are used to pass information 162e8c27ec8Sbaban * about the state of the request between these passes. 16362c60062Sbaban */ 164c5c4113dSnw141292 165e8c27ec8Sbaban /* Initial state. Done. Reset all flags. Remaining passes can be skipped */ 166c5c4113dSnw141292 #define _IDMAP_F_DONE 0x00000000 167e8c27ec8Sbaban /* Set when subsequent passes are required */ 168e8c27ec8Sbaban #define _IDMAP_F_NOTDONE 0x00000001 169e8c27ec8Sbaban /* Don't update name_cache. (e.g. set when winname,SID found in name_cache) */ 170e8c27ec8Sbaban #define _IDMAP_F_DONT_UPDATE_NAMECACHE 0x00000002 171e8c27ec8Sbaban /* Batch this request for AD lookup */ 172e8c27ec8Sbaban #define _IDMAP_F_LOOKUP_AD 0x00000004 173e8c27ec8Sbaban /* Batch this request for nldap directory lookup */ 174e8c27ec8Sbaban #define _IDMAP_F_LOOKUP_NLDAP 0x00000008 175e8c27ec8Sbaban /* 176e8c27ec8Sbaban * Expired ephemeral mapping found in cache when processing sid2uid request. 177e8c27ec8Sbaban * Use it if the given SID cannot be mapped by name 178e8c27ec8Sbaban */ 179e8c27ec8Sbaban #define _IDMAP_F_EXP_EPH_UID 0x00000010 180e8c27ec8Sbaban /* Same as above. Used for sid2gid request */ 181e8c27ec8Sbaban #define _IDMAP_F_EXP_EPH_GID 0x00000020 182*4d61c878SJulian Pullen /* This request is not valid for the current forest */ 183*4d61c878SJulian Pullen #define _IDMAP_F_LOOKUP_OTHER_AD 0x00000040 184*4d61c878SJulian Pullen 185e8c27ec8Sbaban 186e8c27ec8Sbaban /* 187e8c27ec8Sbaban * Check if we are done. If so, subsequent passes can be skipped 188e8c27ec8Sbaban * when processing a given mapping request. 189e8c27ec8Sbaban */ 190e8c27ec8Sbaban #define ARE_WE_DONE(f) ((f & _IDMAP_F_NOTDONE) == 0) 191c5c4113dSnw141292 192c5c4113dSnw141292 #define SIZE_INCR 5 193c5c4113dSnw141292 #define MAX_TRIES 5 194c5c4113dSnw141292 #define IDMAP_DBDIR "/var/idmap" 195c5c4113dSnw141292 #define IDMAP_CACHEDIR "/var/run/idmap" 196c5c4113dSnw141292 #define IDMAP_DBNAME IDMAP_DBDIR "/idmap.db" 197c5c4113dSnw141292 #define IDMAP_CACHENAME IDMAP_CACHEDIR "/idmap.db" 1988e228215Sdm199847 199cd37da74Snw141292 #define IS_BATCH_SID(batch, i) \ 200cd37da74Snw141292 (batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID || \ 201cd37da74Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_USID || \ 202cd37da74Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GSID) 203cd37da74Snw141292 204cd37da74Snw141292 #define IS_BATCH_UID(batch, i) \ 2050dcc7149Snw141292 (batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID) 206cd37da74Snw141292 207cd37da74Snw141292 #define IS_BATCH_GID(batch, i) \ 2080dcc7149Snw141292 (batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID) 209cd37da74Snw141292 210cd37da74Snw141292 #define IS_REQUEST_SID(req, n) \ 211cd37da74Snw141292 ((req).id##n.idtype == IDMAP_SID || \ 212cd37da74Snw141292 (req).id##n.idtype == IDMAP_USID || \ 213cd37da74Snw141292 (req).id##n.idtype == IDMAP_GSID) \ 214cd37da74Snw141292 215cd37da74Snw141292 216cd37da74Snw141292 #define IS_REQUEST_UID(request) \ 2170dcc7149Snw141292 ((request).id1.idtype == IDMAP_UID) 218cd37da74Snw141292 219cd37da74Snw141292 #define IS_REQUEST_GID(request) \ 2200dcc7149Snw141292 ((request).id1.idtype == IDMAP_GID) 221cd37da74Snw141292 222c5c4113dSnw141292 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t); 223c5c4113dSnw141292 typedef int (*list_svc_cb)(void *, int, char **, char **); 224c5c4113dSnw141292 225c5c4113dSnw141292 extern void idmap_prog_1(struct svc_req *, register SVCXPRT *); 226c5c4113dSnw141292 extern void idmapdlog(int, const char *, ...); 227c5c4113dSnw141292 extern int init_mapping_system(); 228c5c4113dSnw141292 extern void fini_mapping_system(); 229c5c4113dSnw141292 extern void print_idmapdstate(); 230c5c4113dSnw141292 extern int create_directory(const char *, uid_t, gid_t); 231c5c4113dSnw141292 extern int load_config(); 232349d5d8fSnw141292 extern void reload_ad(); 23384decf41Sjp151216 extern int idmap_init_tsd_key(void); 234349d5d8fSnw141292 extern void degrade_svc(int, const char *); 235c8e26105Sjp151216 extern void restore_svc(void); 236c5c4113dSnw141292 237c5c4113dSnw141292 238c5c4113dSnw141292 extern int init_dbs(); 239c5c4113dSnw141292 extern void fini_dbs(); 240c5c4113dSnw141292 extern idmap_retcode get_db_handle(sqlite **); 241c5c4113dSnw141292 extern idmap_retcode get_cache_handle(sqlite **); 24271590c90Snw141292 extern idmap_retcode sql_exec_no_cb(sqlite *, const char *, char *); 243c5c4113dSnw141292 extern idmap_retcode add_namerule(sqlite *, idmap_namerule *); 244c5c4113dSnw141292 extern idmap_retcode rm_namerule(sqlite *, idmap_namerule *); 245cd37da74Snw141292 extern idmap_retcode flush_namerules(sqlite *); 246c5c4113dSnw141292 247cd37da74Snw141292 extern char *tolower_u8(const char *); 248cd37da74Snw141292 249cd37da74Snw141292 extern idmap_retcode gen_sql_expr_from_rule(idmap_namerule *, char **); 250c5c4113dSnw141292 extern idmap_retcode validate_list_cb_data(list_cb_data_t *, int, 251c5c4113dSnw141292 char **, int, uchar_t **, size_t); 25271590c90Snw141292 extern idmap_retcode process_list_svc_sql(sqlite *, const char *, char *, 25348258c6bSjp151216 uint64_t, int, list_svc_cb, void *); 254479ac375Sdm199847 extern idmap_retcode sid2pid_first_pass(lookup_state_t *, 255c5c4113dSnw141292 idmap_mapping *, idmap_id_res *); 256479ac375Sdm199847 extern idmap_retcode sid2pid_second_pass(lookup_state_t *, 257479ac375Sdm199847 idmap_mapping *, idmap_id_res *); 258479ac375Sdm199847 extern idmap_retcode pid2sid_first_pass(lookup_state_t *, 259e8c27ec8Sbaban idmap_mapping *, idmap_id_res *, int, int); 260479ac375Sdm199847 extern idmap_retcode pid2sid_second_pass(lookup_state_t *, 261479ac375Sdm199847 idmap_mapping *, idmap_id_res *, int); 262479ac375Sdm199847 extern idmap_retcode update_cache_sid2pid(lookup_state_t *, 263c5c4113dSnw141292 idmap_mapping *, idmap_id_res *); 264479ac375Sdm199847 extern idmap_retcode update_cache_pid2sid(lookup_state_t *, 265c5c4113dSnw141292 idmap_mapping *, idmap_id_res *); 266c5c4113dSnw141292 extern idmap_retcode get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *, 267c5c4113dSnw141292 idmap_mapping *, int); 268c5c4113dSnw141292 extern idmap_retcode get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *, 269c5c4113dSnw141292 idmap_mapping *); 270479ac375Sdm199847 extern idmap_retcode load_cfg_in_state(lookup_state_t *); 271e8c27ec8Sbaban extern void cleanup_lookup_state(lookup_state_t *); 272c5c4113dSnw141292 273e8c27ec8Sbaban extern idmap_retcode ad_lookup_batch(lookup_state_t *, 274c5c4113dSnw141292 idmap_mapping_batch *, idmap_ids_res *); 275479ac375Sdm199847 extern idmap_retcode lookup_name2sid(sqlite *, const char *, const char *, 276479ac375Sdm199847 int *, char **, char **, idmap_rid_t *, 277479ac375Sdm199847 idmap_mapping *, int); 278479ac375Sdm199847 extern idmap_retcode lookup_wksids_name2sid(const char *, char **, char **, 279479ac375Sdm199847 idmap_rid_t *, int *); 280c5c4113dSnw141292 281c5c4113dSnw141292 #ifdef __cplusplus 282c5c4113dSnw141292 } 283c5c4113dSnw141292 #endif 284c5c4113dSnw141292 285c5c4113dSnw141292 #endif /* _IDMAPD_H */ 286