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