update nearly all FSF copyright year lists to include 2010
[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-2010 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 #include "group-member.h"
22
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <stdlib.h>
27
28 #include <unistd.h>
29
30 #include "xalloc.h"
31
32 struct group_info
33   {
34     int n_groups;
35     gid_t *group;
36   };
37
38 static void
39 free_group_info (struct group_info const *g)
40 {
41   free (g->group);
42 }
43
44 static bool
45 get_group_info (struct group_info *gi)
46 {
47   int n_groups;
48   int n_group_slots = getgroups (0, NULL);
49   gid_t *group;
50
51   if (n_group_slots < 0)
52     return false;
53
54   /* Avoid xnmalloc, as it goes awry when SIZE_MAX < n_group_slots.  */
55   if (xalloc_oversized (n_group_slots, sizeof *group))
56     xalloc_die ();
57   group = xmalloc (n_group_slots * sizeof *group);
58   n_groups = getgroups (n_group_slots, group);
59
60   /* In case of error, the user loses. */
61   if (n_groups < 0)
62     {
63       free (group);
64       return false;
65     }
66
67   gi->n_groups = n_groups;
68   gi->group = group;
69
70   return true;
71 }
72
73 /* Return non-zero if GID is one that we have in our groups list.
74    Note that the groups list is not guaranteed to contain the current
75    or effective group ID, so they should generally be checked
76    separately.  */
77
78 int
79 group_member (gid_t gid)
80 {
81   int i;
82   int found;
83   struct group_info gi;
84
85   if (! get_group_info (&gi))
86     return 0;
87
88   /* Search through the list looking for GID. */
89   found = 0;
90   for (i = 0; i < gi.n_groups; i++)
91     {
92       if (gid == gi.group[i])
93         {
94           found = 1;
95           break;
96         }
97     }
98
99   free_group_info (&gi);
100
101   return found;
102 }
103
104 #ifdef TEST
105
106 char *program_name;
107
108 int
109 main (int argc, char **argv)
110 {
111   int i;
112
113   program_name = argv[0];
114
115   for (i = 1; i < argc; i++)
116     {
117       gid_t gid;
118
119       gid = atoi (argv[i]);
120       printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no");
121     }
122   exit (0);
123 }
124
125 #endif /* TEST */