1 /* Copyright (C) 1996-2020 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, see
17 <https://www.gnu.org/licenses/>. */
23 #include <libc-lock.h>
26 #include <stdio_ext.h>
32 #include <netinet/ether.h>
36 #if !defined DO_STATIC_NSS || defined SHARED
37 # include <gnu/lib-names.h>
41 #include "../nscd/nscd_proto.h"
45 #ifdef LINK_OBSOLETE_NSL
46 # define DEFAULT_CONFIG "compat [NOTFOUND=return] files"
47 # define DEFAULT_DEFCONFIG "nis [NOTFOUND=return] files"
49 # define DEFAULT_CONFIG "files"
50 # define DEFAULT_DEFCONFIG "files"
53 /* Prototypes for the local functions. */
54 static name_database *nss_parse_file (const char *fname);
55 static name_database_entry *nss_getline (char *line);
56 static service_user *nss_parse_service_list (const char *line);
57 #if !defined DO_STATIC_NSS || defined SHARED
58 static service_library *nss_new_service (name_database *database,
63 /* Declare external database variables. */
64 #define DEFINE_DATABASE(name) \
65 service_user *__nss_##name##_database attribute_hidden; \
66 weak_extern (__nss_##name##_database)
67 #include "databases.def"
68 #undef DEFINE_DATABASE
70 /* Structure to map database name to variable. */
77 #define DEFINE_DATABASE(name) \
78 { #name, &__nss_##name##_database },
79 #include "databases.def"
80 #undef DEFINE_DATABASE
82 #define ndatabases (sizeof (databases) / sizeof (databases[0]))
85 /* Flags whether custom rules for database is set. */
86 bool __nss_database_custom[NSS_DBSIDX_max];
90 __libc_lock_define_initialized (static, lock)
92 #if !defined DO_STATIC_NSS || defined SHARED
93 /* String with revision number of the shared object files. */
94 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
97 /* The root of the whole data base. */
98 static name_database *service_table;
100 /* List of default service lists that were generated by glibc because
101 /etc/nsswitch.conf did not provide a value.
102 The list is only maintained so we can free such service lists in
104 static name_database_entry *defconfig_entries;
107 #if defined USE_NSCD && (!defined DO_STATIC_NSS || defined SHARED)
108 /* Nonzero if this is the nscd process. */
110 /* The callback passed to the init functions when nscd is used. */
111 static void (*nscd_init_cb) (size_t, struct traced_file *);
115 /* -1 == database not found
116 0 == database entry pointer stored */
118 __nss_database_lookup2 (const char *database, const char *alternate_name,
119 const char *defconfig, service_user **ni)
121 /* Prevent multiple threads to change the service table. */
122 __libc_lock_lock (lock);
124 /* Reconsider database variable in case some other thread called
125 `__nss_configure_lookup' while we waited for the lock. */
128 __libc_lock_unlock (lock);
132 /* Are we initialized yet? */
133 if (service_table == NULL)
134 /* Read config file. */
135 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
137 /* Test whether configuration data is available. */
138 if (service_table != NULL)
140 /* Return first `service_user' entry for DATABASE. */
141 name_database_entry *entry;
143 /* XXX Could use some faster mechanism here. But each database is
144 only requested once and so this might not be critical. */
145 for (entry = service_table->entry; entry != NULL; entry = entry->next)
146 if (strcmp (database, entry->name) == 0)
147 *ni = entry->service;
149 if (*ni == NULL && alternate_name != NULL)
150 /* We haven't found an entry so far. Try to find it with the
152 for (entry = service_table->entry; entry != NULL; entry = entry->next)
153 if (strcmp (alternate_name, entry->name) == 0)
154 *ni = entry->service;
157 /* No configuration data is available, either because nsswitch.conf
158 doesn't exist or because it doesn't have a line for this database.
160 DEFCONFIG specifies the default service list for this database,
161 or null to use the most common default. */
164 *ni = nss_parse_service_list (defconfig ?: DEFAULT_DEFCONFIG);
167 /* Record the memory we've just allocated in defconfig_entries list,
168 so we can free it later. */
169 name_database_entry *entry;
171 /* Allocate ENTRY plus size of name (1 here). */
172 entry = (name_database_entry *) malloc (sizeof (*entry) + 1);
176 entry->next = defconfig_entries;
177 entry->service = *ni;
178 entry->name[0] = '\0';
179 defconfig_entries = entry;
184 __libc_lock_unlock (lock);
186 return *ni != NULL ? 0 : -1;
188 libc_hidden_def (__nss_database_lookup2)
195 __nss_lookup (service_user **ni, const char *fct_name, const char *fct2_name,
198 *fctp = __nss_lookup_function (*ni, fct_name);
199 if (*fctp == NULL && fct2_name != NULL)
200 *fctp = __nss_lookup_function (*ni, fct2_name);
203 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
204 && (*ni)->next != NULL)
208 *fctp = __nss_lookup_function (*ni, fct_name);
209 if (*fctp == NULL && fct2_name != NULL)
210 *fctp = __nss_lookup_function (*ni, fct2_name);
213 return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
215 libc_hidden_def (__nss_lookup)
219 0 == adjusted for next function
222 __nss_next2 (service_user **ni, const char *fct_name, const char *fct2_name,
223 void **fctp, int status, int all_values)
227 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
228 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
229 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
230 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
235 /* This is really only for debugging. */
236 if (__builtin_expect (NSS_STATUS_TRYAGAIN > status
237 || status > NSS_STATUS_RETURN, 0))
238 __libc_fatal ("Illegal status in __nss_next.\n");
240 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
244 if ((*ni)->next == NULL)
251 *fctp = __nss_lookup_function (*ni, fct_name);
252 if (*fctp == NULL && fct2_name != NULL)
253 *fctp = __nss_lookup_function (*ni, fct2_name);
256 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
257 && (*ni)->next != NULL);
259 return *fctp != NULL ? 0 : -1;
261 libc_hidden_def (__nss_next2)
264 __nss_configure_lookup (const char *dbname, const char *service_line)
266 service_user *new_db;
269 for (cnt = 0; cnt < ndatabases; ++cnt)
271 int cmp = strcmp (dbname, databases[cnt].name);
276 __set_errno (EINVAL);
281 if (cnt == ndatabases)
283 __set_errno (EINVAL);
287 /* Test whether it is really used. */
288 if (databases[cnt].dbp == NULL)
289 /* Nothing to do, but we could do. */
292 /* Try to generate new data. */
293 new_db = nss_parse_service_list (service_line);
296 /* Illegal service specification. */
297 __set_errno (EINVAL);
301 /* Prevent multiple threads to change the service table. */
302 __libc_lock_lock (lock);
304 /* Install new rules. */
305 *databases[cnt].dbp = new_db;
307 __nss_database_custom[cnt] = true;
310 __libc_lock_unlock (lock);
316 /* Comparison function for searching NI->known tree. */
318 known_compare (const void *p1, const void *p2)
320 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
321 *(const char *const *) p2);
325 #if !defined DO_STATIC_NSS || defined SHARED
328 nss_load_library (service_user *ni)
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,
340 if (ni->library == NULL)
344 if (ni->library->lib_handle == NULL)
346 /* Load the shared library. */
347 size_t shlen = (7 + strlen (ni->name) + 3
348 + strlen (__nss_shlib_revision) + 1);
349 int saved_errno = errno;
350 char shlib_name[shlen];
352 /* Construct shared object name. */
353 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name,
357 __nss_shlib_revision);
359 ni->library->lib_handle = __libc_dlopen (shlib_name);
360 if (ni->library->lib_handle == NULL)
362 /* Failed to load the library. */
363 ni->library->lib_handle = (void *) -1l;
364 __set_errno (saved_errno);
369 /* Call the init function when nscd is used. */
370 size_t initlen = (5 + strlen (ni->name)
371 + strlen ("_init") + 1);
372 char init_name[initlen];
374 /* Construct the init function name. */
375 __stpcpy (__stpcpy (__stpcpy (init_name,
380 /* Find the optional init function. */
381 void (*ifct) (void (*) (size_t, struct traced_file *))
382 = __libc_dlsym (ni->library->lib_handle, init_name);
385 void (*cb) (size_t, struct traced_file *) = nscd_init_cb;
401 __nss_lookup_function (service_user *ni, const char *fct_name)
403 void **found, *result;
405 /* We now modify global data. Protect it. */
406 __libc_lock_lock (lock);
408 /* Search the tree of functions previously requested. Data in the
409 tree are `known_function' structures, whose first member is a
410 `const char *', the lookup key. The search returns a pointer to
411 the tree node structure; the first member of the is a pointer to
412 our structure (i.e. what will be a `known_function'); since the
413 first member of that is the lookup key string, &FCT_NAME is close
414 enough to a pointer to our structure to use as a lookup key that
415 will be passed to `known_compare' (above). */
417 found = __tsearch (&fct_name, &ni->known, &known_compare);
419 /* This means out-of-memory. */
421 else if (*found != &fct_name)
423 /* The search found an existing structure in the tree. */
424 result = ((known_function *) *found)->fct_ptr;
426 PTR_DEMANGLE (result);
431 /* This name was not known before. Now we have a node in the tree
432 (in the proper sorted position for FCT_NAME) that points to
433 &FCT_NAME instead of any real `known_function' structure.
434 Allocate a new structure and fill it in. */
436 known_function *known = malloc (sizeof *known);
439 #if !defined DO_STATIC_NSS || defined SHARED
442 /* Oops. We can't instantiate this node properly.
443 Remove it from the tree. */
444 __tdelete (&fct_name, &ni->known, &known_compare);
450 /* Point the tree node at this new structure. */
452 known->fct_name = fct_name;
454 #if !defined DO_STATIC_NSS || defined SHARED
455 /* Load the appropriate library. */
456 if (nss_load_library (ni) != 0)
457 /* This only happens when out of memory. */
458 goto remove_from_tree;
460 if (ni->library->lib_handle == (void *) -1l)
461 /* Library not found => function not found. */
465 /* Get the desired function. */
466 size_t namlen = (5 + strlen (ni->name) + 1
467 + strlen (fct_name) + 1);
470 /* Construct the function name. */
471 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
476 /* Look up the symbol. */
477 result = __libc_dlsym (ni->library->lib_handle, name);
480 /* We can't get function address dynamically in static linking. */
482 # define DEFINE_ENT(h,nm) \
483 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
484 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
485 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
486 # define DEFINE_GET(h,nm) \
487 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
488 # define DEFINE_GETBY(h,nm,ky) \
489 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
490 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
492 # include "function.def"
495 size_t namlen = (5 + strlen (ni->name) + 1
496 + strlen (fct_name) + 1);
499 /* Construct the function name. */
500 __stpcpy (__stpcpy (__stpcpy (name, ni->name),
505 for (tp = &tbl[0]; tp->fname; tp++)
506 if (strcmp (tp->fname, name) == 0)
514 /* Remember function pointer for later calls. Even if null, we
515 record it so a second try needn't search the library again. */
516 known->fct_ptr = result;
518 PTR_MANGLE (known->fct_ptr);
523 /* Remove the lock. */
524 __libc_lock_unlock (lock);
528 libc_hidden_def (__nss_lookup_function)
531 static name_database *
532 nss_parse_file (const char *fname)
535 name_database *result;
536 name_database_entry *last;
540 /* Open the configuration file. */
541 fp = fopen (fname, "rce");
545 /* No threads use this stream. */
546 __fsetlocking (fp, FSETLOCKING_BYCALLER);
548 result = (name_database *) malloc (sizeof (name_database));
555 result->entry = NULL;
556 result->library = NULL;
562 name_database_entry *this;
565 n = __getline (&line, &len, fp);
568 if (line[n - 1] == '\n')
571 /* Because the file format does not know any form of quoting we
572 can search forward for the next '#' character and if found
573 make it terminating the line. */
574 *__strchrnul (line, '#') = '\0';
576 /* If the line is blank it is ignored. */
580 /* Each line completely specifies the actions for a database. */
581 this = nss_getline (line);
587 result->entry = this;
592 while (!__feof_unlocked (fp));
594 /* Free the buffer. */
596 /* Close configuration file. */
603 /* Read the source names:
604 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
606 static service_user *
607 nss_parse_service_list (const char *line)
609 service_user *result = NULL, **nextp = &result;
613 service_user *new_service;
616 while (isspace (line[0]))
619 /* No source specified. */
622 /* Read <source> identifier. */
624 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
630 new_service = (service_user *) malloc (sizeof (service_user)
631 + (line - name + 1));
632 if (new_service == NULL)
635 *((char *) __mempcpy (new_service->name, name, line - name)) = '\0';
637 /* Set default actions. */
638 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
639 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
640 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
641 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
642 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
643 new_service->library = NULL;
644 new_service->known = NULL;
645 new_service->next = NULL;
647 while (isspace (line[0]))
652 /* Read criterions. */
655 while (line[0] != '\0' && isspace (line[0]));
660 enum nss_status status;
661 lookup_actions action;
663 /* Grok ! before name to mean all statii but that one. */
664 not = line[0] == '!';
668 /* Read status name. */
670 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
674 /* Compare with known statii. */
675 if (line - name == 7)
677 if (__strncasecmp (name, "SUCCESS", 7) == 0)
678 status = NSS_STATUS_SUCCESS;
679 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
680 status = NSS_STATUS_UNAVAIL;
684 else if (line - name == 8)
686 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
687 status = NSS_STATUS_NOTFOUND;
688 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
689 status = NSS_STATUS_TRYAGAIN;
696 while (isspace (line[0]))
702 while (isspace (line[0]));
705 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
709 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
710 action = NSS_ACTION_RETURN;
711 else if (line - name == 8
712 && __strncasecmp (name, "CONTINUE", 8) == 0)
713 action = NSS_ACTION_CONTINUE;
714 else if (line - name == 5
715 && __strncasecmp (name, "MERGE", 5) == 0)
716 action = NSS_ACTION_MERGE;
722 /* Save the current action setting for this status,
723 set them all to the given action, and reset this one. */
724 const lookup_actions save = new_service->actions[2 + status];
725 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
726 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
727 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
728 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
729 new_service->actions[2 + status] = save;
732 new_service->actions[2 + status] = action;
734 /* Skip white spaces. */
735 while (isspace (line[0]))
738 while (line[0] != ']');
744 *nextp = new_service;
745 nextp = &new_service->next;
754 static name_database_entry *
755 nss_getline (char *line)
758 name_database_entry *result;
761 /* Ignore leading white spaces. ATTENTION: this is different from
762 what is implemented in Solaris. The Solaris man page says a line
763 beginning with a white space character is ignored. We regard
764 this as just another misfeature in Solaris. */
765 while (isspace (line[0]))
768 /* Recognize `<database> ":"'. */
770 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
772 if (line[0] == '\0' || name == line)
777 len = strlen (name) + 1;
779 result = (name_database_entry *) malloc (sizeof (name_database_entry) + len);
783 /* Save the database name. */
784 memcpy (result->name, name, len);
786 /* Parse the list of services. */
787 result->service = nss_parse_service_list (line);
794 #if !defined DO_STATIC_NSS || defined SHARED
795 static service_library *
796 nss_new_service (name_database *database, const char *name)
798 service_library **currentp = &database->library;
800 while (*currentp != NULL)
802 if (strcmp ((*currentp)->name, name) == 0)
804 currentp = &(*currentp)->next;
807 /* We have to add the new service. */
808 *currentp = (service_library *) malloc (sizeof (service_library));
809 if (*currentp == NULL)
812 (*currentp)->name = name;
813 (*currentp)->lib_handle = NULL;
814 (*currentp)->next = NULL;
821 #if defined SHARED && defined USE_NSCD
822 /* Load all libraries for the service. */
824 nss_load_all_libraries (const char *service, const char *def)
826 service_user *ni = NULL;
828 if (__nss_database_lookup2 (service, NULL, def, &ni) == 0)
831 nss_load_library (ni);
837 /* Called by nscd and nscd alone. */
839 __nss_disable_nscd (void (*cb) (size_t, struct traced_file *))
847 /* Find all the relevant modules so that the init functions are called. */
848 nss_load_all_libraries ("passwd", DEFAULT_CONFIG);
849 nss_load_all_libraries ("group", DEFAULT_CONFIG);
850 nss_load_all_libraries ("hosts", "dns [!UNAVAIL=return] files");
851 nss_load_all_libraries ("services", NULL);
853 /* Disable all uses of NSCD. */
854 __nss_not_use_nscd_passwd = -1;
855 __nss_not_use_nscd_group = -1;
856 __nss_not_use_nscd_hosts = -1;
857 __nss_not_use_nscd_services = -1;
858 __nss_not_use_nscd_netgroup = -1;
863 free_database_entries (name_database_entry *entry)
865 while (entry != NULL)
867 name_database_entry *olde = entry;
868 service_user *service = entry->service;
870 while (service != NULL)
872 service_user *olds = service;
874 if (service->known != NULL)
875 __tdestroy (service->known, free);
877 service = service->next;
886 /* Free all resources if necessary. */
887 libc_freeres_fn (free_defconfig)
889 name_database_entry *entry = defconfig_entries;
892 /* defconfig was not used. */
895 /* Don't disturb ongoing other threads (if there are any). */
896 defconfig_entries = NULL;
898 free_database_entries (entry);
901 libc_freeres_fn (free_mem)
903 name_database *top = service_table;
904 service_library *library;
907 /* Maybe we have not read the nsswitch.conf file. */
910 /* Don't disturb ongoing other threads (if there are any). */
911 service_table = NULL;
913 free_database_entries (top->entry);
915 library = top->library;
916 while (library != NULL)
918 service_library *oldl = library;
920 if (library->lib_handle && library->lib_handle != (void *) -1l)
921 __libc_dlclose (library->lib_handle);
923 library = library->next;