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