nsdispatch.c revision 1.22 1 /* $NetBSD: nsdispatch.c,v 1.22 2004/07/24 18:42:51 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999, 2004 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; and by Jason R. Thorpe.
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 /*-
40 * Copyright (c) 2003 Networks Associates Technology, Inc.
41 * All rights reserved.
42 *
43 * Portions of this software were developed for the FreeBSD Project by
44 * Jacques A. Vidrine, Safeport Network Services, and Network
45 * Associates Laboratories, the Security Research Division of Network
46 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
47 * ("CBOSS"), as part of the DARPA CHATS research program.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71 #include <sys/cdefs.h>
72 #if defined(LIBC_SCCS) && !defined(lint)
73 __RCSID("$NetBSD: nsdispatch.c,v 1.22 2004/07/24 18:42:51 thorpej Exp $");
74 #endif /* LIBC_SCCS and not lint */
75
76 #include "namespace.h"
77
78 #include <sys/types.h>
79 #include <sys/param.h>
80 #include <sys/stat.h>
81
82 #include <assert.h>
83 #ifdef __ELF__
84 #include <dlfcn.h>
85 #endif /* __ELF__ */
86 #include <err.h>
87 #include <fcntl.h>
88 #define _NS_PRIVATE
89 #include <nsswitch.h>
90 #include <stdarg.h>
91 #include <stdio.h>
92 #include <stdlib.h>
93 #include <string.h>
94 #include <unistd.h>
95
96 #include "reentrant.h"
97
98 extern FILE *_nsyyin;
99 extern int _nsyyparse(void);
100
101
102 #ifdef __weak_alias
103 __weak_alias(nsdispatch,_nsdispatch)
104 #endif
105
106
107 /*
108 * default sourcelist: `files'
109 */
110 const ns_src __nsdefaultsrc[] = {
111 { NSSRC_FILES, NS_SUCCESS },
112 { 0 },
113 };
114
115 /* Database, source mappings. */
116 static u_int _nsmapsize;
117 static ns_dbt *_nsmap;
118
119 /* Nsswitch modules. */
120 static u_int _nsmodsize;
121 static ns_mod *_nsmod;
122
123 /* Placeholder for built-in modules' dlopen() handles. */
124 static void *_nsbuiltin = &_nsbuiltin;
125
126 #ifdef _REENTRANT
127 /*
128 * Global nsswitch data structures are mostly read-only, but we update them
129 * when we read or re-read nsswitch.conf.
130 */
131 static rwlock_t _nslock = RWLOCK_INITIALIZER;
132 #endif
133
134
135 /*
136 * Runtime determination of whether we are dynamically linked or not.
137 */
138 #ifdef __ELF__
139 extern int _DYNAMIC __attribute__((__weak__));
140 #define is_dynamic() (&_DYNAMIC != NULL)
141 #else
142 #define is_dynamic() (0) /* don't bother - switch to ELF! */
143 #endif /* __ELF__ */
144
145
146 /*
147 * size of dynamic array chunk for _nsmap and _nsmap[x].srclist (and other
148 * growing arrays).
149 */
150 #define NSELEMSPERCHUNK 8
151
152 /*
153 * Dynamically growable arrays are used for lists of databases, sources,
154 * and modules. The following "vector" API is used to isolate the
155 * common operations.
156 */
157 typedef void (*_nsvect_free_elem)(void *);
158
159 static void *
160 _nsvect_append(const void *elem, void *vec, u_int *count, size_t esize)
161 {
162 void *p;
163
164 if ((*count % NSELEMSPERCHUNK) == 0) {
165 p = realloc(vec, (*count + NSELEMSPERCHUNK) * esize);
166 if (p == NULL)
167 return (NULL);
168 vec = p;
169 }
170 memmove((void *)(((uintptr_t)vec) + (*count * esize)), elem, esize);
171 (*count)++;
172 return (vec);
173 }
174
175 static void *
176 _nsvect_elem(u_int i, void *vec, u_int count, size_t esize)
177 {
178
179 if (i < count)
180 return ((void *)((uintptr_t)vec + (i * esize)));
181 else
182 return (NULL);
183 }
184
185 static void
186 _nsvect_free(void *vec, u_int *count, size_t esize, _nsvect_free_elem free_elem)
187 {
188 void *elem;
189 u_int i;
190
191 for (i = 0; i < *count; i++) {
192 elem = _nsvect_elem(i, vec, *count, esize);
193 if (elem != NULL)
194 (*free_elem)(elem);
195 }
196 if (vec != NULL)
197 free(vec);
198 *count = 0;
199 }
200 #define _NSVECT_FREE(v, c, s, f) \
201 do { \
202 _nsvect_free((v), (c), (s), (f)); \
203 (v) = NULL; \
204 } while (/*CONSTCOND*/0)
205
206 static int
207 _nsdbtcmp(const void *a, const void *b)
208 {
209
210 return (strcasecmp(((const ns_dbt *)a)->name,
211 ((const ns_dbt *)b)->name));
212 }
213
214 static int
215 _nsmodcmp(const void *a, const void *b)
216 {
217
218 return (strcasecmp(((const ns_mod *)a)->name,
219 ((const ns_mod *)b)->name));
220 }
221
222 static int
223 _nsmtabcmp(const void *a, const void *b)
224 {
225 int cmp;
226
227 cmp = strcmp(((const ns_mtab *)a)->name,
228 ((const ns_mtab *)b)->name);
229 if (cmp)
230 return (cmp);
231
232 return (strcasecmp(((const ns_mtab *)a)->database,
233 ((const ns_mtab *)b)->database));
234 }
235
236 static void
237 _nsmodfree(ns_mod *mod)
238 {
239
240 /*LINTED const cast*/
241 free((void *)mod->name);
242 if (mod->handle == NULL)
243 return;
244 if (mod->unregister != NULL)
245 (*mod->unregister)(mod->mtab, mod->mtabsize);
246 #ifdef __ELF__
247 if (mod->handle != _nsbuiltin)
248 (void) dlclose(mod->handle);
249 #endif /* __ELF__ */
250 }
251
252 /*
253 * Load a built-in or dyanamically linked module. If the `reg_fn'
254 * argument is non-NULL, assume a built-in module and use `reg_fn'
255 * to register it. Otherwise, search for a dynamic nsswitch module.
256 */
257 static int
258 _nsloadmod(const char *source, nss_module_register_fn reg_fn)
259 {
260 char buf[PATH_MAX];
261 ns_mod mod, *new;
262
263 memset(&mod, 0, sizeof(mod));
264 mod.name = strdup(source);
265 if (mod.name == NULL)
266 return (-1);
267
268 if (reg_fn != NULL) {
269 /*
270 * The placeholder is required, as a NULL handle
271 * represents an invalid module.
272 */
273 mod.handle = _nsbuiltin;
274 } else if (!is_dynamic()) {
275 goto out;
276 } else {
277 #ifdef __ELF__
278 if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name,
279 NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf))
280 goto out;
281 mod.handle = dlopen(buf, RTLD_LOCAL | RTLD_LAZY);
282 if (mod.handle == NULL) {
283 #ifdef _NSS_DEBUG
284 /*
285 * This gets pretty annoying, since the built-in
286 * sources are not yet modules.
287 */
288 /* XXX log some error? */
289 #endif
290 goto out;
291 }
292 reg_fn = (nss_module_register_fn) dlsym(mod.handle,
293 "nss_module_register");
294 if (reg_fn == NULL) {
295 (void) dlclose(mod.handle);
296 mod.handle = NULL;
297 /* XXX log some error? */
298 goto out;
299 }
300 #else /* ! __ELF__ */
301 mod.handle = NULL;
302 #endif /* __ELF__ */
303 }
304 mod.mtab = (*reg_fn)(mod.name, &mod.mtabsize, &mod.unregister);
305 if (mod.mtab == NULL || mod.mtabsize == 0) {
306 #ifdef __ELF__
307 if (mod.handle != _nsbuiltin)
308 (void) dlclose(mod.handle);
309 #endif /* __ELF__ */
310 mod.handle = NULL;
311 /* XXX log some error? */
312 goto out;
313 }
314 if (mod.mtabsize > 1)
315 qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]),
316 _nsmtabcmp);
317 out:
318 new = _nsvect_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod));
319 if (new == NULL) {
320 _nsmodfree(&mod);
321 return (-1);
322 }
323 _nsmod = new;
324 /* _nsmodsize already incremented */
325
326 qsort(_nsmod, _nsmodsize, sizeof(*_nsmod), _nsmodcmp);
327 return (0);
328 }
329
330 static void
331 _nsloadbuiltin(void)
332 {
333
334 /* Do nothing, for now. */
335 }
336
337 int
338 _nsdbtaddsrc(ns_dbt *dbt, const ns_src *src)
339 {
340 void *new;
341 const ns_mod *mod;
342 ns_mod modkey;
343
344 _DIAGASSERT(dbt != NULL);
345 _DIAGASSERT(src != NULL);
346
347 new = _nsvect_append(src, dbt->srclist, &dbt->srclistsize,
348 sizeof(*src));
349 if (new == NULL)
350 return (-1);
351 dbt->srclist = new;
352 /* dbt->srclistsize already incremented */
353
354 modkey.name = src->name;
355 mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod),
356 _nsmodcmp);
357 if (mod == NULL)
358 return (_nsloadmod(src->name, NULL));
359
360 return (0);
361 }
362
363 void
364 _nsdbtdump(const ns_dbt *dbt)
365 {
366 int i;
367
368 _DIAGASSERT(dbt != NULL);
369
370 printf("%s (%d source%s):", dbt->name, dbt->srclistsize,
371 dbt->srclistsize == 1 ? "" : "s");
372 for (i = 0; i < dbt->srclistsize; i++) {
373 printf(" %s", dbt->srclist[i].name);
374 if (!(dbt->srclist[i].flags &
375 (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) &&
376 (dbt->srclist[i].flags & NS_SUCCESS))
377 continue;
378 printf(" [");
379 if (!(dbt->srclist[i].flags & NS_SUCCESS))
380 printf(" SUCCESS=continue");
381 if (dbt->srclist[i].flags & NS_UNAVAIL)
382 printf(" UNAVAIL=return");
383 if (dbt->srclist[i].flags & NS_NOTFOUND)
384 printf(" NOTFOUND=return");
385 if (dbt->srclist[i].flags & NS_TRYAGAIN)
386 printf(" TRYAGAIN=return");
387 printf(" ]");
388 }
389 printf("\n");
390 }
391
392 static void
393 _nssrclist_free(ns_src **src, u_int srclistsize)
394 {
395 u_int i;
396
397 for (i = 0; i < srclistsize; i++) {
398 if ((*src)[i].name != NULL) {
399 /*LINTED const cast*/
400 free((void *)(*src)[i].name);
401 }
402 }
403 free(*src);
404 *src = NULL;
405 }
406
407 static void
408 _nsdbtfree(ns_dbt *dbt)
409 {
410
411 _nssrclist_free(&dbt->srclist, dbt->srclistsize);
412 if (dbt->name != NULL) {
413 /*LINTED const cast*/
414 free((void *)dbt->name);
415 }
416 }
417
418 int
419 _nsdbtput(const ns_dbt *dbt)
420 {
421 ns_dbt *p;
422 void *new;
423 u_int i;
424
425 _DIAGASSERT(dbt != NULL);
426
427 for (i = 0; i < _nsmapsize; i++) {
428 p = _nsvect_elem(i, _nsmap, _nsmapsize, sizeof(*_nsmap));
429 if (strcasecmp(dbt->name, p->name) == 0) {
430 /* overwrite existing entry */
431 if (p->srclist != NULL)
432 _nssrclist_free(&p->srclist, p->srclistsize);
433 memmove(p, dbt, sizeof(*dbt));
434 return (0);
435 }
436 }
437 new = _nsvect_append(dbt, _nsmap, &_nsmapsize, sizeof(*_nsmap));
438 if (new == NULL)
439 return (-1);
440 _nsmap = new;
441 /* _nsmapsize already incremented */
442
443 return (0);
444 }
445
446 /*
447 * This function is called each time nsdispatch() is called. If this
448 * is the first call, or if the configuration has changed, (re-)prepare
449 * the global data used by NSS.
450 */
451 static int
452 _nsconfigure(void)
453 {
454 #ifdef _REENTRANT
455 static mutex_t _nsconflock = MUTEX_INITIALIZER;
456 #endif
457 static time_t _nsconfmod;
458 struct stat statbuf;
459
460 mutex_lock(&_nsconflock);
461
462 if (stat(_PATH_NS_CONF, &statbuf) == -1) {
463 /*
464 * No nsswitch.conf; just use whatever configuration we
465 * currently have, or fall back on the defaults specified
466 * by the caller.
467 */
468 mutex_unlock(&_nsconflock);
469 return (0);
470 }
471
472 if (statbuf.st_mtime <= _nsconfmod) {
473 /* Internal state is up-to-date with nsswitch.conf. */
474 mutex_unlock(&_nsconflock);
475 return (0);
476 }
477
478 /*
479 * Ok, we've decided we need to update the nsswitch configuration
480 * structures. Update the timestamp, acquire a write-lock on
481 * _nslock, and then release _nsconflock. This means that we don't
482 * need to acquire _nsconflock again to update the timetamp, and
483 * prevents another thread from updating the configuration before
484 * we're finished, even if they decide that they need to.
485 *
486 * Acquiring the locks in this fashion is safe: Only here are
487 * both _nslock and _nsconflock both taken, and nsdispatch()
488 * should never be called recursively.
489 */
490
491 _nsconfmod = statbuf.st_mtime;
492 rwlock_wrlock(&_nslock);
493 mutex_unlock(&_nsconflock);
494
495 _nsyyin = fopen(_PATH_NS_CONF, "r");
496 if (_nsyyin == NULL) {
497 /*
498 * Unable to open nsswitch.conf; behave as though the
499 * stat() above failed. Even though we have already
500 * updated _nsconfmod, if the file reappears, the
501 * mtime will change.
502 */
503 rwlock_unlock(&_nslock);
504 return (0);
505 }
506
507 _NSVECT_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
508 (_nsvect_free_elem) _nsdbtfree);
509 _NSVECT_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
510 (_nsvect_free_elem) _nsmodfree);
511
512 _nsloadbuiltin();
513
514 _nsyyparse();
515 (void) fclose(_nsyyin);
516 if (_nsmapsize != 0)
517 qsort(_nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp);
518 rwlock_unlock(&_nslock);
519
520 return (0);
521 }
522
523 static nss_method
524 _nsmethod(const char *source, const char *database, const char *method,
525 const ns_dtab disp_tab[], void **cb_data)
526 {
527 int curdisp;
528 ns_mod *mod, modkey;
529 ns_mtab *mtab, mtabkey;
530
531 if (disp_tab != NULL) {
532 for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) {
533 if (strcasecmp(source, disp_tab[curdisp].src) == 0) {
534 *cb_data = disp_tab[curdisp].cb_data;
535 return (disp_tab[curdisp].callback);
536 }
537 }
538 }
539
540 modkey.name = source;
541 mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod),
542 _nsmodcmp);
543 if (mod != NULL && mod->handle != NULL) {
544 mtabkey.database = database;
545 mtabkey.name = method;
546 mtab = bsearch(&mtabkey, mod->mtab, mod->mtabsize,
547 sizeof(mod->mtab[0]), _nsmtabcmp);
548 if (mtab != NULL) {
549 *cb_data = mtab->mdata;
550 return (mtab->method);
551 }
552 }
553
554 *cb_data = NULL;
555 return (NULL);
556 }
557
558 int
559 /*ARGSUSED*/
560 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
561 const char *method, const ns_src defaults[], ...)
562 {
563 va_list ap;
564 int i, result;
565 ns_dbt key;
566 const ns_dbt *dbt;
567 const ns_src *srclist;
568 int srclistsize;
569 nss_method cb;
570 void *cb_data;
571
572 _DIAGASSERT(database != NULL);
573 _DIAGASSERT(method != NULL);
574 if (database == NULL || method == NULL)
575 return (NS_UNAVAIL);
576
577 if (_nsconfigure())
578 return (NS_UNAVAIL);
579
580 rwlock_rdlock(&_nslock);
581
582 key.name = database;
583 dbt = bsearch(&key, _nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp);
584 if (dbt != NULL) {
585 srclist = dbt->srclist;
586 srclistsize = dbt->srclistsize;
587 } else {
588 srclist = defaults;
589 srclistsize = 0;
590 while (srclist[srclistsize].name != NULL)
591 srclistsize++;
592 }
593 result = 0;
594
595 for (i = 0; i < srclistsize; i++) {
596 cb = _nsmethod(srclist[i].name, database, method,
597 disp_tab, &cb_data);
598 result = 0;
599 if (cb != NULL) {
600 va_start(ap, defaults);
601 result = (*cb)(retval, cb_data, ap);
602 va_end(ap);
603 if (result & srclist[i].flags)
604 break;
605 }
606 }
607
608 rwlock_unlock(&_nslock);
609
610 return (result ? result : NS_NOTFOUND);
611 }
612