1 /* Copyright (C) 1989,91,93,1996-2005,2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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
27 #include <sys/param.h>
28 #include <sys/types.h>
31 #include "../nscd/nscd-client.h"
32 #include "../nscd/nscd_proto.h"
35 /* Type of the lookup function. */
36 typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
37 long int *, long int *,
38 gid_t **, long int, int *);
40 /* The lookup function for the first entry of this service. */
41 extern int __nss_group_lookup (service_user **nip, const char *name,
43 extern void *__nss_lookup_function (service_user *ni, const char *fct_name);
45 extern service_user *__nss_group_database attribute_hidden;
48 #include "compat-initgroups.c"
52 internal_getgrouplist (const char *user, gid_t group, long int *size,
53 gid_t **groupsp, long int limit)
56 if (__nss_not_use_nscd_group > 0
57 && ++__nss_not_use_nscd_group > NSS_NSCD_RETRY)
58 __nss_not_use_nscd_group = 0;
59 if (!__nss_not_use_nscd_group)
61 int n = __nscd_getgrouplist (user, group, size, groupsp, limit);
65 /* nscd is not usable. */
66 __nss_not_use_nscd_group = 1;
70 service_user *nip = NULL;
71 initgroups_dyn_function fct;
72 enum nss_status status = NSS_STATUS_UNAVAIL;
74 /* Start is one, because we have the first group as parameter. */
77 /* Never store more than the starting *SIZE number of elements. */
79 (*groupsp)[0] = group;
81 if (__nss_group_database != NULL)
84 nip = __nss_group_database;
87 no_more = __nss_database_lookup ("group", NULL,
88 "compat [NOTFOUND=return] files", &nip);
92 long int prev_start = start;
94 fct = __nss_lookup_function (nip, "initgroups_dyn");
98 status = compat_call (nip, user, group, &start, size, groupsp,
101 if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE)
105 status = DL_CALL_FCT (fct, (user, group, &start, size, groupsp,
108 /* Remove duplicates. */
109 long int cnt = prev_start;
113 for (inner = 0; inner < prev_start; ++inner)
114 if ((*groupsp)[inner] == (*groupsp)[cnt])
117 if (inner < prev_start)
118 (*groupsp)[cnt] = (*groupsp)[--start];
123 /* This is really only for debugging. */
124 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
125 __libc_fatal ("illegal status in internal_getgrouplist");
127 if (status != NSS_STATUS_SUCCESS
128 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
131 if (nip->next == NULL)
140 /* Store at most *NGROUPS members of the group set for USER into
141 *GROUPS. Also include GROUP. The actual number of groups found is
142 returned in *NGROUPS. Return -1 if the if *NGROUPS is too small. */
144 getgrouplist (const char *user, gid_t group, gid_t *groups, int *ngroups)
146 long int size = MAX (1, *ngroups);
148 gid_t *newgroups = (gid_t *) malloc (size * sizeof (gid_t));
149 if (__builtin_expect (newgroups == NULL, 0))
150 /* No more memory. */
151 // XXX This is wrong. The user provided memory, we have to use
152 // XXX it. The internal functions must be called with the user
153 // XXX provided buffer and not try to increase the size if it is
154 // XXX too small. For initgroups a flag could say: increase size.
157 int total = internal_getgrouplist (user, group, &size, &newgroups, -1);
159 memcpy (groups, newgroups, MIN (*ngroups, total) * sizeof (gid_t));
163 int retval = total > *ngroups ? -1 : total;
169 static_link_warning (getgrouplist)
171 /* Initialize the group set for the current user
172 by reading the group database and using all groups
173 of which USER is a member. Also include GROUP. */
175 initgroups (const char *user, gid_t group)
177 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
179 /* No extra groups allowed. */
189 /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
190 limit can be raised in the kernel configuration without having to
192 long int limit = __sysconf (_SC_NGROUPS_MAX);
195 /* We limit the size of the intially allocated array. */
196 size = MIN (limit, 64);
198 /* No fixed limit on groups. Pick a starting buffer size. */
201 groups = (gid_t *) malloc (size * sizeof (gid_t));
202 if (__builtin_expect (groups == NULL, 0))
203 /* No more memory. */
206 ngroups = internal_getgrouplist (user, group, &size, &groups, limit);
208 /* Try to set the maximum number of groups the kernel can handle. */
210 result = setgroups (ngroups, groups);
211 while (result == -1 && errno == EINVAL && --ngroups > 0);
219 static_link_warning (initgroups)