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