]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - nss/nsswitch.c
Fix IA64 links-dso-program link.
[thirdparty/glibc.git] / nss / nsswitch.c
index 3b48c85bb56975fba08ca1d25926231c6f8e17b1..ee46f24424bc1ca2085f4fd7f1060ae330ee5bae 100644 (file)
@@ -1,61 +1,76 @@
-/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
+/* Copyright (C) 1996-2018 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 #include <ctype.h>
 #include <dlfcn.h>
 #include <errno.h>
 #include <netdb.h>
-#include <bits/libc-lock.h>
-#include <link.h>      /* We need some help from ld.so.  */
+#include <libc-lock.h>
 #include <search.h>
 #include <stdio.h>
+#include <stdio_ext.h>
 #include <stdlib.h>
 #include <string.h>
 
-#if !defined DO_STATIC_NSS || defined PIC
+#include <aliases.h>
+#include <grp.h>
+#include <netinet/ether.h>
+#include <pwd.h>
+#include <shadow.h>
+
+#if !defined DO_STATIC_NSS || defined SHARED
 # include <gnu/lib-names.h>
 #endif
 
 #include "nsswitch.h"
+#include "../nscd/nscd_proto.h"
+#include <sysdep.h>
+#include <config.h>
+
+#ifdef LINK_OBSOLETE_NSL
+# define DEFAULT_CONFIG    "compat [NOTFOUND=return] files"
+# define DEFAULT_DEFCONFIG "nis [NOTFOUND=return] files"
+#else
+# define DEFAULT_CONFIG    "files"
+# define DEFAULT_DEFCONFIG "files"
+#endif
 
 /* Prototypes for the local functions.  */
-static void *nss_lookup_function (service_user *ni, const char *fct_name)
-     internal_function;
-static name_database *nss_parse_file (const char *fname) internal_function;
-static name_database_entry *nss_getline (char *line) internal_function;
-static service_user *nss_parse_service_list (const char *line)
-     internal_function;
+static name_database *nss_parse_file (const char *fname);
+static name_database_entry *nss_getline (char *line);
+static service_user *nss_parse_service_list (const char *line);
+#if !defined DO_STATIC_NSS || defined SHARED
 static service_library *nss_new_service (name_database *database,
-                                        const char *name) internal_function;
+                                        const char *name);
+#endif
 
 
 /* Declare external database variables.  */
 #define DEFINE_DATABASE(name)                                                \
-  extern service_user *__nss_##name##_database;                                      \
+  service_user *__nss_##name##_database attribute_hidden;                    \
   weak_extern (__nss_##name##_database)
 #include "databases.def"
 #undef DEFINE_DATABASE
 
 /* Structure to map database name to variable.  */
-static struct
+static const struct
 {
-  const char *name;
+  const char name[10];
   service_user **dbp;
 } databases[] =
 {
@@ -64,11 +79,17 @@ static struct
 #include "databases.def"
 #undef DEFINE_DATABASE
 };
+#define ndatabases (sizeof (databases) / sizeof (databases[0]))
+
+#ifdef USE_NSCD
+/* Flags whether custom rules for database is set.  */
+bool __nss_database_custom[NSS_DBSIDX_max];
+#endif
 
 
 __libc_lock_define_initialized (static, lock)
 
-#if !defined DO_STATIC_NSS || defined PIC
+#if !defined DO_STATIC_NSS || defined SHARED
 /* String with revision number of the shared object files.  */
 static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
 #endif
@@ -76,6 +97,20 @@ static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
 /* The root of the whole data base.  */
 static name_database *service_table;
 
+/* List of default service lists that were generated by glibc because
+   /etc/nsswitch.conf did not provide a value.
+   The list is only maintained so we can free such service lists in
+   __libc_freeres.  */
+static name_database_entry *defconfig_entries;
+
+
+#if defined USE_NSCD && (!defined DO_STATIC_NSS || defined SHARED)
+/* Nonzero if this is the nscd process.  */
+static bool is_nscd;
+/* The callback passed to the init functions when nscd is used.  */
+static void (*nscd_init_cb) (size_t, struct traced_file *);
+#endif
+
 
 /* -1 == database not found
     0 == database entry pointer stored */
@@ -120,27 +155,49 @@ __nss_database_lookup (const char *database, const char *alternate_name,
     }
 
   /* No configuration data is available, either because nsswitch.conf
-     doesn't exist or because it doesn't has a line for this database.
+     doesn't exist or because it doesn't have a line for this database.
 
      DEFCONFIG specifies the default service list for this database,
      or null to use the most common default.  */
   if (*ni == NULL)
-    *ni = nss_parse_service_list (defconfig
-                                 ?: "nis [NOTFOUND=return] files");
+    {
+      *ni = nss_parse_service_list (defconfig ?: DEFAULT_DEFCONFIG);
+      if (*ni != NULL)
+       {
+         /* Record the memory we've just allocated in defconfig_entries list,
+            so we can free it later.  */
+         name_database_entry *entry;
+
+         /* Allocate ENTRY plus size of name (1 here).  */
+         entry = (name_database_entry *) malloc (sizeof (*entry) + 1);
+
+         if (entry != NULL)
+           {
+             entry->next = defconfig_entries;
+             entry->service = *ni;
+             entry->name[0] = '\0';
+             defconfig_entries = entry;
+           }
+       }
+    }
 
   __libc_lock_unlock (lock);
 
-  return 0;
+  return *ni != NULL ? 0 : -1;
 }
