Last minute change.
[glibc.git] / nss / nsswitch.c
blob0a4c9482d2f5132dc373cd49a17ae6ee2fd07b8a
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. */
20 #include <ctype.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <netdb.h>
24 #include <libc-lock.h>
25 #include <search.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "nsswitch.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,
39 const char *name);
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. */
50 static struct
52 const char *name;
53 service_user **dbp;
54 } databases[] =
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 */
76 int
77 __nss_database_lookup (const char *database, const char *defconfig,
78 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. */
85 if (*ni != NULL)
86 return 0;
88 if (nss_initialized == 0 && service_table == NULL)
89 /* Read config file. */
90 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
92 /* Test whether configuration data is available. */
93 if (service_table != NULL)
95 /* Return first `service_user' entry for DATABASE. */
96 name_database_entry *entry;
98 /* XXX Could use some faster mechanism here. But each database is
99 only requested once and so this might not be critical. */
100 for (entry = service_table->entry; entry != NULL; entry = entry->next)
101 if (strcmp (database, entry->name) == 0)
103 *ni = entry->service;
104 return 0;
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. */
113 *ni = nss_parse_service_list (defconfig ?: "compat [NOTFOUND=return] files");
115 __libc_lock_unlock (lock);
117 return 0;
121 /* -1 == not found
122 0 == adjusted for next function */
124 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
126 *fctp = nss_lookup_function (*ni, fct_name);
128 while (*fctp == NULL
129 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
130 && (*ni)->next != NULL)
132 *ni = (*ni)->next;
134 *fctp = nss_lookup_function (*ni, fct_name);
137 return *fctp != NULL ? 0 : -1;
141 /* -1 == not found
142 0 == adjusted for next function
143 1 == finished */
145 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
146 int all_values)
148 if (all_values)
150 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
151 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
152 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
153 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
154 return 1;
156 else
158 /* This is really only for debugging. */
159 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_SUCCESS)
160 __libc_fatal ("illegal status in " __FUNCTION__);
162 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
163 return 1;
166 if ((*ni)->next == NULL)
167 return -1;
171 *ni = (*ni)->next;
173 *fctp = nss_lookup_function (*ni, fct_name);
175 while (*fctp == NULL
176 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
177 && (*ni)->next != NULL);
179 return *fctp != NULL ? 0 : -1;
184 __nss_configure_lookup (const char *dbname, const char *service_line)
186 service_user *new_db;
187 size_t cnt;
189 for (cnt = 0; cnt < sizeof databases; ++cnt)
190 if (strcmp (dbname, databases[cnt].name) == 0)
191 break;
193 if (cnt == sizeof databases)
195 errno = EINVAL;
196 return -1;
199 /* Test whether it is really used. */
200 if (databases[cnt].dbp == NULL)
201 /* Nothing to do, but we could do. */
202 return 0;
204 /* Try to generate new data. */
205 new_db = nss_parse_service_list (service_line);
206 if (new_db == NULL)
208 /* Illegal service specification. */
209 errno = EINVAL;
210 return -1;
213 /* Prevent multiple threads to change the service table. */
214 __libc_lock_lock (lock);
216 /* Install new rules. */
217 *databases[cnt].dbp = new_db;
219 __libc_lock_unlock (lock);
221 return 0;
225 static int
226 nss_dlerror_run (void (*operate) (void))
228 const char *last_errstring = NULL;
229 const char *last_object_name = NULL;
231 (void) _dl_catch_error (&last_errstring, &last_object_name, operate);
233 return last_errstring != NULL;
237 /* Comparison function for searching NI->known tree. */
238 static int
239 known_compare (const void *p1, const void *p2)
241 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
242 *(const char *const *) p2);
246 static void *
247 nss_lookup_function (service_user *ni, const char *fct_name)
249 void **found, *result;
251 /* We now modify global data. Protect it. */
252 __libc_lock_lock (lock);
254 /* Search the tree of functions previously requested. Data in the
255 tree are `known_function' structures, whose first member is a
256 `const char *', the lookup key. The search returns a pointer to
257 the tree node structure; the first member of the is a pointer to
258 our structure (i.e. what will be a `known_function'); since the
259 first member of that is the lookup key string, &FCT_NAME is close
260 enough to a pointer to our structure to use as a lookup key that
261 will be passed to `known_compare' (above). */
263 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
264 if (*found != &fct_name)
265 /* The search found an existing structure in the tree. */
266 result = ((known_function *) *found)->fct_ptr;
267 else
269 /* This name was not known before. Now we have a node in the tree
270 (in the proper sorted position for FCT_NAME) that points to
271 &FCT_NAME instead of any real `known_function' structure.
272 Allocate a new structure and fill it in. */
274 known_function *known = malloc (sizeof *known);
275 if (! known)
277 remove_from_tree:
278 /* Oops. We can't instantiate this node properly.
279 Remove it from the tree. */
280 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
281 result = NULL;
283 else
285 /* Point the tree node at this new structure. */
286 *found = known;
287 known->fct_name = fct_name;
289 if (ni->library == NULL)
291 /* This service has not yet been used. Fetch the service
292 library for it, creating a new one if need be. If there
293 is no service table from the file, this static variable
294 holds the head of the service_library list made from the
295 default configuration. */
296 static name_database default_table;
297 ni->library = nss_new_service (service_table ?: &default_table,
298 ni->name);
299 if (ni->library == NULL)
301 /* This only happens when out of memory. */
302 free (known);
303 goto remove_from_tree;
307 if (ni->library->lib_handle == NULL)
309 /* Load the shared library. */
310 size_t shlen = (7 + strlen (ni->library->name) + 3
311 + sizeof (NSS_SHLIB_REVISION));
312 char shlib_name[shlen];
314 void do_open (void)
316 /* Open and relocate the shared object. */
317 ni->library->lib_handle = _dl_open (shlib_name, RTLD_LAZY);
320 /* Construct shared object name. */
321 __stpcpy (__stpcpy (__stpcpy (shlib_name, "libnss_"),
322 ni->library->name),
323 ".so" NSS_SHLIB_REVISION);
325 if (nss_dlerror_run (do_open) != 0)
326 /* Failed to load the library. */
327 ni->library->lib_handle = (void *) -1;
330 if (ni->library->lib_handle == (void *) -1)
331 /* Library not found => function not found. */
332 result = NULL;
333 else
335 /* Get the desired function. Again, GNU ld.so magic ahead. */
336 size_t namlen = (5 + strlen (ni->library->name) + 1
337 + strlen (fct_name) + 1);
338 char name[namlen];
339 struct link_map *map = ni->library->lib_handle;
340 ElfW(Addr) loadbase;
341 const ElfW(Sym) *ref = NULL;
342 void get_sym (void)
344 struct link_map *scope[2] = { map, NULL };
345 loadbase = _dl_lookup_symbol (name, &ref,
346 scope, map->l_name, 0, 0);
349 /* Construct the function name. */
350 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
351 ni->library->name),
352 "_"),
353 fct_name);
355 /* Look up the symbol. */
356 result = (nss_dlerror_run (get_sym)
357 ? NULL : (void *) (loadbase + ref->st_value));
360 /* Remember function pointer for later calls. Even if null, we
361 record it so a second try needn't search the library again. */
362 known->fct_ptr = result;
366 /* Remove the lock. */
367 __libc_lock_unlock (lock);
369 return result;
373 static name_database *
374 nss_parse_file (const char *fname)
376 FILE *fp;
377 name_database *result;
378 name_database_entry *last;
379 char *line;
380 size_t len;
382 /* Open the configuration file. */
383 fp = fopen (fname, "r");
384 if (fp == NULL)
385 return NULL;
387 result = (name_database *) malloc (sizeof (name_database));
388 if (result == NULL)
389 return NULL;
391 result->entry = NULL;
392 result->library = NULL;
393 last = NULL;
394 line = NULL;
395 len = 0;
398 name_database_entry *this;
399 ssize_t n;
400 char *cp;
402 n = __getline (&line, &len, fp);
403 if (n < 0)
404 break;
405 if (line[n - 1] == '\n')
406 line[n - 1] = '\0';
408 /* Because the file format does not know any form of quoting we
409 can search forward for the next '#' character and if found
410 make it terminating the line. */
411 cp = strchr (line, '#');
412 if (cp != NULL)
413 *cp = '\0';
415 /* If the line is blank it is ignored. */
416 if (line[0] == '\0')
417 continue;
419 /* Each line completely specifies the actions for a database. */
420 this = nss_getline (line);
421 if (this != NULL)
423 if (last != NULL)
424 last->next = this;
425 else
426 result->entry = this;
428 last = this;
431 while (!feof (fp));
433 /* Free the buffer. */
434 free (line);
435 /* Close configuration file. */
436 fclose (fp);
438 return result;
442 /* Read the source names:
443 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
445 static service_user *
446 nss_parse_service_list (const char *line)
448 service_user *result = NULL, **nextp = &result;
450 while (1)
452 service_user *new_service;
453 const char *name;
455 while (isspace (line[0]))
456 ++line;
457 if (line[0] == '\0')
458 /* No source specified. */
459 return result;
461 /* Read <source> identifier. */
462 name = line;
463 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
464 ++line;
465 if (name == line)
466 return result;
469 new_service = (service_user *) malloc (sizeof (service_user));
470 if (new_service == NULL)
471 return result;
472 else
474 char *source = (char *) malloc (line - name + 1);
475 if (source == NULL)
477 free (new_service);
478 return result;
480 memcpy (source, name, line - name);
481 source[line - name] = '\0';
483 new_service->name = source;
486 /* Set default actions. */
487 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
488 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
489 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
490 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
491 new_service->library = NULL;
492 new_service->known = NULL;
493 new_service->next = NULL;
495 while (isspace (line[0]))
496 ++line;
498 if (line[0] == '[')
500 /* Read criterions. */
502 ++line;
503 while (line[0] != '\0' && isspace (line[0]));
507 int not;
508 enum nss_status status;
509 lookup_actions action;
511 /* Grok ! before name to mean all statii but that one. */
512 if (not = line[0] == '!')
513 ++line;
515 /* Read status name. */
516 name = line;
517 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
518 && line[0] != ']')
519 ++line;
521 /* Compare with known statii. */
522 if (line - name == 7)
524 if (__strncasecmp (name, "SUCCESS", 7) == 0)
525 status = NSS_STATUS_SUCCESS;
526 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
527 status = NSS_STATUS_UNAVAIL;
528 else
529 return result;
531 else if (line - name == 8)
533 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
534 status = NSS_STATUS_NOTFOUND;
535 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
536 status = NSS_STATUS_TRYAGAIN;
537 else
538 return result;
540 else
541 return result;
543 while (isspace (line[0]))
544 ++line;
545 if (line[0] != '=')
546 return result;
548 ++line;
549 while (isspace (line[0]));
551 name = line;
552 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
553 && line[0] != ']')
554 ++line;
556 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
557 action = NSS_ACTION_RETURN;
558 else if (line - name == 8
559 && __strncasecmp (name, "CONTINUE", 8) == 0)
560 action = NSS_ACTION_CONTINUE;
561 else
562 return result;
564 if (not)
566 /* Save the current action setting for this status,
567 set them all to the given action, and reset this one. */
568 const lookup_actions save = new_service->actions[2 + status];
569 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
570 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
571 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
572 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
573 new_service->actions[2 + status] = save;
575 else
576 new_service->actions[2 + status] = action;
578 /* Skip white spaces. */
579 while (isspace (line[0]))
580 ++line;
582 while (line[0] != ']');
584 /* Skip the ']'. */
585 ++line;
588 *nextp = new_service;
589 nextp = &new_service->next;
593 static name_database_entry *
594 nss_getline (char *line)
596 const char *name;
597 name_database_entry *result;
599 /* Ignore leading white spaces. ATTENTION: this is different from
600 what is implemented in Solaris. The Solaris man page says a line
601 beginning with a white space character is ignored. We regard
602 this as just another misfeature in Solaris. */
603 while (isspace (line[0]))
604 ++line;
606 /* Recognize `<database> ":"'. */
607 name = line;
608 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
609 ++line;
610 if (line[0] == '\0' || name == line)
611 /* Syntax error. */
612 return NULL;
613 *line++ = '\0';
615 result = (name_database_entry *) malloc (sizeof (name_database_entry));
616 if (result == NULL)
617 return NULL;
619 /* Save the database name. */
621 const size_t len = strlen (name) + 1;
622 char *new = malloc (len);
623 if (new == NULL)
625 free (result);
626 return NULL;
628 result->name = memcpy (new, name, len);
631 /* Parse the list of services. */
632 result->service = nss_parse_service_list (line);
634 result->next = NULL;
635 return result;
639 static service_library *
640 nss_new_service (name_database *database, const char *name)
642 service_library **currentp = &database->library;
644 while (*currentp != NULL)
646 if (strcmp ((*currentp)->name, name) == 0)
647 return *currentp;
648 currentp = &(*currentp)->next;
651 /* We have to add the new service. */
652 *currentp = (service_library *) malloc (sizeof (service_library));
653 if (*currentp == NULL)
654 return NULL;
656 (*currentp)->name = name;
657 (*currentp)->lib_handle = NULL;
658 (*currentp)->next = NULL;
660 return *currentp;