root/lib/group-member.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. free_group_info
  2. get_group_info
  3. group_member
  4. main

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

/* [<][>][^][v][top][bottom][index][help] */