source: rtems-libbsd/freebsd/lib/libc/net/nsdispatch.c @ 0a57e1d

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 0a57e1d was 0a57e1d, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 08:35:05

Reduce divergence from FreeBSD sources

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