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