Blame nss/nsswitch.c

Packit 6c4009
/* Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include <search.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdio_ext.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <aliases.h>
Packit 6c4009
#include <grp.h>
Packit 6c4009
#include <netinet/ether.h>
Packit 6c4009
#include <pwd.h>
Packit 6c4009
#include <shadow.h>
Packit 6c4009
Packit 6c4009
#if !defined DO_STATIC_NSS || defined SHARED
Packit 6c4009
# include <gnu/lib-names.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include "nsswitch.h"
Packit 6c4009
#include "../nscd/nscd_proto.h"
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <config.h>
Packit 6c4009
Packit 6c4009
#ifdef LINK_OBSOLETE_NSL
Packit 6c4009
# define DEFAULT_CONFIG    "compat [NOTFOUND=return] files"
Packit 6c4009
# define DEFAULT_DEFCONFIG "nis [NOTFOUND=return] files"
Packit 6c4009
#else
Packit 6c4009
# define DEFAULT_CONFIG    "files"
Packit 6c4009
# define DEFAULT_DEFCONFIG "files"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Prototypes for the local functions.  */
Packit 6c4009
static name_database *nss_parse_file (const char *fname);
Packit 6c4009
static name_database_entry *nss_getline (char *line);
Packit 6c4009
static service_user *nss_parse_service_list (const char *line);
Packit 6c4009
#if !defined DO_STATIC_NSS || defined SHARED
Packit 6c4009
static service_library *nss_new_service (name_database *database,
Packit 6c4009
					 const char *name);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Declare external database variables.  */
Packit 6c4009
#define DEFINE_DATABASE(name)						      \
Packit 6c4009
  service_user *__nss_##name##_database attribute_hidden;		      \
