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