summaryrefslogtreecommitdiff
path: root/usr/src/cmd/nscd/nscd_switch.h
blob: 0b3ded9e7008ac1dc9af3f88a1028dd2f7645b25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_NSCD_SWITCH_H
#define	_NSCD_SWITCH_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <nss_dbdefs.h>
#include <thread.h>
#include <libscf.h>
#define	__NSS_PRIVATE_INTERFACE
#include "nsswitch_priv.h"
#undef	__NSS_PRIVATE_INTERFACE
#include "nscd_db.h"
#include "nscd_config.h"

/*
 * max. length of e.g. "passwd: files ldap"
 */
#define	MAX_NSSWITCH_CONFIG_STRING_SZ 256

/*
 * max. length of the name of a NSS database
 */
#define	MAX_NSSWITCH_CONFIG_DB_NAME_SZ 256

/*
 * nscd_nsw_config_t is an abstraction of the configuration
 * for a NSS database
 */
typedef struct {
	char				*db_name;
	char				*nsw_cfg_str;
	nss_db_params_t			fe_params;
	struct __nsw_switchconfig_v1	*nsw_config;
	int				max_src;
	int				*src_idx;	/* ptr to array of */
							/* src index */
	int				nobase;		/* not shared */
} nscd_nsw_config_t;

/*
 * nscd_be_info_t is an abstraction of a NSS backend
 */
typedef struct {
	void			*be_version;
	nss_backend_constr_t	be_constr;
	nss_backend_finder_t	*finder;
	void			*finder_priv;
} nscd_be_info_t;

/*
 * nscd_state_ctrl_t is used to control a nscd_nsw_state pool
 */
typedef struct {
	int			max;
	int			allocated;
	int			free;
	int			waiter;
	struct nscd_nsw_state	*first;
} nscd_state_ctrl_t;

/*
 * nscd_nsw_state_base_t represents the nscd_nsw_state pool
 * for a NSS database
 */
typedef struct nscd_nsw_state_base {
	int			dbi;	/* which database? */
	nscd_state_ctrl_t	nsw_state;
	nscd_state_ctrl_t	nsw_state_thr;
	int			used_by_thr;
	thread_t		tid;
	cond_t			thr_cond;
} nscd_nsw_state_base_t;

/*
 * nscd_nsw_state_t is an abstraction of all the data needed
 * to do lookup of NSS database (e.g. "passwd" or "hosts")
 */
extern	void *_nscd_be_version;		/* default version for supported be */
typedef struct nscd_nsw_state {
	int				dbi;	/* which database? */
	int				max_src; /* is == config->num_lookups */
	int				getent; /* used by getent */
	nscd_bool_t			recheck_be; /* if set, check/init be */
	nss_db_params_t			p;
	struct __nsw_switchconfig_v1	*config;
	nscd_nsw_config_t		**nsw_cfg_p;
	nscd_nsw_state_base_t		*base;
	nss_backend_t			**be; /* array of backends */
	nss_backend_constr_t		*be_constr; /* be constructor array */
	nscd_db_t			***be_db_pp;
	void				**be_version_p; /* version ptr array */
	struct nscd_nsw_state		*next;
} nscd_nsw_state_t;

/*
 * nscd_getent_ctx_base_t represents the nscd_getent_ctx_base_t pool
 * for a NSS database
 */
typedef struct nscd_getent_ctx_base {
	int			dbi;		/* which database? */
	int			max_getent_ctx;
	int			num_getent_ctx;
	struct nscd_getent_context *first;
} nscd_getent_ctx_base_t;

/*
 * nscd_getent_context_t is an abstraction of all the data needed
 * to enumerate a NSS database (e.g. "passwd" or "hosts")
 */
typedef struct nscd_getent_context {
	int				dbi;
	thread_t			thr_id;
	mutex_t				getent_mutex;
	int				aborted;
	int				in_use;
	int				num_reclaim_check;
	nscd_seq_num_t			seq_num;
	nscd_cookie_num_t		cookie_num;
	pid_t				pid;	/* door client's pid */
	int				n_src;	/* >=max_src: end of sequence */
	nscd_nsw_state_t		*nsw_state;
	nss_backend_t			*be;
	nscd_getent_ctx_base_t		*base;
	struct nscd_getent_context	*next;
	struct nscd_getent_context	*next_to_reclaim;
} nscd_getent_context_t;

/*
 * nscd_smf_state_t is used to keep track of the state of the smf
 * service associated with a NSS source (e.g. "passwd" or "hosts")
 */
