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