1 /* Copyright (C) 1998-2005, 2006, 2007, 2008, 2009
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <arpa/nameser.h>
26 #include <not-cancel.h>
28 #include "nscd-client.h"
29 #include "nscd_proto.h"
31 int __nss_not_use_nscd_hosts
;
33 static int nscd_gethst_r (const char *key
, size_t keylen
, request_type type
,
34 struct hostent
*resultbuf
, char *buffer
,
35 size_t buflen
, struct hostent
**result
,
36 int *h_errnop
) internal_function
;
40 __nscd_gethostbyname_r (const char *name
, struct hostent
*resultbuf
,
41 char *buffer
, size_t buflen
, struct hostent
**result
,
46 reqtype
= (_res
.options
& RES_USE_INET6
) ? GETHOSTBYNAMEv6
: GETHOSTBYNAME
;
48 return nscd_gethst_r (name
, strlen (name
) + 1, reqtype
, resultbuf
,
49 buffer
, buflen
, result
, h_errnop
);
54 __nscd_gethostbyname2_r (const char *name
, int af
, struct hostent
*resultbuf
,
55 char *buffer
, size_t buflen
, struct hostent
**result
,
60 reqtype
= af
== AF_INET6
? GETHOSTBYNAMEv6
: GETHOSTBYNAME
;
62 return nscd_gethst_r (name
, strlen (name
) + 1, reqtype
, resultbuf
,
63 buffer
, buflen
, result
, h_errnop
);
68 __nscd_gethostbyaddr_r (const void *addr
, socklen_t len
, int type
,
69 struct hostent
*resultbuf
, char *buffer
, size_t buflen
,
70 struct hostent
**result
, int *h_errnop
)
74 if (!((len
== INADDRSZ
&& type
== AF_INET
)
75 || (len
== IN6ADDRSZ
&& type
== AF_INET6
)))
76 /* LEN and TYPE do not match. */
79 reqtype
= type
== AF_INET6
? GETHOSTBYADDRv6
: GETHOSTBYADDR
;
81 return nscd_gethst_r (addr
, len
, reqtype
, resultbuf
, buffer
, buflen
, result
,
86 libc_locked_map_ptr (, __hst_map_handle
) attribute_hidden
;
87 /* Note that we only free the structure if necessary. The memory
88 mapping is not removed since it is not visible to the malloc
90 libc_freeres_fn (hst_map_free
)
92 if (__hst_map_handle
.mapped
!= NO_MAPPING
)
94 void *p
= __hst_map_handle
.mapped
;
95 __hst_map_handle
.mapped
= NO_MAPPING
;
101 int __nss_have_localdomain attribute_hidden
;
105 nscd_gethst_r (const char *key
, size_t keylen
, request_type type
,
106 struct hostent
*resultbuf
, char *buffer
, size_t buflen
,
107 struct hostent
**result
, int *h_errnop
)
109 if (__builtin_expect (__nss_have_localdomain
>= 0, 0))
111 if (__nss_have_localdomain
== 0)
112 __nss_have_localdomain
= getenv ("LOCALDOMAIN") != NULL
? 1 : -1;
113 if (__nss_have_localdomain
> 0)
115 __nss_not_use_nscd_hosts
= 1;
123 /* If the mapping is available, try to search there instead of
124 communicating with the nscd. */
125 struct mapped_database
*mapped
;
126 mapped
= __nscd_get_map_ref (GETFDHST
, "hosts", &__hst_map_handle
,
130 const char *h_name
= NULL
;
131 const uint32_t *aliases_len
= NULL
;
132 const char *addr_list
= NULL
;
133 size_t addr_list_len
= 0;
135 const char *recend
= (const char *) ~UINTMAX_C (0);
137 hst_response_header hst_resp
;
138 if (mapped
!= NO_MAPPING
)
140 /* No const qualifier, as it can change during garbage collection. */
141 struct datahead
*found
= __nscd_cache_search (type
, key
, keylen
, mapped
,
145 h_name
= (char *) (&found
->data
[0].hstdata
+ 1);
146 hst_resp
= found
->data
[0].hstdata
;
147 aliases_len
= (uint32_t *) (h_name
+ hst_resp
.h_name_len
);
148 addr_list
= ((char *) aliases_len
149 + hst_resp
.h_aliases_cnt
* sizeof (uint32_t));
150 addr_list_len
= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
151 recend
= (const char *) found
->data
+ found
->recsize
;
152 /* Now check if we can trust hst_resp fields. If GC is
153 in progress, it can contain anything. */
154 if (mapped
->head
->gc_cycle
!= gc_cycle
)
160 #ifndef _STRING_ARCH_unaligned
161 /* The aliases_len array in the mapped database might very
162 well be unaligned. We will access it word-wise so on
163 platforms which do not tolerate unaligned accesses we
164 need to make an aligned copy. */
165 if (((uintptr_t) aliases_len
& (__alignof__ (*aliases_len
) - 1))
168 uint32_t *tmp
= alloca (hst_resp
.h_aliases_cnt
169 * sizeof (uint32_t));
170 aliases_len
= memcpy (tmp
, aliases_len
,
171 hst_resp
.h_aliases_cnt
172 * sizeof (uint32_t));
175 if (type
!= GETHOSTBYADDR
&& type
!= GETHOSTBYNAME
)
177 if (hst_resp
.h_length
== INADDRSZ
)
178 addr_list
+= addr_list_len
;
179 addr_list_len
= hst_resp
.h_addr_list_cnt
* IN6ADDRSZ
;
181 if (__builtin_expect ((const char *) addr_list
+ addr_list_len
189 sock
= __nscd_open_socket (key
, keylen
, type
, &hst_resp
,
193 __nss_not_use_nscd_hosts
= 1;
198 /* No value found so far. */
201 if (__builtin_expect (hst_resp
.found
== -1, 0))
203 /* The daemon does not cache this database. */
204 __nss_not_use_nscd_hosts
= 1;
208 if (hst_resp
.found
== 1)
218 /* A first check whether the buffer is sufficiently large is possible. */
219 /* Now allocate the buffer the array for the group members. We must
220 align the pointer and the base of the h_addr_list pointers. */
221 align1
= ((__alignof__ (char *) - (cp
- ((char *) 0)))
222 & (__alignof__ (char *) - 1));
223 align2
= ((__alignof__ (char *) - ((cp
+ align1
+ hst_resp
.h_name_len
)
225 & (__alignof__ (char *) - 1));
226 if (buflen
< (align1
+ hst_resp
.h_name_len
+ align2
227 + ((hst_resp
.h_aliases_cnt
+ hst_resp
.h_addr_list_cnt
230 + hst_resp
.h_addr_list_cnt
* (type
== AF_INET
231 ? INADDRSZ
: IN6ADDRSZ
)))
234 *h_errnop
= NETDB_INTERNAL
;
235 __set_errno (ERANGE
);
241 /* Prepare the result as far as we can. */
242 resultbuf
->h_aliases
= (char **) cp
;
243 cp
+= (hst_resp
.h_aliases_cnt
+ 1) * sizeof (char *);
244 resultbuf
->h_addr_list
= (char **) cp
;
245 cp
+= (hst_resp
.h_addr_list_cnt
+ 1) * sizeof (char *);
247 resultbuf
->h_name
= cp
;
248 cp
+= hst_resp
.h_name_len
+ align2
;
250 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYNAME
)
252 resultbuf
->h_addrtype
= AF_INET
;
253 resultbuf
->h_length
= INADDRSZ
;
257 resultbuf
->h_addrtype
= AF_INET6
;
258 resultbuf
->h_length
= IN6ADDRSZ
;
260 for (cnt
= 0; cnt
< hst_resp
.h_addr_list_cnt
; ++cnt
)
262 resultbuf
->h_addr_list
[cnt
] = cp
;
263 cp
+= resultbuf
->h_length
;
265 resultbuf
->h_addr_list
[cnt
] = NULL
;
271 vec
[0].iov_base
= resultbuf
->h_name
;
272 vec
[0].iov_len
= hst_resp
.h_name_len
;
273 total_len
= hst_resp
.h_name_len
;
276 if (hst_resp
.h_aliases_cnt
> 0)
278 aliases_len
= alloca (hst_resp
.h_aliases_cnt
279 * sizeof (uint32_t));
280 vec
[n
].iov_base
= (void *) aliases_len
;
281 vec
[n
].iov_len
= hst_resp
.h_aliases_cnt
* sizeof (uint32_t);
283 total_len
+= hst_resp
.h_aliases_cnt
* sizeof (uint32_t);
287 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYNAME
)
289 vec
[n
].iov_base
= resultbuf
->h_addr_list
[0];
290 vec
[n
].iov_len
= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
292 total_len
+= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
298 if (hst_resp
.h_length
== INADDRSZ
)
300 ignore
= alloca (hst_resp
.h_addr_list_cnt
* INADDRSZ
);
301 vec
[n
].iov_base
= ignore
;
302 vec
[n
].iov_len
= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
304 total_len
+= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
309 vec
[n
].iov_base
= resultbuf
->h_addr_list
[0];
310 vec
[n
].iov_len
= hst_resp
.h_addr_list_cnt
* IN6ADDRSZ
;
312 total_len
+= hst_resp
.h_addr_list_cnt
* IN6ADDRSZ
;
317 if ((size_t) __readvall (sock
, vec
, n
) != total_len
)
322 memcpy (resultbuf
->h_name
, h_name
, hst_resp
.h_name_len
);
323 memcpy (resultbuf
->h_addr_list
[0], addr_list
, addr_list_len
);
326 /* Now we also can read the aliases. */
328 for (cnt
= 0; cnt
< hst_resp
.h_aliases_cnt
; ++cnt
)
330 resultbuf
->h_aliases
[cnt
] = cp
;
331 cp
+= aliases_len
[cnt
];
332 total_len
+= aliases_len
[cnt
];
334 resultbuf
->h_aliases
[cnt
] = NULL
;
336 if (__builtin_expect ((const char *) addr_list
+ addr_list_len
337 + total_len
> recend
, 0))
339 /* aliases_len array might contain garbage during nscd GC cycle,
340 retry rather than fail in that case. */
341 if (addr_list
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
345 /* See whether this would exceed the buffer capacity. */
346 if (__builtin_expect (cp
> buffer
+ buflen
, 0))
348 /* aliases_len array might contain garbage during nscd GC cycle,
349 retry rather than fail in that case. */
350 if (addr_list
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
358 /* And finally read the aliases. */
359 if (addr_list
== NULL
)
362 || ((size_t) __readall (sock
, resultbuf
->h_aliases
[0], total_len
)
371 memcpy (resultbuf
->h_aliases
[0],
372 (const char *) addr_list
+ addr_list_len
, total_len
);
374 /* Try to detect corrupt databases. */
375 if (resultbuf
->h_name
[hst_resp
.h_name_len
- 1] != '\0'
376 || ({for (cnt
= 0; cnt
< hst_resp
.h_aliases_cnt
; ++cnt
)
377 if (resultbuf
->h_aliases
[cnt
][aliases_len
[cnt
] - 1]
380 cnt
< hst_resp
.h_aliases_cnt
; }))
382 /* We cannot use the database. */
383 if (mapped
->head
->gc_cycle
!= gc_cycle
)
394 /* Store the error number. */
395 *h_errnop
= hst_resp
.error
;
397 /* Set errno to 0 to indicate no error, just no found record. */
399 /* Even though we have not found anything, the result is zero. */
405 close_not_cancel_no_status (sock
);
407 if (__nscd_drop_map_ref (mapped
, &gc_cycle
) != 0)
409 /* When we come here this means there has been a GC cycle while we
410 were looking for the data. This means the data might have been
411 inconsistent. Retry if possible. */
412 if ((gc_cycle
& 1) != 0 || ++nretries
== 5 || retval
== -1)
414 /* nscd is just running gc now. Disable using the mapping. */
415 if (atomic_decrement_val (&mapped
->counter
) == 0)
416 __nscd_unmap (mapped
);