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