Blame nscd/nscd_getpw_r.c

Packit 6c4009
/* Copyright (C) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
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 <pwd.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
#include <sys/uio.h>
Packit 6c4009
#include <sys/un.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
#include <_itoa.h>
Packit 6c4009
Packit 6c4009
#include "nscd-client.h"
Packit 6c4009
#include "nscd_proto.h"
Packit 6c4009
Packit 6c4009
int __nss_not_use_nscd_passwd;
Packit 6c4009
Packit 6c4009
static int nscd_getpw_r (const char *key, size_t keylen, request_type type,
Packit 6c4009
			 struct passwd *resultbuf, char *buffer,
Packit 6c4009
			 size_t buflen, struct passwd **result);
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__nscd_getpwnam_r (const char *name, struct passwd *resultbuf, char *buffer,
Packit 6c4009
		   size_t buflen, struct passwd **result)
Packit 6c4009
{
Packit 6c4009
  if (name == NULL)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  return nscd_getpw_r (name, strlen (name) + 1, GETPWBYNAME, resultbuf,
Packit 6c4009
		       buffer, buflen, result);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__nscd_getpwuid_r (uid_t uid, struct passwd *resultbuf, char *buffer,
Packit 6c4009
		   size_t buflen, struct passwd **result)
Packit 6c4009
{
Packit 6c4009
  char buf[3 * sizeof (uid_t)];
Packit 6c4009
  buf[sizeof (buf) - 1] = '\0';
Packit 6c4009
  char *cp = _itoa_word (uid, buf + sizeof (buf) - 1, 10, 0);
Packit 6c4009
Packit 6c4009
  return nscd_getpw_r (cp, buf + sizeof (buf) - cp, GETPWBYUID, resultbuf,
Packit 6c4009
		       buffer, buflen, result);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
libc_locked_map_ptr (static, map_handle);
Packit 6c4009
/* Note that we only free the structure if necessary.  The memory
Packit 6c4009
   mapping is not removed since it is not visible to the malloc
Packit 6c4009
   handling.  */
