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