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.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Parts of this file are plain copies of the file `gethtnamadr.c' from
21 the bind package and it has the following copyright. */
24 * ++Copyright++ 1985, 1988, 1993
26 * Copyright (c) 1985, 1988, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 4. Neither the name of the University nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
55 * Permission to use, copy, modify, and distribute this software for any
56 * purpose with or without fee is hereby granted, provided that the above
57 * copyright notice and this permission notice appear in all copies, and that
58 * the name of Digital Equipment Corporation not be used in advertising or
59 * publicity pertaining to distribution of the document or software without
60 * specific, written prior permission.
62 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
63 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
64 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
65 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
66 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
67 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
68 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
81 #include <sys/syslog.h>
85 /* Get implementation for some internal functions. */
86 #include <resolv/mapv4v6addr.h>
87 #include <resolv/mapv4v6hostent.h>
91 /* Maximum number of aliases we allow. */
92 #define MAX_NR_ALIASES 48
93 #define MAX_NR_ADDRS 48
96 # define MAXPACKET PACKETSZ
98 # define MAXPACKET 1024
100 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
101 #ifdef MAXHOSTNAMELEN
102 # undef MAXHOSTNAMELEN
104 #define MAXHOSTNAMELEN 256
106 static const char AskedForGot
[] = "\
107 gethostby*.getanswer: asked for \"%s\", got \"%s\"";
110 /* We need this time later. */
111 typedef union querybuf
114 u_char buf
[MAXPACKET
];
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);
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
,
131 _nss_dns_gethostbyname2_r (const char *name
, int af
, struct hostent
*result
,
132 char *buffer
, size_t buflen
, int *errnop
,
135 querybuf host_buffer
;
136 char tmp
[NS_MAXDNAME
];
141 if ((_res
.options
& RES_INIT
) == 0 && __res_ninit (&_res
) == -1)
142 return NSS_STATUS_UNAVAIL
;
155 *errnop
= EAFNOSUPPORT
;
156 return NSS_STATUS_UNAVAIL
;
159 result
->h_addrtype
= af
;
160 result
->h_length
= size
;
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.
167 if (strchr (name
, '.') == NULL
168 && (cp
= res_hostalias (&_res
, name
, tmp
, sizeof (tmp
))) != NULL
)
171 n
= res_nsearch (&_res
, name
, C_IN
, type
, host_buffer
.buf
,
172 sizeof (host_buffer
.buf
));
175 enum nss_status status
= (errno
== ECONNREFUSED
176 ? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
);
178 *errnop
= h_errno
== TRY_AGAIN
? EAGAIN
: ENOENT
;
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
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
));
192 result
->h_addrtype
= AF_INET
;
193 result
->h_length
= INADDRSZ
;;
196 return getanswer_r (&host_buffer
, n
, name
, type
, result
, buffer
, buflen
,
197 errnop
, h_errnop
, map
);
202 _nss_dns_gethostbyname_r (const char *name
, struct hostent
*result
,
203 char *buffer
, size_t buflen
, int *errnop
,
206 enum nss_status status
= NSS_STATUS_NOTFOUND
;
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
);
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
)
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
;
230 char *aliases
[MAX_NR_ALIASES
];
231 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
232 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
234 } *host_data
= (struct host_data
*) buffer
;
235 querybuf host_buffer
;
236 char qbuf
[MAXDNAME
+1], *qp
;
240 if ((_res
.options
& RES_INIT
) == 0 && __res_ninit (&_res
) == -1)
241 return NSS_STATUS_UNAVAIL
;
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
))))
249 addr
+= sizeof mapped
;
250 uaddr
+= sizeof mapped
;
264 *errnop
= EAFNOSUPPORT
;
265 *h_errnop
= NETDB_INTERNAL
;
266 return NSS_STATUS_UNAVAIL
;
270 *errnop
= EAFNOSUPPORT
;
271 *h_errnop
= NETDB_INTERNAL
;
272 return NSS_STATUS_UNAVAIL
;
278 sprintf (qbuf
, "%u.%u.%u.%u.in-addr.arpa", (uaddr
[3] & 0xff),
279 (uaddr
[2] & 0xff), (uaddr
[1] & 0xff), (uaddr
[0] & 0xff));
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");
292 n
= res_nquery (&_res
, qbuf
, C_IN
, T_PTR
, (u_char
*)host_buffer
.buf
,
298 return errno
== ECONNREFUSED
? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
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
)
311 This is
not implemented because it is
not possible to use the current
312 source from bind in a multi
-threaded program
.
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
;
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
))
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
;
331 *h_errnop
= NETDB_SUCCESS
;
332 return NSS_STATUS_SUCCESS
;
336 static void addrsort (char **ap
, int num
);
339 addrsort (char **ap
, int num
)
343 short aval
[MAX_NR_ADDRS
];
347 if (num
> MAX_NR_ADDRS
)
349 for (i
= 0; i
< num
; i
++, p
++)
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
))
356 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
362 while (needsort
++ < num
)
363 for (j
= needsort
- 2; j
>= 0; j
--)
364 if (aval
[j
] > aval
[j
+1])
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
)
388 char *aliases
[MAX_NR_ALIASES
];
389 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
390 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
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
;
401 int (*name_ok
) (const char *);
402 u_char packtmp
[NS_MAXCDNAME
];
405 if (__builtin_expect (linebuflen
, 0) < 0)
407 /* The buffer is too small. */
410 *h_errnop
= NETDB_INTERNAL
;
411 return NSS_STATUS_TRYAGAIN
;
415 result
->h_name
= NULL
;
416 end_of_message
= answer
->buf
+ anslen
;
428 return NSS_STATUS_UNAVAIL
; /* XXX should be abort(); */
432 * find first satisfactory answer
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)
441 *h_errnop
= NO_RECOVERY
;
443 return NSS_STATUS_UNAVAIL
;
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)
450 if (__builtin_expect (errno
, 0) == EMSGSIZE
)
456 if (n
> 0 && bp
[0] == '.')
459 if (n
< 0 || (*name_ok
) (bp
) == 0)
462 *h_errnop
= NO_RECOVERY
;
463 return NSS_STATUS_UNAVAIL
;
467 if (qtype
== T_A
|| qtype
== T_AAAA
)
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).
473 n
= strlen (bp
) + 1; /* for the \0 */
474 if (n
>= MAXHOSTNAMELEN
)
476 *h_errnop
= NO_RECOVERY
;
478 return NSS_STATUS_TRYAGAIN
;
485 /* The qname can be abbreviated, but h_name is now absolute. */
486 qname
= result
->h_name
;
489 ap
= host_data
->aliases
;
491 result
->h_aliases
= host_data
->aliases
;
492 hap
= host_data
->h_addr_ptrs
;
494 result
->h_addr_list
= host_data
->h_addr_ptrs
;
498 while (ancount
-- > 0 && cp
< end_of_message
&& had_error
== 0)
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)
506 if (__builtin_expect (errno
, 0) == EMSGSIZE
)
512 if (n
< 0 || (*name_ok
) (bp
) == 0)
518 type
= ns_get16 (cp
);
519 cp
+= INT16SZ
; /* type */
520 class = ns_get16 (cp
);
521 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
523 cp
+= INT16SZ
; /* len */
526 /* XXX - debug? syslog? */
528 continue; /* XXX - had_error++ ? */
531 if ((qtype
==T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
)
533 if (ap
>= &host_data
->aliases
[MAX_NR_ALIASES
- 1])
535 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
536 if (n
< 0 || (*name_ok
) (tbuf
) == 0)
544 n
= strlen (bp
) + 1; /* For the \0. */
545 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
552 /* Get canonical name. */
553 n
= strlen (tbuf
) + 1; /* For the \0. */
554 if (__builtin_expect (n
> linebuflen
, 0))
556 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
562 bp
= __mempcpy (bp
, tbuf
, n
); /* Cannot overflow. */
567 if (qtype
== T_PTR
&& type
== T_CNAME
)
569 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
570 if (n
< 0 || res_dnok (tbuf
) == 0)
576 /* Get canonical name. */
577 n
= strlen (tbuf
) + 1; /* For the \0. */
578 if (__builtin_expect (n
> linebuflen
, 0))
580 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
586 bp
= __mempcpy (bp
, tbuf
, n
); /* Cannot overflow. */
590 if (__builtin_expect (type
== T_SIG
, 0)
591 || __builtin_expect (type
== T_KEY
, 0)
592 || __builtin_expect (type
== T_NXT
, 0))
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
));
603 if (type
== T_A
&& qtype
== T_AAAA
&& map
)
605 else if (__builtin_expect (type
!= qtype
, 0))
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
));
611 continue; /* XXX - had_error++ ? */
617 if (__strcasecmp (tname
, bp
) != 0)
619 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, qname
, bp
);
621 continue; /* XXX - had_error++ ? */
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)
628 if (__builtin_expect (errno
, 0) == EMSGSIZE
)
634 if (n
< 0 || res_hnok (bp
) == 0)
639 #if MULTI_PTRS_ARE_ALIASES
643 else if (ap
< &host_data
->aliases
[MAXALIASES
-1])
649 n
= strlen (bp
) + 1; /* for the \0 */
650 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
663 n
= strlen (bp
) + 1; /* for the \0 */
664 if (n
>= MAXHOSTNAMELEN
)
671 map_v4v6_hostent (result
, &bp
, &linebuflen
);
673 *h_errnop
= NETDB_SUCCESS
;
674 return NSS_STATUS_SUCCESS
;
678 if (__builtin_expect (strcasecmp (result
->h_name
, bp
), 0) != 0)
680 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, result
->h_name
, bp
);
682 continue; /* XXX - had_error++ ? */
684 if (n
!= result
->h_length
)
694 nn
= strlen (bp
) + 1; /* for the \0 */
699 linebuflen
-= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
700 bp
+= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
702 if (__builtin_expect (n
> linebuflen
, 0))
704 if (hap
>= &host_data
->h_addr_ptrs
[MAX_NR_ADDRS
-1])
709 bp
= __mempcpy (*hap
++ = bp
, cp
, n
);
724 #if defined RESOLVSORT
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
730 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
731 addrsort (host_data
->h_addr_ptrs
, haveanswer
);
732 #endif /*RESOLVSORT*/
734 if (result
->h_name
== NULL
)
736 n
= strlen (qname
) + 1; /* For the \0. */
739 if (n
>= MAXHOSTNAMELEN
)
742 bp
= __mempcpy (bp
, qname
, n
); /* Cannot overflow. */
747 map_v4v6_hostent (result
, &bp
, &linebuflen
);
748 *h_errnop
= NETDB_SUCCESS
;
749 return NSS_STATUS_SUCCESS
;
752 *h_errnop
= NO_RECOVERY
;
754 return NSS_STATUS_TRYAGAIN
;