Include "xalloc.h".
[gnulib.git] / lib / group-member.c
1 /* group-member.c -- determine whether group id is in calling user's group list
2    Copyright (C) 1994, 1997, 1998, 2003 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #include <sys/types.h>
24
25 #ifdef STDC_HEADERS
26 # include <stdlib.h>
27 #endif
28
29 #if HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32
33 #include "group-member.h"
34 #include "xalloc.h"
35
36 struct group_info
37   {
38     int n_groups;
39     GETGROUPS_T *group;
40   };
41
42 #if HAVE_GETGROUPS
43
44 static void
45 free_group_info (struct group_info *g)
46 {
47   free (g->group);
48   free (g);
49 }
50
51 static struct group_info *
52 get_group_info (void)
53 {
54   int n_groups;
55   int n_group_slots;
56   struct group_info *gi;
57   GETGROUPS_T *group;
58
59   /* getgroups () returns the number of elements that it was able to
60      place into the array.  We simply continue to call getgroups ()
61      until the number of elements placed into the array is smaller than
62      the physical size of the array. */
63
64   group = NULL;
65   n_groups = 0;
66   n_group_slots = 0;
67   while (n_groups == n_group_slots)
68     {
69       n_group_slots += 64;
70       group = xrealloc (group, n_group_slots * sizeof (GETGROUPS_T));
71       n_groups = getgroups (n_group_slots, group);
72     }
73
74   /* In case of error, the user loses. */
75   if (n_groups < 0)
76     {
77       free (group);
78       return NULL;
79     }
80
81   gi = xmalloc (sizeof (*gi));
82   gi->n_groups = n_groups;
83   gi->group = group;
84
85   return gi;
86 }
87
88 #endif /* not HAVE_GETGROUPS */
89
90 /* Return non-zero if GID is one that we have in our groups list.
91    If there is no getgroups function, return non-zero if GID matches
92    either of the current or effective group IDs.  */
93
94 int
95 group_member (gid_t gid)
96 {
97 #ifndef HAVE_GETGROUPS
98   return ((gid == getgid ()) || (gid == getegid ()));
99 #else
100   int i;
101   int found;
102   struct group_info *gi;
103
104   gi = get_group_info ();
105   if (gi == NULL)
106     return 0;
107
108   /* Search through the list looking for GID. */
109   found = 0;
110   for (i = 0; i < gi->n_groups; i++)
111     {
112       if (gid == gi->group[i])
113         {
114           found = 1;
115           break;
116         }
117     }
118
119   free_group_info (gi);
120
121   return found;
122 #endif /* HAVE_GETGROUPS */
123 }
124
125 #ifdef TEST
126
127 char *program_name;
128
129 int
130 main (int argc, char** argv)
131 {
132   int i;
133
134   program_name = argv[0];
135
136   for (i=1; i<argc; i++)
137     {
138       gid_t gid;
139
140       gid = atoi (argv[i]);
141       printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no");
142     }
143   exit (0);
144 }
145
146 #endif /* TEST */