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