Blame nss/tst-nss-getpwent.c

Packit 6c4009
/* Copyright (C) 2015-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 <nss.h>
Packit 6c4009
#include <pwd.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <support/support.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  __nss_configure_lookup ("passwd", "files");
Packit 6c4009
Packit 6c4009
  /* Count the number of entries in the password database, and fetch
Packit 6c4009
     data from the first and last entries.  */
Packit 6c4009
  size_t count = 0;
Packit 6c4009
  struct passwd * pw;
Packit 6c4009
  char *first_name = NULL;
Packit 6c4009
  uid_t first_uid = 0;
Packit 6c4009
  char *last_name = NULL;
Packit 6c4009
  uid_t last_uid = 0;
Packit 6c4009
  setpwent ();
Packit 6c4009
  while ((pw  = getpwent ()) != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (first_name == NULL)
Packit 6c4009
	{
Packit 6c4009
	  first_name = xstrdup (pw->pw_name);
Packit 6c4009
	  first_uid = pw->pw_uid;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      free (last_name);
Packit 6c4009
      last_name = xstrdup (pw->pw_name);
Packit 6c4009
      last_uid = pw->pw_uid;
Packit 6c4009
      ++count;
Packit 6c4009
    }
Packit 6c4009
  endpwent ();
Packit 6c4009
Packit 6c4009
  if (count == 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("No entries in the password database.\n");
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Try again, this time interleaving with name-based and UID-based
Packit 6c4009
     lookup operations.  The counts do not match if the interleaved
Packit 6c4009
     lookups affected the enumeration.  */
Packit 6c4009
  size_t new_count = 0;
Packit 6c4009
  setpwent ();
Packit 6c4009
  while ((pw  = getpwent ()) != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (new_count == count)
Packit 6c4009
	{
Packit 6c4009
	  printf ("Additional entry in the password database.\n");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      ++new_count;
Packit 6c4009
      struct passwd *pw2 = getpwnam (first_name);
Packit 6c4009
      if (pw2 == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("getpwnam (%s) failed: %m\n", first_name);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      pw2 = getpwnam (last_name);
Packit 6c4009
      if (pw2 == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("getpwnam (%s) failed: %m\n", last_name);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      pw2 = getpwuid (first_uid);
Packit 6c4009
      if (pw2 == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("getpwuid (%llu) failed: %m\n",
Packit 6c4009
		  (unsigned long long) first_uid);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
      pw2 = getpwuid (last_uid);
Packit 6c4009
      if (pw2 == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("getpwuid (%llu) failed: %m\n",
Packit 6c4009
		  (unsigned long long) last_uid);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  endpwent ();
Packit 6c4009
  if (new_count < count)
Packit 6c4009
    {
Packit 6c4009
      printf ("Missing entry in the password database.\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TIMEOUT 300
Packit 6c4009
#include <support/test-driver.c>