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 /* 22c5c4113dSnw141292 * Copyright 2007 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 #pragma ident "%Z%%M% %I% %E% SMI" 30c5c4113dSnw141292 31c5c4113dSnw141292 #include <stdio.h> 32c5c4113dSnw141292 #include <stdlib.h> 33c5c4113dSnw141292 #include <syslog.h> 34c5c4113dSnw141292 #include <stdarg.h> 35c5c4113dSnw141292 #include <rpc/rpc.h> 36c5c4113dSnw141292 #include <synch.h> 37c5c4113dSnw141292 #include <thread.h> 38c5c4113dSnw141292 #include <libintl.h> 39c5c4113dSnw141292 #include <strings.h> 40c5c4113dSnw141292 #include <sqlite/sqlite.h> 41c5c4113dSnw141292 #include <inttypes.h> 42c5c4113dSnw141292 #include "idmap_prot.h" 43c5c4113dSnw141292 #include "adutils.h" 44c5c4113dSnw141292 #include "idmap_config.h" 45c5c4113dSnw141292 46c5c4113dSnw141292 #ifdef __cplusplus 47c5c4113dSnw141292 extern "C" { 48c5c4113dSnw141292 #endif 49c5c4113dSnw141292 50c5c4113dSnw141292 /* States a server can be in wrt request */ 51c5c4113dSnw141292 #define _IDLE 0 52c5c4113dSnw141292 #define _SERVED 1 53c5c4113dSnw141292 54c5c4113dSnw141292 #define CHECK_NULL(s) s?s:"null" 55c5c4113dSnw141292 56c5c4113dSnw141292 #define SENTINEL_PID UINT32_MAX 57c5c4113dSnw141292 58c5c4113dSnw141292 extern int _rpcsvcstate; /* set when a request is serviced */ 59c5c4113dSnw141292 extern int _rpcsvccount; /* number of requests being serviced */ 60c5c4113dSnw141292 extern mutex_t _svcstate_lock; /* lock for _rpcsvcstate, _rpcsvccount */ 61c5c4113dSnw141292 62c5c4113dSnw141292 /* 63c5c4113dSnw141292 * Global state of idmapd daemon. 64c5c4113dSnw141292 */ 65c5c4113dSnw141292 #define IDMAP_MAX_NAME_LEN 512 66c5c4113dSnw141292 typedef struct idmapd_state { 67c5c4113dSnw141292 rwlock_t rwlk_cfg; /* config lock */ 68c5c4113dSnw141292 idmap_cfg_t *cfg; /* config */ 69c5c4113dSnw141292 bool_t daemon_mode; /* daemon mode? yes/no */ 70c5c4113dSnw141292 char hostname[MAX_NAME_LEN]; /* my hostname */ 71c5c4113dSnw141292 uid_t next_uid; 72c5c4113dSnw141292 gid_t next_gid; 73c5c4113dSnw141292 uid_t limit_uid; 74c5c4113dSnw141292 gid_t limit_gid; 75c5c4113dSnw141292 int new_eph_db; /* was the ephem ID db [re-]created? */ 76c5c4113dSnw141292 ad_t *ad; 77c5c4113dSnw141292 } idmapd_state_t; 78c5c4113dSnw141292 extern idmapd_state_t _idmapdstate; 79c5c4113dSnw141292 80c5c4113dSnw141292 #define RDLOCK_CONFIG() \ 81c5c4113dSnw141292 (void) rw_rdlock(&_idmapdstate.rwlk_cfg); 82c5c4113dSnw141292 #define WRLOCK_CONFIG() \ 83c5c4113dSnw141292 (void) rw_wrlock(&_idmapdstate.rwlk_cfg); 84c5c4113dSnw141292 #define UNLOCK_CONFIG() \ 85c5c4113dSnw141292 (void) rw_unlock(&_idmapdstate.rwlk_cfg); 86c5c4113dSnw141292 8762c60062Sbaban typedef struct hashentry { 8862c60062Sbaban uint_t key; 8962c60062Sbaban uint_t next; 9062c60062Sbaban } hashentry_t; 9162c60062Sbaban 92c5c4113dSnw141292 typedef struct lookup_state { 93c5c4113dSnw141292 bool_t sid2pid_done; 94c5c4113dSnw141292 bool_t pid2sid_done; 95c5c4113dSnw141292 idmap_query_state_t *ad_lookup; 96c5c4113dSnw141292 int ad_nqueries; 9762c60062Sbaban uint_t curpos; 9862c60062Sbaban hashentry_t *sid_history; 9962c60062Sbaban uint_t sid_history_size; 10062c60062Sbaban idmap_mapping_batch *batch; 10162c60062Sbaban idmap_ids_res *result; 102c5c4113dSnw141292 } lookup_state_t; 103c5c4113dSnw141292 104c5c4113dSnw141292 typedef struct list_cb_data { 105c5c4113dSnw141292 void *result; 106c5c4113dSnw141292 uint64_t next; 107c5c4113dSnw141292 uint64_t len; 108c5c4113dSnw141292 uint64_t limit; 109c5c4113dSnw141292 } list_cb_data_t; 110c5c4113dSnw141292 111c5c4113dSnw141292 typedef struct msg_table { 112c5c4113dSnw141292 idmap_retcode retcode; 113c5c4113dSnw141292 const char *msg; 114c5c4113dSnw141292 } msg_table_t; 115c5c4113dSnw141292 11662c60062Sbaban /* 11762c60062Sbaban * Data structure to store well-known SIDs and 11862c60062Sbaban * associated mappings (if any) 11962c60062Sbaban */ 120c5c4113dSnw141292 typedef struct wksids_table { 121c5c4113dSnw141292 const char *sidprefix; 122c5c4113dSnw141292 uint32_t rid; 12362c60062Sbaban const char *winname; 124c5c4113dSnw141292 int is_user; 125c5c4113dSnw141292 uid_t pid; 126c5c4113dSnw141292 int direction; 127c5c4113dSnw141292 } wksids_table_t; 128c5c4113dSnw141292 129c5c4113dSnw141292 130c5c4113dSnw141292 #define _IDMAP_F_DONE 0x00000000 131c5c4113dSnw141292 #define _IDMAP_F_S2N_CACHE 0x00000001 132c5c4113dSnw141292 #define _IDMAP_F_S2N_AD 0x00000002 133c5c4113dSnw141292 #define _IDMAP_F_EXP_EPH_UID 0x00000004 134c5c4113dSnw141292 #define _IDMAP_F_EXP_EPH_GID 0x00000010 135c5c4113dSnw141292 136c5c4113dSnw141292 #define SIZE_INCR 5 137c5c4113dSnw141292 #define MAX_TRIES 5 138c5c4113dSnw141292 #define IDMAP_DBDIR "/var/idmap" 139c5c4113dSnw141292 #define IDMAP_CACHEDIR "/var/run/idmap" 140c5c4113dSnw141292 #define IDMAP_DBNAME IDMAP_DBDIR "/idmap.db" 141c5c4113dSnw141292 #define IDMAP_CACHENAME IDMAP_CACHEDIR "/idmap.db" 1428e228215Sdm199847 #define IDMAP_CACHENAME IDMAP_CACHEDIR "/idmap.db" 1438e228215Sdm199847 1448e228215Sdm199847 #define EMPTY_STRING(str) (str == NULL || *str == 0) 145c5c4113dSnw141292 146*cd37da74Snw141292 #define IS_BATCH_SID(batch, i) \ 147*cd37da74Snw141292 (batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID || \ 148*cd37da74Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_USID || \ 149*cd37da74Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GSID) 150*cd37da74Snw141292 151*cd37da74Snw141292 #define IS_BATCH_UID(batch, i) \ 152*cd37da74Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID 153*cd37da74Snw141292 154*cd37da74Snw141292 #define IS_BATCH_GID(batch, i) \ 155*cd37da74Snw141292 batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID 156*cd37da74Snw141292 157*cd37da74Snw141292 #define IS_REQUEST_SID(req, n) \ 158*cd37da74Snw141292 ((req).id##n.idtype == IDMAP_SID || \ 159*cd37da74Snw141292 (req).id##n.idtype == IDMAP_USID || \ 160*cd37da74Snw141292 (req).id##n.idtype == IDMAP_GSID) \ 161*cd37da74Snw141292 162*cd37da74Snw141292 163*cd37da74Snw141292 #define IS_REQUEST_UID(request) \ 164*cd37da74Snw141292 request.id1.idtype == IDMAP_UID 165*cd37da74Snw141292 166*cd37da74Snw141292 #define IS_REQUEST_GID(request) \ 167*cd37da74Snw141292 request.id1.idtype == IDMAP_GID 168*cd37da74Snw141292 169c5c4113dSnw141292 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t); 170c5c4113dSnw141292 typedef int (*list_svc_cb)(void *, int, char **, char **); 171c5c4113dSnw141292 172c5c4113dSnw141292 extern void idmap_prog_1(struct svc_req *, register SVCXPRT *); 173c5c4113dSnw141292 extern void idmapdlog(int, const char *, ...); 174c5c4113dSnw141292 extern int init_mapping_system(); 175c5c4113dSnw141292 extern void fini_mapping_system(); 176c5c4113dSnw141292 extern void print_idmapdstate(); 177c5c4113dSnw141292 extern int create_directory(const char *, uid_t, gid_t); 178c5c4113dSnw141292 extern int load_config(); 179c8e26105Sjp151216 extern int reload_ad(); 18084decf41Sjp151216 extern int idmap_init_tsd_key(void); 181c8e26105Sjp151216 extern void degrade_svc(void); 182c8e26105Sjp151216 extern void restore_svc(void); 183c5c4113dSnw141292 184c5c4113dSnw141292 185c5c4113dSnw141292 extern int init_dbs(); 186c5c4113dSnw141292 extern void fini_dbs(); 187c5c4113dSnw141292 extern idmap_retcode get_db_handle(sqlite **); 188c5c4113dSnw141292 extern idmap_retcode get_cache_handle(sqlite **); 189c5c4113dSnw141292 extern idmap_retcode sql_exec_no_cb(sqlite *, char *); 190c5c4113dSnw141292 extern idmap_retcode add_namerule(sqlite *, idmap_namerule *); 191c5c4113dSnw141292 extern idmap_retcode rm_namerule(sqlite *, idmap_namerule *); 192*cd37da74Snw141292 extern idmap_retcode flush_namerules(sqlite *); 193c5c4113dSnw141292 194*cd37da74Snw141292 extern char *tolower_u8(const char *); 195*cd37da74Snw141292 196*cd37da74Snw141292 extern idmap_retcode gen_sql_expr_from_rule(idmap_namerule *, char **); 197c5c4113dSnw141292 extern idmap_retcode validate_list_cb_data(list_cb_data_t *, int, 198c5c4113dSnw141292 char **, int, uchar_t **, size_t); 199c5c4113dSnw141292 extern idmap_retcode process_list_svc_sql(sqlite *, char *, uint64_t, 200c5c4113dSnw141292 list_svc_cb, void *); 201c5c4113dSnw141292 extern idmap_retcode sid2pid_first_pass(lookup_state_t *, sqlite *, 202c5c4113dSnw141292 idmap_mapping *, idmap_id_res *); 203c5c4113dSnw141292 extern idmap_retcode sid2pid_second_pass(lookup_state_t *, sqlite *, 204c5c4113dSnw141292 sqlite *, idmap_mapping *, idmap_id_res *); 205c5c4113dSnw141292 extern idmap_retcode pid2sid_first_pass(lookup_state_t *, sqlite *, 206c5c4113dSnw141292 sqlite *, idmap_mapping *, idmap_id_res *, 207c5c4113dSnw141292 int, int); 208c5c4113dSnw141292 extern idmap_retcode update_cache_sid2pid(lookup_state_t *, sqlite *, 209c5c4113dSnw141292 idmap_mapping *, idmap_id_res *); 210c5c4113dSnw141292 extern idmap_retcode update_cache_pid2sid(lookup_state_t *, sqlite *, 211c5c4113dSnw141292 idmap_mapping *, idmap_id_res *); 212c5c4113dSnw141292 extern idmap_retcode get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *, 213c5c4113dSnw141292 idmap_mapping *, int); 214c5c4113dSnw141292 extern idmap_retcode get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *, 215c5c4113dSnw141292 idmap_mapping *); 216c5c4113dSnw141292 217c5c4113dSnw141292 extern idmap_retcode lookup_win_batch_sid2name(lookup_state_t *, 218c5c4113dSnw141292 idmap_mapping_batch *, idmap_ids_res *); 219c5c4113dSnw141292 220c5c4113dSnw141292 221c5c4113dSnw141292 #ifdef __cplusplus 222c5c4113dSnw141292 } 223c5c4113dSnw141292 #endif 224c5c4113dSnw141292 225c5c4113dSnw141292 #endif /* _IDMAPD_H */ 226