1 /* $NetBSD: nsdispatch.c,v 1.9 1999/01/25 00:16:17 lukem Exp $ */
4 * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 2003 Networks Associates Technology, Inc.
33 * All rights reserved.
35 * Portions of this software were developed for the FreeBSD Project by
36 * Jacques A. Vidrine, Safeport Network Services, and Network
37 * Associates Laboratories, the Security Research Division of Network
38 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
39 * ("CBOSS"), as part of the DARPA CHATS research program.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #include "namespace.h"
67 #include <sys/param.h>
76 #include <pthread_np.h>
82 #include "un-namespace.h"
84 #include "libc_private.h"
90 /* Number of elements allocated when we grow a vector */
95 * Global NSS data structures are mostly read-only, but we update
96 * them when we read or re-read the nsswitch.conf.
98 static pthread_rwlock_t nss_lock = PTHREAD_RWLOCK_INITIALIZER;
101 * Runtime determination of whether we are dynamically linked or not.
103 extern int _DYNAMIC __attribute__ ((weak));
104 #define is_dynamic() (&_DYNAMIC != NULL)
107 * default sourcelist: `files'
109 const ns_src __nsdefaultsrc[] = {
110 { NSSRC_FILES, NS_SUCCESS },
114 /* Database, source mappings. */
115 static unsigned int _nsmapsize;
116 static ns_dbt *_nsmap = NULL;
119 static unsigned int _nsmodsize;
120 static ns_mod *_nsmod;
122 /* Placeholder for builtin modules' dlopen `handle'. */
123 static int __nss_builtin_handle;
124 static void *nss_builtin_handle = &__nss_builtin_handle;
128 * Cache lookup cycle prevention function - if !NULL then no cache lookups
131 static void *nss_cache_cycle_prevention_func = NULL;
135 * We keep track of nsdispatch() nesting depth in dispatch_depth. When a
136 * fallback method is invoked from nsdispatch(), we temporarily set
137 * fallback_depth to the current dispatch depth plus one. Subsequent
138 * calls at that exact depth will run in fallback mode (restricted to the
139 * same source as the call that was handled by the fallback method), while
140 * calls below that depth will be handled normally, allowing fallback
141 * methods to perform arbitrary lookups.
147 static void fb_endstate(void *);
148 NSS_TLS_HANDLING(fb);
151 * Attempt to spew relatively uniform messages to syslog.
153 #define nss_log(level, fmt, ...) \
154 syslog((level), "NSSWITCH(%s): " fmt, __func__, __VA_ARGS__)
155 #define nss_log_simple(level, s) \
156 syslog((level), "NSSWITCH(%s): " s, __func__)
159 * Dynamically growable arrays are used for lists of databases, sources,
160 * and modules. The following `vector' interface is used to isolate the
163 typedef int (*vector_comparison)(const void *, const void *);
164 typedef void (*vector_free_elem)(void *);
165 static void vector_sort(void *, unsigned int, size_t,
167 static void vector_free(void *, unsigned int *, size_t,
169 static void *vector_ref(unsigned int, void *, unsigned int, size_t);
170 static void *vector_search(const void *, void *, unsigned int, size_t,
172 static void *vector_append(const void *, void *, unsigned int *, size_t);
176 * Internal interfaces.
178 static int string_compare(const void *, const void *);
179 static int mtab_compare(const void *, const void *);
180 static int nss_configure(void);
181 static void ns_dbt_free(ns_dbt *);
182 static void ns_mod_free(ns_mod *);
183 static void ns_src_free(ns_src **, int);
184 static void nss_load_builtin_modules(void);
185 static void nss_load_module(const char *, nss_module_register_fn);
186 static void nss_atexit(void);
188 extern FILE *_nsyyin;
192 * The vector operations
195 vector_sort(void *vec, unsigned int count, size_t esize,
196 vector_comparison comparison)
198 qsort(vec, count, esize, comparison);
203 vector_search(const void *key, void *vec, unsigned int count, size_t esize,
204 vector_comparison comparison)
206 return (bsearch(key, vec, count, esize, comparison));
211 vector_append(const void *elem, void *vec, unsigned int *count, size_t esize)
215 if ((*count % ELEMSPERCHUNK) == 0) {
216 p = realloc(vec, (*count + ELEMSPERCHUNK) * esize);
218 nss_log_simple(LOG_ERR, "memory allocation failure");
223 memmove((void *)(((uintptr_t)vec) + (*count * esize)), elem, esize);
230 vector_ref(unsigned int i, void *vec, unsigned int count, size_t esize)
233 return (void *)((uintptr_t)vec + (i * esize));
239 #define VECTOR_FREE(v, c, s, f) \
240 do { vector_free(v, c, s, f); v = NULL; } while (0)
242 vector_free(void *vec, unsigned int *count, size_t esize,
243 vector_free_elem free_elem)
248 for (i = 0; i < *count; i++) {
249 elem = vector_ref(i, vec, *count, esize);
258 * Comparison functions for vector_search.
261 string_compare(const void *a, const void *b)
263 return (strcasecmp(*(const char * const *)a, *(const char * const *)b));
268 mtab_compare(const void *a, const void *b)
272 cmp = strcmp(((const ns_mtab *)a)->name, ((const ns_mtab *)b)->name);
276 return (strcmp(((const ns_mtab *)a)->database,
277 ((const ns_mtab *)b)->database));
281 * NSS nsmap management.
284 _nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
288 dbt->srclist = vector_append(src, dbt->srclist, &dbt->srclistsize,
290 modp = vector_search(&src->name, _nsmod, _nsmodsize, sizeof(*_nsmod),
293 nss_load_module(src->name, NULL);
299 _nsdbtdump(const ns_dbt *dbt)
303 printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
304 dbt->srclistsize == 1 ? "" : "s");
305 for (i = 0; i < (int)dbt->srclistsize; i++) {
306 printf(" %s", dbt->srclist[i].name);
307 if (!(dbt->srclist[i].flags &
308 (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
309 (dbt->srclist[i].flags & NS_SUCCESS))
312 if (!(dbt->srclist[i].flags & NS_SUCCESS))
313 printf(" SUCCESS=continue");
314 if (dbt->srclist[i].flags & NS_UNAVAIL)
315 printf(" UNAVAIL=return");
316 if (dbt->srclist[i].flags & NS_NOTFOUND)
317 printf(" NOTFOUND=return");
318 if (dbt->srclist[i].flags & NS_TRYAGAIN)
319 printf(" TRYAGAIN=return");
328 * The first time nsdispatch is called (during a process's lifetime,
329 * or after nsswitch.conf has been updated), nss_configure will
330 * prepare global data needed by NSS.
335 static pthread_mutex_t conf_lock = PTHREAD_MUTEX_INITIALIZER;
336 static time_t confmod;
338 int result, isthreaded;
345 isthreaded = __isthreaded;
346 #if defined(_NSS_DEBUG) && defined(_NSS_SHOOT_FOOT)
347 /* NOTE WELL: THIS IS A SECURITY HOLE. This must only be built
348 * for debugging purposes and MUST NEVER be used in production.
350 path = getenv("NSSWITCH_CONF");
353 path = _PATH_NS_CONF;
354 if (stat(path, &statbuf) != 0)
356 if (statbuf.st_mtime <= confmod)
359 result = _pthread_mutex_trylock(&conf_lock);
362 (void)_pthread_rwlock_unlock(&nss_lock);
363 result = _pthread_rwlock_wrlock(&nss_lock);
367 _nsyyin = fopen(path, "re");
370 VECTOR_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
371 (vector_free_elem)ns_dbt_free);
372 VECTOR_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
373 (vector_free_elem)ns_mod_free);
374 nss_load_builtin_modules();
376 (void)fclose(_nsyyin);
377 vector_sort(_nsmap, _nsmapsize, sizeof(*_nsmap), string_compare);
379 (void)atexit(nss_atexit);
380 confmod = statbuf.st_mtime;
383 handle = libc_dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
384 if (handle != NULL) {
385 nss_cache_cycle_prevention_func = dlsym(handle,
386 "_nss_cache_cycle_prevention_function");
392 (void)_pthread_rwlock_unlock(&nss_lock);
394 result = _pthread_rwlock_rdlock(&nss_lock);
398 (void)_pthread_mutex_unlock(&conf_lock);
404 _nsdbtput(const ns_dbt *dbt)
409 for (i = 0; i < _nsmapsize; i++) {
410 p = vector_ref(i, _nsmap, _nsmapsize, sizeof(*_nsmap));
411 if (string_compare(&dbt->name, &p->name) == 0) {
412 /* overwrite existing entry */
413 if (p->srclist != NULL)
414 ns_src_free(&p->srclist, p->srclistsize);
415 memmove(p, dbt, sizeof(*dbt));
419 _nsmap = vector_append(dbt, _nsmap, &_nsmapsize, sizeof(*_nsmap));
424 ns_dbt_free(ns_dbt *dbt)
426 ns_src_free(&dbt->srclist, dbt->srclistsize);
428 free((void *)dbt->name);
433 ns_src_free(ns_src **src, int srclistsize)
437 for (i = 0; i < srclistsize; i++)
438 if ((*src)[i].name != NULL)
439 /* This one was allocated by nslexer. You'll just
442 free((void *)((*src)[i].name));
450 * NSS module management.
452 /* The built-in NSS modules are all loaded at once. */
453 #define NSS_BACKEND(name, reg) \
454 ns_mtab *reg(unsigned int *, nss_module_unregister_fn *);
455 #include "nss_backends.h"
459 nss_load_builtin_modules(void)
461 #define NSS_BACKEND(name, reg) nss_load_module(#name, reg);
462 #include "nss_backends.h"
467 /* Load a built-in or dynamically linked module. If the `reg_fn'
468 * argument is non-NULL, assume a built-in module and use reg_fn to
469 * register it. Otherwise, search for a dynamic NSS module.
472 nss_load_module(const char *source, nss_module_register_fn reg_fn)
476 nss_module_register_fn fn;
478 memset(&mod, 0, sizeof(mod));
479 mod.name = strdup(source);
480 if (mod.name == NULL) {
481 nss_log_simple(LOG_ERR, "memory allocation failure");
484 if (reg_fn != NULL) {
485 /* The placeholder is required, as a NULL handle
486 * represents an invalid module.
488 mod.handle = nss_builtin_handle;
490 } else if (!is_dynamic())
493 if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name,
494 NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf))
496 mod.handle = libc_dlopen(buf, RTLD_LOCAL|RTLD_LAZY);
497 if (mod.handle == NULL) {
499 /* This gets pretty annoying since the built-in
500 * sources aren't modules yet.
502 nss_log(LOG_DEBUG, "%s, %s", mod.name, dlerror());
506 fn = (nss_module_register_fn)dlfunc(mod.handle,
507 "nss_module_register");
509 (void)dlclose(mod.handle);
511 nss_log(LOG_ERR, "%s, %s", mod.name, dlerror());
515 mod.mtab = fn(mod.name, &mod.mtabsize, &mod.unregister);
516 if (mod.mtab == NULL || mod.mtabsize == 0) {
517 if (mod.handle != nss_builtin_handle)
518 (void)dlclose(mod.handle);
520 nss_log(LOG_ERR, "%s, registration failed", mod.name);
523 if (mod.mtabsize > 1)
524 qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]),
527 _nsmod = vector_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod));
528 vector_sort(_nsmod, _nsmodsize, sizeof(*_nsmod), string_compare);
534 ns_mod_free(ns_mod *mod)
538 if (mod->handle == NULL)
540 if (mod->unregister != NULL)
541 mod->unregister(mod->mtab, mod->mtabsize);
542 if (mod->handle != nss_builtin_handle)
543 (void)dlclose(mod->handle);
556 isthreaded = __isthreaded;
558 (void)_pthread_rwlock_wrlock(&nss_lock);
559 VECTOR_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
560 (vector_free_elem)ns_dbt_free);
561 VECTOR_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
562 (vector_free_elem)ns_mod_free);
564 (void)_pthread_rwlock_unlock(&nss_lock);
570 * Finally, the actual implementation.
573 nss_method_lookup(const char *source, const char *database,
574 const char *method, const ns_dtab disp_tab[], void **mdata)
580 if (disp_tab != NULL)
581 for (i = 0; disp_tab[i].src != NULL; i++)
582 if (strcasecmp(source, disp_tab[i].src) == 0) {
583 *mdata = disp_tab[i].mdata;
584 return (disp_tab[i].method);
586 mod = vector_search(&source, _nsmod, _nsmodsize, sizeof(*_nsmod),
588 if (mod != NULL && mod->handle != NULL) {
589 key.database = database;
591 match = bsearch(&key, mod->mtab, mod->mtabsize,
592 sizeof(mod->mtab[0]), mtab_compare);
594 *mdata = match->mdata;
595 return (match->method);
609 __weak_reference(_nsdispatch, nsdispatch);
612 _nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
613 const char *method_name, const ns_src defaults[], ...)
617 const ns_src *srclist;
618 nss_method method, fb_method;
620 int isthreaded, serrno, i, result, srclistsize;
625 nss_cache_data cache_data;
626 nss_cache_data *cache_data_p;
633 isthreaded = __isthreaded;
636 result = _pthread_rwlock_rdlock(&nss_lock);
643 result = fb_getstate(&st);
649 result = nss_configure();
654 ++st->dispatch_depth;
655 if (st->dispatch_depth > st->fallback_depth) {
656 dbt = vector_search(&database, _nsmap, _nsmapsize, sizeof(*_nsmap),
658 fb_method = nss_method_lookup(NSSRC_FALLBACK, database,
659 method_name, disp_tab, &mdata);
663 srclist = dbt->srclist;
664 srclistsize = dbt->srclistsize;
668 while (srclist[srclistsize].name != NULL)
676 for (i = 0; i < srclistsize; i++) {
677 result = NS_NOTFOUND;
678 method = nss_method_lookup(srclist[i].name, database,
679 method_name, disp_tab, &mdata);
681 if (method != NULL) {
683 if (strcmp(srclist[i].name, NSSRC_CACHE) == 0 &&
684 nss_cache_cycle_prevention_func == NULL) {
685 #ifdef NS_STRICT_LIBC_EID_CHECKING
686 if (issetugid() != 0)
691 memset(&cache_data, 0, sizeof(nss_cache_data));
692 cache_data.info = (nss_cache_info const *)mdata;
693 cache_data_p = &cache_data;
695 va_start(ap, defaults);
696 if (cache_data.info->id_func != NULL)
697 result = __nss_common_cache_read(retval,
699 else if (cache_data.info->marshal_func != NULL)
700 result = __nss_mp_cache_read(retval,
703 result = __nss_mp_cache_end(retval,
709 va_start(ap, defaults);
710 result = method(retval, mdata, ap);
713 #else /* NS_CACHING */
715 va_start(ap, defaults);
716 result = method(retval, mdata, ap);
718 #endif /* NS_CACHING */
720 if (result & (srclist[i].flags))
723 if (fb_method != NULL) {
724 saved_depth = st->fallback_depth;
725 st->fallback_depth = st->dispatch_depth + 1;
726 va_start(ap, defaults);
727 result = fb_method(retval,
728 (void *)srclist[i].name, ap);
730 st->fallback_depth = saved_depth;
732 nss_log(LOG_DEBUG, "%s, %s, %s, not found, "
733 "and no fallback provided",
734 srclist[i].name, database, method_name);
739 if (cache_data_p != NULL &&
740 (result & (NS_NOTFOUND | NS_SUCCESS)) && cache_flag == 0) {
741 va_start(ap, defaults);
742 if (result == NS_SUCCESS) {
743 if (cache_data.info->id_func != NULL)
744 __nss_common_cache_write(retval, cache_data_p,
746 else if (cache_data.info->marshal_func != NULL)
747 __nss_mp_cache_write(retval, cache_data_p, ap);
748 } else if (result == NS_NOTFOUND) {
749 if (cache_data.info->id_func == NULL) {
750 if (cache_data.info->marshal_func != NULL)
751 __nss_mp_cache_write_submit(retval,
754 __nss_common_cache_write_negative(cache_data_p);
758 #endif /* NS_CACHING */
761 (void)_pthread_rwlock_unlock(&nss_lock);
762 --st->dispatch_depth;