1 /* Copyright (C) 1996, 1997 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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include <libc-lock.h>
25 #include <link.h> /* We need some help from ld.so. */
30 #include <gnu/lib-names.h>
34 /* Prototypes for the local functions. */
35 static void *nss_lookup_function (service_user *ni, const char *fct_name);
36 static name_database *nss_parse_file (const char *fname);
37 static name_database_entry *nss_getline (char *line);
38 static service_user *nss_parse_service_list (const char *line);
39 static service_library *nss_new_service (name_database *database,
43 /* Declare external database variables. */
44 #define DEFINE_DATABASE(name) \
45 extern service_user *__nss_##name##_database; \
46 weak_extern (__nss_##name##_database)
47 #include "databases.def"
48 #undef DEFINE_DATABASE
50 /* Structure to map database name to variable. */
57 #define DEFINE_DATABASE(name) \
58 { #name, &__nss_##name##_database },
59 #include "databases.def"
60 #undef DEFINE_DATABASE
64 __libc_lock_define_initialized (static, lock)
67 /* String with revision number of the shared object files. */
68 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
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 *alternate_name,
78 const char *defconfig, service_user **ni)
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 /* Are we initialized yet? */
92 if (service_table == NULL)
93 /* Read config file. */
94 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
96 /* Test whether configuration data is available. */
97 if (service_table != NULL)
99 /* Return first `service_user' entry for DATABASE. */
100 name_database_entry *entry;
102 /* XXX Could use some faster mechanism here. But each database is
103 only requested once and so this might not be critical. */
104 for (entry = service_table->entry; entry != NULL; entry = entry->next)
105 if (strcmp (database, entry->name) == 0)
106 *ni = entry->service;
108 if (*ni == NULL && alternate_name != NULL)
109 /* We haven't found an entry so far. Try to find it with the
111 for (entry = service_table->entry; entry != NULL; entry = entry->next)
112 if (strcmp (alternate_name, entry->name) == 0)
113 *ni = entry->service;
116 /* No configuration data is available, either because nsswitch.conf
117 doesn't exist or because it doesn't has a line for this database.
119 DEFCONFIG specifies the default service list for this database,
120 or null to use the most common default. */
122 *ni = nss_parse_service_list (defconfig
123 ?: "nis [NOTFOUND=return] files");
125 __libc_lock_unlock (lock);
132 0 == adjusted for next function */
134 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
136 *fctp = nss_lookup_function (*ni, fct_name);
139 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
140 && (*ni)->next != NULL)
144 *fctp = nss_lookup_function (*ni, fct_name);
147 return *fctp != NULL ? 0 : -1;
152 0 == adjusted for next function
155 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
160 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
161 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
162 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
163 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
168 /* This is really only for debugging. */
169 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
170 __libc_fatal ("illegal status in " __FUNCTION__);
172 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
176 if ((*ni)->next == NULL)
183 *fctp = nss_lookup_function (*ni, fct_name);
186 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
187 && (*ni)->next != NULL);
189 return *fctp != NULL ? 0 : -1;
194 __nss_configure_lookup (const char *dbname, const char *service_line)
196 service_user *new_db;
199 for (cnt = 0; cnt < sizeof databases; ++cnt)
201 int cmp = strcmp (dbname, databases[cnt].name);
206 __set_errno (EINVAL);
211 if (cnt == sizeof databases)
213 __set_errno (EINVAL);
217 /* Test whether it is really used. */
218 if (databases[cnt].dbp == NULL)
219 /* Nothing to do, but we could do. */
222 /* Try to generate new data. */
223 new_db = nss_parse_service_list (service_line);
226 /* Illegal service specification. */
227 __set_errno (EINVAL);
231 /* Prevent multiple threads to change the service table. */
232 __libc_lock_lock (lock);
234 /* Install new rules. */
235 *databases[cnt].dbp = new_db;
237 __libc_lock_unlock (lock);
244 nss_dlerror_run (void (*operate) (void))
246 char *last_errstring = NULL;
247 const char *last_object_name = NULL;
250 (void) _dl_catch_error (&last_errstring, &last_object_name, operate);
252 result = last_errstring != NULL;
254 free (last_errstring);
260 /* Comparison function for searching NI->known tree. */
262 known_compare (const void *p1, const void *p2)
264 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
265 *(const char *const *) p2);
270 nss_lookup_function (service_user *ni, const char *fct_name)
272 void **found, *result;
274 /* We now modify global data. Protect it. */
275 __libc_lock_lock (lock);
277 /* Search the tree of functions previously requested. Data in the
278 tree are `known_function' structures, whose first member is a
279 `const char *', the lookup key. The search returns a pointer to
280 the tree node structure; the first member of the is a pointer to
281 our structure (i.e. what will be a `known_function'); since the
282 first member of that is the lookup key string, &FCT_NAME is close
283 enough to a pointer to our structure to use as a lookup key that
284 will be passed to `known_compare' (above). */
286 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
287 if (*found != &fct_name)
288 /* The search found an existing structure in the tree. */
289 result = ((known_function *) *found)->fct_ptr;
292 /* This name was not known before. Now we have a node in the tree
293 (in the proper sorted position for FCT_NAME) that points to
294 &FCT_NAME instead of any real `known_function' structure.
295 Allocate a new structure and fill it in. */
297 known_function *known = malloc (sizeof *known);
301 /* Oops. We can't instantiate this node properly.
302 Remove it from the tree. */
303 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
308 /* Point the tree node at this new structure. */
310 known->fct_name = fct_name;
312 if (ni->library == NULL)
314 /* This service has not yet been used. Fetch the service
315 library for it, creating a new one if need be. If there
316 is no service table from the file, this static variable
317 holds the head of the service_library list made from the
318 default configuration. */
319 static name_database default_table;
320 ni->library = nss_new_service (service_table ?: &default_table,
322 if (ni->library == NULL)
324 /* This only happens when out of memory. */
326 goto remove_from_tree;
330 if (ni->library->lib_handle == NULL)
332 /* Load the shared library. */
333 size_t shlen = (7 + strlen (ni->library->name) + 3
334 + strlen (__nss_shlib_revision) + 1);
335 char shlib_name[shlen];
339 /* Open and relocate the shared object. */
340 ni->library->lib_handle = _dl_open (shlib_name, RTLD_LAZY);
343 /* Construct shared object name. */
344 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name, "libnss_"),
347 __nss_shlib_revision);
349 if (nss_dlerror_run (do_open) != 0)
350 /* Failed to load the library. */
351 ni->library->lib_handle = (void *) -1l;
354 if (ni->library->lib_handle == (void *) -1l)
355 /* Library not found => function not found. */
359 /* Get the desired function. Again, GNU ld.so magic ahead. */
360 size_t namlen = (5 + strlen (ni->library->name) + 1
361 + strlen (fct_name) + 1);
363 struct link_map *map = ni->library->lib_handle;
365 const ElfW(Sym) *ref = NULL;
368 struct link_map *scope[2] = { map, NULL };
369 loadbase = _dl_lookup_symbol (name, &ref,
370 scope, map->l_name, 0);
373 /* Construct the function name. */
374 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
379 /* Look up the symbol. */
380 result = (nss_dlerror_run (get_sym)
381 ? NULL : (void *) (loadbase + ref->st_value));
384 /* Remember function pointer for later calls. Even if null, we
385 record it so a second try needn't search the library again. */
386 known->fct_ptr = result;
390 /* Remove the lock. */
391 __libc_lock_unlock (lock);
397 static name_database *
398 nss_parse_file (const char *fname)
401 name_database *result;
402 name_database_entry *last;
406 /* Open the configuration file. */
407 fp = fopen (fname, "r");
411 result = (name_database *) malloc (sizeof (name_database));
415 result->entry = NULL;
416 result->library = NULL;
422 name_database_entry *this;
426 n = __getline (&line, &len, fp);
429 if (line[n - 1] == '\n')
432 /* Because the file format does not know any form of quoting we
433 can search forward for the next '#' character and if found
434 make it terminating the line. */
435 cp = strchr (line, '#');
439 /* If the line is blank it is ignored. */
443 /* Each line completely specifies the actions for a database. */
444 this = nss_getline (line);
450 result->entry = this;
457 /* Free the buffer. */
459 /* Close configuration file. */
466 /* Read the source names:
467 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
469 static service_user *
470 nss_parse_service_list (const char *line)
472 service_user *result = NULL, **nextp = &result;
476 service_user *new_service;
479 while (isspace (line[0]))
482 /* No source specified. */
485 /* Read <source> identifier. */
487 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
493 new_service = (service_user *) malloc (sizeof (service_user));
494 if (new_service == NULL)
498 char *source = (char *) malloc (line - name + 1);
504 memcpy (source, name, line - name);
505 source[line - name] = '\0';
507 new_service->name = source;
510 /* Set default actions. */
511 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
512 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
513 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
514 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
515 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
516 new_service->library = NULL;
517 new_service->known = NULL;
518 new_service->next = NULL;
520 while (isspace (line[0]))
525 /* Read criterions. */
528 while (line[0] != '\0' && isspace (line[0]));
533 enum nss_status status;
534 lookup_actions action;
536 /* Grok ! before name to mean all statii but that one. */
537 if (not = line[0] == '!')
540 /* Read status name. */
542 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
546 /* Compare with known statii. */
547 if (line - name == 7)
549 if (__strncasecmp (name, "SUCCESS", 7) == 0)
550 status = NSS_STATUS_SUCCESS;
551 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
552 status = NSS_STATUS_UNAVAIL;
556 else if (line - name == 8)
558 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
559 status = NSS_STATUS_NOTFOUND;
560 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
561 status = NSS_STATUS_TRYAGAIN;
568 while (isspace (line[0]))
574 while (isspace (line[0]));
577 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
581 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
582 action = NSS_ACTION_RETURN;
583 else if (line - name == 8
584 && __strncasecmp (name, "CONTINUE", 8) == 0)
585 action = NSS_ACTION_CONTINUE;
591 /* Save the current action setting for this status,
592 set them all to the given action, and reset this one. */
593 const lookup_actions save = new_service->actions[2 + status];
594 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
595 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
596 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
597 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
598 new_service->actions[2 + status] = save;
601 new_service->actions[2 + status] = action;
603 /* Skip white spaces. */
604 while (isspace (line[0]))
607 while (line[0] != ']');
613 *nextp = new_service;
614 nextp = &new_service->next;
618 static name_database_entry *
619 nss_getline (char *line)
622 name_database_entry *result;
624 /* Ignore leading white spaces. ATTENTION: this is different from
625 what is implemented in Solaris. The Solaris man page says a line
626 beginning with a white space character is ignored. We regard
627 this as just another misfeature in Solaris. */
628 while (isspace (line[0]))
631 /* Recognize `<database> ":"'. */
633 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
635 if (line[0] == '\0' || name == line)
640 result = (name_database_entry *) malloc (sizeof (name_database_entry));
644 /* Save the database name. */
646 const size_t len = strlen (name) + 1;
647 char *new = malloc (len);
653 result->name = memcpy (new, name, len);
656 /* Parse the list of services. */
657 result->service = nss_parse_service_list (line);
664 static service_library *
665 nss_new_service (name_database *database, const char *name)
667 service_library **currentp = &database->library;
669 while (*currentp != NULL)
671 if (strcmp ((*currentp)->name, name) == 0)
673 currentp = &(*currentp)->next;
676 /* We have to add the new service. */
677 *currentp = (service_library *) malloc (sizeof (service_library));
678 if (*currentp == NULL)
681 (*currentp)->name = name;
682 (*currentp)->lib_handle = NULL;
683 (*currentp)->next = NULL;