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.
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.
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.
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
28 #include <sys/param.h>
29 #include <sys/types.h>
32 #include "../nscd/nscd-client.h"
33 #include "../nscd/nscd_proto.h"
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 *);
41 /* The lookup function for the first entry of this service. */
42 extern int __nss_group_lookup (service_user **nip, const char *name,
44 extern void *__nss_lookup_function (service_user *ni, const char *fct_name);
46 extern service_user *__nss_group_database attribute_hidden;
47 service_user *__nss_initgroups_database;
48 static bool use_initgroups_entry;
51 #include "compat-initgroups.c"
55 internal_getgrouplist (const char *user, gid_t group, long int *size,
56 gid_t **groupsp, long int limit)
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])
65 int n = __nscd_getgrouplist (user, group, size, groupsp, limit);
69 /* nscd is not usable. */
70 __nss_not_use_nscd_group = 1;
74 enum nss_status status = NSS_STATUS_UNAVAIL;
77 /* Never store more than the starting *SIZE number of elements. */
79 (*groupsp)[0] = group;
80 /* Start is one, because we have the first group as parameter. */
83 if (__nss_initgroups_database == NULL)
85 no_more = __nss_database_lookup ("initgroups", NULL, "",
86 &__nss_initgroups_database);
87 if (no_more == 0 && __nss_initgroups_database == NULL)
89 if (__nss_group_database == NULL)
90 no_more = __nss_database_lookup ("group", NULL, "compat files",
91 &__nss_group_database);
93 __nss_initgroups_database = __nss_group_database;
95 else if (__nss_initgroups_database != NULL)
97 assert (no_more == 0);
98 use_initgroups_entry = true;
102 /* __nss_initgroups_database might have been set through
103 __nss_configure_lookup in which case use_initgroups_entry was
105 use_initgroups_entry = __nss_initgroups_database != __nss_group_database;
107 service_user *nip = __nss_initgroups_database;
110 long int prev_start = start;
112 initgroups_dyn_function fct = __nss_lookup_function (nip,
115 status = compat_call (nip, user, group, &start, size, groupsp,
118 status = DL_CALL_FCT (fct, (user, group, &start, size, groupsp,
121 /* Remove duplicates. */
122 long int cnt = prev_start;
126 for (inner = 0; inner < prev_start; ++inner)
127 if ((*groupsp)[inner] == (*groupsp)[cnt])
130 if (inner < prev_start)
131 (*groupsp)[cnt] = (*groupsp)[--start];
136 /* This is really only for debugging. */
137 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
138 __libc_fatal ("illegal status in internal_getgrouplist");
140 /* For compatibility reason we will continue to look for more
141 entries using the next service even though data has already
142 been found if the nsswitch.conf file contained only a 'groups'
143 line and no 'initgroups' line. If the latter is available
144 we always respect the status. This means that the default
145 for successful lookups is to return. */
146 if ((use_initgroups_entry || status != NSS_STATUS_SUCCESS)
147 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
150 if (nip->next == NULL)
159 /* Store at most *NGROUPS members of the group set for USER into
160 *GROUPS. Also include GROUP. The actual number of groups found is
161 returned in *NGROUPS. Return -1 if the if *NGROUPS is too small. */
163 getgrouplist (const char *user, gid_t group, gid_t *groups, int *ngroups)
165 long int size = MAX (1, *ngroups);
167 gid_t *newgroups = (gid_t *) malloc (size * sizeof (gid_t));
168 if (__builtin_expect (newgroups == NULL, 0))
169 /* No more memory. */
170 // XXX This is wrong. The user provided memory, we have to use
171 // XXX it. The internal functions must be called with the user
172 // XXX provided buffer and not try to increase the size if it is
173 // XXX too small. For initgroups a flag could say: increase size.
176 int total = internal_getgrouplist (user, group, &size, &newgroups, -1);
178 memcpy (groups, newgroups, MIN (*ngroups, total) * sizeof (gid_t));
182 int retval = total > *ngroups ? -1 : total;
188 static_link_warning (getgrouplist)
190 /* Initialize the group set for the current user
191 by reading the group database and using all groups
192 of which USER is a member. Also include GROUP. */
194 initgroups (const char *user, gid_t group)
196 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
198 /* No extra groups allowed. */
208 /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
209 limit can be raised in the kernel configuration without having to
211 long int limit = __sysconf (_SC_NGROUPS_MAX);
214 /* We limit the size of the intially allocated array. */
215 size = MIN (limit, 64);
217 /* No fixed limit on groups. Pick a starting buffer size. */
220 groups = (gid_t *) malloc (size * sizeof (gid_t));
221 if (__builtin_expect (groups == NULL, 0))
222 /* No more memory. */
225 ngroups = internal_getgrouplist (user, group, &size, &groups, limit);
227 /* Try to set the maximum number of groups the kernel can handle. */
229 result = setgroups (ngroups, groups);
230 while (result == -1 && errno == EINVAL && --ngroups > 0);
238 static_link_warning (initgroups)