[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[glibc.git] / nscd / nscd_getgr_r.c
blob922b906c19095d985196525355c4024fb32d4db6
1 /* Copyright (C) 1998-2000, 2002-2005, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <alloca.h>
21 #include <assert.h>
22 #include <errno.h>
23 #include <grp.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30 #include <sys/socket.h>
31 #include <sys/uio.h>
32 #include <sys/un.h>
33 #include <not-cancel.h>
34 #include <stdio-common/_itoa.h>
36 #include "nscd-client.h"
37 #include "nscd_proto.h"
39 int __nss_not_use_nscd_group;
41 static int nscd_getgr_r (const char *key, size_t keylen, request_type type,
42 struct group *resultbuf, char *buffer,
43 size_t buflen, struct group **result)
44 internal_function;
47 int
48 __nscd_getgrnam_r (const char *name, struct group *resultbuf, char *buffer,
49 size_t buflen, struct group **result)
51 return nscd_getgr_r (name, strlen (name) + 1, GETGRBYNAME, resultbuf,
52 buffer, buflen, result);
56 int
57 __nscd_getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer,
58 size_t buflen, struct group **result)
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);
64 return nscd_getgr_r (cp, buf + sizeof (buf) - cp, GETGRBYGID, resultbuf,
65 buffer, buflen, result);
69 libc_locked_map_ptr (,__gr_map_handle) attribute_hidden;
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. */
73 libc_freeres_fn (gr_map_free)
75 if (__gr_map_handle.mapped != NO_MAPPING)
77 void *p = __gr_map_handle.mapped;
78 __gr_map_handle.mapped = NO_MAPPING;
79 free (p);
84 static int
85 internal_function
86 nscd_getgr_r (const char *key, size_t keylen, request_type type,
87 struct group *resultbuf, char *buffer, size_t buflen,
88 struct group **result)
90 int gc_cycle;
91 const uint32_t *len = NULL;
92 size_t lensize = 0;
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",
97 &__gr_map_handle,
98 &gc_cycle);
99 retry:;
100 const gr_response_header *gr_resp = NULL;
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);
106 if (mapped != NO_MAPPING)
108 const struct datahead *found = __nscd_cache_search (type, key, keylen,
109 mapped);
110 if (found != NULL)
112 gr_resp = &found->data[0].grdata;
113 len = (const uint32_t *) (gr_resp + 1);
114 /* The alignment is always sufficient. */
115 assert (((uintptr_t) len & (__alignof__ (*len) - 1)) == 0);
116 gr_name = ((const char *) len
117 + gr_resp->gr_mem_cnt * sizeof (uint32_t));
118 gr_name_len = gr_resp->gr_name_len + gr_resp->gr_passwd_len;
119 recend = (const char *) found->data + found->recsize;
123 gr_response_header gr_resp_mem;
124 int sock = -1;
125 if (gr_resp == NULL)
127 sock = __nscd_open_socket (key, keylen, type, &gr_resp_mem,
128 sizeof (gr_resp_mem));
129 if (sock == -1)
131 __nss_not_use_nscd_group = 1;
132 goto out;
135 gr_resp = &gr_resp_mem;
138 /* No value found so far. */
139 *result = NULL;
141 if (__builtin_expect (gr_resp->found == -1, 0))
143 /* The daemon does not cache this database. */
144 __nss_not_use_nscd_group = 1;
145 goto out_close;
148 if (gr_resp->found == 1)
150 struct iovec vec[2];
151 char *p = buffer;
152 size_t total_len;
153 uintptr_t align;
154 nscd_ssize_t cnt;
156 /* Now allocate the buffer the array for the group members. We must
157 align the pointer. */
158 align = ((__alignof__ (char *) - (p - ((char *) 0)))
159 & (__alignof__ (char *) - 1));
160 total_len = (align + (1 + gr_resp->gr_mem_cnt) * sizeof (char *)
161 + gr_resp->gr_name_len + gr_resp->gr_passwd_len);
162 if (__builtin_expect (buflen < total_len, 0))
164 no_room:
165 __set_errno (ERANGE);
166 retval = ERANGE;
167 goto out_close;
169 buflen -= total_len;
171 p += align;
172 resultbuf->gr_mem = (char **) p;
173 p += (1 + gr_resp->gr_mem_cnt) * sizeof (char *);
175 /* Set pointers for strings. */
176 resultbuf->gr_name = p;
177 p += gr_resp->gr_name_len;
178 resultbuf->gr_passwd = p;
179 p += gr_resp->gr_passwd_len;
181 /* Fill in what we know now. */
182 resultbuf->gr_gid = gr_resp->gr_gid;
184 /* Read the length information, group name, and password. */
185 if (gr_name == NULL)
187 /* Allocate array to store lengths. */
188 if (lensize == 0)
190 lensize = gr_resp->gr_mem_cnt * sizeof (uint32_t);
191 len = (uint32_t *) alloca (lensize);
193 else if (gr_resp->gr_mem_cnt * sizeof (uint32_t) > lensize)
194 len = extend_alloca (len, lensize,
195 gr_resp->gr_mem_cnt * sizeof (uint32_t));
197 vec[0].iov_base = (void *) len;
198 vec[0].iov_len = gr_resp->gr_mem_cnt * sizeof (uint32_t);
199 vec[1].iov_base = resultbuf->gr_name;
200 vec[1].iov_len = gr_resp->gr_name_len + gr_resp->gr_passwd_len;
201 total_len = vec[0].iov_len + vec[1].iov_len;
203 /* Get this data. */
204 size_t n = __readvall (sock, vec, 2);
205 if (__builtin_expect (n != total_len, 0))
206 goto out_close;
208 else
209 /* We already have the data. Just copy the group name and
210 password. */
211 memcpy (resultbuf->gr_name, gr_name,
212 gr_resp->gr_name_len + gr_resp->gr_passwd_len);
214 /* Clear the terminating entry. */
215 resultbuf->gr_mem[gr_resp->gr_mem_cnt] = NULL;
217 /* Prepare reading the group members. */
218 total_len = 0;
219 for (cnt = 0; cnt < gr_resp->gr_mem_cnt; ++cnt)
221 resultbuf->gr_mem[cnt] = p;
222 total_len += len[cnt];
223 p += len[cnt];
226 if (__builtin_expect (gr_name + gr_name_len + total_len > recend, 0))
227 goto out_close;
228 if (__builtin_expect (total_len > buflen, 0))
229 goto no_room;
231 retval = 0;
232 if (gr_name == NULL)
234 size_t n = __readall (sock, resultbuf->gr_mem[0], total_len);
235 if (__builtin_expect (n != total_len, 0))
237 /* The `errno' to some value != ERANGE. */
238 __set_errno (ENOENT);
239 retval = ENOENT;
241 else
242 *result = resultbuf;
244 else
246 /* Copy the group member names. */
247 memcpy (resultbuf->gr_mem[0], gr_name + gr_name_len, total_len);
249 /* Try to detect corrupt databases. */
250 if (resultbuf->gr_name[gr_name_len - 1] != '\0'
251 || resultbuf->gr_passwd[gr_resp->gr_passwd_len - 1] != '\0'
252 || ({for (cnt = 0; cnt < gr_resp->gr_mem_cnt; ++cnt)
253 if (resultbuf->gr_mem[cnt][len[cnt] - 1] != '\0')
254 break;
255 cnt < gr_resp->gr_mem_cnt; }))
257 /* We cannot use the database. */
258 retval = -1;
259 goto out_close;
262 *result = resultbuf;
265 else
267 /* The `errno' to some value != ERANGE. */
268 __set_errno (ENOENT);
269 /* Even though we have not found anything, the result is zero. */
270 retval = 0;
273 out_close:
274 if (sock != -1)
275 close_not_cancel_no_status (sock);
276 out:
277 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0 && retval != -1)
279 /* When we come here this means there has been a GC cycle while we
280 were looking for the data. This means the data might have been
281 inconsistent. Retry if possible. */
282 if ((gc_cycle & 1) != 0)
284 /* nscd is just running gc now. Disable using the mapping. */
285 __nscd_unmap (mapped);
286 mapped = NO_MAPPING;
289 goto retry;
292 return retval;