nsdispatch.c revision 1.6 1 /* $NetBSD: nsdispatch.c,v 1.6 1999/01/20 11:49:33 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(((ns_dbt *)a)->name, ((ns_dbt *)b)->name));
86 }
87
88
89 void
90 _nsdbtaddsrc(dbt, src)
91 ns_dbt *dbt;
92 const ns_src *src;
93 {
94 if ((dbt->srclistsize % NSELEMSPERCHUNK) == 0) {
95 dbt->srclist = (ns_src *)realloc(dbt->srclist,
96 (dbt->srclistsize + NSELEMSPERCHUNK) * sizeof(ns_src));
97 if (dbt->srclist == NULL)
98 err(1, "nsdispatch: memory allocation failure");
99 }
100 memmove((void *)&dbt->srclist[dbt->srclistsize++], (void *)src,
101 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 free((char *)
158 _nsmap[i].srclist[j].name);
159 }
160 if (_nsmap[i].srclist)
161 free(_nsmap[i].srclist);
162 if (_nsmap[i].name)
163 free((char *)_nsmap[i].name);
164 }
165 if (_nsmap)
166 free(_nsmap);
167 _nsmap = NULL;
168 _nsmapsize = 0;
169 confmod = 0;
170 }
171 }
172 if (!confmod) {
173 if (stat(_PATH_NS_CONF, &statbuf) == -1)
174 return (NULL);
175 _nsyyin = fopen(_PATH_NS_CONF, "r");
176 if (_nsyyin == NULL)
177 return (NULL);
178 _nsyyparse();
179 (void)fclose(_nsyyin);
180 qsort(_nsmap, _nsmapsize, sizeof(ns_dbt), _nscmp);
181 confmod = statbuf.st_mtime;
182 }
183 return (bsearch(&dbt, _nsmap, _nsmapsize, sizeof(ns_dbt), _nscmp));
184 }
185
186
187 void
188 _nsdbtput(dbt)
189 const ns_dbt *dbt;
190 {
191 int i;
192
193 for (i = 0; i < _nsmapsize; i++) {
194 if (_nscmp(dbt, &_nsmap[i]) == 0) {
195 /* overwrite existing entry */
196 if (_nsmap[i].srclist != NULL)
197 free(_nsmap[i].srclist);
198 memmove((void *)&_nsmap[i], (void *)dbt,
199 sizeof(ns_dbt));
200 return;
201 }
202 }
203
204 if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
205 _nsmap = (ns_dbt *)realloc(_nsmap,
206 (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_dbt));
207 if (_nsmap == NULL)
208 err(1, "nsdispatch: memory allocation failure");
209 }
210 memmove((void *)&_nsmap[_nsmapsize++], (void *)dbt, sizeof(ns_dbt));
211 }
212
213
214 int
215 #if __STDC__
216 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
217 const char *method, const ns_src defaults[], ...)
218 #else
219 nsdispatch(retval, disp_tab, database, method, defaults, va_alist)
220 void *retval;
221 const ns_dtab disp_tab[];
222 const char *database;
223 const char *method;
224 const ns_src defaults[];
225 va_dcl
226 #endif
227 {
228 va_list ap;
229 int i, curdisp, result;
230 const ns_dbt *dbt;
231 const ns_src *srclist;
232 int srclistsize;
233
234 dbt = _nsdbtget(database);
235 if (dbt != NULL) {
236 srclist = dbt->srclist;
237 srclistsize = dbt->srclistsize;
238 } else {
239 srclist = defaults;
240 srclistsize = 0;
241 while (srclist[srclistsize].name != NULL)
242 srclistsize++;
243 }
244 result = 0;
245
246 for (i = 0; i < srclistsize; i++) {
247 for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++)
248 if (strcasecmp(disp_tab[curdisp].src,
249 srclist[i].name) == 0)
250 break;
251 result = 0;
252 if (disp_tab[curdisp].callback) {
253 #if __STDC__
254 va_start(ap, defaults);
255 #else
256 va_start(ap);
257 #endif
258 result = disp_tab[curdisp].callback(retval,
259 disp_tab[curdisp].cb_data, ap);
260 va_end(ap);
261 if (result & srclist[i].flags) {
262 break;
263 }
264 }
265 }
266 return (result ? result : NS_NOTFOUND);
267 }
268