1 /* Copyright (C) 1996 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
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include <libc-lock.h>
31 #include "../elf/link.h" /* We need some help from ld.so. */
33 /* Prototypes for the local functions. */
34 static void *nss_lookup_function (service_user *ni, const char *fct_name);
35 static name_database *nss_parse_file (const char *fname);
36 static name_database_entry *nss_getline (char *line);
37 static service_user *nss_parse_service_list (const char *line);
38 static service_library *nss_new_service (name_database *database,
42 /* Declare external database variables. */
43 #define DEFINE_DATABASE(name) \
44 extern service_user *__nss_##name##_database; \
45 weak_extern (__nss_##name##_database)
46 #include "databases.def"
47 #undef DEFINE_DATABASE
49 /* Structure to map database name to variable. */
56 #define DEFINE_DATABASE(name) \
57 { #name, &__nss_##name##_database },
58 #include "databases.def"
59 #undef DEFINE_DATABASE
63 __libc_lock_define_initialized (static, lock)
66 /* Nonzero if the sevices are already initialized. */
67 static int nss_initialized;
70 /* The root of the whole data base. */
71 static name_database *service_table;
74 /* -1 == database not found
75 0 == database entry pointer stored */
77 __nss_database_lookup (const char *database, const char *defconfig,
80 /* Prevent multiple threads to change the service table. */
81 __libc_lock_lock (lock);
83 /* Reconsider database variable in case some other thread called
84 `__nss_configure_lookup' while we waited for the lock. */
87 __libc_lock_unlock (lock);
91 if (nss_initialized == 0 && service_table == NULL)
92 /* Read config file. */
93 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
95 /* Test whether configuration data is available. */
96 if (service_table != NULL)
98 /* Return first `service_user' entry for DATABASE. */
99 name_database_entry *entry;
101 /* XXX Could use some faster mechanism here. But each database is
102 only requested once and so this might not be critical. */
103 for (entry = service_table->entry; entry != NULL; entry = entry->next)
104 if (strcmp (database, entry->name) == 0)
105 *ni = entry->service;
108 /* No configuration data is available, either because nsswitch.conf
109 doesn't exist or because it doesn't has a line for this database.
111 DEFCONFIG specifies the default service list for this database,
112 or null to use the most common default. */
114 *ni = nss_parse_service_list (defconfig
115 ?: "compat [NOTFOUND=return] files");
117 __libc_lock_unlock (lock);
124 0 == adjusted for next function */
126 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
128 *fctp = nss_lookup_function (*ni, fct_name);
131 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
132 && (*ni)->next != NULL)
136 *fctp = nss_lookup_function (*ni, fct_name);
139 return *fctp != NULL ? 0 : -1;
144 0 == adjusted for next function
147 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
152 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
153 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
154 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
155 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
160 /* This is really only for debugging. */
161 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_SUCCESS)
162 __libc_fatal ("illegal status in " __FUNCTION__);
164 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
168 if ((*ni)->next == NULL)
175 *fctp = nss_lookup_function (*ni, fct_name);
178 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
179 && (*ni)->next != NULL);
181 return *fctp != NULL ? 0 : -1;
186 __nss_configure_lookup (const char *dbname, const char *service_line)
188 service_user *new_db;
191 for (cnt = 0; cnt < sizeof databases; ++cnt)
193 int cmp = strcmp (dbname, databases[cnt].name);
203 if (cnt == sizeof databases)
209 /* Test whether it is really used. */
210 if (databases[cnt].dbp == NULL)
211 /* Nothing to do, but we could do. */
214 /* Try to generate new data. */
215 new_db = nss_parse_service_list (service_line);
218 /* Illegal service specification. */
223 /* Prevent multiple threads to change the service table. */
224 __libc_lock_lock (lock);
226 /* Install new rules. */
227 *databases[cnt].dbp = new_db;
229 __libc_lock_unlock (lock);
236 nss_dlerror_run (void (*operate) (void))
238 const char *last_errstring = NULL;
239 const char *last_object_name = NULL;
241 (void) _dl_catch_error (&last_errstring, &last_object_name, operate);
243 return last_errstring != NULL;
247 /* Comparison function for searching NI->known tree. */
249 known_compare (const void *p1, const void *p2)
251 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
252 *(const char *const *) p2);
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;
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);
288 /* Oops. We can't instantiate this node properly.
289 Remove it from the tree. */
290 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
295 /* Point the tree node at this new structure. */
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,
309 if (ni->library == NULL)
311 /* This only happens when out of memory. */
313 goto remove_from_tree;
317 if (ni->library->lib_handle == NULL)
319 /* Load the shared library. */
320 size_t shlen = (7 + strlen (ni->library->name) + 3
321 + sizeof (NSS_SHLIB_REVISION));
322 char shlib_name[shlen];
326 /* Open and relocate the shared object. */
327 ni->library->lib_handle = _dl_open (shlib_name, RTLD_LAZY);
330 /* Construct shared object name. */
331 __stpcpy (__stpcpy (__stpcpy (shlib_name, "libnss_"),
333 ".so" NSS_SHLIB_REVISION);
335 if (nss_dlerror_run (do_open) != 0)
336 /* Failed to load the library. */
337 ni->library->lib_handle = (void *) -1;
340 if (ni->library->lib_handle == (void *) -1)
341 /* Library not found => function not found. */
345 /* Get the desired function. Again, GNU ld.so magic ahead. */
346 size_t namlen = (5 + strlen (ni->library->name) + 1
347 + strlen (fct_name) + 1);
349 struct link_map *map = ni->library->lib_handle;
351 const ElfW(Sym) *ref = NULL;
354 struct link_map *scope[2] = { map, NULL };
355 loadbase = _dl_lookup_symbol (name, &ref,
356 scope, map->l_name, 0);
359 /* Construct the function name. */
360 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
365 /* Look up the symbol. */
366 result = (nss_dlerror_run (get_sym)
367 ? NULL : (void *) (loadbase + ref->st_value));
370 /* Remember function pointer for later calls. Even if null, we
371 record it so a second try needn't search the library again. */
372 known->fct_ptr = result;
376 /* Remove the lock. */
377 __libc_lock_unlock (lock);
383 static name_database *
384 nss_parse_file (const char *fname)
387 name_database *result;
388 name_database_entry *last;
392 /* Open the configuration file. */
393 fp = fopen (fname, "r");
397 result = (name_database *) malloc (sizeof (name_database));
401 result->entry = NULL;
402 result->library = NULL;
408 name_database_entry *this;
412 n = __getline (&line, &len, fp);
415 if (line[n - 1] == '\n')
418 /* Because the file format does not know any form of quoting we
419 can search forward for the next '#' character and if found
420 make it terminating the line. */
421 cp = strchr (line, '#');
425 /* If the line is blank it is ignored. */
429 /* Each line completely specifies the actions for a database. */
430 this = nss_getline (line);
436 result->entry = this;
443 /* Free the buffer. */
445 /* Close configuration file. */
452 /* Read the source names:
453 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
455 static service_user *
456 nss_parse_service_list (const char *line)
458 service_user *result = NULL, **nextp = &result;
462 service_user *new_service;
465 while (isspace (line[0]))
468 /* No source specified. */
471 /* Read <source> identifier. */
473 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
479 new_service = (service_user *) malloc (sizeof (service_user));
480 if (new_service == NULL)
484 char *source = (char *) malloc (line - name + 1);
490 memcpy (source, name, line - name);
491 source[line - name] = '\0';
493 new_service->name = source;
496 /* Set default actions. */
497 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
498 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
499 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
500 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
501 new_service->library = NULL;
502 new_service->known = NULL;
503 new_service->next = NULL;
505 while (isspace (line[0]))
510 /* Read criterions. */
513 while (line[0] != '\0' && isspace (line[0]));
518 enum nss_status status;
519 lookup_actions action;
521 /* Grok ! before name to mean all statii but that one. */
522 if (not = line[0] == '!')
525 /* Read status name. */
527 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
531 /* Compare with known statii. */
532 if (line - name == 7)
534 if (__strncasecmp (name, "SUCCESS", 7) == 0)
535 status = NSS_STATUS_SUCCESS;
536 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
537 status = NSS_STATUS_UNAVAIL;
541 else if (line - name == 8)
543 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
544 status = NSS_STATUS_NOTFOUND;
545 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
546 status = NSS_STATUS_TRYAGAIN;
553 while (isspace (line[0]))
559 while (isspace (line[0]));
562 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
566 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
567 action = NSS_ACTION_RETURN;
568 else if (line - name == 8
569 && __strncasecmp (name, "CONTINUE", 8) == 0)
570 action = NSS_ACTION_CONTINUE;
576 /* Save the current action setting for this status,
577 set them all to the given action, and reset this one. */
578 const lookup_actions save = new_service->actions[2 + status];
579 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
580 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
581 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
582 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
583 new_service->actions[2 + status] = save;
586 new_service->actions[2 + status] = action;
588 /* Skip white spaces. */
589 while (isspace (line[0]))
592 while (line[0] != ']');
598 *nextp = new_service;
599 nextp = &new_service->next;
603 static name_database_entry *
604 nss_getline (char *line)
607 name_database_entry *result;
609 /* Ignore leading white spaces. ATTENTION: this is different from
610 what is implemented in Solaris. The Solaris man page says a line
611 beginning with a white space character is ignored. We regard
612 this as just another misfeature in Solaris. */
613 while (isspace (line[0]))
616 /* Recognize `<database> ":"'. */
618 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
620 if (line[0] == '\0' || name == line)
625 result = (name_database_entry *) malloc (sizeof (name_database_entry));
629 /* Save the database name. */
631 const size_t len = strlen (name) + 1;
632 char *new = malloc (len);
638 result->name = memcpy (new, name, len);
641 /* Parse the list of services. */
642 result->service = nss_parse_service_list (line);
649 static service_library *
650 nss_new_service (name_database *database, const char *name)
652 service_library **currentp = &database->library;
654 while (*currentp != NULL)
656 if (strcmp ((*currentp)->name, name) == 0)
658 currentp = &(*currentp)->next;
661 /* We have to add the new service. */
662 *currentp = (service_library *) malloc (sizeof (service_library));
663 if (*currentp == NULL)
666 (*currentp)->name = name;
667 (*currentp)->lib_handle = NULL;
668 (*currentp)->next = NULL;