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 <unistd.h>
88
89 static nss_method
_nsmethod(const char * source,const char * database,const char * method,const ns_dtab disp_tab[],void ** cb_data)90 _nsmethod(const char *source, const char *database, const char *method,
91 const ns_dtab disp_tab[], void **cb_data)
92 {
93 int curdisp;
94
95 if (disp_tab != NULL) {
96 for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) {
97 if (strcasecmp(source, disp_tab[curdisp].src) == 0) {
98 *cb_data = disp_tab[curdisp].cb_data;
99 return (disp_tab[curdisp].callback);
100 }
101 }
102 }
103
104 *cb_data = NULL;
105 return (NULL);
106 }
107
108 int
109 /*ARGSUSED*/
nsdispatch(void * retval,const ns_dtab disp_tab[],const char * database,const char * method,const ns_src defaults[],...)110 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
111 const char *method, const ns_src defaults[], ...)
112 {
113 va_list ap;
114 int i, result;
115 const ns_src *srclist;
116 int srclistsize;
117 nss_method cb;
118 void *cb_data;
119
120 /* retval may be NULL */
121 /* disp_tab may be NULL */
122 assert(database != NULL);
123 assert(method != NULL);
124 assert(defaults != NULL);
125 if (database == NULL || method == NULL || defaults == NULL)
126 return (NS_UNAVAIL);
127
128 srclist = defaults;
129 srclistsize = 0;
130 while (srclist[srclistsize].name != NULL)
131 srclistsize++;
132
133 result = 0;
134
135 for (i = 0; i < srclistsize; i++) {
136 cb = _nsmethod(srclist[i].name, database, method,
137 disp_tab, &cb_data);
138 result = 0;
139 if (cb != NULL) {
140 va_start(ap, defaults);
141 result = (*cb)(retval, cb_data, ap);
142 va_end(ap);
143 if (defaults[0].flags & NS_FORCEALL)
144 continue;
145 if (result & srclist[i].flags)
146 break;
147 }
148 }
149 result &= NS_STATUSMASK; /* clear private flags in result */
150
151 return (result ? result : NS_NOTFOUND);
152 }
153