1 /* Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004
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
25 #include <bits/libc-lock.h>
28 #include <stdio_ext.h>
34 #include <netinet/ether.h>
38 #if !defined DO_STATIC_NSS || defined SHARED
39 # include <gnu/lib-names.h>
43 #include "../nscd/nscd_proto.h"
45 /* Prototypes for the local functions. */
46 static name_database *nss_parse_file (const char *fname) internal_function;
47 static name_database_entry *nss_getline (char *line) internal_function;
48 static service_user *nss_parse_service_list (const char *line)
50 static service_library *nss_new_service (name_database *database,
51 const char *name) internal_function;
54 /* Declare external database variables. */
55 #define DEFINE_DATABASE(name) \
56 extern service_user *__nss_##name##_database attribute_hidden; \
57 weak_extern (__nss_##name##_database)
58 #include "databases.def"
59 #undef DEFINE_DATABASE
61 /* Structure to map database name to variable. */
68 #define DEFINE_DATABASE(name) \
69 { #name, &__nss_##name##_database },
70 #include "databases.def"
71 #undef DEFINE_DATABASE
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;
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 */
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. */
99 __libc_lock_unlock (lock);
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
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. */
134 *ni = nss_parse_service_list (defconfig
135 ?: "nis [NOTFOUND=return] files");
137 __libc_lock_unlock (lock);
141 libc_hidden_def (__nss_database_lookup)
148 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
150 *fctp = __nss_lookup_function (*ni, fct_name);
153 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
154 && (*ni)->next != NULL)
158 *fctp = __nss_lookup_function (*ni, fct_name);
161 return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
166 0 == adjusted for next function
169 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
174 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
175 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
176 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
177 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
182 /* This is really only for debugging. */
183 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
184 __libc_fatal ("illegal status in __nss_next");
186 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
190 if ((*ni)->next == NULL)
197 *fctp = __nss_lookup_function (*ni, fct_name);
200 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
201 && (*ni)->next != NULL);
203 return *fctp != NULL ? 0 : -1;
205 libc_hidden_def (__nss_next)
209 __nss_configure_lookup (const char *dbname, const char *service_line)
211 service_user *new_db;
214 for (cnt = 0; cnt < sizeof databases; ++cnt)
216 int cmp = strcmp (dbname, databases[cnt].name);
221 __set_errno (EINVAL);
226 if (cnt == sizeof databases)
228 __set_errno (EINVAL);
232 /* Test whether it is really used. */
233 if (databases[cnt].dbp == NULL)
234 /* Nothing to do, but we could do. */
237 /* Try to generate new data. */
238 new_db = nss_parse_service_list (service_line);
241 /* Illegal service specification. */
242 __set_errno (EINVAL);
246 /* Prevent multiple threads to change the service table. */
247 __libc_lock_lock (lock);
249 /* Install new rules. */
250 *databases[cnt].dbp = new_db;
252 __libc_lock_unlock (lock);
258 /* Comparison function for searching NI->known tree. */
260 known_compare (const void *p1, const void *p2)
262 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
263 *(const char *const *) p2);
268 __nss_lookup_function (service_user *ni, const char *fct_name)
270 void **found, *result;
272 /* We now modify global data. Protect it. */
273 __libc_lock_lock (lock);
275 /* Search the tree of functions previously requested. Data in the
276 tree are `known_function' structures, whose first member is a
277 `const char *', the lookup key. The search returns a pointer to
278 the tree node structure; the first member of the is a pointer to
279 our structure (i.e. what will be a `known_function'); since the
280 first member of that is the lookup key string, &FCT_NAME is close
281 enough to a pointer to our structure to use as a lookup key that
282 will be passed to `known_compare' (above). */
284 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
285 if (*found != &fct_name)
286 /* The search found an existing structure in the tree. */
287 result = ((known_function *) *found)->fct_ptr;
290 /* This name was not known before. Now we have a node in the tree
291 (in the proper sorted position for FCT_NAME) that points to
292 &FCT_NAME instead of any real `known_function' structure.
293 Allocate a new structure and fill it in. */
295 known_function *known = malloc (sizeof *known);
299 /* Oops. We can't instantiate this node properly.
300 Remove it from the tree. */
301 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
306 /* Point the tree node at this new structure. */
308 known->fct_name = fct_name;
310 if (ni->library == NULL)
312 /* This service has not yet been used. Fetch the service
313 library for it, creating a new one if need be. If there
314 is no service table from the file, this static variable
315 holds the head of the service_library list made from the
316 default configuration. */
317 static name_database default_table;
318 ni->library = nss_new_service (service_table ?: &default_table,
320 if (ni->library == NULL)
322 /* This only happens when out of memory. */
324 goto remove_from_tree;
328 #if !defined DO_STATIC_NSS || defined SHARED
329 if (ni->library->lib_handle == NULL)
331 /* Load the shared library. */
332 size_t shlen = (7 + strlen (ni->library->name) + 3
333 + strlen (__nss_shlib_revision) + 1);
334 int saved_errno = errno;
335 char shlib_name[shlen];
337 /* Construct shared object name. */
338 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name,
342 __nss_shlib_revision);
344 ni->library->lib_handle = __libc_dlopen (shlib_name);
345 if (ni->library->lib_handle == NULL)
347 /* Failed to load the library. */
348 ni->library->lib_handle = (void *) -1l;
349 __set_errno (saved_errno);
353 if (ni->library->lib_handle == (void *) -1l)
354 /* Library not found => function not found. */
358 /* Get the desired function. */
359 size_t namlen = (5 + strlen (ni->library->name) + 1
360 + strlen (fct_name) + 1);
363 /* Construct the function name. */
364 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
369 /* Look up the symbol. */
370 result = __libc_dlsym (ni->library->lib_handle, name);
373 /* We can't get function address dynamically in static linking. */
375 # define DEFINE_ENT(h,nm) \
376 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
377 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
378 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
379 # define DEFINE_GET(h,nm) \
380 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
381 # define DEFINE_GETBY(h,nm,ky) \
382 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
383 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
385 # include "function.def"
388 size_t namlen = (5 + strlen (ni->library->name) + 1
389 + strlen (fct_name) + 1);
392 /* Construct the function name. */
393 __stpcpy (__stpcpy (__stpcpy (name, ni->library->name),
398 for (tp = &tbl[0]; tp->fname; tp++)
399 if (strcmp (tp->fname, name) == 0)
407 /* Remember function pointer for later calls. Even if null, we
408 record it so a second try needn't search the library again. */
409 known->fct_ptr = result;
413 /* Remove the lock. */
414 __libc_lock_unlock (lock);
418 libc_hidden_def (__nss_lookup_function)
421 static name_database *
423 nss_parse_file (const char *fname)
426 name_database *result;
427 name_database_entry *last;
431 /* Open the configuration file. */
432 fp = fopen (fname, "rc");
436 /* No threads use this stream. */
437 __fsetlocking (fp, FSETLOCKING_BYCALLER);
439 result = (name_database *) malloc (sizeof (name_database));
443 result->entry = NULL;
444 result->library = NULL;
450 name_database_entry *this;
453 n = __getline (&line, &len, fp);
456 if (line[n - 1] == '\n')
459 /* Because the file format does not know any form of quoting we
460 can search forward for the next '#' character and if found
461 make it terminating the line. */
462 *__strchrnul (line, '#') = '\0';
464 /* If the line is blank it is ignored. */
468 /* Each line completely specifies the actions for a database. */
469 this = nss_getline (line);
475 result->entry = this;
480 while (!feof_unlocked (fp));
482 /* Free the buffer. */
484 /* Close configuration file. */
491 /* Read the source names:
492 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
494 static service_user *
496 nss_parse_service_list (const char *line)
498 service_user *result = NULL, **nextp = &result;
502 service_user *new_service;
505 while (isspace (line[0]))
508 /* No source specified. */
511 /* Read <source> identifier. */
513 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
519 new_service = (service_user *) malloc (sizeof (service_user)
520 + (line - name + 1));
521 if (new_service == NULL)
524 *((char *) __mempcpy (new_service->name, name, line - name)) = '\0';
526 /* Set default actions. */
527 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
528 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
529 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
530 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
531 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
532 new_service->library = NULL;
533 new_service->known = NULL;
534 new_service->next = NULL;
536 while (isspace (line[0]))
541 /* Read criterions. */
544 while (line[0] != '\0' && isspace (line[0]));
549 enum nss_status status;
550 lookup_actions action;
552 /* Grok ! before name to mean all statii but that one. */
553 not = line[0] == '!';
557 /* Read status name. */
559 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
563 /* Compare with known statii. */
564 if (line - name == 7)
566 if (__strncasecmp (name, "SUCCESS", 7) == 0)
567 status = NSS_STATUS_SUCCESS;
568 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
569 status = NSS_STATUS_UNAVAIL;
573 else if (line - name == 8)
575 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
576 status = NSS_STATUS_NOTFOUND;
577 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
578 status = NSS_STATUS_TRYAGAIN;
585 while (isspace (line[0]))
591 while (isspace (line[0]));
594 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
598 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
599 action = NSS_ACTION_RETURN;
600 else if (line - name == 8
601 && __strncasecmp (name, "CONTINUE", 8) == 0)
602 action = NSS_ACTION_CONTINUE;
608 /* Save the current action setting for this status,
609 set them all to the given action, and reset this one. */
610 const lookup_actions save = new_service->actions[2 + status];
611 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
612 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
613 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
614 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
615 new_service->actions[2 + status] = save;
618 new_service->actions[2 + status] = action;
620 /* Skip white spaces. */
621 while (isspace (line[0]))
624 while (line[0] != ']');
630 *nextp = new_service;
631 nextp = &new_service->next;
635 static name_database_entry *
637 nss_getline (char *line)
640 name_database_entry *result;
643 /* Ignore leading white spaces. ATTENTION: this is different from
644 what is implemented in Solaris. The Solaris man page says a line
645 beginning with a white space character is ignored. We regard
646 this as just another misfeature in Solaris. */
647 while (isspace (line[0]))
650 /* Recognize `<database> ":"'. */
652 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
654 if (line[0] == '\0' || name == line)
659 len = strlen (name) + 1;
661 result = (name_database_entry *) malloc (sizeof (name_database_entry) + len);
665 /* Save the database name. */
666 memcpy (result->name, name, len);
668 /* Parse the list of services. */
669 result->service = nss_parse_service_list (line);
676 static service_library *
678 nss_new_service (name_database *database, const char *name)
680 service_library **currentp = &database->library;
682 while (*currentp != NULL)
684 if (strcmp ((*currentp)->name, name) == 0)
686 currentp = &(*currentp)->next;
689 /* We have to add the new service. */
690 *currentp = (service_library *) malloc (sizeof (service_library));
691 if (*currentp == NULL)
694 (*currentp)->name = name;
695 (*currentp)->lib_handle = NULL;
696 (*currentp)->next = NULL;
702 /* Called by nscd and nscd alone. */
704 __nss_disable_nscd (void)
706 /* Disable all uses of NSCD. */
707 __nss_not_use_nscd_passwd = -1;
708 __nss_not_use_nscd_group = -1;
709 __nss_not_use_nscd_hosts = -1;
713 /* Free all resources if necessary. */
714 libc_freeres_fn (free_mem)
716 name_database *top = service_table;
717 name_database_entry *entry;
718 service_library *library;
721 /* Maybe we have not read the nsswitch.conf file. */
724 /* Don't disturb ongoing other threads (if there are any). */
725 service_table = NULL;
728 while (entry != NULL)
730 name_database_entry *olde = entry;
731 service_user *service = entry->service;
733 while (service != NULL)
735 service_user *olds = service;
737 if (service->known != NULL)
738 __tdestroy (service->known, free);
740 service = service->next;
748 library = top->library;
749 while (library != NULL)
751 service_library *oldl = library;
753 if (library->lib_handle && library->lib_handle != (void *) -1l)
754 __libc_dlclose (library->lib_handle);
756 library = library->next;