Update.
[glibc.git] / nss / nsswitch.c
blob7dd3e0526f87bb967c224de62a0071ae7a568fc1
1 /* Copyright (C) 1996, 1997 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 <link.h> /* We need some help from ld.so. */
26 #include <search.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
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 static name_database *nss_parse_file (const char *fname);
40 static name_database_entry *nss_getline (char *line);
41 static service_user *nss_parse_service_list (const char *line);
42 static service_library *nss_new_service (name_database *database,
43 const char *name);
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)
70 #if !defined DO_STATIC_NSS || defined PIC
71 /* String with revision number of the shared object files. */
72 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
73 #endif
75 /* The root of the whole data base. */
76 static name_database *service_table;
79 /* -1 == database not found
80 0 == database entry pointer stored */
81 int
82 __nss_database_lookup (const char *database, const char *alternate_name,
83 const char *defconfig, service_user **ni)
85 /* Prevent multiple threads to change the service table. */
86 __libc_lock_lock (lock);
88 /* Reconsider database variable in case some other thread called
89 `__nss_configure_lookup' while we waited for the lock. */
90 if (*ni != NULL)
92 __libc_lock_unlock (lock);
93 return 0;
96 /* Are we initialized yet? */
97 if (service_table == NULL)
98 /* Read config file. */
99 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
101 /* Test whether configuration data is available. */
102 if (service_table != NULL)
104 /* Return first `service_user' entry for DATABASE. */
105 name_database_entry *entry;
107 /* XXX Could use some faster mechanism here. But each database is
108 only requested once and so this might not be critical. */
109 for (entry = service_table->entry; entry != NULL; entry = entry->next)
110 if (strcmp (database, entry->name) == 0)
111 *ni = entry->service;
113 if (*ni == NULL && alternate_name != NULL)
114 /* We haven't found an entry so far. Try to find it with the
115 alternative name. */
116 for (entry = service_table->entry; entry != NULL; entry = entry->next)
117 if (strcmp (alternate_name, entry->name) == 0)
118 *ni = entry->service;
121 /* No configuration data is available, either because nsswitch.conf
122 doesn't exist or because it doesn't has a line for this database.
124 DEFCONFIG specifies the default service list for this database,
125 or null to use the most common default. */
126 if (*ni == NULL)
127 *ni = nss_parse_service_list (defconfig
128 ?: "nis [NOTFOUND=return] files");
130 __libc_lock_unlock (lock);
132 return 0;
136 /* -1 == not found
137 0 == adjusted for next function */
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 : -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 const char *last_object_name = NULL;
254 int result;
256 (void) _dl_catch_error (&last_errstring, &last_object_name, operate, args);
258 result = last_errstring != NULL;
259 if (result)
260 free (last_errstring);
262 return result;
266 struct do_open_args
268 /* Argument to do_open. */
269 char *shlib_name;
270 service_user *ni;
273 struct get_sym_args
275 /* Arguments to get_sym. */
276 struct link_map *map;
277 char *name;
279 /* Return values of get_sym. */
280 ElfW(Addr) loadbase;
281 const ElfW(Sym) *ref;
284 static void
285 do_open (void *a)
287 struct do_open_args *args = (struct do_open_args *) a;
288 /* Open and relocate the shared object. */
289 args->ni->library->lib_handle = _dl_open (args->shlib_name, RTLD_LAZY);
292 static void
293 get_sym (void *a)
295 struct get_sym_args *args = (struct get_sym_args *) a;
296 struct link_map *scope[2] = { args->map, NULL };
297 args->ref = NULL;
298 args->loadbase = _dl_lookup_symbol (args->name, &args->ref,
299 scope, args->map->l_name, 0);
301 #endif
303 /* Comparison function for searching NI->known tree. */
304 static int
305 known_compare (const void *p1, const void *p2)
307 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
308 *(const char *const *) p2);
312 static void *
313 nss_lookup_function (service_user *ni, const char *fct_name)
315 void **found, *result;
317 /* We now modify global data. Protect it. */
318 __libc_lock_lock (lock);
320 /* Search the tree of functions previously requested. Data in the
321 tree are `known_function' structures, whose first member is a
322 `const char *', the lookup key. The search returns a pointer to
323 the tree node structure; the first member of the is a pointer to
324 our structure (i.e. what will be a `known_function'); since the
325 first member of that is the lookup key string, &FCT_NAME is close
326 enough to a pointer to our structure to use as a lookup key that
327 will be passed to `known_compare' (above). */
329 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
330 if (*found != &fct_name)
331 /* The search found an existing structure in the tree. */
332 result = ((known_function *) *found)->fct_ptr;
333 else
335 /* This name was not known before. Now we have a node in the tree
336 (in the proper sorted position for FCT_NAME) that points to
337 &FCT_NAME instead of any real `known_function' structure.
338 Allocate a new structure and fill it in. */
340 known_function *known = malloc (sizeof *known);
341 if (! known)
343 remove_from_tree:
344 /* Oops. We can't instantiate this node properly.
345 Remove it from the tree. */
346 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
347 result = NULL;
349 else
351 /* Point the tree node at this new structure. */
352 *found = known;
353 known->fct_name = fct_name;
355 if (ni->library == NULL)
357 /* This service has not yet been used. Fetch the service
358 library for it, creating a new one if need be. If there
359 is no service table from the file, this static variable
360 holds the head of the service_library list made from the
361 default configuration. */
362 static name_database default_table;
363 ni->library = nss_new_service (service_table ?: &default_table,
364 ni->name);
365 if (ni->library == NULL)
367 /* This only happens when out of memory. */
368 free (known);
369 goto remove_from_tree;
373 #if !defined DO_STATIC_NSS || defined PIC
374 if (ni->library->lib_handle == NULL)
376 /* Load the shared library. */
377 size_t shlen = (7 + strlen (ni->library->name) + 3
378 + strlen (__nss_shlib_revision) + 1);
380 struct do_open_args args;
381 args.shlib_name = __alloca (shlen);
382 args.ni = ni;
384 /* Construct shared object name. */
385 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.shlib_name,
386 "libnss_"),
387 ni->library->name),
388 ".so"),
389 __nss_shlib_revision);
391 if (nss_dlerror_run (do_open, &args) != 0)
392 /* Failed to load the library. */
393 ni->library->lib_handle = (void *) -1l;
396 if (ni->library->lib_handle == (void *) -1l)
397 /* Library not found => function not found. */
398 result = NULL;
399 else
401 /* Get the desired function. Again, GNU ld.so magic ahead. */
402 size_t namlen = (5 + strlen (ni->library->name) + 1
403 + strlen (fct_name) + 1);
404 struct get_sym_args args;
405 args.name = __alloca (namlen);
406 args.map = ni->library->lib_handle;
408 /* Construct the function name. */
409 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.name, "_nss_"),
410 ni->library->name),
411 "_"),
412 fct_name);
414 /* Look up the symbol. */
415 result = (nss_dlerror_run (get_sym, &args) ? NULL
416 : (void *) (args.loadbase + args.ref->st_value));
418 #else
419 /* We can't get function address dynamically in static linking. */
421 # define DEFINE_ENT(h,nm) \
422 extern void _nss_##h##_get##nm##ent_r (void); \
423 extern void _nss_##h##_end##nm##ent (void); \
424 extern void _nss_##h##_set##nm##ent (void);
425 # define DEFINE_GET(h,nm) \
426 extern void _nss_##h##_get##nm##_r (void);
427 # define DEFINE_GETBY(h,nm,ky) \
428 extern void _nss_##h##_get##nm##by##ky##_r (void);
429 # include "function.def"
430 # undef DEFINE_ENT
431 # undef DEFINE_GET
432 # undef DEFINE_GETBY
433 # define DEFINE_ENT(h,nm) \
434 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
435 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
436 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
437 # define DEFINE_GET(h,nm) \
438 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
439 # define DEFINE_GETBY(h,nm,ky) \
440 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
441 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
443 # include "function.def"
444 { NULL, NULL }
446 size_t namlen = (5 + strlen (ni->library->name) + 1
447 + strlen (fct_name) + 1);
448 char name[namlen];
450 /* Construct the function name. */
451 __stpcpy (__stpcpy (__stpcpy (name, ni->library->name),
452 "_"),
453 fct_name);
455 result = NULL;
456 for (tp = &tbl[0]; tp->fname; tp++)
457 if (strcmp (tp->fname, name) == 0)
459 result = tp->fp;
460 break;
463 #endif
465 /* Remember function pointer for later calls. Even if null, we
466 record it so a second try needn't search the library again. */
467 known->fct_ptr = result;
471 /* Remove the lock. */
472 __libc_lock_unlock (lock);
474 return result;
478 static name_database *
479 nss_parse_file (const char *fname)
481 FILE *fp;
482 name_database *result;
483 name_database_entry *last;
484 char *line;
485 size_t len;
487 /* Open the configuration file. */
488 fp = fopen (fname, "r");
489 if (fp == NULL)
490 return NULL;
492 result = (name_database *) malloc (sizeof (name_database));
493 if (result == NULL)
494 return NULL;
496 result->entry = NULL;
497 result->library = NULL;
498 last = NULL;
499 line = NULL;
500 len = 0;
503 name_database_entry *this;
504 ssize_t n;
505 char *cp;
507 n = __getline (&line, &len, fp);
508 if (n < 0)
509 break;
510 if (line[n - 1] == '\n')
511 line[n - 1] = '\0';
513 /* Because the file format does not know any form of quoting we
514 can search forward for the next '#' character and if found
515 make it terminating the line. */
516 cp = strchr (line, '#');
517 if (cp != NULL)
518 *cp = '\0';
520 /* If the line is blank it is ignored. */
521 if (line[0] == '\0')
522 continue;
524 /* Each line completely specifies the actions for a database. */
525 this = nss_getline (line);
526 if (this != NULL)
528 if (last != NULL)
529 last->next = this;
530 else
531 result->entry = this;
533 last = this;
536 while (!feof (fp));
538 /* Free the buffer. */
539 free (line);
540 /* Close configuration file. */
541 fclose (fp);
543 return result;
547 /* Read the source names:
548 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
550 static service_user *
551 nss_parse_service_list (const char *line)
553 service_user *result = NULL, **nextp = &result;
555 while (1)
557 service_user *new_service;
558 const char *name;
560 while (isspace (line[0]))
561 ++line;
562 if (line[0] == '\0')
563 /* No source specified. */
564 return result;
566 /* Read <source> identifier. */
567 name = line;
568 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
569 ++line;
570 if (name == line)
571 return result;
574 new_service = (service_user *) malloc (sizeof (service_user));
575 if (new_service == NULL)
576 return result;
577 else
579 char *source = (char *) malloc (line - name + 1);
580 if (source == NULL)
582 free (new_service);
583 return result;
585 memcpy (source, name, line - name);
586 source[line - name] = '\0';
588 new_service->name = source;
591 /* Set default actions. */
592 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
593 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
594 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
595 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
596 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
597 new_service->library = NULL;
598 new_service->known = NULL;
599 new_service->next = NULL;
601 while (isspace (line[0]))
602 ++line;
604 if (line[0] == '[')
606 /* Read criterions. */
608 ++line;
609 while (line[0] != '\0' && isspace (line[0]));
613 int not;
614 enum nss_status status;
615 lookup_actions action;
617 /* Grok ! before name to mean all statii but that one. */
618 if (not = line[0] == '!')
619 ++line;
621 /* Read status name. */
622 name = line;
623 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
624 && line[0] != ']')
625 ++line;
627 /* Compare with known statii. */
628 if (line - name == 7)
630 if (__strncasecmp (name, "SUCCESS", 7) == 0)
631 status = NSS_STATUS_SUCCESS;
632 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
633 status = NSS_STATUS_UNAVAIL;
634 else
635 return result;
637 else if (line - name == 8)
639 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
640 status = NSS_STATUS_NOTFOUND;
641 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
642 status = NSS_STATUS_TRYAGAIN;
643 else
644 return result;
646 else
647 return result;
649 while (isspace (line[0]))
650 ++line;
651 if (line[0] != '=')
652 return result;
654 ++line;
655 while (isspace (line[0]));
657 name = line;
658 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
659 && line[0] != ']')
660 ++line;
662 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
663 action = NSS_ACTION_RETURN;
664 else if (line - name == 8
665 && __strncasecmp (name, "CONTINUE", 8) == 0)
666 action = NSS_ACTION_CONTINUE;
667 else
668 return result;
670 if (not)
672 /* Save the current action setting for this status,
673 set them all to the given action, and reset this one. */
674 const lookup_actions save = new_service->actions[2 + status];
675 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
676 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
677 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
678 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
679 new_service->actions[2 + status] = save;
681 else
682 new_service->actions[2 + status] = action;
684 /* Skip white spaces. */
685 while (isspace (line[0]))
686 ++line;
688 while (line[0] != ']');
690 /* Skip the ']'. */
691 ++line;
694 *nextp = new_service;
695 nextp = &new_service->next;
699 static name_database_entry *
700 nss_getline (char *line)
702 const char *name;
703 name_database_entry *result;
705 /* Ignore leading white spaces. ATTENTION: this is different from
706 what is implemented in Solaris. The Solaris man page says a line
707 beginning with a white space character is ignored. We regard
708 this as just another misfeature in Solaris. */
709 while (isspace (line[0]))
710 ++line;
712 /* Recognize `<database> ":"'. */
713 name = line;
714 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
715 ++line;
716 if (line[0] == '\0' || name == line)
717 /* Syntax error. */
718 return NULL;
719 *line++ = '\0';
721 result = (name_database_entry *) malloc (sizeof (name_database_entry));
722 if (result == NULL)
723 return NULL;
725 /* Save the database name. */
727 const size_t len = strlen (name) + 1;
728 char *new = malloc (len);
729 if (new == NULL)
731 free (result);
732 return NULL;
734 result->name = memcpy (new, name, len);
737 /* Parse the list of services. */
738 result->service = nss_parse_service_list (line);
740 result->next = NULL;
741 return result;
745 static service_library *
746 nss_new_service (name_database *database, const char *name)
748 service_library **currentp = &database->library;
750 while (*currentp != NULL)
752 if (strcmp ((*currentp)->name, name) == 0)
753 return *currentp;
754 currentp = &(*currentp)->next;
757 /* We have to add the new service. */
758 *currentp = (service_library *) malloc (sizeof (service_library));
759 if (*currentp == NULL)
760 return NULL;
762 (*currentp)->name = name;
763 (*currentp)->lib_handle = NULL;
764 (*currentp)->next = NULL;
766 return *currentp;