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