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 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #ifndef _IDMAPD_H 27 #define _IDMAPD_H 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <stdarg.h> 32 #include <rpc/rpc.h> 33 #include <synch.h> 34 #include <thread.h> 35 #include <libintl.h> 36 #include <strings.h> 37 #include <sqlite/sqlite.h> 38 #include <syslog.h> 39 #include <inttypes.h> 40 #include "idmap_prot.h" 41 #include "adutils.h" 42 #include "idmap_priv.h" 43 #include "idmap_config.h" 44 #include "libadutils.h" 45 46 #ifdef __cplusplus 47 extern "C" { 48 #endif 49 50 /* States a server can be in wrt request */ 51 #define _IDLE 0 52 #define _SERVED 1 53 54 #define SENTINEL_PID UINT32_MAX 55 #define CHECK_NULL(s) (s != NULL ? s : "null") 56 57 extern int _rpcsvcstate; /* set when a request is serviced */ 58 extern int _rpcsvccount; /* number of requests being serviced */ 59 extern mutex_t _svcstate_lock; /* lock for _rpcsvcstate, _rpcsvccount */ 60 61 typedef enum idmap_namemap_mode { 62 IDMAP_NM_NONE = 0, 63 IDMAP_NM_AD, 64 IDMAP_NM_NLDAP, 65 IDMAP_NM_MIXED 66 } idmap_namemap_mode_t; 67 68 /* 69 * Global state of idmapd daemon. 70 */ 71 #define IDMAP_MAX_NAME_LEN 512 72 typedef struct idmapd_state { 73 rwlock_t rwlk_cfg; /* config lock */ 74 idmap_cfg_t *cfg; /* config */ 75 bool_t daemon_mode; 76 bool_t debug_mode; 77 char hostname[MAX_NAME_LEN]; /* my hostname */ 78 uid_t next_uid; 79 gid_t next_gid; 80 uid_t limit_uid; 81 gid_t limit_gid; 82 int new_eph_db; /* was the ephem ID db [re-]created? */ 83 bool_t eph_map_unres_sids; 84 int num_ads; 85 adutils_ad_t **ads; 86 } idmapd_state_t; 87 extern idmapd_state_t _idmapdstate; 88 89 #define RDLOCK_CONFIG() \ 90 (void) rw_rdlock(&_idmapdstate.rwlk_cfg); 91 #define WRLOCK_CONFIG() \ 92 (void) rw_wrlock(&_idmapdstate.rwlk_cfg); 93 #define UNLOCK_CONFIG() \ 94 (void) rw_unlock(&_idmapdstate.rwlk_cfg); 95 96 typedef struct hashentry { 97 uint_t key; 98 uint_t next; 99 } hashentry_t; 100 101 typedef struct lookup_state { 102 bool_t sid2pid_done; 103 bool_t pid2sid_done; 104 int ad_nqueries; 105 int nldap_nqueries; 106 bool_t eph_map_unres_sids; 107 uint_t curpos; 108 hashentry_t *sid_history; 109 uint_t sid_history_size; 110 idmap_mapping_batch *batch; 111 idmap_ids_res *result; 112 idmap_namemap_mode_t nm_siduid; 113 idmap_namemap_mode_t nm_sidgid; 114 char *ad_unixuser_attr; 115 char *ad_unixgroup_attr; 116 char *nldap_winname_attr; 117 char *defdom; 118 sqlite *cache; 119 sqlite *db; 120 } lookup_state_t; 121 122 #define NLDAP_OR_MIXED(nm) \ 123 (nm == IDMAP_NM_NLDAP || nm == IDMAP_NM_MIXED) 124 #define AD_OR_MIXED(nm) \ 125 (nm == IDMAP_NM_AD || nm == IDMAP_NM_MIXED) 126 127 #define NLDAP_OR_MIXED_MODE(pidtype, ls) \ 128 ((pidtype == IDMAP_UID && NLDAP_OR_MIXED(ls->nm_siduid)) || \ 129 (pidtype == IDMAP_GID && NLDAP_OR_MIXED(ls->nm_sidgid))) 130 #define AD_OR_MIXED_MODE(pidtype, ls)\ 131 ((pidtype == IDMAP_UID && AD_OR_MIXED(ls->nm_siduid)) || \ 132 (pidtype == IDMAP_GID && AD_OR_MIXED(ls->nm_sidgid))) 133 #define NLDAP_MODE(pidtype, ls) \ 134 ((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_NLDAP) || \ 135 (pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_NLDAP)) 136 #define AD_MODE(pidtype, ls) \ 137 ((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_AD) || \ 138 (pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_AD)) 139 #define MIXED_MODE(pidtype, ls) \ 140 ((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_MIXED) || \ 141 (pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_MIXED)) 142 143 144 typedef struct list_cb_data { 145 void *result; 146 uint64_t next; 147 uint64_t len; 148 uint64_t limit; 149 int flag; 150 } list_cb_data_t; 151 152 typedef struct msg_table { 153 idmap_retcode retcode; 154 const char *msg; 155 } msg_table_t; 156 157 #define IDMAPD_SEARCH_TIMEOUT 3 /* seconds */ 158 #define IDMAPD_LDAP_OPEN_TIMEOUT 1 /* secs; initial, w/ exp backoff */ 159 160 /* 161 * The following flags are used by idmapd while processing a 162 * given mapping request. Note that idmapd uses multiple passes to 163 * process the request and the flags are used to pass information 164 * about the state of the request between these passes. 165 */ 166 167 /* Initial state. Done. Reset all flags. Remaining passes can be skipped */ 168 #define _IDMAP_F_DONE 0x00000000 169 /* Set when subsequent passes are required */ 170 #define _IDMAP_F_NOTDONE 0x00000001 171 /* Don't update name_cache. (e.g. set when winname,SID found in name_cache) */ 172 #define _IDMAP_F_DONT_UPDATE_NAMECACHE 0x00000002 173 /* Batch this request for AD lookup */ 174 #define _IDMAP_F_LOOKUP_AD 0x00000004 175 /* Batch this request for nldap directory lookup */ 176 #define _IDMAP_F_LOOKUP_NLDAP 0x00000008 177 /* 178 * Expired ephemeral mapping found in cache when processing sid2uid request. 179 * Use it if the given SID cannot be mapped by name 180 */ 181 #define _IDMAP_F_EXP_EPH_UID 0x00000010 182 /* Same as above. Used for sid2gid request */ 183 #define _IDMAP_F_EXP_EPH_GID 0x00000020 184 /* This request is not valid for the current forest */ 185 #define _IDMAP_F_LOOKUP_OTHER_AD 0x00000040 186 187 188 /* 189 * Check if we are done. If so, subsequent passes can be skipped 190 * when processing a given mapping request. 191 */ 192 #define ARE_WE_DONE(f) ((f & _IDMAP_F_NOTDONE) == 0) 193 194 #define SIZE_INCR 5 195 #define MAX_TRIES 5 196 #define IDMAP_DBDIR "/var/idmap" 197 #define IDMAP_CACHEDIR "/var/run/idmap" 198 #define IDMAP_DBNAME IDMAP_DBDIR "/idmap.db" 199 #define IDMAP_CACHENAME IDMAP_CACHEDIR "/idmap.db" 200 201 #define IS_BATCH_SID(batch, i) \ 202 (batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID || \ 203 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_USID || \ 204 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GSID) 205 206 #define IS_BATCH_UID(batch, i) \ 207 (batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID) 208 209 #define IS_BATCH_GID(batch, i) \ 210 (batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID) 211 212 #define IS_REQUEST_SID(req, n) \ 213 ((req).id##n.idtype == IDMAP_SID || \ 214 (req).id##n.idtype == IDMAP_USID || \ 215 (req).id##n.idtype == IDMAP_GSID) \ 216 217 218 #define IS_REQUEST_UID(request) \ 219 ((request).id1.idtype == IDMAP_UID) 220 221 #define IS_REQUEST_GID(request) \ 222 ((request).id1.idtype == IDMAP_GID) 223 224 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t); 225 typedef int (*list_svc_cb)(void *, int, char **, char **); 226 227 extern void idmap_prog_1(struct svc_req *, register SVCXPRT *); 228 extern void idmapdlog(int, const char *, ...); 229 extern int init_mapping_system(); 230 extern void fini_mapping_system(); 231 extern void print_idmapdstate(); 232 extern int create_directory(const char *, uid_t, gid_t); 233 extern int load_config(); 234 extern void reload_ad(); 235 extern int idmap_init_tsd_key(void); 236 extern void degrade_svc(int, const char *); 237 extern void restore_svc(void); 238 239 240 extern int init_dbs(); 241 extern void fini_dbs(); 242 extern idmap_retcode get_db_handle(sqlite **); 243 extern idmap_retcode get_cache_handle(sqlite **); 244 extern idmap_retcode sql_exec_no_cb(sqlite *, const char *, char *); 245 extern idmap_retcode add_namerule(sqlite *, idmap_namerule *); 246 extern idmap_retcode rm_namerule(sqlite *, idmap_namerule *); 247 extern idmap_retcode flush_namerules(sqlite *); 248 249 extern char *tolower_u8(const char *); 250 251 extern idmap_retcode gen_sql_expr_from_rule(idmap_namerule *, char **); 252 extern idmap_retcode validate_list_cb_data(list_cb_data_t *, int, 253 char **, int, uchar_t **, size_t); 254 extern idmap_retcode process_list_svc_sql(sqlite *, const char *, char *, 255 uint64_t, int, list_svc_cb, void *); 256 extern idmap_retcode sid2pid_first_pass(lookup_state_t *, 257 idmap_mapping *, idmap_id_res *); 258 extern idmap_retcode sid2pid_second_pass(lookup_state_t *, 259 idmap_mapping *, idmap_id_res *); 260 extern idmap_retcode pid2sid_first_pass(lookup_state_t *, 261 idmap_mapping *, idmap_id_res *, int, int); 262 extern idmap_retcode pid2sid_second_pass(lookup_state_t *, 263 idmap_mapping *, idmap_id_res *, int); 264 extern idmap_retcode update_cache_sid2pid(lookup_state_t *, 265 idmap_mapping *, idmap_id_res *); 266 extern idmap_retcode update_cache_pid2sid(lookup_state_t *, 267 idmap_mapping *, idmap_id_res *); 268 extern idmap_retcode get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *, 269 idmap_mapping *, int); 270 extern idmap_retcode get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *, 271 idmap_mapping *); 272 extern idmap_retcode load_cfg_in_state(lookup_state_t *); 273 extern void cleanup_lookup_state(lookup_state_t *); 274 275 extern idmap_retcode ad_lookup_batch(lookup_state_t *, 276 idmap_mapping_batch *, idmap_ids_res *); 277 extern idmap_retcode lookup_name2sid(sqlite *, const char *, const char *, 278 int *, char **, char **, idmap_rid_t *, 279 idmap_mapping *, int); 280 extern idmap_retcode lookup_wksids_name2sid(const char *, char **, char **, 281 idmap_rid_t *, int *); 282 283 284 extern void idmap_log_stderr(int); 285 extern void idmap_log_syslog(boolean_t); 286 extern void idmap_log_degraded(boolean_t); 287 288 #ifdef __cplusplus 289 } 290 #endif 291 292 #endif /* _IDMAPD_H */ 293