group-member.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* group-member.c -- determine whether group id is in calling user's group list
  2. Copyright (C) 1994, 1997-1998, 2003, 2005-2006, 2009-2015 Free Software
  3. Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include <sys/types.h>
  19. #include <stdlib.h>
  20. #include "xalloc-oversized.h"
  21. /* Most processes have no more than this many groups, and for these
  22. processes we can avoid using malloc. */
  23. enum { GROUPBUF_SIZE = 100 };
  24. struct group_info
  25. {
  26. gid_t *group;
  27. gid_t groupbuf[GROUPBUF_SIZE];
  28. };
  29. static void
  30. free_group_info (struct group_info const *g)
  31. {
  32. if (g->group != g->groupbuf)
  33. free (g->group);
  34. }
  35. static int
  36. get_group_info (struct group_info *gi)
  37. {
  38. int n_groups = getgroups (GROUPBUF_SIZE, gi->groupbuf);
  39. gi->group = gi->groupbuf;
  40. if (n_groups < 0)
  41. {
  42. int n_group_slots = getgroups (0, NULL);
  43. if (0 <= n_group_slots
  44. && ! xalloc_oversized (n_group_slots, sizeof *gi->group))
  45. {
  46. gi->group = malloc (n_group_slots * sizeof *gi->group);
  47. if (gi->group)
  48. n_groups = getgroups (n_group_slots, gi->group);
  49. }
  50. }
  51. /* In case of error, the user loses. */
  52. return n_groups;
  53. }
  54. /* Return non-zero if GID is one that we have in our groups list.
  55. Note that the groups list is not guaranteed to contain the current
  56. or effective group ID, so they should generally be checked
  57. separately. */
  58. int
  59. group_member (gid_t gid)
  60. {
  61. int i;
  62. int found;
  63. struct group_info gi;
  64. int n_groups = get_group_info (&gi);
  65. /* Search through the list looking for GID. */
  66. found = 0;
  67. for (i = 0; i < n_groups; i++)
  68. {
  69. if (gid == gi.group[i])
  70. {
  71. found = 1;
  72. break;
  73. }
  74. }
  75. free_group_info (&gi);
  76. return found;
  77. }
  78. #ifdef TEST
  79. char *program_name;
  80. int
  81. main (int argc, char **argv)
  82. {
  83. int i;
  84. program_name = argv[0];
  85. for (i = 1; i < argc; i++)
  86. {
  87. gid_t gid;
  88. gid = atoi (argv[i]);
  89. printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no");
  90. }
  91. exit (0);
  92. }
  93. #endif /* TEST */