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