Hurd: fix mode type for openat
[glibc.git] / nscd / nscd_gethst_r.c
blobc1661f86d4ce301cb95cb6333e17ebd780876d0b
1 /* Copyright (C) 1998-2005, 2006, 2007, 2008, 2009, 2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
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.
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
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <errno.h>
21 #include <resolv.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <arpa/nameser.h>
25 #include <not-cancel.h>
27 #include "nscd-client.h"
28 #include "nscd_proto.h"
30 int __nss_not_use_nscd_hosts;
32 static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
33 struct hostent *resultbuf, char *buffer,
34 size_t buflen, struct hostent **result,
35 int *h_errnop) internal_function;
38 int
39 __nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
40 char *buffer, size_t buflen, struct hostent **result,
41 int *h_errnop)
43 request_type reqtype;
45 reqtype = (_res.options & RES_USE_INET6) ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
47 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
48 buffer, buflen, result, h_errnop);
52 int
53 __nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
54 char *buffer, size_t buflen, struct hostent **result,
55 int *h_errnop)
57 request_type reqtype;
59 reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
61 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
62 buffer, buflen, result, h_errnop);
66 int
67 __nscd_gethostbyaddr_r (const void *addr, socklen_t len, int type,
68 struct hostent *resultbuf, char *buffer, size_t buflen,
69 struct hostent **result, int *h_errnop)
71 request_type reqtype;
73 if (!((len == INADDRSZ && type == AF_INET)
74 || (len == IN6ADDRSZ && type == AF_INET6)))
75 /* LEN and TYPE do not match. */
76 return -1;
78 reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
80 return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen, result,
81 h_errnop);
85 libc_locked_map_ptr (, __hst_map_handle) attribute_hidden;
86 /* Note that we only free the structure if necessary. The memory
87 mapping is not removed since it is not visible to the malloc
88 handling. */
89 libc_freeres_fn (hst_map_free)
91 if (__hst_map_handle.mapped != NO_MAPPING)
93 void *p = __hst_map_handle.mapped;
94 __hst_map_handle.mapped = NO_MAPPING;
95 free (p);
100 uint32_t
101 __nscd_get_nl_timestamp (void)
103 if (__nss_not_use_nscd_hosts != 0)
104 return 0;
106 struct mapped_database *map = __hst_map_handle.mapped;
108 if (map == NULL
109 || (map != NO_MAPPING
110 && map->head->nscd_certainly_running == 0
111 && map->head->timestamp + MAPPING_TIMEOUT < time (NULL)))
112 map = __nscd_get_mapping (GETFDHST, "hosts", &__hst_map_handle.mapped);
114 if (map == NO_MAPPING)
115 return 0;
117 return map->head->extra_data[NSCD_HST_IDX_CONF_TIMESTAMP];
121 int __nss_have_localdomain attribute_hidden;
123 static int
124 internal_function
125 nscd_gethst_r (const char *key, size_t keylen, request_type type,
126 struct hostent *resultbuf, char *buffer, size_t buflen,
127 struct hostent **result, int *h_errnop)
129 if (__builtin_expect (__nss_have_localdomain >= 0, 0))
131 if (__nss_have_localdomain == 0)
132 __nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
133 if (__nss_have_localdomain > 0)
135 __nss_not_use_nscd_hosts = 1;
136 return -1;
140 int gc_cycle;
141 int nretries = 0;
143 /* If the mapping is available, try to search there instead of
144 communicating with the nscd. */
145 struct mapped_database *mapped;
146 mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
147 &gc_cycle);
149 retry:;
150 const char *h_name = NULL;
151 const uint32_t *aliases_len = NULL;
152 const char *addr_list = NULL;
153 size_t addr_list_len = 0;
154 int retval = -1;
155 const char *recend = (const char *) ~UINTMAX_C (0);
156 int sock = -1;
157 hst_response_header hst_resp;
158 if (mapped != NO_MAPPING)
160 /* No const qualifier, as it can change during garbage collection. */
161 struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
162 sizeof hst_resp);
163 if (found != NULL)
165 h_name = (char *) (&found->data[0].hstdata + 1);
166 hst_resp = found->data[0].hstdata;
167 aliases_len = (uint32_t *) (h_name + hst_resp.h_name_len);
168 addr_list = ((char *) aliases_len
169 + hst_resp.h_aliases_cnt * sizeof (uint32_t));
170 addr_list_len = hst_resp.h_addr_list_cnt * INADDRSZ;
171 recend = (const char *) found->data + found->recsize;
172 /* Now check if we can trust hst_resp fields. If GC is
173 in progress, it can contain anything. */
174 if (mapped->head->gc_cycle != gc_cycle)
176 retval = -2;
177 goto out;
180 #ifndef _STRING_ARCH_unaligned
181 /* The aliases_len array in the mapped database might very
182 well be unaligned. We will access it word-wise so on
183 platforms which do not tolerate unaligned accesses we
184 need to make an aligned copy. */
185 if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
186 != 0)
188 uint32_t *tmp = alloca (hst_resp.h_aliases_cnt
189 * sizeof (uint32_t));
190 aliases_len = memcpy (tmp, aliases_len,
191 hst_resp.h_aliases_cnt
192 * sizeof (uint32_t));
194 #endif
195 if (type != GETHOSTBYADDR && type != GETHOSTBYNAME)
197 if (hst_resp.h_length == INADDRSZ)
198 addr_list += addr_list_len;
199 addr_list_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
201 if (__builtin_expect ((const char *) addr_list + addr_list_len
202 > recend, 0))
203 goto out;
207 if (h_name == NULL)
209 sock = __nscd_open_socket (key, keylen, type, &hst_resp,
210 sizeof (hst_resp));
211 if (sock == -1)
213 __nss_not_use_nscd_hosts = 1;
214 goto out;
218 /* No value found so far. */
219 *result = NULL;
221 if (__builtin_expect (hst_resp.found == -1, 0))
223 /* The daemon does not cache this database. */
224 __nss_not_use_nscd_hosts = 1;
225 goto out_close;
228 if (hst_resp.found == 1)
230 char *cp = buffer;
231 uintptr_t align1;
232 uintptr_t align2;
233 size_t total_len;
234 ssize_t cnt;
235 char *ignore;
236 int n;
238 /* A first check whether the buffer is sufficiently large is possible. */
239 /* Now allocate the buffer the array for the group members. We must
240 align the pointer and the base of the h_addr_list pointers. */
241 align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
242 & (__alignof__ (char *) - 1));
243 align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp.h_name_len)
244 - ((char *) 0)))
245 & (__alignof__ (char *) - 1));
246 if (buflen < (align1 + hst_resp.h_name_len + align2
247 + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt
248 + 2)
249 * sizeof (char *))
250 + hst_resp.h_addr_list_cnt * (type == AF_INET
251 ? INADDRSZ : IN6ADDRSZ)))
253 no_room:
254 *h_errnop = NETDB_INTERNAL;
255 __set_errno (ERANGE);
256 retval = ERANGE;
257 goto out_close;
259 cp += align1;
261 /* Prepare the result as far as we can. */
262 resultbuf->h_aliases = (char **) cp;
263 cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
264 resultbuf->h_addr_list = (char **) cp;
265 cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
267 resultbuf->h_name = cp;
268 cp += hst_resp.h_name_len + align2;
270 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
272 resultbuf->h_addrtype = AF_INET;
273 resultbuf->h_length = INADDRSZ;
275 else
277 resultbuf->h_addrtype = AF_INET6;
278 resultbuf->h_length = IN6ADDRSZ;
280 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
282 resultbuf->h_addr_list[cnt] = cp;
283 cp += resultbuf->h_length;
285 resultbuf->h_addr_list[cnt] = NULL;
287 if (h_name == NULL)
289 struct iovec vec[4];
291 vec[0].iov_base = resultbuf->h_name;
292 vec[0].iov_len = hst_resp.h_name_len;
293 total_len = hst_resp.h_name_len;
294 n = 1;
296 if (hst_resp.h_aliases_cnt > 0)
298 aliases_len = alloca (hst_resp.h_aliases_cnt
299 * sizeof (uint32_t));
300 vec[n].iov_base = (void *) aliases_len;
301 vec[n].iov_len = hst_resp.h_aliases_cnt * sizeof (uint32_t);
303 total_len += hst_resp.h_aliases_cnt * sizeof (uint32_t);
304 ++n;
307 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
309 vec[n].iov_base = resultbuf->h_addr_list[0];
310 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
312 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
314 ++n;
316 else
318 if (hst_resp.h_length == INADDRSZ)
320 ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
321 vec[n].iov_base = ignore;
322 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
324 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
326 ++n;
329 vec[n].iov_base = resultbuf->h_addr_list[0];
330 vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
332 total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
334 ++n;
337 if ((size_t) __readvall (sock, vec, n) != total_len)
338 goto out_close;
340 else
342 memcpy (resultbuf->h_name, h_name, hst_resp.h_name_len);
343 memcpy (resultbuf->h_addr_list[0], addr_list, addr_list_len);
346 /* Now we also can read the aliases. */
347 total_len = 0;
348 for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
350 resultbuf->h_aliases[cnt] = cp;
351 cp += aliases_len[cnt];
352 total_len += aliases_len[cnt];
354 resultbuf->h_aliases[cnt] = NULL;
356 if (__builtin_expect ((const char *) addr_list + addr_list_len
357 + total_len > recend, 0))
359 /* aliases_len array might contain garbage during nscd GC cycle,
360 retry rather than fail in that case. */
361 if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
362 retval = -2;
363 goto out_close;
365 /* See whether this would exceed the buffer capacity. */
366 if (__builtin_expect (cp > buffer + buflen, 0))
368 /* aliases_len array might contain garbage during nscd GC cycle,
369 retry rather than fail in that case. */
370 if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
372 retval = -2;
373 goto out_close;
375 goto no_room;
378 /* And finally read the aliases. */
379 if (addr_list == NULL)
381 if (total_len == 0
382 || ((size_t) __readall (sock, resultbuf->h_aliases[0], total_len)
383 == total_len))
385 retval = 0;
386 *result = resultbuf;
389 else
391 memcpy (resultbuf->h_aliases[0],
392 (const char *) addr_list + addr_list_len, total_len);
394 /* Try to detect corrupt databases. */
395 if (resultbuf->h_name[hst_resp.h_name_len - 1] != '\0'
396 || ({for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
397 if (resultbuf->h_aliases[cnt][aliases_len[cnt] - 1]
398 != '\0')
399 break;
400 cnt < hst_resp.h_aliases_cnt; }))
402 /* We cannot use the database. */
403 if (mapped->head->gc_cycle != gc_cycle)
404 retval = -2;
405 goto out_close;
408 retval = 0;
409 *result = resultbuf;
412 else
414 /* Store the error number. */
415 *h_errnop = hst_resp.error;
417 /* Set errno to 0 to indicate no error, just no found record. */
418 __set_errno (0);
419 /* Even though we have not found anything, the result is zero. */
420 retval = 0;
423 out_close:
424 if (sock != -1)
425 close_not_cancel_no_status (sock);
426 out:
427 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
429 /* When we come here this means there has been a GC cycle while we
430 were looking for the data. This means the data might have been
431 inconsistent. Retry if possible. */
432 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
434 /* nscd is just running gc now. Disable using the mapping. */
435 if (atomic_decrement_val (&mapped->counter) == 0)
436 __nscd_unmap (mapped);
437 mapped = NO_MAPPING;
440 if (retval != -1)
441 goto retry;
444 return retval;