nsdispatch.c revision 1.1.4.3 1 1.1.4.3 lukem /* $NetBSD: nsdispatch.c,v 1.1.4.3 1998/11/02 03:29:36 lukem Exp $ */
2 1.1.4.1 lukem
3 1.1.4.1 lukem /*-
4 1.1.4.3 lukem * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.1.4.1 lukem * All rights reserved.
6 1.1.4.1 lukem *
7 1.1.4.3 lukem * This code is derived from software contributed to The NetBSD Foundation
8 1.1.4.3 lukem * by Luke Mewburn.
9 1.1.4.3 lukem *
10 1.1.4.1 lukem * Redistribution and use in source and binary forms, with or without
11 1.1.4.1 lukem * modification, are permitted provided that the following conditions
12 1.1.4.1 lukem * are met:
13 1.1.4.1 lukem * 1. Redistributions of source code must retain the above copyright
14 1.1.4.1 lukem * notice, this list of conditions and the following disclaimer.
15 1.1.4.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.1 lukem * notice, this list of conditions and the following disclaimer in the
17 1.1.4.1 lukem * documentation and/or other materials provided with the distribution.
18 1.1.4.1 lukem * 3. All advertising materials mentioning features or use of this software
19 1.1.4.1 lukem * must display the following acknowledgement:
20 1.1.4.3 lukem * This product includes software developed by the NetBSD
21 1.1.4.3 lukem * Foundation, Inc. and its contributors.
22 1.1.4.3 lukem * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.4.3 lukem * contributors may be used to endorse or promote products derived
24 1.1.4.3 lukem * from this software without specific prior written permission.
25 1.1.4.3 lukem *
26 1.1.4.3 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.4.3 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.4.3 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.4.3 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.4.3 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.4.3 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.4.3 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.4.3 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.4.3 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.4.3 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.4.3 lukem * POSSIBILITY OF SUCH DAMAGE.
37 1.1.4.1 lukem */
38 1.1.4.1 lukem
39 1.1.4.1 lukem #include <sys/types.h>
40 1.1.4.1 lukem #include <sys/param.h>
41 1.1.4.1 lukem #include <sys/stat.h>
42 1.1.4.1 lukem
43 1.1.4.3 lukem #include <err.h>
44 1.1.4.1 lukem #include <fcntl.h>
45 1.1.4.3 lukem #define _NS_PRIVATE
46 1.1.4.3 lukem #include <nsswitch.h>
47 1.1.4.1 lukem #include <stdio.h>
48 1.1.4.1 lukem #include <stdlib.h>
49 1.1.4.1 lukem #include <string.h>
50 1.1.4.1 lukem #include <unistd.h>
51 1.1.4.1 lukem
52 1.1.4.1 lukem
53 1.1.4.1 lukem static int _nsmapsize = 0;
54 1.1.4.1 lukem static ns_DBT *_nsmap = NULL;
55 1.1.4.1 lukem /*
56 1.1.4.1 lukem * number of ns_DBT per chunk in _nsmap. calculated along the lines of
57 1.1.4.1 lukem * NSELEMSPERCHUNK * sizeof(ns_DBT) < (1024 - some slop)
58 1.1.4.1 lukem * this is to help power of 2 mallocs which are really wasteful if the
59 1.1.4.1 lukem * amount just overflows a power of 2 boundary.
60 1.1.4.1 lukem */
61 1.1.4.2 lukem #define NSELEMSPERCHUNK 10
62 1.1.4.1 lukem
63 1.1.4.3 lukem int _nscmp __P((const void *, const void *));
64 1.1.4.3 lukem
65 1.1.4.1 lukem int
66 1.1.4.3 lukem _nscmp(a, b)
67 1.1.4.3 lukem const void *a;
68 1.1.4.3 lukem const void *b;
69 1.1.4.1 lukem {
70 1.1.4.3 lukem return strncmp(((ns_DBT*)a)->name, ((ns_DBT *)b)->name, NS_MAXDBLEN);
71 1.1.4.1 lukem }
72 1.1.4.1 lukem
73 1.1.4.1 lukem void
74 1.1.4.1 lukem __nsdbput(dbt)
75 1.1.4.1 lukem const ns_DBT *dbt;
76 1.1.4.1 lukem {
77 1.1.4.1 lukem int i;
78 1.1.4.1 lukem
79 1.1.4.1 lukem for (i = 0; i < _nsmapsize; i++) {
80 1.1.4.1 lukem if (_nscmp(dbt, &_nsmap[i]) == 0) {
81 1.1.4.1 lukem memmove((void *)&_nsmap[i], (void *)dbt,
82 1.1.4.1 lukem sizeof(ns_DBT));
83 1.1.4.1 lukem return;
84 1.1.4.1 lukem }
85 1.1.4.1 lukem }
86 1.1.4.1 lukem
87 1.1.4.1 lukem if ((_nsmapsize % NSELEMSPERCHUNK) == 0) {
88 1.1.4.1 lukem _nsmap = realloc(_nsmap,
89 1.1.4.1 lukem (_nsmapsize + NSELEMSPERCHUNK) * sizeof(ns_DBT));
90 1.1.4.1 lukem if (_nsmap == NULL)
91 1.1.4.3 lukem err(1, "nsdispatch: %m");
92 1.1.4.1 lukem
93 1.1.4.1 lukem }
94 1.1.4.1 lukem memmove((void *)&_nsmap[_nsmapsize++], (void *)dbt, sizeof(ns_DBT));
95 1.1.4.1 lukem }
96 1.1.4.1 lukem
97 1.1.4.1 lukem
98 1.1.4.1 lukem /*
99 1.1.4.1 lukem * __nsdbget --
100 1.1.4.1 lukem * args: ns_DBT *dbt
101 1.1.4.1 lukem * modifies: dbt
102 1.1.4.1 lukem * looks for the element in dbt->name and finds from there
103 1.1.4.1 lukem *
104 1.1.4.1 lukem */
105 1.1.4.1 lukem void
106 1.1.4.1 lukem __nsdbget(dbt)
107 1.1.4.1 lukem ns_DBT *dbt;
108 1.1.4.1 lukem {
109 1.1.4.3 lukem #ifdef _NS_LINEAR /* XXX: just to compare linear vs bsearch */
110 1.1.4.1 lukem int i;
111 1.1.4.1 lukem for (i = 0; i < _nsmapsize; i++) {
112 1.1.4.1 lukem if (_nscmp(dbt, &_nsmap[i]) == 0) {
113 1.1.4.1 lukem memmove((void *)dbt, (void *)&_nsmap[i],
114 1.1.4.1 lukem sizeof(ns_DBT));
115 1.1.4.1 lukem return;
116 1.1.4.1 lukem }
117 1.1.4.1 lukem }
118 1.1.4.1 lukem #else
119 1.1.4.1 lukem ns_DBT *e;
120 1.1.4.1 lukem e = bsearch(dbt, _nsmap, _nsmapsize, sizeof(ns_DBT), _nscmp);
121 1.1.4.1 lukem if (e != NULL)
122 1.1.4.1 lukem memmove((void *)dbt, (void *)e, sizeof(ns_DBT));
123 1.1.4.1 lukem #endif
124 1.1.4.1 lukem }
125 1.1.4.1 lukem
126 1.1.4.1 lukem
127 1.1.4.1 lukem void
128 1.1.4.1 lukem _nsdumpdbt(dbt)
129 1.1.4.1 lukem const ns_DBT *dbt;
130 1.1.4.1 lukem {
131 1.1.4.1 lukem int i;
132 1.1.4.1 lukem printf("%s:\n", dbt->name);
133 1.1.4.1 lukem for (i = 0; i < dbt->size; i++) {
134 1.1.4.1 lukem int source = (dbt->map[i] & NS_SOURCEMASK);
135 1.1.4.1 lukem int status = (dbt->map[i] & NS_STATUSMASK);
136 1.1.4.1 lukem
137 1.1.4.1 lukem printf(" %2d: [%x] ", i, dbt->map[i]);
138 1.1.4.1 lukem if (!dbt->map[i]) {
139 1.1.4.1 lukem printf(" (empty)\n");
140 1.1.4.1 lukem continue;
141 1.1.4.1 lukem }
142 1.1.4.1 lukem switch(source) {
143 1.1.4.1 lukem case NS_FILES: printf(" files"); break;
144 1.1.4.1 lukem case NS_DNS: printf(" dns"); break;
145 1.1.4.1 lukem case NS_NIS: printf(" nis"); break;
146 1.1.4.1 lukem case NS_NISPLUS: printf(" nisplus"); break;
147 1.1.4.1 lukem case NS_COMPAT: printf(" compat"); break;
148 1.1.4.1 lukem default: printf(" BADSOURCE(%x)", source); break;
149 1.1.4.1 lukem }
150 1.1.4.1 lukem
151 1.1.4.1 lukem if (!(status & NS_SUCCESS))
152 1.1.4.1 lukem printf(" SUCCESS=continue");
153 1.1.4.1 lukem if (status & NS_UNAVAIL)
154 1.1.4.1 lukem printf(" UNAVAIL=return");
155 1.1.4.1 lukem if (status & NS_NOTFOUND)
156 1.1.4.1 lukem printf(" NOTFOUND=return");
157 1.1.4.1 lukem if (status & NS_TRYAGAIN)
158 1.1.4.1 lukem printf(" TRYAGAIN=return");
159 1.1.4.1 lukem printf("\n");
160 1.1.4.1 lukem }
161 1.1.4.1 lukem }
162 1.1.4.1 lukem
163 1.1.4.1 lukem
164 1.1.4.1 lukem void
165 1.1.4.1 lukem _nsgetdbt(src, dbt)
166 1.1.4.1 lukem const char *src;
167 1.1.4.1 lukem ns_DBT *dbt;
168 1.1.4.1 lukem {
169 1.1.4.1 lukem static time_t confmod;
170 1.1.4.1 lukem struct stat statbuf;
171 1.1.4.1 lukem
172 1.1.4.1 lukem extern FILE *_nsyyin;
173 1.1.4.3 lukem extern int _nsyyparse __P((void));
174 1.1.4.1 lukem
175 1.1.4.1 lukem strncpy(dbt->name, src, NS_MAXDBLEN);
176 1.1.4.1 lukem dbt->name[NS_MAXDBLEN - 1] = '\0';
177 1.1.4.1 lukem dbt->size = 1;
178 1.1.4.1 lukem dbt->map[0] = NS_DEFAULTMAP; /* default to 'files' */
179 1.1.4.1 lukem
180 1.1.4.1 lukem if (confmod) {
181 1.1.4.1 lukem if (stat(_PATH_NS_CONF, &statbuf) == -1)
182 1.1.4.1 lukem return;
183 1.1.4.1 lukem if (confmod < statbuf.st_mtime) {
184 1.1.4.1 lukem free(_nsmap);
185 1.1.4.1 lukem _nsmap = NULL;
186 1.1.4.1 lukem _nsmapsize = 0;
187 1.1.4.1 lukem confmod = 0;
188 1.1.4.1 lukem }
189 1.1.4.1 lukem }
190 1.1.4.1 lukem if (!confmod) {
191 1.1.4.1 lukem if (stat(_PATH_NS_CONF, &statbuf) == -1)
192 1.1.4.1 lukem return;
193 1.1.4.1 lukem _nsyyin = fopen(_PATH_NS_CONF, "r");
194 1.1.4.1 lukem if (_nsyyin == NULL)
195 1.1.4.1 lukem return;
196 1.1.4.1 lukem _nsyyparse();
197 1.1.4.3 lukem #ifndef _NS_LINEAR
198 1.1.4.1 lukem qsort(_nsmap, _nsmapsize, sizeof(ns_DBT), _nscmp);
199 1.1.4.1 lukem #endif
200 1.1.4.1 lukem (void)fclose(_nsyyin);
201 1.1.4.1 lukem confmod = statbuf.st_mtime;
202 1.1.4.1 lukem }
203 1.1.4.1 lukem __nsdbget(dbt);
204 1.1.4.1 lukem }
205 1.1.4.1 lukem
206 1.1.4.1 lukem
207 1.1.4.1 lukem int
208 1.1.4.1 lukem #if __STDC__
209 1.1.4.1 lukem nsdispatch(void *retval, ns_dtab disp_tab, const char *database, ...)
210 1.1.4.1 lukem #else
211 1.1.4.1 lukem nsdispatch(retval, disp_tab, database, va_alist)
212 1.1.4.1 lukem void *retval;
213 1.1.4.1 lukem ns_dtab disp_tab;
214 1.1.4.1 lukem const char *database;
215 1.1.4.1 lukem va_dcl
216 1.1.4.1 lukem #endif
217 1.1.4.1 lukem {
218 1.1.4.1 lukem va_list ap;
219 1.1.4.1 lukem int curdisp, result = 0;
220 1.1.4.1 lukem ns_DBT dbt;
221 1.1.4.1 lukem
222 1.1.4.1 lukem _nsgetdbt(database, &dbt);
223 1.1.4.1 lukem
224 1.1.4.3 lukem #if _NS_DEBUG
225 1.1.4.1 lukem _nsdumpdbt(&dbt);
226 1.1.4.1 lukem fprintf(stderr, "nsdispatch: %s\n", database);
227 1.1.4.1 lukem #endif
228 1.1.4.1 lukem for (curdisp = 0; curdisp < dbt.size; curdisp++) {
229 1.1.4.1 lukem int source = (dbt.map[curdisp] & NS_SOURCEMASK);
230 1.1.4.3 lukem #if _NS_DEBUG
231 1.1.4.1 lukem fprintf(stderr, " %2d: source=%d", curdisp, source);
232 1.1.4.1 lukem #endif
233 1.1.4.1 lukem if (source < 0 || source >= NS_MAXSOURCE)
234 1.1.4.1 lukem continue;
235 1.1.4.1 lukem
236 1.1.4.1 lukem result = 0;
237 1.1.4.1 lukem if (disp_tab[source].cb) {
238 1.1.4.1 lukem #if __STDC__
239 1.1.4.1 lukem va_start(ap, database);
240 1.1.4.1 lukem #else
241 1.1.4.1 lukem va_start(ap);
242 1.1.4.1 lukem #endif
243 1.1.4.1 lukem result = disp_tab[source].cb(retval,
244 1.1.4.1 lukem disp_tab[source].cb_data, ap);
245 1.1.4.1 lukem va_end(ap);
246 1.1.4.3 lukem #if _NS_DEBUG
247 1.1.4.1 lukem fprintf(stderr, " result=%d (%d)", result,
248 1.1.4.1 lukem (result & (dbt.map[curdisp] & NS_STATUSMASK)));
249 1.1.4.1 lukem #endif
250 1.1.4.1 lukem if (result & (dbt.map[curdisp] & NS_STATUSMASK)) {
251 1.1.4.3 lukem #if _NS_DEBUG
252 1.1.4.1 lukem fprintf(stderr, " MATCH!\n");
253 1.1.4.1 lukem #endif
254 1.1.4.1 lukem break;
255 1.1.4.1 lukem }
256 1.1.4.1 lukem }
257 1.1.4.3 lukem #if _NS_DEBUG
258 1.1.4.1 lukem fprintf(stderr, "\n");
259 1.1.4.1 lukem #endif
260 1.1.4.1 lukem }
261 1.1.4.1 lukem return (result ? result : NS_NOTFOUND);
262 1.1.4.1 lukem }
263