+libc_hidden_def (__nss_database_lookup)
 
 
 /* -1 == not found
-    0 == adjusted for next function
+    0 == function found
     1 == finished */
 int
-__nss_lookup (service_user **ni, const char *fct_name, void **fctp)
+__nss_lookup (service_user **ni, const char *fct_name, const char *fct2_name,
+             void **fctp)
 {
-  *fctp = nss_lookup_function (*ni, fct_name);
+  *fctp = __nss_lookup_function (*ni, fct_name);
+  if (*fctp == NULL && fct2_name != NULL)
+    *fctp = __nss_lookup_function (*ni, fct2_name);
 
   while (*fctp == NULL
         && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
@@ -148,19 +205,22 @@ __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
     {
       *ni = (*ni)->next;
 
-      *fctp = nss_lookup_function (*ni, fct_name);
+      *fctp = __nss_lookup_function (*ni, fct_name);
+      if (*fctp == NULL && fct2_name != NULL)
+       *fctp = __nss_lookup_function (*ni, fct2_name);
     }
 
   return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
 }
+libc_hidden_def (__nss_lookup)
 
 
 /* -1 == not found
     0 == adjusted for next function
     1 == finished */
 int
-__nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
-           int all_values)
+__nss_next2 (service_user **ni, const char *fct_name, const char *fct2_name,
+            void **fctp, int status, int all_values)
 {
   if (all_values)
     {
@@ -173,8 +233,9 @@ __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
   else
     {
       /* This is really only for debugging.  */
-       if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
-        __libc_fatal ("illegal status in " __FUNCTION__);
+      if (__builtin_expect (NSS_STATUS_TRYAGAIN > status
+                           || status > NSS_STATUS_RETURN, 0))
+        __libc_fatal ("illegal status in __nss_next");
 
        if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
         return 1;
@@ -187,7 +248,9 @@ __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
     {
       *ni = (*ni)->next;
 
-      *fctp = nss_lookup_function (*ni, fct_name);
+      *fctp = __nss_lookup_function (*ni, fct_name);
+      if (*fctp == NULL && fct2_name != NULL)
+       *fctp = __nss_lookup_function (*ni, fct2_name);
     }
   while (*fctp == NULL
         && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
@@ -195,6 +258,16 @@ __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
 
   return *fctp != NULL ? 0 : -1;
 }
+libc_hidden_def (__nss_next2)
+
+
+int
+attribute_compat_text_section
+__nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
+           int all_values)
+{
+  return __nss_next2 (ni, fct_name, NULL, fctp, status, all_values);
+}
 
 
 int
@@ -203,7 +276,7 @@ __nss_configure_lookup (const char *dbname, const char *service_line)
   service_user *new_db;
   size_t cnt;
 
-  for (cnt = 0; cnt < sizeof databases; ++cnt)
+  for (cnt = 0; cnt < ndatabases; ++cnt)
     {
       int cmp = strcmp (dbname, databases[cnt].name);
       if (cmp == 0)
@@ -215,7 +288,7 @@ __nss_configure_lookup (const char *dbname, const char *service_line)
        }
     }
 
-  if (cnt == sizeof databases)
+  if (cnt == ndatabases)
     {
       __set_errno (EINVAL);
       return -1;
@@ -240,6 +313,9 @@ __nss_configure_lookup (const char *dbname, const char *service_line)
 
   /* Install new rules.  */
   *databases[cnt].dbp = new_db;
+#ifdef USE_NSCD
+  __nss_database_custom[cnt] = true;
+#endif
 
   __libc_lock_unlock (lock);
 
@@ -247,72 +323,92 @@ __nss_configure_lookup (const char *dbname, const char *service_line)
 }
 
 
-#if !defined DO_STATIC_NSS || defined PIC
+/* Comparison function for searching NI->known tree.  */
 static int
-nss_dlerror_run (void (*operate) (void *), void *args)
+known_compare (const void *p1, const void *p2)
 {
-  char *last_errstring = NULL;
-  int result;
-
-  (void) _dl_catch_error (&last_errstring, operate, args);
-
-  result = last_errstring != NULL;
-  if (result)
-    free (last_errstring);
-
-  return result;
+  return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
+                               *(const char *const *) p2);
 }
 
 