Packit 6c4009
libc_freeres_fn (pw_map_free)
Packit 6c4009
{
Packit 6c4009
  if (map_handle.mapped != NO_MAPPING)
Packit 6c4009
    {
Packit 6c4009
      void *p = map_handle.mapped;
Packit 6c4009
      map_handle.mapped = NO_MAPPING;
Packit 6c4009
      free (p);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
nscd_getpw_r (const char *key, size_t keylen, request_type type,
Packit 6c4009
	      struct passwd *resultbuf, char *buffer, size_t buflen,
Packit 6c4009
	      struct passwd **result)
Packit 6c4009
{
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 (GETFDPW, "passwd", &map_handle, &gc_cycle);
Packit 6c4009
Packit 6c4009
 retry:;
Packit 6c4009
  const char *pw_name = NULL;
Packit 6c4009
  int retval = -1;
Packit 6c4009
  const char *recend = (const char *) ~UINTMAX_C (0);
Packit 6c4009
  pw_response_header pw_resp;
Packit 6c4009
Packit 6c4009
  if (mapped != NO_MAPPING)
Packit 6c4009
    {
Packit 6c4009
      struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
Packit 6c4009
						    sizeof pw_resp);
Packit 6c4009
      if (found != NULL)
Packit 6c4009
	{
Packit 6c4009
	  pw_name = (const char *) (&found->data[0].pwdata + 1);
Packit 6c4009
	  pw_resp = found->data[0].pwdata;
Packit 6c4009
	  recend = (const char *) found->data + found->recsize;
Packit 6c4009
	  /* Now check if we can trust pw_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
  int sock = -1;
Packit 6c4009
  if (pw_name == NULL)
Packit 6c4009
    {
Packit 6c4009
      sock = __nscd_open_socket (key, keylen, type, &pw_resp,
Packit 6c4009
				 sizeof (pw_resp));
Packit 6c4009
      if (sock == -1)
Packit 6c4009
	{
Packit 6c4009
	  __nss_not_use_nscd_passwd = 1;
Packit 6c4009
	  goto out;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* No value found so far.  */
Packit 6c4009
  *result = NULL;
Packit 6c4009
Packit 6c4009
  if (__glibc_unlikely (pw_resp.found == -1))
Packit 6c4009
    {
Packit 6c4009
      /* The daemon does not cache this database.  */
Packit 6c4009
      __nss_not_use_nscd_passwd = 1;
Packit 6c4009
      goto out_close;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pw_resp.found == 1)
Packit 6c4009
    {
Packit 6c4009
      /* Set the information we already have.  */
Packit 6c4009
      resultbuf->pw_uid = pw_resp.pw_uid;
Packit 6c4009
      resultbuf->pw_gid = pw_resp.pw_gid;
Packit 6c4009
Packit 6c4009
      char *p = buffer;
Packit 6c4009
      /* get pw_name */
Packit 6c4009
      resultbuf->pw_name = p;
Packit 6c4009
      p += pw_resp.pw_name_len;
Packit 6c4009
      /* get pw_passwd */
Packit 6c4009
      resultbuf->pw_passwd = p;
Packit 6c4009
      p += pw_resp.pw_passwd_len;
Packit 6c4009
      /* get pw_gecos */
Packit 6c4009
      resultbuf->pw_gecos = p;
Packit 6c4009
      p += pw_resp.pw_gecos_len;
Packit 6c4009
      /* get pw_dir */
Packit 6c4009
      resultbuf->pw_dir = p;
Packit 6c4009
      p += pw_resp.pw_dir_len;
Packit 6c4009
      /* get pw_pshell */
Packit 6c4009
      resultbuf->pw_shell = p;
Packit 6c4009
      p += pw_resp.pw_shell_len;
Packit 6c4009
Packit 6c4009
      ssize_t total = p - buffer;
Packit 6c4009
      if (__glibc_unlikely (pw_name + total > recend))
Packit 6c4009
	goto out_close;
Packit 6c4009
      if (__glibc_unlikely (buflen < total))
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (ERANGE);
Packit 6c4009
	  retval = ERANGE;
Packit 6c4009
	  goto out_close;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      retval = 0;
Packit 6c4009
      if (pw_name == NULL)
Packit 6c4009
	{
Packit 6c4009
	  ssize_t nbytes = __readall (sock, buffer, total);
Packit 6c4009
Packit 6c4009
	  if (__glibc_unlikely (nbytes != total))
Packit 6c4009
	    {
Packit 6c4009
	      /* The `errno' to some value != ERANGE.  */
Packit 6c4009
	      __set_errno (ENOENT);
Packit 6c4009
	      retval = ENOENT;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    *result = resultbuf;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Copy the various strings.  */
Packit 6c4009
	  memcpy (resultbuf->pw_name, pw_name, total);
Packit 6c4009
Packit 6c4009
	  /* Try to detect corrupt databases.  */
Packit 6c4009
	  if (resultbuf->pw_name[pw_resp.pw_name_len - 1] != '\0'
Packit 6c4009
	      || resultbuf->pw_passwd[pw_resp.pw_passwd_len - 1] != '\0'
Packit 6c4009
	      || resultbuf->pw_gecos[pw_resp.pw_gecos_len - 1] != '\0'
Packit 6c4009
	      || resultbuf->pw_dir[pw_resp.pw_dir_len - 1] != '\0'
Packit 6c4009
	      || resultbuf->pw_shell[pw_resp.pw_shell_len - 1] != '\0')
Packit 6c4009
	    {
Packit 6c4009
	      /* We cannot use the database.  */
Packit 6c4009
	      retval = mapped->head->gc_cycle != gc_cycle ? -2 : -1;
Packit 6c4009
	      goto out_close;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  *result = resultbuf;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
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
	goto retry;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return retval;
Packit 6c4009
}