Change copyright notice from GPLv2+ to GPLv3+.
[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 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     GETGROUPS_T *group;
36   };
37
38 #if HAVE_GETGROUPS
39
40 static void
41 free_group_info (struct group_info const *g)
42 {
43   free (g->group);
44 }
45
46 static bool
47 get_group_info (struct group_info *gi)
48 {
49   int n_groups;
50   int n_group_slots = getgroups (0, NULL);
51   GETGROUPS_T *group;
52
53   if (n_group_slots < 0)
54     return false;
55
56   /* Avoid xnmalloc, as it goes awry when SIZE_MAX < n_group_slots.  */
57   if (xalloc_oversized (n_group_slots, sizeof *group))
58     xalloc_die ();
59   group = xmalloc (n_group_slots * sizeof *group);
60   n_groups = getgroups (n_group_slots, group);
61
62   /* In case of error, the user loses. */
63   if (n_groups < 0)
64     {
65       free (group);
66       return false;
67     }
68
69   gi->n_groups = n_groups;
70   gi->group = group;
71
72   return true;
73 }
74
75 #endif /* not HAVE_GETGROUPS */
76
77 /* Return non-zero if GID is one that we have in our groups list.
78    If there is no getgroups function, return non-zero if GID matches
79    either of the current or effective group IDs.  */
80
81 int
82 group_member (gid_t gid)
83 {
84 #ifndef HAVE_GETGROUPS
85   return ((gid == getgid ()) || (gid == getegid ()));
86 #else
87   int i;
88   int found;
89   struct group_info gi;
90
91   if (! get_group_info (&gi))
92     return 0;
93
94   /* Search through the list looking for GID. */
95   found = 0;
96   for (i = 0; i < gi.n_groups; i++)
97     {
98       if (gid == gi.group[i])
99         {
100           found = 1;
101           break;
102         }
103     }
104
105   free_group_info (&gi);
106
107   return found;
108 #endif /* HAVE_GETGROUPS */
109 }
110
111 #ifdef TEST
112
113 char *program_name;
114
115 int
116 main (int argc, char **argv)
117 {
118   int i;
119
120   program_name = argv[0];
121
122   for (i=1; i<argc; i++)
123     {
124       gid_t gid;
125
126       gid = atoi (argv[i]);
127       printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no");
128     }
129   exit (0);
130 }
131
132 #endif /* TEST */