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>
88 #include "un-namespace.h"
94 /* Number of elements allocated when we grow a vector */
99 * Global NSS data structures are mostly read-only, but we update
100 * them when we read or re-read the nsswitch.conf.
102 static pthread_rwlock_t nss_lock = PTHREAD_RWLOCK_INITIALIZER;
105 * Runtime determination of whether we are dynamically linked or not.
107 extern int _DYNAMIC __attribute__ ((weak));
108 #define is_dynamic() (&_DYNAMIC != NULL)
111 * default sourcelist: `files'
113 const ns_src __nsdefaultsrc[] = {
114 { NSSRC_FILES, NS_SUCCESS },
118 /* Database, source mappings. */
119 static unsigned int _nsmapsize;
120 static ns_dbt *_nsmap = NULL;
123 static unsigned int _nsmodsize;
124 static ns_mod *_nsmod;
126 /* Placeholder for builtin modules' dlopen `handle'. */
127 static int __nss_builtin_handle;
128 static void *nss_builtin_handle = &__nss_builtin_handle;
132 * Cache lookup cycle prevention function - if !NULL then no cache lookups
135 static void *nss_cache_cycle_prevention_func = NULL;
139 * Attempt to spew relatively uniform messages to syslog.
141 #define nss_log(level, fmt, ...) \
142 syslog((level), "NSSWITCH(%s): " fmt, __func__, __VA_ARGS__)
143 #define nss_log_simple(level, s) \
144 syslog((level), "NSSWITCH(%s): " s, __func__)
147 * Dynamically growable arrays are used for lists of databases, sources,
148 * and modules. The following `vector' interface is used to isolate the
151 typedef int (*vector_comparison)(const void *, const void *);
152 typedef void (*vector_free_elem)(void *);
153 static void vector_sort(void *, unsigned int, size_t,
155 static void vector_free(void *, unsigned int *, size_t,
157 static void *vector_ref(unsigned int, void *, unsigned int, size_t);
158 static void *vector_search(const void *, void *, unsigned int, size_t,
160 static void *vector_append(const void *, void *, unsigned int *, size_t);
164 * Internal interfaces.
166 static int string_compare(const void *, const void *);
167 static int mtab_compare(const void *, const void *);
168 static int nss_configure(void);
169 static void ns_dbt_free(ns_dbt *);
170 static void ns_mod_free(ns_mod *);
171 static void ns_src_free(ns_src **, int);
172 static void nss_load_builtin_modules(void);
173 static void nss_load_module(const char *, nss_module_register_fn);
174 static void nss_atexit(void);
176 extern FILE *_nsyyin;
180 * The vector operations
183 vector_sort(void *vec, unsigned int count, size_t esize,
184 vector_comparison comparison)
186 qsort(vec, count, esize, comparison);
191 vector_search(const void *key, void *vec, unsigned int count, size_t esize,
192 vector_comparison comparison)
194 return (bsearch(key, vec, count, esize, comparison));
199 vector_append(const void *elem, void *vec, unsigned int *count, size_t esize)
203 if ((*count % ELEMSPERCHUNK) == 0) {
204 p = realloc(vec, (*count + ELEMSPERCHUNK) * esize);
206 nss_log_simple(LOG_ERR, "memory allocation failure");
211 memmove((void *)(((uintptr_t)vec) + (*count * esize)), elem, esize);
218 vector_ref(unsigned int i, void *vec, unsigned int count, size_t esize)
221 return (void *)((uintptr_t)vec + (i * esize));
227 #define VECTOR_FREE(v, c, s, f) \
228 do { vector_free(v, c, s, f); v = NULL; } while (0)
230 vector_free(void *vec, unsigned int *count, size_t esize,
231 vector_free_elem free_elem)
236 for (i = 0; i < *count; i++) {
237 elem = vector_ref(i, vec, *count, esize);
246 * Comparison functions for vector_search.
249 string_compare(const void *a, const void *b)
251 return (strcasecmp(*(const char * const *)a, *(const char * const *)b));
256 mtab_compare(const void *a, const void *b)
260 cmp = strcmp(((const ns_mtab *)a)->name, ((const ns_mtab *)b)->name);
264 return (strcmp(((const ns_mtab *)a)->database,
265 ((const ns_mtab *)b)->database));
269 * NSS nsmap management.
272 _nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
276 dbt->srclist = vector_append(src, dbt->srclist, &dbt->srclistsize,
278 modp = vector_search(&src->name, _nsmod, _nsmodsize, sizeof(*_nsmod),
281 nss_load_module(src->name, NULL);
287 _nsdbtdump(const ns_dbt *dbt)
291 printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
292 dbt->srclistsize == 1 ? "" : "s");
293 for (i = 0; i < (int)dbt->srclistsize; i++) {
294 printf(" %s", dbt->srclist[i].name);
295 if (!(dbt->srclist[i].flags &
296 (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
297 (dbt->srclist[i].flags & NS_SUCCESS))
300 if (!(dbt->srclist[i].flags & NS_SUCCESS))
301 printf(" SUCCESS=continue");
302 if (dbt->srclist[i].flags & NS_UNAVAIL)
303 printf(" UNAVAIL=return");
304 if (dbt->srclist[i].flags & NS_NOTFOUND)
305 printf(" NOTFOUND=return");
306 if (dbt->srclist[i].flags & NS_TRYAGAIN)
307 printf(" TRYAGAIN=return");
316 * The first time nsdispatch is called (during a process's lifetime,
317 * or after nsswitch.conf has been updated), nss_configure will
318 * prepare global data needed by NSS.
323 static pthread_mutex_t conf_lock = PTHREAD_MUTEX_INITIALIZER;
324 static time_t confmod;
326 int result, isthreaded;
333 isthreaded = __isthreaded;
334 #if defined(_NSS_DEBUG) && defined(_NSS_SHOOT_FOOT)
335 /* NOTE WELL: THIS IS A SECURITY HOLE. This must only be built
336 * for debugging purposes and MUST NEVER be used in production.
338 path = getenv("NSSWITCH_CONF");
341 path = _PATH_NS_CONF;
342 if (stat(path, &statbuf) != 0)
344 if (statbuf.st_mtime <= confmod)
347 result = _pthread_mutex_trylock(&conf_lock);
350 (void)_pthread_rwlock_unlock(&nss_lock);
351 result = _pthread_rwlock_wrlock(&nss_lock);
355 _nsyyin = fopen(path, "r");
358 VECTOR_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
359 (vector_free_elem)ns_dbt_free);
360 VECTOR_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
361 (vector_free_elem)ns_mod_free);
362 nss_load_builtin_modules();
364 (void)fclose(_nsyyin);
365 vector_sort(_nsmap, _nsmapsize, sizeof(*_nsmap), string_compare);
367 (void)atexit(nss_atexit);
368 confmod = statbuf.st_mtime;
371 handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
372 if (handle != NULL) {
373 nss_cache_cycle_prevention_func = dlsym(handle,
374 "_nss_cache_cycle_prevention_function");
380 (void)_pthread_rwlock_unlock(&nss_lock);
382 result = _pthread_rwlock_rdlock(&nss_lock);
386 (void)_pthread_mutex_unlock(&conf_lock);
392 _nsdbtput(const ns_dbt *dbt)
397 for (i = 0; i < _nsmapsize; i++) {
398 p = vector_ref(i, _nsmap, _nsmapsize, sizeof(*_nsmap));
399 if (string_compare(&dbt->name, &p->name) == 0) {
400 /* overwrite existing entry */
401 if (p->srclist != NULL)
402 ns_src_free(&p->srclist, p->srclistsize);
403 memmove(p, dbt, sizeof(*dbt));
407 _nsmap = vector_append(dbt, _nsmap, &_nsmapsize, sizeof(*_nsmap));
412 ns_dbt_free(ns_dbt *dbt)
414 ns_src_free(&dbt->srclist, dbt->srclistsize);
419 ns_src_free(ns_src **src, int srclistsize)
423 for (i = 0; i < srclistsize; i++)
424 if ((*src)[i].name != NULL)
425 /* This one was allocated by nslexer. You'll just
428 free((void *)((*src)[i].name));
436 * NSS module management.
438 /* The built-in NSS modules are all loaded at once. */
439 #define NSS_BACKEND(name, reg) \
440 ns_mtab *reg(unsigned int *, nss_module_unregister_fn *);
441 #include "nss_backends.h"
445 nss_load_builtin_modules(void)
447 #define NSS_BACKEND(name, reg) nss_load_module(#name, reg);
448 #include "nss_backends.h"
453 /* Load a built-in or dynamically linked module. If the `reg_fn'
454 * argument is non-NULL, assume a built-in module and use reg_fn to
455 * register it. Otherwise, search for a dynamic NSS module.
458 nss_load_module(const char *source, nss_module_register_fn reg_fn)
462 nss_module_register_fn fn;
464 memset(&mod, 0, sizeof(mod));
465 mod.name = strdup(source);
466 if (mod.name == NULL) {
467 nss_log_simple(LOG_ERR, "memory allocation failure");
470 if (reg_fn != NULL) {
471 /* The placeholder is required, as a NULL handle
472 * represents an invalid module.
474 mod.handle = nss_builtin_handle;
476 } else if (!is_dynamic())
479 if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name,
480 NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf))
482 mod.handle = dlopen(buf, RTLD_LOCAL|RTLD_LAZY);
483 if (mod.handle == NULL) {
485 /* This gets pretty annoying since the built-in
486 * sources aren't modules yet.
488 nss_log(LOG_DEBUG, "%s, %s", mod.name, dlerror());
492 fn = (nss_module_register_fn)dlfunc(mod.handle,
493 "nss_module_register");
495 (void)dlclose(mod.handle);
497 nss_log(LOG_ERR, "%s, %s", mod.name, dlerror());
501 mod.mtab = fn(mod.name, &mod.mtabsize, &mod.unregister);
502 if (mod.mtab == NULL || mod.mtabsize == 0) {
503 if (mod.handle != nss_builtin_handle)
504 (void)dlclose(mod.handle);
506 nss_log(LOG_ERR, "%s, registration failed", mod.name);
509 if (mod.mtabsize > 1)
510 qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]),
513 _nsmod = vector_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod));
514 vector_sort(_nsmod, _nsmodsize, sizeof(*_nsmod), string_compare);
520 ns_mod_free(ns_mod *mod)
524 if (mod->handle == NULL)
526 if (mod->unregister != NULL)
527 mod->unregister(mod->mtab, mod->mtabsize);
528 if (mod->handle != nss_builtin_handle)
529 (void)dlclose(mod->handle);
542 isthreaded = __isthreaded;
544 (void)_pthread_rwlock_wrlock(&nss_lock);
545 VECTOR_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
546 (vector_free_elem)ns_dbt_free);
547 VECTOR_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
548 (vector_free_elem)ns_mod_free);
550 (void)_pthread_rwlock_unlock(&nss_lock);
556 * Finally, the actual implementation.
559 nss_method_lookup(const char *source, const char *database,
560 const char *method, const ns_dtab disp_tab[], void **mdata)
566 if (disp_tab != NULL)
567 for (i = 0; disp_tab[i].src != NULL; i++)
568 if (strcasecmp(source, disp_tab[i].src) == 0) {
569 *mdata = disp_tab[i].mdata;
570 return (disp_tab[i].method);
572 mod = vector_search(&source, _nsmod, _nsmodsize, sizeof(*_nsmod),
574 if (mod != NULL && mod->handle != NULL) {
575 key.database = database;
577 match = bsearch(&key, mod->mtab, mod->mtabsize,
578 sizeof(mod->mtab[0]), mtab_compare);
580 *mdata = match->mdata;
581 return (match->method);
585 nss_log(LOG_DEBUG, "%s, %s, %s, not found", source, database,
592 __weak_reference(_nsdispatch, nsdispatch);
595 _nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
596 const char *method_name, const ns_src defaults[], ...)
600 const ns_src *srclist;
603 int isthreaded, serrno, i, result, srclistsize;
606 nss_cache_data cache_data;
607 nss_cache_data *cache_data_p;
611 isthreaded = __isthreaded;
614 result = _pthread_rwlock_rdlock(&nss_lock);
620 result = nss_configure();
625 dbt = vector_search(&database, _nsmap, _nsmapsize, sizeof(*_nsmap),
628 srclist = dbt->srclist;
629 srclistsize = dbt->srclistsize;
633 while (srclist[srclistsize].name != NULL)
641 for (i = 0; i < srclistsize; i++) {
642 result = NS_NOTFOUND;
643 method = nss_method_lookup(srclist[i].name, database,
644 method_name, disp_tab, &mdata);
646 if (method != NULL) {
648 if (strcmp(srclist[i].name, NSSRC_CACHE) == 0 &&
649 nss_cache_cycle_prevention_func == NULL) {
650 #ifdef NS_STRICT_LIBC_EID_CHECKING
651 if (issetugid() != 0)
656 memset(&cache_data, 0, sizeof(nss_cache_data));
657 cache_data.info = (nss_cache_info const *)mdata;
658 cache_data_p = &cache_data;
660 va_start(ap, defaults);
661 if (cache_data.info->id_func != NULL)
662 result = __nss_common_cache_read(retval,
664 else if (cache_data.info->marshal_func != NULL)
665 result = __nss_mp_cache_read(retval,
668 result = __nss_mp_cache_end(retval,
673 va_start(ap, defaults);
674 result = method(retval, mdata, ap);
677 #else /* NS_CACHING */
678 va_start(ap, defaults);
679 result = method(retval, mdata, ap);
681 #endif /* NS_CACHING */
683 if (result & (srclist[i].flags))
689 if (cache_data_p != NULL &&
690 (result & (NS_NOTFOUND | NS_SUCCESS)) && cache_flag == 0) {
691 va_start(ap, defaults);
692 if (result == NS_SUCCESS) {
693 if (cache_data.info->id_func != NULL)
694 __nss_common_cache_write(retval, cache_data_p,
696 else if (cache_data.info->marshal_func != NULL)
697 __nss_mp_cache_write(retval, cache_data_p, ap);
698 } else if (result == NS_NOTFOUND) {
699 if (cache_data.info->id_func == NULL) {
700 if (cache_data.info->marshal_func != NULL)
701 __nss_mp_cache_write_submit(retval,
704 __nss_common_cache_write_negative(cache_data_p);
708 #endif /* NS_CACHING */
711 (void)_pthread_rwlock_unlock(&nss_lock);