xref: /netbsd-src/lib/libc/net/nsdispatch.c (revision 388550b026d49b7f7b7480b1113bf82bb8d6a480)
1*388550b0Srillig /*	$NetBSD: nsdispatch.c,v 1.39 2022/04/19 20:32:15 rillig Exp $	*/
2de3b78d7Slukem 
3de3b78d7Slukem /*-
443d6d8d8Sthorpej  * Copyright (c) 1997, 1998, 1999, 2004 The NetBSD Foundation, Inc.
5de3b78d7Slukem  * All rights reserved.
6de3b78d7Slukem  *
7de3b78d7Slukem  * This code is derived from software contributed to The NetBSD Foundation
843d6d8d8Sthorpej  * by Luke Mewburn; and by Jason R. Thorpe.
9de3b78d7Slukem  *
10de3b78d7Slukem  * Redistribution and use in source and binary forms, with or without
11de3b78d7Slukem  * modification, are permitted provided that the following conditions
12de3b78d7Slukem  * are met:
13de3b78d7Slukem  * 1. Redistributions of source code must retain the above copyright
14de3b78d7Slukem  *    notice, this list of conditions and the following disclaimer.
15de3b78d7Slukem  * 2. Redistributions in binary form must reproduce the above copyright
16de3b78d7Slukem  *    notice, this list of conditions and the following disclaimer in the
17de3b78d7Slukem  *    documentation and/or other materials provided with the distribution.
18de3b78d7Slukem  *
19de3b78d7Slukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20de3b78d7Slukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21de3b78d7Slukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22de3b78d7Slukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23de3b78d7Slukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24de3b78d7Slukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25de3b78d7Slukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26de3b78d7Slukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27de3b78d7Slukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28de3b78d7Slukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29de3b78d7Slukem  * POSSIBILITY OF SUCH DAMAGE.
30de3b78d7Slukem  */
31de3b78d7Slukem 
3243d6d8d8Sthorpej /*-
3343d6d8d8Sthorpej  * Copyright (c) 2003 Networks Associates Technology, Inc.
3443d6d8d8Sthorpej  * All rights reserved.
3543d6d8d8Sthorpej  *
3643d6d8d8Sthorpej  * Portions of this software were developed for the FreeBSD Project by
3743d6d8d8Sthorpej  * Jacques A. Vidrine, Safeport Network Services, and Network
3843d6d8d8Sthorpej  * Associates Laboratories, the Security Research Division of Network
3943d6d8d8Sthorpej  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
4043d6d8d8Sthorpej  * ("CBOSS"), as part of the DARPA CHATS research program.
4143d6d8d8Sthorpej  *
4243d6d8d8Sthorpej  * Redistribution and use in source and binary forms, with or without
4343d6d8d8Sthorpej  * modification, are permitted provided that the following conditions
4443d6d8d8Sthorpej  * are met:
4543d6d8d8Sthorpej  * 1. Redistributions of source code must retain the above copyright
4643d6d8d8Sthorpej  *    notice, this list of conditions and the following disclaimer.
4743d6d8d8Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
4843d6d8d8Sthorpej  *    notice, this list of conditions and the following disclaimer in the
4943d6d8d8Sthorpej  *    documentation and/or other materials provided with the distribution.
5043d6d8d8Sthorpej  *
5143d6d8d8Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
5243d6d8d8Sthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5343d6d8d8Sthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5443d6d8d8Sthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
5543d6d8d8Sthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5643d6d8d8Sthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5743d6d8d8Sthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5843d6d8d8Sthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5943d6d8d8Sthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6043d6d8d8Sthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6143d6d8d8Sthorpej  * SUCH DAMAGE.
6243d6d8d8Sthorpej  */
6343d6d8d8Sthorpej 
64645b10c9Slukem #include <sys/cdefs.h>
65645b10c9Slukem #if defined(LIBC_SCCS) && !defined(lint)
66*388550b0Srillig __RCSID("$NetBSD: nsdispatch.c,v 1.39 2022/04/19 20:32:15 rillig Exp $");
67645b10c9Slukem #endif /* LIBC_SCCS and not lint */
68645b10c9Slukem 
69c8851d63Slukem #include "namespace.h"
70c8851d63Slukem 
71de3b78d7Slukem #include <sys/types.h>
72de3b78d7Slukem #include <sys/param.h>
73de3b78d7Slukem #include <sys/stat.h>
74a2bbd451Sthorpej #include <sys/queue.h>
75de3b78d7Slukem 
76b48252f3Slukem #include <assert.h>
7743d6d8d8Sthorpej #ifdef __ELF__
7843d6d8d8Sthorpej #include <dlfcn.h>
7943d6d8d8Sthorpej #endif /* __ELF__ */
80de3b78d7Slukem #include <err.h>
81de3b78d7Slukem #include <fcntl.h>
82de3b78d7Slukem #define _NS_PRIVATE
83de3b78d7Slukem #include <nsswitch.h>
84e3e4fedeSwiz #include <stdarg.h>
85de3b78d7Slukem #include <stdio.h>
86de3b78d7Slukem #include <stdlib.h>
87de3b78d7Slukem #include <string.h>
88de3b78d7Slukem #include <unistd.h>
8943d6d8d8Sthorpej 
9043d6d8d8Sthorpej #include "reentrant.h"
91de3b78d7Slukem 
92663f790fSchristos extern	FILE 	*_nsyyin;
93b8b64b05Sthorpej extern	int	 _nsyyparse(void);
94663f790fSchristos 
95663f790fSchristos 
96c8851d63Slukem #ifdef __weak_alias
9760549036Smycroft __weak_alias(nsdispatch,_nsdispatch)
98c8851d63Slukem #endif
99c8851d63Slukem 
100de3b78d7Slukem 
10137df6720Slukem /*
10237df6720Slukem  * default sourcelist: `files'
10337df6720Slukem  */
10437df6720Slukem const ns_src __nsdefaultsrc[] = {
10537df6720Slukem 	{ NSSRC_FILES,	NS_SUCCESS },
106ce2c90c7Schristos 	{ 0, 0 },
10737df6720Slukem };
10837df6720Slukem 
1095994f053Slukem const ns_src __nsdefaultcompat[] = {
1105994f053Slukem 	{ NSSRC_COMPAT,	NS_SUCCESS },
111ce2c90c7Schristos 	{ 0, 0 }
1125994f053Slukem };
1135994f053Slukem 
1145994f053Slukem const ns_src __nsdefaultcompat_forceall[] = {
1155994f053Slukem 	{ NSSRC_COMPAT,	NS_SUCCESS | NS_FORCEALL },
116ce2c90c7Schristos 	{ 0, 0 }
1175994f053Slukem };
1185994f053Slukem 
1195994f053Slukem const ns_src __nsdefaultfiles[] = {
1205994f053Slukem 	{ NSSRC_FILES,	NS_SUCCESS },
121ce2c90c7Schristos 	{ 0, 0 },
1225994f053Slukem };
1235994f053Slukem 
1245994f053Slukem const ns_src __nsdefaultfiles_forceall[] = {
1255994f053Slukem 	{ NSSRC_FILES,	NS_SUCCESS | NS_FORCEALL },
126ce2c90c7Schristos 	{ 0, 0 },
1275994f053Slukem };
1285994f053Slukem 
1295994f053Slukem const ns_src __nsdefaultnis[] = {
1305994f053Slukem 	{ NSSRC_NIS,	NS_SUCCESS },
131ce2c90c7Schristos 	{ 0, 0 }
1325994f053Slukem };
1335994f053Slukem 
1345994f053Slukem const ns_src __nsdefaultnis_forceall[] = {
1355994f053Slukem 	{ NSSRC_NIS,	NS_SUCCESS | NS_FORCEALL },
136ce2c90c7Schristos 	{ 0, 0 }
1375994f053Slukem };
1385994f053Slukem 
1395994f053Slukem 
14043d6d8d8Sthorpej /* Database, source mappings. */
14143d6d8d8Sthorpej static	u_int			 _nsmapsize;
14243d6d8d8Sthorpej static	ns_dbt			*_nsmap;
14337df6720Slukem 
14443d6d8d8Sthorpej /* Nsswitch modules. */
14543d6d8d8Sthorpej static	u_int			 _nsmodsize;
14643d6d8d8Sthorpej static	ns_mod			*_nsmod;
14743d6d8d8Sthorpej 
14843d6d8d8Sthorpej /* Placeholder for built-in modules' dlopen() handles. */
14943d6d8d8Sthorpej static	void			*_nsbuiltin = &_nsbuiltin;
15043d6d8d8Sthorpej 
15194564d94Schristos #ifdef _REENTRANT
15243d6d8d8Sthorpej /*
15343d6d8d8Sthorpej  * Global nsswitch data structures are mostly read-only, but we update them
15443d6d8d8Sthorpej  * when we read or re-read nsswitch.conf.
15543d6d8d8Sthorpej  */
15643d6d8d8Sthorpej static 	rwlock_t		_nslock = RWLOCK_INITIALIZER;
157a2bbd451Sthorpej 
158a2bbd451Sthorpej /*
159a2bbd451Sthorpej  * List of threads currently in nsdispatch().  We use this to detect
160a2bbd451Sthorpej  * recursive calls and avoid reloading configuration in such cases,
161a2bbd451Sthorpej  * which could cause deadlock.
162a2bbd451Sthorpej  */
163a2bbd451Sthorpej struct _ns_drec {
164a2bbd451Sthorpej 	LIST_ENTRY(_ns_drec)	list;
165a2bbd451Sthorpej 	thr_t			thr;
166a2bbd451Sthorpej };
167a2bbd451Sthorpej static LIST_HEAD(, _ns_drec) _ns_drec = LIST_HEAD_INITIALIZER(&_ns_drec);
168a2bbd451Sthorpej static mutex_t _ns_drec_lock = MUTEX_INITIALIZER;
169a2bbd451Sthorpej #endif /* _REENTRANT */
17094564d94Schristos 
171de3b78d7Slukem 
172de3b78d7Slukem /*
17343d6d8d8Sthorpej  * Runtime determination of whether we are dynamically linked or not.
17443d6d8d8Sthorpej  */
175e189b01aSjoerg #ifndef __ELF__
176e189b01aSjoerg #define	is_dynamic()		(0)	/* don't bother - switch to ELF! */
177e189b01aSjoerg #else
1789674b81eSjoerg __weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
1799674b81eSjoerg #define	is_dynamic()		(&rtld_DYNAMIC != NULL)
180e189b01aSjoerg #endif
18143d6d8d8Sthorpej 
18243d6d8d8Sthorpej 
18343d6d8d8Sthorpej /*
18443d6d8d8Sthorpej  * size of dynamic array chunk for _nsmap and _nsmap[x].srclist (and other
18543d6d8d8Sthorpej  * growing arrays).
186de3b78d7Slukem  */
187de3b78d7Slukem #define NSELEMSPERCHUNK		8
188de3b78d7Slukem 
18943d6d8d8Sthorpej /*
19043d6d8d8Sthorpej  * Dynamically growable arrays are used for lists of databases, sources,
19143d6d8d8Sthorpej  * and modules.  The following "vector" API is used to isolate the
19243d6d8d8Sthorpej  * common operations.
19343d6d8d8Sthorpej  */
19443d6d8d8Sthorpej typedef void	(*_nsvect_free_elem)(void *);
19543d6d8d8Sthorpej 
19643d6d8d8Sthorpej static void *
_nsvect_append(const void * elem,void * vec,u_int * count,size_t esize)19743d6d8d8Sthorpej _nsvect_append(const void *elem, void *vec, u_int *count, size_t esize)
19843d6d8d8Sthorpej {
19943d6d8d8Sthorpej 	void	*p;
20043d6d8d8Sthorpej 
20143d6d8d8Sthorpej 	if ((*count % NSELEMSPERCHUNK) == 0) {
20243d6d8d8Sthorpej 		p = realloc(vec, (*count + NSELEMSPERCHUNK) * esize);
20343d6d8d8Sthorpej 		if (p == NULL)
20443d6d8d8Sthorpej 			return (NULL);
20543d6d8d8Sthorpej 		vec = p;
20643d6d8d8Sthorpej 	}
20743d6d8d8Sthorpej 	memmove((void *)(((uintptr_t)vec) + (*count * esize)), elem, esize);
20843d6d8d8Sthorpej 	(*count)++;
20943d6d8d8Sthorpej 	return (vec);
21043d6d8d8Sthorpej }
21143d6d8d8Sthorpej 
21243d6d8d8Sthorpej static void *
_nsvect_elem(u_int i,void * vec,u_int count,size_t esize)21343d6d8d8Sthorpej _nsvect_elem(u_int i, void *vec, u_int count, size_t esize)
21443d6d8d8Sthorpej {
21543d6d8d8Sthorpej 
21643d6d8d8Sthorpej 	if (i < count)
21743d6d8d8Sthorpej 		return ((void *)((uintptr_t)vec + (i * esize)));
21843d6d8d8Sthorpej 	else
21943d6d8d8Sthorpej 		return (NULL);
22043d6d8d8Sthorpej }
22143d6d8d8Sthorpej 
22243d6d8d8Sthorpej static void
_nsvect_free(void * vec,u_int * count,size_t esize,_nsvect_free_elem free_elem)22343d6d8d8Sthorpej _nsvect_free(void *vec, u_int *count, size_t esize, _nsvect_free_elem free_elem)
22443d6d8d8Sthorpej {
22543d6d8d8Sthorpej 	void	*elem;
22643d6d8d8Sthorpej 	u_int	 i;
22743d6d8d8Sthorpej 
22843d6d8d8Sthorpej 	for (i = 0; i < *count; i++) {
22943d6d8d8Sthorpej 		elem = _nsvect_elem(i, vec, *count, esize);
23043d6d8d8Sthorpej 		if (elem != NULL)
23143d6d8d8Sthorpej 			(*free_elem)(elem);
23243d6d8d8Sthorpej 	}
23343d6d8d8Sthorpej 	if (vec != NULL)
23443d6d8d8Sthorpej 		free(vec);
23543d6d8d8Sthorpej 	*count = 0;
23643d6d8d8Sthorpej }
23743d6d8d8Sthorpej #define	_NSVECT_FREE(v, c, s, f)					\
23843d6d8d8Sthorpej do {									\
23943d6d8d8Sthorpej 	_nsvect_free((v), (c), (s), (f));				\
24043d6d8d8Sthorpej 	(v) = NULL;							\
241*388550b0Srillig } while (0)
242de3b78d7Slukem 
243b8b64b05Sthorpej static int
_nsdbtcmp(const void * a,const void * b)24443d6d8d8Sthorpej _nsdbtcmp(const void *a, const void *b)
245de3b78d7Slukem {
24643d6d8d8Sthorpej 
2474e07a5c0Schristos 	return (strcasecmp(((const ns_dbt *)a)->name,
2484e07a5c0Schristos 	    ((const ns_dbt *)b)->name));
249de3b78d7Slukem }
250de3b78d7Slukem 
25143d6d8d8Sthorpej static int
_nsmodcmp(const void * a,const void * b)25243d6d8d8Sthorpej _nsmodcmp(const void *a, const void *b)
25343d6d8d8Sthorpej {
25443d6d8d8Sthorpej 
25543d6d8d8Sthorpej 	return (strcasecmp(((const ns_mod *)a)->name,
25643d6d8d8Sthorpej 	    ((const ns_mod *)b)->name));
25743d6d8d8Sthorpej }
25843d6d8d8Sthorpej 
25943d6d8d8Sthorpej static int
_nsmtabcmp(const void * a,const void * b)26043d6d8d8Sthorpej _nsmtabcmp(const void *a, const void *b)
26143d6d8d8Sthorpej {
26243d6d8d8Sthorpej 	int	cmp;
26343d6d8d8Sthorpej 
26443d6d8d8Sthorpej 	cmp = strcmp(((const ns_mtab *)a)->name,
26543d6d8d8Sthorpej 	    ((const ns_mtab *)b)->name);
26643d6d8d8Sthorpej 	if (cmp)
26743d6d8d8Sthorpej 		return (cmp);
26843d6d8d8Sthorpej 
26943d6d8d8Sthorpej 	return (strcasecmp(((const ns_mtab *)a)->database,
27043d6d8d8Sthorpej 	    ((const ns_mtab *)b)->database));
27143d6d8d8Sthorpej }
27243d6d8d8Sthorpej 
27343d6d8d8Sthorpej static void
_nsmodfree(ns_mod * mod)27443d6d8d8Sthorpej _nsmodfree(ns_mod *mod)
27543d6d8d8Sthorpej {
27643d6d8d8Sthorpej 
27703256c6eSchristos 	free(__UNCONST(mod->name));
27843d6d8d8Sthorpej 	if (mod->handle == NULL)
27943d6d8d8Sthorpej 		return;
28043d6d8d8Sthorpej 	if (mod->unregister != NULL)
28143d6d8d8Sthorpej 		(*mod->unregister)(mod->mtab, mod->mtabsize);
28243d6d8d8Sthorpej #ifdef __ELF__
28343d6d8d8Sthorpej 	if (mod->handle != _nsbuiltin)
28443d6d8d8Sthorpej 		(void) dlclose(mod->handle);
28543d6d8d8Sthorpej #endif /* __ELF__ */
28643d6d8d8Sthorpej }
28743d6d8d8Sthorpej 
28843d6d8d8Sthorpej /*
28943d6d8d8Sthorpej  * Load a built-in or dyanamically linked module.  If the `reg_fn'
29043d6d8d8Sthorpej  * argument is non-NULL, assume a built-in module and use `reg_fn'
29143d6d8d8Sthorpej  * to register it.  Otherwise, search for a dynamic nsswitch module.
29243d6d8d8Sthorpej  */
29343d6d8d8Sthorpej static int
_nsloadmod(const char * source,nss_module_register_fn reg_fn)29443d6d8d8Sthorpej _nsloadmod(const char *source, nss_module_register_fn reg_fn)
29543d6d8d8Sthorpej {
296965b11f6Ssimonb #ifdef __ELF__
29743d6d8d8Sthorpej 	char	buf[PATH_MAX];
298965b11f6Ssimonb #endif
29943d6d8d8Sthorpej 	ns_mod	mod, *new;
30043d6d8d8Sthorpej 
30143d6d8d8Sthorpej 	memset(&mod, 0, sizeof(mod));
30243d6d8d8Sthorpej 	mod.name = strdup(source);
30343d6d8d8Sthorpej 	if (mod.name == NULL)
30443d6d8d8Sthorpej 		return (-1);
30543d6d8d8Sthorpej 
30643d6d8d8Sthorpej 	if (reg_fn != NULL) {
30743d6d8d8Sthorpej 		/*
30843d6d8d8Sthorpej 		 * The placeholder is required, as a NULL handle
30943d6d8d8Sthorpej 		 * represents an invalid module.
31043d6d8d8Sthorpej 		 */
31143d6d8d8Sthorpej 		mod.handle = _nsbuiltin;
31243d6d8d8Sthorpej 	} else if (!is_dynamic()) {
31343d6d8d8Sthorpej 		goto out;
31443d6d8d8Sthorpej 	} else {
31543d6d8d8Sthorpej #ifdef __ELF__
31643d6d8d8Sthorpej 		if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name,
31743d6d8d8Sthorpej 		    NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf))
31843d6d8d8Sthorpej 			goto out;
31943d6d8d8Sthorpej 		mod.handle = dlopen(buf, RTLD_LOCAL | RTLD_LAZY);
32043d6d8d8Sthorpej 		if (mod.handle == NULL) {
32143d6d8d8Sthorpej #ifdef _NSS_DEBUG
32243d6d8d8Sthorpej 			/*
32343d6d8d8Sthorpej 			 * This gets pretty annoying, since the built-in
32443d6d8d8Sthorpej 			 * sources are not yet modules.
32543d6d8d8Sthorpej 			 */
32643d6d8d8Sthorpej 			/* XXX log some error? */
32743d6d8d8Sthorpej #endif
32843d6d8d8Sthorpej 			goto out;
32943d6d8d8Sthorpej 		}
33043d6d8d8Sthorpej 		reg_fn = (nss_module_register_fn) dlsym(mod.handle,
33143d6d8d8Sthorpej 		    "nss_module_register");
33243d6d8d8Sthorpej 		if (reg_fn == NULL) {
33343d6d8d8Sthorpej 			(void) dlclose(mod.handle);
33443d6d8d8Sthorpej 			mod.handle = NULL;
33543d6d8d8Sthorpej 			/* XXX log some error? */
33643d6d8d8Sthorpej 			goto out;
33743d6d8d8Sthorpej 		}
33843d6d8d8Sthorpej #else /* ! __ELF__ */
33943d6d8d8Sthorpej 		mod.handle = NULL;
34043d6d8d8Sthorpej #endif /* __ELF__ */
34143d6d8d8Sthorpej 	}
34243d6d8d8Sthorpej 	mod.mtab = (*reg_fn)(mod.name, &mod.mtabsize, &mod.unregister);
34343d6d8d8Sthorpej 	if (mod.mtab == NULL || mod.mtabsize == 0) {
34443d6d8d8Sthorpej #ifdef __ELF__
34543d6d8d8Sthorpej 		if (mod.handle != _nsbuiltin)
34643d6d8d8Sthorpej 			(void) dlclose(mod.handle);
34743d6d8d8Sthorpej #endif /* __ELF__ */
34843d6d8d8Sthorpej 		mod.handle = NULL;
34943d6d8d8Sthorpej 		/* XXX log some error? */
35043d6d8d8Sthorpej 		goto out;
35143d6d8d8Sthorpej 	}
35243d6d8d8Sthorpej 	if (mod.mtabsize > 1)
35343d6d8d8Sthorpej 		qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]),
35443d6d8d8Sthorpej 		    _nsmtabcmp);
35543d6d8d8Sthorpej  out:
35643d6d8d8Sthorpej 	new = _nsvect_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod));
35743d6d8d8Sthorpej 	if (new == NULL) {
35843d6d8d8Sthorpej 		_nsmodfree(&mod);
35943d6d8d8Sthorpej 		return (-1);
36043d6d8d8Sthorpej 	}
36143d6d8d8Sthorpej 	_nsmod = new;
36243d6d8d8Sthorpej 	/* _nsmodsize already incremented */
36343d6d8d8Sthorpej 
36443d6d8d8Sthorpej 	qsort(_nsmod, _nsmodsize, sizeof(*_nsmod), _nsmodcmp);
36543d6d8d8Sthorpej 	return (0);
36643d6d8d8Sthorpej }
36743d6d8d8Sthorpej 
36843d6d8d8Sthorpej static void
_nsloadbuiltin(void)36943d6d8d8Sthorpej _nsloadbuiltin(void)
37043d6d8d8Sthorpej {
37143d6d8d8Sthorpej 
37243d6d8d8Sthorpej 	/* Do nothing, for now. */
37343d6d8d8Sthorpej }
374de3b78d7Slukem 
3752468bbbfSlukem int
_nsdbtaddsrc(ns_dbt * dbt,const ns_src * src)376b8b64b05Sthorpej _nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
377de3b78d7Slukem {
37843d6d8d8Sthorpej 	void		*new;
37943d6d8d8Sthorpej 	const ns_mod	*mod;
38043d6d8d8Sthorpej 	ns_mod		 modkey;
381b48252f3Slukem 
382b48252f3Slukem 	_DIAGASSERT(dbt != NULL);
383b48252f3Slukem 	_DIAGASSERT(src != NULL);
384b48252f3Slukem 
38543d6d8d8Sthorpej 	new = _nsvect_append(src, dbt->srclist, &dbt->srclistsize,
38643d6d8d8Sthorpej 	    sizeof(*src));
3872468bbbfSlukem 	if (new == NULL)
3882468bbbfSlukem 		return (-1);
3892468bbbfSlukem 	dbt->srclist = new;
39043d6d8d8Sthorpej 	/* dbt->srclistsize already incremented */
39143d6d8d8Sthorpej 
39243d6d8d8Sthorpej 	modkey.name = src->name;
393c5e820caSchristos 	mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod), _nsmodcmp);
39443d6d8d8Sthorpej 	if (mod == NULL)
39543d6d8d8Sthorpej 		return (_nsloadmod(src->name, NULL));
39643d6d8d8Sthorpej 
3972468bbbfSlukem 	return (0);
398de3b78d7Slukem }
399de3b78d7Slukem 
400de3b78d7Slukem void
_nsdbtdump(const ns_dbt * dbt)401b8b64b05Sthorpej _nsdbtdump(const ns_dbt *dbt)
402de3b78d7Slukem {
4038bbffe06Slukem 	unsigned int	i;
404de3b78d7Slukem 
405b48252f3Slukem 	_DIAGASSERT(dbt != NULL);
406b48252f3Slukem 
407de3b78d7Slukem 	printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
408de3b78d7Slukem 	    dbt->srclistsize == 1 ? "" : "s");
409de3b78d7Slukem 	for (i = 0; i < dbt->srclistsize; i++) {
410de3b78d7Slukem 		printf(" %s", dbt->srclist[i].name);
411de3b78d7Slukem 		if (!(dbt->srclist[i].flags &
412de3b78d7Slukem 		    (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
413de3b78d7Slukem 		    (dbt->srclist[i].flags & NS_SUCCESS))
414de3b78d7Slukem 			continue;
415de3b78d7Slukem 		printf(" [");
416de3b78d7Slukem 		if (!(dbt->srclist[i].flags & NS_SUCCESS))
417de3b78d7Slukem 			printf(" SUCCESS=continue");
418de3b78d7Slukem 		if (dbt->srclist[i].flags & NS_UNAVAIL)
419de3b78d7Slukem 			printf(" UNAVAIL=return");
420de3b78d7Slukem 		if (dbt->srclist[i].flags & NS_NOTFOUND)
421de3b78d7Slukem 			printf(" NOTFOUND=return");
422de3b78d7Slukem 		if (dbt->srclist[i].flags & NS_TRYAGAIN)
423de3b78d7Slukem 			printf(" TRYAGAIN=return");
424de3b78d7Slukem 		printf(" ]");
425de3b78d7Slukem 	}
426de3b78d7Slukem 	printf("\n");
427de3b78d7Slukem }
428de3b78d7Slukem 
42943d6d8d8Sthorpej static void
_nssrclist_free(ns_src ** src,u_int srclistsize)43043d6d8d8Sthorpej _nssrclist_free(ns_src **src, u_int srclistsize)
431de3b78d7Slukem {
43243d6d8d8Sthorpej 	u_int	i;
433de3b78d7Slukem 
43443d6d8d8Sthorpej 	for (i = 0; i < srclistsize; i++) {
43503256c6eSchristos 		if ((*src)[i].name != NULL)
43603256c6eSchristos 			free(__UNCONST((*src)[i].name));
437de3b78d7Slukem 	}
43843d6d8d8Sthorpej 	free(*src);
43943d6d8d8Sthorpej 	*src = NULL;
440de3b78d7Slukem }
441de3b78d7Slukem 
44243d6d8d8Sthorpej static void
_nsdbtfree(ns_dbt * dbt)44343d6d8d8Sthorpej _nsdbtfree(ns_dbt *dbt)
44443d6d8d8Sthorpej {
44543d6d8d8Sthorpej 
44643d6d8d8Sthorpej 	_nssrclist_free(&dbt->srclist, dbt->srclistsize);
44703256c6eSchristos 	if (dbt->name != NULL)
44803256c6eSchristos 		free(__UNCONST(dbt->name));
44943d6d8d8Sthorpej }
450de3b78d7Slukem 
4512468bbbfSlukem int
_nsdbtput(const ns_dbt * dbt)452b8b64b05Sthorpej _nsdbtput(const ns_dbt *dbt)
453de3b78d7Slukem {
45443d6d8d8Sthorpej 	ns_dbt	*p;
45543d6d8d8Sthorpej 	void	*new;
45643d6d8d8Sthorpej 	u_int	i;
457de3b78d7Slukem 
458b48252f3Slukem 	_DIAGASSERT(dbt != NULL);
459b48252f3Slukem 
460de3b78d7Slukem 	for (i = 0; i < _nsmapsize; i++) {
46143d6d8d8Sthorpej 		p = _nsvect_elem(i, _nsmap, _nsmapsize, sizeof(*_nsmap));
46243d6d8d8Sthorpej 		if (strcasecmp(dbt->name, p->name) == 0) {
463de3b78d7Slukem 					/* overwrite existing entry */
46443d6d8d8Sthorpej 			if (p->srclist != NULL)
46543d6d8d8Sthorpej 				_nssrclist_free(&p->srclist, p->srclistsize);
46643d6d8d8Sthorpej 			memmove(p, dbt, sizeof(*dbt));
4672468bbbfSlukem 			return (0);
468de3b78d7Slukem 		}
469de3b78d7Slukem 	}
47043d6d8d8Sthorpej 	new = _nsvect_append(dbt, _nsmap, &_nsmapsize, sizeof(*_nsmap));
471cbebf25aSchristos 	if (new == NULL)
4722468bbbfSlukem 		return (-1);
4732468bbbfSlukem 	_nsmap = new;
47443d6d8d8Sthorpej 	/* _nsmapsize already incremented */
47543d6d8d8Sthorpej 
4762468bbbfSlukem 	return (0);
477de3b78d7Slukem }
478de3b78d7Slukem 
47943d6d8d8Sthorpej /*
48043d6d8d8Sthorpej  * This function is called each time nsdispatch() is called.  If this
48143d6d8d8Sthorpej  * is the first call, or if the configuration has changed, (re-)prepare
48243d6d8d8Sthorpej  * the global data used by NSS.
48343d6d8d8Sthorpej  */
48443d6d8d8Sthorpej static int
_nsconfigure(void)48543d6d8d8Sthorpej _nsconfigure(void)
48643d6d8d8Sthorpej {
48743d6d8d8Sthorpej #ifdef _REENTRANT
48843d6d8d8Sthorpej 	static mutex_t	_nsconflock = MUTEX_INITIALIZER;
48943d6d8d8Sthorpej #endif
49043d6d8d8Sthorpej 	static time_t	_nsconfmod;
49143d6d8d8Sthorpej 	struct stat	statbuf;
49243d6d8d8Sthorpej 
49343d6d8d8Sthorpej 	mutex_lock(&_nsconflock);
49443d6d8d8Sthorpej 
49543d6d8d8Sthorpej 	if (stat(_PATH_NS_CONF, &statbuf) == -1) {
49643d6d8d8Sthorpej 		/*
49743d6d8d8Sthorpej 		 * No nsswitch.conf; just use whatever configuration we
49843d6d8d8Sthorpej 		 * currently have, or fall back on the defaults specified
49943d6d8d8Sthorpej 		 * by the caller.
50043d6d8d8Sthorpej 		 */
50143d6d8d8Sthorpej 		mutex_unlock(&_nsconflock);
50243d6d8d8Sthorpej 		return (0);
50343d6d8d8Sthorpej 	}
50443d6d8d8Sthorpej 
50543d6d8d8Sthorpej 	if (statbuf.st_mtime <= _nsconfmod) {
50643d6d8d8Sthorpej 		/* Internal state is up-to-date with nsswitch.conf. */
50743d6d8d8Sthorpej 		mutex_unlock(&_nsconflock);
50843d6d8d8Sthorpej 		return (0);
50943d6d8d8Sthorpej 	}
51043d6d8d8Sthorpej 
51143d6d8d8Sthorpej 	/*
51243d6d8d8Sthorpej 	 * Ok, we've decided we need to update the nsswitch configuration
513a2bbd451Sthorpej 	 * structures.  Acquire a write-lock on _nslock while continuing
514a2bbd451Sthorpej 	 * to hold _nsconflock.  Acquiring a write-lock blocks while
515a2bbd451Sthorpej 	 * waiting for other threads already holding a read-lock to clear.
516a2bbd451Sthorpej 	 * We hold _nsconflock for the duration, and update the time stamp
517a2bbd451Sthorpej 	 * at the end of the update operation, at which time we release
518a2bbd451Sthorpej 	 * both locks.
51943d6d8d8Sthorpej 	 */
52043d6d8d8Sthorpej 	rwlock_wrlock(&_nslock);
52143d6d8d8Sthorpej 
5229a513d96Schristos 	_nsyyin = fopen(_PATH_NS_CONF, "re");
52343d6d8d8Sthorpej 	if (_nsyyin == NULL) {
52443d6d8d8Sthorpej 		/*
52543d6d8d8Sthorpej 		 * Unable to open nsswitch.conf; behave as though the
52643d6d8d8Sthorpej 		 * stat() above failed.  Even though we have already
52743d6d8d8Sthorpej 		 * updated _nsconfmod, if the file reappears, the
52843d6d8d8Sthorpej 		 * mtime will change.
52943d6d8d8Sthorpej 		 */
530a2bbd451Sthorpej 		goto out;
53143d6d8d8Sthorpej 	}
53243d6d8d8Sthorpej 
53343d6d8d8Sthorpej 	_NSVECT_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
53443d6d8d8Sthorpej 	    (_nsvect_free_elem) _nsdbtfree);
53543d6d8d8Sthorpej 	_NSVECT_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
53643d6d8d8Sthorpej 	    (_nsvect_free_elem) _nsmodfree);
53743d6d8d8Sthorpej 
53843d6d8d8Sthorpej 	_nsloadbuiltin();
53943d6d8d8Sthorpej 
54043d6d8d8Sthorpej 	_nsyyparse();
54143d6d8d8Sthorpej 	(void) fclose(_nsyyin);
54243d6d8d8Sthorpej 	if (_nsmapsize != 0)
54343d6d8d8Sthorpej 		qsort(_nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp);
54443d6d8d8Sthorpej 
545a2bbd451Sthorpej 	_nsconfmod = statbuf.st_mtime;
546a2bbd451Sthorpej 
547a2bbd451Sthorpej  out:
548a2bbd451Sthorpej 	rwlock_unlock(&_nslock);
549a2bbd451Sthorpej 	mutex_unlock(&_nsconflock);
55043d6d8d8Sthorpej 	return (0);
55143d6d8d8Sthorpej }
55243d6d8d8Sthorpej 
55343d6d8d8Sthorpej static nss_method
_nsmethod(const char * source,const char * database,const char * method,const ns_dtab disp_tab[],void ** cb_data)55443d6d8d8Sthorpej _nsmethod(const char *source, const char *database, const char *method,
55543d6d8d8Sthorpej     const ns_dtab disp_tab[], void **cb_data)
55643d6d8d8Sthorpej {
55743d6d8d8Sthorpej 	int	curdisp;
55843d6d8d8Sthorpej 	ns_mod	*mod, modkey;
55943d6d8d8Sthorpej 	ns_mtab	*mtab, mtabkey;
56043d6d8d8Sthorpej 
56143d6d8d8Sthorpej 	if (disp_tab != NULL) {
56243d6d8d8Sthorpej 		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) {
56343d6d8d8Sthorpej 			if (strcasecmp(source, disp_tab[curdisp].src) == 0) {
56443d6d8d8Sthorpej 				*cb_data = disp_tab[curdisp].cb_data;
56543d6d8d8Sthorpej 				return (disp_tab[curdisp].callback);
56643d6d8d8Sthorpej 			}
56743d6d8d8Sthorpej 		}
56843d6d8d8Sthorpej 	}
56943d6d8d8Sthorpej 
57043d6d8d8Sthorpej 	modkey.name = source;
57143d6d8d8Sthorpej 	mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod),
57243d6d8d8Sthorpej 	    _nsmodcmp);
57343d6d8d8Sthorpej 	if (mod != NULL && mod->handle != NULL) {
57443d6d8d8Sthorpej 		mtabkey.database = database;
57543d6d8d8Sthorpej 		mtabkey.name = method;
57643d6d8d8Sthorpej 		mtab = bsearch(&mtabkey, mod->mtab, mod->mtabsize,
57743d6d8d8Sthorpej 		    sizeof(mod->mtab[0]), _nsmtabcmp);
57843d6d8d8Sthorpej 		if (mtab != NULL) {
57943d6d8d8Sthorpej 			*cb_data = mtab->mdata;
58043d6d8d8Sthorpej 			return (mtab->method);
58143d6d8d8Sthorpej 		}
58243d6d8d8Sthorpej 	}
58343d6d8d8Sthorpej 
58443d6d8d8Sthorpej 	*cb_data = NULL;
58543d6d8d8Sthorpej 	return (NULL);
58643d6d8d8Sthorpej }
587de3b78d7Slukem 
588de3b78d7Slukem int
5898fe3352aSchristos /*ARGSUSED*/
nsdispatch(void * retval,const ns_dtab disp_tab[],const char * database,const char * method,const ns_src defaults[],...)59037df6720Slukem nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
59137df6720Slukem 	    const char *method, const ns_src defaults[], ...)
592de3b78d7Slukem {
593a2bbd451Sthorpej 	static int	 _nsdispatching;
594a2bbd451Sthorpej #ifdef _REENTRANT
595a2bbd451Sthorpej 	struct _ns_drec	 drec, *ldrec;
596a2bbd451Sthorpej #endif
597de3b78d7Slukem 	va_list		 ap;
59843d6d8d8Sthorpej 	int		 i, result;
59943d6d8d8Sthorpej 	ns_dbt		 key;
600de3b78d7Slukem 	const ns_dbt	*dbt;
60137df6720Slukem 	const ns_src	*srclist;
60237df6720Slukem 	int		 srclistsize;
60343d6d8d8Sthorpej 	nss_method	 cb;
60443d6d8d8Sthorpej 	void		*cb_data;
605de3b78d7Slukem 
6063d2c2adcSlukem 	/* retval may be NULL */
6073d2c2adcSlukem 	/* disp_tab may be NULL */
608b48252f3Slukem 	_DIAGASSERT(database != NULL);
609b48252f3Slukem 	_DIAGASSERT(method != NULL);
6103d2c2adcSlukem 	_DIAGASSERT(defaults != NULL);
6113d2c2adcSlukem 	if (database == NULL || method == NULL || defaults == NULL)
612b48252f3Slukem 		return (NS_UNAVAIL);
613b48252f3Slukem 
614a2bbd451Sthorpej 	/*
615a2bbd451Sthorpej 	 * In both the threaded and non-threaded cases, avoid reloading
616a2bbd451Sthorpej 	 * the configuration if the current thread is already running
617a2bbd451Sthorpej 	 * nsdispatch() (i.e. recursive call).
618a2bbd451Sthorpej 	 *
619a2bbd451Sthorpej 	 * In the non-threaded case, this avoids changing the data structures
620a2bbd451Sthorpej 	 * while we're using them.
621a2bbd451Sthorpej 	 *
622a2bbd451Sthorpej 	 * In the threaded case, this avoids trying to take a write lock
623a2bbd451Sthorpej 	 * while the current thread holds a read lock (which would result
624a2bbd451Sthorpej 	 * in deadlock).
625a2bbd451Sthorpej 	 */
626a2bbd451Sthorpej #ifdef _REENTRANT
627a2bbd451Sthorpej 	if (__isthreaded) {
628a2bbd451Sthorpej 		drec.thr = thr_self();
629a2bbd451Sthorpej 		mutex_lock(&_ns_drec_lock);
630a2bbd451Sthorpej 		LIST_FOREACH(ldrec, &_ns_drec, list) {
631a2bbd451Sthorpej 			if (ldrec->thr == drec.thr)
632a2bbd451Sthorpej 				break;
633a2bbd451Sthorpej 		}
634a2bbd451Sthorpej 		LIST_INSERT_HEAD(&_ns_drec, &drec, list);
635a2bbd451Sthorpej 		mutex_unlock(&_ns_drec_lock);
636a2bbd451Sthorpej 		if (ldrec == NULL && _nsconfigure()) {
637a2bbd451Sthorpej 			mutex_lock(&_ns_drec_lock);
638a2bbd451Sthorpej 			LIST_REMOVE(&drec, list);
639a2bbd451Sthorpej 			mutex_unlock(&_ns_drec_lock);
64043d6d8d8Sthorpej 			return (NS_UNAVAIL);
641a2bbd451Sthorpej 		}
6421fc5e9b8Sthorpej 	} else
643a2bbd451Sthorpej #endif /* _REENTRANT */
6441fc5e9b8Sthorpej 	if (_nsdispatching++ == 0 && _nsconfigure()) {
6451fc5e9b8Sthorpej 		_nsdispatching--;
6461fc5e9b8Sthorpej 		return (NS_UNAVAIL);
6471fc5e9b8Sthorpej 	}
64843d6d8d8Sthorpej 
64943d6d8d8Sthorpej 	rwlock_rdlock(&_nslock);
65043d6d8d8Sthorpej 
65143d6d8d8Sthorpej 	key.name = database;
65243d6d8d8Sthorpej 	dbt = bsearch(&key, _nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp);
65337df6720Slukem 	if (dbt != NULL) {
65437df6720Slukem 		srclist = dbt->srclist;
65537df6720Slukem 		srclistsize = dbt->srclistsize;
65637df6720Slukem 	} else {
65737df6720Slukem 		srclist = defaults;
65837df6720Slukem 		srclistsize = 0;
65937df6720Slukem 		while (srclist[srclistsize].name != NULL)
66037df6720Slukem 			srclistsize++;
66137df6720Slukem 	}
662de3b78d7Slukem 	result = 0;
663de3b78d7Slukem 
66437df6720Slukem 	for (i = 0; i < srclistsize; i++) {
66543d6d8d8Sthorpej 		cb = _nsmethod(srclist[i].name, database, method,
66643d6d8d8Sthorpej 		    disp_tab, &cb_data);
667de3b78d7Slukem 		result = 0;
66843d6d8d8Sthorpej 		if (cb != NULL) {
66937df6720Slukem 			va_start(ap, defaults);
67043d6d8d8Sthorpej 			result = (*cb)(retval, cb_data, ap);
671de3b78d7Slukem 			va_end(ap);
672e775b9e3Slukem 			if (defaults[0].flags & NS_FORCEALL)
673e775b9e3Slukem 				continue;
67443d6d8d8Sthorpej 			if (result & srclist[i].flags)
675de3b78d7Slukem 				break;
676de3b78d7Slukem 		}
677de3b78d7Slukem 	}
67825d91a60Slukem 	result &= NS_STATUSMASK;	/* clear private flags in result */
67943d6d8d8Sthorpej 
68043d6d8d8Sthorpej 	rwlock_unlock(&_nslock);
68143d6d8d8Sthorpej 
682a2bbd451Sthorpej #ifdef _REENTRANT
683a2bbd451Sthorpej 	if (__isthreaded) {
684a2bbd451Sthorpej 		mutex_lock(&_ns_drec_lock);
685a2bbd451Sthorpej 		LIST_REMOVE(&drec, list);
686a2bbd451Sthorpej 		mutex_unlock(&_ns_drec_lock);
687a2bbd451Sthorpej 	} else
688a2bbd451Sthorpej #endif /* _REENTRANT */
6891fc5e9b8Sthorpej 		_nsdispatching--;
690a2bbd451Sthorpej 
691de3b78d7Slukem 	return (result ? result : NS_NOTFOUND);
692de3b78d7Slukem }
693