typedef struct {
	char	*src_name;
	int	src_state;
} nscd_smf_state_t;

/*
 * nscd_smf_state_t is used to keep track of the state of the smf
 * service associated with a NSS source (e.g. "passwd" or "hosts")
 */
typedef struct {
	int			dbi;		/* database index */
	/*
	 * index of the database of which the switch policy
	 * should be used
	 */
	int			cfgdbi;
	/*
	 * index of the pseudo database that the NSS backend
	 * does search on
	 */
	int			compati;
	/*
	 * ptr to ptr to the siwtch config structure
	 */
	nscd_nsw_config_t	**nswcfg;
	/*
	 * frontend params passed to nss_search or nss_*ent
	 */
	struct nss_db_params	p;
	/*
	 * set to 1 if database is "hosts", else 2 if "ipnodes"
	 */
	int8_t			dnsi;
	/*
	 * set to 1 if require privilege to look up the database
	 */
	uint8_t			privdb;
} nscd_nsw_params_t;

/*
 * additional info returned by the switch engine
 */
typedef struct {
	void	*pbuf;	/* ptr to packed buffer */
	size_t	pbufsiz; /* length of the packed buffer */
	int	srci;	/* last source searched */
	int	errnum; /* errno from the backend */
	int	noarg; /* if set, backend does not use the arg structure */
	int	fallback; /* if set, may need to fall back to main nscd */
	int	datalen; /* pbuf->data_len (backend may change it) */
} nscd_sw_return_t;

/*
 * nscd cookies used for setent/getent/endent
 * - p0 cookie: returned by nscd to indicate
 *              the start of the enumeration position
 * - p1 cookie: returned/updated by nscd to indicate
 *              the current enumeration position
 */
#define	NSCD_P0_COOKIE_SEQNUM	-1
typedef struct {
	pid_t		p0_pid;
	time_t		p0_time;
	nscd_seq_num_t	p0_seqnum;
} nscd_getent_p0_cookie_t;

typedef struct {
	nscd_cookie_num_t	p1_cookie_num;
	nscd_seq_num_t		p1_seqnum;
} nscd_getent_p1_cookie_t;

/*
 * static tables or global data defined in other files
 */
extern int			_nscd_cfg_num_nsw_src;
extern int			_nscd_cfg_num_nsw_src_all;
extern int			_nscd_cfg_num_nsw_db;
extern int			_nscd_cfg_num_nsw_db_all;
extern int			_nscd_cfg_num_smf_services;
extern nscd_cfg_id_t		_nscd_cfg_nsw_src[];
extern nscd_cfg_id_t		*_nscd_cfg_nsw_src_all;
extern nscd_cfg_id_t		_nscd_cfg_nsw_db[];
extern nss_db_initf_t		nscd_nss_db_initf[];
extern nscd_cfg_id_t		_nscd_cfg_smf_services[];
extern nscd_smf_state_t		*nscd_smf_service_state;
extern nscd_db_t		***nscd_src_backend_db;
extern nscd_nsw_config_t	***nscd_nsw_config;
extern nscd_nsw_state_base_t	**nscd_nsw_state_base;
extern nscd_getent_ctx_base_t	**nscd_getent_ctx_base;
extern nscd_cfg_global_switch_t	nscd_switch_cfg_g;
extern nscd_cfg_switch_t	*nscd_switch_cfg;
extern nscd_cfg_stat_global_switch_t nscd_switch_stats_g;
extern nscd_cfg_stat_switch_t	*nscd_switch_stats;

#define	NSCD_NUM_SRC		_nscd_cfg_num_nsw_src_all
#define	NSCD_NUM_DB		_nscd_cfg_num_nsw_db_all
#define	NSCD_NUM_SMF_FMRI	_nscd_cfg_num_smf_services
#define	NSCD_NSW_SRC_NAME(i)	(_nscd_cfg_nsw_src_all + i)->name
#define	NSCD_NSW_DB_NAME(i)	_nscd_cfg_nsw_db[i].name
#define	NSCD_SMF_SVC_FMRI(i)	_nscd_cfg_smf_services[i].name
#define	NSCD_SMF_SVC_INDEX(i)	_nscd_cfg_smf_services[i].index
#define	NSCD_SMF_SVC_STATE(i)	nscd_smf_service_state[i].src_state
#define	NSCD_SW_CFG_G		nscd_switch_cfg_g
#define	NSCD_SW_CFG(i)		nscd_switch_cfg[i]
#define	NSCD_SW_STATS_G		nscd_switch_stats_g
#define	NSCD_SW_STATS(i)	nscd_switch_stats[i]

