]> git.ipfire.org Git - thirdparty/glibc.git/blame - resolv/nss_dns/dns-host.c
Update.
[thirdparty/glibc.git] / resolv / nss_dns / dns-host.c
CommitLineData
5ae19a50 1/* Copyright (C) 1996-2000, 2001, 2002, 2003 Free Software Foundation, Inc.
84384f5b
UD
2 This file is part of the GNU C Library.
3 Extended from original form by Ulrich Drepper <drepper@cygnus.com>, 1996.
5f0e6fc7 4
84384f5b 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
5f0e6fc7 9
84384f5b
UD
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
5f0e6fc7 14
41bdb6e2
AJ
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. */
5f0e6fc7 19
df21c858 20/* Parts of this file are plain copies of the file `gethtnamadr.c' from
5f0e6fc7
RM
21 the bind package and it has the following copyright. */
22
5f0e6fc7 23/*
df21c858
UD
24 * ++Copyright++ 1985, 1988, 1993
25 * -
26 * Copyright (c) 1985, 1988, 1993
27 * The Regents of the University of California. All rights reserved.
5f0e6fc7
RM
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.
5f0e6fc7
RM
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.
df21c858
UD
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--
5f0e6fc7
RM
72 */
73
74#include <ctype.h>
75#include <errno.h>
76#include <netdb.h>
77#include <stdio.h>
78#include <stdlib.h>
79#include <stddef.h>
80#include <string.h>
81#include <sys/syslog.h>
82
5f0e6fc7
RM
83#include "nsswitch.h"
84
85/* Get implementation for some internal functions. */
da2d1bc5
UD
86#include <resolv/mapv4v6addr.h>
87#include <resolv/mapv4v6hostent.h>
5f0e6fc7 88
5db91571
UD
89#define RESOLVSORT
90
5f0e6fc7
RM
91/* Maximum number of aliases we allow. */
92#define MAX_NR_ALIASES 48
93#define MAX_NR_ADDRS 48
94
e2dced82 95#if PACKETSZ > 65536
5f0e6fc7
RM
96# define MAXPACKET PACKETSZ
97#else
e2dced82 98# define MAXPACKET 65536
5f0e6fc7 99#endif
1522c368
UD
100/* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
101#ifdef MAXHOSTNAMELEN
102# undef MAXHOSTNAMELEN
103#endif
104#define MAXHOSTNAMELEN 256
5f0e6fc7
RM
105
106static const char AskedForGot[] = "\
107gethostby*.getanswer: asked for \"%s\", got \"%s\"";
108
109
110/* We need this time later. */
111typedef union querybuf
112{
113 HEADER hdr;
114 u_char buf[MAXPACKET];
115} querybuf;
116
8d8c6efa
UD
117/* These functions are defined in res_comp.c. */
118#define NS_MAXCDNAME 255 /* maximum compressed domain name */
b455972f
UD
119extern int __ns_name_ntop (const u_char *, char *, size_t);
120extern int __ns_name_unpack (const u_char *, const u_char *,
121 const u_char *, u_char *, size_t);
8d8c6efa 122
5f0e6fc7
RM
123
124static enum nss_status getanswer_r (const querybuf *answer, int anslen,
125 const char *qname, int qtype,
df4ef2ab 126 struct hostent *result, char *buffer,
b455972f
UD
127 size_t buflen, int *errnop, int *h_errnop,
128 int map);
5f0e6fc7
RM
129
130enum nss_status
131_nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
67479a70
UD
132 char *buffer, size_t buflen, int *errnop,
133 int *h_errnop)
5f0e6fc7 134{
16b66062
AJ
135 union
136 {
137 querybuf *buf;
138 u_char *ptr;
139 } host_buffer;
140 querybuf *orig_host_buffer;
f80f1a4a 141 char tmp[NS_MAXDNAME];
5f0e6fc7
RM
142 int size, type, n;
143 const char *cp;
0420d888 144 int map = 0;
34816665 145 int olderr = errno;
6166815d 146 enum nss_status status;
5f0e6fc7 147
b43b13ac
UD
148 if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
149 return NSS_STATUS_UNAVAIL;
150
5f0e6fc7
RM
151 switch (af) {
152 case AF_INET:
153 size = INADDRSZ;
154 type = T_A;
155 break;
156 case AF_INET6:
157 size = IN6ADDRSZ;
158 type = T_AAAA;
159 break;
160 default:
0cc70fcf 161 *h_errnop = NO_DATA;
67479a70 162 *errnop = EAFNOSUPPORT;
5f0e6fc7
RM
163 return NSS_STATUS_UNAVAIL;
164 }
165
166 result->h_addrtype = af;
167 result->h_length = size;
168
169 /*
170 * if there aren't any dots, it could be a user-level alias.
171 * this is also done in res_query() since we are not the only
172 * function that looks up host names.
173 */
f80f1a4a
UD
174 if (strchr (name, '.') == NULL
175 && (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL)
5f0e6fc7
RM
176 name = cp;
177
16b66062 178 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
6166815d 179
16b66062
AJ
180 n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer.buf->buf,
181 1024, &host_buffer.ptr);
5f0e6fc7 182 if (n < 0)
dd7d45e8 183 {
b455972f
UD
184 enum nss_status status = (errno == ECONNREFUSED
185 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND);
dd7d45e8 186 *h_errnop = h_errno;
34816665
UD
187 if (h_errno == TRY_AGAIN)
188 *errnop = EAGAIN;
189 else
190 __set_errno (olderr);
b455972f
UD
191
192 /* If we are looking for a IPv6 address and mapping is enabled
193 by having the RES_USE_INET6 bit in _res.options set, we try
194 another lookup. */
195 if (af == AF_INET6 && (_res.options & RES_USE_INET6))
16b66062
AJ
196 n = __libc_res_nsearch (&_res, name, C_IN, T_A, host_buffer.buf->buf,
197 host_buffer.buf != orig_host_buffer
198 ? MAXPACKET : 1024, &host_buffer.ptr);
b455972f
UD
199
200 if (n < 0)
6166815d 201 {
16b66062
AJ
202 if (host_buffer.buf != orig_host_buffer)
203 free (host_buffer.buf);
6166815d
UD
204 return status;
205 }
b455972f
UD
206
207 map = 1;
208
209 result->h_addrtype = AF_INET;
210 result->h_length = INADDRSZ;;
dd7d45e8 211 }
5f0e6fc7 212
16b66062 213 status = getanswer_r (host_buffer.buf, n, name, type, result, buffer, buflen,
6166815d 214 errnop, h_errnop, map);
16b66062
AJ
215 if (host_buffer.buf != orig_host_buffer)
216 free (host_buffer.buf);
6166815d 217 return status;
5f0e6fc7
RM
218}
219
220
221enum nss_status
222_nss_dns_gethostbyname_r (const char *name, struct hostent *result,
67479a70
UD
223 char *buffer, size_t buflen, int *errnop,
224 int *h_errnop)
5f0e6fc7
RM
225{
226 enum nss_status status = NSS_STATUS_NOTFOUND;
227
228 if (_res.options & RES_USE_INET6)
229 status = _nss_dns_gethostbyname2_r (name, AF_INET6, result, buffer,
67479a70 230 buflen, errnop, h_errnop);
5f0e6fc7
RM
231 if (status == NSS_STATUS_NOTFOUND)
232 status = _nss_dns_gethostbyname2_r (name, AF_INET, result, buffer,
67479a70 233 buflen, errnop, h_errnop);
5f0e6fc7
RM
234
235 return status;
236}
237
238
239enum nss_status
51eecc4a 240_nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
df4ef2ab 241 struct hostent *result, char *buffer, size_t buflen,
67479a70 242 int *errnop, int *h_errnop)
5f0e6fc7
RM
243{
244 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
245 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
20cc4c87 246 static const u_char v6local[] = { 0,0, 0,1 };
5f0e6fc7
RM
247 const u_char *uaddr = (const u_char *)addr;
248 struct host_data
249 {
250 char *aliases[MAX_NR_ALIASES];
251 unsigned char host_addr[16]; /* IPv4 or IPv6 */
252 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
253 char linebuffer[0];
254 } *host_data = (struct host_data *) buffer;
16b66062
AJ
255 union
256 {
257 querybuf *buf;
258 u_char *ptr;
259 } host_buffer;
260 querybuf *orig_host_buffer;
1f3f143e 261 char qbuf[MAXDNAME+1], *qp = NULL;
30f22ab1 262 size_t size;
0420d888 263 int n, status;
34816665 264 int olderr = errno;
5f0e6fc7 265
b43b13ac
UD
266 if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1)
267 return NSS_STATUS_UNAVAIL;
268
20cc4c87
UD
269 if (af == AF_INET6 && len == IN6ADDRSZ
270 && (memcmp (uaddr, mapped, sizeof mapped) == 0
271 || (memcmp (uaddr, tunnelled, sizeof tunnelled) == 0
272 && memcmp (&uaddr[sizeof tunnelled], v6local, sizeof v6local))))
5f0e6fc7
RM
273 {
274 /* Unmap. */
275 addr += sizeof mapped;
276 uaddr += sizeof mapped;
277 af = AF_INET;
278 len = INADDRSZ;
279 }
280
281 switch (af)
282 {
283 case AF_INET:
284 size = INADDRSZ;
285 break;
286 case AF_INET6:
287 size = IN6ADDRSZ;
288 break;
289 default:
67479a70 290 *errnop = EAFNOSUPPORT;
5f0e6fc7
RM
291 *h_errnop = NETDB_INTERNAL;
292 return NSS_STATUS_UNAVAIL;
293 }
20cc4c87 294 if (size > len)
5f0e6fc7 295 {
67479a70 296 *errnop = EAFNOSUPPORT;
5f0e6fc7
RM
297 *h_errnop = NETDB_INTERNAL;
298 return NSS_STATUS_UNAVAIL;
299 }
300
301 switch (af)
302 {
303 case AF_INET:
304 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
305 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
306 break;
307 case AF_INET6:
5ae19a50
UD
308 /* XXX Maybe we need an option to select whether to use the nibble
309 or the bitfield form. The RFC requires the bitfield form so
310 we use it. */
5f0e6fc7 311 qp = qbuf;
5ae19a50
UD
312 qp = stpcpy (qbuf, "\\[x");
313 for (n = 0; n < IN6ADDRSZ; ++n)
314 qp += sprintf (qp, "%02hhx", uaddr[n]);
315 strcpy (qp, "].ip6.arpa");
5f0e6fc7
RM
316 break;
317 default:
318 /* Cannot happen. */
9fea9ed6 319 break;
5f0e6fc7
RM
320 }
321
16b66062 322 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
6166815d 323
16b66062
AJ
324 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
325 1024, &host_buffer.ptr);
62a08e44
UD
326 if (n < 0 && af == AF_INET6)
327 {
5ae19a50
UD
328 qp = qbuf;
329 for (n = IN6ADDRSZ - 1; n >= 0; n--)
330 qp += sprintf (qp, "%x.%x.", uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf);
62a08e44 331 strcpy (qp, "ip6.int");
16b66062
AJ
332 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
333 host_buffer.buf != orig_host_buffer
334 ? MAXPACKET : 1024, &host_buffer.ptr);
62a08e44 335 }
5f0e6fc7 336 if (n < 0)
dd7d45e8
UD
337 {
338 *h_errnop = h_errno;
34816665 339 __set_errno (olderr);
16b66062
AJ
340 if (host_buffer.buf != orig_host_buffer)
341 free (host_buffer.buf);
dd7d45e8
UD
342 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
343 }
5f0e6fc7 344
16b66062 345 status = getanswer_r (host_buffer.buf, n, qbuf, T_PTR, result, buffer, buflen,
b455972f 346 errnop, h_errnop, 0 /* XXX */);
16b66062
AJ
347 if (host_buffer.buf != orig_host_buffer)
348 free (host_buffer.buf);
5f0e6fc7 349 if (status != NSS_STATUS_SUCCESS)
dd7d45e8
UD
350 {
351 *h_errnop = h_errno;
67479a70 352 *errnop = errno;
dd7d45e8
UD
353 return status;
354 }
5f0e6fc7
RM
355
356#ifdef SUNSECURITY
357 This is not implemented because it is not possible to use the current
358 source from bind in a multi-threaded program.
359#endif
360
361 result->h_addrtype = af;
362 result->h_length = len;
63551311 363 memcpy (host_data->host_addr, addr, len);
5f0e6fc7
RM
364 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
365 host_data->h_addr_ptrs[1] = NULL;
b455972f
UD
366#if 0
367 /* XXX I think this is wrong. Why should an IPv4 address be
368 converted to IPv6 if the user explicitly asked for IPv4? */
5f0e6fc7
RM
369 if (af == AF_INET && (_res.options & RES_USE_INET6))
370 {
371 map_v4v6_address ((char *) host_data->host_addr,
372 (char *) host_data->host_addr);
373 result->h_addrtype = AF_INET6;
374 result->h_length = IN6ADDRSZ;
375 }
b455972f 376#endif
5f0e6fc7
RM
377 *h_errnop = NETDB_SUCCESS;
378 return NSS_STATUS_SUCCESS;
379}
380
5db91571
UD
381#ifdef RESOLVSORT
382static void addrsort (char **ap, int num);
383
384static void
385addrsort (char **ap, int num)
386{
387 int i, j;
388 char **p;
389 short aval[MAX_NR_ADDRS];
390 int needsort = 0;
391
392 p = ap;
393 if (num > MAX_NR_ADDRS)
394 num = MAX_NR_ADDRS;
395 for (i = 0; i < num; i++, p++)
396 {
397 for (j = 0 ; (unsigned)j < _res.nsort; j++)
398 if (_res.sort_list[j].addr.s_addr ==
399 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
400 break;
401 aval[i] = j;
402 if (needsort == 0 && i > 0 && j < aval[i-1])
403 needsort = i;
404 }
405 if (!needsort)
406 return;
407
408 while (needsort++ < num)
409 for (j = needsort - 2; j >= 0; j--)
410 if (aval[j] > aval[j+1])
411 {
412 char *hp;
413
414 i = aval[j];
415 aval[j] = aval[j+1];
416 aval[j+1] = i;
417
418 hp = ap[j];
419 ap[j] = ap[j+1];
420 ap[j+1] = hp;
421 }
422 else
423 break;
424}
425#endif
5f0e6fc7
RM
426
427static enum nss_status
428getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
df4ef2ab 429 struct hostent *result, char *buffer, size_t buflen,
b455972f 430 int *errnop, int *h_errnop, int map)
5f0e6fc7
RM
431{
432 struct host_data
433 {
434 char *aliases[MAX_NR_ALIASES];
435 unsigned char host_addr[16]; /* IPv4 or IPv6 */
88a96b81 436 char *h_addr_ptrs[0];
5f0e6fc7 437 } *host_data = (struct host_data *) buffer;
88a96b81 438 int linebuflen = buflen - sizeof (struct host_data);
5f0e6fc7
RM
439 register const HEADER *hp;
440 const u_char *end_of_message, *cp;
441 int n, ancount, qdcount;
442 int haveanswer, had_error;
443 char *bp, **ap, **hap;
845dcb57 444 char tbuf[MAXDNAME];
5f0e6fc7 445 const char *tname;
b455972f 446 int (*name_ok) (const char *);
8d8c6efa 447 u_char packtmp[NS_MAXCDNAME];
b455972f 448 int have_to_map = 0;
5f0e6fc7 449
b455972f 450 if (__builtin_expect (linebuflen, 0) < 0)
560d3b76
UD
451 {
452 /* The buffer is too small. */
453 too_small:
454 *errnop = ERANGE;
455 *h_errnop = NETDB_INTERNAL;
456 return NSS_STATUS_TRYAGAIN;
457 }
458
5f0e6fc7
RM
459 tname = qname;
460 result->h_name = NULL;
461 end_of_message = answer->buf + anslen;
462 switch (qtype)
463 {
464 case T_A:
465 case T_AAAA:
466 name_ok = res_hnok;
467 break;
468 case T_PTR:
469 name_ok = res_dnok;
470 break;
471 default:
a0bf6ac7 472 *errnop = ENOENT;
5f0e6fc7
RM
473 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
474 }
475
476 /*
477 * find first satisfactory answer
478 */
479 hp = &answer->hdr;
5f0e6fc7
RM
480 ancount = ntohs (hp->ancount);
481 qdcount = ntohs (hp->qdcount);
482 cp = answer->buf + HFIXEDSZ;
b455972f 483 if (__builtin_expect (qdcount, 1) != 1)
5f0e6fc7
RM
484 {
485 *h_errnop = NO_RECOVERY;
486 return NSS_STATUS_UNAVAIL;
487 }
88a96b81
UD
488 if (sizeof (struct host_data) + (ancount + 1) * sizeof (char *) >= buflen)
489 goto too_small;
490 bp = (char *) &host_data->h_addr_ptrs[ancount + 1];
491 linebuflen -= (ancount + 1) * sizeof (char *);
5f0e6fc7 492
8d8c6efa
UD
493 n = __ns_name_unpack (answer->buf, end_of_message, cp,
494 packtmp, sizeof packtmp);
495 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
5f0e6fc7 496 {
b455972f 497 if (__builtin_expect (errno, 0) == EMSGSIZE)
560d3b76 498 goto too_small;
8d8c6efa
UD
499
500 n = -1;
501 }
502
503 if (n > 0 && bp[0] == '.')
504 bp[0] = '\0';
505
506 if (n < 0 || (*name_ok) (bp) == 0)
507 {
508 *errnop = errno;
509 *h_errnop = NO_RECOVERY;
5f0e6fc7
RM
510 return NSS_STATUS_UNAVAIL;
511 }
512 cp += n + QFIXEDSZ;
513
514 if (qtype == T_A || qtype == T_AAAA)
515 {
516 /* res_send() has already verified that the query name is the
517 * same as the one we sent; this just gets the expanded name
518 * (i.e., with the succeeding search-domain tacked on).
519 */
520 n = strlen (bp) + 1; /* for the \0 */
76b87c03
UD
521 if (n >= MAXHOSTNAMELEN)
522 {
67479a70 523 *h_errnop = NO_RECOVERY;
a0bf6ac7 524 *errnop = ENOENT;
76b87c03
UD
525 return NSS_STATUS_TRYAGAIN;
526 }
5f0e6fc7
RM
527 result->h_name = bp;
528 bp += n;
529 linebuflen -= n;
560d3b76
UD
530 if (linebuflen < 0)
531 goto too_small;
5f0e6fc7
RM
532 /* The qname can be abbreviated, but h_name is now absolute. */
533 qname = result->h_name;
534 }
535
536 ap = host_data->aliases;
537 *ap = NULL;
538 result->h_aliases = host_data->aliases;
539 hap = host_data->h_addr_ptrs;
540 *hap = NULL;
541 result->h_addr_list = host_data->h_addr_ptrs;
542 haveanswer = 0;
543 had_error = 0;
544
545 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
546 {
547 int type, class;
548
8d8c6efa
UD
549 n = __ns_name_unpack (answer->buf, end_of_message, cp,
550 packtmp, sizeof packtmp);
551 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
552 {
b455972f 553 if (__builtin_expect (errno, 0) == EMSGSIZE)
0cc70fcf 554 goto too_small;
8d8c6efa
UD
555
556 n = -1;
557 }
558
5f0e6fc7
RM
559 if (n < 0 || (*name_ok) (bp) == 0)
560 {
561 ++had_error;
562 continue;
563 }
564 cp += n; /* name */
b43b13ac 565 type = ns_get16 (cp);
5f0e6fc7 566 cp += INT16SZ; /* type */
b43b13ac 567 class = ns_get16 (cp);
5f0e6fc7 568 cp += INT16SZ + INT32SZ; /* class, TTL */
b43b13ac 569 n = ns_get16 (cp);
5f0e6fc7
RM
570 cp += INT16SZ; /* len */
571 if (class != C_IN)
572 {
573 /* XXX - debug? syslog? */
574 cp += n;
575 continue; /* XXX - had_error++ ? */
576 }
577
578 if ((qtype ==T_A || qtype == T_AAAA) && type == T_CNAME)
579 {
580 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
581 continue;
582 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
583 if (n < 0 || (*name_ok) (tbuf) == 0)
584 {
585 ++had_error;
586 continue;
587 }
588 cp += n;
589 /* Store alias. */
590 *ap++ = bp;
591 n = strlen (bp) + 1; /* For the \0. */
b455972f 592 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
76b87c03
UD
593 {
594 ++had_error;
595 continue;
596 }
5f0e6fc7
RM
597 bp += n;
598 linebuflen -= n;
599 /* Get canonical name. */
600 n = strlen (tbuf) + 1; /* For the \0. */
b455972f 601 if (__builtin_expect (n > linebuflen, 0))
0cc70fcf 602 goto too_small;
b455972f 603 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
5f0e6fc7
RM
604 {
605 ++had_error;
606 continue;
607 }
8d8c6efa
UD
608 result->h_name = bp;
609 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
5f0e6fc7
RM
610 linebuflen -= n;
611 continue;
612 }
613
614 if (qtype == T_PTR && type == T_CNAME)
615 {
616 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
da2d1bc5 617 if (n < 0 || res_dnok (tbuf) == 0)
5f0e6fc7
RM
618 {
619 ++had_error;
620 continue;
621 }
622 cp += n;
da2d1bc5 623 /* Get canonical name. */
5f0e6fc7 624 n = strlen (tbuf) + 1; /* For the \0. */
b455972f 625 if (__builtin_expect (n > linebuflen, 0))
0cc70fcf 626 goto too_small;
b455972f 627 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
5f0e6fc7
RM
628 {
629 ++had_error;
630 continue;
631 }
8d8c6efa
UD
632 tname = bp;
633 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
5f0e6fc7
RM
634 linebuflen -= n;
635 continue;
636 }
b455972f
UD
637 if (__builtin_expect (type == T_SIG, 0)
638 || __builtin_expect (type == T_KEY, 0)
639 || __builtin_expect (type == T_NXT, 0))
1bc21e7a
UD
640 {
641 /* We don't support DNSSEC yet. For now, ignore the record
642 and send a low priority message to syslog. */
643 syslog (LOG_DEBUG | LOG_AUTH,
644 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
645 qname, p_class (C_IN), p_type(qtype), p_type (type));
646 cp += n;
647 continue;
648 }
b455972f
UD
649
650 if (type == T_A && qtype == T_AAAA && map)
651 have_to_map = 1;
652 else if (__builtin_expect (type != qtype, 0))
5f0e6fc7
RM
653 {
654 syslog (LOG_NOTICE | LOG_AUTH,
655 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
656 qname, p_class (C_IN), p_type (qtype), p_type (type));
657 cp += n;
658 continue; /* XXX - had_error++ ? */
659 }
660
661 switch (type)
662 {
663 case T_PTR:
8d8c6efa 664 if (__strcasecmp (tname, bp) != 0)
5f0e6fc7
RM
665 {
666 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
667 cp += n;
668 continue; /* XXX - had_error++ ? */
669 }
8d8c6efa
UD
670
671 n = __ns_name_unpack (answer->buf, end_of_message, cp,
672 packtmp, sizeof packtmp);
673 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
674 {
b455972f 675 if (__builtin_expect (errno, 0) == EMSGSIZE)
0cc70fcf 676 goto too_small;
8d8c6efa
UD
677
678 n = -1;
679 }
680
5f0e6fc7
RM
681 if (n < 0 || res_hnok (bp) == 0)
682 {
683 ++had_error;
684 break;
685 }
686#if MULTI_PTRS_ARE_ALIASES
687 cp += n;
688 if (haveanswer == 0)
689 result->h_name = bp;
690 else if (ap < &host_data->aliases[MAXALIASES-1])
691 *ap++ = bp;
692 else
693 n = -1;
694 if (n != -1)
695 {
696 n = strlen (bp) + 1; /* for the \0 */
b455972f 697 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
76b87c03
UD
698 {
699 ++had_error;
700 break;
701 }
5f0e6fc7
RM
702 bp += n;
703 linebuflen -= n;
704 }
705 break;
706#else
707 result->h_name = bp;
b455972f 708 if (have_to_map)
5f0e6fc7
RM
709 {
710 n = strlen (bp) + 1; /* for the \0 */
76b87c03
UD
711 if (n >= MAXHOSTNAMELEN)
712 {
713 ++had_error;
714 break;
715 }
5f0e6fc7
RM
716 bp += n;
717 linebuflen -= n;
718 map_v4v6_hostent (result, &bp, &linebuflen);
719 }
720 *h_errnop = NETDB_SUCCESS;
721 return NSS_STATUS_SUCCESS;
722#endif
723 case T_A:
724 case T_AAAA:
b455972f 725 if (__builtin_expect (strcasecmp (result->h_name, bp), 0) != 0)
5f0e6fc7
RM
726 {
727 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
728 cp += n;
729 continue; /* XXX - had_error++ ? */
730 }
1f64ac13 731 if (n != result->h_length)
5f0e6fc7 732 {
1f64ac13
UD
733 cp += n;
734 continue;
5f0e6fc7 735 }
1f64ac13 736 if (!haveanswer)
5f0e6fc7
RM
737 {
738 register int nn;
739
740 result->h_name = bp;
741 nn = strlen (bp) + 1; /* for the \0 */
742 bp += nn;
743 linebuflen -= nn;
744 }
745
560d3b76 746 linebuflen -= sizeof (align) - ((u_long) bp % sizeof (align));
5f0e6fc7
RM
747 bp += sizeof (align) - ((u_long) bp % sizeof (align));
748
b455972f 749 if (__builtin_expect (n > linebuflen, 0))
0cc70fcf 750 goto too_small;
8d8c6efa 751 bp = __mempcpy (*hap++ = bp, cp, n);
5f0e6fc7
RM
752 cp += n;
753 linebuflen -= n;
754 break;
755 default:
756 abort ();
757 }
758 if (had_error == 0)
759 ++haveanswer;
760 }
761
762 if (haveanswer > 0)
763 {
764 *ap = NULL;
765 *hap = NULL;
da2d1bc5 766#if defined RESOLVSORT
5f0e6fc7
RM
767 /*
768 * Note: we sort even if host can take only one address
769 * in its return structures - should give it the "best"
770 * address in that case, not some random one
771 */
772 if (_res.nsort && haveanswer > 1 && qtype == T_A)
773 addrsort (host_data->h_addr_ptrs, haveanswer);
774#endif /*RESOLVSORT*/
775
776 if (result->h_name == NULL)
777 {
778 n = strlen (qname) + 1; /* For the \0. */
8d8c6efa 779 if (n > linebuflen)
0cc70fcf 780 goto too_small;
8d8c6efa 781 if (n >= MAXHOSTNAMELEN)
76b87c03 782 goto no_recovery;
8d8c6efa
UD
783 result->h_name = bp;
784 bp = __mempcpy (bp, qname, n); /* Cannot overflow. */
5f0e6fc7
RM
785 linebuflen -= n;
786 }
787
b455972f 788 if (have_to_map)
5f0e6fc7
RM
789 map_v4v6_hostent (result, &bp, &linebuflen);
790 *h_errnop = NETDB_SUCCESS;
791 return NSS_STATUS_SUCCESS;
792 }
76b87c03
UD
793 no_recovery:
794 *h_errnop = NO_RECOVERY;
a0bf6ac7 795 *errnop = ENOENT;
5f0e6fc7
RM
796 return NSS_STATUS_TRYAGAIN;
797}