]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - nss/nsswitch.c
Fix IA64 links-dso-program link.
[thirdparty/glibc.git] / nss / nsswitch.c
index ac73984576d9d9a36d339fa76563983634d4ebf3..ee46f24424bc1ca2085f4fd7f1060ae330ee5bae 100644 (file)
@@ -1,5 +1,4 @@
-/* Copyright (C) 1996-1999,2001-2007,2009,2010,2011
-   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.
 
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   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 <libc-lock.h>
 #include <search.h>
 #include <stdio.h>
 #include <stdio_ext.h>
 #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 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 attribute_hidden;             \
+  service_user *__nss_##name##_database attribute_hidden;                    \
   weak_extern (__nss_##name##_database)
 #include "databases.def"
 #undef DEFINE_DATABASE
@@ -73,8 +81,10 @@ static const struct
 };
 #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)
@@ -87,11 +97,19 @@ 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
@@ -142,8 +160,26 @@ __nss_database_lookup (const char *database, const char *alternate_name,
      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);
 
@@ -176,6 +212,7 @@ __nss_lookup (service_user **ni, const char *fct_name, const char *fct2_name,
 
   return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
 }
+libc_hidden_def (__nss_lookup)
 
 
 /* -1 == not found
@@ -276,7 +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);
 
@@ -315,7 +354,7 @@ nss_load_library (service_user *ni)
   if (ni->library->lib_handle == NULL)
     {
       /* Load the shared library.  */
-      size_t shlen = (7 + strlen (ni->library->name) + 3
+      size_t shlen = (7 + strlen (ni->name) + 3
                      + strlen (__nss_shlib_revision) + 1);
       int saved_errno = errno;
       char shlib_name[shlen];
@@ -323,7 +362,7 @@ nss_load_library (service_user *ni)
       /* Construct shared object name.  */
       __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name,
                                              "libnss_"),
-                                   ni->library->name),
+                                   ni->name),
                          ".so"),
                __nss_shlib_revision);
 
@@ -334,17 +373,18 @@ nss_load_library (service_user *ni)
          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->library->name)
+         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->library->name),
+                             ni->name),
                    "_init");
 
          /* Find the optional init function.  */
@@ -353,12 +393,13 @@ nss_load_library (service_user *ni)
          if (ifct != NULL)
            {
              void (*cb) (size_t, struct traced_file *) = nscd_init_cb;
-# ifdef PTR_DEMANGLE
+#  ifdef PTR_DEMANGLE
              PTR_DEMANGLE (cb);
-# endif
+#  endif
              ifct (cb);
            }
        }
+# endif
     }
 
   return 0;
@@ -391,7 +432,9 @@ __nss_lookup_function (service_user *ni, const char *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
     {
@@ -403,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, &ni->known, &known_compare);
+         free (known);
          result = NULL;
        }
       else
@@ -418,11 +464,8 @@ __nss_lookup_function (service_user *ni, const char *fct_name)
 #if !defined DO_STATIC_NSS || defined SHARED
          /* Load the appropriate library.  */
          if (nss_load_library (ni) != 0)
-           {
-             /* This only happens when out of memory.  */
-             free (known);
-             goto remove_from_tree;
-           }
+           /* This only happens when out of memory.  */
+           goto remove_from_tree;
 
          if (ni->library->lib_handle == (void *) -1l)
            /* Library not found => function not found.  */
@@ -430,13 +473,13 @@ __nss_lookup_function (service_user *ni, const char *fct_name)
          else
            {
              /* Get the desired function.  */
-             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 (__stpcpy (name, "_nss_"),
-                                           ni->library->name),
+                                           ni->name),
                                  "_"),
                        fct_name);
 
@@ -459,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);
 
@@ -481,7 +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
        }
     }
 
@@ -494,7 +539,6 @@ libc_hidden_def (__nss_lookup_function)
 
 
 static name_database *
-internal_function
 nss_parse_file (const char *fname)
 {
   FILE *fp;
@@ -504,7 +548,7 @@ nss_parse_file (const char *fname)
   size_t len;
 
   /* Open the configuration file.  */
-  fp = fopen (fname, "rc");
+  fp = fopen (fname, "rce");
   if (fp == NULL)
     return NULL;
 
@@ -555,7 +599,7 @@ nss_parse_file (const char *fname)
          last = this;
        }
     }
-  while (!feof_unlocked (fp));
+  while (!__feof_unlocked (fp));
 
   /* Free the buffer.  */
   free (line);
@@ -570,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;
@@ -646,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)
                {
@@ -655,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]));
@@ -678,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)
                {
@@ -707,11 +753,15 @@ 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;
@@ -751,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;
@@ -775,9 +825,10 @@ nss_new_service (name_database *database, const char *name)
 
   return *currentp;
 }
+#endif
 
 
-#ifdef SHARED
+#if defined SHARED && defined USE_NSCD
 /* Load all libraries for the service.  */
 static void
 nss_load_all_libraries (const char *service, const char *def)
@@ -804,8 +855,8 @@ __nss_disable_nscd (void (*cb) (size_t, struct traced_file *))
   is_nscd = true;
 
   /* Find all the relevant modules so that the init functions are called.  */
-  nss_load_all_libraries ("passwd", "compat [NOTFOUND=return] files");
-  nss_load_all_libraries ("group", "compat [NOTFOUND=return] files");
+  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);
 
@@ -814,25 +865,13 @@ __nss_disable_nscd (void (*cb) (size_t, struct traced_file *))
   __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
 
-
-/* Free all resources if necessary.  */
-libc_freeres_fn (free_mem)
+static void
+free_database_entries (name_database_entry *entry)
 {
-  name_database *top = service_table;
-  name_database_entry *entry;
-  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;
-
-  entry = top->entry;
   while (entry != NULL)
     {
       name_database_entry *olde = entry;
@@ -852,6 +891,36 @@ libc_freeres_fn (free_mem)
       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)