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