(M68*:*:R3V[567]*:*): Use uppercase 'M'.
[glibc.git] / nss / nsswitch.c
blob1b061fa3cd1225bdc63b72671021a4f3d6486e19
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 not,
17 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 <link.h> /* We need some help from ld.so. */
26 #include <search.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "nsswitch.h"
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 *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. */
85 if (*ni != NULL)
87 __libc_lock_unlock (lock);
88 return 0;
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;
107 if (*ni == NULL && alternate_name != NULL)
108 /* We haven't found a an entry so far. Try to find it with
109 the alternative name. */
110 for (entry = service_table->entry; entry != NULL; entry = entry->next)
111 if (strcmp (alternate_name, entry->name) == 0)
112 *ni = entry->service;
115 /* No configuration data is available, either because nsswitch.conf
116 doesn't exist or because it doesn't has a line for this database.
118 DEFCONFIG specifies the default service list for this database,
119 or null to use the most common default. */
120 if (*ni == NULL)
121 *ni = nss_parse_service_list (defconfig
122 ?: "nis [NOTFOUND=return] files");
124 __libc_lock_unlock (lock);
126 return 0;
130 /* -1 == not found
131 0 == adjusted for next function */
133 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
135 *fctp = nss_lookup_function (*ni, fct_name);
137 while (*fctp == NULL
138 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
139 && (*ni)->next != NULL)
141 *ni = (*ni)->next;
143 *fctp = nss_lookup_function (*ni, fct_name);
146 return *fctp != NULL ? 0 : -1;
150 /* -1 == not found
151 0 == adjusted for next function
152 1 == finished */
154 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
155 int all_values)
157 if (all_values)
159 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
160 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
161 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
162 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
163 return 1;
165 else
167 /* This is really only for debugging. */
168 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
169 __libc_fatal ("illegal status in " __FUNCTION__);
171 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
172 return 1;
175 if ((*ni)->next == NULL)
176 return -1;
180 *ni = (*ni)->next;
182 *fctp = nss_lookup_function (*ni, fct_name);
184 while (*fctp == NULL
185 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
186 && (*ni)->next != NULL);
188 return *fctp != NULL ? 0 : -1;
193 __nss_configure_lookup (const char *dbname, const char *service_line)
195 service_user *new_db;
196 size_t cnt;
198 for (cnt = 0; cnt < sizeof databases; ++cnt)
200 int cmp = strcmp (dbname, databases[cnt].name);
201 if (cmp == 0)
202 break;
203 if (cmp > 0)
205 __set_errno (EINVAL);
206 return -1;
210 if (cnt == sizeof databases)
212 __set_errno (EINVAL);
213 return -1;
216 /* Test whether it is really used. */
217 if (databases[cnt].dbp == NULL)
218 /* Nothing to do, but we could do. */
219 return 0;
221 /* Try to generate new data. */
222 new_db = nss_parse_service_list (service_line);
223 if (new_db == NULL)
225 /* Illegal service specification. */
226 __set_errno (EINVAL);
227 return -1;
230 /* Prevent multiple threads to change the service table. */
231 __libc_lock_lock (lock);
233 /* Install new rules. */
234 *databases[cnt].dbp = new_db;
236 __libc_lock_unlock (lock);
238 return 0;
242 static int
243 nss_dlerror_run (void (*operate) (void))
245 char *last_errstring = NULL;
246 const char *last_object_name = NULL;
247 int result;
249 (void) _dl_catch_error (&last_errstring, &last_object_name, operate);
251 result = last_errstring != NULL;
252 if (result)
253 free (last_errstring);
255 return result;
259 /* Comparison function for searching NI->known tree. */
260 static int
261 known_compare (const void *p1, const void *p2)
263 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
264 *(const char *const *) p2);
268 static void *
269 nss_lookup_function (service_user *ni, const char *fct_name)
271 void **found, *result;
273 /* We now modify global data. Protect it. */
274 __libc_lock_lock (lock);
276 /* Search the tree of functions previously requested. Data in the
277 tree are `known_function' structures, whose first member is a
278 `const char *', the lookup key. The search returns a pointer to
279 the tree node structure; the first member of the is a pointer to
280 our structure (i.e. what will be a `known_function'); since the
281 first member of that is the lookup key string, &FCT_NAME is close
282 enough to a pointer to our structure to use as a lookup key that
283 will be passed to `known_compare' (above). */
285 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
286 if (*found != &fct_name)
287 /* The search found an existing structure in the tree. */
288 result = ((known_function *) *found)->fct_ptr;
289 else
291 /* This name was not known before. Now we have a node in the tree
292 (in the proper sorted position for FCT_NAME) that points to
293 &FCT_NAME instead of any real `known_function' structure.
294 Allocate a new structure and fill it in. */
296 known_function *known = malloc (sizeof *known);
297 if (! known)
299 remove_from_tree:
300 /* Oops. We can't instantiate this node properly.
301 Remove it from the tree. */
302 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
303 result = NULL;
305 else
307 /* Point the tree node at this new structure. */
308 *found = known;
309 known->fct_name = fct_name;
311 if (ni->library == NULL)
313 /* This service has not yet been used. Fetch the service
314 library for it, creating a new one if need be. If there
315 is no service table from the file, this static variable
316 holds the head of the service_library list made from the
317 default configuration. */
318 static name_database default_table;
319 ni->library = nss_new_service (service_table ?: &default_table,
320 ni->name);
321 if (ni->library == NULL)
323 /* This only happens when out of memory. */
324 free (known);
325 goto remove_from_tree;
329 if (ni->library->lib_handle == NULL)
331 /* Load the shared library. */
332 size_t shlen = (7 + strlen (ni->library->name) + 3
333 + sizeof (NSS_SHLIB_REVISION));
334 char shlib_name[shlen];
336 void do_open (void)
338 /* Open and relocate the shared object. */
339 ni->library->lib_handle = _dl_open (shlib_name, RTLD_LAZY);
342 /* Construct shared object name. */
343 __stpcpy (__stpcpy (__stpcpy (shlib_name, "libnss_"),
344 ni->library->name),
345 ".so" NSS_SHLIB_REVISION);
347 if (nss_dlerror_run (do_open) != 0)
348 /* Failed to load the library. */
349 ni->library->lib_handle = (void *) -1l;
352 if (ni->library->lib_handle == (void *) -1l)
353 /* Library not found => function not found. */
354 result = NULL;
355 else
357 /* Get the desired function. Again, GNU ld.so magic ahead. */
358 size_t namlen = (5 + strlen (ni->library->name) + 1
359 + strlen (fct_name) + 1);
360 char name[namlen];
361 struct link_map *map = ni->library->lib_handle;
362 ElfW(Addr) loadbase;
363 const ElfW(Sym) *ref = NULL;
364 void get_sym (void)
366 struct link_map *scope[2] = { map, NULL };
367 loadbase = _dl_lookup_symbol (name, &ref,
368 scope, map->l_name, 0);
371 /* Construct the function name. */
372 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
373 ni->library->name),
374 "_"),
375 fct_name);
377 /* Look up the symbol. */
378 result = (nss_dlerror_run (get_sym)
379 ? NULL : (void *) (loadbase + ref->st_value));
382 /* Remember function pointer for later calls. Even if null, we
383 record it so a second try needn't search the library again. */
384 known->fct_ptr = result;
388 /* Remove the lock. */
389 __libc_lock_unlock (lock);
391 return result;
395 static name_database *
396 nss_parse_file (const char *fname)
398 FILE *fp;
399 name_database *result;
400 name_database_entry *last;
401 char *line;
402 size_t len;
404 /* Open the configuration file. */
405 fp = fopen (fname, "r");
406 if (fp == NULL)
407 return NULL;
409 result = (name_database *) malloc (sizeof (name_database));
410 if (result == NULL)
411 return NULL;
413 result->entry = NULL;
414 result->library = NULL;
415 last = NULL;
416 line = NULL;
417 len = 0;
420 name_database_entry *this;
421 ssize_t n;
422 char *cp;
424 n = __getline (&line, &len, fp);
425 if (n < 0)
426 break;
427 if (line[n - 1] == '\n')
428 line[n - 1] = '\0';
430 /* Because the file format does not know any form of quoting we
431 can search forward for the next '#' character and if found
432 make it terminating the line. */
433 cp = strchr (line, '#');
434 if (cp != NULL)
435 *cp = '\0';
437 /* If the line is blank it is ignored. */
438 if (line[0] == '\0')
439 continue;
441 /* Each line completely specifies the actions for a database. */
442 this = nss_getline (line);
443 if (this != NULL)
445 if (last != NULL)
446 last->next = this;
447 else
448 result->entry = this;
450 last = this;
453 while (!feof (fp));
455 /* Free the buffer. */
456 free (line);
457 /* Close configuration file. */
458 fclose (fp);
460 return result;
464 /* Read the source names:
465 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
467 static service_user *
468 nss_parse_service_list (const char *line)
470 service_user *result = NULL, **nextp = &result;
472 while (1)
474 service_user *new_service;
475 const char *name;
477 while (isspace (line[0]))
478 ++line;
479 if (line[0] == '\0')
480 /* No source specified. */
481 return result;
483 /* Read <source> identifier. */
484 name = line;
485 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
486 ++line;
487 if (name == line)
488 return result;
491 new_service = (service_user *) malloc (sizeof (service_user));
492 if (new_service == NULL)
493 return result;
494 else
496 char *source = (char *) malloc (line - name + 1);
497 if (source == NULL)
499 free (new_service);
500 return result;
502 memcpy (source, name, line - name);
503 source[line - name] = '\0';
505 new_service->name = source;
508 /* Set default actions. */
509 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
510 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
511 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
512 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
513 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
514 new_service->library = NULL;
515 new_service->known = NULL;
516 new_service->next = NULL;
518 while (isspace (line[0]))
519 ++line;
521 if (line[0] == '[')
523 /* Read criterions. */
525 ++line;
526 while (line[0] != '\0' && isspace (line[0]));
530 int not;
531 enum nss_status status;
532 lookup_actions action;
534 /* Grok ! before name to mean all statii but that one. */
535 if (not = line[0] == '!')
536 ++line;
538 /* Read status name. */
539 name = line;
540 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
541 && line[0] != ']')
542 ++line;
544 /* Compare with known statii. */
545 if (line - name == 7)
547 if (__strncasecmp (name, "SUCCESS", 7) == 0)
548 status = NSS_STATUS_SUCCESS;
549 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
550 status = NSS_STATUS_UNAVAIL;
551 else
552 return result;
554 else if (line - name == 8)
556 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
557 status = NSS_STATUS_NOTFOUND;
558 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
559 status = NSS_STATUS_TRYAGAIN;
560 else
561 return result;
563 else
564 return result;
566 while (isspace (line[0]))
567 ++line;
568 if (line[0] != '=')
569 return result;
571 ++line;
572 while (isspace (line[0]));
574 name = line;
575 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
576 && line[0] != ']')
577 ++line;
579 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
580 action = NSS_ACTION_RETURN;
581 else if (line - name == 8
582 && __strncasecmp (name, "CONTINUE", 8) == 0)
583 action = NSS_ACTION_CONTINUE;
584 else
585 return result;
587 if (not)
589 /* Save the current action setting for this status,
590 set them all to the given action, and reset this one. */
591 const lookup_actions save = new_service->actions[2 + status];
592 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
593 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
594 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
595 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
596 new_service->actions[2 + status] = save;
598 else
599 new_service->actions[2 + status] = action;
601 /* Skip white spaces. */
602 while (isspace (line[0]))
603 ++line;
605 while (line[0] != ']');
607 /* Skip the ']'. */
608 ++line;
611 *nextp = new_service;
612 nextp = &new_service->next;
616 static name_database_entry *
617 nss_getline (char *line)
619 const char *name;
620 name_database_entry *result;
622 /* Ignore leading white spaces. ATTENTION: this is different from
623 what is implemented in Solaris. The Solaris man page says a line
624 beginning with a white space character is ignored. We regard
625 this as just another misfeature in Solaris. */
626 while (isspace (line[0]))
627 ++line;
629 /* Recognize `<database> ":"'. */
630 name = line;
631 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
632 ++line;
633 if (line[0] == '\0' || name == line)
634 /* Syntax error. */
635 return NULL;
636 *line++ = '\0';
638 result = (name_database_entry *) malloc (sizeof (name_database_entry));
639 if (result == NULL)
640 return NULL;
642 /* Save the database name. */
644 const size_t len = strlen (name) + 1;
645 char *new = malloc (len);
646 if (new == NULL)
648 free (result);
649 return NULL;
651 result->name = memcpy (new, name, len);
654 /* Parse the list of services. */
655 result->service = nss_parse_service_list (line);
657 result->next = NULL;
658 return result;
662 static service_library *
663 nss_new_service (name_database *database, const char *name)
665 service_library **currentp = &database->library;
667 while (*currentp != NULL)
669 if (strcmp ((*currentp)->name, name) == 0)
670 return *currentp;
671 currentp = &(*currentp)->next;
674 /* We have to add the new service. */
675 *currentp = (service_library *) malloc (sizeof (service_library));
676 if (*currentp == NULL)
677 return NULL;
679 (*currentp)->name = name;
680 (*currentp)->lib_handle = NULL;
681 (*currentp)->next = NULL;
683 return *currentp;