1 /* Copyright (C) 1996 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
17 not, 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
85 #include <sys/syslog.h>
87 #include <netinet/in.h>
88 #include <arpa/inet.h>
89 #include <arpa/nameser.h>
94 /* Get implementation for some internal functions. */
95 #include "../resolv/mapv4v6addr.h"
96 #include "../resolv/mapv4v6hostent.h"
98 /* Maximum number of aliases we allow. */
99 #define MAX_NR_ALIASES 48
100 #define MAX_NR_ADDRS 48
103 # define MAXPACKET PACKETSZ
105 # define MAXPACKET 1024
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
,
123 char *buffer
, int buflen
, int *h_errnop
);
126 _nss_dns_gethostbyname2_r (const char *name
, int af
, struct hostent
*result
,
127 char *buffer
, int buflen
, int *h_errnop
)
131 char *aliases
[MAX_NR_ALIASES
];
132 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
133 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
135 } *host_data
= (struct host_data
*) buffer
;
136 int linebuflen
= buflen
- offsetof (struct host_data
, linebuffer
);
137 querybuf host_buffer
;
151 *h_errnop
= NETDB_INTERNAL
;
152 errno
= EAFNOSUPPORT
;
153 return NSS_STATUS_UNAVAIL
;
156 result
->h_addrtype
= af
;
157 result
->h_length
= size
;
160 * if there aren't any dots, it could be a user-level alias.
161 * this is also done in res_query() since we are not the only
162 * function that looks up host names.
164 if (strchr (name
, '.') == NULL
&& (cp
= __hostalias (name
)) != NULL
)
168 * disallow names consisting only of digits/dots, unless
171 if (isdigit (name
[0]))
172 for (cp
= name
;; ++cp
)
181 * All-numeric, no dot at the end. Fake up a hostent
182 * as if we'd actually done a lookup.
184 if (inet_pton (af
, name
, host_data
->host_addr
) <= 0)
186 *h_errnop
= HOST_NOT_FOUND
;
187 return NSS_STATUS_NOTFOUND
;
190 bp
= __stpncpy (host_data
->linebuffer
, name
, linebuflen
);
191 host_data
->linebuffer
[linebuflen
- 1] = '\0';
192 linebuflen
-= bp
- host_data
->linebuffer
;
194 result
->h_name
= host_data
->linebuffer
;
195 result
->h_aliases
= host_data
->aliases
;
196 host_data
->aliases
[0] = NULL
;
197 host_data
->h_addr_ptrs
[0] = (char *) host_data
->host_addr
;
198 host_data
->h_addr_ptrs
[1] = NULL
;
199 result
->h_addr_list
= host_data
->h_addr_ptrs
;
201 if (_res
.options
& RES_USE_INET6
)
202 map_v4v6_hostent (result
, &bp
, &linebuflen
);
203 *h_errnop
= NETDB_SUCCESS
;
204 return NSS_STATUS_SUCCESS
;
206 if (!isdigit (*cp
) && *cp
!= '.')
209 if (isxdigit (name
[0]) || name
[0] == ':')
210 for (cp
= name
;; ++cp
)
219 * All-IPv6-legal, no dot at the end. Fake up a hostent
220 * as if we'd actually done a lookup.
222 if (inet_pton (af
, name
, host_data
->host_addr
) <= 0)
224 *h_errnop
= HOST_NOT_FOUND
;
225 return NSS_STATUS_NOTFOUND
;
228 bp
= __stpncpy (host_data
->linebuffer
, name
, linebuflen
);
229 host_data
->linebuffer
[linebuflen
- 1] = '\0';
230 linebuflen
-= bp
- host_data
->linebuffer
;
232 result
->h_name
= host_data
->linebuffer
;
233 result
->h_aliases
= host_data
->aliases
;
234 host_data
->aliases
[0] = NULL
;
235 host_data
->h_addr_ptrs
[0] = (char *) host_data
->host_addr
;
236 host_data
->h_addr_ptrs
[1] = NULL
;
237 result
->h_addr_list
= host_data
->h_addr_ptrs
;
238 *h_errnop
= NETDB_SUCCESS
;
239 return NSS_STATUS_SUCCESS
;
241 if (!isxdigit (*cp
) && *cp
!= ':' && *cp
!= '.')
245 n
= res_search (name
, C_IN
, type
, host_buffer
.buf
, sizeof (host_buffer
));
247 return (errno
== ECONNREFUSED
248 || errno
== EPFNOSUPPORT
249 || errno
== EAFNOSUPPORT
)
250 ? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
252 return getanswer_r (&host_buffer
, n
, name
, type
, result
, buffer
, buflen
,
258 _nss_dns_gethostbyname_r (const char *name
, struct hostent
*result
,
259 char *buffer
, int buflen
, int *h_errnop
)
261 enum nss_status status
= NSS_STATUS_NOTFOUND
;
263 if (_res
.options
& RES_USE_INET6
)
264 status
= _nss_dns_gethostbyname2_r (name
, AF_INET6
, result
, buffer
,
266 if (status
== NSS_STATUS_NOTFOUND
)
267 status
= _nss_dns_gethostbyname2_r (name
, AF_INET
, result
, buffer
,
275 _nss_dns_gethostbyaddr_r (const char *addr
, int len
, int af
,
276 struct hostent
*result
, char *buffer
, int buflen
,
279 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
280 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
281 const u_char
*uaddr
= (const u_char
*)addr
;
284 char *aliases
[MAX_NR_ALIASES
];
285 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
286 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
288 } *host_data
= (struct host_data
*) buffer
;
289 querybuf host_buffer
;
290 char qbuf
[MAXDNAME
+1], *qp
;
293 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
294 (bcmp (uaddr
, mapped
, sizeof mapped
) == 0
295 || bcmp (uaddr
, tunnelled
, sizeof tunnelled
) == 0))
298 addr
+= sizeof mapped
;
299 uaddr
+= sizeof mapped
;
313 errno
= EAFNOSUPPORT
;
314 *h_errnop
= NETDB_INTERNAL
;
315 return NSS_STATUS_UNAVAIL
;
319 errno
= EAFNOSUPPORT
;
320 *h_errnop
= NETDB_INTERNAL
;
321 return NSS_STATUS_UNAVAIL
;
327 sprintf (qbuf
, "%u.%u.%u.%u.in-addr.arpa", (uaddr
[3] & 0xff),
328 (uaddr
[2] & 0xff), (uaddr
[1] & 0xff), (uaddr
[0] & 0xff));
332 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--)
333 qp
+= sprintf (qp
, "%x.%x.", uaddr
[n
] & 0xf, (uaddr
[n
] >> 4) & 0xf);
334 strcpy(qp
, "ip6.int");
340 n
= res_query (qbuf
, C_IN
, T_PTR
, (u_char
*)host_buffer
.buf
,
343 return (errno
== ECONNREFUSED
344 || errno
== EPFNOSUPPORT
345 || errno
== EAFNOSUPPORT
)
346 ? NSS_STATUS_UNAVAIL
: NSS_STATUS_NOTFOUND
;
348 status
= getanswer_r (&host_buffer
, n
, qbuf
, T_PTR
, result
, buffer
, buflen
,
350 if (status
!= NSS_STATUS_SUCCESS
)
354 This is
not implemented because it is
not possible to use the current
355 source from bind in a multi
-threaded program
.
358 result
->h_addrtype
= af
;
359 result
->h_length
= len
;
360 bcopy (addr
, host_data
->host_addr
, len
);
361 host_data
->h_addr_ptrs
[0] = (char *) host_data
->host_addr
;
362 host_data
->h_addr_ptrs
[1] = NULL
;
363 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
))
365 map_v4v6_address ((char *) host_data
->host_addr
,
366 (char *) host_data
->host_addr
);
367 result
->h_addrtype
= AF_INET6
;
368 result
->h_length
= IN6ADDRSZ
;
370 *h_errnop
= NETDB_SUCCESS
;
371 return NSS_STATUS_SUCCESS
;
375 static enum nss_status
376 getanswer_r (const querybuf
*answer
, int anslen
, const char *qname
, int qtype
,
377 struct hostent
*result
, char *buffer
, int buflen
, int *h_errnop
)
381 char *aliases
[MAX_NR_ALIASES
];
382 unsigned char host_addr
[16]; /* IPv4 or IPv6 */
383 char *h_addr_ptrs
[MAX_NR_ADDRS
+ 1];
385 } *host_data
= (struct host_data
*) buffer
;
386 int linebuflen
= buflen
- offsetof (struct host_data
, linebuffer
);
387 register const HEADER
*hp
;
388 const u_char
*end_of_message
, *cp
;
389 int n
, ancount
, qdcount
;
390 int haveanswer
, had_error
;
391 char *bp
, **ap
, **hap
;
394 int (*name_ok
) __P ((const char *));
397 result
->h_name
= NULL
;
398 end_of_message
= answer
->buf
+ anslen
;
409 return NSS_STATUS_UNAVAIL
; /* XXX should be abort(); */
413 * find first satisfactory answer
416 bp
= host_data
->linebuffer
;
417 ancount
= ntohs (hp
->ancount
);
418 qdcount
= ntohs (hp
->qdcount
);
419 cp
= answer
->buf
+ HFIXEDSZ
;
422 *h_errnop
= NO_RECOVERY
;
423 return NSS_STATUS_UNAVAIL
;
426 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
427 if (n
< 0 || (*name_ok
) (bp
) == 0)
429 *h_errnop
= NO_RECOVERY
;
430 return NSS_STATUS_UNAVAIL
;
434 if (qtype
== T_A
|| qtype
== T_AAAA
)
436 /* res_send() has already verified that the query name is the
437 * same as the one we sent; this just gets the expanded name
438 * (i.e., with the succeeding search-domain tacked on).
440 n
= strlen (bp
) + 1; /* for the \0 */
444 /* The qname can be abbreviated, but h_name is now absolute. */
445 qname
= result
->h_name
;
448 ap
= host_data
->aliases
;
450 result
->h_aliases
= host_data
->aliases
;
451 hap
= host_data
->h_addr_ptrs
;
453 result
->h_addr_list
= host_data
->h_addr_ptrs
;
457 while (ancount
-- > 0 && cp
< end_of_message
&& had_error
== 0)
461 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
462 if (n
< 0 || (*name_ok
) (bp
) == 0)
468 type
= _getshort (cp
);
469 cp
+= INT16SZ
; /* type */
470 class = _getshort(cp
);
471 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
473 cp
+= INT16SZ
; /* len */
476 /* XXX - debug? syslog? */
478 continue; /* XXX - had_error++ ? */
481 if ((qtype
==T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
)
483 if (ap
>= &host_data
->aliases
[MAX_NR_ALIASES
- 1])
485 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
486 if (n
< 0 || (*name_ok
) (tbuf
) == 0)
494 n
= strlen (bp
) + 1; /* For the \0. */
497 /* Get canonical name. */
498 n
= strlen (tbuf
) + 1; /* For the \0. */
511 if (qtype
== T_PTR
&& type
== T_CNAME
)
513 n
= dn_expand (answer
->buf
, end_of_message
, cp
, tbuf
, sizeof tbuf
);
514 if (n
< 0 || res_hnok (tbuf
) == 0)
520 /* Get canonical name. */
521 n
= strlen (tbuf
) + 1; /* For the \0. */
535 syslog (LOG_NOTICE
| LOG_AUTH
,
536 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
537 qname
, p_class (C_IN
), p_type (qtype
), p_type (type
));
539 continue; /* XXX - had_error++ ? */
545 if (strcasecmp (tname
, bp
) != 0)
547 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, qname
, bp
);
549 continue; /* XXX - had_error++ ? */
551 n
= dn_expand (answer
->buf
, end_of_message
, cp
, bp
, linebuflen
);
552 if (n
< 0 || res_hnok (bp
) == 0)
557 #if MULTI_PTRS_ARE_ALIASES
561 else if (ap
< &host_data
->aliases
[MAXALIASES
-1])
567 n
= strlen (bp
) + 1; /* for the \0 */
574 if (_res
.options
& RES_USE_INET6
)
576 n
= strlen (bp
) + 1; /* for the \0 */
579 map_v4v6_hostent (result
, &bp
, &linebuflen
);
581 *h_errnop
= NETDB_SUCCESS
;
582 return NSS_STATUS_SUCCESS
;
586 if (strcasecmp (result
->h_name
, bp
) != 0)
588 syslog (LOG_NOTICE
| LOG_AUTH
, AskedForGot
, result
->h_name
, bp
);
590 continue; /* XXX - had_error++ ? */
594 if (n
!= result
->h_length
)
605 nn
= strlen (bp
) + 1; /* for the \0 */
610 bp
+= sizeof (align
) - ((u_long
) bp
% sizeof (align
));
617 if (hap
>= &host_data
->h_addr_ptrs
[MAX_NR_ADDRS
-1])
622 bcopy (cp
, *hap
++ = bp
, n
);
638 #if defined(RESOLVSORT)
640 * Note: we sort even if host can take only one address
641 * in its return structures - should give it the "best"
642 * address in that case, not some random one
644 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
645 addrsort (host_data
->h_addr_ptrs
, haveanswer
);
646 #endif /*RESOLVSORT*/
648 if (result
->h_name
== NULL
)
650 n
= strlen (qname
) + 1; /* For the \0. */
659 if (_res
.options
& RES_USE_INET6
)
660 map_v4v6_hostent (result
, &bp
, &linebuflen
);
661 *h_errnop
= NETDB_SUCCESS
;
662 return NSS_STATUS_SUCCESS
;
665 *h_errnop
= TRY_AGAIN
;
666 return NSS_STATUS_TRYAGAIN
;