* sysdeps/m68k/dl-machine.h (elf_machine_runtime_setup): Set
[glibc.git] / nss / nsswitch.c
blobc968502cdfa4482bc99be25194fbce6c6b66e6f6
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 <elf/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 void *nss_lookup_function (service_user *ni, const char *fct_name)
39 internal_function;
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)
43 internal_function;
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. */
56 static struct
58 const char *name;
59 service_user **dbp;
60 } databases[] =
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)
71 #if !defined DO_STATIC_NSS || defined PIC
72 /* String with revision number of the shared object files. */
73 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
74 #endif
76 /* The root of the whole data base. */
77 static name_database *service_table;
80 /* -1 == database not found
81 0 == database entry pointer stored */
82 int
83 __nss_database_lookup (const char *database, const char *alternate_name,
84 const char *defconfig, service_user **ni)
86 /* Prevent multiple threads to change the service table. */
87 __libc_lock_lock (lock);
89 /* Reconsider database variable in case some other thread called
90 `__nss_configure_lookup' while we waited for the lock. */
91 if (*ni != NULL)
93 __libc_lock_unlock (lock);
94 return 0;
97 /* Are we initialized yet? */
98 if (service_table == NULL)
99 /* Read config file. */
100 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
102 /* Test whether configuration data is available. */
103 if (service_table != NULL)
105 /* Return first `service_user' entry for DATABASE. */
106 name_database_entry *entry;
108 /* XXX Could use some faster mechanism here. But each database is
109 only requested once and so this might not be critical. */
110 for (entry = service_table->entry; entry != NULL; entry = entry->next)
111 if (strcmp (database, entry->name) == 0)
112 *ni = entry->service;
114 if (*ni == NULL && alternate_name != NULL)
115 /* We haven't found an entry so far. Try to find it with the
116 alternative name. */
117 for (entry = service_table->entry; entry != NULL; entry = entry->next)
118 if (strcmp (alternate_name, entry->name) == 0)
119 *ni = entry->service;
122 /* No configuration data is available, either because nsswitch.conf
123 doesn't exist or because it doesn't has a line for this database.
125 DEFCONFIG specifies the default service list for this database,
126 or null to use the most common default. */
127 if (*ni == NULL)
128 *ni = nss_parse_service_list (defconfig
129 ?: "nis [NOTFOUND=return] files");
131 __libc_lock_unlock (lock);
133 return 0;
137 /* -1 == not found
138 0 == function found
139 1 == finished */
141 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
143 *fctp = nss_lookup_function (*ni, fct_name);
145 while (*fctp == NULL
146 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
147 && (*ni)->next != NULL)
149 *ni = (*ni)->next;
151 *fctp = nss_lookup_function (*ni, fct_name);
154 return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
158 /* -1 == not found
159 0 == adjusted for next function
160 1 == finished */
162 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
163 int all_values)
165 if (all_values)
167 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
168 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
169 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
170 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
171 return 1;
173 else
175 /* This is really only for debugging. */
176 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
177 __libc_fatal ("illegal status in " __FUNCTION__);
179 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
180 return 1;
183 if ((*ni)->next == NULL)
184 return -1;
188 *ni = (*ni)->next;
190 *fctp = nss_lookup_function (*ni, fct_name);
192 while (*fctp == NULL
193 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
194 && (*ni)->next != NULL);
196 return *fctp != NULL ? 0 : -1;
201 __nss_configure_lookup (const char *dbname, const char *service_line)
203 service_user *new_db;
204 size_t cnt;
206 for (cnt = 0; cnt < sizeof databases; ++cnt)
208 int cmp = strcmp (dbname, databases[cnt].name);
209 if (cmp == 0)
210 break;
211 if (cmp < 0)
213 __set_errno (EINVAL);
214 return -1;
218 if (cnt == sizeof databases)
220 __set_errno (EINVAL);
221 return -1;
224 /* Test whether it is really used. */
225 if (databases[cnt].dbp == NULL)
226 /* Nothing to do, but we could do. */
227 return 0;
229 /* Try to generate new data. */
230 new_db = nss_parse_service_list (service_line);
231 if (new_db == NULL)
233 /* Illegal service specification. */
234 __set_errno (EINVAL);
235 return -1;
238 /* Prevent multiple threads to change the service table. */
239 __libc_lock_lock (lock);
241 /* Install new rules. */
242 *databases[cnt].dbp = new_db;
244 __libc_lock_unlock (lock);
246 return 0;
250 #if !defined DO_STATIC_NSS || defined PIC
251 static int
252 nss_dlerror_run (void (*operate) (void *), void *args)
254 char *last_errstring = NULL;
255 int result;
257 (void) _dl_catch_error (&last_errstring, operate, args);
259 result = last_errstring != NULL;
260 if (result)
261 free (last_errstring);
263 return result;
267 struct do_open_args
269 /* Argument to do_open. */
270 char *shlib_name;
271 service_user *ni;
274 struct get_sym_args
276 /* Arguments to get_sym. */
277 struct link_map *map;
278 char *name;
280 /* Return values of get_sym. */
281 ElfW(Addr) loadbase;
282 const ElfW(Sym) *ref;
285 static void
286 do_open (void *a)
288 struct do_open_args *args = (struct do_open_args *) a;
289 /* Open and relocate the shared object. */
290 args->ni->library->lib_handle = _dl_open (args->shlib_name, RTLD_LAZY);
293 static void
294 get_sym (void *a)
296 struct get_sym_args *args = (struct get_sym_args *) a;
297 struct link_map *scope[2] = { args->map, NULL };
298 args->ref = NULL;
299 args->loadbase = _dl_lookup_symbol (args->name, &args->ref,
300 scope, args->map->l_name, 0);
302 #endif
304 /* Comparison function for searching NI->known tree. */
305 static int
306 known_compare (const void *p1, const void *p2)
308 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
309 *(const char *const *) p2);
313 static void *
314 internal_function
315 nss_lookup_function (service_user *ni, const char *fct_name)
317 void **found, *result;
319 /* We now modify global data. Protect it. */
320 __libc_lock_lock (lock);
322 /* Search the tree of functions previously requested. Data in the
323 tree are `known_function' structures, whose first member is a
324 `const char *', the lookup key. The search returns a pointer to
325 the tree node structure; the first member of the is a pointer to
326 our structure (i.e. what will be a `known_function'); since the
327 first member of that is the lookup key string, &FCT_NAME is close
328 enough to a pointer to our structure to use as a lookup key that
329 will be passed to `known_compare' (above). */
331 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
332 if (*found != &fct_name)
333 /* The search found an existing structure in the tree. */
334 result = ((known_function *) *found)->fct_ptr;
335 else
337 /* This name was not known before. Now we have a node in the tree
338 (in the proper sorted position for FCT_NAME) that points to
339 &FCT_NAME instead of any real `known_function' structure.
340 Allocate a new structure and fill it in. */
342 known_function *known = malloc (sizeof *known);
343 if (! known)
345 remove_from_tree:
346 /* Oops. We can't instantiate this node properly.
347 Remove it from the tree. */
348 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
349 result = NULL;
351 else
353 /* Point the tree node at this new structure. */
354 *found = known;
355 known->fct_name = fct_name;
357 if (ni->library == NULL)
359 /* This service has not yet been used. Fetch the service
360 library for it, creating a new one if need be. If there
361 is no service table from the file, this static variable
362 holds the head of the service_library list made from the
363 default configuration. */
364 static name_database default_table;
365 ni->library = nss_new_service (service_table ?: &default_table,
366 ni->name);
367 if (ni->library == NULL)
369 /* This only happens when out of memory. */
370 free (known);
371 goto remove_from_tree;
375 #if !defined DO_STATIC_NSS || defined PIC
376 if (ni->library->lib_handle == NULL)
378 /* Load the shared library. */
379 size_t shlen = (7 + strlen (ni->library->name) + 3
380 + strlen (__nss_shlib_revision) + 1);
381 int saved_errno = errno;
382 struct do_open_args args;
383 args.shlib_name = __alloca (shlen);
384 args.ni = ni;
386 /* Construct shared object name. */
387 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.shlib_name,
388 "libnss_"),
389 ni->library->name),
390 ".so"),
391 __nss_shlib_revision);
393 if (nss_dlerror_run (do_open, &args) != 0)
395 /* Failed to load the library. */
396 ni->library->lib_handle = (void *) -1l;
397 __set_errno (saved_errno);
401 if (ni->library->lib_handle == (void *) -1l)
402 /* Library not found => function not found. */
403 result = NULL;
404 else
406 /* Get the desired function. Again, GNU ld.so magic ahead. */
407 size_t namlen = (5 + strlen (ni->library->name) + 1
408 + strlen (fct_name) + 1);
409 struct get_sym_args args;
410 args.name = __alloca (namlen);
411 args.map = ni->library->lib_handle;
413 /* Construct the function name. */
414 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.name, "_nss_"),
415 ni->library->name),
416 "_"),
417 fct_name);
419 /* Look up the symbol. */
420 result = (nss_dlerror_run (get_sym, &args) ? NULL
421 : (void *) (args.loadbase + args.ref->st_value));
423 #else
424 /* We can't get function address dynamically in static linking. */
426 # define DEFINE_ENT(h,nm) \
427 extern void _nss_##h##_get##nm##ent_r (void); \
428 extern void _nss_##h##_end##nm##ent (void); \
429 extern void _nss_##h##_set##nm##ent (void);
430 # define DEFINE_GET(h,nm) \
431 extern void _nss_##h##_get##nm##_r (void);
432 # define DEFINE_GETBY(h,nm,ky) \
433 extern void _nss_##h##_get##nm##by##ky##_r (void);
434 # include "function.def"
435 # undef DEFINE_ENT
436 # undef DEFINE_GET
437 # undef DEFINE_GETBY
438 # define DEFINE_ENT(h,nm) \
439 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
440 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
441 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
442 # define DEFINE_GET(h,nm) \
443 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
444 # define DEFINE_GETBY(h,nm,ky) \
445 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
446 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
448 # include "function.def"
449 { NULL, NULL }
451 size_t namlen = (5 + strlen (ni->library->name) + 1
452 + strlen (fct_name) + 1);
453 char name[namlen];
455 /* Construct the function name. */
456 __stpcpy (__stpcpy (__stpcpy (name, ni->library->name),
457 "_"),
458 fct_name);
460 result = NULL;
461 for (tp = &tbl[0]; tp->fname; tp++)
462 if (strcmp (tp->fname, name) == 0)
464 result = tp->fp;
465 break;
468 #endif
470 /* Remember function pointer for later calls. Even if null, we
471 record it so a second try needn't search the library again. */
472 known->fct_ptr = result;
476 /* Remove the lock. */
477 __libc_lock_unlock (lock);
479 return result;
483 static name_database *
484 internal_function
485 nss_parse_file (const char *fname)
487 FILE *fp;
488 name_database *result;
489 name_database_entry *last;
490 char *line;
491 size_t len;
493 /* Open the configuration file. */
494 fp = fopen (fname, "r");
495 if (fp == NULL)
496 return NULL;
498 result = (name_database *) malloc (sizeof (name_database));
499 if (result == NULL)
500 return NULL;
502 result->entry = NULL;
503 result->library = NULL;
504 last = NULL;
505 line = NULL;
506 len = 0;
509 name_database_entry *this;
510 ssize_t n;
511 char *cp;
513 n = __getline (&line, &len, fp);
514 if (n < 0)
515 break;
516 if (line[n - 1] == '\n')
517 line[n - 1] = '\0';
519 /* Because the file format does not know any form of quoting we
520 can search forward for the next '#' character and if found
521 make it terminating the line. */
522 cp = strchr (line, '#');
523 if (cp != NULL)
524 *cp = '\0';
526 /* If the line is blank it is ignored. */
527 if (line[0] == '\0')
528 continue;
530 /* Each line completely specifies the actions for a database. */
531 this = nss_getline (line);
532 if (this != NULL)
534 if (last != NULL)
535 last->next = this;
536 else
537 result->entry = this;
539 last = this;
542 while (!feof (fp));
544 /* Free the buffer. */
545 free (line);
546 /* Close configuration file. */
547 fclose (fp);
549 return result;
553 /* Read the source names:
554 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
556 static service_user *
557 internal_function
558 nss_parse_service_list (const char *line)
560 service_user *result = NULL, **nextp = &result;
562 while (1)
564 service_user *new_service;
565 const char *name;
567 while (isspace (line[0]))
568 ++line;
569 if (line[0] == '\0')
570 /* No source specified. */
571 return result;
573 /* Read <source> identifier. */
574 name = line;
575 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
576 ++line;
577 if (name == line)
578 return result;
581 new_service = (service_user *) malloc (sizeof (service_user));
582 if (new_service == NULL)
583 return result;
584 else
586 char *source = (char *) malloc (line - name + 1);
587 if (source == NULL)
589 free (new_service);
590 return result;
592 *((char *) __mempcpy (source, name, line - name)) = '\0';
594 new_service->name = source;
597 /* Set default actions. */
598 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
599 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
600 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
601 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
602 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
603 new_service->library = NULL;
604 new_service->known = NULL;
605 new_service->next = NULL;
607 while (isspace (line[0]))
608 ++line;
610 if (line[0] == '[')
612 /* Read criterions. */
614 ++line;
615 while (line[0] != '\0' && isspace (line[0]));
619 int not;
620 enum nss_status status;
621 lookup_actions action;
623 /* Grok ! before name to mean all statii but that one. */
624 if (not = line[0] == '!')
625 ++line;
627 /* Read status name. */
628 name = line;
629 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
630 && line[0] != ']')
631 ++line;
633 /* Compare with known statii. */
634 if (line - name == 7)
636 if (__strncasecmp (name, "SUCCESS", 7) == 0)
637 status = NSS_STATUS_SUCCESS;
638 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
639 status = NSS_STATUS_UNAVAIL;
640 else
641 return result;
643 else if (line - name == 8)
645 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
646 status = NSS_STATUS_NOTFOUND;
647 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
648 status = NSS_STATUS_TRYAGAIN;
649 else
650 return result;
652 else
653 return result;
655 while (isspace (line[0]))
656 ++line;
657 if (line[0] != '=')
658 return result;
660 ++line;
661 while (isspace (line[0]));
663 name = line;
664 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
665 && line[0] != ']')
666 ++line;
668 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
669 action = NSS_ACTION_RETURN;
670 else if (line - name == 8
671 && __strncasecmp (name, "CONTINUE", 8) == 0)
672 action = NSS_ACTION_CONTINUE;
673 else
674 return result;
676 if (not)
678 /* Save the current action setting for this status,
679 set them all to the given action, and reset this one. */
680 const lookup_actions save = new_service->actions[2 + status];
681 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
682 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
683 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
684 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
685 new_service->actions[2 + status] = save;
687 else
688 new_service->actions[2 + status] = action;
690 /* Skip white spaces. */
691 while (isspace (line[0]))
692 ++line;
694 while (line[0] != ']');
696 /* Skip the ']'. */
697 ++line;
700 *nextp = new_service;
701 nextp = &new_service->next;
705 static name_database_entry *
706 internal_function
707 nss_getline (char *line)
709 const char *name;
710 name_database_entry *result;
712 /* Ignore leading white spaces. ATTENTION: this is different from
713 what is implemented in Solaris. The Solaris man page says a line
714 beginning with a white space character is ignored. We regard
715 this as just another misfeature in Solaris. */
716 while (isspace (line[0]))
717 ++line;
719 /* Recognize `<database> ":"'. */
720 name = line;
721 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
722 ++line;
723 if (line[0] == '\0' || name == line)
724 /* Syntax error. */
725 return NULL;
726 *line++ = '\0';
728 result = (name_database_entry *) malloc (sizeof (name_database_entry));
729 if (result == NULL)
730 return NULL;
732 /* Save the database name. */
734 const size_t len = strlen (name) + 1;
735 char *new = malloc (len);
736 if (new == NULL)
738 free (result);
739 return NULL;
741 result->name = memcpy (new, name, len);
744 /* Parse the list of services. */
745 result->service = nss_parse_service_list (line);
747 result->next = NULL;
748 return result;
752 static service_library *
753 internal_function
754 nss_new_service (name_database *database, const char *name)
756 service_library **currentp = &database->library;
758 while (*currentp != NULL)
760 if (strcmp ((*currentp)->name, name) == 0)
761 return *currentp;
762 currentp = &(*currentp)->next;
765 /* We have to add the new service. */
766 *currentp = (service_library *) malloc (sizeof (service_library));
767 if (*currentp == NULL)
768 return NULL;
770 (*currentp)->name = name;
771 (*currentp)->lib_handle = NULL;
772 (*currentp)->next = NULL;
774 return *currentp;