Updated to fedora-glibc-20090427T1419
[glibc/history.git] / nss / nsswitch.c
blobda94ca983870454ab3d384102cd22eb789f82565
1 /* Copyright (C) 1996-1999, 2001-2007, 2009 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 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 <stdio_ext.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include <aliases.h>
32 #include <grp.h>
33 #include <netinet/ether.h>
34 #include <pwd.h>
35 #include <shadow.h>
37 #if !defined DO_STATIC_NSS || defined SHARED
38 # include <gnu/lib-names.h>
39 #endif
41 #include "nsswitch.h"
42 #include "../nscd/nscd_proto.h"
44 /* Prototypes for the local functions. */
45 static name_database *nss_parse_file (const char *fname) internal_function;
46 static name_database_entry *nss_getline (char *line) internal_function;
47 static service_user *nss_parse_service_list (const char *line)
48 internal_function;
49 static service_library *nss_new_service (name_database *database,
50 const char *name) internal_function;
53 /* Declare external database variables. */
54 #define DEFINE_DATABASE(name) \
55 extern service_user *__nss_##name##_database attribute_hidden; \
56 weak_extern (__nss_##name##_database)
57 #include "databases.def"
58 #undef DEFINE_DATABASE
60 /* Structure to map database name to variable. */
61 static const struct
63 const char name[10];
64 service_user **dbp;
65 } databases[] =
67 #define DEFINE_DATABASE(name) \
68 { #name, &__nss_##name##_database },
69 #include "databases.def"
70 #undef DEFINE_DATABASE
72 #define ndatabases (sizeof (databases) / sizeof (databases[0]))
75 __libc_lock_define_initialized (static, lock)
77 #if !defined DO_STATIC_NSS || defined SHARED
78 /* String with revision number of the shared object files. */
79 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
80 #endif
82 /* The root of the whole data base. */
83 static name_database *service_table;
86 /* -1 == database not found
87 0 == database entry pointer stored */
88 int
89 __nss_database_lookup (const char *database, const char *alternate_name,
90 const char *defconfig, service_user **ni)
92 /* Prevent multiple threads to change the service table. */
93 __libc_lock_lock (lock);
95 /* Reconsider database variable in case some other thread called
96 `__nss_configure_lookup' while we waited for the lock. */
97 if (*ni != NULL)
99 __libc_lock_unlock (lock);
100 return 0;
103 /* Are we initialized yet? */
104 if (service_table == NULL)
105 /* Read config file. */
106 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
108 /* Test whether configuration data is available. */
109 if (service_table != NULL)
111 /* Return first `service_user' entry for DATABASE. */
112 name_database_entry *entry;
114 /* XXX Could use some faster mechanism here. But each database is
115 only requested once and so this might not be critical. */
116 for (entry = service_table->entry; entry != NULL; entry = entry->next)
117 if (strcmp (database, entry->name) == 0)
118 *ni = entry->service;
120 if (*ni == NULL && alternate_name != NULL)
121 /* We haven't found an entry so far. Try to find it with the
122 alternative name. */
123 for (entry = service_table->entry; entry != NULL; entry = entry->next)
124 if (strcmp (alternate_name, entry->name) == 0)
125 *ni = entry->service;
128 /* No configuration data is available, either because nsswitch.conf
129 doesn't exist or because it doesn't has a line for this database.
131 DEFCONFIG specifies the default service list for this database,
132 or null to use the most common default. */
133 if (*ni == NULL)
134 *ni = nss_parse_service_list (defconfig
135 ?: "nis [NOTFOUND=return] files");
137 __libc_lock_unlock (lock);
139 return 0;
141 libc_hidden_def (__nss_database_lookup)
144 /* -1 == not found
145 0 == function found
146 1 == finished */
148 __nss_lookup (service_user **ni, const char *fct_name, const char *fct2_name,
149 void **fctp)
151 *fctp = __nss_lookup_function (*ni, fct_name);
152 if (*fctp == NULL && fct2_name != NULL)
153 *fctp = __nss_lookup_function (*ni, fct2_name);
155 while (*fctp == NULL
156 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
157 && (*ni)->next != NULL)
159 *ni = (*ni)->next;
161 *fctp = __nss_lookup_function (*ni, fct_name);
162 if (*fctp == NULL && fct2_name != NULL)
163 *fctp = __nss_lookup_function (*ni, fct2_name);
166 return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
170 /* -1 == not found
171 0 == adjusted for next function
172 1 == finished */
174 __nss_next2 (service_user **ni, const char *fct_name, const char *fct2_name,
175 void **fctp, int status, int all_values)
177 if (all_values)
179 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
180 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
181 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
182 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
183 return 1;
185 else
187 /* This is really only for debugging. */
188 if (__builtin_expect (NSS_STATUS_TRYAGAIN > status
189 || status > NSS_STATUS_RETURN, 0))
190 __libc_fatal ("illegal status in __nss_next");
192 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
193 return 1;
196 if ((*ni)->next == NULL)
197 return -1;
201 *ni = (*ni)->next;
203 *fctp = __nss_lookup_function (*ni, fct_name);
204 if (*fctp == NULL && fct2_name != NULL)
205 *fctp = __nss_lookup_function (*ni, fct2_name);
207 while (*fctp == NULL
208 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
209 && (*ni)->next != NULL);
211 return *fctp != NULL ? 0 : -1;
213 libc_hidden_def (__nss_next2)
217 attribute_compat_text_section
218 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
219 int all_values)
221 return __nss_next2 (ni, fct_name, NULL, fctp, status, all_values);
226 __nss_configure_lookup (const char *dbname, const char *service_line)
228 service_user *new_db;
229 size_t cnt;
231 for (cnt = 0; cnt < ndatabases; ++cnt)
233 int cmp = strcmp (dbname, databases[cnt].name);
234 if (cmp == 0)
235 break;
236 if (cmp < 0)
238 __set_errno (EINVAL);
239 return -1;
243 if (cnt == ndatabases)
245 __set_errno (EINVAL);
246 return -1;
249 /* Test whether it is really used. */
250 if (databases[cnt].dbp == NULL)
251 /* Nothing to do, but we could do. */
252 return 0;
254 /* Try to generate new data. */
255 new_db = nss_parse_service_list (service_line);
256 if (new_db == NULL)
258 /* Illegal service specification. */
259 __set_errno (EINVAL);
260 return -1;
263 /* Prevent multiple threads to change the service table. */
264 __libc_lock_lock (lock);
266 /* Install new rules. */
267 *databases[cnt].dbp = new_db;
269 __libc_lock_unlock (lock);
271 return 0;
275 /* Comparison function for searching NI->known tree. */
276 static int
277 known_compare (const void *p1, const void *p2)
279 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
280 *(const char *const *) p2);
284 void *
285 __nss_lookup_function (service_user *ni, const char *fct_name)
287 void **found, *result;
289 /* We now modify global data. Protect it. */
290 __libc_lock_lock (lock);
292 /* Search the tree of functions previously requested. Data in the
293 tree are `known_function' structures, whose first member is a
294 `const char *', the lookup key. The search returns a pointer to
295 the tree node structure; the first member of the is a pointer to
296 our structure (i.e. what will be a `known_function'); since the
297 first member of that is the lookup key string, &FCT_NAME is close
298 enough to a pointer to our structure to use as a lookup key that
299 will be passed to `known_compare' (above). */
301 found = __tsearch (&fct_name, &ni->known, &known_compare);
302 if (*found != &fct_name)
304 /* The search found an existing structure in the tree. */
305 result = ((known_function *) *found)->fct_ptr;
306 PTR_DEMANGLE (result);
308 else
310 /* This name was not known before. Now we have a node in the tree
311 (in the proper sorted position for FCT_NAME) that points to
312 &FCT_NAME instead of any real `known_function' structure.
313 Allocate a new structure and fill it in. */
315 known_function *known = malloc (sizeof *known);
316 if (! known)
318 remove_from_tree:
319 /* Oops. We can't instantiate this node properly.
320 Remove it from the tree. */
321 __tdelete (&fct_name, &ni->known, &known_compare);
322 result = NULL;
324 else
326 /* Point the tree node at this new structure. */
327 *found = known;
328 known->fct_name = fct_name;
330 if (ni->library == NULL)
332 /* This service has not yet been used. Fetch the service
333 library for it, creating a new one if need be. If there
334 is no service table from the file, this static variable
335 holds the head of the service_library list made from the
336 default configuration. */
337 static name_database default_table;
338 ni->library = nss_new_service (service_table ?: &default_table,
339 ni->name);
340 if (ni->library == NULL)
342 /* This only happens when out of memory. */
343 free (known);
344 goto remove_from_tree;
348 #if !defined DO_STATIC_NSS || defined SHARED
349 if (ni->library->lib_handle == NULL)
351 /* Load the shared library. */
352 size_t shlen = (7 + strlen (ni->library->name) + 3
353 + strlen (__nss_shlib_revision) + 1);
354 int saved_errno = errno;
355 char shlib_name[shlen];
357 /* Construct shared object name. */
358 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name,
359 "libnss_"),
360 ni->library->name),
361 ".so"),
362 __nss_shlib_revision);
364 ni->library->lib_handle = __libc_dlopen (shlib_name);
365 if (ni->library->lib_handle == NULL)
367 /* Failed to load the library. */
368 ni->library->lib_handle = (void *) -1l;
369 __set_errno (saved_errno);
373 if (ni->library->lib_handle == (void *) -1l)
374 /* Library not found => function not found. */
375 result = NULL;
376 else
378 /* Get the desired function. */
379 size_t namlen = (5 + strlen (ni->library->name) + 1
380 + strlen (fct_name) + 1);
381 char name[namlen];
383 /* Construct the function name. */
384 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
385 ni->library->name),
386 "_"),
387 fct_name);
389 /* Look up the symbol. */
390 result = __libc_dlsym (ni->library->lib_handle, name);
392 #else
393 /* We can't get function address dynamically in static linking. */
395 # define DEFINE_ENT(h,nm) \
396 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
397 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
398 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
399 # define DEFINE_GET(h,nm) \
400 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
401 # define DEFINE_GETBY(h,nm,ky) \
402 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
403 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
405 # include "function.def"
406 { NULL, NULL }
408 size_t namlen = (5 + strlen (ni->library->name) + 1
409 + strlen (fct_name) + 1);
410 char name[namlen];
412 /* Construct the function name. */
413 __stpcpy (__stpcpy (__stpcpy (name, ni->library->name),
414 "_"),
415 fct_name);
417 result = NULL;
418 for (tp = &tbl[0]; tp->fname; tp++)
419 if (strcmp (tp->fname, name) == 0)
421 result = tp->fp;
422 break;
425 #endif
427 /* Remember function pointer for later calls. Even if null, we
428 record it so a second try needn't search the library again. */
429 known->fct_ptr = result;
430 PTR_MANGLE (known->fct_ptr);
434 /* Remove the lock. */
435 __libc_lock_unlock (lock);
437 return result;
439 libc_hidden_def (__nss_lookup_function)
442 static name_database *
443 internal_function
444 nss_parse_file (const char *fname)
446 FILE *fp;
447 name_database *result;
448 name_database_entry *last;
449 char *line;
450 size_t len;
452 /* Open the configuration file. */
453 fp = fopen (fname, "rc");
454 if (fp == NULL)
455 return NULL;
457 /* No threads use this stream. */
458 __fsetlocking (fp, FSETLOCKING_BYCALLER);
460 result = (name_database *) malloc (sizeof (name_database));
461 if (result == NULL)
462 return NULL;
464 result->entry = NULL;
465 result->library = NULL;
466 last = NULL;
467 line = NULL;
468 len = 0;
471 name_database_entry *this;
472 ssize_t n;
474 n = __getline (&line, &len, fp);
475 if (n < 0)
476 break;
477 if (line[n - 1] == '\n')
478 line[n - 1] = '\0';
480 /* Because the file format does not know any form of quoting we
481 can search forward for the next '#' character and if found
482 make it terminating the line. */
483 *__strchrnul (line, '#') = '\0';
485 /* If the line is blank it is ignored. */
486 if (line[0] == '\0')
487 continue;
489 /* Each line completely specifies the actions for a database. */
490 this = nss_getline (line);
491 if (this != NULL)
493 if (last != NULL)
494 last->next = this;
495 else
496 result->entry = this;
498 last = this;
501 while (!feof_unlocked (fp));
503 /* Free the buffer. */
504 free (line);
505 /* Close configuration file. */
506 fclose (fp);
508 return result;
512 /* Read the source names:
513 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
515 static service_user *
516 internal_function
517 nss_parse_service_list (const char *line)
519 service_user *result = NULL, **nextp = &result;
521 while (1)
523 service_user *new_service;
524 const char *name;
526 while (isspace (line[0]))
527 ++line;
528 if (line[0] == '\0')
529 /* No source specified. */
530 return result;
532 /* Read <source> identifier. */
533 name = line;
534 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
535 ++line;
536 if (name == line)
537 return result;
540 new_service = (service_user *) malloc (sizeof (service_user)
541 + (line - name + 1));
542 if (new_service == NULL)
543 return result;
545 *((char *) __mempcpy (new_service->name, name, line - name)) = '\0';
547 /* Set default actions. */
548 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
549 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
550 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
551 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
552 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
553 new_service->library = NULL;
554 new_service->known = NULL;
555 new_service->next = NULL;
557 while (isspace (line[0]))
558 ++line;
560 if (line[0] == '[')
562 /* Read criterions. */
564 ++line;
565 while (line[0] != '\0' && isspace (line[0]));
569 int not;
570 enum nss_status status;
571 lookup_actions action;
573 /* Grok ! before name to mean all statii but that one. */
574 not = line[0] == '!';
575 if (not)
576 ++line;
578 /* Read status name. */
579 name = line;
580 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
581 && line[0] != ']')
582 ++line;
584 /* Compare with known statii. */
585 if (line - name == 7)
587 if (__strncasecmp (name, "SUCCESS", 7) == 0)
588 status = NSS_STATUS_SUCCESS;
589 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
590 status = NSS_STATUS_UNAVAIL;
591 else
592 return result;
594 else if (line - name == 8)
596 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
597 status = NSS_STATUS_NOTFOUND;
598 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
599 status = NSS_STATUS_TRYAGAIN;
600 else
601 return result;
603 else
604 return result;
606 while (isspace (line[0]))
607 ++line;
608 if (line[0] != '=')
609 return result;
611 ++line;
612 while (isspace (line[0]));
614 name = line;
615 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
616 && line[0] != ']')
617 ++line;
619 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
620 action = NSS_ACTION_RETURN;
621 else if (line - name == 8
622 && __strncasecmp (name, "CONTINUE", 8) == 0)
623 action = NSS_ACTION_CONTINUE;
624 else
625 return result;
627 if (not)
629 /* Save the current action setting for this status,
630 set them all to the given action, and reset this one. */
631 const lookup_actions save = new_service->actions[2 + status];
632 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
633 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
634 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
635 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
636 new_service->actions[2 + status] = save;
638 else
639 new_service->actions[2 + status] = action;
641 /* Skip white spaces. */
642 while (isspace (line[0]))
643 ++line;
645 while (line[0] != ']');
647 /* Skip the ']'. */
648 ++line;
651 *nextp = new_service;
652 nextp = &new_service->next;
656 static name_database_entry *
657 internal_function
658 nss_getline (char *line)
660 const char *name;
661 name_database_entry *result;
662 size_t len;
664 /* Ignore leading white spaces. ATTENTION: this is different from
665 what is implemented in Solaris. The Solaris man page says a line
666 beginning with a white space character is ignored. We regard
667 this as just another misfeature in Solaris. */
668 while (isspace (line[0]))
669 ++line;
671 /* Recognize `<database> ":"'. */
672 name = line;
673 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
674 ++line;
675 if (line[0] == '\0' || name == line)
676 /* Syntax error. */
677 return NULL;
678 *line++ = '\0';
680 len = strlen (name) + 1;
682 result = (name_database_entry *) malloc (sizeof (name_database_entry) + len);
683 if (result == NULL)
684 return NULL;
686 /* Save the database name. */
687 memcpy (result->name, name, len);
689 /* Parse the list of services. */
690 result->service = nss_parse_service_list (line);
692 result->next = NULL;
693 return result;
697 static service_library *
698 internal_function
699 nss_new_service (name_database *database, const char *name)
701 service_library **currentp = &database->library;
703 while (*currentp != NULL)
705 if (strcmp ((*currentp)->name, name) == 0)
706 return *currentp;
707 currentp = &(*currentp)->next;
710 /* We have to add the new service. */
711 *currentp = (service_library *) malloc (sizeof (service_library));
712 if (*currentp == NULL)
713 return NULL;
715 (*currentp)->name = name;
716 (*currentp)->lib_handle = NULL;
717 (*currentp)->next = NULL;
719 return *currentp;
723 /* Called by nscd and nscd alone. */
724 void
725 __nss_disable_nscd (void)
727 /* Disable all uses of NSCD. */
728 __nss_not_use_nscd_passwd = -1;
729 __nss_not_use_nscd_group = -1;
730 __nss_not_use_nscd_hosts = -1;
731 __nss_not_use_nscd_services = -1;
735 /* Free all resources if necessary. */
736 libc_freeres_fn (free_mem)
738 name_database *top = service_table;
739 name_database_entry *entry;
740 service_library *library;
742 if (top == NULL)
743 /* Maybe we have not read the nsswitch.conf file. */
744 return;
746 /* Don't disturb ongoing other threads (if there are any). */
747 service_table = NULL;
749 entry = top->entry;
750 while (entry != NULL)
752 name_database_entry *olde = entry;
753 service_user *service = entry->service;
755 while (service != NULL)
757 service_user *olds = service;
759 if (service->known != NULL)
760 __tdestroy (service->known, free);
762 service = service->next;
763 free (olds);
766 entry = entry->next;
767 free (olde);
770 library = top->library;
771 while (library != NULL)
773 service_library *oldl = library;
775 if (library->lib_handle && library->lib_handle != (void *) -1l)
776 __libc_dlclose (library->lib_handle);
778 library = library->next;
779 free (oldl);
782 free (top);