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