/*
 * special service states used by the switch engine
 */
#define	NSCD_SVC_STATE_UNINITED		-1
#define	NSCD_SVC_STATE_FOREIGN_SRC	-2
#define	NSCD_SVC_STATE_UNSUPPORTED_SRC	-3

/*
 * prototypes
 */

void
_nscd_put_nsw_state(
	nscd_nsw_state_t	*s);

void
_nscd_put_nsw_state_thread(
	nscd_nsw_state_t	*s);

nscd_rc_t
_nscd_get_nsw_state(
	nss_db_root_t		*rootp,
	nscd_nsw_params_t	*params);

nscd_rc_t
_nscd_get_nsw_state_thread(
	nss_db_root_t		*rootp,
	nscd_nsw_params_t	*params);

nscd_rc_t
_nscd_init_all_nsw_state_base();

nscd_rc_t
_nscd_init_nsw_state_base(
	int			dbi,
	int			compat_basei,
	int			lock);

nscd_rc_t
_nscd_init_all_getent_ctx();

nscd_rc_t
_nscd_init_getent_ctx_base(
	int			dbi,
	int			lock);

nscd_db_t
*_nscd_create_getent_ctxaddrDB();

nscd_rc_t
_nscd_get_getent_ctx(
	nss_getent_t		*contextpp,
	nscd_nsw_params_t	*params);

void
_nscd_put_getent_ctx(
	nscd_getent_context_t	*ctx);
void
_nscd_free_ctx_if_aborted(
	nscd_getent_context_t	*ctx);

int
_nscd_is_getent_ctx_in_use(
	nscd_getent_context_t	*ctx);

nscd_rc_t
_nscd_init_all_nsw_config();

nscd_rc_t
_nscd_init_all_nsw_be_info_db();

#ifdef NSCD_NSSWITCH_CONF_FROM_SMF_PROP
nscd_rc_t
_nscd_get_new_nsw_config(
	scf_handle_t		*hndl,
	const char		*fmri,
	scf_propertygroup_t	*pg);
#endif

nscd_rc_t
_nscd_get_new_service_state(
	int			index,
	scf_handle_t		*hndl,
	scf_property_t		*prop);

nscd_getent_context_t *
_nscd_is_getent_ctx(
	nscd_cookie_num_t	cookie_num);

nscd_rc_t
_nscd_create_sw_struct(
	int			dbi,
	int			compat_basei,
	const char		*dbn,
	const char		*cfgstr,
	void			*swcfgv1,
	nscd_nsw_params_t	*params);

nscd_rc_t
_nscd_create_new_config(
	nscd_nsw_params_t	*params);

void
_nscd_free_nsw_config(
	nscd_nsw_config_t	*nswcfg);

nscd_rc_t
_nscd_init_smf_monitor();

nscd_rc_t
_nscd_alloc_nsw_config();

nscd_rc_t
_nscd_alloc_service_state_table();

nscd_rc_t
_nscd_alloc_nsw_state_base();

nscd_rc_t
_nscd_alloc_nsw_be_info_db();

nscd_rc_t
_nscd_alloc_getent_ctx_base();

void
_nscd_free_all_nsw_state_base();

void
_nscd_free_all_getent_ctx_base();

void
_nscd_free_all_nsw_config();

void
_nscd_free_all_nsw_backend_info_db();

struct __nsw_switchconfig_v1 *
_nsw_getoneconfig_v1(
	const char		*name,
	char			*linep,
	enum __nsw_parse_err	*errp);
int
__nsw_freeconfig_v1(
	struct __nsw_switchconfig_v1 *conf);

int
_nscd_get_smf_state(int srci, int dbi, int recheck);

void
nss_psearch(void *buffer, size_t length);
void
nss_psetent(void *buffer, size_t length, pid_t pid);
void
nss_pgetent(void *buffer, size_t length);
void
nss_pendent(void *buffer, size_t length);
void
nss_pdelete(void *buffer, size_t length);

nscd_rc_t _nscd_alloc_switch_cfg();
nscd_rc_t _nscd_alloc_switch_stats();
nscd_db_t *_nscd_create_getent_ctx_addrDB();
nscd_db_t *_nscd_create_getent_ctxDB();

#ifdef	__cplusplus
}
#endif

#endif	/* _NSCD_SWITCH_H */