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