Update.
[glibc.git] / nss / nsswitch.c
blobb6720fd62eba58d574217bbf60b7a6886f351e1b
1 /* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <ctype.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <netdb.h>
24 #include <bits/libc-lock.h>
25 #include <search.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ldsodefs.h> /* We need some help from ld.so. */
31 #if !defined DO_STATIC_NSS || defined PIC
32 # include <gnu/lib-names.h>
33 #endif
35 #include "nsswitch.h"
37 /* Prototypes for the local functions. */
38 static name_database *nss_parse_file (const char *fname) internal_function;
39 static name_database_entry *nss_getline (char *line) internal_function;
40 static service_user *nss_parse_service_list (const char *line)
41 internal_function;
42 static service_library *nss_new_service (name_database *database,
43 const char *name) internal_function;
46 /* Declare external database variables. */
47 #define DEFINE_DATABASE(name) \
48 extern service_user *__nss_##name##_database; \
49 weak_extern (__nss_##name##_database)
50 #include "databases.def"
51 #undef DEFINE_DATABASE
53 /* Structure to map database name to variable. */
54 static struct
56 const char *name;
57 service_user **dbp;
58 } databases[] =
60 #define DEFINE_DATABASE(name) \
61 { #name, &__nss_##name##_database },
62 #include "databases.def"
63 #undef DEFINE_DATABASE
67 __libc_lock_define_initialized (static, lock)
69 #if !defined DO_STATIC_NSS || defined PIC
70 /* String with revision number of the shared object files. */
71 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
72 #endif
74 /* The root of the whole data base. */
75 static name_database *service_table;
78 /* -1 == database not found
79 0 == database entry pointer stored */
80 int
81 __nss_database_lookup (const char *database, const char *alternate_name,
82 const char *defconfig, service_user **ni)
84 /* Prevent multiple threads to change the service table. */
85 __libc_lock_lock (lock);
87 /* Reconsider database variable in case some other thread called
88 `__nss_configure_lookup' while we waited for the lock. */
89 if (*ni != NULL)
91 __libc_lock_unlock (lock);
92 return 0;
95 /* Are we initialized yet? */
96 if (service_table == NULL)
97 /* Read config file. */
98 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
100 /* Test whether configuration data is available. */
101 if (service_table != NULL)
103 /* Return first `service_user' entry for DATABASE. */
104 name_database_entry *entry;
106 /* XXX Could use some faster mechanism here. But each database is
107 only requested once and so this might not be critical. */
108 for (entry = service_table->entry; entry != NULL; entry = entry->next)
109 if (strcmp (database, entry->name) == 0)
110 *ni = entry->service;
112 if (*ni == NULL && alternate_name != NULL)
113 /* We haven't found an entry so far. Try to find it with the
114 alternative name. */
115 for (entry = service_table->entry; entry != NULL; entry = entry->next)
116 if (strcmp (alternate_name, entry->name) == 0)
117 *ni = entry->service;
120 /* No configuration data is available, either because nsswitch.conf
121 doesn't exist or because it doesn't has a line for this database.
123 DEFCONFIG specifies the default service list for this database,
124 or null to use the most common default. */
125 if (*ni == NULL)
126 *ni = nss_parse_service_list (defconfig
127 ?: "nis [NOTFOUND=return] files");
129 __libc_lock_unlock (lock);
131 return 0;
135 /* -1 == not found
136 0 == function found
137 1 == finished */
139 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
141 *fctp = __nss_lookup_function (*ni, fct_name);
143 while (*fctp == NULL
144 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
145 && (*ni)->next != NULL)
147 *ni = (*ni)->next;
149 *fctp = __nss_lookup_function (*ni, fct_name);
152 return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
156 /* -1 == not found
157 0 == adjusted for next function
158 1 == finished */
160 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
161 int all_values)
163 if (all_values)
165 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
166 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
167 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
168 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
169 return 1;
171 else
173 /* This is really only for debugging. */
174 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
175 __libc_fatal ("illegal status in " __FUNCTION__);
177 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
178 return 1;
181 if ((*ni)->next == NULL)
182 return -1;
186 *ni = (*ni)->next;
188 *fctp = __nss_lookup_function (*ni, fct_name);
190 while (*fctp == NULL
191 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
192 && (*ni)->next != NULL);
194 return *fctp != NULL ? 0 : -1;
199 __nss_configure_lookup (const char *dbname, const char *service_line)
201 service_user *new_db;
202 size_t cnt;
204 for (cnt = 0; cnt < sizeof databases; ++cnt)
206 int cmp = strcmp (dbname, databases[cnt].name);
207 if (cmp == 0)
208 break;
209 if (cmp < 0)
211 __set_errno (EINVAL);
212 return -1;
216 if (cnt == sizeof databases)
218 __set_errno (EINVAL);
219 return -1;
222 /* Test whether it is really used. */
223 if (databases[cnt].dbp == NULL)
224 /* Nothing to do, but we could do. */
225 return 0;
227 /* Try to generate new data. */
228 new_db = nss_parse_service_list (service_line);
229 if (new_db == NULL)
231 /* Illegal service specification. */
232 __set_errno (EINVAL);
233 return -1;
236 /* Prevent multiple threads to change the service table. */
237 __libc_lock_lock (lock);
239 /* Install new rules. */
240 *databases[cnt].dbp = new_db;
242 __libc_lock_unlock (lock);
244 return 0;
248 #if !defined DO_STATIC_NSS || defined PIC
249 static int
250 nss_dlerror_run (void (*operate) (void *), void *args)
252 char *last_errstring = NULL;
253 int result;
255 (void) _dl_catch_error (&last_errstring, operate, args);
257 result = last_errstring != NULL;
258 if (result)
259 free (last_errstring);
261 return result;
265 struct do_open_args
267 /* Argument to do_open. */
268 char *shlib_name;
269 service_user *ni;
272 struct get_sym_args
274 /* Arguments to get_sym. */
275 struct link_map *map;
276 char *name;
278 /* Return values of get_sym. */
279 ElfW(Addr) loadbase;
280 const ElfW(Sym) *ref;
283 static void
284 do_open (void *a)
286 struct do_open_args *args = (struct do_open_args *) a;
287 /* Open and relocate the shared object. */
288 args->ni->library->lib_handle = _dl_open (args->shlib_name, RTLD_LAZY);
291 static void
292 get_sym (void *a)
294 struct get_sym_args *args = (struct get_sym_args *) a;
295 args->ref = NULL;
296 args->loadbase = _dl_lookup_symbol (args->name, &args->ref,
297 args->map->l_local_scope,
298 args->map->l_name, 0);
300 #endif
302 /* Comparison function for searching NI->known tree. */
303 static int
304 known_compare (const void *p1, const void *p2)
306 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
307 *(const char *const *) p2);
311 void *
312 __nss_lookup_function (service_user *ni, const char *fct_name)
314 void **found, *result;
316 /* We now modify global data. Protect it. */
317 __libc_lock_lock (lock);
319 /* Search the tree of functions previously requested. Data in the
320 tree are `known_function' structures, whose first member is a
321 `const char *', the lookup key. The search returns a pointer to
322 the tree node structure; the first member of the is a pointer to
323 our structure (i.e. what will be a `known_function'); since the
324 first member of that is the lookup key string, &FCT_NAME is close
325 enough to a pointer to our structure to use as a lookup key that
326 will be passed to `known_compare' (above). */
328 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
329 if (*found != &fct_name)
330 /* The search found an existing structure in the tree. */
331 result = ((known_function *) *found)->fct_ptr;
332 else
334 /* This name was not known before. Now we have a node in the tree
335 (in the proper sorted position for FCT_NAME) that points to
336 &FCT_NAME instead of any real `known_function' structure.
337 Allocate a new structure and fill it in. */
339 known_function *known = malloc (sizeof *known);
340 if (! known)
342 remove_from_tree:
343 /* Oops. We can't instantiate this node properly.
344 Remove it from the tree. */
345 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
346 result = NULL;
348 else
350 /* Point the tree node at this new structure. */
351 *found = known;
352 known->fct_name = fct_name;
354 if (ni->library == NULL)
356 /* This service has not yet been used. Fetch the service
357 library for it, creating a new one if need be. If there
358 is no service table from the file, this static variable
359 holds the head of the service_library list made from the
360 default configuration. */
361 static name_database default_table;
362 ni->library = nss_new_service (service_table ?: &default_table,
363 ni->name);
364 if (ni->library == NULL)
366 /* This only happens when out of memory. */
367 free (known);
368 goto remove_from_tree;
372 #if !defined DO_STATIC_NSS || defined PIC
373 if (ni->library->lib_handle == NULL)
375 /* Load the shared library. */
376 size_t shlen = (7 + strlen (ni->library->name) + 3
377 + strlen (__nss_shlib_revision) + 1);
378 int saved_errno = errno;
379 struct do_open_args args;
380 args.shlib_name = __alloca (shlen);
381 args.ni = ni;
383 /* Construct shared object name. */
384 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.shlib_name,
385 "libnss_"),
386 ni->library->name),
387 ".so"),
388 __nss_shlib_revision);
390 if (nss_dlerror_run (do_open, &args) != 0)
392 /* Failed to load the library. */
393 ni->library->lib_handle = (void *) -1l;
394 __set_errno (saved_errno);
398 if (ni->library->lib_handle == (void *) -1l)
399 /* Library not found => function not found. */
400 result = NULL;
401 else
403 /* Get the desired function. Again, GNU ld.so magic ahead. */
404 size_t namlen = (5 + strlen (ni->library->name) + 1
405 + strlen (fct_name) + 1);
406 struct get_sym_args args;
407 args.name = __alloca (namlen);
408 args.map = ni->library->lib_handle;
410 /* Construct the function name. */
411 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.name, "_nss_"),
412 ni->library->name),
413 "_"),
414 fct_name);
416 /* Look up the symbol. */
417 result = (nss_dlerror_run (get_sym, &args) ? NULL
418 : (void *) (args.loadbase + args.ref->st_value));
420 #else
421 /* We can't get function address dynamically in static linking. */
423 # define DEFINE_ENT(h,nm) \
424 extern void _nss_##h##_get##nm##ent_r (void); \
425 extern void _nss_##h##_end##nm##ent (void); \
426 extern void _nss_##h##_set##nm##ent (void);
427 # define DEFINE_GET(h,nm) \
428 extern void _nss_##h##_get##nm##_r (void);
429 # define DEFINE_GETBY(h,nm,ky) \
430 extern void _nss_##h##_get##nm##by##ky##_r (void);
431 # include "function.def"
432 # undef DEFINE_ENT
433 # undef DEFINE_GET
434 # undef DEFINE_GETBY
435 # define DEFINE_ENT(h,nm) \
436 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
437 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
438 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
439 # define DEFINE_GET(h,nm) \
440 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
441 # define DEFINE_GETBY(h,nm,ky) \
442 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
443 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
445 # include "function.def"
446 { NULL, NULL }
448 size_t namlen = (5 + strlen (ni->library->name) + 1
449 + strlen (fct_name) + 1);
450 char name[namlen];
452 /* Construct the function name. */
453 __stpcpy (__stpcpy (__stpcpy (name, ni->library->name),
454 "_"),
455 fct_name);
457 result = NULL;
458 for (tp = &tbl[0]; tp->fname; tp++)
459 if (strcmp (tp->fname, name) == 0)
461 result = tp->fp;
462 break;
465 #endif
467 /* Remember function pointer for later calls. Even if null, we
468 record it so a second try needn't search the library again. */
469 known->fct_ptr = result;
473 /* Remove the lock. */
474 __libc_lock_unlock (lock);
476 return result;
480 static name_database *
481 internal_function
482 nss_parse_file (const char *fname)
484 FILE *fp;
485 name_database *result;
486 name_database_entry *last;
487 char *line;
488 size_t len;
490 /* Open the configuration file. */
491 fp = fopen (fname, "r");
492 if (fp == NULL)
493 return NULL;
495 result = (name_database *) malloc (sizeof (name_database));
496 if (result == NULL)
497 return NULL;
499 result->entry = NULL;
500 result->library = NULL;
501 last = NULL;
502 line = NULL;
503 len = 0;
506 name_database_entry *this;
507 ssize_t n;
508 char *cp;
510 n = __getline (&line, &len, fp);
511 if (n < 0)
512 break;
513 if (line[n - 1] == '\n')
514 line[n - 1] = '\0';
516 /* Because the file format does not know any form of quoting we
517 can search forward for the next '#' character and if found
518 make it terminating the line. */
519 cp = strchr (line, '#');
520 if (cp != NULL)
521 *cp = '\0';
523 /* If the line is blank it is ignored. */
524 if (line[0] == '\0')
525 continue;
527 /* Each line completely specifies the actions for a database. */
528 this = nss_getline (line);
529 if (this != NULL)
531 if (last != NULL)
532 last->next = this;
533 else
534 result->entry = this;
536 last = this;
539 while (!feof_unlocked (fp));
541 /* Free the buffer. */
542 free (line);
543 /* Close configuration file. */
544 fclose (fp);
546 return result;
550 /* Read the source names:
551 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
553 static service_user *
554 internal_function
555 nss_parse_service_list (const char *line)
557 service_user *result = NULL, **nextp = &result;
559 while (1)
561 service_user *new_service;
562 const char *name;
564 while (isspace (line[0]))
565 ++line;
566 if (line[0] == '\0')
567 /* No source specified. */
568 return result;
570 /* Read <source> identifier. */
571 name = line;
572 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
573 ++line;
574 if (name == line)
575 return result;
578 new_service = (service_user *) malloc (sizeof (service_user));
579 if (new_service == NULL)
580 return result;
581 else
583 char *source = (char *) malloc (line - name + 1);
584 if (source == NULL)
586 free (new_service);
587 return result;
589 *((char *) __mempcpy (source, name, line - name)) = '\0';
591 new_service->name = source;
594 /* Set default actions. */
595 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
596 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
597 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
598 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
599 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
600 new_service->library = NULL;
601 new_service->known = NULL;
602 new_service->next = NULL;
604 while (isspace (line[0]))
605 ++line;
607 if (line[0] == '[')
609 /* Read criterions. */
611 ++line;
612 while (line[0] != '\0' && isspace (line[0]));
616 int not;
617 enum nss_status status;
618 lookup_actions action;
620 /* Grok ! before name to mean all statii but that one. */
621 not = line[0] == '!';
622 if (not)
623 ++line;
625 /* Read status name. */
626 name = line;
627 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
628 && line[0] != ']')
629 ++line;
631 /* Compare with known statii. */
632 if (line - name == 7)
634 if (__strncasecmp (name, "SUCCESS", 7) == 0)
635 status = NSS_STATUS_SUCCESS;
636 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
637 status = NSS_STATUS_UNAVAIL;
638 else
639 return result;
641 else if (line - name == 8)
643 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
644 status = NSS_STATUS_NOTFOUND;
645 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
646 status = NSS_STATUS_TRYAGAIN;
647 else
648 return result;
650 else
651 return result;
653 while (isspace (line[0]))
654 ++line;
655 if (line[0] != '=')
656 return result;
658 ++line;
659 while (isspace (line[0]));
661 name = line;
662 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
663 && line[0] != ']')
664 ++line;
666 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
667 action = NSS_ACTION_RETURN;
668 else if (line - name == 8
669 && __strncasecmp (name, "CONTINUE", 8) == 0)
670 action = NSS_ACTION_CONTINUE;
671 else
672 return result;
674 if (not)
676 /* Save the current action setting for this status,
677 set them all to the given action, and reset this one. */
678 const lookup_actions save = new_service->actions[2 + status];
679 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
680 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
681 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
682 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
683 new_service->actions[2 + status] = save;
685 else
686 new_service->actions[2 + status] = action;
688 /* Skip white spaces. */
689 while (isspace (line[0]))
690 ++line;
692 while (line[0] != ']');
694 /* Skip the ']'. */
695 ++line;
698 *nextp = new_service;
699 nextp = &new_service->next;
703 static name_database_entry *
704 internal_function
705 nss_getline (char *line)
707 const char *name;
708 name_database_entry *result;
710 /* Ignore leading white spaces. ATTENTION: this is different from
711 what is implemented in Solaris. The Solaris man page says a line
712 beginning with a white space character is ignored. We regard
713 this as just another misfeature in Solaris. */
714 while (isspace (line[0]))
715 ++line;
717 /* Recognize `<database> ":"'. */
718 name = line;
719 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
720 ++line;
721 if (line[0] == '\0' || name == line)
722 /* Syntax error. */
723 return NULL;
724 *line++ = '\0';
726 result = (name_database_entry *) malloc (sizeof (name_database_entry));
727 if (result == NULL)
728 return NULL;
730 /* Save the database name. */
732 const size_t len = strlen (name) + 1;
733 char *new = malloc (len);
734 if (new == NULL)
736 free (result);
737 return NULL;
739 result->name = memcpy (new, name, len);
742 /* Parse the list of services. */
743 result->service = nss_parse_service_list (line);
745 result->next = NULL;
746 return result;
750 static service_library *
751 internal_function
752 nss_new_service (name_database *database, const char *name)
754 service_library **currentp = &database->library;
756 while (*currentp != NULL)
758 if (strcmp ((*currentp)->name, name) == 0)
759 return *currentp;
760 currentp = &(*currentp)->next;
763 /* We have to add the new service. */
764 *currentp = (service_library *) malloc (sizeof (service_library));
765 if (*currentp == NULL)
766 return NULL;
768 (*currentp)->name = name;
769 (*currentp)->lib_handle = NULL;
770 (*currentp)->next = NULL;
772 return *currentp;