Fix jn precision
[glibc.git] / nss / nsswitch.c
blob6c15c3a83f2354d865f5e5127059cb76a8dbd452
1 /* Copyright (C) 1996-1999,2001-2007,2009,2010,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <ctype.h>
22 #include <dlfcn.h>
23 #include <errno.h>
24 #include <netdb.h>
25 #include <bits/libc-lock.h>
26 #include <search.h>
27 #include <stdio.h>
28 #include <stdio_ext.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <aliases.h>
33 #include <grp.h>
34 #include <netinet/ether.h>
35 #include <pwd.h>
36 #include <shadow.h>
38 #if !defined DO_STATIC_NSS || defined SHARED
39 # include <gnu/lib-names.h>
40 #endif
42 #include "nsswitch.h"
43 #include "../nscd/nscd_proto.h"
44 #include <sysdep.h>
46 /* Prototypes for the local functions. */
47 static name_database *nss_parse_file (const char *fname) internal_function;
48 static name_database_entry *nss_getline (char *line) internal_function;
49 static service_user *nss_parse_service_list (const char *line)
50 internal_function;
51 static service_library *nss_new_service (name_database *database,
52 const char *name) internal_function;
55 /* Declare external database variables. */
56 #define DEFINE_DATABASE(name) \
57 extern service_user *__nss_##name##_database attribute_hidden; \
58 weak_extern (__nss_##name##_database)
59 #include "databases.def"
60 #undef DEFINE_DATABASE
62 /* Structure to map database name to variable. */
63 static const struct
65 const char name[10];
66 service_user **dbp;
67 } databases[] =
69 #define DEFINE_DATABASE(name) \
70 { #name, &__nss_##name##_database },
71 #include "databases.def"
72 #undef DEFINE_DATABASE
74 #define ndatabases (sizeof (databases) / sizeof (databases[0]))
76 /* Flags whether custom rules for database is set. */
77 bool __nss_database_custom[NSS_DBSIDX_max];
80 __libc_lock_define_initialized (static, lock)
82 #if !defined DO_STATIC_NSS || defined SHARED
83 /* String with revision number of the shared object files. */
84 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
85 #endif
87 /* The root of the whole data base. */
88 static name_database *service_table;
91 /* Nonzero if this is the nscd process. */
92 static bool is_nscd;
93 /* The callback passed to the init functions when nscd is used. */
94 static void (*nscd_init_cb) (size_t, struct traced_file *);
97 /* -1 == database not found
98 0 == database entry pointer stored */
99 int
100 __nss_database_lookup (const char *database, const char *alternate_name,
101 const char *defconfig, service_user **ni)
103 /* Prevent multiple threads to change the service table. */
104 __libc_lock_lock (lock);
106 /* Reconsider database variable in case some other thread called
107 `__nss_configure_lookup' while we waited for the lock. */
108 if (*ni != NULL)
110 __libc_lock_unlock (lock);
111 return 0;
114 /* Are we initialized yet? */
115 if (service_table == NULL)
116 /* Read config file. */
117 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
119 /* Test whether configuration data is available. */
120 if (service_table != NULL)
122 /* Return first `service_user' entry for DATABASE. */
123 name_database_entry *entry;
125 /* XXX Could use some faster mechanism here. But each database is
126 only requested once and so this might not be critical. */
127 for (entry = service_table->entry; entry != NULL; entry = entry->next)
128 if (strcmp (database, entry->name) == 0)
129 *ni = entry->service;
131 if (*ni == NULL && alternate_name != NULL)
132 /* We haven't found an entry so far. Try to find it with the
133 alternative name. */
134 for (entry = service_table->entry; entry != NULL; entry = entry->next)
135 if (strcmp (alternate_name, entry->name) == 0)
136 *ni = entry->service;
139 /* No configuration data is available, either because nsswitch.conf
140 doesn't exist or because it doesn't have a line for this database.
142 DEFCONFIG specifies the default service list for this database,
143 or null to use the most common default. */
144 if (*ni == NULL)
145 *ni = nss_parse_service_list (defconfig
146 ?: "nis [NOTFOUND=return] files");
148 __libc_lock_unlock (lock);
150 return 0;
152 libc_hidden_def (__nss_database_lookup)
155 /* -1 == not found
156 0 == function found
157 1 == finished */
159 __nss_lookup (service_user **ni, const char *fct_name, const char *fct2_name,
160 void **fctp)
162 *fctp = __nss_lookup_function (*ni, fct_name);
163 if (*fctp == NULL && fct2_name != NULL)
164 *fctp = __nss_lookup_function (*ni, fct2_name);
166 while (*fctp == NULL
167 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
168 && (*ni)->next != NULL)
170 *ni = (*ni)->next;
172 *fctp = __nss_lookup_function (*ni, fct_name);
173 if (*fctp == NULL && fct2_name != NULL)
174 *fctp = __nss_lookup_function (*ni, fct2_name);
177 return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
181 /* -1 == not found
182 0 == adjusted for next function
183 1 == finished */
185 __nss_next2 (service_user **ni, const char *fct_name, const char *fct2_name,
186 void **fctp, int status, int all_values)
188 if (all_values)
190 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
191 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
192 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
193 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
194 return 1;
196 else
198 /* This is really only for debugging. */
199 if (__builtin_expect (NSS_STATUS_TRYAGAIN > status
200 || status > NSS_STATUS_RETURN, 0))
201 __libc_fatal ("illegal status in __nss_next");
203 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
204 return 1;
207 if ((*ni)->next == NULL)
208 return -1;
212 *ni = (*ni)->next;
214 *fctp = __nss_lookup_function (*ni, fct_name);
215 if (*fctp == NULL && fct2_name != NULL)
216 *fctp = __nss_lookup_function (*ni, fct2_name);
218 while (*fctp == NULL
219 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
220 && (*ni)->next != NULL);
222 return *fctp != NULL ? 0 : -1;
224 libc_hidden_def (__nss_next2)
228 attribute_compat_text_section
229 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
230 int all_values)
232 return __nss_next2 (ni, fct_name, NULL, fctp, status, all_values);
237 __nss_configure_lookup (const char *dbname, const char *service_line)
239 service_user *new_db;
240 size_t cnt;
242 for (cnt = 0; cnt < ndatabases; ++cnt)
244 int cmp = strcmp (dbname, databases[cnt].name);
245 if (cmp == 0)
246 break;
247 if (cmp < 0)
249 __set_errno (EINVAL);
250 return -1;
254 if (cnt == ndatabases)
256 __set_errno (EINVAL);
257 return -1;
260 /* Test whether it is really used. */
261 if (databases[cnt].dbp == NULL)
262 /* Nothing to do, but we could do. */
263 return 0;
265 /* Try to generate new data. */
266 new_db = nss_parse_service_list (service_line);
267 if (new_db == NULL)
269 /* Illegal service specification. */
270 __set_errno (EINVAL);
271 return -1;
274 /* Prevent multiple threads to change the service table. */
275 __libc_lock_lock (lock);
277 /* Install new rules. */
278 *databases[cnt].dbp = new_db;
279 __nss_database_custom[cnt] = true;
281 __libc_lock_unlock (lock);
283 return 0;
287 /* Comparison function for searching NI->known tree. */
288 static int
289 known_compare (const void *p1, const void *p2)
291 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
292 *(const char *const *) p2);
296 #if !defined DO_STATIC_NSS || defined SHARED
297 /* Load library. */
298 static int
299 nss_load_library (service_user *ni)
301 if (ni->library == NULL)
303 /* This service has not yet been used. Fetch the service
304 library for it, creating a new one if need be. If there
305 is no service table from the file, this static variable
306 holds the head of the service_library list made from the
307 default configuration. */
308 static name_database default_table;
309 ni->library = nss_new_service (service_table ?: &default_table,
310 ni->name);
311 if (ni->library == NULL)
312 return -1;
315 if (ni->library->lib_handle == NULL)
317 /* Load the shared library. */
318 size_t shlen = (7 + strlen (ni->library->name) + 3
319 + strlen (__nss_shlib_revision) + 1);
320 int saved_errno = errno;
321 char shlib_name[shlen];
323 /* Construct shared object name. */
324 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name,
325 "libnss_"),
326 ni->library->name),
327 ".so"),
328 __nss_shlib_revision);
330 ni->library->lib_handle = __libc_dlopen (shlib_name);
331 if (ni->library->lib_handle == NULL)
333 /* Failed to load the library. */
334 ni->library->lib_handle = (void *) -1l;
335 __set_errno (saved_errno);
337 else if (is_nscd)
339 /* Call the init function when nscd is used. */
340 size_t initlen = (5 + strlen (ni->library->name)
341 + strlen ("_init") + 1);
342 char init_name[initlen];
344 /* Construct the init function name. */
345 __stpcpy (__stpcpy (__stpcpy (init_name,
346 "_nss_"),
347 ni->library->name),
348 "_init");
350 /* Find the optional init function. */
351 void (*ifct) (void (*) (size_t, struct traced_file *))
352 = __libc_dlsym (ni->library->lib_handle, init_name);
353 if (ifct != NULL)
355 void (*cb) (size_t, struct traced_file *) = nscd_init_cb;
356 # ifdef PTR_DEMANGLE
357 PTR_DEMANGLE (cb);
358 # endif
359 ifct (cb);
364 return 0;
366 #endif
369 void *
370 __nss_lookup_function (service_user *ni, const char *fct_name)
372 void **found, *result;
374 /* We now modify global data. Protect it. */
375 __libc_lock_lock (lock);
377 /* Search the tree of functions previously requested. Data in the
378 tree are `known_function' structures, whose first member is a
379 `const char *', the lookup key. The search returns a pointer to
380 the tree node structure; the first member of the is a pointer to
381 our structure (i.e. what will be a `known_function'); since the
382 first member of that is the lookup key string, &FCT_NAME is close
383 enough to a pointer to our structure to use as a lookup key that
384 will be passed to `known_compare' (above). */
386 found = __tsearch (&fct_name, &ni->known, &known_compare);
387 if (*found != &fct_name)
389 /* The search found an existing structure in the tree. */
390 result = ((known_function *) *found)->fct_ptr;
391 PTR_DEMANGLE (result);
393 else
395 /* This name was not known before. Now we have a node in the tree
396 (in the proper sorted position for FCT_NAME) that points to
397 &FCT_NAME instead of any real `known_function' structure.
398 Allocate a new structure and fill it in. */
400 known_function *known = malloc (sizeof *known);
401 if (! known)
403 remove_from_tree:
404 /* Oops. We can't instantiate this node properly.
405 Remove it from the tree. */
406 __tdelete (&fct_name, &ni->known, &known_compare);
407 result = NULL;
409 else
411 /* Point the tree node at this new structure. */
412 *found = known;
413 known->fct_name = fct_name;
415 #if !defined DO_STATIC_NSS || defined SHARED
416 /* Load the appropriate library. */
417 if (nss_load_library (ni) != 0)
419 /* This only happens when out of memory. */
420 free (known);
421 goto remove_from_tree;
424 if (ni->library->lib_handle == (void *) -1l)
425 /* Library not found => function not found. */
426 result = NULL;
427 else
429 /* Get the desired function. */
430 size_t namlen = (5 + strlen (ni->library->name) + 1
431 + strlen (fct_name) + 1);
432 char name[namlen];
434 /* Construct the function name. */
435 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
436 ni->library->name),
437 "_"),
438 fct_name);
440 /* Look up the symbol. */
441 result = __libc_dlsym (ni->library->lib_handle, name);
443 #else
444 /* We can't get function address dynamically in static linking. */
446 # define DEFINE_ENT(h,nm) \
447 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
448 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
449 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
450 # define DEFINE_GET(h,nm) \
451 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
452 # define DEFINE_GETBY(h,nm,ky) \
453 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
454 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
456 # include "function.def"
457 { NULL, NULL }
459 size_t namlen = (5 + strlen (ni->library->name) + 1
460 + strlen (fct_name) + 1);
461 char name[namlen];
463 /* Construct the function name. */
464 __stpcpy (__stpcpy (__stpcpy (name, ni->library->name),
465 "_"),
466 fct_name);
468 result = NULL;
469 for (tp = &tbl[0]; tp->fname; tp++)
470 if (strcmp (tp->fname, name) == 0)
472 result = tp->fp;
473 break;
476 #endif
478 /* Remember function pointer for later calls. Even if null, we
479 record it so a second try needn't search the library again. */
480 known->fct_ptr = result;
481 PTR_MANGLE (known->fct_ptr);
485 /* Remove the lock. */
486 __libc_lock_unlock (lock);
488 return result;
490 libc_hidden_def (__nss_lookup_function)
493 static name_database *
494 internal_function
495 nss_parse_file (const char *fname)
497 FILE *fp;
498 name_database *result;
499 name_database_entry *last;
500 char *line;
501 size_t len;
503 /* Open the configuration file. */
504 fp = fopen (fname, "rc");
505 if (fp == NULL)
506 return NULL;
508 /* No threads use this stream. */
509 __fsetlocking (fp, FSETLOCKING_BYCALLER);
511 result = (name_database *) malloc (sizeof (name_database));
512 if (result == NULL)
514 fclose (fp);
515 return NULL;
518 result->entry = NULL;
519 result->library = NULL;
520 last = NULL;
521 line = NULL;
522 len = 0;
525 name_database_entry *this;
526 ssize_t n;
528 n = __getline (&line, &len, fp);
529 if (n < 0)
530 break;
531 if (line[n - 1] == '\n')
532 line[n - 1] = '\0';
534 /* Because the file format does not know any form of quoting we
535 can search forward for the next '#' character and if found
536 make it terminating the line. */
537 *__strchrnul (line, '#') = '\0';
539 /* If the line is blank it is ignored. */
540 if (line[0] == '\0')
541 continue;
543 /* Each line completely specifies the actions for a database. */
544 this = nss_getline (line);
545 if (this != NULL)
547 if (last != NULL)
548 last->next = this;
549 else
550 result->entry = this;
552 last = this;
555 while (!feof_unlocked (fp));
557 /* Free the buffer. */
558 free (line);
559 /* Close configuration file. */
560 fclose (fp);
562 return result;
566 /* Read the source names:
567 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
569 static service_user *
570 internal_function
571 nss_parse_service_list (const char *line)
573 service_user *result = NULL, **nextp = &result;
575 while (1)
577 service_user *new_service;
578 const char *name;
580 while (isspace (line[0]))
581 ++line;
582 if (line[0] == '\0')
583 /* No source specified. */
584 return result;
586 /* Read <source> identifier. */
587 name = line;
588 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
589 ++line;
590 if (name == line)
591 return result;
594 new_service = (service_user *) malloc (sizeof (service_user)
595 + (line - name + 1));
596 if (new_service == NULL)
597 return result;
599 *((char *) __mempcpy (new_service->name, name, line - name)) = '\0';
601 /* Set default actions. */
602 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
603 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
604 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
605 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
606 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
607 new_service->library = NULL;
608 new_service->known = NULL;
609 new_service->next = NULL;
611 while (isspace (line[0]))
612 ++line;
614 if (line[0] == '[')
616 /* Read criterions. */
618 ++line;
619 while (line[0] != '\0' && isspace (line[0]));
623 int not;
624 enum nss_status status;
625 lookup_actions action;
627 /* Grok ! before name to mean all statii but that one. */
628 not = line[0] == '!';
629 if (not)
630 ++line;
632 /* Read status name. */
633 name = line;
634 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
635 && line[0] != ']')
636 ++line;
638 /* Compare with known statii. */
639 if (line - name == 7)
641 if (__strncasecmp (name, "SUCCESS", 7) == 0)
642 status = NSS_STATUS_SUCCESS;
643 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
644 status = NSS_STATUS_UNAVAIL;
645 else
646 return result;
648 else if (line - name == 8)
650 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
651 status = NSS_STATUS_NOTFOUND;
652 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
653 status = NSS_STATUS_TRYAGAIN;
654 else
655 return result;
657 else
658 return result;
660 while (isspace (line[0]))
661 ++line;
662 if (line[0] != '=')
663 return result;
665 ++line;
666 while (isspace (line[0]));
668 name = line;
669 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
670 && line[0] != ']')
671 ++line;
673 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
674 action = NSS_ACTION_RETURN;
675 else if (line - name == 8
676 && __strncasecmp (name, "CONTINUE", 8) == 0)
677 action = NSS_ACTION_CONTINUE;
678 else
679 return result;
681 if (not)
683 /* Save the current action setting for this status,
684 set them all to the given action, and reset this one. */
685 const lookup_actions save = new_service->actions[2 + status];
686 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
687 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
688 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
689 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
690 new_service->actions[2 + status] = save;
692 else
693 new_service->actions[2 + status] = action;
695 /* Skip white spaces. */
696 while (isspace (line[0]))
697 ++line;
699 while (line[0] != ']');
701 /* Skip the ']'. */
702 ++line;
705 *nextp = new_service;
706 nextp = &new_service->next;
710 static name_database_entry *
711 internal_function
712 nss_getline (char *line)
714 const char *name;
715 name_database_entry *result;
716 size_t len;
718 /* Ignore leading white spaces. ATTENTION: this is different from
719 what is implemented in Solaris. The Solaris man page says a line
720 beginning with a white space character is ignored. We regard
721 this as just another misfeature in Solaris. */
722 while (isspace (line[0]))
723 ++line;
725 /* Recognize `<database> ":"'. */
726 name = line;
727 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
728 ++line;
729 if (line[0] == '\0' || name == line)
730 /* Syntax error. */
731 return NULL;
732 *line++ = '\0';
734 len = strlen (name) + 1;
736 result = (name_database_entry *) malloc (sizeof (name_database_entry) + len);
737 if (result == NULL)
738 return NULL;
740 /* Save the database name. */
741 memcpy (result->name, 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;
777 #ifdef SHARED
778 /* Load all libraries for the service. */
779 static void
780 nss_load_all_libraries (const char *service, const char *def)
782 service_user *ni = NULL;
784 if (__nss_database_lookup (service, NULL, def, &ni) == 0)
785 while (ni != NULL)
787 nss_load_library (ni);
788 ni = ni->next;
793 /* Called by nscd and nscd alone. */
794 void
795 __nss_disable_nscd (void (*cb) (size_t, struct traced_file *))
797 # ifdef PTR_MANGLE
798 PTR_MANGLE (cb);
799 # endif
800 nscd_init_cb = cb;
801 is_nscd = true;
803 /* Find all the relevant modules so that the init functions are called. */
804 nss_load_all_libraries ("passwd", "compat [NOTFOUND=return] files");
805 nss_load_all_libraries ("group", "compat [NOTFOUND=return] files");
806 nss_load_all_libraries ("hosts", "dns [!UNAVAIL=return] files");
807 nss_load_all_libraries ("services", NULL);
809 /* Disable all uses of NSCD. */
810 __nss_not_use_nscd_passwd = -1;
811 __nss_not_use_nscd_group = -1;
812 __nss_not_use_nscd_hosts = -1;
813 __nss_not_use_nscd_services = -1;
815 #endif
818 /* Free all resources if necessary. */
819 libc_freeres_fn (free_mem)
821 name_database *top = service_table;
822 name_database_entry *entry;
823 service_library *library;
825 if (top == NULL)
826 /* Maybe we have not read the nsswitch.conf file. */
827 return;
829 /* Don't disturb ongoing other threads (if there are any). */
830 service_table = NULL;
832 entry = top->entry;
833 while (entry != NULL)
835 name_database_entry *olde = entry;
836 service_user *service = entry->service;
838 while (service != NULL)
840 service_user *olds = service;
842 if (service->known != NULL)
843 __tdestroy (service->known, free);
845 service = service->next;
846 free (olds);
849 entry = entry->next;
850 free (olde);
853 library = top->library;
854 while (library != NULL)
856 service_library *oldl = library;
858 if (library->lib_handle && library->lib_handle != (void *) -1l)
859 __libc_dlclose (library->lib_handle);
861 library = library->next;
862 free (oldl);
865 free (top);