initgroups.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Copyright (C) 1989, 1991-2019 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. <http://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <errno.h>
  16. #include <grp.h>
  17. #include <limits.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/param.h>
  22. #include <sys/types.h>
  23. #include <nsswitch.h>
  24. #include <scratch_buffer.h>
  25. #include <config.h>
  26. #include "../nscd/nscd-client.h"
  27. #include "../nscd/nscd_proto.h"
  28. #ifdef LINK_OBSOLETE_NSL
  29. # define DEFAULT_CONFIG "compat [NOTFOUND=return] files"
  30. #else
  31. # define DEFAULT_CONFIG "files"
  32. #endif
  33. /* Type of the lookup function. */
  34. typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
  35. long int *, long int *,
  36. gid_t **, long int, int *);
  37. static bool use_initgroups_entry;
  38. #include "compat-initgroups.c"
  39. static int
  40. internal_getgrouplist (const char *user, gid_t group, long int *size,
  41. gid_t **groupsp, long int limit)
  42. {
  43. #ifdef USE_NSCD
  44. if (__nss_not_use_nscd_group > 0
  45. && ++__nss_not_use_nscd_group > NSS_NSCD_RETRY)
  46. __nss_not_use_nscd_group = 0;
  47. if (!__nss_not_use_nscd_group
  48. && !__nss_database_custom[NSS_DBSIDX_group])
  49. {
  50. int n = __nscd_getgrouplist (user, group, size, groupsp, limit);
  51. if (n >= 0)
  52. return n;
  53. /* nscd is not usable. */
  54. __nss_not_use_nscd_group = 1;
  55. }
  56. #endif
  57. enum nss_status status = NSS_STATUS_UNAVAIL;
  58. int no_more = 0;
  59. /* Never store more than the starting *SIZE number of elements. */
  60. assert (*size > 0);
  61. (*groupsp)[0] = group;
  62. /* Start is one, because we have the first group as parameter. */
  63. long int start = 1;
  64. if (__nss_initgroups_database == NULL)
  65. {
  66. if (__nss_database_lookup ("initgroups", NULL, "",
  67. &__nss_initgroups_database) < 0)
  68. {
  69. if (__nss_group_database == NULL)
  70. no_more = __nss_database_lookup ("group", NULL, DEFAULT_CONFIG,
  71. &__nss_group_database);
  72. __nss_initgroups_database = __nss_group_database;
  73. }
  74. else
  75. use_initgroups_entry = true;
  76. }
  77. else
  78. /* __nss_initgroups_database might have been set through
  79. __nss_configure_lookup in which case use_initgroups_entry was
  80. not set here. */
  81. use_initgroups_entry = __nss_initgroups_database != __nss_group_database;
  82. service_user *nip = __nss_initgroups_database;
  83. while (! no_more)
  84. {
  85. long int prev_start = start;
  86. initgroups_dyn_function fct = __nss_lookup_function (nip,
  87. "initgroups_dyn");
  88. if (fct == NULL)
  89. status = compat_call (nip, user, group, &start, size, groupsp,
  90. limit, &errno);
  91. else
  92. status = DL_CALL_FCT (fct, (user, group, &start, size, groupsp,
  93. limit, &errno));
  94. /* Remove duplicates. */
  95. long int cnt = prev_start;
  96. while (cnt < start)
  97. {
  98. long int inner;
  99. for (inner = 0; inner < prev_start; ++inner)
  100. if ((*groupsp)[inner] == (*groupsp)[cnt])
  101. break;
  102. if (inner < prev_start)
  103. (*groupsp)[cnt] = (*groupsp)[--start];
  104. else
  105. ++cnt;
  106. }
  107. /* This is really only for debugging. */
  108. if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
  109. __libc_fatal ("Illegal status in internal_getgrouplist.\n");
  110. /* For compatibility reason we will continue to look for more
  111. entries using the next service even though data has already
  112. been found if the nsswitch.conf file contained only a 'groups'
  113. line and no 'initgroups' line. If the latter is available
  114. we always respect the status. This means that the default
  115. for successful lookups is to return. */
  116. if ((use_initgroups_entry || status != NSS_STATUS_SUCCESS)
  117. && nss_next_action (nip, status) == NSS_ACTION_RETURN)
  118. break;
  119. if (nip->next == NULL)
  120. no_more = -1;
  121. else
  122. nip = nip->next;
  123. }
  124. return start;
  125. }
  126. /* Store at most *NGROUPS members of the group set for USER into
  127. *GROUPS. Also include GROUP. The actual number of groups found is
  128. returned in *NGROUPS. Return -1 if the if *NGROUPS is too small. */
  129. int
  130. getgrouplist (const char *user, gid_t group, gid_t *groups, int *ngroups)
  131. {
  132. long int size = MAX (1, *ngroups);
  133. gid_t *newgroups = (gid_t *) malloc (size * sizeof (gid_t));
  134. if (__glibc_unlikely (newgroups == NULL))
  135. /* No more memory. */
  136. // XXX This is wrong. The user provided memory, we have to use
  137. // XXX it. The internal functions must be called with the user
  138. // XXX provided buffer and not try to increase the size if it is
  139. // XXX too small. For initgroups a flag could say: increase size.
  140. return -1;
  141. int total = internal_getgrouplist (user, group, &size, &newgroups, -1);
  142. memcpy (groups, newgroups, MIN (*ngroups, total) * sizeof (gid_t));
  143. free (newgroups);
  144. int retval = total > *ngroups ? -1 : total;
  145. *ngroups = total;
  146. return retval;
  147. }
  148. nss_interface_function (getgrouplist)
  149. /* Initialize the group set for the current user
  150. by reading the group database and using all groups
  151. of which USER is a member. Also include GROUP. */
  152. int
  153. initgroups (const char *user, gid_t group)
  154. {
  155. #if defined NGROUPS_MAX && NGROUPS_MAX == 0
  156. /* No extra groups allowed. */
  157. return 0;
  158. #else
  159. long int size;
  160. gid_t *groups;
  161. int ngroups;
  162. int result;
  163. /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
  164. limit can be raised in the kernel configuration without having to
  165. recompile libc. */
  166. long int limit = __sysconf (_SC_NGROUPS_MAX);
  167. if (limit > 0)
  168. /* We limit the size of the intially allocated array. */
  169. size = MIN (limit, 64);
  170. else
  171. /* No fixed limit on groups. Pick a starting buffer size. */
  172. size = 16;
  173. groups = (gid_t *) malloc (size * sizeof (gid_t));
  174. if (__glibc_unlikely (groups == NULL))
  175. /* No more memory. */
  176. return -1;
  177. ngroups = internal_getgrouplist (user, group, &size, &groups, limit);
  178. /* Try to set the maximum number of groups the kernel can handle. */
  179. do
  180. result = setgroups (ngroups, groups);
  181. while (result == -1 && errno == EINVAL && --ngroups > 0);
  182. free (groups);
  183. return result;
  184. #endif
  185. }
  186. nss_interface_function (initgroups)