-struct do_open_args
-{
-  /* Argument to do_open.  */
-  char *shlib_name;
-  service_user *ni;
-};
-
-struct get_sym_args
+#if !defined DO_STATIC_NSS || defined SHARED
+/* Load library.  */
+static int
+nss_load_library (service_user *ni)
 {
-  /* Arguments to get_sym.  */
-  struct link_map *map;
-  char *name;
-
-  /* Return values of get_sym.  */
-  ElfW(Addr) loadbase;
-  const ElfW(Sym) *ref;
-};
+  if (ni->library == NULL)
+    {
+      /* This service has not yet been used.  Fetch the service
+        library for it, creating a new one if need be.  If there
+        is no service table from the file, this static variable
+        holds the head of the service_library list made from the
+        default configuration.  */
+      static name_database default_table;
+      ni->library = nss_new_service (service_table ?: &default_table,
+                                    ni->name);
+      if (ni->library == NULL)
+       return -1;
+    }
 
-static void
-do_open (void *a)
-{
-  struct do_open_args *args = (struct do_open_args *) a;
-  /* Open and relocate the shared object.  */
-  args->ni->library->lib_handle = _dl_open (args->shlib_name, RTLD_LAZY);
-}
+  if (ni->library->lib_handle == NULL)
+    {
+      /* Load the shared library.  */
+      size_t shlen = (7 + strlen (ni->name) + 3
+                     + strlen (__nss_shlib_revision) + 1);
+      int saved_errno = errno;
+      char shlib_name[shlen];
+
+      /* Construct shared object name.  */
+      __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name,
+                                             "libnss_"),
+                                   ni->name),
+                         ".so"),
+               __nss_shlib_revision);
+
+      ni->library->lib_handle = __libc_dlopen (shlib_name);
+      if (ni->library->lib_handle == NULL)
+       {
+         /* Failed to load the library.  */
+         ni->library->lib_handle = (void *) -1l;
+         __set_errno (saved_errno);
+       }
+# ifdef USE_NSCD
+      else if (is_nscd)
+       {
+         /* Call the init function when nscd is used.  */
+         size_t initlen = (5 + strlen (ni->name)
+                           + strlen ("_init") + 1);
+         char init_name[initlen];
+
+         /* Construct the init function name.  */
+         __stpcpy (__stpcpy (__stpcpy (init_name,
+                                       "_nss_"),
+                             ni->name),
+                   "_init");
+
+         /* Find the optional init function.  */
+         void (*ifct) (void (*) (size_t, struct traced_file *))
+           = __libc_dlsym (ni->library->lib_handle, init_name);
+         if (ifct != NULL)
+           {
+             void (*cb) (size_t, struct traced_file *) = nscd_init_cb;
+#  ifdef PTR_DEMANGLE
+             PTR_DEMANGLE (cb);
+#  endif
+             ifct (cb);
+           }
+       }
+# endif
+    }
 
