Update.
[glibc.git] / nss / nsswitch.c
blobb9e283eefb41d2e14c4736c615f3d5d086b41d8b
1 /* Copyright (C) 1996, 1997, 1998, 1999 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>
30 #if !defined DO_STATIC_NSS || defined SHARED
31 # include <gnu/lib-names.h>
32 #endif
34 #include "nsswitch.h"
36 /* Prototypes for the local functions. */
37 static name_database *nss_parse_file (const char *fname) internal_function;
38 static name_database_entry *nss_getline (char *line) internal_function;
39 static service_user *nss_parse_service_list (const char *line)
40 internal_function;
41 static service_library *nss_new_service (name_database *database,
42 const char *name) internal_function;
45 /* Declare external database variables. */
46 #define DEFINE_DATABASE(name) \
47 extern service_user *__nss_##name##_database; \
48 weak_extern (__nss_##name##_database)
49 #include "databases.def"
50 #undef DEFINE_DATABASE
52 /* Structure to map database name to variable. */
53 static struct
55 const char *name;
56 service_user **dbp;
57 } databases[] =
59 #define DEFINE_DATABASE(name) \
60 { #name, &__nss_##name##_database },
61 #include "databases.def"
62 #undef DEFINE_DATABASE
66 __libc_lock_define_initialized (static, lock)
68 #if !defined DO_STATIC_NSS || defined SHARED
69 /* String with revision number of the shared object files. */
70 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
71 #endif
73 /* The root of the whole data base. */
74 static name_database *service_table;
77 /* -1 == database not found
78 0 == database entry pointer stored */
79 int
80 __nss_database_lookup (const char *database, const char *alternate_name,
81 const char *defconfig, service_user **ni)
83 /* Prevent multiple threads to change the service table. */
84 __libc_lock_lock (lock);
86 /* Reconsider database variable in case some other thread called
87 `__nss_configure_lookup' while we waited for the lock. */
88 if (*ni != NULL)
90 __libc_lock_unlock (lock);
91 return 0;
94 /* Are we initialized yet? */
95 if (service_table == NULL)
96 /* Read config file. */
97 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
99 /* Test whether configuration data is available. */
100 if (service_table != NULL)
102 /* Return first `service_user' entry for DATABASE. */
103 name_database_entry *entry;
105 /* XXX Could use some faster mechanism here. But each database is
106 only requested once and so this might not be critical. */
107 for (entry = service_table->entry; entry != NULL; entry = entry->next)
108 if (strcmp (database, entry->name) == 0)
109 *ni = entry->service;
111 if (*ni == NULL && alternate_name != NULL)
112 /* We haven't found an entry so far. Try to find it with the
113 alternative name. */
114 for (entry = service_table->entry; entry != NULL; entry = entry->next)
115 if (strcmp (alternate_name, entry->name) == 0)
116 *ni = entry->service;
119 /* No configuration data is available, either because nsswitch.conf
120 doesn't exist or because it doesn't has a line for this database.
122 DEFCONFIG specifies the default service list for this database,
123 or null to use the most common default. */
124 if (*ni == NULL)
125 *ni = nss_parse_service_list (defconfig
126 ?: "nis [NOTFOUND=return] files");
128 __libc_lock_unlock (lock);
130 return 0;
134 /* -1 == not found
135 0 == function found
136 1 == finished */
138 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
140 *fctp = __nss_lookup_function (*ni, fct_name);
142 while (*fctp == NULL
143 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
144 && (*ni)->next != NULL)
146 *ni = (*ni)->next;
148 *fctp = __nss_lookup_function (*ni, fct_name);
151 return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
155 /* -1 == not found
156 0 == adjusted for next function
157 1 == finished */
159 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
160 int all_values)
162 if (all_values)
164 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
165 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
166 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
167 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
168 return 1;
170 else
172 /* This is really only for debugging. */
173 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
174 __libc_fatal ("illegal status in " __FUNCTION__);
176 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
177 return 1;
180 if ((*ni)->next == NULL)
181 return -1;
185 *ni = (*ni)->next;
187 *fctp = __nss_lookup_function (*ni, fct_name);
189 while (*fctp == NULL
190 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
191 && (*ni)->next != NULL);
193 return *fctp != NULL ? 0 : -1;
198 __nss_configure_lookup (const char *dbname, const char *service_line)
200 service_user *new_db;
201 size_t cnt;
203 for (cnt = 0; cnt < sizeof databases; ++cnt)
205 int cmp = strcmp (dbname, databases[cnt].name);
206 if (cmp == 0)
207 break;
208 if (cmp < 0)
210 __set_errno (EINVAL);
211 return -1;
215 if (cnt == sizeof databases)
217 __set_errno (EINVAL);
218 return -1;
221 /* Test whether it is really used. */
222 if (databases[cnt].dbp == NULL)
223 /* Nothing to do, but we could do. */
224 return 0;
226 /* Try to generate new data. */
227 new_db = nss_parse_service_list (service_line);
228 if (new_db == NULL)
230 /* Illegal service specification. */
231 __set_errno (EINVAL);
232 return -1;
235 /* Prevent multiple threads to change the service table. */
236 __libc_lock_lock (lock);
238 /* Install new rules. */
239 *databases[cnt].dbp = new_db;
241 __libc_lock_unlock (lock);
243 return 0;
247 /* Comparison function for searching NI->known tree. */
248 static int
249 known_compare (const void *p1, const void *p2)
251 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
252 *(const char *const *) p2);
256 void *
257 __nss_lookup_function (service_user *ni, const char *fct_name)
259 void **found, *result;
261 /* We now modify global data. Protect it. */
262 __libc_lock_lock (lock);
264 /* Search the tree of functions previously requested. Data in the
265 tree are `known_function' structures, whose first member is a
266 `const char *', the lookup key. The search returns a pointer to
267 the tree node structure; the first member of the is a pointer to
268 our structure (i.e. what will be a `known_function'); since the
269 first member of that is the lookup key string, &FCT_NAME is close
270 enough to a pointer to our structure to use as a lookup key that
271 will be passed to `known_compare' (above). */
273 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
274 if (*found != &fct_name)
275 /* The search found an existing structure in the tree. */
276 result = ((known_function *) *found)->fct_ptr;
277 else
279 /* This name was not known before. Now we have a node in the tree
280 (in the proper sorted position for FCT_NAME) that points to
281 &FCT_NAME instead of any real `known_function' structure.
282 Allocate a new structure and fill it in. */
284 known_function *known = malloc (sizeof *known);
285 if (! known)
287 remove_from_tree:
288 /* Oops. We can't instantiate this node properly.
289 Remove it from the tree. */
290 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
291 result = NULL;
293 else
295 /* Point the tree node at this new structure. */
296 *found = known;
297 known->fct_name = fct_name;
299 if (ni->library == NULL)
301 /* This service has not yet been used. Fetch the service
302 library for it, creating a new one if need be. If there
303 is no service table from the file, this static variable
304 holds the head of the service_library list made from the
305 default configuration. */
306 static name_database default_table;
307 ni->library = nss_new_service (service_table ?: &default_table,
308 ni->name);
309 if (ni->library == NULL)
311 /* This only happens when out of memory. */
312 free (known);
313 goto remove_from_tree;
317 #if !defined DO_STATIC_NSS || defined SHARED
318 if (ni->library->lib_handle == NULL)
320 /* Load the shared library. */
321 size_t shlen = (7 + strlen (ni->library->name) + 3
322 + strlen (__nss_shlib_revision) + 1);
323 int saved_errno = errno;
324 char shlib_name[shlen];
326 /* Construct shared object name. */
327 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name,
328 "libnss_"),
329 ni->library->name),
330 ".so"),
331 __nss_shlib_revision);
333 ni->library->lib_handle = __libc_dlopen (shlib_name);
334 if (ni->library->lib_handle == NULL)
336 /* Failed to load the library. */
337 ni->library->lib_handle = (void *) -1l;
338 __set_errno (saved_errno);
342 if (ni->library->lib_handle == (void *) -1l)
343 /* Library not found => function not found. */
344 result = NULL;
345 else
347 /* Get the desired function. */
348 size_t namlen = (5 + strlen (ni->library->name) + 1
349 + strlen (fct_name) + 1);
350 char name[namlen];
352 /* Construct the function name. */
353 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
354 ni->library->name),
355 "_"),
356 fct_name);
358 /* Look up the symbol. */
359 result = __libc_dlsym (ni->library->lib_handle, name);
361 #else
362 /* We can't get function address dynamically in static linking. */
364 # define DEFINE_ENT(h,nm) \
365 extern void _nss_##h##_get##nm##ent_r (void); \
366 extern void _nss_##h##_end##nm##ent (void); \
367 extern void _nss_##h##_set##nm##ent (void);
368 # define DEFINE_GET(h,nm) \
369 extern void _nss_##h##_get##nm##_r (void);
370 # define DEFINE_GETBY(h,nm,ky) \
371 extern void _nss_##h##_get##nm##by##ky##_r (void);
372 # include "function.def"
373 # undef DEFINE_ENT
374 # undef DEFINE_GET
375 # undef DEFINE_GETBY
376 # define DEFINE_ENT(h,nm) \
377 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
378 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
379 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
380 # define DEFINE_GET(h,nm) \
381 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
382 # define DEFINE_GETBY(h,nm,ky) \
383 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
384 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
386 # include "function.def"
387 { NULL, NULL }
389 size_t namlen = (5 + strlen (ni->library->name) + 1
390 + strlen (fct_name) + 1);
391 char name[namlen];
393 /* Construct the function name. */
394 __stpcpy (__stpcpy (__stpcpy (name, ni->library->name),
395 "_"),
396 fct_name);
398 result = NULL;
399 for (tp = &tbl[0]; tp->fname; tp++)
400 if (strcmp (tp->fname, name) == 0)
402 result = tp->fp;
403 break;
406 #endif
408 /* Remember function pointer for later calls. Even if null, we
409 record it so a second try needn't search the library again. */
410 known->fct_ptr = result;
414 /* Remove the lock. */
415 __libc_lock_unlock (lock);
417 return result;
421 static name_database *
422 internal_function
423 nss_parse_file (const char *fname)
425 FILE *fp;
426 name_database *result;
427 name_database_entry *last;
428 char *line;
429 size_t len;
431 /* Open the configuration file. */
432 fp = fopen (fname, "r");
433 if (fp == NULL)
434 return NULL;
436 result = (name_database *) malloc (sizeof (name_database));
437 if (result == NULL)
438 return NULL;
440 result->entry = NULL;
441 result->library = NULL;
442 last = NULL;
443 line = NULL;
444 len = 0;
447 name_database_entry *this;
448 ssize_t n;
450 n = __getline (&line, &len, fp);
451 if (n < 0)
452 break;
453 if (line[n - 1] == '\n')
454 line[n - 1] = '\0';
456 /* Because the file format does not know any form of quoting we
457 can search forward for the next '#' character and if found
458 make it terminating the line. */
459 *__strchrnul (line, '#') = '\0';
461 /* If the line is blank it is ignored. */
462 if (line[0] == '\0')
463 continue;
465 /* Each line completely specifies the actions for a database. */
466 this = nss_getline (line);
467 if (this != NULL)
469 if (last != NULL)
470 last->next = this;
471 else
472 result->entry = this;
474 last = this;
477 while (!feof_unlocked (fp));
479 /* Free the buffer. */
480 free (line);
481 /* Close configuration file. */
482 fclose (fp);
484 return result;
488 /* Read the source names:
489 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
491 static service_user *
492 internal_function
493 nss_parse_service_list (const char *line)
495 service_user *result = NULL, **nextp = &result;
497 while (1)
499 service_user *new_service;
500 const char *name;
502 while (isspace (line[0]))
503 ++line;
504 if (line[0] == '\0')
505 /* No source specified. */
506 return result;
508 /* Read <source> identifier. */
509 name = line;
510 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
511 ++line;
512 if (name == line)
513 return result;
516 new_service = (service_user *) malloc (sizeof (service_user)
517 + (line - name + 1));
518 if (new_service == NULL)
519 return result;
521 *((char *) __mempcpy (new_service->name, name, line - name)) = '\0';
523 /* Set default actions. */
524 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
525 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
526 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
527 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
528 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
529 new_service->library = NULL;
530 new_service->known = NULL;
531 new_service->next = NULL;
533 while (isspace (line[0]))
534 ++line;
536 if (line[0] == '[')
538 /* Read criterions. */
540 ++line;
541 while (line[0] != '\0' && isspace (line[0]));
545 int not;
546 enum nss_status status;
547 lookup_actions action;
549 /* Grok ! before name to mean all statii but that one. */
550 not = line[0] == '!';
551 if (not)
552 ++line;
554 /* Read status name. */
555 name = line;
556 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
557 && line[0] != ']')
558 ++line;
560 /* Compare with known statii. */
561 if (line - name == 7)
563 if (__strncasecmp (name, "SUCCESS", 7) == 0)
564 status = NSS_STATUS_SUCCESS;
565 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
566 status = NSS_STATUS_UNAVAIL;
567 else
568 return result;
570 else if (line - name == 8)
572 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
573 status = NSS_STATUS_NOTFOUND;
574 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
575 status = NSS_STATUS_TRYAGAIN;
576 else
577 return result;
579 else
580 return result;
582 while (isspace (line[0]))
583 ++line;
584 if (line[0] != '=')
585 return result;
587 ++line;
588 while (isspace (line[0]));
590 name = line;
591 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
592 && line[0] != ']')
593 ++line;
595 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
596 action = NSS_ACTION_RETURN;
597 else if (line - name == 8
598 && __strncasecmp (name, "CONTINUE", 8) == 0)
599 action = NSS_ACTION_CONTINUE;
600 else
601 return result;
603 if (not)
605 /* Save the current action setting for this status,
606 set them all to the given action, and reset this one. */
607 const lookup_actions save = new_service->actions[2 + status];
608 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
609 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
610 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
611 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
612 new_service->actions[2 + status] = save;
614 else
615 new_service->actions[2 + status] = action;
617 /* Skip white spaces. */
618 while (isspace (line[0]))
619 ++line;
621 while (line[0] != ']');
623 /* Skip the ']'. */
624 ++line;
627 *nextp = new_service;
628 nextp = &new_service->next;
632 static name_database_entry *
633 internal_function
634 nss_getline (char *line)
636 const char *name;
637 name_database_entry *result;
638 size_t len;
640 /* Ignore leading white spaces. ATTENTION: this is different from
641 what is implemented in Solaris. The Solaris man page says a line
642 beginning with a white space character is ignored. We regard
643 this as just another misfeature in Solaris. */
644 while (isspace (line[0]))
645 ++line;
647 /* Recognize `<database> ":"'. */
648 name = line;
649 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
650 ++line;
651 if (line[0] == '\0' || name == line)
652 /* Syntax error. */
653 return NULL;
654 *line++ = '\0';
656 len = strlen (name) + 1;
658 result = (name_database_entry *) malloc (sizeof (name_database_entry) + len);
659 if (result == NULL)
660 return NULL;
662 /* Save the database name. */
663 memcpy (result->name, name, len);
665 /* Parse the list of services. */
666 result->service = nss_parse_service_list (line);
668 result->next = NULL;
669 return result;
673 static service_library *
674 internal_function
675 nss_new_service (name_database *database, const char *name)
677 service_library **currentp = &database->library;
679 while (*currentp != NULL)
681 if (strcmp ((*currentp)->name, name) == 0)
682 return *currentp;
683 currentp = &(*currentp)->next;
686 /* We have to add the new service. */
687 *currentp = (service_library *) malloc (sizeof (service_library));
688 if (*currentp == NULL)
689 return NULL;
691 (*currentp)->name = name;
692 (*currentp)->lib_handle = NULL;
693 (*currentp)->next = NULL;
695 return *currentp;
699 /* Free all resources if necessary. */
700 static void __attribute__ ((unused))
701 free_mem (void)
703 name_database *top = service_table;
704 name_database_entry *entry;
705 service_library *library;
707 if (top == NULL)
708 /* Maybe we have not read the nsswitch.conf file. */
709 return;
711 /* Don't disturb ongoing other threads (if there are any). */
712 service_table = NULL;
714 entry = top->entry;
715 while (entry != NULL)
717 name_database_entry *olde = entry;
718 service_user *service = entry->service;
720 while (service != NULL)
722 service_user *olds = service;
724 if (service->known != NULL)
725 __tdestroy (service->known, free);
727 service = service->next;
728 free (olds);
731 entry = entry->next;
732 free (olde);
735 library = top->library;
736 while (library != NULL)
738 service_library *oldl = library;
740 __libc_dlclose (library->lib_handle);
742 library = library->next;
743 free (oldl);
746 free (top);
749 text_set_element (__libc_subfreeres, free_mem);