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