-static void
-get_sym (void *a)
-{
-  struct get_sym_args *args = (struct get_sym_args *) a;
-  struct link_map *scope[2] = { args->map, NULL };
-  args->ref = NULL;
-  args->loadbase = _dl_lookup_symbol (args->name, &args->ref,
-                                     scope, args->map->l_name, 0);
+  return 0;
 }
 #endif
 
-/* Comparison function for searching NI->known tree.  */
-static int
-known_compare (const void *p1, const void *p2)
-{
-  return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
-                               *(const char *const *) p2);
-}
-
 
-static void *
-internal_function
-nss_lookup_function (service_user *ni, const char *fct_name)
+void *
+__nss_lookup_function (service_user *ni, const char *fct_name)
 {
   void **found, *result;
 
@@ -328,10 +424,18 @@ nss_lookup_function (service_user *ni, const char *fct_name)
      enough to a pointer to our structure to use as a lookup key that
      will be passed to `known_compare' (above).  */
 
-  found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
-  if (*found != &fct_name)
-    /* The search found an existing structure in the tree.  */
-    result = ((known_function *) *found)->fct_ptr;
+  found = __tsearch (&fct_name, &ni->known, &known_compare);
+  if (found == NULL)
+    /* This means out-of-memory.  */
+    result = NULL;
+  else if (*found != &fct_name)
+    {
+      /* The search found an existing structure in the tree.  */
+      result = ((known_function *) *found)->fct_ptr;
+#ifdef PTR_DEMANGLE
+      PTR_DEMANGLE (result);
+#endif
+    }
   else
     {
       /* This name was not known before.  Now we have a node in the tree
@@ -342,10 +446,13 @@ nss_lookup_function (service_user *ni, const char *fct_name)
       known_function *known = malloc (sizeof *known);
       if (! known)
        {
+#if !defined DO_STATIC_NSS || defined SHARED
        remove_from_tree:
+#endif
          /* Oops.  We can't instantiate this node properly.
             Remove it from the tree.  */
-         __tdelete (&fct_name, (void **) &ni->known, &known_compare);
+         __tdelete (&fct_name, &ni->known, &known_compare);
+         free (known);
          result = NULL;
        }
       else
@@ -354,87 +461,34 @@ nss_lookup_function (service_user *ni, const char *fct_name)
          *found = known;
          known->fct_name = fct_name;
 
-         if (ni->library == NULL)
-           {
-             /* This service has not yet been used.  Fetch the service
-                library for it, creating a new one if need be.  If there
-                is no service table from the file, this static variable
-                holds the head of the service_library list made from the
-                default configuration.  */
-             static name_database default_table;
-             ni->library = nss_new_service (service_table ?: &default_table,
-                                            ni->name);
-             if (ni->library == NULL)
-               {
-                 /* This only happens when out of memory.  */
-                 free (known);
-                 goto remove_from_tree;
-               }
-           }
-
-#if !defined DO_STATIC_NSS || defined PIC
-         if (ni->library->lib_handle == NULL)
-           {
-             /* Load the shared library.  */
-             size_t shlen = (7 + strlen (ni->library->name) + 3
-                             + strlen (__nss_shlib_revision) + 1);
-             int saved_errno = errno;
-             struct do_open_args args;
-             args.shlib_name = __alloca (shlen);
-             args.ni = ni;
-
-             /* Construct shared object name.  */
-             __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.shlib_name,
-                                                     "libnss_"),
-                                           ni->library->name),
-                                 ".so"),
-                       __nss_shlib_revision);
-
-             if (nss_dlerror_run (do_open, &args) != 0)
-               {
-                 /* Failed to load the library.  */
-                 ni->library->lib_handle = (void *) -1l;
-                 __set_errno (saved_errno);
-               }
-           }
+#if !defined DO_STATIC_NSS || defined SHARED
+         /* Load the appropriate library.  */
+         if (nss_load_library (ni) != 0)
+           /* This only happens when out of memory.  */
+           goto remove_from_tree;
 
          if (ni->library->lib_handle == (void *) -1l)
            /* Library not found => function not found.  */
            result = NULL;
          else
            {
-             /* Get the desired function.  Again,  GNU ld.so magic ahead.  */
-             size_t namlen = (5 + strlen (ni->library->name) + 1
+             /* Get the desired function.  */
+             size_t namlen = (5 + strlen (ni->name) + 1
                               + strlen (fct_name) + 1);
-             struct get_sym_args args;
-             args.name = __alloca (namlen);
-             args.map = ni->library->lib_handle;
+             char name[namlen];
 
              /* Construct the function name.  */
-             __stpcpy (__stpcpy (__stpcpy (__stpcpy (args.name, "_nss_"),
-                                           ni->library->name),
+             __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
+                                           ni->name),
                                  "_"),
                        fct_name);
 
              /* Look up the symbol.  */
-             result = (nss_dlerror_run (get_sym, &args) ? NULL
-                       : (void *) (args.loadbase + args.ref->st_value));
+             result = __libc_dlsym (ni->library->lib_handle, name);
            }
 #else
          /* We can't get function address dynamically in static linking. */
          {
-# define DEFINE_ENT(h,nm)                                                    \
-           extern void _nss_##h##_get##nm##ent_r (void);                     \
-           extern void _nss_##h##_end##nm##ent (void);                       \
-           extern void _nss_##h##_set##nm##ent (void);
-# define DEFINE_GET(h,nm)                                                    \
-           extern void _nss_##h##_get##nm##_r (void);
-# define DEFINE_GETBY(h,nm,ky)                                               \
-           extern void _nss_##h##_get##nm##by##ky##_r (void);
-# include "function.def"
-# undef DEFINE_ENT
-# undef DEFINE_GET
-# undef DEFINE_GETBY
 # define DEFINE_ENT(h,nm)                                                    \
            { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r },                \
            { #h"_end"#nm"ent", _nss_##h##_end##nm##ent },                    \
@@ -448,12 +502,12 @@ nss_lookup_function (service_user *ni, const char *fct_name)
 # include "function.def"
                { NULL, NULL }
              };
-           size_t namlen = (5 + strlen (ni->library->name) + 1
+           size_t namlen = (5 + strlen (ni->name) + 1
                             + strlen (fct_name) + 1);
            char name[namlen];
 
            /* Construct the function name.  */
-           __stpcpy (__stpcpy (__stpcpy (name, ni->library->name),
+           __stpcpy (__stpcpy (__stpcpy (name, ni->name),
                                "_"),
                      fct_name);
 
@@ -470,6 +524,9 @@ nss_lookup_function (service_user *ni, const char *fct_name)
          /* Remember function pointer for later calls.  Even if null, we
             record it so a second try needn't search the library again.  */
          known->fct_ptr = result;
+#ifdef PTR_MANGLE
+         PTR_MANGLE (known->fct_ptr);
+#endif
        }
     }
 
@@ -478,10 +535,10 @@ nss_lookup_function (service_user *ni, const char *fct_name)
 
   return result;
 }
+libc_hidden_def (__nss_lookup_function)
 
 
 static name_database *
-internal_function
 nss_parse_file (const char *fname)
 {
   FILE *fp;
@@ -491,13 +548,19 @@ nss_parse_file (const char *fname)
   size_t len;
 
   /* Open the configuration file.  */
-  fp = fopen (fname, "r");
+  fp = fopen (fname, "rce");
   if (fp == NULL)
     return NULL;
 
+  /* No threads use this stream.  */
+  __fsetlocking (fp, FSETLOCKING_BYCALLER);
+
   result = (name_database *) malloc (sizeof (name_database));
   if (result == NULL)
-    return NULL;
+    {
+      fclose (fp);
+      return NULL;
+    }
 
   result->entry = NULL;
   result->library = NULL;
@@ -508,7 +571,6 @@ nss_parse_file (const char *fname)
     {
       name_database_entry *this;
       ssize_t n;
-      char *cp;
 
       n = __getline (&line, &len, fp);
       if (n < 0)
@@ -519,9 +581,7 @@ nss_parse_file (const char *fname)
       /* Because the file format does not know any form of quoting we
         can search forward for the next '#' character and if found
         make it terminating the line.  */
-      cp = strchr (line, '#');
-      if (cp != NULL)
-       *cp = '\0';
+      *__strchrnul (line, '#') = '\0';
 
       /* If the line is blank it is ignored.  */
       if (line[0] == '\0')
@@ -539,7 +599,7 @@ nss_parse_file (const char *fname)
          last = this;
        }
     }
-  while (!feof (fp));
+  while (!__feof_unlocked (fp));
 
   /* Free the buffer.  */
   free (line);
@@ -554,7 +614,6 @@ nss_parse_file (const char *fname)
        `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
    */
 static service_user *
-internal_function
 nss_parse_service_list (const char *line)
 {
   service_user *result = NULL, **nextp = &result;
@@ -578,21 +637,12 @@ nss_parse_service_list (const char *line)
        return result;
 
 
-      new_service = (service_user *) malloc (sizeof (service_user));
+      new_service = (service_user *) malloc (sizeof (service_user)
+                                            + (line - name + 1));
       if (new_service == NULL)
        return result;
-      else
-       {
-         char *source = (char *) malloc (line - name + 1);
-         if (source == NULL)
-           {
-             free (new_service);
-             return result;
-           }
-         *((char *) __mempcpy (source, name, line - name)) = '\0';
 
-         new_service->name = source;
-       }
+      *((char *) __mempcpy (new_service->name, name, line - name)) = '\0';
 
       /* Set default actions.  */
       new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
@@ -621,7 +671,8 @@ nss_parse_service_list (const char *line)
              lookup_actions action;
 
              /* Grok ! before name to mean all statii but that one.  */
-             if (not = line[0] == '!')
+             not = line[0] == '!';
+             if (not)
                ++line;
 
              /* Read status name.  */
@@ -638,7 +689,7 @@ nss_parse_service_list (const char *line)
                  else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
                    status = NSS_STATUS_UNAVAIL;
                  else
-                   return result;
+                   goto finish;
                }
              else if (line - name == 8)
                {
@@ -647,15 +698,15 @@ nss_parse_service_list (const char *line)
                  else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
                    status = NSS_STATUS_TRYAGAIN;
                  else
-                   return result;
+                   goto finish;
                }
              else
-               return result;
+               goto finish;
 
              while (isspace (line[0]))
                ++line;
              if (line[0] != '=')
-               return result;
+               goto finish;
              do
                ++line;
              while (isspace (line[0]));
@@ -670,8 +721,11 @@ nss_parse_service_list (const char *line)
              else if (line - name == 8
                       && __strncasecmp (name, "CONTINUE", 8) == 0)
                action = NSS_ACTION_CONTINUE;
+             else if (line - name == 5
+                      && __strncasecmp (name, "MERGE", 5) == 0)
+               action = NSS_ACTION_MERGE;
              else
-               return result;
+               goto finish;
 
              if (not)
                {
@@ -699,15 +753,20 @@ nss_parse_service_list (const char *line)
 
       *nextp = new_service;
       nextp = &new_service->next;
+      continue;
+
+    finish:
+      free (new_service);
+      return result;
     }
 }
 
 static name_database_entry *
