* sysdeps/m68k/fpu/bits/mathinline.h (isgreater, isgreaterequal)
[glibc.git] / nss / nsswitch.c
blobca411b2cdc6fdef13092de59341265b16ab8399a
1 /* Copyright (C) 1996,1997,1998,1999,2001,2002 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <netdb.h>
24 #include <bits/libc-lock.h>
25 #include <search.h>
26 #include <stdio.h>
27 #include <stdio_ext.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include <aliases.h>
32 #include <grp.h>
33 #include <netinet/ether.h>
34 #include <pwd.h>
35 #include <shadow.h>
37 #if !defined DO_STATIC_NSS || defined SHARED
38 # include <gnu/lib-names.h>
39 #endif
41 #include "nsswitch.h"
43 /* Prototypes for the local functions. */
44 static name_database *nss_parse_file (const char *fname) internal_function;
45 static name_database_entry *nss_getline (char *line) internal_function;
46 static service_user *nss_parse_service_list (const char *line)
47 internal_function;
48 static service_library *nss_new_service (name_database *database,
49 const char *name) internal_function;
52 /* Declare external database variables. */
53 #define DEFINE_DATABASE(name) \
54 extern service_user *__nss_##name##_database attribute_hidden; \
55 weak_extern (__nss_##name##_database)
56 #include "databases.def"
57 #undef DEFINE_DATABASE
59 /* Structure to map database name to variable. */
60 static struct
62 const char *name;
63 service_user **dbp;
64 } databases[] =
66 #define DEFINE_DATABASE(name) \
67 { #name, &__nss_##name##_database },
68 #include "databases.def"
69 #undef DEFINE_DATABASE
73 __libc_lock_define_initialized (static, lock)
75 #if !defined DO_STATIC_NSS || defined SHARED
76 /* String with revision number of the shared object files. */
77 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
78 #endif
80 /* The root of the whole data base. */
81 static name_database *service_table;
84 /* -1 == database not found
85 0 == database entry pointer stored */
86 int
87 __nss_database_lookup (const char *database, const char *alternate_name,
88 const char *defconfig, service_user **ni)
90 /* Prevent multiple threads to change the service table. */
91 __libc_lock_lock (lock);
93 /* Reconsider database variable in case some other thread called
94 `__nss_configure_lookup' while we waited for the lock. */
95 if (*ni != NULL)
97 __libc_lock_unlock (lock);
98 return 0;
101 /* Are we initialized yet? */
102 if (service_table == NULL)
103 /* Read config file. */
104 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
106 /* Test whether configuration data is available. */
107 if (service_table != NULL)
109 /* Return first `service_user' entry for DATABASE. */
110 name_database_entry *entry;
112 /* XXX Could use some faster mechanism here. But each database is
113 only requested once and so this might not be critical. */
114 for (entry = service_table->entry; entry != NULL; entry = entry->next)
115 if (strcmp (database, entry->name) == 0)
116 *ni = entry->service;
118 if (*ni == NULL && alternate_name != NULL)
119 /* We haven't found an entry so far. Try to find it with the
120 alternative name. */
121 for (entry = service_table->entry; entry != NULL; entry = entry->next)
122 if (strcmp (alternate_name, entry->name) == 0)
123 *ni = entry->service;
126 /* No configuration data is available, either because nsswitch.conf
127 doesn't exist or because it doesn't has a line for this database.
129 DEFCONFIG specifies the default service list for this database,
130 or null to use the most common default. */
131 if (*ni == NULL)
132 *ni = nss_parse_service_list (defconfig
133 ?: "nis [NOTFOUND=return] files");
135 __libc_lock_unlock (lock);
137 return 0;
139 libc_hidden_def (__nss_database_lookup)
142 /* -1 == not found
143 0 == function found
144 1 == finished */
146 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
148 *fctp = __nss_lookup_function (*ni, fct_name);
150 while (*fctp == NULL
151 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
152 && (*ni)->next != NULL)
154 *ni = (*ni)->next;
156 *fctp = __nss_lookup_function (*ni, fct_name);
159 return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
163 /* -1 == not found
164 0 == adjusted for next function
165 1 == finished */
167 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
168 int all_values)
170 if (all_values)
172 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
173 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
174 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
175 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
176 return 1;
178 else
180 /* This is really only for debugging. */
181 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
182 __libc_fatal ("illegal status in __nss_next");
184 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
185 return 1;
188 if ((*ni)->next == NULL)
189 return -1;
193 *ni = (*ni)->next;
195 *fctp = __nss_lookup_function (*ni, fct_name);
197 while (*fctp == NULL
198 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
199 && (*ni)->next != NULL);
201 return *fctp != NULL ? 0 : -1;
203 libc_hidden_def (__nss_next)
207 __nss_configure_lookup (const char *dbname, const char *service_line)
209 service_user *new_db;
210 size_t cnt;
212 for (cnt = 0; cnt < sizeof databases; ++cnt)
214 int cmp = strcmp (dbname, databases[cnt].name);
215 if (cmp == 0)
216 break;
217 if (cmp < 0)
219 __set_errno (EINVAL);
220 return -1;
224 if (cnt == sizeof databases)
226 __set_errno (EINVAL);
227 return -1;
230 /* Test whether it is really used. */
231 if (databases[cnt].dbp == NULL)
232 /* Nothing to do, but we could do. */
233 return 0;
235 /* Try to generate new data. */
236 new_db = nss_parse_service_list (service_line);
237 if (new_db == NULL)
239 /* Illegal service specification. */
240 __set_errno (EINVAL);
241 return -1;
244 /* Prevent multiple threads to change the service table. */
245 __libc_lock_lock (lock);
247 /* Install new rules. */
248 *databases[cnt].dbp = new_db;
250 __libc_lock_unlock (lock);
252 return 0;
256 /* Comparison function for searching NI->known tree. */
257 static int
258 known_compare (const void *p1, const void *p2)
260 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
261 *(const char *const *) p2);
265 void *
266 __nss_lookup_function (service_user *ni, const char *fct_name)
268 void **found, *result;
270 /* We now modify global data. Protect it. */
271 __libc_lock_lock (lock);
273 /* Search the tree of functions previously requested. Data in the
274 tree are `known_function' structures, whose first member is a
275 `const char *', the lookup key. The search returns a pointer to
276 the tree node structure; the first member of the is a pointer to
277 our structure (i.e. what will be a `known_function'); since the
278 first member of that is the lookup key string, &FCT_NAME is close
279 enough to a pointer to our structure to use as a lookup key that
280 will be passed to `known_compare' (above). */
282 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
283 if (*found != &fct_name)
284 /* The search found an existing structure in the tree. */
285 result = ((known_function *) *found)->fct_ptr;
286 else
288 /* This name was not known before. Now we have a node in the tree
289 (in the proper sorted position for FCT_NAME) that points to
290 &FCT_NAME instead of any real `known_function' structure.
291 Allocate a new structure and fill it in. */
293 known_function *known = malloc (sizeof *known);
294 if (! known)
296 remove_from_tree:
297 /* Oops. We can't instantiate this node properly.
298 Remove it from the tree. */
299 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
300 result = NULL;
302 else
304 /* Point the tree node at this new structure. */
305 *found = known;
306 known->fct_name = fct_name;
308 if (ni->library == NULL)
310 /* This service has not yet been used. Fetch the service
311 library for it, creating a new one if need be. If there
312 is no service table from the file, this static variable
313 holds the head of the service_library list made from the
314 default configuration. */
315 static name_database default_table;
316 ni->library = nss_new_service (service_table ?: &default_table,
317 ni->name);
318 if (ni->library == NULL)
320 /* This only happens when out of memory. */
321 free (known);
322 goto remove_from_tree;
326 #if !defined DO_STATIC_NSS || defined SHARED
327 if (ni->library->lib_handle == NULL)
329 /* Load the shared library. */
330 size_t shlen = (7 + strlen (ni->library->name) + 3
331 + strlen (__nss_shlib_revision) + 1);
332 int saved_errno = errno;
333 char shlib_name[shlen];
335 /* Construct shared object name. */
336 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name,
337 "libnss_"),
338 ni->library->name),
339 ".so"),
340 __nss_shlib_revision);
342 ni->library->lib_handle = __libc_dlopen (shlib_name);
343 if (ni->library->lib_handle == NULL)
345 /* Failed to load the library. */
346 ni->library->lib_handle = (void *) -1l;
347 __set_errno (saved_errno);
351 if (ni->library->lib_handle == (void *) -1l)
352 /* Library not found => function not found. */
353 result = NULL;
354 else
356 /* Get the desired function. */
357 size_t namlen = (5 + strlen (ni->library->name) + 1
358 + strlen (fct_name) + 1);
359 char name[namlen];
361 /* Construct the function name. */
362 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
363 ni->library->name),
364 "_"),
365 fct_name);
367 /* Look up the symbol. */
368 result = __libc_dlsym (ni->library->lib_handle, name);
370 #else
371 /* We can't get function address dynamically in static linking. */
373 # define DEFINE_ENT(h,nm) \
374 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
375 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
376 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
377 # define DEFINE_GET(h,nm) \
378 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
379 # define DEFINE_GETBY(h,nm,ky) \
380 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
381 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
383 # include "function.def"
384 { NULL, NULL }
386 size_t namlen = (5 + strlen (ni->library->name) + 1
387 + strlen (fct_name) + 1);
388 char name[namlen];
390 /* Construct the function name. */
391 __stpcpy (__stpcpy (__stpcpy (name, ni->library->name),
392 "_"),
393 fct_name);
395 result = NULL;
396 for (tp = &tbl[0]; tp->fname; tp++)
397 if (strcmp (tp->fname, name) == 0)
399 result = tp->fp;
400 break;
403 #endif
405 /* Remember function pointer for later calls. Even if null, we
406 record it so a second try needn't search the library again. */
407 known->fct_ptr = result;
411 /* Remove the lock. */
412 __libc_lock_unlock (lock);
414 return result;
418 static name_database *
419 internal_function
420 nss_parse_file (const char *fname)
422 FILE *fp;
423 name_database *result;
424 name_database_entry *last;
425 char *line;
426 size_t len;
428 /* Open the configuration file. */
429 fp = fopen (fname, "r");
430 if (fp == NULL)
431 return NULL;
433 /* No threads use this stream. */
434 __fsetlocking (fp, FSETLOCKING_BYCALLER);
436 result = (name_database *) malloc (sizeof (name_database));
437 if (result == NULL)
438 return NULL;
440 result->entry = NULL;
441 result->library = NULL;
442 last = NULL;
443 line = NULL;
444 len = 0;
447 name_database_entry *this;
448 ssize_t n;
450 n = __getline (&line, &len, fp);
451 if (n < 0)
452 break;
453 if (line[n - 1] == '\n')
454 line[n - 1] = '\0';
456 /* Because the file format does not know any form of quoting we
457 can search forward for the next '#' character and if found
458 make it terminating the line. */
459 *__strchrnul (line, '#') = '\0';
461 /* If the line is blank it is ignored. */
462 if (line[0] == '\0')
463 continue;
465 /* Each line completely specifies the actions for a database. */
466 this = nss_getline (line);
467 if (this != NULL)
469 if (last != NULL)
470 last->next = this;
471 else
472 result->entry = this;
474 last = this;
477 while (!feof_unlocked (fp));
479 /* Free the buffer. */
480 free (line);
481 /* Close configuration file. */
482 fclose (fp);
484 return result;
488 /* Read the source names:
489 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
491 static service_user *
492 internal_function
493 nss_parse_service_list (const char *line)
495 service_user *result = NULL, **nextp = &result;
497 while (1)
499 service_user *new_service;
500 const char *name;
502 while (isspace (line[0]))
503 ++line;
504 if (line[0] == '\0')
505 /* No source specified. */
506 return result;
508 /* Read <source> identifier. */
509 name = line;
510 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
511 ++line;
512 if (name == line)
513 return result;
516 new_service = (service_user *) malloc (sizeof (service_user)
517 + (line - name + 1));
518 if (new_service == NULL)
519 return result;
521 *((char *) __mempcpy (new_service->name, name, line - name)) = '\0';
523 /* Set default actions. */
524 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
525 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
526 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
527 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
528 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
529 new_service->library = NULL;
530 new_service->known = NULL;
531 new_service->next = NULL;
533 while (isspace (line[0]))
534 ++line;
536 if (line[0] == '[')
538 /* Read criterions. */
540 ++line;
541 while (line[0] != '\0' && isspace (line[0]));
545 int not;
546 enum nss_status status;
547 lookup_actions action;
549 /* Grok ! before name to mean all statii but that one. */
550 not = line[0] == '!';
551 if (not)
552 ++line;
554 /* Read status name. */
555 name = line;
556 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
557 && line[0] != ']')
558 ++line;
560 /* Compare with known statii. */
561 if (line - name == 7)
563 if (__strncasecmp (name, "SUCCESS", 7) == 0)
564 status = NSS_STATUS_SUCCESS;
565 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
566 status = NSS_STATUS_UNAVAIL;
567 else
568 return result;
570 else if (line - name == 8)
572 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
573 status = NSS_STATUS_NOTFOUND;
574 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
575 status = NSS_STATUS_TRYAGAIN;
576 else
577 return result;
579 else
580 return result;
582 while (isspace (line[0]))
583 ++line;
584 if (line[0] != '=')
585 return result;
587 ++line;
588 while (isspace (line[0]));
590 name = line;
591 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
592 && line[0] != ']')
593 ++line;
595 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
596 action = NSS_ACTION_RETURN;
597 else if (line - name == 8
598 && __strncasecmp (name, "CONTINUE", 8) == 0)
599 action = NSS_ACTION_CONTINUE;
600 else
601 return result;
603 if (not)
605 /* Save the current action setting for this status,
606 set them all to the given action, and reset this one. */
607 const lookup_actions save = new_service->actions[2 + status];
608 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
609 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
610 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
611 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
612 new_service->actions[2 + status] = save;
614 else
615 new_service->actions[2 + status] = action;
617 /* Skip white spaces. */
618 while (isspace (line[0]))
619 ++line;
621 while (line[0] != ']');
623 /* Skip the ']'. */
624 ++line;
627 *nextp = new_service;
628 nextp = &new_service->next;
632 static name_database_entry *
633 internal_function
634 nss_getline (char *line)
636 const char *name;
637 name_database_entry *result;
638 size_t len;
640 /* Ignore leading white spaces. ATTENTION: this is different from
641 what is implemented in Solaris. The Solaris man page says a line
642 beginning with a white space character is ignored. We regard
643 this as just another misfeature in Solaris. */
644 while (isspace (line[0]))
645 ++line;
647 /* Recognize `<database> ":"'. */
648 name = line;
649 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
650 ++line;
651 if (line[0] == '\0' || name == line)
652 /* Syntax error. */
653 return NULL;
654 *line++ = '\0';
656 len = strlen (name) + 1;
658 result = (name_database_entry *) malloc (sizeof (name_database_entry) + len);
659 if (result == NULL)
660 return NULL;
662 /* Save the database name. */
663 memcpy (result->name, name, len);
665 /* Parse the list of services. */
666 result->service = nss_parse_service_list (line);
668 result->next = NULL;
669 return result;
673 static service_library *
674 internal_function
675 nss_new_service (name_database *database, const char *name)
677 service_library **currentp = &database->library;
679 while (*currentp != NULL)
681 if (strcmp ((*currentp)->name, name) == 0)
682 return *currentp;
683 currentp = &(*currentp)->next;
686 /* We have to add the new service. */
687 *currentp = (service_library *) malloc (sizeof (service_library));
688 if (*currentp == NULL)
689 return NULL;
691 (*currentp)->name = name;
692 (*currentp)->lib_handle = NULL;
693 (*currentp)->next = NULL;
695 return *currentp;
699 /* Free all resources if necessary. */
700 static void __attribute__ ((unused))
701 free_mem (void)
703 name_database *top = service_table;
704 name_database_entry *entry;
705 service_library *library;
707 if (top == NULL)
708 /* Maybe we have not read the nsswitch.conf file. */
709 return;
711 /* Don't disturb ongoing other threads (if there are any). */
712 service_table = NULL;
714 entry = top->entry;
715 while (entry != NULL)
717 name_database_entry *olde = entry;
718 service_user *service = entry->service;
720 while (service != NULL)
722 service_user *olds = service;
724 if (service->known != NULL)
725 __tdestroy (service->known, free);
727 service = service->next;
728 free (olds);
731 entry = entry->next;
732 free (olde);
735 library = top->library;
736 while (library != NULL)
738 service_library *oldl = library;
740 __libc_dlclose (library->lib_handle);
742 library = library->next;
743 free (oldl);
746 free (top);
749 text_set_element (__libc_subfreeres, free_mem);