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