-internal_function
 nss_getline (char *line)
 {
   const char *name;
   name_database_entry *result;
+  size_t len;
 
   /* Ignore leading white spaces.  ATTENTION: this is different from
      what is implemented in Solaris.  The Solaris man page says a line
@@ -725,21 +784,14 @@ nss_getline (char *line)
     return NULL;
   *line++ = '\0';
 
-  result = (name_database_entry *) malloc (sizeof (name_database_entry));
+  len = strlen (name) + 1;
+
+  result = (name_database_entry *) malloc (sizeof (name_database_entry) + len);
   if (result == NULL)
     return NULL;
 
   /* Save the database name.  */
-  {
-    const size_t len = strlen (name) + 1;
-    char *new = malloc (len);
-    if (new == NULL)
-      {
-       free (result);
-       return NULL;
-      }
-    result->name = memcpy (new, name, len);
-  }
+  memcpy (result->name, name, len);
 
   /* Parse the list of services.  */
   result->service = nss_parse_service_list (line);
@@ -749,8 +801,8 @@ nss_getline (char *line)
 }
 
 
+#if !defined DO_STATIC_NSS || defined SHARED
 static service_library *
-internal_function
 nss_new_service (name_database *database, const char *name)
 {
   service_library **currentp = &database->library;
@@ -773,3 +825,114 @@ nss_new_service (name_database *database, const char *name)
 
   return *currentp;
 }
