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