* lib/getcwd.c (__getcwd): Undo previous change; it mishandled
[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 2, or (at your option)
9    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, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 #include <config.h>
21
22 #include "group-member.h"
23
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <stdlib.h>
28
29 #include <unistd.h>
30
31 #include "xalloc.h"
32
33 struct group_info
34   {
35     int n_groups;
36     GETGROUPS_T *group;
37   };
38
39 #if HAVE_GETGROUPS
40
41 static void
42 free_group_info (struct group_info const *g)
43 {
44   free (g->group);
45 }
46
47 static bool
48 get_group_info (struct group_info *gi)
49 {
50   int n_groups;
51   int n_group_slots = getgroups (0, NULL);
52   GETGROUPS_T *group;
53
54   if (n_group_slots < 0)
55     return false;
56
57   /* Avoid xnmalloc, as it goes awry when SIZE_MAX < n_group_slots.  */
58   if (xalloc_oversized (n_group_slots, sizeof *group))
59     xalloc_die ();
60   group = xmalloc (n_group_slots * sizeof *group);
61   n_groups = getgroups (n_group_slots, group);
62
63   /* In case of error, the user loses. */
64   if (n_groups < 0)
65     {
66       free (group);
67       return false;
68     }
69
70   gi->n_groups = n_groups;
71   gi->group = group;
72
73   return true;
74 }
75
76 #endif /* not HAVE_GETGROUPS */
77
78 /* Return non-zero if GID is one that we have in our groups list.
79    If there is no getgroups function, return non-zero if GID matches
80    either of the current or effective group IDs.  */
81
82 int
83 group_member (gid_t gid)
84 {
85 #ifndef HAVE_GETGROUPS
86   return ((gid == getgid ()) || (gid == getegid ()));
87 #else
88   int i;
89   int found;
90   struct group_info gi;
91
92   if (! get_group_info (&gi))
93     return 0;
94
95   /* Search through the list looking for GID. */
96   found = 0;
97   for (i = 0; i < gi.n_groups; i++)
98     {
99       if (gid == gi.group[i])
100         {
101           found = 1;
102           break;
103         }
104     }
105
106   free_group_info (&gi);
107
108   return found;
109 #endif /* HAVE_GETGROUPS */
110 }
111
112 #ifdef TEST
113
114 char *program_name;
115
116 int
117 main (int argc, char **argv)
118 {
119   int i;
120
121   program_name = argv[0];
122
123   for (i=1; i<argc; i++)
124     {
125       gid_t gid;
126
127       gid = atoi (argv[i]);
128       printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no");
129     }
130   exit (0);
131 }
132
133 #endif /* TEST */