Deduplicate resolv/nss_dns/dns-host.c
[glibc.git] / resolv / nss_dns / dns-host.c
blob47f998c23924eb27f7665be746cde476ab4ade11
1 /* Copyright (C) 1996-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Extended from original form by Ulrich Drepper <drepper@cygnus.com>, 1996.
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, see
17 <http://www.gnu.org/licenses/>. */
19 /* Parts of this file are plain copies of the file `gethtnamadr.c' from
20 the bind package and it has the following copyright. */
23 * ++Copyright++ 1985, 1988, 1993
24 * -
25 * Copyright (c) 1985, 1988, 1993
26 * The Regents of the University of California. All rights reserved.
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.
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.
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.
51 * -
52 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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--
73 #include <assert.h>
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>
83 #include "nsswitch.h"
85 /* Get implementation for some internal functions. */
86 #include <resolv/mapv4v6addr.h>
87 #include <resolv/mapv4v6hostent.h>
89 #define RESOLVSORT
91 #if PACKETSZ > 65536
92 # define MAXPACKET PACKETSZ
93 #else
94 # define MAXPACKET 65536
95 #endif
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
102 static const char AskedForGot[] = "\
103 gethostby*.getanswer: asked for \"%s\", got \"%s\"";
106 /* We need this time later. */
107 typedef union querybuf
109 HEADER hdr;
110 u_char buf[MAXPACKET];
111 } querybuf;
113 /* These functions are defined in res_comp.c. */
114 #define NS_MAXCDNAME 255 /* maximum compressed domain name */
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);
120 static enum nss_status getanswer_r (const querybuf *answer, int anslen,
121 const char *qname, int qtype,
122 struct hostent *result, char *buffer,
123 size_t buflen, int *errnop, int *h_errnop,
124 int map, int32_t *ttlp, char **canonp);
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);
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)
142 enum nss_status
143 _nss_dns_gethostbyname3_r (const char *name, int af, struct hostent *result,
144 char *buffer, size_t buflen, int *errnop,
145 int *h_errnop, int32_t *ttlp, char **canonp)
147 union
149 querybuf *buf;
150 u_char *ptr;
151 } host_buffer;
152 querybuf *orig_host_buffer;
153 char tmp[NS_MAXDNAME];
154 int size, type, n;
155 const char *cp;
156 int map = 0;
157 int olderr = errno;
158 enum nss_status status;
160 if (__res_maybe_init (&_res, 0) == -1)
161 return NSS_STATUS_UNAVAIL;
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:
173 *h_errnop = NO_DATA;
174 *errnop = EAFNOSUPPORT;
175 return NSS_STATUS_UNAVAIL;
178 result->h_addrtype = af;
179 result->h_length = size;
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.
186 if (strchr (name, '.') == NULL
187 && (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL)
188 name = cp;
190 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
192 n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer.buf->buf,
193 1024, &host_buffer.ptr, NULL, NULL, NULL);
194 if (n < 0)
196 switch (errno)
198 case ESRCH:
199 status = NSS_STATUS_TRYAGAIN;
200 h_errno = TRY_AGAIN;
201 break;
202 /* System has run out of file descriptors. */
203 case EMFILE:
204 case ENFILE:
205 h_errno = NETDB_INTERNAL;
206 /* Fall through. */
207 case ECONNREFUSED:
208 case ETIMEDOUT:
209 status = NSS_STATUS_UNAVAIL;
210 break;
211 default:
212 status = NSS_STATUS_NOTFOUND;
213 break;
215 *h_errnop = h_errno;
216 if (h_errno == TRY_AGAIN)
217 *errnop = EAGAIN;
218 else
219 __set_errno (olderr);
221 /* If we are looking for an IPv6 address and mapping is enabled
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))
225 n = __libc_res_nsearch (&_res, name, C_IN, T_A, host_buffer.buf->buf,
226 host_buffer.buf != orig_host_buffer
227 ? MAXPACKET : 1024, &host_buffer.ptr,
228 NULL, NULL, NULL);
230 if (n < 0)
232 if (host_buffer.buf != orig_host_buffer)
233 free (host_buffer.buf);
234 return status;
237 map = 1;
239 result->h_addrtype = AF_INET;
240 result->h_length = INADDRSZ;
243 status = getanswer_r (host_buffer.buf, n, name, type, result, buffer, buflen,
244 errnop, h_errnop, map, ttlp, canonp);
245 if (host_buffer.buf != orig_host_buffer)
246 free (host_buffer.buf);
247 return status;
249 hidden_def (_nss_dns_gethostbyname3_r)
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)
257 return _nss_dns_gethostbyname3_r (name, af, result, buffer, buflen, errnop,
258 h_errnop, NULL, NULL);
262 enum nss_status
263 _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
264 char *buffer, size_t buflen, int *errnop,
265 int *h_errnop)
267 enum nss_status status = NSS_STATUS_NOTFOUND;
269 if (_res.options & RES_USE_INET6)
270 status = _nss_dns_gethostbyname3_r (name, AF_INET6, result, buffer,
271 buflen, errnop, h_errnop, NULL, NULL);
272 if (status == NSS_STATUS_NOTFOUND)
273 status = _nss_dns_gethostbyname3_r (name, AF_INET, result, buffer,
274 buflen, errnop, h_errnop, NULL, NULL);
276 return status;
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)
285 if (__res_maybe_init (&_res, 0) == -1)
286 return NSS_STATUS_UNAVAIL;
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.
293 if (strchr (name, '.') == NULL)
295 char *tmp = alloca (NS_MAXDNAME);
296 const char *cp = res_hostalias (&_res, name, tmp, NS_MAXDNAME);
297 if (cp != NULL)
298 name = cp;
301 int anslen = 2048;
302 union
304 querybuf *buf;
305 u_char *ptr;
306 } host_buffer;
307 querybuf *orig_host_buffer;
308 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (anslen);
309 u_char *ans2p = NULL;
310 int nans2p = 0;
311 int resplen2 = 0;
313 int olderr = errno;
314 enum nss_status status;
315 int n = __libc_res_nsearch (&_res, name, C_IN, T_UNSPEC,
316 host_buffer.buf->buf, anslen, &host_buffer.ptr,
317 &ans2p, &nans2p, &resplen2);
318 if (n >= 0)
320 status = gaih_getanswer (host_buffer.buf, n, (const querybuf *) ans2p,
321 resplen2, name, pat, buffer, buflen,
322 errnop, herrnop, ttlp);
324 else
326 switch (errno)
328 case ESRCH:
329 status = NSS_STATUS_TRYAGAIN;
330 h_errno = TRY_AGAIN;
331 break;
332 /* System has run out of file descriptors. */
333 case EMFILE:
334 case ENFILE:
335 h_errno = NETDB_INTERNAL;
336 /* Fall through. */
337 case ECONNREFUSED:
338 case ETIMEDOUT:
339 status = NSS_STATUS_UNAVAIL;
340 break;
341 default:
342 status = NSS_STATUS_NOTFOUND;
343 break;
346 *herrnop = h_errno;
347 if (h_errno == TRY_AGAIN)
348 *errnop = EAGAIN;
349 else
350 __set_errno (olderr);
353 /* Check whether ans2p was separately allocated. */
354 if (host_buffer.buf != orig_host_buffer)
355 anslen = MAXPACKET;
356 if (ans2p != NULL
357 && (ans2p < host_buffer.ptr || ans2p >= host_buffer.ptr + anslen))
358 free (ans2p);
360 if (host_buffer.buf != orig_host_buffer)
361 free (host_buffer.buf);
363 return status;
367 extern enum nss_status _nss_dns_gethostbyaddr2_r (const void *addr,
368 socklen_t len, int af,
369 struct hostent *result,
370 char *buffer, size_t buflen,
371 int *errnop, int *h_errnop,
372 int32_t *ttlp);
373 hidden_proto (_nss_dns_gethostbyaddr2_r)
375 enum nss_status
376 _nss_dns_gethostbyaddr2_r (const void *addr, socklen_t len, int af,
377 struct hostent *result, char *buffer, size_t buflen,
378 int *errnop, int *h_errnop, int32_t *ttlp)
380 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
381 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
382 static const u_char v6local[] = { 0,0, 0,1 };
383 const u_char *uaddr = (const u_char *)addr;
384 struct host_data
386 char *aliases[MAX_NR_ALIASES];
387 unsigned char host_addr[16]; /* IPv4 or IPv6 */
388 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
389 char linebuffer[0];
390 } *host_data = (struct host_data *) buffer;
391 union
393 querybuf *buf;
394 u_char *ptr;
395 } host_buffer;
396 querybuf *orig_host_buffer;
397 char qbuf[MAXDNAME+1], *qp = NULL;
398 size_t size;
399 int n, status;
400 int olderr = errno;
402 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct host_data);
403 buffer += pad;
404 buflen = buflen > pad ? buflen - pad : 0;
406 if (__glibc_unlikely (buflen < sizeof (struct host_data)))
408 *errnop = ERANGE;
409 *h_errnop = NETDB_INTERNAL;
410 return NSS_STATUS_TRYAGAIN;
413 host_data = (struct host_data *) buffer;
415 if (__res_maybe_init (&_res, 0) == -1)
416 return NSS_STATUS_UNAVAIL;
418 if (af == AF_INET6 && len == IN6ADDRSZ
419 && (memcmp (uaddr, mapped, sizeof mapped) == 0
420 || (memcmp (uaddr, tunnelled, sizeof tunnelled) == 0
421 && memcmp (&uaddr[sizeof tunnelled], v6local, sizeof v6local))))
423 /* Unmap. */
424 addr += sizeof mapped;
425 uaddr += sizeof mapped;
426 af = AF_INET;
427 len = INADDRSZ;
430 switch (af)
432 case AF_INET:
433 size = INADDRSZ;
434 break;
435 case AF_INET6:
436 size = IN6ADDRSZ;
437 break;
438 default:
439 *errnop = EAFNOSUPPORT;
440 *h_errnop = NETDB_INTERNAL;
441 return NSS_STATUS_UNAVAIL;
443 if (size > len)
445 *errnop = EAFNOSUPPORT;
446 *h_errnop = NETDB_INTERNAL;
447 return NSS_STATUS_UNAVAIL;
450 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
452 switch (af)
454 case AF_INET:
455 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
456 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
457 break;
458 case AF_INET6:
459 /* Only lookup with the byte string format if the user wants it. */
460 if (__glibc_unlikely (_res.options & RES_USEBSTRING))
462 qp = stpcpy (qbuf, "\\[x");
463 for (n = 0; n < IN6ADDRSZ; ++n)
464 qp += sprintf (qp, "%02hhx", uaddr[n]);
465 strcpy (qp, "].ip6.arpa");
466 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR,
467 host_buffer.buf->buf, 1024, &host_buffer.ptr,
468 NULL, NULL, NULL);
469 if (n >= 0)
470 goto got_it_already;
472 qp = qbuf;
473 for (n = IN6ADDRSZ - 1; n >= 0; n--)
475 static const char nibblechar[16] = "0123456789abcdef";
476 *qp++ = nibblechar[uaddr[n] & 0xf];
477 *qp++ = '.';
478 *qp++ = nibblechar[(uaddr[n] >> 4) & 0xf];
479 *qp++ = '.';
481 strcpy(qp, "ip6.arpa");
482 break;
483 default:
484 /* Cannot happen. */
485 break;
488 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
489 1024, &host_buffer.ptr, NULL, NULL, NULL);
490 if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0)
492 strcpy (qp, "ip6.int");
493 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
494 host_buffer.buf != orig_host_buffer
495 ? MAXPACKET : 1024, &host_buffer.ptr,
496 NULL, NULL, NULL);
498 if (n < 0)
500 *h_errnop = h_errno;
501 __set_errno (olderr);
502 if (host_buffer.buf != orig_host_buffer)
503 free (host_buffer.buf);
504 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
507 got_it_already:
508 status = getanswer_r (host_buffer.buf, n, qbuf, T_PTR, result, buffer, buflen,
509 errnop, h_errnop, 0 /* XXX */, ttlp, NULL);
510 if (host_buffer.buf != orig_host_buffer)
511 free (host_buffer.buf);
512 if (status != NSS_STATUS_SUCCESS)
513 return status;
515 #ifdef SUNSECURITY
516 This is not implemented because it is not possible to use the current
517 source from bind in a multi-threaded program.
518 #endif
520 result->h_addrtype = af;
521 result->h_length = len;
522 memcpy (host_data->host_addr, addr, len);
523 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
524 host_data->h_addr_ptrs[1] = NULL;
525 #if 0
526 /* XXX I think this is wrong. Why should an IPv4 address be
527 converted to IPv6 if the user explicitly asked for IPv4? */
528 if (af == AF_INET && (_res.options & RES_USE_INET6))
530 map_v4v6_address ((char *) host_data->host_addr,
531 (char *) host_data->host_addr);
532 result->h_addrtype = AF_INET6;
533 result->h_length = IN6ADDRSZ;
535 #endif
536 *h_errnop = NETDB_SUCCESS;
537 return NSS_STATUS_SUCCESS;
539 hidden_def (_nss_dns_gethostbyaddr2_r)
542 enum nss_status
543 _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
544 struct hostent *result, char *buffer, size_t buflen,
545 int *errnop, int *h_errnop)
547 return _nss_dns_gethostbyaddr2_r (addr, len, af, result, buffer, buflen,
548 errnop, h_errnop, NULL);
551 #ifdef RESOLVSORT
552 static void addrsort (char **ap, int num);
554 static void
555 addrsort (char **ap, int num)
557 int i, j;
558 char **p;
559 short aval[MAX_NR_ADDRS];
560 int needsort = 0;
562 p = ap;
563 if (num > MAX_NR_ADDRS)
564 num = MAX_NR_ADDRS;
565 for (i = 0; i < num; i++, p++)
567 for (j = 0 ; (unsigned)j < _res.nsort; j++)
568 if (_res.sort_list[j].addr.s_addr ==
569 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
570 break;
571 aval[i] = j;
572 if (needsort == 0 && i > 0 && j < aval[i-1])
573 needsort = i;
575 if (!needsort)
576 return;
578 while (needsort++ < num)
579 for (j = needsort - 2; j >= 0; j--)
580 if (aval[j] > aval[j+1])
582 char *hp;
584 i = aval[j];
585 aval[j] = aval[j+1];
586 aval[j+1] = i;
588 hp = ap[j];
589 ap[j] = ap[j+1];
590 ap[j+1] = hp;
592 else
593 break;
595 #endif
597 static enum nss_status
598 getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
599 struct hostent *result, char *buffer, size_t buflen,
600 int *errnop, int *h_errnop, int map, int32_t *ttlp, char **canonp)
602 struct host_data
604 char *aliases[MAX_NR_ALIASES];
605 unsigned char host_addr[16]; /* IPv4 or IPv6 */
606 char *h_addr_ptrs[0];
607 } *host_data;
608 int linebuflen;
609 const HEADER *hp;
610 const u_char *end_of_message, *cp;
611 int n, ancount, qdcount;
612 int haveanswer, had_error;
613 char *bp, **ap, **hap;
614 char tbuf[MAXDNAME];
615 const char *tname;
616 int (*name_ok) (const char *);
617 u_char packtmp[NS_MAXCDNAME];
618 int have_to_map = 0;
619 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct host_data);
620 buffer += pad;
621 if (__glibc_unlikely (buflen < sizeof (struct host_data) + pad))
623 /* The buffer is too small. */
624 too_small:
625 *errnop = ERANGE;
626 *h_errnop = NETDB_INTERNAL;
627 return NSS_STATUS_TRYAGAIN;
629 host_data = (struct host_data *) buffer;
630 linebuflen = buflen - sizeof (struct host_data);
631 if (buflen - sizeof (struct host_data) != linebuflen)
632 linebuflen = INT_MAX;
634 tname = qname;
635 result->h_name = NULL;
636 end_of_message = answer->buf + anslen;
637 switch (qtype)
639 case T_A:
640 case T_AAAA:
641 name_ok = res_hnok;
642 break;
643 case T_PTR:
644 name_ok = res_dnok;
645 break;
646 default:
647 *errnop = ENOENT;
648 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
652 * find first satisfactory answer
654 hp = &answer->hdr;
655 ancount = ntohs (hp->ancount);
656 qdcount = ntohs (hp->qdcount);
657 cp = answer->buf + HFIXEDSZ;
658 if (__builtin_expect (qdcount, 1) != 1)
660 *h_errnop = NO_RECOVERY;
661 return NSS_STATUS_UNAVAIL;
663 if (sizeof (struct host_data) + (ancount + 1) * sizeof (char *) >= buflen)
664 goto too_small;
665 bp = (char *) &host_data->h_addr_ptrs[ancount + 1];
666 linebuflen -= (ancount + 1) * sizeof (char *);
668 n = __ns_name_unpack (answer->buf, end_of_message, cp,
669 packtmp, sizeof packtmp);
670 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
672 if (__builtin_expect (errno, 0) == EMSGSIZE)
673 goto too_small;
675 n = -1;
678 if (n > 0 && bp[0] == '.')
679 bp[0] = '\0';
681 if (__builtin_expect (n < 0 || ((*name_ok) (bp) == 0 && (errno = EBADMSG)),
684 *errnop = errno;
685 *h_errnop = NO_RECOVERY;
686 return NSS_STATUS_UNAVAIL;
688 cp += n + QFIXEDSZ;
690 if (qtype == T_A || qtype == T_AAAA)
692 /* res_send() has already verified that the query name is the
693 * same as the one we sent; this just gets the expanded name
694 * (i.e., with the succeeding search-domain tacked on).
696 n = strlen (bp) + 1; /* for the \0 */
697 if (n >= MAXHOSTNAMELEN)
699 *h_errnop = NO_RECOVERY;
700 *errnop = ENOENT;
701 return NSS_STATUS_TRYAGAIN;
703 result->h_name = bp;
704 bp += n;
705 linebuflen -= n;
706 if (linebuflen < 0)
707 goto too_small;
708 /* The qname can be abbreviated, but h_name is now absolute. */
709 qname = result->h_name;
712 ap = host_data->aliases;
713 *ap = NULL;
714 result->h_aliases = host_data->aliases;
715 hap = host_data->h_addr_ptrs;
716 *hap = NULL;
717 result->h_addr_list = host_data->h_addr_ptrs;
718 haveanswer = 0;
719 had_error = 0;
721 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
723 int type, class;
725 n = __ns_name_unpack (answer->buf, end_of_message, cp,
726 packtmp, sizeof packtmp);
727 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
729 if (__builtin_expect (errno, 0) == EMSGSIZE)
730 goto too_small;
732 n = -1;
735 if (__glibc_unlikely (n < 0 || (*name_ok) (bp) == 0))
737 ++had_error;
738 continue;
740 cp += n; /* name */
742 if (__glibc_unlikely (cp + 10 > end_of_message))
744 ++had_error;
745 continue;
748 type = __ns_get16 (cp);
749 cp += INT16SZ; /* type */
750 class = __ns_get16 (cp);
751 cp += INT16SZ; /* class */
752 int32_t ttl = __ns_get32 (cp);
753 cp += INT32SZ; /* TTL */
754 n = __ns_get16 (cp);
755 cp += INT16SZ; /* len */
756 if (__glibc_unlikely (class != C_IN))
758 /* XXX - debug? syslog? */
759 cp += n;
760 continue; /* XXX - had_error++ ? */
763 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME)
765 /* A CNAME could also have a TTL entry. */
766 if (ttlp != NULL && ttl < *ttlp)
767 *ttlp = ttl;
769 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
770 continue;
771 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
772 if (__glibc_unlikely (n < 0 || (*name_ok) (tbuf) == 0))
774 ++had_error;
775 continue;
777 cp += n;
778 /* Store alias. */
779 *ap++ = bp;
780 n = strlen (bp) + 1; /* For the \0. */
781 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
783 ++had_error;
784 continue;
786 bp += n;
787 linebuflen -= n;
788 /* Get canonical name. */
789 n = strlen (tbuf) + 1; /* For the \0. */
790 if (__glibc_unlikely (n > linebuflen))
791 goto too_small;
792 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
794 ++had_error;
795 continue;
797 result->h_name = bp;
798 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
799 linebuflen -= n;
800 continue;
803 if (qtype == T_PTR && type == T_CNAME)
805 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
806 if (__glibc_unlikely (n < 0 || res_dnok (tbuf) == 0))
808 ++had_error;
809 continue;
811 cp += n;
812 /* Get canonical name. */
813 n = strlen (tbuf) + 1; /* For the \0. */
814 if (__glibc_unlikely (n > linebuflen))
815 goto too_small;
816 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
818 ++had_error;
819 continue;
821 tname = bp;
822 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
823 linebuflen -= n;
824 continue;
826 if (__builtin_expect (type == T_SIG, 0)
827 || __builtin_expect (type == T_KEY, 0)
828 || __builtin_expect (type == T_NXT, 0))
830 /* We don't support DNSSEC yet. For now, ignore the record
831 and send a low priority message to syslog. */
832 syslog (LOG_DEBUG | LOG_AUTH,
833 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
834 qname, p_class (C_IN), p_type(qtype), p_type (type));
835 cp += n;
836 continue;
839 if (type == T_A && qtype == T_AAAA && map)
840 have_to_map = 1;
841 else if (__glibc_unlikely (type != qtype))
843 syslog (LOG_NOTICE | LOG_AUTH,
844 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
845 qname, p_class (C_IN), p_type (qtype), p_type (type));
846 cp += n;
847 continue; /* XXX - had_error++ ? */
850 switch (type)
852 case T_PTR:
853 if (__glibc_unlikely (strcasecmp (tname, bp) != 0))
855 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
856 cp += n;
857 continue; /* XXX - had_error++ ? */
860 n = __ns_name_unpack (answer->buf, end_of_message, cp,
861 packtmp, sizeof packtmp);
862 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
864 if (__builtin_expect (errno, 0) == EMSGSIZE)
865 goto too_small;
867 n = -1;
870 if (__glibc_unlikely (n < 0 || res_hnok (bp) == 0))
872 ++had_error;
873 break;
875 #if MULTI_PTRS_ARE_ALIASES
876 cp += n;
877 if (haveanswer == 0)
878 result->h_name = bp;
879 else if (ap < &host_data->aliases[MAXALIASES-1])
880 *ap++ = bp;
881 else
882 n = -1;
883 if (n != -1)
885 n = strlen (bp) + 1; /* for the \0 */
886 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
888 ++had_error;
889 break;
891 bp += n;
892 linebuflen -= n;
894 break;
895 #else
896 result->h_name = bp;
897 if (have_to_map)
899 n = strlen (bp) + 1; /* for the \0 */
900 if (__glibc_unlikely (n >= MAXHOSTNAMELEN))
902 ++had_error;
903 break;
905 bp += n;
906 linebuflen -= n;
907 if (map_v4v6_hostent (result, &bp, &linebuflen))
908 goto too_small;
910 *h_errnop = NETDB_SUCCESS;
911 return NSS_STATUS_SUCCESS;
912 #endif
913 case T_A:
914 case T_AAAA:
915 if (__builtin_expect (strcasecmp (result->h_name, bp), 0) != 0)
917 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
918 cp += n;
919 continue; /* XXX - had_error++ ? */
921 if (n != result->h_length)
923 cp += n;
924 continue;
926 if (!haveanswer)
928 int nn;
930 /* We compose a single hostent out of the entire chain of
931 entries, so the TTL of the hostent is essentially the lowest
932 TTL in the chain. */
933 if (ttlp != NULL && ttl < *ttlp)
934 *ttlp = ttl;
935 if (canonp != NULL)
936 *canonp = bp;
937 result->h_name = bp;
938 nn = strlen (bp) + 1; /* for the \0 */
939 bp += nn;
940 linebuflen -= nn;
943 linebuflen -= sizeof (align) - ((u_long) bp % sizeof (align));
944 bp += sizeof (align) - ((u_long) bp % sizeof (align));
946 if (__glibc_unlikely (n > linebuflen))
947 goto too_small;
948 bp = __mempcpy (*hap++ = bp, cp, n);
949 cp += n;
950 linebuflen -= n;
951 break;
952 default:
953 abort ();
955 if (had_error == 0)
956 ++haveanswer;
959 if (haveanswer > 0)
961 *ap = NULL;
962 *hap = NULL;
963 #if defined RESOLVSORT
965 * Note: we sort even if host can take only one address
966 * in its return structures - should give it the "best"
967 * address in that case, not some random one
969 if (_res.nsort && haveanswer > 1 && qtype == T_A)
970 addrsort (host_data->h_addr_ptrs, haveanswer);
971 #endif /*RESOLVSORT*/
973 if (result->h_name == NULL)
975 n = strlen (qname) + 1; /* For the \0. */
976 if (n > linebuflen)
977 goto too_small;
978 if (n >= MAXHOSTNAMELEN)
979 goto no_recovery;
980 result->h_name = bp;
981 bp = __mempcpy (bp, qname, n); /* Cannot overflow. */
982 linebuflen -= n;
985 if (have_to_map)
986 if (map_v4v6_hostent (result, &bp, &linebuflen))
987 goto too_small;
988 *h_errnop = NETDB_SUCCESS;
989 return NSS_STATUS_SUCCESS;
991 no_recovery:
992 *h_errnop = NO_RECOVERY;
993 *errnop = ENOENT;
994 /* Special case here: if the resolver sent a result but it only
995 contains a CNAME while we are looking for a T_A or T_AAAA record,
996 we fail with NOTFOUND instead of TRYAGAIN. */
997 return ((qtype == T_A || qtype == T_AAAA) && ap != host_data->aliases
998 ? NSS_STATUS_NOTFOUND : NSS_STATUS_TRYAGAIN);
1002 static enum nss_status
1003 gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname,
1004 struct gaih_addrtuple ***patp,
1005 char **bufferp, size_t *buflenp,
1006 int *errnop, int *h_errnop, int32_t *ttlp, int *firstp)
1008 char *buffer = *bufferp;
1009 size_t buflen = *buflenp;
1011 struct gaih_addrtuple **pat = *patp;
1012 const HEADER *hp = &answer->hdr;
1013 int ancount = ntohs (hp->ancount);
1014 int qdcount = ntohs (hp->qdcount);
1015 const u_char *cp = answer->buf + HFIXEDSZ;
1016 const u_char *end_of_message = answer->buf + anslen;
1017 if (__glibc_unlikely (qdcount != 1))
1019 *h_errnop = NO_RECOVERY;
1020 return NSS_STATUS_UNAVAIL;
1023 u_char packtmp[NS_MAXCDNAME];
1024 int n = __ns_name_unpack (answer->buf, end_of_message, cp,
1025 packtmp, sizeof packtmp);
1026 /* We unpack the name to check it for validity. But we do not need
1027 it later. */
1028 if (n != -1 && __ns_name_ntop (packtmp, buffer, buflen) == -1)
1030 if (__builtin_expect (errno, 0) == EMSGSIZE)
1032 too_small:
1033 *errnop = ERANGE;
1034 *h_errnop = NETDB_INTERNAL;
1035 return NSS_STATUS_TRYAGAIN;
1038 n = -1;
1041 if (__builtin_expect (n < 0 || (res_hnok (buffer) == 0
1042 && (errno = EBADMSG)), 0))
1044 *errnop = errno;
1045 *h_errnop = NO_RECOVERY;
1046 return NSS_STATUS_UNAVAIL;
1048 cp += n + QFIXEDSZ;
1050 int haveanswer = 0;
1051 int had_error = 0;
1052 char *canon = NULL;
1053 char *h_name = NULL;
1054 int h_namelen = 0;
1056 if (ancount == 0)
1057 return NSS_STATUS_NOTFOUND;
1059 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
1061 n = __ns_name_unpack (answer->buf, end_of_message, cp,
1062 packtmp, sizeof packtmp);
1063 if (n != -1 &&
1064 (h_namelen = __ns_name_ntop (packtmp, buffer, buflen)) == -1)
1066 if (__builtin_expect (errno, 0) == EMSGSIZE)
1067 goto too_small;
1069 n = -1;
1071 if (__glibc_unlikely (n < 0 || res_hnok (buffer) == 0))
1073 ++had_error;
1074 continue;
1076 if (*firstp && canon == NULL)
1078 h_name = buffer;
1079 buffer += h_namelen;
1080 buflen -= h_namelen;
1083 cp += n; /* name */
1085 if (__glibc_unlikely (cp + 10 > end_of_message))
1087 ++had_error;
1088 continue;
1091 int type = __ns_get16 (cp);
1092 cp += INT16SZ; /* type */
1093 int class = __ns_get16 (cp);
1094 cp += INT16SZ; /* class */
1095 int32_t ttl = __ns_get32 (cp);
1096 cp += INT32SZ; /* TTL */
1097 n = __ns_get16 (cp);
1098 cp += INT16SZ; /* len */
1100 if (class != C_IN)
1102 cp += n;
1103 continue;
1106 if (type == T_CNAME)
1108 char tbuf[MAXDNAME];
1110 /* A CNAME could also have a TTL entry. */
1111 if (ttlp != NULL && ttl < *ttlp)
1112 *ttlp = ttl;
1114 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
1115 if (__glibc_unlikely (n < 0 || res_hnok (tbuf) == 0))
1117 ++had_error;
1118 continue;
1120 cp += n;
1122 if (*firstp)
1124 /* Reclaim buffer space. */
1125 if (h_name + h_namelen == buffer)
1127 buffer = h_name;
1128 buflen += h_namelen;
1131 n = strlen (tbuf) + 1;
1132 if (__glibc_unlikely (n > buflen))
1133 goto too_small;
1134 if (__glibc_unlikely (n >= MAXHOSTNAMELEN))
1136 ++had_error;
1137 continue;
1140 canon = buffer;
1141 buffer = __mempcpy (buffer, tbuf, n);
1142 buflen -= n;
1143 h_namelen = 0;
1145 continue;
1147 #if 1
1148 // We should not see any types other than those explicitly listed
1149 // below. Some types sent by server seem missing, though. Just
1150 // collect the data for now.
1151 if (__glibc_unlikely (type != T_A && type != T_AAAA))
1152 #else
1153 if (__builtin_expect (type == T_SIG, 0)
1154 || __builtin_expect (type == T_KEY, 0)
1155 || __builtin_expect (type == T_NXT, 0)
1156 || __builtin_expect (type == T_PTR, 0)
1157 || __builtin_expect (type == T_DNAME, 0))
1158 #endif
1160 /* We don't support DNSSEC yet. For now, ignore the record
1161 and send a low priority message to syslog.
1163 We also don't expect T_PTR or T_DNAME messages. */
1164 syslog (LOG_DEBUG | LOG_AUTH,
1165 "getaddrinfo*.gaih_getanswer: got type \"%s\"",
1166 p_type (type));
1167 cp += n;
1168 continue;
1170 if (type != T_A && type != T_AAAA)
1171 abort ();
1173 if (*pat == NULL)
1175 uintptr_t pad = (-(uintptr_t) buffer
1176 % __alignof__ (struct gaih_addrtuple));
1177 buffer += pad;
1178 buflen = buflen > pad ? buflen - pad : 0;
1180 if (__builtin_expect (buflen < sizeof (struct gaih_addrtuple),
1182 goto too_small;
1184 *pat = (struct gaih_addrtuple *) buffer;
1185 buffer += sizeof (struct gaih_addrtuple);
1186 buflen -= sizeof (struct gaih_addrtuple);
1189 (*pat)->name = NULL;
1190 (*pat)->next = NULL;
1192 if (*firstp)
1194 /* We compose a single hostent out of the entire chain of
1195 entries, so the TTL of the hostent is essentially the lowest
1196 TTL in the chain. */
1197 if (ttlp != NULL && ttl < *ttlp)
1198 *ttlp = ttl;
1200 (*pat)->name = canon ?: h_name;
1202 *firstp = 0;
1205 (*pat)->family = type == T_A ? AF_INET : AF_INET6;
1206 if (__builtin_expect ((type == T_A && n != INADDRSZ)
1207 || (type == T_AAAA && n != IN6ADDRSZ), 0))
1209 ++had_error;
1210 continue;
1212 memcpy ((*pat)->addr, cp, n);
1213 cp += n;
1214 (*pat)->scopeid = 0;
1216 pat = &((*pat)->next);
1218 haveanswer = 1;
1221 if (haveanswer)
1223 *patp = pat;
1224 *bufferp = buffer;
1225 *buflenp = buflen;
1227 *h_errnop = NETDB_SUCCESS;
1228 return NSS_STATUS_SUCCESS;
1231 /* Special case here: if the resolver sent a result but it only
1232 contains a CNAME while we are looking for a T_A or T_AAAA record,
1233 we fail with NOTFOUND instead of TRYAGAIN. */
1234 return canon == NULL ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
1238 static enum nss_status
1239 gaih_getanswer (const querybuf *answer1, int anslen1, const querybuf *answer2,
1240 int anslen2, const char *qname,
1241 struct gaih_addrtuple **pat, char *buffer, size_t buflen,
1242 int *errnop, int *h_errnop, int32_t *ttlp)
1244 int first = 1;
1246 enum nss_status status = NSS_STATUS_NOTFOUND;
1248 if (anslen1 > 0)
1249 status = gaih_getanswer_slice(answer1, anslen1, qname,
1250 &pat, &buffer, &buflen,
1251 errnop, h_errnop, ttlp,
1252 &first);
1253 if ((status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND
1254 || (status == NSS_STATUS_TRYAGAIN
1255 /* We want to look at the second answer in case of an
1256 NSS_STATUS_TRYAGAIN only if the error is non-recoverable, i.e.
1257 *h_errnop is NO_RECOVERY. If not, and if the failure was due to
1258 an insufficient buffer (ERANGE), then we need to drop the results
1259 and pass on the NSS_STATUS_TRYAGAIN to the caller so that it can
1260 repeat the query with a larger buffer. */
1261 && (*errnop != ERANGE || *h_errnop == NO_RECOVERY)))
1262 && answer2 != NULL && anslen2 > 0)
1264 enum nss_status status2 = gaih_getanswer_slice(answer2, anslen2, qname,
1265 &pat, &buffer, &buflen,
1266 errnop, h_errnop, ttlp,
1267 &first);
1268 if (status != NSS_STATUS_SUCCESS && status2 != NSS_STATUS_NOTFOUND)
1269 status = status2;
1272 return status;