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