Blame nscd/nscd_getai.c

Packit 6c4009
/* Copyright (C) 2004-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
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 <assert.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
Packit 6c4009
#include "nscd-client.h"
Packit 6c4009
#include "nscd_proto.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Define in nscd_gethst_r.c.  */
Packit 6c4009
extern int __nss_not_use_nscd_hosts;
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* We use the mapping from nscd_gethst.  */
Packit 6c4009
libc_locked_map_ptr (extern, __hst_map_handle) attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Defined in nscd_gethst_r.c.  */
Packit 6c4009
extern int __nss_have_localdomain attribute_hidden;
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__nscd_getai (const char *key, struct nscd_ai_result **result, int *h_errnop)
Packit 6c4009
{
Packit 6c4009
  if (__glibc_unlikely (__nss_have_localdomain >= 0))
Packit 6c4009
    {
Packit 6c4009
      if (__nss_have_localdomain == 0)
Packit 6c4009
	__nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
Packit 6c4009
      if (__nss_have_localdomain > 0)
Packit 6c4009
	{
Packit 6c4009
	  __nss_not_use_nscd_hosts = 1;
Packit 6c4009
	  return -1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  size_t keylen = strlen (key) + 1;
Packit 6c4009
  int gc_cycle;
Packit 6c4009
  int nretries = 0;
Packit 6c4009
Packit 6c4009
  /* If the mapping is available, try to search there instead of
Packit 6c4009
     communicating with the nscd.  */
Packit 6c4009
  struct mapped_database *mapped;
Packit 6c4009
  mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
Packit 6c4009
			       &gc_cycle);
Packit 6c4009
Packit 6c4009
 retry:;
Packit 6c4009
  struct nscd_ai_result *resultbuf = NULL;
Packit 6c4009
  const char *recend = (const char *) ~UINTMAX_C (0);
Packit 6c4009
  char *respdata = NULL;
Packit 6c4009
  int retval = -1;
Packit 6c4009
  int sock = -1;
Packit 6c4009
  ai_response_header ai_resp;
Packit 6c4009
Packit 6c4009
  if (mapped != NO_MAPPING)
Packit 6c4009
    {
Packit 6c4009
      struct datahead *found = __nscd_cache_search (GETAI, key, keylen,
Packit 6c4009
						    mapped, sizeof ai_resp);
Packit 6c4009
      if (found != NULL)
Packit 6c4009
	{
Packit 6c4009
	  respdata = (char *) (&found->data[0].aidata + 1);
Packit 6c4009
	  ai_resp = found->data[0].aidata;
Packit 6c4009
	  recend = (const char *) found->data + found->recsize;
Packit 6c4009
	  /* Now check if we can trust ai_resp fields.  If GC is
Packit 6c4009
	     in progress, it can contain anything.  */
Packit 6c4009
	  if (mapped->head->gc_cycle != gc_cycle)
Packit 6c4009
	    {
Packit 6c4009
	      retval = -2;
Packit 6c4009
	      goto out;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* If we do not have the cache mapped, try to get the data over the
Packit 6c4009
     socket.  */
Packit 6c4009
  if (respdata == NULL)
Packit 6c4009
    {
Packit 6c4009
      sock = __nscd_open_socket (key, keylen, GETAI, &ai_resp,
Packit 6c4009
				 sizeof (ai_resp));
Packit 6c4009
      if (sock == -1)
Packit 6c4009
	{
Packit 6c4009
	  /* nscd not running or wrong version.  */
Packit 6c4009
	  __nss_not_use_nscd_hosts = 1;
Packit 6c4009
	  goto out;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (ai_resp.found == 1)
Packit 6c4009
    {
Packit 6c4009
      size_t datalen = ai_resp.naddrs + ai_resp.addrslen + ai_resp.canonlen;
Packit 6c4009
Packit 6c4009
      /* This check really only affects the case where the data
Packit 6c4009
	 comes from the mapped cache.  */
Packit 6c4009
      if (respdata + datalen > recend)
Packit 6c4009
	{
Packit 6c4009
	  assert (sock == -1);
Packit 6c4009
	  goto out;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Create result.  */
Packit 6c4009
      resultbuf = (struct nscd_ai_result *) malloc (sizeof (*resultbuf)
Packit 6c4009
						    + datalen);
Packit 6c4009
      if (resultbuf == NULL)
Packit 6c4009
	{
Packit 6c4009
	  *h_errnop = NETDB_INTERNAL;
Packit 6c4009
	  goto out_close;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Set up the data structure, including pointers.  */
Packit 6c4009
      resultbuf->naddrs = ai_resp.naddrs;
Packit 6c4009
      resultbuf->addrs = (char *) (resultbuf + 1);
Packit 6c4009
      resultbuf->family = (uint8_t *) (resultbuf->addrs + ai_resp.addrslen);
Packit 6c4009
      if (ai_resp.canonlen != 0)
Packit 6c4009
	resultbuf->canon = (char *) (resultbuf->family + resultbuf->naddrs);
Packit 6c4009
      else
Packit 6c4009
	resultbuf->canon = NULL;
Packit 6c4009
Packit 6c4009
      if (respdata == NULL)
Packit 6c4009
	{
Packit 6c4009
	  /* Read the data from the socket.  */
Packit 6c4009
	  if ((size_t) __readall (sock, resultbuf + 1, datalen) == datalen)
Packit 6c4009
	    {
Packit 6c4009
	      retval = 0;
Packit 6c4009
	      *result = resultbuf;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      free (resultbuf);
Packit 6c4009
	      *h_errnop = NETDB_INTERNAL;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Copy the data in the block.  */
Packit 6c4009
	  memcpy (resultbuf + 1, respdata, datalen);
Packit 6c4009
Packit 6c4009
	  /* Try to detect corrupt databases.  */
Packit 6c4009
	  if (resultbuf->canon != NULL
Packit 6c4009
	      && resultbuf->canon[ai_resp.canonlen - 1] != '\0')
Packit 6c4009
	    /* We cannot use the database.  */
Packit 6c4009
	    {
Packit 6c4009
	      if (mapped->head->gc_cycle != gc_cycle)
Packit 6c4009
		retval = -2;
Packit 6c4009
	      else
Packit 6c4009
		free (resultbuf);
Packit 6c4009
	      goto out_close;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  retval = 0;
Packit 6c4009
	  *result = resultbuf;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (__glibc_unlikely (ai_resp.found == -1))
Packit 6c4009
	{
Packit 6c4009
	  /* The daemon does not cache this database.  */
Packit 6c4009
	  __nss_not_use_nscd_hosts = 1;
Packit 6c4009
	  goto out_close;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Store the error number.  */
Packit 6c4009
      *h_errnop = ai_resp.error;
Packit 6c4009
Packit 6c4009
      /* Set errno to 0 to indicate no error, just no found record.  */
Packit 6c4009
      __set_errno (0);
Packit 6c4009
      /* Even though we have not found anything, the result is zero.  */
Packit 6c4009
      retval = 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
 out_close:
Packit 6c4009
  if (sock != -1)
Packit 6c4009
    __close_nocancel_nostatus (sock);
Packit 6c4009
 out:
Packit 6c4009
  if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
Packit 6c4009
    {
Packit 6c4009
      /* When we come here this means there has been a GC cycle while we
Packit 6c4009
	 were looking for the data.  This means the data might have been
Packit 6c4009
	 inconsistent.  Retry if possible.  */
Packit 6c4009
      if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
Packit 6c4009
	{
Packit 6c4009
	  /* nscd is just running gc now.  Disable using the mapping.  */
Packit 6c4009
	  if (atomic_decrement_val (&mapped->counter) == 0)
Packit 6c4009
	    __nscd_unmap (mapped);
Packit 6c4009
	  mapped = NO_MAPPING;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (retval != -1)
Packit 6c4009
	{
Packit 6c4009
	  *result = NULL;
Packit 6c4009
	  free (resultbuf);
Packit 6c4009
	  goto retry;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return retval;
Packit 6c4009
}