Packit 6c4009
  weak_extern (__nss_##name##_database)
Packit 6c4009
#include "databases.def"
Packit 6c4009
#undef DEFINE_DATABASE
Packit 6c4009
Packit 6c4009
/* Structure to map database name to variable.  */
Packit 6c4009
static const struct
Packit 6c4009
{
Packit 6c4009
  const char name[10];
Packit 6c4009
  service_user **dbp;
Packit 6c4009
} databases[] =
Packit 6c4009
{
Packit 6c4009
#define DEFINE_DATABASE(name)						      \
Packit 6c4009
  { #name, &__nss_##name##_database },
Packit 6c4009
#include "databases.def"
Packit 6c4009
#undef DEFINE_DATABASE
Packit 6c4009
};
Packit 6c4009
#define ndatabases (sizeof (databases) / sizeof (databases[0]))
Packit 6c4009
Packit 6c4009
#ifdef USE_NSCD
Packit 6c4009
/* Flags whether custom rules for database is set.  */
Packit 6c4009
bool __nss_database_custom[NSS_DBSIDX_max];
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
__libc_lock_define_initialized (static, lock)
Packit 6c4009
Packit 6c4009
#if !defined DO_STATIC_NSS || defined SHARED
Packit 6c4009
/* String with revision number of the shared object files.  */
Packit 6c4009
static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* The root of the whole data base.  */
Packit 6c4009
static name_database *service_table;
Packit 6c4009
Packit 6c4009
/* List of default service lists that were generated by glibc because
Packit 6c4009
   /etc/nsswitch.conf did not provide a value.
Packit 6c4009
   The list is only maintained so we can free such service lists in
Packit 6c4009
   __libc_freeres.  */
Packit 6c4009
static name_database_entry *defconfig_entries;
Packit 6c4009
Packit 6c4009
Packit 6c4009
#if defined USE_NSCD && (!defined DO_STATIC_NSS || defined SHARED)
Packit 6c4009
/* Nonzero if this is the nscd process.  */
Packit 6c4009
static bool is_nscd;
Packit 6c4009
/* The callback passed to the init functions when nscd is used.  */
Packit 6c4009
static void (*nscd_init_cb) (size_t, struct traced_file *);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* -1 == database not found
Packit 6c4009
    0 == database entry pointer stored */
Packit 6c4009
int
Packit Service cbad28
__nss_database_lookup2 (const char *database, const char *alternate_name,
Packit Service cbad28
			const char *defconfig, service_user **ni)
Packit 6c4009
{
Packit 6c4009
  /* Prevent multiple threads to change the service table.  */
Packit 6c4009
  __libc_lock_lock (lock);
Packit 6c4009
Packit 6c4009
  /* Reconsider database variable in case some other thread called
Packit 6c4009
     `__nss_configure_lookup' while we waited for the lock.  */
Packit 6c4009
  if (*ni != NULL)
Packit 6c4009
    {
Packit 6c4009
      __libc_lock_unlock (lock);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Are we initialized yet?  */
Packit 6c4009
  if (service_table == NULL)
Packit 6c4009
    /* Read config file.  */
Packit 6c4009
    service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
Packit 6c4009
Packit 6c4009
  /* Test whether configuration data is available.  */
Packit 6c4009
  if (service_table != NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Return first `service_user' entry for DATABASE.  */
Packit 6c4009
      name_database_entry *entry;
Packit 6c4009
Packit 6c4009
      /* XXX Could use some faster mechanism here.  But each database is
Packit 6c4009
	 only requested once and so this might not be critical.  */
Packit 6c4009
      for (entry = service_table->entry; entry != NULL; entry = entry->next)
Packit 6c4009
	if (strcmp (database, entry->name) == 0)
Packit 6c4009
	  *ni = entry->service;
Packit 6c4009
Packit 6c4009
      if (*ni == NULL && alternate_name != NULL)
Packit 6c4009
	/* We haven't found an entry so far.  Try to find it with the
Packit 6c4009
	   alternative name.  */
Packit 6c4009
	for (entry = service_table->entry; entry != NULL; entry = entry->next)
Packit 6c4009
	  if (strcmp (alternate_name, entry->name) == 0)
Packit 6c4009
	    *ni = entry->service;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* No configuration data is available, either because nsswitch.conf
Packit 6c4009
     doesn't exist or because it doesn't have a line for this database.
Packit 6c4009
Packit 6c4009
     DEFCONFIG specifies the default service list for this database,
Packit 6c4009
     or null to use the most common default.  */
Packit 6c4009
  if (*ni == NULL)
Packit 6c4009
    {
Packit 6c4009
      *ni = nss_parse_service_list (defconfig ?: DEFAULT_DEFCONFIG);
Packit 6c4009
      if (*ni != NULL)
Packit 6c4009
	{
Packit 6c4009
	  /* Record the memory we've just allocated in defconfig_entries list,
Packit 6c4009
	     so we can free it later.  */
Packit 6c4009
	  name_database_entry *entry;
Packit 6c4009
Packit 6c4009
	  /* Allocate ENTRY plus size of name (1 here).  */
Packit 6c4009
	  entry = (name_database_entry *) malloc (sizeof (*entry) + 1);
Packit 6c4009
Packit 6c4009
	  if (entry != NULL)
Packit 6c4009
	    {
Packit 6c4009
	      entry->next = defconfig_entries;
Packit 6c4009
	      entry->service = *ni;
Packit 6c4009
	      entry->name[0] = '\0';
Packit 6c4009
	      defconfig_entries = entry;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  __libc_lock_unlock (lock);
Packit 6c4009
Packit 6c4009
  return *ni != NULL ? 0 : -1;
Packit 6c4009
}
Packit Service cbad28
libc_hidden_def (__nss_database_lookup2)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* -1 == not found
Packit 6c4009
    0 == function found
Packit 6c4009
    1 == finished */
Packit 6c4009
int
Packit 6c4009
__nss_lookup (service_user **ni, const char *fct_name, const char *fct2_name,
Packit 6c4009
	      void **fctp)
Packit 6c4009
{
Packit 6c4009
  *fctp = __nss_lookup_function (*ni, fct_name);
Packit 6c4009
  if (*fctp == NULL && fct2_name != NULL)
Packit 6c4009
    *fctp = __nss_lookup_function (*ni, fct2_name);
Packit 6c4009
Packit 6c4009
  while (*fctp == NULL
Packit 6c4009
	 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
Packit 6c4009
	 && (*ni)->next != NULL)
Packit 6c4009
    {
Packit 6c4009
      *ni = (*ni)->next;
Packit 6c4009
Packit 6c4009
      *fctp = __nss_lookup_function (*ni, fct_name);
Packit 6c4009
      if (*fctp == NULL && fct2_name != NULL)
Packit 6c4009
	*fctp = __nss_lookup_function (*ni, fct2_name);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__nss_lookup)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* -1 == not found
Packit 6c4009
    0 == adjusted for next function
Packit 6c4009
    1 == finished */
Packit 6c4009
int
Packit 6c4009
__nss_next2 (service_user **ni, const char *fct_name, const char *fct2_name,
Packit 6c4009
	     void **fctp, int status, int all_values)
Packit 6c4009
{
Packit 6c4009
  if (all_values)
Packit 6c4009
    {
Packit 6c4009
      if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
Packit 6c4009
	  && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
Packit 6c4009
	  && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
Packit 6c4009
	  && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
Packit 6c4009
	return 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* This is really only for debugging.  */
Packit 6c4009
      if (__builtin_expect (NSS_STATUS_TRYAGAIN > status
Packit 6c4009
			    || status > NSS_STATUS_RETURN, 0))
Packit Service 88f876
	 __libc_fatal ("Illegal status in __nss_next.\n");
Packit 6c4009
Packit 6c4009
       if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
Packit 6c4009
	 return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if ((*ni)->next == NULL)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      *ni = (*ni)->next;
Packit 6c4009
Packit 6c4009
      *fctp = __nss_lookup_function (*ni, fct_name);
Packit 6c4009
      if (*fctp == NULL && fct2_name != NULL)
Packit 6c4009
	*fctp = __nss_lookup_function (*ni, fct2_name);
Packit 6c4009
    }
Packit 6c4009
  while (*fctp == NULL
Packit 6c4009
	 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
Packit 6c4009
	 && (*ni)->next != NULL);
Packit 6c4009
Packit 6c4009
  return *fctp != NULL ? 0 : -1;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__nss_next2)
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__nss_configure_lookup (const char *dbname, const char *service_line)
Packit 6c4009
{
Packit 6c4009
  service_user *new_db;
Packit 6c4009
  size_t cnt;
Packit 6c4009
Packit 6c4009
  for (cnt = 0; cnt < ndatabases; ++cnt)
Packit 6c4009
    {
Packit 6c4009
      int cmp = strcmp (dbname, databases[cnt].name);
Packit 6c4009
      if (cmp == 0)
Packit 6c4009
	break;
Packit 6c4009
      if (cmp < 0)
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (EINVAL);
Packit 6c4009
	  return -1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (cnt == ndatabases)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Test whether it is really used.  */
Packit 6c4009
  if (databases[cnt].dbp == NULL)
Packit 6c4009
    /* Nothing to do, but we could do.  */
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  /* Try to generate new data.  */
Packit 6c4009
  new_db = nss_parse_service_list (service_line);
Packit 6c4009
  if (new_db == NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Illegal service specification.  */
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Prevent multiple threads to change the service table.  */
Packit 6c4009
  __libc_lock_lock (lock);
Packit 6c4009
Packit 6c4009
  /* Install new rules.  */
Packit 6c4009
  *databases[cnt].dbp = new_db;
Packit 6c4009
#ifdef USE_NSCD
Packit 6c4009
  __nss_database_custom[cnt] = true;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  __libc_lock_unlock (lock);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Comparison function for searching NI->known tree.  */
Packit 6c4009
static int
Packit 6c4009
known_compare (const void *p1, const void *p2)
Packit 6c4009
{
Packit 6c4009
  return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
Packit 6c4009
				*(const char *const *) p2);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#if !defined DO_STATIC_NSS || defined SHARED
Packit 6c4009
/* Load library.  */
Packit 6c4009
static int
Packit 6c4009
nss_load_library (service_user *ni)
Packit 6c4009
{
Packit 6c4009
  if (ni->library == NULL)
Packit 6c4009
    {
Packit 6c4009
      /* This service has not yet been used.  Fetch the service
Packit 6c4009
	 library for it, creating a new one if need be.  If there
Packit 6c4009
	 is no service table from the file, this static variable
Packit 6c4009
	 holds the head of the service_library list made from the
Packit 6c4009
	 default configuration.  */
Packit 6c4009
      static name_database default_table;
Packit 6c4009
      ni->library = nss_new_service (service_table ?: &default_table,
Packit 6c4009
				     ni->name);
Packit 6c4009
      if (ni->library == NULL)
Packit 6c4009
	return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (ni->library->lib_handle == NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Load the shared library.  */
Packit 6c4009
      size_t shlen = (7 + strlen (ni->name) + 3
Packit 6c4009
		      + strlen (__nss_shlib_revision) + 1);
Packit 6c4009
      int saved_errno = errno;
Packit 6c4009
      char shlib_name[shlen];
Packit 6c4009
Packit 6c4009
      /* Construct shared object name.  */
Packit 6c4009
      __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name,
Packit 6c4009
					      "libnss_"),
Packit 6c4009
				    ni->name),
Packit 6c4009
			  ".so"),
Packit 6c4009
		__nss_shlib_revision);
Packit 6c4009
Packit 6c4009
      ni->library->lib_handle = __libc_dlopen (shlib_name);
Packit 6c4009
      if (ni->library->lib_handle == NULL)
Packit 6c4009
	{
Packit 6c4009
	  /* Failed to load the library.  */
Packit 6c4009
	  ni->library->lib_handle = (void *) -1l;
Packit 6c4009
	  __set_errno (saved_errno);
Packit 6c4009
	}
Packit 6c4009
# ifdef USE_NSCD
Packit 6c4009
      else if (is_nscd)
Packit 6c4009
	{
Packit 6c4009
	  /* Call the init function when nscd is used.  */
Packit 6c4009
	  size_t initlen = (5 + strlen (ni->name)
Packit 6c4009
			    + strlen ("_init") + 1);
Packit 6c4009
	  char init_name[initlen];
Packit 6c4009
Packit 6c4009
	  /* Construct the init function name.  */
Packit 6c4009
	  __stpcpy (__stpcpy (__stpcpy (init_name,
Packit 6c4009
					"_nss_"),
Packit 6c4009
			      ni->name),
Packit 6c4009
		    "_init");
Packit 6c4009
Packit 6c4009
	  /* Find the optional init function.  */
Packit 6c4009
	  void (*ifct) (void (*) (size_t, struct traced_file *))
Packit 6c4009
	    = __libc_dlsym (ni->library->lib_handle, init_name);
Packit 6c4009
	  if (ifct != NULL)
Packit 6c4009
	    {
Packit 6c4009
	      void (*cb) (size_t, struct traced_file *) = nscd_init_cb;
Packit 6c4009
#  ifdef PTR_DEMANGLE
Packit 6c4009
	      PTR_DEMANGLE (cb);
Packit 6c4009
#  endif
Packit 6c4009
	      ifct (cb);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
# endif
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
void *
Packit 6c4009
__nss_lookup_function (service_user *ni, const char *fct_name)
Packit 6c4009
{
Packit 6c4009
  void **found, *result;
Packit 6c4009
Packit 6c4009
  /* We now modify global data.  Protect it.  */
Packit 6c4009
  __libc_lock_lock (lock);
Packit 6c4009
Packit 6c4009
  /* Search the tree of functions previously requested.  Data in the
Packit 6c4009
     tree are `known_function' structures, whose first member is a
Packit 6c4009
     `const char *', the lookup key.  The search returns a pointer to
Packit 6c4009
     the tree node structure; the first member of the is a pointer to
Packit 6c4009
     our structure (i.e. what will be a `known_function'); since the
Packit 6c4009
     first member of that is the lookup key string, &FCT_NAME is close
Packit 6c4009
     enough to a pointer to our structure to use as a lookup key that
Packit 6c4009
     will be passed to `known_compare' (above).  */
Packit 6c4009
Packit 6c4009
  found = __tsearch (&fct_name, &ni->known, &known_compare);
Packit 6c4009
  if (found == NULL)
Packit 6c4009
    /* This means out-of-memory.  */
Packit 6c4009
    result = NULL;
Packit 6c4009
  else if (*found != &fct_name)
Packit 6c4009
    {
Packit 6c4009
      /* The search found an existing structure in the tree.  */
Packit 6c4009
      result = ((known_function *) *found)->fct_ptr;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
      PTR_DEMANGLE (result);
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* This name was not known before.  Now we have a node in the tree
Packit 6c4009
	 (in the proper sorted position for FCT_NAME) that points to
Packit 6c4009
	 &FCT_NAME instead of any real `known_function' structure.
Packit 6c4009
	 Allocate a new structure and fill it in.  */
Packit 6c4009
Packit 6c4009
      known_function *known = malloc (sizeof *known);
Packit 6c4009
      if (! known)
Packit 6c4009
	{
Packit 6c4009
#if !defined DO_STATIC_NSS || defined SHARED
Packit 6c4009
	remove_from_tree:
Packit 6c4009
#endif
Packit 6c4009
	  /* Oops.  We can't instantiate this node properly.
Packit 6c4009
	     Remove it from the tree.  */
Packit 6c4009
	  __tdelete (&fct_name, &ni->known, &known_compare);
Packit 6c4009
	  free (known);
Packit 6c4009
	  result = NULL;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Point the tree node at this new structure.  */
Packit 6c4009
	  *found = known;
Packit 6c4009
	  known->fct_name = fct_name;
Packit 6c4009
Packit 6c4009
#if !defined DO_STATIC_NSS || defined SHARED
Packit 6c4009
	  /* Load the appropriate library.  */
Packit 6c4009
	  if (nss_load_library (ni) != 0)
Packit 6c4009
	    /* This only happens when out of memory.  */
Packit 6c4009
	    goto remove_from_tree;
Packit 6c4009
Packit 6c4009
	  if (ni->library->lib_handle == (void *) -1l)
Packit 6c4009
	    /* Library not found => function not found.  */
Packit 6c4009
	    result = NULL;
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      /* Get the desired function.  */
Packit 6c4009
	      size_t namlen = (5 + strlen (ni->name) + 1
Packit 6c4009
			       + strlen (fct_name) + 1);
Packit 6c4009
	      char name[namlen];
Packit 6c4009
Packit 6c4009
	      /* Construct the function name.  */
Packit 6c4009
	      __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
Packit 6c4009
					    ni->name),
Packit 6c4009
				  "_"),
Packit 6c4009
			fct_name);
Packit 6c4009
Packit 6c4009
	      /* Look up the symbol.  */
Packit 6c4009
	      result = __libc_dlsym (ni->library->lib_handle, name);
Packit 6c4009
	    }
Packit 6c4009
#else
Packit 6c4009
	  /* We can't get function address dynamically in static linking. */
Packit 6c4009
	  {
Packit 6c4009
# define DEFINE_ENT(h,nm)						      \
Packit 6c4009
	    { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r },		      \
Packit 6c4009
	    { #h"_end"#nm"ent", _nss_##h##_end##nm##ent },		      \
Packit 6c4009
	    { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
Packit 6c4009
# define DEFINE_GET(h,nm)						      \
Packit 6c4009
	    { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
Packit 6c4009
# define DEFINE_GETBY(h,nm,ky)						      \
Packit 6c4009
	    { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
Packit 6c4009
	    static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
Packit 6c4009
	      {
Packit 6c4009
# include "function.def"
Packit 6c4009
		{ NULL, NULL }
Packit 6c4009
	      };
Packit 6c4009
	    size_t namlen = (5 + strlen (ni->name) + 1
Packit 6c4009
			     + strlen (fct_name) + 1);
Packit 6c4009
	    char name[namlen];
Packit 6c4009
Packit 6c4009
	    /* Construct the function name.  */
Packit 6c4009
	    __stpcpy (__stpcpy (__stpcpy (name, ni->name),
Packit 6c4009
				"_"),
Packit 6c4009
		      fct_name);
Packit 6c4009
Packit 6c4009
	    result = NULL;
Packit 6c4009
	    for (tp = &tbl[0]; tp->fname; tp++)
Packit 6c4009
	      if (strcmp (tp->fname, name) == 0)
Packit 6c4009
		{
Packit 6c4009
		  result = tp->fp;
Packit 6c4009
		  break;
Packit 6c4009
		}
Packit 6c4009
	  }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
	  /* Remember function pointer for later calls.  Even if null, we
Packit 6c4009
	     record it so a second try needn't search the library again.  */
Packit 6c4009
	  known->fct_ptr = result;
Packit 6c4009
#ifdef PTR_MANGLE
Packit 6c4009
	  PTR_MANGLE (known->fct_ptr);
Packit 6c4009
#endif
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Remove the lock.  */
Packit 6c4009
  __libc_lock_unlock (lock);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__nss_lookup_function)
Packit 6c4009
Packit 6c4009
Packit 6c4009
static name_database *
Packit 6c4009
nss_parse_file (const char *fname)
Packit 6c4009
{
Packit 6c4009
  FILE *fp;
Packit 6c4009
  name_database *result;
Packit 6c4009
  name_database_entry *last;
Packit 6c4009
  char *line;
Packit 6c4009
  size_t len;
Packit 6c4009
Packit 6c4009
  /* Open the configuration file.  */
Packit 6c4009
  fp = fopen (fname, "rce");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  /* No threads use this stream.  */
Packit 6c4009
  __fsetlocking (fp, FSETLOCKING_BYCALLER);
Packit 6c4009
Packit 6c4009
  result = (name_database *) malloc (sizeof (name_database));
Packit 6c4009
  if (result == NULL)
Packit 6c4009
    {
Packit 6c4009
      fclose (fp);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  result->entry = NULL;
Packit 6c4009
  result->library = NULL;
Packit 6c4009
  last = NULL;
Packit 6c4009
  line = NULL;
Packit 6c4009
  len = 0;
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      name_database_entry *this;
Packit 6c4009
      ssize_t n;
Packit 6c4009
Packit 6c4009
      n = __getline (&line, &len, fp);
Packit 6c4009
      if (n < 0)
Packit 6c4009
	break;
Packit 6c4009
      if (line[n - 1] == '\n')
Packit 6c4009
	line[n - 1] = '\0';
Packit 6c4009
Packit 6c4009
      /* Because the file format does not know any form of quoting we
Packit 6c4009
	 can search forward for the next '#' character and if found
Packit 6c4009
	 make it terminating the line.  */
Packit 6c4009
      *__strchrnul (line, '#') = '\0';
Packit 6c4009
Packit 6c4009
      /* If the line is blank it is ignored.  */
Packit 6c4009
      if (line[0] == '\0')
Packit 6c4009
	continue;
Packit 6c4009
Packit 6c4009
      /* Each line completely specifies the actions for a database.  */
Packit 6c4009
      this = nss_getline (line);
Packit 6c4009
      if (this != NULL)
Packit 6c4009
	{
Packit 6c4009
	  if (last != NULL)
Packit 6c4009
	    last->next = this;
Packit 6c4009
	  else
Packit 6c4009
	    result->entry = this;
Packit 6c4009
Packit 6c4009
	  last = this;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  while (!__feof_unlocked (fp));
Packit 6c4009
Packit 6c4009
  /* Free the buffer.  */
Packit 6c4009
  free (line);
Packit 6c4009
  /* Close configuration file.  */
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Read the source names:
Packit 6c4009
	`( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
Packit 6c4009
   */
Packit 6c4009
static service_user *
Packit 6c4009
nss_parse_service_list (const char *line)
Packit 6c4009
{
Packit 6c4009
  service_user *result = NULL, **nextp = &result;
Packit 6c4009
Packit 6c4009
  while (1)
Packit 6c4009
    {
Packit 6c4009
      service_user *new_service;
Packit 6c4009
      const char *name;
Packit 6c4009
Packit 6c4009
      while (isspace (line[0]))
Packit 6c4009
	++line;
Packit 6c4009
      if (line[0] == '\0')
Packit 6c4009
	/* No source specified.  */
Packit 6c4009
	return result;
Packit 6c4009
Packit 6c4009
      /* Read <source> identifier.  */
Packit 6c4009
      name = line;
Packit 6c4009
      while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
Packit 6c4009
	++line;
Packit 6c4009
      if (name == line)
Packit 6c4009
	return result;
Packit 6c4009
Packit 6c4009
Packit 6c4009
      new_service = (service_user *) malloc (sizeof (service_user)
Packit 6c4009
					     + (line - name + 1));
Packit 6c4009
      if (new_service == NULL)
Packit 6c4009
	return result;
Packit 6c4009
Packit 6c4009
      *((char *) __mempcpy (new_service->name, name, line - name)) = '\0';
Packit 6c4009
Packit 6c4009
      /* Set default actions.  */
Packit 6c4009
      new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
Packit 6c4009
      new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
Packit 6c4009
      new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
Packit 6c4009
      new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
Packit 6c4009
      new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
Packit 6c4009
      new_service->library = NULL;
Packit 6c4009
      new_service->known = NULL;
Packit 6c4009
      new_service->next = NULL;
Packit 6c4009
Packit 6c4009
      while (isspace (line[0]))
Packit 6c4009
	++line;
Packit 6c4009
Packit 6c4009
      if (line[0] == '[')
Packit 6c4009
	{
Packit 6c4009
	  /* Read criterions.  */
Packit 6c4009
	  do
Packit 6c4009
	    ++line;
Packit 6c4009
	  while (line[0] != '\0' && isspace (line[0]));
Packit 6c4009
Packit 6c4009
	  do
Packit 6c4009
	    {
Packit 6c4009
	      int not;
Packit 6c4009
	      enum nss_status status;
Packit 6c4009
	      lookup_actions action;
Packit 6c4009
Packit 6c4009
	      /* Grok ! before name to mean all statii but that one.  */
Packit 6c4009
	      not = line[0] == '!';
Packit 6c4009
	      if (not)
Packit 6c4009
		++line;
Packit 6c4009
Packit 6c4009
	      /* Read status name.  */
Packit 6c4009
	      name = line;
Packit 6c4009
	      while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
Packit 6c4009
		     && line[0] != ']')
Packit 6c4009
		++line;
Packit 6c4009
Packit 6c4009
	      /* Compare with known statii.  */
Packit 6c4009
	      if (line - name == 7)
Packit 6c4009
		{
Packit 6c4009
		  if (__strncasecmp (name, "SUCCESS", 7) == 0)
Packit 6c4009
		    status = NSS_STATUS_SUCCESS;
Packit 6c4009
		  else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
Packit 6c4009
		    status = NSS_STATUS_UNAVAIL;
Packit 6c4009
		  else
Packit 6c4009
		    goto finish;
Packit 6c4009
		}
Packit 6c4009
	      else if (line - name == 8)
Packit 6c4009
		{
Packit 6c4009
		  if (__strncasecmp (name, "NOTFOUND", 8) == 0)
Packit 6c4009
		    status = NSS_STATUS_NOTFOUND;
Packit 6c4009
		  else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
Packit 6c4009
		    status = NSS_STATUS_TRYAGAIN;
Packit 6c4009
		  else
Packit 6c4009
		    goto finish;
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
		goto finish;
Packit 6c4009
Packit 6c4009
	      while (isspace (line[0]))
Packit 6c4009
		++line;
Packit 6c4009
	      if (line[0] != '=')
Packit 6c4009
		goto finish;
Packit 6c4009
	      do
Packit 6c4009
		++line;
Packit 6c4009
	      while (isspace (line[0]));
Packit 6c4009
Packit 6c4009
	      name = line;
Packit 6c4009
	      while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
Packit 6c4009
		     && line[0] != ']')
Packit 6c4009
		++line;
Packit 6c4009
Packit 6c4009
	      if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
Packit 6c4009
		action = NSS_ACTION_RETURN;
Packit 6c4009
	      else if (line - name == 8
Packit 6c4009
		       && __strncasecmp (name, "CONTINUE", 8) == 0)
Packit 6c4009
		action = NSS_ACTION_CONTINUE;
Packit 6c4009
	      else if (line - name == 5
Packit 6c4009
		       && __strncasecmp (name, "MERGE", 5) == 0)
Packit 6c4009
		action = NSS_ACTION_MERGE;
Packit 6c4009
	      else
Packit 6c4009
		goto finish;
Packit 6c4009
Packit 6c4009
	      if (not)
Packit 6c4009
		{
Packit 6c4009
		  /* Save the current action setting for this status,
Packit 6c4009
		     set them all to the given action, and reset this one.  */
Packit 6c4009
		  const lookup_actions save = new_service->actions[2 + status];
Packit 6c4009
		  new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
Packit 6c4009
		  new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
Packit 6c4009
		  new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
Packit 6c4009
		  new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
Packit 6c4009
		  new_service->actions[2 + status] = save;
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
		new_service->actions[2 + status] = action;
Packit 6c4009
Packit 6c4009
	      /* Skip white spaces.  */
Packit 6c4009
	      while (isspace (line[0]))
Packit 6c4009
		++line;
Packit 6c4009
	    }
Packit 6c4009
	  while (line[0] != ']');
Packit 6c4009
Packit 6c4009
	  /* Skip the ']'.  */
Packit 6c4009
	  ++line;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      *nextp = new_service;
Packit 6c4009
      nextp = &new_service->next;
Packit 6c4009
      continue;
Packit 6c4009
Packit 6c4009
    finish:
Packit 6c4009
      free (new_service);
Packit 6c4009
      return result;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static name_database_entry *
Packit 6c4009
nss_getline (char *line)
Packit 6c4009
{
Packit 6c4009
  const char *name;
Packit 6c4009
  name_database_entry *result;
Packit 6c4009
  size_t len;
Packit 6c4009
Packit 6c4009
  /* Ignore leading white spaces.  ATTENTION: this is different from
Packit 6c4009
     what is implemented in Solaris.  The Solaris man page says a line
Packit 6c4009
     beginning with a white space character is ignored.  We regard
Packit 6c4009
     this as just another misfeature in Solaris.  */
Packit 6c4009
  while (isspace (line[0]))
Packit 6c4009
    ++line;
Packit 6c4009
Packit 6c4009
  /* Recognize `<database> ":"'.  */
Packit 6c4009
  name = line;
Packit 6c4009
  while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
Packit 6c4009
    ++line;
Packit 6c4009
  if (line[0] == '\0' || name == line)
Packit 6c4009
    /* Syntax error.  */
Packit 6c4009
    return NULL;
Packit 6c4009
  *line++ = '\0';
Packit 6c4009
Packit 6c4009
  len = strlen (name) + 1;
Packit 6c4009
Packit 6c4009
  result = (name_database_entry *) malloc (sizeof (name_database_entry) + len);
Packit 6c4009
  if (result == NULL)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  /* Save the database name.  */
Packit 6c4009
  memcpy (result->name, name, len);
Packit 6c4009
Packit 6c4009
  /* Parse the list of services.  */
Packit 6c4009
  result->service = nss_parse_service_list (line);
Packit 6c4009
Packit 6c4009
  result->next = NULL;
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#if !defined DO_STATIC_NSS || defined SHARED
Packit 6c4009
static service_library *
Packit 6c4009
nss_new_service (name_database *database, const char *name)
Packit 6c4009
{
Packit 6c4009
  service_library **currentp = &database->library;
Packit 6c4009
Packit 6c4009
  while (*currentp != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (strcmp ((*currentp)->name, name) == 0)
Packit 6c4009
	return *currentp;
Packit 6c4009
      currentp = &(*currentp)->next;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* We have to add the new service.  */
Packit 6c4009
  *currentp = (service_library *) malloc (sizeof (service_library));
Packit 6c4009
  if (*currentp == NULL)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  (*currentp)->name = name;
Packit 6c4009
  (*currentp)->lib_handle = NULL;
Packit 6c4009
  (*currentp)->next = NULL;
Packit 6c4009
Packit 6c4009
  return *currentp;
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
#if defined SHARED && defined USE_NSCD
Packit 6c4009
/* Load all libraries for the service.  */
Packit 6c4009
static void
Packit 6c4009
nss_load_all_libraries (const char *service, const char *def)
Packit 6c4009
{
Packit 6c4009
  service_user *ni = NULL;
Packit 6c4009
Packit Service cbad28
  if (__nss_database_lookup2 (service, NULL, def, &ni) == 0)
Packit 6c4009
    while (ni != NULL)
Packit 6c4009
      {
Packit 6c4009
	nss_load_library (ni);
Packit 6c4009
	ni = ni->next;
Packit 6c4009
      }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Called by nscd and nscd alone.  */
Packit 6c4009
void
Packit 6c4009
__nss_disable_nscd (void (*cb) (size_t, struct traced_file *))
Packit 6c4009
{
Packit 6c4009
# ifdef PTR_MANGLE
Packit 6c4009
  PTR_MANGLE (cb);
Packit 6c4009
# endif
Packit 6c4009
  nscd_init_cb = cb;
Packit 6c4009
  is_nscd = true;
Packit 6c4009
Packit 6c4009
  /* Find all the relevant modules so that the init functions are called.  */
Packit 6c4009
  nss_load_all_libraries ("passwd", DEFAULT_CONFIG);
Packit 6c4009
  nss_load_all_libraries ("group", DEFAULT_CONFIG);
Packit 6c4009
  nss_load_all_libraries ("hosts", "dns [!UNAVAIL=return] files");
Packit 6c4009
  nss_load_all_libraries ("services", NULL);
Packit 6c4009
Packit 6c4009
  /* Disable all uses of NSCD.  */
Packit 6c4009
  __nss_not_use_nscd_passwd = -1;
Packit 6c4009
  __nss_not_use_nscd_group = -1;
Packit 6c4009
  __nss_not_use_nscd_hosts = -1;
Packit 6c4009
  __nss_not_use_nscd_services = -1;
Packit 6c4009
  __nss_not_use_nscd_netgroup = -1;
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
free_database_entries (name_database_entry *entry)
Packit 6c4009
{
Packit 6c4009
  while (entry != NULL)
Packit 6c4009
    {
Packit 6c4009
      name_database_entry *olde = entry;
Packit 6c4009
      service_user *service = entry->service;
Packit 6c4009
Packit 6c4009
      while (service != NULL)
Packit 6c4009
	{
Packit 6c4009
	  service_user *olds = service;
Packit 6c4009
Packit 6c4009
	  if (service->known != NULL)
Packit 6c4009
	    __tdestroy (service->known, free);
Packit 6c4009
Packit 6c4009
	  service = service->next;
Packit 6c4009
	  free (olds);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      entry = entry->next;
Packit 6c4009
      free (olde);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Free all resources if necessary.  */
Packit 6c4009
libc_freeres_fn (free_defconfig)
Packit 6c4009
{
Packit 6c4009
  name_database_entry *entry = defconfig_entries;
Packit 6c4009
Packit 6c4009
  if (entry == NULL)
Packit 6c4009
    /* defconfig was not used.  */
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  /* Don't disturb ongoing other threads (if there are any).  */
Packit 6c4009
  defconfig_entries = NULL;
Packit 6c4009
Packit 6c4009
  free_database_entries (entry);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
libc_freeres_fn (free_mem)
Packit 6c4009
{
Packit 6c4009
  name_database *top = service_table;
Packit 6c4009
  service_library *library;
Packit 6c4009
Packit 6c4009
  if (top == NULL)
Packit 6c4009
    /* Maybe we have not read the nsswitch.conf file.  */
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  /* Don't disturb ongoing other threads (if there are any).  */
Packit 6c4009
  service_table = NULL;
Packit 6c4009
Packit 6c4009
  free_database_entries (top->entry);
Packit 6c4009
Packit 6c4009
  library = top->library;
Packit 6c4009
  while (library != NULL)
Packit 6c4009
    {
Packit 6c4009
      service_library *oldl = library;
Packit 6c4009
Packit 6c4009
      if (library->lib_handle && library->lib_handle != (void *) -1l)
Packit 6c4009
	__libc_dlclose (library->lib_handle);
Packit 6c4009
Packit 6c4009
      library = library->next;
Packit 6c4009
      free (oldl);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  free (top);
Packit 6c4009
}