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