]> git.ipfire.org Git - thirdparty/glibc.git/blame - resolv/nss_dns/dns-host.c
Adjust __libc_res_nquery and __libc_res_nsend prototypes.
[thirdparty/glibc.git] / resolv / nss_dns / dns-host.c
CommitLineData
65b4743a 1/* Copyright (C) 1996-2004, 2007, 2008 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 127 size_t buflen, int *errnop, int *h_errnop,
d1fe1f22 128 int map, int32_t *ttlp, char **canonp);
5f0e6fc7 129
78d8d211
UD
130extern enum nss_status _nss_dns_gethostbyname3_r (const char *name, int af,
131 struct hostent *result,
132 char *buffer, size_t buflen,
133 int *errnop, int *h_errnop,
134 int32_t *ttlp,
135 char **canonp);
136hidden_proto (_nss_dns_gethostbyname3_r)
137
5f0e6fc7 138enum nss_status
d1fe1f22 139_nss_dns_gethostbyname3_r (const char *name, int af, struct hostent *result,
67479a70 140 char *buffer, size_t buflen, int *errnop,
d1fe1f22 141 int *h_errnop, int32_t *ttlp, char **canonp)
5f0e6fc7 142{
16b66062
AJ
143 union
144 {
145 querybuf *buf;
146 u_char *ptr;
147 } host_buffer;
148 querybuf *orig_host_buffer;
f80f1a4a 149 char tmp[NS_MAXDNAME];
5f0e6fc7
RM
150 int size, type, n;
151 const char *cp;
0420d888 152 int map = 0;
34816665 153 int olderr = errno;
6166815d 154 enum nss_status status;
5f0e6fc7 155
87bb6b6c 156 if (__res_maybe_init (&_res, 0) == -1)
b43b13ac
UD
157 return NSS_STATUS_UNAVAIL;
158
5f0e6fc7
RM
159 switch (af) {
160 case AF_INET:
161 size = INADDRSZ;
162 type = T_A;
163 break;
164 case AF_INET6:
165 size = IN6ADDRSZ;
166 type = T_AAAA;
167 break;
168 default:
0cc70fcf 169 *h_errnop = NO_DATA;
67479a70 170 *errnop = EAFNOSUPPORT;
5f0e6fc7
RM
171 return NSS_STATUS_UNAVAIL;
172 }
173
174 result->h_addrtype = af;
175 result->h_length = size;
176
177 /*
178 * if there aren't any dots, it could be a user-level alias.
179 * this is also done in res_query() since we are not the only
180 * function that looks up host names.
181 */
f80f1a4a
UD
182 if (strchr (name, '.') == NULL
183 && (cp = res_hostalias (&_res, name, tmp, sizeof (tmp))) != NULL)
5f0e6fc7
RM
184 name = cp;
185
16b66062 186 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
6166815d 187
16b66062
AJ
188 n = __libc_res_nsearch (&_res, name, C_IN, type, host_buffer.buf->buf,
189 1024, &host_buffer.ptr);
5f0e6fc7 190 if (n < 0)
dd7d45e8 191 {
b455972f
UD
192 enum nss_status status = (errno == ECONNREFUSED
193 ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND);
dd7d45e8 194 *h_errnop = h_errno;
34816665
UD
195 if (h_errno == TRY_AGAIN)
196 *errnop = EAGAIN;
197 else
198 __set_errno (olderr);
b455972f
UD
199
200 /* If we are looking for a IPv6 address and mapping is enabled
201 by having the RES_USE_INET6 bit in _res.options set, we try
202 another lookup. */
203 if (af == AF_INET6 && (_res.options & RES_USE_INET6))
16b66062
AJ
204 n = __libc_res_nsearch (&_res, name, C_IN, T_A, host_buffer.buf->buf,
205 host_buffer.buf != orig_host_buffer
206 ? MAXPACKET : 1024, &host_buffer.ptr);
b455972f
UD
207
208 if (n < 0)
6166815d 209 {
16b66062
AJ
210 if (host_buffer.buf != orig_host_buffer)
211 free (host_buffer.buf);
6166815d
UD
212 return status;
213 }
b455972f
UD
214
215 map = 1;
216
217 result->h_addrtype = AF_INET;
2884dad4 218 result->h_length = INADDRSZ;
dd7d45e8 219 }
5f0e6fc7 220
16b66062 221 status = getanswer_r (host_buffer.buf, n, name, type, result, buffer, buflen,
d1fe1f22 222 errnop, h_errnop, map, ttlp, canonp);
16b66062
AJ
223 if (host_buffer.buf != orig_host_buffer)
224 free (host_buffer.buf);
6166815d 225 return status;
5f0e6fc7 226}
78d8d211 227hidden_def (_nss_dns_gethostbyname3_r)
5f0e6fc7
RM
228
229
d1fe1f22
UD
230enum nss_status
231_nss_dns_gethostbyname2_r (const char *name, int af, struct hostent *result,
232 char *buffer, size_t buflen, int *errnop,
233 int *h_errnop)
234{
235 return _nss_dns_gethostbyname3_r (name, af, result, buffer, buflen, errnop,
236 h_errnop, NULL, NULL);
237}
238
239
5f0e6fc7
RM
240enum nss_status
241_nss_dns_gethostbyname_r (const char *name, struct hostent *result,
67479a70
UD
242 char *buffer, size_t buflen, int *errnop,
243 int *h_errnop)
5f0e6fc7
RM
244{
245 enum nss_status status = NSS_STATUS_NOTFOUND;
246
247 if (_res.options & RES_USE_INET6)
78d8d211
UD
248 status = _nss_dns_gethostbyname3_r (name, AF_INET6, result, buffer,
249 buflen, errnop, h_errnop, NULL, NULL);
5f0e6fc7 250 if (status == NSS_STATUS_NOTFOUND)
78d8d211
UD
251 status = _nss_dns_gethostbyname3_r (name, AF_INET, result, buffer,
252 buflen, errnop, h_errnop, NULL, NULL);
5f0e6fc7
RM
253
254 return status;
255}
256
257
31e2791c
UD
258extern enum nss_status _nss_dns_gethostbyaddr2_r (const void *addr,
259 socklen_t len, int af,
260 struct hostent *result,
261 char *buffer, size_t buflen,
262 int *errnop, int *h_errnop,
263 int32_t *ttlp);
264hidden_proto (_nss_dns_gethostbyaddr2_r)
265
5f0e6fc7 266enum nss_status
31e2791c
UD
267_nss_dns_gethostbyaddr2_r (const void *addr, socklen_t len, int af,
268 struct hostent *result, char *buffer, size_t buflen,
269 int *errnop, int *h_errnop, int32_t *ttlp)
5f0e6fc7
RM
270{
271 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
272 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
20cc4c87 273 static const u_char v6local[] = { 0,0, 0,1 };
5f0e6fc7
RM
274 const u_char *uaddr = (const u_char *)addr;
275 struct host_data
276 {
277 char *aliases[MAX_NR_ALIASES];
278 unsigned char host_addr[16]; /* IPv4 or IPv6 */
279 char *h_addr_ptrs[MAX_NR_ADDRS + 1];
280 char linebuffer[0];
281 } *host_data = (struct host_data *) buffer;
16b66062
AJ
282 union
283 {
284 querybuf *buf;
285 u_char *ptr;
286 } host_buffer;
287 querybuf *orig_host_buffer;
1f3f143e 288 char qbuf[MAXDNAME+1], *qp = NULL;
30f22ab1 289 size_t size;
0420d888 290 int n, status;
34816665 291 int olderr = errno;
5f0e6fc7 292
87bb6b6c 293 if (__res_maybe_init (&_res, 0) == -1)
b43b13ac
UD
294 return NSS_STATUS_UNAVAIL;
295
20cc4c87
UD
296 if (af == AF_INET6 && len == IN6ADDRSZ
297 && (memcmp (uaddr, mapped, sizeof mapped) == 0
298 || (memcmp (uaddr, tunnelled, sizeof tunnelled) == 0
299 && memcmp (&uaddr[sizeof tunnelled], v6local, sizeof v6local))))
5f0e6fc7
RM
300 {
301 /* Unmap. */
302 addr += sizeof mapped;
303 uaddr += sizeof mapped;
304 af = AF_INET;
305 len = INADDRSZ;
306 }
307
308 switch (af)
309 {
310 case AF_INET:
311 size = INADDRSZ;
312 break;
313 case AF_INET6:
314 size = IN6ADDRSZ;
315 break;
316 default:
67479a70 317 *errnop = EAFNOSUPPORT;
5f0e6fc7
RM
318 *h_errnop = NETDB_INTERNAL;
319 return NSS_STATUS_UNAVAIL;
320 }
20cc4c87 321 if (size > len)
5f0e6fc7 322 {
67479a70 323 *errnop = EAFNOSUPPORT;
5f0e6fc7
RM
324 *h_errnop = NETDB_INTERNAL;
325 return NSS_STATUS_UNAVAIL;
326 }
327
ee778b56
UD
328 host_buffer.buf = orig_host_buffer = (querybuf *) alloca (1024);
329
5f0e6fc7
RM
330 switch (af)
331 {
332 case AF_INET:
333 sprintf (qbuf, "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
334 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
335 break;
336 case AF_INET6:
ee778b56
UD
337 /* Only lookup with the byte string format if the user wants it. */
338 if (__builtin_expect (_res.options & RES_USEBSTRING, 0))
339 {
340 qp = stpcpy (qbuf, "\\[x");
341 for (n = 0; n < IN6ADDRSZ; ++n)
342 qp += sprintf (qp, "%02hhx", uaddr[n]);
343 strcpy (qp, "].ip6.arpa");
344 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR,
345 host_buffer.buf->buf, 1024, &host_buffer.ptr);
346 if (n >= 0)
347 goto got_it_already;
348 }
5f0e6fc7 349 qp = qbuf;
ee778b56
UD
350 for (n = IN6ADDRSZ - 1; n >= 0; n--)
351 {
352 static const char nibblechar[16] = "0123456789abcdef";
353 *qp++ = nibblechar[uaddr[n] & 0xf];
354 *qp++ = '.';
355 *qp++ = nibblechar[(uaddr[n] >> 4) & 0xf];
356 *qp++ = '.';
357 }
358 strcpy(qp, "ip6.arpa");
5f0e6fc7
RM
359 break;
360 default:
361 /* Cannot happen. */
9fea9ed6 362 break;
5f0e6fc7
RM
363 }
364
16b66062
AJ
365 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
366 1024, &host_buffer.ptr);
340ef046 367 if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0)
62a08e44
UD
368 {
369 strcpy (qp, "ip6.int");
16b66062
AJ
370 n = __libc_res_nquery (&_res, qbuf, C_IN, T_PTR, host_buffer.buf->buf,
371 host_buffer.buf != orig_host_buffer
372 ? MAXPACKET : 1024, &host_buffer.ptr);
62a08e44 373 }
5f0e6fc7 374 if (n < 0)
dd7d45e8
UD
375 {
376 *h_errnop = h_errno;
34816665 377 __set_errno (olderr);
16b66062
AJ
378 if (host_buffer.buf != orig_host_buffer)
379 free (host_buffer.buf);
dd7d45e8
UD
380 return errno == ECONNREFUSED ? NSS_STATUS_UNAVAIL : NSS_STATUS_NOTFOUND;
381 }
5f0e6fc7 382
ee778b56 383 got_it_already:
16b66062 384 status = getanswer_r (host_buffer.buf, n, qbuf, T_PTR, result, buffer, buflen,
31e2791c 385 errnop, h_errnop, 0 /* XXX */, ttlp, NULL);
16b66062
AJ
386 if (host_buffer.buf != orig_host_buffer)
387 free (host_buffer.buf);
5f0e6fc7 388 if (status != NSS_STATUS_SUCCESS)
65b4743a 389 return status;
5f0e6fc7
RM
390
391#ifdef SUNSECURITY
392 This is not implemented because it is not possible to use the current
393 source from bind in a multi-threaded program.
394#endif
395
396 result->h_addrtype = af;
397 result->h_length = len;
63551311 398 memcpy (host_data->host_addr, addr, len);
5f0e6fc7
RM
399 host_data->h_addr_ptrs[0] = (char *) host_data->host_addr;
400 host_data->h_addr_ptrs[1] = NULL;
b455972f
UD
401#if 0
402 /* XXX I think this is wrong. Why should an IPv4 address be
403 converted to IPv6 if the user explicitly asked for IPv4? */
5f0e6fc7
RM
404 if (af == AF_INET && (_res.options & RES_USE_INET6))
405 {
406 map_v4v6_address ((char *) host_data->host_addr,
407 (char *) host_data->host_addr);
408 result->h_addrtype = AF_INET6;
409 result->h_length = IN6ADDRSZ;
410 }
b455972f 411#endif
5f0e6fc7
RM
412 *h_errnop = NETDB_SUCCESS;
413 return NSS_STATUS_SUCCESS;
414}
31e2791c
UD
415hidden_def (_nss_dns_gethostbyaddr2_r)
416
417
418enum nss_status
419_nss_dns_gethostbyaddr_r (const void *addr, socklen_t len, int af,
420 struct hostent *result, char *buffer, size_t buflen,
421 int *errnop, int *h_errnop)
422{
423 return _nss_dns_gethostbyaddr2_r (addr, len, af, result, buffer, buflen,
424 errnop, h_errnop, NULL);
425}
5f0e6fc7 426
5db91571
UD
427#ifdef RESOLVSORT
428static void addrsort (char **ap, int num);
429
430static void
431addrsort (char **ap, int num)
432{
433 int i, j;
434 char **p;
435 short aval[MAX_NR_ADDRS];
436 int needsort = 0;
437
438 p = ap;
439 if (num > MAX_NR_ADDRS)
440 num = MAX_NR_ADDRS;
441 for (i = 0; i < num; i++, p++)
442 {
443 for (j = 0 ; (unsigned)j < _res.nsort; j++)
444 if (_res.sort_list[j].addr.s_addr ==
445 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
446 break;
447 aval[i] = j;
448 if (needsort == 0 && i > 0 && j < aval[i-1])
449 needsort = i;
450 }
451 if (!needsort)
452 return;
453
454 while (needsort++ < num)
455 for (j = needsort - 2; j >= 0; j--)
456 if (aval[j] > aval[j+1])
457 {
458 char *hp;
459
460 i = aval[j];
461 aval[j] = aval[j+1];
462 aval[j+1] = i;
463
464 hp = ap[j];
465 ap[j] = ap[j+1];
466 ap[j+1] = hp;
467 }
468 else
469 break;
470}
471#endif
5f0e6fc7
RM
472
473static enum nss_status
474getanswer_r (const querybuf *answer, int anslen, const char *qname, int qtype,
df4ef2ab 475 struct hostent *result, char *buffer, size_t buflen,
d1fe1f22 476 int *errnop, int *h_errnop, int map, int32_t *ttlp, char **canonp)
5f0e6fc7
RM
477{
478 struct host_data
479 {
480 char *aliases[MAX_NR_ALIASES];
481 unsigned char host_addr[16]; /* IPv4 or IPv6 */
88a96b81 482 char *h_addr_ptrs[0];
2f1687b9
UD
483 } *host_data;
484 int linebuflen;
5f0e6fc7
RM
485 register const HEADER *hp;
486 const u_char *end_of_message, *cp;
487 int n, ancount, qdcount;
488 int haveanswer, had_error;
489 char *bp, **ap, **hap;
845dcb57 490 char tbuf[MAXDNAME];
5f0e6fc7 491 const char *tname;
b455972f 492 int (*name_ok) (const char *);
8d8c6efa 493 u_char packtmp[NS_MAXCDNAME];
b455972f 494 int have_to_map = 0;
d1fe1f22 495 int32_t ttl = 0;
2f1687b9
UD
496 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct host_data);
497 buffer += pad;
498 if (__builtin_expect (buflen < sizeof (struct host_data) + pad, 0))
560d3b76
UD
499 {
500 /* The buffer is too small. */
501 too_small:
502 *errnop = ERANGE;
503 *h_errnop = NETDB_INTERNAL;
504 return NSS_STATUS_TRYAGAIN;
505 }
2f1687b9
UD
506 host_data = (struct host_data *) buffer;
507 linebuflen = buflen - sizeof (struct host_data);
508 if (buflen - sizeof (struct host_data) != linebuflen)
509 linebuflen = INT_MAX;
560d3b76 510
5f0e6fc7
RM
511 tname = qname;
512 result->h_name = NULL;
513 end_of_message = answer->buf + anslen;
514 switch (qtype)
515 {
516 case T_A:
517 case T_AAAA:
518 name_ok = res_hnok;
519 break;
520 case T_PTR:
521 name_ok = res_dnok;
522 break;
523 default:
a0bf6ac7 524 *errnop = ENOENT;
5f0e6fc7
RM
525 return NSS_STATUS_UNAVAIL; /* XXX should be abort(); */
526 }
527
528 /*
529 * find first satisfactory answer
530 */
531 hp = &answer->hdr;
5f0e6fc7
RM
532 ancount = ntohs (hp->ancount);
533 qdcount = ntohs (hp->qdcount);
534 cp = answer->buf + HFIXEDSZ;
b455972f 535 if (__builtin_expect (qdcount, 1) != 1)
5f0e6fc7
RM
536 {
537 *h_errnop = NO_RECOVERY;
538 return NSS_STATUS_UNAVAIL;
539 }
88a96b81
UD
540 if (sizeof (struct host_data) + (ancount + 1) * sizeof (char *) >= buflen)
541 goto too_small;
542 bp = (char *) &host_data->h_addr_ptrs[ancount + 1];
543 linebuflen -= (ancount + 1) * sizeof (char *);
5f0e6fc7 544
8d8c6efa
UD
545 n = __ns_name_unpack (answer->buf, end_of_message, cp,
546 packtmp, sizeof packtmp);
547 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
5f0e6fc7 548 {
b455972f 549 if (__builtin_expect (errno, 0) == EMSGSIZE)
560d3b76 550 goto too_small;
8d8c6efa
UD
551
552 n = -1;
553 }
554
555 if (n > 0 && bp[0] == '.')
556 bp[0] = '\0';
557
558 if (n < 0 || (*name_ok) (bp) == 0)
559 {
560 *errnop = errno;
561 *h_errnop = NO_RECOVERY;
5f0e6fc7
RM
562 return NSS_STATUS_UNAVAIL;
563 }
564 cp += n + QFIXEDSZ;
565
566 if (qtype == T_A || qtype == T_AAAA)
567 {
568 /* res_send() has already verified that the query name is the
569 * same as the one we sent; this just gets the expanded name
570 * (i.e., with the succeeding search-domain tacked on).
571 */
572 n = strlen (bp) + 1; /* for the \0 */
76b87c03
UD
573 if (n >= MAXHOSTNAMELEN)
574 {
67479a70 575 *h_errnop = NO_RECOVERY;
a0bf6ac7 576 *errnop = ENOENT;
76b87c03
UD
577 return NSS_STATUS_TRYAGAIN;
578 }
5f0e6fc7
RM
579 result->h_name = bp;
580 bp += n;
581 linebuflen -= n;
560d3b76
UD
582 if (linebuflen < 0)
583 goto too_small;
5f0e6fc7
RM
584 /* The qname can be abbreviated, but h_name is now absolute. */
585 qname = result->h_name;
586 }
587
588 ap = host_data->aliases;
589 *ap = NULL;
590 result->h_aliases = host_data->aliases;
591 hap = host_data->h_addr_ptrs;
592 *hap = NULL;
593 result->h_addr_list = host_data->h_addr_ptrs;
594 haveanswer = 0;
595 had_error = 0;
596
597 while (ancount-- > 0 && cp < end_of_message && had_error == 0)
598 {
599 int type, class;
600
8d8c6efa
UD
601 n = __ns_name_unpack (answer->buf, end_of_message, cp,
602 packtmp, sizeof packtmp);
603 if (n != -1 && __ns_name_ntop (packtmp, bp, linebuflen) == -1)
604 {
b455972f 605 if (__builtin_expect (errno, 0) == EMSGSIZE)
0cc70fcf 606 goto too_small;
8d8c6efa
UD
607
608 n = -1;
609 }
610
5f0e6fc7
RM
611 if (n < 0 || (*name_ok) (bp) == 0)
612 {
613 ++had_error;
614 continue;
615 }
616 cp += n; /* name */
b43b13ac 617 type = ns_get16 (cp);
5f0e6fc7 618 cp += INT16SZ; /* type */
b43b13ac 619 class = ns_get16 (cp);
d1fe1f22
UD
620 cp += INT16SZ; /* class */
621 ttl = ns_get32 (cp);
622 cp += INT32SZ; /* TTL */
b43b13ac 623 n = ns_get16 (cp);
5f0e6fc7
RM
624 cp += INT16SZ; /* len */
625 if (class != C_IN)
626 {
627 /* XXX - debug? syslog? */
628 cp += n;
629 continue; /* XXX - had_error++ ? */
630 }
631
632 if ((qtype ==T_A || qtype == T_AAAA) && type == T_CNAME)
633 {
634 if (ap >= &host_data->aliases[MAX_NR_ALIASES - 1])
635 continue;
636 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
637 if (n < 0 || (*name_ok) (tbuf) == 0)
638 {
639 ++had_error;
640 continue;
641 }
642 cp += n;
643 /* Store alias. */
644 *ap++ = bp;
645 n = strlen (bp) + 1; /* For the \0. */
b455972f 646 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
76b87c03
UD
647 {
648 ++had_error;
649 continue;
650 }
5f0e6fc7
RM
651 bp += n;
652 linebuflen -= n;
653 /* Get canonical name. */
654 n = strlen (tbuf) + 1; /* For the \0. */
b455972f 655 if (__builtin_expect (n > linebuflen, 0))
0cc70fcf 656 goto too_small;
b455972f 657 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
5f0e6fc7
RM
658 {
659 ++had_error;
660 continue;
661 }
8d8c6efa
UD
662 result->h_name = bp;
663 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
5f0e6fc7
RM
664 linebuflen -= n;
665 continue;
666 }
667
668 if (qtype == T_PTR && type == T_CNAME)
669 {
670 n = dn_expand (answer->buf, end_of_message, cp, tbuf, sizeof tbuf);
da2d1bc5 671 if (n < 0 || res_dnok (tbuf) == 0)
5f0e6fc7
RM
672 {
673 ++had_error;
674 continue;
675 }
676 cp += n;
da2d1bc5 677 /* Get canonical name. */
5f0e6fc7 678 n = strlen (tbuf) + 1; /* For the \0. */
b455972f 679 if (__builtin_expect (n > linebuflen, 0))
0cc70fcf 680 goto too_small;
b455972f 681 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
5f0e6fc7
RM
682 {
683 ++had_error;
684 continue;
685 }
8d8c6efa
UD
686 tname = bp;
687 bp = __mempcpy (bp, tbuf, n); /* Cannot overflow. */
5f0e6fc7
RM
688 linebuflen -= n;
689 continue;
690 }
b455972f
UD
691 if (__builtin_expect (type == T_SIG, 0)
692 || __builtin_expect (type == T_KEY, 0)
693 || __builtin_expect (type == T_NXT, 0))
1bc21e7a
UD
694 {
695 /* We don't support DNSSEC yet. For now, ignore the record
696 and send a low priority message to syslog. */
697 syslog (LOG_DEBUG | LOG_AUTH,
698 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
699 qname, p_class (C_IN), p_type(qtype), p_type (type));
700 cp += n;
701 continue;
702 }
b455972f
UD
703
704 if (type == T_A && qtype == T_AAAA && map)
705 have_to_map = 1;
706 else if (__builtin_expect (type != qtype, 0))
5f0e6fc7
RM
707 {
708 syslog (LOG_NOTICE | LOG_AUTH,
709 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
710 qname, p_class (C_IN), p_type (qtype), p_type (type));
711 cp += n;
712 continue; /* XXX - had_error++ ? */
713 }
714
715 switch (type)
716 {
717 case T_PTR:
8d8c6efa 718 if (__strcasecmp (tname, bp) != 0)
5f0e6fc7
RM
719 {
720 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
721 cp += n;
722 continue; /* XXX - had_error++ ? */
723 }
8d8c6efa
UD
724
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)
728 {
b455972f 729 if (__builtin_expect (errno, 0) == EMSGSIZE)
0cc70fcf 730 goto too_small;
8d8c6efa
UD
731
732 n = -1;
733 }
734
5f0e6fc7
RM
735 if (n < 0 || res_hnok (bp) == 0)
736 {
737 ++had_error;
738 break;
739 }
740#if MULTI_PTRS_ARE_ALIASES
741 cp += n;
742 if (haveanswer == 0)
743 result->h_name = bp;
744 else if (ap < &host_data->aliases[MAXALIASES-1])
745 *ap++ = bp;
746 else
747 n = -1;
748 if (n != -1)
749 {
750 n = strlen (bp) + 1; /* for the \0 */
b455972f 751 if (__builtin_expect (n, 0) >= MAXHOSTNAMELEN)
76b87c03
UD
752 {
753 ++had_error;
754 break;
755 }
5f0e6fc7
RM
756 bp += n;
757 linebuflen -= n;
758 }
759 break;
760#else
761 result->h_name = bp;
b455972f 762 if (have_to_map)
5f0e6fc7
RM
763 {
764 n = strlen (bp) + 1; /* for the \0 */
76b87c03
UD
765 if (n >= MAXHOSTNAMELEN)
766 {
767 ++had_error;
768 break;
769 }
5f0e6fc7
RM
770 bp += n;
771 linebuflen -= n;
772 map_v4v6_hostent (result, &bp, &linebuflen);
773 }
774 *h_errnop = NETDB_SUCCESS;
775 return NSS_STATUS_SUCCESS;
776#endif
777 case T_A:
778 case T_AAAA:
b455972f 779 if (__builtin_expect (strcasecmp (result->h_name, bp), 0) != 0)
5f0e6fc7
RM
780 {
781 syslog (LOG_NOTICE | LOG_AUTH, AskedForGot, result->h_name, bp);
782 cp += n;
783 continue; /* XXX - had_error++ ? */
784 }
1f64ac13 785 if (n != result->h_length)
5f0e6fc7 786 {
1f64ac13
UD
787 cp += n;
788 continue;
5f0e6fc7 789 }
1f64ac13 790 if (!haveanswer)
5f0e6fc7
RM
791 {
792 register int nn;
793
d1fe1f22
UD
794 if (ttlp != NULL && ttl != 0)
795 *ttlp = ttl;
796 if (canonp != NULL)
797 *canonp = bp;
5f0e6fc7
RM
798 result->h_name = bp;
799 nn = strlen (bp) + 1; /* for the \0 */
800 bp += nn;
801 linebuflen -= nn;
802 }
803
560d3b76 804 linebuflen -= sizeof (align) - ((u_long) bp % sizeof (align));
5f0e6fc7
RM
805 bp += sizeof (align) - ((u_long) bp % sizeof (align));
806
b455972f 807 if (__builtin_expect (n > linebuflen, 0))
0cc70fcf 808 goto too_small;
8d8c6efa 809 bp = __mempcpy (*hap++ = bp, cp, n);
5f0e6fc7
RM
810 cp += n;
811 linebuflen -= n;
812 break;
813 default:
814 abort ();
815 }
816 if (had_error == 0)
817 ++haveanswer;
818 }
819
820 if (haveanswer > 0)
821 {
822 *ap = NULL;
823 *hap = NULL;
da2d1bc5 824#if defined RESOLVSORT
5f0e6fc7
RM
825 /*
826 * Note: we sort even if host can take only one address
827 * in its return structures - should give it the "best"
828 * address in that case, not some random one
829 */
830 if (_res.nsort && haveanswer > 1 && qtype == T_A)
831 addrsort (host_data->h_addr_ptrs, haveanswer);
832#endif /*RESOLVSORT*/
833
834 if (result->h_name == NULL)
835 {
836 n = strlen (qname) + 1; /* For the \0. */
8d8c6efa 837 if (n > linebuflen)
0cc70fcf 838 goto too_small;
8d8c6efa 839 if (n >= MAXHOSTNAMELEN)
76b87c03 840 goto no_recovery;
8d8c6efa
UD
841 result->h_name = bp;
842 bp = __mempcpy (bp, qname, n); /* Cannot overflow. */
5f0e6fc7
RM
843 linebuflen -= n;
844 }
845
b455972f 846 if (have_to_map)
5f0e6fc7
RM
847 map_v4v6_hostent (result, &bp, &linebuflen);
848 *h_errnop = NETDB_SUCCESS;
849 return NSS_STATUS_SUCCESS;
850 }
76b87c03
UD
851 no_recovery:
852 *h_errnop = NO_RECOVERY;
a0bf6ac7 853 *errnop = ENOENT;
2884dad4
UD
854 /* Special case here: if the resolver sent a result but it only
855 contains a CNAME while we are looking for a T_A or T_AAAA record,
856 we fail with NOTFOUND instead of TRYAGAIN. */
857 return ((qtype == T_A || qtype == T_AAAA) && ap != host_data->aliases
858 ? NSS_STATUS_NOTFOUND : NSS_STATUS_TRYAGAIN);
5f0e6fc7 859}