Blame nis/nss_nisplus/nisplus-grp.c

Packit 6c4009
/* Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
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 <atomic.h>
Packit 6c4009
#include <nss.h>
Packit 6c4009
#include <grp.h>
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include <rpcsvc/nis.h>
Packit 6c4009
Packit 6c4009
#include "nss-nisplus.h"
Packit 6c4009
#include "nisplus-parser.h"
Packit 6c4009
#include <libnsl.h>
Packit 6c4009
#include <nis_intern.h>
Packit 6c4009
#include <nis_xdr.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
__libc_lock_define_initialized (static, lock);
Packit 6c4009
Packit 6c4009
/* Connection information.  */
Packit 6c4009
static ib_request *ibreq;
Packit 6c4009
static directory_obj *dir;
Packit 6c4009
static dir_binding bptr;
Packit 6c4009
static char *tablepath;
Packit 6c4009
static char *tableptr;
Packit 6c4009
/* Cursor.  */
Packit 6c4009
static netobj cursor;
Packit 6c4009
Packit 6c4009
Packit 6c4009
nis_name grp_tablename_val attribute_hidden;
Packit 6c4009
size_t grp_tablename_len attribute_hidden;
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_grp_create_tablename (int *errnop)
Packit 6c4009
{
Packit 6c4009
  if (grp_tablename_val == NULL)
Packit 6c4009
    {
Packit 6c4009
      const char *local_dir = nis_local_directory ();
Packit 6c4009
      size_t local_dir_len = strlen (local_dir);
Packit 6c4009
      static const char prefix[] = "group.org_dir.";
Packit 6c4009
Packit 6c4009
      char *p = malloc (sizeof (prefix) + local_dir_len);
Packit 6c4009
      if (p == NULL)
Packit 6c4009
	{
Packit 6c4009
	  *errnop = errno;
Packit 6c4009
	  return NSS_STATUS_TRYAGAIN;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
Packit 6c4009
Packit 6c4009
      grp_tablename_len = sizeof (prefix) - 1 + local_dir_len;
Packit 6c4009
Packit 6c4009
      atomic_write_barrier ();
Packit 6c4009
Packit 6c4009
      if (atomic_compare_and_exchange_bool_acq (&grp_tablename_val, p, NULL))
Packit 6c4009
	{
Packit 6c4009
	  /* Another thread already installed the value.  */
Packit 6c4009
	  free (p);
Packit 6c4009
	  grp_tablename_len = strlen (grp_tablename_val);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NSS_STATUS_SUCCESS;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
internal_endgrent (void)
Packit 6c4009
{
Packit 6c4009
  __nisbind_destroy (&bptr);
Packit 6c4009
  memset (&bptr, '\0', sizeof (bptr));
Packit 6c4009
Packit 6c4009
  nis_free_directory (dir);
Packit 6c4009
  dir = NULL;
Packit 6c4009
Packit 6c4009
  nis_free_request (ibreq);
Packit 6c4009
  ibreq = NULL;
Packit 6c4009
Packit 6c4009
  xdr_free ((xdrproc_t) xdr_netobj, (char *) &cursor);
Packit 6c4009
  memset (&cursor, '\0', sizeof (cursor));
Packit 6c4009
Packit 6c4009
  free (tablepath);
Packit 6c4009
  tableptr = tablepath = NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static enum nss_status
Packit 6c4009
internal_setgrent (int *errnop)
Packit 6c4009
{
Packit 6c4009
  enum nss_status status = NSS_STATUS_SUCCESS;
Packit 6c4009
Packit 6c4009
  if (grp_tablename_val == NULL)
Packit 6c4009
    status = _nss_grp_create_tablename (errnop);
Packit 6c4009
Packit 6c4009
  if (status == NSS_STATUS_SUCCESS)
Packit 6c4009
    {
Packit 6c4009
      ibreq = __create_ib_request (grp_tablename_val, 0);
Packit 6c4009
      if (ibreq == NULL)
Packit 6c4009
	{
Packit 6c4009
	  *errnop = errno;
Packit 6c4009
	  return NSS_STATUS_TRYAGAIN;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      nis_error retcode = __prepare_niscall (grp_tablename_val, &dir, &bptr, 0);
Packit 6c4009
      if (retcode != NIS_SUCCESS)
Packit 6c4009
	{
Packit 6c4009
	  nis_free_request (ibreq);
Packit 6c4009
	  ibreq = NULL;
Packit 6c4009
	  status = niserr2nss (retcode);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return status;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_nisplus_setgrent (int stayopen)
Packit 6c4009
{
Packit 6c4009
  enum nss_status status;
Packit 6c4009
Packit 6c4009
  __libc_lock_lock (lock);
Packit 6c4009
Packit 6c4009
  internal_endgrent ();
Packit 6c4009
Packit 6c4009
  // XXX We need to be able to set errno.  Pass in new parameter.
Packit 6c4009
  int err;
Packit 6c4009
  status = internal_setgrent (&err;;
Packit 6c4009
Packit 6c4009
  __libc_lock_unlock (lock);
Packit 6c4009
Packit 6c4009
  return status;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_nisplus_endgrent (void)
Packit 6c4009
{
Packit 6c4009
  __libc_lock_lock (lock);
Packit 6c4009
Packit 6c4009
  internal_endgrent ();
Packit 6c4009
Packit 6c4009
  __libc_lock_unlock (lock);
Packit 6c4009
Packit 6c4009
  return NSS_STATUS_SUCCESS;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static enum nss_status
Packit 6c4009
internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen,
Packit 6c4009
			     int *errnop)
Packit 6c4009
{
Packit 6c4009
  int parse_res = -1;
Packit 6c4009
  enum nss_status retval = NSS_STATUS_SUCCESS;
Packit 6c4009
Packit 6c4009
  /* Get the next entry until we found a correct one. */
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      nis_error status;
Packit 6c4009
      nis_result result;
Packit 6c4009
      memset (&result, '\0', sizeof (result));
Packit 6c4009
Packit 6c4009
      if (cursor.n_bytes == NULL)
Packit 6c4009
	{
Packit 6c4009
	  if (ibreq == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      retval = internal_setgrent (errnop);
Packit 6c4009
	      if (retval != NSS_STATUS_SUCCESS)
Packit 6c4009
		return retval;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  status = __do_niscall3 (&bptr, NIS_IBFIRST,
Packit 6c4009
				  (xdrproc_t) _xdr_ib_request,
Packit 6c4009
				  (caddr_t) ibreq,
Packit 6c4009
				  (xdrproc_t) _xdr_nis_result,
Packit 6c4009
				  (caddr_t) &result,
Packit 6c4009
				  0, NULL);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  ibreq->ibr_cookie.n_bytes = cursor.n_bytes;
Packit 6c4009
	  ibreq->ibr_cookie.n_len = cursor.n_len;
Packit 6c4009
Packit 6c4009
	  status = __do_niscall3 (&bptr, NIS_IBNEXT,
Packit 6c4009
				  (xdrproc_t) _xdr_ib_request,
Packit 6c4009
				  (caddr_t) ibreq,
Packit 6c4009
				  (xdrproc_t) _xdr_nis_result,
Packit 6c4009
				  (caddr_t) &result,
Packit 6c4009
				  0, NULL);
Packit 6c4009
Packit 6c4009
	  ibreq->ibr_cookie.n_bytes = NULL;
Packit 6c4009
	  ibreq->ibr_cookie.n_len = 0;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (status != NIS_SUCCESS)
Packit 6c4009
	return niserr2nss (status);
Packit 6c4009
Packit 6c4009
      if (NIS_RES_STATUS (&result) == NIS_NOTFOUND)
Packit 6c4009
	{
Packit 6c4009
	  /* No more entries on this server.  This means we have to go
Packit 6c4009
	     to the next server on the path.  */
Packit 6c4009
	  status = __follow_path (&tablepath, &tableptr, ibreq, &bptr);
Packit 6c4009
	  if (status != NIS_SUCCESS)
Packit 6c4009
	    return niserr2nss (status);
Packit 6c4009
Packit 6c4009
	  directory_obj *newdir = NULL;
Packit 6c4009
	  dir_binding newbptr;
Packit 6c4009
	  status = __prepare_niscall (ibreq->ibr_name, &newdir, &newbptr, 0);
Packit 6c4009
	  if (status != NIS_SUCCESS)
Packit 6c4009
	    return niserr2nss (status);
Packit 6c4009
Packit 6c4009
	  nis_free_directory (dir);
Packit 6c4009
	  dir = newdir;
Packit 6c4009
	  __nisbind_destroy (&bptr);
Packit 6c4009
	  bptr = newbptr;
Packit 6c4009
Packit 6c4009
	  xdr_free ((xdrproc_t) xdr_netobj, (char *) &result.cookie);
Packit 6c4009
	  result.cookie.n_bytes = NULL;
Packit 6c4009
	  result.cookie.n_len = 0;
Packit 6c4009
	  parse_res = 0;
Packit 6c4009
	  goto next;
Packit 6c4009
	}
Packit 6c4009
      else if (NIS_RES_STATUS (&result) != NIS_SUCCESS)
Packit 6c4009
	return niserr2nss (NIS_RES_STATUS (&result));
Packit 6c4009
Packit 6c4009
      parse_res = _nss_nisplus_parse_grent (&result, gr,
Packit 6c4009
					    buffer, buflen, errnop);
Packit 6c4009
      if (__glibc_unlikely (parse_res == -1))
Packit 6c4009
	{
Packit 6c4009
	  *errnop = ERANGE;
Packit 6c4009
	  retval = NSS_STATUS_TRYAGAIN;
Packit 6c4009
	  goto freeres;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
    next:
Packit 6c4009
      /* Free the old cursor.  */
Packit 6c4009
      xdr_free ((xdrproc_t) xdr_netobj, (char *) &cursor);
Packit 6c4009
      /* Remember the new one.  */
Packit 6c4009
      cursor.n_bytes = result.cookie.n_bytes;
Packit 6c4009
      cursor.n_len = result.cookie.n_len;
Packit 6c4009
      /* Free the result structure.  NB: we do not remove the cookie.  */
Packit 6c4009
      result.cookie.n_bytes = NULL;
Packit 6c4009
      result.cookie.n_len = 0;
Packit 6c4009
    freeres:
Packit 6c4009
      xdr_free ((xdrproc_t) _xdr_nis_result, (char *) &result);
Packit 6c4009
      memset (&result, '\0', sizeof (result));
Packit 6c4009
    }
Packit 6c4009
  while (!parse_res);
Packit 6c4009
Packit 6c4009
  return retval;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen,
Packit 6c4009
			 int *errnop)
Packit 6c4009
{
Packit 6c4009
  int status;
Packit 6c4009
Packit 6c4009
  __libc_lock_lock (lock);
Packit 6c4009
Packit 6c4009
  status = internal_nisplus_getgrent_r (result, buffer, buflen, errnop);
Packit 6c4009
Packit 6c4009
  __libc_lock_unlock (lock);
Packit 6c4009
Packit 6c4009
  return status;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_nisplus_getgrnam_r (const char *name, struct group *gr,
Packit 6c4009
			 char *buffer, size_t buflen, int *errnop)
Packit 6c4009
{
Packit 6c4009
  int parse_res;
Packit 6c4009
Packit 6c4009
  if (grp_tablename_val == NULL)
Packit 6c4009
    {
Packit 6c4009
      enum nss_status status = _nss_grp_create_tablename (errnop);
Packit 6c4009
Packit 6c4009
      if (status != NSS_STATUS_SUCCESS)
Packit 6c4009
	return status;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (name == NULL)
Packit 6c4009
    {
Packit 6c4009
      *errnop = EINVAL;
Packit 6c4009
      return NSS_STATUS_NOTFOUND;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  nis_result *result;
Packit 6c4009
  char buf[strlen (name) + 9 + grp_tablename_len];
Packit 6c4009
  int olderr = errno;
Packit 6c4009
Packit 6c4009
  snprintf (buf, sizeof (buf), "[name=%s],%s", name, grp_tablename_val);
Packit 6c4009
Packit 6c4009
  result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
Packit 6c4009
Packit 6c4009
  if (result == NULL)
Packit 6c4009
    {
Packit 6c4009
      *errnop = ENOMEM;
Packit 6c4009
      return NSS_STATUS_TRYAGAIN;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
Packit 6c4009
    {
Packit 6c4009
      enum nss_status status = niserr2nss (result->status);
Packit 6c4009
Packit 6c4009
      nis_freeresult (result);
Packit 6c4009
      return status;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen, errnop);
Packit 6c4009
  nis_freeresult (result);
Packit 6c4009
  if (__glibc_unlikely (parse_res < 1))
Packit 6c4009
    {
Packit 6c4009
      if (parse_res == -1)
Packit 6c4009
	{
Packit 6c4009
	  *errnop = ERANGE;
Packit 6c4009
	  return NSS_STATUS_TRYAGAIN;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (olderr);
Packit 6c4009
	  return NSS_STATUS_NOTFOUND;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NSS_STATUS_SUCCESS;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr,
Packit 6c4009
			 char *buffer, size_t buflen, int *errnop)
Packit 6c4009
{
Packit 6c4009
  if (grp_tablename_val == NULL)
Packit 6c4009
    {
Packit 6c4009
      enum nss_status status = _nss_grp_create_tablename (errnop);
Packit 6c4009
Packit 6c4009
      if (status != NSS_STATUS_SUCCESS)
Packit 6c4009
	return status;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int parse_res;
Packit 6c4009
  nis_result *result;
Packit 6c4009
  char buf[8 + 3 * sizeof (unsigned long int) + grp_tablename_len];
Packit 6c4009
  int olderr = errno;
Packit 6c4009
Packit 6c4009
  snprintf (buf, sizeof (buf), "[gid=%lu],%s",
Packit 6c4009
	    (unsigned long int) gid, grp_tablename_val);
Packit 6c4009
Packit 6c4009
  result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
Packit 6c4009
Packit 6c4009
  if (result == NULL)
Packit 6c4009
    {
Packit 6c4009
      *errnop = ENOMEM;
Packit 6c4009
      return NSS_STATUS_TRYAGAIN;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
Packit 6c4009
    {
Packit 6c4009
      enum nss_status status = niserr2nss (result->status);
Packit 6c4009
Packit 6c4009
      __set_errno (olderr);
Packit 6c4009
Packit 6c4009
      nis_freeresult (result);
Packit 6c4009
      return status;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen, errnop);
Packit 6c4009
Packit 6c4009
  nis_freeresult (result);
Packit 6c4009
  if (__glibc_unlikely (parse_res < 1))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (olderr);
Packit 6c4009
Packit 6c4009
      if (parse_res == -1)
Packit 6c4009
	{
Packit 6c4009
	  *errnop = ERANGE;
Packit 6c4009
	  return NSS_STATUS_TRYAGAIN;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	return NSS_STATUS_NOTFOUND;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NSS_STATUS_SUCCESS;
Packit 6c4009
}