xref: /freebsd/lib/libc/net/nsdispatch.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*	$NetBSD: nsdispatch.c,v 1.9 1999/01/25 00:16:17 lukem Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Luke Mewburn.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*-
34  * Copyright (c) 2003 Networks Associates Technology, Inc.
35  * All rights reserved.
36  *
37  * Portions of this software were developed for the FreeBSD Project by
38  * Jacques A. Vidrine, Safeport Network Services, and Network
39  * Associates Laboratories, the Security Research Division of Network
40  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
41  * ("CBOSS"), as part of the DARPA CHATS research program.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  */
65 #include <sys/cdefs.h>
66 #include "namespace.h"
67 #include <sys/param.h>
68 #include <sys/stat.h>
69 
70 #include <dlfcn.h>
71 #include <errno.h>
72 #include <fcntl.h>
73 #define _NS_PRIVATE
74 #include <nsswitch.h>
75 #include <pthread.h>
76 #include <pthread_np.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <syslog.h>
81 #include <unistd.h>
82 #include "un-namespace.h"
83 #include "nss_tls.h"
84 #include "libc_private.h"
85 #ifdef NS_CACHING
86 #include "nscache.h"
87 #endif
88 
89 enum _nss_constants {
90 	/* Number of elements allocated when we grow a vector */
91 	ELEMSPERCHUNK =	8
92 };
93 
94 /*
95  * Global NSS data structures are mostly read-only, but we update
96  * them when we read or re-read the nsswitch.conf.
97  */
98 static	pthread_rwlock_t	nss_lock = PTHREAD_RWLOCK_INITIALIZER;
99 
100 /*
101  * Runtime determination of whether we are dynamically linked or not.
102  */
103 extern	int		_DYNAMIC __attribute__ ((weak));
104 #define	is_dynamic()	(&_DYNAMIC != NULL)
105 
106 /*
107  * default sourcelist: `files'
108  */
109 const ns_src __nsdefaultsrc[] = {
110 	{ NSSRC_FILES, NS_SUCCESS },
111 	{ 0 },
112 };
113 
114 /* Database, source mappings. */
115 static	unsigned int		 _nsmapsize;
116 static	ns_dbt			*_nsmap = NULL;
117 
118 /* NSS modules. */
119 static	unsigned int		 _nsmodsize;
120 static	ns_mod			*_nsmod;
121 
122 /* Placeholder for builtin modules' dlopen `handle'. */
123 static	int			 __nss_builtin_handle;
124 static	void			*nss_builtin_handle = &__nss_builtin_handle;
125 
126 #ifdef NS_CACHING
127 /*
128  * Cache lookup cycle prevention function - if !NULL then no cache lookups
129  * will be made
130  */
131 static	void			*nss_cache_cycle_prevention_func = NULL;
132 #endif
133 
134 /*
135  * We keep track of nsdispatch() nesting depth in dispatch_depth.  When a
136  * fallback method is invoked from nsdispatch(), we temporarily set
137  * fallback_depth to the current dispatch depth plus one.  Subsequent
138  * calls at that exact depth will run in fallback mode (restricted to the
139  * same source as the call that was handled by the fallback method), while
140  * calls below that depth will be handled normally, allowing fallback
141  * methods to perform arbitrary lookups.
142  */
143 struct fb_state {
144 	int	dispatch_depth;
145 	int	fallback_depth;
146 };
147 static	void	fb_endstate(void *);
148 NSS_TLS_HANDLING(fb);
149 
150 /*
151  * Attempt to spew relatively uniform messages to syslog.
152  */
153 #define nss_log(level, fmt, ...) \
154 	syslog((level), "NSSWITCH(%s): " fmt, __func__, __VA_ARGS__)
155 #define nss_log_simple(level, s) \
156 	syslog((level), "NSSWITCH(%s): " s, __func__)
157 
158 /*
159  * Dynamically growable arrays are used for lists of databases, sources,
160  * and modules.  The following `vector' interface is used to isolate the
161  * common operations.
162  */
163 typedef	int	(*vector_comparison)(const void *, const void *);
164 typedef	void	(*vector_free_elem)(void *);
165 static	void	  vector_sort(void *, unsigned int, size_t,
166 		    vector_comparison);
167 static	void	  vector_free(void *, unsigned int *, size_t,
168 		    vector_free_elem);
169 static	void	 *vector_ref(unsigned int, void *, unsigned int, size_t);
170 static	void	 *vector_search(const void *, void *, unsigned int, size_t,
171 		    vector_comparison);
172 static	void	 *vector_append(const void *, void *, unsigned int *, size_t);
173 
174 
175 /*
176  * Internal interfaces.
177  */
178 static	int	 string_compare(const void *, const void *);
179 static	int	 mtab_compare(const void *, const void *);
180 static	int	 nss_configure(void);
181 static	void	 ns_dbt_free(ns_dbt *);
182 static	void	 ns_mod_free(ns_mod *);
183 static	void	 ns_src_free(ns_src **, int);
184 static	void	 nss_load_builtin_modules(void);
185 static	void	 nss_load_module(const char *, nss_module_register_fn);
186 static	void	 nss_atexit(void);
187 /* nsparser */
188 extern	FILE	*_nsyyin;
189 
190 
191 /*
192  * The vector operations
193  */
194 static void
195 vector_sort(void *vec, unsigned int count, size_t esize,
196     vector_comparison comparison)
197 {
198 	qsort(vec, count, esize, comparison);
199 }
200 
201 
202 static void *
203 vector_search(const void *key, void *vec, unsigned int count, size_t esize,
204     vector_comparison comparison)
205 {
206 	return (bsearch(key, vec, count, esize, comparison));
207 }
208 
209 
210 static void *
211 vector_append(const void *elem, void *vec, unsigned int *count, size_t esize)
212 {
213 	void	*p;
214 
215 	if ((*count % ELEMSPERCHUNK) == 0) {
216 		p = reallocarray(vec, *count + ELEMSPERCHUNK, esize);
217 		if (p == NULL) {
218 			nss_log_simple(LOG_ERR, "memory allocation failure");
219 			return (vec);
220 		}
221 		vec = p;
222 	}
223 	memmove((void *)(((uintptr_t)vec) + (*count * esize)), elem, esize);
224 	(*count)++;
225 	return (vec);
226 }
227 
228 
229 static void *
230 vector_ref(unsigned int i, void *vec, unsigned int count, size_t esize)
231 {
232 	if (i < count)
233 		return (void *)((uintptr_t)vec + (i * esize));
234 	else
235 		return (NULL);
236 }
237 
238 
239 #define VECTOR_FREE(v, c, s, f) \
240 	do { vector_free(v, c, s, f); v = NULL; } while (0)
241 static void
242 vector_free(void *vec, unsigned int *count, size_t esize,
243     vector_free_elem free_elem)
244 {
245 	unsigned int	 i;
246 	void		*elem;
247 
248 	for (i = 0; i < *count; i++) {
249 		elem = vector_ref(i, vec, *count, esize);
250 		if (elem != NULL)
251 			free_elem(elem);
252 	}
253 	free(vec);
254 	*count = 0;
255 }
256 
257 /*
258  * Comparison functions for vector_search.
259  */
260 static int
261 string_compare(const void *a, const void *b)
262 {
263       return (strcasecmp(*(const char * const *)a, *(const char * const *)b));
264 }
265 
266 
267 static int
268 mtab_compare(const void *a, const void *b)
269 {
270       int     cmp;
271 
272       cmp = strcmp(((const ns_mtab *)a)->name, ((const ns_mtab *)b)->name);
273       if (cmp != 0)
274 	      return (cmp);
275       else
276 	      return (strcmp(((const ns_mtab *)a)->database,
277 		  ((const ns_mtab *)b)->database));
278 }
279 
280 /*
281  * NSS nsmap management.
282  */
283 void
284 _nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
285 {
286 	const ns_mod	*modp;
287 
288 	dbt->srclist = vector_append(src, dbt->srclist, &dbt->srclistsize,
289 	    sizeof(*src));
290 	modp = vector_search(&src->name, _nsmod, _nsmodsize, sizeof(*_nsmod),
291 	    string_compare);
292 	if (modp == NULL)
293 		nss_load_module(src->name, NULL);
294 }
295 
296 
297 #ifdef _NSS_DEBUG
298 void
299 _nsdbtdump(const ns_dbt *dbt)
300 {
301 	int i;
302 
303 	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
304 	    dbt->srclistsize == 1 ? "" : "s");
305 	for (i = 0; i < (int)dbt->srclistsize; i++) {
306 		printf(" %s", dbt->srclist[i].name);
307 		if (!(dbt->srclist[i].flags &
308 		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
309 		    (dbt->srclist[i].flags & NS_SUCCESS))
310 			continue;
311 		printf(" [");
312 		if (!(dbt->srclist[i].flags & NS_SUCCESS))
313 			printf(" SUCCESS=continue");
314 		if (dbt->srclist[i].flags & NS_UNAVAIL)
315 			printf(" UNAVAIL=return");
316 		if (dbt->srclist[i].flags & NS_NOTFOUND)
317 			printf(" NOTFOUND=return");
318 		if (dbt->srclist[i].flags & NS_TRYAGAIN)
319 			printf(" TRYAGAIN=return");
320 		printf(" ]");
321 	}
322 	printf("\n");
323 }
324 #endif
325 
326 
327 /*
328  * The first time nsdispatch is called (during a process's lifetime,
329  * or after nsswitch.conf has been updated), nss_configure will
330  * prepare global data needed by NSS.
331  */
332 static int
333 nss_configure(void)
334 {
335 	static time_t	 confmod;
336 #ifndef NS_REREAD_CONF
337 	static int	 already_initialized = 0;
338 #endif
339 	struct stat	 statbuf;
340 	int		 result, isthreaded;
341 	const char	*path;
342 #ifdef NS_CACHING
343 	void		*handle;
344 #endif
345 
346 	result = 0;
347 	isthreaded = __isthreaded;
348 #if defined(_NSS_DEBUG) && defined(_NSS_SHOOT_FOOT)
349 	/* NOTE WELL:  THIS IS A SECURITY HOLE. This must only be built
350 	 * for debugging purposes and MUST NEVER be used in production.
351 	 */
352 	path = getenv("NSSWITCH_CONF");
353 	if (path == NULL)
354 #endif
355 		path = _PATH_NS_CONF;
356 #ifndef NS_REREAD_CONF
357 	/*
358 	 * Define NS_REREAD_CONF to have nsswitch notice changes
359 	 * to nsswitch.conf(5) during runtime.  This involves calling
360 	 * stat(2) every time, which can result in performance hit.
361 	 */
362 	if (already_initialized)
363 		return (0);
364 	already_initialized = 1;
365 #endif /* NS_REREAD_CONF */
366 	if (stat(path, &statbuf) != 0)
367 		return (0);
368 	if (statbuf.st_mtime <= confmod)
369 		return (0);
370 	if (isthreaded) {
371 		(void)_pthread_rwlock_unlock(&nss_lock);
372 		result = _pthread_rwlock_wrlock(&nss_lock);
373 		if (result != 0)
374 			return (result);
375 		if (stat(path, &statbuf) != 0)
376 			goto fin;
377 		if (statbuf.st_mtime <= confmod)
378 			goto fin;
379 	}
380 	_nsyyin = fopen(path, "re");
381 	if (_nsyyin == NULL)
382 		goto fin;
383 	VECTOR_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
384 	    (vector_free_elem)ns_dbt_free);
385 	VECTOR_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
386 	    (vector_free_elem)ns_mod_free);
387 	if (confmod == 0)
388 		(void)atexit(nss_atexit);
389 	nss_load_builtin_modules();
390 	_nsyyparse();
391 	(void)fclose(_nsyyin);
392 	vector_sort(_nsmap, _nsmapsize, sizeof(*_nsmap), string_compare);
393 	confmod = statbuf.st_mtime;
394 
395 #ifdef NS_CACHING
396 	handle = libc_dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
397 	if (handle != NULL) {
398 		nss_cache_cycle_prevention_func = dlsym(handle,
399 		    "_nss_cache_cycle_prevention_function");
400 		dlclose(handle);
401 	}
402 #endif
403 fin:
404 	if (isthreaded) {
405 		(void)_pthread_rwlock_unlock(&nss_lock);
406 		if (result == 0)
407 			result = _pthread_rwlock_rdlock(&nss_lock);
408 	}
409 	return (result);
410 }
411 
412 
413 void
414 _nsdbtput(const ns_dbt *dbt)
415 {
416 	unsigned int	 i;
417 	ns_dbt		*p;
418 
419 	for (i = 0; i < _nsmapsize; i++) {
420 		p = vector_ref(i, _nsmap, _nsmapsize, sizeof(*_nsmap));
421 		if (string_compare(&dbt->name, &p->name) == 0) {
422 			/* overwrite existing entry */
423 			if (p->srclist != NULL)
424 				ns_src_free(&p->srclist, p->srclistsize);
425 			memmove(p, dbt, sizeof(*dbt));
426 			return;
427 		}
428 	}
429 	_nsmap = vector_append(dbt, _nsmap, &_nsmapsize, sizeof(*_nsmap));
430 }
431 
432 
433 static void
434 ns_dbt_free(ns_dbt *dbt)
435 {
436 	ns_src_free(&dbt->srclist, dbt->srclistsize);
437 	if (dbt->name)
438 		free((void *)dbt->name);
439 }
440 
441 
442 static void
443 ns_src_free(ns_src **src, int srclistsize)
444 {
445 	int	i;
446 
447 	for (i = 0; i < srclistsize; i++)
448 		if ((*src)[i].name != NULL)
449 			/* This one was allocated by nslexer. You'll just
450 			 * have to trust me.
451 			 */
452 			free((void *)((*src)[i].name));
453 	free(*src);
454 	*src = NULL;
455 }
456 
457 
458 
459 /*
460  * NSS module management.
461  */
462 /* The built-in NSS modules are all loaded at once. */
463 #define NSS_BACKEND(name, reg) \
464 ns_mtab	*reg(unsigned int *, nss_module_unregister_fn *);
465 #include "nss_backends.h"
466 #undef NSS_BACKEND
467 
468 static void
469 nss_load_builtin_modules(void)
470 {
471 #define NSS_BACKEND(name, reg) nss_load_module(#name, reg);
472 #include "nss_backends.h"
473 #undef NSS_BACKEND
474 }
475 
476 
477 /* Load a built-in or dynamically linked module.  If the `reg_fn'
478  * argument is non-NULL, assume a built-in module and use reg_fn to
479  * register it.  Otherwise, search for a dynamic NSS module.
480  */
481 static void
482 nss_load_module(const char *source, nss_module_register_fn reg_fn)
483 {
484 	char		 buf[PATH_MAX];
485 	ns_mod		 mod;
486 	nss_module_register_fn fn;
487 
488 	memset(&mod, 0, sizeof(mod));
489 	mod.name = strdup(source);
490 	if (mod.name == NULL) {
491 		nss_log_simple(LOG_ERR, "memory allocation failure");
492 		return;
493 	}
494 	if (reg_fn != NULL) {
495 		/* The placeholder is required, as a NULL handle
496 		 * represents an invalid module.
497 		 */
498 		mod.handle = nss_builtin_handle;
499 		fn = reg_fn;
500 	} else if (!is_dynamic()) {
501 		goto fin;
502 	} else if (strcmp(source, NSSRC_CACHE) == 0 ||
503 	    strcmp(source, NSSRC_COMPAT) == 0 ||
504 	    strcmp(source, NSSRC_DB) == 0 ||
505 	    strcmp(source, NSSRC_DNS) == 0 ||
506 	    strcmp(source, NSSRC_FILES) == 0 ||
507 	    strcmp(source, NSSRC_NIS) == 0) {
508 		/*
509 		 * Avoid calling dlopen(3) for built-in modules.
510 		 */
511 		goto fin;
512 	} else {
513 		if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name,
514 		    NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf))
515 			goto fin;
516 		mod.handle = libc_dlopen(buf, RTLD_LOCAL|RTLD_LAZY);
517 		if (mod.handle == NULL) {
518 #ifdef _NSS_DEBUG
519 			/* This gets pretty annoying since the built-in
520 			 * sources aren't modules yet.
521 			 */
522 			nss_log(LOG_DEBUG, "%s, %s", mod.name, dlerror());
523 #endif
524 			goto fin;
525 		}
526 		fn = (nss_module_register_fn)dlfunc(mod.handle,
527 		    "nss_module_register");
528 		if (fn == NULL) {
529 			(void)dlclose(mod.handle);
530 			mod.handle = NULL;
531 			nss_log(LOG_ERR, "%s, %s", mod.name, dlerror());
532 			goto fin;
533 		}
534 	}
535 	mod.mtab = fn(mod.name, &mod.mtabsize, &mod.unregister);
536 	if (mod.mtab == NULL || mod.mtabsize == 0) {
537 		if (mod.handle != nss_builtin_handle)
538 			(void)dlclose(mod.handle);
539 		mod.handle = NULL;
540 		nss_log(LOG_ERR, "%s, registration failed", mod.name);
541 		goto fin;
542 	}
543 	if (mod.mtabsize > 1)
544 		qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]),
545 		    mtab_compare);
546 fin:
547 	_nsmod = vector_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod));
548 	vector_sort(_nsmod, _nsmodsize, sizeof(*_nsmod), string_compare);
549 }
550 
551 static int exiting = 0;
552 
553 static void
554 ns_mod_free(ns_mod *mod)
555 {
556 
557 	free(mod->name);
558 	if (mod->handle == NULL)
559 		return;
560 	if (mod->unregister != NULL)
561 		mod->unregister(mod->mtab, mod->mtabsize);
562 	if (mod->handle != nss_builtin_handle && !exiting)
563 		(void)dlclose(mod->handle);
564 }
565 
566 /*
567  * Cleanup
568  */
569 static void
570 nss_atexit(void)
571 {
572 	int isthreaded;
573 
574 	exiting = 1;
575 	isthreaded = __isthreaded;
576 	if (isthreaded)
577 		(void)_pthread_rwlock_wrlock(&nss_lock);
578 	VECTOR_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
579 	    (vector_free_elem)ns_dbt_free);
580 	VECTOR_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
581 	    (vector_free_elem)ns_mod_free);
582 	if (isthreaded)
583 		(void)_pthread_rwlock_unlock(&nss_lock);
584 }
585 
586 /*
587  * Finally, the actual implementation.
588  */
589 static nss_method
590 nss_method_lookup(const char *source, const char *database,
591     const char *method, const ns_dtab disp_tab[], void **mdata)
592 {
593 	ns_mod	*mod;
594 	ns_mtab	*match, key;
595 	int	 i;
596 
597 	if (disp_tab != NULL)
598 		for (i = 0; disp_tab[i].src != NULL; i++)
599 			if (strcasecmp(source, disp_tab[i].src) == 0) {
600 				*mdata = disp_tab[i].mdata;
601 				return (disp_tab[i].method);
602 			}
603 	mod = vector_search(&source, _nsmod, _nsmodsize, sizeof(*_nsmod),
604 	    string_compare);
605 	if (mod != NULL && mod->handle != NULL) {
606 		key.database = database;
607 		key.name = method;
608 		match = bsearch(&key, mod->mtab, mod->mtabsize,
609 		    sizeof(mod->mtab[0]), mtab_compare);
610 		if (match != NULL) {
611 			*mdata = match->mdata;
612 			return (match->method);
613 		}
614 	}
615 
616 	*mdata = NULL;
617 	return (NULL);
618 }
619 
620 static void
621 fb_endstate(void *p)
622 {
623 	free(p);
624 }
625 
626 __weak_reference(_nsdispatch, nsdispatch);
627 
628 int
629 _nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
630 	    const char *method_name, const ns_src defaults[], ...)
631 {
632 	va_list		 ap;
633 	const ns_dbt	*dbt;
634 	const ns_src	*srclist;
635 	nss_method	 method, fb_method;
636 	void		*mdata;
637 	int		 isthreaded, serrno, i, result, srclistsize;
638 	struct fb_state	*st;
639 	int		 saved_depth;
640 
641 #ifdef NS_CACHING
642 	nss_cache_data	 cache_data;
643 	nss_cache_data	*cache_data_p;
644 	int		 cache_flag;
645 #endif
646 
647 	dbt = NULL;
648 	fb_method = NULL;
649 
650 	isthreaded = __isthreaded;
651 	serrno = errno;
652 	if (isthreaded) {
653 		result = _pthread_rwlock_rdlock(&nss_lock);
654 		if (result != 0) {
655 			result = NS_UNAVAIL;
656 			goto fin;
657 		}
658 	}
659 
660 	result = fb_getstate(&st);
661 	if (result != 0) {
662 		result = NS_UNAVAIL;
663 		goto fin;
664 	}
665 
666 	result = nss_configure();
667 	if (result != 0) {
668 		result = NS_UNAVAIL;
669 		goto fin;
670 	}
671 	++st->dispatch_depth;
672 	if (st->dispatch_depth > st->fallback_depth) {
673 		dbt = vector_search(&database, _nsmap, _nsmapsize, sizeof(*_nsmap),
674 		    string_compare);
675 		fb_method = nss_method_lookup(NSSRC_FALLBACK, database,
676 		    method_name, disp_tab, &mdata);
677 	}
678 
679 	if (dbt != NULL) {
680 		srclist = dbt->srclist;
681 		srclistsize = dbt->srclistsize;
682 	} else {
683 		srclist = defaults;
684 		srclistsize = 0;
685 		while (srclist[srclistsize].name != NULL)
686 			srclistsize++;
687 	}
688 
689 #ifdef NS_CACHING
690 	cache_data_p = NULL;
691 	cache_flag = 0;
692 #endif
693 	for (i = 0; i < srclistsize; i++) {
694 		result = NS_NOTFOUND;
695 		method = nss_method_lookup(srclist[i].name, database,
696 		    method_name, disp_tab, &mdata);
697 
698 		if (method != NULL) {
699 #ifdef NS_CACHING
700 			if (strcmp(srclist[i].name, NSSRC_CACHE) == 0 &&
701 			    nss_cache_cycle_prevention_func == NULL) {
702 #ifdef NS_STRICT_LIBC_EID_CHECKING
703 				if (issetugid() != 0)
704 					continue;
705 #endif
706 				cache_flag = 1;
707 
708 				memset(&cache_data, 0, sizeof(nss_cache_data));
709 				cache_data.info = (nss_cache_info const *)mdata;
710 				cache_data_p = &cache_data;
711 
712 				va_start(ap, defaults);
713 				if (cache_data.info->id_func != NULL)
714 					result = __nss_common_cache_read(retval,
715 					    cache_data_p, ap);
716 				else if (cache_data.info->marshal_func != NULL)
717 					result = __nss_mp_cache_read(retval,
718 					    cache_data_p, ap);
719 				else
720 					result = __nss_mp_cache_end(retval,
721 					    cache_data_p, ap);
722 				va_end(ap);
723 			} else {
724 				cache_flag = 0;
725 				errno = 0;
726 				va_start(ap, defaults);
727 				result = method(retval, mdata, ap);
728 				va_end(ap);
729 			}
730 #else /* NS_CACHING */
731 			errno = 0;
732 			va_start(ap, defaults);
733 			result = method(retval, mdata, ap);
734 			va_end(ap);
735 #endif /* NS_CACHING */
736 
737 			if (result & (srclist[i].flags))
738 				break;
739 		} else {
740 			if (fb_method != NULL) {
741 				saved_depth = st->fallback_depth;
742 				st->fallback_depth = st->dispatch_depth + 1;
743 				va_start(ap, defaults);
744 				result = fb_method(retval,
745 				    (void *)srclist[i].name, ap);
746 				va_end(ap);
747 				st->fallback_depth = saved_depth;
748 			} else
749 				nss_log(LOG_DEBUG, "%s, %s, %s, not found, "
750 				    "and no fallback provided",
751 				    srclist[i].name, database, method_name);
752 		}
753 	}
754 
755 #ifdef NS_CACHING
756 	if (cache_data_p != NULL &&
757 	    (result & (NS_NOTFOUND | NS_SUCCESS)) && cache_flag == 0) {
758 		va_start(ap, defaults);
759 		if (result == NS_SUCCESS) {
760 			if (cache_data.info->id_func != NULL)
761 				__nss_common_cache_write(retval, cache_data_p,
762 				    ap);
763 			else if (cache_data.info->marshal_func != NULL)
764 				__nss_mp_cache_write(retval, cache_data_p, ap);
765 		} else if (result == NS_NOTFOUND) {
766 			if (cache_data.info->id_func == NULL) {
767 				if (cache_data.info->marshal_func != NULL)
768 					__nss_mp_cache_write_submit(retval,
769 					    cache_data_p, ap);
770 			} else
771 				__nss_common_cache_write_negative(cache_data_p);
772 		}
773 		va_end(ap);
774 	}
775 #endif /* NS_CACHING */
776 
777 	if (isthreaded)
778 		(void)_pthread_rwlock_unlock(&nss_lock);
779 	--st->dispatch_depth;
780 fin:
781 	errno = serrno;
782 	return (result);
783 }
784