+#endif
+
+
+#if defined SHARED && defined USE_NSCD
+/* Load all libraries for the service.  */
+static void
+nss_load_all_libraries (const char *service, const char *def)
+{
+  service_user *ni = NULL;
+
+  if (__nss_database_lookup (service, NULL, def, &ni) == 0)
+    while (ni != NULL)
+      {
+       nss_load_library (ni);
+       ni = ni->next;
+      }
+}
+
+
+/* Called by nscd and nscd alone.  */
+void
+__nss_disable_nscd (void (*cb) (size_t, struct traced_file *))
+{
+# ifdef PTR_MANGLE
+  PTR_MANGLE (cb);
+# endif
+  nscd_init_cb = cb;
+  is_nscd = true;
+
+  /* Find all the relevant modules so that the init functions are called.  */
+  nss_load_all_libraries ("passwd", DEFAULT_CONFIG);
+  nss_load_all_libraries ("group", DEFAULT_CONFIG);
+  nss_load_all_libraries ("hosts", "dns [!UNAVAIL=return] files");
+  nss_load_all_libraries ("services", NULL);
+
+  /* Disable all uses of NSCD.  */
+  __nss_not_use_nscd_passwd = -1;
+  __nss_not_use_nscd_group = -1;
+  __nss_not_use_nscd_hosts = -1;
+  __nss_not_use_nscd_services = -1;
+  __nss_not_use_nscd_netgroup = -1;
+}
+#endif
+
+static void
+free_database_entries (name_database_entry *entry)
+{
+  while (entry != NULL)
+    {
+      name_database_entry *olde = entry;
+      service_user *service = entry->service;
+
+      while (service != NULL)
+       {
+         service_user *olds = service;
+
+         if (service->known != NULL)
+           __tdestroy (service->known, free);
+
+         service = service->next;
+         free (olds);
+       }
+
+      entry = entry->next;
+      free (olde);
+    }
+}
+
+/* Free all resources if necessary.  */
+libc_freeres_fn (free_defconfig)
+{
+  name_database_entry *entry = defconfig_entries;
+
+  if (entry == NULL)
+    /* defconfig was not used.  */
+    return;
+
+  /* Don't disturb ongoing other threads (if there are any).  */
+  defconfig_entries = NULL;
+
+  free_database_entries (entry);
+}
+
+libc_freeres_fn (free_mem)
+{
+  name_database *top = service_table;
+  service_library *library;
+
+  if (top == NULL)
+    /* Maybe we have not read the nsswitch.conf file.  */
+    return;
+
+  /* Don't disturb ongoing other threads (if there are any).  */
+  service_table = NULL;
+
+  free_database_entries (top->entry);
+
+  library = top->library;
+  while (library != NULL)
+    {
+      service_library *oldl = library;
+
+      if (library->lib_handle && library->lib_handle != (void *) -1l)
+       __libc_dlclose (library->lib_handle);
+
+      library = library->next;
+      free (oldl);
+    }
+
+  free (top);
+}