files-initgroups.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Initgroups handling in nss_files module.
  2. Copyright (C) 2011-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 <grp.h>
  17. #include <nss.h>
  18. #include <stdio_ext.h>
  19. #include <string.h>
  20. #include <sys/param.h>
  21. #include <stdbool.h>
  22. #include <stdlib.h>
  23. #include <scratch_buffer.h>
  24. #include <nss.h>
  25. #include <nss_files.h>
  26. enum nss_status
  27. _nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
  28. long int *size, gid_t **groupsp, long int limit,
  29. int *errnop)
  30. {
  31. FILE *stream = __nss_files_fopen ("/etc/group");
  32. if (stream == NULL)
  33. {
  34. *errnop = errno;
  35. return *errnop == ENOMEM ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
  36. }
  37. char *line = NULL;
  38. size_t linelen = 0;
  39. enum nss_status status = NSS_STATUS_SUCCESS;
  40. bool any = false;
  41. struct scratch_buffer tmpbuf;
  42. scratch_buffer_init (&tmpbuf);
  43. gid_t *groups = *groupsp;
  44. /* We have to iterate over the entire file. */
  45. while (1)
  46. {
  47. fpos_t pos;
  48. fgetpos (stream, &pos);
  49. ssize_t n = __getline (&line, &linelen, stream);
  50. if (n < 0)
  51. {
  52. if (! __feof_unlocked (stream))
  53. status = ((*errnop = errno) == ENOMEM
  54. ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL);
  55. break;
  56. }
  57. struct group grp;
  58. int res = _nss_files_parse_grent (line, &grp,
  59. tmpbuf.data, tmpbuf.length, errnop);
  60. if (res == -1)
  61. {
  62. if (!scratch_buffer_grow (&tmpbuf))
  63. {
  64. *errnop = ENOMEM;
  65. status = NSS_STATUS_TRYAGAIN;
  66. goto out;
  67. }
  68. /* Reread current line, the parser has clobbered it. */
  69. fsetpos (stream, &pos);
  70. continue;
  71. }
  72. if (res > 0 && grp.gr_gid != group)
  73. for (char **m = grp.gr_mem; *m != NULL; ++m)
  74. if (strcmp (*m, user) == 0)
  75. {
  76. /* Matches user. Insert this group. */
  77. if (*start == *size)
  78. {
  79. /* Need a bigger buffer. */
  80. if (limit > 0 && *size == limit)
  81. /* We reached the maximum. */
  82. goto out;
  83. long int newsize;
  84. if (limit <= 0)
  85. newsize = 2 * *size;
  86. else
  87. newsize = MIN (limit, 2 * *size);
  88. gid_t *newgroups = realloc (groups,
  89. newsize * sizeof (*groups));
  90. if (newgroups == NULL)
  91. {
  92. *errnop = ENOMEM;
  93. status = NSS_STATUS_TRYAGAIN;
  94. goto out;
  95. }
  96. *groupsp = groups = newgroups;
  97. *size = newsize;
  98. }
  99. groups[*start] = grp.gr_gid;
  100. *start += 1;
  101. any = true;
  102. break;
  103. }
  104. }
  105. out:
  106. /* Free memory. */
  107. scratch_buffer_free (&tmpbuf);
  108. free (line);
  109. fclose (stream);
  110. return status == NSS_STATUS_SUCCESS && !any ? NSS_STATUS_NOTFOUND : status;
  111. }
  112. libc_hidden_def (_nss_files_initgroups_dyn)