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