xref: /titanic_51/usr/src/cmd/nscd/nscd_switch.c (revision 89e921d5e5050628462effb26c9db2cf1a87fdf4)
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 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdlib.h>	/* getenv() */
28 #include <assert.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <pthread.h>
32 #include <dlfcn.h>
33 #include <nss_dbdefs.h>
34 #include <exec_attr.h>
35 #include <gssapi/gssapi.h>
36 #include "nscd_door.h"
37 #include "nscd_switch.h"
38 #include "nscd_log.h"
39 #include "nscd_frontend.h"
40 
41 #pragma weak nss_search = _nss_search
42 #define	nss_search	_nss_search
43 
44 extern rwlock_t nscd_smf_service_state_lock;
45 
46 /* nscd id: main, forker, or child */
47 extern int _whoami;
48 
49 static int
50 retry_test(nss_status_t res, int n, struct __nsw_lookup_v1 *lkp)
51 {
52 	if (res != NSS_TRYAGAIN && res !=  NSS_NISSERVDNS_TRYAGAIN) {
53 		if (res == NSS_SUCCESS) {
54 			__NSW_UNPAUSE_ACTION(lkp->actions[__NSW_TRYAGAIN]);
55 			__NSW_UNPAUSE_ACTION(
56 			    lkp->actions[__NSW_NISSERVDNS_TRYAGAIN]);
57 		}
58 		return (0);
59 	}
60 
61 	if ((res == NSS_TRYAGAIN &&
62 	    lkp->actions[__NSW_TRYAGAIN] == __NSW_TRYAGAIN_FOREVER) ||
63 	    (res == NSS_NISSERVDNS_TRYAGAIN &&
64 	    lkp->actions[__NSW_NISSERVDNS_TRYAGAIN] == __NSW_TRYAGAIN_FOREVER))
65 		return (1);
66 
67 	if (res == NSS_TRYAGAIN &&
68 	    lkp->actions[__NSW_TRYAGAIN] == __NSW_TRYAGAIN_NTIMES)
69 		if (n <= lkp->max_retries)
70 			return (1);
71 		else {
72 			lkp->actions[__NSW_TRYAGAIN] = __NSW_TRYAGAIN_PAUSED;
73 			return (0);
74 		}
75 
76 	if (res == NSS_NISSERVDNS_TRYAGAIN &&
77 	    lkp->actions[__NSW_NISSERVDNS_TRYAGAIN] == __NSW_TRYAGAIN_NTIMES)
78 		if (n <= lkp->max_retries)
79 			return (1);
80 		else {
81 			lkp->actions[__NSW_NISSERVDNS_TRYAGAIN] =
82 			    __NSW_TRYAGAIN_PAUSED;
83 			return (0);
84 		}
85 
86 	return (0);
87 }
88 
89 static thread_key_t loopback_key = THR_ONCE_KEY;
90 typedef struct lb_key {
91 	int		srci;
92 	int		dbi;
93 	int		fnum;
94 	int		*lb_flagp;
95 } lb_key_t;
96 
97 static int
98 set_loopback_key(lb_key_t *key) {
99 
100 	int		rc;
101 
102 	rc = thr_keycreate_once(&loopback_key, NULL);
103 	/* set key if not already set */
104 	if (rc == 0 && pthread_getspecific(loopback_key) == NULL)
105 		rc = thr_setspecific(loopback_key, key);
106 
107 	return (rc);
108 }
109 
110 static lb_key_t *
111 get_loopback_key(void) {
112 
113 	char		*me = "get_loopback_key";
114 	lb_key_t	*k = NULL;
115 
116 	k = pthread_getspecific(loopback_key);
117 
118 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
119 	(me, "get loopback key, key = %p\n", k);
120 
121 	return (k);
122 }
123 
124 static void
125 clear_loopback_key(lb_key_t *key) {
126 
127 	char		*me = "clear_loopback_key";
128 
129 	if (loopback_key != THR_ONCE_KEY && key != NULL) {
130 		/*
131 		 * key->lb_flagp points to the location of the
132 		 * flag, check_flag, in the stack where it was
133 		 * first set; clearing the flag tells that
134 		 * stack the loopback error has been resolved
135 		 */
136 		*key->lb_flagp = 0;
137 		(void) thr_setspecific(loopback_key, NULL);
138 	}
139 
140 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
141 	(me, "key %p cleared\n", key);
142 }
143 
144 static thread_key_t initf_key = THR_ONCE_KEY;
145 
146 static int
147 set_initf_key(void *pbuf) {
148 
149 	int		rc;
150 
151 	rc = thr_keycreate_once(&initf_key, NULL);
152 	if (rc == 0)
153 		rc = thr_setspecific(initf_key, pbuf);
154 
155 	return (rc);
156 }
157 
158 static void *
159 get_initf_key(void) {
160 
161 	char		*me = "get_initf_key";
162 	void		*pbuf;
163 
164 	pbuf = pthread_getspecific(initf_key);
165 
166 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
167 	(me, "got initf pbuf, key = %p\n", pbuf);
168 
169 	return (pbuf);
170 }
171 
172 static void
173 clear_initf_key(void) {
174 
175 	char		*me = "clear_initf_key";
176 
177 	(void) thr_setspecific(initf_key, NULL);
178 
179 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
180 	(me, "initf pbuf cleared\n");
181 }
182 
183 /*
184  * Call the input initf function to extract the
185  * NSS front end parameters and examine them to
186  * determine if an NSS lookup is to be performed
187  * on a regular or a pseudo (called from compat
188  * backend) database. Then set the necessary
189  * parameters for later data structures creation
190  * and processing.
191  */
192 static nscd_rc_t
193 getparams(
194 	int			search_fnum,
195 	nss_db_initf_t		initf,
196 	nscd_nsw_params_t	*params)
197 {
198 
199 	nscd_rc_t	rc = NSCD_SUCCESS;
200 	nss_db_params_t	*p;
201 	int		j;
202 	char		*dbn;
203 	const char	*n;
204 	char		*me = "getparams";
205 
206 	p = &params->p;
207 	(void) memset(p, 0, sizeof (*p));
208 	(*initf)(p);
209 	params->dbi = -1;
210 	params->cfgdbi = -1;
211 	params->compati = -1;
212 	params->dnsi = -1;
213 
214 	/* map database name to index */
215 	n = p->name;
216 	for (j = 0; j < NSCD_NUM_DB; j++) {
217 		dbn = NSCD_NSW_DB_NAME(j);
218 		if (*n != *dbn)
219 			continue;
220 		if (strcmp(n, dbn) == 0) {
221 			params->dbi = j;
222 			if (*n != 'h' && *n != 'i' && *n != 's' && *n != 'a')
223 				break;
224 			if (strcmp(n, NSS_DBNAM_HOSTS) == 0 &&
225 			    search_fnum == NSS_DBOP_HOSTS_BYNAME)
226 				params->dnsi = 0;
227 			else if (strcmp(n, NSS_DBNAM_IPNODES) == 0 &&
228 			    search_fnum == NSS_DBOP_IPNODES_BYNAME)
229 				params->dnsi = 1;
230 			else if (strcmp(n, NSS_DBNAM_SHADOW) == 0)
231 				params->privdb = 1;
232 			break;
233 		}
234 	}
235 
236 	/*
237 	 * use the switch policy for passwd_compat or
238 	 * group_compat?
239 	 */
240 	if (p->config_name != NULL) {
241 
242 		n = p->config_name;
243 		for (j = 0; j < NSCD_NUM_DB; j++) {
244 			dbn = NSCD_NSW_DB_NAME(j);
245 			if (*n == *dbn) {
246 				if (strcmp(n, dbn) == 0) {
247 					params->cfgdbi = j;
248 					break;
249 				}
250 			}
251 		}
252 	}
253 
254 	/* map the database name to the pseudo database index */
255 	if (params->cfgdbi != -1) {
256 		if (strstr(p->config_name, "_compat") != NULL) {
257 			n = p->name;
258 			for (j = params->cfgdbi; j < NSCD_NUM_DB; j++) {
259 				dbn = NSCD_NSW_DB_NAME(j);
260 				if (*n == *dbn) {
261 					if (strcmp(n, dbn) == 0) {
262 						params->compati = j;
263 						break;
264 					}
265 				}
266 			}
267 		}
268 	}
269 
270 	/*
271 	 * if unsupported database, let caller determine what to do next
272 	 */
273 	if (params->dbi == -1) {
274 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
275 		(me, "unsupported database: %s\n", p->name);
276 		return (NSCD_CFG_UNSUPPORTED_SWITCH_DB);
277 	}
278 
279 	return (rc);
280 }
281 
282 static void
283 nscd_initf(nss_db_params_t	*p)
284 {
285 	nss_pheader_t		*pbuf;
286 	nssuint_t		off;
287 	nss_dbd_t		*pdbd;
288 	char			*me = "nscd_initf";
289 
290 	pbuf = (nss_pheader_t *)get_initf_key();
291 	if (pbuf == NULL) {
292 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
293 		(me, "ERROR: initf key not set\n");
294 		return;
295 	}
296 
297 	if (pbuf->dbd_len <= sizeof (nss_dbd_t)) {
298 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
299 		(me, "invalid db front params data ? dbd_len = %d\n",
300 		    pbuf->dbd_len);
301 		return;
302 	}
303 
304 	off = pbuf->dbd_off;
305 	pdbd = (nss_dbd_t *)((void *)((char *)pbuf + off));
306 
307 	p->name = (char *)pdbd + pdbd->o_name;
308 	p->config_name = (char *)pdbd + pdbd->o_config_name;
309 	p->default_config = (char *)pdbd + pdbd->o_default_config;
310 	p->flags = (enum nss_dbp_flags)pdbd->flags;
311 	(void) memcpy(&p->private, &pbuf->nscdpriv, sizeof (p->private));
312 
313 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
314 	(me, "db frontend params: name =%s, config_name = %s, "
315 	"default_config = %s, flags = %x\n", p->name,
316 	    (p->config_name && *p->config_name != '\0' ?
317 	    p->config_name : "<NOT SPECIFIED>"),
318 	    (p->default_config && *p->default_config != '\0' ?
319 	    p->default_config : "<NOT SPECIFIED>"),
320 	    p->flags);
321 }
322 
323 
324 static void
325 trace_result(
326 	int		dbi,
327 	int		srci,
328 	int		op,
329 	nss_status_t	res,
330 	nss_XbyY_args_t	*arg)
331 {
332 	char	*res_str;
333 	char	*src = "?";
334 	char	*db = "?";
335 	char	*data_str = "<NOT STRING FORMAT>";
336 	int	data_len = 0;
337 	char	*me = "trace_result";
338 
339 	switch (res) {
340 	case NSS_SUCCESS:
341 		res_str = "NSS_SUCCESS";
342 		break;
343 	case NSS_NOTFOUND:
344 		res_str = "NSS_NOTFOUND";
345 		break;
346 	case NSS_UNAVAIL:
347 		res_str = "NSS_UNAVAIL";
348 		break;
349 	case NSS_TRYAGAIN:
350 		res_str = "NSS_TRYAGAIN";
351 		break;
352 	case NSS_NISSERVDNS_TRYAGAIN:
353 		res_str = "NSS_NISSERVDNS_TRYAGAIN";
354 		break;
355 	default:
356 		res_str = "UNKNOWN STATUS";
357 		break;
358 	}
359 
360 	if (dbi != -1)
361 		db = NSCD_NSW_DB_NAME(dbi);
362 	if (srci != -1)
363 		src = NSCD_NSW_SRC_NAME(srci);
364 
365 	if (arg->buf.result == NULL) {
366 		data_str = arg->buf.buffer;
367 		data_len = arg->returnlen;
368 	}
369 
370 	if (res == NSS_SUCCESS) {
371 		_nscd_logit(me, "%s: database: %s, operation: %d, "
372 		    "source: %s returned >>%s<<, length = %d\n",
373 		    res_str, db, op, src, data_str, data_len);
374 		return;
375 	}
376 
377 	_nscd_logit(me, "%s: database: %s, operation: %d, source: %s, "
378 	    "erange= %d, herrno: %s (%d)\n",
379 	    res_str, db, op, src, arg->erange, hstrerror(arg->h_errno),
380 	    arg->h_errno);
381 }
382 
383 /*
384  * Determine if a request should be done locally in the getXbyY caller's
385  * process. Return none zero if yes, 0 otherwise. This should be called
386  * before the switch engine steps through the backends/sources.
387  * This function returns 1 if:
388  *   -- the database is exec_attr and the search_flag is GET_ALL
389  */
390 static int
391 try_local(
392 	int			dbi,
393 	void			*arg)
394 {
395 	struct nss_XbyY_args	*ap = (struct nss_XbyY_args *)arg;
396 	_priv_execattr		*ep;
397 	int			rc = 0;
398 	char			*me = "try_local";
399 
400 	if (strcmp(NSCD_NSW_DB_NAME(dbi), NSS_DBNAM_EXECATTR) == 0) {
401 		if ((ep = ap->key.attrp) != NULL && ep->search_flag == GET_ALL)
402 			rc = 1;
403 	}
404 
405 	if (rc != 0) {
406 
407 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
408 		(me, "TRYLOCAL: exec_attr:GET_ALL\n");
409 	}
410 
411 	return (rc);
412 }
413 
414 /*
415  * Determine if a request should be done locally in the getXbyY caller's
416  * process. Return none zero if yes, 0 otherwise. This should be called
417  * before the switch engine invokes any backend.
418  * This function returns 1 if:
419  *   -- the database is shadow and the source is nisplus OR
420  *   -- the database is shadow and the source is compat
421  */
422 static int
423 try_local2(
424 	int	dbi,
425 	int	srci)
426 {
427 	int	rc = 0;
428 	char	*me = "try_local2";
429 
430 	if (*NSCD_NSW_DB_NAME(dbi) == 's' &&
431 	    strcmp(NSCD_NSW_DB_NAME(dbi), NSS_DBNAM_SHADOW) == 0) {
432 		if (strcmp(NSCD_NSW_SRC_NAME(srci), "nisplus") == 0 ||
433 		    strcmp(NSCD_NSW_SRC_NAME(srci), "compat") == 0)
434 			rc = 1;
435 	}
436 
437 	if (rc != 0) {
438 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
439 		(me, "TRYLOCAL: database: shadow, source: %s\n",
440 		    NSCD_NSW_SRC_NAME(srci));
441 	}
442 
443 	return (rc);
444 }
445 
446 static nscd_rc_t
447 get_lib_func(void **handle, void **func, mutex_t *lock,
448 	char *lib, char *name, void **func_p)
449 {
450 	char	*me = "get_lib_func";
451 	void	*sym;
452 
453 	if (func_p != NULL && *handle != NULL && *func != NULL) {
454 		*func_p = *func;
455 		return (NSCD_SUCCESS);
456 	}
457 
458 	(void) mutex_lock(lock);
459 
460 	/* close the handle if requested */
461 	if (func_p == NULL) {
462 		if (*handle != NULL) {
463 			(void) dlclose(*handle);
464 			*handle = NULL;
465 			*func = NULL;
466 		}
467 		(void) mutex_unlock(lock);
468 		return (NSCD_SUCCESS);
469 	}
470 
471 	if (*handle != NULL && *func != NULL) {
472 		*func_p = *func;
473 		(void) mutex_unlock(lock);
474 		return (NSCD_SUCCESS);
475 	}
476 
477 	if (*handle == NULL) {
478 		*handle = dlopen(lib, RTLD_LAZY);
479 		if (*handle == NULL) {
480 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_ERROR)
481 			(me, "unable to dlopen %s\n", lib);
482 			(void) mutex_unlock(lock);
483 			return (NSCD_CFG_DLOPEN_ERROR);
484 		}
485 	}
486 
487 	if ((sym = dlsym(*handle, name)) == NULL) {
488 
489 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_ERROR)
490 		(me, "unable to find symbol %s:%s\n", lib, name);
491 		(void) mutex_unlock(lock);
492 		return (NSCD_CFG_DLSYM_ERROR);
493 	} else {
494 		*func_p = sym;
495 		*func = sym;
496 	}
497 
498 	(void) mutex_unlock(lock);
499 	return (NSCD_SUCCESS);
500 }
501 
502 static nscd_rc_t
503 get_libc_nss_search(void **func_p)
504 {
505 	static void	*handle = NULL;
506 	static void	*func = NULL;
507 	static mutex_t	lock = DEFAULTMUTEX;
508 
509 	return (get_lib_func(&handle, &func, &lock,
510 	    "libc.so", "nss_search", func_p));
511 }
512 
513 static nscd_rc_t
514 get_gss_func(void **func_p)
515 {
516 	static void	*handle = NULL;
517 	static void	*func = NULL;
518 	static mutex_t	lock = DEFAULTMUTEX;
519 
520 	return (get_lib_func(&handle, &func, &lock,
521 	    "libgss.so", "gss_inquire_cred", func_p));
522 }
523 
524 /*
525  * get_dns_funcs returns pointers to gethostbyname functions in the
526  * dynamically loaded nss_dns & nss_mdns modules that return host
527  * lookup results along with the TTL value in the DNS resource
528  * records. The dnsi parameter indicates whether the lookup database
529  * is hosts(0) or ipnodes(1). The srcname parameter identifies the DNS
530  * module: dns/mdns and the function returns the address of the specific
531  * gethostbyname function in func_p variable.
532  */
533 static nscd_rc_t
534 get_dns_funcs(int dnsi, nss_status_t (**func_p)(), const char *srcname)
535 {
536 	int		si;
537 	void		**funcpp;
538 	static void	*handle[2] = { NULL, NULL };
539 	static mutex_t	func_lock[2] = { DEFAULTMUTEX, DEFAULTMUTEX };
540 	static void 	*func[2][2] = {{NULL, NULL}, {NULL, NULL}};
541 	static const char	*lib[2] = { "nss_dns.so.1", "nss_mdns.so.1" };
542 	static const char 	*func_name[2][2] =
543 		{{ "_nss_get_dns_hosts_name", "_nss_get_dns_ipnodes_name" },
544 		{ "_nss_get_mdns_hosts_name", "_nss_get_mdns_ipnodes_name" }};
545 
546 	/* source index: 0 = dns, 1 = mdns */
547 	if (strcmp(srcname, "dns") == 0)
548 		si = 0;
549 	else
550 		si = 1;
551 
552 	/*
553 	 * function index (func[si][dnsi]):
554 	 * [0,0] = dns/hosts, [0,1] = dns/ipnodes,
555 	 * [1,0] = mdns/hosts, [1,1] = mdns/ipnodes
556 	 */
557 
558 	if (dnsi < 0) { /* close handle */
559 		funcpp = NULL;
560 		(void) mutex_lock(&func_lock[si]);
561 		func[si][0] = NULL;
562 		func[si][1] = NULL;
563 		(void) mutex_unlock(&func_lock[si]);
564 	} else
565 		funcpp = (void **)func_p;
566 
567 	return (get_lib_func(&handle[si], &func[si][dnsi], &func_lock[si],
568 	    (char *)lib[si], (char *)func_name[si][dnsi], funcpp));
569 }
570 
571 static nss_status_t
572 search_dns_withttl(nscd_sw_return_t *swret, const char *srcname, int dnsi)
573 {
574 	nss_status_t	(*func)();
575 	nss_status_t	res = NSS_UNAVAIL;
576 	nscd_rc_t	rc;
577 
578 	swret->noarg = 0;
579 	if (strcmp(srcname, "dns") != 0 && strcmp(srcname, "mdns") != 0)
580 		return (NSS_ERROR);
581 
582 	rc = get_dns_funcs(dnsi, &func, srcname);
583 	if (rc == NSCD_SUCCESS) {
584 		/*
585 		 * data_len in the packed buf header may be changed
586 		 * by the dns or mdns backend, reset it just in
587 		 * case
588 		 */
589 		((nss_pheader_t *)swret->pbuf)->data_len =
590 		    swret->datalen;
591 		res = (func)(NULL, &swret->pbuf, &swret->pbufsiz);
592 	}
593 	return (res);
594 }
595 
596 /*
597  * Returns a flag to indicate if needs to fall back to the
598  * main nscd when a per-user lookup failed with rc NSS_NOTFOUND.
599  */
600 static int
601 set_fallback_flag(char *srcname, nss_status_t rc)
602 {
603 	char	*me = "set_fallback_flag";
604 	if (strcmp(srcname, "ldap") == 0 && rc == NSS_NOTFOUND) {
605 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
606 		(me, "NSS_NOTFOUND (ldap): fallback to main nscd "
607 		"may be needed\n");
608 		return (1);
609 	}
610 	return (0);
611 }
612 
613 nss_status_t
614 nss_search(nss_db_root_t *rootp, nss_db_initf_t initf, int search_fnum,
615 	void *search_args)
616 {
617 	char			*me = "nss_search";
618 	nss_status_t		res = NSS_UNAVAIL;
619 	nscd_nsw_state_t	*s = NULL;
620 	int			n_src;
621 	unsigned int		status_vec = 0;
622 	int			dbi, srci = -1;
623 	int			check_loopback = 0;
624 	int			state_thr = 0;
625 	lb_key_t		key, *k = NULL;
626 	nss_db_root_t		root_db;
627 	nscd_nsw_params_t	params;
628 	nscd_sw_return_t	*swret;
629 
630 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
631 	(me, "rootp = %p, initf = %p, search_fnum = %d, "
632 	    "search_args = %p\n", rootp, initf,
633 	    search_fnum, search_args);
634 
635 	NSCD_SW_STATS_G.lookup_request_received_g++;
636 	NSCD_SW_STATS_G.lookup_request_in_progress_g++;
637 	NSCD_SW_STATS_G.lookup_request_queued_g++;
638 
639 	/* determine db index, cfg db index, etc */
640 	if (getparams(search_fnum, initf, &params) ==
641 	    NSCD_CFG_UNSUPPORTED_SWITCH_DB) {
642 		/*
643 		 * if unsupported database and the request is from the
644 		 * the door, tell the door client to try it locally
645 		 */
646 		if (initf == nscd_initf) {
647 			res = NSS_TRYLOCAL;
648 			goto error_exit;
649 		} else { /* otherwise, let libc:nss_search() handle it */
650 			nss_status_t	(*func)();
651 
652 			if (get_libc_nss_search((void **)&func) ==
653 			    NSCD_SUCCESS)
654 				return ((func)(rootp, initf, search_fnum,
655 				    search_args));
656 			else
657 				goto error_exit;
658 		}
659 	}
660 	dbi = params.dbi;
661 
662 	/* get address of the switch engine return data area */
663 	if (initf == nscd_initf) {
664 		swret = (nscd_sw_return_t *)params.p.private;
665 		swret->srci = -1;
666 	} else {
667 		swret = NULL;
668 		params.dnsi = -1;
669 	}
670 
671 	/*
672 	 * for door request that should be processed by the client,
673 	 * send it back with status NSS_TRYLOCAL
674 	 */
675 	if (initf == nscd_initf && try_local(dbi, search_args) == 1) {
676 		res = NSS_TRYLOCAL;
677 		goto error_exit;
678 	}
679 
680 	NSCD_SW_STATS(dbi).lookup_request_received++;
681 	NSCD_SW_STATS(dbi).lookup_request_in_progress++;
682 	NSCD_SW_STATS(dbi).lookup_request_queued++;
683 
684 	/* if lookup not enabled, return NSS_UNAVAIL  */
685 	if (!(NSCD_SW_CFG_G.enable_lookup_g == nscd_true &&
686 	    NSCD_SW_CFG(dbi).enable_lookup == nscd_true)) {
687 
688 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
689 		(me, "lookup not enabled for %s\n", NSCD_NSW_DB_NAME(dbi));
690 
691 		goto error_exit;
692 	}
693 
694 	/* determine if loopback checking is configured */
695 	if (NSCD_SW_CFG_G.enable_loopback_checking_g == nscd_true &&
696 	    NSCD_SW_CFG(dbi).enable_loopback_checking == nscd_true) {
697 		check_loopback = 1;
698 
699 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
700 		(me, "loopback checking enabled for %s\n",
701 		    NSCD_NSW_DB_NAME(dbi));
702 	}
703 
704 	if (check_loopback) {
705 		k = get_loopback_key();
706 		if (k != NULL) {
707 			if (k->dbi != dbi || k->fnum != search_fnum) {
708 				clear_loopback_key(k);
709 				k = NULL;
710 			}
711 		}
712 	}
713 
714 	if (s == 0) {
715 		nscd_rc_t	rc;
716 
717 		if (check_loopback) {
718 			rc = _nscd_get_nsw_state_thread(&root_db, &params);
719 			state_thr = 1;
720 		} else
721 			rc = _nscd_get_nsw_state(&root_db, &params);
722 
723 		NSCD_SW_STATS_G.lookup_request_queued_g--;
724 		NSCD_SW_STATS(dbi).lookup_request_queued--;
725 
726 		if (rc != NSCD_SUCCESS)
727 				goto error_exit;
728 
729 		s = (nscd_nsw_state_t *)root_db.s;
730 	}
731 
732 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
733 	(me, "database = %s, config = >>%s<<\n", NSCD_NSW_DB_NAME(dbi),
734 	    (*s->nsw_cfg_p)->nsw_cfg_str);
735 
736 	for (n_src = 0;  n_src < s->max_src;  n_src++) {
737 		nss_backend_t		*be = NULL;
738 		nss_backend_op_t	funcp = NULL;
739 		struct __nsw_lookup_v1	*lkp;
740 		int			smf_state;
741 		int			n_loop = 0;
742 		int			max_retry = 10;
743 
744 		res = NSS_UNAVAIL;
745 
746 		if (n_src == 0)
747 			lkp = s->config->lookups;
748 		else
749 			lkp = lkp->next;
750 
751 		/* set the number of max. retries */
752 		if (lkp->actions[__NSW_TRYAGAIN] == __NSW_TRYAGAIN_NTIMES)
753 			max_retry = lkp->max_retries;
754 
755 		srci = (*s->nsw_cfg_p)->src_idx[n_src];
756 		if (swret != NULL)
757 			swret->srci = srci;
758 
759 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
760 		(me, "nsw source = %s\n", NSCD_NSW_SRC_NAME(srci));
761 
762 		/* if no privilege to look up, skip */
763 		if (params.privdb == 1 && swret != NULL &&
764 		    strcmp(NSCD_NSW_SRC_NAME(srci), "files") == 0 &&
765 		    _nscd_check_client_read_priv() != 0) {
766 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
767 			(me, "no privilege to look up, skip source\n");
768 
769 			goto next_src;
770 		}
771 
772 		/* get state of the (backend) client service */
773 		smf_state = _nscd_get_smf_state(srci, dbi, 0);
774 
775 		/* stop if the source is one that should be TRYLOCAL */
776 		if (initf == nscd_initf &&	/* request is from the door */
777 		    (smf_state == NSCD_SVC_STATE_UNSUPPORTED_SRC ||
778 		    (smf_state == NSCD_SVC_STATE_FOREIGN_SRC &&
779 		    s->be_version_p[n_src] == NULL) ||
780 		    (params.privdb && try_local2(dbi, srci) == 1))) {
781 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
782 			(me, "returning TRYLOCAL ... \n");
783 			res = NSS_TRYLOCAL;
784 			goto free_nsw_state;
785 		}
786 
787 		if (check_loopback && k != NULL) {
788 
789 			if (k->srci == srci && k->dbi == dbi)
790 				if (k->fnum == search_fnum) {
791 
792 					_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
793 					    NSCD_LOG_LEVEL_DEBUG)
794 					(me, "loopback detected: "
795 					    "source = %s, database = %s "
796 					    "search fnum = %d\n",
797 					    NSCD_NSW_SRC_NAME(srci),
798 					    NSCD_NSW_DB_NAME(dbi), search_fnum);
799 
800 				NSCD_SW_STATS_G.loopback_nsw_db_skipped_g++;
801 				NSCD_SW_STATS(dbi).loopback_nsw_db_skipped++;
802 					continue;
803 				}
804 		}
805 
806 		be = s->be[n_src];
807 		if (be != NULL)
808 			funcp = NSS_LOOKUP_DBOP(be, search_fnum);
809 
810 		/* request could be from within nscd so check states again */
811 		if (be == NULL || (params.dnsi < 0 && (funcp == NULL ||
812 		    (smf_state != NSCD_SVC_STATE_UNINITED &&
813 		    smf_state != NSCD_SVC_STATE_UNSUPPORTED_SRC &&
814 		    smf_state != NSCD_SVC_STATE_FOREIGN_SRC &&
815 		    smf_state < SCF_STATE_ONLINE)))) {
816 
817 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
818 			    NSCD_LOG_LEVEL_DEBUG)
819 			(me, "unable to look up source %s: be = %p, "
820 			"smf state = %d, funcp = %p\n",
821 			    NSCD_NSW_SRC_NAME(srci), be, smf_state, funcp);
822 
823 			goto next_src;
824 		}
825 
826 		do {
827 			/*
828 			 * we can only retry max_retry times,
829 			 * otherwise threads may get stuck in this
830 			 * do-while loop forever
831 			 */
832 			if (n_loop > max_retry) {
833 				if (swret != NULL)
834 					res = NSS_TRYLOCAL;
835 				goto free_nsw_state;
836 			}
837 
838 			/*
839 			 * set up to prevent loopback
840 			 */
841 			if (check_loopback && k == NULL) {
842 				key.srci = srci;
843 				key.dbi = dbi;
844 				key.fnum = search_fnum;
845 				key.lb_flagp = &check_loopback;
846 				(void) set_loopback_key(&key);
847 				k = &key;
848 			}
849 
850 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
851 			    NSCD_LOG_LEVEL_DEBUG)
852 			(me, "looking up source = %s, loop# = %d \n",
853 			    NSCD_NSW_SRC_NAME(srci), n_loop);
854 
855 			/*
856 			 * search the backend, if hosts lookups,
857 			 * try to get the hosts data with ttl first
858 			 */
859 			if (params.dnsi >= 0) {
860 				res = search_dns_withttl(swret,
861 				    NSCD_NSW_SRC_NAME(srci), params.dnsi);
862 				/*
863 				 * if not able to get ttl, fall back
864 				 * to the regular backend call
865 				 */
866 				if (res == NSS_ERROR)
867 					res = (*funcp)(be, search_args);
868 				else {
869 					/*
870 					 * status/result are in the
871 					 * packed buffer, not
872 					 * search_args
873 					 */
874 					swret->noarg = 1;
875 				}
876 			} else
877 				res = (*funcp)(be, search_args);
878 			if (swret != NULL)
879 				swret->errnum = errno;
880 
881 			/*
882 			 * backend is not up, check and update the
883 			 * smf state table
884 			 */
885 			if (res == NSS_UNAVAIL)
886 				(void) _nscd_get_smf_state(srci, dbi, 1);
887 
888 			/*
889 			 * may need to fall back to use the main nscd
890 			 * if per-user lookup
891 			 */
892 			if (_whoami == NSCD_CHILD && swret != NULL)
893 				swret->fallback = set_fallback_flag(
894 				    NSCD_NSW_SRC_NAME(srci), res);
895 
896 			_NSCD_LOG_IF(NSCD_LOG_SWITCH_ENGINE,
897 			    NSCD_LOG_LEVEL_DEBUG) {
898 
899 				/*
900 				 * set up to trace the result/status
901 				 * of the dns/ttl lookup
902 				 */
903 				if (swret != NULL && swret->noarg == 1) {
904 					nss_pheader_t *phdr;
905 					struct nss_XbyY_args *arg;
906 					arg = (struct nss_XbyY_args *)
907 					    search_args;
908 					phdr = (nss_pheader_t *)swret->pbuf;
909 					arg->buf.buffer = (char *)phdr +
910 					    phdr->data_off;
911 					arg->returnlen = phdr->data_len;
912 					if (phdr->p_errno == ERANGE)
913 						arg->erange = 1;
914 					arg->h_errno = phdr->p_herrno;
915 				}
916 
917 				trace_result(dbi, srci, search_fnum, res,
918 				    (nss_XbyY_args_t *)search_args);
919 			}
920 
921 			n_loop++;
922 		} while (retry_test(res, n_loop, lkp));
923 
924 		next_src:
925 
926 		status_vec |= (1 << res);
927 
928 		if (__NSW_ACTION_V1(lkp, res) == __NSW_RETURN) {
929 			break;
930 		}
931 	}
932 
933 	free_nsw_state:
934 
935 	if (state_thr == 1)
936 		_nscd_put_nsw_state_thread(s);
937 	else
938 		_nscd_put_nsw_state(s);
939 	if (check_loopback && k != NULL)
940 		clear_loopback_key(k);
941 
942 	if (res != NSS_SUCCESS)
943 		goto error_exit;
944 
945 	NSCD_SW_STATS_G.lookup_request_succeeded_g++;
946 	NSCD_SW_STATS(dbi).lookup_request_succeeded++;
947 	NSCD_SW_STATS_G.lookup_request_in_progress_g--;
948 	NSCD_SW_STATS(dbi).lookup_request_in_progress--;
949 
950 	return (NSS_SUCCESS);
951 
952 	error_exit:
953 
954 	NSCD_SW_STATS_G.lookup_request_failed_g++;
955 	NSCD_SW_STATS_G.lookup_request_in_progress_g--;
956 	NSCD_SW_STATS(dbi).lookup_request_failed++;
957 	NSCD_SW_STATS(dbi).lookup_request_in_progress--;
958 
959 	return (res);
960 }
961 
962 
963 /* ===> get/set/endent */
964 
965 static void		nss_setent_u(nss_db_root_t *,
966 				    nss_db_initf_t,
967 				    nss_getent_t *);
968 static nss_status_t	nss_getent_u(nss_db_root_t *,
969 				    nss_db_initf_t,
970 				    nss_getent_t *,
971 				    void *);
972 static void		nss_endent_u(nss_db_root_t *,
973 				    nss_db_initf_t,
974 				    nss_getent_t *);
975 
976 void
977 nss_setent(nss_db_root_t *rootp, nss_db_initf_t initf,
978 	nss_getent_t *contextpp)
979 {
980 	if (contextpp == 0)
981 		return;
982 	nss_setent_u(rootp, initf, contextpp);
983 }
984 
985 nss_status_t
986 nss_getent(nss_db_root_t *rootp, nss_db_initf_t initf, nss_getent_t *contextpp,
987 	void *args)
988 {
989 	nss_status_t		status;
990 
991 	if (contextpp == 0) {
992 		return (NSS_UNAVAIL);
993 	}
994 	status = nss_getent_u(rootp, initf, contextpp, args);
995 	return (status);
996 }
997 
998 void
999 nss_endent(nss_db_root_t *rootp, nss_db_initf_t initf,
1000 	nss_getent_t *contextpp)
1001 {
1002 	if (contextpp == 0)
1003 		return;
1004 	nss_endent_u(rootp, initf, contextpp);
1005 }
1006 
1007 /*ARGSUSED*/
1008 static void
1009 end_iter_u(nss_db_root_t *rootp, struct nss_getent_context *contextp)
1010 {
1011 	nscd_getent_context_t	*ctx;
1012 	nscd_nsw_state_t	*s;
1013 	nss_backend_t		*be;
1014 	int			n_src;
1015 
1016 	ctx = (nscd_getent_context_t *)contextp;
1017 	s = ctx->nsw_state;
1018 	n_src = ctx->n_src;
1019 	be = ctx->be;
1020 
1021 	if (s != 0) {
1022 		if (n_src < s->max_src && be != 0) {
1023 			(void) NSS_INVOKE_DBOP(be, NSS_DBOP_ENDENT, 0);
1024 			ctx->be = 0;  /* Should be unnecessary, but hey */
1025 		}
1026 	}
1027 	ctx->n_src = 0;
1028 }
1029 
1030 static void
1031 nss_setent_u(nss_db_root_t *rootp, nss_db_initf_t initf,
1032 	nss_getent_t *contextpp)
1033 {
1034 	char			*me = "nss_setent_u";
1035 	nscd_nsw_state_t	*s;
1036 	nscd_getent_context_t	*contextp;
1037 	nscd_nsw_params_t	params;
1038 	nss_db_root_t		root;
1039 	nss_backend_t		*be;
1040 	int			n_src, i;
1041 	nscd_sw_return_t	*swret = NULL;
1042 
1043 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1044 	(me, "rootp = %p, initf = %p, contextpp = %p \n",
1045 	    rootp, initf, contextpp);
1046 
1047 	/*
1048 	 * Get the nsw db index via the initf function. If unsupported
1049 	 * database, no need to continue
1050 	 */
1051 	if (getparams(-1, initf, &params) == NSCD_CFG_UNSUPPORTED_SWITCH_DB)
1052 		return;
1053 
1054 	/* get address of the switch engine return data area */
1055 	if (initf == nscd_initf)
1056 		swret = (nscd_sw_return_t *)params.p.private;
1057 
1058 	/* if no privilege to look up, return */
1059 	if (params.privdb == 1 && swret != NULL &&
1060 	    _nscd_check_client_read_priv() != 0) {
1061 
1062 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1063 		(me, "no privilege \n");
1064 		return;
1065 	}
1066 
1067 	if ((contextp = (nscd_getent_context_t *)contextpp->ctx) == 0) {
1068 		if ((_nscd_get_getent_ctx(contextpp, &params)) !=
1069 		    NSCD_SUCCESS) {
1070 			return;
1071 		}
1072 		contextp = (nscd_getent_context_t *)contextpp->ctx;
1073 	}
1074 	s = contextp->nsw_state;
1075 
1076 	if (s == 0) {
1077 		if (_nscd_get_nsw_state(&root, &params) !=
1078 		    NSCD_SUCCESS) {
1079 			return;
1080 		}
1081 		s = (nscd_nsw_state_t *)root.s;
1082 		contextp->nsw_state = s;
1083 
1084 	} else {
1085 		s	= contextp->nsw_state;
1086 		n_src	= contextp->n_src;
1087 		be	= contextp->be;
1088 		if (n_src == 0 && be != 0) {
1089 			/*
1090 			 * Optimization:  don't do endent, don't change
1091 			 *   backends, just do the setent.  Look Ma, no locks
1092 			 *   (nor any context that needs updating).
1093 			 */
1094 			(void) NSS_INVOKE_DBOP(be, NSS_DBOP_SETENT, 0);
1095 			return;
1096 		}
1097 		if (n_src < s->max_src && be != 0) {
1098 			(void) NSS_INVOKE_DBOP(be, NSS_DBOP_ENDENT, 0);
1099 			contextp->be = 0;	/* Play it safe */
1100 		}
1101 	}
1102 	for (n_src = 0, be = 0; n_src < s->max_src &&
1103 	    (be = s->be[n_src]) == 0; n_src++) {
1104 		;
1105 	}
1106 
1107 	contextp->n_src	= n_src;
1108 	contextp->be	= be;
1109 
1110 	if (be == 0) {
1111 		/* Things are broken enough that we can't do setent/getent */
1112 		nss_endent_u(rootp, initf, contextpp);
1113 		return;
1114 	}
1115 
1116 	/*
1117 	 * make sure all the backends are supported
1118 	 */
1119 	for (i = 0; i < s->max_src; i++) {
1120 		int	st, srci;
1121 
1122 		if (s->be[i] == NULL)
1123 			continue;
1124 
1125 		srci = (*s->nsw_cfg_p)->src_idx[i];
1126 		st = _nscd_get_smf_state(srci, params.dbi, 1);
1127 		if (st == NSCD_SVC_STATE_UNSUPPORTED_SRC ||
1128 		    (st == NSCD_SVC_STATE_FOREIGN_SRC &&
1129 		    s->be_version_p[i] == NULL && initf == nscd_initf) ||
1130 		    st == NSCD_SVC_STATE_UNINITED ||
1131 		    (params.privdb &&
1132 		    try_local2(params.dbi, srci) == 1)) {
1133 			nss_endent_u(rootp, initf, contextpp);
1134 
1135 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
1136 			    NSCD_LOG_LEVEL_DEBUG)
1137 			(me, "backend (%s) not available (state = %d)\n",
1138 			    NSCD_NSW_SRC_NAME(srci), st);
1139 
1140 			return;
1141 		}
1142 	}
1143 
1144 	(void) NSS_INVOKE_DBOP(be, NSS_DBOP_SETENT, 0);
1145 }
1146 
1147 nss_status_t
1148 nss_getent_u(nss_db_root_t *rootp, nss_db_initf_t initf,
1149 	nss_getent_t *contextpp, void *args)
1150 {
1151 	char			*me = "nss_getent_u";
1152 	nscd_nsw_state_t	*s;
1153 	nscd_getent_context_t	*contextp;
1154 	int			n_src;
1155 	nss_backend_t		*be;
1156 
1157 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1158 	(me, "rootp = %p, initf = %p, contextpp = %p, args = %p\n",
1159 	    rootp, initf, contextpp, args);
1160 
1161 	if ((contextp = (nscd_getent_context_t *)contextpp->ctx) == 0) {
1162 		nss_setent_u(rootp, initf, contextpp);
1163 		if ((contextp = (nscd_getent_context_t *)contextpp->ctx) == 0) {
1164 			/* Give up */
1165 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
1166 			    NSCD_LOG_LEVEL_ERROR)
1167 			(me, "not able to obtain getent context ... give up\n");
1168 
1169 			return (NSS_UNAVAIL);
1170 		}
1171 	}
1172 
1173 	s	= contextp->nsw_state;
1174 	n_src	= contextp->n_src;
1175 	be	= contextp->be;
1176 
1177 	if (s == 0) {
1178 		/*
1179 		 * We've done an end_iter() and haven't done nss_setent()
1180 		 * or nss_endent() since;  we should stick in this state
1181 		 * until the caller invokes one of those two routines.
1182 		 */
1183 		return (NSS_SUCCESS);
1184 	}
1185 
1186 	while (n_src < s->max_src) {
1187 		nss_status_t		res;
1188 		struct __nsw_lookup_v1	*lkp = NULL;
1189 		int			n;
1190 
1191 		/* get the nsw config for the current source */
1192 		lkp = s->config->lookups;
1193 		for (n = 0; n < n_src; n++)
1194 			lkp = lkp->next;
1195 
1196 		if (be == 0) {
1197 			/* If it's null it's a bug, but let's play safe */
1198 			res = NSS_UNAVAIL;
1199 		} else {
1200 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
1201 			    NSCD_LOG_LEVEL_DEBUG)
1202 			(me, "database: %s, backend: %s, nsswitch config: %s\n",
1203 			    NSCD_NSW_DB_NAME(s->dbi),
1204 			    lkp->service_name,
1205 			    (*s->nsw_cfg_p)->nsw_cfg_str);
1206 
1207 			res = NSS_INVOKE_DBOP(be, NSS_DBOP_GETENT, args);
1208 		}
1209 
1210 		if (__NSW_ACTION_V1(lkp, res) == __NSW_RETURN) {
1211 			if (res != __NSW_SUCCESS) {
1212 				end_iter_u(rootp,
1213 				    (struct nss_getent_context *)contextp);
1214 			}
1215 			return (res);
1216 		}
1217 		(void) NSS_INVOKE_DBOP(be, NSS_DBOP_ENDENT, 0);
1218 		do {
1219 			n_src++;
1220 		} while (n_src < s->max_src &&
1221 		    (be = s->be[n_src]) == 0);
1222 		if (be == 0) {
1223 			/*
1224 			 * This is the case where we failed to get the backend
1225 			 * for the last source. We exhausted all sources.
1226 			 */
1227 			nss_endent_u(rootp, initf, contextpp);
1228 			return (NSS_NOTFOUND);
1229 		}
1230 		contextp->n_src	= n_src;
1231 		contextp->be	= be;
1232 		(void) NSS_INVOKE_DBOP(be, NSS_DBOP_SETENT, 0);
1233 	}
1234 	/* Got to the end of the sources without finding another entry */
1235 	end_iter_u(rootp, (struct nss_getent_context *)contextp);
1236 	return (NSS_SUCCESS);
1237 	/* success is either a successful entry or end of the sources */
1238 }
1239 
1240 /*ARGSUSED*/
1241 void
1242 nss_endent_u(nss_db_root_t *rootp, nss_db_initf_t initf,
1243 	nss_getent_t *contextpp)
1244 {
1245 	char			*me = "nss_endent_u";
1246 	nscd_getent_context_t	*contextp;
1247 
1248 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1249 	(me, "rootp = %p, initf = %p, contextpp = %p \n",
1250 	    rootp, initf, contextpp);
1251 
1252 	if ((contextp = (nscd_getent_context_t *)contextpp->ctx) == 0) {
1253 		/* nss_endent() on an unused context is a no-op */
1254 		return;
1255 	}
1256 
1257 	if (_nscd_is_getent_ctx_in_use(contextp) == 0) {
1258 		end_iter_u(rootp, (struct nss_getent_context *)contextp);
1259 		_nscd_put_getent_ctx(contextp);
1260 		contextpp->ctx = NULL;
1261 	}
1262 }
1263 
1264 /*
1265  * _nss_db_state_destr() and nss_delete() do nothing in nscd
1266  * but is needed to make the caller (below nscd) happy
1267  */
1268 /*ARGSUSED*/
1269 void
1270 _nss_db_state_destr(struct nss_db_state *s)
1271 {
1272 	/* nsw state in nscd is always reused, so do nothing here */
1273 }
1274 
1275 /*ARGSUSED*/
1276 void
1277 nss_delete(nss_db_root_t *rootp)
1278 {
1279 	/*
1280 	 * the only resource kept tracked by the nss_db_root_t
1281 	 * is the nsw state which is always reused and no need
1282 	 * to be freed. So just return.
1283 	 */
1284 }
1285 
1286 /*
1287  * Start of nss_psearch/nss_psetent()/nss_pgetent()/nss_pendent()
1288  * buffers switch entry points
1289  */
1290 
1291 /*
1292  * nss_psearch opens a packed structure header, assembles a local
1293  * nss_XbyY_args_t structure and calls the local copy of nss_search.
1294  * The return data is assembled in "files native format" in the
1295  * return buffer location.  Status if packed back up with the buffer
1296  * and the whole wad is returned to the cache or the client.
1297  */
1298 
1299 void
1300 nss_psearch(void *buffer, size_t length)
1301 {
1302 	/* inputs */
1303 	nss_db_initf_t		initf;
1304 	int			dbop;
1305 	int			rc;
1306 	nss_XbyY_args_t		arg;
1307 	nss_status_t		status;
1308 	nscd_sw_return_t	swret = { 0 }, *swrp = &swret;
1309 	nss_pheader_t		*pbuf = (nss_pheader_t *)buffer;
1310 	char			*me = "nss_psearch";
1311 
1312 	if (buffer == NULL || length == 0) {
1313 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1314 	}
1315 
1316 	status = nss_packed_arg_init(buffer, length,
1317 	    NULL, &initf, &dbop, &arg);
1318 	if (status != NSS_SUCCESS) {
1319 		NSCD_RETURN_STATUS(pbuf, status, -1);
1320 	}
1321 
1322 	/*
1323 	 * pass the address of the return data area
1324 	 * for the switch engine to return its own data
1325 	 */
1326 	(void) memcpy(&pbuf->nscdpriv, &swrp, sizeof (swrp));
1327 	swret.pbuf = buffer;
1328 	swret.pbufsiz = length;
1329 	swret.datalen = pbuf->data_len;
1330 
1331 	/*
1332 	 * use the generic nscd_initf for all database lookups
1333 	 * (the TSD key is the pointer to the packed header)
1334 	 */
1335 	rc = set_initf_key(pbuf);
1336 	if (rc != 0) {
1337 		NSCD_RETURN_STATUS(pbuf, NSS_UNAVAIL, EINVAL);
1338 	}
1339 	initf = nscd_initf;
1340 
1341 	/* Perform local search and pack results into return buffer */
1342 	/* nscd's search ignores db_root */
1343 	status = nss_search(NULL, initf, dbop, &arg);
1344 
1345 	/*
1346 	 * If status is NSS_NOTFOUND and ldap also returned
1347 	 * NSS_NOTFOUND, it is possible that the user does
1348 	 * not have a credential, so check and see if
1349 	 * needs to return NSS_ALTRETRY to let the main
1350 	 * nscd get a chance to process the lookup
1351 	 */
1352 	if (swret.fallback == 1 && status == NSS_NOTFOUND) {
1353 		OM_uint32	(*func)();
1354 		OM_uint32	stat;
1355 		nscd_rc_t	rc;
1356 
1357 		rc = get_gss_func((void **)&func);
1358 		if (rc == NSCD_SUCCESS) {
1359 			if (func(&stat, GSS_C_NO_CREDENTIAL,
1360 			    NULL, NULL, NULL, NULL) != GSS_S_COMPLETE) {
1361 
1362 				_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
1363 				    NSCD_LOG_LEVEL_DEBUG)
1364 			(me, "NSS_ALTRETRY: fallback to main nscd needed\n");
1365 
1366 				status = NSS_ALTRETRY;
1367 			}
1368 		}
1369 	}
1370 
1371 	NSCD_SET_STATUS(pbuf, status, -1);
1372 	errno = swret.errnum;
1373 
1374 	/*
1375 	 * Move result/status from args to packed buffer only if
1376 	 * arg was being used and rc from the switch engine is not
1377 	 * NSS_TRYLOCAL.
1378 	 */
1379 	if (!swret.noarg && status != NSS_TRYLOCAL)
1380 		nss_packed_set_status(buffer, length, status,  &arg);
1381 
1382 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1383 	(me, "switch engine result: source is %s, status %d, "
1384 	"herrno is %d, errno is %s\n",
1385 	    (swret.srci != -1) ? NSCD_NSW_SRC_NAME(swret.srci) : "<NOTSET>",
1386 	    pbuf->p_status, pbuf->p_herrno, strerror(pbuf->p_errno));
1387 
1388 	/* clear the TSD key used by the generic initf */
1389 	clear_initf_key();
1390 	pbuf->nscdpriv = 0;
1391 }
1392 
1393 static void
1394 nscd_map_contextp(void *buffer, nss_getent_t *contextp,
1395     nssuint_t **cookie_num_p, nssuint_t **seqnum_p, int setent)
1396 {
1397 	nss_pheader_t		*pbuf = (nss_pheader_t *)buffer;
1398 	nssuint_t		off;
1399 	nscd_getent_context_t	*ctx;
1400 	char			*me = "nscd_map_contextp";
1401 	nscd_getent_p1_cookie_t	*cookie;
1402 
1403 	if (buffer == NULL) {
1404 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1405 	}
1406 
1407 	off = pbuf->key_off;
1408 	cookie = (nscd_getent_p1_cookie_t *)((void *)((char *)buffer + off));
1409 	if (seqnum_p != NULL)
1410 		*seqnum_p = &cookie->p1_seqnum;
1411 
1412 	/*
1413 	 * if called by nss_psetent, and the passed in cookie number
1414 	 * is NSCD_NEW_COOKIE, then there is no cookie yet, return a
1415 	 * pointer pointing to where the cookie number will be stored.
1416 	 * Also because there is no cookie to validate, just return
1417 	 * success.
1418 	 *
1419 	 * On the other hand, if a cookie number is passed in, we need
1420 	 * to validate the cookie number before returning.
1421 	 */
1422 	if (cookie_num_p != NULL)
1423 		*cookie_num_p = &cookie->p1_cookie_num;
1424 	if (setent == 1 && cookie->p1_cookie_num == NSCD_NEW_COOKIE) {
1425 			NSCD_RETURN_STATUS_SUCCESS(pbuf);
1426 	}
1427 
1428 	/*
1429 	 * If the sequence number and start time match nscd's p0 cookie,
1430 	 * then either setent was done twice in a row or this is the
1431 	 * first getent after the setent, return success as well.
1432 	 */
1433 	if (cookie->p1_seqnum == NSCD_P0_COOKIE_SEQNUM) {
1434 		nscd_getent_p0_cookie_t *p0c =
1435 		    (nscd_getent_p0_cookie_t *)cookie;
1436 		if (p0c->p0_time == _nscd_get_start_time())
1437 			NSCD_RETURN_STATUS_SUCCESS(pbuf);
1438 	}
1439 
1440 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1441 	(me, "cookie # = %lld,  sequence # = %lld\n",
1442 	    cookie->p1_cookie_num, cookie->p1_seqnum);
1443 
1444 	ctx = _nscd_is_getent_ctx(cookie->p1_cookie_num);
1445 
1446 	if (ctx == NULL) {
1447 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1448 		(me, "No matching context found (cookie number: %lld)\n",
1449 		    cookie->p1_cookie_num);
1450 
1451 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1452 	}
1453 
1454 	/* if not called by nss_psetent, verify sequence number */
1455 	if (setent != 1 && ctx->seq_num !=
1456 	    (nscd_seq_num_t)cookie->p1_seqnum) {
1457 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1458 		(me, "invalid sequence # (%lld)\n", cookie->p1_seqnum);
1459 
1460 		_nscd_free_ctx_if_aborted(ctx);
1461 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1462 	}
1463 
1464 	contextp->ctx = (struct nss_getent_context *)ctx;
1465 
1466 	NSCD_RETURN_STATUS_SUCCESS(pbuf);
1467 }
1468 
1469 void
1470 nss_psetent(void *buffer, size_t length, pid_t pid)
1471 {
1472 	nss_getent_t		context = { 0 };
1473 	nss_getent_t		*contextp = &context;
1474 	nssuint_t		*cookie_num_p;
1475 	nssuint_t		*seqnum_p;
1476 	nss_pheader_t		*pbuf = (nss_pheader_t *)buffer;
1477 	nscd_getent_p0_cookie_t *p0c;
1478 	char			*me = "nss_psetent";
1479 
1480 	if (buffer == NULL || length == 0) {
1481 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1482 	}
1483 
1484 	/*
1485 	 * If this is a per-user nscd, and the user does not have
1486 	 * the necessary credential, return NSS_TRYLOCAL, so the
1487 	 * setent/getent can be done locally in the process of the
1488 	 * setent call
1489 	 */
1490 	if (_whoami == NSCD_CHILD) {
1491 		OM_uint32	(*func)();
1492 		OM_uint32	stat;
1493 		nscd_rc_t	rc;
1494 
1495 		rc = get_gss_func((void **)&func);
1496 		if (rc == NSCD_SUCCESS) {
1497 			if (func(&stat, GSS_C_NO_CREDENTIAL,
1498 			    NULL, NULL, NULL, NULL) != GSS_S_COMPLETE) {
1499 
1500 				_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
1501 				    NSCD_LOG_LEVEL_DEBUG)
1502 			(me, "NSS_TRYLOCAL: fallback to caller process\n");
1503 				NSCD_RETURN_STATUS(pbuf, NSS_TRYLOCAL, 0);
1504 			}
1505 		}
1506 	}
1507 
1508 	/* check cookie number */
1509 	nscd_map_contextp(buffer, contextp, &cookie_num_p, &seqnum_p, 1);
1510 	if (NSCD_STATUS_IS_NOT_OK(pbuf))
1511 		return;
1512 
1513 	/* set cookie number and sequence number */
1514 	p0c = (nscd_getent_p0_cookie_t *)cookie_num_p;
1515 	if (contextp->ctx ==  NULL) {
1516 		/*
1517 		 * first setent (no existing getent context),
1518 		 * return a p0 cookie
1519 		 */
1520 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1521 		(me, "first setent, no getent context yet\n");
1522 	} else {
1523 		/*
1524 		 * doing setent on an existing getent context,
1525 		 * release resources allocated and return a
1526 		 * p0 cookie
1527 		 */
1528 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1529 		(me, "setent resetting sequence number = %lld\n",  *seqnum_p);
1530 
1531 		if (_nscd_is_getent_ctx_in_use((nscd_getent_context_t *)
1532 		    contextp->ctx) == 0) {
1533 			/*
1534 			 * context not in use, release the backend and
1535 			 * return the context to the pool
1536 			 */
1537 			end_iter_u(NULL, contextp->ctx);
1538 			_nscd_put_getent_ctx(
1539 			    (nscd_getent_context_t *)contextp->ctx);
1540 			contextp->ctx = NULL;
1541 		}
1542 	}
1543 
1544 	p0c->p0_pid = pid;
1545 	p0c->p0_time = _nscd_get_start_time();
1546 	p0c->p0_seqnum = NSCD_P0_COOKIE_SEQNUM;
1547 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1548 	(me, "returning a p0 cookie: pid = %ld, time = %ld, seq #= %llx\n",
1549 	    p0c->p0_pid, p0c->p0_time, p0c->p0_seqnum);
1550 
1551 	NSCD_RETURN_STATUS(pbuf, NSS_SUCCESS, 0);
1552 }
1553 
1554 static void
1555 delayed_setent(nss_pheader_t *pbuf, nss_db_initf_t initf,
1556 	nss_getent_t *contextp, nssuint_t *cookie_num_p,
1557 	nssuint_t *seqnum_p, pid_t pid)
1558 {
1559 	nscd_getent_context_t	*ctx;
1560 	nscd_sw_return_t	swret = { 0 }, *swrp = &swret;
1561 	char			*me = "delayed_setent";
1562 
1563 	/*
1564 	 * check credential
1565 	 */
1566 	_nscd_APP_check_cred(pbuf, &pid, "NSCD_DELAYED_SETENT",
1567 	    NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_ERROR);
1568 	if (NSCD_STATUS_IS_NOT_OK(pbuf)) {
1569 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1570 		(me, "invalid credential\n");
1571 		return;
1572 	}
1573 
1574 	/*
1575 	 * pass the packed header buffer pointer to nss_setent
1576 	 */
1577 	(void) memcpy(&pbuf->nscdpriv, &swrp, sizeof (swrp));
1578 	swret.pbuf = pbuf;
1579 
1580 	/* Perform local setent and set context */
1581 	nss_setent(NULL, initf, contextp);
1582 
1583 	/* insert cookie info into packed buffer header */
1584 	ctx = (nscd_getent_context_t *)contextp->ctx;
1585 	if (ctx != NULL) {
1586 		*cookie_num_p = ctx->cookie_num;
1587 		*seqnum_p = ctx->seq_num;
1588 		ctx->pid = pid;
1589 	} else {
1590 		/*
1591 		 * not able to allocate a getent context, the
1592 		 * client should try the enumeration locally
1593 		 */
1594 		*cookie_num_p = NSCD_LOCAL_COOKIE;
1595 		*seqnum_p = 0;
1596 
1597 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1598 		(me, "NSS_TRYLOCAL: cookie # = %lld,  sequence # = %lld\n",
1599 		    *cookie_num_p, *seqnum_p);
1600 		NSCD_RETURN_STATUS(pbuf, NSS_TRYLOCAL, 0);
1601 	}
1602 
1603 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1604 	(me, "NSS_SUCCESS: cookie # = %lld,  sequence # = %lld\n",
1605 	    ctx->cookie_num, ctx->seq_num);
1606 
1607 	NSCD_RETURN_STATUS(pbuf, NSS_SUCCESS, 0);
1608 }
1609 
1610 void
1611 nss_pgetent(void *buffer, size_t length)
1612 {
1613 	/* inputs */
1614 	nss_db_initf_t		initf;
1615 	nss_getent_t		context = { 0 };
1616 	nss_getent_t		*contextp = &context;
1617 	nss_XbyY_args_t		arg = { 0};
1618 	nss_status_t		status;
1619 	nssuint_t		*cookie_num_p;
1620 	nssuint_t		*seqnum_p;
1621 	nscd_getent_context_t	*ctx;
1622 	int			rc;
1623 	nss_pheader_t		*pbuf = (nss_pheader_t *)buffer;
1624 	char			*me = "nss_pgetent";
1625 
1626 	if (buffer == NULL || length == 0) {
1627 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1628 	}
1629 
1630 	/* verify the cookie passed in */
1631 	nscd_map_contextp(buffer, contextp, &cookie_num_p, &seqnum_p, 0);
1632 	if (NSCD_STATUS_IS_NOT_OK(pbuf))
1633 		return;
1634 
1635 	/*
1636 	 * use the generic nscd_initf for all the getent requests
1637 	 * (the TSD key is the pointer to the packed header)
1638 	 */
1639 	rc = set_initf_key(pbuf);
1640 	if (rc != 0) {
1641 		NSCD_RETURN_STATUS(pbuf, NSS_UNAVAIL, EINVAL);
1642 	}
1643 	initf = nscd_initf;
1644 
1645 	/* if no context yet, get one */
1646 	if (contextp->ctx ==  NULL) {
1647 		nscd_getent_p0_cookie_t *p0c =
1648 		    (nscd_getent_p0_cookie_t *)cookie_num_p;
1649 
1650 		delayed_setent(pbuf, initf, contextp, cookie_num_p,
1651 		    seqnum_p, p0c->p0_pid);
1652 		if (NSCD_STATUS_IS_NOT_OK(pbuf)) {
1653 			clear_initf_key();
1654 			return;
1655 		}
1656 	}
1657 
1658 	status = nss_packed_context_init(buffer, length,
1659 	    NULL, &initf, &contextp, &arg);
1660 	if (status != NSS_SUCCESS) {
1661 		clear_initf_key();
1662 		_nscd_free_ctx_if_aborted(
1663 		    (nscd_getent_context_t *)contextp->ctx);
1664 		NSCD_RETURN_STATUS(pbuf, status, -1);
1665 	}
1666 
1667 	/* Perform local search and pack results into return buffer */
1668 	status = nss_getent(NULL, initf, contextp, &arg);
1669 	NSCD_SET_STATUS(pbuf, status, -1);
1670 	nss_packed_set_status(buffer, length, status,  &arg);
1671 
1672 	/* increment sequence number in the buffer and nscd context */
1673 	if (status == NSS_SUCCESS) {
1674 		ctx = (nscd_getent_context_t *)contextp->ctx;
1675 		ctx->seq_num++;
1676 		*seqnum_p = ctx->seq_num;
1677 		*cookie_num_p = ctx->cookie_num;
1678 
1679 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1680 		(me, "getent OK, new sequence # = %lld, len = %lld,"
1681 		    " data = >>%s<<\n", *seqnum_p,
1682 		    pbuf->data_len, (char *)buffer + pbuf->data_off);
1683 
1684 		_nscd_free_ctx_if_aborted(ctx);
1685 	} else {
1686 		/* release the resources used */
1687 		ctx = (nscd_getent_context_t *)contextp->ctx;
1688 		if (ctx != NULL && _nscd_is_getent_ctx_in_use(ctx) == 0) {
1689 			_nscd_put_getent_ctx(ctx);
1690 			contextp->ctx = NULL;
1691 		}
1692 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1693 		(me, "getent failed, status = %d, sequence # = %lld\n",
1694 		    status, *seqnum_p);
1695 	}
1696 
1697 	/* clear the TSD key used by the generic initf */
1698 	clear_initf_key();
1699 }
1700 
1701 void
1702 nss_pendent(void *buffer, size_t length)
1703 {
1704 	nss_getent_t		context = { 0 };
1705 	nss_getent_t		*contextp = &context;
1706 	nssuint_t		*seqnum_p;
1707 	nssuint_t		*cookie_num_p;
1708 	nss_pheader_t		*pbuf = (nss_pheader_t *)buffer;
1709 	char			*me = "nss_pendent";
1710 
1711 	if (buffer == NULL || length == 0) {
1712 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1713 	}
1714 
1715 	/* map the contextp from the cookie information */
1716 	nscd_map_contextp(buffer, contextp, &cookie_num_p, &seqnum_p, 0);
1717 	if (NSCD_STATUS_IS_NOT_OK(pbuf))
1718 		return;
1719 
1720 	if (contextp->ctx == NULL)
1721 		return;
1722 
1723 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1724 	(me, "endent, cookie = %lld, sequence # = %lld\n",
1725 	    *cookie_num_p, *seqnum_p);
1726 
1727 	/* Perform local endent and reset context */
1728 	nss_endent(NULL, NULL, contextp);
1729 
1730 	NSCD_RETURN_STATUS(pbuf, NSS_SUCCESS, 0);
1731 }
1732 
1733 /*ARGSUSED*/
1734 void
1735 nss_pdelete(void *buffer, size_t length)
1736 {
1737 	nss_pheader_t	*pbuf = (nss_pheader_t *)buffer;
1738 
1739 	/* unnecessary, kept for completeness */
1740 	NSCD_RETURN_STATUS_SUCCESS(pbuf);
1741 }
1742