Home | History | Annotate | Line # | Download | only in net
nsdispatch.c revision 1.34
      1 /*	$NetBSD: nsdispatch.c,v 1.34 2009/02/05 13:21:11 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999, 2004 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; and by Jason R. Thorpe.
      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  *
     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.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 2003 Networks Associates Technology, Inc.
     34  * All rights reserved.
     35  *
     36  * Portions of this software were developed for the FreeBSD Project by
     37  * Jacques A. Vidrine, Safeport Network Services, and Network
     38  * Associates Laboratories, the Security Research Division of Network
     39  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
     40  * ("CBOSS"), as part of the DARPA CHATS research program.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 #if defined(LIBC_SCCS) && !defined(lint)
     66 __RCSID("$NetBSD: nsdispatch.c,v 1.34 2009/02/05 13:21:11 lukem Exp $");
     67 #endif /* LIBC_SCCS and not lint */
     68 
     69 #include "namespace.h"
     70 
     71 #include <sys/types.h>
     72 #include <sys/param.h>
     73 #include <sys/stat.h>
     74 #include <sys/queue.h>
     75 
     76 #include <assert.h>
     77 #ifdef __ELF__
     78 #include <dlfcn.h>
     79 #endif /* __ELF__ */
     80 #include <err.h>
     81 #include <fcntl.h>
     82 #define _NS_PRIVATE
     83 #include <nsswitch.h>
     84 #include <stdarg.h>
     85 #include <stdio.h>
     86 #include <stdlib.h>
     87 #include <string.h>
     88 #include <unistd.h>
     89 
     90 #include "reentrant.h"
     91 
     92 extern	FILE 	*_nsyyin;
     93 extern	int	 _nsyyparse(void);
     94 
     95 
     96 #ifdef __weak_alias
     97 __weak_alias(nsdispatch,_nsdispatch)
     98 #endif
     99 
    100 
    101 /*
    102  * default sourcelist: `files'
    103  */
    104 const ns_src __nsdefaultsrc[] = {
    105 	{ NSSRC_FILES,	NS_SUCCESS },
    106 	{ 0, 0 },
    107 };
    108 
    109 const ns_src __nsdefaultcompat[] = {
    110 	{ NSSRC_COMPAT,	NS_SUCCESS },
    111 	{ 0, 0 }
    112 };
    113 
    114 const ns_src __nsdefaultcompat_forceall[] = {
    115 	{ NSSRC_COMPAT,	NS_SUCCESS | NS_FORCEALL },
    116 	{ 0, 0 }
    117 };
    118 
    119 const ns_src __nsdefaultfiles[] = {
    120 	{ NSSRC_FILES,	NS_SUCCESS },
    121 	{ 0, 0 },
    122 };
    123 
    124 const ns_src __nsdefaultfiles_forceall[] = {
    125 	{ NSSRC_FILES,	NS_SUCCESS | NS_FORCEALL },
    126 	{ 0, 0 },
    127 };
    128 
    129 const ns_src __nsdefaultnis[] = {
    130 	{ NSSRC_NIS,	NS_SUCCESS },
    131 	{ 0, 0 }
    132 };
    133 
    134 const ns_src __nsdefaultnis_forceall[] = {
    135 	{ NSSRC_NIS,	NS_SUCCESS | NS_FORCEALL },
    136 	{ 0, 0 }
    137 };
    138 
    139 
    140 /* Database, source mappings. */
    141 static	u_int			 _nsmapsize;
    142 static	ns_dbt			*_nsmap;
    143 
    144 /* Nsswitch modules. */
    145 static	u_int			 _nsmodsize;
    146 static	ns_mod			*_nsmod;
    147 
    148 /* Placeholder for built-in modules' dlopen() handles. */
    149 static	void			*_nsbuiltin = &_nsbuiltin;
    150 
    151 #ifdef _REENTRANT
    152 /*
    153  * Global nsswitch data structures are mostly read-only, but we update them
    154  * when we read or re-read nsswitch.conf.
    155  */
    156 static 	rwlock_t		_nslock = RWLOCK_INITIALIZER;
    157 
    158 /*
    159  * List of threads currently in nsdispatch().  We use this to detect
    160  * recursive calls and avoid reloading configuration in such cases,
    161  * which could cause deadlock.
    162  */
    163 struct _ns_drec {
    164 	LIST_ENTRY(_ns_drec)	list;
    165 	thr_t			thr;
    166 };
    167 static LIST_HEAD(, _ns_drec) _ns_drec = LIST_HEAD_INITIALIZER(&_ns_drec);
    168 static mutex_t _ns_drec_lock = MUTEX_INITIALIZER;
    169 #endif /* _REENTRANT */
    170 
    171 
    172 /*
    173  * Runtime determination of whether we are dynamically linked or not.
    174  */
    175 #ifdef __ELF__
    176 extern	int			_DYNAMIC __weak_reference(_DYNAMIC);
    177 #define	is_dynamic()		(&_DYNAMIC != NULL)
    178 #else
    179 #define	is_dynamic()		(0)	/* don't bother - switch to ELF! */
    180 #endif /* __ELF__ */
    181 
    182 
    183 /*
    184  * size of dynamic array chunk for _nsmap and _nsmap[x].srclist (and other
    185  * growing arrays).
    186  */
    187 #define NSELEMSPERCHUNK		8
    188 
    189 /*
    190  * Dynamically growable arrays are used for lists of databases, sources,
    191  * and modules.  The following "vector" API is used to isolate the
    192  * common operations.
    193  */
    194 typedef void	(*_nsvect_free_elem)(void *);
    195 
    196 static void *
    197 _nsvect_append(const void *elem, void *vec, u_int *count, size_t esize)
    198 {
    199 	void	*p;
    200 
    201 	if ((*count % NSELEMSPERCHUNK) == 0) {
    202 		p = realloc(vec, (*count + NSELEMSPERCHUNK) * esize);
    203 		if (p == NULL)
    204 			return (NULL);
    205 		vec = p;
    206 	}
    207 	memmove((void *)(((uintptr_t)vec) + (*count * esize)), elem, esize);
    208 	(*count)++;
    209 	return (vec);
    210 }
    211 
    212 static void *
    213 _nsvect_elem(u_int i, void *vec, u_int count, size_t esize)
    214 {
    215 
    216 	if (i < count)
    217 		return ((void *)((uintptr_t)vec + (i * esize)));
    218 	else
    219 		return (NULL);
    220 }
    221 
    222 static void
    223 _nsvect_free(void *vec, u_int *count, size_t esize, _nsvect_free_elem free_elem)
    224 {
    225 	void	*elem;
    226 	u_int	 i;
    227 
    228 	for (i = 0; i < *count; i++) {
    229 		elem = _nsvect_elem(i, vec, *count, esize);
    230 		if (elem != NULL)
    231 			(*free_elem)(elem);
    232 	}
    233 	if (vec != NULL)
    234 		free(vec);
    235 	*count = 0;
    236 }
    237 #define	_NSVECT_FREE(v, c, s, f)					\
    238 do {									\
    239 	_nsvect_free((v), (c), (s), (f));				\
    240 	(v) = NULL;							\
    241 } while (/*CONSTCOND*/0)
    242 
    243 static int
    244 _nsdbtcmp(const void *a, const void *b)
    245 {
    246 
    247 	return (strcasecmp(((const ns_dbt *)a)->name,
    248 	    ((const ns_dbt *)b)->name));
    249 }
    250 
    251 static int
    252 _nsmodcmp(const void *a, const void *b)
    253 {
    254 
    255 	return (strcasecmp(((const ns_mod *)a)->name,
    256 	    ((const ns_mod *)b)->name));
    257 }
    258 
    259 static int
    260 _nsmtabcmp(const void *a, const void *b)
    261 {
    262 	int	cmp;
    263 
    264 	cmp = strcmp(((const ns_mtab *)a)->name,
    265 	    ((const ns_mtab *)b)->name);
    266 	if (cmp)
    267 		return (cmp);
    268 
    269 	return (strcasecmp(((const ns_mtab *)a)->database,
    270 	    ((const ns_mtab *)b)->database));
    271 }
    272 
    273 static void
    274 _nsmodfree(ns_mod *mod)
    275 {
    276 
    277 	free(__UNCONST(mod->name));
    278 	if (mod->handle == NULL)
    279 		return;
    280 	if (mod->unregister != NULL)
    281 		(*mod->unregister)(mod->mtab, mod->mtabsize);
    282 #ifdef __ELF__
    283 	if (mod->handle != _nsbuiltin)
    284 		(void) dlclose(mod->handle);
    285 #endif /* __ELF__ */
    286 }
    287 
    288 /*
    289  * Load a built-in or dyanamically linked module.  If the `reg_fn'
    290  * argument is non-NULL, assume a built-in module and use `reg_fn'
    291  * to register it.  Otherwise, search for a dynamic nsswitch module.
    292  */
    293 static int
    294 _nsloadmod(const char *source, nss_module_register_fn reg_fn)
    295 {
    296 #ifdef __ELF__
    297 	char	buf[PATH_MAX];
    298 #endif
    299 	ns_mod	mod, *new;
    300 
    301 	memset(&mod, 0, sizeof(mod));
    302 	mod.name = strdup(source);
    303 	if (mod.name == NULL)
    304 		return (-1);
    305 
    306 	if (reg_fn != NULL) {
    307 		/*
    308 		 * The placeholder is required, as a NULL handle
    309 		 * represents an invalid module.
    310 		 */
    311 		mod.handle = _nsbuiltin;
    312 	} else if (!is_dynamic()) {
    313 		goto out;
    314 	} else {
    315 #ifdef __ELF__
    316 		if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name,
    317 		    NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf))
    318 			goto out;
    319 		mod.handle = dlopen(buf, RTLD_LOCAL | RTLD_LAZY);
    320 		if (mod.handle == NULL) {
    321 #ifdef _NSS_DEBUG
    322 			/*
    323 			 * This gets pretty annoying, since the built-in
    324 			 * sources are not yet modules.
    325 			 */
    326 			/* XXX log some error? */
    327 #endif
    328 			goto out;
    329 		}
    330 		reg_fn = (nss_module_register_fn) dlsym(mod.handle,
    331 		    "nss_module_register");
    332 		if (reg_fn == NULL) {
    333 			(void) dlclose(mod.handle);
    334 			mod.handle = NULL;
    335 			/* XXX log some error? */
    336 			goto out;
    337 		}
    338 #else /* ! __ELF__ */
    339 		mod.handle = NULL;
    340 #endif /* __ELF__ */
    341 	}
    342 	mod.mtab = (*reg_fn)(mod.name, &mod.mtabsize, &mod.unregister);
    343 	if (mod.mtab == NULL || mod.mtabsize == 0) {
    344 #ifdef __ELF__
    345 		if (mod.handle != _nsbuiltin)
    346 			(void) dlclose(mod.handle);
    347 #endif /* __ELF__ */
    348 		mod.handle = NULL;
    349 		/* XXX log some error? */
    350 		goto out;
    351 	}
    352 	if (mod.mtabsize > 1)
    353 		qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]),
    354 		    _nsmtabcmp);
    355  out:
    356 	new = _nsvect_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod));
    357 	if (new == NULL) {
    358 		_nsmodfree(&mod);
    359 		return (-1);
    360 	}
    361 	_nsmod = new;
    362 	/* _nsmodsize already incremented */
    363 
    364 	qsort(_nsmod, _nsmodsize, sizeof(*_nsmod), _nsmodcmp);
    365 	return (0);
    366 }
    367 
    368 static void
    369 _nsloadbuiltin(void)
    370 {
    371 
    372 	/* Do nothing, for now. */
    373 }
    374 
    375 int
    376 _nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
    377 {
    378 	void		*new;
    379 	const ns_mod	*mod;
    380 	ns_mod		 modkey;
    381 
    382 	_DIAGASSERT(dbt != NULL);
    383 	_DIAGASSERT(src != NULL);
    384 
    385 	new = _nsvect_append(src, dbt->srclist, &dbt->srclistsize,
    386 	    sizeof(*src));
    387 	if (new == NULL)
    388 		return (-1);
    389 	dbt->srclist = new;
    390 	/* dbt->srclistsize already incremented */
    391 
    392 	modkey.name = src->name;
    393 	mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod),
    394 	    _nsmodcmp);
    395 	if (mod == NULL)
    396 		return (_nsloadmod(src->name, NULL));
    397 
    398 	return (0);
    399 }
    400 
    401 void
    402 _nsdbtdump(const ns_dbt *dbt)
    403 {
    404 	unsigned int	i;
    405 
    406 	_DIAGASSERT(dbt != NULL);
    407 
    408 	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
    409 	    dbt->srclistsize == 1 ? "" : "s");
    410 	for (i = 0; i < dbt->srclistsize; i++) {
    411 		printf(" %s", dbt->srclist[i].name);
    412 		if (!(dbt->srclist[i].flags &
    413 		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
    414 		    (dbt->srclist[i].flags & NS_SUCCESS))
    415 			continue;
    416 		printf(" [");
    417 		if (!(dbt->srclist[i].flags & NS_SUCCESS))
    418 			printf(" SUCCESS=continue");
    419 		if (dbt->srclist[i].flags & NS_UNAVAIL)
    420 			printf(" UNAVAIL=return");
    421 		if (dbt->srclist[i].flags & NS_NOTFOUND)
    422 			printf(" NOTFOUND=return");
    423 		if (dbt->srclist[i].flags & NS_TRYAGAIN)
    424 			printf(" TRYAGAIN=return");
    425 		printf(" ]");
    426 	}
    427 	printf("\n");
    428 }
    429 
    430 static void
    431 _nssrclist_free(ns_src **src, u_int srclistsize)
    432 {
    433 	u_int	i;
    434 
    435 	for (i = 0; i < srclistsize; i++) {
    436 		if ((*src)[i].name != NULL)
    437 			free(__UNCONST((*src)[i].name));
    438 	}
    439 	free(*src);
    440 	*src = NULL;
    441 }
    442 
    443 static void
    444 _nsdbtfree(ns_dbt *dbt)
    445 {
    446 
    447 	_nssrclist_free(&dbt->srclist, dbt->srclistsize);
    448 	if (dbt->name != NULL)
    449 		free(__UNCONST(dbt->name));
    450 }
    451 
    452 int
    453 _nsdbtput(const ns_dbt *dbt)
    454 {
    455 	ns_dbt	*p;
    456 	void	*new;
    457 	u_int	i;
    458 
    459 	_DIAGASSERT(dbt != NULL);
    460 
    461 	for (i = 0; i < _nsmapsize; i++) {
    462 		p = _nsvect_elem(i, _nsmap, _nsmapsize, sizeof(*_nsmap));
    463 		if (strcasecmp(dbt->name, p->name) == 0) {
    464 					/* overwrite existing entry */
    465 			if (p->srclist != NULL)
    466 				_nssrclist_free(&p->srclist, p->srclistsize);
    467 			memmove(p, dbt, sizeof(*dbt));
    468 			return (0);
    469 		}
    470 	}
    471 	new = _nsvect_append(dbt, _nsmap, &_nsmapsize, sizeof(*_nsmap));
    472 	if (new == NULL)
    473 		return (-1);
    474 	_nsmap = new;
    475 	/* _nsmapsize already incremented */
    476 
    477 	return (0);
    478 }
    479 
    480 /*
    481  * This function is called each time nsdispatch() is called.  If this
    482  * is the first call, or if the configuration has changed, (re-)prepare
    483  * the global data used by NSS.
    484  */
    485 static int
    486 _nsconfigure(void)
    487 {
    488 #ifdef _REENTRANT
    489 	static mutex_t	_nsconflock = MUTEX_INITIALIZER;
    490 #endif
    491 	static time_t	_nsconfmod;
    492 	struct stat	statbuf;
    493 
    494 	mutex_lock(&_nsconflock);
    495 
    496 	if (stat(_PATH_NS_CONF, &statbuf) == -1) {
    497 		/*
    498 		 * No nsswitch.conf; just use whatever configuration we
    499 		 * currently have, or fall back on the defaults specified
    500 		 * by the caller.
    501 		 */
    502 		mutex_unlock(&_nsconflock);
    503 		return (0);
    504 	}
    505 
    506 	if (statbuf.st_mtime <= _nsconfmod) {
    507 		/* Internal state is up-to-date with nsswitch.conf. */
    508 		mutex_unlock(&_nsconflock);
    509 		return (0);
    510 	}
    511 
    512 	/*
    513 	 * Ok, we've decided we need to update the nsswitch configuration
    514 	 * structures.  Acquire a write-lock on _nslock while continuing
    515 	 * to hold _nsconflock.  Acquiring a write-lock blocks while
    516 	 * waiting for other threads already holding a read-lock to clear.
    517 	 * We hold _nsconflock for the duration, and update the time stamp
    518 	 * at the end of the update operation, at which time we release
    519 	 * both locks.
    520 	 */
    521 	rwlock_wrlock(&_nslock);
    522 
    523 	_nsyyin = fopen(_PATH_NS_CONF, "r");
    524 	if (_nsyyin == NULL) {
    525 		/*
    526 		 * Unable to open nsswitch.conf; behave as though the
    527 		 * stat() above failed.  Even though we have already
    528 		 * updated _nsconfmod, if the file reappears, the
    529 		 * mtime will change.
    530 		 */
    531 		goto out;
    532 	}
    533 
    534 	_NSVECT_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
    535 	    (_nsvect_free_elem) _nsdbtfree);
    536 	_NSVECT_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
    537 	    (_nsvect_free_elem) _nsmodfree);
    538 
    539 	_nsloadbuiltin();
    540 
    541 	_nsyyparse();
    542 	(void) fclose(_nsyyin);
    543 	if (_nsmapsize != 0)
    544 		qsort(_nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp);
    545 
    546 	_nsconfmod = statbuf.st_mtime;
    547 
    548  out:
    549 	rwlock_unlock(&_nslock);
    550 	mutex_unlock(&_nsconflock);
    551 	return (0);
    552 }
    553 
    554 static nss_method
    555 _nsmethod(const char *source, const char *database, const char *method,
    556     const ns_dtab disp_tab[], void **cb_data)
    557 {
    558 	int	curdisp;
    559 	ns_mod	*mod, modkey;
    560 	ns_mtab	*mtab, mtabkey;
    561 
    562 	if (disp_tab != NULL) {
    563 		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) {
    564 			if (strcasecmp(source, disp_tab[curdisp].src) == 0) {
    565 				*cb_data = disp_tab[curdisp].cb_data;
    566 				return (disp_tab[curdisp].callback);
    567 			}
    568 		}
    569 	}
    570 
    571 	modkey.name = source;
    572 	mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod),
    573 	    _nsmodcmp);
    574 	if (mod != NULL && mod->handle != NULL) {
    575 		mtabkey.database = database;
    576 		mtabkey.name = method;
    577 		mtab = bsearch(&mtabkey, mod->mtab, mod->mtabsize,
    578 		    sizeof(mod->mtab[0]), _nsmtabcmp);
    579 		if (mtab != NULL) {
    580 			*cb_data = mtab->mdata;
    581 			return (mtab->method);
    582 		}
    583 	}
    584 
    585 	*cb_data = NULL;
    586 	return (NULL);
    587 }
    588 
    589 int
    590 /*ARGSUSED*/
    591 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
    592 	    const char *method, const ns_src defaults[], ...)
    593 {
    594 	static int	 _nsdispatching;
    595 #ifdef _REENTRANT
    596 	struct _ns_drec	 drec, *ldrec;
    597 #endif
    598 	va_list		 ap;
    599 	int		 i, result;
    600 	ns_dbt		 key;
    601 	const ns_dbt	*dbt;
    602 	const ns_src	*srclist;
    603 	int		 srclistsize;
    604 	nss_method	 cb;
    605 	void		*cb_data;
    606 
    607 	/* retval may be NULL */
    608 	/* disp_tab may be NULL */
    609 	_DIAGASSERT(database != NULL);
    610 	_DIAGASSERT(method != NULL);
    611 	_DIAGASSERT(defaults != NULL);
    612 	if (database == NULL || method == NULL || defaults == NULL)
    613 		return (NS_UNAVAIL);
    614 
    615 	/*
    616 	 * In both the threaded and non-threaded cases, avoid reloading
    617 	 * the configuration if the current thread is already running
    618 	 * nsdispatch() (i.e. recursive call).
    619 	 *
    620 	 * In the non-threaded case, this avoids changing the data structures
    621 	 * while we're using them.
    622 	 *
    623 	 * In the threaded case, this avoids trying to take a write lock
    624 	 * while the current thread holds a read lock (which would result
    625 	 * in deadlock).
    626 	 */
    627 #ifdef _REENTRANT
    628 	if (__isthreaded) {
    629 		drec.thr = thr_self();
    630 		mutex_lock(&_ns_drec_lock);
    631 		LIST_FOREACH(ldrec, &_ns_drec, list) {
    632 			if (ldrec->thr == drec.thr)
    633 				break;
    634 		}
    635 		LIST_INSERT_HEAD(&_ns_drec, &drec, list);
    636 		mutex_unlock(&_ns_drec_lock);
    637 		if (ldrec == NULL && _nsconfigure()) {
    638 			mutex_lock(&_ns_drec_lock);
    639 			LIST_REMOVE(&drec, list);
    640 			mutex_unlock(&_ns_drec_lock);
    641 			return (NS_UNAVAIL);
    642 		}
    643 	} else
    644 #endif /* _REENTRANT */
    645 	if (_nsdispatching++ == 0 && _nsconfigure()) {
    646 		_nsdispatching--;
    647 		return (NS_UNAVAIL);
    648 	}
    649 
    650 	rwlock_rdlock(&_nslock);
    651 
    652 	key.name = database;
    653 	dbt = bsearch(&key, _nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp);
    654 	if (dbt != NULL) {
    655 		srclist = dbt->srclist;
    656 		srclistsize = dbt->srclistsize;
    657 	} else {
    658 		srclist = defaults;
    659 		srclistsize = 0;
    660 		while (srclist[srclistsize].name != NULL)
    661 			srclistsize++;
    662 	}
    663 	result = 0;
    664 
    665 	for (i = 0; i < srclistsize; i++) {
    666 		cb = _nsmethod(srclist[i].name, database, method,
    667 		    disp_tab, &cb_data);
    668 		result = 0;
    669 		if (cb != NULL) {
    670 			va_start(ap, defaults);
    671 			result = (*cb)(retval, cb_data, ap);
    672 			va_end(ap);
    673 			if (defaults[0].flags & NS_FORCEALL)
    674 				continue;
    675 			if (result & srclist[i].flags)
    676 				break;
    677 		}
    678 	}
    679 	result &= NS_STATUSMASK;	/* clear private flags in result */
    680 
    681 	rwlock_unlock(&_nslock);
    682 
    683 #ifdef _REENTRANT
    684 	if (__isthreaded) {
    685 		mutex_lock(&_ns_drec_lock);
    686 		LIST_REMOVE(&drec, list);
    687 		mutex_unlock(&_ns_drec_lock);
    688 	} else
    689 #endif /* _REENTRANT */
    690 		_nsdispatching--;
    691 
    692 	return (result ? result : NS_NOTFOUND);
    693 }
    694