bump standards-version
[gnulib.git] / lib / group-member.c
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-2012 Free Software
4    Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program 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 General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <unistd.h>
23
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <stdlib.h>
28
29 #include "xalloc.h"
30
31 struct group_info
32   {
33     int n_groups;
34     gid_t *group;
35   };
36
37 static void
38 free_group_info (struct group_info const *g)
39 {
40   free (g->group);
41 }
42
43 static bool
44 get_group_info (struct group_info *gi)
45 {
46   int n_groups;
47   int n_group_slots = getgroups (0, NULL);
48   gid_t *group;
49
50   if (n_group_slots < 0)
51     return false;
52
53   /* Avoid xnmalloc, as it goes awry when SIZE_MAX < n_group_slots.  */
54   if (xalloc_oversized (n_group_slots, sizeof *group))
55     xalloc_die ();
56   group = xmalloc (n_group_slots * sizeof *group);
57   n_groups = getgroups (n_group_slots, group);
58
59   /* In case of error, the user loses. */
60   if (n_groups < 0)
61     {
62       free (group);
63       return false;
64     }
65
66   gi->n_groups = n_groups;
67   gi->group = group;
68
69   return true;
70 }
71
72 /* Return non-zero if GID is one that we have in our groups list.
73    Note that the groups list is not guaranteed to contain the current
74    or effective group ID, so they should generally be checked
75    separately.  */
76
77 int
78 group_member (gid_t gid)
79 {
80   int i;
81   int found;
82   struct group_info gi;
83
84   if (! get_group_info (&gi))
85     return 0;
86
87   /* Search through the list looking for GID. */
88   found = 0;
89   for (i = 0; i < gi.n_groups; i++)
90     {
91       if (gid == gi.group[i])
92         {
93           found = 1;
94           break;
95         }
96     }
97
98   free_group_info (&gi);
99
100   return found;
101 }
102
103 #ifdef TEST
104
105 char *program_name;
106
107 int
108 main (int argc, char **argv)
109 {
110   int i;
111
112   program_name = argv[0];
113
114   for (i = 1; i < argc; i++)
115     {
116       gid_t gid;
117
118       gid = atoi (argv[i]);
119       printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no");
120     }
121   exit (0);
122 }
123
124 #endif /* TEST */