Support mcount/gprof test with GCC defaulting to PIE
[glibc.git] / grp / initgroups.c
blob0d5b841796f127cddaff36aac5efa2cb8e6b39d5
1 /* Copyright (C) 1989, 1991-2017 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <assert.h>
19 #include <errno.h>
20 #include <grp.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/param.h>
26 #include <sys/types.h>
27 #include <nsswitch.h>
28 #include <scratch_buffer.h>
30 #include "../nscd/nscd-client.h"
31 #include "../nscd/nscd_proto.h"
34 /* Type of the lookup function. */
35 typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t,
36 long int *, long int *,
37 gid_t **, long int, int *);
39 extern service_user *__nss_group_database attribute_hidden;
40 service_user *__nss_initgroups_database;
41 static bool use_initgroups_entry;
44 #include "compat-initgroups.c"
47 static int
48 internal_getgrouplist (const char *user, gid_t group, long int *size,
49 gid_t **groupsp, long int limit)
51 #ifdef USE_NSCD
52 if (__nss_not_use_nscd_group > 0
53 && ++__nss_not_use_nscd_group > NSS_NSCD_RETRY)
54 __nss_not_use_nscd_group = 0;
55 if (!__nss_not_use_nscd_group
56 && !__nss_database_custom[NSS_DBSIDX_group])
58 int n = __nscd_getgrouplist (user, group, size, groupsp, limit);
59 if (n >= 0)
60 return n;
62 /* nscd is not usable. */
63 __nss_not_use_nscd_group = 1;
65 #endif
67 enum nss_status status = NSS_STATUS_UNAVAIL;
68 int no_more = 0;
70 /* Never store more than the starting *SIZE number of elements. */
71 assert (*size > 0);
72 (*groupsp)[0] = group;
73 /* Start is one, because we have the first group as parameter. */
74 long int start = 1;
76 if (__nss_initgroups_database == NULL)
78 if (__nss_database_lookup ("initgroups", NULL, "",
79 &__nss_initgroups_database) < 0)
81 if (__nss_group_database == NULL)
82 no_more = __nss_database_lookup ("group", NULL, "compat files",
83 &__nss_group_database);
85 __nss_initgroups_database = __nss_group_database;
87 else
88 use_initgroups_entry = true;
90 else
91 /* __nss_initgroups_database might have been set through
92 __nss_configure_lookup in which case use_initgroups_entry was
93 not set here. */
94 use_initgroups_entry = __nss_initgroups_database != __nss_group_database;
96 service_user *nip = __nss_initgroups_database;
97 while (! no_more)
99 long int prev_start = start;
101 initgroups_dyn_function fct = __nss_lookup_function (nip,
102 "initgroups_dyn");
103 if (fct == NULL)
104 status = compat_call (nip, user, group, &start, size, groupsp,
105 limit, &errno);
106 else
107 status = DL_CALL_FCT (fct, (user, group, &start, size, groupsp,
108 limit, &errno));
110 /* Remove duplicates. */
111 long int cnt = prev_start;
112 while (cnt < start)
114 long int inner;
115 for (inner = 0; inner < prev_start; ++inner)
116 if ((*groupsp)[inner] == (*groupsp)[cnt])
117 break;
119 if (inner < prev_start)
120 (*groupsp)[cnt] = (*groupsp)[--start];
121 else
122 ++cnt;
125 /* This is really only for debugging. */
126 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN)
127 __libc_fatal ("illegal status in internal_getgrouplist");
129 /* For compatibility reason we will continue to look for more
130 entries using the next service even though data has already
131 been found if the nsswitch.conf file contained only a 'groups'
132 line and no 'initgroups' line. If the latter is available
133 we always respect the status. This means that the default
134 for successful lookups is to return. */
135 if ((use_initgroups_entry || status != NSS_STATUS_SUCCESS)
136 && nss_next_action (nip, status) == NSS_ACTION_RETURN)
137 break;
139 if (nip->next == NULL)
140 no_more = -1;
141 else
142 nip = nip->next;
145 return start;
148 /* Store at most *NGROUPS members of the group set for USER into
149 *GROUPS. Also include GROUP. The actual number of groups found is
150 returned in *NGROUPS. Return -1 if the if *NGROUPS is too small. */
152 getgrouplist (const char *user, gid_t group, gid_t *groups, int *ngroups)
154 long int size = MAX (1, *ngroups);
156 gid_t *newgroups = (gid_t *) malloc (size * sizeof (gid_t));
157 if (__glibc_unlikely (newgroups == NULL))
158 /* No more memory. */
159 // XXX This is wrong. The user provided memory, we have to use
160 // XXX it. The internal functions must be called with the user
161 // XXX provided buffer and not try to increase the size if it is
162 // XXX too small. For initgroups a flag could say: increase size.
163 return -1;
165 int total = internal_getgrouplist (user, group, &size, &newgroups, -1);
167 memcpy (groups, newgroups, MIN (*ngroups, total) * sizeof (gid_t));
169 free (newgroups);
171 int retval = total > *ngroups ? -1 : total;
172 *ngroups = total;
174 return retval;
177 nss_interface_function (getgrouplist)
179 /* Initialize the group set for the current user
180 by reading the group database and using all groups
181 of which USER is a member. Also include GROUP. */
183 initgroups (const char *user, gid_t group)
185 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
187 /* No extra groups allowed. */
188 return 0;
190 #else
192 long int size;
193 gid_t *groups;
194 int ngroups;
195 int result;
197 /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
198 limit can be raised in the kernel configuration without having to
199 recompile libc. */
200 long int limit = __sysconf (_SC_NGROUPS_MAX);
202 if (limit > 0)
203 /* We limit the size of the intially allocated array. */
204 size = MIN (limit, 64);
205 else
206 /* No fixed limit on groups. Pick a starting buffer size. */
207 size = 16;
209 groups = (gid_t *) malloc (size * sizeof (gid_t));
210 if (__glibc_unlikely (groups == NULL))
211 /* No more memory. */
212 return -1;
214 ngroups = internal_getgrouplist (user, group, &size, &groups, limit);
216 /* Try to set the maximum number of groups the kernel can handle. */
218 result = setgroups (ngroups, groups);
219 while (result == -1 && errno == EINVAL && --ngroups > 0);
221 free (groups);
223 return result;
224 #endif
227 nss_interface_function (initgroups)