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