Add finer grained control for initgroups lookups to NSS.
[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
48
49 #include "compat-initgroups.c"
50
51
52 static int
53 internal_getgrouplist (const char *user, gid_t group, long int *size,
54                        gid_t **groupsp, long int limit)
55 {
56 #ifdef USE_NSCD
57   if (__nss_not_use_nscd_group > 0
58       && ++__nss_not_use_nscd_group > NSS_NSCD_RETRY)
59     __nss_not_use_nscd_group = 0;
60   if (!__nss_not_use_nscd_group
61       && !__nss_database_custom[NSS_DBSIDX_group])
62     {
63       int n = __nscd_getgrouplist (user, group, size, groupsp, limit);
64       if (n >= 0)
65         return n;
66
67       /* nscd is not usable.  */
68       __nss_not_use_nscd_group = 1;
69     }
70 #endif
71
72   service_user *nip = NULL;
73   initgroups_dyn_function fct;
74   enum nss_status status = NSS_STATUS_UNAVAIL;
75   int no_more;
76   /* Start is one, because we have the first group as parameter.  */
77   long int start = 1;
78
79   /* Never store more than the starting *SIZE number of elements.  */
80   assert (*size > 0);
81   (*groupsp)[0] = group;
82
83   if (__nss_group_database != NULL)
84     {
85       no_more = 0;
86       nip = __nss_group_database;
87     }
88   else
89     no_more = __nss_database_lookup ("initgroups", "group",
90                                      "compat [NOTFOUND=return] files", &nip);
91
92   while (! no_more)
93     {
94       long int prev_start = start;
95
96       fct = __nss_lookup_function (nip, "initgroups_dyn");
97
98       if (fct == NULL)
99         status = compat_call (nip, user, group, &start, size, groupsp,
100                               limit, &errno);
101       else
102         status = DL_CALL_FCT (fct, (user, group, &start, size, groupsp,
103                                     limit, &errno));
104
105       /* Remove duplicates.  */
106       long int cnt = prev_start;
107       while (cnt < start)
108         {
109           long int inner;
110           for (inner = 0; inner < prev_start; ++inner)
111             if ((*groupsp)[inner] == (*groupsp)[cnt])
112               break;
113
114           if (inner < prev_start)
115             (*groupsp)[cnt] = (*groupsp)[--start];
116           else
117             ++cnt;
118         }
119
120       /* This is really only for debugging.  */
121       if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
122         __libc_fatal ("illegal status in internal_getgrouplist");
123
124       if (status != NSS_STATUS_SUCCESS
125           && nss_next_action (nip, status) == NSS_ACTION_RETURN)
126          break;
127
128       if (nip->next == NULL)
129         no_more = -1;
130       else
131         nip = nip->next;
132     }
133
134   return start;
135 }
136
137 /* Store at most *NGROUPS members of the group set for USER into
138    *GROUPS.  Also include GROUP.  The actual number of groups found is
139    returned in *NGROUPS.  Return -1 if the if *NGROUPS is too small.  */
140 int
141 getgrouplist (const char *user, gid_t group, gid_t *groups, int *ngroups)
142 {
143   long int size = MAX (1, *ngroups);
144
145   gid_t *newgroups = (gid_t *) malloc (size * sizeof (gid_t));
146   if (__builtin_expect (newgroups == NULL, 0))
147     /* No more memory.  */
148     // XXX This is wrong.  The user provided memory, we have to use
149     // XXX it.  The internal functions must be called with the user
150     // XXX provided buffer and not try to increase the size if it is
151     // XXX too small.  For initgroups a flag could say: increase size.
152     return -1;
153
154   int total = internal_getgrouplist (user, group, &size, &newgroups, -1);
155
156   memcpy (groups, newgroups, MIN (*ngroups, total) * sizeof (gid_t));
157
158   free (newgroups);
159
160   int retval = total > *ngroups ? -1 : total;
161   *ngroups = total;
162
163   return retval;
164 }
165
166 static_link_warning (getgrouplist)
167
168 /* Initialize the group set for the current user
169    by reading the group database and using all groups
170    of which USER is a member.  Also include GROUP.  */
171 int
172 initgroups (const char *user, gid_t group)
173 {
174 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
175
176   /* No extra groups allowed.  */
177   return 0;
178
179 #else
180
181   long int size;
182   gid_t *groups;
183   int ngroups;
184   int result;
185
186  /* We always use sysconf even if NGROUPS_MAX is defined.  That way, the
187      limit can be raised in the kernel configuration without having to
188      recompile libc.  */
189   long int limit = __sysconf (_SC_NGROUPS_MAX);
190
191   if (limit > 0)
192     /* We limit the size of the intially allocated array.  */
193     size = MIN (limit, 64);
194   else
195     /* No fixed limit on groups.  Pick a starting buffer size.  */
196     size = 16;
197
198   groups = (gid_t *) malloc (size * sizeof (gid_t));
199   if (__builtin_expect (groups == NULL, 0))
200     /* No more memory.  */
201     return -1;
202
203   ngroups = internal_getgrouplist (user, group, &size, &groups, limit);
204
205   /* Try to set the maximum number of groups the kernel can handle.  */
206   do
207     result = setgroups (ngroups, groups);
208   while (result == -1 && errno == EINVAL && --ngroups > 0);
209
210   free (groups);
211
212   return result;
213 #endif
214 }
215
216 static_link_warning (initgroups)