.
[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 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
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 #ifdef HAVE_CONFIG_H
19 #if defined (CONFIG_BROKETS)
20 /* We use <config.h> instead of "config.h" so that a compilation
21    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
22    (which it would do because it found this file in $srcdir).  */
23 #include <config.h>
24 #else
25 #include "config.h"
26 #endif
27 #endif
28
29 #include <stdio.h>
30 #include <sys/types.h>
31
32 #ifdef STDC_HEADERS
33 #include <stdlib.h>
34 #endif
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #include "group-member.h"
41
42 char *xmalloc ();
43 char *xrealloc ();
44
45 struct group_info
46   {
47     int n_groups;
48     GETGROUPS_T *group;
49   };
50
51 #ifdef HAVE_GETGROUPS
52
53 static void
54 free_group_info (g)
55      struct group_info *g;
56 {
57   free (g->group);
58   free (g);
59 }
60
61 static struct group_info *
62 get_group_info ()
63 {
64   int n_groups;
65   int n_group_slots;
66   struct group_info *gi;
67   GETGROUPS_T *group;
68
69   /* getgroups () returns the number of elements that it was able to
70      place into the array.  We simply continue to call getgroups ()
71      until the number of elements placed into the array is smaller than
72      the physical size of the array. */
73
74   group = NULL;
75   n_groups = 0;
76   n_group_slots = 0;
77   while (n_groups == n_group_slots)
78     {
79       n_group_slots += 64;
80       group = (GETGROUPS_T *) xrealloc (group,
81                                         n_group_slots * sizeof (GETGROUPS_T));
82       n_groups = getgroups (n_group_slots, group);
83     }
84
85   /* In case of error, the user loses. */
86   if (n_groups < 0)
87     {
88       free (group);
89       return NULL;
90     }
91
92   gi = (struct group_info *) xmalloc (sizeof (*gi));
93   gi->n_groups = n_groups;
94   gi->group = group;
95
96   return gi;
97 }
98
99 #endif /* not HAVE_GETGROUPS */
100
101 /* Return non-zero if GID is one that we have in our groups list.
102    If there is no getgroups function, return non-zero if GID matches
103    either of the current or effective group IDs.  */
104
105 int
106 group_member (gid)
107      gid_t gid;
108 {
109 #ifndef HAVE_GETGROUPS
110   return ((gid == getgid ()) || (gid == getegid ()));
111 #else
112   int i;
113   int found;
114   struct group_info *gi;
115
116   gi = get_group_info ();
117   if (gi == NULL)
118     return 0;
119
120   /* Search through the list looking for GID. */
121   found = 0;
122   for (i = 0; i < gi->n_groups; i++)
123     {
124       if (gid == gi->group[i])
125         {
126           found = 1;
127           break;
128         }
129     }
130   
131   free_group_info (gi);
132
133   return found;
134 #endif /* HAVE_GETGROUPS */
135 }
136
137 #ifdef TEST
138
139 char *program_name;
140
141 int
142 main (int argc, char** argv)
143 {
144   int i;
145
146   program_name = argv[0];
147
148   for (i=1; i<argc; i++)
149     {
150       gid_t gid;
151
152       gid = atoi (argv[i]);
153       printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no");
154     }
155   exit (0);
156 }
157
158 #endif /* TEST */