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.
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.
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.
39 * Copyright (c) 2003 Networks Associates Technology, Inc.
40 * All rights reserved.
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.
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
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.
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
70 #include <sys/cdefs.h>
71 __FBSDID("$FreeBSD$");
73 #include "namespace.h"
74 #include <sys/param.h>
83 #include <pthread_np.h>
89 #include "un-namespace.h"
91 #include "libc_private.h"
97 /* Number of elements allocated when we grow a vector */
102 * Global NSS data structures are mostly read-only, but we update
103 * them when we read or re-read the nsswitch.conf.
105 static pthread_rwlock_t nss_lock = PTHREAD_RWLOCK_INITIALIZER;
108 * Runtime determination of whether we are dynamically linked or not.
110 extern int _DYNAMIC __attribute__ ((weak));
111 #define is_dynamic() (&_DYNAMIC != NULL)
114 * default sourcelist: `files'
116 const ns_src __nsdefaultsrc[] = {
117 { NSSRC_FILES, NS_SUCCESS },
121 /* Database, source mappings. */
122 static unsigned int _nsmapsize;
123 static ns_dbt *_nsmap = NULL;
126 static unsigned int _nsmodsize;
127 static ns_mod *_nsmod;
129 /* Placeholder for builtin modules' dlopen `handle'. */
130 static int __nss_builtin_handle;
131 static void *nss_builtin_handle = &__nss_builtin_handle;
135 * Cache lookup cycle prevention function - if !NULL then no cache lookups
138 static void *nss_cache_cycle_prevention_func = NULL;
142 * When this is set to 1, nsdispatch won't use nsswitch.conf
143 * but will consult the 'defaults' source list only.
144 * NOTE: nested fallbacks (when nsdispatch calls fallback functions,
145 * which in turn calls nsdispatch, which should call fallback
146 * function) are not supported
151 static void fb_endstate(void *);
152 NSS_TLS_HANDLING(fb);
155 * Attempt to spew relatively uniform messages to syslog.
157 #define nss_log(level, fmt, ...) \
158 syslog((level), "NSSWITCH(%s): " fmt, __func__, __VA_ARGS__)
159 #define nss_log_simple(level, s) \
160 syslog((level), "NSSWITCH(%s): " s, __func__)
163 * Dynamically growable arrays are used for lists of databases, sources,
164 * and modules. The following `vector' interface is used to isolate the
167 typedef int (*vector_comparison)(const void *, const void *);
168 typedef void (*vector_free_elem)(void *);
169 static void vector_sort(void *, unsigned int, size_t,
171 static void vector_free(void *, unsigned int *, size_t,
173 static void *vector_ref(unsigned int, void *, unsigned int, size_t);
174 static void *vector_search(const void *, void *, unsigned int, size_t,
176 static void *vector_append(const void *, void *, unsigned int *, size_t);
180 * Internal interfaces.
182 static int string_compare(const void *, const void *);
183 static int mtab_compare(const void *, const void *);
184 static int nss_configure(void);
185 static void ns_dbt_free(ns_dbt *);
186 static void ns_mod_free(ns_mod *);
187 static void ns_src_free(ns_src **, int);
188 static void nss_load_builtin_modules(void);
189 static void nss_load_module(const char *, nss_module_register_fn);
190 static void nss_atexit(void);
192 extern FILE *_nsyyin;
196 * The vector operations
199 vector_sort(void *vec, unsigned int count, size_t esize,
200 vector_comparison comparison)
202 qsort(vec, count, esize, comparison);
207 vector_search(const void *key, void *vec, unsigned int count, size_t esize,
208 vector_comparison comparison)
210 return (bsearch(key, vec, count, esize, comparison));
215 vector_append(const void *elem, void *vec, unsigned int *count, size_t esize)
219 if ((*count % ELEMSPERCHUNK) == 0) {
220 p = realloc(vec, (*count + ELEMSPERCHUNK) * esize);
222 nss_log_simple(LOG_ERR, "memory allocation failure");
227 memmove((void *)(((uintptr_t)vec) + (*count * esize)), elem, esize);
234 vector_ref(unsigned int i, void *vec, unsigned int count, size_t esize)
237 return (void *)((uintptr_t)vec + (i * esize));
243 #define VECTOR_FREE(v, c, s, f) \
244 do { vector_free(v, c, s, f); v = NULL; } while (0)
246 vector_free(void *vec, unsigned int *count, size_t esize,
247 vector_free_elem free_elem)
252 for (i = 0; i < *count; i++) {
253 elem = vector_ref(i, vec, *count, esize);
262 * Comparison functions for vector_search.
265 string_compare(const void *a, const void *b)
267 return (strcasecmp(*(const char * const *)a, *(const char * const *)b));
272 mtab_compare(const void *a, const void *b)
276 cmp = strcmp(((const ns_mtab *)a)->name, ((const ns_mtab *)b)->name);
280 return (strcmp(((const ns_mtab *)a)->database,
281 ((const ns_mtab *)b)->database));
285 * NSS nsmap management.
288 _nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
292 dbt->srclist = vector_append(src, dbt->srclist, &dbt->srclistsize,
294 modp = vector_search(&src->name, _nsmod, _nsmodsize, sizeof(*_nsmod),
297 nss_load_module(src->name, NULL);
303 _nsdbtdump(const ns_dbt *dbt)
307 printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
308 dbt->srclistsize == 1 ? "" : "s");
309 for (i = 0; i < (int)dbt->srclistsize; i++) {
310 printf(" %s", dbt->srclist[i].name);
311 if (!(dbt->srclist[i].flags &
312 (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
313 (dbt->srclist[i].flags & NS_SUCCESS))
316 if (!(dbt->srclist[i].flags & NS_SUCCESS))
317 printf(" SUCCESS=continue");
318 if (dbt->srclist[i].flags & NS_UNAVAIL)
319 printf(" UNAVAIL=return");
320 if (dbt->srclist[i].flags & NS_NOTFOUND)
321 printf(" NOTFOUND=return");
322 if (dbt->srclist[i].flags & NS_TRYAGAIN)
323 printf(" TRYAGAIN=return");
332 * The first time nsdispatch is called (during a process's lifetime,
333 * or after nsswitch.conf has been updated), nss_configure will
334 * prepare global data needed by NSS.
339 static pthread_mutex_t conf_lock = PTHREAD_MUTEX_INITIALIZER;
340 static time_t confmod;
342 int result, isthreaded;
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.
354 path = getenv("NSSWITCH_CONF");
357 path = _PATH_NS_CONF;
358 if (stat(path, &statbuf) != 0)
360 if (statbuf.st_mtime <= confmod)
363 result = _pthread_mutex_trylock(&conf_lock);
366 (void)_pthread_rwlock_unlock(&nss_lock);
367 result = _pthread_rwlock_wrlock(&nss_lock);
371 _nsyyin = fopen(path, "r");
374 VECTOR_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
375 (vector_free_elem)ns_dbt_free);
376 VECTOR_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
377 (vector_free_elem)ns_mod_free);
378 nss_load_builtin_modules();
380 (void)fclose(_nsyyin);
381 vector_sort(_nsmap, _nsmapsize, sizeof(*_nsmap), string_compare);
383 (void)atexit(nss_atexit);
384 confmod = statbuf.st_mtime;
387 handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
388 if (handle != NULL) {
389 nss_cache_cycle_prevention_func = dlsym(handle,
390 "_nss_cache_cycle_prevention_function");
396 (void)_pthread_rwlock_unlock(&nss_lock);
398 result = _pthread_rwlock_rdlock(&nss_lock);
402 (void)_pthread_mutex_unlock(&conf_lock);
408 _nsdbtput(const ns_dbt *dbt)
413 for (i = 0; i < _nsmapsize; i++) {
414 p = vector_ref(i, _nsmap, _nsmapsize, sizeof(*_nsmap));
415 if (string_compare(&dbt->name, &p->name) == 0) {
416 /* overwrite existing entry */
417 if (p->srclist != NULL)
418 ns_src_free(&p->srclist, p->srclistsize);
419 memmove(p, dbt, sizeof(*dbt));
423 _nsmap = vector_append(dbt, _nsmap, &_nsmapsize, sizeof(*_nsmap));
428 ns_dbt_free(ns_dbt *dbt)
430 ns_src_free(&dbt->srclist, dbt->srclistsize);
432 free((void *)dbt->name);
437 ns_src_free(ns_src **src, int srclistsize)
441 for (i = 0; i < srclistsize; i++)
442 if ((*src)[i].name != NULL)
443 /* This one was allocated by nslexer. You'll just
446 free((void *)((*src)[i].name));
454 * NSS module management.
456 /* The built-in NSS modules are all loaded at once. */
457 #define NSS_BACKEND(name, reg) \
458 ns_mtab *reg(unsigned int *, nss_module_unregister_fn *);
459 #include "nss_backends.h"
463 nss_load_builtin_modules(void)
465 #define NSS_BACKEND(name, reg) nss_load_module(#name, reg);
466 #include "nss_backends.h"
471 /* Load a built-in or dynamically linked module. If the `reg_fn'
472 * argument is non-NULL, assume a built-in module and use reg_fn to
473 * register it. Otherwise, search for a dynamic NSS module.
476 nss_load_module(const char *source, nss_module_register_fn reg_fn)
480 nss_module_register_fn fn;
482 memset(&mod, 0, sizeof(mod));
483 mod.name = strdup(source);
484 if (mod.name == NULL) {
485 nss_log_simple(LOG_ERR, "memory allocation failure");
488 if (reg_fn != NULL) {
489 /* The placeholder is required, as a NULL handle
490 * represents an invalid module.
492 mod.handle = nss_builtin_handle;
494 } else if (!is_dynamic())
497 if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name,
498 NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf))
500 mod.handle = dlopen(buf, RTLD_LOCAL|RTLD_LAZY);
501 if (mod.handle == NULL) {
503 /* This gets pretty annoying since the built-in
504 * sources aren't modules yet.
506 nss_log(LOG_DEBUG, "%s, %s", mod.name, dlerror());
510 fn = (nss_module_register_fn)dlfunc(mod.handle,
511 "nss_module_register");
513 (void)dlclose(mod.handle);
515 nss_log(LOG_ERR, "%s, %s", mod.name, dlerror());
519 mod.mtab = fn(mod.name, &mod.mtabsize, &mod.unregister);
520 if (mod.mtab == NULL || mod.mtabsize == 0) {
521 if (mod.handle != nss_builtin_handle)
522 (void)dlclose(mod.handle);
524 nss_log(LOG_ERR, "%s, registration failed", mod.name);
527 if (mod.mtabsize > 1)
528 qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]),
531 _nsmod = vector_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod));
532 vector_sort(_nsmod, _nsmodsize, sizeof(*_nsmod), string_compare);
538 ns_mod_free(ns_mod *mod)
542 if (mod->handle == NULL)
544 if (mod->unregister != NULL)
545 mod->unregister(mod->mtab, mod->mtabsize);
546 if (mod->handle != nss_builtin_handle)
547 (void)dlclose(mod->handle);
560 isthreaded = __isthreaded;
562 (void)_pthread_rwlock_wrlock(&nss_lock);
563 VECTOR_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
564 (vector_free_elem)ns_dbt_free);
565 VECTOR_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
566 (vector_free_elem)ns_mod_free);
568 (void)_pthread_rwlock_unlock(&nss_lock);
574 * Finally, the actual implementation.
577 nss_method_lookup(const char *source, const char *database,
578 const char *method, const ns_dtab disp_tab[], void **mdata)
584 if (disp_tab != NULL)
585 for (i = 0; disp_tab[i].src != NULL; i++)
586 if (strcasecmp(source, disp_tab[i].src) == 0) {
587 *mdata = disp_tab[i].mdata;
588 return (disp_tab[i].method);
590 mod = vector_search(&source, _nsmod, _nsmodsize, sizeof(*_nsmod),
592 if (mod != NULL && mod->handle != NULL) {
593 key.database = database;
595 match = bsearch(&key, mod->mtab, mod->mtabsize,
596 sizeof(mod->mtab[0]), mtab_compare);
598 *mdata = match->mdata;
599 return (match->method);
613 __weak_reference(_nsdispatch, nsdispatch);
616 _nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
617 const char *method_name, const ns_src defaults[], ...)
621 const ns_src *srclist;
622 nss_method method, fb_method;
624 int isthreaded, serrno, i, result, srclistsize;
628 nss_cache_data cache_data;
629 nss_cache_data *cache_data_p;
636 isthreaded = __isthreaded;
639 result = _pthread_rwlock_rdlock(&nss_lock);
646 result = fb_getstate(&st);
652 result = nss_configure();
657 if (st->fb_dispatch == 0) {
658 dbt = vector_search(&database, _nsmap, _nsmapsize, sizeof(*_nsmap),
660 fb_method = nss_method_lookup(NSSRC_FALLBACK, database,
661 method_name, disp_tab, &mdata);
665 srclist = dbt->srclist;
666 srclistsize = dbt->srclistsize;
670 while (srclist[srclistsize].name != NULL)
678 for (i = 0; i < srclistsize; i++) {
679 result = NS_NOTFOUND;
680 method = nss_method_lookup(srclist[i].name, database,
681 method_name, disp_tab, &mdata);
683 if (method != NULL) {
685 if (strcmp(srclist[i].name, NSSRC_CACHE) == 0 &&
686 nss_cache_cycle_prevention_func == NULL) {
687 #ifdef NS_STRICT_LIBC_EID_CHECKING
688 if (issetugid() != 0)
693 memset(&cache_data, 0, sizeof(nss_cache_data));
694 cache_data.info = (nss_cache_info const *)mdata;
695 cache_data_p = &cache_data;
697 va_start(ap, defaults);
698 if (cache_data.info->id_func != NULL)
699 result = __nss_common_cache_read(retval,
701 else if (cache_data.info->marshal_func != NULL)
702 result = __nss_mp_cache_read(retval,
705 result = __nss_mp_cache_end(retval,
710 va_start(ap, defaults);
711 result = method(retval, mdata, ap);
714 #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) {
725 va_start(ap, defaults);
726 result = fb_method(retval,
727 (void *)srclist[i].name, ap);
731 nss_log(LOG_DEBUG, "%s, %s, %s, not found, "
732 "and no fallback provided",
733 srclist[i].name, database, method_name);
738 if (cache_data_p != NULL &&
739 (result & (NS_NOTFOUND | NS_SUCCESS)) && cache_flag == 0) {
740 va_start(ap, defaults);
741 if (result == NS_SUCCESS) {
742 if (cache_data.info->id_func != NULL)
743 __nss_common_cache_write(retval, cache_data_p,
745 else if (cache_data.info->marshal_func != NULL)
746 __nss_mp_cache_write(retval, cache_data_p, ap);
747 } else if (result == NS_NOTFOUND) {
748 if (cache_data.info->id_func == NULL) {
749 if (cache_data.info->marshal_func != NULL)
750 __nss_mp_cache_write_submit(retval,
753 __nss_common_cache_write_negative(cache_data_p);
757 #endif /* NS_CACHING */
760 (void)_pthread_rwlock_unlock(&nss_lock);