1/*	$NetBSD: nsdispatch.c,v 1.30 2005/11/29 03:11:59 christos 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 * 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.
25 *
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.
37 */
38
39/*-
40 * Copyright (c) 2003 Networks Associates Technology, Inc.
41 * All rights reserved.
42 *
43 * Portions of this software were developed for the FreeBSD Project by
44 * Jacques A. Vidrine, Safeport Network Services, and Network
45 * Associates Laboratories, the Security Research Division of Network
46 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
47 * ("CBOSS"), as part of the DARPA CHATS research program.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 *    notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 *    notice, this list of conditions and the following disclaimer in the
56 *    documentation and/or other materials provided with the distribution.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71#include <sys/cdefs.h>
72#include <sys/types.h>
73#include <sys/param.h>
74#include <sys/stat.h>
75
76#include <assert.h>
77#ifdef __ELF__
78#include <dlfcn.h>
79#endif /* __ELF__ */
80#include <fcntl.h>
81#define _NS_PRIVATE
82#include <nsswitch.h>
83#include <stdarg.h>
84#include <stdio.h>
85#include <stdlib.h>
86#include <string.h>
87#include <strings.h>
88#include <unistd.h>
89
90static nss_method
91_nsmethod(const char *source, const char *database, const char *method,
92    const ns_dtab disp_tab[], void **cb_data)
93{
94	int	curdisp;
95
96	if (disp_tab != NULL) {
97		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) {
98			if (strcasecmp(source, disp_tab[curdisp].src) == 0) {
99				*cb_data = disp_tab[curdisp].cb_data;
100				return (disp_tab[curdisp].callback);
101			}
102		}
103	}
104
105	*cb_data = NULL;
106	return (NULL);
107}
108
109int
110/*ARGSUSED*/
111nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
112	    const char *method, const ns_src defaults[], ...)
113{
114	va_list		 ap;
115	int		 i, result;
116	const ns_src	*srclist;
117	int		 srclistsize;
118	nss_method	 cb;
119	void		*cb_data;
120
121	/* retval may be NULL */
122	/* disp_tab may be NULL */
123	assert(database != NULL);
124	assert(method != NULL);
125	assert(defaults != NULL);
126	if (database == NULL || method == NULL || defaults == NULL)
127		return (NS_UNAVAIL);
128
129        srclist = defaults;
130        srclistsize = 0;
131        while (srclist[srclistsize].name != NULL)
132                srclistsize++;
133
134	result = 0;
135
136	for (i = 0; i < srclistsize; i++) {
137		cb = _nsmethod(srclist[i].name, database, method,
138		    disp_tab, &cb_data);
139		result = 0;
140		if (cb != NULL) {
141			va_start(ap, defaults);
142			result = (*cb)(retval, cb_data, ap);
143			va_end(ap);
144			if (defaults[0].flags & NS_FORCEALL)
145				continue;
146			if (result & srclist[i].flags)
147				break;
148		}
149	}
150	result &= NS_STATUSMASK;	/* clear private flags in result */
151
152	return (result ? result : NS_NOTFOUND);
153}
154