Blame nss/nss_db/db-netgrp.c

Packit 6c4009
/* Netgroup file parser in nss_db modules.
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 <fcntl.h>
Packit 6c4009
#include <netgroup.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include <paths.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
#include "nsswitch.h"
Packit 6c4009
#include "nss_db.h"
Packit 6c4009
Packit 6c4009
/* The hashing function we use.  */
Packit 6c4009
#include "../intl/hash-string.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define DBFILE		_PATH_VARDB "netgroup.db"
Packit 6c4009

Packit 6c4009
/* Maintenance of the shared handle open on the database.  */
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_db_setnetgrent (const char *group, struct __netgrent *result)
Packit 6c4009
{
Packit 6c4009
  struct nss_db_map state;
Packit 6c4009
  enum nss_status status = internal_setent (DBFILE, &state);
Packit 6c4009
Packit 6c4009
  if (status == NSS_STATUS_SUCCESS)
Packit 6c4009
    {
Packit 6c4009
      const struct nss_db_header *header = state.header;
Packit 6c4009
      const stridx_t *hashtable
Packit 6c4009
	= (const stridx_t *) ((const char *) header
Packit 6c4009
			      + header->dbs[0].hashoffset);
Packit 6c4009
      const char *valstrtab = (const char *) header + header->valstroffset;
Packit 6c4009
      uint32_t hashval = __hash_string (group);
Packit 6c4009
      size_t grouplen = strlen (group);
Packit 6c4009
      size_t hidx = hashval % header->dbs[0].hashsize;
Packit 6c4009
      size_t hval2 = 1 + hashval % (header->dbs[0].hashsize - 2);
Packit 6c4009
Packit 6c4009
      status = NSS_STATUS_NOTFOUND;
Packit 6c4009
      while (hashtable[hidx] != ~((stridx_t) 0))
Packit 6c4009
	{
Packit 6c4009
	  const char *valstr = valstrtab + hashtable[hidx];
Packit 6c4009
Packit 6c4009
	  if (strncmp (valstr, group, grouplen) == 0
Packit 6c4009
	      && isblank (valstr[grouplen]))
Packit 6c4009
	    {
Packit 6c4009
	      const char *cp = &valstr[grouplen + 1];
Packit 6c4009
	      while (isblank (*cp))
Packit 6c4009
		++cp;
Packit 6c4009
	      if (*cp != '\0')
Packit 6c4009
		{
Packit 6c4009
		  result->data = strdup (cp);
Packit 6c4009
		  if (result->data == NULL)
Packit 6c4009
		    status = NSS_STATUS_TRYAGAIN;
Packit 6c4009
		  else
Packit 6c4009
		    {
Packit 6c4009
		      status = NSS_STATUS_SUCCESS;
Packit 6c4009
		      result->cursor = result->data;
Packit 6c4009
		    }
Packit 6c4009
		  break;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if ((hidx += hval2) >= header->dbs[0].hashsize)
Packit 6c4009
	    hidx -= header->dbs[0].hashsize;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      internal_endent (&state);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return status;
Packit 6c4009
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_db_endnetgrent (struct __netgrent *result)
Packit 6c4009
{
Packit 6c4009
  free (result->data);
Packit 6c4009
  result->data = NULL;
Packit 6c4009
  result->data_size = 0;
Packit 6c4009
  result->cursor = NULL;
Packit 6c4009
  return NSS_STATUS_SUCCESS;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
extern enum nss_status _nss_netgroup_parseline (char **cursor,
Packit 6c4009
						struct __netgrent *result,
Packit 6c4009
						char *buffer, size_t buflen,
Packit 6c4009
						int *errnop);
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_db_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
Packit 6c4009
		       int *errnop)
Packit 6c4009
{
Packit 6c4009
  enum nss_status status;
Packit 6c4009
Packit 6c4009
  status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
Packit 6c4009
				    errnop);
Packit 6c4009
Packit 6c4009
  return status;
Packit 6c4009
}