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 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #ifndef _NSCD_SWITCH_H 27 #define _NSCD_SWITCH_H 28 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 33 #include <nss_dbdefs.h> 34 #include <thread.h> 35 #include <libscf.h> 36 #define __NSS_PRIVATE_INTERFACE 37 #include "nsswitch_priv.h" 38 #undef __NSS_PRIVATE_INTERFACE 39 #include "nscd_db.h" 40 #include "nscd_config.h" 41 42 /* 43 * max. length of e.g. "passwd: files ldap" 44 */ 45 #define MAX_NSSWITCH_CONFIG_STRING_SZ 256 46 47 /* 48 * max. length of the name of a NSS database 49 */ 50 #define MAX_NSSWITCH_CONFIG_DB_NAME_SZ 256 51 52 /* 53 * nscd_nsw_config_t is an abstraction of the configuration 54 * for a NSS database 55 */ 56 typedef struct { 57 char *db_name; 58 char *nsw_cfg_str; 59 nss_db_params_t fe_params; 60 struct __nsw_switchconfig_v1 *nsw_config; 61 int max_src; 62 int *src_idx; /* ptr to array of */ 63 /* src index */ 64 int nobase; /* not shared */ 65 } nscd_nsw_config_t; 66 67 /* 68 * nscd_be_info_t is an abstraction of a NSS backend 69 */ 70 typedef struct { 71 void *be_version; 72 nss_backend_constr_t be_constr; 73 nss_backend_finder_t *finder; 74 void *finder_priv; 75 } nscd_be_info_t; 76 77 /* 78 * nscd_state_ctrl_t is used to control a nscd_nsw_state pool 79 */ 80 typedef struct { 81 int max; 82 int allocated; 83 int free; 84 int waiter; 85 struct nscd_nsw_state *first; 86 } nscd_state_ctrl_t; 87 88 /* 89 * nscd_nsw_state_base_t represents the nscd_nsw_state pool 90 * for a NSS database 91 */ 92 typedef struct nscd_nsw_state_base { 93 int dbi; /* which database? */ 94 nscd_state_ctrl_t nsw_state; 95 nscd_state_ctrl_t nsw_state_thr; 96 int used_by_thr; 97 thread_t tid; 98 cond_t thr_cond; 99 } nscd_nsw_state_base_t; 100 101 /* 102 * nscd_nsw_state_t is an abstraction of all the data needed 103 * to do lookup of NSS database (e.g. "passwd" or "hosts") 104 */ 105 extern void *_nscd_be_version; /* default version for supported be */ 106 typedef struct nscd_nsw_state { 107 int dbi; /* which database? */ 108 int max_src; /* is == config->num_lookups */ 109 int getent; /* used by getent */ 110 nscd_bool_t recheck_be; /* if set, check/init be */ 111 nss_db_params_t p; 112 struct __nsw_switchconfig_v1 *config; 113 nscd_nsw_config_t **nsw_cfg_p; 114 nscd_nsw_state_base_t *base; 115 nss_backend_t **be; /* array of backends */ 116 nss_backend_constr_t *be_constr; /* be constructor array */ 117 nscd_db_t ***be_db_pp; 118 void **be_version_p; /* version ptr array */ 119 struct nscd_nsw_state *next; 120 } nscd_nsw_state_t; 121 122 /* 123 * nscd_getent_ctx_base_t represents the nscd_getent_ctx_base_t pool 124 * for a NSS database 125 */ 126 typedef struct nscd_getent_ctx_base { 127 int dbi; /* which database? */ 128 int deattached; /* not associated with */ 129 /* current config */ 130 int max_getent_ctx; 131 int num_getent_ctx; 132 int num_waiter; 133 struct nscd_getent_context *first; 134 } nscd_getent_ctx_base_t; 135 136 /* 137 * nscd_getent_context_t is an abstraction of all the data needed 138 * to enumerate a NSS database (e.g. "passwd" or "hosts") 139 */ 140 typedef struct nscd_getent_context { 141 int dbi; 142 thread_t thr_id; 143 mutex_t getent_mutex; 144 int aborted; 145 int in_use; 146 int num_reclaim_check; 147 nscd_seq_num_t seq_num; 148 nscd_cookie_num_t cookie_num; 149 pid_t pid; /* door client's pid */ 150 int n_src; /* >=max_src: end of sequence */ 151 nscd_nsw_state_t *nsw_state; 152 nss_backend_t *be; 153 nscd_getent_ctx_base_t *base; 154 struct nscd_getent_context *next; 155 struct nscd_getent_context *next_to_reclaim; 156 } nscd_getent_context_t; 157 158 /* 159 * nscd_smf_state_t is used to keep track of the state of the smf 160 * service associated with a NSS source (e.g. "passwd" or "hosts") 161 */ 162 typedef struct { 163 char *src_name; 164 int src_state; 165 } nscd_smf_state_t; 166 167 /* 168 * nscd_smf_state_t is used to keep track of the state of the smf 169 * service associated with a NSS source (e.g. "passwd" or "hosts") 170 */ 171 typedef struct { 172 int dbi; /* database index */ 173 /* 174 * index of the database of which the switch policy 175 * should be used 176 */ 177 int cfgdbi; 178 /* 179 * index of the pseudo database that the NSS backend 180 * does search on 181 */ 182 int compati; 183 /* 184 * ptr to ptr to the siwtch config structure 185 */ 186 nscd_nsw_config_t **nswcfg; 187 /* 188 * frontend params passed to nss_search or nss_*ent 189 */ 190 struct nss_db_params p; 191 /* 192 * set to 1 if database is "hosts", else 2 if "ipnodes" 193 */ 194 int8_t dnsi; 195 /* 196 * set to 1 if require privilege to look up the database 197 */ 198 uint8_t privdb; 199 } nscd_nsw_params_t; 200 201 /* 202 * additional info returned by the switch engine 203 */ 204 typedef struct { 205 void *pbuf; /* ptr to packed buffer */ 206 size_t pbufsiz; /* length of the packed buffer */ 207 int srci; /* last source searched */ 208 int errnum; /* errno from the backend */ 209 int noarg; /* if set, backend does not use the arg structure */ 210 int fallback; /* if set, may need to fall back to main nscd */ 211 int datalen; /* pbuf->data_len (backend may change it) */ 212 } nscd_sw_return_t; 213 214 /* 215 * nscd cookies used for setent/getent/endent 216 * - p0 cookie: returned by nscd to indicate 217 * the start of the enumeration position 218 * - p1 cookie: returned/updated by nscd to indicate 219 * the current enumeration position 220 */ 221 #define NSCD_P0_COOKIE_SEQNUM -1 222 typedef struct { 223 pid_t p0_pid; 224 time_t p0_time; 225 nscd_seq_num_t p0_seqnum; 226 } nscd_getent_p0_cookie_t; 227 228 typedef struct { 229 nscd_cookie_num_t p1_cookie_num; 230 nscd_seq_num_t p1_seqnum; 231 } nscd_getent_p1_cookie_t; 232 233 /* 234 * static tables or global data defined in other files 235 */ 236 extern int _nscd_cfg_num_nsw_src; 237 extern int _nscd_cfg_num_nsw_src_all; 238 extern int _nscd_cfg_num_nsw_db; 239 extern int _nscd_cfg_num_nsw_db_all; 240 extern int _nscd_cfg_num_smf_services; 241 extern nscd_cfg_id_t _nscd_cfg_nsw_src[]; 242 extern nscd_cfg_id_t *_nscd_cfg_nsw_src_all; 243 extern nscd_cfg_id_t _nscd_cfg_nsw_db[]; 244 extern nss_db_initf_t nscd_nss_db_initf[]; 245 extern nscd_cfg_id_t _nscd_cfg_smf_services[]; 246 extern nscd_smf_state_t *nscd_smf_service_state; 247 extern nscd_db_t ***nscd_src_backend_db; 248 extern nscd_nsw_config_t ***nscd_nsw_config; 249 extern nscd_nsw_state_base_t **nscd_nsw_state_base; 250 extern nscd_getent_ctx_base_t **nscd_getent_ctx_base; 251 extern nscd_cfg_global_switch_t nscd_switch_cfg_g; 252 extern nscd_cfg_switch_t *nscd_switch_cfg; 253 extern nscd_cfg_stat_global_switch_t nscd_switch_stats_g; 254 extern nscd_cfg_stat_switch_t *nscd_switch_stats; 255 256 #define NSCD_NUM_SRC _nscd_cfg_num_nsw_src_all 257 #define NSCD_NUM_DB _nscd_cfg_num_nsw_db_all 258 #define NSCD_NUM_SMF_FMRI _nscd_cfg_num_smf_services 259 #define NSCD_NSW_SRC_NAME(i) (_nscd_cfg_nsw_src_all + i)->name 260 #define NSCD_NSW_DB_NAME(i) _nscd_cfg_nsw_db[i].name 261 #define NSCD_SMF_SVC_FMRI(i) _nscd_cfg_smf_services[i].name 262 #define NSCD_SMF_SVC_INDEX(i) _nscd_cfg_smf_services[i].index 263 #define NSCD_SMF_SVC_STATE(i) nscd_smf_service_state[i].src_state 264 #define NSCD_SW_CFG_G nscd_switch_cfg_g 265 #define NSCD_SW_CFG(i) nscd_switch_cfg[i] 266 #define NSCD_SW_STATS_G nscd_switch_stats_g 267 #define NSCD_SW_STATS(i) nscd_switch_stats[i] 268 269 /* 270 * special service states used by the switch engine 271 */ 272 #define NSCD_SVC_STATE_UNINITED -1 273 #define NSCD_SVC_STATE_FOREIGN_SRC -2 274 #define NSCD_SVC_STATE_UNSUPPORTED_SRC -3 275 276 /* 277 * prototypes 278 */ 279 280 void 281 _nscd_put_nsw_state( 282 nscd_nsw_state_t *s); 283 284 void 285 _nscd_put_nsw_state_thread( 286 nscd_nsw_state_t *s); 287 288 nscd_rc_t 289 _nscd_get_nsw_state( 290 nss_db_root_t *rootp, 291 nscd_nsw_params_t *params); 292 293 nscd_rc_t 294 _nscd_get_nsw_state_thread( 295 nss_db_root_t *rootp, 296 nscd_nsw_params_t *params); 297 298 nscd_rc_t 299 _nscd_init_all_nsw_state_base(); 300 301 nscd_rc_t 302 _nscd_init_nsw_state_base( 303 int dbi, 304 int compat_basei, 305 int lock); 306 307 nscd_rc_t 308 _nscd_init_all_getent_ctx(); 309 310 nscd_rc_t 311 _nscd_init_getent_ctx_base( 312 int dbi, 313 int lock); 314 315 nscd_db_t 316 *_nscd_create_getent_ctxaddrDB(); 317 318 nscd_rc_t 319 _nscd_get_getent_ctx( 320 nss_getent_t *contextpp, 321 nscd_nsw_params_t *params); 322 323 void 324 _nscd_put_getent_ctx( 325 nscd_getent_context_t *ctx); 326 void 327 _nscd_free_ctx_if_aborted( 328 nscd_getent_context_t *ctx); 329 330 int 331 _nscd_is_getent_ctx_in_use( 332 nscd_getent_context_t *ctx); 333 334 nscd_rc_t 335 _nscd_init_all_nsw_config(); 336 337 nscd_rc_t 338 _nscd_init_all_nsw_be_info_db(); 339 340 #ifdef NSCD_NSSWITCH_CONF_FROM_SMF_PROP 341 nscd_rc_t 342 _nscd_get_new_nsw_config( 343 scf_handle_t *hndl, 344 const char *fmri, 345 scf_propertygroup_t *pg); 346 #endif 347 348 nscd_rc_t 349 _nscd_get_new_service_state( 350 int index, 351 scf_handle_t *hndl, 352 scf_property_t *prop); 353 354 nscd_getent_context_t * 355 _nscd_is_getent_ctx( 356 nscd_cookie_num_t cookie_num); 357 358 nscd_rc_t 359 _nscd_create_sw_struct( 360 int dbi, 361 int compat_basei, 362 const char *dbn, 363 const char *cfgstr, 364 void *swcfgv1, 365 nscd_nsw_params_t *params); 366 367 nscd_rc_t 368 _nscd_create_new_config( 369 nscd_nsw_params_t *params); 370 371 void 372 _nscd_free_nsw_config( 373 nscd_nsw_config_t *nswcfg); 374 375 nscd_rc_t 376 _nscd_init_smf_monitor(); 377 378 nscd_rc_t 379 _nscd_alloc_nsw_config(); 380 381 nscd_rc_t 382 _nscd_alloc_service_state_table(); 383 384 nscd_rc_t 385 _nscd_alloc_nsw_state_base(); 386 387 nscd_rc_t 388 _nscd_alloc_nsw_be_info_db(); 389 390 nscd_rc_t 391 _nscd_alloc_getent_ctx_base(); 392 393 void 394 _nscd_free_all_nsw_state_base(); 395 396 void 397 _nscd_free_all_getent_ctx_base(); 398 399 void 400 _nscd_free_all_nsw_config(); 401 402 void 403 _nscd_free_all_nsw_backend_info_db(); 404 405 struct __nsw_switchconfig_v1 * 406 _nsw_getoneconfig_v1( 407 const char *name, 408 char *linep, 409 enum __nsw_parse_err *errp); 410 int 411 __nsw_freeconfig_v1( 412 struct __nsw_switchconfig_v1 *conf); 413 414 int 415 _nscd_get_smf_state(int srci, int dbi, int recheck); 416 417 void 418 nss_psearch(void *buffer, size_t length); 419 void 420 nss_psetent(void *buffer, size_t length, pid_t pid); 421 void 422 nss_pgetent(void *buffer, size_t length); 423 void 424 nss_pendent(void *buffer, size_t length); 425 void 426 nss_pdelete(void *buffer, size_t length); 427 428 nscd_rc_t _nscd_alloc_switch_cfg(); 429 nscd_rc_t _nscd_alloc_switch_stats(); 430 nscd_db_t *_nscd_create_getent_ctx_addrDB(); 431 nscd_db_t *_nscd_create_getent_ctxDB(); 432 433 #ifdef __cplusplus 434 } 435 #endif 436 437 #endif /* _NSCD_SWITCH_H */ 438