1 /* Copyright (C) 1996, 1997, 1998 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 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
59 * Permission to use, copy, modify, and distribute this software for any
60 * purpose with or without fee is hereby granted, provided that the above
61 * copyright notice and this permission notice appear in all copies, and that
62 * the name of Digital Equipment Corporation not be used in advertising or
63 * publicity pertaining to distribution of the document or software without
64 * specific, written prior permission.
66 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
67 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
68 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
69 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
70 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
71 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
72 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
80 #include <glibc-compat/include/netdb.h>
85 #include <sys/syslog.h>
89 /* Get implementation for some internal functions. */
90 #include "../resolv/mapv4v6addr.h"
91 #include "../resolv/mapv4v6hostent.h"
93 /* Maximum number of aliases we allow. */
94 #define MAX_NR_ALIASES 48
95 #define MAX_NR_ADDRS 48
98 # define MAXPACKET PACKETSZ
100 # define MAXPACKET 65536
102 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
103 #ifdef MAXHOSTNAMELEN
104 # undef MAXHOSTNAMELEN
106 #define MAXHOSTNAMELEN 256
108 static const char AskedForGot
[] = "\
109 gethostby*.getanswer: asked for \"%s\", got \"%s\"";
112 /* We need this time later. */
113 typedef union querybuf
116 u_char buf
[MAXPACKET
];
120 static enum nss_status
getanswer_r (const querybuf
*answer
, int anslen
,
121 const char *qname
, int qtype
,
122 struct hostent
*result
, char *buffer
,
123 size_t buflen
, int *h_errnop
);
126 _nss_dns_gethostbyname2_r (const char *name
, int af
, struct hostent
*result
,
127 char *buffer
, size_t buflen
, int *h_errnop
)
134 querybuf
*orig_host_buffer
;
137 enum nss_status status
;
139 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1)
141 *h_errnop
= NETDB_INTERNAL
;
142 return NSS_STATUS_UNAVAIL
;
155 *h_errnop
= NETDB_INTERNAL
;
156 __set_errno (EAFNOSUPPORT
);
157 return NSS_STATUS_UNAVAIL
;
160 result
->h_addrtype
= af
;
161 result
->h_length
= size
;
164 * if there aren't any dots, it could be a user-level alias.
165 * this is also done in res_query() since we are not the only
166 * function that looks up host names.
168 if (strchr (name
, '.') == NULL
&& (cp
= __hostalias (name
)) != NULL
)
171 host_buffer
.buf
= orig_host_buffer
= (querybuf
*) alloca (1024);
173 n
= __libc_res_nsearch (&_res
, name
, C_IN
, type
, host_buffer
.buf
->buf
, 1024,
178 if (host_buffer
.buf
!= orig_host_buffer
)
179 free (host_buffer
.buf
);
180 return errno
== ECONNREFUSED
? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
183 status
= getanswer_r (host_buffer
.buf
, n
, name
, type
, result
, buffer
, buflen
,
185 if (host_buffer
.buf
!= orig_host_buffer
)
186 free (host_buffer
.buf
);
192 _nss_dns_gethostbyname_r (const char *name
, struct hostent
*result
,
193 char *buffer
, size_t buflen
, int *h_errnop
)
195 enum nss_status status
= NSS_STATUS_NOTFOUND
;
197 if (_res
.options
& RES_USE_INET6
)
198 status
= _nss_dns_gethostbyname2_r (name
, AF_INET6
, result
, buffer
,
200 if (status
== NSS_STATUS_NOTFOUND
)
201 status
= _nss_dns_gethostbyname2_r (name
, AF_INET
, result
, buffer
,
209 _nss_dns_gethostbyaddr_r (const char *addr
, int len
, int af
,
210 struct hostent
*result
, char *buffer
, size_t buflen
,
213 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
214 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
215 const u_char
*uaddr
= (const u_char
*)addr
;
218 char *aliases
[MAX_NR_ALIASES
];
219 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
220 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
222 } *host_data
= (struct host_data
*) buffer
;
228 querybuf
*orig_host_buffer
;
229 char qbuf
[MAXDNAME
+1], *qp
;
232 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1)
234 *h_errnop
= NETDB_INTERNAL
;
235 return NSS_STATUS_UNAVAIL
;
238 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
239 (memcmp (uaddr
, mapped
, sizeof mapped
) == 0
240 || memcmp (uaddr
, tunnelled
, sizeof tunnelled
) == 0))
243 addr
+= sizeof mapped
;
244 uaddr
+= sizeof mapped
;
258 __set_errno (EAFNOSUPPORT
);
259 *h_errnop
= NETDB_INTERNAL
;
260 return NSS_STATUS_UNAVAIL
;
264 __set_errno (EAFNOSUPPORT
);
265 *h_errnop
= NETDB_INTERNAL
;
266 return NSS_STATUS_UNAVAIL
;
272 sprintf (qbuf
, "%u.%u.%u.%u.in-addr.arpa", (uaddr
[3] & 0xff),
273 (uaddr
[2] & 0xff), (uaddr
[1] & 0xff), (uaddr
[0] & 0xff));
277 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--)
278 qp
+= sprintf (qp
, "%x.%x.", uaddr
[n
] & 0xf, (uaddr
[n
] >> 4) & 0xf);
279 strcpy(qp
, "ip6.int");
286 host_buffer
.buf
= orig_host_buffer
= (querybuf
*) alloca (1024);
288 n
= __libc_res_nquery (&_res
, qbuf
, C_IN
, T_PTR
, host_buffer
.buf
->buf
,
289 1024, &host_buffer
.ptr
);
293 if (host_buffer
.buf
!= orig_host_buffer
)
294 free (host_buffer
.buf
);
295 return errno
== ECONNREFUSED
? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
298 status
= getanswer_r (host_buffer
.buf
, n
, qbuf
, T_PTR
, result
, buffer
, buflen
,
300 if (host_buffer
.buf
!= orig_host_buffer
)
301 free (host_buffer
.buf
);
302 if (status
!= NSS_STATUS_SUCCESS
)
309 This is
not implemented because it is
not possible to use the current
310 source from bind in a multi
-threaded program
.
313 result
->h_addrtype
= af
;
314 result
->h_length
= len
;
315 memcpy (host_data
->host_addr
, addr
, len
);
316 host_data
->h_addr_ptrs
[0] = (char *) host_data
->host_addr
;
317 host_data
->h_addr_ptrs
[1] = NULL
;
318 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
))
320 map_v4v6_address ((char *) host_data
->host_addr
,
321 (char *) host_data
->host_addr
);
322 result
->h_addrtype
= AF_INET6
;
323 result
->h_length
= IN6ADDRSZ
;
325 *h_errnop
= NETDB_SUCCESS
;
326 return NSS_STATUS_SUCCESS
;
330 static enum nss_status
331 getanswer_r (const querybuf
*answer
, int anslen
, const char *qname
, int qtype
,
332 struct hostent
*result
, char *buffer
, size_t buflen
,
337 char *aliases
[MAX_NR_ALIASES
];
338 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
339 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
341 } *host_data
= (struct host_data
*) buffer
;
342 int linebuflen
= buflen
- offsetof (struct host_data
, linebuffer
);
343 register const HEADER
*hp
;
344 const u_char
*end_of_message
, *cp
;
345 int n
, ancount
, qdcount
;
346 int haveanswer
, had_error
;
347 char *bp
, **ap
, **hap
;
350 int (*name_ok
) (const char *);
353 result
->h_name
= NULL
;
354 end_of_message
= answer
->buf
+ anslen
;
365 return NSS_STATUS_UNAVAIL
; /* XXX should be abort(); */
369 * find first satisfactory answer
372 bp
= host_data
->linebuffer
;
373 ancount
= ntohs (hp
->ancount
);
374 qdcount
= ntohs (hp
->qdcount
);
375 cp
= answer
->buf
+ HFIXEDSZ
;
378 *h_errnop
= NO_RECOVERY
;
379 return NSS_STATUS_UNAVAIL
;
382 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
383 if (n
< 0 || (*name_ok
) (bp
) == 0)
385 *h_errnop
= NO_RECOVERY
;
386 return NSS_STATUS_UNAVAIL
;
390 if (qtype
== T_A
|| qtype
== T_AAAA
)
392 /* res_send() has already verified that the query name is the
393 * same as the one we sent; this just gets the expanded name
394 * (i.e., with the succeeding search-domain tacked on).
396 n
= strlen (bp
) + 1; /* for the \0 */
397 if (n
>= MAXHOSTNAMELEN
)
399 __set_h_errno (NO_RECOVERY
);
400 return NSS_STATUS_TRYAGAIN
;
405 /* The qname can be abbreviated, but h_name is now absolute. */
406 qname
= result
->h_name
;
409 ap
= host_data
->aliases
;
411 result
->h_aliases
= host_data
->aliases
;
412 hap
= host_data
->h_addr_ptrs
;
414 result
->h_addr_list
= host_data
->h_addr_ptrs
;
418 while (ancount
-- > 0 && cp
< end_of_message
&& had_error
== 0)
422 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
423 if (n
< 0 || (*name_ok
) (bp
) == 0)
429 type
= _getshort (cp
);
430 cp
+= INT16SZ
; /* type */
431 class = _getshort(cp
);
432 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
434 cp
+= INT16SZ
; /* len */
437 /* XXX - debug? syslog? */
439 continue; /* XXX - had_error++ ? */
442 if ((qtype
==T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
)
444 if (ap
>= &host_data
->aliases
[MAX_NR_ALIASES
- 1])
446 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
447 if (n
< 0 || (*name_ok
) (tbuf
) == 0)
455 n
= strlen (bp
) + 1; /* For the \0. */
456 if (n
>= MAXHOSTNAMELEN
)
463 /* Get canonical name. */
464 n
= strlen (tbuf
) + 1; /* For the \0. */
465 if ((size_t) n
> linebuflen
|| n
>= MAXHOSTNAMELEN
)
470 strcpy (bp
, tbuf
); /* Cannot overflow. */
477 if (qtype
== T_PTR
&& type
== T_CNAME
)
479 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
480 if (n
< 0 || res_dnok (tbuf
) == 0)
486 /* Get canonical name. */
487 n
= strlen (tbuf
) + 1; /* For the \0. */
488 if ((size_t) n
> linebuflen
|| n
>= MAXHOSTNAMELEN
)
493 strcpy (bp
, tbuf
); /* Cannot overflow. */
501 syslog (LOG_NOTICE
| LOG_AUTH
,
502 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
503 qname
, p_class (C_IN
), p_type (qtype
), p_type (type
));
505 continue; /* XXX - had_error++ ? */
511 if (strcasecmp (tname
, bp
) != 0)
513 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, qname
, bp
);
515 continue; /* XXX - had_error++ ? */
517 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
518 if (n
< 0 || res_hnok (bp
) == 0)
523 #if MULTI_PTRS_ARE_ALIASES
527 else if (ap
< &host_data
->aliases
[MAXALIASES
-1])
533 n
= strlen (bp
) + 1; /* for the \0 */
534 if (n
>= MAXHOSTNAMELEN
)
545 if (_res
.options
& RES_USE_INET6
)
547 n
= strlen (bp
) + 1; /* for the \0 */
548 if (n
>= MAXHOSTNAMELEN
)
555 map_v4v6_hostent (result
, &bp
, &linebuflen
);
557 *h_errnop
= NETDB_SUCCESS
;
558 return NSS_STATUS_SUCCESS
;
562 if (strcasecmp (result
->h_name
, bp
) != 0)
564 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, result
->h_name
, bp
);
566 continue; /* XXX - had_error++ ? */
568 if (n
!= result
->h_length
)
578 nn
= strlen (bp
) + 1; /* for the \0 */
583 linebuflen
-= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
584 bp
+= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
591 if (hap
>= &host_data
->h_addr_ptrs
[MAX_NR_ADDRS
-1])
596 memcpy (*hap
++ = bp
, cp
, n
);
612 #if defined(RESOLVSORT)
614 * Note: we sort even if host can take only one address
615 * in its return structures - should give it the "best"
616 * address in that case, not some random one
618 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
619 addrsort (host_data
->h_addr_ptrs
, haveanswer
);
620 #endif /*RESOLVSORT*/
622 if (result
->h_name
== NULL
)
624 n
= strlen (qname
) + 1; /* For the \0. */
625 if (n
> linebuflen
|| n
>= MAXHOSTNAMELEN
)
627 strcpy (bp
, qname
); /* Cannot overflow. */
633 if (_res
.options
& RES_USE_INET6
)
634 map_v4v6_hostent (result
, &bp
, &linebuflen
);
635 *h_errnop
= NETDB_SUCCESS
;
636 return NSS_STATUS_SUCCESS
;
639 *h_errnop
= NO_RECOVERY
;
640 return NSS_STATUS_TRYAGAIN
;