Update.
[platform/upstream/glibc.git] / grp / initgroups.c
1 /* Copyright (C) 1989,91,93,1996-2003, 2004  Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library 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 GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <alloca.h>
20 #include <errno.h>
21 #include <grp.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <nsswitch.h>
29
30 #include "../nscd/nscd-client.h"
31 #include "../nscd/nscd_proto.h"
32
33
34 /* Type of the lookup function.  */
35 typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
36                                                     long int *, long int *,
37                                                     gid_t **, long int, int *);
38
39 /* The lookup function for the first entry of this service.  */
40 extern int __nss_group_lookup (service_user **nip, const char *name,
41                                    void **fctp);
42 extern void *__nss_lookup_function (service_user *ni, const char *fct_name);
43
44 extern service_user *__nss_group_database attribute_hidden;
45
46
47 #include "compat-initgroups.c"
48
49
50 static int
51 internal_getgrouplist (const char *user, gid_t group, long int *size,
52                        gid_t **groupsp, long int limit)
53 {
54 #ifdef USE_NSCD
55   if (__nss_not_use_nscd_group > 0
56       && ++__nss_not_use_nscd_group > NSS_NSCD_RETRY)
57     __nss_not_use_nscd_group = 0;
58   if (!__nss_not_use_nscd_group)
59     {
60       int n = __nscd_getgrouplist (user, group, size, groupsp, limit);
61       if (n >= 0)
62         return n;
63
64       /* nscd is not usable.  */
65       __nss_not_use_nscd_group = 1;
66     }
67 #endif
68
69   service_user *nip = NULL;
70   initgroups_dyn_function fct;
71   enum nss_status status = NSS_STATUS_UNAVAIL;
72   int no_more;
73   /* Start is one, because we have the first group as parameter.  */
74   long int start = 1;
75
76   *groupsp[0] = group;
77
78   if (__nss_group_database != NULL)
79     {
80       no_more = 0;
81       nip = __nss_group_database;
82     }
83   else
84     no_more = __nss_database_lookup ("group", NULL,
85                                      "compat [NOTFOUND=return] files", &nip);
86
87   while (! no_more)
88     {
89       fct = __nss_lookup_function (nip, "initgroups_dyn");
90
91       if (fct == NULL)
92         {
93           status = compat_call (nip, user, group, &start, size, groupsp,
94                                 limit, &errno);
95
96           if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
97             break;
98         }
99       else
100         status = DL_CALL_FCT (fct, (user, group, &start, size, groupsp,
101                                     limit, &errno));
102
103       /* This is really only for debugging.  */
104       if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
105         __libc_fatal ("illegal status in internal_getgrouplist");
106
107       if (status != NSS_STATUS_SUCCESS
108           && nss_next_action (nip, status) == NSS_ACTION_RETURN)
109          break;
110
111       if (nip->next == NULL)
112         no_more = -1;
113       else
114         nip = nip->next;
115     }
116
117   return start;
118 }
119
120 /* Store at most *NGROUPS members of the group set for USER into
121    *GROUPS.  Also include GROUP.  The actual number of groups found is
122    returned in *NGROUPS.  Return -1 if the if *NGROUPS is too small.  */
123 int
124 getgrouplist (const char *user, gid_t group, gid_t *groups, int *ngroups)
125 {
126   gid_t *newgroups;
127   long int size = *ngroups;
128   int result;
129
130   newgroups = (gid_t *) malloc (size * sizeof (gid_t));
131   if (__builtin_expect (newgroups == NULL, 0))
132     /* No more memory.  */
133     // XXX This is wrong.  The user provided memory, we have to use
134     // XXX it.  The internal functions must be called with the user
135     // XXX provided buffer and not try to increase the size if it is
136     // XXX too small.  For initgroups a flag could say: increase size.
137     return -1;
138
139   result = internal_getgrouplist (user, group, &size, &newgroups, -1);
140
141   memcpy (groups, newgroups, MIN (*ngroups, result) * sizeof (gid_t));
142
143   if (result > *ngroups)
144     {
145       *ngroups = result;
146       result = -1;
147     }
148   else
149     *ngroups = result;
150
151   free (newgroups);
152   return result;
153 }
154
155 static_link_warning (getgrouplist)
156
157 /* Initialize the group set for the current user
158    by reading the group database and using all groups
159    of which USER is a member.  Also include GROUP.  */
160 int
161 initgroups (const char *user, gid_t group)
162 {
163 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
164
165   /* No extra groups allowed.  */
166   return 0;
167
168 #else
169
170   long int size;
171   gid_t *groups;
172   int ngroups;
173   int result;
174
175  /* We always use sysconf even if NGROUPS_MAX is defined.  That way, the
176      limit can be raised in the kernel configuration without having to
177      recompile libc.  */
178   long int limit = __sysconf (_SC_NGROUPS_MAX);
179
180   if (limit > 0)
181     /* We limit the size of the intially allocated array.  */
182     size = MIN (limit, 64);
183   else
184     /* No fixed limit on groups.  Pick a starting buffer size.  */
185     size = 16;
186
187   groups = (gid_t *) malloc (size * sizeof (gid_t));
188   if (__builtin_expect (groups == NULL, 0))
189     /* No more memory.  */
190     return -1;
191
192   ngroups = internal_getgrouplist (user, group, &size, &groups, limit);
193
194   /* Try to set the maximum number of groups the kernel can handle.  */
195   do
196     result = setgroups (ngroups, groups);
197   while (result == -1 && errno == EINVAL && --ngroups > 0);
198
199   free (groups);
200
201   return result;
202 #endif
203 }
204
205 static_link_warning (initgroups)