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