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