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