Fix possibly uninitilized variable handling.
[glibc.git] / resolv / nss_dns / dns-host.c
blobe0c4978c3b39cde1c106e9ed76d415b0b12b184b
1 /* Copyright (C) 1996-2004, 2007-2009, 2010 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 /* Parts of this file are plain copies of the file `gethtnamadr.c' from
21 the bind package and it has the following copyright. */
24 * ++Copyright++ 1985, 1988, 1993
25 * -
26 * Copyright (c) 1985, 1988, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 4. Neither the name of the University nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * -
53 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
55 * Permission to use, copy, modify, and distribute this software for any
56 * purpose with or without fee is hereby granted, provided that the above
57 * copyright notice and this permission notice appear in all copies, and that
58 * the name of Digital Equipment Corporation not be used in advertising or
59 * publicity pertaining to distribution of the document or software without
60 * specific, written prior permission.
62 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
63 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
64 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
65 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
66 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
67 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
68 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
69 * SOFTWARE.
70 * -
71 * --Copyright--
74 #include <assert.h>
75 #include <ctype.h>
76 #include <errno.h>
77 #include <netdb.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <stddef.h>
81 #include <string.h>
82 #include <sys/syslog.h>
84 #include "nsswitch.h"
86 /* Get implementation for some internal functions. */
87 #include <resolv/mapv4v6addr.h>
88 #include <resolv/mapv4v6hostent.h>
90 #define RESOLVSORT
92 /* Maximum number of aliases we allow. */
93 #define MAX_NR_ALIASES 48
94 #define MAX_NR_ADDRS 48
96 #if PACKETSZ > 65536
97 # define MAXPACKET PACKETSZ
98 #else
99 # define MAXPACKET 65536
100 #endif
101 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
102 #ifdef MAXHOSTNAMELEN
103 # undef MAXHOSTNAMELEN
104 #endif
105 #define MAXHOSTNAMELEN 256
107 static const char AskedForGot[] = "\
108 gethostby*.getanswer: asked for \"%s\", got \"%s\"";
111 /* We need this time later. */
112 typedef union querybuf
114 HEADER hdr;
115 u_char buf[MAXPACKET];
116 } querybuf;
118 /* These functions are defined in res_comp.c. */
119 #define NS_MAXCDNAME 255 /* maximum compressed domain name */
120 extern int __ns_name_ntop (const u_char *, char *, size_t);
121 extern int __ns_name_unpack (const u_char *, const u_char *,
122 const u_char *, u_char *, size_t);
125 static enum nss_status getanswer_r (const querybuf *answer, int anslen,
126 const char *qname, int qtype,
127 struct hostent *result, char *buffer,
128 size_t buflen, int *errnop, int *h_errnop,
129 int map, int32_t *ttlp, char **canonp);
131 static enum nss_status gaih_getanswer (const querybuf *answer1, int anslen1,
132 const querybuf *answer2, int anslen2,
133 const char *qname,
134 struct gaih_addrtuple **pat,
135 char *buffer, size_t buflen,
136 int *errnop, int *h_errnop,
137 int32_t *ttlp);
139 extern enum nss_status _nss_dns_gethostbyname3_r (const char *name, int af,
140 struct hostent *result,
141 char *buffer, size_t buflen,
142 int *errnop, int *h_errnop,
143 int32_t *ttlp,
144 char **canonp);
145 hidden_proto (_nss_dns_gethostbyname3_r)
147 enum nss_status
148 _nss_dns_gethostbyname3_r (const char *name, int af, struct hostent *result,
149 char *buffer, size_t buflen, int *errnop,
150 int *h_errnop, int32_t *ttlp, char **canonp)
152 union
154 querybuf *buf;
155 u_char *ptr;
156 } host_buffer;
157 querybuf *orig_host_buffer;
158 char tmp[NS_MAXDNAME];
159 int size, type, n;
160 const char *cp;
161 int map = 0;
162 int olderr = errno;
163 enum nss_status status;
165 if (__res_maybe_init (&_res, 0) == -1)
166 return NSS_STATUS_UNAVAIL;
168 switch (af) {
169 case AF_INET:
170 size = INADDRSZ;
171 type = T_A;
172 break;
173 case AF_INET6:
174 size = IN6ADDRSZ;
175 type = T_AAAA;
176 break;
177 default:
178 *h_errnop = NO_DATA;
179 *errnop = EAFNOSUPPORT;
180 return NSS_STATUS_UNAVAIL;
183 result->h_addrtype = af;
184 result->h_length = size;
187 * if there aren't any dots, it could be a user-level alias.
188 * this is also done in res_query() since we are not the only
189 * function that looks up host names.
191 if (strchr (name, '.') == NULL
192 && (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL)
193 name = cp;
195 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
197 n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer.buf->buf,
198 1024, &host_buffer.ptr, NULL, NULL, NULL);
199 if (n < 0)
201 switch (errno)
203 case ESRCH:
204 status = NSS_STATUS_TRYAGAIN;
205 h_errno = TRY_AGAIN;
206 break;
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 union
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;
310 int resplen2 = 0;
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,
316 &ans2p, &nans2p, &resplen2);
317 if (n < 0)
319 if (errno == ESRCH)
321 status = NSS_STATUS_TRYAGAIN;
322 h_errno = TRY_AGAIN;
324 else
325 status = (errno == ECONNREFUSED
326 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND);
327 *herrnop = h_errno;
328 if (h_errno == TRY_AGAIN)
329 *errnop = EAGAIN;
330 else
331 __set_errno (olderr);
333 if (host_buffer.buf != orig_host_buffer)
334 free (host_buffer.buf);
336 return status;
339 status = gaih_getanswer(host_buffer.buf, n, (const querybuf *) ans2p,
340 resplen2, name, pat, buffer, buflen,
341 errnop, herrnop, ttlp);
343 if (host_buffer.buf != orig_host_buffer)
344 free (host_buffer.buf);
346 return status;
350 extern enum nss_status _nss_dns_gethostbyaddr2_r (const void *addr,
351 socklen_t len, int af,
352 struct hostent *result,
353 char *buffer, size_t buflen,
354 int *errnop, int *h_errnop,
355 int32_t *ttlp);
356 hidden_proto (_nss_dns_gethostbyaddr2_r)
358 enum nss_status
359 _nss_dns_gethostbyaddr2_r (const void *addr, socklen_t len, int af,
360 struct hostent *result, char *buffer, size_t buflen,
361 int *errnop, int *h_errnop, int32_t *ttlp)
363 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
364 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
365 static const u_char v6local[] = { 0,0, 0,1 };
366 const u_char *uaddr = (const u_char *)addr;
367 struct host_data
369 char *aliases[MAX_NR_ALIASES];
370 unsigned char host_addr[16]; /* IPv4 or IPv6 */
371 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
372 char linebuffer[0];
373 } *host_data = (struct host_data *) buffer;
374 union
376 querybuf *buf;
377 u_char *ptr;
378 } host_buffer;
379 querybuf *orig_host_buffer;
380 char qbuf[MAXDNAME+1], *qp = NULL;
381 size_t size;
382 int n, status;
383 int olderr = errno;
385 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct host_data);
386 buffer += pad;
387 buflen = buflen > pad ? buflen - pad : 0;
389 if (__builtin_expect (buflen < sizeof (struct host_data), 0))
391 *errnop = ERANGE;
392 *h_errnop = NETDB_INTERNAL;
393 return NSS_STATUS_TRYAGAIN;
396 host_data = (struct host_data *) buffer;
398 if (__res_maybe_init (&_res, 0) == -1)
399 return NSS_STATUS_UNAVAIL;
401 if (af == AF_INET6 && len == IN6ADDRSZ
402 && (memcmp (uaddr, mapped, sizeof mapped) == 0
403 || (memcmp (uaddr, tunnelled, sizeof tunnelled) == 0
404 && memcmp (&uaddr[sizeof tunnelled], v6local, sizeof v6local))))
406 /* Unmap. */
407 addr += sizeof mapped;
408 uaddr += sizeof mapped;
409 af = AF_INET;
410 len = INADDRSZ;
413 switch (af)
415 case AF_INET:
416 size = INADDRSZ;
417 break;
418 case AF_INET6:
419 size = IN6ADDRSZ;
420 break;
421 default:
422 *errnop = EAFNOSUPPORT;
423 *h_errnop = NETDB_INTERNAL;
424 return NSS_STATUS_UNAVAIL;
426 if (size > len)
428 *errnop = EAFNOSUPPORT;
429 *h_errnop = NETDB_INTERNAL;
430 return NSS_STATUS_UNAVAIL;
433 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
435 switch (af)
437 case AF_INET:
438 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
439 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
440 break;
441 case AF_INET6:
442 /* Only lookup with the byte string format if the user wants it. */
443 if (__builtin_expect (_res.options & RES_USEBSTRING, 0))
445 qp = stpcpy (qbuf, "\\[x");
446 for (n = 0; n < IN6ADDRSZ; ++n)
447 qp += sprintf (qp, "%02hhx", uaddr[n]);
448 strcpy (qp, "].ip6.arpa");
449 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR,
450 host_buffer.buf->buf, 1024, &host_buffer.ptr,
451 NULL, NULL, NULL);
452 if (n >= 0)
453 goto got_it_already;
455 qp = qbuf;
456 for (n = IN6ADDRSZ - 1; n >= 0; n--)
458 static const char nibblechar[16] = "0123456789abcdef";
459 *qp++ = nibblechar[uaddr[n] & 0xf];
460 *qp++ = '.';
461 *qp++ = nibblechar[(uaddr[n] >> 4) & 0xf];
462 *qp++ = '.';
464 strcpy(qp, "ip6.arpa");
465 break;
466 default:
467 /* Cannot happen. */
468 break;
471 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
472 1024, &host_buffer.ptr, NULL, NULL, NULL);
473 if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0)
475 strcpy (qp, "ip6.int");
476 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
477 host_buffer.buf != orig_host_buffer
478 ? MAXPACKET : 1024, &host_buffer.ptr,
479 NULL, NULL, NULL);
481 if (n < 0)
483 *h_errnop = h_errno;
484 __set_errno (olderr);
485 if (host_buffer.buf != orig_host_buffer)
486 free (host_buffer.buf);
487 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
490 got_it_already:
491 status = getanswer_r (host_buffer.buf, n, qbuf, T_PTR, result, buffer, buflen,
492 errnop, h_errnop, 0 /* XXX */, ttlp, NULL);
493 if (host_buffer.buf != orig_host_buffer)
494 free (host_buffer.buf);
495 if (status != NSS_STATUS_SUCCESS)
496 return status;
498 #ifdef SUNSECURITY
499 This is not implemented because it is not possible to use the current
500 source from bind in a multi-threaded program.
501 #endif
503 result->h_addrtype = af;
504 result->h_length = len;
505 memcpy (host_data->host_addr, addr, len);
506 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
507 host_data->h_addr_ptrs[1] = NULL;
508 #if 0
509 /* XXX I think this is wrong. Why should an IPv4 address be
510 converted to IPv6 if the user explicitly asked for IPv4? */
511 if (af == AF_INET && (_res.options & RES_USE_INET6))
513 map_v4v6_address ((char *) host_data->host_addr,
514 (char *) host_data->host_addr);
515 result->h_addrtype = AF_INET6;
516 result->h_length = IN6ADDRSZ;
518 #endif
519 *h_errnop = NETDB_SUCCESS;
520 return NSS_STATUS_SUCCESS;
522 hidden_def (_nss_dns_gethostbyaddr2_r)
525 enum nss_status
526 _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
527 struct hostent *result, char *buffer, size_t buflen,
528 int *errnop, int *h_errnop)
530 return _nss_dns_gethostbyaddr2_r (addr, len, af, result, buffer, buflen,
531 errnop, h_errnop, NULL);
534 #ifdef RESOLVSORT
535 static void addrsort (char **ap, int num);
537 static void
538 addrsort (char **ap, int num)
540 int i, j;
541 char **p;
542 short aval[MAX_NR_ADDRS];
543 int needsort = 0;
545 p = ap;
546 if (num > MAX_NR_ADDRS)
547 num = MAX_NR_ADDRS;
548 for (i = 0; i < num; i++, p++)
550 for (j = 0 ; (unsigned)j < _res.nsort; j++)
551 if (_res.sort_list[j].addr.s_addr ==
552 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
553 break;
554 aval[i] = j;
555 if (needsort == 0 && i > 0 && j < aval[i-1])
556 needsort = i;
558 if (!needsort)
559 return;
561 while (needsort++ < num)
562 for (j = needsort - 2; j >= 0; j--)
563 if (aval[j] > aval[j+1])
565 char *hp;
567 i = aval[j];
568 aval[j] = aval[j+1];
569 aval[j+1] = i;
571 hp = ap[j];
572 ap[j] = ap[j+1];
573 ap[j+1] = hp;
575 else
576 break;
578 #endif
580 static enum nss_status
581 getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
582 struct hostent *result, char *buffer, size_t buflen,
583 int *errnop, int *h_errnop, int map, int32_t *ttlp, char **canonp)
585 struct host_data
587 char *aliases[MAX_NR_ALIASES];
588 unsigned char host_addr[16]; /* IPv4 or IPv6 */
589 char *h_addr_ptrs[0];
590 } *host_data;
591 int linebuflen;
592 register const HEADER *hp;
593 const u_char *end_of_message, *cp;
594 int n, ancount, qdcount;
595 int haveanswer, had_error;
596 char *bp, **ap, **hap;
597 char tbuf[MAXDNAME];
598 const char *tname;
599 int (*name_ok) (const char *);
600 u_char packtmp[NS_MAXCDNAME];
601 int have_to_map = 0;
602 int32_t ttl = 0;
603 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct host_data);
604 buffer += pad;
605 if (__builtin_expect (buflen < sizeof (struct host_data) + pad, 0))
607 /* The buffer is too small. */
608 too_small:
609 *errnop = ERANGE;
610 *h_errnop = NETDB_INTERNAL;
611 return NSS_STATUS_TRYAGAIN;
613 host_data = (struct host_data *) buffer;
614 linebuflen = buflen - sizeof (struct host_data);
615 if (buflen - sizeof (struct host_data) != linebuflen)
616 linebuflen = INT_MAX;
618 tname = qname;
619 result->h_name = NULL;
620 end_of_message = answer->buf + anslen;
621 switch (qtype)
623 case T_A:
624 case T_AAAA:
625 name_ok = res_hnok;
626 break;
627 case T_PTR:
628 name_ok = res_dnok;
629 break;
630 default:
631 *errnop = ENOENT;
632 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
636 * find first satisfactory answer
638 hp = &answer->hdr;
639 ancount = ntohs (hp->ancount);
640 qdcount = ntohs (hp->qdcount);
641 cp = answer->buf + HFIXEDSZ;
642 if (__builtin_expect (qdcount, 1) != 1)
644 *h_errnop = NO_RECOVERY;
645 return NSS_STATUS_UNAVAIL;
647 if (sizeof (struct host_data) + (ancount + 1) * sizeof (char *) >= buflen)
648 goto too_small;
649 bp = (char *) &host_data->h_addr_ptrs[ancount + 1];
650 linebuflen -= (ancount + 1) * sizeof (char *);
652 n = __ns_name_unpack (answer->buf, end_of_message, cp,
653 packtmp, sizeof packtmp);
654 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
656 if (__builtin_expect (errno, 0) == EMSGSIZE)
657 goto too_small;
659 n = -1;
662 if (n > 0 && bp[0] == '.')
663 bp[0] = '\0';
665 if (__builtin_expect (n < 0 || ((*name_ok) (bp) == 0 && (errno = EBADMSG)),
668 *errnop = errno;
669 *h_errnop = NO_RECOVERY;
670 return NSS_STATUS_UNAVAIL;
672 cp += n + QFIXEDSZ;
674 if (qtype == T_A || qtype == T_AAAA)
676 /* res_send() has already verified that the query name is the
677 * same as the one we sent; this just gets the expanded name
678 * (i.e., with the succeeding search-domain tacked on).
680 n = strlen (bp) + 1; /* for the \0 */
681 if (n >= MAXHOSTNAMELEN)
683 *h_errnop = NO_RECOVERY;
684 *errnop = ENOENT;
685 return NSS_STATUS_TRYAGAIN;
687 result->h_name = bp;
688 bp += n;
689 linebuflen -= n;
690 if (linebuflen < 0)
691 goto too_small;
692 /* The qname can be abbreviated, but h_name is now absolute. */
693 qname = result->h_name;
696 ap = host_data->aliases;
697 *ap = NULL;
698 result->h_aliases = host_data->aliases;
699 hap = host_data->h_addr_ptrs;
700 *hap = NULL;
701 result->h_addr_list = host_data->h_addr_ptrs;
702 haveanswer = 0;
703 had_error = 0;
705 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
707 int type, class;
709 n = __ns_name_unpack (answer->buf, end_of_message, cp,
710 packtmp, sizeof packtmp);
711 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
713 if (__builtin_expect (errno, 0) == EMSGSIZE)
714 goto too_small;
716 n = -1;
719 if (__builtin_expect (n < 0 || (*name_ok) (bp) == 0, 0))
721 ++had_error;
722 continue;
724 cp += n; /* name */
726 if (__builtin_expect (cp + 10 > end_of_message, 0))
728 ++had_error;
729 continue;
732 type = __ns_get16 (cp);
733 cp += INT16SZ; /* type */
734 class = __ns_get16 (cp);
735 cp += INT16SZ; /* class */
736 ttl = __ns_get32 (cp);
737 cp += INT32SZ; /* TTL */
738 n = __ns_get16 (cp);
739 cp += INT16SZ; /* len */
740 if (__builtin_expect (class != C_IN, 0))
742 /* XXX - debug? syslog? */
743 cp += n;
744 continue; /* XXX - had_error++ ? */
747 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME)
749 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
750 continue;
751 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
752 if (__builtin_expect (n < 0 || (*name_ok) (tbuf) == 0, 0))
754 ++had_error;
755 continue;
757 cp += n;
758 /* Store alias. */
759 *ap++ = bp;
760 n = strlen (bp) + 1; /* For the \0. */
761 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
763 ++had_error;
764 continue;
766 bp += n;
767 linebuflen -= n;
768 /* Get canonical name. */
769 n = strlen (tbuf) + 1; /* For the \0. */
770 if (__builtin_expect (n > linebuflen, 0))
771 goto too_small;
772 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
774 ++had_error;
775 continue;
777 result->h_name = bp;
778 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
779 linebuflen -= n;
780 continue;
783 if (qtype == T_PTR && type == T_CNAME)
785 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
786 if (__builtin_expect (n < 0 || res_dnok (tbuf) == 0, 0))
788 ++had_error;
789 continue;
791 cp += n;
792 /* Get canonical name. */
793 n = strlen (tbuf) + 1; /* For the \0. */
794 if (__builtin_expect (n > linebuflen, 0))
795 goto too_small;
796 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
798 ++had_error;
799 continue;
801 tname = bp;
802 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
803 linebuflen -= n;
804 continue;
806 if (__builtin_expect (type == T_SIG, 0)
807 || __builtin_expect (type == T_KEY, 0)
808 || __builtin_expect (type == T_NXT, 0))
810 /* We don't support DNSSEC yet. For now, ignore the record
811 and send a low priority message to syslog. */
812 syslog (LOG_DEBUG | LOG_AUTH,
813 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
814 qname, p_class (C_IN), p_type(qtype), p_type (type));
815 cp += n;
816 continue;
819 if (type == T_A && qtype == T_AAAA && map)
820 have_to_map = 1;
821 else if (__builtin_expect (type != qtype, 0))
823 syslog (LOG_NOTICE | LOG_AUTH,
824 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
825 qname, p_class (C_IN), p_type (qtype), p_type (type));
826 cp += n;
827 continue; /* XXX - had_error++ ? */
830 switch (type)
832 case T_PTR:
833 if (__builtin_expect (strcasecmp (tname, bp) != 0, 0))
835 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
836 cp += n;
837 continue; /* XXX - had_error++ ? */
840 n = __ns_name_unpack (answer->buf, end_of_message, cp,
841 packtmp, sizeof packtmp);
842 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
844 if (__builtin_expect (errno, 0) == EMSGSIZE)
845 goto too_small;
847 n = -1;
850 if (__builtin_expect (n < 0 || res_hnok (bp) == 0, 0))
852 ++had_error;
853 break;
855 #if MULTI_PTRS_ARE_ALIASES
856 cp += n;
857 if (haveanswer == 0)
858 result->h_name = bp;
859 else if (ap < &host_data->aliases[MAXALIASES-1])
860 *ap++ = bp;
861 else
862 n = -1;
863 if (n != -1)
865 n = strlen (bp) + 1; /* for the \0 */
866 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
868 ++had_error;
869 break;
871 bp += n;
872 linebuflen -= n;
874 break;
875 #else
876 result->h_name = bp;
877 if (have_to_map)
879 n = strlen (bp) + 1; /* for the \0 */
880 if (__builtin_expect (n >= MAXHOSTNAMELEN, 0))
882 ++had_error;
883 break;
885 bp += n;
886 linebuflen -= n;
887 if (map_v4v6_hostent (result, &bp, &linebuflen))
888 goto too_small;
890 *h_errnop = NETDB_SUCCESS;
891 return NSS_STATUS_SUCCESS;
892 #endif
893 case T_A:
894 case T_AAAA:
895 if (__builtin_expect (strcasecmp (result->h_name, bp), 0) != 0)
897 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
898 cp += n;
899 continue; /* XXX - had_error++ ? */
901 if (n != result->h_length)
903 cp += n;
904 continue;
906 if (!haveanswer)
908 register int nn;
910 if (ttlp != NULL && ttl != 0)
911 *ttlp = ttl;
912 if (canonp != NULL)
913 *canonp = bp;
914 result->h_name = bp;
915 nn = strlen (bp) + 1; /* for the \0 */
916 bp += nn;
917 linebuflen -= nn;
920 linebuflen -= sizeof (align) - ((u_long) bp % sizeof (align));
921 bp += sizeof (align) - ((u_long) bp % sizeof (align));
923 if (__builtin_expect (n > linebuflen, 0))
924 goto too_small;
925 bp = __mempcpy (*hap++ = bp, cp, n);
926 cp += n;
927 linebuflen -= n;
928 break;
929 default:
930 abort ();
932 if (had_error == 0)
933 ++haveanswer;
936 if (haveanswer > 0)
938 *ap = NULL;
939 *hap = NULL;
940 #if defined RESOLVSORT
942 * Note: we sort even if host can take only one address
943 * in its return structures - should give it the "best"
944 * address in that case, not some random one
946 if (_res.nsort && haveanswer > 1 && qtype == T_A)
947 addrsort (host_data->h_addr_ptrs, haveanswer);
948 #endif /*RESOLVSORT*/
950 if (result->h_name == NULL)
952 n = strlen (qname) + 1; /* For the \0. */
953 if (n > linebuflen)
954 goto too_small;
955 if (n >= MAXHOSTNAMELEN)
956 goto no_recovery;
957 result->h_name = bp;
958 bp = __mempcpy (bp, qname, n); /* Cannot overflow. */
959 linebuflen -= n;
962 if (have_to_map)
963 if (map_v4v6_hostent (result, &bp, &linebuflen))
964 goto too_small;
965 *h_errnop = NETDB_SUCCESS;
966 return NSS_STATUS_SUCCESS;
968 no_recovery:
969 *h_errnop = NO_RECOVERY;
970 *errnop = ENOENT;
971 /* Special case here: if the resolver sent a result but it only
972 contains a CNAME while we are looking for a T_A or T_AAAA record,
973 we fail with NOTFOUND instead of TRYAGAIN. */
974 return ((qtype == T_A || qtype == T_AAAA) && ap != host_data->aliases
975 ? NSS_STATUS_NOTFOUND : NSS_STATUS_TRYAGAIN);
979 static enum nss_status
980 gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname,
981 struct gaih_addrtuple ***patp,
982 char **bufferp, size_t *buflenp,
983 int *errnop, int *h_errnop, int32_t *ttlp, int *firstp)
985 char *buffer = *bufferp;
986 size_t buflen = *buflenp;
988 struct gaih_addrtuple **pat = *patp;
989 const HEADER *hp = &answer->hdr;
990 int ancount = ntohs (hp->ancount);
991 int qdcount = ntohs (hp->qdcount);
992 const u_char *cp = answer->buf + HFIXEDSZ;
993 const u_char *end_of_message = answer->buf + anslen;
994 if (__builtin_expect (qdcount != 1, 0))
996 *h_errnop = NO_RECOVERY;
997 return NSS_STATUS_UNAVAIL;
1000 u_char packtmp[NS_MAXCDNAME];
1001 int n = __ns_name_unpack (answer->buf, end_of_message, cp,
1002 packtmp, sizeof packtmp);
1003 /* We unpack the name to check it for validity. But we do not need
1004 it later. */
1005 if (n != -1 && __ns_name_ntop (packtmp, buffer, buflen) == -1)
1007 if (__builtin_expect (errno, 0) == EMSGSIZE)
1009 too_small:
1010 *errnop = ERANGE;
1011 *h_errnop = NETDB_INTERNAL;
1012 return NSS_STATUS_TRYAGAIN;
1015 n = -1;
1018 if (__builtin_expect (n < 0 || (res_hnok (buffer) == 0
1019 && (errno = EBADMSG)), 0))
1021 *errnop = errno;
1022 *h_errnop = NO_RECOVERY;
1023 return NSS_STATUS_UNAVAIL;
1025 cp += n + QFIXEDSZ;
1027 int haveanswer = 0;
1028 int had_error = 0;
1029 char *canon = NULL;
1030 char *h_name = NULL;
1031 int h_namelen = 0;
1033 if (ancount == 0)
1034 return NSS_STATUS_NOTFOUND;
1036 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
1038 n = __ns_name_unpack (answer->buf, end_of_message, cp,
1039 packtmp, sizeof packtmp);
1040 if (n != -1 &&
1041 (h_namelen = __ns_name_ntop (packtmp, buffer, buflen)) == -1)
1043 if (__builtin_expect (errno, 0) == EMSGSIZE)
1044 goto too_small;
1046 n = -1;
1048 if (__builtin_expect (n < 0 || res_hnok (buffer) == 0, 0))
1050 ++had_error;
1051 continue;
1053 if (*firstp)
1055 h_name = buffer;
1056 buffer += h_namelen;
1057 buflen -= h_namelen;
1060 cp += n; /* name */
1062 if (__builtin_expect (cp + 10 > end_of_message, 0))
1064 ++had_error;
1065 continue;
1068 int type = __ns_get16 (cp);
1069 cp += INT16SZ; /* type */
1070 int class = __ns_get16 (cp);
1071 cp += INT16SZ; /* class */
1072 int32_t ttl = __ns_get32 (cp);
1073 cp += INT32SZ; /* TTL */
1074 n = __ns_get16 (cp);
1075 cp += INT16SZ; /* len */
1077 if (class != C_IN)
1079 cp += n;
1080 continue;
1083 if (type == T_CNAME)
1085 char tbuf[MAXDNAME];
1086 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
1087 if (__builtin_expect (n < 0 || res_hnok (tbuf) == 0, 0))
1089 ++had_error;
1090 continue;
1092 cp += n;
1094 if (*firstp)
1096 /* Reclaim buffer space. */
1097 if (h_name + h_namelen == buffer)
1099 buffer = h_name;
1100 buflen += h_namelen;
1103 n = strlen (tbuf) + 1;
1104 if (__builtin_expect (n > buflen, 0))
1105 goto too_small;
1106 if (__builtin_expect (n >= MAXHOSTNAMELEN, 0))
1108 ++had_error;
1109 continue;
1112 canon = buffer;
1113 buffer = __mempcpy (buffer, tbuf, n);
1114 buflen -= n;
1115 h_namelen = 0;
1117 continue;
1119 #if 1
1120 // We should not see any types other than those explicitly listed
1121 // below. Some types sent by server seem missing, though. Just
1122 // collect the data for now.
1123 if (__builtin_expect (type != T_A && type != T_AAAA, 0))
1124 #else
1125 if (__builtin_expect (type == T_SIG, 0)
1126 || __builtin_expect (type == T_KEY, 0)
1127 || __builtin_expect (type == T_NXT, 0)
1128 || __builtin_expect (type == T_PTR, 0)
1129 || __builtin_expect (type == T_DNAME, 0))
1130 #endif
1132 /* We don't support DNSSEC yet. For now, ignore the record
1133 and send a low priority message to syslog.
1135 We also don't expect T_PTR or T_DNAME messages. */
1136 syslog (LOG_DEBUG | LOG_AUTH,
1137 "getaddrinfo*.gaih_getanswer: got type \"%s\"",
1138 p_type (type));
1139 cp += n;
1140 continue;
1142 if (type != T_A && type != T_AAAA)
1143 abort ();
1145 if (*pat == NULL)
1147 uintptr_t pad = (-(uintptr_t) buffer
1148 % __alignof__ (struct gaih_addrtuple));
1149 buffer += pad;
1150 buflen = buflen > pad ? buflen - pad : 0;
1152 if (__builtin_expect (buflen < sizeof (struct gaih_addrtuple),
1154 goto too_small;
1156 *pat = (struct gaih_addrtuple *) buffer;
1157 buffer += sizeof (struct gaih_addrtuple);
1158 buflen -= sizeof (struct gaih_addrtuple);
1161 (*pat)->name = NULL;
1162 (*pat)->next = NULL;
1164 if (*firstp)
1166 if (ttl != 0 && ttlp != NULL)
1167 *ttlp = ttl;
1169 if (canon != NULL)
1171 (*pat)->name = canon;
1173 /* Reclaim buffer space. */
1174 if (h_name + h_namelen == buffer)
1176 buffer = h_name;
1177 buflen += h_namelen;
1180 else
1181 (*pat)->name = h_name;
1183 *firstp = 0;
1186 (*pat)->family = type == T_A ? AF_INET : AF_INET6;
1187 if (__builtin_expect ((type == T_A && n != INADDRSZ)
1188 || (type == T_AAAA && n != IN6ADDRSZ), 0))
1190 ++had_error;
1191 continue;
1193 memcpy ((*pat)->addr, cp, n);
1194 cp += n;
1195 (*pat)->scopeid = 0;
1197 pat = &((*pat)->next);
1199 haveanswer = 1;
1202 if (haveanswer)
1204 *patp = pat;
1205 *bufferp = buffer;
1206 *buflenp = buflen;
1208 *h_errnop = NETDB_SUCCESS;
1209 return NSS_STATUS_SUCCESS;
1212 /* Special case here: if the resolver sent a result but it only
1213 contains a CNAME while we are looking for a T_A or T_AAAA record,
1214 we fail with NOTFOUND instead of TRYAGAIN. */
1215 return canon == NULL ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
1219 static enum nss_status
1220 gaih_getanswer (const querybuf *answer1, int anslen1, const querybuf *answer2,
1221 int anslen2, const char *qname,
1222 struct gaih_addrtuple **pat, char *buffer, size_t buflen,
1223 int *errnop, int *h_errnop, int32_t *ttlp)
1225 int first = 1;
1227 enum nss_status status = NSS_STATUS_NOTFOUND;
1229 if (anslen1 > 0)
1230 status = gaih_getanswer_slice(answer1, anslen1, qname,
1231 &pat, &buffer, &buflen,
1232 errnop, h_errnop, ttlp,
1233 &first);
1234 if ((status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND
1235 || (status == NSS_STATUS_TRYAGAIN
1236 && (errno != ERANGE || *h_errnop != NO_RECOVERY)))
1237 && answer2 != NULL && anslen2 > 0)
1239 enum nss_status status2 = gaih_getanswer_slice(answer2, anslen2, qname,
1240 &pat, &buffer, &buflen,
1241 errnop, h_errnop, ttlp,
1242 &first);
1243 if (status != NSS_STATUS_SUCCESS && status2 != NSS_STATUS_NOTFOUND)
1244 status = status2;
1247 return status;