1 /* Copyright (C) 1996, 1997, 1998, 1999, 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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>
89 /* Maximum number of aliases we allow. */
90 #define MAX_NR_ALIASES 48
91 #define MAX_NR_ADDRS 48
94 # define MAXPACKET PACKETSZ
96 # define MAXPACKET 1024
98 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
100 # undef MAXHOSTNAMELEN
102 #define MAXHOSTNAMELEN 256
104 static const char AskedForGot
[] = "\
105 gethostby*.getanswer: asked for \"%s\", got \"%s\"";
108 /* We need this time later. */
109 typedef union querybuf
112 u_char buf
[MAXPACKET
];
115 /* These functions are defined in res_comp.c. */
116 #define NS_MAXCDNAME 255 /* maximum compressed domain name */
117 extern int __ns_name_ntop (const u_char
*, char *, size_t);
118 extern int __ns_name_unpack (const u_char
*, const u_char
*,
119 const u_char
*, u_char
*, size_t);
122 static enum nss_status
getanswer_r (const querybuf
*answer
, int anslen
,
123 const char *qname
, int qtype
,
124 struct hostent
*result
, char *buffer
,
125 size_t buflen
, int *errnop
, int *h_errnop
,
129 _nss_dns_gethostbyname2_r (const char *name
, int af
, struct hostent
*result
,
130 char *buffer
, size_t buflen
, int *errnop
,
133 querybuf host_buffer
;
134 char tmp
[NS_MAXDNAME
];
139 if ((_res
.options
& RES_INIT
) == 0 && __res_ninit (&_res
) == -1)
140 return NSS_STATUS_UNAVAIL
;
153 *errnop
= EAFNOSUPPORT
;
154 return NSS_STATUS_UNAVAIL
;
157 result
->h_addrtype
= af
;
158 result
->h_length
= size
;
161 * if there aren't any dots, it could be a user-level alias.
162 * this is also done in res_query() since we are not the only
163 * function that looks up host names.
165 if (strchr (name
, '.') == NULL
166 && (cp
= res_hostalias (&_res
, name
, tmp
, sizeof (tmp
))) != NULL
)
169 n
= res_nsearch (&_res
, name
, C_IN
, type
, host_buffer
.buf
,
170 sizeof (host_buffer
.buf
));
173 enum nss_status status
= (errno
== ECONNREFUSED
174 ? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
);
176 *errnop
= h_errno
== TRY_AGAIN
? EAGAIN
: ENOENT
;
178 /* If we are looking for a IPv6 address and mapping is enabled
179 by having the RES_USE_INET6 bit in _res.options set, we try
181 if (af
== AF_INET6
&& (_res
.options
& RES_USE_INET6
))
182 n
= res_nsearch (&_res
, name
, C_IN
, T_A
, host_buffer
.buf
,
183 sizeof (host_buffer
.buf
));
190 result
->h_addrtype
= AF_INET
;
191 result
->h_length
= INADDRSZ
;;
194 return getanswer_r (&host_buffer
, n
, name
, type
, result
, buffer
, buflen
,
195 errnop
, h_errnop
, map
);
200 _nss_dns_gethostbyname_r (const char *name
, struct hostent
*result
,
201 char *buffer
, size_t buflen
, int *errnop
,
204 enum nss_status status
= NSS_STATUS_NOTFOUND
;
206 if (_res
.options
& RES_USE_INET6
)
207 status
= _nss_dns_gethostbyname2_r (name
, AF_INET6
, result
, buffer
,
208 buflen
, errnop
, h_errnop
);
209 if (status
== NSS_STATUS_NOTFOUND
)
210 status
= _nss_dns_gethostbyname2_r (name
, AF_INET
, result
, buffer
,
211 buflen
, errnop
, h_errnop
);
218 _nss_dns_gethostbyaddr_r (const char *addr
, size_t len
, int af
,
219 struct hostent
*result
, char *buffer
, size_t buflen
,
220 int *errnop
, int *h_errnop
)
222 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
223 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
224 static const u_char v6local
[] = { 0,0, 0,1 };
225 const u_char
*uaddr
= (const u_char
*)addr
;
228 char *aliases
[MAX_NR_ALIASES
];
229 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
230 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
232 } *host_data
= (struct host_data
*) buffer
;
233 querybuf host_buffer
;
234 char qbuf
[MAXDNAME
+1], *qp
;
238 if ((_res
.options
& RES_INIT
) == 0 && __res_ninit (&_res
) == -1)
239 return NSS_STATUS_UNAVAIL
;
241 if (af
== AF_INET6
&& len
== IN6ADDRSZ
242 && (memcmp (uaddr
, mapped
, sizeof mapped
) == 0
243 || (memcmp (uaddr
, tunnelled
, sizeof tunnelled
) == 0
244 && memcmp (&uaddr
[sizeof tunnelled
], v6local
, sizeof v6local
))))
247 addr
+= sizeof mapped
;
248 uaddr
+= sizeof mapped
;
262 *errnop
= EAFNOSUPPORT
;
263 *h_errnop
= NETDB_INTERNAL
;
264 return NSS_STATUS_UNAVAIL
;
268 *errnop
= EAFNOSUPPORT
;
269 *h_errnop
= NETDB_INTERNAL
;
270 return NSS_STATUS_UNAVAIL
;
276 sprintf (qbuf
, "%u.%u.%u.%u.in-addr.arpa", (uaddr
[3] & 0xff),
277 (uaddr
[2] & 0xff), (uaddr
[1] & 0xff), (uaddr
[0] & 0xff));
281 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--)
282 qp
+= sprintf (qp
, "%x.%x.", uaddr
[n
] & 0xf, (uaddr
[n
] >> 4) & 0xf);
283 strcpy (qp
, "ip6.int");
289 n
= res_nquery (&_res
, qbuf
, C_IN
, T_PTR
, (u_char
*)host_buffer
.buf
,
295 return errno
== ECONNREFUSED
? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
298 status
= getanswer_r (&host_buffer
, n
, qbuf
, T_PTR
, result
, buffer
, buflen
,
299 errnop
, h_errnop
, 0 /* XXX */);
300 if (status
!= NSS_STATUS_SUCCESS
)
308 This is
not implemented because it is
not possible to use the current
309 source from bind in a multi
-threaded program
.
312 result
->h_addrtype
= af
;
313 result
->h_length
= len
;
314 memcpy (host_data
->host_addr
, addr
, len
);
315 host_data
->h_addr_ptrs
[0] = (char *) host_data
->host_addr
;
316 host_data
->h_addr_ptrs
[1] = NULL
;
318 /* XXX I think this is wrong. Why should an IPv4 address be
319 converted to IPv6 if the user explicitly asked for IPv4? */
320 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
))
322 map_v4v6_address ((char *) host_data
->host_addr
,
323 (char *) host_data
->host_addr
);
324 result
->h_addrtype
= AF_INET6
;
325 result
->h_length
= IN6ADDRSZ
;
328 *h_errnop
= NETDB_SUCCESS
;
329 return NSS_STATUS_SUCCESS
;
333 static enum nss_status
334 getanswer_r (const querybuf
*answer
, int anslen
, const char *qname
, int qtype
,
335 struct hostent
*result
, char *buffer
, size_t buflen
,
336 int *errnop
, int *h_errnop
, int map
)
340 char *aliases
[MAX_NR_ALIASES
];
341 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
342 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
344 } *host_data
= (struct host_data
*) buffer
;
345 int linebuflen
= buflen
- offsetof (struct host_data
, linebuffer
);
346 register const HEADER
*hp
;
347 const u_char
*end_of_message
, *cp
;
348 int n
, ancount
, qdcount
;
349 int haveanswer
, had_error
;
350 char *bp
, **ap
, **hap
;
353 int (*name_ok
) (const char *);
354 u_char packtmp
[NS_MAXCDNAME
];
357 if (__builtin_expect (linebuflen
, 0) < 0)
359 /* The buffer is too small. */
362 *h_errnop
= NETDB_INTERNAL
;
363 return NSS_STATUS_TRYAGAIN
;
367 result
->h_name
= NULL
;
368 end_of_message
= answer
->buf
+ anslen
;
380 return NSS_STATUS_UNAVAIL
; /* XXX should be abort(); */
384 * find first satisfactory answer
387 bp
= host_data
->linebuffer
;
388 ancount
= ntohs (hp
->ancount
);
389 qdcount
= ntohs (hp
->qdcount
);
390 cp
= answer
->buf
+ HFIXEDSZ
;
391 if (__builtin_expect (qdcount
, 1) != 1)
393 *h_errnop
= NO_RECOVERY
;
395 return NSS_STATUS_UNAVAIL
;
398 n
= __ns_name_unpack (answer
->buf
, end_of_message
, cp
,
399 packtmp
, sizeof packtmp
);
400 if (n
!= -1 && __ns_name_ntop (packtmp
, bp
, linebuflen
) == -1)
402 if (__builtin_expect (errno
, 0) == EMSGSIZE
)
408 if (n
> 0 && bp
[0] == '.')
411 if (n
< 0 || (*name_ok
) (bp
) == 0)
414 *h_errnop
= NO_RECOVERY
;
415 return NSS_STATUS_UNAVAIL
;
419 if (qtype
== T_A
|| qtype
== T_AAAA
)
421 /* res_send() has already verified that the query name is the
422 * same as the one we sent; this just gets the expanded name
423 * (i.e., with the succeeding search-domain tacked on).
425 n
= strlen (bp
) + 1; /* for the \0 */
426 if (n
>= MAXHOSTNAMELEN
)
428 *h_errnop
= NO_RECOVERY
;
430 return NSS_STATUS_TRYAGAIN
;
437 /* The qname can be abbreviated, but h_name is now absolute. */
438 qname
= result
->h_name
;
441 ap
= host_data
->aliases
;
443 result
->h_aliases
= host_data
->aliases
;
444 hap
= host_data
->h_addr_ptrs
;
446 result
->h_addr_list
= host_data
->h_addr_ptrs
;
450 while (ancount
-- > 0 && cp
< end_of_message
&& had_error
== 0)
454 n
= __ns_name_unpack (answer
->buf
, end_of_message
, cp
,
455 packtmp
, sizeof packtmp
);
456 if (n
!= -1 && __ns_name_ntop (packtmp
, bp
, linebuflen
) == -1)
458 if (__builtin_expect (errno
, 0) == EMSGSIZE
)
464 if (n
< 0 || (*name_ok
) (bp
) == 0)
470 type
= ns_get16 (cp
);
471 cp
+= INT16SZ
; /* type */
472 class = ns_get16 (cp
);
473 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
475 cp
+= INT16SZ
; /* len */
478 /* XXX - debug? syslog? */
480 continue; /* XXX - had_error++ ? */
483 if ((qtype
==T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
)
485 if (ap
>= &host_data
->aliases
[MAX_NR_ALIASES
- 1])
487 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
488 if (n
< 0 || (*name_ok
) (tbuf
) == 0)
496 n
= strlen (bp
) + 1; /* For the \0. */
497 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
504 /* Get canonical name. */
505 n
= strlen (tbuf
) + 1; /* For the \0. */
506 if (__builtin_expect (n
> linebuflen
, 0))
508 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
514 bp
= __mempcpy (bp
, tbuf
, n
); /* Cannot overflow. */
519 if (qtype
== T_PTR
&& type
== T_CNAME
)
521 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
522 if (n
< 0 || res_dnok (tbuf
) == 0)
528 /* Get canonical name. */
529 n
= strlen (tbuf
) + 1; /* For the \0. */
530 if (__builtin_expect (n
> linebuflen
, 0))
532 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
538 bp
= __mempcpy (bp
, tbuf
, n
); /* Cannot overflow. */
542 if (__builtin_expect (type
== T_SIG
, 0)
543 || __builtin_expect (type
== T_KEY
, 0)
544 || __builtin_expect (type
== T_NXT
, 0))
546 /* We don't support DNSSEC yet. For now, ignore the record
547 and send a low priority message to syslog. */
548 syslog (LOG_DEBUG
| LOG_AUTH
,
549 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
550 qname
, p_class (C_IN
), p_type(qtype
), p_type (type
));
555 if (type
== T_A
&& qtype
== T_AAAA
&& map
)
557 else if (__builtin_expect (type
!= qtype
, 0))
559 syslog (LOG_NOTICE
| LOG_AUTH
,
560 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
561 qname
, p_class (C_IN
), p_type (qtype
), p_type (type
));
563 continue; /* XXX - had_error++ ? */
569 if (__strcasecmp (tname
, bp
) != 0)
571 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, qname
, bp
);
573 continue; /* XXX - had_error++ ? */
576 n
= __ns_name_unpack (answer
->buf
, end_of_message
, cp
,
577 packtmp
, sizeof packtmp
);
578 if (n
!= -1 && __ns_name_ntop (packtmp
, bp
, linebuflen
) == -1)
580 if (__builtin_expect (errno
, 0) == EMSGSIZE
)
586 if (n
< 0 || res_hnok (bp
) == 0)
591 #if MULTI_PTRS_ARE_ALIASES
595 else if (ap
< &host_data
->aliases
[MAXALIASES
-1])
601 n
= strlen (bp
) + 1; /* for the \0 */
602 if (__builtin_expect (n
, 0) >= MAXHOSTNAMELEN
)
615 n
= strlen (bp
) + 1; /* for the \0 */
616 if (n
>= MAXHOSTNAMELEN
)
623 map_v4v6_hostent (result
, &bp
, &linebuflen
);
625 *h_errnop
= NETDB_SUCCESS
;
626 return NSS_STATUS_SUCCESS
;
630 if (__builtin_expect (strcasecmp (result
->h_name
, bp
), 0) != 0)
632 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, result
->h_name
, bp
);
634 continue; /* XXX - had_error++ ? */
636 if (n
!= result
->h_length
)
646 nn
= strlen (bp
) + 1; /* for the \0 */
651 linebuflen
-= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
652 bp
+= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
654 if (__builtin_expect (n
> linebuflen
, 0))
656 if (hap
>= &host_data
->h_addr_ptrs
[MAX_NR_ADDRS
-1])
661 bp
= __mempcpy (*hap
++ = bp
, cp
, n
);
676 #if defined RESOLVSORT
678 * Note: we sort even if host can take only one address
679 * in its return structures - should give it the "best"
680 * address in that case, not some random one
682 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
683 addrsort (host_data
->h_addr_ptrs
, haveanswer
);
684 #endif /*RESOLVSORT*/
686 if (result
->h_name
== NULL
)
688 n
= strlen (qname
) + 1; /* For the \0. */
691 if (n
>= MAXHOSTNAMELEN
)
694 bp
= __mempcpy (bp
, qname
, n
); /* Cannot overflow. */
699 map_v4v6_hostent (result
, &bp
, &linebuflen
);
700 *h_errnop
= NETDB_SUCCESS
;
701 return NSS_STATUS_SUCCESS
;
704 *h_errnop
= NO_RECOVERY
;
706 return NSS_STATUS_TRYAGAIN
;