]> git.ipfire.org Git - thirdparty/glibc.git/blob - resolv/nss_dns/dns-host.c
* resolv/nss_dns/dns-host.c (gaih_getanswer_slice): Also log and
[thirdparty/glibc.git] / resolv / nss_dns / dns-host.c
1 /* Copyright (C) 1996-2004, 2007, 2008 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.
4
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.
9
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.
14
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. */
19
20 /* Parts of this file are plain copies of the file `gethtnamadr.c' from
21 the bind package and it has the following copyright. */
22
23 /*
24 * ++Copyright++ 1985, 1988, 1993
25 * -
26 * Copyright (c) 1985, 1988, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
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.
40 *
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.
54 *
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.
61 *
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--
72 */
73
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>
83
84 #include "nsswitch.h"
85
86 /* Get implementation for some internal functions. */
87 #include <resolv/mapv4v6addr.h>
88 #include <resolv/mapv4v6hostent.h>
89
90 #define RESOLVSORT
91
92 /* Maximum number of aliases we allow. */
93 #define MAX_NR_ALIASES 48
94 #define MAX_NR_ADDRS 48
95
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
106
107 static const char AskedForGot[] = "\
108 gethostby*.getanswer: asked for \"%s\", got \"%s\"";
109
110
111 /* We need this time later. */
112 typedef union querybuf
113 {
114 HEADER hdr;
115 u_char buf[MAXPACKET];
116 } querybuf;
117
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);
123
124
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);
130
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);
138
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)
146
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)
151 {
152 union
153 {
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;
164
165 if (__res_maybe_init (&_res, 0) == -1)
166 return NSS_STATUS_UNAVAIL;
167
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;
181 }
182
183 result->h_addrtype = af;
184 result->h_length = size;
185
186 /*
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.
190 */
191 if (strchr (name, '.') == NULL
192 && (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL)
193 name = cp;
194
195 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
196
197 n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer.buf->buf,
198 1024, &host_buffer.ptr, NULL, NULL);
199 if (n < 0)
200 {
201 status = (errno == ECONNREFUSED
202 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND);
203 *h_errnop = h_errno;
204 if (h_errno == TRY_AGAIN)
205 *errnop = EAGAIN;
206 else
207 __set_errno (olderr);
208
209 /* If we are looking for a IPv6 address and mapping is enabled
210 by having the RES_USE_INET6 bit in _res.options set, we try
211 another lookup. */
212 if (af == AF_INET6 && (_res.options & RES_USE_INET6))
213 n = __libc_res_nsearch (&_res, name, C_IN, T_A, host_buffer.buf->buf,
214 host_buffer.buf != orig_host_buffer
215 ? MAXPACKET : 1024, &host_buffer.ptr,
216 NULL, NULL);
217
218 if (n < 0)
219 {
220 if (host_buffer.buf != orig_host_buffer)
221 free (host_buffer.buf);
222 return status;
223 }
224
225 map = 1;
226
227 result->h_addrtype = AF_INET;
228 result->h_length = INADDRSZ;
229 }
230
231 status = getanswer_r (host_buffer.buf, n, name, type, result, buffer, buflen,
232 errnop, h_errnop, map, ttlp, canonp);
233 if (host_buffer.buf != orig_host_buffer)
234 free (host_buffer.buf);
235 return status;
236 }
237 hidden_def (_nss_dns_gethostbyname3_r)
238
239
240 enum nss_status
241 _nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
242 char *buffer, size_t buflen, int *errnop,
243 int *h_errnop)
244 {
245 return _nss_dns_gethostbyname3_r (name, af, result, buffer, buflen, errnop,
246 h_errnop, NULL, NULL);
247 }
248
249
250 enum nss_status
251 _nss_dns_gethostbyname_r (const char *name, struct hostent *result,
252 char *buffer, size_t buflen, int *errnop,
253 int *h_errnop)
254 {
255 enum nss_status status = NSS_STATUS_NOTFOUND;
256
257 if (_res.options & RES_USE_INET6)
258 status = _nss_dns_gethostbyname3_r (name, AF_INET6, result, buffer,
259 buflen, errnop, h_errnop, NULL, NULL);
260 if (status == NSS_STATUS_NOTFOUND)
261 status = _nss_dns_gethostbyname3_r (name, AF_INET, result, buffer,
262 buflen, errnop, h_errnop, NULL, NULL);
263
264 return status;
265 }
266
267
268 enum nss_status
269 _nss_dns_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
270 char *buffer, size_t buflen, int *errnop,
271 int *herrnop, int32_t *ttlp)
272 {
273 if (__res_maybe_init (&_res, 0) == -1)
274 return NSS_STATUS_UNAVAIL;
275
276 char tmp[NS_MAXDNAME];
277
278 /*
279 * if there aren't any dots, it could be a user-level alias.
280 * this is also done in res_query() since we are not the only
281 * function that looks up host names.
282 */
283 if (strchr (name, '.') == NULL)
284 {
285 const char *cp = res_hostalias (&_res, name, tmp, sizeof (tmp));
286 if (cp != NULL)
287 name = cp;
288 }
289
290 union
291 {
292 querybuf *buf;
293 u_char *ptr;
294 } host_buffer;
295 querybuf *orig_host_buffer;
296 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (2048);
297 u_char *ans2p = NULL;
298 int nans2p = 0;
299
300 int olderr = errno;
301 enum nss_status status;
302 int n = __libc_res_nsearch (&_res, name, C_IN, T_UNSPEC,
303 host_buffer.buf->buf, 2048, &host_buffer.ptr,
304 &ans2p, &nans2p);
305 if (n < 0)
306 {
307 status = (errno == ECONNREFUSED
308 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND);
309 *herrnop = h_errno;
310 if (h_errno == TRY_AGAIN)
311 *errnop = EAGAIN;
312 else
313 __set_errno (olderr);
314
315 if (host_buffer.buf != orig_host_buffer)
316 free (host_buffer.buf);
317
318 return status;
319 }
320
321 status = gaih_getanswer(host_buffer.buf, n, (const querybuf *) ans2p,
322 nans2p, name, pat, buffer, buflen,
323 errnop, herrnop, ttlp);
324
325 if (host_buffer.buf != orig_host_buffer)
326 free (host_buffer.buf);
327
328 return status;
329 }
330
331
332 extern enum nss_status _nss_dns_gethostbyaddr2_r (const void *addr,
333 socklen_t len, int af,
334 struct hostent *result,
335 char *buffer, size_t buflen,
336 int *errnop, int *h_errnop,
337 int32_t *ttlp);
338 hidden_proto (_nss_dns_gethostbyaddr2_r)
339
340 enum nss_status
341 _nss_dns_gethostbyaddr2_r (const void *addr, socklen_t len, int af,
342 struct hostent *result, char *buffer, size_t buflen,
343 int *errnop, int *h_errnop, int32_t *ttlp)
344 {
345 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
346 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
347 static const u_char v6local[] = { 0,0, 0,1 };
348 const u_char *uaddr = (const u_char *)addr;
349 struct host_data
350 {
351 char *aliases[MAX_NR_ALIASES];
352 unsigned char host_addr[16]; /* IPv4 or IPv6 */
353 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
354 char linebuffer[0];
355 } *host_data = (struct host_data *) buffer;
356 union
357 {
358 querybuf *buf;
359 u_char *ptr;
360 } host_buffer;
361 querybuf *orig_host_buffer;
362 char qbuf[MAXDNAME+1], *qp = NULL;
363 size_t size;
364 int n, status;
365 int olderr = errno;
366
367 if (__res_maybe_init (&_res, 0) == -1)
368 return NSS_STATUS_UNAVAIL;
369
370 if (af == AF_INET6 && len == IN6ADDRSZ
371 && (memcmp (uaddr, mapped, sizeof mapped) == 0
372 || (memcmp (uaddr, tunnelled, sizeof tunnelled) == 0
373 && memcmp (&uaddr[sizeof tunnelled], v6local, sizeof v6local))))
374 {
375 /* Unmap. */
376 addr += sizeof mapped;
377 uaddr += sizeof mapped;
378 af = AF_INET;
379 len = INADDRSZ;
380 }
381
382 switch (af)
383 {
384 case AF_INET:
385 size = INADDRSZ;
386 break;
387 case AF_INET6:
388 size = IN6ADDRSZ;
389 break;
390 default:
391 *errnop = EAFNOSUPPORT;
392 *h_errnop = NETDB_INTERNAL;
393 return NSS_STATUS_UNAVAIL;
394 }
395 if (size > len)
396 {
397 *errnop = EAFNOSUPPORT;
398 *h_errnop = NETDB_INTERNAL;
399 return NSS_STATUS_UNAVAIL;
400 }
401
402 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
403
404 switch (af)
405 {
406 case AF_INET:
407 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
408 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
409 break;
410 case AF_INET6:
411 /* Only lookup with the byte string format if the user wants it. */
412 if (__builtin_expect (_res.options & RES_USEBSTRING, 0))
413 {
414 qp = stpcpy (qbuf, "\\[x");
415 for (n = 0; n < IN6ADDRSZ; ++n)
416 qp += sprintf (qp, "%02hhx", uaddr[n]);
417 strcpy (qp, "].ip6.arpa");
418 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR,
419 host_buffer.buf->buf, 1024, &host_buffer.ptr,
420 NULL, NULL);
421 if (n >= 0)
422 goto got_it_already;
423 }
424 qp = qbuf;
425 for (n = IN6ADDRSZ - 1; n >= 0; n--)
426 {
427 static const char nibblechar[16] = "0123456789abcdef";
428 *qp++ = nibblechar[uaddr[n] & 0xf];
429 *qp++ = '.';
430 *qp++ = nibblechar[(uaddr[n] >> 4) & 0xf];
431 *qp++ = '.';
432 }
433 strcpy(qp, "ip6.arpa");
434 break;
435 default:
436 /* Cannot happen. */
437 break;
438 }
439
440 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
441 1024, &host_buffer.ptr, NULL, NULL);
442 if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0)
443 {
444 strcpy (qp, "ip6.int");
445 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
446 host_buffer.buf != orig_host_buffer
447 ? MAXPACKET : 1024, &host_buffer.ptr,
448 NULL, NULL);
449 }
450 if (n < 0)
451 {
452 *h_errnop = h_errno;
453 __set_errno (olderr);
454 if (host_buffer.buf != orig_host_buffer)
455 free (host_buffer.buf);
456 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
457 }
458
459 got_it_already:
460 status = getanswer_r (host_buffer.buf, n, qbuf, T_PTR, result, buffer, buflen,
461 errnop, h_errnop, 0 /* XXX */, ttlp, NULL);
462 if (host_buffer.buf != orig_host_buffer)
463 free (host_buffer.buf);
464 if (status != NSS_STATUS_SUCCESS)
465 return status;
466
467 #ifdef SUNSECURITY
468 This is not implemented because it is not possible to use the current
469 source from bind in a multi-threaded program.
470 #endif
471
472 result->h_addrtype = af;
473 result->h_length = len;
474 memcpy (host_data->host_addr, addr, len);
475 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
476 host_data->h_addr_ptrs[1] = NULL;
477 #if 0
478 /* XXX I think this is wrong. Why should an IPv4 address be
479 converted to IPv6 if the user explicitly asked for IPv4? */
480 if (af == AF_INET && (_res.options & RES_USE_INET6))
481 {
482 map_v4v6_address ((char *) host_data->host_addr,
483 (char *) host_data->host_addr);
484 result->h_addrtype = AF_INET6;
485 result->h_length = IN6ADDRSZ;
486 }
487 #endif
488 *h_errnop = NETDB_SUCCESS;
489 return NSS_STATUS_SUCCESS;
490 }
491 hidden_def (_nss_dns_gethostbyaddr2_r)
492
493
494 enum nss_status
495 _nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
496 struct hostent *result, char *buffer, size_t buflen,
497 int *errnop, int *h_errnop)
498 {
499 return _nss_dns_gethostbyaddr2_r (addr, len, af, result, buffer, buflen,
500 errnop, h_errnop, NULL);
501 }
502
503 #ifdef RESOLVSORT
504 static void addrsort (char **ap, int num);
505
506 static void
507 addrsort (char **ap, int num)
508 {
509 int i, j;
510 char **p;
511 short aval[MAX_NR_ADDRS];
512 int needsort = 0;
513
514 p = ap;
515 if (num > MAX_NR_ADDRS)
516 num = MAX_NR_ADDRS;
517 for (i = 0; i < num; i++, p++)
518 {
519 for (j = 0 ; (unsigned)j < _res.nsort; j++)
520 if (_res.sort_list[j].addr.s_addr ==
521 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
522 break;
523 aval[i] = j;
524 if (needsort == 0 && i > 0 && j < aval[i-1])
525 needsort = i;
526 }
527 if (!needsort)
528 return;
529
530 while (needsort++ < num)
531 for (j = needsort - 2; j >= 0; j--)
532 if (aval[j] > aval[j+1])
533 {
534 char *hp;
535
536 i = aval[j];
537 aval[j] = aval[j+1];
538 aval[j+1] = i;
539
540 hp = ap[j];
541 ap[j] = ap[j+1];
542 ap[j+1] = hp;
543 }
544 else
545 break;
546 }
547 #endif
548
549 static enum nss_status
550 getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
551 struct hostent *result, char *buffer, size_t buflen,
552 int *errnop, int *h_errnop, int map, int32_t *ttlp, char **canonp)
553 {
554 struct host_data
555 {
556 char *aliases[MAX_NR_ALIASES];
557 unsigned char host_addr[16]; /* IPv4 or IPv6 */
558 char *h_addr_ptrs[0];
559 } *host_data;
560 int linebuflen;
561 register const HEADER *hp;
562 const u_char *end_of_message, *cp;
563 int n, ancount, qdcount;
564 int haveanswer, had_error;
565 char *bp, **ap, **hap;
566 char tbuf[MAXDNAME];
567 const char *tname;
568 int (*name_ok) (const char *);
569 u_char packtmp[NS_MAXCDNAME];
570 int have_to_map = 0;
571 int32_t ttl = 0;
572 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct host_data);
573 buffer += pad;
574 if (__builtin_expect (buflen < sizeof (struct host_data) + pad, 0))
575 {
576 /* The buffer is too small. */
577 too_small:
578 *errnop = ERANGE;
579 *h_errnop = NETDB_INTERNAL;
580 return NSS_STATUS_TRYAGAIN;
581 }
582 host_data = (struct host_data *) buffer;
583 linebuflen = buflen - sizeof (struct host_data);
584 if (buflen - sizeof (struct host_data) != linebuflen)
585 linebuflen = INT_MAX;
586
587 tname = qname;
588 result->h_name = NULL;
589 end_of_message = answer->buf + anslen;
590 switch (qtype)
591 {
592 case T_A:
593 case T_AAAA:
594 name_ok = res_hnok;
595 break;
596 case T_PTR:
597 name_ok = res_dnok;
598 break;
599 default:
600 *errnop = ENOENT;
601 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
602 }
603
604 /*
605 * find first satisfactory answer
606 */
607 hp = &answer->hdr;
608 ancount = ntohs (hp->ancount);
609 qdcount = ntohs (hp->qdcount);
610 cp = answer->buf + HFIXEDSZ;
611 if (__builtin_expect (qdcount, 1) != 1)
612 {
613 *h_errnop = NO_RECOVERY;
614 return NSS_STATUS_UNAVAIL;
615 }
616 if (sizeof (struct host_data) + (ancount + 1) * sizeof (char *) >= buflen)
617 goto too_small;
618 bp = (char *) &host_data->h_addr_ptrs[ancount + 1];
619 linebuflen -= (ancount + 1) * sizeof (char *);
620
621 n = __ns_name_unpack (answer->buf, end_of_message, cp,
622 packtmp, sizeof packtmp);
623 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
624 {
625 if (__builtin_expect (errno, 0) == EMSGSIZE)
626 goto too_small;
627
628 n = -1;
629 }
630
631 if (n > 0 && bp[0] == '.')
632 bp[0] = '\0';
633
634 if (__builtin_expect (n < 0 || ((*name_ok) (bp) == 0 && (errno = EBADMSG)),
635 0))
636 {
637 *errnop = errno;
638 *h_errnop = NO_RECOVERY;
639 return NSS_STATUS_UNAVAIL;
640 }
641 cp += n + QFIXEDSZ;
642
643 if (qtype == T_A || qtype == T_AAAA)
644 {
645 /* res_send() has already verified that the query name is the
646 * same as the one we sent; this just gets the expanded name
647 * (i.e., with the succeeding search-domain tacked on).
648 */
649 n = strlen (bp) + 1; /* for the \0 */
650 if (n >= MAXHOSTNAMELEN)
651 {
652 *h_errnop = NO_RECOVERY;
653 *errnop = ENOENT;
654 return NSS_STATUS_TRYAGAIN;
655 }
656 result->h_name = bp;
657 bp += n;
658 linebuflen -= n;
659 if (linebuflen < 0)
660 goto too_small;
661 /* The qname can be abbreviated, but h_name is now absolute. */
662 qname = result->h_name;
663 }
664
665 ap = host_data->aliases;
666 *ap = NULL;
667 result->h_aliases = host_data->aliases;
668 hap = host_data->h_addr_ptrs;
669 *hap = NULL;
670 result->h_addr_list = host_data->h_addr_ptrs;
671 haveanswer = 0;
672 had_error = 0;
673
674 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
675 {
676 int type, class;
677
678 n = __ns_name_unpack (answer->buf, end_of_message, cp,
679 packtmp, sizeof packtmp);
680 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
681 {
682 if (__builtin_expect (errno, 0) == EMSGSIZE)
683 goto too_small;
684
685 n = -1;
686 }
687
688 if (n < 0 || (*name_ok) (bp) == 0)
689 {
690 ++had_error;
691 continue;
692 }
693 cp += n; /* name */
694 type = ns_get16 (cp);
695 cp += INT16SZ; /* type */
696 class = ns_get16 (cp);
697 cp += INT16SZ; /* class */
698 ttl = ns_get32 (cp);
699 cp += INT32SZ; /* TTL */
700 n = ns_get16 (cp);
701 cp += INT16SZ; /* len */
702 if (class != C_IN)
703 {
704 /* XXX - debug? syslog? */
705 cp += n;
706 continue; /* XXX - had_error++ ? */
707 }
708
709 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME)
710 {
711 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
712 continue;
713 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
714 if (n < 0 || (*name_ok) (tbuf) == 0)
715 {
716 ++had_error;
717 continue;
718 }
719 cp += n;
720 /* Store alias. */
721 *ap++ = bp;
722 n = strlen (bp) + 1; /* For the \0. */
723 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
724 {
725 ++had_error;
726 continue;
727 }
728 bp += n;
729 linebuflen -= n;
730 /* Get canonical name. */
731 n = strlen (tbuf) + 1; /* For the \0. */
732 if (__builtin_expect (n > linebuflen, 0))
733 goto too_small;
734 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
735 {
736 ++had_error;
737 continue;
738 }
739 result->h_name = bp;
740 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
741 linebuflen -= n;
742 continue;
743 }
744
745 if (qtype == T_PTR && type == T_CNAME)
746 {
747 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
748 if (n < 0 || res_dnok (tbuf) == 0)
749 {
750 ++had_error;
751 continue;
752 }
753 cp += n;
754 /* Get canonical name. */
755 n = strlen (tbuf) + 1; /* For the \0. */
756 if (__builtin_expect (n > linebuflen, 0))
757 goto too_small;
758 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
759 {
760 ++had_error;
761 continue;
762 }
763 tname = bp;
764 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
765 linebuflen -= n;
766 continue;
767 }
768 if (__builtin_expect (type == T_SIG, 0)
769 || __builtin_expect (type == T_KEY, 0)
770 || __builtin_expect (type == T_NXT, 0))
771 {
772 /* We don't support DNSSEC yet. For now, ignore the record
773 and send a low priority message to syslog. */
774 syslog (LOG_DEBUG | LOG_AUTH,
775 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
776 qname, p_class (C_IN), p_type(qtype), p_type (type));
777 cp += n;
778 continue;
779 }
780
781 if (type == T_A && qtype == T_AAAA && map)
782 have_to_map = 1;
783 else if (__builtin_expect (type != qtype, 0))
784 {
785 syslog (LOG_NOTICE | LOG_AUTH,
786 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
787 qname, p_class (C_IN), p_type (qtype), p_type (type));
788 cp += n;
789 continue; /* XXX - had_error++ ? */
790 }
791
792 switch (type)
793 {
794 case T_PTR:
795 if (__strcasecmp (tname, bp) != 0)
796 {
797 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
798 cp += n;
799 continue; /* XXX - had_error++ ? */
800 }
801
802 n = __ns_name_unpack (answer->buf, end_of_message, cp,
803 packtmp, sizeof packtmp);
804 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
805 {
806 if (__builtin_expect (errno, 0) == EMSGSIZE)
807 goto too_small;
808
809 n = -1;
810 }
811
812 if (n < 0 || res_hnok (bp) == 0)
813 {
814 ++had_error;
815 break;
816 }
817 #if MULTI_PTRS_ARE_ALIASES
818 cp += n;
819 if (haveanswer == 0)
820 result->h_name = bp;
821 else if (ap < &host_data->aliases[MAXALIASES-1])
822 *ap++ = bp;
823 else
824 n = -1;
825 if (n != -1)
826 {
827 n = strlen (bp) + 1; /* for the \0 */
828 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
829 {
830 ++had_error;
831 break;
832 }
833 bp += n;
834 linebuflen -= n;
835 }
836 break;
837 #else
838 result->h_name = bp;
839 if (have_to_map)
840 {
841 n = strlen (bp) + 1; /* for the \0 */
842 if (n >= MAXHOSTNAMELEN)
843 {
844 ++had_error;
845 break;
846 }
847 bp += n;
848 linebuflen -= n;
849 map_v4v6_hostent (result, &bp, &linebuflen);
850 }
851 *h_errnop = NETDB_SUCCESS;
852 return NSS_STATUS_SUCCESS;
853 #endif
854 case T_A:
855 case T_AAAA:
856 if (__builtin_expect (strcasecmp (result->h_name, bp), 0) != 0)
857 {
858 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
859 cp += n;
860 continue; /* XXX - had_error++ ? */
861 }
862 if (n != result->h_length)
863 {
864 cp += n;
865 continue;
866 }
867 if (!haveanswer)
868 {
869 register int nn;
870
871 if (ttlp != NULL && ttl != 0)
872 *ttlp = ttl;
873 if (canonp != NULL)
874 *canonp = bp;
875 result->h_name = bp;
876 nn = strlen (bp) + 1; /* for the \0 */
877 bp += nn;
878 linebuflen -= nn;
879 }
880
881 linebuflen -= sizeof (align) - ((u_long) bp % sizeof (align));
882 bp += sizeof (align) - ((u_long) bp % sizeof (align));
883
884 if (__builtin_expect (n > linebuflen, 0))
885 goto too_small;
886 bp = __mempcpy (*hap++ = bp, cp, n);
887 cp += n;
888 linebuflen -= n;
889 break;
890 default:
891 abort ();
892 }
893 if (had_error == 0)
894 ++haveanswer;
895 }
896
897 if (haveanswer > 0)
898 {
899 *ap = NULL;
900 *hap = NULL;
901 #if defined RESOLVSORT
902 /*
903 * Note: we sort even if host can take only one address
904 * in its return structures - should give it the "best"
905 * address in that case, not some random one
906 */
907 if (_res.nsort && haveanswer > 1 && qtype == T_A)
908 addrsort (host_data->h_addr_ptrs, haveanswer);
909 #endif /*RESOLVSORT*/
910
911 if (result->h_name == NULL)
912 {
913 n = strlen (qname) + 1; /* For the \0. */
914 if (n > linebuflen)
915 goto too_small;
916 if (n >= MAXHOSTNAMELEN)
917 goto no_recovery;
918 result->h_name = bp;
919 bp = __mempcpy (bp, qname, n); /* Cannot overflow. */
920 linebuflen -= n;
921 }
922
923 if (have_to_map)
924 map_v4v6_hostent (result, &bp, &linebuflen);
925 *h_errnop = NETDB_SUCCESS;
926 return NSS_STATUS_SUCCESS;
927 }
928 no_recovery:
929 *h_errnop = NO_RECOVERY;
930 *errnop = ENOENT;
931 /* Special case here: if the resolver sent a result but it only
932 contains a CNAME while we are looking for a T_A or T_AAAA record,
933 we fail with NOTFOUND instead of TRYAGAIN. */
934 return ((qtype == T_A || qtype == T_AAAA) && ap != host_data->aliases
935 ? NSS_STATUS_NOTFOUND : NSS_STATUS_TRYAGAIN);
936 }
937
938
939 static enum nss_status
940 gaih_getanswer_slice (const querybuf *answer, int anslen, const char *qname,
941 struct gaih_addrtuple ***patp,
942 char **bufferp, size_t *buflenp,
943 int *errnop, int *h_errnop, int32_t *ttlp, int *firstp)
944 {
945 char *buffer = *bufferp;
946 size_t buflen = *buflenp;
947
948 struct gaih_addrtuple **pat = *patp;
949 const HEADER *hp = &answer->hdr;
950 int ancount = ntohs (hp->ancount);
951 int qdcount = ntohs (hp->qdcount);
952 const u_char *cp = answer->buf + HFIXEDSZ;
953 const u_char *end_of_message = answer->buf + anslen;
954 if (__builtin_expect (qdcount != 1, 0))
955 {
956 *h_errnop = NO_RECOVERY;
957 return NSS_STATUS_UNAVAIL;
958 }
959
960 u_char packtmp[NS_MAXCDNAME];
961 int n = __ns_name_unpack (answer->buf, end_of_message, cp,
962 packtmp, sizeof packtmp);
963 /* We unpack the name to check it for validity. But we do not need
964 it later. */
965 if (n != -1 && __ns_name_ntop (packtmp, buffer, buflen) == -1)
966 {
967 if (__builtin_expect (errno, 0) == EMSGSIZE)
968 {
969 too_small:
970 *errnop = ERANGE;
971 *h_errnop = NETDB_INTERNAL;
972 return NSS_STATUS_TRYAGAIN;
973 }
974
975 n = -1;
976 }
977
978 if (__builtin_expect (n < 0 || (res_hnok (buffer) == 0
979 && (errno = EBADMSG)), 0))
980 {
981 *errnop = errno;
982 *h_errnop = NO_RECOVERY;
983 return NSS_STATUS_UNAVAIL;
984 }
985 cp += n + QFIXEDSZ;
986
987 int haveanswer = 0;
988 int had_error = 0;
989 char *canon = NULL;
990 char *h_name = NULL;
991 int h_namelen = 0;
992
993 if (ancount == 0)
994 return NSS_STATUS_NOTFOUND;
995
996 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
997 {
998 n = __ns_name_unpack (answer->buf, end_of_message, cp,
999 packtmp, sizeof packtmp);
1000 if (n != -1 &&
1001 (h_namelen = __ns_name_ntop (packtmp, buffer, buflen)) == -1)
1002 {
1003 if (__builtin_expect (errno, 0) == EMSGSIZE)
1004 goto too_small;
1005
1006 n = -1;
1007 }
1008 if (n < 0 || res_hnok (buffer) == 0)
1009 {
1010 ++had_error;
1011 continue;
1012 }
1013 if (*firstp)
1014 {
1015 h_name = buffer;
1016 buffer += h_namelen;
1017 buflen -= h_namelen;
1018 }
1019
1020 cp += n; /* name */
1021 int type = ns_get16 (cp);
1022 cp += INT16SZ; /* type */
1023 int class = ns_get16 (cp);
1024 cp += INT16SZ; /* class */
1025 int32_t ttl = ns_get32 (cp);
1026 cp += INT32SZ; /* TTL */
1027 n = ns_get16 (cp);
1028 cp += INT16SZ; /* len */
1029
1030 if (class != C_IN)
1031 {
1032 cp += n;
1033 continue;
1034 }
1035
1036 if (type == T_CNAME)
1037 {
1038 char tbuf[MAXDNAME];
1039 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
1040 if (n < 0 || res_hnok (tbuf) == 0)
1041 {
1042 ++had_error;
1043 continue;
1044 }
1045 cp += n;
1046
1047 if (*firstp)
1048 {
1049 /* Reclaim buffer space. */
1050 if (h_name + h_namelen == buffer)
1051 {
1052 buffer = h_name;
1053 buflen += h_namelen;
1054 }
1055
1056 n = strlen (tbuf) + 1;
1057 if (__builtin_expect (n > buflen, 0))
1058 goto too_small;
1059 if (__builtin_expect (n >= MAXHOSTNAMELEN, 0))
1060 {
1061 ++had_error;
1062 continue;
1063 }
1064
1065 canon = buffer;
1066 buffer = __mempcpy (buffer, tbuf, n);
1067 buflen -= n;
1068 h_namelen = 0;
1069 }
1070 continue;
1071 }
1072 if (__builtin_expect (type == T_SIG, 0)
1073 || __builtin_expect (type == T_KEY, 0)
1074 || __builtin_expect (type == T_NXT, 0)
1075 || __builtin_expect (type == T_PTR, 0)
1076 || __builtin_expect (type == T_DNAME, 0))
1077 {
1078 /* We don't support DNSSEC yet. For now, ignore the record
1079 and send a low priority message to syslog.
1080
1081 We also don't expect T_PTR or T_DNAME messages. */
1082 syslog (LOG_DEBUG | LOG_AUTH,
1083 "getaddrinfo*.gaih_getanswer: got type \"%s\"",
1084 p_type (type));
1085 cp += n;
1086 continue;
1087 }
1088 if (type != T_A && type != T_AAAA)
1089 abort ();
1090
1091 if (*pat == NULL)
1092 {
1093 uintptr_t pad = (-(uintptr_t) buffer
1094 % __alignof__ (struct gaih_addrtuple));
1095 buffer += pad;
1096 buflen = buflen > pad ? buflen - pad : 0;
1097
1098 if (__builtin_expect (buflen < sizeof (struct gaih_addrtuple),
1099 0))
1100 {
1101 *errnop = ERANGE;
1102 *h_errnop = NETDB_INTERNAL;
1103 return NSS_STATUS_TRYAGAIN;
1104 }
1105
1106 *pat = (struct gaih_addrtuple *) buffer;
1107 buffer += sizeof (struct gaih_addrtuple);
1108 buflen -= sizeof (struct gaih_addrtuple);
1109 }
1110
1111 (*pat)->name = NULL;
1112 (*pat)->next = NULL;
1113
1114 if (*firstp)
1115 {
1116 if (ttl != 0 && ttlp != NULL)
1117 *ttlp = ttl;
1118
1119 if (canon != NULL)
1120 {
1121 (*pat)->name = canon;
1122
1123 /* Reclaim buffer space. */
1124 if (h_name + h_namelen == buffer)
1125 {
1126 buffer = h_name;
1127 buflen += h_namelen;
1128 }
1129 }
1130 else
1131 (*pat)->name = h_name;
1132
1133 *firstp = 0;
1134 }
1135
1136 (*pat)->family = type == T_A ? AF_INET : AF_INET6;
1137 memcpy ((*pat)->addr, cp, n);
1138 cp += n;
1139 (*pat)->scopeid = 0;
1140
1141 pat = &((*pat)->next);
1142
1143 haveanswer = 1;
1144 }
1145
1146 if (haveanswer)
1147 {
1148 *patp = pat;
1149 *bufferp = buffer;
1150 *buflenp = buflen;
1151
1152 *h_errnop = NETDB_SUCCESS;
1153 return NSS_STATUS_SUCCESS;
1154 }
1155
1156 /* Special case here: if the resolver sent a result but it only
1157 contains a CNAME while we are looking for a T_A or T_AAAA record,
1158 we fail with NOTFOUND instead of TRYAGAIN. */
1159 return canon == NULL ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
1160 }
1161
1162
1163 static enum nss_status
1164 gaih_getanswer (const querybuf *answer1, int anslen1, const querybuf *answer2,
1165 int anslen2, const char *qname,
1166 struct gaih_addrtuple **pat, char *buffer, size_t buflen,
1167 int *errnop, int *h_errnop, int32_t *ttlp)
1168 {
1169 int first = 1;
1170
1171 enum nss_status status = NSS_STATUS_NOTFOUND;
1172
1173 if (anslen1 > 0)
1174 status = gaih_getanswer_slice(answer1, anslen1, qname,
1175 &pat, &buffer, &buflen,
1176 errnop, h_errnop, ttlp,
1177 &first);
1178 if ((status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND)
1179 && answer2 != NULL && anslen2 > 0)
1180 status = gaih_getanswer_slice(answer2, anslen2, qname,
1181 &pat, &buffer, &buflen,
1182 errnop, h_errnop, ttlp, &first);
1183
1184 return status;
1185 }