tst-nss-getpwent.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (C) 2015-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <nss.h>
  15. #include <pwd.h>
  16. #include <stdbool.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <support/support.h>
  21. int
  22. do_test (void)
  23. {
  24. __nss_configure_lookup ("passwd", "files");
  25. /* Count the number of entries in the password database, and fetch
  26. data from the first and last entries. */
  27. size_t count = 0;
  28. struct passwd * pw;
  29. char *first_name = NULL;
  30. uid_t first_uid = 0;
  31. char *last_name = NULL;
  32. uid_t last_uid = 0;
  33. setpwent ();
  34. while ((pw = getpwent ()) != NULL)
  35. {
  36. if (first_name == NULL)
  37. {
  38. first_name = xstrdup (pw->pw_name);
  39. first_uid = pw->pw_uid;
  40. }
  41. free (last_name);
  42. last_name = xstrdup (pw->pw_name);
  43. last_uid = pw->pw_uid;
  44. ++count;
  45. }
  46. endpwent ();
  47. if (count == 0)
  48. {
  49. printf ("No entries in the password database.\n");
  50. return 0;
  51. }
  52. /* Try again, this time interleaving with name-based and UID-based
  53. lookup operations. The counts do not match if the interleaved
  54. lookups affected the enumeration. */
  55. size_t new_count = 0;
  56. setpwent ();
  57. while ((pw = getpwent ()) != NULL)
  58. {
  59. if (new_count == count)
  60. {
  61. printf ("Additional entry in the password database.\n");
  62. return 1;
  63. }
  64. ++new_count;
  65. struct passwd *pw2 = getpwnam (first_name);
  66. if (pw2 == NULL)
  67. {
  68. printf ("getpwnam (%s) failed: %m\n", first_name);
  69. return 1;
  70. }
  71. pw2 = getpwnam (last_name);
  72. if (pw2 == NULL)
  73. {
  74. printf ("getpwnam (%s) failed: %m\n", last_name);
  75. return 1;
  76. }
  77. pw2 = getpwuid (first_uid);
  78. if (pw2 == NULL)
  79. {
  80. printf ("getpwuid (%llu) failed: %m\n",
  81. (unsigned long long) first_uid);
  82. return 1;
  83. }
  84. pw2 = getpwuid (last_uid);
  85. if (pw2 == NULL)
  86. {
  87. printf ("getpwuid (%llu) failed: %m\n",
  88. (unsigned long long) last_uid);
  89. return 1;
  90. }
  91. }
  92. endpwent ();
  93. if (new_count < count)
  94. {
  95. printf ("Missing entry in the password database.\n");
  96. return 1;
  97. }
  98. return 0;
  99. }
  100. #define TIMEOUT 300
  101. #include <support/test-driver.c>