]> git.ipfire.org Git - thirdparty/glibc.git/blame - nscd/nscd_getgr_r.c
Check __x86_64__ instead of __WORDSIZE in mathinline.h
[thirdparty/glibc.git] / nscd / nscd_getgr_r.c
CommitLineData
cfe1fc10 1/* Copyright (C) 1998-2000, 2002-2005, 2006, 2007, 2009
1a77d37f 2 Free Software Foundation, Inc.
d67281a7
UD
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
d67281a7
UD
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
d67281a7 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
d67281a7 19
0891f970 20#include <alloca.h>
c207f23b 21#include <assert.h>
d67281a7
UD
22#include <errno.h>
23#include <grp.h>
390955cb 24#include <stdint.h>
d67281a7
UD
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
c207f23b 29#include <sys/mman.h>
d67281a7 30#include <sys/socket.h>
264d5b94 31#include <sys/uio.h>
d67281a7 32#include <sys/un.h>
40c38b6c 33#include <not-cancel.h>
9b9ef823 34#include <stdio-common/_itoa.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
UD
92 const uint32_t *len = NULL;
93 size_t lensize = 0;
c207f23b
UD
94
95 /* If the mapping is available, try to search there instead of
96 communicating with the nscd. */
97 struct mapped_database *mapped = __nscd_get_map_ref (GETFDGR, "group",
ed2ced8a
UD
98 &__gr_map_handle,
99 &gc_cycle);
0891f970 100 retry:;
0891f970
UD
101 const char *gr_name = NULL;
102 size_t gr_name_len = 0;
103 int retval = -1;
104 const char *recend = (const char *) ~UINTMAX_C (0);
1a77d37f 105 gr_response_header gr_resp;
0891f970 106
c207f23b 107 if (mapped != NO_MAPPING)
ac16e905 108 {
cfe1fc10
JJ
109 struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
110 sizeof gr_resp);
c207f23b
UD
111 if (found != NULL)
112 {
1a77d37f
JJ
113 len = (const uint32_t *) (&found->data[0].grdata + 1);
114 gr_resp = found->data[0].grdata;
c207f23b 115 gr_name = ((const char *) len
1a77d37f
JJ
116 + gr_resp.gr_mem_cnt * sizeof (uint32_t));
117 gr_name_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
c207f23b 118 recend = (const char *) found->data + found->recsize;
1a77d37f
JJ
119 /* Now check if we can trust gr_resp fields. If GC is
120 in progress, it can contain anything. */
121 if (mapped->head->gc_cycle != gc_cycle)
122 {
123 retval = -2;
124 goto out;
125 }
126
127 /* The alignment is always sufficient, unless GC is in progress. */
128 assert (((uintptr_t) len & (__alignof__ (*len) - 1)) == 0);
c207f23b
UD
129 }
130 }
131
c207f23b 132 int sock = -1;
1a77d37f 133 if (gr_name == NULL)
c207f23b 134 {
1a77d37f
JJ
135 sock = __nscd_open_socket (key, keylen, type, &gr_resp,
136 sizeof (gr_resp));
c207f23b
UD
137 if (sock == -1)
138 {
139 __nss_not_use_nscd_group = 1;
140 goto out;
141 }
ac16e905 142 }
d67281a7 143
16aac663
UD
144 /* No value found so far. */
145 *result = NULL;
146
1a77d37f 147 if (__builtin_expect (gr_resp.found == -1, 0))
d67281a7 148 {
ac16e905 149 /* The daemon does not cache this database. */
ac16e905 150 __nss_not_use_nscd_group = 1;
c207f23b 151 goto out_close;
d67281a7
UD
152 }
153
1a77d37f 154 if (gr_resp.found == 1)
d67281a7 155 {
40c38b6c 156 struct iovec vec[2];
d67281a7 157 char *p = buffer;
264d5b94
UD
158 size_t total_len;
159 uintptr_t align;
eb64f8cb 160 nscd_ssize_t cnt;
d67281a7 161
67479a70
UD
162 /* Now allocate the buffer the array for the group members. We must
163 align the pointer. */
164 align = ((__alignof__ (char *) - (p - ((char *) 0)))
165 & (__alignof__ (char *) - 1));
1a77d37f
JJ
166 total_len = (align + (1 + gr_resp.gr_mem_cnt) * sizeof (char *)
167 + gr_resp.gr_name_len + gr_resp.gr_passwd_len);
12c80513 168 if (__builtin_expect (buflen < total_len, 0))
d67281a7 169 {
67479a70 170 no_room:
d67281a7 171 __set_errno (ERANGE);
261eada2 172 retval = ERANGE;
c207f23b 173 goto out_close;
d67281a7 174 }
bd952512 175 buflen -= total_len;
264d5b94 176
67479a70 177 p += align;
a95a08b4 178 resultbuf->gr_mem = (char **) p;
1a77d37f 179 p += (1 + gr_resp.gr_mem_cnt) * sizeof (char *);
264d5b94 180
67479a70 181 /* Set pointers for strings. */
a95a08b4 182 resultbuf->gr_name = p;
1a77d37f 183 p += gr_resp.gr_name_len;
a95a08b4 184 resultbuf->gr_passwd = p;
1a77d37f 185 p += gr_resp.gr_passwd_len;
d67281a7 186
67479a70 187 /* Fill in what we know now. */
1a77d37f 188 resultbuf->gr_gid = gr_resp.gr_gid;
264d5b94 189
c207f23b 190 /* Read the length information, group name, and password. */
0891f970 191 if (gr_name == NULL)
c207f23b 192 {
43d3e6bd
UD
193 /* Handle a simple, usual case: no group members. */
194 if (__builtin_expect (gr_resp.gr_mem_cnt == 0, 1))
0891f970 195 {
43d3e6bd
UD
196 size_t n = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
197 if (__builtin_expect (__readall (sock, resultbuf->gr_name, n)
198 != (ssize_t) n, 0))
199 goto out_close;
200 }
201 else
202 {
203 /* Allocate array to store lengths. */
204 if (lensize == 0)
205 {
206 lensize = gr_resp.gr_mem_cnt * sizeof (uint32_t);
207 len = (uint32_t *) alloca (lensize);
208 }
209 else if (gr_resp.gr_mem_cnt * sizeof (uint32_t) > lensize)
210 len = extend_alloca (len, lensize,
211 gr_resp.gr_mem_cnt * sizeof (uint32_t));
212
213 vec[0].iov_base = (void *) len;
214 vec[0].iov_len = gr_resp.gr_mem_cnt * sizeof (uint32_t);
215 vec[1].iov_base = resultbuf->gr_name;
216 vec[1].iov_len = gr_resp.gr_name_len + gr_resp.gr_passwd_len;
217 total_len = vec[0].iov_len + vec[1].iov_len;
218
219 /* Get this data. */
220 size_t n = __readvall (sock, vec, 2);
221 if (__builtin_expect (n != total_len, 0))
222 goto out_close;
0891f970 223 }
c207f23b
UD
224 }
225 else
226 /* We already have the data. Just copy the group name and
227 password. */
5429ff76 228 memcpy (resultbuf->gr_name, gr_name,
1a77d37f 229 gr_resp.gr_name_len + gr_resp.gr_passwd_len);
264d5b94 230
67479a70 231 /* Clear the terminating entry. */
1a77d37f 232 resultbuf->gr_mem[gr_resp.gr_mem_cnt] = NULL;
d67281a7 233
67479a70
UD
234 /* Prepare reading the group members. */
235 total_len = 0;
1a77d37f 236 for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
d67281a7 237 {
a95a08b4 238 resultbuf->gr_mem[cnt] = p;
67479a70
UD
239 total_len += len[cnt];
240 p += len[cnt];
d67281a7 241 }
d67281a7 242
c207f23b 243 if (__builtin_expect (gr_name + gr_name_len + total_len > recend, 0))
1a77d37f
JJ
244 {
245 /* len array might contain garbage during nscd GC cycle,
246 retry rather than fail in that case. */
247 if (gr_name != NULL && mapped->head->gc_cycle != gc_cycle)
248 retval = -2;
249 goto out_close;
250 }
12c80513 251 if (__builtin_expect (total_len > buflen, 0))
1a77d37f
JJ
252 {
253 /* len array might contain garbage during nscd GC cycle,
254 retry rather than fail in that case. */
255 if (gr_name != NULL && mapped->head->gc_cycle != gc_cycle)
256 {
257 retval = -2;
258 goto out_close;
259 }
260 else
261 goto no_room;
262 }
d67281a7 263
a95a08b4 264 retval = 0;
43d3e6bd
UD
265
266 /* If there are no group members TOTAL_LEN is zero. */
fa76dde2 267 if (gr_name == NULL)
d67281a7 268 {
895a08c0
UD
269 if (total_len > 0
270 && __builtin_expect (__readall (sock, resultbuf->gr_mem[0],
271 total_len) != total_len, 0))
c207f23b 272 {
895a08c0
UD
273 /* The `errno' to some value != ERANGE. */
274 __set_errno (ENOENT);
275 retval = ENOENT;
c207f23b 276 }
895a08c0
UD
277 else
278 *result = resultbuf;
fa76dde2
UD
279 }
280 else
281 {
282 /* Copy the group member names. */
283 memcpy (resultbuf->gr_mem[0], gr_name + gr_name_len, total_len);
284
285 /* Try to detect corrupt databases. */
286 if (resultbuf->gr_name[gr_name_len - 1] != '\0'
287 || resultbuf->gr_passwd[gr_resp.gr_passwd_len - 1] != '\0'
288 || ({for (cnt = 0; cnt < gr_resp.gr_mem_cnt; ++cnt)
289 if (resultbuf->gr_mem[cnt][len[cnt] - 1] != '\0')
290 break;
291 cnt < gr_resp.gr_mem_cnt; }))
5429ff76 292 {
fa76dde2
UD
293 /* We cannot use the database. */
294 retval = mapped->head->gc_cycle != gc_cycle ? -2 : -1;
295 goto out_close;
5429ff76 296 }
fa76dde2
UD
297
298 *result = resultbuf;
c207f23b 299 }
14e9dd67
UD
300 }
301 else
302 {
cfca0aa3
UD
303 /* Set errno to 0 to indicate no error, just no found record. */
304 __set_errno (0);
261eada2
UD
305 /* Even though we have not found anything, the result is zero. */
306 retval = 0;
d67281a7 307 }
12c80513 308
c207f23b
UD
309 out_close:
310 if (sock != -1)
311 close_not_cancel_no_status (sock);
12c80513 312 out:
1a77d37f 313 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
0891f970
UD
314 {
315 /* When we come here this means there has been a GC cycle while we
316 were looking for the data. This means the data might have been
317 inconsistent. Retry if possible. */
1a77d37f 318 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
0891f970
UD
319 {
320 /* nscd is just running gc now. Disable using the mapping. */
1a77d37f
JJ
321 if (atomic_decrement_val (&mapped->counter) == 0)
322 __nscd_unmap (mapped);
0891f970
UD
323 mapped = NO_MAPPING;
324 }
325
1a77d37f
JJ
326 if (retval != -1)
327 goto retry;
0891f970 328 }
12c80513 329
261eada2 330 return retval;
d67281a7 331}