]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/nsswitch.c
Fix lots of bitrot for stub configurations.
[thirdparty/glibc.git] / nss / nsswitch.c
1 /* Copyright (C) 1996-2012 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <ctype.h>
20 #include <dlfcn.h>
21 #include <errno.h>
22 #include <netdb.h>
23 #include <bits/libc-lock.h>
24 #include <search.h>
25 #include <stdio.h>
26 #include <stdio_ext.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <aliases.h>
31 #include <grp.h>
32 #include <netinet/ether.h>
33 #include <pwd.h>
34 #include <shadow.h>
35
36 #if !defined DO_STATIC_NSS || defined SHARED
37 # include <gnu/lib-names.h>
38 #endif
39
40 #include "nsswitch.h"
41 #include "../nscd/nscd_proto.h"
42 #include <sysdep.h>
43
44 /* Prototypes for the local functions. */
45 static name_database *nss_parse_file (const char *fname) internal_function;
46 static name_database_entry *nss_getline (char *line) internal_function;
47 static service_user *nss_parse_service_list (const char *line)
48 internal_function;
49 static service_library *nss_new_service (name_database *database,
50 const char *name) internal_function;
51
52
53 /* Declare external database variables. */
54 #define DEFINE_DATABASE(name) \
55 extern service_user *__nss_##name##_database attribute_hidden; \
56 weak_extern (__nss_##name##_database)
57 #include "databases.def"
58 #undef DEFINE_DATABASE
59
60 /* Structure to map database name to variable. */
61 static const struct
62 {
63 const char name[10];
64 service_user **dbp;
65 } databases[] =
66 {
67 #define DEFINE_DATABASE(name) \
68 { #name, &__nss_##name##_database },
69 #include "databases.def"
70 #undef DEFINE_DATABASE
71 };
72 #define ndatabases (sizeof (databases) / sizeof (databases[0]))
73
74 /* Flags whether custom rules for database is set. */
75 bool __nss_database_custom[NSS_DBSIDX_max];
76
77
78 __libc_lock_define_initialized (static, lock)
79
80 #if !defined DO_STATIC_NSS || defined SHARED
81 /* String with revision number of the shared object files. */
82 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
83 #endif
84
85 /* The root of the whole data base. */
86 static name_database *service_table;
87
88 /* List of default service lists that were generated by glibc because
89 /etc/nsswitch.conf did not provide a value.
90 The list is only maintained so we can free such service lists in
91 __libc_freeres. */
92 static name_database_entry *defconfig_entries;
93
94
95 /* Nonzero if this is the nscd process. */
96 static bool is_nscd;
97 /* The callback passed to the init functions when nscd is used. */
98 static void (*nscd_init_cb) (size_t, struct traced_file *);
99
100
101 /* -1 == database not found
102 0 == database entry pointer stored */
103 int
104 __nss_database_lookup (const char *database, const char *alternate_name,
105 const char *defconfig, service_user **ni)
106 {
107 /* Prevent multiple threads to change the service table. */
108 __libc_lock_lock (lock);
109
110 /* Reconsider database variable in case some other thread called
111 `__nss_configure_lookup' while we waited for the lock. */
112 if (*ni != NULL)
113 {
114 __libc_lock_unlock (lock);
115 return 0;
116 }
117
118 /* Are we initialized yet? */
119 if (service_table == NULL)
120 /* Read config file. */
121 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
122
123 /* Test whether configuration data is available. */
124 if (service_table != NULL)
125 {
126 /* Return first `service_user' entry for DATABASE. */
127 name_database_entry *entry;
128
129 /* XXX Could use some faster mechanism here. But each database is
130 only requested once and so this might not be critical. */
131 for (entry = service_table->entry; entry != NULL; entry = entry->next)
132 if (strcmp (database, entry->name) == 0)
133 *ni = entry->service;
134
135 if (*ni == NULL && alternate_name != NULL)
136 /* We haven't found an entry so far. Try to find it with the
137 alternative name. */
138 for (entry = service_table->entry; entry != NULL; entry = entry->next)
139 if (strcmp (alternate_name, entry->name) == 0)
140 *ni = entry->service;
141 }
142
143 /* No configuration data is available, either because nsswitch.conf
144 doesn't exist or because it doesn't have a line for this database.
145
146 DEFCONFIG specifies the default service list for this database,
147 or null to use the most common default. */
148 if (*ni == NULL)
149 {
150 *ni = nss_parse_service_list (defconfig
151 ?: "nis [NOTFOUND=return] files");
152 if (*ni != NULL)
153 {
154 /* Record the memory we've just allocated in defconfig_entries list,
155 so we can free it later. */
156 name_database_entry *entry;
157
158 /* Allocate ENTRY plus size of name (1 here). */
159 entry = (name_database_entry *) malloc (sizeof (*entry) + 1);
160
161 if (entry != NULL)
162 {
163 entry->next = defconfig_entries;
164 entry->service = *ni;
165 entry->name[0] = '\0';
166 defconfig_entries = entry;
167 }
168 }
169 }
170
171 __libc_lock_unlock (lock);
172
173 return *ni != NULL ? 0 : -1;
174 }
175 libc_hidden_def (__nss_database_lookup)
176
177
178 /* -1 == not found
179 0 == function found
180 1 == finished */
181 int
182 __nss_lookup (service_user **ni, const char *fct_name, const char *fct2_name,
183 void **fctp)
184 {
185 *fctp = __nss_lookup_function (*ni, fct_name);
186 if (*fctp == NULL && fct2_name != NULL)
187 *fctp = __nss_lookup_function (*ni, fct2_name);
188
189 while (*fctp == NULL
190 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
191 && (*ni)->next != NULL)
192 {
193 *ni = (*ni)->next;
194
195 *fctp = __nss_lookup_function (*ni, fct_name);
196 if (*fctp == NULL && fct2_name != NULL)
197 *fctp = __nss_lookup_function (*ni, fct2_name);
198 }
199
200 return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
201 }
202 libc_hidden_def (__nss_lookup)
203
204
205 /* -1 == not found
206 0 == adjusted for next function
207 1 == finished */
208 int
209 __nss_next2 (service_user **ni, const char *fct_name, const char *fct2_name,
210 void **fctp, int status, int all_values)
211 {
212 if (all_values)
213 {
214 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
215 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
216 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
217 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
218 return 1;
219 }
220 else
221 {
222 /* This is really only for debugging. */
223 if (__builtin_expect (NSS_STATUS_TRYAGAIN > status
224 || status > NSS_STATUS_RETURN, 0))
225 __libc_fatal ("illegal status in __nss_next");
226
227 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
228 return 1;
229 }
230
231 if ((*ni)->next == NULL)
232 return -1;
233
234 do
235 {
236 *ni = (*ni)->next;
237
238 *fctp = __nss_lookup_function (*ni, fct_name);
239 if (*fctp == NULL && fct2_name != NULL)
240 *fctp = __nss_lookup_function (*ni, fct2_name);
241 }
242 while (*fctp == NULL
243 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
244 && (*ni)->next != NULL);
245
246 return *fctp != NULL ? 0 : -1;
247 }
248 libc_hidden_def (__nss_next2)
249
250
251 int
252 attribute_compat_text_section
253 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
254 int all_values)
255 {
256 return __nss_next2 (ni, fct_name, NULL, fctp, status, all_values);
257 }
258
259
260 int
261 __nss_configure_lookup (const char *dbname, const char *service_line)
262 {
263 service_user *new_db;
264 size_t cnt;
265
266 for (cnt = 0; cnt < ndatabases; ++cnt)
267 {
268 int cmp = strcmp (dbname, databases[cnt].name);
269 if (cmp == 0)
270 break;
271 if (cmp < 0)
272 {
273 __set_errno (EINVAL);
274 return -1;
275 }
276 }
277
278 if (cnt == ndatabases)
279 {
280 __set_errno (EINVAL);
281 return -1;
282 }
283
284 /* Test whether it is really used. */
285 if (databases[cnt].dbp == NULL)
286 /* Nothing to do, but we could do. */
287 return 0;
288
289 /* Try to generate new data. */
290 new_db = nss_parse_service_list (service_line);
291 if (new_db == NULL)
292 {
293 /* Illegal service specification. */
294 __set_errno (EINVAL);
295 return -1;
296 }
297
298 /* Prevent multiple threads to change the service table. */
299 __libc_lock_lock (lock);
300
301 /* Install new rules. */
302 *databases[cnt].dbp = new_db;
303 __nss_database_custom[cnt] = true;
304
305 __libc_lock_unlock (lock);
306
307 return 0;
308 }
309
310
311 /* Comparison function for searching NI->known tree. */
312 static int
313 known_compare (const void *p1, const void *p2)
314 {
315 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
316 *(const char *const *) p2);
317 }
318
319
320 #if !defined DO_STATIC_NSS || defined SHARED
321 /* Load library. */
322 static int
323 nss_load_library (service_user *ni)
324 {
325 if (ni->library == NULL)
326 {
327 /* This service has not yet been used. Fetch the service
328 library for it, creating a new one if need be. If there
329 is no service table from the file, this static variable
330 holds the head of the service_library list made from the
331 default configuration. */
332 static name_database default_table;
333 ni->library = nss_new_service (service_table ?: &default_table,
334 ni->name);
335 if (ni->library == NULL)
336 return -1;
337 }
338
339 if (ni->library->lib_handle == NULL)
340 {
341 /* Load the shared library. */
342 size_t shlen = (7 + strlen (ni->name) + 3
343 + strlen (__nss_shlib_revision) + 1);
344 int saved_errno = errno;
345 char shlib_name[shlen];
346
347 /* Construct shared object name. */
348 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name,
349 "libnss_"),
350 ni->name),
351 ".so"),
352 __nss_shlib_revision);
353
354 ni->library->lib_handle = __libc_dlopen (shlib_name);
355 if (ni->library->lib_handle == NULL)
356 {
357 /* Failed to load the library. */
358 ni->library->lib_handle = (void *) -1l;
359 __set_errno (saved_errno);
360 }
361 else if (is_nscd)
362 {
363 /* Call the init function when nscd is used. */
364 size_t initlen = (5 + strlen (ni->name)
365 + strlen ("_init") + 1);
366 char init_name[initlen];
367
368 /* Construct the init function name. */
369 __stpcpy (__stpcpy (__stpcpy (init_name,
370 "_nss_"),
371 ni->name),
372 "_init");
373
374 /* Find the optional init function. */
375 void (*ifct) (void (*) (size_t, struct traced_file *))
376 = __libc_dlsym (ni->library->lib_handle, init_name);
377 if (ifct != NULL)
378 {
379 void (*cb) (size_t, struct traced_file *) = nscd_init_cb;
380 # ifdef PTR_DEMANGLE
381 PTR_DEMANGLE (cb);
382 # endif
383 ifct (cb);
384 }
385 }
386 }
387
388 return 0;
389 }
390 #endif
391
392
393 void *
394 __nss_lookup_function (service_user *ni, const char *fct_name)
395 {
396 void **found, *result;
397
398 /* We now modify global data. Protect it. */
399 __libc_lock_lock (lock);
400
401 /* Search the tree of functions previously requested. Data in the
402 tree are `known_function' structures, whose first member is a
403 `const char *', the lookup key. The search returns a pointer to
404 the tree node structure; the first member of the is a pointer to
405 our structure (i.e. what will be a `known_function'); since the
406 first member of that is the lookup key string, &FCT_NAME is close
407 enough to a pointer to our structure to use as a lookup key that
408 will be passed to `known_compare' (above). */
409
410 found = __tsearch (&fct_name, &ni->known, &known_compare);
411 if (found == NULL)
412 /* This means out-of-memory. */
413 result = NULL;
414 else if (*found != &fct_name)
415 {
416 /* The search found an existing structure in the tree. */
417 result = ((known_function *) *found)->fct_ptr;
418 #ifdef PTR_DEMANGLE
419 PTR_DEMANGLE (result);
420 #endif
421 }
422 else
423 {
424 /* This name was not known before. Now we have a node in the tree
425 (in the proper sorted position for FCT_NAME) that points to
426 &FCT_NAME instead of any real `known_function' structure.
427 Allocate a new structure and fill it in. */
428
429 known_function *known = malloc (sizeof *known);
430 if (! known)
431 {
432 remove_from_tree:
433 /* Oops. We can't instantiate this node properly.
434 Remove it from the tree. */
435 __tdelete (&fct_name, &ni->known, &known_compare);
436 free (known);
437 result = NULL;
438 }
439 else
440 {
441 /* Point the tree node at this new structure. */
442 *found = known;
443 known->fct_name = fct_name;
444
445 #if !defined DO_STATIC_NSS || defined SHARED
446 /* Load the appropriate library. */
447 if (nss_load_library (ni) != 0)
448 /* This only happens when out of memory. */
449 goto remove_from_tree;
450
451 if (ni->library->lib_handle == (void *) -1l)
452 /* Library not found => function not found. */
453 result = NULL;
454 else
455 {
456 /* Get the desired function. */
457 size_t namlen = (5 + strlen (ni->name) + 1
458 + strlen (fct_name) + 1);
459 char name[namlen];
460
461 /* Construct the function name. */
462 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
463 ni->name),
464 "_"),
465 fct_name);
466
467 /* Look up the symbol. */
468 result = __libc_dlsym (ni->library->lib_handle, name);
469 }
470 #else
471 /* We can't get function address dynamically in static linking. */
472 {
473 # define DEFINE_ENT(h,nm) \
474 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
475 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
476 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
477 # define DEFINE_GET(h,nm) \
478 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
479 # define DEFINE_GETBY(h,nm,ky) \
480 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
481 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
482 {
483 # include "function.def"
484 { NULL, NULL }
485 };
486 size_t namlen = (5 + strlen (ni->name) + 1
487 + strlen (fct_name) + 1);
488 char name[namlen];
489
490 /* Construct the function name. */
491 __stpcpy (__stpcpy (__stpcpy (name, ni->name),
492 "_"),
493 fct_name);
494
495 result = NULL;
496 for (tp = &tbl[0]; tp->fname; tp++)
497 if (strcmp (tp->fname, name) == 0)
498 {
499 result = tp->fp;
500 break;
501 }
502 }
503 #endif
504
505 /* Remember function pointer for later calls. Even if null, we
506 record it so a second try needn't search the library again. */
507 known->fct_ptr = result;
508 #ifdef PTR_MANGLE
509 PTR_MANGLE (known->fct_ptr);
510 #endif
511 }
512 }
513
514 /* Remove the lock. */
515 __libc_lock_unlock (lock);
516
517 return result;
518 }
519 libc_hidden_def (__nss_lookup_function)
520
521
522 static name_database *
523 internal_function
524 nss_parse_file (const char *fname)
525 {
526 FILE *fp;
527 name_database *result;
528 name_database_entry *last;
529 char *line;
530 size_t len;
531
532 /* Open the configuration file. */
533 fp = fopen (fname, "rce");
534 if (fp == NULL)
535 return NULL;
536
537 /* No threads use this stream. */
538 __fsetlocking (fp, FSETLOCKING_BYCALLER);
539
540 result = (name_database *) malloc (sizeof (name_database));
541 if (result == NULL)
542 {
543 fclose (fp);
544 return NULL;
545 }
546
547 result->entry = NULL;
548 result->library = NULL;
549 last = NULL;
550 line = NULL;
551 len = 0;
552 do
553 {
554 name_database_entry *this;
555 ssize_t n;
556
557 n = __getline (&line, &len, fp);
558 if (n < 0)
559 break;
560 if (line[n - 1] == '\n')
561 line[n - 1] = '\0';
562
563 /* Because the file format does not know any form of quoting we
564 can search forward for the next '#' character and if found
565 make it terminating the line. */
566 *__strchrnul (line, '#') = '\0';
567
568 /* If the line is blank it is ignored. */
569 if (line[0] == '\0')
570 continue;
571
572 /* Each line completely specifies the actions for a database. */
573 this = nss_getline (line);
574 if (this != NULL)
575 {
576 if (last != NULL)
577 last->next = this;
578 else
579 result->entry = this;
580
581 last = this;
582 }
583 }
584 while (!feof_unlocked (fp));
585
586 /* Free the buffer. */
587 free (line);
588 /* Close configuration file. */
589 fclose (fp);
590
591 return result;
592 }
593
594
595 /* Read the source names:
596 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
597 */
598 static service_user *
599 internal_function
600 nss_parse_service_list (const char *line)
601 {
602 service_user *result = NULL, **nextp = &result;
603
604 while (1)
605 {
606 service_user *new_service;
607 const char *name;
608
609 while (isspace (line[0]))
610 ++line;
611 if (line[0] == '\0')
612 /* No source specified. */
613 return result;
614
615 /* Read <source> identifier. */
616 name = line;
617 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
618 ++line;
619 if (name == line)
620 return result;
621
622
623 new_service = (service_user *) malloc (sizeof (service_user)
624 + (line - name + 1));
625 if (new_service == NULL)
626 return result;
627
628 *((char *) __mempcpy (new_service->name, name, line - name)) = '\0';
629
630 /* Set default actions. */
631 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
632 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
633 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
634 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
635 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
636 new_service->library = NULL;
637 new_service->known = NULL;
638 new_service->next = NULL;
639
640 while (isspace (line[0]))
641 ++line;
642
643 if (line[0] == '[')
644 {
645 /* Read criterions. */
646 do
647 ++line;
648 while (line[0] != '\0' && isspace (line[0]));
649
650 do
651 {
652 int not;
653 enum nss_status status;
654 lookup_actions action;
655
656 /* Grok ! before name to mean all statii but that one. */
657 not = line[0] == '!';
658 if (not)
659 ++line;
660
661 /* Read status name. */
662 name = line;
663 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
664 && line[0] != ']')
665 ++line;
666
667 /* Compare with known statii. */
668 if (line - name == 7)
669 {
670 if (__strncasecmp (name, "SUCCESS", 7) == 0)
671 status = NSS_STATUS_SUCCESS;
672 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
673 status = NSS_STATUS_UNAVAIL;
674 else
675 goto finish;
676 }
677 else if (line - name == 8)
678 {
679 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
680 status = NSS_STATUS_NOTFOUND;
681 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
682 status = NSS_STATUS_TRYAGAIN;
683 else
684 goto finish;
685 }
686 else
687 goto finish;
688
689 while (isspace (line[0]))
690 ++line;
691 if (line[0] != '=')
692 goto finish;
693 do
694 ++line;
695 while (isspace (line[0]));
696
697 name = line;
698 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
699 && line[0] != ']')
700 ++line;
701
702 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
703 action = NSS_ACTION_RETURN;
704 else if (line - name == 8
705 && __strncasecmp (name, "CONTINUE", 8) == 0)
706 action = NSS_ACTION_CONTINUE;
707 else
708 goto finish;
709
710 if (not)
711 {
712 /* Save the current action setting for this status,
713 set them all to the given action, and reset this one. */
714 const lookup_actions save = new_service->actions[2 + status];
715 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
716 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
717 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
718 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
719 new_service->actions[2 + status] = save;
720 }
721 else
722 new_service->actions[2 + status] = action;
723
724 /* Skip white spaces. */
725 while (isspace (line[0]))
726 ++line;
727 }
728 while (line[0] != ']');
729
730 /* Skip the ']'. */
731 ++line;
732 }
733
734 *nextp = new_service;
735 nextp = &new_service->next;
736 continue;
737
738 finish:
739 free (new_service);
740 return result;
741 }
742 }
743
744 static name_database_entry *
745 internal_function
746 nss_getline (char *line)
747 {
748 const char *name;
749 name_database_entry *result;
750 size_t len;
751
752 /* Ignore leading white spaces. ATTENTION: this is different from
753 what is implemented in Solaris. The Solaris man page says a line
754 beginning with a white space character is ignored. We regard
755 this as just another misfeature in Solaris. */
756 while (isspace (line[0]))
757 ++line;
758
759 /* Recognize `<database> ":"'. */
760 name = line;
761 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
762 ++line;
763 if (line[0] == '\0' || name == line)
764 /* Syntax error. */
765 return NULL;
766 *line++ = '\0';
767
768 len = strlen (name) + 1;
769
770 result = (name_database_entry *) malloc (sizeof (name_database_entry) + len);
771 if (result == NULL)
772 return NULL;
773
774 /* Save the database name. */
775 memcpy (result->name, name, len);
776
777 /* Parse the list of services. */
778 result->service = nss_parse_service_list (line);
779
780 result->next = NULL;
781 return result;
782 }
783
784
785 static service_library *
786 internal_function
787 nss_new_service (name_database *database, const char *name)
788 {
789 service_library **currentp = &database->library;
790
791 while (*currentp != NULL)
792 {
793 if (strcmp ((*currentp)->name, name) == 0)
794 return *currentp;
795 currentp = &(*currentp)->next;
796 }
797
798 /* We have to add the new service. */
799 *currentp = (service_library *) malloc (sizeof (service_library));
800 if (*currentp == NULL)
801 return NULL;
802
803 (*currentp)->name = name;
804 (*currentp)->lib_handle = NULL;
805 (*currentp)->next = NULL;
806
807 return *currentp;
808 }
809
810
811 #ifdef SHARED
812 /* Load all libraries for the service. */
813 static void
814 nss_load_all_libraries (const char *service, const char *def)
815 {
816 service_user *ni = NULL;
817
818 if (__nss_database_lookup (service, NULL, def, &ni) == 0)
819 while (ni != NULL)
820 {
821 nss_load_library (ni);
822 ni = ni->next;
823 }
824 }
825
826
827 /* Called by nscd and nscd alone. */
828 void
829 __nss_disable_nscd (void (*cb) (size_t, struct traced_file *))
830 {
831 # ifdef PTR_MANGLE
832 PTR_MANGLE (cb);
833 # endif
834 nscd_init_cb = cb;
835 is_nscd = true;
836
837 /* Find all the relevant modules so that the init functions are called. */
838 nss_load_all_libraries ("passwd", "compat [NOTFOUND=return] files");
839 nss_load_all_libraries ("group", "compat [NOTFOUND=return] files");
840 nss_load_all_libraries ("hosts", "dns [!UNAVAIL=return] files");
841 nss_load_all_libraries ("services", NULL);
842
843 /* Disable all uses of NSCD. */
844 __nss_not_use_nscd_passwd = -1;
845 __nss_not_use_nscd_group = -1;
846 __nss_not_use_nscd_hosts = -1;
847 __nss_not_use_nscd_services = -1;
848 __nss_not_use_nscd_netgroup = -1;
849 }
850 #endif
851
852 static void
853 free_database_entries (name_database_entry *entry)
854 {
855 while (entry != NULL)
856 {
857 name_database_entry *olde = entry;
858 service_user *service = entry->service;
859
860 while (service != NULL)
861 {
862 service_user *olds = service;
863
864 if (service->known != NULL)
865 __tdestroy (service->known, free);
866
867 service = service->next;
868 free (olds);
869 }
870
871 entry = entry->next;
872 free (olde);
873 }
874 }
875
876 /* Free all resources if necessary. */
877 libc_freeres_fn (free_defconfig)
878 {
879 name_database_entry *entry = defconfig_entries;
880
881 if (entry == NULL)
882 /* defconfig was not used. */
883 return;
884
885 /* Don't disturb ongoing other threads (if there are any). */
886 defconfig_entries = NULL;
887
888 free_database_entries (entry);
889 }
890
891 libc_freeres_fn (free_mem)
892 {
893 name_database *top = service_table;
894 service_library *library;
895
896 if (top == NULL)
897 /* Maybe we have not read the nsswitch.conf file. */
898 return;
899
900 /* Don't disturb ongoing other threads (if there are any). */
901 service_table = NULL;
902
903 free_database_entries (top->entry);
904
905 library = top->library;
906 while (library != NULL)
907 {
908 service_library *oldl = library;
909
910 if (library->lib_handle && library->lib_handle != (void *) -1l)
911 __libc_dlclose (library->lib_handle);
912
913 library = library->next;
914 free (oldl);
915 }
916
917 free (top);
918 }