Home | History | Annotate | Line # | Download | only in net
nsdispatch.c revision 1.35
      1 /*	$NetBSD: nsdispatch.c,v 1.35 2011/02/18 23:41:57 joerg 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.35 2011/02/18 23:41:57 joerg 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 #ifndef __ELF__
    176 #define	is_dynamic()		(0)	/* don't bother - switch to ELF! */
    177 #elif __GNUC_PREREQ__(4,2)
    178 static int rtld_DYNAMIC __attribute__((__weakref__, __alias__("_DYNAMIC")));
    179 #define	is_dynamic()		(&rtld_DYNAMIC != NULL)
    180 #else
    181 extern int _DYNAMIC __weak_reference(_DYNAMIC);
    182 #define	is_dynamic()		(&_DYNAMIC != NULL)
    183 #endif
    184 
    185 
    186 /*
    187  * size of dynamic array chunk for _nsmap and _nsmap[x].srclist (and other
    188  * growing arrays).
    189  */
    190 #define NSELEMSPERCHUNK		8
    191 
    192 /*
    193  * Dynamically growable arrays are used for lists of databases, sources,
    194  * and modules.  The following "vector" API is used to isolate the
    195  * common operations.
    196  */
    197 typedef void	(*_nsvect_free_elem)(void *);
    198 
    199 static void *
    200 _nsvect_append(const void *elem, void *vec, u_int *count, size_t esize)
    201 {
    202 	void	*p;
    203 
    204 	if ((*count % NSELEMSPERCHUNK) == 0) {
    205 		p = realloc(vec, (*count + NSELEMSPERCHUNK) * esize);
    206 		if (p == NULL)
    207 			return (NULL);
    208 		vec = p;
    209 	}
    210 	memmove((void *)(((uintptr_t)vec) + (*count * esize)), elem, esize);
    211 	(*count)++;
    212 	return (vec);
    213 }
    214 
    215 static void *
    216 _nsvect_elem(u_int i, void *vec, u_int count, size_t esize)
    217 {
    218 
    219 	if (i < count)
    220 		return ((void *)((uintptr_t)vec + (i * esize)));
    221 	else
    222 		return (NULL);
    223 }
    224 
    225 static void
    226 _nsvect_free(void *vec, u_int *count, size_t esize, _nsvect_free_elem free_elem)
    227 {
    228 	void	*elem;
    229 	u_int	 i;
    230 
    231 	for (i = 0; i < *count; i++) {
    232 		elem = _nsvect_elem(i, vec, *count, esize);
    233 		if (elem != NULL)
    234 			(*free_elem)(elem);
    235 	}
    236 	if (vec != NULL)
    237 		free(vec);
    238 	*count = 0;
    239 }
    240 #define	_NSVECT_FREE(v, c, s, f)					\
    241 do {									\
    242 	_nsvect_free((v), (c), (s), (f));				\
    243 	(v) = NULL;							\
    244 } while (/*CONSTCOND*/0)
    245 
    246 static int
    247 _nsdbtcmp(const void *a, const void *b)
    248 {
    249 
    250 	return (strcasecmp(((const ns_dbt *)a)->name,
    251 	    ((const ns_dbt *)b)->name));
    252 }
    253 
    254 static int
    255 _nsmodcmp(const void *a, const void *b)
    256 {
    257 
    258 	return (strcasecmp(((const ns_mod *)a)->name,
    259 	    ((const ns_mod *)b)->name));
    260 }
    261 
    262 static int
    263 _nsmtabcmp(const void *a, const void *b)
    264 {
    265 	int	cmp;
    266 
    267 	cmp = strcmp(((const ns_mtab *)a)->name,
    268 	    ((const ns_mtab *)b)->name);
    269 	if (cmp)
    270 		return (cmp);
    271 
    272 	return (strcasecmp(((const ns_mtab *)a)->database,
    273 	    ((const ns_mtab *)b)->database));
    274 }
    275 
    276 static void
    277 _nsmodfree(ns_mod *mod)
    278 {
    279 
    280 	free(__UNCONST(mod->name));
    281 	if (mod->handle == NULL)
    282 		return;
    283 	if (mod->unregister != NULL)
    284 		(*mod->unregister)(mod->mtab, mod->mtabsize);
    285 #ifdef __ELF__
    286 	if (mod->handle != _nsbuiltin)
    287 		(void) dlclose(mod->handle);
    288 #endif /* __ELF__ */
    289 }
    290 
    291 /*
    292  * Load a built-in or dyanamically linked module.  If the `reg_fn'
    293  * argument is non-NULL, assume a built-in module and use `reg_fn'
    294  * to register it.  Otherwise, search for a dynamic nsswitch module.
    295  */
    296 static int
    297 _nsloadmod(const char *source, nss_module_register_fn reg_fn)
    298 {
    299 #ifdef __ELF__
    300 	char	buf[PATH_MAX];
    301 #endif
    302 	ns_mod	mod, *new;
    303 
    304 	memset(&mod, 0, sizeof(mod));
    305 	mod.name = strdup(source);
    306 	if (mod.name == NULL)
    307 		return (-1);
    308 
    309 	if (reg_fn != NULL) {
    310 		/*
    311 		 * The placeholder is required, as a NULL handle
    312 		 * represents an invalid module.
    313 		 */
    314 		mod.handle = _nsbuiltin;
    315 	} else if (!is_dynamic()) {
    316 		goto out;
    317 	} else {
    318 #ifdef __ELF__
    319 		if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name,
    320 		    NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf))
    321 			goto out;
    322 		mod.handle = dlopen(buf, RTLD_LOCAL | RTLD_LAZY);
    323 		if (mod.handle == NULL) {
    324 #ifdef _NSS_DEBUG
    325 			/*
    326 			 * This gets pretty annoying, since the built-in
    327 			 * sources are not yet modules.
    328 			 */
    329 			/* XXX log some error? */
    330 #endif
    331 			goto out;
    332 		}
    333 		reg_fn = (nss_module_register_fn) dlsym(mod.handle,
    334 		    "nss_module_register");
    335 		if (reg_fn == NULL) {
    336 			(void) dlclose(mod.handle);
    337 			mod.handle = NULL;
    338 			/* XXX log some error? */
    339 			goto out;
    340 		}
    341 #else /* ! __ELF__ */
    342 		mod.handle = NULL;
    343 #endif /* __ELF__ */
    344 	}
    345 	mod.mtab = (*reg_fn)(mod.name, &mod.mtabsize, &mod.unregister);
    346 	if (mod.mtab == NULL || mod.mtabsize == 0) {
    347 #ifdef __ELF__
    348 		if (mod.handle != _nsbuiltin)
    349 			(void) dlclose(mod.handle);
    350 #endif /* __ELF__ */
    351 		mod.handle = NULL;
    352 		/* XXX log some error? */
    353 		goto out;
    354 	}
    355 	if (mod.mtabsize > 1)
    356 		qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]),
    357 		    _nsmtabcmp);
    358  out:
    359 	new = _nsvect_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod));
    360 	if (new == NULL) {
    361 		_nsmodfree(&mod);
    362 		return (-1);
    363 	}
    364 	_nsmod = new;
    365 	/* _nsmodsize already incremented */
    366 
    367 	qsort(_nsmod, _nsmodsize, sizeof(*_nsmod), _nsmodcmp);
    368 	return (0);
    369 }
    370 
    371 static void
    372 _nsloadbuiltin(void)
    373 {
    374 
    375 	/* Do nothing, for now. */
    376 }
    377 
    378 int
    379 _nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
    380 {
    381 	void		*new;
    382 	const ns_mod	*mod;
    383 	ns_mod		 modkey;
    384 
    385 	_DIAGASSERT(dbt != NULL);
    386 	_DIAGASSERT(src != NULL);
    387 
    388 	new = _nsvect_append(src, dbt->srclist, &dbt->srclistsize,
    389 	    sizeof(*src));
    390 	if (new == NULL)
    391 		return (-1);
    392 	dbt->srclist = new;
    393 	/* dbt->srclistsize already incremented */
    394 
    395 	modkey.name = src->name;
    396 	mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod),
    397 	    _nsmodcmp);
    398 	if (mod == NULL)
    399 		return (_nsloadmod(src->name, NULL));
    400 
    401 	return (0);
    402 }
    403 
    404 void
    405 _nsdbtdump(const ns_dbt *dbt)
    406 {
    407 	unsigned int	i;
    408 
    409 	_DIAGASSERT(dbt != NULL);
    410 
    411 	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
    412 	    dbt->srclistsize == 1 ? "" : "s");
    413 	for (i = 0; i < dbt->srclistsize; i++) {
    414 		printf(" %s", dbt->srclist[i].name);
    415 		if (!(dbt->srclist[i].flags &
    416 		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
    417 		    (dbt->srclist[i].flags & NS_SUCCESS))
    418 			continue;
    419 		printf(" [");
    420 		if (!(dbt->srclist[i].flags & NS_SUCCESS))
    421 			printf(" SUCCESS=continue");
    422 		if (dbt->srclist[i].flags & NS_UNAVAIL)
    423 			printf(" UNAVAIL=return");
    424 		if (dbt->srclist[i].flags & NS_NOTFOUND)
    425 			printf(" NOTFOUND=return");
    426 		if (dbt->srclist[i].flags & NS_TRYAGAIN)
    427 			printf(" TRYAGAIN=return");
    428 		printf(" ]");
    429 	}
    430 	printf("\n");
    431 }
    432 
    433 static void
    434 _nssrclist_free(ns_src **src, u_int srclistsize)
    435 {
    436 	u_int	i;
    437 
    438 	for (i = 0; i < srclistsize; i++) {
    439 		if ((*src)[i].name != NULL)
    440 			free(__UNCONST((*src)[i].name));
    441 	}
    442 	free(*src);
    443 	*src = NULL;
    444 }
    445 
    446 static void
    447 _nsdbtfree(ns_dbt *dbt)
    448 {
    449 
    450 	_nssrclist_free(&dbt->srclist, dbt->srclistsize);
    451 	if (dbt->name != NULL)
    452 		free(__UNCONST(dbt->name));
    453 }
    454 
    455 int
    456 _nsdbtput(const ns_dbt *dbt)
    457 {
    458 	ns_dbt	*p;
    459 	void	*new;
    460 	u_int	i;
    461 
    462 	_DIAGASSERT(dbt != NULL);
    463 
    464 	for (i = 0; i < _nsmapsize; i++) {
    465 		p = _nsvect_elem(i, _nsmap, _nsmapsize, sizeof(*_nsmap));
    466 		if (strcasecmp(dbt->name, p->name) == 0) {
    467 					/* overwrite existing entry */
    468 			if (p->srclist != NULL)
    469 				_nssrclist_free(&p->srclist, p->srclistsize);
    470 			memmove(p, dbt, sizeof(*dbt));
    471 			return (0);
    472 		}
    473 	}
    474 	new = _nsvect_append(dbt, _nsmap, &_nsmapsize, sizeof(*_nsmap));
    475 	if (new == NULL)
    476 		return (-1);
    477 	_nsmap = new;
    478 	/* _nsmapsize already incremented */
    479 
    480 	return (0);
    481 }
    482 
    483 /*
    484  * This function is called each time nsdispatch() is called.  If this
    485  * is the first call, or if the configuration has changed, (re-)prepare
    486  * the global data used by NSS.
    487  */
    488 static int
    489 _nsconfigure(void)
    490 {
    491 #ifdef _REENTRANT
    492 	static mutex_t	_nsconflock = MUTEX_INITIALIZER;
    493 #endif
    494 	static time_t	_nsconfmod;
    495 	struct stat	statbuf;
    496 
    497 	mutex_lock(&_nsconflock);
    498 
    499 	if (stat(_PATH_NS_CONF, &statbuf) == -1) {
    500 		/*
    501 		 * No nsswitch.conf; just use whatever configuration we
    502 		 * currently have, or fall back on the defaults specified
    503 		 * by the caller.
    504 		 */
    505 		mutex_unlock(&_nsconflock);
    506 		return (0);
    507 	}
    508 
    509 	if (statbuf.st_mtime <= _nsconfmod) {
    510 		/* Internal state is up-to-date with nsswitch.conf. */
    511 		mutex_unlock(&_nsconflock);
    512 		return (0);
    513 	}
    514 
    515 	/*
    516 	 * Ok, we've decided we need to update the nsswitch configuration
    517 	 * structures.  Acquire a write-lock on _nslock while continuing
    518 	 * to hold _nsconflock.  Acquiring a write-lock blocks while
    519 	 * waiting for other threads already holding a read-lock to clear.
    520 	 * We hold _nsconflock for the duration, and update the time stamp
    521 	 * at the end of the update operation, at which time we release
    522 	 * both locks.
    523 	 */
    524 	rwlock_wrlock(&_nslock);
    525 
    526 	_nsyyin = fopen(_PATH_NS_CONF, "r");
    527 	if (_nsyyin == NULL) {
    528 		/*
    529 		 * Unable to open nsswitch.conf; behave as though the
    530 		 * stat() above failed.  Even though we have already
    531 		 * updated _nsconfmod, if the file reappears, the
    532 		 * mtime will change.
    533 		 */
    534 		goto out;
    535 	}
    536 
    537 	_NSVECT_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
    538 	    (_nsvect_free_elem) _nsdbtfree);
    539 	_NSVECT_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
    540 	    (_nsvect_free_elem) _nsmodfree);
    541 
    542 	_nsloadbuiltin();
    543 
    544 	_nsyyparse();
    545 	(void) fclose(_nsyyin);
    546 	if (_nsmapsize != 0)
    547 		qsort(_nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp);
    548 
    549 	_nsconfmod = statbuf.st_mtime;
    550 
    551  out:
    552 	rwlock_unlock(&_nslock);
    553 	mutex_unlock(&_nsconflock);
    554 	return (0);
    555 }
    556 
    557 static nss_method
    558 _nsmethod(const char *source, const char *database, const char *method,
    559     const ns_dtab disp_tab[], void **cb_data)
    560 {
    561 	int	curdisp;
    562 	ns_mod	*mod, modkey;
    563 	ns_mtab	*mtab, mtabkey;
    564 
    565 	if (disp_tab != NULL) {
    566 		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) {
    567 			if (strcasecmp(source, disp_tab[curdisp].src) == 0) {
    568 				*cb_data = disp_tab[curdisp].cb_data;
    569 				return (disp_tab[curdisp].callback);
    570 			}
    571 		}
    572 	}
    573 
    574 	modkey.name = source;
    575 	mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod),
    576 	    _nsmodcmp);
    577 	if (mod != NULL && mod->handle != NULL) {
    578 		mtabkey.database = database;
    579 		mtabkey.name = method;
    580 		mtab = bsearch(&mtabkey, mod->mtab, mod->mtabsize,
    581 		    sizeof(mod->mtab[0]), _nsmtabcmp);
    582 		if (mtab != NULL) {
    583 			*cb_data = mtab->mdata;
    584 			return (mtab->method);
    585 		}
    586 	}
    587 
    588 	*cb_data = NULL;
    589 	return (NULL);
    590 }
    591 
    592 int
    593 /*ARGSUSED*/
    594 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
    595 	    const char *method, const ns_src defaults[], ...)
    596 {
    597 	static int	 _nsdispatching;
    598 #ifdef _REENTRANT
    599 	struct _ns_drec	 drec, *ldrec;
    600 #endif
    601 	va_list		 ap;
    602 	int		 i, result;
    603 	ns_dbt		 key;
    604 	const ns_dbt	*dbt;
    605 	const ns_src	*srclist;
    606 	int		 srclistsize;
    607 	nss_method	 cb;
    608 	void		*cb_data;
    609 
    610 	/* retval may be NULL */
    611 	/* disp_tab may be NULL */
    612 	_DIAGASSERT(database != NULL);
    613 	_DIAGASSERT(method != NULL);
    614 	_DIAGASSERT(defaults != NULL);
    615 	if (database == NULL || method == NULL || defaults == NULL)
    616 		return (NS_UNAVAIL);
    617 
    618 	/*
    619 	 * In both the threaded and non-threaded cases, avoid reloading
    620 	 * the configuration if the current thread is already running
    621 	 * nsdispatch() (i.e. recursive call).
    622 	 *
    623 	 * In the non-threaded case, this avoids changing the data structures
    624 	 * while we're using them.
    625 	 *
    626 	 * In the threaded case, this avoids trying to take a write lock
    627 	 * while the current thread holds a read lock (which would result
    628 	 * in deadlock).
    629 	 */
    630 #ifdef _REENTRANT
    631 	if (__isthreaded) {
    632 		drec.thr = thr_self();
    633 		mutex_lock(&_ns_drec_lock);
    634 		LIST_FOREACH(ldrec, &_ns_drec, list) {
    635 			if (ldrec->thr == drec.thr)
    636 				break;
    637 		}
    638 		LIST_INSERT_HEAD(&_ns_drec, &drec, list);
    639 		mutex_unlock(&_ns_drec_lock);
    640 		if (ldrec == NULL && _nsconfigure()) {
    641 			mutex_lock(&_ns_drec_lock);
    642 			LIST_REMOVE(&drec, list);
    643 			mutex_unlock(&_ns_drec_lock);
    644 			return (NS_UNAVAIL);
    645 		}
    646 	} else
    647 #endif /* _REENTRANT */
    648 	if (_nsdispatching++ == 0 && _nsconfigure()) {
    649 		_nsdispatching--;
    650 		return (NS_UNAVAIL);
    651 	}
    652 
    653 	rwlock_rdlock(&_nslock);
    654 
    655 	key.name = database;
    656 	dbt = bsearch(&key, _nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp);
    657 	if (dbt != NULL) {
    658 		srclist = dbt->srclist;
    659 		srclistsize = dbt->srclistsize;
    660 	} else {
    661 		srclist = defaults;
    662 		srclistsize = 0;
    663 		while (srclist[srclistsize].name != NULL)
    664 			srclistsize++;
    665 	}
    666 	result = 0;
    667 
    668 	for (i = 0; i < srclistsize; i++) {
    669 		cb = _nsmethod(srclist[i].name, database, method,
    670 		    disp_tab, &cb_data);
    671 		result = 0;
    672 		if (cb != NULL) {
    673 			va_start(ap, defaults);
    674 			result = (*cb)(retval, cb_data, ap);
    675 			va_end(ap);
    676 			if (defaults[0].flags & NS_FORCEALL)
    677 				continue;
    678 			if (result & srclist[i].flags)
    679 				break;
    680 		}
    681 	}
    682 	result &= NS_STATUSMASK;	/* clear private flags in result */
    683 
    684 	rwlock_unlock(&_nslock);
    685 
    686 #ifdef _REENTRANT
    687 	if (__isthreaded) {
    688 		mutex_lock(&_ns_drec_lock);
    689 		LIST_REMOVE(&drec, list);
    690 		mutex_unlock(&_ns_drec_lock);
    691 	} else
    692 #endif /* _REENTRANT */
    693 		_nsdispatching--;
    694 
    695 	return (result ? result : NS_NOTFOUND);
    696 }
    697