nsdispatch.c revision 1.18 1 /* $NetBSD: nsdispatch.c,v 1.18 2002/05/26 14:48:19 wiz 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.18 2002/05/26 14:48:19 wiz 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 <assert.h>
51 #include <err.h>
52 #include <fcntl.h>
53 #define _NS_PRIVATE
54 #include <nsswitch.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 extern FILE *_nsyyin;
62 extern int _nsyyparse __P((void));
63
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 int
102 _nsdbtaddsrc(dbt, src)
103 ns_dbt *dbt;
104 const ns_src *src;
105 {
106
107 _DIAGASSERT(dbt != NULL);
108 _DIAGASSERT(src != NULL);
109
110 if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
111 ns_src *new;
112
113 new = (ns_src *)realloc(dbt->srclist,
114 (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
115 if (new == NULL)
116 return (-1);
117 dbt->srclist = new;
118 }
119 memmove(&dbt->srclist[dbt->srclistsize++], src, sizeof(ns_src));
120 return (0);
121 }
122
123
124 void
125 _nsdbtdump(dbt)
126 const ns_dbt *dbt;
127 {
128 int i;
129
130 _DIAGASSERT(dbt != NULL);
131
132 printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
133 dbt->srclistsize == 1 ? "" : "s");
134 for (i = 0; i < dbt->srclistsize; i++) {
135 printf(" %s", dbt->srclist[i].name);
136 if (!(dbt->srclist[i].flags &
137 (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
138 (dbt->srclist[i].flags & NS_SUCCESS))
139 continue;
140 printf(" [");
141 if (!(dbt->srclist[i].flags & NS_SUCCESS))
142 printf(" SUCCESS=continue");
143 if (dbt->srclist[i].flags & NS_UNAVAIL)
144 printf(" UNAVAIL=return");
145 if (dbt->srclist[i].flags & NS_NOTFOUND)
146 printf(" NOTFOUND=return");
147 if (dbt->srclist[i].flags & NS_TRYAGAIN)
148 printf(" TRYAGAIN=return");
149 printf(" ]");
150 }
151 printf("\n");
152 }
153
154
155 const ns_dbt *
156 _nsdbtget(name)
157 const char *name;
158 {
159 static time_t confmod;
160
161 struct stat statbuf;
162 ns_dbt dbt;
163
164 _DIAGASSERT(name != NULL);
165
166 dbt.name = name;
167
168 if (confmod) {
169 if (stat(_PATH_NS_CONF, &statbuf) == -1)
170 return (NULL);
171 if (confmod < statbuf.st_mtime) {
172 int i, j;
173
174 for (i = 0; i < _nsmapsize; i++) {
175 for (j = 0; j < _nsmap[i].srclistsize; j++) {
176 if (_nsmap[i].srclist[j].name != NULL) {
177 /*LINTED const cast*/
178 free((void *)
179 _nsmap[i].srclist[j].name);
180 }
181 }
182 if (_nsmap[i].srclist)
183 free(_nsmap[i].srclist);
184 if (_nsmap[i].name) {
185 /*LINTED const cast*/
186 free((void *)_nsmap[i].name);
187 }
188 }
189 if (_nsmap)
190 free(_nsmap);
191 _nsmap = NULL;
192 _nsmapsize = 0;
193 confmod = 0;
194 }
195 }
196 if (!confmod) {
197 if (stat(_PATH_NS_CONF, &statbuf) == -1)
198 return (NULL);
199 _nsyyin = fopen(_PATH_NS_CONF, "r");
200 if (_nsyyin == NULL)
201 return (NULL);
202 _nsyyparse();
203 (void)fclose(_nsyyin);
204 qsort(_nsmap, (size_t)_nsmapsize, sizeof(ns_dbt), _nscmp);
205 confmod = statbuf.st_mtime;
206 }
207 return (bsearch(&dbt, _nsmap, (size_t)_nsmapsize, sizeof(ns_dbt),
208 _nscmp));
209 }
210
211
212 int
213 _nsdbtput(dbt)
214 const ns_dbt *dbt;
215 {
216 int i;
217
218 _DIAGASSERT(dbt != NULL);
219
220 for (i = 0; i < _nsmapsize; i++) {
221 if (_nscmp(dbt, &_nsmap[i]) == 0) {
222 /* overwrite existing entry */
223 if (_nsmap[i].srclist != NULL)
224 free(_nsmap[i].srclist);
225 memmove(&_nsmap[i], dbt, sizeof(ns_dbt));
226 return (0);
227 }
228 }
229
230 if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
231 ns_dbt *new;
232
233 new = (ns_dbt *)realloc(_nsmap,
234 (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
235 if (new == NULL)
236 return (-1);
237 _nsmap = new;
238 }
239 memmove(&_nsmap[_nsmapsize++], dbt, sizeof(ns_dbt));
240 return (0);
241 }
242
243
244 int
245 /*ARGSUSED*/
246 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
247 const char *method, const ns_src defaults[], ...)
248 {
249 va_list ap;
250 int i, curdisp, result;
251 const ns_dbt *dbt;
252 const ns_src *srclist;
253 int srclistsize;
254
255 _DIAGASSERT(database != NULL);
256 _DIAGASSERT(method != NULL);
257 if (database == NULL || method == NULL)
258 return (NS_UNAVAIL);
259
260 dbt = _nsdbtget(database);
261 if (dbt != NULL) {
262 srclist = dbt->srclist;
263 srclistsize = dbt->srclistsize;
264 } else {
265 srclist = defaults;
266 srclistsize = 0;
267 while (srclist[srclistsize].name != NULL)
268 srclistsize++;
269 }
270 result = 0;
271
272 for (i = 0; i < srclistsize; i++) {
273 for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
274 if (strcasecmp(disp_tab[curdisp].src,
275 srclist[i].name) == 0)
276 break;
277 result = 0;
278 if (disp_tab[curdisp].callback) {
279 va_start(ap, defaults);
280 result = disp_tab[curdisp].callback(retval,
281 disp_tab[curdisp].cb_data, ap);
282 va_end(ap);
283 if (result & srclist[i].flags) {
284 break;
285 }
286 }
287 }
288 return (result ? result : NS_NOTFOUND);
289 }
290