]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 1996-2014 Free Software Foundation, Inc. |
84384f5b UD |
2 | This file is part of the GNU C Library. |
3 | Extended from original form by Ulrich Drepper <drepper@cygnus.com>, 1996. | |
5f0e6fc7 | 4 | |
84384f5b | 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. | |
5f0e6fc7 | 9 | |
84384f5b UD |
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. |
5f0e6fc7 | 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/>. */ | |
5f0e6fc7 | 18 | |
df21c858 | 19 | /* Parts of this file are plain copies of the file `gethtnamadr.c' from |
5f0e6fc7 RM |
20 | the bind package and it has the following copyright. */ |
21 | ||
5f0e6fc7 | 22 | /* |
df21c858 UD |
23 | * ++Copyright++ 1985, 1988, 1993 |
24 | * - | |
25 | * Copyright (c) 1985, 1988, 1993 | |
26 | * The Regents of the University of California. All rights reserved. | |
5f0e6fc7 RM |
27 | * |
28 | * Redistribution and use in source and binary forms, with or without | |
29 | * modification, are permitted provided that the following conditions | |
30 | * are met: | |
31 | * 1. Redistributions of source code must retain the above copyright | |
32 | * notice, this list of conditions and the following disclaimer. | |
33 | * 2. Redistributions in binary form must reproduce the above copyright | |
34 | * notice, this list of conditions and the following disclaimer in the | |
35 | * documentation and/or other materials provided with the distribution. | |
5f0e6fc7 RM |
36 | * 4. Neither the name of the University nor the names of its contributors |
37 | * may be used to endorse or promote products derived from this software | |
38 | * without specific prior written permission. | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
df21c858 UD |
51 | * - |
52 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | |
53 | * | |
54 | * Permission to use, copy, modify, and distribute this software for any | |
55 | * purpose with or without fee is hereby granted, provided that the above | |
56 | * copyright notice and this permission notice appear in all copies, and that | |
57 | * the name of Digital Equipment Corporation not be used in advertising or | |
58 | * publicity pertaining to distribution of the document or software without | |
59 | * specific, written prior permission. | |
60 | * | |
61 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | |
62 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | |
63 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | |
64 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
65 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
66 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
67 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
68 | * SOFTWARE. | |
69 | * - | |
70 | * --Copyright-- | |
5f0e6fc7 RM |
71 | */ |
72 | ||
1eb946b9 | 73 | #include <assert.h> |
5f0e6fc7 RM |
74 | #include <ctype.h> |
75 | #include <errno.h> | |
76 | #include <netdb.h> | |
77 | #include <stdio.h> | |
78 | #include <stdlib.h> | |
79 | #include <stddef.h> | |
80 | #include <string.h> | |
81 | #include <sys/syslog.h> | |
82 | ||
5f0e6fc7 RM |
83 | #include "nsswitch.h" |
84 | ||
85 | /* Get implementation for some internal functions. */ | |
da2d1bc5 UD |
86 | #include <resolv/mapv4v6addr.h> |
87 | #include <resolv/mapv4v6hostent.h> | |
5f0e6fc7 | 88 | |
5db91571 UD |
89 | #define RESOLVSORT |
90 | ||
e2dced82 | 91 | #if PACKETSZ > 65536 |
5f0e6fc7 RM |
92 | # define MAXPACKET PACKETSZ |
93 | #else | |
e2dced82 | 94 | # define MAXPACKET 65536 |
5f0e6fc7 | 95 | #endif |
1522c368 UD |
96 | /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */ |
97 | #ifdef MAXHOSTNAMELEN | |
98 | # undef MAXHOSTNAMELEN | |
99 | #endif | |
100 | #define MAXHOSTNAMELEN 256 | |
5f0e6fc7 RM |
101 | |
102 | static const char AskedForGot[] = "\ | |
103 | gethostby*.getanswer: asked for \"%s\", got \"%s\""; | |
104 | ||
105 | ||
106 | /* We need this time later. */ | |
107 | typedef union querybuf | |
108 | { | |
109 | HEADER hdr; | |
110 | u_char buf[MAXPACKET]; | |
111 | } querybuf; | |
112 | ||
8d8c6efa UD |
113 | /* These functions are defined in res_comp.c. */ |
114 | #define NS_MAXCDNAME 255 /* maximum compressed domain name */ | |
b455972f UD |
115 | extern int __ns_name_ntop (const u_char *, char *, size_t); |
116 | extern int __ns_name_unpack (const u_char *, const u_char *, | |
117 | const u_char *, u_char *, size_t); | |
8d8c6efa | 118 | |
5f0e6fc7 RM |
119 | |
120 | static enum nss_status getanswer_r (const querybuf *answer, int anslen, | |
121 | const char *qname, int qtype, | |
df4ef2ab | 122 | struct hostent *result, char *buffer, |
b455972f | 123 | size_t buflen, int *errnop, int *h_errnop, |
d1fe1f22 | 124 | int map, int32_t *ttlp, char **canonp); |
5f0e6fc7 | 125 | |
1eb946b9 UD |
126 | static enum nss_status gaih_getanswer (const querybuf *answer1, int anslen1, |
127 | const querybuf *answer2, int anslen2, | |
128 | const char *qname, | |
129 | struct gaih_addrtuple **pat, | |
130 | char *buffer, size_t buflen, | |
131 | int *errnop, int *h_errnop, | |
132 | int32_t *ttlp); | |
133 | ||
78d8d211 UD |
134 | extern enum nss_status _nss_dns_gethostbyname3_r (const char *name, int af, |
135 | struct hostent *result, | |
136 | char *buffer, size_t buflen, | |
137 | int *errnop, int *h_errnop, | |
138 | int32_t *ttlp, | |
139 | char **canonp); | |
140 | hidden_proto (_nss_dns_gethostbyname3_r) | |
141 | ||
5f0e6fc7 | 142 | enum nss_status |
d1fe1f22 | 143 | _nss_dns_gethostbyname3_r (const char *name, int af, struct hostent *result, |
67479a70 | 144 | char *buffer, size_t buflen, int *errnop, |
d1fe1f22 | 145 | int *h_errnop, int32_t *ttlp, char **canonp) |
5f0e6fc7 | 146 | { |
16b66062 AJ |
147 | union |
148 | { | |
149 | querybuf *buf; | |
150 | u_char *ptr; | |
151 | } host_buffer; | |
152 | querybuf *orig_host_buffer; | |
f80f1a4a | 153 | char tmp[NS_MAXDNAME]; |
5f0e6fc7 RM |
154 | int size, type, n; |
155 | const char *cp; | |
0420d888 | 156 | int map = 0; |
34816665 | 157 | int olderr = errno; |
6166815d | 158 | enum nss_status status; |
5f0e6fc7 | 159 | |
87bb6b6c | 160 | if (__res_maybe_init (&_res, 0) == -1) |
b43b13ac UD |
161 | return NSS_STATUS_UNAVAIL; |
162 | ||
5f0e6fc7 RM |
163 | switch (af) { |
164 | case AF_INET: | |
165 | size = INADDRSZ; | |
166 | type = T_A; | |
167 | break; | |
168 | case AF_INET6: | |
169 | size = IN6ADDRSZ; | |
170 | type = T_AAAA; | |
171 | break; | |
172 | default: | |
0cc70fcf | 173 | *h_errnop = NO_DATA; |
67479a70 | 174 | *errnop = EAFNOSUPPORT; |
5f0e6fc7 RM |
175 | return NSS_STATUS_UNAVAIL; |
176 | } | |
177 | ||
178 | result->h_addrtype = af; | |
179 | result->h_length = size; | |
180 | ||
181 | /* | |
182 | * if there aren't any dots, it could be a user-level alias. | |
183 | * this is also done in res_query() since we are not the only | |
184 | * function that looks up host names. | |
185 | */ | |
f80f1a4a UD |
186 | if (strchr (name, '.') == NULL |
187 | && (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL) | |
5f0e6fc7 RM |
188 | name = cp; |
189 | ||
16b66062 | 190 | host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024); |
6166815d | 191 | |
16b66062 | 192 | n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer.buf->buf, |
b7da31a1 | 193 | 1024, &host_buffer.ptr, NULL, NULL, NULL); |
5f0e6fc7 | 194 | if (n < 0) |
dd7d45e8 | 195 | { |
4535680d | 196 | switch (errno) |
da5ac135 | 197 | { |
4535680d | 198 | case ESRCH: |
da5ac135 UD |
199 | status = NSS_STATUS_TRYAGAIN; |
200 | h_errno = TRY_AGAIN; | |
4535680d | 201 | break; |
cfde9b46 SP |
202 | /* System has run out of file descriptors. */ |
203 | case EMFILE: | |
204 | case ENFILE: | |
205 | h_errno = NETDB_INTERNAL; | |
206 | /* Fall through. */ | |
4535680d UD |
207 | case ECONNREFUSED: |
208 | case ETIMEDOUT: | |
209 | status = NSS_STATUS_UNAVAIL; | |
210 | break; | |
211 | default: | |
212 | status = NSS_STATUS_NOTFOUND; | |
213 | break; | |
da5ac135 | 214 | } |
dd7d45e8 | 215 | *h_errnop = h_errno; |
34816665 UD |
216 | if (h_errno == TRY_AGAIN) |
217 | *errnop = EAGAIN; | |
218 | else | |
219 | __set_errno (olderr); | |
b455972f | 220 | |
4535680d | 221 | /* If we are looking for an IPv6 address and mapping is enabled |
b455972f UD |
222 | by having the RES_USE_INET6 bit in _res.options set, we try |
223 | another lookup. */ | |
224 | if (af == AF_INET6 && (_res.options & RES_USE_INET6)) | |
16b66062 AJ |
225 | n = __libc_res_nsearch (&_res, name, C_IN, T_A, host_buffer.buf->buf, |
226 | host_buffer.buf != orig_host_buffer | |
1eb946b9 | 227 | ? MAXPACKET : 1024, &host_buffer.ptr, |
b7da31a1 | 228 | NULL, NULL, NULL); |
b455972f UD |
229 | |
230 | if (n < 0) | |
6166815d | 231 | { |
16b66062 AJ |
232 | if (host_buffer.buf != orig_host_buffer) |
233 | free (host_buffer.buf); | |
6166815d UD |
234 | return status; |
235 | } | |
b455972f UD |
236 | |
237 | map = 1; | |
238 | ||
239 | result->h_addrtype = AF_INET; | |
2884dad4 | 240 | result->h_length = INADDRSZ; |
dd7d45e8 | 241 | } |
5f0e6fc7 | 242 | |
16b66062 | 243 | status = getanswer_r (host_buffer.buf, n, name, type, result, buffer, buflen, |
d1fe1f22 | 244 | errnop, h_errnop, map, ttlp, canonp); |
16b66062 AJ |
245 | if (host_buffer.buf != orig_host_buffer) |
246 | free (host_buffer.buf); | |
6166815d | 247 | return status; |
5f0e6fc7 | 248 | } |
78d8d211 | 249 | hidden_def (_nss_dns_gethostbyname3_r) |
5f0e6fc7 RM |
250 | |
251 | ||
d1fe1f22 UD |
252 | enum nss_status |
253 | _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result, | |
254 | char *buffer, size_t buflen, int *errnop, | |
255 | int *h_errnop) | |
256 | { | |
257 | return _nss_dns_gethostbyname3_r (name, af, result, buffer, buflen, errnop, | |
258 | h_errnop, NULL, NULL); | |
259 | } | |
260 | ||
261 | ||
5f0e6fc7 RM |
262 | enum nss_status |
263 | _nss_dns_gethostbyname_r (const char *name, struct hostent *result, | |
67479a70 UD |
264 | char *buffer, size_t buflen, int *errnop, |
265 | int *h_errnop) | |
5f0e6fc7 RM |
266 | { |
267 | enum nss_status status = NSS_STATUS_NOTFOUND; | |
268 | ||
269 | if (_res.options & RES_USE_INET6) | |
78d8d211 UD |
270 | status = _nss_dns_gethostbyname3_r (name, AF_INET6, result, buffer, |
271 | buflen, errnop, h_errnop, NULL, NULL); | |
5f0e6fc7 | 272 | if (status == NSS_STATUS_NOTFOUND) |
78d8d211 UD |
273 | status = _nss_dns_gethostbyname3_r (name, AF_INET, result, buffer, |
274 | buflen, errnop, h_errnop, NULL, NULL); | |
5f0e6fc7 RM |
275 | |
276 | return status; | |
277 | } | |
278 | ||
279 | ||
1eb946b9 UD |
280 | enum nss_status |
281 | _nss_dns_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat, | |
282 | char *buffer, size_t buflen, int *errnop, | |
283 | int *herrnop, int32_t *ttlp) | |
284 | { | |
285 | if (__res_maybe_init (&_res, 0) == -1) | |
286 | return NSS_STATUS_UNAVAIL; | |
287 | ||
1eb946b9 UD |
288 | /* |
289 | * if there aren't any dots, it could be a user-level alias. | |
290 | * this is also done in res_query() since we are not the only | |
291 | * function that looks up host names. | |
292 | */ | |
293 | if (strchr (name, '.') == NULL) | |
294 | { | |
b7da31a1 UD |
295 | char *tmp = alloca (NS_MAXDNAME); |
296 | const char *cp = res_hostalias (&_res, name, tmp, NS_MAXDNAME); | |
1eb946b9 UD |
297 | if (cp != NULL) |
298 | name = cp; | |
299 | } | |
300 | ||
301 | union | |
302 | { | |
303 | querybuf *buf; | |
304 | u_char *ptr; | |
305 | } host_buffer; | |
306 | querybuf *orig_host_buffer; | |
307 | host_buffer.buf = orig_host_buffer = (querybuf *) alloca (2048); | |
308 | u_char *ans2p = NULL; | |
309 | int nans2p = 0; | |
b7da31a1 | 310 | int resplen2 = 0; |
1eb946b9 UD |
311 | |
312 | int olderr = errno; | |
313 | enum nss_status status; | |
314 | int n = __libc_res_nsearch (&_res, name, C_IN, T_UNSPEC, | |
315 | host_buffer.buf->buf, 2048, &host_buffer.ptr, | |
b7da31a1 | 316 | &ans2p, &nans2p, &resplen2); |
1eb946b9 UD |
317 | if (n < 0) |
318 | { | |
cfde9b46 | 319 | switch (errno) |
da5ac135 | 320 | { |
cfde9b46 | 321 | case ESRCH: |
da5ac135 UD |
322 | status = NSS_STATUS_TRYAGAIN; |
323 | h_errno = TRY_AGAIN; | |
cfde9b46 SP |
324 | break; |
325 | /* System has run out of file descriptors. */ | |
326 | case EMFILE: | |
327 | case ENFILE: | |
328 | h_errno = NETDB_INTERNAL; | |
329 | /* Fall through. */ | |
330 | case ECONNREFUSED: | |
331 | case ETIMEDOUT: | |
332 | status = NSS_STATUS_UNAVAIL; | |
333 | break; | |
334 | default: | |
335 | status = NSS_STATUS_NOTFOUND; | |
336 | break; | |
da5ac135 | 337 | } |
cfde9b46 | 338 | |
1eb946b9 UD |
339 | *herrnop = h_errno; |
340 | if (h_errno == TRY_AGAIN) | |
341 | *errnop = EAGAIN; | |
342 | else | |
343 | __set_errno (olderr); | |
344 | ||
345 | if (host_buffer.buf != orig_host_buffer) | |
346 | free (host_buffer.buf); | |
347 | ||
348 | return status; | |
349 | } | |
350 | ||
351 | status = gaih_getanswer(host_buffer.buf, n, (const querybuf *) ans2p, | |
b7da31a1 | 352 | resplen2, name, pat, buffer, buflen, |
1eb946b9 UD |
353 | errnop, herrnop, ttlp); |
354 | ||
355 | if (host_buffer.buf != orig_host_buffer) | |
356 | free (host_buffer.buf); | |
357 | ||
358 | return status; | |
359 | } | |
360 | ||
361 | ||
31e2791c UD |
362 | extern enum nss_status _nss_dns_gethostbyaddr2_r (const void *addr, |
363 | socklen_t len, int af, | |
364 | struct hostent *result, | |
365 | char *buffer, size_t buflen, | |
366 | int *errnop, int *h_errnop, | |
367 | int32_t *ttlp); | |
368 | hidden_proto (_nss_dns_gethostbyaddr2_r) | |
369 | ||
5f0e6fc7 | 370 | enum nss_status |
31e2791c UD |
371 | _nss_dns_gethostbyaddr2_r (const void *addr, socklen_t len, int af, |
372 | struct hostent *result, char *buffer, size_t buflen, | |
373 | int *errnop, int *h_errnop, int32_t *ttlp) | |
5f0e6fc7 RM |
374 | { |
375 | static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; | |
376 | static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; | |
20cc4c87 | 377 | static const u_char v6local[] = { 0,0, 0,1 }; |
5f0e6fc7 RM |
378 | const u_char *uaddr = (const u_char *)addr; |
379 | struct host_data | |
380 | { | |
381 | char *aliases[MAX_NR_ALIASES]; | |
382 | unsigned char host_addr[16]; /* IPv4 or IPv6 */ | |
383 | char *h_addr_ptrs[MAX_NR_ADDRS + 1]; | |
384 | char linebuffer[0]; | |
385 | } *host_data = (struct host_data *) buffer; | |
16b66062 AJ |
386 | union |
387 | { | |
388 | querybuf *buf; | |
389 | u_char *ptr; | |
390 | } host_buffer; | |
391 | querybuf *orig_host_buffer; | |
1f3f143e | 392 | char qbuf[MAXDNAME+1], *qp = NULL; |
30f22ab1 | 393 | size_t size; |
0420d888 | 394 | int n, status; |
34816665 | 395 | int olderr = errno; |
5f0e6fc7 | 396 | |
df9293cb UD |
397 | uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct host_data); |
398 | buffer += pad; | |
399 | buflen = buflen > pad ? buflen - pad : 0; | |
400 | ||
a1ffb40e | 401 | if (__glibc_unlikely (buflen < sizeof (struct host_data))) |
df9293cb UD |
402 | { |
403 | *errnop = ERANGE; | |
404 | *h_errnop = NETDB_INTERNAL; | |
405 | return NSS_STATUS_TRYAGAIN; | |
406 | } | |
407 | ||
408 | host_data = (struct host_data *) buffer; | |
409 | ||
87bb6b6c | 410 | if (__res_maybe_init (&_res, 0) == -1) |
b43b13ac UD |
411 | return NSS_STATUS_UNAVAIL; |
412 | ||
20cc4c87 UD |
413 | if (af == AF_INET6 && len == IN6ADDRSZ |
414 | && (memcmp (uaddr, mapped, sizeof mapped) == 0 | |
415 | || (memcmp (uaddr, tunnelled, sizeof tunnelled) == 0 | |
416 | && memcmp (&uaddr[sizeof tunnelled], v6local, sizeof v6local)))) | |
5f0e6fc7 RM |
417 | { |
418 | /* Unmap. */ | |
419 | addr += sizeof mapped; | |
420 | uaddr += sizeof mapped; | |
421 | af = AF_INET; | |
422 | len = INADDRSZ; | |
423 | } | |
424 | ||
425 | switch (af) | |
426 | { | |
427 | case AF_INET: | |
428 | size = INADDRSZ; | |
429 | break; | |
430 | case AF_INET6: | |
431 | size = IN6ADDRSZ; | |
432 | break; | |
433 | default: | |
67479a70 | 434 | *errnop = EAFNOSUPPORT; |
5f0e6fc7 RM |
435 | *h_errnop = NETDB_INTERNAL; |
436 | return NSS_STATUS_UNAVAIL; | |
437 | } | |
20cc4c87 | 438 | if (size > len) |
5f0e6fc7 | 439 | { |
67479a70 | 440 | *errnop = EAFNOSUPPORT; |
5f0e6fc7 RM |
441 | *h_errnop = NETDB_INTERNAL; |
442 | return NSS_STATUS_UNAVAIL; | |
443 | } | |
444 | ||
ee778b56 UD |
445 | host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024); |
446 | ||
5f0e6fc7 RM |
447 | switch (af) |
448 | { | |
449 | case AF_INET: | |
450 | sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff), | |
451 | (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff)); | |
452 | break; | |
453 | case AF_INET6: | |
ee778b56 | 454 | /* Only lookup with the byte string format if the user wants it. */ |
a1ffb40e | 455 | if (__glibc_unlikely (_res.options & RES_USEBSTRING)) |
ee778b56 UD |
456 | { |
457 | qp = stpcpy (qbuf, "\\[x"); | |
458 | for (n = 0; n < IN6ADDRSZ; ++n) | |
459 | qp += sprintf (qp, "%02hhx", uaddr[n]); | |
460 | strcpy (qp, "].ip6.arpa"); | |
461 | n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, | |
1eb946b9 | 462 | host_buffer.buf->buf, 1024, &host_buffer.ptr, |
b7da31a1 | 463 | NULL, NULL, NULL); |
ee778b56 UD |
464 | if (n >= 0) |
465 | goto got_it_already; | |
466 | } | |
5f0e6fc7 | 467 | qp = qbuf; |
ee778b56 UD |
468 | for (n = IN6ADDRSZ - 1; n >= 0; n--) |
469 | { | |
470 | static const char nibblechar[16] = "0123456789abcdef"; | |
471 | *qp++ = nibblechar[uaddr[n] & 0xf]; | |
472 | *qp++ = '.'; | |
473 | *qp++ = nibblechar[(uaddr[n] >> 4) & 0xf]; | |
474 | *qp++ = '.'; | |
475 | } | |
476 | strcpy(qp, "ip6.arpa"); | |
5f0e6fc7 RM |
477 | break; |
478 | default: | |
479 | /* Cannot happen. */ | |
9fea9ed6 | 480 | break; |
5f0e6fc7 RM |
481 | } |
482 | ||
16b66062 | 483 | n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf, |
b7da31a1 | 484 | 1024, &host_buffer.ptr, NULL, NULL, NULL); |
340ef046 | 485 | if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0) |
62a08e44 UD |
486 | { |
487 | strcpy (qp, "ip6.int"); | |
16b66062 AJ |
488 | n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf, |
489 | host_buffer.buf != orig_host_buffer | |
1eb946b9 | 490 | ? MAXPACKET : 1024, &host_buffer.ptr, |
b7da31a1 | 491 | NULL, NULL, NULL); |
62a08e44 | 492 | } |
5f0e6fc7 | 493 | if (n < 0) |
dd7d45e8 UD |
494 | { |
495 | *h_errnop = h_errno; | |
34816665 | 496 | __set_errno (olderr); |
16b66062 AJ |
497 | if (host_buffer.buf != orig_host_buffer) |
498 | free (host_buffer.buf); | |
dd7d45e8 UD |
499 | return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND; |
500 | } | |
5f0e6fc7 | 501 | |
ee778b56 | 502 | got_it_already: |
16b66062 | 503 | status = getanswer_r (host_buffer.buf, n, qbuf, T_PTR, result, buffer, buflen, |
31e2791c | 504 | errnop, h_errnop, 0 /* XXX */, ttlp, NULL); |
16b66062 AJ |
505 | if (host_buffer.buf != orig_host_buffer) |
506 | free (host_buffer.buf); | |
5f0e6fc7 | 507 | if (status != NSS_STATUS_SUCCESS) |
65b4743a | 508 | return status; |
5f0e6fc7 RM |
509 | |
510 | #ifdef SUNSECURITY | |
511 | This is not implemented because it is not possible to use the current | |
512 | source from bind in a multi-threaded program. | |
513 | #endif | |
514 | ||
515 | result->h_addrtype = af; | |
516 | result->h_length = len; | |
63551311 | 517 | memcpy (host_data->host_addr, addr, len); |
5f0e6fc7 RM |
518 | host_data->h_addr_ptrs[0] = (char *) host_data->host_addr; |
519 | host_data->h_addr_ptrs[1] = NULL; | |
b455972f UD |
520 | #if 0 |
521 | /* XXX I think this is wrong. Why should an IPv4 address be | |
522 | converted to IPv6 if the user explicitly asked for IPv4? */ | |
5f0e6fc7 RM |
523 | if (af == AF_INET && (_res.options & RES_USE_INET6)) |
524 | { | |
525 | map_v4v6_address ((char *) host_data->host_addr, | |
526 | (char *) host_data->host_addr); | |
527 | result->h_addrtype = AF_INET6; | |
528 | result->h_length = IN6ADDRSZ; | |
529 | } | |
b455972f | 530 | #endif |
5f0e6fc7 RM |
531 | *h_errnop = NETDB_SUCCESS; |
532 | return NSS_STATUS_SUCCESS; | |
533 | } | |
31e2791c UD |
534 | hidden_def (_nss_dns_gethostbyaddr2_r) |
535 | ||
536 | ||
537 | enum nss_status | |
538 | _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af, | |
539 | struct hostent *result, char *buffer, size_t buflen, | |
540 | int *errnop, int *h_errnop) | |
541 | { | |
542 | return _nss_dns_gethostbyaddr2_r (addr, len, af, result, buffer, buflen, | |
543 | errnop, h_errnop, NULL); | |
544 | } | |
5f0e6fc7 | 545 | |
5db91571 UD |
546 | #ifdef RESOLVSORT |
547 | static void addrsort (char **ap, int num); | |
548 | ||
549 | static void | |
550 | addrsort (char **ap, int num) | |
551 | { | |
552 | int i, j; | |
553 | char **p; | |
554 | short aval[MAX_NR_ADDRS]; | |
555 | int needsort = 0; | |
556 | ||
557 | p = ap; | |
558 | if (num > MAX_NR_ADDRS) | |
559 | num = MAX_NR_ADDRS; | |
560 | for (i = 0; i < num; i++, p++) | |
561 | { | |
562 | for (j = 0 ; (unsigned)j < _res.nsort; j++) | |
563 | if (_res.sort_list[j].addr.s_addr == | |
564 | (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask)) | |
565 | break; | |
566 | aval[i] = j; | |
567 | if (needsort == 0 && i > 0 && j < aval[i-1]) | |
568 | needsort = i; | |
569 | } | |
570 | if (!needsort) | |
571 | return; | |
572 | ||
573 | while (needsort++ < num) | |
574 | for (j = needsort - 2; j >= 0; j--) | |
575 | if (aval[j] > aval[j+1]) | |
576 | { | |
577 | char *hp; | |
578 | ||
579 | i = aval[j]; | |
580 | aval[j] = aval[j+1]; | |
581 | aval[j+1] = i; | |
582 | ||
583 | hp = ap[j]; | |
584 | ap[j] = ap[j+1]; | |
585 | ap[j+1] = hp; | |
586 | } | |
587 | else | |
588 | break; | |
589 | } | |
590 | #endif | |
5f0e6fc7 RM |
591 | |
592 | static enum nss_status | |
593 | getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype, | |
df4ef2ab | 594 | struct hostent *result, char *buffer, size_t buflen, |
d1fe1f22 | 595 | int *errnop, int *h_errnop, int map, int32_t *ttlp, char **canonp) |
5f0e6fc7 RM |
596 | { |
597 | struct host_data | |
598 | { | |
599 | char *aliases[MAX_NR_ALIASES]; | |
600 | unsigned char host_addr[16]; /* IPv4 or IPv6 */ | |
88a96b81 | 601 | char *h_addr_ptrs[0]; |
2f1687b9 UD |
602 | } *host_data; |
603 | int linebuflen; | |
2e09a79a | 604 | const HEADER *hp; |
5f0e6fc7 RM |
605 | const u_char *end_of_message, *cp; |
606 | int n, ancount, qdcount; | |
607 | int haveanswer, had_error; | |
608 | char *bp, **ap, **hap; | |
845dcb57 | 609 | char tbuf[MAXDNAME]; |
5f0e6fc7 | 610 | const char *tname; |
b455972f | 611 | int (*name_ok) (const char *); |
8d8c6efa | 612 | u_char packtmp[NS_MAXCDNAME]; |
b455972f | 613 | int have_to_map = 0; |
2f1687b9 UD |
614 | uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct host_data); |
615 | buffer += pad; | |
a1ffb40e | 616 | if (__glibc_unlikely (buflen < sizeof (struct host_data) + pad)) |
560d3b76 UD |
617 | { |
618 | /* The buffer is too small. */ | |
619 | too_small: | |
620 | *errnop = ERANGE; | |
621 | *h_errnop = NETDB_INTERNAL; | |
622 | return NSS_STATUS_TRYAGAIN; | |
623 | } | |
2f1687b9 UD |
624 | host_data = (struct host_data *) buffer; |
625 | linebuflen = buflen - sizeof (struct host_data); | |
626 | if (buflen - sizeof (struct host_data) != linebuflen) | |
627 | linebuflen = INT_MAX; | |
560d3b76 | 628 | |
5f0e6fc7 RM |
629 | tname = qname; |
630 | result->h_name = NULL; | |
631 | end_of_message = answer->buf + anslen; | |
632 | switch (qtype) | |
633 | { | |
634 | case T_A: | |
635 | case T_AAAA: | |
636 | name_ok = res_hnok; | |
637 | break; | |
638 | case T_PTR: | |
639 | name_ok = res_dnok; | |
640 | break; | |
641 | default: | |
a0bf6ac7 | 642 | *errnop = ENOENT; |
5f0e6fc7 RM |
643 | return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */ |
644 | } | |
645 | ||
646 | /* | |
647 | * find first satisfactory answer | |
648 | */ | |
649 | hp = &answer->hdr; | |
5f0e6fc7 RM |
650 | ancount = ntohs (hp->ancount); |
651 | qdcount = ntohs (hp->qdcount); | |
652 | cp = answer->buf + HFIXEDSZ; | |
b455972f | 653 | if (__builtin_expect (qdcount, 1) != 1) |
5f0e6fc7 RM |
654 | { |
655 | *h_errnop = NO_RECOVERY; | |
656 | return NSS_STATUS_UNAVAIL; | |
657 | } | |
88a96b81 UD |
658 | if (sizeof (struct host_data) + (ancount + 1) * sizeof (char *) >= buflen) |
659 | goto too_small; | |
660 | bp = (char *) &host_data->h_addr_ptrs[ancount + 1]; | |
661 | linebuflen -= (ancount + 1) * sizeof (char *); | |
5f0e6fc7 | 662 | |
8d8c6efa UD |
663 | n = __ns_name_unpack (answer->buf, end_of_message, cp, |
664 | packtmp, sizeof packtmp); | |
665 | if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) | |
5f0e6fc7 | 666 | { |
b455972f | 667 | if (__builtin_expect (errno, 0) == EMSGSIZE) |
560d3b76 | 668 | goto too_small; |
8d8c6efa UD |
669 | |
670 | n = -1; | |
671 | } | |
672 | ||
673 | if (n > 0 && bp[0] == '.') | |
674 | bp[0] = '\0'; | |
675 | ||
1eb946b9 UD |
676 | if (__builtin_expect (n < 0 || ((*name_ok) (bp) == 0 && (errno = EBADMSG)), |
677 | 0)) | |
8d8c6efa UD |
678 | { |
679 | *errnop = errno; | |
680 | *h_errnop = NO_RECOVERY; | |
5f0e6fc7 RM |
681 | return NSS_STATUS_UNAVAIL; |
682 | } | |
683 | cp += n + QFIXEDSZ; | |
684 | ||
685 | if (qtype == T_A || qtype == T_AAAA) | |
686 | { | |
687 | /* res_send() has already verified that the query name is the | |
688 | * same as the one we sent; this just gets the expanded name | |
689 | * (i.e., with the succeeding search-domain tacked on). | |
690 | */ | |
691 | n = strlen (bp) + 1; /* for the \0 */ | |
76b87c03 UD |
692 | if (n >= MAXHOSTNAMELEN) |
693 | { | |
67479a70 | 694 | *h_errnop = NO_RECOVERY; |
a0bf6ac7 | 695 | *errnop = ENOENT; |
76b87c03 UD |
696 | return NSS_STATUS_TRYAGAIN; |
697 | } | |
5f0e6fc7 RM |
698 | result->h_name = bp; |
699 | bp += n; | |
700 | linebuflen -= n; | |
560d3b76 UD |
701 | if (linebuflen < 0) |
702 | goto too_small; | |
5f0e6fc7 RM |
703 | /* The qname can be abbreviated, but h_name is now absolute. */ |
704 | qname = result->h_name; | |
705 | } | |
706 | ||
707 | ap = host_data->aliases; | |
708 | *ap = NULL; | |
709 | result->h_aliases = host_data->aliases; | |
710 | hap = host_data->h_addr_ptrs; | |
711 | *hap = NULL; | |
712 | result->h_addr_list = host_data->h_addr_ptrs; | |
713 | haveanswer = 0; | |
714 | had_error = 0; | |
715 | ||
716 | while (ancount-- > 0 && cp < end_of_message && had_error == 0) | |
717 | { | |
718 | int type, class; | |
719 | ||
8d8c6efa UD |
720 | n = __ns_name_unpack (answer->buf, end_of_message, cp, |
721 | packtmp, sizeof packtmp); | |
722 | if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) | |
723 | { | |
b455972f | 724 | if (__builtin_expect (errno, 0) == EMSGSIZE) |
0cc70fcf | 725 | goto too_small; |
8d8c6efa UD |
726 | |
727 | n = -1; | |
728 | } | |
729 | ||
a1ffb40e | 730 | if (__glibc_unlikely (n < 0 || (*name_ok) (bp) == 0)) |
5f0e6fc7 RM |
731 | { |
732 | ++had_error; | |
733 | continue; | |
734 | } | |
735 | cp += n; /* name */ | |
b7da31a1 | 736 | |
a1ffb40e | 737 | if (__glibc_unlikely (cp + 10 > end_of_message)) |
b7da31a1 UD |
738 | { |
739 | ++had_error; | |
740 | continue; | |
741 | } | |
742 | ||
cd5743fd | 743 | type = __ns_get16 (cp); |
5f0e6fc7 | 744 | cp += INT16SZ; /* type */ |
cd5743fd | 745 | class = __ns_get16 (cp); |
d1fe1f22 | 746 | cp += INT16SZ; /* class */ |
a7690819 | 747 | int32_t ttl = __ns_get32 (cp); |
d1fe1f22 | 748 | cp += INT32SZ; /* TTL */ |
cd5743fd | 749 | n = __ns_get16 (cp); |
5f0e6fc7 | 750 | cp += INT16SZ; /* len */ |
a1ffb40e | 751 | if (__glibc_unlikely (class != C_IN)) |
5f0e6fc7 RM |
752 | { |
753 | /* XXX - debug? syslog? */ | |
754 | cp += n; | |
755 | continue; /* XXX - had_error++ ? */ | |
756 | } | |
757 | ||
1eb946b9 | 758 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) |
5f0e6fc7 | 759 | { |
48c41d04 SP |
760 | /* A CNAME could also have a TTL entry. */ |
761 | if (ttlp != NULL && ttl < *ttlp) | |
762 | *ttlp = ttl; | |
763 | ||
5f0e6fc7 RM |
764 | if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1]) |
765 | continue; | |
766 | n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf); | |
a1ffb40e | 767 | if (__glibc_unlikely (n < 0 || (*name_ok) (tbuf) == 0)) |
5f0e6fc7 RM |
768 | { |
769 | ++had_error; | |
770 | continue; | |
771 | } | |
772 | cp += n; | |
773 | /* Store alias. */ | |
774 | *ap++ = bp; | |
775 | n = strlen (bp) + 1; /* For the \0. */ | |
b455972f | 776 | if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN) |
76b87c03 UD |
777 | { |
778 | ++had_error; | |
779 | continue; | |
780 | } | |
5f0e6fc7 RM |
781 | bp += n; |
782 | linebuflen -= n; | |
783 | /* Get canonical name. */ | |
784 | n = strlen (tbuf) + 1; /* For the \0. */ | |
a1ffb40e | 785 | if (__glibc_unlikely (n > linebuflen)) |
0cc70fcf | 786 | goto too_small; |
b455972f | 787 | if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN) |
5f0e6fc7 RM |
788 | { |
789 | ++had_error; | |
790 | continue; | |
791 | } | |
8d8c6efa UD |
792 | result->h_name = bp; |
793 | bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */ | |
5f0e6fc7 RM |
794 | linebuflen -= n; |
795 | continue; | |
796 | } | |
797 | ||
798 | if (qtype == T_PTR && type == T_CNAME) | |
799 | { | |
800 | n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf); | |
a1ffb40e | 801 | if (__glibc_unlikely (n < 0 || res_dnok (tbuf) == 0)) |
5f0e6fc7 RM |
802 | { |
803 | ++had_error; | |
804 | continue; | |
805 | } | |
806 | cp += n; | |
da2d1bc5 | 807 | /* Get canonical name. */ |
5f0e6fc7 | 808 | n = strlen (tbuf) + 1; /* For the \0. */ |
a1ffb40e | 809 | if (__glibc_unlikely (n > linebuflen)) |
0cc70fcf | 810 | goto too_small; |
b455972f | 811 | if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN) |
5f0e6fc7 RM |
812 | { |
813 | ++had_error; | |
814 | continue; | |
815 | } | |
8d8c6efa UD |
816 | tname = bp; |
817 | bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */ | |
5f0e6fc7 RM |
818 | linebuflen -= n; |
819 | continue; | |
820 | } | |
b455972f UD |
821 | if (__builtin_expect (type == T_SIG, 0) |
822 | || __builtin_expect (type == T_KEY, 0) | |
823 | || __builtin_expect (type == T_NXT, 0)) | |
1bc21e7a UD |
824 | { |
825 | /* We don't support DNSSEC yet. For now, ignore the record | |
826 | and send a low priority message to syslog. */ | |
827 | syslog (LOG_DEBUG | LOG_AUTH, | |
828 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", | |
829 | qname, p_class (C_IN), p_type(qtype), p_type (type)); | |
830 | cp += n; | |
831 | continue; | |
832 | } | |
b455972f UD |
833 | |
834 | if (type == T_A && qtype == T_AAAA && map) | |
835 | have_to_map = 1; | |
a1ffb40e | 836 | else if (__glibc_unlikely (type != qtype)) |
5f0e6fc7 RM |
837 | { |
838 | syslog (LOG_NOTICE | LOG_AUTH, | |
839 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", | |
840 | qname, p_class (C_IN), p_type (qtype), p_type (type)); | |
841 | cp += n; | |
842 | continue; /* XXX - had_error++ ? */ | |
843 | } | |
844 | ||
845 | switch (type) | |
846 | { | |
847 | case T_PTR: | |
a1ffb40e | 848 | if (__glibc_unlikely (strcasecmp (tname, bp) != 0)) |
5f0e6fc7 RM |
849 | { |
850 | syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp); | |
851 | cp += n; | |
852 | continue; /* XXX - had_error++ ? */ | |
853 | } | |
8d8c6efa UD |
854 | |
855 | n = __ns_name_unpack (answer->buf, end_of_message, cp, | |
856 | packtmp, sizeof packtmp); | |
857 | if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1) | |
858 | { | |
b455972f | 859 | if (__builtin_expect (errno, 0) == EMSGSIZE) |
0cc70fcf | 860 | goto too_small; |
8d8c6efa UD |
861 | |
862 | n = -1; | |
863 | } | |
864 | ||
a1ffb40e | 865 | if (__glibc_unlikely (n < 0 || res_hnok (bp) == 0)) |
5f0e6fc7 RM |
866 | { |
867 | ++had_error; | |
868 | break; | |
869 | } | |
870 | #if MULTI_PTRS_ARE_ALIASES | |
871 | cp += n; | |
872 | if (haveanswer == 0) | |
873 | result->h_name = bp; | |
874 | else if (ap < &host_data->aliases[MAXALIASES-1]) | |
875 | *ap++ = bp; | |
876 | else | |
877 | n = -1; | |
878 | if (n != -1) | |
879 | { | |
880 | n = strlen (bp) + 1; /* for the \0 */ | |
b455972f | 881 | if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN) |
76b87c03 UD |
882 | { |
883 | ++had_error; | |
884 | break; | |
885 | } | |
5f0e6fc7 RM |
886 | bp += n; |
887 | linebuflen -= n; | |
888 | } | |
889 | break; | |
890 | #else | |
891 | result->h_name = bp; | |
b455972f | 892 | if (have_to_map) |
5f0e6fc7 RM |
893 | { |
894 | n = strlen (bp) + 1; /* for the \0 */ | |
a1ffb40e | 895 | if (__glibc_unlikely (n >= MAXHOSTNAMELEN)) |
76b87c03 UD |
896 | { |
897 | ++had_error; | |
898 | break; | |
899 | } | |
5f0e6fc7 RM |
900 | bp += n; |
901 | linebuflen -= n; | |
51e4196f AS |
902 | if (map_v4v6_hostent (result, &bp, &linebuflen)) |
903 | goto too_small; | |
5f0e6fc7 RM |
904 | } |
905 | *h_errnop = NETDB_SUCCESS; | |
906 | return NSS_STATUS_SUCCESS; | |
907 | #endif | |
908 | case T_A: | |
909 | case T_AAAA: | |
b455972f | 910 | if (__builtin_expect (strcasecmp (result->h_name, bp), 0) != 0) |
5f0e6fc7 RM |
911 | { |
912 | syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp); | |
913 | cp += n; | |
914 | continue; /* XXX - had_error++ ? */ | |
915 | } | |
1f64ac13 | 916 | if (n != result->h_length) |
5f0e6fc7 | 917 | { |
1f64ac13 UD |
918 | cp += n; |
919 | continue; | |
5f0e6fc7 | 920 | } |
1f64ac13 | 921 | if (!haveanswer) |
5f0e6fc7 | 922 | { |
2e09a79a | 923 | int nn; |
5f0e6fc7 | 924 | |
48c41d04 SP |
925 | /* We compose a single hostent out of the entire chain of |
926 | entries, so the TTL of the hostent is essentially the lowest | |
927 | TTL in the chain. */ | |
928 | if (ttlp != NULL && ttl < *ttlp) | |
d1fe1f22 UD |
929 | *ttlp = ttl; |
930 | if (canonp != NULL) | |
931 | *canonp = bp; | |
5f0e6fc7 RM |
932 | result->h_name = bp; |
933 | nn = strlen (bp) + 1; /* for the \0 */ | |
934 | bp += nn; | |
935 | linebuflen -= nn; | |
936 | } | |
937 | ||
560d3b76 | 938 | linebuflen -= sizeof (align) - ((u_long) bp % sizeof (align)); |
5f0e6fc7 RM |
939 | bp += sizeof (align) - ((u_long) bp % sizeof (align)); |
940 | ||
a1ffb40e | 941 | if (__glibc_unlikely (n > linebuflen)) |
0cc70fcf | 942 | goto too_small; |
8d8c6efa | 943 | bp = __mempcpy (*hap++ = bp, cp, n); |
5f0e6fc7 RM |
944 | cp += n; |
945 | linebuflen -= n; | |
946 | break; | |
947 | default: | |
948 | abort (); | |
949 | } | |
950 | if (had_error == 0) | |
951 | ++haveanswer; | |
952 | } | |
953 | ||
954 | if (haveanswer > 0) | |
955 | { | |
956 | *ap = NULL; | |
957 | *hap = NULL; | |
da2d1bc5 | 958 | #if defined RESOLVSORT |
5f0e6fc7 RM |
959 | /* |
960 | * Note: we sort even if host can take only one address | |
961 | * in its return structures - should give it the "best" | |
962 | * address in that case, not some random one | |
963 | */ | |
964 | if (_res.nsort && haveanswer > 1 && qtype == T_A) | |
965 | addrsort (host_data->h_addr_ptrs, haveanswer); | |
966 | #endif /*RESOLVSORT*/ | |
967 | ||
968 | if (result->h_name == NULL) | |
969 | { | |
970 | n = strlen (qname) + 1; /* For the \0. */ | |
8d8c6efa | 971 | if (n > linebuflen) |
0cc70fcf | 972 | goto too_small; |
8d8c6efa | 973 | if (n >= MAXHOSTNAMELEN) |
76b87c03 | 974 | goto no_recovery; |
8d8c6efa UD |
975 | result->h_name = bp; |
976 | bp = __mempcpy (bp, qname, n); /* Cannot overflow. */ | |
5f0e6fc7 RM |
977 | linebuflen -= n; |
978 | } | |
979 | ||
b455972f | 980 | if (have_to_map) |
51e4196f AS |
981 | if (map_v4v6_hostent (result, &bp, &linebuflen)) |
982 | goto too_small; | |
5f0e6fc7 RM |
983 | *h_errnop = NETDB_SUCCESS; |
984 | return NSS_STATUS_SUCCESS; | |
985 | } | |
76b87c03 UD |
986 | no_recovery: |
987 | *h_errnop = NO_RECOVERY; | |
a0bf6ac7 | 988 | *errnop = ENOENT; |
2884dad4 UD |
989 | /* Special case here: if the resolver sent a result but it only |
990 | contains a CNAME while we are looking for a T_A or T_AAAA record, | |
991 | we fail with NOTFOUND instead of TRYAGAIN. */ | |
992 | return ((qtype == T_A || qtype == T_AAAA) && ap != host_data->aliases | |
993 | ? NSS_STATUS_NOTFOUND : NSS_STATUS_TRYAGAIN); | |
5f0e6fc7 | 994 | } |
1eb946b9 UD |
995 | |
996 | ||
997 | static enum nss_status | |
998 | gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname, | |
999 | struct gaih_addrtuple ***patp, | |
1000 | char **bufferp, size_t *buflenp, | |
1001 | int *errnop, int *h_errnop, int32_t *ttlp, int *firstp) | |
1002 | { | |
1003 | char *buffer = *bufferp; | |
1004 | size_t buflen = *buflenp; | |
1005 | ||
1006 | struct gaih_addrtuple **pat = *patp; | |
1007 | const HEADER *hp = &answer->hdr; | |
1008 | int ancount = ntohs (hp->ancount); | |
1009 | int qdcount = ntohs (hp->qdcount); | |
1010 | const u_char *cp = answer->buf + HFIXEDSZ; | |
1011 | const u_char *end_of_message = answer->buf + anslen; | |
a1ffb40e | 1012 | if (__glibc_unlikely (qdcount != 1)) |
1eb946b9 UD |
1013 | { |
1014 | *h_errnop = NO_RECOVERY; | |
1015 | return NSS_STATUS_UNAVAIL; | |
1016 | } | |
1017 | ||
b7da31a1 | 1018 | u_char packtmp[NS_MAXCDNAME]; |
1eb946b9 UD |
1019 | int n = __ns_name_unpack (answer->buf, end_of_message, cp, |
1020 | packtmp, sizeof packtmp); | |
1021 | /* We unpack the name to check it for validity. But we do not need | |
1022 | it later. */ | |
1023 | if (n != -1 && __ns_name_ntop (packtmp, buffer, buflen) == -1) | |
1024 | { | |
1025 | if (__builtin_expect (errno, 0) == EMSGSIZE) | |
1026 | { | |
1027 | too_small: | |
1028 | *errnop = ERANGE; | |
1029 | *h_errnop = NETDB_INTERNAL; | |
1030 | return NSS_STATUS_TRYAGAIN; | |
1031 | } | |
1032 | ||
1033 | n = -1; | |
1034 | } | |
1035 | ||
1036 | if (__builtin_expect (n < 0 || (res_hnok (buffer) == 0 | |
1037 | && (errno = EBADMSG)), 0)) | |
1038 | { | |
1039 | *errnop = errno; | |
1040 | *h_errnop = NO_RECOVERY; | |
1041 | return NSS_STATUS_UNAVAIL; | |
1042 | } | |
1043 | cp += n + QFIXEDSZ; | |
1044 | ||
1045 | int haveanswer = 0; | |
1046 | int had_error = 0; | |
1047 | char *canon = NULL; | |
1048 | char *h_name = NULL; | |
1049 | int h_namelen = 0; | |
1050 | ||
343996c4 UD |
1051 | if (ancount == 0) |
1052 | return NSS_STATUS_NOTFOUND; | |
1053 | ||
1eb946b9 UD |
1054 | while (ancount-- > 0 && cp < end_of_message && had_error == 0) |
1055 | { | |
1056 | n = __ns_name_unpack (answer->buf, end_of_message, cp, | |
1057 | packtmp, sizeof packtmp); | |
1058 | if (n != -1 && | |
1059 | (h_namelen = __ns_name_ntop (packtmp, buffer, buflen)) == -1) | |
1060 | { | |
1061 | if (__builtin_expect (errno, 0) == EMSGSIZE) | |
1062 | goto too_small; | |
1063 | ||
1064 | n = -1; | |
1065 | } | |
a1ffb40e | 1066 | if (__glibc_unlikely (n < 0 || res_hnok (buffer) == 0)) |
1eb946b9 UD |
1067 | { |
1068 | ++had_error; | |
1069 | continue; | |
1070 | } | |
ea42a20c | 1071 | if (*firstp && canon == NULL) |
1eb946b9 UD |
1072 | { |
1073 | h_name = buffer; | |
1074 | buffer += h_namelen; | |
1075 | buflen -= h_namelen; | |
1076 | } | |
1077 | ||
1078 | cp += n; /* name */ | |
b7da31a1 | 1079 | |
a1ffb40e | 1080 | if (__glibc_unlikely (cp + 10 > end_of_message)) |
b7da31a1 UD |
1081 | { |
1082 | ++had_error; | |
1083 | continue; | |
1084 | } | |
1085 | ||
cd5743fd | 1086 | int type = __ns_get16 (cp); |
1eb946b9 | 1087 | cp += INT16SZ; /* type */ |
cd5743fd | 1088 | int class = __ns_get16 (cp); |
1eb946b9 | 1089 | cp += INT16SZ; /* class */ |
cd5743fd | 1090 | int32_t ttl = __ns_get32 (cp); |
1eb946b9 | 1091 | cp += INT32SZ; /* TTL */ |
cd5743fd | 1092 | n = __ns_get16 (cp); |
1eb946b9 UD |
1093 | cp += INT16SZ; /* len */ |
1094 | ||
1095 | if (class != C_IN) | |
1096 | { | |
1097 | cp += n; | |
1098 | continue; | |
1099 | } | |
1100 | ||
1101 | if (type == T_CNAME) | |
1102 | { | |
1103 | char tbuf[MAXDNAME]; | |
48c41d04 SP |
1104 | |
1105 | /* A CNAME could also have a TTL entry. */ | |
1106 | if (ttlp != NULL && ttl < *ttlp) | |
1107 | *ttlp = ttl; | |
1108 | ||
1eb946b9 | 1109 | n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf); |
a1ffb40e | 1110 | if (__glibc_unlikely (n < 0 || res_hnok (tbuf) == 0)) |
1eb946b9 UD |
1111 | { |
1112 | ++had_error; | |
1113 | continue; | |
1114 | } | |
1115 | cp += n; | |
1116 | ||
1117 | if (*firstp) | |
1118 | { | |
1119 | /* Reclaim buffer space. */ | |
1120 | if (h_name + h_namelen == buffer) | |
1121 | { | |
1122 | buffer = h_name; | |
1123 | buflen += h_namelen; | |
1124 | } | |
1125 | ||
1126 | n = strlen (tbuf) + 1; | |
a1ffb40e | 1127 | if (__glibc_unlikely (n > buflen)) |
1eb946b9 | 1128 | goto too_small; |
a1ffb40e | 1129 | if (__glibc_unlikely (n >= MAXHOSTNAMELEN)) |
1eb946b9 UD |
1130 | { |
1131 | ++had_error; | |
1132 | continue; | |
1133 | } | |
1134 | ||
1135 | canon = buffer; | |
1136 | buffer = __mempcpy (buffer, tbuf, n); | |
1137 | buflen -= n; | |
1138 | h_namelen = 0; | |
1139 | } | |
1140 | continue; | |
1141 | } | |
9ba27474 UD |
1142 | #if 1 |
1143 | // We should not see any types other than those explicitly listed | |
1144 | // below. Some types sent by server seem missing, though. Just | |
1145 | // collect the data for now. | |
a1ffb40e | 1146 | if (__glibc_unlikely (type != T_A && type != T_AAAA)) |
9ba27474 | 1147 | #else |
1eb946b9 UD |
1148 | if (__builtin_expect (type == T_SIG, 0) |
1149 | || __builtin_expect (type == T_KEY, 0) | |
1150 | || __builtin_expect (type == T_NXT, 0) | |
c1283552 UD |
1151 | || __builtin_expect (type == T_PTR, 0) |
1152 | || __builtin_expect (type == T_DNAME, 0)) | |
9ba27474 | 1153 | #endif |
1eb946b9 UD |
1154 | { |
1155 | /* We don't support DNSSEC yet. For now, ignore the record | |
1156 | and send a low priority message to syslog. | |
1157 | ||
c1283552 | 1158 | We also don't expect T_PTR or T_DNAME messages. */ |
1eb946b9 UD |
1159 | syslog (LOG_DEBUG | LOG_AUTH, |
1160 | "getaddrinfo*.gaih_getanswer: got type \"%s\"", | |
1161 | p_type (type)); | |
1162 | cp += n; | |
1163 | continue; | |
1164 | } | |
1165 | if (type != T_A && type != T_AAAA) | |
1166 | abort (); | |
1167 | ||
1168 | if (*pat == NULL) | |
1169 | { | |
1170 | uintptr_t pad = (-(uintptr_t) buffer | |
1171 | % __alignof__ (struct gaih_addrtuple)); | |
1172 | buffer += pad; | |
1173 | buflen = buflen > pad ? buflen - pad : 0; | |
1174 | ||
1175 | if (__builtin_expect (buflen < sizeof (struct gaih_addrtuple), | |
1176 | 0)) | |
f854efd7 | 1177 | goto too_small; |
1eb946b9 UD |
1178 | |
1179 | *pat = (struct gaih_addrtuple *) buffer; | |
1180 | buffer += sizeof (struct gaih_addrtuple); | |
1181 | buflen -= sizeof (struct gaih_addrtuple); | |
1182 | } | |
1183 | ||
1184 | (*pat)->name = NULL; | |
1185 | (*pat)->next = NULL; | |
1186 | ||
1187 | if (*firstp) | |
1188 | { | |
48c41d04 SP |
1189 | /* We compose a single hostent out of the entire chain of |
1190 | entries, so the TTL of the hostent is essentially the lowest | |
1191 | TTL in the chain. */ | |
1192 | if (ttlp != NULL && ttl < *ttlp) | |
1eb946b9 UD |
1193 | *ttlp = ttl; |
1194 | ||
ea42a20c | 1195 | (*pat)->name = canon ?: h_name; |
1eb946b9 UD |
1196 | |
1197 | *firstp = 0; | |
1198 | } | |
1199 | ||
1200 | (*pat)->family = type == T_A ? AF_INET : AF_INET6; | |
b7da31a1 UD |
1201 | if (__builtin_expect ((type == T_A && n != INADDRSZ) |
1202 | || (type == T_AAAA && n != IN6ADDRSZ), 0)) | |
1203 | { | |
1204 | ++had_error; | |
1205 | continue; | |
1206 | } | |
1eb946b9 UD |
1207 | memcpy ((*pat)->addr, cp, n); |
1208 | cp += n; | |
1209 | (*pat)->scopeid = 0; | |
1210 | ||
1211 | pat = &((*pat)->next); | |
1212 | ||
1213 | haveanswer = 1; | |
1214 | } | |
1215 | ||
1216 | if (haveanswer) | |
1217 | { | |
1218 | *patp = pat; | |
1219 | *bufferp = buffer; | |
1220 | *buflenp = buflen; | |
1221 | ||
1222 | *h_errnop = NETDB_SUCCESS; | |
1223 | return NSS_STATUS_SUCCESS; | |
1224 | } | |
1225 | ||
1226 | /* Special case here: if the resolver sent a result but it only | |
1227 | contains a CNAME while we are looking for a T_A or T_AAAA record, | |
1228 | we fail with NOTFOUND instead of TRYAGAIN. */ | |
1229 | return canon == NULL ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND; | |
1230 | } | |
1231 | ||
1232 | ||
1233 | static enum nss_status | |
1234 | gaih_getanswer (const querybuf *answer1, int anslen1, const querybuf *answer2, | |
1235 | int anslen2, const char *qname, | |
1236 | struct gaih_addrtuple **pat, char *buffer, size_t buflen, | |
1237 | int *errnop, int *h_errnop, int32_t *ttlp) | |
1238 | { | |
1239 | int first = 1; | |
1240 | ||
343996c4 UD |
1241 | enum nss_status status = NSS_STATUS_NOTFOUND; |
1242 | ||
1243 | if (anslen1 > 0) | |
1244 | status = gaih_getanswer_slice(answer1, anslen1, qname, | |
1245 | &pat, &buffer, &buflen, | |
1246 | errnop, h_errnop, ttlp, | |
1247 | &first); | |
f854efd7 | 1248 | if ((status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND |
b7da31a1 | 1249 | || (status == NSS_STATUS_TRYAGAIN |
86ae07a8 JL |
1250 | /* We want to look at the second answer in case of an |
1251 | NSS_STATUS_TRYAGAIN only if the error is non-recoverable, i.e. | |
1252 | *h_errnop is NO_RECOVERY. If not, and if the failure was due to | |
1253 | an insufficient buffer (ERANGE), then we need to drop the results | |
1254 | and pass on the NSS_STATUS_TRYAGAIN to the caller so that it can | |
1255 | repeat the query with a larger buffer. */ | |
1256 | && (*errnop != ERANGE || *h_errnop == NO_RECOVERY))) | |
343996c4 | 1257 | && answer2 != NULL && anslen2 > 0) |
f854efd7 UD |
1258 | { |
1259 | enum nss_status status2 = gaih_getanswer_slice(answer2, anslen2, qname, | |
1260 | &pat, &buffer, &buflen, | |
1261 | errnop, h_errnop, ttlp, | |
1262 | &first); | |
b7da31a1 | 1263 | if (status != NSS_STATUS_SUCCESS && status2 != NSS_STATUS_NOTFOUND) |
f854efd7 UD |
1264 | status = status2; |
1265 | } | |
1eb946b9 UD |
1266 | |
1267 | return status; | |
1268 | } |