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. */
24 #include <bits/libc-lock.h>
25 #include <link.h> /* We need some help from ld.so. */
31 #if !defined DO_STATIC_NSS || defined PIC
32 # include <gnu/lib-names.h>
37 /* Prototypes for the local functions. */
38 static void *nss_lookup_function (service_user *ni, const char *fct_name)
40 static name_database *nss_parse_file (const char *fname) internal_function;
41 static name_database_entry *nss_getline (char *line) internal_function;
42 static service_user *nss_parse_service_list (const char *line)
44 static service_library *nss_new_service (name_database *database,
45 const char *name) internal_function;
48 /* Declare external database variables. */
49 #define DEFINE_DATABASE(name) \
50 extern service_user *__nss_##name##_database; \
51 weak_extern (__nss_##name##_database)
52 #include "databases.def"
53 #undef DEFINE_DATABASE
55 /* Structure to map database name to variable. */
62 #define DEFINE_DATABASE(name) \
63 { #name, &__nss_##name##_database },
64 #include "databases.def"
65 #undef DEFINE_DATABASE
69 __libc_lock_define_initialized (static, lock)
72 /* Nonzero if no NSCD is available. */
73 int __nss_nscd_not_available;
75 #if !defined DO_STATIC_NSS || defined PIC
76 /* String with revision number of the shared object files. */
77 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
80 /* The root of the whole data base. */
81 static name_database *service_table;
84 /* -1 == database not found
85 0 == database entry pointer stored */
87 __nss_database_lookup (const char *database, const char *alternate_name,
88 const char *defconfig, service_user **ni)
90 /* Prevent multiple threads to change the service table. */
91 __libc_lock_lock (lock);
93 /* Reconsider database variable in case some other thread called
94 `__nss_configure_lookup' while we waited for the lock. */
97 __libc_lock_unlock (lock);
101 /* Are we initialized yet? */
102 if (service_table == NULL)
103 /* Read config file. */
104 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
106 /* Test whether configuration data is available. */
107 if (service_table != NULL)
109 /* Return first `service_user' entry for DATABASE. */
110 name_database_entry *entry;
112 /* XXX Could use some faster mechanism here. But each database is
113 only requested once and so this might not be critical. */
114 for (entry = service_table->entry; entry != NULL; entry = entry->next)
115 if (strcmp (database, entry->name) == 0)
116 *ni = entry->service;
118 if (*ni == NULL && alternate_name != NULL)
119 /* We haven't found an entry so far. Try to find it with the
121 for (entry = service_table->entry; entry != NULL; entry = entry->next)
122 if (strcmp (alternate_name, entry->name) == 0)
123 *ni = entry->service;
126 /* No configuration data is available, either because nsswitch.conf
127 doesn't exist or because it doesn't has a line for this database.
129 DEFCONFIG specifies the default service list for this database,
130 or null to use the most common default. */
132 *ni = nss_parse_service_list (defconfig
133 ?: "nis [NOTFOUND=return] files");
135 __libc_lock_unlock (lock);
142 0 == adjusted for next function
145 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
147 *fctp = nss_lookup_function (*ni, fct_name);
150 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
151 && (*ni)->next != NULL)
155 *fctp = nss_lookup_function (*ni, fct_name);
158 return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
163 0 == adjusted for next function
166 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
171 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
172 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
173 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
174 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
179 /* This is really only for debugging. */
180 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
181 __libc_fatal ("illegal status in " __FUNCTION__);
183 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
187 if ((*ni)->next == NULL)
194 *fctp = nss_lookup_function (*ni, fct_name);
197 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
198 && (*ni)->next != NULL);
200 return *fctp != NULL ? 0 : -1;
205 __nss_configure_lookup (const char *dbname, const char *service_line)
207 service_user *new_db;
210 for (cnt = 0; cnt < sizeof databases; ++cnt)
212 int cmp = strcmp (dbname, databases[cnt].name);
217 __set_errno (EINVAL);
222 if (cnt == sizeof databases)
224 __set_errno (EINVAL);
228 /* Test whether it is really used. */
229 if (databases[cnt].dbp == NULL)
230 /* Nothing to do, but we could do. */
233 /* Try to generate new data. */
234 new_db = nss_parse_service_list (service_line);
237 /* Illegal service specification. */
238 __set_errno (EINVAL);
242 /* Prevent multiple threads to change the service table. */
243 __libc_lock_lock (lock);
245 /* Install new rules. */
246 *databases[cnt].dbp = new_db;
248 __libc_lock_unlock (lock);
254 #if !defined DO_STATIC_NSS || defined PIC
256 nss_dlerror_run (void (*operate) (void *), void *args)
258 char *last_errstring = NULL;
259 const char *last_object_name = NULL;
262 (void) _dl_catch_error (&last_errstring, &last_object_name, operate, args);
264 result = last_errstring != NULL;
266 free (last_errstring);
274 /* Argument to do_open. */
281 /* Arguments to get_sym. */
282 struct link_map *map;
285 /* Return values of get_sym. */
287 const ElfW(Sym) *ref;
293 struct do_open_args *args = (struct do_open_args *) a;
294 /* Open and relocate the shared object. */
295 args->ni->library->lib_handle = _dl_open (args->shlib_name, RTLD_LAZY);
301 struct get_sym_args *args = (struct get_sym_args *) a;
302 struct link_map *scope[2] = { args->map, NULL };
304 args->loadbase = _dl_lookup_symbol (args->name, &args->ref,
305 scope, args->map->l_name, 0);
309 /* Comparison function for searching NI->known tree. */
311 known_compare (const void *p1, const void *p2)
313 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
314 *(const char *const *) p2);
320 nss_lookup_function (service_user *ni, const char *fct_name)
322 void **found, *result;
324 /* We now modify global data. Protect it. */
325 __libc_lock_lock (lock);
327 /* Search the tree of functions previously requested. Data in the
328 tree are `known_function' structures, whose first member is a
329 `const char *', the lookup key. The search returns a pointer to
330 the tree node structure; the first member of the is a pointer to
331 our structure (i.e. what will be a `known_function'); since the
332 first member of that is the lookup key string, &FCT_NAME is close
333 enough to a pointer to our structure to use as a lookup key that
334 will be passed to `known_compare' (above). */
336 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
337 if (*found != &fct_name)
338 /* The search found an existing structure in the tree. */
339 result = ((known_function *) *found)->fct_ptr;
342 /* This name was not known before. Now we have a node in the tree
343 (in the proper sorted position for FCT_NAME) that points to
344 &FCT_NAME instead of any real `known_function' structure.
345 Allocate a new structure and fill it in. */
347 known_function *known = malloc (sizeof *known);
351 /* Oops. We can't instantiate this node properly.
352 Remove it from the tree. */
353 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
358 /* Point the tree node at this new structure. */
360 known->fct_name = fct_name;
362 if (ni->library == NULL)
364 /* This service has not yet been used. Fetch the service
365 library for it, creating a new one if need be. If there
366 is no service table from the file, this static variable
367 holds the head of the service_library list made from the
368 default configuration. */
369 static name_database default_table;
370 ni->library = nss_new_service (service_table ?: &default_table,
372 if (ni->library == NULL)
374 /* This only happens when out of memory. */
376 goto remove_from_tree;
380 #if !defined DO_STATIC_NSS || defined PIC
381 if (ni->library->lib_handle == NULL)
383 /* Load the shared library. */
384 size_t shlen = (7 + strlen (ni->library->name) + 3
385 + strlen (__nss_shlib_revision) + 1);
386 int saved_errno = errno;
387 struct do_open_args args;
388 args.shlib_name = __alloca (shlen);
391 /* Construct shared object name. */
392 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.shlib_name,
396 __nss_shlib_revision);
398 if (nss_dlerror_run (do_open, &args) != 0)
400 /* Failed to load the library. */
401 ni->library->lib_handle = (void *) -1l;
402 __set_errno (saved_errno);
406 if (ni->library->lib_handle == (void *) -1l)
407 /* Library not found => function not found. */
411 /* Get the desired function. Again, GNU ld.so magic ahead. */
412 size_t namlen = (5 + strlen (ni->library->name) + 1
413 + strlen (fct_name) + 1);
414 struct get_sym_args args;
415 args.name = __alloca (namlen);
416 args.map = ni->library->lib_handle;
418 /* Construct the function name. */
419 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.name, "_nss_"),
424 /* Look up the symbol. */
425 result = (nss_dlerror_run (get_sym, &args) ? NULL
426 : (void *) (args.loadbase + args.ref->st_value));
429 /* We can't get function address dynamically in static linking. */
431 # define DEFINE_ENT(h,nm) \
432 extern void _nss_##h##_get##nm##ent_r (void); \
433 extern void _nss_##h##_end##nm##ent (void); \
434 extern void _nss_##h##_set##nm##ent (void);
435 # define DEFINE_GET(h,nm) \
436 extern void _nss_##h##_get##nm##_r (void);
437 # define DEFINE_GETBY(h,nm,ky) \
438 extern void _nss_##h##_get##nm##by##ky##_r (void);
439 # include "function.def"
443 # define DEFINE_ENT(h,nm) \
444 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
445 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
446 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
447 # define DEFINE_GET(h,nm) \
448 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
449 # define DEFINE_GETBY(h,nm,ky) \
450 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
451 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
453 # include "function.def"
456 size_t namlen = (5 + strlen (ni->library->name) + 1
457 + strlen (fct_name) + 1);
460 /* Construct the function name. */
461 __stpcpy (__stpcpy (__stpcpy (name, ni->library->name),
466 for (tp = &tbl[0]; tp->fname; tp++)
467 if (strcmp (tp->fname, name) == 0)
475 /* Remember function pointer for later calls. Even if null, we
476 record it so a second try needn't search the library again. */
477 known->fct_ptr = result;
481 /* Remove the lock. */
482 __libc_lock_unlock (lock);
488 static name_database *
490 nss_parse_file (const char *fname)
493 name_database *result;
494 name_database_entry *last;
498 /* Open the configuration file. */
499 fp = fopen (fname, "r");
503 result = (name_database *) malloc (sizeof (name_database));
507 result->entry = NULL;
508 result->library = NULL;
514 name_database_entry *this;
518 n = __getline (&line, &len, fp);
521 if (line[n - 1] == '\n')
524 /* Because the file format does not know any form of quoting we
525 can search forward for the next '#' character and if found
526 make it terminating the line. */
527 cp = strchr (line, '#');
531 /* If the line is blank it is ignored. */
535 /* Each line completely specifies the actions for a database. */
536 this = nss_getline (line);
542 result->entry = this;
549 /* Free the buffer. */
551 /* Close configuration file. */
558 /* Read the source names:
559 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
561 static service_user *
563 nss_parse_service_list (const char *line)
565 service_user *result = NULL, **nextp = &result;
569 service_user *new_service;
572 while (isspace (line[0]))
575 /* No source specified. */
578 /* Read <source> identifier. */
580 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
586 new_service = (service_user *) malloc (sizeof (service_user));
587 if (new_service == NULL)
591 char *source = (char *) malloc (line - name + 1);
597 *((char *) __mempcpy (source, name, line - name)) = '\0';
599 new_service->name = source;
602 /* Set default actions. */
603 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
604 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
605 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
606 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
607 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
608 new_service->library = NULL;
609 new_service->known = NULL;
610 new_service->next = NULL;
612 while (isspace (line[0]))
617 /* Read criterions. */
620 while (line[0] != '\0' && isspace (line[0]));
625 enum nss_status status;
626 lookup_actions action;
628 /* Grok ! before name to mean all statii but that one. */
629 if (not = line[0] == '!')
632 /* Read status name. */
634 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
638 /* Compare with known statii. */
639 if (line - name == 7)
641 if (__strncasecmp (name, "SUCCESS", 7) == 0)
642 status = NSS_STATUS_SUCCESS;
643 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
644 status = NSS_STATUS_UNAVAIL;
648 else if (line - name == 8)
650 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
651 status = NSS_STATUS_NOTFOUND;
652 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
653 status = NSS_STATUS_TRYAGAIN;
660 while (isspace (line[0]))
666 while (isspace (line[0]));
669 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
673 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
674 action = NSS_ACTION_RETURN;
675 else if (line - name == 8
676 && __strncasecmp (name, "CONTINUE", 8) == 0)
677 action = NSS_ACTION_CONTINUE;
683 /* Save the current action setting for this status,
684 set them all to the given action, and reset this one. */
685 const lookup_actions save = new_service->actions[2 + status];
686 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
687 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
688 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
689 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
690 new_service->actions[2 + status] = save;
693 new_service->actions[2 + status] = action;
695 /* Skip white spaces. */
696 while (isspace (line[0]))
699 while (line[0] != ']');
705 *nextp = new_service;
706 nextp = &new_service->next;
710 static name_database_entry *
712 nss_getline (char *line)
715 name_database_entry *result;
717 /* Ignore leading white spaces. ATTENTION: this is different from
718 what is implemented in Solaris. The Solaris man page says a line
719 beginning with a white space character is ignored. We regard
720 this as just another misfeature in Solaris. */
721 while (isspace (line[0]))
724 /* Recognize `<database> ":"'. */
726 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
728 if (line[0] == '\0' || name == line)
733 result = (name_database_entry *) malloc (sizeof (name_database_entry));
737 /* Save the database name. */
739 const size_t len = strlen (name) + 1;
740 char *new = malloc (len);
746 result->name = memcpy (new, name, len);
749 /* Parse the list of services. */
750 result->service = nss_parse_service_list (line);
757 static service_library *
759 nss_new_service (name_database *database, const char *name)
761 service_library **currentp = &database->library;
763 while (*currentp != NULL)
765 if (strcmp ((*currentp)->name, name) == 0)
767 currentp = &(*currentp)->next;
770 /* We have to add the new service. */
771 *currentp = (service_library *) malloc (sizeof (service_library));
772 if (*currentp == NULL)
775 (*currentp)->name = name;
776 (*currentp)->lib_handle = NULL;
777 (*currentp)->next = NULL;