nsdispatch.c revision 1.11 1 /* $NetBSD: nsdispatch.c,v 1.11 1999/05/03 15:17:13 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999 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.
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 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 __RCSID("$NetBSD: nsdispatch.c,v 1.11 1999/05/03 15:17:13 christos Exp $");
42 #endif /* LIBC_SCCS and not lint */
43
44 #include "namespace.h"
45
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/stat.h>
49
50 #include <err.h>
51 #include <fcntl.h>
52 #define _NS_PRIVATE
53 #include <nsswitch.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #ifdef __STDC__
60 #include <stdarg.h>
61 #else
62 #include <varargs.h>
63 #endif
64
65 #ifdef __weak_alias
66 __weak_alias(nsdispatch,_nsdispatch);
67 #endif
68
69
70 /*
71 * default sourcelist: `files'
72 */
73 const ns_src __nsdefaultsrc[] = {
74 { NSSRC_FILES, NS_SUCCESS },
75 { 0 },
76 };
77
78
79 static int _nsmapsize = 0;
80 static ns_dbt *_nsmap = NULL;
81
82 /*
83 * size of dynamic array chunk for _nsmap and _nsmap[x].srclist
84 */
85 #define NSELEMSPERCHUNK 8
86
87
88 int _nscmp __P((const void *, const void *));
89
90
91 int
92 _nscmp(a, b)
93 const void *a;
94 const void *b;
95 {
96 return (strcasecmp(((const ns_dbt *)a)->name,
97 ((const ns_dbt *)b)->name));
98 }
99
100
101 void
102 _nsdbtaddsrc(dbt, src)
103 ns_dbt *dbt;
104 const ns_src *src;
105 {
106 if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
107 dbt->srclist = (ns_src *)realloc(dbt->srclist,
108 (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
109 if (dbt->srclist == NULL)
110 err(1, "nsdispatch: memory allocation failure");
111 }
112 memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
113 }
114
115
116 void
117 _nsdbtdump(dbt)
118 const ns_dbt *dbt;
119 {
120 int i;
121
122 printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
123 dbt->srclistsize == 1 ? "" : "s");
124 for (i = 0; i < dbt->srclistsize; i++) {
125 printf(" %s", dbt->srclist[i].name);
126 if (!(dbt->srclist[i].flags &
127 (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
128 (dbt->srclist[i].flags & NS_SUCCESS))
129 continue;
130 printf(" [");
131 if (!(dbt->srclist[i].flags & NS_SUCCESS))
132 printf(" SUCCESS=continue");
133 if (dbt->srclist[i].flags & NS_UNAVAIL)
134 printf(" UNAVAIL=return");
135 if (dbt->srclist[i].flags & NS_NOTFOUND)
136 printf(" NOTFOUND=return");
137 if (dbt->srclist[i].flags & NS_TRYAGAIN)
138 printf(" TRYAGAIN=return");
139 printf(" ]");
140 }
141 printf("\n");
142 }
143
144
145 const ns_dbt *
146 _nsdbtget(name)
147 const char *name;
148 {
149 static time_t confmod;
150
151 struct stat statbuf;
152 ns_dbt dbt;
153
154 extern FILE *_nsyyin;
155 extern int _nsyyparse __P((void));
156
157 dbt.name = name;
158
159 if (confmod) {
160 if (stat(_PATH_NS_CONF, &statbuf) == -1)
161 return (NULL);
162 if (confmod < statbuf.st_mtime) {
163 int i, j;
164
165 for (i = 0; i < _nsmapsize; i++) {
166 for (j = 0; j < _nsmap[i].srclistsize; j++) {
167 if (_nsmap[i].srclist[j].name != NULL) {
168 /*LINTED const cast*/
169 free((void *)
170 _nsmap[i].srclist[j].name);
171 }
172 }
173 if (_nsmap[i].srclist)
174 free(_nsmap[i].srclist);
175 if (_nsmap[i].name) {
176 /*LINTED const cast*/
177 free((void *)_nsmap[i].name);
178 }
179 }
180 if (_nsmap)
181 free(_nsmap);
182 _nsmap = NULL;
183 _nsmapsize = 0;
184 confmod = 0;
185 }
186 }
187 if (!confmod) {
188 if (stat(_PATH_NS_CONF, &statbuf) == -1)
189 return (NULL);
190 _nsyyin = fopen(_PATH_NS_CONF, "r");
191 if (_nsyyin == NULL)
192 return (NULL);
193 _nsyyparse();
194 (void)fclose(_nsyyin);
195 qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
196 confmod = statbuf.st_mtime;
197 }
198 return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
199 _nscmp));
200 }
201
202
203 void
204 _nsdbtput(dbt)
205 const ns_dbt *dbt;
206 {
207 int i;
208
209 for (i = 0; i < _nsmapsize; i++) {
210 if (_nscmp(dbt, &_nsmap[i]) == 0) {
211 /* overwrite existing entry */
212 if (_nsmap[i].srclist != NULL)
213 free(_nsmap[i].srclist);
214 memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
215 return;
216 }
217 }
218
219 if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
220 _nsmap = (ns_dbt *)realloc(_nsmap,
221 (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
222 if (_nsmap == NULL)
223 err(1, "nsdispatch: memory allocation failure");
224 }
225 memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
226 }
227
228
229 int
230 /*ARGSUSED*/
231 #if __STDC__
232 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
233 const char *method, const ns_src defaults[], ...)
234 #else
235 nsdispatch(retval, disp_tab, database, method, defaults, va_alist)
236 void *retval;
237 const ns_dtab disp_tab[];
238 const char *database;
239 const char *method;
240 const ns_src defaults[];
241 va_dcl
242 #endif
243 {
244 va_list ap;
245 int i, curdisp, result;
246 const ns_dbt *dbt;
247 const ns_src *srclist;
248 int srclistsize;
249
250 dbt = _nsdbtget(database);
251 if (dbt != NULL) {
252 srclist = dbt->srclist;
253 srclistsize = dbt->srclistsize;
254 } else {
255 srclist = defaults;
256 srclistsize = 0;
257 while (srclist[srclistsize].name != NULL)
258 srclistsize++;
259 }
260 result = 0;
261
262 for (i = 0; i < srclistsize; i++) {
263 for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
264 if (strcasecmp(disp_tab[curdisp].src,
265 srclist[i].name) == 0)
266 break;
267 result = 0;
268 if (disp_tab[curdisp].callback) {
269 #if __STDC__
270 va_start(ap, defaults);
271 #else
272 va_start(ap);
273 #endif
274 result = disp_tab[curdisp].callback(retval,
275 disp_tab[curdisp].cb_data, ap);
276 va_end(ap);
277 if (result & srclist[i].flags) {
278 break;
279 }
280 }
281 }
282 return (result ? result : NS_NOTFOUND);
283 }
284