Update.
[glibc.git] / nss / nsswitch.c
blob18129bb5c017f86dbd53fe6be985ea1bc17ee273
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 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)
72 #if !defined DO_STATIC_NSS || defined PIC
73 /* String with revision number of the shared object files. */
74 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
75 #endif
77 /* The root of the whole data base. */
78 static name_database *service_table;
81 /* -1 == database not found
82 0 == database entry pointer stored */
83 int
84 __nss_database_lookup (const char *database, const char *alternate_name,
85 const char *defconfig, service_user **ni)
87 /* Prevent multiple threads to change the service table. */
88 __libc_lock_lock (lock);
90 /* Reconsider database variable in case some other thread called
91 `__nss_configure_lookup' while we waited for the lock. */
92 if (*ni != NULL)
94 __libc_lock_unlock (lock);
95 return 0;
98 /* Are we initialized yet? */
99 if (service_table == NULL)
100 /* Read config file. */
101 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
103 /* Test whether configuration data is available. */
104 if (service_table != NULL)
106 /* Return first `service_user' entry for DATABASE. */
107 name_database_entry *entry;
109 /* XXX Could use some faster mechanism here. But each database is
110 only requested once and so this might not be critical. */
111 for (entry = service_table->entry; entry != NULL; entry = entry->next)
112 if (strcmp (database, entry->name) == 0)
113 *ni = entry->service;
115 if (*ni == NULL && alternate_name != NULL)
116 /* We haven't found an entry so far. Try to find it with the
117 alternative name. */
118 for (entry = service_table->entry; entry != NULL; entry = entry->next)
119 if (strcmp (alternate_name, entry->name) == 0)
120 *ni = entry->service;
123 /* No configuration data is available, either because nsswitch.conf
124 doesn't exist or because it doesn't has a line for this database.
126 DEFCONFIG specifies the default service list for this database,
127 or null to use the most common default. */
128 if (*ni == NULL)
129 *ni = nss_parse_service_list (defconfig
130 ?: "nis [NOTFOUND=return] files");
132 __libc_lock_unlock (lock);
134 return 0;
138 /* -1 == not found
139 0 == adjusted for next function */
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 : -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 const char *last_object_name = NULL;
256 int result;
258 (void) _dl_catch_error (&last_errstring, &last_object_name, operate, args);
260 result = last_errstring != NULL;
261 if (result)
262 free (last_errstring);
264 return result;
268 struct do_open_args
270 /* Argument to do_open. */
271 char *shlib_name;
272 service_user *ni;
275 struct get_sym_args
277 /* Arguments to get_sym. */
278 struct link_map *map;
279 char *name;
281 /* Return values of get_sym. */
282 ElfW(Addr) loadbase;
283 const ElfW(Sym) *ref;
286 static void
287 do_open (void *a)
289 struct do_open_args *args = (struct do_open_args *) a;
290 /* Open and relocate the shared object. */
291 args->ni->library->lib_handle = _dl_open (args->shlib_name, RTLD_LAZY);
294 static void
295 get_sym (void *a)
297 struct get_sym_args *args = (struct get_sym_args *) a;
298 struct link_map *scope[2] = { args->map, NULL };
299 args->ref = NULL;
300 args->loadbase = _dl_lookup_symbol (args->name, &args->ref,
301 scope, args->map->l_name, 0);
303 #endif
305 /* Comparison function for searching NI->known tree. */
306 static int
307 known_compare (const void *p1, const void *p2)
309 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
310 *(const char *const *) p2);
314 static void *
315 internal_function
316 nss_lookup_function (service_user *ni, const char *fct_name)
318 void **found, *result;
320 /* We now modify global data. Protect it. */
321 __libc_lock_lock (lock);
323 /* Search the tree of functions previously requested. Data in the
324 tree are `known_function' structures, whose first member is a
325 `const char *', the lookup key. The search returns a pointer to
326 the tree node structure; the first member of the is a pointer to
327 our structure (i.e. what will be a `known_function'); since the
328 first member of that is the lookup key string, &FCT_NAME is close
329 enough to a pointer to our structure to use as a lookup key that
330 will be passed to `known_compare' (above). */
332 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
333 if (*found != &fct_name)
334 /* The search found an existing structure in the tree. */
335 result = ((known_function *) *found)->fct_ptr;
336 else
338 /* This name was not known before. Now we have a node in the tree
339 (in the proper sorted position for FCT_NAME) that points to
340 &FCT_NAME instead of any real `known_function' structure.
341 Allocate a new structure and fill it in. */
343 known_function *known = malloc (sizeof *known);
344 if (! known)
346 remove_from_tree:
347 /* Oops. We can't instantiate this node properly.
348 Remove it from the tree. */
349 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
350 result = NULL;
352 else
354 /* Point the tree node at this new structure. */
355 *found = known;
356 known->fct_name = fct_name;
358 if (ni->library == NULL)
360 /* This service has not yet been used. Fetch the service
361 library for it, creating a new one if need be. If there
362 is no service table from the file, this static variable
363 holds the head of the service_library list made from the
364 default configuration. */
365 static name_database default_table;
366 ni->library = nss_new_service (service_table ?: &default_table,
367 ni->name);
368 if (ni->library == NULL)
370 /* This only happens when out of memory. */
371 free (known);
372 goto remove_from_tree;
376 #if !defined DO_STATIC_NSS || defined PIC
377 if (ni->library->lib_handle == NULL)
379 /* Load the shared library. */
380 size_t shlen = (7 + strlen (ni->library->name) + 3
381 + strlen (__nss_shlib_revision) + 1);
383 struct do_open_args args;
384 args.shlib_name = __alloca (shlen);
385 args.ni = ni;
387 /* Construct shared object name. */
388 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.shlib_name,
389 "libnss_"),
390 ni->library->name),
391 ".so"),
392 __nss_shlib_revision);
394 if (nss_dlerror_run (do_open, &args) != 0)
395 /* Failed to load the library. */
396 ni->library->lib_handle = (void *) -1l;
399 if (ni->library->lib_handle == (void *) -1l)
400 /* Library not found => function not found. */
401 result = NULL;
402 else
404 /* Get the desired function. Again, GNU ld.so magic ahead. */
405 size_t namlen = (5 + strlen (ni->library->name) + 1
406 + strlen (fct_name) + 1);
407 struct get_sym_args args;
408 args.name = __alloca (namlen);
409 args.map = ni->library->lib_handle;
411 /* Construct the function name. */
412 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.name, "_nss_"),
413 ni->library->name),
414 "_"),
415 fct_name);
417 /* Look up the symbol. */
418 result = (nss_dlerror_run (get_sym, &args) ? NULL
419 : (void *) (args.loadbase + args.ref->st_value));
421 #else
422 /* We can't get function address dynamically in static linking. */
424 # define DEFINE_ENT(h,nm) \
425 extern void _nss_##h##_get##nm##ent_r (void); \
426 extern void _nss_##h##_end##nm##ent (void); \
427 extern void _nss_##h##_set##nm##ent (void);
428 # define DEFINE_GET(h,nm) \
429 extern void _nss_##h##_get##nm##_r (void);
430 # define DEFINE_GETBY(h,nm,ky) \
431 extern void _nss_##h##_get##nm##by##ky##_r (void);
432 # include "function.def"
433 # undef DEFINE_ENT
434 # undef DEFINE_GET
435 # undef DEFINE_GETBY
436 # define DEFINE_ENT(h,nm) \
437 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
438 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
439 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
440 # define DEFINE_GET(h,nm) \
441 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
442 # define DEFINE_GETBY(h,nm,ky) \
443 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
444 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
446 # include "function.def"
447 { NULL, NULL }
449 size_t namlen = (5 + strlen (ni->library->name) + 1
450 + strlen (fct_name) + 1);
451 char name[namlen];
453 /* Construct the function name. */
454 __stpcpy (__stpcpy (__stpcpy (name, ni->library->name),
455 "_"),
456 fct_name);
458 result = NULL;
459 for (tp = &tbl[0]; tp->fname; tp++)
460 if (strcmp (tp->fname, name) == 0)
462 result = tp->fp;
463 break;
466 #endif
468 /* Remember function pointer for later calls. Even if null, we
469 record it so a second try needn't search the library again. */
470 known->fct_ptr = result;
474 /* Remove the lock. */
475 __libc_lock_unlock (lock);
477 return result;
481 static name_database *
482 internal_function
483 nss_parse_file (const char *fname)
485 FILE *fp;
486 name_database *result;
487 name_database_entry *last;
488 char *line;
489 size_t len;
491 /* Open the configuration file. */
492 fp = fopen (fname, "r");
493 if (fp == NULL)
494 return NULL;
496 result = (name_database *) malloc (sizeof (name_database));
497 if (result == NULL)
498 return NULL;
500 result->entry = NULL;
501 result->library = NULL;
502 last = NULL;
503 line = NULL;
504 len = 0;
507 name_database_entry *this;
508 ssize_t n;
509 char *cp;
511 n = __getline (&line, &len, fp);
512 if (n < 0)
513 break;
514 if (line[n - 1] == '\n')
515 line[n - 1] = '\0';
517 /* Because the file format does not know any form of quoting we
518 can search forward for the next '#' character and if found
519 make it terminating the line. */
520 cp = strchr (line, '#');
521 if (cp != NULL)
522 *cp = '\0';
524 /* If the line is blank it is ignored. */
525 if (line[0] == '\0')
526 continue;
528 /* Each line completely specifies the actions for a database. */
529 this = nss_getline (line);
530 if (this != NULL)
532 if (last != NULL)
533 last->next = this;
534 else
535 result->entry = this;
537 last = this;
540 while (!feof (fp));
542 /* Free the buffer. */
543 free (line);
544 /* Close configuration file. */
545 fclose (fp);
547 return result;
551 /* Read the source names:
552 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
554 static service_user *
555 internal_function
556 nss_parse_service_list (const char *line)
558 service_user *result = NULL, **nextp = &result;
560 while (1)
562 service_user *new_service;
563 const char *name;
565 while (isspace (line[0]))
566 ++line;
567 if (line[0] == '\0')
568 /* No source specified. */
569 return result;
571 /* Read <source> identifier. */
572 name = line;
573 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
574 ++line;
575 if (name == line)
576 return result;
579 new_service = (service_user *) malloc (sizeof (service_user));
580 if (new_service == NULL)
581 return result;
582 else
584 char *source = (char *) malloc (line - name + 1);
585 if (source == NULL)
587 free (new_service);
588 return result;
590 memcpy (source, name, line - name);
591 source[line - name] = '\0';
593 new_service->name = source;
596 /* Set default actions. */
597 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
598 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
599 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
600 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
601 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
602 new_service->library = NULL;
603 new_service->known = NULL;
604 new_service->next = NULL;
606 while (isspace (line[0]))
607 ++line;
609 if (line[0] == '[')
611 /* Read criterions. */
613 ++line;
614 while (line[0] != '\0' && isspace (line[0]));
618 int not;
619 enum nss_status status;
620 lookup_actions action;
622 /* Grok ! before name to mean all statii but that one. */
623 if (not = line[0] == '!')
624 ++line;
626 /* Read status name. */
627 name = line;
628 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
629 && line[0] != ']')
630 ++line;
632 /* Compare with known statii. */
633 if (line - name == 7)
635 if (__strncasecmp (name, "SUCCESS", 7) == 0)
636 status = NSS_STATUS_SUCCESS;
637 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
638 status = NSS_STATUS_UNAVAIL;
639 else
640 return result;
642 else if (line - name == 8)
644 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
645 status = NSS_STATUS_NOTFOUND;
646 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
647 status = NSS_STATUS_TRYAGAIN;
648 else
649 return result;
651 else
652 return result;
654 while (isspace (line[0]))
655 ++line;
656 if (line[0] != '=')
657 return result;
659 ++line;
660 while (isspace (line[0]));
662 name = line;
663 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
664 && line[0] != ']')
665 ++line;
667 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
668 action = NSS_ACTION_RETURN;
669 else if (line - name == 8
670 && __strncasecmp (name, "CONTINUE", 8) == 0)
671 action = NSS_ACTION_CONTINUE;
672 else
673 return result;
675 if (not)
677 /* Save the current action setting for this status,
678 set them all to the given action, and reset this one. */
679 const lookup_actions save = new_service->actions[2 + status];
680 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
681 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
682 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
683 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
684 new_service->actions[2 + status] = save;
686 else
687 new_service->actions[2 + status] = action;
689 /* Skip white spaces. */
690 while (isspace (line[0]))
691 ++line;
693 while (line[0] != ']');
695 /* Skip the ']'. */
696 ++line;
699 *nextp = new_service;
700 nextp = &new_service->next;
704 static name_database_entry *
705 internal_function
706 nss_getline (char *line)
708 const char *name;
709 name_database_entry *result;
711 /* Ignore leading white spaces. ATTENTION: this is different from
712 what is implemented in Solaris. The Solaris man page says a line
713 beginning with a white space character is ignored. We regard
714 this as just another misfeature in Solaris. */
715 while (isspace (line[0]))
716 ++line;
718 /* Recognize `<database> ":"'. */
719 name = line;
720 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
721 ++line;
722 if (line[0] == '\0' || name == line)
723 /* Syntax error. */
724 return NULL;
725 *line++ = '\0';
727 result = (name_database_entry *) malloc (sizeof (name_database_entry));
728 if (result == NULL)
729 return NULL;
731 /* Save the database name. */
733 const size_t len = strlen (name) + 1;
734 char *new = malloc (len);
735 if (new == NULL)
737 free (result);
738 return NULL;
740 result->name = memcpy (new, name, len);
743 /* Parse the list of services. */
744 result->service = nss_parse_service_list (line);
746 result->next = NULL;
747 return result;
751 static service_library *
752 internal_function
753 nss_new_service (name_database *database, const char *name)
755 service_library **currentp = &database->library;
757 while (*currentp != NULL)
759 if (strcmp ((*currentp)->name, name) == 0)
760 return *currentp;
761 currentp = &(*currentp)->next;
764 /* We have to add the new service. */
765 *currentp = (service_library *) malloc (sizeof (service_library));
766 if (*currentp == NULL)
767 return NULL;
769 (*currentp)->name = name;
770 (*currentp)->lib_handle = NULL;
771 (*currentp)->next = NULL;
773 return *currentp;