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