]>
Commit | Line | Data |
---|---|---|
6d7e8eda | 1 | /* Copyright (C) 1998-2023 Free Software Foundation, Inc. |
d67281a7 | 2 | This file is part of the GNU C Library. |
d67281a7 UD |
3 | |
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. | |
d67281a7 UD |
8 | |
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. |
d67281a7 | 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/>. */ |
d67281a7 | 17 | |
0891f970 | 18 | #include <alloca.h> |
c207f23b | 19 | #include <assert.h> |
d67281a7 UD |
20 | #include <errno.h> |
21 | #include <grp.h> | |
390955cb | 22 | #include <stdint.h> |
d67281a7 UD |
23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
c207f23b | 27 | #include <sys/mman.h> |
d67281a7 | 28 | #include <sys/socket.h> |
264d5b94 | 29 | #include <sys/uio.h> |
d67281a7 | 30 | #include <sys/un.h> |
40c38b6c | 31 | #include <not-cancel.h> |
eb96ffb0 | 32 | #include <_itoa.h> |
561052ad | 33 | #include <scratch_buffer.h> |
d67281a7 | 34 | |
3f804c95 | 35 | #include "nscd-client.h" |
d67281a7 UD |
36 | #include "nscd_proto.h" |
37 | ||
ac16e905 UD |
38 | int __nss_not_use_nscd_group; |
39 | ||
67479a70 UD |
40 | static int nscd_getgr_r (const char *key, size_t keylen, request_type type, |
41 | struct group *resultbuf, char *buffer, | |
75b3047e | 42 | size_t buflen, struct group **result); |
67479a70 | 43 | |
d67281a7 UD |
44 | |
45 | int | |
46 | __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer, | |
261eada2 | 47 | size_t buflen, struct group **result) |
d67281a7 | 48 | { |
67479a70 | 49 | return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf, |
261eada2 | 50 | buffer, buflen, result); |
d67281a7 UD |
51 | } |
52 | ||
67479a70 | 53 | |
d67281a7 UD |
54 | int |
55 | __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer, | |
261eada2 | 56 | size_t buflen, struct group **result) |
d67281a7 | 57 | { |
40c38b6c UD |
58 | char buf[3 * sizeof (gid_t)]; |
59 | buf[sizeof (buf) - 1] = '\0'; | |
60 | char *cp = _itoa_word (gid, buf + sizeof (buf) - 1, 10, 0); | |
d67281a7 | 61 | |
40c38b6c UD |
62 | return nscd_getgr_r (cp, buf + sizeof (buf) - cp, GETGRBYGID, resultbuf, |
63 | buffer, buflen, result); | |
d67281a7 UD |
64 | } |
65 | ||
67479a70 | 66 | |
6f8a7dff | 67 | libc_locked_map_ptr (,__gr_map_handle) attribute_hidden; |
c207f23b UD |
68 | /* Note that we only free the structure if necessary. The memory |
69 | mapping is not removed since it is not visible to the malloc | |
70 | handling. */ | |
71 | libc_freeres_fn (gr_map_free) | |
72 | { | |
ed2ced8a UD |
73 | if (__gr_map_handle.mapped != NO_MAPPING) |
74 | { | |
75 | void *p = __gr_map_handle.mapped; | |
76 | __gr_map_handle.mapped = NO_MAPPING; | |
77 | free (p); | |
78 | } | |
c207f23b UD |
79 | } |
80 | ||
81 | ||
d67281a7 | 82 | static int |
67479a70 | 83 | nscd_getgr_r (const char *key, size_t keylen, request_type type, |
a95a08b4 | 84 | struct group *resultbuf, char *buffer, size_t buflen, |
261eada2 | 85 | struct group **result) |
d67281a7 | 86 | { |
c207f23b | 87 | int gc_cycle; |
1a77d37f | 88 | int nretries = 0; |
0891f970 | 89 | const uint32_t *len = NULL; |
561052ad FW |
90 | struct scratch_buffer lenbuf; |
91 | scratch_buffer_init (&lenbuf); | |
c207f23b UD |
92 | |
93 | /* If the mapping is available, try to search there instead of | |
94 | communicating with the nscd. */ | |
95 | struct mapped_database *mapped = __nscd_get_map_ref (GETFDGR, "group", | |
ed2ced8a UD |
96 | &__gr_map_handle, |
97 | &gc_cycle); | |
0891f970 | 98 | retry:; |
0891f970 UD |
99 | const char *gr_name = NULL; |
100 | size_t gr_name_len = 0; | |
101 | int retval = -1; | |
102 | const char *recend = (const char *) ~UINTMAX_C (0); | |
1a77d37f | 103 | gr_response_header gr_resp; |
0891f970 | 104 | |
c207f23b | 105 | if (mapped != NO_MAPPING) |
ac16e905 | 106 | { |
cfe1fc10 JJ |
107 | struct datahead *found = __nscd_cache_search (type, key, keylen, mapped, |
108 | sizeof gr_resp); | |
c207f23b UD |
109 | if (found != NULL) |
110 | { | |
1a77d37f JJ |
111 | len = (const uint32_t *) (&found->data[0].grdata + 1); |
112 | gr_resp = found->data[0].grdata; | |
c207f23b | 113 | gr_name = ((const char *) len |
1a77d37f JJ |
114 | + gr_resp.gr_mem_cnt * sizeof (uint32_t)); |
115 | gr_name_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len; | |
c207f23b | 116 | recend = (const char *) found->data + found->recsize; |
1a77d37f JJ |
117 | /* Now check if we can trust gr_resp fields. If GC is |
118 | in progress, it can contain anything. */ | |
119 | if (mapped->head->gc_cycle != gc_cycle) | |
120 | { | |
121 | retval = -2; | |
122 | goto out; | |
123 | } | |
124 | ||
125 | /* The alignment is always sufficient, unless GC is in progress. */ | |
126 | assert (((uintptr_t) len & (__alignof__ (*len) - 1)) == 0); | |
c207f23b UD |
127 | } |
128 | } | |
129 | ||
c207f23b | 130 | int sock = -1; |
1a77d37f | 131 | if (gr_name == NULL) |
c207f23b | 132 | { |
1a77d37f JJ |
133 | sock = __nscd_open_socket (key, keylen, type, &gr_resp, |
134 | sizeof (gr_resp)); | |
c207f23b UD |
135 | if (sock == -1) |
136 | { | |
137 | __nss_not_use_nscd_group = 1; | |
138 | goto out; | |
139 | } | |
ac16e905 | 140 | } |
d67281a7 | 141 | |
16aac663 UD |
142 | /* No value found so far. */ |
143 | *result = NULL; | |
144 | ||
a1ffb40e | 145 | if (__glibc_unlikely (gr_resp.found == -1)) |
d67281a7 | 146 | { |
ac16e905 | 147 | /* The daemon does not cache this database. */ |
ac16e905 | 148 | __nss_not_use_nscd_group = 1; |
c207f23b | 149 | goto out_close; |
d67281a7 UD |
150 | } |
151 | ||
1a77d37f | 152 | if (gr_resp.found == 1) |
d67281a7 | 153 | { |
40c38b6c | 154 | struct iovec vec[2]; |
d67281a7 | 155 | char *p = buffer; |
264d5b94 UD |
156 | size_t total_len; |
157 | uintptr_t align; | |
eb64f8cb | 158 | nscd_ssize_t cnt; |
d67281a7 | 159 | |
67479a70 UD |
160 | /* Now allocate the buffer the array for the group members. We must |
161 | align the pointer. */ | |
162 | align = ((__alignof__ (char *) - (p - ((char *) 0))) | |
163 | & (__alignof__ (char *) - 1)); | |
1a77d37f JJ |
164 | total_len = (align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *) |
165 | + gr_resp.gr_name_len + gr_resp.gr_passwd_len); | |
a1ffb40e | 166 | if (__glibc_unlikely (buflen < total_len)) |
d67281a7 | 167 | { |
67479a70 | 168 | no_room: |
d67281a7 | 169 | __set_errno (ERANGE); |
261eada2 | 170 | retval = ERANGE; |
c207f23b | 171 | goto out_close; |
d67281a7 | 172 | } |
bd952512 | 173 | buflen -= total_len; |
264d5b94 | 174 | |
67479a70 | 175 | p += align; |
a95a08b4 | 176 | resultbuf->gr_mem = (char **) p; |
1a77d37f | 177 | p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *); |
264d5b94 | 178 | |
67479a70 | 179 | /* Set pointers for strings. */ |
a95a08b4 | 180 | resultbuf->gr_name = p; |
1a77d37f | 181 | p += gr_resp.gr_name_len; |
a95a08b4 | 182 | resultbuf->gr_passwd = p; |
1a77d37f | 183 | p += gr_resp.gr_passwd_len; |
d67281a7 | 184 | |
67479a70 | 185 | /* Fill in what we know now. */ |
1a77d37f | 186 | resultbuf->gr_gid = gr_resp.gr_gid; |
264d5b94 | 187 | |
c207f23b | 188 | /* Read the length information, group name, and password. */ |
0891f970 | 189 | if (gr_name == NULL) |
c207f23b | 190 | { |
43d3e6bd | 191 | /* Handle a simple, usual case: no group members. */ |
a1ffb40e | 192 | if (__glibc_likely (gr_resp.gr_mem_cnt == 0)) |
0891f970 | 193 | { |
43d3e6bd UD |
194 | size_t n = gr_resp.gr_name_len + gr_resp.gr_passwd_len; |
195 | if (__builtin_expect (__readall (sock, resultbuf->gr_name, n) | |
196 | != (ssize_t) n, 0)) | |
197 | goto out_close; | |
198 | } | |
199 | else | |
200 | { | |
201 | /* Allocate array to store lengths. */ | |
561052ad FW |
202 | if (!scratch_buffer_set_array_size |
203 | (&lenbuf, gr_resp.gr_mem_cnt, sizeof (uint32_t))) | |
204 | goto out_close; | |
205 | len = lenbuf.data; | |
43d3e6bd UD |
206 | |
207 | vec[0].iov_base = (void *) len; | |
208 | vec[0].iov_len = gr_resp.gr_mem_cnt * sizeof (uint32_t); | |
209 | vec[1].iov_base = resultbuf->gr_name; | |
210 | vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len; | |
211 | total_len = vec[0].iov_len + vec[1].iov_len; | |
212 | ||
213 | /* Get this data. */ | |
214 | size_t n = __readvall (sock, vec, 2); | |
a1ffb40e | 215 | if (__glibc_unlikely (n != total_len)) |
43d3e6bd | 216 | goto out_close; |
0891f970 | 217 | } |
c207f23b UD |
218 | } |
219 | else | |
220 | /* We already have the data. Just copy the group name and | |
221 | password. */ | |
5429ff76 | 222 | memcpy (resultbuf->gr_name, gr_name, |
1a77d37f | 223 | gr_resp.gr_name_len + gr_resp.gr_passwd_len); |
264d5b94 | 224 | |
67479a70 | 225 | /* Clear the terminating entry. */ |
1a77d37f | 226 | resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL; |
d67281a7 | 227 | |
67479a70 UD |
228 | /* Prepare reading the group members. */ |
229 | total_len = 0; | |
1a77d37f | 230 | for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt) |
d67281a7 | 231 | { |
a95a08b4 | 232 | resultbuf->gr_mem[cnt] = p; |
67479a70 UD |
233 | total_len += len[cnt]; |
234 | p += len[cnt]; | |
d67281a7 | 235 | } |
d67281a7 | 236 | |
a1ffb40e | 237 | if (__glibc_unlikely (gr_name + gr_name_len + total_len > recend)) |
1a77d37f JJ |
238 | { |
239 | /* len array might contain garbage during nscd GC cycle, | |
240 | retry rather than fail in that case. */ | |
241 | if (gr_name != NULL && mapped->head->gc_cycle != gc_cycle) | |
242 | retval = -2; | |
243 | goto out_close; | |
244 | } | |
a1ffb40e | 245 | if (__glibc_unlikely (total_len > buflen)) |
1a77d37f JJ |
246 | { |
247 | /* len array might contain garbage during nscd GC cycle, | |
248 | retry rather than fail in that case. */ | |
249 | if (gr_name != NULL && mapped->head->gc_cycle != gc_cycle) | |
250 | { | |
251 | retval = -2; | |
252 | goto out_close; | |
253 | } | |
254 | else | |
255 | goto no_room; | |
256 | } | |
d67281a7 | 257 | |
a95a08b4 | 258 | retval = 0; |
43d3e6bd UD |
259 | |
260 | /* If there are no group members TOTAL_LEN is zero. */ | |
fa76dde2 | 261 | if (gr_name == NULL) |
d67281a7 | 262 | { |
895a08c0 UD |
263 | if (total_len > 0 |
264 | && __builtin_expect (__readall (sock, resultbuf->gr_mem[0], | |
265 | total_len) != total_len, 0)) | |
c207f23b | 266 | { |
895a08c0 UD |
267 | /* The `errno' to some value != ERANGE. */ |
268 | __set_errno (ENOENT); | |
269 | retval = ENOENT; | |
c207f23b | 270 | } |
895a08c0 UD |
271 | else |
272 | *result = resultbuf; | |
fa76dde2 UD |
273 | } |
274 | else | |
275 | { | |
276 | /* Copy the group member names. */ | |
277 | memcpy (resultbuf->gr_mem[0], gr_name + gr_name_len, total_len); | |
278 | ||
279 | /* Try to detect corrupt databases. */ | |
280 | if (resultbuf->gr_name[gr_name_len - 1] != '\0' | |
281 | || resultbuf->gr_passwd[gr_resp.gr_passwd_len - 1] != '\0' | |
282 | || ({for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt) | |
283 | if (resultbuf->gr_mem[cnt][len[cnt] - 1] != '\0') | |
284 | break; | |
285 | cnt < gr_resp.gr_mem_cnt; })) | |
5429ff76 | 286 | { |
fa76dde2 UD |
287 | /* We cannot use the database. */ |
288 | retval = mapped->head->gc_cycle != gc_cycle ? -2 : -1; | |
289 | goto out_close; | |
5429ff76 | 290 | } |
fa76dde2 UD |
291 | |
292 | *result = resultbuf; | |
c207f23b | 293 | } |
14e9dd67 UD |
294 | } |
295 | else | |
296 | { | |
cfca0aa3 UD |
297 | /* Set errno to 0 to indicate no error, just no found record. */ |
298 | __set_errno (0); | |
261eada2 UD |
299 | /* Even though we have not found anything, the result is zero. */ |
300 | retval = 0; | |
d67281a7 | 301 | } |
12c80513 | 302 | |
c207f23b UD |
303 | out_close: |
304 | if (sock != -1) | |
c181840c | 305 | __close_nocancel_nostatus (sock); |
12c80513 | 306 | out: |
1a77d37f | 307 | if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0) |
0891f970 UD |
308 | { |
309 | /* When we come here this means there has been a GC cycle while we | |
310 | were looking for the data. This means the data might have been | |
311 | inconsistent. Retry if possible. */ | |
1a77d37f | 312 | if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1) |
0891f970 UD |
313 | { |
314 | /* nscd is just running gc now. Disable using the mapping. */ | |
a364a3a7 | 315 | if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1) |
1a77d37f | 316 | __nscd_unmap (mapped); |
0891f970 UD |
317 | mapped = NO_MAPPING; |
318 | } | |
319 | ||
1a77d37f JJ |
320 | if (retval != -1) |
321 | goto retry; | |
0891f970 | 322 | } |
12c80513 | 323 | |
561052ad FW |
324 | scratch_buffer_free (&lenbuf); |
325 | ||
261eada2 | 326 | return retval; |
d67281a7 | 327 | } |