tst-putsgent.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Test for processing of invalid gshadow entries. [BZ #18724]
  2. Copyright (C) 2015-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <gshadow.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. static bool errors;
  22. static void
  23. check (struct sgrp e, const char *expected)
  24. {
  25. char *buf;
  26. size_t buf_size;
  27. FILE *f = open_memstream (&buf, &buf_size);
  28. if (f == NULL)
  29. {
  30. printf ("open_memstream: %m\n");
  31. errors = true;
  32. return;
  33. }
  34. int ret = putsgent (&e, f);
  35. if (expected == NULL)
  36. {
  37. if (ret == -1)
  38. {
  39. if (errno != EINVAL)
  40. {
  41. printf ("putsgent: unexpected error code: %m\n");
  42. errors = true;
  43. }
  44. }
  45. else
  46. {
  47. printf ("putsgent: unexpected success (\"%s\")\n", e.sg_namp);
  48. errors = true;
  49. }
  50. }
  51. else
  52. {
  53. /* Expect success. */
  54. size_t expected_length = strlen (expected);
  55. if (ret == 0)
  56. {
  57. long written = ftell (f);
  58. if (written <= 0 || fflush (f) < 0)
  59. {
  60. printf ("stream error: %m\n");
  61. errors = true;
  62. }
  63. else if (buf[written - 1] != '\n')
  64. {
  65. printf ("FAILED: \"%s\" without newline\n", expected);
  66. errors = true;
  67. }
  68. else if (strncmp (buf, expected, written - 1) != 0
  69. || written - 1 != expected_length)
  70. {
  71. printf ("FAILED: \"%s\" (%ld), expected \"%s\" (%zu)\n",
  72. buf, written - 1, expected, expected_length);
  73. errors = true;
  74. }
  75. }
  76. else
  77. {
  78. printf ("FAILED: putsgent (expected \"%s\"): %m\n", expected);
  79. errors = true;
  80. }
  81. }
  82. fclose (f);
  83. free (buf);
  84. }
  85. static int
  86. do_test (void)
  87. {
  88. check ((struct sgrp) {
  89. .sg_namp = (char *) "root",
  90. },
  91. "root:::");
  92. check ((struct sgrp) {
  93. .sg_namp = (char *) "root",
  94. .sg_passwd = (char *) "password",
  95. },
  96. "root:password::");
  97. check ((struct sgrp) {
  98. .sg_namp = (char *) "root",
  99. .sg_passwd = (char *) "password",
  100. .sg_adm = (char *[]) {(char *) "adm1", (char *) "adm2", NULL},
  101. .sg_mem = (char *[]) {(char *) "mem1", (char *) "mem2", NULL},
  102. },
  103. "root:password:adm1,adm2:mem1,mem2");
  104. /* Bad values. */
  105. {
  106. static const char *const bad_strings[] = {
  107. ":",
  108. "\n",
  109. ":bad",
  110. "\nbad",
  111. "b:ad",
  112. "b\nad",
  113. "bad:",
  114. "bad\n",
  115. "b:a\nd",
  116. ",",
  117. "\n,",
  118. ":,",
  119. ",bad",
  120. "b,ad",
  121. "bad,",
  122. NULL
  123. };
  124. for (const char *const *bad = bad_strings; *bad != NULL; ++bad)
  125. {
  126. char *members[]
  127. = {(char *) "first", (char *) *bad, (char *) "last", NULL};
  128. if (strpbrk (*bad, ":\n") != NULL)
  129. {
  130. check ((struct sgrp) {
  131. .sg_namp = (char *) *bad,
  132. }, NULL);
  133. check ((struct sgrp) {
  134. .sg_namp = (char *) "root",
  135. .sg_passwd = (char *) *bad,
  136. }, NULL);
  137. }
  138. check ((struct sgrp) {
  139. .sg_namp = (char *) "root",
  140. .sg_passwd = (char *) "password",
  141. .sg_adm = members
  142. }, NULL);
  143. check ((struct sgrp) {
  144. .sg_namp = (char *) "root",
  145. .sg_passwd = (char *) "password",
  146. .sg_mem = members
  147. }, NULL);
  148. }
  149. }
  150. return errors;
  151. }
  152. #define TEST_FUNCTION do_test ()
  153. #include "../test-skeleton.c"